org.el 606 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301
  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.25trans
  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.25trans"
  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 mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  185. (defcustom org-support-shift-select nil
  186. "Non-nil means, make shift-cursor commands select text when possible.
  187. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  188. selecting a region, or enlarge thusly regions started in this way.
  189. In Org-mode, in special contexts, these same keys are used for other
  190. purposes, important enough to compete with shift selection. Org tries
  191. to balance these needs by supporting `shift-select-mode' outside these
  192. special contexts, under control of this variable.
  193. The default of this variable is nil, to avoid confusing behavior. Shifted
  194. cursor keys will then execute Org commands in the following contexts:
  195. - on a headline, changing TODO state (left/right) and priority (up/down)
  196. - on a time stamp, changing the time
  197. - in a plain list item, changing the bullet type
  198. - in a property definition line, switching between allowed values
  199. - in the BEGIN line of a clock table (changing the time block).
  200. Outside these contexts, the commands will throw an error.
  201. When this variable is t and the cursor is not in a special context,
  202. Org-mode will support shift-selection for making and enlarging regions.
  203. To make this more effective, the bullet cycling will no longer happen
  204. anywhere in an item line, but only if the cursor is exactly on the bullet.
  205. If you set this variable to the symbol `always', then the keys
  206. will not be special in headlines, property lines, and item lines, to make
  207. shift selection work there as well. If this is what you want, you can
  208. use the following alternative commands: `C-c C-t' and `C-c ,' to
  209. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  210. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  211. edited by hand or in column view.
  212. However, when the cursor is on a timestamp, shift-cursor commands
  213. will still edit the time stamp - this is just too good to give up.
  214. XEmacs user should have this variable set to nil, because shift-select-mode
  215. is Emacs 23 only."
  216. :group 'org
  217. :type '(choice
  218. (const :tag "Never" nil)
  219. (const :tag "When outside special context" t)
  220. (const :tag "Everywhere except timestamps" always)))
  221. (defgroup org-startup nil
  222. "Options concerning startup of Org-mode."
  223. :tag "Org Startup"
  224. :group 'org)
  225. (defcustom org-startup-folded t
  226. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  227. This can also be configured on a per-file basis by adding one of
  228. the following lines anywhere in the buffer:
  229. #+STARTUP: fold
  230. #+STARTUP: nofold
  231. #+STARTUP: content"
  232. :group 'org-startup
  233. :type '(choice
  234. (const :tag "nofold: show all" nil)
  235. (const :tag "fold: overview" t)
  236. (const :tag "content: all headlines" content)))
  237. (defcustom org-startup-truncated t
  238. "Non-nil means, entering Org-mode will set `truncate-lines'.
  239. This is useful since some lines containing links can be very long and
  240. uninteresting. Also tables look terrible when wrapped."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-startup-align-all-tables nil
  244. "Non-nil means, align all tables when visiting a file.
  245. This is useful when the column width in tables is forced with <N> cookies
  246. in table fields. Such tables will look correct only after the first re-align.
  247. This can also be configured on a per-file basis by adding one of
  248. the following lines anywhere in the buffer:
  249. #+STARTUP: align
  250. #+STARTUP: noalign"
  251. :group 'org-startup
  252. :type 'boolean)
  253. (defcustom org-insert-mode-line-in-empty-file nil
  254. "Non-nil means insert the first line setting Org-mode in empty files.
  255. When the function `org-mode' is called interactively in an empty file, this
  256. normally means that the file name does not automatically trigger Org-mode.
  257. To ensure that the file will always be in Org-mode in the future, a
  258. line enforcing Org-mode will be inserted into the buffer, if this option
  259. has been set."
  260. :group 'org-startup
  261. :type 'boolean)
  262. (defcustom org-replace-disputed-keys nil
  263. "Non-nil means use alternative key bindings for some keys.
  264. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  265. These keys are also used by other packages like shift-selection-mode'
  266. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  267. If you want to use Org-mode together with one of these other modes,
  268. or more generally if you would like to move some Org-mode commands to
  269. other keys, set this variable and configure the keys with the variable
  270. `org-disputed-keys'.
  271. This option is only relevant at load-time of Org-mode, and must be set
  272. *before* org.el is loaded. Changing it requires a restart of Emacs to
  273. become effective."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-use-extra-keys nil
  277. "Non-nil means use extra key sequence definitions for certain
  278. commands. This happens automatically if you run XEmacs or if
  279. window-system is nil. This variable lets you do the same
  280. manually. You must set it before loading org.
  281. Example: on Carbon Emacs 22 running graphically, with an external
  282. keyboard on a Powerbook, the default way of setting M-left might
  283. not work for either Alt or ESC. Setting this variable will make
  284. it work for ESC."
  285. :group 'org-startup
  286. :type 'boolean)
  287. (if (fboundp 'defvaralias)
  288. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  289. (defcustom org-disputed-keys
  290. '(([(shift up)] . [(meta p)])
  291. ([(shift down)] . [(meta n)])
  292. ([(shift left)] . [(meta -)])
  293. ([(shift right)] . [(meta +)])
  294. ([(control shift right)] . [(meta shift +)])
  295. ([(control shift left)] . [(meta shift -)]))
  296. "Keys for which Org-mode and other modes compete.
  297. This is an alist, cars are the default keys, second element specifies
  298. the alternative to use when `org-replace-disputed-keys' is t.
  299. Keys can be specified in any syntax supported by `define-key'.
  300. The value of this option takes effect only at Org-mode's startup,
  301. therefore you'll have to restart Emacs to apply it after changing."
  302. :group 'org-startup
  303. :type 'alist)
  304. (defun org-key (key)
  305. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  306. Or return the original if not disputed."
  307. (if org-replace-disputed-keys
  308. (let* ((nkey (key-description key))
  309. (x (org-find-if (lambda (x)
  310. (equal (key-description (car x)) nkey))
  311. org-disputed-keys)))
  312. (if x (cdr x) key))
  313. key))
  314. (defun org-find-if (predicate seq)
  315. (catch 'exit
  316. (while seq
  317. (if (funcall predicate (car seq))
  318. (throw 'exit (car seq))
  319. (pop seq)))))
  320. (defun org-defkey (keymap key def)
  321. "Define a key, possibly translated, as returned by `org-key'."
  322. (define-key keymap (org-key key) def))
  323. (defcustom org-ellipsis nil
  324. "The ellipsis to use in the Org-mode outline.
  325. When nil, just use the standard three dots. When a string, use that instead,
  326. When a face, use the standard 3 dots, but with the specified face.
  327. The change affects only Org-mode (which will then use its own display table).
  328. Changing this requires executing `M-x org-mode' in a buffer to become
  329. effective."
  330. :group 'org-startup
  331. :type '(choice (const :tag "Default" nil)
  332. (face :tag "Face" :value org-warning)
  333. (string :tag "String" :value "...#")))
  334. (defvar org-display-table nil
  335. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  336. (defgroup org-keywords nil
  337. "Keywords in Org-mode."
  338. :tag "Org Keywords"
  339. :group 'org)
  340. (defcustom org-deadline-string "DEADLINE:"
  341. "String to mark deadline entries.
  342. A deadline is this string, followed by a time stamp. Should be a word,
  343. terminated by a colon. You can insert a schedule keyword and
  344. a timestamp with \\[org-deadline].
  345. Changes become only effective after restarting Emacs."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defcustom org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].
  353. Changes become only effective after restarting Emacs."
  354. :group 'org-keywords
  355. :type 'string)
  356. (defcustom org-closed-string "CLOSED:"
  357. "String used as the prefix for timestamps logging closing a TODO entry."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-clock-string "CLOCK:"
  361. "String used as prefix for timestamps clocking work hours on an item."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-comment-string "COMMENT"
  365. "Entries starting with this keyword will never be exported.
  366. An entry can be toggled between COMMENT and normal with
  367. \\[org-toggle-comment].
  368. Changes become only effective after restarting Emacs."
  369. :group 'org-keywords
  370. :type 'string)
  371. (defcustom org-quote-string "QUOTE"
  372. "Entries starting with this keyword will be exported in fixed-width font.
  373. Quoting applies only to the text in the entry following the headline, and does
  374. not extend beyond the next headline, even if that is lower level.
  375. An entry can be toggled between QUOTE and normal with
  376. \\[org-toggle-fixed-width-section]."
  377. :group 'org-keywords
  378. :type 'string)
  379. (defconst org-repeat-re
  380. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  381. "Regular expression for specifying repeated events.
  382. After a match, group 1 contains the repeat expression.")
  383. (defgroup org-structure nil
  384. "Options concerning the general structure of Org-mode files."
  385. :tag "Org Structure"
  386. :group 'org)
  387. (defgroup org-reveal-location nil
  388. "Options about how to make context of a location visible."
  389. :tag "Org Reveal Location"
  390. :group 'org-structure)
  391. (defconst org-context-choice
  392. '(choice
  393. (const :tag "Always" t)
  394. (const :tag "Never" nil)
  395. (repeat :greedy t :tag "Individual contexts"
  396. (cons
  397. (choice :tag "Context"
  398. (const agenda)
  399. (const org-goto)
  400. (const occur-tree)
  401. (const tags-tree)
  402. (const link-search)
  403. (const mark-goto)
  404. (const bookmark-jump)
  405. (const isearch)
  406. (const default))
  407. (boolean))))
  408. "Contexts for the reveal options.")
  409. (defcustom org-show-hierarchy-above '((default . t))
  410. "Non-nil means, show full hierarchy when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the hierarchy of headings
  413. above the exposed location is shown.
  414. Turning this off for example for sparse trees makes them very compact.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. Valid contexts are
  417. agenda when exposing an entry from the agenda
  418. org-goto when using the command `org-goto' on key C-c C-j
  419. occur-tree when using the command `org-occur' on key C-c /
  420. tags-tree when constructing a sparse tree based on tags matches
  421. link-search when exposing search matches associated with a link
  422. mark-goto when exposing the jump goal of a mark
  423. bookmark-jump when exposing a bookmark location
  424. isearch when exiting from an incremental search
  425. default default for all contexts not set explicitly"
  426. :group 'org-reveal-location
  427. :type org-context-choice)
  428. (defcustom org-show-following-heading '((default . nil))
  429. "Non-nil means, show following heading when revealing a location.
  430. Org-mode often shows locations in an org-mode file which might have
  431. been invisible before. When this is set, the heading following the
  432. match is shown.
  433. Turning this off for example for sparse trees makes them very compact,
  434. but makes it harder to edit the location of the match. In such a case,
  435. use the command \\[org-reveal] to show more context.
  436. Instead of t, this can also be an alist specifying this option for different
  437. contexts. See `org-show-hierarchy-above' for valid contexts."
  438. :group 'org-reveal-location
  439. :type org-context-choice)
  440. (defcustom org-show-siblings '((default . nil) (isearch t))
  441. "Non-nil means, show all sibling heading when revealing a location.
  442. Org-mode often shows locations in an org-mode file which might have
  443. been invisible before. When this is set, the sibling of the current entry
  444. heading are all made visible. If `org-show-hierarchy-above' is t,
  445. the same happens on each level of the hierarchy above the current entry.
  446. By default this is on for the isearch context, off for all other contexts.
  447. Turning this off for example for sparse trees makes them very compact,
  448. but makes it harder to edit the location of the match. In such a case,
  449. use the command \\[org-reveal] to show more context.
  450. Instead of t, this can also be an alist specifying this option for different
  451. contexts. See `org-show-hierarchy-above' for valid contexts."
  452. :group 'org-reveal-location
  453. :type org-context-choice)
  454. (defcustom org-show-entry-below '((default . nil))
  455. "Non-nil means, show the entry below a headline when revealing a location.
  456. Org-mode often shows locations in an org-mode file which might have
  457. been invisible before. When this is set, the text below the headline that is
  458. exposed is also shown.
  459. By default this is off for all contexts.
  460. Instead of t, this can also be an alist specifying this option for different
  461. contexts. See `org-show-hierarchy-above' for valid contexts."
  462. :group 'org-reveal-location
  463. :type org-context-choice)
  464. (defcustom org-indirect-buffer-display 'other-window
  465. "How should indirect tree buffers be displayed?
  466. This applies to indirect buffers created with the commands
  467. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  468. Valid values are:
  469. current-window Display in the current window
  470. other-window Just display in another window.
  471. dedicated-frame Create one new frame, and re-use it each time.
  472. new-frame Make a new frame each time. Note that in this case
  473. previously-made indirect buffers are kept, and you need to
  474. kill these buffers yourself."
  475. :group 'org-structure
  476. :group 'org-agenda-windows
  477. :type '(choice
  478. (const :tag "In current window" current-window)
  479. (const :tag "In current frame, other window" other-window)
  480. (const :tag "Each time a new frame" new-frame)
  481. (const :tag "One dedicated frame" dedicated-frame)))
  482. (defgroup org-cycle nil
  483. "Options concerning visibility cycling in Org-mode."
  484. :tag "Org Cycle"
  485. :group 'org-structure)
  486. (defcustom org-cycle-max-level nil
  487. "Maximum level which should still be subject to visibility cycling.
  488. Levels higher than this will, for cycling, be treated as text, not a headline.
  489. When `org-odd-levels-only' is set, a value of N in this variable actually
  490. means 2N-1 stars as the limiting headline.
  491. When nil, cycle all levels.
  492. Note that the limiting level of cycling is also influenced by
  493. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  494. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  495. than its value."
  496. :group 'org-cycle
  497. :type '(choice
  498. (const :tag "No limit" nil)
  499. (integer :tag "Maximum level")))
  500. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  501. "Names of drawers. Drawers are not opened by cycling on the headline above.
  502. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  503. this:
  504. :DRAWERNAME:
  505. .....
  506. :END:
  507. The drawer \"PROPERTIES\" is special for capturing properties through
  508. the property API.
  509. Drawers can be defined on the per-file basis with a line like:
  510. #+DRAWERS: HIDDEN STATE PROPERTIES"
  511. :group 'org-structure
  512. :group 'org-cycle
  513. :type '(repeat (string :tag "Drawer Name")))
  514. (defcustom org-cycle-global-at-bob nil
  515. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  516. This makes it possible to do global cycling without having to use S-TAB or
  517. C-u TAB. For this special case to work, the first line of the buffer
  518. must not be a headline - it may be empty or some other text. When used in
  519. this way, `org-cycle-hook' is disables temporarily, to make sure the
  520. cursor stays at the beginning of the buffer.
  521. When this option is nil, don't do anything special at the beginning
  522. of the buffer."
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-emulate-tab t
  526. "Where should `org-cycle' emulate TAB.
  527. nil Never
  528. white Only in completely white lines
  529. whitestart Only at the beginning of lines, before the first non-white char
  530. t Everywhere except in headlines
  531. exc-hl-bol Everywhere except at the start of a headline
  532. If TAB is used in a place where it does not emulate TAB, the current subtree
  533. visibility is cycled."
  534. :group 'org-cycle
  535. :type '(choice (const :tag "Never" nil)
  536. (const :tag "Only in completely white lines" white)
  537. (const :tag "Before first char in a line" whitestart)
  538. (const :tag "Everywhere except in headlines" t)
  539. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  540. ))
  541. (defcustom org-cycle-separator-lines 2
  542. "Number of empty lines needed to keep an empty line between collapsed trees.
  543. If you leave an empty line between the end of a subtree and the following
  544. headline, this empty line is hidden when the subtree is folded.
  545. Org-mode will leave (exactly) one empty line visible if the number of
  546. empty lines is equal or larger to the number given in this variable.
  547. So the default 2 means, at least 2 empty lines after the end of a subtree
  548. are needed to produce free space between a collapsed subtree and the
  549. following headline.
  550. Special case: when 0, never leave empty lines in collapsed view."
  551. :group 'org-cycle
  552. :type 'integer)
  553. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  554. (defcustom org-pre-cycle-hook nil
  555. "Hook that is run before visibility cycling is happening.
  556. The function(s) in this hook must accept a single argument which indicates
  557. the new state that will be set right after running this hook. The
  558. argument is a symbol. Before a global state change, it can have the values
  559. `overview', `content', or `all'. Before a local state change, it can have
  560. the values `folded', `children', or `subtree'."
  561. :group 'org-cycle
  562. :type 'hook)
  563. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  564. org-cycle-hide-drawers
  565. org-cycle-show-empty-lines
  566. org-optimize-window-after-visibility-change)
  567. "Hook that is run after `org-cycle' has changed the buffer visibility.
  568. The function(s) in this hook must accept a single argument which indicates
  569. the new state that was set by the most recent `org-cycle' command. The
  570. argument is a symbol. After a global state change, it can have the values
  571. `overview', `content', or `all'. After a local state change, it can have
  572. the values `folded', `children', or `subtree'."
  573. :group 'org-cycle
  574. :type 'hook)
  575. (defgroup org-edit-structure nil
  576. "Options concerning structure editing in Org-mode."
  577. :tag "Org Edit Structure"
  578. :group 'org-structure)
  579. (defcustom org-odd-levels-only nil
  580. "Non-nil means, skip even levels and only use odd levels for the outline.
  581. This has the effect that two stars are being added/taken away in
  582. promotion/demotion commands. It also influences how levels are
  583. handled by the exporters.
  584. Changing it requires restart of `font-lock-mode' to become effective
  585. for fontification also in regions already fontified.
  586. You may also set this on a per-file basis by adding one of the following
  587. lines to the buffer:
  588. #+STARTUP: odd
  589. #+STARTUP: oddeven"
  590. :group 'org-edit-structure
  591. :group 'org-font-lock
  592. :type 'boolean)
  593. (defcustom org-adapt-indentation t
  594. "Non-nil means, adapt indentation when promoting and demoting.
  595. When this is set and the *entire* text in an entry is indented, the
  596. indentation is increased by one space in a demotion command, and
  597. decreased by one in a promotion command. If any line in the entry
  598. body starts at column 0, indentation is not changed at all."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-special-ctrl-a/e nil
  602. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  603. When t, `C-a' will bring back the cursor to the beginning of the
  604. headline text, i.e. after the stars and after a possible TODO keyword.
  605. In an item, this will be the position after the bullet.
  606. When the cursor is already at that position, another `C-a' will bring
  607. it to the beginning of the line.
  608. `C-e' will jump to the end of the headline, ignoring the presence of tags
  609. in the headline. A second `C-e' will then jump to the true end of the
  610. line, after any tags.
  611. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  612. and only a directly following, identical keypress will bring the cursor
  613. to the special positions.
  614. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  615. set separately."
  616. :group 'org-edit-structure
  617. :type '(choice
  618. (const :tag "off" nil)
  619. (const :tag "after stars/bullet and before tags first" t)
  620. (const :tag "true line boundary first" reversed)
  621. (cons :tag "Set C-a and C-e separately"
  622. (choice :tag "Special C-a"
  623. (const :tag "off" nil)
  624. (const :tag "after stars/bullet first" t)
  625. (const :tag "before stars/bullet first" reversed))
  626. (choice :tag "Special C-e"
  627. (const :tag "off" nil)
  628. (const :tag "before tags first" t)
  629. (const :tag "after tags first" reversed)))))
  630. (if (fboundp 'defvaralias)
  631. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  632. (defcustom org-special-ctrl-k nil
  633. "Non-nil means `C-k' will behave specially in headlines.
  634. When nil, `C-k' will call the default `kill-line' command.
  635. When t, the following will happen while the cursor is in the headline:
  636. - When the cursor is at the beginning of a headline, kill the entire
  637. line and possible the folded subtree below the line.
  638. - When in the middle of the headline text, kill the headline up to the tags.
  639. - When after the headline text, kill the tags."
  640. :group 'org-edit-structure
  641. :type 'boolean)
  642. (defcustom org-yank-folded-subtrees t
  643. "Non-nil means, when yanking subtrees, fold them.
  644. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  645. it starts with a heading and all other headings in it are either children
  646. or siblings, then fold all the subtrees. However, do this only if no
  647. text after the yank would be swallowed into a folded tree by this action."
  648. :group 'org-edit-structure
  649. :type 'boolean)
  650. (defcustom org-yank-adjusted-subtrees nil
  651. "Non-nil means, when yanking subtrees, adjust the level.
  652. With this setting, `org-paste-subtree' is used to insert the subtree, see
  653. this function for details."
  654. :group 'org-edit-structure
  655. :type 'boolean)
  656. (defcustom org-M-RET-may-split-line '((default . t))
  657. "Non-nil means, M-RET will split the line at the cursor position.
  658. When nil, it will go to the end of the line before making a
  659. new line.
  660. You may also set this option in a different way for different
  661. contexts. Valid contexts are:
  662. headline when creating a new headline
  663. item when creating a new item
  664. table in a table field
  665. default the value to be used for all contexts not explicitly
  666. customized"
  667. :group 'org-structure
  668. :group 'org-table
  669. :type '(choice
  670. (const :tag "Always" t)
  671. (const :tag "Never" nil)
  672. (repeat :greedy t :tag "Individual contexts"
  673. (cons
  674. (choice :tag "Context"
  675. (const headline)
  676. (const item)
  677. (const table)
  678. (const default))
  679. (boolean)))))
  680. (defcustom org-insert-heading-respect-content nil
  681. "Non-nil means, insert new headings after the current subtree.
  682. When nil, the new heading is created directly after the current line.
  683. The commands \\[org-insert-heading-respect-content] and
  684. \\[org-insert-todo-heading-respect-content] turn this variable on
  685. for the duration of the command."
  686. :group 'org-structure
  687. :type 'boolean)
  688. (defcustom org-blank-before-new-entry '((heading . auto)
  689. (plain-list-item . auto))
  690. "Should `org-insert-heading' leave a blank line before new heading/item?
  691. The value is an alist, with `heading' and `plain-list-item' as car,
  692. and a boolean flag as cdr. For plain lists, if the variable
  693. `org-empty-line-terminates-plain-lists' is set, the setting here
  694. is ignored and no empty line is inserted, to keep the list in tact."
  695. :group 'org-edit-structure
  696. :type '(list
  697. (cons (const heading)
  698. (choice (const :tag "Never" nil)
  699. (const :tag "Always" t)
  700. (const :tag "Auto" auto)))
  701. (cons (const plain-list-item)
  702. (choice (const :tag "Never" nil)
  703. (const :tag "Always" t)
  704. (const :tag "Auto" auto)))))
  705. (defcustom org-insert-heading-hook nil
  706. "Hook being run after inserting a new heading."
  707. :group 'org-edit-structure
  708. :type 'hook)
  709. (defcustom org-enable-fixed-width-editor t
  710. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  711. This currently only means, they are never auto-wrapped.
  712. When nil, such lines will be treated like ordinary lines.
  713. See also the QUOTE keyword."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defcustom org-edit-src-region-extra nil
  717. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  718. For examples see the function `org-edit-src-find-region-and-lang'.
  719. The regular expression identifying the begin marker should end with a newline,
  720. and the regexp marking the end line should start with a newline, to make sure
  721. there are kept outside the narrowed region."
  722. :group 'org-edit-structure
  723. :type '(repeat
  724. (list
  725. (regexp :tag "begin regexp")
  726. (regexp :tag "end regexp")
  727. (choice :tag "language"
  728. (string :tag "specify")
  729. (integer :tag "from match group")
  730. (const :tag "from `lang' element")
  731. (const :tag "from `style' element")))))
  732. (defcustom org-coderef-label-format "(ref:%s)"
  733. "The default coderef format.
  734. This format string will be used to search for coderef labels in literal
  735. examples (EXAMPLE and SRC blocks). The format can be overwritten
  736. an individual literal example with the -f option, like
  737. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  738. ...
  739. #+END_SRC
  740. If you want to use this for HTML export, make sure that the format does
  741. not introduce special font-locking, and avoid the HTML special
  742. characters `<', `>', and `&'. The reason for this restriction is that
  743. the labels are searched for only after htmlize has done its job."
  744. :group 'org-edit-structure ; FIXME this is not in the right group
  745. :type 'string)
  746. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  747. "The mode that should be used to edit fixed-width regions.
  748. These are the regions where each line starts with a colon."
  749. :group 'org-edit-structure
  750. :type '(choice
  751. (const artist-mode)
  752. (const picture-mode)
  753. (const fundamental-mode)
  754. (function :tag "Other (specify)")))
  755. (defcustom org-goto-auto-isearch t
  756. "Non-nil means, typing characters in org-goto starts incremental search."
  757. :group 'org-edit-structure
  758. :type 'boolean)
  759. (defgroup org-sparse-trees nil
  760. "Options concerning sparse trees in Org-mode."
  761. :tag "Org Sparse Trees"
  762. :group 'org-structure)
  763. (defcustom org-highlight-sparse-tree-matches t
  764. "Non-nil means, highlight all matches that define a sparse tree.
  765. The highlights will automatically disappear the next time the buffer is
  766. changed by an edit command."
  767. :group 'org-sparse-trees
  768. :type 'boolean)
  769. (defcustom org-remove-highlights-with-change t
  770. "Non-nil means, any change to the buffer will remove temporary highlights.
  771. Such highlights are created by `org-occur' and `org-clock-display'.
  772. When nil, `C-c C-c needs to be used to get rid of the highlights.
  773. The highlights created by `org-preview-latex-fragment' always need
  774. `C-c C-c' to be removed."
  775. :group 'org-sparse-trees
  776. :group 'org-time
  777. :type 'boolean)
  778. (defcustom org-occur-hook '(org-first-headline-recenter)
  779. "Hook that is run after `org-occur' has constructed a sparse tree.
  780. This can be used to recenter the window to show as much of the structure
  781. as possible."
  782. :group 'org-sparse-trees
  783. :type 'hook)
  784. (defgroup org-imenu-and-speedbar nil
  785. "Options concerning imenu and speedbar in Org-mode."
  786. :tag "Org Imenu and Speedbar"
  787. :group 'org-structure)
  788. (defcustom org-imenu-depth 2
  789. "The maximum level for Imenu access to Org-mode headlines.
  790. This also applied for speedbar access."
  791. :group 'org-imenu-and-speedbar
  792. :type 'integer)
  793. (defgroup org-table nil
  794. "Options concerning tables in Org-mode."
  795. :tag "Org Table"
  796. :group 'org)
  797. (defcustom org-enable-table-editor 'optimized
  798. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  799. When nil, such lines will be treated like ordinary lines.
  800. When equal to the symbol `optimized', the table editor will be optimized to
  801. do the following:
  802. - Automatic overwrite mode in front of whitespace in table fields.
  803. This makes the structure of the table stay in tact as long as the edited
  804. field does not exceed the column width.
  805. - Minimize the number of realigns. Normally, the table is aligned each time
  806. TAB or RET are pressed to move to another field. With optimization this
  807. happens only if changes to a field might have changed the column width.
  808. Optimization requires replacing the functions `self-insert-command',
  809. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  810. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  811. very good at guessing when a re-align will be necessary, but you can always
  812. force one with \\[org-ctrl-c-ctrl-c].
  813. If you would like to use the optimized version in Org-mode, but the
  814. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  815. This variable can be used to turn on and off the table editor during a session,
  816. but in order to toggle optimization, a restart is required.
  817. See also the variable `org-table-auto-blank-field'."
  818. :group 'org-table
  819. :type '(choice
  820. (const :tag "off" nil)
  821. (const :tag "on" t)
  822. (const :tag "on, optimized" optimized)))
  823. (defcustom org-self-insert-cluster-for-undo t
  824. "Non-nil means cluster self-insert commands for undo when possible.
  825. If this is set, then, like in the Emacs command loop, 20 consequtive
  826. characters will be undone together.
  827. This is configurable, because there is some impact on typing performance."
  828. :group 'org-table
  829. :type 'boolean)
  830. (defcustom org-table-tab-recognizes-table.el t
  831. "Non-nil means, TAB will automatically notice a table.el table.
  832. When it sees such a table, it moves point into it and - if necessary -
  833. calls `table-recognize-table'."
  834. :group 'org-table-editing
  835. :type 'boolean)
  836. (defgroup org-link nil
  837. "Options concerning links in Org-mode."
  838. :tag "Org Link"
  839. :group 'org)
  840. (defvar org-link-abbrev-alist-local nil
  841. "Buffer-local version of `org-link-abbrev-alist', which see.
  842. The value of this is taken from the #+LINK lines.")
  843. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  844. (defcustom org-link-abbrev-alist nil
  845. "Alist of link abbreviations.
  846. The car of each element is a string, to be replaced at the start of a link.
  847. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  848. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  849. [[linkkey:tag][description]]
  850. The 'linkkey' must be a word word, starting with a letter, followed
  851. by letters, numbers, '-' or '_'.
  852. If REPLACE is a string, the tag will simply be appended to create the link.
  853. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  854. the placeholder \"%h\" will cause a url-encoded version of the tag to
  855. be inserted at that point (see the function `url-hexify-string').
  856. REPLACE may also be a function that will be called with the tag as the
  857. only argument to create the link, which should be returned as a string.
  858. See the manual for examples."
  859. :group 'org-link
  860. :type '(repeat
  861. (cons
  862. (string :tag "Protocol")
  863. (choice
  864. (string :tag "Format")
  865. (function)))))
  866. (defcustom org-descriptive-links t
  867. "Non-nil means, hide link part and only show description of bracket links.
  868. Bracket links are like [[link][description]]. This variable sets the initial
  869. state in new org-mode buffers. The setting can then be toggled on a
  870. per-buffer basis from the Org->Hyperlinks menu."
  871. :group 'org-link
  872. :type 'boolean)
  873. (defcustom org-link-file-path-type 'adaptive
  874. "How the path name in file links should be stored.
  875. Valid values are:
  876. relative Relative to the current directory, i.e. the directory of the file
  877. into which the link is being inserted.
  878. absolute Absolute path, if possible with ~ for home directory.
  879. noabbrev Absolute path, no abbreviation of home directory.
  880. adaptive Use relative path for files in the current directory and sub-
  881. directories of it. For other files, use an absolute path."
  882. :group 'org-link
  883. :type '(choice
  884. (const relative)
  885. (const absolute)
  886. (const noabbrev)
  887. (const adaptive)))
  888. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  889. "Types of links that should be activated in Org-mode files.
  890. This is a list of symbols, each leading to the activation of a certain link
  891. type. In principle, it does not hurt to turn on most link types - there may
  892. be a small gain when turning off unused link types. The types are:
  893. bracket The recommended [[link][description]] or [[link]] links with hiding.
  894. angular Links in angular brackets that may contain whitespace like
  895. <bbdb:Carsten Dominik>.
  896. plain Plain links in normal text, no whitespace, like http://google.com.
  897. radio Text that is matched by a radio target, see manual for details.
  898. tag Tag settings in a headline (link to tag search).
  899. date Time stamps (link to calendar).
  900. footnote Footnote labels.
  901. Changing this variable requires a restart of Emacs to become effective."
  902. :group 'org-link
  903. :type '(set :greedy t
  904. (const :tag "Double bracket links (new style)" bracket)
  905. (const :tag "Angular bracket links (old style)" angular)
  906. (const :tag "Plain text links" plain)
  907. (const :tag "Radio target matches" radio)
  908. (const :tag "Tags" tag)
  909. (const :tag "Timestamps" date)
  910. (const :tag "Footnotes" footnote)))
  911. (defcustom org-make-link-description-function nil
  912. "Function to use to generate link descriptions from links. If
  913. nil the link location will be used. This function must take two
  914. parameters; the first is the link and the second the description
  915. org-insert-link has generated, and should return the description
  916. to use."
  917. :group 'org-link
  918. :type 'function)
  919. (defgroup org-link-store nil
  920. "Options concerning storing links in Org-mode."
  921. :tag "Org Store Link"
  922. :group 'org-link)
  923. (defcustom org-email-link-description-format "Email %c: %.30s"
  924. "Format of the description part of a link to an email or usenet message.
  925. The following %-escapes will be replaced by corresponding information:
  926. %F full \"From\" field
  927. %f name, taken from \"From\" field, address if no name
  928. %T full \"To\" field
  929. %t first name in \"To\" field, address if no name
  930. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  931. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  932. %s subject
  933. %m message-id.
  934. You may use normal field width specification between the % and the letter.
  935. This is for example useful to limit the length of the subject.
  936. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  937. :group 'org-link-store
  938. :type 'string)
  939. (defcustom org-from-is-user-regexp
  940. (let (r1 r2)
  941. (when (and user-mail-address (not (string= user-mail-address "")))
  942. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  943. (when (and user-full-name (not (string= user-full-name "")))
  944. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  945. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  946. "Regexp matched against the \"From:\" header of an email or usenet message.
  947. It should match if the message is from the user him/herself."
  948. :group 'org-link-store
  949. :type 'regexp)
  950. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  951. "Non-nil means, storing a link to an Org file will use entry IDs.
  952. Note that before this variable is even considered, org-id must be loaded,
  953. to please customize `org-modules' and turn it on.
  954. The variable can have the following values:
  955. t Create an ID if needed to make a link to the current entry.
  956. create-if-interactive
  957. If `org-store-link' is called directly (interactively, as a user
  958. command), do create an ID to support the link. But when doing the
  959. job for remember, only use the ID if it already exists. The
  960. purpose of this setting is to avoid proliferation of unwanted
  961. IDs, just because you happen to be in an Org file when you
  962. call `org-remember' that automatically and preemptively
  963. creates a link. If you do want to get an ID link in a remember
  964. template to an entry not having an ID, create it first by
  965. explicitly creating a link to it, using `C-c C-l' first.
  966. use-existing
  967. Use existing ID, do not create one.
  968. nil Never use an ID to make a link, instead link using a text search for
  969. the headline text."
  970. :group 'org-link-store
  971. :type '(choice
  972. (const :tag "Create ID to make link" t)
  973. (const :tag "Create if storing link interactively"
  974. create-if-interactive)
  975. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  976. create-if-interactive-and-no-custom-id)
  977. (const :tag "Only use existing" use-existing)
  978. (const :tag "Do not use ID to create link" nil)))
  979. (defcustom org-context-in-file-links t
  980. "Non-nil means, file links from `org-store-link' contain context.
  981. A search string will be added to the file name with :: as separator and
  982. used to find the context when the link is activated by the command
  983. `org-open-at-point'.
  984. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  985. negates this setting for the duration of the command."
  986. :group 'org-link-store
  987. :type 'boolean)
  988. (defcustom org-keep-stored-link-after-insertion nil
  989. "Non-nil means, keep link in list for entire session.
  990. The command `org-store-link' adds a link pointing to the current
  991. location to an internal list. These links accumulate during a session.
  992. The command `org-insert-link' can be used to insert links into any
  993. Org-mode file (offering completion for all stored links). When this
  994. option is nil, every link which has been inserted once using \\[org-insert-link]
  995. will be removed from the list, to make completing the unused links
  996. more efficient."
  997. :group 'org-link-store
  998. :type 'boolean)
  999. (defgroup org-link-follow nil
  1000. "Options concerning following links in Org-mode."
  1001. :tag "Org Follow Link"
  1002. :group 'org-link)
  1003. (defcustom org-link-translation-function nil
  1004. "Function to translate links with different syntax to Org syntax.
  1005. This can be used to translate links created for example by the Planner
  1006. or emacs-wiki packages to Org syntax.
  1007. The function must accept two parameters, a TYPE containing the link
  1008. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1009. which is everything after the link protocol. It should return a cons
  1010. with possibly modified values of type and path.
  1011. Org contains a function for this, so if you set this variable to
  1012. `org-translate-link-from-planner', you should be able follow many
  1013. links created by planner."
  1014. :group 'org-link-follow
  1015. :type 'function)
  1016. (defcustom org-follow-link-hook nil
  1017. "Hook that is run after a link has been followed."
  1018. :group 'org-link-follow
  1019. :type 'hook)
  1020. (defcustom org-tab-follows-link nil
  1021. "Non-nil means, on links TAB will follow the link.
  1022. Needs to be set before org.el is loaded.
  1023. This really should not be used, it does not make sense, and the
  1024. implementation is bad."
  1025. :group 'org-link-follow
  1026. :type 'boolean)
  1027. (defcustom org-return-follows-link nil
  1028. "Non-nil means, on links RET will follow the link.
  1029. Needs to be set before org.el is loaded."
  1030. :group 'org-link-follow
  1031. :type 'boolean)
  1032. (defcustom org-mouse-1-follows-link
  1033. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1034. "Non-nil means, mouse-1 on a link will follow the link.
  1035. A longer mouse click will still set point. Does not work on XEmacs.
  1036. Needs to be set before org.el is loaded."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-mark-ring-length 4
  1040. "Number of different positions to be recorded in the ring
  1041. Changing this requires a restart of Emacs to work correctly."
  1042. :group 'org-link-follow
  1043. :type 'integer)
  1044. (defcustom org-link-frame-setup
  1045. '((vm . vm-visit-folder-other-frame)
  1046. (gnus . gnus-other-frame)
  1047. (file . find-file-other-window))
  1048. "Setup the frame configuration for following links.
  1049. When following a link with Emacs, it may often be useful to display
  1050. this link in another window or frame. This variable can be used to
  1051. set this up for the different types of links.
  1052. For VM, use any of
  1053. `vm-visit-folder'
  1054. `vm-visit-folder-other-frame'
  1055. For Gnus, use any of
  1056. `gnus'
  1057. `gnus-other-frame'
  1058. `org-gnus-no-new-news'
  1059. For FILE, use any of
  1060. `find-file'
  1061. `find-file-other-window'
  1062. `find-file-other-frame'
  1063. For the calendar, use the variable `calendar-setup'.
  1064. For BBDB, it is currently only possible to display the matches in
  1065. another window."
  1066. :group 'org-link-follow
  1067. :type '(list
  1068. (cons (const vm)
  1069. (choice
  1070. (const vm-visit-folder)
  1071. (const vm-visit-folder-other-window)
  1072. (const vm-visit-folder-other-frame)))
  1073. (cons (const gnus)
  1074. (choice
  1075. (const gnus)
  1076. (const gnus-other-frame)
  1077. (const org-gnus-no-new-news)))
  1078. (cons (const file)
  1079. (choice
  1080. (const find-file)
  1081. (const find-file-other-window)
  1082. (const find-file-other-frame)))))
  1083. (defcustom org-display-internal-link-with-indirect-buffer nil
  1084. "Non-nil means, use indirect buffer to display infile links.
  1085. Activating internal links (from one location in a file to another location
  1086. in the same file) normally just jumps to the location. When the link is
  1087. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1088. another window. When this option is set, the other window actually displays
  1089. an indirect buffer clone of the current buffer, to avoid any visibility
  1090. changes to the current buffer."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-open-non-existing-files nil
  1094. "Non-nil means, `org-open-file' will open non-existing files.
  1095. When nil, an error will be generated."
  1096. :group 'org-link-follow
  1097. :type 'boolean)
  1098. (defcustom org-open-directory-means-index-dot-org nil
  1099. "Non-nil means, a link to a directory really means to index.org.
  1100. When nil, following a directory link will run dired or open a finder/explorer
  1101. window on that directory."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1105. "Function and arguments to call for following mailto links.
  1106. This is a list with the first element being a lisp function, and the
  1107. remaining elements being arguments to the function. In string arguments,
  1108. %a will be replaced by the address, and %s will be replaced by the subject
  1109. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1110. :group 'org-link-follow
  1111. :type '(choice
  1112. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1113. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1114. (const :tag "message-mail" (message-mail "%a" "%s"))
  1115. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1116. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1117. "Non-nil means, ask for confirmation before executing shell links.
  1118. Shell links can be dangerous: just think about a link
  1119. [[shell:rm -rf ~/*][Google Search]]
  1120. This link would show up in your Org-mode document as \"Google Search\",
  1121. but really it would remove your entire home directory.
  1122. Therefore we advise against setting this variable to nil.
  1123. Just change it to `y-or-n-p' if you want to confirm with a
  1124. single keystroke rather than having to type \"yes\"."
  1125. :group 'org-link-follow
  1126. :type '(choice
  1127. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1128. (const :tag "with y-or-n (faster)" y-or-n-p)
  1129. (const :tag "no confirmation (dangerous)" nil)))
  1130. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1131. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1132. Elisp links can be dangerous: just think about a link
  1133. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1134. This link would show up in your Org-mode document as \"Google Search\",
  1135. but really it would remove your entire home directory.
  1136. Therefore we advise against setting this variable to nil.
  1137. Just change it to `y-or-n-p' if you want to confirm with a
  1138. single keystroke rather than having to type \"yes\"."
  1139. :group 'org-link-follow
  1140. :type '(choice
  1141. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1142. (const :tag "with y-or-n (faster)" y-or-n-p)
  1143. (const :tag "no confirmation (dangerous)" nil)))
  1144. (defconst org-file-apps-defaults-gnu
  1145. '((remote . emacs)
  1146. (system . mailcap)
  1147. (t . mailcap))
  1148. "Default file applications on a UNIX or GNU/Linux system.
  1149. See `org-file-apps'.")
  1150. (defconst org-file-apps-defaults-macosx
  1151. '((remote . emacs)
  1152. (t . "open %s")
  1153. (system . "open %s")
  1154. ("ps.gz" . "gv %s")
  1155. ("eps.gz" . "gv %s")
  1156. ("dvi" . "xdvi %s")
  1157. ("fig" . "xfig %s"))
  1158. "Default file applications on a MacOS X system.
  1159. The system \"open\" is known as a default, but we use X11 applications
  1160. for some files for which the OS does not have a good default.
  1161. See `org-file-apps'.")
  1162. (defconst org-file-apps-defaults-windowsnt
  1163. (list
  1164. '(remote . emacs)
  1165. (cons t
  1166. (list (if (featurep 'xemacs)
  1167. 'mswindows-shell-execute
  1168. 'w32-shell-execute)
  1169. "open" 'file))
  1170. (cons 'system
  1171. (list (if (featurep 'xemacs)
  1172. 'mswindows-shell-execute
  1173. 'w32-shell-execute)
  1174. "open" 'file)))
  1175. "Default file applications on a Windows NT system.
  1176. The system \"open\" is used for most files.
  1177. See `org-file-apps'.")
  1178. (defcustom org-file-apps
  1179. '(
  1180. (auto-mode . emacs)
  1181. ("\\.x?html?\\'" . default)
  1182. ("\\.pdf\\'" . default)
  1183. )
  1184. "External applications for opening `file:path' items in a document.
  1185. Org-mode uses system defaults for different file types, but
  1186. you can use this variable to set the application for a given file
  1187. extension. The entries in this list are cons cells where the car identifies
  1188. files and the cdr the corresponding command. Possible values for the
  1189. file identifier are
  1190. \"regex\" Regular expression matched against the file name. For backward
  1191. compatibility, this can also be a string with only alphanumeric
  1192. characters, which is then interpreted as an extension.
  1193. `directory' Matches a directory
  1194. `remote' Matches a remote file, accessible through tramp or efs.
  1195. Remote files most likely should be visited through Emacs
  1196. because external applications cannot handle such paths.
  1197. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1198. so all files Emacs knows how to handle. Using this with
  1199. command `emacs' will open most files in Emacs. Beware that this
  1200. will also open html files inside Emacs, unless you add
  1201. (\"html\" . default) to the list as well.
  1202. t Default for files not matched by any of the other options.
  1203. `system' The system command to open files, like `open' on Windows
  1204. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1205. that will be selected if you call `C-c C-o' with a double
  1206. `C-u C-u' prefix.
  1207. Possible values for the command are:
  1208. `emacs' The file will be visited by the current Emacs process.
  1209. `default' Use the default application for this file type, which is the
  1210. association for t in the list, most likely in the system-specific
  1211. part.
  1212. This can be used to overrule an unwanted setting in the
  1213. system-specific variable.
  1214. `system' Use the system command for opening files, like \"open\".
  1215. This command is specified by the entry whose car is `system'.
  1216. Most likely, the system-specific version of this variable
  1217. does define this command, but you can overrule/replace it
  1218. here.
  1219. string A command to be executed by a shell; %s will be replaced
  1220. by the path to the file.
  1221. sexp A Lisp form which will be evaluated. The file path will
  1222. be available in the Lisp variable `file'.
  1223. For more examples, see the system specific constants
  1224. `org-file-apps-defaults-macosx'
  1225. `org-file-apps-defaults-windowsnt'
  1226. `org-file-apps-defaults-gnu'."
  1227. :group 'org-link-follow
  1228. :type '(repeat
  1229. (cons (choice :value ""
  1230. (string :tag "Extension")
  1231. (const :tag "System command to open files" system)
  1232. (const :tag "Default for unrecognized files" t)
  1233. (const :tag "Remote file" remote)
  1234. (const :tag "Links to a directory" directory)
  1235. (const :tag "Any files that have Emacs modes"
  1236. auto-mode))
  1237. (choice :value ""
  1238. (const :tag "Visit with Emacs" emacs)
  1239. (const :tag "Use default" default)
  1240. (const :tag "Use the system command" system)
  1241. (string :tag "Command")
  1242. (sexp :tag "Lisp form")))))
  1243. (defgroup org-refile nil
  1244. "Options concerning refiling entries in Org-mode."
  1245. :tag "Org Refile"
  1246. :group 'org)
  1247. (defcustom org-directory "~/org"
  1248. "Directory with org files.
  1249. This is just a default location to look for Org files. There is no need
  1250. at all to put your files into this directory. It is only used in the
  1251. following situations:
  1252. 1. When a remember template specifies a target file that is not an
  1253. absolute path. The path will then be interpreted relative to
  1254. `org-directory'
  1255. 2. When a remember note is filed away in an interactive way (when exiting the
  1256. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1257. with `org-directory' as the default path."
  1258. :group 'org-refile
  1259. :group 'org-remember
  1260. :type 'directory)
  1261. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1262. "Default target for storing notes.
  1263. Used by the hooks for remember.el. This can be a string, or nil to mean
  1264. the value of `remember-data-file'.
  1265. You can set this on a per-template basis with the variable
  1266. `org-remember-templates'."
  1267. :group 'org-refile
  1268. :group 'org-remember
  1269. :type '(choice
  1270. (const :tag "Default from remember-data-file" nil)
  1271. file))
  1272. (defcustom org-goto-interface 'outline
  1273. "The default interface to be used for `org-goto'.
  1274. Allowed values are:
  1275. outline The interface shows an outline of the relevant file
  1276. and the correct heading is found by moving through
  1277. the outline or by searching with incremental search.
  1278. outline-path-completion Headlines in the current buffer are offered via
  1279. completion. This is the interface also used by
  1280. the refile command."
  1281. :group 'org-refile
  1282. :type '(choice
  1283. (const :tag "Outline" outline)
  1284. (const :tag "Outline-path-completion" outline-path-completion)))
  1285. (defcustom org-goto-max-level 5
  1286. "Maximum level to be considered when running org-goto with refile interface."
  1287. :group 'org-refile
  1288. :type 'integer)
  1289. (defcustom org-reverse-note-order nil
  1290. "Non-nil means, store new notes at the beginning of a file or entry.
  1291. When nil, new notes will be filed to the end of a file or entry.
  1292. This can also be a list with cons cells of regular expressions that
  1293. are matched against file names, and values."
  1294. :group 'org-remember
  1295. :group 'org-refile
  1296. :type '(choice
  1297. (const :tag "Reverse always" t)
  1298. (const :tag "Reverse never" nil)
  1299. (repeat :tag "By file name regexp"
  1300. (cons regexp boolean))))
  1301. (defcustom org-refile-targets nil
  1302. "Targets for refiling entries with \\[org-refile].
  1303. This is list of cons cells. Each cell contains:
  1304. - a specification of the files to be considered, either a list of files,
  1305. or a symbol whose function or variable value will be used to retrieve
  1306. a file name or a list of file names. If you use `org-agenda-files' for
  1307. that, all agenda files will be scanned for targets. Nil means, consider
  1308. headings in the current buffer.
  1309. - A specification of how to find candidate refile targets. This may be
  1310. any of:
  1311. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1312. This tag has to be present in all target headlines, inheritance will
  1313. not be considered.
  1314. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1315. todo keyword.
  1316. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1317. headlines that are refiling targets.
  1318. - a cons cell (:level . N). Any headline of level N is considered a target.
  1319. Note that, when `org-odd-levels-only' is set, level corresponds to
  1320. order in hierarchy, not to the number of stars.
  1321. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1322. Note that, when `org-odd-levels-only' is set, level corresponds to
  1323. order in hierarchy, not to the number of stars.
  1324. You can set the variable `org-refile-target-verify-function' to a function
  1325. to verify each headline found by the simple critery above.
  1326. When this variable is nil, all top-level headlines in the current buffer
  1327. are used, equivalent to the value `((nil . (:level . 1))'."
  1328. :group 'org-refile
  1329. :type '(repeat
  1330. (cons
  1331. (choice :value org-agenda-files
  1332. (const :tag "All agenda files" org-agenda-files)
  1333. (const :tag "Current buffer" nil)
  1334. (function) (variable) (file))
  1335. (choice :tag "Identify target headline by"
  1336. (cons :tag "Specific tag" (const :value :tag) (string))
  1337. (cons :tag "TODO keyword" (const :value :todo) (string))
  1338. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1339. (cons :tag "Level number" (const :value :level) (integer))
  1340. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1341. (defcustom org-refile-target-verify-function nil
  1342. "Function to verify if the headline at point should be a refile target.
  1343. The function will be called without arguments, with point at the beginning
  1344. of the headline. It should return t if the headline is a valid target
  1345. for refiling."
  1346. :group 'org-refile
  1347. :type 'function)
  1348. (defcustom org-refile-use-outline-path nil
  1349. "Non-nil means, provide refile targets as paths.
  1350. So a level 3 headline will be available as level1/level2/level3.
  1351. When the value is `file', also include the file name (without directory)
  1352. into the path. In this case, you can also stop the completion after
  1353. the file name, to get entries inserted as top level in the file.
  1354. When `full-file-path', include the full file path."
  1355. :group 'org-refile
  1356. :type '(choice
  1357. (const :tag "Not" nil)
  1358. (const :tag "Yes" t)
  1359. (const :tag "Start with file name" file)
  1360. (const :tag "Start with full file path" full-file-path)))
  1361. (defcustom org-outline-path-complete-in-steps t
  1362. "Non-nil means, complete the outline path in hierarchical steps.
  1363. When Org-mode uses the refile interface to select an outline path
  1364. \(see variable `org-refile-use-outline-path'), the completion of
  1365. the path can be done is a single go, or if can be done in steps down
  1366. the headline hierarchy. Going in steps is probably the best if you
  1367. do not use a special completion package like `ido' or `icicles'.
  1368. However, when using these packages, going in one step can be very
  1369. fast, while still showing the whole path to the entry."
  1370. :group 'org-refile
  1371. :type 'boolean)
  1372. (defcustom org-refile-allow-creating-parent-nodes nil
  1373. "Non-nil means, allow to create new nodes as refile targets.
  1374. New nodes are then created by adding \"/new node name\" to the completion
  1375. of an existing node. When the value of this variable is `confirm',
  1376. new node creation must be confirmed by the user (recommended)
  1377. When nil, the completion must match an existing entry.
  1378. Note that, if the new heading is not seen by the criteria
  1379. listed in `org-refile-targets', multiple instances of the same
  1380. heading would be created by trying again to file under the new
  1381. heading."
  1382. :group 'org-refile
  1383. :type '(choice
  1384. (const :tag "Never" nil)
  1385. (const :tag "Always" t)
  1386. (const :tag "Prompt for confirmation" confirm)))
  1387. (defgroup org-todo nil
  1388. "Options concerning TODO items in Org-mode."
  1389. :tag "Org TODO"
  1390. :group 'org)
  1391. (defgroup org-progress nil
  1392. "Options concerning Progress logging in Org-mode."
  1393. :tag "Org Progress"
  1394. :group 'org-time)
  1395. (defvar org-todo-interpretation-widgets
  1396. '(
  1397. (:tag "Sequence (cycling hits every state)" sequence)
  1398. (:tag "Type (cycling directly to DONE)" type))
  1399. "The available interpretation symbols for customizing
  1400. `org-todo-keywords'.
  1401. Interested libraries should add to this list.")
  1402. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1403. "List of TODO entry keyword sequences and their interpretation.
  1404. \\<org-mode-map>This is a list of sequences.
  1405. Each sequence starts with a symbol, either `sequence' or `type',
  1406. indicating if the keywords should be interpreted as a sequence of
  1407. action steps, or as different types of TODO items. The first
  1408. keywords are states requiring action - these states will select a headline
  1409. for inclusion into the global TODO list Org-mode produces. If one of
  1410. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1411. signify that no further action is necessary. If \"|\" is not found,
  1412. the last keyword is treated as the only DONE state of the sequence.
  1413. The command \\[org-todo] cycles an entry through these states, and one
  1414. additional state where no keyword is present. For details about this
  1415. cycling, see the manual.
  1416. TODO keywords and interpretation can also be set on a per-file basis with
  1417. the special #+SEQ_TODO and #+TYP_TODO lines.
  1418. Each keyword can optionally specify a character for fast state selection
  1419. \(in combination with the variable `org-use-fast-todo-selection')
  1420. and specifiers for state change logging, using the same syntax
  1421. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1422. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1423. indicates to record a time stamp each time this state is selected.
  1424. Each keyword may also specify if a timestamp or a note should be
  1425. recorded when entering or leaving the state, by adding additional
  1426. characters in the parenthesis after the keyword. This looks like this:
  1427. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1428. record only the time of the state change. With X and Y being either
  1429. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1430. Y when leaving the state if and only if the *target* state does not
  1431. define X. You may omit any of the fast-selection key or X or /Y,
  1432. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1433. For backward compatibility, this variable may also be just a list
  1434. of keywords - in this case the interpretation (sequence or type) will be
  1435. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1436. :group 'org-todo
  1437. :group 'org-keywords
  1438. :type '(choice
  1439. (repeat :tag "Old syntax, just keywords"
  1440. (string :tag "Keyword"))
  1441. (repeat :tag "New syntax"
  1442. (cons
  1443. (choice
  1444. :tag "Interpretation"
  1445. ;;Quick and dirty way to see
  1446. ;;`org-todo-interpretations'. This takes the
  1447. ;;place of item arguments
  1448. :convert-widget
  1449. (lambda (widget)
  1450. (widget-put widget
  1451. :args (mapcar
  1452. #'(lambda (x)
  1453. (widget-convert
  1454. (cons 'const x)))
  1455. org-todo-interpretation-widgets))
  1456. widget))
  1457. (repeat
  1458. (string :tag "Keyword"))))))
  1459. (defvar org-todo-keywords-1 nil
  1460. "All TODO and DONE keywords active in a buffer.")
  1461. (make-variable-buffer-local 'org-todo-keywords-1)
  1462. (defvar org-todo-keywords-for-agenda nil)
  1463. (defvar org-done-keywords-for-agenda nil)
  1464. (defvar org-todo-keyword-alist-for-agenda nil)
  1465. (defvar org-tag-alist-for-agenda nil)
  1466. (defvar org-agenda-contributing-files nil)
  1467. (defvar org-not-done-keywords nil)
  1468. (make-variable-buffer-local 'org-not-done-keywords)
  1469. (defvar org-done-keywords nil)
  1470. (make-variable-buffer-local 'org-done-keywords)
  1471. (defvar org-todo-heads nil)
  1472. (make-variable-buffer-local 'org-todo-heads)
  1473. (defvar org-todo-sets nil)
  1474. (make-variable-buffer-local 'org-todo-sets)
  1475. (defvar org-todo-log-states nil)
  1476. (make-variable-buffer-local 'org-todo-log-states)
  1477. (defvar org-todo-kwd-alist nil)
  1478. (make-variable-buffer-local 'org-todo-kwd-alist)
  1479. (defvar org-todo-key-alist nil)
  1480. (make-variable-buffer-local 'org-todo-key-alist)
  1481. (defvar org-todo-key-trigger nil)
  1482. (make-variable-buffer-local 'org-todo-key-trigger)
  1483. (defcustom org-todo-interpretation 'sequence
  1484. "Controls how TODO keywords are interpreted.
  1485. This variable is in principle obsolete and is only used for
  1486. backward compatibility, if the interpretation of todo keywords is
  1487. not given already in `org-todo-keywords'. See that variable for
  1488. more information."
  1489. :group 'org-todo
  1490. :group 'org-keywords
  1491. :type '(choice (const sequence)
  1492. (const type)))
  1493. (defcustom org-use-fast-todo-selection t
  1494. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1495. This variable describes if and under what circumstances the cycling
  1496. mechanism for TODO keywords will be replaced by a single-key, direct
  1497. selection scheme.
  1498. When nil, fast selection is never used.
  1499. When the symbol `prefix', it will be used when `org-todo' is called with
  1500. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1501. in an agenda buffer.
  1502. When t, fast selection is used by default. In this case, the prefix
  1503. argument forces cycling instead.
  1504. In all cases, the special interface is only used if access keys have actually
  1505. been assigned by the user, i.e. if keywords in the configuration are followed
  1506. by a letter in parenthesis, like TODO(t)."
  1507. :group 'org-todo
  1508. :type '(choice
  1509. (const :tag "Never" nil)
  1510. (const :tag "By default" t)
  1511. (const :tag "Only with C-u C-c C-t" prefix)))
  1512. (defcustom org-provide-todo-statistics t
  1513. "Non-nil means, update todo statistics after insert and toggle.
  1514. When this is set, todo statistics is updated in the parent of the current
  1515. entry each time a todo state is changed."
  1516. :group 'org-todo
  1517. :type 'boolean)
  1518. (defcustom org-after-todo-state-change-hook nil
  1519. "Hook which is run after the state of a TODO item was changed.
  1520. The new state (a string with a TODO keyword, or nil) is available in the
  1521. Lisp variable `state'."
  1522. :group 'org-todo
  1523. :type 'hook)
  1524. (defvar org-blocker-hook nil
  1525. "Hook for functions that are allowed to block a state change.
  1526. Each function gets as its single argument a property list, see
  1527. `org-trigger-hook' for more information about this list.
  1528. If any of the functions in this hook returns nil, the state change
  1529. is blocked.")
  1530. (defvar org-trigger-hook nil
  1531. "Hook for functions that are triggered by a state change.
  1532. Each function gets as its single argument a property list with at least
  1533. the following elements:
  1534. (:type type-of-change :position pos-at-entry-start
  1535. :from old-state :to new-state)
  1536. Depending on the type, more properties may be present.
  1537. This mechanism is currently implemented for:
  1538. TODO state changes
  1539. ------------------
  1540. :type todo-state-change
  1541. :from previous state (keyword as a string), or nil, or a symbol
  1542. 'todo' or 'done', to indicate the general type of state.
  1543. :to new state, like in :from")
  1544. (defcustom org-enforce-todo-dependencies nil
  1545. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1546. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1547. be blocked if any prior sibling is not yet done.
  1548. Finally, if the parent is blocked because of ordered siblings of its own,
  1549. the child will also be blocked.
  1550. This variable needs to be set before org.el is loaded, and you need to
  1551. restart Emacs after a change to make the change effective. The only way
  1552. to change is while Emacs is running is through the customize interface."
  1553. :set (lambda (var val)
  1554. (set var val)
  1555. (if val
  1556. (add-hook 'org-blocker-hook
  1557. 'org-block-todo-from-children-or-siblings-or-parent)
  1558. (remove-hook 'org-blocker-hook
  1559. 'org-block-todo-from-children-or-siblings-or-parent)))
  1560. :group 'org-todo
  1561. :type 'boolean)
  1562. (defcustom org-enforce-todo-checkbox-dependencies nil
  1563. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1564. When this is nil, checkboxes have no influence on switching TODO states.
  1565. When non-nil, you first need to check off all check boxes before the TODO
  1566. entry can be switched to DONE.
  1567. This variable needs to be set before org.el is loaded, and you need to
  1568. restart Emacs after a change to make the change effective. The only way
  1569. to change is while Emacs is running is through the customize interface."
  1570. :set (lambda (var val)
  1571. (set var val)
  1572. (if val
  1573. (add-hook 'org-blocker-hook
  1574. 'org-block-todo-from-checkboxes)
  1575. (remove-hook 'org-blocker-hook
  1576. 'org-block-todo-from-checkboxes)))
  1577. :group 'org-todo
  1578. :type 'boolean)
  1579. (defcustom org-todo-state-tags-triggers nil
  1580. "Tag changes that should be triggered by TODO state changes.
  1581. This is a list. Each entry is
  1582. (state-change (tag . flag) .......)
  1583. State-change can be a string with a state, and empty string to indicate the
  1584. state that has no TODO keyword, or it can be one of the symbols `todo'
  1585. or `done', meaning any not-done or done state, respectively."
  1586. :group 'org-todo
  1587. :group 'org-tags
  1588. :type '(repeat
  1589. (cons (choice :tag "When changing to"
  1590. (const :tag "Not-done state" todo)
  1591. (const :tag "Done state" done)
  1592. (string :tag "State"))
  1593. (repeat
  1594. (cons :tag "Tag action"
  1595. (string :tag "Tag")
  1596. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1597. (defcustom org-log-done nil
  1598. "Information to record when a task moves to the DONE state.
  1599. Possible values are:
  1600. nil Don't add anything, just change the keyword
  1601. time Add a time stamp to the task
  1602. note Prompt a closing note and add it with template `org-log-note-headings'
  1603. This option can also be set with on a per-file-basis with
  1604. #+STARTUP: nologdone
  1605. #+STARTUP: logdone
  1606. #+STARTUP: lognotedone
  1607. You can have local logging settings for a subtree by setting the LOGGING
  1608. property to one or more of these keywords."
  1609. :group 'org-todo
  1610. :group 'org-progress
  1611. :type '(choice
  1612. (const :tag "No logging" nil)
  1613. (const :tag "Record CLOSED timestamp" time)
  1614. (const :tag "Record CLOSED timestamp with closing note." note)))
  1615. ;; Normalize old uses of org-log-done.
  1616. (cond
  1617. ((eq org-log-done t) (setq org-log-done 'time))
  1618. ((and (listp org-log-done) (memq 'done org-log-done))
  1619. (setq org-log-done 'note)))
  1620. (defcustom org-log-note-clock-out nil
  1621. "Non-nil means, record a note when clocking out of an item.
  1622. This can also be configured on a per-file basis by adding one of
  1623. the following lines anywhere in the buffer:
  1624. #+STARTUP: lognoteclock-out
  1625. #+STARTUP: nolognoteclock-out"
  1626. :group 'org-todo
  1627. :group 'org-progress
  1628. :type 'boolean)
  1629. (defcustom org-log-done-with-time t
  1630. "Non-nil means, the CLOSED time stamp will contain date and time.
  1631. When nil, only the date will be recorded."
  1632. :group 'org-progress
  1633. :type 'boolean)
  1634. (defcustom org-log-note-headings
  1635. '((done . "CLOSING NOTE %t")
  1636. (state . "State %-12s from %-12S %t")
  1637. (note . "Note taken on %t")
  1638. (clock-out . ""))
  1639. "Headings for notes added to entries.
  1640. The value is an alist, with the car being a symbol indicating the note
  1641. context, and the cdr is the heading to be used. The heading may also be the
  1642. empty string.
  1643. %t in the heading will be replaced by a time stamp.
  1644. %s will be replaced by the new TODO state, in double quotes.
  1645. %S will be replaced by the old TODO state, in double quotes.
  1646. %u will be replaced by the user name.
  1647. %U will be replaced by the full user name."
  1648. :group 'org-todo
  1649. :group 'org-progress
  1650. :type '(list :greedy t
  1651. (cons (const :tag "Heading when closing an item" done) string)
  1652. (cons (const :tag
  1653. "Heading when changing todo state (todo sequence only)"
  1654. state) string)
  1655. (cons (const :tag "Heading when just taking a note" note) string)
  1656. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1657. (unless (assq 'note org-log-note-headings)
  1658. (push '(note . "%t") org-log-note-headings))
  1659. (defcustom org-log-into-drawer nil
  1660. "Non-nil means, insert state change notes and time stamps into a drawer.
  1661. When nil, state changes notes will be inserted after the headline and
  1662. any scheduling and clock lines, but not inside a drawer.
  1663. The value of this variable should be the name of the drawer to use.
  1664. LOGBOOK is proposed at the default drawer for this purpose, you can
  1665. also set this to a string to define the drawer of your choice.
  1666. A value of t is also allowed, representing \"LOGBOOK\".
  1667. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1668. will be ignored."
  1669. :group 'org-todo
  1670. :group 'org-progress
  1671. :type '(choice
  1672. (const :tag "Not into a drawer" nil)
  1673. (const :tag "LOGBOOK" t)
  1674. (string :tag "Other")))
  1675. (if (fboundp 'defvaralias)
  1676. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1677. (defcustom org-log-state-notes-insert-after-drawers nil
  1678. "Non-nil means, insert state change notes after any drawers in entry.
  1679. Only the drawers that *immediately* follow the headline and the
  1680. deadline/scheduled line are skipped.
  1681. When nil, insert notes right after the heading and perhaps the line
  1682. with deadline/scheduling if present.
  1683. This variable will have no effect if `org-log-into-drawer' is
  1684. set."
  1685. :group 'org-todo
  1686. :group 'org-progress
  1687. :type 'boolean)
  1688. (defcustom org-log-states-order-reversed t
  1689. "Non-nil means, the latest state change note will be directly after heading.
  1690. When nil, the notes will be orderer according to time."
  1691. :group 'org-todo
  1692. :group 'org-progress
  1693. :type 'boolean)
  1694. (defcustom org-log-repeat 'time
  1695. "Non-nil means, record moving through the DONE state when triggering repeat.
  1696. An auto-repeating tasks is immediately switched back to TODO when marked
  1697. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1698. the TODO keyword definition, or recording a closing note by setting
  1699. `org-log-done', there will be no record of the task moving through DONE.
  1700. This variable forces taking a note anyway. Possible values are:
  1701. nil Don't force a record
  1702. time Record a time stamp
  1703. note Record a note
  1704. This option can also be set with on a per-file-basis with
  1705. #+STARTUP: logrepeat
  1706. #+STARTUP: lognoterepeat
  1707. #+STARTUP: nologrepeat
  1708. You can have local logging settings for a subtree by setting the LOGGING
  1709. property to one or more of these keywords."
  1710. :group 'org-todo
  1711. :group 'org-progress
  1712. :type '(choice
  1713. (const :tag "Don't force a record" nil)
  1714. (const :tag "Force recording the DONE state" time)
  1715. (const :tag "Force recording a note with the DONE state" note)))
  1716. (defgroup org-priorities nil
  1717. "Priorities in Org-mode."
  1718. :tag "Org Priorities"
  1719. :group 'org-todo)
  1720. (defcustom org-highest-priority ?A
  1721. "The highest priority of TODO items. A character like ?A, ?B etc.
  1722. Must have a smaller ASCII number than `org-lowest-priority'."
  1723. :group 'org-priorities
  1724. :type 'character)
  1725. (defcustom org-lowest-priority ?C
  1726. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1727. Must have a larger ASCII number than `org-highest-priority'."
  1728. :group 'org-priorities
  1729. :type 'character)
  1730. (defcustom org-default-priority ?B
  1731. "The default priority of TODO items.
  1732. This is the priority an item get if no explicit priority is given."
  1733. :group 'org-priorities
  1734. :type 'character)
  1735. (defcustom org-priority-start-cycle-with-default t
  1736. "Non-nil means, start with default priority when starting to cycle.
  1737. When this is nil, the first step in the cycle will be (depending on the
  1738. command used) one higher or lower that the default priority."
  1739. :group 'org-priorities
  1740. :type 'boolean)
  1741. (defgroup org-time nil
  1742. "Options concerning time stamps and deadlines in Org-mode."
  1743. :tag "Org Time"
  1744. :group 'org)
  1745. (defcustom org-insert-labeled-timestamps-at-point nil
  1746. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1747. When nil, these labeled time stamps are forces into the second line of an
  1748. entry, just after the headline. When scheduling from the global TODO list,
  1749. the time stamp will always be forced into the second line."
  1750. :group 'org-time
  1751. :type 'boolean)
  1752. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1753. "Formats for `format-time-string' which are used for time stamps.
  1754. It is not recommended to change this constant.")
  1755. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1756. "Number of minutes to round time stamps to.
  1757. These are two values, the first applies when first creating a time stamp.
  1758. The second applies when changing it with the commands `S-up' and `S-down'.
  1759. When changing the time stamp, this means that it will change in steps
  1760. of N minutes, as given by the second value.
  1761. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1762. numbers should be factors of 60, so for example 5, 10, 15.
  1763. When this is larger than 1, you can still force an exact time-stamp by using
  1764. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1765. and by using a prefix arg to `S-up/down' to specify the exact number
  1766. of minutes to shift."
  1767. :group 'org-time
  1768. :get '(lambda (var) ; Make sure all entries have 5 elements
  1769. (if (integerp (default-value var))
  1770. (list (default-value var) 5)
  1771. (default-value var)))
  1772. :type '(list
  1773. (integer :tag "when inserting times")
  1774. (integer :tag "when modifying times")))
  1775. ;; Normalize old customizations of this variable.
  1776. (when (integerp org-time-stamp-rounding-minutes)
  1777. (setq org-time-stamp-rounding-minutes
  1778. (list org-time-stamp-rounding-minutes
  1779. org-time-stamp-rounding-minutes)))
  1780. (defcustom org-display-custom-times nil
  1781. "Non-nil means, overlay custom formats over all time stamps.
  1782. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1783. To turn this on on a per-file basis, insert anywhere in the file:
  1784. #+STARTUP: customtime"
  1785. :group 'org-time
  1786. :set 'set-default
  1787. :type 'sexp)
  1788. (make-variable-buffer-local 'org-display-custom-times)
  1789. (defcustom org-time-stamp-custom-formats
  1790. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1791. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1792. These are overlayed over the default ISO format if the variable
  1793. `org-display-custom-times' is set. Time like %H:%M should be at the
  1794. end of the second format. The custom formats are also honored by export
  1795. commands, if custom time display is turned on at the time of export."
  1796. :group 'org-time
  1797. :type 'sexp)
  1798. (defun org-time-stamp-format (&optional long inactive)
  1799. "Get the right format for a time string."
  1800. (let ((f (if long (cdr org-time-stamp-formats)
  1801. (car org-time-stamp-formats))))
  1802. (if inactive
  1803. (concat "[" (substring f 1 -1) "]")
  1804. f)))
  1805. (defcustom org-time-clocksum-format "%d:%02d"
  1806. "The format string used when creating CLOCKSUM lines, or when
  1807. org-mode generates a time duration."
  1808. :group 'org-time
  1809. :type 'string)
  1810. (defcustom org-deadline-warning-days 14
  1811. "No. of days before expiration during which a deadline becomes active.
  1812. This variable governs the display in sparse trees and in the agenda.
  1813. When 0 or negative, it means use this number (the absolute value of it)
  1814. even if a deadline has a different individual lead time specified.
  1815. Custom commands can set this variable in the options section."
  1816. :group 'org-time
  1817. :group 'org-agenda-daily/weekly
  1818. :type 'integer)
  1819. (defcustom org-read-date-prefer-future t
  1820. "Non-nil means, assume future for incomplete date input from user.
  1821. This affects the following situations:
  1822. 1. The user gives a day, but no month.
  1823. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1824. read this as the third of *next* month. However, if you enter \"17\",
  1825. it will be considered as *this* month.
  1826. 2. The user gives a month but not a year.
  1827. For example, if it is april and you enter \"feb 2\", this will be read
  1828. as feb 2, *next* year. \"May 5\", however, will be this year.
  1829. Currently this does not work for ISO week specifications.
  1830. When this option is nil, the current month and year will always be used
  1831. as defaults."
  1832. :group 'org-time
  1833. :type 'boolean)
  1834. (defcustom org-read-date-display-live t
  1835. "Non-nil means, display current interpretation of date prompt live.
  1836. This display will be in an overlay, in the minibuffer."
  1837. :group 'org-time
  1838. :type 'boolean)
  1839. (defcustom org-read-date-popup-calendar t
  1840. "Non-nil means, pop up a calendar when prompting for a date.
  1841. In the calendar, the date can be selected with mouse-1. However, the
  1842. minibuffer will also be active, and you can simply enter the date as well.
  1843. When nil, only the minibuffer will be available."
  1844. :group 'org-time
  1845. :type 'boolean)
  1846. (if (fboundp 'defvaralias)
  1847. (defvaralias 'org-popup-calendar-for-date-prompt
  1848. 'org-read-date-popup-calendar))
  1849. (defcustom org-read-date-minibuffer-setup-hook nil
  1850. "Hook to be used to set up keys for the date/time interface.
  1851. Add key definitions to `minibuffer-local-map', which will be a temporary
  1852. copy."
  1853. :group 'org-time
  1854. :type 'hook)
  1855. (defcustom org-extend-today-until 0
  1856. "The hour when your day really ends. Must be an integer.
  1857. This has influence for the following applications:
  1858. - When switching the agenda to \"today\". It it is still earlier than
  1859. the time given here, the day recognized as TODAY is actually yesterday.
  1860. - When a date is read from the user and it is still before the time given
  1861. here, the current date and time will be assumed to be yesterday, 23:59.
  1862. Also, timestamps inserted in remember templates follow this rule.
  1863. IMPORTANT: This is a feature whose implementation is and likely will
  1864. remain incomplete. Really, it is only here because past midnight seems to
  1865. be the favorite working time of John Wiegley :-)"
  1866. :group 'org-time
  1867. :type 'integer)
  1868. (defcustom org-edit-timestamp-down-means-later nil
  1869. "Non-nil means, S-down will increase the time in a time stamp.
  1870. When nil, S-up will increase."
  1871. :group 'org-time
  1872. :type 'boolean)
  1873. (defcustom org-calendar-follow-timestamp-change t
  1874. "Non-nil means, make the calendar window follow timestamp changes.
  1875. When a timestamp is modified and the calendar window is visible, it will be
  1876. moved to the new date."
  1877. :group 'org-time
  1878. :type 'boolean)
  1879. (defgroup org-tags nil
  1880. "Options concerning tags in Org-mode."
  1881. :tag "Org Tags"
  1882. :group 'org)
  1883. (defcustom org-tag-alist nil
  1884. "List of tags allowed in Org-mode files.
  1885. When this list is nil, Org-mode will base TAG input on what is already in the
  1886. buffer.
  1887. The value of this variable is an alist, the car of each entry must be a
  1888. keyword as a string, the cdr may be a character that is used to select
  1889. that tag through the fast-tag-selection interface.
  1890. See the manual for details."
  1891. :group 'org-tags
  1892. :type '(repeat
  1893. (choice
  1894. (cons (string :tag "Tag name")
  1895. (character :tag "Access char"))
  1896. (const :tag "Start radio group" (:startgroup))
  1897. (const :tag "End radio group" (:endgroup))
  1898. (const :tag "New line" (:newline)))))
  1899. (defcustom org-tag-persistent-alist nil
  1900. "List of tags that will always appear in all Org-mode files.
  1901. This is in addition to any in buffer settings or customizations
  1902. of `org-tag-alist'.
  1903. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1904. The value of this variable is an alist, the car of each entry must be a
  1905. keyword as a string, the cdr may be a character that is used to select
  1906. that tag through the fast-tag-selection interface.
  1907. See the manual for details.
  1908. To disable these tags on a per-file basis, insert anywhere in the file:
  1909. #+STARTUP: noptag"
  1910. :group 'org-tags
  1911. :type '(repeat
  1912. (choice
  1913. (cons (string :tag "Tag name")
  1914. (character :tag "Access char"))
  1915. (const :tag "Start radio group" (:startgroup))
  1916. (const :tag "End radio group" (:endgroup))
  1917. (const :tag "New line" (:newline)))))
  1918. (defvar org-file-tags nil
  1919. "List of tags that can be inherited by all entries in the file.
  1920. The tags will be inherited if the variable `org-use-tag-inheritance'
  1921. says they should be.
  1922. This variable is populated from #+TAG lines.")
  1923. (defcustom org-use-fast-tag-selection 'auto
  1924. "Non-nil means, use fast tag selection scheme.
  1925. This is a special interface to select and deselect tags with single keys.
  1926. When nil, fast selection is never used.
  1927. When the symbol `auto', fast selection is used if and only if selection
  1928. characters for tags have been configured, either through the variable
  1929. `org-tag-alist' or through a #+TAGS line in the buffer.
  1930. When t, fast selection is always used and selection keys are assigned
  1931. automatically if necessary."
  1932. :group 'org-tags
  1933. :type '(choice
  1934. (const :tag "Always" t)
  1935. (const :tag "Never" nil)
  1936. (const :tag "When selection characters are configured" 'auto)))
  1937. (defcustom org-fast-tag-selection-single-key nil
  1938. "Non-nil means, fast tag selection exits after first change.
  1939. When nil, you have to press RET to exit it.
  1940. During fast tag selection, you can toggle this flag with `C-c'.
  1941. This variable can also have the value `expert'. In this case, the window
  1942. displaying the tags menu is not even shown, until you press C-c again."
  1943. :group 'org-tags
  1944. :type '(choice
  1945. (const :tag "No" nil)
  1946. (const :tag "Yes" t)
  1947. (const :tag "Expert" expert)))
  1948. (defvar org-fast-tag-selection-include-todo nil
  1949. "Non-nil means, fast tags selection interface will also offer TODO states.
  1950. This is an undocumented feature, you should not rely on it.")
  1951. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1952. "The column to which tags should be indented in a headline.
  1953. If this number is positive, it specifies the column. If it is negative,
  1954. it means that the tags should be flushright to that column. For example,
  1955. -80 works well for a normal 80 character screen."
  1956. :group 'org-tags
  1957. :type 'integer)
  1958. (defcustom org-auto-align-tags t
  1959. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1960. These operations change the length of a headline and therefore shift
  1961. the tags around. With this options turned on, after each such operation
  1962. the tags are again aligned to `org-tags-column'."
  1963. :group 'org-tags
  1964. :type 'boolean)
  1965. (defcustom org-use-tag-inheritance t
  1966. "Non-nil means, tags in levels apply also for sublevels.
  1967. When nil, only the tags directly given in a specific line apply there.
  1968. This may also be a list of tags that should be inherited, or a regexp that
  1969. matches tags that should be inherited. Additional control is possible
  1970. with the variable `org-tags-exclude-from-inheritance' which gives an
  1971. explicit list of tags to be excluded from inheritance., even if the value of
  1972. `org-use-tag-inheritance' would select it for inheritance.
  1973. If this option is t, a match early-on in a tree can lead to a large
  1974. number of matches in the subtree when constructing the agenda or creating
  1975. a sparse tree. If you only want to see the first match in a tree during
  1976. a search, check out the variable `org-tags-match-list-sublevels'."
  1977. :group 'org-tags
  1978. :type '(choice
  1979. (const :tag "Not" nil)
  1980. (const :tag "Always" t)
  1981. (repeat :tag "Specific tags" (string :tag "Tag"))
  1982. (regexp :tag "Tags matched by regexp")))
  1983. (defcustom org-tags-exclude-from-inheritance nil
  1984. "List of tags that should never be inherited.
  1985. This is a way to exclude a few tags from inheritance. For way to do
  1986. the opposite, to actively allow inheritance for selected tags,
  1987. see the variable `org-use-tag-inheritance'."
  1988. :group 'org-tags
  1989. :type '(repeat (string :tag "Tag")))
  1990. (defun org-tag-inherit-p (tag)
  1991. "Check if TAG is one that should be inherited."
  1992. (cond
  1993. ((member tag org-tags-exclude-from-inheritance) nil)
  1994. ((eq org-use-tag-inheritance t) t)
  1995. ((not org-use-tag-inheritance) nil)
  1996. ((stringp org-use-tag-inheritance)
  1997. (string-match org-use-tag-inheritance tag))
  1998. ((listp org-use-tag-inheritance)
  1999. (member tag org-use-tag-inheritance))
  2000. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2001. (defcustom org-tags-match-list-sublevels t
  2002. "Non-nil means list also sublevels of headlines matching a search.
  2003. This variable applies to tags/property searches, and also to stuck
  2004. projects because this search is based on a tags match as well.
  2005. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2006. the sublevels of a headline matching a tag search often also match
  2007. the same search. Listing all of them can create very long lists.
  2008. Setting this variable to nil causes subtrees of a match to be skipped.
  2009. As a special case, if the tag search is restricted to TODO items, the
  2010. value of this variable is ignored and sublevels are always checked, to
  2011. make sure all corresponding TODO items find their way into the list.
  2012. This variable is semi-obsolete and probably should always be true. It
  2013. is better to limit inheritance to certain tags using the variables
  2014. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2015. :group 'org-tags
  2016. :type '(choice
  2017. (const :tag "No, don't list them" nil)
  2018. (const :tag "Yes, do list them" t)
  2019. (const :tag "List them, indented with leading dots" indented)))
  2020. (defvar org-tags-history nil
  2021. "History of minibuffer reads for tags.")
  2022. (defvar org-last-tags-completion-table nil
  2023. "The last used completion table for tags.")
  2024. (defvar org-after-tags-change-hook nil
  2025. "Hook that is run after the tags in a line have changed.")
  2026. (defgroup org-properties nil
  2027. "Options concerning properties in Org-mode."
  2028. :tag "Org Properties"
  2029. :group 'org)
  2030. (defcustom org-property-format "%-10s %s"
  2031. "How property key/value pairs should be formatted by `indent-line'.
  2032. When `indent-line' hits a property definition, it will format the line
  2033. according to this format, mainly to make sure that the values are
  2034. lined-up with respect to each other."
  2035. :group 'org-properties
  2036. :type 'string)
  2037. (defcustom org-use-property-inheritance nil
  2038. "Non-nil means, properties apply also for sublevels.
  2039. This setting is chiefly used during property searches. Turning it on can
  2040. cause significant overhead when doing a search, which is why it is not
  2041. on by default.
  2042. When nil, only the properties directly given in the current entry count.
  2043. When t, every property is inherited. The value may also be a list of
  2044. properties that should have inheritance, or a regular expression matching
  2045. properties that should be inherited.
  2046. However, note that some special properties use inheritance under special
  2047. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2048. and the properties ending in \"_ALL\" when they are used as descriptor
  2049. for valid values of a property.
  2050. Note for programmers:
  2051. When querying an entry with `org-entry-get', you can control if inheritance
  2052. should be used. By default, `org-entry-get' looks only at the local
  2053. properties. You can request inheritance by setting the inherit argument
  2054. to t (to force inheritance) or to `selective' (to respect the setting
  2055. in this variable)."
  2056. :group 'org-properties
  2057. :type '(choice
  2058. (const :tag "Not" nil)
  2059. (const :tag "Always" t)
  2060. (repeat :tag "Specific properties" (string :tag "Property"))
  2061. (regexp :tag "Properties matched by regexp")))
  2062. (defun org-property-inherit-p (property)
  2063. "Check if PROPERTY is one that should be inherited."
  2064. (cond
  2065. ((eq org-use-property-inheritance t) t)
  2066. ((not org-use-property-inheritance) nil)
  2067. ((stringp org-use-property-inheritance)
  2068. (string-match org-use-property-inheritance property))
  2069. ((listp org-use-property-inheritance)
  2070. (member property org-use-property-inheritance))
  2071. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2072. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2073. "The default column format, if no other format has been defined.
  2074. This variable can be set on the per-file basis by inserting a line
  2075. #+COLUMNS: %25ITEM ....."
  2076. :group 'org-properties
  2077. :type 'string)
  2078. (defcustom org-columns-ellipses ".."
  2079. "The ellipses to be used when a field in column view is truncated.
  2080. When this is the empty string, as many characters as possible are shown,
  2081. but then there will be no visual indication that the field has been truncated.
  2082. When this is a string of length N, the last N characters of a truncated
  2083. field are replaced by this string. If the column is narrower than the
  2084. ellipses string, only part of the ellipses string will be shown."
  2085. :group 'org-properties
  2086. :type 'string)
  2087. (defcustom org-columns-modify-value-for-display-function nil
  2088. "Function that modifies values for display in column view.
  2089. For example, it can be used to cut out a certain part from a time stamp.
  2090. The function must take 2 arguments:
  2091. column-title The title of the column (*not* the property name)
  2092. value The value that should be modified.
  2093. The function should return the value that should be displayed,
  2094. or nil if the normal value should be used."
  2095. :group 'org-properties
  2096. :type 'function)
  2097. (defcustom org-effort-property "Effort"
  2098. "The property that is being used to keep track of effort estimates.
  2099. Effort estimates given in this property need to have the format H:MM."
  2100. :group 'org-properties
  2101. :group 'org-progress
  2102. :type '(string :tag "Property"))
  2103. (defconst org-global-properties-fixed
  2104. '(("VISIBILITY_ALL" . "folded children content all"))
  2105. "List of property/value pairs that can be inherited by any entry.
  2106. These are fixed values, for the preset properties. The user variable
  2107. that can be used to add to this list is `org-global-properties'.
  2108. The entries in this list are cons cells where the car is a property
  2109. name and cdr is a string with the value. If the value represents
  2110. multiple items like an \"_ALL\" property, separate the items by
  2111. spaces.")
  2112. (defcustom org-global-properties nil
  2113. "List of property/value pairs that can be inherited by any entry.
  2114. This list will be combined with the constant `org-global-properties-fixed'.
  2115. The entries in this list are cons cells where the car is a property
  2116. name and cdr is a string with the value.
  2117. You can set buffer-local values for the same purpose in the variable
  2118. `org-file-properties' this by adding lines like
  2119. #+PROPERTY: NAME VALUE"
  2120. :group 'org-properties
  2121. :type '(repeat
  2122. (cons (string :tag "Property")
  2123. (string :tag "Value"))))
  2124. (defvar org-file-properties nil
  2125. "List of property/value pairs that can be inherited by any entry.
  2126. Valid for the current buffer.
  2127. This variable is populated from #+PROPERTY lines.")
  2128. (make-variable-buffer-local 'org-file-properties)
  2129. (defgroup org-agenda nil
  2130. "Options concerning agenda views in Org-mode."
  2131. :tag "Org Agenda"
  2132. :group 'org)
  2133. (defvar org-category nil
  2134. "Variable used by org files to set a category for agenda display.
  2135. Such files should use a file variable to set it, for example
  2136. # -*- mode: org; org-category: \"ELisp\"
  2137. or contain a special line
  2138. #+CATEGORY: ELisp
  2139. If the file does not specify a category, then file's base name
  2140. is used instead.")
  2141. (make-variable-buffer-local 'org-category)
  2142. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2143. (defcustom org-agenda-files nil
  2144. "The files to be used for agenda display.
  2145. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2146. \\[org-remove-file]. You can also use customize to edit the list.
  2147. If an entry is a directory, all files in that directory that are matched by
  2148. `org-agenda-file-regexp' will be part of the file list.
  2149. If the value of the variable is not a list but a single file name, then
  2150. the list of agenda files is actually stored and maintained in that file, one
  2151. agenda file per line."
  2152. :group 'org-agenda
  2153. :type '(choice
  2154. (repeat :tag "List of files and directories" file)
  2155. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2156. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2157. "Regular expression to match files for `org-agenda-files'.
  2158. If any element in the list in that variable contains a directory instead
  2159. of a normal file, all files in that directory that are matched by this
  2160. regular expression will be included."
  2161. :group 'org-agenda
  2162. :type 'regexp)
  2163. (defcustom org-agenda-text-search-extra-files nil
  2164. "List of extra files to be searched by text search commands.
  2165. These files will be search in addition to the agenda files by the
  2166. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2167. Note that these files will only be searched for text search commands,
  2168. not for the other agenda views like todo lists, tag searches or the weekly
  2169. agenda. This variable is intended to list notes and possibly archive files
  2170. that should also be searched by these two commands.
  2171. In fact, if the first element in the list is the symbol `agenda-archives',
  2172. than all archive files of all agenda files will be added to the search
  2173. scope."
  2174. :group 'org-agenda
  2175. :type '(set :greedy t
  2176. (const :tag "Agenda Archives" agenda-archives)
  2177. (repeat :inline t (file))))
  2178. (if (fboundp 'defvaralias)
  2179. (defvaralias 'org-agenda-multi-occur-extra-files
  2180. 'org-agenda-text-search-extra-files))
  2181. (defcustom org-agenda-skip-unavailable-files nil
  2182. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2183. A nil value means to remove them, after a query, from the list."
  2184. :group 'org-agenda
  2185. :type 'boolean)
  2186. (defcustom org-calendar-to-agenda-key [?c]
  2187. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2188. The command `org-calendar-goto-agenda' will be bound to this key. The
  2189. default is the character `c' because then `c' can be used to switch back and
  2190. forth between agenda and calendar."
  2191. :group 'org-agenda
  2192. :type 'sexp)
  2193. (defcustom org-calendar-agenda-action-key [?k]
  2194. "The key to be installed in `calendar-mode-map' for agenda-action.
  2195. The command `org-agenda-action' will be bound to this key. The
  2196. default is the character `k' because we use the same key in the agenda."
  2197. :group 'org-agenda
  2198. :type 'sexp)
  2199. (eval-after-load "calendar"
  2200. '(progn
  2201. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2202. 'org-calendar-goto-agenda)
  2203. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2204. 'org-agenda-action)))
  2205. (defgroup org-latex nil
  2206. "Options for embedding LaTeX code into Org-mode."
  2207. :tag "Org LaTeX"
  2208. :group 'org)
  2209. (defcustom org-format-latex-options
  2210. '(:foreground default :background default :scale 1.0
  2211. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2212. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2213. "Options for creating images from LaTeX fragments.
  2214. This is a property list with the following properties:
  2215. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2216. `default' means use the foreground of the default face.
  2217. :background the background color, or \"Transparent\".
  2218. `default' means use the background of the default face.
  2219. :scale a scaling factor for the size of the images.
  2220. :html-foreground, :html-background, :html-scale
  2221. the same numbers for HTML export.
  2222. :matchers a list indicating which matchers should be used to
  2223. find LaTeX fragments. Valid members of this list are:
  2224. \"begin\" find environments
  2225. \"$1\" find single characters surrounded by $.$
  2226. \"$\" find math expressions surrounded by $...$
  2227. \"$$\" find math expressions surrounded by $$....$$
  2228. \"\\(\" find math expressions surrounded by \\(...\\)
  2229. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2230. :group 'org-latex
  2231. :type 'plist)
  2232. (defcustom org-format-latex-header "\\documentclass{article}
  2233. \\usepackage{fullpage} % do not remove
  2234. \\usepackage{amssymb}
  2235. \\usepackage[usenames]{color}
  2236. \\usepackage{amsmath}
  2237. \\usepackage{latexsym}
  2238. \\usepackage[mathscr]{eucal}
  2239. \\pagestyle{empty} % do not remove"
  2240. "The document header used for processing LaTeX fragments."
  2241. :group 'org-latex
  2242. :type 'string)
  2243. (defgroup org-font-lock nil
  2244. "Font-lock settings for highlighting in Org-mode."
  2245. :tag "Org Font Lock"
  2246. :group 'org)
  2247. (defcustom org-level-color-stars-only nil
  2248. "Non-nil means fontify only the stars in each headline.
  2249. When nil, the entire headline is fontified.
  2250. Changing it requires restart of `font-lock-mode' to become effective
  2251. also in regions already fontified."
  2252. :group 'org-font-lock
  2253. :type 'boolean)
  2254. (defcustom org-hide-leading-stars nil
  2255. "Non-nil means, hide the first N-1 stars in a headline.
  2256. This works by using the face `org-hide' for these stars. This
  2257. face is white for a light background, and black for a dark
  2258. background. You may have to customize the face `org-hide' to
  2259. make this work.
  2260. Changing it requires restart of `font-lock-mode' to become effective
  2261. also in regions already fontified.
  2262. You may also set this on a per-file basis by adding one of the following
  2263. lines to the buffer:
  2264. #+STARTUP: hidestars
  2265. #+STARTUP: showstars"
  2266. :group 'org-font-lock
  2267. :type 'boolean)
  2268. (defcustom org-fontify-done-headline nil
  2269. "Non-nil means, change the face of a headline if it is marked DONE.
  2270. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2271. When this is non-nil, the headline after the keyword is set to the
  2272. `org-headline-done' as an additional indication."
  2273. :group 'org-font-lock
  2274. :type 'boolean)
  2275. (defcustom org-fontify-emphasized-text t
  2276. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2277. Changing this variable requires a restart of Emacs to take effect."
  2278. :group 'org-font-lock
  2279. :type 'boolean)
  2280. (defcustom org-highlight-latex-fragments-and-specials nil
  2281. "Non-nil means, fontify what is treated specially by the exporters."
  2282. :group 'org-font-lock
  2283. :type 'boolean)
  2284. (defcustom org-hide-emphasis-markers nil
  2285. "Non-nil mean font-lock should hide the emphasis marker characters."
  2286. :group 'org-font-lock
  2287. :type 'boolean)
  2288. (defvar org-emph-re nil
  2289. "Regular expression for matching emphasis.")
  2290. (defvar org-verbatim-re nil
  2291. "Regular expression for matching verbatim text.")
  2292. (defvar org-emphasis-regexp-components) ; defined just below
  2293. (defvar org-emphasis-alist) ; defined just below
  2294. (defun org-set-emph-re (var val)
  2295. "Set variable and compute the emphasis regular expression."
  2296. (set var val)
  2297. (when (and (boundp 'org-emphasis-alist)
  2298. (boundp 'org-emphasis-regexp-components)
  2299. org-emphasis-alist org-emphasis-regexp-components)
  2300. (let* ((e org-emphasis-regexp-components)
  2301. (pre (car e))
  2302. (post (nth 1 e))
  2303. (border (nth 2 e))
  2304. (body (nth 3 e))
  2305. (nl (nth 4 e))
  2306. (body1 (concat body "*?"))
  2307. (markers (mapconcat 'car org-emphasis-alist ""))
  2308. (vmarkers (mapconcat
  2309. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2310. org-emphasis-alist "")))
  2311. ;; make sure special characters appear at the right position in the class
  2312. (if (string-match "\\^" markers)
  2313. (setq markers (concat (replace-match "" t t markers) "^")))
  2314. (if (string-match "-" markers)
  2315. (setq markers (concat (replace-match "" t t markers) "-")))
  2316. (if (string-match "\\^" vmarkers)
  2317. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2318. (if (string-match "-" vmarkers)
  2319. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2320. (if (> nl 0)
  2321. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2322. (int-to-string nl) "\\}")))
  2323. ;; Make the regexp
  2324. (setq org-emph-re
  2325. (concat "\\([" pre "]\\|^\\)"
  2326. "\\("
  2327. "\\([" markers "]\\)"
  2328. "\\("
  2329. "[^" border "]\\|"
  2330. "[^" border "]"
  2331. body1
  2332. "[^" border "]"
  2333. "\\)"
  2334. "\\3\\)"
  2335. "\\([" post "]\\|$\\)"))
  2336. (setq org-verbatim-re
  2337. (concat "\\([" pre "]\\|^\\)"
  2338. "\\("
  2339. "\\([" vmarkers "]\\)"
  2340. "\\("
  2341. "[^" border "]\\|"
  2342. "[^" border "]"
  2343. body1
  2344. "[^" border "]"
  2345. "\\)"
  2346. "\\3\\)"
  2347. "\\([" post "]\\|$\\)")))))
  2348. (defcustom org-emphasis-regexp-components
  2349. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2350. "Components used to build the regular expression for emphasis.
  2351. This is a list with 6 entries. Terminology: In an emphasis string
  2352. like \" *strong word* \", we call the initial space PREMATCH, the final
  2353. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2354. and \"trong wor\" is the body. The different components in this variable
  2355. specify what is allowed/forbidden in each part:
  2356. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2357. post Chars allowed as postmatch. End of line will be allowed too.
  2358. border The chars *forbidden* as border characters.
  2359. body-regexp A regexp like \".\" to match a body character. Don't use
  2360. non-shy groups here, and don't allow newline here.
  2361. newline The maximum number of newlines allowed in an emphasis exp.
  2362. Use customize to modify this, or restart Emacs after changing it."
  2363. :group 'org-font-lock
  2364. :set 'org-set-emph-re
  2365. :type '(list
  2366. (sexp :tag "Allowed chars in pre ")
  2367. (sexp :tag "Allowed chars in post ")
  2368. (sexp :tag "Forbidden chars in border ")
  2369. (sexp :tag "Regexp for body ")
  2370. (integer :tag "number of newlines allowed")
  2371. (option (boolean :tag "Please ignore this button"))))
  2372. (defcustom org-emphasis-alist
  2373. `(("*" bold "<b>" "</b>")
  2374. ("/" italic "<i>" "</i>")
  2375. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2376. ("=" org-code "<code>" "</code>" verbatim)
  2377. ("~" org-verbatim "<code>" "</code>" verbatim)
  2378. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2379. "<del>" "</del>")
  2380. )
  2381. "Special syntax for emphasized text.
  2382. Text starting and ending with a special character will be emphasized, for
  2383. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2384. characters, the face to be used by font-lock for highlighting in Org-mode
  2385. Emacs buffers, and the HTML tags to be used for this.
  2386. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2387. Use customize to modify this, or restart Emacs after changing it."
  2388. :group 'org-font-lock
  2389. :set 'org-set-emph-re
  2390. :type '(repeat
  2391. (list
  2392. (string :tag "Marker character")
  2393. (choice
  2394. (face :tag "Font-lock-face")
  2395. (plist :tag "Face property list"))
  2396. (string :tag "HTML start tag")
  2397. (string :tag "HTML end tag")
  2398. (option (const verbatim)))))
  2399. ;;; Miscellaneous options
  2400. (defgroup org-completion nil
  2401. "Completion in Org-mode."
  2402. :tag "Org Completion"
  2403. :group 'org)
  2404. (defcustom org-completion-use-ido nil
  2405. "Non-nil means, use ido completion wherever possible.
  2406. Note that `ido-mode' must be active for this variable to be relevant.
  2407. If you decide to turn this variable on, you might well want to turn off
  2408. `org-outline-path-complete-in-steps'."
  2409. :group 'org-completion
  2410. :type 'boolean)
  2411. (defcustom org-completion-fallback-command 'hippie-expand
  2412. "The expansion command called by \\[org-complete] in normal context.
  2413. Normal means, no org-mode-specific context."
  2414. :group 'org-completion
  2415. :type 'function)
  2416. ;;; Functions and variables from ther packages
  2417. ;; Declared here to avoid compiler warnings
  2418. ;; XEmacs only
  2419. (defvar outline-mode-menu-heading)
  2420. (defvar outline-mode-menu-show)
  2421. (defvar outline-mode-menu-hide)
  2422. (defvar zmacs-regions) ; XEmacs regions
  2423. ;; Emacs only
  2424. (defvar mark-active)
  2425. ;; Various packages
  2426. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2427. (declare-function calendar-forward-day "cal-move" (arg))
  2428. (declare-function calendar-goto-date "cal-move" (date))
  2429. (declare-function calendar-goto-today "cal-move" ())
  2430. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2431. (defvar calc-embedded-close-formula)
  2432. (defvar calc-embedded-open-formula)
  2433. (declare-function cdlatex-tab "ext:cdlatex" ())
  2434. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2435. (defvar font-lock-unfontify-region-function)
  2436. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2437. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2438. (defvar iswitchb-temp-buflist)
  2439. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2440. (defvar org-agenda-tags-todo-honor-ignore-options)
  2441. (declare-function org-agenda-skip "org-agenda" ())
  2442. (declare-function org-format-agenda-item "org-agenda"
  2443. (extra txt &optional category tags dotime noprefix remove-re))
  2444. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2445. (declare-function org-agenda-change-all-lines "org-agenda"
  2446. (newhead hdmarker &optional fixface just-this))
  2447. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2448. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2449. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2450. (beg end))
  2451. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2452. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2453. "org-agenda" (&optional end))
  2454. (declare-function parse-time-string "parse-time" (string))
  2455. (declare-function remember "remember" (&optional initial))
  2456. (declare-function remember-buffer-desc "remember" ())
  2457. (declare-function remember-finalize "remember" ())
  2458. (defvar remember-save-after-remembering)
  2459. (defvar remember-data-file)
  2460. (defvar remember-register)
  2461. (defvar remember-buffer)
  2462. (defvar remember-handler-functions)
  2463. (defvar remember-annotation-functions)
  2464. (defvar texmathp-why)
  2465. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2466. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2467. (defvar w3m-current-url)
  2468. (defvar w3m-current-title)
  2469. (defvar org-latex-regexps)
  2470. ;;; Autoload and prepare some org modules
  2471. ;; Some table stuff that needs to be defined here, because it is used
  2472. ;; by the functions setting up org-mode or checking for table context.
  2473. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2474. "Detects an org-type or table-type table.")
  2475. (defconst org-table-line-regexp "^[ \t]*|"
  2476. "Detects an org-type table line.")
  2477. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2478. "Detects an org-type table line.")
  2479. (defconst org-table-hline-regexp "^[ \t]*|-"
  2480. "Detects an org-type table hline.")
  2481. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2482. "Detects a table-type table hline.")
  2483. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2484. "Searching from within a table (any type) this finds the first line
  2485. outside the table.")
  2486. ;; Autoload the functions in org-table.el that are needed by functions here.
  2487. (eval-and-compile
  2488. (org-autoload "org-table"
  2489. '(org-table-align org-table-begin org-table-blank-field
  2490. org-table-convert org-table-convert-region org-table-copy-down
  2491. org-table-copy-region org-table-create
  2492. org-table-create-or-convert-from-region
  2493. org-table-create-with-table.el org-table-current-dline
  2494. org-table-cut-region org-table-delete-column org-table-edit-field
  2495. org-table-edit-formulas org-table-end org-table-eval-formula
  2496. org-table-export org-table-field-info
  2497. org-table-get-stored-formulas org-table-goto-column
  2498. org-table-hline-and-move org-table-import org-table-insert-column
  2499. org-table-insert-hline org-table-insert-row org-table-iterate
  2500. org-table-justify-field-maybe org-table-kill-row
  2501. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2502. org-table-move-column org-table-move-column-left
  2503. org-table-move-column-right org-table-move-row
  2504. org-table-move-row-down org-table-move-row-up
  2505. org-table-next-field org-table-next-row org-table-paste-rectangle
  2506. org-table-previous-field org-table-recalculate
  2507. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2508. org-table-toggle-coordinate-overlays
  2509. org-table-toggle-formula-debugger org-table-wrap-region
  2510. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2511. (defun org-at-table-p (&optional table-type)
  2512. "Return t if the cursor is inside an org-type table.
  2513. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2514. (if org-enable-table-editor
  2515. (save-excursion
  2516. (beginning-of-line 1)
  2517. (looking-at (if table-type org-table-any-line-regexp
  2518. org-table-line-regexp)))
  2519. nil))
  2520. (defsubst org-table-p () (org-at-table-p))
  2521. (defun org-at-table.el-p ()
  2522. "Return t if and only if we are at a table.el table."
  2523. (and (org-at-table-p 'any)
  2524. (save-excursion
  2525. (goto-char (org-table-begin 'any))
  2526. (looking-at org-table1-hline-regexp))))
  2527. (defun org-table-recognize-table.el ()
  2528. "If there is a table.el table nearby, recognize it and move into it."
  2529. (if org-table-tab-recognizes-table.el
  2530. (if (org-at-table.el-p)
  2531. (progn
  2532. (beginning-of-line 1)
  2533. (if (looking-at org-table-dataline-regexp)
  2534. nil
  2535. (if (looking-at org-table1-hline-regexp)
  2536. (progn
  2537. (beginning-of-line 2)
  2538. (if (looking-at org-table-any-border-regexp)
  2539. (beginning-of-line -1)))))
  2540. (if (re-search-forward "|" (org-table-end t) t)
  2541. (progn
  2542. (require 'table)
  2543. (if (table--at-cell-p (point))
  2544. t
  2545. (message "recognizing table.el table...")
  2546. (table-recognize-table)
  2547. (message "recognizing table.el table...done")))
  2548. (error "This should not happen..."))
  2549. t)
  2550. nil)
  2551. nil))
  2552. (defun org-at-table-hline-p ()
  2553. "Return t if the cursor is inside a hline in a table."
  2554. (if org-enable-table-editor
  2555. (save-excursion
  2556. (beginning-of-line 1)
  2557. (looking-at org-table-hline-regexp))
  2558. nil))
  2559. (defvar org-table-clean-did-remove-column nil)
  2560. (defun org-table-map-tables (function)
  2561. "Apply FUNCTION to the start of all tables in the buffer."
  2562. (save-excursion
  2563. (save-restriction
  2564. (widen)
  2565. (goto-char (point-min))
  2566. (while (re-search-forward org-table-any-line-regexp nil t)
  2567. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2568. (beginning-of-line 1)
  2569. (if (looking-at org-table-line-regexp)
  2570. (save-excursion (funcall function)))
  2571. (re-search-forward org-table-any-border-regexp nil 1))))
  2572. (message "Mapping tables: done"))
  2573. ;; Declare and autoload functions from org-exp.el & Co
  2574. (declare-function org-default-export-plist "org-exp")
  2575. (declare-function org-infile-export-plist "org-exp")
  2576. (declare-function org-get-current-options "org-exp")
  2577. (eval-and-compile
  2578. (org-autoload "org-exp"
  2579. '(org-export org-export-visible
  2580. org-insert-export-options-template
  2581. org-table-clean-before-export))
  2582. (org-autoload "org-ascii"
  2583. '(org-export-as-ascii org-export-ascii-preprocess))
  2584. (org-autoload "org-html"
  2585. '(org-export-as-html-and-open
  2586. org-export-as-html-batch org-export-as-html-to-buffer
  2587. org-replace-region-by-html org-export-region-as-html
  2588. org-export-as-html))
  2589. (org-autoload "org-icalendar"
  2590. '(org-export-icalendar-this-file
  2591. org-export-icalendar-all-agenda-files
  2592. org-export-icalendar-combine-agenda-files))
  2593. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2594. ;; Declare and autoload functions from org-agenda.el
  2595. (eval-and-compile
  2596. (org-autoload "org-agenda"
  2597. '(org-agenda org-agenda-list org-search-view
  2598. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2599. org-diary org-agenda-to-appt
  2600. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2601. ;; Autoload org-remember
  2602. (eval-and-compile
  2603. (org-autoload "org-remember"
  2604. '(org-remember-insinuate org-remember-annotation
  2605. org-remember-apply-template org-remember org-remember-handler)))
  2606. ;; Autoload org-clock.el
  2607. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2608. (beg end))
  2609. (declare-function org-clock-update-mode-line "org-clock" ())
  2610. (defvar org-clock-start-time)
  2611. (defvar org-clock-marker (make-marker)
  2612. "Marker recording the last clock-in.")
  2613. (eval-and-compile
  2614. (org-autoload
  2615. "org-clock"
  2616. '(org-clock-in org-clock-out org-clock-cancel
  2617. org-clock-goto org-clock-sum org-clock-display
  2618. org-clock-remove-overlays org-clock-report
  2619. org-clocktable-shift org-dblock-write:clocktable
  2620. org-get-clocktable)))
  2621. (defun org-clock-update-time-maybe ()
  2622. "If this is a CLOCK line, update it and return t.
  2623. Otherwise, return nil."
  2624. (interactive)
  2625. (save-excursion
  2626. (beginning-of-line 1)
  2627. (skip-chars-forward " \t")
  2628. (when (looking-at org-clock-string)
  2629. (let ((re (concat "[ \t]*" org-clock-string
  2630. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2631. "\\([ \t]*=>.*\\)?\\)?"))
  2632. ts te h m s neg)
  2633. (cond
  2634. ((not (looking-at re))
  2635. nil)
  2636. ((not (match-end 2))
  2637. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2638. (> org-clock-marker (point))
  2639. (<= org-clock-marker (point-at-eol)))
  2640. ;; The clock is running here
  2641. (setq org-clock-start-time
  2642. (apply 'encode-time
  2643. (org-parse-time-string (match-string 1))))
  2644. (org-clock-update-mode-line)))
  2645. (t
  2646. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2647. (end-of-line 1)
  2648. (setq ts (match-string 1)
  2649. te (match-string 3))
  2650. (setq s (- (time-to-seconds
  2651. (apply 'encode-time (org-parse-time-string te)))
  2652. (time-to-seconds
  2653. (apply 'encode-time (org-parse-time-string ts))))
  2654. neg (< s 0)
  2655. s (abs s)
  2656. h (floor (/ s 3600))
  2657. s (- s (* 3600 h))
  2658. m (floor (/ s 60))
  2659. s (- s (* 60 s)))
  2660. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2661. t))))))
  2662. (defun org-check-running-clock ()
  2663. "Check if the current buffer contains the running clock.
  2664. If yes, offer to stop it and to save the buffer with the changes."
  2665. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2666. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2667. (buffer-name))))
  2668. (org-clock-out)
  2669. (when (y-or-n-p "Save changed buffer?")
  2670. (save-buffer))))
  2671. (defun org-clocktable-try-shift (dir n)
  2672. "Check if this line starts a clock table, if yes, shift the time block."
  2673. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2674. (org-clocktable-shift dir n)))
  2675. ;; Autoload org-timer.el
  2676. (eval-and-compile
  2677. (org-autoload
  2678. "org-timer"
  2679. '(org-timer-start org-timer org-timer-item
  2680. org-timer-change-times-in-region)))
  2681. ;; Autoload org-feed.el
  2682. (eval-and-compile
  2683. (org-autoload
  2684. "org-feed"
  2685. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2686. ;; Autoload archiving code
  2687. ;; The stuff that is needed for cycling and tags has to be defined here.
  2688. (defgroup org-archive nil
  2689. "Options concerning archiving in Org-mode."
  2690. :tag "Org Archive"
  2691. :group 'org-structure)
  2692. (defcustom org-archive-location "%s_archive::"
  2693. "The location where subtrees should be archived.
  2694. The value of this variable is a string, consisting of two parts,
  2695. separated by a double-colon. The first part is a filename and
  2696. the second part is a headline.
  2697. When the filename is omitted, archiving happens in the same file.
  2698. %s in the filename will be replaced by the current file
  2699. name (without the directory part). Archiving to a different file
  2700. is useful to keep archived entries from contributing to the
  2701. Org-mode Agenda.
  2702. The archived entries will be filed as subtrees of the specified
  2703. headline. When the headline is omitted, the subtrees are simply
  2704. filed away at the end of the file, as top-level entries. Also in
  2705. the heading you can use %s to represent the file name, this can be
  2706. useful when using the same archive for a number of different files.
  2707. Here are a few examples:
  2708. \"%s_archive::\"
  2709. If the current file is Projects.org, archive in file
  2710. Projects.org_archive, as top-level trees. This is the default.
  2711. \"::* Archived Tasks\"
  2712. Archive in the current file, under the top-level headline
  2713. \"* Archived Tasks\".
  2714. \"~/org/archive.org::\"
  2715. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2716. \"~/org/archive.org::From %s\"
  2717. Archive in file ~/org/archive.org (absolute path), und headlines
  2718. \"From FILENAME\" where file name is the current file name.
  2719. \"basement::** Finished Tasks\"
  2720. Archive in file ./basement (relative path), as level 3 trees
  2721. below the level 2 heading \"** Finished Tasks\".
  2722. You may set this option on a per-file basis by adding to the buffer a
  2723. line like
  2724. #+ARCHIVE: basement::** Finished Tasks
  2725. You may also define it locally for a subtree by setting an ARCHIVE property
  2726. in the entry. If such a property is found in an entry, or anywhere up
  2727. the hierarchy, it will be used."
  2728. :group 'org-archive
  2729. :type 'string)
  2730. (defcustom org-archive-tag "ARCHIVE"
  2731. "The tag that marks a subtree as archived.
  2732. An archived subtree does not open during visibility cycling, and does
  2733. not contribute to the agenda listings.
  2734. After changing this, font-lock must be restarted in the relevant buffers to
  2735. get the proper fontification."
  2736. :group 'org-archive
  2737. :group 'org-keywords
  2738. :type 'string)
  2739. (defcustom org-agenda-skip-archived-trees t
  2740. "Non-nil means, the agenda will skip any items located in archived trees.
  2741. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2742. variable is no longer recommended, you should leave it at the value t.
  2743. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2744. :group 'org-archive
  2745. :group 'org-agenda-skip
  2746. :type 'boolean)
  2747. (defcustom org-columns-skip-arrchived-trees t
  2748. "Non-nil means, irgnore archived trees when creating column view."
  2749. :group 'org-archive
  2750. :group 'org-properties
  2751. :type 'boolean)
  2752. (defcustom org-cycle-open-archived-trees nil
  2753. "Non-nil means, `org-cycle' will open archived trees.
  2754. An archived tree is a tree marked with the tag ARCHIVE.
  2755. When nil, archived trees will stay folded. You can still open them with
  2756. normal outline commands like `show-all', but not with the cycling commands."
  2757. :group 'org-archive
  2758. :group 'org-cycle
  2759. :type 'boolean)
  2760. (defcustom org-sparse-tree-open-archived-trees nil
  2761. "Non-nil means sparse tree construction shows matches in archived trees.
  2762. When nil, matches in these trees are highlighted, but the trees are kept in
  2763. collapsed state."
  2764. :group 'org-archive
  2765. :group 'org-sparse-trees
  2766. :type 'boolean)
  2767. (defun org-cycle-hide-archived-subtrees (state)
  2768. "Re-hide all archived subtrees after a visibility state change."
  2769. (when (and (not org-cycle-open-archived-trees)
  2770. (not (memq state '(overview folded))))
  2771. (save-excursion
  2772. (let* ((globalp (memq state '(contents all)))
  2773. (beg (if globalp (point-min) (point)))
  2774. (end (if globalp (point-max) (org-end-of-subtree t))))
  2775. (org-hide-archived-subtrees beg end)
  2776. (goto-char beg)
  2777. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2778. (message "%s" (substitute-command-keys
  2779. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2780. (defun org-force-cycle-archived ()
  2781. "Cycle subtree even if it is archived."
  2782. (interactive)
  2783. (setq this-command 'org-cycle)
  2784. (let ((org-cycle-open-archived-trees t))
  2785. (call-interactively 'org-cycle)))
  2786. (defun org-hide-archived-subtrees (beg end)
  2787. "Re-hide all archived subtrees after a visibility state change."
  2788. (save-excursion
  2789. (let* ((re (concat ":" org-archive-tag ":")))
  2790. (goto-char beg)
  2791. (while (re-search-forward re end t)
  2792. (and (org-on-heading-p) (hide-subtree))
  2793. (org-end-of-subtree t)))))
  2794. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2795. (eval-and-compile
  2796. (org-autoload "org-archive"
  2797. '(org-add-archive-files org-archive-subtree
  2798. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2799. ;; Autoload Column View Code
  2800. (declare-function org-columns-number-to-string "org-colview")
  2801. (declare-function org-columns-get-format-and-top-level "org-colview")
  2802. (declare-function org-columns-compute "org-colview")
  2803. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2804. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2805. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2806. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2807. ;; Autoload ID code
  2808. (declare-function org-id-store-link "org-id")
  2809. (declare-function org-id-locations-load "org-id")
  2810. (declare-function org-id-locations-save "org-id")
  2811. (defvar org-id-track-globally)
  2812. (org-autoload "org-id"
  2813. '(org-id-get-create org-id-new org-id-copy org-id-get
  2814. org-id-get-with-outline-path-completion
  2815. org-id-get-with-outline-drilling
  2816. org-id-goto org-id-find org-id-store-link))
  2817. ;; Autoload Plotting Code
  2818. (org-autoload "org-plot"
  2819. '(org-plot/gnuplot))
  2820. ;;; Variables for pre-computed regular expressions, all buffer local
  2821. (defvar org-drawer-regexp nil
  2822. "Matches first line of a hidden block.")
  2823. (make-variable-buffer-local 'org-drawer-regexp)
  2824. (defvar org-todo-regexp nil
  2825. "Matches any of the TODO state keywords.")
  2826. (make-variable-buffer-local 'org-todo-regexp)
  2827. (defvar org-not-done-regexp nil
  2828. "Matches any of the TODO state keywords except the last one.")
  2829. (make-variable-buffer-local 'org-not-done-regexp)
  2830. (defvar org-not-done-heading-regexp nil
  2831. "Matches a TODO headline that is not done.")
  2832. (make-variable-buffer-local 'org-not-done-regexp)
  2833. (defvar org-todo-line-regexp nil
  2834. "Matches a headline and puts TODO state into group 2 if present.")
  2835. (make-variable-buffer-local 'org-todo-line-regexp)
  2836. (defvar org-complex-heading-regexp nil
  2837. "Matches a headline and puts everything into groups:
  2838. group 1: the stars
  2839. group 2: The todo keyword, maybe
  2840. group 3: Priority cookie
  2841. group 4: True headline
  2842. group 5: Tags")
  2843. (make-variable-buffer-local 'org-complex-heading-regexp)
  2844. (defvar org-todo-line-tags-regexp nil
  2845. "Matches a headline and puts TODO state into group 2 if present.
  2846. Also put tags into group 4 if tags are present.")
  2847. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2848. (defvar org-nl-done-regexp nil
  2849. "Matches newline followed by a headline with the DONE keyword.")
  2850. (make-variable-buffer-local 'org-nl-done-regexp)
  2851. (defvar org-looking-at-done-regexp nil
  2852. "Matches the DONE keyword a point.")
  2853. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2854. (defvar org-ds-keyword-length 12
  2855. "Maximum length of the Deadline and SCHEDULED keywords.")
  2856. (make-variable-buffer-local 'org-ds-keyword-length)
  2857. (defvar org-deadline-regexp nil
  2858. "Matches the DEADLINE keyword.")
  2859. (make-variable-buffer-local 'org-deadline-regexp)
  2860. (defvar org-deadline-time-regexp nil
  2861. "Matches the DEADLINE keyword together with a time stamp.")
  2862. (make-variable-buffer-local 'org-deadline-time-regexp)
  2863. (defvar org-deadline-line-regexp nil
  2864. "Matches the DEADLINE keyword and the rest of the line.")
  2865. (make-variable-buffer-local 'org-deadline-line-regexp)
  2866. (defvar org-scheduled-regexp nil
  2867. "Matches the SCHEDULED keyword.")
  2868. (make-variable-buffer-local 'org-scheduled-regexp)
  2869. (defvar org-scheduled-time-regexp nil
  2870. "Matches the SCHEDULED keyword together with a time stamp.")
  2871. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2872. (defvar org-closed-time-regexp nil
  2873. "Matches the CLOSED keyword together with a time stamp.")
  2874. (make-variable-buffer-local 'org-closed-time-regexp)
  2875. (defvar org-keyword-time-regexp nil
  2876. "Matches any of the 4 keywords, together with the time stamp.")
  2877. (make-variable-buffer-local 'org-keyword-time-regexp)
  2878. (defvar org-keyword-time-not-clock-regexp nil
  2879. "Matches any of the 3 keywords, together with the time stamp.")
  2880. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2881. (defvar org-maybe-keyword-time-regexp nil
  2882. "Matches a timestamp, possibly preceeded by a keyword.")
  2883. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2884. (defvar org-planning-or-clock-line-re nil
  2885. "Matches a line with planning or clock info.")
  2886. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2887. (defconst org-plain-time-of-day-regexp
  2888. (concat
  2889. "\\(\\<[012]?[0-9]"
  2890. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2891. "\\(--?"
  2892. "\\(\\<[012]?[0-9]"
  2893. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2894. "\\)?")
  2895. "Regular expression to match a plain time or time range.
  2896. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2897. groups carry important information:
  2898. 0 the full match
  2899. 1 the first time, range or not
  2900. 8 the second time, if it is a range.")
  2901. (defconst org-plain-time-extension-regexp
  2902. (concat
  2903. "\\(\\<[012]?[0-9]"
  2904. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2905. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2906. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2907. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2908. groups carry important information:
  2909. 0 the full match
  2910. 7 hours of duration
  2911. 9 minutes of duration")
  2912. (defconst org-stamp-time-of-day-regexp
  2913. (concat
  2914. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2915. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2916. "\\(--?"
  2917. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2918. "Regular expression to match a timestamp time or time range.
  2919. After a match, the following groups carry important information:
  2920. 0 the full match
  2921. 1 date plus weekday, for backreferencing to make sure both times on same day
  2922. 2 the first time, range or not
  2923. 4 the second time, if it is a range.")
  2924. (defconst org-startup-options
  2925. '(("fold" org-startup-folded t)
  2926. ("overview" org-startup-folded t)
  2927. ("nofold" org-startup-folded nil)
  2928. ("showall" org-startup-folded nil)
  2929. ("content" org-startup-folded content)
  2930. ("hidestars" org-hide-leading-stars t)
  2931. ("showstars" org-hide-leading-stars nil)
  2932. ("odd" org-odd-levels-only t)
  2933. ("oddeven" org-odd-levels-only nil)
  2934. ("align" org-startup-align-all-tables t)
  2935. ("noalign" org-startup-align-all-tables nil)
  2936. ("customtime" org-display-custom-times t)
  2937. ("logdone" org-log-done time)
  2938. ("lognotedone" org-log-done note)
  2939. ("nologdone" org-log-done nil)
  2940. ("lognoteclock-out" org-log-note-clock-out t)
  2941. ("nolognoteclock-out" org-log-note-clock-out nil)
  2942. ("logrepeat" org-log-repeat state)
  2943. ("lognoterepeat" org-log-repeat note)
  2944. ("nologrepeat" org-log-repeat nil)
  2945. ("fninline" org-footnote-define-inline t)
  2946. ("nofninline" org-footnote-define-inline nil)
  2947. ("fnlocal" org-footnote-section nil)
  2948. ("fnauto" org-footnote-auto-label t)
  2949. ("fnprompt" org-footnote-auto-label nil)
  2950. ("fnconfirm" org-footnote-auto-label confirm)
  2951. ("fnplain" org-footnote-auto-label plain)
  2952. ("constcgs" constants-unit-system cgs)
  2953. ("constSI" constants-unit-system SI)
  2954. ("noptag" org-tag-persistent-alist nil))
  2955. "Variable associated with STARTUP options for org-mode.
  2956. Each element is a list of three items: The startup options as written
  2957. in the #+STARTUP line, the corresponding variable, and the value to
  2958. set this variable to if the option is found. An optional forth element PUSH
  2959. means to push this value onto the list in the variable.")
  2960. (defun org-set-regexps-and-options ()
  2961. "Precompute regular expressions for current buffer."
  2962. (when (org-mode-p)
  2963. (org-set-local 'org-todo-kwd-alist nil)
  2964. (org-set-local 'org-todo-key-alist nil)
  2965. (org-set-local 'org-todo-key-trigger nil)
  2966. (org-set-local 'org-todo-keywords-1 nil)
  2967. (org-set-local 'org-done-keywords nil)
  2968. (org-set-local 'org-todo-heads nil)
  2969. (org-set-local 'org-todo-sets nil)
  2970. (org-set-local 'org-todo-log-states nil)
  2971. (org-set-local 'org-file-properties nil)
  2972. (org-set-local 'org-file-tags nil)
  2973. (let ((re (org-make-options-regexp
  2974. '("CATEGORY" "TODO" "COLUMNS"
  2975. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2976. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2977. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2978. (splitre "[ \t]+")
  2979. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2980. tail sep kws1 prio props ftags drawers
  2981. ext-setup-or-nil setup-contents (start 0))
  2982. (save-excursion
  2983. (save-restriction
  2984. (widen)
  2985. (goto-char (point-min))
  2986. (while (or (and ext-setup-or-nil
  2987. (string-match re ext-setup-or-nil start)
  2988. (setq start (match-end 0)))
  2989. (and (setq ext-setup-or-nil nil start 0)
  2990. (re-search-forward re nil t)))
  2991. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2992. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2993. (cond
  2994. ((equal key "CATEGORY")
  2995. (if (string-match "[ \t]+$" value)
  2996. (setq value (replace-match "" t t value)))
  2997. (setq cat value))
  2998. ((member key '("SEQ_TODO" "TODO"))
  2999. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3000. ((equal key "TYP_TODO")
  3001. (push (cons 'type (org-split-string value splitre)) kwds))
  3002. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3003. ;; general TODO-like setup
  3004. (push (cons (intern (downcase (match-string 1 key)))
  3005. (org-split-string value splitre)) kwds))
  3006. ((equal key "TAGS")
  3007. (setq tags (append tags (if tags '("\\n") nil)
  3008. (org-split-string value splitre))))
  3009. ((equal key "COLUMNS")
  3010. (org-set-local 'org-columns-default-format value))
  3011. ((equal key "LINK")
  3012. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3013. (push (cons (match-string 1 value)
  3014. (org-trim (match-string 2 value)))
  3015. links)))
  3016. ((equal key "PRIORITIES")
  3017. (setq prio (org-split-string value " +")))
  3018. ((equal key "PROPERTY")
  3019. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3020. (push (cons (match-string 1 value) (match-string 2 value))
  3021. props)))
  3022. ((equal key "FILETAGS")
  3023. (when (string-match "\\S-" value)
  3024. (setq ftags
  3025. (append
  3026. ftags
  3027. (apply 'append
  3028. (mapcar (lambda (x) (org-split-string x ":"))
  3029. (org-split-string value)))))))
  3030. ((equal key "DRAWERS")
  3031. (setq drawers (org-split-string value splitre)))
  3032. ((equal key "CONSTANTS")
  3033. (setq const (append const (org-split-string value splitre))))
  3034. ((equal key "STARTUP")
  3035. (let ((opts (org-split-string value splitre))
  3036. l var val)
  3037. (while (setq l (pop opts))
  3038. (when (setq l (assoc l org-startup-options))
  3039. (setq var (nth 1 l) val (nth 2 l))
  3040. (if (not (nth 3 l))
  3041. (set (make-local-variable var) val)
  3042. (if (not (listp (symbol-value var)))
  3043. (set (make-local-variable var) nil))
  3044. (set (make-local-variable var) (symbol-value var))
  3045. (add-to-list var val))))))
  3046. ((equal key "ARCHIVE")
  3047. (string-match " *$" value)
  3048. (setq arch (replace-match "" t t value))
  3049. (remove-text-properties 0 (length arch)
  3050. '(face t fontified t) arch))
  3051. ((equal key "SETUPFILE")
  3052. (setq setup-contents (org-file-contents
  3053. (expand-file-name
  3054. (org-remove-double-quotes value))
  3055. 'noerror))
  3056. (if (not ext-setup-or-nil)
  3057. (setq ext-setup-or-nil setup-contents start 0)
  3058. (setq ext-setup-or-nil
  3059. (concat (substring ext-setup-or-nil 0 start)
  3060. "\n" setup-contents "\n"
  3061. (substring ext-setup-or-nil start)))))
  3062. ))))
  3063. (when cat
  3064. (org-set-local 'org-category (intern cat))
  3065. (push (cons "CATEGORY" cat) props))
  3066. (when prio
  3067. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3068. (setq prio (mapcar 'string-to-char prio))
  3069. (org-set-local 'org-highest-priority (nth 0 prio))
  3070. (org-set-local 'org-lowest-priority (nth 1 prio))
  3071. (org-set-local 'org-default-priority (nth 2 prio)))
  3072. (and props (org-set-local 'org-file-properties (nreverse props)))
  3073. (and ftags (org-set-local 'org-file-tags
  3074. (mapcar 'org-add-prop-inherited ftags)))
  3075. (and drawers (org-set-local 'org-drawers drawers))
  3076. (and arch (org-set-local 'org-archive-location arch))
  3077. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3078. ;; Process the TODO keywords
  3079. (unless kwds
  3080. ;; Use the global values as if they had been given locally.
  3081. (setq kwds (default-value 'org-todo-keywords))
  3082. (if (stringp (car kwds))
  3083. (setq kwds (list (cons org-todo-interpretation
  3084. (default-value 'org-todo-keywords)))))
  3085. (setq kwds (reverse kwds)))
  3086. (setq kwds (nreverse kwds))
  3087. (let (inter kws kw)
  3088. (while (setq kws (pop kwds))
  3089. (let ((kws (or
  3090. (run-hook-with-args-until-success
  3091. 'org-todo-setup-filter-hook kws)
  3092. kws)))
  3093. (setq inter (pop kws) sep (member "|" kws)
  3094. kws0 (delete "|" (copy-sequence kws))
  3095. kwsa nil
  3096. kws1 (mapcar
  3097. (lambda (x)
  3098. ;; 1 2
  3099. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3100. (progn
  3101. (setq kw (match-string 1 x)
  3102. key (and (match-end 2) (match-string 2 x))
  3103. log (org-extract-log-state-settings x))
  3104. (push (cons kw (and key (string-to-char key))) kwsa)
  3105. (and log (push log org-todo-log-states))
  3106. kw)
  3107. (error "Invalid TODO keyword %s" x)))
  3108. kws0)
  3109. kwsa (if kwsa (append '((:startgroup))
  3110. (nreverse kwsa)
  3111. '((:endgroup))))
  3112. hw (car kws1)
  3113. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3114. tail (list inter hw (car dws) (org-last dws))))
  3115. (add-to-list 'org-todo-heads hw 'append)
  3116. (push kws1 org-todo-sets)
  3117. (setq org-done-keywords (append org-done-keywords dws nil))
  3118. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3119. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3120. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3121. (setq org-todo-sets (nreverse org-todo-sets)
  3122. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3123. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3124. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3125. ;; Process the constants
  3126. (when const
  3127. (let (e cst)
  3128. (while (setq e (pop const))
  3129. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3130. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3131. (setq org-table-formula-constants-local cst)))
  3132. ;; Process the tags.
  3133. (when tags
  3134. (let (e tgs)
  3135. (while (setq e (pop tags))
  3136. (cond
  3137. ((equal e "{") (push '(:startgroup) tgs))
  3138. ((equal e "}") (push '(:endgroup) tgs))
  3139. ((equal e "\\n") (push '(:newline) tgs))
  3140. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3141. (push (cons (match-string 1 e)
  3142. (string-to-char (match-string 2 e)))
  3143. tgs))
  3144. (t (push (list e) tgs))))
  3145. (org-set-local 'org-tag-alist nil)
  3146. (while (setq e (pop tgs))
  3147. (or (and (stringp (car e))
  3148. (assoc (car e) org-tag-alist))
  3149. (push e org-tag-alist)))))
  3150. ;; Compute the regular expressions and other local variables
  3151. (if (not org-done-keywords)
  3152. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3153. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3154. (length org-scheduled-string)
  3155. (length org-clock-string)
  3156. (length org-closed-string)))
  3157. org-drawer-regexp
  3158. (concat "^[ \t]*:\\("
  3159. (mapconcat 'regexp-quote org-drawers "\\|")
  3160. "\\):[ \t]*$")
  3161. org-not-done-keywords
  3162. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3163. org-todo-regexp
  3164. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3165. "\\|") "\\)\\>")
  3166. org-not-done-regexp
  3167. (concat "\\<\\("
  3168. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3169. "\\)\\>")
  3170. org-not-done-heading-regexp
  3171. (concat "^\\(\\*+\\)[ \t]+\\("
  3172. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3173. "\\)\\>")
  3174. org-todo-line-regexp
  3175. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3176. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3177. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3178. org-complex-heading-regexp
  3179. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3180. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3181. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3182. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3183. org-nl-done-regexp
  3184. (concat "\n\\*+[ \t]+"
  3185. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3186. "\\)" "\\>")
  3187. org-todo-line-tags-regexp
  3188. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3189. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3190. (org-re
  3191. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3192. org-looking-at-done-regexp
  3193. (concat "^" "\\(?:"
  3194. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3195. "\\>")
  3196. org-deadline-regexp (concat "\\<" org-deadline-string)
  3197. org-deadline-time-regexp
  3198. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3199. org-deadline-line-regexp
  3200. (concat "\\<\\(" org-deadline-string "\\).*")
  3201. org-scheduled-regexp
  3202. (concat "\\<" org-scheduled-string)
  3203. org-scheduled-time-regexp
  3204. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3205. org-closed-time-regexp
  3206. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3207. org-keyword-time-regexp
  3208. (concat "\\<\\(" org-scheduled-string
  3209. "\\|" org-deadline-string
  3210. "\\|" org-closed-string
  3211. "\\|" org-clock-string "\\)"
  3212. " *[[<]\\([^]>]+\\)[]>]")
  3213. org-keyword-time-not-clock-regexp
  3214. (concat "\\<\\(" org-scheduled-string
  3215. "\\|" org-deadline-string
  3216. "\\|" org-closed-string
  3217. "\\)"
  3218. " *[[<]\\([^]>]+\\)[]>]")
  3219. org-maybe-keyword-time-regexp
  3220. (concat "\\(\\<\\(" org-scheduled-string
  3221. "\\|" org-deadline-string
  3222. "\\|" org-closed-string
  3223. "\\|" org-clock-string "\\)\\)?"
  3224. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3225. org-planning-or-clock-line-re
  3226. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3227. "\\|" org-deadline-string
  3228. "\\|" org-closed-string "\\|" org-clock-string
  3229. "\\)\\>\\)")
  3230. )
  3231. (org-compute-latex-and-specials-regexp)
  3232. (org-set-font-lock-defaults))))
  3233. (defun org-file-contents (file &optional noerror)
  3234. "Return the contents of FILE, as a string."
  3235. (if (or (not file)
  3236. (not (file-readable-p file)))
  3237. (if noerror
  3238. (progn
  3239. (message "Cannot read file %s" file)
  3240. (ding) (sit-for 2)
  3241. "")
  3242. (error "Cannot read file %s" file))
  3243. (with-temp-buffer
  3244. (insert-file-contents file)
  3245. (buffer-string))))
  3246. (defun org-extract-log-state-settings (x)
  3247. "Extract the log state setting from a TODO keyword string.
  3248. This will extract info from a string like \"WAIT(w@/!)\"."
  3249. (let (kw key log1 log2)
  3250. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3251. (setq kw (match-string 1 x)
  3252. key (and (match-end 2) (match-string 2 x))
  3253. log1 (and (match-end 3) (match-string 3 x))
  3254. log2 (and (match-end 4) (match-string 4 x)))
  3255. (and (or log1 log2)
  3256. (list kw
  3257. (and log1 (if (equal log1 "!") 'time 'note))
  3258. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3259. (defun org-remove-keyword-keys (list)
  3260. "Remove a pair of parenthesis at the end of each string in LIST."
  3261. (mapcar (lambda (x)
  3262. (if (string-match "(.*)$" x)
  3263. (substring x 0 (match-beginning 0))
  3264. x))
  3265. list))
  3266. ;; FIXME: this could be done much better, using second characters etc.
  3267. (defun org-assign-fast-keys (alist)
  3268. "Assign fast keys to a keyword-key alist.
  3269. Respect keys that are already there."
  3270. (let (new e k c c1 c2 (char ?a))
  3271. (while (setq e (pop alist))
  3272. (cond
  3273. ((equal e '(:startgroup)) (push e new))
  3274. ((equal e '(:endgroup)) (push e new))
  3275. ((equal e '(:newline)) (push e new))
  3276. (t
  3277. (setq k (car e) c2 nil)
  3278. (if (cdr e)
  3279. (setq c (cdr e))
  3280. ;; automatically assign a character.
  3281. (setq c1 (string-to-char
  3282. (downcase (substring
  3283. k (if (= (string-to-char k) ?@) 1 0)))))
  3284. (if (or (rassoc c1 new) (rassoc c1 alist))
  3285. (while (or (rassoc char new) (rassoc char alist))
  3286. (setq char (1+ char)))
  3287. (setq c2 c1))
  3288. (setq c (or c2 char)))
  3289. (push (cons k c) new))))
  3290. (nreverse new)))
  3291. ;;; Some variables used in various places
  3292. (defvar org-window-configuration nil
  3293. "Used in various places to store a window configuration.")
  3294. (defvar org-finish-function nil
  3295. "Function to be called when `C-c C-c' is used.
  3296. This is for getting out of special buffers like remember.")
  3297. ;; FIXME: Occasionally check by commenting these, to make sure
  3298. ;; no other functions uses these, forgetting to let-bind them.
  3299. (defvar entry)
  3300. (defvar state)
  3301. (defvar last-state)
  3302. (defvar date)
  3303. (defvar description)
  3304. ;; Defined somewhere in this file, but used before definition.
  3305. (defvar org-html-entities)
  3306. (defvar org-struct-menu)
  3307. (defvar org-org-menu)
  3308. (defvar org-tbl-menu)
  3309. (defvar org-agenda-keymap)
  3310. ;;;; Define the Org-mode
  3311. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3312. (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."))
  3313. ;; We use a before-change function to check if a table might need
  3314. ;; an update.
  3315. (defvar org-table-may-need-update t
  3316. "Indicates that a table might need an update.
  3317. This variable is set by `org-before-change-function'.
  3318. `org-table-align' sets it back to nil.")
  3319. (defun org-before-change-function (beg end)
  3320. "Every change indicates that a table might need an update."
  3321. (setq org-table-may-need-update t))
  3322. (defvar org-mode-map)
  3323. (defvar org-mode-hook nil
  3324. "Mode hook for Org-mode, run after the mode was turned on.")
  3325. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3326. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3327. (defvar org-table-buffer-is-an nil)
  3328. (defconst org-outline-regexp "\\*+ ")
  3329. ;;;###autoload
  3330. (define-derived-mode org-mode outline-mode "Org"
  3331. "Outline-based notes management and organizer, alias
  3332. \"Carsten's outline-mode for keeping track of everything.\"
  3333. Org-mode develops organizational tasks around a NOTES file which
  3334. contains information about projects as plain text. Org-mode is
  3335. implemented on top of outline-mode, which is ideal to keep the content
  3336. of large files well structured. It supports ToDo items, deadlines and
  3337. time stamps, which magically appear in the diary listing of the Emacs
  3338. calendar. Tables are easily created with a built-in table editor.
  3339. Plain text URL-like links connect to websites, emails (VM), Usenet
  3340. messages (Gnus), BBDB entries, and any files related to the project.
  3341. For printing and sharing of notes, an Org-mode file (or a part of it)
  3342. can be exported as a structured ASCII or HTML file.
  3343. The following commands are available:
  3344. \\{org-mode-map}"
  3345. ;; Get rid of Outline menus, they are not needed
  3346. ;; Need to do this here because define-derived-mode sets up
  3347. ;; the keymap so late. Still, it is a waste to call this each time
  3348. ;; we switch another buffer into org-mode.
  3349. (if (featurep 'xemacs)
  3350. (when (boundp 'outline-mode-menu-heading)
  3351. ;; Assume this is Greg's port, it used easymenu
  3352. (easy-menu-remove outline-mode-menu-heading)
  3353. (easy-menu-remove outline-mode-menu-show)
  3354. (easy-menu-remove outline-mode-menu-hide))
  3355. (define-key org-mode-map [menu-bar headings] 'undefined)
  3356. (define-key org-mode-map [menu-bar hide] 'undefined)
  3357. (define-key org-mode-map [menu-bar show] 'undefined))
  3358. (org-load-modules-maybe)
  3359. (easy-menu-add org-org-menu)
  3360. (easy-menu-add org-tbl-menu)
  3361. (org-install-agenda-files-menu)
  3362. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3363. (org-add-to-invisibility-spec '(org-cwidth))
  3364. (when (featurep 'xemacs)
  3365. (org-set-local 'line-move-ignore-invisible t))
  3366. (org-set-local 'outline-regexp org-outline-regexp)
  3367. (org-set-local 'outline-level 'org-outline-level)
  3368. (when (and org-ellipsis
  3369. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3370. (fboundp 'make-glyph-code))
  3371. (unless org-display-table
  3372. (setq org-display-table (make-display-table)))
  3373. (set-display-table-slot
  3374. org-display-table 4
  3375. (vconcat (mapcar
  3376. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3377. org-ellipsis)))
  3378. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3379. (setq buffer-display-table org-display-table))
  3380. (org-set-regexps-and-options)
  3381. (when (and org-tag-faces (not org-tags-special-faces-re))
  3382. ;; tag faces set outside customize.... force initialization.
  3383. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3384. ;; Calc embedded
  3385. (org-set-local 'calc-embedded-open-mode "# ")
  3386. (modify-syntax-entry ?# "<")
  3387. (modify-syntax-entry ?@ "w")
  3388. (if org-startup-truncated (setq truncate-lines t))
  3389. (org-set-local 'font-lock-unfontify-region-function
  3390. 'org-unfontify-region)
  3391. ;; Activate before-change-function
  3392. (org-set-local 'org-table-may-need-update t)
  3393. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3394. 'local)
  3395. ;; Check for running clock before killing a buffer
  3396. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3397. ;; Paragraphs and auto-filling
  3398. (org-set-autofill-regexps)
  3399. (setq indent-line-function 'org-indent-line-function)
  3400. (org-update-radio-target-regexp)
  3401. ;; Make sure dependence stuff works reliably, even for users who set it
  3402. ;; too late :-(
  3403. (if org-enforce-todo-dependencies
  3404. (add-hook 'org-blocker-hook
  3405. 'org-block-todo-from-children-or-siblings-or-parent)
  3406. (remove-hook 'org-blocker-hook
  3407. 'org-block-todo-from-children-or-siblings-or-parent))
  3408. (if org-enforce-todo-checkbox-dependencies
  3409. (add-hook 'org-blocker-hook
  3410. 'org-block-todo-from-checkboxes)
  3411. (remove-hook 'org-blocker-hook
  3412. 'org-block-todo-from-checkboxes))
  3413. ;; Comment characters
  3414. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3415. (org-set-local 'comment-padding " ")
  3416. ;; Align options lines
  3417. (org-set-local
  3418. 'align-mode-rules-list
  3419. '((org-in-buffer-settings
  3420. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3421. (modes . '(org-mode)))))
  3422. ;; Imenu
  3423. (org-set-local 'imenu-create-index-function
  3424. 'org-imenu-get-tree)
  3425. ;; Make isearch reveal context
  3426. (if (or (featurep 'xemacs)
  3427. (not (boundp 'outline-isearch-open-invisible-function)))
  3428. ;; Emacs 21 and XEmacs make use of the hook
  3429. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3430. ;; Emacs 22 deals with this through a special variable
  3431. (org-set-local 'outline-isearch-open-invisible-function
  3432. (lambda (&rest ignore) (org-show-context 'isearch))))
  3433. ;; If empty file that did not turn on org-mode automatically, make it to.
  3434. (if (and org-insert-mode-line-in-empty-file
  3435. (interactive-p)
  3436. (= (point-min) (point-max)))
  3437. (insert "# -*- mode: org -*-\n\n"))
  3438. (unless org-inhibit-startup
  3439. (when org-startup-align-all-tables
  3440. (let ((bmp (buffer-modified-p)))
  3441. (org-table-map-tables 'org-table-align)
  3442. (set-buffer-modified-p bmp)))
  3443. (org-set-startup-visibility)))
  3444. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3445. (defun org-current-time ()
  3446. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3447. (if (> (car org-time-stamp-rounding-minutes) 1)
  3448. (let ((r (car org-time-stamp-rounding-minutes))
  3449. (time (decode-time)))
  3450. (apply 'encode-time
  3451. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3452. (nthcdr 2 time))))
  3453. (current-time)))
  3454. ;;;; Font-Lock stuff, including the activators
  3455. (defvar org-mouse-map (make-sparse-keymap))
  3456. (org-defkey org-mouse-map
  3457. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3458. (org-defkey org-mouse-map
  3459. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3460. (when org-mouse-1-follows-link
  3461. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3462. (when org-tab-follows-link
  3463. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3464. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3465. (require 'font-lock)
  3466. (defconst org-non-link-chars "]\t\n\r<>")
  3467. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3468. "shell" "elisp"))
  3469. (defvar org-link-types-re nil
  3470. "Matches a link that has a url-like prefix like \"http:\"")
  3471. (defvar org-link-re-with-space nil
  3472. "Matches a link with spaces, optional angular brackets around it.")
  3473. (defvar org-link-re-with-space2 nil
  3474. "Matches a link with spaces, optional angular brackets around it.")
  3475. (defvar org-link-re-with-space3 nil
  3476. "Matches a link with spaces, only for internal part in bracket links.")
  3477. (defvar org-angle-link-re nil
  3478. "Matches link with angular brackets, spaces are allowed.")
  3479. (defvar org-plain-link-re nil
  3480. "Matches plain link, without spaces.")
  3481. (defvar org-bracket-link-regexp nil
  3482. "Matches a link in double brackets.")
  3483. (defvar org-bracket-link-analytic-regexp nil
  3484. "Regular expression used to analyze links.
  3485. Here is what the match groups contain after a match:
  3486. 1: http:
  3487. 2: http
  3488. 3: path
  3489. 4: [desc]
  3490. 5: desc")
  3491. (defvar org-bracket-link-analytic-regexp++ nil
  3492. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3493. (defvar org-any-link-re nil
  3494. "Regular expression matching any link.")
  3495. (defun org-make-link-regexps ()
  3496. "Update the link regular expressions.
  3497. This should be called after the variable `org-link-types' has changed."
  3498. (setq org-link-types-re
  3499. (concat
  3500. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3501. org-link-re-with-space
  3502. (concat
  3503. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3504. "\\([^" org-non-link-chars " ]"
  3505. "[^" org-non-link-chars "]*"
  3506. "[^" org-non-link-chars " ]\\)>?")
  3507. org-link-re-with-space2
  3508. (concat
  3509. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3510. "\\([^" org-non-link-chars " ]"
  3511. "[^\t\n\r]*"
  3512. "[^" org-non-link-chars " ]\\)>?")
  3513. org-link-re-with-space3
  3514. (concat
  3515. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3516. "\\([^" org-non-link-chars " ]"
  3517. "[^\t\n\r]*\\)")
  3518. org-angle-link-re
  3519. (concat
  3520. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3521. "\\([^" org-non-link-chars " ]"
  3522. "[^" org-non-link-chars "]*"
  3523. "\\)>")
  3524. org-plain-link-re
  3525. (concat
  3526. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3527. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3528. org-bracket-link-regexp
  3529. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3530. org-bracket-link-analytic-regexp
  3531. (concat
  3532. "\\[\\["
  3533. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3534. "\\([^]]+\\)"
  3535. "\\]"
  3536. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3537. "\\]")
  3538. org-bracket-link-analytic-regexp++
  3539. (concat
  3540. "\\[\\["
  3541. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3542. "\\([^]]+\\)"
  3543. "\\]"
  3544. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3545. "\\]")
  3546. org-any-link-re
  3547. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3548. org-angle-link-re "\\)\\|\\("
  3549. org-plain-link-re "\\)")))
  3550. (org-make-link-regexps)
  3551. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3552. "Regular expression for fast time stamp matching.")
  3553. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3554. "Regular expression for fast time stamp matching.")
  3555. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3556. "Regular expression matching time strings for analysis.
  3557. This one does not require the space after the date, so it can be used
  3558. on a string that terminates immediately after the date.")
  3559. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3560. "Regular expression matching time strings for analysis.")
  3561. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3562. "Regular expression matching time stamps, with groups.")
  3563. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3564. "Regular expression matching time stamps (also [..]), with groups.")
  3565. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3566. "Regular expression matching a time stamp range.")
  3567. (defconst org-tr-regexp-both
  3568. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3569. "Regular expression matching a time stamp range.")
  3570. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3571. org-ts-regexp "\\)?")
  3572. "Regular expression matching a time stamp or time stamp range.")
  3573. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3574. org-ts-regexp-both "\\)?")
  3575. "Regular expression matching a time stamp or time stamp range.
  3576. The time stamps may be either active or inactive.")
  3577. (defvar org-emph-face nil)
  3578. (defun org-do-emphasis-faces (limit)
  3579. "Run through the buffer and add overlays to links."
  3580. (let (rtn)
  3581. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3582. (if (not (= (char-after (match-beginning 3))
  3583. (char-after (match-beginning 4))))
  3584. (progn
  3585. (setq rtn t)
  3586. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3587. 'face
  3588. (nth 1 (assoc (match-string 3)
  3589. org-emphasis-alist)))
  3590. (add-text-properties (match-beginning 2) (match-end 2)
  3591. '(font-lock-multiline t))
  3592. (when org-hide-emphasis-markers
  3593. (add-text-properties (match-end 4) (match-beginning 5)
  3594. '(invisible org-link))
  3595. (add-text-properties (match-beginning 3) (match-end 3)
  3596. '(invisible org-link)))))
  3597. (backward-char 1))
  3598. rtn))
  3599. (defun org-emphasize (&optional char)
  3600. "Insert or change an emphasis, i.e. a font like bold or italic.
  3601. If there is an active region, change that region to a new emphasis.
  3602. If there is no region, just insert the marker characters and position
  3603. the cursor between them.
  3604. CHAR should be either the marker character, or the first character of the
  3605. HTML tag associated with that emphasis. If CHAR is a space, the means
  3606. to remove the emphasis of the selected region.
  3607. If char is not given (for example in an interactive call) it
  3608. will be prompted for."
  3609. (interactive)
  3610. (let ((eal org-emphasis-alist) e det
  3611. (erc org-emphasis-regexp-components)
  3612. (prompt "")
  3613. (string "") beg end move tag c s)
  3614. (if (org-region-active-p)
  3615. (setq beg (region-beginning) end (region-end)
  3616. string (buffer-substring beg end))
  3617. (setq move t))
  3618. (while (setq e (pop eal))
  3619. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3620. c (aref tag 0))
  3621. (push (cons c (string-to-char (car e))) det)
  3622. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3623. (substring tag 1)))))
  3624. (setq det (nreverse det))
  3625. (unless char
  3626. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3627. (setq char (read-char-exclusive)))
  3628. (setq char (or (cdr (assoc char det)) char))
  3629. (if (equal char ?\ )
  3630. (setq s "" move nil)
  3631. (unless (assoc (char-to-string char) org-emphasis-alist)
  3632. (error "No such emphasis marker: \"%c\"" char))
  3633. (setq s (char-to-string char)))
  3634. (while (and (> (length string) 1)
  3635. (equal (substring string 0 1) (substring string -1))
  3636. (assoc (substring string 0 1) org-emphasis-alist))
  3637. (setq string (substring string 1 -1)))
  3638. (setq string (concat s string s))
  3639. (if beg (delete-region beg end))
  3640. (unless (or (bolp)
  3641. (string-match (concat "[" (nth 0 erc) "\n]")
  3642. (char-to-string (char-before (point)))))
  3643. (insert " "))
  3644. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3645. (char-to-string (char-after (point))))
  3646. (insert " ") (backward-char 1))
  3647. (insert string)
  3648. (and move (backward-char 1))))
  3649. (defconst org-nonsticky-props
  3650. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3651. (defun org-activate-plain-links (limit)
  3652. "Run through the buffer and add overlays to links."
  3653. (catch 'exit
  3654. (let (f)
  3655. (while (re-search-forward org-plain-link-re limit t)
  3656. (setq f (get-text-property (match-beginning 0) 'face))
  3657. (if (or (eq f 'org-tag)
  3658. (and (listp f) (memq 'org-tag f)))
  3659. nil
  3660. (add-text-properties (match-beginning 0) (match-end 0)
  3661. (list 'mouse-face 'highlight
  3662. 'rear-nonsticky org-nonsticky-props
  3663. 'keymap org-mouse-map
  3664. ))
  3665. (throw 'exit t))))))
  3666. (defun org-activate-code (limit)
  3667. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3668. (progn
  3669. (remove-text-properties (match-beginning 0) (match-end 0)
  3670. '(display t invisible t intangible t))
  3671. t)))
  3672. (defun org-activate-angle-links (limit)
  3673. "Run through the buffer and add overlays to links."
  3674. (if (re-search-forward org-angle-link-re limit t)
  3675. (progn
  3676. (add-text-properties (match-beginning 0) (match-end 0)
  3677. (list 'mouse-face 'highlight
  3678. 'rear-nonsticky org-nonsticky-props
  3679. 'keymap org-mouse-map
  3680. ))
  3681. t)))
  3682. (defun org-activate-footnote-links (limit)
  3683. "Run through the buffer and add overlays to links."
  3684. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3685. limit t)
  3686. (progn
  3687. (add-text-properties (match-beginning 2) (match-end 2)
  3688. (list 'mouse-face 'highlight
  3689. 'rear-nonsticky org-nonsticky-props
  3690. 'keymap org-mouse-map
  3691. 'help-echo
  3692. (if (= (point-at-bol) (match-beginning 2))
  3693. "Footnote definition"
  3694. "Footnote reference")
  3695. ))
  3696. t)))
  3697. (defun org-activate-bracket-links (limit)
  3698. "Run through the buffer and add overlays to bracketed links."
  3699. (if (re-search-forward org-bracket-link-regexp limit t)
  3700. (let* ((help (concat "LINK: "
  3701. (org-match-string-no-properties 1)))
  3702. ;; FIXME: above we should remove the escapes.
  3703. ;; but that requires another match, protecting match data,
  3704. ;; a lot of overhead for font-lock.
  3705. (ip (org-maybe-intangible
  3706. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3707. 'keymap org-mouse-map 'mouse-face 'highlight
  3708. 'font-lock-multiline t 'help-echo help)))
  3709. (vp (list 'rear-nonsticky org-nonsticky-props
  3710. 'keymap org-mouse-map 'mouse-face 'highlight
  3711. 'font-lock-multiline t 'help-echo help)))
  3712. ;; We need to remove the invisible property here. Table narrowing
  3713. ;; may have made some of this invisible.
  3714. (remove-text-properties (match-beginning 0) (match-end 0)
  3715. '(invisible nil))
  3716. (if (match-end 3)
  3717. (progn
  3718. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3719. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3720. (add-text-properties (match-end 3) (match-end 0) ip))
  3721. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3722. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3723. (add-text-properties (match-end 1) (match-end 0) ip))
  3724. t)))
  3725. (defun org-activate-dates (limit)
  3726. "Run through the buffer and add overlays to dates."
  3727. (if (re-search-forward org-tsr-regexp-both limit t)
  3728. (progn
  3729. (add-text-properties (match-beginning 0) (match-end 0)
  3730. (list 'mouse-face 'highlight
  3731. 'rear-nonsticky org-nonsticky-props
  3732. 'keymap org-mouse-map))
  3733. (when org-display-custom-times
  3734. (if (match-end 3)
  3735. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3736. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3737. t)))
  3738. (defvar org-target-link-regexp nil
  3739. "Regular expression matching radio targets in plain text.")
  3740. (make-variable-buffer-local 'org-target-link-regexp)
  3741. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3742. "Regular expression matching a link target.")
  3743. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3744. "Regular expression matching a radio target.")
  3745. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3746. "Regular expression matching any target.")
  3747. (defun org-activate-target-links (limit)
  3748. "Run through the buffer and add overlays to target matches."
  3749. (when org-target-link-regexp
  3750. (let ((case-fold-search t))
  3751. (if (re-search-forward org-target-link-regexp limit t)
  3752. (progn
  3753. (add-text-properties (match-beginning 0) (match-end 0)
  3754. (list 'mouse-face 'highlight
  3755. 'rear-nonsticky org-nonsticky-props
  3756. 'keymap org-mouse-map
  3757. 'help-echo "Radio target link"
  3758. 'org-linked-text t))
  3759. t)))))
  3760. (defun org-update-radio-target-regexp ()
  3761. "Find all radio targets in this file and update the regular expression."
  3762. (interactive)
  3763. (when (memq 'radio org-activate-links)
  3764. (setq org-target-link-regexp
  3765. (org-make-target-link-regexp (org-all-targets 'radio)))
  3766. (org-restart-font-lock)))
  3767. (defun org-hide-wide-columns (limit)
  3768. (let (s e)
  3769. (setq s (text-property-any (point) (or limit (point-max))
  3770. 'org-cwidth t))
  3771. (when s
  3772. (setq e (next-single-property-change s 'org-cwidth))
  3773. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3774. (goto-char e)
  3775. t)))
  3776. (defvar org-latex-and-specials-regexp nil
  3777. "Regular expression for highlighting export special stuff.")
  3778. (defvar org-match-substring-regexp)
  3779. (defvar org-match-substring-with-braces-regexp)
  3780. (defvar org-export-html-special-string-regexps)
  3781. (defun org-compute-latex-and-specials-regexp ()
  3782. "Compute regular expression for stuff treated specially by exporters."
  3783. (if (not org-highlight-latex-fragments-and-specials)
  3784. (org-set-local 'org-latex-and-specials-regexp nil)
  3785. (require 'org-exp)
  3786. (let*
  3787. ((matchers (plist-get org-format-latex-options :matchers))
  3788. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3789. org-latex-regexps)))
  3790. (options (org-combine-plists (org-default-export-plist)
  3791. (org-infile-export-plist)))
  3792. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3793. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3794. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3795. (org-export-html-expand (plist-get options :expand-quoted-html))
  3796. (org-export-with-special-strings (plist-get options :special-strings))
  3797. (re-sub
  3798. (cond
  3799. ((equal org-export-with-sub-superscripts '{})
  3800. (list org-match-substring-with-braces-regexp))
  3801. (org-export-with-sub-superscripts
  3802. (list org-match-substring-regexp))
  3803. (t nil)))
  3804. (re-latex
  3805. (if org-export-with-LaTeX-fragments
  3806. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3807. (re-macros
  3808. (if org-export-with-TeX-macros
  3809. (list (concat "\\\\"
  3810. (regexp-opt
  3811. (append (mapcar 'car org-html-entities)
  3812. (if (boundp 'org-latex-entities)
  3813. org-latex-entities nil))
  3814. 'words))) ; FIXME
  3815. ))
  3816. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3817. (re-special (if org-export-with-special-strings
  3818. (mapcar (lambda (x) (car x))
  3819. org-export-html-special-string-regexps)))
  3820. (re-rest
  3821. (delq nil
  3822. (list
  3823. (if org-export-html-expand "@<[^>\n]+>")
  3824. ))))
  3825. (org-set-local
  3826. 'org-latex-and-specials-regexp
  3827. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3828. re-rest) "\\|")))))
  3829. (defun org-do-latex-and-special-faces (limit)
  3830. "Run through the buffer and add overlays to links."
  3831. (when org-latex-and-specials-regexp
  3832. (let (rtn d)
  3833. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3834. limit t))
  3835. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3836. 'face))
  3837. '(org-code org-verbatim underline)))
  3838. (progn
  3839. (setq rtn t
  3840. d (cond ((member (char-after (1+ (match-beginning 0)))
  3841. '(?_ ?^)) 1)
  3842. (t 0)))
  3843. (font-lock-prepend-text-property
  3844. (+ d (match-beginning 0)) (match-end 0)
  3845. 'face 'org-latex-and-export-specials)
  3846. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3847. '(font-lock-multiline t)))))
  3848. rtn)))
  3849. (defun org-restart-font-lock ()
  3850. "Restart font-lock-mode, to force refontification."
  3851. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3852. (font-lock-mode -1)
  3853. (font-lock-mode 1)))
  3854. (defun org-all-targets (&optional radio)
  3855. "Return a list of all targets in this file.
  3856. With optional argument RADIO, only find radio targets."
  3857. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3858. rtn)
  3859. (save-excursion
  3860. (goto-char (point-min))
  3861. (while (re-search-forward re nil t)
  3862. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3863. rtn)))
  3864. (defun org-make-target-link-regexp (targets)
  3865. "Make regular expression matching all strings in TARGETS.
  3866. The regular expression finds the targets also if there is a line break
  3867. between words."
  3868. (and targets
  3869. (concat
  3870. "\\<\\("
  3871. (mapconcat
  3872. (lambda (x)
  3873. (while (string-match " +" x)
  3874. (setq x (replace-match "\\s-+" t t x)))
  3875. x)
  3876. targets
  3877. "\\|")
  3878. "\\)\\>")))
  3879. (defun org-activate-tags (limit)
  3880. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3881. (progn
  3882. (add-text-properties (match-beginning 1) (match-end 1)
  3883. (list 'mouse-face 'highlight
  3884. 'rear-nonsticky org-nonsticky-props
  3885. 'keymap org-mouse-map))
  3886. t)))
  3887. (defun org-outline-level ()
  3888. (save-excursion
  3889. (looking-at outline-regexp)
  3890. (if (match-beginning 1)
  3891. (+ (org-get-string-indentation (match-string 1)) 1000)
  3892. (1- (- (match-end 0) (match-beginning 0))))))
  3893. (defvar org-font-lock-keywords nil)
  3894. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3895. "Regular expression matching a property line.")
  3896. (defvar org-font-lock-hook nil
  3897. "Functions to be called for special font lock stuff.")
  3898. (defun org-font-lock-hook (limit)
  3899. (run-hook-with-args 'org-font-lock-hook limit))
  3900. (defun org-set-font-lock-defaults ()
  3901. (let* ((em org-fontify-emphasized-text)
  3902. (lk org-activate-links)
  3903. (org-font-lock-extra-keywords
  3904. (list
  3905. ;; Call the hook
  3906. '(org-font-lock-hook)
  3907. ;; Headlines
  3908. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3909. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3910. ;; Table lines
  3911. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3912. (1 'org-table t))
  3913. ;; Table internals
  3914. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3915. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3916. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3917. ;; Drawers
  3918. (list org-drawer-regexp '(0 'org-special-keyword t))
  3919. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3920. ;; Properties
  3921. (list org-property-re
  3922. '(1 'org-special-keyword t)
  3923. '(3 'org-property-value t))
  3924. (if org-format-transports-properties-p
  3925. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3926. ;; Links
  3927. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3928. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3929. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3930. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3931. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3932. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3933. (if (memq 'footnote lk) '(org-activate-footnote-links
  3934. (2 'org-footnote t)))
  3935. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3936. '(org-hide-wide-columns (0 nil append))
  3937. ;; TODO lines
  3938. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  3939. '(1 (org-get-todo-face 1) t))
  3940. ;; DONE
  3941. (if org-fontify-done-headline
  3942. (list (concat "^[*]+ +\\<\\("
  3943. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3944. "\\)\\(.*\\)")
  3945. '(2 'org-headline-done t))
  3946. nil)
  3947. ;; Priorities
  3948. '(org-font-lock-add-priority-faces)
  3949. ;; Tags
  3950. '(org-font-lock-add-tag-faces)
  3951. ;; Special keywords
  3952. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3953. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3954. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3955. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3956. ;; Emphasis
  3957. (if em
  3958. (if (featurep 'xemacs)
  3959. '(org-do-emphasis-faces (0 nil append))
  3960. '(org-do-emphasis-faces)))
  3961. ;; Checkboxes
  3962. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3963. 2 'org-checkbox prepend)
  3964. (if org-provide-checkbox-statistics
  3965. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3966. (0 (org-get-checkbox-statistics-face) t)))
  3967. ;; Description list items
  3968. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3969. 2 'bold prepend)
  3970. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3971. '(1 'org-archived prepend))
  3972. ;; Specials
  3973. '(org-do-latex-and-special-faces)
  3974. ;; Code
  3975. '(org-activate-code (1 'org-code t))
  3976. ;; COMMENT
  3977. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3978. "\\|" org-quote-string "\\)\\>")
  3979. '(1 'org-special-keyword t))
  3980. '("^#.*" (0 'font-lock-comment-face t))
  3981. )))
  3982. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3983. ;; Now set the full font-lock-keywords
  3984. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3985. (org-set-local 'font-lock-defaults
  3986. '(org-font-lock-keywords t nil nil backward-paragraph))
  3987. (kill-local-variable 'font-lock-keywords) nil))
  3988. (defvar org-m nil)
  3989. (defvar org-l nil)
  3990. (defvar org-f nil)
  3991. (defun org-get-level-face (n)
  3992. "Get the right face for match N in font-lock matching of headlines."
  3993. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3994. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3995. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3996. (cond
  3997. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3998. ((eq n 2) org-f)
  3999. (t (if org-level-color-stars-only nil org-f))))
  4000. (defun org-get-todo-face (kwd)
  4001. "Get the right face for a TODO keyword KWD.
  4002. If KWD is a number, get the corresponding match group."
  4003. (if (numberp kwd) (setq kwd (match-string kwd)))
  4004. (or (cdr (assoc kwd org-todo-keyword-faces))
  4005. (and (member kwd org-done-keywords) 'org-done)
  4006. 'org-todo))
  4007. (defun org-font-lock-add-tag-faces (limit)
  4008. "Add the special tag faces."
  4009. (when (and org-tag-faces org-tags-special-faces-re)
  4010. (while (re-search-forward org-tags-special-faces-re limit t)
  4011. (add-text-properties (match-beginning 1) (match-end 1)
  4012. (list 'face (org-get-tag-face 1)
  4013. 'font-lock-fontified t))
  4014. (backward-char 1))))
  4015. (defun org-font-lock-add-priority-faces (limit)
  4016. "Add the special priority faces."
  4017. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4018. (add-text-properties
  4019. (match-beginning 0) (match-end 0)
  4020. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4021. org-priority-faces))
  4022. 'org-special-keyword)
  4023. 'font-lock-fontified t))))
  4024. (defun org-get-tag-face (kwd)
  4025. "Get the right face for a TODO keyword KWD.
  4026. If KWD is a number, get the corresponding match group."
  4027. (if (numberp kwd) (setq kwd (match-string kwd)))
  4028. (or (cdr (assoc kwd org-tag-faces))
  4029. 'org-tag))
  4030. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4031. "Remove fontification and activation overlays from links."
  4032. (font-lock-default-unfontify-region beg end)
  4033. (let* ((buffer-undo-list t)
  4034. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4035. (inhibit-modification-hooks t)
  4036. deactivate-mark buffer-file-name buffer-file-truename)
  4037. (remove-text-properties beg end
  4038. '(mouse-face t keymap t org-linked-text t
  4039. invisible t intangible t))))
  4040. ;;;; Visibility cycling, including org-goto and indirect buffer
  4041. ;;; Cycling
  4042. (defvar org-cycle-global-status nil)
  4043. (make-variable-buffer-local 'org-cycle-global-status)
  4044. (defvar org-cycle-subtree-status nil)
  4045. (make-variable-buffer-local 'org-cycle-subtree-status)
  4046. ;;;###autoload
  4047. (defvar org-inlinetask-min-level)
  4048. (defun org-cycle (&optional arg)
  4049. "Visibility cycling for Org-mode.
  4050. - When this function is called with a prefix argument, rotate the entire
  4051. buffer through 3 states (global cycling)
  4052. 1. OVERVIEW: Show only top-level headlines.
  4053. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4054. 3. SHOW ALL: Show everything.
  4055. When called with two C-u C-u prefixes, switch to the startup visibility,
  4056. determined by the variable `org-startup-folded', and by any VISIBILITY
  4057. properties in the buffer.
  4058. When called with three C-u C-u C-u prefixed, show the entire buffer,
  4059. including drawers.
  4060. - When point is at the beginning of a headline, rotate the subtree started
  4061. by this line through 3 different states (local cycling)
  4062. 1. FOLDED: Only the main headline is shown.
  4063. 2. CHILDREN: The main headline and the direct children are shown.
  4064. From this state, you can move to one of the children
  4065. and zoom in further.
  4066. 3. SUBTREE: Show the entire subtree, including body text.
  4067. - When there is a numeric prefix, go up to a heading with level ARG, do
  4068. a `show-subtree' and return to the previous cursor position. If ARG
  4069. is negative, go up that many levels.
  4070. - When point is not at the beginning of a headline, execute the global
  4071. binding for TAB, which is re-indenting the line. See the option
  4072. `org-cycle-emulate-tab' for details.
  4073. - Special case: if point is at the beginning of the buffer and there is
  4074. no headline in line 1, this function will act as if called with prefix arg.
  4075. But only if also the variable `org-cycle-global-at-bob' is t."
  4076. (interactive "P")
  4077. (org-load-modules-maybe)
  4078. (let* ((limit-level
  4079. (or org-cycle-max-level
  4080. (and (boundp 'org-inlinetask-min-level)
  4081. org-inlinetask-min-level
  4082. (1- org-inlinetask-min-level))))
  4083. (nstars (and limit-level
  4084. (if org-odd-levels-only
  4085. (and limit-level (1- (* limit-level 2)))
  4086. limit-level)))
  4087. (outline-regexp
  4088. (cond
  4089. ((not (org-mode-p)) outline-regexp)
  4090. (org-cycle-include-plain-lists
  4091. (concat "\\(?:\\*"
  4092. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4093. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4094. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4095. (bob-special (and org-cycle-global-at-bob (bobp)
  4096. (not (looking-at outline-regexp))))
  4097. (org-cycle-hook
  4098. (if bob-special
  4099. (delq 'org-optimize-window-after-visibility-change
  4100. (copy-sequence org-cycle-hook))
  4101. org-cycle-hook))
  4102. (pos (point)))
  4103. (if (or bob-special (equal arg '(4)))
  4104. ;; special case: use global cycling
  4105. (setq arg t))
  4106. (cond
  4107. ((equal arg '(16))
  4108. (org-set-startup-visibility)
  4109. (message "Startup visibility, plus VISIBILITY properties"))
  4110. ((equal arg '(64))
  4111. (show-all)
  4112. (message "Entire buffer visible, including drawers"))
  4113. ((org-at-table-p 'any)
  4114. ;; Enter the table or move to the next field in the table
  4115. (or (org-table-recognize-table.el)
  4116. (progn
  4117. (if arg (org-table-edit-field t)
  4118. (org-table-justify-field-maybe)
  4119. (call-interactively 'org-table-next-field)))))
  4120. ((eq arg t) ;; Global cycling
  4121. (cond
  4122. ((and (eq last-command this-command)
  4123. (eq org-cycle-global-status 'overview))
  4124. ;; We just created the overview - now do table of contents
  4125. ;; This can be slow in very large buffers, so indicate action
  4126. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4127. (message "CONTENTS...")
  4128. (org-content)
  4129. (message "CONTENTS...done")
  4130. (setq org-cycle-global-status 'contents)
  4131. (run-hook-with-args 'org-cycle-hook 'contents))
  4132. ((and (eq last-command this-command)
  4133. (eq org-cycle-global-status 'contents))
  4134. ;; We just showed the table of contents - now show everything
  4135. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4136. (show-all)
  4137. (message "SHOW ALL")
  4138. (setq org-cycle-global-status 'all)
  4139. (run-hook-with-args 'org-cycle-hook 'all))
  4140. (t
  4141. ;; Default action: go to overview
  4142. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4143. (org-overview)
  4144. (message "OVERVIEW")
  4145. (setq org-cycle-global-status 'overview)
  4146. (run-hook-with-args 'org-cycle-hook 'overview))))
  4147. ((and org-drawers org-drawer-regexp
  4148. (save-excursion
  4149. (beginning-of-line 1)
  4150. (looking-at org-drawer-regexp)))
  4151. ;; Toggle block visibility
  4152. (org-flag-drawer
  4153. (not (get-char-property (match-end 0) 'invisible))))
  4154. ((integerp arg)
  4155. ;; Show-subtree, ARG levels up from here.
  4156. (save-excursion
  4157. (org-back-to-heading)
  4158. (outline-up-heading (if (< arg 0) (- arg)
  4159. (- (funcall outline-level) arg)))
  4160. (org-show-subtree)))
  4161. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4162. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4163. ;; At a heading: rotate between three different views
  4164. (org-back-to-heading)
  4165. (let ((goal-column 0) eoh eol eos)
  4166. ;; First, some boundaries
  4167. (save-excursion
  4168. (org-back-to-heading)
  4169. (save-excursion
  4170. (beginning-of-line 2)
  4171. (while (and (not (eobp)) ;; this is like `next-line'
  4172. (get-char-property (1- (point)) 'invisible))
  4173. (beginning-of-line 2)) (setq eol (point)))
  4174. (outline-end-of-heading) (setq eoh (point))
  4175. (org-end-of-subtree t)
  4176. (unless (eobp)
  4177. (skip-chars-forward " \t\n")
  4178. (beginning-of-line 1) ; in case this is an item
  4179. )
  4180. (setq eos (1- (point))))
  4181. ;; Find out what to do next and set `this-command'
  4182. (cond
  4183. ((= eos eoh)
  4184. ;; Nothing is hidden behind this heading
  4185. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4186. (message "EMPTY ENTRY")
  4187. (setq org-cycle-subtree-status nil)
  4188. (save-excursion
  4189. (goto-char eos)
  4190. (outline-next-heading)
  4191. (if (org-invisible-p) (org-flag-heading nil))))
  4192. ((or (>= eol eos)
  4193. (not (string-match "\\S-" (buffer-substring eol eos))))
  4194. ;; Entire subtree is hidden in one line: open it
  4195. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4196. (org-show-entry)
  4197. (show-children)
  4198. (message "CHILDREN")
  4199. (save-excursion
  4200. (goto-char eos)
  4201. (outline-next-heading)
  4202. (if (org-invisible-p) (org-flag-heading nil)))
  4203. (setq org-cycle-subtree-status 'children)
  4204. (run-hook-with-args 'org-cycle-hook 'children))
  4205. ((and (eq last-command this-command)
  4206. (eq org-cycle-subtree-status 'children))
  4207. ;; We just showed the children, now show everything.
  4208. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4209. (org-show-subtree)
  4210. (message "SUBTREE")
  4211. (setq org-cycle-subtree-status 'subtree)
  4212. (run-hook-with-args 'org-cycle-hook 'subtree))
  4213. (t
  4214. ;; Default action: hide the subtree.
  4215. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4216. (hide-subtree)
  4217. (message "FOLDED")
  4218. (setq org-cycle-subtree-status 'folded)
  4219. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4220. ;; TAB emulation and template completion
  4221. (buffer-read-only (org-back-to-heading))
  4222. ((org-try-structure-completion))
  4223. ((org-try-cdlatex-tab))
  4224. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4225. (or (not (bolp))
  4226. (not (looking-at outline-regexp))))
  4227. (call-interactively (global-key-binding "\t")))
  4228. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4229. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4230. (or (and (eq org-cycle-emulate-tab 'white)
  4231. (= (match-end 0) (point-at-eol)))
  4232. (and (eq org-cycle-emulate-tab 'whitestart)
  4233. (>= (match-end 0) pos))))
  4234. t
  4235. (eq org-cycle-emulate-tab t))
  4236. (call-interactively (global-key-binding "\t")))
  4237. (t (save-excursion
  4238. (org-back-to-heading)
  4239. (org-cycle))))))
  4240. ;;;###autoload
  4241. (defun org-global-cycle (&optional arg)
  4242. "Cycle the global visibility. For details see `org-cycle'.
  4243. With C-u prefix arg, switch to startup visibility.
  4244. With a numeric prefix, show all headlines up to that level."
  4245. (interactive "P")
  4246. (let ((org-cycle-include-plain-lists
  4247. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4248. (cond
  4249. ((integerp arg)
  4250. (show-all)
  4251. (hide-sublevels arg)
  4252. (setq org-cycle-global-status 'contents))
  4253. ((equal arg '(4))
  4254. (org-set-startup-visibility)
  4255. (message "Startup visibility, plus VISIBILITY properties."))
  4256. (t
  4257. (org-cycle '(4))))))
  4258. (defun org-set-startup-visibility ()
  4259. "Set the visibility required by startup options and properties."
  4260. (cond
  4261. ((eq org-startup-folded t)
  4262. (org-cycle '(4)))
  4263. ((eq org-startup-folded 'content)
  4264. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4265. (org-cycle '(4)) (org-cycle '(4)))))
  4266. (org-set-visibility-according-to-property 'no-cleanup)
  4267. (org-cycle-hide-archived-subtrees 'all)
  4268. (org-cycle-hide-drawers 'all)
  4269. (org-cycle-show-empty-lines 'all))
  4270. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4271. "Switch subtree visibilities according to :VISIBILITY: property."
  4272. (interactive)
  4273. (let (org-show-entry-below state)
  4274. (save-excursion
  4275. (goto-char (point-min))
  4276. (while (re-search-forward
  4277. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4278. nil t)
  4279. (setq state (match-string 1))
  4280. (save-excursion
  4281. (org-back-to-heading t)
  4282. (hide-subtree)
  4283. (org-reveal)
  4284. (cond
  4285. ((equal state '("fold" "folded"))
  4286. (hide-subtree))
  4287. ((equal state "children")
  4288. (org-show-hidden-entry)
  4289. (show-children))
  4290. ((equal state "content")
  4291. (save-excursion
  4292. (save-restriction
  4293. (org-narrow-to-subtree)
  4294. (org-content))))
  4295. ((member state '("all" "showall"))
  4296. (show-subtree)))))
  4297. (unless no-cleanup
  4298. (org-cycle-hide-archived-subtrees 'all)
  4299. (org-cycle-hide-drawers 'all)
  4300. (org-cycle-show-empty-lines 'all)))))
  4301. (defun org-overview ()
  4302. "Switch to overview mode, showing only top-level headlines.
  4303. Really, this shows all headlines with level equal or greater than the level
  4304. of the first headline in the buffer. This is important, because if the
  4305. first headline is not level one, then (hide-sublevels 1) gives confusing
  4306. results."
  4307. (interactive)
  4308. (let ((level (save-excursion
  4309. (goto-char (point-min))
  4310. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4311. (progn
  4312. (goto-char (match-beginning 0))
  4313. (funcall outline-level))))))
  4314. (and level (hide-sublevels level))))
  4315. (defun org-content (&optional arg)
  4316. "Show all headlines in the buffer, like a table of contents.
  4317. With numerical argument N, show content up to level N."
  4318. (interactive "P")
  4319. (save-excursion
  4320. ;; Visit all headings and show their offspring
  4321. (and (integerp arg) (org-overview))
  4322. (goto-char (point-max))
  4323. (catch 'exit
  4324. (while (and (progn (condition-case nil
  4325. (outline-previous-visible-heading 1)
  4326. (error (goto-char (point-min))))
  4327. t)
  4328. (looking-at outline-regexp))
  4329. (if (integerp arg)
  4330. (show-children (1- arg))
  4331. (show-branches))
  4332. (if (bobp) (throw 'exit nil))))))
  4333. (defun org-optimize-window-after-visibility-change (state)
  4334. "Adjust the window after a change in outline visibility.
  4335. This function is the default value of the hook `org-cycle-hook'."
  4336. (when (get-buffer-window (current-buffer))
  4337. (cond
  4338. ((eq state 'content) nil)
  4339. ((eq state 'all) nil)
  4340. ((eq state 'folded) nil)
  4341. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4342. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4343. (defun org-compact-display-after-subtree-move ()
  4344. "Show a compacter version of the tree of the entry's parent."
  4345. (save-excursion
  4346. (if (org-up-heading-safe)
  4347. (progn
  4348. (hide-subtree)
  4349. (show-entry)
  4350. (show-children)
  4351. (org-cycle-show-empty-lines 'children)
  4352. (org-cycle-hide-drawers 'children))
  4353. (org-overview))))
  4354. (defun org-cycle-show-empty-lines (state)
  4355. "Show empty lines above all visible headlines.
  4356. The region to be covered depends on STATE when called through
  4357. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4358. entire buffer covered. Note that an empty line is only shown if there
  4359. are at least `org-cycle-separator-lines' empty lines before the headline."
  4360. (when (> org-cycle-separator-lines 0)
  4361. (save-excursion
  4362. (let* ((n org-cycle-separator-lines)
  4363. (re (cond
  4364. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4365. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4366. (t (let ((ns (number-to-string (- n 2))))
  4367. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4368. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4369. beg end)
  4370. (cond
  4371. ((memq state '(overview contents t))
  4372. (setq beg (point-min) end (point-max)))
  4373. ((memq state '(children folded))
  4374. (setq beg (point) end (progn (org-end-of-subtree t t)
  4375. (beginning-of-line 2)
  4376. (point)))))
  4377. (when beg
  4378. (goto-char beg)
  4379. (while (re-search-forward re end t)
  4380. (if (not (get-char-property (match-end 1) 'invisible))
  4381. (outline-flag-region
  4382. (match-beginning 1) (match-end 1) nil)))))))
  4383. ;; Never hide empty lines at the end of the file.
  4384. (save-excursion
  4385. (goto-char (point-max))
  4386. (outline-previous-heading)
  4387. (outline-end-of-heading)
  4388. (if (and (looking-at "[ \t\n]+")
  4389. (= (match-end 0) (point-max)))
  4390. (outline-flag-region (point) (match-end 0) nil))))
  4391. (defun org-show-empty-lines-in-parent ()
  4392. "Move to the parent and re-show empty lines before visible headlines."
  4393. (save-excursion
  4394. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4395. (org-cycle-show-empty-lines context))))
  4396. (defun org-cycle-hide-drawers (state)
  4397. "Re-hide all drawers after a visibility state change."
  4398. (when (and (org-mode-p)
  4399. (not (memq state '(overview folded))))
  4400. (save-excursion
  4401. (let* ((globalp (memq state '(contents all)))
  4402. (beg (if globalp (point-min) (point)))
  4403. (end (if globalp (point-max) (org-end-of-subtree t))))
  4404. (goto-char beg)
  4405. (while (re-search-forward org-drawer-regexp end t)
  4406. (org-flag-drawer t))))))
  4407. (defun org-flag-drawer (flag)
  4408. (save-excursion
  4409. (beginning-of-line 1)
  4410. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4411. (let ((b (match-end 0))
  4412. (outline-regexp org-outline-regexp))
  4413. (if (re-search-forward
  4414. "^[ \t]*:END:"
  4415. (save-excursion (outline-next-heading) (point)) t)
  4416. (outline-flag-region b (point-at-eol) flag)
  4417. (error ":END: line missing"))))))
  4418. (defun org-subtree-end-visible-p ()
  4419. "Is the end of the current subtree visible?"
  4420. (pos-visible-in-window-p
  4421. (save-excursion (org-end-of-subtree t) (point))))
  4422. (defun org-first-headline-recenter (&optional N)
  4423. "Move cursor to the first headline and recenter the headline.
  4424. Optional argument N means, put the headline into the Nth line of the window."
  4425. (goto-char (point-min))
  4426. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4427. (beginning-of-line)
  4428. (recenter (prefix-numeric-value N))))
  4429. ;;; Org-goto
  4430. (defvar org-goto-window-configuration nil)
  4431. (defvar org-goto-marker nil)
  4432. (defvar org-goto-map
  4433. (let ((map (make-sparse-keymap)))
  4434. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4435. (while (setq cmd (pop cmds))
  4436. (substitute-key-definition cmd cmd map global-map)))
  4437. (suppress-keymap map)
  4438. (org-defkey map "\C-m" 'org-goto-ret)
  4439. (org-defkey map [(return)] 'org-goto-ret)
  4440. (org-defkey map [(left)] 'org-goto-left)
  4441. (org-defkey map [(right)] 'org-goto-right)
  4442. (org-defkey map [(control ?g)] 'org-goto-quit)
  4443. (org-defkey map "\C-i" 'org-cycle)
  4444. (org-defkey map [(tab)] 'org-cycle)
  4445. (org-defkey map [(down)] 'outline-next-visible-heading)
  4446. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4447. (if org-goto-auto-isearch
  4448. (if (fboundp 'define-key-after)
  4449. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4450. nil)
  4451. (org-defkey map "q" 'org-goto-quit)
  4452. (org-defkey map "n" 'outline-next-visible-heading)
  4453. (org-defkey map "p" 'outline-previous-visible-heading)
  4454. (org-defkey map "f" 'outline-forward-same-level)
  4455. (org-defkey map "b" 'outline-backward-same-level)
  4456. (org-defkey map "u" 'outline-up-heading))
  4457. (org-defkey map "/" 'org-occur)
  4458. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4459. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4460. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4461. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4462. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4463. map))
  4464. (defconst org-goto-help
  4465. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4466. RET=jump to location [Q]uit and return to previous location
  4467. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4468. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4469. ;; FIXME: Docstring doe not mention both interfaces
  4470. (defun org-goto (&optional alternative-interface)
  4471. "Look up a different location in the current file, keeping current visibility.
  4472. When you want look-up or go to a different location in a document, the
  4473. fastest way is often to fold the entire buffer and then dive into the tree.
  4474. This method has the disadvantage, that the previous location will be folded,
  4475. which may not be what you want.
  4476. This command works around this by showing a copy of the current buffer
  4477. in an indirect buffer, in overview mode. You can dive into the tree in
  4478. that copy, use org-occur and incremental search to find a location.
  4479. When pressing RET or `Q', the command returns to the original buffer in
  4480. which the visibility is still unchanged. After RET is will also jump to
  4481. the location selected in the indirect buffer and expose the
  4482. the headline hierarchy above."
  4483. (interactive "P")
  4484. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4485. (org-refile-use-outline-path t)
  4486. (org-refile-target-verify-function nil)
  4487. (interface
  4488. (if (not alternative-interface)
  4489. org-goto-interface
  4490. (if (eq org-goto-interface 'outline)
  4491. 'outline-path-completion
  4492. 'outline)))
  4493. (org-goto-start-pos (point))
  4494. (selected-point
  4495. (if (eq interface 'outline)
  4496. (car (org-get-location (current-buffer) org-goto-help))
  4497. (nth 3 (org-refile-get-location "Goto: ")))))
  4498. (if selected-point
  4499. (progn
  4500. (org-mark-ring-push org-goto-start-pos)
  4501. (goto-char selected-point)
  4502. (if (or (org-invisible-p) (org-invisible-p2))
  4503. (org-show-context 'org-goto)))
  4504. (message "Quit"))))
  4505. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4506. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4507. (defvar org-goto-local-auto-isearch-map) ; defined below
  4508. (defun org-get-location (buf help)
  4509. "Let the user select a location in the Org-mode buffer BUF.
  4510. This function uses a recursive edit. It returns the selected position
  4511. or nil."
  4512. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4513. (isearch-hide-immediately nil)
  4514. (isearch-search-fun-function
  4515. (lambda () 'org-goto-local-search-headings))
  4516. (org-goto-selected-point org-goto-exit-command))
  4517. (save-excursion
  4518. (save-window-excursion
  4519. (delete-other-windows)
  4520. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4521. (switch-to-buffer
  4522. (condition-case nil
  4523. (make-indirect-buffer (current-buffer) "*org-goto*")
  4524. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4525. (with-output-to-temp-buffer "*Help*"
  4526. (princ help))
  4527. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4528. (setq buffer-read-only nil)
  4529. (let ((org-startup-truncated t)
  4530. (org-startup-folded nil)
  4531. (org-startup-align-all-tables nil))
  4532. (org-mode)
  4533. (org-overview))
  4534. (setq buffer-read-only t)
  4535. (if (and (boundp 'org-goto-start-pos)
  4536. (integer-or-marker-p org-goto-start-pos))
  4537. (let ((org-show-hierarchy-above t)
  4538. (org-show-siblings t)
  4539. (org-show-following-heading t))
  4540. (goto-char org-goto-start-pos)
  4541. (and (org-invisible-p) (org-show-context)))
  4542. (goto-char (point-min)))
  4543. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4544. (message "Select location and press RET")
  4545. (use-local-map org-goto-map)
  4546. (recursive-edit)
  4547. ))
  4548. (kill-buffer "*org-goto*")
  4549. (cons org-goto-selected-point org-goto-exit-command)))
  4550. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4551. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4552. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4553. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4554. (defun org-goto-local-search-headings (string bound noerror)
  4555. "Search and make sure that any matches are in headlines."
  4556. (catch 'return
  4557. (while (if isearch-forward
  4558. (search-forward string bound noerror)
  4559. (search-backward string bound noerror))
  4560. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4561. (and (member :headline context)
  4562. (not (member :tags context))))
  4563. (throw 'return (point))))))
  4564. (defun org-goto-local-auto-isearch ()
  4565. "Start isearch."
  4566. (interactive)
  4567. (goto-char (point-min))
  4568. (let ((keys (this-command-keys)))
  4569. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4570. (isearch-mode t)
  4571. (isearch-process-search-char (string-to-char keys)))))
  4572. (defun org-goto-ret (&optional arg)
  4573. "Finish `org-goto' by going to the new location."
  4574. (interactive "P")
  4575. (setq org-goto-selected-point (point)
  4576. org-goto-exit-command 'return)
  4577. (throw 'exit nil))
  4578. (defun org-goto-left ()
  4579. "Finish `org-goto' by going to the new location."
  4580. (interactive)
  4581. (if (org-on-heading-p)
  4582. (progn
  4583. (beginning-of-line 1)
  4584. (setq org-goto-selected-point (point)
  4585. org-goto-exit-command 'left)
  4586. (throw 'exit nil))
  4587. (error "Not on a heading")))
  4588. (defun org-goto-right ()
  4589. "Finish `org-goto' by going to the new location."
  4590. (interactive)
  4591. (if (org-on-heading-p)
  4592. (progn
  4593. (setq org-goto-selected-point (point)
  4594. org-goto-exit-command 'right)
  4595. (throw 'exit nil))
  4596. (error "Not on a heading")))
  4597. (defun org-goto-quit ()
  4598. "Finish `org-goto' without cursor motion."
  4599. (interactive)
  4600. (setq org-goto-selected-point nil)
  4601. (setq org-goto-exit-command 'quit)
  4602. (throw 'exit nil))
  4603. ;;; Indirect buffer display of subtrees
  4604. (defvar org-indirect-dedicated-frame nil
  4605. "This is the frame being used for indirect tree display.")
  4606. (defvar org-last-indirect-buffer nil)
  4607. (defun org-tree-to-indirect-buffer (&optional arg)
  4608. "Create indirect buffer and narrow it to current subtree.
  4609. With numerical prefix ARG, go up to this level and then take that tree.
  4610. If ARG is negative, go up that many levels.
  4611. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4612. indirect buffer previously made with this command, to avoid proliferation of
  4613. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4614. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4615. is kept so that you can work with several indirect buffers at the same time.
  4616. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4617. requests that a new frame be made for the new buffer, so that the dedicated
  4618. frame is not changed."
  4619. (interactive "P")
  4620. (let ((cbuf (current-buffer))
  4621. (cwin (selected-window))
  4622. (pos (point))
  4623. beg end level heading ibuf)
  4624. (save-excursion
  4625. (org-back-to-heading t)
  4626. (when (numberp arg)
  4627. (setq level (org-outline-level))
  4628. (if (< arg 0) (setq arg (+ level arg)))
  4629. (while (> (setq level (org-outline-level)) arg)
  4630. (outline-up-heading 1 t)))
  4631. (setq beg (point)
  4632. heading (org-get-heading))
  4633. (org-end-of-subtree t) (setq end (point)))
  4634. (if (and (buffer-live-p org-last-indirect-buffer)
  4635. (not (eq org-indirect-buffer-display 'new-frame))
  4636. (not arg))
  4637. (kill-buffer org-last-indirect-buffer))
  4638. (setq ibuf (org-get-indirect-buffer cbuf)
  4639. org-last-indirect-buffer ibuf)
  4640. (cond
  4641. ((or (eq org-indirect-buffer-display 'new-frame)
  4642. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4643. (select-frame (make-frame))
  4644. (delete-other-windows)
  4645. (switch-to-buffer ibuf)
  4646. (org-set-frame-title heading))
  4647. ((eq org-indirect-buffer-display 'dedicated-frame)
  4648. (raise-frame
  4649. (select-frame (or (and org-indirect-dedicated-frame
  4650. (frame-live-p org-indirect-dedicated-frame)
  4651. org-indirect-dedicated-frame)
  4652. (setq org-indirect-dedicated-frame (make-frame)))))
  4653. (delete-other-windows)
  4654. (switch-to-buffer ibuf)
  4655. (org-set-frame-title (concat "Indirect: " heading)))
  4656. ((eq org-indirect-buffer-display 'current-window)
  4657. (switch-to-buffer ibuf))
  4658. ((eq org-indirect-buffer-display 'other-window)
  4659. (pop-to-buffer ibuf))
  4660. (t (error "Invalid value.")))
  4661. (if (featurep 'xemacs)
  4662. (save-excursion (org-mode) (turn-on-font-lock)))
  4663. (narrow-to-region beg end)
  4664. (show-all)
  4665. (goto-char pos)
  4666. (and (window-live-p cwin) (select-window cwin))))
  4667. (defun org-get-indirect-buffer (&optional buffer)
  4668. (setq buffer (or buffer (current-buffer)))
  4669. (let ((n 1) (base (buffer-name buffer)) bname)
  4670. (while (buffer-live-p
  4671. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4672. (setq n (1+ n)))
  4673. (condition-case nil
  4674. (make-indirect-buffer buffer bname 'clone)
  4675. (error (make-indirect-buffer buffer bname)))))
  4676. (defun org-set-frame-title (title)
  4677. "Set the title of the current frame to the string TITLE."
  4678. ;; FIXME: how to name a single frame in XEmacs???
  4679. (unless (featurep 'xemacs)
  4680. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4681. ;;;; Structure editing
  4682. ;;; Inserting headlines
  4683. (defun org-previous-line-empty-p ()
  4684. (save-excursion
  4685. (and (not (bobp))
  4686. (or (beginning-of-line 0) t)
  4687. (save-match-data
  4688. (looking-at "[ \t]*$")))))
  4689. (defun org-insert-heading (&optional force-heading)
  4690. "Insert a new heading or item with same depth at point.
  4691. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4692. If point is at the beginning of a headline, insert a sibling before the
  4693. current headline. If point is not at the beginning, do not split the line,
  4694. but create the new headline after the current line."
  4695. (interactive "P")
  4696. (if (= (buffer-size) 0)
  4697. (insert "\n* ")
  4698. (when (or force-heading (not (org-insert-item)))
  4699. (let* ((empty-line-p nil)
  4700. (head (save-excursion
  4701. (condition-case nil
  4702. (progn
  4703. (org-back-to-heading)
  4704. (setq empty-line-p (org-previous-line-empty-p))
  4705. (match-string 0))
  4706. (error "*"))))
  4707. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4708. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4709. pos hide-previous previous-pos)
  4710. (cond
  4711. ((and (org-on-heading-p) (bolp)
  4712. (or (bobp)
  4713. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4714. ;; insert before the current line
  4715. (open-line (if blank 2 1)))
  4716. ((and (bolp)
  4717. (or (bobp)
  4718. (save-excursion
  4719. (backward-char 1) (not (org-invisible-p)))))
  4720. ;; insert right here
  4721. nil)
  4722. (t
  4723. ;; somewhere in the line
  4724. (save-excursion
  4725. (setq previous-pos (point-at-bol))
  4726. (end-of-line)
  4727. (setq hide-previous (org-invisible-p)))
  4728. (and org-insert-heading-respect-content (org-show-subtree))
  4729. (let ((split
  4730. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4731. (save-excursion
  4732. (let ((p (point)))
  4733. (goto-char (point-at-bol))
  4734. (and (looking-at org-complex-heading-regexp)
  4735. (> p (match-beginning 4)))))))
  4736. tags pos)
  4737. (cond
  4738. (org-insert-heading-respect-content
  4739. (org-end-of-subtree nil t)
  4740. (or (bolp) (newline))
  4741. (or (org-previous-line-empty-p)
  4742. (and blank (newline)))
  4743. (open-line 1))
  4744. ((org-on-heading-p)
  4745. (when hide-previous
  4746. (show-children)
  4747. (org-show-entry))
  4748. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4749. (setq tags (and (match-end 2) (match-string 2)))
  4750. (and (match-end 1)
  4751. (delete-region (match-beginning 1) (match-end 1)))
  4752. (setq pos (point-at-bol))
  4753. (or split (end-of-line 1))
  4754. (delete-horizontal-space)
  4755. (newline (if blank 2 1))
  4756. (when tags
  4757. (save-excursion
  4758. (goto-char pos)
  4759. (end-of-line 1)
  4760. (insert " " tags)
  4761. (org-set-tags nil 'align))))
  4762. (t
  4763. (or split (end-of-line 1))
  4764. (newline (if blank 2 1)))))))
  4765. (insert head) (just-one-space)
  4766. (setq pos (point))
  4767. (end-of-line 1)
  4768. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4769. (when (and org-insert-heading-respect-content hide-previous)
  4770. (save-excursion
  4771. (goto-char previous-pos)
  4772. (hide-subtree)))
  4773. (run-hooks 'org-insert-heading-hook)))))
  4774. (defun org-get-heading (&optional no-tags)
  4775. "Return the heading of the current entry, without the stars."
  4776. (save-excursion
  4777. (org-back-to-heading t)
  4778. (if (looking-at
  4779. (if no-tags
  4780. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4781. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4782. (match-string 1) "")))
  4783. (defun org-heading-components ()
  4784. "Return the components of the current heading.
  4785. This is a list with the following elements:
  4786. - the level as an integer
  4787. - the reduced level, different if `org-odd-levels-only' is set.
  4788. - the TODO keyword, or nil
  4789. - the priority character, like ?A, or nil if no priority is given
  4790. - the headline text itself, or the tags string if no headline text
  4791. - the tags string, or nil."
  4792. (save-excursion
  4793. (org-back-to-heading t)
  4794. (if (looking-at org-complex-heading-regexp)
  4795. (list (length (match-string 1))
  4796. (org-reduced-level (length (match-string 1)))
  4797. (org-match-string-no-properties 2)
  4798. (and (match-end 3) (aref (match-string 3) 2))
  4799. (org-match-string-no-properties 4)
  4800. (org-match-string-no-properties 5)))))
  4801. (defun org-get-entry ()
  4802. "Get the entry text, after heading, entire subtree."
  4803. (save-excursion
  4804. (org-back-to-heading t)
  4805. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  4806. (defun org-insert-heading-after-current ()
  4807. "Insert a new heading with same level as current, after current subtree."
  4808. (interactive)
  4809. (org-back-to-heading)
  4810. (org-insert-heading)
  4811. (org-move-subtree-down)
  4812. (end-of-line 1))
  4813. (defun org-insert-heading-respect-content ()
  4814. (interactive)
  4815. (let ((org-insert-heading-respect-content t))
  4816. (org-insert-heading t)))
  4817. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4818. (interactive "P")
  4819. (let ((org-insert-heading-respect-content t))
  4820. (org-insert-todo-heading force-state t)))
  4821. (defun org-insert-todo-heading (arg &optional force-heading)
  4822. "Insert a new heading with the same level and TODO state as current heading.
  4823. If the heading has no TODO state, or if the state is DONE, use the first
  4824. state (TODO by default). Also with prefix arg, force first state."
  4825. (interactive "P")
  4826. (when (or force-heading (not (org-insert-item 'checkbox)))
  4827. (org-insert-heading force-heading)
  4828. (save-excursion
  4829. (org-back-to-heading)
  4830. (outline-previous-heading)
  4831. (looking-at org-todo-line-regexp))
  4832. (let*
  4833. ((new-mark-x
  4834. (if (or arg
  4835. (not (match-beginning 2))
  4836. (member (match-string 2) org-done-keywords))
  4837. (car org-todo-keywords-1)
  4838. (match-string 2)))
  4839. (new-mark
  4840. (or
  4841. (run-hook-with-args-until-success
  4842. 'org-todo-get-default-hook new-mark-x nil)
  4843. new-mark-x)))
  4844. (beginning-of-line 1)
  4845. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4846. (insert new-mark " ")))
  4847. (when org-provide-todo-statistics
  4848. (org-update-parent-todo-statistics))))
  4849. (defun org-insert-subheading (arg)
  4850. "Insert a new subheading and demote it.
  4851. Works for outline headings and for plain lists alike."
  4852. (interactive "P")
  4853. (org-insert-heading arg)
  4854. (cond
  4855. ((org-on-heading-p) (org-do-demote))
  4856. ((org-at-item-p) (org-indent-item 1))))
  4857. (defun org-insert-todo-subheading (arg)
  4858. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4859. Works for outline headings and for plain lists alike."
  4860. (interactive "P")
  4861. (org-insert-todo-heading arg)
  4862. (cond
  4863. ((org-on-heading-p) (org-do-demote))
  4864. ((org-at-item-p) (org-indent-item 1))))
  4865. ;;; Promotion and Demotion
  4866. (defun org-promote-subtree ()
  4867. "Promote the entire subtree.
  4868. See also `org-promote'."
  4869. (interactive)
  4870. (save-excursion
  4871. (org-map-tree 'org-promote))
  4872. (org-fix-position-after-promote))
  4873. (defun org-demote-subtree ()
  4874. "Demote the entire subtree. See `org-demote'.
  4875. See also `org-promote'."
  4876. (interactive)
  4877. (save-excursion
  4878. (org-map-tree 'org-demote))
  4879. (org-fix-position-after-promote))
  4880. (defun org-do-promote ()
  4881. "Promote the current heading higher up the tree.
  4882. If the region is active in `transient-mark-mode', promote all headings
  4883. in the region."
  4884. (interactive)
  4885. (save-excursion
  4886. (if (org-region-active-p)
  4887. (org-map-region 'org-promote (region-beginning) (region-end))
  4888. (org-promote)))
  4889. (org-fix-position-after-promote))
  4890. (defun org-do-demote ()
  4891. "Demote the current heading lower down the tree.
  4892. If the region is active in `transient-mark-mode', demote all headings
  4893. in the region."
  4894. (interactive)
  4895. (save-excursion
  4896. (if (org-region-active-p)
  4897. (org-map-region 'org-demote (region-beginning) (region-end))
  4898. (org-demote)))
  4899. (org-fix-position-after-promote))
  4900. (defun org-fix-position-after-promote ()
  4901. "Make sure that after pro/demotion cursor position is right."
  4902. (let ((pos (point)))
  4903. (when (save-excursion
  4904. (beginning-of-line 1)
  4905. (looking-at org-todo-line-regexp)
  4906. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4907. (cond ((eobp) (insert " "))
  4908. ((eolp) (insert " "))
  4909. ((equal (char-after) ?\ ) (forward-char 1))))))
  4910. (defun org-reduced-level (l)
  4911. "Compute the effective level of a heading.
  4912. This takes into account the setting of `org-odd-levels-only'."
  4913. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4914. (defun org-get-valid-level (level &optional change)
  4915. "Rectify a level change under the influence of `org-odd-levels-only'
  4916. LEVEL is a current level, CHANGE is by how much the level should be
  4917. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4918. even level numbers will become the next higher odd number."
  4919. (if org-odd-levels-only
  4920. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4921. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4922. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4923. (max 1 (+ level (or change 0)))))
  4924. (if (boundp 'define-obsolete-function-alias)
  4925. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4926. (define-obsolete-function-alias 'org-get-legal-level
  4927. 'org-get-valid-level)
  4928. (define-obsolete-function-alias 'org-get-legal-level
  4929. 'org-get-valid-level "23.1")))
  4930. (defun org-promote ()
  4931. "Promote the current heading higher up the tree.
  4932. If the region is active in `transient-mark-mode', promote all headings
  4933. in the region."
  4934. (org-back-to-heading t)
  4935. (let* ((level (save-match-data (funcall outline-level)))
  4936. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4937. (diff (abs (- level (length up-head) -1))))
  4938. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4939. (replace-match up-head nil t)
  4940. ;; Fixup tag positioning
  4941. (and org-auto-align-tags (org-set-tags nil t))
  4942. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4943. (defun org-demote ()
  4944. "Demote the current heading lower down the tree.
  4945. If the region is active in `transient-mark-mode', demote all headings
  4946. in the region."
  4947. (org-back-to-heading t)
  4948. (let* ((level (save-match-data (funcall outline-level)))
  4949. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4950. (diff (abs (- level (length down-head) -1))))
  4951. (replace-match down-head nil t)
  4952. ;; Fixup tag positioning
  4953. (and org-auto-align-tags (org-set-tags nil t))
  4954. (if org-adapt-indentation (org-fixup-indentation diff))))
  4955. (defun org-map-tree (fun)
  4956. "Call FUN for every heading underneath the current one."
  4957. (org-back-to-heading)
  4958. (let ((level (funcall outline-level)))
  4959. (save-excursion
  4960. (funcall fun)
  4961. (while (and (progn
  4962. (outline-next-heading)
  4963. (> (funcall outline-level) level))
  4964. (not (eobp)))
  4965. (funcall fun)))))
  4966. (defun org-map-region (fun beg end)
  4967. "Call FUN for every heading between BEG and END."
  4968. (let ((org-ignore-region t))
  4969. (save-excursion
  4970. (setq end (copy-marker end))
  4971. (goto-char beg)
  4972. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4973. (< (point) end))
  4974. (funcall fun))
  4975. (while (and (progn
  4976. (outline-next-heading)
  4977. (< (point) end))
  4978. (not (eobp)))
  4979. (funcall fun)))))
  4980. (defun org-fixup-indentation (diff)
  4981. "Change the indentation in the current entry by DIFF
  4982. However, if any line in the current entry has no indentation, or if it
  4983. would end up with no indentation after the change, nothing at all is done."
  4984. (save-excursion
  4985. (let ((end (save-excursion (outline-next-heading)
  4986. (point-marker)))
  4987. (prohibit (if (> diff 0)
  4988. "^\\S-"
  4989. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4990. col)
  4991. (unless (save-excursion (end-of-line 1)
  4992. (re-search-forward prohibit end t))
  4993. (while (and (< (point) end)
  4994. (re-search-forward "^[ \t]+" end t))
  4995. (goto-char (match-end 0))
  4996. (setq col (current-column))
  4997. (if (< diff 0) (replace-match ""))
  4998. (org-indent-to-column (+ diff col))))
  4999. (move-marker end nil))))
  5000. (defun org-convert-to-odd-levels ()
  5001. "Convert an org-mode file with all levels allowed to one with odd levels.
  5002. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5003. level 5 etc."
  5004. (interactive)
  5005. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5006. (let ((org-odd-levels-only nil) n)
  5007. (save-excursion
  5008. (goto-char (point-min))
  5009. (while (re-search-forward "^\\*\\*+ " nil t)
  5010. (setq n (- (length (match-string 0)) 2))
  5011. (while (>= (setq n (1- n)) 0)
  5012. (org-demote))
  5013. (end-of-line 1))))))
  5014. (defun org-convert-to-oddeven-levels ()
  5015. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5016. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5017. section with an even level, conversion would destroy the structure of the file. An error
  5018. is signaled in this case."
  5019. (interactive)
  5020. (goto-char (point-min))
  5021. ;; First check if there are no even levels
  5022. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5023. (org-show-context t)
  5024. (error "Not all levels are odd in this file. Conversion not possible."))
  5025. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5026. (let ((org-odd-levels-only nil) n)
  5027. (save-excursion
  5028. (goto-char (point-min))
  5029. (while (re-search-forward "^\\*\\*+ " nil t)
  5030. (setq n (/ (1- (length (match-string 0))) 2))
  5031. (while (>= (setq n (1- n)) 0)
  5032. (org-promote))
  5033. (end-of-line 1))))))
  5034. (defun org-tr-level (n)
  5035. "Make N odd if required."
  5036. (if org-odd-levels-only (1+ (/ n 2)) n))
  5037. ;;; Vertical tree motion, cutting and pasting of subtrees
  5038. (defun org-move-subtree-up (&optional arg)
  5039. "Move the current subtree up past ARG headlines of the same level."
  5040. (interactive "p")
  5041. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5042. (defun org-move-subtree-down (&optional arg)
  5043. "Move the current subtree down past ARG headlines of the same level."
  5044. (interactive "p")
  5045. (setq arg (prefix-numeric-value arg))
  5046. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5047. 'outline-get-last-sibling))
  5048. (ins-point (make-marker))
  5049. (cnt (abs arg))
  5050. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5051. ;; Select the tree
  5052. (org-back-to-heading)
  5053. (setq beg0 (point))
  5054. (save-excursion
  5055. (setq ne-beg (org-back-over-empty-lines))
  5056. (setq beg (point)))
  5057. (save-match-data
  5058. (save-excursion (outline-end-of-heading)
  5059. (setq folded (org-invisible-p)))
  5060. (outline-end-of-subtree))
  5061. (outline-next-heading)
  5062. (setq ne-end (org-back-over-empty-lines))
  5063. (setq end (point))
  5064. (goto-char beg0)
  5065. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5066. ;; include less whitespace
  5067. (save-excursion
  5068. (goto-char beg)
  5069. (forward-line (- ne-beg ne-end))
  5070. (setq beg (point))))
  5071. ;; Find insertion point, with error handling
  5072. (while (> cnt 0)
  5073. (or (and (funcall movfunc) (looking-at outline-regexp))
  5074. (progn (goto-char beg0)
  5075. (error "Cannot move past superior level or buffer limit")))
  5076. (setq cnt (1- cnt)))
  5077. (if (> arg 0)
  5078. ;; Moving forward - still need to move over subtree
  5079. (progn (org-end-of-subtree t t)
  5080. (save-excursion
  5081. (org-back-over-empty-lines)
  5082. (or (bolp) (newline)))))
  5083. (setq ne-ins (org-back-over-empty-lines))
  5084. (move-marker ins-point (point))
  5085. (setq txt (buffer-substring beg end))
  5086. (org-save-markers-in-region beg end)
  5087. (delete-region beg end)
  5088. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5089. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5090. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5091. (let ((bbb (point)))
  5092. (insert-before-markers txt)
  5093. (org-reinstall-markers-in-region bbb)
  5094. (move-marker ins-point bbb))
  5095. (or (bolp) (insert "\n"))
  5096. (setq ins-end (point))
  5097. (goto-char ins-point)
  5098. (org-skip-whitespace)
  5099. (when (and (< arg 0)
  5100. (org-first-sibling-p)
  5101. (> ne-ins ne-beg))
  5102. ;; Move whitespace back to beginning
  5103. (save-excursion
  5104. (goto-char ins-end)
  5105. (let ((kill-whole-line t))
  5106. (kill-line (- ne-ins ne-beg)) (point)))
  5107. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5108. (move-marker ins-point nil)
  5109. (org-compact-display-after-subtree-move)
  5110. (org-show-empty-lines-in-parent)
  5111. (unless folded
  5112. (org-show-entry)
  5113. (show-children)
  5114. (org-cycle-hide-drawers 'children))))
  5115. (defvar org-subtree-clip ""
  5116. "Clipboard for cut and paste of subtrees.
  5117. This is actually only a copy of the kill, because we use the normal kill
  5118. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5119. (defvar org-subtree-clip-folded nil
  5120. "Was the last copied subtree folded?
  5121. This is used to fold the tree back after pasting.")
  5122. (defun org-cut-subtree (&optional n)
  5123. "Cut the current subtree into the clipboard.
  5124. With prefix arg N, cut this many sequential subtrees.
  5125. This is a short-hand for marking the subtree and then cutting it."
  5126. (interactive "p")
  5127. (org-copy-subtree n 'cut))
  5128. (defun org-copy-subtree (&optional n cut force-store-markers)
  5129. "Cut the current subtree into the clipboard.
  5130. With prefix arg N, cut this many sequential subtrees.
  5131. This is a short-hand for marking the subtree and then copying it.
  5132. If CUT is non-nil, actually cut the subtree.
  5133. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5134. of some markers in the region, even if CUT is non-nil. This is
  5135. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5136. (interactive "p")
  5137. (let (beg end folded (beg0 (point)))
  5138. (if (interactive-p)
  5139. (org-back-to-heading nil) ; take what looks like a subtree
  5140. (org-back-to-heading t)) ; take what is really there
  5141. (org-back-over-empty-lines)
  5142. (setq beg (point))
  5143. (skip-chars-forward " \t\r\n")
  5144. (save-match-data
  5145. (save-excursion (outline-end-of-heading)
  5146. (setq folded (org-invisible-p)))
  5147. (condition-case nil
  5148. (outline-forward-same-level (1- n))
  5149. (error nil))
  5150. (org-end-of-subtree t t))
  5151. (org-back-over-empty-lines)
  5152. (setq end (point))
  5153. (goto-char beg0)
  5154. (when (> end beg)
  5155. (setq org-subtree-clip-folded folded)
  5156. (when (or cut force-store-markers)
  5157. (org-save-markers-in-region beg end))
  5158. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5159. (setq org-subtree-clip (current-kill 0))
  5160. (message "%s: Subtree(s) with %d characters"
  5161. (if cut "Cut" "Copied")
  5162. (length org-subtree-clip)))))
  5163. (defun org-paste-subtree (&optional level tree for-yank)
  5164. "Paste the clipboard as a subtree, with modification of headline level.
  5165. The entire subtree is promoted or demoted in order to match a new headline
  5166. level.
  5167. If the cursor is at the beginning of a headline, the same level as
  5168. that headline is used to paste the tree
  5169. If not, the new level is derived from the *visible* headings
  5170. before and after the insertion point, and taken to be the inferior headline
  5171. level of the two. So if the previous visible heading is level 3 and the
  5172. next is level 4 (or vice versa), level 4 will be used for insertion.
  5173. This makes sure that the subtree remains an independent subtree and does
  5174. not swallow low level entries.
  5175. You can also force a different level, either by using a numeric prefix
  5176. argument, or by inserting the heading marker by hand. For example, if the
  5177. cursor is after \"*****\", then the tree will be shifted to level 5.
  5178. If optional TREE is given, use this text instead of the kill ring.
  5179. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5180. move back over whitespace before inserting, and move point to the end of
  5181. the inserted text when done."
  5182. (interactive "P")
  5183. (unless (org-kill-is-subtree-p tree)
  5184. (error "%s"
  5185. (substitute-command-keys
  5186. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5187. (let* ((visp (not (org-invisible-p)))
  5188. (txt (or tree (and kill-ring (current-kill 0))))
  5189. (^re (concat "^\\(" outline-regexp "\\)"))
  5190. (re (concat "\\(" outline-regexp "\\)"))
  5191. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5192. (old-level (if (string-match ^re txt)
  5193. (- (match-end 0) (match-beginning 0) 1)
  5194. -1))
  5195. (force-level (cond (level (prefix-numeric-value level))
  5196. ((and (looking-at "[ \t]*$")
  5197. (string-match
  5198. ^re_ (buffer-substring
  5199. (point-at-bol) (point))))
  5200. (- (match-end 1) (match-beginning 1)))
  5201. ((and (bolp)
  5202. (looking-at org-outline-regexp))
  5203. (- (match-end 0) (point) 1))
  5204. (t nil)))
  5205. (previous-level (save-excursion
  5206. (condition-case nil
  5207. (progn
  5208. (outline-previous-visible-heading 1)
  5209. (if (looking-at re)
  5210. (- (match-end 0) (match-beginning 0) 1)
  5211. 1))
  5212. (error 1))))
  5213. (next-level (save-excursion
  5214. (condition-case nil
  5215. (progn
  5216. (or (looking-at outline-regexp)
  5217. (outline-next-visible-heading 1))
  5218. (if (looking-at re)
  5219. (- (match-end 0) (match-beginning 0) 1)
  5220. 1))
  5221. (error 1))))
  5222. (new-level (or force-level (max previous-level next-level)))
  5223. (shift (if (or (= old-level -1)
  5224. (= new-level -1)
  5225. (= old-level new-level))
  5226. 0
  5227. (- new-level old-level)))
  5228. (delta (if (> shift 0) -1 1))
  5229. (func (if (> shift 0) 'org-demote 'org-promote))
  5230. (org-odd-levels-only nil)
  5231. beg end newend)
  5232. ;; Remove the forced level indicator
  5233. (if force-level
  5234. (delete-region (point-at-bol) (point)))
  5235. ;; Paste
  5236. (beginning-of-line 1)
  5237. (unless for-yank (org-back-over-empty-lines))
  5238. (setq beg (point))
  5239. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5240. (insert-before-markers txt)
  5241. (unless (string-match "\n\\'" txt) (insert "\n"))
  5242. (setq newend (point))
  5243. (org-reinstall-markers-in-region beg)
  5244. (setq end (point))
  5245. (goto-char beg)
  5246. (skip-chars-forward " \t\n\r")
  5247. (setq beg (point))
  5248. (if (and (org-invisible-p) visp)
  5249. (save-excursion (outline-show-heading)))
  5250. ;; Shift if necessary
  5251. (unless (= shift 0)
  5252. (save-restriction
  5253. (narrow-to-region beg end)
  5254. (while (not (= shift 0))
  5255. (org-map-region func (point-min) (point-max))
  5256. (setq shift (+ delta shift)))
  5257. (goto-char (point-min))
  5258. (setq newend (point-max))))
  5259. (when (or (interactive-p) for-yank)
  5260. (message "Clipboard pasted as level %d subtree" new-level))
  5261. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5262. kill-ring
  5263. (eq org-subtree-clip (current-kill 0))
  5264. org-subtree-clip-folded)
  5265. ;; The tree was folded before it was killed/copied
  5266. (hide-subtree))
  5267. (and for-yank (goto-char newend))))
  5268. (defun org-kill-is-subtree-p (&optional txt)
  5269. "Check if the current kill is an outline subtree, or a set of trees.
  5270. Returns nil if kill does not start with a headline, or if the first
  5271. headline level is not the largest headline level in the tree.
  5272. So this will actually accept several entries of equal levels as well,
  5273. which is OK for `org-paste-subtree'.
  5274. If optional TXT is given, check this string instead of the current kill."
  5275. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5276. (start-level (and kill
  5277. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5278. org-outline-regexp "\\)")
  5279. kill)
  5280. (- (match-end 2) (match-beginning 2) 1)))
  5281. (re (concat "^" org-outline-regexp))
  5282. (start (1+ (or (match-beginning 2) -1))))
  5283. (if (not start-level)
  5284. (progn
  5285. nil) ;; does not even start with a heading
  5286. (catch 'exit
  5287. (while (setq start (string-match re kill (1+ start)))
  5288. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5289. (throw 'exit nil)))
  5290. t))))
  5291. (defvar org-markers-to-move nil
  5292. "Markers that should be moved with a cut-and-paste operation.
  5293. Those markers are stored together with their positions relative to
  5294. the start of the region.")
  5295. (defun org-save-markers-in-region (beg end)
  5296. "Check markers in region.
  5297. If these markers are between BEG and END, record their position relative
  5298. to BEG, so that after moving the block of text, we can put the markers back
  5299. into place.
  5300. This function gets called just before an entry or tree gets cut from the
  5301. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5302. called immediately, to move the markers with the entries."
  5303. (setq org-markers-to-move nil)
  5304. (when (featurep 'org-clock)
  5305. (org-clock-save-markers-for-cut-and-paste beg end))
  5306. (when (featurep 'org-agenda)
  5307. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5308. (defun org-check-and-save-marker (marker beg end)
  5309. "Check if MARKER is between BEG and END.
  5310. If yes, remember the marker and the distance to BEG."
  5311. (when (and (marker-buffer marker)
  5312. (equal (marker-buffer marker) (current-buffer)))
  5313. (if (and (>= marker beg) (< marker end))
  5314. (push (cons marker (- marker beg)) org-markers-to-move))))
  5315. (defun org-reinstall-markers-in-region (beg)
  5316. "Move all remembered markers to their position relative to BEG."
  5317. (mapc (lambda (x)
  5318. (move-marker (car x) (+ beg (cdr x))))
  5319. org-markers-to-move)
  5320. (setq org-markers-to-move nil))
  5321. (defun org-narrow-to-subtree ()
  5322. "Narrow buffer to the current subtree."
  5323. (interactive)
  5324. (save-excursion
  5325. (save-match-data
  5326. (narrow-to-region
  5327. (progn (org-back-to-heading) (point))
  5328. (progn (org-end-of-subtree t) (point))))))
  5329. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5330. "Clone the task (subtree) at point N times.
  5331. The clones will be inserted as siblings.
  5332. In interactive use, the user will be prompted for the number of clones
  5333. to be produced, and for a time SHIFT, which may be a repeater as used
  5334. in time stamps, for example `+3d'.
  5335. When a valid repeater is given and the entry contains any time stamps,
  5336. the clones will become a sequence in time, with time stamps in the
  5337. subtree shifted for each clone produced. If SHIFT is nil or the
  5338. empty string, time stamps will be left alone.
  5339. If the original subtree did contain time stamps with a repeater,
  5340. the following will happen:
  5341. - the repeater will be removed in each clone
  5342. - an additional clone will be produced, with the current, unshifted
  5343. date(s) in the entry.
  5344. - the original entry will be placed *after* all the clones, with
  5345. repeater intact.
  5346. - the start days in the repeater in the original entry will be shifted
  5347. to past the last clone.
  5348. I this way you can spell out a number of instances of a repeating task,
  5349. and still retain the repeater to cover future instances of the task."
  5350. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5351. (let (beg end template task
  5352. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5353. (if (not (and (integerp n) (> n 0)))
  5354. (error "Invalid number of replications %s" n))
  5355. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5356. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5357. shift)))
  5358. (error "Invalid shift specification %s" shift))
  5359. (when doshift
  5360. (setq shift-n (string-to-number (match-string 1 shift))
  5361. shift-what (cdr (assoc (match-string 2 shift)
  5362. '(("d" . day) ("w" . week)
  5363. ("m" . month) ("y" . year))))))
  5364. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5365. (setq nmin 1 nmax n)
  5366. (org-back-to-heading t)
  5367. (setq beg (point))
  5368. (org-end-of-subtree t t)
  5369. (setq end (point))
  5370. (setq template (buffer-substring beg end))
  5371. (when (and doshift
  5372. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5373. (delete-region beg end)
  5374. (setq end beg)
  5375. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5376. (goto-char end)
  5377. (loop for n from nmin to nmax do
  5378. (if (not doshift)
  5379. (setq task template)
  5380. (with-temp-buffer
  5381. (insert template)
  5382. (org-mode)
  5383. (goto-char (point-min))
  5384. (while (re-search-forward org-ts-regexp nil t)
  5385. (org-timestamp-change (* n shift-n) shift-what))
  5386. (unless (= n n-no-remove)
  5387. (goto-char (point-min))
  5388. (while (re-search-forward org-ts-regexp nil t)
  5389. (save-excursion
  5390. (goto-char (match-beginning 0))
  5391. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5392. (delete-region (match-beginning 1) (match-end 1))))))
  5393. (setq task (buffer-string))))
  5394. (insert task))
  5395. (goto-char beg)))
  5396. ;;; Outline Sorting
  5397. (defun org-sort (with-case)
  5398. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5399. Optional argument WITH-CASE means sort case-sensitively.
  5400. With a double prefix argument, also remove duplicate entries."
  5401. (interactive "P")
  5402. (if (org-at-table-p)
  5403. (org-call-with-arg 'org-table-sort-lines with-case)
  5404. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5405. (defun org-sort-remove-invisible (s)
  5406. (remove-text-properties 0 (length s) org-rm-props s)
  5407. (while (string-match org-bracket-link-regexp s)
  5408. (setq s (replace-match (if (match-end 2)
  5409. (match-string 3 s)
  5410. (match-string 1 s)) t t s)))
  5411. s)
  5412. (defvar org-priority-regexp) ; defined later in the file
  5413. (defun org-sort-entries-or-items
  5414. (&optional with-case sorting-type getkey-func compare-func property)
  5415. "Sort entries on a certain level of an outline tree.
  5416. If there is an active region, the entries in the region are sorted.
  5417. Else, if the cursor is before the first entry, sort the top-level items.
  5418. Else, the children of the entry at point are sorted.
  5419. Sorting can be alphabetically, numerically, and by date/time as given by
  5420. the first time stamp in the entry. The command prompts for the sorting
  5421. type unless it has been given to the function through the SORTING-TYPE
  5422. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5423. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5424. called with point at the beginning of the record. It must return either
  5425. a string or a number that should serve as the sorting key for that record.
  5426. Comparing entries ignores case by default. However, with an optional argument
  5427. WITH-CASE, the sorting considers case as well."
  5428. (interactive "P")
  5429. (let ((case-func (if with-case 'identity 'downcase))
  5430. start beg end stars re re2
  5431. txt what tmp plain-list-p)
  5432. ;; Find beginning and end of region to sort
  5433. (cond
  5434. ((org-region-active-p)
  5435. ;; we will sort the region
  5436. (setq end (region-end)
  5437. what "region")
  5438. (goto-char (region-beginning))
  5439. (if (not (org-on-heading-p)) (outline-next-heading))
  5440. (setq start (point)))
  5441. ((org-at-item-p)
  5442. ;; we will sort this plain list
  5443. (org-beginning-of-item-list) (setq start (point))
  5444. (org-end-of-item-list) (setq end (point))
  5445. (goto-char start)
  5446. (setq plain-list-p t
  5447. what "plain list"))
  5448. ((or (org-on-heading-p)
  5449. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5450. ;; we will sort the children of the current headline
  5451. (org-back-to-heading)
  5452. (setq start (point)
  5453. end (progn (org-end-of-subtree t t)
  5454. (org-back-over-empty-lines)
  5455. (point))
  5456. what "children")
  5457. (goto-char start)
  5458. (show-subtree)
  5459. (outline-next-heading))
  5460. (t
  5461. ;; we will sort the top-level entries in this file
  5462. (goto-char (point-min))
  5463. (or (org-on-heading-p) (outline-next-heading))
  5464. (setq start (point) end (point-max) what "top-level")
  5465. (goto-char start)
  5466. (show-all)))
  5467. (setq beg (point))
  5468. (if (>= beg end) (error "Nothing to sort"))
  5469. (unless plain-list-p
  5470. (looking-at "\\(\\*+\\)")
  5471. (setq stars (match-string 1)
  5472. re (concat "^" (regexp-quote stars) " +")
  5473. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5474. txt (buffer-substring beg end))
  5475. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5476. (if (and (not (equal stars "*")) (string-match re2 txt))
  5477. (error "Region to sort contains a level above the first entry")))
  5478. (unless sorting-type
  5479. (message
  5480. (if plain-list-p
  5481. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5482. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5483. what)
  5484. (setq sorting-type (read-char-exclusive))
  5485. (and (= (downcase sorting-type) ?f)
  5486. (setq getkey-func
  5487. (org-ido-completing-read "Sort using function: "
  5488. obarray 'fboundp t nil nil))
  5489. (setq getkey-func (intern getkey-func)))
  5490. (and (= (downcase sorting-type) ?r)
  5491. (setq property
  5492. (org-ido-completing-read "Property: "
  5493. (mapcar 'list (org-buffer-property-keys t))
  5494. nil t))))
  5495. (message "Sorting entries...")
  5496. (save-restriction
  5497. (narrow-to-region start end)
  5498. (let ((dcst (downcase sorting-type))
  5499. (case-fold-search nil)
  5500. (now (current-time)))
  5501. (sort-subr
  5502. (/= dcst sorting-type)
  5503. ;; This function moves to the beginning character of the "record" to
  5504. ;; be sorted.
  5505. (if plain-list-p
  5506. (lambda nil
  5507. (if (org-at-item-p) t (goto-char (point-max))))
  5508. (lambda nil
  5509. (if (re-search-forward re nil t)
  5510. (goto-char (match-beginning 0))
  5511. (goto-char (point-max)))))
  5512. ;; This function moves to the last character of the "record" being
  5513. ;; sorted.
  5514. (if plain-list-p
  5515. 'org-end-of-item
  5516. (lambda nil
  5517. (save-match-data
  5518. (condition-case nil
  5519. (outline-forward-same-level 1)
  5520. (error
  5521. (goto-char (point-max)))))))
  5522. ;; This function returns the value that gets sorted against.
  5523. (if plain-list-p
  5524. (lambda nil
  5525. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5526. (cond
  5527. ((= dcst ?n)
  5528. (string-to-number (buffer-substring (match-end 0)
  5529. (point-at-eol))))
  5530. ((= dcst ?a)
  5531. (buffer-substring (match-end 0) (point-at-eol)))
  5532. ((= dcst ?t)
  5533. (if (re-search-forward org-ts-regexp
  5534. (point-at-eol) t)
  5535. (org-time-string-to-time (match-string 0))
  5536. now))
  5537. ((= dcst ?f)
  5538. (if getkey-func
  5539. (progn
  5540. (setq tmp (funcall getkey-func))
  5541. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5542. tmp)
  5543. (error "Invalid key function `%s'" getkey-func)))
  5544. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5545. (lambda nil
  5546. (cond
  5547. ((= dcst ?n)
  5548. (if (looking-at org-complex-heading-regexp)
  5549. (string-to-number (match-string 4))
  5550. nil))
  5551. ((= dcst ?a)
  5552. (if (looking-at org-complex-heading-regexp)
  5553. (funcall case-func (match-string 4))
  5554. nil))
  5555. ((= dcst ?t)
  5556. (if (re-search-forward org-ts-regexp
  5557. (save-excursion
  5558. (forward-line 2)
  5559. (point)) t)
  5560. (org-time-string-to-time (match-string 0))
  5561. now))
  5562. ((= dcst ?p)
  5563. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5564. (string-to-char (match-string 2))
  5565. org-default-priority))
  5566. ((= dcst ?r)
  5567. (or (org-entry-get nil property) ""))
  5568. ((= dcst ?o)
  5569. (if (looking-at org-complex-heading-regexp)
  5570. (- 9999 (length (member (match-string 2)
  5571. org-todo-keywords-1)))))
  5572. ((= dcst ?f)
  5573. (if getkey-func
  5574. (progn
  5575. (setq tmp (funcall getkey-func))
  5576. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5577. tmp)
  5578. (error "Invalid key function `%s'" getkey-func)))
  5579. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5580. nil
  5581. (cond
  5582. ((= dcst ?a) 'string<)
  5583. ((= dcst ?t) 'time-less-p)
  5584. ((= dcst ?f) compare-func)
  5585. (t nil)))))
  5586. (message "Sorting entries...done")))
  5587. (defun org-do-sort (table what &optional with-case sorting-type)
  5588. "Sort TABLE of WHAT according to SORTING-TYPE.
  5589. The user will be prompted for the SORTING-TYPE if the call to this
  5590. function does not specify it. WHAT is only for the prompt, to indicate
  5591. what is being sorted. The sorting key will be extracted from
  5592. the car of the elements of the table.
  5593. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5594. (unless sorting-type
  5595. (message
  5596. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5597. what)
  5598. (setq sorting-type (read-char-exclusive)))
  5599. (let ((dcst (downcase sorting-type))
  5600. extractfun comparefun)
  5601. ;; Define the appropriate functions
  5602. (cond
  5603. ((= dcst ?n)
  5604. (setq extractfun 'string-to-number
  5605. comparefun (if (= dcst sorting-type) '< '>)))
  5606. ((= dcst ?a)
  5607. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5608. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5609. comparefun (if (= dcst sorting-type)
  5610. 'string<
  5611. (lambda (a b) (and (not (string< a b))
  5612. (not (string= a b)))))))
  5613. ((= dcst ?t)
  5614. (setq extractfun
  5615. (lambda (x)
  5616. (if (or (string-match org-ts-regexp x)
  5617. (string-match org-ts-regexp-both x))
  5618. (time-to-seconds
  5619. (org-time-string-to-time (match-string 0 x)))
  5620. 0))
  5621. comparefun (if (= dcst sorting-type) '< '>)))
  5622. (t (error "Invalid sorting type `%c'" sorting-type)))
  5623. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5624. table)
  5625. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5626. ;;; Editing source examples
  5627. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5628. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5629. (defvar org-edit-src-force-single-line nil)
  5630. (defvar org-edit-src-from-org-mode nil)
  5631. (defvar org-edit-src-picture nil)
  5632. (define-minor-mode org-exit-edit-mode
  5633. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5634. (defun org-edit-src-code ()
  5635. "Edit the source code example at point.
  5636. An indirect buffer is created, and that buffer is then narrowed to the
  5637. example at point and switched to the correct language mode. When done,
  5638. exit by killing the buffer with \\[org-edit-src-exit]."
  5639. (interactive)
  5640. (let ((line (org-current-line))
  5641. (case-fold-search t)
  5642. (msg (substitute-command-keys
  5643. "Edit, then exit with C-c ' (C-c and single quote)"))
  5644. (info (org-edit-src-find-region-and-lang))
  5645. (org-mode-p (eq major-mode 'org-mode))
  5646. beg end lang lang-f single lfmt)
  5647. (if (not info)
  5648. nil
  5649. (setq beg (nth 0 info)
  5650. end (nth 1 info)
  5651. lang (nth 2 info)
  5652. single (nth 3 info)
  5653. lfmt (nth 4 info)
  5654. lang-f (intern (concat lang "-mode")))
  5655. (unless (functionp lang-f)
  5656. (error "No such language mode: %s" lang-f))
  5657. (goto-line line)
  5658. (if (get-buffer "*Org Edit Src Example*")
  5659. (kill-buffer "*Org Edit Src Example*"))
  5660. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5661. "*Org Edit Src Example*"))
  5662. (narrow-to-region beg end)
  5663. (remove-text-properties beg end '(display nil invisible nil
  5664. intangible nil))
  5665. (let ((org-inhibit-startup t))
  5666. (funcall lang-f))
  5667. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5668. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5669. (when lfmt
  5670. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5671. (when org-mode-p
  5672. (goto-char (point-min))
  5673. (while (re-search-forward "^," nil t)
  5674. (replace-match "")))
  5675. (goto-line line)
  5676. (org-exit-edit-mode)
  5677. (org-set-local 'header-line-format msg)
  5678. (message "%s" msg)
  5679. t)))
  5680. (defun org-edit-fixed-width-region ()
  5681. "Edit the fixed-width ascii drawing at point.
  5682. This must be a region where each line starts with a colon followed by
  5683. a space character.
  5684. An indirect buffer is created, and that buffer is then narrowed to the
  5685. example at point and switched to artist-mode. When done,
  5686. exit by killing the buffer with \\[org-edit-src-exit]."
  5687. (interactive)
  5688. (let ((line (org-current-line))
  5689. (case-fold-search t)
  5690. (msg (substitute-command-keys
  5691. "Edit, then exit with C-c ' (C-c and single quote)"))
  5692. (org-mode-p (eq major-mode 'org-mode))
  5693. beg end)
  5694. (beginning-of-line 1)
  5695. (if (looking-at "[ \t]*[^:\n \t]")
  5696. nil
  5697. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5698. (setq beg (point) end beg)
  5699. (save-excursion
  5700. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5701. (setq beg (point-at-bol 2))
  5702. (setq beg (point))))
  5703. (save-excursion
  5704. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5705. (setq end (1- (match-beginning 0)))
  5706. (setq end (point))))
  5707. (goto-line line))
  5708. (if (get-buffer "*Org Edit Picture*")
  5709. (kill-buffer "*Org Edit Picture*"))
  5710. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5711. "*Org Edit Picture*"))
  5712. (narrow-to-region beg end)
  5713. (remove-text-properties beg end '(display nil invisible nil
  5714. intangible nil))
  5715. (when (fboundp 'font-lock-unfontify-region)
  5716. (font-lock-unfontify-region (point-min) (point-max)))
  5717. (cond
  5718. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5719. (fundamental-mode)
  5720. (artist-mode 1))
  5721. (t (funcall org-edit-fixed-width-region-mode)))
  5722. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5723. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5724. (set (make-local-variable 'org-edit-src-picture) t)
  5725. (goto-char (point-min))
  5726. (while (re-search-forward "^[ \t]*: ?" nil t)
  5727. (replace-match ""))
  5728. (goto-line line)
  5729. (org-exit-edit-mode)
  5730. (org-set-local 'header-line-format msg)
  5731. (message "%s" msg)
  5732. t)))
  5733. (defun org-edit-src-find-region-and-lang ()
  5734. "Find the region and language for a local edit.
  5735. Return a list with beginning and end of the region, a string representing
  5736. the language, a switch telling of the content should be in a single line."
  5737. (let ((re-list
  5738. (append
  5739. org-edit-src-region-extra
  5740. '(
  5741. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5742. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5743. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5744. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5745. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5746. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5747. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5748. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5749. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5750. ("^#\\+html:" "\n" "html" single-line)
  5751. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5752. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5753. ("^#\\+latex:" "\n" "latex" single-line)
  5754. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5755. ("^#\\+ascii:" "\n" "ascii" single-line)
  5756. )))
  5757. (pos (point))
  5758. re1 re2 single beg end lang lfmt match-re1)
  5759. (catch 'exit
  5760. (while (setq entry (pop re-list))
  5761. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5762. single (nth 3 entry))
  5763. (save-excursion
  5764. (if (or (looking-at re1)
  5765. (re-search-backward re1 nil t))
  5766. (progn
  5767. (setq match-re1 (match-string 0))
  5768. (setq beg (match-end 0)
  5769. lang (org-edit-src-get-lang lang)
  5770. lfmt (org-edit-src-get-label-format match-re1))
  5771. (if (and (re-search-forward re2 nil t)
  5772. (>= (match-end 0) pos))
  5773. (throw 'exit (list beg (match-beginning 0)
  5774. lang single lfmt))))
  5775. (if (or (looking-at re2)
  5776. (re-search-forward re2 nil t))
  5777. (progn
  5778. (setq end (match-beginning 0))
  5779. (if (and (re-search-backward re1 nil t)
  5780. (<= (match-beginning 0) pos))
  5781. (progn
  5782. (setq lfmt (org-edit-src-get-label-format
  5783. (match-string 0)))
  5784. (throw 'exit
  5785. (list (match-end 0) end
  5786. (org-edit-src-get-lang lang)
  5787. single lfmt))))))))))))
  5788. (defun org-edit-src-get-lang (lang)
  5789. "Extract the src language."
  5790. (let ((m (match-string 0)))
  5791. (cond
  5792. ((stringp lang) lang)
  5793. ((integerp lang) (match-string lang))
  5794. ((and (eq lang 'lang)
  5795. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5796. (match-string 1 m))
  5797. ((and (eq lang 'style)
  5798. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5799. (match-string 1 m))
  5800. (t "fundamental"))))
  5801. (defun org-edit-src-get-label-format (s)
  5802. "Extract the label format."
  5803. (save-match-data
  5804. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5805. (match-string 1 s))))
  5806. (defun org-edit-src-exit ()
  5807. "Exit special edit and protect problematic lines."
  5808. (interactive)
  5809. (unless (buffer-base-buffer (current-buffer))
  5810. (error "This is not an indirect buffer, something is wrong..."))
  5811. (unless (> (point-min) 1)
  5812. (error "This buffer is not narrowed, something is wrong..."))
  5813. (goto-char (point-min))
  5814. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5815. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5816. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5817. (goto-char (point-min))
  5818. (while (re-search-forward "\n" nil t)
  5819. (replace-match " "))
  5820. (goto-char (point-min))
  5821. (if (looking-at "\\s-*") (replace-match " "))
  5822. (if (re-search-forward "\\s-+\\'" nil t)
  5823. (replace-match "")))
  5824. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5825. (goto-char (point-min))
  5826. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5827. (replace-match ",\\1"))
  5828. (when font-lock-mode
  5829. (font-lock-unfontify-region (point-min) (point-max)))
  5830. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5831. (when (org-bound-and-true-p org-edit-src-picture)
  5832. (untabify (point-min) (point-max))
  5833. (goto-char (point-min))
  5834. (while (re-search-forward "^" nil t)
  5835. (replace-match ": "))
  5836. (when font-lock-mode
  5837. (font-lock-unfontify-region (point-min) (point-max)))
  5838. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5839. (kill-buffer (current-buffer))
  5840. (and (org-mode-p) (org-restart-font-lock)))
  5841. ;;; The orgstruct minor mode
  5842. ;; Define a minor mode which can be used in other modes in order to
  5843. ;; integrate the org-mode structure editing commands.
  5844. ;; This is really a hack, because the org-mode structure commands use
  5845. ;; keys which normally belong to the major mode. Here is how it
  5846. ;; works: The minor mode defines all the keys necessary to operate the
  5847. ;; structure commands, but wraps the commands into a function which
  5848. ;; tests if the cursor is currently at a headline or a plain list
  5849. ;; item. If that is the case, the structure command is used,
  5850. ;; temporarily setting many Org-mode variables like regular
  5851. ;; expressions for filling etc. However, when any of those keys is
  5852. ;; used at a different location, function uses `key-binding' to look
  5853. ;; up if the key has an associated command in another currently active
  5854. ;; keymap (minor modes, major mode, global), and executes that
  5855. ;; command. There might be problems if any of the keys is otherwise
  5856. ;; used as a prefix key.
  5857. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5858. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5859. ;; addresses this by checking explicitly for both bindings.
  5860. (defvar orgstruct-mode-map (make-sparse-keymap)
  5861. "Keymap for the minor `orgstruct-mode'.")
  5862. (defvar org-local-vars nil
  5863. "List of local variables, for use by `orgstruct-mode'")
  5864. ;;;###autoload
  5865. (define-minor-mode orgstruct-mode
  5866. "Toggle the minor more `orgstruct-mode'.
  5867. This mode is for using Org-mode structure commands in other modes.
  5868. The following key behave as if Org-mode was active, if the cursor
  5869. is on a headline, or on a plain list item (both in the definition
  5870. of Org-mode).
  5871. M-up Move entry/item up
  5872. M-down Move entry/item down
  5873. M-left Promote
  5874. M-right Demote
  5875. M-S-up Move entry/item up
  5876. M-S-down Move entry/item down
  5877. M-S-left Promote subtree
  5878. M-S-right Demote subtree
  5879. M-q Fill paragraph and items like in Org-mode
  5880. C-c ^ Sort entries
  5881. C-c - Cycle list bullet
  5882. TAB Cycle item visibility
  5883. M-RET Insert new heading/item
  5884. S-M-RET Insert new TODO heading / Checkbox item
  5885. C-c C-c Set tags / toggle checkbox"
  5886. nil " OrgStruct" nil
  5887. (org-load-modules-maybe)
  5888. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5889. ;;;###autoload
  5890. (defun turn-on-orgstruct ()
  5891. "Unconditionally turn on `orgstruct-mode'."
  5892. (orgstruct-mode 1))
  5893. (defun orgstruct++-mode (&optional arg)
  5894. "Toggle `orgstruct-mode', the enhanced version of it.
  5895. In addition to setting orgstruct-mode, this also exports all indentation and
  5896. autofilling variables from org-mode into the buffer. It will also
  5897. recognize item context in multiline items.
  5898. Note that turning off orgstruct-mode will *not* remove the
  5899. indentation/paragraph settings. This can only be done by refreshing the
  5900. major mode, for example with \[normal-mode]."
  5901. (interactive "P")
  5902. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5903. (if (< arg 1)
  5904. (orgstruct-mode -1)
  5905. (orgstruct-mode 1)
  5906. (let (var val)
  5907. (mapc
  5908. (lambda (x)
  5909. (when (string-match
  5910. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5911. (symbol-name (car x)))
  5912. (setq var (car x) val (nth 1 x))
  5913. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5914. org-local-vars)
  5915. (org-set-local 'orgstruct-is-++ t))))
  5916. (defvar orgstruct-is-++ nil
  5917. "Is orgstruct-mode in ++ version in the current-buffer?")
  5918. (make-variable-buffer-local 'orgstruct-is-++)
  5919. ;;;###autoload
  5920. (defun turn-on-orgstruct++ ()
  5921. "Unconditionally turn on `orgstruct++-mode'."
  5922. (orgstruct++-mode 1))
  5923. (defun orgstruct-error ()
  5924. "Error when there is no default binding for a structure key."
  5925. (interactive)
  5926. (error "This key has no function outside structure elements"))
  5927. (defun orgstruct-setup ()
  5928. "Setup orgstruct keymaps."
  5929. (let ((nfunc 0)
  5930. (bindings
  5931. (list
  5932. '([(meta up)] org-metaup)
  5933. '([(meta down)] org-metadown)
  5934. '([(meta left)] org-metaleft)
  5935. '([(meta right)] org-metaright)
  5936. '([(meta shift up)] org-shiftmetaup)
  5937. '([(meta shift down)] org-shiftmetadown)
  5938. '([(meta shift left)] org-shiftmetaleft)
  5939. '([(meta shift right)] org-shiftmetaright)
  5940. '([(shift up)] org-shiftup)
  5941. '([(shift down)] org-shiftdown)
  5942. '([(shift left)] org-shiftleft)
  5943. '([(shift right)] org-shiftright)
  5944. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5945. '("\M-q" fill-paragraph)
  5946. '("\C-c^" org-sort)
  5947. '("\C-c-" org-cycle-list-bullet)))
  5948. elt key fun cmd)
  5949. (while (setq elt (pop bindings))
  5950. (setq nfunc (1+ nfunc))
  5951. (setq key (org-key (car elt))
  5952. fun (nth 1 elt)
  5953. cmd (orgstruct-make-binding fun nfunc key))
  5954. (org-defkey orgstruct-mode-map key cmd))
  5955. ;; Special treatment needed for TAB and RET
  5956. (org-defkey orgstruct-mode-map [(tab)]
  5957. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5958. (org-defkey orgstruct-mode-map "\C-i"
  5959. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5960. (org-defkey orgstruct-mode-map "\M-\C-m"
  5961. (orgstruct-make-binding 'org-insert-heading 105
  5962. "\M-\C-m" [(meta return)]))
  5963. (org-defkey orgstruct-mode-map [(meta return)]
  5964. (orgstruct-make-binding 'org-insert-heading 106
  5965. [(meta return)] "\M-\C-m"))
  5966. (org-defkey orgstruct-mode-map [(shift meta return)]
  5967. (orgstruct-make-binding 'org-insert-todo-heading 107
  5968. [(meta return)] "\M-\C-m"))
  5969. (unless org-local-vars
  5970. (setq org-local-vars (org-get-local-variables)))
  5971. t))
  5972. (defun orgstruct-make-binding (fun n &rest keys)
  5973. "Create a function for binding in the structure minor mode.
  5974. FUN is the command to call inside a table. N is used to create a unique
  5975. command name. KEYS are keys that should be checked in for a command
  5976. to execute outside of tables."
  5977. (eval
  5978. (list 'defun
  5979. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5980. '(arg)
  5981. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5982. "Outside of structure, run the binding of `"
  5983. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5984. "'.")
  5985. '(interactive "p")
  5986. (list 'if
  5987. `(org-context-p 'headline 'item
  5988. (and orgstruct-is-++
  5989. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5990. 'item-body))
  5991. (list 'org-run-like-in-org-mode (list 'quote fun))
  5992. (list 'let '(orgstruct-mode)
  5993. (list 'call-interactively
  5994. (append '(or)
  5995. (mapcar (lambda (k)
  5996. (list 'key-binding k))
  5997. keys)
  5998. '('orgstruct-error))))))))
  5999. (defun org-context-p (&rest contexts)
  6000. "Check if local context is any of CONTEXTS.
  6001. Possible values in the list of contexts are `table', `headline', and `item'."
  6002. (let ((pos (point)))
  6003. (goto-char (point-at-bol))
  6004. (prog1 (or (and (memq 'table contexts)
  6005. (looking-at "[ \t]*|"))
  6006. (and (memq 'headline contexts)
  6007. ;;????????? (looking-at "\\*+"))
  6008. (looking-at outline-regexp))
  6009. (and (memq 'item contexts)
  6010. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6011. (and (memq 'item-body contexts)
  6012. (org-in-item-p)))
  6013. (goto-char pos))))
  6014. (defun org-get-local-variables ()
  6015. "Return a list of all local variables in an org-mode buffer."
  6016. (let (varlist)
  6017. (with-current-buffer (get-buffer-create "*Org tmp*")
  6018. (erase-buffer)
  6019. (org-mode)
  6020. (setq varlist (buffer-local-variables)))
  6021. (kill-buffer "*Org tmp*")
  6022. (delq nil
  6023. (mapcar
  6024. (lambda (x)
  6025. (setq x
  6026. (if (symbolp x)
  6027. (list x)
  6028. (list (car x) (list 'quote (cdr x)))))
  6029. (if (string-match
  6030. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6031. (symbol-name (car x)))
  6032. x nil))
  6033. varlist))))
  6034. ;;;###autoload
  6035. (defun org-run-like-in-org-mode (cmd)
  6036. "Run a command, pretending that the current buffer is in Org-mode.
  6037. This will temporarily bind local variables that are typically bound in
  6038. Org-mode to the values they have in Org-mode, and then interactively
  6039. call CMD."
  6040. (org-load-modules-maybe)
  6041. (unless org-local-vars
  6042. (setq org-local-vars (org-get-local-variables)))
  6043. (eval (list 'let org-local-vars
  6044. (list 'call-interactively (list 'quote cmd)))))
  6045. ;;;; Archiving
  6046. (defun org-get-category (&optional pos)
  6047. "Get the category applying to position POS."
  6048. (get-text-property (or pos (point)) 'org-category))
  6049. (defun org-refresh-category-properties ()
  6050. "Refresh category text properties in the buffer."
  6051. (let ((def-cat (cond
  6052. ((null org-category)
  6053. (if buffer-file-name
  6054. (file-name-sans-extension
  6055. (file-name-nondirectory buffer-file-name))
  6056. "???"))
  6057. ((symbolp org-category) (symbol-name org-category))
  6058. (t org-category)))
  6059. beg end cat pos optionp)
  6060. (org-unmodified
  6061. (save-excursion
  6062. (save-restriction
  6063. (widen)
  6064. (goto-char (point-min))
  6065. (put-text-property (point) (point-max) 'org-category def-cat)
  6066. (while (re-search-forward
  6067. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6068. (setq pos (match-end 0)
  6069. optionp (equal (char-after (match-beginning 0)) ?#)
  6070. cat (org-trim (match-string 2)))
  6071. (if optionp
  6072. (setq beg (point-at-bol) end (point-max))
  6073. (org-back-to-heading t)
  6074. (setq beg (point) end (org-end-of-subtree t t)))
  6075. (put-text-property beg end 'org-category cat)
  6076. (goto-char pos)))))))
  6077. ;;;; Link Stuff
  6078. ;;; Link abbreviations
  6079. (defun org-link-expand-abbrev (link)
  6080. "Apply replacements as defined in `org-link-abbrev-alist."
  6081. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6082. (let* ((key (match-string 1 link))
  6083. (as (or (assoc key org-link-abbrev-alist-local)
  6084. (assoc key org-link-abbrev-alist)))
  6085. (tag (and (match-end 2) (match-string 3 link)))
  6086. rpl)
  6087. (if (not as)
  6088. link
  6089. (setq rpl (cdr as))
  6090. (cond
  6091. ((symbolp rpl) (funcall rpl tag))
  6092. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6093. ((string-match "%h" rpl)
  6094. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6095. (t (concat rpl tag)))))
  6096. link))
  6097. ;;; Storing and inserting links
  6098. (defvar org-insert-link-history nil
  6099. "Minibuffer history for links inserted with `org-insert-link'.")
  6100. (defvar org-stored-links nil
  6101. "Contains the links stored with `org-store-link'.")
  6102. (defvar org-store-link-plist nil
  6103. "Plist with info about the most recently link created with `org-store-link'.")
  6104. (defvar org-link-protocols nil
  6105. "Link protocols added to Org-mode using `org-add-link-type'.")
  6106. (defvar org-store-link-functions nil
  6107. "List of functions that are called to create and store a link.
  6108. Each function will be called in turn until one returns a non-nil
  6109. value. Each function should check if it is responsible for creating
  6110. this link (for example by looking at the major mode).
  6111. If not, it must exit and return nil.
  6112. If yes, it should return a non-nil value after a calling
  6113. `org-store-link-props' with a list of properties and values.
  6114. Special properties are:
  6115. :type The link prefix. like \"http\". This must be given.
  6116. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6117. This is obligatory as well.
  6118. :description Optional default description for the second pair
  6119. of brackets in an Org-mode link. The user can still change
  6120. this when inserting this link into an Org-mode buffer.
  6121. In addition to these, any additional properties can be specified
  6122. and then used in remember templates.")
  6123. (defun org-add-link-type (type &optional follow export)
  6124. "Add TYPE to the list of `org-link-types'.
  6125. Re-compute all regular expressions depending on `org-link-types'
  6126. FOLLOW and EXPORT are two functions.
  6127. FOLLOW should take the link path as the single argument and do whatever
  6128. is necessary to follow the link, for example find a file or display
  6129. a mail message.
  6130. EXPORT should format the link path for export to one of the export formats.
  6131. It should be a function accepting three arguments:
  6132. path the path of the link, the text after the prefix (like \"http:\")
  6133. desc the description of the link, if any, nil if there was no description
  6134. format the export format, a symbol like `html' or `latex'.
  6135. The function may use the FORMAT information to return different values
  6136. depending on the format. The return value will be put literally into
  6137. the exported file.
  6138. Org-mode has a built-in default for exporting links. If you are happy with
  6139. this default, there is no need to define an export function for the link
  6140. type. For a simple example of an export function, see `org-bbdb.el'."
  6141. (add-to-list 'org-link-types type t)
  6142. (org-make-link-regexps)
  6143. (if (assoc type org-link-protocols)
  6144. (setcdr (assoc type org-link-protocols) (list follow export))
  6145. (push (list type follow export) org-link-protocols)))
  6146. ;;;###autoload
  6147. (defun org-store-link (arg)
  6148. "\\<org-mode-map>Store an org-link to the current location.
  6149. This link is added to `org-stored-links' and can later be inserted
  6150. into an org-buffer with \\[org-insert-link].
  6151. For some link types, a prefix arg is interpreted:
  6152. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6153. For file links, arg negates `org-context-in-file-links'."
  6154. (interactive "P")
  6155. (org-load-modules-maybe)
  6156. (setq org-store-link-plist nil) ; reset
  6157. (let (link cpltxt desc description search txt custom-id)
  6158. (cond
  6159. ((run-hook-with-args-until-success 'org-store-link-functions)
  6160. (setq link (plist-get org-store-link-plist :link)
  6161. desc (or (plist-get org-store-link-plist :description) link)))
  6162. ((equal (buffer-name) "*Org Edit Src Example*")
  6163. (let (label gc)
  6164. (while (or (not label)
  6165. (save-excursion
  6166. (save-restriction
  6167. (widen)
  6168. (goto-char (point-min))
  6169. (re-search-forward
  6170. (regexp-quote (format org-coderef-label-format label))
  6171. nil t))))
  6172. (when label (message "Label exists already") (sit-for 2))
  6173. (setq label (read-string "Code line label: " label)))
  6174. (end-of-line 1)
  6175. (setq link (format org-coderef-label-format label))
  6176. (setq gc (- 79 (length link)))
  6177. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6178. (insert link)
  6179. (setq link (concat "(" label ")") desc nil)))
  6180. ((eq major-mode 'calendar-mode)
  6181. (let ((cd (calendar-cursor-to-date)))
  6182. (setq link
  6183. (format-time-string
  6184. (car org-time-stamp-formats)
  6185. (apply 'encode-time
  6186. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6187. nil nil nil))))
  6188. (org-store-link-props :type "calendar" :date cd)))
  6189. ((eq major-mode 'w3-mode)
  6190. (setq cpltxt (if (and (buffer-name)
  6191. (not (string-match "Untitled" (buffer-name))))
  6192. (buffer-name)
  6193. (url-view-url t))
  6194. link (org-make-link (url-view-url t)))
  6195. (org-store-link-props :type "w3" :url (url-view-url t)))
  6196. ((eq major-mode 'w3m-mode)
  6197. (setq cpltxt (or w3m-current-title w3m-current-url)
  6198. link (org-make-link w3m-current-url))
  6199. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6200. ((setq search (run-hook-with-args-until-success
  6201. 'org-create-file-search-functions))
  6202. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6203. "::" search))
  6204. (setq cpltxt (or description link)))
  6205. ((eq major-mode 'image-mode)
  6206. (setq cpltxt (concat "file:"
  6207. (abbreviate-file-name buffer-file-name))
  6208. link (org-make-link cpltxt))
  6209. (org-store-link-props :type "image" :file buffer-file-name))
  6210. ((eq major-mode 'dired-mode)
  6211. ;; link to the file in the current line
  6212. (setq cpltxt (concat "file:"
  6213. (abbreviate-file-name
  6214. (expand-file-name
  6215. (dired-get-filename nil t))))
  6216. link (org-make-link cpltxt)))
  6217. ((and buffer-file-name (org-mode-p))
  6218. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  6219. (cond
  6220. ((org-in-regexp "<<\\(.*?\\)>>")
  6221. (setq cpltxt
  6222. (concat "file:"
  6223. (abbreviate-file-name buffer-file-name)
  6224. "::" (match-string 1))
  6225. link (org-make-link cpltxt)))
  6226. ((and (featurep 'org-id)
  6227. (or (eq org-link-to-org-use-id t)
  6228. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6229. (interactive-p))
  6230. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6231. (interactive-p)
  6232. (not custom-id))
  6233. (and org-link-to-org-use-id
  6234. (condition-case nil
  6235. (org-entry-get nil "ID")
  6236. (error nil)))))
  6237. ;; We can make a link using the ID.
  6238. (setq link (condition-case nil
  6239. (prog1 (org-id-store-link)
  6240. (setq desc (plist-get org-store-link-plist
  6241. :description)))
  6242. (error
  6243. ;; probably before first headline, link to file only
  6244. (concat "file:"
  6245. (abbreviate-file-name buffer-file-name))))))
  6246. (t
  6247. ;; Just link to current headline
  6248. (setq cpltxt (concat "file:"
  6249. (abbreviate-file-name buffer-file-name)))
  6250. ;; Add a context search string
  6251. (when (org-xor org-context-in-file-links arg)
  6252. (setq txt (cond
  6253. ((org-on-heading-p) nil)
  6254. ((org-region-active-p)
  6255. (buffer-substring (region-beginning) (region-end)))
  6256. (t nil)))
  6257. (when (or (null txt) (string-match "\\S-" txt))
  6258. (setq cpltxt
  6259. (concat cpltxt "::"
  6260. (condition-case nil
  6261. (org-make-org-heading-search-string txt)
  6262. (error "")))
  6263. desc "NONE")))
  6264. (if (string-match "::\\'" cpltxt)
  6265. (setq cpltxt (substring cpltxt 0 -2)))
  6266. (setq link (org-make-link cpltxt)))))
  6267. ((buffer-file-name (buffer-base-buffer))
  6268. ;; Just link to this file here.
  6269. (setq cpltxt (concat "file:"
  6270. (abbreviate-file-name
  6271. (buffer-file-name (buffer-base-buffer)))))
  6272. ;; Add a context string
  6273. (when (org-xor org-context-in-file-links arg)
  6274. (setq txt (if (org-region-active-p)
  6275. (buffer-substring (region-beginning) (region-end))
  6276. (buffer-substring (point-at-bol) (point-at-eol))))
  6277. ;; Only use search option if there is some text.
  6278. (when (string-match "\\S-" txt)
  6279. (setq cpltxt
  6280. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6281. desc "NONE")))
  6282. (setq link (org-make-link cpltxt)))
  6283. ((interactive-p)
  6284. (error "Cannot link to a buffer which is not visiting a file"))
  6285. (t (setq link nil)))
  6286. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6287. (setq link (or link cpltxt)
  6288. desc (or desc cpltxt))
  6289. (if (equal desc "NONE") (setq desc nil))
  6290. (if (and (interactive-p) link)
  6291. (progn
  6292. (setq org-stored-links
  6293. (cons (list link desc) org-stored-links))
  6294. (message "Stored: %s" (or desc link))
  6295. (when custom-id
  6296. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6297. "::#" custom-id))
  6298. (setq org-stored-links
  6299. (cons (list link desc) org-stored-links))))
  6300. (and link (org-make-link-string link desc)))))
  6301. (defun org-store-link-props (&rest plist)
  6302. "Store link properties, extract names and addresses."
  6303. (let (x adr)
  6304. (when (setq x (plist-get plist :from))
  6305. (setq adr (mail-extract-address-components x))
  6306. (setq plist (plist-put plist :fromname (car adr)))
  6307. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6308. (when (setq x (plist-get plist :to))
  6309. (setq adr (mail-extract-address-components x))
  6310. (setq plist (plist-put plist :toname (car adr)))
  6311. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6312. (let ((from (plist-get plist :from))
  6313. (to (plist-get plist :to)))
  6314. (when (and from to org-from-is-user-regexp)
  6315. (setq plist
  6316. (plist-put plist :fromto
  6317. (if (string-match org-from-is-user-regexp from)
  6318. (concat "to %t")
  6319. (concat "from %f"))))))
  6320. (setq org-store-link-plist plist))
  6321. (defun org-add-link-props (&rest plist)
  6322. "Add these properties to the link property list."
  6323. (let (key value)
  6324. (while plist
  6325. (setq key (pop plist) value (pop plist))
  6326. (setq org-store-link-plist
  6327. (plist-put org-store-link-plist key value)))))
  6328. (defun org-email-link-description (&optional fmt)
  6329. "Return the description part of an email link.
  6330. This takes information from `org-store-link-plist' and formats it
  6331. according to FMT (default from `org-email-link-description-format')."
  6332. (setq fmt (or fmt org-email-link-description-format))
  6333. (let* ((p org-store-link-plist)
  6334. (to (plist-get p :toaddress))
  6335. (from (plist-get p :fromaddress))
  6336. (table
  6337. (list
  6338. (cons "%c" (plist-get p :fromto))
  6339. (cons "%F" (plist-get p :from))
  6340. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6341. (cons "%T" (plist-get p :to))
  6342. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6343. (cons "%s" (plist-get p :subject))
  6344. (cons "%m" (plist-get p :message-id)))))
  6345. (when (string-match "%c" fmt)
  6346. ;; Check if the user wrote this message
  6347. (if (and org-from-is-user-regexp from to
  6348. (save-match-data (string-match org-from-is-user-regexp from)))
  6349. (setq fmt (replace-match "to %t" t t fmt))
  6350. (setq fmt (replace-match "from %f" t t fmt))))
  6351. (org-replace-escapes fmt table)))
  6352. (defun org-make-org-heading-search-string (&optional string heading)
  6353. "Make search string for STRING or current headline."
  6354. (interactive)
  6355. (let ((s (or string (org-get-heading))))
  6356. (unless (and string (not heading))
  6357. ;; We are using a headline, clean up garbage in there.
  6358. (if (string-match org-todo-regexp s)
  6359. (setq s (replace-match "" t t s)))
  6360. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6361. (setq s (replace-match "" t t s)))
  6362. (setq s (org-trim s))
  6363. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6364. org-comment-string "\\)") s)
  6365. (setq s (replace-match "" t t s)))
  6366. (while (string-match org-ts-regexp s)
  6367. (setq s (replace-match "" t t s))))
  6368. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6369. (setq s (replace-match " " t t s)))
  6370. (or string (setq s (concat "*" s))) ; Add * for headlines
  6371. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6372. (defun org-make-link (&rest strings)
  6373. "Concatenate STRINGS."
  6374. (apply 'concat strings))
  6375. (defun org-make-link-string (link &optional description)
  6376. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6377. (unless (string-match "\\S-" link)
  6378. (error "Empty link"))
  6379. (when (stringp description)
  6380. ;; Remove brackets from the description, they are fatal.
  6381. (while (string-match "\\[" description)
  6382. (setq description (replace-match "{" t t description)))
  6383. (while (string-match "\\]" description)
  6384. (setq description (replace-match "}" t t description))))
  6385. (when (equal (org-link-escape link) description)
  6386. ;; No description needed, it is identical
  6387. (setq description nil))
  6388. (when (and (not description)
  6389. (not (equal link (org-link-escape link))))
  6390. (setq description (org-extract-attributes link)))
  6391. (concat "[[" (org-link-escape link) "]"
  6392. (if description (concat "[" description "]") "")
  6393. "]"))
  6394. (defconst org-link-escape-chars
  6395. '((?\ . "%20")
  6396. (?\[ . "%5B")
  6397. (?\] . "%5D")
  6398. (?\340 . "%E0") ; `a
  6399. (?\342 . "%E2") ; ^a
  6400. (?\347 . "%E7") ; ,c
  6401. (?\350 . "%E8") ; `e
  6402. (?\351 . "%E9") ; 'e
  6403. (?\352 . "%EA") ; ^e
  6404. (?\356 . "%EE") ; ^i
  6405. (?\364 . "%F4") ; ^o
  6406. (?\371 . "%F9") ; `u
  6407. (?\373 . "%FB") ; ^u
  6408. (?\; . "%3B")
  6409. (?? . "%3F")
  6410. (?= . "%3D")
  6411. (?+ . "%2B")
  6412. )
  6413. "Association list of escapes for some characters problematic in links.
  6414. This is the list that is used for internal purposes.")
  6415. (defvar org-url-encoding-use-url-hexify nil)
  6416. (defconst org-link-escape-chars-browser
  6417. '((?\ . "%20")) ; 32 for the SPC char
  6418. "Association list of escapes for some characters problematic in links.
  6419. This is the list that is used before handing over to the browser.")
  6420. (defun org-link-escape (text &optional table)
  6421. "Escape characters in TEXT that are problematic for links."
  6422. (if org-url-encoding-use-url-hexify
  6423. (url-hexify-string text)
  6424. (setq table (or table org-link-escape-chars))
  6425. (when text
  6426. (let ((re (mapconcat (lambda (x) (regexp-quote
  6427. (char-to-string (car x))))
  6428. table "\\|")))
  6429. (while (string-match re text)
  6430. (setq text
  6431. (replace-match
  6432. (cdr (assoc (string-to-char (match-string 0 text))
  6433. table))
  6434. t t text)))
  6435. text))))
  6436. (defun org-link-unescape (text &optional table)
  6437. "Reverse the action of `org-link-escape'."
  6438. (if org-url-encoding-use-url-hexify
  6439. (url-unhex-string text)
  6440. (setq table (or table org-link-escape-chars))
  6441. (when text
  6442. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6443. table "\\|")))
  6444. (while (string-match re text)
  6445. (setq text
  6446. (replace-match
  6447. (char-to-string (car (rassoc (match-string 0 text) table)))
  6448. t t text)))
  6449. text))))
  6450. (defun org-xor (a b)
  6451. "Exclusive or."
  6452. (if a (not b) b))
  6453. (defun org-fixup-message-id-for-http (s)
  6454. "Replace special characters in a message id, so it can be used in an http query."
  6455. (while (string-match "<" s)
  6456. (setq s (replace-match "%3C" t t s)))
  6457. (while (string-match ">" s)
  6458. (setq s (replace-match "%3E" t t s)))
  6459. (while (string-match "@" s)
  6460. (setq s (replace-match "%40" t t s)))
  6461. s)
  6462. ;;;###autoload
  6463. (defun org-insert-link-global ()
  6464. "Insert a link like Org-mode does.
  6465. This command can be called in any mode to insert a link in Org-mode syntax."
  6466. (interactive)
  6467. (org-load-modules-maybe)
  6468. (org-run-like-in-org-mode 'org-insert-link))
  6469. (defun org-insert-link (&optional complete-file link-location)
  6470. "Insert a link. At the prompt, enter the link.
  6471. Completion can be used to insert any of the link protocol prefixes like
  6472. http or ftp in use.
  6473. The history can be used to select a link previously stored with
  6474. `org-store-link'. When the empty string is entered (i.e. if you just
  6475. press RET at the prompt), the link defaults to the most recently
  6476. stored link. As SPC triggers completion in the minibuffer, you need to
  6477. use M-SPC or C-q SPC to force the insertion of a space character.
  6478. You will also be prompted for a description, and if one is given, it will
  6479. be displayed in the buffer instead of the link.
  6480. If there is already a link at point, this command will allow you to edit link
  6481. and description parts.
  6482. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6483. be selected using completion. The path to the file will be relative to the
  6484. current directory if the file is in the current directory or a subdirectory.
  6485. Otherwise, the link will be the absolute path as completed in the minibuffer
  6486. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6487. option `org-link-file-path-type'.
  6488. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6489. the current directory or below.
  6490. With three \\[universal-argument] prefixes, negate the meaning of
  6491. `org-keep-stored-link-after-insertion'.
  6492. If `org-make-link-description-function' is non-nil, this function will be
  6493. called with the link target, and the result will be the default
  6494. link description.
  6495. If the LINK-LOCATION parameter is non-nil, this value will be
  6496. used as the link location instead of reading one interactively."
  6497. (interactive "P")
  6498. (let* ((wcf (current-window-configuration))
  6499. (region (if (org-region-active-p)
  6500. (buffer-substring (region-beginning) (region-end))))
  6501. (remove (and region (list (region-beginning) (region-end))))
  6502. (desc region)
  6503. tmphist ; byte-compile incorrectly complains about this
  6504. (link link-location)
  6505. entry file)
  6506. (cond
  6507. (link-location) ; specified by arg, just use it.
  6508. ((org-in-regexp org-bracket-link-regexp 1)
  6509. ;; We do have a link at point, and we are going to edit it.
  6510. (setq remove (list (match-beginning 0) (match-end 0)))
  6511. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6512. (setq link (read-string "Link: "
  6513. (org-link-unescape
  6514. (org-match-string-no-properties 1)))))
  6515. ((or (org-in-regexp org-angle-link-re)
  6516. (org-in-regexp org-plain-link-re))
  6517. ;; Convert to bracket link
  6518. (setq remove (list (match-beginning 0) (match-end 0))
  6519. link (read-string "Link: "
  6520. (org-remove-angle-brackets (match-string 0)))))
  6521. ((member complete-file '((4) (16)))
  6522. ;; Completing read for file names.
  6523. (setq file (read-file-name "File: "))
  6524. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6525. (pwd1 (file-name-as-directory (abbreviate-file-name
  6526. (expand-file-name ".")))))
  6527. (cond
  6528. ((equal complete-file '(16))
  6529. (setq link (org-make-link
  6530. "file:"
  6531. (abbreviate-file-name (expand-file-name file)))))
  6532. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6533. (setq link (org-make-link "file:" (match-string 1 file))))
  6534. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6535. (expand-file-name file))
  6536. (setq link (org-make-link
  6537. "file:" (match-string 1 (expand-file-name file)))))
  6538. (t (setq link (org-make-link "file:" file))))))
  6539. (t
  6540. ;; Read link, with completion for stored links.
  6541. (with-output-to-temp-buffer "*Org Links*"
  6542. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6543. (when org-stored-links
  6544. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6545. (princ (mapconcat
  6546. (lambda (x)
  6547. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6548. (reverse org-stored-links) "\n"))))
  6549. (let ((cw (selected-window)))
  6550. (select-window (get-buffer-window "*Org Links*"))
  6551. (setq truncate-lines t)
  6552. (org-fit-window-to-buffer)
  6553. (select-window cw))
  6554. ;; Fake a link history, containing the stored links.
  6555. (setq tmphist (append (mapcar 'car org-stored-links)
  6556. org-insert-link-history))
  6557. (unwind-protect
  6558. (setq link
  6559. (let ((org-completion-use-ido nil))
  6560. (org-completing-read
  6561. "Link: "
  6562. (append
  6563. (mapcar (lambda (x) (list (concat (car x) ":")))
  6564. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6565. (mapcar (lambda (x) (list (concat x ":")))
  6566. org-link-types))
  6567. nil nil nil
  6568. 'tmphist
  6569. (or (car (car org-stored-links))))))
  6570. (set-window-configuration wcf)
  6571. (kill-buffer "*Org Links*"))
  6572. (setq entry (assoc link org-stored-links))
  6573. (or entry (push link org-insert-link-history))
  6574. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6575. (not org-keep-stored-link-after-insertion))
  6576. (setq org-stored-links (delq (assoc link org-stored-links)
  6577. org-stored-links)))
  6578. (setq desc (or desc (nth 1 entry)))))
  6579. (if (string-match org-plain-link-re link)
  6580. ;; URL-like link, normalize the use of angular brackets.
  6581. (setq link (org-make-link (org-remove-angle-brackets link))))
  6582. ;; Check if we are linking to the current file with a search option
  6583. ;; If yes, simplify the link by using only the search option.
  6584. (when (and buffer-file-name
  6585. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6586. (let* ((path (match-string 1 link))
  6587. (case-fold-search nil)
  6588. (search (match-string 2 link)))
  6589. (save-match-data
  6590. (if (equal (file-truename buffer-file-name) (file-truename path))
  6591. ;; We are linking to this same file, with a search option
  6592. (setq link search)))))
  6593. ;; Check if we can/should use a relative path. If yes, simplify the link
  6594. (when (string-match "^file:\\(.*\\)" link)
  6595. (let* ((path (match-string 1 link))
  6596. (origpath path)
  6597. (case-fold-search nil))
  6598. (cond
  6599. ((or (eq org-link-file-path-type 'absolute)
  6600. (equal complete-file '(16)))
  6601. (setq path (abbreviate-file-name (expand-file-name path))))
  6602. ((eq org-link-file-path-type 'noabbrev)
  6603. (setq path (expand-file-name path)))
  6604. ((eq org-link-file-path-type 'relative)
  6605. (setq path (file-relative-name path)))
  6606. (t
  6607. (save-match-data
  6608. (if (string-match (concat "^" (regexp-quote
  6609. (file-name-as-directory
  6610. (expand-file-name "."))))
  6611. (expand-file-name path))
  6612. ;; We are linking a file with relative path name.
  6613. (setq path (substring (expand-file-name path)
  6614. (match-end 0)))
  6615. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6616. (setq link (concat "file:" path))
  6617. (if (equal desc origpath)
  6618. (setq desc path))))
  6619. (if org-make-link-description-function
  6620. (setq desc (funcall org-make-link-description-function link desc)))
  6621. (setq desc (read-string "Description: " desc))
  6622. (unless (string-match "\\S-" desc) (setq desc nil))
  6623. (if remove (apply 'delete-region remove))
  6624. (insert (org-make-link-string link desc))))
  6625. (defun org-completing-read (&rest args)
  6626. "Completing-read with SPACE being a normal character."
  6627. (let ((minibuffer-local-completion-map
  6628. (copy-keymap minibuffer-local-completion-map)))
  6629. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6630. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6631. (apply 'org-ido-completing-read args)))
  6632. (defun org-completing-read-no-ido (&rest args)
  6633. (let (org-completion-use-ido)
  6634. (apply 'org-completing-read args)))
  6635. (defun org-ido-completing-read (&rest args)
  6636. "Completing-read using `ido-mode' speedups if available"
  6637. (if (and org-completion-use-ido
  6638. (fboundp 'ido-completing-read)
  6639. (boundp 'ido-mode) ido-mode
  6640. (listp (second args)))
  6641. (let ((ido-enter-matching-directory nil))
  6642. (apply 'ido-completing-read (concat (car args))
  6643. (mapcar (lambda (x) (car x)) (nth 1 args))
  6644. (cddr args)))
  6645. (apply 'completing-read args)))
  6646. (defun org-extract-attributes (s)
  6647. "Extract the attributes cookie from a string and set as text property."
  6648. (let (a attr (start 0) key value)
  6649. (save-match-data
  6650. (when (string-match "{{\\([^}]+\\)}}$" s)
  6651. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6652. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6653. (setq key (match-string 1 a) value (match-string 2 a)
  6654. start (match-end 0)
  6655. attr (plist-put attr (intern key) value))))
  6656. (org-add-props s nil 'org-attr attr))
  6657. s))
  6658. (defun org-extract-attributes-from-string (tag)
  6659. (let (key value attr)
  6660. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6661. (setq key (match-string 1 tag) value (match-string 2 tag)
  6662. tag (replace-match "" t t tag)
  6663. attr (plist-put attr (intern key) value)))
  6664. (cons tag attr)))
  6665. (defun org-attributes-to-string (plist)
  6666. "Format a property list into an HTML attribute list."
  6667. (let ((s "") key value)
  6668. (while plist
  6669. (setq key (pop plist) value (pop plist))
  6670. (and value
  6671. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6672. s))
  6673. ;;; Opening/following a link
  6674. (defvar org-link-search-failed nil)
  6675. (defun org-next-link ()
  6676. "Move forward to the next link.
  6677. If the link is in hidden text, expose it."
  6678. (interactive)
  6679. (when (and org-link-search-failed (eq this-command last-command))
  6680. (goto-char (point-min))
  6681. (message "Link search wrapped back to beginning of buffer"))
  6682. (setq org-link-search-failed nil)
  6683. (let* ((pos (point))
  6684. (ct (org-context))
  6685. (a (assoc :link ct)))
  6686. (if a (goto-char (nth 2 a)))
  6687. (if (re-search-forward org-any-link-re nil t)
  6688. (progn
  6689. (goto-char (match-beginning 0))
  6690. (if (org-invisible-p) (org-show-context)))
  6691. (goto-char pos)
  6692. (setq org-link-search-failed t)
  6693. (error "No further link found"))))
  6694. (defun org-previous-link ()
  6695. "Move backward to the previous link.
  6696. If the link is in hidden text, expose it."
  6697. (interactive)
  6698. (when (and org-link-search-failed (eq this-command last-command))
  6699. (goto-char (point-max))
  6700. (message "Link search wrapped back to end of buffer"))
  6701. (setq org-link-search-failed nil)
  6702. (let* ((pos (point))
  6703. (ct (org-context))
  6704. (a (assoc :link ct)))
  6705. (if a (goto-char (nth 1 a)))
  6706. (if (re-search-backward org-any-link-re nil t)
  6707. (progn
  6708. (goto-char (match-beginning 0))
  6709. (if (org-invisible-p) (org-show-context)))
  6710. (goto-char pos)
  6711. (setq org-link-search-failed t)
  6712. (error "No further link found"))))
  6713. (defun org-translate-link (s)
  6714. "Translate a link string if a translation function has been defined."
  6715. (if (and org-link-translation-function
  6716. (fboundp org-link-translation-function)
  6717. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6718. (progn
  6719. (setq s (funcall org-link-translation-function
  6720. (match-string 1) (match-string 2)))
  6721. (concat (car s) ":" (cdr s)))
  6722. s))
  6723. (defun org-translate-link-from-planner (type path)
  6724. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6725. This is still an experimental function, your mileage may vary."
  6726. (cond
  6727. ((member type '("http" "https" "news" "ftp"))
  6728. ;; standard Internet links are the same.
  6729. nil)
  6730. ((and (equal type "irc") (string-match "^//" path))
  6731. ;; Planner has two / at the beginning of an irc link, we have 1.
  6732. ;; We should have zero, actually....
  6733. (setq path (substring path 1)))
  6734. ((and (equal type "lisp") (string-match "^/" path))
  6735. ;; Planner has a slash, we do not.
  6736. (setq type "elisp" path (substring path 1)))
  6737. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6738. ;; A typical message link. Planner has the id after the fina slash,
  6739. ;; we separate it with a hash mark
  6740. (setq path (concat (match-string 1 path) "#"
  6741. (org-remove-angle-brackets (match-string 2 path)))))
  6742. )
  6743. (cons type path))
  6744. (defun org-find-file-at-mouse (ev)
  6745. "Open file link or URL at mouse."
  6746. (interactive "e")
  6747. (mouse-set-point ev)
  6748. (org-open-at-point 'in-emacs))
  6749. (defun org-open-at-mouse (ev)
  6750. "Open file link or URL at mouse."
  6751. (interactive "e")
  6752. (mouse-set-point ev)
  6753. (if (eq major-mode 'org-agenda-mode)
  6754. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6755. (org-open-at-point))
  6756. (defvar org-window-config-before-follow-link nil
  6757. "The window configuration before following a link.
  6758. This is saved in case the need arises to restore it.")
  6759. (defvar org-open-link-marker (make-marker)
  6760. "Marker pointing to the location where `org-open-at-point; was called.")
  6761. ;;;###autoload
  6762. (defun org-open-at-point-global ()
  6763. "Follow a link like Org-mode does.
  6764. This command can be called in any mode to follow a link that has
  6765. Org-mode syntax."
  6766. (interactive)
  6767. (org-run-like-in-org-mode 'org-open-at-point))
  6768. ;;;###autoload
  6769. (defun org-open-link-from-string (s &optional arg)
  6770. "Open a link in the string S, as if it was in Org-mode."
  6771. (interactive "sLink: \nP")
  6772. (let ((reference-buffer (current-buffer)))
  6773. (with-temp-buffer
  6774. (let ((org-inhibit-startup t))
  6775. (org-mode)
  6776. (insert s)
  6777. (goto-char (point-min))
  6778. (org-open-at-point arg reference-buffer)))))
  6779. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6780. "Open link at or after point.
  6781. If there is no link at point, this function will search forward up to
  6782. the end of the current line.
  6783. Normally, files will be opened by an appropriate application. If the
  6784. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6785. With a double prefix argument, try to open outside of Emacs, in the
  6786. application the system uses for this file type."
  6787. (interactive "P")
  6788. (org-load-modules-maybe)
  6789. (move-marker org-open-link-marker (point))
  6790. (setq org-window-config-before-follow-link (current-window-configuration))
  6791. (org-remove-occur-highlights nil nil t)
  6792. (cond
  6793. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6794. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6795. (org-footnote-action))
  6796. (t
  6797. (let (type path link line search (pos (point)))
  6798. (catch 'match
  6799. (save-excursion
  6800. (skip-chars-forward "^]\n\r")
  6801. (when (org-in-regexp org-bracket-link-regexp)
  6802. (setq link (org-extract-attributes
  6803. (org-link-unescape (org-match-string-no-properties 1))))
  6804. (while (string-match " *\n *" link)
  6805. (setq link (replace-match " " t t link)))
  6806. (setq link (org-link-expand-abbrev link))
  6807. (cond
  6808. ((or (file-name-absolute-p link)
  6809. (string-match "^\\.\\.?/" link))
  6810. (setq type "file" path link))
  6811. ((string-match org-link-re-with-space3 link)
  6812. (setq type (match-string 1 link) path (match-string 2 link)))
  6813. (t (setq type "thisfile" path link)))
  6814. (throw 'match t)))
  6815. (when (get-text-property (point) 'org-linked-text)
  6816. (setq type "thisfile"
  6817. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6818. (1+ (point)) (point))
  6819. path (buffer-substring
  6820. (previous-single-property-change pos 'org-linked-text)
  6821. (next-single-property-change pos 'org-linked-text)))
  6822. (throw 'match t))
  6823. (save-excursion
  6824. (when (or (org-in-regexp org-angle-link-re)
  6825. (org-in-regexp org-plain-link-re))
  6826. (setq type (match-string 1) path (match-string 2))
  6827. (throw 'match t)))
  6828. (save-excursion
  6829. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6830. (setq type "tags"
  6831. path (match-string 1))
  6832. (while (string-match ":" path)
  6833. (setq path (replace-match "+" t t path)))
  6834. (throw 'match t)))
  6835. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6836. (setq type "tree-match"
  6837. path (match-string 1))
  6838. (throw 'match t)))
  6839. (unless path
  6840. (error "No link found"))
  6841. ;; switch back to reference buffer
  6842. ;; needed when if called in a temporary buffer through
  6843. ;; org-open-link-from-string
  6844. (and reference-buffer (switch-to-buffer reference-buffer))
  6845. ;; Remove any trailing spaces in path
  6846. (if (string-match " +\\'" path)
  6847. (setq path (replace-match "" t t path)))
  6848. (if (and org-link-translation-function
  6849. (fboundp org-link-translation-function))
  6850. ;; Check if we need to translate the link
  6851. (let ((tmp (funcall org-link-translation-function type path)))
  6852. (setq type (car tmp) path (cdr tmp))))
  6853. (cond
  6854. ((assoc type org-link-protocols)
  6855. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6856. ((equal type "mailto")
  6857. (let ((cmd (car org-link-mailto-program))
  6858. (args (cdr org-link-mailto-program)) args1
  6859. (address path) (subject "") a)
  6860. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6861. (setq address (match-string 1 path)
  6862. subject (org-link-escape (match-string 2 path))))
  6863. (while args
  6864. (cond
  6865. ((not (stringp (car args))) (push (pop args) args1))
  6866. (t (setq a (pop args))
  6867. (if (string-match "%a" a)
  6868. (setq a (replace-match address t t a)))
  6869. (if (string-match "%s" a)
  6870. (setq a (replace-match subject t t a)))
  6871. (push a args1))))
  6872. (apply cmd (nreverse args1))))
  6873. ((member type '("http" "https" "ftp" "news"))
  6874. (browse-url (concat type ":" (org-link-escape
  6875. path org-link-escape-chars-browser))))
  6876. ((member type '("message"))
  6877. (browse-url (concat type ":" path)))
  6878. ((string= type "tags")
  6879. (org-tags-view in-emacs path))
  6880. ((string= type "thisfile")
  6881. (if in-emacs
  6882. (switch-to-buffer-other-window
  6883. (org-get-buffer-for-internal-link (current-buffer)))
  6884. (org-mark-ring-push))
  6885. (let ((cmd `(org-link-search
  6886. ,path
  6887. ,(cond ((equal in-emacs '(4)) 'occur)
  6888. ((equal in-emacs '(16)) 'org-occur)
  6889. (t nil))
  6890. ,pos)))
  6891. (condition-case nil (eval cmd)
  6892. (error (progn (widen) (eval cmd))))))
  6893. ((string= type "tree-match")
  6894. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6895. ((string= type "file")
  6896. (if (string-match "::\\([0-9]+\\)\\'" path)
  6897. (setq line (string-to-number (match-string 1 path))
  6898. path (substring path 0 (match-beginning 0)))
  6899. (if (string-match "::\\(.+\\)\\'" path)
  6900. (setq search (match-string 1 path)
  6901. path (substring path 0 (match-beginning 0)))))
  6902. (if (string-match "[*?{]" (file-name-nondirectory path))
  6903. (dired path)
  6904. (org-open-file path in-emacs line search)))
  6905. ((string= type "news")
  6906. (require 'org-gnus)
  6907. (org-gnus-follow-link path))
  6908. ((string= type "shell")
  6909. (let ((cmd path))
  6910. (if (or (not org-confirm-shell-link-function)
  6911. (funcall org-confirm-shell-link-function
  6912. (format "Execute \"%s\" in shell? "
  6913. (org-add-props cmd nil
  6914. 'face 'org-warning))))
  6915. (progn
  6916. (message "Executing %s" cmd)
  6917. (shell-command cmd))
  6918. (error "Abort"))))
  6919. ((string= type "elisp")
  6920. (let ((cmd path))
  6921. (if (or (not org-confirm-elisp-link-function)
  6922. (funcall org-confirm-elisp-link-function
  6923. (format "Execute \"%s\" as elisp? "
  6924. (org-add-props cmd nil
  6925. 'face 'org-warning))))
  6926. (message "%s => %s" cmd
  6927. (if (equal (string-to-char cmd) ?\()
  6928. (eval (read cmd))
  6929. (call-interactively (read cmd))))
  6930. (error "Abort"))))
  6931. (t
  6932. (browse-url-at-point))))))
  6933. (move-marker org-open-link-marker nil)
  6934. (run-hook-with-args 'org-follow-link-hook))
  6935. ;;;; Time estimates
  6936. (defun org-get-effort (&optional pom)
  6937. "Get the effort estimate for the current entry."
  6938. (org-entry-get pom org-effort-property))
  6939. ;;; File search
  6940. (defvar org-create-file-search-functions nil
  6941. "List of functions to construct the right search string for a file link.
  6942. These functions are called in turn with point at the location to
  6943. which the link should point.
  6944. A function in the hook should first test if it would like to
  6945. handle this file type, for example by checking the major-mode or
  6946. the file extension. If it decides not to handle this file, it
  6947. should just return nil to give other functions a chance. If it
  6948. does handle the file, it must return the search string to be used
  6949. when following the link. The search string will be part of the
  6950. file link, given after a double colon, and `org-open-at-point'
  6951. will automatically search for it. If special measures must be
  6952. taken to make the search successful, another function should be
  6953. added to the companion hook `org-execute-file-search-functions',
  6954. which see.
  6955. A function in this hook may also use `setq' to set the variable
  6956. `description' to provide a suggestion for the descriptive text to
  6957. be used for this link when it gets inserted into an Org-mode
  6958. buffer with \\[org-insert-link].")
  6959. (defvar org-execute-file-search-functions nil
  6960. "List of functions to execute a file search triggered by a link.
  6961. Functions added to this hook must accept a single argument, the
  6962. search string that was part of the file link, the part after the
  6963. double colon. The function must first check if it would like to
  6964. handle this search, for example by checking the major-mode or the
  6965. file extension. If it decides not to handle this search, it
  6966. should just return nil to give other functions a chance. If it
  6967. does handle the search, it must return a non-nil value to keep
  6968. other functions from trying.
  6969. Each function can access the current prefix argument through the
  6970. variable `current-prefix-argument'. Note that a single prefix is
  6971. used to force opening a link in Emacs, so it may be good to only
  6972. use a numeric or double prefix to guide the search function.
  6973. In case this is needed, a function in this hook can also restore
  6974. the window configuration before `org-open-at-point' was called using:
  6975. (set-window-configuration org-window-config-before-follow-link)")
  6976. (defun org-link-search (s &optional type avoid-pos)
  6977. "Search for a link search option.
  6978. If S is surrounded by forward slashes, it is interpreted as a
  6979. regular expression. In org-mode files, this will create an `org-occur'
  6980. sparse tree. In ordinary files, `occur' will be used to list matches.
  6981. If the current buffer is in `dired-mode', grep will be used to search
  6982. in all files. If AVOID-POS is given, ignore matches near that position."
  6983. (let ((case-fold-search t)
  6984. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6985. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6986. (append '(("") (" ") ("\t") ("\n"))
  6987. org-emphasis-alist)
  6988. "\\|") "\\)"))
  6989. (pos (point))
  6990. (pre nil) (post nil)
  6991. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6992. (cond
  6993. ;; First check if there are any special
  6994. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6995. ;; Now try the builtin stuff
  6996. ((and (equal (string-to-char s0) ?#)
  6997. (> (length s0) 1)
  6998. (save-excursion
  6999. (goto-char (point-min))
  7000. (and
  7001. (re-search-forward
  7002. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7003. (setq type 'dedicated
  7004. pos (match-beginning 0))))
  7005. ;; There is an exact target for this
  7006. (goto-char pos)
  7007. (org-back-to-heading t)))
  7008. ((save-excursion
  7009. (goto-char (point-min))
  7010. (and
  7011. (re-search-forward
  7012. (concat "<<" (regexp-quote s0) ">>") nil t)
  7013. (setq type 'dedicated
  7014. pos (match-beginning 0))))
  7015. ;; There is an exact target for this
  7016. (goto-char pos))
  7017. ((and (string-match "^(\\(.*\\))$" s0)
  7018. (save-excursion
  7019. (goto-char (point-min))
  7020. (and
  7021. (re-search-forward
  7022. (concat "[^[]" (regexp-quote
  7023. (format org-coderef-label-format
  7024. (match-string 1 s0))))
  7025. nil t)
  7026. (setq type 'dedicated
  7027. pos (1+ (match-beginning 0))))))
  7028. ;; There is a coderef target for this
  7029. (goto-char pos))
  7030. ((string-match "^/\\(.*\\)/$" s)
  7031. ;; A regular expression
  7032. (cond
  7033. ((org-mode-p)
  7034. (org-occur (match-string 1 s)))
  7035. ;;((eq major-mode 'dired-mode)
  7036. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7037. (t (org-do-occur (match-string 1 s)))))
  7038. (t
  7039. ;; A normal search strings
  7040. (when (equal (string-to-char s) ?*)
  7041. ;; Anchor on headlines, post may include tags.
  7042. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7043. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7044. s (substring s 1)))
  7045. (remove-text-properties
  7046. 0 (length s)
  7047. '(face nil mouse-face nil keymap nil fontified nil) s)
  7048. ;; Make a series of regular expressions to find a match
  7049. (setq words (org-split-string s "[ \n\r\t]+")
  7050. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7051. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7052. "\\)" markers)
  7053. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7054. re2a (concat "[ \t\r\n]" re2a_)
  7055. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7056. re4 (concat "[^a-zA-Z_]" re4_)
  7057. re1 (concat pre re2 post)
  7058. re3 (concat pre (if pre re4_ re4) post)
  7059. re5 (concat pre ".*" re4)
  7060. re2 (concat pre re2)
  7061. re2a (concat pre (if pre re2a_ re2a))
  7062. re4 (concat pre (if pre re4_ re4))
  7063. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7064. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7065. re5 "\\)"
  7066. ))
  7067. (cond
  7068. ((eq type 'org-occur) (org-occur reall))
  7069. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7070. (t (goto-char (point-min))
  7071. (setq type 'fuzzy)
  7072. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7073. (org-search-not-self 1 re1 nil t)
  7074. (org-search-not-self 1 re2 nil t)
  7075. (org-search-not-self 1 re2a nil t)
  7076. (org-search-not-self 1 re3 nil t)
  7077. (org-search-not-self 1 re4 nil t)
  7078. (org-search-not-self 1 re5 nil t)
  7079. )
  7080. (goto-char (match-beginning 1))
  7081. (goto-char pos)
  7082. (error "No match")))))
  7083. (t
  7084. ;; Normal string-search
  7085. (goto-char (point-min))
  7086. (if (search-forward s nil t)
  7087. (goto-char (match-beginning 0))
  7088. (error "No match"))))
  7089. (and (org-mode-p) (org-show-context 'link-search))
  7090. type))
  7091. (defun org-search-not-self (group &rest args)
  7092. "Execute `re-search-forward', but only accept matches that do not
  7093. enclose the position of `org-open-link-marker'."
  7094. (let ((m org-open-link-marker))
  7095. (catch 'exit
  7096. (while (apply 're-search-forward args)
  7097. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7098. (goto-char (match-end group))
  7099. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7100. (> (match-beginning 0) (marker-position m))
  7101. (< (match-end 0) (marker-position m)))
  7102. (save-match-data
  7103. (or (not (org-in-regexp
  7104. org-bracket-link-analytic-regexp 1))
  7105. (not (match-end 4)) ; no description
  7106. (and (<= (match-beginning 4) (point))
  7107. (>= (match-end 4) (point))))))
  7108. (throw 'exit (point))))))))
  7109. (defun org-get-buffer-for-internal-link (buffer)
  7110. "Return a buffer to be used for displaying the link target of internal links."
  7111. (cond
  7112. ((not org-display-internal-link-with-indirect-buffer)
  7113. buffer)
  7114. ((string-match "(Clone)$" (buffer-name buffer))
  7115. (message "Buffer is already a clone, not making another one")
  7116. ;; we also do not modify visibility in this case
  7117. buffer)
  7118. (t ; make a new indirect buffer for displaying the link
  7119. (let* ((bn (buffer-name buffer))
  7120. (ibn (concat bn "(Clone)"))
  7121. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7122. (with-current-buffer ib (org-overview))
  7123. ib))))
  7124. (defun org-do-occur (regexp &optional cleanup)
  7125. "Call the Emacs command `occur'.
  7126. If CLEANUP is non-nil, remove the printout of the regular expression
  7127. in the *Occur* buffer. This is useful if the regex is long and not useful
  7128. to read."
  7129. (occur regexp)
  7130. (when cleanup
  7131. (let ((cwin (selected-window)) win beg end)
  7132. (when (setq win (get-buffer-window "*Occur*"))
  7133. (select-window win))
  7134. (goto-char (point-min))
  7135. (when (re-search-forward "match[a-z]+" nil t)
  7136. (setq beg (match-end 0))
  7137. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7138. (setq end (1- (match-beginning 0)))))
  7139. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7140. (goto-char (point-min))
  7141. (select-window cwin))))
  7142. ;;; The mark ring for links jumps
  7143. (defvar org-mark-ring nil
  7144. "Mark ring for positions before jumps in Org-mode.")
  7145. (defvar org-mark-ring-last-goto nil
  7146. "Last position in the mark ring used to go back.")
  7147. ;; Fill and close the ring
  7148. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7149. (loop for i from 1 to org-mark-ring-length do
  7150. (push (make-marker) org-mark-ring))
  7151. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7152. org-mark-ring)
  7153. (defun org-mark-ring-push (&optional pos buffer)
  7154. "Put the current position or POS into the mark ring and rotate it."
  7155. (interactive)
  7156. (setq pos (or pos (point)))
  7157. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7158. (move-marker (car org-mark-ring)
  7159. (or pos (point))
  7160. (or buffer (current-buffer)))
  7161. (message "%s"
  7162. (substitute-command-keys
  7163. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7164. (defun org-mark-ring-goto (&optional n)
  7165. "Jump to the previous position in the mark ring.
  7166. With prefix arg N, jump back that many stored positions. When
  7167. called several times in succession, walk through the entire ring.
  7168. Org-mode commands jumping to a different position in the current file,
  7169. or to another Org-mode file, automatically push the old position
  7170. onto the ring."
  7171. (interactive "p")
  7172. (let (p m)
  7173. (if (eq last-command this-command)
  7174. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7175. (setq p org-mark-ring))
  7176. (setq org-mark-ring-last-goto p)
  7177. (setq m (car p))
  7178. (switch-to-buffer (marker-buffer m))
  7179. (goto-char m)
  7180. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7181. (defun org-remove-angle-brackets (s)
  7182. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7183. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7184. s)
  7185. (defun org-add-angle-brackets (s)
  7186. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7187. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7188. s)
  7189. (defun org-remove-double-quotes (s)
  7190. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7191. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7192. s)
  7193. ;;; Following specific links
  7194. (defun org-follow-timestamp-link ()
  7195. (cond
  7196. ((org-at-date-range-p t)
  7197. (let ((org-agenda-start-on-weekday)
  7198. (t1 (match-string 1))
  7199. (t2 (match-string 2)))
  7200. (setq t1 (time-to-days (org-time-string-to-time t1))
  7201. t2 (time-to-days (org-time-string-to-time t2)))
  7202. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7203. ((org-at-timestamp-p t)
  7204. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7205. (substring (match-string 1) 0 10)))
  7206. 1))
  7207. (t (error "This should not happen"))))
  7208. ;;; Following file links
  7209. (defvar org-wait nil)
  7210. (defun org-open-file (path &optional in-emacs line search)
  7211. "Open the file at PATH.
  7212. First, this expands any special file name abbreviations. Then the
  7213. configuration variable `org-file-apps' is checked if it contains an
  7214. entry for this file type, and if yes, the corresponding command is launched.
  7215. If no application is found, Emacs simply visits the file.
  7216. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7217. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7218. and o use an external application to visit the file.
  7219. Optional LINE specifies a line to go to, optional SEARCH a string to
  7220. search for. If LINE or SEARCH is given, the file will always be
  7221. opened in Emacs.
  7222. If the file does not exist, an error is thrown."
  7223. (setq in-emacs (or in-emacs line search))
  7224. (let* ((file (if (equal path "")
  7225. buffer-file-name
  7226. (substitute-in-file-name (expand-file-name path))))
  7227. (apps (append org-file-apps (org-default-apps)))
  7228. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7229. (dirp (if remp nil (file-directory-p file)))
  7230. (file (if (and dirp org-open-directory-means-index-dot-org)
  7231. (concat (file-name-as-directory file) "index.org")
  7232. file))
  7233. (a-m-a-p (assq 'auto-mode apps))
  7234. (dfile (downcase file))
  7235. (old-buffer (current-buffer))
  7236. (old-pos (point))
  7237. (old-mode major-mode)
  7238. ext cmd)
  7239. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7240. (setq ext (match-string 1 dfile))
  7241. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7242. (setq ext (match-string 1 dfile))))
  7243. (cond
  7244. ((equal in-emacs '(16))
  7245. (setq cmd (cdr (assoc 'system apps))))
  7246. (in-emacs (setq cmd 'emacs))
  7247. (t
  7248. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7249. (and dirp (cdr (assoc 'directory apps)))
  7250. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7251. 'string-match)
  7252. (cdr (assoc ext apps))
  7253. (cdr (assoc t apps))))))
  7254. (when (eq cmd 'system)
  7255. (setq cmd (cdr (assoc 'system apps))))
  7256. (when (eq cmd 'default)
  7257. (setq cmd (cdr (assoc t apps))))
  7258. (when (eq cmd 'mailcap)
  7259. (require 'mailcap)
  7260. (mailcap-parse-mailcaps)
  7261. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7262. (command (mailcap-mime-info mime-type)))
  7263. (if (stringp command)
  7264. (setq cmd command)
  7265. (setq cmd 'emacs))))
  7266. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7267. (not (file-exists-p file))
  7268. (not org-open-non-existing-files))
  7269. (error "No such file: %s" file))
  7270. (cond
  7271. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7272. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7273. (while (string-match "['\"]%s['\"]" cmd)
  7274. (setq cmd (replace-match "%s" t t cmd)))
  7275. (while (string-match "%s" cmd)
  7276. (setq cmd (replace-match
  7277. (save-match-data
  7278. (shell-quote-argument
  7279. (convert-standard-filename file)))
  7280. t t cmd)))
  7281. (save-window-excursion
  7282. (start-process-shell-command cmd nil cmd)
  7283. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7284. ))
  7285. ((or (stringp cmd)
  7286. (eq cmd 'emacs))
  7287. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7288. (widen)
  7289. (if line (goto-line line)
  7290. (if search (org-link-search search))))
  7291. ((consp cmd)
  7292. (let ((file (convert-standard-filename file)))
  7293. (eval cmd)))
  7294. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7295. (and (org-mode-p) (eq old-mode 'org-mode)
  7296. (or (not (equal old-buffer (current-buffer)))
  7297. (not (equal old-pos (point))))
  7298. (org-mark-ring-push old-pos old-buffer))))
  7299. (defun org-default-apps ()
  7300. "Return the default applications for this operating system."
  7301. (cond
  7302. ((eq system-type 'darwin)
  7303. org-file-apps-defaults-macosx)
  7304. ((eq system-type 'windows-nt)
  7305. org-file-apps-defaults-windowsnt)
  7306. (t org-file-apps-defaults-gnu)))
  7307. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7308. "Convert extensions to regular expressions in the cars of LIST.
  7309. Also, weed out any non-string entries, because the return value is used
  7310. only for regexp matching.
  7311. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7312. point to the symbol `emacs', indicating that the file should
  7313. be opened in Emacs."
  7314. (append
  7315. (delq nil
  7316. (mapcar (lambda (x)
  7317. (if (not (stringp (car x)))
  7318. nil
  7319. (if (string-match "\\W" (car x))
  7320. x
  7321. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7322. list))
  7323. (if add-auto-mode
  7324. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7325. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7326. (defun org-file-remote-p (file)
  7327. "Test whether FILE specifies a location on a remote system.
  7328. Return non-nil if the location is indeed remote.
  7329. For example, the filename \"/user@host:/foo\" specifies a location
  7330. on the system \"/user@host:\"."
  7331. (cond ((fboundp 'file-remote-p)
  7332. (file-remote-p file))
  7333. ((fboundp 'tramp-handle-file-remote-p)
  7334. (tramp-handle-file-remote-p file))
  7335. ((and (boundp 'ange-ftp-name-format)
  7336. (string-match (car ange-ftp-name-format) file))
  7337. t)
  7338. (t nil)))
  7339. ;;;; Refiling
  7340. (defun org-get-org-file ()
  7341. "Read a filename, with default directory `org-directory'."
  7342. (let ((default (or org-default-notes-file remember-data-file)))
  7343. (read-file-name (format "File name [%s]: " default)
  7344. (file-name-as-directory org-directory)
  7345. default)))
  7346. (defun org-notes-order-reversed-p ()
  7347. "Check if the current file should receive notes in reversed order."
  7348. (cond
  7349. ((not org-reverse-note-order) nil)
  7350. ((eq t org-reverse-note-order) t)
  7351. ((not (listp org-reverse-note-order)) nil)
  7352. (t (catch 'exit
  7353. (let ((all org-reverse-note-order)
  7354. entry)
  7355. (while (setq entry (pop all))
  7356. (if (string-match (car entry) buffer-file-name)
  7357. (throw 'exit (cdr entry))))
  7358. nil)))))
  7359. (defvar org-refile-target-table nil
  7360. "The list of refile targets, created by `org-refile'.")
  7361. (defvar org-agenda-new-buffers nil
  7362. "Buffers created to visit agenda files.")
  7363. (defun org-get-refile-targets (&optional default-buffer)
  7364. "Produce a table with refile targets."
  7365. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7366. targets txt re files f desc descre fast-path-p level)
  7367. (message "Getting targets...")
  7368. (with-current-buffer (or default-buffer (current-buffer))
  7369. (while (setq entry (pop entries))
  7370. (setq files (car entry) desc (cdr entry))
  7371. (setq fast-path-p nil)
  7372. (cond
  7373. ((null files) (setq files (list (current-buffer))))
  7374. ((eq files 'org-agenda-files)
  7375. (setq files (org-agenda-files 'unrestricted)))
  7376. ((and (symbolp files) (fboundp files))
  7377. (setq files (funcall files)))
  7378. ((and (symbolp files) (boundp files))
  7379. (setq files (symbol-value files))))
  7380. (if (stringp files) (setq files (list files)))
  7381. (cond
  7382. ((eq (car desc) :tag)
  7383. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7384. ((eq (car desc) :todo)
  7385. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7386. ((eq (car desc) :regexp)
  7387. (setq descre (cdr desc)))
  7388. ((eq (car desc) :level)
  7389. (setq descre (concat "^\\*\\{" (number-to-string
  7390. (if org-odd-levels-only
  7391. (1- (* 2 (cdr desc)))
  7392. (cdr desc)))
  7393. "\\}[ \t]")))
  7394. ((eq (car desc) :maxlevel)
  7395. (setq fast-path-p t)
  7396. (setq descre (concat "^\\*\\{1," (number-to-string
  7397. (if org-odd-levels-only
  7398. (1- (* 2 (cdr desc)))
  7399. (cdr desc)))
  7400. "\\}[ \t]")))
  7401. (t (error "Bad refiling target description %s" desc)))
  7402. (while (setq f (pop files))
  7403. (save-excursion
  7404. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7405. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7406. (setq f (expand-file-name f))
  7407. (if (eq org-refile-use-outline-path 'file)
  7408. (push (list (file-name-nondirectory f) f nil nil) targets))
  7409. (save-excursion
  7410. (save-restriction
  7411. (widen)
  7412. (goto-char (point-min))
  7413. (while (re-search-forward descre nil t)
  7414. (goto-char (point-at-bol))
  7415. (catch 'next
  7416. (when org-refile-target-verify-function
  7417. (save-excursion
  7418. (save-match-data
  7419. (or (funcall org-refile-target-verify-function)
  7420. (throw 'next t)))))
  7421. (when (looking-at org-complex-heading-regexp)
  7422. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7423. txt (org-link-display-format (match-string 4))
  7424. re (concat "^" (regexp-quote
  7425. (buffer-substring (match-beginning 1)
  7426. (match-end 4)))))
  7427. (if (match-end 5) (setq re (concat re "[ \t]+"
  7428. (regexp-quote
  7429. (match-string 5)))))
  7430. (setq re (concat re "[ \t]*$"))
  7431. (when org-refile-use-outline-path
  7432. (setq txt (mapconcat 'org-protect-slash
  7433. (append
  7434. (if (eq org-refile-use-outline-path 'file)
  7435. (list (file-name-nondirectory
  7436. (buffer-file-name (buffer-base-buffer))))
  7437. (if (eq org-refile-use-outline-path 'full-file-path)
  7438. (list (buffer-file-name (buffer-base-buffer)))))
  7439. (org-get-outline-path fast-path-p level txt)
  7440. (list txt))
  7441. "/")))
  7442. (push (list txt f re (point)) targets)))
  7443. (goto-char (point-at-eol))))))))
  7444. (message "Getting targets...done")
  7445. (nreverse targets))))
  7446. (defun org-protect-slash (s)
  7447. (while (string-match "/" s)
  7448. (setq s (replace-match "\\" t t s)))
  7449. s)
  7450. (defvar org-olpa (make-vector 20 nil))
  7451. (defun org-get-outline-path (&optional fastp level heading)
  7452. "Return the outline path to the current entry, as a list."
  7453. (if fastp
  7454. (progn
  7455. (if (> level 19)
  7456. (error "Outline path failure, more than 19 levels."))
  7457. (loop for i from level upto 19 do
  7458. (aset org-olpa i nil))
  7459. (prog1
  7460. (delq nil (append org-olpa nil))
  7461. (aset org-olpa level heading)))
  7462. (let (rtn)
  7463. (save-excursion
  7464. (while (org-up-heading-safe)
  7465. (when (looking-at org-complex-heading-regexp)
  7466. (push (org-match-string-no-properties 4) rtn)))
  7467. rtn))))
  7468. (defvar org-refile-history nil
  7469. "History for refiling operations.")
  7470. (defun org-refile (&optional goto default-buffer)
  7471. "Move the entry at point to another heading.
  7472. The list of target headings is compiled using the information in
  7473. `org-refile-targets', which see. This list is created before each use
  7474. and will therefore always be up-to-date.
  7475. At the target location, the entry is filed as a subitem of the target heading.
  7476. Depending on `org-reverse-note-order', the new subitem will either be the
  7477. first or the last subitem.
  7478. If there is an active region, all entries in that region will be moved.
  7479. However, the region must fulfil the requirement that the first heading
  7480. is the first one sets the top-level of the moved text - at most siblings
  7481. below it are allowed.
  7482. With prefix arg GOTO, the command will only visit the target location,
  7483. not actually move anything.
  7484. With a double prefix `C-u C-u', go to the location where the last refiling
  7485. operation has put the subtree.
  7486. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7487. (interactive "P")
  7488. (let* ((cbuf (current-buffer))
  7489. (regionp (org-region-active-p))
  7490. (region-start (and regionp (region-beginning)))
  7491. (region-end (and regionp (region-end)))
  7492. (region-length (and regionp (- region-end region-start)))
  7493. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7494. pos it nbuf file re level reversed)
  7495. (when regionp (goto-char region-start)
  7496. (unless (org-kill-is-subtree-p
  7497. (buffer-substring region-start region-end))
  7498. (error "The region is not a (sequence of) subtree(s)")))
  7499. (if (equal goto '(16))
  7500. (org-refile-goto-last-stored)
  7501. (when (setq it (org-refile-get-location
  7502. (if goto "Goto: " "Refile to: ") default-buffer
  7503. org-refile-allow-creating-parent-nodes))
  7504. (setq file (nth 1 it)
  7505. re (nth 2 it)
  7506. pos (nth 3 it))
  7507. (if (and (not goto)
  7508. pos
  7509. (equal (buffer-file-name) file)
  7510. (if regionp
  7511. (and (>= pos region-start)
  7512. (<= pos region-end))
  7513. (and (>= pos (point))
  7514. (< pos (save-excursion
  7515. (org-end-of-subtree t t))))))
  7516. (error "Cannot refile to position inside the tree or region"))
  7517. (setq nbuf (or (find-buffer-visiting file)
  7518. (find-file-noselect file)))
  7519. (if goto
  7520. (progn
  7521. (switch-to-buffer nbuf)
  7522. (goto-char pos)
  7523. (org-show-context 'org-goto))
  7524. (if regionp
  7525. (progn
  7526. (kill-new (buffer-substring region-start region-end))
  7527. (org-save-markers-in-region region-start region-end))
  7528. (org-copy-subtree 1 nil t))
  7529. (save-excursion
  7530. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7531. (find-file-noselect file))))
  7532. (setq reversed (org-notes-order-reversed-p))
  7533. (save-excursion
  7534. (save-restriction
  7535. (widen)
  7536. (if pos
  7537. (progn
  7538. (goto-char pos)
  7539. (looking-at outline-regexp)
  7540. (setq level (org-get-valid-level (funcall outline-level) 1))
  7541. (goto-char
  7542. (if reversed
  7543. (or (outline-next-heading) (point-max))
  7544. (or (save-excursion (outline-get-next-sibling))
  7545. (org-end-of-subtree t t)
  7546. (point-max)))))
  7547. (setq level 1)
  7548. (if (not reversed)
  7549. (goto-char (point-max))
  7550. (goto-char (point-min))
  7551. (or (outline-next-heading) (goto-char (point-max)))))
  7552. (if (not (bolp)) (newline))
  7553. (bookmark-set "org-refile-last-stored")
  7554. (org-paste-subtree level))))
  7555. (if regionp
  7556. (delete-region (point) (+ (point) region-length))
  7557. (org-cut-subtree))
  7558. (setq org-markers-to-move nil)
  7559. (message "Refiled to \"%s\"" (car it))))))
  7560. (org-reveal))
  7561. (defun org-refile-goto-last-stored ()
  7562. "Go to the location where the last refile was stored."
  7563. (interactive)
  7564. (bookmark-jump "org-refile-last-stored")
  7565. (message "This is the location of the last refile"))
  7566. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7567. "Prompt the user for a refile location, using PROMPT."
  7568. (let ((org-refile-targets org-refile-targets)
  7569. (org-refile-use-outline-path org-refile-use-outline-path))
  7570. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7571. (unless org-refile-target-table
  7572. (error "No refile targets"))
  7573. (let* ((cbuf (current-buffer))
  7574. (partial-completion-mode nil)
  7575. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7576. (cfunc (if (and org-refile-use-outline-path
  7577. org-outline-path-complete-in-steps)
  7578. 'org-olpath-completing-read
  7579. 'org-ido-completing-read))
  7580. (extra (if org-refile-use-outline-path "/" ""))
  7581. (filename (and cfn (expand-file-name cfn)))
  7582. (tbl (mapcar
  7583. (lambda (x)
  7584. (if (and (not (member org-refile-use-outline-path
  7585. '(file full-file-path)))
  7586. (not (equal filename (nth 1 x))))
  7587. (cons (concat (car x) extra " ("
  7588. (file-name-nondirectory (nth 1 x)) ")")
  7589. (cdr x))
  7590. (cons (concat (car x) extra) (cdr x))))
  7591. org-refile-target-table))
  7592. (completion-ignore-case t)
  7593. pa answ parent-target child parent)
  7594. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7595. nil 'org-refile-history))
  7596. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7597. (if pa
  7598. pa
  7599. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7600. (setq parent (match-string 1 answ)
  7601. child (match-string 2 answ))
  7602. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7603. (when (and parent-target
  7604. (or (eq new-nodes t)
  7605. (and (eq new-nodes 'confirm)
  7606. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7607. (org-refile-new-child parent-target child))))))
  7608. (defun org-refile-new-child (parent-target child)
  7609. "Use refile target PARENT-TARGET to add new CHILD below it."
  7610. (unless parent-target
  7611. (error "Cannot find parent for new node"))
  7612. (let ((file (nth 1 parent-target))
  7613. (pos (nth 3 parent-target))
  7614. level)
  7615. (with-current-buffer (or (find-buffer-visiting file)
  7616. (find-file-noselect file))
  7617. (save-excursion
  7618. (save-restriction
  7619. (widen)
  7620. (if pos
  7621. (goto-char pos)
  7622. (goto-char (point-max))
  7623. (if (not (bolp)) (newline)))
  7624. (when (looking-at outline-regexp)
  7625. (setq level (funcall outline-level))
  7626. (org-end-of-subtree t t))
  7627. (org-back-over-empty-lines)
  7628. (insert "\n" (make-string
  7629. (if pos (org-get-valid-level level 1) 1) ?*)
  7630. " " child "\n")
  7631. (beginning-of-line 0)
  7632. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7633. (defun org-olpath-completing-read (prompt collection &rest args)
  7634. "Read an outline path like a file name."
  7635. (let ((thetable collection)
  7636. (org-completion-use-ido nil)) ; does not work with ido.
  7637. (apply
  7638. 'org-ido-completing-read prompt
  7639. (lambda (string predicate &optional flag)
  7640. (let (rtn r f (l (length string)))
  7641. (cond
  7642. ((eq flag nil)
  7643. ;; try completion
  7644. (try-completion string thetable))
  7645. ((eq flag t)
  7646. ;; all-completions
  7647. (setq rtn (all-completions string thetable predicate))
  7648. (mapcar
  7649. (lambda (x)
  7650. (setq r (substring x l))
  7651. (if (string-match " ([^)]*)$" x)
  7652. (setq f (match-string 0 x))
  7653. (setq f ""))
  7654. (if (string-match "/" r)
  7655. (concat string (substring r 0 (match-end 0)) f)
  7656. x))
  7657. rtn))
  7658. ((eq flag 'lambda)
  7659. ;; exact match?
  7660. (assoc string thetable)))
  7661. ))
  7662. args)))
  7663. ;;;; Dynamic blocks
  7664. (defun org-find-dblock (name)
  7665. "Find the first dynamic block with name NAME in the buffer.
  7666. If not found, stay at current position and return nil."
  7667. (let (pos)
  7668. (save-excursion
  7669. (goto-char (point-min))
  7670. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7671. nil t)
  7672. (match-beginning 0))))
  7673. (if pos (goto-char pos))
  7674. pos))
  7675. (defconst org-dblock-start-re
  7676. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7677. "Matches the startline of a dynamic block, with parameters.")
  7678. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7679. "Matches the end of a dynamic block.")
  7680. (defun org-create-dblock (plist)
  7681. "Create a dynamic block section, with parameters taken from PLIST.
  7682. PLIST must contain a :name entry which is used as name of the block."
  7683. (unless (bolp) (newline))
  7684. (let ((name (plist-get plist :name)))
  7685. (insert "#+BEGIN: " name)
  7686. (while plist
  7687. (if (eq (car plist) :name)
  7688. (setq plist (cddr plist))
  7689. (insert " " (prin1-to-string (pop plist)))))
  7690. (insert "\n\n#+END:\n")
  7691. (beginning-of-line -2)))
  7692. (defun org-prepare-dblock ()
  7693. "Prepare dynamic block for refresh.
  7694. This empties the block, puts the cursor at the insert position and returns
  7695. the property list including an extra property :name with the block name."
  7696. (unless (looking-at org-dblock-start-re)
  7697. (error "Not at a dynamic block"))
  7698. (let* ((begdel (1+ (match-end 0)))
  7699. (name (org-no-properties (match-string 1)))
  7700. (params (append (list :name name)
  7701. (read (concat "(" (match-string 3) ")")))))
  7702. (unless (re-search-forward org-dblock-end-re nil t)
  7703. (error "Dynamic block not terminated"))
  7704. (setq params
  7705. (append params
  7706. (list :content (buffer-substring
  7707. begdel (match-beginning 0)))))
  7708. (delete-region begdel (match-beginning 0))
  7709. (goto-char begdel)
  7710. (open-line 1)
  7711. params))
  7712. (defun org-map-dblocks (&optional command)
  7713. "Apply COMMAND to all dynamic blocks in the current buffer.
  7714. If COMMAND is not given, use `org-update-dblock'."
  7715. (let ((cmd (or command 'org-update-dblock))
  7716. pos)
  7717. (save-excursion
  7718. (goto-char (point-min))
  7719. (while (re-search-forward org-dblock-start-re nil t)
  7720. (goto-char (setq pos (match-beginning 0)))
  7721. (condition-case nil
  7722. (funcall cmd)
  7723. (error (message "Error during update of dynamic block")))
  7724. (goto-char pos)
  7725. (unless (re-search-forward org-dblock-end-re nil t)
  7726. (error "Dynamic block not terminated"))))))
  7727. (defun org-dblock-update (&optional arg)
  7728. "User command for updating dynamic blocks.
  7729. Update the dynamic block at point. With prefix ARG, update all dynamic
  7730. blocks in the buffer."
  7731. (interactive "P")
  7732. (if arg
  7733. (org-update-all-dblocks)
  7734. (or (looking-at org-dblock-start-re)
  7735. (org-beginning-of-dblock))
  7736. (org-update-dblock)))
  7737. (defun org-update-dblock ()
  7738. "Update the dynamic block at point
  7739. This means to empty the block, parse for parameters and then call
  7740. the correct writing function."
  7741. (save-window-excursion
  7742. (let* ((pos (point))
  7743. (line (org-current-line))
  7744. (params (org-prepare-dblock))
  7745. (name (plist-get params :name))
  7746. (cmd (intern (concat "org-dblock-write:" name))))
  7747. (message "Updating dynamic block `%s' at line %d..." name line)
  7748. (funcall cmd params)
  7749. (message "Updating dynamic block `%s' at line %d...done" name line)
  7750. (goto-char pos))))
  7751. (defun org-beginning-of-dblock ()
  7752. "Find the beginning of the dynamic block at point.
  7753. Error if there is no such block at point."
  7754. (let ((pos (point))
  7755. beg)
  7756. (end-of-line 1)
  7757. (if (and (re-search-backward org-dblock-start-re nil t)
  7758. (setq beg (match-beginning 0))
  7759. (re-search-forward org-dblock-end-re nil t)
  7760. (> (match-end 0) pos))
  7761. (goto-char beg)
  7762. (goto-char pos)
  7763. (error "Not in a dynamic block"))))
  7764. (defun org-update-all-dblocks ()
  7765. "Update all dynamic blocks in the buffer.
  7766. This function can be used in a hook."
  7767. (when (org-mode-p)
  7768. (org-map-dblocks 'org-update-dblock)))
  7769. ;;;; Completion
  7770. (defconst org-additional-option-like-keywords
  7771. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7772. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7773. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7774. "BEGIN_QUOTE" "END_QUOTE"
  7775. "BEGIN_VERSE" "END_VERSE"
  7776. "BEGIN_CENTER" "END_CENTER"
  7777. "BEGIN_SRC" "END_SRC"
  7778. "CATEGORY" "COLUMNS"
  7779. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7780. (defcustom org-structure-template-alist
  7781. '(
  7782. ("s" "#+begin_src ?\n\n#+end_src"
  7783. "<src lang=\"?\">\n\n</src>")
  7784. ("e" "#+begin_example\n?\n#+end_example"
  7785. "<example>\n?\n</example>")
  7786. ("q" "#+begin_quote\n?\n#+end_quote"
  7787. "<quote>\n?\n</quote>")
  7788. ("v" "#+begin_verse\n?\n#+end_verse"
  7789. "<verse>\n?\n/verse>")
  7790. ("c" "#+begin_center\n?\n#+end_center"
  7791. "<center>\n?\n/center>")
  7792. ("l" "#+begin_latex\n?\n#+end_latex"
  7793. "<literal style=\"latex\">\n?\n</literal>")
  7794. ("L" "#+latex: "
  7795. "<literal style=\"latex\">?</literal>")
  7796. ("h" "#+begin_html\n?\n#+end_html"
  7797. "<literal style=\"html\">\n?\n</literal>")
  7798. ("H" "#+html: "
  7799. "<literal style=\"html\">?</literal>")
  7800. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7801. ("A" "#+ascii: ")
  7802. ("i" "#+include %file ?"
  7803. "<include file=%file markup=\"?\">")
  7804. )
  7805. "Structure completion elements.
  7806. This is a list of abbreviation keys and values. The value gets inserted
  7807. it you type @samp{.} followed by the key and then the completion key,
  7808. usually `M-TAB'. %file will be replaced by a file name after prompting
  7809. for the file using completion.
  7810. There are two templates for each key, the first uses the original Org syntax,
  7811. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7812. the default when the /org-mtags.el/ module has been loaded. See also the
  7813. variable `org-mtags-prefer-muse-templates'.
  7814. This is an experimental feature, it is undecided if it is going to stay in."
  7815. :group 'org-completion
  7816. :type '(repeat
  7817. (string :tag "Key")
  7818. (string :tag "Template")
  7819. (string :tag "Muse Template")))
  7820. (defun org-try-structure-completion ()
  7821. "Try to complete a structure template before point.
  7822. This looks for strings like \"<e\" on an otherwise empty line and
  7823. expands them."
  7824. (let ((l (buffer-substring (point-at-bol) (point)))
  7825. a)
  7826. (when (and (looking-at "[ \t]*$")
  7827. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7828. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7829. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7830. (match-beginning 1)) a)
  7831. t)))
  7832. (defun org-complete-expand-structure-template (start cell)
  7833. "Expand a structure template."
  7834. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7835. (rpl (nth (if musep 2 1) cell)))
  7836. (delete-region start (point))
  7837. (when (string-match "\\`#\\+" rpl)
  7838. (cond
  7839. ((bolp))
  7840. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7841. (delete-region (point-at-bol) (point)))
  7842. (t (newline))))
  7843. (setq start (point))
  7844. (if (string-match "%file" rpl)
  7845. (setq rpl (replace-match
  7846. (concat
  7847. "\""
  7848. (save-match-data
  7849. (abbreviate-file-name (read-file-name "Include file: ")))
  7850. "\"")
  7851. t t rpl)))
  7852. (insert rpl)
  7853. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7854. (defun org-complete (&optional arg)
  7855. "Perform completion on word at point.
  7856. At the beginning of a headline, this completes TODO keywords as given in
  7857. `org-todo-keywords'.
  7858. If the current word is preceded by a backslash, completes the TeX symbols
  7859. that are supported for HTML support.
  7860. If the current word is preceded by \"#+\", completes special words for
  7861. setting file options.
  7862. In the line after \"#+STARTUP:, complete valid keywords.\"
  7863. At all other locations, this simply calls the value of
  7864. `org-completion-fallback-command'."
  7865. (interactive "P")
  7866. (org-without-partial-completion
  7867. (catch 'exit
  7868. (let* ((a nil)
  7869. (end (point))
  7870. (beg1 (save-excursion
  7871. (skip-chars-backward (org-re "[:alnum:]_@"))
  7872. (point)))
  7873. (beg (save-excursion
  7874. (skip-chars-backward "a-zA-Z0-9_:$")
  7875. (point)))
  7876. (confirm (lambda (x) (stringp (car x))))
  7877. (searchhead (equal (char-before beg) ?*))
  7878. (struct
  7879. (when (and (member (char-before beg1) '(?. ?<))
  7880. (setq a (assoc (buffer-substring beg1 (point))
  7881. org-structure-template-alist)))
  7882. (org-complete-expand-structure-template (1- beg1) a)
  7883. (throw 'exit t)))
  7884. (tag (and (equal (char-before beg1) ?:)
  7885. (equal (char-after (point-at-bol)) ?*)))
  7886. (prop (and (equal (char-before beg1) ?:)
  7887. (not (equal (char-after (point-at-bol)) ?*))))
  7888. (texp (equal (char-before beg) ?\\))
  7889. (link (equal (char-before beg) ?\[))
  7890. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7891. beg)
  7892. "#+"))
  7893. (startup (string-match "^#\\+STARTUP:.*"
  7894. (buffer-substring (point-at-bol) (point))))
  7895. (completion-ignore-case opt)
  7896. (type nil)
  7897. (tbl nil)
  7898. (table (cond
  7899. (opt
  7900. (setq type :opt)
  7901. (require 'org-exp)
  7902. (append
  7903. (mapcar
  7904. (lambda (x)
  7905. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7906. (cons (match-string 2 x) (match-string 1 x)))
  7907. (org-split-string (org-get-current-options) "\n"))
  7908. (mapcar 'list org-additional-option-like-keywords)))
  7909. (startup
  7910. (setq type :startup)
  7911. org-startup-options)
  7912. (link (append org-link-abbrev-alist-local
  7913. org-link-abbrev-alist))
  7914. (texp
  7915. (setq type :tex)
  7916. org-html-entities)
  7917. ((string-match "\\`\\*+[ \t]+\\'"
  7918. (buffer-substring (point-at-bol) beg))
  7919. (setq type :todo)
  7920. (mapcar 'list org-todo-keywords-1))
  7921. (searchhead
  7922. (setq type :searchhead)
  7923. (save-excursion
  7924. (goto-char (point-min))
  7925. (while (re-search-forward org-todo-line-regexp nil t)
  7926. (push (list
  7927. (org-make-org-heading-search-string
  7928. (match-string 3) t))
  7929. tbl)))
  7930. tbl)
  7931. (tag (setq type :tag beg beg1)
  7932. (or org-tag-alist (org-get-buffer-tags)))
  7933. (prop (setq type :prop beg beg1)
  7934. (mapcar 'list (org-buffer-property-keys nil t t)))
  7935. (t (progn
  7936. (call-interactively org-completion-fallback-command)
  7937. (throw 'exit nil)))))
  7938. (pattern (buffer-substring-no-properties beg end))
  7939. (completion (try-completion pattern table confirm)))
  7940. (cond ((eq completion t)
  7941. (if (not (assoc (upcase pattern) table))
  7942. (message "Already complete")
  7943. (if (and (equal type :opt)
  7944. (not (member (car (assoc (upcase pattern) table))
  7945. org-additional-option-like-keywords)))
  7946. (insert (substring (cdr (assoc (upcase pattern) table))
  7947. (length pattern)))
  7948. (if (memq type '(:tag :prop)) (insert ":")))))
  7949. ((null completion)
  7950. (message "Can't find completion for \"%s\"" pattern)
  7951. (ding))
  7952. ((not (string= pattern completion))
  7953. (delete-region beg end)
  7954. (if (string-match " +$" completion)
  7955. (setq completion (replace-match "" t t completion)))
  7956. (insert completion)
  7957. (if (get-buffer-window "*Completions*")
  7958. (delete-window (get-buffer-window "*Completions*")))
  7959. (if (assoc completion table)
  7960. (if (eq type :todo) (insert " ")
  7961. (if (memq type '(:tag :prop)) (insert ":"))))
  7962. (if (and (equal type :opt) (assoc completion table))
  7963. (message "%s" (substitute-command-keys
  7964. "Press \\[org-complete] again to insert example settings"))))
  7965. (t
  7966. (message "Making completion list...")
  7967. (let ((list (sort (all-completions pattern table confirm)
  7968. 'string<)))
  7969. (with-output-to-temp-buffer "*Completions*"
  7970. (condition-case nil
  7971. ;; Protection needed for XEmacs and emacs 21
  7972. (display-completion-list list pattern)
  7973. (error (display-completion-list list)))))
  7974. (message "Making completion list...%s" "done")))))))
  7975. ;;;; TODO, DEADLINE, Comments
  7976. (defun org-toggle-comment ()
  7977. "Change the COMMENT state of an entry."
  7978. (interactive)
  7979. (save-excursion
  7980. (org-back-to-heading)
  7981. (let (case-fold-search)
  7982. (if (looking-at (concat outline-regexp
  7983. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7984. (replace-match "" t t nil 1)
  7985. (if (looking-at outline-regexp)
  7986. (progn
  7987. (goto-char (match-end 0))
  7988. (insert org-comment-string " ")))))))
  7989. (defvar org-last-todo-state-is-todo nil
  7990. "This is non-nil when the last TODO state change led to a TODO state.
  7991. If the last change removed the TODO tag or switched to DONE, then
  7992. this is nil.")
  7993. (defvar org-setting-tags nil) ; dynamically skipped
  7994. (defun org-parse-local-options (string var)
  7995. "Parse STRING for startup setting relevant for variable VAR."
  7996. (let ((rtn (symbol-value var))
  7997. e opts)
  7998. (save-match-data
  7999. (if (or (not string) (not (string-match "\\S-" string)))
  8000. rtn
  8001. (setq opts (delq nil (mapcar (lambda (x)
  8002. (setq e (assoc x org-startup-options))
  8003. (if (eq (nth 1 e) var) e nil))
  8004. (org-split-string string "[ \t]+"))))
  8005. (if (not opts)
  8006. rtn
  8007. (setq rtn nil)
  8008. (while (setq e (pop opts))
  8009. (if (not (nth 3 e))
  8010. (setq rtn (nth 2 e))
  8011. (if (not (listp rtn)) (setq rtn nil))
  8012. (push (nth 2 e) rtn)))
  8013. rtn)))))
  8014. (defvar org-todo-setup-filter-hook nil
  8015. "Hook for functions that pre-filter todo specs.
  8016. Each function takes a todo spec and returns either `nil' or the spec
  8017. transformed into canonical form." )
  8018. (defvar org-todo-get-default-hook nil
  8019. "Hook for functions that get a default item for todo.
  8020. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8021. `nil' or a string to be used for the todo mark." )
  8022. (defvar org-agenda-headline-snapshot-before-repeat)
  8023. (defun org-todo (&optional arg)
  8024. "Change the TODO state of an item.
  8025. The state of an item is given by a keyword at the start of the heading,
  8026. like
  8027. *** TODO Write paper
  8028. *** DONE Call mom
  8029. The different keywords are specified in the variable `org-todo-keywords'.
  8030. By default the available states are \"TODO\" and \"DONE\".
  8031. So for this example: when the item starts with TODO, it is changed to DONE.
  8032. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8033. DONE are present, add TODO at the beginning of the heading.
  8034. With C-u prefix arg, use completion to determine the new state.
  8035. With numeric prefix arg, switch to that state.
  8036. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8037. With a tripple C-u prefix, circumvent any state blocking.
  8038. For calling through lisp, arg is also interpreted in the following way:
  8039. 'none -> empty state
  8040. \"\"(empty string) -> switch to empty state
  8041. 'done -> switch to DONE
  8042. 'nextset -> switch to the next set of keywords
  8043. 'previousset -> switch to the previous set of keywords
  8044. \"WAITING\" -> switch to the specified keyword, but only if it
  8045. really is a member of `org-todo-keywords'."
  8046. (interactive "P")
  8047. (if (equal arg '(16)) (setq arg 'nextset))
  8048. (let ((org-blocker-hook org-blocker-hook)
  8049. (case-fold-search nil))
  8050. (when (equal arg '(64))
  8051. (setq arg nil org-blocker-hook nil))
  8052. (when (and org-blocker-hook
  8053. (org-entry-get nil "NOBLOCKING"))
  8054. (setq org-blocker-hook nil))
  8055. (save-excursion
  8056. (catch 'exit
  8057. (org-back-to-heading)
  8058. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8059. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8060. (looking-at " *"))
  8061. (let* ((match-data (match-data))
  8062. (startpos (point-at-bol))
  8063. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8064. (org-log-done org-log-done)
  8065. (org-log-repeat org-log-repeat)
  8066. (org-todo-log-states org-todo-log-states)
  8067. (this (match-string 1))
  8068. (hl-pos (match-beginning 0))
  8069. (head (org-get-todo-sequence-head this))
  8070. (ass (assoc head org-todo-kwd-alist))
  8071. (interpret (nth 1 ass))
  8072. (done-word (nth 3 ass))
  8073. (final-done-word (nth 4 ass))
  8074. (last-state (or this ""))
  8075. (completion-ignore-case t)
  8076. (member (member this org-todo-keywords-1))
  8077. (tail (cdr member))
  8078. (state (cond
  8079. ((and org-todo-key-trigger
  8080. (or (and (equal arg '(4))
  8081. (eq org-use-fast-todo-selection 'prefix))
  8082. (and (not arg) org-use-fast-todo-selection
  8083. (not (eq org-use-fast-todo-selection
  8084. 'prefix)))))
  8085. ;; Use fast selection
  8086. (org-fast-todo-selection))
  8087. ((and (equal arg '(4))
  8088. (or (not org-use-fast-todo-selection)
  8089. (not org-todo-key-trigger)))
  8090. ;; Read a state with completion
  8091. (org-ido-completing-read
  8092. "State: " (mapcar (lambda(x) (list x))
  8093. org-todo-keywords-1)
  8094. nil t))
  8095. ((eq arg 'right)
  8096. (if this
  8097. (if tail (car tail) nil)
  8098. (car org-todo-keywords-1)))
  8099. ((eq arg 'left)
  8100. (if (equal member org-todo-keywords-1)
  8101. nil
  8102. (if this
  8103. (nth (- (length org-todo-keywords-1)
  8104. (length tail) 2)
  8105. org-todo-keywords-1)
  8106. (org-last org-todo-keywords-1))))
  8107. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8108. (setq arg nil))) ; hack to fall back to cycling
  8109. (arg
  8110. ;; user or caller requests a specific state
  8111. (cond
  8112. ((equal arg "") nil)
  8113. ((eq arg 'none) nil)
  8114. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8115. ((eq arg 'nextset)
  8116. (or (car (cdr (member head org-todo-heads)))
  8117. (car org-todo-heads)))
  8118. ((eq arg 'previousset)
  8119. (let ((org-todo-heads (reverse org-todo-heads)))
  8120. (or (car (cdr (member head org-todo-heads)))
  8121. (car org-todo-heads))))
  8122. ((car (member arg org-todo-keywords-1)))
  8123. ((nth (1- (prefix-numeric-value arg))
  8124. org-todo-keywords-1))))
  8125. ((null member) (or head (car org-todo-keywords-1)))
  8126. ((equal this final-done-word) nil) ;; -> make empty
  8127. ((null tail) nil) ;; -> first entry
  8128. ((memq interpret '(type priority))
  8129. (if (eq this-command last-command)
  8130. (car tail)
  8131. (if (> (length tail) 0)
  8132. (or done-word (car org-done-keywords))
  8133. nil)))
  8134. (t
  8135. (car tail))))
  8136. (state (or
  8137. (run-hook-with-args-until-success
  8138. 'org-todo-get-default-hook state last-state)
  8139. state))
  8140. (next (if state (concat " " state " ") " "))
  8141. (change-plist (list :type 'todo-state-change :from this :to state
  8142. :position startpos))
  8143. dolog now-done-p)
  8144. (when org-blocker-hook
  8145. (setq org-last-todo-state-is-todo
  8146. (not (member this org-done-keywords)))
  8147. (unless (save-excursion
  8148. (save-match-data
  8149. (run-hook-with-args-until-failure
  8150. 'org-blocker-hook change-plist)))
  8151. (if (interactive-p)
  8152. (error "TODO state change from %s to %s blocked" this state)
  8153. ;; fail silently
  8154. (message "TODO state change from %s to %s blocked" this state)
  8155. (throw 'exit nil))))
  8156. (store-match-data match-data)
  8157. (replace-match next t t)
  8158. (unless (pos-visible-in-window-p hl-pos)
  8159. (message "TODO state changed to %s" (org-trim next)))
  8160. (unless head
  8161. (setq head (org-get-todo-sequence-head state)
  8162. ass (assoc head org-todo-kwd-alist)
  8163. interpret (nth 1 ass)
  8164. done-word (nth 3 ass)
  8165. final-done-word (nth 4 ass)))
  8166. (when (memq arg '(nextset previousset))
  8167. (message "Keyword-Set %d/%d: %s"
  8168. (- (length org-todo-sets) -1
  8169. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8170. (length org-todo-sets)
  8171. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8172. (setq org-last-todo-state-is-todo
  8173. (not (member state org-done-keywords)))
  8174. (setq now-done-p (and (member state org-done-keywords)
  8175. (not (member this org-done-keywords))))
  8176. (and logging (org-local-logging logging))
  8177. (when (and (or org-todo-log-states org-log-done)
  8178. (not (memq arg '(nextset previousset))))
  8179. ;; we need to look at recording a time and note
  8180. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8181. (nth 2 (assoc this org-todo-log-states))))
  8182. (when (and state
  8183. (member state org-not-done-keywords)
  8184. (not (member this org-not-done-keywords)))
  8185. ;; This is now a todo state and was not one before
  8186. ;; If there was a CLOSED time stamp, get rid of it.
  8187. (org-add-planning-info nil nil 'closed))
  8188. (when (and now-done-p org-log-done)
  8189. ;; It is now done, and it was not done before
  8190. (org-add-planning-info 'closed (org-current-time))
  8191. (if (and (not dolog) (eq 'note org-log-done))
  8192. (org-add-log-setup 'done state this 'findpos 'note)))
  8193. (when (and state dolog)
  8194. ;; This is a non-nil state, and we need to log it
  8195. (org-add-log-setup 'state state this 'findpos dolog)))
  8196. ;; Fixup tag positioning
  8197. (org-todo-trigger-tag-changes state)
  8198. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8199. (when org-provide-todo-statistics
  8200. (org-update-parent-todo-statistics))
  8201. (run-hooks 'org-after-todo-state-change-hook)
  8202. (if (and arg (not (member state org-done-keywords)))
  8203. (setq head (org-get-todo-sequence-head state)))
  8204. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8205. ;; Do we need to trigger a repeat?
  8206. (when now-done-p
  8207. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8208. ;; This is for the agenda, take a snapshot of the headline.
  8209. (save-match-data
  8210. (setq org-agenda-headline-snapshot-before-repeat
  8211. (org-get-heading))))
  8212. (org-auto-repeat-maybe state))
  8213. ;; Fixup cursor location if close to the keyword
  8214. (if (and (outline-on-heading-p)
  8215. (not (bolp))
  8216. (save-excursion (beginning-of-line 1)
  8217. (looking-at org-todo-line-regexp))
  8218. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8219. (progn
  8220. (goto-char (or (match-end 2) (match-end 1)))
  8221. (and (looking-at " ") (just-one-space))))
  8222. (when org-trigger-hook
  8223. (save-excursion
  8224. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8225. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8226. "Block turning an entry into a TODO, using the hierarchy.
  8227. This checks whether the current task should be blocked from state
  8228. changes. Such blocking occurs when:
  8229. 1. The task has children which are not all in a completed state.
  8230. 2. A task has a parent with the property :ORDERED:, and there
  8231. are siblings prior to the current task with incomplete
  8232. status.
  8233. 3. The parent of the task is blocked because it has siblings that should
  8234. be done first, or is child of a block grandparent TODO entry."
  8235. (catch 'dont-block
  8236. ;; If this is not a todo state change, or if this entry is already DONE,
  8237. ;; do not block
  8238. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8239. (member (plist-get change-plist :from)
  8240. (cons 'done org-done-keywords))
  8241. (member (plist-get change-plist :to)
  8242. (cons 'todo org-not-done-keywords)))
  8243. (throw 'dont-block t))
  8244. ;; If this task has children, and any are undone, it's blocked
  8245. (save-excursion
  8246. (org-back-to-heading t)
  8247. (let ((this-level (funcall outline-level)))
  8248. (outline-next-heading)
  8249. (let ((child-level (funcall outline-level)))
  8250. (while (and (not (eobp))
  8251. (> child-level this-level))
  8252. ;; this todo has children, check whether they are all
  8253. ;; completed
  8254. (if (and (not (org-entry-is-done-p))
  8255. (org-entry-is-todo-p))
  8256. (throw 'dont-block nil))
  8257. (outline-next-heading)
  8258. (setq child-level (funcall outline-level))))))
  8259. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8260. ;; any previous siblings are undone, it's blocked
  8261. (save-excursion
  8262. (org-back-to-heading t)
  8263. (let* ((pos (point))
  8264. (parent-pos (and (org-up-heading-safe) (point))))
  8265. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8266. (when (and (org-entry-get (point) "ORDERED")
  8267. (forward-line 1)
  8268. (re-search-forward org-not-done-heading-regexp pos t))
  8269. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8270. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8271. (while t
  8272. (goto-char parent-pos)
  8273. (if (not (looking-at org-not-done-heading-regexp))
  8274. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8275. (setq pos (point))
  8276. (setq parent-pos (and (org-up-heading-safe) (point)))
  8277. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8278. (when (and (org-entry-get (point) "ORDERED")
  8279. (forward-line 1)
  8280. (re-search-forward org-not-done-heading-regexp pos t))
  8281. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8282. (defcustom org-track-ordered-property-with-tag nil
  8283. "Should the ORDERED property also be shown as a tag?
  8284. The ORDERED property decides if an entry should require subtasks to be
  8285. completed in sequence. Since a property is not very visible, setting
  8286. this option means that toggling the ORDERED property with the command
  8287. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8288. not relevant for the behavior, but it makes things more visible.
  8289. Note that toggling the tag with tags commands will not change the property
  8290. and therefore not influence behavior!
  8291. This can be t, meaning the tag ORDERED should be used, It can also be a
  8292. string to select a different tag for this task."
  8293. :group 'org-todo
  8294. :type '(choice
  8295. (const :tag "No tracking" nil)
  8296. (const :tag "Track with ORDERED tag" t)
  8297. (string :tag "Use other tag")))
  8298. (defun org-toggle-ordered-property ()
  8299. "Toggle the ORDERED property of the current entry.
  8300. For better visibility, you can track the value of this property with a tag.
  8301. See variable `org-track-ordered-property-with-tag'."
  8302. (interactive)
  8303. (let* ((t1 org-track-ordered-property-with-tag)
  8304. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8305. (save-excursion
  8306. (org-back-to-heading)
  8307. (if (org-entry-get nil "ORDERED")
  8308. (progn
  8309. (org-delete-property "ORDERED")
  8310. (and tag (org-toggle-tag tag 'off))
  8311. (message "Subtasks can be completed in arbitrary order"))
  8312. (org-entry-put nil "ORDERED" "t")
  8313. (and tag (org-toggle-tag tag 'on))
  8314. (message "Subtasks must be completed in sequence")))))
  8315. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8316. (defun org-block-todo-from-checkboxes (change-plist)
  8317. "Block turning an entry into a TODO, using checkboxes.
  8318. This checks whether the current task should be blocked from state
  8319. changes because there are uncheckd boxes in this entry."
  8320. (catch 'dont-block
  8321. ;; If this is not a todo state change, or if this entry is already DONE,
  8322. ;; do not block
  8323. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8324. (member (plist-get change-plist :from)
  8325. (cons 'done org-done-keywords))
  8326. (member (plist-get change-plist :to)
  8327. (cons 'todo org-not-done-keywords)))
  8328. (throw 'dont-block t))
  8329. ;; If this task has checkboxes that are not checked, it's blocked
  8330. (save-excursion
  8331. (org-back-to-heading t)
  8332. (let ((beg (point)) end)
  8333. (outline-next-heading)
  8334. (setq end (point))
  8335. (goto-char beg)
  8336. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8337. end t)
  8338. (progn
  8339. (if (boundp 'org-blocked-by-checkboxes)
  8340. (setq org-blocked-by-checkboxes t))
  8341. (throw 'dont-block nil)))))
  8342. t)) ; do not block
  8343. (defun org-update-parent-todo-statistics ()
  8344. "Update any statistics cookie in the parent of the current headline."
  8345. (interactive)
  8346. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8347. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8348. (catch 'exit
  8349. (save-excursion
  8350. (setq level (org-up-heading-safe))
  8351. (unless level
  8352. (throw 'exit nil))
  8353. (while (re-search-forward box-re (point-at-eol) t)
  8354. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8355. (setq is-percent (match-end 2))
  8356. (save-match-data
  8357. (unless (outline-next-heading) (throw 'exit nil))
  8358. (while (looking-at org-todo-line-regexp)
  8359. (setq kwd (match-string 2))
  8360. (and kwd (setq cnt-all (1+ cnt-all)))
  8361. (and (member kwd org-done-keywords)
  8362. (setq cnt-done (1+ cnt-done)))
  8363. (condition-case nil
  8364. (org-forward-same-level 1)
  8365. (error (end-of-line 1)))))
  8366. (replace-match
  8367. (if is-percent
  8368. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8369. (format "[%d/%d]" cnt-done cnt-all))))
  8370. (when cookie-present
  8371. (run-hook-with-args 'org-after-todo-statistics-hook
  8372. cnt-done (- cnt-all cnt-done)))))))
  8373. (defvar org-after-todo-statistics-hook nil
  8374. "Hook that is called after a TODO statistics cookie has been updated.
  8375. Each function is called with two arguments: the number of not-done entries
  8376. and the number of done entries.
  8377. For example, the following function, when added to this hook, will switch
  8378. an entry to DONE when all children are done, and back to TODO when new
  8379. entries are set to a TODO status. Note that this hook is only called
  8380. when there is a statistics cookie in the headline!
  8381. (defun org-summary-todo (n-done n-not-done)
  8382. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8383. (let (org-log-done org-log-states) ; turn off logging
  8384. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8385. ")
  8386. (defun org-todo-trigger-tag-changes (state)
  8387. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8388. (let ((l org-todo-state-tags-triggers)
  8389. changes)
  8390. (when (or (not state) (equal state ""))
  8391. (setq changes (append changes (cdr (assoc "" l)))))
  8392. (when (and (stringp state) (> (length state) 0))
  8393. (setq changes (append changes (cdr (assoc state l)))))
  8394. (when (member state org-not-done-keywords)
  8395. (setq changes (append changes (cdr (assoc 'todo l)))))
  8396. (when (member state org-done-keywords)
  8397. (setq changes (append changes (cdr (assoc 'done l)))))
  8398. (dolist (c changes)
  8399. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8400. (defun org-local-logging (value)
  8401. "Get logging settings from a property VALUE."
  8402. (let* (words w a)
  8403. ;; directly set the variables, they are already local.
  8404. (setq org-log-done nil
  8405. org-log-repeat nil
  8406. org-todo-log-states nil)
  8407. (setq words (org-split-string value))
  8408. (while (setq w (pop words))
  8409. (cond
  8410. ((setq a (assoc w org-startup-options))
  8411. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8412. (set (nth 1 a) (nth 2 a))))
  8413. ((setq a (org-extract-log-state-settings w))
  8414. (and (member (car a) org-todo-keywords-1)
  8415. (push a org-todo-log-states)))))))
  8416. (defun org-get-todo-sequence-head (kwd)
  8417. "Return the head of the TODO sequence to which KWD belongs.
  8418. If KWD is not set, check if there is a text property remembering the
  8419. right sequence."
  8420. (let (p)
  8421. (cond
  8422. ((not kwd)
  8423. (or (get-text-property (point-at-bol) 'org-todo-head)
  8424. (progn
  8425. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8426. nil (point-at-eol)))
  8427. (get-text-property p 'org-todo-head))))
  8428. ((not (member kwd org-todo-keywords-1))
  8429. (car org-todo-keywords-1))
  8430. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8431. (defun org-fast-todo-selection ()
  8432. "Fast TODO keyword selection with single keys.
  8433. Returns the new TODO keyword, or nil if no state change should occur."
  8434. (let* ((fulltable org-todo-key-alist)
  8435. (done-keywords org-done-keywords) ;; needed for the faces.
  8436. (maxlen (apply 'max (mapcar
  8437. (lambda (x)
  8438. (if (stringp (car x)) (string-width (car x)) 0))
  8439. fulltable)))
  8440. (expert nil)
  8441. (fwidth (+ maxlen 3 1 3))
  8442. (ncol (/ (- (window-width) 4) fwidth))
  8443. tg cnt e c tbl
  8444. groups ingroup)
  8445. (save-excursion
  8446. (save-window-excursion
  8447. (if expert
  8448. (set-buffer (get-buffer-create " *Org todo*"))
  8449. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8450. (erase-buffer)
  8451. (org-set-local 'org-done-keywords done-keywords)
  8452. (setq tbl fulltable cnt 0)
  8453. (while (setq e (pop tbl))
  8454. (cond
  8455. ((equal e '(:startgroup))
  8456. (push '() groups) (setq ingroup t)
  8457. (when (not (= cnt 0))
  8458. (setq cnt 0)
  8459. (insert "\n"))
  8460. (insert "{ "))
  8461. ((equal e '(:endgroup))
  8462. (setq ingroup nil cnt 0)
  8463. (insert "}\n"))
  8464. ((equal e '(:newline))
  8465. (when (not (= cnt 0))
  8466. (setq cnt 0)
  8467. (insert "\n")
  8468. (setq e (car tbl))
  8469. (while (equal (car tbl) '(:newline))
  8470. (insert "\n")
  8471. (setq tbl (cdr tbl)))))
  8472. (t
  8473. (setq tg (car e) c (cdr e))
  8474. (if ingroup (push tg (car groups)))
  8475. (setq tg (org-add-props tg nil 'face
  8476. (org-get-todo-face tg)))
  8477. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8478. (insert "[" c "] " tg (make-string
  8479. (- fwidth 4 (length tg)) ?\ ))
  8480. (when (= (setq cnt (1+ cnt)) ncol)
  8481. (insert "\n")
  8482. (if ingroup (insert " "))
  8483. (setq cnt 0)))))
  8484. (insert "\n")
  8485. (goto-char (point-min))
  8486. (if (not expert) (org-fit-window-to-buffer))
  8487. (message "[a-z..]:Set [SPC]:clear")
  8488. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8489. (cond
  8490. ((or (= c ?\C-g)
  8491. (and (= c ?q) (not (rassoc c fulltable))))
  8492. (setq quit-flag t))
  8493. ((= c ?\ ) nil)
  8494. ((setq e (rassoc c fulltable) tg (car e))
  8495. tg)
  8496. (t (setq quit-flag t)))))))
  8497. (defun org-entry-is-todo-p ()
  8498. (member (org-get-todo-state) org-not-done-keywords))
  8499. (defun org-entry-is-done-p ()
  8500. (member (org-get-todo-state) org-done-keywords))
  8501. (defun org-get-todo-state ()
  8502. (save-excursion
  8503. (org-back-to-heading t)
  8504. (and (looking-at org-todo-line-regexp)
  8505. (match-end 2)
  8506. (match-string 2))))
  8507. (defun org-at-date-range-p (&optional inactive-ok)
  8508. "Is the cursor inside a date range?"
  8509. (interactive)
  8510. (save-excursion
  8511. (catch 'exit
  8512. (let ((pos (point)))
  8513. (skip-chars-backward "^[<\r\n")
  8514. (skip-chars-backward "<[")
  8515. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8516. (>= (match-end 0) pos)
  8517. (throw 'exit t))
  8518. (skip-chars-backward "^<[\r\n")
  8519. (skip-chars-backward "<[")
  8520. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8521. (>= (match-end 0) pos)
  8522. (throw 'exit t)))
  8523. nil)))
  8524. (defun org-get-repeat ()
  8525. "Check if there is a deadline/schedule with repeater in this entry."
  8526. (save-match-data
  8527. (save-excursion
  8528. (org-back-to-heading t)
  8529. (if (re-search-forward
  8530. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8531. (match-string 1)))))
  8532. (defvar org-last-changed-timestamp)
  8533. (defvar org-last-inserted-timestamp)
  8534. (defvar org-log-post-message)
  8535. (defvar org-log-note-purpose)
  8536. (defvar org-log-note-how)
  8537. (defvar org-log-note-extra)
  8538. (defun org-auto-repeat-maybe (done-word)
  8539. "Check if the current headline contains a repeated deadline/schedule.
  8540. If yes, set TODO state back to what it was and change the base date
  8541. of repeating deadline/scheduled time stamps to new date.
  8542. This function is run automatically after each state change to a DONE state."
  8543. ;; last-state is dynamically scoped into this function
  8544. (let* ((repeat (org-get-repeat))
  8545. (aa (assoc last-state org-todo-kwd-alist))
  8546. (interpret (nth 1 aa))
  8547. (head (nth 2 aa))
  8548. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8549. (msg "Entry repeats: ")
  8550. (org-log-done nil)
  8551. (org-todo-log-states nil)
  8552. (nshiftmax 10) (nshift 0)
  8553. re type n what ts time)
  8554. (when repeat
  8555. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8556. (org-todo (if (eq interpret 'type) last-state head))
  8557. (when org-log-repeat
  8558. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8559. (memq 'org-add-log-note post-command-hook))
  8560. ;; OK, we are already setup for some record
  8561. (if (eq org-log-repeat 'note)
  8562. ;; make sure we take a note, not only a time stamp
  8563. (setq org-log-note-how 'note))
  8564. ;; Set up for taking a record
  8565. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8566. last-state
  8567. 'findpos org-log-repeat)))
  8568. (org-back-to-heading t)
  8569. (org-add-planning-info nil nil 'closed)
  8570. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8571. org-deadline-time-regexp "\\)\\|\\("
  8572. org-ts-regexp "\\)"))
  8573. (while (re-search-forward
  8574. re (save-excursion (outline-next-heading) (point)) t)
  8575. (setq type (if (match-end 1) org-scheduled-string
  8576. (if (match-end 3) org-deadline-string "Plain:"))
  8577. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8578. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8579. (setq n (string-to-number (match-string 2 ts))
  8580. what (match-string 3 ts))
  8581. (if (equal what "w") (setq n (* n 7) what "d"))
  8582. ;; Preparation, see if we need to modify the start date for the change
  8583. (when (match-end 1)
  8584. (setq time (save-match-data (org-time-string-to-time ts)))
  8585. (cond
  8586. ((equal (match-string 1 ts) ".")
  8587. ;; Shift starting date to today
  8588. (org-timestamp-change
  8589. (- (time-to-days (current-time)) (time-to-days time))
  8590. 'day))
  8591. ((equal (match-string 1 ts) "+")
  8592. (while (or (= nshift 0)
  8593. (<= (time-to-days time) (time-to-days (current-time))))
  8594. (when (= (incf nshift) nshiftmax)
  8595. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8596. (error "Abort")))
  8597. (org-timestamp-change n (cdr (assoc what whata)))
  8598. (org-at-timestamp-p t)
  8599. (setq ts (match-string 1))
  8600. (setq time (save-match-data (org-time-string-to-time ts))))
  8601. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8602. ;; rematch, so that we have everything in place for the real shift
  8603. (org-at-timestamp-p t)
  8604. (setq ts (match-string 1))
  8605. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8606. (org-timestamp-change n (cdr (assoc what whata)))
  8607. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8608. (setq org-log-post-message msg)
  8609. (message "%s" msg))))
  8610. (defun org-show-todo-tree (arg)
  8611. "Make a compact tree which shows all headlines marked with TODO.
  8612. The tree will show the lines where the regexp matches, and all higher
  8613. headlines above the match.
  8614. With a \\[universal-argument] prefix, also show the DONE entries.
  8615. With a numeric prefix N, construct a sparse tree for the Nth element
  8616. of `org-todo-keywords-1'."
  8617. (interactive "P")
  8618. (let ((case-fold-search nil)
  8619. (kwd-re
  8620. (cond ((null arg) org-not-done-regexp)
  8621. ((equal arg '(4))
  8622. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8623. (mapcar 'list org-todo-keywords-1))))
  8624. (concat "\\("
  8625. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8626. "\\)\\>")))
  8627. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8628. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8629. org-todo-keywords-1)))
  8630. (t (error "Invalid prefix argument: %s" arg)))))
  8631. (message "%d TODO entries found"
  8632. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8633. (defun org-deadline (&optional remove time)
  8634. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8635. With argument REMOVE, remove any deadline from the item.
  8636. When TIME is set, it should be an internal time specification, and the
  8637. scheduling will use the corresponding date."
  8638. (interactive "P")
  8639. (if remove
  8640. (progn
  8641. (org-remove-timestamp-with-keyword org-deadline-string)
  8642. (message "Item no longer has a deadline."))
  8643. (if (org-get-repeat)
  8644. (error "Cannot change deadline on task with repeater, please do that by hand")
  8645. (org-add-planning-info 'deadline time 'closed)
  8646. (message "Deadline on %s" org-last-inserted-timestamp))))
  8647. (defun org-schedule (&optional remove time)
  8648. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8649. With argument REMOVE, remove any scheduling date from the item.
  8650. When TIME is set, it should be an internal time specification, and the
  8651. scheduling will use the corresponding date."
  8652. (interactive "P")
  8653. (if remove
  8654. (progn
  8655. (org-remove-timestamp-with-keyword org-scheduled-string)
  8656. (message "Item is no longer scheduled."))
  8657. (if (org-get-repeat)
  8658. (error "Cannot reschedule task with repeater, please do that by hand")
  8659. (org-add-planning-info 'scheduled time 'closed)
  8660. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8661. (defun org-get-scheduled-time (pom &optional inherit)
  8662. "Get the scheduled time as a time tuple, of a format suitable
  8663. for calling org-schedule with, or if there is no scheduling,
  8664. returns nil."
  8665. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8666. (when time
  8667. (apply 'encode-time (org-parse-time-string time)))))
  8668. (defun org-get-deadline-time (pom &optional inherit)
  8669. "Get the deadine as a time tuple, of a format suitable for
  8670. calling org-deadlin with, or if there is no scheduling, returns
  8671. nil."
  8672. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8673. (when time
  8674. (apply 'encode-time (org-parse-time-string time)))))
  8675. (defun org-remove-timestamp-with-keyword (keyword)
  8676. "Remove all time stamps with KEYWORD in the current entry."
  8677. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8678. beg)
  8679. (save-excursion
  8680. (org-back-to-heading t)
  8681. (setq beg (point))
  8682. (org-end-of-subtree t t)
  8683. (while (re-search-backward re beg t)
  8684. (replace-match "")
  8685. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8686. (equal (char-before) ?\ ))
  8687. (backward-delete-char 1)
  8688. (if (string-match "^[ \t]*$" (buffer-substring
  8689. (point-at-bol) (point-at-eol)))
  8690. (delete-region (point-at-bol)
  8691. (min (point-max) (1+ (point-at-eol))))))))))
  8692. (defun org-add-planning-info (what &optional time &rest remove)
  8693. "Insert new timestamp with keyword in the line directly after the headline.
  8694. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8695. If non is given, the user is prompted for a date.
  8696. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8697. be removed."
  8698. (interactive)
  8699. (let (org-time-was-given org-end-time-was-given ts
  8700. end default-time default-input)
  8701. (catch 'exit
  8702. (when (and (not time) (memq what '(scheduled deadline)))
  8703. ;; Try to get a default date/time from existing timestamp
  8704. (save-excursion
  8705. (org-back-to-heading t)
  8706. (setq end (save-excursion (outline-next-heading) (point)))
  8707. (when (re-search-forward (if (eq what 'scheduled)
  8708. org-scheduled-time-regexp
  8709. org-deadline-time-regexp)
  8710. end t)
  8711. (setq ts (match-string 1)
  8712. default-time
  8713. (apply 'encode-time (org-parse-time-string ts))
  8714. default-input (and ts (org-get-compact-tod ts))))))
  8715. (when what
  8716. ;; If necessary, get the time from the user
  8717. (setq time (or time (org-read-date nil 'to-time nil nil
  8718. default-time default-input))))
  8719. (when (and org-insert-labeled-timestamps-at-point
  8720. (member what '(scheduled deadline)))
  8721. (insert
  8722. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8723. (org-insert-time-stamp time org-time-was-given
  8724. nil nil nil (list org-end-time-was-given))
  8725. (setq what nil))
  8726. (save-excursion
  8727. (save-restriction
  8728. (let (col list elt ts buffer-invisibility-spec)
  8729. (org-back-to-heading t)
  8730. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8731. (goto-char (match-end 1))
  8732. (setq col (current-column))
  8733. (goto-char (match-end 0))
  8734. (if (eobp) (insert "\n") (forward-char 1))
  8735. (when (and (not what)
  8736. (not (looking-at
  8737. (concat "[ \t]*"
  8738. org-keyword-time-not-clock-regexp))))
  8739. ;; Nothing to add, nothing to remove...... :-)
  8740. (throw 'exit nil))
  8741. (if (and (not (looking-at outline-regexp))
  8742. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8743. "[^\r\n]*"))
  8744. (not (equal (match-string 1) org-clock-string)))
  8745. (narrow-to-region (match-beginning 0) (match-end 0))
  8746. (insert-before-markers "\n")
  8747. (backward-char 1)
  8748. (narrow-to-region (point) (point))
  8749. (and org-adapt-indentation (org-indent-to-column col)))
  8750. ;; Check if we have to remove something.
  8751. (setq list (cons what remove))
  8752. (while list
  8753. (setq elt (pop list))
  8754. (goto-char (point-min))
  8755. (when (or (and (eq elt 'scheduled)
  8756. (re-search-forward org-scheduled-time-regexp nil t))
  8757. (and (eq elt 'deadline)
  8758. (re-search-forward org-deadline-time-regexp nil t))
  8759. (and (eq elt 'closed)
  8760. (re-search-forward org-closed-time-regexp nil t)))
  8761. (replace-match "")
  8762. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8763. (if (looking-at " +") (replace-match ""))))
  8764. (goto-char (point-max))
  8765. (when what
  8766. (insert
  8767. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8768. (cond ((eq what 'scheduled) org-scheduled-string)
  8769. ((eq what 'deadline) org-deadline-string)
  8770. ((eq what 'closed) org-closed-string))
  8771. " ")
  8772. (setq ts (org-insert-time-stamp
  8773. time
  8774. (or org-time-was-given
  8775. (and (eq what 'closed) org-log-done-with-time))
  8776. (eq what 'closed)
  8777. nil nil (list org-end-time-was-given)))
  8778. (end-of-line 1))
  8779. (goto-char (point-min))
  8780. (widen)
  8781. (if (and (looking-at "[ \t]+\n")
  8782. (equal (char-before) ?\n))
  8783. (delete-region (1- (point)) (point-at-eol)))
  8784. ts))))))
  8785. (defvar org-log-note-marker (make-marker))
  8786. (defvar org-log-note-purpose nil)
  8787. (defvar org-log-note-state nil)
  8788. (defvar org-log-note-previous-state nil)
  8789. (defvar org-log-note-how nil)
  8790. (defvar org-log-note-extra nil)
  8791. (defvar org-log-note-window-configuration nil)
  8792. (defvar org-log-note-return-to (make-marker))
  8793. (defvar org-log-post-message nil
  8794. "Message to be displayed after a log note has been stored.
  8795. The auto-repeater uses this.")
  8796. (defun org-add-note ()
  8797. "Add a note to the current entry.
  8798. This is done in the same way as adding a state change note."
  8799. (interactive)
  8800. (org-add-log-setup 'note nil nil 'findpos nil))
  8801. (defvar org-property-end-re)
  8802. (defun org-add-log-setup (&optional purpose state prev-state
  8803. findpos how &optional extra)
  8804. "Set up the post command hook to take a note.
  8805. If this is about to TODO state change, the new state is expected in STATE.
  8806. When FINDPOS is non-nil, find the correct position for the note in
  8807. the current entry. If not, assume that it can be inserted at point.
  8808. HOW is an indicator what kind of note should be created.
  8809. EXTRA is additional text that will be inserted into the notes buffer."
  8810. (let ((drawer (cond ((stringp org-log-into-drawer)
  8811. org-log-into-drawer)
  8812. (org-log-into-drawer "LOGBOOK")
  8813. (t nil))))
  8814. (save-restriction
  8815. (save-excursion
  8816. (when findpos
  8817. (org-back-to-heading t)
  8818. (narrow-to-region (point) (save-excursion
  8819. (outline-next-heading) (point)))
  8820. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8821. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8822. "[^\r\n]*\\)?"))
  8823. (goto-char (match-end 0))
  8824. (cond
  8825. (drawer
  8826. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8827. nil t)
  8828. (progn
  8829. (goto-char (match-end 0))
  8830. (or org-log-states-order-reversed
  8831. (and (re-search-forward org-property-end-re nil t)
  8832. (goto-char (1- (match-beginning 0))))))
  8833. (insert "\n:" drawer ":\n:END:")
  8834. (beginning-of-line 0)
  8835. (org-indent-line-function)
  8836. (beginning-of-line 2)
  8837. (org-indent-line-function)
  8838. (end-of-line 0)))
  8839. ((and org-log-state-notes-insert-after-drawers
  8840. (save-excursion
  8841. (forward-line) (looking-at org-drawer-regexp)))
  8842. (forward-line)
  8843. (while (looking-at org-drawer-regexp)
  8844. (goto-char (match-end 0))
  8845. (re-search-forward org-property-end-re (point-max) t)
  8846. (forward-line))
  8847. (forward-line -1)))
  8848. (unless org-log-states-order-reversed
  8849. (and (= (char-after) ?\n) (forward-char 1))
  8850. (org-skip-over-state-notes)
  8851. (skip-chars-backward " \t\n\r")))
  8852. (move-marker org-log-note-marker (point))
  8853. (setq org-log-note-purpose purpose
  8854. org-log-note-state state
  8855. org-log-note-previous-state prev-state
  8856. org-log-note-how how
  8857. org-log-note-extra extra)
  8858. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8859. (defun org-skip-over-state-notes ()
  8860. "Skip past the list of State notes in an entry."
  8861. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8862. (while (looking-at "[ \t]*- State")
  8863. (condition-case nil
  8864. (org-next-item)
  8865. (error (org-end-of-item)))))
  8866. (defun org-add-log-note (&optional purpose)
  8867. "Pop up a window for taking a note, and add this note later at point."
  8868. (remove-hook 'post-command-hook 'org-add-log-note)
  8869. (setq org-log-note-window-configuration (current-window-configuration))
  8870. (delete-other-windows)
  8871. (move-marker org-log-note-return-to (point))
  8872. (switch-to-buffer (marker-buffer org-log-note-marker))
  8873. (goto-char org-log-note-marker)
  8874. (org-switch-to-buffer-other-window "*Org Note*")
  8875. (erase-buffer)
  8876. (if (memq org-log-note-how '(time state))
  8877. (let (current-prefix-arg) (org-store-log-note))
  8878. (let ((org-inhibit-startup t)) (org-mode))
  8879. (insert (format "# Insert note for %s.
  8880. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8881. (cond
  8882. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8883. ((eq org-log-note-purpose 'done) "closed todo item")
  8884. ((eq org-log-note-purpose 'state)
  8885. (format "state change from \"%s\" to \"%s\""
  8886. (or org-log-note-previous-state "")
  8887. (or org-log-note-state "")))
  8888. ((eq org-log-note-purpose 'note)
  8889. "this entry")
  8890. (t (error "This should not happen")))))
  8891. (if org-log-note-extra (insert org-log-note-extra))
  8892. (org-set-local 'org-finish-function 'org-store-log-note)))
  8893. (defvar org-note-abort nil) ; dynamically scoped
  8894. (defun org-store-log-note ()
  8895. "Finish taking a log note, and insert it to where it belongs."
  8896. (let ((txt (buffer-string))
  8897. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8898. lines ind)
  8899. (kill-buffer (current-buffer))
  8900. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8901. (setq txt (replace-match "" t t txt)))
  8902. (if (string-match "\\s-+\\'" txt)
  8903. (setq txt (replace-match "" t t txt)))
  8904. (setq lines (org-split-string txt "\n"))
  8905. (when (and note (string-match "\\S-" note))
  8906. (setq note
  8907. (org-replace-escapes
  8908. note
  8909. (list (cons "%u" (user-login-name))
  8910. (cons "%U" user-full-name)
  8911. (cons "%t" (format-time-string
  8912. (org-time-stamp-format 'long 'inactive)
  8913. (current-time)))
  8914. (cons "%s" (if org-log-note-state
  8915. (concat "\"" org-log-note-state "\"")
  8916. ""))
  8917. (cons "%S" (if org-log-note-previous-state
  8918. (concat "\"" org-log-note-previous-state "\"")
  8919. "\"\"")))))
  8920. (if lines (setq note (concat note " \\\\")))
  8921. (push note lines))
  8922. (when (or current-prefix-arg org-note-abort)
  8923. (when org-log-into-drawer
  8924. (org-remove-empty-drawer-at
  8925. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8926. org-log-note-marker))
  8927. (setq lines nil))
  8928. (when lines
  8929. (save-excursion
  8930. (set-buffer (marker-buffer org-log-note-marker))
  8931. (save-excursion
  8932. (goto-char org-log-note-marker)
  8933. (move-marker org-log-note-marker nil)
  8934. (end-of-line 1)
  8935. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8936. (insert "- " (pop lines))
  8937. (org-indent-line-function)
  8938. (beginning-of-line 1)
  8939. (looking-at "[ \t]*")
  8940. (setq ind (concat (match-string 0) " "))
  8941. (end-of-line 1)
  8942. (while lines (insert "\n" ind (pop lines)))
  8943. (message "Note stored")
  8944. (org-back-to-heading t)
  8945. (org-cycle-hide-drawers 'children)))))
  8946. (set-window-configuration org-log-note-window-configuration)
  8947. (with-current-buffer (marker-buffer org-log-note-return-to)
  8948. (goto-char org-log-note-return-to))
  8949. (move-marker org-log-note-return-to nil)
  8950. (and org-log-post-message (message "%s" org-log-post-message)))
  8951. (defun org-remove-empty-drawer-at (drawer pos)
  8952. "Remove an emptyr DARWER drawer at position POS.
  8953. POS may also be a marker."
  8954. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8955. (save-excursion
  8956. (save-restriction
  8957. (widen)
  8958. (goto-char pos)
  8959. (if (org-in-regexp
  8960. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8961. (replace-match ""))))))
  8962. (defun org-sparse-tree (&optional arg)
  8963. "Create a sparse tree, prompt for the details.
  8964. This command can create sparse trees. You first need to select the type
  8965. of match used to create the tree:
  8966. t Show entries with a specific TODO keyword.
  8967. m Show entries selected by a tags/property match.
  8968. p Enter a property name and its value (both with completion on existing
  8969. names/values) and show entries with that property.
  8970. r Show entries matching a regular expression.
  8971. d Show deadlines due within `org-deadline-warning-days'.
  8972. b Show deadlines and scheduled items before a date.
  8973. a Show deadlines and scheduled items after a date."
  8974. (interactive "P")
  8975. (let (ans kwd value)
  8976. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  8977. (setq ans (read-char-exclusive))
  8978. (cond
  8979. ((equal ans ?d)
  8980. (call-interactively 'org-check-deadlines))
  8981. ((equal ans ?b)
  8982. (call-interactively 'org-check-before-date))
  8983. ((equal ans ?a)
  8984. (call-interactively 'org-check-after-date))
  8985. ((equal ans ?t)
  8986. (org-show-todo-tree '(4)))
  8987. ((member ans '(?T ?m))
  8988. (call-interactively 'org-match-sparse-tree))
  8989. ((member ans '(?p ?P))
  8990. (setq kwd (org-ido-completing-read "Property: "
  8991. (mapcar 'list (org-buffer-property-keys))))
  8992. (setq value (org-ido-completing-read "Value: "
  8993. (mapcar 'list (org-property-values kwd))))
  8994. (unless (string-match "\\`{.*}\\'" value)
  8995. (setq value (concat "\"" value "\"")))
  8996. (org-match-sparse-tree arg (concat kwd "=" value)))
  8997. ((member ans '(?r ?R ?/))
  8998. (call-interactively 'org-occur))
  8999. (t (error "No such sparse tree command \"%c\"" ans)))))
  9000. (defvar org-occur-highlights nil
  9001. "List of overlays used for occur matches.")
  9002. (make-variable-buffer-local 'org-occur-highlights)
  9003. (defvar org-occur-parameters nil
  9004. "Parameters of the active org-occur calls.
  9005. This is a list, each call to org-occur pushes as cons cell,
  9006. containing the regular expression and the callback, onto the list.
  9007. The list can contain several entries if `org-occur' has been called
  9008. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9009. will only contain one set of parameters. When the highlights are
  9010. removed (for example with `C-c C-c', or with the next edit (depending
  9011. on `org-remove-highlights-with-change'), this variable is emptied
  9012. as well.")
  9013. (make-variable-buffer-local 'org-occur-parameters)
  9014. (defun org-occur (regexp &optional keep-previous callback)
  9015. "Make a compact tree which shows all matches of REGEXP.
  9016. The tree will show the lines where the regexp matches, and all higher
  9017. headlines above the match. It will also show the heading after the match,
  9018. to make sure editing the matching entry is easy.
  9019. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9020. call to `org-occur' will be kept, to allow stacking of calls to this
  9021. command.
  9022. If CALLBACK is non-nil, it is a function which is called to confirm
  9023. that the match should indeed be shown."
  9024. (interactive "sRegexp: \nP")
  9025. (unless keep-previous
  9026. (org-remove-occur-highlights nil nil t))
  9027. (push (cons regexp callback) org-occur-parameters)
  9028. (let ((cnt 0))
  9029. (save-excursion
  9030. (goto-char (point-min))
  9031. (if (or (not keep-previous) ; do not want to keep
  9032. (not org-occur-highlights)) ; no previous matches
  9033. ;; hide everything
  9034. (org-overview))
  9035. (while (re-search-forward regexp nil t)
  9036. (when (or (not callback)
  9037. (save-match-data (funcall callback)))
  9038. (setq cnt (1+ cnt))
  9039. (when org-highlight-sparse-tree-matches
  9040. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9041. (org-show-context 'occur-tree))))
  9042. (when org-remove-highlights-with-change
  9043. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9044. nil 'local))
  9045. (unless org-sparse-tree-open-archived-trees
  9046. (org-hide-archived-subtrees (point-min) (point-max)))
  9047. (run-hooks 'org-occur-hook)
  9048. (if (interactive-p)
  9049. (message "%d match(es) for regexp %s" cnt regexp))
  9050. cnt))
  9051. (defun org-show-context (&optional key)
  9052. "Make sure point and context and visible.
  9053. How much context is shown depends upon the variables
  9054. `org-show-hierarchy-above', `org-show-following-heading'. and
  9055. `org-show-siblings'."
  9056. (let ((heading-p (org-on-heading-p t))
  9057. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9058. (following-p (org-get-alist-option org-show-following-heading key))
  9059. (entry-p (org-get-alist-option org-show-entry-below key))
  9060. (siblings-p (org-get-alist-option org-show-siblings key)))
  9061. (catch 'exit
  9062. ;; Show heading or entry text
  9063. (if (and heading-p (not entry-p))
  9064. (org-flag-heading nil) ; only show the heading
  9065. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9066. (org-show-hidden-entry))) ; show entire entry
  9067. (when following-p
  9068. ;; Show next sibling, or heading below text
  9069. (save-excursion
  9070. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9071. (org-flag-heading nil))))
  9072. (when siblings-p (org-show-siblings))
  9073. (when hierarchy-p
  9074. ;; show all higher headings, possibly with siblings
  9075. (save-excursion
  9076. (while (and (condition-case nil
  9077. (progn (org-up-heading-all 1) t)
  9078. (error nil))
  9079. (not (bobp)))
  9080. (org-flag-heading nil)
  9081. (when siblings-p (org-show-siblings))))))))
  9082. (defun org-reveal (&optional siblings)
  9083. "Show current entry, hierarchy above it, and the following headline.
  9084. This can be used to show a consistent set of context around locations
  9085. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9086. not t for the search context.
  9087. With optional argument SIBLINGS, on each level of the hierarchy all
  9088. siblings are shown. This repairs the tree structure to what it would
  9089. look like when opened with hierarchical calls to `org-cycle'."
  9090. (interactive "P")
  9091. (let ((org-show-hierarchy-above t)
  9092. (org-show-following-heading t)
  9093. (org-show-siblings (if siblings t org-show-siblings)))
  9094. (org-show-context nil)))
  9095. (defun org-highlight-new-match (beg end)
  9096. "Highlight from BEG to END and mark the highlight is an occur headline."
  9097. (let ((ov (org-make-overlay beg end)))
  9098. (org-overlay-put ov 'face 'secondary-selection)
  9099. (push ov org-occur-highlights)))
  9100. (defun org-remove-occur-highlights (&optional beg end noremove)
  9101. "Remove the occur highlights from the buffer.
  9102. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9103. from the `before-change-functions' in the current buffer."
  9104. (interactive)
  9105. (unless org-inhibit-highlight-removal
  9106. (mapc 'org-delete-overlay org-occur-highlights)
  9107. (setq org-occur-highlights nil)
  9108. (setq org-occur-parameters nil)
  9109. (unless noremove
  9110. (remove-hook 'before-change-functions
  9111. 'org-remove-occur-highlights 'local))))
  9112. ;;;; Priorities
  9113. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9114. "Regular expression matching the priority indicator.")
  9115. (defvar org-remove-priority-next-time nil)
  9116. (defun org-priority-up ()
  9117. "Increase the priority of the current item."
  9118. (interactive)
  9119. (org-priority 'up))
  9120. (defun org-priority-down ()
  9121. "Decrease the priority of the current item."
  9122. (interactive)
  9123. (org-priority 'down))
  9124. (defun org-priority (&optional action)
  9125. "Change the priority of an item by ARG.
  9126. ACTION can be `set', `up', `down', or a character."
  9127. (interactive)
  9128. (setq action (or action 'set))
  9129. (let (current new news have remove)
  9130. (save-excursion
  9131. (org-back-to-heading t)
  9132. (if (looking-at org-priority-regexp)
  9133. (setq current (string-to-char (match-string 2))
  9134. have t)
  9135. (setq current org-default-priority))
  9136. (cond
  9137. ((or (eq action 'set)
  9138. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9139. (if (not (eq action 'set))
  9140. (setq new action)
  9141. (message "Priority %c-%c, SPC to remove: "
  9142. org-highest-priority org-lowest-priority)
  9143. (setq new (read-char-exclusive)))
  9144. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9145. (= (upcase org-lowest-priority) org-lowest-priority))
  9146. (setq new (upcase new)))
  9147. (cond ((equal new ?\ ) (setq remove t))
  9148. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9149. (error "Priority must be between `%c' and `%c'"
  9150. org-highest-priority org-lowest-priority))))
  9151. ((eq action 'up)
  9152. (if (and (not have) (eq last-command this-command))
  9153. (setq new org-lowest-priority)
  9154. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9155. org-default-priority (1- current)))))
  9156. ((eq action 'down)
  9157. (if (and (not have) (eq last-command this-command))
  9158. (setq new org-highest-priority)
  9159. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9160. org-default-priority (1+ current)))))
  9161. (t (error "Invalid action")))
  9162. (if (or (< (upcase new) org-highest-priority)
  9163. (> (upcase new) org-lowest-priority))
  9164. (setq remove t))
  9165. (setq news (format "%c" new))
  9166. (if have
  9167. (if remove
  9168. (replace-match "" t t nil 1)
  9169. (replace-match news t t nil 2))
  9170. (if remove
  9171. (error "No priority cookie found in line")
  9172. (let ((case-fold-search nil))
  9173. (looking-at org-todo-line-regexp))
  9174. (if (match-end 2)
  9175. (progn
  9176. (goto-char (match-end 2))
  9177. (insert " [#" news "]"))
  9178. (goto-char (match-beginning 3))
  9179. (insert "[#" news "] ")))))
  9180. (org-preserve-lc (org-set-tags nil 'align))
  9181. (if remove
  9182. (message "Priority removed")
  9183. (message "Priority of current item set to %s" news))))
  9184. (defun org-get-priority (s)
  9185. "Find priority cookie and return priority."
  9186. (save-match-data
  9187. (if (not (string-match org-priority-regexp s))
  9188. (* 1000 (- org-lowest-priority org-default-priority))
  9189. (* 1000 (- org-lowest-priority
  9190. (string-to-char (match-string 2 s)))))))
  9191. ;;;; Tags
  9192. (defvar org-agenda-archives-mode)
  9193. (defvar org-map-continue-from nil
  9194. "Position from where mapping should continue.
  9195. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9196. (defvar org-scanner-tags nil
  9197. "The current tag list while the tags scanner is running.")
  9198. (defvar org-trust-scanner-tags nil
  9199. "Should `org-get-tags-at' use the tags fro the scanner.
  9200. This is for internal dynamical scoping only.
  9201. When this is non-nil, the function `org-get-tags-at' will return the value
  9202. of `org-scanner-tags' instead of building the list by itself. This
  9203. can lead to large speed-ups when the tags scanner is used in a file with
  9204. many entries, and when the list of tags is retrieved, for example to
  9205. obtain a list of properties. Building the tags list for each entry in such
  9206. a file becomes an N^2 operation - but with this variable set, it scales
  9207. as N.")
  9208. (defun org-scan-tags (action matcher &optional todo-only)
  9209. "Scan headline tags with inheritance and produce output ACTION.
  9210. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9211. or `agenda' to produce an entry list for an agenda view. It can also be
  9212. a Lisp form or a function that should be called at each matched headline, in
  9213. this case the return value is a list of all return values from these calls.
  9214. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9215. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9216. only lines with a TODO keyword are included in the output."
  9217. (require 'org-agenda)
  9218. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9219. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9220. (org-re
  9221. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9222. (props (list 'face 'default
  9223. 'done-face 'org-agenda-done
  9224. 'undone-face 'default
  9225. 'mouse-face 'highlight
  9226. 'org-not-done-regexp org-not-done-regexp
  9227. 'org-todo-regexp org-todo-regexp
  9228. 'keymap org-agenda-keymap
  9229. 'help-echo
  9230. (format "mouse-2 or RET jump to org file %s"
  9231. (abbreviate-file-name
  9232. (or (buffer-file-name (buffer-base-buffer))
  9233. (buffer-name (buffer-base-buffer)))))))
  9234. (case-fold-search nil)
  9235. (org-map-continue-from nil)
  9236. lspos tags tags-list
  9237. (tags-alist (list (cons 0 org-file-tags)))
  9238. (llast 0) rtn rtn1 level category i txt
  9239. todo marker entry priority)
  9240. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9241. (setq action (list 'lambda nil action)))
  9242. (save-excursion
  9243. (goto-char (point-min))
  9244. (when (eq action 'sparse-tree)
  9245. (org-overview)
  9246. (org-remove-occur-highlights))
  9247. (while (re-search-forward re nil t)
  9248. (catch :skip
  9249. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9250. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9251. (goto-char (setq lspos (match-beginning 0)))
  9252. (setq level (org-reduced-level (funcall outline-level))
  9253. category (org-get-category))
  9254. (setq i llast llast level)
  9255. ;; remove tag lists from same and sublevels
  9256. (while (>= i level)
  9257. (when (setq entry (assoc i tags-alist))
  9258. (setq tags-alist (delete entry tags-alist)))
  9259. (setq i (1- i)))
  9260. ;; add the next tags
  9261. (when tags
  9262. (setq tags (org-split-string tags ":")
  9263. tags-alist
  9264. (cons (cons level tags) tags-alist)))
  9265. ;; compile tags for current headline
  9266. (setq tags-list
  9267. (if org-use-tag-inheritance
  9268. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9269. tags)
  9270. org-scanner-tags tags-list)
  9271. (when org-use-tag-inheritance
  9272. (setcdr (car tags-alist)
  9273. (mapcar (lambda (x)
  9274. (setq x (copy-sequence x))
  9275. (org-add-prop-inherited x))
  9276. (cdar tags-alist))))
  9277. (when (and tags org-use-tag-inheritance
  9278. (or (not (eq t org-use-tag-inheritance))
  9279. org-tags-exclude-from-inheritance))
  9280. ;; selective inheritance, remove uninherited ones
  9281. (setcdr (car tags-alist)
  9282. (org-remove-uniherited-tags (cdar tags-alist))))
  9283. (when (and (or (not todo-only)
  9284. (and (member todo org-not-done-keywords)
  9285. (or (not org-agenda-tags-todo-honor-ignore-options)
  9286. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9287. (let ((case-fold-search t)) (eval matcher))
  9288. (or
  9289. (not (member org-archive-tag tags-list))
  9290. ;; we have an archive tag, should we use this anyway?
  9291. (or (not org-agenda-skip-archived-trees)
  9292. (and (eq action 'agenda) org-agenda-archives-mode))))
  9293. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9294. ;; select this headline
  9295. (cond
  9296. ((eq action 'sparse-tree)
  9297. (and org-highlight-sparse-tree-matches
  9298. (org-get-heading) (match-end 0)
  9299. (org-highlight-new-match
  9300. (match-beginning 0) (match-beginning 1)))
  9301. (org-show-context 'tags-tree))
  9302. ((eq action 'agenda)
  9303. (setq txt (org-format-agenda-item
  9304. ""
  9305. (concat
  9306. (if (eq org-tags-match-list-sublevels 'indented)
  9307. (make-string (1- level) ?.) "")
  9308. (org-get-heading))
  9309. category
  9310. tags-list
  9311. )
  9312. priority (org-get-priority txt))
  9313. (goto-char lspos)
  9314. (setq marker (org-agenda-new-marker))
  9315. (org-add-props txt props
  9316. 'org-marker marker 'org-hd-marker marker 'org-category category
  9317. 'todo-state todo
  9318. 'priority priority 'type "tagsmatch")
  9319. (push txt rtn))
  9320. ((functionp action)
  9321. (setq org-map-continue-from nil)
  9322. (save-excursion
  9323. (setq rtn1 (funcall action))
  9324. (push rtn1 rtn)))
  9325. (t (error "Invalid action")))
  9326. ;; if we are to skip sublevels, jump to end of subtree
  9327. (unless org-tags-match-list-sublevels
  9328. (org-end-of-subtree t)
  9329. (backward-char 1))))
  9330. ;; Get the correct position from where to continue
  9331. (if org-map-continue-from
  9332. (goto-char org-map-continue-from)
  9333. (and (= (point) lspos) (end-of-line 1)))))
  9334. (when (and (eq action 'sparse-tree)
  9335. (not org-sparse-tree-open-archived-trees))
  9336. (org-hide-archived-subtrees (point-min) (point-max)))
  9337. (nreverse rtn)))
  9338. (defun org-remove-uniherited-tags (tags)
  9339. "Remove all tags that are not inherited from the list TAGS."
  9340. (cond
  9341. ((eq org-use-tag-inheritance t)
  9342. (if org-tags-exclude-from-inheritance
  9343. (org-delete-all org-tags-exclude-from-inheritance tags)
  9344. tags))
  9345. ((not org-use-tag-inheritance) nil)
  9346. ((stringp org-use-tag-inheritance)
  9347. (delq nil (mapcar
  9348. (lambda (x)
  9349. (if (and (string-match org-use-tag-inheritance x)
  9350. (not (member x org-tags-exclude-from-inheritance)))
  9351. x nil))
  9352. tags)))
  9353. ((listp org-use-tag-inheritance)
  9354. (delq nil (mapcar
  9355. (lambda (x)
  9356. (if (member x org-use-tag-inheritance) x nil))
  9357. tags)))))
  9358. (defvar todo-only) ;; dynamically scoped
  9359. (defun org-match-sparse-tree (&optional todo-only match)
  9360. "Create a sparse tree according to tags string MATCH.
  9361. MATCH can contain positive and negative selection of tags, like
  9362. \"+WORK+URGENT-WITHBOSS\".
  9363. If optional argument TODO-ONLY is non-nil, only select lines that are
  9364. also TODO lines."
  9365. (interactive "P")
  9366. (org-prepare-agenda-buffers (list (current-buffer)))
  9367. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9368. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9369. (defvar org-cached-props nil)
  9370. (defun org-cached-entry-get (pom property)
  9371. (if (or (eq t org-use-property-inheritance)
  9372. (and (stringp org-use-property-inheritance)
  9373. (string-match org-use-property-inheritance property))
  9374. (and (listp org-use-property-inheritance)
  9375. (member property org-use-property-inheritance)))
  9376. ;; Caching is not possible, check it directly
  9377. (org-entry-get pom property 'inherit)
  9378. ;; Get all properties, so that we can do complicated checks easily
  9379. (cdr (assoc property (or org-cached-props
  9380. (setq org-cached-props
  9381. (org-entry-properties pom)))))))
  9382. (defun org-global-tags-completion-table (&optional files)
  9383. "Return the list of all tags in all agenda buffer/files."
  9384. (save-excursion
  9385. (org-uniquify
  9386. (delq nil
  9387. (apply 'append
  9388. (mapcar
  9389. (lambda (file)
  9390. (set-buffer (find-file-noselect file))
  9391. (append (org-get-buffer-tags)
  9392. (mapcar (lambda (x) (if (stringp (car-safe x))
  9393. (list (car-safe x)) nil))
  9394. org-tag-alist)))
  9395. (if (and files (car files))
  9396. files
  9397. (org-agenda-files))))))))
  9398. (defun org-make-tags-matcher (match)
  9399. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9400. ;; todo-only is scoped dynamically into this function, and the function
  9401. ;; may change it if the matcher asks for it.
  9402. (unless match
  9403. ;; Get a new match request, with completion
  9404. (let ((org-last-tags-completion-table
  9405. (org-global-tags-completion-table)))
  9406. (setq match (org-completing-read-no-ido
  9407. "Match: " 'org-tags-completion-function nil nil nil
  9408. 'org-tags-history))))
  9409. ;; Parse the string and create a lisp form
  9410. (let ((match0 match)
  9411. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9412. minus tag mm
  9413. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9414. orterms term orlist re-p str-p level-p level-op time-p
  9415. prop-p pn pv po cat-p gv rest)
  9416. (if (string-match "/+" match)
  9417. ;; match contains also a todo-matching request
  9418. (progn
  9419. (setq tagsmatch (substring match 0 (match-beginning 0))
  9420. todomatch (substring match (match-end 0)))
  9421. (if (string-match "^!" todomatch)
  9422. (setq todo-only t todomatch (substring todomatch 1)))
  9423. (if (string-match "^\\s-*$" todomatch)
  9424. (setq todomatch nil)))
  9425. ;; only matching tags
  9426. (setq tagsmatch match todomatch nil))
  9427. ;; Make the tags matcher
  9428. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9429. (setq tagsmatcher t)
  9430. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9431. (while (setq term (pop orterms))
  9432. (while (and (equal (substring term -1) "\\") orterms)
  9433. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9434. (while (string-match re term)
  9435. (setq rest (substring term (match-end 0))
  9436. minus (and (match-end 1)
  9437. (equal (match-string 1 term) "-"))
  9438. tag (match-string 2 term)
  9439. re-p (equal (string-to-char tag) ?{)
  9440. level-p (match-end 4)
  9441. prop-p (match-end 5)
  9442. mm (cond
  9443. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9444. (level-p
  9445. (setq level-op (org-op-to-function (match-string 3 term)))
  9446. `(,level-op level ,(string-to-number
  9447. (match-string 4 term))))
  9448. (prop-p
  9449. (setq pn (match-string 5 term)
  9450. po (match-string 6 term)
  9451. pv (match-string 7 term)
  9452. cat-p (equal pn "CATEGORY")
  9453. re-p (equal (string-to-char pv) ?{)
  9454. str-p (equal (string-to-char pv) ?\")
  9455. time-p (save-match-data
  9456. (string-match "^\"[[<].*[]>]\"$" pv))
  9457. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9458. (if time-p (setq pv (org-matcher-time pv)))
  9459. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9460. (cond
  9461. ((equal pn "CATEGORY")
  9462. (setq gv '(get-text-property (point) 'org-category)))
  9463. ((equal pn "TODO")
  9464. (setq gv 'todo))
  9465. (t
  9466. (setq gv `(org-cached-entry-get nil ,pn))))
  9467. (if re-p
  9468. (if (eq po 'org<>)
  9469. `(not (string-match ,pv (or ,gv "")))
  9470. `(string-match ,pv (or ,gv "")))
  9471. (if str-p
  9472. `(,po (or ,gv "") ,pv)
  9473. `(,po (string-to-number (or ,gv ""))
  9474. ,(string-to-number pv) ))))
  9475. (t `(member ,tag tags-list)))
  9476. mm (if minus (list 'not mm) mm)
  9477. term rest)
  9478. (push mm tagsmatcher))
  9479. (push (if (> (length tagsmatcher) 1)
  9480. (cons 'and tagsmatcher)
  9481. (car tagsmatcher))
  9482. orlist)
  9483. (setq tagsmatcher nil))
  9484. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9485. (setq tagsmatcher
  9486. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9487. ;; Make the todo matcher
  9488. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9489. (setq todomatcher t)
  9490. (setq orterms (org-split-string todomatch "|") orlist nil)
  9491. (while (setq term (pop orterms))
  9492. (while (string-match re term)
  9493. (setq minus (and (match-end 1)
  9494. (equal (match-string 1 term) "-"))
  9495. kwd (match-string 2 term)
  9496. re-p (equal (string-to-char kwd) ?{)
  9497. term (substring term (match-end 0))
  9498. mm (if re-p
  9499. `(string-match ,(substring kwd 1 -1) todo)
  9500. (list 'equal 'todo kwd))
  9501. mm (if minus (list 'not mm) mm))
  9502. (push mm todomatcher))
  9503. (push (if (> (length todomatcher) 1)
  9504. (cons 'and todomatcher)
  9505. (car todomatcher))
  9506. orlist)
  9507. (setq todomatcher nil))
  9508. (setq todomatcher (if (> (length orlist) 1)
  9509. (cons 'or orlist) (car orlist))))
  9510. ;; Return the string and lisp forms of the matcher
  9511. (setq matcher (if todomatcher
  9512. (list 'and tagsmatcher todomatcher)
  9513. tagsmatcher))
  9514. (cons match0 matcher)))
  9515. (defun org-op-to-function (op &optional stringp)
  9516. "Turn an operator into the appropriate function."
  9517. (setq op
  9518. (cond
  9519. ((equal op "<" ) '(< string< org-time<))
  9520. ((equal op ">" ) '(> org-string> org-time>))
  9521. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9522. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9523. ((member op '("=" "==")) '(= string= org-time=))
  9524. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9525. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9526. (defun org<> (a b) (not (= a b)))
  9527. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9528. (defun org-string>= (a b) (not (string< a b)))
  9529. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9530. (defun org-string<> (a b) (not (string= a b)))
  9531. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9532. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9533. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9534. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9535. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9536. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9537. (defun org-2ft (s)
  9538. "Convert S to a floating point time.
  9539. If S is already a number, just return it. If it is a string, parse
  9540. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9541. (cond
  9542. ((numberp s) s)
  9543. ((stringp s)
  9544. (condition-case nil
  9545. (float-time (apply 'encode-time (org-parse-time-string s)))
  9546. (error 0.)))
  9547. (t 0.)))
  9548. (defun org-time-today ()
  9549. "Time in seconds today at 0:00.
  9550. Returns the float number of seconds since the beginning of the
  9551. epoch to the beginning of today (00:00)."
  9552. (float-time (apply 'encode-time
  9553. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9554. (defun org-matcher-time (s)
  9555. "Interpret a time comparison value."
  9556. (save-match-data
  9557. (cond
  9558. ((string= s "<now>") (float-time))
  9559. ((string= s "<today>") (org-time-today))
  9560. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9561. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9562. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9563. (+ (org-time-today)
  9564. (* (string-to-number (match-string 1 s))
  9565. (cdr (assoc (match-string 2 s)
  9566. '(("d" . 86400.0) ("w" . 604800.0)
  9567. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9568. (t (org-2ft s)))))
  9569. (defun org-match-any-p (re list)
  9570. "Does re match any element of list?"
  9571. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9572. (delq nil list))
  9573. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9574. (defvar org-tags-overlay (org-make-overlay 1 1))
  9575. (org-detach-overlay org-tags-overlay)
  9576. (defun org-get-local-tags-at (&optional pos)
  9577. "Get a list of tags defined in the current headline."
  9578. (org-get-tags-at pos 'local))
  9579. (defun org-get-local-tags ()
  9580. "Get a list of tags defined in the current headline."
  9581. (org-get-tags-at nil 'local))
  9582. (defun org-get-tags-at (&optional pos local)
  9583. "Get a list of all headline tags applicable at POS.
  9584. POS defaults to point. If tags are inherited, the list contains
  9585. the targets in the same sequence as the headlines appear, i.e.
  9586. the tags of the current headline come last.
  9587. When LOCAL is non-nil, only return tags from the current headline,
  9588. ignore inherited ones."
  9589. (interactive)
  9590. (if (and org-trust-scanner-tags
  9591. (or (not pos) (equal pos (point)))
  9592. (not local))
  9593. org-scanner-tags
  9594. (let (tags ltags lastpos parent)
  9595. (save-excursion
  9596. (save-restriction
  9597. (widen)
  9598. (goto-char (or pos (point)))
  9599. (save-match-data
  9600. (catch 'done
  9601. (condition-case nil
  9602. (progn
  9603. (org-back-to-heading t)
  9604. (while (not (equal lastpos (point)))
  9605. (setq lastpos (point))
  9606. (when (looking-at
  9607. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9608. (setq ltags (org-split-string
  9609. (org-match-string-no-properties 1) ":"))
  9610. (when parent
  9611. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9612. (setq tags (append
  9613. (if parent
  9614. (org-remove-uniherited-tags ltags)
  9615. ltags)
  9616. tags)))
  9617. (or org-use-tag-inheritance (throw 'done t))
  9618. (if local (throw 'done t))
  9619. (org-up-heading-all 1)
  9620. (setq parent t)))
  9621. (error nil)))))
  9622. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9623. (defun org-add-prop-inherited (s)
  9624. (add-text-properties 0 (length s) '(inherited t) s)
  9625. s)
  9626. (defun org-toggle-tag (tag &optional onoff)
  9627. "Toggle the tag TAG for the current line.
  9628. If ONOFF is `on' or `off', don't toggle but set to this state."
  9629. (let (res current)
  9630. (save-excursion
  9631. (org-back-to-heading t)
  9632. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9633. (point-at-eol) t)
  9634. (progn
  9635. (setq current (match-string 1))
  9636. (replace-match ""))
  9637. (setq current ""))
  9638. (setq current (nreverse (org-split-string current ":")))
  9639. (cond
  9640. ((eq onoff 'on)
  9641. (setq res t)
  9642. (or (member tag current) (push tag current)))
  9643. ((eq onoff 'off)
  9644. (or (not (member tag current)) (setq current (delete tag current))))
  9645. (t (if (member tag current)
  9646. (setq current (delete tag current))
  9647. (setq res t)
  9648. (push tag current))))
  9649. (end-of-line 1)
  9650. (if current
  9651. (progn
  9652. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9653. (org-set-tags nil t))
  9654. (delete-horizontal-space))
  9655. (run-hooks 'org-after-tags-change-hook))
  9656. res))
  9657. (defun org-align-tags-here (to-col)
  9658. ;; Assumes that this is a headline
  9659. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9660. (beginning-of-line 1)
  9661. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9662. (< pos (match-beginning 2)))
  9663. (progn
  9664. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9665. (goto-char (match-beginning 1))
  9666. (insert " ")
  9667. (delete-region (point) (1+ (match-beginning 2)))
  9668. (setq ncol (max (1+ (current-column))
  9669. (1+ col)
  9670. (if (> to-col 0)
  9671. to-col
  9672. (- (abs to-col) tags-l))))
  9673. (setq p (point))
  9674. (insert (make-string (- ncol (current-column)) ?\ ))
  9675. (setq ncol (current-column))
  9676. (when indent-tabs-mode (tabify p (point-at-eol)))
  9677. (org-move-to-column (min ncol col) t))
  9678. (goto-char pos))))
  9679. (defun org-set-tags-command (&optional arg just-align)
  9680. "Call the set-tags command for the current entry."
  9681. (interactive "P")
  9682. (if (org-on-heading-p)
  9683. (org-set-tags arg just-align)
  9684. (save-excursion
  9685. (org-back-to-heading t)
  9686. (org-set-tags arg just-align))))
  9687. (defun org-set-tags (&optional arg just-align)
  9688. "Set the tags for the current headline.
  9689. With prefix ARG, realign all tags in headings in the current buffer."
  9690. (interactive "P")
  9691. (let* ((re (concat "^" outline-regexp))
  9692. (current (org-get-tags-string))
  9693. (col (current-column))
  9694. (org-setting-tags t)
  9695. table current-tags inherited-tags ; computed below when needed
  9696. tags p0 c0 c1 rpl)
  9697. (if arg
  9698. (save-excursion
  9699. (goto-char (point-min))
  9700. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9701. (while (re-search-forward re nil t)
  9702. (org-set-tags nil t)
  9703. (end-of-line 1)))
  9704. (message "All tags realigned to column %d" org-tags-column))
  9705. (if just-align
  9706. (setq tags current)
  9707. ;; Get a new set of tags from the user
  9708. (save-excursion
  9709. (setq table (append org-tag-persistent-alist
  9710. (or org-tag-alist (org-get-buffer-tags)))
  9711. org-last-tags-completion-table table
  9712. current-tags (org-split-string current ":")
  9713. inherited-tags (nreverse
  9714. (nthcdr (length current-tags)
  9715. (nreverse (org-get-tags-at))))
  9716. tags
  9717. (if (or (eq t org-use-fast-tag-selection)
  9718. (and org-use-fast-tag-selection
  9719. (delq nil (mapcar 'cdr table))))
  9720. (org-fast-tag-selection
  9721. current-tags inherited-tags table
  9722. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9723. (let ((org-add-colon-after-tag-completion t))
  9724. (org-trim
  9725. (org-without-partial-completion
  9726. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9727. nil nil current 'org-tags-history)))))))
  9728. (while (string-match "[-+&]+" tags)
  9729. ;; No boolean logic, just a list
  9730. (setq tags (replace-match ":" t t tags))))
  9731. (if (string-match "\\`[\t ]*\\'" tags)
  9732. (setq tags "")
  9733. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9734. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9735. ;; Insert new tags at the correct column
  9736. (beginning-of-line 1)
  9737. (cond
  9738. ((and (equal current "") (equal tags "")))
  9739. ((re-search-forward
  9740. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9741. (point-at-eol) t)
  9742. (if (equal tags "")
  9743. (setq rpl "")
  9744. (goto-char (match-beginning 0))
  9745. (setq c0 (current-column) p0 (point)
  9746. c1 (max (1+ c0) (if (> org-tags-column 0)
  9747. org-tags-column
  9748. (- (- org-tags-column) (length tags))))
  9749. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9750. (replace-match rpl t t)
  9751. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9752. tags)
  9753. (t (error "Tags alignment failed")))
  9754. (org-move-to-column col)
  9755. (unless just-align
  9756. (run-hooks 'org-after-tags-change-hook)))))
  9757. (defun org-change-tag-in-region (beg end tag off)
  9758. "Add or remove TAG for each entry in the region.
  9759. This works in the agenda, and also in an org-mode buffer."
  9760. (interactive
  9761. (list (region-beginning) (region-end)
  9762. (let ((org-last-tags-completion-table
  9763. (if (org-mode-p)
  9764. (org-get-buffer-tags)
  9765. (org-global-tags-completion-table))))
  9766. (org-ido-completing-read
  9767. "Tag: " 'org-tags-completion-function nil nil nil
  9768. 'org-tags-history))
  9769. (progn
  9770. (message "[s]et or [r]emove? ")
  9771. (equal (read-char-exclusive) ?r))))
  9772. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9773. (let ((agendap (equal major-mode 'org-agenda-mode))
  9774. l1 l2 m buf pos newhead (cnt 0))
  9775. (goto-char end)
  9776. (setq l2 (1- (org-current-line)))
  9777. (goto-char beg)
  9778. (setq l1 (org-current-line))
  9779. (loop for l from l1 to l2 do
  9780. (goto-line l)
  9781. (setq m (get-text-property (point) 'org-hd-marker))
  9782. (when (or (and (org-mode-p) (org-on-heading-p))
  9783. (and agendap m))
  9784. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9785. pos (if agendap m (point)))
  9786. (with-current-buffer buf
  9787. (save-excursion
  9788. (save-restriction
  9789. (goto-char pos)
  9790. (setq cnt (1+ cnt))
  9791. (org-toggle-tag tag (if off 'off 'on))
  9792. (setq newhead (org-get-heading)))))
  9793. (and agendap (org-agenda-change-all-lines newhead m))))
  9794. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9795. (defun org-tags-completion-function (string predicate &optional flag)
  9796. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9797. (confirm (lambda (x) (stringp (car x)))))
  9798. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9799. (setq s1 (match-string 1 string)
  9800. s2 (match-string 2 string))
  9801. (setq s1 "" s2 string))
  9802. (cond
  9803. ((eq flag nil)
  9804. ;; try completion
  9805. (setq rtn (try-completion s2 ctable confirm))
  9806. (if (stringp rtn)
  9807. (setq rtn
  9808. (concat s1 s2 (substring rtn (length s2))
  9809. (if (and org-add-colon-after-tag-completion
  9810. (assoc rtn ctable))
  9811. ":" ""))))
  9812. rtn)
  9813. ((eq flag t)
  9814. ;; all-completions
  9815. (all-completions s2 ctable confirm)
  9816. )
  9817. ((eq flag 'lambda)
  9818. ;; exact match?
  9819. (assoc s2 ctable)))
  9820. ))
  9821. (defun org-fast-tag-insert (kwd tags face &optional end)
  9822. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9823. (insert (format "%-12s" (concat kwd ":"))
  9824. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9825. (or end "")))
  9826. (defun org-fast-tag-show-exit (flag)
  9827. (save-excursion
  9828. (goto-line 3)
  9829. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9830. (replace-match ""))
  9831. (when flag
  9832. (end-of-line 1)
  9833. (org-move-to-column (- (window-width) 19) t)
  9834. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9835. (defun org-set-current-tags-overlay (current prefix)
  9836. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9837. (if (featurep 'xemacs)
  9838. (org-overlay-display org-tags-overlay (concat prefix s)
  9839. 'secondary-selection)
  9840. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9841. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9842. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9843. "Fast tag selection with single keys.
  9844. CURRENT is the current list of tags in the headline, INHERITED is the
  9845. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9846. possibly with grouping information. TODO-TABLE is a similar table with
  9847. TODO keywords, should these have keys assigned to them.
  9848. If the keys are nil, a-z are automatically assigned.
  9849. Returns the new tags string, or nil to not change the current settings."
  9850. (let* ((fulltable (append table todo-table))
  9851. (maxlen (apply 'max (mapcar
  9852. (lambda (x)
  9853. (if (stringp (car x)) (string-width (car x)) 0))
  9854. fulltable)))
  9855. (buf (current-buffer))
  9856. (expert (eq org-fast-tag-selection-single-key 'expert))
  9857. (buffer-tags nil)
  9858. (fwidth (+ maxlen 3 1 3))
  9859. (ncol (/ (- (window-width) 4) fwidth))
  9860. (i-face 'org-done)
  9861. (c-face 'org-todo)
  9862. tg cnt e c char c1 c2 ntable tbl rtn
  9863. ov-start ov-end ov-prefix
  9864. (exit-after-next org-fast-tag-selection-single-key)
  9865. (done-keywords org-done-keywords)
  9866. groups ingroup)
  9867. (save-excursion
  9868. (beginning-of-line 1)
  9869. (if (looking-at
  9870. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9871. (setq ov-start (match-beginning 1)
  9872. ov-end (match-end 1)
  9873. ov-prefix "")
  9874. (setq ov-start (1- (point-at-eol))
  9875. ov-end (1+ ov-start))
  9876. (skip-chars-forward "^\n\r")
  9877. (setq ov-prefix
  9878. (concat
  9879. (buffer-substring (1- (point)) (point))
  9880. (if (> (current-column) org-tags-column)
  9881. " "
  9882. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9883. (org-move-overlay org-tags-overlay ov-start ov-end)
  9884. (save-window-excursion
  9885. (if expert
  9886. (set-buffer (get-buffer-create " *Org tags*"))
  9887. (delete-other-windows)
  9888. (split-window-vertically)
  9889. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9890. (erase-buffer)
  9891. (org-set-local 'org-done-keywords done-keywords)
  9892. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9893. (org-fast-tag-insert "Current" current c-face "\n\n")
  9894. (org-fast-tag-show-exit exit-after-next)
  9895. (org-set-current-tags-overlay current ov-prefix)
  9896. (setq tbl fulltable char ?a cnt 0)
  9897. (while (setq e (pop tbl))
  9898. (cond
  9899. ((equal e '(:startgroup))
  9900. (push '() groups) (setq ingroup t)
  9901. (when (not (= cnt 0))
  9902. (setq cnt 0)
  9903. (insert "\n"))
  9904. (insert "{ "))
  9905. ((equal e '(:endgroup))
  9906. (setq ingroup nil cnt 0)
  9907. (insert "}\n"))
  9908. ((equal e '(:newline))
  9909. (when (not (= cnt 0))
  9910. (setq cnt 0)
  9911. (insert "\n")
  9912. (setq e (car tbl))
  9913. (while (equal (car tbl) '(:newline))
  9914. (insert "\n")
  9915. (setq tbl (cdr tbl)))))
  9916. (t
  9917. (setq tg (car e) c2 nil)
  9918. (if (cdr e)
  9919. (setq c (cdr e))
  9920. ;; automatically assign a character.
  9921. (setq c1 (string-to-char
  9922. (downcase (substring
  9923. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9924. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9925. (while (or (rassoc char ntable) (rassoc char table))
  9926. (setq char (1+ char)))
  9927. (setq c2 c1))
  9928. (setq c (or c2 char)))
  9929. (if ingroup (push tg (car groups)))
  9930. (setq tg (org-add-props tg nil 'face
  9931. (cond
  9932. ((not (assoc tg table))
  9933. (org-get-todo-face tg))
  9934. ((member tg current) c-face)
  9935. ((member tg inherited) i-face)
  9936. (t nil))))
  9937. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9938. (insert "[" c "] " tg (make-string
  9939. (- fwidth 4 (length tg)) ?\ ))
  9940. (push (cons tg c) ntable)
  9941. (when (= (setq cnt (1+ cnt)) ncol)
  9942. (insert "\n")
  9943. (if ingroup (insert " "))
  9944. (setq cnt 0)))))
  9945. (setq ntable (nreverse ntable))
  9946. (insert "\n")
  9947. (goto-char (point-min))
  9948. (if (not expert) (org-fit-window-to-buffer))
  9949. (setq rtn
  9950. (catch 'exit
  9951. (while t
  9952. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9953. (if groups " [!] no groups" " [!]groups")
  9954. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9955. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9956. (cond
  9957. ((= c ?\r) (throw 'exit t))
  9958. ((= c ?!)
  9959. (setq groups (not groups))
  9960. (goto-char (point-min))
  9961. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9962. ((= c ?\C-c)
  9963. (if (not expert)
  9964. (org-fast-tag-show-exit
  9965. (setq exit-after-next (not exit-after-next)))
  9966. (setq expert nil)
  9967. (delete-other-windows)
  9968. (split-window-vertically)
  9969. (org-switch-to-buffer-other-window " *Org tags*")
  9970. (org-fit-window-to-buffer)))
  9971. ((or (= c ?\C-g)
  9972. (and (= c ?q) (not (rassoc c ntable))))
  9973. (org-detach-overlay org-tags-overlay)
  9974. (setq quit-flag t))
  9975. ((= c ?\ )
  9976. (setq current nil)
  9977. (if exit-after-next (setq exit-after-next 'now)))
  9978. ((= c ?\t)
  9979. (condition-case nil
  9980. (setq tg (org-ido-completing-read
  9981. "Tag: "
  9982. (or buffer-tags
  9983. (with-current-buffer buf
  9984. (org-get-buffer-tags)))))
  9985. (quit (setq tg "")))
  9986. (when (string-match "\\S-" tg)
  9987. (add-to-list 'buffer-tags (list tg))
  9988. (if (member tg current)
  9989. (setq current (delete tg current))
  9990. (push tg current)))
  9991. (if exit-after-next (setq exit-after-next 'now)))
  9992. ((setq e (rassoc c todo-table) tg (car e))
  9993. (with-current-buffer buf
  9994. (save-excursion (org-todo tg)))
  9995. (if exit-after-next (setq exit-after-next 'now)))
  9996. ((setq e (rassoc c ntable) tg (car e))
  9997. (if (member tg current)
  9998. (setq current (delete tg current))
  9999. (loop for g in groups do
  10000. (if (member tg g)
  10001. (mapc (lambda (x)
  10002. (setq current (delete x current)))
  10003. g)))
  10004. (push tg current))
  10005. (if exit-after-next (setq exit-after-next 'now))))
  10006. ;; Create a sorted list
  10007. (setq current
  10008. (sort current
  10009. (lambda (a b)
  10010. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10011. (if (eq exit-after-next 'now) (throw 'exit t))
  10012. (goto-char (point-min))
  10013. (beginning-of-line 2)
  10014. (delete-region (point) (point-at-eol))
  10015. (org-fast-tag-insert "Current" current c-face)
  10016. (org-set-current-tags-overlay current ov-prefix)
  10017. (while (re-search-forward
  10018. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10019. (setq tg (match-string 1))
  10020. (add-text-properties
  10021. (match-beginning 1) (match-end 1)
  10022. (list 'face
  10023. (cond
  10024. ((member tg current) c-face)
  10025. ((member tg inherited) i-face)
  10026. (t (get-text-property (match-beginning 1) 'face))))))
  10027. (goto-char (point-min)))))
  10028. (org-detach-overlay org-tags-overlay)
  10029. (if rtn
  10030. (mapconcat 'identity current ":")
  10031. nil))))
  10032. (defun org-get-tags-string ()
  10033. "Get the TAGS string in the current headline."
  10034. (unless (org-on-heading-p t)
  10035. (error "Not on a heading"))
  10036. (save-excursion
  10037. (beginning-of-line 1)
  10038. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10039. (org-match-string-no-properties 1)
  10040. "")))
  10041. (defun org-get-tags ()
  10042. "Get the list of tags specified in the current headline."
  10043. (org-split-string (org-get-tags-string) ":"))
  10044. (defun org-get-buffer-tags ()
  10045. "Get a table of all tags used in the buffer, for completion."
  10046. (let (tags)
  10047. (save-excursion
  10048. (goto-char (point-min))
  10049. (while (re-search-forward
  10050. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10051. (when (equal (char-after (point-at-bol 0)) ?*)
  10052. (mapc (lambda (x) (add-to-list 'tags x))
  10053. (org-split-string (org-match-string-no-properties 1) ":")))))
  10054. (mapcar 'list tags)))
  10055. ;;;; The mapping API
  10056. ;;;###autoload
  10057. (defun org-map-entries (func &optional match scope &rest skip)
  10058. "Call FUNC at each headline selected by MATCH in SCOPE.
  10059. FUNC is a function or a lisp form. The function will be called without
  10060. arguments, with the cursor positioned at the beginning of the headline.
  10061. The return values of all calls to the function will be collected and
  10062. returned as a list.
  10063. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10064. does not need to preserve point. After evaluaton, the cursor will be
  10065. moved to the end of the line (presumably of the headline of the
  10066. processed entry) and search continues from there. Under some
  10067. circumstances, this may not produce the wanted results. For example,
  10068. if you have removed (e.g. archived) the current (sub)tree it could
  10069. mean that the next entry will be skipped entirely. In such cases, you
  10070. can specify the position from where search should continue by making
  10071. FUNC set the variable `org-map-continue-from' to the desired buffer
  10072. position.
  10073. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10074. Only headlines that are matched by this query will be considered during
  10075. the iteration. When MATCH is nil or t, all headlines will be
  10076. visited by the iteration.
  10077. SCOPE determines the scope of this command. It can be any of:
  10078. nil The current buffer, respecting the restriction if any
  10079. tree The subtree started with the entry at point
  10080. file The current buffer, without restriction
  10081. file-with-archives
  10082. The current buffer, and any archives associated with it
  10083. agenda All agenda files
  10084. agenda-with-archives
  10085. All agenda files with any archive files associated with them
  10086. \(file1 file2 ...)
  10087. If this is a list, all files in the list will be scanned
  10088. The remaining args are treated as settings for the skipping facilities of
  10089. the scanner. The following items can be given here:
  10090. archive skip trees with the archive tag.
  10091. comment skip trees with the COMMENT keyword
  10092. function or Emacs Lisp form:
  10093. will be used as value for `org-agenda-skip-function', so whenever
  10094. the the function returns t, FUNC will not be called for that
  10095. entry and search will continue from the point where the
  10096. function leaves it.
  10097. If your function needs to retrieve the tags including inherited tags
  10098. at the *current* entry, you can use the value of the variable
  10099. `org-scanner-tags' which will be much faster than getting the value
  10100. with `org-get-tags-at'. If your function gets properties with
  10101. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10102. to t around the call to `org-entry-properties' to get the same speedup.
  10103. Note that if your function moves around to retrieve tags and properties at
  10104. a *different* entry, you cannot use these techniques."
  10105. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10106. (org-agenda-skip-archived-trees (memq 'archive skip))
  10107. (org-agenda-skip-comment-trees (memq 'comment skip))
  10108. (org-agenda-skip-function
  10109. (car (org-delete-all '(comment archive) skip)))
  10110. (org-tags-match-list-sublevels t)
  10111. matcher file res
  10112. org-todo-keywords-for-agenda
  10113. org-done-keywords-for-agenda
  10114. org-todo-keyword-alist-for-agenda
  10115. org-tag-alist-for-agenda)
  10116. (cond
  10117. ((eq match t) (setq matcher t))
  10118. ((eq match nil) (setq matcher t))
  10119. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10120. (save-excursion
  10121. (save-restriction
  10122. (when (eq scope 'tree)
  10123. (org-back-to-heading t)
  10124. (org-narrow-to-subtree)
  10125. (setq scope nil))
  10126. (if (not scope)
  10127. (progn
  10128. (org-prepare-agenda-buffers
  10129. (list (buffer-file-name (current-buffer))))
  10130. (setq res (org-scan-tags func matcher)))
  10131. ;; Get the right scope
  10132. (cond
  10133. ((and scope (listp scope) (symbolp (car scope)))
  10134. (setq scope (eval scope)))
  10135. ((eq scope 'agenda)
  10136. (setq scope (org-agenda-files t)))
  10137. ((eq scope 'agenda-with-archives)
  10138. (setq scope (org-agenda-files t))
  10139. (setq scope (org-add-archive-files scope)))
  10140. ((eq scope 'file)
  10141. (setq scope (list (buffer-file-name))))
  10142. ((eq scope 'file-with-archives)
  10143. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10144. (org-prepare-agenda-buffers scope)
  10145. (while (setq file (pop scope))
  10146. (with-current-buffer (org-find-base-buffer-visiting file)
  10147. (save-excursion
  10148. (save-restriction
  10149. (widen)
  10150. (goto-char (point-min))
  10151. (setq res (append res (org-scan-tags func matcher))))))))))
  10152. res))
  10153. ;;;; Properties
  10154. ;;; Setting and retrieving properties
  10155. (defconst org-special-properties
  10156. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10157. "TIMESTAMP" "TIMESTAMP_IA")
  10158. "The special properties valid in Org-mode.
  10159. These are properties that are not defined in the property drawer,
  10160. but in some other way.")
  10161. (defconst org-default-properties
  10162. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_LINK"
  10163. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10164. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10165. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  10166. "Some properties that are used by Org-mode for various purposes.
  10167. Being in this list makes sure that they are offered for completion.")
  10168. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10169. "Regular expression matching the first line of a property drawer.")
  10170. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10171. "Regular expression matching the first line of a property drawer.")
  10172. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10173. "Regular expression matching the first line of a property drawer.")
  10174. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10175. "Regular expression matching the first line of a property drawer.")
  10176. (defconst org-property-drawer-re
  10177. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10178. org-property-end-re "\\)\n?")
  10179. "Matches an entire property drawer.")
  10180. (defconst org-clock-drawer-re
  10181. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10182. org-property-end-re "\\)\n?")
  10183. "Matches an entire clock drawer.")
  10184. (defun org-property-action ()
  10185. "Do an action on properties."
  10186. (interactive)
  10187. (let (c)
  10188. (org-at-property-p)
  10189. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10190. (setq c (read-char-exclusive))
  10191. (cond
  10192. ((equal c ?s)
  10193. (call-interactively 'org-set-property))
  10194. ((equal c ?d)
  10195. (call-interactively 'org-delete-property))
  10196. ((equal c ?D)
  10197. (call-interactively 'org-delete-property-globally))
  10198. ((equal c ?c)
  10199. (call-interactively 'org-compute-property-at-point))
  10200. (t (error "No such property action %c" c)))))
  10201. (defun org-at-property-p ()
  10202. "Is the cursor in a property line?"
  10203. ;; FIXME: Does not check if we are actually in the drawer.
  10204. ;; FIXME: also returns true on any drawers.....
  10205. ;; This is used by C-c C-c for property action.
  10206. (save-excursion
  10207. (beginning-of-line 1)
  10208. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10209. (defun org-get-property-block (&optional beg end force)
  10210. "Return the (beg . end) range of the body of the property drawer.
  10211. BEG and END can be beginning and end of subtree, if not given
  10212. they will be found.
  10213. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10214. (catch 'exit
  10215. (save-excursion
  10216. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10217. (end (or end (progn (outline-next-heading) (point)))))
  10218. (goto-char beg)
  10219. (if (re-search-forward org-property-start-re end t)
  10220. (setq beg (1+ (match-end 0)))
  10221. (if force
  10222. (save-excursion
  10223. (org-insert-property-drawer)
  10224. (setq end (progn (outline-next-heading) (point))))
  10225. (throw 'exit nil))
  10226. (goto-char beg)
  10227. (if (re-search-forward org-property-start-re end t)
  10228. (setq beg (1+ (match-end 0)))))
  10229. (if (re-search-forward org-property-end-re end t)
  10230. (setq end (match-beginning 0))
  10231. (or force (throw 'exit nil))
  10232. (goto-char beg)
  10233. (setq end beg)
  10234. (org-indent-line-function)
  10235. (insert ":END:\n"))
  10236. (cons beg end)))))
  10237. (defun org-entry-properties (&optional pom which)
  10238. "Get all properties of the entry at point-or-marker POM.
  10239. This includes the TODO keyword, the tags, time strings for deadline,
  10240. scheduled, and clocking, and any additional properties defined in the
  10241. entry. The return value is an alist, keys may occur multiple times
  10242. if the property key was used several times.
  10243. POM may also be nil, in which case the current entry is used.
  10244. If WHICH is nil or `all', get all properties. If WHICH is
  10245. `special' or `standard', only get that subclass."
  10246. (setq which (or which 'all))
  10247. (org-with-point-at pom
  10248. (let ((clockstr (substring org-clock-string 0 -1))
  10249. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10250. beg end range props sum-props key value string clocksum)
  10251. (save-excursion
  10252. (when (condition-case nil
  10253. (and (org-mode-p) (org-back-to-heading t))
  10254. (error nil))
  10255. (setq beg (point))
  10256. (setq sum-props (get-text-property (point) 'org-summaries))
  10257. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10258. (outline-next-heading)
  10259. (setq end (point))
  10260. (when (memq which '(all special))
  10261. ;; Get the special properties, like TODO and tags
  10262. (goto-char beg)
  10263. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10264. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10265. (when (looking-at org-priority-regexp)
  10266. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10267. (when (and (setq value (org-get-tags-string))
  10268. (string-match "\\S-" value))
  10269. (push (cons "TAGS" value) props))
  10270. (when (setq value (org-get-tags-at))
  10271. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10272. props))
  10273. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10274. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10275. string (if (equal key clockstr)
  10276. (org-no-properties
  10277. (org-trim
  10278. (buffer-substring
  10279. (match-beginning 3) (goto-char (point-at-eol)))))
  10280. (substring (org-match-string-no-properties 3) 1 -1)))
  10281. (unless key
  10282. (if (= (char-after (match-beginning 3)) ?\[)
  10283. (setq key "TIMESTAMP_IA")
  10284. (setq key "TIMESTAMP")))
  10285. (when (or (equal key clockstr) (not (assoc key props)))
  10286. (push (cons key string) props)))
  10287. )
  10288. (when (memq which '(all standard))
  10289. ;; Get the standard properties, like :PROP: ...
  10290. (setq range (org-get-property-block beg end))
  10291. (when range
  10292. (goto-char (car range))
  10293. (while (re-search-forward
  10294. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10295. (cdr range) t)
  10296. (setq key (org-match-string-no-properties 1)
  10297. value (org-trim (or (org-match-string-no-properties 2) "")))
  10298. (unless (member key excluded)
  10299. (push (cons key (or value "")) props)))))
  10300. (if clocksum
  10301. (push (cons "CLOCKSUM"
  10302. (org-columns-number-to-string (/ (float clocksum) 60.)
  10303. 'add_times))
  10304. props))
  10305. (unless (assoc "CATEGORY" props)
  10306. (setq value (or (org-get-category)
  10307. (progn (org-refresh-category-properties)
  10308. (org-get-category))))
  10309. (push (cons "CATEGORY" value) props))
  10310. (append sum-props (nreverse props)))))))
  10311. (defun org-entry-get (pom property &optional inherit)
  10312. "Get value of PROPERTY for entry at point-or-marker POM.
  10313. If INHERIT is non-nil and the entry does not have the property,
  10314. then also check higher levels of the hierarchy.
  10315. If INHERIT is the symbol `selective', use inheritance only if the setting
  10316. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10317. If the property is present but empty, the return value is the empty string.
  10318. If the property is not present at all, nil is returned."
  10319. (org-with-point-at pom
  10320. (if (and inherit (if (eq inherit 'selective)
  10321. (org-property-inherit-p property)
  10322. t))
  10323. (org-entry-get-with-inheritance property)
  10324. (if (member property org-special-properties)
  10325. ;; We need a special property. Use brute force, get all properties.
  10326. (cdr (assoc property (org-entry-properties nil 'special)))
  10327. (let ((range (org-get-property-block)))
  10328. (if (and range
  10329. (goto-char (car range))
  10330. (re-search-forward
  10331. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10332. (cdr range) t))
  10333. ;; Found the property, return it.
  10334. (if (match-end 1)
  10335. (org-match-string-no-properties 1)
  10336. "")))))))
  10337. (defun org-property-or-variable-value (var &optional inherit)
  10338. "Check if there is a property fixing the value of VAR.
  10339. If yes, return this value. If not, return the current value of the variable."
  10340. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10341. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10342. (read prop)
  10343. (symbol-value var))))
  10344. (defun org-entry-delete (pom property)
  10345. "Delete the property PROPERTY from entry at point-or-marker POM."
  10346. (org-with-point-at pom
  10347. (if (member property org-special-properties)
  10348. nil ; cannot delete these properties.
  10349. (let ((range (org-get-property-block)))
  10350. (if (and range
  10351. (goto-char (car range))
  10352. (re-search-forward
  10353. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10354. (cdr range) t))
  10355. (progn
  10356. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10357. t)
  10358. nil)))))
  10359. ;; Multi-values properties are properties that contain multiple values
  10360. ;; These values are assumed to be single words, separated by whitespace.
  10361. (defun org-entry-add-to-multivalued-property (pom property value)
  10362. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10363. (let* ((old (org-entry-get pom property))
  10364. (values (and old (org-split-string old "[ \t]"))))
  10365. (setq value (org-entry-protect-space value))
  10366. (unless (member value values)
  10367. (setq values (cons value values))
  10368. (org-entry-put pom property
  10369. (mapconcat 'identity values " ")))))
  10370. (defun org-entry-remove-from-multivalued-property (pom property value)
  10371. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10372. (let* ((old (org-entry-get pom property))
  10373. (values (and old (org-split-string old "[ \t]"))))
  10374. (setq value (org-entry-protect-space value))
  10375. (when (member value values)
  10376. (setq values (delete value values))
  10377. (org-entry-put pom property
  10378. (mapconcat 'identity values " ")))))
  10379. (defun org-entry-member-in-multivalued-property (pom property value)
  10380. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10381. (let* ((old (org-entry-get pom property))
  10382. (values (and old (org-split-string old "[ \t]"))))
  10383. (setq value (org-entry-protect-space value))
  10384. (member value values)))
  10385. (defun org-entry-get-multivalued-property (pom property)
  10386. "Return a list of values in a multivalued property."
  10387. (let* ((value (org-entry-get pom property))
  10388. (values (and value (org-split-string value "[ \t]"))))
  10389. (mapcar 'org-entry-restore-space values)))
  10390. (defun org-entry-put-multivalued-property (pom property &rest values)
  10391. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10392. VALUES should be a list of strings. Spaces will be protected."
  10393. (org-entry-put pom property
  10394. (mapconcat 'org-entry-protect-space values " "))
  10395. (let* ((value (org-entry-get pom property))
  10396. (values (and value (org-split-string value "[ \t]"))))
  10397. (mapcar 'org-entry-restore-space values)))
  10398. (defun org-entry-protect-space (s)
  10399. "Protect spaces and newline in string S."
  10400. (while (string-match " " s)
  10401. (setq s (replace-match "%20" t t s)))
  10402. (while (string-match "\n" s)
  10403. (setq s (replace-match "%0A" t t s)))
  10404. s)
  10405. (defun org-entry-restore-space (s)
  10406. "Restore spaces and newline in string S."
  10407. (while (string-match "%20" s)
  10408. (setq s (replace-match " " t t s)))
  10409. (while (string-match "%0A" s)
  10410. (setq s (replace-match "\n" t t s)))
  10411. s)
  10412. (defvar org-entry-property-inherited-from (make-marker)
  10413. "Marker pointing to the entry from where a property was inherited.
  10414. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10415. location of the entry where the inheritance search matched. If there was
  10416. no match, the marker will point nowhere.
  10417. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10418. is set.")
  10419. (defun org-entry-get-with-inheritance (property)
  10420. "Get entry property, and search higher levels if not present."
  10421. (move-marker org-entry-property-inherited-from nil)
  10422. (let (tmp)
  10423. (save-excursion
  10424. (save-restriction
  10425. (widen)
  10426. (catch 'ex
  10427. (while t
  10428. (when (setq tmp (org-entry-get nil property))
  10429. (org-back-to-heading t)
  10430. (move-marker org-entry-property-inherited-from (point))
  10431. (throw 'ex tmp))
  10432. (or (org-up-heading-safe) (throw 'ex nil)))))
  10433. (or tmp
  10434. (cdr (assoc property org-file-properties))
  10435. (cdr (assoc property org-global-properties))
  10436. (cdr (assoc property org-global-properties-fixed))))))
  10437. (defun org-entry-put (pom property value)
  10438. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10439. (org-with-point-at pom
  10440. (org-back-to-heading t)
  10441. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10442. range)
  10443. (cond
  10444. ((equal property "TODO")
  10445. (when (and (stringp value) (string-match "\\S-" value)
  10446. (not (member value org-todo-keywords-1)))
  10447. (error "\"%s\" is not a valid TODO state" value))
  10448. (if (or (not value)
  10449. (not (string-match "\\S-" value)))
  10450. (setq value 'none))
  10451. (org-todo value)
  10452. (org-set-tags nil 'align))
  10453. ((equal property "PRIORITY")
  10454. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10455. (string-to-char value) ?\ ))
  10456. (org-set-tags nil 'align))
  10457. ((equal property "SCHEDULED")
  10458. (if (re-search-forward org-scheduled-time-regexp end t)
  10459. (cond
  10460. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10461. ((eq value 'later) (org-timestamp-change 1 'day))
  10462. (t (call-interactively 'org-schedule)))
  10463. (call-interactively 'org-schedule)))
  10464. ((equal property "DEADLINE")
  10465. (if (re-search-forward org-deadline-time-regexp end t)
  10466. (cond
  10467. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10468. ((eq value 'later) (org-timestamp-change 1 'day))
  10469. (t (call-interactively 'org-deadline)))
  10470. (call-interactively 'org-deadline)))
  10471. ((member property org-special-properties)
  10472. (error "The %s property can not yet be set with `org-entry-put'"
  10473. property))
  10474. (t ; a non-special property
  10475. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10476. (setq range (org-get-property-block beg end 'force))
  10477. (goto-char (car range))
  10478. (if (re-search-forward
  10479. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10480. (progn
  10481. (delete-region (match-beginning 1) (match-end 1))
  10482. (goto-char (match-beginning 1)))
  10483. (goto-char (cdr range))
  10484. (insert "\n")
  10485. (backward-char 1)
  10486. (org-indent-line-function)
  10487. (insert ":" property ":"))
  10488. (and value (insert " " value))
  10489. (org-indent-line-function)))))))
  10490. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10491. "Get all property keys in the current buffer.
  10492. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10493. like tags and TODO state.
  10494. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10495. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10496. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10497. formats in the current buffer."
  10498. (let (rtn range cfmt s p)
  10499. (save-excursion
  10500. (save-restriction
  10501. (widen)
  10502. (goto-char (point-min))
  10503. (while (re-search-forward org-property-start-re nil t)
  10504. (setq range (org-get-property-block))
  10505. (goto-char (car range))
  10506. (while (re-search-forward
  10507. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10508. (cdr range) t)
  10509. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10510. (outline-next-heading))))
  10511. (when include-specials
  10512. (setq rtn (append org-special-properties rtn)))
  10513. (when include-defaults
  10514. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10515. (when include-columns
  10516. (save-excursion
  10517. (save-restriction
  10518. (widen)
  10519. (goto-char (point-min))
  10520. (while (re-search-forward
  10521. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10522. nil t)
  10523. (setq cfmt (match-string 2) s 0)
  10524. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10525. cfmt s)
  10526. (setq s (match-end 0)
  10527. p (match-string 1 cfmt))
  10528. (unless (or (equal p "ITEM")
  10529. (member p org-special-properties))
  10530. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10531. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10532. (defun org-property-values (key)
  10533. "Return a list of all values of property KEY."
  10534. (save-excursion
  10535. (save-restriction
  10536. (widen)
  10537. (goto-char (point-min))
  10538. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10539. values)
  10540. (while (re-search-forward re nil t)
  10541. (add-to-list 'values (org-trim (match-string 1))))
  10542. (delete "" values)))))
  10543. (defun org-insert-property-drawer ()
  10544. "Insert a property drawer into the current entry."
  10545. (interactive)
  10546. (org-back-to-heading t)
  10547. (looking-at outline-regexp)
  10548. (let ((indent (- (match-end 0)(match-beginning 0)))
  10549. (beg (point))
  10550. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10551. end hiddenp)
  10552. (outline-next-heading)
  10553. (setq end (point))
  10554. (goto-char beg)
  10555. (while (re-search-forward re end t))
  10556. (setq hiddenp (org-invisible-p))
  10557. (end-of-line 1)
  10558. (and (equal (char-after) ?\n) (forward-char 1))
  10559. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10560. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10561. ;; just skip this line
  10562. (beginning-of-line 2)
  10563. ;; Drawer start, find the end
  10564. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10565. (beginning-of-line 1)))
  10566. (org-skip-over-state-notes)
  10567. (skip-chars-backward " \t\n\r")
  10568. (if (eq (char-before) ?*) (forward-char 1))
  10569. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10570. (beginning-of-line 0)
  10571. (org-indent-to-column indent)
  10572. (beginning-of-line 2)
  10573. (org-indent-to-column indent)
  10574. (beginning-of-line 0)
  10575. (if hiddenp
  10576. (save-excursion
  10577. (org-back-to-heading t)
  10578. (hide-entry))
  10579. (org-flag-drawer t))))
  10580. (defun org-set-property (property value)
  10581. "In the current entry, set PROPERTY to VALUE.
  10582. When called interactively, this will prompt for a property name, offering
  10583. completion on existing and default properties. And then it will prompt
  10584. for a value, offering completion either on allowed values (via an inherited
  10585. xxx_ALL property) or on existing values in other instances of this property
  10586. in the current file."
  10587. (interactive
  10588. (let* ((completion-ignore-case t)
  10589. (keys (org-buffer-property-keys nil t t))
  10590. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10591. (prop (if (member prop0 keys)
  10592. prop0
  10593. (or (cdr (assoc (downcase prop0)
  10594. (mapcar (lambda (x) (cons (downcase x) x))
  10595. keys)))
  10596. prop0)))
  10597. (cur (org-entry-get nil prop))
  10598. (allowed (org-property-get-allowed-values nil prop 'table))
  10599. (existing (mapcar 'list (org-property-values prop)))
  10600. (val (if allowed
  10601. (org-completing-read "Value: " allowed nil 'req-match)
  10602. (let (org-completion-use-ido)
  10603. (org-completing-read
  10604. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10605. (concat "[" cur "]") "")
  10606. ": ")
  10607. existing nil nil "" nil cur)))))
  10608. (list prop (if (equal val "") cur val))))
  10609. (unless (equal (org-entry-get nil property) value)
  10610. (org-entry-put nil property value)))
  10611. (defun org-delete-property (property)
  10612. "In the current entry, delete PROPERTY."
  10613. (interactive
  10614. (let* ((completion-ignore-case t)
  10615. (prop (org-ido-completing-read
  10616. "Property: " (org-entry-properties nil 'standard))))
  10617. (list prop)))
  10618. (message "Property %s %s" property
  10619. (if (org-entry-delete nil property)
  10620. "deleted"
  10621. "was not present in the entry")))
  10622. (defun org-delete-property-globally (property)
  10623. "Remove PROPERTY globally, from all entries."
  10624. (interactive
  10625. (let* ((completion-ignore-case t)
  10626. (prop (org-ido-completing-read
  10627. "Globally remove property: "
  10628. (mapcar 'list (org-buffer-property-keys)))))
  10629. (list prop)))
  10630. (save-excursion
  10631. (save-restriction
  10632. (widen)
  10633. (goto-char (point-min))
  10634. (let ((cnt 0))
  10635. (while (re-search-forward
  10636. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10637. nil t)
  10638. (setq cnt (1+ cnt))
  10639. (replace-match ""))
  10640. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10641. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10642. (defun org-compute-property-at-point ()
  10643. "Compute the property at point.
  10644. This looks for an enclosing column format, extracts the operator and
  10645. then applies it to the property in the column format's scope."
  10646. (interactive)
  10647. (unless (org-at-property-p)
  10648. (error "Not at a property"))
  10649. (let ((prop (org-match-string-no-properties 2)))
  10650. (org-columns-get-format-and-top-level)
  10651. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10652. (error "No operator defined for property %s" prop))
  10653. (org-columns-compute prop)))
  10654. (defun org-property-get-allowed-values (pom property &optional table)
  10655. "Get allowed values for the property PROPERTY.
  10656. When TABLE is non-nil, return an alist that can directly be used for
  10657. completion."
  10658. (let (vals)
  10659. (cond
  10660. ((equal property "TODO")
  10661. (setq vals (org-with-point-at pom
  10662. (append org-todo-keywords-1 '("")))))
  10663. ((equal property "PRIORITY")
  10664. (let ((n org-lowest-priority))
  10665. (while (>= n org-highest-priority)
  10666. (push (char-to-string n) vals)
  10667. (setq n (1- n)))))
  10668. ((member property org-special-properties))
  10669. (t
  10670. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10671. (when (and vals (string-match "\\S-" vals))
  10672. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10673. (setq vals (mapcar (lambda (x)
  10674. (cond ((stringp x) x)
  10675. ((numberp x) (number-to-string x))
  10676. ((symbolp x) (symbol-name x))
  10677. (t "???")))
  10678. vals)))))
  10679. (if table (mapcar 'list vals) vals)))
  10680. (defun org-property-previous-allowed-value (&optional previous)
  10681. "Switch to the next allowed value for this property."
  10682. (interactive)
  10683. (org-property-next-allowed-value t))
  10684. (defun org-property-next-allowed-value (&optional previous)
  10685. "Switch to the next allowed value for this property."
  10686. (interactive)
  10687. (unless (org-at-property-p)
  10688. (error "Not at a property"))
  10689. (let* ((key (match-string 2))
  10690. (value (match-string 3))
  10691. (allowed (or (org-property-get-allowed-values (point) key)
  10692. (and (member value '("[ ]" "[-]" "[X]"))
  10693. '("[ ]" "[X]"))))
  10694. nval)
  10695. (unless allowed
  10696. (error "Allowed values for this property have not been defined"))
  10697. (if previous (setq allowed (reverse allowed)))
  10698. (if (member value allowed)
  10699. (setq nval (car (cdr (member value allowed)))))
  10700. (setq nval (or nval (car allowed)))
  10701. (if (equal nval value)
  10702. (error "Only one allowed value for this property"))
  10703. (org-at-property-p)
  10704. (replace-match (concat " :" key ": " nval) t t)
  10705. (org-indent-line-function)
  10706. (beginning-of-line 1)
  10707. (skip-chars-forward " \t")))
  10708. (defun org-find-entry-with-id (ident)
  10709. "Locate the entry that contains the ID property with exact value IDENT.
  10710. IDENT can be a string, a symbol or a number, this function will search for
  10711. the string representation of it.
  10712. Return the position where this entry starts, or nil if there is no such entry."
  10713. (interactive "sID: ")
  10714. (let ((id (cond
  10715. ((stringp ident) ident)
  10716. ((symbol-name ident) (symbol-name ident))
  10717. ((numberp ident) (number-to-string ident))
  10718. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10719. (case-fold-search nil))
  10720. (save-excursion
  10721. (save-restriction
  10722. (widen)
  10723. (goto-char (point-min))
  10724. (when (re-search-forward
  10725. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10726. nil t)
  10727. (org-back-to-heading)
  10728. (point))))))
  10729. ;;;; Timestamps
  10730. (defvar org-last-changed-timestamp nil)
  10731. (defvar org-last-inserted-timestamp nil
  10732. "The last time stamp inserted with `org-insert-time-stamp'.")
  10733. (defvar org-time-was-given) ; dynamically scoped parameter
  10734. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10735. (defvar org-ts-what) ; dynamically scoped parameter
  10736. (defun org-time-stamp (arg &optional inactive)
  10737. "Prompt for a date/time and insert a time stamp.
  10738. If the user specifies a time like HH:MM, or if this command is called
  10739. with a prefix argument, the time stamp will contain date and time.
  10740. Otherwise, only the date will be included. All parts of a date not
  10741. specified by the user will be filled in from the current date/time.
  10742. So if you press just return without typing anything, the time stamp
  10743. will represent the current date/time. If there is already a timestamp
  10744. at the cursor, it will be modified."
  10745. (interactive "P")
  10746. (let* ((ts nil)
  10747. (default-time
  10748. ;; Default time is either today, or, when entering a range,
  10749. ;; the range start.
  10750. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10751. (save-excursion
  10752. (re-search-backward
  10753. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10754. (- (point) 20) t)))
  10755. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10756. (current-time)))
  10757. (default-input (and ts (org-get-compact-tod ts)))
  10758. org-time-was-given org-end-time-was-given time)
  10759. (cond
  10760. ((and (org-at-timestamp-p t)
  10761. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10762. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10763. (insert "--")
  10764. (setq time (let ((this-command this-command))
  10765. (org-read-date arg 'totime nil nil
  10766. default-time default-input)))
  10767. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10768. ((org-at-timestamp-p t)
  10769. (setq time (let ((this-command this-command))
  10770. (org-read-date arg 'totime nil nil default-time default-input)))
  10771. (when (org-at-timestamp-p t) ; just to get the match data
  10772. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10773. (replace-match "")
  10774. (setq org-last-changed-timestamp
  10775. (org-insert-time-stamp
  10776. time (or org-time-was-given arg)
  10777. inactive nil nil (list org-end-time-was-given))))
  10778. (message "Timestamp updated"))
  10779. (t
  10780. (setq time (let ((this-command this-command))
  10781. (org-read-date arg 'totime nil nil default-time default-input)))
  10782. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10783. nil nil (list org-end-time-was-given))))))
  10784. ;; FIXME: can we use this for something else, like computing time differences?
  10785. (defun org-get-compact-tod (s)
  10786. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10787. (let* ((t1 (match-string 1 s))
  10788. (h1 (string-to-number (match-string 2 s)))
  10789. (m1 (string-to-number (match-string 3 s)))
  10790. (t2 (and (match-end 4) (match-string 5 s)))
  10791. (h2 (and t2 (string-to-number (match-string 6 s))))
  10792. (m2 (and t2 (string-to-number (match-string 7 s))))
  10793. dh dm)
  10794. (if (not t2)
  10795. t1
  10796. (setq dh (- h2 h1) dm (- m2 m1))
  10797. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10798. (concat t1 "+" (number-to-string dh)
  10799. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10800. (defun org-time-stamp-inactive (&optional arg)
  10801. "Insert an inactive time stamp.
  10802. An inactive time stamp is enclosed in square brackets instead of angle
  10803. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10804. does not link to the calendar and cannot be changed with the S-cursor keys.
  10805. So these are more for recording a certain time/date."
  10806. (interactive "P")
  10807. (org-time-stamp arg 'inactive))
  10808. (defvar org-date-ovl (org-make-overlay 1 1))
  10809. (org-overlay-put org-date-ovl 'face 'org-warning)
  10810. (org-detach-overlay org-date-ovl)
  10811. (defvar org-ans1) ; dynamically scoped parameter
  10812. (defvar org-ans2) ; dynamically scoped parameter
  10813. (defvar org-plain-time-of-day-regexp) ; defined below
  10814. (defvar org-overriding-default-time nil) ; dynamically scoped
  10815. (defvar org-read-date-overlay nil)
  10816. (defvar org-dcst nil) ; dynamically scoped
  10817. (defvar org-read-date-history nil)
  10818. (defun org-read-date (&optional with-time to-time from-string prompt
  10819. default-time default-input)
  10820. "Read a date, possibly a time, and make things smooth for the user.
  10821. The prompt will suggest to enter an ISO date, but you can also enter anything
  10822. which will at least partially be understood by `parse-time-string'.
  10823. Unrecognized parts of the date will default to the current day, month, year,
  10824. hour and minute. If this command is called to replace a timestamp at point,
  10825. of to enter the second timestamp of a range, the default time is taken from the
  10826. existing stamp. For example,
  10827. 3-2-5 --> 2003-02-05
  10828. feb 15 --> currentyear-02-15
  10829. sep 12 9 --> 2009-09-12
  10830. 12:45 --> today 12:45
  10831. 22 sept 0:34 --> currentyear-09-22 0:34
  10832. 12 --> currentyear-currentmonth-12
  10833. Fri --> nearest Friday (today or later)
  10834. etc.
  10835. Furthermore you can specify a relative date by giving, as the *first* thing
  10836. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10837. change in days weeks, months, years.
  10838. With a single plus or minus, the date is relative to today. With a double
  10839. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10840. +4d --> four days from today
  10841. +4 --> same as above
  10842. +2w --> two weeks from today
  10843. ++5 --> five days from default date
  10844. The function understands only English month and weekday abbreviations,
  10845. but this can be configured with the variables `parse-time-months' and
  10846. `parse-time-weekdays'.
  10847. While prompting, a calendar is popped up - you can also select the
  10848. date with the mouse (button 1). The calendar shows a period of three
  10849. months. To scroll it to other months, use the keys `>' and `<'.
  10850. If you don't like the calendar, turn it off with
  10851. \(setq org-read-date-popup-calendar nil)
  10852. With optional argument TO-TIME, the date will immediately be converted
  10853. to an internal time.
  10854. With an optional argument WITH-TIME, the prompt will suggest to also
  10855. insert a time. Note that when WITH-TIME is not set, you can still
  10856. enter a time, and this function will inform the calling routine about
  10857. this change. The calling routine may then choose to change the format
  10858. used to insert the time stamp into the buffer to include the time.
  10859. With optional argument FROM-STRING, read from this string instead from
  10860. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10861. the time/date that is used for everything that is not specified by the
  10862. user."
  10863. (require 'parse-time)
  10864. (let* ((org-time-stamp-rounding-minutes
  10865. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10866. (org-dcst org-display-custom-times)
  10867. (ct (org-current-time))
  10868. (def (or org-overriding-default-time default-time ct))
  10869. (defdecode (decode-time def))
  10870. (dummy (progn
  10871. (when (< (nth 2 defdecode) org-extend-today-until)
  10872. (setcar (nthcdr 2 defdecode) -1)
  10873. (setcar (nthcdr 1 defdecode) 59)
  10874. (setq def (apply 'encode-time defdecode)
  10875. defdecode (decode-time def)))))
  10876. (calendar-move-hook nil)
  10877. (calendar-view-diary-initially-flag nil)
  10878. (view-diary-entries-initially nil)
  10879. (calendar-view-holidays-initially-flag nil)
  10880. (view-calendar-holidays-initially nil)
  10881. (timestr (format-time-string
  10882. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10883. (prompt (concat (if prompt (concat prompt " ") "")
  10884. (format "Date+time [%s]: " timestr)))
  10885. ans (org-ans0 "") org-ans1 org-ans2 final)
  10886. (cond
  10887. (from-string (setq ans from-string))
  10888. (org-read-date-popup-calendar
  10889. (save-excursion
  10890. (save-window-excursion
  10891. (calendar)
  10892. (calendar-forward-day (- (time-to-days def)
  10893. (calendar-absolute-from-gregorian
  10894. (calendar-current-date))))
  10895. (org-eval-in-calendar nil t)
  10896. (let* ((old-map (current-local-map))
  10897. (map (copy-keymap calendar-mode-map))
  10898. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10899. (org-defkey map (kbd "RET") 'org-calendar-select)
  10900. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10901. 'org-calendar-select-mouse)
  10902. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10903. 'org-calendar-select-mouse)
  10904. (org-defkey minibuffer-local-map [(meta shift left)]
  10905. (lambda () (interactive)
  10906. (org-eval-in-calendar '(calendar-backward-month 1))))
  10907. (org-defkey minibuffer-local-map [(meta shift right)]
  10908. (lambda () (interactive)
  10909. (org-eval-in-calendar '(calendar-forward-month 1))))
  10910. (org-defkey minibuffer-local-map [(meta shift up)]
  10911. (lambda () (interactive)
  10912. (org-eval-in-calendar '(calendar-backward-year 1))))
  10913. (org-defkey minibuffer-local-map [(meta shift down)]
  10914. (lambda () (interactive)
  10915. (org-eval-in-calendar '(calendar-forward-year 1))))
  10916. (org-defkey minibuffer-local-map [(shift up)]
  10917. (lambda () (interactive)
  10918. (org-eval-in-calendar '(calendar-backward-week 1))))
  10919. (org-defkey minibuffer-local-map [(shift down)]
  10920. (lambda () (interactive)
  10921. (org-eval-in-calendar '(calendar-forward-week 1))))
  10922. (org-defkey minibuffer-local-map [(shift left)]
  10923. (lambda () (interactive)
  10924. (org-eval-in-calendar '(calendar-backward-day 1))))
  10925. (org-defkey minibuffer-local-map [(shift right)]
  10926. (lambda () (interactive)
  10927. (org-eval-in-calendar '(calendar-forward-day 1))))
  10928. (org-defkey minibuffer-local-map ">"
  10929. (lambda () (interactive)
  10930. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10931. (org-defkey minibuffer-local-map "<"
  10932. (lambda () (interactive)
  10933. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10934. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10935. (unwind-protect
  10936. (progn
  10937. (use-local-map map)
  10938. (add-hook 'post-command-hook 'org-read-date-display)
  10939. (setq org-ans0 (read-string prompt default-input
  10940. 'org-read-date-history nil))
  10941. ;; org-ans0: from prompt
  10942. ;; org-ans1: from mouse click
  10943. ;; org-ans2: from calendar motion
  10944. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10945. (remove-hook 'post-command-hook 'org-read-date-display)
  10946. (use-local-map old-map)
  10947. (when org-read-date-overlay
  10948. (org-delete-overlay org-read-date-overlay)
  10949. (setq org-read-date-overlay nil)))))))
  10950. (t ; Naked prompt only
  10951. (unwind-protect
  10952. (setq ans (read-string prompt default-input
  10953. 'org-read-date-history timestr))
  10954. (when org-read-date-overlay
  10955. (org-delete-overlay org-read-date-overlay)
  10956. (setq org-read-date-overlay nil)))))
  10957. (setq final (org-read-date-analyze ans def defdecode))
  10958. (if to-time
  10959. (apply 'encode-time final)
  10960. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10961. (format "%04d-%02d-%02d %02d:%02d"
  10962. (nth 5 final) (nth 4 final) (nth 3 final)
  10963. (nth 2 final) (nth 1 final))
  10964. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10965. (defvar def)
  10966. (defvar defdecode)
  10967. (defvar with-time)
  10968. (defun org-read-date-display ()
  10969. "Display the current date prompt interpretation in the minibuffer."
  10970. (when org-read-date-display-live
  10971. (when org-read-date-overlay
  10972. (org-delete-overlay org-read-date-overlay))
  10973. (let ((p (point)))
  10974. (end-of-line 1)
  10975. (while (not (equal (buffer-substring
  10976. (max (point-min) (- (point) 4)) (point))
  10977. " "))
  10978. (insert " "))
  10979. (goto-char p))
  10980. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10981. " " (or org-ans1 org-ans2)))
  10982. (org-end-time-was-given nil)
  10983. (f (org-read-date-analyze ans def defdecode))
  10984. (fmts (if org-dcst
  10985. org-time-stamp-custom-formats
  10986. org-time-stamp-formats))
  10987. (fmt (if (or with-time
  10988. (and (boundp 'org-time-was-given) org-time-was-given))
  10989. (cdr fmts)
  10990. (car fmts)))
  10991. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10992. (when (and org-end-time-was-given
  10993. (string-match org-plain-time-of-day-regexp txt))
  10994. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10995. org-end-time-was-given
  10996. (substring txt (match-end 0)))))
  10997. (setq org-read-date-overlay
  10998. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10999. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11000. (defun org-read-date-analyze (ans def defdecode)
  11001. "Analyse the combined answer of the date prompt."
  11002. ;; FIXME: cleanup and comment
  11003. (let (delta deltan deltaw deltadef year month day
  11004. hour minute second wday pm h2 m2 tl wday1
  11005. iso-year iso-weekday iso-week iso-year iso-date)
  11006. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11007. (setq ans "+0"))
  11008. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11009. (setq ans (replace-match "" t t ans)
  11010. deltan (car delta)
  11011. deltaw (nth 1 delta)
  11012. deltadef (nth 2 delta)))
  11013. ;; Check if there is an iso week date in there
  11014. ;; If yes, sore the info and postpone interpreting it until the rest
  11015. ;; of the parsing is done
  11016. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11017. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11018. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11019. iso-week (string-to-number (match-string 2 ans)))
  11020. (setq ans (replace-match "" t t ans)))
  11021. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11022. (when (string-match
  11023. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11024. (setq year (if (match-end 2)
  11025. (string-to-number (match-string 2 ans))
  11026. (string-to-number (format-time-string "%Y")))
  11027. month (string-to-number (match-string 3 ans))
  11028. day (string-to-number (match-string 4 ans)))
  11029. (if (< year 100) (setq year (+ 2000 year)))
  11030. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11031. t nil ans)))
  11032. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11033. ;; If there is a time with am/pm, and *no* time without it, we convert
  11034. ;; so that matching will be successful.
  11035. (loop for i from 1 to 2 do ; twice, for end time as well
  11036. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11037. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11038. (setq hour (string-to-number (match-string 1 ans))
  11039. minute (if (match-end 3)
  11040. (string-to-number (match-string 3 ans))
  11041. 0)
  11042. pm (equal ?p
  11043. (string-to-char (downcase (match-string 4 ans)))))
  11044. (if (and (= hour 12) (not pm))
  11045. (setq hour 0)
  11046. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11047. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11048. t t ans))))
  11049. ;; Check if a time range is given as a duration
  11050. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11051. (setq hour (string-to-number (match-string 1 ans))
  11052. h2 (+ hour (string-to-number (match-string 3 ans)))
  11053. minute (string-to-number (match-string 2 ans))
  11054. m2 (+ minute (if (match-end 5) (string-to-number
  11055. (match-string 5 ans))0)))
  11056. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11057. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11058. t t ans)))
  11059. ;; Check if there is a time range
  11060. (when (boundp 'org-end-time-was-given)
  11061. (setq org-time-was-given nil)
  11062. (when (and (string-match org-plain-time-of-day-regexp ans)
  11063. (match-end 8))
  11064. (setq org-end-time-was-given (match-string 8 ans))
  11065. (setq ans (concat (substring ans 0 (match-beginning 7))
  11066. (substring ans (match-end 7))))))
  11067. (setq tl (parse-time-string ans)
  11068. day (or (nth 3 tl) (nth 3 defdecode))
  11069. month (or (nth 4 tl)
  11070. (if (and org-read-date-prefer-future
  11071. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11072. (1+ (nth 4 defdecode))
  11073. (nth 4 defdecode)))
  11074. year (or (nth 5 tl)
  11075. (if (and org-read-date-prefer-future
  11076. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11077. (1+ (nth 5 defdecode))
  11078. (nth 5 defdecode)))
  11079. hour (or (nth 2 tl) (nth 2 defdecode))
  11080. minute (or (nth 1 tl) (nth 1 defdecode))
  11081. second (or (nth 0 tl) 0)
  11082. wday (nth 6 tl))
  11083. ;; Special date definitions below
  11084. (cond
  11085. (iso-week
  11086. ;; There was an iso week
  11087. (setq year (or iso-year year)
  11088. day (or iso-weekday wday 1)
  11089. wday nil ; to make sure that the trigger below does not match
  11090. iso-date (calendar-gregorian-from-absolute
  11091. (calendar-absolute-from-iso
  11092. (list iso-week day year))))
  11093. ; FIXME: Should we also push ISO weeks into the future?
  11094. ; (when (and org-read-date-prefer-future
  11095. ; (not iso-year)
  11096. ; (< (calendar-absolute-from-gregorian iso-date)
  11097. ; (time-to-days (current-time))))
  11098. ; (setq year (1+ year)
  11099. ; iso-date (calendar-gregorian-from-absolute
  11100. ; (calendar-absolute-from-iso
  11101. ; (list iso-week day year)))))
  11102. (setq month (car iso-date)
  11103. year (nth 2 iso-date)
  11104. day (nth 1 iso-date)))
  11105. (deltan
  11106. (unless deltadef
  11107. (let ((now (decode-time (current-time))))
  11108. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11109. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11110. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11111. ((equal deltaw "m") (setq month (+ month deltan)))
  11112. ((equal deltaw "y") (setq year (+ year deltan)))))
  11113. ((and wday (not (nth 3 tl)))
  11114. ;; Weekday was given, but no day, so pick that day in the week
  11115. ;; on or after the derived date.
  11116. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11117. (unless (equal wday wday1)
  11118. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11119. (if (and (boundp 'org-time-was-given)
  11120. (nth 2 tl))
  11121. (setq org-time-was-given t))
  11122. (if (< year 100) (setq year (+ 2000 year)))
  11123. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11124. (list second minute hour day month year)))
  11125. (defvar parse-time-weekdays)
  11126. (defun org-read-date-get-relative (s today default)
  11127. "Check string S for special relative date string.
  11128. TODAY and DEFAULT are internal times, for today and for a default.
  11129. Return shift list (N what def-flag)
  11130. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11131. N is the number of WHATs to shift.
  11132. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11133. the DEFAULT date rather than TODAY."
  11134. (when (and
  11135. (string-match
  11136. (concat
  11137. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11138. "\\([0-9]+\\)?"
  11139. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11140. "\\([ \t]\\|$\\)") s)
  11141. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11142. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11143. (string-to-char (substring (match-string 1 s) -1))
  11144. ?+))
  11145. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11146. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11147. (what (if (match-end 3) (match-string 3 s) "d"))
  11148. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11149. (date (if rel default today))
  11150. (wday (nth 6 (decode-time date)))
  11151. delta)
  11152. (if wday1
  11153. (progn
  11154. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11155. (if (= dir ?-) (setq delta (- delta 7)))
  11156. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11157. (list delta "d" rel))
  11158. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11159. (defun org-eval-in-calendar (form &optional keepdate)
  11160. "Eval FORM in the calendar window and return to current window.
  11161. Also, store the cursor date in variable org-ans2."
  11162. (let ((sw (selected-window)))
  11163. (select-window (get-buffer-window "*Calendar*"))
  11164. (eval form)
  11165. (when (and (not keepdate) (calendar-cursor-to-date))
  11166. (let* ((date (calendar-cursor-to-date))
  11167. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11168. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11169. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11170. (select-window sw)))
  11171. (defun org-calendar-select ()
  11172. "Return to `org-read-date' with the date currently selected.
  11173. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11174. (interactive)
  11175. (when (calendar-cursor-to-date)
  11176. (let* ((date (calendar-cursor-to-date))
  11177. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11178. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11179. (if (active-minibuffer-window) (exit-minibuffer))))
  11180. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11181. "Insert a date stamp for the date given by the internal TIME.
  11182. WITH-HM means, use the stamp format that includes the time of the day.
  11183. INACTIVE means use square brackets instead of angular ones, so that the
  11184. stamp will not contribute to the agenda.
  11185. PRE and POST are optional strings to be inserted before and after the
  11186. stamp.
  11187. The command returns the inserted time stamp."
  11188. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11189. stamp)
  11190. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11191. (insert-before-markers (or pre ""))
  11192. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11193. (when (listp extra)
  11194. (setq extra (car extra))
  11195. (if (and (stringp extra)
  11196. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11197. (setq extra (format "-%02d:%02d"
  11198. (string-to-number (match-string 1 extra))
  11199. (string-to-number (match-string 2 extra))))
  11200. (setq extra nil)))
  11201. (when extra
  11202. (backward-char 1)
  11203. (insert-before-markers extra)
  11204. (forward-char 1))
  11205. (insert-before-markers (or post ""))
  11206. (setq org-last-inserted-timestamp stamp)))
  11207. (defun org-toggle-time-stamp-overlays ()
  11208. "Toggle the use of custom time stamp formats."
  11209. (interactive)
  11210. (setq org-display-custom-times (not org-display-custom-times))
  11211. (unless org-display-custom-times
  11212. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11213. (while (setq p (next-single-property-change p 'display))
  11214. (if (and (get-text-property p 'display)
  11215. (eq (get-text-property p 'face) 'org-date))
  11216. (remove-text-properties
  11217. p (setq p (next-single-property-change p 'display))
  11218. '(display t))))
  11219. (set-buffer-modified-p bmp)))
  11220. (if (featurep 'xemacs)
  11221. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11222. (org-restart-font-lock)
  11223. (setq org-table-may-need-update t)
  11224. (if org-display-custom-times
  11225. (message "Time stamps are overlayed with custom format")
  11226. (message "Time stamp overlays removed")))
  11227. (defun org-display-custom-time (beg end)
  11228. "Overlay modified time stamp format over timestamp between BEG and END."
  11229. (let* ((ts (buffer-substring beg end))
  11230. t1 w1 with-hm tf time str w2 (off 0))
  11231. (save-match-data
  11232. (setq t1 (org-parse-time-string ts t))
  11233. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11234. (setq off (- (match-end 0) (match-beginning 0)))))
  11235. (setq end (- end off))
  11236. (setq w1 (- end beg)
  11237. with-hm (and (nth 1 t1) (nth 2 t1))
  11238. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11239. time (org-fix-decoded-time t1)
  11240. str (org-add-props
  11241. (format-time-string
  11242. (substring tf 1 -1) (apply 'encode-time time))
  11243. nil 'mouse-face 'highlight)
  11244. w2 (length str))
  11245. (if (not (= w2 w1))
  11246. (add-text-properties (1+ beg) (+ 2 beg)
  11247. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11248. (if (featurep 'xemacs)
  11249. (progn
  11250. (put-text-property beg end 'invisible t)
  11251. (put-text-property beg end 'end-glyph (make-glyph str)))
  11252. (put-text-property beg end 'display str))))
  11253. (defun org-translate-time (string)
  11254. "Translate all timestamps in STRING to custom format.
  11255. But do this only if the variable `org-display-custom-times' is set."
  11256. (when org-display-custom-times
  11257. (save-match-data
  11258. (let* ((start 0)
  11259. (re org-ts-regexp-both)
  11260. t1 with-hm inactive tf time str beg end)
  11261. (while (setq start (string-match re string start))
  11262. (setq beg (match-beginning 0)
  11263. end (match-end 0)
  11264. t1 (save-match-data
  11265. (org-parse-time-string (substring string beg end) t))
  11266. with-hm (and (nth 1 t1) (nth 2 t1))
  11267. inactive (equal (substring string beg (1+ beg)) "[")
  11268. tf (funcall (if with-hm 'cdr 'car)
  11269. org-time-stamp-custom-formats)
  11270. time (org-fix-decoded-time t1)
  11271. str (format-time-string
  11272. (concat
  11273. (if inactive "[" "<") (substring tf 1 -1)
  11274. (if inactive "]" ">"))
  11275. (apply 'encode-time time))
  11276. string (replace-match str t t string)
  11277. start (+ start (length str)))))))
  11278. string)
  11279. (defun org-fix-decoded-time (time)
  11280. "Set 0 instead of nil for the first 6 elements of time.
  11281. Don't touch the rest."
  11282. (let ((n 0))
  11283. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11284. (defun org-days-to-time (timestamp-string)
  11285. "Difference between TIMESTAMP-STRING and now in days."
  11286. (- (time-to-days (org-time-string-to-time timestamp-string))
  11287. (time-to-days (current-time))))
  11288. (defun org-deadline-close (timestamp-string &optional ndays)
  11289. "Is the time in TIMESTAMP-STRING close to the current date?"
  11290. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11291. (and (< (org-days-to-time timestamp-string) ndays)
  11292. (not (org-entry-is-done-p))))
  11293. (defun org-get-wdays (ts)
  11294. "Get the deadline lead time appropriate for timestring TS."
  11295. (cond
  11296. ((<= org-deadline-warning-days 0)
  11297. ;; 0 or negative, enforce this value no matter what
  11298. (- org-deadline-warning-days))
  11299. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11300. ;; lead time is specified.
  11301. (floor (* (string-to-number (match-string 1 ts))
  11302. (cdr (assoc (match-string 2 ts)
  11303. '(("d" . 1) ("w" . 7)
  11304. ("m" . 30.4) ("y" . 365.25)))))))
  11305. ;; go for the default.
  11306. (t org-deadline-warning-days)))
  11307. (defun org-calendar-select-mouse (ev)
  11308. "Return to `org-read-date' with the date currently selected.
  11309. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11310. (interactive "e")
  11311. (mouse-set-point ev)
  11312. (when (calendar-cursor-to-date)
  11313. (let* ((date (calendar-cursor-to-date))
  11314. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11315. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11316. (if (active-minibuffer-window) (exit-minibuffer))))
  11317. (defun org-check-deadlines (ndays)
  11318. "Check if there are any deadlines due or past due.
  11319. A deadline is considered due if it happens within `org-deadline-warning-days'
  11320. days from today's date. If the deadline appears in an entry marked DONE,
  11321. it is not shown. The prefix arg NDAYS can be used to test that many
  11322. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11323. (interactive "P")
  11324. (let* ((org-warn-days
  11325. (cond
  11326. ((equal ndays '(4)) 100000)
  11327. (ndays (prefix-numeric-value ndays))
  11328. (t (abs org-deadline-warning-days))))
  11329. (case-fold-search nil)
  11330. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11331. (callback
  11332. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11333. (message "%d deadlines past-due or due within %d days"
  11334. (org-occur regexp nil callback)
  11335. org-warn-days)))
  11336. (defun org-check-before-date (date)
  11337. "Check if there are deadlines or scheduled entries before DATE."
  11338. (interactive (list (org-read-date)))
  11339. (let ((case-fold-search nil)
  11340. (regexp (concat "\\<\\(" org-deadline-string
  11341. "\\|" org-scheduled-string
  11342. "\\) *<\\([^>]+\\)>"))
  11343. (callback
  11344. (lambda () (time-less-p
  11345. (org-time-string-to-time (match-string 2))
  11346. (org-time-string-to-time date)))))
  11347. (message "%d entries before %s"
  11348. (org-occur regexp nil callback) date)))
  11349. (defun org-check-after-date (date)
  11350. "Check if there are deadlines or scheduled entries after DATE."
  11351. (interactive (list (org-read-date)))
  11352. (let ((case-fold-search nil)
  11353. (regexp (concat "\\<\\(" org-deadline-string
  11354. "\\|" org-scheduled-string
  11355. "\\) *<\\([^>]+\\)>"))
  11356. (callback
  11357. (lambda () (not
  11358. (time-less-p
  11359. (org-time-string-to-time (match-string 2))
  11360. (org-time-string-to-time date))))))
  11361. (message "%d entries after %s"
  11362. (org-occur regexp nil callback) date)))
  11363. (defun org-evaluate-time-range (&optional to-buffer)
  11364. "Evaluate a time range by computing the difference between start and end.
  11365. Normally the result is just printed in the echo area, but with prefix arg
  11366. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11367. If the time range is actually in a table, the result is inserted into the
  11368. next column.
  11369. For time difference computation, a year is assumed to be exactly 365
  11370. days in order to avoid rounding problems."
  11371. (interactive "P")
  11372. (or
  11373. (org-clock-update-time-maybe)
  11374. (save-excursion
  11375. (unless (org-at-date-range-p t)
  11376. (goto-char (point-at-bol))
  11377. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11378. (if (not (org-at-date-range-p t))
  11379. (error "Not at a time-stamp range, and none found in current line")))
  11380. (let* ((ts1 (match-string 1))
  11381. (ts2 (match-string 2))
  11382. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11383. (match-end (match-end 0))
  11384. (time1 (org-time-string-to-time ts1))
  11385. (time2 (org-time-string-to-time ts2))
  11386. (t1 (time-to-seconds time1))
  11387. (t2 (time-to-seconds time2))
  11388. (diff (abs (- t2 t1)))
  11389. (negative (< (- t2 t1) 0))
  11390. ;; (ys (floor (* 365 24 60 60)))
  11391. (ds (* 24 60 60))
  11392. (hs (* 60 60))
  11393. (fy "%dy %dd %02d:%02d")
  11394. (fy1 "%dy %dd")
  11395. (fd "%dd %02d:%02d")
  11396. (fd1 "%dd")
  11397. (fh "%02d:%02d")
  11398. y d h m align)
  11399. (if havetime
  11400. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11401. y 0
  11402. d (floor (/ diff ds)) diff (mod diff ds)
  11403. h (floor (/ diff hs)) diff (mod diff hs)
  11404. m (floor (/ diff 60)))
  11405. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11406. y 0
  11407. d (floor (+ (/ diff ds) 0.5))
  11408. h 0 m 0))
  11409. (if (not to-buffer)
  11410. (message "%s" (org-make-tdiff-string y d h m))
  11411. (if (org-at-table-p)
  11412. (progn
  11413. (goto-char match-end)
  11414. (setq align t)
  11415. (and (looking-at " *|") (goto-char (match-end 0))))
  11416. (goto-char match-end))
  11417. (if (looking-at
  11418. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11419. (replace-match ""))
  11420. (if negative (insert " -"))
  11421. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11422. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11423. (insert " " (format fh h m))))
  11424. (if align (org-table-align))
  11425. (message "Time difference inserted")))))
  11426. (defun org-make-tdiff-string (y d h m)
  11427. (let ((fmt "")
  11428. (l nil))
  11429. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11430. l (push y l)))
  11431. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11432. l (push d l)))
  11433. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11434. l (push h l)))
  11435. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11436. l (push m l)))
  11437. (apply 'format fmt (nreverse l))))
  11438. (defun org-time-string-to-time (s)
  11439. (apply 'encode-time (org-parse-time-string s)))
  11440. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11441. "Convert a time stamp to an absolute day number.
  11442. If there is a specifyer for a cyclic time stamp, get the closest date to
  11443. DAYNR.
  11444. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11445. (cond
  11446. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11447. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11448. daynr
  11449. (+ daynr 1000)))
  11450. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11451. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11452. (time-to-days (current-time))) (match-string 0 s)
  11453. prefer show-all))
  11454. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11455. (defun org-days-to-iso-week (days)
  11456. "Return the iso week number."
  11457. (require 'cal-iso)
  11458. (car (calendar-iso-from-absolute days)))
  11459. (defun org-small-year-to-year (year)
  11460. "Convert 2-digit years into 4-digit years.
  11461. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11462. The year 2000 cannot be abbreviated. Any year larger than 99
  11463. is returned unchanged."
  11464. (if (< year 38)
  11465. (setq year (+ 2000 year))
  11466. (if (< year 100)
  11467. (setq year (+ 1900 year))))
  11468. year)
  11469. (defun org-time-from-absolute (d)
  11470. "Return the time corresponding to date D.
  11471. D may be an absolute day number, or a calendar-type list (month day year)."
  11472. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11473. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11474. (defun org-calendar-holiday ()
  11475. "List of holidays, for Diary display in Org-mode."
  11476. (require 'holidays)
  11477. (let ((hl (funcall
  11478. (if (fboundp 'calendar-check-holidays)
  11479. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11480. (if hl (mapconcat 'identity hl "; "))))
  11481. (defun org-diary-sexp-entry (sexp entry date)
  11482. "Process a SEXP diary ENTRY for DATE."
  11483. (require 'diary-lib)
  11484. (let ((result (if calendar-debug-sexp
  11485. (let ((stack-trace-on-error t))
  11486. (eval (car (read-from-string sexp))))
  11487. (condition-case nil
  11488. (eval (car (read-from-string sexp)))
  11489. (error
  11490. (beep)
  11491. (message "Bad sexp at line %d in %s: %s"
  11492. (org-current-line)
  11493. (buffer-file-name) sexp)
  11494. (sleep-for 2))))))
  11495. (cond ((stringp result) result)
  11496. ((and (consp result)
  11497. (stringp (cdr result))) (cdr result))
  11498. (result entry)
  11499. (t nil))))
  11500. (defun org-diary-to-ical-string (frombuf)
  11501. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11502. This uses the icalendar.el library."
  11503. (let* ((tmpdir (if (featurep 'xemacs)
  11504. (temp-directory)
  11505. temporary-file-directory))
  11506. (tmpfile (make-temp-name
  11507. (expand-file-name "orgics" tmpdir)))
  11508. buf rtn b e)
  11509. (save-excursion
  11510. (set-buffer frombuf)
  11511. (icalendar-export-region (point-min) (point-max) tmpfile)
  11512. (setq buf (find-buffer-visiting tmpfile))
  11513. (set-buffer buf)
  11514. (goto-char (point-min))
  11515. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11516. (setq b (match-beginning 0)))
  11517. (goto-char (point-max))
  11518. (if (re-search-backward "^END:VEVENT" nil t)
  11519. (setq e (match-end 0)))
  11520. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11521. (kill-buffer buf)
  11522. (delete-file tmpfile)
  11523. rtn))
  11524. (defun org-closest-date (start current change prefer show-all)
  11525. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11526. When PREFER is `past' return a date that is either CURRENT or past.
  11527. When PREFER is `future', return a date that is either CURRENT or future.
  11528. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11529. ;; Make the proper lists from the dates
  11530. (catch 'exit
  11531. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11532. dn dw sday cday n1 n2 n0
  11533. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11534. (setq start (org-date-to-gregorian start)
  11535. current (org-date-to-gregorian
  11536. (if show-all
  11537. current
  11538. (time-to-days (current-time))))
  11539. sday (calendar-absolute-from-gregorian start)
  11540. cday (calendar-absolute-from-gregorian current))
  11541. (if (<= cday sday) (throw 'exit sday))
  11542. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11543. (setq dn (string-to-number (match-string 1 change))
  11544. dw (cdr (assoc (match-string 2 change) a1)))
  11545. (error "Invalid change specifyer: %s" change))
  11546. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11547. (cond
  11548. ((eq dw 'day)
  11549. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11550. n2 (+ n1 dn)))
  11551. ((eq dw 'year)
  11552. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11553. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11554. (setq date1 (list m d y1)
  11555. n1 (calendar-absolute-from-gregorian date1)
  11556. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11557. n2 (calendar-absolute-from-gregorian date2)))
  11558. ((eq dw 'month)
  11559. ;; approx number of month between the two dates
  11560. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11561. ;; How often does dn fit in there?
  11562. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11563. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11564. m (+ m nm)
  11565. ny (floor (/ m 12))
  11566. y (+ y ny)
  11567. m (- m (* ny 12)))
  11568. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11569. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11570. (setq m2 (+ m dn) y2 y)
  11571. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11572. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11573. (while (<= n2 cday)
  11574. (setq n1 n2 m m2 y y2)
  11575. (setq m2 (+ m dn) y2 y)
  11576. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11577. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11578. ;; Make sure n1 is the earlier date
  11579. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11580. (if show-all
  11581. (cond
  11582. ((eq prefer 'past) n1)
  11583. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11584. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11585. (cond
  11586. ((eq prefer 'past) n1)
  11587. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11588. (t (if (= cday n1) n1 n2)))))))
  11589. (defun org-date-to-gregorian (date)
  11590. "Turn any specification of DATE into a gregorian date for the calendar."
  11591. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11592. ((and (listp date) (= (length date) 3)) date)
  11593. ((stringp date)
  11594. (setq date (org-parse-time-string date))
  11595. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11596. ((listp date)
  11597. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11598. (defun org-parse-time-string (s &optional nodefault)
  11599. "Parse the standard Org-mode time string.
  11600. This should be a lot faster than the normal `parse-time-string'.
  11601. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11602. hour and minute fields will be nil if not given."
  11603. (if (string-match org-ts-regexp0 s)
  11604. (list 0
  11605. (if (or (match-beginning 8) (not nodefault))
  11606. (string-to-number (or (match-string 8 s) "0")))
  11607. (if (or (match-beginning 7) (not nodefault))
  11608. (string-to-number (or (match-string 7 s) "0")))
  11609. (string-to-number (match-string 4 s))
  11610. (string-to-number (match-string 3 s))
  11611. (string-to-number (match-string 2 s))
  11612. nil nil nil)
  11613. (make-list 9 0)))
  11614. (defun org-timestamp-up (&optional arg)
  11615. "Increase the date item at the cursor by one.
  11616. If the cursor is on the year, change the year. If it is on the month or
  11617. the day, change that.
  11618. With prefix ARG, change by that many units."
  11619. (interactive "p")
  11620. (org-timestamp-change (prefix-numeric-value arg)))
  11621. (defun org-timestamp-down (&optional arg)
  11622. "Decrease the date item at the cursor by one.
  11623. If the cursor is on the year, change the year. If it is on the month or
  11624. the day, change that.
  11625. With prefix ARG, change by that many units."
  11626. (interactive "p")
  11627. (org-timestamp-change (- (prefix-numeric-value arg))))
  11628. (defun org-timestamp-up-day (&optional arg)
  11629. "Increase the date in the time stamp by one day.
  11630. With prefix ARG, change that many days."
  11631. (interactive "p")
  11632. (if (and (not (org-at-timestamp-p t))
  11633. (org-on-heading-p))
  11634. (org-todo 'up)
  11635. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11636. (defun org-timestamp-down-day (&optional arg)
  11637. "Decrease the date in the time stamp by one day.
  11638. With prefix ARG, change that many days."
  11639. (interactive "p")
  11640. (if (and (not (org-at-timestamp-p t))
  11641. (org-on-heading-p))
  11642. (org-todo 'down)
  11643. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11644. (defun org-at-timestamp-p (&optional inactive-ok)
  11645. "Determine if the cursor is in or at a timestamp."
  11646. (interactive)
  11647. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11648. (pos (point))
  11649. (ans (or (looking-at tsr)
  11650. (save-excursion
  11651. (skip-chars-backward "^[<\n\r\t")
  11652. (if (> (point) (point-min)) (backward-char 1))
  11653. (and (looking-at tsr)
  11654. (> (- (match-end 0) pos) -1))))))
  11655. (and ans
  11656. (boundp 'org-ts-what)
  11657. (setq org-ts-what
  11658. (cond
  11659. ((= pos (match-beginning 0)) 'bracket)
  11660. ((= pos (1- (match-end 0))) 'bracket)
  11661. ((org-pos-in-match-range pos 2) 'year)
  11662. ((org-pos-in-match-range pos 3) 'month)
  11663. ((org-pos-in-match-range pos 7) 'hour)
  11664. ((org-pos-in-match-range pos 8) 'minute)
  11665. ((or (org-pos-in-match-range pos 4)
  11666. (org-pos-in-match-range pos 5)) 'day)
  11667. ((and (> pos (or (match-end 8) (match-end 5)))
  11668. (< pos (match-end 0)))
  11669. (- pos (or (match-end 8) (match-end 5))))
  11670. (t 'day))))
  11671. ans))
  11672. (defun org-toggle-timestamp-type ()
  11673. "Toggle the type (<active> or [inactive]) of a time stamp."
  11674. (interactive)
  11675. (when (org-at-timestamp-p t)
  11676. (let ((beg (match-beginning 0)) (end (match-end 0))
  11677. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11678. (save-excursion
  11679. (goto-char beg)
  11680. (while (re-search-forward "[][<>]" end t)
  11681. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11682. t t)))
  11683. (message "Timestamp is now %sactive"
  11684. (if (equal (char-after beg) ?<) "" "in")))))
  11685. (defun org-timestamp-change (n &optional what)
  11686. "Change the date in the time stamp at point.
  11687. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11688. `year', `minute', `second'. If WHAT is not given, the cursor position
  11689. in the timestamp determines what will be changed."
  11690. (let ((pos (point))
  11691. with-hm inactive
  11692. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11693. org-ts-what
  11694. extra rem
  11695. ts time time0)
  11696. (if (not (org-at-timestamp-p t))
  11697. (error "Not at a timestamp"))
  11698. (if (and (not what) (eq org-ts-what 'bracket))
  11699. (org-toggle-timestamp-type)
  11700. (if (and (not what) (not (eq org-ts-what 'day))
  11701. org-display-custom-times
  11702. (get-text-property (point) 'display)
  11703. (not (get-text-property (1- (point)) 'display)))
  11704. (setq org-ts-what 'day))
  11705. (setq org-ts-what (or what org-ts-what)
  11706. inactive (= (char-after (match-beginning 0)) ?\[)
  11707. ts (match-string 0))
  11708. (replace-match "")
  11709. (if (string-match
  11710. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11711. ts)
  11712. (setq extra (match-string 1 ts)))
  11713. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11714. (setq with-hm t))
  11715. (setq time0 (org-parse-time-string ts))
  11716. (when (and (eq org-ts-what 'minute)
  11717. (eq current-prefix-arg nil))
  11718. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11719. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11720. (setcar (cdr time0) (+ (nth 1 time0)
  11721. (if (> n 0) (- rem) (- dm rem))))))
  11722. (setq time
  11723. (encode-time (or (car time0) 0)
  11724. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11725. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11726. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11727. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11728. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11729. (nthcdr 6 time0)))
  11730. (when (and (member org-ts-what '(hour minute))
  11731. extra
  11732. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11733. (setq extra (org-modify-ts-extra
  11734. extra
  11735. (if (eq org-ts-what 'hour) 2 5)
  11736. n dm)))
  11737. (when (integerp org-ts-what)
  11738. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11739. (if (eq what 'calendar)
  11740. (let ((cal-date (org-get-date-from-calendar)))
  11741. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11742. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11743. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11744. (setcar time0 (or (car time0) 0))
  11745. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11746. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11747. (setq time (apply 'encode-time time0))))
  11748. (setq org-last-changed-timestamp
  11749. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11750. (org-clock-update-time-maybe)
  11751. (goto-char pos)
  11752. ;; Try to recenter the calendar window, if any
  11753. (if (and org-calendar-follow-timestamp-change
  11754. (get-buffer-window "*Calendar*" t)
  11755. (memq org-ts-what '(day month year)))
  11756. (org-recenter-calendar (time-to-days time))))))
  11757. (defun org-modify-ts-extra (s pos n dm)
  11758. "Change the different parts of the lead-time and repeat fields in timestamp."
  11759. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11760. ng h m new rem)
  11761. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11762. (cond
  11763. ((or (org-pos-in-match-range pos 2)
  11764. (org-pos-in-match-range pos 3))
  11765. (setq m (string-to-number (match-string 3 s))
  11766. h (string-to-number (match-string 2 s)))
  11767. (if (org-pos-in-match-range pos 2)
  11768. (setq h (+ h n))
  11769. (setq n (* dm (org-no-warnings (signum n))))
  11770. (when (not (= 0 (setq rem (% m dm))))
  11771. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11772. (setq m (+ m n)))
  11773. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11774. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11775. (setq h (min 24 (max 0 h)))
  11776. (setq ng 1 new (format "-%02d:%02d" h m)))
  11777. ((org-pos-in-match-range pos 6)
  11778. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11779. ((org-pos-in-match-range pos 5)
  11780. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11781. ((org-pos-in-match-range pos 9)
  11782. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11783. ((org-pos-in-match-range pos 8)
  11784. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11785. (when ng
  11786. (setq s (concat
  11787. (substring s 0 (match-beginning ng))
  11788. new
  11789. (substring s (match-end ng))))))
  11790. s))
  11791. (defun org-recenter-calendar (date)
  11792. "If the calendar is visible, recenter it to DATE."
  11793. (let* ((win (selected-window))
  11794. (cwin (get-buffer-window "*Calendar*" t))
  11795. (calendar-move-hook nil))
  11796. (when cwin
  11797. (select-window cwin)
  11798. (calendar-goto-date (if (listp date) date
  11799. (calendar-gregorian-from-absolute date)))
  11800. (select-window win))))
  11801. (defun org-goto-calendar (&optional arg)
  11802. "Go to the Emacs calendar at the current date.
  11803. If there is a time stamp in the current line, go to that date.
  11804. A prefix ARG can be used to force the current date."
  11805. (interactive "P")
  11806. (let ((tsr org-ts-regexp) diff
  11807. (calendar-move-hook nil)
  11808. (calendar-view-holidays-initially-flag nil)
  11809. (view-calendar-holidays-initially nil)
  11810. (calendar-view-diary-initially-flag nil)
  11811. (view-diary-entries-initially nil))
  11812. (if (or (org-at-timestamp-p)
  11813. (save-excursion
  11814. (beginning-of-line 1)
  11815. (looking-at (concat ".*" tsr))))
  11816. (let ((d1 (time-to-days (current-time)))
  11817. (d2 (time-to-days
  11818. (org-time-string-to-time (match-string 1)))))
  11819. (setq diff (- d2 d1))))
  11820. (calendar)
  11821. (calendar-goto-today)
  11822. (if (and diff (not arg)) (calendar-forward-day diff))))
  11823. (defun org-get-date-from-calendar ()
  11824. "Return a list (month day year) of date at point in calendar."
  11825. (with-current-buffer "*Calendar*"
  11826. (save-match-data
  11827. (calendar-cursor-to-date))))
  11828. (defun org-date-from-calendar ()
  11829. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11830. If there is already a time stamp at the cursor position, update it."
  11831. (interactive)
  11832. (if (org-at-timestamp-p t)
  11833. (org-timestamp-change 0 'calendar)
  11834. (let ((cal-date (org-get-date-from-calendar)))
  11835. (org-insert-time-stamp
  11836. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11837. (defun org-minutes-to-hh:mm-string (m)
  11838. "Compute H:MM from a number of minutes."
  11839. (let ((h (/ m 60)))
  11840. (setq m (- m (* 60 h)))
  11841. (format org-time-clocksum-format h m)))
  11842. (defun org-hh:mm-string-to-minutes (s)
  11843. "Convert a string H:MM to a number of minutes."
  11844. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11845. (+ (* (string-to-number (match-string 1 s)) 60)
  11846. (string-to-number (match-string 2 s)))
  11847. 0))
  11848. ;;;; Files
  11849. (defun org-save-all-org-buffers ()
  11850. "Save all Org-mode buffers without user confirmation."
  11851. (interactive)
  11852. (message "Saving all Org-mode buffers...")
  11853. (save-some-buffers t 'org-mode-p)
  11854. (when (featurep 'org-id) (org-id-locations-save))
  11855. (message "Saving all Org-mode buffers... done"))
  11856. (defun org-revert-all-org-buffers ()
  11857. "Revert all Org-mode buffers.
  11858. Prompt for confirmation when there are unsaved changes.
  11859. Be sure you know what you are doing before letting this function
  11860. overwrite your changes.
  11861. This function is useful in a setup where one tracks org files
  11862. with a version control system, to revert on one machine after pulling
  11863. changes from another. I believe the procedure must be like this:
  11864. 1. M-x org-save-all-org-buffers
  11865. 2. Pull changes from the other machine, resolve conflicts
  11866. 3. M-x org-revert-all-org-buffers"
  11867. (interactive)
  11868. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11869. (error "Abort"))
  11870. (save-excursion
  11871. (save-window-excursion
  11872. (mapc
  11873. (lambda (b)
  11874. (when (and (with-current-buffer b (org-mode-p))
  11875. (with-current-buffer b buffer-file-name))
  11876. (switch-to-buffer b)
  11877. (revert-buffer t 'no-confirm)))
  11878. (buffer-list))
  11879. (when (and (featurep 'org-id) org-id-track-globally)
  11880. (org-id-locations-load)))))
  11881. ;;;; Agenda files
  11882. ;;;###autoload
  11883. (defun org-iswitchb (&optional arg)
  11884. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11885. With a prefix argument, restrict available to files.
  11886. With two prefix arguments, restrict available buffers to agenda files.
  11887. Due to some yet unresolved reason, the global function
  11888. `iswitchb-mode' needs to be active for this function to work."
  11889. (interactive "P")
  11890. (require 'iswitchb)
  11891. (let ((enabled iswitchb-mode) blist)
  11892. (or enabled (iswitchb-mode 1))
  11893. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11894. ((equal arg '(16)) (org-buffer-list 'agenda))
  11895. (t (org-buffer-list))))
  11896. (unwind-protect
  11897. (let ((iswitchb-make-buflist-hook
  11898. (lambda ()
  11899. (setq iswitchb-temp-buflist
  11900. (mapcar 'buffer-name blist)))))
  11901. (switch-to-buffer
  11902. (iswitchb-read-buffer
  11903. "Switch-to: " nil t))
  11904. (or enabled (iswitchb-mode -1))))))
  11905. ;;;###autoload
  11906. (defun org-ido-switchb (&optional arg)
  11907. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11908. With a prefix argument, restrict available to files.
  11909. With two prefix arguments, restrict available buffers to agenda files."
  11910. (interactive "P")
  11911. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11912. ((equal arg '(16)) (org-buffer-list 'agenda))
  11913. (t (org-buffer-list)))))
  11914. (switch-to-buffer
  11915. (org-ido-completing-read "Org buffer: "
  11916. (mapcar 'list (mapcar 'buffer-name blist))
  11917. nil t))))
  11918. (defun org-buffer-list (&optional predicate exclude-tmp)
  11919. "Return a list of Org buffers.
  11920. PREDICATE can be `export', `files' or `agenda'.
  11921. export restrict the list to Export buffers.
  11922. files restrict the list to buffers visiting Org files.
  11923. agenda restrict the list to buffers visiting agenda files.
  11924. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11925. (let* ((bfn nil)
  11926. (agenda-files (and (eq predicate 'agenda)
  11927. (mapcar 'file-truename (org-agenda-files t))))
  11928. (filter
  11929. (cond
  11930. ((eq predicate 'files)
  11931. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11932. ((eq predicate 'export)
  11933. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11934. ((eq predicate 'agenda)
  11935. (lambda (b)
  11936. (with-current-buffer b
  11937. (and (eq major-mode 'org-mode)
  11938. (setq bfn (buffer-file-name b))
  11939. (member (file-truename bfn) agenda-files)))))
  11940. (t (lambda (b) (with-current-buffer b
  11941. (or (eq major-mode 'org-mode)
  11942. (string-match "\*Org .*Export"
  11943. (buffer-name b)))))))))
  11944. (delq nil
  11945. (mapcar
  11946. (lambda(b)
  11947. (if (and (funcall filter b)
  11948. (or (not exclude-tmp)
  11949. (not (string-match "tmp" (buffer-name b)))))
  11950. b
  11951. nil))
  11952. (buffer-list)))))
  11953. (defun org-agenda-files (&optional unrestricted archives)
  11954. "Get the list of agenda files.
  11955. Optional UNRESTRICTED means return the full list even if a restriction
  11956. is currently in place.
  11957. When ARCHIVES is t, include all archive files hat are really being
  11958. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11959. `org-agenda-archives-mode' is t."
  11960. (let ((files
  11961. (cond
  11962. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11963. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11964. ((listp org-agenda-files) org-agenda-files)
  11965. (t (error "Invalid value of `org-agenda-files'")))))
  11966. (setq files (apply 'append
  11967. (mapcar (lambda (f)
  11968. (if (file-directory-p f)
  11969. (directory-files
  11970. f t org-agenda-file-regexp)
  11971. (list f)))
  11972. files)))
  11973. (when org-agenda-skip-unavailable-files
  11974. (setq files (delq nil
  11975. (mapcar (function
  11976. (lambda (file)
  11977. (and (file-readable-p file) file)))
  11978. files))))
  11979. (when (or (eq archives t)
  11980. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11981. (setq files (org-add-archive-files files)))
  11982. files))
  11983. (defun org-edit-agenda-file-list ()
  11984. "Edit the list of agenda files.
  11985. Depending on setup, this either uses customize to edit the variable
  11986. `org-agenda-files', or it visits the file that is holding the list. In the
  11987. latter case, the buffer is set up in a way that saving it automatically kills
  11988. the buffer and restores the previous window configuration."
  11989. (interactive)
  11990. (if (stringp org-agenda-files)
  11991. (let ((cw (current-window-configuration)))
  11992. (find-file org-agenda-files)
  11993. (org-set-local 'org-window-configuration cw)
  11994. (org-add-hook 'after-save-hook
  11995. (lambda ()
  11996. (set-window-configuration
  11997. (prog1 org-window-configuration
  11998. (kill-buffer (current-buffer))))
  11999. (org-install-agenda-files-menu)
  12000. (message "New agenda file list installed"))
  12001. nil 'local)
  12002. (message "%s" (substitute-command-keys
  12003. "Edit list and finish with \\[save-buffer]")))
  12004. (customize-variable 'org-agenda-files)))
  12005. (defun org-store-new-agenda-file-list (list)
  12006. "Set new value for the agenda file list and save it correctly."
  12007. (if (stringp org-agenda-files)
  12008. (let ((f org-agenda-files) b)
  12009. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12010. (with-temp-file f
  12011. (insert (mapconcat 'identity list "\n") "\n")))
  12012. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12013. (setq org-agenda-files list)
  12014. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12015. (defun org-read-agenda-file-list ()
  12016. "Read the list of agenda files from a file."
  12017. (when (file-directory-p org-agenda-files)
  12018. (error "`org-agenda-files' cannot be a single directory"))
  12019. (when (stringp org-agenda-files)
  12020. (with-temp-buffer
  12021. (insert-file-contents org-agenda-files)
  12022. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12023. ;;;###autoload
  12024. (defun org-cycle-agenda-files ()
  12025. "Cycle through the files in `org-agenda-files'.
  12026. If the current buffer visits an agenda file, find the next one in the list.
  12027. If the current buffer does not, find the first agenda file."
  12028. (interactive)
  12029. (let* ((fs (org-agenda-files t))
  12030. (files (append fs (list (car fs))))
  12031. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12032. file)
  12033. (unless files (error "No agenda files"))
  12034. (catch 'exit
  12035. (while (setq file (pop files))
  12036. (if (equal (file-truename file) tcf)
  12037. (when (car files)
  12038. (find-file (car files))
  12039. (throw 'exit t))))
  12040. (find-file (car fs)))
  12041. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12042. (defun org-agenda-file-to-front (&optional to-end)
  12043. "Move/add the current file to the top of the agenda file list.
  12044. If the file is not present in the list, it is added to the front. If it is
  12045. present, it is moved there. With optional argument TO-END, add/move to the
  12046. end of the list."
  12047. (interactive "P")
  12048. (let ((org-agenda-skip-unavailable-files nil)
  12049. (file-alist (mapcar (lambda (x)
  12050. (cons (file-truename x) x))
  12051. (org-agenda-files t)))
  12052. (ctf (file-truename buffer-file-name))
  12053. x had)
  12054. (setq x (assoc ctf file-alist) had x)
  12055. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12056. (if to-end
  12057. (setq file-alist (append (delq x file-alist) (list x)))
  12058. (setq file-alist (cons x (delq x file-alist))))
  12059. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12060. (org-install-agenda-files-menu)
  12061. (message "File %s to %s of agenda file list"
  12062. (if had "moved" "added") (if to-end "end" "front"))))
  12063. (defun org-remove-file (&optional file)
  12064. "Remove current file from the list of files in variable `org-agenda-files'.
  12065. These are the files which are being checked for agenda entries.
  12066. Optional argument FILE means, use this file instead of the current."
  12067. (interactive)
  12068. (let* ((org-agenda-skip-unavailable-files nil)
  12069. (file (or file buffer-file-name))
  12070. (true-file (file-truename file))
  12071. (afile (abbreviate-file-name file))
  12072. (files (delq nil (mapcar
  12073. (lambda (x)
  12074. (if (equal true-file
  12075. (file-truename x))
  12076. nil x))
  12077. (org-agenda-files t)))))
  12078. (if (not (= (length files) (length (org-agenda-files t))))
  12079. (progn
  12080. (org-store-new-agenda-file-list files)
  12081. (org-install-agenda-files-menu)
  12082. (message "Removed file: %s" afile))
  12083. (message "File was not in list: %s (not removed)" afile))))
  12084. (defun org-file-menu-entry (file)
  12085. (vector file (list 'find-file file) t))
  12086. (defun org-check-agenda-file (file)
  12087. "Make sure FILE exists. If not, ask user what to do."
  12088. (when (not (file-exists-p file))
  12089. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12090. (abbreviate-file-name file))
  12091. (let ((r (downcase (read-char-exclusive))))
  12092. (cond
  12093. ((equal r ?r)
  12094. (org-remove-file file)
  12095. (throw 'nextfile t))
  12096. (t (error "Abort"))))))
  12097. (defun org-get-agenda-file-buffer (file)
  12098. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12099. it to the list of buffers which might be released later."
  12100. (let ((buf (org-find-base-buffer-visiting file)))
  12101. (if buf
  12102. buf ; just return it
  12103. ;; Make a new buffer and remember it
  12104. (setq buf (find-file-noselect file))
  12105. (if buf (push buf org-agenda-new-buffers))
  12106. buf)))
  12107. (defun org-release-buffers (blist)
  12108. "Release all buffers in list, asking the user for confirmation when needed.
  12109. When a buffer is unmodified, it is just killed. When modified, it is saved
  12110. \(if the user agrees) and then killed."
  12111. (let (buf file)
  12112. (while (setq buf (pop blist))
  12113. (setq file (buffer-file-name buf))
  12114. (when (and (buffer-modified-p buf)
  12115. file
  12116. (y-or-n-p (format "Save file %s? " file)))
  12117. (with-current-buffer buf (save-buffer)))
  12118. (kill-buffer buf))))
  12119. (defun org-prepare-agenda-buffers (files)
  12120. "Create buffers for all agenda files, protect archived trees and comments."
  12121. (interactive)
  12122. (let ((pa '(:org-archived t))
  12123. (pc '(:org-comment t))
  12124. (pall '(:org-archived t :org-comment t))
  12125. (inhibit-read-only t)
  12126. (rea (concat ":" org-archive-tag ":"))
  12127. bmp file re)
  12128. (save-excursion
  12129. (save-restriction
  12130. (while (setq file (pop files))
  12131. (if (bufferp file)
  12132. (set-buffer file)
  12133. (org-check-agenda-file file)
  12134. (set-buffer (org-get-agenda-file-buffer file)))
  12135. (widen)
  12136. (setq bmp (buffer-modified-p))
  12137. (org-refresh-category-properties)
  12138. (setq org-todo-keywords-for-agenda
  12139. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12140. (setq org-done-keywords-for-agenda
  12141. (append org-done-keywords-for-agenda org-done-keywords))
  12142. (setq org-todo-keyword-alist-for-agenda
  12143. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12144. (setq org-tag-alist-for-agenda
  12145. (append org-tag-alist-for-agenda org-tag-alist))
  12146. (save-excursion
  12147. (remove-text-properties (point-min) (point-max) pall)
  12148. (when org-agenda-skip-archived-trees
  12149. (goto-char (point-min))
  12150. (while (re-search-forward rea nil t)
  12151. (if (org-on-heading-p t)
  12152. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12153. (goto-char (point-min))
  12154. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12155. (while (re-search-forward re nil t)
  12156. (add-text-properties
  12157. (match-beginning 0) (org-end-of-subtree t) pc)))
  12158. (set-buffer-modified-p bmp))))
  12159. (setq org-todo-keyword-alist-for-agenda
  12160. (org-uniquify org-todo-keyword-alist-for-agenda)
  12161. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12162. ;;;; Embedded LaTeX
  12163. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12164. "Keymap for the minor `org-cdlatex-mode'.")
  12165. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12166. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12167. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12168. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12169. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12170. (defvar org-cdlatex-texmathp-advice-is-done nil
  12171. "Flag remembering if we have applied the advice to texmathp already.")
  12172. (define-minor-mode org-cdlatex-mode
  12173. "Toggle the minor `org-cdlatex-mode'.
  12174. This mode supports entering LaTeX environment and math in LaTeX fragments
  12175. in Org-mode.
  12176. \\{org-cdlatex-mode-map}"
  12177. nil " OCDL" nil
  12178. (when org-cdlatex-mode (require 'cdlatex))
  12179. (unless org-cdlatex-texmathp-advice-is-done
  12180. (setq org-cdlatex-texmathp-advice-is-done t)
  12181. (defadvice texmathp (around org-math-always-on activate)
  12182. "Always return t in org-mode buffers.
  12183. This is because we want to insert math symbols without dollars even outside
  12184. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12185. an embedded LaTeX fragment, let texmathp do its job.
  12186. \\[org-cdlatex-mode-map]"
  12187. (interactive)
  12188. (let (p)
  12189. (cond
  12190. ((not (org-mode-p)) ad-do-it)
  12191. ((eq this-command 'cdlatex-math-symbol)
  12192. (setq ad-return-value t
  12193. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12194. (t
  12195. (let ((p (org-inside-LaTeX-fragment-p)))
  12196. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12197. (setq ad-return-value t
  12198. texmathp-why '("Org-mode embedded math" . 0))
  12199. (if p ad-do-it)))))))))
  12200. (defun turn-on-org-cdlatex ()
  12201. "Unconditionally turn on `org-cdlatex-mode'."
  12202. (org-cdlatex-mode 1))
  12203. (defun org-inside-LaTeX-fragment-p ()
  12204. "Test if point is inside a LaTeX fragment.
  12205. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12206. sequence appearing also before point.
  12207. Even though the matchers for math are configurable, this function assumes
  12208. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12209. delimiters are skipped when they have been removed by customization.
  12210. The return value is nil, or a cons cell with the delimiter and
  12211. and the position of this delimiter.
  12212. This function does a reasonably good job, but can locally be fooled by
  12213. for example currency specifications. For example it will assume being in
  12214. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12215. fragments that are properly closed, but during editing, we have to live
  12216. with the uncertainty caused by missing closing delimiters. This function
  12217. looks only before point, not after."
  12218. (catch 'exit
  12219. (let ((pos (point))
  12220. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12221. (lim (progn
  12222. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12223. (point)))
  12224. dd-on str (start 0) m re)
  12225. (goto-char pos)
  12226. (when dodollar
  12227. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12228. re (nth 1 (assoc "$" org-latex-regexps)))
  12229. (while (string-match re str start)
  12230. (cond
  12231. ((= (match-end 0) (length str))
  12232. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12233. ((= (match-end 0) (- (length str) 5))
  12234. (throw 'exit nil))
  12235. (t (setq start (match-end 0))))))
  12236. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12237. (goto-char pos)
  12238. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12239. (and (match-beginning 2) (throw 'exit nil))
  12240. ;; count $$
  12241. (while (re-search-backward "\\$\\$" lim t)
  12242. (setq dd-on (not dd-on)))
  12243. (goto-char pos)
  12244. (if dd-on (cons "$$" m))))))
  12245. (defun org-try-cdlatex-tab ()
  12246. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12247. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12248. - inside a LaTeX fragment, or
  12249. - after the first word in a line, where an abbreviation expansion could
  12250. insert a LaTeX environment."
  12251. (when org-cdlatex-mode
  12252. (cond
  12253. ((save-excursion
  12254. (skip-chars-backward "a-zA-Z0-9*")
  12255. (skip-chars-backward " \t")
  12256. (bolp))
  12257. (cdlatex-tab) t)
  12258. ((org-inside-LaTeX-fragment-p)
  12259. (cdlatex-tab) t)
  12260. (t nil))))
  12261. (defun org-cdlatex-underscore-caret (&optional arg)
  12262. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12263. Revert to the normal definition outside of these fragments."
  12264. (interactive "P")
  12265. (if (org-inside-LaTeX-fragment-p)
  12266. (call-interactively 'cdlatex-sub-superscript)
  12267. (let (org-cdlatex-mode)
  12268. (call-interactively (key-binding (vector last-input-event))))))
  12269. (defun org-cdlatex-math-modify (&optional arg)
  12270. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12271. Revert to the normal definition outside of these fragments."
  12272. (interactive "P")
  12273. (if (org-inside-LaTeX-fragment-p)
  12274. (call-interactively 'cdlatex-math-modify)
  12275. (let (org-cdlatex-mode)
  12276. (call-interactively (key-binding (vector last-input-event))))))
  12277. (defvar org-latex-fragment-image-overlays nil
  12278. "List of overlays carrying the images of latex fragments.")
  12279. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12280. (defun org-remove-latex-fragment-image-overlays ()
  12281. "Remove all overlays with LaTeX fragment images in current buffer."
  12282. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12283. (setq org-latex-fragment-image-overlays nil))
  12284. (defun org-preview-latex-fragment (&optional subtree)
  12285. "Preview the LaTeX fragment at point, or all locally or globally.
  12286. If the cursor is in a LaTeX fragment, create the image and overlay
  12287. it over the source code. If there is no fragment at point, display
  12288. all fragments in the current text, from one headline to the next. With
  12289. prefix SUBTREE, display all fragments in the current subtree. With a
  12290. double prefix `C-u C-u', or when the cursor is before the first headline,
  12291. display all fragments in the buffer.
  12292. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12293. (interactive "P")
  12294. (org-remove-latex-fragment-image-overlays)
  12295. (save-excursion
  12296. (save-restriction
  12297. (let (beg end at msg)
  12298. (cond
  12299. ((or (equal subtree '(16))
  12300. (not (save-excursion
  12301. (re-search-backward (concat "^" outline-regexp) nil t))))
  12302. (setq beg (point-min) end (point-max)
  12303. msg "Creating images for buffer...%s"))
  12304. ((equal subtree '(4))
  12305. (org-back-to-heading)
  12306. (setq beg (point) end (org-end-of-subtree t)
  12307. msg "Creating images for subtree...%s"))
  12308. (t
  12309. (if (setq at (org-inside-LaTeX-fragment-p))
  12310. (goto-char (max (point-min) (- (cdr at) 2)))
  12311. (org-back-to-heading))
  12312. (setq beg (point) end (progn (outline-next-heading) (point))
  12313. msg (if at "Creating image...%s"
  12314. "Creating images for entry...%s"))))
  12315. (message msg "")
  12316. (narrow-to-region beg end)
  12317. (goto-char beg)
  12318. (org-format-latex
  12319. (concat "ltxpng/" (file-name-sans-extension
  12320. (file-name-nondirectory
  12321. buffer-file-name)))
  12322. default-directory 'overlays msg at 'forbuffer)
  12323. (message msg "done. Use `C-c C-c' to remove images.")))))
  12324. (defvar org-latex-regexps
  12325. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12326. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12327. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12328. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12329. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12330. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12331. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12332. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12333. "Regular expressions for matching embedded LaTeX.")
  12334. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12335. "Replace LaTeX fragments with links to an image, and produce images."
  12336. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12337. (let* ((prefixnodir (file-name-nondirectory prefix))
  12338. (absprefix (expand-file-name prefix dir))
  12339. (todir (file-name-directory absprefix))
  12340. (opt org-format-latex-options)
  12341. (matchers (plist-get opt :matchers))
  12342. (re-list org-latex-regexps)
  12343. (cnt 0) txt link beg end re e checkdir
  12344. m n block linkfile movefile ov)
  12345. ;; Check if there are old images files with this prefix, and remove them
  12346. (when (file-directory-p todir)
  12347. (mapc 'delete-file
  12348. (directory-files
  12349. todir 'full
  12350. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12351. ;; Check the different regular expressions
  12352. (while (setq e (pop re-list))
  12353. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12354. block (if (nth 3 e) "\n\n" ""))
  12355. (when (member m matchers)
  12356. (goto-char (point-min))
  12357. (while (re-search-forward re nil t)
  12358. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12359. (not (get-text-property (match-beginning n)
  12360. 'org-protected)))
  12361. (setq txt (match-string n)
  12362. beg (match-beginning n) end (match-end n)
  12363. cnt (1+ cnt)
  12364. linkfile (format "%s_%04d.png" prefix cnt)
  12365. movefile (format "%s_%04d.png" absprefix cnt)
  12366. link (concat block "[[file:" linkfile "]]" block))
  12367. (if msg (message msg cnt))
  12368. (goto-char beg)
  12369. (unless checkdir ; make sure the directory exists
  12370. (setq checkdir t)
  12371. (or (file-directory-p todir) (make-directory todir)))
  12372. (org-create-formula-image
  12373. txt movefile opt forbuffer)
  12374. (if overlays
  12375. (progn
  12376. (setq ov (org-make-overlay beg end))
  12377. (if (featurep 'xemacs)
  12378. (progn
  12379. (org-overlay-put ov 'invisible t)
  12380. (org-overlay-put
  12381. ov 'end-glyph
  12382. (make-glyph (vector 'png :file movefile))))
  12383. (org-overlay-put
  12384. ov 'display
  12385. (list 'image :type 'png :file movefile :ascent 'center)))
  12386. (push ov org-latex-fragment-image-overlays)
  12387. (goto-char end))
  12388. (delete-region beg end)
  12389. (insert link))))))))
  12390. ;; This function borrows from Ganesh Swami's latex2png.el
  12391. (defun org-create-formula-image (string tofile options buffer)
  12392. (let* ((tmpdir (if (featurep 'xemacs)
  12393. (temp-directory)
  12394. temporary-file-directory))
  12395. (texfilebase (make-temp-name
  12396. (expand-file-name "orgtex" tmpdir)))
  12397. (texfile (concat texfilebase ".tex"))
  12398. (dvifile (concat texfilebase ".dvi"))
  12399. (pngfile (concat texfilebase ".png"))
  12400. (fnh (if (featurep 'xemacs)
  12401. (font-height (get-face-font 'default))
  12402. (face-attribute 'default :height nil)))
  12403. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12404. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12405. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12406. "Black"))
  12407. (bg (or (plist-get options (if buffer :background :html-background))
  12408. "Transparent")))
  12409. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12410. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12411. (with-temp-file texfile
  12412. (insert org-format-latex-header
  12413. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12414. (let ((dir default-directory))
  12415. (condition-case nil
  12416. (progn
  12417. (cd tmpdir)
  12418. (call-process "latex" nil nil nil texfile))
  12419. (error nil))
  12420. (cd dir))
  12421. (if (not (file-exists-p dvifile))
  12422. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12423. (condition-case nil
  12424. (call-process "dvipng" nil nil nil
  12425. "-E" "-fg" fg "-bg" bg
  12426. "-D" dpi
  12427. ;;"-x" scale "-y" scale
  12428. "-T" "tight"
  12429. "-o" pngfile
  12430. dvifile)
  12431. (error nil))
  12432. (if (not (file-exists-p pngfile))
  12433. (progn (message "Failed to create png file from %s" texfile) nil)
  12434. ;; Use the requested file name and clean up
  12435. (copy-file pngfile tofile 'replace)
  12436. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12437. (delete-file (concat texfilebase e)))
  12438. pngfile))))
  12439. (defun org-dvipng-color (attr)
  12440. "Return an rgb color specification for dvipng."
  12441. (apply 'format "rgb %s %s %s"
  12442. (mapcar 'org-normalize-color
  12443. (color-values (face-attribute 'default attr nil)))))
  12444. (defun org-normalize-color (value)
  12445. "Return string to be used as color value for an RGB component."
  12446. (format "%g" (/ value 65535.0)))
  12447. ;;;; Key bindings
  12448. ;; Make `C-c C-x' a prefix key
  12449. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12450. ;; TAB key with modifiers
  12451. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12452. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12453. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12454. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12455. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12456. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12457. ;; The following line is necessary under Suse GNU/Linux
  12458. (unless (featurep 'xemacs)
  12459. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12460. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12461. (define-key org-mode-map [backtab] 'org-shifttab)
  12462. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12463. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12464. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12465. ;; Cursor keys with modifiers
  12466. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12467. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12468. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12469. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12470. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12471. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12472. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12473. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12474. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12475. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12476. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12477. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12478. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12479. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12480. ;;; Extra keys for tty access.
  12481. ;; We only set them when really needed because otherwise the
  12482. ;; menus don't show the simple keys
  12483. (when (or org-use-extra-keys
  12484. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12485. (not window-system))
  12486. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12487. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12488. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12489. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12490. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12491. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12492. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12493. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12494. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12495. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12496. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12497. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12498. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12499. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12500. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12501. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12502. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12503. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12504. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12505. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12506. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12507. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12508. ;; All the other keys
  12509. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12510. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12511. (if (boundp 'narrow-map)
  12512. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12513. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12514. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12515. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12516. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12517. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12518. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12519. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12520. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12521. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12522. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12523. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12524. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12525. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12526. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12527. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12528. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12529. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12530. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12531. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12532. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12533. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12534. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12535. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12536. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12537. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12538. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12539. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12540. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12541. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12542. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12543. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12544. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12545. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12546. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12547. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12548. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12549. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12550. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12551. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12552. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12553. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12554. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12555. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12556. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12557. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12558. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12559. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12560. (org-defkey org-mode-map "\C-m" 'org-return)
  12561. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12562. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12563. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12564. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12565. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12566. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12567. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12568. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12569. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12570. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12571. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12572. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12573. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12574. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12575. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12576. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12577. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12578. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12579. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12580. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12581. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12582. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12583. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12584. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12585. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12586. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12587. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12588. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12589. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12590. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12591. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12592. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12593. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12594. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12595. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12596. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12597. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12598. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12599. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12600. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12601. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12602. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12603. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12604. (when (featurep 'xemacs)
  12605. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12606. (defvar org-self-insert-command-undo-counter 0)
  12607. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12608. (defun org-self-insert-command (N)
  12609. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12610. If the cursor is in a table looking at whitespace, the whitespace is
  12611. overwritten, and the table is not marked as requiring realignment."
  12612. (interactive "p")
  12613. (if (and
  12614. (org-table-p)
  12615. (progn
  12616. ;; check if we blank the field, and if that triggers align
  12617. (and (featurep 'org-table) org-table-auto-blank-field
  12618. (member last-command
  12619. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12620. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12621. ;; got extra space, this field does not determine column width
  12622. (let (org-table-may-need-update) (org-table-blank-field))
  12623. ;; no extra space, this field may determine column width
  12624. (org-table-blank-field)))
  12625. t)
  12626. (eq N 1)
  12627. (looking-at "[^|\n]* |"))
  12628. (let (org-table-may-need-update)
  12629. (goto-char (1- (match-end 0)))
  12630. (delete-backward-char 1)
  12631. (goto-char (match-beginning 0))
  12632. (self-insert-command N))
  12633. (setq org-table-may-need-update t)
  12634. (self-insert-command N)
  12635. (org-fix-tags-on-the-fly)
  12636. (if org-self-insert-cluster-for-undo
  12637. (if (not (eq last-command 'org-self-insert-command))
  12638. (setq org-self-insert-command-undo-counter 1)
  12639. (if (>= org-self-insert-command-undo-counter 20)
  12640. (setq org-self-insert-command-undo-counter 1)
  12641. (and (> org-self-insert-command-undo-counter 0)
  12642. buffer-undo-list
  12643. (not (cadr buffer-undo-list)) ; remove nil entry
  12644. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12645. (setq org-self-insert-command-undo-counter
  12646. (1+ org-self-insert-command-undo-counter)))))))
  12647. (defun org-fix-tags-on-the-fly ()
  12648. (when (and (equal (char-after (point-at-bol)) ?*)
  12649. (org-on-heading-p))
  12650. (org-align-tags-here org-tags-column)))
  12651. (defun org-delete-backward-char (N)
  12652. "Like `delete-backward-char', insert whitespace at field end in tables.
  12653. When deleting backwards, in tables this function will insert whitespace in
  12654. front of the next \"|\" separator, to keep the table aligned. The table will
  12655. still be marked for re-alignment if the field did fill the entire column,
  12656. because, in this case the deletion might narrow the column."
  12657. (interactive "p")
  12658. (if (and (org-table-p)
  12659. (eq N 1)
  12660. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12661. (looking-at ".*?|"))
  12662. (let ((pos (point))
  12663. (noalign (looking-at "[^|\n\r]* |"))
  12664. (c org-table-may-need-update))
  12665. (backward-delete-char N)
  12666. (skip-chars-forward "^|")
  12667. (insert " ")
  12668. (goto-char (1- pos))
  12669. ;; noalign: if there were two spaces at the end, this field
  12670. ;; does not determine the width of the column.
  12671. (if noalign (setq org-table-may-need-update c)))
  12672. (backward-delete-char N)
  12673. (org-fix-tags-on-the-fly)))
  12674. (defun org-delete-char (N)
  12675. "Like `delete-char', but insert whitespace at field end in tables.
  12676. When deleting characters, in tables this function will insert whitespace in
  12677. front of the next \"|\" separator, to keep the table aligned. The table will
  12678. still be marked for re-alignment if the field did fill the entire column,
  12679. because, in this case the deletion might narrow the column."
  12680. (interactive "p")
  12681. (if (and (org-table-p)
  12682. (not (bolp))
  12683. (not (= (char-after) ?|))
  12684. (eq N 1))
  12685. (if (looking-at ".*?|")
  12686. (let ((pos (point))
  12687. (noalign (looking-at "[^|\n\r]* |"))
  12688. (c org-table-may-need-update))
  12689. (replace-match (concat
  12690. (substring (match-string 0) 1 -1)
  12691. " |"))
  12692. (goto-char pos)
  12693. ;; noalign: if there were two spaces at the end, this field
  12694. ;; does not determine the width of the column.
  12695. (if noalign (setq org-table-may-need-update c)))
  12696. (delete-char N))
  12697. (delete-char N)
  12698. (org-fix-tags-on-the-fly)))
  12699. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12700. (put 'org-self-insert-command 'delete-selection t)
  12701. (put 'orgtbl-self-insert-command 'delete-selection t)
  12702. (put 'org-delete-char 'delete-selection 'supersede)
  12703. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12704. (put 'org-yank 'delete-selection 'yank)
  12705. ;; Make `flyspell-mode' delay after some commands
  12706. (put 'org-self-insert-command 'flyspell-delayed t)
  12707. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12708. (put 'org-delete-char 'flyspell-delayed t)
  12709. (put 'org-delete-backward-char 'flyspell-delayed t)
  12710. ;; Make pabbrev-mode expand after org-mode commands
  12711. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12712. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12713. ;; How to do this: Measure non-white length of current string
  12714. ;; If equal to column width, we should realign.
  12715. (defun org-remap (map &rest commands)
  12716. "In MAP, remap the functions given in COMMANDS.
  12717. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12718. (let (new old)
  12719. (while commands
  12720. (setq old (pop commands) new (pop commands))
  12721. (if (fboundp 'command-remapping)
  12722. (org-defkey map (vector 'remap old) new)
  12723. (substitute-key-definition old new map global-map)))))
  12724. (when (eq org-enable-table-editor 'optimized)
  12725. ;; If the user wants maximum table support, we need to hijack
  12726. ;; some standard editing functions
  12727. (org-remap org-mode-map
  12728. 'self-insert-command 'org-self-insert-command
  12729. 'delete-char 'org-delete-char
  12730. 'delete-backward-char 'org-delete-backward-char)
  12731. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12732. (defvar org-ctrl-c-ctrl-c-hook nil
  12733. "Hook for functions attaching themselves to `C-c C-c'.
  12734. This can be used to add additional functionality to the C-c C-c key which
  12735. executes context-dependent commands.
  12736. Each function will be called with no arguments. The function must check
  12737. if the context is appropriate for it to act. If yes, it should do its
  12738. thing and then return a non-nil value. If the context is wrong,
  12739. just do nothing.")
  12740. (defvar org-metaleft-hook nil
  12741. "Hook for functions attaching themselves to `M-left'.
  12742. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12743. (defvar org-metaright-hook nil
  12744. "Hook for functions attaching themselves to `M-right'.
  12745. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12746. (defvar org-metaup-hook nil
  12747. "Hook for functions attaching themselves to `M-up'.
  12748. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12749. (defvar org-metadown-hook nil
  12750. "Hook for functions attaching themselves to `M-down'.
  12751. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12752. (defvar org-shiftmetaleft-hook nil
  12753. "Hook for functions attaching themselves to `M-S-left'.
  12754. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12755. (defvar org-shiftmetaright-hook nil
  12756. "Hook for functions attaching themselves to `M-S-right'.
  12757. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12758. (defvar org-shiftmetaup-hook nil
  12759. "Hook for functions attaching themselves to `M-S-up'.
  12760. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12761. (defvar org-shiftmetadown-hook nil
  12762. "Hook for functions attaching themselves to `M-S-down'.
  12763. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12764. (defvar org-metareturn-hook nil
  12765. "Hook for functions attaching themselves to `M-RET'.
  12766. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12767. (defun org-modifier-cursor-error ()
  12768. "Throw an error, a modified cursor command was applied in wrong context."
  12769. (error "This command is active in special context like tables, headlines or items"))
  12770. (defun org-shiftselect-error ()
  12771. "Throw an error because Shift-Cursor command was applied in wrong context."
  12772. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12773. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12774. (error "This command works only in special context like headlines or timestamps.")))
  12775. (defun org-call-for-shift-select (cmd)
  12776. (let ((this-command-keys-shift-translated t))
  12777. (call-interactively cmd)))
  12778. (defun org-shifttab (&optional arg)
  12779. "Global visibility cycling or move to previous table field.
  12780. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12781. on context.
  12782. See the individual commands for more information."
  12783. (interactive "P")
  12784. (cond
  12785. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12786. ((integerp arg)
  12787. (message "Content view to level: %d" arg)
  12788. (org-content (prefix-numeric-value arg))
  12789. (setq org-cycle-global-status 'overview))
  12790. (t (call-interactively 'org-global-cycle))))
  12791. (defun org-shiftmetaleft ()
  12792. "Promote subtree or delete table column.
  12793. Calls `org-promote-subtree', `org-outdent-item',
  12794. or `org-table-delete-column', depending on context.
  12795. See the individual commands for more information."
  12796. (interactive)
  12797. (cond
  12798. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12799. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12800. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12801. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12802. (t (org-modifier-cursor-error))))
  12803. (defun org-shiftmetaright ()
  12804. "Demote subtree or insert table column.
  12805. Calls `org-demote-subtree', `org-indent-item',
  12806. or `org-table-insert-column', depending on context.
  12807. See the individual commands for more information."
  12808. (interactive)
  12809. (cond
  12810. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12811. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12812. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12813. ((org-at-item-p) (call-interactively 'org-indent-item))
  12814. (t (org-modifier-cursor-error))))
  12815. (defun org-shiftmetaup (&optional arg)
  12816. "Move subtree up or kill table row.
  12817. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12818. `org-move-item-up' depending on context. See the individual commands
  12819. for more information."
  12820. (interactive "P")
  12821. (cond
  12822. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12823. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12824. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12825. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12826. (t (org-modifier-cursor-error))))
  12827. (defun org-shiftmetadown (&optional arg)
  12828. "Move subtree down or insert table row.
  12829. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12830. `org-move-item-down', depending on context. See the individual
  12831. commands for more information."
  12832. (interactive "P")
  12833. (cond
  12834. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12835. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12836. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12837. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12838. (t (org-modifier-cursor-error))))
  12839. (defun org-metaleft (&optional arg)
  12840. "Promote heading or move table column to left.
  12841. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12842. With no specific context, calls the Emacs default `backward-word'.
  12843. See the individual commands for more information."
  12844. (interactive "P")
  12845. (cond
  12846. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12847. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12848. ((or (org-on-heading-p)
  12849. (and (org-region-active-p)
  12850. (save-excursion
  12851. (goto-char (region-beginning))
  12852. (org-on-heading-p))))
  12853. (call-interactively 'org-do-promote))
  12854. ((or (org-at-item-p)
  12855. (and (org-region-active-p)
  12856. (save-excursion
  12857. (goto-char (region-beginning))
  12858. (org-at-item-p))))
  12859. (call-interactively 'org-outdent-item))
  12860. (t (call-interactively 'backward-word))))
  12861. (defun org-metaright (&optional arg)
  12862. "Demote subtree or move table column to right.
  12863. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12864. With no specific context, calls the Emacs default `forward-word'.
  12865. See the individual commands for more information."
  12866. (interactive "P")
  12867. (cond
  12868. ((run-hook-with-args-until-success 'org-metaright-hook))
  12869. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12870. ((or (org-on-heading-p)
  12871. (and (org-region-active-p)
  12872. (save-excursion
  12873. (goto-char (region-beginning))
  12874. (org-on-heading-p))))
  12875. (call-interactively 'org-do-demote))
  12876. ((or (org-at-item-p)
  12877. (and (org-region-active-p)
  12878. (save-excursion
  12879. (goto-char (region-beginning))
  12880. (org-at-item-p))))
  12881. (call-interactively 'org-indent-item))
  12882. (t (call-interactively 'forward-word))))
  12883. (defun org-metaup (&optional arg)
  12884. "Move subtree up or move table row up.
  12885. Calls `org-move-subtree-up' or `org-table-move-row' or
  12886. `org-move-item-up', depending on context. See the individual commands
  12887. for more information."
  12888. (interactive "P")
  12889. (cond
  12890. ((run-hook-with-args-until-success 'org-metaup-hook))
  12891. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12892. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12893. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12894. (t (transpose-lines 1) (beginning-of-line -1))))
  12895. (defun org-metadown (&optional arg)
  12896. "Move subtree down or move table row down.
  12897. Calls `org-move-subtree-down' or `org-table-move-row' or
  12898. `org-move-item-down', depending on context. See the individual
  12899. commands for more information."
  12900. (interactive "P")
  12901. (cond
  12902. ((run-hook-with-args-until-success 'org-metadown-hook))
  12903. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12904. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12905. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12906. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12907. (defun org-shiftup (&optional arg)
  12908. "Increase item in timestamp or increase priority of current headline.
  12909. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12910. depending on context. See the individual commands for more information."
  12911. (interactive "P")
  12912. (cond
  12913. ((and org-support-shift-select (org-region-active-p))
  12914. (org-call-for-shift-select 'previous-line))
  12915. ((org-at-timestamp-p t)
  12916. (call-interactively (if org-edit-timestamp-down-means-later
  12917. 'org-timestamp-down 'org-timestamp-up)))
  12918. ((and (not (eq org-support-shift-select 'always))
  12919. (org-on-heading-p))
  12920. (call-interactively 'org-priority-up))
  12921. ((and (not org-support-shift-select) (org-at-item-p))
  12922. (call-interactively 'org-previous-item))
  12923. ((org-clocktable-try-shift 'up arg))
  12924. (org-support-shift-select
  12925. (org-call-for-shift-select 'previous-line))
  12926. (t (org-shiftselect-error))))
  12927. (defun org-shiftdown (&optional arg)
  12928. "Decrease item in timestamp or decrease priority of current headline.
  12929. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12930. depending on context. See the individual commands for more information."
  12931. (interactive "P")
  12932. (cond
  12933. ((and org-support-shift-select (org-region-active-p))
  12934. (org-call-for-shift-select 'next-line))
  12935. ((org-at-timestamp-p t)
  12936. (call-interactively (if org-edit-timestamp-down-means-later
  12937. 'org-timestamp-up 'org-timestamp-down)))
  12938. ((and (not (eq org-support-shift-select 'always))
  12939. (org-on-heading-p))
  12940. (call-interactively 'org-priority-down))
  12941. ((and (not org-support-shift-select) (org-at-item-p))
  12942. (call-interactively 'org-next-item))
  12943. ((org-clocktable-try-shift 'down arg))
  12944. (org-support-shift-select
  12945. (org-call-for-shift-select 'next-line))
  12946. (t (org-shiftselect-error))))
  12947. (defun org-shiftright (&optional arg)
  12948. "Cycle the thing at point or in the current line, depending on context.
  12949. Depending on context, this does one of the following:
  12950. - switch a timestamp at point one day into the future
  12951. - on a headline, switch to the next TODO keyword.
  12952. - on an item, switch entire list to the next bullet type
  12953. - on a property line, switch to the next allowed value
  12954. - on a clocktable definition line, move time block into the future"
  12955. (interactive "P")
  12956. (cond
  12957. ((and org-support-shift-select (org-region-active-p))
  12958. (org-call-for-shift-select 'forward-char))
  12959. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12960. ((and (not (eq org-support-shift-select 'always))
  12961. (org-on-heading-p))
  12962. (org-call-with-arg 'org-todo 'right))
  12963. ((or (and org-support-shift-select
  12964. (not (eq org-support-shift-select 'always))
  12965. (org-at-item-bullet-p))
  12966. (and (not org-support-shift-select) (org-at-item-p)))
  12967. (org-call-with-arg 'org-cycle-list-bullet nil))
  12968. ((and (not (eq org-support-shift-select 'always))
  12969. (org-at-property-p))
  12970. (call-interactively 'org-property-next-allowed-value))
  12971. ((org-clocktable-try-shift 'right arg))
  12972. (org-support-shift-select
  12973. (org-call-for-shift-select 'forward-char))
  12974. (t (org-shiftselect-error))))
  12975. (defun org-shiftleft (&optional arg)
  12976. "Cycle the thing at point or in the current line, depending on context.
  12977. Depending on context, this does one of the following:
  12978. - switch a timestamp at point one day into the past
  12979. - on a headline, switch to the previous TODO keyword.
  12980. - on an item, switch entire list to the previous bullet type
  12981. - on a property line, switch to the previous allowed value
  12982. - on a clocktable definition line, move time block into the past"
  12983. (interactive "P")
  12984. (cond
  12985. ((and org-support-shift-select (org-region-active-p))
  12986. (org-call-for-shift-select 'backward-char))
  12987. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12988. ((and (not (eq org-support-shift-select 'always))
  12989. (org-on-heading-p))
  12990. (org-call-with-arg 'org-todo 'left))
  12991. ((or (and org-support-shift-select
  12992. (not (eq org-support-shift-select 'always))
  12993. (org-at-item-bullet-p))
  12994. (and (not org-support-shift-select) (org-at-item-p)))
  12995. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12996. ((and (not (eq org-support-shift-select 'always))
  12997. (org-at-property-p))
  12998. (call-interactively 'org-property-previous-allowed-value))
  12999. ((org-clocktable-try-shift 'left arg))
  13000. (org-support-shift-select
  13001. (org-call-for-shift-select 'backward-char))
  13002. (t (org-shiftselect-error))))
  13003. (defun org-shiftcontrolright ()
  13004. "Switch to next TODO set."
  13005. (interactive)
  13006. (cond
  13007. ((and org-support-shift-select (org-region-active-p))
  13008. (org-call-for-shift-select 'forward-word))
  13009. ((and (not (eq org-support-shift-select 'always))
  13010. (org-on-heading-p))
  13011. (org-call-with-arg 'org-todo 'nextset))
  13012. (org-support-shift-select
  13013. (org-call-for-shift-select 'forward-word))
  13014. (t (org-shiftselect-error))))
  13015. (defun org-shiftcontrolleft ()
  13016. "Switch to previous TODO set."
  13017. (interactive)
  13018. (cond
  13019. ((and org-support-shift-select (org-region-active-p))
  13020. (org-call-for-shift-select 'backward-word))
  13021. ((and (not (eq org-support-shift-select 'always))
  13022. (org-on-heading-p))
  13023. (org-call-with-arg 'org-todo 'previousset))
  13024. (org-support-shift-select
  13025. (org-call-for-shift-select 'backward-word))
  13026. (t (org-shiftselect-error))))
  13027. (defun org-ctrl-c-ret ()
  13028. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13029. (interactive)
  13030. (cond
  13031. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13032. (t (call-interactively 'org-insert-heading))))
  13033. (defun org-copy-special ()
  13034. "Copy region in table or copy current subtree.
  13035. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13036. See the individual commands for more information."
  13037. (interactive)
  13038. (call-interactively
  13039. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13040. (defun org-cut-special ()
  13041. "Cut region in table or cut current subtree.
  13042. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13043. See the individual commands for more information."
  13044. (interactive)
  13045. (call-interactively
  13046. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13047. (defun org-paste-special (arg)
  13048. "Paste rectangular region into table, or past subtree relative to level.
  13049. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13050. See the individual commands for more information."
  13051. (interactive "P")
  13052. (if (org-at-table-p)
  13053. (org-table-paste-rectangle)
  13054. (org-paste-subtree arg)))
  13055. (defun org-edit-special ()
  13056. "Call a special editor for the stuff at point.
  13057. When at a table, call the formula editor with `org-table-edit-formulas'.
  13058. When at the first line of an src example, call `org-edit-src-code'.
  13059. When in an #+include line, visit the include file. Otherwise call
  13060. `ffap' to visit the file at point."
  13061. (interactive)
  13062. (cond
  13063. ((org-at-table-p)
  13064. (call-interactively 'org-table-edit-formulas))
  13065. ((save-excursion
  13066. (beginning-of-line 1)
  13067. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13068. (find-file (org-trim (match-string 1))))
  13069. ((org-edit-src-code))
  13070. ((org-edit-fixed-width-region))
  13071. (t (call-interactively 'ffap))))
  13072. (defun org-ctrl-c-ctrl-c (&optional arg)
  13073. "Set tags in headline, or update according to changed information at point.
  13074. This command does many different things, depending on context:
  13075. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13076. this is what we do.
  13077. - If the cursor is in a headline, prompt for tags and insert them
  13078. into the current line, aligned to `org-tags-column'. When called
  13079. with prefix arg, realign all tags in the current buffer.
  13080. - If the cursor is in one of the special #+KEYWORD lines, this
  13081. triggers scanning the buffer for these lines and updating the
  13082. information.
  13083. - If the cursor is inside a table, realign the table. This command
  13084. works even if the automatic table editor has been turned off.
  13085. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13086. the entire table.
  13087. - If the cursor is at a footnote reference or definition, jump to
  13088. the corresponding definition or references, respectively.
  13089. - If the cursor is a the beginning of a dynamic block, update it.
  13090. - If the cursor is inside a table created by the table.el package,
  13091. activate that table.
  13092. - If the current buffer is a remember buffer, close note and file
  13093. it. A prefix argument of 1 files to the default location
  13094. without further interaction. A prefix argument of 2 files to
  13095. the currently clocking task.
  13096. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13097. links in this buffer.
  13098. - If the cursor is on a numbered item in a plain list, renumber the
  13099. ordered list.
  13100. - If the cursor is on a checkbox, toggle it."
  13101. (interactive "P")
  13102. (let ((org-enable-table-editor t))
  13103. (cond
  13104. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13105. org-occur-highlights
  13106. org-latex-fragment-image-overlays)
  13107. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13108. (org-remove-occur-highlights)
  13109. (org-remove-latex-fragment-image-overlays)
  13110. (message "Temporary highlights/overlays removed from current buffer"))
  13111. ((and (local-variable-p 'org-finish-function (current-buffer))
  13112. (fboundp org-finish-function))
  13113. (funcall org-finish-function))
  13114. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13115. ((org-at-property-p)
  13116. (call-interactively 'org-property-action))
  13117. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13118. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13119. ((org-at-table.el-p)
  13120. (require 'table)
  13121. (beginning-of-line 1)
  13122. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13123. (call-interactively 'table-recognize-table))
  13124. ((org-at-table-p)
  13125. (org-table-maybe-eval-formula)
  13126. (if arg
  13127. (call-interactively 'org-table-recalculate)
  13128. (org-table-maybe-recalculate-line))
  13129. (call-interactively 'org-table-align))
  13130. ((or (org-footnote-at-reference-p)
  13131. (org-footnote-at-definition-p))
  13132. (call-interactively 'org-footnote-action))
  13133. ((org-at-item-checkbox-p)
  13134. (call-interactively 'org-toggle-checkbox))
  13135. ((org-at-item-p)
  13136. (if arg
  13137. (call-interactively 'org-toggle-checkbox)
  13138. (call-interactively 'org-maybe-renumber-ordered-list)))
  13139. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13140. ;; Dynamic block
  13141. (beginning-of-line 1)
  13142. (save-excursion (org-update-dblock)))
  13143. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13144. (cond
  13145. ((equal (match-string 1) "TBLFM")
  13146. ;; Recalculate the table before this line
  13147. (save-excursion
  13148. (beginning-of-line 1)
  13149. (skip-chars-backward " \r\n\t")
  13150. (if (org-at-table-p)
  13151. (org-call-with-arg 'org-table-recalculate t))))
  13152. (t
  13153. ; (org-set-regexps-and-options)
  13154. ; (org-restart-font-lock)
  13155. (let ((org-inhibit-startup t)) (org-mode-restart))
  13156. (message "Local setup has been refreshed"))))
  13157. (t (error "C-c C-c can do nothing useful at this location.")))))
  13158. (defun org-mode-restart ()
  13159. "Restart Org-mode, to scan again for special lines.
  13160. Also updates the keyword regular expressions."
  13161. (interactive)
  13162. (org-mode)
  13163. (message "Org-mode restarted"))
  13164. (defun org-kill-note-or-show-branches ()
  13165. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13166. (interactive)
  13167. (if (not org-finish-function)
  13168. (call-interactively 'show-branches)
  13169. (let ((org-note-abort t))
  13170. (funcall org-finish-function))))
  13171. (defun org-return (&optional indent)
  13172. "Goto next table row or insert a newline.
  13173. Calls `org-table-next-row' or `newline', depending on context.
  13174. See the individual commands for more information."
  13175. (interactive)
  13176. (cond
  13177. ((bobp) (if indent (newline-and-indent) (newline)))
  13178. ((org-at-table-p)
  13179. (org-table-justify-field-maybe)
  13180. (call-interactively 'org-table-next-row))
  13181. ((and org-return-follows-link
  13182. (eq (get-text-property (point) 'face) 'org-link))
  13183. (call-interactively 'org-open-at-point))
  13184. ((and (org-at-heading-p)
  13185. (looking-at
  13186. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13187. (org-show-entry)
  13188. (end-of-line 1)
  13189. (newline))
  13190. (t (if indent (newline-and-indent) (newline)))))
  13191. (defun org-return-indent ()
  13192. "Goto next table row or insert a newline and indent.
  13193. Calls `org-table-next-row' or `newline-and-indent', depending on
  13194. context. See the individual commands for more information."
  13195. (interactive)
  13196. (org-return t))
  13197. (defun org-ctrl-c-star ()
  13198. "Compute table, or change heading status of lines.
  13199. Calls `org-table-recalculate' or `org-toggle-heading',
  13200. depending on context."
  13201. (interactive)
  13202. (cond
  13203. ((org-at-table-p)
  13204. (call-interactively 'org-table-recalculate))
  13205. (t
  13206. ;; Convert all lines in region to list items
  13207. (call-interactively 'org-toggle-heading))))
  13208. (defun org-ctrl-c-minus ()
  13209. "Insert separator line in table or modify bullet status of line.
  13210. Also turns a plain line or a region of lines into list items.
  13211. Calls `org-table-insert-hline', `org-toggle-item', or
  13212. `org-cycle-list-bullet', depending on context."
  13213. (interactive)
  13214. (cond
  13215. ((org-at-table-p)
  13216. (call-interactively 'org-table-insert-hline))
  13217. ((org-region-active-p)
  13218. (call-interactively 'org-toggle-item))
  13219. ((org-in-item-p)
  13220. (call-interactively 'org-cycle-list-bullet))
  13221. (t
  13222. (call-interactively 'org-toggle-item))))
  13223. (defun org-toggle-item ()
  13224. "Convert headings or normal lines to items, items to normal lines.
  13225. If there is no active region, only the current line is considered.
  13226. If the first line in the region is a headline, convert all headlines to items.
  13227. If the first line in the region is an item, convert all items to normal lines.
  13228. If the first line is normal text, add an item bullet to each line."
  13229. (interactive)
  13230. (let (l2 l beg end)
  13231. (if (org-region-active-p)
  13232. (setq beg (region-beginning) end (region-end))
  13233. (setq beg (point-at-bol)
  13234. end (min (1+ (point-at-eol)) (point-max))))
  13235. (save-excursion
  13236. (goto-char end)
  13237. (setq l2 (org-current-line))
  13238. (goto-char beg)
  13239. (beginning-of-line 1)
  13240. (setq l (1- (org-current-line)))
  13241. (if (org-at-item-p)
  13242. ;; We already have items, de-itemize
  13243. (while (< (setq l (1+ l)) l2)
  13244. (when (org-at-item-p)
  13245. (goto-char (match-beginning 2))
  13246. (delete-region (match-beginning 2) (match-end 2))
  13247. (and (looking-at "[ \t]+") (replace-match "")))
  13248. (beginning-of-line 2))
  13249. (if (org-on-heading-p)
  13250. ;; Headings, convert to items
  13251. (while (< (setq l (1+ l)) l2)
  13252. (if (looking-at org-outline-regexp)
  13253. (replace-match "- " t t))
  13254. (beginning-of-line 2))
  13255. ;; normal lines, turn them into items
  13256. (while (< (setq l (1+ l)) l2)
  13257. (unless (org-at-item-p)
  13258. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13259. (replace-match "\\1- \\2")))
  13260. (beginning-of-line 2)))))))
  13261. (defun org-toggle-heading (&optional nstars)
  13262. "Convert headings to normal text, or items or text to headings.
  13263. If there is no active region, only the current line is considered.
  13264. If the first line is a heading, remove the stars from all headlines
  13265. in the region.
  13266. If the first line is a plain list item, turn all plain list items into
  13267. headings.
  13268. If the first line is a normal line, turn each and every line in the region
  13269. into a heading.
  13270. When converting a line into a heading, the number of stars is chosen
  13271. such that the lines become children of the current entry. However, when
  13272. a prefix argument is given, its value determines the number of stars to add."
  13273. (interactive "P")
  13274. (let (l2 l itemp beg end)
  13275. (if (org-region-active-p)
  13276. (setq beg (region-beginning) end (region-end))
  13277. (setq beg (point-at-bol)
  13278. end (min (1+ (point-at-eol)) (point-max))))
  13279. (save-excursion
  13280. (goto-char end)
  13281. (setq l2 (org-current-line))
  13282. (goto-char beg)
  13283. (beginning-of-line 1)
  13284. (setq l (1- (org-current-line)))
  13285. (if (org-on-heading-p)
  13286. ;; We already have headlines, de-star them
  13287. (while (< (setq l (1+ l)) l2)
  13288. (when (org-on-heading-p t)
  13289. (and (looking-at outline-regexp) (replace-match "")))
  13290. (beginning-of-line 2))
  13291. (setq itemp (org-at-item-p))
  13292. (let* ((stars
  13293. (if nstars
  13294. (make-string (prefix-numeric-value current-prefix-arg)
  13295. ?*)
  13296. (save-excursion
  13297. (re-search-backward org-complex-heading-regexp nil t)
  13298. (or (match-string 1) ""))))
  13299. (add-stars (cond (nstars "")
  13300. ((equal stars "") "*")
  13301. (org-odd-levels-only "**")
  13302. (t "*")))
  13303. (rpl (concat stars add-stars " ")))
  13304. (while (< (setq l (1+ l)) l2)
  13305. (if itemp
  13306. (and (org-at-item-p) (replace-match rpl t t))
  13307. (unless (org-on-heading-p)
  13308. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13309. (replace-match (concat rpl (match-string 2))))))
  13310. (beginning-of-line 2)))))))
  13311. (defun org-meta-return (&optional arg)
  13312. "Insert a new heading or wrap a region in a table.
  13313. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13314. See the individual commands for more information."
  13315. (interactive "P")
  13316. (cond
  13317. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13318. ((org-at-table-p)
  13319. (call-interactively 'org-table-wrap-region))
  13320. (t (call-interactively 'org-insert-heading))))
  13321. ;;; Menu entries
  13322. ;; Define the Org-mode menus
  13323. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13324. '("Tbl"
  13325. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13326. ["Next Field" org-cycle (org-at-table-p)]
  13327. ["Previous Field" org-shifttab (org-at-table-p)]
  13328. ["Next Row" org-return (org-at-table-p)]
  13329. "--"
  13330. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13331. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13332. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13333. "--"
  13334. ("Column"
  13335. ["Move Column Left" org-metaleft (org-at-table-p)]
  13336. ["Move Column Right" org-metaright (org-at-table-p)]
  13337. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13338. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13339. ("Row"
  13340. ["Move Row Up" org-metaup (org-at-table-p)]
  13341. ["Move Row Down" org-metadown (org-at-table-p)]
  13342. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13343. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13344. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13345. "--"
  13346. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13347. ("Rectangle"
  13348. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13349. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13350. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13351. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13352. "--"
  13353. ("Calculate"
  13354. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13355. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13356. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13357. "--"
  13358. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13359. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13360. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13361. "--"
  13362. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13363. "--"
  13364. ["Sum Column/Rectangle" org-table-sum
  13365. (or (org-at-table-p) (org-region-active-p))]
  13366. ["Which Column?" org-table-current-column (org-at-table-p)])
  13367. ["Debug Formulas"
  13368. org-table-toggle-formula-debugger
  13369. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13370. ["Show Col/Row Numbers"
  13371. org-table-toggle-coordinate-overlays
  13372. :style toggle
  13373. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13374. "--"
  13375. ["Create" org-table-create (and (not (org-at-table-p))
  13376. org-enable-table-editor)]
  13377. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13378. ["Import from File" org-table-import (not (org-at-table-p))]
  13379. ["Export to File" org-table-export (org-at-table-p)]
  13380. "--"
  13381. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13382. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13383. '("Org"
  13384. ("Show/Hide"
  13385. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13386. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13387. ["Sparse Tree..." org-sparse-tree t]
  13388. ["Reveal Context" org-reveal t]
  13389. ["Show All" show-all t]
  13390. "--"
  13391. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13392. "--"
  13393. ["New Heading" org-insert-heading t]
  13394. ("Navigate Headings"
  13395. ["Up" outline-up-heading t]
  13396. ["Next" outline-next-visible-heading t]
  13397. ["Previous" outline-previous-visible-heading t]
  13398. ["Next Same Level" outline-forward-same-level t]
  13399. ["Previous Same Level" outline-backward-same-level t]
  13400. "--"
  13401. ["Jump" org-goto t])
  13402. ("Edit Structure"
  13403. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13404. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13405. "--"
  13406. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13407. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13408. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13409. "--"
  13410. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13411. "--"
  13412. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13413. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13414. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13415. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13416. "--"
  13417. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13418. "--"
  13419. ["Convert to odd levels" org-convert-to-odd-levels t]
  13420. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13421. ("Editing"
  13422. ["Emphasis..." org-emphasize t]
  13423. ["Edit Source Example" org-edit-special t]
  13424. "--"
  13425. ["Footnote new/jump" org-footnote-action t]
  13426. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13427. ("Archive"
  13428. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13429. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13430. ; :active t :keys "C-u C-c C-x C-a"]
  13431. ["Sparse trees open ARCHIVE trees"
  13432. (setq org-sparse-tree-open-archived-trees
  13433. (not org-sparse-tree-open-archived-trees))
  13434. :style toggle :selected org-sparse-tree-open-archived-trees]
  13435. ["Cycling opens ARCHIVE trees"
  13436. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13437. :style toggle :selected org-cycle-open-archived-trees]
  13438. "--"
  13439. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13440. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13441. ; ["Check and Move Children" (org-archive-subtree '(4))
  13442. ; :active t :keys "C-u C-c C-x C-s"]
  13443. )
  13444. "--"
  13445. ("Hyperlinks"
  13446. ["Store Link (Global)" org-store-link t]
  13447. ["Insert Link" org-insert-link t]
  13448. ["Follow Link" org-open-at-point t]
  13449. "--"
  13450. ["Next link" org-next-link t]
  13451. ["Previous link" org-previous-link t]
  13452. "--"
  13453. ["Descriptive Links"
  13454. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13455. :style radio
  13456. :selected (member '(org-link) buffer-invisibility-spec)]
  13457. ["Literal Links"
  13458. (progn
  13459. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13460. :style radio
  13461. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13462. "--"
  13463. ("TODO Lists"
  13464. ["TODO/DONE/-" org-todo t]
  13465. ("Select keyword"
  13466. ["Next keyword" org-shiftright (org-on-heading-p)]
  13467. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13468. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13469. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13470. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13471. ["Show TODO Tree" org-show-todo-tree t]
  13472. ["Global TODO list" org-todo-list t]
  13473. "--"
  13474. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13475. :selected org-enforce-todo-dependencies :style toggle :active t]
  13476. "Settings for tree at point"
  13477. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13478. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13479. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13480. ["Do Children parallel" org-toggle-ordered-property :style radio
  13481. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13482. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13483. "--"
  13484. ["Set Priority" org-priority t]
  13485. ["Priority Up" org-shiftup t]
  13486. ["Priority Down" org-shiftdown t]
  13487. "--"
  13488. ["Get news from all feeds" org-feed-update-all t]
  13489. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13490. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13491. ("TAGS and Properties"
  13492. ["Set Tags" org-set-tags-command t]
  13493. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13494. "--"
  13495. ["Set property" org-set-property t]
  13496. ["Column view of properties" org-columns t]
  13497. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13498. ("Dates and Scheduling"
  13499. ["Timestamp" org-time-stamp t]
  13500. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13501. ("Change Date"
  13502. ["1 Day Later" org-shiftright t]
  13503. ["1 Day Earlier" org-shiftleft t]
  13504. ["1 ... Later" org-shiftup t]
  13505. ["1 ... Earlier" org-shiftdown t])
  13506. ["Compute Time Range" org-evaluate-time-range t]
  13507. ["Schedule Item" org-schedule t]
  13508. ["Deadline" org-deadline t]
  13509. "--"
  13510. ["Custom time format" org-toggle-time-stamp-overlays
  13511. :style radio :selected org-display-custom-times]
  13512. "--"
  13513. ["Goto Calendar" org-goto-calendar t]
  13514. ["Date from Calendar" org-date-from-calendar t]
  13515. "--"
  13516. ["Start/Restart Timer" org-timer-start t]
  13517. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13518. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13519. ["Insert Timer String" org-timer t]
  13520. ["Insert Timer Item" org-timer-item t])
  13521. ("Logging work"
  13522. ["Clock in" org-clock-in t]
  13523. ["Clock out" org-clock-out t]
  13524. ["Clock cancel" org-clock-cancel t]
  13525. ["Goto running clock" org-clock-goto t]
  13526. ["Display times" org-clock-display t]
  13527. ["Create clock table" org-clock-report t]
  13528. "--"
  13529. ["Record DONE time"
  13530. (progn (setq org-log-done (not org-log-done))
  13531. (message "Switching to %s will %s record a timestamp"
  13532. (car org-done-keywords)
  13533. (if org-log-done "automatically" "not")))
  13534. :style toggle :selected org-log-done])
  13535. "--"
  13536. ["Agenda Command..." org-agenda t]
  13537. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13538. ("File List for Agenda")
  13539. ("Special views current file"
  13540. ["TODO Tree" org-show-todo-tree t]
  13541. ["Check Deadlines" org-check-deadlines t]
  13542. ["Timeline" org-timeline t]
  13543. ["Tags/Property tree" org-match-sparse-tree t])
  13544. "--"
  13545. ["Export/Publish..." org-export t]
  13546. ("LaTeX"
  13547. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13548. :selected org-cdlatex-mode]
  13549. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13550. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13551. ["Modify math symbol" org-cdlatex-math-modify
  13552. (org-inside-LaTeX-fragment-p)]
  13553. ["Insert citation" org-reftex-citation t]
  13554. "--"
  13555. ["Export LaTeX fragments as images"
  13556. (if (featurep 'org-exp)
  13557. (setq org-export-with-LaTeX-fragments
  13558. (not org-export-with-LaTeX-fragments))
  13559. (require 'org-exp))
  13560. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13561. org-export-with-LaTeX-fragments)])
  13562. "--"
  13563. ("Documentation"
  13564. ["Show Version" org-version t]
  13565. ["Info Documentation" org-info t])
  13566. ("Customize"
  13567. ["Browse Org Group" org-customize t]
  13568. "--"
  13569. ["Expand This Menu" org-create-customize-menu
  13570. (fboundp 'customize-menu-create)])
  13571. "--"
  13572. ("Refresh/Reload"
  13573. ["Refresh setup current buffer" org-mode-restart t]
  13574. ["Reload Org (after update)" org-reload t]
  13575. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13576. ))
  13577. (defun org-info (&optional node)
  13578. "Read documentation for Org-mode in the info system.
  13579. With optional NODE, go directly to that node."
  13580. (interactive)
  13581. (info (format "(org)%s" (or node ""))))
  13582. (defun org-install-agenda-files-menu ()
  13583. (let ((bl (buffer-list)))
  13584. (save-excursion
  13585. (while bl
  13586. (set-buffer (pop bl))
  13587. (if (org-mode-p) (setq bl nil)))
  13588. (when (org-mode-p)
  13589. (easy-menu-change
  13590. '("Org") "File List for Agenda"
  13591. (append
  13592. (list
  13593. ["Edit File List" (org-edit-agenda-file-list) t]
  13594. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13595. ["Remove Current File from List" org-remove-file t]
  13596. ["Cycle through agenda files" org-cycle-agenda-files t]
  13597. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13598. "--")
  13599. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13600. ;;;; Documentation
  13601. ;;;###autoload
  13602. (defun org-require-autoloaded-modules ()
  13603. (interactive)
  13604. (mapc 'require
  13605. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13606. org-docbook org-exp org-html org-icalendar
  13607. org-id org-latex
  13608. org-publish org-remember org-table
  13609. org-timer org-xoxo)))
  13610. ;;;###autoload
  13611. (defun org-reload (&optional uncompiled)
  13612. "Reload all org lisp files.
  13613. With prefix arg UNCOMPILED, load the uncompiled versions."
  13614. (interactive "P")
  13615. (require 'find-func)
  13616. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13617. (dir-org (file-name-directory (org-find-library-name "org")))
  13618. (dir-org-contrib (ignore-errors
  13619. (file-name-directory
  13620. (org-find-library-name "org-contribdir"))))
  13621. (files
  13622. (append (directory-files dir-org t file-re)
  13623. (and dir-org-contrib
  13624. (directory-files dir-org-contrib t file-re))))
  13625. (remove-re (concat (if (featurep 'xemacs)
  13626. "org-colview" "org-colview-xemacs")
  13627. "\\'")))
  13628. (setq files (mapcar 'file-name-sans-extension files))
  13629. (setq files (mapcar
  13630. (lambda (x) (if (string-match remove-re x) nil x))
  13631. files))
  13632. (setq files (delq nil files))
  13633. (mapc
  13634. (lambda (f)
  13635. (when (featurep (intern (file-name-nondirectory f)))
  13636. (if (and (not uncompiled)
  13637. (file-exists-p (concat f ".elc")))
  13638. (load (concat f ".elc") nil nil t)
  13639. (load (concat f ".el") nil nil t))))
  13640. files))
  13641. (org-version))
  13642. ;;;###autoload
  13643. (defun org-customize ()
  13644. "Call the customize function with org as argument."
  13645. (interactive)
  13646. (org-load-modules-maybe)
  13647. (org-require-autoloaded-modules)
  13648. (customize-browse 'org))
  13649. (defun org-create-customize-menu ()
  13650. "Create a full customization menu for Org-mode, insert it into the menu."
  13651. (interactive)
  13652. (org-load-modules-maybe)
  13653. (org-require-autoloaded-modules)
  13654. (if (fboundp 'customize-menu-create)
  13655. (progn
  13656. (easy-menu-change
  13657. '("Org") "Customize"
  13658. `(["Browse Org group" org-customize t]
  13659. "--"
  13660. ,(customize-menu-create 'org)
  13661. ["Set" Custom-set t]
  13662. ["Save" Custom-save t]
  13663. ["Reset to Current" Custom-reset-current t]
  13664. ["Reset to Saved" Custom-reset-saved t]
  13665. ["Reset to Standard Settings" Custom-reset-standard t]))
  13666. (message "\"Org\"-menu now contains full customization menu"))
  13667. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13668. ;;;; Miscellaneous stuff
  13669. ;;; Generally useful functions
  13670. (defun org-find-text-property-in-string (prop s)
  13671. "Return the first non-nil value of property PROP in string S."
  13672. (or (get-text-property 0 prop s)
  13673. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13674. prop s)))
  13675. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13676. "Display the given MESSAGE as a warning."
  13677. (if (fboundp 'display-warning)
  13678. (display-warning 'org message
  13679. (if (featurep 'xemacs)
  13680. 'warning
  13681. :warning))
  13682. (let ((buf (get-buffer-create "*Org warnings*")))
  13683. (with-current-buffer buf
  13684. (goto-char (point-max))
  13685. (insert "Warning (Org): " message)
  13686. (unless (bolp)
  13687. (newline)))
  13688. (display-buffer buf)
  13689. (sit-for 0))))
  13690. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13691. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13692. (if (and marker (marker-buffer marker)
  13693. (buffer-live-p (marker-buffer marker)))
  13694. (progn
  13695. (switch-to-buffer (marker-buffer marker))
  13696. (if (or (> marker (point-max)) (< marker (point-min)))
  13697. (widen))
  13698. (goto-char marker)
  13699. (org-show-context 'org-goto))
  13700. (if bookmark
  13701. (bookmark-jump bookmark)
  13702. (error "Cannot find location"))))
  13703. (defun org-quote-csv-field (s)
  13704. "Quote field for inclusion in CSV material."
  13705. (if (string-match "[\",]" s)
  13706. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13707. s))
  13708. (defun org-plist-delete (plist property)
  13709. "Delete PROPERTY from PLIST.
  13710. This is in contrast to merely setting it to 0."
  13711. (let (p)
  13712. (while plist
  13713. (if (not (eq property (car plist)))
  13714. (setq p (plist-put p (car plist) (nth 1 plist))))
  13715. (setq plist (cddr plist)))
  13716. p))
  13717. (defun org-force-self-insert (N)
  13718. "Needed to enforce self-insert under remapping."
  13719. (interactive "p")
  13720. (self-insert-command N))
  13721. (defun org-string-width (s)
  13722. "Compute width of string, ignoring invisible characters.
  13723. This ignores character with invisibility property `org-link', and also
  13724. characters with property `org-cwidth', because these will become invisible
  13725. upon the next fontification round."
  13726. (let (b l)
  13727. (when (or (eq t buffer-invisibility-spec)
  13728. (assq 'org-link buffer-invisibility-spec))
  13729. (while (setq b (text-property-any 0 (length s)
  13730. 'invisible 'org-link s))
  13731. (setq s (concat (substring s 0 b)
  13732. (substring s (or (next-single-property-change
  13733. b 'invisible s) (length s)))))))
  13734. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13735. (setq s (concat (substring s 0 b)
  13736. (substring s (or (next-single-property-change
  13737. b 'org-cwidth s) (length s))))))
  13738. (setq l (string-width s) b -1)
  13739. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13740. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13741. l))
  13742. (defun org-get-indentation (&optional line)
  13743. "Get the indentation of the current line, interpreting tabs.
  13744. When LINE is given, assume it represents a line and compute its indentation."
  13745. (if line
  13746. (if (string-match "^ *" (org-remove-tabs line))
  13747. (match-end 0))
  13748. (save-excursion
  13749. (beginning-of-line 1)
  13750. (skip-chars-forward " \t")
  13751. (current-column))))
  13752. (defun org-remove-tabs (s &optional width)
  13753. "Replace tabulators in S with spaces.
  13754. Assumes that s is a single line, starting in column 0."
  13755. (setq width (or width tab-width))
  13756. (while (string-match "\t" s)
  13757. (setq s (replace-match
  13758. (make-string
  13759. (- (* width (/ (+ (match-beginning 0) width) width))
  13760. (match-beginning 0)) ?\ )
  13761. t t s)))
  13762. s)
  13763. (defun org-fix-indentation (line ind)
  13764. "Fix indentation in LINE.
  13765. IND is a cons cell with target and minimum indentation.
  13766. If the current indentation in LINE is smaller than the minimum,
  13767. leave it alone. If it is larger than ind, set it to the target."
  13768. (let* ((l (org-remove-tabs line))
  13769. (i (org-get-indentation l))
  13770. (i1 (car ind)) (i2 (cdr ind)))
  13771. (if (>= i i2) (setq l (substring line i2)))
  13772. (if (> i1 0)
  13773. (concat (make-string i1 ?\ ) l)
  13774. l)))
  13775. (defun org-base-buffer (buffer)
  13776. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13777. (if (not buffer)
  13778. buffer
  13779. (or (buffer-base-buffer buffer)
  13780. buffer)))
  13781. (defun org-trim (s)
  13782. "Remove whitespace at beginning and end of string."
  13783. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13784. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13785. s)
  13786. (defun org-wrap (string &optional width lines)
  13787. "Wrap string to either a number of lines, or a width in characters.
  13788. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13789. that costs. If there is a word longer than WIDTH, the text is actually
  13790. wrapped to the length of that word.
  13791. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13792. many lines, whatever width that takes.
  13793. The return value is a list of lines, without newlines at the end."
  13794. (let* ((words (org-split-string string "[ \t\n]+"))
  13795. (maxword (apply 'max (mapcar 'org-string-width words)))
  13796. w ll)
  13797. (cond (width
  13798. (org-do-wrap words (max maxword width)))
  13799. (lines
  13800. (setq w maxword)
  13801. (setq ll (org-do-wrap words maxword))
  13802. (if (<= (length ll) lines)
  13803. ll
  13804. (setq ll words)
  13805. (while (> (length ll) lines)
  13806. (setq w (1+ w))
  13807. (setq ll (org-do-wrap words w)))
  13808. ll))
  13809. (t (error "Cannot wrap this")))))
  13810. (defun org-do-wrap (words width)
  13811. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13812. (let (lines line)
  13813. (while words
  13814. (setq line (pop words))
  13815. (while (and words (< (+ (length line) (length (car words))) width))
  13816. (setq line (concat line " " (pop words))))
  13817. (setq lines (push line lines)))
  13818. (nreverse lines)))
  13819. (defun org-split-string (string &optional separators)
  13820. "Splits STRING into substrings at SEPARATORS.
  13821. No empty strings are returned if there are matches at the beginning
  13822. and end of string."
  13823. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13824. (start 0)
  13825. notfirst
  13826. (list nil))
  13827. (while (and (string-match rexp string
  13828. (if (and notfirst
  13829. (= start (match-beginning 0))
  13830. (< start (length string)))
  13831. (1+ start) start))
  13832. (< (match-beginning 0) (length string)))
  13833. (setq notfirst t)
  13834. (or (eq (match-beginning 0) 0)
  13835. (and (eq (match-beginning 0) (match-end 0))
  13836. (eq (match-beginning 0) start))
  13837. (setq list
  13838. (cons (substring string start (match-beginning 0))
  13839. list)))
  13840. (setq start (match-end 0)))
  13841. (or (eq start (length string))
  13842. (setq list
  13843. (cons (substring string start)
  13844. list)))
  13845. (nreverse list)))
  13846. (defun org-uuidgen-p (s)
  13847. "Is S an ID created by UUIDGEN?"
  13848. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13849. (defun org-context ()
  13850. "Return a list of contexts of the current cursor position.
  13851. If several contexts apply, all are returned.
  13852. Each context entry is a list with a symbol naming the context, and
  13853. two positions indicating start and end of the context. Possible
  13854. contexts are:
  13855. :headline anywhere in a headline
  13856. :headline-stars on the leading stars in a headline
  13857. :todo-keyword on a TODO keyword (including DONE) in a headline
  13858. :tags on the TAGS in a headline
  13859. :priority on the priority cookie in a headline
  13860. :item on the first line of a plain list item
  13861. :item-bullet on the bullet/number of a plain list item
  13862. :checkbox on the checkbox in a plain list item
  13863. :table in an org-mode table
  13864. :table-special on a special filed in a table
  13865. :table-table in a table.el table
  13866. :link on a hyperlink
  13867. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13868. :target on a <<target>>
  13869. :radio-target on a <<<radio-target>>>
  13870. :latex-fragment on a LaTeX fragment
  13871. :latex-preview on a LaTeX fragment with overlayed preview image
  13872. This function expects the position to be visible because it uses font-lock
  13873. faces as a help to recognize the following contexts: :table-special, :link,
  13874. and :keyword."
  13875. (let* ((f (get-text-property (point) 'face))
  13876. (faces (if (listp f) f (list f)))
  13877. (p (point)) clist o)
  13878. ;; First the large context
  13879. (cond
  13880. ((org-on-heading-p t)
  13881. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13882. (when (progn
  13883. (beginning-of-line 1)
  13884. (looking-at org-todo-line-tags-regexp))
  13885. (push (org-point-in-group p 1 :headline-stars) clist)
  13886. (push (org-point-in-group p 2 :todo-keyword) clist)
  13887. (push (org-point-in-group p 4 :tags) clist))
  13888. (goto-char p)
  13889. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13890. (if (looking-at "\\[#[A-Z0-9]\\]")
  13891. (push (org-point-in-group p 0 :priority) clist)))
  13892. ((org-at-item-p)
  13893. (push (org-point-in-group p 2 :item-bullet) clist)
  13894. (push (list :item (point-at-bol)
  13895. (save-excursion (org-end-of-item) (point)))
  13896. clist)
  13897. (and (org-at-item-checkbox-p)
  13898. (push (org-point-in-group p 0 :checkbox) clist)))
  13899. ((org-at-table-p)
  13900. (push (list :table (org-table-begin) (org-table-end)) clist)
  13901. (if (memq 'org-formula faces)
  13902. (push (list :table-special
  13903. (previous-single-property-change p 'face)
  13904. (next-single-property-change p 'face)) clist)))
  13905. ((org-at-table-p 'any)
  13906. (push (list :table-table) clist)))
  13907. (goto-char p)
  13908. ;; Now the small context
  13909. (cond
  13910. ((org-at-timestamp-p)
  13911. (push (org-point-in-group p 0 :timestamp) clist))
  13912. ((memq 'org-link faces)
  13913. (push (list :link
  13914. (previous-single-property-change p 'face)
  13915. (next-single-property-change p 'face)) clist))
  13916. ((memq 'org-special-keyword faces)
  13917. (push (list :keyword
  13918. (previous-single-property-change p 'face)
  13919. (next-single-property-change p 'face)) clist))
  13920. ((org-on-target-p)
  13921. (push (org-point-in-group p 0 :target) clist)
  13922. (goto-char (1- (match-beginning 0)))
  13923. (if (looking-at org-radio-target-regexp)
  13924. (push (org-point-in-group p 0 :radio-target) clist))
  13925. (goto-char p))
  13926. ((setq o (car (delq nil
  13927. (mapcar
  13928. (lambda (x)
  13929. (if (memq x org-latex-fragment-image-overlays) x))
  13930. (org-overlays-at (point))))))
  13931. (push (list :latex-fragment
  13932. (org-overlay-start o) (org-overlay-end o)) clist)
  13933. (push (list :latex-preview
  13934. (org-overlay-start o) (org-overlay-end o)) clist))
  13935. ((org-inside-LaTeX-fragment-p)
  13936. ;; FIXME: positions wrong.
  13937. (push (list :latex-fragment (point) (point)) clist)))
  13938. (setq clist (nreverse (delq nil clist)))
  13939. clist))
  13940. ;; FIXME: Compare with at-regexp-p Do we need both?
  13941. (defun org-in-regexp (re &optional nlines visually)
  13942. "Check if point is inside a match of regexp.
  13943. Normally only the current line is checked, but you can include NLINES extra
  13944. lines both before and after point into the search.
  13945. If VISUALLY is set, require that the cursor is not after the match but
  13946. really on, so that the block visually is on the match."
  13947. (catch 'exit
  13948. (let ((pos (point))
  13949. (eol (point-at-eol (+ 1 (or nlines 0))))
  13950. (inc (if visually 1 0)))
  13951. (save-excursion
  13952. (beginning-of-line (- 1 (or nlines 0)))
  13953. (while (re-search-forward re eol t)
  13954. (if (and (<= (match-beginning 0) pos)
  13955. (>= (+ inc (match-end 0)) pos))
  13956. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13957. (defun org-at-regexp-p (regexp)
  13958. "Is point inside a match of REGEXP in the current line?"
  13959. (catch 'exit
  13960. (save-excursion
  13961. (let ((pos (point)) (end (point-at-eol)))
  13962. (beginning-of-line 1)
  13963. (while (re-search-forward regexp end t)
  13964. (if (and (<= (match-beginning 0) pos)
  13965. (>= (match-end 0) pos))
  13966. (throw 'exit t)))
  13967. nil))))
  13968. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13969. "Call `multi-occur' with buffers for all agenda files."
  13970. (interactive "sOrg-files matching: \np")
  13971. (let* ((files (org-agenda-files))
  13972. (tnames (mapcar 'file-truename files))
  13973. (extra org-agenda-text-search-extra-files)
  13974. f)
  13975. (when (eq (car extra) 'agenda-archives)
  13976. (setq extra (cdr extra))
  13977. (setq files (org-add-archive-files files)))
  13978. (while (setq f (pop extra))
  13979. (unless (member (file-truename f) tnames)
  13980. (add-to-list 'files f 'append)
  13981. (add-to-list 'tnames (file-truename f) 'append)))
  13982. (multi-occur
  13983. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13984. regexp)))
  13985. (if (boundp 'occur-mode-find-occurrence-hook)
  13986. ;; Emacs 23
  13987. (add-hook 'occur-mode-find-occurrence-hook
  13988. (lambda ()
  13989. (when (org-mode-p)
  13990. (org-reveal))))
  13991. ;; Emacs 22
  13992. (defadvice occur-mode-goto-occurrence
  13993. (after org-occur-reveal activate)
  13994. (and (org-mode-p) (org-reveal)))
  13995. (defadvice occur-mode-goto-occurrence-other-window
  13996. (after org-occur-reveal activate)
  13997. (and (org-mode-p) (org-reveal)))
  13998. (defadvice occur-mode-display-occurrence
  13999. (after org-occur-reveal activate)
  14000. (when (org-mode-p)
  14001. (let ((pos (occur-mode-find-occurrence)))
  14002. (with-current-buffer (marker-buffer pos)
  14003. (save-excursion
  14004. (goto-char pos)
  14005. (org-reveal)))))))
  14006. (defun org-uniquify (list)
  14007. "Remove duplicate elements from LIST."
  14008. (let (res)
  14009. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14010. res))
  14011. (defun org-delete-all (elts list)
  14012. "Remove all elements in ELTS from LIST."
  14013. (while elts
  14014. (setq list (delete (pop elts) list)))
  14015. list)
  14016. (defun org-back-over-empty-lines ()
  14017. "Move backwards over whitespace, to the beginning of the first empty line.
  14018. Returns the number of empty lines passed."
  14019. (let ((pos (point)))
  14020. (skip-chars-backward " \t\n\r")
  14021. (beginning-of-line 2)
  14022. (goto-char (min (point) pos))
  14023. (count-lines (point) pos)))
  14024. (defun org-skip-whitespace ()
  14025. (skip-chars-forward " \t\n\r"))
  14026. (defun org-point-in-group (point group &optional context)
  14027. "Check if POINT is in match-group GROUP.
  14028. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14029. match. If the match group does ot exist or point is not inside it,
  14030. return nil."
  14031. (and (match-beginning group)
  14032. (>= point (match-beginning group))
  14033. (<= point (match-end group))
  14034. (if context
  14035. (list context (match-beginning group) (match-end group))
  14036. t)))
  14037. (defun org-switch-to-buffer-other-window (&rest args)
  14038. "Switch to buffer in a second window on the current frame.
  14039. In particular, do not allow pop-up frames."
  14040. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14041. special-display-function)
  14042. (apply 'switch-to-buffer-other-window args)))
  14043. (defun org-combine-plists (&rest plists)
  14044. "Create a single property list from all plists in PLISTS.
  14045. The process starts by copying the first list, and then setting properties
  14046. from the other lists. Settings in the last list are the most significant
  14047. ones and overrule settings in the other lists."
  14048. (let ((rtn (copy-sequence (pop plists)))
  14049. p v ls)
  14050. (while plists
  14051. (setq ls (pop plists))
  14052. (while ls
  14053. (setq p (pop ls) v (pop ls))
  14054. (setq rtn (plist-put rtn p v))))
  14055. rtn))
  14056. (defun org-move-line-down (arg)
  14057. "Move the current line down. With prefix argument, move it past ARG lines."
  14058. (interactive "p")
  14059. (let ((col (current-column))
  14060. beg end pos)
  14061. (beginning-of-line 1) (setq beg (point))
  14062. (beginning-of-line 2) (setq end (point))
  14063. (beginning-of-line (+ 1 arg))
  14064. (setq pos (move-marker (make-marker) (point)))
  14065. (insert (delete-and-extract-region beg end))
  14066. (goto-char pos)
  14067. (org-move-to-column col)))
  14068. (defun org-move-line-up (arg)
  14069. "Move the current line up. With prefix argument, move it past ARG lines."
  14070. (interactive "p")
  14071. (let ((col (current-column))
  14072. beg end pos)
  14073. (beginning-of-line 1) (setq beg (point))
  14074. (beginning-of-line 2) (setq end (point))
  14075. (beginning-of-line (- arg))
  14076. (setq pos (move-marker (make-marker) (point)))
  14077. (insert (delete-and-extract-region beg end))
  14078. (goto-char pos)
  14079. (org-move-to-column col)))
  14080. (defun org-replace-escapes (string table)
  14081. "Replace %-escapes in STRING with values in TABLE.
  14082. TABLE is an association list with keys like \"%a\" and string values.
  14083. The sequences in STRING may contain normal field width and padding information,
  14084. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14085. so values can contain further %-escapes if they are define later in TABLE."
  14086. (let ((case-fold-search nil)
  14087. e re rpl)
  14088. (while (setq e (pop table))
  14089. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14090. (while (string-match re string)
  14091. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14092. (cdr e)))
  14093. (setq string (replace-match rpl t t string))))
  14094. string))
  14095. (defun org-sublist (list start end)
  14096. "Return a section of LIST, from START to END.
  14097. Counting starts at 1."
  14098. (let (rtn (c start))
  14099. (setq list (nthcdr (1- start) list))
  14100. (while (and list (<= c end))
  14101. (push (pop list) rtn)
  14102. (setq c (1+ c)))
  14103. (nreverse rtn)))
  14104. (defun org-find-base-buffer-visiting (file)
  14105. "Like `find-buffer-visiting' but alway return the base buffer and
  14106. not an indirect buffer."
  14107. (let ((buf (find-buffer-visiting file)))
  14108. (if buf
  14109. (or (buffer-base-buffer buf) buf)
  14110. nil)))
  14111. (defun org-image-file-name-regexp (&optional extensions)
  14112. "Return regexp matching the file names of images.
  14113. If EXTENSIONS is given, only match these."
  14114. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14115. (image-file-name-regexp)
  14116. (let ((image-file-name-extensions
  14117. (or extensions
  14118. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14119. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14120. (concat "\\."
  14121. (regexp-opt (nconc (mapcar 'upcase
  14122. image-file-name-extensions)
  14123. image-file-name-extensions)
  14124. t)
  14125. "\\'"))))
  14126. (defun org-file-image-p (file &optional extensions)
  14127. "Return non-nil if FILE is an image."
  14128. (save-match-data
  14129. (string-match (org-image-file-name-regexp extensions) file)))
  14130. (defun org-get-cursor-date ()
  14131. "Return the date at cursor in as a time.
  14132. This works in the calendar and in the agenda, anywhere else it just
  14133. returns the current time."
  14134. (let (date day defd)
  14135. (cond
  14136. ((eq major-mode 'calendar-mode)
  14137. (setq date (calendar-cursor-to-date)
  14138. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14139. ((eq major-mode 'org-agenda-mode)
  14140. (setq day (get-text-property (point) 'day))
  14141. (if day
  14142. (setq date (calendar-gregorian-from-absolute day)
  14143. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14144. (nth 2 date))))))
  14145. (or defd (current-time))))
  14146. (defvar org-agenda-action-marker (make-marker)
  14147. "Marker pointing to the entry for the next agenda action.")
  14148. (defun org-mark-entry-for-agenda-action ()
  14149. "Mark the current entry as target of an agenda action.
  14150. Agenda actions are actions executed from the agenda with the key `k',
  14151. which make use of the date at the cursor."
  14152. (interactive)
  14153. (move-marker org-agenda-action-marker
  14154. (save-excursion (org-back-to-heading t) (point))
  14155. (current-buffer))
  14156. (message
  14157. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14158. ;;; Paragraph filling stuff.
  14159. ;; We want this to be just right, so use the full arsenal.
  14160. (defun org-indent-line-function ()
  14161. "Indent line like previous, but further if previous was headline or item."
  14162. (interactive)
  14163. (let* ((pos (point))
  14164. (itemp (org-at-item-p))
  14165. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14166. column bpos bcol tpos tcol bullet btype bullet-type)
  14167. ;; Find the previous relevant line
  14168. (beginning-of-line 1)
  14169. (cond
  14170. ((looking-at "#") (setq column 0))
  14171. ((looking-at "\\*+ ") (setq column 0))
  14172. ((and (looking-at "[ \t]*:END:")
  14173. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14174. (save-excursion
  14175. (goto-char (1- (match-beginning 1)))
  14176. (setq column (current-column))))
  14177. (t
  14178. (beginning-of-line 0)
  14179. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14180. (not (looking-at "[ \t]*:END:"))
  14181. (not (looking-at org-drawer-regexp)))
  14182. (beginning-of-line 0))
  14183. (cond
  14184. ((looking-at "\\*+[ \t]+")
  14185. (if (not org-adapt-indentation)
  14186. (setq column 0)
  14187. (goto-char (match-end 0))
  14188. (setq column (current-column))))
  14189. ((looking-at org-drawer-regexp)
  14190. (goto-char (1- (match-beginning 1)))
  14191. (setq column (current-column)))
  14192. ((looking-at "\\([ \t]*\\):END:")
  14193. (goto-char (match-end 1))
  14194. (setq column (current-column)))
  14195. ((org-in-item-p)
  14196. (org-beginning-of-item)
  14197. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14198. (setq bpos (match-beginning 1) tpos (match-end 0)
  14199. bcol (progn (goto-char bpos) (current-column))
  14200. tcol (progn (goto-char tpos) (current-column))
  14201. bullet (match-string 1)
  14202. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14203. (if (> tcol (+ bcol org-description-max-indent))
  14204. (setq tcol (+ bcol 5)))
  14205. (if (not itemp)
  14206. (setq column tcol)
  14207. (goto-char pos)
  14208. (beginning-of-line 1)
  14209. (if (looking-at "\\S-")
  14210. (progn
  14211. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14212. (setq bullet (match-string 1)
  14213. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14214. (setq column (if (equal btype bullet-type) bcol tcol)))
  14215. (setq column (org-get-indentation)))))
  14216. (t (setq column (org-get-indentation))))))
  14217. (goto-char pos)
  14218. (if (<= (current-column) (current-indentation))
  14219. (org-indent-line-to column)
  14220. (save-excursion (org-indent-line-to column)))
  14221. (setq column (current-column))
  14222. (beginning-of-line 1)
  14223. (if (looking-at
  14224. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14225. (replace-match (concat "\\1" (format org-property-format
  14226. (match-string 2) (match-string 3)))
  14227. t nil))
  14228. (org-move-to-column column)))
  14229. (defun org-set-autofill-regexps ()
  14230. (interactive)
  14231. ;; In the paragraph separator we include headlines, because filling
  14232. ;; text in a line directly attached to a headline would otherwise
  14233. ;; fill the headline as well.
  14234. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14235. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14236. ;; The paragraph starter includes hand-formatted lists.
  14237. (org-set-local 'paragraph-start
  14238. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14239. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14240. ;; But only if the user has not turned off tables or fixed-width regions
  14241. (org-set-local
  14242. 'auto-fill-inhibit-regexp
  14243. (concat "\\*+ \\|#\\+"
  14244. "\\|[ \t]*" org-keyword-time-regexp
  14245. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14246. (concat
  14247. "\\|[ \t]*["
  14248. (if org-enable-table-editor "|" "")
  14249. (if org-enable-fixed-width-editor ":" "")
  14250. "]"))))
  14251. ;; We use our own fill-paragraph function, to make sure that tables
  14252. ;; and fixed-width regions are not wrapped. That function will pass
  14253. ;; through to `fill-paragraph' when appropriate.
  14254. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14255. ; Adaptive filling: To get full control, first make sure that
  14256. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14257. (org-set-local 'adaptive-fill-regexp "\000")
  14258. (org-set-local 'adaptive-fill-function
  14259. 'org-adaptive-fill-function)
  14260. (org-set-local
  14261. 'align-mode-rules-list
  14262. '((org-in-buffer-settings
  14263. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14264. (modes . '(org-mode))))))
  14265. (defun org-fill-paragraph (&optional justify)
  14266. "Re-align a table, pass through to fill-paragraph if no table."
  14267. (let ((table-p (org-at-table-p))
  14268. (table.el-p (org-at-table.el-p)))
  14269. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14270. (save-excursion (goto-char (point-at-bol))
  14271. (looking-at outline-regexp)))
  14272. t) ; skip headlines
  14273. (table.el-p t) ; skip table.el tables
  14274. (table-p (org-table-align) t) ; align org-mode tables
  14275. (t nil)))) ; call paragraph-fill
  14276. ;; For reference, this is the default value of adaptive-fill-regexp
  14277. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14278. (defun org-adaptive-fill-function ()
  14279. "Return a fill prefix for org-mode files.
  14280. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14281. work correctly."
  14282. (cond ((looking-at "#[ \t]+")
  14283. (match-string 0))
  14284. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14285. (save-excursion
  14286. (if (> (match-end 1) (+ (match-beginning 1)
  14287. org-description-max-indent))
  14288. (goto-char (+ (match-beginning 1) 5))
  14289. (goto-char (match-end 0)))
  14290. (make-string (current-column) ?\ )))
  14291. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14292. (save-excursion
  14293. (goto-char (match-end 0))
  14294. (make-string (current-column) ?\ )))
  14295. (t nil)))
  14296. ;;; Other stuff.
  14297. (defun org-toggle-fixed-width-section (arg)
  14298. "Toggle the fixed-width export.
  14299. If there is no active region, the QUOTE keyword at the current headline is
  14300. inserted or removed. When present, it causes the text between this headline
  14301. and the next to be exported as fixed-width text, and unmodified.
  14302. If there is an active region, this command adds or removes a colon as the
  14303. first character of this line. If the first character of a line is a colon,
  14304. this line is also exported in fixed-width font."
  14305. (interactive "P")
  14306. (let* ((cc 0)
  14307. (regionp (org-region-active-p))
  14308. (beg (if regionp (region-beginning) (point)))
  14309. (end (if regionp (region-end)))
  14310. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14311. (case-fold-search nil)
  14312. (re "[ \t]*\\(: \\)")
  14313. off)
  14314. (if regionp
  14315. (save-excursion
  14316. (goto-char beg)
  14317. (setq cc (current-column))
  14318. (beginning-of-line 1)
  14319. (setq off (looking-at re))
  14320. (while (> nlines 0)
  14321. (setq nlines (1- nlines))
  14322. (beginning-of-line 1)
  14323. (cond
  14324. (arg
  14325. (org-move-to-column cc t)
  14326. (insert ": \n")
  14327. (forward-line -1))
  14328. ((and off (looking-at re))
  14329. (replace-match "" t t nil 1))
  14330. ((not off) (org-move-to-column cc t) (insert ": ")))
  14331. (forward-line 1)))
  14332. (save-excursion
  14333. (org-back-to-heading)
  14334. (if (looking-at (concat outline-regexp
  14335. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14336. (replace-match "" t t nil 1)
  14337. (if (looking-at outline-regexp)
  14338. (progn
  14339. (goto-char (match-end 0))
  14340. (insert org-quote-string " "))))))))
  14341. (defun org-reftex-citation ()
  14342. "Use reftex-citation to insert a citation into the buffer.
  14343. This looks for a line like
  14344. #+BIBLIOGRAPHY: foo plain option:-d
  14345. and derives from it that foo.bib is the bbliography file relevant
  14346. for this document. It then installs the necessary environment for RefTeX
  14347. to work in this buffer and calls `reftex-citation' to insert a citation
  14348. into the buffer.
  14349. Export of such citations to both LaTeX and HTML is handled by the contributed
  14350. package org-exp-bibtex by Taru Karttunen."
  14351. (interactive)
  14352. (let ((reftex-docstruct-symbol 'rds)
  14353. (reftex-cite-format "\\cite{%l}")
  14354. rds bib)
  14355. (save-excursion
  14356. (save-restriction
  14357. (widen)
  14358. (let ((case-fold-search t)
  14359. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14360. (if (not (save-excursion
  14361. (or (re-search-forward re nil t)
  14362. (re-search-backward re nil t))))
  14363. (error "No bibliography defined in file")
  14364. (setq bib (concat (match-string 1) ".bib")
  14365. rds (list (list 'bib bib)))))))
  14366. (call-interactively 'reftex-citation)))
  14367. ;;;; Functions extending outline functionality
  14368. (defun org-beginning-of-line (&optional arg)
  14369. "Go to the beginning of the current line. If that is invisible, continue
  14370. to a visible line beginning. This makes the function of C-a more intuitive.
  14371. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14372. first attempt, and only move to after the tags when the cursor is already
  14373. beyond the end of the headline."
  14374. (interactive "P")
  14375. (let ((pos (point))
  14376. (special (if (consp org-special-ctrl-a/e)
  14377. (car org-special-ctrl-a/e)
  14378. org-special-ctrl-a/e))
  14379. refpos)
  14380. (beginning-of-line 1)
  14381. (if (and arg (fboundp 'move-beginning-of-line))
  14382. (call-interactively 'move-beginning-of-line)
  14383. (if (bobp)
  14384. nil
  14385. (backward-char 1)
  14386. (if (org-invisible-p)
  14387. (while (and (not (bobp)) (org-invisible-p))
  14388. (backward-char 1)
  14389. (beginning-of-line 1))
  14390. (forward-char 1))))
  14391. (when special
  14392. (cond
  14393. ((and (looking-at org-complex-heading-regexp)
  14394. (= (char-after (match-end 1)) ?\ ))
  14395. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14396. (point-at-eol)))
  14397. (goto-char
  14398. (if (eq special t)
  14399. (cond ((> pos refpos) refpos)
  14400. ((= pos (point)) refpos)
  14401. (t (point)))
  14402. (cond ((> pos (point)) (point))
  14403. ((not (eq last-command this-command)) (point))
  14404. (t refpos)))))
  14405. ((org-at-item-p)
  14406. (goto-char
  14407. (if (eq special t)
  14408. (cond ((> pos (match-end 4)) (match-end 4))
  14409. ((= pos (point)) (match-end 4))
  14410. (t (point)))
  14411. (cond ((> pos (point)) (point))
  14412. ((not (eq last-command this-command)) (point))
  14413. (t (match-end 4))))))))
  14414. (org-no-warnings
  14415. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14416. (defun org-end-of-line (&optional arg)
  14417. "Go to the end of the line.
  14418. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14419. first attempt, and only move to after the tags when the cursor is already
  14420. beyond the end of the headline."
  14421. (interactive "P")
  14422. (let ((special (if (consp org-special-ctrl-a/e)
  14423. (cdr org-special-ctrl-a/e)
  14424. org-special-ctrl-a/e)))
  14425. (if (or (not special)
  14426. (not (org-on-heading-p))
  14427. arg)
  14428. (call-interactively (if (fboundp 'move-end-of-line)
  14429. 'move-end-of-line
  14430. 'end-of-line))
  14431. (let ((pos (point)))
  14432. (beginning-of-line 1)
  14433. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14434. (if (eq special t)
  14435. (if (or (< pos (match-beginning 1))
  14436. (= pos (match-end 0)))
  14437. (goto-char (match-beginning 1))
  14438. (goto-char (match-end 0)))
  14439. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14440. (goto-char (match-end 0))
  14441. (goto-char (match-beginning 1))))
  14442. (call-interactively (if (fboundp 'move-end-of-line)
  14443. 'move-end-of-line
  14444. 'end-of-line)))))
  14445. (org-no-warnings
  14446. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14447. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14448. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14449. (defun org-backward-sentence (&optional arg)
  14450. "Go to beginning of sentence, or beginning of table field.
  14451. This will call `backward-sentence' or `org-table-beginning-of-field',
  14452. depending on context."
  14453. (interactive "P")
  14454. (cond
  14455. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14456. (t (call-interactively 'backward-sentence))))
  14457. (defun org-forward-sentence (&optional arg)
  14458. "Go to end of sentence, or end of table field.
  14459. This will call `forward-sentence' or `org-table-end-of-field',
  14460. depending on context."
  14461. (interactive "P")
  14462. (cond
  14463. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14464. (t (call-interactively 'forward-sentence))))
  14465. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14466. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14467. (defun org-kill-line (&optional arg)
  14468. "Kill line, to tags or end of line."
  14469. (interactive "P")
  14470. (cond
  14471. ((or (not org-special-ctrl-k)
  14472. (bolp)
  14473. (not (org-on-heading-p)))
  14474. (call-interactively 'kill-line))
  14475. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14476. (kill-region (point) (match-beginning 1))
  14477. (org-set-tags nil t))
  14478. (t (kill-region (point) (point-at-eol)))))
  14479. (define-key org-mode-map "\C-k" 'org-kill-line)
  14480. (defun org-yank (&optional arg)
  14481. "Yank. If the kill is a subtree, treat it specially.
  14482. This command will look at the current kill and check if is a single
  14483. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14484. cursor is at the beginning of a line or after the stars of a currently
  14485. empty headline, then the yank is handled specially. How exactly depends
  14486. on the value of the following variables, both set by default.
  14487. org-yank-folded-subtrees
  14488. When set, the subtree(s) will be folded after insertion, but only
  14489. if doing so would now swallow text after the yanked text.
  14490. org-yank-adjusted-subtrees
  14491. When set, the subtree will be promoted or demoted in order to
  14492. fit into the local outline tree structure, which means that the level
  14493. will be adjusted so that it becomes the smaller one of the two
  14494. *visible* surrounding headings.
  14495. Any prefix to this command will cause `yank' to be called directly with
  14496. no special treatment. In particular, a simple `C-u' prefix will just
  14497. plainly yank the text as it is.
  14498. \[1] Basically, the test checks if the first non-white line is a heading
  14499. and if there are no other headings with fewer stars."
  14500. (interactive "P")
  14501. (setq this-command 'yank)
  14502. (if arg
  14503. (call-interactively 'yank)
  14504. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14505. (and (org-kill-is-subtree-p)
  14506. (or (bolp)
  14507. (and (looking-at "[ \t]*$")
  14508. (string-match
  14509. "\\`\\*+\\'"
  14510. (buffer-substring (point-at-bol) (point)))))))
  14511. swallowp)
  14512. (cond
  14513. ((and subtreep org-yank-folded-subtrees)
  14514. (let ((beg (point))
  14515. end)
  14516. (if (and subtreep org-yank-adjusted-subtrees)
  14517. (org-paste-subtree nil nil 'for-yank)
  14518. (call-interactively 'yank))
  14519. (setq end (point))
  14520. (goto-char beg)
  14521. (when (and (bolp) subtreep
  14522. (not (setq swallowp
  14523. (org-yank-folding-would-swallow-text beg end))))
  14524. (or (looking-at outline-regexp)
  14525. (re-search-forward (concat "^" outline-regexp) end t))
  14526. (while (and (< (point) end) (looking-at outline-regexp))
  14527. (hide-subtree)
  14528. (org-cycle-show-empty-lines 'folded)
  14529. (condition-case nil
  14530. (outline-forward-same-level 1)
  14531. (error (goto-char end)))))
  14532. (when swallowp
  14533. (message
  14534. "Yanked text not folded because that would swallow text"))
  14535. (goto-char end)
  14536. (skip-chars-forward " \t\n\r")
  14537. (beginning-of-line 1)
  14538. (push-mark beg 'nomsg)))
  14539. ((and subtreep org-yank-adjusted-subtrees)
  14540. (let ((beg (point-at-bol)))
  14541. (org-paste-subtree nil nil 'for-yank)
  14542. (push-mark beg 'nomsg)))
  14543. (t
  14544. (call-interactively 'yank))))))
  14545. (defun org-yank-folding-would-swallow-text (beg end)
  14546. "Would hide-subtree at BEG swallow any text after END?"
  14547. (let (level)
  14548. (save-excursion
  14549. (goto-char beg)
  14550. (when (or (looking-at outline-regexp)
  14551. (re-search-forward (concat "^" outline-regexp) end t))
  14552. (setq level (org-outline-level)))
  14553. (goto-char end)
  14554. (skip-chars-forward " \t\r\n\v\f")
  14555. (if (or (eobp)
  14556. (and (bolp) (looking-at org-outline-regexp)
  14557. (<= (org-outline-level) level)))
  14558. nil ; Nothing would be swallowed
  14559. t)))) ; something would swallow
  14560. (define-key org-mode-map "\C-y" 'org-yank)
  14561. (defun org-invisible-p ()
  14562. "Check if point is at a character currently not visible."
  14563. ;; Early versions of noutline don't have `outline-invisible-p'.
  14564. (if (fboundp 'outline-invisible-p)
  14565. (outline-invisible-p)
  14566. (get-char-property (point) 'invisible)))
  14567. (defun org-invisible-p2 ()
  14568. "Check if point is at a character currently not visible."
  14569. (save-excursion
  14570. (if (and (eolp) (not (bobp))) (backward-char 1))
  14571. ;; Early versions of noutline don't have `outline-invisible-p'.
  14572. (if (fboundp 'outline-invisible-p)
  14573. (outline-invisible-p)
  14574. (get-char-property (point) 'invisible))))
  14575. (defun org-back-to-heading (&optional invisible-ok)
  14576. "Call `outline-back-to-heading', but provide a better error message."
  14577. (condition-case nil
  14578. (outline-back-to-heading invisible-ok)
  14579. (error (error "Before first headline at position %d in buffer %s"
  14580. (point) (current-buffer)))))
  14581. (defun org-before-first-heading-p ()
  14582. "Before first heading?"
  14583. (save-excursion
  14584. (null (re-search-backward "^\\*+ " nil t))))
  14585. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14586. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14587. (defun org-at-heading-or-item-p ()
  14588. (or (org-on-heading-p) (org-at-item-p)))
  14589. (defun org-on-target-p ()
  14590. (or (org-in-regexp org-radio-target-regexp)
  14591. (org-in-regexp org-target-regexp)))
  14592. (defun org-up-heading-all (arg)
  14593. "Move to the heading line of which the present line is a subheading.
  14594. This function considers both visible and invisible heading lines.
  14595. With argument, move up ARG levels."
  14596. (if (fboundp 'outline-up-heading-all)
  14597. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14598. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14599. (defun org-up-heading-safe ()
  14600. "Move to the heading line of which the present line is a subheading.
  14601. This version will not throw an error. It will return the level of the
  14602. headline found, or nil if no higher level is found.
  14603. Also, this function will be a lot faster than `outline-up-heading',
  14604. because it relies on stars being the outline starters. This can really
  14605. make a significant difference in outlines with very many siblings."
  14606. (let (start-level re)
  14607. (org-back-to-heading t)
  14608. (setq start-level (funcall outline-level))
  14609. (if (equal start-level 1)
  14610. nil
  14611. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14612. (if (re-search-backward re nil t)
  14613. (funcall outline-level)))))
  14614. (defun org-first-sibling-p ()
  14615. "Is this heading the first child of its parents?"
  14616. (interactive)
  14617. (let ((re (concat "^" outline-regexp))
  14618. level l)
  14619. (unless (org-at-heading-p t)
  14620. (error "Not at a heading"))
  14621. (setq level (funcall outline-level))
  14622. (save-excursion
  14623. (if (not (re-search-backward re nil t))
  14624. t
  14625. (setq l (funcall outline-level))
  14626. (< l level)))))
  14627. (defun org-goto-sibling (&optional previous)
  14628. "Goto the next sibling, even if it is invisible.
  14629. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14630. when a sibling was found. When none is found, return nil and don't
  14631. move point."
  14632. (let ((fun (if previous 're-search-backward 're-search-forward))
  14633. (pos (point))
  14634. (re (concat "^" outline-regexp))
  14635. level l)
  14636. (when (condition-case nil (org-back-to-heading t) (error nil))
  14637. (setq level (funcall outline-level))
  14638. (catch 'exit
  14639. (or previous (forward-char 1))
  14640. (while (funcall fun re nil t)
  14641. (setq l (funcall outline-level))
  14642. (when (< l level) (goto-char pos) (throw 'exit nil))
  14643. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14644. (goto-char pos)
  14645. nil))))
  14646. (defun org-show-siblings ()
  14647. "Show all siblings of the current headline."
  14648. (save-excursion
  14649. (while (org-goto-sibling) (org-flag-heading nil)))
  14650. (save-excursion
  14651. (while (org-goto-sibling 'previous)
  14652. (org-flag-heading nil))))
  14653. (defun org-show-hidden-entry ()
  14654. "Show an entry where even the heading is hidden."
  14655. (save-excursion
  14656. (org-show-entry)))
  14657. (defun org-flag-heading (flag &optional entry)
  14658. "Flag the current heading. FLAG non-nil means make invisible.
  14659. When ENTRY is non-nil, show the entire entry."
  14660. (save-excursion
  14661. (org-back-to-heading t)
  14662. ;; Check if we should show the entire entry
  14663. (if entry
  14664. (progn
  14665. (org-show-entry)
  14666. (save-excursion
  14667. (and (outline-next-heading)
  14668. (org-flag-heading nil))))
  14669. (outline-flag-region (max (point-min) (1- (point)))
  14670. (save-excursion (outline-end-of-heading) (point))
  14671. flag))))
  14672. (defun org-forward-same-level (arg)
  14673. "Move forward to the ARG'th subheading at same level as this one.
  14674. Stop at the first and last subheadings of a superior heading.
  14675. This is like outline-forward-same-level, but invisible headings are ok."
  14676. (interactive "p")
  14677. (org-back-to-heading t)
  14678. (while (> arg 0)
  14679. (let ((point-to-move-to (save-excursion
  14680. (org-get-next-sibling))))
  14681. (if point-to-move-to
  14682. (progn
  14683. (goto-char point-to-move-to)
  14684. (setq arg (1- arg)))
  14685. (progn
  14686. (setq arg 0)
  14687. (error "No following same-level heading"))))))
  14688. (defun org-get-next-sibling ()
  14689. "Move to next heading of the same level, and return point.
  14690. If there is no such heading, return nil.
  14691. This is like outline-next-sibling, but invisible headings are ok."
  14692. (let ((level (funcall outline-level)))
  14693. (outline-next-heading)
  14694. (while (and (not (eobp)) (> (funcall outline-level) level))
  14695. (outline-next-heading))
  14696. (if (or (eobp) (< (funcall outline-level) level))
  14697. nil
  14698. (point))))
  14699. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14700. ;; This is an exact copy of the original function, but it uses
  14701. ;; `org-back-to-heading', to make it work also in invisible
  14702. ;; trees. And is uses an invisible-OK argument.
  14703. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14704. (org-back-to-heading invisible-OK)
  14705. (let ((first t)
  14706. (level (funcall outline-level)))
  14707. (while (and (not (eobp))
  14708. (or first (> (funcall outline-level) level)))
  14709. (setq first nil)
  14710. (outline-next-heading))
  14711. (unless to-heading
  14712. (if (memq (preceding-char) '(?\n ?\^M))
  14713. (progn
  14714. ;; Go to end of line before heading
  14715. (forward-char -1)
  14716. (if (memq (preceding-char) '(?\n ?\^M))
  14717. ;; leave blank line before heading
  14718. (forward-char -1))))))
  14719. (point))
  14720. (defun org-show-subtree ()
  14721. "Show everything after this heading at deeper levels."
  14722. (outline-flag-region
  14723. (point)
  14724. (save-excursion
  14725. (outline-end-of-subtree) (outline-next-heading) (point))
  14726. nil))
  14727. (defun org-show-entry ()
  14728. "Show the body directly following this heading.
  14729. Show the heading too, if it is currently invisible."
  14730. (interactive)
  14731. (save-excursion
  14732. (condition-case nil
  14733. (progn
  14734. (org-back-to-heading t)
  14735. (outline-flag-region
  14736. (max (point-min) (1- (point)))
  14737. (save-excursion
  14738. (if (re-search-forward
  14739. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14740. (match-beginning 1)
  14741. (point-max)))
  14742. nil))
  14743. (error nil))))
  14744. (defun org-make-options-regexp (kwds &optional extra)
  14745. "Make a regular expression for keyword lines."
  14746. (concat
  14747. "^"
  14748. "#?[ \t]*\\+\\("
  14749. (mapconcat 'regexp-quote kwds "\\|")
  14750. (if extra (concat "\\|" extra))
  14751. "\\):[ \t]*"
  14752. "\\(.+\\)"))
  14753. ;; Make isearch reveal the necessary context
  14754. (defun org-isearch-end ()
  14755. "Reveal context after isearch exits."
  14756. (when isearch-success ; only if search was successful
  14757. (if (featurep 'xemacs)
  14758. ;; Under XEmacs, the hook is run in the correct place,
  14759. ;; we directly show the context.
  14760. (org-show-context 'isearch)
  14761. ;; In Emacs the hook runs *before* restoring the overlays.
  14762. ;; So we have to use a one-time post-command-hook to do this.
  14763. ;; (Emacs 22 has a special variable, see function `org-mode')
  14764. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14765. isearch-mode-end-hook-quit)
  14766. ;; Only when the isearch was not quitted.
  14767. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14768. 'append 'local)))))
  14769. (defun org-isearch-post-command ()
  14770. "Remove self from hook, and show context."
  14771. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14772. (org-show-context 'isearch))
  14773. ;;;; Integration with and fixes for other packages
  14774. ;;; Imenu support
  14775. (defvar org-imenu-markers nil
  14776. "All markers currently used by Imenu.")
  14777. (make-variable-buffer-local 'org-imenu-markers)
  14778. (defun org-imenu-new-marker (&optional pos)
  14779. "Return a new marker for use by Imenu, and remember the marker."
  14780. (let ((m (make-marker)))
  14781. (move-marker m (or pos (point)))
  14782. (push m org-imenu-markers)
  14783. m))
  14784. (defun org-imenu-get-tree ()
  14785. "Produce the index for Imenu."
  14786. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14787. (setq org-imenu-markers nil)
  14788. (let* ((n org-imenu-depth)
  14789. (re (concat "^" outline-regexp))
  14790. (subs (make-vector (1+ n) nil))
  14791. (last-level 0)
  14792. m level head)
  14793. (save-excursion
  14794. (save-restriction
  14795. (widen)
  14796. (goto-char (point-max))
  14797. (while (re-search-backward re nil t)
  14798. (setq level (org-reduced-level (funcall outline-level)))
  14799. (when (<= level n)
  14800. (looking-at org-complex-heading-regexp)
  14801. (setq head (org-link-display-format
  14802. (org-match-string-no-properties 4))
  14803. m (org-imenu-new-marker))
  14804. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14805. (if (>= level last-level)
  14806. (push (cons head m) (aref subs level))
  14807. (push (cons head (aref subs (1+ level))) (aref subs level))
  14808. (loop for i from (1+ level) to n do (aset subs i nil)))
  14809. (setq last-level level)))))
  14810. (aref subs 1)))
  14811. (eval-after-load "imenu"
  14812. '(progn
  14813. (add-hook 'imenu-after-jump-hook
  14814. (lambda ()
  14815. (if (eq major-mode 'org-mode)
  14816. (org-show-context 'org-goto))))))
  14817. (defun org-link-display-format (link)
  14818. "Replace a link with either the description, or the link target
  14819. if no description is present"
  14820. (save-match-data
  14821. (if (string-match org-bracket-link-analytic-regexp link)
  14822. (replace-match (or (match-string 5 link)
  14823. (concat (match-string 1 link)
  14824. (match-string 3 link)))
  14825. nil nil link)
  14826. link)))
  14827. ;; Speedbar support
  14828. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14829. "Overlay marking the agenda restriction line in speedbar.")
  14830. (org-overlay-put org-speedbar-restriction-lock-overlay
  14831. 'face 'org-agenda-restriction-lock)
  14832. (org-overlay-put org-speedbar-restriction-lock-overlay
  14833. 'help-echo "Agendas are currently limited to this item.")
  14834. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14835. (defun org-speedbar-set-agenda-restriction ()
  14836. "Restrict future agenda commands to the location at point in speedbar.
  14837. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14838. (interactive)
  14839. (require 'org-agenda)
  14840. (let (p m tp np dir txt)
  14841. (cond
  14842. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14843. 'org-imenu t))
  14844. (setq m (get-text-property p 'org-imenu-marker))
  14845. (save-excursion
  14846. (save-restriction
  14847. (set-buffer (marker-buffer m))
  14848. (goto-char m)
  14849. (org-agenda-set-restriction-lock 'subtree))))
  14850. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14851. 'speedbar-function 'speedbar-find-file))
  14852. (setq tp (previous-single-property-change
  14853. (1+ p) 'speedbar-function)
  14854. np (next-single-property-change
  14855. tp 'speedbar-function)
  14856. dir (speedbar-line-directory)
  14857. txt (buffer-substring-no-properties (or tp (point-min))
  14858. (or np (point-max))))
  14859. (save-excursion
  14860. (save-restriction
  14861. (set-buffer (find-file-noselect
  14862. (let ((default-directory dir))
  14863. (expand-file-name txt))))
  14864. (unless (org-mode-p)
  14865. (error "Cannot restrict to non-Org-mode file"))
  14866. (org-agenda-set-restriction-lock 'file))))
  14867. (t (error "Don't know how to restrict Org-mode's agenda")))
  14868. (org-move-overlay org-speedbar-restriction-lock-overlay
  14869. (point-at-bol) (point-at-eol))
  14870. (setq current-prefix-arg nil)
  14871. (org-agenda-maybe-redo)))
  14872. (eval-after-load "speedbar"
  14873. '(progn
  14874. (speedbar-add-supported-extension ".org")
  14875. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14876. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14877. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14878. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14879. (add-hook 'speedbar-visiting-tag-hook
  14880. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14881. ;;; Fixes and Hacks for problems with other packages
  14882. ;; Make flyspell not check words in links, to not mess up our keymap
  14883. (defun org-mode-flyspell-verify ()
  14884. "Don't let flyspell put overlays at active buttons."
  14885. (not (get-text-property (point) 'keymap)))
  14886. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14887. (eval-after-load "bookmark"
  14888. '(if (boundp 'bookmark-after-jump-hook)
  14889. ;; We can use the hook
  14890. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14891. ;; Hook not available, use advice
  14892. (defadvice bookmark-jump (after org-make-visible activate)
  14893. "Make the position visible."
  14894. (org-bookmark-jump-unhide))))
  14895. ;; Make sure saveplace show the location if it was hidden
  14896. (eval-after-load "saveplace"
  14897. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14898. "Make the position visible."
  14899. (org-bookmark-jump-unhide)))
  14900. (defun org-bookmark-jump-unhide ()
  14901. "Unhide the current position, to show the bookmark location."
  14902. (and (org-mode-p)
  14903. (or (org-invisible-p)
  14904. (save-excursion (goto-char (max (point-min) (1- (point))))
  14905. (org-invisible-p)))
  14906. (org-show-context 'bookmark-jump)))
  14907. ;; Make session.el ignore our circular variable
  14908. (eval-after-load "session"
  14909. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14910. ;;;; Experimental code
  14911. (defun org-closed-in-range ()
  14912. "Sparse tree of items closed in a certain time range.
  14913. Still experimental, may disappear in the future."
  14914. (interactive)
  14915. ;; Get the time interval from the user.
  14916. (let* ((time1 (time-to-seconds
  14917. (org-read-date nil 'to-time nil "Starting date: ")))
  14918. (time2 (time-to-seconds
  14919. (org-read-date nil 'to-time nil "End date:")))
  14920. ;; callback function
  14921. (callback (lambda ()
  14922. (let ((time
  14923. (time-to-seconds
  14924. (apply 'encode-time
  14925. (org-parse-time-string
  14926. (match-string 1))))))
  14927. ;; check if time in interval
  14928. (and (>= time time1) (<= time time2))))))
  14929. ;; make tree, check each match with the callback
  14930. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14931. ;;;; Finish up
  14932. (provide 'org)
  14933. (run-hooks 'org-load-hook)
  14934. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14935. ;;; org.el ends here