org.el 643 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244
  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.30trans
  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.30trans"
  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* ((version org-version)
  96. (git-version)
  97. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  98. (if (and (file-exists-p (expand-file-name ".git" dir))
  99. (executable-find "git"))
  100. (let ((pwd (substring (pwd) 10)))
  101. (cd dir)
  102. (if (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  103. (save-excursion
  104. (set-buffer "*Shell Command Output*")
  105. (goto-char (point-min))
  106. (re-search-forward "[^\n]+")
  107. (setq git-version (match-string 0))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (shell-command "git diff-index --name-only HEAD --")
  110. (unless (eql 1 (point-max))
  111. (setq git-version (concat git-version ".dirty")))
  112. (setq version (concat version " (" git-version ")")))
  113. (cd pwd))))
  114. (setq version (format "Org-mode version %s" version))
  115. (if here (insert version))
  116. (message version)
  117. 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-load-hook nil
  127. "Hook that is run after org.el has been loaded."
  128. :group 'org
  129. :type 'hook)
  130. (defvar org-modules) ; defined below
  131. (defvar org-modules-loaded nil
  132. "Have the modules been loaded already?")
  133. (defun org-load-modules-maybe (&optional force)
  134. "Load all extensions listed in `org-modules'."
  135. (when (or force (not org-modules-loaded))
  136. (mapc (lambda (ext)
  137. (condition-case nil (require ext)
  138. (error (message "Problems while trying to load feature `%s'" ext))))
  139. org-modules)
  140. (setq org-modules-loaded t)))
  141. (defun org-set-modules (var value)
  142. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  143. (set var value)
  144. (when (featurep 'org)
  145. (org-load-modules-maybe 'force)))
  146. (when (org-bound-and-true-p org-modules)
  147. (let ((a (member 'org-infojs org-modules)))
  148. (and a (setcar a 'org-jsinfo))))
  149. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  150. "Modules that should always be loaded together with org.el.
  151. If a description starts with <C>, the file is not part of Emacs
  152. and loading it will require that you have downloaded and properly installed
  153. the org-mode distribution.
  154. You can also use this system to load external packages (i.e. neither Org
  155. core modules, nor modules from the CONTRIB directory). Just add symbols
  156. to the end of the list. If the package is called org-xyz.el, then you need
  157. to add the symbol `xyz', and the package must have a call to
  158. (provide 'org-xyz)"
  159. :group 'org
  160. :set 'org-set-modules
  161. :type
  162. '(set :greedy t
  163. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  164. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  165. (const :tag " crypt: Encryption of subtrees" org-crypt)
  166. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  167. (const :tag " id: Global IDs for identifying entries" org-id)
  168. (const :tag " info: Links to Info nodes" org-info)
  169. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  170. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  171. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  172. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  173. (const :tag " mew Links to Mew folders/messages" org-mew)
  174. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  175. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  176. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  177. (const :tag " vm: Links to VM folders/messages" org-vm)
  178. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  179. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  180. (const :tag " mouse: Additional mouse support" org-mouse)
  181. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  182. (const :tag "C annotation-helper: Call Remember directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-annotation-helper)
  183. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  184. (const :tag "C browser-url: Store link, directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-browser-url)
  185. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  186. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  187. (const :tag "C collector: Collect properties into tables" org-collector)
  188. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  189. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  190. (const :tag "C eval: Include command output as text" org-eval)
  191. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  192. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  193. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  194. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  195. (const :tag "C invoice Help manage client invoices in OrgMode" org-invoice)
  196. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  197. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  198. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  199. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  200. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  201. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  202. (const :tag "C R: Computation using the R language" org-R)
  203. (const :tag "C registry: A registry for Org links" org-registry)
  204. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  205. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  206. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  207. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  208. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  209. (const :tag "C track: Keep up with Org development" org-track)
  210. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  211. (defcustom org-support-shift-select nil
  212. "Non-nil means, make shift-cursor commands select text when possible.
  213. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  214. selecting a region, or enlarge thusly regions started in this way.
  215. In Org-mode, in special contexts, these same keys are used for other
  216. purposes, important enough to compete with shift selection. Org tries
  217. to balance these needs by supporting `shift-select-mode' outside these
  218. special contexts, under control of this variable.
  219. The default of this variable is nil, to avoid confusing behavior. Shifted
  220. cursor keys will then execute Org commands in the following contexts:
  221. - on a headline, changing TODO state (left/right) and priority (up/down)
  222. - on a time stamp, changing the time
  223. - in a plain list item, changing the bullet type
  224. - in a property definition line, switching between allowed values
  225. - in the BEGIN line of a clock table (changing the time block).
  226. Outside these contexts, the commands will throw an error.
  227. When this variable is t and the cursor is not in a special context,
  228. Org-mode will support shift-selection for making and enlarging regions.
  229. To make this more effective, the bullet cycling will no longer happen
  230. anywhere in an item line, but only if the cursor is exactly on the bullet.
  231. If you set this variable to the symbol `always', then the keys
  232. will not be special in headlines, property lines, and item lines, to make
  233. shift selection work there as well. If this is what you want, you can
  234. use the following alternative commands: `C-c C-t' and `C-c ,' to
  235. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  236. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  237. edited by hand or in column view.
  238. However, when the cursor is on a timestamp, shift-cursor commands
  239. will still edit the time stamp - this is just too good to give up.
  240. XEmacs user should have this variable set to nil, because shift-select-mode
  241. is Emacs 23 only."
  242. :group 'org
  243. :type '(choice
  244. (const :tag "Never" nil)
  245. (const :tag "When outside special context" t)
  246. (const :tag "Everywhere except timestamps" always)))
  247. (defgroup org-startup nil
  248. "Options concerning startup of Org-mode."
  249. :tag "Org Startup"
  250. :group 'org)
  251. (defcustom org-startup-folded t
  252. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  253. This can also be configured on a per-file basis by adding one of
  254. the following lines anywhere in the buffer:
  255. #+STARTUP: fold (or `overview', this is equivalent)
  256. #+STARTUP: nofold (or `showall', this is equivalent)
  257. #+STARTUP: content
  258. #+STARTUP: showeverything"
  259. :group 'org-startup
  260. :type '(choice
  261. (const :tag "nofold: show all" nil)
  262. (const :tag "fold: overview" t)
  263. (const :tag "content: all headlines" content)
  264. (const :tag "show everything, even drawers" showeverything)))
  265. (defcustom org-startup-truncated t
  266. "Non-nil means, entering Org-mode will set `truncate-lines'.
  267. This is useful since some lines containing links can be very long and
  268. uninteresting. Also tables look terrible when wrapped."
  269. :group 'org-startup
  270. :type 'boolean)
  271. (defcustom org-startup-indented nil
  272. "Non-nil means, turn on `org-indent-mode' on startup.
  273. This can also be configured on a per-file basis by adding one of
  274. the following lines anywhere in the buffer:
  275. #+STARTUP: indent
  276. #+STARTUP: noindent"
  277. :group 'org-structure
  278. :type '(choice
  279. (const :tag "Not" nil)
  280. (const :tag "Globally (slow on startup in large files)" t)))
  281. (defcustom org-startup-align-all-tables nil
  282. "Non-nil means, align all tables when visiting a file.
  283. This is useful when the column width in tables is forced with <N> cookies
  284. in table fields. Such tables will look correct only after the first re-align.
  285. This can also be configured on a per-file basis by adding one of
  286. the following lines anywhere in the buffer:
  287. #+STARTUP: align
  288. #+STARTUP: noalign"
  289. :group 'org-startup
  290. :type 'boolean)
  291. (defcustom org-insert-mode-line-in-empty-file nil
  292. "Non-nil means insert the first line setting Org-mode in empty files.
  293. When the function `org-mode' is called interactively in an empty file, this
  294. normally means that the file name does not automatically trigger Org-mode.
  295. To ensure that the file will always be in Org-mode in the future, a
  296. line enforcing Org-mode will be inserted into the buffer, if this option
  297. has been set."
  298. :group 'org-startup
  299. :type 'boolean)
  300. (defcustom org-replace-disputed-keys nil
  301. "Non-nil means use alternative key bindings for some keys.
  302. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  303. These keys are also used by other packages like shift-selection-mode'
  304. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  305. If you want to use Org-mode together with one of these other modes,
  306. or more generally if you would like to move some Org-mode commands to
  307. other keys, set this variable and configure the keys with the variable
  308. `org-disputed-keys'.
  309. This option is only relevant at load-time of Org-mode, and must be set
  310. *before* org.el is loaded. Changing it requires a restart of Emacs to
  311. become effective."
  312. :group 'org-startup
  313. :type 'boolean)
  314. (defcustom org-use-extra-keys nil
  315. "Non-nil means use extra key sequence definitions for certain
  316. commands. This happens automatically if you run XEmacs or if
  317. window-system is nil. This variable lets you do the same
  318. manually. You must set it before loading org.
  319. Example: on Carbon Emacs 22 running graphically, with an external
  320. keyboard on a Powerbook, the default way of setting M-left might
  321. not work for either Alt or ESC. Setting this variable will make
  322. it work for ESC."
  323. :group 'org-startup
  324. :type 'boolean)
  325. (if (fboundp 'defvaralias)
  326. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  327. (defcustom org-disputed-keys
  328. '(([(shift up)] . [(meta p)])
  329. ([(shift down)] . [(meta n)])
  330. ([(shift left)] . [(meta -)])
  331. ([(shift right)] . [(meta +)])
  332. ([(control shift right)] . [(meta shift +)])
  333. ([(control shift left)] . [(meta shift -)]))
  334. "Keys for which Org-mode and other modes compete.
  335. This is an alist, cars are the default keys, second element specifies
  336. the alternative to use when `org-replace-disputed-keys' is t.
  337. Keys can be specified in any syntax supported by `define-key'.
  338. The value of this option takes effect only at Org-mode's startup,
  339. therefore you'll have to restart Emacs to apply it after changing."
  340. :group 'org-startup
  341. :type 'alist)
  342. (defun org-key (key)
  343. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  344. Or return the original if not disputed."
  345. (if org-replace-disputed-keys
  346. (let* ((nkey (key-description key))
  347. (x (org-find-if (lambda (x)
  348. (equal (key-description (car x)) nkey))
  349. org-disputed-keys)))
  350. (if x (cdr x) key))
  351. key))
  352. (defun org-find-if (predicate seq)
  353. (catch 'exit
  354. (while seq
  355. (if (funcall predicate (car seq))
  356. (throw 'exit (car seq))
  357. (pop seq)))))
  358. (defun org-defkey (keymap key def)
  359. "Define a key, possibly translated, as returned by `org-key'."
  360. (define-key keymap (org-key key) def))
  361. (defcustom org-ellipsis nil
  362. "The ellipsis to use in the Org-mode outline.
  363. When nil, just use the standard three dots. When a string, use that instead,
  364. When a face, use the standard 3 dots, but with the specified face.
  365. The change affects only Org-mode (which will then use its own display table).
  366. Changing this requires executing `M-x org-mode' in a buffer to become
  367. effective."
  368. :group 'org-startup
  369. :type '(choice (const :tag "Default" nil)
  370. (face :tag "Face" :value org-warning)
  371. (string :tag "String" :value "...#")))
  372. (defvar org-display-table nil
  373. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  374. (defgroup org-keywords nil
  375. "Keywords in Org-mode."
  376. :tag "Org Keywords"
  377. :group 'org)
  378. (defcustom org-deadline-string "DEADLINE:"
  379. "String to mark deadline entries.
  380. A deadline is this string, followed by a time stamp. Should be a word,
  381. terminated by a colon. You can insert a schedule keyword and
  382. a timestamp with \\[org-deadline].
  383. Changes become only effective after restarting Emacs."
  384. :group 'org-keywords
  385. :type 'string)
  386. (defcustom org-scheduled-string "SCHEDULED:"
  387. "String to mark scheduled TODO entries.
  388. A schedule is this string, followed by a time stamp. Should be a word,
  389. terminated by a colon. You can insert a schedule keyword and
  390. a timestamp with \\[org-schedule].
  391. Changes become only effective after restarting Emacs."
  392. :group 'org-keywords
  393. :type 'string)
  394. (defcustom org-closed-string "CLOSED:"
  395. "String used as the prefix for timestamps logging closing a TODO entry."
  396. :group 'org-keywords
  397. :type 'string)
  398. (defcustom org-clock-string "CLOCK:"
  399. "String used as prefix for timestamps clocking work hours on an item."
  400. :group 'org-keywords
  401. :type 'string)
  402. (defcustom org-comment-string "COMMENT"
  403. "Entries starting with this keyword will never be exported.
  404. An entry can be toggled between COMMENT and normal with
  405. \\[org-toggle-comment].
  406. Changes become only effective after restarting Emacs."
  407. :group 'org-keywords
  408. :type 'string)
  409. (defcustom org-quote-string "QUOTE"
  410. "Entries starting with this keyword will be exported in fixed-width font.
  411. Quoting applies only to the text in the entry following the headline, and does
  412. not extend beyond the next headline, even if that is lower level.
  413. An entry can be toggled between QUOTE and normal with
  414. \\[org-toggle-fixed-width-section]."
  415. :group 'org-keywords
  416. :type 'string)
  417. (defconst org-repeat-re
  418. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  419. "Regular expression for specifying repeated events.
  420. After a match, group 1 contains the repeat expression.")
  421. (defgroup org-structure nil
  422. "Options concerning the general structure of Org-mode files."
  423. :tag "Org Structure"
  424. :group 'org)
  425. (defgroup org-reveal-location nil
  426. "Options about how to make context of a location visible."
  427. :tag "Org Reveal Location"
  428. :group 'org-structure)
  429. (defconst org-context-choice
  430. '(choice
  431. (const :tag "Always" t)
  432. (const :tag "Never" nil)
  433. (repeat :greedy t :tag "Individual contexts"
  434. (cons
  435. (choice :tag "Context"
  436. (const agenda)
  437. (const org-goto)
  438. (const occur-tree)
  439. (const tags-tree)
  440. (const link-search)
  441. (const mark-goto)
  442. (const bookmark-jump)
  443. (const isearch)
  444. (const default))
  445. (boolean))))
  446. "Contexts for the reveal options.")
  447. (defcustom org-show-hierarchy-above '((default . t))
  448. "Non-nil means, show full hierarchy when revealing a location.
  449. Org-mode often shows locations in an org-mode file which might have
  450. been invisible before. When this is set, the hierarchy of headings
  451. above the exposed location is shown.
  452. Turning this off for example for sparse trees makes them very compact.
  453. Instead of t, this can also be an alist specifying this option for different
  454. contexts. Valid contexts are
  455. agenda when exposing an entry from the agenda
  456. org-goto when using the command `org-goto' on key C-c C-j
  457. occur-tree when using the command `org-occur' on key C-c /
  458. tags-tree when constructing a sparse tree based on tags matches
  459. link-search when exposing search matches associated with a link
  460. mark-goto when exposing the jump goal of a mark
  461. bookmark-jump when exposing a bookmark location
  462. isearch when exiting from an incremental search
  463. default default for all contexts not set explicitly"
  464. :group 'org-reveal-location
  465. :type org-context-choice)
  466. (defcustom org-show-following-heading '((default . nil))
  467. "Non-nil means, show following heading when revealing a location.
  468. Org-mode often shows locations in an org-mode file which might have
  469. been invisible before. When this is set, the heading following the
  470. match is shown.
  471. Turning this off for example for sparse trees makes them very compact,
  472. but makes it harder to edit the location of the match. In such a case,
  473. use the command \\[org-reveal] to show more context.
  474. Instead of t, this can also be an alist specifying this option for different
  475. contexts. See `org-show-hierarchy-above' for valid contexts."
  476. :group 'org-reveal-location
  477. :type org-context-choice)
  478. (defcustom org-show-siblings '((default . nil) (isearch t))
  479. "Non-nil means, show all sibling heading when revealing a location.
  480. Org-mode often shows locations in an org-mode file which might have
  481. been invisible before. When this is set, the sibling of the current entry
  482. heading are all made visible. If `org-show-hierarchy-above' is t,
  483. the same happens on each level of the hierarchy above the current entry.
  484. By default this is on for the isearch context, off for all other contexts.
  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-entry-below '((default . nil))
  493. "Non-nil means, show the entry below a headline 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 text below the headline that is
  496. exposed is also shown.
  497. By default this is off for all contexts.
  498. Instead of t, this can also be an alist specifying this option for different
  499. contexts. See `org-show-hierarchy-above' for valid contexts."
  500. :group 'org-reveal-location
  501. :type org-context-choice)
  502. (defcustom org-indirect-buffer-display 'other-window
  503. "How should indirect tree buffers be displayed?
  504. This applies to indirect buffers created with the commands
  505. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  506. Valid values are:
  507. current-window Display in the current window
  508. other-window Just display in another window.
  509. dedicated-frame Create one new frame, and re-use it each time.
  510. new-frame Make a new frame each time. Note that in this case
  511. previously-made indirect buffers are kept, and you need to
  512. kill these buffers yourself."
  513. :group 'org-structure
  514. :group 'org-agenda-windows
  515. :type '(choice
  516. (const :tag "In current window" current-window)
  517. (const :tag "In current frame, other window" other-window)
  518. (const :tag "Each time a new frame" new-frame)
  519. (const :tag "One dedicated frame" dedicated-frame)))
  520. (defgroup org-cycle nil
  521. "Options concerning visibility cycling in Org-mode."
  522. :tag "Org Cycle"
  523. :group 'org-structure)
  524. (defcustom org-cycle-skip-children-state-if-no-children t
  525. "Non-nil means, skip CHILDREN state in entries that don't have any."
  526. :group 'org-cycle
  527. :type 'boolean)
  528. (defcustom org-cycle-max-level nil
  529. "Maximum level which should still be subject to visibility cycling.
  530. Levels higher than this will, for cycling, be treated as text, not a headline.
  531. When `org-odd-levels-only' is set, a value of N in this variable actually
  532. means 2N-1 stars as the limiting headline.
  533. When nil, cycle all levels.
  534. Note that the limiting level of cycling is also influenced by
  535. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  536. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  537. than its value."
  538. :group 'org-cycle
  539. :type '(choice
  540. (const :tag "No limit" nil)
  541. (integer :tag "Maximum level")))
  542. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  543. "Names of drawers. Drawers are not opened by cycling on the headline above.
  544. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  545. this:
  546. :DRAWERNAME:
  547. .....
  548. :END:
  549. The drawer \"PROPERTIES\" is special for capturing properties through
  550. the property API.
  551. Drawers can be defined on the per-file basis with a line like:
  552. #+DRAWERS: HIDDEN STATE PROPERTIES"
  553. :group 'org-structure
  554. :group 'org-cycle
  555. :type '(repeat (string :tag "Drawer Name")))
  556. (defcustom org-hide-block-startup nil
  557. "Non-nil means, , entering Org-mode will fold all blocks.
  558. This can also be set in on a per-file basis with
  559. #+STARTUP: hideblocks
  560. #+STARTUP: showblocks"
  561. :group 'org-startup
  562. :group 'org-cycle
  563. :type 'boolean)
  564. (defcustom org-cycle-global-at-bob nil
  565. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  566. This makes it possible to do global cycling without having to use S-TAB or
  567. C-u TAB. For this special case to work, the first line of the buffer
  568. must not be a headline - it may be empty or some other text. When used in
  569. this way, `org-cycle-hook' is disables temporarily, to make sure the
  570. cursor stays at the beginning of the buffer.
  571. When this option is nil, don't do anything special at the beginning
  572. of the buffer."
  573. :group 'org-cycle
  574. :type 'boolean)
  575. (defcustom org-cycle-emulate-tab t
  576. "Where should `org-cycle' emulate TAB.
  577. nil Never
  578. white Only in completely white lines
  579. whitestart Only at the beginning of lines, before the first non-white char
  580. t Everywhere except in headlines
  581. exc-hl-bol Everywhere except at the start of a headline
  582. If TAB is used in a place where it does not emulate TAB, the current subtree
  583. visibility is cycled."
  584. :group 'org-cycle
  585. :type '(choice (const :tag "Never" nil)
  586. (const :tag "Only in completely white lines" white)
  587. (const :tag "Before first char in a line" whitestart)
  588. (const :tag "Everywhere except in headlines" t)
  589. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  590. ))
  591. (defcustom org-cycle-separator-lines 2
  592. "Number of empty lines needed to keep an empty line between collapsed trees.
  593. If you leave an empty line between the end of a subtree and the following
  594. headline, this empty line is hidden when the subtree is folded.
  595. Org-mode will leave (exactly) one empty line visible if the number of
  596. empty lines is equal or larger to the number given in this variable.
  597. So the default 2 means, at least 2 empty lines after the end of a subtree
  598. are needed to produce free space between a collapsed subtree and the
  599. following headline.
  600. If the number is negative, and the number of empty lines is at least -N,
  601. all empty lines are shown.
  602. Special case: when 0, never leave empty lines in collapsed view."
  603. :group 'org-cycle
  604. :type 'integer)
  605. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  606. (defcustom org-pre-cycle-hook nil
  607. "Hook that is run before visibility cycling is happening.
  608. The function(s) in this hook must accept a single argument which indicates
  609. the new state that will be set right after running this hook. The
  610. argument is a symbol. Before a global state change, it can have the values
  611. `overview', `content', or `all'. Before a local state change, it can have
  612. the values `folded', `children', or `subtree'."
  613. :group 'org-cycle
  614. :type 'hook)
  615. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  616. org-cycle-hide-drawers
  617. org-cycle-show-empty-lines
  618. org-optimize-window-after-visibility-change)
  619. "Hook that is run after `org-cycle' has changed the buffer visibility.
  620. The function(s) in this hook must accept a single argument which indicates
  621. the new state that was set by the most recent `org-cycle' command. The
  622. argument is a symbol. After a global state change, it can have the values
  623. `overview', `content', or `all'. After a local state change, it can have
  624. the values `folded', `children', or `subtree'."
  625. :group 'org-cycle
  626. :type 'hook)
  627. (defgroup org-edit-structure nil
  628. "Options concerning structure editing in Org-mode."
  629. :tag "Org Edit Structure"
  630. :group 'org-structure)
  631. (defcustom org-odd-levels-only nil
  632. "Non-nil means, skip even levels and only use odd levels for the outline.
  633. This has the effect that two stars are being added/taken away in
  634. promotion/demotion commands. It also influences how levels are
  635. handled by the exporters.
  636. Changing it requires restart of `font-lock-mode' to become effective
  637. for fontification also in regions already fontified.
  638. You may also set this on a per-file basis by adding one of the following
  639. lines to the buffer:
  640. #+STARTUP: odd
  641. #+STARTUP: oddeven"
  642. :group 'org-edit-structure
  643. :group 'org-font-lock
  644. :type 'boolean)
  645. (defcustom org-adapt-indentation t
  646. "Non-nil means, adapt indentation to outline node level.
  647. When this variable is set, Org assumes that you write outlines by
  648. indenting text in each node to align with the headline (after the stars).
  649. The following issues are influenced by this variable:
  650. - When this is set and the *entire* text in an entry is indented, the
  651. indentation is increased by one space in a demotion command, and
  652. decreased by one in a promotion command. If any line in the entry
  653. body starts with text at column 0, indentation is not changed at all.
  654. - Property drawers and planning information is inserted indented when
  655. this variable s set. When nil, they will not be indented.
  656. - TAB indents a line relative to context. The lines below a headline
  657. will be indented when this variable is set.
  658. Note that this is all about true indentation, by adding and removing
  659. space characters. See also `org-indent.el' which does level-dependent
  660. indentation in a virtual way, i.e. at display time in Emacs."
  661. :group 'org-edit-structure
  662. :type 'boolean)
  663. (defcustom org-special-ctrl-a/e nil
  664. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  665. When t, `C-a' will bring back the cursor to the beginning of the
  666. headline text, i.e. after the stars and after a possible TODO keyword.
  667. In an item, this will be the position after the bullet.
  668. When the cursor is already at that position, another `C-a' will bring
  669. it to the beginning of the line.
  670. `C-e' will jump to the end of the headline, ignoring the presence of tags
  671. in the headline. A second `C-e' will then jump to the true end of the
  672. line, after any tags.
  673. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  674. going to the true line boundary first. Only a directly following, identical
  675. keypress will bring the cursor to the special positions.
  676. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  677. set separately."
  678. :group 'org-edit-structure
  679. :type '(choice
  680. (const :tag "off" nil)
  681. (const :tag "after stars/bullet and before tags first" t)
  682. (const :tag "true line boundary first" reversed)
  683. (cons :tag "Set C-a and C-e separately"
  684. (choice :tag "Special C-a"
  685. (const :tag "off" nil)
  686. (const :tag "after stars/bullet first" t)
  687. (const :tag "before stars/bullet first" reversed))
  688. (choice :tag "Special C-e"
  689. (const :tag "off" nil)
  690. (const :tag "before tags first" t)
  691. (const :tag "after tags first" reversed)))))
  692. (if (fboundp 'defvaralias)
  693. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  694. (defcustom org-special-ctrl-k nil
  695. "Non-nil means `C-k' will behave specially in headlines.
  696. When nil, `C-k' will call the default `kill-line' command.
  697. When t, the following will happen while the cursor is in the headline:
  698. - When the cursor is at the beginning of a headline, kill the entire
  699. line and possible the folded subtree below the line.
  700. - When in the middle of the headline text, kill the headline up to the tags.
  701. - When after the headline text, kill the tags."
  702. :group 'org-edit-structure
  703. :type 'boolean)
  704. (defcustom org-yank-folded-subtrees t
  705. "Non-nil means, when yanking subtrees, fold them.
  706. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  707. it starts with a heading and all other headings in it are either children
  708. or siblings, then fold all the subtrees. However, do this only if no
  709. text after the yank would be swallowed into a folded tree by this action."
  710. :group 'org-edit-structure
  711. :type 'boolean)
  712. (defcustom org-yank-adjusted-subtrees nil
  713. "Non-nil means, when yanking subtrees, adjust the level.
  714. With this setting, `org-paste-subtree' is used to insert the subtree, see
  715. this function for details."
  716. :group 'org-edit-structure
  717. :type 'boolean)
  718. (defcustom org-M-RET-may-split-line '((default . t))
  719. "Non-nil means, M-RET will split the line at the cursor position.
  720. When nil, it will go to the end of the line before making a
  721. new line.
  722. You may also set this option in a different way for different
  723. contexts. Valid contexts are:
  724. headline when creating a new headline
  725. item when creating a new item
  726. table in a table field
  727. default the value to be used for all contexts not explicitly
  728. customized"
  729. :group 'org-structure
  730. :group 'org-table
  731. :type '(choice
  732. (const :tag "Always" t)
  733. (const :tag "Never" nil)
  734. (repeat :greedy t :tag "Individual contexts"
  735. (cons
  736. (choice :tag "Context"
  737. (const headline)
  738. (const item)
  739. (const table)
  740. (const default))
  741. (boolean)))))
  742. (defcustom org-insert-heading-respect-content nil
  743. "Non-nil means, insert new headings after the current subtree.
  744. When nil, the new heading is created directly after the current line.
  745. The commands \\[org-insert-heading-respect-content] and
  746. \\[org-insert-todo-heading-respect-content] turn this variable on
  747. for the duration of the command."
  748. :group 'org-structure
  749. :type 'boolean)
  750. (defcustom org-blank-before-new-entry '((heading . auto)
  751. (plain-list-item . auto))
  752. "Should `org-insert-heading' leave a blank line before new heading/item?
  753. The value is an alist, with `heading' and `plain-list-item' as car,
  754. and a boolean flag as cdr. For plain lists, if the variable
  755. `org-empty-line-terminates-plain-lists' is set, the setting here
  756. is ignored and no empty line is inserted, to keep the list in tact."
  757. :group 'org-edit-structure
  758. :type '(list
  759. (cons (const heading)
  760. (choice (const :tag "Never" nil)
  761. (const :tag "Always" t)
  762. (const :tag "Auto" auto)))
  763. (cons (const plain-list-item)
  764. (choice (const :tag "Never" nil)
  765. (const :tag "Always" t)
  766. (const :tag "Auto" auto)))))
  767. (defcustom org-insert-heading-hook nil
  768. "Hook being run after inserting a new heading."
  769. :group 'org-edit-structure
  770. :type 'hook)
  771. (defcustom org-enable-fixed-width-editor t
  772. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  773. This currently only means, they are never auto-wrapped.
  774. When nil, such lines will be treated like ordinary lines.
  775. See also the QUOTE keyword."
  776. :group 'org-edit-structure
  777. :type 'boolean)
  778. (defcustom org-goto-auto-isearch t
  779. "Non-nil means, typing characters in org-goto starts incremental search."
  780. :group 'org-edit-structure
  781. :type 'boolean)
  782. (defgroup org-sparse-trees nil
  783. "Options concerning sparse trees in Org-mode."
  784. :tag "Org Sparse Trees"
  785. :group 'org-structure)
  786. (defcustom org-highlight-sparse-tree-matches t
  787. "Non-nil means, highlight all matches that define a sparse tree.
  788. The highlights will automatically disappear the next time the buffer is
  789. changed by an edit command."
  790. :group 'org-sparse-trees
  791. :type 'boolean)
  792. (defcustom org-remove-highlights-with-change t
  793. "Non-nil means, any change to the buffer will remove temporary highlights.
  794. Such highlights are created by `org-occur' and `org-clock-display'.
  795. When nil, `C-c C-c needs to be used to get rid of the highlights.
  796. The highlights created by `org-preview-latex-fragment' always need
  797. `C-c C-c' to be removed."
  798. :group 'org-sparse-trees
  799. :group 'org-time
  800. :type 'boolean)
  801. (defcustom org-occur-hook '(org-first-headline-recenter)
  802. "Hook that is run after `org-occur' has constructed a sparse tree.
  803. This can be used to recenter the window to show as much of the structure
  804. as possible."
  805. :group 'org-sparse-trees
  806. :type 'hook)
  807. (defgroup org-imenu-and-speedbar nil
  808. "Options concerning imenu and speedbar in Org-mode."
  809. :tag "Org Imenu and Speedbar"
  810. :group 'org-structure)
  811. (defcustom org-imenu-depth 2
  812. "The maximum level for Imenu access to Org-mode headlines.
  813. This also applied for speedbar access."
  814. :group 'org-imenu-and-speedbar
  815. :type 'integer)
  816. (defgroup org-table nil
  817. "Options concerning tables in Org-mode."
  818. :tag "Org Table"
  819. :group 'org)
  820. (defcustom org-enable-table-editor 'optimized
  821. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  822. When nil, such lines will be treated like ordinary lines.
  823. When equal to the symbol `optimized', the table editor will be optimized to
  824. do the following:
  825. - Automatic overwrite mode in front of whitespace in table fields.
  826. This makes the structure of the table stay in tact as long as the edited
  827. field does not exceed the column width.
  828. - Minimize the number of realigns. Normally, the table is aligned each time
  829. TAB or RET are pressed to move to another field. With optimization this
  830. happens only if changes to a field might have changed the column width.
  831. Optimization requires replacing the functions `self-insert-command',
  832. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  833. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  834. very good at guessing when a re-align will be necessary, but you can always
  835. force one with \\[org-ctrl-c-ctrl-c].
  836. If you would like to use the optimized version in Org-mode, but the
  837. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  838. This variable can be used to turn on and off the table editor during a session,
  839. but in order to toggle optimization, a restart is required.
  840. See also the variable `org-table-auto-blank-field'."
  841. :group 'org-table
  842. :type '(choice
  843. (const :tag "off" nil)
  844. (const :tag "on" t)
  845. (const :tag "on, optimized" optimized)))
  846. (defcustom org-self-insert-cluster-for-undo t
  847. "Non-nil means cluster self-insert commands for undo when possible.
  848. If this is set, then, like in the Emacs command loop, 20 consequtive
  849. characters will be undone together.
  850. This is configurable, because there is some impact on typing performance."
  851. :group 'org-table
  852. :type 'boolean)
  853. (defcustom org-table-tab-recognizes-table.el t
  854. "Non-nil means, TAB will automatically notice a table.el table.
  855. When it sees such a table, it moves point into it and - if necessary -
  856. calls `table-recognize-table'."
  857. :group 'org-table-editing
  858. :type 'boolean)
  859. (defgroup org-link nil
  860. "Options concerning links in Org-mode."
  861. :tag "Org Link"
  862. :group 'org)
  863. (defvar org-link-abbrev-alist-local nil
  864. "Buffer-local version of `org-link-abbrev-alist', which see.
  865. The value of this is taken from the #+LINK lines.")
  866. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  867. (defcustom org-link-abbrev-alist nil
  868. "Alist of link abbreviations.
  869. The car of each element is a string, to be replaced at the start of a link.
  870. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  871. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  872. [[linkkey:tag][description]]
  873. The 'linkkey' must be a word word, starting with a letter, followed
  874. by letters, numbers, '-' or '_'.
  875. If REPLACE is a string, the tag will simply be appended to create the link.
  876. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  877. the placeholder \"%h\" will cause a url-encoded version of the tag to
  878. be inserted at that point (see the function `url-hexify-string').
  879. REPLACE may also be a function that will be called with the tag as the
  880. only argument to create the link, which should be returned as a string.
  881. See the manual for examples."
  882. :group 'org-link
  883. :type '(repeat
  884. (cons
  885. (string :tag "Protocol")
  886. (choice
  887. (string :tag "Format")
  888. (function)))))
  889. (defcustom org-descriptive-links t
  890. "Non-nil means, hide link part and only show description of bracket links.
  891. Bracket links are like [[link][description]]. This variable sets the initial
  892. state in new org-mode buffers. The setting can then be toggled on a
  893. per-buffer basis from the Org->Hyperlinks menu."
  894. :group 'org-link
  895. :type 'boolean)
  896. (defcustom org-link-file-path-type 'adaptive
  897. "How the path name in file links should be stored.
  898. Valid values are:
  899. relative Relative to the current directory, i.e. the directory of the file
  900. into which the link is being inserted.
  901. absolute Absolute path, if possible with ~ for home directory.
  902. noabbrev Absolute path, no abbreviation of home directory.
  903. adaptive Use relative path for files in the current directory and sub-
  904. directories of it. For other files, use an absolute path."
  905. :group 'org-link
  906. :type '(choice
  907. (const relative)
  908. (const absolute)
  909. (const noabbrev)
  910. (const adaptive)))
  911. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  912. "Types of links that should be activated in Org-mode files.
  913. This is a list of symbols, each leading to the activation of a certain link
  914. type. In principle, it does not hurt to turn on most link types - there may
  915. be a small gain when turning off unused link types. The types are:
  916. bracket The recommended [[link][description]] or [[link]] links with hiding.
  917. angular Links in angular brackets that may contain whitespace like
  918. <bbdb:Carsten Dominik>.
  919. plain Plain links in normal text, no whitespace, like http://google.com.
  920. radio Text that is matched by a radio target, see manual for details.
  921. tag Tag settings in a headline (link to tag search).
  922. date Time stamps (link to calendar).
  923. footnote Footnote labels.
  924. Changing this variable requires a restart of Emacs to become effective."
  925. :group 'org-link
  926. :type '(set :greedy t
  927. (const :tag "Double bracket links (new style)" bracket)
  928. (const :tag "Angular bracket links (old style)" angular)
  929. (const :tag "Plain text links" plain)
  930. (const :tag "Radio target matches" radio)
  931. (const :tag "Tags" tag)
  932. (const :tag "Timestamps" date)
  933. (const :tag "Footnotes" footnote)))
  934. (defcustom org-make-link-description-function nil
  935. "Function to use to generate link descriptions from links. If
  936. nil the link location will be used. This function must take two
  937. parameters; the first is the link and the second the description
  938. org-insert-link has generated, and should return the description
  939. to use."
  940. :group 'org-link
  941. :type 'function)
  942. (defgroup org-link-store nil
  943. "Options concerning storing links in Org-mode."
  944. :tag "Org Store Link"
  945. :group 'org-link)
  946. (defcustom org-email-link-description-format "Email %c: %.30s"
  947. "Format of the description part of a link to an email or usenet message.
  948. The following %-escapes will be replaced by corresponding information:
  949. %F full \"From\" field
  950. %f name, taken from \"From\" field, address if no name
  951. %T full \"To\" field
  952. %t first name in \"To\" field, address if no name
  953. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  954. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  955. %s subject
  956. %m message-id.
  957. You may use normal field width specification between the % and the letter.
  958. This is for example useful to limit the length of the subject.
  959. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  960. :group 'org-link-store
  961. :type 'string)
  962. (defcustom org-from-is-user-regexp
  963. (let (r1 r2)
  964. (when (and user-mail-address (not (string= user-mail-address "")))
  965. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  966. (when (and user-full-name (not (string= user-full-name "")))
  967. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  968. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  969. "Regexp matched against the \"From:\" header of an email or usenet message.
  970. It should match if the message is from the user him/herself."
  971. :group 'org-link-store
  972. :type 'regexp)
  973. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  974. "Non-nil means, storing a link to an Org file will use entry IDs.
  975. Note that before this variable is even considered, org-id must be loaded,
  976. so please customize `org-modules' and turn it on.
  977. The variable can have the following values:
  978. t Create an ID if needed to make a link to the current entry.
  979. create-if-interactive
  980. If `org-store-link' is called directly (interactively, as a user
  981. command), do create an ID to support the link. But when doing the
  982. job for remember, only use the ID if it already exists. The
  983. purpose of this setting is to avoid proliferation of unwanted
  984. IDs, just because you happen to be in an Org file when you
  985. call `org-remember' that automatically and preemptively
  986. creates a link. If you do want to get an ID link in a remember
  987. template to an entry not having an ID, create it first by
  988. explicitly creating a link to it, using `C-c C-l' first.
  989. create-if-interactive-and-no-custom-id
  990. Like create-if-interactive, but do not create an ID if there is
  991. a CUSTOM_ID property defined in the entry. This is the default.
  992. use-existing
  993. Use existing ID, do not create one.
  994. nil Never use an ID to make a link, instead link using a text search for
  995. the headline text."
  996. :group 'org-link-store
  997. :type '(choice
  998. (const :tag "Create ID to make link" t)
  999. (const :tag "Create if storing link interactively"
  1000. create-if-interactive)
  1001. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1002. create-if-interactive-and-no-custom-id)
  1003. (const :tag "Only use existing" use-existing)
  1004. (const :tag "Do not use ID to create link" nil)))
  1005. (defcustom org-context-in-file-links t
  1006. "Non-nil means, file links from `org-store-link' contain context.
  1007. A search string will be added to the file name with :: as separator and
  1008. used to find the context when the link is activated by the command
  1009. `org-open-at-point'.
  1010. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1011. negates this setting for the duration of the command."
  1012. :group 'org-link-store
  1013. :type 'boolean)
  1014. (defcustom org-keep-stored-link-after-insertion nil
  1015. "Non-nil means, keep link in list for entire session.
  1016. The command `org-store-link' adds a link pointing to the current
  1017. location to an internal list. These links accumulate during a session.
  1018. The command `org-insert-link' can be used to insert links into any
  1019. Org-mode file (offering completion for all stored links). When this
  1020. option is nil, every link which has been inserted once using \\[org-insert-link]
  1021. will be removed from the list, to make completing the unused links
  1022. more efficient."
  1023. :group 'org-link-store
  1024. :type 'boolean)
  1025. (defgroup org-link-follow nil
  1026. "Options concerning following links in Org-mode."
  1027. :tag "Org Follow Link"
  1028. :group 'org-link)
  1029. (defcustom org-link-translation-function nil
  1030. "Function to translate links with different syntax to Org syntax.
  1031. This can be used to translate links created for example by the Planner
  1032. or emacs-wiki packages to Org syntax.
  1033. The function must accept two parameters, a TYPE containing the link
  1034. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1035. which is everything after the link protocol. It should return a cons
  1036. with possibly modified values of type and path.
  1037. Org contains a function for this, so if you set this variable to
  1038. `org-translate-link-from-planner', you should be able follow many
  1039. links created by planner."
  1040. :group 'org-link-follow
  1041. :type 'function)
  1042. (defcustom org-follow-link-hook nil
  1043. "Hook that is run after a link has been followed."
  1044. :group 'org-link-follow
  1045. :type 'hook)
  1046. (defcustom org-tab-follows-link nil
  1047. "Non-nil means, on links TAB will follow the link.
  1048. Needs to be set before org.el is loaded.
  1049. This really should not be used, it does not make sense, and the
  1050. implementation is bad."
  1051. :group 'org-link-follow
  1052. :type 'boolean)
  1053. (defcustom org-return-follows-link nil
  1054. "Non-nil means, on links RET will follow the link.
  1055. Needs to be set before org.el is loaded."
  1056. :group 'org-link-follow
  1057. :type 'boolean)
  1058. (defcustom org-mouse-1-follows-link
  1059. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1060. "Non-nil means, mouse-1 on a link will follow the link.
  1061. A longer mouse click will still set point. Does not work on XEmacs.
  1062. Needs to be set before org.el is loaded."
  1063. :group 'org-link-follow
  1064. :type 'boolean)
  1065. (defcustom org-mark-ring-length 4
  1066. "Number of different positions to be recorded in the ring
  1067. Changing this requires a restart of Emacs to work correctly."
  1068. :group 'org-link-follow
  1069. :type 'integer)
  1070. (defcustom org-link-frame-setup
  1071. '((vm . vm-visit-folder-other-frame)
  1072. (gnus . gnus-other-frame)
  1073. (file . find-file-other-window))
  1074. "Setup the frame configuration for following links.
  1075. When following a link with Emacs, it may often be useful to display
  1076. this link in another window or frame. This variable can be used to
  1077. set this up for the different types of links.
  1078. For VM, use any of
  1079. `vm-visit-folder'
  1080. `vm-visit-folder-other-frame'
  1081. For Gnus, use any of
  1082. `gnus'
  1083. `gnus-other-frame'
  1084. `org-gnus-no-new-news'
  1085. For FILE, use any of
  1086. `find-file'
  1087. `find-file-other-window'
  1088. `find-file-other-frame'
  1089. For the calendar, use the variable `calendar-setup'.
  1090. For BBDB, it is currently only possible to display the matches in
  1091. another window."
  1092. :group 'org-link-follow
  1093. :type '(list
  1094. (cons (const vm)
  1095. (choice
  1096. (const vm-visit-folder)
  1097. (const vm-visit-folder-other-window)
  1098. (const vm-visit-folder-other-frame)))
  1099. (cons (const gnus)
  1100. (choice
  1101. (const gnus)
  1102. (const gnus-other-frame)
  1103. (const org-gnus-no-new-news)))
  1104. (cons (const file)
  1105. (choice
  1106. (const find-file)
  1107. (const find-file-other-window)
  1108. (const find-file-other-frame)))))
  1109. (defcustom org-display-internal-link-with-indirect-buffer nil
  1110. "Non-nil means, use indirect buffer to display infile links.
  1111. Activating internal links (from one location in a file to another location
  1112. in the same file) normally just jumps to the location. When the link is
  1113. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1114. another window. When this option is set, the other window actually displays
  1115. an indirect buffer clone of the current buffer, to avoid any visibility
  1116. changes to the current buffer."
  1117. :group 'org-link-follow
  1118. :type 'boolean)
  1119. (defcustom org-open-non-existing-files nil
  1120. "Non-nil means, `org-open-file' will open non-existing files.
  1121. When nil, an error will be generated.
  1122. This variable applies only to external applications because they
  1123. might choke on non-existing files. If the link is to a file that
  1124. will be openend in Emacs, the variable is ignored."
  1125. :group 'org-link-follow
  1126. :type 'boolean)
  1127. (defcustom org-open-directory-means-index-dot-org nil
  1128. "Non-nil means, a link to a directory really means to index.org.
  1129. When nil, following a directory link will run dired or open a finder/explorer
  1130. window on that directory."
  1131. :group 'org-link-follow
  1132. :type 'boolean)
  1133. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1134. "Function and arguments to call for following mailto links.
  1135. This is a list with the first element being a lisp function, and the
  1136. remaining elements being arguments to the function. In string arguments,
  1137. %a will be replaced by the address, and %s will be replaced by the subject
  1138. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1139. :group 'org-link-follow
  1140. :type '(choice
  1141. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1142. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1143. (const :tag "message-mail" (message-mail "%a" "%s"))
  1144. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1145. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1146. "Non-nil means, ask for confirmation before executing shell links.
  1147. Shell links can be dangerous: just think about a link
  1148. [[shell:rm -rf ~/*][Google Search]]
  1149. This link would show up in your Org-mode document as \"Google Search\",
  1150. but really it would remove your entire home directory.
  1151. Therefore we advise against setting this variable to nil.
  1152. Just change it to `y-or-n-p' if you want to confirm with a
  1153. single keystroke rather than having to type \"yes\"."
  1154. :group 'org-link-follow
  1155. :type '(choice
  1156. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1157. (const :tag "with y-or-n (faster)" y-or-n-p)
  1158. (const :tag "no confirmation (dangerous)" nil)))
  1159. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1160. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1161. Elisp links can be dangerous: just think about a link
  1162. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1163. This link would show up in your Org-mode document as \"Google Search\",
  1164. but really it would remove your entire home directory.
  1165. Therefore we advise against setting this variable to nil.
  1166. Just change it to `y-or-n-p' if you want to confirm with a
  1167. single keystroke rather than having to type \"yes\"."
  1168. :group 'org-link-follow
  1169. :type '(choice
  1170. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1171. (const :tag "with y-or-n (faster)" y-or-n-p)
  1172. (const :tag "no confirmation (dangerous)" nil)))
  1173. (defconst org-file-apps-defaults-gnu
  1174. '((remote . emacs)
  1175. (system . mailcap)
  1176. (t . mailcap))
  1177. "Default file applications on a UNIX or GNU/Linux system.
  1178. See `org-file-apps'.")
  1179. (defconst org-file-apps-defaults-macosx
  1180. '((remote . emacs)
  1181. (t . "open %s")
  1182. (system . "open %s")
  1183. ("ps.gz" . "gv %s")
  1184. ("eps.gz" . "gv %s")
  1185. ("dvi" . "xdvi %s")
  1186. ("fig" . "xfig %s"))
  1187. "Default file applications on a MacOS X system.
  1188. The system \"open\" is known as a default, but we use X11 applications
  1189. for some files for which the OS does not have a good default.
  1190. See `org-file-apps'.")
  1191. (defconst org-file-apps-defaults-windowsnt
  1192. (list
  1193. '(remote . emacs)
  1194. (cons t
  1195. (list (if (featurep 'xemacs)
  1196. 'mswindows-shell-execute
  1197. 'w32-shell-execute)
  1198. "open" 'file))
  1199. (cons 'system
  1200. (list (if (featurep 'xemacs)
  1201. 'mswindows-shell-execute
  1202. 'w32-shell-execute)
  1203. "open" 'file)))
  1204. "Default file applications on a Windows NT system.
  1205. The system \"open\" is used for most files.
  1206. See `org-file-apps'.")
  1207. (defcustom org-file-apps
  1208. '(
  1209. (auto-mode . emacs)
  1210. ("\\.x?html?\\'" . default)
  1211. ("\\.pdf\\'" . default)
  1212. )
  1213. "External applications for opening `file:path' items in a document.
  1214. Org-mode uses system defaults for different file types, but
  1215. you can use this variable to set the application for a given file
  1216. extension. The entries in this list are cons cells where the car identifies
  1217. files and the cdr the corresponding command. Possible values for the
  1218. file identifier are
  1219. \"regex\" Regular expression matched against the file name. For backward
  1220. compatibility, this can also be a string with only alphanumeric
  1221. characters, which is then interpreted as an extension.
  1222. `directory' Matches a directory
  1223. `remote' Matches a remote file, accessible through tramp or efs.
  1224. Remote files most likely should be visited through Emacs
  1225. because external applications cannot handle such paths.
  1226. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1227. so all files Emacs knows how to handle. Using this with
  1228. command `emacs' will open most files in Emacs. Beware that this
  1229. will also open html files inside Emacs, unless you add
  1230. (\"html\" . default) to the list as well.
  1231. t Default for files not matched by any of the other options.
  1232. `system' The system command to open files, like `open' on Windows
  1233. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1234. that will be selected if you call `C-c C-o' with a double
  1235. `C-u C-u' prefix.
  1236. Possible values for the command are:
  1237. `emacs' The file will be visited by the current Emacs process.
  1238. `default' Use the default application for this file type, which is the
  1239. association for t in the list, most likely in the system-specific
  1240. part.
  1241. This can be used to overrule an unwanted setting in the
  1242. system-specific variable.
  1243. `system' Use the system command for opening files, like \"open\".
  1244. This command is specified by the entry whose car is `system'.
  1245. Most likely, the system-specific version of this variable
  1246. does define this command, but you can overrule/replace it
  1247. here.
  1248. string A command to be executed by a shell; %s will be replaced
  1249. by the path to the file.
  1250. sexp A Lisp form which will be evaluated. The file path will
  1251. be available in the Lisp variable `file'.
  1252. For more examples, see the system specific constants
  1253. `org-file-apps-defaults-macosx'
  1254. `org-file-apps-defaults-windowsnt'
  1255. `org-file-apps-defaults-gnu'."
  1256. :group 'org-link-follow
  1257. :type '(repeat
  1258. (cons (choice :value ""
  1259. (string :tag "Extension")
  1260. (const :tag "System command to open files" system)
  1261. (const :tag "Default for unrecognized files" t)
  1262. (const :tag "Remote file" remote)
  1263. (const :tag "Links to a directory" directory)
  1264. (const :tag "Any files that have Emacs modes"
  1265. auto-mode))
  1266. (choice :value ""
  1267. (const :tag "Visit with Emacs" emacs)
  1268. (const :tag "Use default" default)
  1269. (const :tag "Use the system command" system)
  1270. (string :tag "Command")
  1271. (sexp :tag "Lisp form")))))
  1272. (defgroup org-refile nil
  1273. "Options concerning refiling entries in Org-mode."
  1274. :tag "Org Refile"
  1275. :group 'org)
  1276. (defcustom org-directory "~/org"
  1277. "Directory with org files.
  1278. This is just a default location to look for Org files. There is no need
  1279. at all to put your files into this directory. It is only used in the
  1280. following situations:
  1281. 1. When a remember template specifies a target file that is not an
  1282. absolute path. The path will then be interpreted relative to
  1283. `org-directory'
  1284. 2. When a remember note is filed away in an interactive way (when exiting the
  1285. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1286. with `org-directory' as the default path."
  1287. :group 'org-refile
  1288. :group 'org-remember
  1289. :type 'directory)
  1290. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1291. "Default target for storing notes.
  1292. Used by the hooks for remember.el. This can be a string, or nil to mean
  1293. the value of `remember-data-file'.
  1294. You can set this on a per-template basis with the variable
  1295. `org-remember-templates'."
  1296. :group 'org-refile
  1297. :group 'org-remember
  1298. :type '(choice
  1299. (const :tag "Default from remember-data-file" nil)
  1300. file))
  1301. (defcustom org-goto-interface 'outline
  1302. "The default interface to be used for `org-goto'.
  1303. Allowed values are:
  1304. outline The interface shows an outline of the relevant file
  1305. and the correct heading is found by moving through
  1306. the outline or by searching with incremental search.
  1307. outline-path-completion Headlines in the current buffer are offered via
  1308. completion. This is the interface also used by
  1309. the refile command."
  1310. :group 'org-refile
  1311. :type '(choice
  1312. (const :tag "Outline" outline)
  1313. (const :tag "Outline-path-completion" outline-path-completion)))
  1314. (defcustom org-goto-max-level 5
  1315. "Maximum level to be considered when running org-goto with refile interface."
  1316. :group 'org-refile
  1317. :type 'integer)
  1318. (defcustom org-reverse-note-order nil
  1319. "Non-nil means, store new notes at the beginning of a file or entry.
  1320. When nil, new notes will be filed to the end of a file or entry.
  1321. This can also be a list with cons cells of regular expressions that
  1322. are matched against file names, and values."
  1323. :group 'org-remember
  1324. :group 'org-refile
  1325. :type '(choice
  1326. (const :tag "Reverse always" t)
  1327. (const :tag "Reverse never" nil)
  1328. (repeat :tag "By file name regexp"
  1329. (cons regexp boolean))))
  1330. (defcustom org-refile-targets nil
  1331. "Targets for refiling entries with \\[org-refile].
  1332. This is list of cons cells. Each cell contains:
  1333. - a specification of the files to be considered, either a list of files,
  1334. or a symbol whose function or variable value will be used to retrieve
  1335. a file name or a list of file names. If you use `org-agenda-files' for
  1336. that, all agenda files will be scanned for targets. Nil means, consider
  1337. headings in the current buffer.
  1338. - A specification of how to find candidate refile targets. This may be
  1339. any of:
  1340. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1341. This tag has to be present in all target headlines, inheritance will
  1342. not be considered.
  1343. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1344. todo keyword.
  1345. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1346. headlines that are refiling targets.
  1347. - a cons cell (:level . N). Any headline of level N is considered a target.
  1348. Note that, when `org-odd-levels-only' is set, level corresponds to
  1349. order in hierarchy, not to the number of stars.
  1350. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1351. Note that, when `org-odd-levels-only' is set, level corresponds to
  1352. order in hierarchy, not to the number of stars.
  1353. You can set the variable `org-refile-target-verify-function' to a function
  1354. to verify each headline found by the simple critery above.
  1355. When this variable is nil, all top-level headlines in the current buffer
  1356. are used, equivalent to the value `((nil . (:level . 1))'."
  1357. :group 'org-refile
  1358. :type '(repeat
  1359. (cons
  1360. (choice :value org-agenda-files
  1361. (const :tag "All agenda files" org-agenda-files)
  1362. (const :tag "Current buffer" nil)
  1363. (function) (variable) (file))
  1364. (choice :tag "Identify target headline by"
  1365. (cons :tag "Specific tag" (const :value :tag) (string))
  1366. (cons :tag "TODO keyword" (const :value :todo) (string))
  1367. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1368. (cons :tag "Level number" (const :value :level) (integer))
  1369. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1370. (defcustom org-refile-target-verify-function nil
  1371. "Function to verify if the headline at point should be a refile target.
  1372. The function will be called without arguments, with point at the
  1373. beginning of the headline. It should return t and leave point
  1374. where it is if the headline is a valid target for refiling.
  1375. If the target should not be selected, the function must return nil.
  1376. In addition to this, it may move point to a place from where the search
  1377. should be continued. For example, the function may decide that the entire
  1378. subtree of the current entry should be excluded and move point to the end
  1379. of the subtree."
  1380. :group 'org-refile
  1381. :type 'function)
  1382. (defcustom org-refile-use-outline-path nil
  1383. "Non-nil means, provide refile targets as paths.
  1384. So a level 3 headline will be available as level1/level2/level3.
  1385. When the value is `file', also include the file name (without directory)
  1386. into the path. In this case, you can also stop the completion after
  1387. the file name, to get entries inserted as top level in the file.
  1388. When `full-file-path', include the full file path."
  1389. :group 'org-refile
  1390. :type '(choice
  1391. (const :tag "Not" nil)
  1392. (const :tag "Yes" t)
  1393. (const :tag "Start with file name" file)
  1394. (const :tag "Start with full file path" full-file-path)))
  1395. (defcustom org-outline-path-complete-in-steps t
  1396. "Non-nil means, complete the outline path in hierarchical steps.
  1397. When Org-mode uses the refile interface to select an outline path
  1398. \(see variable `org-refile-use-outline-path'), the completion of
  1399. the path can be done is a single go, or if can be done in steps down
  1400. the headline hierarchy. Going in steps is probably the best if you
  1401. do not use a special completion package like `ido' or `icicles'.
  1402. However, when using these packages, going in one step can be very
  1403. fast, while still showing the whole path to the entry."
  1404. :group 'org-refile
  1405. :type 'boolean)
  1406. (defcustom org-refile-allow-creating-parent-nodes nil
  1407. "Non-nil means, allow to create new nodes as refile targets.
  1408. New nodes are then created by adding \"/new node name\" to the completion
  1409. of an existing node. When the value of this variable is `confirm',
  1410. new node creation must be confirmed by the user (recommended)
  1411. When nil, the completion must match an existing entry.
  1412. Note that, if the new heading is not seen by the criteria
  1413. listed in `org-refile-targets', multiple instances of the same
  1414. heading would be created by trying again to file under the new
  1415. heading."
  1416. :group 'org-refile
  1417. :type '(choice
  1418. (const :tag "Never" nil)
  1419. (const :tag "Always" t)
  1420. (const :tag "Prompt for confirmation" confirm)))
  1421. (defgroup org-todo nil
  1422. "Options concerning TODO items in Org-mode."
  1423. :tag "Org TODO"
  1424. :group 'org)
  1425. (defgroup org-progress nil
  1426. "Options concerning Progress logging in Org-mode."
  1427. :tag "Org Progress"
  1428. :group 'org-time)
  1429. (defvar org-todo-interpretation-widgets
  1430. '(
  1431. (:tag "Sequence (cycling hits every state)" sequence)
  1432. (:tag "Type (cycling directly to DONE)" type))
  1433. "The available interpretation symbols for customizing
  1434. `org-todo-keywords'.
  1435. Interested libraries should add to this list.")
  1436. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1437. "List of TODO entry keyword sequences and their interpretation.
  1438. \\<org-mode-map>This is a list of sequences.
  1439. Each sequence starts with a symbol, either `sequence' or `type',
  1440. indicating if the keywords should be interpreted as a sequence of
  1441. action steps, or as different types of TODO items. The first
  1442. keywords are states requiring action - these states will select a headline
  1443. for inclusion into the global TODO list Org-mode produces. If one of
  1444. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1445. signify that no further action is necessary. If \"|\" is not found,
  1446. the last keyword is treated as the only DONE state of the sequence.
  1447. The command \\[org-todo] cycles an entry through these states, and one
  1448. additional state where no keyword is present. For details about this
  1449. cycling, see the manual.
  1450. TODO keywords and interpretation can also be set on a per-file basis with
  1451. the special #+SEQ_TODO and #+TYP_TODO lines.
  1452. Each keyword can optionally specify a character for fast state selection
  1453. \(in combination with the variable `org-use-fast-todo-selection')
  1454. and specifiers for state change logging, using the same syntax
  1455. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1456. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1457. indicates to record a time stamp each time this state is selected.
  1458. Each keyword may also specify if a timestamp or a note should be
  1459. recorded when entering or leaving the state, by adding additional
  1460. characters in the parenthesis after the keyword. This looks like this:
  1461. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1462. record only the time of the state change. With X and Y being either
  1463. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1464. Y when leaving the state if and only if the *target* state does not
  1465. define X. You may omit any of the fast-selection key or X or /Y,
  1466. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1467. For backward compatibility, this variable may also be just a list
  1468. of keywords - in this case the interpretation (sequence or type) will be
  1469. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1470. :group 'org-todo
  1471. :group 'org-keywords
  1472. :type '(choice
  1473. (repeat :tag "Old syntax, just keywords"
  1474. (string :tag "Keyword"))
  1475. (repeat :tag "New syntax"
  1476. (cons
  1477. (choice
  1478. :tag "Interpretation"
  1479. ;;Quick and dirty way to see
  1480. ;;`org-todo-interpretations'. This takes the
  1481. ;;place of item arguments
  1482. :convert-widget
  1483. (lambda (widget)
  1484. (widget-put widget
  1485. :args (mapcar
  1486. #'(lambda (x)
  1487. (widget-convert
  1488. (cons 'const x)))
  1489. org-todo-interpretation-widgets))
  1490. widget))
  1491. (repeat
  1492. (string :tag "Keyword"))))))
  1493. (defvar org-todo-keywords-1 nil
  1494. "All TODO and DONE keywords active in a buffer.")
  1495. (make-variable-buffer-local 'org-todo-keywords-1)
  1496. (defvar org-todo-keywords-for-agenda nil)
  1497. (defvar org-done-keywords-for-agenda nil)
  1498. (defvar org-todo-keyword-alist-for-agenda nil)
  1499. (defvar org-tag-alist-for-agenda nil)
  1500. (defvar org-agenda-contributing-files nil)
  1501. (defvar org-not-done-keywords nil)
  1502. (make-variable-buffer-local 'org-not-done-keywords)
  1503. (defvar org-done-keywords nil)
  1504. (make-variable-buffer-local 'org-done-keywords)
  1505. (defvar org-todo-heads nil)
  1506. (make-variable-buffer-local 'org-todo-heads)
  1507. (defvar org-todo-sets nil)
  1508. (make-variable-buffer-local 'org-todo-sets)
  1509. (defvar org-todo-log-states nil)
  1510. (make-variable-buffer-local 'org-todo-log-states)
  1511. (defvar org-todo-kwd-alist nil)
  1512. (make-variable-buffer-local 'org-todo-kwd-alist)
  1513. (defvar org-todo-key-alist nil)
  1514. (make-variable-buffer-local 'org-todo-key-alist)
  1515. (defvar org-todo-key-trigger nil)
  1516. (make-variable-buffer-local 'org-todo-key-trigger)
  1517. (defcustom org-todo-interpretation 'sequence
  1518. "Controls how TODO keywords are interpreted.
  1519. This variable is in principle obsolete and is only used for
  1520. backward compatibility, if the interpretation of todo keywords is
  1521. not given already in `org-todo-keywords'. See that variable for
  1522. more information."
  1523. :group 'org-todo
  1524. :group 'org-keywords
  1525. :type '(choice (const sequence)
  1526. (const type)))
  1527. (defcustom org-use-fast-todo-selection t
  1528. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1529. This variable describes if and under what circumstances the cycling
  1530. mechanism for TODO keywords will be replaced by a single-key, direct
  1531. selection scheme.
  1532. When nil, fast selection is never used.
  1533. When the symbol `prefix', it will be used when `org-todo' is called with
  1534. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1535. in an agenda buffer.
  1536. When t, fast selection is used by default. In this case, the prefix
  1537. argument forces cycling instead.
  1538. In all cases, the special interface is only used if access keys have actually
  1539. been assigned by the user, i.e. if keywords in the configuration are followed
  1540. by a letter in parenthesis, like TODO(t)."
  1541. :group 'org-todo
  1542. :type '(choice
  1543. (const :tag "Never" nil)
  1544. (const :tag "By default" t)
  1545. (const :tag "Only with C-u C-c C-t" prefix)))
  1546. (defcustom org-provide-todo-statistics t
  1547. "Non-nil means, update todo statistics after insert and toggle.
  1548. ALL-HEADLINES means update todo statistics by including headlines
  1549. with no TODO keyword as well, counting them as not done.
  1550. A list of TODO keywords means the same, but skip keywords that are
  1551. not in this list.
  1552. When this is set, todo statistics is updated in the parent of the
  1553. current entry each time a todo state is changed."
  1554. :group 'org-todo
  1555. :type '(choice
  1556. (const :tag "Yes, only for TODO entries" t)
  1557. (const :tag "Yes, including all entries" 'all-headlines)
  1558. (repeat :tag "Yes, for TODOs in this list"
  1559. (string :tag "TODO keyword"))
  1560. (other :tag "No TODO statistics" nil)))
  1561. (defcustom org-hierarchical-todo-statistics t
  1562. "Non-nil means, TODO statistics covers just direct children.
  1563. When nil, all entries in the subtree are considered.
  1564. This has only an effect if `org-provide-todo-statistics' is set.
  1565. To set this to nil for only a single subtree, use a COOKIE_DATA
  1566. property and include the word \"recursive\" into the value."
  1567. :group 'org-todo
  1568. :type 'boolean)
  1569. (defcustom org-after-todo-state-change-hook nil
  1570. "Hook which is run after the state of a TODO item was changed.
  1571. The new state (a string with a TODO keyword, or nil) is available in the
  1572. Lisp variable `state'."
  1573. :group 'org-todo
  1574. :type 'hook)
  1575. (defvar org-blocker-hook nil
  1576. "Hook for functions that are allowed to block a state change.
  1577. Each function gets as its single argument a property list, see
  1578. `org-trigger-hook' for more information about this list.
  1579. If any of the functions in this hook returns nil, the state change
  1580. is blocked.")
  1581. (defvar org-trigger-hook nil
  1582. "Hook for functions that are triggered by a state change.
  1583. Each function gets as its single argument a property list with at least
  1584. the following elements:
  1585. (:type type-of-change :position pos-at-entry-start
  1586. :from old-state :to new-state)
  1587. Depending on the type, more properties may be present.
  1588. This mechanism is currently implemented for:
  1589. TODO state changes
  1590. ------------------
  1591. :type todo-state-change
  1592. :from previous state (keyword as a string), or nil, or a symbol
  1593. 'todo' or 'done', to indicate the general type of state.
  1594. :to new state, like in :from")
  1595. (defcustom org-enforce-todo-dependencies nil
  1596. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1597. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1598. be blocked if any prior sibling is not yet done.
  1599. Finally, if the parent is blocked because of ordered siblings of its own,
  1600. the child will also be blocked.
  1601. This variable needs to be set before org.el is loaded, and you need to
  1602. restart Emacs after a change to make the change effective. The only way
  1603. to change is while Emacs is running is through the customize interface."
  1604. :set (lambda (var val)
  1605. (set var val)
  1606. (if val
  1607. (add-hook 'org-blocker-hook
  1608. 'org-block-todo-from-children-or-siblings-or-parent)
  1609. (remove-hook 'org-blocker-hook
  1610. 'org-block-todo-from-children-or-siblings-or-parent)))
  1611. :group 'org-todo
  1612. :type 'boolean)
  1613. (defcustom org-enforce-todo-checkbox-dependencies nil
  1614. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1615. When this is nil, checkboxes have no influence on switching TODO states.
  1616. When non-nil, you first need to check off all check boxes before the TODO
  1617. entry can be switched to DONE.
  1618. This variable needs to be set before org.el is loaded, and you need to
  1619. restart Emacs after a change to make the change effective. The only way
  1620. to change is while Emacs is running is through the customize interface."
  1621. :set (lambda (var val)
  1622. (set var val)
  1623. (if val
  1624. (add-hook 'org-blocker-hook
  1625. 'org-block-todo-from-checkboxes)
  1626. (remove-hook 'org-blocker-hook
  1627. 'org-block-todo-from-checkboxes)))
  1628. :group 'org-todo
  1629. :type 'boolean)
  1630. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1631. "Non-nil means, inserting a TODO heading is treated as state change.
  1632. So when the command \\[org-insert-todo-heading] is used, state change
  1633. logging will apply if appropriate. When nil, the new TODO item will
  1634. be inserted directly, and no logging will take place."
  1635. :group 'org-todo
  1636. :type 'boolean)
  1637. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1638. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1639. This is the default behavior. However, setting this to nil allows a
  1640. convenient way to select a TODO state and bypass any logging associated
  1641. with that."
  1642. :group 'org-todo
  1643. :type 'boolean)
  1644. (defcustom org-todo-state-tags-triggers nil
  1645. "Tag changes that should be triggered by TODO state changes.
  1646. This is a list. Each entry is
  1647. (state-change (tag . flag) .......)
  1648. State-change can be a string with a state, and empty string to indicate the
  1649. state that has no TODO keyword, or it can be one of the symbols `todo'
  1650. or `done', meaning any not-done or done state, respectively."
  1651. :group 'org-todo
  1652. :group 'org-tags
  1653. :type '(repeat
  1654. (cons (choice :tag "When changing to"
  1655. (const :tag "Not-done state" todo)
  1656. (const :tag "Done state" done)
  1657. (string :tag "State"))
  1658. (repeat
  1659. (cons :tag "Tag action"
  1660. (string :tag "Tag")
  1661. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1662. (defcustom org-log-done nil
  1663. "Information to record when a task moves to the DONE state.
  1664. Possible values are:
  1665. nil Don't add anything, just change the keyword
  1666. time Add a time stamp to the task
  1667. note Prompt a closing note and add it with template `org-log-note-headings'
  1668. This option can also be set with on a per-file-basis with
  1669. #+STARTUP: nologdone
  1670. #+STARTUP: logdone
  1671. #+STARTUP: lognotedone
  1672. You can have local logging settings for a subtree by setting the LOGGING
  1673. property to one or more of these keywords."
  1674. :group 'org-todo
  1675. :group 'org-progress
  1676. :type '(choice
  1677. (const :tag "No logging" nil)
  1678. (const :tag "Record CLOSED timestamp" time)
  1679. (const :tag "Record CLOSED timestamp with closing note." note)))
  1680. ;; Normalize old uses of org-log-done.
  1681. (cond
  1682. ((eq org-log-done t) (setq org-log-done 'time))
  1683. ((and (listp org-log-done) (memq 'done org-log-done))
  1684. (setq org-log-done 'note)))
  1685. (defcustom org-log-note-clock-out nil
  1686. "Non-nil means, record a note when clocking out of an item.
  1687. This can also be configured on a per-file basis by adding one of
  1688. the following lines anywhere in the buffer:
  1689. #+STARTUP: lognoteclock-out
  1690. #+STARTUP: nolognoteclock-out"
  1691. :group 'org-todo
  1692. :group 'org-progress
  1693. :type 'boolean)
  1694. (defcustom org-log-done-with-time t
  1695. "Non-nil means, the CLOSED time stamp will contain date and time.
  1696. When nil, only the date will be recorded."
  1697. :group 'org-progress
  1698. :type 'boolean)
  1699. (defcustom org-log-note-headings
  1700. '((done . "CLOSING NOTE %t")
  1701. (state . "State %-12s from %-12S %t")
  1702. (note . "Note taken on %t")
  1703. (clock-out . ""))
  1704. "Headings for notes added to entries.
  1705. The value is an alist, with the car being a symbol indicating the note
  1706. context, and the cdr is the heading to be used. The heading may also be the
  1707. empty string.
  1708. %t in the heading will be replaced by a time stamp.
  1709. %s will be replaced by the new TODO state, in double quotes.
  1710. %S will be replaced by the old TODO state, in double quotes.
  1711. %u will be replaced by the user name.
  1712. %U will be replaced by the full user name."
  1713. :group 'org-todo
  1714. :group 'org-progress
  1715. :type '(list :greedy t
  1716. (cons (const :tag "Heading when closing an item" done) string)
  1717. (cons (const :tag
  1718. "Heading when changing todo state (todo sequence only)"
  1719. state) string)
  1720. (cons (const :tag "Heading when just taking a note" note) string)
  1721. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1722. (unless (assq 'note org-log-note-headings)
  1723. (push '(note . "%t") org-log-note-headings))
  1724. (defcustom org-log-into-drawer nil
  1725. "Non-nil means, insert state change notes and time stamps into a drawer.
  1726. When nil, state changes notes will be inserted after the headline and
  1727. any scheduling and clock lines, but not inside a drawer.
  1728. The value of this variable should be the name of the drawer to use.
  1729. LOGBOOK is proposed at the default drawer for this purpose, you can
  1730. also set this to a string to define the drawer of your choice.
  1731. A value of t is also allowed, representing \"LOGBOOK\".
  1732. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1733. will be ignored.
  1734. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1735. a subtree."
  1736. :group 'org-todo
  1737. :group 'org-progress
  1738. :type '(choice
  1739. (const :tag "Not into a drawer" nil)
  1740. (const :tag "LOGBOOK" t)
  1741. (string :tag "Other")))
  1742. (if (fboundp 'defvaralias)
  1743. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1744. (defun org-log-into-drawer ()
  1745. "Return the value of `org-log-into-drawer', but let properties overrule.
  1746. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1747. used instead of the default value."
  1748. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1749. (cond
  1750. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1751. ((equal p "t") "LOGBOOK")
  1752. (t p))))
  1753. (defcustom org-log-state-notes-insert-after-drawers nil
  1754. "Non-nil means, insert state change notes after any drawers in entry.
  1755. Only the drawers that *immediately* follow the headline and the
  1756. deadline/scheduled line are skipped.
  1757. When nil, insert notes right after the heading and perhaps the line
  1758. with deadline/scheduling if present.
  1759. This variable will have no effect if `org-log-into-drawer' is
  1760. set."
  1761. :group 'org-todo
  1762. :group 'org-progress
  1763. :type 'boolean)
  1764. (defcustom org-log-states-order-reversed t
  1765. "Non-nil means, the latest state change note will be directly after heading.
  1766. When nil, the notes will be orderer according to time."
  1767. :group 'org-todo
  1768. :group 'org-progress
  1769. :type 'boolean)
  1770. (defcustom org-log-repeat 'time
  1771. "Non-nil means, record moving through the DONE state when triggering repeat.
  1772. An auto-repeating task is immediately switched back to TODO when
  1773. marked DONE. If you are not logging state changes (by adding \"@\"
  1774. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1775. record a closing note, there will be no record of the task moving
  1776. through DONE. This variable forces taking a note anyway.
  1777. nil Don't force a record
  1778. time Record a time stamp
  1779. note Record a note
  1780. This option can also be set with on a per-file-basis with
  1781. #+STARTUP: logrepeat
  1782. #+STARTUP: lognoterepeat
  1783. #+STARTUP: nologrepeat
  1784. You can have local logging settings for a subtree by setting the LOGGING
  1785. property to one or more of these keywords."
  1786. :group 'org-todo
  1787. :group 'org-progress
  1788. :type '(choice
  1789. (const :tag "Don't force a record" nil)
  1790. (const :tag "Force recording the DONE state" time)
  1791. (const :tag "Force recording a note with the DONE state" note)))
  1792. (defgroup org-priorities nil
  1793. "Priorities in Org-mode."
  1794. :tag "Org Priorities"
  1795. :group 'org-todo)
  1796. (defcustom org-enable-priority-commands t
  1797. "Non-nil means, priority commands are active.
  1798. When nil, these commands will be disabled, so that you never accidentally
  1799. set a priority."
  1800. :group 'org-priorities
  1801. :type 'boolean)
  1802. (defcustom org-highest-priority ?A
  1803. "The highest priority of TODO items. A character like ?A, ?B etc.
  1804. Must have a smaller ASCII number than `org-lowest-priority'."
  1805. :group 'org-priorities
  1806. :type 'character)
  1807. (defcustom org-lowest-priority ?C
  1808. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1809. Must have a larger ASCII number than `org-highest-priority'."
  1810. :group 'org-priorities
  1811. :type 'character)
  1812. (defcustom org-default-priority ?B
  1813. "The default priority of TODO items.
  1814. This is the priority an item get if no explicit priority is given."
  1815. :group 'org-priorities
  1816. :type 'character)
  1817. (defcustom org-priority-start-cycle-with-default t
  1818. "Non-nil means, start with default priority when starting to cycle.
  1819. When this is nil, the first step in the cycle will be (depending on the
  1820. command used) one higher or lower that the default priority."
  1821. :group 'org-priorities
  1822. :type 'boolean)
  1823. (defgroup org-time nil
  1824. "Options concerning time stamps and deadlines in Org-mode."
  1825. :tag "Org Time"
  1826. :group 'org)
  1827. (defcustom org-insert-labeled-timestamps-at-point nil
  1828. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1829. When nil, these labeled time stamps are forces into the second line of an
  1830. entry, just after the headline. When scheduling from the global TODO list,
  1831. the time stamp will always be forced into the second line."
  1832. :group 'org-time
  1833. :type 'boolean)
  1834. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1835. "Formats for `format-time-string' which are used for time stamps.
  1836. It is not recommended to change this constant.")
  1837. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1838. "Number of minutes to round time stamps to.
  1839. These are two values, the first applies when first creating a time stamp.
  1840. The second applies when changing it with the commands `S-up' and `S-down'.
  1841. When changing the time stamp, this means that it will change in steps
  1842. of N minutes, as given by the second value.
  1843. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1844. numbers should be factors of 60, so for example 5, 10, 15.
  1845. When this is larger than 1, you can still force an exact time-stamp by using
  1846. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1847. and by using a prefix arg to `S-up/down' to specify the exact number
  1848. of minutes to shift."
  1849. :group 'org-time
  1850. :get '(lambda (var) ; Make sure all entries have 5 elements
  1851. (if (integerp (default-value var))
  1852. (list (default-value var) 5)
  1853. (default-value var)))
  1854. :type '(list
  1855. (integer :tag "when inserting times")
  1856. (integer :tag "when modifying times")))
  1857. ;; Normalize old customizations of this variable.
  1858. (when (integerp org-time-stamp-rounding-minutes)
  1859. (setq org-time-stamp-rounding-minutes
  1860. (list org-time-stamp-rounding-minutes
  1861. org-time-stamp-rounding-minutes)))
  1862. (defcustom org-display-custom-times nil
  1863. "Non-nil means, overlay custom formats over all time stamps.
  1864. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1865. To turn this on on a per-file basis, insert anywhere in the file:
  1866. #+STARTUP: customtime"
  1867. :group 'org-time
  1868. :set 'set-default
  1869. :type 'sexp)
  1870. (make-variable-buffer-local 'org-display-custom-times)
  1871. (defcustom org-time-stamp-custom-formats
  1872. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1873. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1874. These are overlayed over the default ISO format if the variable
  1875. `org-display-custom-times' is set. Time like %H:%M should be at the
  1876. end of the second format. The custom formats are also honored by export
  1877. commands, if custom time display is turned on at the time of export."
  1878. :group 'org-time
  1879. :type 'sexp)
  1880. (defun org-time-stamp-format (&optional long inactive)
  1881. "Get the right format for a time string."
  1882. (let ((f (if long (cdr org-time-stamp-formats)
  1883. (car org-time-stamp-formats))))
  1884. (if inactive
  1885. (concat "[" (substring f 1 -1) "]")
  1886. f)))
  1887. (defcustom org-time-clocksum-format "%d:%02d"
  1888. "The format string used when creating CLOCKSUM lines, or when
  1889. org-mode generates a time duration."
  1890. :group 'org-time
  1891. :type 'string)
  1892. (defcustom org-deadline-warning-days 14
  1893. "No. of days before expiration during which a deadline becomes active.
  1894. This variable governs the display in sparse trees and in the agenda.
  1895. When 0 or negative, it means use this number (the absolute value of it)
  1896. even if a deadline has a different individual lead time specified.
  1897. Custom commands can set this variable in the options section."
  1898. :group 'org-time
  1899. :group 'org-agenda-daily/weekly
  1900. :type 'integer)
  1901. (defcustom org-read-date-prefer-future t
  1902. "Non-nil means, assume future for incomplete date input from user.
  1903. This affects the following situations:
  1904. 1. The user gives a day, but no month.
  1905. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1906. read this as the third of *next* month. However, if you enter \"17\",
  1907. it will be considered as *this* month.
  1908. 2. The user gives a month but not a year.
  1909. For example, if it is april and you enter \"feb 2\", this will be read
  1910. as feb 2, *next* year. \"May 5\", however, will be this year.
  1911. Currently this does not work for ISO week specifications.
  1912. When this option is nil, the current month and year will always be used
  1913. as defaults."
  1914. :group 'org-time
  1915. :type 'boolean)
  1916. (defcustom org-read-date-display-live t
  1917. "Non-nil means, display current interpretation of date prompt live.
  1918. This display will be in an overlay, in the minibuffer."
  1919. :group 'org-time
  1920. :type 'boolean)
  1921. (defcustom org-read-date-popup-calendar t
  1922. "Non-nil means, pop up a calendar when prompting for a date.
  1923. In the calendar, the date can be selected with mouse-1. However, the
  1924. minibuffer will also be active, and you can simply enter the date as well.
  1925. When nil, only the minibuffer will be available."
  1926. :group 'org-time
  1927. :type 'boolean)
  1928. (if (fboundp 'defvaralias)
  1929. (defvaralias 'org-popup-calendar-for-date-prompt
  1930. 'org-read-date-popup-calendar))
  1931. (defcustom org-read-date-minibuffer-setup-hook nil
  1932. "Hook to be used to set up keys for the date/time interface.
  1933. Add key definitions to `minibuffer-local-map', which will be a temporary
  1934. copy."
  1935. :group 'org-time
  1936. :type 'hook)
  1937. (defcustom org-extend-today-until 0
  1938. "The hour when your day really ends. Must be an integer.
  1939. This has influence for the following applications:
  1940. - When switching the agenda to \"today\". It it is still earlier than
  1941. the time given here, the day recognized as TODAY is actually yesterday.
  1942. - When a date is read from the user and it is still before the time given
  1943. here, the current date and time will be assumed to be yesterday, 23:59.
  1944. Also, timestamps inserted in remember templates follow this rule.
  1945. IMPORTANT: This is a feature whose implementation is and likely will
  1946. remain incomplete. Really, it is only here because past midnight seems to
  1947. be the favorite working time of John Wiegley :-)"
  1948. :group 'org-time
  1949. :type 'integer)
  1950. (defcustom org-edit-timestamp-down-means-later nil
  1951. "Non-nil means, S-down will increase the time in a time stamp.
  1952. When nil, S-up will increase."
  1953. :group 'org-time
  1954. :type 'boolean)
  1955. (defcustom org-calendar-follow-timestamp-change t
  1956. "Non-nil means, make the calendar window follow timestamp changes.
  1957. When a timestamp is modified and the calendar window is visible, it will be
  1958. moved to the new date."
  1959. :group 'org-time
  1960. :type 'boolean)
  1961. (defgroup org-tags nil
  1962. "Options concerning tags in Org-mode."
  1963. :tag "Org Tags"
  1964. :group 'org)
  1965. (defcustom org-tag-alist nil
  1966. "List of tags allowed in Org-mode files.
  1967. When this list is nil, Org-mode will base TAG input on what is already in the
  1968. buffer.
  1969. The value of this variable is an alist, the car of each entry must be a
  1970. keyword as a string, the cdr may be a character that is used to select
  1971. that tag through the fast-tag-selection interface.
  1972. See the manual for details."
  1973. :group 'org-tags
  1974. :type '(repeat
  1975. (choice
  1976. (cons (string :tag "Tag name")
  1977. (character :tag "Access char"))
  1978. (const :tag "Start radio group" (:startgroup))
  1979. (const :tag "End radio group" (:endgroup))
  1980. (const :tag "New line" (:newline)))))
  1981. (defcustom org-tag-persistent-alist nil
  1982. "List of tags that will always appear in all Org-mode files.
  1983. This is in addition to any in buffer settings or customizations
  1984. of `org-tag-alist'.
  1985. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1986. The value of this variable is an alist, the car of each entry must be a
  1987. keyword as a string, the cdr may be a character that is used to select
  1988. that tag through the fast-tag-selection interface.
  1989. See the manual for details.
  1990. To disable these tags on a per-file basis, insert anywhere in the file:
  1991. #+STARTUP: noptag"
  1992. :group 'org-tags
  1993. :type '(repeat
  1994. (choice
  1995. (cons (string :tag "Tag name")
  1996. (character :tag "Access char"))
  1997. (const :tag "Start radio group" (:startgroup))
  1998. (const :tag "End radio group" (:endgroup))
  1999. (const :tag "New line" (:newline)))))
  2000. (defvar org-file-tags nil
  2001. "List of tags that can be inherited by all entries in the file.
  2002. The tags will be inherited if the variable `org-use-tag-inheritance'
  2003. says they should be.
  2004. This variable is populated from #+TAG lines.")
  2005. (defcustom org-use-fast-tag-selection 'auto
  2006. "Non-nil means, use fast tag selection scheme.
  2007. This is a special interface to select and deselect tags with single keys.
  2008. When nil, fast selection is never used.
  2009. When the symbol `auto', fast selection is used if and only if selection
  2010. characters for tags have been configured, either through the variable
  2011. `org-tag-alist' or through a #+TAGS line in the buffer.
  2012. When t, fast selection is always used and selection keys are assigned
  2013. automatically if necessary."
  2014. :group 'org-tags
  2015. :type '(choice
  2016. (const :tag "Always" t)
  2017. (const :tag "Never" nil)
  2018. (const :tag "When selection characters are configured" 'auto)))
  2019. (defcustom org-fast-tag-selection-single-key nil
  2020. "Non-nil means, fast tag selection exits after first change.
  2021. When nil, you have to press RET to exit it.
  2022. During fast tag selection, you can toggle this flag with `C-c'.
  2023. This variable can also have the value `expert'. In this case, the window
  2024. displaying the tags menu is not even shown, until you press C-c again."
  2025. :group 'org-tags
  2026. :type '(choice
  2027. (const :tag "No" nil)
  2028. (const :tag "Yes" t)
  2029. (const :tag "Expert" expert)))
  2030. (defvar org-fast-tag-selection-include-todo nil
  2031. "Non-nil means, fast tags selection interface will also offer TODO states.
  2032. This is an undocumented feature, you should not rely on it.")
  2033. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2034. "The column to which tags should be indented in a headline.
  2035. If this number is positive, it specifies the column. If it is negative,
  2036. it means that the tags should be flushright to that column. For example,
  2037. -80 works well for a normal 80 character screen."
  2038. :group 'org-tags
  2039. :type 'integer)
  2040. (defcustom org-auto-align-tags t
  2041. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2042. These operations change the length of a headline and therefore shift
  2043. the tags around. With this options turned on, after each such operation
  2044. the tags are again aligned to `org-tags-column'."
  2045. :group 'org-tags
  2046. :type 'boolean)
  2047. (defcustom org-use-tag-inheritance t
  2048. "Non-nil means, tags in levels apply also for sublevels.
  2049. When nil, only the tags directly given in a specific line apply there.
  2050. This may also be a list of tags that should be inherited, or a regexp that
  2051. matches tags that should be inherited. Additional control is possible
  2052. with the variable `org-tags-exclude-from-inheritance' which gives an
  2053. explicit list of tags to be excluded from inheritance., even if the value of
  2054. `org-use-tag-inheritance' would select it for inheritance.
  2055. If this option is t, a match early-on in a tree can lead to a large
  2056. number of matches in the subtree when constructing the agenda or creating
  2057. a sparse tree. If you only want to see the first match in a tree during
  2058. a search, check out the variable `org-tags-match-list-sublevels'."
  2059. :group 'org-tags
  2060. :type '(choice
  2061. (const :tag "Not" nil)
  2062. (const :tag "Always" t)
  2063. (repeat :tag "Specific tags" (string :tag "Tag"))
  2064. (regexp :tag "Tags matched by regexp")))
  2065. (defcustom org-tags-exclude-from-inheritance nil
  2066. "List of tags that should never be inherited.
  2067. This is a way to exclude a few tags from inheritance. For way to do
  2068. the opposite, to actively allow inheritance for selected tags,
  2069. see the variable `org-use-tag-inheritance'."
  2070. :group 'org-tags
  2071. :type '(repeat (string :tag "Tag")))
  2072. (defun org-tag-inherit-p (tag)
  2073. "Check if TAG is one that should be inherited."
  2074. (cond
  2075. ((member tag org-tags-exclude-from-inheritance) nil)
  2076. ((eq org-use-tag-inheritance t) t)
  2077. ((not org-use-tag-inheritance) nil)
  2078. ((stringp org-use-tag-inheritance)
  2079. (string-match org-use-tag-inheritance tag))
  2080. ((listp org-use-tag-inheritance)
  2081. (member tag org-use-tag-inheritance))
  2082. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2083. (defcustom org-tags-match-list-sublevels t
  2084. "Non-nil means list also sublevels of headlines matching a search.
  2085. This variable applies to tags/property searches, and also to stuck
  2086. projects because this search is based on a tags match as well.
  2087. When set to the symbol `indented', sublevels are indented with
  2088. leading dots.
  2089. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2090. the sublevels of a headline matching a tag search often also match
  2091. the same search. Listing all of them can create very long lists.
  2092. Setting this variable to nil causes subtrees of a match to be skipped.
  2093. This variable is semi-obsolete and probably should always be true. It
  2094. is better to limit inheritance to certain tags using the variables
  2095. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2096. :group 'org-tags
  2097. :type '(choice
  2098. (const :tag "No, don't list them" nil)
  2099. (const :tag "Yes, do list them" t)
  2100. (const :tag "List them, indented with leading dots" indented)))
  2101. (defcustom org-tags-sort-function nil
  2102. "When set, tags are sorted using this function as a comparator"
  2103. :group 'org-tags
  2104. :type '(choice
  2105. (const :tag "No sorting" nil)
  2106. (const :tag "Alphabetical" string<)
  2107. (const :tag "Reverse alphabetical" string>)
  2108. (function :tag "Custom function" nil)))
  2109. (defvar org-tags-history nil
  2110. "History of minibuffer reads for tags.")
  2111. (defvar org-last-tags-completion-table nil
  2112. "The last used completion table for tags.")
  2113. (defvar org-after-tags-change-hook nil
  2114. "Hook that is run after the tags in a line have changed.")
  2115. (defgroup org-properties nil
  2116. "Options concerning properties in Org-mode."
  2117. :tag "Org Properties"
  2118. :group 'org)
  2119. (defcustom org-property-format "%-10s %s"
  2120. "How property key/value pairs should be formatted by `indent-line'.
  2121. When `indent-line' hits a property definition, it will format the line
  2122. according to this format, mainly to make sure that the values are
  2123. lined-up with respect to each other."
  2124. :group 'org-properties
  2125. :type 'string)
  2126. (defcustom org-use-property-inheritance nil
  2127. "Non-nil means, properties apply also for sublevels.
  2128. This setting is chiefly used during property searches. Turning it on can
  2129. cause significant overhead when doing a search, which is why it is not
  2130. on by default.
  2131. When nil, only the properties directly given in the current entry count.
  2132. When t, every property is inherited. The value may also be a list of
  2133. properties that should have inheritance, or a regular expression matching
  2134. properties that should be inherited.
  2135. However, note that some special properties use inheritance under special
  2136. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2137. and the properties ending in \"_ALL\" when they are used as descriptor
  2138. for valid values of a property.
  2139. Note for programmers:
  2140. When querying an entry with `org-entry-get', you can control if inheritance
  2141. should be used. By default, `org-entry-get' looks only at the local
  2142. properties. You can request inheritance by setting the inherit argument
  2143. to t (to force inheritance) or to `selective' (to respect the setting
  2144. in this variable)."
  2145. :group 'org-properties
  2146. :type '(choice
  2147. (const :tag "Not" nil)
  2148. (const :tag "Always" t)
  2149. (repeat :tag "Specific properties" (string :tag "Property"))
  2150. (regexp :tag "Properties matched by regexp")))
  2151. (defun org-property-inherit-p (property)
  2152. "Check if PROPERTY is one that should be inherited."
  2153. (cond
  2154. ((eq org-use-property-inheritance t) t)
  2155. ((not org-use-property-inheritance) nil)
  2156. ((stringp org-use-property-inheritance)
  2157. (string-match org-use-property-inheritance property))
  2158. ((listp org-use-property-inheritance)
  2159. (member property org-use-property-inheritance))
  2160. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2161. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2162. "The default column format, if no other format has been defined.
  2163. This variable can be set on the per-file basis by inserting a line
  2164. #+COLUMNS: %25ITEM ....."
  2165. :group 'org-properties
  2166. :type 'string)
  2167. (defcustom org-columns-ellipses ".."
  2168. "The ellipses to be used when a field in column view is truncated.
  2169. When this is the empty string, as many characters as possible are shown,
  2170. but then there will be no visual indication that the field has been truncated.
  2171. When this is a string of length N, the last N characters of a truncated
  2172. field are replaced by this string. If the column is narrower than the
  2173. ellipses string, only part of the ellipses string will be shown."
  2174. :group 'org-properties
  2175. :type 'string)
  2176. (defcustom org-columns-modify-value-for-display-function nil
  2177. "Function that modifies values for display in column view.
  2178. For example, it can be used to cut out a certain part from a time stamp.
  2179. The function must take 2 arguments:
  2180. column-title The title of the column (*not* the property name)
  2181. value The value that should be modified.
  2182. The function should return the value that should be displayed,
  2183. or nil if the normal value should be used."
  2184. :group 'org-properties
  2185. :type 'function)
  2186. (defcustom org-effort-property "Effort"
  2187. "The property that is being used to keep track of effort estimates.
  2188. Effort estimates given in this property need to have the format H:MM."
  2189. :group 'org-properties
  2190. :group 'org-progress
  2191. :type '(string :tag "Property"))
  2192. (defconst org-global-properties-fixed
  2193. '(("VISIBILITY_ALL" . "folded children content all")
  2194. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2195. "List of property/value pairs that can be inherited by any entry.
  2196. These are fixed values, for the preset properties. The user variable
  2197. that can be used to add to this list is `org-global-properties'.
  2198. The entries in this list are cons cells where the car is a property
  2199. name and cdr is a string with the value. If the value represents
  2200. multiple items like an \"_ALL\" property, separate the items by
  2201. spaces.")
  2202. (defcustom org-global-properties nil
  2203. "List of property/value pairs that can be inherited by any entry.
  2204. This list will be combined with the constant `org-global-properties-fixed'.
  2205. The entries in this list are cons cells where the car is a property
  2206. name and cdr is a string with the value.
  2207. You can set buffer-local values for the same purpose in the variable
  2208. `org-file-properties' this by adding lines like
  2209. #+PROPERTY: NAME VALUE"
  2210. :group 'org-properties
  2211. :type '(repeat
  2212. (cons (string :tag "Property")
  2213. (string :tag "Value"))))
  2214. (defvar org-file-properties nil
  2215. "List of property/value pairs that can be inherited by any entry.
  2216. Valid for the current buffer.
  2217. This variable is populated from #+PROPERTY lines.")
  2218. (make-variable-buffer-local 'org-file-properties)
  2219. (defgroup org-agenda nil
  2220. "Options concerning agenda views in Org-mode."
  2221. :tag "Org Agenda"
  2222. :group 'org)
  2223. (defvar org-category nil
  2224. "Variable used by org files to set a category for agenda display.
  2225. Such files should use a file variable to set it, for example
  2226. # -*- mode: org; org-category: \"ELisp\"
  2227. or contain a special line
  2228. #+CATEGORY: ELisp
  2229. If the file does not specify a category, then file's base name
  2230. is used instead.")
  2231. (make-variable-buffer-local 'org-category)
  2232. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2233. (defcustom org-agenda-files nil
  2234. "The files to be used for agenda display.
  2235. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2236. \\[org-remove-file]. You can also use customize to edit the list.
  2237. If an entry is a directory, all files in that directory that are matched by
  2238. `org-agenda-file-regexp' will be part of the file list.
  2239. If the value of the variable is not a list but a single file name, then
  2240. the list of agenda files is actually stored and maintained in that file, one
  2241. agenda file per line."
  2242. :group 'org-agenda
  2243. :type '(choice
  2244. (repeat :tag "List of files and directories" file)
  2245. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2246. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2247. "Regular expression to match files for `org-agenda-files'.
  2248. If any element in the list in that variable contains a directory instead
  2249. of a normal file, all files in that directory that are matched by this
  2250. regular expression will be included."
  2251. :group 'org-agenda
  2252. :type 'regexp)
  2253. (defcustom org-agenda-text-search-extra-files nil
  2254. "List of extra files to be searched by text search commands.
  2255. These files will be search in addition to the agenda files by the
  2256. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2257. Note that these files will only be searched for text search commands,
  2258. not for the other agenda views like todo lists, tag searches or the weekly
  2259. agenda. This variable is intended to list notes and possibly archive files
  2260. that should also be searched by these two commands.
  2261. In fact, if the first element in the list is the symbol `agenda-archives',
  2262. than all archive files of all agenda files will be added to the search
  2263. scope."
  2264. :group 'org-agenda
  2265. :type '(set :greedy t
  2266. (const :tag "Agenda Archives" agenda-archives)
  2267. (repeat :inline t (file))))
  2268. (if (fboundp 'defvaralias)
  2269. (defvaralias 'org-agenda-multi-occur-extra-files
  2270. 'org-agenda-text-search-extra-files))
  2271. (defcustom org-agenda-skip-unavailable-files nil
  2272. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2273. A nil value means to remove them, after a query, from the list."
  2274. :group 'org-agenda
  2275. :type 'boolean)
  2276. (defcustom org-calendar-to-agenda-key [?c]
  2277. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2278. The command `org-calendar-goto-agenda' will be bound to this key. The
  2279. default is the character `c' because then `c' can be used to switch back and
  2280. forth between agenda and calendar."
  2281. :group 'org-agenda
  2282. :type 'sexp)
  2283. (defcustom org-calendar-agenda-action-key [?k]
  2284. "The key to be installed in `calendar-mode-map' for agenda-action.
  2285. The command `org-agenda-action' will be bound to this key. The
  2286. default is the character `k' because we use the same key in the agenda."
  2287. :group 'org-agenda
  2288. :type 'sexp)
  2289. (eval-after-load "calendar"
  2290. '(progn
  2291. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2292. 'org-calendar-goto-agenda)
  2293. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2294. 'org-agenda-action)))
  2295. (defgroup org-latex nil
  2296. "Options for embedding LaTeX code into Org-mode."
  2297. :tag "Org LaTeX"
  2298. :group 'org)
  2299. (defcustom org-format-latex-options
  2300. '(:foreground default :background default :scale 1.0
  2301. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2302. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2303. "Options for creating images from LaTeX fragments.
  2304. This is a property list with the following properties:
  2305. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2306. `default' means use the foreground of the default face.
  2307. :background the background color, or \"Transparent\".
  2308. `default' means use the background of the default face.
  2309. :scale a scaling factor for the size of the images.
  2310. :html-foreground, :html-background, :html-scale
  2311. the same numbers for HTML export.
  2312. :matchers a list indicating which matchers should be used to
  2313. find LaTeX fragments. Valid members of this list are:
  2314. \"begin\" find environments
  2315. \"$1\" find single characters surrounded by $.$
  2316. \"$\" find math expressions surrounded by $...$
  2317. \"$$\" find math expressions surrounded by $$....$$
  2318. \"\\(\" find math expressions surrounded by \\(...\\)
  2319. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2320. :group 'org-latex
  2321. :type 'plist)
  2322. (defcustom org-format-latex-header "\\documentclass{article}
  2323. \\usepackage{fullpage} % do not remove
  2324. \\usepackage{amssymb}
  2325. \\usepackage[usenames]{color}
  2326. \\usepackage{amsmath}
  2327. \\usepackage{latexsym}
  2328. \\usepackage[mathscr]{eucal}
  2329. \\pagestyle{empty} % do not remove"
  2330. "The document header used for processing LaTeX fragments."
  2331. :group 'org-latex
  2332. :type 'string)
  2333. (defgroup org-font-lock nil
  2334. "Font-lock settings for highlighting in Org-mode."
  2335. :tag "Org Font Lock"
  2336. :group 'org)
  2337. (defcustom org-level-color-stars-only nil
  2338. "Non-nil means fontify only the stars in each headline.
  2339. When nil, the entire headline is fontified.
  2340. Changing it requires restart of `font-lock-mode' to become effective
  2341. also in regions already fontified."
  2342. :group 'org-font-lock
  2343. :type 'boolean)
  2344. (defcustom org-hide-leading-stars nil
  2345. "Non-nil means, hide the first N-1 stars in a headline.
  2346. This works by using the face `org-hide' for these stars. This
  2347. face is white for a light background, and black for a dark
  2348. background. You may have to customize the face `org-hide' to
  2349. make this work.
  2350. Changing it requires restart of `font-lock-mode' to become effective
  2351. also in regions already fontified.
  2352. You may also set this on a per-file basis by adding one of the following
  2353. lines to the buffer:
  2354. #+STARTUP: hidestars
  2355. #+STARTUP: showstars"
  2356. :group 'org-font-lock
  2357. :type 'boolean)
  2358. (defcustom org-fontify-done-headline nil
  2359. "Non-nil means, change the face of a headline if it is marked DONE.
  2360. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2361. When this is non-nil, the headline after the keyword is set to the
  2362. `org-headline-done' as an additional indication."
  2363. :group 'org-font-lock
  2364. :type 'boolean)
  2365. (defcustom org-fontify-emphasized-text t
  2366. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2367. Changing this variable requires a restart of Emacs to take effect."
  2368. :group 'org-font-lock
  2369. :type 'boolean)
  2370. (defcustom org-fontify-whole-heading-line nil
  2371. "Non-nil means fontify the whole line for headings.
  2372. This is useful when setting a background color for the
  2373. org-leve-* faces."
  2374. :group 'org-font-lock
  2375. :type 'boolean)
  2376. (defcustom org-highlight-latex-fragments-and-specials nil
  2377. "Non-nil means, fontify what is treated specially by the exporters."
  2378. :group 'org-font-lock
  2379. :type 'boolean)
  2380. (defcustom org-hide-emphasis-markers nil
  2381. "Non-nil mean font-lock should hide the emphasis marker characters."
  2382. :group 'org-font-lock
  2383. :type 'boolean)
  2384. (defvar org-emph-re nil
  2385. "Regular expression for matching emphasis.")
  2386. (defvar org-verbatim-re nil
  2387. "Regular expression for matching verbatim text.")
  2388. (defvar org-emphasis-regexp-components) ; defined just below
  2389. (defvar org-emphasis-alist) ; defined just below
  2390. (defun org-set-emph-re (var val)
  2391. "Set variable and compute the emphasis regular expression."
  2392. (set var val)
  2393. (when (and (boundp 'org-emphasis-alist)
  2394. (boundp 'org-emphasis-regexp-components)
  2395. org-emphasis-alist org-emphasis-regexp-components)
  2396. (let* ((e org-emphasis-regexp-components)
  2397. (pre (car e))
  2398. (post (nth 1 e))
  2399. (border (nth 2 e))
  2400. (body (nth 3 e))
  2401. (nl (nth 4 e))
  2402. (body1 (concat body "*?"))
  2403. (markers (mapconcat 'car org-emphasis-alist ""))
  2404. (vmarkers (mapconcat
  2405. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2406. org-emphasis-alist "")))
  2407. ;; make sure special characters appear at the right position in the class
  2408. (if (string-match "\\^" markers)
  2409. (setq markers (concat (replace-match "" t t markers) "^")))
  2410. (if (string-match "-" markers)
  2411. (setq markers (concat (replace-match "" t t markers) "-")))
  2412. (if (string-match "\\^" vmarkers)
  2413. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2414. (if (string-match "-" vmarkers)
  2415. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2416. (if (> nl 0)
  2417. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2418. (int-to-string nl) "\\}")))
  2419. ;; Make the regexp
  2420. (setq org-emph-re
  2421. (concat "\\([" pre "]\\|^\\)"
  2422. "\\("
  2423. "\\([" markers "]\\)"
  2424. "\\("
  2425. "[^" border "]\\|"
  2426. "[^" border "]"
  2427. body1
  2428. "[^" border "]"
  2429. "\\)"
  2430. "\\3\\)"
  2431. "\\([" post "]\\|$\\)"))
  2432. (setq org-verbatim-re
  2433. (concat "\\([" pre "]\\|^\\)"
  2434. "\\("
  2435. "\\([" vmarkers "]\\)"
  2436. "\\("
  2437. "[^" border "]\\|"
  2438. "[^" border "]"
  2439. body1
  2440. "[^" border "]"
  2441. "\\)"
  2442. "\\3\\)"
  2443. "\\([" post "]\\|$\\)")))))
  2444. (defcustom org-emphasis-regexp-components
  2445. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2446. "Components used to build the regular expression for emphasis.
  2447. This is a list with 6 entries. Terminology: In an emphasis string
  2448. like \" *strong word* \", we call the initial space PREMATCH, the final
  2449. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2450. and \"trong wor\" is the body. The different components in this variable
  2451. specify what is allowed/forbidden in each part:
  2452. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2453. post Chars allowed as postmatch. End of line will be allowed too.
  2454. border The chars *forbidden* as border characters.
  2455. body-regexp A regexp like \".\" to match a body character. Don't use
  2456. non-shy groups here, and don't allow newline here.
  2457. newline The maximum number of newlines allowed in an emphasis exp.
  2458. Use customize to modify this, or restart Emacs after changing it."
  2459. :group 'org-font-lock
  2460. :set 'org-set-emph-re
  2461. :type '(list
  2462. (sexp :tag "Allowed chars in pre ")
  2463. (sexp :tag "Allowed chars in post ")
  2464. (sexp :tag "Forbidden chars in border ")
  2465. (sexp :tag "Regexp for body ")
  2466. (integer :tag "number of newlines allowed")
  2467. (option (boolean :tag "Please ignore this button"))))
  2468. (defcustom org-emphasis-alist
  2469. `(("*" bold "<b>" "</b>")
  2470. ("/" italic "<i>" "</i>")
  2471. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2472. ("=" org-code "<code>" "</code>" verbatim)
  2473. ("~" org-verbatim "<code>" "</code>" verbatim)
  2474. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2475. "<del>" "</del>")
  2476. )
  2477. "Special syntax for emphasized text.
  2478. Text starting and ending with a special character will be emphasized, for
  2479. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2480. characters, the face to be used by font-lock for highlighting in Org-mode
  2481. Emacs buffers, and the HTML tags to be used for this.
  2482. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2483. Use customize to modify this, or restart Emacs after changing it."
  2484. :group 'org-font-lock
  2485. :set 'org-set-emph-re
  2486. :type '(repeat
  2487. (list
  2488. (string :tag "Marker character")
  2489. (choice
  2490. (face :tag "Font-lock-face")
  2491. (plist :tag "Face property list"))
  2492. (string :tag "HTML start tag")
  2493. (string :tag "HTML end tag")
  2494. (option (const verbatim)))))
  2495. (defvar org-protecting-blocks
  2496. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2497. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2498. This is needed for font-lock setup.")
  2499. ;;; Miscellaneous options
  2500. (defgroup org-completion nil
  2501. "Completion in Org-mode."
  2502. :tag "Org Completion"
  2503. :group 'org)
  2504. (defcustom org-completion-use-ido nil
  2505. "Non-nil means, use ido completion wherever possible.
  2506. Note that `ido-mode' must be active for this variable to be relevant.
  2507. If you decide to turn this variable on, you might well want to turn off
  2508. `org-outline-path-complete-in-steps'.
  2509. See also `org-completion-use-iswitchb'."
  2510. :group 'org-completion
  2511. :type 'boolean)
  2512. (defcustom org-completion-use-iswitchb nil
  2513. "Non-nil means, use iswitchb completion wherever possible.
  2514. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2515. If you decide to turn this variable on, you might well want to turn off
  2516. `org-outline-path-complete-in-steps'.
  2517. Note that thi variable has only an effect if `org-completion-use-ido' is nil."
  2518. :group 'org-completion
  2519. :type 'boolean)
  2520. (defcustom org-completion-fallback-command 'hippie-expand
  2521. "The expansion command called by \\[org-complete] in normal context.
  2522. Normal means, no org-mode-specific context."
  2523. :group 'org-completion
  2524. :type 'function)
  2525. ;;; Functions and variables from ther packages
  2526. ;; Declared here to avoid compiler warnings
  2527. ;; XEmacs only
  2528. (defvar outline-mode-menu-heading)
  2529. (defvar outline-mode-menu-show)
  2530. (defvar outline-mode-menu-hide)
  2531. (defvar zmacs-regions) ; XEmacs regions
  2532. ;; Emacs only
  2533. (defvar mark-active)
  2534. ;; Various packages
  2535. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2536. (declare-function calendar-forward-day "cal-move" (arg))
  2537. (declare-function calendar-goto-date "cal-move" (date))
  2538. (declare-function calendar-goto-today "cal-move" ())
  2539. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2540. (defvar calc-embedded-close-formula)
  2541. (defvar calc-embedded-open-formula)
  2542. (declare-function cdlatex-tab "ext:cdlatex" ())
  2543. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2544. (defvar font-lock-unfontify-region-function)
  2545. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2546. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2547. (defvar iswitchb-temp-buflist)
  2548. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2549. (defvar org-agenda-tags-todo-honor-ignore-options)
  2550. (declare-function org-agenda-skip "org-agenda" ())
  2551. (declare-function org-format-agenda-item "org-agenda"
  2552. (extra txt &optional category tags dotime noprefix remove-re))
  2553. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2554. (declare-function org-agenda-change-all-lines "org-agenda"
  2555. (newhead hdmarker &optional fixface just-this))
  2556. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2557. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2558. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2559. (beg end))
  2560. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2561. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2562. "org-agenda" (&optional end))
  2563. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2564. (declare-function org-indent-mode "org-indent" (arg))
  2565. (declare-function parse-time-string "parse-time" (string))
  2566. (declare-function remember "remember" (&optional initial))
  2567. (declare-function remember-buffer-desc "remember" ())
  2568. (declare-function remember-finalize "remember" ())
  2569. (defvar remember-save-after-remembering)
  2570. (defvar remember-data-file)
  2571. (defvar remember-register)
  2572. (defvar remember-buffer)
  2573. (defvar remember-handler-functions)
  2574. (defvar remember-annotation-functions)
  2575. (defvar texmathp-why)
  2576. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2577. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2578. (defvar w3m-current-url)
  2579. (defvar w3m-current-title)
  2580. (defvar org-latex-regexps)
  2581. ;;; Autoload and prepare some org modules
  2582. ;; Some table stuff that needs to be defined here, because it is used
  2583. ;; by the functions setting up org-mode or checking for table context.
  2584. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2585. "Detects an org-type or table-type table.")
  2586. (defconst org-table-line-regexp "^[ \t]*|"
  2587. "Detects an org-type table line.")
  2588. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2589. "Detects an org-type table line.")
  2590. (defconst org-table-hline-regexp "^[ \t]*|-"
  2591. "Detects an org-type table hline.")
  2592. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2593. "Detects a table-type table hline.")
  2594. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2595. "Searching from within a table (any type) this finds the first line
  2596. outside the table.")
  2597. ;; Autoload the functions in org-table.el that are needed by functions here.
  2598. (eval-and-compile
  2599. (org-autoload "org-table"
  2600. '(org-table-align org-table-begin org-table-blank-field
  2601. org-table-convert org-table-convert-region org-table-copy-down
  2602. org-table-copy-region org-table-create
  2603. org-table-create-or-convert-from-region
  2604. org-table-create-with-table.el org-table-current-dline
  2605. org-table-cut-region org-table-delete-column org-table-edit-field
  2606. org-table-edit-formulas org-table-end org-table-eval-formula
  2607. org-table-export org-table-field-info
  2608. org-table-get-stored-formulas org-table-goto-column
  2609. org-table-hline-and-move org-table-import org-table-insert-column
  2610. org-table-insert-hline org-table-insert-row org-table-iterate
  2611. org-table-justify-field-maybe org-table-kill-row
  2612. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2613. org-table-move-column org-table-move-column-left
  2614. org-table-move-column-right org-table-move-row
  2615. org-table-move-row-down org-table-move-row-up
  2616. org-table-next-field org-table-next-row org-table-paste-rectangle
  2617. org-table-previous-field org-table-recalculate
  2618. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2619. org-table-toggle-coordinate-overlays
  2620. org-table-toggle-formula-debugger org-table-wrap-region
  2621. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2622. (defun org-at-table-p (&optional table-type)
  2623. "Return t if the cursor is inside an org-type table.
  2624. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2625. (if org-enable-table-editor
  2626. (save-excursion
  2627. (beginning-of-line 1)
  2628. (looking-at (if table-type org-table-any-line-regexp
  2629. org-table-line-regexp)))
  2630. nil))
  2631. (defsubst org-table-p () (org-at-table-p))
  2632. (defun org-at-table.el-p ()
  2633. "Return t if and only if we are at a table.el table."
  2634. (and (org-at-table-p 'any)
  2635. (save-excursion
  2636. (goto-char (org-table-begin 'any))
  2637. (looking-at org-table1-hline-regexp))))
  2638. (defun org-table-recognize-table.el ()
  2639. "If there is a table.el table nearby, recognize it and move into it."
  2640. (if org-table-tab-recognizes-table.el
  2641. (if (org-at-table.el-p)
  2642. (progn
  2643. (beginning-of-line 1)
  2644. (if (looking-at org-table-dataline-regexp)
  2645. nil
  2646. (if (looking-at org-table1-hline-regexp)
  2647. (progn
  2648. (beginning-of-line 2)
  2649. (if (looking-at org-table-any-border-regexp)
  2650. (beginning-of-line -1)))))
  2651. (if (re-search-forward "|" (org-table-end t) t)
  2652. (progn
  2653. (require 'table)
  2654. (if (table--at-cell-p (point))
  2655. t
  2656. (message "recognizing table.el table...")
  2657. (table-recognize-table)
  2658. (message "recognizing table.el table...done")))
  2659. (error "This should not happen..."))
  2660. t)
  2661. nil)
  2662. nil))
  2663. (defun org-at-table-hline-p ()
  2664. "Return t if the cursor is inside a hline in a table."
  2665. (if org-enable-table-editor
  2666. (save-excursion
  2667. (beginning-of-line 1)
  2668. (looking-at org-table-hline-regexp))
  2669. nil))
  2670. (defvar org-table-clean-did-remove-column nil)
  2671. (defun org-table-map-tables (function)
  2672. "Apply FUNCTION to the start of all tables in the buffer."
  2673. (save-excursion
  2674. (save-restriction
  2675. (widen)
  2676. (goto-char (point-min))
  2677. (while (re-search-forward org-table-any-line-regexp nil t)
  2678. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2679. (beginning-of-line 1)
  2680. (when (looking-at org-table-line-regexp)
  2681. (save-excursion (funcall function))
  2682. (or (looking-at org-table-line-regexp)
  2683. (forward-char 1)))
  2684. (re-search-forward org-table-any-border-regexp nil 1))))
  2685. (message "Mapping tables: done"))
  2686. ;; Declare and autoload functions from org-exp.el & Co
  2687. (declare-function org-default-export-plist "org-exp")
  2688. (declare-function org-infile-export-plist "org-exp")
  2689. (declare-function org-get-current-options "org-exp")
  2690. (eval-and-compile
  2691. (org-autoload "org-exp"
  2692. '(org-export org-export-visible
  2693. org-insert-export-options-template
  2694. org-table-clean-before-export))
  2695. (org-autoload "org-ascii"
  2696. '(org-export-as-ascii org-export-ascii-preprocess
  2697. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2698. org-export-region-as-ascii))
  2699. (org-autoload "org-html"
  2700. '(org-export-as-html-and-open
  2701. org-export-as-html-batch org-export-as-html-to-buffer
  2702. org-replace-region-by-html org-export-region-as-html
  2703. org-export-as-html))
  2704. (org-autoload "org-icalendar"
  2705. '(org-export-icalendar-this-file
  2706. org-export-icalendar-all-agenda-files
  2707. org-export-icalendar-combine-agenda-files))
  2708. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2709. ;; Declare and autoload functions from org-agenda.el
  2710. (eval-and-compile
  2711. (org-autoload "org-agenda"
  2712. '(org-agenda org-agenda-list org-search-view
  2713. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2714. org-diary org-agenda-to-appt
  2715. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2716. ;; Autoload org-remember
  2717. (eval-and-compile
  2718. (org-autoload "org-remember"
  2719. '(org-remember-insinuate org-remember-annotation
  2720. org-remember-apply-template org-remember org-remember-handler)))
  2721. ;; Autoload org-clock.el
  2722. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2723. (beg end))
  2724. (declare-function org-clock-update-mode-line "org-clock" ())
  2725. (defvar org-clock-start-time)
  2726. (defvar org-clock-marker (make-marker)
  2727. "Marker recording the last clock-in.")
  2728. (defvar org-clock-hd-marker (make-marker)
  2729. "Marker recording the last clock-in, but the headline position.")
  2730. (defun org-clock-is-active ()
  2731. "Return non-nil if clock is currently running.
  2732. The return value is actually the clock marker."
  2733. (marker-buffer org-clock-marker))
  2734. (eval-and-compile
  2735. (org-autoload
  2736. "org-clock"
  2737. '(org-clock-in org-clock-out org-clock-cancel
  2738. org-clock-goto org-clock-sum org-clock-display
  2739. org-clock-remove-overlays org-clock-report
  2740. org-clocktable-shift org-dblock-write:clocktable
  2741. org-get-clocktable)))
  2742. (defun org-clock-update-time-maybe ()
  2743. "If this is a CLOCK line, update it and return t.
  2744. Otherwise, return nil."
  2745. (interactive)
  2746. (save-excursion
  2747. (beginning-of-line 1)
  2748. (skip-chars-forward " \t")
  2749. (when (looking-at org-clock-string)
  2750. (let ((re (concat "[ \t]*" org-clock-string
  2751. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2752. "\\([ \t]*=>.*\\)?\\)?"))
  2753. ts te h m s neg)
  2754. (cond
  2755. ((not (looking-at re))
  2756. nil)
  2757. ((not (match-end 2))
  2758. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2759. (> org-clock-marker (point))
  2760. (<= org-clock-marker (point-at-eol)))
  2761. ;; The clock is running here
  2762. (setq org-clock-start-time
  2763. (apply 'encode-time
  2764. (org-parse-time-string (match-string 1))))
  2765. (org-clock-update-mode-line)))
  2766. (t
  2767. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2768. (end-of-line 1)
  2769. (setq ts (match-string 1)
  2770. te (match-string 3))
  2771. (setq s (- (org-float-time
  2772. (apply 'encode-time (org-parse-time-string te)))
  2773. (org-float-time
  2774. (apply 'encode-time (org-parse-time-string ts))))
  2775. neg (< s 0)
  2776. s (abs s)
  2777. h (floor (/ s 3600))
  2778. s (- s (* 3600 h))
  2779. m (floor (/ s 60))
  2780. s (- s (* 60 s)))
  2781. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2782. t))))))
  2783. (defun org-check-running-clock ()
  2784. "Check if the current buffer contains the running clock.
  2785. If yes, offer to stop it and to save the buffer with the changes."
  2786. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2787. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2788. (buffer-name))))
  2789. (org-clock-out)
  2790. (when (y-or-n-p "Save changed buffer?")
  2791. (save-buffer))))
  2792. (defun org-clocktable-try-shift (dir n)
  2793. "Check if this line starts a clock table, if yes, shift the time block."
  2794. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2795. (org-clocktable-shift dir n)))
  2796. ;; Autoload org-timer.el
  2797. (eval-and-compile
  2798. (org-autoload
  2799. "org-timer"
  2800. '(org-timer-start org-timer org-timer-item
  2801. org-timer-change-times-in-region
  2802. org-timer-set-timer
  2803. org-timer-reset-timers
  2804. org-timer-show-remaining-time)))
  2805. ;; Autoload org-feed.el
  2806. (eval-and-compile
  2807. (org-autoload
  2808. "org-feed"
  2809. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2810. ;; Autoload org-indent.el
  2811. (eval-and-compile
  2812. (org-autoload
  2813. "org-indent"
  2814. '(org-indent-mode)))
  2815. ;; Autoload org-mobile.el
  2816. (eval-and-compile
  2817. (org-autoload
  2818. "org-mobile"
  2819. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  2820. ;; Autoload archiving code
  2821. ;; The stuff that is needed for cycling and tags has to be defined here.
  2822. (defgroup org-archive nil
  2823. "Options concerning archiving in Org-mode."
  2824. :tag "Org Archive"
  2825. :group 'org-structure)
  2826. (defcustom org-archive-location "%s_archive::"
  2827. "The location where subtrees should be archived.
  2828. The value of this variable is a string, consisting of two parts,
  2829. separated by a double-colon. The first part is a filename and
  2830. the second part is a headline.
  2831. When the filename is omitted, archiving happens in the same file.
  2832. %s in the filename will be replaced by the current file
  2833. name (without the directory part). Archiving to a different file
  2834. is useful to keep archived entries from contributing to the
  2835. Org-mode Agenda.
  2836. The archived entries will be filed as subtrees of the specified
  2837. headline. When the headline is omitted, the subtrees are simply
  2838. filed away at the end of the file, as top-level entries. Also in
  2839. the heading you can use %s to represent the file name, this can be
  2840. useful when using the same archive for a number of different files.
  2841. Here are a few examples:
  2842. \"%s_archive::\"
  2843. If the current file is Projects.org, archive in file
  2844. Projects.org_archive, as top-level trees. This is the default.
  2845. \"::* Archived Tasks\"
  2846. Archive in the current file, under the top-level headline
  2847. \"* Archived Tasks\".
  2848. \"~/org/archive.org::\"
  2849. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2850. \"~/org/archive.org::From %s\"
  2851. Archive in file ~/org/archive.org (absolute path), und headlines
  2852. \"From FILENAME\" where file name is the current file name.
  2853. \"basement::** Finished Tasks\"
  2854. Archive in file ./basement (relative path), as level 3 trees
  2855. below the level 2 heading \"** Finished Tasks\".
  2856. You may set this option on a per-file basis by adding to the buffer a
  2857. line like
  2858. #+ARCHIVE: basement::** Finished Tasks
  2859. You may also define it locally for a subtree by setting an ARCHIVE property
  2860. in the entry. If such a property is found in an entry, or anywhere up
  2861. the hierarchy, it will be used."
  2862. :group 'org-archive
  2863. :type 'string)
  2864. (defcustom org-archive-tag "ARCHIVE"
  2865. "The tag that marks a subtree as archived.
  2866. An archived subtree does not open during visibility cycling, and does
  2867. not contribute to the agenda listings.
  2868. After changing this, font-lock must be restarted in the relevant buffers to
  2869. get the proper fontification."
  2870. :group 'org-archive
  2871. :group 'org-keywords
  2872. :type 'string)
  2873. (defcustom org-agenda-skip-archived-trees t
  2874. "Non-nil means, the agenda will skip any items located in archived trees.
  2875. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2876. variable is no longer recommended, you should leave it at the value t.
  2877. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2878. :group 'org-archive
  2879. :group 'org-agenda-skip
  2880. :type 'boolean)
  2881. (defcustom org-columns-skip-arrchived-trees t
  2882. "Non-nil means, irgnore archived trees when creating column view."
  2883. :group 'org-archive
  2884. :group 'org-properties
  2885. :type 'boolean)
  2886. (defcustom org-cycle-open-archived-trees nil
  2887. "Non-nil means, `org-cycle' will open archived trees.
  2888. An archived tree is a tree marked with the tag ARCHIVE.
  2889. When nil, archived trees will stay folded. You can still open them with
  2890. normal outline commands like `show-all', but not with the cycling commands."
  2891. :group 'org-archive
  2892. :group 'org-cycle
  2893. :type 'boolean)
  2894. (defcustom org-sparse-tree-open-archived-trees nil
  2895. "Non-nil means sparse tree construction shows matches in archived trees.
  2896. When nil, matches in these trees are highlighted, but the trees are kept in
  2897. collapsed state."
  2898. :group 'org-archive
  2899. :group 'org-sparse-trees
  2900. :type 'boolean)
  2901. (defun org-cycle-hide-archived-subtrees (state)
  2902. "Re-hide all archived subtrees after a visibility state change."
  2903. (when (and (not org-cycle-open-archived-trees)
  2904. (not (memq state '(overview folded))))
  2905. (save-excursion
  2906. (let* ((globalp (memq state '(contents all)))
  2907. (beg (if globalp (point-min) (point)))
  2908. (end (if globalp (point-max) (org-end-of-subtree t))))
  2909. (org-hide-archived-subtrees beg end)
  2910. (goto-char beg)
  2911. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2912. (message "%s" (substitute-command-keys
  2913. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2914. (defun org-force-cycle-archived ()
  2915. "Cycle subtree even if it is archived."
  2916. (interactive)
  2917. (setq this-command 'org-cycle)
  2918. (let ((org-cycle-open-archived-trees t))
  2919. (call-interactively 'org-cycle)))
  2920. (defun org-hide-archived-subtrees (beg end)
  2921. "Re-hide all archived subtrees after a visibility state change."
  2922. (save-excursion
  2923. (let* ((re (concat ":" org-archive-tag ":")))
  2924. (goto-char beg)
  2925. (while (re-search-forward re end t)
  2926. (and (org-on-heading-p) (hide-subtree))
  2927. (org-end-of-subtree t)))))
  2928. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2929. (eval-and-compile
  2930. (org-autoload "org-archive"
  2931. '(org-add-archive-files org-archive-subtree
  2932. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2933. ;; Autoload Column View Code
  2934. (declare-function org-columns-number-to-string "org-colview")
  2935. (declare-function org-columns-get-format-and-top-level "org-colview")
  2936. (declare-function org-columns-compute "org-colview")
  2937. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2938. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2939. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2940. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2941. ;; Autoload ID code
  2942. (declare-function org-id-store-link "org-id")
  2943. (declare-function org-id-locations-load "org-id")
  2944. (declare-function org-id-locations-save "org-id")
  2945. (defvar org-id-track-globally)
  2946. (org-autoload "org-id"
  2947. '(org-id-get-create org-id-new org-id-copy org-id-get
  2948. org-id-get-with-outline-path-completion
  2949. org-id-get-with-outline-drilling
  2950. org-id-goto org-id-find org-id-store-link))
  2951. ;; Autoload Plotting Code
  2952. (org-autoload "org-plot"
  2953. '(org-plot/gnuplot))
  2954. ;;; Variables for pre-computed regular expressions, all buffer local
  2955. (defvar org-drawer-regexp nil
  2956. "Matches first line of a hidden block.")
  2957. (make-variable-buffer-local 'org-drawer-regexp)
  2958. (defvar org-todo-regexp nil
  2959. "Matches any of the TODO state keywords.")
  2960. (make-variable-buffer-local 'org-todo-regexp)
  2961. (defvar org-not-done-regexp nil
  2962. "Matches any of the TODO state keywords except the last one.")
  2963. (make-variable-buffer-local 'org-not-done-regexp)
  2964. (defvar org-not-done-heading-regexp nil
  2965. "Matches a TODO headline that is not done.")
  2966. (make-variable-buffer-local 'org-not-done-regexp)
  2967. (defvar org-todo-line-regexp nil
  2968. "Matches a headline and puts TODO state into group 2 if present.")
  2969. (make-variable-buffer-local 'org-todo-line-regexp)
  2970. (defvar org-complex-heading-regexp nil
  2971. "Matches a headline and puts everything into groups:
  2972. group 1: the stars
  2973. group 2: The todo keyword, maybe
  2974. group 3: Priority cookie
  2975. group 4: True headline
  2976. group 5: Tags")
  2977. (make-variable-buffer-local 'org-complex-heading-regexp)
  2978. (defvar org-todo-line-tags-regexp nil
  2979. "Matches a headline and puts TODO state into group 2 if present.
  2980. Also put tags into group 4 if tags are present.")
  2981. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2982. (defvar org-nl-done-regexp nil
  2983. "Matches newline followed by a headline with the DONE keyword.")
  2984. (make-variable-buffer-local 'org-nl-done-regexp)
  2985. (defvar org-looking-at-done-regexp nil
  2986. "Matches the DONE keyword a point.")
  2987. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2988. (defvar org-ds-keyword-length 12
  2989. "Maximum length of the Deadline and SCHEDULED keywords.")
  2990. (make-variable-buffer-local 'org-ds-keyword-length)
  2991. (defvar org-deadline-regexp nil
  2992. "Matches the DEADLINE keyword.")
  2993. (make-variable-buffer-local 'org-deadline-regexp)
  2994. (defvar org-deadline-time-regexp nil
  2995. "Matches the DEADLINE keyword together with a time stamp.")
  2996. (make-variable-buffer-local 'org-deadline-time-regexp)
  2997. (defvar org-deadline-line-regexp nil
  2998. "Matches the DEADLINE keyword and the rest of the line.")
  2999. (make-variable-buffer-local 'org-deadline-line-regexp)
  3000. (defvar org-scheduled-regexp nil
  3001. "Matches the SCHEDULED keyword.")
  3002. (make-variable-buffer-local 'org-scheduled-regexp)
  3003. (defvar org-scheduled-time-regexp nil
  3004. "Matches the SCHEDULED keyword together with a time stamp.")
  3005. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3006. (defvar org-closed-time-regexp nil
  3007. "Matches the CLOSED keyword together with a time stamp.")
  3008. (make-variable-buffer-local 'org-closed-time-regexp)
  3009. (defvar org-keyword-time-regexp nil
  3010. "Matches any of the 4 keywords, together with the time stamp.")
  3011. (make-variable-buffer-local 'org-keyword-time-regexp)
  3012. (defvar org-keyword-time-not-clock-regexp nil
  3013. "Matches any of the 3 keywords, together with the time stamp.")
  3014. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3015. (defvar org-maybe-keyword-time-regexp nil
  3016. "Matches a timestamp, possibly preceeded by a keyword.")
  3017. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3018. (defvar org-planning-or-clock-line-re nil
  3019. "Matches a line with planning or clock info.")
  3020. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3021. (defconst org-plain-time-of-day-regexp
  3022. (concat
  3023. "\\(\\<[012]?[0-9]"
  3024. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3025. "\\(--?"
  3026. "\\(\\<[012]?[0-9]"
  3027. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3028. "\\)?")
  3029. "Regular expression to match a plain time or time range.
  3030. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3031. groups carry important information:
  3032. 0 the full match
  3033. 1 the first time, range or not
  3034. 8 the second time, if it is a range.")
  3035. (defconst org-plain-time-extension-regexp
  3036. (concat
  3037. "\\(\\<[012]?[0-9]"
  3038. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3039. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3040. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3041. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3042. groups carry important information:
  3043. 0 the full match
  3044. 7 hours of duration
  3045. 9 minutes of duration")
  3046. (defconst org-stamp-time-of-day-regexp
  3047. (concat
  3048. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3049. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3050. "\\(--?"
  3051. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3052. "Regular expression to match a timestamp time or time range.
  3053. After a match, the following groups carry important information:
  3054. 0 the full match
  3055. 1 date plus weekday, for backreferencing to make sure both times on same day
  3056. 2 the first time, range or not
  3057. 4 the second time, if it is a range.")
  3058. (defconst org-startup-options
  3059. '(("fold" org-startup-folded t)
  3060. ("overview" org-startup-folded t)
  3061. ("nofold" org-startup-folded nil)
  3062. ("showall" org-startup-folded nil)
  3063. ("showeverything" org-startup-folded showeverything)
  3064. ("content" org-startup-folded content)
  3065. ("indent" org-startup-indented t)
  3066. ("noindent" org-startup-indented nil)
  3067. ("hidestars" org-hide-leading-stars t)
  3068. ("showstars" org-hide-leading-stars nil)
  3069. ("odd" org-odd-levels-only t)
  3070. ("oddeven" org-odd-levels-only nil)
  3071. ("align" org-startup-align-all-tables t)
  3072. ("noalign" org-startup-align-all-tables nil)
  3073. ("customtime" org-display-custom-times t)
  3074. ("logdone" org-log-done time)
  3075. ("lognotedone" org-log-done note)
  3076. ("nologdone" org-log-done nil)
  3077. ("lognoteclock-out" org-log-note-clock-out t)
  3078. ("nolognoteclock-out" org-log-note-clock-out nil)
  3079. ("logrepeat" org-log-repeat state)
  3080. ("lognoterepeat" org-log-repeat note)
  3081. ("nologrepeat" org-log-repeat nil)
  3082. ("fninline" org-footnote-define-inline t)
  3083. ("nofninline" org-footnote-define-inline nil)
  3084. ("fnlocal" org-footnote-section nil)
  3085. ("fnauto" org-footnote-auto-label t)
  3086. ("fnprompt" org-footnote-auto-label nil)
  3087. ("fnconfirm" org-footnote-auto-label confirm)
  3088. ("fnplain" org-footnote-auto-label plain)
  3089. ("fnadjust" org-footnote-auto-adjust t)
  3090. ("nofnadjust" org-footnote-auto-adjust nil)
  3091. ("constcgs" constants-unit-system cgs)
  3092. ("constSI" constants-unit-system SI)
  3093. ("noptag" org-tag-persistent-alist nil)
  3094. ("hideblocks" org-hide-block-startup t)
  3095. ("nohideblocks" org-hide-block-startup nil))
  3096. "Variable associated with STARTUP options for org-mode.
  3097. Each element is a list of three items: The startup options as written
  3098. in the #+STARTUP line, the corresponding variable, and the value to
  3099. set this variable to if the option is found. An optional forth element PUSH
  3100. means to push this value onto the list in the variable.")
  3101. (defun org-set-regexps-and-options ()
  3102. "Precompute regular expressions for current buffer."
  3103. (when (org-mode-p)
  3104. (org-set-local 'org-todo-kwd-alist nil)
  3105. (org-set-local 'org-todo-key-alist nil)
  3106. (org-set-local 'org-todo-key-trigger nil)
  3107. (org-set-local 'org-todo-keywords-1 nil)
  3108. (org-set-local 'org-done-keywords nil)
  3109. (org-set-local 'org-todo-heads nil)
  3110. (org-set-local 'org-todo-sets nil)
  3111. (org-set-local 'org-todo-log-states nil)
  3112. (org-set-local 'org-file-properties nil)
  3113. (org-set-local 'org-file-tags nil)
  3114. (let ((re (org-make-options-regexp
  3115. '("CATEGORY" "TODO" "COLUMNS"
  3116. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3117. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3118. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3119. (splitre "[ \t]+")
  3120. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3121. tail sep kws1 prio props ftags drawers
  3122. ext-setup-or-nil setup-contents (start 0))
  3123. (save-excursion
  3124. (save-restriction
  3125. (widen)
  3126. (goto-char (point-min))
  3127. (while (or (and ext-setup-or-nil
  3128. (string-match re ext-setup-or-nil start)
  3129. (setq start (match-end 0)))
  3130. (and (setq ext-setup-or-nil nil start 0)
  3131. (re-search-forward re nil t)))
  3132. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3133. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3134. (cond
  3135. ((equal key "CATEGORY")
  3136. (if (string-match "[ \t]+$" value)
  3137. (setq value (replace-match "" t t value)))
  3138. (setq cat value))
  3139. ((member key '("SEQ_TODO" "TODO"))
  3140. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3141. ((equal key "TYP_TODO")
  3142. (push (cons 'type (org-split-string value splitre)) kwds))
  3143. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3144. ;; general TODO-like setup
  3145. (push (cons (intern (downcase (match-string 1 key)))
  3146. (org-split-string value splitre)) kwds))
  3147. ((equal key "TAGS")
  3148. (setq tags (append tags (if tags '("\\n") nil)
  3149. (org-split-string value splitre))))
  3150. ((equal key "COLUMNS")
  3151. (org-set-local 'org-columns-default-format value))
  3152. ((equal key "LINK")
  3153. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3154. (push (cons (match-string 1 value)
  3155. (org-trim (match-string 2 value)))
  3156. links)))
  3157. ((equal key "PRIORITIES")
  3158. (setq prio (org-split-string value " +")))
  3159. ((equal key "PROPERTY")
  3160. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3161. (push (cons (match-string 1 value) (match-string 2 value))
  3162. props)))
  3163. ((equal key "FILETAGS")
  3164. (when (string-match "\\S-" value)
  3165. (setq ftags
  3166. (append
  3167. ftags
  3168. (apply 'append
  3169. (mapcar (lambda (x) (org-split-string x ":"))
  3170. (org-split-string value)))))))
  3171. ((equal key "DRAWERS")
  3172. (setq drawers (org-split-string value splitre)))
  3173. ((equal key "CONSTANTS")
  3174. (setq const (append const (org-split-string value splitre))))
  3175. ((equal key "STARTUP")
  3176. (let ((opts (org-split-string value splitre))
  3177. l var val)
  3178. (while (setq l (pop opts))
  3179. (when (setq l (assoc l org-startup-options))
  3180. (setq var (nth 1 l) val (nth 2 l))
  3181. (if (not (nth 3 l))
  3182. (set (make-local-variable var) val)
  3183. (if (not (listp (symbol-value var)))
  3184. (set (make-local-variable var) nil))
  3185. (set (make-local-variable var) (symbol-value var))
  3186. (add-to-list var val))))))
  3187. ((equal key "ARCHIVE")
  3188. (string-match " *$" value)
  3189. (setq arch (replace-match "" t t value))
  3190. (remove-text-properties 0 (length arch)
  3191. '(face t fontified t) arch))
  3192. ((equal key "SETUPFILE")
  3193. (setq setup-contents (org-file-contents
  3194. (expand-file-name
  3195. (org-remove-double-quotes value))
  3196. 'noerror))
  3197. (if (not ext-setup-or-nil)
  3198. (setq ext-setup-or-nil setup-contents start 0)
  3199. (setq ext-setup-or-nil
  3200. (concat (substring ext-setup-or-nil 0 start)
  3201. "\n" setup-contents "\n"
  3202. (substring ext-setup-or-nil start)))))
  3203. ))))
  3204. (when cat
  3205. (org-set-local 'org-category (intern cat))
  3206. (push (cons "CATEGORY" cat) props))
  3207. (when prio
  3208. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3209. (setq prio (mapcar 'string-to-char prio))
  3210. (org-set-local 'org-highest-priority (nth 0 prio))
  3211. (org-set-local 'org-lowest-priority (nth 1 prio))
  3212. (org-set-local 'org-default-priority (nth 2 prio)))
  3213. (and props (org-set-local 'org-file-properties (nreverse props)))
  3214. (and ftags (org-set-local 'org-file-tags
  3215. (mapcar 'org-add-prop-inherited ftags)))
  3216. (and drawers (org-set-local 'org-drawers drawers))
  3217. (and arch (org-set-local 'org-archive-location arch))
  3218. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3219. ;; Process the TODO keywords
  3220. (unless kwds
  3221. ;; Use the global values as if they had been given locally.
  3222. (setq kwds (default-value 'org-todo-keywords))
  3223. (if (stringp (car kwds))
  3224. (setq kwds (list (cons org-todo-interpretation
  3225. (default-value 'org-todo-keywords)))))
  3226. (setq kwds (reverse kwds)))
  3227. (setq kwds (nreverse kwds))
  3228. (let (inter kws kw)
  3229. (while (setq kws (pop kwds))
  3230. (let ((kws (or
  3231. (run-hook-with-args-until-success
  3232. 'org-todo-setup-filter-hook kws)
  3233. kws)))
  3234. (setq inter (pop kws) sep (member "|" kws)
  3235. kws0 (delete "|" (copy-sequence kws))
  3236. kwsa nil
  3237. kws1 (mapcar
  3238. (lambda (x)
  3239. ;; 1 2
  3240. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3241. (progn
  3242. (setq kw (match-string 1 x)
  3243. key (and (match-end 2) (match-string 2 x))
  3244. log (org-extract-log-state-settings x))
  3245. (push (cons kw (and key (string-to-char key))) kwsa)
  3246. (and log (push log org-todo-log-states))
  3247. kw)
  3248. (error "Invalid TODO keyword %s" x)))
  3249. kws0)
  3250. kwsa (if kwsa (append '((:startgroup))
  3251. (nreverse kwsa)
  3252. '((:endgroup))))
  3253. hw (car kws1)
  3254. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3255. tail (list inter hw (car dws) (org-last dws))))
  3256. (add-to-list 'org-todo-heads hw 'append)
  3257. (push kws1 org-todo-sets)
  3258. (setq org-done-keywords (append org-done-keywords dws nil))
  3259. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3260. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3261. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3262. (setq org-todo-sets (nreverse org-todo-sets)
  3263. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3264. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3265. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3266. ;; Process the constants
  3267. (when const
  3268. (let (e cst)
  3269. (while (setq e (pop const))
  3270. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3271. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3272. (setq org-table-formula-constants-local cst)))
  3273. ;; Process the tags.
  3274. (when tags
  3275. (let (e tgs)
  3276. (while (setq e (pop tags))
  3277. (cond
  3278. ((equal e "{") (push '(:startgroup) tgs))
  3279. ((equal e "}") (push '(:endgroup) tgs))
  3280. ((equal e "\\n") (push '(:newline) tgs))
  3281. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3282. (push (cons (match-string 1 e)
  3283. (string-to-char (match-string 2 e)))
  3284. tgs))
  3285. (t (push (list e) tgs))))
  3286. (org-set-local 'org-tag-alist nil)
  3287. (while (setq e (pop tgs))
  3288. (or (and (stringp (car e))
  3289. (assoc (car e) org-tag-alist))
  3290. (push e org-tag-alist)))))
  3291. ;; Compute the regular expressions and other local variables
  3292. (if (not org-done-keywords)
  3293. (setq org-done-keywords (and org-todo-keywords-1
  3294. (list (org-last org-todo-keywords-1)))))
  3295. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3296. (length org-scheduled-string)
  3297. (length org-clock-string)
  3298. (length org-closed-string)))
  3299. org-drawer-regexp
  3300. (concat "^[ \t]*:\\("
  3301. (mapconcat 'regexp-quote org-drawers "\\|")
  3302. "\\):[ \t]*$")
  3303. org-not-done-keywords
  3304. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3305. org-todo-regexp
  3306. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3307. "\\|") "\\)\\>")
  3308. org-not-done-regexp
  3309. (concat "\\<\\("
  3310. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3311. "\\)\\>")
  3312. org-not-done-heading-regexp
  3313. (concat "^\\(\\*+\\)[ \t]+\\("
  3314. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3315. "\\)\\>")
  3316. org-todo-line-regexp
  3317. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3318. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3319. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3320. org-complex-heading-regexp
  3321. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3322. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3323. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3324. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3325. org-nl-done-regexp
  3326. (concat "\n\\*+[ \t]+"
  3327. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3328. "\\)" "\\>")
  3329. org-todo-line-tags-regexp
  3330. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3331. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3332. (org-re
  3333. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3334. org-looking-at-done-regexp
  3335. (concat "^" "\\(?:"
  3336. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3337. "\\>")
  3338. org-deadline-regexp (concat "\\<" org-deadline-string)
  3339. org-deadline-time-regexp
  3340. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3341. org-deadline-line-regexp
  3342. (concat "\\<\\(" org-deadline-string "\\).*")
  3343. org-scheduled-regexp
  3344. (concat "\\<" org-scheduled-string)
  3345. org-scheduled-time-regexp
  3346. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3347. org-closed-time-regexp
  3348. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3349. org-keyword-time-regexp
  3350. (concat "\\<\\(" org-scheduled-string
  3351. "\\|" org-deadline-string
  3352. "\\|" org-closed-string
  3353. "\\|" org-clock-string "\\)"
  3354. " *[[<]\\([^]>]+\\)[]>]")
  3355. org-keyword-time-not-clock-regexp
  3356. (concat "\\<\\(" org-scheduled-string
  3357. "\\|" org-deadline-string
  3358. "\\|" org-closed-string
  3359. "\\)"
  3360. " *[[<]\\([^]>]+\\)[]>]")
  3361. org-maybe-keyword-time-regexp
  3362. (concat "\\(\\<\\(" org-scheduled-string
  3363. "\\|" org-deadline-string
  3364. "\\|" org-closed-string
  3365. "\\|" org-clock-string "\\)\\)?"
  3366. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3367. org-planning-or-clock-line-re
  3368. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3369. "\\|" org-deadline-string
  3370. "\\|" org-closed-string "\\|" org-clock-string
  3371. "\\)\\>\\)")
  3372. )
  3373. (org-compute-latex-and-specials-regexp)
  3374. (org-set-font-lock-defaults))))
  3375. (defun org-file-contents (file &optional noerror)
  3376. "Return the contents of FILE, as a string."
  3377. (if (or (not file)
  3378. (not (file-readable-p file)))
  3379. (if noerror
  3380. (progn
  3381. (message "Cannot read file %s" file)
  3382. (ding) (sit-for 2)
  3383. "")
  3384. (error "Cannot read file %s" file))
  3385. (with-temp-buffer
  3386. (insert-file-contents file)
  3387. (buffer-string))))
  3388. (defun org-extract-log-state-settings (x)
  3389. "Extract the log state setting from a TODO keyword string.
  3390. This will extract info from a string like \"WAIT(w@/!)\"."
  3391. (let (kw key log1 log2)
  3392. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3393. (setq kw (match-string 1 x)
  3394. key (and (match-end 2) (match-string 2 x))
  3395. log1 (and (match-end 3) (match-string 3 x))
  3396. log2 (and (match-end 4) (match-string 4 x)))
  3397. (and (or log1 log2)
  3398. (list kw
  3399. (and log1 (if (equal log1 "!") 'time 'note))
  3400. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3401. (defun org-remove-keyword-keys (list)
  3402. "Remove a pair of parenthesis at the end of each string in LIST."
  3403. (mapcar (lambda (x)
  3404. (if (string-match "(.*)$" x)
  3405. (substring x 0 (match-beginning 0))
  3406. x))
  3407. list))
  3408. ;; FIXME: this could be done much better, using second characters etc.
  3409. (defun org-assign-fast-keys (alist)
  3410. "Assign fast keys to a keyword-key alist.
  3411. Respect keys that are already there."
  3412. (let (new e k c c1 c2 (char ?a))
  3413. (while (setq e (pop alist))
  3414. (cond
  3415. ((equal e '(:startgroup)) (push e new))
  3416. ((equal e '(:endgroup)) (push e new))
  3417. ((equal e '(:newline)) (push e new))
  3418. (t
  3419. (setq k (car e) c2 nil)
  3420. (if (cdr e)
  3421. (setq c (cdr e))
  3422. ;; automatically assign a character.
  3423. (setq c1 (string-to-char
  3424. (downcase (substring
  3425. k (if (= (string-to-char k) ?@) 1 0)))))
  3426. (if (or (rassoc c1 new) (rassoc c1 alist))
  3427. (while (or (rassoc char new) (rassoc char alist))
  3428. (setq char (1+ char)))
  3429. (setq c2 c1))
  3430. (setq c (or c2 char)))
  3431. (push (cons k c) new))))
  3432. (nreverse new)))
  3433. ;;; Some variables used in various places
  3434. (defvar org-window-configuration nil
  3435. "Used in various places to store a window configuration.")
  3436. (defvar org-selected-window nil
  3437. "Used in various places to store a window configuration.")
  3438. (defvar org-finish-function nil
  3439. "Function to be called when `C-c C-c' is used.
  3440. This is for getting out of special buffers like remember.")
  3441. ;; FIXME: Occasionally check by commenting these, to make sure
  3442. ;; no other functions uses these, forgetting to let-bind them.
  3443. (defvar entry)
  3444. (defvar last-state)
  3445. (defvar date)
  3446. ;; Defined somewhere in this file, but used before definition.
  3447. (defvar org-html-entities)
  3448. (defvar org-struct-menu)
  3449. (defvar org-org-menu)
  3450. (defvar org-tbl-menu)
  3451. (defvar org-agenda-keymap)
  3452. ;;;; Define the Org-mode
  3453. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3454. (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."))
  3455. ;; We use a before-change function to check if a table might need
  3456. ;; an update.
  3457. (defvar org-table-may-need-update t
  3458. "Indicates that a table might need an update.
  3459. This variable is set by `org-before-change-function'.
  3460. `org-table-align' sets it back to nil.")
  3461. (defun org-before-change-function (beg end)
  3462. "Every change indicates that a table might need an update."
  3463. (setq org-table-may-need-update t))
  3464. (defvar org-mode-map)
  3465. (defvar org-mode-hook nil
  3466. "Mode hook for Org-mode, run after the mode was turned on.")
  3467. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3468. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3469. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3470. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3471. (defvar org-table-buffer-is-an nil)
  3472. (defconst org-outline-regexp "\\*+ ")
  3473. ;;;###autoload
  3474. (define-derived-mode org-mode outline-mode "Org"
  3475. "Outline-based notes management and organizer, alias
  3476. \"Carsten's outline-mode for keeping track of everything.\"
  3477. Org-mode develops organizational tasks around a NOTES file which
  3478. contains information about projects as plain text. Org-mode is
  3479. implemented on top of outline-mode, which is ideal to keep the content
  3480. of large files well structured. It supports ToDo items, deadlines and
  3481. time stamps, which magically appear in the diary listing of the Emacs
  3482. calendar. Tables are easily created with a built-in table editor.
  3483. Plain text URL-like links connect to websites, emails (VM), Usenet
  3484. messages (Gnus), BBDB entries, and any files related to the project.
  3485. For printing and sharing of notes, an Org-mode file (or a part of it)
  3486. can be exported as a structured ASCII or HTML file.
  3487. The following commands are available:
  3488. \\{org-mode-map}"
  3489. ;; Get rid of Outline menus, they are not needed
  3490. ;; Need to do this here because define-derived-mode sets up
  3491. ;; the keymap so late. Still, it is a waste to call this each time
  3492. ;; we switch another buffer into org-mode.
  3493. (if (featurep 'xemacs)
  3494. (when (boundp 'outline-mode-menu-heading)
  3495. ;; Assume this is Greg's port, it used easymenu
  3496. (easy-menu-remove outline-mode-menu-heading)
  3497. (easy-menu-remove outline-mode-menu-show)
  3498. (easy-menu-remove outline-mode-menu-hide))
  3499. (define-key org-mode-map [menu-bar headings] 'undefined)
  3500. (define-key org-mode-map [menu-bar hide] 'undefined)
  3501. (define-key org-mode-map [menu-bar show] 'undefined))
  3502. (org-load-modules-maybe)
  3503. (easy-menu-add org-org-menu)
  3504. (easy-menu-add org-tbl-menu)
  3505. (org-install-agenda-files-menu)
  3506. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3507. (org-add-to-invisibility-spec '(org-cwidth))
  3508. (org-add-to-invisibility-spec '(org-hide-block . t))
  3509. (when (featurep 'xemacs)
  3510. (org-set-local 'line-move-ignore-invisible t))
  3511. (org-set-local 'outline-regexp org-outline-regexp)
  3512. (org-set-local 'outline-level 'org-outline-level)
  3513. (when (and org-ellipsis
  3514. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3515. (fboundp 'make-glyph-code))
  3516. (unless org-display-table
  3517. (setq org-display-table (make-display-table)))
  3518. (set-display-table-slot
  3519. org-display-table 4
  3520. (vconcat (mapcar
  3521. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3522. org-ellipsis)))
  3523. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3524. (setq buffer-display-table org-display-table))
  3525. (org-set-regexps-and-options)
  3526. (when (and org-tag-faces (not org-tags-special-faces-re))
  3527. ;; tag faces set outside customize.... force initialization.
  3528. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3529. ;; Calc embedded
  3530. (org-set-local 'calc-embedded-open-mode "# ")
  3531. (modify-syntax-entry ?# "<")
  3532. (modify-syntax-entry ?@ "w")
  3533. (if org-startup-truncated (setq truncate-lines t))
  3534. (org-set-local 'font-lock-unfontify-region-function
  3535. 'org-unfontify-region)
  3536. ;; Activate before-change-function
  3537. (org-set-local 'org-table-may-need-update t)
  3538. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3539. 'local)
  3540. ;; Check for running clock before killing a buffer
  3541. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3542. ;; Paragraphs and auto-filling
  3543. (org-set-autofill-regexps)
  3544. (setq indent-line-function 'org-indent-line-function)
  3545. (org-update-radio-target-regexp)
  3546. ;; Make sure dependence stuff works reliably, even for users who set it
  3547. ;; too late :-(
  3548. (if org-enforce-todo-dependencies
  3549. (add-hook 'org-blocker-hook
  3550. 'org-block-todo-from-children-or-siblings-or-parent)
  3551. (remove-hook 'org-blocker-hook
  3552. 'org-block-todo-from-children-or-siblings-or-parent))
  3553. (if org-enforce-todo-checkbox-dependencies
  3554. (add-hook 'org-blocker-hook
  3555. 'org-block-todo-from-checkboxes)
  3556. (remove-hook 'org-blocker-hook
  3557. 'org-block-todo-from-checkboxes))
  3558. ;; Comment characters
  3559. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3560. (org-set-local 'comment-padding " ")
  3561. ;; Align options lines
  3562. (org-set-local
  3563. 'align-mode-rules-list
  3564. '((org-in-buffer-settings
  3565. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3566. (modes . '(org-mode)))))
  3567. ;; Imenu
  3568. (org-set-local 'imenu-create-index-function
  3569. 'org-imenu-get-tree)
  3570. ;; Make isearch reveal context
  3571. (if (or (featurep 'xemacs)
  3572. (not (boundp 'outline-isearch-open-invisible-function)))
  3573. ;; Emacs 21 and XEmacs make use of the hook
  3574. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3575. ;; Emacs 22 deals with this through a special variable
  3576. (org-set-local 'outline-isearch-open-invisible-function
  3577. (lambda (&rest ignore) (org-show-context 'isearch))))
  3578. ;; If empty file that did not turn on org-mode automatically, make it to.
  3579. (if (and org-insert-mode-line-in-empty-file
  3580. (interactive-p)
  3581. (= (point-min) (point-max)))
  3582. (insert "# -*- mode: org -*-\n\n"))
  3583. (unless org-inhibit-startup
  3584. (when org-startup-align-all-tables
  3585. (let ((bmp (buffer-modified-p)))
  3586. (org-table-map-tables 'org-table-align)
  3587. (set-buffer-modified-p bmp)))
  3588. (when org-startup-indented
  3589. (require 'org-indent)
  3590. (org-indent-mode 1))
  3591. (org-set-startup-visibility)))
  3592. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3593. (defun org-current-time ()
  3594. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3595. (if (> (car org-time-stamp-rounding-minutes) 1)
  3596. (let ((r (car org-time-stamp-rounding-minutes))
  3597. (time (decode-time)))
  3598. (apply 'encode-time
  3599. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3600. (nthcdr 2 time))))
  3601. (current-time)))
  3602. ;;;; Font-Lock stuff, including the activators
  3603. (defvar org-mouse-map (make-sparse-keymap))
  3604. (org-defkey org-mouse-map
  3605. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3606. (org-defkey org-mouse-map
  3607. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3608. (when org-mouse-1-follows-link
  3609. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3610. (when org-tab-follows-link
  3611. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3612. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3613. (require 'font-lock)
  3614. (defconst org-non-link-chars "]\t\n\r<>")
  3615. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3616. "shell" "elisp"))
  3617. (defvar org-link-types-re nil
  3618. "Matches a link that has a url-like prefix like \"http:\"")
  3619. (defvar org-link-re-with-space nil
  3620. "Matches a link with spaces, optional angular brackets around it.")
  3621. (defvar org-link-re-with-space2 nil
  3622. "Matches a link with spaces, optional angular brackets around it.")
  3623. (defvar org-link-re-with-space3 nil
  3624. "Matches a link with spaces, only for internal part in bracket links.")
  3625. (defvar org-angle-link-re nil
  3626. "Matches link with angular brackets, spaces are allowed.")
  3627. (defvar org-plain-link-re nil
  3628. "Matches plain link, without spaces.")
  3629. (defvar org-bracket-link-regexp nil
  3630. "Matches a link in double brackets.")
  3631. (defvar org-bracket-link-analytic-regexp nil
  3632. "Regular expression used to analyze links.
  3633. Here is what the match groups contain after a match:
  3634. 1: http:
  3635. 2: http
  3636. 3: path
  3637. 4: [desc]
  3638. 5: desc")
  3639. (defvar org-bracket-link-analytic-regexp++ nil
  3640. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3641. (defvar org-any-link-re nil
  3642. "Regular expression matching any link.")
  3643. (defun org-make-link-regexps ()
  3644. "Update the link regular expressions.
  3645. This should be called after the variable `org-link-types' has changed."
  3646. (setq org-link-types-re
  3647. (concat
  3648. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3649. org-link-re-with-space
  3650. (concat
  3651. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3652. "\\([^" org-non-link-chars " ]"
  3653. "[^" org-non-link-chars "]*"
  3654. "[^" org-non-link-chars " ]\\)>?")
  3655. org-link-re-with-space2
  3656. (concat
  3657. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3658. "\\([^" org-non-link-chars " ]"
  3659. "[^\t\n\r]*"
  3660. "[^" org-non-link-chars " ]\\)>?")
  3661. org-link-re-with-space3
  3662. (concat
  3663. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3664. "\\([^" org-non-link-chars " ]"
  3665. "[^\t\n\r]*\\)")
  3666. org-angle-link-re
  3667. (concat
  3668. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3669. "\\([^" org-non-link-chars " ]"
  3670. "[^" org-non-link-chars "]*"
  3671. "\\)>")
  3672. org-plain-link-re
  3673. (concat
  3674. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3675. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3676. org-bracket-link-regexp
  3677. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3678. org-bracket-link-analytic-regexp
  3679. (concat
  3680. "\\[\\["
  3681. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3682. "\\([^]]+\\)"
  3683. "\\]"
  3684. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3685. "\\]")
  3686. org-bracket-link-analytic-regexp++
  3687. (concat
  3688. "\\[\\["
  3689. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3690. "\\([^]]+\\)"
  3691. "\\]"
  3692. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3693. "\\]")
  3694. org-any-link-re
  3695. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3696. org-angle-link-re "\\)\\|\\("
  3697. org-plain-link-re "\\)")))
  3698. (org-make-link-regexps)
  3699. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3700. "Regular expression for fast time stamp matching.")
  3701. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3702. "Regular expression for fast time stamp matching.")
  3703. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3704. "Regular expression matching time strings for analysis.
  3705. This one does not require the space after the date, so it can be used
  3706. on a string that terminates immediately after the date.")
  3707. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3708. "Regular expression matching time strings for analysis.")
  3709. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3710. "Regular expression matching time stamps, with groups.")
  3711. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3712. "Regular expression matching time stamps (also [..]), with groups.")
  3713. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3714. "Regular expression matching a time stamp range.")
  3715. (defconst org-tr-regexp-both
  3716. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3717. "Regular expression matching a time stamp range.")
  3718. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3719. org-ts-regexp "\\)?")
  3720. "Regular expression matching a time stamp or time stamp range.")
  3721. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3722. org-ts-regexp-both "\\)?")
  3723. "Regular expression matching a time stamp or time stamp range.
  3724. The time stamps may be either active or inactive.")
  3725. (defvar org-emph-face nil)
  3726. (defun org-do-emphasis-faces (limit)
  3727. "Run through the buffer and add overlays to links."
  3728. (let (rtn a)
  3729. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3730. (if (not (= (char-after (match-beginning 3))
  3731. (char-after (match-beginning 4))))
  3732. (progn
  3733. (setq rtn t)
  3734. (setq a (assoc (match-string 3) org-emphasis-alist))
  3735. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3736. 'face
  3737. (nth 1 a))
  3738. (and (nth 4 a)
  3739. (org-remove-flyspell-overlays-in
  3740. (match-beginning 0) (match-end 0)))
  3741. (add-text-properties (match-beginning 2) (match-end 2)
  3742. '(font-lock-multiline t))
  3743. (when org-hide-emphasis-markers
  3744. (add-text-properties (match-end 4) (match-beginning 5)
  3745. '(invisible org-link))
  3746. (add-text-properties (match-beginning 3) (match-end 3)
  3747. '(invisible org-link)))))
  3748. (backward-char 1))
  3749. rtn))
  3750. (defun org-emphasize (&optional char)
  3751. "Insert or change an emphasis, i.e. a font like bold or italic.
  3752. If there is an active region, change that region to a new emphasis.
  3753. If there is no region, just insert the marker characters and position
  3754. the cursor between them.
  3755. CHAR should be either the marker character, or the first character of the
  3756. HTML tag associated with that emphasis. If CHAR is a space, the means
  3757. to remove the emphasis of the selected region.
  3758. If char is not given (for example in an interactive call) it
  3759. will be prompted for."
  3760. (interactive)
  3761. (let ((eal org-emphasis-alist) e det
  3762. (erc org-emphasis-regexp-components)
  3763. (prompt "")
  3764. (string "") beg end move tag c s)
  3765. (if (org-region-active-p)
  3766. (setq beg (region-beginning) end (region-end)
  3767. string (buffer-substring beg end))
  3768. (setq move t))
  3769. (while (setq e (pop eal))
  3770. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3771. c (aref tag 0))
  3772. (push (cons c (string-to-char (car e))) det)
  3773. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3774. (substring tag 1)))))
  3775. (setq det (nreverse det))
  3776. (unless char
  3777. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3778. (setq char (read-char-exclusive)))
  3779. (setq char (or (cdr (assoc char det)) char))
  3780. (if (equal char ?\ )
  3781. (setq s "" move nil)
  3782. (unless (assoc (char-to-string char) org-emphasis-alist)
  3783. (error "No such emphasis marker: \"%c\"" char))
  3784. (setq s (char-to-string char)))
  3785. (while (and (> (length string) 1)
  3786. (equal (substring string 0 1) (substring string -1))
  3787. (assoc (substring string 0 1) org-emphasis-alist))
  3788. (setq string (substring string 1 -1)))
  3789. (setq string (concat s string s))
  3790. (if beg (delete-region beg end))
  3791. (unless (or (bolp)
  3792. (string-match (concat "[" (nth 0 erc) "\n]")
  3793. (char-to-string (char-before (point)))))
  3794. (insert " "))
  3795. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3796. (char-to-string (char-after (point))))
  3797. (insert " ") (backward-char 1))
  3798. (insert string)
  3799. (and move (backward-char 1))))
  3800. (defconst org-nonsticky-props
  3801. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3802. (defsubst org-rear-nonsticky-at (pos)
  3803. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3804. (defun org-activate-plain-links (limit)
  3805. "Run through the buffer and add overlays to links."
  3806. (catch 'exit
  3807. (let (f)
  3808. (if (re-search-forward org-plain-link-re limit t)
  3809. (progn
  3810. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3811. (setq f (get-text-property (match-beginning 0) 'face))
  3812. (if (or (eq f 'org-tag)
  3813. (and (listp f) (memq 'org-tag f)))
  3814. nil
  3815. (add-text-properties (match-beginning 0) (match-end 0)
  3816. (list 'mouse-face 'highlight
  3817. 'keymap org-mouse-map))
  3818. (org-rear-nonsticky-at (match-end 0)))
  3819. t)))))
  3820. (defun org-activate-code (limit)
  3821. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3822. (progn
  3823. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3824. (remove-text-properties (match-beginning 0) (match-end 0)
  3825. '(display t invisible t intangible t))
  3826. t)))
  3827. (defun org-fontify-meta-lines-and-blocks (limit)
  3828. "Fontify #+ lines and blocks, in the correct ways."
  3829. (let ((case-fold-search t))
  3830. (if (re-search-forward
  3831. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3832. limit t)
  3833. (let ((beg (match-beginning 0))
  3834. (beg1 (line-beginning-position 2))
  3835. (dc1 (downcase (match-string 2)))
  3836. (dc3 (downcase (match-string 3)))
  3837. end end1 quoting)
  3838. (cond
  3839. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3840. ;; a single line of backend-specific content
  3841. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3842. (remove-text-properties (match-beginning 0) (match-end 0)
  3843. '(display t invisible t intangible t))
  3844. (add-text-properties (match-beginning 1) (match-end 3)
  3845. '(font-lock-fontified t face org-meta-line))
  3846. (add-text-properties (match-beginning 6) (match-end 6)
  3847. '(font-lock-fontified t face org-block))
  3848. t)
  3849. ((and (match-end 4) (equal dc3 "begin"))
  3850. ;; Truely a block
  3851. (setq quoting (member (downcase (match-string 5))
  3852. org-protecting-blocks))
  3853. (when (re-search-forward
  3854. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3855. nil t) ;; on purpose, we look further than LIMIT
  3856. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3857. (when quoting
  3858. (remove-text-properties beg end
  3859. '(display t invisible t intangible t)))
  3860. (add-text-properties
  3861. beg end
  3862. '(font-lock-fontified t font-lock-multiline t))
  3863. (add-text-properties beg beg1 '(face org-meta-line))
  3864. (add-text-properties end1 end '(face org-meta-line))
  3865. (when quoting
  3866. (add-text-properties beg1 end1 '(face org-block)))
  3867. t))
  3868. ((not (member (char-after beg) '(?\ ?\t)))
  3869. ;; just any other in-buffer setting, but not indented
  3870. (add-text-properties
  3871. beg (match-end 0)
  3872. '(font-lock-fontified t face org-meta-line))
  3873. t)
  3874. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  3875. "orgtbl:" "tblfm:" "tblname:"))
  3876. (and (match-end 4) (equal dc3 "attr")))
  3877. (add-text-properties
  3878. beg (match-end 0)
  3879. '(font-lock-fontified t face org-meta-line))
  3880. t)
  3881. (t nil))))))
  3882. (defun org-activate-angle-links (limit)
  3883. "Run through the buffer and add overlays to links."
  3884. (if (re-search-forward org-angle-link-re limit t)
  3885. (progn
  3886. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3887. (add-text-properties (match-beginning 0) (match-end 0)
  3888. (list 'mouse-face 'highlight
  3889. 'keymap org-mouse-map))
  3890. (org-rear-nonsticky-at (match-end 0))
  3891. t)))
  3892. (defun org-activate-footnote-links (limit)
  3893. "Run through the buffer and add overlays to links."
  3894. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3895. limit t)
  3896. (progn
  3897. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3898. (add-text-properties (match-beginning 2) (match-end 2)
  3899. (list 'mouse-face 'highlight
  3900. 'keymap org-mouse-map
  3901. 'help-echo
  3902. (if (= (point-at-bol) (match-beginning 2))
  3903. "Footnote definition"
  3904. "Footnote reference")
  3905. ))
  3906. (org-rear-nonsticky-at (match-end 2))
  3907. t)))
  3908. (defun org-activate-bracket-links (limit)
  3909. "Run through the buffer and add overlays to bracketed links."
  3910. (if (re-search-forward org-bracket-link-regexp limit t)
  3911. (let* ((help (concat "LINK: "
  3912. (org-match-string-no-properties 1)))
  3913. ;; FIXME: above we should remove the escapes.
  3914. ;; but that requires another match, protecting match data,
  3915. ;; a lot of overhead for font-lock.
  3916. (ip (org-maybe-intangible
  3917. (list 'invisible 'org-link
  3918. 'keymap org-mouse-map 'mouse-face 'highlight
  3919. 'font-lock-multiline t 'help-echo help)))
  3920. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3921. 'font-lock-multiline t 'help-echo help)))
  3922. ;; We need to remove the invisible property here. Table narrowing
  3923. ;; may have made some of this invisible.
  3924. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3925. (remove-text-properties (match-beginning 0) (match-end 0)
  3926. '(invisible nil))
  3927. (if (match-end 3)
  3928. (progn
  3929. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3930. (org-rear-nonsticky-at (match-beginning 3))
  3931. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3932. (org-rear-nonsticky-at (match-end 3))
  3933. (add-text-properties (match-end 3) (match-end 0) ip)
  3934. (org-rear-nonsticky-at (match-end 0)))
  3935. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3936. (org-rear-nonsticky-at (match-beginning 1))
  3937. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3938. (org-rear-nonsticky-at (match-end 1))
  3939. (add-text-properties (match-end 1) (match-end 0) ip)
  3940. (org-rear-nonsticky-at (match-end 0)))
  3941. t)))
  3942. (defun org-activate-dates (limit)
  3943. "Run through the buffer and add overlays to dates."
  3944. (if (re-search-forward org-tsr-regexp-both limit t)
  3945. (progn
  3946. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3947. (add-text-properties (match-beginning 0) (match-end 0)
  3948. (list 'mouse-face 'highlight
  3949. 'keymap org-mouse-map))
  3950. (org-rear-nonsticky-at (match-end 0))
  3951. (when org-display-custom-times
  3952. (if (match-end 3)
  3953. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3954. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3955. t)))
  3956. (defvar org-target-link-regexp nil
  3957. "Regular expression matching radio targets in plain text.")
  3958. (make-variable-buffer-local 'org-target-link-regexp)
  3959. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3960. "Regular expression matching a link target.")
  3961. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3962. "Regular expression matching a radio target.")
  3963. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3964. "Regular expression matching any target.")
  3965. (defun org-activate-target-links (limit)
  3966. "Run through the buffer and add overlays to target matches."
  3967. (when org-target-link-regexp
  3968. (let ((case-fold-search t))
  3969. (if (re-search-forward org-target-link-regexp limit t)
  3970. (progn
  3971. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3972. (add-text-properties (match-beginning 0) (match-end 0)
  3973. (list 'mouse-face 'highlight
  3974. 'keymap org-mouse-map
  3975. 'help-echo "Radio target link"
  3976. 'org-linked-text t))
  3977. (org-rear-nonsticky-at (match-end 0))
  3978. t)))))
  3979. (defun org-update-radio-target-regexp ()
  3980. "Find all radio targets in this file and update the regular expression."
  3981. (interactive)
  3982. (when (memq 'radio org-activate-links)
  3983. (setq org-target-link-regexp
  3984. (org-make-target-link-regexp (org-all-targets 'radio)))
  3985. (org-restart-font-lock)))
  3986. (defun org-hide-wide-columns (limit)
  3987. (let (s e)
  3988. (setq s (text-property-any (point) (or limit (point-max))
  3989. 'org-cwidth t))
  3990. (when s
  3991. (setq e (next-single-property-change s 'org-cwidth))
  3992. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3993. (goto-char e)
  3994. t)))
  3995. (defvar org-latex-and-specials-regexp nil
  3996. "Regular expression for highlighting export special stuff.")
  3997. (defvar org-match-substring-regexp)
  3998. (defvar org-match-substring-with-braces-regexp)
  3999. ;; This should be with the exporter code, but we also use if for font-locking
  4000. (defconst org-export-html-special-string-regexps
  4001. '(("\\\\-" . "&shy;")
  4002. ("---\\([^-]\\)" . "&mdash;\\1")
  4003. ("--\\([^-]\\)" . "&ndash;\\1")
  4004. ("\\.\\.\\." . "&hellip;"))
  4005. "Regular expressions for special string conversion.")
  4006. (defun org-compute-latex-and-specials-regexp ()
  4007. "Compute regular expression for stuff treated specially by exporters."
  4008. (if (not org-highlight-latex-fragments-and-specials)
  4009. (org-set-local 'org-latex-and-specials-regexp nil)
  4010. (require 'org-exp)
  4011. (let*
  4012. ((matchers (plist-get org-format-latex-options :matchers))
  4013. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4014. org-latex-regexps)))
  4015. (options (org-combine-plists (org-default-export-plist)
  4016. (org-infile-export-plist)))
  4017. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4018. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4019. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4020. (org-export-html-expand (plist-get options :expand-quoted-html))
  4021. (org-export-with-special-strings (plist-get options :special-strings))
  4022. (re-sub
  4023. (cond
  4024. ((equal org-export-with-sub-superscripts '{})
  4025. (list org-match-substring-with-braces-regexp))
  4026. (org-export-with-sub-superscripts
  4027. (list org-match-substring-regexp))
  4028. (t nil)))
  4029. (re-latex
  4030. (if org-export-with-LaTeX-fragments
  4031. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4032. (re-macros
  4033. (if org-export-with-TeX-macros
  4034. (list (concat "\\\\"
  4035. (regexp-opt
  4036. (append (mapcar 'car org-html-entities)
  4037. (if (boundp 'org-latex-entities)
  4038. (mapcar (lambda (x)
  4039. (or (car-safe x) x))
  4040. org-latex-entities)
  4041. nil))
  4042. 'words))) ; FIXME
  4043. ))
  4044. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4045. (re-special (if org-export-with-special-strings
  4046. (mapcar (lambda (x) (car x))
  4047. org-export-html-special-string-regexps)))
  4048. (re-rest
  4049. (delq nil
  4050. (list
  4051. (if org-export-html-expand "@<[^>\n]+>")
  4052. ))))
  4053. (org-set-local
  4054. 'org-latex-and-specials-regexp
  4055. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4056. re-rest) "\\|")))))
  4057. (defun org-do-latex-and-special-faces (limit)
  4058. "Run through the buffer and add overlays to links."
  4059. (when org-latex-and-specials-regexp
  4060. (let (rtn d)
  4061. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4062. limit t))
  4063. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4064. 'face))
  4065. '(org-code org-verbatim underline)))
  4066. (progn
  4067. (setq rtn t
  4068. d (cond ((member (char-after (1+ (match-beginning 0)))
  4069. '(?_ ?^)) 1)
  4070. (t 0)))
  4071. (font-lock-prepend-text-property
  4072. (+ d (match-beginning 0)) (match-end 0)
  4073. 'face 'org-latex-and-export-specials)
  4074. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4075. '(font-lock-multiline t)))))
  4076. rtn)))
  4077. (defun org-restart-font-lock ()
  4078. "Restart font-lock-mode, to force refontification."
  4079. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4080. (font-lock-mode -1)
  4081. (font-lock-mode 1)))
  4082. (defun org-all-targets (&optional radio)
  4083. "Return a list of all targets in this file.
  4084. With optional argument RADIO, only find radio targets."
  4085. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4086. rtn)
  4087. (save-excursion
  4088. (goto-char (point-min))
  4089. (while (re-search-forward re nil t)
  4090. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4091. rtn)))
  4092. (defun org-make-target-link-regexp (targets)
  4093. "Make regular expression matching all strings in TARGETS.
  4094. The regular expression finds the targets also if there is a line break
  4095. between words."
  4096. (and targets
  4097. (concat
  4098. "\\<\\("
  4099. (mapconcat
  4100. (lambda (x)
  4101. (while (string-match " +" x)
  4102. (setq x (replace-match "\\s-+" t t x)))
  4103. x)
  4104. targets
  4105. "\\|")
  4106. "\\)\\>")))
  4107. (defun org-activate-tags (limit)
  4108. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4109. (progn
  4110. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4111. (add-text-properties (match-beginning 1) (match-end 1)
  4112. (list 'mouse-face 'highlight
  4113. 'keymap org-mouse-map))
  4114. (org-rear-nonsticky-at (match-end 1))
  4115. t)))
  4116. (defun org-outline-level ()
  4117. (save-excursion
  4118. (looking-at outline-regexp)
  4119. (if (match-beginning 1)
  4120. (+ (org-get-string-indentation (match-string 1)) 1000)
  4121. (1- (- (match-end 0) (match-beginning 0))))))
  4122. (defvar org-font-lock-keywords nil)
  4123. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4124. "Regular expression matching a property line.")
  4125. (defvar org-font-lock-hook nil
  4126. "Functions to be called for special font lock stuff.")
  4127. (defun org-font-lock-hook (limit)
  4128. (run-hook-with-args 'org-font-lock-hook limit))
  4129. (defun org-set-font-lock-defaults ()
  4130. (let* ((em org-fontify-emphasized-text)
  4131. (lk org-activate-links)
  4132. (org-font-lock-extra-keywords
  4133. (list
  4134. ;; Call the hook
  4135. '(org-font-lock-hook)
  4136. ;; Headlines
  4137. `(,(if org-fontify-whole-heading-line
  4138. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4139. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4140. (1 (org-get-level-face 1))
  4141. (2 (org-get-level-face 2))
  4142. (3 (org-get-level-face 3)))
  4143. ;; Table lines
  4144. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4145. (1 'org-table t))
  4146. ;; Table internals
  4147. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4148. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4149. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4150. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4151. ;; Drawers
  4152. (list org-drawer-regexp '(0 'org-special-keyword t))
  4153. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4154. ;; Properties
  4155. (list org-property-re
  4156. '(1 'org-special-keyword t)
  4157. '(3 'org-property-value t))
  4158. ;; Links
  4159. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4160. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4161. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4162. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4163. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4164. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4165. (if (memq 'footnote lk) '(org-activate-footnote-links
  4166. (2 'org-footnote t)))
  4167. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4168. '(org-hide-wide-columns (0 nil append))
  4169. ;; TODO lines
  4170. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4171. '(1 (org-get-todo-face 1) t))
  4172. ;; DONE
  4173. (if org-fontify-done-headline
  4174. (list (concat "^[*]+ +\\<\\("
  4175. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4176. "\\)\\(.*\\)")
  4177. '(2 'org-headline-done t))
  4178. nil)
  4179. ;; Priorities
  4180. '(org-font-lock-add-priority-faces)
  4181. ;; Tags
  4182. '(org-font-lock-add-tag-faces)
  4183. ;; Special keywords
  4184. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4185. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4186. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4187. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4188. ;; Emphasis
  4189. (if em
  4190. (if (featurep 'xemacs)
  4191. '(org-do-emphasis-faces (0 nil append))
  4192. '(org-do-emphasis-faces)))
  4193. ;; Checkboxes
  4194. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4195. 2 'org-checkbox prepend)
  4196. (if org-provide-checkbox-statistics
  4197. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4198. (0 (org-get-checkbox-statistics-face) t)))
  4199. ;; Description list items
  4200. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4201. 2 'bold prepend)
  4202. ;; ARCHIVEd headings
  4203. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4204. '(1 'org-archived prepend))
  4205. ;; Specials
  4206. '(org-do-latex-and-special-faces)
  4207. ;; Code
  4208. '(org-activate-code (1 'org-code t))
  4209. ;; COMMENT
  4210. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4211. "\\|" org-quote-string "\\)\\>")
  4212. '(1 'org-special-keyword t))
  4213. '("^#.*" (0 'font-lock-comment-face t))
  4214. ;; Blocks and meta lines
  4215. '(org-fontify-meta-lines-and-blocks)
  4216. )))
  4217. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4218. ;; Now set the full font-lock-keywords
  4219. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4220. (org-set-local 'font-lock-defaults
  4221. '(org-font-lock-keywords t nil nil backward-paragraph))
  4222. (kill-local-variable 'font-lock-keywords) nil))
  4223. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4224. "Fontify string S like in Org-mode"
  4225. (with-temp-buffer
  4226. (insert s)
  4227. (let ((org-odd-levels-only odd-levels))
  4228. (org-mode)
  4229. (font-lock-fontify-buffer)
  4230. (buffer-string))))
  4231. (defvar org-m nil)
  4232. (defvar org-l nil)
  4233. (defvar org-f nil)
  4234. (defun org-get-level-face (n)
  4235. "Get the right face for match N in font-lock matching of headlines."
  4236. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4237. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4238. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4239. (cond
  4240. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4241. ((eq n 2) org-f)
  4242. (t (if org-level-color-stars-only nil org-f))))
  4243. (defun org-get-todo-face (kwd)
  4244. "Get the right face for a TODO keyword KWD.
  4245. If KWD is a number, get the corresponding match group."
  4246. (if (numberp kwd) (setq kwd (match-string kwd)))
  4247. (or (cdr (assoc kwd org-todo-keyword-faces))
  4248. (and (member kwd org-done-keywords) 'org-done)
  4249. 'org-todo))
  4250. (defun org-font-lock-add-tag-faces (limit)
  4251. "Add the special tag faces."
  4252. (when (and org-tag-faces org-tags-special-faces-re)
  4253. (while (re-search-forward org-tags-special-faces-re limit t)
  4254. (add-text-properties (match-beginning 1) (match-end 1)
  4255. (list 'face (org-get-tag-face 1)
  4256. 'font-lock-fontified t))
  4257. (backward-char 1))))
  4258. (defun org-font-lock-add-priority-faces (limit)
  4259. "Add the special priority faces."
  4260. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4261. (add-text-properties
  4262. (match-beginning 0) (match-end 0)
  4263. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4264. org-priority-faces))
  4265. 'org-special-keyword)
  4266. 'font-lock-fontified t))))
  4267. (defun org-get-tag-face (kwd)
  4268. "Get the right face for a TODO keyword KWD.
  4269. If KWD is a number, get the corresponding match group."
  4270. (if (numberp kwd) (setq kwd (match-string kwd)))
  4271. (or (cdr (assoc kwd org-tag-faces))
  4272. 'org-tag))
  4273. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4274. "Remove fontification and activation overlays from links."
  4275. (font-lock-default-unfontify-region beg end)
  4276. (let* ((buffer-undo-list t)
  4277. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4278. (inhibit-modification-hooks t)
  4279. deactivate-mark buffer-file-name buffer-file-truename)
  4280. (remove-text-properties beg end
  4281. '(mouse-face t keymap t org-linked-text t
  4282. invisible t intangible t
  4283. line-prefix t wrap-prefix t
  4284. org-no-flyspell t))))
  4285. ;;;; Visibility cycling, including org-goto and indirect buffer
  4286. ;;; Cycling
  4287. (defvar org-cycle-global-status nil)
  4288. (make-variable-buffer-local 'org-cycle-global-status)
  4289. (defvar org-cycle-subtree-status nil)
  4290. (make-variable-buffer-local 'org-cycle-subtree-status)
  4291. ;;;###autoload
  4292. (defvar org-inlinetask-min-level)
  4293. (defun org-cycle (&optional arg)
  4294. "TAB-action and visibility cycling for Org-mode.
  4295. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4296. is outine visibility cycling, but it also invokes other actions
  4297. in special contexts.
  4298. - When this function is called with a prefix argument, rotate the entire
  4299. buffer through 3 states (global cycling)
  4300. 1. OVERVIEW: Show only top-level headlines.
  4301. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4302. 3. SHOW ALL: Show everything.
  4303. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4304. determined by the variable `org-startup-folded', and by any VISIBILITY
  4305. properties in the buffer.
  4306. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4307. including any drawers.
  4308. - When inside a table, re-align the table and move to the next field.
  4309. - When point is at the beginning of a headline, rotate the subtree started
  4310. by this line through 3 different states (local cycling)
  4311. 1. FOLDED: Only the main headline is shown.
  4312. 2. CHILDREN: The main headline and the direct children are shown.
  4313. From this state, you can move to one of the children
  4314. and zoom in further.
  4315. 3. SUBTREE: Show the entire subtree, including body text.
  4316. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4317. - When there is a numeric prefix, go up to a heading with level ARG, do
  4318. a `show-subtree' and return to the previous cursor position. If ARG
  4319. is negative, go up that many levels.
  4320. - When point is not at the beginning of a headline, execute the global
  4321. binding for TAB, which is re-indenting the line. See the option
  4322. `org-cycle-emulate-tab' for details.
  4323. - Special case: if point is at the beginning of the buffer and there is
  4324. no headline in line 1, this function will act as if called with prefix arg.
  4325. But only if also the variable `org-cycle-global-at-bob' is t."
  4326. (interactive "P")
  4327. (org-load-modules-maybe)
  4328. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4329. (let* ((limit-level
  4330. (or org-cycle-max-level
  4331. (and (boundp 'org-inlinetask-min-level)
  4332. org-inlinetask-min-level
  4333. (1- org-inlinetask-min-level))))
  4334. (nstars (and limit-level
  4335. (if org-odd-levels-only
  4336. (and limit-level (1- (* limit-level 2)))
  4337. limit-level)))
  4338. (outline-regexp
  4339. (cond
  4340. ((not (org-mode-p)) outline-regexp)
  4341. ((or (eq org-cycle-include-plain-lists 'integrate)
  4342. (and org-cycle-include-plain-lists (org-at-item-p)))
  4343. (concat "\\(?:\\*"
  4344. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4345. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4346. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4347. (bob-special (and org-cycle-global-at-bob (bobp)
  4348. (not (looking-at outline-regexp))))
  4349. (org-cycle-hook
  4350. (if bob-special
  4351. (delq 'org-optimize-window-after-visibility-change
  4352. (copy-sequence org-cycle-hook))
  4353. org-cycle-hook))
  4354. (pos (point)))
  4355. (if (or bob-special (equal arg '(4)))
  4356. ;; special case: use global cycling
  4357. (setq arg t))
  4358. (cond
  4359. ((equal arg '(16))
  4360. (org-set-startup-visibility)
  4361. (message "Startup visibility, plus VISIBILITY properties"))
  4362. ((equal arg '(64))
  4363. (show-all)
  4364. (message "Entire buffer visible, including drawers"))
  4365. ((org-at-table-p 'any)
  4366. ;; Enter the table or move to the next field in the table
  4367. (or (org-table-recognize-table.el)
  4368. (progn
  4369. (if arg (org-table-edit-field t)
  4370. (org-table-justify-field-maybe)
  4371. (call-interactively 'org-table-next-field)))))
  4372. ((run-hook-with-args-until-success
  4373. 'org-tab-after-check-for-table-hook))
  4374. ((eq arg t) ;; Global cycling
  4375. (org-cycle-internal-global))
  4376. ((and org-drawers org-drawer-regexp
  4377. (save-excursion
  4378. (beginning-of-line 1)
  4379. (looking-at org-drawer-regexp)))
  4380. ;; Toggle block visibility
  4381. (org-flag-drawer
  4382. (not (get-char-property (match-end 0) 'invisible))))
  4383. ((integerp arg)
  4384. ;; Show-subtree, ARG levels up from here.
  4385. (save-excursion
  4386. (org-back-to-heading)
  4387. (outline-up-heading (if (< arg 0) (- arg)
  4388. (- (funcall outline-level) arg)))
  4389. (org-show-subtree)))
  4390. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4391. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4392. (org-cycle-internal-local))
  4393. ;; TAB emulation and template completion
  4394. (buffer-read-only (org-back-to-heading))
  4395. ((run-hook-with-args-until-success
  4396. 'org-tab-after-check-for-cycling-hook))
  4397. ((org-try-structure-completion))
  4398. ((org-try-cdlatex-tab))
  4399. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4400. (or (not (bolp))
  4401. (not (looking-at outline-regexp))))
  4402. (call-interactively (global-key-binding "\t")))
  4403. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4404. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4405. (or (and (eq org-cycle-emulate-tab 'white)
  4406. (= (match-end 0) (point-at-eol)))
  4407. (and (eq org-cycle-emulate-tab 'whitestart)
  4408. (>= (match-end 0) pos))))
  4409. t
  4410. (eq org-cycle-emulate-tab t))
  4411. (call-interactively (global-key-binding "\t")))
  4412. (t (save-excursion
  4413. (org-back-to-heading)
  4414. (org-cycle)))))))
  4415. (defun org-cycle-internal-global ()
  4416. "Do the global cycling action."
  4417. (cond
  4418. ((and (eq last-command this-command)
  4419. (eq org-cycle-global-status 'overview))
  4420. ;; We just created the overview - now do table of contents
  4421. ;; This can be slow in very large buffers, so indicate action
  4422. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4423. (message "CONTENTS...")
  4424. (org-content)
  4425. (message "CONTENTS...done")
  4426. (setq org-cycle-global-status 'contents)
  4427. (run-hook-with-args 'org-cycle-hook 'contents))
  4428. ((and (eq last-command this-command)
  4429. (eq org-cycle-global-status 'contents))
  4430. ;; We just showed the table of contents - now show everything
  4431. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4432. (show-all)
  4433. (message "SHOW ALL")
  4434. (setq org-cycle-global-status 'all)
  4435. (run-hook-with-args 'org-cycle-hook 'all))
  4436. (t
  4437. ;; Default action: go to overview
  4438. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4439. (org-overview)
  4440. (message "OVERVIEW")
  4441. (setq org-cycle-global-status 'overview)
  4442. (run-hook-with-args 'org-cycle-hook 'overview))))
  4443. (defun org-cycle-internal-local ()
  4444. "Do the local cycling action."
  4445. (org-back-to-heading)
  4446. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4447. ;; First, some boundaries
  4448. (save-excursion
  4449. (org-back-to-heading)
  4450. (setq level (funcall outline-level))
  4451. (save-excursion
  4452. (beginning-of-line 2)
  4453. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4454. ; XEmacs does not have `next-single-char-property-change'
  4455. ; I'm not sure about Emacs 21.
  4456. (while (and (not (eobp)) ;; this is like `next-line'
  4457. (get-char-property (1- (point)) 'invisible))
  4458. (beginning-of-line 2))
  4459. (while (and (not (eobp)) ;; this is like `next-line'
  4460. (get-char-property (1- (point)) 'invisible))
  4461. (goto-char (next-single-char-property-change (point) 'invisible))
  4462. ;;;??? (or (bolp) (beginning-of-line 2))))
  4463. (and (eolp) (beginning-of-line 2))))
  4464. (setq eol (point)))
  4465. (outline-end-of-heading) (setq eoh (point))
  4466. (save-excursion
  4467. (outline-next-heading)
  4468. (setq has-children (and (org-at-heading-p t)
  4469. (> (funcall outline-level) level))))
  4470. (org-end-of-subtree t)
  4471. (unless (eobp)
  4472. (skip-chars-forward " \t\n")
  4473. (beginning-of-line 1) ; in case this is an item
  4474. )
  4475. (setq eos (if (eobp) (point) (1- (point)))))
  4476. ;; Find out what to do next and set `this-command'
  4477. (cond
  4478. ((= eos eoh)
  4479. ;; Nothing is hidden behind this heading
  4480. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4481. (message "EMPTY ENTRY")
  4482. (setq org-cycle-subtree-status nil)
  4483. (save-excursion
  4484. (goto-char eos)
  4485. (outline-next-heading)
  4486. (if (org-invisible-p) (org-flag-heading nil))))
  4487. ((and (or (>= eol eos)
  4488. (not (string-match "\\S-" (buffer-substring eol eos))))
  4489. (or has-children
  4490. (not (setq children-skipped
  4491. org-cycle-skip-children-state-if-no-children))))
  4492. ;; Entire subtree is hidden in one line: children view
  4493. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4494. (org-show-entry)
  4495. (show-children)
  4496. (message "CHILDREN")
  4497. (save-excursion
  4498. (goto-char eos)
  4499. (outline-next-heading)
  4500. (if (org-invisible-p) (org-flag-heading nil)))
  4501. (setq org-cycle-subtree-status 'children)
  4502. (run-hook-with-args 'org-cycle-hook 'children))
  4503. ((or children-skipped
  4504. (and (eq last-command this-command)
  4505. (eq org-cycle-subtree-status 'children)))
  4506. ;; We just showed the children, or no children are there,
  4507. ;; now show everything.
  4508. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4509. (org-show-subtree)
  4510. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4511. (setq org-cycle-subtree-status 'subtree)
  4512. (run-hook-with-args 'org-cycle-hook 'subtree))
  4513. (t
  4514. ;; Default action: hide the subtree.
  4515. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4516. (hide-subtree)
  4517. (message "FOLDED")
  4518. (setq org-cycle-subtree-status 'folded)
  4519. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4520. ;;;###autoload
  4521. (defun org-global-cycle (&optional arg)
  4522. "Cycle the global visibility. For details see `org-cycle'.
  4523. With C-u prefix arg, switch to startup visibility.
  4524. With a numeric prefix, show all headlines up to that level."
  4525. (interactive "P")
  4526. (let ((org-cycle-include-plain-lists
  4527. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4528. (cond
  4529. ((integerp arg)
  4530. (show-all)
  4531. (hide-sublevels arg)
  4532. (setq org-cycle-global-status 'contents))
  4533. ((equal arg '(4))
  4534. (org-set-startup-visibility)
  4535. (message "Startup visibility, plus VISIBILITY properties."))
  4536. (t
  4537. (org-cycle '(4))))))
  4538. (defun org-set-startup-visibility ()
  4539. "Set the visibility required by startup options and properties."
  4540. (cond
  4541. ((eq org-startup-folded t)
  4542. (org-cycle '(4)))
  4543. ((eq org-startup-folded 'content)
  4544. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4545. (org-cycle '(4)) (org-cycle '(4)))))
  4546. (unless (eq org-startup-folded 'showeverything)
  4547. (if org-hide-block-startup (org-hide-block-all))
  4548. (org-set-visibility-according-to-property 'no-cleanup)
  4549. (org-cycle-hide-archived-subtrees 'all)
  4550. (org-cycle-hide-drawers 'all)
  4551. (org-cycle-show-empty-lines 'all)))
  4552. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4553. "Switch subtree visibilities according to :VISIBILITY: property."
  4554. (interactive)
  4555. (let (org-show-entry-below state)
  4556. (save-excursion
  4557. (goto-char (point-min))
  4558. (while (re-search-forward
  4559. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4560. nil t)
  4561. (setq state (match-string 1))
  4562. (save-excursion
  4563. (org-back-to-heading t)
  4564. (hide-subtree)
  4565. (org-reveal)
  4566. (cond
  4567. ((equal state '("fold" "folded"))
  4568. (hide-subtree))
  4569. ((equal state "children")
  4570. (org-show-hidden-entry)
  4571. (show-children))
  4572. ((equal state "content")
  4573. (save-excursion
  4574. (save-restriction
  4575. (org-narrow-to-subtree)
  4576. (org-content))))
  4577. ((member state '("all" "showall"))
  4578. (show-subtree)))))
  4579. (unless no-cleanup
  4580. (org-cycle-hide-archived-subtrees 'all)
  4581. (org-cycle-hide-drawers 'all)
  4582. (org-cycle-show-empty-lines 'all)))))
  4583. (defun org-overview ()
  4584. "Switch to overview mode, showing only top-level headlines.
  4585. Really, this shows all headlines with level equal or greater than the level
  4586. of the first headline in the buffer. This is important, because if the
  4587. first headline is not level one, then (hide-sublevels 1) gives confusing
  4588. results."
  4589. (interactive)
  4590. (let ((level (save-excursion
  4591. (goto-char (point-min))
  4592. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4593. (progn
  4594. (goto-char (match-beginning 0))
  4595. (funcall outline-level))))))
  4596. (and level (hide-sublevels level))))
  4597. (defun org-content (&optional arg)
  4598. "Show all headlines in the buffer, like a table of contents.
  4599. With numerical argument N, show content up to level N."
  4600. (interactive "P")
  4601. (save-excursion
  4602. ;; Visit all headings and show their offspring
  4603. (and (integerp arg) (org-overview))
  4604. (goto-char (point-max))
  4605. (catch 'exit
  4606. (while (and (progn (condition-case nil
  4607. (outline-previous-visible-heading 1)
  4608. (error (goto-char (point-min))))
  4609. t)
  4610. (looking-at outline-regexp))
  4611. (if (integerp arg)
  4612. (show-children (1- arg))
  4613. (show-branches))
  4614. (if (bobp) (throw 'exit nil))))))
  4615. (defun org-optimize-window-after-visibility-change (state)
  4616. "Adjust the window after a change in outline visibility.
  4617. This function is the default value of the hook `org-cycle-hook'."
  4618. (when (get-buffer-window (current-buffer))
  4619. (cond
  4620. ((eq state 'content) nil)
  4621. ((eq state 'all) nil)
  4622. ((eq state 'folded) nil)
  4623. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4624. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4625. ;; FIXME: no longer in use
  4626. (defun org-compact-display-after-subtree-move ()
  4627. "Show a compacter version of the tree of the entry's parent."
  4628. (save-excursion
  4629. (if (org-up-heading-safe)
  4630. (progn
  4631. (hide-subtree)
  4632. (show-entry)
  4633. (show-children)
  4634. (org-cycle-show-empty-lines 'children)
  4635. (org-cycle-hide-drawers 'children))
  4636. (org-overview))))
  4637. (defun org-remove-empty-overlays-at (pos)
  4638. "Remove outline overlays that do not contain non-white stuff."
  4639. (mapc
  4640. (lambda (o)
  4641. (and (eq 'outline (org-overlay-get o 'invisible))
  4642. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4643. (org-overlay-end o))))
  4644. (org-delete-overlay o)))
  4645. (org-overlays-at pos)))
  4646. (defun org-clean-visibility-after-subtree-move ()
  4647. "Fix visibility issues after moving a subtree."
  4648. ;; First, find a reasonable region to look at:
  4649. ;; Start two siblings above, end three below
  4650. (let* ((beg (save-excursion
  4651. (and (org-get-last-sibling)
  4652. (org-get-last-sibling))
  4653. (point)))
  4654. (end (save-excursion
  4655. (and (org-get-next-sibling)
  4656. (org-get-next-sibling)
  4657. (org-get-next-sibling))
  4658. (if (org-at-heading-p)
  4659. (point-at-eol)
  4660. (point))))
  4661. (level (looking-at "\\*+"))
  4662. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4663. (save-excursion
  4664. (save-restriction
  4665. (narrow-to-region beg end)
  4666. (when re
  4667. ;; Properly fold already folded siblings
  4668. (goto-char (point-min))
  4669. (while (re-search-forward re nil t)
  4670. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4671. (hide-entry))))
  4672. (org-cycle-show-empty-lines 'overview)
  4673. (org-cycle-hide-drawers 'overview)))))
  4674. (defun org-cycle-show-empty-lines (state)
  4675. "Show empty lines above all visible headlines.
  4676. The region to be covered depends on STATE when called through
  4677. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4678. entire buffer covered. Note that an empty line is only shown if there
  4679. are at least `org-cycle-separator-lines' empty lines before the headline."
  4680. (when (not (= org-cycle-separator-lines 0))
  4681. (save-excursion
  4682. (let* ((n (abs org-cycle-separator-lines))
  4683. (re (cond
  4684. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4685. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4686. (t (let ((ns (number-to-string (- n 2))))
  4687. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4688. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4689. beg end b e)
  4690. (cond
  4691. ((memq state '(overview contents t))
  4692. (setq beg (point-min) end (point-max)))
  4693. ((memq state '(children folded))
  4694. (setq beg (point) end (progn (org-end-of-subtree t t)
  4695. (beginning-of-line 2)
  4696. (point)))))
  4697. (when beg
  4698. (goto-char beg)
  4699. (while (re-search-forward re end t)
  4700. (unless (get-char-property (match-end 1) 'invisible)
  4701. (setq e (match-end 1))
  4702. (if (< org-cycle-separator-lines 0)
  4703. (setq b (save-excursion
  4704. (goto-char (match-beginning 0))
  4705. (org-back-over-empty-lines)
  4706. (if (save-excursion
  4707. (goto-char (max (point-min) (1- (point))))
  4708. (org-on-heading-p))
  4709. (1- (point))
  4710. (point))))
  4711. (setq b (match-beginning 1)))
  4712. (outline-flag-region b e nil)))))))
  4713. ;; Never hide empty lines at the end of the file.
  4714. (save-excursion
  4715. (goto-char (point-max))
  4716. (outline-previous-heading)
  4717. (outline-end-of-heading)
  4718. (if (and (looking-at "[ \t\n]+")
  4719. (= (match-end 0) (point-max)))
  4720. (outline-flag-region (point) (match-end 0) nil))))
  4721. (defun org-show-empty-lines-in-parent ()
  4722. "Move to the parent and re-show empty lines before visible headlines."
  4723. (save-excursion
  4724. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4725. (org-cycle-show-empty-lines context))))
  4726. (defun org-cycle-hide-drawers (state)
  4727. "Re-hide all drawers after a visibility state change."
  4728. (when (and (org-mode-p)
  4729. (not (memq state '(overview folded contents))))
  4730. (save-excursion
  4731. (let* ((globalp (memq state '(contents all)))
  4732. (beg (if globalp (point-min) (point)))
  4733. (end (if globalp (point-max)
  4734. (if (eq state 'children)
  4735. (save-excursion (outline-next-heading) (point))
  4736. (org-end-of-subtree t)))))
  4737. (goto-char beg)
  4738. (while (re-search-forward org-drawer-regexp end t)
  4739. (org-flag-drawer t))))))
  4740. (defun org-flag-drawer (flag)
  4741. (save-excursion
  4742. (beginning-of-line 1)
  4743. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4744. (let ((b (match-end 0))
  4745. (outline-regexp org-outline-regexp))
  4746. (if (re-search-forward
  4747. "^[ \t]*:END:"
  4748. (save-excursion (outline-next-heading) (point)) t)
  4749. (outline-flag-region b (point-at-eol) flag)
  4750. (error ":END: line missing at position %s" b))))))
  4751. (defun org-subtree-end-visible-p ()
  4752. "Is the end of the current subtree visible?"
  4753. (pos-visible-in-window-p
  4754. (save-excursion (org-end-of-subtree t) (point))))
  4755. (defun org-first-headline-recenter (&optional N)
  4756. "Move cursor to the first headline and recenter the headline.
  4757. Optional argument N means, put the headline into the Nth line of the window."
  4758. (goto-char (point-min))
  4759. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4760. (beginning-of-line)
  4761. (recenter (prefix-numeric-value N))))
  4762. ;;; Folding of blocks
  4763. (defconst org-block-regexp
  4764. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4765. "Regular expression for hiding blocks.")
  4766. (defvar org-hide-block-overlays nil
  4767. "Overays hiding blocks.")
  4768. (make-variable-buffer-local 'org-hide-block-overlays)
  4769. (defun org-block-map (function &optional start end)
  4770. "Call func at the head of all source blocks in the current
  4771. buffer. Optional arguments START and END can be used to limit
  4772. the range."
  4773. (let ((start (or start (point-min)))
  4774. (end (or end (point-max))))
  4775. (save-excursion
  4776. (goto-char start)
  4777. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4778. (save-excursion
  4779. (save-match-data
  4780. (goto-char (match-beginning 0))
  4781. (funcall function)))))))
  4782. (defun org-hide-block-toggle-all ()
  4783. "Toggle the visibility of all blocks in the current buffer."
  4784. (org-block-map #'org-hide-block-toggle))
  4785. (defun org-hide-block-all ()
  4786. "Fold all blocks in the current buffer."
  4787. (interactive)
  4788. (org-show-block-all)
  4789. (org-block-map #'org-hide-block-toggle-maybe))
  4790. (defun org-show-block-all ()
  4791. "Unfold all blocks in the current buffer."
  4792. (mapc 'org-delete-overlay org-hide-block-overlays)
  4793. (setq org-hide-block-overlays nil))
  4794. (defun org-hide-block-toggle-maybe ()
  4795. "Toggle visibility of block at point."
  4796. (interactive)
  4797. (let ((case-fold-search t))
  4798. (if (save-excursion
  4799. (beginning-of-line 1)
  4800. (looking-at org-block-regexp))
  4801. (progn (org-hide-block-toggle)
  4802. t) ;; to signal that we took action
  4803. nil))) ;; to signal that we did not
  4804. (defun org-hide-block-toggle (&optional force)
  4805. "Toggle the visibility of the current block."
  4806. (interactive)
  4807. (save-excursion
  4808. (beginning-of-line)
  4809. (if (re-search-forward org-block-regexp nil t)
  4810. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4811. (end (match-end 0)) ;; end of entire body
  4812. ov)
  4813. (if (memq t (mapcar (lambda (overlay)
  4814. (eq (org-overlay-get overlay 'invisible)
  4815. 'org-hide-block))
  4816. (org-overlays-at start)))
  4817. (if (or (not force) (eq force 'off))
  4818. (mapc (lambda (ov)
  4819. (when (member ov org-hide-block-overlays)
  4820. (setq org-hide-block-overlays
  4821. (delq ov org-hide-block-overlays)))
  4822. (when (eq (org-overlay-get ov 'invisible)
  4823. 'org-hide-block)
  4824. (org-delete-overlay ov)))
  4825. (org-overlays-at start)))
  4826. (setq ov (org-make-overlay start end))
  4827. (org-overlay-put ov 'invisible 'org-hide-block)
  4828. ;; make the block accessible to isearch
  4829. (org-overlay-put
  4830. ov 'isearch-open-invisible
  4831. (lambda (ov)
  4832. (when (member ov org-hide-block-overlays)
  4833. (setq org-hide-block-overlays
  4834. (delq ov org-hide-block-overlays)))
  4835. (when (eq (org-overlay-get ov 'invisible)
  4836. 'org-hide-block)
  4837. (org-delete-overlay ov))))
  4838. (push ov org-hide-block-overlays)))
  4839. (error "Not looking at a source block"))))
  4840. ;; org-tab-after-check-for-cycling-hook
  4841. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4842. ;; Remove overlays when changing major mode
  4843. (add-hook 'org-mode-hook
  4844. (lambda () (org-add-hook 'change-major-mode-hook
  4845. 'org-show-block-all 'append 'local)))
  4846. ;;; Org-goto
  4847. (defvar org-goto-window-configuration nil)
  4848. (defvar org-goto-marker nil)
  4849. (defvar org-goto-map
  4850. (let ((map (make-sparse-keymap)))
  4851. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4852. (while (setq cmd (pop cmds))
  4853. (substitute-key-definition cmd cmd map global-map)))
  4854. (suppress-keymap map)
  4855. (org-defkey map "\C-m" 'org-goto-ret)
  4856. (org-defkey map [(return)] 'org-goto-ret)
  4857. (org-defkey map [(left)] 'org-goto-left)
  4858. (org-defkey map [(right)] 'org-goto-right)
  4859. (org-defkey map [(control ?g)] 'org-goto-quit)
  4860. (org-defkey map "\C-i" 'org-cycle)
  4861. (org-defkey map [(tab)] 'org-cycle)
  4862. (org-defkey map [(down)] 'outline-next-visible-heading)
  4863. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4864. (if org-goto-auto-isearch
  4865. (if (fboundp 'define-key-after)
  4866. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4867. nil)
  4868. (org-defkey map "q" 'org-goto-quit)
  4869. (org-defkey map "n" 'outline-next-visible-heading)
  4870. (org-defkey map "p" 'outline-previous-visible-heading)
  4871. (org-defkey map "f" 'outline-forward-same-level)
  4872. (org-defkey map "b" 'outline-backward-same-level)
  4873. (org-defkey map "u" 'outline-up-heading))
  4874. (org-defkey map "/" 'org-occur)
  4875. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4876. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4877. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4878. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4879. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4880. map))
  4881. (defconst org-goto-help
  4882. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4883. RET=jump to location [Q]uit and return to previous location
  4884. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4885. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4886. ;; FIXME: Docstring doe not mention both interfaces
  4887. (defun org-goto (&optional alternative-interface)
  4888. "Look up a different location in the current file, keeping current visibility.
  4889. When you want look-up or go to a different location in a document, the
  4890. fastest way is often to fold the entire buffer and then dive into the tree.
  4891. This method has the disadvantage, that the previous location will be folded,
  4892. which may not be what you want.
  4893. This command works around this by showing a copy of the current buffer
  4894. in an indirect buffer, in overview mode. You can dive into the tree in
  4895. that copy, use org-occur and incremental search to find a location.
  4896. When pressing RET or `Q', the command returns to the original buffer in
  4897. which the visibility is still unchanged. After RET is will also jump to
  4898. the location selected in the indirect buffer and expose the
  4899. the headline hierarchy above."
  4900. (interactive "P")
  4901. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4902. (org-refile-use-outline-path t)
  4903. (org-refile-target-verify-function nil)
  4904. (interface
  4905. (if (not alternative-interface)
  4906. org-goto-interface
  4907. (if (eq org-goto-interface 'outline)
  4908. 'outline-path-completion
  4909. 'outline)))
  4910. (org-goto-start-pos (point))
  4911. (selected-point
  4912. (if (eq interface 'outline)
  4913. (car (org-get-location (current-buffer) org-goto-help))
  4914. (nth 3 (org-refile-get-location "Goto: ")))))
  4915. (if selected-point
  4916. (progn
  4917. (org-mark-ring-push org-goto-start-pos)
  4918. (goto-char selected-point)
  4919. (if (or (org-invisible-p) (org-invisible-p2))
  4920. (org-show-context 'org-goto)))
  4921. (message "Quit"))))
  4922. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4923. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4924. (defvar org-goto-local-auto-isearch-map) ; defined below
  4925. (defun org-get-location (buf help)
  4926. "Let the user select a location in the Org-mode buffer BUF.
  4927. This function uses a recursive edit. It returns the selected position
  4928. or nil."
  4929. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4930. (isearch-hide-immediately nil)
  4931. (isearch-search-fun-function
  4932. (lambda () 'org-goto-local-search-headings))
  4933. (org-goto-selected-point org-goto-exit-command))
  4934. (save-excursion
  4935. (save-window-excursion
  4936. (delete-other-windows)
  4937. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4938. (switch-to-buffer
  4939. (condition-case nil
  4940. (make-indirect-buffer (current-buffer) "*org-goto*")
  4941. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4942. (with-output-to-temp-buffer "*Help*"
  4943. (princ help))
  4944. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4945. (setq buffer-read-only nil)
  4946. (let ((org-startup-truncated t)
  4947. (org-startup-folded nil)
  4948. (org-startup-align-all-tables nil))
  4949. (org-mode)
  4950. (org-overview))
  4951. (setq buffer-read-only t)
  4952. (if (and (boundp 'org-goto-start-pos)
  4953. (integer-or-marker-p org-goto-start-pos))
  4954. (let ((org-show-hierarchy-above t)
  4955. (org-show-siblings t)
  4956. (org-show-following-heading t))
  4957. (goto-char org-goto-start-pos)
  4958. (and (org-invisible-p) (org-show-context)))
  4959. (goto-char (point-min)))
  4960. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4961. (message "Select location and press RET")
  4962. (use-local-map org-goto-map)
  4963. (recursive-edit)
  4964. ))
  4965. (kill-buffer "*org-goto*")
  4966. (cons org-goto-selected-point org-goto-exit-command)))
  4967. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4968. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4969. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4970. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4971. (defun org-goto-local-search-headings (string bound noerror)
  4972. "Search and make sure that any matches are in headlines."
  4973. (catch 'return
  4974. (while (if isearch-forward
  4975. (search-forward string bound noerror)
  4976. (search-backward string bound noerror))
  4977. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4978. (and (member :headline context)
  4979. (not (member :tags context))))
  4980. (throw 'return (point))))))
  4981. (defun org-goto-local-auto-isearch ()
  4982. "Start isearch."
  4983. (interactive)
  4984. (goto-char (point-min))
  4985. (let ((keys (this-command-keys)))
  4986. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4987. (isearch-mode t)
  4988. (isearch-process-search-char (string-to-char keys)))))
  4989. (defun org-goto-ret (&optional arg)
  4990. "Finish `org-goto' by going to the new location."
  4991. (interactive "P")
  4992. (setq org-goto-selected-point (point)
  4993. org-goto-exit-command 'return)
  4994. (throw 'exit nil))
  4995. (defun org-goto-left ()
  4996. "Finish `org-goto' by going to the new location."
  4997. (interactive)
  4998. (if (org-on-heading-p)
  4999. (progn
  5000. (beginning-of-line 1)
  5001. (setq org-goto-selected-point (point)
  5002. org-goto-exit-command 'left)
  5003. (throw 'exit nil))
  5004. (error "Not on a heading")))
  5005. (defun org-goto-right ()
  5006. "Finish `org-goto' by going to the new location."
  5007. (interactive)
  5008. (if (org-on-heading-p)
  5009. (progn
  5010. (setq org-goto-selected-point (point)
  5011. org-goto-exit-command 'right)
  5012. (throw 'exit nil))
  5013. (error "Not on a heading")))
  5014. (defun org-goto-quit ()
  5015. "Finish `org-goto' without cursor motion."
  5016. (interactive)
  5017. (setq org-goto-selected-point nil)
  5018. (setq org-goto-exit-command 'quit)
  5019. (throw 'exit nil))
  5020. ;;; Indirect buffer display of subtrees
  5021. (defvar org-indirect-dedicated-frame nil
  5022. "This is the frame being used for indirect tree display.")
  5023. (defvar org-last-indirect-buffer nil)
  5024. (defun org-tree-to-indirect-buffer (&optional arg)
  5025. "Create indirect buffer and narrow it to current subtree.
  5026. With numerical prefix ARG, go up to this level and then take that tree.
  5027. If ARG is negative, go up that many levels.
  5028. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5029. indirect buffer previously made with this command, to avoid proliferation of
  5030. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5031. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5032. is kept so that you can work with several indirect buffers at the same time.
  5033. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5034. requests that a new frame be made for the new buffer, so that the dedicated
  5035. frame is not changed."
  5036. (interactive "P")
  5037. (let ((cbuf (current-buffer))
  5038. (cwin (selected-window))
  5039. (pos (point))
  5040. beg end level heading ibuf)
  5041. (save-excursion
  5042. (org-back-to-heading t)
  5043. (when (numberp arg)
  5044. (setq level (org-outline-level))
  5045. (if (< arg 0) (setq arg (+ level arg)))
  5046. (while (> (setq level (org-outline-level)) arg)
  5047. (outline-up-heading 1 t)))
  5048. (setq beg (point)
  5049. heading (org-get-heading))
  5050. (org-end-of-subtree t t) (setq end (point)))
  5051. (if (and (buffer-live-p org-last-indirect-buffer)
  5052. (not (eq org-indirect-buffer-display 'new-frame))
  5053. (not arg))
  5054. (kill-buffer org-last-indirect-buffer))
  5055. (setq ibuf (org-get-indirect-buffer cbuf)
  5056. org-last-indirect-buffer ibuf)
  5057. (cond
  5058. ((or (eq org-indirect-buffer-display 'new-frame)
  5059. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5060. (select-frame (make-frame))
  5061. (delete-other-windows)
  5062. (switch-to-buffer ibuf)
  5063. (org-set-frame-title heading))
  5064. ((eq org-indirect-buffer-display 'dedicated-frame)
  5065. (raise-frame
  5066. (select-frame (or (and org-indirect-dedicated-frame
  5067. (frame-live-p org-indirect-dedicated-frame)
  5068. org-indirect-dedicated-frame)
  5069. (setq org-indirect-dedicated-frame (make-frame)))))
  5070. (delete-other-windows)
  5071. (switch-to-buffer ibuf)
  5072. (org-set-frame-title (concat "Indirect: " heading)))
  5073. ((eq org-indirect-buffer-display 'current-window)
  5074. (switch-to-buffer ibuf))
  5075. ((eq org-indirect-buffer-display 'other-window)
  5076. (pop-to-buffer ibuf))
  5077. (t (error "Invalid value.")))
  5078. (if (featurep 'xemacs)
  5079. (save-excursion (org-mode) (turn-on-font-lock)))
  5080. (narrow-to-region beg end)
  5081. (show-all)
  5082. (goto-char pos)
  5083. (and (window-live-p cwin) (select-window cwin))))
  5084. (defun org-get-indirect-buffer (&optional buffer)
  5085. (setq buffer (or buffer (current-buffer)))
  5086. (let ((n 1) (base (buffer-name buffer)) bname)
  5087. (while (buffer-live-p
  5088. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5089. (setq n (1+ n)))
  5090. (condition-case nil
  5091. (make-indirect-buffer buffer bname 'clone)
  5092. (error (make-indirect-buffer buffer bname)))))
  5093. (defun org-set-frame-title (title)
  5094. "Set the title of the current frame to the string TITLE."
  5095. ;; FIXME: how to name a single frame in XEmacs???
  5096. (unless (featurep 'xemacs)
  5097. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5098. ;;;; Structure editing
  5099. ;;; Inserting headlines
  5100. (defun org-previous-line-empty-p ()
  5101. (save-excursion
  5102. (and (not (bobp))
  5103. (or (beginning-of-line 0) t)
  5104. (save-match-data
  5105. (looking-at "[ \t]*$")))))
  5106. (defun org-insert-heading (&optional force-heading)
  5107. "Insert a new heading or item with same depth at point.
  5108. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5109. If point is at the beginning of a headline, insert a sibling before the
  5110. current headline. If point is not at the beginning, do not split the line,
  5111. but create the new headline after the current line."
  5112. (interactive "P")
  5113. (if (= (buffer-size) 0)
  5114. (insert "\n* ")
  5115. (when (or force-heading (not (org-insert-item)))
  5116. (let* ((empty-line-p nil)
  5117. (head (save-excursion
  5118. (condition-case nil
  5119. (progn
  5120. (org-back-to-heading)
  5121. (setq empty-line-p (org-previous-line-empty-p))
  5122. (match-string 0))
  5123. (error "*"))))
  5124. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5125. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5126. pos hide-previous previous-pos)
  5127. (cond
  5128. ((and (org-on-heading-p) (bolp)
  5129. (or (bobp)
  5130. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5131. ;; insert before the current line
  5132. (open-line (if blank 2 1)))
  5133. ((and (bolp)
  5134. (not org-insert-heading-respect-content)
  5135. (or (bobp)
  5136. (save-excursion
  5137. (backward-char 1) (not (org-invisible-p)))))
  5138. ;; insert right here
  5139. nil)
  5140. (t
  5141. ;; somewhere in the line
  5142. (save-excursion
  5143. (setq previous-pos (point-at-bol))
  5144. (end-of-line)
  5145. (setq hide-previous (org-invisible-p)))
  5146. (and org-insert-heading-respect-content (org-show-subtree))
  5147. (let ((split
  5148. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5149. (save-excursion
  5150. (let ((p (point)))
  5151. (goto-char (point-at-bol))
  5152. (and (looking-at org-complex-heading-regexp)
  5153. (> p (match-beginning 4)))))))
  5154. tags pos)
  5155. (cond
  5156. (org-insert-heading-respect-content
  5157. (org-end-of-subtree nil t)
  5158. (or (bolp) (newline))
  5159. (or (org-previous-line-empty-p)
  5160. (and blank (newline)))
  5161. (open-line 1))
  5162. ((org-on-heading-p)
  5163. (when hide-previous
  5164. (show-children)
  5165. (org-show-entry))
  5166. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5167. (setq tags (and (match-end 2) (match-string 2)))
  5168. (and (match-end 1)
  5169. (delete-region (match-beginning 1) (match-end 1)))
  5170. (setq pos (point-at-bol))
  5171. (or split (end-of-line 1))
  5172. (delete-horizontal-space)
  5173. (newline (if blank 2 1))
  5174. (when tags
  5175. (save-excursion
  5176. (goto-char pos)
  5177. (end-of-line 1)
  5178. (insert " " tags)
  5179. (org-set-tags nil 'align))))
  5180. (t
  5181. (or split (end-of-line 1))
  5182. (newline (if blank 2 1)))))))
  5183. (insert head) (just-one-space)
  5184. (setq pos (point))
  5185. (end-of-line 1)
  5186. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5187. (when (and org-insert-heading-respect-content hide-previous)
  5188. (save-excursion
  5189. (goto-char previous-pos)
  5190. (hide-subtree)))
  5191. (run-hooks 'org-insert-heading-hook)))))
  5192. (defun org-get-heading (&optional no-tags)
  5193. "Return the heading of the current entry, without the stars."
  5194. (save-excursion
  5195. (org-back-to-heading t)
  5196. (if (looking-at
  5197. (if no-tags
  5198. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5199. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5200. (match-string 1) "")))
  5201. (defun org-heading-components ()
  5202. "Return the components of the current heading.
  5203. This is a list with the following elements:
  5204. - the level as an integer
  5205. - the reduced level, different if `org-odd-levels-only' is set.
  5206. - the TODO keyword, or nil
  5207. - the priority character, like ?A, or nil if no priority is given
  5208. - the headline text itself, or the tags string if no headline text
  5209. - the tags string, or nil."
  5210. (save-excursion
  5211. (org-back-to-heading t)
  5212. (if (looking-at org-complex-heading-regexp)
  5213. (list (length (match-string 1))
  5214. (org-reduced-level (length (match-string 1)))
  5215. (org-match-string-no-properties 2)
  5216. (and (match-end 3) (aref (match-string 3) 2))
  5217. (org-match-string-no-properties 4)
  5218. (org-match-string-no-properties 5)))))
  5219. (defun org-get-entry ()
  5220. "Get the entry text, after heading, entire subtree."
  5221. (save-excursion
  5222. (org-back-to-heading t)
  5223. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5224. (defun org-insert-heading-after-current ()
  5225. "Insert a new heading with same level as current, after current subtree."
  5226. (interactive)
  5227. (org-back-to-heading)
  5228. (org-insert-heading)
  5229. (org-move-subtree-down)
  5230. (end-of-line 1))
  5231. (defun org-insert-heading-respect-content ()
  5232. (interactive)
  5233. (let ((org-insert-heading-respect-content t))
  5234. (org-insert-heading t)))
  5235. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5236. (interactive "P")
  5237. (let ((org-insert-heading-respect-content t))
  5238. (org-insert-todo-heading force-state t)))
  5239. (defun org-insert-todo-heading (arg &optional force-heading)
  5240. "Insert a new heading with the same level and TODO state as current heading.
  5241. If the heading has no TODO state, or if the state is DONE, use the first
  5242. state (TODO by default). Also with prefix arg, force first state."
  5243. (interactive "P")
  5244. (when (or force-heading (not (org-insert-item 'checkbox)))
  5245. (org-insert-heading force-heading)
  5246. (save-excursion
  5247. (org-back-to-heading)
  5248. (outline-previous-heading)
  5249. (looking-at org-todo-line-regexp))
  5250. (let*
  5251. ((new-mark-x
  5252. (if (or arg
  5253. (not (match-beginning 2))
  5254. (member (match-string 2) org-done-keywords))
  5255. (car org-todo-keywords-1)
  5256. (match-string 2)))
  5257. (new-mark
  5258. (or
  5259. (run-hook-with-args-until-success
  5260. 'org-todo-get-default-hook new-mark-x nil)
  5261. new-mark-x)))
  5262. (beginning-of-line 1)
  5263. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5264. (if org-treat-insert-todo-heading-as-state-change
  5265. (org-todo new-mark)
  5266. (insert new-mark " "))))
  5267. (when org-provide-todo-statistics
  5268. (org-update-parent-todo-statistics))))
  5269. (defun org-insert-subheading (arg)
  5270. "Insert a new subheading and demote it.
  5271. Works for outline headings and for plain lists alike."
  5272. (interactive "P")
  5273. (org-insert-heading arg)
  5274. (cond
  5275. ((org-on-heading-p) (org-do-demote))
  5276. ((org-at-item-p) (org-indent-item 1))))
  5277. (defun org-insert-todo-subheading (arg)
  5278. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5279. Works for outline headings and for plain lists alike."
  5280. (interactive "P")
  5281. (org-insert-todo-heading arg)
  5282. (cond
  5283. ((org-on-heading-p) (org-do-demote))
  5284. ((org-at-item-p) (org-indent-item 1))))
  5285. ;;; Promotion and Demotion
  5286. (defvar org-after-demote-entry-hook nil
  5287. "Hook run after an entry has been demoted.
  5288. The cursor will be at the beginning of the entry.
  5289. When a subtree is being demoted, the hook will be called for each node.")
  5290. (defvar org-after-promote-entry-hook nil
  5291. "Hook run after an entry has been promoted.
  5292. The cursor will be at the beginning of the entry.
  5293. When a subtree is being promoted, the hook will be called for each node.")
  5294. (defun org-promote-subtree ()
  5295. "Promote the entire subtree.
  5296. See also `org-promote'."
  5297. (interactive)
  5298. (save-excursion
  5299. (org-map-tree 'org-promote))
  5300. (org-fix-position-after-promote))
  5301. (defun org-demote-subtree ()
  5302. "Demote the entire subtree. See `org-demote'.
  5303. See also `org-promote'."
  5304. (interactive)
  5305. (save-excursion
  5306. (org-map-tree 'org-demote))
  5307. (org-fix-position-after-promote))
  5308. (defun org-do-promote ()
  5309. "Promote the current heading higher up the tree.
  5310. If the region is active in `transient-mark-mode', promote all headings
  5311. in the region."
  5312. (interactive)
  5313. (save-excursion
  5314. (if (org-region-active-p)
  5315. (org-map-region 'org-promote (region-beginning) (region-end))
  5316. (org-promote)))
  5317. (org-fix-position-after-promote))
  5318. (defun org-do-demote ()
  5319. "Demote the current heading lower down the tree.
  5320. If the region is active in `transient-mark-mode', demote all headings
  5321. in the region."
  5322. (interactive)
  5323. (save-excursion
  5324. (if (org-region-active-p)
  5325. (org-map-region 'org-demote (region-beginning) (region-end))
  5326. (org-demote)))
  5327. (org-fix-position-after-promote))
  5328. (defun org-fix-position-after-promote ()
  5329. "Make sure that after pro/demotion cursor position is right."
  5330. (let ((pos (point)))
  5331. (when (save-excursion
  5332. (beginning-of-line 1)
  5333. (looking-at org-todo-line-regexp)
  5334. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5335. (cond ((eobp) (insert " "))
  5336. ((eolp) (insert " "))
  5337. ((equal (char-after) ?\ ) (forward-char 1))))))
  5338. (defun org-reduced-level (l)
  5339. "Compute the effective level of a heading.
  5340. This takes into account the setting of `org-odd-levels-only'."
  5341. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5342. (defun org-get-valid-level (level &optional change)
  5343. "Rectify a level change under the influence of `org-odd-levels-only'
  5344. LEVEL is a current level, CHANGE is by how much the level should be
  5345. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5346. even level numbers will become the next higher odd number."
  5347. (if org-odd-levels-only
  5348. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5349. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5350. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5351. (max 1 (+ level (or change 0)))))
  5352. (if (boundp 'define-obsolete-function-alias)
  5353. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5354. (define-obsolete-function-alias 'org-get-legal-level
  5355. 'org-get-valid-level)
  5356. (define-obsolete-function-alias 'org-get-legal-level
  5357. 'org-get-valid-level "23.1")))
  5358. (defun org-promote ()
  5359. "Promote the current heading higher up the tree.
  5360. If the region is active in `transient-mark-mode', promote all headings
  5361. in the region."
  5362. (org-back-to-heading t)
  5363. (let* ((level (save-match-data (funcall outline-level)))
  5364. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5365. (diff (abs (- level (length up-head) -1))))
  5366. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5367. (replace-match up-head nil t)
  5368. ;; Fixup tag positioning
  5369. (and org-auto-align-tags (org-set-tags nil t))
  5370. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5371. (run-hooks 'org-after-promote-entry-hook)))
  5372. (defun org-demote ()
  5373. "Demote the current heading lower down the tree.
  5374. If the region is active in `transient-mark-mode', demote all headings
  5375. in the region."
  5376. (org-back-to-heading t)
  5377. (let* ((level (save-match-data (funcall outline-level)))
  5378. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5379. (diff (abs (- level (length down-head) -1))))
  5380. (replace-match down-head nil t)
  5381. ;; Fixup tag positioning
  5382. (and org-auto-align-tags (org-set-tags nil t))
  5383. (if org-adapt-indentation (org-fixup-indentation diff))
  5384. (run-hooks 'org-after-demote-entry-hook)))
  5385. (defun org-map-tree (fun)
  5386. "Call FUN for every heading underneath the current one."
  5387. (org-back-to-heading)
  5388. (let ((level (funcall outline-level)))
  5389. (save-excursion
  5390. (funcall fun)
  5391. (while (and (progn
  5392. (outline-next-heading)
  5393. (> (funcall outline-level) level))
  5394. (not (eobp)))
  5395. (funcall fun)))))
  5396. (defun org-map-region (fun beg end)
  5397. "Call FUN for every heading between BEG and END."
  5398. (let ((org-ignore-region t))
  5399. (save-excursion
  5400. (setq end (copy-marker end))
  5401. (goto-char beg)
  5402. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5403. (< (point) end))
  5404. (funcall fun))
  5405. (while (and (progn
  5406. (outline-next-heading)
  5407. (< (point) end))
  5408. (not (eobp)))
  5409. (funcall fun)))))
  5410. (defun org-fixup-indentation (diff)
  5411. "Change the indentation in the current entry by DIFF
  5412. However, if any line in the current entry has no indentation, or if it
  5413. would end up with no indentation after the change, nothing at all is done."
  5414. (save-excursion
  5415. (let ((end (save-excursion (outline-next-heading)
  5416. (point-marker)))
  5417. (prohibit (if (> diff 0)
  5418. "^\\S-"
  5419. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5420. col)
  5421. (unless (save-excursion (end-of-line 1)
  5422. (re-search-forward prohibit end t))
  5423. (while (and (< (point) end)
  5424. (re-search-forward "^[ \t]+" end t))
  5425. (goto-char (match-end 0))
  5426. (setq col (current-column))
  5427. (if (< diff 0) (replace-match ""))
  5428. (org-indent-to-column (+ diff col))))
  5429. (move-marker end nil))))
  5430. (defun org-convert-to-odd-levels ()
  5431. "Convert an org-mode file with all levels allowed to one with odd levels.
  5432. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5433. level 5 etc."
  5434. (interactive)
  5435. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5436. (let ((outline-regexp org-outline-regexp)
  5437. (outline-level 'org-outline-level)
  5438. (org-odd-levels-only nil) n)
  5439. (save-excursion
  5440. (goto-char (point-min))
  5441. (while (re-search-forward "^\\*\\*+ " nil t)
  5442. (setq n (- (length (match-string 0)) 2))
  5443. (while (>= (setq n (1- n)) 0)
  5444. (org-demote))
  5445. (end-of-line 1))))))
  5446. (defun org-convert-to-oddeven-levels ()
  5447. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5448. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5449. section with an even level, conversion would destroy the structure of the file. An error
  5450. is signaled in this case."
  5451. (interactive)
  5452. (goto-char (point-min))
  5453. ;; First check if there are no even levels
  5454. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5455. (org-show-context t)
  5456. (error "Not all levels are odd in this file. Conversion not possible."))
  5457. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5458. (let ((outline-regexp org-outline-regexp)
  5459. (outline-level 'org-outline-level)
  5460. (org-odd-levels-only nil) n)
  5461. (save-excursion
  5462. (goto-char (point-min))
  5463. (while (re-search-forward "^\\*\\*+ " nil t)
  5464. (setq n (/ (1- (length (match-string 0))) 2))
  5465. (while (>= (setq n (1- n)) 0)
  5466. (org-promote))
  5467. (end-of-line 1))))))
  5468. (defun org-tr-level (n)
  5469. "Make N odd if required."
  5470. (if org-odd-levels-only (1+ (/ n 2)) n))
  5471. ;;; Vertical tree motion, cutting and pasting of subtrees
  5472. (defun org-move-subtree-up (&optional arg)
  5473. "Move the current subtree up past ARG headlines of the same level."
  5474. (interactive "p")
  5475. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5476. (defun org-move-subtree-down (&optional arg)
  5477. "Move the current subtree down past ARG headlines of the same level."
  5478. (interactive "p")
  5479. (setq arg (prefix-numeric-value arg))
  5480. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5481. 'org-get-last-sibling))
  5482. (ins-point (make-marker))
  5483. (cnt (abs arg))
  5484. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5485. ;; Select the tree
  5486. (org-back-to-heading)
  5487. (setq beg0 (point))
  5488. (save-excursion
  5489. (setq ne-beg (org-back-over-empty-lines))
  5490. (setq beg (point)))
  5491. (save-match-data
  5492. (save-excursion (outline-end-of-heading)
  5493. (setq folded (org-invisible-p)))
  5494. (outline-end-of-subtree))
  5495. (outline-next-heading)
  5496. (setq ne-end (org-back-over-empty-lines))
  5497. (setq end (point))
  5498. (goto-char beg0)
  5499. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5500. ;; include less whitespace
  5501. (save-excursion
  5502. (goto-char beg)
  5503. (forward-line (- ne-beg ne-end))
  5504. (setq beg (point))))
  5505. ;; Find insertion point, with error handling
  5506. (while (> cnt 0)
  5507. (or (and (funcall movfunc) (looking-at outline-regexp))
  5508. (progn (goto-char beg0)
  5509. (error "Cannot move past superior level or buffer limit")))
  5510. (setq cnt (1- cnt)))
  5511. (if (> arg 0)
  5512. ;; Moving forward - still need to move over subtree
  5513. (progn (org-end-of-subtree t t)
  5514. (save-excursion
  5515. (org-back-over-empty-lines)
  5516. (or (bolp) (newline)))))
  5517. (setq ne-ins (org-back-over-empty-lines))
  5518. (move-marker ins-point (point))
  5519. (setq txt (buffer-substring beg end))
  5520. (org-save-markers-in-region beg end)
  5521. (delete-region beg end)
  5522. (org-remove-empty-overlays-at beg)
  5523. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5524. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5525. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5526. (let ((bbb (point)))
  5527. (insert-before-markers txt)
  5528. (org-reinstall-markers-in-region bbb)
  5529. (move-marker ins-point bbb))
  5530. (or (bolp) (insert "\n"))
  5531. (setq ins-end (point))
  5532. (goto-char ins-point)
  5533. (org-skip-whitespace)
  5534. (when (and (< arg 0)
  5535. (org-first-sibling-p)
  5536. (> ne-ins ne-beg))
  5537. ;; Move whitespace back to beginning
  5538. (save-excursion
  5539. (goto-char ins-end)
  5540. (let ((kill-whole-line t))
  5541. (kill-line (- ne-ins ne-beg)) (point)))
  5542. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5543. (move-marker ins-point nil)
  5544. (if folded
  5545. (hide-subtree)
  5546. (org-show-entry)
  5547. (show-children)
  5548. (org-cycle-hide-drawers 'children))
  5549. (org-clean-visibility-after-subtree-move)))
  5550. (defvar org-subtree-clip ""
  5551. "Clipboard for cut and paste of subtrees.
  5552. This is actually only a copy of the kill, because we use the normal kill
  5553. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5554. (defvar org-subtree-clip-folded nil
  5555. "Was the last copied subtree folded?
  5556. This is used to fold the tree back after pasting.")
  5557. (defun org-cut-subtree (&optional n)
  5558. "Cut the current subtree into the clipboard.
  5559. With prefix arg N, cut this many sequential subtrees.
  5560. This is a short-hand for marking the subtree and then cutting it."
  5561. (interactive "p")
  5562. (org-copy-subtree n 'cut))
  5563. (defun org-copy-subtree (&optional n cut force-store-markers)
  5564. "Cut the current subtree into the clipboard.
  5565. With prefix arg N, cut this many sequential subtrees.
  5566. This is a short-hand for marking the subtree and then copying it.
  5567. If CUT is non-nil, actually cut the subtree.
  5568. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5569. of some markers in the region, even if CUT is non-nil. This is
  5570. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5571. (interactive "p")
  5572. (let (beg end folded (beg0 (point)))
  5573. (if (interactive-p)
  5574. (org-back-to-heading nil) ; take what looks like a subtree
  5575. (org-back-to-heading t)) ; take what is really there
  5576. (org-back-over-empty-lines)
  5577. (setq beg (point))
  5578. (skip-chars-forward " \t\r\n")
  5579. (save-match-data
  5580. (save-excursion (outline-end-of-heading)
  5581. (setq folded (org-invisible-p)))
  5582. (condition-case nil
  5583. (org-forward-same-level (1- n) t)
  5584. (error nil))
  5585. (org-end-of-subtree t t))
  5586. (org-back-over-empty-lines)
  5587. (setq end (point))
  5588. (goto-char beg0)
  5589. (when (> end beg)
  5590. (setq org-subtree-clip-folded folded)
  5591. (when (or cut force-store-markers)
  5592. (org-save-markers-in-region beg end))
  5593. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5594. (setq org-subtree-clip (current-kill 0))
  5595. (message "%s: Subtree(s) with %d characters"
  5596. (if cut "Cut" "Copied")
  5597. (length org-subtree-clip)))))
  5598. (defun org-paste-subtree (&optional level tree for-yank)
  5599. "Paste the clipboard as a subtree, with modification of headline level.
  5600. The entire subtree is promoted or demoted in order to match a new headline
  5601. level.
  5602. If the cursor is at the beginning of a headline, the same level as
  5603. that headline is used to paste the tree
  5604. If not, the new level is derived from the *visible* headings
  5605. before and after the insertion point, and taken to be the inferior headline
  5606. level of the two. So if the previous visible heading is level 3 and the
  5607. next is level 4 (or vice versa), level 4 will be used for insertion.
  5608. This makes sure that the subtree remains an independent subtree and does
  5609. not swallow low level entries.
  5610. You can also force a different level, either by using a numeric prefix
  5611. argument, or by inserting the heading marker by hand. For example, if the
  5612. cursor is after \"*****\", then the tree will be shifted to level 5.
  5613. If optional TREE is given, use this text instead of the kill ring.
  5614. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5615. move back over whitespace before inserting, and move point to the end of
  5616. the inserted text when done."
  5617. (interactive "P")
  5618. (setq tree (or tree (and kill-ring (current-kill 0))))
  5619. (unless (org-kill-is-subtree-p tree)
  5620. (error "%s"
  5621. (substitute-command-keys
  5622. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5623. (let* ((visp (not (org-invisible-p)))
  5624. (txt tree)
  5625. (^re (concat "^\\(" outline-regexp "\\)"))
  5626. (re (concat "\\(" outline-regexp "\\)"))
  5627. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5628. (old-level (if (string-match ^re txt)
  5629. (- (match-end 0) (match-beginning 0) 1)
  5630. -1))
  5631. (force-level (cond (level (prefix-numeric-value level))
  5632. ((and (looking-at "[ \t]*$")
  5633. (string-match
  5634. ^re_ (buffer-substring
  5635. (point-at-bol) (point))))
  5636. (- (match-end 1) (match-beginning 1)))
  5637. ((and (bolp)
  5638. (looking-at org-outline-regexp))
  5639. (- (match-end 0) (point) 1))
  5640. (t nil)))
  5641. (previous-level (save-excursion
  5642. (condition-case nil
  5643. (progn
  5644. (outline-previous-visible-heading 1)
  5645. (if (looking-at re)
  5646. (- (match-end 0) (match-beginning 0) 1)
  5647. 1))
  5648. (error 1))))
  5649. (next-level (save-excursion
  5650. (condition-case nil
  5651. (progn
  5652. (or (looking-at outline-regexp)
  5653. (outline-next-visible-heading 1))
  5654. (if (looking-at re)
  5655. (- (match-end 0) (match-beginning 0) 1)
  5656. 1))
  5657. (error 1))))
  5658. (new-level (or force-level (max previous-level next-level)))
  5659. (shift (if (or (= old-level -1)
  5660. (= new-level -1)
  5661. (= old-level new-level))
  5662. 0
  5663. (- new-level old-level)))
  5664. (delta (if (> shift 0) -1 1))
  5665. (func (if (> shift 0) 'org-demote 'org-promote))
  5666. (org-odd-levels-only nil)
  5667. beg end newend)
  5668. ;; Remove the forced level indicator
  5669. (if force-level
  5670. (delete-region (point-at-bol) (point)))
  5671. ;; Paste
  5672. (beginning-of-line 1)
  5673. (unless for-yank (org-back-over-empty-lines))
  5674. (setq beg (point))
  5675. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5676. (insert-before-markers txt)
  5677. (unless (string-match "\n\\'" txt) (insert "\n"))
  5678. (setq newend (point))
  5679. (org-reinstall-markers-in-region beg)
  5680. (setq end (point))
  5681. (goto-char beg)
  5682. (skip-chars-forward " \t\n\r")
  5683. (setq beg (point))
  5684. (if (and (org-invisible-p) visp)
  5685. (save-excursion (outline-show-heading)))
  5686. ;; Shift if necessary
  5687. (unless (= shift 0)
  5688. (save-restriction
  5689. (narrow-to-region beg end)
  5690. (while (not (= shift 0))
  5691. (org-map-region func (point-min) (point-max))
  5692. (setq shift (+ delta shift)))
  5693. (goto-char (point-min))
  5694. (setq newend (point-max))))
  5695. (when (or (interactive-p) for-yank)
  5696. (message "Clipboard pasted as level %d subtree" new-level))
  5697. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5698. kill-ring
  5699. (eq org-subtree-clip (current-kill 0))
  5700. org-subtree-clip-folded)
  5701. ;; The tree was folded before it was killed/copied
  5702. (hide-subtree))
  5703. (and for-yank (goto-char newend))))
  5704. (defun org-kill-is-subtree-p (&optional txt)
  5705. "Check if the current kill is an outline subtree, or a set of trees.
  5706. Returns nil if kill does not start with a headline, or if the first
  5707. headline level is not the largest headline level in the tree.
  5708. So this will actually accept several entries of equal levels as well,
  5709. which is OK for `org-paste-subtree'.
  5710. If optional TXT is given, check this string instead of the current kill."
  5711. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5712. (start-level (and kill
  5713. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5714. org-outline-regexp "\\)")
  5715. kill)
  5716. (- (match-end 2) (match-beginning 2) 1)))
  5717. (re (concat "^" org-outline-regexp))
  5718. (start (1+ (or (match-beginning 2) -1))))
  5719. (if (not start-level)
  5720. (progn
  5721. nil) ;; does not even start with a heading
  5722. (catch 'exit
  5723. (while (setq start (string-match re kill (1+ start)))
  5724. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5725. (throw 'exit nil)))
  5726. t))))
  5727. (defvar org-markers-to-move nil
  5728. "Markers that should be moved with a cut-and-paste operation.
  5729. Those markers are stored together with their positions relative to
  5730. the start of the region.")
  5731. (defun org-save-markers-in-region (beg end)
  5732. "Check markers in region.
  5733. If these markers are between BEG and END, record their position relative
  5734. to BEG, so that after moving the block of text, we can put the markers back
  5735. into place.
  5736. This function gets called just before an entry or tree gets cut from the
  5737. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5738. called immediately, to move the markers with the entries."
  5739. (setq org-markers-to-move nil)
  5740. (when (featurep 'org-clock)
  5741. (org-clock-save-markers-for-cut-and-paste beg end))
  5742. (when (featurep 'org-agenda)
  5743. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5744. (defun org-check-and-save-marker (marker beg end)
  5745. "Check if MARKER is between BEG and END.
  5746. If yes, remember the marker and the distance to BEG."
  5747. (when (and (marker-buffer marker)
  5748. (equal (marker-buffer marker) (current-buffer)))
  5749. (if (and (>= marker beg) (< marker end))
  5750. (push (cons marker (- marker beg)) org-markers-to-move))))
  5751. (defun org-reinstall-markers-in-region (beg)
  5752. "Move all remembered markers to their position relative to BEG."
  5753. (mapc (lambda (x)
  5754. (move-marker (car x) (+ beg (cdr x))))
  5755. org-markers-to-move)
  5756. (setq org-markers-to-move nil))
  5757. (defun org-narrow-to-subtree ()
  5758. "Narrow buffer to the current subtree."
  5759. (interactive)
  5760. (save-excursion
  5761. (save-match-data
  5762. (narrow-to-region
  5763. (progn (org-back-to-heading t) (point))
  5764. (progn (org-end-of-subtree t t) (point))))))
  5765. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5766. "Clone the task (subtree) at point N times.
  5767. The clones will be inserted as siblings.
  5768. In interactive use, the user will be prompted for the number of clones
  5769. to be produced, and for a time SHIFT, which may be a repeater as used
  5770. in time stamps, for example `+3d'.
  5771. When a valid repeater is given and the entry contains any time stamps,
  5772. the clones will become a sequence in time, with time stamps in the
  5773. subtree shifted for each clone produced. If SHIFT is nil or the
  5774. empty string, time stamps will be left alone.
  5775. If the original subtree did contain time stamps with a repeater,
  5776. the following will happen:
  5777. - the repeater will be removed in each clone
  5778. - an additional clone will be produced, with the current, unshifted
  5779. date(s) in the entry.
  5780. - the original entry will be placed *after* all the clones, with
  5781. repeater intact.
  5782. - the start days in the repeater in the original entry will be shifted
  5783. to past the last clone.
  5784. I this way you can spell out a number of instances of a repeating task,
  5785. and still retain the repeater to cover future instances of the task."
  5786. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5787. (let (beg end template task
  5788. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5789. (if (not (and (integerp n) (> n 0)))
  5790. (error "Invalid number of replications %s" n))
  5791. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5792. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5793. shift)))
  5794. (error "Invalid shift specification %s" shift))
  5795. (when doshift
  5796. (setq shift-n (string-to-number (match-string 1 shift))
  5797. shift-what (cdr (assoc (match-string 2 shift)
  5798. '(("d" . day) ("w" . week)
  5799. ("m" . month) ("y" . year))))))
  5800. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5801. (setq nmin 1 nmax n)
  5802. (org-back-to-heading t)
  5803. (setq beg (point))
  5804. (org-end-of-subtree t t)
  5805. (setq end (point))
  5806. (setq template (buffer-substring beg end))
  5807. (when (and doshift
  5808. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5809. (delete-region beg end)
  5810. (setq end beg)
  5811. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5812. (goto-char end)
  5813. (loop for n from nmin to nmax do
  5814. (if (not doshift)
  5815. (setq task template)
  5816. (with-temp-buffer
  5817. (insert template)
  5818. (org-mode)
  5819. (goto-char (point-min))
  5820. (while (re-search-forward org-ts-regexp-both nil t)
  5821. (org-timestamp-change (* n shift-n) shift-what))
  5822. (unless (= n n-no-remove)
  5823. (goto-char (point-min))
  5824. (while (re-search-forward org-ts-regexp nil t)
  5825. (save-excursion
  5826. (goto-char (match-beginning 0))
  5827. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5828. (delete-region (match-beginning 1) (match-end 1))))))
  5829. (setq task (buffer-string))))
  5830. (insert task))
  5831. (goto-char beg)))
  5832. ;;; Outline Sorting
  5833. (defun org-sort (with-case)
  5834. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5835. Optional argument WITH-CASE means sort case-sensitively.
  5836. With a double prefix argument, also remove duplicate entries."
  5837. (interactive "P")
  5838. (if (org-at-table-p)
  5839. (org-call-with-arg 'org-table-sort-lines with-case)
  5840. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5841. (defun org-sort-remove-invisible (s)
  5842. (remove-text-properties 0 (length s) org-rm-props s)
  5843. (while (string-match org-bracket-link-regexp s)
  5844. (setq s (replace-match (if (match-end 2)
  5845. (match-string 3 s)
  5846. (match-string 1 s)) t t s)))
  5847. s)
  5848. (defvar org-priority-regexp) ; defined later in the file
  5849. (defvar org-after-sorting-entries-or-items-hook nil
  5850. "Hook that is run after a bunch of entries or items have been sorted.
  5851. When children are sorted, the cursor is in the parent line when this
  5852. hook gets called. When a region or a plain list is sorted, the cursor
  5853. will be in the first entry of the sorted region/list.")
  5854. (defun org-sort-entries-or-items
  5855. (&optional with-case sorting-type getkey-func compare-func property)
  5856. "Sort entries on a certain level of an outline tree, or plain list items.
  5857. If there is an active region, the entries in the region are sorted.
  5858. Else, if the cursor is before the first entry, sort the top-level items.
  5859. Else, the children of the entry at point are sorted.
  5860. If the cursor is at the first item in a plain list, the list items will be
  5861. sorted.
  5862. Sorting can be alphabetically, numerically, by date/time as given by
  5863. a time stamp, by a property or by priority.
  5864. The command prompts for the sorting type unless it has been given to the
  5865. function through the SORTING-TYPE argument, which needs to a character,
  5866. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5867. precise meaning of each character:
  5868. n Numerically, by converting the beginning of the entry/item to a number.
  5869. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5870. t By date/time, either the first active time stamp in the entry, or, if
  5871. none exist, by the first inactive one.
  5872. In items, only the first line will be chekced.
  5873. s By the scheduled date/time.
  5874. d By deadline date/time.
  5875. c By creation time, which is assumed to be the first inactive time stamp
  5876. at the beginning of a line.
  5877. p By priority according to the cookie.
  5878. r By the value of a property.
  5879. Capital letters will reverse the sort order.
  5880. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5881. called with point at the beginning of the record. It must return either
  5882. a string or a number that should serve as the sorting key for that record.
  5883. Comparing entries ignores case by default. However, with an optional argument
  5884. WITH-CASE, the sorting considers case as well."
  5885. (interactive "P")
  5886. (let ((case-func (if with-case 'identity 'downcase))
  5887. start beg end stars re re2
  5888. txt what tmp plain-list-p)
  5889. ;; Find beginning and end of region to sort
  5890. (cond
  5891. ((org-region-active-p)
  5892. ;; we will sort the region
  5893. (setq end (region-end)
  5894. what "region")
  5895. (goto-char (region-beginning))
  5896. (if (not (org-on-heading-p)) (outline-next-heading))
  5897. (setq start (point)))
  5898. ((org-at-item-p)
  5899. ;; we will sort this plain list
  5900. (org-beginning-of-item-list) (setq start (point))
  5901. (org-end-of-item-list) (setq end (point))
  5902. (goto-char start)
  5903. (setq plain-list-p t
  5904. what "plain list"))
  5905. ((or (org-on-heading-p)
  5906. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5907. ;; we will sort the children of the current headline
  5908. (org-back-to-heading)
  5909. (setq start (point)
  5910. end (progn (org-end-of-subtree t t)
  5911. (org-back-over-empty-lines)
  5912. (point))
  5913. what "children")
  5914. (goto-char start)
  5915. (show-subtree)
  5916. (outline-next-heading))
  5917. (t
  5918. ;; we will sort the top-level entries in this file
  5919. (goto-char (point-min))
  5920. (or (org-on-heading-p) (outline-next-heading))
  5921. (setq start (point) end (point-max) what "top-level")
  5922. (goto-char start)
  5923. (show-all)))
  5924. (setq beg (point))
  5925. (if (>= beg end) (error "Nothing to sort"))
  5926. (unless plain-list-p
  5927. (looking-at "\\(\\*+\\)")
  5928. (setq stars (match-string 1)
  5929. re (concat "^" (regexp-quote stars) " +")
  5930. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5931. txt (buffer-substring beg end))
  5932. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5933. (if (and (not (equal stars "*")) (string-match re2 txt))
  5934. (error "Region to sort contains a level above the first entry")))
  5935. (unless sorting-type
  5936. (message
  5937. (if plain-list-p
  5938. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5939. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5940. [t]ime [s]cheduled [d]eadline [c]reated
  5941. A/N/T/S/D/C/P/O/F means reversed:")
  5942. what)
  5943. (setq sorting-type (read-char-exclusive))
  5944. (and (= (downcase sorting-type) ?f)
  5945. (setq getkey-func
  5946. (org-icompleting-read "Sort using function: "
  5947. obarray 'fboundp t nil nil))
  5948. (setq getkey-func (intern getkey-func)))
  5949. (and (= (downcase sorting-type) ?r)
  5950. (setq property
  5951. (org-icompleting-read "Property: "
  5952. (mapcar 'list (org-buffer-property-keys t))
  5953. nil t))))
  5954. (message "Sorting entries...")
  5955. (save-restriction
  5956. (narrow-to-region start end)
  5957. (let ((dcst (downcase sorting-type))
  5958. (case-fold-search nil)
  5959. (now (current-time)))
  5960. (sort-subr
  5961. (/= dcst sorting-type)
  5962. ;; This function moves to the beginning character of the "record" to
  5963. ;; be sorted.
  5964. (if plain-list-p
  5965. (lambda nil
  5966. (if (org-at-item-p) t (goto-char (point-max))))
  5967. (lambda nil
  5968. (if (re-search-forward re nil t)
  5969. (goto-char (match-beginning 0))
  5970. (goto-char (point-max)))))
  5971. ;; This function moves to the last character of the "record" being
  5972. ;; sorted.
  5973. (if plain-list-p
  5974. 'org-end-of-item
  5975. (lambda nil
  5976. (save-match-data
  5977. (condition-case nil
  5978. (outline-forward-same-level 1)
  5979. (error
  5980. (goto-char (point-max)))))))
  5981. ;; This function returns the value that gets sorted against.
  5982. (if plain-list-p
  5983. (lambda nil
  5984. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5985. (cond
  5986. ((= dcst ?n)
  5987. (string-to-number (buffer-substring (match-end 0)
  5988. (point-at-eol))))
  5989. ((= dcst ?a)
  5990. (buffer-substring (match-end 0) (point-at-eol)))
  5991. ((= dcst ?t)
  5992. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5993. (re-search-forward org-ts-regexp-both
  5994. (point-at-eol) t))
  5995. (org-time-string-to-seconds (match-string 0))
  5996. (org-float-time now)))
  5997. ((= dcst ?f)
  5998. (if getkey-func
  5999. (progn
  6000. (setq tmp (funcall getkey-func))
  6001. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6002. tmp)
  6003. (error "Invalid key function `%s'" getkey-func)))
  6004. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6005. (lambda nil
  6006. (cond
  6007. ((= dcst ?n)
  6008. (if (looking-at org-complex-heading-regexp)
  6009. (string-to-number (match-string 4))
  6010. nil))
  6011. ((= dcst ?a)
  6012. (if (looking-at org-complex-heading-regexp)
  6013. (funcall case-func (match-string 4))
  6014. nil))
  6015. ((= dcst ?t)
  6016. (let ((end (save-excursion (outline-next-heading) (point))))
  6017. (if (or (re-search-forward org-ts-regexp end t)
  6018. (re-search-forward org-ts-regexp-both end t))
  6019. (org-time-string-to-seconds (match-string 0))
  6020. (org-float-time now))))
  6021. ((= dcst ?c)
  6022. (let ((end (save-excursion (outline-next-heading) (point))))
  6023. (if (re-search-forward
  6024. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6025. end t)
  6026. (org-time-string-to-seconds (match-string 0))
  6027. (org-float-time now))))
  6028. ((= dcst ?s)
  6029. (let ((end (save-excursion (outline-next-heading) (point))))
  6030. (if (re-search-forward org-scheduled-time-regexp end t)
  6031. (org-time-string-to-seconds (match-string 1))
  6032. (org-float-time now))))
  6033. ((= dcst ?d)
  6034. (let ((end (save-excursion (outline-next-heading) (point))))
  6035. (if (re-search-forward org-deadline-time-regexp end t)
  6036. (org-time-string-to-seconds (match-string 1))
  6037. (org-float-time now))))
  6038. ((= dcst ?p)
  6039. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6040. (string-to-char (match-string 2))
  6041. org-default-priority))
  6042. ((= dcst ?r)
  6043. (or (org-entry-get nil property) ""))
  6044. ((= dcst ?o)
  6045. (if (looking-at org-complex-heading-regexp)
  6046. (- 9999 (length (member (match-string 2)
  6047. org-todo-keywords-1)))))
  6048. ((= dcst ?f)
  6049. (if getkey-func
  6050. (progn
  6051. (setq tmp (funcall getkey-func))
  6052. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6053. tmp)
  6054. (error "Invalid key function `%s'" getkey-func)))
  6055. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6056. nil
  6057. (cond
  6058. ((= dcst ?a) 'string<)
  6059. ((= dcst ?f) compare-func)
  6060. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6061. (t nil)))))
  6062. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6063. (message "Sorting entries...done")))
  6064. (defun org-do-sort (table what &optional with-case sorting-type)
  6065. "Sort TABLE of WHAT according to SORTING-TYPE.
  6066. The user will be prompted for the SORTING-TYPE if the call to this
  6067. function does not specify it. WHAT is only for the prompt, to indicate
  6068. what is being sorted. The sorting key will be extracted from
  6069. the car of the elements of the table.
  6070. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6071. (unless sorting-type
  6072. (message
  6073. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6074. what)
  6075. (setq sorting-type (read-char-exclusive)))
  6076. (let ((dcst (downcase sorting-type))
  6077. extractfun comparefun)
  6078. ;; Define the appropriate functions
  6079. (cond
  6080. ((= dcst ?n)
  6081. (setq extractfun 'string-to-number
  6082. comparefun (if (= dcst sorting-type) '< '>)))
  6083. ((= dcst ?a)
  6084. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6085. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6086. comparefun (if (= dcst sorting-type)
  6087. 'string<
  6088. (lambda (a b) (and (not (string< a b))
  6089. (not (string= a b)))))))
  6090. ((= dcst ?t)
  6091. (setq extractfun
  6092. (lambda (x)
  6093. (if (or (string-match org-ts-regexp x)
  6094. (string-match org-ts-regexp-both x))
  6095. (org-float-time
  6096. (org-time-string-to-time (match-string 0 x)))
  6097. 0))
  6098. comparefun (if (= dcst sorting-type) '< '>)))
  6099. (t (error "Invalid sorting type `%c'" sorting-type)))
  6100. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6101. table)
  6102. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6103. ;;; The orgstruct minor mode
  6104. ;; Define a minor mode which can be used in other modes in order to
  6105. ;; integrate the org-mode structure editing commands.
  6106. ;; This is really a hack, because the org-mode structure commands use
  6107. ;; keys which normally belong to the major mode. Here is how it
  6108. ;; works: The minor mode defines all the keys necessary to operate the
  6109. ;; structure commands, but wraps the commands into a function which
  6110. ;; tests if the cursor is currently at a headline or a plain list
  6111. ;; item. If that is the case, the structure command is used,
  6112. ;; temporarily setting many Org-mode variables like regular
  6113. ;; expressions for filling etc. However, when any of those keys is
  6114. ;; used at a different location, function uses `key-binding' to look
  6115. ;; up if the key has an associated command in another currently active
  6116. ;; keymap (minor modes, major mode, global), and executes that
  6117. ;; command. There might be problems if any of the keys is otherwise
  6118. ;; used as a prefix key.
  6119. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6120. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6121. ;; addresses this by checking explicitly for both bindings.
  6122. (defvar orgstruct-mode-map (make-sparse-keymap)
  6123. "Keymap for the minor `orgstruct-mode'.")
  6124. (defvar org-local-vars nil
  6125. "List of local variables, for use by `orgstruct-mode'")
  6126. ;;;###autoload
  6127. (define-minor-mode orgstruct-mode
  6128. "Toggle the minor more `orgstruct-mode'.
  6129. This mode is for using Org-mode structure commands in other modes.
  6130. The following key behave as if Org-mode was active, if the cursor
  6131. is on a headline, or on a plain list item (both in the definition
  6132. of Org-mode).
  6133. M-up Move entry/item up
  6134. M-down Move entry/item down
  6135. M-left Promote
  6136. M-right Demote
  6137. M-S-up Move entry/item up
  6138. M-S-down Move entry/item down
  6139. M-S-left Promote subtree
  6140. M-S-right Demote subtree
  6141. M-q Fill paragraph and items like in Org-mode
  6142. C-c ^ Sort entries
  6143. C-c - Cycle list bullet
  6144. TAB Cycle item visibility
  6145. M-RET Insert new heading/item
  6146. S-M-RET Insert new TODO heading / Checkbox item
  6147. C-c C-c Set tags / toggle checkbox"
  6148. nil " OrgStruct" nil
  6149. (org-load-modules-maybe)
  6150. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6151. ;;;###autoload
  6152. (defun turn-on-orgstruct ()
  6153. "Unconditionally turn on `orgstruct-mode'."
  6154. (orgstruct-mode 1))
  6155. (defun orgstruct++-mode (&optional arg)
  6156. "Toggle `orgstruct-mode', the enhanced version of it.
  6157. In addition to setting orgstruct-mode, this also exports all indentation
  6158. and autofilling variables from org-mode into the buffer. It will also
  6159. recognize item context in multiline items.
  6160. Note that turning off orgstruct-mode will *not* remove the
  6161. indentation/paragraph settings. This can only be done by refreshing the
  6162. major mode, for example with \\[normal-mode]."
  6163. (interactive "P")
  6164. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6165. (if (< arg 1)
  6166. (orgstruct-mode -1)
  6167. (orgstruct-mode 1)
  6168. (let (var val)
  6169. (mapc
  6170. (lambda (x)
  6171. (when (string-match
  6172. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6173. (symbol-name (car x)))
  6174. (setq var (car x) val (nth 1 x))
  6175. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6176. org-local-vars)
  6177. (org-set-local 'orgstruct-is-++ t))))
  6178. (defvar orgstruct-is-++ nil
  6179. "Is orgstruct-mode in ++ version in the current-buffer?")
  6180. (make-variable-buffer-local 'orgstruct-is-++)
  6181. ;;;###autoload
  6182. (defun turn-on-orgstruct++ ()
  6183. "Unconditionally turn on `orgstruct++-mode'."
  6184. (orgstruct++-mode 1))
  6185. (defun orgstruct-error ()
  6186. "Error when there is no default binding for a structure key."
  6187. (interactive)
  6188. (error "This key has no function outside structure elements"))
  6189. (defun orgstruct-setup ()
  6190. "Setup orgstruct keymaps."
  6191. (let ((nfunc 0)
  6192. (bindings
  6193. (list
  6194. '([(meta up)] org-metaup)
  6195. '([(meta down)] org-metadown)
  6196. '([(meta left)] org-metaleft)
  6197. '([(meta right)] org-metaright)
  6198. '([(meta shift up)] org-shiftmetaup)
  6199. '([(meta shift down)] org-shiftmetadown)
  6200. '([(meta shift left)] org-shiftmetaleft)
  6201. '([(meta shift right)] org-shiftmetaright)
  6202. '([?\e (up)] org-metaup)
  6203. '([?\e (down)] org-metadown)
  6204. '([?\e (left)] org-metaleft)
  6205. '([?\e (right)] org-metaright)
  6206. '([?\e (shift up)] org-shiftmetaup)
  6207. '([?\e (shift down)] org-shiftmetadown)
  6208. '([?\e (shift left)] org-shiftmetaleft)
  6209. '([?\e (shift right)] org-shiftmetaright)
  6210. '([(shift up)] org-shiftup)
  6211. '([(shift down)] org-shiftdown)
  6212. '([(shift left)] org-shiftleft)
  6213. '([(shift right)] org-shiftright)
  6214. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6215. '("\M-q" fill-paragraph)
  6216. '("\C-c^" org-sort)
  6217. '("\C-c-" org-cycle-list-bullet)))
  6218. elt key fun cmd)
  6219. (while (setq elt (pop bindings))
  6220. (setq nfunc (1+ nfunc))
  6221. (setq key (org-key (car elt))
  6222. fun (nth 1 elt)
  6223. cmd (orgstruct-make-binding fun nfunc key))
  6224. (org-defkey orgstruct-mode-map key cmd))
  6225. ;; Special treatment needed for TAB and RET
  6226. (org-defkey orgstruct-mode-map [(tab)]
  6227. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6228. (org-defkey orgstruct-mode-map "\C-i"
  6229. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6230. (org-defkey orgstruct-mode-map "\M-\C-m"
  6231. (orgstruct-make-binding 'org-insert-heading 105
  6232. "\M-\C-m" [(meta return)]))
  6233. (org-defkey orgstruct-mode-map [(meta return)]
  6234. (orgstruct-make-binding 'org-insert-heading 106
  6235. [(meta return)] "\M-\C-m"))
  6236. (org-defkey orgstruct-mode-map [(shift meta return)]
  6237. (orgstruct-make-binding 'org-insert-todo-heading 107
  6238. [(meta return)] "\M-\C-m"))
  6239. (org-defkey orgstruct-mode-map "\e\C-m"
  6240. (orgstruct-make-binding 'org-insert-heading 108
  6241. "\e\C-m" [?\e (return)]))
  6242. (org-defkey orgstruct-mode-map [?\e (return)]
  6243. (orgstruct-make-binding 'org-insert-heading 109
  6244. [?\e (return)] "\e\C-m"))
  6245. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6246. (orgstruct-make-binding 'org-insert-todo-heading 110
  6247. [?\e (return)] "\e\C-m"))
  6248. (unless org-local-vars
  6249. (setq org-local-vars (org-get-local-variables)))
  6250. t))
  6251. (defun orgstruct-make-binding (fun n &rest keys)
  6252. "Create a function for binding in the structure minor mode.
  6253. FUN is the command to call inside a table. N is used to create a unique
  6254. command name. KEYS are keys that should be checked in for a command
  6255. to execute outside of tables."
  6256. (eval
  6257. (list 'defun
  6258. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6259. '(arg)
  6260. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6261. "Outside of structure, run the binding of `"
  6262. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6263. "'.")
  6264. '(interactive "p")
  6265. (list 'if
  6266. `(org-context-p 'headline 'item
  6267. (and orgstruct-is-++
  6268. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6269. 'item-body))
  6270. (list 'org-run-like-in-org-mode (list 'quote fun))
  6271. (list 'let '(orgstruct-mode)
  6272. (list 'call-interactively
  6273. (append '(or)
  6274. (mapcar (lambda (k)
  6275. (list 'key-binding k))
  6276. keys)
  6277. '('orgstruct-error))))))))
  6278. (defun org-context-p (&rest contexts)
  6279. "Check if local context is any of CONTEXTS.
  6280. Possible values in the list of contexts are `table', `headline', and `item'."
  6281. (let ((pos (point)))
  6282. (goto-char (point-at-bol))
  6283. (prog1 (or (and (memq 'table contexts)
  6284. (looking-at "[ \t]*|"))
  6285. (and (memq 'headline contexts)
  6286. ;;????????? (looking-at "\\*+"))
  6287. (looking-at outline-regexp))
  6288. (and (memq 'item contexts)
  6289. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6290. (and (memq 'item-body contexts)
  6291. (org-in-item-p)))
  6292. (goto-char pos))))
  6293. (defun org-get-local-variables ()
  6294. "Return a list of all local variables in an org-mode buffer."
  6295. (let (varlist)
  6296. (with-current-buffer (get-buffer-create "*Org tmp*")
  6297. (erase-buffer)
  6298. (org-mode)
  6299. (setq varlist (buffer-local-variables)))
  6300. (kill-buffer "*Org tmp*")
  6301. (delq nil
  6302. (mapcar
  6303. (lambda (x)
  6304. (setq x
  6305. (if (symbolp x)
  6306. (list x)
  6307. (list (car x) (list 'quote (cdr x)))))
  6308. (if (string-match
  6309. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6310. (symbol-name (car x)))
  6311. x nil))
  6312. varlist))))
  6313. ;;;###autoload
  6314. (defun org-run-like-in-org-mode (cmd)
  6315. "Run a command, pretending that the current buffer is in Org-mode.
  6316. This will temporarily bind local variables that are typically bound in
  6317. Org-mode to the values they have in Org-mode, and then interactively
  6318. call CMD."
  6319. (org-load-modules-maybe)
  6320. (unless org-local-vars
  6321. (setq org-local-vars (org-get-local-variables)))
  6322. (eval (list 'let org-local-vars
  6323. (list 'call-interactively (list 'quote cmd)))))
  6324. ;;;; Archiving
  6325. (defun org-get-category (&optional pos)
  6326. "Get the category applying to position POS."
  6327. (get-text-property (or pos (point)) 'org-category))
  6328. (defun org-refresh-category-properties ()
  6329. "Refresh category text properties in the buffer."
  6330. (let ((def-cat (cond
  6331. ((null org-category)
  6332. (if buffer-file-name
  6333. (file-name-sans-extension
  6334. (file-name-nondirectory buffer-file-name))
  6335. "???"))
  6336. ((symbolp org-category) (symbol-name org-category))
  6337. (t org-category)))
  6338. beg end cat pos optionp)
  6339. (org-unmodified
  6340. (save-excursion
  6341. (save-restriction
  6342. (widen)
  6343. (goto-char (point-min))
  6344. (put-text-property (point) (point-max) 'org-category def-cat)
  6345. (while (re-search-forward
  6346. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6347. (setq pos (match-end 0)
  6348. optionp (equal (char-after (match-beginning 0)) ?#)
  6349. cat (org-trim (match-string 2)))
  6350. (if optionp
  6351. (setq beg (point-at-bol) end (point-max))
  6352. (org-back-to-heading t)
  6353. (setq beg (point) end (org-end-of-subtree t t)))
  6354. (put-text-property beg end 'org-category cat)
  6355. (goto-char pos)))))))
  6356. ;;;; Link Stuff
  6357. ;;; Link abbreviations
  6358. (defun org-link-expand-abbrev (link)
  6359. "Apply replacements as defined in `org-link-abbrev-alist."
  6360. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6361. (let* ((key (match-string 1 link))
  6362. (as (or (assoc key org-link-abbrev-alist-local)
  6363. (assoc key org-link-abbrev-alist)))
  6364. (tag (and (match-end 2) (match-string 3 link)))
  6365. rpl)
  6366. (if (not as)
  6367. link
  6368. (setq rpl (cdr as))
  6369. (cond
  6370. ((symbolp rpl) (funcall rpl tag))
  6371. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6372. ((string-match "%h" rpl)
  6373. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6374. (t (concat rpl tag)))))
  6375. link))
  6376. ;;; Storing and inserting links
  6377. (defvar org-insert-link-history nil
  6378. "Minibuffer history for links inserted with `org-insert-link'.")
  6379. (defvar org-stored-links nil
  6380. "Contains the links stored with `org-store-link'.")
  6381. (defvar org-store-link-plist nil
  6382. "Plist with info about the most recently link created with `org-store-link'.")
  6383. (defvar org-link-protocols nil
  6384. "Link protocols added to Org-mode using `org-add-link-type'.")
  6385. (defvar org-store-link-functions nil
  6386. "List of functions that are called to create and store a link.
  6387. Each function will be called in turn until one returns a non-nil
  6388. value. Each function should check if it is responsible for creating
  6389. this link (for example by looking at the major mode).
  6390. If not, it must exit and return nil.
  6391. If yes, it should return a non-nil value after a calling
  6392. `org-store-link-props' with a list of properties and values.
  6393. Special properties are:
  6394. :type The link prefix. like \"http\". This must be given.
  6395. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6396. This is obligatory as well.
  6397. :description Optional default description for the second pair
  6398. of brackets in an Org-mode link. The user can still change
  6399. this when inserting this link into an Org-mode buffer.
  6400. In addition to these, any additional properties can be specified
  6401. and then used in remember templates.")
  6402. (defun org-add-link-type (type &optional follow export)
  6403. "Add TYPE to the list of `org-link-types'.
  6404. Re-compute all regular expressions depending on `org-link-types'
  6405. FOLLOW and EXPORT are two functions.
  6406. FOLLOW should take the link path as the single argument and do whatever
  6407. is necessary to follow the link, for example find a file or display
  6408. a mail message.
  6409. EXPORT should format the link path for export to one of the export formats.
  6410. It should be a function accepting three arguments:
  6411. path the path of the link, the text after the prefix (like \"http:\")
  6412. desc the description of the link, if any, nil if there was no description
  6413. format the export format, a symbol like `html' or `latex'.
  6414. The function may use the FORMAT information to return different values
  6415. depending on the format. The return value will be put literally into
  6416. the exported file.
  6417. Org-mode has a built-in default for exporting links. If you are happy with
  6418. this default, there is no need to define an export function for the link
  6419. type. For a simple example of an export function, see `org-bbdb.el'."
  6420. (add-to-list 'org-link-types type t)
  6421. (org-make-link-regexps)
  6422. (if (assoc type org-link-protocols)
  6423. (setcdr (assoc type org-link-protocols) (list follow export))
  6424. (push (list type follow export) org-link-protocols)))
  6425. (defvar org-agenda-buffer-name)
  6426. ;;;###autoload
  6427. (defun org-store-link (arg)
  6428. "\\<org-mode-map>Store an org-link to the current location.
  6429. This link is added to `org-stored-links' and can later be inserted
  6430. into an org-buffer with \\[org-insert-link].
  6431. For some link types, a prefix arg is interpreted:
  6432. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6433. For file links, arg negates `org-context-in-file-links'."
  6434. (interactive "P")
  6435. (org-load-modules-maybe)
  6436. (setq org-store-link-plist nil) ; reset
  6437. (let ((outline-regexp (org-get-limited-outline-regexp))
  6438. link cpltxt desc description search txt custom-id)
  6439. (cond
  6440. ((run-hook-with-args-until-success 'org-store-link-functions)
  6441. (setq link (plist-get org-store-link-plist :link)
  6442. desc (or (plist-get org-store-link-plist :description) link)))
  6443. ((equal (buffer-name) "*Org Edit Src Example*")
  6444. (let (label gc)
  6445. (while (or (not label)
  6446. (save-excursion
  6447. (save-restriction
  6448. (widen)
  6449. (goto-char (point-min))
  6450. (re-search-forward
  6451. (regexp-quote (format org-coderef-label-format label))
  6452. nil t))))
  6453. (when label (message "Label exists already") (sit-for 2))
  6454. (setq label (read-string "Code line label: " label)))
  6455. (end-of-line 1)
  6456. (setq link (format org-coderef-label-format label))
  6457. (setq gc (- 79 (length link)))
  6458. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6459. (insert link)
  6460. (setq link (concat "(" label ")") desc nil)))
  6461. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6462. ;; We are in the agenda, link to referenced location
  6463. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6464. (get-text-property (point) 'org-marker))))
  6465. (when m
  6466. (org-with-point-at m
  6467. (call-interactively 'org-store-link)))))
  6468. ((eq major-mode 'calendar-mode)
  6469. (let ((cd (calendar-cursor-to-date)))
  6470. (setq link
  6471. (format-time-string
  6472. (car org-time-stamp-formats)
  6473. (apply 'encode-time
  6474. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6475. nil nil nil))))
  6476. (org-store-link-props :type "calendar" :date cd)))
  6477. ((eq major-mode 'w3-mode)
  6478. (setq cpltxt (if (and (buffer-name)
  6479. (not (string-match "Untitled" (buffer-name))))
  6480. (buffer-name)
  6481. (url-view-url t))
  6482. link (org-make-link (url-view-url t)))
  6483. (org-store-link-props :type "w3" :url (url-view-url t)))
  6484. ((eq major-mode 'w3m-mode)
  6485. (setq cpltxt (or w3m-current-title w3m-current-url)
  6486. link (org-make-link w3m-current-url))
  6487. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6488. ((setq search (run-hook-with-args-until-success
  6489. 'org-create-file-search-functions))
  6490. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6491. "::" search))
  6492. (setq cpltxt (or description link)))
  6493. ((eq major-mode 'image-mode)
  6494. (setq cpltxt (concat "file:"
  6495. (abbreviate-file-name buffer-file-name))
  6496. link (org-make-link cpltxt))
  6497. (org-store-link-props :type "image" :file buffer-file-name))
  6498. ((eq major-mode 'dired-mode)
  6499. ;; link to the file in the current line
  6500. (setq cpltxt (concat "file:"
  6501. (abbreviate-file-name
  6502. (expand-file-name
  6503. (dired-get-filename nil t))))
  6504. link (org-make-link cpltxt)))
  6505. ((and buffer-file-name (org-mode-p))
  6506. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6507. (cond
  6508. ((org-in-regexp "<<\\(.*?\\)>>")
  6509. (setq cpltxt
  6510. (concat "file:"
  6511. (abbreviate-file-name buffer-file-name)
  6512. "::" (match-string 1))
  6513. link (org-make-link cpltxt)))
  6514. ((and (featurep 'org-id)
  6515. (or (eq org-link-to-org-use-id t)
  6516. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6517. (interactive-p))
  6518. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6519. (interactive-p)
  6520. (not custom-id))
  6521. (and org-link-to-org-use-id
  6522. (condition-case nil
  6523. (org-entry-get nil "ID")
  6524. (error nil)))))
  6525. ;; We can make a link using the ID.
  6526. (setq link (condition-case nil
  6527. (prog1 (org-id-store-link)
  6528. (setq desc (plist-get org-store-link-plist
  6529. :description)))
  6530. (error
  6531. ;; probably before first headline, link to file only
  6532. (concat "file:"
  6533. (abbreviate-file-name buffer-file-name))))))
  6534. (t
  6535. ;; Just link to current headline
  6536. (setq cpltxt (concat "file:"
  6537. (abbreviate-file-name buffer-file-name)))
  6538. ;; Add a context search string
  6539. (when (org-xor org-context-in-file-links arg)
  6540. (setq txt (cond
  6541. ((org-on-heading-p) nil)
  6542. ((org-region-active-p)
  6543. (buffer-substring (region-beginning) (region-end)))
  6544. (t nil)))
  6545. (when (or (null txt) (string-match "\\S-" txt))
  6546. (setq cpltxt
  6547. (concat cpltxt "::"
  6548. (condition-case nil
  6549. (org-make-org-heading-search-string txt)
  6550. (error "")))
  6551. desc (or (nth 4 (ignore-errors
  6552. (org-heading-components))) "NONE"))))
  6553. (if (string-match "::\\'" cpltxt)
  6554. (setq cpltxt (substring cpltxt 0 -2)))
  6555. (setq link (org-make-link cpltxt)))))
  6556. ((buffer-file-name (buffer-base-buffer))
  6557. ;; Just link to this file here.
  6558. (setq cpltxt (concat "file:"
  6559. (abbreviate-file-name
  6560. (buffer-file-name (buffer-base-buffer)))))
  6561. ;; Add a context string
  6562. (when (org-xor org-context-in-file-links arg)
  6563. (setq txt (if (org-region-active-p)
  6564. (buffer-substring (region-beginning) (region-end))
  6565. (buffer-substring (point-at-bol) (point-at-eol))))
  6566. ;; Only use search option if there is some text.
  6567. (when (string-match "\\S-" txt)
  6568. (setq cpltxt
  6569. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6570. desc "NONE")))
  6571. (setq link (org-make-link cpltxt)))
  6572. ((interactive-p)
  6573. (error "Cannot link to a buffer which is not visiting a file"))
  6574. (t (setq link nil)))
  6575. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6576. (setq link (or link cpltxt)
  6577. desc (or desc cpltxt))
  6578. (if (equal desc "NONE") (setq desc nil))
  6579. (if (and (or (interactive-p) executing-kbd-macro) link)
  6580. (progn
  6581. (setq org-stored-links
  6582. (cons (list link desc) org-stored-links))
  6583. (message "Stored: %s" (or desc link))
  6584. (when custom-id
  6585. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6586. "::#" custom-id))
  6587. (setq org-stored-links
  6588. (cons (list link desc) org-stored-links))))
  6589. (and link (org-make-link-string link desc)))))
  6590. (defun org-store-link-props (&rest plist)
  6591. "Store link properties, extract names and addresses."
  6592. (let (x adr)
  6593. (when (setq x (plist-get plist :from))
  6594. (setq adr (mail-extract-address-components x))
  6595. (setq plist (plist-put plist :fromname (car adr)))
  6596. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6597. (when (setq x (plist-get plist :to))
  6598. (setq adr (mail-extract-address-components x))
  6599. (setq plist (plist-put plist :toname (car adr)))
  6600. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6601. (let ((from (plist-get plist :from))
  6602. (to (plist-get plist :to)))
  6603. (when (and from to org-from-is-user-regexp)
  6604. (setq plist
  6605. (plist-put plist :fromto
  6606. (if (string-match org-from-is-user-regexp from)
  6607. (concat "to %t")
  6608. (concat "from %f"))))))
  6609. (setq org-store-link-plist plist))
  6610. (defun org-add-link-props (&rest plist)
  6611. "Add these properties to the link property list."
  6612. (let (key value)
  6613. (while plist
  6614. (setq key (pop plist) value (pop plist))
  6615. (setq org-store-link-plist
  6616. (plist-put org-store-link-plist key value)))))
  6617. (defun org-email-link-description (&optional fmt)
  6618. "Return the description part of an email link.
  6619. This takes information from `org-store-link-plist' and formats it
  6620. according to FMT (default from `org-email-link-description-format')."
  6621. (setq fmt (or fmt org-email-link-description-format))
  6622. (let* ((p org-store-link-plist)
  6623. (to (plist-get p :toaddress))
  6624. (from (plist-get p :fromaddress))
  6625. (table
  6626. (list
  6627. (cons "%c" (plist-get p :fromto))
  6628. (cons "%F" (plist-get p :from))
  6629. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6630. (cons "%T" (plist-get p :to))
  6631. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6632. (cons "%s" (plist-get p :subject))
  6633. (cons "%m" (plist-get p :message-id)))))
  6634. (when (string-match "%c" fmt)
  6635. ;; Check if the user wrote this message
  6636. (if (and org-from-is-user-regexp from to
  6637. (save-match-data (string-match org-from-is-user-regexp from)))
  6638. (setq fmt (replace-match "to %t" t t fmt))
  6639. (setq fmt (replace-match "from %f" t t fmt))))
  6640. (org-replace-escapes fmt table)))
  6641. (defun org-make-org-heading-search-string (&optional string heading)
  6642. "Make search string for STRING or current headline."
  6643. (interactive)
  6644. (let ((s (or string (org-get-heading))))
  6645. (unless (and string (not heading))
  6646. ;; We are using a headline, clean up garbage in there.
  6647. (if (string-match org-todo-regexp s)
  6648. (setq s (replace-match "" t t s)))
  6649. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6650. (setq s (replace-match "" t t s)))
  6651. (setq s (org-trim s))
  6652. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6653. org-comment-string "\\)") s)
  6654. (setq s (replace-match "" t t s)))
  6655. (while (string-match org-ts-regexp s)
  6656. (setq s (replace-match "" t t s))))
  6657. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6658. (setq s (replace-match " " t t s)))
  6659. (or string (setq s (concat "*" s))) ; Add * for headlines
  6660. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6661. (defun org-make-link (&rest strings)
  6662. "Concatenate STRINGS."
  6663. (apply 'concat strings))
  6664. (defun org-make-link-string (link &optional description)
  6665. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6666. (unless (string-match "\\S-" link)
  6667. (error "Empty link"))
  6668. (when (stringp description)
  6669. ;; Remove brackets from the description, they are fatal.
  6670. (while (string-match "\\[" description)
  6671. (setq description (replace-match "{" t t description)))
  6672. (while (string-match "\\]" description)
  6673. (setq description (replace-match "}" t t description))))
  6674. (when (equal (org-link-escape link) description)
  6675. ;; No description needed, it is identical
  6676. (setq description nil))
  6677. (when (and (not description)
  6678. (not (equal link (org-link-escape link))))
  6679. (setq description (org-extract-attributes link)))
  6680. (concat "[[" (org-link-escape link) "]"
  6681. (if description (concat "[" description "]") "")
  6682. "]"))
  6683. (defconst org-link-escape-chars
  6684. '((?\ . "%20")
  6685. (?\[ . "%5B")
  6686. (?\] . "%5D")
  6687. (?\340 . "%E0") ; `a
  6688. (?\342 . "%E2") ; ^a
  6689. (?\347 . "%E7") ; ,c
  6690. (?\350 . "%E8") ; `e
  6691. (?\351 . "%E9") ; 'e
  6692. (?\352 . "%EA") ; ^e
  6693. (?\356 . "%EE") ; ^i
  6694. (?\364 . "%F4") ; ^o
  6695. (?\371 . "%F9") ; `u
  6696. (?\373 . "%FB") ; ^u
  6697. (?\; . "%3B")
  6698. (?? . "%3F")
  6699. (?= . "%3D")
  6700. (?+ . "%2B")
  6701. )
  6702. "Association list of escapes for some characters problematic in links.
  6703. This is the list that is used for internal purposes.")
  6704. (defvar org-url-encoding-use-url-hexify nil)
  6705. (defconst org-link-escape-chars-browser
  6706. '((?\ . "%20")) ; 32 for the SPC char
  6707. "Association list of escapes for some characters problematic in links.
  6708. This is the list that is used before handing over to the browser.")
  6709. (defun org-link-escape (text &optional table)
  6710. "Escape characters in TEXT that are problematic for links."
  6711. (if org-url-encoding-use-url-hexify
  6712. (url-hexify-string text)
  6713. (setq table (or table org-link-escape-chars))
  6714. (when text
  6715. (let ((re (mapconcat (lambda (x) (regexp-quote
  6716. (char-to-string (car x))))
  6717. table "\\|")))
  6718. (while (string-match re text)
  6719. (setq text
  6720. (replace-match
  6721. (cdr (assoc (string-to-char (match-string 0 text))
  6722. table))
  6723. t t text)))
  6724. text))))
  6725. (defun org-link-unescape (text &optional table)
  6726. "Reverse the action of `org-link-escape'."
  6727. (if org-url-encoding-use-url-hexify
  6728. (url-unhex-string text)
  6729. (setq table (or table org-link-escape-chars))
  6730. (when text
  6731. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6732. table "\\|")))
  6733. (while (string-match re text)
  6734. (setq text
  6735. (replace-match
  6736. (char-to-string (car (rassoc (match-string 0 text) table)))
  6737. t t text)))
  6738. text))))
  6739. (defun org-xor (a b)
  6740. "Exclusive or."
  6741. (if a (not b) b))
  6742. (defun org-fixup-message-id-for-http (s)
  6743. "Replace special characters in a message id, so it can be used in an http query."
  6744. (while (string-match "<" s)
  6745. (setq s (replace-match "%3C" t t s)))
  6746. (while (string-match ">" s)
  6747. (setq s (replace-match "%3E" t t s)))
  6748. (while (string-match "@" s)
  6749. (setq s (replace-match "%40" t t s)))
  6750. s)
  6751. ;;;###autoload
  6752. (defun org-insert-link-global ()
  6753. "Insert a link like Org-mode does.
  6754. This command can be called in any mode to insert a link in Org-mode syntax."
  6755. (interactive)
  6756. (org-load-modules-maybe)
  6757. (org-run-like-in-org-mode 'org-insert-link))
  6758. (defun org-insert-link (&optional complete-file link-location)
  6759. "Insert a link. At the prompt, enter the link.
  6760. Completion can be used to insert any of the link protocol prefixes like
  6761. http or ftp in use.
  6762. The history can be used to select a link previously stored with
  6763. `org-store-link'. When the empty string is entered (i.e. if you just
  6764. press RET at the prompt), the link defaults to the most recently
  6765. stored link. As SPC triggers completion in the minibuffer, you need to
  6766. use M-SPC or C-q SPC to force the insertion of a space character.
  6767. You will also be prompted for a description, and if one is given, it will
  6768. be displayed in the buffer instead of the link.
  6769. If there is already a link at point, this command will allow you to edit link
  6770. and description parts.
  6771. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6772. be selected using completion. The path to the file will be relative to the
  6773. current directory if the file is in the current directory or a subdirectory.
  6774. Otherwise, the link will be the absolute path as completed in the minibuffer
  6775. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6776. option `org-link-file-path-type'.
  6777. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6778. the current directory or below.
  6779. With three \\[universal-argument] prefixes, negate the meaning of
  6780. `org-keep-stored-link-after-insertion'.
  6781. If `org-make-link-description-function' is non-nil, this function will be
  6782. called with the link target, and the result will be the default
  6783. link description.
  6784. If the LINK-LOCATION parameter is non-nil, this value will be
  6785. used as the link location instead of reading one interactively."
  6786. (interactive "P")
  6787. (let* ((wcf (current-window-configuration))
  6788. (region (if (org-region-active-p)
  6789. (buffer-substring (region-beginning) (region-end))))
  6790. (remove (and region (list (region-beginning) (region-end))))
  6791. (desc region)
  6792. tmphist ; byte-compile incorrectly complains about this
  6793. (link link-location)
  6794. entry file all-prefixes)
  6795. (cond
  6796. (link-location) ; specified by arg, just use it.
  6797. ((org-in-regexp org-bracket-link-regexp 1)
  6798. ;; We do have a link at point, and we are going to edit it.
  6799. (setq remove (list (match-beginning 0) (match-end 0)))
  6800. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6801. (setq link (read-string "Link: "
  6802. (org-link-unescape
  6803. (org-match-string-no-properties 1)))))
  6804. ((or (org-in-regexp org-angle-link-re)
  6805. (org-in-regexp org-plain-link-re))
  6806. ;; Convert to bracket link
  6807. (setq remove (list (match-beginning 0) (match-end 0))
  6808. link (read-string "Link: "
  6809. (org-remove-angle-brackets (match-string 0)))))
  6810. ((member complete-file '((4) (16)))
  6811. ;; Completing read for file names.
  6812. (setq link (org-file-complete-link complete-file)))
  6813. (t
  6814. ;; Read link, with completion for stored links.
  6815. (with-output-to-temp-buffer "*Org Links*"
  6816. (princ "Insert a link.
  6817. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6818. (when org-stored-links
  6819. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6820. (princ (mapconcat
  6821. (lambda (x)
  6822. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6823. (reverse org-stored-links) "\n"))))
  6824. (let ((cw (selected-window)))
  6825. (select-window (get-buffer-window "*Org Links*"))
  6826. (setq truncate-lines t)
  6827. (unless (pos-visible-in-window-p (point-max))
  6828. (org-fit-window-to-buffer))
  6829. (and (window-live-p cw) (select-window cw)))
  6830. ;; Fake a link history, containing the stored links.
  6831. (setq tmphist (append (mapcar 'car org-stored-links)
  6832. org-insert-link-history))
  6833. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6834. (mapcar 'car org-link-abbrev-alist)
  6835. org-link-types))
  6836. (unwind-protect
  6837. (progn
  6838. (setq link
  6839. (let ((org-completion-use-ido nil)
  6840. (org-completion-use-iswitchb nil))
  6841. (org-completing-read
  6842. "Link: "
  6843. (append
  6844. (mapcar (lambda (x) (list (concat x ":")))
  6845. all-prefixes)
  6846. (mapcar 'car org-stored-links))
  6847. nil nil nil
  6848. 'tmphist
  6849. (car (car org-stored-links)))))
  6850. (if (or (member link all-prefixes)
  6851. (and (equal ":" (substring link -1))
  6852. (member (substring link 0 -1) all-prefixes)
  6853. (setq link (substring link 0 -1))))
  6854. (setq link (org-link-try-special-completion link))))
  6855. (set-window-configuration wcf)
  6856. (kill-buffer "*Org Links*"))
  6857. (setq entry (assoc link org-stored-links))
  6858. (or entry (push link org-insert-link-history))
  6859. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6860. (not org-keep-stored-link-after-insertion))
  6861. (setq org-stored-links (delq (assoc link org-stored-links)
  6862. org-stored-links)))
  6863. (setq desc (or desc (nth 1 entry)))))
  6864. (if (string-match org-plain-link-re link)
  6865. ;; URL-like link, normalize the use of angular brackets.
  6866. (setq link (org-make-link (org-remove-angle-brackets link))))
  6867. ;; Check if we are linking to the current file with a search option
  6868. ;; If yes, simplify the link by using only the search option.
  6869. (when (and buffer-file-name
  6870. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6871. (let* ((path (match-string 1 link))
  6872. (case-fold-search nil)
  6873. (search (match-string 2 link)))
  6874. (save-match-data
  6875. (if (equal (file-truename buffer-file-name) (file-truename path))
  6876. ;; We are linking to this same file, with a search option
  6877. (setq link search)))))
  6878. ;; Check if we can/should use a relative path. If yes, simplify the link
  6879. (when (string-match "^file:\\(.*\\)" link)
  6880. (let* ((path (match-string 1 link))
  6881. (origpath path)
  6882. (case-fold-search nil))
  6883. (cond
  6884. ((or (eq org-link-file-path-type 'absolute)
  6885. (equal complete-file '(16)))
  6886. (setq path (abbreviate-file-name (expand-file-name path))))
  6887. ((eq org-link-file-path-type 'noabbrev)
  6888. (setq path (expand-file-name path)))
  6889. ((eq org-link-file-path-type 'relative)
  6890. (setq path (file-relative-name path)))
  6891. (t
  6892. (save-match-data
  6893. (if (string-match (concat "^" (regexp-quote
  6894. (file-name-as-directory
  6895. (expand-file-name "."))))
  6896. (expand-file-name path))
  6897. ;; We are linking a file with relative path name.
  6898. (setq path (substring (expand-file-name path)
  6899. (match-end 0)))
  6900. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6901. (setq link (concat "file:" path))
  6902. (if (equal desc origpath)
  6903. (setq desc path))))
  6904. (if org-make-link-description-function
  6905. (setq desc (funcall org-make-link-description-function link desc)))
  6906. (setq desc (read-string "Description: " desc))
  6907. (unless (string-match "\\S-" desc) (setq desc nil))
  6908. (if remove (apply 'delete-region remove))
  6909. (insert (org-make-link-string link desc))))
  6910. (defun org-link-try-special-completion (type)
  6911. "If there is completion support for link type TYPE, offer it."
  6912. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6913. (if (functionp fun)
  6914. (funcall fun)
  6915. (read-string "Link (no completion support): " (concat type ":")))))
  6916. (defun org-file-complete-link (&optional arg)
  6917. "Create a file link using completion."
  6918. (let (file link)
  6919. (setq file (read-file-name "File: "))
  6920. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6921. (pwd1 (file-name-as-directory (abbreviate-file-name
  6922. (expand-file-name ".")))))
  6923. (cond
  6924. ((equal arg '(16))
  6925. (setq link (org-make-link
  6926. "file:"
  6927. (abbreviate-file-name (expand-file-name file)))))
  6928. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6929. (setq link (org-make-link "file:" (match-string 1 file))))
  6930. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6931. (expand-file-name file))
  6932. (setq link (org-make-link
  6933. "file:" (match-string 1 (expand-file-name file)))))
  6934. (t (setq link (org-make-link "file:" file)))))
  6935. link))
  6936. (defun org-completing-read (&rest args)
  6937. "Completing-read with SPACE being a normal character."
  6938. (let ((minibuffer-local-completion-map
  6939. (copy-keymap minibuffer-local-completion-map)))
  6940. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6941. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6942. (apply 'org-icompleting-read args)))
  6943. (defun org-completing-read-no-i (&rest args)
  6944. (let (org-completion-use-ido org-completion-use-iswitchb)
  6945. (apply 'org-completing-read args)))
  6946. (defun org-iswitchb-completing-read (prompt choices &rest args)
  6947. "Use iswitch as a completing-read replacement to choose from choices.
  6948. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  6949. from."
  6950. (let* ((iswitchb-use-virtual-buffers nil)
  6951. (iswitchb-make-buflist-hook
  6952. (lambda ()
  6953. (setq iswitchb-temp-buflist choices))))
  6954. (iswitchb-read-buffer prompt)))
  6955. (defun org-icompleting-read (&rest args)
  6956. "Completing-read using `ido-mode' or `iswitchb' speedups if available"
  6957. (if (and org-completion-use-ido
  6958. (fboundp 'ido-completing-read)
  6959. (boundp 'ido-mode) ido-mode
  6960. (listp (second args)))
  6961. (let ((ido-enter-matching-directory nil))
  6962. (apply 'ido-completing-read (concat (car args))
  6963. (if (consp (car (nth 1 args)))
  6964. (mapcar (lambda (x) (car x)) (nth 1 args))
  6965. (nth 1 args))
  6966. (cddr args)))
  6967. (if (and org-completion-use-iswitchb
  6968. (boundp 'iswitchb-mode) iswitchb-mode
  6969. (listp (second args)))
  6970. (apply 'org-iswitchb-completing-read (concat (car args))
  6971. (mapcar (lambda (x) (car x)) (nth 1 args))
  6972. (cddr args))
  6973. (apply 'completing-read args))))
  6974. (defun org-extract-attributes (s)
  6975. "Extract the attributes cookie from a string and set as text property."
  6976. (let (a attr (start 0) key value)
  6977. (save-match-data
  6978. (when (string-match "{{\\([^}]+\\)}}$" s)
  6979. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6980. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6981. (setq key (match-string 1 a) value (match-string 2 a)
  6982. start (match-end 0)
  6983. attr (plist-put attr (intern key) value))))
  6984. (org-add-props s nil 'org-attr attr))
  6985. s))
  6986. (defun org-extract-attributes-from-string (tag)
  6987. (let (key value attr)
  6988. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6989. (setq key (match-string 1 tag) value (match-string 2 tag)
  6990. tag (replace-match "" t t tag)
  6991. attr (plist-put attr (intern key) value)))
  6992. (cons tag attr)))
  6993. (defun org-attributes-to-string (plist)
  6994. "Format a property list into an HTML attribute list."
  6995. (let ((s "") key value)
  6996. (while plist
  6997. (setq key (pop plist) value (pop plist))
  6998. (and value
  6999. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7000. s))
  7001. ;;; Opening/following a link
  7002. (defvar org-link-search-failed nil)
  7003. (defun org-next-link ()
  7004. "Move forward to the next link.
  7005. If the link is in hidden text, expose it."
  7006. (interactive)
  7007. (when (and org-link-search-failed (eq this-command last-command))
  7008. (goto-char (point-min))
  7009. (message "Link search wrapped back to beginning of buffer"))
  7010. (setq org-link-search-failed nil)
  7011. (let* ((pos (point))
  7012. (ct (org-context))
  7013. (a (assoc :link ct)))
  7014. (if a (goto-char (nth 2 a)))
  7015. (if (re-search-forward org-any-link-re nil t)
  7016. (progn
  7017. (goto-char (match-beginning 0))
  7018. (if (org-invisible-p) (org-show-context)))
  7019. (goto-char pos)
  7020. (setq org-link-search-failed t)
  7021. (error "No further link found"))))
  7022. (defun org-previous-link ()
  7023. "Move backward to the previous link.
  7024. If the link is in hidden text, expose it."
  7025. (interactive)
  7026. (when (and org-link-search-failed (eq this-command last-command))
  7027. (goto-char (point-max))
  7028. (message "Link search wrapped back to end of buffer"))
  7029. (setq org-link-search-failed nil)
  7030. (let* ((pos (point))
  7031. (ct (org-context))
  7032. (a (assoc :link ct)))
  7033. (if a (goto-char (nth 1 a)))
  7034. (if (re-search-backward org-any-link-re nil t)
  7035. (progn
  7036. (goto-char (match-beginning 0))
  7037. (if (org-invisible-p) (org-show-context)))
  7038. (goto-char pos)
  7039. (setq org-link-search-failed t)
  7040. (error "No further link found"))))
  7041. (defun org-translate-link (s)
  7042. "Translate a link string if a translation function has been defined."
  7043. (if (and org-link-translation-function
  7044. (fboundp org-link-translation-function)
  7045. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7046. (progn
  7047. (setq s (funcall org-link-translation-function
  7048. (match-string 1) (match-string 2)))
  7049. (concat (car s) ":" (cdr s)))
  7050. s))
  7051. (defun org-translate-link-from-planner (type path)
  7052. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7053. This is still an experimental function, your mileage may vary."
  7054. (cond
  7055. ((member type '("http" "https" "news" "ftp"))
  7056. ;; standard Internet links are the same.
  7057. nil)
  7058. ((and (equal type "irc") (string-match "^//" path))
  7059. ;; Planner has two / at the beginning of an irc link, we have 1.
  7060. ;; We should have zero, actually....
  7061. (setq path (substring path 1)))
  7062. ((and (equal type "lisp") (string-match "^/" path))
  7063. ;; Planner has a slash, we do not.
  7064. (setq type "elisp" path (substring path 1)))
  7065. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7066. ;; A typical message link. Planner has the id after the fina slash,
  7067. ;; we separate it with a hash mark
  7068. (setq path (concat (match-string 1 path) "#"
  7069. (org-remove-angle-brackets (match-string 2 path)))))
  7070. )
  7071. (cons type path))
  7072. (defun org-find-file-at-mouse (ev)
  7073. "Open file link or URL at mouse."
  7074. (interactive "e")
  7075. (mouse-set-point ev)
  7076. (org-open-at-point 'in-emacs))
  7077. (defun org-open-at-mouse (ev)
  7078. "Open file link or URL at mouse."
  7079. (interactive "e")
  7080. (mouse-set-point ev)
  7081. (if (eq major-mode 'org-agenda-mode)
  7082. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7083. (org-open-at-point))
  7084. (defvar org-window-config-before-follow-link nil
  7085. "The window configuration before following a link.
  7086. This is saved in case the need arises to restore it.")
  7087. (defvar org-open-link-marker (make-marker)
  7088. "Marker pointing to the location where `org-open-at-point; was called.")
  7089. ;;;###autoload
  7090. (defun org-open-at-point-global ()
  7091. "Follow a link like Org-mode does.
  7092. This command can be called in any mode to follow a link that has
  7093. Org-mode syntax."
  7094. (interactive)
  7095. (org-run-like-in-org-mode 'org-open-at-point))
  7096. ;;;###autoload
  7097. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7098. "Open a link in the string S, as if it was in Org-mode."
  7099. (interactive "sLink: \nP")
  7100. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7101. (with-temp-buffer
  7102. (let ((org-inhibit-startup t))
  7103. (org-mode)
  7104. (insert s)
  7105. (goto-char (point-min))
  7106. (org-open-at-point arg reference-buffer)))))
  7107. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7108. "Open link at or after point.
  7109. If there is no link at point, this function will search forward up to
  7110. the end of the current line.
  7111. Normally, files will be opened by an appropriate application. If the
  7112. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7113. With a double prefix argument, try to open outside of Emacs, in the
  7114. application the system uses for this file type."
  7115. (interactive "P")
  7116. (org-load-modules-maybe)
  7117. (move-marker org-open-link-marker (point))
  7118. (setq org-window-config-before-follow-link (current-window-configuration))
  7119. (org-remove-occur-highlights nil nil t)
  7120. (cond
  7121. ((and (org-on-heading-p)
  7122. (not (org-in-regexp
  7123. (concat org-plain-link-re "\\|"
  7124. org-bracket-link-regexp "\\|"
  7125. org-angle-link-re "\\|"
  7126. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7127. (org-offer-links-in-entry in-emacs))
  7128. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7129. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7130. (org-footnote-action))
  7131. (t
  7132. (let (type path link line search (pos (point)))
  7133. (catch 'match
  7134. (save-excursion
  7135. (skip-chars-forward "^]\n\r")
  7136. (when (org-in-regexp org-bracket-link-regexp)
  7137. (setq link (org-extract-attributes
  7138. (org-link-unescape (org-match-string-no-properties 1))))
  7139. (while (string-match " *\n *" link)
  7140. (setq link (replace-match " " t t link)))
  7141. (setq link (org-link-expand-abbrev link))
  7142. (cond
  7143. ((or (file-name-absolute-p link)
  7144. (string-match "^\\.\\.?/" link))
  7145. (setq type "file" path link))
  7146. ((string-match org-link-re-with-space3 link)
  7147. (setq type (match-string 1 link) path (match-string 2 link)))
  7148. (t (setq type "thisfile" path link)))
  7149. (throw 'match t)))
  7150. (when (get-text-property (point) 'org-linked-text)
  7151. (setq type "thisfile"
  7152. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7153. (1+ (point)) (point))
  7154. path (buffer-substring
  7155. (previous-single-property-change pos 'org-linked-text)
  7156. (next-single-property-change pos 'org-linked-text)))
  7157. (throw 'match t))
  7158. (save-excursion
  7159. (when (or (org-in-regexp org-angle-link-re)
  7160. (org-in-regexp org-plain-link-re))
  7161. (setq type (match-string 1) path (match-string 2))
  7162. (throw 'match t)))
  7163. (save-excursion
  7164. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7165. (setq type "tags"
  7166. path (match-string 1))
  7167. (while (string-match ":" path)
  7168. (setq path (replace-match "+" t t path)))
  7169. (throw 'match t)))
  7170. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7171. (setq type "tree-match"
  7172. path (match-string 1))
  7173. (throw 'match t)))
  7174. (unless path
  7175. (error "No link found"))
  7176. ;; switch back to reference buffer
  7177. ;; needed when if called in a temporary buffer through
  7178. ;; org-open-link-from-string
  7179. (with-current-buffer (or reference-buffer (current-buffer))
  7180. ;; Remove any trailing spaces in path
  7181. (if (string-match " +\\'" path)
  7182. (setq path (replace-match "" t t path)))
  7183. (if (and org-link-translation-function
  7184. (fboundp org-link-translation-function))
  7185. ;; Check if we need to translate the link
  7186. (let ((tmp (funcall org-link-translation-function type path)))
  7187. (setq type (car tmp) path (cdr tmp))))
  7188. (cond
  7189. ((assoc type org-link-protocols)
  7190. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7191. ((equal type "mailto")
  7192. (let ((cmd (car org-link-mailto-program))
  7193. (args (cdr org-link-mailto-program)) args1
  7194. (address path) (subject "") a)
  7195. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7196. (setq address (match-string 1 path)
  7197. subject (org-link-escape (match-string 2 path))))
  7198. (while args
  7199. (cond
  7200. ((not (stringp (car args))) (push (pop args) args1))
  7201. (t (setq a (pop args))
  7202. (if (string-match "%a" a)
  7203. (setq a (replace-match address t t a)))
  7204. (if (string-match "%s" a)
  7205. (setq a (replace-match subject t t a)))
  7206. (push a args1))))
  7207. (apply cmd (nreverse args1))))
  7208. ((member type '("http" "https" "ftp" "news"))
  7209. (browse-url (concat type ":" (org-link-escape
  7210. path org-link-escape-chars-browser))))
  7211. ((member type '("message"))
  7212. (browse-url (concat type ":" path)))
  7213. ((string= type "tags")
  7214. (org-tags-view in-emacs path))
  7215. ((string= type "thisfile")
  7216. (if in-emacs
  7217. (switch-to-buffer-other-window
  7218. (org-get-buffer-for-internal-link (current-buffer)))
  7219. (org-mark-ring-push))
  7220. (let ((cmd `(org-link-search
  7221. ,path
  7222. ,(cond ((equal in-emacs '(4)) 'occur)
  7223. ((equal in-emacs '(16)) 'org-occur)
  7224. (t nil))
  7225. ,pos)))
  7226. (condition-case nil (eval cmd)
  7227. (error (progn (widen) (eval cmd))))))
  7228. ((string= type "tree-match")
  7229. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7230. ((string= type "file")
  7231. (if (string-match "::\\([0-9]+\\)\\'" path)
  7232. (setq line (string-to-number (match-string 1 path))
  7233. path (substring path 0 (match-beginning 0)))
  7234. (if (string-match "::\\(.+\\)\\'" path)
  7235. (setq search (match-string 1 path)
  7236. path (substring path 0 (match-beginning 0)))))
  7237. (if (string-match "[*?{]" (file-name-nondirectory path))
  7238. (dired path)
  7239. (org-open-file path in-emacs line search)))
  7240. ((string= type "news")
  7241. (require 'org-gnus)
  7242. (org-gnus-follow-link path))
  7243. ((string= type "shell")
  7244. (let ((cmd path))
  7245. (if (or (not org-confirm-shell-link-function)
  7246. (funcall org-confirm-shell-link-function
  7247. (format "Execute \"%s\" in shell? "
  7248. (org-add-props cmd nil
  7249. 'face 'org-warning))))
  7250. (progn
  7251. (message "Executing %s" cmd)
  7252. (shell-command cmd))
  7253. (error "Abort"))))
  7254. ((string= type "elisp")
  7255. (let ((cmd path))
  7256. (if (or (not org-confirm-elisp-link-function)
  7257. (funcall org-confirm-elisp-link-function
  7258. (format "Execute \"%s\" as elisp? "
  7259. (org-add-props cmd nil
  7260. 'face 'org-warning))))
  7261. (message "%s => %s" cmd
  7262. (if (equal (string-to-char cmd) ?\()
  7263. (eval (read cmd))
  7264. (call-interactively (read cmd))))
  7265. (error "Abort"))))
  7266. (t
  7267. (browse-url-at-point))))))
  7268. (move-marker org-open-link-marker nil)
  7269. (run-hook-with-args 'org-follow-link-hook)))
  7270. (defun org-offer-links-in-entry (&optional nth zero)
  7271. "Offer links in the curren entry and follow the selected link.
  7272. If there is only one link, follow it immediately as well.
  7273. If NTH is an integer, immediately pick the NTH link found.
  7274. If ZERO is a string, check also this string for a link, and if
  7275. there is one, offer it as link number zero."
  7276. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7277. "\\(" org-angle-link-re "\\)\\|"
  7278. "\\(" org-plain-link-re "\\)"))
  7279. (cnt ?0)
  7280. (in-emacs (if (integerp nth) nil nth))
  7281. have-zero end links link c)
  7282. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7283. (push (match-string 0 zero) links)
  7284. (setq cnt (1- cnt) have-zero t))
  7285. (save-excursion
  7286. (org-back-to-heading t)
  7287. (setq end (save-excursion (outline-next-heading) (point)))
  7288. (while (re-search-forward re end t)
  7289. (push (match-string 0) links))
  7290. (setq links (org-uniquify (reverse links))))
  7291. (cond
  7292. ((null links) (error "No links"))
  7293. ((equal (length links) 1)
  7294. (setq link (car links)))
  7295. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7296. (setq link (nth (if have-zero nth (1- nth)) links)))
  7297. (t ; we have to select a link
  7298. (save-excursion
  7299. (save-window-excursion
  7300. (delete-other-windows)
  7301. (with-output-to-temp-buffer "*Select Link*"
  7302. (mapc (lambda (l)
  7303. (if (not (string-match org-bracket-link-regexp l))
  7304. (princ (format "[%c] %s\n" (incf cnt)
  7305. (org-remove-angle-brackets l)))
  7306. (if (match-end 3)
  7307. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7308. (match-string 3 l) (match-string 1 l)))
  7309. (princ (format "[%c] %s\n" (incf cnt)
  7310. (match-string 1 l))))))
  7311. links))
  7312. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7313. (message "Select link to open:")
  7314. (setq c (read-char-exclusive))
  7315. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7316. (when (equal c ?q) (error "Abort"))
  7317. (setq nth (- c ?0))
  7318. (if have-zero (setq nth (1+ nth)))
  7319. (unless (and (integerp nth) (>= (length links) nth))
  7320. (error "Invalid link selection"))
  7321. (setq link (nth (1- nth) links))))
  7322. (org-open-link-from-string link in-emacs (current-buffer))))
  7323. ;;;; Time estimates
  7324. (defun org-get-effort (&optional pom)
  7325. "Get the effort estimate for the current entry."
  7326. (org-entry-get pom org-effort-property))
  7327. ;;; File search
  7328. (defvar org-create-file-search-functions nil
  7329. "List of functions to construct the right search string for a file link.
  7330. These functions are called in turn with point at the location to
  7331. which the link should point.
  7332. A function in the hook should first test if it would like to
  7333. handle this file type, for example by checking the major-mode or
  7334. the file extension. If it decides not to handle this file, it
  7335. should just return nil to give other functions a chance. If it
  7336. does handle the file, it must return the search string to be used
  7337. when following the link. The search string will be part of the
  7338. file link, given after a double colon, and `org-open-at-point'
  7339. will automatically search for it. If special measures must be
  7340. taken to make the search successful, another function should be
  7341. added to the companion hook `org-execute-file-search-functions',
  7342. which see.
  7343. A function in this hook may also use `setq' to set the variable
  7344. `description' to provide a suggestion for the descriptive text to
  7345. be used for this link when it gets inserted into an Org-mode
  7346. buffer with \\[org-insert-link].")
  7347. (defvar org-execute-file-search-functions nil
  7348. "List of functions to execute a file search triggered by a link.
  7349. Functions added to this hook must accept a single argument, the
  7350. search string that was part of the file link, the part after the
  7351. double colon. The function must first check if it would like to
  7352. handle this search, for example by checking the major-mode or the
  7353. file extension. If it decides not to handle this search, it
  7354. should just return nil to give other functions a chance. If it
  7355. does handle the search, it must return a non-nil value to keep
  7356. other functions from trying.
  7357. Each function can access the current prefix argument through the
  7358. variable `current-prefix-argument'. Note that a single prefix is
  7359. used to force opening a link in Emacs, so it may be good to only
  7360. use a numeric or double prefix to guide the search function.
  7361. In case this is needed, a function in this hook can also restore
  7362. the window configuration before `org-open-at-point' was called using:
  7363. (set-window-configuration org-window-config-before-follow-link)")
  7364. (defun org-link-search (s &optional type avoid-pos)
  7365. "Search for a link search option.
  7366. If S is surrounded by forward slashes, it is interpreted as a
  7367. regular expression. In org-mode files, this will create an `org-occur'
  7368. sparse tree. In ordinary files, `occur' will be used to list matches.
  7369. If the current buffer is in `dired-mode', grep will be used to search
  7370. in all files. If AVOID-POS is given, ignore matches near that position."
  7371. (let ((case-fold-search t)
  7372. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7373. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7374. (append '(("") (" ") ("\t") ("\n"))
  7375. org-emphasis-alist)
  7376. "\\|") "\\)"))
  7377. (pos (point))
  7378. (pre nil) (post nil)
  7379. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7380. (cond
  7381. ;; First check if there are any special
  7382. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7383. ;; Now try the builtin stuff
  7384. ((and (equal (string-to-char s0) ?#)
  7385. (> (length s0) 1)
  7386. (save-excursion
  7387. (goto-char (point-min))
  7388. (and
  7389. (re-search-forward
  7390. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7391. (setq type 'dedicated
  7392. pos (match-beginning 0))))
  7393. ;; There is an exact target for this
  7394. (goto-char pos)
  7395. (org-back-to-heading t)))
  7396. ((save-excursion
  7397. (goto-char (point-min))
  7398. (and
  7399. (re-search-forward
  7400. (concat "<<" (regexp-quote s0) ">>") nil t)
  7401. (setq type 'dedicated
  7402. pos (match-beginning 0))))
  7403. ;; There is an exact target for this
  7404. (goto-char pos))
  7405. ((and (string-match "^(\\(.*\\))$" s0)
  7406. (save-excursion
  7407. (goto-char (point-min))
  7408. (and
  7409. (re-search-forward
  7410. (concat "[^[]" (regexp-quote
  7411. (format org-coderef-label-format
  7412. (match-string 1 s0))))
  7413. nil t)
  7414. (setq type 'dedicated
  7415. pos (1+ (match-beginning 0))))))
  7416. ;; There is a coderef target for this
  7417. (goto-char pos))
  7418. ((string-match "^/\\(.*\\)/$" s)
  7419. ;; A regular expression
  7420. (cond
  7421. ((org-mode-p)
  7422. (org-occur (match-string 1 s)))
  7423. ;;((eq major-mode 'dired-mode)
  7424. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7425. (t (org-do-occur (match-string 1 s)))))
  7426. (t
  7427. ;; A normal search strings
  7428. (when (equal (string-to-char s) ?*)
  7429. ;; Anchor on headlines, post may include tags.
  7430. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7431. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7432. s (substring s 1)))
  7433. (remove-text-properties
  7434. 0 (length s)
  7435. '(face nil mouse-face nil keymap nil fontified nil) s)
  7436. ;; Make a series of regular expressions to find a match
  7437. (setq words (org-split-string s "[ \n\r\t]+")
  7438. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7439. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7440. "\\)" markers)
  7441. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7442. re2a (concat "[ \t\r\n]" re2a_)
  7443. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7444. re4 (concat "[^a-zA-Z_]" re4_)
  7445. re1 (concat pre re2 post)
  7446. re3 (concat pre (if pre re4_ re4) post)
  7447. re5 (concat pre ".*" re4)
  7448. re2 (concat pre re2)
  7449. re2a (concat pre (if pre re2a_ re2a))
  7450. re4 (concat pre (if pre re4_ re4))
  7451. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7452. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7453. re5 "\\)"
  7454. ))
  7455. (cond
  7456. ((eq type 'org-occur) (org-occur reall))
  7457. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7458. (t (goto-char (point-min))
  7459. (setq type 'fuzzy)
  7460. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7461. (org-search-not-self 1 re1 nil t)
  7462. (org-search-not-self 1 re2 nil t)
  7463. (org-search-not-self 1 re2a nil t)
  7464. (org-search-not-self 1 re3 nil t)
  7465. (org-search-not-self 1 re4 nil t)
  7466. (org-search-not-self 1 re5 nil t)
  7467. )
  7468. (goto-char (match-beginning 1))
  7469. (goto-char pos)
  7470. (error "No match")))))
  7471. (t
  7472. ;; Normal string-search
  7473. (goto-char (point-min))
  7474. (if (search-forward s nil t)
  7475. (goto-char (match-beginning 0))
  7476. (error "No match"))))
  7477. (and (org-mode-p) (org-show-context 'link-search))
  7478. type))
  7479. (defun org-search-not-self (group &rest args)
  7480. "Execute `re-search-forward', but only accept matches that do not
  7481. enclose the position of `org-open-link-marker'."
  7482. (let ((m org-open-link-marker))
  7483. (catch 'exit
  7484. (while (apply 're-search-forward args)
  7485. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7486. (goto-char (match-end group))
  7487. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7488. (> (match-beginning 0) (marker-position m))
  7489. (< (match-end 0) (marker-position m)))
  7490. (save-match-data
  7491. (or (not (org-in-regexp
  7492. org-bracket-link-analytic-regexp 1))
  7493. (not (match-end 4)) ; no description
  7494. (and (<= (match-beginning 4) (point))
  7495. (>= (match-end 4) (point))))))
  7496. (throw 'exit (point))))))))
  7497. (defun org-get-buffer-for-internal-link (buffer)
  7498. "Return a buffer to be used for displaying the link target of internal links."
  7499. (cond
  7500. ((not org-display-internal-link-with-indirect-buffer)
  7501. buffer)
  7502. ((string-match "(Clone)$" (buffer-name buffer))
  7503. (message "Buffer is already a clone, not making another one")
  7504. ;; we also do not modify visibility in this case
  7505. buffer)
  7506. (t ; make a new indirect buffer for displaying the link
  7507. (let* ((bn (buffer-name buffer))
  7508. (ibn (concat bn "(Clone)"))
  7509. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7510. (with-current-buffer ib (org-overview))
  7511. ib))))
  7512. (defun org-do-occur (regexp &optional cleanup)
  7513. "Call the Emacs command `occur'.
  7514. If CLEANUP is non-nil, remove the printout of the regular expression
  7515. in the *Occur* buffer. This is useful if the regex is long and not useful
  7516. to read."
  7517. (occur regexp)
  7518. (when cleanup
  7519. (let ((cwin (selected-window)) win beg end)
  7520. (when (setq win (get-buffer-window "*Occur*"))
  7521. (select-window win))
  7522. (goto-char (point-min))
  7523. (when (re-search-forward "match[a-z]+" nil t)
  7524. (setq beg (match-end 0))
  7525. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7526. (setq end (1- (match-beginning 0)))))
  7527. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7528. (goto-char (point-min))
  7529. (select-window cwin))))
  7530. ;;; The mark ring for links jumps
  7531. (defvar org-mark-ring nil
  7532. "Mark ring for positions before jumps in Org-mode.")
  7533. (defvar org-mark-ring-last-goto nil
  7534. "Last position in the mark ring used to go back.")
  7535. ;; Fill and close the ring
  7536. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7537. (loop for i from 1 to org-mark-ring-length do
  7538. (push (make-marker) org-mark-ring))
  7539. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7540. org-mark-ring)
  7541. (defun org-mark-ring-push (&optional pos buffer)
  7542. "Put the current position or POS into the mark ring and rotate it."
  7543. (interactive)
  7544. (setq pos (or pos (point)))
  7545. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7546. (move-marker (car org-mark-ring)
  7547. (or pos (point))
  7548. (or buffer (current-buffer)))
  7549. (message "%s"
  7550. (substitute-command-keys
  7551. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7552. (defun org-mark-ring-goto (&optional n)
  7553. "Jump to the previous position in the mark ring.
  7554. With prefix arg N, jump back that many stored positions. When
  7555. called several times in succession, walk through the entire ring.
  7556. Org-mode commands jumping to a different position in the current file,
  7557. or to another Org-mode file, automatically push the old position
  7558. onto the ring."
  7559. (interactive "p")
  7560. (let (p m)
  7561. (if (eq last-command this-command)
  7562. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7563. (setq p org-mark-ring))
  7564. (setq org-mark-ring-last-goto p)
  7565. (setq m (car p))
  7566. (switch-to-buffer (marker-buffer m))
  7567. (goto-char m)
  7568. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7569. (defun org-remove-angle-brackets (s)
  7570. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7571. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7572. s)
  7573. (defun org-add-angle-brackets (s)
  7574. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7575. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7576. s)
  7577. (defun org-remove-double-quotes (s)
  7578. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7579. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7580. s)
  7581. ;;; Following specific links
  7582. (defun org-follow-timestamp-link ()
  7583. (cond
  7584. ((org-at-date-range-p t)
  7585. (let ((org-agenda-start-on-weekday)
  7586. (t1 (match-string 1))
  7587. (t2 (match-string 2)))
  7588. (setq t1 (time-to-days (org-time-string-to-time t1))
  7589. t2 (time-to-days (org-time-string-to-time t2)))
  7590. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7591. ((org-at-timestamp-p t)
  7592. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7593. (substring (match-string 1) 0 10)))
  7594. 1))
  7595. (t (error "This should not happen"))))
  7596. ;;; Following file links
  7597. (defvar org-wait nil)
  7598. (defun org-open-file (path &optional in-emacs line search)
  7599. "Open the file at PATH.
  7600. First, this expands any special file name abbreviations. Then the
  7601. configuration variable `org-file-apps' is checked if it contains an
  7602. entry for this file type, and if yes, the corresponding command is launched.
  7603. If no application is found, Emacs simply visits the file.
  7604. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7605. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7606. and o use an external application to visit the file.
  7607. Optional LINE specifies a line to go to, optional SEARCH a string to
  7608. search for. If LINE or SEARCH is given, the file will always be
  7609. opened in Emacs.
  7610. If the file does not exist, an error is thrown."
  7611. (setq in-emacs (or in-emacs line search))
  7612. (let* ((file (if (equal path "")
  7613. buffer-file-name
  7614. (substitute-in-file-name (expand-file-name path))))
  7615. (apps (append org-file-apps (org-default-apps)))
  7616. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7617. (dirp (if remp nil (file-directory-p file)))
  7618. (file (if (and dirp org-open-directory-means-index-dot-org)
  7619. (concat (file-name-as-directory file) "index.org")
  7620. file))
  7621. (a-m-a-p (assq 'auto-mode apps))
  7622. (dfile (downcase file))
  7623. (old-buffer (current-buffer))
  7624. (old-pos (point))
  7625. (old-mode major-mode)
  7626. ext cmd)
  7627. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7628. (setq ext (match-string 1 dfile))
  7629. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7630. (setq ext (match-string 1 dfile))))
  7631. (cond
  7632. ((equal in-emacs '(16))
  7633. (setq cmd (cdr (assoc 'system apps))))
  7634. (in-emacs (setq cmd 'emacs))
  7635. (t
  7636. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7637. (and dirp (cdr (assoc 'directory apps)))
  7638. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7639. 'string-match)
  7640. (cdr (assoc ext apps))
  7641. (cdr (assoc t apps))))))
  7642. (when (eq cmd 'system)
  7643. (setq cmd (cdr (assoc 'system apps))))
  7644. (when (eq cmd 'default)
  7645. (setq cmd (cdr (assoc t apps))))
  7646. (when (eq cmd 'mailcap)
  7647. (require 'mailcap)
  7648. (mailcap-parse-mailcaps)
  7649. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7650. (command (mailcap-mime-info mime-type)))
  7651. (if (stringp command)
  7652. (setq cmd command)
  7653. (setq cmd 'emacs))))
  7654. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7655. (not (file-exists-p file))
  7656. (not org-open-non-existing-files))
  7657. (error "No such file: %s" file))
  7658. (cond
  7659. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7660. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7661. (while (string-match "['\"]%s['\"]" cmd)
  7662. (setq cmd (replace-match "%s" t t cmd)))
  7663. (while (string-match "%s" cmd)
  7664. (setq cmd (replace-match
  7665. (save-match-data
  7666. (shell-quote-argument
  7667. (convert-standard-filename file)))
  7668. t t cmd)))
  7669. (save-window-excursion
  7670. (start-process-shell-command cmd nil cmd)
  7671. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7672. ))
  7673. ((or (stringp cmd)
  7674. (eq cmd 'emacs))
  7675. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7676. (widen)
  7677. (if line (org-goto-line line)
  7678. (if search (org-link-search search))))
  7679. ((consp cmd)
  7680. (let ((file (convert-standard-filename file)))
  7681. (eval cmd)))
  7682. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7683. (and (org-mode-p) (eq old-mode 'org-mode)
  7684. (or (not (equal old-buffer (current-buffer)))
  7685. (not (equal old-pos (point))))
  7686. (org-mark-ring-push old-pos old-buffer))))
  7687. (defun org-default-apps ()
  7688. "Return the default applications for this operating system."
  7689. (cond
  7690. ((eq system-type 'darwin)
  7691. org-file-apps-defaults-macosx)
  7692. ((eq system-type 'windows-nt)
  7693. org-file-apps-defaults-windowsnt)
  7694. (t org-file-apps-defaults-gnu)))
  7695. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7696. "Convert extensions to regular expressions in the cars of LIST.
  7697. Also, weed out any non-string entries, because the return value is used
  7698. only for regexp matching.
  7699. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7700. point to the symbol `emacs', indicating that the file should
  7701. be opened in Emacs."
  7702. (append
  7703. (delq nil
  7704. (mapcar (lambda (x)
  7705. (if (not (stringp (car x)))
  7706. nil
  7707. (if (string-match "\\W" (car x))
  7708. x
  7709. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7710. list))
  7711. (if add-auto-mode
  7712. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7713. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7714. (defun org-file-remote-p (file)
  7715. "Test whether FILE specifies a location on a remote system.
  7716. Return non-nil if the location is indeed remote.
  7717. For example, the filename \"/user@host:/foo\" specifies a location
  7718. on the system \"/user@host:\"."
  7719. (cond ((fboundp 'file-remote-p)
  7720. (file-remote-p file))
  7721. ((fboundp 'tramp-handle-file-remote-p)
  7722. (tramp-handle-file-remote-p file))
  7723. ((and (boundp 'ange-ftp-name-format)
  7724. (string-match (car ange-ftp-name-format) file))
  7725. t)
  7726. (t nil)))
  7727. ;;;; Refiling
  7728. (defun org-get-org-file ()
  7729. "Read a filename, with default directory `org-directory'."
  7730. (let ((default (or org-default-notes-file remember-data-file)))
  7731. (read-file-name (format "File name [%s]: " default)
  7732. (file-name-as-directory org-directory)
  7733. default)))
  7734. (defun org-notes-order-reversed-p ()
  7735. "Check if the current file should receive notes in reversed order."
  7736. (cond
  7737. ((not org-reverse-note-order) nil)
  7738. ((eq t org-reverse-note-order) t)
  7739. ((not (listp org-reverse-note-order)) nil)
  7740. (t (catch 'exit
  7741. (let ((all org-reverse-note-order)
  7742. entry)
  7743. (while (setq entry (pop all))
  7744. (if (string-match (car entry) buffer-file-name)
  7745. (throw 'exit (cdr entry))))
  7746. nil)))))
  7747. (defvar org-refile-target-table nil
  7748. "The list of refile targets, created by `org-refile'.")
  7749. (defvar org-agenda-new-buffers nil
  7750. "Buffers created to visit agenda files.")
  7751. (defun org-get-refile-targets (&optional default-buffer)
  7752. "Produce a table with refile targets."
  7753. (let ((case-fold-search nil)
  7754. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7755. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7756. targets txt re files f desc descre fast-path-p level pos0)
  7757. (message "Getting targets...")
  7758. (with-current-buffer (or default-buffer (current-buffer))
  7759. (while (setq entry (pop entries))
  7760. (setq files (car entry) desc (cdr entry))
  7761. (setq fast-path-p nil)
  7762. (cond
  7763. ((null files) (setq files (list (current-buffer))))
  7764. ((eq files 'org-agenda-files)
  7765. (setq files (org-agenda-files 'unrestricted)))
  7766. ((and (symbolp files) (fboundp files))
  7767. (setq files (funcall files)))
  7768. ((and (symbolp files) (boundp files))
  7769. (setq files (symbol-value files))))
  7770. (if (stringp files) (setq files (list files)))
  7771. (cond
  7772. ((eq (car desc) :tag)
  7773. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7774. ((eq (car desc) :todo)
  7775. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7776. ((eq (car desc) :regexp)
  7777. (setq descre (cdr desc)))
  7778. ((eq (car desc) :level)
  7779. (setq descre (concat "^\\*\\{" (number-to-string
  7780. (if org-odd-levels-only
  7781. (1- (* 2 (cdr desc)))
  7782. (cdr desc)))
  7783. "\\}[ \t]")))
  7784. ((eq (car desc) :maxlevel)
  7785. (setq fast-path-p t)
  7786. (setq descre (concat "^\\*\\{1," (number-to-string
  7787. (if org-odd-levels-only
  7788. (1- (* 2 (cdr desc)))
  7789. (cdr desc)))
  7790. "\\}[ \t]")))
  7791. (t (error "Bad refiling target description %s" desc)))
  7792. (while (setq f (pop files))
  7793. (save-excursion
  7794. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7795. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7796. (setq f (expand-file-name f))
  7797. (if (eq org-refile-use-outline-path 'file)
  7798. (push (list (file-name-nondirectory f) f nil nil) targets))
  7799. (save-excursion
  7800. (save-restriction
  7801. (widen)
  7802. (goto-char (point-min))
  7803. (while (re-search-forward descre nil t)
  7804. (goto-char (setq pos0 (point-at-bol)))
  7805. (catch 'next
  7806. (when org-refile-target-verify-function
  7807. (save-match-data
  7808. (or (funcall org-refile-target-verify-function)
  7809. (throw 'next t))))
  7810. (when (looking-at org-complex-heading-regexp)
  7811. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7812. txt (org-link-display-format (match-string 4))
  7813. re (concat "^" (regexp-quote
  7814. (buffer-substring (match-beginning 1)
  7815. (match-end 4)))))
  7816. (if (match-end 5) (setq re (concat re "[ \t]+"
  7817. (regexp-quote
  7818. (match-string 5)))))
  7819. (setq re (concat re "[ \t]*$"))
  7820. (when org-refile-use-outline-path
  7821. (setq txt (mapconcat 'org-protect-slash
  7822. (append
  7823. (if (eq org-refile-use-outline-path 'file)
  7824. (list (file-name-nondirectory
  7825. (buffer-file-name (buffer-base-buffer))))
  7826. (if (eq org-refile-use-outline-path 'full-file-path)
  7827. (list (buffer-file-name (buffer-base-buffer)))))
  7828. (org-get-outline-path fast-path-p level txt)
  7829. (list txt))
  7830. "/")))
  7831. (push (list txt f re (point)) targets)))
  7832. (when (= (point) pos0)
  7833. ;; verification function has not moved point
  7834. (goto-char (point-at-eol))))))))))
  7835. (message "Getting targets...done")
  7836. (nreverse targets)))
  7837. (defun org-protect-slash (s)
  7838. (while (string-match "/" s)
  7839. (setq s (replace-match "\\" t t s)))
  7840. s)
  7841. (defvar org-olpa (make-vector 20 nil))
  7842. (defun org-get-outline-path (&optional fastp level heading)
  7843. "Return the outline path to the current entry, as a list."
  7844. (if fastp
  7845. (progn
  7846. (if (> level 19)
  7847. (error "Outline path failure, more than 19 levels."))
  7848. (loop for i from level upto 19 do
  7849. (aset org-olpa i nil))
  7850. (prog1
  7851. (delq nil (append org-olpa nil))
  7852. (aset org-olpa level heading)))
  7853. (let (rtn)
  7854. (save-excursion
  7855. (while (org-up-heading-safe)
  7856. (when (looking-at org-complex-heading-regexp)
  7857. (push (org-match-string-no-properties 4) rtn)))
  7858. rtn))))
  7859. (defvar org-refile-history nil
  7860. "History for refiling operations.")
  7861. (defvar org-after-refile-insert-hook nil
  7862. "Hook run after `org-refile' has inserted its stuff at the new location.
  7863. Note that this is still *before* the stuff will be removed from
  7864. the *old* location.")
  7865. (defun org-refile (&optional goto default-buffer rfloc)
  7866. "Move the entry at point to another heading.
  7867. The list of target headings is compiled using the information in
  7868. `org-refile-targets', which see. This list is created before each use
  7869. and will therefore always be up-to-date.
  7870. At the target location, the entry is filed as a subitem of the target heading.
  7871. Depending on `org-reverse-note-order', the new subitem will either be the
  7872. first or the last subitem.
  7873. If there is an active region, all entries in that region will be moved.
  7874. However, the region must fulfil the requirement that the first heading
  7875. is the first one sets the top-level of the moved text - at most siblings
  7876. below it are allowed.
  7877. With prefix arg GOTO, the command will only visit the target location,
  7878. not actually move anything.
  7879. With a double prefix `C-u C-u', go to the location where the last refiling
  7880. operation has put the subtree.
  7881. RFLOC can be a refile location obtained in a different way.
  7882. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7883. (interactive "P")
  7884. (let* ((cbuf (current-buffer))
  7885. (regionp (org-region-active-p))
  7886. (region-start (and regionp (region-beginning)))
  7887. (region-end (and regionp (region-end)))
  7888. (region-length (and regionp (- region-end region-start)))
  7889. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7890. pos it nbuf file re level reversed)
  7891. (when regionp
  7892. (goto-char region-start)
  7893. (or (bolp) (goto-char (point-at-bol)))
  7894. (setq region-start (point))
  7895. (unless (org-kill-is-subtree-p
  7896. (buffer-substring region-start region-end))
  7897. (error "The region is not a (sequence of) subtree(s)")))
  7898. (if (equal goto '(16))
  7899. (org-refile-goto-last-stored)
  7900. (when (setq it (or rfloc
  7901. (save-excursion
  7902. (org-refile-get-location
  7903. (if goto "Goto: " "Refile to: ") default-buffer
  7904. org-refile-allow-creating-parent-nodes))))
  7905. (setq file (nth 1 it)
  7906. re (nth 2 it)
  7907. pos (nth 3 it))
  7908. (if (and (not goto)
  7909. pos
  7910. (equal (buffer-file-name) file)
  7911. (if regionp
  7912. (and (>= pos region-start)
  7913. (<= pos region-end))
  7914. (and (>= pos (point))
  7915. (< pos (save-excursion
  7916. (org-end-of-subtree t t))))))
  7917. (error "Cannot refile to position inside the tree or region"))
  7918. (setq nbuf (or (find-buffer-visiting file)
  7919. (find-file-noselect file)))
  7920. (if goto
  7921. (progn
  7922. (switch-to-buffer nbuf)
  7923. (goto-char pos)
  7924. (org-show-context 'org-goto))
  7925. (if regionp
  7926. (progn
  7927. (org-kill-new (buffer-substring region-start region-end))
  7928. (org-save-markers-in-region region-start region-end))
  7929. (org-copy-subtree 1 nil t))
  7930. (save-excursion
  7931. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7932. (find-file-noselect file))))
  7933. (setq reversed (org-notes-order-reversed-p))
  7934. (save-excursion
  7935. (save-restriction
  7936. (widen)
  7937. (if pos
  7938. (progn
  7939. (goto-char pos)
  7940. (looking-at outline-regexp)
  7941. (setq level (org-get-valid-level (funcall outline-level) 1))
  7942. (goto-char
  7943. (if reversed
  7944. (or (outline-next-heading) (point-max))
  7945. (or (save-excursion (org-get-next-sibling))
  7946. (org-end-of-subtree t t)
  7947. (point-max)))))
  7948. (setq level 1)
  7949. (if (not reversed)
  7950. (goto-char (point-max))
  7951. (goto-char (point-min))
  7952. (or (outline-next-heading) (goto-char (point-max)))))
  7953. (if (not (bolp)) (newline))
  7954. (bookmark-set "org-refile-last-stored")
  7955. (org-paste-subtree level)
  7956. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7957. (run-hooks 'org-after-refile-insert-hook))))
  7958. (if regionp
  7959. (delete-region (point) (+ (point) region-length))
  7960. (org-cut-subtree))
  7961. (when (featurep 'org-inlinetask)
  7962. (org-inlinetask-remove-END-maybe))
  7963. (setq org-markers-to-move nil)
  7964. (message "Refiled to \"%s\"" (car it))))))
  7965. (org-reveal))
  7966. (defun org-refile-goto-last-stored ()
  7967. "Go to the location where the last refile was stored."
  7968. (interactive)
  7969. (bookmark-jump "org-refile-last-stored")
  7970. (message "This is the location of the last refile"))
  7971. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7972. "Prompt the user for a refile location, using PROMPT."
  7973. (let ((org-refile-targets org-refile-targets)
  7974. (org-refile-use-outline-path org-refile-use-outline-path))
  7975. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7976. (unless org-refile-target-table
  7977. (error "No refile targets"))
  7978. (let* ((cbuf (current-buffer))
  7979. (partial-completion-mode nil)
  7980. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7981. (cfunc (if (and org-refile-use-outline-path
  7982. org-outline-path-complete-in-steps)
  7983. 'org-olpath-completing-read
  7984. 'org-icompleting-read))
  7985. (extra (if org-refile-use-outline-path "/" ""))
  7986. (filename (and cfn (expand-file-name cfn)))
  7987. (tbl (mapcar
  7988. (lambda (x)
  7989. (if (and (not (member org-refile-use-outline-path
  7990. '(file full-file-path)))
  7991. (not (equal filename (nth 1 x))))
  7992. (cons (concat (car x) extra " ("
  7993. (file-name-nondirectory (nth 1 x)) ")")
  7994. (cdr x))
  7995. (cons (concat (car x) extra) (cdr x))))
  7996. org-refile-target-table))
  7997. (completion-ignore-case t)
  7998. pa answ parent-target child parent old-hist)
  7999. (setq old-hist org-refile-history)
  8000. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8001. nil 'org-refile-history))
  8002. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8003. (if pa
  8004. (progn
  8005. (when (or (not org-refile-history)
  8006. (not (eq old-hist org-refile-history))
  8007. (not (equal (car pa) (car org-refile-history))))
  8008. (setq org-refile-history
  8009. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8010. org-refile-history
  8011. (cdr org-refile-history))))
  8012. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8013. (pop org-refile-history)))
  8014. pa)
  8015. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8016. (setq parent (match-string 1 answ)
  8017. child (match-string 2 answ))
  8018. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8019. (when (and parent-target
  8020. (or (eq new-nodes t)
  8021. (and (eq new-nodes 'confirm)
  8022. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8023. (org-refile-new-child parent-target child))))))
  8024. (defun org-refile-new-child (parent-target child)
  8025. "Use refile target PARENT-TARGET to add new CHILD below it."
  8026. (unless parent-target
  8027. (error "Cannot find parent for new node"))
  8028. (let ((file (nth 1 parent-target))
  8029. (pos (nth 3 parent-target))
  8030. level)
  8031. (with-current-buffer (or (find-buffer-visiting file)
  8032. (find-file-noselect file))
  8033. (save-excursion
  8034. (save-restriction
  8035. (widen)
  8036. (if pos
  8037. (goto-char pos)
  8038. (goto-char (point-max))
  8039. (if (not (bolp)) (newline)))
  8040. (when (looking-at outline-regexp)
  8041. (setq level (funcall outline-level))
  8042. (org-end-of-subtree t t))
  8043. (org-back-over-empty-lines)
  8044. (insert "\n" (make-string
  8045. (if pos (org-get-valid-level level 1) 1) ?*)
  8046. " " child "\n")
  8047. (beginning-of-line 0)
  8048. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8049. (defun org-olpath-completing-read (prompt collection &rest args)
  8050. "Read an outline path like a file name."
  8051. (let ((thetable collection)
  8052. (org-completion-use-ido nil) ; does not work with ido.
  8053. (org-completion-use-iswitchb nil)) ; or iswitchb
  8054. (apply
  8055. 'org-icompleting-read prompt
  8056. (lambda (string predicate &optional flag)
  8057. (let (rtn r f (l (length string)))
  8058. (cond
  8059. ((eq flag nil)
  8060. ;; try completion
  8061. (try-completion string thetable))
  8062. ((eq flag t)
  8063. ;; all-completions
  8064. (setq rtn (all-completions string thetable predicate))
  8065. (mapcar
  8066. (lambda (x)
  8067. (setq r (substring x l))
  8068. (if (string-match " ([^)]*)$" x)
  8069. (setq f (match-string 0 x))
  8070. (setq f ""))
  8071. (if (string-match "/" r)
  8072. (concat string (substring r 0 (match-end 0)) f)
  8073. x))
  8074. rtn))
  8075. ((eq flag 'lambda)
  8076. ;; exact match?
  8077. (assoc string thetable)))
  8078. ))
  8079. args)))
  8080. ;;;; Dynamic blocks
  8081. (defun org-find-dblock (name)
  8082. "Find the first dynamic block with name NAME in the buffer.
  8083. If not found, stay at current position and return nil."
  8084. (let (pos)
  8085. (save-excursion
  8086. (goto-char (point-min))
  8087. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8088. nil t)
  8089. (match-beginning 0))))
  8090. (if pos (goto-char pos))
  8091. pos))
  8092. (defconst org-dblock-start-re
  8093. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8094. "Matches the startline of a dynamic block, with parameters.")
  8095. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8096. "Matches the end of a dynamic block.")
  8097. (defun org-create-dblock (plist)
  8098. "Create a dynamic block section, with parameters taken from PLIST.
  8099. PLIST must contain a :name entry which is used as name of the block."
  8100. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8101. (end-of-line 1)
  8102. (newline))
  8103. (let ((col (current-column))
  8104. (name (plist-get plist :name)))
  8105. (insert "#+BEGIN: " name)
  8106. (while plist
  8107. (if (eq (car plist) :name)
  8108. (setq plist (cddr plist))
  8109. (insert " " (prin1-to-string (pop plist)))))
  8110. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8111. (beginning-of-line -2)))
  8112. (defun org-prepare-dblock ()
  8113. "Prepare dynamic block for refresh.
  8114. This empties the block, puts the cursor at the insert position and returns
  8115. the property list including an extra property :name with the block name."
  8116. (unless (looking-at org-dblock-start-re)
  8117. (error "Not at a dynamic block"))
  8118. (let* ((begdel (1+ (match-end 0)))
  8119. (name (org-no-properties (match-string 1)))
  8120. (params (append (list :name name)
  8121. (read (concat "(" (match-string 3) ")")))))
  8122. (save-excursion
  8123. (beginning-of-line 1)
  8124. (skip-chars-forward " \t")
  8125. (setq params (plist-put params :indentation-column (current-column))))
  8126. (unless (re-search-forward org-dblock-end-re nil t)
  8127. (error "Dynamic block not terminated"))
  8128. (setq params
  8129. (append params
  8130. (list :content (buffer-substring
  8131. begdel (match-beginning 0)))))
  8132. (delete-region begdel (match-beginning 0))
  8133. (goto-char begdel)
  8134. (open-line 1)
  8135. params))
  8136. (defun org-map-dblocks (&optional command)
  8137. "Apply COMMAND to all dynamic blocks in the current buffer.
  8138. If COMMAND is not given, use `org-update-dblock'."
  8139. (let ((cmd (or command 'org-update-dblock))
  8140. pos)
  8141. (save-excursion
  8142. (goto-char (point-min))
  8143. (while (re-search-forward org-dblock-start-re nil t)
  8144. (goto-char (setq pos (match-beginning 0)))
  8145. (condition-case nil
  8146. (funcall cmd)
  8147. (error (message "Error during update of dynamic block")))
  8148. (goto-char pos)
  8149. (unless (re-search-forward org-dblock-end-re nil t)
  8150. (error "Dynamic block not terminated"))))))
  8151. (defun org-dblock-update (&optional arg)
  8152. "User command for updating dynamic blocks.
  8153. Update the dynamic block at point. With prefix ARG, update all dynamic
  8154. blocks in the buffer."
  8155. (interactive "P")
  8156. (if arg
  8157. (org-update-all-dblocks)
  8158. (or (looking-at org-dblock-start-re)
  8159. (org-beginning-of-dblock))
  8160. (org-update-dblock)))
  8161. (defun org-update-dblock ()
  8162. "Update the dynamic block at point
  8163. This means to empty the block, parse for parameters and then call
  8164. the correct writing function."
  8165. (save-window-excursion
  8166. (let* ((pos (point))
  8167. (line (org-current-line))
  8168. (params (org-prepare-dblock))
  8169. (name (plist-get params :name))
  8170. (indent (plist-get params :indentation-column))
  8171. (cmd (intern (concat "org-dblock-write:" name))))
  8172. (message "Updating dynamic block `%s' at line %d..." name line)
  8173. (funcall cmd params)
  8174. (message "Updating dynamic block `%s' at line %d...done" name line)
  8175. (goto-char pos)
  8176. (when (and indent (> indent 0))
  8177. (setq indent (make-string indent ?\ ))
  8178. (save-excursion
  8179. (org-beginning-of-dblock)
  8180. (forward-line 1)
  8181. (while (not (looking-at org-dblock-end-re))
  8182. (insert indent)
  8183. (beginning-of-line 2))
  8184. (when (looking-at org-dblock-end-re)
  8185. (and (looking-at "[ \t]+")
  8186. (replace-match ""))
  8187. (insert indent)))))))
  8188. (defun org-beginning-of-dblock ()
  8189. "Find the beginning of the dynamic block at point.
  8190. Error if there is no such block at point."
  8191. (let ((pos (point))
  8192. beg)
  8193. (end-of-line 1)
  8194. (if (and (re-search-backward org-dblock-start-re nil t)
  8195. (setq beg (match-beginning 0))
  8196. (re-search-forward org-dblock-end-re nil t)
  8197. (> (match-end 0) pos))
  8198. (goto-char beg)
  8199. (goto-char pos)
  8200. (error "Not in a dynamic block"))))
  8201. (defun org-update-all-dblocks ()
  8202. "Update all dynamic blocks in the buffer.
  8203. This function can be used in a hook."
  8204. (when (org-mode-p)
  8205. (org-map-dblocks 'org-update-dblock)))
  8206. ;;;; Completion
  8207. (defconst org-additional-option-like-keywords
  8208. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8209. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8210. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  8211. "BEGIN:" "END:"
  8212. "ORGTBL" "TBLFM:" "TBLNAME:"
  8213. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8214. "BEGIN_QUOTE" "END_QUOTE"
  8215. "BEGIN_VERSE" "END_VERSE"
  8216. "BEGIN_CENTER" "END_CENTER"
  8217. "BEGIN_SRC" "END_SRC"
  8218. "CATEGORY" "COLUMNS"
  8219. "CAPTION" "LABEL"
  8220. "BIND"
  8221. "MACRO"))
  8222. (defcustom org-structure-template-alist
  8223. '(
  8224. ("s" "#+begin_src ?\n\n#+end_src"
  8225. "<src lang=\"?\">\n\n</src>")
  8226. ("e" "#+begin_example\n?\n#+end_example"
  8227. "<example>\n?\n</example>")
  8228. ("q" "#+begin_quote\n?\n#+end_quote"
  8229. "<quote>\n?\n</quote>")
  8230. ("v" "#+begin_verse\n?\n#+end_verse"
  8231. "<verse>\n?\n/verse>")
  8232. ("c" "#+begin_center\n?\n#+end_center"
  8233. "<center>\n?\n/center>")
  8234. ("l" "#+begin_latex\n?\n#+end_latex"
  8235. "<literal style=\"latex\">\n?\n</literal>")
  8236. ("L" "#+latex: "
  8237. "<literal style=\"latex\">?</literal>")
  8238. ("h" "#+begin_html\n?\n#+end_html"
  8239. "<literal style=\"html\">\n?\n</literal>")
  8240. ("H" "#+html: "
  8241. "<literal style=\"html\">?</literal>")
  8242. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8243. ("A" "#+ascii: ")
  8244. ("i" "#+include %file ?"
  8245. "<include file=%file markup=\"?\">")
  8246. )
  8247. "Structure completion elements.
  8248. This is a list of abbreviation keys and values. The value gets inserted
  8249. it you type @samp{.} followed by the key and then the completion key,
  8250. usually `M-TAB'. %file will be replaced by a file name after prompting
  8251. for the file using completion.
  8252. There are two templates for each key, the first uses the original Org syntax,
  8253. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8254. the default when the /org-mtags.el/ module has been loaded. See also the
  8255. variable `org-mtags-prefer-muse-templates'.
  8256. This is an experimental feature, it is undecided if it is going to stay in."
  8257. :group 'org-completion
  8258. :type '(repeat
  8259. (string :tag "Key")
  8260. (string :tag "Template")
  8261. (string :tag "Muse Template")))
  8262. (defun org-try-structure-completion ()
  8263. "Try to complete a structure template before point.
  8264. This looks for strings like \"<e\" on an otherwise empty line and
  8265. expands them."
  8266. (let ((l (buffer-substring (point-at-bol) (point)))
  8267. a)
  8268. (when (and (looking-at "[ \t]*$")
  8269. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8270. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8271. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8272. (match-beginning 1)) a)
  8273. t)))
  8274. (defun org-complete-expand-structure-template (start cell)
  8275. "Expand a structure template."
  8276. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8277. (rpl (nth (if musep 2 1) cell))
  8278. (ind ""))
  8279. (delete-region start (point))
  8280. (when (string-match "\\`#\\+" rpl)
  8281. (cond
  8282. ((bolp))
  8283. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8284. (setq ind (buffer-substring (point-at-bol) (point))))
  8285. (t (newline))))
  8286. (setq start (point))
  8287. (if (string-match "%file" rpl)
  8288. (setq rpl (replace-match
  8289. (concat
  8290. "\""
  8291. (save-match-data
  8292. (abbreviate-file-name (read-file-name "Include file: ")))
  8293. "\"")
  8294. t t rpl)))
  8295. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8296. (concat "\n" ind)))
  8297. (insert rpl)
  8298. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8299. (defun org-complete (&optional arg)
  8300. "Perform completion on word at point.
  8301. At the beginning of a headline, this completes TODO keywords as given in
  8302. `org-todo-keywords'.
  8303. If the current word is preceded by a backslash, completes the TeX symbols
  8304. that are supported for HTML support.
  8305. If the current word is preceded by \"#+\", completes special words for
  8306. setting file options.
  8307. In the line after \"#+STARTUP:, complete valid keywords.\"
  8308. At all other locations, this simply calls the value of
  8309. `org-completion-fallback-command'."
  8310. (interactive "P")
  8311. (org-without-partial-completion
  8312. (catch 'exit
  8313. (let* ((a nil)
  8314. (end (point))
  8315. (beg1 (save-excursion
  8316. (skip-chars-backward (org-re "[:alnum:]_@"))
  8317. (point)))
  8318. (beg (save-excursion
  8319. (skip-chars-backward "a-zA-Z0-9_:$")
  8320. (point)))
  8321. (confirm (lambda (x) (stringp (car x))))
  8322. (searchhead (equal (char-before beg) ?*))
  8323. (struct
  8324. (when (and (member (char-before beg1) '(?. ?<))
  8325. (setq a (assoc (buffer-substring beg1 (point))
  8326. org-structure-template-alist)))
  8327. (org-complete-expand-structure-template (1- beg1) a)
  8328. (throw 'exit t)))
  8329. (tag (and (equal (char-before beg1) ?:)
  8330. (equal (char-after (point-at-bol)) ?*)))
  8331. (prop (and (equal (char-before beg1) ?:)
  8332. (not (equal (char-after (point-at-bol)) ?*))))
  8333. (texp (equal (char-before beg) ?\\))
  8334. (link (equal (char-before beg) ?\[))
  8335. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8336. beg)
  8337. "#+"))
  8338. (startup (string-match "^#\\+STARTUP:.*"
  8339. (buffer-substring (point-at-bol) (point))))
  8340. (completion-ignore-case opt)
  8341. (type nil)
  8342. (tbl nil)
  8343. (table (cond
  8344. (opt
  8345. (setq type :opt)
  8346. (require 'org-exp)
  8347. (append
  8348. (delq nil
  8349. (mapcar
  8350. (lambda (x)
  8351. (if (string-match
  8352. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8353. (cons (match-string 2 x)
  8354. (match-string 1 x))))
  8355. (org-split-string (org-get-current-options) "\n")))
  8356. (mapcar 'list org-additional-option-like-keywords)))
  8357. (startup
  8358. (setq type :startup)
  8359. org-startup-options)
  8360. (link (append org-link-abbrev-alist-local
  8361. org-link-abbrev-alist))
  8362. (texp
  8363. (setq type :tex)
  8364. org-html-entities)
  8365. ((string-match "\\`\\*+[ \t]+\\'"
  8366. (buffer-substring (point-at-bol) beg))
  8367. (setq type :todo)
  8368. (mapcar 'list org-todo-keywords-1))
  8369. (searchhead
  8370. (setq type :searchhead)
  8371. (save-excursion
  8372. (goto-char (point-min))
  8373. (while (re-search-forward org-todo-line-regexp nil t)
  8374. (push (list
  8375. (org-make-org-heading-search-string
  8376. (match-string 3) t))
  8377. tbl)))
  8378. tbl)
  8379. (tag (setq type :tag beg beg1)
  8380. (or org-tag-alist (org-get-buffer-tags)))
  8381. (prop (setq type :prop beg beg1)
  8382. (mapcar 'list (org-buffer-property-keys nil t t)))
  8383. (t (progn
  8384. (call-interactively org-completion-fallback-command)
  8385. (throw 'exit nil)))))
  8386. (pattern (buffer-substring-no-properties beg end))
  8387. (completion (try-completion pattern table confirm)))
  8388. (cond ((eq completion t)
  8389. (if (not (assoc (upcase pattern) table))
  8390. (message "Already complete")
  8391. (if (and (equal type :opt)
  8392. (not (member (car (assoc (upcase pattern) table))
  8393. org-additional-option-like-keywords)))
  8394. (insert (substring (cdr (assoc (upcase pattern) table))
  8395. (length pattern)))
  8396. (if (memq type '(:tag :prop)) (insert ":")))))
  8397. ((null completion)
  8398. (message "Can't find completion for \"%s\"" pattern)
  8399. (ding))
  8400. ((not (string= pattern completion))
  8401. (delete-region beg end)
  8402. (if (string-match " +$" completion)
  8403. (setq completion (replace-match "" t t completion)))
  8404. (insert completion)
  8405. (if (get-buffer-window "*Completions*")
  8406. (delete-window (get-buffer-window "*Completions*")))
  8407. (if (assoc completion table)
  8408. (if (eq type :todo) (insert " ")
  8409. (if (memq type '(:tag :prop)) (insert ":"))))
  8410. (if (and (equal type :opt) (assoc completion table))
  8411. (message "%s" (substitute-command-keys
  8412. "Press \\[org-complete] again to insert example settings"))))
  8413. (t
  8414. (message "Making completion list...")
  8415. (let ((list (sort (all-completions pattern table confirm)
  8416. 'string<)))
  8417. (with-output-to-temp-buffer "*Completions*"
  8418. (condition-case nil
  8419. ;; Protection needed for XEmacs and emacs 21
  8420. (display-completion-list list pattern)
  8421. (error (display-completion-list list)))))
  8422. (message "Making completion list...%s" "done")))))))
  8423. ;;;; TODO, DEADLINE, Comments
  8424. (defun org-toggle-comment ()
  8425. "Change the COMMENT state of an entry."
  8426. (interactive)
  8427. (save-excursion
  8428. (org-back-to-heading)
  8429. (let (case-fold-search)
  8430. (if (looking-at (concat outline-regexp
  8431. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8432. (replace-match "" t t nil 1)
  8433. (if (looking-at outline-regexp)
  8434. (progn
  8435. (goto-char (match-end 0))
  8436. (insert org-comment-string " ")))))))
  8437. (defvar org-last-todo-state-is-todo nil
  8438. "This is non-nil when the last TODO state change led to a TODO state.
  8439. If the last change removed the TODO tag or switched to DONE, then
  8440. this is nil.")
  8441. (defvar org-setting-tags nil) ; dynamically skipped
  8442. (defun org-parse-local-options (string var)
  8443. "Parse STRING for startup setting relevant for variable VAR."
  8444. (let ((rtn (symbol-value var))
  8445. e opts)
  8446. (save-match-data
  8447. (if (or (not string) (not (string-match "\\S-" string)))
  8448. rtn
  8449. (setq opts (delq nil (mapcar (lambda (x)
  8450. (setq e (assoc x org-startup-options))
  8451. (if (eq (nth 1 e) var) e nil))
  8452. (org-split-string string "[ \t]+"))))
  8453. (if (not opts)
  8454. rtn
  8455. (setq rtn nil)
  8456. (while (setq e (pop opts))
  8457. (if (not (nth 3 e))
  8458. (setq rtn (nth 2 e))
  8459. (if (not (listp rtn)) (setq rtn nil))
  8460. (push (nth 2 e) rtn)))
  8461. rtn)))))
  8462. (defvar org-todo-setup-filter-hook nil
  8463. "Hook for functions that pre-filter todo specs.
  8464. Each function takes a todo spec and returns either `nil' or the spec
  8465. transformed into canonical form." )
  8466. (defvar org-todo-get-default-hook nil
  8467. "Hook for functions that get a default item for todo.
  8468. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8469. `nil' or a string to be used for the todo mark." )
  8470. (defvar org-agenda-headline-snapshot-before-repeat)
  8471. (defun org-todo (&optional arg)
  8472. "Change the TODO state of an item.
  8473. The state of an item is given by a keyword at the start of the heading,
  8474. like
  8475. *** TODO Write paper
  8476. *** DONE Call mom
  8477. The different keywords are specified in the variable `org-todo-keywords'.
  8478. By default the available states are \"TODO\" and \"DONE\".
  8479. So for this example: when the item starts with TODO, it is changed to DONE.
  8480. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8481. DONE are present, add TODO at the beginning of the heading.
  8482. With C-u prefix arg, use completion to determine the new state.
  8483. With numeric prefix arg, switch to that state.
  8484. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8485. With a tripple C-u prefix, circumvent any state blocking.
  8486. For calling through lisp, arg is also interpreted in the following way:
  8487. 'none -> empty state
  8488. \"\"(empty string) -> switch to empty state
  8489. 'done -> switch to DONE
  8490. 'nextset -> switch to the next set of keywords
  8491. 'previousset -> switch to the previous set of keywords
  8492. \"WAITING\" -> switch to the specified keyword, but only if it
  8493. really is a member of `org-todo-keywords'."
  8494. (interactive "P")
  8495. (if (equal arg '(16)) (setq arg 'nextset))
  8496. (let ((org-blocker-hook org-blocker-hook)
  8497. (case-fold-search nil))
  8498. (when (equal arg '(64))
  8499. (setq arg nil org-blocker-hook nil))
  8500. (when (and org-blocker-hook
  8501. (or org-inhibit-blocking
  8502. (org-entry-get nil "NOBLOCKING")))
  8503. (setq org-blocker-hook nil))
  8504. (save-excursion
  8505. (catch 'exit
  8506. (org-back-to-heading)
  8507. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8508. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8509. (looking-at " *"))
  8510. (let* ((match-data (match-data))
  8511. (startpos (point-at-bol))
  8512. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8513. (org-log-done org-log-done)
  8514. (org-log-repeat org-log-repeat)
  8515. (org-todo-log-states org-todo-log-states)
  8516. (this (match-string 1))
  8517. (hl-pos (match-beginning 0))
  8518. (head (org-get-todo-sequence-head this))
  8519. (ass (assoc head org-todo-kwd-alist))
  8520. (interpret (nth 1 ass))
  8521. (done-word (nth 3 ass))
  8522. (final-done-word (nth 4 ass))
  8523. (last-state (or this ""))
  8524. (completion-ignore-case t)
  8525. (member (member this org-todo-keywords-1))
  8526. (tail (cdr member))
  8527. (state (cond
  8528. ((and org-todo-key-trigger
  8529. (or (and (equal arg '(4))
  8530. (eq org-use-fast-todo-selection 'prefix))
  8531. (and (not arg) org-use-fast-todo-selection
  8532. (not (eq org-use-fast-todo-selection
  8533. 'prefix)))))
  8534. ;; Use fast selection
  8535. (org-fast-todo-selection))
  8536. ((and (equal arg '(4))
  8537. (or (not org-use-fast-todo-selection)
  8538. (not org-todo-key-trigger)))
  8539. ;; Read a state with completion
  8540. (org-icompleting-read
  8541. "State: " (mapcar (lambda(x) (list x))
  8542. org-todo-keywords-1)
  8543. nil t))
  8544. ((eq arg 'right)
  8545. (if this
  8546. (if tail (car tail) nil)
  8547. (car org-todo-keywords-1)))
  8548. ((eq arg 'left)
  8549. (if (equal member org-todo-keywords-1)
  8550. nil
  8551. (if this
  8552. (nth (- (length org-todo-keywords-1)
  8553. (length tail) 2)
  8554. org-todo-keywords-1)
  8555. (org-last org-todo-keywords-1))))
  8556. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8557. (setq arg nil))) ; hack to fall back to cycling
  8558. (arg
  8559. ;; user or caller requests a specific state
  8560. (cond
  8561. ((equal arg "") nil)
  8562. ((eq arg 'none) nil)
  8563. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8564. ((eq arg 'nextset)
  8565. (or (car (cdr (member head org-todo-heads)))
  8566. (car org-todo-heads)))
  8567. ((eq arg 'previousset)
  8568. (let ((org-todo-heads (reverse org-todo-heads)))
  8569. (or (car (cdr (member head org-todo-heads)))
  8570. (car org-todo-heads))))
  8571. ((car (member arg org-todo-keywords-1)))
  8572. ((nth (1- (prefix-numeric-value arg))
  8573. org-todo-keywords-1))))
  8574. ((null member) (or head (car org-todo-keywords-1)))
  8575. ((equal this final-done-word) nil) ;; -> make empty
  8576. ((null tail) nil) ;; -> first entry
  8577. ((memq interpret '(type priority))
  8578. (if (eq this-command last-command)
  8579. (car tail)
  8580. (if (> (length tail) 0)
  8581. (or done-word (car org-done-keywords))
  8582. nil)))
  8583. (t
  8584. (car tail))))
  8585. (state (or
  8586. (run-hook-with-args-until-success
  8587. 'org-todo-get-default-hook state last-state)
  8588. state))
  8589. (next (if state (concat " " state " ") " "))
  8590. (change-plist (list :type 'todo-state-change :from this :to state
  8591. :position startpos))
  8592. dolog now-done-p)
  8593. (when org-blocker-hook
  8594. (setq org-last-todo-state-is-todo
  8595. (not (member this org-done-keywords)))
  8596. (unless (save-excursion
  8597. (save-match-data
  8598. (run-hook-with-args-until-failure
  8599. 'org-blocker-hook change-plist)))
  8600. (if (interactive-p)
  8601. (error "TODO state change from %s to %s blocked" this state)
  8602. ;; fail silently
  8603. (message "TODO state change from %s to %s blocked" this state)
  8604. (throw 'exit nil))))
  8605. (store-match-data match-data)
  8606. (replace-match next t t)
  8607. (unless (pos-visible-in-window-p hl-pos)
  8608. (message "TODO state changed to %s" (org-trim next)))
  8609. (unless head
  8610. (setq head (org-get-todo-sequence-head state)
  8611. ass (assoc head org-todo-kwd-alist)
  8612. interpret (nth 1 ass)
  8613. done-word (nth 3 ass)
  8614. final-done-word (nth 4 ass)))
  8615. (when (memq arg '(nextset previousset))
  8616. (message "Keyword-Set %d/%d: %s"
  8617. (- (length org-todo-sets) -1
  8618. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8619. (length org-todo-sets)
  8620. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8621. (setq org-last-todo-state-is-todo
  8622. (not (member state org-done-keywords)))
  8623. (setq now-done-p (and (member state org-done-keywords)
  8624. (not (member this org-done-keywords))))
  8625. (and logging (org-local-logging logging))
  8626. (when (and (or org-todo-log-states org-log-done)
  8627. (not (eq org-inhibit-logging t))
  8628. (not (memq arg '(nextset previousset))))
  8629. ;; we need to look at recording a time and note
  8630. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8631. (nth 2 (assoc this org-todo-log-states))))
  8632. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8633. (setq dolog 'time))
  8634. (when (and state
  8635. (member state org-not-done-keywords)
  8636. (not (member this org-not-done-keywords)))
  8637. ;; This is now a todo state and was not one before
  8638. ;; If there was a CLOSED time stamp, get rid of it.
  8639. (org-add-planning-info nil nil 'closed))
  8640. (when (and now-done-p org-log-done)
  8641. ;; It is now done, and it was not done before
  8642. (org-add-planning-info 'closed (org-current-time))
  8643. (if (and (not dolog) (eq 'note org-log-done))
  8644. (org-add-log-setup 'done state this 'findpos 'note)))
  8645. (when (and state dolog)
  8646. ;; This is a non-nil state, and we need to log it
  8647. (org-add-log-setup 'state state this 'findpos dolog)))
  8648. ;; Fixup tag positioning
  8649. (org-todo-trigger-tag-changes state)
  8650. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8651. (when org-provide-todo-statistics
  8652. (org-update-parent-todo-statistics))
  8653. (run-hooks 'org-after-todo-state-change-hook)
  8654. (if (and arg (not (member state org-done-keywords)))
  8655. (setq head (org-get-todo-sequence-head state)))
  8656. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8657. ;; Do we need to trigger a repeat?
  8658. (when now-done-p
  8659. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8660. ;; This is for the agenda, take a snapshot of the headline.
  8661. (save-match-data
  8662. (setq org-agenda-headline-snapshot-before-repeat
  8663. (org-get-heading))))
  8664. (org-auto-repeat-maybe state))
  8665. ;; Fixup cursor location if close to the keyword
  8666. (if (and (outline-on-heading-p)
  8667. (not (bolp))
  8668. (save-excursion (beginning-of-line 1)
  8669. (looking-at org-todo-line-regexp))
  8670. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8671. (progn
  8672. (goto-char (or (match-end 2) (match-end 1)))
  8673. (and (looking-at " ") (just-one-space))))
  8674. (when org-trigger-hook
  8675. (save-excursion
  8676. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8677. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8678. "Block turning an entry into a TODO, using the hierarchy.
  8679. This checks whether the current task should be blocked from state
  8680. changes. Such blocking occurs when:
  8681. 1. The task has children which are not all in a completed state.
  8682. 2. A task has a parent with the property :ORDERED:, and there
  8683. are siblings prior to the current task with incomplete
  8684. status.
  8685. 3. The parent of the task is blocked because it has siblings that should
  8686. be done first, or is child of a block grandparent TODO entry."
  8687. (catch 'dont-block
  8688. ;; If this is not a todo state change, or if this entry is already DONE,
  8689. ;; do not block
  8690. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8691. (member (plist-get change-plist :from)
  8692. (cons 'done org-done-keywords))
  8693. (member (plist-get change-plist :to)
  8694. (cons 'todo org-not-done-keywords)))
  8695. (throw 'dont-block t))
  8696. ;; If this task has children, and any are undone, it's blocked
  8697. (save-excursion
  8698. (org-back-to-heading t)
  8699. (let ((this-level (funcall outline-level)))
  8700. (outline-next-heading)
  8701. (let ((child-level (funcall outline-level)))
  8702. (while (and (not (eobp))
  8703. (> child-level this-level))
  8704. ;; this todo has children, check whether they are all
  8705. ;; completed
  8706. (if (and (not (org-entry-is-done-p))
  8707. (org-entry-is-todo-p))
  8708. (throw 'dont-block nil))
  8709. (outline-next-heading)
  8710. (setq child-level (funcall outline-level))))))
  8711. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8712. ;; any previous siblings are undone, it's blocked
  8713. (save-excursion
  8714. (org-back-to-heading t)
  8715. (let* ((pos (point))
  8716. (parent-pos (and (org-up-heading-safe) (point))))
  8717. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8718. (when (and (org-entry-get (point) "ORDERED")
  8719. (forward-line 1)
  8720. (re-search-forward org-not-done-heading-regexp pos t))
  8721. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8722. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8723. (while t
  8724. (goto-char parent-pos)
  8725. (if (not (looking-at org-not-done-heading-regexp))
  8726. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8727. (setq pos (point))
  8728. (setq parent-pos (and (org-up-heading-safe) (point)))
  8729. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8730. (when (and (org-entry-get (point) "ORDERED")
  8731. (forward-line 1)
  8732. (re-search-forward org-not-done-heading-regexp pos t))
  8733. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8734. (defcustom org-track-ordered-property-with-tag nil
  8735. "Should the ORDERED property also be shown as a tag?
  8736. The ORDERED property decides if an entry should require subtasks to be
  8737. completed in sequence. Since a property is not very visible, setting
  8738. this option means that toggling the ORDERED property with the command
  8739. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8740. not relevant for the behavior, but it makes things more visible.
  8741. Note that toggling the tag with tags commands will not change the property
  8742. and therefore not influence behavior!
  8743. This can be t, meaning the tag ORDERED should be used, It can also be a
  8744. string to select a different tag for this task."
  8745. :group 'org-todo
  8746. :type '(choice
  8747. (const :tag "No tracking" nil)
  8748. (const :tag "Track with ORDERED tag" t)
  8749. (string :tag "Use other tag")))
  8750. (defun org-toggle-ordered-property ()
  8751. "Toggle the ORDERED property of the current entry.
  8752. For better visibility, you can track the value of this property with a tag.
  8753. See variable `org-track-ordered-property-with-tag'."
  8754. (interactive)
  8755. (let* ((t1 org-track-ordered-property-with-tag)
  8756. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8757. (save-excursion
  8758. (org-back-to-heading)
  8759. (if (org-entry-get nil "ORDERED")
  8760. (progn
  8761. (org-delete-property "ORDERED")
  8762. (and tag (org-toggle-tag tag 'off))
  8763. (message "Subtasks can be completed in arbitrary order"))
  8764. (org-entry-put nil "ORDERED" "t")
  8765. (and tag (org-toggle-tag tag 'on))
  8766. (message "Subtasks must be completed in sequence")))))
  8767. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8768. (defun org-block-todo-from-checkboxes (change-plist)
  8769. "Block turning an entry into a TODO, using checkboxes.
  8770. This checks whether the current task should be blocked from state
  8771. changes because there are uncheckd boxes in this entry."
  8772. (catch 'dont-block
  8773. ;; If this is not a todo state change, or if this entry is already DONE,
  8774. ;; do not block
  8775. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8776. (member (plist-get change-plist :from)
  8777. (cons 'done org-done-keywords))
  8778. (member (plist-get change-plist :to)
  8779. (cons 'todo org-not-done-keywords)))
  8780. (throw 'dont-block t))
  8781. ;; If this task has checkboxes that are not checked, it's blocked
  8782. (save-excursion
  8783. (org-back-to-heading t)
  8784. (let ((beg (point)) end)
  8785. (outline-next-heading)
  8786. (setq end (point))
  8787. (goto-char beg)
  8788. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8789. end t)
  8790. (progn
  8791. (if (boundp 'org-blocked-by-checkboxes)
  8792. (setq org-blocked-by-checkboxes t))
  8793. (throw 'dont-block nil)))))
  8794. t)) ; do not block
  8795. (defun org-update-statistics-cookies (all)
  8796. "Update the statistics cookie, either from TODO or from checkboxes.
  8797. This should be called with the cursor in a line with a statistics cookie."
  8798. (interactive "P")
  8799. (if all
  8800. (progn
  8801. (org-update-checkbox-count 'all)
  8802. (org-map-entries 'org-update-parent-todo-statistics))
  8803. (if (not (org-on-heading-p))
  8804. (org-update-checkbox-count)
  8805. (let ((pos (move-marker (make-marker) (point)))
  8806. end l1 l2)
  8807. (ignore-errors (org-back-to-heading t))
  8808. (if (not (org-on-heading-p))
  8809. (org-update-checkbox-count)
  8810. (setq l1 (org-outline-level))
  8811. (setq end (save-excursion
  8812. (outline-next-heading)
  8813. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  8814. (point)))
  8815. (if (and (save-excursion (re-search-forward
  8816. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  8817. (not (save-excursion (re-search-forward
  8818. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  8819. (org-update-checkbox-count)
  8820. (if (and l2 (> l2 l1))
  8821. (progn
  8822. (goto-char end)
  8823. (org-update-parent-todo-statistics))
  8824. (error "No data for statistics cookie"))))
  8825. (goto-char pos)
  8826. (move-marker pos nil)))))
  8827. (defvar org-entry-property-inherited-from) ;; defined below
  8828. (defun org-update-parent-todo-statistics ()
  8829. "Update any statistics cookie in the parent of the current headline.
  8830. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8831. the entire subtree and this will travel up the hierarchy and update
  8832. statistics everywhere."
  8833. (interactive)
  8834. (let* ((lim 0) prop
  8835. (recursive (or (not org-hierarchical-todo-statistics)
  8836. (string-match
  8837. "\\<recursive\\>"
  8838. (or (setq prop (org-entry-get
  8839. nil "COOKIE_DATA" 'inherit)) ""))))
  8840. (lim (or (and prop (marker-position
  8841. org-entry-property-inherited-from))
  8842. lim))
  8843. (first t)
  8844. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8845. level ltoggle l1 new ndel
  8846. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8847. (catch 'exit
  8848. (save-excursion
  8849. (beginning-of-line 1)
  8850. (if (org-at-heading-p)
  8851. (setq ltoggle (funcall outline-level))
  8852. (error "This should not happen"))
  8853. (while (and (setq level (org-up-heading-safe))
  8854. (or recursive first)
  8855. (>= (point) lim))
  8856. (setq first nil)
  8857. (unless (and level
  8858. (not (string-match
  8859. "\\<checkbox\\>"
  8860. (downcase
  8861. (or (org-entry-get
  8862. nil "COOKIE_DATA")
  8863. "")))))
  8864. (throw 'exit nil))
  8865. (while (re-search-forward box-re (point-at-eol) t)
  8866. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8867. (setq is-percent (match-end 2))
  8868. (save-match-data
  8869. (unless (outline-next-heading) (throw 'exit nil))
  8870. (while (and (looking-at org-complex-heading-regexp)
  8871. (> (setq l1 (length (match-string 1))) level))
  8872. (setq kwd (and (or recursive (= l1 ltoggle))
  8873. (match-string 2)))
  8874. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8875. (and (listp org-provide-todo-statistics)
  8876. (or (member kwd org-provide-todo-statistics)
  8877. (member kwd org-done-keywords))))
  8878. (setq cnt-all (1+ cnt-all))
  8879. (if (eq org-provide-todo-statistics t)
  8880. (and kwd (setq cnt-all (1+ cnt-all)))))
  8881. (and (member kwd org-done-keywords)
  8882. (setq cnt-done (1+ cnt-done)))
  8883. (outline-next-heading)))
  8884. (setq new
  8885. (if is-percent
  8886. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8887. (format "[%d/%d]" cnt-done cnt-all))
  8888. ndel (- (match-end 0) (match-beginning 0)))
  8889. (goto-char (match-beginning 0))
  8890. (insert new)
  8891. (delete-region (point) (+ (point) ndel))))
  8892. (when cookie-present
  8893. (run-hook-with-args 'org-after-todo-statistics-hook
  8894. cnt-done (- cnt-all cnt-done)))))
  8895. (run-hooks 'org-todo-statistics-hook)))
  8896. (defvar org-after-todo-statistics-hook nil
  8897. "Hook that is called after a TODO statistics cookie has been updated.
  8898. Each function is called with two arguments: the number of not-done entries
  8899. and the number of done entries.
  8900. For example, the following function, when added to this hook, will switch
  8901. an entry to DONE when all children are done, and back to TODO when new
  8902. entries are set to a TODO status. Note that this hook is only called
  8903. when there is a statistics cookie in the headline!
  8904. (defun org-summary-todo (n-done n-not-done)
  8905. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8906. (let (org-log-done org-log-states) ; turn off logging
  8907. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8908. ")
  8909. (defvar org-todo-statistics-hook nil
  8910. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8911. This hook runs even if there is no statisics cookie present, in which case
  8912. `org-after-todo-statistics-hook' would not run.")
  8913. (defun org-todo-trigger-tag-changes (state)
  8914. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8915. (let ((l org-todo-state-tags-triggers)
  8916. changes)
  8917. (when (or (not state) (equal state ""))
  8918. (setq changes (append changes (cdr (assoc "" l)))))
  8919. (when (and (stringp state) (> (length state) 0))
  8920. (setq changes (append changes (cdr (assoc state l)))))
  8921. (when (member state org-not-done-keywords)
  8922. (setq changes (append changes (cdr (assoc 'todo l)))))
  8923. (when (member state org-done-keywords)
  8924. (setq changes (append changes (cdr (assoc 'done l)))))
  8925. (dolist (c changes)
  8926. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8927. (defun org-local-logging (value)
  8928. "Get logging settings from a property VALUE."
  8929. (let* (words w a)
  8930. ;; directly set the variables, they are already local.
  8931. (setq org-log-done nil
  8932. org-log-repeat nil
  8933. org-todo-log-states nil)
  8934. (setq words (org-split-string value))
  8935. (while (setq w (pop words))
  8936. (cond
  8937. ((setq a (assoc w org-startup-options))
  8938. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8939. (set (nth 1 a) (nth 2 a))))
  8940. ((setq a (org-extract-log-state-settings w))
  8941. (and (member (car a) org-todo-keywords-1)
  8942. (push a org-todo-log-states)))))))
  8943. (defun org-get-todo-sequence-head (kwd)
  8944. "Return the head of the TODO sequence to which KWD belongs.
  8945. If KWD is not set, check if there is a text property remembering the
  8946. right sequence."
  8947. (let (p)
  8948. (cond
  8949. ((not kwd)
  8950. (or (get-text-property (point-at-bol) 'org-todo-head)
  8951. (progn
  8952. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8953. nil (point-at-eol)))
  8954. (get-text-property p 'org-todo-head))))
  8955. ((not (member kwd org-todo-keywords-1))
  8956. (car org-todo-keywords-1))
  8957. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8958. (defun org-fast-todo-selection ()
  8959. "Fast TODO keyword selection with single keys.
  8960. Returns the new TODO keyword, or nil if no state change should occur."
  8961. (let* ((fulltable org-todo-key-alist)
  8962. (done-keywords org-done-keywords) ;; needed for the faces.
  8963. (maxlen (apply 'max (mapcar
  8964. (lambda (x)
  8965. (if (stringp (car x)) (string-width (car x)) 0))
  8966. fulltable)))
  8967. (expert nil)
  8968. (fwidth (+ maxlen 3 1 3))
  8969. (ncol (/ (- (window-width) 4) fwidth))
  8970. tg cnt e c tbl
  8971. groups ingroup)
  8972. (save-excursion
  8973. (save-window-excursion
  8974. (if expert
  8975. (set-buffer (get-buffer-create " *Org todo*"))
  8976. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8977. (erase-buffer)
  8978. (org-set-local 'org-done-keywords done-keywords)
  8979. (setq tbl fulltable cnt 0)
  8980. (while (setq e (pop tbl))
  8981. (cond
  8982. ((equal e '(:startgroup))
  8983. (push '() groups) (setq ingroup t)
  8984. (when (not (= cnt 0))
  8985. (setq cnt 0)
  8986. (insert "\n"))
  8987. (insert "{ "))
  8988. ((equal e '(:endgroup))
  8989. (setq ingroup nil cnt 0)
  8990. (insert "}\n"))
  8991. ((equal e '(:newline))
  8992. (when (not (= cnt 0))
  8993. (setq cnt 0)
  8994. (insert "\n")
  8995. (setq e (car tbl))
  8996. (while (equal (car tbl) '(:newline))
  8997. (insert "\n")
  8998. (setq tbl (cdr tbl)))))
  8999. (t
  9000. (setq tg (car e) c (cdr e))
  9001. (if ingroup (push tg (car groups)))
  9002. (setq tg (org-add-props tg nil 'face
  9003. (org-get-todo-face tg)))
  9004. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9005. (insert "[" c "] " tg (make-string
  9006. (- fwidth 4 (length tg)) ?\ ))
  9007. (when (= (setq cnt (1+ cnt)) ncol)
  9008. (insert "\n")
  9009. (if ingroup (insert " "))
  9010. (setq cnt 0)))))
  9011. (insert "\n")
  9012. (goto-char (point-min))
  9013. (if (not expert) (org-fit-window-to-buffer))
  9014. (message "[a-z..]:Set [SPC]:clear")
  9015. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9016. (cond
  9017. ((or (= c ?\C-g)
  9018. (and (= c ?q) (not (rassoc c fulltable))))
  9019. (setq quit-flag t))
  9020. ((= c ?\ ) nil)
  9021. ((setq e (rassoc c fulltable) tg (car e))
  9022. tg)
  9023. (t (setq quit-flag t)))))))
  9024. (defun org-entry-is-todo-p ()
  9025. (member (org-get-todo-state) org-not-done-keywords))
  9026. (defun org-entry-is-done-p ()
  9027. (member (org-get-todo-state) org-done-keywords))
  9028. (defun org-get-todo-state ()
  9029. (save-excursion
  9030. (org-back-to-heading t)
  9031. (and (looking-at org-todo-line-regexp)
  9032. (match-end 2)
  9033. (match-string 2))))
  9034. (defun org-at-date-range-p (&optional inactive-ok)
  9035. "Is the cursor inside a date range?"
  9036. (interactive)
  9037. (save-excursion
  9038. (catch 'exit
  9039. (let ((pos (point)))
  9040. (skip-chars-backward "^[<\r\n")
  9041. (skip-chars-backward "<[")
  9042. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9043. (>= (match-end 0) pos)
  9044. (throw 'exit t))
  9045. (skip-chars-backward "^<[\r\n")
  9046. (skip-chars-backward "<[")
  9047. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9048. (>= (match-end 0) pos)
  9049. (throw 'exit t)))
  9050. nil)))
  9051. (defun org-get-repeat ()
  9052. "Check if there is a deadline/schedule with repeater in this entry."
  9053. (save-match-data
  9054. (save-excursion
  9055. (org-back-to-heading t)
  9056. (if (re-search-forward
  9057. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  9058. (match-string 1)))))
  9059. (defvar org-last-changed-timestamp)
  9060. (defvar org-last-inserted-timestamp)
  9061. (defvar org-log-post-message)
  9062. (defvar org-log-note-purpose)
  9063. (defvar org-log-note-how)
  9064. (defvar org-log-note-extra)
  9065. (defun org-auto-repeat-maybe (done-word)
  9066. "Check if the current headline contains a repeated deadline/schedule.
  9067. If yes, set TODO state back to what it was and change the base date
  9068. of repeating deadline/scheduled time stamps to new date.
  9069. This function is run automatically after each state change to a DONE state."
  9070. ;; last-state is dynamically scoped into this function
  9071. (let* ((repeat (org-get-repeat))
  9072. (aa (assoc last-state org-todo-kwd-alist))
  9073. (interpret (nth 1 aa))
  9074. (head (nth 2 aa))
  9075. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9076. (msg "Entry repeats: ")
  9077. (org-log-done nil)
  9078. (org-todo-log-states nil)
  9079. (nshiftmax 10) (nshift 0)
  9080. re type n what ts time)
  9081. (when repeat
  9082. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9083. (org-todo (if (eq interpret 'type) last-state head))
  9084. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9085. (org-time-stamp-format t t)))
  9086. (when org-log-repeat
  9087. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9088. (memq 'org-add-log-note post-command-hook))
  9089. ;; OK, we are already setup for some record
  9090. (if (eq org-log-repeat 'note)
  9091. ;; make sure we take a note, not only a time stamp
  9092. (setq org-log-note-how 'note))
  9093. ;; Set up for taking a record
  9094. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9095. last-state
  9096. 'findpos org-log-repeat)))
  9097. (org-back-to-heading t)
  9098. (org-add-planning-info nil nil 'closed)
  9099. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9100. org-deadline-time-regexp "\\)\\|\\("
  9101. org-ts-regexp "\\)"))
  9102. (while (re-search-forward
  9103. re (save-excursion (outline-next-heading) (point)) t)
  9104. (setq type (if (match-end 1) org-scheduled-string
  9105. (if (match-end 3) org-deadline-string "Plain:"))
  9106. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9107. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9108. (setq n (string-to-number (match-string 2 ts))
  9109. what (match-string 3 ts))
  9110. (if (equal what "w") (setq n (* n 7) what "d"))
  9111. ;; Preparation, see if we need to modify the start date for the change
  9112. (when (match-end 1)
  9113. (setq time (save-match-data (org-time-string-to-time ts)))
  9114. (cond
  9115. ((equal (match-string 1 ts) ".")
  9116. ;; Shift starting date to today
  9117. (org-timestamp-change
  9118. (- (time-to-days (current-time)) (time-to-days time))
  9119. 'day))
  9120. ((equal (match-string 1 ts) "+")
  9121. (while (or (= nshift 0)
  9122. (<= (time-to-days time) (time-to-days (current-time))))
  9123. (when (= (incf nshift) nshiftmax)
  9124. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9125. (error "Abort")))
  9126. (org-timestamp-change n (cdr (assoc what whata)))
  9127. (org-at-timestamp-p t)
  9128. (setq ts (match-string 1))
  9129. (setq time (save-match-data (org-time-string-to-time ts))))
  9130. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9131. ;; rematch, so that we have everything in place for the real shift
  9132. (org-at-timestamp-p t)
  9133. (setq ts (match-string 1))
  9134. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9135. (org-timestamp-change n (cdr (assoc what whata)))
  9136. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9137. (setq org-log-post-message msg)
  9138. (message "%s" msg))))
  9139. (defun org-show-todo-tree (arg)
  9140. "Make a compact tree which shows all headlines marked with TODO.
  9141. The tree will show the lines where the regexp matches, and all higher
  9142. headlines above the match.
  9143. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9144. With a numeric prefix N, construct a sparse tree for the Nth element
  9145. of `org-todo-keywords-1'."
  9146. (interactive "P")
  9147. (let ((case-fold-search nil)
  9148. (kwd-re
  9149. (cond ((null arg) org-not-done-regexp)
  9150. ((equal arg '(4))
  9151. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9152. (mapcar 'list org-todo-keywords-1))))
  9153. (concat "\\("
  9154. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9155. "\\)\\>")))
  9156. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9157. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9158. org-todo-keywords-1)))
  9159. (t (error "Invalid prefix argument: %s" arg)))))
  9160. (message "%d TODO entries found"
  9161. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9162. (defun org-deadline (&optional remove time)
  9163. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9164. With argument REMOVE, remove any deadline from the item.
  9165. When TIME is set, it should be an internal time specification, and the
  9166. scheduling will use the corresponding date."
  9167. (interactive "P")
  9168. (if remove
  9169. (progn
  9170. (org-remove-timestamp-with-keyword org-deadline-string)
  9171. (message "Item no longer has a deadline."))
  9172. (if (org-get-repeat)
  9173. (error "Cannot change deadline on task with repeater, please do that by hand")
  9174. (org-add-planning-info 'deadline time 'closed)
  9175. (message "Deadline on %s" org-last-inserted-timestamp))))
  9176. (defun org-schedule (&optional remove time)
  9177. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9178. With argument REMOVE, remove any scheduling date from the item.
  9179. When TIME is set, it should be an internal time specification, and the
  9180. scheduling will use the corresponding date."
  9181. (interactive "P")
  9182. (if remove
  9183. (progn
  9184. (org-remove-timestamp-with-keyword org-scheduled-string)
  9185. (message "Item is no longer scheduled."))
  9186. (if (org-get-repeat)
  9187. (error "Cannot reschedule task with repeater, please do that by hand")
  9188. (org-add-planning-info 'scheduled time 'closed)
  9189. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9190. (defun org-get-scheduled-time (pom &optional inherit)
  9191. "Get the scheduled time as a time tuple, of a format suitable
  9192. for calling org-schedule with, or if there is no scheduling,
  9193. returns nil."
  9194. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9195. (when time
  9196. (apply 'encode-time (org-parse-time-string time)))))
  9197. (defun org-get-deadline-time (pom &optional inherit)
  9198. "Get the deadine as a time tuple, of a format suitable for
  9199. calling org-deadlin with, or if there is no scheduling, returns
  9200. nil."
  9201. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9202. (when time
  9203. (apply 'encode-time (org-parse-time-string time)))))
  9204. (defun org-remove-timestamp-with-keyword (keyword)
  9205. "Remove all time stamps with KEYWORD in the current entry."
  9206. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9207. beg)
  9208. (save-excursion
  9209. (org-back-to-heading t)
  9210. (setq beg (point))
  9211. (outline-next-heading)
  9212. (while (re-search-backward re beg t)
  9213. (replace-match "")
  9214. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9215. (equal (char-before) ?\ ))
  9216. (backward-delete-char 1)
  9217. (if (string-match "^[ \t]*$" (buffer-substring
  9218. (point-at-bol) (point-at-eol)))
  9219. (delete-region (point-at-bol)
  9220. (min (point-max) (1+ (point-at-eol))))))))))
  9221. (defun org-add-planning-info (what &optional time &rest remove)
  9222. "Insert new timestamp with keyword in the line directly after the headline.
  9223. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9224. If non is given, the user is prompted for a date.
  9225. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9226. be removed."
  9227. (interactive)
  9228. (let (org-time-was-given org-end-time-was-given ts
  9229. end default-time default-input)
  9230. (catch 'exit
  9231. (when (and (not time) (memq what '(scheduled deadline)))
  9232. ;; Try to get a default date/time from existing timestamp
  9233. (save-excursion
  9234. (org-back-to-heading t)
  9235. (setq end (save-excursion (outline-next-heading) (point)))
  9236. (when (re-search-forward (if (eq what 'scheduled)
  9237. org-scheduled-time-regexp
  9238. org-deadline-time-regexp)
  9239. end t)
  9240. (setq ts (match-string 1)
  9241. default-time
  9242. (apply 'encode-time (org-parse-time-string ts))
  9243. default-input (and ts (org-get-compact-tod ts))))))
  9244. (when what
  9245. ;; If necessary, get the time from the user
  9246. (setq time (or time (org-read-date nil 'to-time nil nil
  9247. default-time default-input))))
  9248. (when (and org-insert-labeled-timestamps-at-point
  9249. (member what '(scheduled deadline)))
  9250. (insert
  9251. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9252. (org-insert-time-stamp time org-time-was-given
  9253. nil nil nil (list org-end-time-was-given))
  9254. (setq what nil))
  9255. (save-excursion
  9256. (save-restriction
  9257. (let (col list elt ts buffer-invisibility-spec)
  9258. (org-back-to-heading t)
  9259. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9260. (goto-char (match-end 1))
  9261. (setq col (current-column))
  9262. (goto-char (match-end 0))
  9263. (if (eobp) (insert "\n") (forward-char 1))
  9264. (when (and (not what)
  9265. (not (looking-at
  9266. (concat "[ \t]*"
  9267. org-keyword-time-not-clock-regexp))))
  9268. ;; Nothing to add, nothing to remove...... :-)
  9269. (throw 'exit nil))
  9270. (if (and (not (looking-at outline-regexp))
  9271. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9272. "[^\r\n]*"))
  9273. (not (equal (match-string 1) org-clock-string)))
  9274. (narrow-to-region (match-beginning 0) (match-end 0))
  9275. (insert-before-markers "\n")
  9276. (backward-char 1)
  9277. (narrow-to-region (point) (point))
  9278. (and org-adapt-indentation (org-indent-to-column col)))
  9279. ;; Check if we have to remove something.
  9280. (setq list (cons what remove))
  9281. (while list
  9282. (setq elt (pop list))
  9283. (goto-char (point-min))
  9284. (when (or (and (eq elt 'scheduled)
  9285. (re-search-forward org-scheduled-time-regexp nil t))
  9286. (and (eq elt 'deadline)
  9287. (re-search-forward org-deadline-time-regexp nil t))
  9288. (and (eq elt 'closed)
  9289. (re-search-forward org-closed-time-regexp nil t)))
  9290. (replace-match "")
  9291. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9292. (skip-chars-backward " ")
  9293. (if (looking-at " +") (replace-match ""))))
  9294. (goto-char (point-max))
  9295. (when what
  9296. (insert
  9297. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9298. (cond ((eq what 'scheduled) org-scheduled-string)
  9299. ((eq what 'deadline) org-deadline-string)
  9300. ((eq what 'closed) org-closed-string))
  9301. " ")
  9302. (setq ts (org-insert-time-stamp
  9303. time
  9304. (or org-time-was-given
  9305. (and (eq what 'closed) org-log-done-with-time))
  9306. (eq what 'closed)
  9307. nil nil (list org-end-time-was-given)))
  9308. (end-of-line 1))
  9309. (goto-char (point-min))
  9310. (widen)
  9311. (if (and (looking-at "[ \t]+\n")
  9312. (equal (char-before) ?\n))
  9313. (delete-region (1- (point)) (point-at-eol)))
  9314. ts))))))
  9315. (defvar org-log-note-marker (make-marker))
  9316. (defvar org-log-note-purpose nil)
  9317. (defvar org-log-note-state nil)
  9318. (defvar org-log-note-previous-state nil)
  9319. (defvar org-log-note-how nil)
  9320. (defvar org-log-note-extra nil)
  9321. (defvar org-log-note-window-configuration nil)
  9322. (defvar org-log-note-return-to (make-marker))
  9323. (defvar org-log-post-message nil
  9324. "Message to be displayed after a log note has been stored.
  9325. The auto-repeater uses this.")
  9326. (defun org-add-note ()
  9327. "Add a note to the current entry.
  9328. This is done in the same way as adding a state change note."
  9329. (interactive)
  9330. (org-add-log-setup 'note nil nil 'findpos nil))
  9331. (defvar org-property-end-re)
  9332. (defun org-add-log-setup (&optional purpose state prev-state
  9333. findpos how &optional extra)
  9334. "Set up the post command hook to take a note.
  9335. If this is about to TODO state change, the new state is expected in STATE.
  9336. When FINDPOS is non-nil, find the correct position for the note in
  9337. the current entry. If not, assume that it can be inserted at point.
  9338. HOW is an indicator what kind of note should be created.
  9339. EXTRA is additional text that will be inserted into the notes buffer."
  9340. (let* ((org-log-into-drawer (org-log-into-drawer))
  9341. (drawer (cond ((stringp org-log-into-drawer)
  9342. org-log-into-drawer)
  9343. (org-log-into-drawer "LOGBOOK")
  9344. (t nil))))
  9345. (save-restriction
  9346. (save-excursion
  9347. (when findpos
  9348. (org-back-to-heading t)
  9349. (narrow-to-region (point) (save-excursion
  9350. (outline-next-heading) (point)))
  9351. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9352. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9353. "[^\r\n]*\\)?"))
  9354. (goto-char (match-end 0))
  9355. (cond
  9356. (drawer
  9357. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9358. nil t)
  9359. (progn
  9360. (goto-char (match-end 0))
  9361. (or org-log-states-order-reversed
  9362. (and (re-search-forward org-property-end-re nil t)
  9363. (goto-char (1- (match-beginning 0))))))
  9364. (insert "\n:" drawer ":\n:END:")
  9365. (beginning-of-line 0)
  9366. (org-indent-line-function)
  9367. (beginning-of-line 2)
  9368. (org-indent-line-function)
  9369. (end-of-line 0)))
  9370. ((and org-log-state-notes-insert-after-drawers
  9371. (save-excursion
  9372. (forward-line) (looking-at org-drawer-regexp)))
  9373. (forward-line)
  9374. (while (looking-at org-drawer-regexp)
  9375. (goto-char (match-end 0))
  9376. (re-search-forward org-property-end-re (point-max) t)
  9377. (forward-line))
  9378. (forward-line -1)))
  9379. (unless org-log-states-order-reversed
  9380. (and (= (char-after) ?\n) (forward-char 1))
  9381. (org-skip-over-state-notes)
  9382. (skip-chars-backward " \t\n\r")))
  9383. (move-marker org-log-note-marker (point))
  9384. (setq org-log-note-purpose purpose
  9385. org-log-note-state state
  9386. org-log-note-previous-state prev-state
  9387. org-log-note-how how
  9388. org-log-note-extra extra)
  9389. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9390. (defun org-skip-over-state-notes ()
  9391. "Skip past the list of State notes in an entry."
  9392. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9393. (while (looking-at "[ \t]*- State")
  9394. (condition-case nil
  9395. (org-next-item)
  9396. (error (org-end-of-item)))))
  9397. (defun org-add-log-note (&optional purpose)
  9398. "Pop up a window for taking a note, and add this note later at point."
  9399. (remove-hook 'post-command-hook 'org-add-log-note)
  9400. (setq org-log-note-window-configuration (current-window-configuration))
  9401. (delete-other-windows)
  9402. (move-marker org-log-note-return-to (point))
  9403. (switch-to-buffer (marker-buffer org-log-note-marker))
  9404. (goto-char org-log-note-marker)
  9405. (org-switch-to-buffer-other-window "*Org Note*")
  9406. (erase-buffer)
  9407. (if (memq org-log-note-how '(time state))
  9408. (let (current-prefix-arg) (org-store-log-note))
  9409. (let ((org-inhibit-startup t)) (org-mode))
  9410. (insert (format "# Insert note for %s.
  9411. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9412. (cond
  9413. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9414. ((eq org-log-note-purpose 'done) "closed todo item")
  9415. ((eq org-log-note-purpose 'state)
  9416. (format "state change from \"%s\" to \"%s\""
  9417. (or org-log-note-previous-state "")
  9418. (or org-log-note-state "")))
  9419. ((eq org-log-note-purpose 'note)
  9420. "this entry")
  9421. (t (error "This should not happen")))))
  9422. (if org-log-note-extra (insert org-log-note-extra))
  9423. (org-set-local 'org-finish-function 'org-store-log-note)))
  9424. (defvar org-note-abort nil) ; dynamically scoped
  9425. (defun org-store-log-note ()
  9426. "Finish taking a log note, and insert it to where it belongs."
  9427. (let ((txt (buffer-string))
  9428. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9429. lines ind)
  9430. (kill-buffer (current-buffer))
  9431. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9432. (setq txt (replace-match "" t t txt)))
  9433. (if (string-match "\\s-+\\'" txt)
  9434. (setq txt (replace-match "" t t txt)))
  9435. (setq lines (org-split-string txt "\n"))
  9436. (when (and note (string-match "\\S-" note))
  9437. (setq note
  9438. (org-replace-escapes
  9439. note
  9440. (list (cons "%u" (user-login-name))
  9441. (cons "%U" user-full-name)
  9442. (cons "%t" (format-time-string
  9443. (org-time-stamp-format 'long 'inactive)
  9444. (current-time)))
  9445. (cons "%s" (if org-log-note-state
  9446. (concat "\"" org-log-note-state "\"")
  9447. ""))
  9448. (cons "%S" (if org-log-note-previous-state
  9449. (concat "\"" org-log-note-previous-state "\"")
  9450. "\"\"")))))
  9451. (if lines (setq note (concat note " \\\\")))
  9452. (push note lines))
  9453. (when (or current-prefix-arg org-note-abort)
  9454. (when org-log-into-drawer
  9455. (org-remove-empty-drawer-at
  9456. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9457. org-log-note-marker))
  9458. (setq lines nil))
  9459. (when lines
  9460. (save-excursion
  9461. (set-buffer (marker-buffer org-log-note-marker))
  9462. (save-excursion
  9463. (goto-char org-log-note-marker)
  9464. (move-marker org-log-note-marker nil)
  9465. (end-of-line 1)
  9466. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9467. (insert "- " (pop lines))
  9468. (org-indent-line-function)
  9469. (beginning-of-line 1)
  9470. (looking-at "[ \t]*")
  9471. (setq ind (concat (match-string 0) " "))
  9472. (end-of-line 1)
  9473. (while lines (insert "\n" ind (pop lines)))
  9474. (message "Note stored")
  9475. (org-back-to-heading t)
  9476. (org-cycle-hide-drawers 'children)))))
  9477. (set-window-configuration org-log-note-window-configuration)
  9478. (with-current-buffer (marker-buffer org-log-note-return-to)
  9479. (goto-char org-log-note-return-to))
  9480. (move-marker org-log-note-return-to nil)
  9481. (and org-log-post-message (message "%s" org-log-post-message)))
  9482. (defun org-remove-empty-drawer-at (drawer pos)
  9483. "Remove an emptyr DARWER drawer at position POS.
  9484. POS may also be a marker."
  9485. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9486. (save-excursion
  9487. (save-restriction
  9488. (widen)
  9489. (goto-char pos)
  9490. (if (org-in-regexp
  9491. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9492. (replace-match ""))))))
  9493. (defun org-sparse-tree (&optional arg)
  9494. "Create a sparse tree, prompt for the details.
  9495. This command can create sparse trees. You first need to select the type
  9496. of match used to create the tree:
  9497. t Show entries with a specific TODO keyword.
  9498. m Show entries selected by a tags/property match.
  9499. p Enter a property name and its value (both with completion on existing
  9500. names/values) and show entries with that property.
  9501. r Show entries matching a regular expression.
  9502. d Show deadlines due within `org-deadline-warning-days'.
  9503. b Show deadlines and scheduled items before a date.
  9504. a Show deadlines and scheduled items after a date."
  9505. (interactive "P")
  9506. (let (ans kwd value)
  9507. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9508. (setq ans (read-char-exclusive))
  9509. (cond
  9510. ((equal ans ?d)
  9511. (call-interactively 'org-check-deadlines))
  9512. ((equal ans ?b)
  9513. (call-interactively 'org-check-before-date))
  9514. ((equal ans ?a)
  9515. (call-interactively 'org-check-after-date))
  9516. ((equal ans ?t)
  9517. (org-show-todo-tree '(4)))
  9518. ((member ans '(?T ?m))
  9519. (call-interactively 'org-match-sparse-tree))
  9520. ((member ans '(?p ?P))
  9521. (setq kwd (org-icompleting-read "Property: "
  9522. (mapcar 'list (org-buffer-property-keys))))
  9523. (setq value (org-icompleting-read "Value: "
  9524. (mapcar 'list (org-property-values kwd))))
  9525. (unless (string-match "\\`{.*}\\'" value)
  9526. (setq value (concat "\"" value "\"")))
  9527. (org-match-sparse-tree arg (concat kwd "=" value)))
  9528. ((member ans '(?r ?R ?/))
  9529. (call-interactively 'org-occur))
  9530. (t (error "No such sparse tree command \"%c\"" ans)))))
  9531. (defvar org-occur-highlights nil
  9532. "List of overlays used for occur matches.")
  9533. (make-variable-buffer-local 'org-occur-highlights)
  9534. (defvar org-occur-parameters nil
  9535. "Parameters of the active org-occur calls.
  9536. This is a list, each call to org-occur pushes as cons cell,
  9537. containing the regular expression and the callback, onto the list.
  9538. The list can contain several entries if `org-occur' has been called
  9539. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9540. will only contain one set of parameters. When the highlights are
  9541. removed (for example with `C-c C-c', or with the next edit (depending
  9542. on `org-remove-highlights-with-change'), this variable is emptied
  9543. as well.")
  9544. (make-variable-buffer-local 'org-occur-parameters)
  9545. (defun org-occur (regexp &optional keep-previous callback)
  9546. "Make a compact tree which shows all matches of REGEXP.
  9547. The tree will show the lines where the regexp matches, and all higher
  9548. headlines above the match. It will also show the heading after the match,
  9549. to make sure editing the matching entry is easy.
  9550. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9551. call to `org-occur' will be kept, to allow stacking of calls to this
  9552. command.
  9553. If CALLBACK is non-nil, it is a function which is called to confirm
  9554. that the match should indeed be shown."
  9555. (interactive "sRegexp: \nP")
  9556. (when (equal regexp "")
  9557. (error "Regexp cannot be empty"))
  9558. (unless keep-previous
  9559. (org-remove-occur-highlights nil nil t))
  9560. (push (cons regexp callback) org-occur-parameters)
  9561. (let ((cnt 0))
  9562. (save-excursion
  9563. (goto-char (point-min))
  9564. (if (or (not keep-previous) ; do not want to keep
  9565. (not org-occur-highlights)) ; no previous matches
  9566. ;; hide everything
  9567. (org-overview))
  9568. (while (re-search-forward regexp nil t)
  9569. (when (or (not callback)
  9570. (save-match-data (funcall callback)))
  9571. (setq cnt (1+ cnt))
  9572. (when org-highlight-sparse-tree-matches
  9573. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9574. (org-show-context 'occur-tree))))
  9575. (when org-remove-highlights-with-change
  9576. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9577. nil 'local))
  9578. (unless org-sparse-tree-open-archived-trees
  9579. (org-hide-archived-subtrees (point-min) (point-max)))
  9580. (run-hooks 'org-occur-hook)
  9581. (if (interactive-p)
  9582. (message "%d match(es) for regexp %s" cnt regexp))
  9583. cnt))
  9584. (defun org-show-context (&optional key)
  9585. "Make sure point and context and visible.
  9586. How much context is shown depends upon the variables
  9587. `org-show-hierarchy-above', `org-show-following-heading'. and
  9588. `org-show-siblings'."
  9589. (let ((heading-p (org-on-heading-p t))
  9590. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9591. (following-p (org-get-alist-option org-show-following-heading key))
  9592. (entry-p (org-get-alist-option org-show-entry-below key))
  9593. (siblings-p (org-get-alist-option org-show-siblings key)))
  9594. (catch 'exit
  9595. ;; Show heading or entry text
  9596. (if (and heading-p (not entry-p))
  9597. (org-flag-heading nil) ; only show the heading
  9598. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9599. (org-show-hidden-entry))) ; show entire entry
  9600. (when following-p
  9601. ;; Show next sibling, or heading below text
  9602. (save-excursion
  9603. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9604. (org-flag-heading nil))))
  9605. (when siblings-p (org-show-siblings))
  9606. (when hierarchy-p
  9607. ;; show all higher headings, possibly with siblings
  9608. (save-excursion
  9609. (while (and (condition-case nil
  9610. (progn (org-up-heading-all 1) t)
  9611. (error nil))
  9612. (not (bobp)))
  9613. (org-flag-heading nil)
  9614. (when siblings-p (org-show-siblings))))))))
  9615. (defun org-reveal (&optional siblings)
  9616. "Show current entry, hierarchy above it, and the following headline.
  9617. This can be used to show a consistent set of context around locations
  9618. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9619. not t for the search context.
  9620. With optional argument SIBLINGS, on each level of the hierarchy all
  9621. siblings are shown. This repairs the tree structure to what it would
  9622. look like when opened with hierarchical calls to `org-cycle'."
  9623. (interactive "P")
  9624. (let ((org-show-hierarchy-above t)
  9625. (org-show-following-heading t)
  9626. (org-show-siblings (if siblings t org-show-siblings)))
  9627. (org-show-context nil)))
  9628. (defun org-highlight-new-match (beg end)
  9629. "Highlight from BEG to END and mark the highlight is an occur headline."
  9630. (let ((ov (org-make-overlay beg end)))
  9631. (org-overlay-put ov 'face 'secondary-selection)
  9632. (push ov org-occur-highlights)))
  9633. (defun org-remove-occur-highlights (&optional beg end noremove)
  9634. "Remove the occur highlights from the buffer.
  9635. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9636. from the `before-change-functions' in the current buffer."
  9637. (interactive)
  9638. (unless org-inhibit-highlight-removal
  9639. (mapc 'org-delete-overlay org-occur-highlights)
  9640. (setq org-occur-highlights nil)
  9641. (setq org-occur-parameters nil)
  9642. (unless noremove
  9643. (remove-hook 'before-change-functions
  9644. 'org-remove-occur-highlights 'local))))
  9645. ;;;; Priorities
  9646. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9647. "Regular expression matching the priority indicator.")
  9648. (defvar org-remove-priority-next-time nil)
  9649. (defun org-priority-up ()
  9650. "Increase the priority of the current item."
  9651. (interactive)
  9652. (org-priority 'up))
  9653. (defun org-priority-down ()
  9654. "Decrease the priority of the current item."
  9655. (interactive)
  9656. (org-priority 'down))
  9657. (defun org-priority (&optional action)
  9658. "Change the priority of an item by ARG.
  9659. ACTION can be `set', `up', `down', or a character."
  9660. (interactive)
  9661. (unless org-enable-priority-commands
  9662. (error "Priority commands are disabled"))
  9663. (setq action (or action 'set))
  9664. (let (current new news have remove)
  9665. (save-excursion
  9666. (org-back-to-heading t)
  9667. (if (looking-at org-priority-regexp)
  9668. (setq current (string-to-char (match-string 2))
  9669. have t)
  9670. (setq current org-default-priority))
  9671. (cond
  9672. ((or (eq action 'set)
  9673. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9674. (if (not (eq action 'set))
  9675. (setq new action)
  9676. (message "Priority %c-%c, SPC to remove: "
  9677. org-highest-priority org-lowest-priority)
  9678. (setq new (read-char-exclusive)))
  9679. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9680. (= (upcase org-lowest-priority) org-lowest-priority))
  9681. (setq new (upcase new)))
  9682. (cond ((equal new ?\ ) (setq remove t))
  9683. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9684. (error "Priority must be between `%c' and `%c'"
  9685. org-highest-priority org-lowest-priority))))
  9686. ((eq action 'up)
  9687. (if (and (not have) (eq last-command this-command))
  9688. (setq new org-lowest-priority)
  9689. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9690. org-default-priority (1- current)))))
  9691. ((eq action 'down)
  9692. (if (and (not have) (eq last-command this-command))
  9693. (setq new org-highest-priority)
  9694. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9695. org-default-priority (1+ current)))))
  9696. (t (error "Invalid action")))
  9697. (if (or (< (upcase new) org-highest-priority)
  9698. (> (upcase new) org-lowest-priority))
  9699. (setq remove t))
  9700. (setq news (format "%c" new))
  9701. (if have
  9702. (if remove
  9703. (replace-match "" t t nil 1)
  9704. (replace-match news t t nil 2))
  9705. (if remove
  9706. (error "No priority cookie found in line")
  9707. (let ((case-fold-search nil))
  9708. (looking-at org-todo-line-regexp))
  9709. (if (match-end 2)
  9710. (progn
  9711. (goto-char (match-end 2))
  9712. (insert " [#" news "]"))
  9713. (goto-char (match-beginning 3))
  9714. (insert "[#" news "] "))))
  9715. (org-preserve-lc (org-set-tags nil 'align)))
  9716. (if remove
  9717. (message "Priority removed")
  9718. (message "Priority of current item set to %s" news))))
  9719. (defun org-get-priority (s)
  9720. "Find priority cookie and return priority."
  9721. (save-match-data
  9722. (if (not (string-match org-priority-regexp s))
  9723. (* 1000 (- org-lowest-priority org-default-priority))
  9724. (* 1000 (- org-lowest-priority
  9725. (string-to-char (match-string 2 s)))))))
  9726. ;;;; Tags
  9727. (defvar org-agenda-archives-mode)
  9728. (defvar org-map-continue-from nil
  9729. "Position from where mapping should continue.
  9730. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9731. (defvar org-scanner-tags nil
  9732. "The current tag list while the tags scanner is running.")
  9733. (defvar org-trust-scanner-tags nil
  9734. "Should `org-get-tags-at' use the tags fro the scanner.
  9735. This is for internal dynamical scoping only.
  9736. When this is non-nil, the function `org-get-tags-at' will return the value
  9737. of `org-scanner-tags' instead of building the list by itself. This
  9738. can lead to large speed-ups when the tags scanner is used in a file with
  9739. many entries, and when the list of tags is retrieved, for example to
  9740. obtain a list of properties. Building the tags list for each entry in such
  9741. a file becomes an N^2 operation - but with this variable set, it scales
  9742. as N.")
  9743. (defun org-scan-tags (action matcher &optional todo-only)
  9744. "Scan headline tags with inheritance and produce output ACTION.
  9745. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9746. or `agenda' to produce an entry list for an agenda view. It can also be
  9747. a Lisp form or a function that should be called at each matched headline, in
  9748. this case the return value is a list of all return values from these calls.
  9749. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9750. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9751. only lines with a TODO keyword are included in the output."
  9752. (require 'org-agenda)
  9753. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9754. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9755. (org-re
  9756. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9757. (props (list 'face 'default
  9758. 'done-face 'org-agenda-done
  9759. 'undone-face 'default
  9760. 'mouse-face 'highlight
  9761. 'org-not-done-regexp org-not-done-regexp
  9762. 'org-todo-regexp org-todo-regexp
  9763. 'keymap org-agenda-keymap
  9764. 'help-echo
  9765. (format "mouse-2 or RET jump to org file %s"
  9766. (abbreviate-file-name
  9767. (or (buffer-file-name (buffer-base-buffer))
  9768. (buffer-name (buffer-base-buffer)))))))
  9769. (case-fold-search nil)
  9770. (org-map-continue-from nil)
  9771. lspos tags tags-list
  9772. (tags-alist (list (cons 0 org-file-tags)))
  9773. (llast 0) rtn rtn1 level category i txt
  9774. todo marker entry priority)
  9775. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9776. (setq action (list 'lambda nil action)))
  9777. (save-excursion
  9778. (goto-char (point-min))
  9779. (when (eq action 'sparse-tree)
  9780. (org-overview)
  9781. (org-remove-occur-highlights))
  9782. (while (re-search-forward re nil t)
  9783. (catch :skip
  9784. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9785. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9786. (goto-char (setq lspos (match-beginning 0)))
  9787. (setq level (org-reduced-level (funcall outline-level))
  9788. category (org-get-category))
  9789. (setq i llast llast level)
  9790. ;; remove tag lists from same and sublevels
  9791. (while (>= i level)
  9792. (when (setq entry (assoc i tags-alist))
  9793. (setq tags-alist (delete entry tags-alist)))
  9794. (setq i (1- i)))
  9795. ;; add the next tags
  9796. (when tags
  9797. (setq tags (org-split-string tags ":")
  9798. tags-alist
  9799. (cons (cons level tags) tags-alist)))
  9800. ;; compile tags for current headline
  9801. (setq tags-list
  9802. (if org-use-tag-inheritance
  9803. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9804. tags)
  9805. org-scanner-tags tags-list)
  9806. (when org-use-tag-inheritance
  9807. (setcdr (car tags-alist)
  9808. (mapcar (lambda (x)
  9809. (setq x (copy-sequence x))
  9810. (org-add-prop-inherited x))
  9811. (cdar tags-alist))))
  9812. (when (and tags org-use-tag-inheritance
  9813. (or (not (eq t org-use-tag-inheritance))
  9814. org-tags-exclude-from-inheritance))
  9815. ;; selective inheritance, remove uninherited ones
  9816. (setcdr (car tags-alist)
  9817. (org-remove-uniherited-tags (cdar tags-alist))))
  9818. (when (and (or (not todo-only)
  9819. (and (member todo org-not-done-keywords)
  9820. (or (not org-agenda-tags-todo-honor-ignore-options)
  9821. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9822. (let ((case-fold-search t)) (eval matcher))
  9823. (or
  9824. (not (member org-archive-tag tags-list))
  9825. ;; we have an archive tag, should we use this anyway?
  9826. (or (not org-agenda-skip-archived-trees)
  9827. (and (eq action 'agenda) org-agenda-archives-mode))))
  9828. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9829. ;; select this headline
  9830. (cond
  9831. ((eq action 'sparse-tree)
  9832. (and org-highlight-sparse-tree-matches
  9833. (org-get-heading) (match-end 0)
  9834. (org-highlight-new-match
  9835. (match-beginning 0) (match-beginning 1)))
  9836. (org-show-context 'tags-tree))
  9837. ((eq action 'agenda)
  9838. (setq txt (org-format-agenda-item
  9839. ""
  9840. (concat
  9841. (if (eq org-tags-match-list-sublevels 'indented)
  9842. (make-string (1- level) ?.) "")
  9843. (org-get-heading))
  9844. category
  9845. tags-list
  9846. )
  9847. priority (org-get-priority txt))
  9848. (goto-char lspos)
  9849. (setq marker (org-agenda-new-marker))
  9850. (org-add-props txt props
  9851. 'org-marker marker 'org-hd-marker marker 'org-category category
  9852. 'todo-state todo
  9853. 'priority priority 'type "tagsmatch")
  9854. (push txt rtn))
  9855. ((functionp action)
  9856. (setq org-map-continue-from nil)
  9857. (save-excursion
  9858. (setq rtn1 (funcall action))
  9859. (push rtn1 rtn)))
  9860. (t (error "Invalid action")))
  9861. ;; if we are to skip sublevels, jump to end of subtree
  9862. (unless org-tags-match-list-sublevels
  9863. (org-end-of-subtree t)
  9864. (backward-char 1))))
  9865. ;; Get the correct position from where to continue
  9866. (if org-map-continue-from
  9867. (goto-char org-map-continue-from)
  9868. (and (= (point) lspos) (end-of-line 1)))))
  9869. (when (and (eq action 'sparse-tree)
  9870. (not org-sparse-tree-open-archived-trees))
  9871. (org-hide-archived-subtrees (point-min) (point-max)))
  9872. (nreverse rtn)))
  9873. (defun org-remove-uniherited-tags (tags)
  9874. "Remove all tags that are not inherited from the list TAGS."
  9875. (cond
  9876. ((eq org-use-tag-inheritance t)
  9877. (if org-tags-exclude-from-inheritance
  9878. (org-delete-all org-tags-exclude-from-inheritance tags)
  9879. tags))
  9880. ((not org-use-tag-inheritance) nil)
  9881. ((stringp org-use-tag-inheritance)
  9882. (delq nil (mapcar
  9883. (lambda (x)
  9884. (if (and (string-match org-use-tag-inheritance x)
  9885. (not (member x org-tags-exclude-from-inheritance)))
  9886. x nil))
  9887. tags)))
  9888. ((listp org-use-tag-inheritance)
  9889. (delq nil (mapcar
  9890. (lambda (x)
  9891. (if (member x org-use-tag-inheritance) x nil))
  9892. tags)))))
  9893. (defvar todo-only) ;; dynamically scoped
  9894. (defun org-match-sparse-tree (&optional todo-only match)
  9895. "Create a sparse tree according to tags string MATCH.
  9896. MATCH can contain positive and negative selection of tags, like
  9897. \"+WORK+URGENT-WITHBOSS\".
  9898. If optional argument TODO-ONLY is non-nil, only select lines that are
  9899. also TODO lines."
  9900. (interactive "P")
  9901. (org-prepare-agenda-buffers (list (current-buffer)))
  9902. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9903. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9904. (defvar org-cached-props nil)
  9905. (defun org-cached-entry-get (pom property)
  9906. (if (or (eq t org-use-property-inheritance)
  9907. (and (stringp org-use-property-inheritance)
  9908. (string-match org-use-property-inheritance property))
  9909. (and (listp org-use-property-inheritance)
  9910. (member property org-use-property-inheritance)))
  9911. ;; Caching is not possible, check it directly
  9912. (org-entry-get pom property 'inherit)
  9913. ;; Get all properties, so that we can do complicated checks easily
  9914. (cdr (assoc property (or org-cached-props
  9915. (setq org-cached-props
  9916. (org-entry-properties pom)))))))
  9917. (defun org-global-tags-completion-table (&optional files)
  9918. "Return the list of all tags in all agenda buffer/files."
  9919. (save-excursion
  9920. (org-uniquify
  9921. (delq nil
  9922. (apply 'append
  9923. (mapcar
  9924. (lambda (file)
  9925. (set-buffer (find-file-noselect file))
  9926. (append (org-get-buffer-tags)
  9927. (mapcar (lambda (x) (if (stringp (car-safe x))
  9928. (list (car-safe x)) nil))
  9929. org-tag-alist)))
  9930. (if (and files (car files))
  9931. files
  9932. (org-agenda-files))))))))
  9933. (defun org-make-tags-matcher (match)
  9934. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9935. ;; todo-only is scoped dynamically into this function, and the function
  9936. ;; may change it if the matcher asks for it.
  9937. (unless match
  9938. ;; Get a new match request, with completion
  9939. (let ((org-last-tags-completion-table
  9940. (org-global-tags-completion-table)))
  9941. (setq match (org-completing-read-no-i
  9942. "Match: " 'org-tags-completion-function nil nil nil
  9943. 'org-tags-history))))
  9944. ;; Parse the string and create a lisp form
  9945. (let ((match0 match)
  9946. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9947. minus tag mm
  9948. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9949. orterms term orlist re-p str-p level-p level-op time-p
  9950. prop-p pn pv po cat-p gv rest)
  9951. (if (string-match "/+" match)
  9952. ;; match contains also a todo-matching request
  9953. (progn
  9954. (setq tagsmatch (substring match 0 (match-beginning 0))
  9955. todomatch (substring match (match-end 0)))
  9956. (if (string-match "^!" todomatch)
  9957. (setq todo-only t todomatch (substring todomatch 1)))
  9958. (if (string-match "^\\s-*$" todomatch)
  9959. (setq todomatch nil)))
  9960. ;; only matching tags
  9961. (setq tagsmatch match todomatch nil))
  9962. ;; Make the tags matcher
  9963. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9964. (setq tagsmatcher t)
  9965. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9966. (while (setq term (pop orterms))
  9967. (while (and (equal (substring term -1) "\\") orterms)
  9968. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9969. (while (string-match re term)
  9970. (setq rest (substring term (match-end 0))
  9971. minus (and (match-end 1)
  9972. (equal (match-string 1 term) "-"))
  9973. tag (match-string 2 term)
  9974. re-p (equal (string-to-char tag) ?{)
  9975. level-p (match-end 4)
  9976. prop-p (match-end 5)
  9977. mm (cond
  9978. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9979. (level-p
  9980. (setq level-op (org-op-to-function (match-string 3 term)))
  9981. `(,level-op level ,(string-to-number
  9982. (match-string 4 term))))
  9983. (prop-p
  9984. (setq pn (match-string 5 term)
  9985. po (match-string 6 term)
  9986. pv (match-string 7 term)
  9987. cat-p (equal pn "CATEGORY")
  9988. re-p (equal (string-to-char pv) ?{)
  9989. str-p (equal (string-to-char pv) ?\")
  9990. time-p (save-match-data
  9991. (string-match "^\"[[<].*[]>]\"$" pv))
  9992. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9993. (if time-p (setq pv (org-matcher-time pv)))
  9994. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9995. (cond
  9996. ((equal pn "CATEGORY")
  9997. (setq gv '(get-text-property (point) 'org-category)))
  9998. ((equal pn "TODO")
  9999. (setq gv 'todo))
  10000. (t
  10001. (setq gv `(org-cached-entry-get nil ,pn))))
  10002. (if re-p
  10003. (if (eq po 'org<>)
  10004. `(not (string-match ,pv (or ,gv "")))
  10005. `(string-match ,pv (or ,gv "")))
  10006. (if str-p
  10007. `(,po (or ,gv "") ,pv)
  10008. `(,po (string-to-number (or ,gv ""))
  10009. ,(string-to-number pv) ))))
  10010. (t `(member ,tag tags-list)))
  10011. mm (if minus (list 'not mm) mm)
  10012. term rest)
  10013. (push mm tagsmatcher))
  10014. (push (if (> (length tagsmatcher) 1)
  10015. (cons 'and tagsmatcher)
  10016. (car tagsmatcher))
  10017. orlist)
  10018. (setq tagsmatcher nil))
  10019. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10020. (setq tagsmatcher
  10021. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10022. ;; Make the todo matcher
  10023. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10024. (setq todomatcher t)
  10025. (setq orterms (org-split-string todomatch "|") orlist nil)
  10026. (while (setq term (pop orterms))
  10027. (while (string-match re term)
  10028. (setq minus (and (match-end 1)
  10029. (equal (match-string 1 term) "-"))
  10030. kwd (match-string 2 term)
  10031. re-p (equal (string-to-char kwd) ?{)
  10032. term (substring term (match-end 0))
  10033. mm (if re-p
  10034. `(string-match ,(substring kwd 1 -1) todo)
  10035. (list 'equal 'todo kwd))
  10036. mm (if minus (list 'not mm) mm))
  10037. (push mm todomatcher))
  10038. (push (if (> (length todomatcher) 1)
  10039. (cons 'and todomatcher)
  10040. (car todomatcher))
  10041. orlist)
  10042. (setq todomatcher nil))
  10043. (setq todomatcher (if (> (length orlist) 1)
  10044. (cons 'or orlist) (car orlist))))
  10045. ;; Return the string and lisp forms of the matcher
  10046. (setq matcher (if todomatcher
  10047. (list 'and tagsmatcher todomatcher)
  10048. tagsmatcher))
  10049. (cons match0 matcher)))
  10050. (defun org-op-to-function (op &optional stringp)
  10051. "Turn an operator into the appropriate function."
  10052. (setq op
  10053. (cond
  10054. ((equal op "<" ) '(< string< org-time<))
  10055. ((equal op ">" ) '(> org-string> org-time>))
  10056. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10057. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10058. ((member op '("=" "==")) '(= string= org-time=))
  10059. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10060. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10061. (defun org<> (a b) (not (= a b)))
  10062. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10063. (defun org-string>= (a b) (not (string< a b)))
  10064. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10065. (defun org-string<> (a b) (not (string= a b)))
  10066. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10067. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10068. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10069. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10070. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10071. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10072. (defun org-2ft (s)
  10073. "Convert S to a floating point time.
  10074. If S is already a number, just return it. If it is a string, parse
  10075. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10076. (cond
  10077. ((numberp s) s)
  10078. ((stringp s)
  10079. (condition-case nil
  10080. (float-time (apply 'encode-time (org-parse-time-string s)))
  10081. (error 0.)))
  10082. (t 0.)))
  10083. (defun org-time-today ()
  10084. "Time in seconds today at 0:00.
  10085. Returns the float number of seconds since the beginning of the
  10086. epoch to the beginning of today (00:00)."
  10087. (float-time (apply 'encode-time
  10088. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10089. (defun org-matcher-time (s)
  10090. "Interpret a time comparison value."
  10091. (save-match-data
  10092. (cond
  10093. ((string= s "<now>") (float-time))
  10094. ((string= s "<today>") (org-time-today))
  10095. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10096. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10097. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10098. (+ (org-time-today)
  10099. (* (string-to-number (match-string 1 s))
  10100. (cdr (assoc (match-string 2 s)
  10101. '(("d" . 86400.0) ("w" . 604800.0)
  10102. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10103. (t (org-2ft s)))))
  10104. (defun org-match-any-p (re list)
  10105. "Does re match any element of list?"
  10106. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10107. (delq nil list))
  10108. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10109. (defvar org-tags-overlay (org-make-overlay 1 1))
  10110. (org-detach-overlay org-tags-overlay)
  10111. (defun org-get-local-tags-at (&optional pos)
  10112. "Get a list of tags defined in the current headline."
  10113. (org-get-tags-at pos 'local))
  10114. (defun org-get-local-tags ()
  10115. "Get a list of tags defined in the current headline."
  10116. (org-get-tags-at nil 'local))
  10117. (defun org-get-tags-at (&optional pos local)
  10118. "Get a list of all headline tags applicable at POS.
  10119. POS defaults to point. If tags are inherited, the list contains
  10120. the targets in the same sequence as the headlines appear, i.e.
  10121. the tags of the current headline come last.
  10122. When LOCAL is non-nil, only return tags from the current headline,
  10123. ignore inherited ones."
  10124. (interactive)
  10125. (if (and org-trust-scanner-tags
  10126. (or (not pos) (equal pos (point)))
  10127. (not local))
  10128. org-scanner-tags
  10129. (let (tags ltags lastpos parent)
  10130. (save-excursion
  10131. (save-restriction
  10132. (widen)
  10133. (goto-char (or pos (point)))
  10134. (save-match-data
  10135. (catch 'done
  10136. (condition-case nil
  10137. (progn
  10138. (org-back-to-heading t)
  10139. (while (not (equal lastpos (point)))
  10140. (setq lastpos (point))
  10141. (when (looking-at
  10142. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10143. (setq ltags (org-split-string
  10144. (org-match-string-no-properties 1) ":"))
  10145. (when parent
  10146. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10147. (setq tags (append
  10148. (if parent
  10149. (org-remove-uniherited-tags ltags)
  10150. ltags)
  10151. tags)))
  10152. (or org-use-tag-inheritance (throw 'done t))
  10153. (if local (throw 'done t))
  10154. (or (org-up-heading-safe) (error nil))
  10155. (setq parent t)))
  10156. (error nil)))))
  10157. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10158. (defun org-add-prop-inherited (s)
  10159. (add-text-properties 0 (length s) '(inherited t) s)
  10160. s)
  10161. (defun org-toggle-tag (tag &optional onoff)
  10162. "Toggle the tag TAG for the current line.
  10163. If ONOFF is `on' or `off', don't toggle but set to this state."
  10164. (let (res current)
  10165. (save-excursion
  10166. (org-back-to-heading t)
  10167. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10168. (point-at-eol) t)
  10169. (progn
  10170. (setq current (match-string 1))
  10171. (replace-match ""))
  10172. (setq current ""))
  10173. (setq current (nreverse (org-split-string current ":")))
  10174. (cond
  10175. ((eq onoff 'on)
  10176. (setq res t)
  10177. (or (member tag current) (push tag current)))
  10178. ((eq onoff 'off)
  10179. (or (not (member tag current)) (setq current (delete tag current))))
  10180. (t (if (member tag current)
  10181. (setq current (delete tag current))
  10182. (setq res t)
  10183. (push tag current))))
  10184. (end-of-line 1)
  10185. (if current
  10186. (progn
  10187. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10188. (org-set-tags nil t))
  10189. (delete-horizontal-space))
  10190. (run-hooks 'org-after-tags-change-hook))
  10191. res))
  10192. (defun org-align-tags-here (to-col)
  10193. ;; Assumes that this is a headline
  10194. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10195. (beginning-of-line 1)
  10196. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10197. (< pos (match-beginning 2)))
  10198. (progn
  10199. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10200. (goto-char (match-beginning 1))
  10201. (insert " ")
  10202. (delete-region (point) (1+ (match-beginning 2)))
  10203. (setq ncol (max (1+ (current-column))
  10204. (1+ col)
  10205. (if (> to-col 0)
  10206. to-col
  10207. (- (abs to-col) tags-l))))
  10208. (setq p (point))
  10209. (insert (make-string (- ncol (current-column)) ?\ ))
  10210. (setq ncol (current-column))
  10211. (when indent-tabs-mode (tabify p (point-at-eol)))
  10212. (org-move-to-column (min ncol col) t))
  10213. (goto-char pos))))
  10214. (defun org-set-tags-command (&optional arg just-align)
  10215. "Call the set-tags command for the current entry."
  10216. (interactive "P")
  10217. (if (org-on-heading-p)
  10218. (org-set-tags arg just-align)
  10219. (save-excursion
  10220. (org-back-to-heading t)
  10221. (org-set-tags arg just-align))))
  10222. (defun org-set-tags (&optional arg just-align)
  10223. "Set the tags for the current headline.
  10224. With prefix ARG, realign all tags in headings in the current buffer."
  10225. (interactive "P")
  10226. (let* ((re (concat "^" outline-regexp))
  10227. (current (org-get-tags-string))
  10228. (col (current-column))
  10229. (org-setting-tags t)
  10230. table current-tags inherited-tags ; computed below when needed
  10231. tags p0 c0 c1 rpl)
  10232. (if arg
  10233. (save-excursion
  10234. (goto-char (point-min))
  10235. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10236. (while (re-search-forward re nil t)
  10237. (org-set-tags nil t)
  10238. (end-of-line 1)))
  10239. (message "All tags realigned to column %d" org-tags-column))
  10240. (if just-align
  10241. (setq tags current)
  10242. ;; Get a new set of tags from the user
  10243. (save-excursion
  10244. (setq table (append org-tag-persistent-alist
  10245. (or org-tag-alist (org-get-buffer-tags)))
  10246. org-last-tags-completion-table table
  10247. current-tags (org-split-string current ":")
  10248. inherited-tags (nreverse
  10249. (nthcdr (length current-tags)
  10250. (nreverse (org-get-tags-at))))
  10251. tags
  10252. (if (or (eq t org-use-fast-tag-selection)
  10253. (and org-use-fast-tag-selection
  10254. (delq nil (mapcar 'cdr table))))
  10255. (org-fast-tag-selection
  10256. current-tags inherited-tags table
  10257. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10258. (let ((org-add-colon-after-tag-completion t))
  10259. (org-trim
  10260. (org-without-partial-completion
  10261. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10262. nil nil current 'org-tags-history)))))))
  10263. (while (string-match "[-+&]+" tags)
  10264. ;; No boolean logic, just a list
  10265. (setq tags (replace-match ":" t t tags))))
  10266. (if org-tags-sort-function
  10267. (setq tags (mapconcat 'identity
  10268. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10269. org-tags-sort-function) ":")))
  10270. (if (string-match "\\`[\t ]*\\'" tags)
  10271. (setq tags "")
  10272. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10273. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10274. ;; Insert new tags at the correct column
  10275. (beginning-of-line 1)
  10276. (cond
  10277. ((and (equal current "") (equal tags "")))
  10278. ((re-search-forward
  10279. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10280. (point-at-eol) t)
  10281. (if (equal tags "")
  10282. (setq rpl "")
  10283. (goto-char (match-beginning 0))
  10284. (setq c0 (current-column) p0 (point)
  10285. c1 (max (1+ c0) (if (> org-tags-column 0)
  10286. org-tags-column
  10287. (- (- org-tags-column) (length tags))))
  10288. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10289. (replace-match rpl t t)
  10290. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10291. tags)
  10292. (t (error "Tags alignment failed")))
  10293. (org-move-to-column col)
  10294. (unless just-align
  10295. (run-hooks 'org-after-tags-change-hook)))))
  10296. (defun org-change-tag-in-region (beg end tag off)
  10297. "Add or remove TAG for each entry in the region.
  10298. This works in the agenda, and also in an org-mode buffer."
  10299. (interactive
  10300. (list (region-beginning) (region-end)
  10301. (let ((org-last-tags-completion-table
  10302. (if (org-mode-p)
  10303. (org-get-buffer-tags)
  10304. (org-global-tags-completion-table))))
  10305. (org-icompleting-read
  10306. "Tag: " 'org-tags-completion-function nil nil nil
  10307. 'org-tags-history))
  10308. (progn
  10309. (message "[s]et or [r]emove? ")
  10310. (equal (read-char-exclusive) ?r))))
  10311. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10312. (let ((agendap (equal major-mode 'org-agenda-mode))
  10313. l1 l2 m buf pos newhead (cnt 0))
  10314. (goto-char end)
  10315. (setq l2 (1- (org-current-line)))
  10316. (goto-char beg)
  10317. (setq l1 (org-current-line))
  10318. (loop for l from l1 to l2 do
  10319. (org-goto-line l)
  10320. (setq m (get-text-property (point) 'org-hd-marker))
  10321. (when (or (and (org-mode-p) (org-on-heading-p))
  10322. (and agendap m))
  10323. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10324. pos (if agendap m (point)))
  10325. (with-current-buffer buf
  10326. (save-excursion
  10327. (save-restriction
  10328. (goto-char pos)
  10329. (setq cnt (1+ cnt))
  10330. (org-toggle-tag tag (if off 'off 'on))
  10331. (setq newhead (org-get-heading)))))
  10332. (and agendap (org-agenda-change-all-lines newhead m))))
  10333. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10334. (defun org-tags-completion-function (string predicate &optional flag)
  10335. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10336. (confirm (lambda (x) (stringp (car x)))))
  10337. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10338. (setq s1 (match-string 1 string)
  10339. s2 (match-string 2 string))
  10340. (setq s1 "" s2 string))
  10341. (cond
  10342. ((eq flag nil)
  10343. ;; try completion
  10344. (setq rtn (try-completion s2 ctable confirm))
  10345. (if (stringp rtn)
  10346. (setq rtn
  10347. (concat s1 s2 (substring rtn (length s2))
  10348. (if (and org-add-colon-after-tag-completion
  10349. (assoc rtn ctable))
  10350. ":" ""))))
  10351. rtn)
  10352. ((eq flag t)
  10353. ;; all-completions
  10354. (all-completions s2 ctable confirm)
  10355. )
  10356. ((eq flag 'lambda)
  10357. ;; exact match?
  10358. (assoc s2 ctable)))
  10359. ))
  10360. (defun org-fast-tag-insert (kwd tags face &optional end)
  10361. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10362. (insert (format "%-12s" (concat kwd ":"))
  10363. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10364. (or end "")))
  10365. (defun org-fast-tag-show-exit (flag)
  10366. (save-excursion
  10367. (org-goto-line 3)
  10368. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10369. (replace-match ""))
  10370. (when flag
  10371. (end-of-line 1)
  10372. (org-move-to-column (- (window-width) 19) t)
  10373. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10374. (defun org-set-current-tags-overlay (current prefix)
  10375. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10376. (if (featurep 'xemacs)
  10377. (org-overlay-display org-tags-overlay (concat prefix s)
  10378. 'secondary-selection)
  10379. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10380. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10381. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10382. "Fast tag selection with single keys.
  10383. CURRENT is the current list of tags in the headline, INHERITED is the
  10384. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10385. possibly with grouping information. TODO-TABLE is a similar table with
  10386. TODO keywords, should these have keys assigned to them.
  10387. If the keys are nil, a-z are automatically assigned.
  10388. Returns the new tags string, or nil to not change the current settings."
  10389. (let* ((fulltable (append table todo-table))
  10390. (maxlen (apply 'max (mapcar
  10391. (lambda (x)
  10392. (if (stringp (car x)) (string-width (car x)) 0))
  10393. fulltable)))
  10394. (buf (current-buffer))
  10395. (expert (eq org-fast-tag-selection-single-key 'expert))
  10396. (buffer-tags nil)
  10397. (fwidth (+ maxlen 3 1 3))
  10398. (ncol (/ (- (window-width) 4) fwidth))
  10399. (i-face 'org-done)
  10400. (c-face 'org-todo)
  10401. tg cnt e c char c1 c2 ntable tbl rtn
  10402. ov-start ov-end ov-prefix
  10403. (exit-after-next org-fast-tag-selection-single-key)
  10404. (done-keywords org-done-keywords)
  10405. groups ingroup)
  10406. (save-excursion
  10407. (beginning-of-line 1)
  10408. (if (looking-at
  10409. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10410. (setq ov-start (match-beginning 1)
  10411. ov-end (match-end 1)
  10412. ov-prefix "")
  10413. (setq ov-start (1- (point-at-eol))
  10414. ov-end (1+ ov-start))
  10415. (skip-chars-forward "^\n\r")
  10416. (setq ov-prefix
  10417. (concat
  10418. (buffer-substring (1- (point)) (point))
  10419. (if (> (current-column) org-tags-column)
  10420. " "
  10421. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10422. (org-move-overlay org-tags-overlay ov-start ov-end)
  10423. (save-window-excursion
  10424. (if expert
  10425. (set-buffer (get-buffer-create " *Org tags*"))
  10426. (delete-other-windows)
  10427. (split-window-vertically)
  10428. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10429. (erase-buffer)
  10430. (org-set-local 'org-done-keywords done-keywords)
  10431. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10432. (org-fast-tag-insert "Current" current c-face "\n\n")
  10433. (org-fast-tag-show-exit exit-after-next)
  10434. (org-set-current-tags-overlay current ov-prefix)
  10435. (setq tbl fulltable char ?a cnt 0)
  10436. (while (setq e (pop tbl))
  10437. (cond
  10438. ((equal e '(:startgroup))
  10439. (push '() groups) (setq ingroup t)
  10440. (when (not (= cnt 0))
  10441. (setq cnt 0)
  10442. (insert "\n"))
  10443. (insert "{ "))
  10444. ((equal e '(:endgroup))
  10445. (setq ingroup nil cnt 0)
  10446. (insert "}\n"))
  10447. ((equal e '(:newline))
  10448. (when (not (= cnt 0))
  10449. (setq cnt 0)
  10450. (insert "\n")
  10451. (setq e (car tbl))
  10452. (while (equal (car tbl) '(:newline))
  10453. (insert "\n")
  10454. (setq tbl (cdr tbl)))))
  10455. (t
  10456. (setq tg (copy-sequence (car e)) c2 nil)
  10457. (if (cdr e)
  10458. (setq c (cdr e))
  10459. ;; automatically assign a character.
  10460. (setq c1 (string-to-char
  10461. (downcase (substring
  10462. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10463. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10464. (while (or (rassoc char ntable) (rassoc char table))
  10465. (setq char (1+ char)))
  10466. (setq c2 c1))
  10467. (setq c (or c2 char)))
  10468. (if ingroup (push tg (car groups)))
  10469. (setq tg (org-add-props tg nil 'face
  10470. (cond
  10471. ((not (assoc tg table))
  10472. (org-get-todo-face tg))
  10473. ((member tg current) c-face)
  10474. ((member tg inherited) i-face)
  10475. (t nil))))
  10476. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10477. (insert "[" c "] " tg (make-string
  10478. (- fwidth 4 (length tg)) ?\ ))
  10479. (push (cons tg c) ntable)
  10480. (when (= (setq cnt (1+ cnt)) ncol)
  10481. (insert "\n")
  10482. (if ingroup (insert " "))
  10483. (setq cnt 0)))))
  10484. (setq ntable (nreverse ntable))
  10485. (insert "\n")
  10486. (goto-char (point-min))
  10487. (if (not expert) (org-fit-window-to-buffer))
  10488. (setq rtn
  10489. (catch 'exit
  10490. (while t
  10491. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10492. (if groups " [!] no groups" " [!]groups")
  10493. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10494. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10495. (cond
  10496. ((= c ?\r) (throw 'exit t))
  10497. ((= c ?!)
  10498. (setq groups (not groups))
  10499. (goto-char (point-min))
  10500. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10501. ((= c ?\C-c)
  10502. (if (not expert)
  10503. (org-fast-tag-show-exit
  10504. (setq exit-after-next (not exit-after-next)))
  10505. (setq expert nil)
  10506. (delete-other-windows)
  10507. (split-window-vertically)
  10508. (org-switch-to-buffer-other-window " *Org tags*")
  10509. (org-fit-window-to-buffer)))
  10510. ((or (= c ?\C-g)
  10511. (and (= c ?q) (not (rassoc c ntable))))
  10512. (org-detach-overlay org-tags-overlay)
  10513. (setq quit-flag t))
  10514. ((= c ?\ )
  10515. (setq current nil)
  10516. (if exit-after-next (setq exit-after-next 'now)))
  10517. ((= c ?\t)
  10518. (condition-case nil
  10519. (setq tg (org-icompleting-read
  10520. "Tag: "
  10521. (or buffer-tags
  10522. (with-current-buffer buf
  10523. (org-get-buffer-tags)))))
  10524. (quit (setq tg "")))
  10525. (when (string-match "\\S-" tg)
  10526. (add-to-list 'buffer-tags (list tg))
  10527. (if (member tg current)
  10528. (setq current (delete tg current))
  10529. (push tg current)))
  10530. (if exit-after-next (setq exit-after-next 'now)))
  10531. ((setq e (rassoc c todo-table) tg (car e))
  10532. (with-current-buffer buf
  10533. (save-excursion (org-todo tg)))
  10534. (if exit-after-next (setq exit-after-next 'now)))
  10535. ((setq e (rassoc c ntable) tg (car e))
  10536. (if (member tg current)
  10537. (setq current (delete tg current))
  10538. (loop for g in groups do
  10539. (if (member tg g)
  10540. (mapc (lambda (x)
  10541. (setq current (delete x current)))
  10542. g)))
  10543. (push tg current))
  10544. (if exit-after-next (setq exit-after-next 'now))))
  10545. ;; Create a sorted list
  10546. (setq current
  10547. (sort current
  10548. (lambda (a b)
  10549. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10550. (if (eq exit-after-next 'now) (throw 'exit t))
  10551. (goto-char (point-min))
  10552. (beginning-of-line 2)
  10553. (delete-region (point) (point-at-eol))
  10554. (org-fast-tag-insert "Current" current c-face)
  10555. (org-set-current-tags-overlay current ov-prefix)
  10556. (while (re-search-forward
  10557. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10558. (setq tg (match-string 1))
  10559. (add-text-properties
  10560. (match-beginning 1) (match-end 1)
  10561. (list 'face
  10562. (cond
  10563. ((member tg current) c-face)
  10564. ((member tg inherited) i-face)
  10565. (t (get-text-property (match-beginning 1) 'face))))))
  10566. (goto-char (point-min)))))
  10567. (org-detach-overlay org-tags-overlay)
  10568. (if rtn
  10569. (mapconcat 'identity current ":")
  10570. nil))))
  10571. (defun org-get-tags-string ()
  10572. "Get the TAGS string in the current headline."
  10573. (unless (org-on-heading-p t)
  10574. (error "Not on a heading"))
  10575. (save-excursion
  10576. (beginning-of-line 1)
  10577. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10578. (org-match-string-no-properties 1)
  10579. "")))
  10580. (defun org-get-tags ()
  10581. "Get the list of tags specified in the current headline."
  10582. (org-split-string (org-get-tags-string) ":"))
  10583. (defun org-get-buffer-tags ()
  10584. "Get a table of all tags used in the buffer, for completion."
  10585. (let (tags)
  10586. (save-excursion
  10587. (goto-char (point-min))
  10588. (while (re-search-forward
  10589. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10590. (when (equal (char-after (point-at-bol 0)) ?*)
  10591. (mapc (lambda (x) (add-to-list 'tags x))
  10592. (org-split-string (org-match-string-no-properties 1) ":")))))
  10593. (mapcar 'list tags)))
  10594. ;;;; The mapping API
  10595. ;;;###autoload
  10596. (defun org-map-entries (func &optional match scope &rest skip)
  10597. "Call FUNC at each headline selected by MATCH in SCOPE.
  10598. FUNC is a function or a lisp form. The function will be called without
  10599. arguments, with the cursor positioned at the beginning of the headline.
  10600. The return values of all calls to the function will be collected and
  10601. returned as a list.
  10602. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10603. does not need to preserve point. After evaluation, the cursor will be
  10604. moved to the end of the line (presumably of the headline of the
  10605. processed entry) and search continues from there. Under some
  10606. circumstances, this may not produce the wanted results. For example,
  10607. if you have removed (e.g. archived) the current (sub)tree it could
  10608. mean that the next entry will be skipped entirely. In such cases, you
  10609. can specify the position from where search should continue by making
  10610. FUNC set the variable `org-map-continue-from' to the desired buffer
  10611. position.
  10612. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10613. Only headlines that are matched by this query will be considered during
  10614. the iteration. When MATCH is nil or t, all headlines will be
  10615. visited by the iteration.
  10616. SCOPE determines the scope of this command. It can be any of:
  10617. nil The current buffer, respecting the restriction if any
  10618. tree The subtree started with the entry at point
  10619. file The current buffer, without restriction
  10620. file-with-archives
  10621. The current buffer, and any archives associated with it
  10622. agenda All agenda files
  10623. agenda-with-archives
  10624. All agenda files with any archive files associated with them
  10625. \(file1 file2 ...)
  10626. If this is a list, all files in the list will be scanned
  10627. The remaining args are treated as settings for the skipping facilities of
  10628. the scanner. The following items can be given here:
  10629. archive skip trees with the archive tag.
  10630. comment skip trees with the COMMENT keyword
  10631. function or Emacs Lisp form:
  10632. will be used as value for `org-agenda-skip-function', so whenever
  10633. the the function returns t, FUNC will not be called for that
  10634. entry and search will continue from the point where the
  10635. function leaves it.
  10636. If your function needs to retrieve the tags including inherited tags
  10637. at the *current* entry, you can use the value of the variable
  10638. `org-scanner-tags' which will be much faster than getting the value
  10639. with `org-get-tags-at'. If your function gets properties with
  10640. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10641. to t around the call to `org-entry-properties' to get the same speedup.
  10642. Note that if your function moves around to retrieve tags and properties at
  10643. a *different* entry, you cannot use these techniques."
  10644. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10645. (org-agenda-skip-archived-trees (memq 'archive skip))
  10646. (org-agenda-skip-comment-trees (memq 'comment skip))
  10647. (org-agenda-skip-function
  10648. (car (org-delete-all '(comment archive) skip)))
  10649. (org-tags-match-list-sublevels t)
  10650. matcher file res
  10651. org-todo-keywords-for-agenda
  10652. org-done-keywords-for-agenda
  10653. org-todo-keyword-alist-for-agenda
  10654. org-tag-alist-for-agenda)
  10655. (cond
  10656. ((eq match t) (setq matcher t))
  10657. ((eq match nil) (setq matcher t))
  10658. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10659. (save-excursion
  10660. (save-restriction
  10661. (when (eq scope 'tree)
  10662. (org-back-to-heading t)
  10663. (org-narrow-to-subtree)
  10664. (setq scope nil))
  10665. (if (not scope)
  10666. (progn
  10667. (org-prepare-agenda-buffers
  10668. (list (buffer-file-name (current-buffer))))
  10669. (setq res (org-scan-tags func matcher)))
  10670. ;; Get the right scope
  10671. (cond
  10672. ((and scope (listp scope) (symbolp (car scope)))
  10673. (setq scope (eval scope)))
  10674. ((eq scope 'agenda)
  10675. (setq scope (org-agenda-files t)))
  10676. ((eq scope 'agenda-with-archives)
  10677. (setq scope (org-agenda-files t))
  10678. (setq scope (org-add-archive-files scope)))
  10679. ((eq scope 'file)
  10680. (setq scope (list (buffer-file-name))))
  10681. ((eq scope 'file-with-archives)
  10682. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10683. (org-prepare-agenda-buffers scope)
  10684. (while (setq file (pop scope))
  10685. (with-current-buffer (org-find-base-buffer-visiting file)
  10686. (save-excursion
  10687. (save-restriction
  10688. (widen)
  10689. (goto-char (point-min))
  10690. (setq res (append res (org-scan-tags func matcher))))))))))
  10691. res))
  10692. ;;;; Properties
  10693. ;;; Setting and retrieving properties
  10694. (defconst org-special-properties
  10695. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10696. "TIMESTAMP" "TIMESTAMP_IA")
  10697. "The special properties valid in Org-mode.
  10698. These are properties that are not defined in the property drawer,
  10699. but in some other way.")
  10700. (defconst org-default-properties
  10701. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10702. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10703. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10704. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10705. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10706. "CLOCK_MODELINE_TOTAL")
  10707. "Some properties that are used by Org-mode for various purposes.
  10708. Being in this list makes sure that they are offered for completion.")
  10709. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10710. "Regular expression matching the first line of a property drawer.")
  10711. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10712. "Regular expression matching the first line of a property drawer.")
  10713. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10714. "Regular expression matching the first line of a property drawer.")
  10715. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10716. "Regular expression matching the first line of a property drawer.")
  10717. (defconst org-property-drawer-re
  10718. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10719. org-property-end-re "\\)\n?")
  10720. "Matches an entire property drawer.")
  10721. (defconst org-clock-drawer-re
  10722. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10723. org-property-end-re "\\)\n?")
  10724. "Matches an entire clock drawer.")
  10725. (defun org-property-action ()
  10726. "Do an action on properties."
  10727. (interactive)
  10728. (let (c)
  10729. (org-at-property-p)
  10730. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10731. (setq c (read-char-exclusive))
  10732. (cond
  10733. ((equal c ?s)
  10734. (call-interactively 'org-set-property))
  10735. ((equal c ?d)
  10736. (call-interactively 'org-delete-property))
  10737. ((equal c ?D)
  10738. (call-interactively 'org-delete-property-globally))
  10739. ((equal c ?c)
  10740. (call-interactively 'org-compute-property-at-point))
  10741. (t (error "No such property action %c" c)))))
  10742. (defun org-set-effort (&optional value)
  10743. "Set the effort property of the current entry.
  10744. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  10745. allowed value."
  10746. (interactive "P")
  10747. (if (equal value 0) (setq value 10))
  10748. (let* ((completion-ignore-case t)
  10749. (prop org-effort-property)
  10750. (cur (org-entry-get nil prop))
  10751. (allowed (org-property-get-allowed-values nil prop 'table))
  10752. (existing (mapcar 'list (org-property-values prop)))
  10753. (val (cond
  10754. ((stringp value) value)
  10755. ((and allowed (integerp value))
  10756. (or (car (nth (1- value) allowed))
  10757. (car (org-last allowed))))
  10758. (allowed
  10759. (org-completing-read "Value: " allowed nil 'req-match))
  10760. (t
  10761. (let (org-completion-use-ido org-completion-use-iswitchb)
  10762. (org-completing-read
  10763. (concat "Value " (if (and cur (string-match "\\S-" cur))
  10764. (concat "[" cur "]") "")
  10765. ": ")
  10766. existing nil nil "" nil cur))))))
  10767. (unless (equal (org-entry-get nil prop) val)
  10768. (org-entry-put nil prop val))
  10769. (message "%s is now %s" prop val)))
  10770. (defun org-at-property-p ()
  10771. "Is the cursor in a property line?"
  10772. ;; FIXME: Does not check if we are actually in the drawer.
  10773. ;; FIXME: also returns true on any drawers.....
  10774. ;; This is used by C-c C-c for property action.
  10775. (save-excursion
  10776. (beginning-of-line 1)
  10777. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10778. (defun org-get-property-block (&optional beg end force)
  10779. "Return the (beg . end) range of the body of the property drawer.
  10780. BEG and END can be beginning and end of subtree, if not given
  10781. they will be found.
  10782. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10783. (catch 'exit
  10784. (save-excursion
  10785. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10786. (end (or end (progn (outline-next-heading) (point)))))
  10787. (goto-char beg)
  10788. (if (re-search-forward org-property-start-re end t)
  10789. (setq beg (1+ (match-end 0)))
  10790. (if force
  10791. (save-excursion
  10792. (org-insert-property-drawer)
  10793. (setq end (progn (outline-next-heading) (point))))
  10794. (throw 'exit nil))
  10795. (goto-char beg)
  10796. (if (re-search-forward org-property-start-re end t)
  10797. (setq beg (1+ (match-end 0)))))
  10798. (if (re-search-forward org-property-end-re end t)
  10799. (setq end (match-beginning 0))
  10800. (or force (throw 'exit nil))
  10801. (goto-char beg)
  10802. (setq end beg)
  10803. (org-indent-line-function)
  10804. (insert ":END:\n"))
  10805. (cons beg end)))))
  10806. (defun org-entry-properties (&optional pom which)
  10807. "Get all properties of the entry at point-or-marker POM.
  10808. This includes the TODO keyword, the tags, time strings for deadline,
  10809. scheduled, and clocking, and any additional properties defined in the
  10810. entry. The return value is an alist, keys may occur multiple times
  10811. if the property key was used several times.
  10812. POM may also be nil, in which case the current entry is used.
  10813. If WHICH is nil or `all', get all properties. If WHICH is
  10814. `special' or `standard', only get that subclass."
  10815. (setq which (or which 'all))
  10816. (org-with-point-at pom
  10817. (let ((clockstr (substring org-clock-string 0 -1))
  10818. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10819. beg end range props sum-props key value string clocksum)
  10820. (save-excursion
  10821. (when (condition-case nil
  10822. (and (org-mode-p) (org-back-to-heading t))
  10823. (error nil))
  10824. (setq beg (point))
  10825. (setq sum-props (get-text-property (point) 'org-summaries))
  10826. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10827. (outline-next-heading)
  10828. (setq end (point))
  10829. (when (memq which '(all special))
  10830. ;; Get the special properties, like TODO and tags
  10831. (goto-char beg)
  10832. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10833. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10834. (when (looking-at org-priority-regexp)
  10835. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10836. (when (and (setq value (org-get-tags-string))
  10837. (string-match "\\S-" value))
  10838. (push (cons "TAGS" value) props))
  10839. (when (setq value (org-get-tags-at))
  10840. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10841. props))
  10842. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10843. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10844. string (if (equal key clockstr)
  10845. (org-no-properties
  10846. (org-trim
  10847. (buffer-substring
  10848. (match-beginning 3) (goto-char (point-at-eol)))))
  10849. (substring (org-match-string-no-properties 3) 1 -1)))
  10850. (unless key
  10851. (if (= (char-after (match-beginning 3)) ?\[)
  10852. (setq key "TIMESTAMP_IA")
  10853. (setq key "TIMESTAMP")))
  10854. (when (or (equal key clockstr) (not (assoc key props)))
  10855. (push (cons key string) props)))
  10856. )
  10857. (when (memq which '(all standard))
  10858. ;; Get the standard properties, like :PROP: ...
  10859. (setq range (org-get-property-block beg end))
  10860. (when range
  10861. (goto-char (car range))
  10862. (while (re-search-forward
  10863. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10864. (cdr range) t)
  10865. (setq key (org-match-string-no-properties 1)
  10866. value (org-trim (or (org-match-string-no-properties 2) "")))
  10867. (unless (member key excluded)
  10868. (push (cons key (or value "")) props)))))
  10869. (if clocksum
  10870. (push (cons "CLOCKSUM"
  10871. (org-columns-number-to-string (/ (float clocksum) 60.)
  10872. 'add_times))
  10873. props))
  10874. (unless (assoc "CATEGORY" props)
  10875. (setq value (or (org-get-category)
  10876. (progn (org-refresh-category-properties)
  10877. (org-get-category))))
  10878. (push (cons "CATEGORY" value) props))
  10879. (append sum-props (nreverse props)))))))
  10880. (defun org-entry-get (pom property &optional inherit)
  10881. "Get value of PROPERTY for entry at point-or-marker POM.
  10882. If INHERIT is non-nil and the entry does not have the property,
  10883. then also check higher levels of the hierarchy.
  10884. If INHERIT is the symbol `selective', use inheritance only if the setting
  10885. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10886. If the property is present but empty, the return value is the empty string.
  10887. If the property is not present at all, nil is returned."
  10888. (org-with-point-at pom
  10889. (if (and inherit (if (eq inherit 'selective)
  10890. (org-property-inherit-p property)
  10891. t))
  10892. (org-entry-get-with-inheritance property)
  10893. (if (member property org-special-properties)
  10894. ;; We need a special property. Use brute force, get all properties.
  10895. (cdr (assoc property (org-entry-properties nil 'special)))
  10896. (let ((range (org-get-property-block)))
  10897. (if (and range
  10898. (goto-char (car range))
  10899. (re-search-forward
  10900. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10901. (cdr range) t))
  10902. ;; Found the property, return it.
  10903. (if (match-end 1)
  10904. (org-match-string-no-properties 1)
  10905. "")))))))
  10906. (defun org-property-or-variable-value (var &optional inherit)
  10907. "Check if there is a property fixing the value of VAR.
  10908. If yes, return this value. If not, return the current value of the variable."
  10909. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10910. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10911. (read prop)
  10912. (symbol-value var))))
  10913. (defun org-entry-delete (pom property)
  10914. "Delete the property PROPERTY from entry at point-or-marker POM."
  10915. (org-with-point-at pom
  10916. (if (member property org-special-properties)
  10917. nil ; cannot delete these properties.
  10918. (let ((range (org-get-property-block)))
  10919. (if (and range
  10920. (goto-char (car range))
  10921. (re-search-forward
  10922. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10923. (cdr range) t))
  10924. (progn
  10925. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10926. t)
  10927. nil)))))
  10928. ;; Multi-values properties are properties that contain multiple values
  10929. ;; These values are assumed to be single words, separated by whitespace.
  10930. (defun org-entry-add-to-multivalued-property (pom property value)
  10931. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10932. (let* ((old (org-entry-get pom property))
  10933. (values (and old (org-split-string old "[ \t]"))))
  10934. (setq value (org-entry-protect-space value))
  10935. (unless (member value values)
  10936. (setq values (cons value values))
  10937. (org-entry-put pom property
  10938. (mapconcat 'identity values " ")))))
  10939. (defun org-entry-remove-from-multivalued-property (pom property value)
  10940. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10941. (let* ((old (org-entry-get pom property))
  10942. (values (and old (org-split-string old "[ \t]"))))
  10943. (setq value (org-entry-protect-space value))
  10944. (when (member value values)
  10945. (setq values (delete value values))
  10946. (org-entry-put pom property
  10947. (mapconcat 'identity values " ")))))
  10948. (defun org-entry-member-in-multivalued-property (pom property value)
  10949. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10950. (let* ((old (org-entry-get pom property))
  10951. (values (and old (org-split-string old "[ \t]"))))
  10952. (setq value (org-entry-protect-space value))
  10953. (member value values)))
  10954. (defun org-entry-get-multivalued-property (pom property)
  10955. "Return a list of values in a multivalued property."
  10956. (let* ((value (org-entry-get pom property))
  10957. (values (and value (org-split-string value "[ \t]"))))
  10958. (mapcar 'org-entry-restore-space values)))
  10959. (defun org-entry-put-multivalued-property (pom property &rest values)
  10960. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10961. VALUES should be a list of strings. Spaces will be protected."
  10962. (org-entry-put pom property
  10963. (mapconcat 'org-entry-protect-space values " "))
  10964. (let* ((value (org-entry-get pom property))
  10965. (values (and value (org-split-string value "[ \t]"))))
  10966. (mapcar 'org-entry-restore-space values)))
  10967. (defun org-entry-protect-space (s)
  10968. "Protect spaces and newline in string S."
  10969. (while (string-match " " s)
  10970. (setq s (replace-match "%20" t t s)))
  10971. (while (string-match "\n" s)
  10972. (setq s (replace-match "%0A" t t s)))
  10973. s)
  10974. (defun org-entry-restore-space (s)
  10975. "Restore spaces and newline in string S."
  10976. (while (string-match "%20" s)
  10977. (setq s (replace-match " " t t s)))
  10978. (while (string-match "%0A" s)
  10979. (setq s (replace-match "\n" t t s)))
  10980. s)
  10981. (defvar org-entry-property-inherited-from (make-marker)
  10982. "Marker pointing to the entry from where a property was inherited.
  10983. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10984. location of the entry where the inheritance search matched. If there was
  10985. no match, the marker will point nowhere.
  10986. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10987. is set.")
  10988. (defun org-entry-get-with-inheritance (property)
  10989. "Get entry property, and search higher levels if not present."
  10990. (move-marker org-entry-property-inherited-from nil)
  10991. (let (tmp)
  10992. (save-excursion
  10993. (save-restriction
  10994. (widen)
  10995. (catch 'ex
  10996. (while t
  10997. (when (setq tmp (org-entry-get nil property))
  10998. (org-back-to-heading t)
  10999. (move-marker org-entry-property-inherited-from (point))
  11000. (throw 'ex tmp))
  11001. (or (org-up-heading-safe) (throw 'ex nil)))))
  11002. (or tmp
  11003. (cdr (assoc property org-file-properties))
  11004. (cdr (assoc property org-global-properties))
  11005. (cdr (assoc property org-global-properties-fixed))))))
  11006. (defun org-entry-put (pom property value)
  11007. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11008. (org-with-point-at pom
  11009. (org-back-to-heading t)
  11010. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11011. range)
  11012. (cond
  11013. ((equal property "TODO")
  11014. (when (and (stringp value) (string-match "\\S-" value)
  11015. (not (member value org-todo-keywords-1)))
  11016. (error "\"%s\" is not a valid TODO state" value))
  11017. (if (or (not value)
  11018. (not (string-match "\\S-" value)))
  11019. (setq value 'none))
  11020. (org-todo value)
  11021. (org-set-tags nil 'align))
  11022. ((equal property "PRIORITY")
  11023. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11024. (string-to-char value) ?\ ))
  11025. (org-set-tags nil 'align))
  11026. ((equal property "SCHEDULED")
  11027. (if (re-search-forward org-scheduled-time-regexp end t)
  11028. (cond
  11029. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11030. ((eq value 'later) (org-timestamp-change 1 'day))
  11031. (t (call-interactively 'org-schedule)))
  11032. (call-interactively 'org-schedule)))
  11033. ((equal property "DEADLINE")
  11034. (if (re-search-forward org-deadline-time-regexp end t)
  11035. (cond
  11036. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11037. ((eq value 'later) (org-timestamp-change 1 'day))
  11038. (t (call-interactively 'org-deadline)))
  11039. (call-interactively 'org-deadline)))
  11040. ((member property org-special-properties)
  11041. (error "The %s property can not yet be set with `org-entry-put'"
  11042. property))
  11043. (t ; a non-special property
  11044. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11045. (setq range (org-get-property-block beg end 'force))
  11046. (goto-char (car range))
  11047. (if (re-search-forward
  11048. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11049. (progn
  11050. (delete-region (match-beginning 1) (match-end 1))
  11051. (goto-char (match-beginning 1)))
  11052. (goto-char (cdr range))
  11053. (insert "\n")
  11054. (backward-char 1)
  11055. (org-indent-line-function)
  11056. (insert ":" property ":"))
  11057. (and value (insert " " value))
  11058. (org-indent-line-function)))))))
  11059. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11060. "Get all property keys in the current buffer.
  11061. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11062. like tags and TODO state.
  11063. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11064. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11065. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11066. formats in the current buffer."
  11067. (let (rtn range cfmt s p)
  11068. (save-excursion
  11069. (save-restriction
  11070. (widen)
  11071. (goto-char (point-min))
  11072. (while (re-search-forward org-property-start-re nil t)
  11073. (setq range (org-get-property-block))
  11074. (goto-char (car range))
  11075. (while (re-search-forward
  11076. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11077. (cdr range) t)
  11078. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11079. (outline-next-heading))))
  11080. (when include-specials
  11081. (setq rtn (append org-special-properties rtn)))
  11082. (when include-defaults
  11083. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11084. (add-to-list 'rtn org-effort-property))
  11085. (when include-columns
  11086. (save-excursion
  11087. (save-restriction
  11088. (widen)
  11089. (goto-char (point-min))
  11090. (while (re-search-forward
  11091. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11092. nil t)
  11093. (setq cfmt (match-string 2) s 0)
  11094. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11095. cfmt s)
  11096. (setq s (match-end 0)
  11097. p (match-string 1 cfmt))
  11098. (unless (or (equal p "ITEM")
  11099. (member p org-special-properties))
  11100. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11101. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11102. (defun org-property-values (key)
  11103. "Return a list of all values of property KEY."
  11104. (save-excursion
  11105. (save-restriction
  11106. (widen)
  11107. (goto-char (point-min))
  11108. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11109. values)
  11110. (while (re-search-forward re nil t)
  11111. (add-to-list 'values (org-trim (match-string 1))))
  11112. (delete "" values)))))
  11113. (defun org-insert-property-drawer ()
  11114. "Insert a property drawer into the current entry."
  11115. (interactive)
  11116. (org-back-to-heading t)
  11117. (looking-at outline-regexp)
  11118. (let ((indent (if org-adapt-indentation
  11119. (- (match-end 0)(match-beginning 0))
  11120. 0))
  11121. (beg (point))
  11122. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11123. end hiddenp)
  11124. (outline-next-heading)
  11125. (setq end (point))
  11126. (goto-char beg)
  11127. (while (re-search-forward re end t))
  11128. (setq hiddenp (org-invisible-p))
  11129. (end-of-line 1)
  11130. (and (equal (char-after) ?\n) (forward-char 1))
  11131. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11132. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11133. ;; just skip this line
  11134. (beginning-of-line 2)
  11135. ;; Drawer start, find the end
  11136. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11137. (beginning-of-line 1)))
  11138. (org-skip-over-state-notes)
  11139. (skip-chars-backward " \t\n\r")
  11140. (if (eq (char-before) ?*) (forward-char 1))
  11141. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11142. (beginning-of-line 0)
  11143. (org-indent-to-column indent)
  11144. (beginning-of-line 2)
  11145. (org-indent-to-column indent)
  11146. (beginning-of-line 0)
  11147. (if hiddenp
  11148. (save-excursion
  11149. (org-back-to-heading t)
  11150. (hide-entry))
  11151. (org-flag-drawer t))))
  11152. (defun org-set-property (property value)
  11153. "In the current entry, set PROPERTY to VALUE.
  11154. When called interactively, this will prompt for a property name, offering
  11155. completion on existing and default properties. And then it will prompt
  11156. for a value, offering completion either on allowed values (via an inherited
  11157. xxx_ALL property) or on existing values in other instances of this property
  11158. in the current file."
  11159. (interactive
  11160. (let* ((completion-ignore-case t)
  11161. (keys (org-buffer-property-keys nil t t))
  11162. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11163. (prop (if (member prop0 keys)
  11164. prop0
  11165. (or (cdr (assoc (downcase prop0)
  11166. (mapcar (lambda (x) (cons (downcase x) x))
  11167. keys)))
  11168. prop0)))
  11169. (cur (org-entry-get nil prop))
  11170. (allowed (org-property-get-allowed-values nil prop 'table))
  11171. (existing (mapcar 'list (org-property-values prop)))
  11172. (val (if allowed
  11173. (org-completing-read "Value: " allowed nil 'req-match)
  11174. (let (org-completion-use-ido org-completion-use-iswitchb)
  11175. (org-completing-read
  11176. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11177. (concat "[" cur "]") "")
  11178. ": ")
  11179. existing nil nil "" nil cur)))))
  11180. (list prop (if (equal val "") cur val))))
  11181. (unless (equal (org-entry-get nil property) value)
  11182. (org-entry-put nil property value)))
  11183. (defun org-delete-property (property)
  11184. "In the current entry, delete PROPERTY."
  11185. (interactive
  11186. (let* ((completion-ignore-case t)
  11187. (prop (org-icompleting-read
  11188. "Property: " (org-entry-properties nil 'standard))))
  11189. (list prop)))
  11190. (message "Property %s %s" property
  11191. (if (org-entry-delete nil property)
  11192. "deleted"
  11193. "was not present in the entry")))
  11194. (defun org-delete-property-globally (property)
  11195. "Remove PROPERTY globally, from all entries."
  11196. (interactive
  11197. (let* ((completion-ignore-case t)
  11198. (prop (org-icompleting-read
  11199. "Globally remove property: "
  11200. (mapcar 'list (org-buffer-property-keys)))))
  11201. (list prop)))
  11202. (save-excursion
  11203. (save-restriction
  11204. (widen)
  11205. (goto-char (point-min))
  11206. (let ((cnt 0))
  11207. (while (re-search-forward
  11208. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11209. nil t)
  11210. (setq cnt (1+ cnt))
  11211. (replace-match ""))
  11212. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11213. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11214. (defun org-compute-property-at-point ()
  11215. "Compute the property at point.
  11216. This looks for an enclosing column format, extracts the operator and
  11217. then applies it to the property in the column format's scope."
  11218. (interactive)
  11219. (unless (org-at-property-p)
  11220. (error "Not at a property"))
  11221. (let ((prop (org-match-string-no-properties 2)))
  11222. (org-columns-get-format-and-top-level)
  11223. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11224. (error "No operator defined for property %s" prop))
  11225. (org-columns-compute prop)))
  11226. (defun org-property-get-allowed-values (pom property &optional table)
  11227. "Get allowed values for the property PROPERTY.
  11228. When TABLE is non-nil, return an alist that can directly be used for
  11229. completion."
  11230. (let (vals)
  11231. (cond
  11232. ((equal property "TODO")
  11233. (setq vals (org-with-point-at pom
  11234. (append org-todo-keywords-1 '("")))))
  11235. ((equal property "PRIORITY")
  11236. (let ((n org-lowest-priority))
  11237. (while (>= n org-highest-priority)
  11238. (push (char-to-string n) vals)
  11239. (setq n (1- n)))))
  11240. ((member property org-special-properties))
  11241. (t
  11242. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11243. (when (and vals (string-match "\\S-" vals))
  11244. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11245. (setq vals (mapcar (lambda (x)
  11246. (cond ((stringp x) x)
  11247. ((numberp x) (number-to-string x))
  11248. ((symbolp x) (symbol-name x))
  11249. (t "???")))
  11250. vals)))))
  11251. (if table (mapcar 'list vals) vals)))
  11252. (defun org-property-previous-allowed-value (&optional previous)
  11253. "Switch to the next allowed value for this property."
  11254. (interactive)
  11255. (org-property-next-allowed-value t))
  11256. (defun org-property-next-allowed-value (&optional previous)
  11257. "Switch to the next allowed value for this property."
  11258. (interactive)
  11259. (unless (org-at-property-p)
  11260. (error "Not at a property"))
  11261. (let* ((key (match-string 2))
  11262. (value (match-string 3))
  11263. (allowed (or (org-property-get-allowed-values (point) key)
  11264. (and (member value '("[ ]" "[-]" "[X]"))
  11265. '("[ ]" "[X]"))))
  11266. nval)
  11267. (unless allowed
  11268. (error "Allowed values for this property have not been defined"))
  11269. (if previous (setq allowed (reverse allowed)))
  11270. (if (member value allowed)
  11271. (setq nval (car (cdr (member value allowed)))))
  11272. (setq nval (or nval (car allowed)))
  11273. (if (equal nval value)
  11274. (error "Only one allowed value for this property"))
  11275. (org-at-property-p)
  11276. (replace-match (concat " :" key ": " nval) t t)
  11277. (org-indent-line-function)
  11278. (beginning-of-line 1)
  11279. (skip-chars-forward " \t")))
  11280. (defun org-find-entry-with-id (ident)
  11281. "Locate the entry that contains the ID property with exact value IDENT.
  11282. IDENT can be a string, a symbol or a number, this function will search for
  11283. the string representation of it.
  11284. Return the position where this entry starts, or nil if there is no such entry."
  11285. (interactive "sID: ")
  11286. (let ((id (cond
  11287. ((stringp ident) ident)
  11288. ((symbol-name ident) (symbol-name ident))
  11289. ((numberp ident) (number-to-string ident))
  11290. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11291. (case-fold-search nil))
  11292. (save-excursion
  11293. (save-restriction
  11294. (widen)
  11295. (goto-char (point-min))
  11296. (when (re-search-forward
  11297. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11298. nil t)
  11299. (org-back-to-heading t)
  11300. (point))))))
  11301. ;;;; Timestamps
  11302. (defvar org-last-changed-timestamp nil)
  11303. (defvar org-last-inserted-timestamp nil
  11304. "The last time stamp inserted with `org-insert-time-stamp'.")
  11305. (defvar org-time-was-given) ; dynamically scoped parameter
  11306. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11307. (defvar org-ts-what) ; dynamically scoped parameter
  11308. (defun org-time-stamp (arg &optional inactive)
  11309. "Prompt for a date/time and insert a time stamp.
  11310. If the user specifies a time like HH:MM, or if this command is called
  11311. with a prefix argument, the time stamp will contain date and time.
  11312. Otherwise, only the date will be included. All parts of a date not
  11313. specified by the user will be filled in from the current date/time.
  11314. So if you press just return without typing anything, the time stamp
  11315. will represent the current date/time. If there is already a timestamp
  11316. at the cursor, it will be modified."
  11317. (interactive "P")
  11318. (let* ((ts nil)
  11319. (default-time
  11320. ;; Default time is either today, or, when entering a range,
  11321. ;; the range start.
  11322. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11323. (save-excursion
  11324. (re-search-backward
  11325. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11326. (- (point) 20) t)))
  11327. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11328. (current-time)))
  11329. (default-input (and ts (org-get-compact-tod ts)))
  11330. org-time-was-given org-end-time-was-given time)
  11331. (cond
  11332. ((and (org-at-timestamp-p t)
  11333. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11334. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11335. (insert "--")
  11336. (setq time (let ((this-command this-command))
  11337. (org-read-date arg 'totime nil nil
  11338. default-time default-input)))
  11339. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11340. ((org-at-timestamp-p t)
  11341. (setq time (let ((this-command this-command))
  11342. (org-read-date arg 'totime nil nil default-time default-input)))
  11343. (when (org-at-timestamp-p t) ; just to get the match data
  11344. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11345. (replace-match "")
  11346. (setq org-last-changed-timestamp
  11347. (org-insert-time-stamp
  11348. time (or org-time-was-given arg)
  11349. inactive nil nil (list org-end-time-was-given))))
  11350. (message "Timestamp updated"))
  11351. (t
  11352. (setq time (let ((this-command this-command))
  11353. (org-read-date arg 'totime nil nil default-time default-input)))
  11354. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11355. nil nil (list org-end-time-was-given))))))
  11356. ;; FIXME: can we use this for something else, like computing time differences?
  11357. (defun org-get-compact-tod (s)
  11358. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11359. (let* ((t1 (match-string 1 s))
  11360. (h1 (string-to-number (match-string 2 s)))
  11361. (m1 (string-to-number (match-string 3 s)))
  11362. (t2 (and (match-end 4) (match-string 5 s)))
  11363. (h2 (and t2 (string-to-number (match-string 6 s))))
  11364. (m2 (and t2 (string-to-number (match-string 7 s))))
  11365. dh dm)
  11366. (if (not t2)
  11367. t1
  11368. (setq dh (- h2 h1) dm (- m2 m1))
  11369. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11370. (concat t1 "+" (number-to-string dh)
  11371. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11372. (defun org-time-stamp-inactive (&optional arg)
  11373. "Insert an inactive time stamp.
  11374. An inactive time stamp is enclosed in square brackets instead of angle
  11375. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11376. does not link to the calendar and cannot be changed with the S-cursor keys.
  11377. So these are more for recording a certain time/date."
  11378. (interactive "P")
  11379. (org-time-stamp arg 'inactive))
  11380. (defvar org-date-ovl (org-make-overlay 1 1))
  11381. (org-overlay-put org-date-ovl 'face 'org-warning)
  11382. (org-detach-overlay org-date-ovl)
  11383. (defvar org-ans1) ; dynamically scoped parameter
  11384. (defvar org-ans2) ; dynamically scoped parameter
  11385. (defvar org-plain-time-of-day-regexp) ; defined below
  11386. (defvar org-overriding-default-time nil) ; dynamically scoped
  11387. (defvar org-read-date-overlay nil)
  11388. (defvar org-dcst nil) ; dynamically scoped
  11389. (defvar org-read-date-history nil)
  11390. (defvar org-read-date-final-answer nil)
  11391. (defun org-read-date (&optional with-time to-time from-string prompt
  11392. default-time default-input)
  11393. "Read a date, possibly a time, and make things smooth for the user.
  11394. The prompt will suggest to enter an ISO date, but you can also enter anything
  11395. which will at least partially be understood by `parse-time-string'.
  11396. Unrecognized parts of the date will default to the current day, month, year,
  11397. hour and minute. If this command is called to replace a timestamp at point,
  11398. of to enter the second timestamp of a range, the default time is taken from the
  11399. existing stamp. For example,
  11400. 3-2-5 --> 2003-02-05
  11401. feb 15 --> currentyear-02-15
  11402. sep 12 9 --> 2009-09-12
  11403. 12:45 --> today 12:45
  11404. 22 sept 0:34 --> currentyear-09-22 0:34
  11405. 12 --> currentyear-currentmonth-12
  11406. Fri --> nearest Friday (today or later)
  11407. etc.
  11408. Furthermore you can specify a relative date by giving, as the *first* thing
  11409. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11410. change in days weeks, months, years.
  11411. With a single plus or minus, the date is relative to today. With a double
  11412. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11413. +4d --> four days from today
  11414. +4 --> same as above
  11415. +2w --> two weeks from today
  11416. ++5 --> five days from default date
  11417. The function understands only English month and weekday abbreviations,
  11418. but this can be configured with the variables `parse-time-months' and
  11419. `parse-time-weekdays'.
  11420. While prompting, a calendar is popped up - you can also select the
  11421. date with the mouse (button 1). The calendar shows a period of three
  11422. months. To scroll it to other months, use the keys `>' and `<'.
  11423. If you don't like the calendar, turn it off with
  11424. \(setq org-read-date-popup-calendar nil)
  11425. With optional argument TO-TIME, the date will immediately be converted
  11426. to an internal time.
  11427. With an optional argument WITH-TIME, the prompt will suggest to also
  11428. insert a time. Note that when WITH-TIME is not set, you can still
  11429. enter a time, and this function will inform the calling routine about
  11430. this change. The calling routine may then choose to change the format
  11431. used to insert the time stamp into the buffer to include the time.
  11432. With optional argument FROM-STRING, read from this string instead from
  11433. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11434. the time/date that is used for everything that is not specified by the
  11435. user."
  11436. (require 'parse-time)
  11437. (let* ((org-time-stamp-rounding-minutes
  11438. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11439. (org-dcst org-display-custom-times)
  11440. (ct (org-current-time))
  11441. (def (or org-overriding-default-time default-time ct))
  11442. (defdecode (decode-time def))
  11443. (dummy (progn
  11444. (when (< (nth 2 defdecode) org-extend-today-until)
  11445. (setcar (nthcdr 2 defdecode) -1)
  11446. (setcar (nthcdr 1 defdecode) 59)
  11447. (setq def (apply 'encode-time defdecode)
  11448. defdecode (decode-time def)))))
  11449. (calendar-frame-setup nil)
  11450. (calendar-move-hook nil)
  11451. (calendar-view-diary-initially-flag nil)
  11452. (view-diary-entries-initially nil)
  11453. (calendar-view-holidays-initially-flag nil)
  11454. (view-calendar-holidays-initially nil)
  11455. (timestr (format-time-string
  11456. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11457. (prompt (concat (if prompt (concat prompt " ") "")
  11458. (format "Date+time [%s]: " timestr)))
  11459. ans (org-ans0 "") org-ans1 org-ans2 final)
  11460. (cond
  11461. (from-string (setq ans from-string))
  11462. (org-read-date-popup-calendar
  11463. (save-excursion
  11464. (save-window-excursion
  11465. (calendar)
  11466. (calendar-forward-day (- (time-to-days def)
  11467. (calendar-absolute-from-gregorian
  11468. (calendar-current-date))))
  11469. (org-eval-in-calendar nil t)
  11470. (let* ((old-map (current-local-map))
  11471. (map (copy-keymap calendar-mode-map))
  11472. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11473. (org-defkey map (kbd "RET") 'org-calendar-select)
  11474. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11475. 'org-calendar-select-mouse)
  11476. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11477. 'org-calendar-select-mouse)
  11478. (org-defkey minibuffer-local-map [(meta shift left)]
  11479. (lambda () (interactive)
  11480. (org-eval-in-calendar '(calendar-backward-month 1))))
  11481. (org-defkey minibuffer-local-map [(meta shift right)]
  11482. (lambda () (interactive)
  11483. (org-eval-in-calendar '(calendar-forward-month 1))))
  11484. (org-defkey minibuffer-local-map [(meta shift up)]
  11485. (lambda () (interactive)
  11486. (org-eval-in-calendar '(calendar-backward-year 1))))
  11487. (org-defkey minibuffer-local-map [(meta shift down)]
  11488. (lambda () (interactive)
  11489. (org-eval-in-calendar '(calendar-forward-year 1))))
  11490. (org-defkey minibuffer-local-map [?\e (shift left)]
  11491. (lambda () (interactive)
  11492. (org-eval-in-calendar '(calendar-backward-month 1))))
  11493. (org-defkey minibuffer-local-map [?\e (shift right)]
  11494. (lambda () (interactive)
  11495. (org-eval-in-calendar '(calendar-forward-month 1))))
  11496. (org-defkey minibuffer-local-map [?\e (shift up)]
  11497. (lambda () (interactive)
  11498. (org-eval-in-calendar '(calendar-backward-year 1))))
  11499. (org-defkey minibuffer-local-map [?\e (shift down)]
  11500. (lambda () (interactive)
  11501. (org-eval-in-calendar '(calendar-forward-year 1))))
  11502. (org-defkey minibuffer-local-map [(shift up)]
  11503. (lambda () (interactive)
  11504. (org-eval-in-calendar '(calendar-backward-week 1))))
  11505. (org-defkey minibuffer-local-map [(shift down)]
  11506. (lambda () (interactive)
  11507. (org-eval-in-calendar '(calendar-forward-week 1))))
  11508. (org-defkey minibuffer-local-map [(shift left)]
  11509. (lambda () (interactive)
  11510. (org-eval-in-calendar '(calendar-backward-day 1))))
  11511. (org-defkey minibuffer-local-map [(shift right)]
  11512. (lambda () (interactive)
  11513. (org-eval-in-calendar '(calendar-forward-day 1))))
  11514. (org-defkey minibuffer-local-map ">"
  11515. (lambda () (interactive)
  11516. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11517. (org-defkey minibuffer-local-map "<"
  11518. (lambda () (interactive)
  11519. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11520. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11521. (unwind-protect
  11522. (progn
  11523. (use-local-map map)
  11524. (add-hook 'post-command-hook 'org-read-date-display)
  11525. (setq org-ans0 (read-string prompt default-input
  11526. 'org-read-date-history nil))
  11527. ;; org-ans0: from prompt
  11528. ;; org-ans1: from mouse click
  11529. ;; org-ans2: from calendar motion
  11530. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11531. (remove-hook 'post-command-hook 'org-read-date-display)
  11532. (use-local-map old-map)
  11533. (when org-read-date-overlay
  11534. (org-delete-overlay org-read-date-overlay)
  11535. (setq org-read-date-overlay nil)))))))
  11536. (t ; Naked prompt only
  11537. (unwind-protect
  11538. (setq ans (read-string prompt default-input
  11539. 'org-read-date-history timestr))
  11540. (when org-read-date-overlay
  11541. (org-delete-overlay org-read-date-overlay)
  11542. (setq org-read-date-overlay nil)))))
  11543. (setq final (org-read-date-analyze ans def defdecode))
  11544. (setq org-read-date-final-answer ans)
  11545. (if to-time
  11546. (apply 'encode-time final)
  11547. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11548. (format "%04d-%02d-%02d %02d:%02d"
  11549. (nth 5 final) (nth 4 final) (nth 3 final)
  11550. (nth 2 final) (nth 1 final))
  11551. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11552. (defvar def)
  11553. (defvar defdecode)
  11554. (defvar with-time)
  11555. (defun org-read-date-display ()
  11556. "Display the current date prompt interpretation in the minibuffer."
  11557. (when org-read-date-display-live
  11558. (when org-read-date-overlay
  11559. (org-delete-overlay org-read-date-overlay))
  11560. (let ((p (point)))
  11561. (end-of-line 1)
  11562. (while (not (equal (buffer-substring
  11563. (max (point-min) (- (point) 4)) (point))
  11564. " "))
  11565. (insert " "))
  11566. (goto-char p))
  11567. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11568. " " (or org-ans1 org-ans2)))
  11569. (org-end-time-was-given nil)
  11570. (f (org-read-date-analyze ans def defdecode))
  11571. (fmts (if org-dcst
  11572. org-time-stamp-custom-formats
  11573. org-time-stamp-formats))
  11574. (fmt (if (or with-time
  11575. (and (boundp 'org-time-was-given) org-time-was-given))
  11576. (cdr fmts)
  11577. (car fmts)))
  11578. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11579. (when (and org-end-time-was-given
  11580. (string-match org-plain-time-of-day-regexp txt))
  11581. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11582. org-end-time-was-given
  11583. (substring txt (match-end 0)))))
  11584. (setq org-read-date-overlay
  11585. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11586. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11587. (defun org-read-date-analyze (ans def defdecode)
  11588. "Analyse the combined answer of the date prompt."
  11589. ;; FIXME: cleanup and comment
  11590. (let (delta deltan deltaw deltadef year month day
  11591. hour minute second wday pm h2 m2 tl wday1
  11592. iso-year iso-weekday iso-week iso-year iso-date)
  11593. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11594. (setq ans "+0"))
  11595. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11596. (setq ans (replace-match "" t t ans)
  11597. deltan (car delta)
  11598. deltaw (nth 1 delta)
  11599. deltadef (nth 2 delta)))
  11600. ;; Check if there is an iso week date in there
  11601. ;; If yes, sore the info and postpone interpreting it until the rest
  11602. ;; of the parsing is done
  11603. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11604. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11605. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11606. iso-week (string-to-number (match-string 2 ans)))
  11607. (setq ans (replace-match "" t t ans)))
  11608. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11609. (when (string-match
  11610. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11611. (setq year (if (match-end 2)
  11612. (string-to-number (match-string 2 ans))
  11613. (string-to-number (format-time-string "%Y")))
  11614. month (string-to-number (match-string 3 ans))
  11615. day (string-to-number (match-string 4 ans)))
  11616. (if (< year 100) (setq year (+ 2000 year)))
  11617. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11618. t nil ans)))
  11619. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11620. ;; If there is a time with am/pm, and *no* time without it, we convert
  11621. ;; so that matching will be successful.
  11622. (loop for i from 1 to 2 do ; twice, for end time as well
  11623. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11624. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11625. (setq hour (string-to-number (match-string 1 ans))
  11626. minute (if (match-end 3)
  11627. (string-to-number (match-string 3 ans))
  11628. 0)
  11629. pm (equal ?p
  11630. (string-to-char (downcase (match-string 4 ans)))))
  11631. (if (and (= hour 12) (not pm))
  11632. (setq hour 0)
  11633. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11634. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11635. t t ans))))
  11636. ;; Check if a time range is given as a duration
  11637. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11638. (setq hour (string-to-number (match-string 1 ans))
  11639. h2 (+ hour (string-to-number (match-string 3 ans)))
  11640. minute (string-to-number (match-string 2 ans))
  11641. m2 (+ minute (if (match-end 5) (string-to-number
  11642. (match-string 5 ans))0)))
  11643. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11644. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11645. t t ans)))
  11646. ;; Check if there is a time range
  11647. (when (boundp 'org-end-time-was-given)
  11648. (setq org-time-was-given nil)
  11649. (when (and (string-match org-plain-time-of-day-regexp ans)
  11650. (match-end 8))
  11651. (setq org-end-time-was-given (match-string 8 ans))
  11652. (setq ans (concat (substring ans 0 (match-beginning 7))
  11653. (substring ans (match-end 7))))))
  11654. (setq tl (parse-time-string ans)
  11655. day (or (nth 3 tl) (nth 3 defdecode))
  11656. month (or (nth 4 tl)
  11657. (if (and org-read-date-prefer-future
  11658. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11659. (1+ (nth 4 defdecode))
  11660. (nth 4 defdecode)))
  11661. year (or (nth 5 tl)
  11662. (if (and org-read-date-prefer-future
  11663. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11664. (1+ (nth 5 defdecode))
  11665. (nth 5 defdecode)))
  11666. hour (or (nth 2 tl) (nth 2 defdecode))
  11667. minute (or (nth 1 tl) (nth 1 defdecode))
  11668. second (or (nth 0 tl) 0)
  11669. wday (nth 6 tl))
  11670. ;; Special date definitions below
  11671. (cond
  11672. (iso-week
  11673. ;; There was an iso week
  11674. (setq year (or iso-year year)
  11675. day (or iso-weekday wday 1)
  11676. wday nil ; to make sure that the trigger below does not match
  11677. iso-date (calendar-gregorian-from-absolute
  11678. (calendar-absolute-from-iso
  11679. (list iso-week day year))))
  11680. ; FIXME: Should we also push ISO weeks into the future?
  11681. ; (when (and org-read-date-prefer-future
  11682. ; (not iso-year)
  11683. ; (< (calendar-absolute-from-gregorian iso-date)
  11684. ; (time-to-days (current-time))))
  11685. ; (setq year (1+ year)
  11686. ; iso-date (calendar-gregorian-from-absolute
  11687. ; (calendar-absolute-from-iso
  11688. ; (list iso-week day year)))))
  11689. (setq month (car iso-date)
  11690. year (nth 2 iso-date)
  11691. day (nth 1 iso-date)))
  11692. (deltan
  11693. (unless deltadef
  11694. (let ((now (decode-time (current-time))))
  11695. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11696. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11697. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11698. ((equal deltaw "m") (setq month (+ month deltan)))
  11699. ((equal deltaw "y") (setq year (+ year deltan)))))
  11700. ((and wday (not (nth 3 tl)))
  11701. ;; Weekday was given, but no day, so pick that day in the week
  11702. ;; on or after the derived date.
  11703. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11704. (unless (equal wday wday1)
  11705. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11706. (if (and (boundp 'org-time-was-given)
  11707. (nth 2 tl))
  11708. (setq org-time-was-given t))
  11709. (if (< year 100) (setq year (+ 2000 year)))
  11710. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11711. (list second minute hour day month year)))
  11712. (defvar parse-time-weekdays)
  11713. (defun org-read-date-get-relative (s today default)
  11714. "Check string S for special relative date string.
  11715. TODAY and DEFAULT are internal times, for today and for a default.
  11716. Return shift list (N what def-flag)
  11717. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11718. N is the number of WHATs to shift.
  11719. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11720. the DEFAULT date rather than TODAY."
  11721. (when (and
  11722. (string-match
  11723. (concat
  11724. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11725. "\\([0-9]+\\)?"
  11726. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11727. "\\([ \t]\\|$\\)") s)
  11728. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11729. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11730. (string-to-char (substring (match-string 1 s) -1))
  11731. ?+))
  11732. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11733. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11734. (what (if (match-end 3) (match-string 3 s) "d"))
  11735. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11736. (date (if rel default today))
  11737. (wday (nth 6 (decode-time date)))
  11738. delta)
  11739. (if wday1
  11740. (progn
  11741. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11742. (if (= dir ?-) (setq delta (- delta 7)))
  11743. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11744. (list delta "d" rel))
  11745. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11746. (defun org-eval-in-calendar (form &optional keepdate)
  11747. "Eval FORM in the calendar window and return to current window.
  11748. Also, store the cursor date in variable org-ans2."
  11749. (let ((sf (selected-frame))
  11750. (sw (selected-window)))
  11751. (select-window (get-buffer-window "*Calendar*" t))
  11752. (eval form)
  11753. (when (and (not keepdate) (calendar-cursor-to-date))
  11754. (let* ((date (calendar-cursor-to-date))
  11755. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11756. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11757. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11758. (select-window sw)
  11759. (org-select-frame-set-input-focus sf)))
  11760. (defun org-calendar-select ()
  11761. "Return to `org-read-date' with the date currently selected.
  11762. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11763. (interactive)
  11764. (when (calendar-cursor-to-date)
  11765. (let* ((date (calendar-cursor-to-date))
  11766. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11767. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11768. (if (active-minibuffer-window) (exit-minibuffer))))
  11769. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11770. "Insert a date stamp for the date given by the internal TIME.
  11771. WITH-HM means, use the stamp format that includes the time of the day.
  11772. INACTIVE means use square brackets instead of angular ones, so that the
  11773. stamp will not contribute to the agenda.
  11774. PRE and POST are optional strings to be inserted before and after the
  11775. stamp.
  11776. The command returns the inserted time stamp."
  11777. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11778. stamp)
  11779. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11780. (insert-before-markers (or pre ""))
  11781. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11782. (when (listp extra)
  11783. (setq extra (car extra))
  11784. (if (and (stringp extra)
  11785. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11786. (setq extra (format "-%02d:%02d"
  11787. (string-to-number (match-string 1 extra))
  11788. (string-to-number (match-string 2 extra))))
  11789. (setq extra nil)))
  11790. (when extra
  11791. (backward-char 1)
  11792. (insert-before-markers extra)
  11793. (forward-char 1))
  11794. (insert-before-markers (or post ""))
  11795. (setq org-last-inserted-timestamp stamp)))
  11796. (defun org-toggle-time-stamp-overlays ()
  11797. "Toggle the use of custom time stamp formats."
  11798. (interactive)
  11799. (setq org-display-custom-times (not org-display-custom-times))
  11800. (unless org-display-custom-times
  11801. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11802. (while (setq p (next-single-property-change p 'display))
  11803. (if (and (get-text-property p 'display)
  11804. (eq (get-text-property p 'face) 'org-date))
  11805. (remove-text-properties
  11806. p (setq p (next-single-property-change p 'display))
  11807. '(display t))))
  11808. (set-buffer-modified-p bmp)))
  11809. (if (featurep 'xemacs)
  11810. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11811. (org-restart-font-lock)
  11812. (setq org-table-may-need-update t)
  11813. (if org-display-custom-times
  11814. (message "Time stamps are overlayed with custom format")
  11815. (message "Time stamp overlays removed")))
  11816. (defun org-display-custom-time (beg end)
  11817. "Overlay modified time stamp format over timestamp between BEG and END."
  11818. (let* ((ts (buffer-substring beg end))
  11819. t1 w1 with-hm tf time str w2 (off 0))
  11820. (save-match-data
  11821. (setq t1 (org-parse-time-string ts t))
  11822. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11823. (setq off (- (match-end 0) (match-beginning 0)))))
  11824. (setq end (- end off))
  11825. (setq w1 (- end beg)
  11826. with-hm (and (nth 1 t1) (nth 2 t1))
  11827. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11828. time (org-fix-decoded-time t1)
  11829. str (org-add-props
  11830. (format-time-string
  11831. (substring tf 1 -1) (apply 'encode-time time))
  11832. nil 'mouse-face 'highlight)
  11833. w2 (length str))
  11834. (if (not (= w2 w1))
  11835. (add-text-properties (1+ beg) (+ 2 beg)
  11836. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11837. (if (featurep 'xemacs)
  11838. (progn
  11839. (put-text-property beg end 'invisible t)
  11840. (put-text-property beg end 'end-glyph (make-glyph str)))
  11841. (put-text-property beg end 'display str))))
  11842. (defun org-translate-time (string)
  11843. "Translate all timestamps in STRING to custom format.
  11844. But do this only if the variable `org-display-custom-times' is set."
  11845. (when org-display-custom-times
  11846. (save-match-data
  11847. (let* ((start 0)
  11848. (re org-ts-regexp-both)
  11849. t1 with-hm inactive tf time str beg end)
  11850. (while (setq start (string-match re string start))
  11851. (setq beg (match-beginning 0)
  11852. end (match-end 0)
  11853. t1 (save-match-data
  11854. (org-parse-time-string (substring string beg end) t))
  11855. with-hm (and (nth 1 t1) (nth 2 t1))
  11856. inactive (equal (substring string beg (1+ beg)) "[")
  11857. tf (funcall (if with-hm 'cdr 'car)
  11858. org-time-stamp-custom-formats)
  11859. time (org-fix-decoded-time t1)
  11860. str (format-time-string
  11861. (concat
  11862. (if inactive "[" "<") (substring tf 1 -1)
  11863. (if inactive "]" ">"))
  11864. (apply 'encode-time time))
  11865. string (replace-match str t t string)
  11866. start (+ start (length str)))))))
  11867. string)
  11868. (defun org-fix-decoded-time (time)
  11869. "Set 0 instead of nil for the first 6 elements of time.
  11870. Don't touch the rest."
  11871. (let ((n 0))
  11872. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11873. (defun org-days-to-time (timestamp-string)
  11874. "Difference between TIMESTAMP-STRING and now in days."
  11875. (- (time-to-days (org-time-string-to-time timestamp-string))
  11876. (time-to-days (current-time))))
  11877. (defun org-deadline-close (timestamp-string &optional ndays)
  11878. "Is the time in TIMESTAMP-STRING close to the current date?"
  11879. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11880. (and (< (org-days-to-time timestamp-string) ndays)
  11881. (not (org-entry-is-done-p))))
  11882. (defun org-get-wdays (ts)
  11883. "Get the deadline lead time appropriate for timestring TS."
  11884. (cond
  11885. ((<= org-deadline-warning-days 0)
  11886. ;; 0 or negative, enforce this value no matter what
  11887. (- org-deadline-warning-days))
  11888. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11889. ;; lead time is specified.
  11890. (floor (* (string-to-number (match-string 1 ts))
  11891. (cdr (assoc (match-string 2 ts)
  11892. '(("d" . 1) ("w" . 7)
  11893. ("m" . 30.4) ("y" . 365.25)))))))
  11894. ;; go for the default.
  11895. (t org-deadline-warning-days)))
  11896. (defun org-calendar-select-mouse (ev)
  11897. "Return to `org-read-date' with the date currently selected.
  11898. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11899. (interactive "e")
  11900. (mouse-set-point ev)
  11901. (when (calendar-cursor-to-date)
  11902. (let* ((date (calendar-cursor-to-date))
  11903. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11904. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11905. (if (active-minibuffer-window) (exit-minibuffer))))
  11906. (defun org-check-deadlines (ndays)
  11907. "Check if there are any deadlines due or past due.
  11908. A deadline is considered due if it happens within `org-deadline-warning-days'
  11909. days from today's date. If the deadline appears in an entry marked DONE,
  11910. it is not shown. The prefix arg NDAYS can be used to test that many
  11911. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11912. (interactive "P")
  11913. (let* ((org-warn-days
  11914. (cond
  11915. ((equal ndays '(4)) 100000)
  11916. (ndays (prefix-numeric-value ndays))
  11917. (t (abs org-deadline-warning-days))))
  11918. (case-fold-search nil)
  11919. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11920. (callback
  11921. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11922. (message "%d deadlines past-due or due within %d days"
  11923. (org-occur regexp nil callback)
  11924. org-warn-days)))
  11925. (defun org-check-before-date (date)
  11926. "Check if there are deadlines or scheduled entries before DATE."
  11927. (interactive (list (org-read-date)))
  11928. (let ((case-fold-search nil)
  11929. (regexp (concat "\\<\\(" org-deadline-string
  11930. "\\|" org-scheduled-string
  11931. "\\) *<\\([^>]+\\)>"))
  11932. (callback
  11933. (lambda () (time-less-p
  11934. (org-time-string-to-time (match-string 2))
  11935. (org-time-string-to-time date)))))
  11936. (message "%d entries before %s"
  11937. (org-occur regexp nil callback) date)))
  11938. (defun org-check-after-date (date)
  11939. "Check if there are deadlines or scheduled entries after DATE."
  11940. (interactive (list (org-read-date)))
  11941. (let ((case-fold-search nil)
  11942. (regexp (concat "\\<\\(" org-deadline-string
  11943. "\\|" org-scheduled-string
  11944. "\\) *<\\([^>]+\\)>"))
  11945. (callback
  11946. (lambda () (not
  11947. (time-less-p
  11948. (org-time-string-to-time (match-string 2))
  11949. (org-time-string-to-time date))))))
  11950. (message "%d entries after %s"
  11951. (org-occur regexp nil callback) date)))
  11952. (defun org-evaluate-time-range (&optional to-buffer)
  11953. "Evaluate a time range by computing the difference between start and end.
  11954. Normally the result is just printed in the echo area, but with prefix arg
  11955. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11956. If the time range is actually in a table, the result is inserted into the
  11957. next column.
  11958. For time difference computation, a year is assumed to be exactly 365
  11959. days in order to avoid rounding problems."
  11960. (interactive "P")
  11961. (or
  11962. (org-clock-update-time-maybe)
  11963. (save-excursion
  11964. (unless (org-at-date-range-p t)
  11965. (goto-char (point-at-bol))
  11966. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11967. (if (not (org-at-date-range-p t))
  11968. (error "Not at a time-stamp range, and none found in current line")))
  11969. (let* ((ts1 (match-string 1))
  11970. (ts2 (match-string 2))
  11971. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11972. (match-end (match-end 0))
  11973. (time1 (org-time-string-to-time ts1))
  11974. (time2 (org-time-string-to-time ts2))
  11975. (t1 (org-float-time time1))
  11976. (t2 (org-float-time time2))
  11977. (diff (abs (- t2 t1)))
  11978. (negative (< (- t2 t1) 0))
  11979. ;; (ys (floor (* 365 24 60 60)))
  11980. (ds (* 24 60 60))
  11981. (hs (* 60 60))
  11982. (fy "%dy %dd %02d:%02d")
  11983. (fy1 "%dy %dd")
  11984. (fd "%dd %02d:%02d")
  11985. (fd1 "%dd")
  11986. (fh "%02d:%02d")
  11987. y d h m align)
  11988. (if havetime
  11989. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11990. y 0
  11991. d (floor (/ diff ds)) diff (mod diff ds)
  11992. h (floor (/ diff hs)) diff (mod diff hs)
  11993. m (floor (/ diff 60)))
  11994. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11995. y 0
  11996. d (floor (+ (/ diff ds) 0.5))
  11997. h 0 m 0))
  11998. (if (not to-buffer)
  11999. (message "%s" (org-make-tdiff-string y d h m))
  12000. (if (org-at-table-p)
  12001. (progn
  12002. (goto-char match-end)
  12003. (setq align t)
  12004. (and (looking-at " *|") (goto-char (match-end 0))))
  12005. (goto-char match-end))
  12006. (if (looking-at
  12007. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12008. (replace-match ""))
  12009. (if negative (insert " -"))
  12010. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12011. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12012. (insert " " (format fh h m))))
  12013. (if align (org-table-align))
  12014. (message "Time difference inserted")))))
  12015. (defun org-make-tdiff-string (y d h m)
  12016. (let ((fmt "")
  12017. (l nil))
  12018. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12019. l (push y l)))
  12020. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12021. l (push d l)))
  12022. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12023. l (push h l)))
  12024. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12025. l (push m l)))
  12026. (apply 'format fmt (nreverse l))))
  12027. (defun org-time-string-to-time (s)
  12028. (apply 'encode-time (org-parse-time-string s)))
  12029. (defun org-time-string-to-seconds (s)
  12030. (org-float-time (org-time-string-to-time s)))
  12031. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12032. "Convert a time stamp to an absolute day number.
  12033. If there is a specifyer for a cyclic time stamp, get the closest date to
  12034. DAYNR.
  12035. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12036. the variable date is bound by the calendar when this is called."
  12037. (cond
  12038. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12039. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12040. daynr
  12041. (+ daynr 1000)))
  12042. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12043. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12044. (time-to-days (current-time))) (match-string 0 s)
  12045. prefer show-all))
  12046. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12047. (defun org-days-to-iso-week (days)
  12048. "Return the iso week number."
  12049. (require 'cal-iso)
  12050. (car (calendar-iso-from-absolute days)))
  12051. (defun org-small-year-to-year (year)
  12052. "Convert 2-digit years into 4-digit years.
  12053. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12054. The year 2000 cannot be abbreviated. Any year larger than 99
  12055. is returned unchanged."
  12056. (if (< year 38)
  12057. (setq year (+ 2000 year))
  12058. (if (< year 100)
  12059. (setq year (+ 1900 year))))
  12060. year)
  12061. (defun org-time-from-absolute (d)
  12062. "Return the time corresponding to date D.
  12063. D may be an absolute day number, or a calendar-type list (month day year)."
  12064. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12065. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12066. (defun org-calendar-holiday ()
  12067. "List of holidays, for Diary display in Org-mode."
  12068. (require 'holidays)
  12069. (let ((hl (funcall
  12070. (if (fboundp 'calendar-check-holidays)
  12071. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12072. (if hl (mapconcat 'identity hl "; "))))
  12073. (defun org-diary-sexp-entry (sexp entry date)
  12074. "Process a SEXP diary ENTRY for DATE."
  12075. (require 'diary-lib)
  12076. (let ((result (if calendar-debug-sexp
  12077. (let ((stack-trace-on-error t))
  12078. (eval (car (read-from-string sexp))))
  12079. (condition-case nil
  12080. (eval (car (read-from-string sexp)))
  12081. (error
  12082. (beep)
  12083. (message "Bad sexp at line %d in %s: %s"
  12084. (org-current-line)
  12085. (buffer-file-name) sexp)
  12086. (sleep-for 2))))))
  12087. (cond ((stringp result) result)
  12088. ((and (consp result)
  12089. (stringp (cdr result))) (cdr result))
  12090. (result entry)
  12091. (t nil))))
  12092. (defun org-diary-to-ical-string (frombuf)
  12093. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12094. This uses the icalendar.el library."
  12095. (let* ((tmpdir (if (featurep 'xemacs)
  12096. (temp-directory)
  12097. temporary-file-directory))
  12098. (tmpfile (make-temp-name
  12099. (expand-file-name "orgics" tmpdir)))
  12100. buf rtn b e)
  12101. (save-excursion
  12102. (set-buffer frombuf)
  12103. (icalendar-export-region (point-min) (point-max) tmpfile)
  12104. (setq buf (find-buffer-visiting tmpfile))
  12105. (set-buffer buf)
  12106. (goto-char (point-min))
  12107. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12108. (setq b (match-beginning 0)))
  12109. (goto-char (point-max))
  12110. (if (re-search-backward "^END:VEVENT" nil t)
  12111. (setq e (match-end 0)))
  12112. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12113. (kill-buffer buf)
  12114. (delete-file tmpfile)
  12115. rtn))
  12116. (defun org-closest-date (start current change prefer show-all)
  12117. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12118. When PREFER is `past' return a date that is either CURRENT or past.
  12119. When PREFER is `future', return a date that is either CURRENT or future.
  12120. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12121. ;; Make the proper lists from the dates
  12122. (catch 'exit
  12123. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12124. dn dw sday cday n1 n2 n0
  12125. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12126. (setq start (org-date-to-gregorian start)
  12127. current (org-date-to-gregorian
  12128. (if show-all
  12129. current
  12130. (time-to-days (current-time))))
  12131. sday (calendar-absolute-from-gregorian start)
  12132. cday (calendar-absolute-from-gregorian current))
  12133. (if (<= cday sday) (throw 'exit sday))
  12134. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12135. (setq dn (string-to-number (match-string 1 change))
  12136. dw (cdr (assoc (match-string 2 change) a1)))
  12137. (error "Invalid change specifyer: %s" change))
  12138. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12139. (cond
  12140. ((eq dw 'day)
  12141. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12142. n2 (+ n1 dn)))
  12143. ((eq dw 'year)
  12144. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12145. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12146. (setq date1 (list m d y1)
  12147. n1 (calendar-absolute-from-gregorian date1)
  12148. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12149. n2 (calendar-absolute-from-gregorian date2)))
  12150. ((eq dw 'month)
  12151. ;; approx number of month between the two dates
  12152. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12153. ;; How often does dn fit in there?
  12154. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12155. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12156. m (+ m nm)
  12157. ny (floor (/ m 12))
  12158. y (+ y ny)
  12159. m (- m (* ny 12)))
  12160. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12161. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12162. (setq m2 (+ m dn) y2 y)
  12163. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12164. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12165. (while (<= n2 cday)
  12166. (setq n1 n2 m m2 y y2)
  12167. (setq m2 (+ m dn) y2 y)
  12168. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12169. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12170. ;; Make sure n1 is the earlier date
  12171. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12172. (if show-all
  12173. (cond
  12174. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12175. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12176. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12177. (cond
  12178. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12179. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12180. (t (if (= cday n1) n1 n2)))))))
  12181. (defun org-date-to-gregorian (date)
  12182. "Turn any specification of DATE into a gregorian date for the calendar."
  12183. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12184. ((and (listp date) (= (length date) 3)) date)
  12185. ((stringp date)
  12186. (setq date (org-parse-time-string date))
  12187. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12188. ((listp date)
  12189. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12190. (defun org-parse-time-string (s &optional nodefault)
  12191. "Parse the standard Org-mode time string.
  12192. This should be a lot faster than the normal `parse-time-string'.
  12193. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12194. hour and minute fields will be nil if not given."
  12195. (if (string-match org-ts-regexp0 s)
  12196. (list 0
  12197. (if (or (match-beginning 8) (not nodefault))
  12198. (string-to-number (or (match-string 8 s) "0")))
  12199. (if (or (match-beginning 7) (not nodefault))
  12200. (string-to-number (or (match-string 7 s) "0")))
  12201. (string-to-number (match-string 4 s))
  12202. (string-to-number (match-string 3 s))
  12203. (string-to-number (match-string 2 s))
  12204. nil nil nil)
  12205. (error "Not a standard Org-mode time string: %s" s)))
  12206. (defun org-timestamp-up (&optional arg)
  12207. "Increase the date item at the cursor by one.
  12208. If the cursor is on the year, change the year. If it is on the month or
  12209. the day, change that.
  12210. With prefix ARG, change by that many units."
  12211. (interactive "p")
  12212. (org-timestamp-change (prefix-numeric-value arg)))
  12213. (defun org-timestamp-down (&optional arg)
  12214. "Decrease the date item at the cursor by one.
  12215. If the cursor is on the year, change the year. If it is on the month or
  12216. the day, change that.
  12217. With prefix ARG, change by that many units."
  12218. (interactive "p")
  12219. (org-timestamp-change (- (prefix-numeric-value arg))))
  12220. (defun org-timestamp-up-day (&optional arg)
  12221. "Increase the date in the time stamp by one day.
  12222. With prefix ARG, change that many days."
  12223. (interactive "p")
  12224. (if (and (not (org-at-timestamp-p t))
  12225. (org-on-heading-p))
  12226. (org-todo 'up)
  12227. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12228. (defun org-timestamp-down-day (&optional arg)
  12229. "Decrease the date in the time stamp by one day.
  12230. With prefix ARG, change that many days."
  12231. (interactive "p")
  12232. (if (and (not (org-at-timestamp-p t))
  12233. (org-on-heading-p))
  12234. (org-todo 'down)
  12235. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12236. (defun org-at-timestamp-p (&optional inactive-ok)
  12237. "Determine if the cursor is in or at a timestamp."
  12238. (interactive)
  12239. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12240. (pos (point))
  12241. (ans (or (looking-at tsr)
  12242. (save-excursion
  12243. (skip-chars-backward "^[<\n\r\t")
  12244. (if (> (point) (point-min)) (backward-char 1))
  12245. (and (looking-at tsr)
  12246. (> (- (match-end 0) pos) -1))))))
  12247. (and ans
  12248. (boundp 'org-ts-what)
  12249. (setq org-ts-what
  12250. (cond
  12251. ((= pos (match-beginning 0)) 'bracket)
  12252. ((= pos (1- (match-end 0))) 'bracket)
  12253. ((org-pos-in-match-range pos 2) 'year)
  12254. ((org-pos-in-match-range pos 3) 'month)
  12255. ((org-pos-in-match-range pos 7) 'hour)
  12256. ((org-pos-in-match-range pos 8) 'minute)
  12257. ((or (org-pos-in-match-range pos 4)
  12258. (org-pos-in-match-range pos 5)) 'day)
  12259. ((and (> pos (or (match-end 8) (match-end 5)))
  12260. (< pos (match-end 0)))
  12261. (- pos (or (match-end 8) (match-end 5))))
  12262. (t 'day))))
  12263. ans))
  12264. (defun org-toggle-timestamp-type ()
  12265. "Toggle the type (<active> or [inactive]) of a time stamp."
  12266. (interactive)
  12267. (when (org-at-timestamp-p t)
  12268. (let ((beg (match-beginning 0)) (end (match-end 0))
  12269. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12270. (save-excursion
  12271. (goto-char beg)
  12272. (while (re-search-forward "[][<>]" end t)
  12273. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12274. t t)))
  12275. (message "Timestamp is now %sactive"
  12276. (if (equal (char-after beg) ?<) "" "in")))))
  12277. (defun org-timestamp-change (n &optional what)
  12278. "Change the date in the time stamp at point.
  12279. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12280. `year', `minute', `second'. If WHAT is not given, the cursor position
  12281. in the timestamp determines what will be changed."
  12282. (let ((pos (point))
  12283. with-hm inactive
  12284. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12285. org-ts-what
  12286. extra rem
  12287. ts time time0)
  12288. (if (not (org-at-timestamp-p t))
  12289. (error "Not at a timestamp"))
  12290. (if (and (not what) (eq org-ts-what 'bracket))
  12291. (org-toggle-timestamp-type)
  12292. (if (and (not what) (not (eq org-ts-what 'day))
  12293. org-display-custom-times
  12294. (get-text-property (point) 'display)
  12295. (not (get-text-property (1- (point)) 'display)))
  12296. (setq org-ts-what 'day))
  12297. (setq org-ts-what (or what org-ts-what)
  12298. inactive (= (char-after (match-beginning 0)) ?\[)
  12299. ts (match-string 0))
  12300. (replace-match "")
  12301. (if (string-match
  12302. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12303. ts)
  12304. (setq extra (match-string 1 ts)))
  12305. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12306. (setq with-hm t))
  12307. (setq time0 (org-parse-time-string ts))
  12308. (when (and (eq org-ts-what 'minute)
  12309. (eq current-prefix-arg nil))
  12310. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12311. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12312. (setcar (cdr time0) (+ (nth 1 time0)
  12313. (if (> n 0) (- rem) (- dm rem))))))
  12314. (setq time
  12315. (encode-time (or (car time0) 0)
  12316. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12317. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12318. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12319. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12320. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12321. (nthcdr 6 time0)))
  12322. (when (and (member org-ts-what '(hour minute))
  12323. extra
  12324. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12325. (setq extra (org-modify-ts-extra
  12326. extra
  12327. (if (eq org-ts-what 'hour) 2 5)
  12328. n dm)))
  12329. (when (integerp org-ts-what)
  12330. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12331. (if (eq what 'calendar)
  12332. (let ((cal-date (org-get-date-from-calendar)))
  12333. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12334. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12335. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12336. (setcar time0 (or (car time0) 0))
  12337. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12338. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12339. (setq time (apply 'encode-time time0))))
  12340. (setq org-last-changed-timestamp
  12341. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12342. (org-clock-update-time-maybe)
  12343. (goto-char pos)
  12344. ;; Try to recenter the calendar window, if any
  12345. (if (and org-calendar-follow-timestamp-change
  12346. (get-buffer-window "*Calendar*" t)
  12347. (memq org-ts-what '(day month year)))
  12348. (org-recenter-calendar (time-to-days time))))))
  12349. (defun org-modify-ts-extra (s pos n dm)
  12350. "Change the different parts of the lead-time and repeat fields in timestamp."
  12351. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12352. ng h m new rem)
  12353. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12354. (cond
  12355. ((or (org-pos-in-match-range pos 2)
  12356. (org-pos-in-match-range pos 3))
  12357. (setq m (string-to-number (match-string 3 s))
  12358. h (string-to-number (match-string 2 s)))
  12359. (if (org-pos-in-match-range pos 2)
  12360. (setq h (+ h n))
  12361. (setq n (* dm (org-no-warnings (signum n))))
  12362. (when (not (= 0 (setq rem (% m dm))))
  12363. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12364. (setq m (+ m n)))
  12365. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12366. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12367. (setq h (min 24 (max 0 h)))
  12368. (setq ng 1 new (format "-%02d:%02d" h m)))
  12369. ((org-pos-in-match-range pos 6)
  12370. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12371. ((org-pos-in-match-range pos 5)
  12372. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12373. ((org-pos-in-match-range pos 9)
  12374. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12375. ((org-pos-in-match-range pos 8)
  12376. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12377. (when ng
  12378. (setq s (concat
  12379. (substring s 0 (match-beginning ng))
  12380. new
  12381. (substring s (match-end ng))))))
  12382. s))
  12383. (defun org-recenter-calendar (date)
  12384. "If the calendar is visible, recenter it to DATE."
  12385. (let* ((win (selected-window))
  12386. (cwin (get-buffer-window "*Calendar*" t))
  12387. (calendar-move-hook nil))
  12388. (when cwin
  12389. (select-window cwin)
  12390. (calendar-goto-date (if (listp date) date
  12391. (calendar-gregorian-from-absolute date)))
  12392. (select-window win))))
  12393. (defun org-goto-calendar (&optional arg)
  12394. "Go to the Emacs calendar at the current date.
  12395. If there is a time stamp in the current line, go to that date.
  12396. A prefix ARG can be used to force the current date."
  12397. (interactive "P")
  12398. (let ((tsr org-ts-regexp) diff
  12399. (calendar-move-hook nil)
  12400. (calendar-view-holidays-initially-flag nil)
  12401. (view-calendar-holidays-initially nil)
  12402. (calendar-view-diary-initially-flag nil)
  12403. (view-diary-entries-initially nil))
  12404. (if (or (org-at-timestamp-p)
  12405. (save-excursion
  12406. (beginning-of-line 1)
  12407. (looking-at (concat ".*" tsr))))
  12408. (let ((d1 (time-to-days (current-time)))
  12409. (d2 (time-to-days
  12410. (org-time-string-to-time (match-string 1)))))
  12411. (setq diff (- d2 d1))))
  12412. (calendar)
  12413. (calendar-goto-today)
  12414. (if (and diff (not arg)) (calendar-forward-day diff))))
  12415. (defun org-get-date-from-calendar ()
  12416. "Return a list (month day year) of date at point in calendar."
  12417. (with-current-buffer "*Calendar*"
  12418. (save-match-data
  12419. (calendar-cursor-to-date))))
  12420. (defun org-date-from-calendar ()
  12421. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12422. If there is already a time stamp at the cursor position, update it."
  12423. (interactive)
  12424. (if (org-at-timestamp-p t)
  12425. (org-timestamp-change 0 'calendar)
  12426. (let ((cal-date (org-get-date-from-calendar)))
  12427. (org-insert-time-stamp
  12428. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12429. (defun org-minutes-to-hh:mm-string (m)
  12430. "Compute H:MM from a number of minutes."
  12431. (let ((h (/ m 60)))
  12432. (setq m (- m (* 60 h)))
  12433. (format org-time-clocksum-format h m)))
  12434. (defun org-hh:mm-string-to-minutes (s)
  12435. "Convert a string H:MM to a number of minutes.
  12436. If the string is just a number, interprete it as minutes.
  12437. In fact, the first hh:mm or number in the string will be taken,
  12438. there can be extra stuff in the string.
  12439. If no number is found, the return value is 0."
  12440. (cond
  12441. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12442. (+ (* (string-to-number (match-string 1 s)) 60)
  12443. (string-to-number (match-string 2 s))))
  12444. ((string-match "\\([0-9]+\\)" s)
  12445. (string-to-number (match-string 1 s)))
  12446. (t 0)))
  12447. ;;;; Files
  12448. (defun org-save-all-org-buffers ()
  12449. "Save all Org-mode buffers without user confirmation."
  12450. (interactive)
  12451. (message "Saving all Org-mode buffers...")
  12452. (save-some-buffers t 'org-mode-p)
  12453. (when (featurep 'org-id) (org-id-locations-save))
  12454. (message "Saving all Org-mode buffers... done"))
  12455. (defun org-revert-all-org-buffers ()
  12456. "Revert all Org-mode buffers.
  12457. Prompt for confirmation when there are unsaved changes.
  12458. Be sure you know what you are doing before letting this function
  12459. overwrite your changes.
  12460. This function is useful in a setup where one tracks org files
  12461. with a version control system, to revert on one machine after pulling
  12462. changes from another. I believe the procedure must be like this:
  12463. 1. M-x org-save-all-org-buffers
  12464. 2. Pull changes from the other machine, resolve conflicts
  12465. 3. M-x org-revert-all-org-buffers"
  12466. (interactive)
  12467. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12468. (error "Abort"))
  12469. (save-excursion
  12470. (save-window-excursion
  12471. (mapc
  12472. (lambda (b)
  12473. (when (and (with-current-buffer b (org-mode-p))
  12474. (with-current-buffer b buffer-file-name))
  12475. (switch-to-buffer b)
  12476. (revert-buffer t 'no-confirm)))
  12477. (buffer-list))
  12478. (when (and (featurep 'org-id) org-id-track-globally)
  12479. (org-id-locations-load)))))
  12480. ;;;; Agenda files
  12481. ;;;###autoload
  12482. (defun org-iswitchb (&optional arg)
  12483. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12484. With a prefix argument, restrict available to files.
  12485. With two prefix arguments, restrict available buffers to agenda files."
  12486. (interactive "P")
  12487. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12488. ((equal arg '(16)) (org-buffer-list 'agenda))
  12489. (t (org-buffer-list)))))
  12490. (switch-to-buffer
  12491. (org-icompleting-read "Org buffer: "
  12492. (mapcar 'list (mapcar 'buffer-name blist))
  12493. nil t))))
  12494. ;;;###autoload
  12495. (defalias 'org-ido-switchb 'org-iswitchb)
  12496. (defun org-buffer-list (&optional predicate exclude-tmp)
  12497. "Return a list of Org buffers.
  12498. PREDICATE can be `export', `files' or `agenda'.
  12499. export restrict the list to Export buffers.
  12500. files restrict the list to buffers visiting Org files.
  12501. agenda restrict the list to buffers visiting agenda files.
  12502. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12503. (let* ((bfn nil)
  12504. (agenda-files (and (eq predicate 'agenda)
  12505. (mapcar 'file-truename (org-agenda-files t))))
  12506. (filter
  12507. (cond
  12508. ((eq predicate 'files)
  12509. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12510. ((eq predicate 'export)
  12511. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12512. ((eq predicate 'agenda)
  12513. (lambda (b)
  12514. (with-current-buffer b
  12515. (and (eq major-mode 'org-mode)
  12516. (setq bfn (buffer-file-name b))
  12517. (member (file-truename bfn) agenda-files)))))
  12518. (t (lambda (b) (with-current-buffer b
  12519. (or (eq major-mode 'org-mode)
  12520. (string-match "\*Org .*Export"
  12521. (buffer-name b)))))))))
  12522. (delq nil
  12523. (mapcar
  12524. (lambda(b)
  12525. (if (and (funcall filter b)
  12526. (or (not exclude-tmp)
  12527. (not (string-match "tmp" (buffer-name b)))))
  12528. b
  12529. nil))
  12530. (buffer-list)))))
  12531. (defun org-agenda-files (&optional unrestricted archives)
  12532. "Get the list of agenda files.
  12533. Optional UNRESTRICTED means return the full list even if a restriction
  12534. is currently in place.
  12535. When ARCHIVES is t, include all archive files hat are really being
  12536. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12537. `org-agenda-archives-mode' is t."
  12538. (let ((files
  12539. (cond
  12540. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12541. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12542. ((listp org-agenda-files) org-agenda-files)
  12543. (t (error "Invalid value of `org-agenda-files'")))))
  12544. (setq files (apply 'append
  12545. (mapcar (lambda (f)
  12546. (if (file-directory-p f)
  12547. (directory-files
  12548. f t org-agenda-file-regexp)
  12549. (list f)))
  12550. files)))
  12551. (when org-agenda-skip-unavailable-files
  12552. (setq files (delq nil
  12553. (mapcar (function
  12554. (lambda (file)
  12555. (and (file-readable-p file) file)))
  12556. files))))
  12557. (when (or (eq archives t)
  12558. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12559. (setq files (org-add-archive-files files)))
  12560. files))
  12561. (defun org-edit-agenda-file-list ()
  12562. "Edit the list of agenda files.
  12563. Depending on setup, this either uses customize to edit the variable
  12564. `org-agenda-files', or it visits the file that is holding the list. In the
  12565. latter case, the buffer is set up in a way that saving it automatically kills
  12566. the buffer and restores the previous window configuration."
  12567. (interactive)
  12568. (if (stringp org-agenda-files)
  12569. (let ((cw (current-window-configuration)))
  12570. (find-file org-agenda-files)
  12571. (org-set-local 'org-window-configuration cw)
  12572. (org-add-hook 'after-save-hook
  12573. (lambda ()
  12574. (set-window-configuration
  12575. (prog1 org-window-configuration
  12576. (kill-buffer (current-buffer))))
  12577. (org-install-agenda-files-menu)
  12578. (message "New agenda file list installed"))
  12579. nil 'local)
  12580. (message "%s" (substitute-command-keys
  12581. "Edit list and finish with \\[save-buffer]")))
  12582. (customize-variable 'org-agenda-files)))
  12583. (defun org-store-new-agenda-file-list (list)
  12584. "Set new value for the agenda file list and save it correctly."
  12585. (if (stringp org-agenda-files)
  12586. (let ((f org-agenda-files) b)
  12587. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12588. (with-temp-file f
  12589. (insert (mapconcat 'identity list "\n") "\n")))
  12590. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12591. (org-insert-mode-line-in-empty-file nil))
  12592. (setq org-agenda-files list)
  12593. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12594. (defun org-read-agenda-file-list ()
  12595. "Read the list of agenda files from a file."
  12596. (when (file-directory-p org-agenda-files)
  12597. (error "`org-agenda-files' cannot be a single directory"))
  12598. (when (stringp org-agenda-files)
  12599. (with-temp-buffer
  12600. (insert-file-contents org-agenda-files)
  12601. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12602. ;;;###autoload
  12603. (defun org-cycle-agenda-files ()
  12604. "Cycle through the files in `org-agenda-files'.
  12605. If the current buffer visits an agenda file, find the next one in the list.
  12606. If the current buffer does not, find the first agenda file."
  12607. (interactive)
  12608. (let* ((fs (org-agenda-files t))
  12609. (files (append fs (list (car fs))))
  12610. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12611. file)
  12612. (unless files (error "No agenda files"))
  12613. (catch 'exit
  12614. (while (setq file (pop files))
  12615. (if (equal (file-truename file) tcf)
  12616. (when (car files)
  12617. (find-file (car files))
  12618. (throw 'exit t))))
  12619. (find-file (car fs)))
  12620. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12621. (defun org-agenda-file-to-front (&optional to-end)
  12622. "Move/add the current file to the top of the agenda file list.
  12623. If the file is not present in the list, it is added to the front. If it is
  12624. present, it is moved there. With optional argument TO-END, add/move to the
  12625. end of the list."
  12626. (interactive "P")
  12627. (let ((org-agenda-skip-unavailable-files nil)
  12628. (file-alist (mapcar (lambda (x)
  12629. (cons (file-truename x) x))
  12630. (org-agenda-files t)))
  12631. (ctf (file-truename buffer-file-name))
  12632. x had)
  12633. (setq x (assoc ctf file-alist) had x)
  12634. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12635. (if to-end
  12636. (setq file-alist (append (delq x file-alist) (list x)))
  12637. (setq file-alist (cons x (delq x file-alist))))
  12638. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12639. (org-install-agenda-files-menu)
  12640. (message "File %s to %s of agenda file list"
  12641. (if had "moved" "added") (if to-end "end" "front"))))
  12642. (defun org-remove-file (&optional file)
  12643. "Remove current file from the list of files in variable `org-agenda-files'.
  12644. These are the files which are being checked for agenda entries.
  12645. Optional argument FILE means, use this file instead of the current."
  12646. (interactive)
  12647. (let* ((org-agenda-skip-unavailable-files nil)
  12648. (file (or file buffer-file-name))
  12649. (true-file (file-truename file))
  12650. (afile (abbreviate-file-name file))
  12651. (files (delq nil (mapcar
  12652. (lambda (x)
  12653. (if (equal true-file
  12654. (file-truename x))
  12655. nil x))
  12656. (org-agenda-files t)))))
  12657. (if (not (= (length files) (length (org-agenda-files t))))
  12658. (progn
  12659. (org-store-new-agenda-file-list files)
  12660. (org-install-agenda-files-menu)
  12661. (message "Removed file: %s" afile))
  12662. (message "File was not in list: %s (not removed)" afile))))
  12663. (defun org-file-menu-entry (file)
  12664. (vector file (list 'find-file file) t))
  12665. (defun org-check-agenda-file (file)
  12666. "Make sure FILE exists. If not, ask user what to do."
  12667. (when (not (file-exists-p file))
  12668. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  12669. (abbreviate-file-name file))
  12670. (let ((r (downcase (read-char-exclusive))))
  12671. (cond
  12672. ((equal r ?r)
  12673. (org-remove-file file)
  12674. (throw 'nextfile t))
  12675. (t (error "Abort"))))))
  12676. (defun org-get-agenda-file-buffer (file)
  12677. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12678. it to the list of buffers which might be released later."
  12679. (let ((buf (org-find-base-buffer-visiting file)))
  12680. (if buf
  12681. buf ; just return it
  12682. ;; Make a new buffer and remember it
  12683. (setq buf (find-file-noselect file))
  12684. (if buf (push buf org-agenda-new-buffers))
  12685. buf)))
  12686. (defun org-release-buffers (blist)
  12687. "Release all buffers in list, asking the user for confirmation when needed.
  12688. When a buffer is unmodified, it is just killed. When modified, it is saved
  12689. \(if the user agrees) and then killed."
  12690. (let (buf file)
  12691. (while (setq buf (pop blist))
  12692. (setq file (buffer-file-name buf))
  12693. (when (and (buffer-modified-p buf)
  12694. file
  12695. (y-or-n-p (format "Save file %s? " file)))
  12696. (with-current-buffer buf (save-buffer)))
  12697. (kill-buffer buf))))
  12698. (defun org-prepare-agenda-buffers (files)
  12699. "Create buffers for all agenda files, protect archived trees and comments."
  12700. (interactive)
  12701. (let ((pa '(:org-archived t))
  12702. (pc '(:org-comment t))
  12703. (pall '(:org-archived t :org-comment t))
  12704. (inhibit-read-only t)
  12705. (rea (concat ":" org-archive-tag ":"))
  12706. bmp file re)
  12707. (save-excursion
  12708. (save-restriction
  12709. (while (setq file (pop files))
  12710. (catch 'nextfile
  12711. (if (bufferp file)
  12712. (set-buffer file)
  12713. (org-check-agenda-file file)
  12714. (set-buffer (org-get-agenda-file-buffer file)))
  12715. (widen)
  12716. (setq bmp (buffer-modified-p))
  12717. (org-refresh-category-properties)
  12718. (setq org-todo-keywords-for-agenda
  12719. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12720. (setq org-done-keywords-for-agenda
  12721. (append org-done-keywords-for-agenda org-done-keywords))
  12722. (setq org-todo-keyword-alist-for-agenda
  12723. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12724. (setq org-tag-alist-for-agenda
  12725. (append org-tag-alist-for-agenda org-tag-alist))
  12726. (save-excursion
  12727. (remove-text-properties (point-min) (point-max) pall)
  12728. (when org-agenda-skip-archived-trees
  12729. (goto-char (point-min))
  12730. (while (re-search-forward rea nil t)
  12731. (if (org-on-heading-p t)
  12732. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12733. (goto-char (point-min))
  12734. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12735. (while (re-search-forward re nil t)
  12736. (add-text-properties
  12737. (match-beginning 0) (org-end-of-subtree t) pc)))
  12738. (set-buffer-modified-p bmp)))))
  12739. (setq org-todo-keyword-alist-for-agenda
  12740. (org-uniquify org-todo-keyword-alist-for-agenda)
  12741. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12742. ;;;; Embedded LaTeX
  12743. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12744. "Keymap for the minor `org-cdlatex-mode'.")
  12745. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12746. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12747. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12748. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12749. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12750. (defvar org-cdlatex-texmathp-advice-is-done nil
  12751. "Flag remembering if we have applied the advice to texmathp already.")
  12752. (define-minor-mode org-cdlatex-mode
  12753. "Toggle the minor `org-cdlatex-mode'.
  12754. This mode supports entering LaTeX environment and math in LaTeX fragments
  12755. in Org-mode.
  12756. \\{org-cdlatex-mode-map}"
  12757. nil " OCDL" nil
  12758. (when org-cdlatex-mode (require 'cdlatex))
  12759. (unless org-cdlatex-texmathp-advice-is-done
  12760. (setq org-cdlatex-texmathp-advice-is-done t)
  12761. (defadvice texmathp (around org-math-always-on activate)
  12762. "Always return t in org-mode buffers.
  12763. This is because we want to insert math symbols without dollars even outside
  12764. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12765. an embedded LaTeX fragment, let texmathp do its job.
  12766. \\[org-cdlatex-mode-map]"
  12767. (interactive)
  12768. (let (p)
  12769. (cond
  12770. ((not (org-mode-p)) ad-do-it)
  12771. ((eq this-command 'cdlatex-math-symbol)
  12772. (setq ad-return-value t
  12773. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12774. (t
  12775. (let ((p (org-inside-LaTeX-fragment-p)))
  12776. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12777. (setq ad-return-value t
  12778. texmathp-why '("Org-mode embedded math" . 0))
  12779. (if p ad-do-it)))))))))
  12780. (defun turn-on-org-cdlatex ()
  12781. "Unconditionally turn on `org-cdlatex-mode'."
  12782. (org-cdlatex-mode 1))
  12783. (defun org-inside-LaTeX-fragment-p ()
  12784. "Test if point is inside a LaTeX fragment.
  12785. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12786. sequence appearing also before point.
  12787. Even though the matchers for math are configurable, this function assumes
  12788. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12789. delimiters are skipped when they have been removed by customization.
  12790. The return value is nil, or a cons cell with the delimiter and
  12791. and the position of this delimiter.
  12792. This function does a reasonably good job, but can locally be fooled by
  12793. for example currency specifications. For example it will assume being in
  12794. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12795. fragments that are properly closed, but during editing, we have to live
  12796. with the uncertainty caused by missing closing delimiters. This function
  12797. looks only before point, not after."
  12798. (catch 'exit
  12799. (let ((pos (point))
  12800. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12801. (lim (progn
  12802. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12803. (point)))
  12804. dd-on str (start 0) m re)
  12805. (goto-char pos)
  12806. (when dodollar
  12807. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12808. re (nth 1 (assoc "$" org-latex-regexps)))
  12809. (while (string-match re str start)
  12810. (cond
  12811. ((= (match-end 0) (length str))
  12812. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12813. ((= (match-end 0) (- (length str) 5))
  12814. (throw 'exit nil))
  12815. (t (setq start (match-end 0))))))
  12816. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12817. (goto-char pos)
  12818. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12819. (and (match-beginning 2) (throw 'exit nil))
  12820. ;; count $$
  12821. (while (re-search-backward "\\$\\$" lim t)
  12822. (setq dd-on (not dd-on)))
  12823. (goto-char pos)
  12824. (if dd-on (cons "$$" m))))))
  12825. (defun org-try-cdlatex-tab ()
  12826. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12827. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12828. - inside a LaTeX fragment, or
  12829. - after the first word in a line, where an abbreviation expansion could
  12830. insert a LaTeX environment."
  12831. (when org-cdlatex-mode
  12832. (cond
  12833. ((save-excursion
  12834. (skip-chars-backward "a-zA-Z0-9*")
  12835. (skip-chars-backward " \t")
  12836. (bolp))
  12837. (cdlatex-tab) t)
  12838. ((org-inside-LaTeX-fragment-p)
  12839. (cdlatex-tab) t)
  12840. (t nil))))
  12841. (defun org-cdlatex-underscore-caret (&optional arg)
  12842. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12843. Revert to the normal definition outside of these fragments."
  12844. (interactive "P")
  12845. (if (org-inside-LaTeX-fragment-p)
  12846. (call-interactively 'cdlatex-sub-superscript)
  12847. (let (org-cdlatex-mode)
  12848. (call-interactively (key-binding (vector last-input-event))))))
  12849. (defun org-cdlatex-math-modify (&optional arg)
  12850. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12851. Revert to the normal definition outside of these fragments."
  12852. (interactive "P")
  12853. (if (org-inside-LaTeX-fragment-p)
  12854. (call-interactively 'cdlatex-math-modify)
  12855. (let (org-cdlatex-mode)
  12856. (call-interactively (key-binding (vector last-input-event))))))
  12857. (defvar org-latex-fragment-image-overlays nil
  12858. "List of overlays carrying the images of latex fragments.")
  12859. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12860. (defun org-remove-latex-fragment-image-overlays ()
  12861. "Remove all overlays with LaTeX fragment images in current buffer."
  12862. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12863. (setq org-latex-fragment-image-overlays nil))
  12864. (defun org-preview-latex-fragment (&optional subtree)
  12865. "Preview the LaTeX fragment at point, or all locally or globally.
  12866. If the cursor is in a LaTeX fragment, create the image and overlay
  12867. it over the source code. If there is no fragment at point, display
  12868. all fragments in the current text, from one headline to the next. With
  12869. prefix SUBTREE, display all fragments in the current subtree. With a
  12870. double prefix `C-u C-u', or when the cursor is before the first headline,
  12871. display all fragments in the buffer.
  12872. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12873. (interactive "P")
  12874. (org-remove-latex-fragment-image-overlays)
  12875. (save-excursion
  12876. (save-restriction
  12877. (let (beg end at msg)
  12878. (cond
  12879. ((or (equal subtree '(16))
  12880. (not (save-excursion
  12881. (re-search-backward (concat "^" outline-regexp) nil t))))
  12882. (setq beg (point-min) end (point-max)
  12883. msg "Creating images for buffer...%s"))
  12884. ((equal subtree '(4))
  12885. (org-back-to-heading)
  12886. (setq beg (point) end (org-end-of-subtree t)
  12887. msg "Creating images for subtree...%s"))
  12888. (t
  12889. (if (setq at (org-inside-LaTeX-fragment-p))
  12890. (goto-char (max (point-min) (- (cdr at) 2)))
  12891. (org-back-to-heading))
  12892. (setq beg (point) end (progn (outline-next-heading) (point))
  12893. msg (if at "Creating image...%s"
  12894. "Creating images for entry...%s"))))
  12895. (message msg "")
  12896. (narrow-to-region beg end)
  12897. (goto-char beg)
  12898. (org-format-latex
  12899. (concat "ltxpng/" (file-name-sans-extension
  12900. (file-name-nondirectory
  12901. buffer-file-name)))
  12902. default-directory 'overlays msg at 'forbuffer)
  12903. (message msg "done. Use `C-c C-c' to remove images.")))))
  12904. (defvar org-latex-regexps
  12905. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12906. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12907. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12908. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12909. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12910. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12911. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  12912. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  12913. "Regular expressions for matching embedded LaTeX.")
  12914. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12915. "Replace LaTeX fragments with links to an image, and produce images."
  12916. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12917. (let* ((prefixnodir (file-name-nondirectory prefix))
  12918. (absprefix (expand-file-name prefix dir))
  12919. (todir (file-name-directory absprefix))
  12920. (opt org-format-latex-options)
  12921. (matchers (plist-get opt :matchers))
  12922. (re-list org-latex-regexps)
  12923. (cnt 0) txt link beg end re e checkdir
  12924. executables-checked
  12925. m n block linkfile movefile ov)
  12926. ;; Check if there are old images files with this prefix, and remove them
  12927. (when (file-directory-p todir)
  12928. (mapc 'delete-file
  12929. (directory-files
  12930. todir 'full
  12931. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12932. ;; Check the different regular expressions
  12933. (while (setq e (pop re-list))
  12934. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12935. block (if (nth 3 e) "\n\n" ""))
  12936. (when (member m matchers)
  12937. (goto-char (point-min))
  12938. (while (re-search-forward re nil t)
  12939. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12940. (not (get-text-property (match-beginning n)
  12941. 'org-protected))
  12942. (or (not overlays)
  12943. (not (eq (get-char-property (match-beginning n)
  12944. 'org-overlay-type)
  12945. 'org-latex-overlay))))
  12946. (setq txt (match-string n)
  12947. beg (match-beginning n) end (match-end n)
  12948. cnt (1+ cnt)
  12949. linkfile (format "%s_%04d.png" prefix cnt)
  12950. movefile (format "%s_%04d.png" absprefix cnt)
  12951. link (concat block "[[file:" linkfile "]]" block))
  12952. (if msg (message msg cnt))
  12953. (goto-char beg)
  12954. (unless checkdir ; make sure the directory exists
  12955. (setq checkdir t)
  12956. (or (file-directory-p todir) (make-directory todir)))
  12957. (unless executables-checked
  12958. (org-check-external-command
  12959. "latex" "needed to convert LaTeX fragments to images")
  12960. (org-check-external-command
  12961. "dvipng" "needed to convert LaTeX fragments to images")
  12962. (setq executables-checked t))
  12963. (org-create-formula-image
  12964. txt movefile opt forbuffer)
  12965. (if overlays
  12966. (progn
  12967. (mapc (lambda (o)
  12968. (if (eq (org-overlay-get o 'org-overlay-type)
  12969. 'org-latex-overlay)
  12970. (org-delete-overlay o)))
  12971. (org-overlays-in beg end))
  12972. (setq ov (org-make-overlay beg end))
  12973. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  12974. (if (featurep 'xemacs)
  12975. (progn
  12976. (org-overlay-put ov 'invisible t)
  12977. (org-overlay-put
  12978. ov 'end-glyph
  12979. (make-glyph (vector 'png :file movefile))))
  12980. (org-overlay-put
  12981. ov 'display
  12982. (list 'image :type 'png :file movefile :ascent 'center)))
  12983. (push ov org-latex-fragment-image-overlays)
  12984. (goto-char end))
  12985. (delete-region beg end)
  12986. (insert link))))))))
  12987. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  12988. ;; This function borrows from Ganesh Swami's latex2png.el
  12989. (defun org-create-formula-image (string tofile options buffer)
  12990. (require 'org-latex)
  12991. (let* ((tmpdir (if (featurep 'xemacs)
  12992. (temp-directory)
  12993. temporary-file-directory))
  12994. (texfilebase (make-temp-name
  12995. (expand-file-name "orgtex" tmpdir)))
  12996. (texfile (concat texfilebase ".tex"))
  12997. (dvifile (concat texfilebase ".dvi"))
  12998. (pngfile (concat texfilebase ".png"))
  12999. (fnh (if (featurep 'xemacs)
  13000. (font-height (get-face-font 'default))
  13001. (face-attribute 'default :height nil)))
  13002. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13003. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13004. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13005. "Black"))
  13006. (bg (or (plist-get options (if buffer :background :html-background))
  13007. "Transparent")))
  13008. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13009. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13010. (with-temp-file texfile
  13011. (insert org-format-latex-header
  13012. (if org-export-latex-packages-alist
  13013. (concat "\n"
  13014. (mapconcat (lambda(p)
  13015. (if (equal "" (car p))
  13016. (format "\\usepackage{%s}" (cadr p))
  13017. (format "\\usepackage[%s]{%s}"
  13018. (car p) (cadr p))))
  13019. org-export-latex-packages-alist "\n"))
  13020. "")
  13021. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13022. (let ((dir default-directory))
  13023. (condition-case nil
  13024. (progn
  13025. (cd tmpdir)
  13026. (call-process "latex" nil nil nil texfile))
  13027. (error nil))
  13028. (cd dir))
  13029. (if (not (file-exists-p dvifile))
  13030. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13031. (condition-case nil
  13032. (call-process "dvipng" nil nil nil
  13033. "-fg" fg "-bg" bg
  13034. "-D" dpi
  13035. ;;"-x" scale "-y" scale
  13036. "-T" "tight"
  13037. "-o" pngfile
  13038. dvifile)
  13039. (error nil))
  13040. (if (not (file-exists-p pngfile))
  13041. (progn (message "Failed to create png file from %s" texfile) nil)
  13042. ;; Use the requested file name and clean up
  13043. (copy-file pngfile tofile 'replace)
  13044. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13045. (delete-file (concat texfilebase e)))
  13046. pngfile))))
  13047. (defun org-dvipng-color (attr)
  13048. "Return an rgb color specification for dvipng."
  13049. (apply 'format "rgb %s %s %s"
  13050. (mapcar 'org-normalize-color
  13051. (color-values (face-attribute 'default attr nil)))))
  13052. (defun org-normalize-color (value)
  13053. "Return string to be used as color value for an RGB component."
  13054. (format "%g" (/ value 65535.0)))
  13055. ;;;; Key bindings
  13056. ;; Make `C-c C-x' a prefix key
  13057. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13058. ;; TAB key with modifiers
  13059. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13060. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13061. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13062. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13063. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13064. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13065. ;; The following line is necessary under Suse GNU/Linux
  13066. (unless (featurep 'xemacs)
  13067. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13068. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13069. (define-key org-mode-map [backtab] 'org-shifttab)
  13070. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13071. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13072. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13073. ;; Cursor keys with modifiers
  13074. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13075. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13076. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13077. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13078. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13079. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13080. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13081. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13082. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13083. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13084. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13085. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13086. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13087. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13088. ;;; Extra keys for tty access.
  13089. ;; We only set them when really needed because otherwise the
  13090. ;; menus don't show the simple keys
  13091. (when (or org-use-extra-keys
  13092. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13093. (not window-system))
  13094. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13095. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13096. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13097. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13098. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13099. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13100. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13101. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13102. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13103. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13104. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13105. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13106. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13107. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13108. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13109. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13110. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13111. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13112. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13113. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13114. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13115. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13116. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13117. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13118. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13119. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13120. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13121. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13122. ;; All the other keys
  13123. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13124. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13125. (if (boundp 'narrow-map)
  13126. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13127. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13128. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13129. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13130. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13131. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13132. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  13133. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13134. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13135. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13136. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13137. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13138. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13139. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13140. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13141. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13142. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13143. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13144. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13145. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13146. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13147. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13148. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13149. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13150. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13151. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13152. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13153. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13154. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13155. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13156. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13157. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13158. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13159. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13160. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13161. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13162. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13163. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13164. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13165. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13166. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13167. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13168. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13169. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13170. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13171. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13172. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13173. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13174. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13175. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13176. (org-defkey org-mode-map "\C-m" 'org-return)
  13177. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13178. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13179. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13180. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13181. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13182. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13183. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13184. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13185. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13186. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13187. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13188. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13189. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13190. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13191. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13192. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13193. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13194. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13195. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13196. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13197. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13198. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13199. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13200. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13201. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13202. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13203. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13204. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13205. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13206. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13207. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13208. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13209. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13210. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13211. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13212. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13213. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13214. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13215. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13216. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13217. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13218. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13219. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13220. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13221. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13222. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13223. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13224. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13225. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13226. (when (featurep 'xemacs)
  13227. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13228. (defvar org-self-insert-command-undo-counter 0)
  13229. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13230. (defun org-self-insert-command (N)
  13231. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13232. If the cursor is in a table looking at whitespace, the whitespace is
  13233. overwritten, and the table is not marked as requiring realignment."
  13234. (interactive "p")
  13235. (if (and
  13236. (org-table-p)
  13237. (progn
  13238. ;; check if we blank the field, and if that triggers align
  13239. (and (featurep 'org-table) org-table-auto-blank-field
  13240. (member last-command
  13241. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13242. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13243. ;; got extra space, this field does not determine column width
  13244. (let (org-table-may-need-update) (org-table-blank-field))
  13245. ;; no extra space, this field may determine column width
  13246. (org-table-blank-field)))
  13247. t)
  13248. (eq N 1)
  13249. (looking-at "[^|\n]* |"))
  13250. (let (org-table-may-need-update)
  13251. (goto-char (1- (match-end 0)))
  13252. (delete-backward-char 1)
  13253. (goto-char (match-beginning 0))
  13254. (self-insert-command N))
  13255. (setq org-table-may-need-update t)
  13256. (self-insert-command N)
  13257. (org-fix-tags-on-the-fly)
  13258. (if org-self-insert-cluster-for-undo
  13259. (if (not (eq last-command 'org-self-insert-command))
  13260. (setq org-self-insert-command-undo-counter 1)
  13261. (if (>= org-self-insert-command-undo-counter 20)
  13262. (setq org-self-insert-command-undo-counter 1)
  13263. (and (> org-self-insert-command-undo-counter 0)
  13264. buffer-undo-list
  13265. (not (cadr buffer-undo-list)) ; remove nil entry
  13266. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13267. (setq org-self-insert-command-undo-counter
  13268. (1+ org-self-insert-command-undo-counter)))))))
  13269. (defun org-fix-tags-on-the-fly ()
  13270. (when (and (equal (char-after (point-at-bol)) ?*)
  13271. (org-on-heading-p))
  13272. (org-align-tags-here org-tags-column)))
  13273. (defun org-delete-backward-char (N)
  13274. "Like `delete-backward-char', insert whitespace at field end in tables.
  13275. When deleting backwards, in tables this function will insert whitespace in
  13276. front of the next \"|\" separator, to keep the table aligned. The table will
  13277. still be marked for re-alignment if the field did fill the entire column,
  13278. because, in this case the deletion might narrow the column."
  13279. (interactive "p")
  13280. (if (and (org-table-p)
  13281. (eq N 1)
  13282. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13283. (looking-at ".*?|"))
  13284. (let ((pos (point))
  13285. (noalign (looking-at "[^|\n\r]* |"))
  13286. (c org-table-may-need-update))
  13287. (backward-delete-char N)
  13288. (skip-chars-forward "^|")
  13289. (insert " ")
  13290. (goto-char (1- pos))
  13291. ;; noalign: if there were two spaces at the end, this field
  13292. ;; does not determine the width of the column.
  13293. (if noalign (setq org-table-may-need-update c)))
  13294. (backward-delete-char N)
  13295. (org-fix-tags-on-the-fly)))
  13296. (defun org-delete-char (N)
  13297. "Like `delete-char', but insert whitespace at field end in tables.
  13298. When deleting characters, in tables this function will insert whitespace in
  13299. front of the next \"|\" separator, to keep the table aligned. The table will
  13300. still be marked for re-alignment if the field did fill the entire column,
  13301. because, in this case the deletion might narrow the column."
  13302. (interactive "p")
  13303. (if (and (org-table-p)
  13304. (not (bolp))
  13305. (not (= (char-after) ?|))
  13306. (eq N 1))
  13307. (if (looking-at ".*?|")
  13308. (let ((pos (point))
  13309. (noalign (looking-at "[^|\n\r]* |"))
  13310. (c org-table-may-need-update))
  13311. (replace-match (concat
  13312. (substring (match-string 0) 1 -1)
  13313. " |"))
  13314. (goto-char pos)
  13315. ;; noalign: if there were two spaces at the end, this field
  13316. ;; does not determine the width of the column.
  13317. (if noalign (setq org-table-may-need-update c)))
  13318. (delete-char N))
  13319. (delete-char N)
  13320. (org-fix-tags-on-the-fly)))
  13321. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13322. (put 'org-self-insert-command 'delete-selection t)
  13323. (put 'orgtbl-self-insert-command 'delete-selection t)
  13324. (put 'org-delete-char 'delete-selection 'supersede)
  13325. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13326. (put 'org-yank 'delete-selection 'yank)
  13327. ;; Make `flyspell-mode' delay after some commands
  13328. (put 'org-self-insert-command 'flyspell-delayed t)
  13329. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13330. (put 'org-delete-char 'flyspell-delayed t)
  13331. (put 'org-delete-backward-char 'flyspell-delayed t)
  13332. ;; Make pabbrev-mode expand after org-mode commands
  13333. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13334. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13335. ;; How to do this: Measure non-white length of current string
  13336. ;; If equal to column width, we should realign.
  13337. (defun org-remap (map &rest commands)
  13338. "In MAP, remap the functions given in COMMANDS.
  13339. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13340. (let (new old)
  13341. (while commands
  13342. (setq old (pop commands) new (pop commands))
  13343. (if (fboundp 'command-remapping)
  13344. (org-defkey map (vector 'remap old) new)
  13345. (substitute-key-definition old new map global-map)))))
  13346. (when (eq org-enable-table-editor 'optimized)
  13347. ;; If the user wants maximum table support, we need to hijack
  13348. ;; some standard editing functions
  13349. (org-remap org-mode-map
  13350. 'self-insert-command 'org-self-insert-command
  13351. 'delete-char 'org-delete-char
  13352. 'delete-backward-char 'org-delete-backward-char)
  13353. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13354. (defvar org-ctrl-c-ctrl-c-hook nil
  13355. "Hook for functions attaching themselves to `C-c C-c'.
  13356. This can be used to add additional functionality to the C-c C-c key which
  13357. executes context-dependent commands.
  13358. Each function will be called with no arguments. The function must check
  13359. if the context is appropriate for it to act. If yes, it should do its
  13360. thing and then return a non-nil value. If the context is wrong,
  13361. just do nothing and return nil.")
  13362. (defvar org-tab-first-hook nil
  13363. "Hook for functions to attach themselves to TAB.
  13364. See `org-ctrl-c-ctrl-c-hook' for more information.
  13365. This hook runs as the first action when TAB is pressed, even before
  13366. `org-cycle' messes around with the `outline-regexp' to cater for
  13367. inline tasks and plain list item folding.
  13368. If any function in this hook returns t, not other actions like table
  13369. field motion visibility cycling will be done.")
  13370. (defvar org-tab-after-check-for-table-hook nil
  13371. "Hook for functions to attach themselves to TAB.
  13372. See `org-ctrl-c-ctrl-c-hook' for more information.
  13373. This hook runs after it has been established that the cursor is not in a
  13374. table, but before checking if the cursor is in a headline or if global cycling
  13375. should be done.
  13376. If any function in this hook returns t, not other actions like visibility
  13377. cycling will be done.")
  13378. (defvar org-tab-after-check-for-cycling-hook nil
  13379. "Hook for functions to attach themselves to TAB.
  13380. See `org-ctrl-c-ctrl-c-hook' for more information.
  13381. This hook runs after it has been established that not table field motion and
  13382. not visibility should be done because of current context. This is probably
  13383. the place where a package like yasnippets can hook in.")
  13384. (defvar org-metaleft-hook nil
  13385. "Hook for functions attaching themselves to `M-left'.
  13386. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13387. (defvar org-metaright-hook nil
  13388. "Hook for functions attaching themselves to `M-right'.
  13389. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13390. (defvar org-metaup-hook nil
  13391. "Hook for functions attaching themselves to `M-up'.
  13392. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13393. (defvar org-metadown-hook nil
  13394. "Hook for functions attaching themselves to `M-down'.
  13395. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13396. (defvar org-shiftmetaleft-hook nil
  13397. "Hook for functions attaching themselves to `M-S-left'.
  13398. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13399. (defvar org-shiftmetaright-hook nil
  13400. "Hook for functions attaching themselves to `M-S-right'.
  13401. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13402. (defvar org-shiftmetaup-hook nil
  13403. "Hook for functions attaching themselves to `M-S-up'.
  13404. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13405. (defvar org-shiftmetadown-hook nil
  13406. "Hook for functions attaching themselves to `M-S-down'.
  13407. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13408. (defvar org-metareturn-hook nil
  13409. "Hook for functions attaching themselves to `M-RET'.
  13410. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13411. (defun org-modifier-cursor-error ()
  13412. "Throw an error, a modified cursor command was applied in wrong context."
  13413. (error "This command is active in special context like tables, headlines or items"))
  13414. (defun org-shiftselect-error ()
  13415. "Throw an error because Shift-Cursor command was applied in wrong context."
  13416. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13417. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13418. (error "This command works only in special context like headlines or timestamps.")))
  13419. (defun org-call-for-shift-select (cmd)
  13420. (let ((this-command-keys-shift-translated t))
  13421. (call-interactively cmd)))
  13422. (defun org-shifttab (&optional arg)
  13423. "Global visibility cycling or move to previous table field.
  13424. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13425. on context.
  13426. See the individual commands for more information."
  13427. (interactive "P")
  13428. (cond
  13429. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13430. ((integerp arg)
  13431. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13432. (message "Content view to level: %d" arg)
  13433. (org-content (prefix-numeric-value arg2))
  13434. (setq org-cycle-global-status 'overview)))
  13435. (t (call-interactively 'org-global-cycle))))
  13436. (defun org-shiftmetaleft ()
  13437. "Promote subtree or delete table column.
  13438. Calls `org-promote-subtree', `org-outdent-item',
  13439. or `org-table-delete-column', depending on context.
  13440. See the individual commands for more information."
  13441. (interactive)
  13442. (cond
  13443. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13444. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13445. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13446. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13447. (t (org-modifier-cursor-error))))
  13448. (defun org-shiftmetaright ()
  13449. "Demote subtree or insert table column.
  13450. Calls `org-demote-subtree', `org-indent-item',
  13451. or `org-table-insert-column', depending on context.
  13452. See the individual commands for more information."
  13453. (interactive)
  13454. (cond
  13455. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13456. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13457. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13458. ((org-at-item-p) (call-interactively 'org-indent-item))
  13459. (t (org-modifier-cursor-error))))
  13460. (defun org-shiftmetaup (&optional arg)
  13461. "Move subtree up or kill table row.
  13462. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13463. `org-move-item-up' depending on context. See the individual commands
  13464. for more information."
  13465. (interactive "P")
  13466. (cond
  13467. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13468. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13469. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13470. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13471. (t (org-modifier-cursor-error))))
  13472. (defun org-shiftmetadown (&optional arg)
  13473. "Move subtree down or insert table row.
  13474. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13475. `org-move-item-down', depending on context. See the individual
  13476. commands for more information."
  13477. (interactive "P")
  13478. (cond
  13479. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13480. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13481. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13482. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13483. (t (org-modifier-cursor-error))))
  13484. (defun org-metaleft (&optional arg)
  13485. "Promote heading or move table column to left.
  13486. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13487. With no specific context, calls the Emacs default `backward-word'.
  13488. See the individual commands for more information."
  13489. (interactive "P")
  13490. (cond
  13491. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13492. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13493. ((or (org-on-heading-p)
  13494. (and (org-region-active-p)
  13495. (save-excursion
  13496. (goto-char (region-beginning))
  13497. (org-on-heading-p))))
  13498. (call-interactively 'org-do-promote))
  13499. ((or (org-at-item-p)
  13500. (and (org-region-active-p)
  13501. (save-excursion
  13502. (goto-char (region-beginning))
  13503. (org-at-item-p))))
  13504. (call-interactively 'org-outdent-item))
  13505. (t (call-interactively 'backward-word))))
  13506. (defun org-metaright (&optional arg)
  13507. "Demote subtree or move table column to right.
  13508. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13509. With no specific context, calls the Emacs default `forward-word'.
  13510. See the individual commands for more information."
  13511. (interactive "P")
  13512. (cond
  13513. ((run-hook-with-args-until-success 'org-metaright-hook))
  13514. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13515. ((or (org-on-heading-p)
  13516. (and (org-region-active-p)
  13517. (save-excursion
  13518. (goto-char (region-beginning))
  13519. (org-on-heading-p))))
  13520. (call-interactively 'org-do-demote))
  13521. ((or (org-at-item-p)
  13522. (and (org-region-active-p)
  13523. (save-excursion
  13524. (goto-char (region-beginning))
  13525. (org-at-item-p))))
  13526. (call-interactively 'org-indent-item))
  13527. (t (call-interactively 'forward-word))))
  13528. (defun org-metaup (&optional arg)
  13529. "Move subtree up or move table row up.
  13530. Calls `org-move-subtree-up' or `org-table-move-row' or
  13531. `org-move-item-up', depending on context. See the individual commands
  13532. for more information."
  13533. (interactive "P")
  13534. (cond
  13535. ((run-hook-with-args-until-success 'org-metaup-hook))
  13536. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13537. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13538. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13539. (t (transpose-lines 1) (beginning-of-line -1))))
  13540. (defun org-metadown (&optional arg)
  13541. "Move subtree down or move table row down.
  13542. Calls `org-move-subtree-down' or `org-table-move-row' or
  13543. `org-move-item-down', depending on context. See the individual
  13544. commands for more information."
  13545. (interactive "P")
  13546. (cond
  13547. ((run-hook-with-args-until-success 'org-metadown-hook))
  13548. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13549. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13550. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13551. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13552. (defun org-shiftup (&optional arg)
  13553. "Increase item in timestamp or increase priority of current headline.
  13554. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13555. depending on context. See the individual commands for more information."
  13556. (interactive "P")
  13557. (cond
  13558. ((and org-support-shift-select (org-region-active-p))
  13559. (org-call-for-shift-select 'previous-line))
  13560. ((org-at-timestamp-p t)
  13561. (call-interactively (if org-edit-timestamp-down-means-later
  13562. 'org-timestamp-down 'org-timestamp-up)))
  13563. ((and (not (eq org-support-shift-select 'always))
  13564. org-enable-priority-commands
  13565. (org-on-heading-p))
  13566. (call-interactively 'org-priority-up))
  13567. ((and (not org-support-shift-select) (org-at-item-p))
  13568. (call-interactively 'org-previous-item))
  13569. ((org-clocktable-try-shift 'up arg))
  13570. (org-support-shift-select
  13571. (org-call-for-shift-select 'previous-line))
  13572. (t (org-shiftselect-error))))
  13573. (defun org-shiftdown (&optional arg)
  13574. "Decrease item in timestamp or decrease priority of current headline.
  13575. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13576. depending on context. See the individual commands for more information."
  13577. (interactive "P")
  13578. (cond
  13579. ((and org-support-shift-select (org-region-active-p))
  13580. (org-call-for-shift-select 'next-line))
  13581. ((org-at-timestamp-p t)
  13582. (call-interactively (if org-edit-timestamp-down-means-later
  13583. 'org-timestamp-up 'org-timestamp-down)))
  13584. ((and (not (eq org-support-shift-select 'always))
  13585. org-enable-priority-commands
  13586. (org-on-heading-p))
  13587. (call-interactively 'org-priority-down))
  13588. ((and (not org-support-shift-select) (org-at-item-p))
  13589. (call-interactively 'org-next-item))
  13590. ((org-clocktable-try-shift 'down arg))
  13591. (org-support-shift-select
  13592. (org-call-for-shift-select 'next-line))
  13593. (t (org-shiftselect-error))))
  13594. (defun org-shiftright (&optional arg)
  13595. "Cycle the thing at point or in the current line, depending on context.
  13596. Depending on context, this does one of the following:
  13597. - switch a timestamp at point one day into the future
  13598. - on a headline, switch to the next TODO keyword.
  13599. - on an item, switch entire list to the next bullet type
  13600. - on a property line, switch to the next allowed value
  13601. - on a clocktable definition line, move time block into the future"
  13602. (interactive "P")
  13603. (cond
  13604. ((and org-support-shift-select (org-region-active-p))
  13605. (org-call-for-shift-select 'forward-char))
  13606. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13607. ((and (not (eq org-support-shift-select 'always))
  13608. (org-on-heading-p))
  13609. (let ((org-inhibit-logging
  13610. (not org-treat-S-cursor-todo-selection-as-state-change))
  13611. (org-inhibit-blocking
  13612. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13613. (org-call-with-arg 'org-todo 'right)))
  13614. ((or (and org-support-shift-select
  13615. (not (eq org-support-shift-select 'always))
  13616. (org-at-item-bullet-p))
  13617. (and (not org-support-shift-select) (org-at-item-p)))
  13618. (org-call-with-arg 'org-cycle-list-bullet nil))
  13619. ((and (not (eq org-support-shift-select 'always))
  13620. (org-at-property-p))
  13621. (call-interactively 'org-property-next-allowed-value))
  13622. ((org-clocktable-try-shift 'right arg))
  13623. (org-support-shift-select
  13624. (org-call-for-shift-select 'forward-char))
  13625. (t (org-shiftselect-error))))
  13626. (defun org-shiftleft (&optional arg)
  13627. "Cycle the thing at point or in the current line, depending on context.
  13628. Depending on context, this does one of the following:
  13629. - switch a timestamp at point one day into the past
  13630. - on a headline, switch to the previous TODO keyword.
  13631. - on an item, switch entire list to the previous bullet type
  13632. - on a property line, switch to the previous allowed value
  13633. - on a clocktable definition line, move time block into the past"
  13634. (interactive "P")
  13635. (cond
  13636. ((and org-support-shift-select (org-region-active-p))
  13637. (org-call-for-shift-select 'backward-char))
  13638. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13639. ((and (not (eq org-support-shift-select 'always))
  13640. (org-on-heading-p))
  13641. (let ((org-inhibit-logging
  13642. (not org-treat-S-cursor-todo-selection-as-state-change))
  13643. (org-inhibit-blocking
  13644. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13645. (org-call-with-arg 'org-todo 'left)))
  13646. ((or (and org-support-shift-select
  13647. (not (eq org-support-shift-select 'always))
  13648. (org-at-item-bullet-p))
  13649. (and (not org-support-shift-select) (org-at-item-p)))
  13650. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13651. ((and (not (eq org-support-shift-select 'always))
  13652. (org-at-property-p))
  13653. (call-interactively 'org-property-previous-allowed-value))
  13654. ((org-clocktable-try-shift 'left arg))
  13655. (org-support-shift-select
  13656. (org-call-for-shift-select 'backward-char))
  13657. (t (org-shiftselect-error))))
  13658. (defun org-shiftcontrolright ()
  13659. "Switch to next TODO set."
  13660. (interactive)
  13661. (cond
  13662. ((and org-support-shift-select (org-region-active-p))
  13663. (org-call-for-shift-select 'forward-word))
  13664. ((and (not (eq org-support-shift-select 'always))
  13665. (org-on-heading-p))
  13666. (org-call-with-arg 'org-todo 'nextset))
  13667. (org-support-shift-select
  13668. (org-call-for-shift-select 'forward-word))
  13669. (t (org-shiftselect-error))))
  13670. (defun org-shiftcontrolleft ()
  13671. "Switch to previous TODO set."
  13672. (interactive)
  13673. (cond
  13674. ((and org-support-shift-select (org-region-active-p))
  13675. (org-call-for-shift-select 'backward-word))
  13676. ((and (not (eq org-support-shift-select 'always))
  13677. (org-on-heading-p))
  13678. (org-call-with-arg 'org-todo 'previousset))
  13679. (org-support-shift-select
  13680. (org-call-for-shift-select 'backward-word))
  13681. (t (org-shiftselect-error))))
  13682. (defun org-ctrl-c-ret ()
  13683. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13684. (interactive)
  13685. (cond
  13686. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13687. (t (call-interactively 'org-insert-heading))))
  13688. (defun org-copy-special ()
  13689. "Copy region in table or copy current subtree.
  13690. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13691. See the individual commands for more information."
  13692. (interactive)
  13693. (call-interactively
  13694. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13695. (defun org-cut-special ()
  13696. "Cut region in table or cut current subtree.
  13697. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13698. See the individual commands for more information."
  13699. (interactive)
  13700. (call-interactively
  13701. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13702. (defun org-paste-special (arg)
  13703. "Paste rectangular region into table, or past subtree relative to level.
  13704. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13705. See the individual commands for more information."
  13706. (interactive "P")
  13707. (if (org-at-table-p)
  13708. (org-table-paste-rectangle)
  13709. (org-paste-subtree arg)))
  13710. (defun org-edit-special ()
  13711. "Call a special editor for the stuff at point.
  13712. When at a table, call the formula editor with `org-table-edit-formulas'.
  13713. When at the first line of an src example, call `org-edit-src-code'.
  13714. When in an #+include line, visit the include file. Otherwise call
  13715. `ffap' to visit the file at point."
  13716. (interactive)
  13717. (cond
  13718. ((org-at-table-p)
  13719. (call-interactively 'org-table-edit-formulas))
  13720. ((save-excursion
  13721. (beginning-of-line 1)
  13722. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13723. (find-file (org-trim (match-string 1))))
  13724. ((org-edit-src-code))
  13725. ((org-edit-fixed-width-region))
  13726. (t (call-interactively 'ffap))))
  13727. (defun org-ctrl-c-ctrl-c (&optional arg)
  13728. "Set tags in headline, or update according to changed information at point.
  13729. This command does many different things, depending on context:
  13730. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13731. this is what we do.
  13732. - If the cursor is on a statistics cookie, update it.
  13733. - If the cursor is in a headline, prompt for tags and insert them
  13734. into the current line, aligned to `org-tags-column'. When called
  13735. with prefix arg, realign all tags in the current buffer.
  13736. - If the cursor is in one of the special #+KEYWORD lines, this
  13737. triggers scanning the buffer for these lines and updating the
  13738. information.
  13739. - If the cursor is inside a table, realign the table. This command
  13740. works even if the automatic table editor has been turned off.
  13741. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13742. the entire table.
  13743. - If the cursor is at a footnote reference or definition, jump to
  13744. the corresponding definition or references, respectively.
  13745. - If the cursor is a the beginning of a dynamic block, update it.
  13746. - If the cursor is inside a table created by the table.el package,
  13747. activate that table.
  13748. - If the current buffer is a remember buffer, close note and file
  13749. it. A prefix argument of 1 files to the default location
  13750. without further interaction. A prefix argument of 2 files to
  13751. the currently clocking task.
  13752. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13753. links in this buffer.
  13754. - If the cursor is on a numbered item in a plain list, renumber the
  13755. ordered list.
  13756. - If the cursor is on a checkbox, toggle it."
  13757. (interactive "P")
  13758. (let ((org-enable-table-editor t))
  13759. (cond
  13760. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13761. org-occur-highlights
  13762. org-latex-fragment-image-overlays)
  13763. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13764. (org-remove-occur-highlights)
  13765. (org-remove-latex-fragment-image-overlays)
  13766. (message "Temporary highlights/overlays removed from current buffer"))
  13767. ((and (local-variable-p 'org-finish-function (current-buffer))
  13768. (fboundp org-finish-function))
  13769. (funcall org-finish-function))
  13770. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13771. ((org-at-property-p)
  13772. (call-interactively 'org-property-action))
  13773. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13774. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  13775. (or (org-on-heading-p) (org-at-item-p)))
  13776. (call-interactively 'org-update-statistics-cookies))
  13777. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13778. ((org-at-table.el-p)
  13779. (require 'table)
  13780. (beginning-of-line 1)
  13781. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13782. (call-interactively 'table-recognize-table))
  13783. ((org-at-table-p)
  13784. (org-table-maybe-eval-formula)
  13785. (if arg
  13786. (call-interactively 'org-table-recalculate)
  13787. (org-table-maybe-recalculate-line))
  13788. (call-interactively 'org-table-align))
  13789. ((or (org-footnote-at-reference-p)
  13790. (org-footnote-at-definition-p))
  13791. (call-interactively 'org-footnote-action))
  13792. ((org-at-item-checkbox-p)
  13793. (call-interactively 'org-toggle-checkbox))
  13794. ((org-at-item-p)
  13795. (if arg
  13796. (call-interactively 'org-toggle-checkbox)
  13797. (call-interactively 'org-maybe-renumber-ordered-list)))
  13798. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  13799. ;; Dynamic block
  13800. (beginning-of-line 1)
  13801. (save-excursion (org-update-dblock)))
  13802. ((save-excursion
  13803. (beginning-of-line 1)
  13804. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13805. (cond
  13806. ((equal (match-string 1) "TBLFM")
  13807. ;; Recalculate the table before this line
  13808. (save-excursion
  13809. (beginning-of-line 1)
  13810. (skip-chars-backward " \r\n\t")
  13811. (if (org-at-table-p)
  13812. (org-call-with-arg 'org-table-recalculate t))))
  13813. (t
  13814. ; (org-set-regexps-and-options)
  13815. ; (org-restart-font-lock)
  13816. (let ((org-inhibit-startup t)) (org-mode-restart))
  13817. (message "Local setup has been refreshed"))))
  13818. ((org-clock-update-time-maybe))
  13819. (t (error "C-c C-c can do nothing useful at this location.")))))
  13820. (defun org-mode-restart ()
  13821. "Restart Org-mode, to scan again for special lines.
  13822. Also updates the keyword regular expressions."
  13823. (interactive)
  13824. (org-mode)
  13825. (message "Org-mode restarted"))
  13826. (defun org-kill-note-or-show-branches ()
  13827. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13828. (interactive)
  13829. (if (not org-finish-function)
  13830. (call-interactively 'show-branches)
  13831. (let ((org-note-abort t))
  13832. (funcall org-finish-function))))
  13833. (defun org-return (&optional indent)
  13834. "Goto next table row or insert a newline.
  13835. Calls `org-table-next-row' or `newline', depending on context.
  13836. See the individual commands for more information."
  13837. (interactive)
  13838. (cond
  13839. ((bobp) (if indent (newline-and-indent) (newline)))
  13840. ((org-at-table-p)
  13841. (org-table-justify-field-maybe)
  13842. (call-interactively 'org-table-next-row))
  13843. ((and org-return-follows-link
  13844. (eq (get-text-property (point) 'face) 'org-link))
  13845. (call-interactively 'org-open-at-point))
  13846. ((and (org-at-heading-p)
  13847. (looking-at
  13848. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13849. (org-show-entry)
  13850. (end-of-line 1)
  13851. (newline))
  13852. (t (if indent (newline-and-indent) (newline)))))
  13853. (defun org-return-indent ()
  13854. "Goto next table row or insert a newline and indent.
  13855. Calls `org-table-next-row' or `newline-and-indent', depending on
  13856. context. See the individual commands for more information."
  13857. (interactive)
  13858. (org-return t))
  13859. (defun org-ctrl-c-star ()
  13860. "Compute table, or change heading status of lines.
  13861. Calls `org-table-recalculate' or `org-toggle-heading',
  13862. depending on context."
  13863. (interactive)
  13864. (cond
  13865. ((org-at-table-p)
  13866. (call-interactively 'org-table-recalculate))
  13867. (t
  13868. ;; Convert all lines in region to list items
  13869. (call-interactively 'org-toggle-heading))))
  13870. (defun org-ctrl-c-minus ()
  13871. "Insert separator line in table or modify bullet status of line.
  13872. Also turns a plain line or a region of lines into list items.
  13873. Calls `org-table-insert-hline', `org-toggle-item', or
  13874. `org-cycle-list-bullet', depending on context."
  13875. (interactive)
  13876. (cond
  13877. ((org-at-table-p)
  13878. (call-interactively 'org-table-insert-hline))
  13879. ((org-region-active-p)
  13880. (call-interactively 'org-toggle-item))
  13881. ((org-in-item-p)
  13882. (call-interactively 'org-cycle-list-bullet))
  13883. (t
  13884. (call-interactively 'org-toggle-item))))
  13885. (defun org-toggle-item ()
  13886. "Convert headings or normal lines to items, items to normal lines.
  13887. If there is no active region, only the current line is considered.
  13888. If the first line in the region is a headline, convert all headlines to items.
  13889. If the first line in the region is an item, convert all items to normal lines.
  13890. If the first line is normal text, add an item bullet to each line."
  13891. (interactive)
  13892. (let (l2 l beg end)
  13893. (if (org-region-active-p)
  13894. (setq beg (region-beginning) end (region-end))
  13895. (setq beg (point-at-bol)
  13896. end (min (1+ (point-at-eol)) (point-max))))
  13897. (save-excursion
  13898. (goto-char end)
  13899. (setq l2 (org-current-line))
  13900. (goto-char beg)
  13901. (beginning-of-line 1)
  13902. (setq l (1- (org-current-line)))
  13903. (if (org-at-item-p)
  13904. ;; We already have items, de-itemize
  13905. (while (< (setq l (1+ l)) l2)
  13906. (when (org-at-item-p)
  13907. (goto-char (match-beginning 2))
  13908. (delete-region (match-beginning 2) (match-end 2))
  13909. (and (looking-at "[ \t]+") (replace-match "")))
  13910. (beginning-of-line 2))
  13911. (if (org-on-heading-p)
  13912. ;; Headings, convert to items
  13913. (while (< (setq l (1+ l)) l2)
  13914. (if (looking-at org-outline-regexp)
  13915. (replace-match "- " t t))
  13916. (beginning-of-line 2))
  13917. ;; normal lines, turn them into items
  13918. (while (< (setq l (1+ l)) l2)
  13919. (unless (org-at-item-p)
  13920. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13921. (replace-match "\\1- \\2")))
  13922. (beginning-of-line 2)))))))
  13923. (defun org-toggle-heading (&optional nstars)
  13924. "Convert headings to normal text, or items or text to headings.
  13925. If there is no active region, only the current line is considered.
  13926. If the first line is a heading, remove the stars from all headlines
  13927. in the region.
  13928. If the first line is a plain list item, turn all plain list items
  13929. into headings.
  13930. If the first line is a normal line, turn each and every line in the
  13931. region into a heading.
  13932. When converting a line into a heading, the number of stars is chosen
  13933. such that the lines become children of the current entry. However,
  13934. when a prefix argument is given, its value determines the number of
  13935. stars to add."
  13936. (interactive "P")
  13937. (let (l2 l itemp beg end)
  13938. (if (org-region-active-p)
  13939. (setq beg (region-beginning) end (region-end))
  13940. (setq beg (point-at-bol)
  13941. end (min (1+ (point-at-eol)) (point-max))))
  13942. (save-excursion
  13943. (goto-char end)
  13944. (setq l2 (org-current-line))
  13945. (goto-char beg)
  13946. (beginning-of-line 1)
  13947. (setq l (1- (org-current-line)))
  13948. (if (org-on-heading-p)
  13949. ;; We already have headlines, de-star them
  13950. (while (< (setq l (1+ l)) l2)
  13951. (when (org-on-heading-p t)
  13952. (and (looking-at outline-regexp) (replace-match "")))
  13953. (beginning-of-line 2))
  13954. (setq itemp (org-at-item-p))
  13955. (let* ((stars
  13956. (if nstars
  13957. (make-string (prefix-numeric-value current-prefix-arg)
  13958. ?*)
  13959. (save-excursion
  13960. (if (re-search-backward org-complex-heading-regexp nil t)
  13961. (match-string 1) ""))))
  13962. (add-stars (cond (nstars "")
  13963. ((equal stars "") "*")
  13964. (org-odd-levels-only "**")
  13965. (t "*")))
  13966. (rpl (concat stars add-stars " ")))
  13967. (while (< (setq l (1+ l)) l2)
  13968. (if itemp
  13969. (and (org-at-item-p) (replace-match rpl t t))
  13970. (unless (org-on-heading-p)
  13971. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13972. (replace-match (concat rpl (match-string 2))))))
  13973. (beginning-of-line 2)))))))
  13974. (defun org-meta-return (&optional arg)
  13975. "Insert a new heading or wrap a region in a table.
  13976. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13977. See the individual commands for more information."
  13978. (interactive "P")
  13979. (cond
  13980. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13981. ((org-at-table-p)
  13982. (call-interactively 'org-table-wrap-region))
  13983. (t (call-interactively 'org-insert-heading))))
  13984. ;;; Menu entries
  13985. ;; Define the Org-mode menus
  13986. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13987. '("Tbl"
  13988. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13989. ["Next Field" org-cycle (org-at-table-p)]
  13990. ["Previous Field" org-shifttab (org-at-table-p)]
  13991. ["Next Row" org-return (org-at-table-p)]
  13992. "--"
  13993. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13994. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13995. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13996. "--"
  13997. ("Column"
  13998. ["Move Column Left" org-metaleft (org-at-table-p)]
  13999. ["Move Column Right" org-metaright (org-at-table-p)]
  14000. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14001. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14002. ("Row"
  14003. ["Move Row Up" org-metaup (org-at-table-p)]
  14004. ["Move Row Down" org-metadown (org-at-table-p)]
  14005. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14006. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14007. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14008. "--"
  14009. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14010. ("Rectangle"
  14011. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14012. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14013. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14014. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14015. "--"
  14016. ("Calculate"
  14017. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14018. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14019. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14020. "--"
  14021. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14022. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14023. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14024. "--"
  14025. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14026. "--"
  14027. ["Sum Column/Rectangle" org-table-sum
  14028. (or (org-at-table-p) (org-region-active-p))]
  14029. ["Which Column?" org-table-current-column (org-at-table-p)])
  14030. ["Debug Formulas"
  14031. org-table-toggle-formula-debugger
  14032. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14033. ["Show Col/Row Numbers"
  14034. org-table-toggle-coordinate-overlays
  14035. :style toggle
  14036. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14037. "--"
  14038. ["Create" org-table-create (and (not (org-at-table-p))
  14039. org-enable-table-editor)]
  14040. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14041. ["Import from File" org-table-import (not (org-at-table-p))]
  14042. ["Export to File" org-table-export (org-at-table-p)]
  14043. "--"
  14044. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14045. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14046. '("Org"
  14047. ("Show/Hide"
  14048. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14049. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14050. ["Sparse Tree..." org-sparse-tree t]
  14051. ["Reveal Context" org-reveal t]
  14052. ["Show All" show-all t]
  14053. "--"
  14054. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14055. "--"
  14056. ["New Heading" org-insert-heading t]
  14057. ("Navigate Headings"
  14058. ["Up" outline-up-heading t]
  14059. ["Next" outline-next-visible-heading t]
  14060. ["Previous" outline-previous-visible-heading t]
  14061. ["Next Same Level" outline-forward-same-level t]
  14062. ["Previous Same Level" outline-backward-same-level t]
  14063. "--"
  14064. ["Jump" org-goto t])
  14065. ("Edit Structure"
  14066. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14067. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14068. "--"
  14069. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14070. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14071. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14072. "--"
  14073. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14074. "--"
  14075. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14076. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14077. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14078. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14079. "--"
  14080. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14081. "--"
  14082. ["Convert to odd levels" org-convert-to-odd-levels t]
  14083. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14084. ("Editing"
  14085. ["Emphasis..." org-emphasize t]
  14086. ["Edit Source Example" org-edit-special t]
  14087. "--"
  14088. ["Footnote new/jump" org-footnote-action t]
  14089. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14090. ("Archive"
  14091. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14092. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  14093. ; :active t :keys "C-u C-c C-x C-a"]
  14094. ["Sparse trees open ARCHIVE trees"
  14095. (setq org-sparse-tree-open-archived-trees
  14096. (not org-sparse-tree-open-archived-trees))
  14097. :style toggle :selected org-sparse-tree-open-archived-trees]
  14098. ["Cycling opens ARCHIVE trees"
  14099. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  14100. :style toggle :selected org-cycle-open-archived-trees]
  14101. "--"
  14102. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  14103. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  14104. ; ["Check and Move Children" (org-archive-subtree '(4))
  14105. ; :active t :keys "C-u C-c C-x C-s"]
  14106. )
  14107. "--"
  14108. ("Hyperlinks"
  14109. ["Store Link (Global)" org-store-link t]
  14110. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14111. ["Insert Link" org-insert-link t]
  14112. ["Follow Link" org-open-at-point t]
  14113. "--"
  14114. ["Next link" org-next-link t]
  14115. ["Previous link" org-previous-link t]
  14116. "--"
  14117. ["Descriptive Links"
  14118. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14119. :style radio
  14120. :selected (member '(org-link) buffer-invisibility-spec)]
  14121. ["Literal Links"
  14122. (progn
  14123. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14124. :style radio
  14125. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14126. "--"
  14127. ("TODO Lists"
  14128. ["TODO/DONE/-" org-todo t]
  14129. ("Select keyword"
  14130. ["Next keyword" org-shiftright (org-on-heading-p)]
  14131. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14132. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14133. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14134. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14135. ["Show TODO Tree" org-show-todo-tree t]
  14136. ["Global TODO list" org-todo-list t]
  14137. "--"
  14138. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14139. :selected org-enforce-todo-dependencies :style toggle :active t]
  14140. "Settings for tree at point"
  14141. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14142. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14143. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14144. ["Do Children parallel" org-toggle-ordered-property :style radio
  14145. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14146. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14147. "--"
  14148. ["Set Priority" org-priority t]
  14149. ["Priority Up" org-shiftup t]
  14150. ["Priority Down" org-shiftdown t]
  14151. "--"
  14152. ["Get news from all feeds" org-feed-update-all t]
  14153. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14154. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14155. ("TAGS and Properties"
  14156. ["Set Tags" org-set-tags-command t]
  14157. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14158. "--"
  14159. ["Set property" org-set-property t]
  14160. ["Column view of properties" org-columns t]
  14161. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14162. ("Dates and Scheduling"
  14163. ["Timestamp" org-time-stamp t]
  14164. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14165. ("Change Date"
  14166. ["1 Day Later" org-shiftright t]
  14167. ["1 Day Earlier" org-shiftleft t]
  14168. ["1 ... Later" org-shiftup t]
  14169. ["1 ... Earlier" org-shiftdown t])
  14170. ["Compute Time Range" org-evaluate-time-range t]
  14171. ["Schedule Item" org-schedule t]
  14172. ["Deadline" org-deadline t]
  14173. "--"
  14174. ["Custom time format" org-toggle-time-stamp-overlays
  14175. :style radio :selected org-display-custom-times]
  14176. "--"
  14177. ["Goto Calendar" org-goto-calendar t]
  14178. ["Date from Calendar" org-date-from-calendar t]
  14179. "--"
  14180. ["Start/Restart Timer" org-timer-start t]
  14181. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14182. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14183. ["Insert Timer String" org-timer t]
  14184. ["Insert Timer Item" org-timer-item t])
  14185. ("Logging work"
  14186. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14187. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14188. ["Clock out" org-clock-out t]
  14189. ["Clock cancel" org-clock-cancel t]
  14190. "--"
  14191. ["Mark as default task" org-clock-mark-default-task t]
  14192. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14193. ["Goto running clock" org-clock-goto t]
  14194. "--"
  14195. ["Display times" org-clock-display t]
  14196. ["Create clock table" org-clock-report t]
  14197. "--"
  14198. ["Record DONE time"
  14199. (progn (setq org-log-done (not org-log-done))
  14200. (message "Switching to %s will %s record a timestamp"
  14201. (car org-done-keywords)
  14202. (if org-log-done "automatically" "not")))
  14203. :style toggle :selected org-log-done])
  14204. "--"
  14205. ["Agenda Command..." org-agenda t]
  14206. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14207. ("File List for Agenda")
  14208. ("Special views current file"
  14209. ["TODO Tree" org-show-todo-tree t]
  14210. ["Check Deadlines" org-check-deadlines t]
  14211. ["Timeline" org-timeline t]
  14212. ["Tags/Property tree" org-match-sparse-tree t])
  14213. "--"
  14214. ["Export/Publish..." org-export t]
  14215. ("LaTeX"
  14216. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14217. :selected org-cdlatex-mode]
  14218. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14219. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14220. ["Modify math symbol" org-cdlatex-math-modify
  14221. (org-inside-LaTeX-fragment-p)]
  14222. ["Insert citation" org-reftex-citation t]
  14223. "--"
  14224. ["Export LaTeX fragments as images"
  14225. (if (featurep 'org-exp)
  14226. (setq org-export-with-LaTeX-fragments
  14227. (not org-export-with-LaTeX-fragments))
  14228. (require 'org-exp))
  14229. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14230. org-export-with-LaTeX-fragments)])
  14231. "--"
  14232. ("MobileOrg"
  14233. ["Push Files and Views" org-mobile-push t]
  14234. ["Get Captured and Flagged" org-mobile-pull t]
  14235. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  14236. "--"
  14237. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  14238. "--"
  14239. ("Documentation"
  14240. ["Show Version" org-version t]
  14241. ["Info Documentation" org-info t])
  14242. ("Customize"
  14243. ["Browse Org Group" org-customize t]
  14244. "--"
  14245. ["Expand This Menu" org-create-customize-menu
  14246. (fboundp 'customize-menu-create)])
  14247. ["Send bug report" org-submit-bug-report t]
  14248. "--"
  14249. ("Refresh/Reload"
  14250. ["Refresh setup current buffer" org-mode-restart t]
  14251. ["Reload Org (after update)" org-reload t]
  14252. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14253. ))
  14254. (defun org-info (&optional node)
  14255. "Read documentation for Org-mode in the info system.
  14256. With optional NODE, go directly to that node."
  14257. (interactive)
  14258. (info (format "(org)%s" (or node ""))))
  14259. ;;;###autoload
  14260. (defun org-submit-bug-report ()
  14261. "Submit a bug report on Org-mode via mail.
  14262. Don't hesitate to report any problems or inaccurate documentation.
  14263. If you don't have setup sending mail from (X)Emacs, please copy the
  14264. output buffer into your mail program, as it gives us important
  14265. information about your Org-mode version and configuration."
  14266. (interactive)
  14267. (require 'reporter)
  14268. (org-load-modules-maybe)
  14269. (org-require-autoloaded-modules)
  14270. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14271. (reporter-submit-bug-report
  14272. "emacs-orgmode@gnu.org"
  14273. (org-version)
  14274. (let (list)
  14275. (save-window-excursion
  14276. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14277. (delete-other-windows)
  14278. (erase-buffer)
  14279. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14280. We would like to add your full Org-mode and Outline configuration to the
  14281. bug report. This greatly simplifies the work of the maintainer and
  14282. other experts on the mailing list.
  14283. HOWEVER, some variables you have customized may contain private
  14284. information. The names of customers, colleagues, or friends, might
  14285. appear in the form of file names, tags, todo states, or search strings.
  14286. If you answer yes to the prompt, you might want to check and remove
  14287. such private information before sending the email.")
  14288. (add-text-properties (point-min) (point-max) '(face org-warning))
  14289. (when (yes-or-no-p "Include your Org-mode configuration ")
  14290. (mapatoms
  14291. (lambda (v)
  14292. (and (boundp v)
  14293. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14294. (or (and (symbol-value v)
  14295. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14296. (and
  14297. (get v 'custom-type) (get v 'standard-value)
  14298. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14299. (push v list)))))
  14300. (kill-buffer (get-buffer "*Warn about privacy*"))
  14301. list))
  14302. nil nil
  14303. "Remember to cover the basics, that is, what you expected to happen and
  14304. what in fact did happen. You don't know how to make a good report? See
  14305. http://orgmode.org/manual/Feedback.html#Feedback
  14306. Your bug report will be posted to the Org-mode mailing list.
  14307. ------------------------------------------------------------------------")))
  14308. (defun org-install-agenda-files-menu ()
  14309. (let ((bl (buffer-list)))
  14310. (save-excursion
  14311. (while bl
  14312. (set-buffer (pop bl))
  14313. (if (org-mode-p) (setq bl nil)))
  14314. (when (org-mode-p)
  14315. (easy-menu-change
  14316. '("Org") "File List for Agenda"
  14317. (append
  14318. (list
  14319. ["Edit File List" (org-edit-agenda-file-list) t]
  14320. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14321. ["Remove Current File from List" org-remove-file t]
  14322. ["Cycle through agenda files" org-cycle-agenda-files t]
  14323. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14324. "--")
  14325. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14326. ;;;; Documentation
  14327. ;;;###autoload
  14328. (defun org-require-autoloaded-modules ()
  14329. (interactive)
  14330. (mapc 'require
  14331. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14332. org-docbook org-exp org-html org-icalendar
  14333. org-id org-latex
  14334. org-publish org-remember org-table
  14335. org-timer org-xoxo)))
  14336. ;;;###autoload
  14337. (defun org-reload (&optional uncompiled)
  14338. "Reload all org lisp files.
  14339. With prefix arg UNCOMPILED, load the uncompiled versions."
  14340. (interactive "P")
  14341. (require 'find-func)
  14342. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14343. (dir-org (file-name-directory (org-find-library-name "org")))
  14344. (dir-org-contrib (ignore-errors
  14345. (file-name-directory
  14346. (org-find-library-name "org-contribdir"))))
  14347. (files
  14348. (append (directory-files dir-org t file-re)
  14349. (and dir-org-contrib
  14350. (directory-files dir-org-contrib t file-re))))
  14351. (remove-re (concat (if (featurep 'xemacs)
  14352. "org-colview" "org-colview-xemacs")
  14353. "\\'")))
  14354. (setq files (mapcar 'file-name-sans-extension files))
  14355. (setq files (mapcar
  14356. (lambda (x) (if (string-match remove-re x) nil x))
  14357. files))
  14358. (setq files (delq nil files))
  14359. (mapc
  14360. (lambda (f)
  14361. (when (featurep (intern (file-name-nondirectory f)))
  14362. (if (and (not uncompiled)
  14363. (file-exists-p (concat f ".elc")))
  14364. (load (concat f ".elc") nil nil t)
  14365. (load (concat f ".el") nil nil t))))
  14366. files))
  14367. (org-version))
  14368. ;;;###autoload
  14369. (defun org-customize ()
  14370. "Call the customize function with org as argument."
  14371. (interactive)
  14372. (org-load-modules-maybe)
  14373. (org-require-autoloaded-modules)
  14374. (customize-browse 'org))
  14375. (defun org-create-customize-menu ()
  14376. "Create a full customization menu for Org-mode, insert it into the menu."
  14377. (interactive)
  14378. (org-load-modules-maybe)
  14379. (org-require-autoloaded-modules)
  14380. (if (fboundp 'customize-menu-create)
  14381. (progn
  14382. (easy-menu-change
  14383. '("Org") "Customize"
  14384. `(["Browse Org group" org-customize t]
  14385. "--"
  14386. ,(customize-menu-create 'org)
  14387. ["Set" Custom-set t]
  14388. ["Save" Custom-save t]
  14389. ["Reset to Current" Custom-reset-current t]
  14390. ["Reset to Saved" Custom-reset-saved t]
  14391. ["Reset to Standard Settings" Custom-reset-standard t]))
  14392. (message "\"Org\"-menu now contains full customization menu"))
  14393. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14394. ;;;; Miscellaneous stuff
  14395. ;;; Generally useful functions
  14396. (defun org-find-text-property-in-string (prop s)
  14397. "Return the first non-nil value of property PROP in string S."
  14398. (or (get-text-property 0 prop s)
  14399. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14400. prop s)))
  14401. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14402. "Display the given MESSAGE as a warning."
  14403. (if (fboundp 'display-warning)
  14404. (display-warning 'org message
  14405. (if (featurep 'xemacs)
  14406. 'warning
  14407. :warning))
  14408. (let ((buf (get-buffer-create "*Org warnings*")))
  14409. (with-current-buffer buf
  14410. (goto-char (point-max))
  14411. (insert "Warning (Org): " message)
  14412. (unless (bolp)
  14413. (newline)))
  14414. (display-buffer buf)
  14415. (sit-for 0))))
  14416. (defun org-in-commented-line ()
  14417. "Is point in a line starting with `#'?"
  14418. (equal (char-after (point-at-bol)) ?#))
  14419. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14420. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14421. (if (and marker (marker-buffer marker)
  14422. (buffer-live-p (marker-buffer marker)))
  14423. (progn
  14424. (switch-to-buffer (marker-buffer marker))
  14425. (if (or (> marker (point-max)) (< marker (point-min)))
  14426. (widen))
  14427. (goto-char marker)
  14428. (org-show-context 'org-goto))
  14429. (if bookmark
  14430. (bookmark-jump bookmark)
  14431. (error "Cannot find location"))))
  14432. (defun org-quote-csv-field (s)
  14433. "Quote field for inclusion in CSV material."
  14434. (if (string-match "[\",]" s)
  14435. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14436. s))
  14437. (defun org-plist-delete (plist property)
  14438. "Delete PROPERTY from PLIST.
  14439. This is in contrast to merely setting it to 0."
  14440. (let (p)
  14441. (while plist
  14442. (if (not (eq property (car plist)))
  14443. (setq p (plist-put p (car plist) (nth 1 plist))))
  14444. (setq plist (cddr plist)))
  14445. p))
  14446. (defun org-force-self-insert (N)
  14447. "Needed to enforce self-insert under remapping."
  14448. (interactive "p")
  14449. (self-insert-command N))
  14450. (defun org-string-width (s)
  14451. "Compute width of string, ignoring invisible characters.
  14452. This ignores character with invisibility property `org-link', and also
  14453. characters with property `org-cwidth', because these will become invisible
  14454. upon the next fontification round."
  14455. (let (b l)
  14456. (when (or (eq t buffer-invisibility-spec)
  14457. (assq 'org-link buffer-invisibility-spec))
  14458. (while (setq b (text-property-any 0 (length s)
  14459. 'invisible 'org-link s))
  14460. (setq s (concat (substring s 0 b)
  14461. (substring s (or (next-single-property-change
  14462. b 'invisible s) (length s)))))))
  14463. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14464. (setq s (concat (substring s 0 b)
  14465. (substring s (or (next-single-property-change
  14466. b 'org-cwidth s) (length s))))))
  14467. (setq l (string-width s) b -1)
  14468. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14469. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14470. l))
  14471. (defun org-get-indentation (&optional line)
  14472. "Get the indentation of the current line, interpreting tabs.
  14473. When LINE is given, assume it represents a line and compute its indentation."
  14474. (if line
  14475. (if (string-match "^ *" (org-remove-tabs line))
  14476. (match-end 0))
  14477. (save-excursion
  14478. (beginning-of-line 1)
  14479. (skip-chars-forward " \t")
  14480. (current-column))))
  14481. (defun org-remove-tabs (s &optional width)
  14482. "Replace tabulators in S with spaces.
  14483. Assumes that s is a single line, starting in column 0."
  14484. (setq width (or width tab-width))
  14485. (while (string-match "\t" s)
  14486. (setq s (replace-match
  14487. (make-string
  14488. (- (* width (/ (+ (match-beginning 0) width) width))
  14489. (match-beginning 0)) ?\ )
  14490. t t s)))
  14491. s)
  14492. (defun org-fix-indentation (line ind)
  14493. "Fix indentation in LINE.
  14494. IND is a cons cell with target and minimum indentation.
  14495. If the current indentation in LINE is smaller than the minimum,
  14496. leave it alone. If it is larger than ind, set it to the target."
  14497. (let* ((l (org-remove-tabs line))
  14498. (i (org-get-indentation l))
  14499. (i1 (car ind)) (i2 (cdr ind)))
  14500. (if (>= i i2) (setq l (substring line i2)))
  14501. (if (> i1 0)
  14502. (concat (make-string i1 ?\ ) l)
  14503. l)))
  14504. (defun org-remove-indentation (code &optional n)
  14505. "Remove the maximum common indentation from the lines in CODE.
  14506. N may optionally be the number of spaces to remove."
  14507. (with-temp-buffer
  14508. (insert code)
  14509. (org-do-remove-indentation n)
  14510. (buffer-string)))
  14511. (defun org-do-remove-indentation (&optional n)
  14512. "Remove the maximum common indentation from the buffer."
  14513. (untabify (point-min) (point-max))
  14514. (let ((min 10000) re)
  14515. (if n
  14516. (setq min n)
  14517. (goto-char (point-min))
  14518. (while (re-search-forward "^ *[^ \n]" nil t)
  14519. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14520. (unless (or (= min 0) (= min 10000))
  14521. (setq re (format "^ \\{%d\\}" min))
  14522. (goto-char (point-min))
  14523. (while (re-search-forward re nil t)
  14524. (replace-match "")
  14525. (end-of-line 1))
  14526. min)))
  14527. (defun org-base-buffer (buffer)
  14528. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14529. (if (not buffer)
  14530. buffer
  14531. (or (buffer-base-buffer buffer)
  14532. buffer)))
  14533. (defun org-trim (s)
  14534. "Remove whitespace at beginning and end of string."
  14535. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14536. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14537. s)
  14538. (defun org-wrap (string &optional width lines)
  14539. "Wrap string to either a number of lines, or a width in characters.
  14540. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14541. that costs. If there is a word longer than WIDTH, the text is actually
  14542. wrapped to the length of that word.
  14543. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14544. many lines, whatever width that takes.
  14545. The return value is a list of lines, without newlines at the end."
  14546. (let* ((words (org-split-string string "[ \t\n]+"))
  14547. (maxword (apply 'max (mapcar 'org-string-width words)))
  14548. w ll)
  14549. (cond (width
  14550. (org-do-wrap words (max maxword width)))
  14551. (lines
  14552. (setq w maxword)
  14553. (setq ll (org-do-wrap words maxword))
  14554. (if (<= (length ll) lines)
  14555. ll
  14556. (setq ll words)
  14557. (while (> (length ll) lines)
  14558. (setq w (1+ w))
  14559. (setq ll (org-do-wrap words w)))
  14560. ll))
  14561. (t (error "Cannot wrap this")))))
  14562. (defun org-do-wrap (words width)
  14563. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14564. (let (lines line)
  14565. (while words
  14566. (setq line (pop words))
  14567. (while (and words (< (+ (length line) (length (car words))) width))
  14568. (setq line (concat line " " (pop words))))
  14569. (setq lines (push line lines)))
  14570. (nreverse lines)))
  14571. (defun org-split-string (string &optional separators)
  14572. "Splits STRING into substrings at SEPARATORS.
  14573. No empty strings are returned if there are matches at the beginning
  14574. and end of string."
  14575. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14576. (start 0)
  14577. notfirst
  14578. (list nil))
  14579. (while (and (string-match rexp string
  14580. (if (and notfirst
  14581. (= start (match-beginning 0))
  14582. (< start (length string)))
  14583. (1+ start) start))
  14584. (< (match-beginning 0) (length string)))
  14585. (setq notfirst t)
  14586. (or (eq (match-beginning 0) 0)
  14587. (and (eq (match-beginning 0) (match-end 0))
  14588. (eq (match-beginning 0) start))
  14589. (setq list
  14590. (cons (substring string start (match-beginning 0))
  14591. list)))
  14592. (setq start (match-end 0)))
  14593. (or (eq start (length string))
  14594. (setq list
  14595. (cons (substring string start)
  14596. list)))
  14597. (nreverse list)))
  14598. (defun org-quote-vert (s)
  14599. "Replace \"|\" with \"\\vert\"."
  14600. (while (string-match "|" s)
  14601. (setq s (replace-match "\\vert" t t s)))
  14602. s)
  14603. (defun org-uuidgen-p (s)
  14604. "Is S an ID created by UUIDGEN?"
  14605. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14606. (defun org-context ()
  14607. "Return a list of contexts of the current cursor position.
  14608. If several contexts apply, all are returned.
  14609. Each context entry is a list with a symbol naming the context, and
  14610. two positions indicating start and end of the context. Possible
  14611. contexts are:
  14612. :headline anywhere in a headline
  14613. :headline-stars on the leading stars in a headline
  14614. :todo-keyword on a TODO keyword (including DONE) in a headline
  14615. :tags on the TAGS in a headline
  14616. :priority on the priority cookie in a headline
  14617. :item on the first line of a plain list item
  14618. :item-bullet on the bullet/number of a plain list item
  14619. :checkbox on the checkbox in a plain list item
  14620. :table in an org-mode table
  14621. :table-special on a special filed in a table
  14622. :table-table in a table.el table
  14623. :link on a hyperlink
  14624. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14625. :target on a <<target>>
  14626. :radio-target on a <<<radio-target>>>
  14627. :latex-fragment on a LaTeX fragment
  14628. :latex-preview on a LaTeX fragment with overlayed preview image
  14629. This function expects the position to be visible because it uses font-lock
  14630. faces as a help to recognize the following contexts: :table-special, :link,
  14631. and :keyword."
  14632. (let* ((f (get-text-property (point) 'face))
  14633. (faces (if (listp f) f (list f)))
  14634. (p (point)) clist o)
  14635. ;; First the large context
  14636. (cond
  14637. ((org-on-heading-p t)
  14638. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14639. (when (progn
  14640. (beginning-of-line 1)
  14641. (looking-at org-todo-line-tags-regexp))
  14642. (push (org-point-in-group p 1 :headline-stars) clist)
  14643. (push (org-point-in-group p 2 :todo-keyword) clist)
  14644. (push (org-point-in-group p 4 :tags) clist))
  14645. (goto-char p)
  14646. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14647. (if (looking-at "\\[#[A-Z0-9]\\]")
  14648. (push (org-point-in-group p 0 :priority) clist)))
  14649. ((org-at-item-p)
  14650. (push (org-point-in-group p 2 :item-bullet) clist)
  14651. (push (list :item (point-at-bol)
  14652. (save-excursion (org-end-of-item) (point)))
  14653. clist)
  14654. (and (org-at-item-checkbox-p)
  14655. (push (org-point-in-group p 0 :checkbox) clist)))
  14656. ((org-at-table-p)
  14657. (push (list :table (org-table-begin) (org-table-end)) clist)
  14658. (if (memq 'org-formula faces)
  14659. (push (list :table-special
  14660. (previous-single-property-change p 'face)
  14661. (next-single-property-change p 'face)) clist)))
  14662. ((org-at-table-p 'any)
  14663. (push (list :table-table) clist)))
  14664. (goto-char p)
  14665. ;; Now the small context
  14666. (cond
  14667. ((org-at-timestamp-p)
  14668. (push (org-point-in-group p 0 :timestamp) clist))
  14669. ((memq 'org-link faces)
  14670. (push (list :link
  14671. (previous-single-property-change p 'face)
  14672. (next-single-property-change p 'face)) clist))
  14673. ((memq 'org-special-keyword faces)
  14674. (push (list :keyword
  14675. (previous-single-property-change p 'face)
  14676. (next-single-property-change p 'face)) clist))
  14677. ((org-on-target-p)
  14678. (push (org-point-in-group p 0 :target) clist)
  14679. (goto-char (1- (match-beginning 0)))
  14680. (if (looking-at org-radio-target-regexp)
  14681. (push (org-point-in-group p 0 :radio-target) clist))
  14682. (goto-char p))
  14683. ((setq o (car (delq nil
  14684. (mapcar
  14685. (lambda (x)
  14686. (if (memq x org-latex-fragment-image-overlays) x))
  14687. (org-overlays-at (point))))))
  14688. (push (list :latex-fragment
  14689. (org-overlay-start o) (org-overlay-end o)) clist)
  14690. (push (list :latex-preview
  14691. (org-overlay-start o) (org-overlay-end o)) clist))
  14692. ((org-inside-LaTeX-fragment-p)
  14693. ;; FIXME: positions wrong.
  14694. (push (list :latex-fragment (point) (point)) clist)))
  14695. (setq clist (nreverse (delq nil clist)))
  14696. clist))
  14697. ;; FIXME: Compare with at-regexp-p Do we need both?
  14698. (defun org-in-regexp (re &optional nlines visually)
  14699. "Check if point is inside a match of regexp.
  14700. Normally only the current line is checked, but you can include NLINES extra
  14701. lines both before and after point into the search.
  14702. If VISUALLY is set, require that the cursor is not after the match but
  14703. really on, so that the block visually is on the match."
  14704. (catch 'exit
  14705. (let ((pos (point))
  14706. (eol (point-at-eol (+ 1 (or nlines 0))))
  14707. (inc (if visually 1 0)))
  14708. (save-excursion
  14709. (beginning-of-line (- 1 (or nlines 0)))
  14710. (while (re-search-forward re eol t)
  14711. (if (and (<= (match-beginning 0) pos)
  14712. (>= (+ inc (match-end 0)) pos))
  14713. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14714. (defun org-at-regexp-p (regexp)
  14715. "Is point inside a match of REGEXP in the current line?"
  14716. (catch 'exit
  14717. (save-excursion
  14718. (let ((pos (point)) (end (point-at-eol)))
  14719. (beginning-of-line 1)
  14720. (while (re-search-forward regexp end t)
  14721. (if (and (<= (match-beginning 0) pos)
  14722. (>= (match-end 0) pos))
  14723. (throw 'exit t)))
  14724. nil))))
  14725. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14726. "Call `multi-occur' with buffers for all agenda files."
  14727. (interactive "sOrg-files matching: \np")
  14728. (let* ((files (org-agenda-files))
  14729. (tnames (mapcar 'file-truename files))
  14730. (extra org-agenda-text-search-extra-files)
  14731. f)
  14732. (when (eq (car extra) 'agenda-archives)
  14733. (setq extra (cdr extra))
  14734. (setq files (org-add-archive-files files)))
  14735. (while (setq f (pop extra))
  14736. (unless (member (file-truename f) tnames)
  14737. (add-to-list 'files f 'append)
  14738. (add-to-list 'tnames (file-truename f) 'append)))
  14739. (multi-occur
  14740. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14741. regexp)))
  14742. (if (boundp 'occur-mode-find-occurrence-hook)
  14743. ;; Emacs 23
  14744. (add-hook 'occur-mode-find-occurrence-hook
  14745. (lambda ()
  14746. (when (org-mode-p)
  14747. (org-reveal))))
  14748. ;; Emacs 22
  14749. (defadvice occur-mode-goto-occurrence
  14750. (after org-occur-reveal activate)
  14751. (and (org-mode-p) (org-reveal)))
  14752. (defadvice occur-mode-goto-occurrence-other-window
  14753. (after org-occur-reveal activate)
  14754. (and (org-mode-p) (org-reveal)))
  14755. (defadvice occur-mode-display-occurrence
  14756. (after org-occur-reveal activate)
  14757. (when (org-mode-p)
  14758. (let ((pos (occur-mode-find-occurrence)))
  14759. (with-current-buffer (marker-buffer pos)
  14760. (save-excursion
  14761. (goto-char pos)
  14762. (org-reveal)))))))
  14763. (defun org-occur-link-in-agenda-files ()
  14764. "Create a link and search for it in the agendas.
  14765. The link is not stored in `org-stored-links', it is just created
  14766. for the search purpose."
  14767. (interactive)
  14768. (let ((link (condition-case nil
  14769. (org-store-link nil)
  14770. (error "Unable to create a link to here"))))
  14771. (org-occur-in-agenda-files (regexp-quote link))))
  14772. (defun org-uniquify (list)
  14773. "Remove duplicate elements from LIST."
  14774. (let (res)
  14775. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14776. res))
  14777. (defun org-delete-all (elts list)
  14778. "Remove all elements in ELTS from LIST."
  14779. (while elts
  14780. (setq list (delete (pop elts) list)))
  14781. list)
  14782. (defun org-back-over-empty-lines ()
  14783. "Move backwards over whitespace, to the beginning of the first empty line.
  14784. Returns the number of empty lines passed."
  14785. (let ((pos (point)))
  14786. (skip-chars-backward " \t\n\r")
  14787. (beginning-of-line 2)
  14788. (goto-char (min (point) pos))
  14789. (count-lines (point) pos)))
  14790. (defun org-skip-whitespace ()
  14791. (skip-chars-forward " \t\n\r"))
  14792. (defun org-point-in-group (point group &optional context)
  14793. "Check if POINT is in match-group GROUP.
  14794. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14795. match. If the match group does ot exist or point is not inside it,
  14796. return nil."
  14797. (and (match-beginning group)
  14798. (>= point (match-beginning group))
  14799. (<= point (match-end group))
  14800. (if context
  14801. (list context (match-beginning group) (match-end group))
  14802. t)))
  14803. (defun org-switch-to-buffer-other-window (&rest args)
  14804. "Switch to buffer in a second window on the current frame.
  14805. In particular, do not allow pop-up frames."
  14806. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14807. special-display-function)
  14808. (apply 'switch-to-buffer-other-window args)))
  14809. (defun org-combine-plists (&rest plists)
  14810. "Create a single property list from all plists in PLISTS.
  14811. The process starts by copying the first list, and then setting properties
  14812. from the other lists. Settings in the last list are the most significant
  14813. ones and overrule settings in the other lists."
  14814. (let ((rtn (copy-sequence (pop plists)))
  14815. p v ls)
  14816. (while plists
  14817. (setq ls (pop plists))
  14818. (while ls
  14819. (setq p (pop ls) v (pop ls))
  14820. (setq rtn (plist-put rtn p v))))
  14821. rtn))
  14822. (defun org-move-line-down (arg)
  14823. "Move the current line down. With prefix argument, move it past ARG lines."
  14824. (interactive "p")
  14825. (let ((col (current-column))
  14826. beg end pos)
  14827. (beginning-of-line 1) (setq beg (point))
  14828. (beginning-of-line 2) (setq end (point))
  14829. (beginning-of-line (+ 1 arg))
  14830. (setq pos (move-marker (make-marker) (point)))
  14831. (insert (delete-and-extract-region beg end))
  14832. (goto-char pos)
  14833. (org-move-to-column col)))
  14834. (defun org-move-line-up (arg)
  14835. "Move the current line up. With prefix argument, move it past ARG lines."
  14836. (interactive "p")
  14837. (let ((col (current-column))
  14838. beg end pos)
  14839. (beginning-of-line 1) (setq beg (point))
  14840. (beginning-of-line 2) (setq end (point))
  14841. (beginning-of-line (- arg))
  14842. (setq pos (move-marker (make-marker) (point)))
  14843. (insert (delete-and-extract-region beg end))
  14844. (goto-char pos)
  14845. (org-move-to-column col)))
  14846. (defun org-replace-escapes (string table)
  14847. "Replace %-escapes in STRING with values in TABLE.
  14848. TABLE is an association list with keys like \"%a\" and string values.
  14849. The sequences in STRING may contain normal field width and padding information,
  14850. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14851. so values can contain further %-escapes if they are define later in TABLE."
  14852. (let ((case-fold-search nil)
  14853. e re rpl)
  14854. (while (setq e (pop table))
  14855. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14856. (while (string-match re string)
  14857. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14858. (cdr e)))
  14859. (setq string (replace-match rpl t t string))))
  14860. string))
  14861. (defun org-sublist (list start end)
  14862. "Return a section of LIST, from START to END.
  14863. Counting starts at 1."
  14864. (let (rtn (c start))
  14865. (setq list (nthcdr (1- start) list))
  14866. (while (and list (<= c end))
  14867. (push (pop list) rtn)
  14868. (setq c (1+ c)))
  14869. (nreverse rtn)))
  14870. (defun org-find-base-buffer-visiting (file)
  14871. "Like `find-buffer-visiting' but always return the base buffer and
  14872. not an indirect buffer."
  14873. (let ((buf (or (get-file-buffer file)
  14874. (find-buffer-visiting file))))
  14875. (if buf
  14876. (or (buffer-base-buffer buf) buf)
  14877. nil)))
  14878. (defun org-image-file-name-regexp (&optional extensions)
  14879. "Return regexp matching the file names of images.
  14880. If EXTENSIONS is given, only match these."
  14881. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14882. (image-file-name-regexp)
  14883. (let ((image-file-name-extensions
  14884. (or extensions
  14885. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14886. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14887. (concat "\\."
  14888. (regexp-opt (nconc (mapcar 'upcase
  14889. image-file-name-extensions)
  14890. image-file-name-extensions)
  14891. t)
  14892. "\\'"))))
  14893. (defun org-file-image-p (file &optional extensions)
  14894. "Return non-nil if FILE is an image."
  14895. (save-match-data
  14896. (string-match (org-image-file-name-regexp extensions) file)))
  14897. (defun org-get-cursor-date ()
  14898. "Return the date at cursor in as a time.
  14899. This works in the calendar and in the agenda, anywhere else it just
  14900. returns the current time."
  14901. (let (date day defd)
  14902. (cond
  14903. ((eq major-mode 'calendar-mode)
  14904. (setq date (calendar-cursor-to-date)
  14905. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14906. ((eq major-mode 'org-agenda-mode)
  14907. (setq day (get-text-property (point) 'day))
  14908. (if day
  14909. (setq date (calendar-gregorian-from-absolute day)
  14910. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14911. (nth 2 date))))))
  14912. (or defd (current-time))))
  14913. (defvar org-agenda-action-marker (make-marker)
  14914. "Marker pointing to the entry for the next agenda action.")
  14915. (defun org-mark-entry-for-agenda-action ()
  14916. "Mark the current entry as target of an agenda action.
  14917. Agenda actions are actions executed from the agenda with the key `k',
  14918. which make use of the date at the cursor."
  14919. (interactive)
  14920. (move-marker org-agenda-action-marker
  14921. (save-excursion (org-back-to-heading t) (point))
  14922. (current-buffer))
  14923. (message
  14924. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14925. ;;; Paragraph filling stuff.
  14926. ;; We want this to be just right, so use the full arsenal.
  14927. (defun org-indent-line-function ()
  14928. "Indent line like previous, but further if previous was headline or item."
  14929. (interactive)
  14930. (let* ((pos (point))
  14931. (itemp (org-at-item-p))
  14932. (case-fold-search t)
  14933. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14934. column bpos bcol tpos tcol bullet btype bullet-type)
  14935. ;; Find the previous relevant line
  14936. (beginning-of-line 1)
  14937. (cond
  14938. ((looking-at "#") (setq column 0))
  14939. ((looking-at "\\*+ ") (setq column 0))
  14940. ((and (looking-at "[ \t]*:END:")
  14941. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14942. (save-excursion
  14943. (goto-char (1- (match-beginning 1)))
  14944. (setq column (current-column))))
  14945. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14946. (save-excursion
  14947. (re-search-backward
  14948. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14949. (setq column (org-get-indentation (match-string 0))))
  14950. (t
  14951. (beginning-of-line 0)
  14952. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14953. (not (looking-at "[ \t]*:END:"))
  14954. (not (looking-at org-drawer-regexp)))
  14955. (beginning-of-line 0))
  14956. (cond
  14957. ((looking-at "\\*+[ \t]+")
  14958. (if (not org-adapt-indentation)
  14959. (setq column 0)
  14960. (goto-char (match-end 0))
  14961. (setq column (current-column))))
  14962. ((looking-at org-drawer-regexp)
  14963. (goto-char (1- (match-beginning 1)))
  14964. (setq column (current-column)))
  14965. ((looking-at "\\([ \t]*\\):END:")
  14966. (goto-char (match-end 1))
  14967. (setq column (current-column)))
  14968. ((org-in-item-p)
  14969. (org-beginning-of-item)
  14970. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14971. (setq bpos (match-beginning 1) tpos (match-end 0)
  14972. bcol (progn (goto-char bpos) (current-column))
  14973. tcol (progn (goto-char tpos) (current-column))
  14974. bullet (match-string 1)
  14975. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14976. (if (> tcol (+ bcol org-description-max-indent))
  14977. (setq tcol (+ bcol 5)))
  14978. (if (not itemp)
  14979. (setq column tcol)
  14980. (goto-char pos)
  14981. (beginning-of-line 1)
  14982. (if (looking-at "\\S-")
  14983. (progn
  14984. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14985. (setq bullet (match-string 1)
  14986. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14987. (setq column (if (equal btype bullet-type) bcol tcol)))
  14988. (setq column (org-get-indentation)))))
  14989. (t (setq column (org-get-indentation))))))
  14990. (goto-char pos)
  14991. (if (<= (current-column) (current-indentation))
  14992. (org-indent-line-to column)
  14993. (save-excursion (org-indent-line-to column)))
  14994. (setq column (current-column))
  14995. (beginning-of-line 1)
  14996. (if (looking-at
  14997. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14998. (replace-match (concat "\\1" (format org-property-format
  14999. (match-string 2) (match-string 3)))
  15000. t nil))
  15001. (org-move-to-column column)))
  15002. (defun org-set-autofill-regexps ()
  15003. (interactive)
  15004. ;; In the paragraph separator we include headlines, because filling
  15005. ;; text in a line directly attached to a headline would otherwise
  15006. ;; fill the headline as well.
  15007. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15008. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15009. ;; The paragraph starter includes hand-formatted lists.
  15010. (org-set-local
  15011. 'paragraph-start
  15012. (concat
  15013. "\f" "\\|"
  15014. "[ ]*$" "\\|"
  15015. "\\*+ " "\\|"
  15016. "[ \t]*#" "\\|"
  15017. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15018. "[ \t]*[:|]" "\\|"
  15019. "\\$\\$" "\\|"
  15020. "\\\\\\(begin\\|end\\|[][]\\)"))
  15021. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15022. ;; But only if the user has not turned off tables or fixed-width regions
  15023. (org-set-local
  15024. 'auto-fill-inhibit-regexp
  15025. (concat "\\*+ \\|#\\+"
  15026. "\\|[ \t]*" org-keyword-time-regexp
  15027. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15028. (concat
  15029. "\\|[ \t]*["
  15030. (if org-enable-table-editor "|" "")
  15031. (if org-enable-fixed-width-editor ":" "")
  15032. "]"))))
  15033. ;; We use our own fill-paragraph function, to make sure that tables
  15034. ;; and fixed-width regions are not wrapped. That function will pass
  15035. ;; through to `fill-paragraph' when appropriate.
  15036. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15037. ; Adaptive filling: To get full control, first make sure that
  15038. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15039. (org-set-local 'adaptive-fill-regexp "\000")
  15040. (org-set-local 'adaptive-fill-function
  15041. 'org-adaptive-fill-function)
  15042. (org-set-local
  15043. 'align-mode-rules-list
  15044. '((org-in-buffer-settings
  15045. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15046. (modes . '(org-mode))))))
  15047. (defun org-fill-paragraph (&optional justify)
  15048. "Re-align a table, pass through to fill-paragraph if no table."
  15049. (let ((table-p (org-at-table-p))
  15050. (table.el-p (org-at-table.el-p)))
  15051. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15052. (save-excursion (goto-char (point-at-bol))
  15053. (looking-at outline-regexp)))
  15054. t) ; skip headlines
  15055. (table.el-p t) ; skip table.el tables
  15056. (table-p (org-table-align) t) ; align org-mode tables
  15057. (t nil)))) ; call paragraph-fill
  15058. ;; For reference, this is the default value of adaptive-fill-regexp
  15059. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15060. (defun org-adaptive-fill-function ()
  15061. "Return a fill prefix for org-mode files.
  15062. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15063. work correctly."
  15064. (cond ((looking-at "#[ \t]+")
  15065. (match-string 0))
  15066. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15067. (save-excursion
  15068. (if (> (match-end 1) (+ (match-beginning 1)
  15069. org-description-max-indent))
  15070. (goto-char (+ (match-beginning 1) 5))
  15071. (goto-char (match-end 0)))
  15072. (make-string (current-column) ?\ )))
  15073. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15074. (save-excursion
  15075. (goto-char (match-end 0))
  15076. (make-string (current-column) ?\ )))
  15077. (t nil)))
  15078. ;;; Other stuff.
  15079. (defun org-toggle-fixed-width-section (arg)
  15080. "Toggle the fixed-width export.
  15081. If there is no active region, the QUOTE keyword at the current headline is
  15082. inserted or removed. When present, it causes the text between this headline
  15083. and the next to be exported as fixed-width text, and unmodified.
  15084. If there is an active region, this command adds or removes a colon as the
  15085. first character of this line. If the first character of a line is a colon,
  15086. this line is also exported in fixed-width font."
  15087. (interactive "P")
  15088. (let* ((cc 0)
  15089. (regionp (org-region-active-p))
  15090. (beg (if regionp (region-beginning) (point)))
  15091. (end (if regionp (region-end)))
  15092. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15093. (case-fold-search nil)
  15094. (re "[ \t]*\\(: \\)")
  15095. off)
  15096. (if regionp
  15097. (save-excursion
  15098. (goto-char beg)
  15099. (setq cc (current-column))
  15100. (beginning-of-line 1)
  15101. (setq off (looking-at re))
  15102. (while (> nlines 0)
  15103. (setq nlines (1- nlines))
  15104. (beginning-of-line 1)
  15105. (cond
  15106. (arg
  15107. (org-move-to-column cc t)
  15108. (insert ": \n")
  15109. (forward-line -1))
  15110. ((and off (looking-at re))
  15111. (replace-match "" t t nil 1))
  15112. ((not off) (org-move-to-column cc t) (insert ": ")))
  15113. (forward-line 1)))
  15114. (save-excursion
  15115. (org-back-to-heading)
  15116. (if (looking-at (concat outline-regexp
  15117. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15118. (replace-match "" t t nil 1)
  15119. (if (looking-at outline-regexp)
  15120. (progn
  15121. (goto-char (match-end 0))
  15122. (insert org-quote-string " "))))))))
  15123. (defun org-reftex-citation ()
  15124. "Use reftex-citation to insert a citation into the buffer.
  15125. This looks for a line like
  15126. #+BIBLIOGRAPHY: foo plain option:-d
  15127. and derives from it that foo.bib is the bbliography file relevant
  15128. for this document. It then installs the necessary environment for RefTeX
  15129. to work in this buffer and calls `reftex-citation' to insert a citation
  15130. into the buffer.
  15131. Export of such citations to both LaTeX and HTML is handled by the contributed
  15132. package org-exp-bibtex by Taru Karttunen."
  15133. (interactive)
  15134. (let ((reftex-docstruct-symbol 'rds)
  15135. (reftex-cite-format "\\cite{%l}")
  15136. rds bib)
  15137. (save-excursion
  15138. (save-restriction
  15139. (widen)
  15140. (let ((case-fold-search t)
  15141. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15142. (if (not (save-excursion
  15143. (or (re-search-forward re nil t)
  15144. (re-search-backward re nil t))))
  15145. (error "No bibliography defined in file")
  15146. (setq bib (concat (match-string 1) ".bib")
  15147. rds (list (list 'bib bib)))))))
  15148. (call-interactively 'reftex-citation)))
  15149. ;;;; Functions extending outline functionality
  15150. (defun org-beginning-of-line (&optional arg)
  15151. "Go to the beginning of the current line. If that is invisible, continue
  15152. to a visible line beginning. This makes the function of C-a more intuitive.
  15153. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15154. first attempt, and only move to after the tags when the cursor is already
  15155. beyond the end of the headline."
  15156. (interactive "P")
  15157. (let ((pos (point))
  15158. (special (if (consp org-special-ctrl-a/e)
  15159. (car org-special-ctrl-a/e)
  15160. org-special-ctrl-a/e))
  15161. refpos)
  15162. (if (org-bound-and-true-p line-move-visual)
  15163. (beginning-of-visual-line 1)
  15164. (beginning-of-line 1))
  15165. (if (and arg (fboundp 'move-beginning-of-line))
  15166. (call-interactively 'move-beginning-of-line)
  15167. (if (bobp)
  15168. nil
  15169. (backward-char 1)
  15170. (if (org-invisible-p)
  15171. (while (and (not (bobp)) (org-invisible-p))
  15172. (backward-char 1)
  15173. (beginning-of-line 1))
  15174. (forward-char 1))))
  15175. (when special
  15176. (cond
  15177. ((and (looking-at org-complex-heading-regexp)
  15178. (= (char-after (match-end 1)) ?\ ))
  15179. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15180. (point-at-eol)))
  15181. (goto-char
  15182. (if (eq special t)
  15183. (cond ((> pos refpos) refpos)
  15184. ((= pos (point)) refpos)
  15185. (t (point)))
  15186. (cond ((> pos (point)) (point))
  15187. ((not (eq last-command this-command)) (point))
  15188. (t refpos)))))
  15189. ((org-at-item-p)
  15190. (goto-char
  15191. (if (eq special t)
  15192. (cond ((> pos (match-end 4)) (match-end 4))
  15193. ((= pos (point)) (match-end 4))
  15194. (t (point)))
  15195. (cond ((> pos (point)) (point))
  15196. ((not (eq last-command this-command)) (point))
  15197. (t (match-end 4))))))))
  15198. (org-no-warnings
  15199. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15200. (defun org-end-of-line (&optional arg)
  15201. "Go to the end of the line.
  15202. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15203. first attempt, and only move to after the tags when the cursor is already
  15204. beyond the end of the headline."
  15205. (interactive "P")
  15206. (let ((special (if (consp org-special-ctrl-a/e)
  15207. (cdr org-special-ctrl-a/e)
  15208. org-special-ctrl-a/e)))
  15209. (if (or (not special)
  15210. (not (org-on-heading-p))
  15211. arg)
  15212. (call-interactively
  15213. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15214. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15215. (t 'end-of-line)))
  15216. (let ((pos (point)))
  15217. (beginning-of-line 1)
  15218. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  15219. (if (eq special t)
  15220. (if (or (< pos (match-beginning 1))
  15221. (= pos (match-end 0)))
  15222. (goto-char (match-beginning 1))
  15223. (goto-char (match-end 0)))
  15224. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15225. (goto-char (match-end 0))
  15226. (goto-char (match-beginning 1))))
  15227. (call-interactively (if (fboundp 'move-end-of-line)
  15228. 'move-end-of-line
  15229. 'end-of-line)))))
  15230. (org-no-warnings
  15231. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15232. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15233. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15234. (defun org-backward-sentence (&optional arg)
  15235. "Go to beginning of sentence, or beginning of table field.
  15236. This will call `backward-sentence' or `org-table-beginning-of-field',
  15237. depending on context."
  15238. (interactive "P")
  15239. (cond
  15240. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15241. (t (call-interactively 'backward-sentence))))
  15242. (defun org-forward-sentence (&optional arg)
  15243. "Go to end of sentence, or end of table field.
  15244. This will call `forward-sentence' or `org-table-end-of-field',
  15245. depending on context."
  15246. (interactive "P")
  15247. (cond
  15248. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15249. (t (call-interactively 'forward-sentence))))
  15250. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15251. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15252. (defun org-kill-line (&optional arg)
  15253. "Kill line, to tags or end of line."
  15254. (interactive "P")
  15255. (cond
  15256. ((or (not org-special-ctrl-k)
  15257. (bolp)
  15258. (not (org-on-heading-p)))
  15259. (call-interactively 'kill-line))
  15260. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15261. (kill-region (point) (match-beginning 1))
  15262. (org-set-tags nil t))
  15263. (t (kill-region (point) (point-at-eol)))))
  15264. (define-key org-mode-map "\C-k" 'org-kill-line)
  15265. (defun org-yank (&optional arg)
  15266. "Yank. If the kill is a subtree, treat it specially.
  15267. This command will look at the current kill and check if is a single
  15268. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15269. cursor is at the beginning of a line or after the stars of a currently
  15270. empty headline, then the yank is handled specially. How exactly depends
  15271. on the value of the following variables, both set by default.
  15272. org-yank-folded-subtrees
  15273. When set, the subtree(s) will be folded after insertion, but only
  15274. if doing so would now swallow text after the yanked text.
  15275. org-yank-adjusted-subtrees
  15276. When set, the subtree will be promoted or demoted in order to
  15277. fit into the local outline tree structure, which means that the level
  15278. will be adjusted so that it becomes the smaller one of the two
  15279. *visible* surrounding headings.
  15280. Any prefix to this command will cause `yank' to be called directly with
  15281. no special treatment. In particular, a simple `C-u' prefix will just
  15282. plainly yank the text as it is.
  15283. \[1] The test checks if the first non-white line is a heading
  15284. and if there are no other headings with fewer stars."
  15285. (interactive "P")
  15286. (org-yank-generic 'yank arg))
  15287. (defun org-yank-generic (command arg)
  15288. "Perform some yank-like command.
  15289. This function implements the behavior described in the `org-yank'
  15290. documentation. However, it has been generalized to work for any
  15291. interactive command with similar behavior."
  15292. ;; pretend to be command COMMAND
  15293. (setq this-command command)
  15294. (if arg
  15295. (call-interactively command)
  15296. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15297. (and (org-kill-is-subtree-p)
  15298. (or (bolp)
  15299. (and (looking-at "[ \t]*$")
  15300. (string-match
  15301. "\\`\\*+\\'"
  15302. (buffer-substring (point-at-bol) (point)))))))
  15303. swallowp)
  15304. (cond
  15305. ((and subtreep org-yank-folded-subtrees)
  15306. (let ((beg (point))
  15307. end)
  15308. (if (and subtreep org-yank-adjusted-subtrees)
  15309. (org-paste-subtree nil nil 'for-yank)
  15310. (call-interactively command))
  15311. (setq end (point))
  15312. (goto-char beg)
  15313. (when (and (bolp) subtreep
  15314. (not (setq swallowp
  15315. (org-yank-folding-would-swallow-text beg end))))
  15316. (or (looking-at outline-regexp)
  15317. (re-search-forward (concat "^" outline-regexp) end t))
  15318. (while (and (< (point) end) (looking-at outline-regexp))
  15319. (hide-subtree)
  15320. (org-cycle-show-empty-lines 'folded)
  15321. (condition-case nil
  15322. (outline-forward-same-level 1)
  15323. (error (goto-char end)))))
  15324. (when swallowp
  15325. (message
  15326. "Inserted text not folded because that would swallow text"))
  15327. (goto-char end)
  15328. (skip-chars-forward " \t\n\r")
  15329. (beginning-of-line 1)
  15330. (push-mark beg 'nomsg)))
  15331. ((and subtreep org-yank-adjusted-subtrees)
  15332. (let ((beg (point-at-bol)))
  15333. (org-paste-subtree nil nil 'for-yank)
  15334. (push-mark beg 'nomsg)))
  15335. (t
  15336. (call-interactively command))))))
  15337. (defun org-yank-folding-would-swallow-text (beg end)
  15338. "Would hide-subtree at BEG swallow any text after END?"
  15339. (let (level)
  15340. (save-excursion
  15341. (goto-char beg)
  15342. (when (or (looking-at outline-regexp)
  15343. (re-search-forward (concat "^" outline-regexp) end t))
  15344. (setq level (org-outline-level)))
  15345. (goto-char end)
  15346. (skip-chars-forward " \t\r\n\v\f")
  15347. (if (or (eobp)
  15348. (and (bolp) (looking-at org-outline-regexp)
  15349. (<= (org-outline-level) level)))
  15350. nil ; Nothing would be swallowed
  15351. t)))) ; something would swallow
  15352. (define-key org-mode-map "\C-y" 'org-yank)
  15353. (defun org-invisible-p ()
  15354. "Check if point is at a character currently not visible."
  15355. ;; Early versions of noutline don't have `outline-invisible-p'.
  15356. (if (fboundp 'outline-invisible-p)
  15357. (outline-invisible-p)
  15358. (get-char-property (point) 'invisible)))
  15359. (defun org-invisible-p2 ()
  15360. "Check if point is at a character currently not visible."
  15361. (save-excursion
  15362. (if (and (eolp) (not (bobp))) (backward-char 1))
  15363. ;; Early versions of noutline don't have `outline-invisible-p'.
  15364. (if (fboundp 'outline-invisible-p)
  15365. (outline-invisible-p)
  15366. (get-char-property (point) 'invisible))))
  15367. (defun org-back-to-heading (&optional invisible-ok)
  15368. "Call `outline-back-to-heading', but provide a better error message."
  15369. (condition-case nil
  15370. (outline-back-to-heading invisible-ok)
  15371. (error (error "Before first headline at position %d in buffer %s"
  15372. (point) (current-buffer)))))
  15373. (defun org-before-first-heading-p ()
  15374. "Before first heading?"
  15375. (save-excursion
  15376. (null (re-search-backward "^\\*+ " nil t))))
  15377. (defun org-on-heading-p (&optional ignored)
  15378. (outline-on-heading-p t))
  15379. (defun org-at-heading-p (&optional ignored)
  15380. (outline-on-heading-p t))
  15381. (defun org-at-heading-or-item-p ()
  15382. (or (org-on-heading-p) (org-at-item-p)))
  15383. (defun org-on-target-p ()
  15384. (or (org-in-regexp org-radio-target-regexp)
  15385. (org-in-regexp org-target-regexp)))
  15386. (defun org-up-heading-all (arg)
  15387. "Move to the heading line of which the present line is a subheading.
  15388. This function considers both visible and invisible heading lines.
  15389. With argument, move up ARG levels."
  15390. (if (fboundp 'outline-up-heading-all)
  15391. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15392. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15393. (defun org-up-heading-safe ()
  15394. "Move to the heading line of which the present line is a subheading.
  15395. This version will not throw an error. It will return the level of the
  15396. headline found, or nil if no higher level is found.
  15397. Also, this function will be a lot faster than `outline-up-heading',
  15398. because it relies on stars being the outline starters. This can really
  15399. make a significant difference in outlines with very many siblings."
  15400. (let (start-level re)
  15401. (org-back-to-heading t)
  15402. (setq start-level (funcall outline-level))
  15403. (if (equal start-level 1)
  15404. nil
  15405. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15406. (if (re-search-backward re nil t)
  15407. (funcall outline-level)))))
  15408. (defun org-first-sibling-p ()
  15409. "Is this heading the first child of its parents?"
  15410. (interactive)
  15411. (let ((re (concat "^" outline-regexp))
  15412. level l)
  15413. (unless (org-at-heading-p t)
  15414. (error "Not at a heading"))
  15415. (setq level (funcall outline-level))
  15416. (save-excursion
  15417. (if (not (re-search-backward re nil t))
  15418. t
  15419. (setq l (funcall outline-level))
  15420. (< l level)))))
  15421. (defun org-goto-sibling (&optional previous)
  15422. "Goto the next sibling, even if it is invisible.
  15423. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15424. when a sibling was found. When none is found, return nil and don't
  15425. move point."
  15426. (let ((fun (if previous 're-search-backward 're-search-forward))
  15427. (pos (point))
  15428. (re (concat "^" outline-regexp))
  15429. level l)
  15430. (when (condition-case nil (org-back-to-heading t) (error nil))
  15431. (setq level (funcall outline-level))
  15432. (catch 'exit
  15433. (or previous (forward-char 1))
  15434. (while (funcall fun re nil t)
  15435. (setq l (funcall outline-level))
  15436. (when (< l level) (goto-char pos) (throw 'exit nil))
  15437. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15438. (goto-char pos)
  15439. nil))))
  15440. (defun org-show-siblings ()
  15441. "Show all siblings of the current headline."
  15442. (save-excursion
  15443. (while (org-goto-sibling) (org-flag-heading nil)))
  15444. (save-excursion
  15445. (while (org-goto-sibling 'previous)
  15446. (org-flag-heading nil))))
  15447. (defun org-show-hidden-entry ()
  15448. "Show an entry where even the heading is hidden."
  15449. (save-excursion
  15450. (org-show-entry)))
  15451. (defun org-flag-heading (flag &optional entry)
  15452. "Flag the current heading. FLAG non-nil means make invisible.
  15453. When ENTRY is non-nil, show the entire entry."
  15454. (save-excursion
  15455. (org-back-to-heading t)
  15456. ;; Check if we should show the entire entry
  15457. (if entry
  15458. (progn
  15459. (org-show-entry)
  15460. (save-excursion
  15461. (and (outline-next-heading)
  15462. (org-flag-heading nil))))
  15463. (outline-flag-region (max (point-min) (1- (point)))
  15464. (save-excursion (outline-end-of-heading) (point))
  15465. flag))))
  15466. (defun org-get-next-sibling ()
  15467. "Move to next heading of the same level, and return point.
  15468. If there is no such heading, return nil.
  15469. This is like outline-next-sibling, but invisible headings are ok."
  15470. (let ((level (funcall outline-level)))
  15471. (outline-next-heading)
  15472. (while (and (not (eobp)) (> (funcall outline-level) level))
  15473. (outline-next-heading))
  15474. (if (or (eobp) (< (funcall outline-level) level))
  15475. nil
  15476. (point))))
  15477. (defun org-get-last-sibling ()
  15478. "Move to previous heading of the same level, and return point.
  15479. If there is no such heading, return nil."
  15480. (let ((opoint (point))
  15481. (level (funcall outline-level)))
  15482. (outline-previous-heading)
  15483. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15484. (while (and (> (funcall outline-level) level)
  15485. (not (bobp)))
  15486. (outline-previous-heading))
  15487. (if (< (funcall outline-level) level)
  15488. nil
  15489. (point)))))
  15490. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15491. ;; This contains an exact copy of the original function, but it uses
  15492. ;; `org-back-to-heading', to make it work also in invisible
  15493. ;; trees. And is uses an invisible-OK argument.
  15494. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15495. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15496. ;; with many children and grandchildren etc, this can be much faster
  15497. ;; than the outline version.
  15498. (org-back-to-heading invisible-OK)
  15499. (let ((first t)
  15500. (level (funcall outline-level)))
  15501. (if (and (org-mode-p) (< level 1000))
  15502. ;; A true heading (not a plain list item), in Org-mode
  15503. ;; This means we can easily find the end by looking
  15504. ;; only for the right number of stars. Using a regexp to do
  15505. ;; this is so much faster than using a Lisp loop.
  15506. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15507. (forward-char 1)
  15508. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15509. ;; something else, do it the slow way
  15510. (while (and (not (eobp))
  15511. (or first (> (funcall outline-level) level)))
  15512. (setq first nil)
  15513. (outline-next-heading)))
  15514. (unless to-heading
  15515. (if (memq (preceding-char) '(?\n ?\^M))
  15516. (progn
  15517. ;; Go to end of line before heading
  15518. (forward-char -1)
  15519. (if (memq (preceding-char) '(?\n ?\^M))
  15520. ;; leave blank line before heading
  15521. (forward-char -1))))))
  15522. (point))
  15523. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15524. "Use Org version in org-mode, for dramatic speed-up."
  15525. (if (eq major-mode 'org-mode)
  15526. (progn
  15527. (org-end-of-subtree nil t)
  15528. (unless (eobp) (backward-char 1)))
  15529. ad-do-it))
  15530. (defun org-forward-same-level (arg &optional invisible-ok)
  15531. "Move forward to the arg'th subheading at same level as this one.
  15532. Stop at the first and last subheadings of a superior heading."
  15533. (interactive "p")
  15534. (org-back-to-heading invisible-ok)
  15535. (org-on-heading-p)
  15536. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15537. (re (format "^\\*\\{1,%d\\} " level))
  15538. l)
  15539. (forward-char 1)
  15540. (while (> arg 0)
  15541. (while (and (re-search-forward re nil 'move)
  15542. (setq l (- (match-end 0) (match-beginning 0) 1))
  15543. (= l level)
  15544. (not invisible-ok)
  15545. (org-invisible-p))
  15546. (if (< l level) (setq arg 1)))
  15547. (setq arg (1- arg)))
  15548. (beginning-of-line 1)))
  15549. (defun org-backward-same-level (arg &optional invisible-ok)
  15550. "Move backward to the arg'th subheading at same level as this one.
  15551. Stop at the first and last subheadings of a superior heading."
  15552. (interactive "p")
  15553. (org-back-to-heading)
  15554. (org-on-heading-p)
  15555. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15556. (re (format "^\\*\\{1,%d\\} " level))
  15557. l)
  15558. (while (> arg 0)
  15559. (while (and (re-search-backward re nil 'move)
  15560. (setq l (- (match-end 0) (match-beginning 0) 1))
  15561. (= l level)
  15562. (not invisible-ok)
  15563. (org-invisible-p))
  15564. (if (< l level) (setq arg 1)))
  15565. (setq arg (1- arg)))))
  15566. (defun org-show-subtree ()
  15567. "Show everything after this heading at deeper levels."
  15568. (outline-flag-region
  15569. (point)
  15570. (save-excursion
  15571. (org-end-of-subtree t t))
  15572. nil))
  15573. (defun org-show-entry ()
  15574. "Show the body directly following this heading.
  15575. Show the heading too, if it is currently invisible."
  15576. (interactive)
  15577. (save-excursion
  15578. (condition-case nil
  15579. (progn
  15580. (org-back-to-heading t)
  15581. (outline-flag-region
  15582. (max (point-min) (1- (point)))
  15583. (save-excursion
  15584. (if (re-search-forward
  15585. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15586. (match-beginning 1)
  15587. (point-max)))
  15588. nil)
  15589. (org-cycle-hide-drawers 'children))
  15590. (error nil))))
  15591. (defun org-make-options-regexp (kwds &optional extra)
  15592. "Make a regular expression for keyword lines."
  15593. (concat
  15594. "^"
  15595. "#?[ \t]*\\+\\("
  15596. (mapconcat 'regexp-quote kwds "\\|")
  15597. (if extra (concat "\\|" extra))
  15598. "\\):[ \t]*"
  15599. "\\(.*\\)"))
  15600. ;; Make isearch reveal the necessary context
  15601. (defun org-isearch-end ()
  15602. "Reveal context after isearch exits."
  15603. (when isearch-success ; only if search was successful
  15604. (if (featurep 'xemacs)
  15605. ;; Under XEmacs, the hook is run in the correct place,
  15606. ;; we directly show the context.
  15607. (org-show-context 'isearch)
  15608. ;; In Emacs the hook runs *before* restoring the overlays.
  15609. ;; So we have to use a one-time post-command-hook to do this.
  15610. ;; (Emacs 22 has a special variable, see function `org-mode')
  15611. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15612. isearch-mode-end-hook-quit)
  15613. ;; Only when the isearch was not quitted.
  15614. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15615. 'append 'local)))))
  15616. (defun org-isearch-post-command ()
  15617. "Remove self from hook, and show context."
  15618. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15619. (org-show-context 'isearch))
  15620. ;;;; Integration with and fixes for other packages
  15621. ;;; Imenu support
  15622. (defvar org-imenu-markers nil
  15623. "All markers currently used by Imenu.")
  15624. (make-variable-buffer-local 'org-imenu-markers)
  15625. (defun org-imenu-new-marker (&optional pos)
  15626. "Return a new marker for use by Imenu, and remember the marker."
  15627. (let ((m (make-marker)))
  15628. (move-marker m (or pos (point)))
  15629. (push m org-imenu-markers)
  15630. m))
  15631. (defun org-imenu-get-tree ()
  15632. "Produce the index for Imenu."
  15633. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15634. (setq org-imenu-markers nil)
  15635. (let* ((n org-imenu-depth)
  15636. (re (concat "^" outline-regexp))
  15637. (subs (make-vector (1+ n) nil))
  15638. (last-level 0)
  15639. m level head)
  15640. (save-excursion
  15641. (save-restriction
  15642. (widen)
  15643. (goto-char (point-max))
  15644. (while (re-search-backward re nil t)
  15645. (setq level (org-reduced-level (funcall outline-level)))
  15646. (when (<= level n)
  15647. (looking-at org-complex-heading-regexp)
  15648. (setq head (org-link-display-format
  15649. (org-match-string-no-properties 4))
  15650. m (org-imenu-new-marker))
  15651. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15652. (if (>= level last-level)
  15653. (push (cons head m) (aref subs level))
  15654. (push (cons head (aref subs (1+ level))) (aref subs level))
  15655. (loop for i from (1+ level) to n do (aset subs i nil)))
  15656. (setq last-level level)))))
  15657. (aref subs 1)))
  15658. (eval-after-load "imenu"
  15659. '(progn
  15660. (add-hook 'imenu-after-jump-hook
  15661. (lambda ()
  15662. (if (eq major-mode 'org-mode)
  15663. (org-show-context 'org-goto))))))
  15664. (defun org-link-display-format (link)
  15665. "Replace a link with either the description, or the link target
  15666. if no description is present"
  15667. (save-match-data
  15668. (if (string-match org-bracket-link-analytic-regexp link)
  15669. (replace-match (or (match-string 5 link)
  15670. (concat (match-string 1 link)
  15671. (match-string 3 link)))
  15672. nil nil link)
  15673. link)))
  15674. ;; Speedbar support
  15675. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15676. "Overlay marking the agenda restriction line in speedbar.")
  15677. (org-overlay-put org-speedbar-restriction-lock-overlay
  15678. 'face 'org-agenda-restriction-lock)
  15679. (org-overlay-put org-speedbar-restriction-lock-overlay
  15680. 'help-echo "Agendas are currently limited to this item.")
  15681. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15682. (defun org-speedbar-set-agenda-restriction ()
  15683. "Restrict future agenda commands to the location at point in speedbar.
  15684. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15685. (interactive)
  15686. (require 'org-agenda)
  15687. (let (p m tp np dir txt)
  15688. (cond
  15689. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15690. 'org-imenu t))
  15691. (setq m (get-text-property p 'org-imenu-marker))
  15692. (save-excursion
  15693. (save-restriction
  15694. (set-buffer (marker-buffer m))
  15695. (goto-char m)
  15696. (org-agenda-set-restriction-lock 'subtree))))
  15697. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15698. 'speedbar-function 'speedbar-find-file))
  15699. (setq tp (previous-single-property-change
  15700. (1+ p) 'speedbar-function)
  15701. np (next-single-property-change
  15702. tp 'speedbar-function)
  15703. dir (speedbar-line-directory)
  15704. txt (buffer-substring-no-properties (or tp (point-min))
  15705. (or np (point-max))))
  15706. (save-excursion
  15707. (save-restriction
  15708. (set-buffer (find-file-noselect
  15709. (let ((default-directory dir))
  15710. (expand-file-name txt))))
  15711. (unless (org-mode-p)
  15712. (error "Cannot restrict to non-Org-mode file"))
  15713. (org-agenda-set-restriction-lock 'file))))
  15714. (t (error "Don't know how to restrict Org-mode's agenda")))
  15715. (org-move-overlay org-speedbar-restriction-lock-overlay
  15716. (point-at-bol) (point-at-eol))
  15717. (setq current-prefix-arg nil)
  15718. (org-agenda-maybe-redo)))
  15719. (eval-after-load "speedbar"
  15720. '(progn
  15721. (speedbar-add-supported-extension ".org")
  15722. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15723. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15724. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15725. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15726. (add-hook 'speedbar-visiting-tag-hook
  15727. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15728. ;;; Fixes and Hacks for problems with other packages
  15729. ;; Make flyspell not check words in links, to not mess up our keymap
  15730. (defun org-mode-flyspell-verify ()
  15731. "Don't let flyspell put overlays at active buttons."
  15732. (and (not (get-text-property (point) 'keymap))
  15733. (not (get-text-property (point) 'org-no-flyspell))))
  15734. (defun org-remove-flyspell-overlays-in (beg end)
  15735. "Remove flyspell overlays in region."
  15736. (and (org-bound-and-true-p flyspell-mode)
  15737. (fboundp 'flyspell-delete-region-overlays)
  15738. (flyspell-delete-region-overlays beg end))
  15739. (add-text-properties beg end '(org-no-flyspell t)))
  15740. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15741. (eval-after-load "bookmark"
  15742. '(if (boundp 'bookmark-after-jump-hook)
  15743. ;; We can use the hook
  15744. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15745. ;; Hook not available, use advice
  15746. (defadvice bookmark-jump (after org-make-visible activate)
  15747. "Make the position visible."
  15748. (org-bookmark-jump-unhide))))
  15749. ;; Make sure saveplace show the location if it was hidden
  15750. (eval-after-load "saveplace"
  15751. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15752. "Make the position visible."
  15753. (org-bookmark-jump-unhide)))
  15754. (defun org-bookmark-jump-unhide ()
  15755. "Unhide the current position, to show the bookmark location."
  15756. (and (org-mode-p)
  15757. (or (org-invisible-p)
  15758. (save-excursion (goto-char (max (point-min) (1- (point))))
  15759. (org-invisible-p)))
  15760. (org-show-context 'bookmark-jump)))
  15761. ;; Make session.el ignore our circular variable
  15762. (eval-after-load "session"
  15763. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15764. ;;;; Experimental code
  15765. (defun org-closed-in-range ()
  15766. "Sparse tree of items closed in a certain time range.
  15767. Still experimental, may disappear in the future."
  15768. (interactive)
  15769. ;; Get the time interval from the user.
  15770. (let* ((time1 (org-float-time
  15771. (org-read-date nil 'to-time nil "Starting date: ")))
  15772. (time2 (org-float-time
  15773. (org-read-date nil 'to-time nil "End date:")))
  15774. ;; callback function
  15775. (callback (lambda ()
  15776. (let ((time
  15777. (org-float-time
  15778. (apply 'encode-time
  15779. (org-parse-time-string
  15780. (match-string 1))))))
  15781. ;; check if time in interval
  15782. (and (>= time time1) (<= time time2))))))
  15783. ;; make tree, check each match with the callback
  15784. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15785. ;;;; Finish up
  15786. (provide 'org)
  15787. (run-hooks 'org-load-hook)
  15788. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15789. ;;; org.el ends here