org.el 632 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956
  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.27trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.27trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  175. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  176. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  177. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  178. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  179. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  180. (const :tag "C R: Computation using the R language" org-R)
  181. (const :tag "C registry: A registry for Org links" org-registry)
  182. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  183. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  184. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  185. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  186. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  187. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  188. (defcustom org-support-shift-select nil
  189. "Non-nil means, make shift-cursor commands select text when possible.
  190. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  191. selecting a region, or enlarge thusly regions started in this way.
  192. In Org-mode, in special contexts, these same keys are used for other
  193. purposes, important enough to compete with shift selection. Org tries
  194. to balance these needs by supporting `shift-select-mode' outside these
  195. special contexts, under control of this variable.
  196. The default of this variable is nil, to avoid confusing behavior. Shifted
  197. cursor keys will then execute Org commands in the following contexts:
  198. - on a headline, changing TODO state (left/right) and priority (up/down)
  199. - on a time stamp, changing the time
  200. - in a plain list item, changing the bullet type
  201. - in a property definition line, switching between allowed values
  202. - in the BEGIN line of a clock table (changing the time block).
  203. Outside these contexts, the commands will throw an error.
  204. When this variable is t and the cursor is not in a special context,
  205. Org-mode will support shift-selection for making and enlarging regions.
  206. To make this more effective, the bullet cycling will no longer happen
  207. anywhere in an item line, but only if the cursor is exactly on the bullet.
  208. If you set this variable to the symbol `always', then the keys
  209. will not be special in headlines, property lines, and item lines, to make
  210. shift selection work there as well. If this is what you want, you can
  211. use the following alternative commands: `C-c C-t' and `C-c ,' to
  212. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  213. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  214. edited by hand or in column view.
  215. However, when the cursor is on a timestamp, shift-cursor commands
  216. will still edit the time stamp - this is just too good to give up.
  217. XEmacs user should have this variable set to nil, because shift-select-mode
  218. is Emacs 23 only."
  219. :group 'org
  220. :type '(choice
  221. (const :tag "Never" nil)
  222. (const :tag "When outside special context" t)
  223. (const :tag "Everywhere except timestamps" always)))
  224. (defgroup org-startup nil
  225. "Options concerning startup of Org-mode."
  226. :tag "Org Startup"
  227. :group 'org)
  228. (defcustom org-startup-folded t
  229. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  230. This can also be configured on a per-file basis by adding one of
  231. the following lines anywhere in the buffer:
  232. #+STARTUP: fold
  233. #+STARTUP: nofold
  234. #+STARTUP: content"
  235. :group 'org-startup
  236. :type '(choice
  237. (const :tag "nofold: show all" nil)
  238. (const :tag "fold: overview" t)
  239. (const :tag "content: all headlines" content)))
  240. (defcustom org-startup-truncated t
  241. "Non-nil means, entering Org-mode will set `truncate-lines'.
  242. This is useful since some lines containing links can be very long and
  243. uninteresting. Also tables look terrible when wrapped."
  244. :group 'org-startup
  245. :type 'boolean)
  246. (defcustom org-startup-align-all-tables nil
  247. "Non-nil means, align all tables when visiting a file.
  248. This is useful when the column width in tables is forced with <N> cookies
  249. in table fields. Such tables will look correct only after the first re-align.
  250. This can also be configured on a per-file basis by adding one of
  251. the following lines anywhere in the buffer:
  252. #+STARTUP: align
  253. #+STARTUP: noalign"
  254. :group 'org-startup
  255. :type 'boolean)
  256. (defcustom org-insert-mode-line-in-empty-file nil
  257. "Non-nil means insert the first line setting Org-mode in empty files.
  258. When the function `org-mode' is called interactively in an empty file, this
  259. normally means that the file name does not automatically trigger Org-mode.
  260. To ensure that the file will always be in Org-mode in the future, a
  261. line enforcing Org-mode will be inserted into the buffer, if this option
  262. has been set."
  263. :group 'org-startup
  264. :type 'boolean)
  265. (defcustom org-replace-disputed-keys nil
  266. "Non-nil means use alternative key bindings for some keys.
  267. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  268. These keys are also used by other packages like shift-selection-mode'
  269. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  270. If you want to use Org-mode together with one of these other modes,
  271. or more generally if you would like to move some Org-mode commands to
  272. other keys, set this variable and configure the keys with the variable
  273. `org-disputed-keys'.
  274. This option is only relevant at load-time of Org-mode, and must be set
  275. *before* org.el is loaded. Changing it requires a restart of Emacs to
  276. become effective."
  277. :group 'org-startup
  278. :type 'boolean)
  279. (defcustom org-use-extra-keys nil
  280. "Non-nil means use extra key sequence definitions for certain
  281. commands. This happens automatically if you run XEmacs or if
  282. window-system is nil. This variable lets you do the same
  283. manually. You must set it before loading org.
  284. Example: on Carbon Emacs 22 running graphically, with an external
  285. keyboard on a Powerbook, the default way of setting M-left might
  286. not work for either Alt or ESC. Setting this variable will make
  287. it work for ESC."
  288. :group 'org-startup
  289. :type 'boolean)
  290. (if (fboundp 'defvaralias)
  291. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  292. (defcustom org-disputed-keys
  293. '(([(shift up)] . [(meta p)])
  294. ([(shift down)] . [(meta n)])
  295. ([(shift left)] . [(meta -)])
  296. ([(shift right)] . [(meta +)])
  297. ([(control shift right)] . [(meta shift +)])
  298. ([(control shift left)] . [(meta shift -)]))
  299. "Keys for which Org-mode and other modes compete.
  300. This is an alist, cars are the default keys, second element specifies
  301. the alternative to use when `org-replace-disputed-keys' is t.
  302. Keys can be specified in any syntax supported by `define-key'.
  303. The value of this option takes effect only at Org-mode's startup,
  304. therefore you'll have to restart Emacs to apply it after changing."
  305. :group 'org-startup
  306. :type 'alist)
  307. (defun org-key (key)
  308. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  309. Or return the original if not disputed."
  310. (if org-replace-disputed-keys
  311. (let* ((nkey (key-description key))
  312. (x (org-find-if (lambda (x)
  313. (equal (key-description (car x)) nkey))
  314. org-disputed-keys)))
  315. (if x (cdr x) key))
  316. key))
  317. (defun org-find-if (predicate seq)
  318. (catch 'exit
  319. (while seq
  320. (if (funcall predicate (car seq))
  321. (throw 'exit (car seq))
  322. (pop seq)))))
  323. (defun org-defkey (keymap key def)
  324. "Define a key, possibly translated, as returned by `org-key'."
  325. (define-key keymap (org-key key) def))
  326. (defcustom org-ellipsis nil
  327. "The ellipsis to use in the Org-mode outline.
  328. When nil, just use the standard three dots. When a string, use that instead,
  329. When a face, use the standard 3 dots, but with the specified face.
  330. The change affects only Org-mode (which will then use its own display table).
  331. Changing this requires executing `M-x org-mode' in a buffer to become
  332. effective."
  333. :group 'org-startup
  334. :type '(choice (const :tag "Default" nil)
  335. (face :tag "Face" :value org-warning)
  336. (string :tag "String" :value "...#")))
  337. (defvar org-display-table nil
  338. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  339. (defgroup org-keywords nil
  340. "Keywords in Org-mode."
  341. :tag "Org Keywords"
  342. :group 'org)
  343. (defcustom org-deadline-string "DEADLINE:"
  344. "String to mark deadline entries.
  345. A deadline is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-deadline].
  348. Changes become only effective after restarting Emacs."
  349. :group 'org-keywords
  350. :type 'string)
  351. (defcustom org-scheduled-string "SCHEDULED:"
  352. "String to mark scheduled TODO entries.
  353. A schedule is this string, followed by a time stamp. Should be a word,
  354. terminated by a colon. You can insert a schedule keyword and
  355. a timestamp with \\[org-schedule].
  356. Changes become only effective after restarting Emacs."
  357. :group 'org-keywords
  358. :type 'string)
  359. (defcustom org-closed-string "CLOSED:"
  360. "String used as the prefix for timestamps logging closing a TODO entry."
  361. :group 'org-keywords
  362. :type 'string)
  363. (defcustom org-clock-string "CLOCK:"
  364. "String used as prefix for timestamps clocking work hours on an item."
  365. :group 'org-keywords
  366. :type 'string)
  367. (defcustom org-comment-string "COMMENT"
  368. "Entries starting with this keyword will never be exported.
  369. An entry can be toggled between COMMENT and normal with
  370. \\[org-toggle-comment].
  371. Changes become only effective after restarting Emacs."
  372. :group 'org-keywords
  373. :type 'string)
  374. (defcustom org-quote-string "QUOTE"
  375. "Entries starting with this keyword will be exported in fixed-width font.
  376. Quoting applies only to the text in the entry following the headline, and does
  377. not extend beyond the next headline, even if that is lower level.
  378. An entry can be toggled between QUOTE and normal with
  379. \\[org-toggle-fixed-width-section]."
  380. :group 'org-keywords
  381. :type 'string)
  382. (defconst org-repeat-re
  383. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  384. "Regular expression for specifying repeated events.
  385. After a match, group 1 contains the repeat expression.")
  386. (defgroup org-structure nil
  387. "Options concerning the general structure of Org-mode files."
  388. :tag "Org Structure"
  389. :group 'org)
  390. (defgroup org-reveal-location nil
  391. "Options about how to make context of a location visible."
  392. :tag "Org Reveal Location"
  393. :group 'org-structure)
  394. (defconst org-context-choice
  395. '(choice
  396. (const :tag "Always" t)
  397. (const :tag "Never" nil)
  398. (repeat :greedy t :tag "Individual contexts"
  399. (cons
  400. (choice :tag "Context"
  401. (const agenda)
  402. (const org-goto)
  403. (const occur-tree)
  404. (const tags-tree)
  405. (const link-search)
  406. (const mark-goto)
  407. (const bookmark-jump)
  408. (const isearch)
  409. (const default))
  410. (boolean))))
  411. "Contexts for the reveal options.")
  412. (defcustom org-show-hierarchy-above '((default . t))
  413. "Non-nil means, show full hierarchy when revealing a location.
  414. Org-mode often shows locations in an org-mode file which might have
  415. been invisible before. When this is set, the hierarchy of headings
  416. above the exposed location is shown.
  417. Turning this off for example for sparse trees makes them very compact.
  418. Instead of t, this can also be an alist specifying this option for different
  419. contexts. Valid contexts are
  420. agenda when exposing an entry from the agenda
  421. org-goto when using the command `org-goto' on key C-c C-j
  422. occur-tree when using the command `org-occur' on key C-c /
  423. tags-tree when constructing a sparse tree based on tags matches
  424. link-search when exposing search matches associated with a link
  425. mark-goto when exposing the jump goal of a mark
  426. bookmark-jump when exposing a bookmark location
  427. isearch when exiting from an incremental search
  428. default default for all contexts not set explicitly"
  429. :group 'org-reveal-location
  430. :type org-context-choice)
  431. (defcustom org-show-following-heading '((default . nil))
  432. "Non-nil means, show following heading when revealing a location.
  433. Org-mode often shows locations in an org-mode file which might have
  434. been invisible before. When this is set, the heading following the
  435. match is shown.
  436. Turning this off for example for sparse trees makes them very compact,
  437. but makes it harder to edit the location of the match. In such a case,
  438. use the command \\[org-reveal] to show more context.
  439. Instead of t, this can also be an alist specifying this option for different
  440. contexts. See `org-show-hierarchy-above' for valid contexts."
  441. :group 'org-reveal-location
  442. :type org-context-choice)
  443. (defcustom org-show-siblings '((default . nil) (isearch t))
  444. "Non-nil means, show all sibling heading when revealing a location.
  445. Org-mode often shows locations in an org-mode file which might have
  446. been invisible before. When this is set, the sibling of the current entry
  447. heading are all made visible. If `org-show-hierarchy-above' is t,
  448. the same happens on each level of the hierarchy above the current entry.
  449. By default this is on for the isearch context, off for all other contexts.
  450. Turning this off for example for sparse trees makes them very compact,
  451. but makes it harder to edit the location of the match. In such a case,
  452. use the command \\[org-reveal] to show more context.
  453. Instead of t, this can also be an alist specifying this option for different
  454. contexts. See `org-show-hierarchy-above' for valid contexts."
  455. :group 'org-reveal-location
  456. :type org-context-choice)
  457. (defcustom org-show-entry-below '((default . nil))
  458. "Non-nil means, show the entry below a headline when revealing a location.
  459. Org-mode often shows locations in an org-mode file which might have
  460. been invisible before. When this is set, the text below the headline that is
  461. exposed is also shown.
  462. By default this is off for all contexts.
  463. Instead of t, this can also be an alist specifying this option for different
  464. contexts. See `org-show-hierarchy-above' for valid contexts."
  465. :group 'org-reveal-location
  466. :type org-context-choice)
  467. (defcustom org-indirect-buffer-display 'other-window
  468. "How should indirect tree buffers be displayed?
  469. This applies to indirect buffers created with the commands
  470. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  471. Valid values are:
  472. current-window Display in the current window
  473. other-window Just display in another window.
  474. dedicated-frame Create one new frame, and re-use it each time.
  475. new-frame Make a new frame each time. Note that in this case
  476. previously-made indirect buffers are kept, and you need to
  477. kill these buffers yourself."
  478. :group 'org-structure
  479. :group 'org-agenda-windows
  480. :type '(choice
  481. (const :tag "In current window" current-window)
  482. (const :tag "In current frame, other window" other-window)
  483. (const :tag "Each time a new frame" new-frame)
  484. (const :tag "One dedicated frame" dedicated-frame)))
  485. (defgroup org-cycle nil
  486. "Options concerning visibility cycling in Org-mode."
  487. :tag "Org Cycle"
  488. :group 'org-structure)
  489. (defcustom org-cycle-max-level nil
  490. "Maximum level which should still be subject to visibility cycling.
  491. Levels higher than this will, for cycling, be treated as text, not a headline.
  492. When `org-odd-levels-only' is set, a value of N in this variable actually
  493. means 2N-1 stars as the limiting headline.
  494. When nil, cycle all levels.
  495. Note that the limiting level of cycling is also influenced by
  496. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  497. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  498. than its value."
  499. :group 'org-cycle
  500. :type '(choice
  501. (const :tag "No limit" nil)
  502. (integer :tag "Maximum level")))
  503. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  504. "Names of drawers. Drawers are not opened by cycling on the headline above.
  505. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  506. this:
  507. :DRAWERNAME:
  508. .....
  509. :END:
  510. The drawer \"PROPERTIES\" is special for capturing properties through
  511. the property API.
  512. Drawers can be defined on the per-file basis with a line like:
  513. #+DRAWERS: HIDDEN STATE PROPERTIES"
  514. :group 'org-structure
  515. :group 'org-cycle
  516. :type '(repeat (string :tag "Drawer Name")))
  517. (defcustom org-hide-block-startup nil
  518. "Non-nil means, , entering Org-mode will fold all blocks.
  519. This can also be set in on a per-file basis with
  520. #+STARTUP: hideblocks
  521. #+STARTUP: showblocks"
  522. :group 'org-startup
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-global-at-bob nil
  526. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  527. This makes it possible to do global cycling without having to use S-TAB or
  528. C-u TAB. For this special case to work, the first line of the buffer
  529. must not be a headline - it may be empty or some other text. When used in
  530. this way, `org-cycle-hook' is disables temporarily, to make sure the
  531. cursor stays at the beginning of the buffer.
  532. When this option is nil, don't do anything special at the beginning
  533. of the buffer."
  534. :group 'org-cycle
  535. :type 'boolean)
  536. (defcustom org-cycle-emulate-tab t
  537. "Where should `org-cycle' emulate TAB.
  538. nil Never
  539. white Only in completely white lines
  540. whitestart Only at the beginning of lines, before the first non-white char
  541. t Everywhere except in headlines
  542. exc-hl-bol Everywhere except at the start of a headline
  543. If TAB is used in a place where it does not emulate TAB, the current subtree
  544. visibility is cycled."
  545. :group 'org-cycle
  546. :type '(choice (const :tag "Never" nil)
  547. (const :tag "Only in completely white lines" white)
  548. (const :tag "Before first char in a line" whitestart)
  549. (const :tag "Everywhere except in headlines" t)
  550. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  551. ))
  552. (defcustom org-cycle-separator-lines 2
  553. "Number of empty lines needed to keep an empty line between collapsed trees.
  554. If you leave an empty line between the end of a subtree and the following
  555. headline, this empty line is hidden when the subtree is folded.
  556. Org-mode will leave (exactly) one empty line visible if the number of
  557. empty lines is equal or larger to the number given in this variable.
  558. So the default 2 means, at least 2 empty lines after the end of a subtree
  559. are needed to produce free space between a collapsed subtree and the
  560. following headline.
  561. Special case: when 0, never leave empty lines in collapsed view."
  562. :group 'org-cycle
  563. :type 'integer)
  564. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  565. (defcustom org-pre-cycle-hook nil
  566. "Hook that is run before visibility cycling is happening.
  567. The function(s) in this hook must accept a single argument which indicates
  568. the new state that will be set right after running this hook. The
  569. argument is a symbol. Before a global state change, it can have the values
  570. `overview', `content', or `all'. Before a local state change, it can have
  571. the values `folded', `children', or `subtree'."
  572. :group 'org-cycle
  573. :type 'hook)
  574. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  575. org-cycle-hide-drawers
  576. org-cycle-show-empty-lines
  577. org-optimize-window-after-visibility-change)
  578. "Hook that is run after `org-cycle' has changed the buffer visibility.
  579. The function(s) in this hook must accept a single argument which indicates
  580. the new state that was set by the most recent `org-cycle' command. The
  581. argument is a symbol. After a global state change, it can have the values
  582. `overview', `content', or `all'. After a local state change, it can have
  583. the values `folded', `children', or `subtree'."
  584. :group 'org-cycle
  585. :type 'hook)
  586. (defgroup org-edit-structure nil
  587. "Options concerning structure editing in Org-mode."
  588. :tag "Org Edit Structure"
  589. :group 'org-structure)
  590. (defcustom org-odd-levels-only nil
  591. "Non-nil means, skip even levels and only use odd levels for the outline.
  592. This has the effect that two stars are being added/taken away in
  593. promotion/demotion commands. It also influences how levels are
  594. handled by the exporters.
  595. Changing it requires restart of `font-lock-mode' to become effective
  596. for fontification also in regions already fontified.
  597. You may also set this on a per-file basis by adding one of the following
  598. lines to the buffer:
  599. #+STARTUP: odd
  600. #+STARTUP: oddeven"
  601. :group 'org-edit-structure
  602. :group 'org-font-lock
  603. :type 'boolean)
  604. (defcustom org-adapt-indentation t
  605. "Non-nil means, adapt indentation when promoting and demoting.
  606. When this is set and the *entire* text in an entry is indented, the
  607. indentation is increased by one space in a demotion command, and
  608. decreased by one in a promotion command. If any line in the entry
  609. body starts at column 0, indentation is not changed at all.
  610. This variable also influences how property drawers and planning
  611. information is inserted. When t, these lines drawers will be inserted
  612. indented. When nil, they will not be indented."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defcustom org-special-ctrl-a/e nil
  616. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  617. When t, `C-a' will bring back the cursor to the beginning of the
  618. headline text, i.e. after the stars and after a possible TODO keyword.
  619. In an item, this will be the position after the bullet.
  620. When the cursor is already at that position, another `C-a' will bring
  621. it to the beginning of the line.
  622. `C-e' will jump to the end of the headline, ignoring the presence of tags
  623. in the headline. A second `C-e' will then jump to the true end of the
  624. line, after any tags.
  625. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  626. and only a directly following, identical keypress will bring the cursor
  627. to the special positions.
  628. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  629. set separately."
  630. :group 'org-edit-structure
  631. :type '(choice
  632. (const :tag "off" nil)
  633. (const :tag "after stars/bullet and before tags first" t)
  634. (const :tag "true line boundary first" reversed)
  635. (cons :tag "Set C-a and C-e separately"
  636. (choice :tag "Special C-a"
  637. (const :tag "off" nil)
  638. (const :tag "after stars/bullet first" t)
  639. (const :tag "before stars/bullet first" reversed))
  640. (choice :tag "Special C-e"
  641. (const :tag "off" nil)
  642. (const :tag "before tags first" t)
  643. (const :tag "after tags first" reversed)))))
  644. (if (fboundp 'defvaralias)
  645. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  646. (defcustom org-special-ctrl-k nil
  647. "Non-nil means `C-k' will behave specially in headlines.
  648. When nil, `C-k' will call the default `kill-line' command.
  649. When t, the following will happen while the cursor is in the headline:
  650. - When the cursor is at the beginning of a headline, kill the entire
  651. line and possible the folded subtree below the line.
  652. - When in the middle of the headline text, kill the headline up to the tags.
  653. - When after the headline text, kill the tags."
  654. :group 'org-edit-structure
  655. :type 'boolean)
  656. (defcustom org-yank-folded-subtrees t
  657. "Non-nil means, when yanking subtrees, fold them.
  658. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  659. it starts with a heading and all other headings in it are either children
  660. or siblings, then fold all the subtrees. However, do this only if no
  661. text after the yank would be swallowed into a folded tree by this action."
  662. :group 'org-edit-structure
  663. :type 'boolean)
  664. (defcustom org-yank-adjusted-subtrees nil
  665. "Non-nil means, when yanking subtrees, adjust the level.
  666. With this setting, `org-paste-subtree' is used to insert the subtree, see
  667. this function for details."
  668. :group 'org-edit-structure
  669. :type 'boolean)
  670. (defcustom org-M-RET-may-split-line '((default . t))
  671. "Non-nil means, M-RET will split the line at the cursor position.
  672. When nil, it will go to the end of the line before making a
  673. new line.
  674. You may also set this option in a different way for different
  675. contexts. Valid contexts are:
  676. headline when creating a new headline
  677. item when creating a new item
  678. table in a table field
  679. default the value to be used for all contexts not explicitly
  680. customized"
  681. :group 'org-structure
  682. :group 'org-table
  683. :type '(choice
  684. (const :tag "Always" t)
  685. (const :tag "Never" nil)
  686. (repeat :greedy t :tag "Individual contexts"
  687. (cons
  688. (choice :tag "Context"
  689. (const headline)
  690. (const item)
  691. (const table)
  692. (const default))
  693. (boolean)))))
  694. (defcustom org-insert-heading-respect-content nil
  695. "Non-nil means, insert new headings after the current subtree.
  696. When nil, the new heading is created directly after the current line.
  697. The commands \\[org-insert-heading-respect-content] and
  698. \\[org-insert-todo-heading-respect-content] turn this variable on
  699. for the duration of the command."
  700. :group 'org-structure
  701. :type 'boolean)
  702. (defcustom org-blank-before-new-entry '((heading . auto)
  703. (plain-list-item . auto))
  704. "Should `org-insert-heading' leave a blank line before new heading/item?
  705. The value is an alist, with `heading' and `plain-list-item' as car,
  706. and a boolean flag as cdr. For plain lists, if the variable
  707. `org-empty-line-terminates-plain-lists' is set, the setting here
  708. is ignored and no empty line is inserted, to keep the list in tact."
  709. :group 'org-edit-structure
  710. :type '(list
  711. (cons (const heading)
  712. (choice (const :tag "Never" nil)
  713. (const :tag "Always" t)
  714. (const :tag "Auto" auto)))
  715. (cons (const plain-list-item)
  716. (choice (const :tag "Never" nil)
  717. (const :tag "Always" t)
  718. (const :tag "Auto" auto)))))
  719. (defcustom org-insert-heading-hook nil
  720. "Hook being run after inserting a new heading."
  721. :group 'org-edit-structure
  722. :type 'hook)
  723. (defcustom org-enable-fixed-width-editor t
  724. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  725. This currently only means, they are never auto-wrapped.
  726. When nil, such lines will be treated like ordinary lines.
  727. See also the QUOTE keyword."
  728. :group 'org-edit-structure
  729. :type 'boolean)
  730. (defcustom org-edit-src-region-extra nil
  731. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  732. For examples see the function `org-edit-src-find-region-and-lang'.
  733. The regular expression identifying the begin marker should end with a newline,
  734. and the regexp marking the end line should start with a newline, to make sure
  735. there are kept outside the narrowed region."
  736. :group 'org-edit-structure
  737. :type '(repeat
  738. (list
  739. (regexp :tag "begin regexp")
  740. (regexp :tag "end regexp")
  741. (choice :tag "language"
  742. (string :tag "specify")
  743. (integer :tag "from match group")
  744. (const :tag "from `lang' element")
  745. (const :tag "from `style' element")))))
  746. (defcustom org-coderef-label-format "(ref:%s)"
  747. "The default coderef format.
  748. This format string will be used to search for coderef labels in literal
  749. examples (EXAMPLE and SRC blocks). The format can be overwritten
  750. an individual literal example with the -f option, like
  751. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  752. ...
  753. #+END_SRC
  754. If you want to use this for HTML export, make sure that the format does
  755. not introduce special font-locking, and avoid the HTML special
  756. characters `<', `>', and `&'. The reason for this restriction is that
  757. the labels are searched for only after htmlize has done its job."
  758. :group 'org-edit-structure ; FIXME this is not in the right group
  759. :type 'string)
  760. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  761. "The mode that should be used to edit fixed-width regions.
  762. These are the regions where each line starts with a colon."
  763. :group 'org-edit-structure
  764. :type '(choice
  765. (const artist-mode)
  766. (const picture-mode)
  767. (const fundamental-mode)
  768. (function :tag "Other (specify)")))
  769. (defcustom org-edit-src-persistent-message t
  770. "Non-nil means show persistent exit help message while editing src examples.
  771. The message is shown in the header-line, which will be created in the
  772. first line of the window showing the editing buffer.
  773. When nil, the message will only be shown intermittently in the echo area."
  774. :group 'org-edit-structure
  775. :type 'boolean)
  776. (defcustom org-goto-auto-isearch t
  777. "Non-nil means, typing characters in org-goto starts incremental search."
  778. :group 'org-edit-structure
  779. :type 'boolean)
  780. (defgroup org-sparse-trees nil
  781. "Options concerning sparse trees in Org-mode."
  782. :tag "Org Sparse Trees"
  783. :group 'org-structure)
  784. (defcustom org-highlight-sparse-tree-matches t
  785. "Non-nil means, highlight all matches that define a sparse tree.
  786. The highlights will automatically disappear the next time the buffer is
  787. changed by an edit command."
  788. :group 'org-sparse-trees
  789. :type 'boolean)
  790. (defcustom org-remove-highlights-with-change t
  791. "Non-nil means, any change to the buffer will remove temporary highlights.
  792. Such highlights are created by `org-occur' and `org-clock-display'.
  793. When nil, `C-c C-c needs to be used to get rid of the highlights.
  794. The highlights created by `org-preview-latex-fragment' always need
  795. `C-c C-c' to be removed."
  796. :group 'org-sparse-trees
  797. :group 'org-time
  798. :type 'boolean)
  799. (defcustom org-occur-hook '(org-first-headline-recenter)
  800. "Hook that is run after `org-occur' has constructed a sparse tree.
  801. This can be used to recenter the window to show as much of the structure
  802. as possible."
  803. :group 'org-sparse-trees
  804. :type 'hook)
  805. (defgroup org-imenu-and-speedbar nil
  806. "Options concerning imenu and speedbar in Org-mode."
  807. :tag "Org Imenu and Speedbar"
  808. :group 'org-structure)
  809. (defcustom org-imenu-depth 2
  810. "The maximum level for Imenu access to Org-mode headlines.
  811. This also applied for speedbar access."
  812. :group 'org-imenu-and-speedbar
  813. :type 'integer)
  814. (defgroup org-table nil
  815. "Options concerning tables in Org-mode."
  816. :tag "Org Table"
  817. :group 'org)
  818. (defcustom org-enable-table-editor 'optimized
  819. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  820. When nil, such lines will be treated like ordinary lines.
  821. When equal to the symbol `optimized', the table editor will be optimized to
  822. do the following:
  823. - Automatic overwrite mode in front of whitespace in table fields.
  824. This makes the structure of the table stay in tact as long as the edited
  825. field does not exceed the column width.
  826. - Minimize the number of realigns. Normally, the table is aligned each time
  827. TAB or RET are pressed to move to another field. With optimization this
  828. happens only if changes to a field might have changed the column width.
  829. Optimization requires replacing the functions `self-insert-command',
  830. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  831. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  832. very good at guessing when a re-align will be necessary, but you can always
  833. force one with \\[org-ctrl-c-ctrl-c].
  834. If you would like to use the optimized version in Org-mode, but the
  835. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  836. This variable can be used to turn on and off the table editor during a session,
  837. but in order to toggle optimization, a restart is required.
  838. See also the variable `org-table-auto-blank-field'."
  839. :group 'org-table
  840. :type '(choice
  841. (const :tag "off" nil)
  842. (const :tag "on" t)
  843. (const :tag "on, optimized" optimized)))
  844. (defcustom org-self-insert-cluster-for-undo t
  845. "Non-nil means cluster self-insert commands for undo when possible.
  846. If this is set, then, like in the Emacs command loop, 20 consequtive
  847. characters will be undone together.
  848. This is configurable, because there is some impact on typing performance."
  849. :group 'org-table
  850. :type 'boolean)
  851. (defcustom org-table-tab-recognizes-table.el t
  852. "Non-nil means, TAB will automatically notice a table.el table.
  853. When it sees such a table, it moves point into it and - if necessary -
  854. calls `table-recognize-table'."
  855. :group 'org-table-editing
  856. :type 'boolean)
  857. (defgroup org-link nil
  858. "Options concerning links in Org-mode."
  859. :tag "Org Link"
  860. :group 'org)
  861. (defvar org-link-abbrev-alist-local nil
  862. "Buffer-local version of `org-link-abbrev-alist', which see.
  863. The value of this is taken from the #+LINK lines.")
  864. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  865. (defcustom org-link-abbrev-alist nil
  866. "Alist of link abbreviations.
  867. The car of each element is a string, to be replaced at the start of a link.
  868. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  869. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  870. [[linkkey:tag][description]]
  871. The 'linkkey' must be a word word, starting with a letter, followed
  872. by letters, numbers, '-' or '_'.
  873. If REPLACE is a string, the tag will simply be appended to create the link.
  874. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  875. the placeholder \"%h\" will cause a url-encoded version of the tag to
  876. be inserted at that point (see the function `url-hexify-string').
  877. REPLACE may also be a function that will be called with the tag as the
  878. only argument to create the link, which should be returned as a string.
  879. See the manual for examples."
  880. :group 'org-link
  881. :type '(repeat
  882. (cons
  883. (string :tag "Protocol")
  884. (choice
  885. (string :tag "Format")
  886. (function)))))
  887. (defcustom org-descriptive-links t
  888. "Non-nil means, hide link part and only show description of bracket links.
  889. Bracket links are like [[link][description]]. This variable sets the initial
  890. state in new org-mode buffers. The setting can then be toggled on a
  891. per-buffer basis from the Org->Hyperlinks menu."
  892. :group 'org-link
  893. :type 'boolean)
  894. (defcustom org-link-file-path-type 'adaptive
  895. "How the path name in file links should be stored.
  896. Valid values are:
  897. relative Relative to the current directory, i.e. the directory of the file
  898. into which the link is being inserted.
  899. absolute Absolute path, if possible with ~ for home directory.
  900. noabbrev Absolute path, no abbreviation of home directory.
  901. adaptive Use relative path for files in the current directory and sub-
  902. directories of it. For other files, use an absolute path."
  903. :group 'org-link
  904. :type '(choice
  905. (const relative)
  906. (const absolute)
  907. (const noabbrev)
  908. (const adaptive)))
  909. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  910. "Types of links that should be activated in Org-mode files.
  911. This is a list of symbols, each leading to the activation of a certain link
  912. type. In principle, it does not hurt to turn on most link types - there may
  913. be a small gain when turning off unused link types. The types are:
  914. bracket The recommended [[link][description]] or [[link]] links with hiding.
  915. angular Links in angular brackets that may contain whitespace like
  916. <bbdb:Carsten Dominik>.
  917. plain Plain links in normal text, no whitespace, like http://google.com.
  918. radio Text that is matched by a radio target, see manual for details.
  919. tag Tag settings in a headline (link to tag search).
  920. date Time stamps (link to calendar).
  921. footnote Footnote labels.
  922. Changing this variable requires a restart of Emacs to become effective."
  923. :group 'org-link
  924. :type '(set :greedy t
  925. (const :tag "Double bracket links (new style)" bracket)
  926. (const :tag "Angular bracket links (old style)" angular)
  927. (const :tag "Plain text links" plain)
  928. (const :tag "Radio target matches" radio)
  929. (const :tag "Tags" tag)
  930. (const :tag "Timestamps" date)
  931. (const :tag "Footnotes" footnote)))
  932. (defcustom org-make-link-description-function nil
  933. "Function to use to generate link descriptions from links. If
  934. nil the link location will be used. This function must take two
  935. parameters; the first is the link and the second the description
  936. org-insert-link has generated, and should return the description
  937. to use."
  938. :group 'org-link
  939. :type 'function)
  940. (defgroup org-link-store nil
  941. "Options concerning storing links in Org-mode."
  942. :tag "Org Store Link"
  943. :group 'org-link)
  944. (defcustom org-email-link-description-format "Email %c: %.30s"
  945. "Format of the description part of a link to an email or usenet message.
  946. The following %-escapes will be replaced by corresponding information:
  947. %F full \"From\" field
  948. %f name, taken from \"From\" field, address if no name
  949. %T full \"To\" field
  950. %t first name in \"To\" field, address if no name
  951. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  952. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  953. %s subject
  954. %m message-id.
  955. You may use normal field width specification between the % and the letter.
  956. This is for example useful to limit the length of the subject.
  957. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  958. :group 'org-link-store
  959. :type 'string)
  960. (defcustom org-from-is-user-regexp
  961. (let (r1 r2)
  962. (when (and user-mail-address (not (string= user-mail-address "")))
  963. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  964. (when (and user-full-name (not (string= user-full-name "")))
  965. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  966. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  967. "Regexp matched against the \"From:\" header of an email or usenet message.
  968. It should match if the message is from the user him/herself."
  969. :group 'org-link-store
  970. :type 'regexp)
  971. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  972. "Non-nil means, storing a link to an Org file will use entry IDs.
  973. Note that before this variable is even considered, org-id must be loaded,
  974. to please customize `org-modules' and turn it on.
  975. The variable can have the following values:
  976. t Create an ID if needed to make a link to the current entry.
  977. create-if-interactive
  978. If `org-store-link' is called directly (interactively, as a user
  979. command), do create an ID to support the link. But when doing the
  980. job for remember, only use the ID if it already exists. The
  981. purpose of this setting is to avoid proliferation of unwanted
  982. IDs, just because you happen to be in an Org file when you
  983. call `org-remember' that automatically and preemptively
  984. creates a link. If you do want to get an ID link in a remember
  985. template to an entry not having an ID, create it first by
  986. explicitly creating a link to it, using `C-c C-l' first.
  987. use-existing
  988. Use existing ID, do not create one.
  989. nil Never use an ID to make a link, instead link using a text search for
  990. the headline text."
  991. :group 'org-link-store
  992. :type '(choice
  993. (const :tag "Create ID to make link" t)
  994. (const :tag "Create if storing link interactively"
  995. create-if-interactive)
  996. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  997. create-if-interactive-and-no-custom-id)
  998. (const :tag "Only use existing" use-existing)
  999. (const :tag "Do not use ID to create link" nil)))
  1000. (defcustom org-context-in-file-links t
  1001. "Non-nil means, file links from `org-store-link' contain context.
  1002. A search string will be added to the file name with :: as separator and
  1003. used to find the context when the link is activated by the command
  1004. `org-open-at-point'.
  1005. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1006. negates this setting for the duration of the command."
  1007. :group 'org-link-store
  1008. :type 'boolean)
  1009. (defcustom org-keep-stored-link-after-insertion nil
  1010. "Non-nil means, keep link in list for entire session.
  1011. The command `org-store-link' adds a link pointing to the current
  1012. location to an internal list. These links accumulate during a session.
  1013. The command `org-insert-link' can be used to insert links into any
  1014. Org-mode file (offering completion for all stored links). When this
  1015. option is nil, every link which has been inserted once using \\[org-insert-link]
  1016. will be removed from the list, to make completing the unused links
  1017. more efficient."
  1018. :group 'org-link-store
  1019. :type 'boolean)
  1020. (defgroup org-link-follow nil
  1021. "Options concerning following links in Org-mode."
  1022. :tag "Org Follow Link"
  1023. :group 'org-link)
  1024. (defcustom org-link-translation-function nil
  1025. "Function to translate links with different syntax to Org syntax.
  1026. This can be used to translate links created for example by the Planner
  1027. or emacs-wiki packages to Org syntax.
  1028. The function must accept two parameters, a TYPE containing the link
  1029. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1030. which is everything after the link protocol. It should return a cons
  1031. with possibly modified values of type and path.
  1032. Org contains a function for this, so if you set this variable to
  1033. `org-translate-link-from-planner', you should be able follow many
  1034. links created by planner."
  1035. :group 'org-link-follow
  1036. :type 'function)
  1037. (defcustom org-follow-link-hook nil
  1038. "Hook that is run after a link has been followed."
  1039. :group 'org-link-follow
  1040. :type 'hook)
  1041. (defcustom org-tab-follows-link nil
  1042. "Non-nil means, on links TAB will follow the link.
  1043. Needs to be set before org.el is loaded.
  1044. This really should not be used, it does not make sense, and the
  1045. implementation is bad."
  1046. :group 'org-link-follow
  1047. :type 'boolean)
  1048. (defcustom org-return-follows-link nil
  1049. "Non-nil means, on links RET will follow the link.
  1050. Needs to be set before org.el is loaded."
  1051. :group 'org-link-follow
  1052. :type 'boolean)
  1053. (defcustom org-mouse-1-follows-link
  1054. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1055. "Non-nil means, mouse-1 on a link will follow the link.
  1056. A longer mouse click will still set point. Does not work on XEmacs.
  1057. Needs to be set before org.el is loaded."
  1058. :group 'org-link-follow
  1059. :type 'boolean)
  1060. (defcustom org-mark-ring-length 4
  1061. "Number of different positions to be recorded in the ring
  1062. Changing this requires a restart of Emacs to work correctly."
  1063. :group 'org-link-follow
  1064. :type 'integer)
  1065. (defcustom org-link-frame-setup
  1066. '((vm . vm-visit-folder-other-frame)
  1067. (gnus . gnus-other-frame)
  1068. (file . find-file-other-window))
  1069. "Setup the frame configuration for following links.
  1070. When following a link with Emacs, it may often be useful to display
  1071. this link in another window or frame. This variable can be used to
  1072. set this up for the different types of links.
  1073. For VM, use any of
  1074. `vm-visit-folder'
  1075. `vm-visit-folder-other-frame'
  1076. For Gnus, use any of
  1077. `gnus'
  1078. `gnus-other-frame'
  1079. `org-gnus-no-new-news'
  1080. For FILE, use any of
  1081. `find-file'
  1082. `find-file-other-window'
  1083. `find-file-other-frame'
  1084. For the calendar, use the variable `calendar-setup'.
  1085. For BBDB, it is currently only possible to display the matches in
  1086. another window."
  1087. :group 'org-link-follow
  1088. :type '(list
  1089. (cons (const vm)
  1090. (choice
  1091. (const vm-visit-folder)
  1092. (const vm-visit-folder-other-window)
  1093. (const vm-visit-folder-other-frame)))
  1094. (cons (const gnus)
  1095. (choice
  1096. (const gnus)
  1097. (const gnus-other-frame)
  1098. (const org-gnus-no-new-news)))
  1099. (cons (const file)
  1100. (choice
  1101. (const find-file)
  1102. (const find-file-other-window)
  1103. (const find-file-other-frame)))))
  1104. (defcustom org-display-internal-link-with-indirect-buffer nil
  1105. "Non-nil means, use indirect buffer to display infile links.
  1106. Activating internal links (from one location in a file to another location
  1107. in the same file) normally just jumps to the location. When the link is
  1108. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1109. another window. When this option is set, the other window actually displays
  1110. an indirect buffer clone of the current buffer, to avoid any visibility
  1111. changes to the current buffer."
  1112. :group 'org-link-follow
  1113. :type 'boolean)
  1114. (defcustom org-open-non-existing-files nil
  1115. "Non-nil means, `org-open-file' will open non-existing files.
  1116. When nil, an error will be generated."
  1117. :group 'org-link-follow
  1118. :type 'boolean)
  1119. (defcustom org-open-directory-means-index-dot-org nil
  1120. "Non-nil means, a link to a directory really means to index.org.
  1121. When nil, following a directory link will run dired or open a finder/explorer
  1122. window on that directory."
  1123. :group 'org-link-follow
  1124. :type 'boolean)
  1125. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1126. "Function and arguments to call for following mailto links.
  1127. This is a list with the first element being a lisp function, and the
  1128. remaining elements being arguments to the function. In string arguments,
  1129. %a will be replaced by the address, and %s will be replaced by the subject
  1130. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1131. :group 'org-link-follow
  1132. :type '(choice
  1133. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1134. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1135. (const :tag "message-mail" (message-mail "%a" "%s"))
  1136. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1137. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1138. "Non-nil means, ask for confirmation before executing shell links.
  1139. Shell links can be dangerous: just think about a link
  1140. [[shell:rm -rf ~/*][Google Search]]
  1141. This link would show up in your Org-mode document as \"Google Search\",
  1142. but really it would remove your entire home directory.
  1143. Therefore we advise against setting this variable to nil.
  1144. Just change it to `y-or-n-p' if you want to confirm with a
  1145. single keystroke rather than having to type \"yes\"."
  1146. :group 'org-link-follow
  1147. :type '(choice
  1148. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1149. (const :tag "with y-or-n (faster)" y-or-n-p)
  1150. (const :tag "no confirmation (dangerous)" nil)))
  1151. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1152. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1153. Elisp links can be dangerous: just think about a link
  1154. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1155. This link would show up in your Org-mode document as \"Google Search\",
  1156. but really it would remove your entire home directory.
  1157. Therefore we advise against setting this variable to nil.
  1158. Just change it to `y-or-n-p' if you want to confirm with a
  1159. single keystroke rather than having to type \"yes\"."
  1160. :group 'org-link-follow
  1161. :type '(choice
  1162. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1163. (const :tag "with y-or-n (faster)" y-or-n-p)
  1164. (const :tag "no confirmation (dangerous)" nil)))
  1165. (defconst org-file-apps-defaults-gnu
  1166. '((remote . emacs)
  1167. (system . mailcap)
  1168. (t . mailcap))
  1169. "Default file applications on a UNIX or GNU/Linux system.
  1170. See `org-file-apps'.")
  1171. (defconst org-file-apps-defaults-macosx
  1172. '((remote . emacs)
  1173. (t . "open %s")
  1174. (system . "open %s")
  1175. ("ps.gz" . "gv %s")
  1176. ("eps.gz" . "gv %s")
  1177. ("dvi" . "xdvi %s")
  1178. ("fig" . "xfig %s"))
  1179. "Default file applications on a MacOS X system.
  1180. The system \"open\" is known as a default, but we use X11 applications
  1181. for some files for which the OS does not have a good default.
  1182. See `org-file-apps'.")
  1183. (defconst org-file-apps-defaults-windowsnt
  1184. (list
  1185. '(remote . emacs)
  1186. (cons t
  1187. (list (if (featurep 'xemacs)
  1188. 'mswindows-shell-execute
  1189. 'w32-shell-execute)
  1190. "open" 'file))
  1191. (cons 'system
  1192. (list (if (featurep 'xemacs)
  1193. 'mswindows-shell-execute
  1194. 'w32-shell-execute)
  1195. "open" 'file)))
  1196. "Default file applications on a Windows NT system.
  1197. The system \"open\" is used for most files.
  1198. See `org-file-apps'.")
  1199. (defcustom org-file-apps
  1200. '(
  1201. (auto-mode . emacs)
  1202. ("\\.x?html?\\'" . default)
  1203. ("\\.pdf\\'" . default)
  1204. )
  1205. "External applications for opening `file:path' items in a document.
  1206. Org-mode uses system defaults for different file types, but
  1207. you can use this variable to set the application for a given file
  1208. extension. The entries in this list are cons cells where the car identifies
  1209. files and the cdr the corresponding command. Possible values for the
  1210. file identifier are
  1211. \"regex\" Regular expression matched against the file name. For backward
  1212. compatibility, this can also be a string with only alphanumeric
  1213. characters, which is then interpreted as an extension.
  1214. `directory' Matches a directory
  1215. `remote' Matches a remote file, accessible through tramp or efs.
  1216. Remote files most likely should be visited through Emacs
  1217. because external applications cannot handle such paths.
  1218. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1219. so all files Emacs knows how to handle. Using this with
  1220. command `emacs' will open most files in Emacs. Beware that this
  1221. will also open html files inside Emacs, unless you add
  1222. (\"html\" . default) to the list as well.
  1223. t Default for files not matched by any of the other options.
  1224. `system' The system command to open files, like `open' on Windows
  1225. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1226. that will be selected if you call `C-c C-o' with a double
  1227. `C-u C-u' prefix.
  1228. Possible values for the command are:
  1229. `emacs' The file will be visited by the current Emacs process.
  1230. `default' Use the default application for this file type, which is the
  1231. association for t in the list, most likely in the system-specific
  1232. part.
  1233. This can be used to overrule an unwanted setting in the
  1234. system-specific variable.
  1235. `system' Use the system command for opening files, like \"open\".
  1236. This command is specified by the entry whose car is `system'.
  1237. Most likely, the system-specific version of this variable
  1238. does define this command, but you can overrule/replace it
  1239. here.
  1240. string A command to be executed by a shell; %s will be replaced
  1241. by the path to the file.
  1242. sexp A Lisp form which will be evaluated. The file path will
  1243. be available in the Lisp variable `file'.
  1244. For more examples, see the system specific constants
  1245. `org-file-apps-defaults-macosx'
  1246. `org-file-apps-defaults-windowsnt'
  1247. `org-file-apps-defaults-gnu'."
  1248. :group 'org-link-follow
  1249. :type '(repeat
  1250. (cons (choice :value ""
  1251. (string :tag "Extension")
  1252. (const :tag "System command to open files" system)
  1253. (const :tag "Default for unrecognized files" t)
  1254. (const :tag "Remote file" remote)
  1255. (const :tag "Links to a directory" directory)
  1256. (const :tag "Any files that have Emacs modes"
  1257. auto-mode))
  1258. (choice :value ""
  1259. (const :tag "Visit with Emacs" emacs)
  1260. (const :tag "Use default" default)
  1261. (const :tag "Use the system command" system)
  1262. (string :tag "Command")
  1263. (sexp :tag "Lisp form")))))
  1264. (defgroup org-refile nil
  1265. "Options concerning refiling entries in Org-mode."
  1266. :tag "Org Refile"
  1267. :group 'org)
  1268. (defcustom org-directory "~/org"
  1269. "Directory with org files.
  1270. This is just a default location to look for Org files. There is no need
  1271. at all to put your files into this directory. It is only used in the
  1272. following situations:
  1273. 1. When a remember template specifies a target file that is not an
  1274. absolute path. The path will then be interpreted relative to
  1275. `org-directory'
  1276. 2. When a remember note is filed away in an interactive way (when exiting the
  1277. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1278. with `org-directory' as the default path."
  1279. :group 'org-refile
  1280. :group 'org-remember
  1281. :type 'directory)
  1282. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1283. "Default target for storing notes.
  1284. Used by the hooks for remember.el. This can be a string, or nil to mean
  1285. the value of `remember-data-file'.
  1286. You can set this on a per-template basis with the variable
  1287. `org-remember-templates'."
  1288. :group 'org-refile
  1289. :group 'org-remember
  1290. :type '(choice
  1291. (const :tag "Default from remember-data-file" nil)
  1292. file))
  1293. (defcustom org-goto-interface 'outline
  1294. "The default interface to be used for `org-goto'.
  1295. Allowed values are:
  1296. outline The interface shows an outline of the relevant file
  1297. and the correct heading is found by moving through
  1298. the outline or by searching with incremental search.
  1299. outline-path-completion Headlines in the current buffer are offered via
  1300. completion. This is the interface also used by
  1301. the refile command."
  1302. :group 'org-refile
  1303. :type '(choice
  1304. (const :tag "Outline" outline)
  1305. (const :tag "Outline-path-completion" outline-path-completion)))
  1306. (defcustom org-goto-max-level 5
  1307. "Maximum level to be considered when running org-goto with refile interface."
  1308. :group 'org-refile
  1309. :type 'integer)
  1310. (defcustom org-reverse-note-order nil
  1311. "Non-nil means, store new notes at the beginning of a file or entry.
  1312. When nil, new notes will be filed to the end of a file or entry.
  1313. This can also be a list with cons cells of regular expressions that
  1314. are matched against file names, and values."
  1315. :group 'org-remember
  1316. :group 'org-refile
  1317. :type '(choice
  1318. (const :tag "Reverse always" t)
  1319. (const :tag "Reverse never" nil)
  1320. (repeat :tag "By file name regexp"
  1321. (cons regexp boolean))))
  1322. (defcustom org-refile-targets nil
  1323. "Targets for refiling entries with \\[org-refile].
  1324. This is list of cons cells. Each cell contains:
  1325. - a specification of the files to be considered, either a list of files,
  1326. or a symbol whose function or variable value will be used to retrieve
  1327. a file name or a list of file names. If you use `org-agenda-files' for
  1328. that, all agenda files will be scanned for targets. Nil means, consider
  1329. headings in the current buffer.
  1330. - A specification of how to find candidate refile targets. This may be
  1331. any of:
  1332. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1333. This tag has to be present in all target headlines, inheritance will
  1334. not be considered.
  1335. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1336. todo keyword.
  1337. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1338. headlines that are refiling targets.
  1339. - a cons cell (:level . N). Any headline of level N is considered a target.
  1340. Note that, when `org-odd-levels-only' is set, level corresponds to
  1341. order in hierarchy, not to the number of stars.
  1342. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1343. Note that, when `org-odd-levels-only' is set, level corresponds to
  1344. order in hierarchy, not to the number of stars.
  1345. You can set the variable `org-refile-target-verify-function' to a function
  1346. to verify each headline found by the simple critery above.
  1347. When this variable is nil, all top-level headlines in the current buffer
  1348. are used, equivalent to the value `((nil . (:level . 1))'."
  1349. :group 'org-refile
  1350. :type '(repeat
  1351. (cons
  1352. (choice :value org-agenda-files
  1353. (const :tag "All agenda files" org-agenda-files)
  1354. (const :tag "Current buffer" nil)
  1355. (function) (variable) (file))
  1356. (choice :tag "Identify target headline by"
  1357. (cons :tag "Specific tag" (const :value :tag) (string))
  1358. (cons :tag "TODO keyword" (const :value :todo) (string))
  1359. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1360. (cons :tag "Level number" (const :value :level) (integer))
  1361. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1362. (defcustom org-refile-target-verify-function nil
  1363. "Function to verify if the headline at point should be a refile target.
  1364. The function will be called without arguments, with point at the
  1365. beginning of the headline. It should return t and leave point
  1366. where it is if the headline is a valid target for refiling.
  1367. If the target should not be selected, the function must return nil.
  1368. In addition to this, it may move point to a place from where the search
  1369. should be continued. For example, the function may decide that the entire
  1370. subtree of the current entry should be excluded and move point to the end
  1371. of the subtree."
  1372. :group 'org-refile
  1373. :type 'function)
  1374. (defcustom org-refile-use-outline-path nil
  1375. "Non-nil means, provide refile targets as paths.
  1376. So a level 3 headline will be available as level1/level2/level3.
  1377. When the value is `file', also include the file name (without directory)
  1378. into the path. In this case, you can also stop the completion after
  1379. the file name, to get entries inserted as top level in the file.
  1380. When `full-file-path', include the full file path."
  1381. :group 'org-refile
  1382. :type '(choice
  1383. (const :tag "Not" nil)
  1384. (const :tag "Yes" t)
  1385. (const :tag "Start with file name" file)
  1386. (const :tag "Start with full file path" full-file-path)))
  1387. (defcustom org-outline-path-complete-in-steps t
  1388. "Non-nil means, complete the outline path in hierarchical steps.
  1389. When Org-mode uses the refile interface to select an outline path
  1390. \(see variable `org-refile-use-outline-path'), the completion of
  1391. the path can be done is a single go, or if can be done in steps down
  1392. the headline hierarchy. Going in steps is probably the best if you
  1393. do not use a special completion package like `ido' or `icicles'.
  1394. However, when using these packages, going in one step can be very
  1395. fast, while still showing the whole path to the entry."
  1396. :group 'org-refile
  1397. :type 'boolean)
  1398. (defcustom org-refile-allow-creating-parent-nodes nil
  1399. "Non-nil means, allow to create new nodes as refile targets.
  1400. New nodes are then created by adding \"/new node name\" to the completion
  1401. of an existing node. When the value of this variable is `confirm',
  1402. new node creation must be confirmed by the user (recommended)
  1403. When nil, the completion must match an existing entry.
  1404. Note that, if the new heading is not seen by the criteria
  1405. listed in `org-refile-targets', multiple instances of the same
  1406. heading would be created by trying again to file under the new
  1407. heading."
  1408. :group 'org-refile
  1409. :type '(choice
  1410. (const :tag "Never" nil)
  1411. (const :tag "Always" t)
  1412. (const :tag "Prompt for confirmation" confirm)))
  1413. (defgroup org-todo nil
  1414. "Options concerning TODO items in Org-mode."
  1415. :tag "Org TODO"
  1416. :group 'org)
  1417. (defgroup org-progress nil
  1418. "Options concerning Progress logging in Org-mode."
  1419. :tag "Org Progress"
  1420. :group 'org-time)
  1421. (defvar org-todo-interpretation-widgets
  1422. '(
  1423. (:tag "Sequence (cycling hits every state)" sequence)
  1424. (:tag "Type (cycling directly to DONE)" type))
  1425. "The available interpretation symbols for customizing
  1426. `org-todo-keywords'.
  1427. Interested libraries should add to this list.")
  1428. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1429. "List of TODO entry keyword sequences and their interpretation.
  1430. \\<org-mode-map>This is a list of sequences.
  1431. Each sequence starts with a symbol, either `sequence' or `type',
  1432. indicating if the keywords should be interpreted as a sequence of
  1433. action steps, or as different types of TODO items. The first
  1434. keywords are states requiring action - these states will select a headline
  1435. for inclusion into the global TODO list Org-mode produces. If one of
  1436. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1437. signify that no further action is necessary. If \"|\" is not found,
  1438. the last keyword is treated as the only DONE state of the sequence.
  1439. The command \\[org-todo] cycles an entry through these states, and one
  1440. additional state where no keyword is present. For details about this
  1441. cycling, see the manual.
  1442. TODO keywords and interpretation can also be set on a per-file basis with
  1443. the special #+SEQ_TODO and #+TYP_TODO lines.
  1444. Each keyword can optionally specify a character for fast state selection
  1445. \(in combination with the variable `org-use-fast-todo-selection')
  1446. and specifiers for state change logging, using the same syntax
  1447. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1448. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1449. indicates to record a time stamp each time this state is selected.
  1450. Each keyword may also specify if a timestamp or a note should be
  1451. recorded when entering or leaving the state, by adding additional
  1452. characters in the parenthesis after the keyword. This looks like this:
  1453. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1454. record only the time of the state change. With X and Y being either
  1455. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1456. Y when leaving the state if and only if the *target* state does not
  1457. define X. You may omit any of the fast-selection key or X or /Y,
  1458. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1459. For backward compatibility, this variable may also be just a list
  1460. of keywords - in this case the interpretation (sequence or type) will be
  1461. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1462. :group 'org-todo
  1463. :group 'org-keywords
  1464. :type '(choice
  1465. (repeat :tag "Old syntax, just keywords"
  1466. (string :tag "Keyword"))
  1467. (repeat :tag "New syntax"
  1468. (cons
  1469. (choice
  1470. :tag "Interpretation"
  1471. ;;Quick and dirty way to see
  1472. ;;`org-todo-interpretations'. This takes the
  1473. ;;place of item arguments
  1474. :convert-widget
  1475. (lambda (widget)
  1476. (widget-put widget
  1477. :args (mapcar
  1478. #'(lambda (x)
  1479. (widget-convert
  1480. (cons 'const x)))
  1481. org-todo-interpretation-widgets))
  1482. widget))
  1483. (repeat
  1484. (string :tag "Keyword"))))))
  1485. (defvar org-todo-keywords-1 nil
  1486. "All TODO and DONE keywords active in a buffer.")
  1487. (make-variable-buffer-local 'org-todo-keywords-1)
  1488. (defvar org-todo-keywords-for-agenda nil)
  1489. (defvar org-done-keywords-for-agenda nil)
  1490. (defvar org-todo-keyword-alist-for-agenda nil)
  1491. (defvar org-tag-alist-for-agenda nil)
  1492. (defvar org-agenda-contributing-files nil)
  1493. (defvar org-not-done-keywords nil)
  1494. (make-variable-buffer-local 'org-not-done-keywords)
  1495. (defvar org-done-keywords nil)
  1496. (make-variable-buffer-local 'org-done-keywords)
  1497. (defvar org-todo-heads nil)
  1498. (make-variable-buffer-local 'org-todo-heads)
  1499. (defvar org-todo-sets nil)
  1500. (make-variable-buffer-local 'org-todo-sets)
  1501. (defvar org-todo-log-states nil)
  1502. (make-variable-buffer-local 'org-todo-log-states)
  1503. (defvar org-todo-kwd-alist nil)
  1504. (make-variable-buffer-local 'org-todo-kwd-alist)
  1505. (defvar org-todo-key-alist nil)
  1506. (make-variable-buffer-local 'org-todo-key-alist)
  1507. (defvar org-todo-key-trigger nil)
  1508. (make-variable-buffer-local 'org-todo-key-trigger)
  1509. (defcustom org-todo-interpretation 'sequence
  1510. "Controls how TODO keywords are interpreted.
  1511. This variable is in principle obsolete and is only used for
  1512. backward compatibility, if the interpretation of todo keywords is
  1513. not given already in `org-todo-keywords'. See that variable for
  1514. more information."
  1515. :group 'org-todo
  1516. :group 'org-keywords
  1517. :type '(choice (const sequence)
  1518. (const type)))
  1519. (defcustom org-use-fast-todo-selection t
  1520. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1521. This variable describes if and under what circumstances the cycling
  1522. mechanism for TODO keywords will be replaced by a single-key, direct
  1523. selection scheme.
  1524. When nil, fast selection is never used.
  1525. When the symbol `prefix', it will be used when `org-todo' is called with
  1526. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1527. in an agenda buffer.
  1528. When t, fast selection is used by default. In this case, the prefix
  1529. argument forces cycling instead.
  1530. In all cases, the special interface is only used if access keys have actually
  1531. been assigned by the user, i.e. if keywords in the configuration are followed
  1532. by a letter in parenthesis, like TODO(t)."
  1533. :group 'org-todo
  1534. :type '(choice
  1535. (const :tag "Never" nil)
  1536. (const :tag "By default" t)
  1537. (const :tag "Only with C-u C-c C-t" prefix)))
  1538. (defcustom org-provide-todo-statistics t
  1539. "Non-nil means, update todo statistics after insert and toggle.
  1540. When this is set, todo statistics is updated in the parent of the current
  1541. entry each time a todo state is changed."
  1542. :group 'org-todo
  1543. :type 'boolean)
  1544. (defcustom org-hierarchical-todo-statistics t
  1545. "Non-nil means, TODO statistics covers just direct children.
  1546. When nil, all entries in the subtree are considered.
  1547. This has only an effect if `org-provide-todo-statistics' is set."
  1548. :group 'org-todo
  1549. :type 'boolean)
  1550. (defcustom org-after-todo-state-change-hook nil
  1551. "Hook which is run after the state of a TODO item was changed.
  1552. The new state (a string with a TODO keyword, or nil) is available in the
  1553. Lisp variable `state'."
  1554. :group 'org-todo
  1555. :type 'hook)
  1556. (defvar org-blocker-hook nil
  1557. "Hook for functions that are allowed to block a state change.
  1558. Each function gets as its single argument a property list, see
  1559. `org-trigger-hook' for more information about this list.
  1560. If any of the functions in this hook returns nil, the state change
  1561. is blocked.")
  1562. (defvar org-trigger-hook nil
  1563. "Hook for functions that are triggered by a state change.
  1564. Each function gets as its single argument a property list with at least
  1565. the following elements:
  1566. (:type type-of-change :position pos-at-entry-start
  1567. :from old-state :to new-state)
  1568. Depending on the type, more properties may be present.
  1569. This mechanism is currently implemented for:
  1570. TODO state changes
  1571. ------------------
  1572. :type todo-state-change
  1573. :from previous state (keyword as a string), or nil, or a symbol
  1574. 'todo' or 'done', to indicate the general type of state.
  1575. :to new state, like in :from")
  1576. (defcustom org-enforce-todo-dependencies nil
  1577. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1578. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1579. be blocked if any prior sibling is not yet done.
  1580. Finally, if the parent is blocked because of ordered siblings of its own,
  1581. the child will also be blocked.
  1582. This variable needs to be set before org.el is loaded, and you need to
  1583. restart Emacs after a change to make the change effective. The only way
  1584. to change is while Emacs is running is through the customize interface."
  1585. :set (lambda (var val)
  1586. (set var val)
  1587. (if val
  1588. (add-hook 'org-blocker-hook
  1589. 'org-block-todo-from-children-or-siblings-or-parent)
  1590. (remove-hook 'org-blocker-hook
  1591. 'org-block-todo-from-children-or-siblings-or-parent)))
  1592. :group 'org-todo
  1593. :type 'boolean)
  1594. (defcustom org-enforce-todo-checkbox-dependencies nil
  1595. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1596. When this is nil, checkboxes have no influence on switching TODO states.
  1597. When non-nil, you first need to check off all check boxes before the TODO
  1598. entry can be switched to DONE.
  1599. This variable needs to be set before org.el is loaded, and you need to
  1600. restart Emacs after a change to make the change effective. The only way
  1601. to change is while Emacs is running is through the customize interface."
  1602. :set (lambda (var val)
  1603. (set var val)
  1604. (if val
  1605. (add-hook 'org-blocker-hook
  1606. 'org-block-todo-from-checkboxes)
  1607. (remove-hook 'org-blocker-hook
  1608. 'org-block-todo-from-checkboxes)))
  1609. :group 'org-todo
  1610. :type 'boolean)
  1611. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1612. "Non-nil means, inserting a TODO heading is treated as state change.
  1613. So when the command \\[org-insert-todo-heading] is used, state change
  1614. logging will apply if appropriate. When nil, the new TODO item will
  1615. be inserted directly, and no logging will take place."
  1616. :group 'org-todo
  1617. :type 'boolean)
  1618. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1619. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1620. This is the default behavior. However, setting this to nil allows a
  1621. convenient way to select a TODO state and bypass any logging associated
  1622. with that."
  1623. :group 'org-todo
  1624. :type 'boolean)
  1625. (defcustom org-todo-state-tags-triggers nil
  1626. "Tag changes that should be triggered by TODO state changes.
  1627. This is a list. Each entry is
  1628. (state-change (tag . flag) .......)
  1629. State-change can be a string with a state, and empty string to indicate the
  1630. state that has no TODO keyword, or it can be one of the symbols `todo'
  1631. or `done', meaning any not-done or done state, respectively."
  1632. :group 'org-todo
  1633. :group 'org-tags
  1634. :type '(repeat
  1635. (cons (choice :tag "When changing to"
  1636. (const :tag "Not-done state" todo)
  1637. (const :tag "Done state" done)
  1638. (string :tag "State"))
  1639. (repeat
  1640. (cons :tag "Tag action"
  1641. (string :tag "Tag")
  1642. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1643. (defcustom org-log-done nil
  1644. "Information to record when a task moves to the DONE state.
  1645. Possible values are:
  1646. nil Don't add anything, just change the keyword
  1647. time Add a time stamp to the task
  1648. note Prompt a closing note and add it with template `org-log-note-headings'
  1649. This option can also be set with on a per-file-basis with
  1650. #+STARTUP: nologdone
  1651. #+STARTUP: logdone
  1652. #+STARTUP: lognotedone
  1653. You can have local logging settings for a subtree by setting the LOGGING
  1654. property to one or more of these keywords."
  1655. :group 'org-todo
  1656. :group 'org-progress
  1657. :type '(choice
  1658. (const :tag "No logging" nil)
  1659. (const :tag "Record CLOSED timestamp" time)
  1660. (const :tag "Record CLOSED timestamp with closing note." note)))
  1661. ;; Normalize old uses of org-log-done.
  1662. (cond
  1663. ((eq org-log-done t) (setq org-log-done 'time))
  1664. ((and (listp org-log-done) (memq 'done org-log-done))
  1665. (setq org-log-done 'note)))
  1666. (defcustom org-log-note-clock-out nil
  1667. "Non-nil means, record a note when clocking out of an item.
  1668. This can also be configured on a per-file basis by adding one of
  1669. the following lines anywhere in the buffer:
  1670. #+STARTUP: lognoteclock-out
  1671. #+STARTUP: nolognoteclock-out"
  1672. :group 'org-todo
  1673. :group 'org-progress
  1674. :type 'boolean)
  1675. (defcustom org-log-done-with-time t
  1676. "Non-nil means, the CLOSED time stamp will contain date and time.
  1677. When nil, only the date will be recorded."
  1678. :group 'org-progress
  1679. :type 'boolean)
  1680. (defcustom org-log-note-headings
  1681. '((done . "CLOSING NOTE %t")
  1682. (state . "State %-12s from %-12S %t")
  1683. (note . "Note taken on %t")
  1684. (clock-out . ""))
  1685. "Headings for notes added to entries.
  1686. The value is an alist, with the car being a symbol indicating the note
  1687. context, and the cdr is the heading to be used. The heading may also be the
  1688. empty string.
  1689. %t in the heading will be replaced by a time stamp.
  1690. %s will be replaced by the new TODO state, in double quotes.
  1691. %S will be replaced by the old TODO state, in double quotes.
  1692. %u will be replaced by the user name.
  1693. %U will be replaced by the full user name."
  1694. :group 'org-todo
  1695. :group 'org-progress
  1696. :type '(list :greedy t
  1697. (cons (const :tag "Heading when closing an item" done) string)
  1698. (cons (const :tag
  1699. "Heading when changing todo state (todo sequence only)"
  1700. state) string)
  1701. (cons (const :tag "Heading when just taking a note" note) string)
  1702. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1703. (unless (assq 'note org-log-note-headings)
  1704. (push '(note . "%t") org-log-note-headings))
  1705. (defcustom org-log-into-drawer nil
  1706. "Non-nil means, insert state change notes and time stamps into a drawer.
  1707. When nil, state changes notes will be inserted after the headline and
  1708. any scheduling and clock lines, but not inside a drawer.
  1709. The value of this variable should be the name of the drawer to use.
  1710. LOGBOOK is proposed at the default drawer for this purpose, you can
  1711. also set this to a string to define the drawer of your choice.
  1712. A value of t is also allowed, representing \"LOGBOOK\".
  1713. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1714. will be ignored.
  1715. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1716. a subtree."
  1717. :group 'org-todo
  1718. :group 'org-progress
  1719. :type '(choice
  1720. (const :tag "Not into a drawer" nil)
  1721. (const :tag "LOGBOOK" t)
  1722. (string :tag "Other")))
  1723. (if (fboundp 'defvaralias)
  1724. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1725. (defun org-log-into-drawer ()
  1726. "Return the value of `org-log-into-drawer', but let properties overrule.
  1727. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1728. used instead of the default value."
  1729. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1730. (cond
  1731. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1732. ((equal p "t") "LOGBOOK")
  1733. (t p))))
  1734. (defcustom org-log-state-notes-insert-after-drawers nil
  1735. "Non-nil means, insert state change notes after any drawers in entry.
  1736. Only the drawers that *immediately* follow the headline and the
  1737. deadline/scheduled line are skipped.
  1738. When nil, insert notes right after the heading and perhaps the line
  1739. with deadline/scheduling if present.
  1740. This variable will have no effect if `org-log-into-drawer' is
  1741. set."
  1742. :group 'org-todo
  1743. :group 'org-progress
  1744. :type 'boolean)
  1745. (defcustom org-log-states-order-reversed t
  1746. "Non-nil means, the latest state change note will be directly after heading.
  1747. When nil, the notes will be orderer according to time."
  1748. :group 'org-todo
  1749. :group 'org-progress
  1750. :type 'boolean)
  1751. (defcustom org-log-repeat 'time
  1752. "Non-nil means, record moving through the DONE state when triggering repeat.
  1753. An auto-repeating tasks is immediately switched back to TODO when marked
  1754. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1755. the TODO keyword definition, or recording a closing note by setting
  1756. `org-log-done', there will be no record of the task moving through DONE.
  1757. This variable forces taking a note anyway. Possible values are:
  1758. nil Don't force a record
  1759. time Record a time stamp
  1760. note Record a note
  1761. This option can also be set with on a per-file-basis with
  1762. #+STARTUP: logrepeat
  1763. #+STARTUP: lognoterepeat
  1764. #+STARTUP: nologrepeat
  1765. You can have local logging settings for a subtree by setting the LOGGING
  1766. property to one or more of these keywords."
  1767. :group 'org-todo
  1768. :group 'org-progress
  1769. :type '(choice
  1770. (const :tag "Don't force a record" nil)
  1771. (const :tag "Force recording the DONE state" time)
  1772. (const :tag "Force recording a note with the DONE state" note)))
  1773. (defgroup org-priorities nil
  1774. "Priorities in Org-mode."
  1775. :tag "Org Priorities"
  1776. :group 'org-todo)
  1777. (defcustom org-enable-priority-commands t
  1778. "Non-nil means, priority commands are active.
  1779. When nil, these commands will be disabled, so that you never accidentally
  1780. set a priority."
  1781. :group 'org-priorities
  1782. :type 'boolean)
  1783. (defcustom org-highest-priority ?A
  1784. "The highest priority of TODO items. A character like ?A, ?B etc.
  1785. Must have a smaller ASCII number than `org-lowest-priority'."
  1786. :group 'org-priorities
  1787. :type 'character)
  1788. (defcustom org-lowest-priority ?C
  1789. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1790. Must have a larger ASCII number than `org-highest-priority'."
  1791. :group 'org-priorities
  1792. :type 'character)
  1793. (defcustom org-default-priority ?B
  1794. "The default priority of TODO items.
  1795. This is the priority an item get if no explicit priority is given."
  1796. :group 'org-priorities
  1797. :type 'character)
  1798. (defcustom org-priority-start-cycle-with-default t
  1799. "Non-nil means, start with default priority when starting to cycle.
  1800. When this is nil, the first step in the cycle will be (depending on the
  1801. command used) one higher or lower that the default priority."
  1802. :group 'org-priorities
  1803. :type 'boolean)
  1804. (defgroup org-time nil
  1805. "Options concerning time stamps and deadlines in Org-mode."
  1806. :tag "Org Time"
  1807. :group 'org)
  1808. (defcustom org-insert-labeled-timestamps-at-point nil
  1809. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1810. When nil, these labeled time stamps are forces into the second line of an
  1811. entry, just after the headline. When scheduling from the global TODO list,
  1812. the time stamp will always be forced into the second line."
  1813. :group 'org-time
  1814. :type 'boolean)
  1815. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1816. "Formats for `format-time-string' which are used for time stamps.
  1817. It is not recommended to change this constant.")
  1818. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1819. "Number of minutes to round time stamps to.
  1820. These are two values, the first applies when first creating a time stamp.
  1821. The second applies when changing it with the commands `S-up' and `S-down'.
  1822. When changing the time stamp, this means that it will change in steps
  1823. of N minutes, as given by the second value.
  1824. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1825. numbers should be factors of 60, so for example 5, 10, 15.
  1826. When this is larger than 1, you can still force an exact time-stamp by using
  1827. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1828. and by using a prefix arg to `S-up/down' to specify the exact number
  1829. of minutes to shift."
  1830. :group 'org-time
  1831. :get '(lambda (var) ; Make sure all entries have 5 elements
  1832. (if (integerp (default-value var))
  1833. (list (default-value var) 5)
  1834. (default-value var)))
  1835. :type '(list
  1836. (integer :tag "when inserting times")
  1837. (integer :tag "when modifying times")))
  1838. ;; Normalize old customizations of this variable.
  1839. (when (integerp org-time-stamp-rounding-minutes)
  1840. (setq org-time-stamp-rounding-minutes
  1841. (list org-time-stamp-rounding-minutes
  1842. org-time-stamp-rounding-minutes)))
  1843. (defcustom org-display-custom-times nil
  1844. "Non-nil means, overlay custom formats over all time stamps.
  1845. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1846. To turn this on on a per-file basis, insert anywhere in the file:
  1847. #+STARTUP: customtime"
  1848. :group 'org-time
  1849. :set 'set-default
  1850. :type 'sexp)
  1851. (make-variable-buffer-local 'org-display-custom-times)
  1852. (defcustom org-time-stamp-custom-formats
  1853. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1854. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1855. These are overlayed over the default ISO format if the variable
  1856. `org-display-custom-times' is set. Time like %H:%M should be at the
  1857. end of the second format. The custom formats are also honored by export
  1858. commands, if custom time display is turned on at the time of export."
  1859. :group 'org-time
  1860. :type 'sexp)
  1861. (defun org-time-stamp-format (&optional long inactive)
  1862. "Get the right format for a time string."
  1863. (let ((f (if long (cdr org-time-stamp-formats)
  1864. (car org-time-stamp-formats))))
  1865. (if inactive
  1866. (concat "[" (substring f 1 -1) "]")
  1867. f)))
  1868. (defcustom org-time-clocksum-format "%d:%02d"
  1869. "The format string used when creating CLOCKSUM lines, or when
  1870. org-mode generates a time duration."
  1871. :group 'org-time
  1872. :type 'string)
  1873. (defcustom org-deadline-warning-days 14
  1874. "No. of days before expiration during which a deadline becomes active.
  1875. This variable governs the display in sparse trees and in the agenda.
  1876. When 0 or negative, it means use this number (the absolute value of it)
  1877. even if a deadline has a different individual lead time specified.
  1878. Custom commands can set this variable in the options section."
  1879. :group 'org-time
  1880. :group 'org-agenda-daily/weekly
  1881. :type 'integer)
  1882. (defcustom org-read-date-prefer-future t
  1883. "Non-nil means, assume future for incomplete date input from user.
  1884. This affects the following situations:
  1885. 1. The user gives a day, but no month.
  1886. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1887. read this as the third of *next* month. However, if you enter \"17\",
  1888. it will be considered as *this* month.
  1889. 2. The user gives a month but not a year.
  1890. For example, if it is april and you enter \"feb 2\", this will be read
  1891. as feb 2, *next* year. \"May 5\", however, will be this year.
  1892. Currently this does not work for ISO week specifications.
  1893. When this option is nil, the current month and year will always be used
  1894. as defaults."
  1895. :group 'org-time
  1896. :type 'boolean)
  1897. (defcustom org-read-date-display-live t
  1898. "Non-nil means, display current interpretation of date prompt live.
  1899. This display will be in an overlay, in the minibuffer."
  1900. :group 'org-time
  1901. :type 'boolean)
  1902. (defcustom org-read-date-popup-calendar t
  1903. "Non-nil means, pop up a calendar when prompting for a date.
  1904. In the calendar, the date can be selected with mouse-1. However, the
  1905. minibuffer will also be active, and you can simply enter the date as well.
  1906. When nil, only the minibuffer will be available."
  1907. :group 'org-time
  1908. :type 'boolean)
  1909. (if (fboundp 'defvaralias)
  1910. (defvaralias 'org-popup-calendar-for-date-prompt
  1911. 'org-read-date-popup-calendar))
  1912. (defcustom org-read-date-minibuffer-setup-hook nil
  1913. "Hook to be used to set up keys for the date/time interface.
  1914. Add key definitions to `minibuffer-local-map', which will be a temporary
  1915. copy."
  1916. :group 'org-time
  1917. :type 'hook)
  1918. (defcustom org-extend-today-until 0
  1919. "The hour when your day really ends. Must be an integer.
  1920. This has influence for the following applications:
  1921. - When switching the agenda to \"today\". It it is still earlier than
  1922. the time given here, the day recognized as TODAY is actually yesterday.
  1923. - When a date is read from the user and it is still before the time given
  1924. here, the current date and time will be assumed to be yesterday, 23:59.
  1925. Also, timestamps inserted in remember templates follow this rule.
  1926. IMPORTANT: This is a feature whose implementation is and likely will
  1927. remain incomplete. Really, it is only here because past midnight seems to
  1928. be the favorite working time of John Wiegley :-)"
  1929. :group 'org-time
  1930. :type 'integer)
  1931. (defcustom org-edit-timestamp-down-means-later nil
  1932. "Non-nil means, S-down will increase the time in a time stamp.
  1933. When nil, S-up will increase."
  1934. :group 'org-time
  1935. :type 'boolean)
  1936. (defcustom org-calendar-follow-timestamp-change t
  1937. "Non-nil means, make the calendar window follow timestamp changes.
  1938. When a timestamp is modified and the calendar window is visible, it will be
  1939. moved to the new date."
  1940. :group 'org-time
  1941. :type 'boolean)
  1942. (defgroup org-tags nil
  1943. "Options concerning tags in Org-mode."
  1944. :tag "Org Tags"
  1945. :group 'org)
  1946. (defcustom org-tag-alist nil
  1947. "List of tags allowed in Org-mode files.
  1948. When this list is nil, Org-mode will base TAG input on what is already in the
  1949. buffer.
  1950. The value of this variable is an alist, the car of each entry must be a
  1951. keyword as a string, the cdr may be a character that is used to select
  1952. that tag through the fast-tag-selection interface.
  1953. See the manual for details."
  1954. :group 'org-tags
  1955. :type '(repeat
  1956. (choice
  1957. (cons (string :tag "Tag name")
  1958. (character :tag "Access char"))
  1959. (const :tag "Start radio group" (:startgroup))
  1960. (const :tag "End radio group" (:endgroup))
  1961. (const :tag "New line" (:newline)))))
  1962. (defcustom org-tag-persistent-alist nil
  1963. "List of tags that will always appear in all Org-mode files.
  1964. This is in addition to any in buffer settings or customizations
  1965. of `org-tag-alist'.
  1966. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1967. The value of this variable is an alist, the car of each entry must be a
  1968. keyword as a string, the cdr may be a character that is used to select
  1969. that tag through the fast-tag-selection interface.
  1970. See the manual for details.
  1971. To disable these tags on a per-file basis, insert anywhere in the file:
  1972. #+STARTUP: noptag"
  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. (defvar org-file-tags nil
  1982. "List of tags that can be inherited by all entries in the file.
  1983. The tags will be inherited if the variable `org-use-tag-inheritance'
  1984. says they should be.
  1985. This variable is populated from #+TAG lines.")
  1986. (defcustom org-use-fast-tag-selection 'auto
  1987. "Non-nil means, use fast tag selection scheme.
  1988. This is a special interface to select and deselect tags with single keys.
  1989. When nil, fast selection is never used.
  1990. When the symbol `auto', fast selection is used if and only if selection
  1991. characters for tags have been configured, either through the variable
  1992. `org-tag-alist' or through a #+TAGS line in the buffer.
  1993. When t, fast selection is always used and selection keys are assigned
  1994. automatically if necessary."
  1995. :group 'org-tags
  1996. :type '(choice
  1997. (const :tag "Always" t)
  1998. (const :tag "Never" nil)
  1999. (const :tag "When selection characters are configured" 'auto)))
  2000. (defcustom org-fast-tag-selection-single-key nil
  2001. "Non-nil means, fast tag selection exits after first change.
  2002. When nil, you have to press RET to exit it.
  2003. During fast tag selection, you can toggle this flag with `C-c'.
  2004. This variable can also have the value `expert'. In this case, the window
  2005. displaying the tags menu is not even shown, until you press C-c again."
  2006. :group 'org-tags
  2007. :type '(choice
  2008. (const :tag "No" nil)
  2009. (const :tag "Yes" t)
  2010. (const :tag "Expert" expert)))
  2011. (defvar org-fast-tag-selection-include-todo nil
  2012. "Non-nil means, fast tags selection interface will also offer TODO states.
  2013. This is an undocumented feature, you should not rely on it.")
  2014. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2015. "The column to which tags should be indented in a headline.
  2016. If this number is positive, it specifies the column. If it is negative,
  2017. it means that the tags should be flushright to that column. For example,
  2018. -80 works well for a normal 80 character screen."
  2019. :group 'org-tags
  2020. :type 'integer)
  2021. (defcustom org-auto-align-tags t
  2022. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2023. These operations change the length of a headline and therefore shift
  2024. the tags around. With this options turned on, after each such operation
  2025. the tags are again aligned to `org-tags-column'."
  2026. :group 'org-tags
  2027. :type 'boolean)
  2028. (defcustom org-use-tag-inheritance t
  2029. "Non-nil means, tags in levels apply also for sublevels.
  2030. When nil, only the tags directly given in a specific line apply there.
  2031. This may also be a list of tags that should be inherited, or a regexp that
  2032. matches tags that should be inherited. Additional control is possible
  2033. with the variable `org-tags-exclude-from-inheritance' which gives an
  2034. explicit list of tags to be excluded from inheritance., even if the value of
  2035. `org-use-tag-inheritance' would select it for inheritance.
  2036. If this option is t, a match early-on in a tree can lead to a large
  2037. number of matches in the subtree when constructing the agenda or creating
  2038. a sparse tree. If you only want to see the first match in a tree during
  2039. a search, check out the variable `org-tags-match-list-sublevels'."
  2040. :group 'org-tags
  2041. :type '(choice
  2042. (const :tag "Not" nil)
  2043. (const :tag "Always" t)
  2044. (repeat :tag "Specific tags" (string :tag "Tag"))
  2045. (regexp :tag "Tags matched by regexp")))
  2046. (defcustom org-tags-exclude-from-inheritance nil
  2047. "List of tags that should never be inherited.
  2048. This is a way to exclude a few tags from inheritance. For way to do
  2049. the opposite, to actively allow inheritance for selected tags,
  2050. see the variable `org-use-tag-inheritance'."
  2051. :group 'org-tags
  2052. :type '(repeat (string :tag "Tag")))
  2053. (defun org-tag-inherit-p (tag)
  2054. "Check if TAG is one that should be inherited."
  2055. (cond
  2056. ((member tag org-tags-exclude-from-inheritance) nil)
  2057. ((eq org-use-tag-inheritance t) t)
  2058. ((not org-use-tag-inheritance) nil)
  2059. ((stringp org-use-tag-inheritance)
  2060. (string-match org-use-tag-inheritance tag))
  2061. ((listp org-use-tag-inheritance)
  2062. (member tag org-use-tag-inheritance))
  2063. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2064. (defcustom org-tags-match-list-sublevels t
  2065. "Non-nil means list also sublevels of headlines matching a search.
  2066. This variable applies to tags/property searches, and also to stuck
  2067. projects because this search is based on a tags match as well.
  2068. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2069. the sublevels of a headline matching a tag search often also match
  2070. the same search. Listing all of them can create very long lists.
  2071. Setting this variable to nil causes subtrees of a match to be skipped.
  2072. As a special case, if the tag search is restricted to TODO items, the
  2073. value of this variable is ignored and sublevels are always checked, to
  2074. make sure all corresponding TODO items find their way into the list.
  2075. This variable is semi-obsolete and probably should always be true. It
  2076. is better to limit inheritance to certain tags using the variables
  2077. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2078. :group 'org-tags
  2079. :type '(choice
  2080. (const :tag "No, don't list them" nil)
  2081. (const :tag "Yes, do list them" t)
  2082. (const :tag "List them, indented with leading dots" indented)))
  2083. (defvar org-tags-history nil
  2084. "History of minibuffer reads for tags.")
  2085. (defvar org-last-tags-completion-table nil
  2086. "The last used completion table for tags.")
  2087. (defvar org-after-tags-change-hook nil
  2088. "Hook that is run after the tags in a line have changed.")
  2089. (defgroup org-properties nil
  2090. "Options concerning properties in Org-mode."
  2091. :tag "Org Properties"
  2092. :group 'org)
  2093. (defcustom org-property-format "%-10s %s"
  2094. "How property key/value pairs should be formatted by `indent-line'.
  2095. When `indent-line' hits a property definition, it will format the line
  2096. according to this format, mainly to make sure that the values are
  2097. lined-up with respect to each other."
  2098. :group 'org-properties
  2099. :type 'string)
  2100. (defcustom org-use-property-inheritance nil
  2101. "Non-nil means, properties apply also for sublevels.
  2102. This setting is chiefly used during property searches. Turning it on can
  2103. cause significant overhead when doing a search, which is why it is not
  2104. on by default.
  2105. When nil, only the properties directly given in the current entry count.
  2106. When t, every property is inherited. The value may also be a list of
  2107. properties that should have inheritance, or a regular expression matching
  2108. properties that should be inherited.
  2109. However, note that some special properties use inheritance under special
  2110. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2111. and the properties ending in \"_ALL\" when they are used as descriptor
  2112. for valid values of a property.
  2113. Note for programmers:
  2114. When querying an entry with `org-entry-get', you can control if inheritance
  2115. should be used. By default, `org-entry-get' looks only at the local
  2116. properties. You can request inheritance by setting the inherit argument
  2117. to t (to force inheritance) or to `selective' (to respect the setting
  2118. in this variable)."
  2119. :group 'org-properties
  2120. :type '(choice
  2121. (const :tag "Not" nil)
  2122. (const :tag "Always" t)
  2123. (repeat :tag "Specific properties" (string :tag "Property"))
  2124. (regexp :tag "Properties matched by regexp")))
  2125. (defun org-property-inherit-p (property)
  2126. "Check if PROPERTY is one that should be inherited."
  2127. (cond
  2128. ((eq org-use-property-inheritance t) t)
  2129. ((not org-use-property-inheritance) nil)
  2130. ((stringp org-use-property-inheritance)
  2131. (string-match org-use-property-inheritance property))
  2132. ((listp org-use-property-inheritance)
  2133. (member property org-use-property-inheritance))
  2134. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2135. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2136. "The default column format, if no other format has been defined.
  2137. This variable can be set on the per-file basis by inserting a line
  2138. #+COLUMNS: %25ITEM ....."
  2139. :group 'org-properties
  2140. :type 'string)
  2141. (defcustom org-columns-ellipses ".."
  2142. "The ellipses to be used when a field in column view is truncated.
  2143. When this is the empty string, as many characters as possible are shown,
  2144. but then there will be no visual indication that the field has been truncated.
  2145. When this is a string of length N, the last N characters of a truncated
  2146. field are replaced by this string. If the column is narrower than the
  2147. ellipses string, only part of the ellipses string will be shown."
  2148. :group 'org-properties
  2149. :type 'string)
  2150. (defcustom org-columns-modify-value-for-display-function nil
  2151. "Function that modifies values for display in column view.
  2152. For example, it can be used to cut out a certain part from a time stamp.
  2153. The function must take 2 arguments:
  2154. column-title The title of the column (*not* the property name)
  2155. value The value that should be modified.
  2156. The function should return the value that should be displayed,
  2157. or nil if the normal value should be used."
  2158. :group 'org-properties
  2159. :type 'function)
  2160. (defcustom org-effort-property "Effort"
  2161. "The property that is being used to keep track of effort estimates.
  2162. Effort estimates given in this property need to have the format H:MM."
  2163. :group 'org-properties
  2164. :group 'org-progress
  2165. :type '(string :tag "Property"))
  2166. (defconst org-global-properties-fixed
  2167. '(("VISIBILITY_ALL" . "folded children content all")
  2168. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2169. "List of property/value pairs that can be inherited by any entry.
  2170. These are fixed values, for the preset properties. The user variable
  2171. that can be used to add to this list is `org-global-properties'.
  2172. The entries in this list are cons cells where the car is a property
  2173. name and cdr is a string with the value. If the value represents
  2174. multiple items like an \"_ALL\" property, separate the items by
  2175. spaces.")
  2176. (defcustom org-global-properties nil
  2177. "List of property/value pairs that can be inherited by any entry.
  2178. This list will be combined with the constant `org-global-properties-fixed'.
  2179. The entries in this list are cons cells where the car is a property
  2180. name and cdr is a string with the value.
  2181. You can set buffer-local values for the same purpose in the variable
  2182. `org-file-properties' this by adding lines like
  2183. #+PROPERTY: NAME VALUE"
  2184. :group 'org-properties
  2185. :type '(repeat
  2186. (cons (string :tag "Property")
  2187. (string :tag "Value"))))
  2188. (defvar org-file-properties nil
  2189. "List of property/value pairs that can be inherited by any entry.
  2190. Valid for the current buffer.
  2191. This variable is populated from #+PROPERTY lines.")
  2192. (make-variable-buffer-local 'org-file-properties)
  2193. (defgroup org-agenda nil
  2194. "Options concerning agenda views in Org-mode."
  2195. :tag "Org Agenda"
  2196. :group 'org)
  2197. (defvar org-category nil
  2198. "Variable used by org files to set a category for agenda display.
  2199. Such files should use a file variable to set it, for example
  2200. # -*- mode: org; org-category: \"ELisp\"
  2201. or contain a special line
  2202. #+CATEGORY: ELisp
  2203. If the file does not specify a category, then file's base name
  2204. is used instead.")
  2205. (make-variable-buffer-local 'org-category)
  2206. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2207. (defcustom org-agenda-files nil
  2208. "The files to be used for agenda display.
  2209. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2210. \\[org-remove-file]. You can also use customize to edit the list.
  2211. If an entry is a directory, all files in that directory that are matched by
  2212. `org-agenda-file-regexp' will be part of the file list.
  2213. If the value of the variable is not a list but a single file name, then
  2214. the list of agenda files is actually stored and maintained in that file, one
  2215. agenda file per line."
  2216. :group 'org-agenda
  2217. :type '(choice
  2218. (repeat :tag "List of files and directories" file)
  2219. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2220. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2221. "Regular expression to match files for `org-agenda-files'.
  2222. If any element in the list in that variable contains a directory instead
  2223. of a normal file, all files in that directory that are matched by this
  2224. regular expression will be included."
  2225. :group 'org-agenda
  2226. :type 'regexp)
  2227. (defcustom org-agenda-text-search-extra-files nil
  2228. "List of extra files to be searched by text search commands.
  2229. These files will be search in addition to the agenda files by the
  2230. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2231. Note that these files will only be searched for text search commands,
  2232. not for the other agenda views like todo lists, tag searches or the weekly
  2233. agenda. This variable is intended to list notes and possibly archive files
  2234. that should also be searched by these two commands.
  2235. In fact, if the first element in the list is the symbol `agenda-archives',
  2236. than all archive files of all agenda files will be added to the search
  2237. scope."
  2238. :group 'org-agenda
  2239. :type '(set :greedy t
  2240. (const :tag "Agenda Archives" agenda-archives)
  2241. (repeat :inline t (file))))
  2242. (if (fboundp 'defvaralias)
  2243. (defvaralias 'org-agenda-multi-occur-extra-files
  2244. 'org-agenda-text-search-extra-files))
  2245. (defcustom org-agenda-skip-unavailable-files nil
  2246. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2247. A nil value means to remove them, after a query, from the list."
  2248. :group 'org-agenda
  2249. :type 'boolean)
  2250. (defcustom org-calendar-to-agenda-key [?c]
  2251. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2252. The command `org-calendar-goto-agenda' will be bound to this key. The
  2253. default is the character `c' because then `c' can be used to switch back and
  2254. forth between agenda and calendar."
  2255. :group 'org-agenda
  2256. :type 'sexp)
  2257. (defcustom org-calendar-agenda-action-key [?k]
  2258. "The key to be installed in `calendar-mode-map' for agenda-action.
  2259. The command `org-agenda-action' will be bound to this key. The
  2260. default is the character `k' because we use the same key in the agenda."
  2261. :group 'org-agenda
  2262. :type 'sexp)
  2263. (eval-after-load "calendar"
  2264. '(progn
  2265. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2266. 'org-calendar-goto-agenda)
  2267. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2268. 'org-agenda-action)))
  2269. (defgroup org-latex nil
  2270. "Options for embedding LaTeX code into Org-mode."
  2271. :tag "Org LaTeX"
  2272. :group 'org)
  2273. (defcustom org-format-latex-options
  2274. '(:foreground default :background default :scale 1.0
  2275. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2276. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2277. "Options for creating images from LaTeX fragments.
  2278. This is a property list with the following properties:
  2279. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2280. `default' means use the foreground of the default face.
  2281. :background the background color, or \"Transparent\".
  2282. `default' means use the background of the default face.
  2283. :scale a scaling factor for the size of the images.
  2284. :html-foreground, :html-background, :html-scale
  2285. the same numbers for HTML export.
  2286. :matchers a list indicating which matchers should be used to
  2287. find LaTeX fragments. Valid members of this list are:
  2288. \"begin\" find environments
  2289. \"$1\" find single characters surrounded by $.$
  2290. \"$\" find math expressions surrounded by $...$
  2291. \"$$\" find math expressions surrounded by $$....$$
  2292. \"\\(\" find math expressions surrounded by \\(...\\)
  2293. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2294. :group 'org-latex
  2295. :type 'plist)
  2296. (defcustom org-format-latex-header "\\documentclass{article}
  2297. \\usepackage{fullpage} % do not remove
  2298. \\usepackage{amssymb}
  2299. \\usepackage[usenames]{color}
  2300. \\usepackage{amsmath}
  2301. \\usepackage{latexsym}
  2302. \\usepackage[mathscr]{eucal}
  2303. \\pagestyle{empty} % do not remove"
  2304. "The document header used for processing LaTeX fragments."
  2305. :group 'org-latex
  2306. :type 'string)
  2307. (defgroup org-font-lock nil
  2308. "Font-lock settings for highlighting in Org-mode."
  2309. :tag "Org Font Lock"
  2310. :group 'org)
  2311. (defcustom org-level-color-stars-only nil
  2312. "Non-nil means fontify only the stars in each headline.
  2313. When nil, the entire headline is fontified.
  2314. Changing it requires restart of `font-lock-mode' to become effective
  2315. also in regions already fontified."
  2316. :group 'org-font-lock
  2317. :type 'boolean)
  2318. (defcustom org-hide-leading-stars nil
  2319. "Non-nil means, hide the first N-1 stars in a headline.
  2320. This works by using the face `org-hide' for these stars. This
  2321. face is white for a light background, and black for a dark
  2322. background. You may have to customize the face `org-hide' to
  2323. make this work.
  2324. Changing it requires restart of `font-lock-mode' to become effective
  2325. also in regions already fontified.
  2326. You may also set this on a per-file basis by adding one of the following
  2327. lines to the buffer:
  2328. #+STARTUP: hidestars
  2329. #+STARTUP: showstars"
  2330. :group 'org-font-lock
  2331. :type 'boolean)
  2332. (defcustom org-fontify-done-headline nil
  2333. "Non-nil means, change the face of a headline if it is marked DONE.
  2334. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2335. When this is non-nil, the headline after the keyword is set to the
  2336. `org-headline-done' as an additional indication."
  2337. :group 'org-font-lock
  2338. :type 'boolean)
  2339. (defcustom org-fontify-emphasized-text t
  2340. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2341. Changing this variable requires a restart of Emacs to take effect."
  2342. :group 'org-font-lock
  2343. :type 'boolean)
  2344. (defcustom org-highlight-latex-fragments-and-specials nil
  2345. "Non-nil means, fontify what is treated specially by the exporters."
  2346. :group 'org-font-lock
  2347. :type 'boolean)
  2348. (defcustom org-hide-emphasis-markers nil
  2349. "Non-nil mean font-lock should hide the emphasis marker characters."
  2350. :group 'org-font-lock
  2351. :type 'boolean)
  2352. (defvar org-emph-re nil
  2353. "Regular expression for matching emphasis.")
  2354. (defvar org-verbatim-re nil
  2355. "Regular expression for matching verbatim text.")
  2356. (defvar org-emphasis-regexp-components) ; defined just below
  2357. (defvar org-emphasis-alist) ; defined just below
  2358. (defun org-set-emph-re (var val)
  2359. "Set variable and compute the emphasis regular expression."
  2360. (set var val)
  2361. (when (and (boundp 'org-emphasis-alist)
  2362. (boundp 'org-emphasis-regexp-components)
  2363. org-emphasis-alist org-emphasis-regexp-components)
  2364. (let* ((e org-emphasis-regexp-components)
  2365. (pre (car e))
  2366. (post (nth 1 e))
  2367. (border (nth 2 e))
  2368. (body (nth 3 e))
  2369. (nl (nth 4 e))
  2370. (body1 (concat body "*?"))
  2371. (markers (mapconcat 'car org-emphasis-alist ""))
  2372. (vmarkers (mapconcat
  2373. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2374. org-emphasis-alist "")))
  2375. ;; make sure special characters appear at the right position in the class
  2376. (if (string-match "\\^" markers)
  2377. (setq markers (concat (replace-match "" t t markers) "^")))
  2378. (if (string-match "-" markers)
  2379. (setq markers (concat (replace-match "" t t markers) "-")))
  2380. (if (string-match "\\^" vmarkers)
  2381. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2382. (if (string-match "-" vmarkers)
  2383. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2384. (if (> nl 0)
  2385. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2386. (int-to-string nl) "\\}")))
  2387. ;; Make the regexp
  2388. (setq org-emph-re
  2389. (concat "\\([" pre "]\\|^\\)"
  2390. "\\("
  2391. "\\([" markers "]\\)"
  2392. "\\("
  2393. "[^" border "]\\|"
  2394. "[^" border "]"
  2395. body1
  2396. "[^" border "]"
  2397. "\\)"
  2398. "\\3\\)"
  2399. "\\([" post "]\\|$\\)"))
  2400. (setq org-verbatim-re
  2401. (concat "\\([" pre "]\\|^\\)"
  2402. "\\("
  2403. "\\([" vmarkers "]\\)"
  2404. "\\("
  2405. "[^" border "]\\|"
  2406. "[^" border "]"
  2407. body1
  2408. "[^" border "]"
  2409. "\\)"
  2410. "\\3\\)"
  2411. "\\([" post "]\\|$\\)")))))
  2412. (defcustom org-emphasis-regexp-components
  2413. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2414. "Components used to build the regular expression for emphasis.
  2415. This is a list with 6 entries. Terminology: In an emphasis string
  2416. like \" *strong word* \", we call the initial space PREMATCH, the final
  2417. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2418. and \"trong wor\" is the body. The different components in this variable
  2419. specify what is allowed/forbidden in each part:
  2420. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2421. post Chars allowed as postmatch. End of line will be allowed too.
  2422. border The chars *forbidden* as border characters.
  2423. body-regexp A regexp like \".\" to match a body character. Don't use
  2424. non-shy groups here, and don't allow newline here.
  2425. newline The maximum number of newlines allowed in an emphasis exp.
  2426. Use customize to modify this, or restart Emacs after changing it."
  2427. :group 'org-font-lock
  2428. :set 'org-set-emph-re
  2429. :type '(list
  2430. (sexp :tag "Allowed chars in pre ")
  2431. (sexp :tag "Allowed chars in post ")
  2432. (sexp :tag "Forbidden chars in border ")
  2433. (sexp :tag "Regexp for body ")
  2434. (integer :tag "number of newlines allowed")
  2435. (option (boolean :tag "Please ignore this button"))))
  2436. (defcustom org-emphasis-alist
  2437. `(("*" bold "<b>" "</b>")
  2438. ("/" italic "<i>" "</i>")
  2439. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2440. ("=" org-code "<code>" "</code>" verbatim)
  2441. ("~" org-verbatim "<code>" "</code>" verbatim)
  2442. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2443. "<del>" "</del>")
  2444. )
  2445. "Special syntax for emphasized text.
  2446. Text starting and ending with a special character will be emphasized, for
  2447. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2448. characters, the face to be used by font-lock for highlighting in Org-mode
  2449. Emacs buffers, and the HTML tags to be used for this.
  2450. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2451. Use customize to modify this, or restart Emacs after changing it."
  2452. :group 'org-font-lock
  2453. :set 'org-set-emph-re
  2454. :type '(repeat
  2455. (list
  2456. (string :tag "Marker character")
  2457. (choice
  2458. (face :tag "Font-lock-face")
  2459. (plist :tag "Face property list"))
  2460. (string :tag "HTML start tag")
  2461. (string :tag "HTML end tag")
  2462. (option (const verbatim)))))
  2463. ;;; Miscellaneous options
  2464. (defgroup org-completion nil
  2465. "Completion in Org-mode."
  2466. :tag "Org Completion"
  2467. :group 'org)
  2468. (defcustom org-completion-use-ido nil
  2469. "Non-nil means, use ido completion wherever possible.
  2470. Note that `ido-mode' must be active for this variable to be relevant.
  2471. If you decide to turn this variable on, you might well want to turn off
  2472. `org-outline-path-complete-in-steps'."
  2473. :group 'org-completion
  2474. :type 'boolean)
  2475. (defcustom org-completion-fallback-command 'hippie-expand
  2476. "The expansion command called by \\[org-complete] in normal context.
  2477. Normal means, no org-mode-specific context."
  2478. :group 'org-completion
  2479. :type 'function)
  2480. ;;; Functions and variables from ther packages
  2481. ;; Declared here to avoid compiler warnings
  2482. ;; XEmacs only
  2483. (defvar outline-mode-menu-heading)
  2484. (defvar outline-mode-menu-show)
  2485. (defvar outline-mode-menu-hide)
  2486. (defvar zmacs-regions) ; XEmacs regions
  2487. ;; Emacs only
  2488. (defvar mark-active)
  2489. ;; Various packages
  2490. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2491. (declare-function calendar-forward-day "cal-move" (arg))
  2492. (declare-function calendar-goto-date "cal-move" (date))
  2493. (declare-function calendar-goto-today "cal-move" ())
  2494. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2495. (defvar calc-embedded-close-formula)
  2496. (defvar calc-embedded-open-formula)
  2497. (declare-function cdlatex-tab "ext:cdlatex" ())
  2498. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2499. (defvar font-lock-unfontify-region-function)
  2500. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2501. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2502. (defvar iswitchb-temp-buflist)
  2503. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2504. (defvar org-agenda-tags-todo-honor-ignore-options)
  2505. (declare-function org-agenda-skip "org-agenda" ())
  2506. (declare-function org-format-agenda-item "org-agenda"
  2507. (extra txt &optional category tags dotime noprefix remove-re))
  2508. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2509. (declare-function org-agenda-change-all-lines "org-agenda"
  2510. (newhead hdmarker &optional fixface just-this))
  2511. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2512. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2513. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2514. (beg end))
  2515. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2516. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2517. "org-agenda" (&optional end))
  2518. (declare-function parse-time-string "parse-time" (string))
  2519. (declare-function remember "remember" (&optional initial))
  2520. (declare-function remember-buffer-desc "remember" ())
  2521. (declare-function remember-finalize "remember" ())
  2522. (defvar remember-save-after-remembering)
  2523. (defvar remember-data-file)
  2524. (defvar remember-register)
  2525. (defvar remember-buffer)
  2526. (defvar remember-handler-functions)
  2527. (defvar remember-annotation-functions)
  2528. (defvar texmathp-why)
  2529. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2530. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2531. (defvar w3m-current-url)
  2532. (defvar w3m-current-title)
  2533. (defvar org-latex-regexps)
  2534. ;;; Autoload and prepare some org modules
  2535. ;; Some table stuff that needs to be defined here, because it is used
  2536. ;; by the functions setting up org-mode or checking for table context.
  2537. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2538. "Detects an org-type or table-type table.")
  2539. (defconst org-table-line-regexp "^[ \t]*|"
  2540. "Detects an org-type table line.")
  2541. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2542. "Detects an org-type table line.")
  2543. (defconst org-table-hline-regexp "^[ \t]*|-"
  2544. "Detects an org-type table hline.")
  2545. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2546. "Detects a table-type table hline.")
  2547. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2548. "Searching from within a table (any type) this finds the first line
  2549. outside the table.")
  2550. ;; Autoload the functions in org-table.el that are needed by functions here.
  2551. (eval-and-compile
  2552. (org-autoload "org-table"
  2553. '(org-table-align org-table-begin org-table-blank-field
  2554. org-table-convert org-table-convert-region org-table-copy-down
  2555. org-table-copy-region org-table-create
  2556. org-table-create-or-convert-from-region
  2557. org-table-create-with-table.el org-table-current-dline
  2558. org-table-cut-region org-table-delete-column org-table-edit-field
  2559. org-table-edit-formulas org-table-end org-table-eval-formula
  2560. org-table-export org-table-field-info
  2561. org-table-get-stored-formulas org-table-goto-column
  2562. org-table-hline-and-move org-table-import org-table-insert-column
  2563. org-table-insert-hline org-table-insert-row org-table-iterate
  2564. org-table-justify-field-maybe org-table-kill-row
  2565. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2566. org-table-move-column org-table-move-column-left
  2567. org-table-move-column-right org-table-move-row
  2568. org-table-move-row-down org-table-move-row-up
  2569. org-table-next-field org-table-next-row org-table-paste-rectangle
  2570. org-table-previous-field org-table-recalculate
  2571. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2572. org-table-toggle-coordinate-overlays
  2573. org-table-toggle-formula-debugger org-table-wrap-region
  2574. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2575. (defun org-at-table-p (&optional table-type)
  2576. "Return t if the cursor is inside an org-type table.
  2577. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2578. (if org-enable-table-editor
  2579. (save-excursion
  2580. (beginning-of-line 1)
  2581. (looking-at (if table-type org-table-any-line-regexp
  2582. org-table-line-regexp)))
  2583. nil))
  2584. (defsubst org-table-p () (org-at-table-p))
  2585. (defun org-at-table.el-p ()
  2586. "Return t if and only if we are at a table.el table."
  2587. (and (org-at-table-p 'any)
  2588. (save-excursion
  2589. (goto-char (org-table-begin 'any))
  2590. (looking-at org-table1-hline-regexp))))
  2591. (defun org-table-recognize-table.el ()
  2592. "If there is a table.el table nearby, recognize it and move into it."
  2593. (if org-table-tab-recognizes-table.el
  2594. (if (org-at-table.el-p)
  2595. (progn
  2596. (beginning-of-line 1)
  2597. (if (looking-at org-table-dataline-regexp)
  2598. nil
  2599. (if (looking-at org-table1-hline-regexp)
  2600. (progn
  2601. (beginning-of-line 2)
  2602. (if (looking-at org-table-any-border-regexp)
  2603. (beginning-of-line -1)))))
  2604. (if (re-search-forward "|" (org-table-end t) t)
  2605. (progn
  2606. (require 'table)
  2607. (if (table--at-cell-p (point))
  2608. t
  2609. (message "recognizing table.el table...")
  2610. (table-recognize-table)
  2611. (message "recognizing table.el table...done")))
  2612. (error "This should not happen..."))
  2613. t)
  2614. nil)
  2615. nil))
  2616. (defun org-at-table-hline-p ()
  2617. "Return t if the cursor is inside a hline in a table."
  2618. (if org-enable-table-editor
  2619. (save-excursion
  2620. (beginning-of-line 1)
  2621. (looking-at org-table-hline-regexp))
  2622. nil))
  2623. (defvar org-table-clean-did-remove-column nil)
  2624. (defun org-table-map-tables (function)
  2625. "Apply FUNCTION to the start of all tables in the buffer."
  2626. (save-excursion
  2627. (save-restriction
  2628. (widen)
  2629. (goto-char (point-min))
  2630. (while (re-search-forward org-table-any-line-regexp nil t)
  2631. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2632. (beginning-of-line 1)
  2633. (if (looking-at org-table-line-regexp)
  2634. (save-excursion (funcall function)))
  2635. (re-search-forward org-table-any-border-regexp nil 1))))
  2636. (message "Mapping tables: done"))
  2637. ;; Declare and autoload functions from org-exp.el & Co
  2638. (declare-function org-default-export-plist "org-exp")
  2639. (declare-function org-infile-export-plist "org-exp")
  2640. (declare-function org-get-current-options "org-exp")
  2641. (eval-and-compile
  2642. (org-autoload "org-exp"
  2643. '(org-export org-export-visible
  2644. org-insert-export-options-template
  2645. org-table-clean-before-export))
  2646. (org-autoload "org-ascii"
  2647. '(org-export-as-ascii org-export-ascii-preprocess
  2648. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2649. org-export-region-as-ascii))
  2650. (org-autoload "org-html"
  2651. '(org-export-as-html-and-open
  2652. org-export-as-html-batch org-export-as-html-to-buffer
  2653. org-replace-region-by-html org-export-region-as-html
  2654. org-export-as-html))
  2655. (org-autoload "org-icalendar"
  2656. '(org-export-icalendar-this-file
  2657. org-export-icalendar-all-agenda-files
  2658. org-export-icalendar-combine-agenda-files))
  2659. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2660. ;; Declare and autoload functions from org-agenda.el
  2661. (eval-and-compile
  2662. (org-autoload "org-agenda"
  2663. '(org-agenda org-agenda-list org-search-view
  2664. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2665. org-diary org-agenda-to-appt
  2666. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2667. ;; Autoload org-remember
  2668. (eval-and-compile
  2669. (org-autoload "org-remember"
  2670. '(org-remember-insinuate org-remember-annotation
  2671. org-remember-apply-template org-remember org-remember-handler)))
  2672. ;; Autoload org-clock.el
  2673. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2674. (beg end))
  2675. (declare-function org-clock-update-mode-line "org-clock" ())
  2676. (defvar org-clock-start-time)
  2677. (defvar org-clock-marker (make-marker)
  2678. "Marker recording the last clock-in.")
  2679. (eval-and-compile
  2680. (org-autoload
  2681. "org-clock"
  2682. '(org-clock-in org-clock-out org-clock-cancel
  2683. org-clock-goto org-clock-sum org-clock-display
  2684. org-clock-remove-overlays org-clock-report
  2685. org-clocktable-shift org-dblock-write:clocktable
  2686. org-get-clocktable)))
  2687. (defun org-clock-update-time-maybe ()
  2688. "If this is a CLOCK line, update it and return t.
  2689. Otherwise, return nil."
  2690. (interactive)
  2691. (save-excursion
  2692. (beginning-of-line 1)
  2693. (skip-chars-forward " \t")
  2694. (when (looking-at org-clock-string)
  2695. (let ((re (concat "[ \t]*" org-clock-string
  2696. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2697. "\\([ \t]*=>.*\\)?\\)?"))
  2698. ts te h m s neg)
  2699. (cond
  2700. ((not (looking-at re))
  2701. nil)
  2702. ((not (match-end 2))
  2703. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2704. (> org-clock-marker (point))
  2705. (<= org-clock-marker (point-at-eol)))
  2706. ;; The clock is running here
  2707. (setq org-clock-start-time
  2708. (apply 'encode-time
  2709. (org-parse-time-string (match-string 1))))
  2710. (org-clock-update-mode-line)))
  2711. (t
  2712. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2713. (end-of-line 1)
  2714. (setq ts (match-string 1)
  2715. te (match-string 3))
  2716. (setq s (- (time-to-seconds
  2717. (apply 'encode-time (org-parse-time-string te)))
  2718. (time-to-seconds
  2719. (apply 'encode-time (org-parse-time-string ts))))
  2720. neg (< s 0)
  2721. s (abs s)
  2722. h (floor (/ s 3600))
  2723. s (- s (* 3600 h))
  2724. m (floor (/ s 60))
  2725. s (- s (* 60 s)))
  2726. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2727. t))))))
  2728. (defun org-check-running-clock ()
  2729. "Check if the current buffer contains the running clock.
  2730. If yes, offer to stop it and to save the buffer with the changes."
  2731. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2732. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2733. (buffer-name))))
  2734. (org-clock-out)
  2735. (when (y-or-n-p "Save changed buffer?")
  2736. (save-buffer))))
  2737. (defun org-clocktable-try-shift (dir n)
  2738. "Check if this line starts a clock table, if yes, shift the time block."
  2739. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2740. (org-clocktable-shift dir n)))
  2741. ;; Autoload org-timer.el
  2742. (eval-and-compile
  2743. (org-autoload
  2744. "org-timer"
  2745. '(org-timer-start org-timer org-timer-item
  2746. org-timer-change-times-in-region)))
  2747. ;; Autoload org-feed.el
  2748. (eval-and-compile
  2749. (org-autoload
  2750. "org-feed"
  2751. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2752. ;; Autoload archiving code
  2753. ;; The stuff that is needed for cycling and tags has to be defined here.
  2754. (defgroup org-archive nil
  2755. "Options concerning archiving in Org-mode."
  2756. :tag "Org Archive"
  2757. :group 'org-structure)
  2758. (defcustom org-archive-location "%s_archive::"
  2759. "The location where subtrees should be archived.
  2760. The value of this variable is a string, consisting of two parts,
  2761. separated by a double-colon. The first part is a filename and
  2762. the second part is a headline.
  2763. When the filename is omitted, archiving happens in the same file.
  2764. %s in the filename will be replaced by the current file
  2765. name (without the directory part). Archiving to a different file
  2766. is useful to keep archived entries from contributing to the
  2767. Org-mode Agenda.
  2768. The archived entries will be filed as subtrees of the specified
  2769. headline. When the headline is omitted, the subtrees are simply
  2770. filed away at the end of the file, as top-level entries. Also in
  2771. the heading you can use %s to represent the file name, this can be
  2772. useful when using the same archive for a number of different files.
  2773. Here are a few examples:
  2774. \"%s_archive::\"
  2775. If the current file is Projects.org, archive in file
  2776. Projects.org_archive, as top-level trees. This is the default.
  2777. \"::* Archived Tasks\"
  2778. Archive in the current file, under the top-level headline
  2779. \"* Archived Tasks\".
  2780. \"~/org/archive.org::\"
  2781. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2782. \"~/org/archive.org::From %s\"
  2783. Archive in file ~/org/archive.org (absolute path), und headlines
  2784. \"From FILENAME\" where file name is the current file name.
  2785. \"basement::** Finished Tasks\"
  2786. Archive in file ./basement (relative path), as level 3 trees
  2787. below the level 2 heading \"** Finished Tasks\".
  2788. You may set this option on a per-file basis by adding to the buffer a
  2789. line like
  2790. #+ARCHIVE: basement::** Finished Tasks
  2791. You may also define it locally for a subtree by setting an ARCHIVE property
  2792. in the entry. If such a property is found in an entry, or anywhere up
  2793. the hierarchy, it will be used."
  2794. :group 'org-archive
  2795. :type 'string)
  2796. (defcustom org-archive-tag "ARCHIVE"
  2797. "The tag that marks a subtree as archived.
  2798. An archived subtree does not open during visibility cycling, and does
  2799. not contribute to the agenda listings.
  2800. After changing this, font-lock must be restarted in the relevant buffers to
  2801. get the proper fontification."
  2802. :group 'org-archive
  2803. :group 'org-keywords
  2804. :type 'string)
  2805. (defcustom org-agenda-skip-archived-trees t
  2806. "Non-nil means, the agenda will skip any items located in archived trees.
  2807. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2808. variable is no longer recommended, you should leave it at the value t.
  2809. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2810. :group 'org-archive
  2811. :group 'org-agenda-skip
  2812. :type 'boolean)
  2813. (defcustom org-columns-skip-arrchived-trees t
  2814. "Non-nil means, irgnore archived trees when creating column view."
  2815. :group 'org-archive
  2816. :group 'org-properties
  2817. :type 'boolean)
  2818. (defcustom org-cycle-open-archived-trees nil
  2819. "Non-nil means, `org-cycle' will open archived trees.
  2820. An archived tree is a tree marked with the tag ARCHIVE.
  2821. When nil, archived trees will stay folded. You can still open them with
  2822. normal outline commands like `show-all', but not with the cycling commands."
  2823. :group 'org-archive
  2824. :group 'org-cycle
  2825. :type 'boolean)
  2826. (defcustom org-sparse-tree-open-archived-trees nil
  2827. "Non-nil means sparse tree construction shows matches in archived trees.
  2828. When nil, matches in these trees are highlighted, but the trees are kept in
  2829. collapsed state."
  2830. :group 'org-archive
  2831. :group 'org-sparse-trees
  2832. :type 'boolean)
  2833. (defun org-cycle-hide-archived-subtrees (state)
  2834. "Re-hide all archived subtrees after a visibility state change."
  2835. (when (and (not org-cycle-open-archived-trees)
  2836. (not (memq state '(overview folded))))
  2837. (save-excursion
  2838. (let* ((globalp (memq state '(contents all)))
  2839. (beg (if globalp (point-min) (point)))
  2840. (end (if globalp (point-max) (org-end-of-subtree t))))
  2841. (org-hide-archived-subtrees beg end)
  2842. (goto-char beg)
  2843. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2844. (message "%s" (substitute-command-keys
  2845. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2846. (defun org-force-cycle-archived ()
  2847. "Cycle subtree even if it is archived."
  2848. (interactive)
  2849. (setq this-command 'org-cycle)
  2850. (let ((org-cycle-open-archived-trees t))
  2851. (call-interactively 'org-cycle)))
  2852. (defun org-hide-archived-subtrees (beg end)
  2853. "Re-hide all archived subtrees after a visibility state change."
  2854. (save-excursion
  2855. (let* ((re (concat ":" org-archive-tag ":")))
  2856. (goto-char beg)
  2857. (while (re-search-forward re end t)
  2858. (and (org-on-heading-p) (hide-subtree))
  2859. (org-end-of-subtree t)))))
  2860. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2861. (eval-and-compile
  2862. (org-autoload "org-archive"
  2863. '(org-add-archive-files org-archive-subtree
  2864. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2865. ;; Autoload Column View Code
  2866. (declare-function org-columns-number-to-string "org-colview")
  2867. (declare-function org-columns-get-format-and-top-level "org-colview")
  2868. (declare-function org-columns-compute "org-colview")
  2869. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2870. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2871. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2872. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2873. ;; Autoload ID code
  2874. (declare-function org-id-store-link "org-id")
  2875. (declare-function org-id-locations-load "org-id")
  2876. (declare-function org-id-locations-save "org-id")
  2877. (defvar org-id-track-globally)
  2878. (org-autoload "org-id"
  2879. '(org-id-get-create org-id-new org-id-copy org-id-get
  2880. org-id-get-with-outline-path-completion
  2881. org-id-get-with-outline-drilling
  2882. org-id-goto org-id-find org-id-store-link))
  2883. ;; Autoload Plotting Code
  2884. (org-autoload "org-plot"
  2885. '(org-plot/gnuplot))
  2886. ;;; Variables for pre-computed regular expressions, all buffer local
  2887. (defvar org-drawer-regexp nil
  2888. "Matches first line of a hidden block.")
  2889. (make-variable-buffer-local 'org-drawer-regexp)
  2890. (defvar org-todo-regexp nil
  2891. "Matches any of the TODO state keywords.")
  2892. (make-variable-buffer-local 'org-todo-regexp)
  2893. (defvar org-not-done-regexp nil
  2894. "Matches any of the TODO state keywords except the last one.")
  2895. (make-variable-buffer-local 'org-not-done-regexp)
  2896. (defvar org-not-done-heading-regexp nil
  2897. "Matches a TODO headline that is not done.")
  2898. (make-variable-buffer-local 'org-not-done-regexp)
  2899. (defvar org-todo-line-regexp nil
  2900. "Matches a headline and puts TODO state into group 2 if present.")
  2901. (make-variable-buffer-local 'org-todo-line-regexp)
  2902. (defvar org-complex-heading-regexp nil
  2903. "Matches a headline and puts everything into groups:
  2904. group 1: the stars
  2905. group 2: The todo keyword, maybe
  2906. group 3: Priority cookie
  2907. group 4: True headline
  2908. group 5: Tags")
  2909. (make-variable-buffer-local 'org-complex-heading-regexp)
  2910. (defvar org-todo-line-tags-regexp nil
  2911. "Matches a headline and puts TODO state into group 2 if present.
  2912. Also put tags into group 4 if tags are present.")
  2913. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2914. (defvar org-nl-done-regexp nil
  2915. "Matches newline followed by a headline with the DONE keyword.")
  2916. (make-variable-buffer-local 'org-nl-done-regexp)
  2917. (defvar org-looking-at-done-regexp nil
  2918. "Matches the DONE keyword a point.")
  2919. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2920. (defvar org-ds-keyword-length 12
  2921. "Maximum length of the Deadline and SCHEDULED keywords.")
  2922. (make-variable-buffer-local 'org-ds-keyword-length)
  2923. (defvar org-deadline-regexp nil
  2924. "Matches the DEADLINE keyword.")
  2925. (make-variable-buffer-local 'org-deadline-regexp)
  2926. (defvar org-deadline-time-regexp nil
  2927. "Matches the DEADLINE keyword together with a time stamp.")
  2928. (make-variable-buffer-local 'org-deadline-time-regexp)
  2929. (defvar org-deadline-line-regexp nil
  2930. "Matches the DEADLINE keyword and the rest of the line.")
  2931. (make-variable-buffer-local 'org-deadline-line-regexp)
  2932. (defvar org-scheduled-regexp nil
  2933. "Matches the SCHEDULED keyword.")
  2934. (make-variable-buffer-local 'org-scheduled-regexp)
  2935. (defvar org-scheduled-time-regexp nil
  2936. "Matches the SCHEDULED keyword together with a time stamp.")
  2937. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2938. (defvar org-closed-time-regexp nil
  2939. "Matches the CLOSED keyword together with a time stamp.")
  2940. (make-variable-buffer-local 'org-closed-time-regexp)
  2941. (defvar org-keyword-time-regexp nil
  2942. "Matches any of the 4 keywords, together with the time stamp.")
  2943. (make-variable-buffer-local 'org-keyword-time-regexp)
  2944. (defvar org-keyword-time-not-clock-regexp nil
  2945. "Matches any of the 3 keywords, together with the time stamp.")
  2946. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2947. (defvar org-maybe-keyword-time-regexp nil
  2948. "Matches a timestamp, possibly preceeded by a keyword.")
  2949. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2950. (defvar org-planning-or-clock-line-re nil
  2951. "Matches a line with planning or clock info.")
  2952. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2953. (defconst org-plain-time-of-day-regexp
  2954. (concat
  2955. "\\(\\<[012]?[0-9]"
  2956. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2957. "\\(--?"
  2958. "\\(\\<[012]?[0-9]"
  2959. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2960. "\\)?")
  2961. "Regular expression to match a plain time or time range.
  2962. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2963. groups carry important information:
  2964. 0 the full match
  2965. 1 the first time, range or not
  2966. 8 the second time, if it is a range.")
  2967. (defconst org-plain-time-extension-regexp
  2968. (concat
  2969. "\\(\\<[012]?[0-9]"
  2970. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2971. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2972. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2973. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2974. groups carry important information:
  2975. 0 the full match
  2976. 7 hours of duration
  2977. 9 minutes of duration")
  2978. (defconst org-stamp-time-of-day-regexp
  2979. (concat
  2980. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2981. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2982. "\\(--?"
  2983. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2984. "Regular expression to match a timestamp time or time range.
  2985. After a match, the following groups carry important information:
  2986. 0 the full match
  2987. 1 date plus weekday, for backreferencing to make sure both times on same day
  2988. 2 the first time, range or not
  2989. 4 the second time, if it is a range.")
  2990. (defconst org-startup-options
  2991. '(("fold" org-startup-folded t)
  2992. ("overview" org-startup-folded t)
  2993. ("nofold" org-startup-folded nil)
  2994. ("showall" org-startup-folded nil)
  2995. ("content" org-startup-folded content)
  2996. ("hidestars" org-hide-leading-stars t)
  2997. ("showstars" org-hide-leading-stars nil)
  2998. ("odd" org-odd-levels-only t)
  2999. ("oddeven" org-odd-levels-only nil)
  3000. ("align" org-startup-align-all-tables t)
  3001. ("noalign" org-startup-align-all-tables nil)
  3002. ("customtime" org-display-custom-times t)
  3003. ("logdone" org-log-done time)
  3004. ("lognotedone" org-log-done note)
  3005. ("nologdone" org-log-done nil)
  3006. ("lognoteclock-out" org-log-note-clock-out t)
  3007. ("nolognoteclock-out" org-log-note-clock-out nil)
  3008. ("logrepeat" org-log-repeat state)
  3009. ("lognoterepeat" org-log-repeat note)
  3010. ("nologrepeat" org-log-repeat nil)
  3011. ("fninline" org-footnote-define-inline t)
  3012. ("nofninline" org-footnote-define-inline nil)
  3013. ("fnlocal" org-footnote-section nil)
  3014. ("fnauto" org-footnote-auto-label t)
  3015. ("fnprompt" org-footnote-auto-label nil)
  3016. ("fnconfirm" org-footnote-auto-label confirm)
  3017. ("fnplain" org-footnote-auto-label plain)
  3018. ("constcgs" constants-unit-system cgs)
  3019. ("constSI" constants-unit-system SI)
  3020. ("noptag" org-tag-persistent-alist nil)
  3021. ("hideblocks" org-hide-block-startup t)
  3022. ("nohideblocks" org-hide-block-startup nil))
  3023. "Variable associated with STARTUP options for org-mode.
  3024. Each element is a list of three items: The startup options as written
  3025. in the #+STARTUP line, the corresponding variable, and the value to
  3026. set this variable to if the option is found. An optional forth element PUSH
  3027. means to push this value onto the list in the variable.")
  3028. (defun org-set-regexps-and-options ()
  3029. "Precompute regular expressions for current buffer."
  3030. (when (org-mode-p)
  3031. (org-set-local 'org-todo-kwd-alist nil)
  3032. (org-set-local 'org-todo-key-alist nil)
  3033. (org-set-local 'org-todo-key-trigger nil)
  3034. (org-set-local 'org-todo-keywords-1 nil)
  3035. (org-set-local 'org-done-keywords nil)
  3036. (org-set-local 'org-todo-heads nil)
  3037. (org-set-local 'org-todo-sets nil)
  3038. (org-set-local 'org-todo-log-states nil)
  3039. (org-set-local 'org-file-properties nil)
  3040. (org-set-local 'org-file-tags nil)
  3041. (let ((re (org-make-options-regexp
  3042. '("CATEGORY" "TODO" "COLUMNS"
  3043. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3044. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3045. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3046. (splitre "[ \t]+")
  3047. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3048. tail sep kws1 prio props ftags drawers
  3049. ext-setup-or-nil setup-contents (start 0))
  3050. (save-excursion
  3051. (save-restriction
  3052. (widen)
  3053. (goto-char (point-min))
  3054. (while (or (and ext-setup-or-nil
  3055. (string-match re ext-setup-or-nil start)
  3056. (setq start (match-end 0)))
  3057. (and (setq ext-setup-or-nil nil start 0)
  3058. (re-search-forward re nil t)))
  3059. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3060. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3061. (cond
  3062. ((equal key "CATEGORY")
  3063. (if (string-match "[ \t]+$" value)
  3064. (setq value (replace-match "" t t value)))
  3065. (setq cat value))
  3066. ((member key '("SEQ_TODO" "TODO"))
  3067. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3068. ((equal key "TYP_TODO")
  3069. (push (cons 'type (org-split-string value splitre)) kwds))
  3070. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3071. ;; general TODO-like setup
  3072. (push (cons (intern (downcase (match-string 1 key)))
  3073. (org-split-string value splitre)) kwds))
  3074. ((equal key "TAGS")
  3075. (setq tags (append tags (if tags '("\\n") nil)
  3076. (org-split-string value splitre))))
  3077. ((equal key "COLUMNS")
  3078. (org-set-local 'org-columns-default-format value))
  3079. ((equal key "LINK")
  3080. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3081. (push (cons (match-string 1 value)
  3082. (org-trim (match-string 2 value)))
  3083. links)))
  3084. ((equal key "PRIORITIES")
  3085. (setq prio (org-split-string value " +")))
  3086. ((equal key "PROPERTY")
  3087. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3088. (push (cons (match-string 1 value) (match-string 2 value))
  3089. props)))
  3090. ((equal key "FILETAGS")
  3091. (when (string-match "\\S-" value)
  3092. (setq ftags
  3093. (append
  3094. ftags
  3095. (apply 'append
  3096. (mapcar (lambda (x) (org-split-string x ":"))
  3097. (org-split-string value)))))))
  3098. ((equal key "DRAWERS")
  3099. (setq drawers (org-split-string value splitre)))
  3100. ((equal key "CONSTANTS")
  3101. (setq const (append const (org-split-string value splitre))))
  3102. ((equal key "STARTUP")
  3103. (let ((opts (org-split-string value splitre))
  3104. l var val)
  3105. (while (setq l (pop opts))
  3106. (when (setq l (assoc l org-startup-options))
  3107. (setq var (nth 1 l) val (nth 2 l))
  3108. (if (not (nth 3 l))
  3109. (set (make-local-variable var) val)
  3110. (if (not (listp (symbol-value var)))
  3111. (set (make-local-variable var) nil))
  3112. (set (make-local-variable var) (symbol-value var))
  3113. (add-to-list var val))))))
  3114. ((equal key "ARCHIVE")
  3115. (string-match " *$" value)
  3116. (setq arch (replace-match "" t t value))
  3117. (remove-text-properties 0 (length arch)
  3118. '(face t fontified t) arch))
  3119. ((equal key "SETUPFILE")
  3120. (setq setup-contents (org-file-contents
  3121. (expand-file-name
  3122. (org-remove-double-quotes value))
  3123. 'noerror))
  3124. (if (not ext-setup-or-nil)
  3125. (setq ext-setup-or-nil setup-contents start 0)
  3126. (setq ext-setup-or-nil
  3127. (concat (substring ext-setup-or-nil 0 start)
  3128. "\n" setup-contents "\n"
  3129. (substring ext-setup-or-nil start)))))
  3130. ))))
  3131. (when cat
  3132. (org-set-local 'org-category (intern cat))
  3133. (push (cons "CATEGORY" cat) props))
  3134. (when prio
  3135. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3136. (setq prio (mapcar 'string-to-char prio))
  3137. (org-set-local 'org-highest-priority (nth 0 prio))
  3138. (org-set-local 'org-lowest-priority (nth 1 prio))
  3139. (org-set-local 'org-default-priority (nth 2 prio)))
  3140. (and props (org-set-local 'org-file-properties (nreverse props)))
  3141. (and ftags (org-set-local 'org-file-tags
  3142. (mapcar 'org-add-prop-inherited ftags)))
  3143. (and drawers (org-set-local 'org-drawers drawers))
  3144. (and arch (org-set-local 'org-archive-location arch))
  3145. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3146. ;; Process the TODO keywords
  3147. (unless kwds
  3148. ;; Use the global values as if they had been given locally.
  3149. (setq kwds (default-value 'org-todo-keywords))
  3150. (if (stringp (car kwds))
  3151. (setq kwds (list (cons org-todo-interpretation
  3152. (default-value 'org-todo-keywords)))))
  3153. (setq kwds (reverse kwds)))
  3154. (setq kwds (nreverse kwds))
  3155. (let (inter kws kw)
  3156. (while (setq kws (pop kwds))
  3157. (let ((kws (or
  3158. (run-hook-with-args-until-success
  3159. 'org-todo-setup-filter-hook kws)
  3160. kws)))
  3161. (setq inter (pop kws) sep (member "|" kws)
  3162. kws0 (delete "|" (copy-sequence kws))
  3163. kwsa nil
  3164. kws1 (mapcar
  3165. (lambda (x)
  3166. ;; 1 2
  3167. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3168. (progn
  3169. (setq kw (match-string 1 x)
  3170. key (and (match-end 2) (match-string 2 x))
  3171. log (org-extract-log-state-settings x))
  3172. (push (cons kw (and key (string-to-char key))) kwsa)
  3173. (and log (push log org-todo-log-states))
  3174. kw)
  3175. (error "Invalid TODO keyword %s" x)))
  3176. kws0)
  3177. kwsa (if kwsa (append '((:startgroup))
  3178. (nreverse kwsa)
  3179. '((:endgroup))))
  3180. hw (car kws1)
  3181. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3182. tail (list inter hw (car dws) (org-last dws))))
  3183. (add-to-list 'org-todo-heads hw 'append)
  3184. (push kws1 org-todo-sets)
  3185. (setq org-done-keywords (append org-done-keywords dws nil))
  3186. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3187. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3188. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3189. (setq org-todo-sets (nreverse org-todo-sets)
  3190. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3191. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3192. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3193. ;; Process the constants
  3194. (when const
  3195. (let (e cst)
  3196. (while (setq e (pop const))
  3197. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3198. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3199. (setq org-table-formula-constants-local cst)))
  3200. ;; Process the tags.
  3201. (when tags
  3202. (let (e tgs)
  3203. (while (setq e (pop tags))
  3204. (cond
  3205. ((equal e "{") (push '(:startgroup) tgs))
  3206. ((equal e "}") (push '(:endgroup) tgs))
  3207. ((equal e "\\n") (push '(:newline) tgs))
  3208. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3209. (push (cons (match-string 1 e)
  3210. (string-to-char (match-string 2 e)))
  3211. tgs))
  3212. (t (push (list e) tgs))))
  3213. (org-set-local 'org-tag-alist nil)
  3214. (while (setq e (pop tgs))
  3215. (or (and (stringp (car e))
  3216. (assoc (car e) org-tag-alist))
  3217. (push e org-tag-alist)))))
  3218. ;; Compute the regular expressions and other local variables
  3219. (if (not org-done-keywords)
  3220. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3221. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3222. (length org-scheduled-string)
  3223. (length org-clock-string)
  3224. (length org-closed-string)))
  3225. org-drawer-regexp
  3226. (concat "^[ \t]*:\\("
  3227. (mapconcat 'regexp-quote org-drawers "\\|")
  3228. "\\):[ \t]*$")
  3229. org-not-done-keywords
  3230. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3231. org-todo-regexp
  3232. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3233. "\\|") "\\)\\>")
  3234. org-not-done-regexp
  3235. (concat "\\<\\("
  3236. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3237. "\\)\\>")
  3238. org-not-done-heading-regexp
  3239. (concat "^\\(\\*+\\)[ \t]+\\("
  3240. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3241. "\\)\\>")
  3242. org-todo-line-regexp
  3243. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3244. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3245. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3246. org-complex-heading-regexp
  3247. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3248. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3249. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3250. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3251. org-nl-done-regexp
  3252. (concat "\n\\*+[ \t]+"
  3253. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3254. "\\)" "\\>")
  3255. org-todo-line-tags-regexp
  3256. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3257. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3258. (org-re
  3259. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3260. org-looking-at-done-regexp
  3261. (concat "^" "\\(?:"
  3262. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3263. "\\>")
  3264. org-deadline-regexp (concat "\\<" org-deadline-string)
  3265. org-deadline-time-regexp
  3266. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3267. org-deadline-line-regexp
  3268. (concat "\\<\\(" org-deadline-string "\\).*")
  3269. org-scheduled-regexp
  3270. (concat "\\<" org-scheduled-string)
  3271. org-scheduled-time-regexp
  3272. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3273. org-closed-time-regexp
  3274. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3275. org-keyword-time-regexp
  3276. (concat "\\<\\(" org-scheduled-string
  3277. "\\|" org-deadline-string
  3278. "\\|" org-closed-string
  3279. "\\|" org-clock-string "\\)"
  3280. " *[[<]\\([^]>]+\\)[]>]")
  3281. org-keyword-time-not-clock-regexp
  3282. (concat "\\<\\(" org-scheduled-string
  3283. "\\|" org-deadline-string
  3284. "\\|" org-closed-string
  3285. "\\)"
  3286. " *[[<]\\([^]>]+\\)[]>]")
  3287. org-maybe-keyword-time-regexp
  3288. (concat "\\(\\<\\(" org-scheduled-string
  3289. "\\|" org-deadline-string
  3290. "\\|" org-closed-string
  3291. "\\|" org-clock-string "\\)\\)?"
  3292. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3293. org-planning-or-clock-line-re
  3294. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3295. "\\|" org-deadline-string
  3296. "\\|" org-closed-string "\\|" org-clock-string
  3297. "\\)\\>\\)")
  3298. )
  3299. (org-compute-latex-and-specials-regexp)
  3300. (org-set-font-lock-defaults))))
  3301. (defun org-file-contents (file &optional noerror)
  3302. "Return the contents of FILE, as a string."
  3303. (if (or (not file)
  3304. (not (file-readable-p file)))
  3305. (if noerror
  3306. (progn
  3307. (message "Cannot read file %s" file)
  3308. (ding) (sit-for 2)
  3309. "")
  3310. (error "Cannot read file %s" file))
  3311. (with-temp-buffer
  3312. (insert-file-contents file)
  3313. (buffer-string))))
  3314. (defun org-extract-log-state-settings (x)
  3315. "Extract the log state setting from a TODO keyword string.
  3316. This will extract info from a string like \"WAIT(w@/!)\"."
  3317. (let (kw key log1 log2)
  3318. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3319. (setq kw (match-string 1 x)
  3320. key (and (match-end 2) (match-string 2 x))
  3321. log1 (and (match-end 3) (match-string 3 x))
  3322. log2 (and (match-end 4) (match-string 4 x)))
  3323. (and (or log1 log2)
  3324. (list kw
  3325. (and log1 (if (equal log1 "!") 'time 'note))
  3326. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3327. (defun org-remove-keyword-keys (list)
  3328. "Remove a pair of parenthesis at the end of each string in LIST."
  3329. (mapcar (lambda (x)
  3330. (if (string-match "(.*)$" x)
  3331. (substring x 0 (match-beginning 0))
  3332. x))
  3333. list))
  3334. ;; FIXME: this could be done much better, using second characters etc.
  3335. (defun org-assign-fast-keys (alist)
  3336. "Assign fast keys to a keyword-key alist.
  3337. Respect keys that are already there."
  3338. (let (new e k c c1 c2 (char ?a))
  3339. (while (setq e (pop alist))
  3340. (cond
  3341. ((equal e '(:startgroup)) (push e new))
  3342. ((equal e '(:endgroup)) (push e new))
  3343. ((equal e '(:newline)) (push e new))
  3344. (t
  3345. (setq k (car e) c2 nil)
  3346. (if (cdr e)
  3347. (setq c (cdr e))
  3348. ;; automatically assign a character.
  3349. (setq c1 (string-to-char
  3350. (downcase (substring
  3351. k (if (= (string-to-char k) ?@) 1 0)))))
  3352. (if (or (rassoc c1 new) (rassoc c1 alist))
  3353. (while (or (rassoc char new) (rassoc char alist))
  3354. (setq char (1+ char)))
  3355. (setq c2 c1))
  3356. (setq c (or c2 char)))
  3357. (push (cons k c) new))))
  3358. (nreverse new)))
  3359. ;;; Some variables used in various places
  3360. (defvar org-window-configuration nil
  3361. "Used in various places to store a window configuration.")
  3362. (defvar org-finish-function nil
  3363. "Function to be called when `C-c C-c' is used.
  3364. This is for getting out of special buffers like remember.")
  3365. ;; FIXME: Occasionally check by commenting these, to make sure
  3366. ;; no other functions uses these, forgetting to let-bind them.
  3367. (defvar entry)
  3368. (defvar state)
  3369. (defvar last-state)
  3370. (defvar date)
  3371. (defvar description)
  3372. ;; Defined somewhere in this file, but used before definition.
  3373. (defvar org-html-entities)
  3374. (defvar org-struct-menu)
  3375. (defvar org-org-menu)
  3376. (defvar org-tbl-menu)
  3377. (defvar org-agenda-keymap)
  3378. ;;;; Define the Org-mode
  3379. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3380. (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."))
  3381. ;; We use a before-change function to check if a table might need
  3382. ;; an update.
  3383. (defvar org-table-may-need-update t
  3384. "Indicates that a table might need an update.
  3385. This variable is set by `org-before-change-function'.
  3386. `org-table-align' sets it back to nil.")
  3387. (defun org-before-change-function (beg end)
  3388. "Every change indicates that a table might need an update."
  3389. (setq org-table-may-need-update t))
  3390. (defvar org-mode-map)
  3391. (defvar org-mode-hook nil
  3392. "Mode hook for Org-mode, run after the mode was turned on.")
  3393. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3394. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3395. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3396. (defvar org-table-buffer-is-an nil)
  3397. (defconst org-outline-regexp "\\*+ ")
  3398. ;;;###autoload
  3399. (define-derived-mode org-mode outline-mode "Org"
  3400. "Outline-based notes management and organizer, alias
  3401. \"Carsten's outline-mode for keeping track of everything.\"
  3402. Org-mode develops organizational tasks around a NOTES file which
  3403. contains information about projects as plain text. Org-mode is
  3404. implemented on top of outline-mode, which is ideal to keep the content
  3405. of large files well structured. It supports ToDo items, deadlines and
  3406. time stamps, which magically appear in the diary listing of the Emacs
  3407. calendar. Tables are easily created with a built-in table editor.
  3408. Plain text URL-like links connect to websites, emails (VM), Usenet
  3409. messages (Gnus), BBDB entries, and any files related to the project.
  3410. For printing and sharing of notes, an Org-mode file (or a part of it)
  3411. can be exported as a structured ASCII or HTML file.
  3412. The following commands are available:
  3413. \\{org-mode-map}"
  3414. ;; Get rid of Outline menus, they are not needed
  3415. ;; Need to do this here because define-derived-mode sets up
  3416. ;; the keymap so late. Still, it is a waste to call this each time
  3417. ;; we switch another buffer into org-mode.
  3418. (if (featurep 'xemacs)
  3419. (when (boundp 'outline-mode-menu-heading)
  3420. ;; Assume this is Greg's port, it used easymenu
  3421. (easy-menu-remove outline-mode-menu-heading)
  3422. (easy-menu-remove outline-mode-menu-show)
  3423. (easy-menu-remove outline-mode-menu-hide))
  3424. (define-key org-mode-map [menu-bar headings] 'undefined)
  3425. (define-key org-mode-map [menu-bar hide] 'undefined)
  3426. (define-key org-mode-map [menu-bar show] 'undefined))
  3427. (org-load-modules-maybe)
  3428. (easy-menu-add org-org-menu)
  3429. (easy-menu-add org-tbl-menu)
  3430. (org-install-agenda-files-menu)
  3431. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3432. (org-add-to-invisibility-spec '(org-cwidth))
  3433. (org-add-to-invisibility-spec '(org-hide-block . t))
  3434. (when (featurep 'xemacs)
  3435. (org-set-local 'line-move-ignore-invisible t))
  3436. (org-set-local 'outline-regexp org-outline-regexp)
  3437. (org-set-local 'outline-level 'org-outline-level)
  3438. (when (and org-ellipsis
  3439. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3440. (fboundp 'make-glyph-code))
  3441. (unless org-display-table
  3442. (setq org-display-table (make-display-table)))
  3443. (set-display-table-slot
  3444. org-display-table 4
  3445. (vconcat (mapcar
  3446. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3447. org-ellipsis)))
  3448. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3449. (setq buffer-display-table org-display-table))
  3450. (org-set-regexps-and-options)
  3451. (when (and org-tag-faces (not org-tags-special-faces-re))
  3452. ;; tag faces set outside customize.... force initialization.
  3453. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3454. ;; Calc embedded
  3455. (org-set-local 'calc-embedded-open-mode "# ")
  3456. (modify-syntax-entry ?# "<")
  3457. (modify-syntax-entry ?@ "w")
  3458. (if org-startup-truncated (setq truncate-lines t))
  3459. (org-set-local 'font-lock-unfontify-region-function
  3460. 'org-unfontify-region)
  3461. ;; Activate before-change-function
  3462. (org-set-local 'org-table-may-need-update t)
  3463. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3464. 'local)
  3465. ;; Check for running clock before killing a buffer
  3466. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3467. ;; Paragraphs and auto-filling
  3468. (org-set-autofill-regexps)
  3469. (setq indent-line-function 'org-indent-line-function)
  3470. (org-update-radio-target-regexp)
  3471. ;; Make sure dependence stuff works reliably, even for users who set it
  3472. ;; too late :-(
  3473. (if org-enforce-todo-dependencies
  3474. (add-hook 'org-blocker-hook
  3475. 'org-block-todo-from-children-or-siblings-or-parent)
  3476. (remove-hook 'org-blocker-hook
  3477. 'org-block-todo-from-children-or-siblings-or-parent))
  3478. (if org-enforce-todo-checkbox-dependencies
  3479. (add-hook 'org-blocker-hook
  3480. 'org-block-todo-from-checkboxes)
  3481. (remove-hook 'org-blocker-hook
  3482. 'org-block-todo-from-checkboxes))
  3483. ;; Comment characters
  3484. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3485. (org-set-local 'comment-padding " ")
  3486. ;; Align options lines
  3487. (org-set-local
  3488. 'align-mode-rules-list
  3489. '((org-in-buffer-settings
  3490. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3491. (modes . '(org-mode)))))
  3492. ;; Imenu
  3493. (org-set-local 'imenu-create-index-function
  3494. 'org-imenu-get-tree)
  3495. ;; Make isearch reveal context
  3496. (if (or (featurep 'xemacs)
  3497. (not (boundp 'outline-isearch-open-invisible-function)))
  3498. ;; Emacs 21 and XEmacs make use of the hook
  3499. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3500. ;; Emacs 22 deals with this through a special variable
  3501. (org-set-local 'outline-isearch-open-invisible-function
  3502. (lambda (&rest ignore) (org-show-context 'isearch))))
  3503. ;; If empty file that did not turn on org-mode automatically, make it to.
  3504. (if (and org-insert-mode-line-in-empty-file
  3505. (interactive-p)
  3506. (= (point-min) (point-max)))
  3507. (insert "# -*- mode: org -*-\n\n"))
  3508. (unless org-inhibit-startup
  3509. (when org-startup-align-all-tables
  3510. (let ((bmp (buffer-modified-p)))
  3511. (org-table-map-tables 'org-table-align)
  3512. (set-buffer-modified-p bmp)))
  3513. (org-set-startup-visibility)))
  3514. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3515. (defun org-current-time ()
  3516. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3517. (if (> (car org-time-stamp-rounding-minutes) 1)
  3518. (let ((r (car org-time-stamp-rounding-minutes))
  3519. (time (decode-time)))
  3520. (apply 'encode-time
  3521. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3522. (nthcdr 2 time))))
  3523. (current-time)))
  3524. ;;;; Font-Lock stuff, including the activators
  3525. (defvar org-mouse-map (make-sparse-keymap))
  3526. (org-defkey org-mouse-map
  3527. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3528. (org-defkey org-mouse-map
  3529. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3530. (when org-mouse-1-follows-link
  3531. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3532. (when org-tab-follows-link
  3533. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3534. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3535. (require 'font-lock)
  3536. (defconst org-non-link-chars "]\t\n\r<>")
  3537. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3538. "shell" "elisp"))
  3539. (defvar org-link-types-re nil
  3540. "Matches a link that has a url-like prefix like \"http:\"")
  3541. (defvar org-link-re-with-space nil
  3542. "Matches a link with spaces, optional angular brackets around it.")
  3543. (defvar org-link-re-with-space2 nil
  3544. "Matches a link with spaces, optional angular brackets around it.")
  3545. (defvar org-link-re-with-space3 nil
  3546. "Matches a link with spaces, only for internal part in bracket links.")
  3547. (defvar org-angle-link-re nil
  3548. "Matches link with angular brackets, spaces are allowed.")
  3549. (defvar org-plain-link-re nil
  3550. "Matches plain link, without spaces.")
  3551. (defvar org-bracket-link-regexp nil
  3552. "Matches a link in double brackets.")
  3553. (defvar org-bracket-link-analytic-regexp nil
  3554. "Regular expression used to analyze links.
  3555. Here is what the match groups contain after a match:
  3556. 1: http:
  3557. 2: http
  3558. 3: path
  3559. 4: [desc]
  3560. 5: desc")
  3561. (defvar org-bracket-link-analytic-regexp++ nil
  3562. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3563. (defvar org-any-link-re nil
  3564. "Regular expression matching any link.")
  3565. (defun org-make-link-regexps ()
  3566. "Update the link regular expressions.
  3567. This should be called after the variable `org-link-types' has changed."
  3568. (setq org-link-types-re
  3569. (concat
  3570. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3571. org-link-re-with-space
  3572. (concat
  3573. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3574. "\\([^" org-non-link-chars " ]"
  3575. "[^" org-non-link-chars "]*"
  3576. "[^" org-non-link-chars " ]\\)>?")
  3577. org-link-re-with-space2
  3578. (concat
  3579. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3580. "\\([^" org-non-link-chars " ]"
  3581. "[^\t\n\r]*"
  3582. "[^" org-non-link-chars " ]\\)>?")
  3583. org-link-re-with-space3
  3584. (concat
  3585. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3586. "\\([^" org-non-link-chars " ]"
  3587. "[^\t\n\r]*\\)")
  3588. org-angle-link-re
  3589. (concat
  3590. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3591. "\\([^" org-non-link-chars " ]"
  3592. "[^" org-non-link-chars "]*"
  3593. "\\)>")
  3594. org-plain-link-re
  3595. (concat
  3596. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3597. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3598. org-bracket-link-regexp
  3599. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3600. org-bracket-link-analytic-regexp
  3601. (concat
  3602. "\\[\\["
  3603. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3604. "\\([^]]+\\)"
  3605. "\\]"
  3606. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3607. "\\]")
  3608. org-bracket-link-analytic-regexp++
  3609. (concat
  3610. "\\[\\["
  3611. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3612. "\\([^]]+\\)"
  3613. "\\]"
  3614. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3615. "\\]")
  3616. org-any-link-re
  3617. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3618. org-angle-link-re "\\)\\|\\("
  3619. org-plain-link-re "\\)")))
  3620. (org-make-link-regexps)
  3621. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3622. "Regular expression for fast time stamp matching.")
  3623. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3624. "Regular expression for fast time stamp matching.")
  3625. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3626. "Regular expression matching time strings for analysis.
  3627. This one does not require the space after the date, so it can be used
  3628. on a string that terminates immediately after the date.")
  3629. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3630. "Regular expression matching time strings for analysis.")
  3631. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3632. "Regular expression matching time stamps, with groups.")
  3633. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3634. "Regular expression matching time stamps (also [..]), with groups.")
  3635. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3636. "Regular expression matching a time stamp range.")
  3637. (defconst org-tr-regexp-both
  3638. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3639. "Regular expression matching a time stamp range.")
  3640. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3641. org-ts-regexp "\\)?")
  3642. "Regular expression matching a time stamp or time stamp range.")
  3643. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3644. org-ts-regexp-both "\\)?")
  3645. "Regular expression matching a time stamp or time stamp range.
  3646. The time stamps may be either active or inactive.")
  3647. (defvar org-emph-face nil)
  3648. (defun org-do-emphasis-faces (limit)
  3649. "Run through the buffer and add overlays to links."
  3650. (let (rtn a)
  3651. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3652. (if (not (= (char-after (match-beginning 3))
  3653. (char-after (match-beginning 4))))
  3654. (progn
  3655. (setq rtn t)
  3656. (setq a (assoc (match-string 3) org-emphasis-alist))
  3657. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3658. 'face
  3659. (nth 1 a))
  3660. (and (nth 4 a)
  3661. (org-remove-flyspell-overlays-in
  3662. (match-beginning 0) (match-end 0)))
  3663. (add-text-properties (match-beginning 2) (match-end 2)
  3664. '(font-lock-multiline t))
  3665. (when org-hide-emphasis-markers
  3666. (add-text-properties (match-end 4) (match-beginning 5)
  3667. '(invisible org-link))
  3668. (add-text-properties (match-beginning 3) (match-end 3)
  3669. '(invisible org-link)))))
  3670. (backward-char 1))
  3671. rtn))
  3672. (defun org-emphasize (&optional char)
  3673. "Insert or change an emphasis, i.e. a font like bold or italic.
  3674. If there is an active region, change that region to a new emphasis.
  3675. If there is no region, just insert the marker characters and position
  3676. the cursor between them.
  3677. CHAR should be either the marker character, or the first character of the
  3678. HTML tag associated with that emphasis. If CHAR is a space, the means
  3679. to remove the emphasis of the selected region.
  3680. If char is not given (for example in an interactive call) it
  3681. will be prompted for."
  3682. (interactive)
  3683. (let ((eal org-emphasis-alist) e det
  3684. (erc org-emphasis-regexp-components)
  3685. (prompt "")
  3686. (string "") beg end move tag c s)
  3687. (if (org-region-active-p)
  3688. (setq beg (region-beginning) end (region-end)
  3689. string (buffer-substring beg end))
  3690. (setq move t))
  3691. (while (setq e (pop eal))
  3692. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3693. c (aref tag 0))
  3694. (push (cons c (string-to-char (car e))) det)
  3695. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3696. (substring tag 1)))))
  3697. (setq det (nreverse det))
  3698. (unless char
  3699. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3700. (setq char (read-char-exclusive)))
  3701. (setq char (or (cdr (assoc char det)) char))
  3702. (if (equal char ?\ )
  3703. (setq s "" move nil)
  3704. (unless (assoc (char-to-string char) org-emphasis-alist)
  3705. (error "No such emphasis marker: \"%c\"" char))
  3706. (setq s (char-to-string char)))
  3707. (while (and (> (length string) 1)
  3708. (equal (substring string 0 1) (substring string -1))
  3709. (assoc (substring string 0 1) org-emphasis-alist))
  3710. (setq string (substring string 1 -1)))
  3711. (setq string (concat s string s))
  3712. (if beg (delete-region beg end))
  3713. (unless (or (bolp)
  3714. (string-match (concat "[" (nth 0 erc) "\n]")
  3715. (char-to-string (char-before (point)))))
  3716. (insert " "))
  3717. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3718. (char-to-string (char-after (point))))
  3719. (insert " ") (backward-char 1))
  3720. (insert string)
  3721. (and move (backward-char 1))))
  3722. (defconst org-nonsticky-props
  3723. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3724. (defsubst org-rear-nonsticky-at (pos)
  3725. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3726. (defun org-activate-plain-links (limit)
  3727. "Run through the buffer and add overlays to links."
  3728. (catch 'exit
  3729. (let (f)
  3730. (while (re-search-forward org-plain-link-re limit t)
  3731. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3732. (setq f (get-text-property (match-beginning 0) 'face))
  3733. (if (or (eq f 'org-tag)
  3734. (and (listp f) (memq 'org-tag f)))
  3735. nil
  3736. (add-text-properties (match-beginning 0) (match-end 0)
  3737. (list 'mouse-face 'highlight
  3738. 'keymap org-mouse-map))
  3739. (org-rear-nonsticky-at (match-end 0))
  3740. (throw 'exit t))))))
  3741. (defun org-activate-code (limit)
  3742. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3743. (progn
  3744. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3745. (remove-text-properties (match-beginning 0) (match-end 0)
  3746. '(display t invisible t intangible t))
  3747. t)))
  3748. (defvar org-protecting-blocks
  3749. '("src" "example" "latex" "ascii" "html" "docbook")
  3750. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3751. This is needed for font-lock setup.")
  3752. (defun org-fontify-meta-lines-and-blocks (limit)
  3753. "Fontify #+ lines and blocks, in the correct ways."
  3754. (let ((case-fold-search t))
  3755. (if (re-search-forward
  3756. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3757. limit t)
  3758. (let ((beg (match-beginning 0))
  3759. (beg1 (line-beginning-position 2))
  3760. (dc1 (downcase (match-string 2)))
  3761. (dc3 (downcase (match-string 3)))
  3762. end end1 quoting)
  3763. (cond
  3764. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3765. ;; a single line of backend-specific content
  3766. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3767. (remove-text-properties (match-beginning 0) (match-end 0)
  3768. '(display t invisible t intangible t))
  3769. (add-text-properties (match-beginning 1) (match-end 3)
  3770. '(font-lock-fontified t face org-meta-line))
  3771. (add-text-properties (match-beginning 6) (match-end 6)
  3772. '(font-lock-fontified t face org-block))
  3773. t)
  3774. ((and (match-end 4) (equal dc3 "begin"))
  3775. ;; Truely a block
  3776. (setq quoting (member (downcase (match-string 5))
  3777. org-protecting-blocks))
  3778. (when (re-search-forward
  3779. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3780. nil t) ;; on purpose, we look further than LIMIT
  3781. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3782. (when quoting
  3783. (remove-text-properties beg end
  3784. '(display t invisible t intangible t)))
  3785. (add-text-properties
  3786. beg end
  3787. '(font-lock-fontified t font-lock-multiline t))
  3788. (add-text-properties beg beg1 '(face org-meta-line))
  3789. (add-text-properties end1 end '(face org-meta-line))
  3790. (when quoting
  3791. (add-text-properties beg1 end1 '(face org-block)))
  3792. t))
  3793. ((not (member (char-after beg) '(?\ ?\t)))
  3794. ;; just any other in-buffer setting, but not indented
  3795. (add-text-properties
  3796. beg (match-end 0)
  3797. '(font-lock-fontified t face org-meta-line))
  3798. t)
  3799. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:"))
  3800. (and (match-end 4) (equal dc3 "attr")))
  3801. (add-text-properties
  3802. beg (match-end 0)
  3803. '(font-lock-fontified t face org-meta-line))
  3804. t)
  3805. (t nil))))))
  3806. (defun org-activate-angle-links (limit)
  3807. "Run through the buffer and add overlays to links."
  3808. (if (re-search-forward org-angle-link-re limit t)
  3809. (progn
  3810. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3811. (add-text-properties (match-beginning 0) (match-end 0)
  3812. (list 'mouse-face 'highlight
  3813. 'keymap org-mouse-map))
  3814. (org-rear-nonsticky-at (match-end 0))
  3815. t)))
  3816. (defun org-activate-footnote-links (limit)
  3817. "Run through the buffer and add overlays to links."
  3818. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3819. limit t)
  3820. (progn
  3821. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3822. (add-text-properties (match-beginning 2) (match-end 2)
  3823. (list 'mouse-face 'highlight
  3824. 'keymap org-mouse-map
  3825. 'help-echo
  3826. (if (= (point-at-bol) (match-beginning 2))
  3827. "Footnote definition"
  3828. "Footnote reference")
  3829. ))
  3830. (org-rear-nonsticky-at (match-end 2))
  3831. t)))
  3832. (defun org-activate-bracket-links (limit)
  3833. "Run through the buffer and add overlays to bracketed links."
  3834. (if (re-search-forward org-bracket-link-regexp limit t)
  3835. (let* ((help (concat "LINK: "
  3836. (org-match-string-no-properties 1)))
  3837. ;; FIXME: above we should remove the escapes.
  3838. ;; but that requires another match, protecting match data,
  3839. ;; a lot of overhead for font-lock.
  3840. (ip (org-maybe-intangible
  3841. (list 'invisible 'org-link
  3842. 'keymap org-mouse-map 'mouse-face 'highlight
  3843. 'font-lock-multiline t 'help-echo help)))
  3844. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3845. 'font-lock-multiline t 'help-echo help)))
  3846. ;; We need to remove the invisible property here. Table narrowing
  3847. ;; may have made some of this invisible.
  3848. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3849. (remove-text-properties (match-beginning 0) (match-end 0)
  3850. '(invisible nil))
  3851. (if (match-end 3)
  3852. (progn
  3853. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3854. (org-rear-nonsticky-at (match-beginning 3))
  3855. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3856. (org-rear-nonsticky-at (match-end 3))
  3857. (add-text-properties (match-end 3) (match-end 0) ip)
  3858. (org-rear-nonsticky-at (match-end 0)))
  3859. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3860. (org-rear-nonsticky-at (match-beginning 1))
  3861. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3862. (org-rear-nonsticky-at (match-end 1))
  3863. (add-text-properties (match-end 1) (match-end 0) ip)
  3864. (org-rear-nonsticky-at (match-end 0)))
  3865. t)))
  3866. (defun org-activate-dates (limit)
  3867. "Run through the buffer and add overlays to dates."
  3868. (if (re-search-forward org-tsr-regexp-both limit t)
  3869. (progn
  3870. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3871. (add-text-properties (match-beginning 0) (match-end 0)
  3872. (list 'mouse-face 'highlight
  3873. 'keymap org-mouse-map))
  3874. (org-rear-nonsticky-at (match-end 0))
  3875. (when org-display-custom-times
  3876. (if (match-end 3)
  3877. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3878. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3879. t)))
  3880. (defvar org-target-link-regexp nil
  3881. "Regular expression matching radio targets in plain text.")
  3882. (make-variable-buffer-local 'org-target-link-regexp)
  3883. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3884. "Regular expression matching a link target.")
  3885. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3886. "Regular expression matching a radio target.")
  3887. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3888. "Regular expression matching any target.")
  3889. (defun org-activate-target-links (limit)
  3890. "Run through the buffer and add overlays to target matches."
  3891. (when org-target-link-regexp
  3892. (let ((case-fold-search t))
  3893. (if (re-search-forward org-target-link-regexp limit t)
  3894. (progn
  3895. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3896. (add-text-properties (match-beginning 0) (match-end 0)
  3897. (list 'mouse-face 'highlight
  3898. 'keymap org-mouse-map
  3899. 'help-echo "Radio target link"
  3900. 'org-linked-text t))
  3901. (org-rear-nonsticky-at (match-end 0))
  3902. t)))))
  3903. (defun org-update-radio-target-regexp ()
  3904. "Find all radio targets in this file and update the regular expression."
  3905. (interactive)
  3906. (when (memq 'radio org-activate-links)
  3907. (setq org-target-link-regexp
  3908. (org-make-target-link-regexp (org-all-targets 'radio)))
  3909. (org-restart-font-lock)))
  3910. (defun org-hide-wide-columns (limit)
  3911. (let (s e)
  3912. (setq s (text-property-any (point) (or limit (point-max))
  3913. 'org-cwidth t))
  3914. (when s
  3915. (setq e (next-single-property-change s 'org-cwidth))
  3916. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3917. (goto-char e)
  3918. t)))
  3919. (defvar org-latex-and-specials-regexp nil
  3920. "Regular expression for highlighting export special stuff.")
  3921. (defvar org-match-substring-regexp)
  3922. (defvar org-match-substring-with-braces-regexp)
  3923. (defvar org-export-html-special-string-regexps)
  3924. (defun org-compute-latex-and-specials-regexp ()
  3925. "Compute regular expression for stuff treated specially by exporters."
  3926. (if (not org-highlight-latex-fragments-and-specials)
  3927. (org-set-local 'org-latex-and-specials-regexp nil)
  3928. (require 'org-exp)
  3929. (let*
  3930. ((matchers (plist-get org-format-latex-options :matchers))
  3931. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3932. org-latex-regexps)))
  3933. (options (org-combine-plists (org-default-export-plist)
  3934. (org-infile-export-plist)))
  3935. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3936. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3937. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3938. (org-export-html-expand (plist-get options :expand-quoted-html))
  3939. (org-export-with-special-strings (plist-get options :special-strings))
  3940. (re-sub
  3941. (cond
  3942. ((equal org-export-with-sub-superscripts '{})
  3943. (list org-match-substring-with-braces-regexp))
  3944. (org-export-with-sub-superscripts
  3945. (list org-match-substring-regexp))
  3946. (t nil)))
  3947. (re-latex
  3948. (if org-export-with-LaTeX-fragments
  3949. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3950. (re-macros
  3951. (if org-export-with-TeX-macros
  3952. (list (concat "\\\\"
  3953. (regexp-opt
  3954. (append (mapcar 'car org-html-entities)
  3955. (if (boundp 'org-latex-entities)
  3956. (mapcar (lambda (x)
  3957. (or (car-safe x) x))
  3958. org-latex-entities)
  3959. nil))
  3960. 'words))) ; FIXME
  3961. ))
  3962. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3963. (re-special (if org-export-with-special-strings
  3964. (mapcar (lambda (x) (car x))
  3965. org-export-html-special-string-regexps)))
  3966. (re-rest
  3967. (delq nil
  3968. (list
  3969. (if org-export-html-expand "@<[^>\n]+>")
  3970. ))))
  3971. (org-set-local
  3972. 'org-latex-and-specials-regexp
  3973. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3974. re-rest) "\\|")))))
  3975. (defun org-do-latex-and-special-faces (limit)
  3976. "Run through the buffer and add overlays to links."
  3977. (when org-latex-and-specials-regexp
  3978. (let (rtn d)
  3979. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3980. limit t))
  3981. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3982. 'face))
  3983. '(org-code org-verbatim underline)))
  3984. (progn
  3985. (setq rtn t
  3986. d (cond ((member (char-after (1+ (match-beginning 0)))
  3987. '(?_ ?^)) 1)
  3988. (t 0)))
  3989. (font-lock-prepend-text-property
  3990. (+ d (match-beginning 0)) (match-end 0)
  3991. 'face 'org-latex-and-export-specials)
  3992. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3993. '(font-lock-multiline t)))))
  3994. rtn)))
  3995. (defun org-restart-font-lock ()
  3996. "Restart font-lock-mode, to force refontification."
  3997. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3998. (font-lock-mode -1)
  3999. (font-lock-mode 1)))
  4000. (defun org-all-targets (&optional radio)
  4001. "Return a list of all targets in this file.
  4002. With optional argument RADIO, only find radio targets."
  4003. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4004. rtn)
  4005. (save-excursion
  4006. (goto-char (point-min))
  4007. (while (re-search-forward re nil t)
  4008. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4009. rtn)))
  4010. (defun org-make-target-link-regexp (targets)
  4011. "Make regular expression matching all strings in TARGETS.
  4012. The regular expression finds the targets also if there is a line break
  4013. between words."
  4014. (and targets
  4015. (concat
  4016. "\\<\\("
  4017. (mapconcat
  4018. (lambda (x)
  4019. (while (string-match " +" x)
  4020. (setq x (replace-match "\\s-+" t t x)))
  4021. x)
  4022. targets
  4023. "\\|")
  4024. "\\)\\>")))
  4025. (defun org-activate-tags (limit)
  4026. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4027. (progn
  4028. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4029. (add-text-properties (match-beginning 1) (match-end 1)
  4030. (list 'mouse-face 'highlight
  4031. 'keymap org-mouse-map))
  4032. (org-rear-nonsticky-at (match-end 1))
  4033. t)))
  4034. (defun org-outline-level ()
  4035. (save-excursion
  4036. (looking-at outline-regexp)
  4037. (if (match-beginning 1)
  4038. (+ (org-get-string-indentation (match-string 1)) 1000)
  4039. (1- (- (match-end 0) (match-beginning 0))))))
  4040. (defvar org-font-lock-keywords nil)
  4041. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4042. "Regular expression matching a property line.")
  4043. (defvar org-font-lock-hook nil
  4044. "Functions to be called for special font lock stuff.")
  4045. (defun org-font-lock-hook (limit)
  4046. (run-hook-with-args 'org-font-lock-hook limit))
  4047. (defun org-set-font-lock-defaults ()
  4048. (let* ((em org-fontify-emphasized-text)
  4049. (lk org-activate-links)
  4050. (org-font-lock-extra-keywords
  4051. (list
  4052. ;; Call the hook
  4053. '(org-font-lock-hook)
  4054. ;; Headlines
  4055. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4056. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4057. ;; Table lines
  4058. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4059. (1 'org-table t))
  4060. ;; Table internals
  4061. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4062. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4063. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4064. ;; Drawers
  4065. (list org-drawer-regexp '(0 'org-special-keyword t))
  4066. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4067. ;; Properties
  4068. (list org-property-re
  4069. '(1 'org-special-keyword t)
  4070. '(3 'org-property-value t))
  4071. (if org-format-transports-properties-p
  4072. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4073. ;; Links
  4074. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4075. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4076. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4077. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4078. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4079. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4080. (if (memq 'footnote lk) '(org-activate-footnote-links
  4081. (2 'org-footnote t)))
  4082. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4083. '(org-hide-wide-columns (0 nil append))
  4084. ;; TODO lines
  4085. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4086. '(1 (org-get-todo-face 1) t))
  4087. ;; DONE
  4088. (if org-fontify-done-headline
  4089. (list (concat "^[*]+ +\\<\\("
  4090. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4091. "\\)\\(.*\\)")
  4092. '(2 'org-headline-done t))
  4093. nil)
  4094. ;; Priorities
  4095. '(org-font-lock-add-priority-faces)
  4096. ;; Tags
  4097. '(org-font-lock-add-tag-faces)
  4098. ;; Special keywords
  4099. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4100. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4101. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4102. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4103. ;; Emphasis
  4104. (if em
  4105. (if (featurep 'xemacs)
  4106. '(org-do-emphasis-faces (0 nil append))
  4107. '(org-do-emphasis-faces)))
  4108. ;; Checkboxes
  4109. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4110. 2 'org-checkbox prepend)
  4111. (if org-provide-checkbox-statistics
  4112. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4113. (0 (org-get-checkbox-statistics-face) t)))
  4114. ;; Description list items
  4115. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4116. 2 'bold prepend)
  4117. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4118. '(1 'org-archived prepend))
  4119. ;; Specials
  4120. '(org-do-latex-and-special-faces)
  4121. ;; Code
  4122. '(org-activate-code (1 'org-code t))
  4123. ;; COMMENT
  4124. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4125. "\\|" org-quote-string "\\)\\>")
  4126. '(1 'org-special-keyword t))
  4127. '("^#.*" (0 'font-lock-comment-face t))
  4128. ;; Blocks and meta lines
  4129. '(org-fontify-meta-lines-and-blocks)
  4130. )))
  4131. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4132. ;; Now set the full font-lock-keywords
  4133. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4134. (org-set-local 'font-lock-defaults
  4135. '(org-font-lock-keywords t nil nil backward-paragraph))
  4136. (kill-local-variable 'font-lock-keywords) nil))
  4137. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4138. "Fontify string S like in Org-mode"
  4139. (with-temp-buffer
  4140. (insert s)
  4141. (let ((org-odd-levels-only odd-levels))
  4142. (org-mode)
  4143. (font-lock-fontify-buffer)
  4144. (buffer-string))))
  4145. (defvar org-m nil)
  4146. (defvar org-l nil)
  4147. (defvar org-f nil)
  4148. (defun org-get-level-face (n)
  4149. "Get the right face for match N in font-lock matching of headlines."
  4150. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4151. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4152. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4153. (cond
  4154. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4155. ((eq n 2) org-f)
  4156. (t (if org-level-color-stars-only nil org-f))))
  4157. (defun org-get-todo-face (kwd)
  4158. "Get the right face for a TODO keyword KWD.
  4159. If KWD is a number, get the corresponding match group."
  4160. (if (numberp kwd) (setq kwd (match-string kwd)))
  4161. (or (cdr (assoc kwd org-todo-keyword-faces))
  4162. (and (member kwd org-done-keywords) 'org-done)
  4163. 'org-todo))
  4164. (defun org-font-lock-add-tag-faces (limit)
  4165. "Add the special tag faces."
  4166. (when (and org-tag-faces org-tags-special-faces-re)
  4167. (while (re-search-forward org-tags-special-faces-re limit t)
  4168. (add-text-properties (match-beginning 1) (match-end 1)
  4169. (list 'face (org-get-tag-face 1)
  4170. 'font-lock-fontified t))
  4171. (backward-char 1))))
  4172. (defun org-font-lock-add-priority-faces (limit)
  4173. "Add the special priority faces."
  4174. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4175. (add-text-properties
  4176. (match-beginning 0) (match-end 0)
  4177. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4178. org-priority-faces))
  4179. 'org-special-keyword)
  4180. 'font-lock-fontified t))))
  4181. (defun org-get-tag-face (kwd)
  4182. "Get the right face for a TODO keyword KWD.
  4183. If KWD is a number, get the corresponding match group."
  4184. (if (numberp kwd) (setq kwd (match-string kwd)))
  4185. (or (cdr (assoc kwd org-tag-faces))
  4186. 'org-tag))
  4187. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4188. "Remove fontification and activation overlays from links."
  4189. (font-lock-default-unfontify-region beg end)
  4190. (let* ((buffer-undo-list t)
  4191. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4192. (inhibit-modification-hooks t)
  4193. deactivate-mark buffer-file-name buffer-file-truename)
  4194. (remove-text-properties beg end
  4195. '(mouse-face t keymap t org-linked-text t
  4196. invisible t intangible t))))
  4197. ;;;; Visibility cycling, including org-goto and indirect buffer
  4198. ;;; Cycling
  4199. (defvar org-cycle-global-status nil)
  4200. (make-variable-buffer-local 'org-cycle-global-status)
  4201. (defvar org-cycle-subtree-status nil)
  4202. (make-variable-buffer-local 'org-cycle-subtree-status)
  4203. ;;;###autoload
  4204. (defvar org-inlinetask-min-level)
  4205. (defun org-cycle (&optional arg)
  4206. "TAB-action and visibility cycling for Org-mode.
  4207. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4208. is outine visibility cycling, but it also invokes other actions
  4209. in special contexts.
  4210. - When this function is called with a prefix argument, rotate the entire
  4211. buffer through 3 states (global cycling)
  4212. 1. OVERVIEW: Show only top-level headlines.
  4213. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4214. 3. SHOW ALL: Show everything.
  4215. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4216. determined by the variable `org-startup-folded', and by any VISIBILITY
  4217. properties in the buffer.
  4218. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4219. including any drawers.
  4220. - When inside a table, re-align the table and move to the next field.
  4221. - When point is at the beginning of a headline, rotate the subtree started
  4222. by this line through 3 different states (local cycling)
  4223. 1. FOLDED: Only the main headline is shown.
  4224. 2. CHILDREN: The main headline and the direct children are shown.
  4225. From this state, you can move to one of the children
  4226. and zoom in further.
  4227. 3. SUBTREE: Show the entire subtree, including body text.
  4228. - When there is a numeric prefix, go up to a heading with level ARG, do
  4229. a `show-subtree' and return to the previous cursor position. If ARG
  4230. is negative, go up that many levels.
  4231. - When point is not at the beginning of a headline, execute the global
  4232. binding for TAB, which is re-indenting the line. See the option
  4233. `org-cycle-emulate-tab' for details.
  4234. - Special case: if point is at the beginning of the buffer and there is
  4235. no headline in line 1, this function will act as if called with prefix arg.
  4236. But only if also the variable `org-cycle-global-at-bob' is t."
  4237. (interactive "P")
  4238. (org-load-modules-maybe)
  4239. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4240. (let* ((limit-level
  4241. (or org-cycle-max-level
  4242. (and (boundp 'org-inlinetask-min-level)
  4243. org-inlinetask-min-level
  4244. (1- org-inlinetask-min-level))))
  4245. (nstars (and limit-level
  4246. (if org-odd-levels-only
  4247. (and limit-level (1- (* limit-level 2)))
  4248. limit-level)))
  4249. (outline-regexp
  4250. (cond
  4251. ((not (org-mode-p)) outline-regexp)
  4252. (org-cycle-include-plain-lists
  4253. (concat "\\(?:\\*"
  4254. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4255. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4256. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4257. (bob-special (and org-cycle-global-at-bob (bobp)
  4258. (not (looking-at outline-regexp))))
  4259. (org-cycle-hook
  4260. (if bob-special
  4261. (delq 'org-optimize-window-after-visibility-change
  4262. (copy-sequence org-cycle-hook))
  4263. org-cycle-hook))
  4264. (pos (point)))
  4265. (if (or bob-special (equal arg '(4)))
  4266. ;; special case: use global cycling
  4267. (setq arg t))
  4268. (cond
  4269. ((equal arg '(16))
  4270. (org-set-startup-visibility)
  4271. (message "Startup visibility, plus VISIBILITY properties"))
  4272. ((equal arg '(64))
  4273. (show-all)
  4274. (message "Entire buffer visible, including drawers"))
  4275. ((org-at-table-p 'any)
  4276. ;; Enter the table or move to the next field in the table
  4277. (or (org-table-recognize-table.el)
  4278. (progn
  4279. (if arg (org-table-edit-field t)
  4280. (org-table-justify-field-maybe)
  4281. (call-interactively 'org-table-next-field)))))
  4282. ((run-hook-with-args-until-success
  4283. 'org-tab-after-check-for-table-hook))
  4284. ((eq arg t) ;; Global cycling
  4285. (org-cycle-internal-global))
  4286. ((and org-drawers org-drawer-regexp
  4287. (save-excursion
  4288. (beginning-of-line 1)
  4289. (looking-at org-drawer-regexp)))
  4290. ;; Toggle block visibility
  4291. (org-flag-drawer
  4292. (not (get-char-property (match-end 0) 'invisible))))
  4293. ((integerp arg)
  4294. ;; Show-subtree, ARG levels up from here.
  4295. (save-excursion
  4296. (org-back-to-heading)
  4297. (outline-up-heading (if (< arg 0) (- arg)
  4298. (- (funcall outline-level) arg)))
  4299. (org-show-subtree)))
  4300. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4301. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4302. (org-cycle-internal-local))
  4303. ;; TAB emulation and template completion
  4304. (buffer-read-only (org-back-to-heading))
  4305. ((run-hook-with-args-until-success
  4306. 'org-tab-after-check-for-cycling-hook))
  4307. ((org-try-structure-completion))
  4308. ((org-try-cdlatex-tab))
  4309. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4310. (or (not (bolp))
  4311. (not (looking-at outline-regexp))))
  4312. (call-interactively (global-key-binding "\t")))
  4313. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4314. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4315. (or (and (eq org-cycle-emulate-tab 'white)
  4316. (= (match-end 0) (point-at-eol)))
  4317. (and (eq org-cycle-emulate-tab 'whitestart)
  4318. (>= (match-end 0) pos))))
  4319. t
  4320. (eq org-cycle-emulate-tab t))
  4321. (call-interactively (global-key-binding "\t")))
  4322. (t (save-excursion
  4323. (org-back-to-heading)
  4324. (org-cycle)))))))
  4325. (defun org-cycle-internal-global ()
  4326. "Do the global cycling action."
  4327. (cond
  4328. ((and (eq last-command this-command)
  4329. (eq org-cycle-global-status 'overview))
  4330. ;; We just created the overview - now do table of contents
  4331. ;; This can be slow in very large buffers, so indicate action
  4332. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4333. (message "CONTENTS...")
  4334. (org-content)
  4335. (message "CONTENTS...done")
  4336. (setq org-cycle-global-status 'contents)
  4337. (run-hook-with-args 'org-cycle-hook 'contents))
  4338. ((and (eq last-command this-command)
  4339. (eq org-cycle-global-status 'contents))
  4340. ;; We just showed the table of contents - now show everything
  4341. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4342. (show-all)
  4343. (message "SHOW ALL")
  4344. (setq org-cycle-global-status 'all)
  4345. (run-hook-with-args 'org-cycle-hook 'all))
  4346. (t
  4347. ;; Default action: go to overview
  4348. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4349. (org-overview)
  4350. (message "OVERVIEW")
  4351. (setq org-cycle-global-status 'overview)
  4352. (run-hook-with-args 'org-cycle-hook 'overview))))
  4353. (defun org-cycle-internal-local ()
  4354. "Do the local cycling action."
  4355. (org-back-to-heading)
  4356. (let ((goal-column 0) eoh eol eos)
  4357. ;; First, some boundaries
  4358. (save-excursion
  4359. (org-back-to-heading)
  4360. (save-excursion
  4361. (beginning-of-line 2)
  4362. (while (and (not (eobp)) ;; this is like `next-line'
  4363. (get-char-property (1- (point)) 'invisible))
  4364. (beginning-of-line 2)) (setq eol (point)))
  4365. (outline-end-of-heading) (setq eoh (point))
  4366. (org-end-of-subtree t)
  4367. (unless (eobp)
  4368. (skip-chars-forward " \t\n")
  4369. (beginning-of-line 1) ; in case this is an item
  4370. )
  4371. (setq eos (1- (point))))
  4372. ;; Find out what to do next and set `this-command'
  4373. (cond
  4374. ((= eos eoh)
  4375. ;; Nothing is hidden behind this heading
  4376. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4377. (message "EMPTY ENTRY")
  4378. (setq org-cycle-subtree-status nil)
  4379. (save-excursion
  4380. (goto-char eos)
  4381. (outline-next-heading)
  4382. (if (org-invisible-p) (org-flag-heading nil))))
  4383. ((or (>= eol eos)
  4384. (not (string-match "\\S-" (buffer-substring eol eos))))
  4385. ;; Entire subtree is hidden in one line: open it
  4386. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4387. (org-show-entry)
  4388. (show-children)
  4389. (message "CHILDREN")
  4390. (save-excursion
  4391. (goto-char eos)
  4392. (outline-next-heading)
  4393. (if (org-invisible-p) (org-flag-heading nil)))
  4394. (setq org-cycle-subtree-status 'children)
  4395. (run-hook-with-args 'org-cycle-hook 'children))
  4396. ((and (eq last-command this-command)
  4397. (eq org-cycle-subtree-status 'children))
  4398. ;; We just showed the children, now show everything.
  4399. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4400. (org-show-subtree)
  4401. (message "SUBTREE")
  4402. (setq org-cycle-subtree-status 'subtree)
  4403. (run-hook-with-args 'org-cycle-hook 'subtree))
  4404. (t
  4405. ;; Default action: hide the subtree.
  4406. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4407. (hide-subtree)
  4408. (message "FOLDED")
  4409. (setq org-cycle-subtree-status 'folded)
  4410. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4411. ;;;###autoload
  4412. (defun org-global-cycle (&optional arg)
  4413. "Cycle the global visibility. For details see `org-cycle'.
  4414. With C-u prefix arg, switch to startup visibility.
  4415. With a numeric prefix, show all headlines up to that level."
  4416. (interactive "P")
  4417. (let ((org-cycle-include-plain-lists
  4418. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4419. (cond
  4420. ((integerp arg)
  4421. (show-all)
  4422. (hide-sublevels arg)
  4423. (setq org-cycle-global-status 'contents))
  4424. ((equal arg '(4))
  4425. (org-set-startup-visibility)
  4426. (message "Startup visibility, plus VISIBILITY properties."))
  4427. (t
  4428. (org-cycle '(4))))))
  4429. (defun org-set-startup-visibility ()
  4430. "Set the visibility required by startup options and properties."
  4431. (cond
  4432. ((eq org-startup-folded t)
  4433. (org-cycle '(4)))
  4434. ((eq org-startup-folded 'content)
  4435. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4436. (org-cycle '(4)) (org-cycle '(4)))))
  4437. (if org-hide-block-startup (org-hide-block-all))
  4438. (org-set-visibility-according-to-property 'no-cleanup)
  4439. (org-cycle-hide-archived-subtrees 'all)
  4440. (org-cycle-hide-drawers 'all)
  4441. (org-cycle-show-empty-lines 'all))
  4442. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4443. "Switch subtree visibilities according to :VISIBILITY: property."
  4444. (interactive)
  4445. (let (org-show-entry-below state)
  4446. (save-excursion
  4447. (goto-char (point-min))
  4448. (while (re-search-forward
  4449. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4450. nil t)
  4451. (setq state (match-string 1))
  4452. (save-excursion
  4453. (org-back-to-heading t)
  4454. (hide-subtree)
  4455. (org-reveal)
  4456. (cond
  4457. ((equal state '("fold" "folded"))
  4458. (hide-subtree))
  4459. ((equal state "children")
  4460. (org-show-hidden-entry)
  4461. (show-children))
  4462. ((equal state "content")
  4463. (save-excursion
  4464. (save-restriction
  4465. (org-narrow-to-subtree)
  4466. (org-content))))
  4467. ((member state '("all" "showall"))
  4468. (show-subtree)))))
  4469. (unless no-cleanup
  4470. (org-cycle-hide-archived-subtrees 'all)
  4471. (org-cycle-hide-drawers 'all)
  4472. (org-cycle-show-empty-lines 'all)))))
  4473. (defun org-overview ()
  4474. "Switch to overview mode, showing only top-level headlines.
  4475. Really, this shows all headlines with level equal or greater than the level
  4476. of the first headline in the buffer. This is important, because if the
  4477. first headline is not level one, then (hide-sublevels 1) gives confusing
  4478. results."
  4479. (interactive)
  4480. (let ((level (save-excursion
  4481. (goto-char (point-min))
  4482. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4483. (progn
  4484. (goto-char (match-beginning 0))
  4485. (funcall outline-level))))))
  4486. (and level (hide-sublevels level))))
  4487. (defun org-content (&optional arg)
  4488. "Show all headlines in the buffer, like a table of contents.
  4489. With numerical argument N, show content up to level N."
  4490. (interactive "P")
  4491. (save-excursion
  4492. ;; Visit all headings and show their offspring
  4493. (and (integerp arg) (org-overview))
  4494. (goto-char (point-max))
  4495. (catch 'exit
  4496. (while (and (progn (condition-case nil
  4497. (outline-previous-visible-heading 1)
  4498. (error (goto-char (point-min))))
  4499. t)
  4500. (looking-at outline-regexp))
  4501. (if (integerp arg)
  4502. (show-children (1- arg))
  4503. (show-branches))
  4504. (if (bobp) (throw 'exit nil))))))
  4505. (defun org-optimize-window-after-visibility-change (state)
  4506. "Adjust the window after a change in outline visibility.
  4507. This function is the default value of the hook `org-cycle-hook'."
  4508. (when (get-buffer-window (current-buffer))
  4509. (cond
  4510. ((eq state 'content) nil)
  4511. ((eq state 'all) nil)
  4512. ((eq state 'folded) nil)
  4513. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4514. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4515. (defun org-compact-display-after-subtree-move ()
  4516. "Show a compacter version of the tree of the entry's parent."
  4517. (save-excursion
  4518. (if (org-up-heading-safe)
  4519. (progn
  4520. (hide-subtree)
  4521. (show-entry)
  4522. (show-children)
  4523. (org-cycle-show-empty-lines 'children)
  4524. (org-cycle-hide-drawers 'children))
  4525. (org-overview))))
  4526. (defun org-cycle-show-empty-lines (state)
  4527. "Show empty lines above all visible headlines.
  4528. The region to be covered depends on STATE when called through
  4529. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4530. entire buffer covered. Note that an empty line is only shown if there
  4531. are at least `org-cycle-separator-lines' empty lines before the headline."
  4532. (when (> org-cycle-separator-lines 0)
  4533. (save-excursion
  4534. (let* ((n org-cycle-separator-lines)
  4535. (re (cond
  4536. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4537. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4538. (t (let ((ns (number-to-string (- n 2))))
  4539. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4540. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4541. beg end)
  4542. (cond
  4543. ((memq state '(overview contents t))
  4544. (setq beg (point-min) end (point-max)))
  4545. ((memq state '(children folded))
  4546. (setq beg (point) end (progn (org-end-of-subtree t t)
  4547. (beginning-of-line 2)
  4548. (point)))))
  4549. (when beg
  4550. (goto-char beg)
  4551. (while (re-search-forward re end t)
  4552. (if (not (get-char-property (match-end 1) 'invisible))
  4553. (outline-flag-region
  4554. (match-beginning 1) (match-end 1) nil)))))))
  4555. ;; Never hide empty lines at the end of the file.
  4556. (save-excursion
  4557. (goto-char (point-max))
  4558. (outline-previous-heading)
  4559. (outline-end-of-heading)
  4560. (if (and (looking-at "[ \t\n]+")
  4561. (= (match-end 0) (point-max)))
  4562. (outline-flag-region (point) (match-end 0) nil))))
  4563. (defun org-show-empty-lines-in-parent ()
  4564. "Move to the parent and re-show empty lines before visible headlines."
  4565. (save-excursion
  4566. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4567. (org-cycle-show-empty-lines context))))
  4568. (defun org-cycle-hide-drawers (state)
  4569. "Re-hide all drawers after a visibility state change."
  4570. (when (and (org-mode-p)
  4571. (not (memq state '(overview folded))))
  4572. (save-excursion
  4573. (let* ((globalp (memq state '(contents all)))
  4574. (beg (if globalp (point-min) (point)))
  4575. (end (if globalp (point-max) (org-end-of-subtree t))))
  4576. (goto-char beg)
  4577. (while (re-search-forward org-drawer-regexp end t)
  4578. (org-flag-drawer t))))))
  4579. (defun org-flag-drawer (flag)
  4580. (save-excursion
  4581. (beginning-of-line 1)
  4582. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4583. (let ((b (match-end 0))
  4584. (outline-regexp org-outline-regexp))
  4585. (if (re-search-forward
  4586. "^[ \t]*:END:"
  4587. (save-excursion (outline-next-heading) (point)) t)
  4588. (outline-flag-region b (point-at-eol) flag)
  4589. (error ":END: line missing"))))))
  4590. (defun org-subtree-end-visible-p ()
  4591. "Is the end of the current subtree visible?"
  4592. (pos-visible-in-window-p
  4593. (save-excursion (org-end-of-subtree t) (point))))
  4594. (defun org-first-headline-recenter (&optional N)
  4595. "Move cursor to the first headline and recenter the headline.
  4596. Optional argument N means, put the headline into the Nth line of the window."
  4597. (goto-char (point-min))
  4598. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4599. (beginning-of-line)
  4600. (recenter (prefix-numeric-value N))))
  4601. ;;; Folding of blocks
  4602. (defconst org-block-regexp
  4603. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4604. "Regular expression for hiding blocks.")
  4605. (defvar org-hide-block-overlays nil
  4606. "Overays hiding blocks.")
  4607. (make-variable-buffer-local 'org-hide-block-overlays)
  4608. (defun org-block-map (function &optional start end)
  4609. "Call func at the head of all source blocks in the current
  4610. buffer. Optional arguments START and END can be used to limit
  4611. the range."
  4612. (let ((start (or start (point-min)))
  4613. (end (or end (point-max))))
  4614. (save-excursion
  4615. (goto-char start)
  4616. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4617. (save-excursion
  4618. (save-match-data
  4619. (goto-char (match-beginning 0))
  4620. (funcall function)))))))
  4621. (defun org-hide-block-toggle-all ()
  4622. "Toggle the visibility of all blocks in the current buffer."
  4623. (org-block-map #'org-hide-block-toggle))
  4624. (defun org-hide-block-all ()
  4625. "Fold all blocks in the current buffer."
  4626. (interactive)
  4627. (org-show-block-all)
  4628. (org-block-map #'org-hide-block-toggle-maybe))
  4629. (defun org-show-block-all ()
  4630. "Unfold all blocks in the current buffer."
  4631. (mapc 'org-delete-overlay org-hide-block-overlays)
  4632. (setq org-hide-block-overlays nil))
  4633. (defun org-hide-block-toggle-maybe ()
  4634. "Toggle visibility of block at point."
  4635. (interactive)
  4636. (let ((case-fold-search t))
  4637. (if (save-excursion
  4638. (beginning-of-line 1)
  4639. (looking-at org-block-regexp))
  4640. (progn (org-hide-block-toggle)
  4641. t) ;; to signal that we took action
  4642. nil))) ;; to signal that we did not
  4643. (defun org-hide-block-toggle (&optional force)
  4644. "Toggle the visibility of the current block."
  4645. (interactive)
  4646. (save-excursion
  4647. (beginning-of-line)
  4648. (if (re-search-forward org-block-regexp nil t)
  4649. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4650. (end (match-end 0))
  4651. ov) ;; end of entire body
  4652. (if (memq t (mapcar (lambda (overlay)
  4653. (eq (org-overlay-get overlay 'invisible)
  4654. 'org-hide-block))
  4655. (org-overlays-at start)))
  4656. (if (or (not force) (eq force 'off))
  4657. (mapc (lambda (ov)
  4658. (when (member ov org-hide-block-overlays)
  4659. (setq org-hide-block-overlays
  4660. (delq ov org-hide-block-overlays)))
  4661. (when (eq (org-overlay-get ov 'invisible)
  4662. 'org-hide-block)
  4663. (org-delete-overlay ov)))
  4664. (org-overlays-at start)))
  4665. (setq ov (make-overlay start end))
  4666. (org-overlay-put ov 'invisible 'org-hide-block)
  4667. (push ov org-hide-block-overlays)))
  4668. (error "Not looking at a source block"))))
  4669. ;; org-tab-after-check-for-cycling-hook
  4670. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4671. ;; Remove overlays when changing major mode
  4672. (add-hook 'org-mode-hook
  4673. (lambda () (org-add-hook 'change-major-mode-hook
  4674. 'org-show-block-all 'append 'local)))
  4675. ;;; Org-goto
  4676. (defvar org-goto-window-configuration nil)
  4677. (defvar org-goto-marker nil)
  4678. (defvar org-goto-map
  4679. (let ((map (make-sparse-keymap)))
  4680. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4681. (while (setq cmd (pop cmds))
  4682. (substitute-key-definition cmd cmd map global-map)))
  4683. (suppress-keymap map)
  4684. (org-defkey map "\C-m" 'org-goto-ret)
  4685. (org-defkey map [(return)] 'org-goto-ret)
  4686. (org-defkey map [(left)] 'org-goto-left)
  4687. (org-defkey map [(right)] 'org-goto-right)
  4688. (org-defkey map [(control ?g)] 'org-goto-quit)
  4689. (org-defkey map "\C-i" 'org-cycle)
  4690. (org-defkey map [(tab)] 'org-cycle)
  4691. (org-defkey map [(down)] 'outline-next-visible-heading)
  4692. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4693. (if org-goto-auto-isearch
  4694. (if (fboundp 'define-key-after)
  4695. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4696. nil)
  4697. (org-defkey map "q" 'org-goto-quit)
  4698. (org-defkey map "n" 'outline-next-visible-heading)
  4699. (org-defkey map "p" 'outline-previous-visible-heading)
  4700. (org-defkey map "f" 'outline-forward-same-level)
  4701. (org-defkey map "b" 'outline-backward-same-level)
  4702. (org-defkey map "u" 'outline-up-heading))
  4703. (org-defkey map "/" 'org-occur)
  4704. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4705. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4706. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4707. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4708. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4709. map))
  4710. (defconst org-goto-help
  4711. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4712. RET=jump to location [Q]uit and return to previous location
  4713. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4714. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4715. ;; FIXME: Docstring doe not mention both interfaces
  4716. (defun org-goto (&optional alternative-interface)
  4717. "Look up a different location in the current file, keeping current visibility.
  4718. When you want look-up or go to a different location in a document, the
  4719. fastest way is often to fold the entire buffer and then dive into the tree.
  4720. This method has the disadvantage, that the previous location will be folded,
  4721. which may not be what you want.
  4722. This command works around this by showing a copy of the current buffer
  4723. in an indirect buffer, in overview mode. You can dive into the tree in
  4724. that copy, use org-occur and incremental search to find a location.
  4725. When pressing RET or `Q', the command returns to the original buffer in
  4726. which the visibility is still unchanged. After RET is will also jump to
  4727. the location selected in the indirect buffer and expose the
  4728. the headline hierarchy above."
  4729. (interactive "P")
  4730. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4731. (org-refile-use-outline-path t)
  4732. (org-refile-target-verify-function nil)
  4733. (interface
  4734. (if (not alternative-interface)
  4735. org-goto-interface
  4736. (if (eq org-goto-interface 'outline)
  4737. 'outline-path-completion
  4738. 'outline)))
  4739. (org-goto-start-pos (point))
  4740. (selected-point
  4741. (if (eq interface 'outline)
  4742. (car (org-get-location (current-buffer) org-goto-help))
  4743. (nth 3 (org-refile-get-location "Goto: ")))))
  4744. (if selected-point
  4745. (progn
  4746. (org-mark-ring-push org-goto-start-pos)
  4747. (goto-char selected-point)
  4748. (if (or (org-invisible-p) (org-invisible-p2))
  4749. (org-show-context 'org-goto)))
  4750. (message "Quit"))))
  4751. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4752. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4753. (defvar org-goto-local-auto-isearch-map) ; defined below
  4754. (defun org-get-location (buf help)
  4755. "Let the user select a location in the Org-mode buffer BUF.
  4756. This function uses a recursive edit. It returns the selected position
  4757. or nil."
  4758. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4759. (isearch-hide-immediately nil)
  4760. (isearch-search-fun-function
  4761. (lambda () 'org-goto-local-search-headings))
  4762. (org-goto-selected-point org-goto-exit-command))
  4763. (save-excursion
  4764. (save-window-excursion
  4765. (delete-other-windows)
  4766. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4767. (switch-to-buffer
  4768. (condition-case nil
  4769. (make-indirect-buffer (current-buffer) "*org-goto*")
  4770. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4771. (with-output-to-temp-buffer "*Help*"
  4772. (princ help))
  4773. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4774. (setq buffer-read-only nil)
  4775. (let ((org-startup-truncated t)
  4776. (org-startup-folded nil)
  4777. (org-startup-align-all-tables nil))
  4778. (org-mode)
  4779. (org-overview))
  4780. (setq buffer-read-only t)
  4781. (if (and (boundp 'org-goto-start-pos)
  4782. (integer-or-marker-p org-goto-start-pos))
  4783. (let ((org-show-hierarchy-above t)
  4784. (org-show-siblings t)
  4785. (org-show-following-heading t))
  4786. (goto-char org-goto-start-pos)
  4787. (and (org-invisible-p) (org-show-context)))
  4788. (goto-char (point-min)))
  4789. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4790. (message "Select location and press RET")
  4791. (use-local-map org-goto-map)
  4792. (recursive-edit)
  4793. ))
  4794. (kill-buffer "*org-goto*")
  4795. (cons org-goto-selected-point org-goto-exit-command)))
  4796. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4797. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4798. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4799. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4800. (defun org-goto-local-search-headings (string bound noerror)
  4801. "Search and make sure that any matches are in headlines."
  4802. (catch 'return
  4803. (while (if isearch-forward
  4804. (search-forward string bound noerror)
  4805. (search-backward string bound noerror))
  4806. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4807. (and (member :headline context)
  4808. (not (member :tags context))))
  4809. (throw 'return (point))))))
  4810. (defun org-goto-local-auto-isearch ()
  4811. "Start isearch."
  4812. (interactive)
  4813. (goto-char (point-min))
  4814. (let ((keys (this-command-keys)))
  4815. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4816. (isearch-mode t)
  4817. (isearch-process-search-char (string-to-char keys)))))
  4818. (defun org-goto-ret (&optional arg)
  4819. "Finish `org-goto' by going to the new location."
  4820. (interactive "P")
  4821. (setq org-goto-selected-point (point)
  4822. org-goto-exit-command 'return)
  4823. (throw 'exit nil))
  4824. (defun org-goto-left ()
  4825. "Finish `org-goto' by going to the new location."
  4826. (interactive)
  4827. (if (org-on-heading-p)
  4828. (progn
  4829. (beginning-of-line 1)
  4830. (setq org-goto-selected-point (point)
  4831. org-goto-exit-command 'left)
  4832. (throw 'exit nil))
  4833. (error "Not on a heading")))
  4834. (defun org-goto-right ()
  4835. "Finish `org-goto' by going to the new location."
  4836. (interactive)
  4837. (if (org-on-heading-p)
  4838. (progn
  4839. (setq org-goto-selected-point (point)
  4840. org-goto-exit-command 'right)
  4841. (throw 'exit nil))
  4842. (error "Not on a heading")))
  4843. (defun org-goto-quit ()
  4844. "Finish `org-goto' without cursor motion."
  4845. (interactive)
  4846. (setq org-goto-selected-point nil)
  4847. (setq org-goto-exit-command 'quit)
  4848. (throw 'exit nil))
  4849. ;;; Indirect buffer display of subtrees
  4850. (defvar org-indirect-dedicated-frame nil
  4851. "This is the frame being used for indirect tree display.")
  4852. (defvar org-last-indirect-buffer nil)
  4853. (defun org-tree-to-indirect-buffer (&optional arg)
  4854. "Create indirect buffer and narrow it to current subtree.
  4855. With numerical prefix ARG, go up to this level and then take that tree.
  4856. If ARG is negative, go up that many levels.
  4857. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4858. indirect buffer previously made with this command, to avoid proliferation of
  4859. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4860. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4861. is kept so that you can work with several indirect buffers at the same time.
  4862. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4863. requests that a new frame be made for the new buffer, so that the dedicated
  4864. frame is not changed."
  4865. (interactive "P")
  4866. (let ((cbuf (current-buffer))
  4867. (cwin (selected-window))
  4868. (pos (point))
  4869. beg end level heading ibuf)
  4870. (save-excursion
  4871. (org-back-to-heading t)
  4872. (when (numberp arg)
  4873. (setq level (org-outline-level))
  4874. (if (< arg 0) (setq arg (+ level arg)))
  4875. (while (> (setq level (org-outline-level)) arg)
  4876. (outline-up-heading 1 t)))
  4877. (setq beg (point)
  4878. heading (org-get-heading))
  4879. (org-end-of-subtree t) (setq end (point)))
  4880. (if (and (buffer-live-p org-last-indirect-buffer)
  4881. (not (eq org-indirect-buffer-display 'new-frame))
  4882. (not arg))
  4883. (kill-buffer org-last-indirect-buffer))
  4884. (setq ibuf (org-get-indirect-buffer cbuf)
  4885. org-last-indirect-buffer ibuf)
  4886. (cond
  4887. ((or (eq org-indirect-buffer-display 'new-frame)
  4888. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4889. (select-frame (make-frame))
  4890. (delete-other-windows)
  4891. (switch-to-buffer ibuf)
  4892. (org-set-frame-title heading))
  4893. ((eq org-indirect-buffer-display 'dedicated-frame)
  4894. (raise-frame
  4895. (select-frame (or (and org-indirect-dedicated-frame
  4896. (frame-live-p org-indirect-dedicated-frame)
  4897. org-indirect-dedicated-frame)
  4898. (setq org-indirect-dedicated-frame (make-frame)))))
  4899. (delete-other-windows)
  4900. (switch-to-buffer ibuf)
  4901. (org-set-frame-title (concat "Indirect: " heading)))
  4902. ((eq org-indirect-buffer-display 'current-window)
  4903. (switch-to-buffer ibuf))
  4904. ((eq org-indirect-buffer-display 'other-window)
  4905. (pop-to-buffer ibuf))
  4906. (t (error "Invalid value.")))
  4907. (if (featurep 'xemacs)
  4908. (save-excursion (org-mode) (turn-on-font-lock)))
  4909. (narrow-to-region beg end)
  4910. (show-all)
  4911. (goto-char pos)
  4912. (and (window-live-p cwin) (select-window cwin))))
  4913. (defun org-get-indirect-buffer (&optional buffer)
  4914. (setq buffer (or buffer (current-buffer)))
  4915. (let ((n 1) (base (buffer-name buffer)) bname)
  4916. (while (buffer-live-p
  4917. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4918. (setq n (1+ n)))
  4919. (condition-case nil
  4920. (make-indirect-buffer buffer bname 'clone)
  4921. (error (make-indirect-buffer buffer bname)))))
  4922. (defun org-set-frame-title (title)
  4923. "Set the title of the current frame to the string TITLE."
  4924. ;; FIXME: how to name a single frame in XEmacs???
  4925. (unless (featurep 'xemacs)
  4926. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4927. ;;;; Structure editing
  4928. ;;; Inserting headlines
  4929. (defun org-previous-line-empty-p ()
  4930. (save-excursion
  4931. (and (not (bobp))
  4932. (or (beginning-of-line 0) t)
  4933. (save-match-data
  4934. (looking-at "[ \t]*$")))))
  4935. (defun org-insert-heading (&optional force-heading)
  4936. "Insert a new heading or item with same depth at point.
  4937. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4938. If point is at the beginning of a headline, insert a sibling before the
  4939. current headline. If point is not at the beginning, do not split the line,
  4940. but create the new headline after the current line."
  4941. (interactive "P")
  4942. (if (= (buffer-size) 0)
  4943. (insert "\n* ")
  4944. (when (or force-heading (not (org-insert-item)))
  4945. (let* ((empty-line-p nil)
  4946. (head (save-excursion
  4947. (condition-case nil
  4948. (progn
  4949. (org-back-to-heading)
  4950. (setq empty-line-p (org-previous-line-empty-p))
  4951. (match-string 0))
  4952. (error "*"))))
  4953. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4954. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4955. pos hide-previous previous-pos)
  4956. (cond
  4957. ((and (org-on-heading-p) (bolp)
  4958. (or (bobp)
  4959. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4960. ;; insert before the current line
  4961. (open-line (if blank 2 1)))
  4962. ((and (bolp)
  4963. (or (bobp)
  4964. (save-excursion
  4965. (backward-char 1) (not (org-invisible-p)))))
  4966. ;; insert right here
  4967. nil)
  4968. (t
  4969. ;; somewhere in the line
  4970. (save-excursion
  4971. (setq previous-pos (point-at-bol))
  4972. (end-of-line)
  4973. (setq hide-previous (org-invisible-p)))
  4974. (and org-insert-heading-respect-content (org-show-subtree))
  4975. (let ((split
  4976. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4977. (save-excursion
  4978. (let ((p (point)))
  4979. (goto-char (point-at-bol))
  4980. (and (looking-at org-complex-heading-regexp)
  4981. (> p (match-beginning 4)))))))
  4982. tags pos)
  4983. (cond
  4984. (org-insert-heading-respect-content
  4985. (org-end-of-subtree nil t)
  4986. (or (bolp) (newline))
  4987. (or (org-previous-line-empty-p)
  4988. (and blank (newline)))
  4989. (open-line 1))
  4990. ((org-on-heading-p)
  4991. (when hide-previous
  4992. (show-children)
  4993. (org-show-entry))
  4994. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4995. (setq tags (and (match-end 2) (match-string 2)))
  4996. (and (match-end 1)
  4997. (delete-region (match-beginning 1) (match-end 1)))
  4998. (setq pos (point-at-bol))
  4999. (or split (end-of-line 1))
  5000. (delete-horizontal-space)
  5001. (newline (if blank 2 1))
  5002. (when tags
  5003. (save-excursion
  5004. (goto-char pos)
  5005. (end-of-line 1)
  5006. (insert " " tags)
  5007. (org-set-tags nil 'align))))
  5008. (t
  5009. (or split (end-of-line 1))
  5010. (newline (if blank 2 1)))))))
  5011. (insert head) (just-one-space)
  5012. (setq pos (point))
  5013. (end-of-line 1)
  5014. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5015. (when (and org-insert-heading-respect-content hide-previous)
  5016. (save-excursion
  5017. (goto-char previous-pos)
  5018. (hide-subtree)))
  5019. (run-hooks 'org-insert-heading-hook)))))
  5020. (defun org-get-heading (&optional no-tags)
  5021. "Return the heading of the current entry, without the stars."
  5022. (save-excursion
  5023. (org-back-to-heading t)
  5024. (if (looking-at
  5025. (if no-tags
  5026. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5027. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5028. (match-string 1) "")))
  5029. (defun org-heading-components ()
  5030. "Return the components of the current heading.
  5031. This is a list with the following elements:
  5032. - the level as an integer
  5033. - the reduced level, different if `org-odd-levels-only' is set.
  5034. - the TODO keyword, or nil
  5035. - the priority character, like ?A, or nil if no priority is given
  5036. - the headline text itself, or the tags string if no headline text
  5037. - the tags string, or nil."
  5038. (save-excursion
  5039. (org-back-to-heading t)
  5040. (if (looking-at org-complex-heading-regexp)
  5041. (list (length (match-string 1))
  5042. (org-reduced-level (length (match-string 1)))
  5043. (org-match-string-no-properties 2)
  5044. (and (match-end 3) (aref (match-string 3) 2))
  5045. (org-match-string-no-properties 4)
  5046. (org-match-string-no-properties 5)))))
  5047. (defun org-get-entry ()
  5048. "Get the entry text, after heading, entire subtree."
  5049. (save-excursion
  5050. (org-back-to-heading t)
  5051. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5052. (defun org-insert-heading-after-current ()
  5053. "Insert a new heading with same level as current, after current subtree."
  5054. (interactive)
  5055. (org-back-to-heading)
  5056. (org-insert-heading)
  5057. (org-move-subtree-down)
  5058. (end-of-line 1))
  5059. (defun org-insert-heading-respect-content ()
  5060. (interactive)
  5061. (let ((org-insert-heading-respect-content t))
  5062. (org-insert-heading t)))
  5063. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5064. (interactive "P")
  5065. (let ((org-insert-heading-respect-content t))
  5066. (org-insert-todo-heading force-state t)))
  5067. (defun org-insert-todo-heading (arg &optional force-heading)
  5068. "Insert a new heading with the same level and TODO state as current heading.
  5069. If the heading has no TODO state, or if the state is DONE, use the first
  5070. state (TODO by default). Also with prefix arg, force first state."
  5071. (interactive "P")
  5072. (when (or force-heading (not (org-insert-item 'checkbox)))
  5073. (org-insert-heading force-heading)
  5074. (save-excursion
  5075. (org-back-to-heading)
  5076. (outline-previous-heading)
  5077. (looking-at org-todo-line-regexp))
  5078. (let*
  5079. ((new-mark-x
  5080. (if (or arg
  5081. (not (match-beginning 2))
  5082. (member (match-string 2) org-done-keywords))
  5083. (car org-todo-keywords-1)
  5084. (match-string 2)))
  5085. (new-mark
  5086. (or
  5087. (run-hook-with-args-until-success
  5088. 'org-todo-get-default-hook new-mark-x nil)
  5089. new-mark-x)))
  5090. (beginning-of-line 1)
  5091. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5092. (if org-treat-insert-todo-heading-as-state-change
  5093. (org-todo new-mark)
  5094. (insert new-mark " "))))
  5095. (when org-provide-todo-statistics
  5096. (org-update-parent-todo-statistics))))
  5097. (defun org-insert-subheading (arg)
  5098. "Insert a new subheading and demote it.
  5099. Works for outline headings and for plain lists alike."
  5100. (interactive "P")
  5101. (org-insert-heading arg)
  5102. (cond
  5103. ((org-on-heading-p) (org-do-demote))
  5104. ((org-at-item-p) (org-indent-item 1))))
  5105. (defun org-insert-todo-subheading (arg)
  5106. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5107. Works for outline headings and for plain lists alike."
  5108. (interactive "P")
  5109. (org-insert-todo-heading arg)
  5110. (cond
  5111. ((org-on-heading-p) (org-do-demote))
  5112. ((org-at-item-p) (org-indent-item 1))))
  5113. ;;; Promotion and Demotion
  5114. (defun org-promote-subtree ()
  5115. "Promote the entire subtree.
  5116. See also `org-promote'."
  5117. (interactive)
  5118. (save-excursion
  5119. (org-map-tree 'org-promote))
  5120. (org-fix-position-after-promote))
  5121. (defun org-demote-subtree ()
  5122. "Demote the entire subtree. See `org-demote'.
  5123. See also `org-promote'."
  5124. (interactive)
  5125. (save-excursion
  5126. (org-map-tree 'org-demote))
  5127. (org-fix-position-after-promote))
  5128. (defun org-do-promote ()
  5129. "Promote the current heading higher up the tree.
  5130. If the region is active in `transient-mark-mode', promote all headings
  5131. in the region."
  5132. (interactive)
  5133. (save-excursion
  5134. (if (org-region-active-p)
  5135. (org-map-region 'org-promote (region-beginning) (region-end))
  5136. (org-promote)))
  5137. (org-fix-position-after-promote))
  5138. (defun org-do-demote ()
  5139. "Demote the current heading lower down the tree.
  5140. If the region is active in `transient-mark-mode', demote all headings
  5141. in the region."
  5142. (interactive)
  5143. (save-excursion
  5144. (if (org-region-active-p)
  5145. (org-map-region 'org-demote (region-beginning) (region-end))
  5146. (org-demote)))
  5147. (org-fix-position-after-promote))
  5148. (defun org-fix-position-after-promote ()
  5149. "Make sure that after pro/demotion cursor position is right."
  5150. (let ((pos (point)))
  5151. (when (save-excursion
  5152. (beginning-of-line 1)
  5153. (looking-at org-todo-line-regexp)
  5154. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5155. (cond ((eobp) (insert " "))
  5156. ((eolp) (insert " "))
  5157. ((equal (char-after) ?\ ) (forward-char 1))))))
  5158. (defun org-reduced-level (l)
  5159. "Compute the effective level of a heading.
  5160. This takes into account the setting of `org-odd-levels-only'."
  5161. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5162. (defun org-get-valid-level (level &optional change)
  5163. "Rectify a level change under the influence of `org-odd-levels-only'
  5164. LEVEL is a current level, CHANGE is by how much the level should be
  5165. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5166. even level numbers will become the next higher odd number."
  5167. (if org-odd-levels-only
  5168. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5169. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5170. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5171. (max 1 (+ level (or change 0)))))
  5172. (if (boundp 'define-obsolete-function-alias)
  5173. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5174. (define-obsolete-function-alias 'org-get-legal-level
  5175. 'org-get-valid-level)
  5176. (define-obsolete-function-alias 'org-get-legal-level
  5177. 'org-get-valid-level "23.1")))
  5178. (defun org-promote ()
  5179. "Promote the current heading higher up the tree.
  5180. If the region is active in `transient-mark-mode', promote all headings
  5181. in the region."
  5182. (org-back-to-heading t)
  5183. (let* ((level (save-match-data (funcall outline-level)))
  5184. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5185. (diff (abs (- level (length up-head) -1))))
  5186. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5187. (replace-match up-head nil t)
  5188. ;; Fixup tag positioning
  5189. (and org-auto-align-tags (org-set-tags nil t))
  5190. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5191. (defun org-demote ()
  5192. "Demote the current heading lower down the tree.
  5193. If the region is active in `transient-mark-mode', demote all headings
  5194. in the region."
  5195. (org-back-to-heading t)
  5196. (let* ((level (save-match-data (funcall outline-level)))
  5197. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5198. (diff (abs (- level (length down-head) -1))))
  5199. (replace-match down-head nil t)
  5200. ;; Fixup tag positioning
  5201. (and org-auto-align-tags (org-set-tags nil t))
  5202. (if org-adapt-indentation (org-fixup-indentation diff))))
  5203. (defun org-map-tree (fun)
  5204. "Call FUN for every heading underneath the current one."
  5205. (org-back-to-heading)
  5206. (let ((level (funcall outline-level)))
  5207. (save-excursion
  5208. (funcall fun)
  5209. (while (and (progn
  5210. (outline-next-heading)
  5211. (> (funcall outline-level) level))
  5212. (not (eobp)))
  5213. (funcall fun)))))
  5214. (defun org-map-region (fun beg end)
  5215. "Call FUN for every heading between BEG and END."
  5216. (let ((org-ignore-region t))
  5217. (save-excursion
  5218. (setq end (copy-marker end))
  5219. (goto-char beg)
  5220. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5221. (< (point) end))
  5222. (funcall fun))
  5223. (while (and (progn
  5224. (outline-next-heading)
  5225. (< (point) end))
  5226. (not (eobp)))
  5227. (funcall fun)))))
  5228. (defun org-fixup-indentation (diff)
  5229. "Change the indentation in the current entry by DIFF
  5230. However, if any line in the current entry has no indentation, or if it
  5231. would end up with no indentation after the change, nothing at all is done."
  5232. (save-excursion
  5233. (let ((end (save-excursion (outline-next-heading)
  5234. (point-marker)))
  5235. (prohibit (if (> diff 0)
  5236. "^\\S-"
  5237. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5238. col)
  5239. (unless (save-excursion (end-of-line 1)
  5240. (re-search-forward prohibit end t))
  5241. (while (and (< (point) end)
  5242. (re-search-forward "^[ \t]+" end t))
  5243. (goto-char (match-end 0))
  5244. (setq col (current-column))
  5245. (if (< diff 0) (replace-match ""))
  5246. (org-indent-to-column (+ diff col))))
  5247. (move-marker end nil))))
  5248. (defun org-convert-to-odd-levels ()
  5249. "Convert an org-mode file with all levels allowed to one with odd levels.
  5250. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5251. level 5 etc."
  5252. (interactive)
  5253. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5254. (let ((org-odd-levels-only nil) n)
  5255. (save-excursion
  5256. (goto-char (point-min))
  5257. (while (re-search-forward "^\\*\\*+ " nil t)
  5258. (setq n (- (length (match-string 0)) 2))
  5259. (while (>= (setq n (1- n)) 0)
  5260. (org-demote))
  5261. (end-of-line 1))))))
  5262. (defun org-convert-to-oddeven-levels ()
  5263. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5264. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5265. section with an even level, conversion would destroy the structure of the file. An error
  5266. is signaled in this case."
  5267. (interactive)
  5268. (goto-char (point-min))
  5269. ;; First check if there are no even levels
  5270. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5271. (org-show-context t)
  5272. (error "Not all levels are odd in this file. Conversion not possible."))
  5273. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5274. (let ((org-odd-levels-only nil) n)
  5275. (save-excursion
  5276. (goto-char (point-min))
  5277. (while (re-search-forward "^\\*\\*+ " nil t)
  5278. (setq n (/ (1- (length (match-string 0))) 2))
  5279. (while (>= (setq n (1- n)) 0)
  5280. (org-promote))
  5281. (end-of-line 1))))))
  5282. (defun org-tr-level (n)
  5283. "Make N odd if required."
  5284. (if org-odd-levels-only (1+ (/ n 2)) n))
  5285. ;;; Vertical tree motion, cutting and pasting of subtrees
  5286. (defun org-move-subtree-up (&optional arg)
  5287. "Move the current subtree up past ARG headlines of the same level."
  5288. (interactive "p")
  5289. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5290. (defun org-move-subtree-down (&optional arg)
  5291. "Move the current subtree down past ARG headlines of the same level."
  5292. (interactive "p")
  5293. (setq arg (prefix-numeric-value arg))
  5294. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5295. 'outline-get-last-sibling))
  5296. (ins-point (make-marker))
  5297. (cnt (abs arg))
  5298. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5299. ;; Select the tree
  5300. (org-back-to-heading)
  5301. (setq beg0 (point))
  5302. (save-excursion
  5303. (setq ne-beg (org-back-over-empty-lines))
  5304. (setq beg (point)))
  5305. (save-match-data
  5306. (save-excursion (outline-end-of-heading)
  5307. (setq folded (org-invisible-p)))
  5308. (outline-end-of-subtree))
  5309. (outline-next-heading)
  5310. (setq ne-end (org-back-over-empty-lines))
  5311. (setq end (point))
  5312. (goto-char beg0)
  5313. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5314. ;; include less whitespace
  5315. (save-excursion
  5316. (goto-char beg)
  5317. (forward-line (- ne-beg ne-end))
  5318. (setq beg (point))))
  5319. ;; Find insertion point, with error handling
  5320. (while (> cnt 0)
  5321. (or (and (funcall movfunc) (looking-at outline-regexp))
  5322. (progn (goto-char beg0)
  5323. (error "Cannot move past superior level or buffer limit")))
  5324. (setq cnt (1- cnt)))
  5325. (if (> arg 0)
  5326. ;; Moving forward - still need to move over subtree
  5327. (progn (org-end-of-subtree t t)
  5328. (save-excursion
  5329. (org-back-over-empty-lines)
  5330. (or (bolp) (newline)))))
  5331. (setq ne-ins (org-back-over-empty-lines))
  5332. (move-marker ins-point (point))
  5333. (setq txt (buffer-substring beg end))
  5334. (org-save-markers-in-region beg end)
  5335. (delete-region beg end)
  5336. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5337. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5338. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5339. (let ((bbb (point)))
  5340. (insert-before-markers txt)
  5341. (org-reinstall-markers-in-region bbb)
  5342. (move-marker ins-point bbb))
  5343. (or (bolp) (insert "\n"))
  5344. (setq ins-end (point))
  5345. (goto-char ins-point)
  5346. (org-skip-whitespace)
  5347. (when (and (< arg 0)
  5348. (org-first-sibling-p)
  5349. (> ne-ins ne-beg))
  5350. ;; Move whitespace back to beginning
  5351. (save-excursion
  5352. (goto-char ins-end)
  5353. (let ((kill-whole-line t))
  5354. (kill-line (- ne-ins ne-beg)) (point)))
  5355. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5356. (move-marker ins-point nil)
  5357. (org-compact-display-after-subtree-move)
  5358. (org-show-empty-lines-in-parent)
  5359. (unless folded
  5360. (org-show-entry)
  5361. (show-children)
  5362. (org-cycle-hide-drawers 'children))))
  5363. (defvar org-subtree-clip ""
  5364. "Clipboard for cut and paste of subtrees.
  5365. This is actually only a copy of the kill, because we use the normal kill
  5366. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5367. (defvar org-subtree-clip-folded nil
  5368. "Was the last copied subtree folded?
  5369. This is used to fold the tree back after pasting.")
  5370. (defun org-cut-subtree (&optional n)
  5371. "Cut the current subtree into the clipboard.
  5372. With prefix arg N, cut this many sequential subtrees.
  5373. This is a short-hand for marking the subtree and then cutting it."
  5374. (interactive "p")
  5375. (org-copy-subtree n 'cut))
  5376. (defun org-copy-subtree (&optional n cut force-store-markers)
  5377. "Cut the current subtree into the clipboard.
  5378. With prefix arg N, cut this many sequential subtrees.
  5379. This is a short-hand for marking the subtree and then copying it.
  5380. If CUT is non-nil, actually cut the subtree.
  5381. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5382. of some markers in the region, even if CUT is non-nil. This is
  5383. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5384. (interactive "p")
  5385. (let (beg end folded (beg0 (point)))
  5386. (if (interactive-p)
  5387. (org-back-to-heading nil) ; take what looks like a subtree
  5388. (org-back-to-heading t)) ; take what is really there
  5389. (org-back-over-empty-lines)
  5390. (setq beg (point))
  5391. (skip-chars-forward " \t\r\n")
  5392. (save-match-data
  5393. (save-excursion (outline-end-of-heading)
  5394. (setq folded (org-invisible-p)))
  5395. (condition-case nil
  5396. (outline-forward-same-level (1- n))
  5397. (error nil))
  5398. (org-end-of-subtree t t))
  5399. (org-back-over-empty-lines)
  5400. (setq end (point))
  5401. (goto-char beg0)
  5402. (when (> end beg)
  5403. (setq org-subtree-clip-folded folded)
  5404. (when (or cut force-store-markers)
  5405. (org-save-markers-in-region beg end))
  5406. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5407. (setq org-subtree-clip (current-kill 0))
  5408. (message "%s: Subtree(s) with %d characters"
  5409. (if cut "Cut" "Copied")
  5410. (length org-subtree-clip)))))
  5411. (defun org-paste-subtree (&optional level tree for-yank)
  5412. "Paste the clipboard as a subtree, with modification of headline level.
  5413. The entire subtree is promoted or demoted in order to match a new headline
  5414. level.
  5415. If the cursor is at the beginning of a headline, the same level as
  5416. that headline is used to paste the tree
  5417. If not, the new level is derived from the *visible* headings
  5418. before and after the insertion point, and taken to be the inferior headline
  5419. level of the two. So if the previous visible heading is level 3 and the
  5420. next is level 4 (or vice versa), level 4 will be used for insertion.
  5421. This makes sure that the subtree remains an independent subtree and does
  5422. not swallow low level entries.
  5423. You can also force a different level, either by using a numeric prefix
  5424. argument, or by inserting the heading marker by hand. For example, if the
  5425. cursor is after \"*****\", then the tree will be shifted to level 5.
  5426. If optional TREE is given, use this text instead of the kill ring.
  5427. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5428. move back over whitespace before inserting, and move point to the end of
  5429. the inserted text when done."
  5430. (interactive "P")
  5431. (unless (org-kill-is-subtree-p tree)
  5432. (error "%s"
  5433. (substitute-command-keys
  5434. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5435. (let* ((visp (not (org-invisible-p)))
  5436. (txt (or tree (and kill-ring (current-kill 0))))
  5437. (^re (concat "^\\(" outline-regexp "\\)"))
  5438. (re (concat "\\(" outline-regexp "\\)"))
  5439. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5440. (old-level (if (string-match ^re txt)
  5441. (- (match-end 0) (match-beginning 0) 1)
  5442. -1))
  5443. (force-level (cond (level (prefix-numeric-value level))
  5444. ((and (looking-at "[ \t]*$")
  5445. (string-match
  5446. ^re_ (buffer-substring
  5447. (point-at-bol) (point))))
  5448. (- (match-end 1) (match-beginning 1)))
  5449. ((and (bolp)
  5450. (looking-at org-outline-regexp))
  5451. (- (match-end 0) (point) 1))
  5452. (t nil)))
  5453. (previous-level (save-excursion
  5454. (condition-case nil
  5455. (progn
  5456. (outline-previous-visible-heading 1)
  5457. (if (looking-at re)
  5458. (- (match-end 0) (match-beginning 0) 1)
  5459. 1))
  5460. (error 1))))
  5461. (next-level (save-excursion
  5462. (condition-case nil
  5463. (progn
  5464. (or (looking-at outline-regexp)
  5465. (outline-next-visible-heading 1))
  5466. (if (looking-at re)
  5467. (- (match-end 0) (match-beginning 0) 1)
  5468. 1))
  5469. (error 1))))
  5470. (new-level (or force-level (max previous-level next-level)))
  5471. (shift (if (or (= old-level -1)
  5472. (= new-level -1)
  5473. (= old-level new-level))
  5474. 0
  5475. (- new-level old-level)))
  5476. (delta (if (> shift 0) -1 1))
  5477. (func (if (> shift 0) 'org-demote 'org-promote))
  5478. (org-odd-levels-only nil)
  5479. beg end newend)
  5480. ;; Remove the forced level indicator
  5481. (if force-level
  5482. (delete-region (point-at-bol) (point)))
  5483. ;; Paste
  5484. (beginning-of-line 1)
  5485. (unless for-yank (org-back-over-empty-lines))
  5486. (setq beg (point))
  5487. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5488. (insert-before-markers txt)
  5489. (unless (string-match "\n\\'" txt) (insert "\n"))
  5490. (setq newend (point))
  5491. (org-reinstall-markers-in-region beg)
  5492. (setq end (point))
  5493. (goto-char beg)
  5494. (skip-chars-forward " \t\n\r")
  5495. (setq beg (point))
  5496. (if (and (org-invisible-p) visp)
  5497. (save-excursion (outline-show-heading)))
  5498. ;; Shift if necessary
  5499. (unless (= shift 0)
  5500. (save-restriction
  5501. (narrow-to-region beg end)
  5502. (while (not (= shift 0))
  5503. (org-map-region func (point-min) (point-max))
  5504. (setq shift (+ delta shift)))
  5505. (goto-char (point-min))
  5506. (setq newend (point-max))))
  5507. (when (or (interactive-p) for-yank)
  5508. (message "Clipboard pasted as level %d subtree" new-level))
  5509. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5510. kill-ring
  5511. (eq org-subtree-clip (current-kill 0))
  5512. org-subtree-clip-folded)
  5513. ;; The tree was folded before it was killed/copied
  5514. (hide-subtree))
  5515. (and for-yank (goto-char newend))))
  5516. (defun org-kill-is-subtree-p (&optional txt)
  5517. "Check if the current kill is an outline subtree, or a set of trees.
  5518. Returns nil if kill does not start with a headline, or if the first
  5519. headline level is not the largest headline level in the tree.
  5520. So this will actually accept several entries of equal levels as well,
  5521. which is OK for `org-paste-subtree'.
  5522. If optional TXT is given, check this string instead of the current kill."
  5523. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5524. (start-level (and kill
  5525. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5526. org-outline-regexp "\\)")
  5527. kill)
  5528. (- (match-end 2) (match-beginning 2) 1)))
  5529. (re (concat "^" org-outline-regexp))
  5530. (start (1+ (or (match-beginning 2) -1))))
  5531. (if (not start-level)
  5532. (progn
  5533. nil) ;; does not even start with a heading
  5534. (catch 'exit
  5535. (while (setq start (string-match re kill (1+ start)))
  5536. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5537. (throw 'exit nil)))
  5538. t))))
  5539. (defvar org-markers-to-move nil
  5540. "Markers that should be moved with a cut-and-paste operation.
  5541. Those markers are stored together with their positions relative to
  5542. the start of the region.")
  5543. (defun org-save-markers-in-region (beg end)
  5544. "Check markers in region.
  5545. If these markers are between BEG and END, record their position relative
  5546. to BEG, so that after moving the block of text, we can put the markers back
  5547. into place.
  5548. This function gets called just before an entry or tree gets cut from the
  5549. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5550. called immediately, to move the markers with the entries."
  5551. (setq org-markers-to-move nil)
  5552. (when (featurep 'org-clock)
  5553. (org-clock-save-markers-for-cut-and-paste beg end))
  5554. (when (featurep 'org-agenda)
  5555. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5556. (defun org-check-and-save-marker (marker beg end)
  5557. "Check if MARKER is between BEG and END.
  5558. If yes, remember the marker and the distance to BEG."
  5559. (when (and (marker-buffer marker)
  5560. (equal (marker-buffer marker) (current-buffer)))
  5561. (if (and (>= marker beg) (< marker end))
  5562. (push (cons marker (- marker beg)) org-markers-to-move))))
  5563. (defun org-reinstall-markers-in-region (beg)
  5564. "Move all remembered markers to their position relative to BEG."
  5565. (mapc (lambda (x)
  5566. (move-marker (car x) (+ beg (cdr x))))
  5567. org-markers-to-move)
  5568. (setq org-markers-to-move nil))
  5569. (defun org-narrow-to-subtree ()
  5570. "Narrow buffer to the current subtree."
  5571. (interactive)
  5572. (save-excursion
  5573. (save-match-data
  5574. (narrow-to-region
  5575. (progn (org-back-to-heading) (point))
  5576. (progn (org-end-of-subtree t) (point))))))
  5577. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5578. "Clone the task (subtree) at point N times.
  5579. The clones will be inserted as siblings.
  5580. In interactive use, the user will be prompted for the number of clones
  5581. to be produced, and for a time SHIFT, which may be a repeater as used
  5582. in time stamps, for example `+3d'.
  5583. When a valid repeater is given and the entry contains any time stamps,
  5584. the clones will become a sequence in time, with time stamps in the
  5585. subtree shifted for each clone produced. If SHIFT is nil or the
  5586. empty string, time stamps will be left alone.
  5587. If the original subtree did contain time stamps with a repeater,
  5588. the following will happen:
  5589. - the repeater will be removed in each clone
  5590. - an additional clone will be produced, with the current, unshifted
  5591. date(s) in the entry.
  5592. - the original entry will be placed *after* all the clones, with
  5593. repeater intact.
  5594. - the start days in the repeater in the original entry will be shifted
  5595. to past the last clone.
  5596. I this way you can spell out a number of instances of a repeating task,
  5597. and still retain the repeater to cover future instances of the task."
  5598. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5599. (let (beg end template task
  5600. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5601. (if (not (and (integerp n) (> n 0)))
  5602. (error "Invalid number of replications %s" n))
  5603. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5604. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5605. shift)))
  5606. (error "Invalid shift specification %s" shift))
  5607. (when doshift
  5608. (setq shift-n (string-to-number (match-string 1 shift))
  5609. shift-what (cdr (assoc (match-string 2 shift)
  5610. '(("d" . day) ("w" . week)
  5611. ("m" . month) ("y" . year))))))
  5612. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5613. (setq nmin 1 nmax n)
  5614. (org-back-to-heading t)
  5615. (setq beg (point))
  5616. (org-end-of-subtree t t)
  5617. (setq end (point))
  5618. (setq template (buffer-substring beg end))
  5619. (when (and doshift
  5620. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5621. (delete-region beg end)
  5622. (setq end beg)
  5623. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5624. (goto-char end)
  5625. (loop for n from nmin to nmax do
  5626. (if (not doshift)
  5627. (setq task template)
  5628. (with-temp-buffer
  5629. (insert template)
  5630. (org-mode)
  5631. (goto-char (point-min))
  5632. (while (re-search-forward org-ts-regexp nil t)
  5633. (org-timestamp-change (* n shift-n) shift-what))
  5634. (unless (= n n-no-remove)
  5635. (goto-char (point-min))
  5636. (while (re-search-forward org-ts-regexp nil t)
  5637. (save-excursion
  5638. (goto-char (match-beginning 0))
  5639. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5640. (delete-region (match-beginning 1) (match-end 1))))))
  5641. (setq task (buffer-string))))
  5642. (insert task))
  5643. (goto-char beg)))
  5644. ;;; Outline Sorting
  5645. (defun org-sort (with-case)
  5646. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5647. Optional argument WITH-CASE means sort case-sensitively.
  5648. With a double prefix argument, also remove duplicate entries."
  5649. (interactive "P")
  5650. (if (org-at-table-p)
  5651. (org-call-with-arg 'org-table-sort-lines with-case)
  5652. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5653. (defun org-sort-remove-invisible (s)
  5654. (remove-text-properties 0 (length s) org-rm-props s)
  5655. (while (string-match org-bracket-link-regexp s)
  5656. (setq s (replace-match (if (match-end 2)
  5657. (match-string 3 s)
  5658. (match-string 1 s)) t t s)))
  5659. s)
  5660. (defvar org-priority-regexp) ; defined later in the file
  5661. (defvar org-after-sorting-entries-or-items-hook nil
  5662. "Hook that is run after a bunch of entries or items have been sorted.
  5663. When children are sorted, the cursor is in the parent line when this
  5664. hook gets called. When a region or a plain list is sorted, the cursor
  5665. will be in the first entry of the sorted region/list.")
  5666. (defun org-sort-entries-or-items
  5667. (&optional with-case sorting-type getkey-func compare-func property)
  5668. "Sort entries on a certain level of an outline tree, or plain list items.
  5669. If there is an active region, the entries in the region are sorted.
  5670. Else, if the cursor is before the first entry, sort the top-level items.
  5671. Else, the children of the entry at point are sorted.
  5672. If the cursor is at the first item in a plain list, the list items will be
  5673. sorted.
  5674. Sorting can be alphabetically, numerically, by date/time as given by
  5675. a time stamp, by a property or by priority.
  5676. The command prompts for the sorting type unless it has been given to the
  5677. function through the SORTING-TYPE argument, which needs to a character,
  5678. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5679. precise meaning of each character:
  5680. n Numerically, by converting the beginning of the entry/item to a number.
  5681. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5682. t By date/time, either the first active time stamp in the entry, or, if
  5683. none exist, by the first inactive one.
  5684. In items, only the first line will be chekced.
  5685. s By the scheduled date/time.
  5686. d By deadline date/time.
  5687. c By creation time, which is assumed to be the first inactive time stamp
  5688. at the beginning of a line.
  5689. p By priority according to the cookie.
  5690. r By the value of a property.
  5691. Capital letters will reverse the sort order.
  5692. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5693. called with point at the beginning of the record. It must return either
  5694. a string or a number that should serve as the sorting key for that record.
  5695. Comparing entries ignores case by default. However, with an optional argument
  5696. WITH-CASE, the sorting considers case as well."
  5697. (interactive "P")
  5698. (let ((case-func (if with-case 'identity 'downcase))
  5699. start beg end stars re re2
  5700. txt what tmp plain-list-p)
  5701. ;; Find beginning and end of region to sort
  5702. (cond
  5703. ((org-region-active-p)
  5704. ;; we will sort the region
  5705. (setq end (region-end)
  5706. what "region")
  5707. (goto-char (region-beginning))
  5708. (if (not (org-on-heading-p)) (outline-next-heading))
  5709. (setq start (point)))
  5710. ((org-at-item-p)
  5711. ;; we will sort this plain list
  5712. (org-beginning-of-item-list) (setq start (point))
  5713. (org-end-of-item-list) (setq end (point))
  5714. (goto-char start)
  5715. (setq plain-list-p t
  5716. what "plain list"))
  5717. ((or (org-on-heading-p)
  5718. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5719. ;; we will sort the children of the current headline
  5720. (org-back-to-heading)
  5721. (setq start (point)
  5722. end (progn (org-end-of-subtree t t)
  5723. (org-back-over-empty-lines)
  5724. (point))
  5725. what "children")
  5726. (goto-char start)
  5727. (show-subtree)
  5728. (outline-next-heading))
  5729. (t
  5730. ;; we will sort the top-level entries in this file
  5731. (goto-char (point-min))
  5732. (or (org-on-heading-p) (outline-next-heading))
  5733. (setq start (point) end (point-max) what "top-level")
  5734. (goto-char start)
  5735. (show-all)))
  5736. (setq beg (point))
  5737. (if (>= beg end) (error "Nothing to sort"))
  5738. (unless plain-list-p
  5739. (looking-at "\\(\\*+\\)")
  5740. (setq stars (match-string 1)
  5741. re (concat "^" (regexp-quote stars) " +")
  5742. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5743. txt (buffer-substring beg end))
  5744. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5745. (if (and (not (equal stars "*")) (string-match re2 txt))
  5746. (error "Region to sort contains a level above the first entry")))
  5747. (unless sorting-type
  5748. (message
  5749. (if plain-list-p
  5750. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5751. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5752. [t]ime [s]cheduled [d]eadline [c]reated
  5753. A/N/T/S/D/C/P/O/F means reversed:")
  5754. what)
  5755. (setq sorting-type (read-char-exclusive))
  5756. (and (= (downcase sorting-type) ?f)
  5757. (setq getkey-func
  5758. (org-ido-completing-read "Sort using function: "
  5759. obarray 'fboundp t nil nil))
  5760. (setq getkey-func (intern getkey-func)))
  5761. (and (= (downcase sorting-type) ?r)
  5762. (setq property
  5763. (org-ido-completing-read "Property: "
  5764. (mapcar 'list (org-buffer-property-keys t))
  5765. nil t))))
  5766. (message "Sorting entries...")
  5767. (save-restriction
  5768. (narrow-to-region start end)
  5769. (let ((dcst (downcase sorting-type))
  5770. (case-fold-search nil)
  5771. (now (current-time)))
  5772. (sort-subr
  5773. (/= dcst sorting-type)
  5774. ;; This function moves to the beginning character of the "record" to
  5775. ;; be sorted.
  5776. (if plain-list-p
  5777. (lambda nil
  5778. (if (org-at-item-p) t (goto-char (point-max))))
  5779. (lambda nil
  5780. (if (re-search-forward re nil t)
  5781. (goto-char (match-beginning 0))
  5782. (goto-char (point-max)))))
  5783. ;; This function moves to the last character of the "record" being
  5784. ;; sorted.
  5785. (if plain-list-p
  5786. 'org-end-of-item
  5787. (lambda nil
  5788. (save-match-data
  5789. (condition-case nil
  5790. (outline-forward-same-level 1)
  5791. (error
  5792. (goto-char (point-max)))))))
  5793. ;; This function returns the value that gets sorted against.
  5794. (if plain-list-p
  5795. (lambda nil
  5796. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5797. (cond
  5798. ((= dcst ?n)
  5799. (string-to-number (buffer-substring (match-end 0)
  5800. (point-at-eol))))
  5801. ((= dcst ?a)
  5802. (buffer-substring (match-end 0) (point-at-eol)))
  5803. ((= dcst ?t)
  5804. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5805. (re-search-forward org-ts-regexp-both
  5806. (point-at-eol) t))
  5807. (org-time-string-to-seconds (match-string 0))
  5808. (time-to-seconds now)))
  5809. ((= dcst ?f)
  5810. (if getkey-func
  5811. (progn
  5812. (setq tmp (funcall getkey-func))
  5813. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5814. tmp)
  5815. (error "Invalid key function `%s'" getkey-func)))
  5816. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5817. (lambda nil
  5818. (cond
  5819. ((= dcst ?n)
  5820. (if (looking-at org-complex-heading-regexp)
  5821. (string-to-number (match-string 4))
  5822. nil))
  5823. ((= dcst ?a)
  5824. (if (looking-at org-complex-heading-regexp)
  5825. (funcall case-func (match-string 4))
  5826. nil))
  5827. ((= dcst ?t)
  5828. (let ((end (save-excursion (outline-next-heading) (point))))
  5829. (if (or (re-search-forward org-ts-regexp end t)
  5830. (re-search-forward org-ts-regexp-both end t))
  5831. (org-time-string-to-seconds (match-string 0))
  5832. (time-to-seconds now))))
  5833. ((= dcst ?c)
  5834. (let ((end (save-excursion (outline-next-heading) (point))))
  5835. (if (re-search-forward
  5836. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5837. end t)
  5838. (org-time-string-to-seconds (match-string 0))
  5839. (time-to-seconds now))))
  5840. ((= dcst ?s)
  5841. (let ((end (save-excursion (outline-next-heading) (point))))
  5842. (if (re-search-forward org-scheduled-time-regexp end t)
  5843. (org-time-string-to-seconds (match-string 1))
  5844. (time-to-seconds now))))
  5845. ((= dcst ?d)
  5846. (let ((end (save-excursion (outline-next-heading) (point))))
  5847. (if (re-search-forward org-deadline-time-regexp end t)
  5848. (org-time-string-to-seconds (match-string 1))
  5849. (time-to-seconds now))))
  5850. ((= dcst ?p)
  5851. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5852. (string-to-char (match-string 2))
  5853. org-default-priority))
  5854. ((= dcst ?r)
  5855. (or (org-entry-get nil property) ""))
  5856. ((= dcst ?o)
  5857. (if (looking-at org-complex-heading-regexp)
  5858. (- 9999 (length (member (match-string 2)
  5859. org-todo-keywords-1)))))
  5860. ((= dcst ?f)
  5861. (if getkey-func
  5862. (progn
  5863. (setq tmp (funcall getkey-func))
  5864. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5865. tmp)
  5866. (error "Invalid key function `%s'" getkey-func)))
  5867. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5868. nil
  5869. (cond
  5870. ((= dcst ?a) 'string<)
  5871. ((= dcst ?f) compare-func)
  5872. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  5873. (t nil)))))
  5874. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5875. (message "Sorting entries...done")))
  5876. (defun org-do-sort (table what &optional with-case sorting-type)
  5877. "Sort TABLE of WHAT according to SORTING-TYPE.
  5878. The user will be prompted for the SORTING-TYPE if the call to this
  5879. function does not specify it. WHAT is only for the prompt, to indicate
  5880. what is being sorted. The sorting key will be extracted from
  5881. the car of the elements of the table.
  5882. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5883. (unless sorting-type
  5884. (message
  5885. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5886. what)
  5887. (setq sorting-type (read-char-exclusive)))
  5888. (let ((dcst (downcase sorting-type))
  5889. extractfun comparefun)
  5890. ;; Define the appropriate functions
  5891. (cond
  5892. ((= dcst ?n)
  5893. (setq extractfun 'string-to-number
  5894. comparefun (if (= dcst sorting-type) '< '>)))
  5895. ((= dcst ?a)
  5896. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5897. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5898. comparefun (if (= dcst sorting-type)
  5899. 'string<
  5900. (lambda (a b) (and (not (string< a b))
  5901. (not (string= a b)))))))
  5902. ((= dcst ?t)
  5903. (setq extractfun
  5904. (lambda (x)
  5905. (if (or (string-match org-ts-regexp x)
  5906. (string-match org-ts-regexp-both x))
  5907. (time-to-seconds
  5908. (org-time-string-to-time (match-string 0 x)))
  5909. 0))
  5910. comparefun (if (= dcst sorting-type) '< '>)))
  5911. (t (error "Invalid sorting type `%c'" sorting-type)))
  5912. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5913. table)
  5914. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5915. ;;; Editing source examples
  5916. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5917. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5918. (define-key org-exit-edit-mode-map "\C-x\C-s" 'org-edit-src-save)
  5919. (defvar org-edit-src-force-single-line nil)
  5920. (defvar org-edit-src-from-org-mode nil)
  5921. (defvar org-edit-src-picture nil)
  5922. (defvar org-edit-src-beg-marker nil)
  5923. (defvar org-edit-src-end-marker nil)
  5924. (defvar org-edit-src-overlay nil)
  5925. (defvar org-edit-src-nindent nil)
  5926. (define-minor-mode org-exit-edit-mode
  5927. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5928. (defun org-edit-src-code ()
  5929. "Edit the source code example at point.
  5930. The example is copied to a separate buffer, and that buffer is switched
  5931. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  5932. This will remove the original code in the Org buffer, and replace it with
  5933. the edited version."
  5934. (interactive)
  5935. (let ((line (org-current-line))
  5936. (case-fold-search t)
  5937. (msg (substitute-command-keys
  5938. "Edit, then exit with C-c ' (C-c and single quote)"))
  5939. (info (org-edit-src-find-region-and-lang))
  5940. (org-mode-p (eq major-mode 'org-mode))
  5941. (beg (make-marker))
  5942. (end (make-marker))
  5943. nindent ovl lang lang-f single lfmt code begline buffer)
  5944. (if (not info)
  5945. nil
  5946. (setq beg (move-marker beg (nth 0 info))
  5947. end (move-marker end (nth 1 info))
  5948. code (buffer-substring-no-properties beg end)
  5949. lang (nth 2 info)
  5950. single (nth 3 info)
  5951. lfmt (nth 4 info)
  5952. lang-f (intern (concat lang "-mode"))
  5953. begline (save-excursion (goto-char beg) (org-current-line)))
  5954. (unless (functionp lang-f)
  5955. (error "No such language mode: %s" lang-f))
  5956. (goto-line line)
  5957. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  5958. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  5959. (switch-to-buffer buffer)
  5960. (when buffer
  5961. (with-current-buffer buffer
  5962. (if (boundp 'org-edit-src-overlay)
  5963. (org-delete-overlay org-edit-src-overlay)))
  5964. (kill-buffer buffer))
  5965. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  5966. (setq ovl (org-make-overlay beg end))
  5967. (org-overlay-put ovl 'face 'secondary-selection)
  5968. (org-overlay-put ovl 'edit-buffer buffer)
  5969. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  5970. (org-overlay-put ovl 'face 'secondary-selection)
  5971. (org-overlay-put ovl
  5972. 'keymap
  5973. (let ((map (make-sparse-keymap)))
  5974. (define-key map [mouse-1] 'org-edit-src-continue)
  5975. map))
  5976. (org-overlay-put ovl :read-only "Leave me alone")
  5977. (switch-to-buffer buffer)
  5978. (insert code)
  5979. (remove-text-properties (point-min) (point-max)
  5980. '(display nil invisible nil intangible nil))
  5981. (setq nindent (org-do-remove-indentation))
  5982. (let ((org-inhibit-startup t))
  5983. (funcall lang-f))
  5984. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5985. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5986. (when lfmt
  5987. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5988. (when org-mode-p
  5989. (goto-char (point-min))
  5990. (while (re-search-forward "^," nil t)
  5991. (replace-match "")))
  5992. (goto-line (1+ (- line begline)))
  5993. (org-exit-edit-mode)
  5994. (org-set-local 'org-edit-src-beg-marker beg)
  5995. (org-set-local 'org-edit-src-end-marker end)
  5996. (org-set-local 'org-edit-src-overlay ovl)
  5997. (org-set-local 'org-edit-src-nindent nindent)
  5998. (and org-edit-src-persistent-message
  5999. (org-set-local 'header-line-format msg)))
  6000. (message "%s" msg)
  6001. t)))
  6002. (defun org-edit-src-continue (e)
  6003. (interactive "e")
  6004. (mouse-set-point e)
  6005. (let ((buf (get-char-property (point) 'edit-buffer)))
  6006. (if buf (switch-to-buffer buf)
  6007. (error "Something is wrong here"))))
  6008. (defun org-edit-src-find-buffer (beg end)
  6009. "Find a source editing buffer that is already editing the region BEG to END."
  6010. (catch 'exit
  6011. (mapc
  6012. (lambda (b)
  6013. (with-current-buffer b
  6014. (if (and (string-match "\\`*Org Edit " (buffer-name))
  6015. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  6016. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  6017. (equal beg org-edit-src-beg-marker)
  6018. (equal end org-edit-src-end-marker))
  6019. (throw 'exit (current-buffer)))))
  6020. (buffer-list))
  6021. nil))
  6022. (defun org-edit-fixed-width-region ()
  6023. "Edit the fixed-width ascii drawing at point.
  6024. This must be a region where each line starts with a colon followed by
  6025. a space character.
  6026. An new buffer is created and the fixed-width region is copied into it,
  6027. and the buffer is switched into `artist-mode' for editing. When done,
  6028. exit with \\[org-edit-src-exit]. The edited text will then replace
  6029. the fragment in the Org-mode buffer."
  6030. (interactive)
  6031. (let ((line (org-current-line))
  6032. (case-fold-search t)
  6033. (msg (substitute-command-keys
  6034. "Edit, then exit with C-c ' (C-c and single quote)"))
  6035. (org-mode-p (eq major-mode 'org-mode))
  6036. (beg (make-marker))
  6037. (end (make-marker))
  6038. nindent ovl beg1 end1 code begline buffer)
  6039. (beginning-of-line 1)
  6040. (if (looking-at "[ \t]*[^:\n \t]")
  6041. nil
  6042. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  6043. (setq beg1 (point) end1 beg1)
  6044. (save-excursion
  6045. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  6046. (setq beg1 (point-at-bol 2))
  6047. (setq beg1 (point))))
  6048. (save-excursion
  6049. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  6050. (setq end1 (1- (match-beginning 0)))
  6051. (setq end1 (point))))
  6052. (goto-line line))
  6053. (setq beg (move-marker beg beg1)
  6054. end (move-marker end end1)
  6055. code (buffer-substring-no-properties beg end)
  6056. begline (save-excursion (goto-char beg) (org-current-line)))
  6057. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  6058. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  6059. (switch-to-buffer buffer)
  6060. (when buffer
  6061. (with-current-buffer buffer
  6062. (if (boundp 'org-edit-src-overlay)
  6063. (org-delete-overlay org-edit-src-overlay)))
  6064. (kill-buffer buffer))
  6065. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  6066. (setq ovl (org-make-overlay beg end))
  6067. (org-overlay-put ovl 'face 'secondary-selection)
  6068. (org-overlay-put ovl 'edit-buffer buffer)
  6069. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  6070. (org-overlay-put ovl 'face 'secondary-selection)
  6071. (org-overlay-put ovl
  6072. 'keymap
  6073. (let ((map (make-sparse-keymap)))
  6074. (define-key map [mouse-1] 'org-edit-src-continue)
  6075. map))
  6076. (org-overlay-put ovl :read-only "Leave me alone")
  6077. (switch-to-buffer buffer)
  6078. (insert code)
  6079. (remove-text-properties (point-min) (point-max)
  6080. '(display nil invisible nil intangible nil))
  6081. (setq nindent (org-do-remove-indentation))
  6082. (cond
  6083. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  6084. (fundamental-mode)
  6085. (artist-mode 1))
  6086. (t (funcall org-edit-fixed-width-region-mode)))
  6087. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  6088. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  6089. (set (make-local-variable 'org-edit-src-picture) t)
  6090. (goto-char (point-min))
  6091. (while (re-search-forward "^[ \t]*: ?" nil t)
  6092. (replace-match ""))
  6093. (goto-line (1+ (- line begline)))
  6094. (org-exit-edit-mode)
  6095. (org-set-local 'org-edit-src-beg-marker beg)
  6096. (org-set-local 'org-edit-src-end-marker end)
  6097. (org-set-local 'org-edit-src-overlay ovl)
  6098. (org-set-local 'org-edit-src-nindent nindent)
  6099. (and org-edit-src-persistent-message
  6100. (org-set-local 'header-line-format msg)))
  6101. (message "%s" msg)
  6102. t)))
  6103. (defun org-edit-src-find-region-and-lang ()
  6104. "Find the region and language for a local edit.
  6105. Return a list with beginning and end of the region, a string representing
  6106. the language, a switch telling of the content should be in a single line."
  6107. (let ((re-list
  6108. (append
  6109. org-edit-src-region-extra
  6110. '(
  6111. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  6112. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  6113. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  6114. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  6115. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  6116. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  6117. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  6118. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  6119. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  6120. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  6121. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  6122. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  6123. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  6124. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  6125. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  6126. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  6127. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  6128. )))
  6129. (pos (point))
  6130. re1 re2 single beg end lang lfmt match-re1 ind)
  6131. (catch 'exit
  6132. (while (setq entry (pop re-list))
  6133. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  6134. single (nth 3 entry))
  6135. (save-excursion
  6136. (if (or (looking-at re1)
  6137. (re-search-backward re1 nil t))
  6138. (progn
  6139. (setq match-re1 (match-string 0))
  6140. (setq beg (match-end 0)
  6141. lang (org-edit-src-get-lang lang)
  6142. lfmt (org-edit-src-get-label-format match-re1))
  6143. (if (and (re-search-forward re2 nil t)
  6144. (>= (match-end 0) pos))
  6145. (throw 'exit (list beg (match-beginning 0)
  6146. lang single lfmt))))
  6147. (if (or (looking-at re2)
  6148. (re-search-forward re2 nil t))
  6149. (progn
  6150. (setq end (match-beginning 0))
  6151. (if (and (re-search-backward re1 nil t)
  6152. (<= (match-beginning 0) pos))
  6153. (progn
  6154. (setq lfmt (org-edit-src-get-label-format
  6155. (match-string 0)))
  6156. (throw 'exit
  6157. (list (match-end 0) end
  6158. (org-edit-src-get-lang lang)
  6159. single lfmt))))))))))))
  6160. (defun org-edit-src-get-lang (lang)
  6161. "Extract the src language."
  6162. (let ((m (match-string 0)))
  6163. (cond
  6164. ((stringp lang) lang)
  6165. ((integerp lang) (match-string lang))
  6166. ((and (eq lang 'lang)
  6167. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  6168. (match-string 1 m))
  6169. ((and (eq lang 'style)
  6170. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  6171. (match-string 1 m))
  6172. (t "fundamental"))))
  6173. (defun org-edit-src-get-label-format (s)
  6174. "Extract the label format."
  6175. (save-match-data
  6176. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  6177. (match-string 1 s))))
  6178. (defun org-edit-src-exit ()
  6179. "Exit special edit and protect problematic lines."
  6180. (interactive)
  6181. (unless (string-match "\\`*Org Edit " (buffer-name (current-buffer)))
  6182. (error "This is not an sub-editing buffer, something is wrong..."))
  6183. (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line)
  6184. 1
  6185. (org-current-line)))
  6186. (beg org-edit-src-beg-marker)
  6187. (end org-edit-src-end-marker)
  6188. (ovl org-edit-src-overlay)
  6189. (buffer (current-buffer))
  6190. (nindent org-edit-src-nindent)
  6191. code)
  6192. (goto-char (point-min))
  6193. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  6194. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  6195. (when (org-bound-and-true-p org-edit-src-force-single-line)
  6196. (goto-char (point-min))
  6197. (while (re-search-forward "\n" nil t)
  6198. (replace-match " "))
  6199. (goto-char (point-min))
  6200. (if (looking-at "\\s-*") (replace-match " "))
  6201. (if (re-search-forward "\\s-+\\'" nil t)
  6202. (replace-match "")))
  6203. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  6204. (goto-char (point-min))
  6205. (while (re-search-forward
  6206. (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
  6207. (replace-match ",\\1")))
  6208. (when (org-bound-and-true-p org-edit-src-picture)
  6209. (untabify (point-min) (point-max))
  6210. (goto-char (point-min))
  6211. (while (re-search-forward "^" nil t)
  6212. (replace-match ": ")))
  6213. (when nindent
  6214. (setq nindent (make-string nindent ?\ ))
  6215. (goto-char (point-min))
  6216. (while (re-search-forward "^" nil t)
  6217. (replace-match nindent)))
  6218. (setq code (buffer-string))
  6219. (switch-to-buffer (marker-buffer beg))
  6220. (kill-buffer buffer)
  6221. (goto-char beg)
  6222. (org-delete-overlay ovl)
  6223. (delete-region beg end)
  6224. (insert code)
  6225. (goto-char beg)
  6226. (goto-line (1- (+ (org-current-line) line)))
  6227. (move-marker beg nil)
  6228. (move-marker end nil)))
  6229. (defun org-edit-src-save ()
  6230. "Save parent buffer with current state source-code buffer."
  6231. (interactive)
  6232. (let ((p (point)) (m (mark)) msg)
  6233. (org-edit-src-exit)
  6234. (save-buffer)
  6235. (setq msg (current-message))
  6236. (org-edit-src-code)
  6237. (push-mark m 'nomessage)
  6238. (goto-char (min p (point-max)))
  6239. (message (or msg ""))))
  6240. ;;; The orgstruct minor mode
  6241. ;; Define a minor mode which can be used in other modes in order to
  6242. ;; integrate the org-mode structure editing commands.
  6243. ;; This is really a hack, because the org-mode structure commands use
  6244. ;; keys which normally belong to the major mode. Here is how it
  6245. ;; works: The minor mode defines all the keys necessary to operate the
  6246. ;; structure commands, but wraps the commands into a function which
  6247. ;; tests if the cursor is currently at a headline or a plain list
  6248. ;; item. If that is the case, the structure command is used,
  6249. ;; temporarily setting many Org-mode variables like regular
  6250. ;; expressions for filling etc. However, when any of those keys is
  6251. ;; used at a different location, function uses `key-binding' to look
  6252. ;; up if the key has an associated command in another currently active
  6253. ;; keymap (minor modes, major mode, global), and executes that
  6254. ;; command. There might be problems if any of the keys is otherwise
  6255. ;; used as a prefix key.
  6256. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6257. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6258. ;; addresses this by checking explicitly for both bindings.
  6259. (defvar orgstruct-mode-map (make-sparse-keymap)
  6260. "Keymap for the minor `orgstruct-mode'.")
  6261. (defvar org-local-vars nil
  6262. "List of local variables, for use by `orgstruct-mode'")
  6263. ;;;###autoload
  6264. (define-minor-mode orgstruct-mode
  6265. "Toggle the minor more `orgstruct-mode'.
  6266. This mode is for using Org-mode structure commands in other modes.
  6267. The following key behave as if Org-mode was active, if the cursor
  6268. is on a headline, or on a plain list item (both in the definition
  6269. of Org-mode).
  6270. M-up Move entry/item up
  6271. M-down Move entry/item down
  6272. M-left Promote
  6273. M-right Demote
  6274. M-S-up Move entry/item up
  6275. M-S-down Move entry/item down
  6276. M-S-left Promote subtree
  6277. M-S-right Demote subtree
  6278. M-q Fill paragraph and items like in Org-mode
  6279. C-c ^ Sort entries
  6280. C-c - Cycle list bullet
  6281. TAB Cycle item visibility
  6282. M-RET Insert new heading/item
  6283. S-M-RET Insert new TODO heading / Checkbox item
  6284. C-c C-c Set tags / toggle checkbox"
  6285. nil " OrgStruct" nil
  6286. (org-load-modules-maybe)
  6287. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6288. ;;;###autoload
  6289. (defun turn-on-orgstruct ()
  6290. "Unconditionally turn on `orgstruct-mode'."
  6291. (orgstruct-mode 1))
  6292. (defun orgstruct++-mode (&optional arg)
  6293. "Toggle `orgstruct-mode', the enhanced version of it.
  6294. In addition to setting orgstruct-mode, this also exports all indentation and
  6295. autofilling variables from org-mode into the buffer. It will also
  6296. recognize item context in multiline items.
  6297. Note that turning off orgstruct-mode will *not* remove the
  6298. indentation/paragraph settings. This can only be done by refreshing the
  6299. major mode, for example with \[normal-mode]."
  6300. (interactive "P")
  6301. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6302. (if (< arg 1)
  6303. (orgstruct-mode -1)
  6304. (orgstruct-mode 1)
  6305. (let (var val)
  6306. (mapc
  6307. (lambda (x)
  6308. (when (string-match
  6309. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6310. (symbol-name (car x)))
  6311. (setq var (car x) val (nth 1 x))
  6312. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6313. org-local-vars)
  6314. (org-set-local 'orgstruct-is-++ t))))
  6315. (defvar orgstruct-is-++ nil
  6316. "Is orgstruct-mode in ++ version in the current-buffer?")
  6317. (make-variable-buffer-local 'orgstruct-is-++)
  6318. ;;;###autoload
  6319. (defun turn-on-orgstruct++ ()
  6320. "Unconditionally turn on `orgstruct++-mode'."
  6321. (orgstruct++-mode 1))
  6322. (defun orgstruct-error ()
  6323. "Error when there is no default binding for a structure key."
  6324. (interactive)
  6325. (error "This key has no function outside structure elements"))
  6326. (defun orgstruct-setup ()
  6327. "Setup orgstruct keymaps."
  6328. (let ((nfunc 0)
  6329. (bindings
  6330. (list
  6331. '([(meta up)] org-metaup)
  6332. '([(meta down)] org-metadown)
  6333. '([(meta left)] org-metaleft)
  6334. '([(meta right)] org-metaright)
  6335. '([(meta shift up)] org-shiftmetaup)
  6336. '([(meta shift down)] org-shiftmetadown)
  6337. '([(meta shift left)] org-shiftmetaleft)
  6338. '([(meta shift right)] org-shiftmetaright)
  6339. '([?\e (up)] org-metaup)
  6340. '([?\e (down)] org-metadown)
  6341. '([?\e (left)] org-metaleft)
  6342. '([?\e (right)] org-metaright)
  6343. '([?\e (shift up)] org-shiftmetaup)
  6344. '([?\e (shift down)] org-shiftmetadown)
  6345. '([?\e (shift left)] org-shiftmetaleft)
  6346. '([?\e (shift right)] org-shiftmetaright)
  6347. '([(shift up)] org-shiftup)
  6348. '([(shift down)] org-shiftdown)
  6349. '([(shift left)] org-shiftleft)
  6350. '([(shift right)] org-shiftright)
  6351. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6352. '("\M-q" fill-paragraph)
  6353. '("\C-c^" org-sort)
  6354. '("\C-c-" org-cycle-list-bullet)))
  6355. elt key fun cmd)
  6356. (while (setq elt (pop bindings))
  6357. (setq nfunc (1+ nfunc))
  6358. (setq key (org-key (car elt))
  6359. fun (nth 1 elt)
  6360. cmd (orgstruct-make-binding fun nfunc key))
  6361. (org-defkey orgstruct-mode-map key cmd))
  6362. ;; Special treatment needed for TAB and RET
  6363. (org-defkey orgstruct-mode-map [(tab)]
  6364. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6365. (org-defkey orgstruct-mode-map "\C-i"
  6366. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6367. (org-defkey orgstruct-mode-map "\M-\C-m"
  6368. (orgstruct-make-binding 'org-insert-heading 105
  6369. "\M-\C-m" [(meta return)]))
  6370. (org-defkey orgstruct-mode-map [(meta return)]
  6371. (orgstruct-make-binding 'org-insert-heading 106
  6372. [(meta return)] "\M-\C-m"))
  6373. (org-defkey orgstruct-mode-map [(shift meta return)]
  6374. (orgstruct-make-binding 'org-insert-todo-heading 107
  6375. [(meta return)] "\M-\C-m"))
  6376. (org-defkey orgstruct-mode-map "\e\C-m"
  6377. (orgstruct-make-binding 'org-insert-heading 108
  6378. "\e\C-m" [?\e (return)]))
  6379. (org-defkey orgstruct-mode-map [?\e (return)]
  6380. (orgstruct-make-binding 'org-insert-heading 109
  6381. [?\e (return)] "\e\C-m"))
  6382. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6383. (orgstruct-make-binding 'org-insert-todo-heading 110
  6384. [?\e (return)] "\e\C-m"))
  6385. (unless org-local-vars
  6386. (setq org-local-vars (org-get-local-variables)))
  6387. t))
  6388. (defun orgstruct-make-binding (fun n &rest keys)
  6389. "Create a function for binding in the structure minor mode.
  6390. FUN is the command to call inside a table. N is used to create a unique
  6391. command name. KEYS are keys that should be checked in for a command
  6392. to execute outside of tables."
  6393. (eval
  6394. (list 'defun
  6395. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6396. '(arg)
  6397. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6398. "Outside of structure, run the binding of `"
  6399. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6400. "'.")
  6401. '(interactive "p")
  6402. (list 'if
  6403. `(org-context-p 'headline 'item
  6404. (and orgstruct-is-++
  6405. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6406. 'item-body))
  6407. (list 'org-run-like-in-org-mode (list 'quote fun))
  6408. (list 'let '(orgstruct-mode)
  6409. (list 'call-interactively
  6410. (append '(or)
  6411. (mapcar (lambda (k)
  6412. (list 'key-binding k))
  6413. keys)
  6414. '('orgstruct-error))))))))
  6415. (defun org-context-p (&rest contexts)
  6416. "Check if local context is any of CONTEXTS.
  6417. Possible values in the list of contexts are `table', `headline', and `item'."
  6418. (let ((pos (point)))
  6419. (goto-char (point-at-bol))
  6420. (prog1 (or (and (memq 'table contexts)
  6421. (looking-at "[ \t]*|"))
  6422. (and (memq 'headline contexts)
  6423. ;;????????? (looking-at "\\*+"))
  6424. (looking-at outline-regexp))
  6425. (and (memq 'item contexts)
  6426. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6427. (and (memq 'item-body contexts)
  6428. (org-in-item-p)))
  6429. (goto-char pos))))
  6430. (defun org-get-local-variables ()
  6431. "Return a list of all local variables in an org-mode buffer."
  6432. (let (varlist)
  6433. (with-current-buffer (get-buffer-create "*Org tmp*")
  6434. (erase-buffer)
  6435. (org-mode)
  6436. (setq varlist (buffer-local-variables)))
  6437. (kill-buffer "*Org tmp*")
  6438. (delq nil
  6439. (mapcar
  6440. (lambda (x)
  6441. (setq x
  6442. (if (symbolp x)
  6443. (list x)
  6444. (list (car x) (list 'quote (cdr x)))))
  6445. (if (string-match
  6446. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6447. (symbol-name (car x)))
  6448. x nil))
  6449. varlist))))
  6450. ;;;###autoload
  6451. (defun org-run-like-in-org-mode (cmd)
  6452. "Run a command, pretending that the current buffer is in Org-mode.
  6453. This will temporarily bind local variables that are typically bound in
  6454. Org-mode to the values they have in Org-mode, and then interactively
  6455. call CMD."
  6456. (org-load-modules-maybe)
  6457. (unless org-local-vars
  6458. (setq org-local-vars (org-get-local-variables)))
  6459. (eval (list 'let org-local-vars
  6460. (list 'call-interactively (list 'quote cmd)))))
  6461. ;;;; Archiving
  6462. (defun org-get-category (&optional pos)
  6463. "Get the category applying to position POS."
  6464. (get-text-property (or pos (point)) 'org-category))
  6465. (defun org-refresh-category-properties ()
  6466. "Refresh category text properties in the buffer."
  6467. (let ((def-cat (cond
  6468. ((null org-category)
  6469. (if buffer-file-name
  6470. (file-name-sans-extension
  6471. (file-name-nondirectory buffer-file-name))
  6472. "???"))
  6473. ((symbolp org-category) (symbol-name org-category))
  6474. (t org-category)))
  6475. beg end cat pos optionp)
  6476. (org-unmodified
  6477. (save-excursion
  6478. (save-restriction
  6479. (widen)
  6480. (goto-char (point-min))
  6481. (put-text-property (point) (point-max) 'org-category def-cat)
  6482. (while (re-search-forward
  6483. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6484. (setq pos (match-end 0)
  6485. optionp (equal (char-after (match-beginning 0)) ?#)
  6486. cat (org-trim (match-string 2)))
  6487. (if optionp
  6488. (setq beg (point-at-bol) end (point-max))
  6489. (org-back-to-heading t)
  6490. (setq beg (point) end (org-end-of-subtree t t)))
  6491. (put-text-property beg end 'org-category cat)
  6492. (goto-char pos)))))))
  6493. ;;;; Link Stuff
  6494. ;;; Link abbreviations
  6495. (defun org-link-expand-abbrev (link)
  6496. "Apply replacements as defined in `org-link-abbrev-alist."
  6497. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6498. (let* ((key (match-string 1 link))
  6499. (as (or (assoc key org-link-abbrev-alist-local)
  6500. (assoc key org-link-abbrev-alist)))
  6501. (tag (and (match-end 2) (match-string 3 link)))
  6502. rpl)
  6503. (if (not as)
  6504. link
  6505. (setq rpl (cdr as))
  6506. (cond
  6507. ((symbolp rpl) (funcall rpl tag))
  6508. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6509. ((string-match "%h" rpl)
  6510. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6511. (t (concat rpl tag)))))
  6512. link))
  6513. ;;; Storing and inserting links
  6514. (defvar org-insert-link-history nil
  6515. "Minibuffer history for links inserted with `org-insert-link'.")
  6516. (defvar org-stored-links nil
  6517. "Contains the links stored with `org-store-link'.")
  6518. (defvar org-store-link-plist nil
  6519. "Plist with info about the most recently link created with `org-store-link'.")
  6520. (defvar org-link-protocols nil
  6521. "Link protocols added to Org-mode using `org-add-link-type'.")
  6522. (defvar org-store-link-functions nil
  6523. "List of functions that are called to create and store a link.
  6524. Each function will be called in turn until one returns a non-nil
  6525. value. Each function should check if it is responsible for creating
  6526. this link (for example by looking at the major mode).
  6527. If not, it must exit and return nil.
  6528. If yes, it should return a non-nil value after a calling
  6529. `org-store-link-props' with a list of properties and values.
  6530. Special properties are:
  6531. :type The link prefix. like \"http\". This must be given.
  6532. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6533. This is obligatory as well.
  6534. :description Optional default description for the second pair
  6535. of brackets in an Org-mode link. The user can still change
  6536. this when inserting this link into an Org-mode buffer.
  6537. In addition to these, any additional properties can be specified
  6538. and then used in remember templates.")
  6539. (defun org-add-link-type (type &optional follow export)
  6540. "Add TYPE to the list of `org-link-types'.
  6541. Re-compute all regular expressions depending on `org-link-types'
  6542. FOLLOW and EXPORT are two functions.
  6543. FOLLOW should take the link path as the single argument and do whatever
  6544. is necessary to follow the link, for example find a file or display
  6545. a mail message.
  6546. EXPORT should format the link path for export to one of the export formats.
  6547. It should be a function accepting three arguments:
  6548. path the path of the link, the text after the prefix (like \"http:\")
  6549. desc the description of the link, if any, nil if there was no description
  6550. format the export format, a symbol like `html' or `latex'.
  6551. The function may use the FORMAT information to return different values
  6552. depending on the format. The return value will be put literally into
  6553. the exported file.
  6554. Org-mode has a built-in default for exporting links. If you are happy with
  6555. this default, there is no need to define an export function for the link
  6556. type. For a simple example of an export function, see `org-bbdb.el'."
  6557. (add-to-list 'org-link-types type t)
  6558. (org-make-link-regexps)
  6559. (if (assoc type org-link-protocols)
  6560. (setcdr (assoc type org-link-protocols) (list follow export))
  6561. (push (list type follow export) org-link-protocols)))
  6562. ;;;###autoload
  6563. (defun org-store-link (arg)
  6564. "\\<org-mode-map>Store an org-link to the current location.
  6565. This link is added to `org-stored-links' and can later be inserted
  6566. into an org-buffer with \\[org-insert-link].
  6567. For some link types, a prefix arg is interpreted:
  6568. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6569. For file links, arg negates `org-context-in-file-links'."
  6570. (interactive "P")
  6571. (org-load-modules-maybe)
  6572. (setq org-store-link-plist nil) ; reset
  6573. (let (link cpltxt desc description search txt custom-id)
  6574. (cond
  6575. ((run-hook-with-args-until-success 'org-store-link-functions)
  6576. (setq link (plist-get org-store-link-plist :link)
  6577. desc (or (plist-get org-store-link-plist :description) link)))
  6578. ((equal (buffer-name) "*Org Edit Src Example*")
  6579. (let (label gc)
  6580. (while (or (not label)
  6581. (save-excursion
  6582. (save-restriction
  6583. (widen)
  6584. (goto-char (point-min))
  6585. (re-search-forward
  6586. (regexp-quote (format org-coderef-label-format label))
  6587. nil t))))
  6588. (when label (message "Label exists already") (sit-for 2))
  6589. (setq label (read-string "Code line label: " label)))
  6590. (end-of-line 1)
  6591. (setq link (format org-coderef-label-format label))
  6592. (setq gc (- 79 (length link)))
  6593. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6594. (insert link)
  6595. (setq link (concat "(" label ")") desc nil)))
  6596. ((eq major-mode 'calendar-mode)
  6597. (let ((cd (calendar-cursor-to-date)))
  6598. (setq link
  6599. (format-time-string
  6600. (car org-time-stamp-formats)
  6601. (apply 'encode-time
  6602. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6603. nil nil nil))))
  6604. (org-store-link-props :type "calendar" :date cd)))
  6605. ((eq major-mode 'w3-mode)
  6606. (setq cpltxt (if (and (buffer-name)
  6607. (not (string-match "Untitled" (buffer-name))))
  6608. (buffer-name)
  6609. (url-view-url t))
  6610. link (org-make-link (url-view-url t)))
  6611. (org-store-link-props :type "w3" :url (url-view-url t)))
  6612. ((eq major-mode 'w3m-mode)
  6613. (setq cpltxt (or w3m-current-title w3m-current-url)
  6614. link (org-make-link w3m-current-url))
  6615. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6616. ((setq search (run-hook-with-args-until-success
  6617. 'org-create-file-search-functions))
  6618. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6619. "::" search))
  6620. (setq cpltxt (or description link)))
  6621. ((eq major-mode 'image-mode)
  6622. (setq cpltxt (concat "file:"
  6623. (abbreviate-file-name buffer-file-name))
  6624. link (org-make-link cpltxt))
  6625. (org-store-link-props :type "image" :file buffer-file-name))
  6626. ((eq major-mode 'dired-mode)
  6627. ;; link to the file in the current line
  6628. (setq cpltxt (concat "file:"
  6629. (abbreviate-file-name
  6630. (expand-file-name
  6631. (dired-get-filename nil t))))
  6632. link (org-make-link cpltxt)))
  6633. ((and buffer-file-name (org-mode-p))
  6634. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6635. (cond
  6636. ((org-in-regexp "<<\\(.*?\\)>>")
  6637. (setq cpltxt
  6638. (concat "file:"
  6639. (abbreviate-file-name buffer-file-name)
  6640. "::" (match-string 1))
  6641. link (org-make-link cpltxt)))
  6642. ((and (featurep 'org-id)
  6643. (or (eq org-link-to-org-use-id t)
  6644. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6645. (interactive-p))
  6646. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6647. (interactive-p)
  6648. (not custom-id))
  6649. (and org-link-to-org-use-id
  6650. (condition-case nil
  6651. (org-entry-get nil "ID")
  6652. (error nil)))))
  6653. ;; We can make a link using the ID.
  6654. (setq link (condition-case nil
  6655. (prog1 (org-id-store-link)
  6656. (setq desc (plist-get org-store-link-plist
  6657. :description)))
  6658. (error
  6659. ;; probably before first headline, link to file only
  6660. (concat "file:"
  6661. (abbreviate-file-name buffer-file-name))))))
  6662. (t
  6663. ;; Just link to current headline
  6664. (setq cpltxt (concat "file:"
  6665. (abbreviate-file-name buffer-file-name)))
  6666. ;; Add a context search string
  6667. (when (org-xor org-context-in-file-links arg)
  6668. (setq txt (cond
  6669. ((org-on-heading-p) nil)
  6670. ((org-region-active-p)
  6671. (buffer-substring (region-beginning) (region-end)))
  6672. (t nil)))
  6673. (when (or (null txt) (string-match "\\S-" txt))
  6674. (setq cpltxt
  6675. (concat cpltxt "::"
  6676. (condition-case nil
  6677. (org-make-org-heading-search-string txt)
  6678. (error "")))
  6679. desc "NONE")))
  6680. (if (string-match "::\\'" cpltxt)
  6681. (setq cpltxt (substring cpltxt 0 -2)))
  6682. (setq link (org-make-link cpltxt)))))
  6683. ((buffer-file-name (buffer-base-buffer))
  6684. ;; Just link to this file here.
  6685. (setq cpltxt (concat "file:"
  6686. (abbreviate-file-name
  6687. (buffer-file-name (buffer-base-buffer)))))
  6688. ;; Add a context string
  6689. (when (org-xor org-context-in-file-links arg)
  6690. (setq txt (if (org-region-active-p)
  6691. (buffer-substring (region-beginning) (region-end))
  6692. (buffer-substring (point-at-bol) (point-at-eol))))
  6693. ;; Only use search option if there is some text.
  6694. (when (string-match "\\S-" txt)
  6695. (setq cpltxt
  6696. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6697. desc "NONE")))
  6698. (setq link (org-make-link cpltxt)))
  6699. ((interactive-p)
  6700. (error "Cannot link to a buffer which is not visiting a file"))
  6701. (t (setq link nil)))
  6702. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6703. (setq link (or link cpltxt)
  6704. desc (or desc cpltxt))
  6705. (if (equal desc "NONE") (setq desc nil))
  6706. (if (and (interactive-p) link)
  6707. (progn
  6708. (setq org-stored-links
  6709. (cons (list link desc) org-stored-links))
  6710. (message "Stored: %s" (or desc link))
  6711. (when custom-id
  6712. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6713. "::#" custom-id))
  6714. (setq org-stored-links
  6715. (cons (list link desc) org-stored-links))))
  6716. (and link (org-make-link-string link desc)))))
  6717. (defun org-store-link-props (&rest plist)
  6718. "Store link properties, extract names and addresses."
  6719. (let (x adr)
  6720. (when (setq x (plist-get plist :from))
  6721. (setq adr (mail-extract-address-components x))
  6722. (setq plist (plist-put plist :fromname (car adr)))
  6723. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6724. (when (setq x (plist-get plist :to))
  6725. (setq adr (mail-extract-address-components x))
  6726. (setq plist (plist-put plist :toname (car adr)))
  6727. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6728. (let ((from (plist-get plist :from))
  6729. (to (plist-get plist :to)))
  6730. (when (and from to org-from-is-user-regexp)
  6731. (setq plist
  6732. (plist-put plist :fromto
  6733. (if (string-match org-from-is-user-regexp from)
  6734. (concat "to %t")
  6735. (concat "from %f"))))))
  6736. (setq org-store-link-plist plist))
  6737. (defun org-add-link-props (&rest plist)
  6738. "Add these properties to the link property list."
  6739. (let (key value)
  6740. (while plist
  6741. (setq key (pop plist) value (pop plist))
  6742. (setq org-store-link-plist
  6743. (plist-put org-store-link-plist key value)))))
  6744. (defun org-email-link-description (&optional fmt)
  6745. "Return the description part of an email link.
  6746. This takes information from `org-store-link-plist' and formats it
  6747. according to FMT (default from `org-email-link-description-format')."
  6748. (setq fmt (or fmt org-email-link-description-format))
  6749. (let* ((p org-store-link-plist)
  6750. (to (plist-get p :toaddress))
  6751. (from (plist-get p :fromaddress))
  6752. (table
  6753. (list
  6754. (cons "%c" (plist-get p :fromto))
  6755. (cons "%F" (plist-get p :from))
  6756. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6757. (cons "%T" (plist-get p :to))
  6758. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6759. (cons "%s" (plist-get p :subject))
  6760. (cons "%m" (plist-get p :message-id)))))
  6761. (when (string-match "%c" fmt)
  6762. ;; Check if the user wrote this message
  6763. (if (and org-from-is-user-regexp from to
  6764. (save-match-data (string-match org-from-is-user-regexp from)))
  6765. (setq fmt (replace-match "to %t" t t fmt))
  6766. (setq fmt (replace-match "from %f" t t fmt))))
  6767. (org-replace-escapes fmt table)))
  6768. (defun org-make-org-heading-search-string (&optional string heading)
  6769. "Make search string for STRING or current headline."
  6770. (interactive)
  6771. (let ((s (or string (org-get-heading))))
  6772. (unless (and string (not heading))
  6773. ;; We are using a headline, clean up garbage in there.
  6774. (if (string-match org-todo-regexp s)
  6775. (setq s (replace-match "" t t s)))
  6776. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6777. (setq s (replace-match "" t t s)))
  6778. (setq s (org-trim s))
  6779. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6780. org-comment-string "\\)") s)
  6781. (setq s (replace-match "" t t s)))
  6782. (while (string-match org-ts-regexp s)
  6783. (setq s (replace-match "" t t s))))
  6784. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6785. (setq s (replace-match " " t t s)))
  6786. (or string (setq s (concat "*" s))) ; Add * for headlines
  6787. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6788. (defun org-make-link (&rest strings)
  6789. "Concatenate STRINGS."
  6790. (apply 'concat strings))
  6791. (defun org-make-link-string (link &optional description)
  6792. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6793. (unless (string-match "\\S-" link)
  6794. (error "Empty link"))
  6795. (when (stringp description)
  6796. ;; Remove brackets from the description, they are fatal.
  6797. (while (string-match "\\[" description)
  6798. (setq description (replace-match "{" t t description)))
  6799. (while (string-match "\\]" description)
  6800. (setq description (replace-match "}" t t description))))
  6801. (when (equal (org-link-escape link) description)
  6802. ;; No description needed, it is identical
  6803. (setq description nil))
  6804. (when (and (not description)
  6805. (not (equal link (org-link-escape link))))
  6806. (setq description (org-extract-attributes link)))
  6807. (concat "[[" (org-link-escape link) "]"
  6808. (if description (concat "[" description "]") "")
  6809. "]"))
  6810. (defconst org-link-escape-chars
  6811. '((?\ . "%20")
  6812. (?\[ . "%5B")
  6813. (?\] . "%5D")
  6814. (?\340 . "%E0") ; `a
  6815. (?\342 . "%E2") ; ^a
  6816. (?\347 . "%E7") ; ,c
  6817. (?\350 . "%E8") ; `e
  6818. (?\351 . "%E9") ; 'e
  6819. (?\352 . "%EA") ; ^e
  6820. (?\356 . "%EE") ; ^i
  6821. (?\364 . "%F4") ; ^o
  6822. (?\371 . "%F9") ; `u
  6823. (?\373 . "%FB") ; ^u
  6824. (?\; . "%3B")
  6825. (?? . "%3F")
  6826. (?= . "%3D")
  6827. (?+ . "%2B")
  6828. )
  6829. "Association list of escapes for some characters problematic in links.
  6830. This is the list that is used for internal purposes.")
  6831. (defvar org-url-encoding-use-url-hexify nil)
  6832. (defconst org-link-escape-chars-browser
  6833. '((?\ . "%20")) ; 32 for the SPC char
  6834. "Association list of escapes for some characters problematic in links.
  6835. This is the list that is used before handing over to the browser.")
  6836. (defun org-link-escape (text &optional table)
  6837. "Escape characters in TEXT that are problematic for links."
  6838. (if org-url-encoding-use-url-hexify
  6839. (url-hexify-string text)
  6840. (setq table (or table org-link-escape-chars))
  6841. (when text
  6842. (let ((re (mapconcat (lambda (x) (regexp-quote
  6843. (char-to-string (car x))))
  6844. table "\\|")))
  6845. (while (string-match re text)
  6846. (setq text
  6847. (replace-match
  6848. (cdr (assoc (string-to-char (match-string 0 text))
  6849. table))
  6850. t t text)))
  6851. text))))
  6852. (defun org-link-unescape (text &optional table)
  6853. "Reverse the action of `org-link-escape'."
  6854. (if org-url-encoding-use-url-hexify
  6855. (url-unhex-string text)
  6856. (setq table (or table org-link-escape-chars))
  6857. (when text
  6858. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6859. table "\\|")))
  6860. (while (string-match re text)
  6861. (setq text
  6862. (replace-match
  6863. (char-to-string (car (rassoc (match-string 0 text) table)))
  6864. t t text)))
  6865. text))))
  6866. (defun org-xor (a b)
  6867. "Exclusive or."
  6868. (if a (not b) b))
  6869. (defun org-fixup-message-id-for-http (s)
  6870. "Replace special characters in a message id, so it can be used in an http query."
  6871. (while (string-match "<" s)
  6872. (setq s (replace-match "%3C" t t s)))
  6873. (while (string-match ">" s)
  6874. (setq s (replace-match "%3E" t t s)))
  6875. (while (string-match "@" s)
  6876. (setq s (replace-match "%40" t t s)))
  6877. s)
  6878. ;;;###autoload
  6879. (defun org-insert-link-global ()
  6880. "Insert a link like Org-mode does.
  6881. This command can be called in any mode to insert a link in Org-mode syntax."
  6882. (interactive)
  6883. (org-load-modules-maybe)
  6884. (org-run-like-in-org-mode 'org-insert-link))
  6885. (defun org-insert-link (&optional complete-file link-location)
  6886. "Insert a link. At the prompt, enter the link.
  6887. Completion can be used to insert any of the link protocol prefixes like
  6888. http or ftp in use.
  6889. The history can be used to select a link previously stored with
  6890. `org-store-link'. When the empty string is entered (i.e. if you just
  6891. press RET at the prompt), the link defaults to the most recently
  6892. stored link. As SPC triggers completion in the minibuffer, you need to
  6893. use M-SPC or C-q SPC to force the insertion of a space character.
  6894. You will also be prompted for a description, and if one is given, it will
  6895. be displayed in the buffer instead of the link.
  6896. If there is already a link at point, this command will allow you to edit link
  6897. and description parts.
  6898. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6899. be selected using completion. The path to the file will be relative to the
  6900. current directory if the file is in the current directory or a subdirectory.
  6901. Otherwise, the link will be the absolute path as completed in the minibuffer
  6902. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6903. option `org-link-file-path-type'.
  6904. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6905. the current directory or below.
  6906. With three \\[universal-argument] prefixes, negate the meaning of
  6907. `org-keep-stored-link-after-insertion'.
  6908. If `org-make-link-description-function' is non-nil, this function will be
  6909. called with the link target, and the result will be the default
  6910. link description.
  6911. If the LINK-LOCATION parameter is non-nil, this value will be
  6912. used as the link location instead of reading one interactively."
  6913. (interactive "P")
  6914. (let* ((wcf (current-window-configuration))
  6915. (region (if (org-region-active-p)
  6916. (buffer-substring (region-beginning) (region-end))))
  6917. (remove (and region (list (region-beginning) (region-end))))
  6918. (desc region)
  6919. tmphist ; byte-compile incorrectly complains about this
  6920. (link link-location)
  6921. entry file all-prefixes)
  6922. (cond
  6923. (link-location) ; specified by arg, just use it.
  6924. ((org-in-regexp org-bracket-link-regexp 1)
  6925. ;; We do have a link at point, and we are going to edit it.
  6926. (setq remove (list (match-beginning 0) (match-end 0)))
  6927. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6928. (setq link (read-string "Link: "
  6929. (org-link-unescape
  6930. (org-match-string-no-properties 1)))))
  6931. ((or (org-in-regexp org-angle-link-re)
  6932. (org-in-regexp org-plain-link-re))
  6933. ;; Convert to bracket link
  6934. (setq remove (list (match-beginning 0) (match-end 0))
  6935. link (read-string "Link: "
  6936. (org-remove-angle-brackets (match-string 0)))))
  6937. ((member complete-file '((4) (16)))
  6938. ;; Completing read for file names.
  6939. (setq link (org-file-complete-link complete-file)))
  6940. (t
  6941. ;; Read link, with completion for stored links.
  6942. (with-output-to-temp-buffer "*Org Links*"
  6943. (princ "Insert a link.
  6944. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6945. (when org-stored-links
  6946. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6947. (princ (mapconcat
  6948. (lambda (x)
  6949. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6950. (reverse org-stored-links) "\n"))))
  6951. (let ((cw (selected-window)))
  6952. (select-window (get-buffer-window "*Org Links*"))
  6953. (setq truncate-lines t)
  6954. (unless (pos-visible-in-window-p (point-max))
  6955. (org-fit-window-to-buffer))
  6956. (and (window-live-p cw) (select-window cw)))
  6957. ;; Fake a link history, containing the stored links.
  6958. (setq tmphist (append (mapcar 'car org-stored-links)
  6959. org-insert-link-history))
  6960. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6961. (mapcar 'car org-link-abbrev-alist)
  6962. org-link-types))
  6963. (unwind-protect
  6964. (progn
  6965. (setq link
  6966. (let ((org-completion-use-ido nil))
  6967. (org-completing-read
  6968. "Link: "
  6969. (mapcar (lambda (x) (list (concat x ":")))
  6970. all-prefixes)
  6971. nil nil nil
  6972. 'tmphist
  6973. (or (car (car org-stored-links))))))
  6974. (if (or (member link all-prefixes)
  6975. (and (equal ":" (substring link -1))
  6976. (member (substring link 0 -1) all-prefixes)
  6977. (setq link (substring link 0 -1))))
  6978. (setq link (org-link-try-special-completion link))))
  6979. (set-window-configuration wcf)
  6980. (kill-buffer "*Org Links*"))
  6981. (setq entry (assoc link org-stored-links))
  6982. (or entry (push link org-insert-link-history))
  6983. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6984. (not org-keep-stored-link-after-insertion))
  6985. (setq org-stored-links (delq (assoc link org-stored-links)
  6986. org-stored-links)))
  6987. (setq desc (or desc (nth 1 entry)))))
  6988. (if (string-match org-plain-link-re link)
  6989. ;; URL-like link, normalize the use of angular brackets.
  6990. (setq link (org-make-link (org-remove-angle-brackets link))))
  6991. ;; Check if we are linking to the current file with a search option
  6992. ;; If yes, simplify the link by using only the search option.
  6993. (when (and buffer-file-name
  6994. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6995. (let* ((path (match-string 1 link))
  6996. (case-fold-search nil)
  6997. (search (match-string 2 link)))
  6998. (save-match-data
  6999. (if (equal (file-truename buffer-file-name) (file-truename path))
  7000. ;; We are linking to this same file, with a search option
  7001. (setq link search)))))
  7002. ;; Check if we can/should use a relative path. If yes, simplify the link
  7003. (when (string-match "^file:\\(.*\\)" link)
  7004. (let* ((path (match-string 1 link))
  7005. (origpath path)
  7006. (case-fold-search nil))
  7007. (cond
  7008. ((or (eq org-link-file-path-type 'absolute)
  7009. (equal complete-file '(16)))
  7010. (setq path (abbreviate-file-name (expand-file-name path))))
  7011. ((eq org-link-file-path-type 'noabbrev)
  7012. (setq path (expand-file-name path)))
  7013. ((eq org-link-file-path-type 'relative)
  7014. (setq path (file-relative-name path)))
  7015. (t
  7016. (save-match-data
  7017. (if (string-match (concat "^" (regexp-quote
  7018. (file-name-as-directory
  7019. (expand-file-name "."))))
  7020. (expand-file-name path))
  7021. ;; We are linking a file with relative path name.
  7022. (setq path (substring (expand-file-name path)
  7023. (match-end 0)))
  7024. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7025. (setq link (concat "file:" path))
  7026. (if (equal desc origpath)
  7027. (setq desc path))))
  7028. (if org-make-link-description-function
  7029. (setq desc (funcall org-make-link-description-function link desc)))
  7030. (setq desc (read-string "Description: " desc))
  7031. (unless (string-match "\\S-" desc) (setq desc nil))
  7032. (if remove (apply 'delete-region remove))
  7033. (insert (org-make-link-string link desc))))
  7034. (defun org-link-try-special-completion (type)
  7035. "If there is completion support for link type TAPE, offer it."
  7036. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7037. (if (functionp fun)
  7038. (funcall fun)
  7039. (read-string "Link (no completion support): " (concat type ":")))))
  7040. (defun org-file-complete-link (&optional arg)
  7041. "Create a file link using completion."
  7042. (let (file link)
  7043. (setq file (read-file-name "File: "))
  7044. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7045. (pwd1 (file-name-as-directory (abbreviate-file-name
  7046. (expand-file-name ".")))))
  7047. (cond
  7048. ((equal arg '(16))
  7049. (setq link (org-make-link
  7050. "file:"
  7051. (abbreviate-file-name (expand-file-name file)))))
  7052. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7053. (setq link (org-make-link "file:" (match-string 1 file))))
  7054. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7055. (expand-file-name file))
  7056. (setq link (org-make-link
  7057. "file:" (match-string 1 (expand-file-name file)))))
  7058. (t (setq link (org-make-link "file:" file)))))
  7059. link))
  7060. (defun org-completing-read (&rest args)
  7061. "Completing-read with SPACE being a normal character."
  7062. (let ((minibuffer-local-completion-map
  7063. (copy-keymap minibuffer-local-completion-map)))
  7064. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7065. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7066. (apply 'org-ido-completing-read args)))
  7067. (defun org-completing-read-no-ido (&rest args)
  7068. (let (org-completion-use-ido)
  7069. (apply 'org-completing-read args)))
  7070. (defun org-ido-completing-read (&rest args)
  7071. "Completing-read using `ido-mode' speedups if available"
  7072. (if (and org-completion-use-ido
  7073. (fboundp 'ido-completing-read)
  7074. (boundp 'ido-mode) ido-mode
  7075. (listp (second args)))
  7076. (let ((ido-enter-matching-directory nil))
  7077. (apply 'ido-completing-read (concat (car args))
  7078. (mapcar (lambda (x) (car x)) (nth 1 args))
  7079. (cddr args)))
  7080. (apply 'completing-read args)))
  7081. (defun org-extract-attributes (s)
  7082. "Extract the attributes cookie from a string and set as text property."
  7083. (let (a attr (start 0) key value)
  7084. (save-match-data
  7085. (when (string-match "{{\\([^}]+\\)}}$" s)
  7086. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7087. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7088. (setq key (match-string 1 a) value (match-string 2 a)
  7089. start (match-end 0)
  7090. attr (plist-put attr (intern key) value))))
  7091. (org-add-props s nil 'org-attr attr))
  7092. s))
  7093. (defun org-extract-attributes-from-string (tag)
  7094. (let (key value attr)
  7095. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7096. (setq key (match-string 1 tag) value (match-string 2 tag)
  7097. tag (replace-match "" t t tag)
  7098. attr (plist-put attr (intern key) value)))
  7099. (cons tag attr)))
  7100. (defun org-attributes-to-string (plist)
  7101. "Format a property list into an HTML attribute list."
  7102. (let ((s "") key value)
  7103. (while plist
  7104. (setq key (pop plist) value (pop plist))
  7105. (and value
  7106. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7107. s))
  7108. ;;; Opening/following a link
  7109. (defvar org-link-search-failed nil)
  7110. (defun org-next-link ()
  7111. "Move forward to the next link.
  7112. If the link is in hidden text, expose it."
  7113. (interactive)
  7114. (when (and org-link-search-failed (eq this-command last-command))
  7115. (goto-char (point-min))
  7116. (message "Link search wrapped back to beginning of buffer"))
  7117. (setq org-link-search-failed nil)
  7118. (let* ((pos (point))
  7119. (ct (org-context))
  7120. (a (assoc :link ct)))
  7121. (if a (goto-char (nth 2 a)))
  7122. (if (re-search-forward org-any-link-re nil t)
  7123. (progn
  7124. (goto-char (match-beginning 0))
  7125. (if (org-invisible-p) (org-show-context)))
  7126. (goto-char pos)
  7127. (setq org-link-search-failed t)
  7128. (error "No further link found"))))
  7129. (defun org-previous-link ()
  7130. "Move backward to the previous link.
  7131. If the link is in hidden text, expose it."
  7132. (interactive)
  7133. (when (and org-link-search-failed (eq this-command last-command))
  7134. (goto-char (point-max))
  7135. (message "Link search wrapped back to end of buffer"))
  7136. (setq org-link-search-failed nil)
  7137. (let* ((pos (point))
  7138. (ct (org-context))
  7139. (a (assoc :link ct)))
  7140. (if a (goto-char (nth 1 a)))
  7141. (if (re-search-backward org-any-link-re nil t)
  7142. (progn
  7143. (goto-char (match-beginning 0))
  7144. (if (org-invisible-p) (org-show-context)))
  7145. (goto-char pos)
  7146. (setq org-link-search-failed t)
  7147. (error "No further link found"))))
  7148. (defun org-translate-link (s)
  7149. "Translate a link string if a translation function has been defined."
  7150. (if (and org-link-translation-function
  7151. (fboundp org-link-translation-function)
  7152. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7153. (progn
  7154. (setq s (funcall org-link-translation-function
  7155. (match-string 1) (match-string 2)))
  7156. (concat (car s) ":" (cdr s)))
  7157. s))
  7158. (defun org-translate-link-from-planner (type path)
  7159. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7160. This is still an experimental function, your mileage may vary."
  7161. (cond
  7162. ((member type '("http" "https" "news" "ftp"))
  7163. ;; standard Internet links are the same.
  7164. nil)
  7165. ((and (equal type "irc") (string-match "^//" path))
  7166. ;; Planner has two / at the beginning of an irc link, we have 1.
  7167. ;; We should have zero, actually....
  7168. (setq path (substring path 1)))
  7169. ((and (equal type "lisp") (string-match "^/" path))
  7170. ;; Planner has a slash, we do not.
  7171. (setq type "elisp" path (substring path 1)))
  7172. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7173. ;; A typical message link. Planner has the id after the fina slash,
  7174. ;; we separate it with a hash mark
  7175. (setq path (concat (match-string 1 path) "#"
  7176. (org-remove-angle-brackets (match-string 2 path)))))
  7177. )
  7178. (cons type path))
  7179. (defun org-find-file-at-mouse (ev)
  7180. "Open file link or URL at mouse."
  7181. (interactive "e")
  7182. (mouse-set-point ev)
  7183. (org-open-at-point 'in-emacs))
  7184. (defun org-open-at-mouse (ev)
  7185. "Open file link or URL at mouse."
  7186. (interactive "e")
  7187. (mouse-set-point ev)
  7188. (if (eq major-mode 'org-agenda-mode)
  7189. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7190. (org-open-at-point))
  7191. (defvar org-window-config-before-follow-link nil
  7192. "The window configuration before following a link.
  7193. This is saved in case the need arises to restore it.")
  7194. (defvar org-open-link-marker (make-marker)
  7195. "Marker pointing to the location where `org-open-at-point; was called.")
  7196. ;;;###autoload
  7197. (defun org-open-at-point-global ()
  7198. "Follow a link like Org-mode does.
  7199. This command can be called in any mode to follow a link that has
  7200. Org-mode syntax."
  7201. (interactive)
  7202. (org-run-like-in-org-mode 'org-open-at-point))
  7203. ;;;###autoload
  7204. (defun org-open-link-from-string (s &optional arg)
  7205. "Open a link in the string S, as if it was in Org-mode."
  7206. (interactive "sLink: \nP")
  7207. (let ((reference-buffer (current-buffer)))
  7208. (with-temp-buffer
  7209. (let ((org-inhibit-startup t))
  7210. (org-mode)
  7211. (insert s)
  7212. (goto-char (point-min))
  7213. (org-open-at-point arg reference-buffer)))))
  7214. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7215. "Open link at or after point.
  7216. If there is no link at point, this function will search forward up to
  7217. the end of the current line.
  7218. Normally, files will be opened by an appropriate application. If the
  7219. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7220. With a double prefix argument, try to open outside of Emacs, in the
  7221. application the system uses for this file type."
  7222. (interactive "P")
  7223. (org-load-modules-maybe)
  7224. (move-marker org-open-link-marker (point))
  7225. (setq org-window-config-before-follow-link (current-window-configuration))
  7226. (org-remove-occur-highlights nil nil t)
  7227. (cond
  7228. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7229. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7230. (org-footnote-action))
  7231. (t
  7232. (let (type path link line search (pos (point)))
  7233. (catch 'match
  7234. (save-excursion
  7235. (skip-chars-forward "^]\n\r")
  7236. (when (org-in-regexp org-bracket-link-regexp)
  7237. (setq link (org-extract-attributes
  7238. (org-link-unescape (org-match-string-no-properties 1))))
  7239. (while (string-match " *\n *" link)
  7240. (setq link (replace-match " " t t link)))
  7241. (setq link (org-link-expand-abbrev link))
  7242. (cond
  7243. ((or (file-name-absolute-p link)
  7244. (string-match "^\\.\\.?/" link))
  7245. (setq type "file" path link))
  7246. ((string-match org-link-re-with-space3 link)
  7247. (setq type (match-string 1 link) path (match-string 2 link)))
  7248. (t (setq type "thisfile" path link)))
  7249. (throw 'match t)))
  7250. (when (get-text-property (point) 'org-linked-text)
  7251. (setq type "thisfile"
  7252. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7253. (1+ (point)) (point))
  7254. path (buffer-substring
  7255. (previous-single-property-change pos 'org-linked-text)
  7256. (next-single-property-change pos 'org-linked-text)))
  7257. (throw 'match t))
  7258. (save-excursion
  7259. (when (or (org-in-regexp org-angle-link-re)
  7260. (org-in-regexp org-plain-link-re))
  7261. (setq type (match-string 1) path (match-string 2))
  7262. (throw 'match t)))
  7263. (save-excursion
  7264. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7265. (setq type "tags"
  7266. path (match-string 1))
  7267. (while (string-match ":" path)
  7268. (setq path (replace-match "+" t t path)))
  7269. (throw 'match t)))
  7270. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7271. (setq type "tree-match"
  7272. path (match-string 1))
  7273. (throw 'match t)))
  7274. (unless path
  7275. (error "No link found"))
  7276. ;; switch back to reference buffer
  7277. ;; needed when if called in a temporary buffer through
  7278. ;; org-open-link-from-string
  7279. (and reference-buffer (switch-to-buffer reference-buffer))
  7280. ;; Remove any trailing spaces in path
  7281. (if (string-match " +\\'" path)
  7282. (setq path (replace-match "" t t path)))
  7283. (if (and org-link-translation-function
  7284. (fboundp org-link-translation-function))
  7285. ;; Check if we need to translate the link
  7286. (let ((tmp (funcall org-link-translation-function type path)))
  7287. (setq type (car tmp) path (cdr tmp))))
  7288. (cond
  7289. ((assoc type org-link-protocols)
  7290. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7291. ((equal type "mailto")
  7292. (let ((cmd (car org-link-mailto-program))
  7293. (args (cdr org-link-mailto-program)) args1
  7294. (address path) (subject "") a)
  7295. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7296. (setq address (match-string 1 path)
  7297. subject (org-link-escape (match-string 2 path))))
  7298. (while args
  7299. (cond
  7300. ((not (stringp (car args))) (push (pop args) args1))
  7301. (t (setq a (pop args))
  7302. (if (string-match "%a" a)
  7303. (setq a (replace-match address t t a)))
  7304. (if (string-match "%s" a)
  7305. (setq a (replace-match subject t t a)))
  7306. (push a args1))))
  7307. (apply cmd (nreverse args1))))
  7308. ((member type '("http" "https" "ftp" "news"))
  7309. (browse-url (concat type ":" (org-link-escape
  7310. path org-link-escape-chars-browser))))
  7311. ((member type '("message"))
  7312. (browse-url (concat type ":" path)))
  7313. ((string= type "tags")
  7314. (org-tags-view in-emacs path))
  7315. ((string= type "thisfile")
  7316. (if in-emacs
  7317. (switch-to-buffer-other-window
  7318. (org-get-buffer-for-internal-link (current-buffer)))
  7319. (org-mark-ring-push))
  7320. (let ((cmd `(org-link-search
  7321. ,path
  7322. ,(cond ((equal in-emacs '(4)) 'occur)
  7323. ((equal in-emacs '(16)) 'org-occur)
  7324. (t nil))
  7325. ,pos)))
  7326. (condition-case nil (eval cmd)
  7327. (error (progn (widen) (eval cmd))))))
  7328. ((string= type "tree-match")
  7329. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7330. ((string= type "file")
  7331. (if (string-match "::\\([0-9]+\\)\\'" path)
  7332. (setq line (string-to-number (match-string 1 path))
  7333. path (substring path 0 (match-beginning 0)))
  7334. (if (string-match "::\\(.+\\)\\'" path)
  7335. (setq search (match-string 1 path)
  7336. path (substring path 0 (match-beginning 0)))))
  7337. (if (string-match "[*?{]" (file-name-nondirectory path))
  7338. (dired path)
  7339. (org-open-file path in-emacs line search)))
  7340. ((string= type "news")
  7341. (require 'org-gnus)
  7342. (org-gnus-follow-link path))
  7343. ((string= type "shell")
  7344. (let ((cmd path))
  7345. (if (or (not org-confirm-shell-link-function)
  7346. (funcall org-confirm-shell-link-function
  7347. (format "Execute \"%s\" in shell? "
  7348. (org-add-props cmd nil
  7349. 'face 'org-warning))))
  7350. (progn
  7351. (message "Executing %s" cmd)
  7352. (shell-command cmd))
  7353. (error "Abort"))))
  7354. ((string= type "elisp")
  7355. (let ((cmd path))
  7356. (if (or (not org-confirm-elisp-link-function)
  7357. (funcall org-confirm-elisp-link-function
  7358. (format "Execute \"%s\" as elisp? "
  7359. (org-add-props cmd nil
  7360. 'face 'org-warning))))
  7361. (message "%s => %s" cmd
  7362. (if (equal (string-to-char cmd) ?\()
  7363. (eval (read cmd))
  7364. (call-interactively (read cmd))))
  7365. (error "Abort"))))
  7366. (t
  7367. (browse-url-at-point))))))
  7368. (move-marker org-open-link-marker nil)
  7369. (run-hook-with-args 'org-follow-link-hook))
  7370. ;;;; Time estimates
  7371. (defun org-get-effort (&optional pom)
  7372. "Get the effort estimate for the current entry."
  7373. (org-entry-get pom org-effort-property))
  7374. ;;; File search
  7375. (defvar org-create-file-search-functions nil
  7376. "List of functions to construct the right search string for a file link.
  7377. These functions are called in turn with point at the location to
  7378. which the link should point.
  7379. A function in the hook should first test if it would like to
  7380. handle this file type, for example by checking the major-mode or
  7381. the file extension. If it decides not to handle this file, it
  7382. should just return nil to give other functions a chance. If it
  7383. does handle the file, it must return the search string to be used
  7384. when following the link. The search string will be part of the
  7385. file link, given after a double colon, and `org-open-at-point'
  7386. will automatically search for it. If special measures must be
  7387. taken to make the search successful, another function should be
  7388. added to the companion hook `org-execute-file-search-functions',
  7389. which see.
  7390. A function in this hook may also use `setq' to set the variable
  7391. `description' to provide a suggestion for the descriptive text to
  7392. be used for this link when it gets inserted into an Org-mode
  7393. buffer with \\[org-insert-link].")
  7394. (defvar org-execute-file-search-functions nil
  7395. "List of functions to execute a file search triggered by a link.
  7396. Functions added to this hook must accept a single argument, the
  7397. search string that was part of the file link, the part after the
  7398. double colon. The function must first check if it would like to
  7399. handle this search, for example by checking the major-mode or the
  7400. file extension. If it decides not to handle this search, it
  7401. should just return nil to give other functions a chance. If it
  7402. does handle the search, it must return a non-nil value to keep
  7403. other functions from trying.
  7404. Each function can access the current prefix argument through the
  7405. variable `current-prefix-argument'. Note that a single prefix is
  7406. used to force opening a link in Emacs, so it may be good to only
  7407. use a numeric or double prefix to guide the search function.
  7408. In case this is needed, a function in this hook can also restore
  7409. the window configuration before `org-open-at-point' was called using:
  7410. (set-window-configuration org-window-config-before-follow-link)")
  7411. (defun org-link-search (s &optional type avoid-pos)
  7412. "Search for a link search option.
  7413. If S is surrounded by forward slashes, it is interpreted as a
  7414. regular expression. In org-mode files, this will create an `org-occur'
  7415. sparse tree. In ordinary files, `occur' will be used to list matches.
  7416. If the current buffer is in `dired-mode', grep will be used to search
  7417. in all files. If AVOID-POS is given, ignore matches near that position."
  7418. (let ((case-fold-search t)
  7419. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7420. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7421. (append '(("") (" ") ("\t") ("\n"))
  7422. org-emphasis-alist)
  7423. "\\|") "\\)"))
  7424. (pos (point))
  7425. (pre nil) (post nil)
  7426. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7427. (cond
  7428. ;; First check if there are any special
  7429. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7430. ;; Now try the builtin stuff
  7431. ((and (equal (string-to-char s0) ?#)
  7432. (> (length s0) 1)
  7433. (save-excursion
  7434. (goto-char (point-min))
  7435. (and
  7436. (re-search-forward
  7437. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7438. (setq type 'dedicated
  7439. pos (match-beginning 0))))
  7440. ;; There is an exact target for this
  7441. (goto-char pos)
  7442. (org-back-to-heading t)))
  7443. ((save-excursion
  7444. (goto-char (point-min))
  7445. (and
  7446. (re-search-forward
  7447. (concat "<<" (regexp-quote s0) ">>") nil t)
  7448. (setq type 'dedicated
  7449. pos (match-beginning 0))))
  7450. ;; There is an exact target for this
  7451. (goto-char pos))
  7452. ((and (string-match "^(\\(.*\\))$" s0)
  7453. (save-excursion
  7454. (goto-char (point-min))
  7455. (and
  7456. (re-search-forward
  7457. (concat "[^[]" (regexp-quote
  7458. (format org-coderef-label-format
  7459. (match-string 1 s0))))
  7460. nil t)
  7461. (setq type 'dedicated
  7462. pos (1+ (match-beginning 0))))))
  7463. ;; There is a coderef target for this
  7464. (goto-char pos))
  7465. ((string-match "^/\\(.*\\)/$" s)
  7466. ;; A regular expression
  7467. (cond
  7468. ((org-mode-p)
  7469. (org-occur (match-string 1 s)))
  7470. ;;((eq major-mode 'dired-mode)
  7471. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7472. (t (org-do-occur (match-string 1 s)))))
  7473. (t
  7474. ;; A normal search strings
  7475. (when (equal (string-to-char s) ?*)
  7476. ;; Anchor on headlines, post may include tags.
  7477. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7478. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7479. s (substring s 1)))
  7480. (remove-text-properties
  7481. 0 (length s)
  7482. '(face nil mouse-face nil keymap nil fontified nil) s)
  7483. ;; Make a series of regular expressions to find a match
  7484. (setq words (org-split-string s "[ \n\r\t]+")
  7485. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7486. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7487. "\\)" markers)
  7488. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7489. re2a (concat "[ \t\r\n]" re2a_)
  7490. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7491. re4 (concat "[^a-zA-Z_]" re4_)
  7492. re1 (concat pre re2 post)
  7493. re3 (concat pre (if pre re4_ re4) post)
  7494. re5 (concat pre ".*" re4)
  7495. re2 (concat pre re2)
  7496. re2a (concat pre (if pre re2a_ re2a))
  7497. re4 (concat pre (if pre re4_ re4))
  7498. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7499. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7500. re5 "\\)"
  7501. ))
  7502. (cond
  7503. ((eq type 'org-occur) (org-occur reall))
  7504. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7505. (t (goto-char (point-min))
  7506. (setq type 'fuzzy)
  7507. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7508. (org-search-not-self 1 re1 nil t)
  7509. (org-search-not-self 1 re2 nil t)
  7510. (org-search-not-self 1 re2a nil t)
  7511. (org-search-not-self 1 re3 nil t)
  7512. (org-search-not-self 1 re4 nil t)
  7513. (org-search-not-self 1 re5 nil t)
  7514. )
  7515. (goto-char (match-beginning 1))
  7516. (goto-char pos)
  7517. (error "No match")))))
  7518. (t
  7519. ;; Normal string-search
  7520. (goto-char (point-min))
  7521. (if (search-forward s nil t)
  7522. (goto-char (match-beginning 0))
  7523. (error "No match"))))
  7524. (and (org-mode-p) (org-show-context 'link-search))
  7525. type))
  7526. (defun org-search-not-self (group &rest args)
  7527. "Execute `re-search-forward', but only accept matches that do not
  7528. enclose the position of `org-open-link-marker'."
  7529. (let ((m org-open-link-marker))
  7530. (catch 'exit
  7531. (while (apply 're-search-forward args)
  7532. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7533. (goto-char (match-end group))
  7534. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7535. (> (match-beginning 0) (marker-position m))
  7536. (< (match-end 0) (marker-position m)))
  7537. (save-match-data
  7538. (or (not (org-in-regexp
  7539. org-bracket-link-analytic-regexp 1))
  7540. (not (match-end 4)) ; no description
  7541. (and (<= (match-beginning 4) (point))
  7542. (>= (match-end 4) (point))))))
  7543. (throw 'exit (point))))))))
  7544. (defun org-get-buffer-for-internal-link (buffer)
  7545. "Return a buffer to be used for displaying the link target of internal links."
  7546. (cond
  7547. ((not org-display-internal-link-with-indirect-buffer)
  7548. buffer)
  7549. ((string-match "(Clone)$" (buffer-name buffer))
  7550. (message "Buffer is already a clone, not making another one")
  7551. ;; we also do not modify visibility in this case
  7552. buffer)
  7553. (t ; make a new indirect buffer for displaying the link
  7554. (let* ((bn (buffer-name buffer))
  7555. (ibn (concat bn "(Clone)"))
  7556. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7557. (with-current-buffer ib (org-overview))
  7558. ib))))
  7559. (defun org-do-occur (regexp &optional cleanup)
  7560. "Call the Emacs command `occur'.
  7561. If CLEANUP is non-nil, remove the printout of the regular expression
  7562. in the *Occur* buffer. This is useful if the regex is long and not useful
  7563. to read."
  7564. (occur regexp)
  7565. (when cleanup
  7566. (let ((cwin (selected-window)) win beg end)
  7567. (when (setq win (get-buffer-window "*Occur*"))
  7568. (select-window win))
  7569. (goto-char (point-min))
  7570. (when (re-search-forward "match[a-z]+" nil t)
  7571. (setq beg (match-end 0))
  7572. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7573. (setq end (1- (match-beginning 0)))))
  7574. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7575. (goto-char (point-min))
  7576. (select-window cwin))))
  7577. ;;; The mark ring for links jumps
  7578. (defvar org-mark-ring nil
  7579. "Mark ring for positions before jumps in Org-mode.")
  7580. (defvar org-mark-ring-last-goto nil
  7581. "Last position in the mark ring used to go back.")
  7582. ;; Fill and close the ring
  7583. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7584. (loop for i from 1 to org-mark-ring-length do
  7585. (push (make-marker) org-mark-ring))
  7586. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7587. org-mark-ring)
  7588. (defun org-mark-ring-push (&optional pos buffer)
  7589. "Put the current position or POS into the mark ring and rotate it."
  7590. (interactive)
  7591. (setq pos (or pos (point)))
  7592. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7593. (move-marker (car org-mark-ring)
  7594. (or pos (point))
  7595. (or buffer (current-buffer)))
  7596. (message "%s"
  7597. (substitute-command-keys
  7598. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7599. (defun org-mark-ring-goto (&optional n)
  7600. "Jump to the previous position in the mark ring.
  7601. With prefix arg N, jump back that many stored positions. When
  7602. called several times in succession, walk through the entire ring.
  7603. Org-mode commands jumping to a different position in the current file,
  7604. or to another Org-mode file, automatically push the old position
  7605. onto the ring."
  7606. (interactive "p")
  7607. (let (p m)
  7608. (if (eq last-command this-command)
  7609. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7610. (setq p org-mark-ring))
  7611. (setq org-mark-ring-last-goto p)
  7612. (setq m (car p))
  7613. (switch-to-buffer (marker-buffer m))
  7614. (goto-char m)
  7615. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7616. (defun org-remove-angle-brackets (s)
  7617. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7618. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7619. s)
  7620. (defun org-add-angle-brackets (s)
  7621. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7622. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7623. s)
  7624. (defun org-remove-double-quotes (s)
  7625. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7626. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7627. s)
  7628. ;;; Following specific links
  7629. (defun org-follow-timestamp-link ()
  7630. (cond
  7631. ((org-at-date-range-p t)
  7632. (let ((org-agenda-start-on-weekday)
  7633. (t1 (match-string 1))
  7634. (t2 (match-string 2)))
  7635. (setq t1 (time-to-days (org-time-string-to-time t1))
  7636. t2 (time-to-days (org-time-string-to-time t2)))
  7637. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7638. ((org-at-timestamp-p t)
  7639. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7640. (substring (match-string 1) 0 10)))
  7641. 1))
  7642. (t (error "This should not happen"))))
  7643. ;;; Following file links
  7644. (defvar org-wait nil)
  7645. (defun org-open-file (path &optional in-emacs line search)
  7646. "Open the file at PATH.
  7647. First, this expands any special file name abbreviations. Then the
  7648. configuration variable `org-file-apps' is checked if it contains an
  7649. entry for this file type, and if yes, the corresponding command is launched.
  7650. If no application is found, Emacs simply visits the file.
  7651. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7652. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7653. and o use an external application to visit the file.
  7654. Optional LINE specifies a line to go to, optional SEARCH a string to
  7655. search for. If LINE or SEARCH is given, the file will always be
  7656. opened in Emacs.
  7657. If the file does not exist, an error is thrown."
  7658. (setq in-emacs (or in-emacs line search))
  7659. (let* ((file (if (equal path "")
  7660. buffer-file-name
  7661. (substitute-in-file-name (expand-file-name path))))
  7662. (apps (append org-file-apps (org-default-apps)))
  7663. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7664. (dirp (if remp nil (file-directory-p file)))
  7665. (file (if (and dirp org-open-directory-means-index-dot-org)
  7666. (concat (file-name-as-directory file) "index.org")
  7667. file))
  7668. (a-m-a-p (assq 'auto-mode apps))
  7669. (dfile (downcase file))
  7670. (old-buffer (current-buffer))
  7671. (old-pos (point))
  7672. (old-mode major-mode)
  7673. ext cmd)
  7674. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7675. (setq ext (match-string 1 dfile))
  7676. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7677. (setq ext (match-string 1 dfile))))
  7678. (cond
  7679. ((equal in-emacs '(16))
  7680. (setq cmd (cdr (assoc 'system apps))))
  7681. (in-emacs (setq cmd 'emacs))
  7682. (t
  7683. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7684. (and dirp (cdr (assoc 'directory apps)))
  7685. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7686. 'string-match)
  7687. (cdr (assoc ext apps))
  7688. (cdr (assoc t apps))))))
  7689. (when (eq cmd 'system)
  7690. (setq cmd (cdr (assoc 'system apps))))
  7691. (when (eq cmd 'default)
  7692. (setq cmd (cdr (assoc t apps))))
  7693. (when (eq cmd 'mailcap)
  7694. (require 'mailcap)
  7695. (mailcap-parse-mailcaps)
  7696. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7697. (command (mailcap-mime-info mime-type)))
  7698. (if (stringp command)
  7699. (setq cmd command)
  7700. (setq cmd 'emacs))))
  7701. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7702. (not (file-exists-p file))
  7703. (not org-open-non-existing-files))
  7704. (error "No such file: %s" file))
  7705. (cond
  7706. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7707. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7708. (while (string-match "['\"]%s['\"]" cmd)
  7709. (setq cmd (replace-match "%s" t t cmd)))
  7710. (while (string-match "%s" cmd)
  7711. (setq cmd (replace-match
  7712. (save-match-data
  7713. (shell-quote-argument
  7714. (convert-standard-filename file)))
  7715. t t cmd)))
  7716. (save-window-excursion
  7717. (start-process-shell-command cmd nil cmd)
  7718. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7719. ))
  7720. ((or (stringp cmd)
  7721. (eq cmd 'emacs))
  7722. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7723. (widen)
  7724. (if line (goto-line line)
  7725. (if search (org-link-search search))))
  7726. ((consp cmd)
  7727. (let ((file (convert-standard-filename file)))
  7728. (eval cmd)))
  7729. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7730. (and (org-mode-p) (eq old-mode 'org-mode)
  7731. (or (not (equal old-buffer (current-buffer)))
  7732. (not (equal old-pos (point))))
  7733. (org-mark-ring-push old-pos old-buffer))))
  7734. (defun org-default-apps ()
  7735. "Return the default applications for this operating system."
  7736. (cond
  7737. ((eq system-type 'darwin)
  7738. org-file-apps-defaults-macosx)
  7739. ((eq system-type 'windows-nt)
  7740. org-file-apps-defaults-windowsnt)
  7741. (t org-file-apps-defaults-gnu)))
  7742. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7743. "Convert extensions to regular expressions in the cars of LIST.
  7744. Also, weed out any non-string entries, because the return value is used
  7745. only for regexp matching.
  7746. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7747. point to the symbol `emacs', indicating that the file should
  7748. be opened in Emacs."
  7749. (append
  7750. (delq nil
  7751. (mapcar (lambda (x)
  7752. (if (not (stringp (car x)))
  7753. nil
  7754. (if (string-match "\\W" (car x))
  7755. x
  7756. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7757. list))
  7758. (if add-auto-mode
  7759. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7760. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7761. (defun org-file-remote-p (file)
  7762. "Test whether FILE specifies a location on a remote system.
  7763. Return non-nil if the location is indeed remote.
  7764. For example, the filename \"/user@host:/foo\" specifies a location
  7765. on the system \"/user@host:\"."
  7766. (cond ((fboundp 'file-remote-p)
  7767. (file-remote-p file))
  7768. ((fboundp 'tramp-handle-file-remote-p)
  7769. (tramp-handle-file-remote-p file))
  7770. ((and (boundp 'ange-ftp-name-format)
  7771. (string-match (car ange-ftp-name-format) file))
  7772. t)
  7773. (t nil)))
  7774. ;;;; Refiling
  7775. (defun org-get-org-file ()
  7776. "Read a filename, with default directory `org-directory'."
  7777. (let ((default (or org-default-notes-file remember-data-file)))
  7778. (read-file-name (format "File name [%s]: " default)
  7779. (file-name-as-directory org-directory)
  7780. default)))
  7781. (defun org-notes-order-reversed-p ()
  7782. "Check if the current file should receive notes in reversed order."
  7783. (cond
  7784. ((not org-reverse-note-order) nil)
  7785. ((eq t org-reverse-note-order) t)
  7786. ((not (listp org-reverse-note-order)) nil)
  7787. (t (catch 'exit
  7788. (let ((all org-reverse-note-order)
  7789. entry)
  7790. (while (setq entry (pop all))
  7791. (if (string-match (car entry) buffer-file-name)
  7792. (throw 'exit (cdr entry))))
  7793. nil)))))
  7794. (defvar org-refile-target-table nil
  7795. "The list of refile targets, created by `org-refile'.")
  7796. (defvar org-agenda-new-buffers nil
  7797. "Buffers created to visit agenda files.")
  7798. (defun org-get-refile-targets (&optional default-buffer)
  7799. "Produce a table with refile targets."
  7800. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7801. targets txt re files f desc descre fast-path-p level pos0)
  7802. (message "Getting targets...")
  7803. (with-current-buffer (or default-buffer (current-buffer))
  7804. (while (setq entry (pop entries))
  7805. (setq files (car entry) desc (cdr entry))
  7806. (setq fast-path-p nil)
  7807. (cond
  7808. ((null files) (setq files (list (current-buffer))))
  7809. ((eq files 'org-agenda-files)
  7810. (setq files (org-agenda-files 'unrestricted)))
  7811. ((and (symbolp files) (fboundp files))
  7812. (setq files (funcall files)))
  7813. ((and (symbolp files) (boundp files))
  7814. (setq files (symbol-value files))))
  7815. (if (stringp files) (setq files (list files)))
  7816. (cond
  7817. ((eq (car desc) :tag)
  7818. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7819. ((eq (car desc) :todo)
  7820. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7821. ((eq (car desc) :regexp)
  7822. (setq descre (cdr desc)))
  7823. ((eq (car desc) :level)
  7824. (setq descre (concat "^\\*\\{" (number-to-string
  7825. (if org-odd-levels-only
  7826. (1- (* 2 (cdr desc)))
  7827. (cdr desc)))
  7828. "\\}[ \t]")))
  7829. ((eq (car desc) :maxlevel)
  7830. (setq fast-path-p t)
  7831. (setq descre (concat "^\\*\\{1," (number-to-string
  7832. (if org-odd-levels-only
  7833. (1- (* 2 (cdr desc)))
  7834. (cdr desc)))
  7835. "\\}[ \t]")))
  7836. (t (error "Bad refiling target description %s" desc)))
  7837. (while (setq f (pop files))
  7838. (save-excursion
  7839. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7840. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7841. (setq f (expand-file-name f))
  7842. (if (eq org-refile-use-outline-path 'file)
  7843. (push (list (file-name-nondirectory f) f nil nil) targets))
  7844. (save-excursion
  7845. (save-restriction
  7846. (widen)
  7847. (goto-char (point-min))
  7848. (while (re-search-forward descre nil t)
  7849. (goto-char (setq pos0 (point-at-bol)))
  7850. (catch 'next
  7851. (when org-refile-target-verify-function
  7852. (save-match-data
  7853. (or (funcall org-refile-target-verify-function)
  7854. (throw 'next t))))
  7855. (when (looking-at org-complex-heading-regexp)
  7856. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7857. txt (org-link-display-format (match-string 4))
  7858. re (concat "^" (regexp-quote
  7859. (buffer-substring (match-beginning 1)
  7860. (match-end 4)))))
  7861. (if (match-end 5) (setq re (concat re "[ \t]+"
  7862. (regexp-quote
  7863. (match-string 5)))))
  7864. (setq re (concat re "[ \t]*$"))
  7865. (when org-refile-use-outline-path
  7866. (setq txt (mapconcat 'org-protect-slash
  7867. (append
  7868. (if (eq org-refile-use-outline-path 'file)
  7869. (list (file-name-nondirectory
  7870. (buffer-file-name (buffer-base-buffer))))
  7871. (if (eq org-refile-use-outline-path 'full-file-path)
  7872. (list (buffer-file-name (buffer-base-buffer)))))
  7873. (org-get-outline-path fast-path-p level txt)
  7874. (list txt))
  7875. "/")))
  7876. (push (list txt f re (point)) targets)))
  7877. (when (= (point) pos0)
  7878. ;; verification function has not moved point
  7879. (goto-char (point-at-eol))))))))))
  7880. (message "Getting targets...done")
  7881. (nreverse targets)))
  7882. (defun org-protect-slash (s)
  7883. (while (string-match "/" s)
  7884. (setq s (replace-match "\\" t t s)))
  7885. s)
  7886. (defvar org-olpa (make-vector 20 nil))
  7887. (defun org-get-outline-path (&optional fastp level heading)
  7888. "Return the outline path to the current entry, as a list."
  7889. (if fastp
  7890. (progn
  7891. (if (> level 19)
  7892. (error "Outline path failure, more than 19 levels."))
  7893. (loop for i from level upto 19 do
  7894. (aset org-olpa i nil))
  7895. (prog1
  7896. (delq nil (append org-olpa nil))
  7897. (aset org-olpa level heading)))
  7898. (let (rtn)
  7899. (save-excursion
  7900. (while (org-up-heading-safe)
  7901. (when (looking-at org-complex-heading-regexp)
  7902. (push (org-match-string-no-properties 4) rtn)))
  7903. rtn))))
  7904. (defvar org-refile-history nil
  7905. "History for refiling operations.")
  7906. (defvar org-after-refile-insert-hook nil
  7907. "Hook run after `org-refile' has inserted its stuff at the new location.
  7908. Note that this is still *before* the stuff will be removed from
  7909. the *old* location.")
  7910. (defun org-refile (&optional goto default-buffer)
  7911. "Move the entry at point to another heading.
  7912. The list of target headings is compiled using the information in
  7913. `org-refile-targets', which see. This list is created before each use
  7914. and will therefore always be up-to-date.
  7915. At the target location, the entry is filed as a subitem of the target heading.
  7916. Depending on `org-reverse-note-order', the new subitem will either be the
  7917. first or the last subitem.
  7918. If there is an active region, all entries in that region will be moved.
  7919. However, the region must fulfil the requirement that the first heading
  7920. is the first one sets the top-level of the moved text - at most siblings
  7921. below it are allowed.
  7922. With prefix arg GOTO, the command will only visit the target location,
  7923. not actually move anything.
  7924. With a double prefix `C-u C-u', go to the location where the last refiling
  7925. operation has put the subtree.
  7926. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7927. (interactive "P")
  7928. (let* ((cbuf (current-buffer))
  7929. (regionp (org-region-active-p))
  7930. (region-start (and regionp (region-beginning)))
  7931. (region-end (and regionp (region-end)))
  7932. (region-length (and regionp (- region-end region-start)))
  7933. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7934. pos it nbuf file re level reversed)
  7935. (when regionp (goto-char region-start)
  7936. (unless (org-kill-is-subtree-p
  7937. (buffer-substring region-start region-end))
  7938. (error "The region is not a (sequence of) subtree(s)")))
  7939. (if (equal goto '(16))
  7940. (org-refile-goto-last-stored)
  7941. (when (setq it (org-refile-get-location
  7942. (if goto "Goto: " "Refile to: ") default-buffer
  7943. org-refile-allow-creating-parent-nodes))
  7944. (setq file (nth 1 it)
  7945. re (nth 2 it)
  7946. pos (nth 3 it))
  7947. (if (and (not goto)
  7948. pos
  7949. (equal (buffer-file-name) file)
  7950. (if regionp
  7951. (and (>= pos region-start)
  7952. (<= pos region-end))
  7953. (and (>= pos (point))
  7954. (< pos (save-excursion
  7955. (org-end-of-subtree t t))))))
  7956. (error "Cannot refile to position inside the tree or region"))
  7957. (setq nbuf (or (find-buffer-visiting file)
  7958. (find-file-noselect file)))
  7959. (if goto
  7960. (progn
  7961. (switch-to-buffer nbuf)
  7962. (goto-char pos)
  7963. (org-show-context 'org-goto))
  7964. (if regionp
  7965. (progn
  7966. (kill-new (buffer-substring region-start region-end))
  7967. (org-save-markers-in-region region-start region-end))
  7968. (org-copy-subtree 1 nil t))
  7969. (save-excursion
  7970. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7971. (find-file-noselect file))))
  7972. (setq reversed (org-notes-order-reversed-p))
  7973. (save-excursion
  7974. (save-restriction
  7975. (widen)
  7976. (if pos
  7977. (progn
  7978. (goto-char pos)
  7979. (looking-at outline-regexp)
  7980. (setq level (org-get-valid-level (funcall outline-level) 1))
  7981. (goto-char
  7982. (if reversed
  7983. (or (outline-next-heading) (point-max))
  7984. (or (save-excursion (outline-get-next-sibling))
  7985. (org-end-of-subtree t t)
  7986. (point-max)))))
  7987. (setq level 1)
  7988. (if (not reversed)
  7989. (goto-char (point-max))
  7990. (goto-char (point-min))
  7991. (or (outline-next-heading) (goto-char (point-max)))))
  7992. (if (not (bolp)) (newline))
  7993. (bookmark-set "org-refile-last-stored")
  7994. (org-paste-subtree level)
  7995. (run-hooks 'org-after-refile-insert-hook))))
  7996. (if regionp
  7997. (delete-region (point) (+ (point) region-length))
  7998. (org-cut-subtree))
  7999. (setq org-markers-to-move nil)
  8000. (message "Refiled to \"%s\"" (car it))))))
  8001. (org-reveal))
  8002. (defun org-refile-goto-last-stored ()
  8003. "Go to the location where the last refile was stored."
  8004. (interactive)
  8005. (bookmark-jump "org-refile-last-stored")
  8006. (message "This is the location of the last refile"))
  8007. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8008. "Prompt the user for a refile location, using PROMPT."
  8009. (let ((org-refile-targets org-refile-targets)
  8010. (org-refile-use-outline-path org-refile-use-outline-path))
  8011. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8012. (unless org-refile-target-table
  8013. (error "No refile targets"))
  8014. (let* ((cbuf (current-buffer))
  8015. (partial-completion-mode nil)
  8016. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8017. (cfunc (if (and org-refile-use-outline-path
  8018. org-outline-path-complete-in-steps)
  8019. 'org-olpath-completing-read
  8020. 'org-ido-completing-read))
  8021. (extra (if org-refile-use-outline-path "/" ""))
  8022. (filename (and cfn (expand-file-name cfn)))
  8023. (tbl (mapcar
  8024. (lambda (x)
  8025. (if (and (not (member org-refile-use-outline-path
  8026. '(file full-file-path)))
  8027. (not (equal filename (nth 1 x))))
  8028. (cons (concat (car x) extra " ("
  8029. (file-name-nondirectory (nth 1 x)) ")")
  8030. (cdr x))
  8031. (cons (concat (car x) extra) (cdr x))))
  8032. org-refile-target-table))
  8033. (completion-ignore-case t)
  8034. pa answ parent-target child parent)
  8035. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8036. nil 'org-refile-history))
  8037. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8038. (if pa
  8039. pa
  8040. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8041. (setq parent (match-string 1 answ)
  8042. child (match-string 2 answ))
  8043. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8044. (when (and parent-target
  8045. (or (eq new-nodes t)
  8046. (and (eq new-nodes 'confirm)
  8047. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8048. (org-refile-new-child parent-target child))))))
  8049. (defun org-refile-new-child (parent-target child)
  8050. "Use refile target PARENT-TARGET to add new CHILD below it."
  8051. (unless parent-target
  8052. (error "Cannot find parent for new node"))
  8053. (let ((file (nth 1 parent-target))
  8054. (pos (nth 3 parent-target))
  8055. level)
  8056. (with-current-buffer (or (find-buffer-visiting file)
  8057. (find-file-noselect file))
  8058. (save-excursion
  8059. (save-restriction
  8060. (widen)
  8061. (if pos
  8062. (goto-char pos)
  8063. (goto-char (point-max))
  8064. (if (not (bolp)) (newline)))
  8065. (when (looking-at outline-regexp)
  8066. (setq level (funcall outline-level))
  8067. (org-end-of-subtree t t))
  8068. (org-back-over-empty-lines)
  8069. (insert "\n" (make-string
  8070. (if pos (org-get-valid-level level 1) 1) ?*)
  8071. " " child "\n")
  8072. (beginning-of-line 0)
  8073. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8074. (defun org-olpath-completing-read (prompt collection &rest args)
  8075. "Read an outline path like a file name."
  8076. (let ((thetable collection)
  8077. (org-completion-use-ido nil)) ; does not work with ido.
  8078. (apply
  8079. 'org-ido-completing-read prompt
  8080. (lambda (string predicate &optional flag)
  8081. (let (rtn r f (l (length string)))
  8082. (cond
  8083. ((eq flag nil)
  8084. ;; try completion
  8085. (try-completion string thetable))
  8086. ((eq flag t)
  8087. ;; all-completions
  8088. (setq rtn (all-completions string thetable predicate))
  8089. (mapcar
  8090. (lambda (x)
  8091. (setq r (substring x l))
  8092. (if (string-match " ([^)]*)$" x)
  8093. (setq f (match-string 0 x))
  8094. (setq f ""))
  8095. (if (string-match "/" r)
  8096. (concat string (substring r 0 (match-end 0)) f)
  8097. x))
  8098. rtn))
  8099. ((eq flag 'lambda)
  8100. ;; exact match?
  8101. (assoc string thetable)))
  8102. ))
  8103. args)))
  8104. ;;;; Dynamic blocks
  8105. (defun org-find-dblock (name)
  8106. "Find the first dynamic block with name NAME in the buffer.
  8107. If not found, stay at current position and return nil."
  8108. (let (pos)
  8109. (save-excursion
  8110. (goto-char (point-min))
  8111. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8112. nil t)
  8113. (match-beginning 0))))
  8114. (if pos (goto-char pos))
  8115. pos))
  8116. (defconst org-dblock-start-re
  8117. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8118. "Matches the startline of a dynamic block, with parameters.")
  8119. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  8120. "Matches the end of a dynamic block.")
  8121. (defun org-create-dblock (plist)
  8122. "Create a dynamic block section, with parameters taken from PLIST.
  8123. PLIST must contain a :name entry which is used as name of the block."
  8124. (unless (bolp) (newline))
  8125. (let ((name (plist-get plist :name)))
  8126. (insert "#+BEGIN: " name)
  8127. (while plist
  8128. (if (eq (car plist) :name)
  8129. (setq plist (cddr plist))
  8130. (insert " " (prin1-to-string (pop plist)))))
  8131. (insert "\n\n#+END:\n")
  8132. (beginning-of-line -2)))
  8133. (defun org-prepare-dblock ()
  8134. "Prepare dynamic block for refresh.
  8135. This empties the block, puts the cursor at the insert position and returns
  8136. the property list including an extra property :name with the block name."
  8137. (unless (looking-at org-dblock-start-re)
  8138. (error "Not at a dynamic block"))
  8139. (let* ((begdel (1+ (match-end 0)))
  8140. (name (org-no-properties (match-string 1)))
  8141. (params (append (list :name name)
  8142. (read (concat "(" (match-string 3) ")")))))
  8143. (unless (re-search-forward org-dblock-end-re nil t)
  8144. (error "Dynamic block not terminated"))
  8145. (setq params
  8146. (append params
  8147. (list :content (buffer-substring
  8148. begdel (match-beginning 0)))))
  8149. (delete-region begdel (match-beginning 0))
  8150. (goto-char begdel)
  8151. (open-line 1)
  8152. params))
  8153. (defun org-map-dblocks (&optional command)
  8154. "Apply COMMAND to all dynamic blocks in the current buffer.
  8155. If COMMAND is not given, use `org-update-dblock'."
  8156. (let ((cmd (or command 'org-update-dblock))
  8157. pos)
  8158. (save-excursion
  8159. (goto-char (point-min))
  8160. (while (re-search-forward org-dblock-start-re nil t)
  8161. (goto-char (setq pos (match-beginning 0)))
  8162. (condition-case nil
  8163. (funcall cmd)
  8164. (error (message "Error during update of dynamic block")))
  8165. (goto-char pos)
  8166. (unless (re-search-forward org-dblock-end-re nil t)
  8167. (error "Dynamic block not terminated"))))))
  8168. (defun org-dblock-update (&optional arg)
  8169. "User command for updating dynamic blocks.
  8170. Update the dynamic block at point. With prefix ARG, update all dynamic
  8171. blocks in the buffer."
  8172. (interactive "P")
  8173. (if arg
  8174. (org-update-all-dblocks)
  8175. (or (looking-at org-dblock-start-re)
  8176. (org-beginning-of-dblock))
  8177. (org-update-dblock)))
  8178. (defun org-update-dblock ()
  8179. "Update the dynamic block at point
  8180. This means to empty the block, parse for parameters and then call
  8181. the correct writing function."
  8182. (save-window-excursion
  8183. (let* ((pos (point))
  8184. (line (org-current-line))
  8185. (params (org-prepare-dblock))
  8186. (name (plist-get params :name))
  8187. (cmd (intern (concat "org-dblock-write:" name))))
  8188. (message "Updating dynamic block `%s' at line %d..." name line)
  8189. (funcall cmd params)
  8190. (message "Updating dynamic block `%s' at line %d...done" name line)
  8191. (goto-char pos))))
  8192. (defun org-beginning-of-dblock ()
  8193. "Find the beginning of the dynamic block at point.
  8194. Error if there is no such block at point."
  8195. (let ((pos (point))
  8196. beg)
  8197. (end-of-line 1)
  8198. (if (and (re-search-backward org-dblock-start-re nil t)
  8199. (setq beg (match-beginning 0))
  8200. (re-search-forward org-dblock-end-re nil t)
  8201. (> (match-end 0) pos))
  8202. (goto-char beg)
  8203. (goto-char pos)
  8204. (error "Not in a dynamic block"))))
  8205. (defun org-update-all-dblocks ()
  8206. "Update all dynamic blocks in the buffer.
  8207. This function can be used in a hook."
  8208. (when (org-mode-p)
  8209. (org-map-dblocks 'org-update-dblock)))
  8210. ;;;; Completion
  8211. (defconst org-additional-option-like-keywords
  8212. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  8213. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  8214. "LATEX_HEADER:"
  8215. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8216. "BEGIN_QUOTE" "END_QUOTE"
  8217. "BEGIN_VERSE" "END_VERSE"
  8218. "BEGIN_CENTER" "END_CENTER"
  8219. "BEGIN_SRC" "END_SRC"
  8220. "CATEGORY" "COLUMNS"
  8221. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  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. (mapcar
  8349. (lambda (x)
  8350. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8351. (cons (match-string 2 x) (match-string 1 x)))
  8352. (org-split-string (org-get-current-options) "\n"))
  8353. (mapcar 'list org-additional-option-like-keywords)))
  8354. (startup
  8355. (setq type :startup)
  8356. org-startup-options)
  8357. (link (append org-link-abbrev-alist-local
  8358. org-link-abbrev-alist))
  8359. (texp
  8360. (setq type :tex)
  8361. org-html-entities)
  8362. ((string-match "\\`\\*+[ \t]+\\'"
  8363. (buffer-substring (point-at-bol) beg))
  8364. (setq type :todo)
  8365. (mapcar 'list org-todo-keywords-1))
  8366. (searchhead
  8367. (setq type :searchhead)
  8368. (save-excursion
  8369. (goto-char (point-min))
  8370. (while (re-search-forward org-todo-line-regexp nil t)
  8371. (push (list
  8372. (org-make-org-heading-search-string
  8373. (match-string 3) t))
  8374. tbl)))
  8375. tbl)
  8376. (tag (setq type :tag beg beg1)
  8377. (or org-tag-alist (org-get-buffer-tags)))
  8378. (prop (setq type :prop beg beg1)
  8379. (mapcar 'list (org-buffer-property-keys nil t t)))
  8380. (t (progn
  8381. (call-interactively org-completion-fallback-command)
  8382. (throw 'exit nil)))))
  8383. (pattern (buffer-substring-no-properties beg end))
  8384. (completion (try-completion pattern table confirm)))
  8385. (cond ((eq completion t)
  8386. (if (not (assoc (upcase pattern) table))
  8387. (message "Already complete")
  8388. (if (and (equal type :opt)
  8389. (not (member (car (assoc (upcase pattern) table))
  8390. org-additional-option-like-keywords)))
  8391. (insert (substring (cdr (assoc (upcase pattern) table))
  8392. (length pattern)))
  8393. (if (memq type '(:tag :prop)) (insert ":")))))
  8394. ((null completion)
  8395. (message "Can't find completion for \"%s\"" pattern)
  8396. (ding))
  8397. ((not (string= pattern completion))
  8398. (delete-region beg end)
  8399. (if (string-match " +$" completion)
  8400. (setq completion (replace-match "" t t completion)))
  8401. (insert completion)
  8402. (if (get-buffer-window "*Completions*")
  8403. (delete-window (get-buffer-window "*Completions*")))
  8404. (if (assoc completion table)
  8405. (if (eq type :todo) (insert " ")
  8406. (if (memq type '(:tag :prop)) (insert ":"))))
  8407. (if (and (equal type :opt) (assoc completion table))
  8408. (message "%s" (substitute-command-keys
  8409. "Press \\[org-complete] again to insert example settings"))))
  8410. (t
  8411. (message "Making completion list...")
  8412. (let ((list (sort (all-completions pattern table confirm)
  8413. 'string<)))
  8414. (with-output-to-temp-buffer "*Completions*"
  8415. (condition-case nil
  8416. ;; Protection needed for XEmacs and emacs 21
  8417. (display-completion-list list pattern)
  8418. (error (display-completion-list list)))))
  8419. (message "Making completion list...%s" "done")))))))
  8420. ;;;; TODO, DEADLINE, Comments
  8421. (defun org-toggle-comment ()
  8422. "Change the COMMENT state of an entry."
  8423. (interactive)
  8424. (save-excursion
  8425. (org-back-to-heading)
  8426. (let (case-fold-search)
  8427. (if (looking-at (concat outline-regexp
  8428. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8429. (replace-match "" t t nil 1)
  8430. (if (looking-at outline-regexp)
  8431. (progn
  8432. (goto-char (match-end 0))
  8433. (insert org-comment-string " ")))))))
  8434. (defvar org-last-todo-state-is-todo nil
  8435. "This is non-nil when the last TODO state change led to a TODO state.
  8436. If the last change removed the TODO tag or switched to DONE, then
  8437. this is nil.")
  8438. (defvar org-setting-tags nil) ; dynamically skipped
  8439. (defun org-parse-local-options (string var)
  8440. "Parse STRING for startup setting relevant for variable VAR."
  8441. (let ((rtn (symbol-value var))
  8442. e opts)
  8443. (save-match-data
  8444. (if (or (not string) (not (string-match "\\S-" string)))
  8445. rtn
  8446. (setq opts (delq nil (mapcar (lambda (x)
  8447. (setq e (assoc x org-startup-options))
  8448. (if (eq (nth 1 e) var) e nil))
  8449. (org-split-string string "[ \t]+"))))
  8450. (if (not opts)
  8451. rtn
  8452. (setq rtn nil)
  8453. (while (setq e (pop opts))
  8454. (if (not (nth 3 e))
  8455. (setq rtn (nth 2 e))
  8456. (if (not (listp rtn)) (setq rtn nil))
  8457. (push (nth 2 e) rtn)))
  8458. rtn)))))
  8459. (defvar org-todo-setup-filter-hook nil
  8460. "Hook for functions that pre-filter todo specs.
  8461. Each function takes a todo spec and returns either `nil' or the spec
  8462. transformed into canonical form." )
  8463. (defvar org-todo-get-default-hook nil
  8464. "Hook for functions that get a default item for todo.
  8465. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8466. `nil' or a string to be used for the todo mark." )
  8467. (defvar org-agenda-headline-snapshot-before-repeat)
  8468. (defun org-todo (&optional arg)
  8469. "Change the TODO state of an item.
  8470. The state of an item is given by a keyword at the start of the heading,
  8471. like
  8472. *** TODO Write paper
  8473. *** DONE Call mom
  8474. The different keywords are specified in the variable `org-todo-keywords'.
  8475. By default the available states are \"TODO\" and \"DONE\".
  8476. So for this example: when the item starts with TODO, it is changed to DONE.
  8477. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8478. DONE are present, add TODO at the beginning of the heading.
  8479. With C-u prefix arg, use completion to determine the new state.
  8480. With numeric prefix arg, switch to that state.
  8481. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8482. With a tripple C-u prefix, circumvent any state blocking.
  8483. For calling through lisp, arg is also interpreted in the following way:
  8484. 'none -> empty state
  8485. \"\"(empty string) -> switch to empty state
  8486. 'done -> switch to DONE
  8487. 'nextset -> switch to the next set of keywords
  8488. 'previousset -> switch to the previous set of keywords
  8489. \"WAITING\" -> switch to the specified keyword, but only if it
  8490. really is a member of `org-todo-keywords'."
  8491. (interactive "P")
  8492. (if (equal arg '(16)) (setq arg 'nextset))
  8493. (let ((org-blocker-hook org-blocker-hook)
  8494. (case-fold-search nil))
  8495. (when (equal arg '(64))
  8496. (setq arg nil org-blocker-hook nil))
  8497. (when (and org-blocker-hook
  8498. (org-entry-get nil "NOBLOCKING"))
  8499. (setq org-blocker-hook nil))
  8500. (save-excursion
  8501. (catch 'exit
  8502. (org-back-to-heading)
  8503. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8504. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8505. (looking-at " *"))
  8506. (let* ((match-data (match-data))
  8507. (startpos (point-at-bol))
  8508. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8509. (org-log-done org-log-done)
  8510. (org-log-repeat org-log-repeat)
  8511. (org-todo-log-states org-todo-log-states)
  8512. (this (match-string 1))
  8513. (hl-pos (match-beginning 0))
  8514. (head (org-get-todo-sequence-head this))
  8515. (ass (assoc head org-todo-kwd-alist))
  8516. (interpret (nth 1 ass))
  8517. (done-word (nth 3 ass))
  8518. (final-done-word (nth 4 ass))
  8519. (last-state (or this ""))
  8520. (completion-ignore-case t)
  8521. (member (member this org-todo-keywords-1))
  8522. (tail (cdr member))
  8523. (state (cond
  8524. ((and org-todo-key-trigger
  8525. (or (and (equal arg '(4))
  8526. (eq org-use-fast-todo-selection 'prefix))
  8527. (and (not arg) org-use-fast-todo-selection
  8528. (not (eq org-use-fast-todo-selection
  8529. 'prefix)))))
  8530. ;; Use fast selection
  8531. (org-fast-todo-selection))
  8532. ((and (equal arg '(4))
  8533. (or (not org-use-fast-todo-selection)
  8534. (not org-todo-key-trigger)))
  8535. ;; Read a state with completion
  8536. (org-ido-completing-read
  8537. "State: " (mapcar (lambda(x) (list x))
  8538. org-todo-keywords-1)
  8539. nil t))
  8540. ((eq arg 'right)
  8541. (if this
  8542. (if tail (car tail) nil)
  8543. (car org-todo-keywords-1)))
  8544. ((eq arg 'left)
  8545. (if (equal member org-todo-keywords-1)
  8546. nil
  8547. (if this
  8548. (nth (- (length org-todo-keywords-1)
  8549. (length tail) 2)
  8550. org-todo-keywords-1)
  8551. (org-last org-todo-keywords-1))))
  8552. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8553. (setq arg nil))) ; hack to fall back to cycling
  8554. (arg
  8555. ;; user or caller requests a specific state
  8556. (cond
  8557. ((equal arg "") nil)
  8558. ((eq arg 'none) nil)
  8559. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8560. ((eq arg 'nextset)
  8561. (or (car (cdr (member head org-todo-heads)))
  8562. (car org-todo-heads)))
  8563. ((eq arg 'previousset)
  8564. (let ((org-todo-heads (reverse org-todo-heads)))
  8565. (or (car (cdr (member head org-todo-heads)))
  8566. (car org-todo-heads))))
  8567. ((car (member arg org-todo-keywords-1)))
  8568. ((nth (1- (prefix-numeric-value arg))
  8569. org-todo-keywords-1))))
  8570. ((null member) (or head (car org-todo-keywords-1)))
  8571. ((equal this final-done-word) nil) ;; -> make empty
  8572. ((null tail) nil) ;; -> first entry
  8573. ((memq interpret '(type priority))
  8574. (if (eq this-command last-command)
  8575. (car tail)
  8576. (if (> (length tail) 0)
  8577. (or done-word (car org-done-keywords))
  8578. nil)))
  8579. (t
  8580. (car tail))))
  8581. (state (or
  8582. (run-hook-with-args-until-success
  8583. 'org-todo-get-default-hook state last-state)
  8584. state))
  8585. (next (if state (concat " " state " ") " "))
  8586. (change-plist (list :type 'todo-state-change :from this :to state
  8587. :position startpos))
  8588. dolog now-done-p)
  8589. (when org-blocker-hook
  8590. (setq org-last-todo-state-is-todo
  8591. (not (member this org-done-keywords)))
  8592. (unless (save-excursion
  8593. (save-match-data
  8594. (run-hook-with-args-until-failure
  8595. 'org-blocker-hook change-plist)))
  8596. (if (interactive-p)
  8597. (error "TODO state change from %s to %s blocked" this state)
  8598. ;; fail silently
  8599. (message "TODO state change from %s to %s blocked" this state)
  8600. (throw 'exit nil))))
  8601. (store-match-data match-data)
  8602. (replace-match next t t)
  8603. (unless (pos-visible-in-window-p hl-pos)
  8604. (message "TODO state changed to %s" (org-trim next)))
  8605. (unless head
  8606. (setq head (org-get-todo-sequence-head state)
  8607. ass (assoc head org-todo-kwd-alist)
  8608. interpret (nth 1 ass)
  8609. done-word (nth 3 ass)
  8610. final-done-word (nth 4 ass)))
  8611. (when (memq arg '(nextset previousset))
  8612. (message "Keyword-Set %d/%d: %s"
  8613. (- (length org-todo-sets) -1
  8614. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8615. (length org-todo-sets)
  8616. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8617. (setq org-last-todo-state-is-todo
  8618. (not (member state org-done-keywords)))
  8619. (setq now-done-p (and (member state org-done-keywords)
  8620. (not (member this org-done-keywords))))
  8621. (and logging (org-local-logging logging))
  8622. (when (and (or org-todo-log-states org-log-done)
  8623. (not org-inhibit-logging)
  8624. (not (memq arg '(nextset previousset))))
  8625. ;; we need to look at recording a time and note
  8626. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8627. (nth 2 (assoc this org-todo-log-states))))
  8628. (when (and state
  8629. (member state org-not-done-keywords)
  8630. (not (member this org-not-done-keywords)))
  8631. ;; This is now a todo state and was not one before
  8632. ;; If there was a CLOSED time stamp, get rid of it.
  8633. (org-add-planning-info nil nil 'closed))
  8634. (when (and now-done-p org-log-done)
  8635. ;; It is now done, and it was not done before
  8636. (org-add-planning-info 'closed (org-current-time))
  8637. (if (and (not dolog) (eq 'note org-log-done))
  8638. (org-add-log-setup 'done state this 'findpos 'note)))
  8639. (when (and state dolog)
  8640. ;; This is a non-nil state, and we need to log it
  8641. (org-add-log-setup 'state state this 'findpos dolog)))
  8642. ;; Fixup tag positioning
  8643. (org-todo-trigger-tag-changes state)
  8644. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8645. (when org-provide-todo-statistics
  8646. (org-update-parent-todo-statistics))
  8647. (run-hooks 'org-after-todo-state-change-hook)
  8648. (if (and arg (not (member state org-done-keywords)))
  8649. (setq head (org-get-todo-sequence-head state)))
  8650. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8651. ;; Do we need to trigger a repeat?
  8652. (when now-done-p
  8653. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8654. ;; This is for the agenda, take a snapshot of the headline.
  8655. (save-match-data
  8656. (setq org-agenda-headline-snapshot-before-repeat
  8657. (org-get-heading))))
  8658. (org-auto-repeat-maybe state))
  8659. ;; Fixup cursor location if close to the keyword
  8660. (if (and (outline-on-heading-p)
  8661. (not (bolp))
  8662. (save-excursion (beginning-of-line 1)
  8663. (looking-at org-todo-line-regexp))
  8664. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8665. (progn
  8666. (goto-char (or (match-end 2) (match-end 1)))
  8667. (and (looking-at " ") (just-one-space))))
  8668. (when org-trigger-hook
  8669. (save-excursion
  8670. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8671. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8672. "Block turning an entry into a TODO, using the hierarchy.
  8673. This checks whether the current task should be blocked from state
  8674. changes. Such blocking occurs when:
  8675. 1. The task has children which are not all in a completed state.
  8676. 2. A task has a parent with the property :ORDERED:, and there
  8677. are siblings prior to the current task with incomplete
  8678. status.
  8679. 3. The parent of the task is blocked because it has siblings that should
  8680. be done first, or is child of a block grandparent TODO entry."
  8681. (catch 'dont-block
  8682. ;; If this is not a todo state change, or if this entry is already DONE,
  8683. ;; do not block
  8684. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8685. (member (plist-get change-plist :from)
  8686. (cons 'done org-done-keywords))
  8687. (member (plist-get change-plist :to)
  8688. (cons 'todo org-not-done-keywords)))
  8689. (throw 'dont-block t))
  8690. ;; If this task has children, and any are undone, it's blocked
  8691. (save-excursion
  8692. (org-back-to-heading t)
  8693. (let ((this-level (funcall outline-level)))
  8694. (outline-next-heading)
  8695. (let ((child-level (funcall outline-level)))
  8696. (while (and (not (eobp))
  8697. (> child-level this-level))
  8698. ;; this todo has children, check whether they are all
  8699. ;; completed
  8700. (if (and (not (org-entry-is-done-p))
  8701. (org-entry-is-todo-p))
  8702. (throw 'dont-block nil))
  8703. (outline-next-heading)
  8704. (setq child-level (funcall outline-level))))))
  8705. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8706. ;; any previous siblings are undone, it's blocked
  8707. (save-excursion
  8708. (org-back-to-heading t)
  8709. (let* ((pos (point))
  8710. (parent-pos (and (org-up-heading-safe) (point))))
  8711. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8712. (when (and (org-entry-get (point) "ORDERED")
  8713. (forward-line 1)
  8714. (re-search-forward org-not-done-heading-regexp pos t))
  8715. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8716. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8717. (while t
  8718. (goto-char parent-pos)
  8719. (if (not (looking-at org-not-done-heading-regexp))
  8720. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8721. (setq pos (point))
  8722. (setq parent-pos (and (org-up-heading-safe) (point)))
  8723. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8724. (when (and (org-entry-get (point) "ORDERED")
  8725. (forward-line 1)
  8726. (re-search-forward org-not-done-heading-regexp pos t))
  8727. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8728. (defcustom org-track-ordered-property-with-tag nil
  8729. "Should the ORDERED property also be shown as a tag?
  8730. The ORDERED property decides if an entry should require subtasks to be
  8731. completed in sequence. Since a property is not very visible, setting
  8732. this option means that toggling the ORDERED property with the command
  8733. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8734. not relevant for the behavior, but it makes things more visible.
  8735. Note that toggling the tag with tags commands will not change the property
  8736. and therefore not influence behavior!
  8737. This can be t, meaning the tag ORDERED should be used, It can also be a
  8738. string to select a different tag for this task."
  8739. :group 'org-todo
  8740. :type '(choice
  8741. (const :tag "No tracking" nil)
  8742. (const :tag "Track with ORDERED tag" t)
  8743. (string :tag "Use other tag")))
  8744. (defun org-toggle-ordered-property ()
  8745. "Toggle the ORDERED property of the current entry.
  8746. For better visibility, you can track the value of this property with a tag.
  8747. See variable `org-track-ordered-property-with-tag'."
  8748. (interactive)
  8749. (let* ((t1 org-track-ordered-property-with-tag)
  8750. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8751. (save-excursion
  8752. (org-back-to-heading)
  8753. (if (org-entry-get nil "ORDERED")
  8754. (progn
  8755. (org-delete-property "ORDERED")
  8756. (and tag (org-toggle-tag tag 'off))
  8757. (message "Subtasks can be completed in arbitrary order"))
  8758. (org-entry-put nil "ORDERED" "t")
  8759. (and tag (org-toggle-tag tag 'on))
  8760. (message "Subtasks must be completed in sequence")))))
  8761. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8762. (defun org-block-todo-from-checkboxes (change-plist)
  8763. "Block turning an entry into a TODO, using checkboxes.
  8764. This checks whether the current task should be blocked from state
  8765. changes because there are uncheckd boxes in this entry."
  8766. (catch 'dont-block
  8767. ;; If this is not a todo state change, or if this entry is already DONE,
  8768. ;; do not block
  8769. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8770. (member (plist-get change-plist :from)
  8771. (cons 'done org-done-keywords))
  8772. (member (plist-get change-plist :to)
  8773. (cons 'todo org-not-done-keywords)))
  8774. (throw 'dont-block t))
  8775. ;; If this task has checkboxes that are not checked, it's blocked
  8776. (save-excursion
  8777. (org-back-to-heading t)
  8778. (let ((beg (point)) end)
  8779. (outline-next-heading)
  8780. (setq end (point))
  8781. (goto-char beg)
  8782. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8783. end t)
  8784. (progn
  8785. (if (boundp 'org-blocked-by-checkboxes)
  8786. (setq org-blocked-by-checkboxes t))
  8787. (throw 'dont-block nil)))))
  8788. t)) ; do not block
  8789. (defvar org-entry-property-inherited-from) ;; defined below
  8790. (defun org-update-parent-todo-statistics ()
  8791. "Update any statistics cookie in the parent of the current headline.
  8792. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8793. the entire subtree and this will travel up the hierarchy and update
  8794. statistics everywhere."
  8795. (interactive)
  8796. (let* ((lim 0) prop
  8797. (recursive (or (not org-hierarchical-todo-statistics)
  8798. (string-match
  8799. "\\<recursive\\>"
  8800. (or (setq prop (org-entry-get
  8801. nil "COOKIE_DATA" 'inherit)) ""))))
  8802. (lim (or (and prop (marker-position
  8803. org-entry-property-inherited-from))
  8804. lim))
  8805. (first t)
  8806. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8807. level ltoggle l1
  8808. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8809. (catch 'exit
  8810. (save-excursion
  8811. (beginning-of-line 1)
  8812. (if (org-at-heading-p)
  8813. (setq ltoggle (funcall outline-level))
  8814. (error "This should not happen"))
  8815. (while (and (setq level (org-up-heading-safe))
  8816. (or recursive first)
  8817. (>= (point) lim))
  8818. (setq first nil)
  8819. (unless (and level
  8820. (not (string-match
  8821. "\\<checkbox\\>"
  8822. (downcase
  8823. (or (org-entry-get
  8824. nil "COOKIE_DATA")
  8825. "")))))
  8826. (throw 'exit nil))
  8827. (while (re-search-forward box-re (point-at-eol) t)
  8828. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8829. (setq is-percent (match-end 2))
  8830. (save-match-data
  8831. (unless (outline-next-heading) (throw 'exit nil))
  8832. (while (and (looking-at org-complex-heading-regexp)
  8833. (> (setq l1 (length (match-string 1))) level))
  8834. (setq kwd (and (or recursive (= l1 ltoggle))
  8835. (match-string 2)))
  8836. (and kwd (setq cnt-all (1+ cnt-all)))
  8837. (and (member kwd org-done-keywords)
  8838. (setq cnt-done (1+ cnt-done)))
  8839. (outline-next-heading)))
  8840. (replace-match
  8841. (if is-percent
  8842. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8843. (format "[%d/%d]" cnt-done cnt-all)))))
  8844. (when cookie-present
  8845. (run-hook-with-args 'org-after-todo-statistics-hook
  8846. cnt-done (- cnt-all cnt-done)))))
  8847. (run-hooks 'org-todo-statistics-hook)))
  8848. (defvar org-after-todo-statistics-hook nil
  8849. "Hook that is called after a TODO statistics cookie has been updated.
  8850. Each function is called with two arguments: the number of not-done entries
  8851. and the number of done entries.
  8852. For example, the following function, when added to this hook, will switch
  8853. an entry to DONE when all children are done, and back to TODO when new
  8854. entries are set to a TODO status. Note that this hook is only called
  8855. when there is a statistics cookie in the headline!
  8856. (defun org-summary-todo (n-done n-not-done)
  8857. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8858. (let (org-log-done org-log-states) ; turn off logging
  8859. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8860. ")
  8861. (defvar org-todo-statistics-hook nil
  8862. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8863. This hook runs even if there is no statisics cookie present, in which case
  8864. `org-after-todo-statistics-hook' would not run.")
  8865. (defun org-todo-trigger-tag-changes (state)
  8866. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8867. (let ((l org-todo-state-tags-triggers)
  8868. changes)
  8869. (when (or (not state) (equal state ""))
  8870. (setq changes (append changes (cdr (assoc "" l)))))
  8871. (when (and (stringp state) (> (length state) 0))
  8872. (setq changes (append changes (cdr (assoc state l)))))
  8873. (when (member state org-not-done-keywords)
  8874. (setq changes (append changes (cdr (assoc 'todo l)))))
  8875. (when (member state org-done-keywords)
  8876. (setq changes (append changes (cdr (assoc 'done l)))))
  8877. (dolist (c changes)
  8878. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8879. (defun org-local-logging (value)
  8880. "Get logging settings from a property VALUE."
  8881. (let* (words w a)
  8882. ;; directly set the variables, they are already local.
  8883. (setq org-log-done nil
  8884. org-log-repeat nil
  8885. org-todo-log-states nil)
  8886. (setq words (org-split-string value))
  8887. (while (setq w (pop words))
  8888. (cond
  8889. ((setq a (assoc w org-startup-options))
  8890. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8891. (set (nth 1 a) (nth 2 a))))
  8892. ((setq a (org-extract-log-state-settings w))
  8893. (and (member (car a) org-todo-keywords-1)
  8894. (push a org-todo-log-states)))))))
  8895. (defun org-get-todo-sequence-head (kwd)
  8896. "Return the head of the TODO sequence to which KWD belongs.
  8897. If KWD is not set, check if there is a text property remembering the
  8898. right sequence."
  8899. (let (p)
  8900. (cond
  8901. ((not kwd)
  8902. (or (get-text-property (point-at-bol) 'org-todo-head)
  8903. (progn
  8904. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8905. nil (point-at-eol)))
  8906. (get-text-property p 'org-todo-head))))
  8907. ((not (member kwd org-todo-keywords-1))
  8908. (car org-todo-keywords-1))
  8909. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8910. (defun org-fast-todo-selection ()
  8911. "Fast TODO keyword selection with single keys.
  8912. Returns the new TODO keyword, or nil if no state change should occur."
  8913. (let* ((fulltable org-todo-key-alist)
  8914. (done-keywords org-done-keywords) ;; needed for the faces.
  8915. (maxlen (apply 'max (mapcar
  8916. (lambda (x)
  8917. (if (stringp (car x)) (string-width (car x)) 0))
  8918. fulltable)))
  8919. (expert nil)
  8920. (fwidth (+ maxlen 3 1 3))
  8921. (ncol (/ (- (window-width) 4) fwidth))
  8922. tg cnt e c tbl
  8923. groups ingroup)
  8924. (save-excursion
  8925. (save-window-excursion
  8926. (if expert
  8927. (set-buffer (get-buffer-create " *Org todo*"))
  8928. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8929. (erase-buffer)
  8930. (org-set-local 'org-done-keywords done-keywords)
  8931. (setq tbl fulltable cnt 0)
  8932. (while (setq e (pop tbl))
  8933. (cond
  8934. ((equal e '(:startgroup))
  8935. (push '() groups) (setq ingroup t)
  8936. (when (not (= cnt 0))
  8937. (setq cnt 0)
  8938. (insert "\n"))
  8939. (insert "{ "))
  8940. ((equal e '(:endgroup))
  8941. (setq ingroup nil cnt 0)
  8942. (insert "}\n"))
  8943. ((equal e '(:newline))
  8944. (when (not (= cnt 0))
  8945. (setq cnt 0)
  8946. (insert "\n")
  8947. (setq e (car tbl))
  8948. (while (equal (car tbl) '(:newline))
  8949. (insert "\n")
  8950. (setq tbl (cdr tbl)))))
  8951. (t
  8952. (setq tg (car e) c (cdr e))
  8953. (if ingroup (push tg (car groups)))
  8954. (setq tg (org-add-props tg nil 'face
  8955. (org-get-todo-face tg)))
  8956. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8957. (insert "[" c "] " tg (make-string
  8958. (- fwidth 4 (length tg)) ?\ ))
  8959. (when (= (setq cnt (1+ cnt)) ncol)
  8960. (insert "\n")
  8961. (if ingroup (insert " "))
  8962. (setq cnt 0)))))
  8963. (insert "\n")
  8964. (goto-char (point-min))
  8965. (if (not expert) (org-fit-window-to-buffer))
  8966. (message "[a-z..]:Set [SPC]:clear")
  8967. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8968. (cond
  8969. ((or (= c ?\C-g)
  8970. (and (= c ?q) (not (rassoc c fulltable))))
  8971. (setq quit-flag t))
  8972. ((= c ?\ ) nil)
  8973. ((setq e (rassoc c fulltable) tg (car e))
  8974. tg)
  8975. (t (setq quit-flag t)))))))
  8976. (defun org-entry-is-todo-p ()
  8977. (member (org-get-todo-state) org-not-done-keywords))
  8978. (defun org-entry-is-done-p ()
  8979. (member (org-get-todo-state) org-done-keywords))
  8980. (defun org-get-todo-state ()
  8981. (save-excursion
  8982. (org-back-to-heading t)
  8983. (and (looking-at org-todo-line-regexp)
  8984. (match-end 2)
  8985. (match-string 2))))
  8986. (defun org-at-date-range-p (&optional inactive-ok)
  8987. "Is the cursor inside a date range?"
  8988. (interactive)
  8989. (save-excursion
  8990. (catch 'exit
  8991. (let ((pos (point)))
  8992. (skip-chars-backward "^[<\r\n")
  8993. (skip-chars-backward "<[")
  8994. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8995. (>= (match-end 0) pos)
  8996. (throw 'exit t))
  8997. (skip-chars-backward "^<[\r\n")
  8998. (skip-chars-backward "<[")
  8999. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9000. (>= (match-end 0) pos)
  9001. (throw 'exit t)))
  9002. nil)))
  9003. (defun org-get-repeat ()
  9004. "Check if there is a deadline/schedule with repeater in this entry."
  9005. (save-match-data
  9006. (save-excursion
  9007. (org-back-to-heading t)
  9008. (if (re-search-forward
  9009. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  9010. (match-string 1)))))
  9011. (defvar org-last-changed-timestamp)
  9012. (defvar org-last-inserted-timestamp)
  9013. (defvar org-log-post-message)
  9014. (defvar org-log-note-purpose)
  9015. (defvar org-log-note-how)
  9016. (defvar org-log-note-extra)
  9017. (defun org-auto-repeat-maybe (done-word)
  9018. "Check if the current headline contains a repeated deadline/schedule.
  9019. If yes, set TODO state back to what it was and change the base date
  9020. of repeating deadline/scheduled time stamps to new date.
  9021. This function is run automatically after each state change to a DONE state."
  9022. ;; last-state is dynamically scoped into this function
  9023. (let* ((repeat (org-get-repeat))
  9024. (aa (assoc last-state org-todo-kwd-alist))
  9025. (interpret (nth 1 aa))
  9026. (head (nth 2 aa))
  9027. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9028. (msg "Entry repeats: ")
  9029. (org-log-done nil)
  9030. (org-todo-log-states nil)
  9031. (nshiftmax 10) (nshift 0)
  9032. re type n what ts time)
  9033. (when repeat
  9034. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9035. (org-todo (if (eq interpret 'type) last-state head))
  9036. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9037. (org-time-stamp-format t t)))
  9038. (when org-log-repeat
  9039. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9040. (memq 'org-add-log-note post-command-hook))
  9041. ;; OK, we are already setup for some record
  9042. (if (eq org-log-repeat 'note)
  9043. ;; make sure we take a note, not only a time stamp
  9044. (setq org-log-note-how 'note))
  9045. ;; Set up for taking a record
  9046. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9047. last-state
  9048. 'findpos org-log-repeat)))
  9049. (org-back-to-heading t)
  9050. (org-add-planning-info nil nil 'closed)
  9051. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9052. org-deadline-time-regexp "\\)\\|\\("
  9053. org-ts-regexp "\\)"))
  9054. (while (re-search-forward
  9055. re (save-excursion (outline-next-heading) (point)) t)
  9056. (setq type (if (match-end 1) org-scheduled-string
  9057. (if (match-end 3) org-deadline-string "Plain:"))
  9058. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9059. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9060. (setq n (string-to-number (match-string 2 ts))
  9061. what (match-string 3 ts))
  9062. (if (equal what "w") (setq n (* n 7) what "d"))
  9063. ;; Preparation, see if we need to modify the start date for the change
  9064. (when (match-end 1)
  9065. (setq time (save-match-data (org-time-string-to-time ts)))
  9066. (cond
  9067. ((equal (match-string 1 ts) ".")
  9068. ;; Shift starting date to today
  9069. (org-timestamp-change
  9070. (- (time-to-days (current-time)) (time-to-days time))
  9071. 'day))
  9072. ((equal (match-string 1 ts) "+")
  9073. (while (or (= nshift 0)
  9074. (<= (time-to-days time) (time-to-days (current-time))))
  9075. (when (= (incf nshift) nshiftmax)
  9076. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9077. (error "Abort")))
  9078. (org-timestamp-change n (cdr (assoc what whata)))
  9079. (org-at-timestamp-p t)
  9080. (setq ts (match-string 1))
  9081. (setq time (save-match-data (org-time-string-to-time ts))))
  9082. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9083. ;; rematch, so that we have everything in place for the real shift
  9084. (org-at-timestamp-p t)
  9085. (setq ts (match-string 1))
  9086. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9087. (org-timestamp-change n (cdr (assoc what whata)))
  9088. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9089. (setq org-log-post-message msg)
  9090. (message "%s" msg))))
  9091. (defun org-show-todo-tree (arg)
  9092. "Make a compact tree which shows all headlines marked with TODO.
  9093. The tree will show the lines where the regexp matches, and all higher
  9094. headlines above the match.
  9095. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9096. With a numeric prefix N, construct a sparse tree for the Nth element
  9097. of `org-todo-keywords-1'."
  9098. (interactive "P")
  9099. (let ((case-fold-search nil)
  9100. (kwd-re
  9101. (cond ((null arg) org-not-done-regexp)
  9102. ((equal arg '(4))
  9103. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  9104. (mapcar 'list org-todo-keywords-1))))
  9105. (concat "\\("
  9106. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9107. "\\)\\>")))
  9108. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9109. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9110. org-todo-keywords-1)))
  9111. (t (error "Invalid prefix argument: %s" arg)))))
  9112. (message "%d TODO entries found"
  9113. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9114. (defun org-deadline (&optional remove time)
  9115. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9116. With argument REMOVE, remove any deadline from the item.
  9117. When TIME is set, it should be an internal time specification, and the
  9118. scheduling will use the corresponding date."
  9119. (interactive "P")
  9120. (if remove
  9121. (progn
  9122. (org-remove-timestamp-with-keyword org-deadline-string)
  9123. (message "Item no longer has a deadline."))
  9124. (if (org-get-repeat)
  9125. (error "Cannot change deadline on task with repeater, please do that by hand")
  9126. (org-add-planning-info 'deadline time 'closed)
  9127. (message "Deadline on %s" org-last-inserted-timestamp))))
  9128. (defun org-schedule (&optional remove time)
  9129. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9130. With argument REMOVE, remove any scheduling date from the item.
  9131. When TIME is set, it should be an internal time specification, and the
  9132. scheduling will use the corresponding date."
  9133. (interactive "P")
  9134. (if remove
  9135. (progn
  9136. (org-remove-timestamp-with-keyword org-scheduled-string)
  9137. (message "Item is no longer scheduled."))
  9138. (if (org-get-repeat)
  9139. (error "Cannot reschedule task with repeater, please do that by hand")
  9140. (org-add-planning-info 'scheduled time 'closed)
  9141. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9142. (defun org-get-scheduled-time (pom &optional inherit)
  9143. "Get the scheduled time as a time tuple, of a format suitable
  9144. for calling org-schedule with, or if there is no scheduling,
  9145. returns nil."
  9146. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9147. (when time
  9148. (apply 'encode-time (org-parse-time-string time)))))
  9149. (defun org-get-deadline-time (pom &optional inherit)
  9150. "Get the deadine as a time tuple, of a format suitable for
  9151. calling org-deadlin with, or if there is no scheduling, returns
  9152. nil."
  9153. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9154. (when time
  9155. (apply 'encode-time (org-parse-time-string time)))))
  9156. (defun org-remove-timestamp-with-keyword (keyword)
  9157. "Remove all time stamps with KEYWORD in the current entry."
  9158. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9159. beg)
  9160. (save-excursion
  9161. (org-back-to-heading t)
  9162. (setq beg (point))
  9163. (org-end-of-subtree t t)
  9164. (while (re-search-backward re beg t)
  9165. (replace-match "")
  9166. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9167. (equal (char-before) ?\ ))
  9168. (backward-delete-char 1)
  9169. (if (string-match "^[ \t]*$" (buffer-substring
  9170. (point-at-bol) (point-at-eol)))
  9171. (delete-region (point-at-bol)
  9172. (min (point-max) (1+ (point-at-eol))))))))))
  9173. (defun org-add-planning-info (what &optional time &rest remove)
  9174. "Insert new timestamp with keyword in the line directly after the headline.
  9175. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9176. If non is given, the user is prompted for a date.
  9177. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9178. be removed."
  9179. (interactive)
  9180. (let (org-time-was-given org-end-time-was-given ts
  9181. end default-time default-input)
  9182. (catch 'exit
  9183. (when (and (not time) (memq what '(scheduled deadline)))
  9184. ;; Try to get a default date/time from existing timestamp
  9185. (save-excursion
  9186. (org-back-to-heading t)
  9187. (setq end (save-excursion (outline-next-heading) (point)))
  9188. (when (re-search-forward (if (eq what 'scheduled)
  9189. org-scheduled-time-regexp
  9190. org-deadline-time-regexp)
  9191. end t)
  9192. (setq ts (match-string 1)
  9193. default-time
  9194. (apply 'encode-time (org-parse-time-string ts))
  9195. default-input (and ts (org-get-compact-tod ts))))))
  9196. (when what
  9197. ;; If necessary, get the time from the user
  9198. (setq time (or time (org-read-date nil 'to-time nil nil
  9199. default-time default-input))))
  9200. (when (and org-insert-labeled-timestamps-at-point
  9201. (member what '(scheduled deadline)))
  9202. (insert
  9203. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9204. (org-insert-time-stamp time org-time-was-given
  9205. nil nil nil (list org-end-time-was-given))
  9206. (setq what nil))
  9207. (save-excursion
  9208. (save-restriction
  9209. (let (col list elt ts buffer-invisibility-spec)
  9210. (org-back-to-heading t)
  9211. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9212. (goto-char (match-end 1))
  9213. (setq col (current-column))
  9214. (goto-char (match-end 0))
  9215. (if (eobp) (insert "\n") (forward-char 1))
  9216. (when (and (not what)
  9217. (not (looking-at
  9218. (concat "[ \t]*"
  9219. org-keyword-time-not-clock-regexp))))
  9220. ;; Nothing to add, nothing to remove...... :-)
  9221. (throw 'exit nil))
  9222. (if (and (not (looking-at outline-regexp))
  9223. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9224. "[^\r\n]*"))
  9225. (not (equal (match-string 1) org-clock-string)))
  9226. (narrow-to-region (match-beginning 0) (match-end 0))
  9227. (insert-before-markers "\n")
  9228. (backward-char 1)
  9229. (narrow-to-region (point) (point))
  9230. (and org-adapt-indentation (org-indent-to-column col)))
  9231. ;; Check if we have to remove something.
  9232. (setq list (cons what remove))
  9233. (while list
  9234. (setq elt (pop list))
  9235. (goto-char (point-min))
  9236. (when (or (and (eq elt 'scheduled)
  9237. (re-search-forward org-scheduled-time-regexp nil t))
  9238. (and (eq elt 'deadline)
  9239. (re-search-forward org-deadline-time-regexp nil t))
  9240. (and (eq elt 'closed)
  9241. (re-search-forward org-closed-time-regexp nil t)))
  9242. (replace-match "")
  9243. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9244. (if (looking-at " +") (replace-match ""))))
  9245. (goto-char (point-max))
  9246. (when what
  9247. (insert
  9248. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9249. (cond ((eq what 'scheduled) org-scheduled-string)
  9250. ((eq what 'deadline) org-deadline-string)
  9251. ((eq what 'closed) org-closed-string))
  9252. " ")
  9253. (setq ts (org-insert-time-stamp
  9254. time
  9255. (or org-time-was-given
  9256. (and (eq what 'closed) org-log-done-with-time))
  9257. (eq what 'closed)
  9258. nil nil (list org-end-time-was-given)))
  9259. (end-of-line 1))
  9260. (goto-char (point-min))
  9261. (widen)
  9262. (if (and (looking-at "[ \t]+\n")
  9263. (equal (char-before) ?\n))
  9264. (delete-region (1- (point)) (point-at-eol)))
  9265. ts))))))
  9266. (defvar org-log-note-marker (make-marker))
  9267. (defvar org-log-note-purpose nil)
  9268. (defvar org-log-note-state nil)
  9269. (defvar org-log-note-previous-state nil)
  9270. (defvar org-log-note-how nil)
  9271. (defvar org-log-note-extra nil)
  9272. (defvar org-log-note-window-configuration nil)
  9273. (defvar org-log-note-return-to (make-marker))
  9274. (defvar org-log-post-message nil
  9275. "Message to be displayed after a log note has been stored.
  9276. The auto-repeater uses this.")
  9277. (defun org-add-note ()
  9278. "Add a note to the current entry.
  9279. This is done in the same way as adding a state change note."
  9280. (interactive)
  9281. (org-add-log-setup 'note nil nil 'findpos nil))
  9282. (defvar org-property-end-re)
  9283. (defun org-add-log-setup (&optional purpose state prev-state
  9284. findpos how &optional extra)
  9285. "Set up the post command hook to take a note.
  9286. If this is about to TODO state change, the new state is expected in STATE.
  9287. When FINDPOS is non-nil, find the correct position for the note in
  9288. the current entry. If not, assume that it can be inserted at point.
  9289. HOW is an indicator what kind of note should be created.
  9290. EXTRA is additional text that will be inserted into the notes buffer."
  9291. (let* ((org-log-into-drawer (org-log-into-drawer))
  9292. (drawer (cond ((stringp org-log-into-drawer)
  9293. org-log-into-drawer)
  9294. (org-log-into-drawer "LOGBOOK")
  9295. (t nil))))
  9296. (save-restriction
  9297. (save-excursion
  9298. (when findpos
  9299. (org-back-to-heading t)
  9300. (narrow-to-region (point) (save-excursion
  9301. (outline-next-heading) (point)))
  9302. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9303. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9304. "[^\r\n]*\\)?"))
  9305. (goto-char (match-end 0))
  9306. (cond
  9307. (drawer
  9308. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9309. nil t)
  9310. (progn
  9311. (goto-char (match-end 0))
  9312. (or org-log-states-order-reversed
  9313. (and (re-search-forward org-property-end-re nil t)
  9314. (goto-char (1- (match-beginning 0))))))
  9315. (insert "\n:" drawer ":\n:END:")
  9316. (beginning-of-line 0)
  9317. (org-indent-line-function)
  9318. (beginning-of-line 2)
  9319. (org-indent-line-function)
  9320. (end-of-line 0)))
  9321. ((and org-log-state-notes-insert-after-drawers
  9322. (save-excursion
  9323. (forward-line) (looking-at org-drawer-regexp)))
  9324. (forward-line)
  9325. (while (looking-at org-drawer-regexp)
  9326. (goto-char (match-end 0))
  9327. (re-search-forward org-property-end-re (point-max) t)
  9328. (forward-line))
  9329. (forward-line -1)))
  9330. (unless org-log-states-order-reversed
  9331. (and (= (char-after) ?\n) (forward-char 1))
  9332. (org-skip-over-state-notes)
  9333. (skip-chars-backward " \t\n\r")))
  9334. (move-marker org-log-note-marker (point))
  9335. (setq org-log-note-purpose purpose
  9336. org-log-note-state state
  9337. org-log-note-previous-state prev-state
  9338. org-log-note-how how
  9339. org-log-note-extra extra)
  9340. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9341. (defun org-skip-over-state-notes ()
  9342. "Skip past the list of State notes in an entry."
  9343. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9344. (while (looking-at "[ \t]*- State")
  9345. (condition-case nil
  9346. (org-next-item)
  9347. (error (org-end-of-item)))))
  9348. (defun org-add-log-note (&optional purpose)
  9349. "Pop up a window for taking a note, and add this note later at point."
  9350. (remove-hook 'post-command-hook 'org-add-log-note)
  9351. (setq org-log-note-window-configuration (current-window-configuration))
  9352. (delete-other-windows)
  9353. (move-marker org-log-note-return-to (point))
  9354. (switch-to-buffer (marker-buffer org-log-note-marker))
  9355. (goto-char org-log-note-marker)
  9356. (org-switch-to-buffer-other-window "*Org Note*")
  9357. (erase-buffer)
  9358. (if (memq org-log-note-how '(time state))
  9359. (let (current-prefix-arg) (org-store-log-note))
  9360. (let ((org-inhibit-startup t)) (org-mode))
  9361. (insert (format "# Insert note for %s.
  9362. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9363. (cond
  9364. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9365. ((eq org-log-note-purpose 'done) "closed todo item")
  9366. ((eq org-log-note-purpose 'state)
  9367. (format "state change from \"%s\" to \"%s\""
  9368. (or org-log-note-previous-state "")
  9369. (or org-log-note-state "")))
  9370. ((eq org-log-note-purpose 'note)
  9371. "this entry")
  9372. (t (error "This should not happen")))))
  9373. (if org-log-note-extra (insert org-log-note-extra))
  9374. (org-set-local 'org-finish-function 'org-store-log-note)))
  9375. (defvar org-note-abort nil) ; dynamically scoped
  9376. (defun org-store-log-note ()
  9377. "Finish taking a log note, and insert it to where it belongs."
  9378. (let ((txt (buffer-string))
  9379. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9380. lines ind)
  9381. (kill-buffer (current-buffer))
  9382. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9383. (setq txt (replace-match "" t t txt)))
  9384. (if (string-match "\\s-+\\'" txt)
  9385. (setq txt (replace-match "" t t txt)))
  9386. (setq lines (org-split-string txt "\n"))
  9387. (when (and note (string-match "\\S-" note))
  9388. (setq note
  9389. (org-replace-escapes
  9390. note
  9391. (list (cons "%u" (user-login-name))
  9392. (cons "%U" user-full-name)
  9393. (cons "%t" (format-time-string
  9394. (org-time-stamp-format 'long 'inactive)
  9395. (current-time)))
  9396. (cons "%s" (if org-log-note-state
  9397. (concat "\"" org-log-note-state "\"")
  9398. ""))
  9399. (cons "%S" (if org-log-note-previous-state
  9400. (concat "\"" org-log-note-previous-state "\"")
  9401. "\"\"")))))
  9402. (if lines (setq note (concat note " \\\\")))
  9403. (push note lines))
  9404. (when (or current-prefix-arg org-note-abort)
  9405. (when org-log-into-drawer
  9406. (org-remove-empty-drawer-at
  9407. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9408. org-log-note-marker))
  9409. (setq lines nil))
  9410. (when lines
  9411. (save-excursion
  9412. (set-buffer (marker-buffer org-log-note-marker))
  9413. (save-excursion
  9414. (goto-char org-log-note-marker)
  9415. (move-marker org-log-note-marker nil)
  9416. (end-of-line 1)
  9417. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9418. (insert "- " (pop lines))
  9419. (org-indent-line-function)
  9420. (beginning-of-line 1)
  9421. (looking-at "[ \t]*")
  9422. (setq ind (concat (match-string 0) " "))
  9423. (end-of-line 1)
  9424. (while lines (insert "\n" ind (pop lines)))
  9425. (message "Note stored")
  9426. (org-back-to-heading t)
  9427. (org-cycle-hide-drawers 'children)))))
  9428. (set-window-configuration org-log-note-window-configuration)
  9429. (with-current-buffer (marker-buffer org-log-note-return-to)
  9430. (goto-char org-log-note-return-to))
  9431. (move-marker org-log-note-return-to nil)
  9432. (and org-log-post-message (message "%s" org-log-post-message)))
  9433. (defun org-remove-empty-drawer-at (drawer pos)
  9434. "Remove an emptyr DARWER drawer at position POS.
  9435. POS may also be a marker."
  9436. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9437. (save-excursion
  9438. (save-restriction
  9439. (widen)
  9440. (goto-char pos)
  9441. (if (org-in-regexp
  9442. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9443. (replace-match ""))))))
  9444. (defun org-sparse-tree (&optional arg)
  9445. "Create a sparse tree, prompt for the details.
  9446. This command can create sparse trees. You first need to select the type
  9447. of match used to create the tree:
  9448. t Show entries with a specific TODO keyword.
  9449. m Show entries selected by a tags/property match.
  9450. p Enter a property name and its value (both with completion on existing
  9451. names/values) and show entries with that property.
  9452. r Show entries matching a regular expression.
  9453. d Show deadlines due within `org-deadline-warning-days'.
  9454. b Show deadlines and scheduled items before a date.
  9455. a Show deadlines and scheduled items after a date."
  9456. (interactive "P")
  9457. (let (ans kwd value)
  9458. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9459. (setq ans (read-char-exclusive))
  9460. (cond
  9461. ((equal ans ?d)
  9462. (call-interactively 'org-check-deadlines))
  9463. ((equal ans ?b)
  9464. (call-interactively 'org-check-before-date))
  9465. ((equal ans ?a)
  9466. (call-interactively 'org-check-after-date))
  9467. ((equal ans ?t)
  9468. (org-show-todo-tree '(4)))
  9469. ((member ans '(?T ?m))
  9470. (call-interactively 'org-match-sparse-tree))
  9471. ((member ans '(?p ?P))
  9472. (setq kwd (org-ido-completing-read "Property: "
  9473. (mapcar 'list (org-buffer-property-keys))))
  9474. (setq value (org-ido-completing-read "Value: "
  9475. (mapcar 'list (org-property-values kwd))))
  9476. (unless (string-match "\\`{.*}\\'" value)
  9477. (setq value (concat "\"" value "\"")))
  9478. (org-match-sparse-tree arg (concat kwd "=" value)))
  9479. ((member ans '(?r ?R ?/))
  9480. (call-interactively 'org-occur))
  9481. (t (error "No such sparse tree command \"%c\"" ans)))))
  9482. (defvar org-occur-highlights nil
  9483. "List of overlays used for occur matches.")
  9484. (make-variable-buffer-local 'org-occur-highlights)
  9485. (defvar org-occur-parameters nil
  9486. "Parameters of the active org-occur calls.
  9487. This is a list, each call to org-occur pushes as cons cell,
  9488. containing the regular expression and the callback, onto the list.
  9489. The list can contain several entries if `org-occur' has been called
  9490. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9491. will only contain one set of parameters. When the highlights are
  9492. removed (for example with `C-c C-c', or with the next edit (depending
  9493. on `org-remove-highlights-with-change'), this variable is emptied
  9494. as well.")
  9495. (make-variable-buffer-local 'org-occur-parameters)
  9496. (defun org-occur (regexp &optional keep-previous callback)
  9497. "Make a compact tree which shows all matches of REGEXP.
  9498. The tree will show the lines where the regexp matches, and all higher
  9499. headlines above the match. It will also show the heading after the match,
  9500. to make sure editing the matching entry is easy.
  9501. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9502. call to `org-occur' will be kept, to allow stacking of calls to this
  9503. command.
  9504. If CALLBACK is non-nil, it is a function which is called to confirm
  9505. that the match should indeed be shown."
  9506. (interactive "sRegexp: \nP")
  9507. (unless keep-previous
  9508. (org-remove-occur-highlights nil nil t))
  9509. (push (cons regexp callback) org-occur-parameters)
  9510. (let ((cnt 0))
  9511. (save-excursion
  9512. (goto-char (point-min))
  9513. (if (or (not keep-previous) ; do not want to keep
  9514. (not org-occur-highlights)) ; no previous matches
  9515. ;; hide everything
  9516. (org-overview))
  9517. (while (re-search-forward regexp nil t)
  9518. (when (or (not callback)
  9519. (save-match-data (funcall callback)))
  9520. (setq cnt (1+ cnt))
  9521. (when org-highlight-sparse-tree-matches
  9522. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9523. (org-show-context 'occur-tree))))
  9524. (when org-remove-highlights-with-change
  9525. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9526. nil 'local))
  9527. (unless org-sparse-tree-open-archived-trees
  9528. (org-hide-archived-subtrees (point-min) (point-max)))
  9529. (run-hooks 'org-occur-hook)
  9530. (if (interactive-p)
  9531. (message "%d match(es) for regexp %s" cnt regexp))
  9532. cnt))
  9533. (defun org-show-context (&optional key)
  9534. "Make sure point and context and visible.
  9535. How much context is shown depends upon the variables
  9536. `org-show-hierarchy-above', `org-show-following-heading'. and
  9537. `org-show-siblings'."
  9538. (let ((heading-p (org-on-heading-p t))
  9539. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9540. (following-p (org-get-alist-option org-show-following-heading key))
  9541. (entry-p (org-get-alist-option org-show-entry-below key))
  9542. (siblings-p (org-get-alist-option org-show-siblings key)))
  9543. (catch 'exit
  9544. ;; Show heading or entry text
  9545. (if (and heading-p (not entry-p))
  9546. (org-flag-heading nil) ; only show the heading
  9547. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9548. (org-show-hidden-entry))) ; show entire entry
  9549. (when following-p
  9550. ;; Show next sibling, or heading below text
  9551. (save-excursion
  9552. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9553. (org-flag-heading nil))))
  9554. (when siblings-p (org-show-siblings))
  9555. (when hierarchy-p
  9556. ;; show all higher headings, possibly with siblings
  9557. (save-excursion
  9558. (while (and (condition-case nil
  9559. (progn (org-up-heading-all 1) t)
  9560. (error nil))
  9561. (not (bobp)))
  9562. (org-flag-heading nil)
  9563. (when siblings-p (org-show-siblings))))))))
  9564. (defun org-reveal (&optional siblings)
  9565. "Show current entry, hierarchy above it, and the following headline.
  9566. This can be used to show a consistent set of context around locations
  9567. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9568. not t for the search context.
  9569. With optional argument SIBLINGS, on each level of the hierarchy all
  9570. siblings are shown. This repairs the tree structure to what it would
  9571. look like when opened with hierarchical calls to `org-cycle'."
  9572. (interactive "P")
  9573. (let ((org-show-hierarchy-above t)
  9574. (org-show-following-heading t)
  9575. (org-show-siblings (if siblings t org-show-siblings)))
  9576. (org-show-context nil)))
  9577. (defun org-highlight-new-match (beg end)
  9578. "Highlight from BEG to END and mark the highlight is an occur headline."
  9579. (let ((ov (org-make-overlay beg end)))
  9580. (org-overlay-put ov 'face 'secondary-selection)
  9581. (push ov org-occur-highlights)))
  9582. (defun org-remove-occur-highlights (&optional beg end noremove)
  9583. "Remove the occur highlights from the buffer.
  9584. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9585. from the `before-change-functions' in the current buffer."
  9586. (interactive)
  9587. (unless org-inhibit-highlight-removal
  9588. (mapc 'org-delete-overlay org-occur-highlights)
  9589. (setq org-occur-highlights nil)
  9590. (setq org-occur-parameters nil)
  9591. (unless noremove
  9592. (remove-hook 'before-change-functions
  9593. 'org-remove-occur-highlights 'local))))
  9594. ;;;; Priorities
  9595. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9596. "Regular expression matching the priority indicator.")
  9597. (defvar org-remove-priority-next-time nil)
  9598. (defun org-priority-up ()
  9599. "Increase the priority of the current item."
  9600. (interactive)
  9601. (org-priority 'up))
  9602. (defun org-priority-down ()
  9603. "Decrease the priority of the current item."
  9604. (interactive)
  9605. (org-priority 'down))
  9606. (defun org-priority (&optional action)
  9607. "Change the priority of an item by ARG.
  9608. ACTION can be `set', `up', `down', or a character."
  9609. (interactive)
  9610. (unless org-enable-priority-commands
  9611. (error "Priority commands are disabled"))
  9612. (setq action (or action 'set))
  9613. (let (current new news have remove)
  9614. (save-excursion
  9615. (org-back-to-heading t)
  9616. (if (looking-at org-priority-regexp)
  9617. (setq current (string-to-char (match-string 2))
  9618. have t)
  9619. (setq current org-default-priority))
  9620. (cond
  9621. ((or (eq action 'set)
  9622. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9623. (if (not (eq action 'set))
  9624. (setq new action)
  9625. (message "Priority %c-%c, SPC to remove: "
  9626. org-highest-priority org-lowest-priority)
  9627. (setq new (read-char-exclusive)))
  9628. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9629. (= (upcase org-lowest-priority) org-lowest-priority))
  9630. (setq new (upcase new)))
  9631. (cond ((equal new ?\ ) (setq remove t))
  9632. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9633. (error "Priority must be between `%c' and `%c'"
  9634. org-highest-priority org-lowest-priority))))
  9635. ((eq action 'up)
  9636. (if (and (not have) (eq last-command this-command))
  9637. (setq new org-lowest-priority)
  9638. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9639. org-default-priority (1- current)))))
  9640. ((eq action 'down)
  9641. (if (and (not have) (eq last-command this-command))
  9642. (setq new org-highest-priority)
  9643. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9644. org-default-priority (1+ current)))))
  9645. (t (error "Invalid action")))
  9646. (if (or (< (upcase new) org-highest-priority)
  9647. (> (upcase new) org-lowest-priority))
  9648. (setq remove t))
  9649. (setq news (format "%c" new))
  9650. (if have
  9651. (if remove
  9652. (replace-match "" t t nil 1)
  9653. (replace-match news t t nil 2))
  9654. (if remove
  9655. (error "No priority cookie found in line")
  9656. (let ((case-fold-search nil))
  9657. (looking-at org-todo-line-regexp))
  9658. (if (match-end 2)
  9659. (progn
  9660. (goto-char (match-end 2))
  9661. (insert " [#" news "]"))
  9662. (goto-char (match-beginning 3))
  9663. (insert "[#" news "] ")))))
  9664. (org-preserve-lc (org-set-tags nil 'align))
  9665. (if remove
  9666. (message "Priority removed")
  9667. (message "Priority of current item set to %s" news))))
  9668. (defun org-get-priority (s)
  9669. "Find priority cookie and return priority."
  9670. (save-match-data
  9671. (if (not (string-match org-priority-regexp s))
  9672. (* 1000 (- org-lowest-priority org-default-priority))
  9673. (* 1000 (- org-lowest-priority
  9674. (string-to-char (match-string 2 s)))))))
  9675. ;;;; Tags
  9676. (defvar org-agenda-archives-mode)
  9677. (defvar org-map-continue-from nil
  9678. "Position from where mapping should continue.
  9679. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9680. (defvar org-scanner-tags nil
  9681. "The current tag list while the tags scanner is running.")
  9682. (defvar org-trust-scanner-tags nil
  9683. "Should `org-get-tags-at' use the tags fro the scanner.
  9684. This is for internal dynamical scoping only.
  9685. When this is non-nil, the function `org-get-tags-at' will return the value
  9686. of `org-scanner-tags' instead of building the list by itself. This
  9687. can lead to large speed-ups when the tags scanner is used in a file with
  9688. many entries, and when the list of tags is retrieved, for example to
  9689. obtain a list of properties. Building the tags list for each entry in such
  9690. a file becomes an N^2 operation - but with this variable set, it scales
  9691. as N.")
  9692. (defun org-scan-tags (action matcher &optional todo-only)
  9693. "Scan headline tags with inheritance and produce output ACTION.
  9694. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9695. or `agenda' to produce an entry list for an agenda view. It can also be
  9696. a Lisp form or a function that should be called at each matched headline, in
  9697. this case the return value is a list of all return values from these calls.
  9698. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9699. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9700. only lines with a TODO keyword are included in the output."
  9701. (require 'org-agenda)
  9702. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9703. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9704. (org-re
  9705. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9706. (props (list 'face 'default
  9707. 'done-face 'org-agenda-done
  9708. 'undone-face 'default
  9709. 'mouse-face 'highlight
  9710. 'org-not-done-regexp org-not-done-regexp
  9711. 'org-todo-regexp org-todo-regexp
  9712. 'keymap org-agenda-keymap
  9713. 'help-echo
  9714. (format "mouse-2 or RET jump to org file %s"
  9715. (abbreviate-file-name
  9716. (or (buffer-file-name (buffer-base-buffer))
  9717. (buffer-name (buffer-base-buffer)))))))
  9718. (case-fold-search nil)
  9719. (org-map-continue-from nil)
  9720. lspos tags tags-list
  9721. (tags-alist (list (cons 0 org-file-tags)))
  9722. (llast 0) rtn rtn1 level category i txt
  9723. todo marker entry priority)
  9724. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9725. (setq action (list 'lambda nil action)))
  9726. (save-excursion
  9727. (goto-char (point-min))
  9728. (when (eq action 'sparse-tree)
  9729. (org-overview)
  9730. (org-remove-occur-highlights))
  9731. (while (re-search-forward re nil t)
  9732. (catch :skip
  9733. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9734. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9735. (goto-char (setq lspos (match-beginning 0)))
  9736. (setq level (org-reduced-level (funcall outline-level))
  9737. category (org-get-category))
  9738. (setq i llast llast level)
  9739. ;; remove tag lists from same and sublevels
  9740. (while (>= i level)
  9741. (when (setq entry (assoc i tags-alist))
  9742. (setq tags-alist (delete entry tags-alist)))
  9743. (setq i (1- i)))
  9744. ;; add the next tags
  9745. (when tags
  9746. (setq tags (org-split-string tags ":")
  9747. tags-alist
  9748. (cons (cons level tags) tags-alist)))
  9749. ;; compile tags for current headline
  9750. (setq tags-list
  9751. (if org-use-tag-inheritance
  9752. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9753. tags)
  9754. org-scanner-tags tags-list)
  9755. (when org-use-tag-inheritance
  9756. (setcdr (car tags-alist)
  9757. (mapcar (lambda (x)
  9758. (setq x (copy-sequence x))
  9759. (org-add-prop-inherited x))
  9760. (cdar tags-alist))))
  9761. (when (and tags org-use-tag-inheritance
  9762. (or (not (eq t org-use-tag-inheritance))
  9763. org-tags-exclude-from-inheritance))
  9764. ;; selective inheritance, remove uninherited ones
  9765. (setcdr (car tags-alist)
  9766. (org-remove-uniherited-tags (cdar tags-alist))))
  9767. (when (and (or (not todo-only)
  9768. (and (member todo org-not-done-keywords)
  9769. (or (not org-agenda-tags-todo-honor-ignore-options)
  9770. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9771. (let ((case-fold-search t)) (eval matcher))
  9772. (or
  9773. (not (member org-archive-tag tags-list))
  9774. ;; we have an archive tag, should we use this anyway?
  9775. (or (not org-agenda-skip-archived-trees)
  9776. (and (eq action 'agenda) org-agenda-archives-mode))))
  9777. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9778. ;; select this headline
  9779. (cond
  9780. ((eq action 'sparse-tree)
  9781. (and org-highlight-sparse-tree-matches
  9782. (org-get-heading) (match-end 0)
  9783. (org-highlight-new-match
  9784. (match-beginning 0) (match-beginning 1)))
  9785. (org-show-context 'tags-tree))
  9786. ((eq action 'agenda)
  9787. (setq txt (org-format-agenda-item
  9788. ""
  9789. (concat
  9790. (if (eq org-tags-match-list-sublevels 'indented)
  9791. (make-string (1- level) ?.) "")
  9792. (org-get-heading))
  9793. category
  9794. tags-list
  9795. )
  9796. priority (org-get-priority txt))
  9797. (goto-char lspos)
  9798. (setq marker (org-agenda-new-marker))
  9799. (org-add-props txt props
  9800. 'org-marker marker 'org-hd-marker marker 'org-category category
  9801. 'todo-state todo
  9802. 'priority priority 'type "tagsmatch")
  9803. (push txt rtn))
  9804. ((functionp action)
  9805. (setq org-map-continue-from nil)
  9806. (save-excursion
  9807. (setq rtn1 (funcall action))
  9808. (push rtn1 rtn)))
  9809. (t (error "Invalid action")))
  9810. ;; if we are to skip sublevels, jump to end of subtree
  9811. (unless org-tags-match-list-sublevels
  9812. (org-end-of-subtree t)
  9813. (backward-char 1))))
  9814. ;; Get the correct position from where to continue
  9815. (if org-map-continue-from
  9816. (goto-char org-map-continue-from)
  9817. (and (= (point) lspos) (end-of-line 1)))))
  9818. (when (and (eq action 'sparse-tree)
  9819. (not org-sparse-tree-open-archived-trees))
  9820. (org-hide-archived-subtrees (point-min) (point-max)))
  9821. (nreverse rtn)))
  9822. (defun org-remove-uniherited-tags (tags)
  9823. "Remove all tags that are not inherited from the list TAGS."
  9824. (cond
  9825. ((eq org-use-tag-inheritance t)
  9826. (if org-tags-exclude-from-inheritance
  9827. (org-delete-all org-tags-exclude-from-inheritance tags)
  9828. tags))
  9829. ((not org-use-tag-inheritance) nil)
  9830. ((stringp org-use-tag-inheritance)
  9831. (delq nil (mapcar
  9832. (lambda (x)
  9833. (if (and (string-match org-use-tag-inheritance x)
  9834. (not (member x org-tags-exclude-from-inheritance)))
  9835. x nil))
  9836. tags)))
  9837. ((listp org-use-tag-inheritance)
  9838. (delq nil (mapcar
  9839. (lambda (x)
  9840. (if (member x org-use-tag-inheritance) x nil))
  9841. tags)))))
  9842. (defvar todo-only) ;; dynamically scoped
  9843. (defun org-match-sparse-tree (&optional todo-only match)
  9844. "Create a sparse tree according to tags string MATCH.
  9845. MATCH can contain positive and negative selection of tags, like
  9846. \"+WORK+URGENT-WITHBOSS\".
  9847. If optional argument TODO-ONLY is non-nil, only select lines that are
  9848. also TODO lines."
  9849. (interactive "P")
  9850. (org-prepare-agenda-buffers (list (current-buffer)))
  9851. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9852. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9853. (defvar org-cached-props nil)
  9854. (defun org-cached-entry-get (pom property)
  9855. (if (or (eq t org-use-property-inheritance)
  9856. (and (stringp org-use-property-inheritance)
  9857. (string-match org-use-property-inheritance property))
  9858. (and (listp org-use-property-inheritance)
  9859. (member property org-use-property-inheritance)))
  9860. ;; Caching is not possible, check it directly
  9861. (org-entry-get pom property 'inherit)
  9862. ;; Get all properties, so that we can do complicated checks easily
  9863. (cdr (assoc property (or org-cached-props
  9864. (setq org-cached-props
  9865. (org-entry-properties pom)))))))
  9866. (defun org-global-tags-completion-table (&optional files)
  9867. "Return the list of all tags in all agenda buffer/files."
  9868. (save-excursion
  9869. (org-uniquify
  9870. (delq nil
  9871. (apply 'append
  9872. (mapcar
  9873. (lambda (file)
  9874. (set-buffer (find-file-noselect file))
  9875. (append (org-get-buffer-tags)
  9876. (mapcar (lambda (x) (if (stringp (car-safe x))
  9877. (list (car-safe x)) nil))
  9878. org-tag-alist)))
  9879. (if (and files (car files))
  9880. files
  9881. (org-agenda-files))))))))
  9882. (defun org-make-tags-matcher (match)
  9883. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9884. ;; todo-only is scoped dynamically into this function, and the function
  9885. ;; may change it if the matcher asks for it.
  9886. (unless match
  9887. ;; Get a new match request, with completion
  9888. (let ((org-last-tags-completion-table
  9889. (org-global-tags-completion-table)))
  9890. (setq match (org-completing-read-no-ido
  9891. "Match: " 'org-tags-completion-function nil nil nil
  9892. 'org-tags-history))))
  9893. ;; Parse the string and create a lisp form
  9894. (let ((match0 match)
  9895. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9896. minus tag mm
  9897. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9898. orterms term orlist re-p str-p level-p level-op time-p
  9899. prop-p pn pv po cat-p gv rest)
  9900. (if (string-match "/+" match)
  9901. ;; match contains also a todo-matching request
  9902. (progn
  9903. (setq tagsmatch (substring match 0 (match-beginning 0))
  9904. todomatch (substring match (match-end 0)))
  9905. (if (string-match "^!" todomatch)
  9906. (setq todo-only t todomatch (substring todomatch 1)))
  9907. (if (string-match "^\\s-*$" todomatch)
  9908. (setq todomatch nil)))
  9909. ;; only matching tags
  9910. (setq tagsmatch match todomatch nil))
  9911. ;; Make the tags matcher
  9912. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9913. (setq tagsmatcher t)
  9914. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9915. (while (setq term (pop orterms))
  9916. (while (and (equal (substring term -1) "\\") orterms)
  9917. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9918. (while (string-match re term)
  9919. (setq rest (substring term (match-end 0))
  9920. minus (and (match-end 1)
  9921. (equal (match-string 1 term) "-"))
  9922. tag (match-string 2 term)
  9923. re-p (equal (string-to-char tag) ?{)
  9924. level-p (match-end 4)
  9925. prop-p (match-end 5)
  9926. mm (cond
  9927. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9928. (level-p
  9929. (setq level-op (org-op-to-function (match-string 3 term)))
  9930. `(,level-op level ,(string-to-number
  9931. (match-string 4 term))))
  9932. (prop-p
  9933. (setq pn (match-string 5 term)
  9934. po (match-string 6 term)
  9935. pv (match-string 7 term)
  9936. cat-p (equal pn "CATEGORY")
  9937. re-p (equal (string-to-char pv) ?{)
  9938. str-p (equal (string-to-char pv) ?\")
  9939. time-p (save-match-data
  9940. (string-match "^\"[[<].*[]>]\"$" pv))
  9941. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9942. (if time-p (setq pv (org-matcher-time pv)))
  9943. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9944. (cond
  9945. ((equal pn "CATEGORY")
  9946. (setq gv '(get-text-property (point) 'org-category)))
  9947. ((equal pn "TODO")
  9948. (setq gv 'todo))
  9949. (t
  9950. (setq gv `(org-cached-entry-get nil ,pn))))
  9951. (if re-p
  9952. (if (eq po 'org<>)
  9953. `(not (string-match ,pv (or ,gv "")))
  9954. `(string-match ,pv (or ,gv "")))
  9955. (if str-p
  9956. `(,po (or ,gv "") ,pv)
  9957. `(,po (string-to-number (or ,gv ""))
  9958. ,(string-to-number pv) ))))
  9959. (t `(member ,tag tags-list)))
  9960. mm (if minus (list 'not mm) mm)
  9961. term rest)
  9962. (push mm tagsmatcher))
  9963. (push (if (> (length tagsmatcher) 1)
  9964. (cons 'and tagsmatcher)
  9965. (car tagsmatcher))
  9966. orlist)
  9967. (setq tagsmatcher nil))
  9968. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9969. (setq tagsmatcher
  9970. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9971. ;; Make the todo matcher
  9972. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9973. (setq todomatcher t)
  9974. (setq orterms (org-split-string todomatch "|") orlist nil)
  9975. (while (setq term (pop orterms))
  9976. (while (string-match re term)
  9977. (setq minus (and (match-end 1)
  9978. (equal (match-string 1 term) "-"))
  9979. kwd (match-string 2 term)
  9980. re-p (equal (string-to-char kwd) ?{)
  9981. term (substring term (match-end 0))
  9982. mm (if re-p
  9983. `(string-match ,(substring kwd 1 -1) todo)
  9984. (list 'equal 'todo kwd))
  9985. mm (if minus (list 'not mm) mm))
  9986. (push mm todomatcher))
  9987. (push (if (> (length todomatcher) 1)
  9988. (cons 'and todomatcher)
  9989. (car todomatcher))
  9990. orlist)
  9991. (setq todomatcher nil))
  9992. (setq todomatcher (if (> (length orlist) 1)
  9993. (cons 'or orlist) (car orlist))))
  9994. ;; Return the string and lisp forms of the matcher
  9995. (setq matcher (if todomatcher
  9996. (list 'and tagsmatcher todomatcher)
  9997. tagsmatcher))
  9998. (cons match0 matcher)))
  9999. (defun org-op-to-function (op &optional stringp)
  10000. "Turn an operator into the appropriate function."
  10001. (setq op
  10002. (cond
  10003. ((equal op "<" ) '(< string< org-time<))
  10004. ((equal op ">" ) '(> org-string> org-time>))
  10005. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10006. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10007. ((member op '("=" "==")) '(= string= org-time=))
  10008. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10009. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10010. (defun org<> (a b) (not (= a b)))
  10011. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10012. (defun org-string>= (a b) (not (string< a b)))
  10013. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10014. (defun org-string<> (a b) (not (string= a b)))
  10015. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10016. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10017. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10018. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10019. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10020. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10021. (defun org-2ft (s)
  10022. "Convert S to a floating point time.
  10023. If S is already a number, just return it. If it is a string, parse
  10024. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10025. (cond
  10026. ((numberp s) s)
  10027. ((stringp s)
  10028. (condition-case nil
  10029. (float-time (apply 'encode-time (org-parse-time-string s)))
  10030. (error 0.)))
  10031. (t 0.)))
  10032. (defun org-time-today ()
  10033. "Time in seconds today at 0:00.
  10034. Returns the float number of seconds since the beginning of the
  10035. epoch to the beginning of today (00:00)."
  10036. (float-time (apply 'encode-time
  10037. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10038. (defun org-matcher-time (s)
  10039. "Interpret a time comparison value."
  10040. (save-match-data
  10041. (cond
  10042. ((string= s "<now>") (float-time))
  10043. ((string= s "<today>") (org-time-today))
  10044. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10045. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10046. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10047. (+ (org-time-today)
  10048. (* (string-to-number (match-string 1 s))
  10049. (cdr (assoc (match-string 2 s)
  10050. '(("d" . 86400.0) ("w" . 604800.0)
  10051. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10052. (t (org-2ft s)))))
  10053. (defun org-match-any-p (re list)
  10054. "Does re match any element of list?"
  10055. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10056. (delq nil list))
  10057. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10058. (defvar org-tags-overlay (org-make-overlay 1 1))
  10059. (org-detach-overlay org-tags-overlay)
  10060. (defun org-get-local-tags-at (&optional pos)
  10061. "Get a list of tags defined in the current headline."
  10062. (org-get-tags-at pos 'local))
  10063. (defun org-get-local-tags ()
  10064. "Get a list of tags defined in the current headline."
  10065. (org-get-tags-at nil 'local))
  10066. (defun org-get-tags-at (&optional pos local)
  10067. "Get a list of all headline tags applicable at POS.
  10068. POS defaults to point. If tags are inherited, the list contains
  10069. the targets in the same sequence as the headlines appear, i.e.
  10070. the tags of the current headline come last.
  10071. When LOCAL is non-nil, only return tags from the current headline,
  10072. ignore inherited ones."
  10073. (interactive)
  10074. (if (and org-trust-scanner-tags
  10075. (or (not pos) (equal pos (point)))
  10076. (not local))
  10077. org-scanner-tags
  10078. (let (tags ltags lastpos parent)
  10079. (save-excursion
  10080. (save-restriction
  10081. (widen)
  10082. (goto-char (or pos (point)))
  10083. (save-match-data
  10084. (catch 'done
  10085. (condition-case nil
  10086. (progn
  10087. (org-back-to-heading t)
  10088. (while (not (equal lastpos (point)))
  10089. (setq lastpos (point))
  10090. (when (looking-at
  10091. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10092. (setq ltags (org-split-string
  10093. (org-match-string-no-properties 1) ":"))
  10094. (when parent
  10095. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10096. (setq tags (append
  10097. (if parent
  10098. (org-remove-uniherited-tags ltags)
  10099. ltags)
  10100. tags)))
  10101. (or org-use-tag-inheritance (throw 'done t))
  10102. (if local (throw 'done t))
  10103. (or (org-up-heading-safe) (error nil))
  10104. (setq parent t)))
  10105. (error nil)))))
  10106. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10107. (defun org-add-prop-inherited (s)
  10108. (add-text-properties 0 (length s) '(inherited t) s)
  10109. s)
  10110. (defun org-toggle-tag (tag &optional onoff)
  10111. "Toggle the tag TAG for the current line.
  10112. If ONOFF is `on' or `off', don't toggle but set to this state."
  10113. (let (res current)
  10114. (save-excursion
  10115. (org-back-to-heading t)
  10116. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10117. (point-at-eol) t)
  10118. (progn
  10119. (setq current (match-string 1))
  10120. (replace-match ""))
  10121. (setq current ""))
  10122. (setq current (nreverse (org-split-string current ":")))
  10123. (cond
  10124. ((eq onoff 'on)
  10125. (setq res t)
  10126. (or (member tag current) (push tag current)))
  10127. ((eq onoff 'off)
  10128. (or (not (member tag current)) (setq current (delete tag current))))
  10129. (t (if (member tag current)
  10130. (setq current (delete tag current))
  10131. (setq res t)
  10132. (push tag current))))
  10133. (end-of-line 1)
  10134. (if current
  10135. (progn
  10136. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10137. (org-set-tags nil t))
  10138. (delete-horizontal-space))
  10139. (run-hooks 'org-after-tags-change-hook))
  10140. res))
  10141. (defun org-align-tags-here (to-col)
  10142. ;; Assumes that this is a headline
  10143. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10144. (beginning-of-line 1)
  10145. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10146. (< pos (match-beginning 2)))
  10147. (progn
  10148. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10149. (goto-char (match-beginning 1))
  10150. (insert " ")
  10151. (delete-region (point) (1+ (match-beginning 2)))
  10152. (setq ncol (max (1+ (current-column))
  10153. (1+ col)
  10154. (if (> to-col 0)
  10155. to-col
  10156. (- (abs to-col) tags-l))))
  10157. (setq p (point))
  10158. (insert (make-string (- ncol (current-column)) ?\ ))
  10159. (setq ncol (current-column))
  10160. (when indent-tabs-mode (tabify p (point-at-eol)))
  10161. (org-move-to-column (min ncol col) t))
  10162. (goto-char pos))))
  10163. (defun org-set-tags-command (&optional arg just-align)
  10164. "Call the set-tags command for the current entry."
  10165. (interactive "P")
  10166. (if (org-on-heading-p)
  10167. (org-set-tags arg just-align)
  10168. (save-excursion
  10169. (org-back-to-heading t)
  10170. (org-set-tags arg just-align))))
  10171. (defun org-set-tags (&optional arg just-align)
  10172. "Set the tags for the current headline.
  10173. With prefix ARG, realign all tags in headings in the current buffer."
  10174. (interactive "P")
  10175. (let* ((re (concat "^" outline-regexp))
  10176. (current (org-get-tags-string))
  10177. (col (current-column))
  10178. (org-setting-tags t)
  10179. table current-tags inherited-tags ; computed below when needed
  10180. tags p0 c0 c1 rpl)
  10181. (if arg
  10182. (save-excursion
  10183. (goto-char (point-min))
  10184. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10185. (while (re-search-forward re nil t)
  10186. (org-set-tags nil t)
  10187. (end-of-line 1)))
  10188. (message "All tags realigned to column %d" org-tags-column))
  10189. (if just-align
  10190. (setq tags current)
  10191. ;; Get a new set of tags from the user
  10192. (save-excursion
  10193. (setq table (append org-tag-persistent-alist
  10194. (or org-tag-alist (org-get-buffer-tags)))
  10195. org-last-tags-completion-table table
  10196. current-tags (org-split-string current ":")
  10197. inherited-tags (nreverse
  10198. (nthcdr (length current-tags)
  10199. (nreverse (org-get-tags-at))))
  10200. tags
  10201. (if (or (eq t org-use-fast-tag-selection)
  10202. (and org-use-fast-tag-selection
  10203. (delq nil (mapcar 'cdr table))))
  10204. (org-fast-tag-selection
  10205. current-tags inherited-tags table
  10206. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10207. (let ((org-add-colon-after-tag-completion t))
  10208. (org-trim
  10209. (org-without-partial-completion
  10210. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  10211. nil nil current 'org-tags-history)))))))
  10212. (while (string-match "[-+&]+" tags)
  10213. ;; No boolean logic, just a list
  10214. (setq tags (replace-match ":" t t tags))))
  10215. (if (string-match "\\`[\t ]*\\'" tags)
  10216. (setq tags "")
  10217. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10218. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10219. ;; Insert new tags at the correct column
  10220. (beginning-of-line 1)
  10221. (cond
  10222. ((and (equal current "") (equal tags "")))
  10223. ((re-search-forward
  10224. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10225. (point-at-eol) t)
  10226. (if (equal tags "")
  10227. (setq rpl "")
  10228. (goto-char (match-beginning 0))
  10229. (setq c0 (current-column) p0 (point)
  10230. c1 (max (1+ c0) (if (> org-tags-column 0)
  10231. org-tags-column
  10232. (- (- org-tags-column) (length tags))))
  10233. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10234. (replace-match rpl t t)
  10235. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10236. tags)
  10237. (t (error "Tags alignment failed")))
  10238. (org-move-to-column col)
  10239. (unless just-align
  10240. (run-hooks 'org-after-tags-change-hook)))))
  10241. (defun org-change-tag-in-region (beg end tag off)
  10242. "Add or remove TAG for each entry in the region.
  10243. This works in the agenda, and also in an org-mode buffer."
  10244. (interactive
  10245. (list (region-beginning) (region-end)
  10246. (let ((org-last-tags-completion-table
  10247. (if (org-mode-p)
  10248. (org-get-buffer-tags)
  10249. (org-global-tags-completion-table))))
  10250. (org-ido-completing-read
  10251. "Tag: " 'org-tags-completion-function nil nil nil
  10252. 'org-tags-history))
  10253. (progn
  10254. (message "[s]et or [r]emove? ")
  10255. (equal (read-char-exclusive) ?r))))
  10256. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10257. (let ((agendap (equal major-mode 'org-agenda-mode))
  10258. l1 l2 m buf pos newhead (cnt 0))
  10259. (goto-char end)
  10260. (setq l2 (1- (org-current-line)))
  10261. (goto-char beg)
  10262. (setq l1 (org-current-line))
  10263. (loop for l from l1 to l2 do
  10264. (goto-line l)
  10265. (setq m (get-text-property (point) 'org-hd-marker))
  10266. (when (or (and (org-mode-p) (org-on-heading-p))
  10267. (and agendap m))
  10268. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10269. pos (if agendap m (point)))
  10270. (with-current-buffer buf
  10271. (save-excursion
  10272. (save-restriction
  10273. (goto-char pos)
  10274. (setq cnt (1+ cnt))
  10275. (org-toggle-tag tag (if off 'off 'on))
  10276. (setq newhead (org-get-heading)))))
  10277. (and agendap (org-agenda-change-all-lines newhead m))))
  10278. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10279. (defun org-tags-completion-function (string predicate &optional flag)
  10280. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10281. (confirm (lambda (x) (stringp (car x)))))
  10282. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10283. (setq s1 (match-string 1 string)
  10284. s2 (match-string 2 string))
  10285. (setq s1 "" s2 string))
  10286. (cond
  10287. ((eq flag nil)
  10288. ;; try completion
  10289. (setq rtn (try-completion s2 ctable confirm))
  10290. (if (stringp rtn)
  10291. (setq rtn
  10292. (concat s1 s2 (substring rtn (length s2))
  10293. (if (and org-add-colon-after-tag-completion
  10294. (assoc rtn ctable))
  10295. ":" ""))))
  10296. rtn)
  10297. ((eq flag t)
  10298. ;; all-completions
  10299. (all-completions s2 ctable confirm)
  10300. )
  10301. ((eq flag 'lambda)
  10302. ;; exact match?
  10303. (assoc s2 ctable)))
  10304. ))
  10305. (defun org-fast-tag-insert (kwd tags face &optional end)
  10306. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10307. (insert (format "%-12s" (concat kwd ":"))
  10308. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10309. (or end "")))
  10310. (defun org-fast-tag-show-exit (flag)
  10311. (save-excursion
  10312. (goto-line 3)
  10313. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10314. (replace-match ""))
  10315. (when flag
  10316. (end-of-line 1)
  10317. (org-move-to-column (- (window-width) 19) t)
  10318. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10319. (defun org-set-current-tags-overlay (current prefix)
  10320. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10321. (if (featurep 'xemacs)
  10322. (org-overlay-display org-tags-overlay (concat prefix s)
  10323. 'secondary-selection)
  10324. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10325. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10326. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10327. "Fast tag selection with single keys.
  10328. CURRENT is the current list of tags in the headline, INHERITED is the
  10329. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10330. possibly with grouping information. TODO-TABLE is a similar table with
  10331. TODO keywords, should these have keys assigned to them.
  10332. If the keys are nil, a-z are automatically assigned.
  10333. Returns the new tags string, or nil to not change the current settings."
  10334. (let* ((fulltable (append table todo-table))
  10335. (maxlen (apply 'max (mapcar
  10336. (lambda (x)
  10337. (if (stringp (car x)) (string-width (car x)) 0))
  10338. fulltable)))
  10339. (buf (current-buffer))
  10340. (expert (eq org-fast-tag-selection-single-key 'expert))
  10341. (buffer-tags nil)
  10342. (fwidth (+ maxlen 3 1 3))
  10343. (ncol (/ (- (window-width) 4) fwidth))
  10344. (i-face 'org-done)
  10345. (c-face 'org-todo)
  10346. tg cnt e c char c1 c2 ntable tbl rtn
  10347. ov-start ov-end ov-prefix
  10348. (exit-after-next org-fast-tag-selection-single-key)
  10349. (done-keywords org-done-keywords)
  10350. groups ingroup)
  10351. (save-excursion
  10352. (beginning-of-line 1)
  10353. (if (looking-at
  10354. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10355. (setq ov-start (match-beginning 1)
  10356. ov-end (match-end 1)
  10357. ov-prefix "")
  10358. (setq ov-start (1- (point-at-eol))
  10359. ov-end (1+ ov-start))
  10360. (skip-chars-forward "^\n\r")
  10361. (setq ov-prefix
  10362. (concat
  10363. (buffer-substring (1- (point)) (point))
  10364. (if (> (current-column) org-tags-column)
  10365. " "
  10366. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10367. (org-move-overlay org-tags-overlay ov-start ov-end)
  10368. (save-window-excursion
  10369. (if expert
  10370. (set-buffer (get-buffer-create " *Org tags*"))
  10371. (delete-other-windows)
  10372. (split-window-vertically)
  10373. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10374. (erase-buffer)
  10375. (org-set-local 'org-done-keywords done-keywords)
  10376. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10377. (org-fast-tag-insert "Current" current c-face "\n\n")
  10378. (org-fast-tag-show-exit exit-after-next)
  10379. (org-set-current-tags-overlay current ov-prefix)
  10380. (setq tbl fulltable char ?a cnt 0)
  10381. (while (setq e (pop tbl))
  10382. (cond
  10383. ((equal e '(:startgroup))
  10384. (push '() groups) (setq ingroup t)
  10385. (when (not (= cnt 0))
  10386. (setq cnt 0)
  10387. (insert "\n"))
  10388. (insert "{ "))
  10389. ((equal e '(:endgroup))
  10390. (setq ingroup nil cnt 0)
  10391. (insert "}\n"))
  10392. ((equal e '(:newline))
  10393. (when (not (= cnt 0))
  10394. (setq cnt 0)
  10395. (insert "\n")
  10396. (setq e (car tbl))
  10397. (while (equal (car tbl) '(:newline))
  10398. (insert "\n")
  10399. (setq tbl (cdr tbl)))))
  10400. (t
  10401. (setq tg (car e) c2 nil)
  10402. (if (cdr e)
  10403. (setq c (cdr e))
  10404. ;; automatically assign a character.
  10405. (setq c1 (string-to-char
  10406. (downcase (substring
  10407. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10408. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10409. (while (or (rassoc char ntable) (rassoc char table))
  10410. (setq char (1+ char)))
  10411. (setq c2 c1))
  10412. (setq c (or c2 char)))
  10413. (if ingroup (push tg (car groups)))
  10414. (setq tg (org-add-props tg nil 'face
  10415. (cond
  10416. ((not (assoc tg table))
  10417. (org-get-todo-face tg))
  10418. ((member tg current) c-face)
  10419. ((member tg inherited) i-face)
  10420. (t nil))))
  10421. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10422. (insert "[" c "] " tg (make-string
  10423. (- fwidth 4 (length tg)) ?\ ))
  10424. (push (cons tg c) ntable)
  10425. (when (= (setq cnt (1+ cnt)) ncol)
  10426. (insert "\n")
  10427. (if ingroup (insert " "))
  10428. (setq cnt 0)))))
  10429. (setq ntable (nreverse ntable))
  10430. (insert "\n")
  10431. (goto-char (point-min))
  10432. (if (not expert) (org-fit-window-to-buffer))
  10433. (setq rtn
  10434. (catch 'exit
  10435. (while t
  10436. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10437. (if groups " [!] no groups" " [!]groups")
  10438. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10439. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10440. (cond
  10441. ((= c ?\r) (throw 'exit t))
  10442. ((= c ?!)
  10443. (setq groups (not groups))
  10444. (goto-char (point-min))
  10445. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10446. ((= c ?\C-c)
  10447. (if (not expert)
  10448. (org-fast-tag-show-exit
  10449. (setq exit-after-next (not exit-after-next)))
  10450. (setq expert nil)
  10451. (delete-other-windows)
  10452. (split-window-vertically)
  10453. (org-switch-to-buffer-other-window " *Org tags*")
  10454. (org-fit-window-to-buffer)))
  10455. ((or (= c ?\C-g)
  10456. (and (= c ?q) (not (rassoc c ntable))))
  10457. (org-detach-overlay org-tags-overlay)
  10458. (setq quit-flag t))
  10459. ((= c ?\ )
  10460. (setq current nil)
  10461. (if exit-after-next (setq exit-after-next 'now)))
  10462. ((= c ?\t)
  10463. (condition-case nil
  10464. (setq tg (org-ido-completing-read
  10465. "Tag: "
  10466. (or buffer-tags
  10467. (with-current-buffer buf
  10468. (org-get-buffer-tags)))))
  10469. (quit (setq tg "")))
  10470. (when (string-match "\\S-" tg)
  10471. (add-to-list 'buffer-tags (list tg))
  10472. (if (member tg current)
  10473. (setq current (delete tg current))
  10474. (push tg current)))
  10475. (if exit-after-next (setq exit-after-next 'now)))
  10476. ((setq e (rassoc c todo-table) tg (car e))
  10477. (with-current-buffer buf
  10478. (save-excursion (org-todo tg)))
  10479. (if exit-after-next (setq exit-after-next 'now)))
  10480. ((setq e (rassoc c ntable) tg (car e))
  10481. (if (member tg current)
  10482. (setq current (delete tg current))
  10483. (loop for g in groups do
  10484. (if (member tg g)
  10485. (mapc (lambda (x)
  10486. (setq current (delete x current)))
  10487. g)))
  10488. (push tg current))
  10489. (if exit-after-next (setq exit-after-next 'now))))
  10490. ;; Create a sorted list
  10491. (setq current
  10492. (sort current
  10493. (lambda (a b)
  10494. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10495. (if (eq exit-after-next 'now) (throw 'exit t))
  10496. (goto-char (point-min))
  10497. (beginning-of-line 2)
  10498. (delete-region (point) (point-at-eol))
  10499. (org-fast-tag-insert "Current" current c-face)
  10500. (org-set-current-tags-overlay current ov-prefix)
  10501. (while (re-search-forward
  10502. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10503. (setq tg (match-string 1))
  10504. (add-text-properties
  10505. (match-beginning 1) (match-end 1)
  10506. (list 'face
  10507. (cond
  10508. ((member tg current) c-face)
  10509. ((member tg inherited) i-face)
  10510. (t (get-text-property (match-beginning 1) 'face))))))
  10511. (goto-char (point-min)))))
  10512. (org-detach-overlay org-tags-overlay)
  10513. (if rtn
  10514. (mapconcat 'identity current ":")
  10515. nil))))
  10516. (defun org-get-tags-string ()
  10517. "Get the TAGS string in the current headline."
  10518. (unless (org-on-heading-p t)
  10519. (error "Not on a heading"))
  10520. (save-excursion
  10521. (beginning-of-line 1)
  10522. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10523. (org-match-string-no-properties 1)
  10524. "")))
  10525. (defun org-get-tags ()
  10526. "Get the list of tags specified in the current headline."
  10527. (org-split-string (org-get-tags-string) ":"))
  10528. (defun org-get-buffer-tags ()
  10529. "Get a table of all tags used in the buffer, for completion."
  10530. (let (tags)
  10531. (save-excursion
  10532. (goto-char (point-min))
  10533. (while (re-search-forward
  10534. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10535. (when (equal (char-after (point-at-bol 0)) ?*)
  10536. (mapc (lambda (x) (add-to-list 'tags x))
  10537. (org-split-string (org-match-string-no-properties 1) ":")))))
  10538. (mapcar 'list tags)))
  10539. ;;;; The mapping API
  10540. ;;;###autoload
  10541. (defun org-map-entries (func &optional match scope &rest skip)
  10542. "Call FUNC at each headline selected by MATCH in SCOPE.
  10543. FUNC is a function or a lisp form. The function will be called without
  10544. arguments, with the cursor positioned at the beginning of the headline.
  10545. The return values of all calls to the function will be collected and
  10546. returned as a list.
  10547. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10548. does not need to preserve point. After evaluaton, the cursor will be
  10549. moved to the end of the line (presumably of the headline of the
  10550. processed entry) and search continues from there. Under some
  10551. circumstances, this may not produce the wanted results. For example,
  10552. if you have removed (e.g. archived) the current (sub)tree it could
  10553. mean that the next entry will be skipped entirely. In such cases, you
  10554. can specify the position from where search should continue by making
  10555. FUNC set the variable `org-map-continue-from' to the desired buffer
  10556. position.
  10557. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10558. Only headlines that are matched by this query will be considered during
  10559. the iteration. When MATCH is nil or t, all headlines will be
  10560. visited by the iteration.
  10561. SCOPE determines the scope of this command. It can be any of:
  10562. nil The current buffer, respecting the restriction if any
  10563. tree The subtree started with the entry at point
  10564. file The current buffer, without restriction
  10565. file-with-archives
  10566. The current buffer, and any archives associated with it
  10567. agenda All agenda files
  10568. agenda-with-archives
  10569. All agenda files with any archive files associated with them
  10570. \(file1 file2 ...)
  10571. If this is a list, all files in the list will be scanned
  10572. The remaining args are treated as settings for the skipping facilities of
  10573. the scanner. The following items can be given here:
  10574. archive skip trees with the archive tag.
  10575. comment skip trees with the COMMENT keyword
  10576. function or Emacs Lisp form:
  10577. will be used as value for `org-agenda-skip-function', so whenever
  10578. the the function returns t, FUNC will not be called for that
  10579. entry and search will continue from the point where the
  10580. function leaves it.
  10581. If your function needs to retrieve the tags including inherited tags
  10582. at the *current* entry, you can use the value of the variable
  10583. `org-scanner-tags' which will be much faster than getting the value
  10584. with `org-get-tags-at'. If your function gets properties with
  10585. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10586. to t around the call to `org-entry-properties' to get the same speedup.
  10587. Note that if your function moves around to retrieve tags and properties at
  10588. a *different* entry, you cannot use these techniques."
  10589. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10590. (org-agenda-skip-archived-trees (memq 'archive skip))
  10591. (org-agenda-skip-comment-trees (memq 'comment skip))
  10592. (org-agenda-skip-function
  10593. (car (org-delete-all '(comment archive) skip)))
  10594. (org-tags-match-list-sublevels t)
  10595. matcher file res
  10596. org-todo-keywords-for-agenda
  10597. org-done-keywords-for-agenda
  10598. org-todo-keyword-alist-for-agenda
  10599. org-tag-alist-for-agenda)
  10600. (cond
  10601. ((eq match t) (setq matcher t))
  10602. ((eq match nil) (setq matcher t))
  10603. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10604. (save-excursion
  10605. (save-restriction
  10606. (when (eq scope 'tree)
  10607. (org-back-to-heading t)
  10608. (org-narrow-to-subtree)
  10609. (setq scope nil))
  10610. (if (not scope)
  10611. (progn
  10612. (org-prepare-agenda-buffers
  10613. (list (buffer-file-name (current-buffer))))
  10614. (setq res (org-scan-tags func matcher)))
  10615. ;; Get the right scope
  10616. (cond
  10617. ((and scope (listp scope) (symbolp (car scope)))
  10618. (setq scope (eval scope)))
  10619. ((eq scope 'agenda)
  10620. (setq scope (org-agenda-files t)))
  10621. ((eq scope 'agenda-with-archives)
  10622. (setq scope (org-agenda-files t))
  10623. (setq scope (org-add-archive-files scope)))
  10624. ((eq scope 'file)
  10625. (setq scope (list (buffer-file-name))))
  10626. ((eq scope 'file-with-archives)
  10627. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10628. (org-prepare-agenda-buffers scope)
  10629. (while (setq file (pop scope))
  10630. (with-current-buffer (org-find-base-buffer-visiting file)
  10631. (save-excursion
  10632. (save-restriction
  10633. (widen)
  10634. (goto-char (point-min))
  10635. (setq res (append res (org-scan-tags func matcher))))))))))
  10636. res))
  10637. ;;;; Properties
  10638. ;;; Setting and retrieving properties
  10639. (defconst org-special-properties
  10640. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10641. "TIMESTAMP" "TIMESTAMP_IA")
  10642. "The special properties valid in Org-mode.
  10643. These are properties that are not defined in the property drawer,
  10644. but in some other way.")
  10645. (defconst org-default-properties
  10646. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10647. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10648. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10649. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10650. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10651. "CLOCK_MODELINE_TOTAL")
  10652. "Some properties that are used by Org-mode for various purposes.
  10653. Being in this list makes sure that they are offered for completion.")
  10654. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10655. "Regular expression matching the first line of a property drawer.")
  10656. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10657. "Regular expression matching the first line of a property drawer.")
  10658. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10659. "Regular expression matching the first line of a property drawer.")
  10660. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10661. "Regular expression matching the first line of a property drawer.")
  10662. (defconst org-property-drawer-re
  10663. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10664. org-property-end-re "\\)\n?")
  10665. "Matches an entire property drawer.")
  10666. (defconst org-clock-drawer-re
  10667. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10668. org-property-end-re "\\)\n?")
  10669. "Matches an entire clock drawer.")
  10670. (defun org-property-action ()
  10671. "Do an action on properties."
  10672. (interactive)
  10673. (let (c)
  10674. (org-at-property-p)
  10675. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10676. (setq c (read-char-exclusive))
  10677. (cond
  10678. ((equal c ?s)
  10679. (call-interactively 'org-set-property))
  10680. ((equal c ?d)
  10681. (call-interactively 'org-delete-property))
  10682. ((equal c ?D)
  10683. (call-interactively 'org-delete-property-globally))
  10684. ((equal c ?c)
  10685. (call-interactively 'org-compute-property-at-point))
  10686. (t (error "No such property action %c" c)))))
  10687. (defun org-at-property-p ()
  10688. "Is the cursor in a property line?"
  10689. ;; FIXME: Does not check if we are actually in the drawer.
  10690. ;; FIXME: also returns true on any drawers.....
  10691. ;; This is used by C-c C-c for property action.
  10692. (save-excursion
  10693. (beginning-of-line 1)
  10694. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10695. (defun org-get-property-block (&optional beg end force)
  10696. "Return the (beg . end) range of the body of the property drawer.
  10697. BEG and END can be beginning and end of subtree, if not given
  10698. they will be found.
  10699. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10700. (catch 'exit
  10701. (save-excursion
  10702. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10703. (end (or end (progn (outline-next-heading) (point)))))
  10704. (goto-char beg)
  10705. (if (re-search-forward org-property-start-re end t)
  10706. (setq beg (1+ (match-end 0)))
  10707. (if force
  10708. (save-excursion
  10709. (org-insert-property-drawer)
  10710. (setq end (progn (outline-next-heading) (point))))
  10711. (throw 'exit nil))
  10712. (goto-char beg)
  10713. (if (re-search-forward org-property-start-re end t)
  10714. (setq beg (1+ (match-end 0)))))
  10715. (if (re-search-forward org-property-end-re end t)
  10716. (setq end (match-beginning 0))
  10717. (or force (throw 'exit nil))
  10718. (goto-char beg)
  10719. (setq end beg)
  10720. (org-indent-line-function)
  10721. (insert ":END:\n"))
  10722. (cons beg end)))))
  10723. (defun org-entry-properties (&optional pom which)
  10724. "Get all properties of the entry at point-or-marker POM.
  10725. This includes the TODO keyword, the tags, time strings for deadline,
  10726. scheduled, and clocking, and any additional properties defined in the
  10727. entry. The return value is an alist, keys may occur multiple times
  10728. if the property key was used several times.
  10729. POM may also be nil, in which case the current entry is used.
  10730. If WHICH is nil or `all', get all properties. If WHICH is
  10731. `special' or `standard', only get that subclass."
  10732. (setq which (or which 'all))
  10733. (org-with-point-at pom
  10734. (let ((clockstr (substring org-clock-string 0 -1))
  10735. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10736. beg end range props sum-props key value string clocksum)
  10737. (save-excursion
  10738. (when (condition-case nil
  10739. (and (org-mode-p) (org-back-to-heading t))
  10740. (error nil))
  10741. (setq beg (point))
  10742. (setq sum-props (get-text-property (point) 'org-summaries))
  10743. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10744. (outline-next-heading)
  10745. (setq end (point))
  10746. (when (memq which '(all special))
  10747. ;; Get the special properties, like TODO and tags
  10748. (goto-char beg)
  10749. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10750. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10751. (when (looking-at org-priority-regexp)
  10752. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10753. (when (and (setq value (org-get-tags-string))
  10754. (string-match "\\S-" value))
  10755. (push (cons "TAGS" value) props))
  10756. (when (setq value (org-get-tags-at))
  10757. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10758. props))
  10759. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10760. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10761. string (if (equal key clockstr)
  10762. (org-no-properties
  10763. (org-trim
  10764. (buffer-substring
  10765. (match-beginning 3) (goto-char (point-at-eol)))))
  10766. (substring (org-match-string-no-properties 3) 1 -1)))
  10767. (unless key
  10768. (if (= (char-after (match-beginning 3)) ?\[)
  10769. (setq key "TIMESTAMP_IA")
  10770. (setq key "TIMESTAMP")))
  10771. (when (or (equal key clockstr) (not (assoc key props)))
  10772. (push (cons key string) props)))
  10773. )
  10774. (when (memq which '(all standard))
  10775. ;; Get the standard properties, like :PROP: ...
  10776. (setq range (org-get-property-block beg end))
  10777. (when range
  10778. (goto-char (car range))
  10779. (while (re-search-forward
  10780. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10781. (cdr range) t)
  10782. (setq key (org-match-string-no-properties 1)
  10783. value (org-trim (or (org-match-string-no-properties 2) "")))
  10784. (unless (member key excluded)
  10785. (push (cons key (or value "")) props)))))
  10786. (if clocksum
  10787. (push (cons "CLOCKSUM"
  10788. (org-columns-number-to-string (/ (float clocksum) 60.)
  10789. 'add_times))
  10790. props))
  10791. (unless (assoc "CATEGORY" props)
  10792. (setq value (or (org-get-category)
  10793. (progn (org-refresh-category-properties)
  10794. (org-get-category))))
  10795. (push (cons "CATEGORY" value) props))
  10796. (append sum-props (nreverse props)))))))
  10797. (defun org-entry-get (pom property &optional inherit)
  10798. "Get value of PROPERTY for entry at point-or-marker POM.
  10799. If INHERIT is non-nil and the entry does not have the property,
  10800. then also check higher levels of the hierarchy.
  10801. If INHERIT is the symbol `selective', use inheritance only if the setting
  10802. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10803. If the property is present but empty, the return value is the empty string.
  10804. If the property is not present at all, nil is returned."
  10805. (org-with-point-at pom
  10806. (if (and inherit (if (eq inherit 'selective)
  10807. (org-property-inherit-p property)
  10808. t))
  10809. (org-entry-get-with-inheritance property)
  10810. (if (member property org-special-properties)
  10811. ;; We need a special property. Use brute force, get all properties.
  10812. (cdr (assoc property (org-entry-properties nil 'special)))
  10813. (let ((range (org-get-property-block)))
  10814. (if (and range
  10815. (goto-char (car range))
  10816. (re-search-forward
  10817. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10818. (cdr range) t))
  10819. ;; Found the property, return it.
  10820. (if (match-end 1)
  10821. (org-match-string-no-properties 1)
  10822. "")))))))
  10823. (defun org-property-or-variable-value (var &optional inherit)
  10824. "Check if there is a property fixing the value of VAR.
  10825. If yes, return this value. If not, return the current value of the variable."
  10826. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10827. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10828. (read prop)
  10829. (symbol-value var))))
  10830. (defun org-entry-delete (pom property)
  10831. "Delete the property PROPERTY from entry at point-or-marker POM."
  10832. (org-with-point-at pom
  10833. (if (member property org-special-properties)
  10834. nil ; cannot delete these properties.
  10835. (let ((range (org-get-property-block)))
  10836. (if (and range
  10837. (goto-char (car range))
  10838. (re-search-forward
  10839. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10840. (cdr range) t))
  10841. (progn
  10842. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10843. t)
  10844. nil)))))
  10845. ;; Multi-values properties are properties that contain multiple values
  10846. ;; These values are assumed to be single words, separated by whitespace.
  10847. (defun org-entry-add-to-multivalued-property (pom property value)
  10848. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10849. (let* ((old (org-entry-get pom property))
  10850. (values (and old (org-split-string old "[ \t]"))))
  10851. (setq value (org-entry-protect-space value))
  10852. (unless (member value values)
  10853. (setq values (cons value values))
  10854. (org-entry-put pom property
  10855. (mapconcat 'identity values " ")))))
  10856. (defun org-entry-remove-from-multivalued-property (pom property value)
  10857. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10858. (let* ((old (org-entry-get pom property))
  10859. (values (and old (org-split-string old "[ \t]"))))
  10860. (setq value (org-entry-protect-space value))
  10861. (when (member value values)
  10862. (setq values (delete value values))
  10863. (org-entry-put pom property
  10864. (mapconcat 'identity values " ")))))
  10865. (defun org-entry-member-in-multivalued-property (pom property value)
  10866. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10867. (let* ((old (org-entry-get pom property))
  10868. (values (and old (org-split-string old "[ \t]"))))
  10869. (setq value (org-entry-protect-space value))
  10870. (member value values)))
  10871. (defun org-entry-get-multivalued-property (pom property)
  10872. "Return a list of values in a multivalued property."
  10873. (let* ((value (org-entry-get pom property))
  10874. (values (and value (org-split-string value "[ \t]"))))
  10875. (mapcar 'org-entry-restore-space values)))
  10876. (defun org-entry-put-multivalued-property (pom property &rest values)
  10877. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10878. VALUES should be a list of strings. Spaces will be protected."
  10879. (org-entry-put pom property
  10880. (mapconcat 'org-entry-protect-space values " "))
  10881. (let* ((value (org-entry-get pom property))
  10882. (values (and value (org-split-string value "[ \t]"))))
  10883. (mapcar 'org-entry-restore-space values)))
  10884. (defun org-entry-protect-space (s)
  10885. "Protect spaces and newline in string S."
  10886. (while (string-match " " s)
  10887. (setq s (replace-match "%20" t t s)))
  10888. (while (string-match "\n" s)
  10889. (setq s (replace-match "%0A" t t s)))
  10890. s)
  10891. (defun org-entry-restore-space (s)
  10892. "Restore spaces and newline in string S."
  10893. (while (string-match "%20" s)
  10894. (setq s (replace-match " " t t s)))
  10895. (while (string-match "%0A" s)
  10896. (setq s (replace-match "\n" t t s)))
  10897. s)
  10898. (defvar org-entry-property-inherited-from (make-marker)
  10899. "Marker pointing to the entry from where a property was inherited.
  10900. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10901. location of the entry where the inheritance search matched. If there was
  10902. no match, the marker will point nowhere.
  10903. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10904. is set.")
  10905. (defun org-entry-get-with-inheritance (property)
  10906. "Get entry property, and search higher levels if not present."
  10907. (move-marker org-entry-property-inherited-from nil)
  10908. (let (tmp)
  10909. (save-excursion
  10910. (save-restriction
  10911. (widen)
  10912. (catch 'ex
  10913. (while t
  10914. (when (setq tmp (org-entry-get nil property))
  10915. (org-back-to-heading t)
  10916. (move-marker org-entry-property-inherited-from (point))
  10917. (throw 'ex tmp))
  10918. (or (org-up-heading-safe) (throw 'ex nil)))))
  10919. (or tmp
  10920. (cdr (assoc property org-file-properties))
  10921. (cdr (assoc property org-global-properties))
  10922. (cdr (assoc property org-global-properties-fixed))))))
  10923. (defun org-entry-put (pom property value)
  10924. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10925. (org-with-point-at pom
  10926. (org-back-to-heading t)
  10927. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10928. range)
  10929. (cond
  10930. ((equal property "TODO")
  10931. (when (and (stringp value) (string-match "\\S-" value)
  10932. (not (member value org-todo-keywords-1)))
  10933. (error "\"%s\" is not a valid TODO state" value))
  10934. (if (or (not value)
  10935. (not (string-match "\\S-" value)))
  10936. (setq value 'none))
  10937. (org-todo value)
  10938. (org-set-tags nil 'align))
  10939. ((equal property "PRIORITY")
  10940. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10941. (string-to-char value) ?\ ))
  10942. (org-set-tags nil 'align))
  10943. ((equal property "SCHEDULED")
  10944. (if (re-search-forward org-scheduled-time-regexp end t)
  10945. (cond
  10946. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10947. ((eq value 'later) (org-timestamp-change 1 'day))
  10948. (t (call-interactively 'org-schedule)))
  10949. (call-interactively 'org-schedule)))
  10950. ((equal property "DEADLINE")
  10951. (if (re-search-forward org-deadline-time-regexp end t)
  10952. (cond
  10953. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10954. ((eq value 'later) (org-timestamp-change 1 'day))
  10955. (t (call-interactively 'org-deadline)))
  10956. (call-interactively 'org-deadline)))
  10957. ((member property org-special-properties)
  10958. (error "The %s property can not yet be set with `org-entry-put'"
  10959. property))
  10960. (t ; a non-special property
  10961. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10962. (setq range (org-get-property-block beg end 'force))
  10963. (goto-char (car range))
  10964. (if (re-search-forward
  10965. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10966. (progn
  10967. (delete-region (match-beginning 1) (match-end 1))
  10968. (goto-char (match-beginning 1)))
  10969. (goto-char (cdr range))
  10970. (insert "\n")
  10971. (backward-char 1)
  10972. (org-indent-line-function)
  10973. (insert ":" property ":"))
  10974. (and value (insert " " value))
  10975. (org-indent-line-function)))))))
  10976. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10977. "Get all property keys in the current buffer.
  10978. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10979. like tags and TODO state.
  10980. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10981. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10982. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10983. formats in the current buffer."
  10984. (let (rtn range cfmt s p)
  10985. (save-excursion
  10986. (save-restriction
  10987. (widen)
  10988. (goto-char (point-min))
  10989. (while (re-search-forward org-property-start-re nil t)
  10990. (setq range (org-get-property-block))
  10991. (goto-char (car range))
  10992. (while (re-search-forward
  10993. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10994. (cdr range) t)
  10995. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10996. (outline-next-heading))))
  10997. (when include-specials
  10998. (setq rtn (append org-special-properties rtn)))
  10999. (when include-defaults
  11000. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11001. (add-to-list 'rtn org-effort-property))
  11002. (when include-columns
  11003. (save-excursion
  11004. (save-restriction
  11005. (widen)
  11006. (goto-char (point-min))
  11007. (while (re-search-forward
  11008. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11009. nil t)
  11010. (setq cfmt (match-string 2) s 0)
  11011. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11012. cfmt s)
  11013. (setq s (match-end 0)
  11014. p (match-string 1 cfmt))
  11015. (unless (or (equal p "ITEM")
  11016. (member p org-special-properties))
  11017. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11018. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11019. (defun org-property-values (key)
  11020. "Return a list of all values of property KEY."
  11021. (save-excursion
  11022. (save-restriction
  11023. (widen)
  11024. (goto-char (point-min))
  11025. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11026. values)
  11027. (while (re-search-forward re nil t)
  11028. (add-to-list 'values (org-trim (match-string 1))))
  11029. (delete "" values)))))
  11030. (defun org-insert-property-drawer ()
  11031. "Insert a property drawer into the current entry."
  11032. (interactive)
  11033. (org-back-to-heading t)
  11034. (looking-at outline-regexp)
  11035. (let ((indent (if org-adapt-indentation
  11036. (- (match-end 0)(match-beginning 0))
  11037. 0))
  11038. (beg (point))
  11039. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11040. end hiddenp)
  11041. (outline-next-heading)
  11042. (setq end (point))
  11043. (goto-char beg)
  11044. (while (re-search-forward re end t))
  11045. (setq hiddenp (org-invisible-p))
  11046. (end-of-line 1)
  11047. (and (equal (char-after) ?\n) (forward-char 1))
  11048. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11049. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11050. ;; just skip this line
  11051. (beginning-of-line 2)
  11052. ;; Drawer start, find the end
  11053. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11054. (beginning-of-line 1)))
  11055. (org-skip-over-state-notes)
  11056. (skip-chars-backward " \t\n\r")
  11057. (if (eq (char-before) ?*) (forward-char 1))
  11058. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11059. (beginning-of-line 0)
  11060. (org-indent-to-column indent)
  11061. (beginning-of-line 2)
  11062. (org-indent-to-column indent)
  11063. (beginning-of-line 0)
  11064. (if hiddenp
  11065. (save-excursion
  11066. (org-back-to-heading t)
  11067. (hide-entry))
  11068. (org-flag-drawer t))))
  11069. (defun org-set-property (property value)
  11070. "In the current entry, set PROPERTY to VALUE.
  11071. When called interactively, this will prompt for a property name, offering
  11072. completion on existing and default properties. And then it will prompt
  11073. for a value, offering completion either on allowed values (via an inherited
  11074. xxx_ALL property) or on existing values in other instances of this property
  11075. in the current file."
  11076. (interactive
  11077. (let* ((completion-ignore-case t)
  11078. (keys (org-buffer-property-keys nil t t))
  11079. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  11080. (prop (if (member prop0 keys)
  11081. prop0
  11082. (or (cdr (assoc (downcase prop0)
  11083. (mapcar (lambda (x) (cons (downcase x) x))
  11084. keys)))
  11085. prop0)))
  11086. (cur (org-entry-get nil prop))
  11087. (allowed (org-property-get-allowed-values nil prop 'table))
  11088. (existing (mapcar 'list (org-property-values prop)))
  11089. (val (if allowed
  11090. (org-completing-read "Value: " allowed nil 'req-match)
  11091. (let (org-completion-use-ido)
  11092. (org-completing-read
  11093. (concat "Value" (if (and cur (string-match "\\S-" cur))
  11094. (concat "[" cur "]") "")
  11095. ": ")
  11096. existing nil nil "" nil cur)))))
  11097. (list prop (if (equal val "") cur val))))
  11098. (unless (equal (org-entry-get nil property) value)
  11099. (org-entry-put nil property value)))
  11100. (defun org-delete-property (property)
  11101. "In the current entry, delete PROPERTY."
  11102. (interactive
  11103. (let* ((completion-ignore-case t)
  11104. (prop (org-ido-completing-read
  11105. "Property: " (org-entry-properties nil 'standard))))
  11106. (list prop)))
  11107. (message "Property %s %s" property
  11108. (if (org-entry-delete nil property)
  11109. "deleted"
  11110. "was not present in the entry")))
  11111. (defun org-delete-property-globally (property)
  11112. "Remove PROPERTY globally, from all entries."
  11113. (interactive
  11114. (let* ((completion-ignore-case t)
  11115. (prop (org-ido-completing-read
  11116. "Globally remove property: "
  11117. (mapcar 'list (org-buffer-property-keys)))))
  11118. (list prop)))
  11119. (save-excursion
  11120. (save-restriction
  11121. (widen)
  11122. (goto-char (point-min))
  11123. (let ((cnt 0))
  11124. (while (re-search-forward
  11125. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11126. nil t)
  11127. (setq cnt (1+ cnt))
  11128. (replace-match ""))
  11129. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11130. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11131. (defun org-compute-property-at-point ()
  11132. "Compute the property at point.
  11133. This looks for an enclosing column format, extracts the operator and
  11134. then applies it to the property in the column format's scope."
  11135. (interactive)
  11136. (unless (org-at-property-p)
  11137. (error "Not at a property"))
  11138. (let ((prop (org-match-string-no-properties 2)))
  11139. (org-columns-get-format-and-top-level)
  11140. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11141. (error "No operator defined for property %s" prop))
  11142. (org-columns-compute prop)))
  11143. (defun org-property-get-allowed-values (pom property &optional table)
  11144. "Get allowed values for the property PROPERTY.
  11145. When TABLE is non-nil, return an alist that can directly be used for
  11146. completion."
  11147. (let (vals)
  11148. (cond
  11149. ((equal property "TODO")
  11150. (setq vals (org-with-point-at pom
  11151. (append org-todo-keywords-1 '("")))))
  11152. ((equal property "PRIORITY")
  11153. (let ((n org-lowest-priority))
  11154. (while (>= n org-highest-priority)
  11155. (push (char-to-string n) vals)
  11156. (setq n (1- n)))))
  11157. ((member property org-special-properties))
  11158. (t
  11159. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11160. (when (and vals (string-match "\\S-" vals))
  11161. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11162. (setq vals (mapcar (lambda (x)
  11163. (cond ((stringp x) x)
  11164. ((numberp x) (number-to-string x))
  11165. ((symbolp x) (symbol-name x))
  11166. (t "???")))
  11167. vals)))))
  11168. (if table (mapcar 'list vals) vals)))
  11169. (defun org-property-previous-allowed-value (&optional previous)
  11170. "Switch to the next allowed value for this property."
  11171. (interactive)
  11172. (org-property-next-allowed-value t))
  11173. (defun org-property-next-allowed-value (&optional previous)
  11174. "Switch to the next allowed value for this property."
  11175. (interactive)
  11176. (unless (org-at-property-p)
  11177. (error "Not at a property"))
  11178. (let* ((key (match-string 2))
  11179. (value (match-string 3))
  11180. (allowed (or (org-property-get-allowed-values (point) key)
  11181. (and (member value '("[ ]" "[-]" "[X]"))
  11182. '("[ ]" "[X]"))))
  11183. nval)
  11184. (unless allowed
  11185. (error "Allowed values for this property have not been defined"))
  11186. (if previous (setq allowed (reverse allowed)))
  11187. (if (member value allowed)
  11188. (setq nval (car (cdr (member value allowed)))))
  11189. (setq nval (or nval (car allowed)))
  11190. (if (equal nval value)
  11191. (error "Only one allowed value for this property"))
  11192. (org-at-property-p)
  11193. (replace-match (concat " :" key ": " nval) t t)
  11194. (org-indent-line-function)
  11195. (beginning-of-line 1)
  11196. (skip-chars-forward " \t")))
  11197. (defun org-find-entry-with-id (ident)
  11198. "Locate the entry that contains the ID property with exact value IDENT.
  11199. IDENT can be a string, a symbol or a number, this function will search for
  11200. the string representation of it.
  11201. Return the position where this entry starts, or nil if there is no such entry."
  11202. (interactive "sID: ")
  11203. (let ((id (cond
  11204. ((stringp ident) ident)
  11205. ((symbol-name ident) (symbol-name ident))
  11206. ((numberp ident) (number-to-string ident))
  11207. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11208. (case-fold-search nil))
  11209. (save-excursion
  11210. (save-restriction
  11211. (widen)
  11212. (goto-char (point-min))
  11213. (when (re-search-forward
  11214. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11215. nil t)
  11216. (org-back-to-heading)
  11217. (point))))))
  11218. ;;;; Timestamps
  11219. (defvar org-last-changed-timestamp nil)
  11220. (defvar org-last-inserted-timestamp nil
  11221. "The last time stamp inserted with `org-insert-time-stamp'.")
  11222. (defvar org-time-was-given) ; dynamically scoped parameter
  11223. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11224. (defvar org-ts-what) ; dynamically scoped parameter
  11225. (defun org-time-stamp (arg &optional inactive)
  11226. "Prompt for a date/time and insert a time stamp.
  11227. If the user specifies a time like HH:MM, or if this command is called
  11228. with a prefix argument, the time stamp will contain date and time.
  11229. Otherwise, only the date will be included. All parts of a date not
  11230. specified by the user will be filled in from the current date/time.
  11231. So if you press just return without typing anything, the time stamp
  11232. will represent the current date/time. If there is already a timestamp
  11233. at the cursor, it will be modified."
  11234. (interactive "P")
  11235. (let* ((ts nil)
  11236. (default-time
  11237. ;; Default time is either today, or, when entering a range,
  11238. ;; the range start.
  11239. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11240. (save-excursion
  11241. (re-search-backward
  11242. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11243. (- (point) 20) t)))
  11244. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11245. (current-time)))
  11246. (default-input (and ts (org-get-compact-tod ts)))
  11247. org-time-was-given org-end-time-was-given time)
  11248. (cond
  11249. ((and (org-at-timestamp-p t)
  11250. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11251. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11252. (insert "--")
  11253. (setq time (let ((this-command this-command))
  11254. (org-read-date arg 'totime nil nil
  11255. default-time default-input)))
  11256. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11257. ((org-at-timestamp-p t)
  11258. (setq time (let ((this-command this-command))
  11259. (org-read-date arg 'totime nil nil default-time default-input)))
  11260. (when (org-at-timestamp-p t) ; just to get the match data
  11261. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11262. (replace-match "")
  11263. (setq org-last-changed-timestamp
  11264. (org-insert-time-stamp
  11265. time (or org-time-was-given arg)
  11266. inactive nil nil (list org-end-time-was-given))))
  11267. (message "Timestamp updated"))
  11268. (t
  11269. (setq time (let ((this-command this-command))
  11270. (org-read-date arg 'totime nil nil default-time default-input)))
  11271. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11272. nil nil (list org-end-time-was-given))))))
  11273. ;; FIXME: can we use this for something else, like computing time differences?
  11274. (defun org-get-compact-tod (s)
  11275. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11276. (let* ((t1 (match-string 1 s))
  11277. (h1 (string-to-number (match-string 2 s)))
  11278. (m1 (string-to-number (match-string 3 s)))
  11279. (t2 (and (match-end 4) (match-string 5 s)))
  11280. (h2 (and t2 (string-to-number (match-string 6 s))))
  11281. (m2 (and t2 (string-to-number (match-string 7 s))))
  11282. dh dm)
  11283. (if (not t2)
  11284. t1
  11285. (setq dh (- h2 h1) dm (- m2 m1))
  11286. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11287. (concat t1 "+" (number-to-string dh)
  11288. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11289. (defun org-time-stamp-inactive (&optional arg)
  11290. "Insert an inactive time stamp.
  11291. An inactive time stamp is enclosed in square brackets instead of angle
  11292. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11293. does not link to the calendar and cannot be changed with the S-cursor keys.
  11294. So these are more for recording a certain time/date."
  11295. (interactive "P")
  11296. (org-time-stamp arg 'inactive))
  11297. (defvar org-date-ovl (org-make-overlay 1 1))
  11298. (org-overlay-put org-date-ovl 'face 'org-warning)
  11299. (org-detach-overlay org-date-ovl)
  11300. (defvar org-ans1) ; dynamically scoped parameter
  11301. (defvar org-ans2) ; dynamically scoped parameter
  11302. (defvar org-plain-time-of-day-regexp) ; defined below
  11303. (defvar org-overriding-default-time nil) ; dynamically scoped
  11304. (defvar org-read-date-overlay nil)
  11305. (defvar org-dcst nil) ; dynamically scoped
  11306. (defvar org-read-date-history nil)
  11307. (defun org-read-date (&optional with-time to-time from-string prompt
  11308. default-time default-input)
  11309. "Read a date, possibly a time, and make things smooth for the user.
  11310. The prompt will suggest to enter an ISO date, but you can also enter anything
  11311. which will at least partially be understood by `parse-time-string'.
  11312. Unrecognized parts of the date will default to the current day, month, year,
  11313. hour and minute. If this command is called to replace a timestamp at point,
  11314. of to enter the second timestamp of a range, the default time is taken from the
  11315. existing stamp. For example,
  11316. 3-2-5 --> 2003-02-05
  11317. feb 15 --> currentyear-02-15
  11318. sep 12 9 --> 2009-09-12
  11319. 12:45 --> today 12:45
  11320. 22 sept 0:34 --> currentyear-09-22 0:34
  11321. 12 --> currentyear-currentmonth-12
  11322. Fri --> nearest Friday (today or later)
  11323. etc.
  11324. Furthermore you can specify a relative date by giving, as the *first* thing
  11325. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11326. change in days weeks, months, years.
  11327. With a single plus or minus, the date is relative to today. With a double
  11328. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11329. +4d --> four days from today
  11330. +4 --> same as above
  11331. +2w --> two weeks from today
  11332. ++5 --> five days from default date
  11333. The function understands only English month and weekday abbreviations,
  11334. but this can be configured with the variables `parse-time-months' and
  11335. `parse-time-weekdays'.
  11336. While prompting, a calendar is popped up - you can also select the
  11337. date with the mouse (button 1). The calendar shows a period of three
  11338. months. To scroll it to other months, use the keys `>' and `<'.
  11339. If you don't like the calendar, turn it off with
  11340. \(setq org-read-date-popup-calendar nil)
  11341. With optional argument TO-TIME, the date will immediately be converted
  11342. to an internal time.
  11343. With an optional argument WITH-TIME, the prompt will suggest to also
  11344. insert a time. Note that when WITH-TIME is not set, you can still
  11345. enter a time, and this function will inform the calling routine about
  11346. this change. The calling routine may then choose to change the format
  11347. used to insert the time stamp into the buffer to include the time.
  11348. With optional argument FROM-STRING, read from this string instead from
  11349. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11350. the time/date that is used for everything that is not specified by the
  11351. user."
  11352. (require 'parse-time)
  11353. (let* ((org-time-stamp-rounding-minutes
  11354. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11355. (org-dcst org-display-custom-times)
  11356. (ct (org-current-time))
  11357. (def (or org-overriding-default-time default-time ct))
  11358. (defdecode (decode-time def))
  11359. (dummy (progn
  11360. (when (< (nth 2 defdecode) org-extend-today-until)
  11361. (setcar (nthcdr 2 defdecode) -1)
  11362. (setcar (nthcdr 1 defdecode) 59)
  11363. (setq def (apply 'encode-time defdecode)
  11364. defdecode (decode-time def)))))
  11365. (calendar-move-hook nil)
  11366. (calendar-view-diary-initially-flag nil)
  11367. (view-diary-entries-initially nil)
  11368. (calendar-view-holidays-initially-flag nil)
  11369. (view-calendar-holidays-initially nil)
  11370. (timestr (format-time-string
  11371. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11372. (prompt (concat (if prompt (concat prompt " ") "")
  11373. (format "Date+time [%s]: " timestr)))
  11374. ans (org-ans0 "") org-ans1 org-ans2 final)
  11375. (cond
  11376. (from-string (setq ans from-string))
  11377. (org-read-date-popup-calendar
  11378. (save-excursion
  11379. (save-window-excursion
  11380. (calendar)
  11381. (calendar-forward-day (- (time-to-days def)
  11382. (calendar-absolute-from-gregorian
  11383. (calendar-current-date))))
  11384. (org-eval-in-calendar nil t)
  11385. (let* ((old-map (current-local-map))
  11386. (map (copy-keymap calendar-mode-map))
  11387. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11388. (org-defkey map (kbd "RET") 'org-calendar-select)
  11389. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11390. 'org-calendar-select-mouse)
  11391. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11392. 'org-calendar-select-mouse)
  11393. (org-defkey minibuffer-local-map [(meta shift left)]
  11394. (lambda () (interactive)
  11395. (org-eval-in-calendar '(calendar-backward-month 1))))
  11396. (org-defkey minibuffer-local-map [(meta shift right)]
  11397. (lambda () (interactive)
  11398. (org-eval-in-calendar '(calendar-forward-month 1))))
  11399. (org-defkey minibuffer-local-map [(meta shift up)]
  11400. (lambda () (interactive)
  11401. (org-eval-in-calendar '(calendar-backward-year 1))))
  11402. (org-defkey minibuffer-local-map [(meta shift down)]
  11403. (lambda () (interactive)
  11404. (org-eval-in-calendar '(calendar-forward-year 1))))
  11405. (org-defkey minibuffer-local-map [?\e (shift left)]
  11406. (lambda () (interactive)
  11407. (org-eval-in-calendar '(calendar-backward-month 1))))
  11408. (org-defkey minibuffer-local-map [?\e (shift right)]
  11409. (lambda () (interactive)
  11410. (org-eval-in-calendar '(calendar-forward-month 1))))
  11411. (org-defkey minibuffer-local-map [?\e (shift up)]
  11412. (lambda () (interactive)
  11413. (org-eval-in-calendar '(calendar-backward-year 1))))
  11414. (org-defkey minibuffer-local-map [?\e (shift down)]
  11415. (lambda () (interactive)
  11416. (org-eval-in-calendar '(calendar-forward-year 1))))
  11417. (org-defkey minibuffer-local-map [(shift up)]
  11418. (lambda () (interactive)
  11419. (org-eval-in-calendar '(calendar-backward-week 1))))
  11420. (org-defkey minibuffer-local-map [(shift down)]
  11421. (lambda () (interactive)
  11422. (org-eval-in-calendar '(calendar-forward-week 1))))
  11423. (org-defkey minibuffer-local-map [(shift left)]
  11424. (lambda () (interactive)
  11425. (org-eval-in-calendar '(calendar-backward-day 1))))
  11426. (org-defkey minibuffer-local-map [(shift right)]
  11427. (lambda () (interactive)
  11428. (org-eval-in-calendar '(calendar-forward-day 1))))
  11429. (org-defkey minibuffer-local-map ">"
  11430. (lambda () (interactive)
  11431. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11432. (org-defkey minibuffer-local-map "<"
  11433. (lambda () (interactive)
  11434. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11435. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11436. (unwind-protect
  11437. (progn
  11438. (use-local-map map)
  11439. (add-hook 'post-command-hook 'org-read-date-display)
  11440. (setq org-ans0 (read-string prompt default-input
  11441. 'org-read-date-history nil))
  11442. ;; org-ans0: from prompt
  11443. ;; org-ans1: from mouse click
  11444. ;; org-ans2: from calendar motion
  11445. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11446. (remove-hook 'post-command-hook 'org-read-date-display)
  11447. (use-local-map old-map)
  11448. (when org-read-date-overlay
  11449. (org-delete-overlay org-read-date-overlay)
  11450. (setq org-read-date-overlay nil)))))))
  11451. (t ; Naked prompt only
  11452. (unwind-protect
  11453. (setq ans (read-string prompt default-input
  11454. 'org-read-date-history timestr))
  11455. (when org-read-date-overlay
  11456. (org-delete-overlay org-read-date-overlay)
  11457. (setq org-read-date-overlay nil)))))
  11458. (setq final (org-read-date-analyze ans def defdecode))
  11459. (if to-time
  11460. (apply 'encode-time final)
  11461. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11462. (format "%04d-%02d-%02d %02d:%02d"
  11463. (nth 5 final) (nth 4 final) (nth 3 final)
  11464. (nth 2 final) (nth 1 final))
  11465. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11466. (defvar def)
  11467. (defvar defdecode)
  11468. (defvar with-time)
  11469. (defun org-read-date-display ()
  11470. "Display the current date prompt interpretation in the minibuffer."
  11471. (when org-read-date-display-live
  11472. (when org-read-date-overlay
  11473. (org-delete-overlay org-read-date-overlay))
  11474. (let ((p (point)))
  11475. (end-of-line 1)
  11476. (while (not (equal (buffer-substring
  11477. (max (point-min) (- (point) 4)) (point))
  11478. " "))
  11479. (insert " "))
  11480. (goto-char p))
  11481. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11482. " " (or org-ans1 org-ans2)))
  11483. (org-end-time-was-given nil)
  11484. (f (org-read-date-analyze ans def defdecode))
  11485. (fmts (if org-dcst
  11486. org-time-stamp-custom-formats
  11487. org-time-stamp-formats))
  11488. (fmt (if (or with-time
  11489. (and (boundp 'org-time-was-given) org-time-was-given))
  11490. (cdr fmts)
  11491. (car fmts)))
  11492. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11493. (when (and org-end-time-was-given
  11494. (string-match org-plain-time-of-day-regexp txt))
  11495. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11496. org-end-time-was-given
  11497. (substring txt (match-end 0)))))
  11498. (setq org-read-date-overlay
  11499. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11500. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11501. (defun org-read-date-analyze (ans def defdecode)
  11502. "Analyse the combined answer of the date prompt."
  11503. ;; FIXME: cleanup and comment
  11504. (let (delta deltan deltaw deltadef year month day
  11505. hour minute second wday pm h2 m2 tl wday1
  11506. iso-year iso-weekday iso-week iso-year iso-date)
  11507. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11508. (setq ans "+0"))
  11509. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11510. (setq ans (replace-match "" t t ans)
  11511. deltan (car delta)
  11512. deltaw (nth 1 delta)
  11513. deltadef (nth 2 delta)))
  11514. ;; Check if there is an iso week date in there
  11515. ;; If yes, sore the info and postpone interpreting it until the rest
  11516. ;; of the parsing is done
  11517. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11518. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11519. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11520. iso-week (string-to-number (match-string 2 ans)))
  11521. (setq ans (replace-match "" t t ans)))
  11522. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11523. (when (string-match
  11524. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11525. (setq year (if (match-end 2)
  11526. (string-to-number (match-string 2 ans))
  11527. (string-to-number (format-time-string "%Y")))
  11528. month (string-to-number (match-string 3 ans))
  11529. day (string-to-number (match-string 4 ans)))
  11530. (if (< year 100) (setq year (+ 2000 year)))
  11531. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11532. t nil ans)))
  11533. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11534. ;; If there is a time with am/pm, and *no* time without it, we convert
  11535. ;; so that matching will be successful.
  11536. (loop for i from 1 to 2 do ; twice, for end time as well
  11537. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11538. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11539. (setq hour (string-to-number (match-string 1 ans))
  11540. minute (if (match-end 3)
  11541. (string-to-number (match-string 3 ans))
  11542. 0)
  11543. pm (equal ?p
  11544. (string-to-char (downcase (match-string 4 ans)))))
  11545. (if (and (= hour 12) (not pm))
  11546. (setq hour 0)
  11547. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11548. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11549. t t ans))))
  11550. ;; Check if a time range is given as a duration
  11551. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11552. (setq hour (string-to-number (match-string 1 ans))
  11553. h2 (+ hour (string-to-number (match-string 3 ans)))
  11554. minute (string-to-number (match-string 2 ans))
  11555. m2 (+ minute (if (match-end 5) (string-to-number
  11556. (match-string 5 ans))0)))
  11557. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11558. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11559. t t ans)))
  11560. ;; Check if there is a time range
  11561. (when (boundp 'org-end-time-was-given)
  11562. (setq org-time-was-given nil)
  11563. (when (and (string-match org-plain-time-of-day-regexp ans)
  11564. (match-end 8))
  11565. (setq org-end-time-was-given (match-string 8 ans))
  11566. (setq ans (concat (substring ans 0 (match-beginning 7))
  11567. (substring ans (match-end 7))))))
  11568. (setq tl (parse-time-string ans)
  11569. day (or (nth 3 tl) (nth 3 defdecode))
  11570. month (or (nth 4 tl)
  11571. (if (and org-read-date-prefer-future
  11572. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11573. (1+ (nth 4 defdecode))
  11574. (nth 4 defdecode)))
  11575. year (or (nth 5 tl)
  11576. (if (and org-read-date-prefer-future
  11577. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11578. (1+ (nth 5 defdecode))
  11579. (nth 5 defdecode)))
  11580. hour (or (nth 2 tl) (nth 2 defdecode))
  11581. minute (or (nth 1 tl) (nth 1 defdecode))
  11582. second (or (nth 0 tl) 0)
  11583. wday (nth 6 tl))
  11584. ;; Special date definitions below
  11585. (cond
  11586. (iso-week
  11587. ;; There was an iso week
  11588. (setq year (or iso-year year)
  11589. day (or iso-weekday wday 1)
  11590. wday nil ; to make sure that the trigger below does not match
  11591. iso-date (calendar-gregorian-from-absolute
  11592. (calendar-absolute-from-iso
  11593. (list iso-week day year))))
  11594. ; FIXME: Should we also push ISO weeks into the future?
  11595. ; (when (and org-read-date-prefer-future
  11596. ; (not iso-year)
  11597. ; (< (calendar-absolute-from-gregorian iso-date)
  11598. ; (time-to-days (current-time))))
  11599. ; (setq year (1+ year)
  11600. ; iso-date (calendar-gregorian-from-absolute
  11601. ; (calendar-absolute-from-iso
  11602. ; (list iso-week day year)))))
  11603. (setq month (car iso-date)
  11604. year (nth 2 iso-date)
  11605. day (nth 1 iso-date)))
  11606. (deltan
  11607. (unless deltadef
  11608. (let ((now (decode-time (current-time))))
  11609. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11610. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11611. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11612. ((equal deltaw "m") (setq month (+ month deltan)))
  11613. ((equal deltaw "y") (setq year (+ year deltan)))))
  11614. ((and wday (not (nth 3 tl)))
  11615. ;; Weekday was given, but no day, so pick that day in the week
  11616. ;; on or after the derived date.
  11617. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11618. (unless (equal wday wday1)
  11619. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11620. (if (and (boundp 'org-time-was-given)
  11621. (nth 2 tl))
  11622. (setq org-time-was-given t))
  11623. (if (< year 100) (setq year (+ 2000 year)))
  11624. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11625. (list second minute hour day month year)))
  11626. (defvar parse-time-weekdays)
  11627. (defun org-read-date-get-relative (s today default)
  11628. "Check string S for special relative date string.
  11629. TODAY and DEFAULT are internal times, for today and for a default.
  11630. Return shift list (N what def-flag)
  11631. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11632. N is the number of WHATs to shift.
  11633. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11634. the DEFAULT date rather than TODAY."
  11635. (when (and
  11636. (string-match
  11637. (concat
  11638. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11639. "\\([0-9]+\\)?"
  11640. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11641. "\\([ \t]\\|$\\)") s)
  11642. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11643. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11644. (string-to-char (substring (match-string 1 s) -1))
  11645. ?+))
  11646. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11647. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11648. (what (if (match-end 3) (match-string 3 s) "d"))
  11649. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11650. (date (if rel default today))
  11651. (wday (nth 6 (decode-time date)))
  11652. delta)
  11653. (if wday1
  11654. (progn
  11655. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11656. (if (= dir ?-) (setq delta (- delta 7)))
  11657. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11658. (list delta "d" rel))
  11659. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11660. (defun org-eval-in-calendar (form &optional keepdate)
  11661. "Eval FORM in the calendar window and return to current window.
  11662. Also, store the cursor date in variable org-ans2."
  11663. (let ((sw (selected-window)))
  11664. (select-window (get-buffer-window "*Calendar*"))
  11665. (eval form)
  11666. (when (and (not keepdate) (calendar-cursor-to-date))
  11667. (let* ((date (calendar-cursor-to-date))
  11668. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11669. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11670. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11671. (select-window sw)))
  11672. (defun org-calendar-select ()
  11673. "Return to `org-read-date' with the date currently selected.
  11674. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11675. (interactive)
  11676. (when (calendar-cursor-to-date)
  11677. (let* ((date (calendar-cursor-to-date))
  11678. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11679. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11680. (if (active-minibuffer-window) (exit-minibuffer))))
  11681. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11682. "Insert a date stamp for the date given by the internal TIME.
  11683. WITH-HM means, use the stamp format that includes the time of the day.
  11684. INACTIVE means use square brackets instead of angular ones, so that the
  11685. stamp will not contribute to the agenda.
  11686. PRE and POST are optional strings to be inserted before and after the
  11687. stamp.
  11688. The command returns the inserted time stamp."
  11689. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11690. stamp)
  11691. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11692. (insert-before-markers (or pre ""))
  11693. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11694. (when (listp extra)
  11695. (setq extra (car extra))
  11696. (if (and (stringp extra)
  11697. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11698. (setq extra (format "-%02d:%02d"
  11699. (string-to-number (match-string 1 extra))
  11700. (string-to-number (match-string 2 extra))))
  11701. (setq extra nil)))
  11702. (when extra
  11703. (backward-char 1)
  11704. (insert-before-markers extra)
  11705. (forward-char 1))
  11706. (insert-before-markers (or post ""))
  11707. (setq org-last-inserted-timestamp stamp)))
  11708. (defun org-toggle-time-stamp-overlays ()
  11709. "Toggle the use of custom time stamp formats."
  11710. (interactive)
  11711. (setq org-display-custom-times (not org-display-custom-times))
  11712. (unless org-display-custom-times
  11713. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11714. (while (setq p (next-single-property-change p 'display))
  11715. (if (and (get-text-property p 'display)
  11716. (eq (get-text-property p 'face) 'org-date))
  11717. (remove-text-properties
  11718. p (setq p (next-single-property-change p 'display))
  11719. '(display t))))
  11720. (set-buffer-modified-p bmp)))
  11721. (if (featurep 'xemacs)
  11722. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11723. (org-restart-font-lock)
  11724. (setq org-table-may-need-update t)
  11725. (if org-display-custom-times
  11726. (message "Time stamps are overlayed with custom format")
  11727. (message "Time stamp overlays removed")))
  11728. (defun org-display-custom-time (beg end)
  11729. "Overlay modified time stamp format over timestamp between BEG and END."
  11730. (let* ((ts (buffer-substring beg end))
  11731. t1 w1 with-hm tf time str w2 (off 0))
  11732. (save-match-data
  11733. (setq t1 (org-parse-time-string ts t))
  11734. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11735. (setq off (- (match-end 0) (match-beginning 0)))))
  11736. (setq end (- end off))
  11737. (setq w1 (- end beg)
  11738. with-hm (and (nth 1 t1) (nth 2 t1))
  11739. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11740. time (org-fix-decoded-time t1)
  11741. str (org-add-props
  11742. (format-time-string
  11743. (substring tf 1 -1) (apply 'encode-time time))
  11744. nil 'mouse-face 'highlight)
  11745. w2 (length str))
  11746. (if (not (= w2 w1))
  11747. (add-text-properties (1+ beg) (+ 2 beg)
  11748. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11749. (if (featurep 'xemacs)
  11750. (progn
  11751. (put-text-property beg end 'invisible t)
  11752. (put-text-property beg end 'end-glyph (make-glyph str)))
  11753. (put-text-property beg end 'display str))))
  11754. (defun org-translate-time (string)
  11755. "Translate all timestamps in STRING to custom format.
  11756. But do this only if the variable `org-display-custom-times' is set."
  11757. (when org-display-custom-times
  11758. (save-match-data
  11759. (let* ((start 0)
  11760. (re org-ts-regexp-both)
  11761. t1 with-hm inactive tf time str beg end)
  11762. (while (setq start (string-match re string start))
  11763. (setq beg (match-beginning 0)
  11764. end (match-end 0)
  11765. t1 (save-match-data
  11766. (org-parse-time-string (substring string beg end) t))
  11767. with-hm (and (nth 1 t1) (nth 2 t1))
  11768. inactive (equal (substring string beg (1+ beg)) "[")
  11769. tf (funcall (if with-hm 'cdr 'car)
  11770. org-time-stamp-custom-formats)
  11771. time (org-fix-decoded-time t1)
  11772. str (format-time-string
  11773. (concat
  11774. (if inactive "[" "<") (substring tf 1 -1)
  11775. (if inactive "]" ">"))
  11776. (apply 'encode-time time))
  11777. string (replace-match str t t string)
  11778. start (+ start (length str)))))))
  11779. string)
  11780. (defun org-fix-decoded-time (time)
  11781. "Set 0 instead of nil for the first 6 elements of time.
  11782. Don't touch the rest."
  11783. (let ((n 0))
  11784. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11785. (defun org-days-to-time (timestamp-string)
  11786. "Difference between TIMESTAMP-STRING and now in days."
  11787. (- (time-to-days (org-time-string-to-time timestamp-string))
  11788. (time-to-days (current-time))))
  11789. (defun org-deadline-close (timestamp-string &optional ndays)
  11790. "Is the time in TIMESTAMP-STRING close to the current date?"
  11791. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11792. (and (< (org-days-to-time timestamp-string) ndays)
  11793. (not (org-entry-is-done-p))))
  11794. (defun org-get-wdays (ts)
  11795. "Get the deadline lead time appropriate for timestring TS."
  11796. (cond
  11797. ((<= org-deadline-warning-days 0)
  11798. ;; 0 or negative, enforce this value no matter what
  11799. (- org-deadline-warning-days))
  11800. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11801. ;; lead time is specified.
  11802. (floor (* (string-to-number (match-string 1 ts))
  11803. (cdr (assoc (match-string 2 ts)
  11804. '(("d" . 1) ("w" . 7)
  11805. ("m" . 30.4) ("y" . 365.25)))))))
  11806. ;; go for the default.
  11807. (t org-deadline-warning-days)))
  11808. (defun org-calendar-select-mouse (ev)
  11809. "Return to `org-read-date' with the date currently selected.
  11810. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11811. (interactive "e")
  11812. (mouse-set-point ev)
  11813. (when (calendar-cursor-to-date)
  11814. (let* ((date (calendar-cursor-to-date))
  11815. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11816. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11817. (if (active-minibuffer-window) (exit-minibuffer))))
  11818. (defun org-check-deadlines (ndays)
  11819. "Check if there are any deadlines due or past due.
  11820. A deadline is considered due if it happens within `org-deadline-warning-days'
  11821. days from today's date. If the deadline appears in an entry marked DONE,
  11822. it is not shown. The prefix arg NDAYS can be used to test that many
  11823. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11824. (interactive "P")
  11825. (let* ((org-warn-days
  11826. (cond
  11827. ((equal ndays '(4)) 100000)
  11828. (ndays (prefix-numeric-value ndays))
  11829. (t (abs org-deadline-warning-days))))
  11830. (case-fold-search nil)
  11831. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11832. (callback
  11833. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11834. (message "%d deadlines past-due or due within %d days"
  11835. (org-occur regexp nil callback)
  11836. org-warn-days)))
  11837. (defun org-check-before-date (date)
  11838. "Check if there are deadlines or scheduled entries before DATE."
  11839. (interactive (list (org-read-date)))
  11840. (let ((case-fold-search nil)
  11841. (regexp (concat "\\<\\(" org-deadline-string
  11842. "\\|" org-scheduled-string
  11843. "\\) *<\\([^>]+\\)>"))
  11844. (callback
  11845. (lambda () (time-less-p
  11846. (org-time-string-to-time (match-string 2))
  11847. (org-time-string-to-time date)))))
  11848. (message "%d entries before %s"
  11849. (org-occur regexp nil callback) date)))
  11850. (defun org-check-after-date (date)
  11851. "Check if there are deadlines or scheduled entries after DATE."
  11852. (interactive (list (org-read-date)))
  11853. (let ((case-fold-search nil)
  11854. (regexp (concat "\\<\\(" org-deadline-string
  11855. "\\|" org-scheduled-string
  11856. "\\) *<\\([^>]+\\)>"))
  11857. (callback
  11858. (lambda () (not
  11859. (time-less-p
  11860. (org-time-string-to-time (match-string 2))
  11861. (org-time-string-to-time date))))))
  11862. (message "%d entries after %s"
  11863. (org-occur regexp nil callback) date)))
  11864. (defun org-evaluate-time-range (&optional to-buffer)
  11865. "Evaluate a time range by computing the difference between start and end.
  11866. Normally the result is just printed in the echo area, but with prefix arg
  11867. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11868. If the time range is actually in a table, the result is inserted into the
  11869. next column.
  11870. For time difference computation, a year is assumed to be exactly 365
  11871. days in order to avoid rounding problems."
  11872. (interactive "P")
  11873. (or
  11874. (org-clock-update-time-maybe)
  11875. (save-excursion
  11876. (unless (org-at-date-range-p t)
  11877. (goto-char (point-at-bol))
  11878. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11879. (if (not (org-at-date-range-p t))
  11880. (error "Not at a time-stamp range, and none found in current line")))
  11881. (let* ((ts1 (match-string 1))
  11882. (ts2 (match-string 2))
  11883. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11884. (match-end (match-end 0))
  11885. (time1 (org-time-string-to-time ts1))
  11886. (time2 (org-time-string-to-time ts2))
  11887. (t1 (time-to-seconds time1))
  11888. (t2 (time-to-seconds time2))
  11889. (diff (abs (- t2 t1)))
  11890. (negative (< (- t2 t1) 0))
  11891. ;; (ys (floor (* 365 24 60 60)))
  11892. (ds (* 24 60 60))
  11893. (hs (* 60 60))
  11894. (fy "%dy %dd %02d:%02d")
  11895. (fy1 "%dy %dd")
  11896. (fd "%dd %02d:%02d")
  11897. (fd1 "%dd")
  11898. (fh "%02d:%02d")
  11899. y d h m align)
  11900. (if havetime
  11901. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11902. y 0
  11903. d (floor (/ diff ds)) diff (mod diff ds)
  11904. h (floor (/ diff hs)) diff (mod diff hs)
  11905. m (floor (/ diff 60)))
  11906. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11907. y 0
  11908. d (floor (+ (/ diff ds) 0.5))
  11909. h 0 m 0))
  11910. (if (not to-buffer)
  11911. (message "%s" (org-make-tdiff-string y d h m))
  11912. (if (org-at-table-p)
  11913. (progn
  11914. (goto-char match-end)
  11915. (setq align t)
  11916. (and (looking-at " *|") (goto-char (match-end 0))))
  11917. (goto-char match-end))
  11918. (if (looking-at
  11919. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11920. (replace-match ""))
  11921. (if negative (insert " -"))
  11922. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11923. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11924. (insert " " (format fh h m))))
  11925. (if align (org-table-align))
  11926. (message "Time difference inserted")))))
  11927. (defun org-make-tdiff-string (y d h m)
  11928. (let ((fmt "")
  11929. (l nil))
  11930. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11931. l (push y l)))
  11932. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11933. l (push d l)))
  11934. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11935. l (push h l)))
  11936. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11937. l (push m l)))
  11938. (apply 'format fmt (nreverse l))))
  11939. (defun org-time-string-to-time (s)
  11940. (apply 'encode-time (org-parse-time-string s)))
  11941. (defun org-time-string-to-seconds (s)
  11942. (time-to-seconds (org-time-string-to-time s)))
  11943. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11944. "Convert a time stamp to an absolute day number.
  11945. If there is a specifyer for a cyclic time stamp, get the closest date to
  11946. DAYNR.
  11947. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11948. (cond
  11949. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11950. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11951. daynr
  11952. (+ daynr 1000)))
  11953. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11954. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11955. (time-to-days (current-time))) (match-string 0 s)
  11956. prefer show-all))
  11957. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11958. (defun org-days-to-iso-week (days)
  11959. "Return the iso week number."
  11960. (require 'cal-iso)
  11961. (car (calendar-iso-from-absolute days)))
  11962. (defun org-small-year-to-year (year)
  11963. "Convert 2-digit years into 4-digit years.
  11964. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11965. The year 2000 cannot be abbreviated. Any year larger than 99
  11966. is returned unchanged."
  11967. (if (< year 38)
  11968. (setq year (+ 2000 year))
  11969. (if (< year 100)
  11970. (setq year (+ 1900 year))))
  11971. year)
  11972. (defun org-time-from-absolute (d)
  11973. "Return the time corresponding to date D.
  11974. D may be an absolute day number, or a calendar-type list (month day year)."
  11975. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11976. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11977. (defun org-calendar-holiday ()
  11978. "List of holidays, for Diary display in Org-mode."
  11979. (require 'holidays)
  11980. (let ((hl (funcall
  11981. (if (fboundp 'calendar-check-holidays)
  11982. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11983. (if hl (mapconcat 'identity hl "; "))))
  11984. (defun org-diary-sexp-entry (sexp entry date)
  11985. "Process a SEXP diary ENTRY for DATE."
  11986. (require 'diary-lib)
  11987. (let ((result (if calendar-debug-sexp
  11988. (let ((stack-trace-on-error t))
  11989. (eval (car (read-from-string sexp))))
  11990. (condition-case nil
  11991. (eval (car (read-from-string sexp)))
  11992. (error
  11993. (beep)
  11994. (message "Bad sexp at line %d in %s: %s"
  11995. (org-current-line)
  11996. (buffer-file-name) sexp)
  11997. (sleep-for 2))))))
  11998. (cond ((stringp result) result)
  11999. ((and (consp result)
  12000. (stringp (cdr result))) (cdr result))
  12001. (result entry)
  12002. (t nil))))
  12003. (defun org-diary-to-ical-string (frombuf)
  12004. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12005. This uses the icalendar.el library."
  12006. (let* ((tmpdir (if (featurep 'xemacs)
  12007. (temp-directory)
  12008. temporary-file-directory))
  12009. (tmpfile (make-temp-name
  12010. (expand-file-name "orgics" tmpdir)))
  12011. buf rtn b e)
  12012. (save-excursion
  12013. (set-buffer frombuf)
  12014. (icalendar-export-region (point-min) (point-max) tmpfile)
  12015. (setq buf (find-buffer-visiting tmpfile))
  12016. (set-buffer buf)
  12017. (goto-char (point-min))
  12018. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12019. (setq b (match-beginning 0)))
  12020. (goto-char (point-max))
  12021. (if (re-search-backward "^END:VEVENT" nil t)
  12022. (setq e (match-end 0)))
  12023. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12024. (kill-buffer buf)
  12025. (delete-file tmpfile)
  12026. rtn))
  12027. (defun org-closest-date (start current change prefer show-all)
  12028. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12029. When PREFER is `past' return a date that is either CURRENT or past.
  12030. When PREFER is `future', return a date that is either CURRENT or future.
  12031. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12032. ;; Make the proper lists from the dates
  12033. (catch 'exit
  12034. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12035. dn dw sday cday n1 n2 n0
  12036. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12037. (setq start (org-date-to-gregorian start)
  12038. current (org-date-to-gregorian
  12039. (if show-all
  12040. current
  12041. (time-to-days (current-time))))
  12042. sday (calendar-absolute-from-gregorian start)
  12043. cday (calendar-absolute-from-gregorian current))
  12044. (if (<= cday sday) (throw 'exit sday))
  12045. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12046. (setq dn (string-to-number (match-string 1 change))
  12047. dw (cdr (assoc (match-string 2 change) a1)))
  12048. (error "Invalid change specifyer: %s" change))
  12049. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12050. (cond
  12051. ((eq dw 'day)
  12052. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12053. n2 (+ n1 dn)))
  12054. ((eq dw 'year)
  12055. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12056. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12057. (setq date1 (list m d y1)
  12058. n1 (calendar-absolute-from-gregorian date1)
  12059. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12060. n2 (calendar-absolute-from-gregorian date2)))
  12061. ((eq dw 'month)
  12062. ;; approx number of month between the two dates
  12063. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12064. ;; How often does dn fit in there?
  12065. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12066. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12067. m (+ m nm)
  12068. ny (floor (/ m 12))
  12069. y (+ y ny)
  12070. m (- m (* ny 12)))
  12071. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12072. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12073. (setq m2 (+ m dn) y2 y)
  12074. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12075. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12076. (while (<= n2 cday)
  12077. (setq n1 n2 m m2 y y2)
  12078. (setq m2 (+ m dn) y2 y)
  12079. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12080. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12081. ;; Make sure n1 is the earlier date
  12082. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12083. (if show-all
  12084. (cond
  12085. ((eq prefer 'past) n1)
  12086. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12087. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12088. (cond
  12089. ((eq prefer 'past) n1)
  12090. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12091. (t (if (= cday n1) n1 n2)))))))
  12092. (defun org-date-to-gregorian (date)
  12093. "Turn any specification of DATE into a gregorian date for the calendar."
  12094. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12095. ((and (listp date) (= (length date) 3)) date)
  12096. ((stringp date)
  12097. (setq date (org-parse-time-string date))
  12098. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12099. ((listp date)
  12100. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12101. (defun org-parse-time-string (s &optional nodefault)
  12102. "Parse the standard Org-mode time string.
  12103. This should be a lot faster than the normal `parse-time-string'.
  12104. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12105. hour and minute fields will be nil if not given."
  12106. (if (string-match org-ts-regexp0 s)
  12107. (list 0
  12108. (if (or (match-beginning 8) (not nodefault))
  12109. (string-to-number (or (match-string 8 s) "0")))
  12110. (if (or (match-beginning 7) (not nodefault))
  12111. (string-to-number (or (match-string 7 s) "0")))
  12112. (string-to-number (match-string 4 s))
  12113. (string-to-number (match-string 3 s))
  12114. (string-to-number (match-string 2 s))
  12115. nil nil nil)
  12116. (make-list 9 0)))
  12117. (defun org-timestamp-up (&optional arg)
  12118. "Increase the date item at the cursor by one.
  12119. If the cursor is on the year, change the year. If it is on the month or
  12120. the day, change that.
  12121. With prefix ARG, change by that many units."
  12122. (interactive "p")
  12123. (org-timestamp-change (prefix-numeric-value arg)))
  12124. (defun org-timestamp-down (&optional arg)
  12125. "Decrease the date item at the cursor by one.
  12126. If the cursor is on the year, change the year. If it is on the month or
  12127. the day, change that.
  12128. With prefix ARG, change by that many units."
  12129. (interactive "p")
  12130. (org-timestamp-change (- (prefix-numeric-value arg))))
  12131. (defun org-timestamp-up-day (&optional arg)
  12132. "Increase the date in the time stamp by one day.
  12133. With prefix ARG, change that many days."
  12134. (interactive "p")
  12135. (if (and (not (org-at-timestamp-p t))
  12136. (org-on-heading-p))
  12137. (org-todo 'up)
  12138. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12139. (defun org-timestamp-down-day (&optional arg)
  12140. "Decrease the date in the time stamp by one day.
  12141. With prefix ARG, change that many days."
  12142. (interactive "p")
  12143. (if (and (not (org-at-timestamp-p t))
  12144. (org-on-heading-p))
  12145. (org-todo 'down)
  12146. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12147. (defun org-at-timestamp-p (&optional inactive-ok)
  12148. "Determine if the cursor is in or at a timestamp."
  12149. (interactive)
  12150. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12151. (pos (point))
  12152. (ans (or (looking-at tsr)
  12153. (save-excursion
  12154. (skip-chars-backward "^[<\n\r\t")
  12155. (if (> (point) (point-min)) (backward-char 1))
  12156. (and (looking-at tsr)
  12157. (> (- (match-end 0) pos) -1))))))
  12158. (and ans
  12159. (boundp 'org-ts-what)
  12160. (setq org-ts-what
  12161. (cond
  12162. ((= pos (match-beginning 0)) 'bracket)
  12163. ((= pos (1- (match-end 0))) 'bracket)
  12164. ((org-pos-in-match-range pos 2) 'year)
  12165. ((org-pos-in-match-range pos 3) 'month)
  12166. ((org-pos-in-match-range pos 7) 'hour)
  12167. ((org-pos-in-match-range pos 8) 'minute)
  12168. ((or (org-pos-in-match-range pos 4)
  12169. (org-pos-in-match-range pos 5)) 'day)
  12170. ((and (> pos (or (match-end 8) (match-end 5)))
  12171. (< pos (match-end 0)))
  12172. (- pos (or (match-end 8) (match-end 5))))
  12173. (t 'day))))
  12174. ans))
  12175. (defun org-toggle-timestamp-type ()
  12176. "Toggle the type (<active> or [inactive]) of a time stamp."
  12177. (interactive)
  12178. (when (org-at-timestamp-p t)
  12179. (let ((beg (match-beginning 0)) (end (match-end 0))
  12180. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12181. (save-excursion
  12182. (goto-char beg)
  12183. (while (re-search-forward "[][<>]" end t)
  12184. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12185. t t)))
  12186. (message "Timestamp is now %sactive"
  12187. (if (equal (char-after beg) ?<) "" "in")))))
  12188. (defun org-timestamp-change (n &optional what)
  12189. "Change the date in the time stamp at point.
  12190. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12191. `year', `minute', `second'. If WHAT is not given, the cursor position
  12192. in the timestamp determines what will be changed."
  12193. (let ((pos (point))
  12194. with-hm inactive
  12195. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12196. org-ts-what
  12197. extra rem
  12198. ts time time0)
  12199. (if (not (org-at-timestamp-p t))
  12200. (error "Not at a timestamp"))
  12201. (if (and (not what) (eq org-ts-what 'bracket))
  12202. (org-toggle-timestamp-type)
  12203. (if (and (not what) (not (eq org-ts-what 'day))
  12204. org-display-custom-times
  12205. (get-text-property (point) 'display)
  12206. (not (get-text-property (1- (point)) 'display)))
  12207. (setq org-ts-what 'day))
  12208. (setq org-ts-what (or what org-ts-what)
  12209. inactive (= (char-after (match-beginning 0)) ?\[)
  12210. ts (match-string 0))
  12211. (replace-match "")
  12212. (if (string-match
  12213. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12214. ts)
  12215. (setq extra (match-string 1 ts)))
  12216. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12217. (setq with-hm t))
  12218. (setq time0 (org-parse-time-string ts))
  12219. (when (and (eq org-ts-what 'minute)
  12220. (eq current-prefix-arg nil))
  12221. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12222. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12223. (setcar (cdr time0) (+ (nth 1 time0)
  12224. (if (> n 0) (- rem) (- dm rem))))))
  12225. (setq time
  12226. (encode-time (or (car time0) 0)
  12227. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12228. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12229. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12230. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12231. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12232. (nthcdr 6 time0)))
  12233. (when (and (member org-ts-what '(hour minute))
  12234. extra
  12235. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12236. (setq extra (org-modify-ts-extra
  12237. extra
  12238. (if (eq org-ts-what 'hour) 2 5)
  12239. n dm)))
  12240. (when (integerp org-ts-what)
  12241. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12242. (if (eq what 'calendar)
  12243. (let ((cal-date (org-get-date-from-calendar)))
  12244. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12245. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12246. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12247. (setcar time0 (or (car time0) 0))
  12248. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12249. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12250. (setq time (apply 'encode-time time0))))
  12251. (setq org-last-changed-timestamp
  12252. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12253. (org-clock-update-time-maybe)
  12254. (goto-char pos)
  12255. ;; Try to recenter the calendar window, if any
  12256. (if (and org-calendar-follow-timestamp-change
  12257. (get-buffer-window "*Calendar*" t)
  12258. (memq org-ts-what '(day month year)))
  12259. (org-recenter-calendar (time-to-days time))))))
  12260. (defun org-modify-ts-extra (s pos n dm)
  12261. "Change the different parts of the lead-time and repeat fields in timestamp."
  12262. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12263. ng h m new rem)
  12264. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12265. (cond
  12266. ((or (org-pos-in-match-range pos 2)
  12267. (org-pos-in-match-range pos 3))
  12268. (setq m (string-to-number (match-string 3 s))
  12269. h (string-to-number (match-string 2 s)))
  12270. (if (org-pos-in-match-range pos 2)
  12271. (setq h (+ h n))
  12272. (setq n (* dm (org-no-warnings (signum n))))
  12273. (when (not (= 0 (setq rem (% m dm))))
  12274. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12275. (setq m (+ m n)))
  12276. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12277. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12278. (setq h (min 24 (max 0 h)))
  12279. (setq ng 1 new (format "-%02d:%02d" h m)))
  12280. ((org-pos-in-match-range pos 6)
  12281. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12282. ((org-pos-in-match-range pos 5)
  12283. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12284. ((org-pos-in-match-range pos 9)
  12285. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12286. ((org-pos-in-match-range pos 8)
  12287. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12288. (when ng
  12289. (setq s (concat
  12290. (substring s 0 (match-beginning ng))
  12291. new
  12292. (substring s (match-end ng))))))
  12293. s))
  12294. (defun org-recenter-calendar (date)
  12295. "If the calendar is visible, recenter it to DATE."
  12296. (let* ((win (selected-window))
  12297. (cwin (get-buffer-window "*Calendar*" t))
  12298. (calendar-move-hook nil))
  12299. (when cwin
  12300. (select-window cwin)
  12301. (calendar-goto-date (if (listp date) date
  12302. (calendar-gregorian-from-absolute date)))
  12303. (select-window win))))
  12304. (defun org-goto-calendar (&optional arg)
  12305. "Go to the Emacs calendar at the current date.
  12306. If there is a time stamp in the current line, go to that date.
  12307. A prefix ARG can be used to force the current date."
  12308. (interactive "P")
  12309. (let ((tsr org-ts-regexp) diff
  12310. (calendar-move-hook nil)
  12311. (calendar-view-holidays-initially-flag nil)
  12312. (view-calendar-holidays-initially nil)
  12313. (calendar-view-diary-initially-flag nil)
  12314. (view-diary-entries-initially nil))
  12315. (if (or (org-at-timestamp-p)
  12316. (save-excursion
  12317. (beginning-of-line 1)
  12318. (looking-at (concat ".*" tsr))))
  12319. (let ((d1 (time-to-days (current-time)))
  12320. (d2 (time-to-days
  12321. (org-time-string-to-time (match-string 1)))))
  12322. (setq diff (- d2 d1))))
  12323. (calendar)
  12324. (calendar-goto-today)
  12325. (if (and diff (not arg)) (calendar-forward-day diff))))
  12326. (defun org-get-date-from-calendar ()
  12327. "Return a list (month day year) of date at point in calendar."
  12328. (with-current-buffer "*Calendar*"
  12329. (save-match-data
  12330. (calendar-cursor-to-date))))
  12331. (defun org-date-from-calendar ()
  12332. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12333. If there is already a time stamp at the cursor position, update it."
  12334. (interactive)
  12335. (if (org-at-timestamp-p t)
  12336. (org-timestamp-change 0 'calendar)
  12337. (let ((cal-date (org-get-date-from-calendar)))
  12338. (org-insert-time-stamp
  12339. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12340. (defun org-minutes-to-hh:mm-string (m)
  12341. "Compute H:MM from a number of minutes."
  12342. (let ((h (/ m 60)))
  12343. (setq m (- m (* 60 h)))
  12344. (format org-time-clocksum-format h m)))
  12345. (defun org-hh:mm-string-to-minutes (s)
  12346. "Convert a string H:MM to a number of minutes."
  12347. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12348. (+ (* (string-to-number (match-string 1 s)) 60)
  12349. (string-to-number (match-string 2 s)))
  12350. 0))
  12351. ;;;; Files
  12352. (defun org-save-all-org-buffers ()
  12353. "Save all Org-mode buffers without user confirmation."
  12354. (interactive)
  12355. (message "Saving all Org-mode buffers...")
  12356. (save-some-buffers t 'org-mode-p)
  12357. (when (featurep 'org-id) (org-id-locations-save))
  12358. (message "Saving all Org-mode buffers... done"))
  12359. (defun org-revert-all-org-buffers ()
  12360. "Revert all Org-mode buffers.
  12361. Prompt for confirmation when there are unsaved changes.
  12362. Be sure you know what you are doing before letting this function
  12363. overwrite your changes.
  12364. This function is useful in a setup where one tracks org files
  12365. with a version control system, to revert on one machine after pulling
  12366. changes from another. I believe the procedure must be like this:
  12367. 1. M-x org-save-all-org-buffers
  12368. 2. Pull changes from the other machine, resolve conflicts
  12369. 3. M-x org-revert-all-org-buffers"
  12370. (interactive)
  12371. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12372. (error "Abort"))
  12373. (save-excursion
  12374. (save-window-excursion
  12375. (mapc
  12376. (lambda (b)
  12377. (when (and (with-current-buffer b (org-mode-p))
  12378. (with-current-buffer b buffer-file-name))
  12379. (switch-to-buffer b)
  12380. (revert-buffer t 'no-confirm)))
  12381. (buffer-list))
  12382. (when (and (featurep 'org-id) org-id-track-globally)
  12383. (org-id-locations-load)))))
  12384. ;;;; Agenda files
  12385. ;;;###autoload
  12386. (defun org-iswitchb (&optional arg)
  12387. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12388. With a prefix argument, restrict available to files.
  12389. With two prefix arguments, restrict available buffers to agenda files.
  12390. Due to some yet unresolved reason, the global function
  12391. `iswitchb-mode' needs to be active for this function to work."
  12392. (interactive "P")
  12393. (require 'iswitchb)
  12394. (let ((enabled iswitchb-mode) blist)
  12395. (or enabled (iswitchb-mode 1))
  12396. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12397. ((equal arg '(16)) (org-buffer-list 'agenda))
  12398. (t (org-buffer-list))))
  12399. (unwind-protect
  12400. (let ((iswitchb-make-buflist-hook
  12401. (lambda ()
  12402. (setq iswitchb-temp-buflist
  12403. (mapcar 'buffer-name blist)))))
  12404. (switch-to-buffer
  12405. (iswitchb-read-buffer
  12406. "Switch-to: " nil t))
  12407. (or enabled (iswitchb-mode -1))))))
  12408. ;;;###autoload
  12409. (defun org-ido-switchb (&optional arg)
  12410. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12411. With a prefix argument, restrict available to files.
  12412. With two prefix arguments, restrict available buffers to agenda files."
  12413. (interactive "P")
  12414. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12415. ((equal arg '(16)) (org-buffer-list 'agenda))
  12416. (t (org-buffer-list)))))
  12417. (switch-to-buffer
  12418. (org-ido-completing-read "Org buffer: "
  12419. (mapcar 'list (mapcar 'buffer-name blist))
  12420. nil t))))
  12421. (defun org-buffer-list (&optional predicate exclude-tmp)
  12422. "Return a list of Org buffers.
  12423. PREDICATE can be `export', `files' or `agenda'.
  12424. export restrict the list to Export buffers.
  12425. files restrict the list to buffers visiting Org files.
  12426. agenda restrict the list to buffers visiting agenda files.
  12427. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12428. (let* ((bfn nil)
  12429. (agenda-files (and (eq predicate 'agenda)
  12430. (mapcar 'file-truename (org-agenda-files t))))
  12431. (filter
  12432. (cond
  12433. ((eq predicate 'files)
  12434. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12435. ((eq predicate 'export)
  12436. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12437. ((eq predicate 'agenda)
  12438. (lambda (b)
  12439. (with-current-buffer b
  12440. (and (eq major-mode 'org-mode)
  12441. (setq bfn (buffer-file-name b))
  12442. (member (file-truename bfn) agenda-files)))))
  12443. (t (lambda (b) (with-current-buffer b
  12444. (or (eq major-mode 'org-mode)
  12445. (string-match "\*Org .*Export"
  12446. (buffer-name b)))))))))
  12447. (delq nil
  12448. (mapcar
  12449. (lambda(b)
  12450. (if (and (funcall filter b)
  12451. (or (not exclude-tmp)
  12452. (not (string-match "tmp" (buffer-name b)))))
  12453. b
  12454. nil))
  12455. (buffer-list)))))
  12456. (defun org-agenda-files (&optional unrestricted archives)
  12457. "Get the list of agenda files.
  12458. Optional UNRESTRICTED means return the full list even if a restriction
  12459. is currently in place.
  12460. When ARCHIVES is t, include all archive files hat are really being
  12461. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12462. `org-agenda-archives-mode' is t."
  12463. (let ((files
  12464. (cond
  12465. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12466. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12467. ((listp org-agenda-files) org-agenda-files)
  12468. (t (error "Invalid value of `org-agenda-files'")))))
  12469. (setq files (apply 'append
  12470. (mapcar (lambda (f)
  12471. (if (file-directory-p f)
  12472. (directory-files
  12473. f t org-agenda-file-regexp)
  12474. (list f)))
  12475. files)))
  12476. (when org-agenda-skip-unavailable-files
  12477. (setq files (delq nil
  12478. (mapcar (function
  12479. (lambda (file)
  12480. (and (file-readable-p file) file)))
  12481. files))))
  12482. (when (or (eq archives t)
  12483. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12484. (setq files (org-add-archive-files files)))
  12485. files))
  12486. (defun org-edit-agenda-file-list ()
  12487. "Edit the list of agenda files.
  12488. Depending on setup, this either uses customize to edit the variable
  12489. `org-agenda-files', or it visits the file that is holding the list. In the
  12490. latter case, the buffer is set up in a way that saving it automatically kills
  12491. the buffer and restores the previous window configuration."
  12492. (interactive)
  12493. (if (stringp org-agenda-files)
  12494. (let ((cw (current-window-configuration)))
  12495. (find-file org-agenda-files)
  12496. (org-set-local 'org-window-configuration cw)
  12497. (org-add-hook 'after-save-hook
  12498. (lambda ()
  12499. (set-window-configuration
  12500. (prog1 org-window-configuration
  12501. (kill-buffer (current-buffer))))
  12502. (org-install-agenda-files-menu)
  12503. (message "New agenda file list installed"))
  12504. nil 'local)
  12505. (message "%s" (substitute-command-keys
  12506. "Edit list and finish with \\[save-buffer]")))
  12507. (customize-variable 'org-agenda-files)))
  12508. (defun org-store-new-agenda-file-list (list)
  12509. "Set new value for the agenda file list and save it correctly."
  12510. (if (stringp org-agenda-files)
  12511. (let ((f org-agenda-files) b)
  12512. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12513. (with-temp-file f
  12514. (insert (mapconcat 'identity list "\n") "\n")))
  12515. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12516. (setq org-agenda-files list)
  12517. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12518. (defun org-read-agenda-file-list ()
  12519. "Read the list of agenda files from a file."
  12520. (when (file-directory-p org-agenda-files)
  12521. (error "`org-agenda-files' cannot be a single directory"))
  12522. (when (stringp org-agenda-files)
  12523. (with-temp-buffer
  12524. (insert-file-contents org-agenda-files)
  12525. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12526. ;;;###autoload
  12527. (defun org-cycle-agenda-files ()
  12528. "Cycle through the files in `org-agenda-files'.
  12529. If the current buffer visits an agenda file, find the next one in the list.
  12530. If the current buffer does not, find the first agenda file."
  12531. (interactive)
  12532. (let* ((fs (org-agenda-files t))
  12533. (files (append fs (list (car fs))))
  12534. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12535. file)
  12536. (unless files (error "No agenda files"))
  12537. (catch 'exit
  12538. (while (setq file (pop files))
  12539. (if (equal (file-truename file) tcf)
  12540. (when (car files)
  12541. (find-file (car files))
  12542. (throw 'exit t))))
  12543. (find-file (car fs)))
  12544. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12545. (defun org-agenda-file-to-front (&optional to-end)
  12546. "Move/add the current file to the top of the agenda file list.
  12547. If the file is not present in the list, it is added to the front. If it is
  12548. present, it is moved there. With optional argument TO-END, add/move to the
  12549. end of the list."
  12550. (interactive "P")
  12551. (let ((org-agenda-skip-unavailable-files nil)
  12552. (file-alist (mapcar (lambda (x)
  12553. (cons (file-truename x) x))
  12554. (org-agenda-files t)))
  12555. (ctf (file-truename buffer-file-name))
  12556. x had)
  12557. (setq x (assoc ctf file-alist) had x)
  12558. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12559. (if to-end
  12560. (setq file-alist (append (delq x file-alist) (list x)))
  12561. (setq file-alist (cons x (delq x file-alist))))
  12562. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12563. (org-install-agenda-files-menu)
  12564. (message "File %s to %s of agenda file list"
  12565. (if had "moved" "added") (if to-end "end" "front"))))
  12566. (defun org-remove-file (&optional file)
  12567. "Remove current file from the list of files in variable `org-agenda-files'.
  12568. These are the files which are being checked for agenda entries.
  12569. Optional argument FILE means, use this file instead of the current."
  12570. (interactive)
  12571. (let* ((org-agenda-skip-unavailable-files nil)
  12572. (file (or file buffer-file-name))
  12573. (true-file (file-truename file))
  12574. (afile (abbreviate-file-name file))
  12575. (files (delq nil (mapcar
  12576. (lambda (x)
  12577. (if (equal true-file
  12578. (file-truename x))
  12579. nil x))
  12580. (org-agenda-files t)))))
  12581. (if (not (= (length files) (length (org-agenda-files t))))
  12582. (progn
  12583. (org-store-new-agenda-file-list files)
  12584. (org-install-agenda-files-menu)
  12585. (message "Removed file: %s" afile))
  12586. (message "File was not in list: %s (not removed)" afile))))
  12587. (defun org-file-menu-entry (file)
  12588. (vector file (list 'find-file file) t))
  12589. (defun org-check-agenda-file (file)
  12590. "Make sure FILE exists. If not, ask user what to do."
  12591. (when (not (file-exists-p file))
  12592. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12593. (abbreviate-file-name file))
  12594. (let ((r (downcase (read-char-exclusive))))
  12595. (cond
  12596. ((equal r ?r)
  12597. (org-remove-file file)
  12598. (throw 'nextfile t))
  12599. (t (error "Abort"))))))
  12600. (defun org-get-agenda-file-buffer (file)
  12601. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12602. it to the list of buffers which might be released later."
  12603. (let ((buf (org-find-base-buffer-visiting file)))
  12604. (if buf
  12605. buf ; just return it
  12606. ;; Make a new buffer and remember it
  12607. (setq buf (find-file-noselect file))
  12608. (if buf (push buf org-agenda-new-buffers))
  12609. buf)))
  12610. (defun org-release-buffers (blist)
  12611. "Release all buffers in list, asking the user for confirmation when needed.
  12612. When a buffer is unmodified, it is just killed. When modified, it is saved
  12613. \(if the user agrees) and then killed."
  12614. (let (buf file)
  12615. (while (setq buf (pop blist))
  12616. (setq file (buffer-file-name buf))
  12617. (when (and (buffer-modified-p buf)
  12618. file
  12619. (y-or-n-p (format "Save file %s? " file)))
  12620. (with-current-buffer buf (save-buffer)))
  12621. (kill-buffer buf))))
  12622. (defun org-prepare-agenda-buffers (files)
  12623. "Create buffers for all agenda files, protect archived trees and comments."
  12624. (interactive)
  12625. (let ((pa '(:org-archived t))
  12626. (pc '(:org-comment t))
  12627. (pall '(:org-archived t :org-comment t))
  12628. (inhibit-read-only t)
  12629. (rea (concat ":" org-archive-tag ":"))
  12630. bmp file re)
  12631. (save-excursion
  12632. (save-restriction
  12633. (while (setq file (pop files))
  12634. (catch 'nextfile
  12635. (if (bufferp file)
  12636. (set-buffer file)
  12637. (org-check-agenda-file file)
  12638. (set-buffer (org-get-agenda-file-buffer file)))
  12639. (widen)
  12640. (setq bmp (buffer-modified-p))
  12641. (org-refresh-category-properties)
  12642. (setq org-todo-keywords-for-agenda
  12643. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12644. (setq org-done-keywords-for-agenda
  12645. (append org-done-keywords-for-agenda org-done-keywords))
  12646. (setq org-todo-keyword-alist-for-agenda
  12647. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12648. (setq org-tag-alist-for-agenda
  12649. (append org-tag-alist-for-agenda org-tag-alist))
  12650. (save-excursion
  12651. (remove-text-properties (point-min) (point-max) pall)
  12652. (when org-agenda-skip-archived-trees
  12653. (goto-char (point-min))
  12654. (while (re-search-forward rea nil t)
  12655. (if (org-on-heading-p t)
  12656. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12657. (goto-char (point-min))
  12658. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12659. (while (re-search-forward re nil t)
  12660. (add-text-properties
  12661. (match-beginning 0) (org-end-of-subtree t) pc)))
  12662. (set-buffer-modified-p bmp)))))
  12663. (setq org-todo-keyword-alist-for-agenda
  12664. (org-uniquify org-todo-keyword-alist-for-agenda)
  12665. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12666. ;;;; Embedded LaTeX
  12667. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12668. "Keymap for the minor `org-cdlatex-mode'.")
  12669. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12670. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12671. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12672. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12673. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12674. (defvar org-cdlatex-texmathp-advice-is-done nil
  12675. "Flag remembering if we have applied the advice to texmathp already.")
  12676. (define-minor-mode org-cdlatex-mode
  12677. "Toggle the minor `org-cdlatex-mode'.
  12678. This mode supports entering LaTeX environment and math in LaTeX fragments
  12679. in Org-mode.
  12680. \\{org-cdlatex-mode-map}"
  12681. nil " OCDL" nil
  12682. (when org-cdlatex-mode (require 'cdlatex))
  12683. (unless org-cdlatex-texmathp-advice-is-done
  12684. (setq org-cdlatex-texmathp-advice-is-done t)
  12685. (defadvice texmathp (around org-math-always-on activate)
  12686. "Always return t in org-mode buffers.
  12687. This is because we want to insert math symbols without dollars even outside
  12688. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12689. an embedded LaTeX fragment, let texmathp do its job.
  12690. \\[org-cdlatex-mode-map]"
  12691. (interactive)
  12692. (let (p)
  12693. (cond
  12694. ((not (org-mode-p)) ad-do-it)
  12695. ((eq this-command 'cdlatex-math-symbol)
  12696. (setq ad-return-value t
  12697. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12698. (t
  12699. (let ((p (org-inside-LaTeX-fragment-p)))
  12700. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12701. (setq ad-return-value t
  12702. texmathp-why '("Org-mode embedded math" . 0))
  12703. (if p ad-do-it)))))))))
  12704. (defun turn-on-org-cdlatex ()
  12705. "Unconditionally turn on `org-cdlatex-mode'."
  12706. (org-cdlatex-mode 1))
  12707. (defun org-inside-LaTeX-fragment-p ()
  12708. "Test if point is inside a LaTeX fragment.
  12709. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12710. sequence appearing also before point.
  12711. Even though the matchers for math are configurable, this function assumes
  12712. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12713. delimiters are skipped when they have been removed by customization.
  12714. The return value is nil, or a cons cell with the delimiter and
  12715. and the position of this delimiter.
  12716. This function does a reasonably good job, but can locally be fooled by
  12717. for example currency specifications. For example it will assume being in
  12718. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12719. fragments that are properly closed, but during editing, we have to live
  12720. with the uncertainty caused by missing closing delimiters. This function
  12721. looks only before point, not after."
  12722. (catch 'exit
  12723. (let ((pos (point))
  12724. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12725. (lim (progn
  12726. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12727. (point)))
  12728. dd-on str (start 0) m re)
  12729. (goto-char pos)
  12730. (when dodollar
  12731. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12732. re (nth 1 (assoc "$" org-latex-regexps)))
  12733. (while (string-match re str start)
  12734. (cond
  12735. ((= (match-end 0) (length str))
  12736. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12737. ((= (match-end 0) (- (length str) 5))
  12738. (throw 'exit nil))
  12739. (t (setq start (match-end 0))))))
  12740. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12741. (goto-char pos)
  12742. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12743. (and (match-beginning 2) (throw 'exit nil))
  12744. ;; count $$
  12745. (while (re-search-backward "\\$\\$" lim t)
  12746. (setq dd-on (not dd-on)))
  12747. (goto-char pos)
  12748. (if dd-on (cons "$$" m))))))
  12749. (defun org-try-cdlatex-tab ()
  12750. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12751. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12752. - inside a LaTeX fragment, or
  12753. - after the first word in a line, where an abbreviation expansion could
  12754. insert a LaTeX environment."
  12755. (when org-cdlatex-mode
  12756. (cond
  12757. ((save-excursion
  12758. (skip-chars-backward "a-zA-Z0-9*")
  12759. (skip-chars-backward " \t")
  12760. (bolp))
  12761. (cdlatex-tab) t)
  12762. ((org-inside-LaTeX-fragment-p)
  12763. (cdlatex-tab) t)
  12764. (t nil))))
  12765. (defun org-cdlatex-underscore-caret (&optional arg)
  12766. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12767. Revert to the normal definition outside of these fragments."
  12768. (interactive "P")
  12769. (if (org-inside-LaTeX-fragment-p)
  12770. (call-interactively 'cdlatex-sub-superscript)
  12771. (let (org-cdlatex-mode)
  12772. (call-interactively (key-binding (vector last-input-event))))))
  12773. (defun org-cdlatex-math-modify (&optional arg)
  12774. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12775. Revert to the normal definition outside of these fragments."
  12776. (interactive "P")
  12777. (if (org-inside-LaTeX-fragment-p)
  12778. (call-interactively 'cdlatex-math-modify)
  12779. (let (org-cdlatex-mode)
  12780. (call-interactively (key-binding (vector last-input-event))))))
  12781. (defvar org-latex-fragment-image-overlays nil
  12782. "List of overlays carrying the images of latex fragments.")
  12783. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12784. (defun org-remove-latex-fragment-image-overlays ()
  12785. "Remove all overlays with LaTeX fragment images in current buffer."
  12786. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12787. (setq org-latex-fragment-image-overlays nil))
  12788. (defun org-preview-latex-fragment (&optional subtree)
  12789. "Preview the LaTeX fragment at point, or all locally or globally.
  12790. If the cursor is in a LaTeX fragment, create the image and overlay
  12791. it over the source code. If there is no fragment at point, display
  12792. all fragments in the current text, from one headline to the next. With
  12793. prefix SUBTREE, display all fragments in the current subtree. With a
  12794. double prefix `C-u C-u', or when the cursor is before the first headline,
  12795. display all fragments in the buffer.
  12796. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12797. (interactive "P")
  12798. (org-remove-latex-fragment-image-overlays)
  12799. (save-excursion
  12800. (save-restriction
  12801. (let (beg end at msg)
  12802. (cond
  12803. ((or (equal subtree '(16))
  12804. (not (save-excursion
  12805. (re-search-backward (concat "^" outline-regexp) nil t))))
  12806. (setq beg (point-min) end (point-max)
  12807. msg "Creating images for buffer...%s"))
  12808. ((equal subtree '(4))
  12809. (org-back-to-heading)
  12810. (setq beg (point) end (org-end-of-subtree t)
  12811. msg "Creating images for subtree...%s"))
  12812. (t
  12813. (if (setq at (org-inside-LaTeX-fragment-p))
  12814. (goto-char (max (point-min) (- (cdr at) 2)))
  12815. (org-back-to-heading))
  12816. (setq beg (point) end (progn (outline-next-heading) (point))
  12817. msg (if at "Creating image...%s"
  12818. "Creating images for entry...%s"))))
  12819. (message msg "")
  12820. (narrow-to-region beg end)
  12821. (goto-char beg)
  12822. (org-format-latex
  12823. (concat "ltxpng/" (file-name-sans-extension
  12824. (file-name-nondirectory
  12825. buffer-file-name)))
  12826. default-directory 'overlays msg at 'forbuffer)
  12827. (message msg "done. Use `C-c C-c' to remove images.")))))
  12828. (defvar org-latex-regexps
  12829. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12830. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12831. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12832. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12833. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12834. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12835. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12836. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12837. "Regular expressions for matching embedded LaTeX.")
  12838. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12839. "Replace LaTeX fragments with links to an image, and produce images."
  12840. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12841. (let* ((prefixnodir (file-name-nondirectory prefix))
  12842. (absprefix (expand-file-name prefix dir))
  12843. (todir (file-name-directory absprefix))
  12844. (opt org-format-latex-options)
  12845. (matchers (plist-get opt :matchers))
  12846. (re-list org-latex-regexps)
  12847. (cnt 0) txt link beg end re e checkdir
  12848. executables-checked
  12849. m n block linkfile movefile ov)
  12850. ;; Check if there are old images files with this prefix, and remove them
  12851. (when (file-directory-p todir)
  12852. (mapc 'delete-file
  12853. (directory-files
  12854. todir 'full
  12855. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12856. ;; Check the different regular expressions
  12857. (while (setq e (pop re-list))
  12858. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12859. block (if (nth 3 e) "\n\n" ""))
  12860. (when (member m matchers)
  12861. (goto-char (point-min))
  12862. (while (re-search-forward re nil t)
  12863. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12864. (not (get-text-property (match-beginning n)
  12865. 'org-protected)))
  12866. (setq txt (match-string n)
  12867. beg (match-beginning n) end (match-end n)
  12868. cnt (1+ cnt)
  12869. linkfile (format "%s_%04d.png" prefix cnt)
  12870. movefile (format "%s_%04d.png" absprefix cnt)
  12871. link (concat block "[[file:" linkfile "]]" block))
  12872. (if msg (message msg cnt))
  12873. (goto-char beg)
  12874. (unless checkdir ; make sure the directory exists
  12875. (setq checkdir t)
  12876. (or (file-directory-p todir) (make-directory todir)))
  12877. (unless executables-checked
  12878. (org-check-external-command
  12879. "latex" "needed to convert LaTeX fragments to images")
  12880. (org-check-external-command
  12881. "dvipng" "needed to convert LaTeX fragments to images")
  12882. (setq executables-checked t))
  12883. (org-create-formula-image
  12884. txt movefile opt forbuffer)
  12885. (if overlays
  12886. (progn
  12887. (setq ov (org-make-overlay beg end))
  12888. (if (featurep 'xemacs)
  12889. (progn
  12890. (org-overlay-put ov 'invisible t)
  12891. (org-overlay-put
  12892. ov 'end-glyph
  12893. (make-glyph (vector 'png :file movefile))))
  12894. (org-overlay-put
  12895. ov 'display
  12896. (list 'image :type 'png :file movefile :ascent 'center)))
  12897. (push ov org-latex-fragment-image-overlays)
  12898. (goto-char end))
  12899. (delete-region beg end)
  12900. (insert link))))))))
  12901. ;; This function borrows from Ganesh Swami's latex2png.el
  12902. (defun org-create-formula-image (string tofile options buffer)
  12903. (let* ((tmpdir (if (featurep 'xemacs)
  12904. (temp-directory)
  12905. temporary-file-directory))
  12906. (texfilebase (make-temp-name
  12907. (expand-file-name "orgtex" tmpdir)))
  12908. (texfile (concat texfilebase ".tex"))
  12909. (dvifile (concat texfilebase ".dvi"))
  12910. (pngfile (concat texfilebase ".png"))
  12911. (fnh (if (featurep 'xemacs)
  12912. (font-height (get-face-font 'default))
  12913. (face-attribute 'default :height nil)))
  12914. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12915. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12916. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12917. "Black"))
  12918. (bg (or (plist-get options (if buffer :background :html-background))
  12919. "Transparent")))
  12920. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12921. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12922. (with-temp-file texfile
  12923. (insert org-format-latex-header
  12924. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12925. (let ((dir default-directory))
  12926. (condition-case nil
  12927. (progn
  12928. (cd tmpdir)
  12929. (call-process "latex" nil nil nil texfile))
  12930. (error nil))
  12931. (cd dir))
  12932. (if (not (file-exists-p dvifile))
  12933. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12934. (condition-case nil
  12935. (call-process "dvipng" nil nil nil
  12936. "-E" "-fg" fg "-bg" bg
  12937. "-D" dpi
  12938. ;;"-x" scale "-y" scale
  12939. "-T" "tight"
  12940. "-o" pngfile
  12941. dvifile)
  12942. (error nil))
  12943. (if (not (file-exists-p pngfile))
  12944. (progn (message "Failed to create png file from %s" texfile) nil)
  12945. ;; Use the requested file name and clean up
  12946. (copy-file pngfile tofile 'replace)
  12947. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12948. (delete-file (concat texfilebase e)))
  12949. pngfile))))
  12950. (defun org-dvipng-color (attr)
  12951. "Return an rgb color specification for dvipng."
  12952. (apply 'format "rgb %s %s %s"
  12953. (mapcar 'org-normalize-color
  12954. (color-values (face-attribute 'default attr nil)))))
  12955. (defun org-normalize-color (value)
  12956. "Return string to be used as color value for an RGB component."
  12957. (format "%g" (/ value 65535.0)))
  12958. ;;;; Key bindings
  12959. ;; Make `C-c C-x' a prefix key
  12960. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12961. ;; TAB key with modifiers
  12962. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12963. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12964. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12965. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12966. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12967. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12968. ;; The following line is necessary under Suse GNU/Linux
  12969. (unless (featurep 'xemacs)
  12970. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12971. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12972. (define-key org-mode-map [backtab] 'org-shifttab)
  12973. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12974. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12975. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12976. ;; Cursor keys with modifiers
  12977. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12978. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12979. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12980. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12981. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12982. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12983. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12984. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12985. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12986. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12987. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12988. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12989. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12990. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12991. ;;; Extra keys for tty access.
  12992. ;; We only set them when really needed because otherwise the
  12993. ;; menus don't show the simple keys
  12994. (when (or org-use-extra-keys
  12995. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12996. (not window-system))
  12997. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12998. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12999. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13000. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13001. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13002. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13003. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13004. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13005. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13006. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13007. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13008. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13009. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13010. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13011. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13012. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13013. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13014. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13015. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13016. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13017. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13018. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13019. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13020. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13021. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13022. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13023. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13024. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13025. ;; All the other keys
  13026. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13027. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13028. (if (boundp 'narrow-map)
  13029. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13030. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13031. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13032. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13033. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  13034. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13035. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13036. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13037. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13038. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13039. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13040. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13041. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13042. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13043. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13044. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13045. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13046. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13047. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13048. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13049. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13050. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13051. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13052. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13053. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13054. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13055. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13056. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13057. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13058. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13059. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13060. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13061. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13062. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13063. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13064. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13065. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13066. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13067. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13068. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13069. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13070. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13071. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13072. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13073. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13074. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13075. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13076. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  13077. (org-defkey org-mode-map "\C-m" 'org-return)
  13078. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13079. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13080. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13081. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13082. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13083. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13084. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13085. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13086. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13087. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13088. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13089. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13090. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13091. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13092. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13093. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13094. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13095. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13096. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13097. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13098. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13099. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13100. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13101. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13102. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13103. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13104. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13105. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13106. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13107. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13108. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13109. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13110. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13111. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13112. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13113. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13114. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13115. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13116. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13117. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13118. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13119. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13120. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13121. (when (featurep 'xemacs)
  13122. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13123. (defvar org-self-insert-command-undo-counter 0)
  13124. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13125. (defun org-self-insert-command (N)
  13126. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13127. If the cursor is in a table looking at whitespace, the whitespace is
  13128. overwritten, and the table is not marked as requiring realignment."
  13129. (interactive "p")
  13130. (if (and
  13131. (org-table-p)
  13132. (progn
  13133. ;; check if we blank the field, and if that triggers align
  13134. (and (featurep 'org-table) org-table-auto-blank-field
  13135. (member last-command
  13136. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  13137. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13138. ;; got extra space, this field does not determine column width
  13139. (let (org-table-may-need-update) (org-table-blank-field))
  13140. ;; no extra space, this field may determine column width
  13141. (org-table-blank-field)))
  13142. t)
  13143. (eq N 1)
  13144. (looking-at "[^|\n]* |"))
  13145. (let (org-table-may-need-update)
  13146. (goto-char (1- (match-end 0)))
  13147. (delete-backward-char 1)
  13148. (goto-char (match-beginning 0))
  13149. (self-insert-command N))
  13150. (setq org-table-may-need-update t)
  13151. (self-insert-command N)
  13152. (org-fix-tags-on-the-fly)
  13153. (if org-self-insert-cluster-for-undo
  13154. (if (not (eq last-command 'org-self-insert-command))
  13155. (setq org-self-insert-command-undo-counter 1)
  13156. (if (>= org-self-insert-command-undo-counter 20)
  13157. (setq org-self-insert-command-undo-counter 1)
  13158. (and (> org-self-insert-command-undo-counter 0)
  13159. buffer-undo-list
  13160. (not (cadr buffer-undo-list)) ; remove nil entry
  13161. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13162. (setq org-self-insert-command-undo-counter
  13163. (1+ org-self-insert-command-undo-counter)))))))
  13164. (defun org-fix-tags-on-the-fly ()
  13165. (when (and (equal (char-after (point-at-bol)) ?*)
  13166. (org-on-heading-p))
  13167. (org-align-tags-here org-tags-column)))
  13168. (defun org-delete-backward-char (N)
  13169. "Like `delete-backward-char', insert whitespace at field end in tables.
  13170. When deleting backwards, in tables this function will insert whitespace in
  13171. front of the next \"|\" separator, to keep the table aligned. The table will
  13172. still be marked for re-alignment if the field did fill the entire column,
  13173. because, in this case the deletion might narrow the column."
  13174. (interactive "p")
  13175. (if (and (org-table-p)
  13176. (eq N 1)
  13177. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13178. (looking-at ".*?|"))
  13179. (let ((pos (point))
  13180. (noalign (looking-at "[^|\n\r]* |"))
  13181. (c org-table-may-need-update))
  13182. (backward-delete-char N)
  13183. (skip-chars-forward "^|")
  13184. (insert " ")
  13185. (goto-char (1- pos))
  13186. ;; noalign: if there were two spaces at the end, this field
  13187. ;; does not determine the width of the column.
  13188. (if noalign (setq org-table-may-need-update c)))
  13189. (backward-delete-char N)
  13190. (org-fix-tags-on-the-fly)))
  13191. (defun org-delete-char (N)
  13192. "Like `delete-char', but insert whitespace at field end in tables.
  13193. When deleting characters, in tables this function will insert whitespace in
  13194. front of the next \"|\" separator, to keep the table aligned. The table will
  13195. still be marked for re-alignment if the field did fill the entire column,
  13196. because, in this case the deletion might narrow the column."
  13197. (interactive "p")
  13198. (if (and (org-table-p)
  13199. (not (bolp))
  13200. (not (= (char-after) ?|))
  13201. (eq N 1))
  13202. (if (looking-at ".*?|")
  13203. (let ((pos (point))
  13204. (noalign (looking-at "[^|\n\r]* |"))
  13205. (c org-table-may-need-update))
  13206. (replace-match (concat
  13207. (substring (match-string 0) 1 -1)
  13208. " |"))
  13209. (goto-char pos)
  13210. ;; noalign: if there were two spaces at the end, this field
  13211. ;; does not determine the width of the column.
  13212. (if noalign (setq org-table-may-need-update c)))
  13213. (delete-char N))
  13214. (delete-char N)
  13215. (org-fix-tags-on-the-fly)))
  13216. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13217. (put 'org-self-insert-command 'delete-selection t)
  13218. (put 'orgtbl-self-insert-command 'delete-selection t)
  13219. (put 'org-delete-char 'delete-selection 'supersede)
  13220. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13221. (put 'org-yank 'delete-selection 'yank)
  13222. ;; Make `flyspell-mode' delay after some commands
  13223. (put 'org-self-insert-command 'flyspell-delayed t)
  13224. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13225. (put 'org-delete-char 'flyspell-delayed t)
  13226. (put 'org-delete-backward-char 'flyspell-delayed t)
  13227. ;; Make pabbrev-mode expand after org-mode commands
  13228. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13229. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13230. ;; How to do this: Measure non-white length of current string
  13231. ;; If equal to column width, we should realign.
  13232. (defun org-remap (map &rest commands)
  13233. "In MAP, remap the functions given in COMMANDS.
  13234. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13235. (let (new old)
  13236. (while commands
  13237. (setq old (pop commands) new (pop commands))
  13238. (if (fboundp 'command-remapping)
  13239. (org-defkey map (vector 'remap old) new)
  13240. (substitute-key-definition old new map global-map)))))
  13241. (when (eq org-enable-table-editor 'optimized)
  13242. ;; If the user wants maximum table support, we need to hijack
  13243. ;; some standard editing functions
  13244. (org-remap org-mode-map
  13245. 'self-insert-command 'org-self-insert-command
  13246. 'delete-char 'org-delete-char
  13247. 'delete-backward-char 'org-delete-backward-char)
  13248. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13249. (defvar org-ctrl-c-ctrl-c-hook nil
  13250. "Hook for functions attaching themselves to `C-c C-c'.
  13251. This can be used to add additional functionality to the C-c C-c key which
  13252. executes context-dependent commands.
  13253. Each function will be called with no arguments. The function must check
  13254. if the context is appropriate for it to act. If yes, it should do its
  13255. thing and then return a non-nil value. If the context is wrong,
  13256. just do nothing and return nil.")
  13257. (defvar org-tab-first-hook nil
  13258. "Hook for functions to attach themselves to TAB.
  13259. See `org-ctrl-c-ctrl-c-hook' for more information.
  13260. This hook runs as the first action when TAB is pressed, even before
  13261. `org-cycle' messes around with the `outline-regexp' to cater for
  13262. inline tasks and plain list item folding.
  13263. If any function in this hook returns t, not other actions like table
  13264. field motion visibility cycling will be done.")
  13265. (defvar org-tab-after-check-for-table-hook nil
  13266. "Hook for functions to attach themselves to TAB.
  13267. See `org-ctrl-c-ctrl-c-hook' for more information.
  13268. This hook runs after it has been established that the cursor is not in a
  13269. table, but before checking if the cursor is in a headline or if global cycling
  13270. should be done.
  13271. If any function in this hook returns t, not other actions like visibility
  13272. cycling will be done.")
  13273. (defvar org-tab-after-check-for-cycling-hook nil
  13274. "Hook for functions to attach themselves to TAB.
  13275. See `org-ctrl-c-ctrl-c-hook' for more information.
  13276. This hook runs after it has been established that not table field motion and
  13277. not visibility should be done because of current context. This is probably
  13278. the place where a package like yasnippets can hook in.")
  13279. (defvar org-metaleft-hook nil
  13280. "Hook for functions attaching themselves to `M-left'.
  13281. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13282. (defvar org-metaright-hook nil
  13283. "Hook for functions attaching themselves to `M-right'.
  13284. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13285. (defvar org-metaup-hook nil
  13286. "Hook for functions attaching themselves to `M-up'.
  13287. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13288. (defvar org-metadown-hook nil
  13289. "Hook for functions attaching themselves to `M-down'.
  13290. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13291. (defvar org-shiftmetaleft-hook nil
  13292. "Hook for functions attaching themselves to `M-S-left'.
  13293. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13294. (defvar org-shiftmetaright-hook nil
  13295. "Hook for functions attaching themselves to `M-S-right'.
  13296. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13297. (defvar org-shiftmetaup-hook nil
  13298. "Hook for functions attaching themselves to `M-S-up'.
  13299. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13300. (defvar org-shiftmetadown-hook nil
  13301. "Hook for functions attaching themselves to `M-S-down'.
  13302. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13303. (defvar org-metareturn-hook nil
  13304. "Hook for functions attaching themselves to `M-RET'.
  13305. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13306. (defun org-modifier-cursor-error ()
  13307. "Throw an error, a modified cursor command was applied in wrong context."
  13308. (error "This command is active in special context like tables, headlines or items"))
  13309. (defun org-shiftselect-error ()
  13310. "Throw an error because Shift-Cursor command was applied in wrong context."
  13311. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13312. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13313. (error "This command works only in special context like headlines or timestamps.")))
  13314. (defun org-call-for-shift-select (cmd)
  13315. (let ((this-command-keys-shift-translated t))
  13316. (call-interactively cmd)))
  13317. (defun org-shifttab (&optional arg)
  13318. "Global visibility cycling or move to previous table field.
  13319. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13320. on context.
  13321. See the individual commands for more information."
  13322. (interactive "P")
  13323. (cond
  13324. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13325. ((integerp arg)
  13326. (message "Content view to level: %d" arg)
  13327. (org-content (prefix-numeric-value arg))
  13328. (setq org-cycle-global-status 'overview))
  13329. (t (call-interactively 'org-global-cycle))))
  13330. (defun org-shiftmetaleft ()
  13331. "Promote subtree or delete table column.
  13332. Calls `org-promote-subtree', `org-outdent-item',
  13333. or `org-table-delete-column', depending on context.
  13334. See the individual commands for more information."
  13335. (interactive)
  13336. (cond
  13337. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13338. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13339. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13340. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13341. (t (org-modifier-cursor-error))))
  13342. (defun org-shiftmetaright ()
  13343. "Demote subtree or insert table column.
  13344. Calls `org-demote-subtree', `org-indent-item',
  13345. or `org-table-insert-column', depending on context.
  13346. See the individual commands for more information."
  13347. (interactive)
  13348. (cond
  13349. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13350. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13351. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13352. ((org-at-item-p) (call-interactively 'org-indent-item))
  13353. (t (org-modifier-cursor-error))))
  13354. (defun org-shiftmetaup (&optional arg)
  13355. "Move subtree up or kill table row.
  13356. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13357. `org-move-item-up' depending on context. See the individual commands
  13358. for more information."
  13359. (interactive "P")
  13360. (cond
  13361. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13362. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13363. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13364. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13365. (t (org-modifier-cursor-error))))
  13366. (defun org-shiftmetadown (&optional arg)
  13367. "Move subtree down or insert table row.
  13368. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13369. `org-move-item-down', depending on context. See the individual
  13370. commands for more information."
  13371. (interactive "P")
  13372. (cond
  13373. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13374. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13375. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13376. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13377. (t (org-modifier-cursor-error))))
  13378. (defun org-metaleft (&optional arg)
  13379. "Promote heading or move table column to left.
  13380. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13381. With no specific context, calls the Emacs default `backward-word'.
  13382. See the individual commands for more information."
  13383. (interactive "P")
  13384. (cond
  13385. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13386. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13387. ((or (org-on-heading-p)
  13388. (and (org-region-active-p)
  13389. (save-excursion
  13390. (goto-char (region-beginning))
  13391. (org-on-heading-p))))
  13392. (call-interactively 'org-do-promote))
  13393. ((or (org-at-item-p)
  13394. (and (org-region-active-p)
  13395. (save-excursion
  13396. (goto-char (region-beginning))
  13397. (org-at-item-p))))
  13398. (call-interactively 'org-outdent-item))
  13399. (t (call-interactively 'backward-word))))
  13400. (defun org-metaright (&optional arg)
  13401. "Demote subtree or move table column to right.
  13402. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13403. With no specific context, calls the Emacs default `forward-word'.
  13404. See the individual commands for more information."
  13405. (interactive "P")
  13406. (cond
  13407. ((run-hook-with-args-until-success 'org-metaright-hook))
  13408. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13409. ((or (org-on-heading-p)
  13410. (and (org-region-active-p)
  13411. (save-excursion
  13412. (goto-char (region-beginning))
  13413. (org-on-heading-p))))
  13414. (call-interactively 'org-do-demote))
  13415. ((or (org-at-item-p)
  13416. (and (org-region-active-p)
  13417. (save-excursion
  13418. (goto-char (region-beginning))
  13419. (org-at-item-p))))
  13420. (call-interactively 'org-indent-item))
  13421. (t (call-interactively 'forward-word))))
  13422. (defun org-metaup (&optional arg)
  13423. "Move subtree up or move table row up.
  13424. Calls `org-move-subtree-up' or `org-table-move-row' or
  13425. `org-move-item-up', depending on context. See the individual commands
  13426. for more information."
  13427. (interactive "P")
  13428. (cond
  13429. ((run-hook-with-args-until-success 'org-metaup-hook))
  13430. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13431. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13432. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13433. (t (transpose-lines 1) (beginning-of-line -1))))
  13434. (defun org-metadown (&optional arg)
  13435. "Move subtree down or move table row down.
  13436. Calls `org-move-subtree-down' or `org-table-move-row' or
  13437. `org-move-item-down', depending on context. See the individual
  13438. commands for more information."
  13439. (interactive "P")
  13440. (cond
  13441. ((run-hook-with-args-until-success 'org-metadown-hook))
  13442. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13443. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13444. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13445. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13446. (defun org-shiftup (&optional arg)
  13447. "Increase item in timestamp or increase priority of current headline.
  13448. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13449. depending on context. See the individual commands for more information."
  13450. (interactive "P")
  13451. (cond
  13452. ((and org-support-shift-select (org-region-active-p))
  13453. (org-call-for-shift-select 'previous-line))
  13454. ((org-at-timestamp-p t)
  13455. (call-interactively (if org-edit-timestamp-down-means-later
  13456. 'org-timestamp-down 'org-timestamp-up)))
  13457. ((and (not (eq org-support-shift-select 'always))
  13458. org-enable-priority-commands
  13459. (org-on-heading-p))
  13460. (call-interactively 'org-priority-up))
  13461. ((and (not org-support-shift-select) (org-at-item-p))
  13462. (call-interactively 'org-previous-item))
  13463. ((org-clocktable-try-shift 'up arg))
  13464. (org-support-shift-select
  13465. (org-call-for-shift-select 'previous-line))
  13466. (t (org-shiftselect-error))))
  13467. (defun org-shiftdown (&optional arg)
  13468. "Decrease item in timestamp or decrease priority of current headline.
  13469. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13470. depending on context. See the individual commands for more information."
  13471. (interactive "P")
  13472. (cond
  13473. ((and org-support-shift-select (org-region-active-p))
  13474. (org-call-for-shift-select 'next-line))
  13475. ((org-at-timestamp-p t)
  13476. (call-interactively (if org-edit-timestamp-down-means-later
  13477. 'org-timestamp-up 'org-timestamp-down)))
  13478. ((and (not (eq org-support-shift-select 'always))
  13479. org-enable-priority-commands
  13480. (org-on-heading-p))
  13481. (call-interactively 'org-priority-down))
  13482. ((and (not org-support-shift-select) (org-at-item-p))
  13483. (call-interactively 'org-next-item))
  13484. ((org-clocktable-try-shift 'down arg))
  13485. (org-support-shift-select
  13486. (org-call-for-shift-select 'next-line))
  13487. (t (org-shiftselect-error))))
  13488. (defun org-shiftright (&optional arg)
  13489. "Cycle the thing at point or in the current line, depending on context.
  13490. Depending on context, this does one of the following:
  13491. - switch a timestamp at point one day into the future
  13492. - on a headline, switch to the next TODO keyword.
  13493. - on an item, switch entire list to the next bullet type
  13494. - on a property line, switch to the next allowed value
  13495. - on a clocktable definition line, move time block into the future"
  13496. (interactive "P")
  13497. (cond
  13498. ((and org-support-shift-select (org-region-active-p))
  13499. (org-call-for-shift-select 'forward-char))
  13500. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13501. ((and (not (eq org-support-shift-select 'always))
  13502. (org-on-heading-p))
  13503. (let ((org-inhibit-logging
  13504. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13505. (org-call-with-arg 'org-todo 'right)))
  13506. ((or (and org-support-shift-select
  13507. (not (eq org-support-shift-select 'always))
  13508. (org-at-item-bullet-p))
  13509. (and (not org-support-shift-select) (org-at-item-p)))
  13510. (org-call-with-arg 'org-cycle-list-bullet nil))
  13511. ((and (not (eq org-support-shift-select 'always))
  13512. (org-at-property-p))
  13513. (call-interactively 'org-property-next-allowed-value))
  13514. ((org-clocktable-try-shift 'right arg))
  13515. (org-support-shift-select
  13516. (org-call-for-shift-select 'forward-char))
  13517. (t (org-shiftselect-error))))
  13518. (defun org-shiftleft (&optional arg)
  13519. "Cycle the thing at point or in the current line, depending on context.
  13520. Depending on context, this does one of the following:
  13521. - switch a timestamp at point one day into the past
  13522. - on a headline, switch to the previous TODO keyword.
  13523. - on an item, switch entire list to the previous bullet type
  13524. - on a property line, switch to the previous allowed value
  13525. - on a clocktable definition line, move time block into the past"
  13526. (interactive "P")
  13527. (cond
  13528. ((and org-support-shift-select (org-region-active-p))
  13529. (org-call-for-shift-select 'backward-char))
  13530. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13531. ((and (not (eq org-support-shift-select 'always))
  13532. (org-on-heading-p))
  13533. (let ((org-inhibit-logging
  13534. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13535. (org-call-with-arg 'org-todo 'left)))
  13536. ((or (and org-support-shift-select
  13537. (not (eq org-support-shift-select 'always))
  13538. (org-at-item-bullet-p))
  13539. (and (not org-support-shift-select) (org-at-item-p)))
  13540. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13541. ((and (not (eq org-support-shift-select 'always))
  13542. (org-at-property-p))
  13543. (call-interactively 'org-property-previous-allowed-value))
  13544. ((org-clocktable-try-shift 'left arg))
  13545. (org-support-shift-select
  13546. (org-call-for-shift-select 'backward-char))
  13547. (t (org-shiftselect-error))))
  13548. (defun org-shiftcontrolright ()
  13549. "Switch to next TODO set."
  13550. (interactive)
  13551. (cond
  13552. ((and org-support-shift-select (org-region-active-p))
  13553. (org-call-for-shift-select 'forward-word))
  13554. ((and (not (eq org-support-shift-select 'always))
  13555. (org-on-heading-p))
  13556. (org-call-with-arg 'org-todo 'nextset))
  13557. (org-support-shift-select
  13558. (org-call-for-shift-select 'forward-word))
  13559. (t (org-shiftselect-error))))
  13560. (defun org-shiftcontrolleft ()
  13561. "Switch to previous TODO set."
  13562. (interactive)
  13563. (cond
  13564. ((and org-support-shift-select (org-region-active-p))
  13565. (org-call-for-shift-select 'backward-word))
  13566. ((and (not (eq org-support-shift-select 'always))
  13567. (org-on-heading-p))
  13568. (org-call-with-arg 'org-todo 'previousset))
  13569. (org-support-shift-select
  13570. (org-call-for-shift-select 'backward-word))
  13571. (t (org-shiftselect-error))))
  13572. (defun org-ctrl-c-ret ()
  13573. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13574. (interactive)
  13575. (cond
  13576. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13577. (t (call-interactively 'org-insert-heading))))
  13578. (defun org-copy-special ()
  13579. "Copy region in table or copy current subtree.
  13580. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13581. See the individual commands for more information."
  13582. (interactive)
  13583. (call-interactively
  13584. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13585. (defun org-cut-special ()
  13586. "Cut region in table or cut current subtree.
  13587. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13588. See the individual commands for more information."
  13589. (interactive)
  13590. (call-interactively
  13591. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13592. (defun org-paste-special (arg)
  13593. "Paste rectangular region into table, or past subtree relative to level.
  13594. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13595. See the individual commands for more information."
  13596. (interactive "P")
  13597. (if (org-at-table-p)
  13598. (org-table-paste-rectangle)
  13599. (org-paste-subtree arg)))
  13600. (defun org-edit-special ()
  13601. "Call a special editor for the stuff at point.
  13602. When at a table, call the formula editor with `org-table-edit-formulas'.
  13603. When at the first line of an src example, call `org-edit-src-code'.
  13604. When in an #+include line, visit the include file. Otherwise call
  13605. `ffap' to visit the file at point."
  13606. (interactive)
  13607. (cond
  13608. ((org-at-table-p)
  13609. (call-interactively 'org-table-edit-formulas))
  13610. ((save-excursion
  13611. (beginning-of-line 1)
  13612. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13613. (find-file (org-trim (match-string 1))))
  13614. ((org-edit-src-code))
  13615. ((org-edit-fixed-width-region))
  13616. (t (call-interactively 'ffap))))
  13617. (defun org-ctrl-c-ctrl-c (&optional arg)
  13618. "Set tags in headline, or update according to changed information at point.
  13619. This command does many different things, depending on context:
  13620. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13621. this is what we do.
  13622. - If the cursor is in a headline, prompt for tags and insert them
  13623. into the current line, aligned to `org-tags-column'. When called
  13624. with prefix arg, realign all tags in the current buffer.
  13625. - If the cursor is in one of the special #+KEYWORD lines, this
  13626. triggers scanning the buffer for these lines and updating the
  13627. information.
  13628. - If the cursor is inside a table, realign the table. This command
  13629. works even if the automatic table editor has been turned off.
  13630. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13631. the entire table.
  13632. - If the cursor is at a footnote reference or definition, jump to
  13633. the corresponding definition or references, respectively.
  13634. - If the cursor is a the beginning of a dynamic block, update it.
  13635. - If the cursor is inside a table created by the table.el package,
  13636. activate that table.
  13637. - If the current buffer is a remember buffer, close note and file
  13638. it. A prefix argument of 1 files to the default location
  13639. without further interaction. A prefix argument of 2 files to
  13640. the currently clocking task.
  13641. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13642. links in this buffer.
  13643. - If the cursor is on a numbered item in a plain list, renumber the
  13644. ordered list.
  13645. - If the cursor is on a checkbox, toggle it."
  13646. (interactive "P")
  13647. (let ((org-enable-table-editor t))
  13648. (cond
  13649. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13650. org-occur-highlights
  13651. org-latex-fragment-image-overlays)
  13652. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13653. (org-remove-occur-highlights)
  13654. (org-remove-latex-fragment-image-overlays)
  13655. (message "Temporary highlights/overlays removed from current buffer"))
  13656. ((and (local-variable-p 'org-finish-function (current-buffer))
  13657. (fboundp org-finish-function))
  13658. (funcall org-finish-function))
  13659. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13660. ((org-at-property-p)
  13661. (call-interactively 'org-property-action))
  13662. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13663. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13664. ((org-at-table.el-p)
  13665. (require 'table)
  13666. (beginning-of-line 1)
  13667. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13668. (call-interactively 'table-recognize-table))
  13669. ((org-at-table-p)
  13670. (org-table-maybe-eval-formula)
  13671. (if arg
  13672. (call-interactively 'org-table-recalculate)
  13673. (org-table-maybe-recalculate-line))
  13674. (call-interactively 'org-table-align))
  13675. ((or (org-footnote-at-reference-p)
  13676. (org-footnote-at-definition-p))
  13677. (call-interactively 'org-footnote-action))
  13678. ((org-at-item-checkbox-p)
  13679. (call-interactively 'org-toggle-checkbox))
  13680. ((org-at-item-p)
  13681. (if arg
  13682. (call-interactively 'org-toggle-checkbox)
  13683. (call-interactively 'org-maybe-renumber-ordered-list)))
  13684. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13685. ;; Dynamic block
  13686. (beginning-of-line 1)
  13687. (save-excursion (org-update-dblock)))
  13688. ((save-excursion
  13689. (beginning-of-line 1)
  13690. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13691. (cond
  13692. ((equal (match-string 1) "TBLFM")
  13693. ;; Recalculate the table before this line
  13694. (save-excursion
  13695. (beginning-of-line 1)
  13696. (skip-chars-backward " \r\n\t")
  13697. (if (org-at-table-p)
  13698. (org-call-with-arg 'org-table-recalculate t))))
  13699. (t
  13700. ; (org-set-regexps-and-options)
  13701. ; (org-restart-font-lock)
  13702. (let ((org-inhibit-startup t)) (org-mode-restart))
  13703. (message "Local setup has been refreshed"))))
  13704. ((org-clock-update-time-maybe))
  13705. (t (error "C-c C-c can do nothing useful at this location.")))))
  13706. (defun org-mode-restart ()
  13707. "Restart Org-mode, to scan again for special lines.
  13708. Also updates the keyword regular expressions."
  13709. (interactive)
  13710. (org-mode)
  13711. (message "Org-mode restarted"))
  13712. (defun org-kill-note-or-show-branches ()
  13713. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13714. (interactive)
  13715. (if (not org-finish-function)
  13716. (call-interactively 'show-branches)
  13717. (let ((org-note-abort t))
  13718. (funcall org-finish-function))))
  13719. (defun org-return (&optional indent)
  13720. "Goto next table row or insert a newline.
  13721. Calls `org-table-next-row' or `newline', depending on context.
  13722. See the individual commands for more information."
  13723. (interactive)
  13724. (cond
  13725. ((bobp) (if indent (newline-and-indent) (newline)))
  13726. ((org-at-table-p)
  13727. (org-table-justify-field-maybe)
  13728. (call-interactively 'org-table-next-row))
  13729. ((and org-return-follows-link
  13730. (eq (get-text-property (point) 'face) 'org-link))
  13731. (call-interactively 'org-open-at-point))
  13732. ((and (org-at-heading-p)
  13733. (looking-at
  13734. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13735. (org-show-entry)
  13736. (end-of-line 1)
  13737. (newline))
  13738. (t (if indent (newline-and-indent) (newline)))))
  13739. (defun org-return-indent ()
  13740. "Goto next table row or insert a newline and indent.
  13741. Calls `org-table-next-row' or `newline-and-indent', depending on
  13742. context. See the individual commands for more information."
  13743. (interactive)
  13744. (org-return t))
  13745. (defun org-ctrl-c-star ()
  13746. "Compute table, or change heading status of lines.
  13747. Calls `org-table-recalculate' or `org-toggle-heading',
  13748. depending on context."
  13749. (interactive)
  13750. (cond
  13751. ((org-at-table-p)
  13752. (call-interactively 'org-table-recalculate))
  13753. (t
  13754. ;; Convert all lines in region to list items
  13755. (call-interactively 'org-toggle-heading))))
  13756. (defun org-ctrl-c-minus ()
  13757. "Insert separator line in table or modify bullet status of line.
  13758. Also turns a plain line or a region of lines into list items.
  13759. Calls `org-table-insert-hline', `org-toggle-item', or
  13760. `org-cycle-list-bullet', depending on context."
  13761. (interactive)
  13762. (cond
  13763. ((org-at-table-p)
  13764. (call-interactively 'org-table-insert-hline))
  13765. ((org-region-active-p)
  13766. (call-interactively 'org-toggle-item))
  13767. ((org-in-item-p)
  13768. (call-interactively 'org-cycle-list-bullet))
  13769. (t
  13770. (call-interactively 'org-toggle-item))))
  13771. (defun org-toggle-item ()
  13772. "Convert headings or normal lines to items, items to normal lines.
  13773. If there is no active region, only the current line is considered.
  13774. If the first line in the region is a headline, convert all headlines to items.
  13775. If the first line in the region is an item, convert all items to normal lines.
  13776. If the first line is normal text, add an item bullet to each line."
  13777. (interactive)
  13778. (let (l2 l beg end)
  13779. (if (org-region-active-p)
  13780. (setq beg (region-beginning) end (region-end))
  13781. (setq beg (point-at-bol)
  13782. end (min (1+ (point-at-eol)) (point-max))))
  13783. (save-excursion
  13784. (goto-char end)
  13785. (setq l2 (org-current-line))
  13786. (goto-char beg)
  13787. (beginning-of-line 1)
  13788. (setq l (1- (org-current-line)))
  13789. (if (org-at-item-p)
  13790. ;; We already have items, de-itemize
  13791. (while (< (setq l (1+ l)) l2)
  13792. (when (org-at-item-p)
  13793. (goto-char (match-beginning 2))
  13794. (delete-region (match-beginning 2) (match-end 2))
  13795. (and (looking-at "[ \t]+") (replace-match "")))
  13796. (beginning-of-line 2))
  13797. (if (org-on-heading-p)
  13798. ;; Headings, convert to items
  13799. (while (< (setq l (1+ l)) l2)
  13800. (if (looking-at org-outline-regexp)
  13801. (replace-match "- " t t))
  13802. (beginning-of-line 2))
  13803. ;; normal lines, turn them into items
  13804. (while (< (setq l (1+ l)) l2)
  13805. (unless (org-at-item-p)
  13806. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13807. (replace-match "\\1- \\2")))
  13808. (beginning-of-line 2)))))))
  13809. (defun org-toggle-heading (&optional nstars)
  13810. "Convert headings to normal text, or items or text to headings.
  13811. If there is no active region, only the current line is considered.
  13812. If the first line is a heading, remove the stars from all headlines
  13813. in the region.
  13814. If the first line is a plain list item, turn all plain list items into
  13815. headings.
  13816. If the first line is a normal line, turn each and every line in the region
  13817. into a heading.
  13818. When converting a line into a heading, the number of stars is chosen
  13819. such that the lines become children of the current entry. However, when
  13820. a prefix argument is given, its value determines the number of stars to add."
  13821. (interactive "P")
  13822. (let (l2 l itemp beg end)
  13823. (if (org-region-active-p)
  13824. (setq beg (region-beginning) end (region-end))
  13825. (setq beg (point-at-bol)
  13826. end (min (1+ (point-at-eol)) (point-max))))
  13827. (save-excursion
  13828. (goto-char end)
  13829. (setq l2 (org-current-line))
  13830. (goto-char beg)
  13831. (beginning-of-line 1)
  13832. (setq l (1- (org-current-line)))
  13833. (if (org-on-heading-p)
  13834. ;; We already have headlines, de-star them
  13835. (while (< (setq l (1+ l)) l2)
  13836. (when (org-on-heading-p t)
  13837. (and (looking-at outline-regexp) (replace-match "")))
  13838. (beginning-of-line 2))
  13839. (setq itemp (org-at-item-p))
  13840. (let* ((stars
  13841. (if nstars
  13842. (make-string (prefix-numeric-value current-prefix-arg)
  13843. ?*)
  13844. (save-excursion
  13845. (re-search-backward org-complex-heading-regexp nil t)
  13846. (or (match-string 1) ""))))
  13847. (add-stars (cond (nstars "")
  13848. ((equal stars "") "*")
  13849. (org-odd-levels-only "**")
  13850. (t "*")))
  13851. (rpl (concat stars add-stars " ")))
  13852. (while (< (setq l (1+ l)) l2)
  13853. (if itemp
  13854. (and (org-at-item-p) (replace-match rpl t t))
  13855. (unless (org-on-heading-p)
  13856. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13857. (replace-match (concat rpl (match-string 2))))))
  13858. (beginning-of-line 2)))))))
  13859. (defun org-meta-return (&optional arg)
  13860. "Insert a new heading or wrap a region in a table.
  13861. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13862. See the individual commands for more information."
  13863. (interactive "P")
  13864. (cond
  13865. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13866. ((org-at-table-p)
  13867. (call-interactively 'org-table-wrap-region))
  13868. (t (call-interactively 'org-insert-heading))))
  13869. ;;; Menu entries
  13870. ;; Define the Org-mode menus
  13871. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13872. '("Tbl"
  13873. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13874. ["Next Field" org-cycle (org-at-table-p)]
  13875. ["Previous Field" org-shifttab (org-at-table-p)]
  13876. ["Next Row" org-return (org-at-table-p)]
  13877. "--"
  13878. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13879. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13880. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13881. "--"
  13882. ("Column"
  13883. ["Move Column Left" org-metaleft (org-at-table-p)]
  13884. ["Move Column Right" org-metaright (org-at-table-p)]
  13885. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13886. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13887. ("Row"
  13888. ["Move Row Up" org-metaup (org-at-table-p)]
  13889. ["Move Row Down" org-metadown (org-at-table-p)]
  13890. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13891. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13892. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13893. "--"
  13894. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13895. ("Rectangle"
  13896. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13897. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13898. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13899. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13900. "--"
  13901. ("Calculate"
  13902. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13903. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13904. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13905. "--"
  13906. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13907. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13908. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13909. "--"
  13910. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13911. "--"
  13912. ["Sum Column/Rectangle" org-table-sum
  13913. (or (org-at-table-p) (org-region-active-p))]
  13914. ["Which Column?" org-table-current-column (org-at-table-p)])
  13915. ["Debug Formulas"
  13916. org-table-toggle-formula-debugger
  13917. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13918. ["Show Col/Row Numbers"
  13919. org-table-toggle-coordinate-overlays
  13920. :style toggle
  13921. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13922. "--"
  13923. ["Create" org-table-create (and (not (org-at-table-p))
  13924. org-enable-table-editor)]
  13925. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13926. ["Import from File" org-table-import (not (org-at-table-p))]
  13927. ["Export to File" org-table-export (org-at-table-p)]
  13928. "--"
  13929. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13930. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13931. '("Org"
  13932. ("Show/Hide"
  13933. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13934. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13935. ["Sparse Tree..." org-sparse-tree t]
  13936. ["Reveal Context" org-reveal t]
  13937. ["Show All" show-all t]
  13938. "--"
  13939. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13940. "--"
  13941. ["New Heading" org-insert-heading t]
  13942. ("Navigate Headings"
  13943. ["Up" outline-up-heading t]
  13944. ["Next" outline-next-visible-heading t]
  13945. ["Previous" outline-previous-visible-heading t]
  13946. ["Next Same Level" outline-forward-same-level t]
  13947. ["Previous Same Level" outline-backward-same-level t]
  13948. "--"
  13949. ["Jump" org-goto t])
  13950. ("Edit Structure"
  13951. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13952. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13953. "--"
  13954. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13955. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13956. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13957. "--"
  13958. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13959. "--"
  13960. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13961. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13962. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13963. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13964. "--"
  13965. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13966. "--"
  13967. ["Convert to odd levels" org-convert-to-odd-levels t]
  13968. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13969. ("Editing"
  13970. ["Emphasis..." org-emphasize t]
  13971. ["Edit Source Example" org-edit-special t]
  13972. "--"
  13973. ["Footnote new/jump" org-footnote-action t]
  13974. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13975. ("Archive"
  13976. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13977. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13978. ; :active t :keys "C-u C-c C-x C-a"]
  13979. ["Sparse trees open ARCHIVE trees"
  13980. (setq org-sparse-tree-open-archived-trees
  13981. (not org-sparse-tree-open-archived-trees))
  13982. :style toggle :selected org-sparse-tree-open-archived-trees]
  13983. ["Cycling opens ARCHIVE trees"
  13984. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13985. :style toggle :selected org-cycle-open-archived-trees]
  13986. "--"
  13987. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13988. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13989. ; ["Check and Move Children" (org-archive-subtree '(4))
  13990. ; :active t :keys "C-u C-c C-x C-s"]
  13991. )
  13992. "--"
  13993. ("Hyperlinks"
  13994. ["Store Link (Global)" org-store-link t]
  13995. ["Insert Link" org-insert-link t]
  13996. ["Follow Link" org-open-at-point t]
  13997. "--"
  13998. ["Next link" org-next-link t]
  13999. ["Previous link" org-previous-link t]
  14000. "--"
  14001. ["Descriptive Links"
  14002. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14003. :style radio
  14004. :selected (member '(org-link) buffer-invisibility-spec)]
  14005. ["Literal Links"
  14006. (progn
  14007. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14008. :style radio
  14009. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14010. "--"
  14011. ("TODO Lists"
  14012. ["TODO/DONE/-" org-todo t]
  14013. ("Select keyword"
  14014. ["Next keyword" org-shiftright (org-on-heading-p)]
  14015. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14016. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14017. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14018. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14019. ["Show TODO Tree" org-show-todo-tree t]
  14020. ["Global TODO list" org-todo-list t]
  14021. "--"
  14022. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14023. :selected org-enforce-todo-dependencies :style toggle :active t]
  14024. "Settings for tree at point"
  14025. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14026. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14027. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14028. ["Do Children parallel" org-toggle-ordered-property :style radio
  14029. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14030. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14031. "--"
  14032. ["Set Priority" org-priority t]
  14033. ["Priority Up" org-shiftup t]
  14034. ["Priority Down" org-shiftdown t]
  14035. "--"
  14036. ["Get news from all feeds" org-feed-update-all t]
  14037. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14038. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14039. ("TAGS and Properties"
  14040. ["Set Tags" org-set-tags-command t]
  14041. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14042. "--"
  14043. ["Set property" org-set-property t]
  14044. ["Column view of properties" org-columns t]
  14045. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14046. ("Dates and Scheduling"
  14047. ["Timestamp" org-time-stamp t]
  14048. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14049. ("Change Date"
  14050. ["1 Day Later" org-shiftright t]
  14051. ["1 Day Earlier" org-shiftleft t]
  14052. ["1 ... Later" org-shiftup t]
  14053. ["1 ... Earlier" org-shiftdown t])
  14054. ["Compute Time Range" org-evaluate-time-range t]
  14055. ["Schedule Item" org-schedule t]
  14056. ["Deadline" org-deadline t]
  14057. "--"
  14058. ["Custom time format" org-toggle-time-stamp-overlays
  14059. :style radio :selected org-display-custom-times]
  14060. "--"
  14061. ["Goto Calendar" org-goto-calendar t]
  14062. ["Date from Calendar" org-date-from-calendar t]
  14063. "--"
  14064. ["Start/Restart Timer" org-timer-start t]
  14065. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14066. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14067. ["Insert Timer String" org-timer t]
  14068. ["Insert Timer Item" org-timer-item t])
  14069. ("Logging work"
  14070. ["Clock in" org-clock-in t]
  14071. ["Clock out" org-clock-out t]
  14072. ["Clock cancel" org-clock-cancel t]
  14073. ["Goto running clock" org-clock-goto t]
  14074. ["Display times" org-clock-display t]
  14075. ["Create clock table" org-clock-report t]
  14076. "--"
  14077. ["Record DONE time"
  14078. (progn (setq org-log-done (not org-log-done))
  14079. (message "Switching to %s will %s record a timestamp"
  14080. (car org-done-keywords)
  14081. (if org-log-done "automatically" "not")))
  14082. :style toggle :selected org-log-done])
  14083. "--"
  14084. ["Agenda Command..." org-agenda t]
  14085. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14086. ("File List for Agenda")
  14087. ("Special views current file"
  14088. ["TODO Tree" org-show-todo-tree t]
  14089. ["Check Deadlines" org-check-deadlines t]
  14090. ["Timeline" org-timeline t]
  14091. ["Tags/Property tree" org-match-sparse-tree t])
  14092. "--"
  14093. ["Export/Publish..." org-export t]
  14094. ("LaTeX"
  14095. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14096. :selected org-cdlatex-mode]
  14097. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14098. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14099. ["Modify math symbol" org-cdlatex-math-modify
  14100. (org-inside-LaTeX-fragment-p)]
  14101. ["Insert citation" org-reftex-citation t]
  14102. "--"
  14103. ["Export LaTeX fragments as images"
  14104. (if (featurep 'org-exp)
  14105. (setq org-export-with-LaTeX-fragments
  14106. (not org-export-with-LaTeX-fragments))
  14107. (require 'org-exp))
  14108. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14109. org-export-with-LaTeX-fragments)])
  14110. "--"
  14111. ("Documentation"
  14112. ["Show Version" org-version t]
  14113. ["Info Documentation" org-info t])
  14114. ("Customize"
  14115. ["Browse Org Group" org-customize t]
  14116. "--"
  14117. ["Expand This Menu" org-create-customize-menu
  14118. (fboundp 'customize-menu-create)])
  14119. "--"
  14120. ("Refresh/Reload"
  14121. ["Refresh setup current buffer" org-mode-restart t]
  14122. ["Reload Org (after update)" org-reload t]
  14123. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14124. ))
  14125. (defun org-info (&optional node)
  14126. "Read documentation for Org-mode in the info system.
  14127. With optional NODE, go directly to that node."
  14128. (interactive)
  14129. (info (format "(org)%s" (or node ""))))
  14130. (defun org-install-agenda-files-menu ()
  14131. (let ((bl (buffer-list)))
  14132. (save-excursion
  14133. (while bl
  14134. (set-buffer (pop bl))
  14135. (if (org-mode-p) (setq bl nil)))
  14136. (when (org-mode-p)
  14137. (easy-menu-change
  14138. '("Org") "File List for Agenda"
  14139. (append
  14140. (list
  14141. ["Edit File List" (org-edit-agenda-file-list) t]
  14142. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14143. ["Remove Current File from List" org-remove-file t]
  14144. ["Cycle through agenda files" org-cycle-agenda-files t]
  14145. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14146. "--")
  14147. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14148. ;;;; Documentation
  14149. ;;;###autoload
  14150. (defun org-require-autoloaded-modules ()
  14151. (interactive)
  14152. (mapc 'require
  14153. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14154. org-docbook org-exp org-html org-icalendar
  14155. org-id org-latex
  14156. org-publish org-remember org-table
  14157. org-timer org-xoxo)))
  14158. ;;;###autoload
  14159. (defun org-reload (&optional uncompiled)
  14160. "Reload all org lisp files.
  14161. With prefix arg UNCOMPILED, load the uncompiled versions."
  14162. (interactive "P")
  14163. (require 'find-func)
  14164. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14165. (dir-org (file-name-directory (org-find-library-name "org")))
  14166. (dir-org-contrib (ignore-errors
  14167. (file-name-directory
  14168. (org-find-library-name "org-contribdir"))))
  14169. (files
  14170. (append (directory-files dir-org t file-re)
  14171. (and dir-org-contrib
  14172. (directory-files dir-org-contrib t file-re))))
  14173. (remove-re (concat (if (featurep 'xemacs)
  14174. "org-colview" "org-colview-xemacs")
  14175. "\\'")))
  14176. (setq files (mapcar 'file-name-sans-extension files))
  14177. (setq files (mapcar
  14178. (lambda (x) (if (string-match remove-re x) nil x))
  14179. files))
  14180. (setq files (delq nil files))
  14181. (mapc
  14182. (lambda (f)
  14183. (when (featurep (intern (file-name-nondirectory f)))
  14184. (if (and (not uncompiled)
  14185. (file-exists-p (concat f ".elc")))
  14186. (load (concat f ".elc") nil nil t)
  14187. (load (concat f ".el") nil nil t))))
  14188. files))
  14189. (org-version))
  14190. ;;;###autoload
  14191. (defun org-customize ()
  14192. "Call the customize function with org as argument."
  14193. (interactive)
  14194. (org-load-modules-maybe)
  14195. (org-require-autoloaded-modules)
  14196. (customize-browse 'org))
  14197. (defun org-create-customize-menu ()
  14198. "Create a full customization menu for Org-mode, insert it into the menu."
  14199. (interactive)
  14200. (org-load-modules-maybe)
  14201. (org-require-autoloaded-modules)
  14202. (if (fboundp 'customize-menu-create)
  14203. (progn
  14204. (easy-menu-change
  14205. '("Org") "Customize"
  14206. `(["Browse Org group" org-customize t]
  14207. "--"
  14208. ,(customize-menu-create 'org)
  14209. ["Set" Custom-set t]
  14210. ["Save" Custom-save t]
  14211. ["Reset to Current" Custom-reset-current t]
  14212. ["Reset to Saved" Custom-reset-saved t]
  14213. ["Reset to Standard Settings" Custom-reset-standard t]))
  14214. (message "\"Org\"-menu now contains full customization menu"))
  14215. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14216. ;;;; Miscellaneous stuff
  14217. ;;; Generally useful functions
  14218. (defun org-find-text-property-in-string (prop s)
  14219. "Return the first non-nil value of property PROP in string S."
  14220. (or (get-text-property 0 prop s)
  14221. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14222. prop s)))
  14223. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14224. "Display the given MESSAGE as a warning."
  14225. (if (fboundp 'display-warning)
  14226. (display-warning 'org message
  14227. (if (featurep 'xemacs)
  14228. 'warning
  14229. :warning))
  14230. (let ((buf (get-buffer-create "*Org warnings*")))
  14231. (with-current-buffer buf
  14232. (goto-char (point-max))
  14233. (insert "Warning (Org): " message)
  14234. (unless (bolp)
  14235. (newline)))
  14236. (display-buffer buf)
  14237. (sit-for 0))))
  14238. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14239. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14240. (if (and marker (marker-buffer marker)
  14241. (buffer-live-p (marker-buffer marker)))
  14242. (progn
  14243. (switch-to-buffer (marker-buffer marker))
  14244. (if (or (> marker (point-max)) (< marker (point-min)))
  14245. (widen))
  14246. (goto-char marker)
  14247. (org-show-context 'org-goto))
  14248. (if bookmark
  14249. (bookmark-jump bookmark)
  14250. (error "Cannot find location"))))
  14251. (defun org-quote-csv-field (s)
  14252. "Quote field for inclusion in CSV material."
  14253. (if (string-match "[\",]" s)
  14254. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14255. s))
  14256. (defun org-plist-delete (plist property)
  14257. "Delete PROPERTY from PLIST.
  14258. This is in contrast to merely setting it to 0."
  14259. (let (p)
  14260. (while plist
  14261. (if (not (eq property (car plist)))
  14262. (setq p (plist-put p (car plist) (nth 1 plist))))
  14263. (setq plist (cddr plist)))
  14264. p))
  14265. (defun org-force-self-insert (N)
  14266. "Needed to enforce self-insert under remapping."
  14267. (interactive "p")
  14268. (self-insert-command N))
  14269. (defun org-string-width (s)
  14270. "Compute width of string, ignoring invisible characters.
  14271. This ignores character with invisibility property `org-link', and also
  14272. characters with property `org-cwidth', because these will become invisible
  14273. upon the next fontification round."
  14274. (let (b l)
  14275. (when (or (eq t buffer-invisibility-spec)
  14276. (assq 'org-link buffer-invisibility-spec))
  14277. (while (setq b (text-property-any 0 (length s)
  14278. 'invisible 'org-link s))
  14279. (setq s (concat (substring s 0 b)
  14280. (substring s (or (next-single-property-change
  14281. b 'invisible s) (length s)))))))
  14282. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14283. (setq s (concat (substring s 0 b)
  14284. (substring s (or (next-single-property-change
  14285. b 'org-cwidth s) (length s))))))
  14286. (setq l (string-width s) b -1)
  14287. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14288. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14289. l))
  14290. (defun org-get-indentation (&optional line)
  14291. "Get the indentation of the current line, interpreting tabs.
  14292. When LINE is given, assume it represents a line and compute its indentation."
  14293. (if line
  14294. (if (string-match "^ *" (org-remove-tabs line))
  14295. (match-end 0))
  14296. (save-excursion
  14297. (beginning-of-line 1)
  14298. (skip-chars-forward " \t")
  14299. (current-column))))
  14300. (defun org-remove-tabs (s &optional width)
  14301. "Replace tabulators in S with spaces.
  14302. Assumes that s is a single line, starting in column 0."
  14303. (setq width (or width tab-width))
  14304. (while (string-match "\t" s)
  14305. (setq s (replace-match
  14306. (make-string
  14307. (- (* width (/ (+ (match-beginning 0) width) width))
  14308. (match-beginning 0)) ?\ )
  14309. t t s)))
  14310. s)
  14311. (defun org-fix-indentation (line ind)
  14312. "Fix indentation in LINE.
  14313. IND is a cons cell with target and minimum indentation.
  14314. If the current indentation in LINE is smaller than the minimum,
  14315. leave it alone. If it is larger than ind, set it to the target."
  14316. (let* ((l (org-remove-tabs line))
  14317. (i (org-get-indentation l))
  14318. (i1 (car ind)) (i2 (cdr ind)))
  14319. (if (>= i i2) (setq l (substring line i2)))
  14320. (if (> i1 0)
  14321. (concat (make-string i1 ?\ ) l)
  14322. l)))
  14323. (defun org-remove-indentation (code &optional n)
  14324. "Remove the maximum common indentation from the lines in CODE.
  14325. N may optionally be the number of spaces to remove."
  14326. (with-temp-buffer
  14327. (insert code)
  14328. (org-do-remove-indentation n)
  14329. (buffer-string)))
  14330. (defun org-do-remove-indentation (&optional n)
  14331. "Remove the maximum common indentation from the buffer."
  14332. (untabify (point-min) (point-max))
  14333. (let ((min 10000) re)
  14334. (if n
  14335. (setq min n)
  14336. (goto-char (point-min))
  14337. (while (re-search-forward "^ *[^ \n]" nil t)
  14338. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14339. (unless (or (= min 0) (= min 10000))
  14340. (setq re (format "^ \\{%d\\}" min))
  14341. (goto-char (point-min))
  14342. (while (re-search-forward re nil t)
  14343. (replace-match "")
  14344. (end-of-line 1))
  14345. min)))
  14346. (defun org-base-buffer (buffer)
  14347. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14348. (if (not buffer)
  14349. buffer
  14350. (or (buffer-base-buffer buffer)
  14351. buffer)))
  14352. (defun org-trim (s)
  14353. "Remove whitespace at beginning and end of string."
  14354. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14355. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14356. s)
  14357. (defun org-wrap (string &optional width lines)
  14358. "Wrap string to either a number of lines, or a width in characters.
  14359. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14360. that costs. If there is a word longer than WIDTH, the text is actually
  14361. wrapped to the length of that word.
  14362. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14363. many lines, whatever width that takes.
  14364. The return value is a list of lines, without newlines at the end."
  14365. (let* ((words (org-split-string string "[ \t\n]+"))
  14366. (maxword (apply 'max (mapcar 'org-string-width words)))
  14367. w ll)
  14368. (cond (width
  14369. (org-do-wrap words (max maxword width)))
  14370. (lines
  14371. (setq w maxword)
  14372. (setq ll (org-do-wrap words maxword))
  14373. (if (<= (length ll) lines)
  14374. ll
  14375. (setq ll words)
  14376. (while (> (length ll) lines)
  14377. (setq w (1+ w))
  14378. (setq ll (org-do-wrap words w)))
  14379. ll))
  14380. (t (error "Cannot wrap this")))))
  14381. (defun org-do-wrap (words width)
  14382. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14383. (let (lines line)
  14384. (while words
  14385. (setq line (pop words))
  14386. (while (and words (< (+ (length line) (length (car words))) width))
  14387. (setq line (concat line " " (pop words))))
  14388. (setq lines (push line lines)))
  14389. (nreverse lines)))
  14390. (defun org-split-string (string &optional separators)
  14391. "Splits STRING into substrings at SEPARATORS.
  14392. No empty strings are returned if there are matches at the beginning
  14393. and end of string."
  14394. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14395. (start 0)
  14396. notfirst
  14397. (list nil))
  14398. (while (and (string-match rexp string
  14399. (if (and notfirst
  14400. (= start (match-beginning 0))
  14401. (< start (length string)))
  14402. (1+ start) start))
  14403. (< (match-beginning 0) (length string)))
  14404. (setq notfirst t)
  14405. (or (eq (match-beginning 0) 0)
  14406. (and (eq (match-beginning 0) (match-end 0))
  14407. (eq (match-beginning 0) start))
  14408. (setq list
  14409. (cons (substring string start (match-beginning 0))
  14410. list)))
  14411. (setq start (match-end 0)))
  14412. (or (eq start (length string))
  14413. (setq list
  14414. (cons (substring string start)
  14415. list)))
  14416. (nreverse list)))
  14417. (defun org-uuidgen-p (s)
  14418. "Is S an ID created by UUIDGEN?"
  14419. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14420. (defun org-context ()
  14421. "Return a list of contexts of the current cursor position.
  14422. If several contexts apply, all are returned.
  14423. Each context entry is a list with a symbol naming the context, and
  14424. two positions indicating start and end of the context. Possible
  14425. contexts are:
  14426. :headline anywhere in a headline
  14427. :headline-stars on the leading stars in a headline
  14428. :todo-keyword on a TODO keyword (including DONE) in a headline
  14429. :tags on the TAGS in a headline
  14430. :priority on the priority cookie in a headline
  14431. :item on the first line of a plain list item
  14432. :item-bullet on the bullet/number of a plain list item
  14433. :checkbox on the checkbox in a plain list item
  14434. :table in an org-mode table
  14435. :table-special on a special filed in a table
  14436. :table-table in a table.el table
  14437. :link on a hyperlink
  14438. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14439. :target on a <<target>>
  14440. :radio-target on a <<<radio-target>>>
  14441. :latex-fragment on a LaTeX fragment
  14442. :latex-preview on a LaTeX fragment with overlayed preview image
  14443. This function expects the position to be visible because it uses font-lock
  14444. faces as a help to recognize the following contexts: :table-special, :link,
  14445. and :keyword."
  14446. (let* ((f (get-text-property (point) 'face))
  14447. (faces (if (listp f) f (list f)))
  14448. (p (point)) clist o)
  14449. ;; First the large context
  14450. (cond
  14451. ((org-on-heading-p t)
  14452. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14453. (when (progn
  14454. (beginning-of-line 1)
  14455. (looking-at org-todo-line-tags-regexp))
  14456. (push (org-point-in-group p 1 :headline-stars) clist)
  14457. (push (org-point-in-group p 2 :todo-keyword) clist)
  14458. (push (org-point-in-group p 4 :tags) clist))
  14459. (goto-char p)
  14460. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14461. (if (looking-at "\\[#[A-Z0-9]\\]")
  14462. (push (org-point-in-group p 0 :priority) clist)))
  14463. ((org-at-item-p)
  14464. (push (org-point-in-group p 2 :item-bullet) clist)
  14465. (push (list :item (point-at-bol)
  14466. (save-excursion (org-end-of-item) (point)))
  14467. clist)
  14468. (and (org-at-item-checkbox-p)
  14469. (push (org-point-in-group p 0 :checkbox) clist)))
  14470. ((org-at-table-p)
  14471. (push (list :table (org-table-begin) (org-table-end)) clist)
  14472. (if (memq 'org-formula faces)
  14473. (push (list :table-special
  14474. (previous-single-property-change p 'face)
  14475. (next-single-property-change p 'face)) clist)))
  14476. ((org-at-table-p 'any)
  14477. (push (list :table-table) clist)))
  14478. (goto-char p)
  14479. ;; Now the small context
  14480. (cond
  14481. ((org-at-timestamp-p)
  14482. (push (org-point-in-group p 0 :timestamp) clist))
  14483. ((memq 'org-link faces)
  14484. (push (list :link
  14485. (previous-single-property-change p 'face)
  14486. (next-single-property-change p 'face)) clist))
  14487. ((memq 'org-special-keyword faces)
  14488. (push (list :keyword
  14489. (previous-single-property-change p 'face)
  14490. (next-single-property-change p 'face)) clist))
  14491. ((org-on-target-p)
  14492. (push (org-point-in-group p 0 :target) clist)
  14493. (goto-char (1- (match-beginning 0)))
  14494. (if (looking-at org-radio-target-regexp)
  14495. (push (org-point-in-group p 0 :radio-target) clist))
  14496. (goto-char p))
  14497. ((setq o (car (delq nil
  14498. (mapcar
  14499. (lambda (x)
  14500. (if (memq x org-latex-fragment-image-overlays) x))
  14501. (org-overlays-at (point))))))
  14502. (push (list :latex-fragment
  14503. (org-overlay-start o) (org-overlay-end o)) clist)
  14504. (push (list :latex-preview
  14505. (org-overlay-start o) (org-overlay-end o)) clist))
  14506. ((org-inside-LaTeX-fragment-p)
  14507. ;; FIXME: positions wrong.
  14508. (push (list :latex-fragment (point) (point)) clist)))
  14509. (setq clist (nreverse (delq nil clist)))
  14510. clist))
  14511. ;; FIXME: Compare with at-regexp-p Do we need both?
  14512. (defun org-in-regexp (re &optional nlines visually)
  14513. "Check if point is inside a match of regexp.
  14514. Normally only the current line is checked, but you can include NLINES extra
  14515. lines both before and after point into the search.
  14516. If VISUALLY is set, require that the cursor is not after the match but
  14517. really on, so that the block visually is on the match."
  14518. (catch 'exit
  14519. (let ((pos (point))
  14520. (eol (point-at-eol (+ 1 (or nlines 0))))
  14521. (inc (if visually 1 0)))
  14522. (save-excursion
  14523. (beginning-of-line (- 1 (or nlines 0)))
  14524. (while (re-search-forward re eol t)
  14525. (if (and (<= (match-beginning 0) pos)
  14526. (>= (+ inc (match-end 0)) pos))
  14527. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14528. (defun org-at-regexp-p (regexp)
  14529. "Is point inside a match of REGEXP in the current line?"
  14530. (catch 'exit
  14531. (save-excursion
  14532. (let ((pos (point)) (end (point-at-eol)))
  14533. (beginning-of-line 1)
  14534. (while (re-search-forward regexp end t)
  14535. (if (and (<= (match-beginning 0) pos)
  14536. (>= (match-end 0) pos))
  14537. (throw 'exit t)))
  14538. nil))))
  14539. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14540. "Call `multi-occur' with buffers for all agenda files."
  14541. (interactive "sOrg-files matching: \np")
  14542. (let* ((files (org-agenda-files))
  14543. (tnames (mapcar 'file-truename files))
  14544. (extra org-agenda-text-search-extra-files)
  14545. f)
  14546. (when (eq (car extra) 'agenda-archives)
  14547. (setq extra (cdr extra))
  14548. (setq files (org-add-archive-files files)))
  14549. (while (setq f (pop extra))
  14550. (unless (member (file-truename f) tnames)
  14551. (add-to-list 'files f 'append)
  14552. (add-to-list 'tnames (file-truename f) 'append)))
  14553. (multi-occur
  14554. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14555. regexp)))
  14556. (if (boundp 'occur-mode-find-occurrence-hook)
  14557. ;; Emacs 23
  14558. (add-hook 'occur-mode-find-occurrence-hook
  14559. (lambda ()
  14560. (when (org-mode-p)
  14561. (org-reveal))))
  14562. ;; Emacs 22
  14563. (defadvice occur-mode-goto-occurrence
  14564. (after org-occur-reveal activate)
  14565. (and (org-mode-p) (org-reveal)))
  14566. (defadvice occur-mode-goto-occurrence-other-window
  14567. (after org-occur-reveal activate)
  14568. (and (org-mode-p) (org-reveal)))
  14569. (defadvice occur-mode-display-occurrence
  14570. (after org-occur-reveal activate)
  14571. (when (org-mode-p)
  14572. (let ((pos (occur-mode-find-occurrence)))
  14573. (with-current-buffer (marker-buffer pos)
  14574. (save-excursion
  14575. (goto-char pos)
  14576. (org-reveal)))))))
  14577. (defun org-uniquify (list)
  14578. "Remove duplicate elements from LIST."
  14579. (let (res)
  14580. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14581. res))
  14582. (defun org-delete-all (elts list)
  14583. "Remove all elements in ELTS from LIST."
  14584. (while elts
  14585. (setq list (delete (pop elts) list)))
  14586. list)
  14587. (defun org-back-over-empty-lines ()
  14588. "Move backwards over whitespace, to the beginning of the first empty line.
  14589. Returns the number of empty lines passed."
  14590. (let ((pos (point)))
  14591. (skip-chars-backward " \t\n\r")
  14592. (beginning-of-line 2)
  14593. (goto-char (min (point) pos))
  14594. (count-lines (point) pos)))
  14595. (defun org-skip-whitespace ()
  14596. (skip-chars-forward " \t\n\r"))
  14597. (defun org-point-in-group (point group &optional context)
  14598. "Check if POINT is in match-group GROUP.
  14599. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14600. match. If the match group does ot exist or point is not inside it,
  14601. return nil."
  14602. (and (match-beginning group)
  14603. (>= point (match-beginning group))
  14604. (<= point (match-end group))
  14605. (if context
  14606. (list context (match-beginning group) (match-end group))
  14607. t)))
  14608. (defun org-switch-to-buffer-other-window (&rest args)
  14609. "Switch to buffer in a second window on the current frame.
  14610. In particular, do not allow pop-up frames."
  14611. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14612. special-display-function)
  14613. (apply 'switch-to-buffer-other-window args)))
  14614. (defun org-combine-plists (&rest plists)
  14615. "Create a single property list from all plists in PLISTS.
  14616. The process starts by copying the first list, and then setting properties
  14617. from the other lists. Settings in the last list are the most significant
  14618. ones and overrule settings in the other lists."
  14619. (let ((rtn (copy-sequence (pop plists)))
  14620. p v ls)
  14621. (while plists
  14622. (setq ls (pop plists))
  14623. (while ls
  14624. (setq p (pop ls) v (pop ls))
  14625. (setq rtn (plist-put rtn p v))))
  14626. rtn))
  14627. (defun org-move-line-down (arg)
  14628. "Move the current line down. With prefix argument, move it past ARG lines."
  14629. (interactive "p")
  14630. (let ((col (current-column))
  14631. beg end pos)
  14632. (beginning-of-line 1) (setq beg (point))
  14633. (beginning-of-line 2) (setq end (point))
  14634. (beginning-of-line (+ 1 arg))
  14635. (setq pos (move-marker (make-marker) (point)))
  14636. (insert (delete-and-extract-region beg end))
  14637. (goto-char pos)
  14638. (org-move-to-column col)))
  14639. (defun org-move-line-up (arg)
  14640. "Move the current line up. With prefix argument, move it past ARG lines."
  14641. (interactive "p")
  14642. (let ((col (current-column))
  14643. beg end pos)
  14644. (beginning-of-line 1) (setq beg (point))
  14645. (beginning-of-line 2) (setq end (point))
  14646. (beginning-of-line (- arg))
  14647. (setq pos (move-marker (make-marker) (point)))
  14648. (insert (delete-and-extract-region beg end))
  14649. (goto-char pos)
  14650. (org-move-to-column col)))
  14651. (defun org-replace-escapes (string table)
  14652. "Replace %-escapes in STRING with values in TABLE.
  14653. TABLE is an association list with keys like \"%a\" and string values.
  14654. The sequences in STRING may contain normal field width and padding information,
  14655. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14656. so values can contain further %-escapes if they are define later in TABLE."
  14657. (let ((case-fold-search nil)
  14658. e re rpl)
  14659. (while (setq e (pop table))
  14660. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14661. (while (string-match re string)
  14662. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14663. (cdr e)))
  14664. (setq string (replace-match rpl t t string))))
  14665. string))
  14666. (defun org-sublist (list start end)
  14667. "Return a section of LIST, from START to END.
  14668. Counting starts at 1."
  14669. (let (rtn (c start))
  14670. (setq list (nthcdr (1- start) list))
  14671. (while (and list (<= c end))
  14672. (push (pop list) rtn)
  14673. (setq c (1+ c)))
  14674. (nreverse rtn)))
  14675. (defun org-find-base-buffer-visiting (file)
  14676. "Like `find-buffer-visiting' but always return the base buffer and
  14677. not an indirect buffer."
  14678. (let ((buf (or (get-file-buffer file)
  14679. (find-buffer-visiting file))))
  14680. (if buf
  14681. (or (buffer-base-buffer buf) buf)
  14682. nil)))
  14683. (defun org-image-file-name-regexp (&optional extensions)
  14684. "Return regexp matching the file names of images.
  14685. If EXTENSIONS is given, only match these."
  14686. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14687. (image-file-name-regexp)
  14688. (let ((image-file-name-extensions
  14689. (or extensions
  14690. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14691. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14692. (concat "\\."
  14693. (regexp-opt (nconc (mapcar 'upcase
  14694. image-file-name-extensions)
  14695. image-file-name-extensions)
  14696. t)
  14697. "\\'"))))
  14698. (defun org-file-image-p (file &optional extensions)
  14699. "Return non-nil if FILE is an image."
  14700. (save-match-data
  14701. (string-match (org-image-file-name-regexp extensions) file)))
  14702. (defun org-get-cursor-date ()
  14703. "Return the date at cursor in as a time.
  14704. This works in the calendar and in the agenda, anywhere else it just
  14705. returns the current time."
  14706. (let (date day defd)
  14707. (cond
  14708. ((eq major-mode 'calendar-mode)
  14709. (setq date (calendar-cursor-to-date)
  14710. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14711. ((eq major-mode 'org-agenda-mode)
  14712. (setq day (get-text-property (point) 'day))
  14713. (if day
  14714. (setq date (calendar-gregorian-from-absolute day)
  14715. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14716. (nth 2 date))))))
  14717. (or defd (current-time))))
  14718. (defvar org-agenda-action-marker (make-marker)
  14719. "Marker pointing to the entry for the next agenda action.")
  14720. (defun org-mark-entry-for-agenda-action ()
  14721. "Mark the current entry as target of an agenda action.
  14722. Agenda actions are actions executed from the agenda with the key `k',
  14723. which make use of the date at the cursor."
  14724. (interactive)
  14725. (move-marker org-agenda-action-marker
  14726. (save-excursion (org-back-to-heading t) (point))
  14727. (current-buffer))
  14728. (message
  14729. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14730. ;;; Paragraph filling stuff.
  14731. ;; We want this to be just right, so use the full arsenal.
  14732. (defun org-indent-line-function ()
  14733. "Indent line like previous, but further if previous was headline or item."
  14734. (interactive)
  14735. (let* ((pos (point))
  14736. (itemp (org-at-item-p))
  14737. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14738. column bpos bcol tpos tcol bullet btype bullet-type)
  14739. ;; Find the previous relevant line
  14740. (beginning-of-line 1)
  14741. (cond
  14742. ((looking-at "#") (setq column 0))
  14743. ((looking-at "\\*+ ") (setq column 0))
  14744. ((and (looking-at "[ \t]*:END:")
  14745. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14746. (save-excursion
  14747. (goto-char (1- (match-beginning 1)))
  14748. (setq column (current-column))))
  14749. (t
  14750. (beginning-of-line 0)
  14751. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14752. (not (looking-at "[ \t]*:END:"))
  14753. (not (looking-at org-drawer-regexp)))
  14754. (beginning-of-line 0))
  14755. (cond
  14756. ((looking-at "\\*+[ \t]+")
  14757. (if (not org-adapt-indentation)
  14758. (setq column 0)
  14759. (goto-char (match-end 0))
  14760. (setq column (current-column))))
  14761. ((looking-at org-drawer-regexp)
  14762. (goto-char (1- (match-beginning 1)))
  14763. (setq column (current-column)))
  14764. ((looking-at "\\([ \t]*\\):END:")
  14765. (goto-char (match-end 1))
  14766. (setq column (current-column)))
  14767. ((org-in-item-p)
  14768. (org-beginning-of-item)
  14769. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14770. (setq bpos (match-beginning 1) tpos (match-end 0)
  14771. bcol (progn (goto-char bpos) (current-column))
  14772. tcol (progn (goto-char tpos) (current-column))
  14773. bullet (match-string 1)
  14774. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14775. (if (> tcol (+ bcol org-description-max-indent))
  14776. (setq tcol (+ bcol 5)))
  14777. (if (not itemp)
  14778. (setq column tcol)
  14779. (goto-char pos)
  14780. (beginning-of-line 1)
  14781. (if (looking-at "\\S-")
  14782. (progn
  14783. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14784. (setq bullet (match-string 1)
  14785. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14786. (setq column (if (equal btype bullet-type) bcol tcol)))
  14787. (setq column (org-get-indentation)))))
  14788. (t (setq column (org-get-indentation))))))
  14789. (goto-char pos)
  14790. (if (<= (current-column) (current-indentation))
  14791. (org-indent-line-to column)
  14792. (save-excursion (org-indent-line-to column)))
  14793. (setq column (current-column))
  14794. (beginning-of-line 1)
  14795. (if (looking-at
  14796. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14797. (replace-match (concat "\\1" (format org-property-format
  14798. (match-string 2) (match-string 3)))
  14799. t nil))
  14800. (org-move-to-column column)))
  14801. (defun org-set-autofill-regexps ()
  14802. (interactive)
  14803. ;; In the paragraph separator we include headlines, because filling
  14804. ;; text in a line directly attached to a headline would otherwise
  14805. ;; fill the headline as well.
  14806. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14807. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14808. ;; The paragraph starter includes hand-formatted lists.
  14809. (org-set-local 'paragraph-start
  14810. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14811. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14812. ;; But only if the user has not turned off tables or fixed-width regions
  14813. (org-set-local
  14814. 'auto-fill-inhibit-regexp
  14815. (concat "\\*+ \\|#\\+"
  14816. "\\|[ \t]*" org-keyword-time-regexp
  14817. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14818. (concat
  14819. "\\|[ \t]*["
  14820. (if org-enable-table-editor "|" "")
  14821. (if org-enable-fixed-width-editor ":" "")
  14822. "]"))))
  14823. ;; We use our own fill-paragraph function, to make sure that tables
  14824. ;; and fixed-width regions are not wrapped. That function will pass
  14825. ;; through to `fill-paragraph' when appropriate.
  14826. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14827. ; Adaptive filling: To get full control, first make sure that
  14828. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14829. (org-set-local 'adaptive-fill-regexp "\000")
  14830. (org-set-local 'adaptive-fill-function
  14831. 'org-adaptive-fill-function)
  14832. (org-set-local
  14833. 'align-mode-rules-list
  14834. '((org-in-buffer-settings
  14835. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14836. (modes . '(org-mode))))))
  14837. (defun org-fill-paragraph (&optional justify)
  14838. "Re-align a table, pass through to fill-paragraph if no table."
  14839. (let ((table-p (org-at-table-p))
  14840. (table.el-p (org-at-table.el-p)))
  14841. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14842. (save-excursion (goto-char (point-at-bol))
  14843. (looking-at outline-regexp)))
  14844. t) ; skip headlines
  14845. (table.el-p t) ; skip table.el tables
  14846. (table-p (org-table-align) t) ; align org-mode tables
  14847. (t nil)))) ; call paragraph-fill
  14848. ;; For reference, this is the default value of adaptive-fill-regexp
  14849. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14850. (defun org-adaptive-fill-function ()
  14851. "Return a fill prefix for org-mode files.
  14852. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14853. work correctly."
  14854. (cond ((looking-at "#[ \t]+")
  14855. (match-string 0))
  14856. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14857. (save-excursion
  14858. (if (> (match-end 1) (+ (match-beginning 1)
  14859. org-description-max-indent))
  14860. (goto-char (+ (match-beginning 1) 5))
  14861. (goto-char (match-end 0)))
  14862. (make-string (current-column) ?\ )))
  14863. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14864. (save-excursion
  14865. (goto-char (match-end 0))
  14866. (make-string (current-column) ?\ )))
  14867. (t nil)))
  14868. ;;; Other stuff.
  14869. (defun org-toggle-fixed-width-section (arg)
  14870. "Toggle the fixed-width export.
  14871. If there is no active region, the QUOTE keyword at the current headline is
  14872. inserted or removed. When present, it causes the text between this headline
  14873. and the next to be exported as fixed-width text, and unmodified.
  14874. If there is an active region, this command adds or removes a colon as the
  14875. first character of this line. If the first character of a line is a colon,
  14876. this line is also exported in fixed-width font."
  14877. (interactive "P")
  14878. (let* ((cc 0)
  14879. (regionp (org-region-active-p))
  14880. (beg (if regionp (region-beginning) (point)))
  14881. (end (if regionp (region-end)))
  14882. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14883. (case-fold-search nil)
  14884. (re "[ \t]*\\(: \\)")
  14885. off)
  14886. (if regionp
  14887. (save-excursion
  14888. (goto-char beg)
  14889. (setq cc (current-column))
  14890. (beginning-of-line 1)
  14891. (setq off (looking-at re))
  14892. (while (> nlines 0)
  14893. (setq nlines (1- nlines))
  14894. (beginning-of-line 1)
  14895. (cond
  14896. (arg
  14897. (org-move-to-column cc t)
  14898. (insert ": \n")
  14899. (forward-line -1))
  14900. ((and off (looking-at re))
  14901. (replace-match "" t t nil 1))
  14902. ((not off) (org-move-to-column cc t) (insert ": ")))
  14903. (forward-line 1)))
  14904. (save-excursion
  14905. (org-back-to-heading)
  14906. (if (looking-at (concat outline-regexp
  14907. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14908. (replace-match "" t t nil 1)
  14909. (if (looking-at outline-regexp)
  14910. (progn
  14911. (goto-char (match-end 0))
  14912. (insert org-quote-string " "))))))))
  14913. (defun org-reftex-citation ()
  14914. "Use reftex-citation to insert a citation into the buffer.
  14915. This looks for a line like
  14916. #+BIBLIOGRAPHY: foo plain option:-d
  14917. and derives from it that foo.bib is the bbliography file relevant
  14918. for this document. It then installs the necessary environment for RefTeX
  14919. to work in this buffer and calls `reftex-citation' to insert a citation
  14920. into the buffer.
  14921. Export of such citations to both LaTeX and HTML is handled by the contributed
  14922. package org-exp-bibtex by Taru Karttunen."
  14923. (interactive)
  14924. (let ((reftex-docstruct-symbol 'rds)
  14925. (reftex-cite-format "\\cite{%l}")
  14926. rds bib)
  14927. (save-excursion
  14928. (save-restriction
  14929. (widen)
  14930. (let ((case-fold-search t)
  14931. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14932. (if (not (save-excursion
  14933. (or (re-search-forward re nil t)
  14934. (re-search-backward re nil t))))
  14935. (error "No bibliography defined in file")
  14936. (setq bib (concat (match-string 1) ".bib")
  14937. rds (list (list 'bib bib)))))))
  14938. (call-interactively 'reftex-citation)))
  14939. ;;;; Functions extending outline functionality
  14940. (defun org-beginning-of-line (&optional arg)
  14941. "Go to the beginning of the current line. If that is invisible, continue
  14942. to a visible line beginning. This makes the function of C-a more intuitive.
  14943. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14944. first attempt, and only move to after the tags when the cursor is already
  14945. beyond the end of the headline."
  14946. (interactive "P")
  14947. (let ((pos (point))
  14948. (special (if (consp org-special-ctrl-a/e)
  14949. (car org-special-ctrl-a/e)
  14950. org-special-ctrl-a/e))
  14951. refpos)
  14952. (beginning-of-line 1)
  14953. (if (and arg (fboundp 'move-beginning-of-line))
  14954. (call-interactively 'move-beginning-of-line)
  14955. (if (bobp)
  14956. nil
  14957. (backward-char 1)
  14958. (if (org-invisible-p)
  14959. (while (and (not (bobp)) (org-invisible-p))
  14960. (backward-char 1)
  14961. (beginning-of-line 1))
  14962. (forward-char 1))))
  14963. (when special
  14964. (cond
  14965. ((and (looking-at org-complex-heading-regexp)
  14966. (= (char-after (match-end 1)) ?\ ))
  14967. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14968. (point-at-eol)))
  14969. (goto-char
  14970. (if (eq special t)
  14971. (cond ((> pos refpos) refpos)
  14972. ((= pos (point)) refpos)
  14973. (t (point)))
  14974. (cond ((> pos (point)) (point))
  14975. ((not (eq last-command this-command)) (point))
  14976. (t refpos)))))
  14977. ((org-at-item-p)
  14978. (goto-char
  14979. (if (eq special t)
  14980. (cond ((> pos (match-end 4)) (match-end 4))
  14981. ((= pos (point)) (match-end 4))
  14982. (t (point)))
  14983. (cond ((> pos (point)) (point))
  14984. ((not (eq last-command this-command)) (point))
  14985. (t (match-end 4))))))))
  14986. (org-no-warnings
  14987. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14988. (defun org-end-of-line (&optional arg)
  14989. "Go to the end of the line.
  14990. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14991. first attempt, and only move to after the tags when the cursor is already
  14992. beyond the end of the headline."
  14993. (interactive "P")
  14994. (let ((special (if (consp org-special-ctrl-a/e)
  14995. (cdr org-special-ctrl-a/e)
  14996. org-special-ctrl-a/e)))
  14997. (if (or (not special)
  14998. (not (org-on-heading-p))
  14999. arg)
  15000. (call-interactively (if (fboundp 'move-end-of-line)
  15001. 'move-end-of-line
  15002. 'end-of-line))
  15003. (let ((pos (point)))
  15004. (beginning-of-line 1)
  15005. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  15006. (if (eq special t)
  15007. (if (or (< pos (match-beginning 1))
  15008. (= pos (match-end 0)))
  15009. (goto-char (match-beginning 1))
  15010. (goto-char (match-end 0)))
  15011. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15012. (goto-char (match-end 0))
  15013. (goto-char (match-beginning 1))))
  15014. (call-interactively (if (fboundp 'move-end-of-line)
  15015. 'move-end-of-line
  15016. 'end-of-line)))))
  15017. (org-no-warnings
  15018. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15019. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15020. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15021. (defun org-backward-sentence (&optional arg)
  15022. "Go to beginning of sentence, or beginning of table field.
  15023. This will call `backward-sentence' or `org-table-beginning-of-field',
  15024. depending on context."
  15025. (interactive "P")
  15026. (cond
  15027. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15028. (t (call-interactively 'backward-sentence))))
  15029. (defun org-forward-sentence (&optional arg)
  15030. "Go to end of sentence, or end of table field.
  15031. This will call `forward-sentence' or `org-table-end-of-field',
  15032. depending on context."
  15033. (interactive "P")
  15034. (cond
  15035. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15036. (t (call-interactively 'forward-sentence))))
  15037. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15038. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15039. (defun org-kill-line (&optional arg)
  15040. "Kill line, to tags or end of line."
  15041. (interactive "P")
  15042. (cond
  15043. ((or (not org-special-ctrl-k)
  15044. (bolp)
  15045. (not (org-on-heading-p)))
  15046. (call-interactively 'kill-line))
  15047. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15048. (kill-region (point) (match-beginning 1))
  15049. (org-set-tags nil t))
  15050. (t (kill-region (point) (point-at-eol)))))
  15051. (define-key org-mode-map "\C-k" 'org-kill-line)
  15052. (defun org-yank (&optional arg)
  15053. "Yank. If the kill is a subtree, treat it specially.
  15054. This command will look at the current kill and check if is a single
  15055. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15056. cursor is at the beginning of a line or after the stars of a currently
  15057. empty headline, then the yank is handled specially. How exactly depends
  15058. on the value of the following variables, both set by default.
  15059. org-yank-folded-subtrees
  15060. When set, the subtree(s) will be folded after insertion, but only
  15061. if doing so would now swallow text after the yanked text.
  15062. org-yank-adjusted-subtrees
  15063. When set, the subtree will be promoted or demoted in order to
  15064. fit into the local outline tree structure, which means that the level
  15065. will be adjusted so that it becomes the smaller one of the two
  15066. *visible* surrounding headings.
  15067. Any prefix to this command will cause `yank' to be called directly with
  15068. no special treatment. In particular, a simple `C-u' prefix will just
  15069. plainly yank the text as it is.
  15070. \[1] The test checks if the first non-white line is a heading
  15071. and if there are no other headings with fewer stars."
  15072. (interactive "P")
  15073. (org-yank-generic 'yank arg))
  15074. (defun org-yank-generic (command arg)
  15075. "Perform some yank-like command.
  15076. This function implements the behavior described in the `org-yank'
  15077. documentation. However, it has been generalized to work for any
  15078. interactive command with similar behavior."
  15079. ;; pretend to be command COMMAND
  15080. (setq this-command command)
  15081. (if arg
  15082. (call-interactively command)
  15083. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15084. (and (org-kill-is-subtree-p)
  15085. (or (bolp)
  15086. (and (looking-at "[ \t]*$")
  15087. (string-match
  15088. "\\`\\*+\\'"
  15089. (buffer-substring (point-at-bol) (point)))))))
  15090. swallowp)
  15091. (cond
  15092. ((and subtreep org-yank-folded-subtrees)
  15093. (let ((beg (point))
  15094. end)
  15095. (if (and subtreep org-yank-adjusted-subtrees)
  15096. (org-paste-subtree nil nil 'for-yank)
  15097. (call-interactively command))
  15098. (setq end (point))
  15099. (goto-char beg)
  15100. (when (and (bolp) subtreep
  15101. (not (setq swallowp
  15102. (org-yank-folding-would-swallow-text beg end))))
  15103. (or (looking-at outline-regexp)
  15104. (re-search-forward (concat "^" outline-regexp) end t))
  15105. (while (and (< (point) end) (looking-at outline-regexp))
  15106. (hide-subtree)
  15107. (org-cycle-show-empty-lines 'folded)
  15108. (condition-case nil
  15109. (outline-forward-same-level 1)
  15110. (error (goto-char end)))))
  15111. (when swallowp
  15112. (message
  15113. "Inserted text not folded because that would swallow text"))
  15114. (goto-char end)
  15115. (skip-chars-forward " \t\n\r")
  15116. (beginning-of-line 1)
  15117. (push-mark beg 'nomsg)))
  15118. ((and subtreep org-yank-adjusted-subtrees)
  15119. (let ((beg (point-at-bol)))
  15120. (org-paste-subtree nil nil 'for-yank)
  15121. (push-mark beg 'nomsg)))
  15122. (t
  15123. (call-interactively command))))))
  15124. (defun org-yank-folding-would-swallow-text (beg end)
  15125. "Would hide-subtree at BEG swallow any text after END?"
  15126. (let (level)
  15127. (save-excursion
  15128. (goto-char beg)
  15129. (when (or (looking-at outline-regexp)
  15130. (re-search-forward (concat "^" outline-regexp) end t))
  15131. (setq level (org-outline-level)))
  15132. (goto-char end)
  15133. (skip-chars-forward " \t\r\n\v\f")
  15134. (if (or (eobp)
  15135. (and (bolp) (looking-at org-outline-regexp)
  15136. (<= (org-outline-level) level)))
  15137. nil ; Nothing would be swallowed
  15138. t)))) ; something would swallow
  15139. (define-key org-mode-map "\C-y" 'org-yank)
  15140. (defun org-invisible-p ()
  15141. "Check if point is at a character currently not visible."
  15142. ;; Early versions of noutline don't have `outline-invisible-p'.
  15143. (if (fboundp 'outline-invisible-p)
  15144. (outline-invisible-p)
  15145. (get-char-property (point) 'invisible)))
  15146. (defun org-invisible-p2 ()
  15147. "Check if point is at a character currently not visible."
  15148. (save-excursion
  15149. (if (and (eolp) (not (bobp))) (backward-char 1))
  15150. ;; Early versions of noutline don't have `outline-invisible-p'.
  15151. (if (fboundp 'outline-invisible-p)
  15152. (outline-invisible-p)
  15153. (get-char-property (point) 'invisible))))
  15154. (defun org-back-to-heading (&optional invisible-ok)
  15155. "Call `outline-back-to-heading', but provide a better error message."
  15156. (condition-case nil
  15157. (outline-back-to-heading invisible-ok)
  15158. (error (error "Before first headline at position %d in buffer %s"
  15159. (point) (current-buffer)))))
  15160. (defun org-before-first-heading-p ()
  15161. "Before first heading?"
  15162. (save-excursion
  15163. (null (re-search-backward "^\\*+ " nil t))))
  15164. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15165. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15166. (defun org-at-heading-or-item-p ()
  15167. (or (org-on-heading-p) (org-at-item-p)))
  15168. (defun org-on-target-p ()
  15169. (or (org-in-regexp org-radio-target-regexp)
  15170. (org-in-regexp org-target-regexp)))
  15171. (defun org-up-heading-all (arg)
  15172. "Move to the heading line of which the present line is a subheading.
  15173. This function considers both visible and invisible heading lines.
  15174. With argument, move up ARG levels."
  15175. (if (fboundp 'outline-up-heading-all)
  15176. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15177. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15178. (defun org-up-heading-safe ()
  15179. "Move to the heading line of which the present line is a subheading.
  15180. This version will not throw an error. It will return the level of the
  15181. headline found, or nil if no higher level is found.
  15182. Also, this function will be a lot faster than `outline-up-heading',
  15183. because it relies on stars being the outline starters. This can really
  15184. make a significant difference in outlines with very many siblings."
  15185. (let (start-level re)
  15186. (org-back-to-heading t)
  15187. (setq start-level (funcall outline-level))
  15188. (if (equal start-level 1)
  15189. nil
  15190. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15191. (if (re-search-backward re nil t)
  15192. (funcall outline-level)))))
  15193. (defun org-first-sibling-p ()
  15194. "Is this heading the first child of its parents?"
  15195. (interactive)
  15196. (let ((re (concat "^" outline-regexp))
  15197. level l)
  15198. (unless (org-at-heading-p t)
  15199. (error "Not at a heading"))
  15200. (setq level (funcall outline-level))
  15201. (save-excursion
  15202. (if (not (re-search-backward re nil t))
  15203. t
  15204. (setq l (funcall outline-level))
  15205. (< l level)))))
  15206. (defun org-goto-sibling (&optional previous)
  15207. "Goto the next sibling, even if it is invisible.
  15208. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15209. when a sibling was found. When none is found, return nil and don't
  15210. move point."
  15211. (let ((fun (if previous 're-search-backward 're-search-forward))
  15212. (pos (point))
  15213. (re (concat "^" outline-regexp))
  15214. level l)
  15215. (when (condition-case nil (org-back-to-heading t) (error nil))
  15216. (setq level (funcall outline-level))
  15217. (catch 'exit
  15218. (or previous (forward-char 1))
  15219. (while (funcall fun re nil t)
  15220. (setq l (funcall outline-level))
  15221. (when (< l level) (goto-char pos) (throw 'exit nil))
  15222. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15223. (goto-char pos)
  15224. nil))))
  15225. (defun org-show-siblings ()
  15226. "Show all siblings of the current headline."
  15227. (save-excursion
  15228. (while (org-goto-sibling) (org-flag-heading nil)))
  15229. (save-excursion
  15230. (while (org-goto-sibling 'previous)
  15231. (org-flag-heading nil))))
  15232. (defun org-show-hidden-entry ()
  15233. "Show an entry where even the heading is hidden."
  15234. (save-excursion
  15235. (org-show-entry)))
  15236. (defun org-flag-heading (flag &optional entry)
  15237. "Flag the current heading. FLAG non-nil means make invisible.
  15238. When ENTRY is non-nil, show the entire entry."
  15239. (save-excursion
  15240. (org-back-to-heading t)
  15241. ;; Check if we should show the entire entry
  15242. (if entry
  15243. (progn
  15244. (org-show-entry)
  15245. (save-excursion
  15246. (and (outline-next-heading)
  15247. (org-flag-heading nil))))
  15248. (outline-flag-region (max (point-min) (1- (point)))
  15249. (save-excursion (outline-end-of-heading) (point))
  15250. flag))))
  15251. (defun org-forward-same-level (arg)
  15252. "Move forward to the ARG'th subheading at same level as this one.
  15253. Stop at the first and last subheadings of a superior heading.
  15254. This is like outline-forward-same-level, but invisible headings are ok."
  15255. (interactive "p")
  15256. (org-back-to-heading t)
  15257. (while (> arg 0)
  15258. (let ((point-to-move-to (save-excursion
  15259. (org-get-next-sibling))))
  15260. (if point-to-move-to
  15261. (progn
  15262. (goto-char point-to-move-to)
  15263. (setq arg (1- arg)))
  15264. (progn
  15265. (setq arg 0)
  15266. (error "No following same-level heading"))))))
  15267. (defun org-get-next-sibling ()
  15268. "Move to next heading of the same level, and return point.
  15269. If there is no such heading, return nil.
  15270. This is like outline-next-sibling, but invisible headings are ok."
  15271. (let ((level (funcall outline-level)))
  15272. (outline-next-heading)
  15273. (while (and (not (eobp)) (> (funcall outline-level) level))
  15274. (outline-next-heading))
  15275. (if (or (eobp) (< (funcall outline-level) level))
  15276. nil
  15277. (point))))
  15278. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15279. ;; This contains an exact copy of the original function, but it uses
  15280. ;; `org-back-to-heading', to make it work also in invisible
  15281. ;; trees. And is uses an invisible-OK argument.
  15282. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15283. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15284. ;; with many children and grandchildren etc, this can be much faster
  15285. ;; than the outline version.
  15286. (org-back-to-heading invisible-OK)
  15287. (let ((first t)
  15288. (level (funcall outline-level)))
  15289. (if (and (org-mode-p) (< level 1000))
  15290. ;; A true heading (not a plain list item), in Org-mode
  15291. ;; This means we can easily find the end by looking
  15292. ;; only for the right number of stars. Using a regexp to do
  15293. ;; this is so much faster than using a Lisp loop.
  15294. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15295. (forward-char 1)
  15296. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15297. ;; something else, do it the slow way
  15298. (while (and (not (eobp))
  15299. (or first (> (funcall outline-level) level)))
  15300. (setq first nil)
  15301. (outline-next-heading)))
  15302. (unless to-heading
  15303. (if (memq (preceding-char) '(?\n ?\^M))
  15304. (progn
  15305. ;; Go to end of line before heading
  15306. (forward-char -1)
  15307. (if (memq (preceding-char) '(?\n ?\^M))
  15308. ;; leave blank line before heading
  15309. (forward-char -1))))))
  15310. (point))
  15311. (defun org-show-subtree ()
  15312. "Show everything after this heading at deeper levels."
  15313. (outline-flag-region
  15314. (point)
  15315. (save-excursion
  15316. (outline-end-of-subtree) (outline-next-heading) (point))
  15317. nil))
  15318. (defun org-show-entry ()
  15319. "Show the body directly following this heading.
  15320. Show the heading too, if it is currently invisible."
  15321. (interactive)
  15322. (save-excursion
  15323. (condition-case nil
  15324. (progn
  15325. (org-back-to-heading t)
  15326. (outline-flag-region
  15327. (max (point-min) (1- (point)))
  15328. (save-excursion
  15329. (if (re-search-forward
  15330. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15331. (match-beginning 1)
  15332. (point-max)))
  15333. nil))
  15334. (error nil))))
  15335. (defun org-make-options-regexp (kwds &optional extra)
  15336. "Make a regular expression for keyword lines."
  15337. (concat
  15338. "^"
  15339. "#?[ \t]*\\+\\("
  15340. (mapconcat 'regexp-quote kwds "\\|")
  15341. (if extra (concat "\\|" extra))
  15342. "\\):[ \t]*"
  15343. "\\(.+\\)"))
  15344. ;; Make isearch reveal the necessary context
  15345. (defun org-isearch-end ()
  15346. "Reveal context after isearch exits."
  15347. (when isearch-success ; only if search was successful
  15348. (if (featurep 'xemacs)
  15349. ;; Under XEmacs, the hook is run in the correct place,
  15350. ;; we directly show the context.
  15351. (org-show-context 'isearch)
  15352. ;; In Emacs the hook runs *before* restoring the overlays.
  15353. ;; So we have to use a one-time post-command-hook to do this.
  15354. ;; (Emacs 22 has a special variable, see function `org-mode')
  15355. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15356. isearch-mode-end-hook-quit)
  15357. ;; Only when the isearch was not quitted.
  15358. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15359. 'append 'local)))))
  15360. (defun org-isearch-post-command ()
  15361. "Remove self from hook, and show context."
  15362. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15363. (org-show-context 'isearch))
  15364. ;;;; Integration with and fixes for other packages
  15365. ;;; Imenu support
  15366. (defvar org-imenu-markers nil
  15367. "All markers currently used by Imenu.")
  15368. (make-variable-buffer-local 'org-imenu-markers)
  15369. (defun org-imenu-new-marker (&optional pos)
  15370. "Return a new marker for use by Imenu, and remember the marker."
  15371. (let ((m (make-marker)))
  15372. (move-marker m (or pos (point)))
  15373. (push m org-imenu-markers)
  15374. m))
  15375. (defun org-imenu-get-tree ()
  15376. "Produce the index for Imenu."
  15377. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15378. (setq org-imenu-markers nil)
  15379. (let* ((n org-imenu-depth)
  15380. (re (concat "^" outline-regexp))
  15381. (subs (make-vector (1+ n) nil))
  15382. (last-level 0)
  15383. m level head)
  15384. (save-excursion
  15385. (save-restriction
  15386. (widen)
  15387. (goto-char (point-max))
  15388. (while (re-search-backward re nil t)
  15389. (setq level (org-reduced-level (funcall outline-level)))
  15390. (when (<= level n)
  15391. (looking-at org-complex-heading-regexp)
  15392. (setq head (org-link-display-format
  15393. (org-match-string-no-properties 4))
  15394. m (org-imenu-new-marker))
  15395. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15396. (if (>= level last-level)
  15397. (push (cons head m) (aref subs level))
  15398. (push (cons head (aref subs (1+ level))) (aref subs level))
  15399. (loop for i from (1+ level) to n do (aset subs i nil)))
  15400. (setq last-level level)))))
  15401. (aref subs 1)))
  15402. (eval-after-load "imenu"
  15403. '(progn
  15404. (add-hook 'imenu-after-jump-hook
  15405. (lambda ()
  15406. (if (eq major-mode 'org-mode)
  15407. (org-show-context 'org-goto))))))
  15408. (defun org-link-display-format (link)
  15409. "Replace a link with either the description, or the link target
  15410. if no description is present"
  15411. (save-match-data
  15412. (if (string-match org-bracket-link-analytic-regexp link)
  15413. (replace-match (or (match-string 5 link)
  15414. (concat (match-string 1 link)
  15415. (match-string 3 link)))
  15416. nil nil link)
  15417. link)))
  15418. ;; Speedbar support
  15419. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15420. "Overlay marking the agenda restriction line in speedbar.")
  15421. (org-overlay-put org-speedbar-restriction-lock-overlay
  15422. 'face 'org-agenda-restriction-lock)
  15423. (org-overlay-put org-speedbar-restriction-lock-overlay
  15424. 'help-echo "Agendas are currently limited to this item.")
  15425. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15426. (defun org-speedbar-set-agenda-restriction ()
  15427. "Restrict future agenda commands to the location at point in speedbar.
  15428. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15429. (interactive)
  15430. (require 'org-agenda)
  15431. (let (p m tp np dir txt)
  15432. (cond
  15433. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15434. 'org-imenu t))
  15435. (setq m (get-text-property p 'org-imenu-marker))
  15436. (save-excursion
  15437. (save-restriction
  15438. (set-buffer (marker-buffer m))
  15439. (goto-char m)
  15440. (org-agenda-set-restriction-lock 'subtree))))
  15441. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15442. 'speedbar-function 'speedbar-find-file))
  15443. (setq tp (previous-single-property-change
  15444. (1+ p) 'speedbar-function)
  15445. np (next-single-property-change
  15446. tp 'speedbar-function)
  15447. dir (speedbar-line-directory)
  15448. txt (buffer-substring-no-properties (or tp (point-min))
  15449. (or np (point-max))))
  15450. (save-excursion
  15451. (save-restriction
  15452. (set-buffer (find-file-noselect
  15453. (let ((default-directory dir))
  15454. (expand-file-name txt))))
  15455. (unless (org-mode-p)
  15456. (error "Cannot restrict to non-Org-mode file"))
  15457. (org-agenda-set-restriction-lock 'file))))
  15458. (t (error "Don't know how to restrict Org-mode's agenda")))
  15459. (org-move-overlay org-speedbar-restriction-lock-overlay
  15460. (point-at-bol) (point-at-eol))
  15461. (setq current-prefix-arg nil)
  15462. (org-agenda-maybe-redo)))
  15463. (eval-after-load "speedbar"
  15464. '(progn
  15465. (speedbar-add-supported-extension ".org")
  15466. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15467. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15468. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15469. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15470. (add-hook 'speedbar-visiting-tag-hook
  15471. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15472. ;;; Fixes and Hacks for problems with other packages
  15473. ;; Make flyspell not check words in links, to not mess up our keymap
  15474. (defun org-mode-flyspell-verify ()
  15475. "Don't let flyspell put overlays at active buttons."
  15476. (not (get-text-property (point) 'keymap)))
  15477. (defun org-remove-flyspell-overlays-in (beg end)
  15478. "Remove flyspell overlays in region."
  15479. (and (org-bound-and-true-p flyspell-mode)
  15480. (fboundp 'flyspell-delete-region-overlays)
  15481. (flyspell-delete-region-overlays beg end)))
  15482. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15483. (eval-after-load "bookmark"
  15484. '(if (boundp 'bookmark-after-jump-hook)
  15485. ;; We can use the hook
  15486. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15487. ;; Hook not available, use advice
  15488. (defadvice bookmark-jump (after org-make-visible activate)
  15489. "Make the position visible."
  15490. (org-bookmark-jump-unhide))))
  15491. ;; Make sure saveplace show the location if it was hidden
  15492. (eval-after-load "saveplace"
  15493. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15494. "Make the position visible."
  15495. (org-bookmark-jump-unhide)))
  15496. (defun org-bookmark-jump-unhide ()
  15497. "Unhide the current position, to show the bookmark location."
  15498. (and (org-mode-p)
  15499. (or (org-invisible-p)
  15500. (save-excursion (goto-char (max (point-min) (1- (point))))
  15501. (org-invisible-p)))
  15502. (org-show-context 'bookmark-jump)))
  15503. ;; Make session.el ignore our circular variable
  15504. (eval-after-load "session"
  15505. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15506. ;;;; Experimental code
  15507. (defun org-closed-in-range ()
  15508. "Sparse tree of items closed in a certain time range.
  15509. Still experimental, may disappear in the future."
  15510. (interactive)
  15511. ;; Get the time interval from the user.
  15512. (let* ((time1 (time-to-seconds
  15513. (org-read-date nil 'to-time nil "Starting date: ")))
  15514. (time2 (time-to-seconds
  15515. (org-read-date nil 'to-time nil "End date:")))
  15516. ;; callback function
  15517. (callback (lambda ()
  15518. (let ((time
  15519. (time-to-seconds
  15520. (apply 'encode-time
  15521. (org-parse-time-string
  15522. (match-string 1))))))
  15523. ;; check if time in interval
  15524. (and (>= time time1) (<= time time2))))))
  15525. ;; make tree, check each match with the callback
  15526. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15527. ;;;; Finish up
  15528. (provide 'org)
  15529. (run-hooks 'org-load-hook)
  15530. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15531. ;;; org.el ends here