org.el 584 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733
  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.23trans
  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.23trans"
  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 " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  163. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  164. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  165. (const :tag "C eval: Include command output as text" org-eval)
  166. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  167. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  168. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  169. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  170. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  171. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  172. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  173. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  174. (const :tag "C R: Computation using the R language" org-R)
  175. (const :tag "C registry: A registry for Org links" org-registry)
  176. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  177. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  178. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  179. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  180. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  181. (defcustom org-support-shift-select nil
  182. "Non-nil means, make shift-cursor commands select text when possible.
  183. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  184. selecting a region, or enlarge thusly regions started in this way.
  185. In Org-mode, in special contexts, these same keys are used for other
  186. purposes, important enough to compete with shift selection. Org tries
  187. to balance these needs by supporting `shift-select-mode' outside these
  188. special contexts, under control of this variable.
  189. The default of this variable is nil, to avoid confusing behavior. Shifted
  190. cursor keys will then execute Org commands in the following contexts:
  191. - on a headline, changing TODO state (left/right) and priority (up/down)
  192. - on a time stamp, changing the time
  193. - in a plain list item, changing the bullet type
  194. - in a property definition line, switching between allowed values
  195. - in the BEGIN line of a clock table (changing the time block).
  196. Outside these contexts, the commands will throw an error.
  197. When this variable is t and the cursor is not in a special context,
  198. Org-mode will support shift-selection for making and enlarging regions.
  199. To make this more effective, the bullet cycling will no longer happen
  200. anywhere in an item line, but only if the cursor is exactly on the bullet.
  201. If you set this variable to the symbol `always', then the keys
  202. will not be special in headlines, property lines, and item lines, to make
  203. shift selection work there as well. If this is what you want, you can
  204. use the following alternative commands: `C-c C-t' and `C-c ,' to
  205. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  206. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  207. edited by hand or in column view.
  208. However, when the cursor is on a timestamp, shift-cursor commands
  209. will still edit the time stamp - this is just too good to give up.
  210. XEmacs user should have this variable set to nil, because shift-select-mode
  211. is Emacs 23 only."
  212. :group 'org
  213. :type '(choice
  214. (const :tag "Never" nil)
  215. (const :tag "When outside special context" t)
  216. (const :tag "Everywhere except timestamps" always)))
  217. (defgroup org-startup nil
  218. "Options concerning startup of Org-mode."
  219. :tag "Org Startup"
  220. :group 'org)
  221. (defcustom org-startup-folded t
  222. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  223. This can also be configured on a per-file basis by adding one of
  224. the following lines anywhere in the buffer:
  225. #+STARTUP: fold
  226. #+STARTUP: nofold
  227. #+STARTUP: content"
  228. :group 'org-startup
  229. :type '(choice
  230. (const :tag "nofold: show all" nil)
  231. (const :tag "fold: overview" t)
  232. (const :tag "content: all headlines" content)))
  233. (defcustom org-startup-truncated t
  234. "Non-nil means, entering Org-mode will set `truncate-lines'.
  235. This is useful since some lines containing links can be very long and
  236. uninteresting. Also tables look terrible when wrapped."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (defcustom org-startup-align-all-tables nil
  240. "Non-nil means, align all tables when visiting a file.
  241. This is useful when the column width in tables is forced with <N> cookies
  242. in table fields. Such tables will look correct only after the first re-align.
  243. This can also be configured on a per-file basis by adding one of
  244. the following lines anywhere in the buffer:
  245. #+STARTUP: align
  246. #+STARTUP: noalign"
  247. :group 'org-startup
  248. :type 'boolean)
  249. (defcustom org-insert-mode-line-in-empty-file nil
  250. "Non-nil means insert the first line setting Org-mode in empty files.
  251. When the function `org-mode' is called interactively in an empty file, this
  252. normally means that the file name does not automatically trigger Org-mode.
  253. To ensure that the file will always be in Org-mode in the future, a
  254. line enforcing Org-mode will be inserted into the buffer, if this option
  255. has been set."
  256. :group 'org-startup
  257. :type 'boolean)
  258. (defcustom org-replace-disputed-keys nil
  259. "Non-nil means use alternative key bindings for some keys.
  260. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  261. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  262. If you want to use Org-mode together with one of these other modes,
  263. or more generally if you would like to move some Org-mode commands to
  264. other keys, set this variable and configure the keys with the variable
  265. `org-disputed-keys'.
  266. This option is only relevant at load-time of Org-mode, and must be set
  267. *before* org.el is loaded. Changing it requires a restart of Emacs to
  268. become effective."
  269. :group 'org-startup
  270. :type 'boolean)
  271. (defcustom org-use-extra-keys nil
  272. "Non-nil means use extra key sequence definitions for certain
  273. commands. This happens automatically if you run XEmacs or if
  274. window-system is nil. This variable lets you do the same
  275. manually. You must set it before loading org.
  276. Example: on Carbon Emacs 22 running graphically, with an external
  277. keyboard on a Powerbook, the default way of setting M-left might
  278. not work for either Alt or ESC. Setting this variable will make
  279. it work for ESC."
  280. :group 'org-startup
  281. :type 'boolean)
  282. (if (fboundp 'defvaralias)
  283. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  284. (defcustom org-disputed-keys
  285. '(([(shift up)] . [(meta p)])
  286. ([(shift down)] . [(meta n)])
  287. ([(shift left)] . [(meta -)])
  288. ([(shift right)] . [(meta +)])
  289. ([(control shift right)] . [(meta shift +)])
  290. ([(control shift left)] . [(meta shift -)]))
  291. "Keys for which Org-mode and other modes compete.
  292. This is an alist, cars are the default keys, second element specifies
  293. the alternative to use when `org-replace-disputed-keys' is t.
  294. Keys can be specified in any syntax supported by `define-key'.
  295. The value of this option takes effect only at Org-mode's startup,
  296. therefore you'll have to restart Emacs to apply it after changing."
  297. :group 'org-startup
  298. :type 'alist)
  299. (defun org-key (key)
  300. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  301. Or return the original if not disputed."
  302. (if org-replace-disputed-keys
  303. (let* ((nkey (key-description key))
  304. (x (org-find-if (lambda (x)
  305. (equal (key-description (car x)) nkey))
  306. org-disputed-keys)))
  307. (if x (cdr x) key))
  308. key))
  309. (defun org-find-if (predicate seq)
  310. (catch 'exit
  311. (while seq
  312. (if (funcall predicate (car seq))
  313. (throw 'exit (car seq))
  314. (pop seq)))))
  315. (defun org-defkey (keymap key def)
  316. "Define a key, possibly translated, as returned by `org-key'."
  317. (define-key keymap (org-key key) def))
  318. (defcustom org-ellipsis nil
  319. "The ellipsis to use in the Org-mode outline.
  320. When nil, just use the standard three dots. When a string, use that instead,
  321. When a face, use the standard 3 dots, but with the specified face.
  322. The change affects only Org-mode (which will then use its own display table).
  323. Changing this requires executing `M-x org-mode' in a buffer to become
  324. effective."
  325. :group 'org-startup
  326. :type '(choice (const :tag "Default" nil)
  327. (face :tag "Face" :value org-warning)
  328. (string :tag "String" :value "...#")))
  329. (defvar org-display-table nil
  330. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  331. (defgroup org-keywords nil
  332. "Keywords in Org-mode."
  333. :tag "Org Keywords"
  334. :group 'org)
  335. (defcustom org-deadline-string "DEADLINE:"
  336. "String to mark deadline entries.
  337. A deadline is this string, followed by a time stamp. Should be a word,
  338. terminated by a colon. You can insert a schedule keyword and
  339. a timestamp with \\[org-deadline].
  340. Changes become only effective after restarting Emacs."
  341. :group 'org-keywords
  342. :type 'string)
  343. (defcustom org-scheduled-string "SCHEDULED:"
  344. "String to mark scheduled TODO entries.
  345. A schedule is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-schedule].
  348. Changes become only effective after restarting Emacs."
  349. :group 'org-keywords
  350. :type 'string)
  351. (defcustom org-closed-string "CLOSED:"
  352. "String used as the prefix for timestamps logging closing a TODO entry."
  353. :group 'org-keywords
  354. :type 'string)
  355. (defcustom org-clock-string "CLOCK:"
  356. "String used as prefix for timestamps clocking work hours on an item."
  357. :group 'org-keywords
  358. :type 'string)
  359. (defcustom org-comment-string "COMMENT"
  360. "Entries starting with this keyword will never be exported.
  361. An entry can be toggled between COMMENT and normal with
  362. \\[org-toggle-comment].
  363. Changes become only effective after restarting Emacs."
  364. :group 'org-keywords
  365. :type 'string)
  366. (defcustom org-quote-string "QUOTE"
  367. "Entries starting with this keyword will be exported in fixed-width font.
  368. Quoting applies only to the text in the entry following the headline, and does
  369. not extend beyond the next headline, even if that is lower level.
  370. An entry can be toggled between QUOTE and normal with
  371. \\[org-toggle-fixed-width-section]."
  372. :group 'org-keywords
  373. :type 'string)
  374. (defconst org-repeat-re
  375. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  376. "Regular expression for specifying repeated events.
  377. After a match, group 1 contains the repeat expression.")
  378. (defgroup org-structure nil
  379. "Options concerning the general structure of Org-mode files."
  380. :tag "Org Structure"
  381. :group 'org)
  382. (defgroup org-reveal-location nil
  383. "Options about how to make context of a location visible."
  384. :tag "Org Reveal Location"
  385. :group 'org-structure)
  386. (defconst org-context-choice
  387. '(choice
  388. (const :tag "Always" t)
  389. (const :tag "Never" nil)
  390. (repeat :greedy t :tag "Individual contexts"
  391. (cons
  392. (choice :tag "Context"
  393. (const agenda)
  394. (const org-goto)
  395. (const occur-tree)
  396. (const tags-tree)
  397. (const link-search)
  398. (const mark-goto)
  399. (const bookmark-jump)
  400. (const isearch)
  401. (const default))
  402. (boolean))))
  403. "Contexts for the reveal options.")
  404. (defcustom org-show-hierarchy-above '((default . t))
  405. "Non-nil means, show full hierarchy when revealing a location.
  406. Org-mode often shows locations in an org-mode file which might have
  407. been invisible before. When this is set, the hierarchy of headings
  408. above the exposed location is shown.
  409. Turning this off for example for sparse trees makes them very compact.
  410. Instead of t, this can also be an alist specifying this option for different
  411. contexts. Valid contexts are
  412. agenda when exposing an entry from the agenda
  413. org-goto when using the command `org-goto' on key C-c C-j
  414. occur-tree when using the command `org-occur' on key C-c /
  415. tags-tree when constructing a sparse tree based on tags matches
  416. link-search when exposing search matches associated with a link
  417. mark-goto when exposing the jump goal of a mark
  418. bookmark-jump when exposing a bookmark location
  419. isearch when exiting from an incremental search
  420. default default for all contexts not set explicitly"
  421. :group 'org-reveal-location
  422. :type org-context-choice)
  423. (defcustom org-show-following-heading '((default . nil))
  424. "Non-nil means, show following heading when revealing a location.
  425. Org-mode often shows locations in an org-mode file which might have
  426. been invisible before. When this is set, the heading following the
  427. match is shown.
  428. Turning this off for example for sparse trees makes them very compact,
  429. but makes it harder to edit the location of the match. In such a case,
  430. use the command \\[org-reveal] to show more context.
  431. Instead of t, this can also be an alist specifying this option for different
  432. contexts. See `org-show-hierarchy-above' for valid contexts."
  433. :group 'org-reveal-location
  434. :type org-context-choice)
  435. (defcustom org-show-siblings '((default . nil) (isearch t))
  436. "Non-nil means, show all sibling heading when revealing a location.
  437. Org-mode often shows locations in an org-mode file which might have
  438. been invisible before. When this is set, the sibling of the current entry
  439. heading are all made visible. If `org-show-hierarchy-above' is t,
  440. the same happens on each level of the hierarchy above the current entry.
  441. By default this is on for the isearch context, off for all other contexts.
  442. Turning this off for example for sparse trees makes them very compact,
  443. but makes it harder to edit the location of the match. In such a case,
  444. use the command \\[org-reveal] to show more context.
  445. Instead of t, this can also be an alist specifying this option for different
  446. contexts. See `org-show-hierarchy-above' for valid contexts."
  447. :group 'org-reveal-location
  448. :type org-context-choice)
  449. (defcustom org-show-entry-below '((default . nil))
  450. "Non-nil means, show the entry below a headline when revealing a location.
  451. Org-mode often shows locations in an org-mode file which might have
  452. been invisible before. When this is set, the text below the headline that is
  453. exposed is also shown.
  454. By default this is off for all contexts.
  455. Instead of t, this can also be an alist specifying this option for different
  456. contexts. See `org-show-hierarchy-above' for valid contexts."
  457. :group 'org-reveal-location
  458. :type org-context-choice)
  459. (defcustom org-indirect-buffer-display 'other-window
  460. "How should indirect tree buffers be displayed?
  461. This applies to indirect buffers created with the commands
  462. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  463. Valid values are:
  464. current-window Display in the current window
  465. other-window Just display in another window.
  466. dedicated-frame Create one new frame, and re-use it each time.
  467. new-frame Make a new frame each time. Note that in this case
  468. previously-made indirect buffers are kept, and you need to
  469. kill these buffers yourself."
  470. :group 'org-structure
  471. :group 'org-agenda-windows
  472. :type '(choice
  473. (const :tag "In current window" current-window)
  474. (const :tag "In current frame, other window" other-window)
  475. (const :tag "Each time a new frame" new-frame)
  476. (const :tag "One dedicated frame" dedicated-frame)))
  477. (defgroup org-cycle nil
  478. "Options concerning visibility cycling in Org-mode."
  479. :tag "Org Cycle"
  480. :group 'org-structure)
  481. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  482. "Names of drawers. Drawers are not opened by cycling on the headline above.
  483. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  484. this:
  485. :DRAWERNAME:
  486. .....
  487. :END:
  488. The drawer \"PROPERTIES\" is special for capturing properties through
  489. the property API.
  490. Drawers can be defined on the per-file basis with a line like:
  491. #+DRAWERS: HIDDEN STATE PROPERTIES"
  492. :group 'org-structure
  493. :type '(repeat (string :tag "Drawer Name")))
  494. (defcustom org-cycle-global-at-bob nil
  495. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  496. This makes it possible to do global cycling without having to use S-TAB or
  497. C-u TAB. For this special case to work, the first line of the buffer
  498. must not be a headline - it may be empty or some other text. When used in
  499. this way, `org-cycle-hook' is disables temporarily, to make sure the
  500. cursor stays at the beginning of the buffer.
  501. When this option is nil, don't do anything special at the beginning
  502. of the buffer."
  503. :group 'org-cycle
  504. :type 'boolean)
  505. (defcustom org-cycle-emulate-tab t
  506. "Where should `org-cycle' emulate TAB.
  507. nil Never
  508. white Only in completely white lines
  509. whitestart Only at the beginning of lines, before the first non-white char
  510. t Everywhere except in headlines
  511. exc-hl-bol Everywhere except at the start of a headline
  512. If TAB is used in a place where it does not emulate TAB, the current subtree
  513. visibility is cycled."
  514. :group 'org-cycle
  515. :type '(choice (const :tag "Never" nil)
  516. (const :tag "Only in completely white lines" white)
  517. (const :tag "Before first char in a line" whitestart)
  518. (const :tag "Everywhere except in headlines" t)
  519. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  520. ))
  521. (defcustom org-cycle-separator-lines 2
  522. "Number of empty lines needed to keep an empty line between collapsed trees.
  523. If you leave an empty line between the end of a subtree and the following
  524. headline, this empty line is hidden when the subtree is folded.
  525. Org-mode will leave (exactly) one empty line visible if the number of
  526. empty lines is equal or larger to the number given in this variable.
  527. So the default 2 means, at least 2 empty lines after the end of a subtree
  528. are needed to produce free space between a collapsed subtree and the
  529. following headline.
  530. Special case: when 0, never leave empty lines in collapsed view."
  531. :group 'org-cycle
  532. :type 'integer)
  533. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  534. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  535. org-cycle-hide-drawers
  536. org-cycle-show-empty-lines
  537. org-optimize-window-after-visibility-change)
  538. "Hook that is run after `org-cycle' has changed the buffer visibility.
  539. The function(s) in this hook must accept a single argument which indicates
  540. the new state that was set by the most recent `org-cycle' command. The
  541. argument is a symbol. After a global state change, it can have the values
  542. `overview', `content', or `all'. After a local state change, it can have
  543. the values `folded', `children', or `subtree'."
  544. :group 'org-cycle
  545. :type 'hook)
  546. (defgroup org-edit-structure nil
  547. "Options concerning structure editing in Org-mode."
  548. :tag "Org Edit Structure"
  549. :group 'org-structure)
  550. (defcustom org-odd-levels-only nil
  551. "Non-nil means, skip even levels and only use odd levels for the outline.
  552. This has the effect that two stars are being added/taken away in
  553. promotion/demotion commands. It also influences how levels are
  554. handled by the exporters.
  555. Changing it requires restart of `font-lock-mode' to become effective
  556. for fontification also in regions already fontified.
  557. You may also set this on a per-file basis by adding one of the following
  558. lines to the buffer:
  559. #+STARTUP: odd
  560. #+STARTUP: oddeven"
  561. :group 'org-edit-structure
  562. :group 'org-font-lock
  563. :type 'boolean)
  564. (defcustom org-adapt-indentation t
  565. "Non-nil means, adapt indentation when promoting and demoting.
  566. When this is set and the *entire* text in an entry is indented, the
  567. indentation is increased by one space in a demotion command, and
  568. decreased by one in a promotion command. If any line in the entry
  569. body starts at column 0, indentation is not changed at all."
  570. :group 'org-edit-structure
  571. :type 'boolean)
  572. (defcustom org-special-ctrl-a/e nil
  573. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  574. When t, `C-a' will bring back the cursor to the beginning of the
  575. headline text, i.e. after the stars and after a possible TODO keyword.
  576. In an item, this will be the position after the bullet.
  577. When the cursor is already at that position, another `C-a' will bring
  578. it to the beginning of the line.
  579. `C-e' will jump to the end of the headline, ignoring the presence of tags
  580. in the headline. A second `C-e' will then jump to the true end of the
  581. line, after any tags.
  582. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  583. and only a directly following, identical keypress will bring the cursor
  584. to the special positions.
  585. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  586. set separately."
  587. :group 'org-edit-structure
  588. :type '(choice
  589. (const :tag "off" nil)
  590. (const :tag "after stars/bullet and before tags first" t)
  591. (const :tag "true line boundary first" reversed)
  592. (cons :tag "Set C-a and C-e separately"
  593. (choice :tag "Special C-a"
  594. (const :tag "off" nil)
  595. (const :tag "after stars/bullet first" t)
  596. (const :tag "before stars/bullet first" reversed))
  597. (choice :tag "Special C-e"
  598. (const :tag "off" nil)
  599. (const :tag "before tags first" t)
  600. (const :tag "after tags first" reversed)))))
  601. (if (fboundp 'defvaralias)
  602. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  603. (defcustom org-special-ctrl-k nil
  604. "Non-nil means `C-k' will behave specially in headlines.
  605. When nil, `C-k' will call the default `kill-line' command.
  606. When t, the following will happen while the cursor is in the headline:
  607. - When the cursor is at the beginning of a headline, kill the entire
  608. line and possible the folded subtree below the line.
  609. - When in the middle of the headline text, kill the headline up to the tags.
  610. - When after the headline text, kill the tags."
  611. :group 'org-edit-structure
  612. :type 'boolean)
  613. (defcustom org-yank-folded-subtrees t
  614. "Non-nil means, when yanking subtrees, fold them.
  615. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  616. it starts with a heading and all other headings in it are either children
  617. or siblings, then fold all the subtrees. However, do this only if no
  618. text after the yank would be swallowed into a folded tree by this action."
  619. :group 'org-edit-structure
  620. :type 'boolean)
  621. (defcustom org-yank-adjusted-subtrees nil
  622. "Non-nil means, when yanking subtrees, adjust the level.
  623. With this setting, `org-paste-subtree' is used to insert the subtree, see
  624. this function for details."
  625. :group 'org-edit-structure
  626. :type 'boolean)
  627. (defcustom org-M-RET-may-split-line '((default . t))
  628. "Non-nil means, M-RET will split the line at the cursor position.
  629. When nil, it will go to the end of the line before making a
  630. new line.
  631. You may also set this option in a different way for different
  632. contexts. Valid contexts are:
  633. headline when creating a new headline
  634. item when creating a new item
  635. table in a table field
  636. default the value to be used for all contexts not explicitly
  637. customized"
  638. :group 'org-structure
  639. :group 'org-table
  640. :type '(choice
  641. (const :tag "Always" t)
  642. (const :tag "Never" nil)
  643. (repeat :greedy t :tag "Individual contexts"
  644. (cons
  645. (choice :tag "Context"
  646. (const headline)
  647. (const item)
  648. (const table)
  649. (const default))
  650. (boolean)))))
  651. (defcustom org-insert-heading-respect-content nil
  652. "Non-nil means, insert new headings after the current subtree.
  653. When nil, the new heading is created directly after the current line.
  654. The commands \\[org-insert-heading-respect-content] and
  655. \\[org-insert-todo-heading-respect-content] turn this variable on
  656. for the duration of the command."
  657. :group 'org-structure
  658. :type 'boolean)
  659. (defcustom org-blank-before-new-entry '((heading . auto)
  660. (plain-list-item . auto))
  661. "Should `org-insert-heading' leave a blank line before new heading/item?
  662. The value is an alist, with `heading' and `plain-list-item' as car,
  663. and a boolean flag as cdr."
  664. :group 'org-edit-structure
  665. :type '(list
  666. (cons (const heading)
  667. (choice (const :tag "Never" nil)
  668. (const :tag "Always" t)
  669. (const :tag "Auto" auto)))
  670. (cons (const plain-list-item)
  671. (choice (const :tag "Never" nil)
  672. (const :tag "Always" t)
  673. (const :tag "Auto" auto)))))
  674. (defcustom org-insert-heading-hook nil
  675. "Hook being run after inserting a new heading."
  676. :group 'org-edit-structure
  677. :type 'hook)
  678. (defcustom org-enable-fixed-width-editor t
  679. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  680. This currently only means, they are never auto-wrapped.
  681. When nil, such lines will be treated like ordinary lines.
  682. See also the QUOTE keyword."
  683. :group 'org-edit-structure
  684. :type 'boolean)
  685. (defcustom org-edit-src-region-extra nil
  686. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  687. For examples see the function `org-edit-src-find-region-and-lang'.
  688. The regular expression identifying the begin marker should end with a newline,
  689. and the regexp marking the end line should start with a newline, to make sure
  690. there are kept outside the narrowed region."
  691. :group 'org-edit-structure
  692. :type '(repeat
  693. (list
  694. (regexp :tag "begin regexp")
  695. (regexp :tag "end regexp")
  696. (choice :tag "language"
  697. (string :tag "specify")
  698. (integer :tag "from match group")
  699. (const :tag "from `lang' element")
  700. (const :tag "from `style' element")))))
  701. (defcustom org-coderef-label-format "(ref:%s)"
  702. "The default coderef format.
  703. This format string will be used to search for coderef labels in literal
  704. examples (EXAMPLE and SRC blocks). The format can be overwritten
  705. an individual literal example with the -f option, like
  706. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  707. ...
  708. #+END_SRC
  709. If you want to use this for HTML export, make sure that the format does
  710. not introduce special font-locking, and avoid the HTML special
  711. characters `<', `>', and `&'. The reason for this restriction is that
  712. the labels are searched for only after htmlize has done its job."
  713. :group 'org-edit-structure ; FIXME this is not in the right group
  714. :type 'string)
  715. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  716. "The mode that should be used to edit fixed-width regions.
  717. These are the regions where each line starts with a colon."
  718. :group 'org-edit-structure
  719. :type '(choice
  720. (const artist-mode)
  721. (const picture-mode)
  722. (const fundamental-mode)
  723. (function :tag "Other (specify)")))
  724. (defcustom org-goto-auto-isearch t
  725. "Non-nil means, typing characters in org-goto starts incremental search."
  726. :group 'org-edit-structure
  727. :type 'boolean)
  728. (defgroup org-sparse-trees nil
  729. "Options concerning sparse trees in Org-mode."
  730. :tag "Org Sparse Trees"
  731. :group 'org-structure)
  732. (defcustom org-highlight-sparse-tree-matches t
  733. "Non-nil means, highlight all matches that define a sparse tree.
  734. The highlights will automatically disappear the next time the buffer is
  735. changed by an edit command."
  736. :group 'org-sparse-trees
  737. :type 'boolean)
  738. (defcustom org-remove-highlights-with-change t
  739. "Non-nil means, any change to the buffer will remove temporary highlights.
  740. Such highlights are created by `org-occur' and `org-clock-display'.
  741. When nil, `C-c C-c needs to be used to get rid of the highlights.
  742. The highlights created by `org-preview-latex-fragment' always need
  743. `C-c C-c' to be removed."
  744. :group 'org-sparse-trees
  745. :group 'org-time
  746. :type 'boolean)
  747. (defcustom org-occur-hook '(org-first-headline-recenter)
  748. "Hook that is run after `org-occur' has constructed a sparse tree.
  749. This can be used to recenter the window to show as much of the structure
  750. as possible."
  751. :group 'org-sparse-trees
  752. :type 'hook)
  753. (defgroup org-imenu-and-speedbar nil
  754. "Options concerning imenu and speedbar in Org-mode."
  755. :tag "Org Imenu and Speedbar"
  756. :group 'org-structure)
  757. (defcustom org-imenu-depth 2
  758. "The maximum level for Imenu access to Org-mode headlines.
  759. This also applied for speedbar access."
  760. :group 'org-imenu-and-speedbar
  761. :type 'number)
  762. (defgroup org-table nil
  763. "Options concerning tables in Org-mode."
  764. :tag "Org Table"
  765. :group 'org)
  766. (defcustom org-enable-table-editor 'optimized
  767. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  768. When nil, such lines will be treated like ordinary lines.
  769. When equal to the symbol `optimized', the table editor will be optimized to
  770. do the following:
  771. - Automatic overwrite mode in front of whitespace in table fields.
  772. This makes the structure of the table stay in tact as long as the edited
  773. field does not exceed the column width.
  774. - Minimize the number of realigns. Normally, the table is aligned each time
  775. TAB or RET are pressed to move to another field. With optimization this
  776. happens only if changes to a field might have changed the column width.
  777. Optimization requires replacing the functions `self-insert-command',
  778. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  779. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  780. very good at guessing when a re-align will be necessary, but you can always
  781. force one with \\[org-ctrl-c-ctrl-c].
  782. If you would like to use the optimized version in Org-mode, but the
  783. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  784. This variable can be used to turn on and off the table editor during a session,
  785. but in order to toggle optimization, a restart is required.
  786. See also the variable `org-table-auto-blank-field'."
  787. :group 'org-table
  788. :type '(choice
  789. (const :tag "off" nil)
  790. (const :tag "on" t)
  791. (const :tag "on, optimized" optimized)))
  792. (defcustom org-self-insert-cluster-for-undo t
  793. "Non-nil means cluster self-insert commands for undo when possible.
  794. If this is set, then, like in the Emacs command loop, 20 consequtive
  795. characters will be undone together.
  796. This is configurable, because there is some impact on typing performance."
  797. :group 'org-table
  798. :type 'boolean)
  799. (defcustom org-table-tab-recognizes-table.el t
  800. "Non-nil means, TAB will automatically notice a table.el table.
  801. When it sees such a table, it moves point into it and - if necessary -
  802. calls `table-recognize-table'."
  803. :group 'org-table-editing
  804. :type 'boolean)
  805. (defgroup org-link nil
  806. "Options concerning links in Org-mode."
  807. :tag "Org Link"
  808. :group 'org)
  809. (defvar org-link-abbrev-alist-local nil
  810. "Buffer-local version of `org-link-abbrev-alist', which see.
  811. The value of this is taken from the #+LINK lines.")
  812. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  813. (defcustom org-link-abbrev-alist nil
  814. "Alist of link abbreviations.
  815. The car of each element is a string, to be replaced at the start of a link.
  816. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  817. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  818. [[linkkey:tag][description]]
  819. The 'linkkey' must be a word word, starting with a letter, followed
  820. by letters, numbers, '-' or '_'.
  821. If REPLACE is a string, the tag will simply be appended to create the link.
  822. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  823. the placeholder \"%h\" will cause a url-encoded version of the tag to
  824. be inserted at that point (see the function `url-hexify-string').
  825. REPLACE may also be a function that will be called with the tag as the
  826. only argument to create the link, which should be returned as a string.
  827. See the manual for examples."
  828. :group 'org-link
  829. :type '(repeat
  830. (cons
  831. (string :tag "Protocol")
  832. (choice
  833. (string :tag "Format")
  834. (function)))))
  835. (defcustom org-descriptive-links t
  836. "Non-nil means, hide link part and only show description of bracket links.
  837. Bracket links are like [[link][description]]. This variable sets the initial
  838. state in new org-mode buffers. The setting can then be toggled on a
  839. per-buffer basis from the Org->Hyperlinks menu."
  840. :group 'org-link
  841. :type 'boolean)
  842. (defcustom org-link-file-path-type 'adaptive
  843. "How the path name in file links should be stored.
  844. Valid values are:
  845. relative Relative to the current directory, i.e. the directory of the file
  846. into which the link is being inserted.
  847. absolute Absolute path, if possible with ~ for home directory.
  848. noabbrev Absolute path, no abbreviation of home directory.
  849. adaptive Use relative path for files in the current directory and sub-
  850. directories of it. For other files, use an absolute path."
  851. :group 'org-link
  852. :type '(choice
  853. (const relative)
  854. (const absolute)
  855. (const noabbrev)
  856. (const adaptive)))
  857. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  858. "Types of links that should be activated in Org-mode files.
  859. This is a list of symbols, each leading to the activation of a certain link
  860. type. In principle, it does not hurt to turn on most link types - there may
  861. be a small gain when turning off unused link types. The types are:
  862. bracket The recommended [[link][description]] or [[link]] links with hiding.
  863. angular Links in angular brackets that may contain whitespace like
  864. <bbdb:Carsten Dominik>.
  865. plain Plain links in normal text, no whitespace, like http://google.com.
  866. radio Text that is matched by a radio target, see manual for details.
  867. tag Tag settings in a headline (link to tag search).
  868. date Time stamps (link to calendar).
  869. footnote Footnote labels.
  870. Changing this variable requires a restart of Emacs to become effective."
  871. :group 'org-link
  872. :type '(set :greedy t
  873. (const :tag "Double bracket links (new style)" bracket)
  874. (const :tag "Angular bracket links (old style)" angular)
  875. (const :tag "Plain text links" plain)
  876. (const :tag "Radio target matches" radio)
  877. (const :tag "Tags" tag)
  878. (const :tag "Timestamps" date)
  879. (const :tag "Footnotes" footnote)))
  880. (defcustom org-make-link-description-function nil
  881. "Function to use to generate link descriptions from links. If
  882. nil the link location will be used. This function must take two
  883. parameters; the first is the link and the second the description
  884. org-insert-link has generated, and should return the description
  885. to use."
  886. :group 'org-link
  887. :type 'function)
  888. (defgroup org-link-store nil
  889. "Options concerning storing links in Org-mode."
  890. :tag "Org Store Link"
  891. :group 'org-link)
  892. (defcustom org-email-link-description-format "Email %c: %.30s"
  893. "Format of the description part of a link to an email or usenet message.
  894. The following %-escapes will be replaced by corresponding information:
  895. %F full \"From\" field
  896. %f name, taken from \"From\" field, address if no name
  897. %T full \"To\" field
  898. %t first name in \"To\" field, address if no name
  899. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  900. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  901. %s subject
  902. %m message-id.
  903. You may use normal field width specification between the % and the letter.
  904. This is for example useful to limit the length of the subject.
  905. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  906. :group 'org-link-store
  907. :type 'string)
  908. (defcustom org-from-is-user-regexp
  909. (let (r1 r2)
  910. (when (and user-mail-address (not (string= user-mail-address "")))
  911. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  912. (when (and user-full-name (not (string= user-full-name "")))
  913. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  914. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  915. "Regexp matched against the \"From:\" header of an email or usenet message.
  916. It should match if the message is from the user him/herself."
  917. :group 'org-link-store
  918. :type 'regexp)
  919. (defcustom org-link-to-org-use-id 'create-if-interactive
  920. "Non-nil means, storing a link to an Org file will use entry IDs.
  921. Note that before this variable is even considered, org-id must be loaded,
  922. to please customize `org-modules' and turn it on.
  923. The variable can have the following values:
  924. t Create an ID if needed to make a link to the current entry.
  925. create-if-interactive
  926. If `org-store-link' is called directly (interactively, as a user
  927. command), do create an ID to support the link. But when doing the
  928. job for remember, only use the ID if it already exists. The
  929. purpose of this setting is to avoid proliferation of unwanted
  930. IDs, just because you happen to be in an Org file when you
  931. call `org-remember' that automatically and preemptively
  932. creates a link. If you do want to get an ID link in a remember
  933. template to an entry not having an ID, create it first by
  934. explicitly creating a link to it, using `C-c C-l' first.
  935. use-existing
  936. Use existing ID, do not create one.
  937. nil Never use an ID to make a link, instead link using a text search for
  938. the headline text."
  939. :group 'org-link-store
  940. :type '(choice
  941. (const :tag "Create ID to make link" t)
  942. (const :tag "Create if string link interactively"
  943. 'create-if-interactive)
  944. (const :tag "Only use existing" 'use-existing)
  945. (const :tag "Do not use ID to create link" nil)))
  946. (defcustom org-context-in-file-links t
  947. "Non-nil means, file links from `org-store-link' contain context.
  948. A search string will be added to the file name with :: as separator and
  949. used to find the context when the link is activated by the command
  950. `org-open-at-point'.
  951. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  952. negates this setting for the duration of the command."
  953. :group 'org-link-store
  954. :type 'boolean)
  955. (defcustom org-keep-stored-link-after-insertion nil
  956. "Non-nil means, keep link in list for entire session.
  957. The command `org-store-link' adds a link pointing to the current
  958. location to an internal list. These links accumulate during a session.
  959. The command `org-insert-link' can be used to insert links into any
  960. Org-mode file (offering completion for all stored links). When this
  961. option is nil, every link which has been inserted once using \\[org-insert-link]
  962. will be removed from the list, to make completing the unused links
  963. more efficient."
  964. :group 'org-link-store
  965. :type 'boolean)
  966. (defgroup org-link-follow nil
  967. "Options concerning following links in Org-mode."
  968. :tag "Org Follow Link"
  969. :group 'org-link)
  970. (defcustom org-link-translation-function nil
  971. "Function to translate links with different syntax to Org syntax.
  972. This can be used to translate links created for example by the Planner
  973. or emacs-wiki packages to Org syntax.
  974. The function must accept two parameters, a TYPE containing the link
  975. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  976. which is everything after the link protocol. It should return a cons
  977. with possibly modified values of type and path.
  978. Org contains a function for this, so if you set this variable to
  979. `org-translate-link-from-planner', you should be able follow many
  980. links created by planner."
  981. :group 'org-link-follow
  982. :type 'function)
  983. (defcustom org-follow-link-hook nil
  984. "Hook that is run after a link has been followed."
  985. :group 'org-link-follow
  986. :type 'hook)
  987. (defcustom org-tab-follows-link nil
  988. "Non-nil means, on links TAB will follow the link.
  989. Needs to be set before org.el is loaded.
  990. This really should not be used, it does not make sense, and the
  991. implementation is bad."
  992. :group 'org-link-follow
  993. :type 'boolean)
  994. (defcustom org-return-follows-link nil
  995. "Non-nil means, on links RET will follow the link.
  996. Needs to be set before org.el is loaded."
  997. :group 'org-link-follow
  998. :type 'boolean)
  999. (defcustom org-mouse-1-follows-link
  1000. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1001. "Non-nil means, mouse-1 on a link will follow the link.
  1002. A longer mouse click will still set point. Does not work on XEmacs.
  1003. Needs to be set before org.el is loaded."
  1004. :group 'org-link-follow
  1005. :type 'boolean)
  1006. (defcustom org-mark-ring-length 4
  1007. "Number of different positions to be recorded in the ring
  1008. Changing this requires a restart of Emacs to work correctly."
  1009. :group 'org-link-follow
  1010. :type 'integer)
  1011. (defcustom org-link-frame-setup
  1012. '((vm . vm-visit-folder-other-frame)
  1013. (gnus . gnus-other-frame)
  1014. (file . find-file-other-window))
  1015. "Setup the frame configuration for following links.
  1016. When following a link with Emacs, it may often be useful to display
  1017. this link in another window or frame. This variable can be used to
  1018. set this up for the different types of links.
  1019. For VM, use any of
  1020. `vm-visit-folder'
  1021. `vm-visit-folder-other-frame'
  1022. For Gnus, use any of
  1023. `gnus'
  1024. `gnus-other-frame'
  1025. `org-gnus-no-new-news'
  1026. For FILE, use any of
  1027. `find-file'
  1028. `find-file-other-window'
  1029. `find-file-other-frame'
  1030. For the calendar, use the variable `calendar-setup'.
  1031. For BBDB, it is currently only possible to display the matches in
  1032. another window."
  1033. :group 'org-link-follow
  1034. :type '(list
  1035. (cons (const vm)
  1036. (choice
  1037. (const vm-visit-folder)
  1038. (const vm-visit-folder-other-window)
  1039. (const vm-visit-folder-other-frame)))
  1040. (cons (const gnus)
  1041. (choice
  1042. (const gnus)
  1043. (const gnus-other-frame)
  1044. (const org-gnus-no-new-news)))
  1045. (cons (const file)
  1046. (choice
  1047. (const find-file)
  1048. (const find-file-other-window)
  1049. (const find-file-other-frame)))))
  1050. (defcustom org-display-internal-link-with-indirect-buffer nil
  1051. "Non-nil means, use indirect buffer to display infile links.
  1052. Activating internal links (from one location in a file to another location
  1053. in the same file) normally just jumps to the location. When the link is
  1054. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1055. another window. When this option is set, the other window actually displays
  1056. an indirect buffer clone of the current buffer, to avoid any visibility
  1057. changes to the current buffer."
  1058. :group 'org-link-follow
  1059. :type 'boolean)
  1060. (defcustom org-open-non-existing-files nil
  1061. "Non-nil means, `org-open-file' will open non-existing files.
  1062. When nil, an error will be generated."
  1063. :group 'org-link-follow
  1064. :type 'boolean)
  1065. (defcustom org-open-directory-means-index-dot-org nil
  1066. "Non-nil means, a link to a directory really means to index.org.
  1067. When nil, following a directory link will run dired or open a finder/explorer
  1068. window on that directory."
  1069. :group 'org-link-follow
  1070. :type 'boolean)
  1071. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1072. "Function and arguments to call for following mailto links.
  1073. This is a list with the first element being a lisp function, and the
  1074. remaining elements being arguments to the function. In string arguments,
  1075. %a will be replaced by the address, and %s will be replaced by the subject
  1076. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1077. :group 'org-link-follow
  1078. :type '(choice
  1079. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1080. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1081. (const :tag "message-mail" (message-mail "%a" "%s"))
  1082. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1083. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1084. "Non-nil means, ask for confirmation before executing shell links.
  1085. Shell links can be dangerous: just think about a link
  1086. [[shell:rm -rf ~/*][Google Search]]
  1087. This link would show up in your Org-mode document as \"Google Search\",
  1088. but really it would remove your entire home directory.
  1089. Therefore we advise against setting this variable to nil.
  1090. Just change it to `y-or-n-p' of you want to confirm with a
  1091. single keystroke rather than having to type \"yes\"."
  1092. :group 'org-link-follow
  1093. :type '(choice
  1094. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1095. (const :tag "with y-or-n (faster)" y-or-n-p)
  1096. (const :tag "no confirmation (dangerous)" nil)))
  1097. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1098. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1099. Elisp links can be dangerous: just think about a link
  1100. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1101. This link would show up in your Org-mode document as \"Google Search\",
  1102. but really it would remove your entire home directory.
  1103. Therefore we advise against setting this variable to nil.
  1104. Just change it to `y-or-n-p' of you want to confirm with a
  1105. single keystroke rather than having to type \"yes\"."
  1106. :group 'org-link-follow
  1107. :type '(choice
  1108. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1109. (const :tag "with y-or-n (faster)" y-or-n-p)
  1110. (const :tag "no confirmation (dangerous)" nil)))
  1111. (defconst org-file-apps-defaults-gnu
  1112. '((remote . emacs)
  1113. (system . mailcap)
  1114. (t . mailcap))
  1115. "Default file applications on a UNIX or GNU/Linux system.
  1116. See `org-file-apps'.")
  1117. (defconst org-file-apps-defaults-macosx
  1118. '((remote . emacs)
  1119. (t . "open %s")
  1120. (system . "open %s")
  1121. ("ps.gz" . "gv %s")
  1122. ("eps.gz" . "gv %s")
  1123. ("dvi" . "xdvi %s")
  1124. ("fig" . "xfig %s"))
  1125. "Default file applications on a MacOS X system.
  1126. The system \"open\" is known as a default, but we use X11 applications
  1127. for some files for which the OS does not have a good default.
  1128. See `org-file-apps'.")
  1129. (defconst org-file-apps-defaults-windowsnt
  1130. (list
  1131. '(remote . emacs)
  1132. (cons t
  1133. (list (if (featurep 'xemacs)
  1134. 'mswindows-shell-execute
  1135. 'w32-shell-execute)
  1136. "open" 'file))
  1137. (cons 'system
  1138. (list (if (featurep 'xemacs)
  1139. 'mswindows-shell-execute
  1140. 'w32-shell-execute)
  1141. "open" 'file)))
  1142. "Default file applications on a Windows NT system.
  1143. The system \"open\" is used for most files.
  1144. See `org-file-apps'.")
  1145. (defcustom org-file-apps
  1146. '(
  1147. (auto-mode . emacs)
  1148. ("\\.x?html?\\'" . default)
  1149. ("\\.pdf\\'" . default)
  1150. )
  1151. "External applications for opening `file:path' items in a document.
  1152. Org-mode uses system defaults for different file types, but
  1153. you can use this variable to set the application for a given file
  1154. extension. The entries in this list are cons cells where the car identifies
  1155. files and the cdr the corresponding command. Possible values for the
  1156. file identifier are
  1157. \"regex\" Regular expression matched against the file name. For backward
  1158. compatibility, this can also be a string with only alphanumeric
  1159. characters, which is then interpreted as an extension.
  1160. `directory' Matches a directory
  1161. `remote' Matches a remote file, accessible through tramp or efs.
  1162. Remote files most likely should be visited through Emacs
  1163. because external applications cannot handle such paths.
  1164. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1165. so all files Emacs knows how to handle. Using this with
  1166. command `emacs' will open most files in Emacs. Beware that this
  1167. will also open html files inside Emacs, unless you add
  1168. (\"html\" . default) to the list as well.
  1169. t Default for files not matched by any of the other options.
  1170. `system' The system command to open files, like `open' on Windows
  1171. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1172. that will be selected if you call `C-c C-o' with a double
  1173. `C-u C-u' prefix.
  1174. Possible values for the command are:
  1175. `emacs' The file will be visited by the current Emacs process.
  1176. `default' Use the default application for this file type, which is the
  1177. association for t in the list, most likely in the system-specific
  1178. part.
  1179. This can be used to overrule an unwanted setting in the
  1180. system-specific variable.
  1181. `system' Use the system command for opening files, like \"open\".
  1182. This command is specified by the entry whose car is `system'.
  1183. Most likely, the system-specific version of this variable
  1184. does define this command, but you can overrule/replace it
  1185. here.
  1186. string A command to be executed by a shell; %s will be replaced
  1187. by the path to the file.
  1188. sexp A Lisp form which will be evaluated. The file path will
  1189. be available in the Lisp variable `file'.
  1190. For more examples, see the system specific constants
  1191. `org-file-apps-defaults-macosx'
  1192. `org-file-apps-defaults-windowsnt'
  1193. `org-file-apps-defaults-gnu'."
  1194. :group 'org-link-follow
  1195. :type '(repeat
  1196. (cons (choice :value ""
  1197. (string :tag "Extension")
  1198. (const :tag "System command to open files" system)
  1199. (const :tag "Default for unrecognized files" t)
  1200. (const :tag "Remote file" remote)
  1201. (const :tag "Links to a directory" directory)
  1202. (const :tag "Any files that have Emacs modes"
  1203. auto-mode))
  1204. (choice :value ""
  1205. (const :tag "Visit with Emacs" emacs)
  1206. (const :tag "Use default" default)
  1207. (const :tag "Use the system command" system)
  1208. (string :tag "Command")
  1209. (sexp :tag "Lisp form")))))
  1210. (defgroup org-refile nil
  1211. "Options concerning refiling entries in Org-mode."
  1212. :tag "Org Refile"
  1213. :group 'org)
  1214. (defcustom org-directory "~/org"
  1215. "Directory with org files.
  1216. This is just a default location to look for Org files. There is no need
  1217. at all to put your files into this directory. It is only used in the
  1218. following situations:
  1219. 1. When a remember template specifies a target file that is not an
  1220. absolute path. The path will then be interpreted relative to
  1221. `org-directory'
  1222. 2. When a remember note is filed away in an interactive way (when exiting the
  1223. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1224. with `org-directory' as the default path."
  1225. :group 'org-refile
  1226. :group 'org-remember
  1227. :type 'directory)
  1228. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1229. "Default target for storing notes.
  1230. Used by the hooks for remember.el. This can be a string, or nil to mean
  1231. the value of `remember-data-file'.
  1232. You can set this on a per-template basis with the variable
  1233. `org-remember-templates'."
  1234. :group 'org-refile
  1235. :group 'org-remember
  1236. :type '(choice
  1237. (const :tag "Default from remember-data-file" nil)
  1238. file))
  1239. (defcustom org-goto-interface 'outline
  1240. "The default interface to be used for `org-goto'.
  1241. Allowed values are:
  1242. outline The interface shows an outline of the relevant file
  1243. and the correct heading is found by moving through
  1244. the outline or by searching with incremental search.
  1245. outline-path-completion Headlines in the current buffer are offered via
  1246. completion. This is the interface also used by
  1247. the refile command."
  1248. :group 'org-refile
  1249. :type '(choice
  1250. (const :tag "Outline" outline)
  1251. (const :tag "Outline-path-completion" outline-path-completion)))
  1252. (defcustom org-goto-max-level 5
  1253. "Maximum level to be considered when running org-goto with refile interface."
  1254. :group 'org-refile
  1255. :type 'number)
  1256. (defcustom org-reverse-note-order nil
  1257. "Non-nil means, store new notes at the beginning of a file or entry.
  1258. When nil, new notes will be filed to the end of a file or entry.
  1259. This can also be a list with cons cells of regular expressions that
  1260. are matched against file names, and values."
  1261. :group 'org-remember
  1262. :group 'org-refile
  1263. :type '(choice
  1264. (const :tag "Reverse always" t)
  1265. (const :tag "Reverse never" nil)
  1266. (repeat :tag "By file name regexp"
  1267. (cons regexp boolean))))
  1268. (defcustom org-refile-targets nil
  1269. "Targets for refiling entries with \\[org-refile].
  1270. This is list of cons cells. Each cell contains:
  1271. - a specification of the files to be considered, either a list of files,
  1272. or a symbol whose function or variable value will be used to retrieve
  1273. a file name or a list of file names. If you use `org-agenda-files' for
  1274. that, all agenda files will be scanned for targets. Nil means, consider
  1275. headings in the current buffer.
  1276. - A specification of how to find candidate refile targets. This may be
  1277. any of:
  1278. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1279. This tag has to be present in all target headlines, inheritance will
  1280. not be considered.
  1281. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1282. todo keyword.
  1283. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1284. headlines that are refiling targets.
  1285. - a cons cell (:level . N). Any headline of level N is considered a target.
  1286. Note that, when `org-odd-levels-only' is set, level corresponds to
  1287. order in hierarchy, not to the number of stars.
  1288. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1289. Note that, when `org-odd-levels-only' is set, level corresponds to
  1290. order in hierarchy, not to the number of stars.
  1291. When this variable is nil, all top-level headlines in the current buffer
  1292. are used, equivalent to the value `((nil . (:level . 1))'."
  1293. :group 'org-refile
  1294. :type '(repeat
  1295. (cons
  1296. (choice :value org-agenda-files
  1297. (const :tag "All agenda files" org-agenda-files)
  1298. (const :tag "Current buffer" nil)
  1299. (function) (variable) (file))
  1300. (choice :tag "Identify target headline by"
  1301. (cons :tag "Specific tag" (const :value :tag) (string))
  1302. (cons :tag "TODO keyword" (const :value :todo) (string))
  1303. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1304. (cons :tag "Level number" (const :value :level) (integer))
  1305. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1306. (defcustom org-refile-use-outline-path nil
  1307. "Non-nil means, provide refile targets as paths.
  1308. So a level 3 headline will be available as level1/level2/level3.
  1309. When the value is `file', also include the file name (without directory)
  1310. into the path. When `full-file-path', include the full file path."
  1311. :group 'org-refile
  1312. :type '(choice
  1313. (const :tag "Not" nil)
  1314. (const :tag "Yes" t)
  1315. (const :tag "Start with file name" file)
  1316. (const :tag "Start with full file path" full-file-path)))
  1317. (defcustom org-outline-path-complete-in-steps t
  1318. "Non-nil means, complete the outline path in hierarchical steps.
  1319. When Org-mode uses the refile interface to select an outline path
  1320. \(see variable `org-refile-use-outline-path'), the completion of
  1321. the path can be done is a single go, or if can be done in steps down
  1322. the headline hierarchy. Going in steps is probably the best if you
  1323. do not use a special completion package like `ido' or `icicles'.
  1324. However, when using these packages, going in one step can be very
  1325. fast, while still showing the whole path to the entry."
  1326. :group 'org-refile
  1327. :type 'boolean)
  1328. (defgroup org-todo nil
  1329. "Options concerning TODO items in Org-mode."
  1330. :tag "Org TODO"
  1331. :group 'org)
  1332. (defgroup org-progress nil
  1333. "Options concerning Progress logging in Org-mode."
  1334. :tag "Org Progress"
  1335. :group 'org-time)
  1336. (defvar org-todo-interpretation-widgets
  1337. '(
  1338. (:tag "Sequence (cycling hits every state)" sequence)
  1339. (:tag "Type (cycling directly to DONE)" type))
  1340. "The available interpretation symbols for customizing
  1341. `org-todo-keywords'.
  1342. Interested libraries should add to this list.")
  1343. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1344. "List of TODO entry keyword sequences and their interpretation.
  1345. \\<org-mode-map>This is a list of sequences.
  1346. Each sequence starts with a symbol, either `sequence' or `type',
  1347. indicating if the keywords should be interpreted as a sequence of
  1348. action steps, or as different types of TODO items. The first
  1349. keywords are states requiring action - these states will select a headline
  1350. for inclusion into the global TODO list Org-mode produces. If one of
  1351. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1352. signify that no further action is necessary. If \"|\" is not found,
  1353. the last keyword is treated as the only DONE state of the sequence.
  1354. The command \\[org-todo] cycles an entry through these states, and one
  1355. additional state where no keyword is present. For details about this
  1356. cycling, see the manual.
  1357. TODO keywords and interpretation can also be set on a per-file basis with
  1358. the special #+SEQ_TODO and #+TYP_TODO lines.
  1359. Each keyword can optionally specify a character for fast state selection
  1360. \(in combination with the variable `org-use-fast-todo-selection')
  1361. and specifiers for state change logging, using the same syntax
  1362. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1363. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1364. indicates to record a time stamp each time this state is selected.
  1365. Each keyword may also specify if a timestamp or a note should be
  1366. recorded when entering or leaving the state, by adding additional
  1367. characters in the parenthesis after the keyword. This looks like this:
  1368. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1369. record only the time of the state change. With X and Y being either
  1370. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1371. Y when leaving the state if and only if the *target* state does not
  1372. define X. You may omit any of the fast-selection key or X or /Y,
  1373. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1374. For backward compatibility, this variable may also be just a list
  1375. of keywords - in this case the interpretation (sequence or type) will be
  1376. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1377. :group 'org-todo
  1378. :group 'org-keywords
  1379. :type '(choice
  1380. (repeat :tag "Old syntax, just keywords"
  1381. (string :tag "Keyword"))
  1382. (repeat :tag "New syntax"
  1383. (cons
  1384. (choice
  1385. :tag "Interpretation"
  1386. ;;Quick and dirty way to see
  1387. ;;`org-todo-interpretations'. This takes the
  1388. ;;place of item arguments
  1389. :convert-widget
  1390. (lambda (widget)
  1391. (widget-put widget
  1392. :args (mapcar
  1393. #'(lambda (x)
  1394. (widget-convert
  1395. (cons 'const x)))
  1396. org-todo-interpretation-widgets))
  1397. widget))
  1398. (repeat
  1399. (string :tag "Keyword"))))))
  1400. (defvar org-todo-keywords-1 nil
  1401. "All TODO and DONE keywords active in a buffer.")
  1402. (make-variable-buffer-local 'org-todo-keywords-1)
  1403. (defvar org-todo-keywords-for-agenda nil)
  1404. (defvar org-done-keywords-for-agenda nil)
  1405. (defvar org-todo-keyword-alist-for-agenda nil)
  1406. (defvar org-tag-alist-for-agenda nil)
  1407. (defvar org-agenda-contributing-files nil)
  1408. (defvar org-not-done-keywords nil)
  1409. (make-variable-buffer-local 'org-not-done-keywords)
  1410. (defvar org-done-keywords nil)
  1411. (make-variable-buffer-local 'org-done-keywords)
  1412. (defvar org-todo-heads nil)
  1413. (make-variable-buffer-local 'org-todo-heads)
  1414. (defvar org-todo-sets nil)
  1415. (make-variable-buffer-local 'org-todo-sets)
  1416. (defvar org-todo-log-states nil)
  1417. (make-variable-buffer-local 'org-todo-log-states)
  1418. (defvar org-todo-kwd-alist nil)
  1419. (make-variable-buffer-local 'org-todo-kwd-alist)
  1420. (defvar org-todo-key-alist nil)
  1421. (make-variable-buffer-local 'org-todo-key-alist)
  1422. (defvar org-todo-key-trigger nil)
  1423. (make-variable-buffer-local 'org-todo-key-trigger)
  1424. (defcustom org-todo-interpretation 'sequence
  1425. "Controls how TODO keywords are interpreted.
  1426. This variable is in principle obsolete and is only used for
  1427. backward compatibility, if the interpretation of todo keywords is
  1428. not given already in `org-todo-keywords'. See that variable for
  1429. more information."
  1430. :group 'org-todo
  1431. :group 'org-keywords
  1432. :type '(choice (const sequence)
  1433. (const type)))
  1434. (defcustom org-use-fast-todo-selection t
  1435. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1436. This variable describes if and under what circumstances the cycling
  1437. mechanism for TODO keywords will be replaced by a single-key, direct
  1438. selection scheme.
  1439. When nil, fast selection is never used.
  1440. When the symbol `prefix', it will be used when `org-todo' is called with
  1441. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1442. in an agenda buffer.
  1443. When t, fast selection is used by default. In this case, the prefix
  1444. argument forces cycling instead.
  1445. In all cases, the special interface is only used if access keys have actually
  1446. been assigned by the user, i.e. if keywords in the configuration are followed
  1447. by a letter in parenthesis, like TODO(t)."
  1448. :group 'org-todo
  1449. :type '(choice
  1450. (const :tag "Never" nil)
  1451. (const :tag "By default" t)
  1452. (const :tag "Only with C-u C-c C-t" prefix)))
  1453. (defcustom org-provide-todo-statistics t
  1454. "Non-nil means, update todo statistics after insert and toggle.
  1455. When this is set, todo statistics is updated in the parent of the current
  1456. entry each time a todo state is changed."
  1457. :group 'org-todo
  1458. :type 'boolean)
  1459. (defcustom org-after-todo-state-change-hook nil
  1460. "Hook which is run after the state of a TODO item was changed.
  1461. The new state (a string with a TODO keyword, or nil) is available in the
  1462. Lisp variable `state'."
  1463. :group 'org-todo
  1464. :type 'hook)
  1465. (defvar org-blocker-hook nil
  1466. "Hook for functions that are allowed to block a state change.
  1467. Each function gets as its single argument a property list, see
  1468. `org-trigger-hook' for more information about this list.
  1469. If any of the functions in this hook returns nil, the state change
  1470. is blocked.")
  1471. (defvar org-trigger-hook nil
  1472. "Hook for functions that are triggered by a state change.
  1473. Each function gets as its single argument a property list with at least
  1474. the following elements:
  1475. (:type type-of-change :position pos-at-entry-start
  1476. :from old-state :to new-state)
  1477. Depending on the type, more properties may be present.
  1478. This mechanism is currently implemented for:
  1479. TODO state changes
  1480. ------------------
  1481. :type todo-state-change
  1482. :from previous state (keyword as a string), or nil, or a symbol
  1483. 'todo' or 'done', to indicate the general type of state.
  1484. :to new state, like in :from")
  1485. (defcustom org-enforce-todo-dependencies nil
  1486. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1487. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1488. be blocked if any prior sibling is not yet done.
  1489. This variable needs to be set before org.el is loaded, and you need to
  1490. restart Emacs after a change to make the change effective. The only way
  1491. to change is while Emacs is running is through the customize interface."
  1492. :set (lambda (var val)
  1493. (set var val)
  1494. (if val
  1495. (add-hook 'org-blocker-hook
  1496. 'org-block-todo-from-children-or-siblings)
  1497. (remove-hook 'org-blocker-hook
  1498. 'org-block-todo-from-children-or-siblings)))
  1499. :group 'org-todo
  1500. :type 'boolean)
  1501. (defcustom org-enforce-todo-checkbox-dependencies nil
  1502. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1503. When this is nil, checkboxes have no influence on switching TODO states.
  1504. When non-nil, you first need to check off all check boxes before the TODO
  1505. entry can be switched to DONE.
  1506. This variable needs to be set before org.el is loaded, and you need to
  1507. restart Emacs after a change to make the change effective. The only way
  1508. to change is while Emacs is running is through the customize interface."
  1509. :set (lambda (var val)
  1510. (set var val)
  1511. (if val
  1512. (add-hook 'org-blocker-hook
  1513. 'org-block-todo-from-checkboxes)
  1514. (remove-hook 'org-blocker-hook
  1515. 'org-block-todo-from-checkboxes)))
  1516. :group 'org-todo
  1517. :type 'boolean)
  1518. (defcustom org-todo-state-tags-triggers nil
  1519. "Tag changes that should be triggered by TODO state changes.
  1520. This is a list. Each entry is
  1521. (state-change (tag . flag) .......)
  1522. State-change can be a string with a state, and empty string to indicate the
  1523. state that has no TODO keyword, or it can be one of the symbols `todo'
  1524. or `done', meaning any not-done or done state, respectively."
  1525. :group 'org-todo
  1526. :group 'org-tags
  1527. :type '(repeat
  1528. (cons (choice :tag "When changing to"
  1529. (const :tag "Not-done state" todo)
  1530. (const :tag "Done state" done)
  1531. (string :tag "State"))
  1532. (repeat
  1533. (cons :tag "Tag action"
  1534. (string :tag "Tag")
  1535. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1536. (defcustom org-log-done nil
  1537. "Information to record when a task moves to the DONE state.
  1538. Possible values are:
  1539. nil Don't add anything, just change the keyword
  1540. time Add a time stamp to the task
  1541. note Prompt a closing note and add it with template `org-log-note-headings'
  1542. This option can also be set with on a per-file-basis with
  1543. #+STARTUP: nologdone
  1544. #+STARTUP: logdone
  1545. #+STARTUP: lognotedone
  1546. You can have local logging settings for a subtree by setting the LOGGING
  1547. property to one or more of these keywords."
  1548. :group 'org-todo
  1549. :group 'org-progress
  1550. :type '(choice
  1551. (const :tag "No logging" nil)
  1552. (const :tag "Record CLOSED timestamp" time)
  1553. (const :tag "Record CLOSED timestamp with closing note." note)))
  1554. ;; Normalize old uses of org-log-done.
  1555. (cond
  1556. ((eq org-log-done t) (setq org-log-done 'time))
  1557. ((and (listp org-log-done) (memq 'done org-log-done))
  1558. (setq org-log-done 'note)))
  1559. (defcustom org-log-note-clock-out nil
  1560. "Non-nil means, record a note when clocking out of an item.
  1561. This can also be configured on a per-file basis by adding one of
  1562. the following lines anywhere in the buffer:
  1563. #+STARTUP: lognoteclock-out
  1564. #+STARTUP: nolognoteclock-out"
  1565. :group 'org-todo
  1566. :group 'org-progress
  1567. :type 'boolean)
  1568. (defcustom org-log-done-with-time t
  1569. "Non-nil means, the CLOSED time stamp will contain date and time.
  1570. When nil, only the date will be recorded."
  1571. :group 'org-progress
  1572. :type 'boolean)
  1573. (defcustom org-log-note-headings
  1574. '((done . "CLOSING NOTE %t")
  1575. (state . "State %-12s from %-12S %t")
  1576. (note . "Note taken on %t")
  1577. (clock-out . ""))
  1578. "Headings for notes added to entries.
  1579. The value is an alist, with the car being a symbol indicating the note
  1580. context, and the cdr is the heading to be used. The heading may also be the
  1581. empty string.
  1582. %t in the heading will be replaced by a time stamp.
  1583. %s will be replaced by the new TODO state, in double quotes.
  1584. %S will be replaced by the old TODO state, in double quotes.
  1585. %u will be replaced by the user name.
  1586. %U will be replaced by the full user name."
  1587. :group 'org-todo
  1588. :group 'org-progress
  1589. :type '(list :greedy t
  1590. (cons (const :tag "Heading when closing an item" done) string)
  1591. (cons (const :tag
  1592. "Heading when changing todo state (todo sequence only)"
  1593. state) string)
  1594. (cons (const :tag "Heading when just taking a note" note) string)
  1595. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1596. (unless (assq 'note org-log-note-headings)
  1597. (push '(note . "%t") org-log-note-headings))
  1598. (defcustom org-log-into-drawer nil
  1599. "Non-nil means, insert state change notes and time stamps into a drawer.
  1600. When nil, state changes notes will be inserted after the headline and
  1601. any scheduling and clock lines, but not inside a drawer.
  1602. The value of this variable should be the name of the drawer to use.
  1603. LOGBOOK is proposed at the default drawer for this purpose, you can
  1604. also set this to a string to define the drawer of your choice.
  1605. A value of t is also allowed, representing \"LOGBOOK\".
  1606. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1607. will be ognored."
  1608. :group 'org-todo
  1609. :group 'org-progress
  1610. :type '(choice
  1611. (const :tag "Not into a drawer" nil)
  1612. (const :tag "LOGBOOK" t)
  1613. (string :tag "Other")))
  1614. (if (fboundp 'defvaralias)
  1615. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1616. (defcustom org-log-state-notes-insert-after-drawers nil
  1617. "Non-nil means, insert state change notes after any drawers in entry.
  1618. Only the drawers that *immediately* follow the headline and the
  1619. deadline/scheduled line are skipped.
  1620. When nil, insert notes right after the heading and perhaps the line
  1621. with deadline/scheduling if present.
  1622. This variable will have no effect if `org-log-into-drawer' is
  1623. set."
  1624. :group 'org-todo
  1625. :group 'org-progress
  1626. :type 'boolean)
  1627. (defcustom org-log-states-order-reversed t
  1628. "Non-nil means, the latest state change note will be directly after heading.
  1629. When nil, the notes will be orderer according to time."
  1630. :group 'org-todo
  1631. :group 'org-progress
  1632. :type 'boolean)
  1633. (defcustom org-log-repeat 'time
  1634. "Non-nil means, record moving through the DONE state when triggering repeat.
  1635. An auto-repeating tasks is immediately switched back to TODO when marked
  1636. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1637. the TODO keyword definition, or recording a closing note by setting
  1638. `org-log-done', there will be no record of the task moving through DONE.
  1639. This variable forces taking a note anyway. Possible values are:
  1640. nil Don't force a record
  1641. time Record a time stamp
  1642. note Record a note
  1643. This option can also be set with on a per-file-basis with
  1644. #+STARTUP: logrepeat
  1645. #+STARTUP: lognoterepeat
  1646. #+STARTUP: nologrepeat
  1647. You can have local logging settings for a subtree by setting the LOGGING
  1648. property to one or more of these keywords."
  1649. :group 'org-todo
  1650. :group 'org-progress
  1651. :type '(choice
  1652. (const :tag "Don't force a record" nil)
  1653. (const :tag "Force recording the DONE state" time)
  1654. (const :tag "Force recording a note with the DONE state" note)))
  1655. (defgroup org-priorities nil
  1656. "Priorities in Org-mode."
  1657. :tag "Org Priorities"
  1658. :group 'org-todo)
  1659. (defcustom org-highest-priority ?A
  1660. "The highest priority of TODO items. A character like ?A, ?B etc.
  1661. Must have a smaller ASCII number than `org-lowest-priority'."
  1662. :group 'org-priorities
  1663. :type 'character)
  1664. (defcustom org-lowest-priority ?C
  1665. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1666. Must have a larger ASCII number than `org-highest-priority'."
  1667. :group 'org-priorities
  1668. :type 'character)
  1669. (defcustom org-default-priority ?B
  1670. "The default priority of TODO items.
  1671. This is the priority an item get if no explicit priority is given."
  1672. :group 'org-priorities
  1673. :type 'character)
  1674. (defcustom org-priority-start-cycle-with-default t
  1675. "Non-nil means, start with default priority when starting to cycle.
  1676. When this is nil, the first step in the cycle will be (depending on the
  1677. command used) one higher or lower that the default priority."
  1678. :group 'org-priorities
  1679. :type 'boolean)
  1680. (defgroup org-time nil
  1681. "Options concerning time stamps and deadlines in Org-mode."
  1682. :tag "Org Time"
  1683. :group 'org)
  1684. (defcustom org-insert-labeled-timestamps-at-point nil
  1685. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1686. When nil, these labeled time stamps are forces into the second line of an
  1687. entry, just after the headline. When scheduling from the global TODO list,
  1688. the time stamp will always be forced into the second line."
  1689. :group 'org-time
  1690. :type 'boolean)
  1691. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1692. "Formats for `format-time-string' which are used for time stamps.
  1693. It is not recommended to change this constant.")
  1694. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1695. "Number of minutes to round time stamps to.
  1696. These are two values, the first applies when first creating a time stamp.
  1697. The second applies when changing it with the commands `S-up' and `S-down'.
  1698. When changing the time stamp, this means that it will change in steps
  1699. of N minutes, as given by the second value.
  1700. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1701. numbers should be factors of 60, so for example 5, 10, 15.
  1702. When this is larger than 1, you can still force an exact time-stamp by using
  1703. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1704. and by using a prefix arg to `S-up/down' to specify the exact number
  1705. of minutes to shift."
  1706. :group 'org-time
  1707. :get '(lambda (var) ; Make sure all entries have 5 elements
  1708. (if (integerp (default-value var))
  1709. (list (default-value var) 5)
  1710. (default-value var)))
  1711. :type '(list
  1712. (integer :tag "when inserting times")
  1713. (integer :tag "when modifying times")))
  1714. ;; Normalize old customizations of this variable.
  1715. (when (integerp org-time-stamp-rounding-minutes)
  1716. (setq org-time-stamp-rounding-minutes
  1717. (list org-time-stamp-rounding-minutes
  1718. org-time-stamp-rounding-minutes)))
  1719. (defcustom org-display-custom-times nil
  1720. "Non-nil means, overlay custom formats over all time stamps.
  1721. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1722. To turn this on on a per-file basis, insert anywhere in the file:
  1723. #+STARTUP: customtime"
  1724. :group 'org-time
  1725. :set 'set-default
  1726. :type 'sexp)
  1727. (make-variable-buffer-local 'org-display-custom-times)
  1728. (defcustom org-time-stamp-custom-formats
  1729. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1730. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1731. These are overlayed over the default ISO format if the variable
  1732. `org-display-custom-times' is set. Time like %H:%M should be at the
  1733. end of the second format."
  1734. :group 'org-time
  1735. :type 'sexp)
  1736. (defun org-time-stamp-format (&optional long inactive)
  1737. "Get the right format for a time string."
  1738. (let ((f (if long (cdr org-time-stamp-formats)
  1739. (car org-time-stamp-formats))))
  1740. (if inactive
  1741. (concat "[" (substring f 1 -1) "]")
  1742. f)))
  1743. (defcustom org-time-clocksum-format "%d:%02d"
  1744. "The format string used when creating CLOCKSUM lines, or when
  1745. org-mode generates a time duration."
  1746. :group 'org-time
  1747. :type 'string)
  1748. (defcustom org-deadline-warning-days 14
  1749. "No. of days before expiration during which a deadline becomes active.
  1750. This variable governs the display in sparse trees and in the agenda.
  1751. When 0 or negative, it means use this number (the absolute value of it)
  1752. even if a deadline has a different individual lead time specified."
  1753. :group 'org-time
  1754. :group 'org-agenda-daily/weekly
  1755. :type 'number)
  1756. (defcustom org-read-date-prefer-future t
  1757. "Non-nil means, assume future for incomplete date input from user.
  1758. This affects the following situations:
  1759. 1. The user gives a day, but no month.
  1760. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1761. read this as the third of *next* month. However, if you enter \"17\",
  1762. it will be considered as *this* month.
  1763. 2. The user gives a month but not a year.
  1764. For example, if it is april and you enter \"feb 2\", this will be read
  1765. as feb 2, *next* year. \"May 5\", however, will be this year.
  1766. Currently this does not work for ISO week specifications.
  1767. When this option is nil, the current month and year will always be used
  1768. as defaults."
  1769. :group 'org-time
  1770. :type 'boolean)
  1771. (defcustom org-read-date-display-live t
  1772. "Non-nil means, display current interpretation of date prompt live.
  1773. This display will be in an overlay, in the minibuffer."
  1774. :group 'org-time
  1775. :type 'boolean)
  1776. (defcustom org-read-date-popup-calendar t
  1777. "Non-nil means, pop up a calendar when prompting for a date.
  1778. In the calendar, the date can be selected with mouse-1. However, the
  1779. minibuffer will also be active, and you can simply enter the date as well.
  1780. When nil, only the minibuffer will be available."
  1781. :group 'org-time
  1782. :type 'boolean)
  1783. (if (fboundp 'defvaralias)
  1784. (defvaralias 'org-popup-calendar-for-date-prompt
  1785. 'org-read-date-popup-calendar))
  1786. (defcustom org-extend-today-until 0
  1787. "The hour when your day really ends. Must be an integer.
  1788. This has influence for the following applications:
  1789. - When switching the agenda to \"today\". It it is still earlier than
  1790. the time given here, the day recognized as TODAY is actually yesterday.
  1791. - When a date is read from the user and it is still before the time given
  1792. here, the current date and time will be assumed to be yesterday, 23:59.
  1793. Also, timestamps inserted in remember templates follow this rule.
  1794. IMPORTANT: This is a feature whose implementation is and likely will
  1795. remain incomplete. Really, it is only here because past midnight seems to
  1796. be the favorite working time of John Wiegley :-)"
  1797. :group 'org-time
  1798. :type 'number)
  1799. (defcustom org-edit-timestamp-down-means-later nil
  1800. "Non-nil means, S-down will increase the time in a time stamp.
  1801. When nil, S-up will increase."
  1802. :group 'org-time
  1803. :type 'boolean)
  1804. (defcustom org-calendar-follow-timestamp-change t
  1805. "Non-nil means, make the calendar window follow timestamp changes.
  1806. When a timestamp is modified and the calendar window is visible, it will be
  1807. moved to the new date."
  1808. :group 'org-time
  1809. :type 'boolean)
  1810. (defgroup org-tags nil
  1811. "Options concerning tags in Org-mode."
  1812. :tag "Org Tags"
  1813. :group 'org)
  1814. (defcustom org-tag-alist nil
  1815. "List of tags allowed in Org-mode files.
  1816. When this list is nil, Org-mode will base TAG input on what is already in the
  1817. buffer.
  1818. The value of this variable is an alist, the car of each entry must be a
  1819. keyword as a string, the cdr may be a character that is used to select
  1820. that tag through the fast-tag-selection interface.
  1821. See the manual for details."
  1822. :group 'org-tags
  1823. :type '(repeat
  1824. (choice
  1825. (cons (string :tag "Tag name")
  1826. (character :tag "Access char"))
  1827. (const :tag "Start radio group" (:startgroup))
  1828. (const :tag "End radio group" (:endgroup))
  1829. (const :tag "New line" (:newline)))))
  1830. (defvar org-file-tags nil
  1831. "List of tags that can be inherited by all entries in the file.
  1832. The tags will be inherited if the variable `org-use-tag-inheritance'
  1833. says they should be.
  1834. This variable is populated from #+TAG lines.")
  1835. (defcustom org-use-fast-tag-selection 'auto
  1836. "Non-nil means, use fast tag selection scheme.
  1837. This is a special interface to select and deselect tags with single keys.
  1838. When nil, fast selection is never used.
  1839. When the symbol `auto', fast selection is used if and only if selection
  1840. characters for tags have been configured, either through the variable
  1841. `org-tag-alist' or through a #+TAGS line in the buffer.
  1842. When t, fast selection is always used and selection keys are assigned
  1843. automatically if necessary."
  1844. :group 'org-tags
  1845. :type '(choice
  1846. (const :tag "Always" t)
  1847. (const :tag "Never" nil)
  1848. (const :tag "When selection characters are configured" 'auto)))
  1849. (defcustom org-fast-tag-selection-single-key nil
  1850. "Non-nil means, fast tag selection exits after first change.
  1851. When nil, you have to press RET to exit it.
  1852. During fast tag selection, you can toggle this flag with `C-c'.
  1853. This variable can also have the value `expert'. In this case, the window
  1854. displaying the tags menu is not even shown, until you press C-c again."
  1855. :group 'org-tags
  1856. :type '(choice
  1857. (const :tag "No" nil)
  1858. (const :tag "Yes" t)
  1859. (const :tag "Expert" expert)))
  1860. (defvar org-fast-tag-selection-include-todo nil
  1861. "Non-nil means, fast tags selection interface will also offer TODO states.
  1862. This is an undocumented feature, you should not rely on it.")
  1863. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1864. "The column to which tags should be indented in a headline.
  1865. If this number is positive, it specifies the column. If it is negative,
  1866. it means that the tags should be flushright to that column. For example,
  1867. -80 works well for a normal 80 character screen."
  1868. :group 'org-tags
  1869. :type 'integer)
  1870. (defcustom org-auto-align-tags t
  1871. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1872. These operations change the length of a headline and therefore shift
  1873. the tags around. With this options turned on, after each such operation
  1874. the tags are again aligned to `org-tags-column'."
  1875. :group 'org-tags
  1876. :type 'boolean)
  1877. (defcustom org-use-tag-inheritance t
  1878. "Non-nil means, tags in levels apply also for sublevels.
  1879. When nil, only the tags directly given in a specific line apply there.
  1880. This may also be a list of tags that should be inherited, or a regexp that
  1881. matches tags that should be inherited. Additional control is possible
  1882. with the variable `org-tags-exclude-from-inheritance' which gives an
  1883. explicit list of tags to be excluded from inheritance., even if the value of
  1884. `org-use-tag-inheritance' would select it for inheritance.
  1885. If this option is t, a match early-on in a tree can lead to a large
  1886. number of matches in the subtree when constructing the agenda or creating
  1887. a sparse tree. If you only want to see the first match in a tree during
  1888. a search, check out the variable `org-tags-match-list-sublevels'."
  1889. :group 'org-tags
  1890. :type '(choice
  1891. (const :tag "Not" nil)
  1892. (const :tag "Always" t)
  1893. (repeat :tag "Specific tags" (string :tag "Tag"))
  1894. (regexp :tag "Tags matched by regexp")))
  1895. (defcustom org-tags-exclude-from-inheritance nil
  1896. "List of tags that should never be inherited.
  1897. This is a way to exclude a few tags from inheritance. For way to do
  1898. the opposite, to actively allow inheritance for selected tags,
  1899. see the variable `org-use-tag-inheritance'."
  1900. :group 'org-tags
  1901. :type '(repeat (string :tag "Tag")))
  1902. (defun org-tag-inherit-p (tag)
  1903. "Check if TAG is one that should be inherited."
  1904. (cond
  1905. ((member tag org-tags-exclude-from-inheritance) nil)
  1906. ((eq org-use-tag-inheritance t) t)
  1907. ((not org-use-tag-inheritance) nil)
  1908. ((stringp org-use-tag-inheritance)
  1909. (string-match org-use-tag-inheritance tag))
  1910. ((listp org-use-tag-inheritance)
  1911. (member tag org-use-tag-inheritance))
  1912. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1913. (defcustom org-tags-match-list-sublevels t
  1914. "Non-nil means list also sublevels of headlines matching tag search.
  1915. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1916. the sublevels of a headline matching a tag search often also match
  1917. the same search. Listing all of them can create very long lists.
  1918. Setting this variable to nil causes subtrees of a match to be skipped.
  1919. This option is off by default, because inheritance in on. If you turn
  1920. inheritance off, you very likely want to turn this option on.
  1921. As a special case, if the tag search is restricted to TODO items, the
  1922. value of this variable is ignored and sublevels are always checked, to
  1923. make sure all corresponding TODO items find their way into the list.
  1924. This variable is semi-obsolete and probably should always be true. It
  1925. is better to limit inheritance to certain tags using the variables
  1926. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1927. :group 'org-tags
  1928. :type 'boolean)
  1929. (defvar org-tags-history nil
  1930. "History of minibuffer reads for tags.")
  1931. (defvar org-last-tags-completion-table nil
  1932. "The last used completion table for tags.")
  1933. (defvar org-after-tags-change-hook nil
  1934. "Hook that is run after the tags in a line have changed.")
  1935. (defgroup org-properties nil
  1936. "Options concerning properties in Org-mode."
  1937. :tag "Org Properties"
  1938. :group 'org)
  1939. (defcustom org-property-format "%-10s %s"
  1940. "How property key/value pairs should be formatted by `indent-line'.
  1941. When `indent-line' hits a property definition, it will format the line
  1942. according to this format, mainly to make sure that the values are
  1943. lined-up with respect to each other."
  1944. :group 'org-properties
  1945. :type 'string)
  1946. (defcustom org-use-property-inheritance nil
  1947. "Non-nil means, properties apply also for sublevels.
  1948. This setting is chiefly used during property searches. Turning it on can
  1949. cause significant overhead when doing a search, which is why it is not
  1950. on by default.
  1951. When nil, only the properties directly given in the current entry count.
  1952. When t, every property is inherited. The value may also be a list of
  1953. properties that should have inheritance, or a regular expression matching
  1954. properties that should be inherited.
  1955. However, note that some special properties use inheritance under special
  1956. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1957. and the properties ending in \"_ALL\" when they are used as descriptor
  1958. for valid values of a property.
  1959. Note for programmers:
  1960. When querying an entry with `org-entry-get', you can control if inheritance
  1961. should be used. By default, `org-entry-get' looks only at the local
  1962. properties. You can request inheritance by setting the inherit argument
  1963. to t (to force inheritance) or to `selective' (to respect the setting
  1964. in this variable)."
  1965. :group 'org-properties
  1966. :type '(choice
  1967. (const :tag "Not" nil)
  1968. (const :tag "Always" t)
  1969. (repeat :tag "Specific properties" (string :tag "Property"))
  1970. (regexp :tag "Properties matched by regexp")))
  1971. (defun org-property-inherit-p (property)
  1972. "Check if PROPERTY is one that should be inherited."
  1973. (cond
  1974. ((eq org-use-property-inheritance t) t)
  1975. ((not org-use-property-inheritance) nil)
  1976. ((stringp org-use-property-inheritance)
  1977. (string-match org-use-property-inheritance property))
  1978. ((listp org-use-property-inheritance)
  1979. (member property org-use-property-inheritance))
  1980. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1981. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1982. "The default column format, if no other format has been defined.
  1983. This variable can be set on the per-file basis by inserting a line
  1984. #+COLUMNS: %25ITEM ....."
  1985. :group 'org-properties
  1986. :type 'string)
  1987. (defcustom org-columns-ellipses ".."
  1988. "The ellipses to be used when a field in column view is truncated.
  1989. When this is the empty string, as many characters as possible are shown,
  1990. but then there will be no visual indication that the field has been truncated.
  1991. When this is a string of length N, the last N characters of a truncated
  1992. field are replaced by this string. If the column is narrower than the
  1993. ellipses string, only part of the ellipses string will be shown."
  1994. :group 'org-properties
  1995. :type 'string)
  1996. (defcustom org-columns-modify-value-for-display-function nil
  1997. "Function that modifies values for display in column view.
  1998. For example, it can be used to cut out a certain part from a time stamp.
  1999. The function must take 2 arguments:
  2000. column-title The title of the column (*not* the property name)
  2001. value The value that should be modified.
  2002. The function should return the value that should be displayed,
  2003. or nil if the normal value should be used."
  2004. :group 'org-properties
  2005. :type 'function)
  2006. (defcustom org-effort-property "Effort"
  2007. "The property that is being used to keep track of effort estimates.
  2008. Effort estimates given in this property need to have the format H:MM."
  2009. :group 'org-properties
  2010. :group 'org-progress
  2011. :type '(string :tag "Property"))
  2012. (defconst org-global-properties-fixed
  2013. '(("VISIBILITY_ALL" . "folded children content all"))
  2014. "List of property/value pairs that can be inherited by any entry.
  2015. These are fixed values, for the preset properties. The user variable
  2016. that can be used to add to this list is `org-global-properties'.
  2017. The entries in this list are cons cells where the car is a property
  2018. name and cdr is a string with the value. If the value represents
  2019. multiple items like an \"_ALL\" property, separate the items by
  2020. spaces.")
  2021. (defcustom org-global-properties nil
  2022. "List of property/value pairs that can be inherited by any entry.
  2023. This list will be combined with the constant `org-global-properties-fixed'.
  2024. The entries in this list are cons cells where the car is a property
  2025. name and cdr is a string with the value.
  2026. You can set buffer-local values for the same purpose in the variable
  2027. `org-file-properties' this by adding lines like
  2028. #+PROPERTY: NAME VALUE"
  2029. :group 'org-properties
  2030. :type '(repeat
  2031. (cons (string :tag "Property")
  2032. (string :tag "Value"))))
  2033. (defvar org-file-properties nil
  2034. "List of property/value pairs that can be inherited by any entry.
  2035. Valid for the current buffer.
  2036. This variable is populated from #+PROPERTY lines.")
  2037. (make-variable-buffer-local 'org-file-properties)
  2038. (defgroup org-agenda nil
  2039. "Options concerning agenda views in Org-mode."
  2040. :tag "Org Agenda"
  2041. :group 'org)
  2042. (defvar org-category nil
  2043. "Variable used by org files to set a category for agenda display.
  2044. Such files should use a file variable to set it, for example
  2045. # -*- mode: org; org-category: \"ELisp\"
  2046. or contain a special line
  2047. #+CATEGORY: ELisp
  2048. If the file does not specify a category, then file's base name
  2049. is used instead.")
  2050. (make-variable-buffer-local 'org-category)
  2051. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2052. (defcustom org-agenda-files nil
  2053. "The files to be used for agenda display.
  2054. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2055. \\[org-remove-file]. You can also use customize to edit the list.
  2056. If an entry is a directory, all files in that directory that are matched by
  2057. `org-agenda-file-regexp' will be part of the file list.
  2058. If the value of the variable is not a list but a single file name, then
  2059. the list of agenda files is actually stored and maintained in that file, one
  2060. agenda file per line."
  2061. :group 'org-agenda
  2062. :type '(choice
  2063. (repeat :tag "List of files and directories" file)
  2064. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2065. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2066. "Regular expression to match files for `org-agenda-files'.
  2067. If any element in the list in that variable contains a directory instead
  2068. of a normal file, all files in that directory that are matched by this
  2069. regular expression will be included."
  2070. :group 'org-agenda
  2071. :type 'regexp)
  2072. (defcustom org-agenda-text-search-extra-files nil
  2073. "List of extra files to be searched by text search commands.
  2074. These files will be search in addition to the agenda files by the
  2075. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2076. Note that these files will only be searched for text search commands,
  2077. not for the other agenda views like todo lists, tag searches or the weekly
  2078. agenda. This variable is intended to list notes and possibly archive files
  2079. that should also be searched by these two commands.
  2080. In fact, if the first element in the list is the symbol `agenda-archives',
  2081. than all archive files of all agenda files will be added to the search
  2082. scope."
  2083. :group 'org-agenda
  2084. :type '(set :greedy t
  2085. (const :tag "Agenda Archives" agenda-archives)
  2086. (repeat :inline t (file))))
  2087. (if (fboundp 'defvaralias)
  2088. (defvaralias 'org-agenda-multi-occur-extra-files
  2089. 'org-agenda-text-search-extra-files))
  2090. (defcustom org-agenda-skip-unavailable-files nil
  2091. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2092. A nil value means to remove them, after a query, from the list."
  2093. :group 'org-agenda
  2094. :type 'boolean)
  2095. (defcustom org-calendar-to-agenda-key [?c]
  2096. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2097. The command `org-calendar-goto-agenda' will be bound to this key. The
  2098. default is the character `c' because then `c' can be used to switch back and
  2099. forth between agenda and calendar."
  2100. :group 'org-agenda
  2101. :type 'sexp)
  2102. (defcustom org-calendar-agenda-action-key [?k]
  2103. "The key to be installed in `calendar-mode-map' for agenda-action.
  2104. The command `org-agenda-action' will be bound to this key. The
  2105. default is the character `k' because we use the same key in the agenda."
  2106. :group 'org-agenda
  2107. :type 'sexp)
  2108. (eval-after-load "calendar"
  2109. '(progn
  2110. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2111. 'org-calendar-goto-agenda)
  2112. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2113. 'org-agenda-action)))
  2114. (defgroup org-latex nil
  2115. "Options for embedding LaTeX code into Org-mode."
  2116. :tag "Org LaTeX"
  2117. :group 'org)
  2118. (defcustom org-format-latex-options
  2119. '(:foreground default :background default :scale 1.0
  2120. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2121. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2122. "Options for creating images from LaTeX fragments.
  2123. This is a property list with the following properties:
  2124. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2125. `default' means use the foreground of the default face.
  2126. :background the background color, or \"Transparent\".
  2127. `default' means use the background of the default face.
  2128. :scale a scaling factor for the size of the images.
  2129. :html-foreground, :html-background, :html-scale
  2130. the same numbers for HTML export.
  2131. :matchers a list indicating which matchers should be used to
  2132. find LaTeX fragments. Valid members of this list are:
  2133. \"begin\" find environments
  2134. \"$1\" find single characters surrounded by $.$
  2135. \"$\" find math expressions surrounded by $...$
  2136. \"$$\" find math expressions surrounded by $$....$$
  2137. \"\\(\" find math expressions surrounded by \\(...\\)
  2138. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2139. :group 'org-latex
  2140. :type 'plist)
  2141. (defcustom org-format-latex-header "\\documentclass{article}
  2142. \\usepackage{fullpage} % do not remove
  2143. \\usepackage{amssymb}
  2144. \\usepackage[usenames]{color}
  2145. \\usepackage{amsmath}
  2146. \\usepackage{latexsym}
  2147. \\usepackage[mathscr]{eucal}
  2148. \\pagestyle{empty} % do not remove"
  2149. "The document header used for processing LaTeX fragments."
  2150. :group 'org-latex
  2151. :type 'string)
  2152. (defgroup org-font-lock nil
  2153. "Font-lock settings for highlighting in Org-mode."
  2154. :tag "Org Font Lock"
  2155. :group 'org)
  2156. (defcustom org-level-color-stars-only nil
  2157. "Non-nil means fontify only the stars in each headline.
  2158. When nil, the entire headline is fontified.
  2159. Changing it requires restart of `font-lock-mode' to become effective
  2160. also in regions already fontified."
  2161. :group 'org-font-lock
  2162. :type 'boolean)
  2163. (defcustom org-hide-leading-stars nil
  2164. "Non-nil means, hide the first N-1 stars in a headline.
  2165. This works by using the face `org-hide' for these stars. This
  2166. face is white for a light background, and black for a dark
  2167. background. You may have to customize the face `org-hide' to
  2168. make this work.
  2169. Changing it requires restart of `font-lock-mode' to become effective
  2170. also in regions already fontified.
  2171. You may also set this on a per-file basis by adding one of the following
  2172. lines to the buffer:
  2173. #+STARTUP: hidestars
  2174. #+STARTUP: showstars"
  2175. :group 'org-font-lock
  2176. :type 'boolean)
  2177. (defcustom org-fontify-done-headline nil
  2178. "Non-nil means, change the face of a headline if it is marked DONE.
  2179. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2180. When this is non-nil, the headline after the keyword is set to the
  2181. `org-headline-done' as an additional indication."
  2182. :group 'org-font-lock
  2183. :type 'boolean)
  2184. (defcustom org-fontify-emphasized-text t
  2185. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2186. Changing this variable requires a restart of Emacs to take effect."
  2187. :group 'org-font-lock
  2188. :type 'boolean)
  2189. (defcustom org-highlight-latex-fragments-and-specials nil
  2190. "Non-nil means, fontify what is treated specially by the exporters."
  2191. :group 'org-font-lock
  2192. :type 'boolean)
  2193. (defcustom org-hide-emphasis-markers nil
  2194. "Non-nil mean font-lock should hide the emphasis marker characters."
  2195. :group 'org-font-lock
  2196. :type 'boolean)
  2197. (defvar org-emph-re nil
  2198. "Regular expression for matching emphasis.")
  2199. (defvar org-verbatim-re nil
  2200. "Regular expression for matching verbatim text.")
  2201. (defvar org-emphasis-regexp-components) ; defined just below
  2202. (defvar org-emphasis-alist) ; defined just below
  2203. (defun org-set-emph-re (var val)
  2204. "Set variable and compute the emphasis regular expression."
  2205. (set var val)
  2206. (when (and (boundp 'org-emphasis-alist)
  2207. (boundp 'org-emphasis-regexp-components)
  2208. org-emphasis-alist org-emphasis-regexp-components)
  2209. (let* ((e org-emphasis-regexp-components)
  2210. (pre (car e))
  2211. (post (nth 1 e))
  2212. (border (nth 2 e))
  2213. (body (nth 3 e))
  2214. (nl (nth 4 e))
  2215. (body1 (concat body "*?"))
  2216. (markers (mapconcat 'car org-emphasis-alist ""))
  2217. (vmarkers (mapconcat
  2218. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2219. org-emphasis-alist "")))
  2220. ;; make sure special characters appear at the right position in the class
  2221. (if (string-match "\\^" markers)
  2222. (setq markers (concat (replace-match "" t t markers) "^")))
  2223. (if (string-match "-" markers)
  2224. (setq markers (concat (replace-match "" t t markers) "-")))
  2225. (if (string-match "\\^" vmarkers)
  2226. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2227. (if (string-match "-" vmarkers)
  2228. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2229. (if (> nl 0)
  2230. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2231. (int-to-string nl) "\\}")))
  2232. ;; Make the regexp
  2233. (setq org-emph-re
  2234. (concat "\\([" pre "]\\|^\\)"
  2235. "\\("
  2236. "\\([" markers "]\\)"
  2237. "\\("
  2238. "[^" border "]\\|"
  2239. "[^" border "]"
  2240. body1
  2241. "[^" border "]"
  2242. "\\)"
  2243. "\\3\\)"
  2244. "\\([" post "]\\|$\\)"))
  2245. (setq org-verbatim-re
  2246. (concat "\\([" pre "]\\|^\\)"
  2247. "\\("
  2248. "\\([" vmarkers "]\\)"
  2249. "\\("
  2250. "[^" border "]\\|"
  2251. "[^" border "]"
  2252. body1
  2253. "[^" border "]"
  2254. "\\)"
  2255. "\\3\\)"
  2256. "\\([" post "]\\|$\\)")))))
  2257. (defcustom org-emphasis-regexp-components
  2258. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2259. "Components used to build the regular expression for emphasis.
  2260. This is a list with 6 entries. Terminology: In an emphasis string
  2261. like \" *strong word* \", we call the initial space PREMATCH, the final
  2262. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2263. and \"trong wor\" is the body. The different components in this variable
  2264. specify what is allowed/forbidden in each part:
  2265. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2266. post Chars allowed as postmatch. End of line will be allowed too.
  2267. border The chars *forbidden* as border characters.
  2268. body-regexp A regexp like \".\" to match a body character. Don't use
  2269. non-shy groups here, and don't allow newline here.
  2270. newline The maximum number of newlines allowed in an emphasis exp.
  2271. Use customize to modify this, or restart Emacs after changing it."
  2272. :group 'org-font-lock
  2273. :set 'org-set-emph-re
  2274. :type '(list
  2275. (sexp :tag "Allowed chars in pre ")
  2276. (sexp :tag "Allowed chars in post ")
  2277. (sexp :tag "Forbidden chars in border ")
  2278. (sexp :tag "Regexp for body ")
  2279. (integer :tag "number of newlines allowed")
  2280. (option (boolean :tag "Please ignore this button"))))
  2281. (defcustom org-emphasis-alist
  2282. `(("*" bold "<b>" "</b>")
  2283. ("/" italic "<i>" "</i>")
  2284. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2285. ("=" org-code "<code>" "</code>" verbatim)
  2286. ("~" org-verbatim "<code>" "</code>" verbatim)
  2287. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2288. "<del>" "</del>")
  2289. )
  2290. "Special syntax for emphasized text.
  2291. Text starting and ending with a special character will be emphasized, for
  2292. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2293. characters, the face to be used by font-lock for highlighting in Org-mode
  2294. Emacs buffers, and the HTML tags to be used for this.
  2295. Use customize to modify this, or restart Emacs after changing it."
  2296. :group 'org-font-lock
  2297. :set 'org-set-emph-re
  2298. :type '(repeat
  2299. (list
  2300. (string :tag "Marker character")
  2301. (choice
  2302. (face :tag "Font-lock-face")
  2303. (plist :tag "Face property list"))
  2304. (string :tag "HTML start tag")
  2305. (string :tag "HTML end tag")
  2306. (option (const verbatim)))))
  2307. ;;; Miscellaneous options
  2308. (defgroup org-completion nil
  2309. "Completion in Org-mode."
  2310. :tag "Org Completion"
  2311. :group 'org)
  2312. (defcustom org-completion-use-ido nil
  2313. "Non-nil means, use ido completion wherever possible.
  2314. Note that `ido-mode' must be active for this variable to be relevant.
  2315. If you decide to turn this variable on, you might well want to turn off
  2316. `org-outline-path-complete-in-steps'."
  2317. :group 'org-completion
  2318. :type 'boolean)
  2319. (defcustom org-completion-fallback-command 'hippie-expand
  2320. "The expansion command called by \\[org-complete] in normal context.
  2321. Normal means, no org-mode-specific context."
  2322. :group 'org-completion
  2323. :type 'function)
  2324. ;;; Functions and variables from ther packages
  2325. ;; Declared here to avoid compiler warnings
  2326. ;; XEmacs only
  2327. (defvar outline-mode-menu-heading)
  2328. (defvar outline-mode-menu-show)
  2329. (defvar outline-mode-menu-hide)
  2330. (defvar zmacs-regions) ; XEmacs regions
  2331. ;; Emacs only
  2332. (defvar mark-active)
  2333. ;; Various packages
  2334. (declare-function find-library-name "find-func" (library))
  2335. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2336. (declare-function calendar-forward-day "cal-move" (arg))
  2337. (declare-function calendar-goto-date "cal-move" (date))
  2338. (declare-function calendar-goto-today "cal-move" ())
  2339. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2340. (defvar calc-embedded-close-formula)
  2341. (defvar calc-embedded-open-formula)
  2342. (declare-function cdlatex-tab "ext:cdlatex" ())
  2343. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2344. (defvar font-lock-unfontify-region-function)
  2345. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2346. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2347. (defvar iswitchb-temp-buflist)
  2348. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2349. (defvar org-agenda-tags-todo-honor-ignore-options)
  2350. (declare-function org-agenda-skip "org-agenda" ())
  2351. (declare-function org-format-agenda-item "org-agenda"
  2352. (extra txt &optional category tags dotime noprefix remove-re))
  2353. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2354. (declare-function org-agenda-change-all-lines "org-agenda"
  2355. (newhead hdmarker &optional fixface just-this))
  2356. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2357. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2358. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2359. (beg end))
  2360. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2361. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2362. "org-agenda" (&optional end))
  2363. (declare-function parse-time-string "parse-time" (string))
  2364. (declare-function remember "remember" (&optional initial))
  2365. (declare-function remember-buffer-desc "remember" ())
  2366. (declare-function remember-finalize "remember" ())
  2367. (defvar remember-save-after-remembering)
  2368. (defvar remember-data-file)
  2369. (defvar remember-register)
  2370. (defvar remember-buffer)
  2371. (defvar remember-handler-functions)
  2372. (defvar remember-annotation-functions)
  2373. (defvar texmathp-why)
  2374. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2375. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2376. (defvar w3m-current-url)
  2377. (defvar w3m-current-title)
  2378. (defvar org-latex-regexps)
  2379. ;;; Autoload and prepare some org modules
  2380. ;; Some table stuff that needs to be defined here, because it is used
  2381. ;; by the functions setting up org-mode or checking for table context.
  2382. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2383. "Detects an org-type or table-type table.")
  2384. (defconst org-table-line-regexp "^[ \t]*|"
  2385. "Detects an org-type table line.")
  2386. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2387. "Detects an org-type table line.")
  2388. (defconst org-table-hline-regexp "^[ \t]*|-"
  2389. "Detects an org-type table hline.")
  2390. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2391. "Detects a table-type table hline.")
  2392. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2393. "Searching from within a table (any type) this finds the first line
  2394. outside the table.")
  2395. ;; Autoload the functions in org-table.el that are needed by functions here.
  2396. (eval-and-compile
  2397. (org-autoload "org-table"
  2398. '(org-table-align org-table-begin org-table-blank-field
  2399. org-table-convert org-table-convert-region org-table-copy-down
  2400. org-table-copy-region org-table-create
  2401. org-table-create-or-convert-from-region
  2402. org-table-create-with-table.el org-table-current-dline
  2403. org-table-cut-region org-table-delete-column org-table-edit-field
  2404. org-table-edit-formulas org-table-end org-table-eval-formula
  2405. org-table-export org-table-field-info
  2406. org-table-get-stored-formulas org-table-goto-column
  2407. org-table-hline-and-move org-table-import org-table-insert-column
  2408. org-table-insert-hline org-table-insert-row org-table-iterate
  2409. org-table-justify-field-maybe org-table-kill-row
  2410. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2411. org-table-move-column org-table-move-column-left
  2412. org-table-move-column-right org-table-move-row
  2413. org-table-move-row-down org-table-move-row-up
  2414. org-table-next-field org-table-next-row org-table-paste-rectangle
  2415. org-table-previous-field org-table-recalculate
  2416. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2417. org-table-toggle-coordinate-overlays
  2418. org-table-toggle-formula-debugger org-table-wrap-region
  2419. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2420. (defun org-at-table-p (&optional table-type)
  2421. "Return t if the cursor is inside an org-type table.
  2422. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2423. (if org-enable-table-editor
  2424. (save-excursion
  2425. (beginning-of-line 1)
  2426. (looking-at (if table-type org-table-any-line-regexp
  2427. org-table-line-regexp)))
  2428. nil))
  2429. (defsubst org-table-p () (org-at-table-p))
  2430. (defun org-at-table.el-p ()
  2431. "Return t if and only if we are at a table.el table."
  2432. (and (org-at-table-p 'any)
  2433. (save-excursion
  2434. (goto-char (org-table-begin 'any))
  2435. (looking-at org-table1-hline-regexp))))
  2436. (defun org-table-recognize-table.el ()
  2437. "If there is a table.el table nearby, recognize it and move into it."
  2438. (if org-table-tab-recognizes-table.el
  2439. (if (org-at-table.el-p)
  2440. (progn
  2441. (beginning-of-line 1)
  2442. (if (looking-at org-table-dataline-regexp)
  2443. nil
  2444. (if (looking-at org-table1-hline-regexp)
  2445. (progn
  2446. (beginning-of-line 2)
  2447. (if (looking-at org-table-any-border-regexp)
  2448. (beginning-of-line -1)))))
  2449. (if (re-search-forward "|" (org-table-end t) t)
  2450. (progn
  2451. (require 'table)
  2452. (if (table--at-cell-p (point))
  2453. t
  2454. (message "recognizing table.el table...")
  2455. (table-recognize-table)
  2456. (message "recognizing table.el table...done")))
  2457. (error "This should not happen..."))
  2458. t)
  2459. nil)
  2460. nil))
  2461. (defun org-at-table-hline-p ()
  2462. "Return t if the cursor is inside a hline in a table."
  2463. (if org-enable-table-editor
  2464. (save-excursion
  2465. (beginning-of-line 1)
  2466. (looking-at org-table-hline-regexp))
  2467. nil))
  2468. (defvar org-table-clean-did-remove-column nil)
  2469. (defun org-table-map-tables (function)
  2470. "Apply FUNCTION to the start of all tables in the buffer."
  2471. (save-excursion
  2472. (save-restriction
  2473. (widen)
  2474. (goto-char (point-min))
  2475. (while (re-search-forward org-table-any-line-regexp nil t)
  2476. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2477. (beginning-of-line 1)
  2478. (if (looking-at org-table-line-regexp)
  2479. (save-excursion (funcall function)))
  2480. (re-search-forward org-table-any-border-regexp nil 1))))
  2481. (message "Mapping tables: done"))
  2482. ;; Declare and autoload functions from org-exp.el
  2483. (declare-function org-default-export-plist "org-exp")
  2484. (declare-function org-infile-export-plist "org-exp")
  2485. (declare-function org-get-current-options "org-exp")
  2486. (eval-and-compile
  2487. (org-autoload "org-exp"
  2488. '(org-export org-export-as-ascii org-export-visible
  2489. org-insert-export-options-template org-export-as-html-and-open
  2490. org-export-as-html-batch org-export-as-html-to-buffer
  2491. org-replace-region-by-html org-export-region-as-html
  2492. org-export-as-html org-export-icalendar-this-file
  2493. org-export-icalendar-all-agenda-files
  2494. org-table-clean-before-export
  2495. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2496. ;; Declare and autoload functions from org-agenda.el
  2497. (eval-and-compile
  2498. (org-autoload "org-agenda"
  2499. '(org-agenda org-agenda-list org-search-view
  2500. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2501. org-diary org-agenda-to-appt
  2502. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2503. ;; Autoload org-remember
  2504. (eval-and-compile
  2505. (org-autoload "org-remember"
  2506. '(org-remember-insinuate org-remember-annotation
  2507. org-remember-apply-template org-remember org-remember-handler)))
  2508. ;; Autoload org-clock.el
  2509. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2510. (beg end))
  2511. (declare-function org-clock-update-mode-line "org-clock" ())
  2512. (defvar org-clock-start-time)
  2513. (defvar org-clock-marker (make-marker)
  2514. "Marker recording the last clock-in.")
  2515. (eval-and-compile
  2516. (org-autoload
  2517. "org-clock"
  2518. '(org-clock-in org-clock-out org-clock-cancel
  2519. org-clock-goto org-clock-sum org-clock-display
  2520. org-clock-remove-overlays org-clock-report
  2521. org-clocktable-shift org-dblock-write:clocktable
  2522. org-get-clocktable)))
  2523. (defun org-clock-update-time-maybe ()
  2524. "If this is a CLOCK line, update it and return t.
  2525. Otherwise, return nil."
  2526. (interactive)
  2527. (save-excursion
  2528. (beginning-of-line 1)
  2529. (skip-chars-forward " \t")
  2530. (when (looking-at org-clock-string)
  2531. (let ((re (concat "[ \t]*" org-clock-string
  2532. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2533. "\\([ \t]*=>.*\\)?\\)?"))
  2534. ts te h m s neg)
  2535. (cond
  2536. ((not (looking-at re))
  2537. nil)
  2538. ((not (match-end 2))
  2539. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2540. (> org-clock-marker (point))
  2541. (<= org-clock-marker (point-at-eol)))
  2542. ;; The clock is running here
  2543. (setq org-clock-start-time
  2544. (apply 'encode-time
  2545. (org-parse-time-string (match-string 1))))
  2546. (org-clock-update-mode-line)))
  2547. (t
  2548. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2549. (end-of-line 1)
  2550. (setq ts (match-string 1)
  2551. te (match-string 3))
  2552. (setq s (- (time-to-seconds
  2553. (apply 'encode-time (org-parse-time-string te)))
  2554. (time-to-seconds
  2555. (apply 'encode-time (org-parse-time-string ts))))
  2556. neg (< s 0)
  2557. s (abs s)
  2558. h (floor (/ s 3600))
  2559. s (- s (* 3600 h))
  2560. m (floor (/ s 60))
  2561. s (- s (* 60 s)))
  2562. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2563. t))))))
  2564. (defun org-check-running-clock ()
  2565. "Check if the current buffer contains the running clock.
  2566. If yes, offer to stop it and to save the buffer with the changes."
  2567. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2568. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2569. (buffer-name))))
  2570. (org-clock-out)
  2571. (when (y-or-n-p "Save changed buffer?")
  2572. (save-buffer))))
  2573. (defun org-clocktable-try-shift (dir n)
  2574. "Check if this line starts a clock table, if yes, shift the time block."
  2575. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2576. (org-clocktable-shift dir n)))
  2577. ;; Autoload org-timer.el
  2578. ;(declare-function org-timer "org-timer")
  2579. (eval-and-compile
  2580. (org-autoload
  2581. "org-timer"
  2582. '(org-timer-start org-timer org-timer-item
  2583. org-timer-change-times-in-region)))
  2584. ;; Autoload archiving code
  2585. ;; The stuff that is needed for cycling and tags has to be defined here.
  2586. (defgroup org-archive nil
  2587. "Options concerning archiving in Org-mode."
  2588. :tag "Org Archive"
  2589. :group 'org-structure)
  2590. (defcustom org-archive-location "%s_archive::"
  2591. "The location where subtrees should be archived.
  2592. The value of this variable is a string, consisting of two parts,
  2593. separated by a double-colon. The first part is a filename and
  2594. the second part is a headline.
  2595. When the filename is omitted, archiving happens in the same file.
  2596. %s in the filename will be replaced by the current file
  2597. name (without the directory part). Archiving to a different file
  2598. is useful to keep archived entries from contributing to the
  2599. Org-mode Agenda.
  2600. The archived entries will be filed as subtrees of the specified
  2601. headline. When the headline is omitted, the subtrees are simply
  2602. filed away at the end of the file, as top-level entries. Also in
  2603. the heading you can use %s to represent the file name, this can be
  2604. useful when using the same archive for a number of different files.
  2605. Here are a few examples:
  2606. \"%s_archive::\"
  2607. If the current file is Projects.org, archive in file
  2608. Projects.org_archive, as top-level trees. This is the default.
  2609. \"::* Archived Tasks\"
  2610. Archive in the current file, under the top-level headline
  2611. \"* Archived Tasks\".
  2612. \"~/org/archive.org::\"
  2613. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2614. \"~/org/archive.org::From %s\"
  2615. Archive in file ~/org/archive.org (absolute path), und headlines
  2616. \"From FILENAME\" where file name is the current file name.
  2617. \"basement::** Finished Tasks\"
  2618. Archive in file ./basement (relative path), as level 3 trees
  2619. below the level 2 heading \"** Finished Tasks\".
  2620. You may set this option on a per-file basis by adding to the buffer a
  2621. line like
  2622. #+ARCHIVE: basement::** Finished Tasks
  2623. You may also define it locally for a subtree by setting an ARCHIVE property
  2624. in the entry. If such a property is found in an entry, or anywhere up
  2625. the hierarchy, it will be used."
  2626. :group 'org-archive
  2627. :type 'string)
  2628. (defcustom org-archive-tag "ARCHIVE"
  2629. "The tag that marks a subtree as archived.
  2630. An archived subtree does not open during visibility cycling, and does
  2631. not contribute to the agenda listings.
  2632. After changing this, font-lock must be restarted in the relevant buffers to
  2633. get the proper fontification."
  2634. :group 'org-archive
  2635. :group 'org-keywords
  2636. :type 'string)
  2637. (defcustom org-agenda-skip-archived-trees t
  2638. "Non-nil means, the agenda will skip any items located in archived trees.
  2639. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2640. variable is no longer recommended, you should leave it at the value t.
  2641. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2642. :group 'org-archive
  2643. :group 'org-agenda-skip
  2644. :type 'boolean)
  2645. (defcustom org-cycle-open-archived-trees nil
  2646. "Non-nil means, `org-cycle' will open archived trees.
  2647. An archived tree is a tree marked with the tag ARCHIVE.
  2648. When nil, archived trees will stay folded. You can still open them with
  2649. normal outline commands like `show-all', but not with the cycling commands."
  2650. :group 'org-archive
  2651. :group 'org-cycle
  2652. :type 'boolean)
  2653. (defcustom org-sparse-tree-open-archived-trees nil
  2654. "Non-nil means sparse tree construction shows matches in archived trees.
  2655. When nil, matches in these trees are highlighted, but the trees are kept in
  2656. collapsed state."
  2657. :group 'org-archive
  2658. :group 'org-sparse-trees
  2659. :type 'boolean)
  2660. (defun org-cycle-hide-archived-subtrees (state)
  2661. "Re-hide all archived subtrees after a visibility state change."
  2662. (when (and (not org-cycle-open-archived-trees)
  2663. (not (memq state '(overview folded))))
  2664. (save-excursion
  2665. (let* ((globalp (memq state '(contents all)))
  2666. (beg (if globalp (point-min) (point)))
  2667. (end (if globalp (point-max) (org-end-of-subtree t))))
  2668. (org-hide-archived-subtrees beg end)
  2669. (goto-char beg)
  2670. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2671. (message "%s" (substitute-command-keys
  2672. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2673. (defun org-force-cycle-archived ()
  2674. "Cycle subtree even if it is archived."
  2675. (interactive)
  2676. (setq this-command 'org-cycle)
  2677. (let ((org-cycle-open-archived-trees t))
  2678. (call-interactively 'org-cycle)))
  2679. (defun org-hide-archived-subtrees (beg end)
  2680. "Re-hide all archived subtrees after a visibility state change."
  2681. (save-excursion
  2682. (let* ((re (concat ":" org-archive-tag ":")))
  2683. (goto-char beg)
  2684. (while (re-search-forward re end t)
  2685. (and (org-on-heading-p) (hide-subtree))
  2686. (org-end-of-subtree t)))))
  2687. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2688. (eval-and-compile
  2689. (org-autoload "org-archive"
  2690. '(org-add-archive-files org-archive-subtree
  2691. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2692. ;; Autoload Column View Code
  2693. (declare-function org-columns-number-to-string "org-colview")
  2694. (declare-function org-columns-get-format-and-top-level "org-colview")
  2695. (declare-function org-columns-compute "org-colview")
  2696. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2697. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2698. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2699. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2700. ;; Autoload ID code
  2701. (declare-function org-id-store-link "org-id")
  2702. (declare-function org-id-locations-load "org-id")
  2703. (declare-function org-id-locations-save "org-id")
  2704. (defvar org-id-track-globally)
  2705. (org-autoload "org-id"
  2706. '(org-id-get-create org-id-new org-id-copy org-id-get
  2707. org-id-get-with-outline-path-completion
  2708. org-id-get-with-outline-drilling
  2709. org-id-goto org-id-find org-id-store-link))
  2710. ;; Autoload Plotting Code
  2711. (org-autoload "org-plot"
  2712. '(org-plot/gnuplot))
  2713. ;;; Variables for pre-computed regular expressions, all buffer local
  2714. (defvar org-drawer-regexp nil
  2715. "Matches first line of a hidden block.")
  2716. (make-variable-buffer-local 'org-drawer-regexp)
  2717. (defvar org-todo-regexp nil
  2718. "Matches any of the TODO state keywords.")
  2719. (make-variable-buffer-local 'org-todo-regexp)
  2720. (defvar org-not-done-regexp nil
  2721. "Matches any of the TODO state keywords except the last one.")
  2722. (make-variable-buffer-local 'org-not-done-regexp)
  2723. (defvar org-todo-line-regexp nil
  2724. "Matches a headline and puts TODO state into group 2 if present.")
  2725. (make-variable-buffer-local 'org-todo-line-regexp)
  2726. (defvar org-complex-heading-regexp nil
  2727. "Matches a headline and puts everything into groups:
  2728. group 1: the stars
  2729. group 2: The todo keyword, maybe
  2730. group 3: Priority cookie
  2731. group 4: True headline
  2732. group 5: Tags")
  2733. (make-variable-buffer-local 'org-complex-heading-regexp)
  2734. (defvar org-todo-line-tags-regexp nil
  2735. "Matches a headline and puts TODO state into group 2 if present.
  2736. Also put tags into group 4 if tags are present.")
  2737. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2738. (defvar org-nl-done-regexp nil
  2739. "Matches newline followed by a headline with the DONE keyword.")
  2740. (make-variable-buffer-local 'org-nl-done-regexp)
  2741. (defvar org-looking-at-done-regexp nil
  2742. "Matches the DONE keyword a point.")
  2743. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2744. (defvar org-ds-keyword-length 12
  2745. "Maximum length of the Deadline and SCHEDULED keywords.")
  2746. (make-variable-buffer-local 'org-ds-keyword-length)
  2747. (defvar org-deadline-regexp nil
  2748. "Matches the DEADLINE keyword.")
  2749. (make-variable-buffer-local 'org-deadline-regexp)
  2750. (defvar org-deadline-time-regexp nil
  2751. "Matches the DEADLINE keyword together with a time stamp.")
  2752. (make-variable-buffer-local 'org-deadline-time-regexp)
  2753. (defvar org-deadline-line-regexp nil
  2754. "Matches the DEADLINE keyword and the rest of the line.")
  2755. (make-variable-buffer-local 'org-deadline-line-regexp)
  2756. (defvar org-scheduled-regexp nil
  2757. "Matches the SCHEDULED keyword.")
  2758. (make-variable-buffer-local 'org-scheduled-regexp)
  2759. (defvar org-scheduled-time-regexp nil
  2760. "Matches the SCHEDULED keyword together with a time stamp.")
  2761. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2762. (defvar org-closed-time-regexp nil
  2763. "Matches the CLOSED keyword together with a time stamp.")
  2764. (make-variable-buffer-local 'org-closed-time-regexp)
  2765. (defvar org-keyword-time-regexp nil
  2766. "Matches any of the 4 keywords, together with the time stamp.")
  2767. (make-variable-buffer-local 'org-keyword-time-regexp)
  2768. (defvar org-keyword-time-not-clock-regexp nil
  2769. "Matches any of the 3 keywords, together with the time stamp.")
  2770. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2771. (defvar org-maybe-keyword-time-regexp nil
  2772. "Matches a timestamp, possibly preceeded by a keyword.")
  2773. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2774. (defvar org-planning-or-clock-line-re nil
  2775. "Matches a line with planning or clock info.")
  2776. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2777. (defconst org-plain-time-of-day-regexp
  2778. (concat
  2779. "\\(\\<[012]?[0-9]"
  2780. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2781. "\\(--?"
  2782. "\\(\\<[012]?[0-9]"
  2783. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2784. "\\)?")
  2785. "Regular expression to match a plain time or time range.
  2786. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2787. groups carry important information:
  2788. 0 the full match
  2789. 1 the first time, range or not
  2790. 8 the second time, if it is a range.")
  2791. (defconst org-plain-time-extension-regexp
  2792. (concat
  2793. "\\(\\<[012]?[0-9]"
  2794. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2795. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2796. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2797. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2798. groups carry important information:
  2799. 0 the full match
  2800. 7 hours of duration
  2801. 9 minutes of duration")
  2802. (defconst org-stamp-time-of-day-regexp
  2803. (concat
  2804. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2805. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2806. "\\(--?"
  2807. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2808. "Regular expression to match a timestamp time or time range.
  2809. After a match, the following groups carry important information:
  2810. 0 the full match
  2811. 1 date plus weekday, for backreferencing to make sure both times on same day
  2812. 2 the first time, range or not
  2813. 4 the second time, if it is a range.")
  2814. (defconst org-startup-options
  2815. '(("fold" org-startup-folded t)
  2816. ("overview" org-startup-folded t)
  2817. ("nofold" org-startup-folded nil)
  2818. ("showall" org-startup-folded nil)
  2819. ("content" org-startup-folded content)
  2820. ("hidestars" org-hide-leading-stars t)
  2821. ("showstars" org-hide-leading-stars nil)
  2822. ("odd" org-odd-levels-only t)
  2823. ("oddeven" org-odd-levels-only nil)
  2824. ("align" org-startup-align-all-tables t)
  2825. ("noalign" org-startup-align-all-tables nil)
  2826. ("customtime" org-display-custom-times t)
  2827. ("logdone" org-log-done time)
  2828. ("lognotedone" org-log-done note)
  2829. ("nologdone" org-log-done nil)
  2830. ("lognoteclock-out" org-log-note-clock-out t)
  2831. ("nolognoteclock-out" org-log-note-clock-out nil)
  2832. ("logrepeat" org-log-repeat state)
  2833. ("lognoterepeat" org-log-repeat note)
  2834. ("nologrepeat" org-log-repeat nil)
  2835. ("fninline" org-footnote-define-inline t)
  2836. ("nofninline" org-footnote-define-inline nil)
  2837. ("fnlocal" org-footnote-section nil)
  2838. ("fnauto" org-footnote-auto-label t)
  2839. ("fnprompt" org-footnote-auto-label nil)
  2840. ("fnconfirm" org-footnote-auto-label confirm)
  2841. ("fnplain" org-footnote-auto-label plain)
  2842. ("constcgs" constants-unit-system cgs)
  2843. ("constSI" constants-unit-system SI))
  2844. "Variable associated with STARTUP options for org-mode.
  2845. Each element is a list of three items: The startup options as written
  2846. in the #+STARTUP line, the corresponding variable, and the value to
  2847. set this variable to if the option is found. An optional forth element PUSH
  2848. means to push this value onto the list in the variable.")
  2849. (defun org-set-regexps-and-options ()
  2850. "Precompute regular expressions for current buffer."
  2851. (when (org-mode-p)
  2852. (org-set-local 'org-todo-kwd-alist nil)
  2853. (org-set-local 'org-todo-key-alist nil)
  2854. (org-set-local 'org-todo-key-trigger nil)
  2855. (org-set-local 'org-todo-keywords-1 nil)
  2856. (org-set-local 'org-done-keywords nil)
  2857. (org-set-local 'org-todo-heads nil)
  2858. (org-set-local 'org-todo-sets nil)
  2859. (org-set-local 'org-todo-log-states nil)
  2860. (org-set-local 'org-file-properties nil)
  2861. (org-set-local 'org-file-tags nil)
  2862. (let ((re (org-make-options-regexp
  2863. '("CATEGORY" "TODO" "COLUMNS"
  2864. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2865. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2866. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2867. (splitre "[ \t]+")
  2868. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2869. tail sep kws1 prio props ftags drawers
  2870. ext-setup-or-nil setup-contents (start 0))
  2871. (save-excursion
  2872. (save-restriction
  2873. (widen)
  2874. (goto-char (point-min))
  2875. (while (or (and ext-setup-or-nil
  2876. (string-match re ext-setup-or-nil start)
  2877. (setq start (match-end 0)))
  2878. (and (setq ext-setup-or-nil nil start 0)
  2879. (re-search-forward re nil t)))
  2880. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2881. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2882. (cond
  2883. ((equal key "CATEGORY")
  2884. (if (string-match "[ \t]+$" value)
  2885. (setq value (replace-match "" t t value)))
  2886. (setq cat value))
  2887. ((member key '("SEQ_TODO" "TODO"))
  2888. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2889. ((equal key "TYP_TODO")
  2890. (push (cons 'type (org-split-string value splitre)) kwds))
  2891. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2892. ;; general TODO-like setup
  2893. (push (cons (intern (downcase (match-string 1 key)))
  2894. (org-split-string value splitre)) kwds))
  2895. ((equal key "TAGS")
  2896. (setq tags (append tags (if tags '("\\n") nil)
  2897. (org-split-string value splitre))))
  2898. ((equal key "COLUMNS")
  2899. (org-set-local 'org-columns-default-format value))
  2900. ((equal key "LINK")
  2901. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2902. (push (cons (match-string 1 value)
  2903. (org-trim (match-string 2 value)))
  2904. links)))
  2905. ((equal key "PRIORITIES")
  2906. (setq prio (org-split-string value " +")))
  2907. ((equal key "PROPERTY")
  2908. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2909. (push (cons (match-string 1 value) (match-string 2 value))
  2910. props)))
  2911. ((equal key "FILETAGS")
  2912. (when (string-match "\\S-" value)
  2913. (setq ftags
  2914. (append
  2915. ftags
  2916. (apply 'append
  2917. (mapcar (lambda (x) (org-split-string x ":"))
  2918. (org-split-string value)))))))
  2919. ((equal key "DRAWERS")
  2920. (setq drawers (org-split-string value splitre)))
  2921. ((equal key "CONSTANTS")
  2922. (setq const (append const (org-split-string value splitre))))
  2923. ((equal key "STARTUP")
  2924. (let ((opts (org-split-string value splitre))
  2925. l var val)
  2926. (while (setq l (pop opts))
  2927. (when (setq l (assoc l org-startup-options))
  2928. (setq var (nth 1 l) val (nth 2 l))
  2929. (if (not (nth 3 l))
  2930. (set (make-local-variable var) val)
  2931. (if (not (listp (symbol-value var)))
  2932. (set (make-local-variable var) nil))
  2933. (set (make-local-variable var) (symbol-value var))
  2934. (add-to-list var val))))))
  2935. ((equal key "ARCHIVE")
  2936. (string-match " *$" value)
  2937. (setq arch (replace-match "" t t value))
  2938. (remove-text-properties 0 (length arch)
  2939. '(face t fontified t) arch))
  2940. ((equal key "SETUPFILE")
  2941. (setq setup-contents (org-file-contents
  2942. (expand-file-name
  2943. (org-remove-double-quotes value))
  2944. 'noerror))
  2945. (if (not ext-setup-or-nil)
  2946. (setq ext-setup-or-nil setup-contents start 0)
  2947. (setq ext-setup-or-nil
  2948. (concat (substring ext-setup-or-nil 0 start)
  2949. "\n" setup-contents "\n"
  2950. (substring ext-setup-or-nil start)))))
  2951. ))))
  2952. (when cat
  2953. (org-set-local 'org-category (intern cat))
  2954. (push (cons "CATEGORY" cat) props))
  2955. (when prio
  2956. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2957. (setq prio (mapcar 'string-to-char prio))
  2958. (org-set-local 'org-highest-priority (nth 0 prio))
  2959. (org-set-local 'org-lowest-priority (nth 1 prio))
  2960. (org-set-local 'org-default-priority (nth 2 prio)))
  2961. (and props (org-set-local 'org-file-properties (nreverse props)))
  2962. (and ftags (org-set-local 'org-file-tags ftags))
  2963. (and drawers (org-set-local 'org-drawers drawers))
  2964. (and arch (org-set-local 'org-archive-location arch))
  2965. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2966. ;; Process the TODO keywords
  2967. (unless kwds
  2968. ;; Use the global values as if they had been given locally.
  2969. (setq kwds (default-value 'org-todo-keywords))
  2970. (if (stringp (car kwds))
  2971. (setq kwds (list (cons org-todo-interpretation
  2972. (default-value 'org-todo-keywords)))))
  2973. (setq kwds (reverse kwds)))
  2974. (setq kwds (nreverse kwds))
  2975. (let (inter kws kw)
  2976. (while (setq kws (pop kwds))
  2977. (let ((kws (or
  2978. (run-hook-with-args-until-success
  2979. 'org-todo-setup-filter-hook kws)
  2980. kws)))
  2981. (setq inter (pop kws) sep (member "|" kws)
  2982. kws0 (delete "|" (copy-sequence kws))
  2983. kwsa nil
  2984. kws1 (mapcar
  2985. (lambda (x)
  2986. ;; 1 2
  2987. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2988. (progn
  2989. (setq kw (match-string 1 x)
  2990. key (and (match-end 2) (match-string 2 x))
  2991. log (org-extract-log-state-settings x))
  2992. (push (cons kw (and key (string-to-char key))) kwsa)
  2993. (and log (push log org-todo-log-states))
  2994. kw)
  2995. (error "Invalid TODO keyword %s" x)))
  2996. kws0)
  2997. kwsa (if kwsa (append '((:startgroup))
  2998. (nreverse kwsa)
  2999. '((:endgroup))))
  3000. hw (car kws1)
  3001. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3002. tail (list inter hw (car dws) (org-last dws))))
  3003. (add-to-list 'org-todo-heads hw 'append)
  3004. (push kws1 org-todo-sets)
  3005. (setq org-done-keywords (append org-done-keywords dws nil))
  3006. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3007. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3008. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3009. (setq org-todo-sets (nreverse org-todo-sets)
  3010. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3011. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3012. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3013. ;; Process the constants
  3014. (when const
  3015. (let (e cst)
  3016. (while (setq e (pop const))
  3017. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3018. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3019. (setq org-table-formula-constants-local cst)))
  3020. ;; Process the tags.
  3021. (when tags
  3022. (let (e tgs)
  3023. (while (setq e (pop tags))
  3024. (cond
  3025. ((equal e "{") (push '(:startgroup) tgs))
  3026. ((equal e "}") (push '(:endgroup) tgs))
  3027. ((equal e "\\n") (push '(:newline) tgs))
  3028. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3029. (push (cons (match-string 1 e)
  3030. (string-to-char (match-string 2 e)))
  3031. tgs))
  3032. (t (push (list e) tgs))))
  3033. (org-set-local 'org-tag-alist nil)
  3034. (while (setq e (pop tgs))
  3035. (or (and (stringp (car e))
  3036. (assoc (car e) org-tag-alist))
  3037. (push e org-tag-alist)))))
  3038. ;; Compute the regular expressions and other local variables
  3039. (if (not org-done-keywords)
  3040. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3041. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3042. (length org-scheduled-string)
  3043. (length org-clock-string)
  3044. (length org-closed-string)))
  3045. org-drawer-regexp
  3046. (concat "^[ \t]*:\\("
  3047. (mapconcat 'regexp-quote org-drawers "\\|")
  3048. "\\):[ \t]*$")
  3049. org-not-done-keywords
  3050. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3051. org-todo-regexp
  3052. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3053. "\\|") "\\)\\>")
  3054. org-not-done-regexp
  3055. (concat "\\<\\("
  3056. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3057. "\\)\\>")
  3058. org-todo-line-regexp
  3059. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3060. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3061. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3062. org-complex-heading-regexp
  3063. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3064. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3065. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3066. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3067. org-nl-done-regexp
  3068. (concat "\n\\*+[ \t]+"
  3069. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3070. "\\)" "\\>")
  3071. org-todo-line-tags-regexp
  3072. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3073. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3074. (org-re
  3075. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3076. org-looking-at-done-regexp
  3077. (concat "^" "\\(?:"
  3078. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3079. "\\>")
  3080. org-deadline-regexp (concat "\\<" org-deadline-string)
  3081. org-deadline-time-regexp
  3082. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3083. org-deadline-line-regexp
  3084. (concat "\\<\\(" org-deadline-string "\\).*")
  3085. org-scheduled-regexp
  3086. (concat "\\<" org-scheduled-string)
  3087. org-scheduled-time-regexp
  3088. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3089. org-closed-time-regexp
  3090. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3091. org-keyword-time-regexp
  3092. (concat "\\<\\(" org-scheduled-string
  3093. "\\|" org-deadline-string
  3094. "\\|" org-closed-string
  3095. "\\|" org-clock-string "\\)"
  3096. " *[[<]\\([^]>]+\\)[]>]")
  3097. org-keyword-time-not-clock-regexp
  3098. (concat "\\<\\(" org-scheduled-string
  3099. "\\|" org-deadline-string
  3100. "\\|" org-closed-string
  3101. "\\)"
  3102. " *[[<]\\([^]>]+\\)[]>]")
  3103. org-maybe-keyword-time-regexp
  3104. (concat "\\(\\<\\(" org-scheduled-string
  3105. "\\|" org-deadline-string
  3106. "\\|" org-closed-string
  3107. "\\|" org-clock-string "\\)\\)?"
  3108. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3109. org-planning-or-clock-line-re
  3110. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3111. "\\|" org-deadline-string
  3112. "\\|" org-closed-string "\\|" org-clock-string
  3113. "\\)\\>\\)")
  3114. )
  3115. (org-compute-latex-and-specials-regexp)
  3116. (org-set-font-lock-defaults))))
  3117. (defun org-file-contents (file &optional noerror)
  3118. "Return the contents of FILE, as a string."
  3119. (if (or (not file)
  3120. (not (file-readable-p file)))
  3121. (if noerror
  3122. (progn
  3123. (message "Cannot read file %s" file)
  3124. (ding) (sit-for 2)
  3125. "")
  3126. (error "Cannot read file %s" file))
  3127. (with-temp-buffer
  3128. (insert-file-contents file)
  3129. (buffer-string))))
  3130. (defun org-extract-log-state-settings (x)
  3131. "Extract the log state setting from a TODO keyword string.
  3132. This will extract info from a string like \"WAIT(w@/!)\"."
  3133. (let (kw key log1 log2)
  3134. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3135. (setq kw (match-string 1 x)
  3136. key (and (match-end 2) (match-string 2 x))
  3137. log1 (and (match-end 3) (match-string 3 x))
  3138. log2 (and (match-end 4) (match-string 4 x)))
  3139. (and (or log1 log2)
  3140. (list kw
  3141. (and log1 (if (equal log1 "!") 'time 'note))
  3142. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3143. (defun org-remove-keyword-keys (list)
  3144. "Remove a pair of parenthesis at the end of each string in LIST."
  3145. (mapcar (lambda (x)
  3146. (if (string-match "(.*)$" x)
  3147. (substring x 0 (match-beginning 0))
  3148. x))
  3149. list))
  3150. ;; FIXME: this could be done much better, using second characters etc.
  3151. (defun org-assign-fast-keys (alist)
  3152. "Assign fast keys to a keyword-key alist.
  3153. Respect keys that are already there."
  3154. (let (new e k c c1 c2 (char ?a))
  3155. (while (setq e (pop alist))
  3156. (cond
  3157. ((equal e '(:startgroup)) (push e new))
  3158. ((equal e '(:endgroup)) (push e new))
  3159. ((equal e '(:newline)) (push e new))
  3160. (t
  3161. (setq k (car e) c2 nil)
  3162. (if (cdr e)
  3163. (setq c (cdr e))
  3164. ;; automatically assign a character.
  3165. (setq c1 (string-to-char
  3166. (downcase (substring
  3167. k (if (= (string-to-char k) ?@) 1 0)))))
  3168. (if (or (rassoc c1 new) (rassoc c1 alist))
  3169. (while (or (rassoc char new) (rassoc char alist))
  3170. (setq char (1+ char)))
  3171. (setq c2 c1))
  3172. (setq c (or c2 char)))
  3173. (push (cons k c) new))))
  3174. (nreverse new)))
  3175. ;;; Some variables used in various places
  3176. (defvar org-window-configuration nil
  3177. "Used in various places to store a window configuration.")
  3178. (defvar org-finish-function nil
  3179. "Function to be called when `C-c C-c' is used.
  3180. This is for getting out of special buffers like remember.")
  3181. ;; FIXME: Occasionally check by commenting these, to make sure
  3182. ;; no other functions uses these, forgetting to let-bind them.
  3183. (defvar entry)
  3184. (defvar state)
  3185. (defvar last-state)
  3186. (defvar date)
  3187. (defvar description)
  3188. ;; Defined somewhere in this file, but used before definition.
  3189. (defvar org-html-entities)
  3190. (defvar org-struct-menu)
  3191. (defvar org-org-menu)
  3192. (defvar org-tbl-menu)
  3193. (defvar org-agenda-keymap)
  3194. ;;;; Define the Org-mode
  3195. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3196. (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."))
  3197. ;; We use a before-change function to check if a table might need
  3198. ;; an update.
  3199. (defvar org-table-may-need-update t
  3200. "Indicates that a table might need an update.
  3201. This variable is set by `org-before-change-function'.
  3202. `org-table-align' sets it back to nil.")
  3203. (defun org-before-change-function (beg end)
  3204. "Every change indicates that a table might need an update."
  3205. (setq org-table-may-need-update t))
  3206. (defvar org-mode-map)
  3207. (defvar org-mode-hook nil
  3208. "Mode hook for Org-mode, run after the mode was turned on.")
  3209. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3210. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3211. (defvar org-table-buffer-is-an nil)
  3212. (defconst org-outline-regexp "\\*+ ")
  3213. ;;;###autoload
  3214. (define-derived-mode org-mode outline-mode "Org"
  3215. "Outline-based notes management and organizer, alias
  3216. \"Carsten's outline-mode for keeping track of everything.\"
  3217. Org-mode develops organizational tasks around a NOTES file which
  3218. contains information about projects as plain text. Org-mode is
  3219. implemented on top of outline-mode, which is ideal to keep the content
  3220. of large files well structured. It supports ToDo items, deadlines and
  3221. time stamps, which magically appear in the diary listing of the Emacs
  3222. calendar. Tables are easily created with a built-in table editor.
  3223. Plain text URL-like links connect to websites, emails (VM), Usenet
  3224. messages (Gnus), BBDB entries, and any files related to the project.
  3225. For printing and sharing of notes, an Org-mode file (or a part of it)
  3226. can be exported as a structured ASCII or HTML file.
  3227. The following commands are available:
  3228. \\{org-mode-map}"
  3229. ;; Get rid of Outline menus, they are not needed
  3230. ;; Need to do this here because define-derived-mode sets up
  3231. ;; the keymap so late. Still, it is a waste to call this each time
  3232. ;; we switch another buffer into org-mode.
  3233. (if (featurep 'xemacs)
  3234. (when (boundp 'outline-mode-menu-heading)
  3235. ;; Assume this is Greg's port, it used easymenu
  3236. (easy-menu-remove outline-mode-menu-heading)
  3237. (easy-menu-remove outline-mode-menu-show)
  3238. (easy-menu-remove outline-mode-menu-hide))
  3239. (define-key org-mode-map [menu-bar headings] 'undefined)
  3240. (define-key org-mode-map [menu-bar hide] 'undefined)
  3241. (define-key org-mode-map [menu-bar show] 'undefined))
  3242. (org-load-modules-maybe)
  3243. (easy-menu-add org-org-menu)
  3244. (easy-menu-add org-tbl-menu)
  3245. (org-install-agenda-files-menu)
  3246. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3247. (org-add-to-invisibility-spec '(org-cwidth))
  3248. (when (featurep 'xemacs)
  3249. (org-set-local 'line-move-ignore-invisible t))
  3250. (org-set-local 'outline-regexp org-outline-regexp)
  3251. (org-set-local 'outline-level 'org-outline-level)
  3252. (when (and org-ellipsis
  3253. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3254. (fboundp 'make-glyph-code))
  3255. (unless org-display-table
  3256. (setq org-display-table (make-display-table)))
  3257. (set-display-table-slot
  3258. org-display-table 4
  3259. (vconcat (mapcar
  3260. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3261. org-ellipsis)))
  3262. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3263. (setq buffer-display-table org-display-table))
  3264. (org-set-regexps-and-options)
  3265. (when (and org-tag-faces (not org-tags-special-faces-re))
  3266. ;; tag faces set outside customize.... force initialization.
  3267. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3268. ;; Calc embedded
  3269. (org-set-local 'calc-embedded-open-mode "# ")
  3270. (modify-syntax-entry ?# "<")
  3271. (modify-syntax-entry ?@ "w")
  3272. (if org-startup-truncated (setq truncate-lines t))
  3273. (org-set-local 'font-lock-unfontify-region-function
  3274. 'org-unfontify-region)
  3275. ;; Activate before-change-function
  3276. (org-set-local 'org-table-may-need-update t)
  3277. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3278. 'local)
  3279. ;; Check for running clock before killing a buffer
  3280. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3281. ;; Paragraphs and auto-filling
  3282. (org-set-autofill-regexps)
  3283. (setq indent-line-function 'org-indent-line-function)
  3284. (org-update-radio-target-regexp)
  3285. ;; Make sure dependence stuff works reliably, even for users who set it
  3286. ;; too late :-(
  3287. (if org-enforce-todo-dependencies
  3288. (add-hook 'org-blocker-hook
  3289. 'org-block-todo-from-children-or-siblings)
  3290. (remove-hook 'org-blocker-hook
  3291. 'org-block-todo-from-children-or-siblings))
  3292. (if org-enforce-todo-checkbox-dependencies
  3293. (add-hook 'org-blocker-hook
  3294. 'org-block-todo-from-checkboxes)
  3295. (remove-hook 'org-blocker-hook
  3296. 'org-block-todo-from-checkboxes))
  3297. ;; Comment characters
  3298. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3299. (org-set-local 'comment-padding " ")
  3300. ;; Align options lines
  3301. (org-set-local
  3302. 'align-mode-rules-list
  3303. '((org-in-buffer-settings
  3304. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3305. (modes . '(org-mode)))))
  3306. ;; Imenu
  3307. (org-set-local 'imenu-create-index-function
  3308. 'org-imenu-get-tree)
  3309. ;; Make isearch reveal context
  3310. (if (or (featurep 'xemacs)
  3311. (not (boundp 'outline-isearch-open-invisible-function)))
  3312. ;; Emacs 21 and XEmacs make use of the hook
  3313. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3314. ;; Emacs 22 deals with this through a special variable
  3315. (org-set-local 'outline-isearch-open-invisible-function
  3316. (lambda (&rest ignore) (org-show-context 'isearch))))
  3317. ;; If empty file that did not turn on org-mode automatically, make it to.
  3318. (if (and org-insert-mode-line-in-empty-file
  3319. (interactive-p)
  3320. (= (point-min) (point-max)))
  3321. (insert "# -*- mode: org -*-\n\n"))
  3322. (unless org-inhibit-startup
  3323. (when org-startup-align-all-tables
  3324. (let ((bmp (buffer-modified-p)))
  3325. (org-table-map-tables 'org-table-align)
  3326. (set-buffer-modified-p bmp)))
  3327. (org-set-startup-visibility)))
  3328. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3329. (defun org-current-time ()
  3330. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3331. (if (> (car org-time-stamp-rounding-minutes) 1)
  3332. (let ((r (car org-time-stamp-rounding-minutes))
  3333. (time (decode-time)))
  3334. (apply 'encode-time
  3335. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3336. (nthcdr 2 time))))
  3337. (current-time)))
  3338. ;;;; Font-Lock stuff, including the activators
  3339. (defvar org-mouse-map (make-sparse-keymap))
  3340. (org-defkey org-mouse-map
  3341. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3342. (org-defkey org-mouse-map
  3343. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3344. (when org-mouse-1-follows-link
  3345. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3346. (when org-tab-follows-link
  3347. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3348. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3349. (require 'font-lock)
  3350. (defconst org-non-link-chars "]\t\n\r<>")
  3351. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3352. "shell" "elisp"))
  3353. (defvar org-link-types-re nil
  3354. "Matches a link that has a url-like prefix like \"http:\"")
  3355. (defvar org-link-re-with-space nil
  3356. "Matches a link with spaces, optional angular brackets around it.")
  3357. (defvar org-link-re-with-space2 nil
  3358. "Matches a link with spaces, optional angular brackets around it.")
  3359. (defvar org-link-re-with-space3 nil
  3360. "Matches a link with spaces, only for internal part in bracket links.")
  3361. (defvar org-angle-link-re nil
  3362. "Matches link with angular brackets, spaces are allowed.")
  3363. (defvar org-plain-link-re nil
  3364. "Matches plain link, without spaces.")
  3365. (defvar org-bracket-link-regexp nil
  3366. "Matches a link in double brackets.")
  3367. (defvar org-bracket-link-analytic-regexp nil
  3368. "Regular expression used to analyze links.
  3369. Here is what the match groups contain after a match:
  3370. 1: http:
  3371. 2: http
  3372. 3: path
  3373. 4: [desc]
  3374. 5: desc")
  3375. (defvar org-bracket-link-analytic-regexp++ nil
  3376. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3377. (defvar org-any-link-re nil
  3378. "Regular expression matching any link.")
  3379. (defun org-make-link-regexps ()
  3380. "Update the link regular expressions.
  3381. This should be called after the variable `org-link-types' has changed."
  3382. (setq org-link-types-re
  3383. (concat
  3384. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3385. org-link-re-with-space
  3386. (concat
  3387. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3388. "\\([^" org-non-link-chars " ]"
  3389. "[^" org-non-link-chars "]*"
  3390. "[^" org-non-link-chars " ]\\)>?")
  3391. org-link-re-with-space2
  3392. (concat
  3393. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3394. "\\([^" org-non-link-chars " ]"
  3395. "[^\t\n\r]*"
  3396. "[^" org-non-link-chars " ]\\)>?")
  3397. org-link-re-with-space3
  3398. (concat
  3399. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3400. "\\([^" org-non-link-chars " ]"
  3401. "[^\t\n\r]*\\)")
  3402. org-angle-link-re
  3403. (concat
  3404. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3405. "\\([^" org-non-link-chars " ]"
  3406. "[^" org-non-link-chars "]*"
  3407. "\\)>")
  3408. org-plain-link-re
  3409. (concat
  3410. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3411. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3412. org-bracket-link-regexp
  3413. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3414. org-bracket-link-analytic-regexp
  3415. (concat
  3416. "\\[\\["
  3417. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3418. "\\([^]]+\\)"
  3419. "\\]"
  3420. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3421. "\\]")
  3422. org-bracket-link-analytic-regexp++
  3423. (concat
  3424. "\\[\\["
  3425. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3426. "\\([^]]+\\)"
  3427. "\\]"
  3428. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3429. "\\]")
  3430. org-any-link-re
  3431. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3432. org-angle-link-re "\\)\\|\\("
  3433. org-plain-link-re "\\)")))
  3434. (org-make-link-regexps)
  3435. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3436. "Regular expression for fast time stamp matching.")
  3437. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3438. "Regular expression for fast time stamp matching.")
  3439. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3440. "Regular expression matching time strings for analysis.
  3441. This one does not require the space after the date, so it can be used
  3442. on a string that terminates immediately after the date.")
  3443. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3444. "Regular expression matching time strings for analysis.")
  3445. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3446. "Regular expression matching time stamps, with groups.")
  3447. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3448. "Regular expression matching time stamps (also [..]), with groups.")
  3449. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3450. "Regular expression matching a time stamp range.")
  3451. (defconst org-tr-regexp-both
  3452. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3453. "Regular expression matching a time stamp range.")
  3454. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3455. org-ts-regexp "\\)?")
  3456. "Regular expression matching a time stamp or time stamp range.")
  3457. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3458. org-ts-regexp-both "\\)?")
  3459. "Regular expression matching a time stamp or time stamp range.
  3460. The time stamps may be either active or inactive.")
  3461. (defvar org-emph-face nil)
  3462. (defun org-do-emphasis-faces (limit)
  3463. "Run through the buffer and add overlays to links."
  3464. (let (rtn)
  3465. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3466. (if (not (= (char-after (match-beginning 3))
  3467. (char-after (match-beginning 4))))
  3468. (progn
  3469. (setq rtn t)
  3470. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3471. 'face
  3472. (nth 1 (assoc (match-string 3)
  3473. org-emphasis-alist)))
  3474. (add-text-properties (match-beginning 2) (match-end 2)
  3475. '(font-lock-multiline t))
  3476. (when org-hide-emphasis-markers
  3477. (add-text-properties (match-end 4) (match-beginning 5)
  3478. '(invisible org-link))
  3479. (add-text-properties (match-beginning 3) (match-end 3)
  3480. '(invisible org-link)))))
  3481. (backward-char 1))
  3482. rtn))
  3483. (defun org-emphasize (&optional char)
  3484. "Insert or change an emphasis, i.e. a font like bold or italic.
  3485. If there is an active region, change that region to a new emphasis.
  3486. If there is no region, just insert the marker characters and position
  3487. the cursor between them.
  3488. CHAR should be either the marker character, or the first character of the
  3489. HTML tag associated with that emphasis. If CHAR is a space, the means
  3490. to remove the emphasis of the selected region.
  3491. If char is not given (for example in an interactive call) it
  3492. will be prompted for."
  3493. (interactive)
  3494. (let ((eal org-emphasis-alist) e det
  3495. (erc org-emphasis-regexp-components)
  3496. (prompt "")
  3497. (string "") beg end move tag c s)
  3498. (if (org-region-active-p)
  3499. (setq beg (region-beginning) end (region-end)
  3500. string (buffer-substring beg end))
  3501. (setq move t))
  3502. (while (setq e (pop eal))
  3503. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3504. c (aref tag 0))
  3505. (push (cons c (string-to-char (car e))) det)
  3506. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3507. (substring tag 1)))))
  3508. (setq det (nreverse det))
  3509. (unless char
  3510. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3511. (setq char (read-char-exclusive)))
  3512. (setq char (or (cdr (assoc char det)) char))
  3513. (if (equal char ?\ )
  3514. (setq s "" move nil)
  3515. (unless (assoc (char-to-string char) org-emphasis-alist)
  3516. (error "No such emphasis marker: \"%c\"" char))
  3517. (setq s (char-to-string char)))
  3518. (while (and (> (length string) 1)
  3519. (equal (substring string 0 1) (substring string -1))
  3520. (assoc (substring string 0 1) org-emphasis-alist))
  3521. (setq string (substring string 1 -1)))
  3522. (setq string (concat s string s))
  3523. (if beg (delete-region beg end))
  3524. (unless (or (bolp)
  3525. (string-match (concat "[" (nth 0 erc) "\n]")
  3526. (char-to-string (char-before (point)))))
  3527. (insert " "))
  3528. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3529. (char-to-string (char-after (point))))
  3530. (insert " ") (backward-char 1))
  3531. (insert string)
  3532. (and move (backward-char 1))))
  3533. (defconst org-nonsticky-props
  3534. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3535. (defun org-activate-plain-links (limit)
  3536. "Run through the buffer and add overlays to links."
  3537. (catch 'exit
  3538. (let (f)
  3539. (while (re-search-forward org-plain-link-re limit t)
  3540. (setq f (get-text-property (match-beginning 0) 'face))
  3541. (if (or (eq f 'org-tag)
  3542. (and (listp f) (memq 'org-tag f)))
  3543. nil
  3544. (add-text-properties (match-beginning 0) (match-end 0)
  3545. (list 'mouse-face 'highlight
  3546. 'rear-nonsticky org-nonsticky-props
  3547. 'keymap org-mouse-map
  3548. ))
  3549. (throw 'exit t))))))
  3550. (defun org-activate-code (limit)
  3551. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3552. (progn
  3553. (remove-text-properties (match-beginning 0) (match-end 0)
  3554. '(display t invisible t intangible t))
  3555. t)))
  3556. (defun org-activate-angle-links (limit)
  3557. "Run through the buffer and add overlays to links."
  3558. (if (re-search-forward org-angle-link-re limit t)
  3559. (progn
  3560. (add-text-properties (match-beginning 0) (match-end 0)
  3561. (list 'mouse-face 'highlight
  3562. 'rear-nonsticky org-nonsticky-props
  3563. 'keymap org-mouse-map
  3564. ))
  3565. t)))
  3566. (defun org-activate-footnote-links (limit)
  3567. "Run through the buffer and add overlays to links."
  3568. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3569. limit t)
  3570. (progn
  3571. (add-text-properties (match-beginning 2) (match-end 2)
  3572. (list 'mouse-face 'highlight
  3573. 'rear-nonsticky org-nonsticky-props
  3574. 'keymap org-mouse-map
  3575. 'help-echo
  3576. (if (= (point-at-bol) (match-beginning 2))
  3577. "Footnote definition"
  3578. "Footnote reference")
  3579. ))
  3580. t)))
  3581. (defun org-activate-bracket-links (limit)
  3582. "Run through the buffer and add overlays to bracketed links."
  3583. (if (re-search-forward org-bracket-link-regexp limit t)
  3584. (let* ((help (concat "LINK: "
  3585. (org-match-string-no-properties 1)))
  3586. ;; FIXME: above we should remove the escapes.
  3587. ;; but that requires another match, protecting match data,
  3588. ;; a lot of overhead for font-lock.
  3589. (ip (org-maybe-intangible
  3590. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3591. 'keymap org-mouse-map 'mouse-face 'highlight
  3592. 'font-lock-multiline t 'help-echo help)))
  3593. (vp (list 'rear-nonsticky org-nonsticky-props
  3594. 'keymap org-mouse-map 'mouse-face 'highlight
  3595. ' font-lock-multiline t 'help-echo help)))
  3596. ;; We need to remove the invisible property here. Table narrowing
  3597. ;; may have made some of this invisible.
  3598. (remove-text-properties (match-beginning 0) (match-end 0)
  3599. '(invisible nil))
  3600. (if (match-end 3)
  3601. (progn
  3602. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3603. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3604. (add-text-properties (match-end 3) (match-end 0) ip))
  3605. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3606. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3607. (add-text-properties (match-end 1) (match-end 0) ip))
  3608. t)))
  3609. (defun org-activate-dates (limit)
  3610. "Run through the buffer and add overlays to dates."
  3611. (if (re-search-forward org-tsr-regexp-both limit t)
  3612. (progn
  3613. (add-text-properties (match-beginning 0) (match-end 0)
  3614. (list 'mouse-face 'highlight
  3615. 'rear-nonsticky org-nonsticky-props
  3616. 'keymap org-mouse-map))
  3617. (when org-display-custom-times
  3618. (if (match-end 3)
  3619. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3620. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3621. t)))
  3622. (defvar org-target-link-regexp nil
  3623. "Regular expression matching radio targets in plain text.")
  3624. (make-variable-buffer-local 'org-target-link-regexp)
  3625. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3626. "Regular expression matching a link target.")
  3627. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3628. "Regular expression matching a radio target.")
  3629. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3630. "Regular expression matching any target.")
  3631. (defun org-activate-target-links (limit)
  3632. "Run through the buffer and add overlays to target matches."
  3633. (when org-target-link-regexp
  3634. (let ((case-fold-search t))
  3635. (if (re-search-forward org-target-link-regexp limit t)
  3636. (progn
  3637. (add-text-properties (match-beginning 0) (match-end 0)
  3638. (list 'mouse-face 'highlight
  3639. 'rear-nonsticky org-nonsticky-props
  3640. 'keymap org-mouse-map
  3641. 'help-echo "Radio target link"
  3642. 'org-linked-text t))
  3643. t)))))
  3644. (defun org-update-radio-target-regexp ()
  3645. "Find all radio targets in this file and update the regular expression."
  3646. (interactive)
  3647. (when (memq 'radio org-activate-links)
  3648. (setq org-target-link-regexp
  3649. (org-make-target-link-regexp (org-all-targets 'radio)))
  3650. (org-restart-font-lock)))
  3651. (defun org-hide-wide-columns (limit)
  3652. (let (s e)
  3653. (setq s (text-property-any (point) (or limit (point-max))
  3654. 'org-cwidth t))
  3655. (when s
  3656. (setq e (next-single-property-change s 'org-cwidth))
  3657. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3658. (goto-char e)
  3659. t)))
  3660. (defvar org-latex-and-specials-regexp nil
  3661. "Regular expression for highlighting export special stuff.")
  3662. (defvar org-match-substring-regexp)
  3663. (defvar org-match-substring-with-braces-regexp)
  3664. (defvar org-export-html-special-string-regexps)
  3665. (defun org-compute-latex-and-specials-regexp ()
  3666. "Compute regular expression for stuff treated specially by exporters."
  3667. (if (not org-highlight-latex-fragments-and-specials)
  3668. (org-set-local 'org-latex-and-specials-regexp nil)
  3669. (require 'org-exp)
  3670. (let*
  3671. ((matchers (plist-get org-format-latex-options :matchers))
  3672. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3673. org-latex-regexps)))
  3674. (options (org-combine-plists (org-default-export-plist)
  3675. (org-infile-export-plist)))
  3676. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3677. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3678. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3679. (org-export-html-expand (plist-get options :expand-quoted-html))
  3680. (org-export-with-special-strings (plist-get options :special-strings))
  3681. (re-sub
  3682. (cond
  3683. ((equal org-export-with-sub-superscripts '{})
  3684. (list org-match-substring-with-braces-regexp))
  3685. (org-export-with-sub-superscripts
  3686. (list org-match-substring-regexp))
  3687. (t nil)))
  3688. (re-latex
  3689. (if org-export-with-LaTeX-fragments
  3690. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3691. (re-macros
  3692. (if org-export-with-TeX-macros
  3693. (list (concat "\\\\"
  3694. (regexp-opt
  3695. (append (mapcar 'car org-html-entities)
  3696. (if (boundp 'org-latex-entities)
  3697. org-latex-entities nil))
  3698. 'words))) ; FIXME
  3699. ))
  3700. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3701. (re-special (if org-export-with-special-strings
  3702. (mapcar (lambda (x) (car x))
  3703. org-export-html-special-string-regexps)))
  3704. (re-rest
  3705. (delq nil
  3706. (list
  3707. (if org-export-html-expand "@<[^>\n]+>")
  3708. ))))
  3709. (org-set-local
  3710. 'org-latex-and-specials-regexp
  3711. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3712. re-rest) "\\|")))))
  3713. (defun org-do-latex-and-special-faces (limit)
  3714. "Run through the buffer and add overlays to links."
  3715. (when org-latex-and-specials-regexp
  3716. (let (rtn d)
  3717. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3718. limit t))
  3719. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3720. 'face))
  3721. '(org-code org-verbatim underline)))
  3722. (progn
  3723. (setq rtn t
  3724. d (cond ((member (char-after (1+ (match-beginning 0)))
  3725. '(?_ ?^)) 1)
  3726. (t 0)))
  3727. (font-lock-prepend-text-property
  3728. (+ d (match-beginning 0)) (match-end 0)
  3729. 'face 'org-latex-and-export-specials)
  3730. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3731. '(font-lock-multiline t)))))
  3732. rtn)))
  3733. (defun org-restart-font-lock ()
  3734. "Restart font-lock-mode, to force refontification."
  3735. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3736. (font-lock-mode -1)
  3737. (font-lock-mode 1)))
  3738. (defun org-all-targets (&optional radio)
  3739. "Return a list of all targets in this file.
  3740. With optional argument RADIO, only find radio targets."
  3741. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3742. rtn)
  3743. (save-excursion
  3744. (goto-char (point-min))
  3745. (while (re-search-forward re nil t)
  3746. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3747. rtn)))
  3748. (defun org-make-target-link-regexp (targets)
  3749. "Make regular expression matching all strings in TARGETS.
  3750. The regular expression finds the targets also if there is a line break
  3751. between words."
  3752. (and targets
  3753. (concat
  3754. "\\<\\("
  3755. (mapconcat
  3756. (lambda (x)
  3757. (while (string-match " +" x)
  3758. (setq x (replace-match "\\s-+" t t x)))
  3759. x)
  3760. targets
  3761. "\\|")
  3762. "\\)\\>")))
  3763. (defun org-activate-tags (limit)
  3764. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3765. (progn
  3766. (add-text-properties (match-beginning 1) (match-end 1)
  3767. (list 'mouse-face 'highlight
  3768. 'rear-nonsticky org-nonsticky-props
  3769. 'keymap org-mouse-map))
  3770. t)))
  3771. (defun org-outline-level ()
  3772. (save-excursion
  3773. (looking-at outline-regexp)
  3774. (if (match-beginning 1)
  3775. (+ (org-get-string-indentation (match-string 1)) 1000)
  3776. (1- (- (match-end 0) (match-beginning 0))))))
  3777. (defvar org-font-lock-keywords nil)
  3778. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3779. "Regular expression matching a property line.")
  3780. (defvar org-font-lock-hook nil
  3781. "Functions to be called for special font lock stuff.")
  3782. (defun org-font-lock-hook (limit)
  3783. (run-hook-with-args 'org-font-lock-hook limit))
  3784. (defun org-set-font-lock-defaults ()
  3785. (let* ((em org-fontify-emphasized-text)
  3786. (lk org-activate-links)
  3787. (org-font-lock-extra-keywords
  3788. (list
  3789. ;; Call the hook
  3790. '(org-font-lock-hook)
  3791. ;; Headlines
  3792. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3793. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3794. ;; Table lines
  3795. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3796. (1 'org-table t))
  3797. ;; Table internals
  3798. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3799. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3800. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3801. ;; Drawers
  3802. (list org-drawer-regexp '(0 'org-special-keyword t))
  3803. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3804. ;; Properties
  3805. (list org-property-re
  3806. '(1 'org-special-keyword t)
  3807. '(3 'org-property-value t))
  3808. (if org-format-transports-properties-p
  3809. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3810. ;; Links
  3811. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3812. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3813. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3814. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3815. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3816. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3817. (if (memq 'footnote lk) '(org-activate-footnote-links
  3818. (2 'org-footnote t)))
  3819. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3820. '(org-hide-wide-columns (0 nil append))
  3821. ;; TODO lines
  3822. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3823. '(1 (org-get-todo-face 1) t))
  3824. ;; DONE
  3825. (if org-fontify-done-headline
  3826. (list (concat "^[*]+ +\\<\\("
  3827. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3828. "\\)\\(.*\\)")
  3829. '(2 'org-headline-done t))
  3830. nil)
  3831. ;; Priorities
  3832. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3833. ;; Tags
  3834. '(org-font-lock-add-tag-faces)
  3835. ;; Special keywords
  3836. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3837. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3838. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3839. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3840. ;; Emphasis
  3841. (if em
  3842. (if (featurep 'xemacs)
  3843. '(org-do-emphasis-faces (0 nil append))
  3844. '(org-do-emphasis-faces)))
  3845. ;; Checkboxes
  3846. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3847. 2 'bold prepend)
  3848. (if org-provide-checkbox-statistics
  3849. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3850. (0 (org-get-checkbox-statistics-face) t)))
  3851. ;; Description list items
  3852. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3853. 2 'bold prepend)
  3854. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3855. '(1 'org-archived prepend))
  3856. ;; Specials
  3857. '(org-do-latex-and-special-faces)
  3858. ;; Code
  3859. '(org-activate-code (1 'org-code t))
  3860. ;; COMMENT
  3861. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3862. "\\|" org-quote-string "\\)\\>")
  3863. '(1 'org-special-keyword t))
  3864. '("^#.*" (0 'font-lock-comment-face t))
  3865. )))
  3866. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3867. ;; Now set the full font-lock-keywords
  3868. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3869. (org-set-local 'font-lock-defaults
  3870. '(org-font-lock-keywords t nil nil backward-paragraph))
  3871. (kill-local-variable 'font-lock-keywords) nil))
  3872. (defvar org-m nil)
  3873. (defvar org-l nil)
  3874. (defvar org-f nil)
  3875. (defun org-get-level-face (n)
  3876. "Get the right face for match N in font-lock matching of headlines."
  3877. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3878. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3879. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3880. (cond
  3881. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3882. ((eq n 2) org-f)
  3883. (t (if org-level-color-stars-only nil org-f))))
  3884. (defun org-get-todo-face (kwd)
  3885. "Get the right face for a TODO keyword KWD.
  3886. If KWD is a number, get the corresponding match group."
  3887. (if (numberp kwd) (setq kwd (match-string kwd)))
  3888. (or (cdr (assoc kwd org-todo-keyword-faces))
  3889. (and (member kwd org-done-keywords) 'org-done)
  3890. 'org-todo))
  3891. (defun org-font-lock-add-tag-faces (limit)
  3892. "Add the special tag faces."
  3893. (when (and org-tag-faces org-tags-special-faces-re)
  3894. (while (re-search-forward org-tags-special-faces-re limit t)
  3895. (add-text-properties (match-beginning 1) (match-end 1)
  3896. (list 'face (org-get-tag-face 1)
  3897. 'font-lock-fontified t))
  3898. (backward-char 1))))
  3899. (defun org-get-tag-face (kwd)
  3900. "Get the right face for a TODO keyword KWD.
  3901. If KWD is a number, get the corresponding match group."
  3902. (if (numberp kwd) (setq kwd (match-string kwd)))
  3903. (or (cdr (assoc kwd org-tag-faces))
  3904. 'org-tag))
  3905. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3906. "Remove fontification and activation overlays from links."
  3907. (font-lock-default-unfontify-region beg end)
  3908. (let* ((buffer-undo-list t)
  3909. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3910. (inhibit-modification-hooks t)
  3911. deactivate-mark buffer-file-name buffer-file-truename)
  3912. (remove-text-properties beg end
  3913. '(mouse-face t keymap t org-linked-text t
  3914. invisible t intangible t))))
  3915. ;;;; Visibility cycling, including org-goto and indirect buffer
  3916. ;;; Cycling
  3917. (defvar org-cycle-global-status nil)
  3918. (make-variable-buffer-local 'org-cycle-global-status)
  3919. (defvar org-cycle-subtree-status nil)
  3920. (make-variable-buffer-local 'org-cycle-subtree-status)
  3921. ;;;###autoload
  3922. (defun org-cycle (&optional arg)
  3923. "Visibility cycling for Org-mode.
  3924. - When this function is called with a prefix argument, rotate the entire
  3925. buffer through 3 states (global cycling)
  3926. 1. OVERVIEW: Show only top-level headlines.
  3927. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3928. 3. SHOW ALL: Show everything.
  3929. When called with two C-u C-u prefixes, switch to the startup visibility,
  3930. determined by the variable `org-startup-folded', and by any VISIBILITY
  3931. properties in the buffer.
  3932. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3933. including drawers.
  3934. - When point is at the beginning of a headline, rotate the subtree started
  3935. by this line through 3 different states (local cycling)
  3936. 1. FOLDED: Only the main headline is shown.
  3937. 2. CHILDREN: The main headline and the direct children are shown.
  3938. From this state, you can move to one of the children
  3939. and zoom in further.
  3940. 3. SUBTREE: Show the entire subtree, including body text.
  3941. - When there is a numeric prefix, go up to a heading with level ARG, do
  3942. a `show-subtree' and return to the previous cursor position. If ARG
  3943. is negative, go up that many levels.
  3944. - When point is not at the beginning of a headline, execute the global
  3945. binding for TAB, which is re-indenting the line. See the option
  3946. `org-cycle-emulate-tab' for details.
  3947. - Special case: if point is at the beginning of the buffer and there is
  3948. no headline in line 1, this function will act as if called with prefix arg.
  3949. But only if also the variable `org-cycle-global-at-bob' is t."
  3950. (interactive "P")
  3951. (org-load-modules-maybe)
  3952. (let* ((outline-regexp
  3953. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3954. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3955. outline-regexp))
  3956. (bob-special (and org-cycle-global-at-bob (bobp)
  3957. (not (looking-at outline-regexp))))
  3958. (org-cycle-hook
  3959. (if bob-special
  3960. (delq 'org-optimize-window-after-visibility-change
  3961. (copy-sequence org-cycle-hook))
  3962. org-cycle-hook))
  3963. (pos (point)))
  3964. (if (or bob-special (equal arg '(4)))
  3965. ;; special case: use global cycling
  3966. (setq arg t))
  3967. (cond
  3968. ((equal arg '(16))
  3969. (org-set-startup-visibility)
  3970. (message "Startup visibility, plus VISIBILITY properties"))
  3971. ((equal arg '(64))
  3972. (show-all)
  3973. (message "Entire buffer visible, including drawers"))
  3974. ((org-at-table-p 'any)
  3975. ;; Enter the table or move to the next field in the table
  3976. (or (org-table-recognize-table.el)
  3977. (progn
  3978. (if arg (org-table-edit-field t)
  3979. (org-table-justify-field-maybe)
  3980. (call-interactively 'org-table-next-field)))))
  3981. ((eq arg t) ;; Global cycling
  3982. (cond
  3983. ((and (eq last-command this-command)
  3984. (eq org-cycle-global-status 'overview))
  3985. ;; We just created the overview - now do table of contents
  3986. ;; This can be slow in very large buffers, so indicate action
  3987. (message "CONTENTS...")
  3988. (org-content)
  3989. (message "CONTENTS...done")
  3990. (setq org-cycle-global-status 'contents)
  3991. (run-hook-with-args 'org-cycle-hook 'contents))
  3992. ((and (eq last-command this-command)
  3993. (eq org-cycle-global-status 'contents))
  3994. ;; We just showed the table of contents - now show everything
  3995. (show-all)
  3996. (message "SHOW ALL")
  3997. (setq org-cycle-global-status 'all)
  3998. (run-hook-with-args 'org-cycle-hook 'all))
  3999. (t
  4000. ;; Default action: go to overview
  4001. (org-overview)
  4002. (message "OVERVIEW")
  4003. (setq org-cycle-global-status 'overview)
  4004. (run-hook-with-args 'org-cycle-hook 'overview))))
  4005. ((and org-drawers org-drawer-regexp
  4006. (save-excursion
  4007. (beginning-of-line 1)
  4008. (looking-at org-drawer-regexp)))
  4009. ;; Toggle block visibility
  4010. (org-flag-drawer
  4011. (not (get-char-property (match-end 0) 'invisible))))
  4012. ((integerp arg)
  4013. ;; Show-subtree, ARG levels up from here.
  4014. (save-excursion
  4015. (org-back-to-heading)
  4016. (outline-up-heading (if (< arg 0) (- arg)
  4017. (- (funcall outline-level) arg)))
  4018. (org-show-subtree)))
  4019. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4020. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4021. ;; At a heading: rotate between three different views
  4022. (org-back-to-heading)
  4023. (let ((goal-column 0) eoh eol eos)
  4024. ;; First, some boundaries
  4025. (save-excursion
  4026. (org-back-to-heading)
  4027. (save-excursion
  4028. (beginning-of-line 2)
  4029. (while (and (not (eobp)) ;; this is like `next-line'
  4030. (get-char-property (1- (point)) 'invisible))
  4031. (beginning-of-line 2)) (setq eol (point)))
  4032. (outline-end-of-heading) (setq eoh (point))
  4033. (org-end-of-subtree t)
  4034. (unless (eobp)
  4035. (skip-chars-forward " \t\n")
  4036. (beginning-of-line 1) ; in case this is an item
  4037. )
  4038. (setq eos (1- (point))))
  4039. ;; Find out what to do next and set `this-command'
  4040. (cond
  4041. ((= eos eoh)
  4042. ;; Nothing is hidden behind this heading
  4043. (message "EMPTY ENTRY")
  4044. (setq org-cycle-subtree-status nil)
  4045. (save-excursion
  4046. (goto-char eos)
  4047. (outline-next-heading)
  4048. (if (org-invisible-p) (org-flag-heading nil))))
  4049. ((or (>= eol eos)
  4050. (not (string-match "\\S-" (buffer-substring eol eos))))
  4051. ;; Entire subtree is hidden in one line: open it
  4052. (org-show-entry)
  4053. (show-children)
  4054. (message "CHILDREN")
  4055. (save-excursion
  4056. (goto-char eos)
  4057. (outline-next-heading)
  4058. (if (org-invisible-p) (org-flag-heading nil)))
  4059. (setq org-cycle-subtree-status 'children)
  4060. (run-hook-with-args 'org-cycle-hook 'children))
  4061. ((and (eq last-command this-command)
  4062. (eq org-cycle-subtree-status 'children))
  4063. ;; We just showed the children, now show everything.
  4064. (org-show-subtree)
  4065. (message "SUBTREE")
  4066. (setq org-cycle-subtree-status 'subtree)
  4067. (run-hook-with-args 'org-cycle-hook 'subtree))
  4068. (t
  4069. ;; Default action: hide the subtree.
  4070. (hide-subtree)
  4071. (message "FOLDED")
  4072. (setq org-cycle-subtree-status 'folded)
  4073. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4074. ;; TAB emulation and template completion
  4075. (buffer-read-only (org-back-to-heading))
  4076. ((org-try-structure-completion))
  4077. ((org-try-cdlatex-tab))
  4078. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4079. (or (not (bolp))
  4080. (not (looking-at outline-regexp))))
  4081. (call-interactively (global-key-binding "\t")))
  4082. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4083. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4084. (or (and (eq org-cycle-emulate-tab 'white)
  4085. (= (match-end 0) (point-at-eol)))
  4086. (and (eq org-cycle-emulate-tab 'whitestart)
  4087. (>= (match-end 0) pos))))
  4088. t
  4089. (eq org-cycle-emulate-tab t))
  4090. (call-interactively (global-key-binding "\t")))
  4091. (t (save-excursion
  4092. (org-back-to-heading)
  4093. (org-cycle))))))
  4094. ;;;###autoload
  4095. (defun org-global-cycle (&optional arg)
  4096. "Cycle the global visibility. For details see `org-cycle'.
  4097. With C-u prefix arg, switch to startup visibility.
  4098. With a numeric prefix, show all headlines up to that level."
  4099. (interactive "P")
  4100. (let ((org-cycle-include-plain-lists
  4101. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4102. (cond
  4103. ((integerp arg)
  4104. (show-all)
  4105. (hide-sublevels arg)
  4106. (setq org-cycle-global-status 'contents))
  4107. ((equal arg '(4))
  4108. (org-set-startup-visibility)
  4109. (message "Startup visibility, plus VISIBILITY properties."))
  4110. (t
  4111. (org-cycle '(4))))))
  4112. (defun org-set-startup-visibility ()
  4113. "Set the visibility required by startup options and properties."
  4114. (cond
  4115. ((eq org-startup-folded t)
  4116. (org-cycle '(4)))
  4117. ((eq org-startup-folded 'content)
  4118. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4119. (org-cycle '(4)) (org-cycle '(4)))))
  4120. (org-set-visibility-according-to-property 'no-cleanup)
  4121. (org-cycle-hide-archived-subtrees 'all)
  4122. (org-cycle-hide-drawers 'all)
  4123. (org-cycle-show-empty-lines 'all))
  4124. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4125. "Switch subtree visibilities according to :VISIBILITY: property."
  4126. (interactive)
  4127. (let (org-show-entry-below state)
  4128. (save-excursion
  4129. (goto-char (point-min))
  4130. (while (re-search-forward
  4131. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4132. nil t)
  4133. (setq state (match-string 1))
  4134. (save-excursion
  4135. (org-back-to-heading t)
  4136. (hide-subtree)
  4137. (org-reveal)
  4138. (cond
  4139. ((equal state '("fold" "folded"))
  4140. (hide-subtree))
  4141. ((equal state "children")
  4142. (org-show-hidden-entry)
  4143. (show-children))
  4144. ((equal state "content")
  4145. (save-excursion
  4146. (save-restriction
  4147. (org-narrow-to-subtree)
  4148. (org-content))))
  4149. ((member state '("all" "showall"))
  4150. (show-subtree)))))
  4151. (unless no-cleanup
  4152. (org-cycle-hide-archived-subtrees 'all)
  4153. (org-cycle-hide-drawers 'all)
  4154. (org-cycle-show-empty-lines 'all)))))
  4155. (defun org-overview ()
  4156. "Switch to overview mode, showing only top-level headlines.
  4157. Really, this shows all headlines with level equal or greater than the level
  4158. of the first headline in the buffer. This is important, because if the
  4159. first headline is not level one, then (hide-sublevels 1) gives confusing
  4160. results."
  4161. (interactive)
  4162. (let ((level (save-excursion
  4163. (goto-char (point-min))
  4164. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4165. (progn
  4166. (goto-char (match-beginning 0))
  4167. (funcall outline-level))))))
  4168. (and level (hide-sublevels level))))
  4169. (defun org-content (&optional arg)
  4170. "Show all headlines in the buffer, like a table of contents.
  4171. With numerical argument N, show content up to level N."
  4172. (interactive "P")
  4173. (save-excursion
  4174. ;; Visit all headings and show their offspring
  4175. (and (integerp arg) (org-overview))
  4176. (goto-char (point-max))
  4177. (catch 'exit
  4178. (while (and (progn (condition-case nil
  4179. (outline-previous-visible-heading 1)
  4180. (error (goto-char (point-min))))
  4181. t)
  4182. (looking-at outline-regexp))
  4183. (if (integerp arg)
  4184. (show-children (1- arg))
  4185. (show-branches))
  4186. (if (bobp) (throw 'exit nil))))))
  4187. (defun org-optimize-window-after-visibility-change (state)
  4188. "Adjust the window after a change in outline visibility.
  4189. This function is the default value of the hook `org-cycle-hook'."
  4190. (when (get-buffer-window (current-buffer))
  4191. (cond
  4192. ((eq state 'content) nil)
  4193. ((eq state 'all) nil)
  4194. ((eq state 'folded) nil)
  4195. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4196. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4197. (defun org-compact-display-after-subtree-move ()
  4198. "Show a compacter version of the tree of the entry's parent."
  4199. (save-excursion
  4200. (if (org-up-heading-safe)
  4201. (progn
  4202. (hide-subtree)
  4203. (show-entry)
  4204. (show-children)
  4205. (org-cycle-show-empty-lines 'children)
  4206. (org-cycle-hide-drawers 'children))
  4207. (org-overview))))
  4208. (defun org-cycle-show-empty-lines (state)
  4209. "Show empty lines above all visible headlines.
  4210. The region to be covered depends on STATE when called through
  4211. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4212. entire buffer covered. Note that an empty line is only shown if there
  4213. are at least `org-cycle-separator-lines' empty lines before the headline."
  4214. (when (> org-cycle-separator-lines 0)
  4215. (save-excursion
  4216. (let* ((n org-cycle-separator-lines)
  4217. (re (cond
  4218. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4219. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4220. (t (let ((ns (number-to-string (- n 2))))
  4221. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4222. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4223. beg end)
  4224. (cond
  4225. ((memq state '(overview contents t))
  4226. (setq beg (point-min) end (point-max)))
  4227. ((memq state '(children folded))
  4228. (setq beg (point) end (progn (org-end-of-subtree t t)
  4229. (beginning-of-line 2)
  4230. (point)))))
  4231. (when beg
  4232. (goto-char beg)
  4233. (while (re-search-forward re end t)
  4234. (if (not (get-char-property (match-end 1) 'invisible))
  4235. (outline-flag-region
  4236. (match-beginning 1) (match-end 1) nil)))))))
  4237. ;; Never hide empty lines at the end of the file.
  4238. (save-excursion
  4239. (goto-char (point-max))
  4240. (outline-previous-heading)
  4241. (outline-end-of-heading)
  4242. (if (and (looking-at "[ \t\n]+")
  4243. (= (match-end 0) (point-max)))
  4244. (outline-flag-region (point) (match-end 0) nil))))
  4245. (defun org-show-empty-lines-in-parent ()
  4246. "Move to the parent and re-show empty lines before visible headlines."
  4247. (save-excursion
  4248. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4249. (org-cycle-show-empty-lines context))))
  4250. (defun org-cycle-hide-drawers (state)
  4251. "Re-hide all drawers after a visibility state change."
  4252. (when (and (org-mode-p)
  4253. (not (memq state '(overview folded))))
  4254. (save-excursion
  4255. (let* ((globalp (memq state '(contents all)))
  4256. (beg (if globalp (point-min) (point)))
  4257. (end (if globalp (point-max) (org-end-of-subtree t))))
  4258. (goto-char beg)
  4259. (while (re-search-forward org-drawer-regexp end t)
  4260. (org-flag-drawer t))))))
  4261. (defun org-flag-drawer (flag)
  4262. (save-excursion
  4263. (beginning-of-line 1)
  4264. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4265. (let ((b (match-end 0))
  4266. (outline-regexp org-outline-regexp))
  4267. (if (re-search-forward
  4268. "^[ \t]*:END:"
  4269. (save-excursion (outline-next-heading) (point)) t)
  4270. (outline-flag-region b (point-at-eol) flag)
  4271. (error ":END: line missing"))))))
  4272. (defun org-subtree-end-visible-p ()
  4273. "Is the end of the current subtree visible?"
  4274. (pos-visible-in-window-p
  4275. (save-excursion (org-end-of-subtree t) (point))))
  4276. (defun org-first-headline-recenter (&optional N)
  4277. "Move cursor to the first headline and recenter the headline.
  4278. Optional argument N means, put the headline into the Nth line of the window."
  4279. (goto-char (point-min))
  4280. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4281. (beginning-of-line)
  4282. (recenter (prefix-numeric-value N))))
  4283. ;;; Org-goto
  4284. (defvar org-goto-window-configuration nil)
  4285. (defvar org-goto-marker nil)
  4286. (defvar org-goto-map
  4287. (let ((map (make-sparse-keymap)))
  4288. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4289. (while (setq cmd (pop cmds))
  4290. (substitute-key-definition cmd cmd map global-map)))
  4291. (suppress-keymap map)
  4292. (org-defkey map "\C-m" 'org-goto-ret)
  4293. (org-defkey map [(return)] 'org-goto-ret)
  4294. (org-defkey map [(left)] 'org-goto-left)
  4295. (org-defkey map [(right)] 'org-goto-right)
  4296. (org-defkey map [(control ?g)] 'org-goto-quit)
  4297. (org-defkey map "\C-i" 'org-cycle)
  4298. (org-defkey map [(tab)] 'org-cycle)
  4299. (org-defkey map [(down)] 'outline-next-visible-heading)
  4300. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4301. (if org-goto-auto-isearch
  4302. (if (fboundp 'define-key-after)
  4303. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4304. nil)
  4305. (org-defkey map "q" 'org-goto-quit)
  4306. (org-defkey map "n" 'outline-next-visible-heading)
  4307. (org-defkey map "p" 'outline-previous-visible-heading)
  4308. (org-defkey map "f" 'outline-forward-same-level)
  4309. (org-defkey map "b" 'outline-backward-same-level)
  4310. (org-defkey map "u" 'outline-up-heading))
  4311. (org-defkey map "/" 'org-occur)
  4312. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4313. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4314. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4315. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4316. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4317. map))
  4318. (defconst org-goto-help
  4319. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4320. RET=jump to location [Q]uit and return to previous location
  4321. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4322. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4323. ;; FIXME: Docstring doe not mention both interfaces
  4324. (defun org-goto (&optional alternative-interface)
  4325. "Look up a different location in the current file, keeping current visibility.
  4326. When you want look-up or go to a different location in a document, the
  4327. fastest way is often to fold the entire buffer and then dive into the tree.
  4328. This method has the disadvantage, that the previous location will be folded,
  4329. which may not be what you want.
  4330. This command works around this by showing a copy of the current buffer
  4331. in an indirect buffer, in overview mode. You can dive into the tree in
  4332. that copy, use org-occur and incremental search to find a location.
  4333. When pressing RET or `Q', the command returns to the original buffer in
  4334. which the visibility is still unchanged. After RET is will also jump to
  4335. the location selected in the indirect buffer and expose the
  4336. the headline hierarchy above."
  4337. (interactive "P")
  4338. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4339. (org-refile-use-outline-path t)
  4340. (interface
  4341. (if (not alternative-interface)
  4342. org-goto-interface
  4343. (if (eq org-goto-interface 'outline)
  4344. 'outline-path-completion
  4345. 'outline)))
  4346. (org-goto-start-pos (point))
  4347. (selected-point
  4348. (if (eq interface 'outline)
  4349. (car (org-get-location (current-buffer) org-goto-help))
  4350. (nth 3 (org-refile-get-location "Goto: ")))))
  4351. (if selected-point
  4352. (progn
  4353. (org-mark-ring-push org-goto-start-pos)
  4354. (goto-char selected-point)
  4355. (if (or (org-invisible-p) (org-invisible-p2))
  4356. (org-show-context 'org-goto)))
  4357. (message "Quit"))))
  4358. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4359. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4360. (defvar org-goto-local-auto-isearch-map) ; defined below
  4361. (defun org-get-location (buf help)
  4362. "Let the user select a location in the Org-mode buffer BUF.
  4363. This function uses a recursive edit. It returns the selected position
  4364. or nil."
  4365. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4366. (isearch-hide-immediately nil)
  4367. (isearch-search-fun-function
  4368. (lambda () 'org-goto-local-search-headings))
  4369. (org-goto-selected-point org-goto-exit-command))
  4370. (save-excursion
  4371. (save-window-excursion
  4372. (delete-other-windows)
  4373. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4374. (switch-to-buffer
  4375. (condition-case nil
  4376. (make-indirect-buffer (current-buffer) "*org-goto*")
  4377. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4378. (with-output-to-temp-buffer "*Help*"
  4379. (princ help))
  4380. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4381. (setq buffer-read-only nil)
  4382. (let ((org-startup-truncated t)
  4383. (org-startup-folded nil)
  4384. (org-startup-align-all-tables nil))
  4385. (org-mode)
  4386. (org-overview))
  4387. (setq buffer-read-only t)
  4388. (if (and (boundp 'org-goto-start-pos)
  4389. (integer-or-marker-p org-goto-start-pos))
  4390. (let ((org-show-hierarchy-above t)
  4391. (org-show-siblings t)
  4392. (org-show-following-heading t))
  4393. (goto-char org-goto-start-pos)
  4394. (and (org-invisible-p) (org-show-context)))
  4395. (goto-char (point-min)))
  4396. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4397. (message "Select location and press RET")
  4398. (use-local-map org-goto-map)
  4399. (recursive-edit)
  4400. ))
  4401. (kill-buffer "*org-goto*")
  4402. (cons org-goto-selected-point org-goto-exit-command)))
  4403. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4404. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4405. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4406. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4407. (defun org-goto-local-search-headings (string bound noerror)
  4408. "Search and make sure that any matches are in headlines."
  4409. (catch 'return
  4410. (while (if isearch-forward
  4411. (search-forward string bound noerror)
  4412. (search-backward string bound noerror))
  4413. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4414. (and (member :headline context)
  4415. (not (member :tags context))))
  4416. (throw 'return (point))))))
  4417. (defun org-goto-local-auto-isearch ()
  4418. "Start isearch."
  4419. (interactive)
  4420. (goto-char (point-min))
  4421. (let ((keys (this-command-keys)))
  4422. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4423. (isearch-mode t)
  4424. (isearch-process-search-char (string-to-char keys)))))
  4425. (defun org-goto-ret (&optional arg)
  4426. "Finish `org-goto' by going to the new location."
  4427. (interactive "P")
  4428. (setq org-goto-selected-point (point)
  4429. org-goto-exit-command 'return)
  4430. (throw 'exit nil))
  4431. (defun org-goto-left ()
  4432. "Finish `org-goto' by going to the new location."
  4433. (interactive)
  4434. (if (org-on-heading-p)
  4435. (progn
  4436. (beginning-of-line 1)
  4437. (setq org-goto-selected-point (point)
  4438. org-goto-exit-command 'left)
  4439. (throw 'exit nil))
  4440. (error "Not on a heading")))
  4441. (defun org-goto-right ()
  4442. "Finish `org-goto' by going to the new location."
  4443. (interactive)
  4444. (if (org-on-heading-p)
  4445. (progn
  4446. (setq org-goto-selected-point (point)
  4447. org-goto-exit-command 'right)
  4448. (throw 'exit nil))
  4449. (error "Not on a heading")))
  4450. (defun org-goto-quit ()
  4451. "Finish `org-goto' without cursor motion."
  4452. (interactive)
  4453. (setq org-goto-selected-point nil)
  4454. (setq org-goto-exit-command 'quit)
  4455. (throw 'exit nil))
  4456. ;;; Indirect buffer display of subtrees
  4457. (defvar org-indirect-dedicated-frame nil
  4458. "This is the frame being used for indirect tree display.")
  4459. (defvar org-last-indirect-buffer nil)
  4460. (defun org-tree-to-indirect-buffer (&optional arg)
  4461. "Create indirect buffer and narrow it to current subtree.
  4462. With numerical prefix ARG, go up to this level and then take that tree.
  4463. If ARG is negative, go up that many levels.
  4464. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4465. indirect buffer previously made with this command, to avoid proliferation of
  4466. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4467. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4468. is kept so that you can work with several indirect buffers at the same time.
  4469. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4470. requests that a new frame be made for the new buffer, so that the dedicated
  4471. frame is not changed."
  4472. (interactive "P")
  4473. (let ((cbuf (current-buffer))
  4474. (cwin (selected-window))
  4475. (pos (point))
  4476. beg end level heading ibuf)
  4477. (save-excursion
  4478. (org-back-to-heading t)
  4479. (when (numberp arg)
  4480. (setq level (org-outline-level))
  4481. (if (< arg 0) (setq arg (+ level arg)))
  4482. (while (> (setq level (org-outline-level)) arg)
  4483. (outline-up-heading 1 t)))
  4484. (setq beg (point)
  4485. heading (org-get-heading))
  4486. (org-end-of-subtree t) (setq end (point)))
  4487. (if (and (buffer-live-p org-last-indirect-buffer)
  4488. (not (eq org-indirect-buffer-display 'new-frame))
  4489. (not arg))
  4490. (kill-buffer org-last-indirect-buffer))
  4491. (setq ibuf (org-get-indirect-buffer cbuf)
  4492. org-last-indirect-buffer ibuf)
  4493. (cond
  4494. ((or (eq org-indirect-buffer-display 'new-frame)
  4495. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4496. (select-frame (make-frame))
  4497. (delete-other-windows)
  4498. (switch-to-buffer ibuf)
  4499. (org-set-frame-title heading))
  4500. ((eq org-indirect-buffer-display 'dedicated-frame)
  4501. (raise-frame
  4502. (select-frame (or (and org-indirect-dedicated-frame
  4503. (frame-live-p org-indirect-dedicated-frame)
  4504. org-indirect-dedicated-frame)
  4505. (setq org-indirect-dedicated-frame (make-frame)))))
  4506. (delete-other-windows)
  4507. (switch-to-buffer ibuf)
  4508. (org-set-frame-title (concat "Indirect: " heading)))
  4509. ((eq org-indirect-buffer-display 'current-window)
  4510. (switch-to-buffer ibuf))
  4511. ((eq org-indirect-buffer-display 'other-window)
  4512. (pop-to-buffer ibuf))
  4513. (t (error "Invalid value.")))
  4514. (if (featurep 'xemacs)
  4515. (save-excursion (org-mode) (turn-on-font-lock)))
  4516. (narrow-to-region beg end)
  4517. (show-all)
  4518. (goto-char pos)
  4519. (and (window-live-p cwin) (select-window cwin))))
  4520. (defun org-get-indirect-buffer (&optional buffer)
  4521. (setq buffer (or buffer (current-buffer)))
  4522. (let ((n 1) (base (buffer-name buffer)) bname)
  4523. (while (buffer-live-p
  4524. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4525. (setq n (1+ n)))
  4526. (condition-case nil
  4527. (make-indirect-buffer buffer bname 'clone)
  4528. (error (make-indirect-buffer buffer bname)))))
  4529. (defun org-set-frame-title (title)
  4530. "Set the title of the current frame to the string TITLE."
  4531. ;; FIXME: how to name a single frame in XEmacs???
  4532. (unless (featurep 'xemacs)
  4533. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4534. ;;;; Structure editing
  4535. ;;; Inserting headlines
  4536. (defun org-previous-line-empty-p ()
  4537. (save-excursion
  4538. (and (not (bobp))
  4539. (or (beginning-of-line 0) t)
  4540. (save-match-data
  4541. (looking-at "[ \t]*$")))))
  4542. (defun org-insert-heading (&optional force-heading)
  4543. "Insert a new heading or item with same depth at point.
  4544. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4545. If point is at the beginning of a headline, insert a sibling before the
  4546. current headline. If point is not at the beginning, do not split the line,
  4547. but create the new headline after the current line."
  4548. (interactive "P")
  4549. (if (= (buffer-size) 0)
  4550. (insert "\n* ")
  4551. (when (or force-heading (not (org-insert-item)))
  4552. (let* ((empty-line-p nil)
  4553. (head (save-excursion
  4554. (condition-case nil
  4555. (progn
  4556. (org-back-to-heading)
  4557. (setq empty-line-p (org-previous-line-empty-p))
  4558. (match-string 0))
  4559. (error "*"))))
  4560. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4561. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4562. pos hide-previous previous-pos)
  4563. (cond
  4564. ((and (org-on-heading-p) (bolp)
  4565. (or (bobp)
  4566. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4567. ;; insert before the current line
  4568. (open-line (if blank 2 1)))
  4569. ((and (bolp)
  4570. (or (bobp)
  4571. (save-excursion
  4572. (backward-char 1) (not (org-invisible-p)))))
  4573. ;; insert right here
  4574. nil)
  4575. (t
  4576. ;; somewhere in the line
  4577. (save-excursion
  4578. (setq previous-pos (point-at-bol))
  4579. (end-of-line)
  4580. (setq hide-previous (org-invisible-p)))
  4581. (and org-insert-heading-respect-content (org-show-subtree))
  4582. (let ((split
  4583. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4584. (save-excursion
  4585. (let ((p (point)))
  4586. (goto-char (point-at-bol))
  4587. (and (looking-at org-complex-heading-regexp)
  4588. (> p (match-beginning 4)))))))
  4589. tags pos)
  4590. (cond
  4591. (org-insert-heading-respect-content
  4592. (org-end-of-subtree nil t)
  4593. (or (bolp) (newline))
  4594. (or (org-previous-line-empty-p)
  4595. (and blank (newline)))
  4596. (open-line 1))
  4597. ((org-on-heading-p)
  4598. (when hide-previous
  4599. (show-children)
  4600. (org-show-entry))
  4601. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4602. (setq tags (and (match-end 2) (match-string 2)))
  4603. (and (match-end 1)
  4604. (delete-region (match-beginning 1) (match-end 1)))
  4605. (setq pos (point-at-bol))
  4606. (or split (end-of-line 1))
  4607. (delete-horizontal-space)
  4608. (newline (if blank 2 1))
  4609. (when tags
  4610. (save-excursion
  4611. (goto-char pos)
  4612. (end-of-line 1)
  4613. (insert " " tags)
  4614. (org-set-tags nil 'align))))
  4615. (t
  4616. (or split (end-of-line 1))
  4617. (newline (if blank 2 1)))))))
  4618. (insert head) (just-one-space)
  4619. (setq pos (point))
  4620. (end-of-line 1)
  4621. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4622. (when (and org-insert-heading-respect-content hide-previous)
  4623. (save-excursion
  4624. (goto-char previous-pos)
  4625. (hide-subtree)))
  4626. (run-hooks 'org-insert-heading-hook)))))
  4627. (defun org-get-heading (&optional no-tags)
  4628. "Return the heading of the current entry, without the stars."
  4629. (save-excursion
  4630. (org-back-to-heading t)
  4631. (if (looking-at
  4632. (if no-tags
  4633. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4634. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4635. (match-string 1) "")))
  4636. (defun org-heading-components ()
  4637. "Return the components of the current heading.
  4638. This is a list with the following elements:
  4639. - the level as an integer
  4640. - the reduced level, different if `org-odd-levels-only' is set.
  4641. - the TODO keyword, or nil
  4642. - the priority character, like ?A, or nil if no priority is given
  4643. - the headline text itself, or the tags string if no headline text
  4644. - the tags string, or nil."
  4645. (save-excursion
  4646. (org-back-to-heading t)
  4647. (if (looking-at org-complex-heading-regexp)
  4648. (list (length (match-string 1))
  4649. (org-reduced-level (length (match-string 1)))
  4650. (org-match-string-no-properties 2)
  4651. (and (match-end 3) (aref (match-string 3) 2))
  4652. (org-match-string-no-properties 4)
  4653. (org-match-string-no-properties 5)))))
  4654. (defun org-insert-heading-after-current ()
  4655. "Insert a new heading with same level as current, after current subtree."
  4656. (interactive)
  4657. (org-back-to-heading)
  4658. (org-insert-heading)
  4659. (org-move-subtree-down)
  4660. (end-of-line 1))
  4661. (defun org-insert-heading-respect-content ()
  4662. (interactive)
  4663. (let ((org-insert-heading-respect-content t))
  4664. (org-insert-heading t)))
  4665. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4666. (interactive "P")
  4667. (let ((org-insert-heading-respect-content t))
  4668. (org-insert-todo-heading force-state t)))
  4669. (defun org-insert-todo-heading (arg &optional force-heading)
  4670. "Insert a new heading with the same level and TODO state as current heading.
  4671. If the heading has no TODO state, or if the state is DONE, use the first
  4672. state (TODO by default). Also with prefix arg, force first state."
  4673. (interactive "P")
  4674. (when (or force-heading (not (org-insert-item 'checkbox)))
  4675. (org-insert-heading force-heading)
  4676. (save-excursion
  4677. (org-back-to-heading)
  4678. (outline-previous-heading)
  4679. (looking-at org-todo-line-regexp))
  4680. (let*
  4681. ((new-mark-x
  4682. (if (or arg
  4683. (not (match-beginning 2))
  4684. (member (match-string 2) org-done-keywords))
  4685. (car org-todo-keywords-1)
  4686. (match-string 2)))
  4687. (new-mark
  4688. (or
  4689. (run-hook-with-args-until-success
  4690. 'org-todo-get-default-hook new-mark-x nil)
  4691. new-mark-x)))
  4692. (insert new-mark " "))
  4693. (when org-provide-todo-statistics
  4694. (org-update-parent-todo-statistics))))
  4695. (defun org-insert-subheading (arg)
  4696. "Insert a new subheading and demote it.
  4697. Works for outline headings and for plain lists alike."
  4698. (interactive "P")
  4699. (org-insert-heading arg)
  4700. (cond
  4701. ((org-on-heading-p) (org-do-demote))
  4702. ((org-at-item-p) (org-indent-item 1))))
  4703. (defun org-insert-todo-subheading (arg)
  4704. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4705. Works for outline headings and for plain lists alike."
  4706. (interactive "P")
  4707. (org-insert-todo-heading arg)
  4708. (cond
  4709. ((org-on-heading-p) (org-do-demote))
  4710. ((org-at-item-p) (org-indent-item 1))))
  4711. ;;; Promotion and Demotion
  4712. (defun org-promote-subtree ()
  4713. "Promote the entire subtree.
  4714. See also `org-promote'."
  4715. (interactive)
  4716. (save-excursion
  4717. (org-map-tree 'org-promote))
  4718. (org-fix-position-after-promote))
  4719. (defun org-demote-subtree ()
  4720. "Demote the entire subtree. See `org-demote'.
  4721. See also `org-promote'."
  4722. (interactive)
  4723. (save-excursion
  4724. (org-map-tree 'org-demote))
  4725. (org-fix-position-after-promote))
  4726. (defun org-do-promote ()
  4727. "Promote the current heading higher up the tree.
  4728. If the region is active in `transient-mark-mode', promote all headings
  4729. in the region."
  4730. (interactive)
  4731. (save-excursion
  4732. (if (org-region-active-p)
  4733. (org-map-region 'org-promote (region-beginning) (region-end))
  4734. (org-promote)))
  4735. (org-fix-position-after-promote))
  4736. (defun org-do-demote ()
  4737. "Demote the current heading lower down the tree.
  4738. If the region is active in `transient-mark-mode', demote all headings
  4739. in the region."
  4740. (interactive)
  4741. (save-excursion
  4742. (if (org-region-active-p)
  4743. (org-map-region 'org-demote (region-beginning) (region-end))
  4744. (org-demote)))
  4745. (org-fix-position-after-promote))
  4746. (defun org-fix-position-after-promote ()
  4747. "Make sure that after pro/demotion cursor position is right."
  4748. (let ((pos (point)))
  4749. (when (save-excursion
  4750. (beginning-of-line 1)
  4751. (looking-at org-todo-line-regexp)
  4752. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4753. (cond ((eobp) (insert " "))
  4754. ((eolp) (insert " "))
  4755. ((equal (char-after) ?\ ) (forward-char 1))))))
  4756. (defun org-reduced-level (l)
  4757. "Compute the effective level of a heading.
  4758. This takes into account the setting of `org-odd-levels-only'."
  4759. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4760. (defun org-get-valid-level (level &optional change)
  4761. "Rectify a level change under the influence of `org-odd-levels-only'
  4762. LEVEL is a current level, CHANGE is by how much the level should be
  4763. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4764. even level numbers will become the next higher odd number."
  4765. (if org-odd-levels-only
  4766. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4767. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4768. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4769. (max 1 (+ level change))))
  4770. (if (boundp 'define-obsolete-function-alias)
  4771. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4772. (define-obsolete-function-alias 'org-get-legal-level
  4773. 'org-get-valid-level)
  4774. (define-obsolete-function-alias 'org-get-legal-level
  4775. 'org-get-valid-level "23.1")))
  4776. (defun org-promote ()
  4777. "Promote the current heading higher up the tree.
  4778. If the region is active in `transient-mark-mode', promote all headings
  4779. in the region."
  4780. (org-back-to-heading t)
  4781. (let* ((level (save-match-data (funcall outline-level)))
  4782. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4783. (diff (abs (- level (length up-head) -1))))
  4784. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4785. (replace-match up-head nil t)
  4786. ;; Fixup tag positioning
  4787. (and org-auto-align-tags (org-set-tags nil t))
  4788. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4789. (defun org-demote ()
  4790. "Demote the current heading lower down the tree.
  4791. If the region is active in `transient-mark-mode', demote all headings
  4792. in the region."
  4793. (org-back-to-heading t)
  4794. (let* ((level (save-match-data (funcall outline-level)))
  4795. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4796. (diff (abs (- level (length down-head) -1))))
  4797. (replace-match down-head nil t)
  4798. ;; Fixup tag positioning
  4799. (and org-auto-align-tags (org-set-tags nil t))
  4800. (if org-adapt-indentation (org-fixup-indentation diff))))
  4801. (defun org-map-tree (fun)
  4802. "Call FUN for every heading underneath the current one."
  4803. (org-back-to-heading)
  4804. (let ((level (funcall outline-level)))
  4805. (save-excursion
  4806. (funcall fun)
  4807. (while (and (progn
  4808. (outline-next-heading)
  4809. (> (funcall outline-level) level))
  4810. (not (eobp)))
  4811. (funcall fun)))))
  4812. (defun org-map-region (fun beg end)
  4813. "Call FUN for every heading between BEG and END."
  4814. (let ((org-ignore-region t))
  4815. (save-excursion
  4816. (setq end (copy-marker end))
  4817. (goto-char beg)
  4818. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4819. (< (point) end))
  4820. (funcall fun))
  4821. (while (and (progn
  4822. (outline-next-heading)
  4823. (< (point) end))
  4824. (not (eobp)))
  4825. (funcall fun)))))
  4826. (defun org-fixup-indentation (diff)
  4827. "Change the indentation in the current entry by DIFF
  4828. However, if any line in the current entry has no indentation, or if it
  4829. would end up with no indentation after the change, nothing at all is done."
  4830. (save-excursion
  4831. (let ((end (save-excursion (outline-next-heading)
  4832. (point-marker)))
  4833. (prohibit (if (> diff 0)
  4834. "^\\S-"
  4835. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4836. col)
  4837. (unless (save-excursion (end-of-line 1)
  4838. (re-search-forward prohibit end t))
  4839. (while (and (< (point) end)
  4840. (re-search-forward "^[ \t]+" end t))
  4841. (goto-char (match-end 0))
  4842. (setq col (current-column))
  4843. (if (< diff 0) (replace-match ""))
  4844. (org-indent-to-column (+ diff col))))
  4845. (move-marker end nil))))
  4846. (defun org-convert-to-odd-levels ()
  4847. "Convert an org-mode file with all levels allowed to one with odd levels.
  4848. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4849. level 5 etc."
  4850. (interactive)
  4851. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4852. (let ((org-odd-levels-only nil) n)
  4853. (save-excursion
  4854. (goto-char (point-min))
  4855. (while (re-search-forward "^\\*\\*+ " nil t)
  4856. (setq n (- (length (match-string 0)) 2))
  4857. (while (>= (setq n (1- n)) 0)
  4858. (org-demote))
  4859. (end-of-line 1))))))
  4860. (defun org-convert-to-oddeven-levels ()
  4861. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4862. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4863. section with an even level, conversion would destroy the structure of the file. An error
  4864. is signaled in this case."
  4865. (interactive)
  4866. (goto-char (point-min))
  4867. ;; First check if there are no even levels
  4868. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4869. (org-show-context t)
  4870. (error "Not all levels are odd in this file. Conversion not possible."))
  4871. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4872. (let ((org-odd-levels-only nil) n)
  4873. (save-excursion
  4874. (goto-char (point-min))
  4875. (while (re-search-forward "^\\*\\*+ " nil t)
  4876. (setq n (/ (1- (length (match-string 0))) 2))
  4877. (while (>= (setq n (1- n)) 0)
  4878. (org-promote))
  4879. (end-of-line 1))))))
  4880. (defun org-tr-level (n)
  4881. "Make N odd if required."
  4882. (if org-odd-levels-only (1+ (/ n 2)) n))
  4883. ;;; Vertical tree motion, cutting and pasting of subtrees
  4884. (defun org-move-subtree-up (&optional arg)
  4885. "Move the current subtree up past ARG headlines of the same level."
  4886. (interactive "p")
  4887. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4888. (defun org-move-subtree-down (&optional arg)
  4889. "Move the current subtree down past ARG headlines of the same level."
  4890. (interactive "p")
  4891. (setq arg (prefix-numeric-value arg))
  4892. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4893. 'outline-get-last-sibling))
  4894. (ins-point (make-marker))
  4895. (cnt (abs arg))
  4896. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4897. ;; Select the tree
  4898. (org-back-to-heading)
  4899. (setq beg0 (point))
  4900. (save-excursion
  4901. (setq ne-beg (org-back-over-empty-lines))
  4902. (setq beg (point)))
  4903. (save-match-data
  4904. (save-excursion (outline-end-of-heading)
  4905. (setq folded (org-invisible-p)))
  4906. (outline-end-of-subtree))
  4907. (outline-next-heading)
  4908. (setq ne-end (org-back-over-empty-lines))
  4909. (setq end (point))
  4910. (goto-char beg0)
  4911. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4912. ;; include less whitespace
  4913. (save-excursion
  4914. (goto-char beg)
  4915. (forward-line (- ne-beg ne-end))
  4916. (setq beg (point))))
  4917. ;; Find insertion point, with error handling
  4918. (while (> cnt 0)
  4919. (or (and (funcall movfunc) (looking-at outline-regexp))
  4920. (progn (goto-char beg0)
  4921. (error "Cannot move past superior level or buffer limit")))
  4922. (setq cnt (1- cnt)))
  4923. (if (> arg 0)
  4924. ;; Moving forward - still need to move over subtree
  4925. (progn (org-end-of-subtree t t)
  4926. (save-excursion
  4927. (org-back-over-empty-lines)
  4928. (or (bolp) (newline)))))
  4929. (setq ne-ins (org-back-over-empty-lines))
  4930. (move-marker ins-point (point))
  4931. (setq txt (buffer-substring beg end))
  4932. (org-save-markers-in-region beg end)
  4933. (delete-region beg end)
  4934. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4935. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4936. (let ((bbb (point)))
  4937. (insert-before-markers txt)
  4938. (org-reinstall-markers-in-region bbb)
  4939. (move-marker ins-point bbb))
  4940. (or (bolp) (insert "\n"))
  4941. (setq ins-end (point))
  4942. (goto-char ins-point)
  4943. (org-skip-whitespace)
  4944. (when (and (< arg 0)
  4945. (org-first-sibling-p)
  4946. (> ne-ins ne-beg))
  4947. ;; Move whitespace back to beginning
  4948. (save-excursion
  4949. (goto-char ins-end)
  4950. (let ((kill-whole-line t))
  4951. (kill-line (- ne-ins ne-beg)) (point)))
  4952. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4953. (move-marker ins-point nil)
  4954. (org-compact-display-after-subtree-move)
  4955. (org-show-empty-lines-in-parent)
  4956. (unless folded
  4957. (org-show-entry)
  4958. (show-children)
  4959. (org-cycle-hide-drawers 'children))))
  4960. (defvar org-subtree-clip ""
  4961. "Clipboard for cut and paste of subtrees.
  4962. This is actually only a copy of the kill, because we use the normal kill
  4963. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4964. (defvar org-subtree-clip-folded nil
  4965. "Was the last copied subtree folded?
  4966. This is used to fold the tree back after pasting.")
  4967. (defun org-cut-subtree (&optional n)
  4968. "Cut the current subtree into the clipboard.
  4969. With prefix arg N, cut this many sequential subtrees.
  4970. This is a short-hand for marking the subtree and then cutting it."
  4971. (interactive "p")
  4972. (org-copy-subtree n 'cut))
  4973. (defun org-copy-subtree (&optional n cut force-store-markers)
  4974. "Cut the current subtree into the clipboard.
  4975. With prefix arg N, cut this many sequential subtrees.
  4976. This is a short-hand for marking the subtree and then copying it.
  4977. If CUT is non-nil, actually cut the subtree.
  4978. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4979. of some markers in the region, even if CUT is non-nil. This is
  4980. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4981. (interactive "p")
  4982. (let (beg end folded (beg0 (point)))
  4983. (if (interactive-p)
  4984. (org-back-to-heading nil) ; take what looks like a subtree
  4985. (org-back-to-heading t)) ; take what is really there
  4986. (org-back-over-empty-lines)
  4987. (setq beg (point))
  4988. (skip-chars-forward " \t\r\n")
  4989. (save-match-data
  4990. (save-excursion (outline-end-of-heading)
  4991. (setq folded (org-invisible-p)))
  4992. (condition-case nil
  4993. (outline-forward-same-level (1- n))
  4994. (error nil))
  4995. (org-end-of-subtree t t))
  4996. (org-back-over-empty-lines)
  4997. (setq end (point))
  4998. (goto-char beg0)
  4999. (when (> end beg)
  5000. (setq org-subtree-clip-folded folded)
  5001. (when (or cut force-store-markers)
  5002. (org-save-markers-in-region beg end))
  5003. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5004. (setq org-subtree-clip (current-kill 0))
  5005. (message "%s: Subtree(s) with %d characters"
  5006. (if cut "Cut" "Copied")
  5007. (length org-subtree-clip)))))
  5008. (defun org-paste-subtree (&optional level tree for-yank)
  5009. "Paste the clipboard as a subtree, with modification of headline level.
  5010. The entire subtree is promoted or demoted in order to match a new headline
  5011. level.
  5012. If the cursor is at the beginning of a headline, the same level as
  5013. that headline is used to paste the tree
  5014. If not, the new level is derived from the *visible* headings
  5015. before and after the insertion point, and taken to be the inferior headline
  5016. level of the two. So if the previous visible heading is level 3 and the
  5017. next is level 4 (or vice versa), level 4 will be used for insertion.
  5018. This makes sure that the subtree remains an independent subtree and does
  5019. not swallow low level entries.
  5020. You can also force a different level, either by using a numeric prefix
  5021. argument, or by inserting the heading marker by hand. For example, if the
  5022. cursor is after \"*****\", then the tree will be shifted to level 5.
  5023. If optional TREE is given, use this text instead of the kill ring.
  5024. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5025. move back over whitespace before inserting, and move point to the end of
  5026. the inserted text when done."
  5027. (interactive "P")
  5028. (unless (org-kill-is-subtree-p tree)
  5029. (error "%s"
  5030. (substitute-command-keys
  5031. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5032. (let* ((visp (not (org-invisible-p)))
  5033. (txt (or tree (and kill-ring (current-kill 0))))
  5034. (^re (concat "^\\(" outline-regexp "\\)"))
  5035. (re (concat "\\(" outline-regexp "\\)"))
  5036. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5037. (old-level (if (string-match ^re txt)
  5038. (- (match-end 0) (match-beginning 0) 1)
  5039. -1))
  5040. (force-level (cond (level (prefix-numeric-value level))
  5041. ((and (looking-at "[ \t]*$")
  5042. (string-match
  5043. ^re_ (buffer-substring
  5044. (point-at-bol) (point))))
  5045. (- (match-end 1) (match-beginning 1)))
  5046. ((and (bolp)
  5047. (looking-at org-outline-regexp))
  5048. (- (match-end 0) (point) 1))
  5049. (t nil)))
  5050. (previous-level (save-excursion
  5051. (condition-case nil
  5052. (progn
  5053. (outline-previous-visible-heading 1)
  5054. (if (looking-at re)
  5055. (- (match-end 0) (match-beginning 0) 1)
  5056. 1))
  5057. (error 1))))
  5058. (next-level (save-excursion
  5059. (condition-case nil
  5060. (progn
  5061. (or (looking-at outline-regexp)
  5062. (outline-next-visible-heading 1))
  5063. (if (looking-at re)
  5064. (- (match-end 0) (match-beginning 0) 1)
  5065. 1))
  5066. (error 1))))
  5067. (new-level (or force-level (max previous-level next-level)))
  5068. (shift (if (or (= old-level -1)
  5069. (= new-level -1)
  5070. (= old-level new-level))
  5071. 0
  5072. (- new-level old-level)))
  5073. (delta (if (> shift 0) -1 1))
  5074. (func (if (> shift 0) 'org-demote 'org-promote))
  5075. (org-odd-levels-only nil)
  5076. beg end newend)
  5077. ;; Remove the forced level indicator
  5078. (if force-level
  5079. (delete-region (point-at-bol) (point)))
  5080. ;; Paste
  5081. (beginning-of-line 1)
  5082. (unless for-yank (org-back-over-empty-lines))
  5083. (setq beg (point))
  5084. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5085. (insert-before-markers txt)
  5086. (unless (string-match "\n\\'" txt) (insert "\n"))
  5087. (setq newend (point))
  5088. (org-reinstall-markers-in-region beg)
  5089. (setq end (point))
  5090. (goto-char beg)
  5091. (skip-chars-forward " \t\n\r")
  5092. (setq beg (point))
  5093. (if (and (org-invisible-p) visp)
  5094. (save-excursion (outline-show-heading)))
  5095. ;; Shift if necessary
  5096. (unless (= shift 0)
  5097. (save-restriction
  5098. (narrow-to-region beg end)
  5099. (while (not (= shift 0))
  5100. (org-map-region func (point-min) (point-max))
  5101. (setq shift (+ delta shift)))
  5102. (goto-char (point-min))
  5103. (setq newend (point-max))))
  5104. (when (or (interactive-p) for-yank)
  5105. (message "Clipboard pasted as level %d subtree" new-level))
  5106. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5107. kill-ring
  5108. (eq org-subtree-clip (current-kill 0))
  5109. org-subtree-clip-folded)
  5110. ;; The tree was folded before it was killed/copied
  5111. (hide-subtree))
  5112. (and for-yank (goto-char newend))))
  5113. (defun org-kill-is-subtree-p (&optional txt)
  5114. "Check if the current kill is an outline subtree, or a set of trees.
  5115. Returns nil if kill does not start with a headline, or if the first
  5116. headline level is not the largest headline level in the tree.
  5117. So this will actually accept several entries of equal levels as well,
  5118. which is OK for `org-paste-subtree'.
  5119. If optional TXT is given, check this string instead of the current kill."
  5120. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5121. (start-level (and kill
  5122. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5123. org-outline-regexp "\\)")
  5124. kill)
  5125. (- (match-end 2) (match-beginning 2) 1)))
  5126. (re (concat "^" org-outline-regexp))
  5127. (start (1+ (or (match-beginning 2) -1))))
  5128. (if (not start-level)
  5129. (progn
  5130. nil) ;; does not even start with a heading
  5131. (catch 'exit
  5132. (while (setq start (string-match re kill (1+ start)))
  5133. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5134. (throw 'exit nil)))
  5135. t))))
  5136. (defvar org-markers-to-move nil
  5137. "Markers that should be moved with a cut-and-paste operation.
  5138. Those markers are stored together with their positions relative to
  5139. the start of the region.")
  5140. (defun org-save-markers-in-region (beg end)
  5141. "Check markers in region.
  5142. If these markers are between BEG and END, record their position relative
  5143. to BEG, so that after moving the block of text, we can put the markers back
  5144. into place.
  5145. This function gets called just before an entry or tree gets cut from the
  5146. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5147. called immediately, to move the markers with the entries."
  5148. (setq org-markers-to-move nil)
  5149. (when (featurep 'org-clock)
  5150. (org-clock-save-markers-for-cut-and-paste beg end))
  5151. (when (featurep 'org-agenda)
  5152. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5153. (defun org-check-and-save-marker (marker beg end)
  5154. "Check if MARKER is between BEG and END.
  5155. If yes, remember the marker and the distance to BEG."
  5156. (when (and (marker-buffer marker)
  5157. (equal (marker-buffer marker) (current-buffer)))
  5158. (if (and (>= marker beg) (< marker end))
  5159. (push (cons marker (- marker beg)) org-markers-to-move))))
  5160. (defun org-reinstall-markers-in-region (beg)
  5161. "Move all remembered markers to their position relative to BEG."
  5162. (mapc (lambda (x)
  5163. (move-marker (car x) (+ beg (cdr x))))
  5164. org-markers-to-move)
  5165. (setq org-markers-to-move nil))
  5166. (defun org-narrow-to-subtree ()
  5167. "Narrow buffer to the current subtree."
  5168. (interactive)
  5169. (save-excursion
  5170. (save-match-data
  5171. (narrow-to-region
  5172. (progn (org-back-to-heading) (point))
  5173. (progn (org-end-of-subtree t) (point))))))
  5174. ;;; Outline Sorting
  5175. (defun org-sort (with-case)
  5176. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5177. Optional argument WITH-CASE means sort case-sensitively.
  5178. With a double prefix argument, also remove duplicate entries."
  5179. (interactive "P")
  5180. (if (org-at-table-p)
  5181. (org-call-with-arg 'org-table-sort-lines with-case)
  5182. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5183. (defun org-sort-remove-invisible (s)
  5184. (remove-text-properties 0 (length s) org-rm-props s)
  5185. (while (string-match org-bracket-link-regexp s)
  5186. (setq s (replace-match (if (match-end 2)
  5187. (match-string 3 s)
  5188. (match-string 1 s)) t t s)))
  5189. s)
  5190. (defvar org-priority-regexp) ; defined later in the file
  5191. (defun org-sort-entries-or-items
  5192. (&optional with-case sorting-type getkey-func compare-func property)
  5193. "Sort entries on a certain level of an outline tree.
  5194. If there is an active region, the entries in the region are sorted.
  5195. Else, if the cursor is before the first entry, sort the top-level items.
  5196. Else, the children of the entry at point are sorted.
  5197. Sorting can be alphabetically, numerically, and by date/time as given by
  5198. the first time stamp in the entry. The command prompts for the sorting
  5199. type unless it has been given to the function through the SORTING-TYPE
  5200. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5201. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5202. called with point at the beginning of the record. It must return either
  5203. a string or a number that should serve as the sorting key for that record.
  5204. Comparing entries ignores case by default. However, with an optional argument
  5205. WITH-CASE, the sorting considers case as well."
  5206. (interactive "P")
  5207. (let ((case-func (if with-case 'identity 'downcase))
  5208. start beg end stars re re2
  5209. txt what tmp plain-list-p)
  5210. ;; Find beginning and end of region to sort
  5211. (cond
  5212. ((org-region-active-p)
  5213. ;; we will sort the region
  5214. (setq end (region-end)
  5215. what "region")
  5216. (goto-char (region-beginning))
  5217. (if (not (org-on-heading-p)) (outline-next-heading))
  5218. (setq start (point)))
  5219. ((org-at-item-p)
  5220. ;; we will sort this plain list
  5221. (org-beginning-of-item-list) (setq start (point))
  5222. (org-end-of-item-list) (setq end (point))
  5223. (goto-char start)
  5224. (setq plain-list-p t
  5225. what "plain list"))
  5226. ((or (org-on-heading-p)
  5227. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5228. ;; we will sort the children of the current headline
  5229. (org-back-to-heading)
  5230. (setq start (point)
  5231. end (progn (org-end-of-subtree t t)
  5232. (org-back-over-empty-lines)
  5233. (point))
  5234. what "children")
  5235. (goto-char start)
  5236. (show-subtree)
  5237. (outline-next-heading))
  5238. (t
  5239. ;; we will sort the top-level entries in this file
  5240. (goto-char (point-min))
  5241. (or (org-on-heading-p) (outline-next-heading))
  5242. (setq start (point) end (point-max) what "top-level")
  5243. (goto-char start)
  5244. (show-all)))
  5245. (setq beg (point))
  5246. (if (>= beg end) (error "Nothing to sort"))
  5247. (unless plain-list-p
  5248. (looking-at "\\(\\*+\\)")
  5249. (setq stars (match-string 1)
  5250. re (concat "^" (regexp-quote stars) " +")
  5251. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5252. txt (buffer-substring beg end))
  5253. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5254. (if (and (not (equal stars "*")) (string-match re2 txt))
  5255. (error "Region to sort contains a level above the first entry")))
  5256. (unless sorting-type
  5257. (message
  5258. (if plain-list-p
  5259. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5260. "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:")
  5261. what)
  5262. (setq sorting-type (read-char-exclusive))
  5263. (and (= (downcase sorting-type) ?f)
  5264. (setq getkey-func
  5265. (org-ido-completing-read "Sort using function: "
  5266. obarray 'fboundp t nil nil))
  5267. (setq getkey-func (intern getkey-func)))
  5268. (and (= (downcase sorting-type) ?r)
  5269. (setq property
  5270. (org-ido-completing-read "Property: "
  5271. (mapcar 'list (org-buffer-property-keys t))
  5272. nil t))))
  5273. (message "Sorting entries...")
  5274. (save-restriction
  5275. (narrow-to-region start end)
  5276. (let ((dcst (downcase sorting-type))
  5277. (now (current-time)))
  5278. (sort-subr
  5279. (/= dcst sorting-type)
  5280. ;; This function moves to the beginning character of the "record" to
  5281. ;; be sorted.
  5282. (if plain-list-p
  5283. (lambda nil
  5284. (if (org-at-item-p) t (goto-char (point-max))))
  5285. (lambda nil
  5286. (if (re-search-forward re nil t)
  5287. (goto-char (match-beginning 0))
  5288. (goto-char (point-max)))))
  5289. ;; This function moves to the last character of the "record" being
  5290. ;; sorted.
  5291. (if plain-list-p
  5292. 'org-end-of-item
  5293. (lambda nil
  5294. (save-match-data
  5295. (condition-case nil
  5296. (outline-forward-same-level 1)
  5297. (error
  5298. (goto-char (point-max)))))))
  5299. ;; This function returns the value that gets sorted against.
  5300. (if plain-list-p
  5301. (lambda nil
  5302. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5303. (cond
  5304. ((= dcst ?n)
  5305. (string-to-number (buffer-substring (match-end 0)
  5306. (point-at-eol))))
  5307. ((= dcst ?a)
  5308. (buffer-substring (match-end 0) (point-at-eol)))
  5309. ((= dcst ?t)
  5310. (if (re-search-forward org-ts-regexp
  5311. (point-at-eol) t)
  5312. (org-time-string-to-time (match-string 0))
  5313. now))
  5314. ((= dcst ?f)
  5315. (if getkey-func
  5316. (progn
  5317. (setq tmp (funcall getkey-func))
  5318. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5319. tmp)
  5320. (error "Invalid key function `%s'" getkey-func)))
  5321. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5322. (lambda nil
  5323. (cond
  5324. ((= dcst ?n)
  5325. (if (looking-at org-complex-heading-regexp)
  5326. (string-to-number (match-string 4))
  5327. nil))
  5328. ((= dcst ?a)
  5329. (if (looking-at org-complex-heading-regexp)
  5330. (funcall case-func (match-string 4))
  5331. nil))
  5332. ((= dcst ?t)
  5333. (if (re-search-forward org-ts-regexp
  5334. (save-excursion
  5335. (forward-line 2)
  5336. (point)) t)
  5337. (org-time-string-to-time (match-string 0))
  5338. now))
  5339. ((= dcst ?p)
  5340. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5341. (string-to-char (match-string 2))
  5342. org-default-priority))
  5343. ((= dcst ?r)
  5344. (or (org-entry-get nil property) ""))
  5345. ((= dcst ?o)
  5346. (if (looking-at org-complex-heading-regexp)
  5347. (- 9999 (length (member (match-string 2)
  5348. org-todo-keywords-1)))))
  5349. ((= dcst ?f)
  5350. (if getkey-func
  5351. (progn
  5352. (setq tmp (funcall getkey-func))
  5353. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5354. tmp)
  5355. (error "Invalid key function `%s'" getkey-func)))
  5356. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5357. nil
  5358. (cond
  5359. ((= dcst ?a) 'string<)
  5360. ((= dcst ?t) 'time-less-p)
  5361. ((= dcst ?f) compare-func)
  5362. (t nil)))))
  5363. (message "Sorting entries...done")))
  5364. (defun org-do-sort (table what &optional with-case sorting-type)
  5365. "Sort TABLE of WHAT according to SORTING-TYPE.
  5366. The user will be prompted for the SORTING-TYPE if the call to this
  5367. function does not specify it. WHAT is only for the prompt, to indicate
  5368. what is being sorted. The sorting key will be extracted from
  5369. the car of the elements of the table.
  5370. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5371. (unless sorting-type
  5372. (message
  5373. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5374. what)
  5375. (setq sorting-type (read-char-exclusive)))
  5376. (let ((dcst (downcase sorting-type))
  5377. extractfun comparefun)
  5378. ;; Define the appropriate functions
  5379. (cond
  5380. ((= dcst ?n)
  5381. (setq extractfun 'string-to-number
  5382. comparefun (if (= dcst sorting-type) '< '>)))
  5383. ((= dcst ?a)
  5384. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5385. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5386. comparefun (if (= dcst sorting-type)
  5387. 'string<
  5388. (lambda (a b) (and (not (string< a b))
  5389. (not (string= a b)))))))
  5390. ((= dcst ?t)
  5391. (setq extractfun
  5392. (lambda (x)
  5393. (if (or (string-match org-ts-regexp x)
  5394. (string-match org-ts-regexp-both x))
  5395. (time-to-seconds
  5396. (org-time-string-to-time (match-string 0 x)))
  5397. 0))
  5398. comparefun (if (= dcst sorting-type) '< '>)))
  5399. (t (error "Invalid sorting type `%c'" sorting-type)))
  5400. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5401. table)
  5402. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5403. ;;; Editing source examples
  5404. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5405. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5406. (defvar org-edit-src-force-single-line nil)
  5407. (defvar org-edit-src-from-org-mode nil)
  5408. (defvar org-edit-src-picture nil)
  5409. (define-minor-mode org-exit-edit-mode
  5410. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5411. (defun org-edit-src-code ()
  5412. "Edit the source code example at point.
  5413. An indirect buffer is created, and that buffer is then narrowed to the
  5414. example at point and switched to the correct language mode. When done,
  5415. exit by killing the buffer with \\[org-edit-src-exit]."
  5416. (interactive)
  5417. (let ((line (org-current-line))
  5418. (case-fold-search t)
  5419. (msg (substitute-command-keys
  5420. "Edit, then exit with C-c ' (C-c and single quote)"))
  5421. (info (org-edit-src-find-region-and-lang))
  5422. (org-mode-p (eq major-mode 'org-mode))
  5423. beg end lang lang-f single lfmt)
  5424. (if (not info)
  5425. nil
  5426. (setq beg (nth 0 info)
  5427. end (nth 1 info)
  5428. lang (nth 2 info)
  5429. single (nth 3 info)
  5430. lfmt (nth 4 info)
  5431. lang-f (intern (concat lang "-mode")))
  5432. (unless (functionp lang-f)
  5433. (error "No such language mode: %s" lang-f))
  5434. (goto-line line)
  5435. (if (get-buffer "*Org Edit Src Example*")
  5436. (kill-buffer "*Org Edit Src Example*"))
  5437. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5438. "*Org Edit Src Example*"))
  5439. (narrow-to-region beg end)
  5440. (remove-text-properties beg end '(display nil invisible nil
  5441. intangible nil))
  5442. (let ((org-inhibit-startup t))
  5443. (funcall lang-f))
  5444. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5445. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5446. (when lfmt
  5447. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5448. (when org-mode-p
  5449. (goto-char (point-min))
  5450. (while (re-search-forward "^," nil t)
  5451. (replace-match "")))
  5452. (goto-line line)
  5453. (org-exit-edit-mode)
  5454. (org-set-local 'header-line-format msg)
  5455. (message "%s" msg)
  5456. t)))
  5457. (defun org-edit-fixed-width-region ()
  5458. "Edit the fixed-width ascii drawing at point.
  5459. This must be a region where each line starts with a colon followed by
  5460. a space character.
  5461. An indirect buffer is created, and that buffer is then narrowed to the
  5462. example at point and switched to artist-mode. When done,
  5463. exit by killing the buffer with \\[org-edit-src-exit]."
  5464. (interactive)
  5465. (let ((line (org-current-line))
  5466. (case-fold-search t)
  5467. (msg (substitute-command-keys
  5468. "Edit, then exit with C-c ' (C-c and single quote)"))
  5469. (org-mode-p (eq major-mode 'org-mode))
  5470. beg end)
  5471. (beginning-of-line 1)
  5472. (if (looking-at "[ \t]*[^:\n \t]")
  5473. nil
  5474. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5475. (setq beg (point) end beg)
  5476. (save-excursion
  5477. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5478. (setq beg (point-at-bol 2))
  5479. (setq beg (point))))
  5480. (save-excursion
  5481. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5482. (setq end (1- (match-beginning 0)))
  5483. (setq end (point))))
  5484. (goto-line line))
  5485. (if (get-buffer "*Org Edit Picture*")
  5486. (kill-buffer "*Org Edit Picture*"))
  5487. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5488. "*Org Edit Picture*"))
  5489. (narrow-to-region beg end)
  5490. (remove-text-properties beg end '(display nil invisible nil
  5491. intangible nil))
  5492. (when (fboundp 'font-lock-unfontify-region)
  5493. (font-lock-unfontify-region (point-min) (point-max)))
  5494. (cond
  5495. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5496. (fundamental-mode)
  5497. (artist-mode 1))
  5498. (t (funcall org-edit-fixed-width-region-mode)))
  5499. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5500. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5501. (set (make-local-variable 'org-edit-src-picture) t)
  5502. (goto-char (point-min))
  5503. (while (re-search-forward "^[ \t]*: ?" nil t)
  5504. (replace-match ""))
  5505. (goto-line line)
  5506. (org-exit-edit-mode)
  5507. (org-set-local 'header-line-format msg)
  5508. (message "%s" msg)
  5509. t)))
  5510. (defun org-edit-src-find-region-and-lang ()
  5511. "Find the region and language for a local edit.
  5512. Return a list with beginning and end of the region, a string representing
  5513. the language, a switch telling of the content should be in a single line."
  5514. (let ((re-list
  5515. (append
  5516. org-edit-src-region-extra
  5517. '(
  5518. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5519. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5520. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5521. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5522. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5523. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5524. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5525. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5526. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5527. ("^#\\+html:" "\n" "html" single-line)
  5528. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5529. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5530. ("^#\\+latex:" "\n" "latex" single-line)
  5531. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5532. ("^#\\+ascii:" "\n" "ascii" single-line)
  5533. )))
  5534. (pos (point))
  5535. re1 re2 single beg end lang lfmt match-re1)
  5536. (catch 'exit
  5537. (while (setq entry (pop re-list))
  5538. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5539. single (nth 3 entry))
  5540. (save-excursion
  5541. (if (or (looking-at re1)
  5542. (re-search-backward re1 nil t))
  5543. (progn
  5544. (setq match-re1 (match-string 0))
  5545. (setq beg (match-end 0)
  5546. lang (org-edit-src-get-lang lang)
  5547. lfmt (org-edit-src-get-label-format match-re1))
  5548. (if (and (re-search-forward re2 nil t)
  5549. (>= (match-end 0) pos))
  5550. (throw 'exit (list beg (match-beginning 0)
  5551. lang single lfmt))))
  5552. (if (or (looking-at re2)
  5553. (re-search-forward re2 nil t))
  5554. (progn
  5555. (setq end (match-beginning 0))
  5556. (if (and (re-search-backward re1 nil t)
  5557. (<= (match-beginning 0) pos))
  5558. (progn
  5559. (setq lfmt (org-edit-src-get-label-format
  5560. (match-string 0)))
  5561. (throw 'exit
  5562. (list (match-end 0) end
  5563. (org-edit-src-get-lang lang)
  5564. single lfmt))))))))))))
  5565. (defun org-edit-src-get-lang (lang)
  5566. "Extract the src language."
  5567. (let ((m (match-string 0)))
  5568. (cond
  5569. ((stringp lang) lang)
  5570. ((integerp lang) (match-string lang))
  5571. ((and (eq lang 'lang)
  5572. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5573. (match-string 1 m))
  5574. ((and (eq lang 'style)
  5575. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5576. (match-string 1 m))
  5577. (t "fundamental"))))
  5578. (defun org-edit-src-get-label-format (s)
  5579. "Extract the label format."
  5580. (save-match-data
  5581. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5582. (match-string 1 s))))
  5583. (defun org-edit-src-exit ()
  5584. "Exit special edit and protect problematic lines."
  5585. (interactive)
  5586. (unless (buffer-base-buffer (current-buffer))
  5587. (error "This is not an indirect buffer, something is wrong..."))
  5588. (unless (> (point-min) 1)
  5589. (error "This buffer is not narrowed, something is wrong..."))
  5590. (goto-char (point-min))
  5591. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5592. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5593. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5594. (goto-char (point-min))
  5595. (while (re-search-forward "\n" nil t)
  5596. (replace-match " "))
  5597. (goto-char (point-min))
  5598. (if (looking-at "\\s-*") (replace-match " "))
  5599. (if (re-search-forward "\\s-+\\'" nil t)
  5600. (replace-match "")))
  5601. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5602. (goto-char (point-min))
  5603. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5604. (replace-match ",\\1"))
  5605. (when font-lock-mode
  5606. (font-lock-unfontify-region (point-min) (point-max)))
  5607. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5608. (when (org-bound-and-true-p org-edit-src-picture)
  5609. (untabify (point-min) (point-max))
  5610. (goto-char (point-min))
  5611. (while (re-search-forward "^" nil t)
  5612. (replace-match ": "))
  5613. (when font-lock-mode
  5614. (font-lock-unfontify-region (point-min) (point-max)))
  5615. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5616. (kill-buffer (current-buffer))
  5617. (and (org-mode-p) (org-restart-font-lock)))
  5618. ;;; The orgstruct minor mode
  5619. ;; Define a minor mode which can be used in other modes in order to
  5620. ;; integrate the org-mode structure editing commands.
  5621. ;; This is really a hack, because the org-mode structure commands use
  5622. ;; keys which normally belong to the major mode. Here is how it
  5623. ;; works: The minor mode defines all the keys necessary to operate the
  5624. ;; structure commands, but wraps the commands into a function which
  5625. ;; tests if the cursor is currently at a headline or a plain list
  5626. ;; item. If that is the case, the structure command is used,
  5627. ;; temporarily setting many Org-mode variables like regular
  5628. ;; expressions for filling etc. However, when any of those keys is
  5629. ;; used at a different location, function uses `key-binding' to look
  5630. ;; up if the key has an associated command in another currently active
  5631. ;; keymap (minor modes, major mode, global), and executes that
  5632. ;; command. There might be problems if any of the keys is otherwise
  5633. ;; used as a prefix key.
  5634. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5635. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5636. ;; addresses this by checking explicitly for both bindings.
  5637. (defvar orgstruct-mode-map (make-sparse-keymap)
  5638. "Keymap for the minor `orgstruct-mode'.")
  5639. (defvar org-local-vars nil
  5640. "List of local variables, for use by `orgstruct-mode'")
  5641. ;;;###autoload
  5642. (define-minor-mode orgstruct-mode
  5643. "Toggle the minor more `orgstruct-mode'.
  5644. This mode is for using Org-mode structure commands in other modes.
  5645. The following key behave as if Org-mode was active, if the cursor
  5646. is on a headline, or on a plain list item (both in the definition
  5647. of Org-mode).
  5648. M-up Move entry/item up
  5649. M-down Move entry/item down
  5650. M-left Promote
  5651. M-right Demote
  5652. M-S-up Move entry/item up
  5653. M-S-down Move entry/item down
  5654. M-S-left Promote subtree
  5655. M-S-right Demote subtree
  5656. M-q Fill paragraph and items like in Org-mode
  5657. C-c ^ Sort entries
  5658. C-c - Cycle list bullet
  5659. TAB Cycle item visibility
  5660. M-RET Insert new heading/item
  5661. S-M-RET Insert new TODO heading / Checkbox item
  5662. C-c C-c Set tags / toggle checkbox"
  5663. nil " OrgStruct" nil
  5664. (org-load-modules-maybe)
  5665. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5666. ;;;###autoload
  5667. (defun turn-on-orgstruct ()
  5668. "Unconditionally turn on `orgstruct-mode'."
  5669. (orgstruct-mode 1))
  5670. (defun orgstruct++-mode (&optional arg)
  5671. "Toggle `orgstruct-mode', the enhanced version of it.
  5672. In addition to setting orgstruct-mode, this also exports all indentation and
  5673. autofilling variables from org-mode into the buffer. It will also
  5674. recognize item context in multiline items.
  5675. Note that turning off orgstruct-mode will *not* remove the
  5676. indentation/paragraph settings. This can only be done by refreshing the
  5677. major mode, for example with \[normal-mode]."
  5678. (interactive "P")
  5679. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5680. (if (< arg 1)
  5681. (orgstruct-mode -1)
  5682. (orgstruct-mode 1)
  5683. (let (var val)
  5684. (mapc
  5685. (lambda (x)
  5686. (when (string-match
  5687. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5688. (symbol-name (car x)))
  5689. (setq var (car x) val (nth 1 x))
  5690. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5691. org-local-vars)
  5692. (org-set-local 'orgstruct-is-++ t))))
  5693. (defvar orgstruct-is-++ nil
  5694. "Is orgstruct-mode in ++ version in the current-buffer?")
  5695. (make-variable-buffer-local 'orgstruct-is-++)
  5696. ;;;###autoload
  5697. (defun turn-on-orgstruct++ ()
  5698. "Unconditionally turn on `orgstruct++-mode'."
  5699. (orgstruct++-mode 1))
  5700. (defun orgstruct-error ()
  5701. "Error when there is no default binding for a structure key."
  5702. (interactive)
  5703. (error "This key has no function outside structure elements"))
  5704. (defun orgstruct-setup ()
  5705. "Setup orgstruct keymaps."
  5706. (let ((nfunc 0)
  5707. (bindings
  5708. (list
  5709. '([(meta up)] org-metaup)
  5710. '([(meta down)] org-metadown)
  5711. '([(meta left)] org-metaleft)
  5712. '([(meta right)] org-metaright)
  5713. '([(meta shift up)] org-shiftmetaup)
  5714. '([(meta shift down)] org-shiftmetadown)
  5715. '([(meta shift left)] org-shiftmetaleft)
  5716. '([(meta shift right)] org-shiftmetaright)
  5717. '([(shift up)] org-shiftup)
  5718. '([(shift down)] org-shiftdown)
  5719. '([(shift left)] org-shiftleft)
  5720. '([(shift right)] org-shiftright)
  5721. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5722. '("\M-q" fill-paragraph)
  5723. '("\C-c^" org-sort)
  5724. '("\C-c-" org-cycle-list-bullet)))
  5725. elt key fun cmd)
  5726. (while (setq elt (pop bindings))
  5727. (setq nfunc (1+ nfunc))
  5728. (setq key (org-key (car elt))
  5729. fun (nth 1 elt)
  5730. cmd (orgstruct-make-binding fun nfunc key))
  5731. (org-defkey orgstruct-mode-map key cmd))
  5732. ;; Special treatment needed for TAB and RET
  5733. (org-defkey orgstruct-mode-map [(tab)]
  5734. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5735. (org-defkey orgstruct-mode-map "\C-i"
  5736. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5737. (org-defkey orgstruct-mode-map "\M-\C-m"
  5738. (orgstruct-make-binding 'org-insert-heading 105
  5739. "\M-\C-m" [(meta return)]))
  5740. (org-defkey orgstruct-mode-map [(meta return)]
  5741. (orgstruct-make-binding 'org-insert-heading 106
  5742. [(meta return)] "\M-\C-m"))
  5743. (org-defkey orgstruct-mode-map [(shift meta return)]
  5744. (orgstruct-make-binding 'org-insert-todo-heading 107
  5745. [(meta return)] "\M-\C-m"))
  5746. (unless org-local-vars
  5747. (setq org-local-vars (org-get-local-variables)))
  5748. t))
  5749. (defun orgstruct-make-binding (fun n &rest keys)
  5750. "Create a function for binding in the structure minor mode.
  5751. FUN is the command to call inside a table. N is used to create a unique
  5752. command name. KEYS are keys that should be checked in for a command
  5753. to execute outside of tables."
  5754. (eval
  5755. (list 'defun
  5756. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5757. '(arg)
  5758. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5759. "Outside of structure, run the binding of `"
  5760. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5761. "'.")
  5762. '(interactive "p")
  5763. (list 'if
  5764. `(org-context-p 'headline 'item
  5765. (and orgstruct-is-++
  5766. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5767. 'item-body))
  5768. (list 'org-run-like-in-org-mode (list 'quote fun))
  5769. (list 'let '(orgstruct-mode)
  5770. (list 'call-interactively
  5771. (append '(or)
  5772. (mapcar (lambda (k)
  5773. (list 'key-binding k))
  5774. keys)
  5775. '('orgstruct-error))))))))
  5776. (defun org-context-p (&rest contexts)
  5777. "Check if local context is any of CONTEXTS.
  5778. Possible values in the list of contexts are `table', `headline', and `item'."
  5779. (let ((pos (point)))
  5780. (goto-char (point-at-bol))
  5781. (prog1 (or (and (memq 'table contexts)
  5782. (looking-at "[ \t]*|"))
  5783. (and (memq 'headline contexts)
  5784. ;;????????? (looking-at "\\*+"))
  5785. (looking-at outline-regexp))
  5786. (and (memq 'item contexts)
  5787. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5788. (and (memq 'item-body contexts)
  5789. (org-in-item-p)))
  5790. (goto-char pos))))
  5791. (defun org-get-local-variables ()
  5792. "Return a list of all local variables in an org-mode buffer."
  5793. (let (varlist)
  5794. (with-current-buffer (get-buffer-create "*Org tmp*")
  5795. (erase-buffer)
  5796. (org-mode)
  5797. (setq varlist (buffer-local-variables)))
  5798. (kill-buffer "*Org tmp*")
  5799. (delq nil
  5800. (mapcar
  5801. (lambda (x)
  5802. (setq x
  5803. (if (symbolp x)
  5804. (list x)
  5805. (list (car x) (list 'quote (cdr x)))))
  5806. (if (string-match
  5807. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5808. (symbol-name (car x)))
  5809. x nil))
  5810. varlist))))
  5811. ;;;###autoload
  5812. (defun org-run-like-in-org-mode (cmd)
  5813. "Run a command, pretending that the current buffer is in Org-mode.
  5814. This will temporarily bind local variables that are typically bound in
  5815. Org-mode to the values they have in Org-mode, and then interactively
  5816. call CMD."
  5817. (org-load-modules-maybe)
  5818. (unless org-local-vars
  5819. (setq org-local-vars (org-get-local-variables)))
  5820. (eval (list 'let org-local-vars
  5821. (list 'call-interactively (list 'quote cmd)))))
  5822. ;;;; Archiving
  5823. (defun org-get-category (&optional pos)
  5824. "Get the category applying to position POS."
  5825. (get-text-property (or pos (point)) 'org-category))
  5826. (defun org-refresh-category-properties ()
  5827. "Refresh category text properties in the buffer."
  5828. (let ((def-cat (cond
  5829. ((null org-category)
  5830. (if buffer-file-name
  5831. (file-name-sans-extension
  5832. (file-name-nondirectory buffer-file-name))
  5833. "???"))
  5834. ((symbolp org-category) (symbol-name org-category))
  5835. (t org-category)))
  5836. beg end cat pos optionp)
  5837. (org-unmodified
  5838. (save-excursion
  5839. (save-restriction
  5840. (widen)
  5841. (goto-char (point-min))
  5842. (put-text-property (point) (point-max) 'org-category def-cat)
  5843. (while (re-search-forward
  5844. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5845. (setq pos (match-end 0)
  5846. optionp (equal (char-after (match-beginning 0)) ?#)
  5847. cat (org-trim (match-string 2)))
  5848. (if optionp
  5849. (setq beg (point-at-bol) end (point-max))
  5850. (org-back-to-heading t)
  5851. (setq beg (point) end (org-end-of-subtree t t)))
  5852. (put-text-property beg end 'org-category cat)
  5853. (goto-char pos)))))))
  5854. ;;;; Link Stuff
  5855. ;;; Link abbreviations
  5856. (defun org-link-expand-abbrev (link)
  5857. "Apply replacements as defined in `org-link-abbrev-alist."
  5858. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5859. (let* ((key (match-string 1 link))
  5860. (as (or (assoc key org-link-abbrev-alist-local)
  5861. (assoc key org-link-abbrev-alist)))
  5862. (tag (and (match-end 2) (match-string 3 link)))
  5863. rpl)
  5864. (if (not as)
  5865. link
  5866. (setq rpl (cdr as))
  5867. (cond
  5868. ((symbolp rpl) (funcall rpl tag))
  5869. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5870. ((string-match "%h" rpl)
  5871. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5872. (t (concat rpl tag)))))
  5873. link))
  5874. ;;; Storing and inserting links
  5875. (defvar org-insert-link-history nil
  5876. "Minibuffer history for links inserted with `org-insert-link'.")
  5877. (defvar org-stored-links nil
  5878. "Contains the links stored with `org-store-link'.")
  5879. (defvar org-store-link-plist nil
  5880. "Plist with info about the most recently link created with `org-store-link'.")
  5881. (defvar org-link-protocols nil
  5882. "Link protocols added to Org-mode using `org-add-link-type'.")
  5883. (defvar org-store-link-functions nil
  5884. "List of functions that are called to create and store a link.
  5885. Each function will be called in turn until one returns a non-nil
  5886. value. Each function should check if it is responsible for creating
  5887. this link (for example by looking at the major mode).
  5888. If not, it must exit and return nil.
  5889. If yes, it should return a non-nil value after a calling
  5890. `org-store-link-props' with a list of properties and values.
  5891. Special properties are:
  5892. :type The link prefix. like \"http\". This must be given.
  5893. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5894. This is obligatory as well.
  5895. :description Optional default description for the second pair
  5896. of brackets in an Org-mode link. The user can still change
  5897. this when inserting this link into an Org-mode buffer.
  5898. In addition to these, any additional properties can be specified
  5899. and then used in remember templates.")
  5900. (defun org-add-link-type (type &optional follow export)
  5901. "Add TYPE to the list of `org-link-types'.
  5902. Re-compute all regular expressions depending on `org-link-types'
  5903. FOLLOW and EXPORT are two functions.
  5904. FOLLOW should take the link path as the single argument and do whatever
  5905. is necessary to follow the link, for example find a file or display
  5906. a mail message.
  5907. EXPORT should format the link path for export to one of the export formats.
  5908. It should be a function accepting three arguments:
  5909. path the path of the link, the text after the prefix (like \"http:\")
  5910. desc the description of the link, if any, nil if there was no description
  5911. format the export format, a symbol like `html' or `latex'.
  5912. The function may use the FORMAT information to return different values
  5913. depending on the format. The return value will be put literally into
  5914. the exported file.
  5915. Org-mode has a built-in default for exporting links. If you are happy with
  5916. this default, there is no need to define an export function for the link
  5917. type. For a simple example of an export function, see `org-bbdb.el'."
  5918. (add-to-list 'org-link-types type t)
  5919. (org-make-link-regexps)
  5920. (if (assoc type org-link-protocols)
  5921. (setcdr (assoc type org-link-protocols) (list follow export))
  5922. (push (list type follow export) org-link-protocols)))
  5923. ;;;###autoload
  5924. (defun org-store-link (arg)
  5925. "\\<org-mode-map>Store an org-link to the current location.
  5926. This link is added to `org-stored-links' and can later be inserted
  5927. into an org-buffer with \\[org-insert-link].
  5928. For some link types, a prefix arg is interpreted:
  5929. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5930. For file links, arg negates `org-context-in-file-links'."
  5931. (interactive "P")
  5932. (org-load-modules-maybe)
  5933. (setq org-store-link-plist nil) ; reset
  5934. (let (link cpltxt desc description search txt)
  5935. (cond
  5936. ((run-hook-with-args-until-success 'org-store-link-functions)
  5937. (setq link (plist-get org-store-link-plist :link)
  5938. desc (or (plist-get org-store-link-plist :description) link)))
  5939. ((equal (buffer-name) "*Org Edit Src Example*")
  5940. (let (label gc)
  5941. (while (or (not label)
  5942. (save-excursion
  5943. (save-restriction
  5944. (widen)
  5945. (goto-char (point-min))
  5946. (re-search-forward
  5947. (regexp-quote (format org-coderef-label-format label))
  5948. nil t))))
  5949. (when label (message "Label exists already") (sit-for 2))
  5950. (setq label (read-string "Code line label: " label)))
  5951. (end-of-line 1)
  5952. (setq link (format org-coderef-label-format label))
  5953. (setq gc (- 79 (length link)))
  5954. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5955. (insert link)
  5956. (setq link (concat "(" label ")") desc nil)))
  5957. ((eq major-mode 'calendar-mode)
  5958. (let ((cd (calendar-cursor-to-date)))
  5959. (setq link
  5960. (format-time-string
  5961. (car org-time-stamp-formats)
  5962. (apply 'encode-time
  5963. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5964. nil nil nil))))
  5965. (org-store-link-props :type "calendar" :date cd)))
  5966. ((eq major-mode 'w3-mode)
  5967. (setq cpltxt (url-view-url t)
  5968. link (org-make-link cpltxt))
  5969. (org-store-link-props :type "w3" :url (url-view-url t)))
  5970. ((eq major-mode 'w3m-mode)
  5971. (setq cpltxt (or w3m-current-title w3m-current-url)
  5972. link (org-make-link w3m-current-url))
  5973. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5974. ((setq search (run-hook-with-args-until-success
  5975. 'org-create-file-search-functions))
  5976. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5977. "::" search))
  5978. (setq cpltxt (or description link)))
  5979. ((eq major-mode 'image-mode)
  5980. (setq cpltxt (concat "file:"
  5981. (abbreviate-file-name buffer-file-name))
  5982. link (org-make-link cpltxt))
  5983. (org-store-link-props :type "image" :file buffer-file-name))
  5984. ((eq major-mode 'dired-mode)
  5985. ;; link to the file in the current line
  5986. (setq cpltxt (concat "file:"
  5987. (abbreviate-file-name
  5988. (expand-file-name
  5989. (dired-get-filename nil t))))
  5990. link (org-make-link cpltxt)))
  5991. ((and buffer-file-name (org-mode-p))
  5992. (cond
  5993. ((org-in-regexp "<<\\(.*?\\)>>")
  5994. (setq cpltxt
  5995. (concat "file:"
  5996. (abbreviate-file-name buffer-file-name)
  5997. "::" (match-string 1))
  5998. link (org-make-link cpltxt)))
  5999. ((and (featurep 'org-id)
  6000. (or (eq org-link-to-org-use-id t)
  6001. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6002. (interactive-p))
  6003. (and org-link-to-org-use-id
  6004. (condition-case nil
  6005. (org-entry-get nil "ID")
  6006. (error nil)))))
  6007. ;; We can make a link using the ID.
  6008. (setq link (condition-case nil
  6009. (prog1 (org-id-store-link)
  6010. (setq desc (plist-get org-store-link-plist
  6011. :description)))
  6012. (error
  6013. ;; probably before first headline, link to file only
  6014. (concat "file:"
  6015. (abbreviate-file-name buffer-file-name))))))
  6016. (t
  6017. ;; Just link to current headline
  6018. (setq cpltxt (concat "file:"
  6019. (abbreviate-file-name buffer-file-name)))
  6020. ;; Add a context search string
  6021. (when (org-xor org-context-in-file-links arg)
  6022. (setq txt (cond
  6023. ((org-on-heading-p) nil)
  6024. ((org-region-active-p)
  6025. (buffer-substring (region-beginning) (region-end)))
  6026. (t nil)))
  6027. (when (or (null txt) (string-match "\\S-" txt))
  6028. (setq cpltxt
  6029. (concat cpltxt "::"
  6030. (condition-case nil
  6031. (org-make-org-heading-search-string txt)
  6032. (error "")))
  6033. desc "NONE")))
  6034. (if (string-match "::\\'" cpltxt)
  6035. (setq cpltxt (substring cpltxt 0 -2)))
  6036. (setq link (org-make-link cpltxt)))))
  6037. ((buffer-file-name (buffer-base-buffer))
  6038. ;; Just link to this file here.
  6039. (setq cpltxt (concat "file:"
  6040. (abbreviate-file-name
  6041. (buffer-file-name (buffer-base-buffer)))))
  6042. ;; Add a context string
  6043. (when (org-xor org-context-in-file-links arg)
  6044. (setq txt (if (org-region-active-p)
  6045. (buffer-substring (region-beginning) (region-end))
  6046. (buffer-substring (point-at-bol) (point-at-eol))))
  6047. ;; Only use search option if there is some text.
  6048. (when (string-match "\\S-" txt)
  6049. (setq cpltxt
  6050. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6051. desc "NONE")))
  6052. (setq link (org-make-link cpltxt)))
  6053. ((interactive-p)
  6054. (error "Cannot link to a buffer which is not visiting a file"))
  6055. (t (setq link nil)))
  6056. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6057. (setq link (or link cpltxt)
  6058. desc (or desc cpltxt))
  6059. (if (equal desc "NONE") (setq desc nil))
  6060. (if (and (interactive-p) link)
  6061. (progn
  6062. (setq org-stored-links
  6063. (cons (list link desc) org-stored-links))
  6064. (message "Stored: %s" (or desc link)))
  6065. (and link (org-make-link-string link desc)))))
  6066. (defun org-store-link-props (&rest plist)
  6067. "Store link properties, extract names and addresses."
  6068. (let (x adr)
  6069. (when (setq x (plist-get plist :from))
  6070. (setq adr (mail-extract-address-components x))
  6071. (setq plist (plist-put plist :fromname (car adr)))
  6072. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6073. (when (setq x (plist-get plist :to))
  6074. (setq adr (mail-extract-address-components x))
  6075. (setq plist (plist-put plist :toname (car adr)))
  6076. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6077. (let ((from (plist-get plist :from))
  6078. (to (plist-get plist :to)))
  6079. (when (and from to org-from-is-user-regexp)
  6080. (setq plist
  6081. (plist-put plist :fromto
  6082. (if (string-match org-from-is-user-regexp from)
  6083. (concat "to %t")
  6084. (concat "from %f"))))))
  6085. (setq org-store-link-plist plist))
  6086. (defun org-add-link-props (&rest plist)
  6087. "Add these properties to the link property list."
  6088. (let (key value)
  6089. (while plist
  6090. (setq key (pop plist) value (pop plist))
  6091. (setq org-store-link-plist
  6092. (plist-put org-store-link-plist key value)))))
  6093. (defun org-email-link-description (&optional fmt)
  6094. "Return the description part of an email link.
  6095. This takes information from `org-store-link-plist' and formats it
  6096. according to FMT (default from `org-email-link-description-format')."
  6097. (setq fmt (or fmt org-email-link-description-format))
  6098. (let* ((p org-store-link-plist)
  6099. (to (plist-get p :toaddress))
  6100. (from (plist-get p :fromaddress))
  6101. (table
  6102. (list
  6103. (cons "%c" (plist-get p :fromto))
  6104. (cons "%F" (plist-get p :from))
  6105. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6106. (cons "%T" (plist-get p :to))
  6107. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6108. (cons "%s" (plist-get p :subject))
  6109. (cons "%m" (plist-get p :message-id)))))
  6110. (when (string-match "%c" fmt)
  6111. ;; Check if the user wrote this message
  6112. (if (and org-from-is-user-regexp from to
  6113. (save-match-data (string-match org-from-is-user-regexp from)))
  6114. (setq fmt (replace-match "to %t" t t fmt))
  6115. (setq fmt (replace-match "from %f" t t fmt))))
  6116. (org-replace-escapes fmt table)))
  6117. (defun org-make-org-heading-search-string (&optional string heading)
  6118. "Make search string for STRING or current headline."
  6119. (interactive)
  6120. (let ((s (or string (org-get-heading))))
  6121. (unless (and string (not heading))
  6122. ;; We are using a headline, clean up garbage in there.
  6123. (if (string-match org-todo-regexp s)
  6124. (setq s (replace-match "" t t s)))
  6125. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6126. (setq s (replace-match "" t t s)))
  6127. (setq s (org-trim s))
  6128. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6129. org-comment-string "\\)") s)
  6130. (setq s (replace-match "" t t s)))
  6131. (while (string-match org-ts-regexp s)
  6132. (setq s (replace-match "" t t s))))
  6133. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6134. (setq s (replace-match " " t t s)))
  6135. (or string (setq s (concat "*" s))) ; Add * for headlines
  6136. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6137. (defun org-make-link (&rest strings)
  6138. "Concatenate STRINGS."
  6139. (apply 'concat strings))
  6140. (defun org-make-link-string (link &optional description)
  6141. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6142. (unless (string-match "\\S-" link)
  6143. (error "Empty link"))
  6144. (when (stringp description)
  6145. ;; Remove brackets from the description, they are fatal.
  6146. (while (string-match "\\[" description)
  6147. (setq description (replace-match "{" t t description)))
  6148. (while (string-match "\\]" description)
  6149. (setq description (replace-match "}" t t description))))
  6150. (when (equal (org-link-escape link) description)
  6151. ;; No description needed, it is identical
  6152. (setq description nil))
  6153. (when (and (not description)
  6154. (not (equal link (org-link-escape link))))
  6155. (setq description (org-extract-attributes link)))
  6156. (concat "[[" (org-link-escape link) "]"
  6157. (if description (concat "[" description "]") "")
  6158. "]"))
  6159. (defconst org-link-escape-chars
  6160. '((?\ . "%20")
  6161. (?\[ . "%5B")
  6162. (?\] . "%5D")
  6163. (?\340 . "%E0") ; `a
  6164. (?\342 . "%E2") ; ^a
  6165. (?\347 . "%E7") ; ,c
  6166. (?\350 . "%E8") ; `e
  6167. (?\351 . "%E9") ; 'e
  6168. (?\352 . "%EA") ; ^e
  6169. (?\356 . "%EE") ; ^i
  6170. (?\364 . "%F4") ; ^o
  6171. (?\371 . "%F9") ; `u
  6172. (?\373 . "%FB") ; ^u
  6173. (?\; . "%3B")
  6174. (?? . "%3F")
  6175. (?= . "%3D")
  6176. (?+ . "%2B")
  6177. )
  6178. "Association list of escapes for some characters problematic in links.
  6179. This is the list that is used for internal purposes.")
  6180. (defconst org-link-escape-chars-browser
  6181. '((?\ . "%20")) ; 32 for the SPC char
  6182. "Association list of escapes for some characters problematic in links.
  6183. This is the list that is used before handing over to the browser.")
  6184. (defun org-link-escape (text &optional table)
  6185. "Escape characters in TEXT that are problematic for links."
  6186. (setq table (or table org-link-escape-chars))
  6187. (when text
  6188. (let ((re (mapconcat (lambda (x) (regexp-quote
  6189. (char-to-string (car x))))
  6190. table "\\|")))
  6191. (while (string-match re text)
  6192. (setq text
  6193. (replace-match
  6194. (cdr (assoc (string-to-char (match-string 0 text))
  6195. table))
  6196. t t text)))
  6197. text)))
  6198. (defun org-link-unescape (text &optional table)
  6199. "Reverse the action of `org-link-escape'."
  6200. (setq table (or table org-link-escape-chars))
  6201. (when text
  6202. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6203. table "\\|")))
  6204. (while (string-match re text)
  6205. (setq text
  6206. (replace-match
  6207. (char-to-string (car (rassoc (match-string 0 text) table)))
  6208. t t text)))
  6209. text)))
  6210. (defun org-xor (a b)
  6211. "Exclusive or."
  6212. (if a (not b) b))
  6213. (defun org-fixup-message-id-for-http (s)
  6214. "Replace special characters in a message id, so it can be used in an http query."
  6215. (while (string-match "<" s)
  6216. (setq s (replace-match "%3C" t t s)))
  6217. (while (string-match ">" s)
  6218. (setq s (replace-match "%3E" t t s)))
  6219. (while (string-match "@" s)
  6220. (setq s (replace-match "%40" t t s)))
  6221. s)
  6222. ;;;###autoload
  6223. (defun org-insert-link-global ()
  6224. "Insert a link like Org-mode does.
  6225. This command can be called in any mode to insert a link in Org-mode syntax."
  6226. (interactive)
  6227. (org-load-modules-maybe)
  6228. (org-run-like-in-org-mode 'org-insert-link))
  6229. (defun org-insert-link (&optional complete-file link-location)
  6230. "Insert a link. At the prompt, enter the link.
  6231. Completion can be used to insert any of the link protocol prefixes like
  6232. http or ftp in use.
  6233. The history can be used to select a link previously stored with
  6234. `org-store-link'. When the empty string is entered (i.e. if you just
  6235. press RET at the prompt), the link defaults to the most recently
  6236. stored link. As SPC triggers completion in the minibuffer, you need to
  6237. use M-SPC or C-q SPC to force the insertion of a space character.
  6238. You will also be prompted for a description, and if one is given, it will
  6239. be displayed in the buffer instead of the link.
  6240. If there is already a link at point, this command will allow you to edit link
  6241. and description parts.
  6242. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6243. be selected using completion. The path to the file will be relative to the
  6244. current directory if the file is in the current directory or a subdirectory.
  6245. Otherwise, the link will be the absolute path as completed in the minibuffer
  6246. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6247. option `org-link-file-path-type'.
  6248. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6249. the current directory or below.
  6250. With three \\[universal-argument] prefixes, negate the meaning of
  6251. `org-keep-stored-link-after-insertion'.
  6252. If `org-make-link-description-function' is non-nil, this function will be
  6253. called with the link target, and the result will be the default
  6254. link description.
  6255. If the LINK-LOCATION parameter is non-nil, this value will be
  6256. used as the link location instead of reading one interactively."
  6257. (interactive "P")
  6258. (let* ((wcf (current-window-configuration))
  6259. (region (if (org-region-active-p)
  6260. (buffer-substring (region-beginning) (region-end))))
  6261. (remove (and region (list (region-beginning) (region-end))))
  6262. (desc region)
  6263. tmphist ; byte-compile incorrectly complains about this
  6264. (link link-location)
  6265. entry file)
  6266. (cond
  6267. (link-location) ; specified by arg, just use it.
  6268. ((org-in-regexp org-bracket-link-regexp 1)
  6269. ;; We do have a link at point, and we are going to edit it.
  6270. (setq remove (list (match-beginning 0) (match-end 0)))
  6271. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6272. (setq link (read-string "Link: "
  6273. (org-link-unescape
  6274. (org-match-string-no-properties 1)))))
  6275. ((or (org-in-regexp org-angle-link-re)
  6276. (org-in-regexp org-plain-link-re))
  6277. ;; Convert to bracket link
  6278. (setq remove (list (match-beginning 0) (match-end 0))
  6279. link (read-string "Link: "
  6280. (org-remove-angle-brackets (match-string 0)))))
  6281. ((member complete-file '((4) (16)))
  6282. ;; Completing read for file names.
  6283. (setq file (read-file-name "File: "))
  6284. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6285. (pwd1 (file-name-as-directory (abbreviate-file-name
  6286. (expand-file-name ".")))))
  6287. (cond
  6288. ((equal complete-file '(16))
  6289. (setq link (org-make-link
  6290. "file:"
  6291. (abbreviate-file-name (expand-file-name file)))))
  6292. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6293. (setq link (org-make-link "file:" (match-string 1 file))))
  6294. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6295. (expand-file-name file))
  6296. (setq link (org-make-link
  6297. "file:" (match-string 1 (expand-file-name file)))))
  6298. (t (setq link (org-make-link "file:" file))))))
  6299. (t
  6300. ;; Read link, with completion for stored links.
  6301. (with-output-to-temp-buffer "*Org Links*"
  6302. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6303. (when org-stored-links
  6304. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6305. (princ (mapconcat
  6306. (lambda (x)
  6307. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6308. (reverse org-stored-links) "\n"))))
  6309. (let ((cw (selected-window)))
  6310. (select-window (get-buffer-window "*Org Links*"))
  6311. (setq truncate-lines t)
  6312. (org-fit-window-to-buffer)
  6313. (select-window cw))
  6314. ;; Fake a link history, containing the stored links.
  6315. (setq tmphist (append (mapcar 'car org-stored-links)
  6316. org-insert-link-history))
  6317. (unwind-protect
  6318. (setq link
  6319. (let ((org-completion-use-ido nil))
  6320. (org-completing-read
  6321. "Link: "
  6322. (append
  6323. (mapcar (lambda (x) (list (concat (car x) ":")))
  6324. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6325. (mapcar (lambda (x) (list (concat x ":")))
  6326. org-link-types))
  6327. nil nil nil
  6328. 'tmphist
  6329. (or (car (car org-stored-links))))))
  6330. (set-window-configuration wcf)
  6331. (kill-buffer "*Org Links*"))
  6332. (setq entry (assoc link org-stored-links))
  6333. (or entry (push link org-insert-link-history))
  6334. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6335. (not org-keep-stored-link-after-insertion))
  6336. (setq org-stored-links (delq (assoc link org-stored-links)
  6337. org-stored-links)))
  6338. (setq desc (or desc (nth 1 entry)))))
  6339. (if (string-match org-plain-link-re link)
  6340. ;; URL-like link, normalize the use of angular brackets.
  6341. (setq link (org-make-link (org-remove-angle-brackets link))))
  6342. ;; Check if we are linking to the current file with a search option
  6343. ;; If yes, simplify the link by using only the search option.
  6344. (when (and buffer-file-name
  6345. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6346. (let* ((path (match-string 1 link))
  6347. (case-fold-search nil)
  6348. (search (match-string 2 link)))
  6349. (save-match-data
  6350. (if (equal (file-truename buffer-file-name) (file-truename path))
  6351. ;; We are linking to this same file, with a search option
  6352. (setq link search)))))
  6353. ;; Check if we can/should use a relative path. If yes, simplify the link
  6354. (when (string-match "^file:\\(.*\\)" link)
  6355. (let* ((path (match-string 1 link))
  6356. (origpath path)
  6357. (case-fold-search nil))
  6358. (cond
  6359. ((or (eq org-link-file-path-type 'absolute)
  6360. (equal complete-file '(16)))
  6361. (setq path (abbreviate-file-name (expand-file-name path))))
  6362. ((eq org-link-file-path-type 'noabbrev)
  6363. (setq path (expand-file-name path)))
  6364. ((eq org-link-file-path-type 'relative)
  6365. (setq path (file-relative-name path)))
  6366. (t
  6367. (save-match-data
  6368. (if (string-match (concat "^" (regexp-quote
  6369. (file-name-as-directory
  6370. (expand-file-name "."))))
  6371. (expand-file-name path))
  6372. ;; We are linking a file with relative path name.
  6373. (setq path (substring (expand-file-name path)
  6374. (match-end 0)))
  6375. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6376. (setq link (concat "file:" path))
  6377. (if (equal desc origpath)
  6378. (setq desc path))))
  6379. (if org-make-link-description-function
  6380. (setq desc (funcall org-make-link-description-function link desc)))
  6381. (setq desc (read-string "Description: " desc))
  6382. (unless (string-match "\\S-" desc) (setq desc nil))
  6383. (if remove (apply 'delete-region remove))
  6384. (insert (org-make-link-string link desc))))
  6385. (defun org-completing-read (&rest args)
  6386. "Completing-read with SPACE being a normal character."
  6387. (let ((minibuffer-local-completion-map
  6388. (copy-keymap minibuffer-local-completion-map)))
  6389. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6390. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6391. (apply 'org-ido-completing-read args)))
  6392. (defun org-completing-read-no-ido (&rest args)
  6393. (let (org-completion-use-ido)
  6394. (apply 'org-completing-read args)))
  6395. (defun org-ido-completing-read (&rest args)
  6396. "Completing-read using `ido-mode' speedups if available"
  6397. (if (and org-completion-use-ido
  6398. (fboundp 'ido-completing-read)
  6399. (boundp 'ido-mode) ido-mode
  6400. (listp (second args)))
  6401. (let ((ido-enter-matching-directory nil))
  6402. (apply 'ido-completing-read (concat (car args))
  6403. (mapcar (lambda (x) (car x)) (nth 1 args))
  6404. (cddr args)))
  6405. (apply 'completing-read args)))
  6406. (defun org-extract-attributes (s)
  6407. "Extract the attributes cookie from a string and set as text property."
  6408. (let (a attr (start 0) key value)
  6409. (save-match-data
  6410. (when (string-match "{{\\([^}]+\\)}}$" s)
  6411. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6412. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6413. (setq key (match-string 1 a) value (match-string 2 a)
  6414. start (match-end 0)
  6415. attr (plist-put attr (intern key) value))))
  6416. (org-add-props s nil 'org-attr attr))
  6417. s))
  6418. (defun org-attributes-to-string (plist)
  6419. "Format a property list into an HTML attribute list."
  6420. (let ((s "") key value)
  6421. (while plist
  6422. (setq key (pop plist) value (pop plist))
  6423. (and value
  6424. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6425. s))
  6426. ;;; Opening/following a link
  6427. (defvar org-link-search-failed nil)
  6428. (defun org-next-link ()
  6429. "Move forward to the next link.
  6430. If the link is in hidden text, expose it."
  6431. (interactive)
  6432. (when (and org-link-search-failed (eq this-command last-command))
  6433. (goto-char (point-min))
  6434. (message "Link search wrapped back to beginning of buffer"))
  6435. (setq org-link-search-failed nil)
  6436. (let* ((pos (point))
  6437. (ct (org-context))
  6438. (a (assoc :link ct)))
  6439. (if a (goto-char (nth 2 a)))
  6440. (if (re-search-forward org-any-link-re nil t)
  6441. (progn
  6442. (goto-char (match-beginning 0))
  6443. (if (org-invisible-p) (org-show-context)))
  6444. (goto-char pos)
  6445. (setq org-link-search-failed t)
  6446. (error "No further link found"))))
  6447. (defun org-previous-link ()
  6448. "Move backward to the previous link.
  6449. If the link is in hidden text, expose it."
  6450. (interactive)
  6451. (when (and org-link-search-failed (eq this-command last-command))
  6452. (goto-char (point-max))
  6453. (message "Link search wrapped back to end of buffer"))
  6454. (setq org-link-search-failed nil)
  6455. (let* ((pos (point))
  6456. (ct (org-context))
  6457. (a (assoc :link ct)))
  6458. (if a (goto-char (nth 1 a)))
  6459. (if (re-search-backward org-any-link-re nil t)
  6460. (progn
  6461. (goto-char (match-beginning 0))
  6462. (if (org-invisible-p) (org-show-context)))
  6463. (goto-char pos)
  6464. (setq org-link-search-failed t)
  6465. (error "No further link found"))))
  6466. (defun org-translate-link (s)
  6467. "Translate a link string if a translation function has been defined."
  6468. (if (and org-link-translation-function
  6469. (fboundp org-link-translation-function)
  6470. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6471. (progn
  6472. (setq s (funcall org-link-translation-function
  6473. (match-string 1) (match-string 2)))
  6474. (concat (car s) ":" (cdr s)))
  6475. s))
  6476. (defun org-translate-link-from-planner (type path)
  6477. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6478. This is still an experimental function, your mileage may vary."
  6479. (cond
  6480. ((member type '("http" "https" "news" "ftp"))
  6481. ;; standard Internet links are the same.
  6482. nil)
  6483. ((and (equal type "irc") (string-match "^//" path))
  6484. ;; Planner has two / at the beginning of an irc link, we have 1.
  6485. ;; We should have zero, actually....
  6486. (setq path (substring path 1)))
  6487. ((and (equal type "lisp") (string-match "^/" path))
  6488. ;; Planner has a slash, we do not.
  6489. (setq type "elisp" path (substring path 1)))
  6490. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6491. ;; A typical message link. Planner has the id after the fina slash,
  6492. ;; we separate it with a hash mark
  6493. (setq path (concat (match-string 1 path) "#"
  6494. (org-remove-angle-brackets (match-string 2 path)))))
  6495. )
  6496. (cons type path))
  6497. (defun org-find-file-at-mouse (ev)
  6498. "Open file link or URL at mouse."
  6499. (interactive "e")
  6500. (mouse-set-point ev)
  6501. (org-open-at-point 'in-emacs))
  6502. (defun org-open-at-mouse (ev)
  6503. "Open file link or URL at mouse."
  6504. (interactive "e")
  6505. (mouse-set-point ev)
  6506. (if (eq major-mode 'org-agenda-mode)
  6507. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6508. (org-open-at-point))
  6509. (defvar org-window-config-before-follow-link nil
  6510. "The window configuration before following a link.
  6511. This is saved in case the need arises to restore it.")
  6512. (defvar org-open-link-marker (make-marker)
  6513. "Marker pointing to the location where `org-open-at-point; was called.")
  6514. ;;;###autoload
  6515. (defun org-open-at-point-global ()
  6516. "Follow a link like Org-mode does.
  6517. This command can be called in any mode to follow a link that has
  6518. Org-mode syntax."
  6519. (interactive)
  6520. (org-run-like-in-org-mode 'org-open-at-point))
  6521. ;;;###autoload
  6522. (defun org-open-link-from-string (s &optional arg)
  6523. "Open a link in the string S, as if it was in Org-mode."
  6524. (interactive "sLink: \nP")
  6525. (with-temp-buffer
  6526. (let ((org-inhibit-startup t))
  6527. (org-mode)
  6528. (insert s)
  6529. (goto-char (point-min))
  6530. (org-open-at-point arg))))
  6531. (defun org-open-at-point (&optional in-emacs)
  6532. "Open link at or after point.
  6533. If there is no link at point, this function will search forward up to
  6534. the end of the current subtree.
  6535. Normally, files will be opened by an appropriate application. If the
  6536. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6537. With a double prefix argument, try to open outside of Emacs, in the
  6538. application the system uses for this file type."
  6539. (interactive "P")
  6540. (org-load-modules-maybe)
  6541. (move-marker org-open-link-marker (point))
  6542. (setq org-window-config-before-follow-link (current-window-configuration))
  6543. (org-remove-occur-highlights nil nil t)
  6544. (cond
  6545. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6546. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6547. (org-footnote-action))
  6548. (t
  6549. (let (type path link line search (pos (point)))
  6550. (catch 'match
  6551. (save-excursion
  6552. (skip-chars-forward "^]\n\r")
  6553. (when (org-in-regexp org-bracket-link-regexp)
  6554. (setq link (org-extract-attributes
  6555. (org-link-unescape (org-match-string-no-properties 1))))
  6556. (while (string-match " *\n *" link)
  6557. (setq link (replace-match " " t t link)))
  6558. (setq link (org-link-expand-abbrev link))
  6559. (cond
  6560. ((or (file-name-absolute-p link)
  6561. (string-match "^\\.\\.?/" link))
  6562. (setq type "file" path link))
  6563. ((string-match org-link-re-with-space3 link)
  6564. (setq type (match-string 1 link) path (match-string 2 link)))
  6565. (t (setq type "thisfile" path link)))
  6566. (throw 'match t)))
  6567. (when (get-text-property (point) 'org-linked-text)
  6568. (setq type "thisfile"
  6569. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6570. (1+ (point)) (point))
  6571. path (buffer-substring
  6572. (previous-single-property-change pos 'org-linked-text)
  6573. (next-single-property-change pos 'org-linked-text)))
  6574. (throw 'match t))
  6575. (save-excursion
  6576. (when (or (org-in-regexp org-angle-link-re)
  6577. (org-in-regexp org-plain-link-re))
  6578. (setq type (match-string 1) path (match-string 2))
  6579. (throw 'match t)))
  6580. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6581. (setq type "tree-match"
  6582. path (match-string 1))
  6583. (throw 'match t))
  6584. (save-excursion
  6585. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6586. (setq type "tags"
  6587. path (match-string 1))
  6588. (while (string-match ":" path)
  6589. (setq path (replace-match "+" t t path)))
  6590. (throw 'match t))))
  6591. (unless path
  6592. (error "No link found"))
  6593. ;; Remove any trailing spaces in path
  6594. (if (string-match " +\\'" path)
  6595. (setq path (replace-match "" t t path)))
  6596. (if (and org-link-translation-function
  6597. (fboundp org-link-translation-function))
  6598. ;; Check if we need to translate the link
  6599. (let ((tmp (funcall org-link-translation-function type path)))
  6600. (setq type (car tmp) path (cdr tmp))))
  6601. (cond
  6602. ((assoc type org-link-protocols)
  6603. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6604. ((equal type "mailto")
  6605. (let ((cmd (car org-link-mailto-program))
  6606. (args (cdr org-link-mailto-program)) args1
  6607. (address path) (subject "") a)
  6608. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6609. (setq address (match-string 1 path)
  6610. subject (org-link-escape (match-string 2 path))))
  6611. (while args
  6612. (cond
  6613. ((not (stringp (car args))) (push (pop args) args1))
  6614. (t (setq a (pop args))
  6615. (if (string-match "%a" a)
  6616. (setq a (replace-match address t t a)))
  6617. (if (string-match "%s" a)
  6618. (setq a (replace-match subject t t a)))
  6619. (push a args1))))
  6620. (apply cmd (nreverse args1))))
  6621. ((member type '("http" "https" "ftp" "news"))
  6622. (browse-url (concat type ":" (org-link-escape
  6623. path org-link-escape-chars-browser))))
  6624. ((member type '("message"))
  6625. (browse-url (concat type ":" path)))
  6626. ((string= type "tags")
  6627. (org-tags-view in-emacs path))
  6628. ((string= type "thisfile")
  6629. (if in-emacs
  6630. (switch-to-buffer-other-window
  6631. (org-get-buffer-for-internal-link (current-buffer)))
  6632. (org-mark-ring-push))
  6633. (let ((cmd `(org-link-search
  6634. ,path
  6635. ,(cond ((equal in-emacs '(4)) 'occur)
  6636. ((equal in-emacs '(16)) 'org-occur)
  6637. (t nil))
  6638. ,pos)))
  6639. (condition-case nil (eval cmd)
  6640. (error (progn (widen) (eval cmd))))))
  6641. ((string= type "tree-match")
  6642. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6643. ((string= type "file")
  6644. (if (string-match "::\\([0-9]+\\)\\'" path)
  6645. (setq line (string-to-number (match-string 1 path))
  6646. path (substring path 0 (match-beginning 0)))
  6647. (if (string-match "::\\(.+\\)\\'" path)
  6648. (setq search (match-string 1 path)
  6649. path (substring path 0 (match-beginning 0)))))
  6650. (if (string-match "[*?{]" (file-name-nondirectory path))
  6651. (dired path)
  6652. (org-open-file path in-emacs line search)))
  6653. ((string= type "news")
  6654. (require 'org-gnus)
  6655. (org-gnus-follow-link path))
  6656. ((string= type "shell")
  6657. (let ((cmd path))
  6658. (if (or (not org-confirm-shell-link-function)
  6659. (funcall org-confirm-shell-link-function
  6660. (format "Execute \"%s\" in shell? "
  6661. (org-add-props cmd nil
  6662. 'face 'org-warning))))
  6663. (progn
  6664. (message "Executing %s" cmd)
  6665. (shell-command cmd))
  6666. (error "Abort"))))
  6667. ((string= type "elisp")
  6668. (let ((cmd path))
  6669. (if (or (not org-confirm-elisp-link-function)
  6670. (funcall org-confirm-elisp-link-function
  6671. (format "Execute \"%s\" as elisp? "
  6672. (org-add-props cmd nil
  6673. 'face 'org-warning))))
  6674. (message "%s => %s" cmd
  6675. (if (equal (string-to-char cmd) ?\()
  6676. (eval (read cmd))
  6677. (call-interactively (read cmd))))
  6678. (error "Abort"))))
  6679. (t
  6680. (browse-url-at-point))))))
  6681. (move-marker org-open-link-marker nil)
  6682. (run-hook-with-args 'org-follow-link-hook))
  6683. ;;;; Time estimates
  6684. (defun org-get-effort (&optional pom)
  6685. "Get the effort estimate for the current entry."
  6686. (org-entry-get pom org-effort-property))
  6687. ;;; File search
  6688. (defvar org-create-file-search-functions nil
  6689. "List of functions to construct the right search string for a file link.
  6690. These functions are called in turn with point at the location to
  6691. which the link should point.
  6692. A function in the hook should first test if it would like to
  6693. handle this file type, for example by checking the major-mode or
  6694. the file extension. If it decides not to handle this file, it
  6695. should just return nil to give other functions a chance. If it
  6696. does handle the file, it must return the search string to be used
  6697. when following the link. The search string will be part of the
  6698. file link, given after a double colon, and `org-open-at-point'
  6699. will automatically search for it. If special measures must be
  6700. taken to make the search successful, another function should be
  6701. added to the companion hook `org-execute-file-search-functions',
  6702. which see.
  6703. A function in this hook may also use `setq' to set the variable
  6704. `description' to provide a suggestion for the descriptive text to
  6705. be used for this link when it gets inserted into an Org-mode
  6706. buffer with \\[org-insert-link].")
  6707. (defvar org-execute-file-search-functions nil
  6708. "List of functions to execute a file search triggered by a link.
  6709. Functions added to this hook must accept a single argument, the
  6710. search string that was part of the file link, the part after the
  6711. double colon. The function must first check if it would like to
  6712. handle this search, for example by checking the major-mode or the
  6713. file extension. If it decides not to handle this search, it
  6714. should just return nil to give other functions a chance. If it
  6715. does handle the search, it must return a non-nil value to keep
  6716. other functions from trying.
  6717. Each function can access the current prefix argument through the
  6718. variable `current-prefix-argument'. Note that a single prefix is
  6719. used to force opening a link in Emacs, so it may be good to only
  6720. use a numeric or double prefix to guide the search function.
  6721. In case this is needed, a function in this hook can also restore
  6722. the window configuration before `org-open-at-point' was called using:
  6723. (set-window-configuration org-window-config-before-follow-link)")
  6724. (defun org-link-search (s &optional type avoid-pos)
  6725. "Search for a link search option.
  6726. If S is surrounded by forward slashes, it is interpreted as a
  6727. regular expression. In org-mode files, this will create an `org-occur'
  6728. sparse tree. In ordinary files, `occur' will be used to list matches.
  6729. If the current buffer is in `dired-mode', grep will be used to search
  6730. in all files. If AVOID-POS is given, ignore matches near that position."
  6731. (let ((case-fold-search t)
  6732. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6733. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6734. (append '(("") (" ") ("\t") ("\n"))
  6735. org-emphasis-alist)
  6736. "\\|") "\\)"))
  6737. (pos (point))
  6738. (pre nil) (post nil)
  6739. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6740. (cond
  6741. ;; First check if there are any special
  6742. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6743. ;; Now try the builtin stuff
  6744. ((save-excursion
  6745. (goto-char (point-min))
  6746. (and
  6747. (re-search-forward
  6748. (concat "<<" (regexp-quote s0) ">>") nil t)
  6749. (setq type 'dedicated
  6750. pos (match-beginning 0))))
  6751. ;; There is an exact target for this
  6752. (goto-char pos))
  6753. ((and (string-match "^(\\(.*\\))$" s0)
  6754. (save-excursion
  6755. (goto-char (point-min))
  6756. (and
  6757. (re-search-forward
  6758. (concat "[^[]" (regexp-quote
  6759. (format org-coderef-label-format
  6760. (match-string 1 s0))))
  6761. nil t)
  6762. (setq type 'dedicated
  6763. pos (1+ (match-beginning 0))))))
  6764. ;; There is a coderef target for this
  6765. (goto-char pos))
  6766. ((string-match "^/\\(.*\\)/$" s)
  6767. ;; A regular expression
  6768. (cond
  6769. ((org-mode-p)
  6770. (org-occur (match-string 1 s)))
  6771. ;;((eq major-mode 'dired-mode)
  6772. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6773. (t (org-do-occur (match-string 1 s)))))
  6774. (t
  6775. ;; A normal search strings
  6776. (when (equal (string-to-char s) ?*)
  6777. ;; Anchor on headlines, post may include tags.
  6778. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6779. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6780. s (substring s 1)))
  6781. (remove-text-properties
  6782. 0 (length s)
  6783. '(face nil mouse-face nil keymap nil fontified nil) s)
  6784. ;; Make a series of regular expressions to find a match
  6785. (setq words (org-split-string s "[ \n\r\t]+")
  6786. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6787. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6788. "\\)" markers)
  6789. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6790. re2a (concat "[ \t\r\n]" re2a_)
  6791. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6792. re4 (concat "[^a-zA-Z_]" re4_)
  6793. re1 (concat pre re2 post)
  6794. re3 (concat pre (if pre re4_ re4) post)
  6795. re5 (concat pre ".*" re4)
  6796. re2 (concat pre re2)
  6797. re2a (concat pre (if pre re2a_ re2a))
  6798. re4 (concat pre (if pre re4_ re4))
  6799. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6800. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6801. re5 "\\)"
  6802. ))
  6803. (cond
  6804. ((eq type 'org-occur) (org-occur reall))
  6805. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6806. (t (goto-char (point-min))
  6807. (setq type 'fuzzy)
  6808. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6809. (org-search-not-self 1 re1 nil t)
  6810. (org-search-not-self 1 re2 nil t)
  6811. (org-search-not-self 1 re2a nil t)
  6812. (org-search-not-self 1 re3 nil t)
  6813. (org-search-not-self 1 re4 nil t)
  6814. (org-search-not-self 1 re5 nil t)
  6815. )
  6816. (goto-char (match-beginning 1))
  6817. (goto-char pos)
  6818. (error "No match")))))
  6819. (t
  6820. ;; Normal string-search
  6821. (goto-char (point-min))
  6822. (if (search-forward s nil t)
  6823. (goto-char (match-beginning 0))
  6824. (error "No match"))))
  6825. (and (org-mode-p) (org-show-context 'link-search))
  6826. type))
  6827. (defun org-search-not-self (group &rest args)
  6828. "Execute `re-search-forward', but only accept matches that do not
  6829. enclose the position of `org-open-link-marker'."
  6830. (let ((m org-open-link-marker))
  6831. (catch 'exit
  6832. (while (apply 're-search-forward args)
  6833. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6834. (goto-char (match-end group))
  6835. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6836. (> (match-beginning 0) (marker-position m))
  6837. (< (match-end 0) (marker-position m)))
  6838. (save-match-data
  6839. (or (not (org-in-regexp
  6840. org-bracket-link-analytic-regexp 1))
  6841. (not (match-end 4)) ; no description
  6842. (and (<= (match-beginning 4) (point))
  6843. (>= (match-end 4) (point))))))
  6844. (throw 'exit (point))))))))
  6845. (defun org-get-buffer-for-internal-link (buffer)
  6846. "Return a buffer to be used for displaying the link target of internal links."
  6847. (cond
  6848. ((not org-display-internal-link-with-indirect-buffer)
  6849. buffer)
  6850. ((string-match "(Clone)$" (buffer-name buffer))
  6851. (message "Buffer is already a clone, not making another one")
  6852. ;; we also do not modify visibility in this case
  6853. buffer)
  6854. (t ; make a new indirect buffer for displaying the link
  6855. (let* ((bn (buffer-name buffer))
  6856. (ibn (concat bn "(Clone)"))
  6857. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6858. (with-current-buffer ib (org-overview))
  6859. ib))))
  6860. (defun org-do-occur (regexp &optional cleanup)
  6861. "Call the Emacs command `occur'.
  6862. If CLEANUP is non-nil, remove the printout of the regular expression
  6863. in the *Occur* buffer. This is useful if the regex is long and not useful
  6864. to read."
  6865. (occur regexp)
  6866. (when cleanup
  6867. (let ((cwin (selected-window)) win beg end)
  6868. (when (setq win (get-buffer-window "*Occur*"))
  6869. (select-window win))
  6870. (goto-char (point-min))
  6871. (when (re-search-forward "match[a-z]+" nil t)
  6872. (setq beg (match-end 0))
  6873. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6874. (setq end (1- (match-beginning 0)))))
  6875. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6876. (goto-char (point-min))
  6877. (select-window cwin))))
  6878. ;;; The mark ring for links jumps
  6879. (defvar org-mark-ring nil
  6880. "Mark ring for positions before jumps in Org-mode.")
  6881. (defvar org-mark-ring-last-goto nil
  6882. "Last position in the mark ring used to go back.")
  6883. ;; Fill and close the ring
  6884. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6885. (loop for i from 1 to org-mark-ring-length do
  6886. (push (make-marker) org-mark-ring))
  6887. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6888. org-mark-ring)
  6889. (defun org-mark-ring-push (&optional pos buffer)
  6890. "Put the current position or POS into the mark ring and rotate it."
  6891. (interactive)
  6892. (setq pos (or pos (point)))
  6893. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6894. (move-marker (car org-mark-ring)
  6895. (or pos (point))
  6896. (or buffer (current-buffer)))
  6897. (message "%s"
  6898. (substitute-command-keys
  6899. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6900. (defun org-mark-ring-goto (&optional n)
  6901. "Jump to the previous position in the mark ring.
  6902. With prefix arg N, jump back that many stored positions. When
  6903. called several times in succession, walk through the entire ring.
  6904. Org-mode commands jumping to a different position in the current file,
  6905. or to another Org-mode file, automatically push the old position
  6906. onto the ring."
  6907. (interactive "p")
  6908. (let (p m)
  6909. (if (eq last-command this-command)
  6910. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6911. (setq p org-mark-ring))
  6912. (setq org-mark-ring-last-goto p)
  6913. (setq m (car p))
  6914. (switch-to-buffer (marker-buffer m))
  6915. (goto-char m)
  6916. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6917. (defun org-remove-angle-brackets (s)
  6918. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6919. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6920. s)
  6921. (defun org-add-angle-brackets (s)
  6922. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6923. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6924. s)
  6925. (defun org-remove-double-quotes (s)
  6926. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6927. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6928. s)
  6929. ;;; Following specific links
  6930. (defun org-follow-timestamp-link ()
  6931. (cond
  6932. ((org-at-date-range-p t)
  6933. (let ((org-agenda-start-on-weekday)
  6934. (t1 (match-string 1))
  6935. (t2 (match-string 2)))
  6936. (setq t1 (time-to-days (org-time-string-to-time t1))
  6937. t2 (time-to-days (org-time-string-to-time t2)))
  6938. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6939. ((org-at-timestamp-p t)
  6940. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6941. (substring (match-string 1) 0 10)))
  6942. 1))
  6943. (t (error "This should not happen"))))
  6944. ;;; Following file links
  6945. (defvar org-wait nil)
  6946. (defun org-open-file (path &optional in-emacs line search)
  6947. "Open the file at PATH.
  6948. First, this expands any special file name abbreviations. Then the
  6949. configuration variable `org-file-apps' is checked if it contains an
  6950. entry for this file type, and if yes, the corresponding command is launched.
  6951. If no application is found, Emacs simply visits the file.
  6952. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6953. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6954. and o use an external application to visit the file.
  6955. Optional LINE specifies a line to go to, optional SEARCH a string to
  6956. search for. If LINE or SEARCH is given, the file will always be
  6957. opened in Emacs.
  6958. If the file does not exist, an error is thrown."
  6959. (setq in-emacs (or in-emacs line search))
  6960. (let* ((file (if (equal path "")
  6961. buffer-file-name
  6962. (substitute-in-file-name (expand-file-name path))))
  6963. (apps (append org-file-apps (org-default-apps)))
  6964. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6965. (dirp (if remp nil (file-directory-p file)))
  6966. (file (if (and dirp org-open-directory-means-index-dot-org)
  6967. (concat (file-name-as-directory file) "index.org")
  6968. file))
  6969. (a-m-a-p (assq 'auto-mode apps))
  6970. (dfile (downcase file))
  6971. (old-buffer (current-buffer))
  6972. (old-pos (point))
  6973. (old-mode major-mode)
  6974. ext cmd)
  6975. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6976. (setq ext (match-string 1 dfile))
  6977. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6978. (setq ext (match-string 1 dfile))))
  6979. (cond
  6980. ((equal in-emacs '(16))
  6981. (setq cmd (cdr (assoc 'system apps))))
  6982. (in-emacs (setq cmd 'emacs))
  6983. (t
  6984. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6985. (and dirp (cdr (assoc 'directory apps)))
  6986. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6987. 'string-match)
  6988. (cdr (assoc ext apps))
  6989. (cdr (assoc t apps))))))
  6990. (when (eq cmd 'system)
  6991. (setq cmd (cdr (assoc 'system apps))))
  6992. (when (eq cmd 'default)
  6993. (setq cmd (cdr (assoc t apps))))
  6994. (when (eq cmd 'mailcap)
  6995. (require 'mailcap)
  6996. (mailcap-parse-mailcaps)
  6997. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6998. (command (mailcap-mime-info mime-type)))
  6999. (if (stringp command)
  7000. (setq cmd command)
  7001. (setq cmd 'emacs))))
  7002. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7003. (not (file-exists-p file))
  7004. (not org-open-non-existing-files))
  7005. (error "No such file: %s" file))
  7006. (cond
  7007. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7008. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7009. (while (string-match "['\"]%s['\"]" cmd)
  7010. (setq cmd (replace-match "%s" t t cmd)))
  7011. (while (string-match "%s" cmd)
  7012. (setq cmd (replace-match
  7013. (save-match-data
  7014. (shell-quote-argument
  7015. (convert-standard-filename file)))
  7016. t t cmd)))
  7017. (save-window-excursion
  7018. (start-process-shell-command cmd nil cmd)
  7019. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7020. ))
  7021. ((or (stringp cmd)
  7022. (eq cmd 'emacs))
  7023. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7024. (widen)
  7025. (if line (goto-line line)
  7026. (if search (org-link-search search))))
  7027. ((consp cmd)
  7028. (let ((file (convert-standard-filename file)))
  7029. (eval cmd)))
  7030. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7031. (and (org-mode-p) (eq old-mode 'org-mode)
  7032. (or (not (equal old-buffer (current-buffer)))
  7033. (not (equal old-pos (point))))
  7034. (org-mark-ring-push old-pos old-buffer))))
  7035. (defun org-default-apps ()
  7036. "Return the default applications for this operating system."
  7037. (cond
  7038. ((eq system-type 'darwin)
  7039. org-file-apps-defaults-macosx)
  7040. ((eq system-type 'windows-nt)
  7041. org-file-apps-defaults-windowsnt)
  7042. (t org-file-apps-defaults-gnu)))
  7043. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7044. "Convert extensions to regular expressions in the cars of LIST.
  7045. Also, weed out any non-string entries, because the return value is used
  7046. only for regexp matching.
  7047. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7048. point to the symbol `emacs', indicating that the file should
  7049. be opened in Emacs."
  7050. (append
  7051. (delq nil
  7052. (mapcar (lambda (x)
  7053. (if (not (stringp (car x)))
  7054. nil
  7055. (if (string-match "\\W" (car x))
  7056. x
  7057. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7058. list))
  7059. (if add-auto-mode
  7060. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7061. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7062. (defun org-file-remote-p (file)
  7063. "Test whether FILE specifies a location on a remote system.
  7064. Return non-nil if the location is indeed remote.
  7065. For example, the filename \"/user@host:/foo\" specifies a location
  7066. on the system \"/user@host:\"."
  7067. (cond ((fboundp 'file-remote-p)
  7068. (file-remote-p file))
  7069. ((fboundp 'tramp-handle-file-remote-p)
  7070. (tramp-handle-file-remote-p file))
  7071. ((and (boundp 'ange-ftp-name-format)
  7072. (string-match (car ange-ftp-name-format) file))
  7073. t)
  7074. (t nil)))
  7075. ;;;; Refiling
  7076. (defun org-get-org-file ()
  7077. "Read a filename, with default directory `org-directory'."
  7078. (let ((default (or org-default-notes-file remember-data-file)))
  7079. (read-file-name (format "File name [%s]: " default)
  7080. (file-name-as-directory org-directory)
  7081. default)))
  7082. (defun org-notes-order-reversed-p ()
  7083. "Check if the current file should receive notes in reversed order."
  7084. (cond
  7085. ((not org-reverse-note-order) nil)
  7086. ((eq t org-reverse-note-order) t)
  7087. ((not (listp org-reverse-note-order)) nil)
  7088. (t (catch 'exit
  7089. (let ((all org-reverse-note-order)
  7090. entry)
  7091. (while (setq entry (pop all))
  7092. (if (string-match (car entry) buffer-file-name)
  7093. (throw 'exit (cdr entry))))
  7094. nil)))))
  7095. (defvar org-refile-target-table nil
  7096. "The list of refile targets, created by `org-refile'.")
  7097. (defvar org-agenda-new-buffers nil
  7098. "Buffers created to visit agenda files.")
  7099. (defun org-get-refile-targets (&optional default-buffer)
  7100. "Produce a table with refile targets."
  7101. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7102. targets txt re files f desc descre fast-path-p level)
  7103. (message "Getting targets...")
  7104. (with-current-buffer (or default-buffer (current-buffer))
  7105. (while (setq entry (pop entries))
  7106. (setq files (car entry) desc (cdr entry))
  7107. (setq fast-path-p nil)
  7108. (cond
  7109. ((null files) (setq files (list (current-buffer))))
  7110. ((eq files 'org-agenda-files)
  7111. (setq files (org-agenda-files 'unrestricted)))
  7112. ((and (symbolp files) (fboundp files))
  7113. (setq files (funcall files)))
  7114. ((and (symbolp files) (boundp files))
  7115. (setq files (symbol-value files))))
  7116. (if (stringp files) (setq files (list files)))
  7117. (cond
  7118. ((eq (car desc) :tag)
  7119. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7120. ((eq (car desc) :todo)
  7121. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7122. ((eq (car desc) :regexp)
  7123. (setq descre (cdr desc)))
  7124. ((eq (car desc) :level)
  7125. (setq descre (concat "^\\*\\{" (number-to-string
  7126. (if org-odd-levels-only
  7127. (1- (* 2 (cdr desc)))
  7128. (cdr desc)))
  7129. "\\}[ \t]")))
  7130. ((eq (car desc) :maxlevel)
  7131. (setq fast-path-p t)
  7132. (setq descre (concat "^\\*\\{1," (number-to-string
  7133. (if org-odd-levels-only
  7134. (1- (* 2 (cdr desc)))
  7135. (cdr desc)))
  7136. "\\}[ \t]")))
  7137. (t (error "Bad refiling target description %s" desc)))
  7138. (while (setq f (pop files))
  7139. (save-excursion
  7140. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7141. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7142. (setq f (expand-file-name f))
  7143. (save-excursion
  7144. (save-restriction
  7145. (widen)
  7146. (goto-char (point-min))
  7147. (while (re-search-forward descre nil t)
  7148. (goto-char (point-at-bol))
  7149. (when (looking-at org-complex-heading-regexp)
  7150. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7151. txt (org-link-display-format (match-string 4))
  7152. re (concat "^" (regexp-quote
  7153. (buffer-substring (match-beginning 1)
  7154. (match-end 4)))))
  7155. (if (match-end 5) (setq re (concat re "[ \t]+"
  7156. (regexp-quote
  7157. (match-string 5)))))
  7158. (setq re (concat re "[ \t]*$"))
  7159. (when org-refile-use-outline-path
  7160. (setq txt (mapconcat 'org-protect-slash
  7161. (append
  7162. (if (eq org-refile-use-outline-path 'file)
  7163. (list (file-name-nondirectory
  7164. (buffer-file-name (buffer-base-buffer))))
  7165. (if (eq org-refile-use-outline-path 'full-file-path)
  7166. (list (buffer-file-name (buffer-base-buffer)))))
  7167. (org-get-outline-path fast-path-p level txt)
  7168. (list txt))
  7169. "/")))
  7170. (push (list txt f re (point)) targets))
  7171. (goto-char (point-at-eol))))))))
  7172. (message "Getting targets...done")
  7173. (nreverse targets))))
  7174. (defun org-protect-slash (s)
  7175. (while (string-match "/" s)
  7176. (setq s (replace-match "\\" t t s)))
  7177. s)
  7178. (defvar org-olpa (make-vector 20 nil))
  7179. (defun org-get-outline-path (&optional fastp level heading)
  7180. "Return the outline path to the current entry, as a list."
  7181. (if fastp
  7182. (progn
  7183. (if (> level 19)
  7184. (error "Outline path failure, more than 19 levels."))
  7185. (loop for i from level upto 19 do
  7186. (aset org-olpa i nil))
  7187. (prog1
  7188. (delq nil (append org-olpa nil))
  7189. (aset org-olpa level heading)))
  7190. (let (rtn)
  7191. (save-excursion
  7192. (while (org-up-heading-safe)
  7193. (when (looking-at org-complex-heading-regexp)
  7194. (push (org-match-string-no-properties 4) rtn)))
  7195. rtn))))
  7196. (defvar org-refile-history nil
  7197. "History for refiling operations.")
  7198. (defun org-refile (&optional goto default-buffer)
  7199. "Move the entry at point to another heading.
  7200. The list of target headings is compiled using the information in
  7201. `org-refile-targets', which see. This list is created before each use
  7202. and will therefore always be up-to-date.
  7203. At the target location, the entry is filed as a subitem of the target heading.
  7204. Depending on `org-reverse-note-order', the new subitem will either be the
  7205. first or the last subitem.
  7206. If there is an active region, all entries in that region will be moved.
  7207. However, the region must fulfil the requirement that the first heading
  7208. is the first one sets the top-level of the moved text - at most siblings
  7209. below it are allowed.
  7210. With prefix arg GOTO, the command will only visit the target location,
  7211. not actually move anything.
  7212. With a double prefix `C-u C-u', go to the location where the last refiling
  7213. operation has put the subtree."
  7214. (interactive "P")
  7215. (let* ((cbuf (current-buffer))
  7216. (regionp (org-region-active-p))
  7217. (region-start (and regionp (region-beginning)))
  7218. (region-end (and regionp (region-end)))
  7219. (region-length (and regionp (- region-end region-start)))
  7220. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7221. pos it nbuf file re level reversed)
  7222. (when regionp (goto-char region-start)
  7223. (unless (org-kill-is-subtree-p
  7224. (buffer-substring region-start region-end))
  7225. (error "The region is not a (sequence of) subtree(s)")))
  7226. (if (equal goto '(16))
  7227. (org-refile-goto-last-stored)
  7228. (when (setq it (org-refile-get-location
  7229. (if goto "Goto: " "Refile to: ") default-buffer))
  7230. (setq file (nth 1 it)
  7231. re (nth 2 it)
  7232. pos (nth 3 it))
  7233. (if (and (equal (buffer-file-name) file)
  7234. (if regionp
  7235. (and (>= pos region-start)
  7236. (<= pos region-end))
  7237. (and (>= pos (point))
  7238. (< pos (save-excursion
  7239. (org-end-of-subtree t t))))))
  7240. (error "Cannot refile to position inside the tree or region"))
  7241. (setq nbuf (or (find-buffer-visiting file)
  7242. (find-file-noselect file)))
  7243. (if goto
  7244. (progn
  7245. (switch-to-buffer nbuf)
  7246. (goto-char pos)
  7247. (org-show-context 'org-goto))
  7248. (if regionp
  7249. (progn
  7250. (kill-new (buffer-substring region-start region-end))
  7251. (org-save-markers-in-region region-start region-end))
  7252. (org-copy-subtree 1 nil t))
  7253. (save-excursion
  7254. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7255. (find-file-noselect file))))
  7256. (setq reversed (org-notes-order-reversed-p))
  7257. (save-excursion
  7258. (save-restriction
  7259. (widen)
  7260. (goto-char pos)
  7261. (looking-at outline-regexp)
  7262. (setq level (org-get-valid-level (funcall outline-level) 1))
  7263. (goto-char
  7264. (if reversed
  7265. (or (outline-next-heading) (point-max))
  7266. (or (save-excursion (outline-get-next-sibling))
  7267. (org-end-of-subtree t t)
  7268. (point-max))))
  7269. (if (not (bolp)) (newline))
  7270. (bookmark-set "org-refile-last-stored")
  7271. (org-paste-subtree level))))
  7272. (if regionp
  7273. (delete-region (point) (+ (point) region-length))
  7274. (org-cut-subtree))
  7275. (setq org-markers-to-move nil)
  7276. (message "Refiled to \"%s\"" (car it)))))))
  7277. (defun org-refile-goto-last-stored ()
  7278. "Go to the location where the last refile was stored."
  7279. (interactive)
  7280. (bookmark-jump "org-refile-last-stored")
  7281. (message "This is the location of the last refile"))
  7282. (defun org-refile-get-location (&optional prompt default-buffer)
  7283. "Prompt the user for a refile location, using PROMPT."
  7284. (let ((org-refile-targets org-refile-targets)
  7285. (org-refile-use-outline-path org-refile-use-outline-path))
  7286. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7287. (unless org-refile-target-table
  7288. (error "No refile targets"))
  7289. (let* ((cbuf (current-buffer))
  7290. (partial-completion-mode nil)
  7291. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7292. (cfunc (if (and org-refile-use-outline-path
  7293. org-outline-path-complete-in-steps)
  7294. 'org-olpath-completing-read
  7295. 'org-ido-completing-read))
  7296. (extra (if org-refile-use-outline-path "/" ""))
  7297. (filename (and cfn (expand-file-name cfn)))
  7298. (tbl (mapcar
  7299. (lambda (x)
  7300. (if (not (equal filename (nth 1 x)))
  7301. (cons (concat (car x) extra " ("
  7302. (file-name-nondirectory (nth 1 x)) ")")
  7303. (cdr x))
  7304. (cons (concat (car x) extra) (cdr x))))
  7305. org-refile-target-table))
  7306. (completion-ignore-case t))
  7307. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7308. tbl)))
  7309. (defun org-olpath-completing-read (prompt collection &rest args)
  7310. "Read an outline path like a file name."
  7311. (let ((thetable collection)
  7312. (org-completion-use-ido nil)) ; does not work with ido.
  7313. (apply
  7314. 'org-ido-completing-read prompt
  7315. (lambda (string predicate &optional flag)
  7316. (let (rtn r f (l (length string)))
  7317. (cond
  7318. ((eq flag nil)
  7319. ;; try completion
  7320. (try-completion string thetable))
  7321. ((eq flag t)
  7322. ;; all-completions
  7323. (setq rtn (all-completions string thetable predicate))
  7324. (mapcar
  7325. (lambda (x)
  7326. (setq r (substring x l))
  7327. (if (string-match " ([^)]*)$" x)
  7328. (setq f (match-string 0 x))
  7329. (setq f ""))
  7330. (if (string-match "/" r)
  7331. (concat string (substring r 0 (match-end 0)) f)
  7332. x))
  7333. rtn))
  7334. ((eq flag 'lambda)
  7335. ;; exact match?
  7336. (assoc string thetable)))
  7337. ))
  7338. args)))
  7339. ;;;; Dynamic blocks
  7340. (defun org-find-dblock (name)
  7341. "Find the first dynamic block with name NAME in the buffer.
  7342. If not found, stay at current position and return nil."
  7343. (let (pos)
  7344. (save-excursion
  7345. (goto-char (point-min))
  7346. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7347. nil t)
  7348. (match-beginning 0))))
  7349. (if pos (goto-char pos))
  7350. pos))
  7351. (defconst org-dblock-start-re
  7352. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7353. "Matches the startline of a dynamic block, with parameters.")
  7354. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7355. "Matches the end of a dynamic block.")
  7356. (defun org-create-dblock (plist)
  7357. "Create a dynamic block section, with parameters taken from PLIST.
  7358. PLIST must contain a :name entry which is used as name of the block."
  7359. (unless (bolp) (newline))
  7360. (let ((name (plist-get plist :name)))
  7361. (insert "#+BEGIN: " name)
  7362. (while plist
  7363. (if (eq (car plist) :name)
  7364. (setq plist (cddr plist))
  7365. (insert " " (prin1-to-string (pop plist)))))
  7366. (insert "\n\n#+END:\n")
  7367. (beginning-of-line -2)))
  7368. (defun org-prepare-dblock ()
  7369. "Prepare dynamic block for refresh.
  7370. This empties the block, puts the cursor at the insert position and returns
  7371. the property list including an extra property :name with the block name."
  7372. (unless (looking-at org-dblock-start-re)
  7373. (error "Not at a dynamic block"))
  7374. (let* ((begdel (1+ (match-end 0)))
  7375. (name (org-no-properties (match-string 1)))
  7376. (params (append (list :name name)
  7377. (read (concat "(" (match-string 3) ")")))))
  7378. (unless (re-search-forward org-dblock-end-re nil t)
  7379. (error "Dynamic block not terminated"))
  7380. (setq params
  7381. (append params
  7382. (list :content (buffer-substring
  7383. begdel (match-beginning 0)))))
  7384. (delete-region begdel (match-beginning 0))
  7385. (goto-char begdel)
  7386. (open-line 1)
  7387. params))
  7388. (defun org-map-dblocks (&optional command)
  7389. "Apply COMMAND to all dynamic blocks in the current buffer.
  7390. If COMMAND is not given, use `org-update-dblock'."
  7391. (let ((cmd (or command 'org-update-dblock))
  7392. pos)
  7393. (save-excursion
  7394. (goto-char (point-min))
  7395. (while (re-search-forward org-dblock-start-re nil t)
  7396. (goto-char (setq pos (match-beginning 0)))
  7397. (condition-case nil
  7398. (funcall cmd)
  7399. (error (message "Error during update of dynamic block")))
  7400. (goto-char pos)
  7401. (unless (re-search-forward org-dblock-end-re nil t)
  7402. (error "Dynamic block not terminated"))))))
  7403. (defun org-dblock-update (&optional arg)
  7404. "User command for updating dynamic blocks.
  7405. Update the dynamic block at point. With prefix ARG, update all dynamic
  7406. blocks in the buffer."
  7407. (interactive "P")
  7408. (if arg
  7409. (org-update-all-dblocks)
  7410. (or (looking-at org-dblock-start-re)
  7411. (org-beginning-of-dblock))
  7412. (org-update-dblock)))
  7413. (defun org-update-dblock ()
  7414. "Update the dynamic block at point
  7415. This means to empty the block, parse for parameters and then call
  7416. the correct writing function."
  7417. (save-window-excursion
  7418. (let* ((pos (point))
  7419. (line (org-current-line))
  7420. (params (org-prepare-dblock))
  7421. (name (plist-get params :name))
  7422. (cmd (intern (concat "org-dblock-write:" name))))
  7423. (message "Updating dynamic block `%s' at line %d..." name line)
  7424. (funcall cmd params)
  7425. (message "Updating dynamic block `%s' at line %d...done" name line)
  7426. (goto-char pos))))
  7427. (defun org-beginning-of-dblock ()
  7428. "Find the beginning of the dynamic block at point.
  7429. Error if there is no such block at point."
  7430. (let ((pos (point))
  7431. beg)
  7432. (end-of-line 1)
  7433. (if (and (re-search-backward org-dblock-start-re nil t)
  7434. (setq beg (match-beginning 0))
  7435. (re-search-forward org-dblock-end-re nil t)
  7436. (> (match-end 0) pos))
  7437. (goto-char beg)
  7438. (goto-char pos)
  7439. (error "Not in a dynamic block"))))
  7440. (defun org-update-all-dblocks ()
  7441. "Update all dynamic blocks in the buffer.
  7442. This function can be used in a hook."
  7443. (when (org-mode-p)
  7444. (org-map-dblocks 'org-update-dblock)))
  7445. ;;;; Completion
  7446. (defconst org-additional-option-like-keywords
  7447. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7448. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7449. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7450. "BEGIN_QUOTE" "END_QUOTE"
  7451. "BEGIN_VERSE" "END_VERSE"
  7452. "BEGIN_SRC" "END_SRC"
  7453. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7454. (defcustom org-structure-template-alist
  7455. '(
  7456. ("s" "#+begin_src ?\n\n#+end_src"
  7457. "<src lang=\"?\">\n\n</src>")
  7458. ("e" "#+begin_example\n?\n#+end_example"
  7459. "<example>\n?\n</example>")
  7460. ("q" "#+begin_quote\n?\n#+end_quote"
  7461. "<quote>\n?\n</quote>")
  7462. ("v" "#+begin_verse\n?\n#+end_verse"
  7463. "<verse>\n?\n/verse>")
  7464. ("l" "#+begin_latex\n?\n#+end_latex"
  7465. "<literal style=\"latex\">\n?\n</literal>")
  7466. ("L" "#+latex: "
  7467. "<literal style=\"latex\">?</literal>")
  7468. ("h" "#+begin_html\n?\n#+end_html"
  7469. "<literal style=\"html\">\n?\n</literal>")
  7470. ("H" "#+html: "
  7471. "<literal style=\"html\">?</literal>")
  7472. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7473. ("A" "#+ascii: ")
  7474. ("i" "#+include %file ?"
  7475. "<include file=%file markup=\"?\">")
  7476. )
  7477. "Structure completion elements.
  7478. This is a list of abbreviation keys and values. The value gets inserted
  7479. it you type @samp{.} followed by the key and then the completion key,
  7480. usually `M-TAB'. %file will be replaced by a file name after prompting
  7481. for the file using completion.
  7482. There are two templates for each key, the first uses the original Org syntax,
  7483. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7484. the default when the /org-mtags.el/ module has been loaded. See also the
  7485. variable `org-mtags-prefer-muse-templates'.
  7486. This is an experimental feature, it is undecided if it is going to stay in."
  7487. :group 'org-completion
  7488. :type '(repeat
  7489. (string :tag "Key")
  7490. (string :tag "Template")
  7491. (string :tag "Muse Template")))
  7492. (defun org-try-structure-completion ()
  7493. "Try to complete a structure template before point.
  7494. This looks for strings like \"<e\" on an otherwise empty line and
  7495. expands them."
  7496. (let ((l (buffer-substring (point-at-bol) (point)))
  7497. a)
  7498. (when (and (looking-at "[ \t]*$")
  7499. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7500. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7501. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7502. (match-beginning 1)) a)
  7503. t)))
  7504. (defun org-complete-expand-structure-template (start cell)
  7505. "Expand a structure template."
  7506. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7507. (rpl (nth (if musep 2 1) cell)))
  7508. (delete-region start (point))
  7509. (when (string-match "\\`#\\+" rpl)
  7510. (cond
  7511. ((bolp))
  7512. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7513. (delete-region (point-at-bol) (point)))
  7514. (t (newline))))
  7515. (setq start (point))
  7516. (if (string-match "%file" rpl)
  7517. (setq rpl (replace-match
  7518. (concat
  7519. "\""
  7520. (save-match-data
  7521. (abbreviate-file-name (read-file-name "Include file: ")))
  7522. "\"")
  7523. t t rpl)))
  7524. (insert rpl)
  7525. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7526. (defun org-complete (&optional arg)
  7527. "Perform completion on word at point.
  7528. At the beginning of a headline, this completes TODO keywords as given in
  7529. `org-todo-keywords'.
  7530. If the current word is preceded by a backslash, completes the TeX symbols
  7531. that are supported for HTML support.
  7532. If the current word is preceded by \"#+\", completes special words for
  7533. setting file options.
  7534. In the line after \"#+STARTUP:, complete valid keywords.\"
  7535. At all other locations, this simply calls the value of
  7536. `org-completion-fallback-command'."
  7537. (interactive "P")
  7538. (org-without-partial-completion
  7539. (catch 'exit
  7540. (let* ((a nil)
  7541. (end (point))
  7542. (beg1 (save-excursion
  7543. (skip-chars-backward (org-re "[:alnum:]_@"))
  7544. (point)))
  7545. (beg (save-excursion
  7546. (skip-chars-backward "a-zA-Z0-9_:$")
  7547. (point)))
  7548. (confirm (lambda (x) (stringp (car x))))
  7549. (searchhead (equal (char-before beg) ?*))
  7550. (struct
  7551. (when (and (member (char-before beg1) '(?. ?<))
  7552. (setq a (assoc (buffer-substring beg1 (point))
  7553. org-structure-template-alist)))
  7554. (org-complete-expand-structure-template (1- beg1) a)
  7555. (throw 'exit t)))
  7556. (tag (and (equal (char-before beg1) ?:)
  7557. (equal (char-after (point-at-bol)) ?*)))
  7558. (prop (and (equal (char-before beg1) ?:)
  7559. (not (equal (char-after (point-at-bol)) ?*))))
  7560. (texp (equal (char-before beg) ?\\))
  7561. (link (equal (char-before beg) ?\[))
  7562. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7563. beg)
  7564. "#+"))
  7565. (startup (string-match "^#\\+STARTUP:.*"
  7566. (buffer-substring (point-at-bol) (point))))
  7567. (completion-ignore-case opt)
  7568. (type nil)
  7569. (tbl nil)
  7570. (table (cond
  7571. (opt
  7572. (setq type :opt)
  7573. (require 'org-exp)
  7574. (append
  7575. (mapcar
  7576. (lambda (x)
  7577. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7578. (cons (match-string 2 x) (match-string 1 x)))
  7579. (org-split-string (org-get-current-options) "\n"))
  7580. (mapcar 'list org-additional-option-like-keywords)))
  7581. (startup
  7582. (setq type :startup)
  7583. org-startup-options)
  7584. (link (append org-link-abbrev-alist-local
  7585. org-link-abbrev-alist))
  7586. (texp
  7587. (setq type :tex)
  7588. org-html-entities)
  7589. ((string-match "\\`\\*+[ \t]+\\'"
  7590. (buffer-substring (point-at-bol) beg))
  7591. (setq type :todo)
  7592. (mapcar 'list org-todo-keywords-1))
  7593. (searchhead
  7594. (setq type :searchhead)
  7595. (save-excursion
  7596. (goto-char (point-min))
  7597. (while (re-search-forward org-todo-line-regexp nil t)
  7598. (push (list
  7599. (org-make-org-heading-search-string
  7600. (match-string 3) t))
  7601. tbl)))
  7602. tbl)
  7603. (tag (setq type :tag beg beg1)
  7604. (or org-tag-alist (org-get-buffer-tags)))
  7605. (prop (setq type :prop beg beg1)
  7606. (mapcar 'list (org-buffer-property-keys nil t t)))
  7607. (t (progn
  7608. (call-interactively org-completion-fallback-command)
  7609. (throw 'exit nil)))))
  7610. (pattern (buffer-substring-no-properties beg end))
  7611. (completion (try-completion pattern table confirm)))
  7612. (cond ((eq completion t)
  7613. (if (not (assoc (upcase pattern) table))
  7614. (message "Already complete")
  7615. (if (and (equal type :opt)
  7616. (not (member (car (assoc (upcase pattern) table))
  7617. org-additional-option-like-keywords)))
  7618. (insert (substring (cdr (assoc (upcase pattern) table))
  7619. (length pattern)))
  7620. (if (memq type '(:tag :prop)) (insert ":")))))
  7621. ((null completion)
  7622. (message "Can't find completion for \"%s\"" pattern)
  7623. (ding))
  7624. ((not (string= pattern completion))
  7625. (delete-region beg end)
  7626. (if (string-match " +$" completion)
  7627. (setq completion (replace-match "" t t completion)))
  7628. (insert completion)
  7629. (if (get-buffer-window "*Completions*")
  7630. (delete-window (get-buffer-window "*Completions*")))
  7631. (if (assoc completion table)
  7632. (if (eq type :todo) (insert " ")
  7633. (if (memq type '(:tag :prop)) (insert ":"))))
  7634. (if (and (equal type :opt) (assoc completion table))
  7635. (message "%s" (substitute-command-keys
  7636. "Press \\[org-complete] again to insert example settings"))))
  7637. (t
  7638. (message "Making completion list...")
  7639. (let ((list (sort (all-completions pattern table confirm)
  7640. 'string<)))
  7641. (with-output-to-temp-buffer "*Completions*"
  7642. (condition-case nil
  7643. ;; Protection needed for XEmacs and emacs 21
  7644. (display-completion-list list pattern)
  7645. (error (display-completion-list list)))))
  7646. (message "Making completion list...%s" "done")))))))
  7647. ;;;; TODO, DEADLINE, Comments
  7648. (defun org-toggle-comment ()
  7649. "Change the COMMENT state of an entry."
  7650. (interactive)
  7651. (save-excursion
  7652. (org-back-to-heading)
  7653. (let (case-fold-search)
  7654. (if (looking-at (concat outline-regexp
  7655. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7656. (replace-match "" t t nil 1)
  7657. (if (looking-at outline-regexp)
  7658. (progn
  7659. (goto-char (match-end 0))
  7660. (insert org-comment-string " ")))))))
  7661. (defvar org-last-todo-state-is-todo nil
  7662. "This is non-nil when the last TODO state change led to a TODO state.
  7663. If the last change removed the TODO tag or switched to DONE, then
  7664. this is nil.")
  7665. (defvar org-setting-tags nil) ; dynamically skipped
  7666. (defun org-parse-local-options (string var)
  7667. "Parse STRING for startup setting relevant for variable VAR."
  7668. (let ((rtn (symbol-value var))
  7669. e opts)
  7670. (save-match-data
  7671. (if (or (not string) (not (string-match "\\S-" string)))
  7672. rtn
  7673. (setq opts (delq nil (mapcar (lambda (x)
  7674. (setq e (assoc x org-startup-options))
  7675. (if (eq (nth 1 e) var) e nil))
  7676. (org-split-string string "[ \t]+"))))
  7677. (if (not opts)
  7678. rtn
  7679. (setq rtn nil)
  7680. (while (setq e (pop opts))
  7681. (if (not (nth 3 e))
  7682. (setq rtn (nth 2 e))
  7683. (if (not (listp rtn)) (setq rtn nil))
  7684. (push (nth 2 e) rtn)))
  7685. rtn)))))
  7686. (defvar org-todo-setup-filter-hook nil
  7687. "Hook for functions that pre-filter todo specs.
  7688. Each function takes a todo spec and returns either `nil' or the spec
  7689. transformed into canonical form." )
  7690. (defvar org-todo-get-default-hook nil
  7691. "Hook for functions that get a default item for todo.
  7692. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7693. `nil' or a string to be used for the todo mark." )
  7694. (defvar org-agenda-headline-snapshot-before-repeat)
  7695. (defun org-todo (&optional arg)
  7696. "Change the TODO state of an item.
  7697. The state of an item is given by a keyword at the start of the heading,
  7698. like
  7699. *** TODO Write paper
  7700. *** DONE Call mom
  7701. The different keywords are specified in the variable `org-todo-keywords'.
  7702. By default the available states are \"TODO\" and \"DONE\".
  7703. So for this example: when the item starts with TODO, it is changed to DONE.
  7704. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7705. DONE are present, add TODO at the beginning of the heading.
  7706. With C-u prefix arg, use completion to determine the new state.
  7707. With numeric prefix arg, switch to that state.
  7708. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7709. With a tripple C-u prefix, circumvent any state blocking.
  7710. For calling through lisp, arg is also interpreted in the following way:
  7711. 'none -> empty state
  7712. \"\"(empty string) -> switch to empty state
  7713. 'done -> switch to DONE
  7714. 'nextset -> switch to the next set of keywords
  7715. 'previousset -> switch to the previous set of keywords
  7716. \"WAITING\" -> switch to the specified keyword, but only if it
  7717. really is a member of `org-todo-keywords'."
  7718. (interactive "P")
  7719. (if (equal arg '(16)) (setq arg 'nextset))
  7720. (let ((org-blocker-hook org-blocker-hook))
  7721. (when (equal arg '(64))
  7722. (setq arg nil org-blocker-hook nil))
  7723. (save-excursion
  7724. (catch 'exit
  7725. (org-back-to-heading)
  7726. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7727. (or (looking-at (concat " +" org-todo-regexp " *"))
  7728. (looking-at " *"))
  7729. (let* ((match-data (match-data))
  7730. (startpos (point-at-bol))
  7731. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7732. (org-log-done org-log-done)
  7733. (org-log-repeat org-log-repeat)
  7734. (org-todo-log-states org-todo-log-states)
  7735. (this (match-string 1))
  7736. (hl-pos (match-beginning 0))
  7737. (head (org-get-todo-sequence-head this))
  7738. (ass (assoc head org-todo-kwd-alist))
  7739. (interpret (nth 1 ass))
  7740. (done-word (nth 3 ass))
  7741. (final-done-word (nth 4 ass))
  7742. (last-state (or this ""))
  7743. (completion-ignore-case t)
  7744. (member (member this org-todo-keywords-1))
  7745. (tail (cdr member))
  7746. (state (cond
  7747. ((and org-todo-key-trigger
  7748. (or (and (equal arg '(4))
  7749. (eq org-use-fast-todo-selection 'prefix))
  7750. (and (not arg) org-use-fast-todo-selection
  7751. (not (eq org-use-fast-todo-selection
  7752. 'prefix)))))
  7753. ;; Use fast selection
  7754. (org-fast-todo-selection))
  7755. ((and (equal arg '(4))
  7756. (or (not org-use-fast-todo-selection)
  7757. (not org-todo-key-trigger)))
  7758. ;; Read a state with completion
  7759. (org-ido-completing-read
  7760. "State: " (mapcar (lambda(x) (list x))
  7761. org-todo-keywords-1)
  7762. nil t))
  7763. ((eq arg 'right)
  7764. (if this
  7765. (if tail (car tail) nil)
  7766. (car org-todo-keywords-1)))
  7767. ((eq arg 'left)
  7768. (if (equal member org-todo-keywords-1)
  7769. nil
  7770. (if this
  7771. (nth (- (length org-todo-keywords-1)
  7772. (length tail) 2)
  7773. org-todo-keywords-1)
  7774. (org-last org-todo-keywords-1))))
  7775. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7776. (setq arg nil))) ; hack to fall back to cycling
  7777. (arg
  7778. ;; user or caller requests a specific state
  7779. (cond
  7780. ((equal arg "") nil)
  7781. ((eq arg 'none) nil)
  7782. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7783. ((eq arg 'nextset)
  7784. (or (car (cdr (member head org-todo-heads)))
  7785. (car org-todo-heads)))
  7786. ((eq arg 'previousset)
  7787. (let ((org-todo-heads (reverse org-todo-heads)))
  7788. (or (car (cdr (member head org-todo-heads)))
  7789. (car org-todo-heads))))
  7790. ((car (member arg org-todo-keywords-1)))
  7791. ((nth (1- (prefix-numeric-value arg))
  7792. org-todo-keywords-1))))
  7793. ((null member) (or head (car org-todo-keywords-1)))
  7794. ((equal this final-done-word) nil) ;; -> make empty
  7795. ((null tail) nil) ;; -> first entry
  7796. ((memq interpret '(type priority))
  7797. (if (eq this-command last-command)
  7798. (car tail)
  7799. (if (> (length tail) 0)
  7800. (or done-word (car org-done-keywords))
  7801. nil)))
  7802. (t
  7803. (car tail))))
  7804. (state (or
  7805. (run-hook-with-args-until-success
  7806. 'org-todo-get-default-hook state last-state)
  7807. state))
  7808. (next (if state (concat " " state " ") " "))
  7809. (change-plist (list :type 'todo-state-change :from this :to state
  7810. :position startpos))
  7811. dolog now-done-p)
  7812. (when org-blocker-hook
  7813. (setq org-last-todo-state-is-todo
  7814. (not (member this org-done-keywords)))
  7815. (unless (save-excursion
  7816. (save-match-data
  7817. (run-hook-with-args-until-failure
  7818. 'org-blocker-hook change-plist)))
  7819. (if (interactive-p)
  7820. (error "TODO state change from %s to %s blocked" this state)
  7821. ;; fail silently
  7822. (message "TODO state change from %s to %s blocked" this state)
  7823. (throw 'exit nil))))
  7824. (store-match-data match-data)
  7825. (replace-match next t t)
  7826. (unless (pos-visible-in-window-p hl-pos)
  7827. (message "TODO state changed to %s" (org-trim next)))
  7828. (unless head
  7829. (setq head (org-get-todo-sequence-head state)
  7830. ass (assoc head org-todo-kwd-alist)
  7831. interpret (nth 1 ass)
  7832. done-word (nth 3 ass)
  7833. final-done-word (nth 4 ass)))
  7834. (when (memq arg '(nextset previousset))
  7835. (message "Keyword-Set %d/%d: %s"
  7836. (- (length org-todo-sets) -1
  7837. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7838. (length org-todo-sets)
  7839. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7840. (setq org-last-todo-state-is-todo
  7841. (not (member state org-done-keywords)))
  7842. (setq now-done-p (and (member state org-done-keywords)
  7843. (not (member this org-done-keywords))))
  7844. (and logging (org-local-logging logging))
  7845. (when (and (or org-todo-log-states org-log-done)
  7846. (not (memq arg '(nextset previousset))))
  7847. ;; we need to look at recording a time and note
  7848. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7849. (nth 2 (assoc this org-todo-log-states))))
  7850. (when (and state
  7851. (member state org-not-done-keywords)
  7852. (not (member this org-not-done-keywords)))
  7853. ;; This is now a todo state and was not one before
  7854. ;; If there was a CLOSED time stamp, get rid of it.
  7855. (org-add-planning-info nil nil 'closed))
  7856. (when (and now-done-p org-log-done)
  7857. ;; It is now done, and it was not done before
  7858. (org-add-planning-info 'closed (org-current-time))
  7859. (if (and (not dolog) (eq 'note org-log-done))
  7860. (org-add-log-setup 'done state this 'findpos 'note)))
  7861. (when (and state dolog)
  7862. ;; This is a non-nil state, and we need to log it
  7863. (org-add-log-setup 'state state this 'findpos dolog)))
  7864. ;; Fixup tag positioning
  7865. (org-todo-trigger-tag-changes state)
  7866. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7867. (when org-provide-todo-statistics
  7868. (org-update-parent-todo-statistics))
  7869. (run-hooks 'org-after-todo-state-change-hook)
  7870. (if (and arg (not (member state org-done-keywords)))
  7871. (setq head (org-get-todo-sequence-head state)))
  7872. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7873. ;; Do we need to trigger a repeat?
  7874. (when now-done-p
  7875. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7876. ;; This is for the agenda, take a snapshot of the headline.
  7877. (save-match-data
  7878. (setq org-agenda-headline-snapshot-before-repeat
  7879. (org-get-heading))))
  7880. (org-auto-repeat-maybe state))
  7881. ;; Fixup cursor location if close to the keyword
  7882. (if (and (outline-on-heading-p)
  7883. (not (bolp))
  7884. (save-excursion (beginning-of-line 1)
  7885. (looking-at org-todo-line-regexp))
  7886. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7887. (progn
  7888. (goto-char (or (match-end 2) (match-end 1)))
  7889. (just-one-space)))
  7890. (when org-trigger-hook
  7891. (save-excursion
  7892. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7893. (defun org-block-todo-from-children-or-siblings (change-plist)
  7894. "Block turning an entry into a TODO, using the hierarchy.
  7895. This checks whether the current task should be blocked from state
  7896. changes. Such blocking occurs when:
  7897. 1. The task has children which are not all in a completed state.
  7898. 2. A task has a parent with the property :ORDERED:, and there
  7899. are siblings prior to the current task with incomplete
  7900. status."
  7901. (catch 'dont-block
  7902. ;; If this is not a todo state change, or if this entry is already DONE,
  7903. ;; do not block
  7904. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7905. (member (plist-get change-plist :from)
  7906. (cons 'done org-done-keywords))
  7907. (member (plist-get change-plist :to)
  7908. (cons 'todo org-not-done-keywords)))
  7909. (throw 'dont-block t))
  7910. ;; If this task has children, and any are undone, it's blocked
  7911. (save-excursion
  7912. (org-back-to-heading t)
  7913. (let ((this-level (funcall outline-level)))
  7914. (outline-next-heading)
  7915. (let ((child-level (funcall outline-level)))
  7916. (while (and (not (eobp))
  7917. (> child-level this-level))
  7918. ;; this todo has children, check whether they are all
  7919. ;; completed
  7920. (if (and (not (org-entry-is-done-p))
  7921. (org-entry-is-todo-p))
  7922. (throw 'dont-block nil))
  7923. (outline-next-heading)
  7924. (setq child-level (funcall outline-level))))))
  7925. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7926. ;; any previous siblings are undone, it's blocked
  7927. (save-excursion
  7928. (org-back-to-heading t)
  7929. (when (save-excursion
  7930. (ignore-errors
  7931. (org-up-heading-all 1)
  7932. (org-entry-get (point) "ORDERED")))
  7933. (let* ((this-level (funcall outline-level))
  7934. (current-level this-level))
  7935. (while (and (not (bobp))
  7936. (= current-level this-level))
  7937. (outline-previous-heading)
  7938. (setq current-level (funcall outline-level))
  7939. (if (= current-level this-level)
  7940. ;; this todo has children, check whether they are all
  7941. ;; completed
  7942. (if (and (not (org-entry-is-done-p))
  7943. (org-entry-is-todo-p))
  7944. (throw 'dont-block nil)))))))
  7945. t)) ; don't block
  7946. (defcustom org-track-ordered-property-with-tag nil
  7947. "Should the ORDERED property also be shown as a tag?
  7948. The ORDERED property decides if an entry should require subtasks to be
  7949. completed in sequence. Since a property is not very visible, setting
  7950. this option means that toggling the ORDERED property with the command
  7951. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  7952. not relevant for the behavior, but it makes things more visible.
  7953. Note that toggling the tag with tags commands will not change the property
  7954. and therefore not influence behavior!
  7955. This can be t, meaning the tag ORDERED should be used, It can also be a
  7956. string to select a different tag for this task."
  7957. :group 'org-todo
  7958. :type '(choice
  7959. (const :tag "No tracking" nil)
  7960. (const :tag "Track with ORDERED tag" t)
  7961. (string :tag "Use other tag")))
  7962. (defun org-toggle-ordered-property ()
  7963. "Toggle the ORDERED property of the current entry.
  7964. For better visibility, you can track the value of this property with a tag.
  7965. See variable `org-track-ordered-property-with-tag'."
  7966. (interactive)
  7967. (let* ((t1 org-track-ordered-property-with-tag)
  7968. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  7969. (save-excursion
  7970. (org-back-to-heading)
  7971. (if (org-entry-get nil "ORDERED")
  7972. (progn
  7973. (org-delete-property "ORDERED")
  7974. (and tag (org-toggle-tag tag 'off))
  7975. (message "Subtasks can be completed in arbitrary order"))
  7976. (org-entry-put nil "ORDERED" "t")
  7977. (and tag (org-toggle-tag tag 'on))
  7978. (message "Subtasks must be completed in sequence")))))
  7979. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  7980. (defun org-block-todo-from-checkboxes (change-plist)
  7981. "Block turning an entry into a TODO, using checkboxes.
  7982. This checks whether the current task should be blocked from state
  7983. changes because there are uncheckd boxes in this entry."
  7984. (catch 'dont-block
  7985. ;; If this is not a todo state change, or if this entry is already DONE,
  7986. ;; do not block
  7987. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7988. (member (plist-get change-plist :from)
  7989. (cons 'done org-done-keywords))
  7990. (member (plist-get change-plist :to)
  7991. (cons 'todo org-not-done-keywords)))
  7992. (throw 'dont-block t))
  7993. ;; If this task has checkboxes that are not checked, it's blocked
  7994. (save-excursion
  7995. (org-back-to-heading t)
  7996. (let ((beg (point)) end)
  7997. (outline-next-heading)
  7998. (setq end (point))
  7999. (goto-char beg)
  8000. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8001. end t)
  8002. (progn
  8003. (if (boundp 'org-blocked-by-checkboxes)
  8004. (setq org-blocked-by-checkboxes t))
  8005. (throw 'dont-block nil)))))
  8006. t)) ; do not block
  8007. (defun org-update-parent-todo-statistics ()
  8008. "Update any statistics cookie in the parent of the current headline."
  8009. (interactive)
  8010. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8011. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8012. (catch 'exit
  8013. (save-excursion
  8014. (setq level (org-up-heading-safe))
  8015. (unless level
  8016. (throw 'exit nil))
  8017. (while (re-search-forward box-re (point-at-eol) t)
  8018. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8019. (setq is-percent (match-end 2))
  8020. (save-match-data
  8021. (unless (outline-next-heading) (throw 'exit nil))
  8022. (while (looking-at org-todo-line-regexp)
  8023. (setq kwd (match-string 2))
  8024. (and kwd (setq cnt-all (1+ cnt-all)))
  8025. (and (member kwd org-done-keywords)
  8026. (setq cnt-done (1+ cnt-done)))
  8027. (condition-case nil
  8028. (org-forward-same-level 1)
  8029. (error (end-of-line 1)))))
  8030. (replace-match
  8031. (if is-percent
  8032. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8033. (format "[%d/%d]" cnt-done cnt-all))))
  8034. (when cookie-present
  8035. (run-hook-with-args 'org-after-todo-statistics-hook
  8036. cnt-done (- cnt-all cnt-done)))))))
  8037. (defvar org-after-todo-statistics-hook nil
  8038. "Hook that is called after a TODO statistics cookie has been updated.
  8039. Each function is called with two arguments: the number of not-done entries
  8040. and the number of done entries.
  8041. For example, the following function, when added to this hook, will switch
  8042. an entry to DONE when all children are done, and back to TODO when new
  8043. entries are set to a TODO status. Note that this hook is only called
  8044. when there is a statistics cookie in the headline!
  8045. (defun org-summary-todo (n-done n-not-done)
  8046. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8047. (let (org-log-done org-log-states) ; turn off logging
  8048. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8049. ")
  8050. (defun org-todo-trigger-tag-changes (state)
  8051. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8052. (let ((l org-todo-state-tags-triggers)
  8053. changes)
  8054. (when (or (not state) (equal state ""))
  8055. (setq changes (append changes (cdr (assoc "" l)))))
  8056. (when (and (stringp state) (> (length state) 0))
  8057. (setq changes (append changes (cdr (assoc state l)))))
  8058. (when (member state org-not-done-keywords)
  8059. (setq changes (append changes (cdr (assoc 'todo l)))))
  8060. (when (member state org-done-keywords)
  8061. (setq changes (append changes (cdr (assoc 'done l)))))
  8062. (dolist (c changes)
  8063. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8064. (defun org-local-logging (value)
  8065. "Get logging settings from a property VALUE."
  8066. (let* (words w a)
  8067. ;; directly set the variables, they are already local.
  8068. (setq org-log-done nil
  8069. org-log-repeat nil
  8070. org-todo-log-states nil)
  8071. (setq words (org-split-string value))
  8072. (while (setq w (pop words))
  8073. (cond
  8074. ((setq a (assoc w org-startup-options))
  8075. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8076. (set (nth 1 a) (nth 2 a))))
  8077. ((setq a (org-extract-log-state-settings w))
  8078. (and (member (car a) org-todo-keywords-1)
  8079. (push a org-todo-log-states)))))))
  8080. (defun org-get-todo-sequence-head (kwd)
  8081. "Return the head of the TODO sequence to which KWD belongs.
  8082. If KWD is not set, check if there is a text property remembering the
  8083. right sequence."
  8084. (let (p)
  8085. (cond
  8086. ((not kwd)
  8087. (or (get-text-property (point-at-bol) 'org-todo-head)
  8088. (progn
  8089. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8090. nil (point-at-eol)))
  8091. (get-text-property p 'org-todo-head))))
  8092. ((not (member kwd org-todo-keywords-1))
  8093. (car org-todo-keywords-1))
  8094. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8095. (defun org-fast-todo-selection ()
  8096. "Fast TODO keyword selection with single keys.
  8097. Returns the new TODO keyword, or nil if no state change should occur."
  8098. (let* ((fulltable org-todo-key-alist)
  8099. (done-keywords org-done-keywords) ;; needed for the faces.
  8100. (maxlen (apply 'max (mapcar
  8101. (lambda (x)
  8102. (if (stringp (car x)) (string-width (car x)) 0))
  8103. fulltable)))
  8104. (expert nil)
  8105. (fwidth (+ maxlen 3 1 3))
  8106. (ncol (/ (- (window-width) 4) fwidth))
  8107. tg cnt e c tbl
  8108. groups ingroup)
  8109. (save-excursion
  8110. (save-window-excursion
  8111. (if expert
  8112. (set-buffer (get-buffer-create " *Org todo*"))
  8113. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8114. (erase-buffer)
  8115. (org-set-local 'org-done-keywords done-keywords)
  8116. (setq tbl fulltable cnt 0)
  8117. (while (setq e (pop tbl))
  8118. (cond
  8119. ((equal e '(:startgroup))
  8120. (push '() groups) (setq ingroup t)
  8121. (when (not (= cnt 0))
  8122. (setq cnt 0)
  8123. (insert "\n"))
  8124. (insert "{ "))
  8125. ((equal e '(:endgroup))
  8126. (setq ingroup nil cnt 0)
  8127. (insert "}\n"))
  8128. ((equal e '(:newline))
  8129. (insert "\n "))
  8130. (t
  8131. (setq tg (car e) c (cdr e))
  8132. (if ingroup (push tg (car groups)))
  8133. (setq tg (org-add-props tg nil 'face
  8134. (org-get-todo-face tg)))
  8135. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8136. (insert "[" c "] " tg (make-string
  8137. (- fwidth 4 (length tg)) ?\ ))
  8138. (when (= (setq cnt (1+ cnt)) ncol)
  8139. (insert "\n")
  8140. (if ingroup (insert " "))
  8141. (setq cnt 0)))))
  8142. (insert "\n")
  8143. (goto-char (point-min))
  8144. (if (not expert) (org-fit-window-to-buffer))
  8145. (message "[a-z..]:Set [SPC]:clear")
  8146. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8147. (cond
  8148. ((or (= c ?\C-g)
  8149. (and (= c ?q) (not (rassoc c fulltable))))
  8150. (setq quit-flag t))
  8151. ((= c ?\ ) nil)
  8152. ((setq e (rassoc c fulltable) tg (car e))
  8153. tg)
  8154. (t (setq quit-flag t)))))))
  8155. (defun org-entry-is-todo-p ()
  8156. (member (org-get-todo-state) org-not-done-keywords))
  8157. (defun org-entry-is-done-p ()
  8158. (member (org-get-todo-state) org-done-keywords))
  8159. (defun org-get-todo-state ()
  8160. (save-excursion
  8161. (org-back-to-heading t)
  8162. (and (looking-at org-todo-line-regexp)
  8163. (match-end 2)
  8164. (match-string 2))))
  8165. (defun org-at-date-range-p (&optional inactive-ok)
  8166. "Is the cursor inside a date range?"
  8167. (interactive)
  8168. (save-excursion
  8169. (catch 'exit
  8170. (let ((pos (point)))
  8171. (skip-chars-backward "^[<\r\n")
  8172. (skip-chars-backward "<[")
  8173. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8174. (>= (match-end 0) pos)
  8175. (throw 'exit t))
  8176. (skip-chars-backward "^<[\r\n")
  8177. (skip-chars-backward "<[")
  8178. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8179. (>= (match-end 0) pos)
  8180. (throw 'exit t)))
  8181. nil)))
  8182. (defun org-get-repeat ()
  8183. "Check if there is a deadline/schedule with repeater in this entry."
  8184. (save-match-data
  8185. (save-excursion
  8186. (org-back-to-heading t)
  8187. (if (re-search-forward
  8188. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8189. (match-string 1)))))
  8190. (defvar org-last-changed-timestamp)
  8191. (defvar org-last-inserted-timestamp)
  8192. (defvar org-log-post-message)
  8193. (defvar org-log-note-purpose)
  8194. (defvar org-log-note-how)
  8195. (defvar org-log-note-extra)
  8196. (defun org-auto-repeat-maybe (done-word)
  8197. "Check if the current headline contains a repeated deadline/schedule.
  8198. If yes, set TODO state back to what it was and change the base date
  8199. of repeating deadline/scheduled time stamps to new date.
  8200. This function is run automatically after each state change to a DONE state."
  8201. ;; last-state is dynamically scoped into this function
  8202. (let* ((repeat (org-get-repeat))
  8203. (aa (assoc last-state org-todo-kwd-alist))
  8204. (interpret (nth 1 aa))
  8205. (head (nth 2 aa))
  8206. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8207. (msg "Entry repeats: ")
  8208. (org-log-done nil)
  8209. (org-todo-log-states nil)
  8210. (nshiftmax 10) (nshift 0)
  8211. re type n what ts time)
  8212. (when repeat
  8213. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8214. (org-todo (if (eq interpret 'type) last-state head))
  8215. (when org-log-repeat
  8216. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8217. (memq 'org-add-log-note post-command-hook))
  8218. ;; OK, we are already setup for some record
  8219. (if (eq org-log-repeat 'note)
  8220. ;; make sure we take a note, not only a time stamp
  8221. (setq org-log-note-how 'note))
  8222. ;; Set up for taking a record
  8223. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8224. last-state
  8225. 'findpos org-log-repeat)))
  8226. (org-back-to-heading t)
  8227. (org-add-planning-info nil nil 'closed)
  8228. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8229. org-deadline-time-regexp "\\)\\|\\("
  8230. org-ts-regexp "\\)"))
  8231. (while (re-search-forward
  8232. re (save-excursion (outline-next-heading) (point)) t)
  8233. (setq type (if (match-end 1) org-scheduled-string
  8234. (if (match-end 3) org-deadline-string "Plain:"))
  8235. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8236. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8237. (setq n (string-to-number (match-string 2 ts))
  8238. what (match-string 3 ts))
  8239. (if (equal what "w") (setq n (* n 7) what "d"))
  8240. ;; Preparation, see if we need to modify the start date for the change
  8241. (when (match-end 1)
  8242. (setq time (save-match-data (org-time-string-to-time ts)))
  8243. (cond
  8244. ((equal (match-string 1 ts) ".")
  8245. ;; Shift starting date to today
  8246. (org-timestamp-change
  8247. (- (time-to-days (current-time)) (time-to-days time))
  8248. 'day))
  8249. ((equal (match-string 1 ts) "+")
  8250. (while (or (= nshift 0)
  8251. (<= (time-to-days time) (time-to-days (current-time))))
  8252. (when (= (incf nshift) nshiftmax)
  8253. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8254. (error "Abort")))
  8255. (org-timestamp-change n (cdr (assoc what whata)))
  8256. (org-at-timestamp-p t)
  8257. (setq ts (match-string 1))
  8258. (setq time (save-match-data (org-time-string-to-time ts))))
  8259. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8260. ;; rematch, so that we have everything in place for the real shift
  8261. (org-at-timestamp-p t)
  8262. (setq ts (match-string 1))
  8263. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8264. (org-timestamp-change n (cdr (assoc what whata)))
  8265. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8266. (setq org-log-post-message msg)
  8267. (message "%s" msg))))
  8268. (defun org-show-todo-tree (arg)
  8269. "Make a compact tree which shows all headlines marked with TODO.
  8270. The tree will show the lines where the regexp matches, and all higher
  8271. headlines above the match.
  8272. With a \\[universal-argument] prefix, also show the DONE entries.
  8273. With a numeric prefix N, construct a sparse tree for the Nth element
  8274. of `org-todo-keywords-1'."
  8275. (interactive "P")
  8276. (let ((case-fold-search nil)
  8277. (kwd-re
  8278. (cond ((null arg) org-not-done-regexp)
  8279. ((equal arg '(4))
  8280. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8281. (mapcar 'list org-todo-keywords-1))))
  8282. (concat "\\("
  8283. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8284. "\\)\\>")))
  8285. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8286. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8287. org-todo-keywords-1)))
  8288. (t (error "Invalid prefix argument: %s" arg)))))
  8289. (message "%d TODO entries found"
  8290. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8291. (defun org-deadline (&optional remove time)
  8292. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8293. With argument REMOVE, remove any deadline from the item.
  8294. When TIME is set, it should be an internal time specification, and the
  8295. scheduling will use the corresponding date."
  8296. (interactive "P")
  8297. (if remove
  8298. (progn
  8299. (org-remove-timestamp-with-keyword org-deadline-string)
  8300. (message "Item no longer has a deadline."))
  8301. (if (org-get-repeat)
  8302. (error "Cannot change deadline on task with repeater, please do that by hand")
  8303. (org-add-planning-info 'deadline time 'closed)
  8304. (message "Deadline on %s" org-last-inserted-timestamp))))
  8305. (defun org-schedule (&optional remove time)
  8306. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8307. With argument REMOVE, remove any scheduling date from the item.
  8308. When TIME is set, it should be an internal time specification, and the
  8309. scheduling will use the corresponding date."
  8310. (interactive "P")
  8311. (if remove
  8312. (progn
  8313. (org-remove-timestamp-with-keyword org-scheduled-string)
  8314. (message "Item is no longer scheduled."))
  8315. (if (org-get-repeat)
  8316. (error "Cannot reschedule task with repeater, please do that by hand")
  8317. (org-add-planning-info 'scheduled time 'closed)
  8318. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8319. (defun org-get-scheduled-time (pom &optional inherit)
  8320. "Get the scheduled time as a time tuple, of a format suitable
  8321. for calling org-schedule with, or if there is no scheduling,
  8322. returns nil."
  8323. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8324. (when time
  8325. (apply 'encode-time (org-parse-time-string time)))))
  8326. (defun org-get-deadline-time (pom &optional inherit)
  8327. "Get the deadine as a time tuple, of a format suitable for
  8328. calling org-deadlin with, or if there is no scheduling, returns
  8329. nil."
  8330. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8331. (when time
  8332. (apply 'encode-time (org-parse-time-string time)))))
  8333. (defun org-remove-timestamp-with-keyword (keyword)
  8334. "Remove all time stamps with KEYWORD in the current entry."
  8335. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8336. beg)
  8337. (save-excursion
  8338. (org-back-to-heading t)
  8339. (setq beg (point))
  8340. (org-end-of-subtree t t)
  8341. (while (re-search-backward re beg t)
  8342. (replace-match "")
  8343. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8344. (equal (char-before) ?\ ))
  8345. (backward-delete-char 1)
  8346. (if (string-match "^[ \t]*$" (buffer-substring
  8347. (point-at-bol) (point-at-eol)))
  8348. (delete-region (point-at-bol)
  8349. (min (point-max) (1+ (point-at-eol))))))))))
  8350. (defun org-add-planning-info (what &optional time &rest remove)
  8351. "Insert new timestamp with keyword in the line directly after the headline.
  8352. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8353. If non is given, the user is prompted for a date.
  8354. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8355. be removed."
  8356. (interactive)
  8357. (let (org-time-was-given org-end-time-was-given ts
  8358. end default-time default-input)
  8359. (catch 'exit
  8360. (when (and (not time) (memq what '(scheduled deadline)))
  8361. ;; Try to get a default date/time from existing timestamp
  8362. (save-excursion
  8363. (org-back-to-heading t)
  8364. (setq end (save-excursion (outline-next-heading) (point)))
  8365. (when (re-search-forward (if (eq what 'scheduled)
  8366. org-scheduled-time-regexp
  8367. org-deadline-time-regexp)
  8368. end t)
  8369. (setq ts (match-string 1)
  8370. default-time
  8371. (apply 'encode-time (org-parse-time-string ts))
  8372. default-input (and ts (org-get-compact-tod ts))))))
  8373. (when what
  8374. ;; If necessary, get the time from the user
  8375. (setq time (or time (org-read-date nil 'to-time nil nil
  8376. default-time default-input))))
  8377. (when (and org-insert-labeled-timestamps-at-point
  8378. (member what '(scheduled deadline)))
  8379. (insert
  8380. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8381. (org-insert-time-stamp time org-time-was-given
  8382. nil nil nil (list org-end-time-was-given))
  8383. (setq what nil))
  8384. (save-excursion
  8385. (save-restriction
  8386. (let (col list elt ts buffer-invisibility-spec)
  8387. (org-back-to-heading t)
  8388. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8389. (goto-char (match-end 1))
  8390. (setq col (current-column))
  8391. (goto-char (match-end 0))
  8392. (if (eobp) (insert "\n") (forward-char 1))
  8393. (when (and (not what)
  8394. (not (looking-at org-keyword-time-not-clock-regexp)))
  8395. ;; Nothing to add, nothing to remove...... :-)
  8396. (throw 'exit nil))
  8397. (if (and (not (looking-at outline-regexp))
  8398. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8399. "[^\r\n]*"))
  8400. (not (equal (match-string 1) org-clock-string)))
  8401. (narrow-to-region (match-beginning 0) (match-end 0))
  8402. (insert-before-markers "\n")
  8403. (backward-char 1)
  8404. (narrow-to-region (point) (point))
  8405. (and org-adapt-indentation (org-indent-to-column col)))
  8406. ;; Check if we have to remove something.
  8407. (setq list (cons what remove))
  8408. (while list
  8409. (setq elt (pop list))
  8410. (goto-char (point-min))
  8411. (when (or (and (eq elt 'scheduled)
  8412. (re-search-forward org-scheduled-time-regexp nil t))
  8413. (and (eq elt 'deadline)
  8414. (re-search-forward org-deadline-time-regexp nil t))
  8415. (and (eq elt 'closed)
  8416. (re-search-forward org-closed-time-regexp nil t)))
  8417. (replace-match "")
  8418. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8419. (if (looking-at " +") (replace-match ""))))
  8420. (goto-char (point-max))
  8421. (when what
  8422. (insert
  8423. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8424. (cond ((eq what 'scheduled) org-scheduled-string)
  8425. ((eq what 'deadline) org-deadline-string)
  8426. ((eq what 'closed) org-closed-string))
  8427. " ")
  8428. (setq ts (org-insert-time-stamp
  8429. time
  8430. (or org-time-was-given
  8431. (and (eq what 'closed) org-log-done-with-time))
  8432. (eq what 'closed)
  8433. nil nil (list org-end-time-was-given)))
  8434. (end-of-line 1))
  8435. (goto-char (point-min))
  8436. (widen)
  8437. (if (and (looking-at "[ \t]+\n")
  8438. (equal (char-before) ?\n))
  8439. (delete-region (1- (point)) (point-at-eol)))
  8440. ts))))))
  8441. (defvar org-log-note-marker (make-marker))
  8442. (defvar org-log-note-purpose nil)
  8443. (defvar org-log-note-state nil)
  8444. (defvar org-log-note-previous-state nil)
  8445. (defvar org-log-note-how nil)
  8446. (defvar org-log-note-extra nil)
  8447. (defvar org-log-note-window-configuration nil)
  8448. (defvar org-log-note-return-to (make-marker))
  8449. (defvar org-log-post-message nil
  8450. "Message to be displayed after a log note has been stored.
  8451. The auto-repeater uses this.")
  8452. (defun org-add-note ()
  8453. "Add a note to the current entry.
  8454. This is done in the same way as adding a state change note."
  8455. (interactive)
  8456. (org-add-log-setup 'note nil nil 'findpos nil))
  8457. (defvar org-property-end-re)
  8458. (defun org-add-log-setup (&optional purpose state prev-state
  8459. findpos how &optional extra)
  8460. "Set up the post command hook to take a note.
  8461. If this is about to TODO state change, the new state is expected in STATE.
  8462. When FINDPOS is non-nil, find the correct position for the note in
  8463. the current entry. If not, assume that it can be inserted at point.
  8464. HOW is an indicator what kind of note should be created.
  8465. EXTRA is additional text that will be inserted into the notes buffer."
  8466. (let ((drawer (cond ((stringp org-log-into-drawer)
  8467. org-log-into-drawer)
  8468. (org-log-into-drawer "LOGBOOK")
  8469. (t nil))))
  8470. (save-restriction
  8471. (save-excursion
  8472. (when findpos
  8473. (org-back-to-heading t)
  8474. (narrow-to-region (point) (save-excursion
  8475. (outline-next-heading) (point)))
  8476. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8477. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8478. "[^\r\n]*\\)?"))
  8479. (goto-char (match-end 0))
  8480. (cond
  8481. (drawer
  8482. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8483. nil t)
  8484. (progn
  8485. (goto-char (match-end 0))
  8486. (or org-log-states-order-reversed
  8487. (and (re-search-forward org-property-end-re nil t)
  8488. (goto-char (1- (match-beginning 0))))))
  8489. (insert "\n:" drawer ":\n:END:")
  8490. (beginning-of-line 0)
  8491. (org-indent-line-function)
  8492. (beginning-of-line 2)
  8493. (org-indent-line-function)
  8494. (end-of-line 0)))
  8495. ((and org-log-state-notes-insert-after-drawers
  8496. (save-excursion
  8497. (forward-line) (looking-at org-drawer-regexp)))
  8498. (forward-line)
  8499. (while (looking-at org-drawer-regexp)
  8500. (goto-char (match-end 0))
  8501. (re-search-forward org-property-end-re (point-max) t)
  8502. (forward-line))
  8503. (forward-line -1)))
  8504. (unless org-log-states-order-reversed
  8505. (and (= (char-after) ?\n) (forward-char 1))
  8506. (org-skip-over-state-notes)
  8507. (skip-chars-backward " \t\n\r")))
  8508. (move-marker org-log-note-marker (point))
  8509. (setq org-log-note-purpose purpose
  8510. org-log-note-state state
  8511. org-log-note-previous-state prev-state
  8512. org-log-note-how how
  8513. org-log-note-extra extra)
  8514. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8515. (defun org-skip-over-state-notes ()
  8516. "Skip past the list of State notes in an entry."
  8517. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8518. (while (looking-at "[ \t]*- State")
  8519. (condition-case nil
  8520. (org-next-item)
  8521. (error (org-end-of-item)))))
  8522. (defun org-add-log-note (&optional purpose)
  8523. "Pop up a window for taking a note, and add this note later at point."
  8524. (remove-hook 'post-command-hook 'org-add-log-note)
  8525. (setq org-log-note-window-configuration (current-window-configuration))
  8526. (delete-other-windows)
  8527. (move-marker org-log-note-return-to (point))
  8528. (switch-to-buffer (marker-buffer org-log-note-marker))
  8529. (goto-char org-log-note-marker)
  8530. (org-switch-to-buffer-other-window "*Org Note*")
  8531. (erase-buffer)
  8532. (if (memq org-log-note-how '(time state))
  8533. (let (current-prefix-arg) (org-store-log-note))
  8534. (let ((org-inhibit-startup t)) (org-mode))
  8535. (insert (format "# Insert note for %s.
  8536. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8537. (cond
  8538. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8539. ((eq org-log-note-purpose 'done) "closed todo item")
  8540. ((eq org-log-note-purpose 'state)
  8541. (format "state change from \"%s\" to \"%s\""
  8542. (or org-log-note-previous-state "")
  8543. (or org-log-note-state "")))
  8544. ((eq org-log-note-purpose 'note)
  8545. "this entry")
  8546. (t (error "This should not happen")))))
  8547. (if org-log-note-extra (insert org-log-note-extra))
  8548. (org-set-local 'org-finish-function 'org-store-log-note)))
  8549. (defvar org-note-abort nil) ; dynamically scoped
  8550. (defun org-store-log-note ()
  8551. "Finish taking a log note, and insert it to where it belongs."
  8552. (let ((txt (buffer-string))
  8553. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8554. lines ind)
  8555. (kill-buffer (current-buffer))
  8556. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8557. (setq txt (replace-match "" t t txt)))
  8558. (if (string-match "\\s-+\\'" txt)
  8559. (setq txt (replace-match "" t t txt)))
  8560. (setq lines (org-split-string txt "\n"))
  8561. (when (and note (string-match "\\S-" note))
  8562. (setq note
  8563. (org-replace-escapes
  8564. note
  8565. (list (cons "%u" (user-login-name))
  8566. (cons "%U" user-full-name)
  8567. (cons "%t" (format-time-string
  8568. (org-time-stamp-format 'long 'inactive)
  8569. (current-time)))
  8570. (cons "%s" (if org-log-note-state
  8571. (concat "\"" org-log-note-state "\"")
  8572. ""))
  8573. (cons "%S" (if org-log-note-previous-state
  8574. (concat "\"" org-log-note-previous-state "\"")
  8575. "\"\"")))))
  8576. (if lines (setq note (concat note " \\\\")))
  8577. (push note lines))
  8578. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8579. (when lines
  8580. (save-excursion
  8581. (set-buffer (marker-buffer org-log-note-marker))
  8582. (save-excursion
  8583. (goto-char org-log-note-marker)
  8584. (move-marker org-log-note-marker nil)
  8585. (end-of-line 1)
  8586. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8587. (insert "- " (pop lines))
  8588. (org-indent-line-function)
  8589. (beginning-of-line 1)
  8590. (looking-at "[ \t]*")
  8591. (setq ind (concat (match-string 0) " "))
  8592. (end-of-line 1)
  8593. (while lines (insert "\n" ind (pop lines)))
  8594. (message "Note stored")
  8595. (org-back-to-heading t)
  8596. (org-cycle-hide-drawers 'children)))))
  8597. (set-window-configuration org-log-note-window-configuration)
  8598. (with-current-buffer (marker-buffer org-log-note-return-to)
  8599. (goto-char org-log-note-return-to))
  8600. (move-marker org-log-note-return-to nil)
  8601. (and org-log-post-message (message "%s" org-log-post-message)))
  8602. (defun org-sparse-tree (&optional arg)
  8603. "Create a sparse tree, prompt for the details.
  8604. This command can create sparse trees. You first need to select the type
  8605. of match used to create the tree:
  8606. t Show entries with a specific TODO keyword.
  8607. T Show entries selected by a tags match.
  8608. p Enter a property name and its value (both with completion on existing
  8609. names/values) and show entries with that property.
  8610. r Show entries matching a regular expression
  8611. d Show deadlines due within `org-deadline-warning-days'."
  8612. (interactive "P")
  8613. (let (ans kwd value)
  8614. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8615. (setq ans (read-char-exclusive))
  8616. (cond
  8617. ((equal ans ?d)
  8618. (call-interactively 'org-check-deadlines))
  8619. ((equal ans ?b)
  8620. (call-interactively 'org-check-before-date))
  8621. ((equal ans ?t)
  8622. (org-show-todo-tree '(4)))
  8623. ((equal ans ?T)
  8624. (call-interactively 'org-tags-sparse-tree))
  8625. ((member ans '(?p ?P))
  8626. (setq kwd (org-ido-completing-read "Property: "
  8627. (mapcar 'list (org-buffer-property-keys))))
  8628. (setq value (org-ido-completing-read "Value: "
  8629. (mapcar 'list (org-property-values kwd))))
  8630. (unless (string-match "\\`{.*}\\'" value)
  8631. (setq value (concat "\"" value "\"")))
  8632. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8633. ((member ans '(?r ?R ?/))
  8634. (call-interactively 'org-occur))
  8635. (t (error "No such sparse tree command \"%c\"" ans)))))
  8636. (defvar org-occur-highlights nil
  8637. "List of overlays used for occur matches.")
  8638. (make-variable-buffer-local 'org-occur-highlights)
  8639. (defvar org-occur-parameters nil
  8640. "Parameters of the active org-occur calls.
  8641. This is a list, each call to org-occur pushes as cons cell,
  8642. containing the regular expression and the callback, onto the list.
  8643. The list can contain several entries if `org-occur' has been called
  8644. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8645. will only contain one set of parameters. When the highlights are
  8646. removed (for example with `C-c C-c', or with the next edit (depending
  8647. on `org-remove-highlights-with-change'), this variable is emptied
  8648. as well.")
  8649. (make-variable-buffer-local 'org-occur-parameters)
  8650. (defun org-occur (regexp &optional keep-previous callback)
  8651. "Make a compact tree which shows all matches of REGEXP.
  8652. The tree will show the lines where the regexp matches, and all higher
  8653. headlines above the match. It will also show the heading after the match,
  8654. to make sure editing the matching entry is easy.
  8655. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8656. call to `org-occur' will be kept, to allow stacking of calls to this
  8657. command.
  8658. If CALLBACK is non-nil, it is a function which is called to confirm
  8659. that the match should indeed be shown."
  8660. (interactive "sRegexp: \nP")
  8661. (unless keep-previous
  8662. (org-remove-occur-highlights nil nil t))
  8663. (push (cons regexp callback) org-occur-parameters)
  8664. (let ((cnt 0))
  8665. (save-excursion
  8666. (goto-char (point-min))
  8667. (if (or (not keep-previous) ; do not want to keep
  8668. (not org-occur-highlights)) ; no previous matches
  8669. ;; hide everything
  8670. (org-overview))
  8671. (while (re-search-forward regexp nil t)
  8672. (when (or (not callback)
  8673. (save-match-data (funcall callback)))
  8674. (setq cnt (1+ cnt))
  8675. (when org-highlight-sparse-tree-matches
  8676. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8677. (org-show-context 'occur-tree))))
  8678. (when org-remove-highlights-with-change
  8679. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8680. nil 'local))
  8681. (unless org-sparse-tree-open-archived-trees
  8682. (org-hide-archived-subtrees (point-min) (point-max)))
  8683. (run-hooks 'org-occur-hook)
  8684. (if (interactive-p)
  8685. (message "%d match(es) for regexp %s" cnt regexp))
  8686. cnt))
  8687. (defun org-show-context (&optional key)
  8688. "Make sure point and context and visible.
  8689. How much context is shown depends upon the variables
  8690. `org-show-hierarchy-above', `org-show-following-heading'. and
  8691. `org-show-siblings'."
  8692. (let ((heading-p (org-on-heading-p t))
  8693. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8694. (following-p (org-get-alist-option org-show-following-heading key))
  8695. (entry-p (org-get-alist-option org-show-entry-below key))
  8696. (siblings-p (org-get-alist-option org-show-siblings key)))
  8697. (catch 'exit
  8698. ;; Show heading or entry text
  8699. (if (and heading-p (not entry-p))
  8700. (org-flag-heading nil) ; only show the heading
  8701. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8702. (org-show-hidden-entry))) ; show entire entry
  8703. (when following-p
  8704. ;; Show next sibling, or heading below text
  8705. (save-excursion
  8706. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8707. (org-flag-heading nil))))
  8708. (when siblings-p (org-show-siblings))
  8709. (when hierarchy-p
  8710. ;; show all higher headings, possibly with siblings
  8711. (save-excursion
  8712. (while (and (condition-case nil
  8713. (progn (org-up-heading-all 1) t)
  8714. (error nil))
  8715. (not (bobp)))
  8716. (org-flag-heading nil)
  8717. (when siblings-p (org-show-siblings))))))))
  8718. (defun org-reveal (&optional siblings)
  8719. "Show current entry, hierarchy above it, and the following headline.
  8720. This can be used to show a consistent set of context around locations
  8721. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8722. not t for the search context.
  8723. With optional argument SIBLINGS, on each level of the hierarchy all
  8724. siblings are shown. This repairs the tree structure to what it would
  8725. look like when opened with hierarchical calls to `org-cycle'."
  8726. (interactive "P")
  8727. (let ((org-show-hierarchy-above t)
  8728. (org-show-following-heading t)
  8729. (org-show-siblings (if siblings t org-show-siblings)))
  8730. (org-show-context nil)))
  8731. (defun org-highlight-new-match (beg end)
  8732. "Highlight from BEG to END and mark the highlight is an occur headline."
  8733. (let ((ov (org-make-overlay beg end)))
  8734. (org-overlay-put ov 'face 'secondary-selection)
  8735. (push ov org-occur-highlights)))
  8736. (defun org-remove-occur-highlights (&optional beg end noremove)
  8737. "Remove the occur highlights from the buffer.
  8738. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8739. from the `before-change-functions' in the current buffer."
  8740. (interactive)
  8741. (unless org-inhibit-highlight-removal
  8742. (mapc 'org-delete-overlay org-occur-highlights)
  8743. (setq org-occur-highlights nil)
  8744. (setq org-occur-parameters nil)
  8745. (unless noremove
  8746. (remove-hook 'before-change-functions
  8747. 'org-remove-occur-highlights 'local))))
  8748. ;;;; Priorities
  8749. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8750. "Regular expression matching the priority indicator.")
  8751. (defvar org-remove-priority-next-time nil)
  8752. (defun org-priority-up ()
  8753. "Increase the priority of the current item."
  8754. (interactive)
  8755. (org-priority 'up))
  8756. (defun org-priority-down ()
  8757. "Decrease the priority of the current item."
  8758. (interactive)
  8759. (org-priority 'down))
  8760. (defun org-priority (&optional action)
  8761. "Change the priority of an item by ARG.
  8762. ACTION can be `set', `up', `down', or a character."
  8763. (interactive)
  8764. (setq action (or action 'set))
  8765. (let (current new news have remove)
  8766. (save-excursion
  8767. (org-back-to-heading t)
  8768. (if (looking-at org-priority-regexp)
  8769. (setq current (string-to-char (match-string 2))
  8770. have t)
  8771. (setq current org-default-priority))
  8772. (cond
  8773. ((or (eq action 'set)
  8774. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8775. (if (not (eq action 'set))
  8776. (setq new action)
  8777. (message "Priority %c-%c, SPC to remove: "
  8778. org-highest-priority org-lowest-priority)
  8779. (setq new (read-char-exclusive)))
  8780. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8781. (= (upcase org-lowest-priority) org-lowest-priority))
  8782. (setq new (upcase new)))
  8783. (cond ((equal new ?\ ) (setq remove t))
  8784. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8785. (error "Priority must be between `%c' and `%c'"
  8786. org-highest-priority org-lowest-priority))))
  8787. ((eq action 'up)
  8788. (if (and (not have) (eq last-command this-command))
  8789. (setq new org-lowest-priority)
  8790. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8791. org-default-priority (1- current)))))
  8792. ((eq action 'down)
  8793. (if (and (not have) (eq last-command this-command))
  8794. (setq new org-highest-priority)
  8795. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8796. org-default-priority (1+ current)))))
  8797. (t (error "Invalid action")))
  8798. (if (or (< (upcase new) org-highest-priority)
  8799. (> (upcase new) org-lowest-priority))
  8800. (setq remove t))
  8801. (setq news (format "%c" new))
  8802. (if have
  8803. (if remove
  8804. (replace-match "" t t nil 1)
  8805. (replace-match news t t nil 2))
  8806. (if remove
  8807. (error "No priority cookie found in line")
  8808. (looking-at org-todo-line-regexp)
  8809. (if (match-end 2)
  8810. (progn
  8811. (goto-char (match-end 2))
  8812. (insert " [#" news "]"))
  8813. (goto-char (match-beginning 3))
  8814. (insert "[#" news "] ")))))
  8815. (org-preserve-lc (org-set-tags nil 'align))
  8816. (if remove
  8817. (message "Priority removed")
  8818. (message "Priority of current item set to %s" news))))
  8819. (defun org-get-priority (s)
  8820. "Find priority cookie and return priority."
  8821. (save-match-data
  8822. (if (not (string-match org-priority-regexp s))
  8823. (* 1000 (- org-lowest-priority org-default-priority))
  8824. (* 1000 (- org-lowest-priority
  8825. (string-to-char (match-string 2 s)))))))
  8826. ;;;; Tags
  8827. (defvar org-agenda-archives-mode)
  8828. (defun org-scan-tags (action matcher &optional todo-only)
  8829. "Scan headline tags with inheritance and produce output ACTION.
  8830. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8831. or `agenda' to produce an entry list for an agenda view. It can also be
  8832. a Lisp form or a function that should be called at each matched headline, in
  8833. this case the return value is a list of all return values from these calls.
  8834. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8835. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8836. only lines with a TODO keyword are included in the output."
  8837. (require 'org-agenda)
  8838. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8839. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8840. (org-re
  8841. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8842. (props (list 'face 'default
  8843. 'done-face 'org-done
  8844. 'undone-face 'default
  8845. 'mouse-face 'highlight
  8846. 'org-not-done-regexp org-not-done-regexp
  8847. 'org-todo-regexp org-todo-regexp
  8848. 'keymap org-agenda-keymap
  8849. 'help-echo
  8850. (format "mouse-2 or RET jump to org file %s"
  8851. (abbreviate-file-name
  8852. (or (buffer-file-name (buffer-base-buffer))
  8853. (buffer-name (buffer-base-buffer)))))))
  8854. (case-fold-search nil)
  8855. lspos tags tags-list
  8856. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8857. (llast 0) rtn rtn1 level category i txt
  8858. todo marker entry priority)
  8859. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8860. (setq action (list 'lambda nil action)))
  8861. (save-excursion
  8862. (goto-char (point-min))
  8863. (when (eq action 'sparse-tree)
  8864. (org-overview)
  8865. (org-remove-occur-highlights))
  8866. (while (re-search-forward re nil t)
  8867. (catch :skip
  8868. (setq todo (if (match-end 1) (match-string 2))
  8869. tags (if (match-end 4) (match-string 4)))
  8870. (goto-char (setq lspos (1+ (match-beginning 0))))
  8871. (setq level (org-reduced-level (funcall outline-level))
  8872. category (org-get-category))
  8873. (setq i llast llast level)
  8874. ;; remove tag lists from same and sublevels
  8875. (while (>= i level)
  8876. (when (setq entry (assoc i tags-alist))
  8877. (setq tags-alist (delete entry tags-alist)))
  8878. (setq i (1- i)))
  8879. ;; add the next tags
  8880. (when tags
  8881. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8882. tags-alist
  8883. (cons (cons level tags) tags-alist)))
  8884. ;; compile tags for current headline
  8885. (setq tags-list
  8886. (if org-use-tag-inheritance
  8887. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8888. tags))
  8889. (when org-use-tag-inheritance
  8890. (setcdr (car tags-alist)
  8891. (mapcar (lambda (x)
  8892. (setq x (copy-sequence x))
  8893. (org-add-prop-inherited x))
  8894. (cdar tags-alist))))
  8895. (when (and tags org-use-tag-inheritance
  8896. (not (eq t org-use-tag-inheritance)))
  8897. ;; selective inheritance, remove uninherited ones
  8898. (setcdr (car tags-alist)
  8899. (org-remove-uniherited-tags (cdar tags-alist))))
  8900. (when (and (or (not todo-only)
  8901. (and (member todo org-not-done-keywords)
  8902. (or (not org-agenda-tags-todo-honor-ignore-options)
  8903. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8904. (let ((case-fold-search t)) (eval matcher))
  8905. (or
  8906. (not (member org-archive-tag tags-list))
  8907. ;; we have an archive tag, should we use this anyway?
  8908. (or (not org-agenda-skip-archived-trees)
  8909. (and (eq action 'agenda) org-agenda-archives-mode))))
  8910. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8911. ;; select this headline
  8912. (cond
  8913. ((eq action 'sparse-tree)
  8914. (and org-highlight-sparse-tree-matches
  8915. (org-get-heading) (match-end 0)
  8916. (org-highlight-new-match
  8917. (match-beginning 0) (match-beginning 1)))
  8918. (org-show-context 'tags-tree))
  8919. ((eq action 'agenda)
  8920. (setq txt (org-format-agenda-item
  8921. ""
  8922. (concat
  8923. (if org-tags-match-list-sublevels
  8924. (make-string (1- level) ?.) "")
  8925. (org-get-heading))
  8926. category (org-get-tags-at))
  8927. priority (org-get-priority txt))
  8928. (goto-char lspos)
  8929. (setq marker (org-agenda-new-marker))
  8930. (org-add-props txt props
  8931. 'org-marker marker 'org-hd-marker marker 'org-category category
  8932. 'todo-state todo
  8933. 'priority priority 'type "tagsmatch")
  8934. (push txt rtn))
  8935. ((functionp action)
  8936. (save-excursion
  8937. (setq rtn1 (funcall action))
  8938. (push rtn1 rtn))
  8939. (goto-char (point-at-eol)))
  8940. (t (error "Invalid action")))
  8941. ;; if we are to skip sublevels, jump to end of subtree
  8942. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8943. (when (and (eq action 'sparse-tree)
  8944. (not org-sparse-tree-open-archived-trees))
  8945. (org-hide-archived-subtrees (point-min) (point-max)))
  8946. (nreverse rtn)))
  8947. (defun org-remove-uniherited-tags (tags)
  8948. "Remove all tags that are not inherited from the list TAGS."
  8949. (cond
  8950. ((eq org-use-tag-inheritance t)
  8951. (if org-tags-exclude-from-inheritance
  8952. (org-delete-all org-tags-exclude-from-inheritance tags)
  8953. tags))
  8954. ((not org-use-tag-inheritance) nil)
  8955. ((stringp org-use-tag-inheritance)
  8956. (delq nil (mapcar
  8957. (lambda (x)
  8958. (if (and (string-match org-use-tag-inheritance x)
  8959. (not (member x org-tags-exclude-from-inheritance)))
  8960. x nil))
  8961. tags)))
  8962. ((listp org-use-tag-inheritance)
  8963. (delq nil (mapcar
  8964. (lambda (x)
  8965. (if (member x org-use-tag-inheritance) x nil))
  8966. tags)))))
  8967. (defvar todo-only) ;; dynamically scoped
  8968. (defun org-tags-sparse-tree (&optional todo-only match)
  8969. "Create a sparse tree according to tags string MATCH.
  8970. MATCH can contain positive and negative selection of tags, like
  8971. \"+WORK+URGENT-WITHBOSS\".
  8972. If optional argument TODO-ONLY is non-nil, only select lines that are
  8973. also TODO lines."
  8974. (interactive "P")
  8975. (org-prepare-agenda-buffers (list (current-buffer)))
  8976. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8977. (defvar org-cached-props nil)
  8978. (defun org-cached-entry-get (pom property)
  8979. (if (or (eq t org-use-property-inheritance)
  8980. (and (stringp org-use-property-inheritance)
  8981. (string-match org-use-property-inheritance property))
  8982. (and (listp org-use-property-inheritance)
  8983. (member property org-use-property-inheritance)))
  8984. ;; Caching is not possible, check it directly
  8985. (org-entry-get pom property 'inherit)
  8986. ;; Get all properties, so that we can do complicated checks easily
  8987. (cdr (assoc property (or org-cached-props
  8988. (setq org-cached-props
  8989. (org-entry-properties pom)))))))
  8990. (defun org-global-tags-completion-table (&optional files)
  8991. "Return the list of all tags in all agenda buffer/files."
  8992. (save-excursion
  8993. (org-uniquify
  8994. (delq nil
  8995. (apply 'append
  8996. (mapcar
  8997. (lambda (file)
  8998. (set-buffer (find-file-noselect file))
  8999. (append (org-get-buffer-tags)
  9000. (mapcar (lambda (x) (if (stringp (car-safe x))
  9001. (list (car-safe x)) nil))
  9002. org-tag-alist)))
  9003. (if (and files (car files))
  9004. files
  9005. (org-agenda-files))))))))
  9006. (defun org-make-tags-matcher (match)
  9007. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9008. ;; todo-only is scoped dynamically into this function, and the function
  9009. ;; may change it if the matcher asks for it.
  9010. (unless match
  9011. ;; Get a new match request, with completion
  9012. (let ((org-last-tags-completion-table
  9013. (org-global-tags-completion-table)))
  9014. (setq match (org-completing-read-no-ido
  9015. "Match: " 'org-tags-completion-function nil nil nil
  9016. 'org-tags-history))))
  9017. ;; Parse the string and create a lisp form
  9018. (let ((match0 match)
  9019. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9020. minus tag mm
  9021. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9022. orterms term orlist re-p str-p level-p level-op time-p
  9023. prop-p pn pv po cat-p gv rest)
  9024. (if (string-match "/+" match)
  9025. ;; match contains also a todo-matching request
  9026. (progn
  9027. (setq tagsmatch (substring match 0 (match-beginning 0))
  9028. todomatch (substring match (match-end 0)))
  9029. (if (string-match "^!" todomatch)
  9030. (setq todo-only t todomatch (substring todomatch 1)))
  9031. (if (string-match "^\\s-*$" todomatch)
  9032. (setq todomatch nil)))
  9033. ;; only matching tags
  9034. (setq tagsmatch match todomatch nil))
  9035. ;; Make the tags matcher
  9036. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9037. (setq tagsmatcher t)
  9038. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9039. (while (setq term (pop orterms))
  9040. (while (and (equal (substring term -1) "\\") orterms)
  9041. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9042. (while (string-match re term)
  9043. (setq rest (substring term (match-end 0))
  9044. minus (and (match-end 1)
  9045. (equal (match-string 1 term) "-"))
  9046. tag (match-string 2 term)
  9047. re-p (equal (string-to-char tag) ?{)
  9048. level-p (match-end 4)
  9049. prop-p (match-end 5)
  9050. mm (cond
  9051. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9052. (level-p
  9053. (setq level-op (org-op-to-function (match-string 3 term)))
  9054. `(,level-op level ,(string-to-number
  9055. (match-string 4 term))))
  9056. (prop-p
  9057. (setq pn (match-string 5 term)
  9058. po (match-string 6 term)
  9059. pv (match-string 7 term)
  9060. cat-p (equal pn "CATEGORY")
  9061. re-p (equal (string-to-char pv) ?{)
  9062. str-p (equal (string-to-char pv) ?\")
  9063. time-p (save-match-data
  9064. (string-match "^\"[[<].*[]>]\"$" pv))
  9065. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9066. (if time-p (setq pv (org-matcher-time pv)))
  9067. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9068. (cond
  9069. ((equal pn "CATEGORY")
  9070. (setq gv '(get-text-property (point) 'org-category)))
  9071. ((equal pn "TODO")
  9072. (setq gv 'todo))
  9073. (t
  9074. (setq gv `(org-cached-entry-get nil ,pn))))
  9075. (if re-p
  9076. (if (eq po 'org<>)
  9077. `(not (string-match ,pv (or ,gv "")))
  9078. `(string-match ,pv (or ,gv "")))
  9079. (if str-p
  9080. `(,po (or ,gv "") ,pv)
  9081. `(,po (string-to-number (or ,gv ""))
  9082. ,(string-to-number pv) ))))
  9083. (t `(member ,(downcase tag) tags-list)))
  9084. mm (if minus (list 'not mm) mm)
  9085. term rest)
  9086. (push mm tagsmatcher))
  9087. (push (if (> (length tagsmatcher) 1)
  9088. (cons 'and tagsmatcher)
  9089. (car tagsmatcher))
  9090. orlist)
  9091. (setq tagsmatcher nil))
  9092. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9093. (setq tagsmatcher
  9094. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9095. ;; Make the todo matcher
  9096. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9097. (setq todomatcher t)
  9098. (setq orterms (org-split-string todomatch "|") orlist nil)
  9099. (while (setq term (pop orterms))
  9100. (while (string-match re term)
  9101. (setq minus (and (match-end 1)
  9102. (equal (match-string 1 term) "-"))
  9103. kwd (match-string 2 term)
  9104. re-p (equal (string-to-char kwd) ?{)
  9105. term (substring term (match-end 0))
  9106. mm (if re-p
  9107. `(string-match ,(substring kwd 1 -1) todo)
  9108. (list 'equal 'todo kwd))
  9109. mm (if minus (list 'not mm) mm))
  9110. (push mm todomatcher))
  9111. (push (if (> (length todomatcher) 1)
  9112. (cons 'and todomatcher)
  9113. (car todomatcher))
  9114. orlist)
  9115. (setq todomatcher nil))
  9116. (setq todomatcher (if (> (length orlist) 1)
  9117. (cons 'or orlist) (car orlist))))
  9118. ;; Return the string and lisp forms of the matcher
  9119. (setq matcher (if todomatcher
  9120. (list 'and tagsmatcher todomatcher)
  9121. tagsmatcher))
  9122. (cons match0 matcher)))
  9123. (defun org-op-to-function (op &optional stringp)
  9124. "Turn an operator into the appropriate function."
  9125. (setq op
  9126. (cond
  9127. ((equal op "<" ) '(< string< org-time<))
  9128. ((equal op ">" ) '(> org-string> org-time>))
  9129. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9130. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9131. ((member op '("=" "==")) '(= string= org-time=))
  9132. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9133. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9134. (defun org<> (a b) (not (= a b)))
  9135. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9136. (defun org-string>= (a b) (not (string< a b)))
  9137. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9138. (defun org-string<> (a b) (not (string= a b)))
  9139. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9140. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9141. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9142. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9143. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9144. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9145. (defun org-2ft (s)
  9146. "Convert S to a floating point time.
  9147. If S is already a number, just return it. If it is a string, parse
  9148. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9149. (cond
  9150. ((numberp s) s)
  9151. ((stringp s)
  9152. (condition-case nil
  9153. (float-time (apply 'encode-time (org-parse-time-string s)))
  9154. (error 0.)))
  9155. (t 0.)))
  9156. (defun org-time-today ()
  9157. "Time in seconds today at 0:00.
  9158. Returns the float number of seconds since the beginning of the
  9159. epoch to the beginning of today (00:00)."
  9160. (float-time (apply 'encode-time
  9161. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9162. (defun org-matcher-time (s)
  9163. "Interpret a time comparison value."
  9164. (save-match-data
  9165. (cond
  9166. ((string= s "<now>") (float-time))
  9167. ((string= s "<today>") (org-time-today))
  9168. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9169. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9170. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9171. (+ (org-time-today)
  9172. (* (string-to-number (match-string 1 s))
  9173. (cdr (assoc (match-string 2 s)
  9174. '(("d" . 86400.0) ("w" . 604800.0)
  9175. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9176. (t (org-2ft s)))))
  9177. (defun org-match-any-p (re list)
  9178. "Does re match any element of list?"
  9179. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9180. (delq nil list))
  9181. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9182. (defvar org-tags-overlay (org-make-overlay 1 1))
  9183. (org-detach-overlay org-tags-overlay)
  9184. (defun org-get-local-tags-at (&optional pos)
  9185. "Get a list of tags defined in the current headline."
  9186. (org-get-tags-at pos 'local))
  9187. (defun org-get-local-tags ()
  9188. "Get a list of tags defined in the current headline."
  9189. (org-get-tags-at nil 'local))
  9190. (defun org-get-tags-at (&optional pos local)
  9191. "Get a list of all headline tags applicable at POS.
  9192. POS defaults to point. If tags are inherited, the list contains
  9193. the targets in the same sequence as the headlines appear, i.e.
  9194. the tags of the current headline come last.
  9195. When LOCAL is non-nil, only return tags from the current headline,
  9196. ignore inherited ones."
  9197. (interactive)
  9198. (let (tags ltags lastpos parent)
  9199. (save-excursion
  9200. (save-restriction
  9201. (widen)
  9202. (goto-char (or pos (point)))
  9203. (save-match-data
  9204. (catch 'done
  9205. (condition-case nil
  9206. (progn
  9207. (org-back-to-heading t)
  9208. (while (not (equal lastpos (point)))
  9209. (setq lastpos (point))
  9210. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9211. (setq ltags (org-split-string
  9212. (org-match-string-no-properties 1) ":"))
  9213. (when parent
  9214. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9215. (setq tags (append
  9216. (if parent
  9217. (org-remove-uniherited-tags ltags)
  9218. ltags)
  9219. tags)))
  9220. (or org-use-tag-inheritance (throw 'done t))
  9221. (if local (throw 'done t))
  9222. (org-up-heading-all 1)
  9223. (setq parent t)))
  9224. (error nil)))))
  9225. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9226. (defun org-add-prop-inherited (s)
  9227. (add-text-properties 0 (length s) '(inherited t) s)
  9228. s)
  9229. (defun org-toggle-tag (tag &optional onoff)
  9230. "Toggle the tag TAG for the current line.
  9231. If ONOFF is `on' or `off', don't toggle but set to this state."
  9232. (let (res current)
  9233. (save-excursion
  9234. (org-back-to-heading t)
  9235. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9236. (point-at-eol) t)
  9237. (progn
  9238. (setq current (match-string 1))
  9239. (replace-match ""))
  9240. (setq current ""))
  9241. (setq current (nreverse (org-split-string current ":")))
  9242. (cond
  9243. ((eq onoff 'on)
  9244. (setq res t)
  9245. (or (member tag current) (push tag current)))
  9246. ((eq onoff 'off)
  9247. (or (not (member tag current)) (setq current (delete tag current))))
  9248. (t (if (member tag current)
  9249. (setq current (delete tag current))
  9250. (setq res t)
  9251. (push tag current))))
  9252. (end-of-line 1)
  9253. (if current
  9254. (progn
  9255. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9256. (org-set-tags nil t))
  9257. (delete-horizontal-space))
  9258. (run-hooks 'org-after-tags-change-hook))
  9259. res))
  9260. (defun org-align-tags-here (to-col)
  9261. ;; Assumes that this is a headline
  9262. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9263. (beginning-of-line 1)
  9264. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9265. (< pos (match-beginning 2)))
  9266. (progn
  9267. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9268. (goto-char (match-beginning 1))
  9269. (insert " ")
  9270. (delete-region (point) (1+ (match-beginning 2)))
  9271. (setq ncol (max (1+ (current-column))
  9272. (1+ col)
  9273. (if (> to-col 0)
  9274. to-col
  9275. (- (abs to-col) tags-l))))
  9276. (setq p (point))
  9277. (insert (make-string (- ncol (current-column)) ?\ ))
  9278. (setq ncol (current-column))
  9279. (when indent-tabs-mode (tabify p (point-at-eol)))
  9280. (org-move-to-column (min ncol col) t))
  9281. (goto-char pos))))
  9282. (defun org-set-tags-command (&optional arg just-align)
  9283. "Call the set-tags command for the current entry."
  9284. (interactive "P")
  9285. (if (org-on-heading-p)
  9286. (org-set-tags arg just-align)
  9287. (save-excursion
  9288. (org-back-to-heading t)
  9289. (org-set-tags arg just-align))))
  9290. (defun org-set-tags (&optional arg just-align)
  9291. "Set the tags for the current headline.
  9292. With prefix ARG, realign all tags in headings in the current buffer."
  9293. (interactive "P")
  9294. (let* ((re (concat "^" outline-regexp))
  9295. (current (org-get-tags-string))
  9296. (col (current-column))
  9297. (org-setting-tags t)
  9298. table current-tags inherited-tags ; computed below when needed
  9299. tags p0 c0 c1 rpl)
  9300. (if arg
  9301. (save-excursion
  9302. (goto-char (point-min))
  9303. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9304. (while (re-search-forward re nil t)
  9305. (org-set-tags nil t)
  9306. (end-of-line 1)))
  9307. (message "All tags realigned to column %d" org-tags-column))
  9308. (if just-align
  9309. (setq tags current)
  9310. ;; Get a new set of tags from the user
  9311. (save-excursion
  9312. (setq table (or org-tag-alist (org-get-buffer-tags))
  9313. org-last-tags-completion-table table
  9314. current-tags (org-split-string current ":")
  9315. inherited-tags (nreverse
  9316. (nthcdr (length current-tags)
  9317. (nreverse (org-get-tags-at))))
  9318. tags
  9319. (if (or (eq t org-use-fast-tag-selection)
  9320. (and org-use-fast-tag-selection
  9321. (delq nil (mapcar 'cdr table))))
  9322. (org-fast-tag-selection
  9323. current-tags inherited-tags table
  9324. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9325. (let ((org-add-colon-after-tag-completion t))
  9326. (org-trim
  9327. (org-without-partial-completion
  9328. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9329. nil nil current 'org-tags-history)))))))
  9330. (while (string-match "[-+&]+" tags)
  9331. ;; No boolean logic, just a list
  9332. (setq tags (replace-match ":" t t tags))))
  9333. (if (string-match "\\`[\t ]*\\'" tags)
  9334. (setq tags "")
  9335. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9336. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9337. ;; Insert new tags at the correct column
  9338. (beginning-of-line 1)
  9339. (cond
  9340. ((and (equal current "") (equal tags "")))
  9341. ((re-search-forward
  9342. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9343. (point-at-eol) t)
  9344. (if (equal tags "")
  9345. (setq rpl "")
  9346. (goto-char (match-beginning 0))
  9347. (setq c0 (current-column) p0 (point)
  9348. c1 (max (1+ c0) (if (> org-tags-column 0)
  9349. org-tags-column
  9350. (- (- org-tags-column) (length tags))))
  9351. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9352. (replace-match rpl t t)
  9353. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9354. tags)
  9355. (t (error "Tags alignment failed")))
  9356. (org-move-to-column col)
  9357. (unless just-align
  9358. (run-hooks 'org-after-tags-change-hook)))))
  9359. (defun org-change-tag-in-region (beg end tag off)
  9360. "Add or remove TAG for each entry in the region.
  9361. This works in the agenda, and also in an org-mode buffer."
  9362. (interactive
  9363. (list (region-beginning) (region-end)
  9364. (let ((org-last-tags-completion-table
  9365. (if (org-mode-p)
  9366. (org-get-buffer-tags)
  9367. (org-global-tags-completion-table))))
  9368. (org-ido-completing-read
  9369. "Tag: " 'org-tags-completion-function nil nil nil
  9370. 'org-tags-history))
  9371. (progn
  9372. (message "[s]et or [r]emove? ")
  9373. (equal (read-char-exclusive) ?r))))
  9374. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9375. (let ((agendap (equal major-mode 'org-agenda-mode))
  9376. l1 l2 m buf pos newhead (cnt 0))
  9377. (goto-char end)
  9378. (setq l2 (1- (org-current-line)))
  9379. (goto-char beg)
  9380. (setq l1 (org-current-line))
  9381. (loop for l from l1 to l2 do
  9382. (goto-line l)
  9383. (setq m (get-text-property (point) 'org-hd-marker))
  9384. (when (or (and (org-mode-p) (org-on-heading-p))
  9385. (and agendap m))
  9386. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9387. pos (if agendap m (point)))
  9388. (with-current-buffer buf
  9389. (save-excursion
  9390. (save-restriction
  9391. (goto-char pos)
  9392. (setq cnt (1+ cnt))
  9393. (org-toggle-tag tag (if off 'off 'on))
  9394. (setq newhead (org-get-heading)))))
  9395. (and agendap (org-agenda-change-all-lines newhead m))))
  9396. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9397. (defun org-tags-completion-function (string predicate &optional flag)
  9398. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9399. (confirm (lambda (x) (stringp (car x)))))
  9400. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9401. (setq s1 (match-string 1 string)
  9402. s2 (match-string 2 string))
  9403. (setq s1 "" s2 string))
  9404. (cond
  9405. ((eq flag nil)
  9406. ;; try completion
  9407. (setq rtn (try-completion s2 ctable confirm))
  9408. (if (stringp rtn)
  9409. (setq rtn
  9410. (concat s1 s2 (substring rtn (length s2))
  9411. (if (and org-add-colon-after-tag-completion
  9412. (assoc rtn ctable))
  9413. ":" ""))))
  9414. rtn)
  9415. ((eq flag t)
  9416. ;; all-completions
  9417. (all-completions s2 ctable confirm)
  9418. )
  9419. ((eq flag 'lambda)
  9420. ;; exact match?
  9421. (assoc s2 ctable)))
  9422. ))
  9423. (defun org-fast-tag-insert (kwd tags face &optional end)
  9424. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9425. (insert (format "%-12s" (concat kwd ":"))
  9426. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9427. (or end "")))
  9428. (defun org-fast-tag-show-exit (flag)
  9429. (save-excursion
  9430. (goto-line 3)
  9431. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9432. (replace-match ""))
  9433. (when flag
  9434. (end-of-line 1)
  9435. (org-move-to-column (- (window-width) 19) t)
  9436. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9437. (defun org-set-current-tags-overlay (current prefix)
  9438. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9439. (if (featurep 'xemacs)
  9440. (org-overlay-display org-tags-overlay (concat prefix s)
  9441. 'secondary-selection)
  9442. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9443. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9444. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9445. "Fast tag selection with single keys.
  9446. CURRENT is the current list of tags in the headline, INHERITED is the
  9447. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9448. possibly with grouping information. TODO-TABLE is a similar table with
  9449. TODO keywords, should these have keys assigned to them.
  9450. If the keys are nil, a-z are automatically assigned.
  9451. Returns the new tags string, or nil to not change the current settings."
  9452. (let* ((fulltable (append table todo-table))
  9453. (maxlen (apply 'max (mapcar
  9454. (lambda (x)
  9455. (if (stringp (car x)) (string-width (car x)) 0))
  9456. fulltable)))
  9457. (buf (current-buffer))
  9458. (expert (eq org-fast-tag-selection-single-key 'expert))
  9459. (buffer-tags nil)
  9460. (fwidth (+ maxlen 3 1 3))
  9461. (ncol (/ (- (window-width) 4) fwidth))
  9462. (i-face 'org-done)
  9463. (c-face 'org-todo)
  9464. tg cnt e c char c1 c2 ntable tbl rtn
  9465. ov-start ov-end ov-prefix
  9466. (exit-after-next org-fast-tag-selection-single-key)
  9467. (done-keywords org-done-keywords)
  9468. groups ingroup)
  9469. (save-excursion
  9470. (beginning-of-line 1)
  9471. (if (looking-at
  9472. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9473. (setq ov-start (match-beginning 1)
  9474. ov-end (match-end 1)
  9475. ov-prefix "")
  9476. (setq ov-start (1- (point-at-eol))
  9477. ov-end (1+ ov-start))
  9478. (skip-chars-forward "^\n\r")
  9479. (setq ov-prefix
  9480. (concat
  9481. (buffer-substring (1- (point)) (point))
  9482. (if (> (current-column) org-tags-column)
  9483. " "
  9484. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9485. (org-move-overlay org-tags-overlay ov-start ov-end)
  9486. (save-window-excursion
  9487. (if expert
  9488. (set-buffer (get-buffer-create " *Org tags*"))
  9489. (delete-other-windows)
  9490. (split-window-vertically)
  9491. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9492. (erase-buffer)
  9493. (org-set-local 'org-done-keywords done-keywords)
  9494. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9495. (org-fast-tag-insert "Current" current c-face "\n\n")
  9496. (org-fast-tag-show-exit exit-after-next)
  9497. (org-set-current-tags-overlay current ov-prefix)
  9498. (setq tbl fulltable char ?a cnt 0)
  9499. (while (setq e (pop tbl))
  9500. (cond
  9501. ((equal e '(:startgroup))
  9502. (push '() groups) (setq ingroup t)
  9503. (when (not (= cnt 0))
  9504. (setq cnt 0)
  9505. (insert "\n"))
  9506. (insert "{ "))
  9507. ((equal e '(:endgroup))
  9508. (setq ingroup nil cnt 0)
  9509. (insert "}\n"))
  9510. ((equal e '(:newline))
  9511. (insert "\n "))
  9512. (t
  9513. (setq tg (car e) c2 nil)
  9514. (if (cdr e)
  9515. (setq c (cdr e))
  9516. ;; automatically assign a character.
  9517. (setq c1 (string-to-char
  9518. (downcase (substring
  9519. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9520. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9521. (while (or (rassoc char ntable) (rassoc char table))
  9522. (setq char (1+ char)))
  9523. (setq c2 c1))
  9524. (setq c (or c2 char)))
  9525. (if ingroup (push tg (car groups)))
  9526. (setq tg (org-add-props tg nil 'face
  9527. (cond
  9528. ((not (assoc tg table))
  9529. (org-get-todo-face tg))
  9530. ((member tg current) c-face)
  9531. ((member tg inherited) i-face)
  9532. (t nil))))
  9533. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9534. (insert "[" c "] " tg (make-string
  9535. (- fwidth 4 (length tg)) ?\ ))
  9536. (push (cons tg c) ntable)
  9537. (when (= (setq cnt (1+ cnt)) ncol)
  9538. (insert "\n")
  9539. (if ingroup (insert " "))
  9540. (setq cnt 0)))))
  9541. (setq ntable (nreverse ntable))
  9542. (insert "\n")
  9543. (goto-char (point-min))
  9544. (if (not expert) (org-fit-window-to-buffer))
  9545. (setq rtn
  9546. (catch 'exit
  9547. (while t
  9548. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9549. (if groups " [!] no groups" " [!]groups")
  9550. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9551. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9552. (cond
  9553. ((= c ?\r) (throw 'exit t))
  9554. ((= c ?!)
  9555. (setq groups (not groups))
  9556. (goto-char (point-min))
  9557. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9558. ((= c ?\C-c)
  9559. (if (not expert)
  9560. (org-fast-tag-show-exit
  9561. (setq exit-after-next (not exit-after-next)))
  9562. (setq expert nil)
  9563. (delete-other-windows)
  9564. (split-window-vertically)
  9565. (org-switch-to-buffer-other-window " *Org tags*")
  9566. (org-fit-window-to-buffer)))
  9567. ((or (= c ?\C-g)
  9568. (and (= c ?q) (not (rassoc c ntable))))
  9569. (org-detach-overlay org-tags-overlay)
  9570. (setq quit-flag t))
  9571. ((= c ?\ )
  9572. (setq current nil)
  9573. (if exit-after-next (setq exit-after-next 'now)))
  9574. ((= c ?\t)
  9575. (condition-case nil
  9576. (setq tg (org-ido-completing-read
  9577. "Tag: "
  9578. (or buffer-tags
  9579. (with-current-buffer buf
  9580. (org-get-buffer-tags)))))
  9581. (quit (setq tg "")))
  9582. (when (string-match "\\S-" tg)
  9583. (add-to-list 'buffer-tags (list tg))
  9584. (if (member tg current)
  9585. (setq current (delete tg current))
  9586. (push tg current)))
  9587. (if exit-after-next (setq exit-after-next 'now)))
  9588. ((setq e (rassoc c todo-table) tg (car e))
  9589. (with-current-buffer buf
  9590. (save-excursion (org-todo tg)))
  9591. (if exit-after-next (setq exit-after-next 'now)))
  9592. ((setq e (rassoc c ntable) tg (car e))
  9593. (if (member tg current)
  9594. (setq current (delete tg current))
  9595. (loop for g in groups do
  9596. (if (member tg g)
  9597. (mapc (lambda (x)
  9598. (setq current (delete x current)))
  9599. g)))
  9600. (push tg current))
  9601. (if exit-after-next (setq exit-after-next 'now))))
  9602. ;; Create a sorted list
  9603. (setq current
  9604. (sort current
  9605. (lambda (a b)
  9606. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9607. (if (eq exit-after-next 'now) (throw 'exit t))
  9608. (goto-char (point-min))
  9609. (beginning-of-line 2)
  9610. (delete-region (point) (point-at-eol))
  9611. (org-fast-tag-insert "Current" current c-face)
  9612. (org-set-current-tags-overlay current ov-prefix)
  9613. (while (re-search-forward
  9614. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9615. (setq tg (match-string 1))
  9616. (add-text-properties
  9617. (match-beginning 1) (match-end 1)
  9618. (list 'face
  9619. (cond
  9620. ((member tg current) c-face)
  9621. ((member tg inherited) i-face)
  9622. (t (get-text-property (match-beginning 1) 'face))))))
  9623. (goto-char (point-min)))))
  9624. (org-detach-overlay org-tags-overlay)
  9625. (if rtn
  9626. (mapconcat 'identity current ":")
  9627. nil))))
  9628. (defun org-get-tags-string ()
  9629. "Get the TAGS string in the current headline."
  9630. (unless (org-on-heading-p t)
  9631. (error "Not on a heading"))
  9632. (save-excursion
  9633. (beginning-of-line 1)
  9634. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9635. (org-match-string-no-properties 1)
  9636. "")))
  9637. (defun org-get-tags ()
  9638. "Get the list of tags specified in the current headline."
  9639. (org-split-string (org-get-tags-string) ":"))
  9640. (defun org-get-buffer-tags ()
  9641. "Get a table of all tags used in the buffer, for completion."
  9642. (let (tags)
  9643. (save-excursion
  9644. (goto-char (point-min))
  9645. (while (re-search-forward
  9646. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9647. (when (equal (char-after (point-at-bol 0)) ?*)
  9648. (mapc (lambda (x) (add-to-list 'tags x))
  9649. (org-split-string (org-match-string-no-properties 1) ":")))))
  9650. (mapcar 'list tags)))
  9651. ;;;; The mapping API
  9652. ;;;###autoload
  9653. (defun org-map-entries (func &optional match scope &rest skip)
  9654. "Call FUNC at each headline selected by MATCH in SCOPE.
  9655. FUNC is a function or a lisp form. The function will be called without
  9656. arguments, with the cursor positioned at the beginning of the headline.
  9657. The return values of all calls to the function will be collected and
  9658. returned as a list.
  9659. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9660. Only headlines that are matched by this query will be considered during
  9661. the iteration. When MATCH is nil or t, all headlines will be
  9662. visited by the iteration.
  9663. SCOPE determines the scope of this command. It can be any of:
  9664. nil The current buffer, respecting the restriction if any
  9665. tree The subtree started with the entry at point
  9666. file The current buffer, without restriction
  9667. file-with-archives
  9668. The current buffer, and any archives associated with it
  9669. agenda All agenda files
  9670. agenda-with-archives
  9671. All agenda files with any archive files associated with them
  9672. \(file1 file2 ...)
  9673. If this is a list, all files in the list will be scanned
  9674. The remaining args are treated as settings for the skipping facilities of
  9675. the scanner. The following items can be given here:
  9676. archive skip trees with the archive tag.
  9677. comment skip trees with the COMMENT keyword
  9678. function or Emacs Lisp form:
  9679. will be used as value for `org-agenda-skip-function', so whenever
  9680. the the function returns t, FUNC will not be called for that
  9681. entry and search will continue from the point where the
  9682. function leaves it."
  9683. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9684. (org-agenda-skip-archived-trees (memq 'archive skip))
  9685. (org-agenda-skip-comment-trees (memq 'comment skip))
  9686. (org-agenda-skip-function
  9687. (car (org-delete-all '(comment archive) skip)))
  9688. (org-tags-match-list-sublevels t)
  9689. matcher file res
  9690. org-todo-keywords-for-agenda
  9691. org-done-keywords-for-agenda
  9692. org-todo-keyword-alist-for-agenda
  9693. org-tag-alist-for-agenda)
  9694. (cond
  9695. ((eq match t) (setq matcher t))
  9696. ((eq match nil) (setq matcher t))
  9697. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9698. (save-excursion
  9699. (save-restriction
  9700. (when (eq scope 'tree)
  9701. (org-back-to-heading t)
  9702. (org-narrow-to-subtree)
  9703. (setq scope nil))
  9704. (if (not scope)
  9705. (progn
  9706. (org-prepare-agenda-buffers
  9707. (list (buffer-file-name (current-buffer))))
  9708. (setq res (org-scan-tags func matcher)))
  9709. ;; Get the right scope
  9710. (cond
  9711. ((and scope (listp scope) (symbolp (car scope)))
  9712. (setq scope (eval scope)))
  9713. ((eq scope 'agenda)
  9714. (setq scope (org-agenda-files t)))
  9715. ((eq scope 'agenda-with-archives)
  9716. (setq scope (org-agenda-files t))
  9717. (setq scope (org-add-archive-files scope)))
  9718. ((eq scope 'file)
  9719. (setq scope (list (buffer-file-name))))
  9720. ((eq scope 'file-with-archives)
  9721. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9722. (org-prepare-agenda-buffers scope)
  9723. (while (setq file (pop scope))
  9724. (with-current-buffer (org-find-base-buffer-visiting file)
  9725. (save-excursion
  9726. (save-restriction
  9727. (widen)
  9728. (goto-char (point-min))
  9729. (setq res (append res (org-scan-tags func matcher))))))))))
  9730. res))
  9731. ;;;; Properties
  9732. ;;; Setting and retrieving properties
  9733. (defconst org-special-properties
  9734. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9735. "TIMESTAMP" "TIMESTAMP_IA")
  9736. "The special properties valid in Org-mode.
  9737. These are properties that are not defined in the property drawer,
  9738. but in some other way.")
  9739. (defconst org-default-properties
  9740. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9741. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9742. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9743. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9744. "Some properties that are used by Org-mode for various purposes.
  9745. Being in this list makes sure that they are offered for completion.")
  9746. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9747. "Regular expression matching the first line of a property drawer.")
  9748. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9749. "Regular expression matching the first line of a property drawer.")
  9750. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9751. "Regular expression matching the first line of a property drawer.")
  9752. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9753. "Regular expression matching the first line of a property drawer.")
  9754. (defconst org-property-drawer-re
  9755. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9756. org-property-end-re "\\)\n?")
  9757. "Matches an entire property drawer.")
  9758. (defconst org-clock-drawer-re
  9759. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9760. org-property-end-re "\\)\n?")
  9761. "Matches an entire clock drawer.")
  9762. (defun org-property-action ()
  9763. "Do an action on properties."
  9764. (interactive)
  9765. (let (c)
  9766. (org-at-property-p)
  9767. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9768. (setq c (read-char-exclusive))
  9769. (cond
  9770. ((equal c ?s)
  9771. (call-interactively 'org-set-property))
  9772. ((equal c ?d)
  9773. (call-interactively 'org-delete-property))
  9774. ((equal c ?D)
  9775. (call-interactively 'org-delete-property-globally))
  9776. ((equal c ?c)
  9777. (call-interactively 'org-compute-property-at-point))
  9778. (t (error "No such property action %c" c)))))
  9779. (defun org-at-property-p ()
  9780. "Is the cursor in a property line?"
  9781. ;; FIXME: Does not check if we are actually in the drawer.
  9782. ;; FIXME: also returns true on any drawers.....
  9783. ;; This is used by C-c C-c for property action.
  9784. (save-excursion
  9785. (beginning-of-line 1)
  9786. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9787. (defun org-get-property-block (&optional beg end force)
  9788. "Return the (beg . end) range of the body of the property drawer.
  9789. BEG and END can be beginning and end of subtree, if not given
  9790. they will be found.
  9791. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9792. (catch 'exit
  9793. (save-excursion
  9794. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9795. (end (or end (progn (outline-next-heading) (point)))))
  9796. (goto-char beg)
  9797. (if (re-search-forward org-property-start-re end t)
  9798. (setq beg (1+ (match-end 0)))
  9799. (if force
  9800. (save-excursion
  9801. (org-insert-property-drawer)
  9802. (setq end (progn (outline-next-heading) (point))))
  9803. (throw 'exit nil))
  9804. (goto-char beg)
  9805. (if (re-search-forward org-property-start-re end t)
  9806. (setq beg (1+ (match-end 0)))))
  9807. (if (re-search-forward org-property-end-re end t)
  9808. (setq end (match-beginning 0))
  9809. (or force (throw 'exit nil))
  9810. (goto-char beg)
  9811. (setq end beg)
  9812. (org-indent-line-function)
  9813. (insert ":END:\n"))
  9814. (cons beg end)))))
  9815. (defun org-entry-properties (&optional pom which)
  9816. "Get all properties of the entry at point-or-marker POM.
  9817. This includes the TODO keyword, the tags, time strings for deadline,
  9818. scheduled, and clocking, and any additional properties defined in the
  9819. entry. The return value is an alist, keys may occur multiple times
  9820. if the property key was used several times.
  9821. POM may also be nil, in which case the current entry is used.
  9822. If WHICH is nil or `all', get all properties. If WHICH is
  9823. `special' or `standard', only get that subclass."
  9824. (setq which (or which 'all))
  9825. (org-with-point-at pom
  9826. (let ((clockstr (substring org-clock-string 0 -1))
  9827. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9828. beg end range props sum-props key value string clocksum)
  9829. (save-excursion
  9830. (when (condition-case nil
  9831. (and (org-mode-p) (org-back-to-heading t))
  9832. (error nil))
  9833. (setq beg (point))
  9834. (setq sum-props (get-text-property (point) 'org-summaries))
  9835. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9836. (outline-next-heading)
  9837. (setq end (point))
  9838. (when (memq which '(all special))
  9839. ;; Get the special properties, like TODO and tags
  9840. (goto-char beg)
  9841. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9842. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9843. (when (looking-at org-priority-regexp)
  9844. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9845. (when (and (setq value (org-get-tags-string))
  9846. (string-match "\\S-" value))
  9847. (push (cons "TAGS" value) props))
  9848. (when (setq value (org-get-tags-at))
  9849. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9850. props))
  9851. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9852. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9853. string (if (equal key clockstr)
  9854. (org-no-properties
  9855. (org-trim
  9856. (buffer-substring
  9857. (match-beginning 3) (goto-char (point-at-eol)))))
  9858. (substring (org-match-string-no-properties 3) 1 -1)))
  9859. (unless key
  9860. (if (= (char-after (match-beginning 3)) ?\[)
  9861. (setq key "TIMESTAMP_IA")
  9862. (setq key "TIMESTAMP")))
  9863. (when (or (equal key clockstr) (not (assoc key props)))
  9864. (push (cons key string) props)))
  9865. )
  9866. (when (memq which '(all standard))
  9867. ;; Get the standard properties, like :PORP: ...
  9868. (setq range (org-get-property-block beg end))
  9869. (when range
  9870. (goto-char (car range))
  9871. (while (re-search-forward
  9872. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9873. (cdr range) t)
  9874. (setq key (org-match-string-no-properties 1)
  9875. value (org-trim (or (org-match-string-no-properties 2) "")))
  9876. (unless (member key excluded)
  9877. (push (cons key (or value "")) props)))))
  9878. (if clocksum
  9879. (push (cons "CLOCKSUM"
  9880. (org-columns-number-to-string (/ (float clocksum) 60.)
  9881. 'add_times))
  9882. props))
  9883. (unless (assoc "CATEGORY" props)
  9884. (setq value (or (org-get-category)
  9885. (progn (org-refresh-category-properties)
  9886. (org-get-category))))
  9887. (push (cons "CATEGORY" value) props))
  9888. (append sum-props (nreverse props)))))))
  9889. (defun org-entry-get (pom property &optional inherit)
  9890. "Get value of PROPERTY for entry at point-or-marker POM.
  9891. If INHERIT is non-nil and the entry does not have the property,
  9892. then also check higher levels of the hierarchy.
  9893. If INHERIT is the symbol `selective', use inheritance only if the setting
  9894. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9895. If the property is present but empty, the return value is the empty string.
  9896. If the property is not present at all, nil is returned."
  9897. (org-with-point-at pom
  9898. (if (and inherit (if (eq inherit 'selective)
  9899. (org-property-inherit-p property)
  9900. t))
  9901. (org-entry-get-with-inheritance property)
  9902. (if (member property org-special-properties)
  9903. ;; We need a special property. Use brute force, get all properties.
  9904. (cdr (assoc property (org-entry-properties nil 'special)))
  9905. (let ((range (org-get-property-block)))
  9906. (if (and range
  9907. (goto-char (car range))
  9908. (re-search-forward
  9909. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9910. (cdr range) t))
  9911. ;; Found the property, return it.
  9912. (if (match-end 1)
  9913. (org-match-string-no-properties 1)
  9914. "")))))))
  9915. (defun org-property-or-variable-value (var &optional inherit)
  9916. "Check if there is a property fixing the value of VAR.
  9917. If yes, return this value. If not, return the current value of the variable."
  9918. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9919. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9920. (read prop)
  9921. (symbol-value var))))
  9922. (defun org-entry-delete (pom property)
  9923. "Delete the property PROPERTY from entry at point-or-marker POM."
  9924. (org-with-point-at pom
  9925. (if (member property org-special-properties)
  9926. nil ; cannot delete these properties.
  9927. (let ((range (org-get-property-block)))
  9928. (if (and range
  9929. (goto-char (car range))
  9930. (re-search-forward
  9931. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9932. (cdr range) t))
  9933. (progn
  9934. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9935. t)
  9936. nil)))))
  9937. ;; Multi-values properties are properties that contain multiple values
  9938. ;; These values are assumed to be single words, separated by whitespace.
  9939. (defun org-entry-add-to-multivalued-property (pom property value)
  9940. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9941. (let* ((old (org-entry-get pom property))
  9942. (values (and old (org-split-string old "[ \t]"))))
  9943. (setq value (org-entry-protect-space value))
  9944. (unless (member value values)
  9945. (setq values (cons value values))
  9946. (org-entry-put pom property
  9947. (mapconcat 'identity values " ")))))
  9948. (defun org-entry-remove-from-multivalued-property (pom property value)
  9949. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9950. (let* ((old (org-entry-get pom property))
  9951. (values (and old (org-split-string old "[ \t]"))))
  9952. (setq value (org-entry-protect-space value))
  9953. (when (member value values)
  9954. (setq values (delete value values))
  9955. (org-entry-put pom property
  9956. (mapconcat 'identity values " ")))))
  9957. (defun org-entry-member-in-multivalued-property (pom property value)
  9958. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9959. (let* ((old (org-entry-get pom property))
  9960. (values (and old (org-split-string old "[ \t]"))))
  9961. (setq value (org-entry-protect-space value))
  9962. (member value values)))
  9963. (defun org-entry-get-multivalued-property (pom property)
  9964. "Return a list of values in a multivalued property."
  9965. (let* ((value (org-entry-get pom property))
  9966. (values (and value (org-split-string value "[ \t]"))))
  9967. (mapcar 'org-entry-restore-space values)))
  9968. (defun org-entry-put-multivalued-property (pom property &rest values)
  9969. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9970. VALUES should be a list of strings. Spaces will be protected."
  9971. (org-entry-put pom property
  9972. (mapconcat 'org-entry-protect-space values " "))
  9973. (let* ((value (org-entry-get pom property))
  9974. (values (and value (org-split-string value "[ \t]"))))
  9975. (mapcar 'org-entry-restore-space values)))
  9976. (defun org-entry-protect-space (s)
  9977. "Protect spaces and newline in string S."
  9978. (while (string-match " " s)
  9979. (setq s (replace-match "%20" t t s)))
  9980. (while (string-match "\n" s)
  9981. (setq s (replace-match "%0A" t t s)))
  9982. s)
  9983. (defun org-entry-restore-space (s)
  9984. "Restore spaces and newline in string S."
  9985. (while (string-match "%20" s)
  9986. (setq s (replace-match " " t t s)))
  9987. (while (string-match "%0A" s)
  9988. (setq s (replace-match "\n" t t s)))
  9989. s)
  9990. (defvar org-entry-property-inherited-from (make-marker)
  9991. "Marker pointing to the entry from where a property was inherited.
  9992. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9993. location of the entry where the inheritance search matched. If there was
  9994. no match, the marker will point nowhere.
  9995. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9996. is set.")
  9997. (defun org-entry-get-with-inheritance (property)
  9998. "Get entry property, and search higher levels if not present."
  9999. (move-marker org-entry-property-inherited-from nil)
  10000. (let (tmp)
  10001. (save-excursion
  10002. (save-restriction
  10003. (widen)
  10004. (catch 'ex
  10005. (while t
  10006. (when (setq tmp (org-entry-get nil property))
  10007. (org-back-to-heading t)
  10008. (move-marker org-entry-property-inherited-from (point))
  10009. (throw 'ex tmp))
  10010. (or (org-up-heading-safe) (throw 'ex nil)))))
  10011. (or tmp
  10012. (cdr (assoc property org-file-properties))
  10013. (cdr (assoc property org-global-properties))
  10014. (cdr (assoc property org-global-properties-fixed))))))
  10015. (defun org-entry-put (pom property value)
  10016. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10017. (org-with-point-at pom
  10018. (org-back-to-heading t)
  10019. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10020. range)
  10021. (cond
  10022. ((equal property "TODO")
  10023. (when (and (stringp value) (string-match "\\S-" value)
  10024. (not (member value org-todo-keywords-1)))
  10025. (error "\"%s\" is not a valid TODO state" value))
  10026. (if (or (not value)
  10027. (not (string-match "\\S-" value)))
  10028. (setq value 'none))
  10029. (org-todo value)
  10030. (org-set-tags nil 'align))
  10031. ((equal property "PRIORITY")
  10032. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10033. (string-to-char value) ?\ ))
  10034. (org-set-tags nil 'align))
  10035. ((equal property "SCHEDULED")
  10036. (if (re-search-forward org-scheduled-time-regexp end t)
  10037. (cond
  10038. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10039. ((eq value 'later) (org-timestamp-change 1 'day))
  10040. (t (call-interactively 'org-schedule)))
  10041. (call-interactively 'org-schedule)))
  10042. ((equal property "DEADLINE")
  10043. (if (re-search-forward org-deadline-time-regexp end t)
  10044. (cond
  10045. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10046. ((eq value 'later) (org-timestamp-change 1 'day))
  10047. (t (call-interactively 'org-deadline)))
  10048. (call-interactively 'org-deadline)))
  10049. ((member property org-special-properties)
  10050. (error "The %s property can not yet be set with `org-entry-put'"
  10051. property))
  10052. (t ; a non-special property
  10053. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10054. (setq range (org-get-property-block beg end 'force))
  10055. (goto-char (car range))
  10056. (if (re-search-forward
  10057. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10058. (progn
  10059. (delete-region (match-beginning 1) (match-end 1))
  10060. (goto-char (match-beginning 1)))
  10061. (goto-char (cdr range))
  10062. (insert "\n")
  10063. (backward-char 1)
  10064. (org-indent-line-function)
  10065. (insert ":" property ":"))
  10066. (and value (insert " " value))
  10067. (org-indent-line-function)))))))
  10068. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10069. "Get all property keys in the current buffer.
  10070. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10071. like tags and TODO state.
  10072. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10073. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10074. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10075. formats in the current buffer."
  10076. (let (rtn range cfmt s p)
  10077. (save-excursion
  10078. (save-restriction
  10079. (widen)
  10080. (goto-char (point-min))
  10081. (while (re-search-forward org-property-start-re nil t)
  10082. (setq range (org-get-property-block))
  10083. (goto-char (car range))
  10084. (while (re-search-forward
  10085. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10086. (cdr range) t)
  10087. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10088. (outline-next-heading))))
  10089. (when include-specials
  10090. (setq rtn (append org-special-properties rtn)))
  10091. (when include-defaults
  10092. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10093. (when include-columns
  10094. (save-excursion
  10095. (save-restriction
  10096. (widen)
  10097. (goto-char (point-min))
  10098. (while (re-search-forward
  10099. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10100. nil t)
  10101. (setq cfmt (match-string 2) s 0)
  10102. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10103. cfmt s)
  10104. (setq s (match-end 0)
  10105. p (match-string 1 cfmt))
  10106. (unless (or (equal p "ITEM")
  10107. (member p org-special-properties))
  10108. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10109. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10110. (defun org-property-values (key)
  10111. "Return a list of all values of property KEY."
  10112. (save-excursion
  10113. (save-restriction
  10114. (widen)
  10115. (goto-char (point-min))
  10116. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10117. values)
  10118. (while (re-search-forward re nil t)
  10119. (add-to-list 'values (org-trim (match-string 1))))
  10120. (delete "" values)))))
  10121. (defun org-insert-property-drawer ()
  10122. "Insert a property drawer into the current entry."
  10123. (interactive)
  10124. (org-back-to-heading t)
  10125. (looking-at outline-regexp)
  10126. (let ((indent (- (match-end 0)(match-beginning 0)))
  10127. (beg (point))
  10128. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10129. end hiddenp)
  10130. (outline-next-heading)
  10131. (setq end (point))
  10132. (goto-char beg)
  10133. (while (re-search-forward re end t))
  10134. (setq hiddenp (org-invisible-p))
  10135. (end-of-line 1)
  10136. (and (equal (char-after) ?\n) (forward-char 1))
  10137. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10138. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10139. ;; just skip this line
  10140. (beginning-of-line 2)
  10141. ;; Drawer start, find the end
  10142. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10143. (beginning-of-line 1)))
  10144. (org-skip-over-state-notes)
  10145. (skip-chars-backward " \t\n\r")
  10146. (if (eq (char-before) ?*) (forward-char 1))
  10147. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10148. (beginning-of-line 0)
  10149. (org-indent-to-column indent)
  10150. (beginning-of-line 2)
  10151. (org-indent-to-column indent)
  10152. (beginning-of-line 0)
  10153. (if hiddenp
  10154. (save-excursion
  10155. (org-back-to-heading t)
  10156. (hide-entry))
  10157. (org-flag-drawer t))))
  10158. (defun org-set-property (property value)
  10159. "In the current entry, set PROPERTY to VALUE.
  10160. When called interactively, this will prompt for a property name, offering
  10161. completion on existing and default properties. And then it will prompt
  10162. for a value, offering completion either on allowed values (via an inherited
  10163. xxx_ALL property) or on existing values in other instances of this property
  10164. in the current file."
  10165. (interactive
  10166. (let* ((completion-ignore-case t)
  10167. (keys (org-buffer-property-keys nil t t))
  10168. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10169. (prop (if (member prop0 keys)
  10170. prop0
  10171. (or (cdr (assoc (downcase prop0)
  10172. (mapcar (lambda (x) (cons (downcase x) x))
  10173. keys)))
  10174. prop0)))
  10175. (cur (org-entry-get nil prop))
  10176. (allowed (org-property-get-allowed-values nil prop 'table))
  10177. (existing (mapcar 'list (org-property-values prop)))
  10178. (val (if allowed
  10179. (org-completing-read "Value: " allowed nil 'req-match)
  10180. (let (org-completion-use-ido)
  10181. (org-completing-read
  10182. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10183. (concat "[" cur "]") "")
  10184. ": ")
  10185. existing nil nil "" nil cur)))))
  10186. (list prop (if (equal val "") cur val))))
  10187. (unless (equal (org-entry-get nil property) value)
  10188. (org-entry-put nil property value)))
  10189. (defun org-delete-property (property)
  10190. "In the current entry, delete PROPERTY."
  10191. (interactive
  10192. (let* ((completion-ignore-case t)
  10193. (prop (org-ido-completing-read
  10194. "Property: " (org-entry-properties nil 'standard))))
  10195. (list prop)))
  10196. (message "Property %s %s" property
  10197. (if (org-entry-delete nil property)
  10198. "deleted"
  10199. "was not present in the entry")))
  10200. (defun org-delete-property-globally (property)
  10201. "Remove PROPERTY globally, from all entries."
  10202. (interactive
  10203. (let* ((completion-ignore-case t)
  10204. (prop (org-ido-completing-read
  10205. "Globally remove property: "
  10206. (mapcar 'list (org-buffer-property-keys)))))
  10207. (list prop)))
  10208. (save-excursion
  10209. (save-restriction
  10210. (widen)
  10211. (goto-char (point-min))
  10212. (let ((cnt 0))
  10213. (while (re-search-forward
  10214. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10215. nil t)
  10216. (setq cnt (1+ cnt))
  10217. (replace-match ""))
  10218. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10219. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10220. (defun org-compute-property-at-point ()
  10221. "Compute the property at point.
  10222. This looks for an enclosing column format, extracts the operator and
  10223. then applies it to the property in the column format's scope."
  10224. (interactive)
  10225. (unless (org-at-property-p)
  10226. (error "Not at a property"))
  10227. (let ((prop (org-match-string-no-properties 2)))
  10228. (org-columns-get-format-and-top-level)
  10229. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10230. (error "No operator defined for property %s" prop))
  10231. (org-columns-compute prop)))
  10232. (defun org-property-get-allowed-values (pom property &optional table)
  10233. "Get allowed values for the property PROPERTY.
  10234. When TABLE is non-nil, return an alist that can directly be used for
  10235. completion."
  10236. (let (vals)
  10237. (cond
  10238. ((equal property "TODO")
  10239. (setq vals (org-with-point-at pom
  10240. (append org-todo-keywords-1 '("")))))
  10241. ((equal property "PRIORITY")
  10242. (let ((n org-lowest-priority))
  10243. (while (>= n org-highest-priority)
  10244. (push (char-to-string n) vals)
  10245. (setq n (1- n)))))
  10246. ((member property org-special-properties))
  10247. (t
  10248. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10249. (when (and vals (string-match "\\S-" vals))
  10250. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10251. (setq vals (mapcar (lambda (x)
  10252. (cond ((stringp x) x)
  10253. ((numberp x) (number-to-string x))
  10254. ((symbolp x) (symbol-name x))
  10255. (t "???")))
  10256. vals)))))
  10257. (if table (mapcar 'list vals) vals)))
  10258. (defun org-property-previous-allowed-value (&optional previous)
  10259. "Switch to the next allowed value for this property."
  10260. (interactive)
  10261. (org-property-next-allowed-value t))
  10262. (defun org-property-next-allowed-value (&optional previous)
  10263. "Switch to the next allowed value for this property."
  10264. (interactive)
  10265. (unless (org-at-property-p)
  10266. (error "Not at a property"))
  10267. (let* ((key (match-string 2))
  10268. (value (match-string 3))
  10269. (allowed (or (org-property-get-allowed-values (point) key)
  10270. (and (member value '("[ ]" "[-]" "[X]"))
  10271. '("[ ]" "[X]"))))
  10272. nval)
  10273. (unless allowed
  10274. (error "Allowed values for this property have not been defined"))
  10275. (if previous (setq allowed (reverse allowed)))
  10276. (if (member value allowed)
  10277. (setq nval (car (cdr (member value allowed)))))
  10278. (setq nval (or nval (car allowed)))
  10279. (if (equal nval value)
  10280. (error "Only one allowed value for this property"))
  10281. (org-at-property-p)
  10282. (replace-match (concat " :" key ": " nval) t t)
  10283. (org-indent-line-function)
  10284. (beginning-of-line 1)
  10285. (skip-chars-forward " \t")))
  10286. (defun org-find-entry-with-id (ident)
  10287. "Locate the entry that contains the ID property with exact value IDENT.
  10288. IDENT can be a string, a symbol or a number, this function will search for
  10289. the string representation of it.
  10290. Return the position where this entry starts, or nil if there is no such entry."
  10291. (interactive "sID: ")
  10292. (let ((id (cond
  10293. ((stringp ident) ident)
  10294. ((symbol-name ident) (symbol-name ident))
  10295. ((numberp ident) (number-to-string ident))
  10296. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10297. (case-fold-search nil))
  10298. (save-excursion
  10299. (save-restriction
  10300. (widen)
  10301. (goto-char (point-min))
  10302. (when (re-search-forward
  10303. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10304. nil t)
  10305. (org-back-to-heading)
  10306. (point))))))
  10307. ;;;; Timestamps
  10308. (defvar org-last-changed-timestamp nil)
  10309. (defvar org-last-inserted-timestamp nil
  10310. "The last time stamp inserted with `org-insert-time-stamp'.")
  10311. (defvar org-time-was-given) ; dynamically scoped parameter
  10312. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10313. (defvar org-ts-what) ; dynamically scoped parameter
  10314. (defun org-time-stamp (arg &optional inactive)
  10315. "Prompt for a date/time and insert a time stamp.
  10316. If the user specifies a time like HH:MM, or if this command is called
  10317. with a prefix argument, the time stamp will contain date and time.
  10318. Otherwise, only the date will be included. All parts of a date not
  10319. specified by the user will be filled in from the current date/time.
  10320. So if you press just return without typing anything, the time stamp
  10321. will represent the current date/time. If there is already a timestamp
  10322. at the cursor, it will be modified."
  10323. (interactive "P")
  10324. (let* ((ts nil)
  10325. (default-time
  10326. ;; Default time is either today, or, when entering a range,
  10327. ;; the range start.
  10328. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10329. (save-excursion
  10330. (re-search-backward
  10331. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10332. (- (point) 20) t)))
  10333. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10334. (current-time)))
  10335. (default-input (and ts (org-get-compact-tod ts)))
  10336. org-time-was-given org-end-time-was-given time)
  10337. (cond
  10338. ((and (org-at-timestamp-p t)
  10339. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10340. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10341. (insert "--")
  10342. (setq time (let ((this-command this-command))
  10343. (org-read-date arg 'totime nil nil
  10344. default-time default-input)))
  10345. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10346. ((org-at-timestamp-p t)
  10347. (setq time (let ((this-command this-command))
  10348. (org-read-date arg 'totime nil nil default-time default-input)))
  10349. (when (org-at-timestamp-p t) ; just to get the match data
  10350. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10351. (replace-match "")
  10352. (setq org-last-changed-timestamp
  10353. (org-insert-time-stamp
  10354. time (or org-time-was-given arg)
  10355. inactive nil nil (list org-end-time-was-given))))
  10356. (message "Timestamp updated"))
  10357. (t
  10358. (setq time (let ((this-command this-command))
  10359. (org-read-date arg 'totime nil nil default-time default-input)))
  10360. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10361. nil nil (list org-end-time-was-given))))))
  10362. ;; FIXME: can we use this for something else, like computing time differences?
  10363. (defun org-get-compact-tod (s)
  10364. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10365. (let* ((t1 (match-string 1 s))
  10366. (h1 (string-to-number (match-string 2 s)))
  10367. (m1 (string-to-number (match-string 3 s)))
  10368. (t2 (and (match-end 4) (match-string 5 s)))
  10369. (h2 (and t2 (string-to-number (match-string 6 s))))
  10370. (m2 (and t2 (string-to-number (match-string 7 s))))
  10371. dh dm)
  10372. (if (not t2)
  10373. t1
  10374. (setq dh (- h2 h1) dm (- m2 m1))
  10375. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10376. (concat t1 "+" (number-to-string dh)
  10377. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10378. (defun org-time-stamp-inactive (&optional arg)
  10379. "Insert an inactive time stamp.
  10380. An inactive time stamp is enclosed in square brackets instead of angle
  10381. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10382. does not link to the calendar and cannot be changed with the S-cursor keys.
  10383. So these are more for recording a certain time/date."
  10384. (interactive "P")
  10385. (org-time-stamp arg 'inactive))
  10386. (defvar org-date-ovl (org-make-overlay 1 1))
  10387. (org-overlay-put org-date-ovl 'face 'org-warning)
  10388. (org-detach-overlay org-date-ovl)
  10389. (defvar org-ans1) ; dynamically scoped parameter
  10390. (defvar org-ans2) ; dynamically scoped parameter
  10391. (defvar org-plain-time-of-day-regexp) ; defined below
  10392. (defvar org-overriding-default-time nil) ; dynamically scoped
  10393. (defvar org-read-date-overlay nil)
  10394. (defvar org-dcst nil) ; dynamically scoped
  10395. (defun org-read-date (&optional with-time to-time from-string prompt
  10396. default-time default-input)
  10397. "Read a date, possibly a time, and make things smooth for the user.
  10398. The prompt will suggest to enter an ISO date, but you can also enter anything
  10399. which will at least partially be understood by `parse-time-string'.
  10400. Unrecognized parts of the date will default to the current day, month, year,
  10401. hour and minute. If this command is called to replace a timestamp at point,
  10402. of to enter the second timestamp of a range, the default time is taken from the
  10403. existing stamp. For example,
  10404. 3-2-5 --> 2003-02-05
  10405. feb 15 --> currentyear-02-15
  10406. sep 12 9 --> 2009-09-12
  10407. 12:45 --> today 12:45
  10408. 22 sept 0:34 --> currentyear-09-22 0:34
  10409. 12 --> currentyear-currentmonth-12
  10410. Fri --> nearest Friday (today or later)
  10411. etc.
  10412. Furthermore you can specify a relative date by giving, as the *first* thing
  10413. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10414. change in days weeks, months, years.
  10415. With a single plus or minus, the date is relative to today. With a double
  10416. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10417. +4d --> four days from today
  10418. +4 --> same as above
  10419. +2w --> two weeks from today
  10420. ++5 --> five days from default date
  10421. The function understands only English month and weekday abbreviations,
  10422. but this can be configured with the variables `parse-time-months' and
  10423. `parse-time-weekdays'.
  10424. While prompting, a calendar is popped up - you can also select the
  10425. date with the mouse (button 1). The calendar shows a period of three
  10426. months. To scroll it to other months, use the keys `>' and `<'.
  10427. If you don't like the calendar, turn it off with
  10428. \(setq org-read-date-popup-calendar nil)
  10429. With optional argument TO-TIME, the date will immediately be converted
  10430. to an internal time.
  10431. With an optional argument WITH-TIME, the prompt will suggest to also
  10432. insert a time. Note that when WITH-TIME is not set, you can still
  10433. enter a time, and this function will inform the calling routine about
  10434. this change. The calling routine may then choose to change the format
  10435. used to insert the time stamp into the buffer to include the time.
  10436. With optional argument FROM-STRING, read from this string instead from
  10437. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10438. the time/date that is used for everything that is not specified by the
  10439. user."
  10440. (require 'parse-time)
  10441. (let* ((org-time-stamp-rounding-minutes
  10442. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10443. (org-dcst org-display-custom-times)
  10444. (ct (org-current-time))
  10445. (def (or org-overriding-default-time default-time ct))
  10446. (defdecode (decode-time def))
  10447. (dummy (progn
  10448. (when (< (nth 2 defdecode) org-extend-today-until)
  10449. (setcar (nthcdr 2 defdecode) -1)
  10450. (setcar (nthcdr 1 defdecode) 59)
  10451. (setq def (apply 'encode-time defdecode)
  10452. defdecode (decode-time def)))))
  10453. (calendar-move-hook nil)
  10454. (calendar-view-diary-initially-flag nil)
  10455. (view-diary-entries-initially nil)
  10456. (calendar-view-holidays-initially-flag nil)
  10457. (view-calendar-holidays-initially nil)
  10458. (timestr (format-time-string
  10459. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10460. (prompt (concat (if prompt (concat prompt " ") "")
  10461. (format "Date+time [%s]: " timestr)))
  10462. ans (org-ans0 "") org-ans1 org-ans2 final)
  10463. (cond
  10464. (from-string (setq ans from-string))
  10465. (org-read-date-popup-calendar
  10466. (save-excursion
  10467. (save-window-excursion
  10468. (calendar)
  10469. (calendar-forward-day (- (time-to-days def)
  10470. (calendar-absolute-from-gregorian
  10471. (calendar-current-date))))
  10472. (org-eval-in-calendar nil t)
  10473. (let* ((old-map (current-local-map))
  10474. (map (copy-keymap calendar-mode-map))
  10475. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10476. (org-defkey map (kbd "RET") 'org-calendar-select)
  10477. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10478. 'org-calendar-select-mouse)
  10479. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10480. 'org-calendar-select-mouse)
  10481. (org-defkey minibuffer-local-map [(meta shift left)]
  10482. (lambda () (interactive)
  10483. (org-eval-in-calendar '(calendar-backward-month 1))))
  10484. (org-defkey minibuffer-local-map [(meta shift right)]
  10485. (lambda () (interactive)
  10486. (org-eval-in-calendar '(calendar-forward-month 1))))
  10487. (org-defkey minibuffer-local-map [(meta shift up)]
  10488. (lambda () (interactive)
  10489. (org-eval-in-calendar '(calendar-backward-year 1))))
  10490. (org-defkey minibuffer-local-map [(meta shift down)]
  10491. (lambda () (interactive)
  10492. (org-eval-in-calendar '(calendar-forward-year 1))))
  10493. (org-defkey minibuffer-local-map [(shift up)]
  10494. (lambda () (interactive)
  10495. (org-eval-in-calendar '(calendar-backward-week 1))))
  10496. (org-defkey minibuffer-local-map [(shift down)]
  10497. (lambda () (interactive)
  10498. (org-eval-in-calendar '(calendar-forward-week 1))))
  10499. (org-defkey minibuffer-local-map [(shift left)]
  10500. (lambda () (interactive)
  10501. (org-eval-in-calendar '(calendar-backward-day 1))))
  10502. (org-defkey minibuffer-local-map [(shift right)]
  10503. (lambda () (interactive)
  10504. (org-eval-in-calendar '(calendar-forward-day 1))))
  10505. (org-defkey minibuffer-local-map ">"
  10506. (lambda () (interactive)
  10507. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10508. (org-defkey minibuffer-local-map "<"
  10509. (lambda () (interactive)
  10510. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10511. (unwind-protect
  10512. (progn
  10513. (use-local-map map)
  10514. (add-hook 'post-command-hook 'org-read-date-display)
  10515. (setq org-ans0 (read-string prompt default-input nil nil))
  10516. ;; org-ans0: from prompt
  10517. ;; org-ans1: from mouse click
  10518. ;; org-ans2: from calendar motion
  10519. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10520. (remove-hook 'post-command-hook 'org-read-date-display)
  10521. (use-local-map old-map)
  10522. (when org-read-date-overlay
  10523. (org-delete-overlay org-read-date-overlay)
  10524. (setq org-read-date-overlay nil)))))))
  10525. (t ; Naked prompt only
  10526. (unwind-protect
  10527. (setq ans (read-string prompt default-input nil timestr))
  10528. (when org-read-date-overlay
  10529. (org-delete-overlay org-read-date-overlay)
  10530. (setq org-read-date-overlay nil)))))
  10531. (setq final (org-read-date-analyze ans def defdecode))
  10532. (if to-time
  10533. (apply 'encode-time final)
  10534. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10535. (format "%04d-%02d-%02d %02d:%02d"
  10536. (nth 5 final) (nth 4 final) (nth 3 final)
  10537. (nth 2 final) (nth 1 final))
  10538. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10539. (defvar def)
  10540. (defvar defdecode)
  10541. (defvar with-time)
  10542. (defun org-read-date-display ()
  10543. "Display the current date prompt interpretation in the minibuffer."
  10544. (when org-read-date-display-live
  10545. (when org-read-date-overlay
  10546. (org-delete-overlay org-read-date-overlay))
  10547. (let ((p (point)))
  10548. (end-of-line 1)
  10549. (while (not (equal (buffer-substring
  10550. (max (point-min) (- (point) 4)) (point))
  10551. " "))
  10552. (insert " "))
  10553. (goto-char p))
  10554. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10555. " " (or org-ans1 org-ans2)))
  10556. (org-end-time-was-given nil)
  10557. (f (org-read-date-analyze ans def defdecode))
  10558. (fmts (if org-dcst
  10559. org-time-stamp-custom-formats
  10560. org-time-stamp-formats))
  10561. (fmt (if (or with-time
  10562. (and (boundp 'org-time-was-given) org-time-was-given))
  10563. (cdr fmts)
  10564. (car fmts)))
  10565. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10566. (when (and org-end-time-was-given
  10567. (string-match org-plain-time-of-day-regexp txt))
  10568. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10569. org-end-time-was-given
  10570. (substring txt (match-end 0)))))
  10571. (setq org-read-date-overlay
  10572. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10573. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10574. (defun org-read-date-analyze (ans def defdecode)
  10575. "Analyse the combined answer of the date prompt."
  10576. ;; FIXME: cleanup and comment
  10577. (let (delta deltan deltaw deltadef year month day
  10578. hour minute second wday pm h2 m2 tl wday1
  10579. iso-year iso-weekday iso-week iso-year iso-date)
  10580. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10581. (setq ans "+0"))
  10582. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10583. (setq ans (replace-match "" t t ans)
  10584. deltan (car delta)
  10585. deltaw (nth 1 delta)
  10586. deltadef (nth 2 delta)))
  10587. ;; Check if there is an iso week date in there
  10588. ;; If yes, sore the info and postpone interpreting it until the rest
  10589. ;; of the parsing is done
  10590. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10591. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10592. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10593. iso-week (string-to-number (match-string 2 ans)))
  10594. (setq ans (replace-match "" t t ans)))
  10595. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10596. (when (string-match
  10597. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10598. (setq year (if (match-end 2)
  10599. (string-to-number (match-string 2 ans))
  10600. (string-to-number (format-time-string "%Y")))
  10601. month (string-to-number (match-string 3 ans))
  10602. day (string-to-number (match-string 4 ans)))
  10603. (if (< year 100) (setq year (+ 2000 year)))
  10604. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10605. t nil ans)))
  10606. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10607. ;; If there is a time with am/pm, and *no* time without it, we convert
  10608. ;; so that matching will be successful.
  10609. (loop for i from 1 to 2 do ; twice, for end time as well
  10610. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10611. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10612. (setq hour (string-to-number (match-string 1 ans))
  10613. minute (if (match-end 3)
  10614. (string-to-number (match-string 3 ans))
  10615. 0)
  10616. pm (equal ?p
  10617. (string-to-char (downcase (match-string 4 ans)))))
  10618. (if (and (= hour 12) (not pm))
  10619. (setq hour 0)
  10620. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10621. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10622. t t ans))))
  10623. ;; Check if a time range is given as a duration
  10624. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10625. (setq hour (string-to-number (match-string 1 ans))
  10626. h2 (+ hour (string-to-number (match-string 3 ans)))
  10627. minute (string-to-number (match-string 2 ans))
  10628. m2 (+ minute (if (match-end 5) (string-to-number
  10629. (match-string 5 ans))0)))
  10630. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10631. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10632. t t ans)))
  10633. ;; Check if there is a time range
  10634. (when (boundp 'org-end-time-was-given)
  10635. (setq org-time-was-given nil)
  10636. (when (and (string-match org-plain-time-of-day-regexp ans)
  10637. (match-end 8))
  10638. (setq org-end-time-was-given (match-string 8 ans))
  10639. (setq ans (concat (substring ans 0 (match-beginning 7))
  10640. (substring ans (match-end 7))))))
  10641. (setq tl (parse-time-string ans)
  10642. day (or (nth 3 tl) (nth 3 defdecode))
  10643. month (or (nth 4 tl)
  10644. (if (and org-read-date-prefer-future
  10645. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10646. (1+ (nth 4 defdecode))
  10647. (nth 4 defdecode)))
  10648. year (or (nth 5 tl)
  10649. (if (and org-read-date-prefer-future
  10650. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10651. (1+ (nth 5 defdecode))
  10652. (nth 5 defdecode)))
  10653. hour (or (nth 2 tl) (nth 2 defdecode))
  10654. minute (or (nth 1 tl) (nth 1 defdecode))
  10655. second (or (nth 0 tl) 0)
  10656. wday (nth 6 tl))
  10657. ;; Special date definitions below
  10658. (cond
  10659. (iso-week
  10660. ;; There was an iso week
  10661. (setq year (or iso-year year)
  10662. day (or iso-weekday wday 1)
  10663. wday nil ; to make sure that the trigger below does not match
  10664. iso-date (calendar-gregorian-from-absolute
  10665. (calendar-absolute-from-iso
  10666. (list iso-week day year))))
  10667. ; FIXME: Should we also push ISO weeks into the future?
  10668. ; (when (and org-read-date-prefer-future
  10669. ; (not iso-year)
  10670. ; (< (calendar-absolute-from-gregorian iso-date)
  10671. ; (time-to-days (current-time))))
  10672. ; (setq year (1+ year)
  10673. ; iso-date (calendar-gregorian-from-absolute
  10674. ; (calendar-absolute-from-iso
  10675. ; (list iso-week day year)))))
  10676. (setq month (car iso-date)
  10677. year (nth 2 iso-date)
  10678. day (nth 1 iso-date)))
  10679. (deltan
  10680. (unless deltadef
  10681. (let ((now (decode-time (current-time))))
  10682. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10683. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10684. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10685. ((equal deltaw "m") (setq month (+ month deltan)))
  10686. ((equal deltaw "y") (setq year (+ year deltan)))))
  10687. ((and wday (not (nth 3 tl)))
  10688. ;; Weekday was given, but no day, so pick that day in the week
  10689. ;; on or after the derived date.
  10690. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10691. (unless (equal wday wday1)
  10692. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10693. (if (and (boundp 'org-time-was-given)
  10694. (nth 2 tl))
  10695. (setq org-time-was-given t))
  10696. (if (< year 100) (setq year (+ 2000 year)))
  10697. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10698. (list second minute hour day month year)))
  10699. (defvar parse-time-weekdays)
  10700. (defun org-read-date-get-relative (s today default)
  10701. "Check string S for special relative date string.
  10702. TODAY and DEFAULT are internal times, for today and for a default.
  10703. Return shift list (N what def-flag)
  10704. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10705. N is the number of WHATs to shift.
  10706. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10707. the DEFAULT date rather than TODAY."
  10708. (when (and
  10709. (string-match
  10710. (concat
  10711. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10712. "\\([0-9]+\\)?"
  10713. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10714. "\\([ \t]\\|$\\)") s)
  10715. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10716. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10717. (string-to-char (substring (match-string 1 s) -1))
  10718. ?+))
  10719. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10720. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10721. (what (if (match-end 3) (match-string 3 s) "d"))
  10722. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10723. (date (if rel default today))
  10724. (wday (nth 6 (decode-time date)))
  10725. delta)
  10726. (if wday1
  10727. (progn
  10728. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10729. (if (= dir ?-) (setq delta (- delta 7)))
  10730. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10731. (list delta "d" rel))
  10732. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10733. (defun org-eval-in-calendar (form &optional keepdate)
  10734. "Eval FORM in the calendar window and return to current window.
  10735. Also, store the cursor date in variable org-ans2."
  10736. (let ((sw (selected-window)))
  10737. (select-window (get-buffer-window "*Calendar*"))
  10738. (eval form)
  10739. (when (and (not keepdate) (calendar-cursor-to-date))
  10740. (let* ((date (calendar-cursor-to-date))
  10741. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10742. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10743. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10744. (select-window sw)))
  10745. (defun org-calendar-select ()
  10746. "Return to `org-read-date' with the date currently selected.
  10747. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10748. (interactive)
  10749. (when (calendar-cursor-to-date)
  10750. (let* ((date (calendar-cursor-to-date))
  10751. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10752. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10753. (if (active-minibuffer-window) (exit-minibuffer))))
  10754. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10755. "Insert a date stamp for the date given by the internal TIME.
  10756. WITH-HM means, use the stamp format that includes the time of the day.
  10757. INACTIVE means use square brackets instead of angular ones, so that the
  10758. stamp will not contribute to the agenda.
  10759. PRE and POST are optional strings to be inserted before and after the
  10760. stamp.
  10761. The command returns the inserted time stamp."
  10762. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10763. stamp)
  10764. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10765. (insert-before-markers (or pre ""))
  10766. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10767. (when (listp extra)
  10768. (setq extra (car extra))
  10769. (if (and (stringp extra)
  10770. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10771. (setq extra (format "-%02d:%02d"
  10772. (string-to-number (match-string 1 extra))
  10773. (string-to-number (match-string 2 extra))))
  10774. (setq extra nil)))
  10775. (when extra
  10776. (backward-char 1)
  10777. (insert-before-markers extra)
  10778. (forward-char 1))
  10779. (insert-before-markers (or post ""))
  10780. (setq org-last-inserted-timestamp stamp)))
  10781. (defun org-toggle-time-stamp-overlays ()
  10782. "Toggle the use of custom time stamp formats."
  10783. (interactive)
  10784. (setq org-display-custom-times (not org-display-custom-times))
  10785. (unless org-display-custom-times
  10786. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10787. (while (setq p (next-single-property-change p 'display))
  10788. (if (and (get-text-property p 'display)
  10789. (eq (get-text-property p 'face) 'org-date))
  10790. (remove-text-properties
  10791. p (setq p (next-single-property-change p 'display))
  10792. '(display t))))
  10793. (set-buffer-modified-p bmp)))
  10794. (if (featurep 'xemacs)
  10795. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10796. (org-restart-font-lock)
  10797. (setq org-table-may-need-update t)
  10798. (if org-display-custom-times
  10799. (message "Time stamps are overlayed with custom format")
  10800. (message "Time stamp overlays removed")))
  10801. (defun org-display-custom-time (beg end)
  10802. "Overlay modified time stamp format over timestamp between BEG and END."
  10803. (let* ((ts (buffer-substring beg end))
  10804. t1 w1 with-hm tf time str w2 (off 0))
  10805. (save-match-data
  10806. (setq t1 (org-parse-time-string ts t))
  10807. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10808. (setq off (- (match-end 0) (match-beginning 0)))))
  10809. (setq end (- end off))
  10810. (setq w1 (- end beg)
  10811. with-hm (and (nth 1 t1) (nth 2 t1))
  10812. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10813. time (org-fix-decoded-time t1)
  10814. str (org-add-props
  10815. (format-time-string
  10816. (substring tf 1 -1) (apply 'encode-time time))
  10817. nil 'mouse-face 'highlight)
  10818. w2 (length str))
  10819. (if (not (= w2 w1))
  10820. (add-text-properties (1+ beg) (+ 2 beg)
  10821. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10822. (if (featurep 'xemacs)
  10823. (progn
  10824. (put-text-property beg end 'invisible t)
  10825. (put-text-property beg end 'end-glyph (make-glyph str)))
  10826. (put-text-property beg end 'display str))))
  10827. (defun org-translate-time (string)
  10828. "Translate all timestamps in STRING to custom format.
  10829. But do this only if the variable `org-display-custom-times' is set."
  10830. (when org-display-custom-times
  10831. (save-match-data
  10832. (let* ((start 0)
  10833. (re org-ts-regexp-both)
  10834. t1 with-hm inactive tf time str beg end)
  10835. (while (setq start (string-match re string start))
  10836. (setq beg (match-beginning 0)
  10837. end (match-end 0)
  10838. t1 (save-match-data
  10839. (org-parse-time-string (substring string beg end) t))
  10840. with-hm (and (nth 1 t1) (nth 2 t1))
  10841. inactive (equal (substring string beg (1+ beg)) "[")
  10842. tf (funcall (if with-hm 'cdr 'car)
  10843. org-time-stamp-custom-formats)
  10844. time (org-fix-decoded-time t1)
  10845. str (format-time-string
  10846. (concat
  10847. (if inactive "[" "<") (substring tf 1 -1)
  10848. (if inactive "]" ">"))
  10849. (apply 'encode-time time))
  10850. string (replace-match str t t string)
  10851. start (+ start (length str)))))))
  10852. string)
  10853. (defun org-fix-decoded-time (time)
  10854. "Set 0 instead of nil for the first 6 elements of time.
  10855. Don't touch the rest."
  10856. (let ((n 0))
  10857. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10858. (defun org-days-to-time (timestamp-string)
  10859. "Difference between TIMESTAMP-STRING and now in days."
  10860. (- (time-to-days (org-time-string-to-time timestamp-string))
  10861. (time-to-days (current-time))))
  10862. (defun org-deadline-close (timestamp-string &optional ndays)
  10863. "Is the time in TIMESTAMP-STRING close to the current date?"
  10864. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10865. (and (< (org-days-to-time timestamp-string) ndays)
  10866. (not (org-entry-is-done-p))))
  10867. (defun org-get-wdays (ts)
  10868. "Get the deadline lead time appropriate for timestring TS."
  10869. (cond
  10870. ((<= org-deadline-warning-days 0)
  10871. ;; 0 or negative, enforce this value no matter what
  10872. (- org-deadline-warning-days))
  10873. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  10874. ;; lead time is specified.
  10875. (floor (* (string-to-number (match-string 1 ts))
  10876. (cdr (assoc (match-string 2 ts)
  10877. '(("d" . 1) ("w" . 7)
  10878. ("m" . 30.4) ("y" . 365.25)))))))
  10879. ;; go for the default.
  10880. (t org-deadline-warning-days)))
  10881. (defun org-calendar-select-mouse (ev)
  10882. "Return to `org-read-date' with the date currently selected.
  10883. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10884. (interactive "e")
  10885. (mouse-set-point ev)
  10886. (when (calendar-cursor-to-date)
  10887. (let* ((date (calendar-cursor-to-date))
  10888. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10889. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10890. (if (active-minibuffer-window) (exit-minibuffer))))
  10891. (defun org-check-deadlines (ndays)
  10892. "Check if there are any deadlines due or past due.
  10893. A deadline is considered due if it happens within `org-deadline-warning-days'
  10894. days from today's date. If the deadline appears in an entry marked DONE,
  10895. it is not shown. The prefix arg NDAYS can be used to test that many
  10896. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10897. (interactive "P")
  10898. (let* ((org-warn-days
  10899. (cond
  10900. ((equal ndays '(4)) 100000)
  10901. (ndays (prefix-numeric-value ndays))
  10902. (t (abs org-deadline-warning-days))))
  10903. (case-fold-search nil)
  10904. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10905. (callback
  10906. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10907. (message "%d deadlines past-due or due within %d days"
  10908. (org-occur regexp nil callback)
  10909. org-warn-days)))
  10910. (defun org-check-before-date (date)
  10911. "Check if there are deadlines or scheduled entries before DATE."
  10912. (interactive (list (org-read-date)))
  10913. (let ((case-fold-search nil)
  10914. (regexp (concat "\\<\\(" org-deadline-string
  10915. "\\|" org-scheduled-string
  10916. "\\) *<\\([^>]+\\)>"))
  10917. (callback
  10918. (lambda () (time-less-p
  10919. (org-time-string-to-time (match-string 2))
  10920. (org-time-string-to-time date)))))
  10921. (message "%d entries before %s"
  10922. (org-occur regexp nil callback) date)))
  10923. (defun org-evaluate-time-range (&optional to-buffer)
  10924. "Evaluate a time range by computing the difference between start and end.
  10925. Normally the result is just printed in the echo area, but with prefix arg
  10926. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10927. If the time range is actually in a table, the result is inserted into the
  10928. next column.
  10929. For time difference computation, a year is assumed to be exactly 365
  10930. days in order to avoid rounding problems."
  10931. (interactive "P")
  10932. (or
  10933. (org-clock-update-time-maybe)
  10934. (save-excursion
  10935. (unless (org-at-date-range-p t)
  10936. (goto-char (point-at-bol))
  10937. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10938. (if (not (org-at-date-range-p t))
  10939. (error "Not at a time-stamp range, and none found in current line")))
  10940. (let* ((ts1 (match-string 1))
  10941. (ts2 (match-string 2))
  10942. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10943. (match-end (match-end 0))
  10944. (time1 (org-time-string-to-time ts1))
  10945. (time2 (org-time-string-to-time ts2))
  10946. (t1 (time-to-seconds time1))
  10947. (t2 (time-to-seconds time2))
  10948. (diff (abs (- t2 t1)))
  10949. (negative (< (- t2 t1) 0))
  10950. ;; (ys (floor (* 365 24 60 60)))
  10951. (ds (* 24 60 60))
  10952. (hs (* 60 60))
  10953. (fy "%dy %dd %02d:%02d")
  10954. (fy1 "%dy %dd")
  10955. (fd "%dd %02d:%02d")
  10956. (fd1 "%dd")
  10957. (fh "%02d:%02d")
  10958. y d h m align)
  10959. (if havetime
  10960. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10961. y 0
  10962. d (floor (/ diff ds)) diff (mod diff ds)
  10963. h (floor (/ diff hs)) diff (mod diff hs)
  10964. m (floor (/ diff 60)))
  10965. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10966. y 0
  10967. d (floor (+ (/ diff ds) 0.5))
  10968. h 0 m 0))
  10969. (if (not to-buffer)
  10970. (message "%s" (org-make-tdiff-string y d h m))
  10971. (if (org-at-table-p)
  10972. (progn
  10973. (goto-char match-end)
  10974. (setq align t)
  10975. (and (looking-at " *|") (goto-char (match-end 0))))
  10976. (goto-char match-end))
  10977. (if (looking-at
  10978. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10979. (replace-match ""))
  10980. (if negative (insert " -"))
  10981. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10982. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10983. (insert " " (format fh h m))))
  10984. (if align (org-table-align))
  10985. (message "Time difference inserted")))))
  10986. (defun org-make-tdiff-string (y d h m)
  10987. (let ((fmt "")
  10988. (l nil))
  10989. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10990. l (push y l)))
  10991. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10992. l (push d l)))
  10993. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10994. l (push h l)))
  10995. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10996. l (push m l)))
  10997. (apply 'format fmt (nreverse l))))
  10998. (defun org-time-string-to-time (s)
  10999. (apply 'encode-time (org-parse-time-string s)))
  11000. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11001. "Convert a time stamp to an absolute day number.
  11002. If there is a specifyer for a cyclic time stamp, get the closest date to
  11003. DAYNR.
  11004. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11005. (cond
  11006. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11007. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11008. daynr
  11009. (+ daynr 1000)))
  11010. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11011. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11012. (time-to-days (current-time))) (match-string 0 s)
  11013. prefer show-all))
  11014. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11015. (defun org-days-to-iso-week (days)
  11016. "Return the iso week number."
  11017. (require 'cal-iso)
  11018. (car (calendar-iso-from-absolute days)))
  11019. (defun org-small-year-to-year (year)
  11020. "Convert 2-digit years into 4-digit years.
  11021. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11022. The year 2000 cannot be abbreviated. Any year larger than 99
  11023. is returned unchanged."
  11024. (if (< year 38)
  11025. (setq year (+ 2000 year))
  11026. (if (< year 100)
  11027. (setq year (+ 1900 year))))
  11028. year)
  11029. (defun org-time-from-absolute (d)
  11030. "Return the time corresponding to date D.
  11031. D may be an absolute day number, or a calendar-type list (month day year)."
  11032. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11033. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11034. (defun org-calendar-holiday ()
  11035. "List of holidays, for Diary display in Org-mode."
  11036. (require 'holidays)
  11037. (let ((hl (funcall
  11038. (if (fboundp 'calendar-check-holidays)
  11039. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11040. (if hl (mapconcat 'identity hl "; "))))
  11041. (defun org-diary-sexp-entry (sexp entry date)
  11042. "Process a SEXP diary ENTRY for DATE."
  11043. (require 'diary-lib)
  11044. (let ((result (if calendar-debug-sexp
  11045. (let ((stack-trace-on-error t))
  11046. (eval (car (read-from-string sexp))))
  11047. (condition-case nil
  11048. (eval (car (read-from-string sexp)))
  11049. (error
  11050. (beep)
  11051. (message "Bad sexp at line %d in %s: %s"
  11052. (org-current-line)
  11053. (buffer-file-name) sexp)
  11054. (sleep-for 2))))))
  11055. (cond ((stringp result) result)
  11056. ((and (consp result)
  11057. (stringp (cdr result))) (cdr result))
  11058. (result entry)
  11059. (t nil))))
  11060. (defun org-diary-to-ical-string (frombuf)
  11061. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11062. This uses the icalendar.el library."
  11063. (let* ((tmpdir (if (featurep 'xemacs)
  11064. (temp-directory)
  11065. temporary-file-directory))
  11066. (tmpfile (make-temp-name
  11067. (expand-file-name "orgics" tmpdir)))
  11068. buf rtn b e)
  11069. (save-excursion
  11070. (set-buffer frombuf)
  11071. (icalendar-export-region (point-min) (point-max) tmpfile)
  11072. (setq buf (find-buffer-visiting tmpfile))
  11073. (set-buffer buf)
  11074. (goto-char (point-min))
  11075. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11076. (setq b (match-beginning 0)))
  11077. (goto-char (point-max))
  11078. (if (re-search-backward "^END:VEVENT" nil t)
  11079. (setq e (match-end 0)))
  11080. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11081. (kill-buffer buf)
  11082. (delete-file tmpfile)
  11083. rtn))
  11084. (defun org-closest-date (start current change prefer show-all)
  11085. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11086. When PREFER is `past' return a date that is either CURRENT or past.
  11087. When PREFER is `future', return a date that is either CURRENT or future.
  11088. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11089. ;; Make the proper lists from the dates
  11090. (catch 'exit
  11091. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11092. dn dw sday cday n1 n2 n0
  11093. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11094. (setq start (org-date-to-gregorian start)
  11095. current (org-date-to-gregorian
  11096. (if show-all
  11097. current
  11098. (time-to-days (current-time))))
  11099. sday (calendar-absolute-from-gregorian start)
  11100. cday (calendar-absolute-from-gregorian current))
  11101. (if (<= cday sday) (throw 'exit sday))
  11102. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11103. (setq dn (string-to-number (match-string 1 change))
  11104. dw (cdr (assoc (match-string 2 change) a1)))
  11105. (error "Invalid change specifyer: %s" change))
  11106. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11107. (cond
  11108. ((eq dw 'day)
  11109. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11110. n2 (+ n1 dn)))
  11111. ((eq dw 'year)
  11112. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11113. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11114. (setq date1 (list m d y1)
  11115. n1 (calendar-absolute-from-gregorian date1)
  11116. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11117. n2 (calendar-absolute-from-gregorian date2)))
  11118. ((eq dw 'month)
  11119. ;; approx number of month between the two dates
  11120. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11121. ;; How often does dn fit in there?
  11122. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11123. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11124. m (+ m nm)
  11125. ny (floor (/ m 12))
  11126. y (+ y ny)
  11127. m (- m (* ny 12)))
  11128. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11129. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11130. (setq m2 (+ m dn) y2 y)
  11131. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11132. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11133. (while (<= n2 cday)
  11134. (setq n1 n2 m m2 y y2)
  11135. (setq m2 (+ m dn) y2 y)
  11136. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11137. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11138. ;; Make sure n1 is the earlier date
  11139. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11140. (if show-all
  11141. (cond
  11142. ((eq prefer 'past) n1)
  11143. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11144. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11145. (cond
  11146. ((eq prefer 'past) n1)
  11147. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11148. (t (if (= cday n1) n1 n2)))))))
  11149. (defun org-date-to-gregorian (date)
  11150. "Turn any specification of DATE into a gregorian date for the calendar."
  11151. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11152. ((and (listp date) (= (length date) 3)) date)
  11153. ((stringp date)
  11154. (setq date (org-parse-time-string date))
  11155. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11156. ((listp date)
  11157. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11158. (defun org-parse-time-string (s &optional nodefault)
  11159. "Parse the standard Org-mode time string.
  11160. This should be a lot faster than the normal `parse-time-string'.
  11161. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11162. hour and minute fields will be nil if not given."
  11163. (if (string-match org-ts-regexp0 s)
  11164. (list 0
  11165. (if (or (match-beginning 8) (not nodefault))
  11166. (string-to-number (or (match-string 8 s) "0")))
  11167. (if (or (match-beginning 7) (not nodefault))
  11168. (string-to-number (or (match-string 7 s) "0")))
  11169. (string-to-number (match-string 4 s))
  11170. (string-to-number (match-string 3 s))
  11171. (string-to-number (match-string 2 s))
  11172. nil nil nil)
  11173. (make-list 9 0)))
  11174. (defun org-timestamp-up (&optional arg)
  11175. "Increase the date item at the cursor by one.
  11176. If the cursor is on the year, change the year. If it is on the month or
  11177. the day, change that.
  11178. With prefix ARG, change by that many units."
  11179. (interactive "p")
  11180. (org-timestamp-change (prefix-numeric-value arg)))
  11181. (defun org-timestamp-down (&optional arg)
  11182. "Decrease the date item at the cursor by one.
  11183. If the cursor is on the year, change the year. If it is on the month or
  11184. the day, change that.
  11185. With prefix ARG, change by that many units."
  11186. (interactive "p")
  11187. (org-timestamp-change (- (prefix-numeric-value arg))))
  11188. (defun org-timestamp-up-day (&optional arg)
  11189. "Increase the date in the time stamp by one day.
  11190. With prefix ARG, change that many days."
  11191. (interactive "p")
  11192. (if (and (not (org-at-timestamp-p t))
  11193. (org-on-heading-p))
  11194. (org-todo 'up)
  11195. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11196. (defun org-timestamp-down-day (&optional arg)
  11197. "Decrease the date in the time stamp by one day.
  11198. With prefix ARG, change that many days."
  11199. (interactive "p")
  11200. (if (and (not (org-at-timestamp-p t))
  11201. (org-on-heading-p))
  11202. (org-todo 'down)
  11203. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11204. (defun org-at-timestamp-p (&optional inactive-ok)
  11205. "Determine if the cursor is in or at a timestamp."
  11206. (interactive)
  11207. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11208. (pos (point))
  11209. (ans (or (looking-at tsr)
  11210. (save-excursion
  11211. (skip-chars-backward "^[<\n\r\t")
  11212. (if (> (point) (point-min)) (backward-char 1))
  11213. (and (looking-at tsr)
  11214. (> (- (match-end 0) pos) -1))))))
  11215. (and ans
  11216. (boundp 'org-ts-what)
  11217. (setq org-ts-what
  11218. (cond
  11219. ((= pos (match-beginning 0)) 'bracket)
  11220. ((= pos (1- (match-end 0))) 'bracket)
  11221. ((org-pos-in-match-range pos 2) 'year)
  11222. ((org-pos-in-match-range pos 3) 'month)
  11223. ((org-pos-in-match-range pos 7) 'hour)
  11224. ((org-pos-in-match-range pos 8) 'minute)
  11225. ((or (org-pos-in-match-range pos 4)
  11226. (org-pos-in-match-range pos 5)) 'day)
  11227. ((and (> pos (or (match-end 8) (match-end 5)))
  11228. (< pos (match-end 0)))
  11229. (- pos (or (match-end 8) (match-end 5))))
  11230. (t 'day))))
  11231. ans))
  11232. (defun org-toggle-timestamp-type ()
  11233. "Toggle the type (<active> or [inactive]) of a time stamp."
  11234. (interactive)
  11235. (when (org-at-timestamp-p t)
  11236. (let ((beg (match-beginning 0)) (end (match-end 0))
  11237. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11238. (save-excursion
  11239. (goto-char beg)
  11240. (while (re-search-forward "[][<>]" end t)
  11241. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11242. t t)))
  11243. (message "Timestamp is now %sactive"
  11244. (if (equal (char-after beg) ?<) "" "in")))))
  11245. (defun org-timestamp-change (n &optional what)
  11246. "Change the date in the time stamp at point.
  11247. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11248. `year', `minute', `second'. If WHAT is not given, the cursor position
  11249. in the timestamp determines what will be changed."
  11250. (let ((pos (point))
  11251. with-hm inactive
  11252. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11253. org-ts-what
  11254. extra rem
  11255. ts time time0)
  11256. (if (not (org-at-timestamp-p t))
  11257. (error "Not at a timestamp"))
  11258. (if (and (not what) (eq org-ts-what 'bracket))
  11259. (org-toggle-timestamp-type)
  11260. (if (and (not what) (not (eq org-ts-what 'day))
  11261. org-display-custom-times
  11262. (get-text-property (point) 'display)
  11263. (not (get-text-property (1- (point)) 'display)))
  11264. (setq org-ts-what 'day))
  11265. (setq org-ts-what (or what org-ts-what)
  11266. inactive (= (char-after (match-beginning 0)) ?\[)
  11267. ts (match-string 0))
  11268. (replace-match "")
  11269. (if (string-match
  11270. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11271. ts)
  11272. (setq extra (match-string 1 ts)))
  11273. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11274. (setq with-hm t))
  11275. (setq time0 (org-parse-time-string ts))
  11276. (when (and (eq org-ts-what 'minute)
  11277. (eq current-prefix-arg nil))
  11278. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11279. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11280. (setcar (cdr time0) (+ (nth 1 time0)
  11281. (if (> n 0) (- rem) (- dm rem))))))
  11282. (setq time
  11283. (encode-time (or (car time0) 0)
  11284. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11285. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11286. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11287. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11288. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11289. (nthcdr 6 time0)))
  11290. (when (integerp org-ts-what)
  11291. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11292. (if (eq what 'calendar)
  11293. (let ((cal-date (org-get-date-from-calendar)))
  11294. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11295. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11296. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11297. (setcar time0 (or (car time0) 0))
  11298. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11299. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11300. (setq time (apply 'encode-time time0))))
  11301. (setq org-last-changed-timestamp
  11302. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11303. (org-clock-update-time-maybe)
  11304. (goto-char pos)
  11305. ;; Try to recenter the calendar window, if any
  11306. (if (and org-calendar-follow-timestamp-change
  11307. (get-buffer-window "*Calendar*" t)
  11308. (memq org-ts-what '(day month year)))
  11309. (org-recenter-calendar (time-to-days time))))))
  11310. (defun org-modify-ts-extra (s pos n dm)
  11311. "Change the different parts of the lead-time and repeat fields in timestamp."
  11312. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11313. ng h m new rem)
  11314. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11315. (cond
  11316. ((or (org-pos-in-match-range pos 2)
  11317. (org-pos-in-match-range pos 3))
  11318. (setq m (string-to-number (match-string 3 s))
  11319. h (string-to-number (match-string 2 s)))
  11320. (if (org-pos-in-match-range pos 2)
  11321. (setq h (+ h n))
  11322. (setq n (* dm (org-no-warnings (signum n))))
  11323. (when (not (= 0 (setq rem (% m dm))))
  11324. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11325. (setq m (+ m n)))
  11326. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11327. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11328. (setq h (min 24 (max 0 h)))
  11329. (setq ng 1 new (format "-%02d:%02d" h m)))
  11330. ((org-pos-in-match-range pos 6)
  11331. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11332. ((org-pos-in-match-range pos 5)
  11333. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11334. ((org-pos-in-match-range pos 9)
  11335. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11336. ((org-pos-in-match-range pos 8)
  11337. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11338. (when ng
  11339. (setq s (concat
  11340. (substring s 0 (match-beginning ng))
  11341. new
  11342. (substring s (match-end ng))))))
  11343. s))
  11344. (defun org-recenter-calendar (date)
  11345. "If the calendar is visible, recenter it to DATE."
  11346. (let* ((win (selected-window))
  11347. (cwin (get-buffer-window "*Calendar*" t))
  11348. (calendar-move-hook nil))
  11349. (when cwin
  11350. (select-window cwin)
  11351. (calendar-goto-date (if (listp date) date
  11352. (calendar-gregorian-from-absolute date)))
  11353. (select-window win))))
  11354. (defun org-goto-calendar (&optional arg)
  11355. "Go to the Emacs calendar at the current date.
  11356. If there is a time stamp in the current line, go to that date.
  11357. A prefix ARG can be used to force the current date."
  11358. (interactive "P")
  11359. (let ((tsr org-ts-regexp) diff
  11360. (calendar-move-hook nil)
  11361. (calendar-view-holidays-initially-flag nil)
  11362. (view-calendar-holidays-initially nil)
  11363. (calendar-view-diary-initially-flag nil)
  11364. (view-diary-entries-initially nil))
  11365. (if (or (org-at-timestamp-p)
  11366. (save-excursion
  11367. (beginning-of-line 1)
  11368. (looking-at (concat ".*" tsr))))
  11369. (let ((d1 (time-to-days (current-time)))
  11370. (d2 (time-to-days
  11371. (org-time-string-to-time (match-string 1)))))
  11372. (setq diff (- d2 d1))))
  11373. (calendar)
  11374. (calendar-goto-today)
  11375. (if (and diff (not arg)) (calendar-forward-day diff))))
  11376. (defun org-get-date-from-calendar ()
  11377. "Return a list (month day year) of date at point in calendar."
  11378. (with-current-buffer "*Calendar*"
  11379. (save-match-data
  11380. (calendar-cursor-to-date))))
  11381. (defun org-date-from-calendar ()
  11382. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11383. If there is already a time stamp at the cursor position, update it."
  11384. (interactive)
  11385. (if (org-at-timestamp-p t)
  11386. (org-timestamp-change 0 'calendar)
  11387. (let ((cal-date (org-get-date-from-calendar)))
  11388. (org-insert-time-stamp
  11389. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11390. (defun org-minutes-to-hh:mm-string (m)
  11391. "Compute H:MM from a number of minutes."
  11392. (let ((h (/ m 60)))
  11393. (setq m (- m (* 60 h)))
  11394. (format org-time-clocksum-format h m)))
  11395. (defun org-hh:mm-string-to-minutes (s)
  11396. "Convert a string H:MM to a number of minutes."
  11397. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11398. (+ (* (string-to-number (match-string 1 s)) 60)
  11399. (string-to-number (match-string 2 s)))
  11400. 0))
  11401. ;;;; Files
  11402. (defun org-save-all-org-buffers ()
  11403. "Save all Org-mode buffers without user confirmation."
  11404. (interactive)
  11405. (message "Saving all Org-mode buffers...")
  11406. (save-some-buffers t 'org-mode-p)
  11407. (when (featurep 'org-id) (org-id-locations-save))
  11408. (message "Saving all Org-mode buffers... done"))
  11409. (defun org-revert-all-org-buffers ()
  11410. "Revert all Org-mode buffers.
  11411. Prompt for confirmation when there are unsaved changes.
  11412. Be sure you know what you are doing before letting this function
  11413. overwrite your changes.
  11414. This function is useful in a setup where one tracks org files
  11415. with a version control system, to revert on one machine after pulling
  11416. changes from another. I believe the procedure must be like this:
  11417. 1. M-x org-save-all-org-buffers
  11418. 2. Pull changes from the other machine, resolve conflicts
  11419. 3. M-x org-revert-all-org-buffers"
  11420. (interactive)
  11421. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11422. (error "Abort"))
  11423. (save-excursion
  11424. (save-window-excursion
  11425. (mapc
  11426. (lambda (b)
  11427. (when (and (with-current-buffer b (org-mode-p))
  11428. (with-current-buffer b buffer-file-name))
  11429. (switch-to-buffer b)
  11430. (revert-buffer t 'no-confirm)))
  11431. (buffer-list))
  11432. (when (and (featurep 'org-id) org-id-track-globally)
  11433. (org-id-locations-load)))))
  11434. ;;;; Agenda files
  11435. ;;;###autoload
  11436. (defun org-iswitchb (&optional arg)
  11437. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11438. With a prefix argument, restrict available to files.
  11439. With two prefix arguments, restrict available buffers to agenda files.
  11440. Due to some yet unresolved reason, the global function
  11441. `iswitchb-mode' needs to be active for this function to work."
  11442. (interactive "P")
  11443. (require 'iswitchb)
  11444. (let ((enabled iswitchb-mode) blist)
  11445. (or enabled (iswitchb-mode 1))
  11446. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11447. ((equal arg '(16)) (org-buffer-list 'agenda))
  11448. (t (org-buffer-list))))
  11449. (unwind-protect
  11450. (let ((iswitchb-make-buflist-hook
  11451. (lambda ()
  11452. (setq iswitchb-temp-buflist
  11453. (mapcar 'buffer-name blist)))))
  11454. (switch-to-buffer
  11455. (iswitchb-read-buffer
  11456. "Switch-to: " nil t))
  11457. (or enabled (iswitchb-mode -1))))))
  11458. ;;;###autoload
  11459. (defun org-ido-switchb (&optional arg)
  11460. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11461. With a prefix argument, restrict available to files.
  11462. With two prefix arguments, restrict available buffers to agenda files."
  11463. (interactive "P")
  11464. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11465. ((equal arg '(16)) (org-buffer-list 'agenda))
  11466. (t (org-buffer-list)))))
  11467. (switch-to-buffer
  11468. (org-ido-completing-read "Org buffer: "
  11469. (mapcar 'buffer-name blist)
  11470. nil t))))
  11471. (defun org-buffer-list (&optional predicate exclude-tmp)
  11472. "Return a list of Org buffers.
  11473. PREDICATE can be `export', `files' or `agenda'.
  11474. export restrict the list to Export buffers.
  11475. files restrict the list to buffers visiting Org files.
  11476. agenda restrict the list to buffers visiting agenda files.
  11477. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11478. (let* ((bfn nil)
  11479. (agenda-files (and (eq predicate 'agenda)
  11480. (mapcar 'file-truename (org-agenda-files t))))
  11481. (filter
  11482. (cond
  11483. ((eq predicate 'files)
  11484. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11485. ((eq predicate 'export)
  11486. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11487. ((eq predicate 'agenda)
  11488. (lambda (b)
  11489. (with-current-buffer b
  11490. (and (eq major-mode 'org-mode)
  11491. (setq bfn (buffer-file-name b))
  11492. (member (file-truename bfn) agenda-files)))))
  11493. (t (lambda (b) (with-current-buffer b
  11494. (or (eq major-mode 'org-mode)
  11495. (string-match "\*Org .*Export"
  11496. (buffer-name b)))))))))
  11497. (delq nil
  11498. (mapcar
  11499. (lambda(b)
  11500. (if (and (funcall filter b)
  11501. (or (not exclude-tmp)
  11502. (not (string-match "tmp" (buffer-name b)))))
  11503. b
  11504. nil))
  11505. (buffer-list)))))
  11506. (defun org-agenda-files (&optional unrestricted archives)
  11507. "Get the list of agenda files.
  11508. Optional UNRESTRICTED means return the full list even if a restriction
  11509. is currently in place.
  11510. When ARCHIVES is t, include all archive files hat are really being
  11511. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11512. `org-agenda-archives-mode' is t."
  11513. (let ((files
  11514. (cond
  11515. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11516. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11517. ((listp org-agenda-files) org-agenda-files)
  11518. (t (error "Invalid value of `org-agenda-files'")))))
  11519. (setq files (apply 'append
  11520. (mapcar (lambda (f)
  11521. (if (file-directory-p f)
  11522. (directory-files
  11523. f t org-agenda-file-regexp)
  11524. (list f)))
  11525. files)))
  11526. (when org-agenda-skip-unavailable-files
  11527. (setq files (delq nil
  11528. (mapcar (function
  11529. (lambda (file)
  11530. (and (file-readable-p file) file)))
  11531. files))))
  11532. (when (or (eq archives t)
  11533. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11534. (setq files (org-add-archive-files files)))
  11535. files))
  11536. (defun org-edit-agenda-file-list ()
  11537. "Edit the list of agenda files.
  11538. Depending on setup, this either uses customize to edit the variable
  11539. `org-agenda-files', or it visits the file that is holding the list. In the
  11540. latter case, the buffer is set up in a way that saving it automatically kills
  11541. the buffer and restores the previous window configuration."
  11542. (interactive)
  11543. (if (stringp org-agenda-files)
  11544. (let ((cw (current-window-configuration)))
  11545. (find-file org-agenda-files)
  11546. (org-set-local 'org-window-configuration cw)
  11547. (org-add-hook 'after-save-hook
  11548. (lambda ()
  11549. (set-window-configuration
  11550. (prog1 org-window-configuration
  11551. (kill-buffer (current-buffer))))
  11552. (org-install-agenda-files-menu)
  11553. (message "New agenda file list installed"))
  11554. nil 'local)
  11555. (message "%s" (substitute-command-keys
  11556. "Edit list and finish with \\[save-buffer]")))
  11557. (customize-variable 'org-agenda-files)))
  11558. (defun org-store-new-agenda-file-list (list)
  11559. "Set new value for the agenda file list and save it correctly."
  11560. (if (stringp org-agenda-files)
  11561. (let ((f org-agenda-files) b)
  11562. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11563. (with-temp-file f
  11564. (insert (mapconcat 'identity list "\n") "\n")))
  11565. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11566. (setq org-agenda-files list)
  11567. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11568. (defun org-read-agenda-file-list ()
  11569. "Read the list of agenda files from a file."
  11570. (when (file-directory-p org-agenda-files)
  11571. (error "`org-agenda-files' cannot be a single directory"))
  11572. (when (stringp org-agenda-files)
  11573. (with-temp-buffer
  11574. (insert-file-contents org-agenda-files)
  11575. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11576. ;;;###autoload
  11577. (defun org-cycle-agenda-files ()
  11578. "Cycle through the files in `org-agenda-files'.
  11579. If the current buffer visits an agenda file, find the next one in the list.
  11580. If the current buffer does not, find the first agenda file."
  11581. (interactive)
  11582. (let* ((fs (org-agenda-files t))
  11583. (files (append fs (list (car fs))))
  11584. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11585. file)
  11586. (unless files (error "No agenda files"))
  11587. (catch 'exit
  11588. (while (setq file (pop files))
  11589. (if (equal (file-truename file) tcf)
  11590. (when (car files)
  11591. (find-file (car files))
  11592. (throw 'exit t))))
  11593. (find-file (car fs)))
  11594. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11595. (defun org-agenda-file-to-front (&optional to-end)
  11596. "Move/add the current file to the top of the agenda file list.
  11597. If the file is not present in the list, it is added to the front. If it is
  11598. present, it is moved there. With optional argument TO-END, add/move to the
  11599. end of the list."
  11600. (interactive "P")
  11601. (let ((org-agenda-skip-unavailable-files nil)
  11602. (file-alist (mapcar (lambda (x)
  11603. (cons (file-truename x) x))
  11604. (org-agenda-files t)))
  11605. (ctf (file-truename buffer-file-name))
  11606. x had)
  11607. (setq x (assoc ctf file-alist) had x)
  11608. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11609. (if to-end
  11610. (setq file-alist (append (delq x file-alist) (list x)))
  11611. (setq file-alist (cons x (delq x file-alist))))
  11612. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11613. (org-install-agenda-files-menu)
  11614. (message "File %s to %s of agenda file list"
  11615. (if had "moved" "added") (if to-end "end" "front"))))
  11616. (defun org-remove-file (&optional file)
  11617. "Remove current file from the list of files in variable `org-agenda-files'.
  11618. These are the files which are being checked for agenda entries.
  11619. Optional argument FILE means, use this file instead of the current."
  11620. (interactive)
  11621. (let* ((org-agenda-skip-unavailable-files nil)
  11622. (file (or file buffer-file-name))
  11623. (true-file (file-truename file))
  11624. (afile (abbreviate-file-name file))
  11625. (files (delq nil (mapcar
  11626. (lambda (x)
  11627. (if (equal true-file
  11628. (file-truename x))
  11629. nil x))
  11630. (org-agenda-files t)))))
  11631. (if (not (= (length files) (length (org-agenda-files t))))
  11632. (progn
  11633. (org-store-new-agenda-file-list files)
  11634. (org-install-agenda-files-menu)
  11635. (message "Removed file: %s" afile))
  11636. (message "File was not in list: %s (not removed)" afile))))
  11637. (defun org-file-menu-entry (file)
  11638. (vector file (list 'find-file file) t))
  11639. (defun org-check-agenda-file (file)
  11640. "Make sure FILE exists. If not, ask user what to do."
  11641. (when (not (file-exists-p file))
  11642. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11643. (abbreviate-file-name file))
  11644. (let ((r (downcase (read-char-exclusive))))
  11645. (cond
  11646. ((equal r ?r)
  11647. (org-remove-file file)
  11648. (throw 'nextfile t))
  11649. (t (error "Abort"))))))
  11650. (defun org-get-agenda-file-buffer (file)
  11651. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11652. it to the list of buffers which might be released later."
  11653. (let ((buf (org-find-base-buffer-visiting file)))
  11654. (if buf
  11655. buf ; just return it
  11656. ;; Make a new buffer and remember it
  11657. (setq buf (find-file-noselect file))
  11658. (if buf (push buf org-agenda-new-buffers))
  11659. buf)))
  11660. (defun org-release-buffers (blist)
  11661. "Release all buffers in list, asking the user for confirmation when needed.
  11662. When a buffer is unmodified, it is just killed. When modified, it is saved
  11663. \(if the user agrees) and then killed."
  11664. (let (buf file)
  11665. (while (setq buf (pop blist))
  11666. (setq file (buffer-file-name buf))
  11667. (when (and (buffer-modified-p buf)
  11668. file
  11669. (y-or-n-p (format "Save file %s? " file)))
  11670. (with-current-buffer buf (save-buffer)))
  11671. (kill-buffer buf))))
  11672. (defun org-prepare-agenda-buffers (files)
  11673. "Create buffers for all agenda files, protect archived trees and comments."
  11674. (interactive)
  11675. (let ((pa '(:org-archived t))
  11676. (pc '(:org-comment t))
  11677. (pall '(:org-archived t :org-comment t))
  11678. (inhibit-read-only t)
  11679. (rea (concat ":" org-archive-tag ":"))
  11680. bmp file re)
  11681. (save-excursion
  11682. (save-restriction
  11683. (while (setq file (pop files))
  11684. (if (bufferp file)
  11685. (set-buffer file)
  11686. (org-check-agenda-file file)
  11687. (set-buffer (org-get-agenda-file-buffer file)))
  11688. (widen)
  11689. (setq bmp (buffer-modified-p))
  11690. (org-refresh-category-properties)
  11691. (setq org-todo-keywords-for-agenda
  11692. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11693. (setq org-done-keywords-for-agenda
  11694. (append org-done-keywords-for-agenda org-done-keywords))
  11695. (setq org-todo-keyword-alist-for-agenda
  11696. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11697. (setq org-tag-alist-for-agenda
  11698. (append org-tag-alist-for-agenda org-tag-alist))
  11699. (save-excursion
  11700. (remove-text-properties (point-min) (point-max) pall)
  11701. (when org-agenda-skip-archived-trees
  11702. (goto-char (point-min))
  11703. (while (re-search-forward rea nil t)
  11704. (if (org-on-heading-p t)
  11705. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11706. (goto-char (point-min))
  11707. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11708. (while (re-search-forward re nil t)
  11709. (add-text-properties
  11710. (match-beginning 0) (org-end-of-subtree t) pc)))
  11711. (set-buffer-modified-p bmp))))
  11712. (setq org-todo-keyword-alist-for-agenda
  11713. (org-uniquify org-todo-keyword-alist-for-agenda)
  11714. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11715. ;;;; Embedded LaTeX
  11716. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11717. "Keymap for the minor `org-cdlatex-mode'.")
  11718. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11719. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11720. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11721. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11722. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11723. (defvar org-cdlatex-texmathp-advice-is-done nil
  11724. "Flag remembering if we have applied the advice to texmathp already.")
  11725. (define-minor-mode org-cdlatex-mode
  11726. "Toggle the minor `org-cdlatex-mode'.
  11727. This mode supports entering LaTeX environment and math in LaTeX fragments
  11728. in Org-mode.
  11729. \\{org-cdlatex-mode-map}"
  11730. nil " OCDL" nil
  11731. (when org-cdlatex-mode (require 'cdlatex))
  11732. (unless org-cdlatex-texmathp-advice-is-done
  11733. (setq org-cdlatex-texmathp-advice-is-done t)
  11734. (defadvice texmathp (around org-math-always-on activate)
  11735. "Always return t in org-mode buffers.
  11736. This is because we want to insert math symbols without dollars even outside
  11737. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11738. an embedded LaTeX fragment, let texmathp do its job.
  11739. \\[org-cdlatex-mode-map]"
  11740. (interactive)
  11741. (let (p)
  11742. (cond
  11743. ((not (org-mode-p)) ad-do-it)
  11744. ((eq this-command 'cdlatex-math-symbol)
  11745. (setq ad-return-value t
  11746. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11747. (t
  11748. (let ((p (org-inside-LaTeX-fragment-p)))
  11749. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11750. (setq ad-return-value t
  11751. texmathp-why '("Org-mode embedded math" . 0))
  11752. (if p ad-do-it)))))))))
  11753. (defun turn-on-org-cdlatex ()
  11754. "Unconditionally turn on `org-cdlatex-mode'."
  11755. (org-cdlatex-mode 1))
  11756. (defun org-inside-LaTeX-fragment-p ()
  11757. "Test if point is inside a LaTeX fragment.
  11758. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11759. sequence appearing also before point.
  11760. Even though the matchers for math are configurable, this function assumes
  11761. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11762. delimiters are skipped when they have been removed by customization.
  11763. The return value is nil, or a cons cell with the delimiter and
  11764. and the position of this delimiter.
  11765. This function does a reasonably good job, but can locally be fooled by
  11766. for example currency specifications. For example it will assume being in
  11767. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11768. fragments that are properly closed, but during editing, we have to live
  11769. with the uncertainty caused by missing closing delimiters. This function
  11770. looks only before point, not after."
  11771. (catch 'exit
  11772. (let ((pos (point))
  11773. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11774. (lim (progn
  11775. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11776. (point)))
  11777. dd-on str (start 0) m re)
  11778. (goto-char pos)
  11779. (when dodollar
  11780. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11781. re (nth 1 (assoc "$" org-latex-regexps)))
  11782. (while (string-match re str start)
  11783. (cond
  11784. ((= (match-end 0) (length str))
  11785. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11786. ((= (match-end 0) (- (length str) 5))
  11787. (throw 'exit nil))
  11788. (t (setq start (match-end 0))))))
  11789. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11790. (goto-char pos)
  11791. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11792. (and (match-beginning 2) (throw 'exit nil))
  11793. ;; count $$
  11794. (while (re-search-backward "\\$\\$" lim t)
  11795. (setq dd-on (not dd-on)))
  11796. (goto-char pos)
  11797. (if dd-on (cons "$$" m))))))
  11798. (defun org-try-cdlatex-tab ()
  11799. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11800. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11801. - inside a LaTeX fragment, or
  11802. - after the first word in a line, where an abbreviation expansion could
  11803. insert a LaTeX environment."
  11804. (when org-cdlatex-mode
  11805. (cond
  11806. ((save-excursion
  11807. (skip-chars-backward "a-zA-Z0-9*")
  11808. (skip-chars-backward " \t")
  11809. (bolp))
  11810. (cdlatex-tab) t)
  11811. ((org-inside-LaTeX-fragment-p)
  11812. (cdlatex-tab) t)
  11813. (t nil))))
  11814. (defun org-cdlatex-underscore-caret (&optional arg)
  11815. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11816. Revert to the normal definition outside of these fragments."
  11817. (interactive "P")
  11818. (if (org-inside-LaTeX-fragment-p)
  11819. (call-interactively 'cdlatex-sub-superscript)
  11820. (let (org-cdlatex-mode)
  11821. (call-interactively (key-binding (vector last-input-event))))))
  11822. (defun org-cdlatex-math-modify (&optional arg)
  11823. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11824. Revert to the normal definition outside of these fragments."
  11825. (interactive "P")
  11826. (if (org-inside-LaTeX-fragment-p)
  11827. (call-interactively 'cdlatex-math-modify)
  11828. (let (org-cdlatex-mode)
  11829. (call-interactively (key-binding (vector last-input-event))))))
  11830. (defvar org-latex-fragment-image-overlays nil
  11831. "List of overlays carrying the images of latex fragments.")
  11832. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11833. (defun org-remove-latex-fragment-image-overlays ()
  11834. "Remove all overlays with LaTeX fragment images in current buffer."
  11835. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11836. (setq org-latex-fragment-image-overlays nil))
  11837. (defun org-preview-latex-fragment (&optional subtree)
  11838. "Preview the LaTeX fragment at point, or all locally or globally.
  11839. If the cursor is in a LaTeX fragment, create the image and overlay
  11840. it over the source code. If there is no fragment at point, display
  11841. all fragments in the current text, from one headline to the next. With
  11842. prefix SUBTREE, display all fragments in the current subtree. With a
  11843. double prefix `C-u C-u', or when the cursor is before the first headline,
  11844. display all fragments in the buffer.
  11845. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11846. (interactive "P")
  11847. (org-remove-latex-fragment-image-overlays)
  11848. (save-excursion
  11849. (save-restriction
  11850. (let (beg end at msg)
  11851. (cond
  11852. ((or (equal subtree '(16))
  11853. (not (save-excursion
  11854. (re-search-backward (concat "^" outline-regexp) nil t))))
  11855. (setq beg (point-min) end (point-max)
  11856. msg "Creating images for buffer...%s"))
  11857. ((equal subtree '(4))
  11858. (org-back-to-heading)
  11859. (setq beg (point) end (org-end-of-subtree t)
  11860. msg "Creating images for subtree...%s"))
  11861. (t
  11862. (if (setq at (org-inside-LaTeX-fragment-p))
  11863. (goto-char (max (point-min) (- (cdr at) 2)))
  11864. (org-back-to-heading))
  11865. (setq beg (point) end (progn (outline-next-heading) (point))
  11866. msg (if at "Creating image...%s"
  11867. "Creating images for entry...%s"))))
  11868. (message msg "")
  11869. (narrow-to-region beg end)
  11870. (goto-char beg)
  11871. (org-format-latex
  11872. (concat "ltxpng/" (file-name-sans-extension
  11873. (file-name-nondirectory
  11874. buffer-file-name)))
  11875. default-directory 'overlays msg at 'forbuffer)
  11876. (message msg "done. Use `C-c C-c' to remove images.")))))
  11877. (defvar org-latex-regexps
  11878. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11879. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11880. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11881. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11882. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11883. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11884. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11885. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11886. "Regular expressions for matching embedded LaTeX.")
  11887. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11888. "Replace LaTeX fragments with links to an image, and produce images."
  11889. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11890. (let* ((prefixnodir (file-name-nondirectory prefix))
  11891. (absprefix (expand-file-name prefix dir))
  11892. (todir (file-name-directory absprefix))
  11893. (opt org-format-latex-options)
  11894. (matchers (plist-get opt :matchers))
  11895. (re-list org-latex-regexps)
  11896. (cnt 0) txt link beg end re e checkdir
  11897. m n block linkfile movefile ov)
  11898. ;; Check if there are old images files with this prefix, and remove them
  11899. (when (file-directory-p todir)
  11900. (mapc 'delete-file
  11901. (directory-files
  11902. todir 'full
  11903. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11904. ;; Check the different regular expressions
  11905. (while (setq e (pop re-list))
  11906. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11907. block (if (nth 3 e) "\n\n" ""))
  11908. (when (member m matchers)
  11909. (goto-char (point-min))
  11910. (while (re-search-forward re nil t)
  11911. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11912. (not (get-text-property (match-beginning n)
  11913. 'org-protected)))
  11914. (setq txt (match-string n)
  11915. beg (match-beginning n) end (match-end n)
  11916. cnt (1+ cnt)
  11917. linkfile (format "%s_%04d.png" prefix cnt)
  11918. movefile (format "%s_%04d.png" absprefix cnt)
  11919. link (concat block "[[file:" linkfile "]]" block))
  11920. (if msg (message msg cnt))
  11921. (goto-char beg)
  11922. (unless checkdir ; make sure the directory exists
  11923. (setq checkdir t)
  11924. (or (file-directory-p todir) (make-directory todir)))
  11925. (org-create-formula-image
  11926. txt movefile opt forbuffer)
  11927. (if overlays
  11928. (progn
  11929. (setq ov (org-make-overlay beg end))
  11930. (if (featurep 'xemacs)
  11931. (progn
  11932. (org-overlay-put ov 'invisible t)
  11933. (org-overlay-put
  11934. ov 'end-glyph
  11935. (make-glyph (vector 'png :file movefile))))
  11936. (org-overlay-put
  11937. ov 'display
  11938. (list 'image :type 'png :file movefile :ascent 'center)))
  11939. (push ov org-latex-fragment-image-overlays)
  11940. (goto-char end))
  11941. (delete-region beg end)
  11942. (insert link))))))))
  11943. ;; This function borrows from Ganesh Swami's latex2png.el
  11944. (defun org-create-formula-image (string tofile options buffer)
  11945. (let* ((tmpdir (if (featurep 'xemacs)
  11946. (temp-directory)
  11947. temporary-file-directory))
  11948. (texfilebase (make-temp-name
  11949. (expand-file-name "orgtex" tmpdir)))
  11950. (texfile (concat texfilebase ".tex"))
  11951. (dvifile (concat texfilebase ".dvi"))
  11952. (pngfile (concat texfilebase ".png"))
  11953. (fnh (if (featurep 'xemacs)
  11954. (font-height (get-face-font 'default))
  11955. (face-attribute 'default :height nil)))
  11956. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11957. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11958. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11959. "Black"))
  11960. (bg (or (plist-get options (if buffer :background :html-background))
  11961. "Transparent")))
  11962. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11963. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11964. (with-temp-file texfile
  11965. (insert org-format-latex-header
  11966. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11967. (let ((dir default-directory))
  11968. (condition-case nil
  11969. (progn
  11970. (cd tmpdir)
  11971. (call-process "latex" nil nil nil texfile))
  11972. (error nil))
  11973. (cd dir))
  11974. (if (not (file-exists-p dvifile))
  11975. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11976. (condition-case nil
  11977. (call-process "dvipng" nil nil nil
  11978. "-E" "-fg" fg "-bg" bg
  11979. "-D" dpi
  11980. ;;"-x" scale "-y" scale
  11981. "-T" "tight"
  11982. "-o" pngfile
  11983. dvifile)
  11984. (error nil))
  11985. (if (not (file-exists-p pngfile))
  11986. (progn (message "Failed to create png file from %s" texfile) nil)
  11987. ;; Use the requested file name and clean up
  11988. (copy-file pngfile tofile 'replace)
  11989. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11990. (delete-file (concat texfilebase e)))
  11991. pngfile))))
  11992. (defun org-dvipng-color (attr)
  11993. "Return an rgb color specification for dvipng."
  11994. (apply 'format "rgb %s %s %s"
  11995. (mapcar 'org-normalize-color
  11996. (color-values (face-attribute 'default attr nil)))))
  11997. (defun org-normalize-color (value)
  11998. "Return string to be used as color value for an RGB component."
  11999. (format "%g" (/ value 65535.0)))
  12000. ;;;; Key bindings
  12001. ;; Make `C-c C-x' a prefix key
  12002. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12003. ;; TAB key with modifiers
  12004. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12005. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12006. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12007. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12008. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12009. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12010. ;; The following line is necessary under Suse GNU/Linux
  12011. (unless (featurep 'xemacs)
  12012. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12013. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12014. (define-key org-mode-map [backtab] 'org-shifttab)
  12015. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12016. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12017. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12018. ;; Cursor keys with modifiers
  12019. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12020. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12021. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12022. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12023. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12024. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12025. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12026. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12027. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12028. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12029. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12030. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12031. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12032. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12033. ;;; Extra keys for tty access.
  12034. ;; We only set them when really needed because otherwise the
  12035. ;; menus don't show the simple keys
  12036. (when (or org-use-extra-keys
  12037. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12038. (not window-system))
  12039. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12040. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12041. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12042. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12043. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12044. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12045. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12046. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12047. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12048. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12049. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12050. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12051. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12052. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12053. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12054. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12055. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12056. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12057. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12058. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12059. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12060. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12061. ;; All the other keys
  12062. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12063. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12064. (if (boundp 'narrow-map)
  12065. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12066. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12067. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12068. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12069. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12070. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12071. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12072. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12073. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12074. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12075. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12076. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12077. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12078. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12079. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12080. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12081. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12082. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12083. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12084. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12085. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12086. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12087. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12088. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12089. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12090. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12091. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12092. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12093. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12094. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12095. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12096. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12097. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12098. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12099. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12100. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12101. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12102. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12103. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12104. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12105. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12106. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12107. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12108. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12109. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12110. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12111. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12112. (org-defkey org-mode-map "\C-m" 'org-return)
  12113. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12114. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12115. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12116. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12117. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12118. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12119. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12120. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12121. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12122. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12123. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12124. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12125. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12126. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12127. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12128. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12129. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12130. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12131. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12132. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12133. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12134. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12135. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12136. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12137. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12138. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12139. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12140. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12141. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12142. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12143. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12144. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12145. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12146. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12147. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12148. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12149. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12150. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12151. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12152. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12153. (when (featurep 'xemacs)
  12154. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12155. (defvar org-self-insert-command-undo-counter 0)
  12156. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12157. (defun org-self-insert-command (N)
  12158. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12159. If the cursor is in a table looking at whitespace, the whitespace is
  12160. overwritten, and the table is not marked as requiring realignment."
  12161. (interactive "p")
  12162. (if (and
  12163. (org-table-p)
  12164. (progn
  12165. ;; check if we blank the field, and if that triggers align
  12166. (and (featurep 'org-table) org-table-auto-blank-field
  12167. (member last-command
  12168. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12169. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12170. ;; got extra space, this field does not determine column width
  12171. (let (org-table-may-need-update) (org-table-blank-field))
  12172. ;; no extra space, this field may determine column width
  12173. (org-table-blank-field)))
  12174. t)
  12175. (eq N 1)
  12176. (looking-at "[^|\n]* |"))
  12177. (let (org-table-may-need-update)
  12178. (goto-char (1- (match-end 0)))
  12179. (delete-backward-char 1)
  12180. (goto-char (match-beginning 0))
  12181. (self-insert-command N))
  12182. (setq org-table-may-need-update t)
  12183. (self-insert-command N)
  12184. (org-fix-tags-on-the-fly)
  12185. (if org-self-insert-cluster-for-undo
  12186. (if (not (eq last-command 'org-self-insert-command))
  12187. (setq org-self-insert-command-undo-counter 1)
  12188. (if (>= org-self-insert-command-undo-counter 20)
  12189. (setq org-self-insert-command-undo-counter 1)
  12190. (and (> org-self-insert-command-undo-counter 0)
  12191. buffer-undo-list
  12192. (not (cadr buffer-undo-list)) ; remove nil entry
  12193. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12194. (setq org-self-insert-command-undo-counter
  12195. (1+ org-self-insert-command-undo-counter)))))))
  12196. (defun org-fix-tags-on-the-fly ()
  12197. (when (and (equal (char-after (point-at-bol)) ?*)
  12198. (org-on-heading-p))
  12199. (org-align-tags-here org-tags-column)))
  12200. (defun org-delete-backward-char (N)
  12201. "Like `delete-backward-char', insert whitespace at field end in tables.
  12202. When deleting backwards, in tables this function will insert whitespace in
  12203. front of the next \"|\" separator, to keep the table aligned. The table will
  12204. still be marked for re-alignment if the field did fill the entire column,
  12205. because, in this case the deletion might narrow the column."
  12206. (interactive "p")
  12207. (if (and (org-table-p)
  12208. (eq N 1)
  12209. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12210. (looking-at ".*?|"))
  12211. (let ((pos (point))
  12212. (noalign (looking-at "[^|\n\r]* |"))
  12213. (c org-table-may-need-update))
  12214. (backward-delete-char N)
  12215. (skip-chars-forward "^|")
  12216. (insert " ")
  12217. (goto-char (1- pos))
  12218. ;; noalign: if there were two spaces at the end, this field
  12219. ;; does not determine the width of the column.
  12220. (if noalign (setq org-table-may-need-update c)))
  12221. (backward-delete-char N)
  12222. (org-fix-tags-on-the-fly)))
  12223. (defun org-delete-char (N)
  12224. "Like `delete-char', but insert whitespace at field end in tables.
  12225. When deleting characters, in tables this function will insert whitespace in
  12226. front of the next \"|\" separator, to keep the table aligned. The table will
  12227. still be marked for re-alignment if the field did fill the entire column,
  12228. because, in this case the deletion might narrow the column."
  12229. (interactive "p")
  12230. (if (and (org-table-p)
  12231. (not (bolp))
  12232. (not (= (char-after) ?|))
  12233. (eq N 1))
  12234. (if (looking-at ".*?|")
  12235. (let ((pos (point))
  12236. (noalign (looking-at "[^|\n\r]* |"))
  12237. (c org-table-may-need-update))
  12238. (replace-match (concat
  12239. (substring (match-string 0) 1 -1)
  12240. " |"))
  12241. (goto-char pos)
  12242. ;; noalign: if there were two spaces at the end, this field
  12243. ;; does not determine the width of the column.
  12244. (if noalign (setq org-table-may-need-update c)))
  12245. (delete-char N))
  12246. (delete-char N)
  12247. (org-fix-tags-on-the-fly)))
  12248. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12249. (put 'org-self-insert-command 'delete-selection t)
  12250. (put 'orgtbl-self-insert-command 'delete-selection t)
  12251. (put 'org-delete-char 'delete-selection 'supersede)
  12252. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12253. (put 'org-yank 'delete-selection 'yank)
  12254. ;; Make `flyspell-mode' delay after some commands
  12255. (put 'org-self-insert-command 'flyspell-delayed t)
  12256. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12257. (put 'org-delete-char 'flyspell-delayed t)
  12258. (put 'org-delete-backward-char 'flyspell-delayed t)
  12259. ;; Make pabbrev-mode expand after org-mode commands
  12260. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12261. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12262. ;; How to do this: Measure non-white length of current string
  12263. ;; If equal to column width, we should realign.
  12264. (defun org-remap (map &rest commands)
  12265. "In MAP, remap the functions given in COMMANDS.
  12266. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12267. (let (new old)
  12268. (while commands
  12269. (setq old (pop commands) new (pop commands))
  12270. (if (fboundp 'command-remapping)
  12271. (org-defkey map (vector 'remap old) new)
  12272. (substitute-key-definition old new map global-map)))))
  12273. (when (eq org-enable-table-editor 'optimized)
  12274. ;; If the user wants maximum table support, we need to hijack
  12275. ;; some standard editing functions
  12276. (org-remap org-mode-map
  12277. 'self-insert-command 'org-self-insert-command
  12278. 'delete-char 'org-delete-char
  12279. 'delete-backward-char 'org-delete-backward-char)
  12280. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12281. (defvar org-ctrl-c-ctrl-c-hook nil
  12282. "Hook for functions attaching themselves to `C-c C-c'.
  12283. This can be used to add additional functionality to the C-c C-c key which
  12284. executes context-dependent commands.
  12285. Each function will be called with no arguments. The function must check
  12286. if the context is appropriate for it to act. If yes, it should do its
  12287. thing and then return a non-nil value. If the context is wrong,
  12288. just do nothing.")
  12289. (defvar org-metaleft-hook nil
  12290. "Hook for functions attaching themselves to `M-left'.
  12291. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12292. (defvar org-metaright-hook nil
  12293. "Hook for functions attaching themselves to `M-right'.
  12294. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12295. (defvar org-metaup-hook nil
  12296. "Hook for functions attaching themselves to `M-up'.
  12297. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12298. (defvar org-metadown-hook nil
  12299. "Hook for functions attaching themselves to `M-down'.
  12300. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12301. (defvar org-shiftmetaleft-hook nil
  12302. "Hook for functions attaching themselves to `M-S-left'.
  12303. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12304. (defvar org-shiftmetaright-hook nil
  12305. "Hook for functions attaching themselves to `M-S-right'.
  12306. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12307. (defvar org-shiftmetaup-hook nil
  12308. "Hook for functions attaching themselves to `M-S-up'.
  12309. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12310. (defvar org-shiftmetadown-hook nil
  12311. "Hook for functions attaching themselves to `M-S-down'.
  12312. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12313. (defvar org-metareturn-hook nil
  12314. "Hook for functions attaching themselves to `M-RET'.
  12315. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12316. (defun org-modifier-cursor-error ()
  12317. "Throw an error, a modified cursor command was applied in wrong context."
  12318. (error "This command is active in special context like tables, headlines or items"))
  12319. (defun org-shiftselect-error ()
  12320. "Throw an error because Shift-Cursor command was applied in wrong context."
  12321. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12322. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12323. (error "This command works only in special context like headlines or timestamps.")))
  12324. (defun org-call-for-shift-select (cmd)
  12325. (let ((this-command-keys-shift-translated t))
  12326. (call-interactively cmd)))
  12327. (defun org-shifttab (&optional arg)
  12328. "Global visibility cycling or move to previous table field.
  12329. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12330. on context.
  12331. See the individual commands for more information."
  12332. (interactive "P")
  12333. (cond
  12334. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12335. ((integerp arg)
  12336. (message "Content view to level: %d" arg)
  12337. (org-content (prefix-numeric-value arg))
  12338. (setq org-cycle-global-status 'overview))
  12339. (t (call-interactively 'org-global-cycle))))
  12340. (defun org-shiftmetaleft ()
  12341. "Promote subtree or delete table column.
  12342. Calls `org-promote-subtree', `org-outdent-item',
  12343. or `org-table-delete-column', depending on context.
  12344. See the individual commands for more information."
  12345. (interactive)
  12346. (cond
  12347. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12348. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12349. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12350. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12351. (t (org-modifier-cursor-error))))
  12352. (defun org-shiftmetaright ()
  12353. "Demote subtree or insert table column.
  12354. Calls `org-demote-subtree', `org-indent-item',
  12355. or `org-table-insert-column', depending on context.
  12356. See the individual commands for more information."
  12357. (interactive)
  12358. (cond
  12359. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12360. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12361. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12362. ((org-at-item-p) (call-interactively 'org-indent-item))
  12363. (t (org-modifier-cursor-error))))
  12364. (defun org-shiftmetaup (&optional arg)
  12365. "Move subtree up or kill table row.
  12366. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12367. `org-move-item-up' depending on context. See the individual commands
  12368. for more information."
  12369. (interactive "P")
  12370. (cond
  12371. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12372. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12373. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12374. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12375. (t (org-modifier-cursor-error))))
  12376. (defun org-shiftmetadown (&optional arg)
  12377. "Move subtree down or insert table row.
  12378. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12379. `org-move-item-down', depending on context. See the individual
  12380. commands for more information."
  12381. (interactive "P")
  12382. (cond
  12383. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12384. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12385. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12386. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12387. (t (org-modifier-cursor-error))))
  12388. (defun org-metaleft (&optional arg)
  12389. "Promote heading or move table column to left.
  12390. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12391. With no specific context, calls the Emacs default `backward-word'.
  12392. See the individual commands for more information."
  12393. (interactive "P")
  12394. (cond
  12395. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12396. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12397. ((or (org-on-heading-p)
  12398. (and (org-region-active-p)
  12399. (save-excursion
  12400. (goto-char (region-beginning))
  12401. (org-on-heading-p))))
  12402. (call-interactively 'org-do-promote))
  12403. ((or (org-at-item-p)
  12404. (and (org-region-active-p)
  12405. (save-excursion
  12406. (goto-char (region-beginning))
  12407. (org-at-item-p))))
  12408. (call-interactively 'org-outdent-item))
  12409. (t (call-interactively 'backward-word))))
  12410. (defun org-metaright (&optional arg)
  12411. "Demote subtree or move table column to right.
  12412. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12413. With no specific context, calls the Emacs default `forward-word'.
  12414. See the individual commands for more information."
  12415. (interactive "P")
  12416. (cond
  12417. ((run-hook-with-args-until-success 'org-metaright-hook))
  12418. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12419. ((or (org-on-heading-p)
  12420. (and (org-region-active-p)
  12421. (save-excursion
  12422. (goto-char (region-beginning))
  12423. (org-on-heading-p))))
  12424. (call-interactively 'org-do-demote))
  12425. ((or (org-at-item-p)
  12426. (and (org-region-active-p)
  12427. (save-excursion
  12428. (goto-char (region-beginning))
  12429. (org-at-item-p))))
  12430. (call-interactively 'org-indent-item))
  12431. (t (call-interactively 'forward-word))))
  12432. (defun org-metaup (&optional arg)
  12433. "Move subtree up or move table row up.
  12434. Calls `org-move-subtree-up' or `org-table-move-row' or
  12435. `org-move-item-up', depending on context. See the individual commands
  12436. for more information."
  12437. (interactive "P")
  12438. (cond
  12439. ((run-hook-with-args-until-success 'org-metaup-hook))
  12440. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12441. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12442. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12443. (t (transpose-lines 1) (beginning-of-line -1))))
  12444. (defun org-metadown (&optional arg)
  12445. "Move subtree down or move table row down.
  12446. Calls `org-move-subtree-down' or `org-table-move-row' or
  12447. `org-move-item-down', depending on context. See the individual
  12448. commands for more information."
  12449. (interactive "P")
  12450. (cond
  12451. ((run-hook-with-args-until-success 'org-metadown-hook))
  12452. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12453. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12454. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12455. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12456. (defun org-shiftup (&optional arg)
  12457. "Increase item in timestamp or increase priority of current headline.
  12458. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12459. depending on context. See the individual commands for more information."
  12460. (interactive "P")
  12461. (cond
  12462. ((and org-support-shift-select (org-region-active-p))
  12463. (org-call-for-shift-select 'previous-line))
  12464. ((org-at-timestamp-p t)
  12465. (call-interactively (if org-edit-timestamp-down-means-later
  12466. 'org-timestamp-down 'org-timestamp-up)))
  12467. ((and (not (eq org-support-shift-select 'always))
  12468. (org-on-heading-p))
  12469. (call-interactively 'org-priority-up))
  12470. ((and (not org-support-shift-select) (org-at-item-p))
  12471. (call-interactively 'org-previous-item))
  12472. ((org-clocktable-try-shift 'up arg))
  12473. (org-support-shift-select
  12474. (org-call-for-shift-select 'previous-line))
  12475. (t (org-shiftselect-error))))
  12476. (defun org-shiftdown (&optional arg)
  12477. "Decrease item in timestamp or decrease priority of current headline.
  12478. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12479. depending on context. See the individual commands for more information."
  12480. (interactive "P")
  12481. (cond
  12482. ((and org-support-shift-select (org-region-active-p))
  12483. (org-call-for-shift-select 'next-line))
  12484. ((org-at-timestamp-p t)
  12485. (call-interactively (if org-edit-timestamp-down-means-later
  12486. 'org-timestamp-up 'org-timestamp-down)))
  12487. ((and (not (eq org-support-shift-select 'always))
  12488. (org-on-heading-p))
  12489. (call-interactively 'org-priority-down))
  12490. ((and (not org-support-shift-select) (org-at-item-p))
  12491. (call-interactively 'org-next-item))
  12492. ((org-clocktable-try-shift 'down arg))
  12493. (org-support-shift-select
  12494. (org-call-for-shift-select 'next-line))
  12495. (t (org-shiftselect-error))))
  12496. (defun org-shiftright (&optional arg)
  12497. "Cycle the thing at point or in the current line, depending on context.
  12498. Depending on context, this does one of the following:
  12499. - switch a timestamp at point one day into the future
  12500. - on a headline, switch to the next TODO keyword.
  12501. - on an item, switch entire list to the next bullet type
  12502. - on a property line, switch to the next allowed value
  12503. - on a clocktable definition line, move time block into the future"
  12504. (interactive "P")
  12505. (cond
  12506. ((and org-support-shift-select (org-region-active-p))
  12507. (org-call-for-shift-select 'forward-char))
  12508. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12509. ((and (not (eq org-support-shift-select 'always))
  12510. (org-on-heading-p))
  12511. (org-call-with-arg 'org-todo 'right))
  12512. ((or (and org-support-shift-select
  12513. (not (eq org-support-shift-select 'always))
  12514. (org-at-item-bullet-p))
  12515. (and (not org-support-shift-select) (org-at-item-p)))
  12516. (org-call-with-arg 'org-cycle-list-bullet nil))
  12517. ((and (not (eq org-support-shift-select 'always))
  12518. (org-at-property-p))
  12519. (call-interactively 'org-property-next-allowed-value))
  12520. ((org-clocktable-try-shift 'right arg))
  12521. (org-support-shift-select
  12522. (org-call-for-shift-select 'forward-char))
  12523. (t (org-shiftselect-error))))
  12524. (defun org-shiftleft (&optional arg)
  12525. "Cycle the thing at point or in the current line, depending on context.
  12526. Depending on context, this does one of the following:
  12527. - switch a timestamp at point one day into the past
  12528. - on a headline, switch to the previous TODO keyword.
  12529. - on an item, switch entire list to the previous bullet type
  12530. - on a property line, switch to the previous allowed value
  12531. - on a clocktable definition line, move time block into the past"
  12532. (interactive "P")
  12533. (cond
  12534. ((and org-support-shift-select (org-region-active-p))
  12535. (org-call-for-shift-select 'backward-char))
  12536. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12537. ((and (not (eq org-support-shift-select 'always))
  12538. (org-on-heading-p))
  12539. (org-call-with-arg 'org-todo 'left))
  12540. ((or (and org-support-shift-select
  12541. (not (eq org-support-shift-select 'always))
  12542. (org-at-item-bullet-p))
  12543. (and (not org-support-shift-select) (org-at-item-p)))
  12544. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12545. ((and (not (eq org-support-shift-select 'always))
  12546. (org-at-property-p))
  12547. (call-interactively 'org-property-previous-allowed-value))
  12548. ((org-clocktable-try-shift 'left arg))
  12549. (org-support-shift-select
  12550. (org-call-for-shift-select 'backward-char))
  12551. (t (org-shiftselect-error))))
  12552. (defun org-shiftcontrolright ()
  12553. "Switch to next TODO set."
  12554. (interactive)
  12555. (cond
  12556. ((and org-support-shift-select (org-region-active-p))
  12557. (org-call-for-shift-select 'forward-word))
  12558. ((and (not (eq org-support-shift-select 'always))
  12559. (org-on-heading-p))
  12560. (org-call-with-arg 'org-todo 'nextset))
  12561. (org-support-shift-select
  12562. (org-call-for-shift-select 'forward-word))
  12563. (t (org-shiftselect-error))))
  12564. (defun org-shiftcontrolleft ()
  12565. "Switch to previous TODO set."
  12566. (interactive)
  12567. (cond
  12568. ((and org-support-shift-select (org-region-active-p))
  12569. (org-call-for-shift-select 'backward-word))
  12570. ((and (not (eq org-support-shift-select 'always))
  12571. (org-on-heading-p))
  12572. (org-call-with-arg 'org-todo 'previousset))
  12573. (org-support-shift-select
  12574. (org-call-for-shift-select 'backward-word))
  12575. (t (org-shiftselect-error))))
  12576. (defun org-ctrl-c-ret ()
  12577. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12578. (interactive)
  12579. (cond
  12580. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12581. (t (call-interactively 'org-insert-heading))))
  12582. (defun org-copy-special ()
  12583. "Copy region in table or copy current subtree.
  12584. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12585. See the individual commands for more information."
  12586. (interactive)
  12587. (call-interactively
  12588. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12589. (defun org-cut-special ()
  12590. "Cut region in table or cut current subtree.
  12591. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12592. See the individual commands for more information."
  12593. (interactive)
  12594. (call-interactively
  12595. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12596. (defun org-paste-special (arg)
  12597. "Paste rectangular region into table, or past subtree relative to level.
  12598. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12599. See the individual commands for more information."
  12600. (interactive "P")
  12601. (if (org-at-table-p)
  12602. (org-table-paste-rectangle)
  12603. (org-paste-subtree arg)))
  12604. (defun org-edit-special ()
  12605. "Call a special editor for the stuff at point.
  12606. When at a table, call the formula editor with `org-table-edit-formulas'.
  12607. When at the first line of an src example, call `org-edit-src-code'.
  12608. When in an #+include line, visit the include file. Otherwise call
  12609. `ffap' to visit the file at point."
  12610. (interactive)
  12611. (cond
  12612. ((org-at-table-p)
  12613. (call-interactively 'org-table-edit-formulas))
  12614. ((save-excursion
  12615. (beginning-of-line 1)
  12616. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12617. (find-file (org-trim (match-string 1))))
  12618. ((org-edit-src-code))
  12619. ((org-edit-fixed-width-region))
  12620. (t (call-interactively 'ffap))))
  12621. (defun org-ctrl-c-ctrl-c (&optional arg)
  12622. "Set tags in headline, or update according to changed information at point.
  12623. This command does many different things, depending on context:
  12624. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12625. this is what we do.
  12626. - If the cursor is in a headline, prompt for tags and insert them
  12627. into the current line, aligned to `org-tags-column'. When called
  12628. with prefix arg, realign all tags in the current buffer.
  12629. - If the cursor is in one of the special #+KEYWORD lines, this
  12630. triggers scanning the buffer for these lines and updating the
  12631. information.
  12632. - If the cursor is inside a table, realign the table. This command
  12633. works even if the automatic table editor has been turned off.
  12634. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12635. the entire table.
  12636. - If the cursor is at a footnote reference or definition, jump to
  12637. the corresponding definition or references, respectively.
  12638. - If the cursor is a the beginning of a dynamic block, update it.
  12639. - If the cursor is inside a table created by the table.el package,
  12640. activate that table.
  12641. - If the current buffer is a remember buffer, close note and file
  12642. it. A prefix argument of 1 files to the default location
  12643. without further interaction. A prefix argument of 2 files to
  12644. the currently clocking task.
  12645. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12646. links in this buffer.
  12647. - If the cursor is on a numbered item in a plain list, renumber the
  12648. ordered list.
  12649. - If the cursor is on a checkbox, toggle it."
  12650. (interactive "P")
  12651. (let ((org-enable-table-editor t))
  12652. (cond
  12653. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12654. org-occur-highlights
  12655. org-latex-fragment-image-overlays)
  12656. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12657. (org-remove-occur-highlights)
  12658. (org-remove-latex-fragment-image-overlays)
  12659. (message "Temporary highlights/overlays removed from current buffer"))
  12660. ((and (local-variable-p 'org-finish-function (current-buffer))
  12661. (fboundp org-finish-function))
  12662. (funcall org-finish-function))
  12663. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12664. ((org-at-property-p)
  12665. (call-interactively 'org-property-action))
  12666. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12667. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12668. ((org-at-table.el-p)
  12669. (require 'table)
  12670. (beginning-of-line 1)
  12671. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12672. (call-interactively 'table-recognize-table))
  12673. ((org-at-table-p)
  12674. (org-table-maybe-eval-formula)
  12675. (if arg
  12676. (call-interactively 'org-table-recalculate)
  12677. (org-table-maybe-recalculate-line))
  12678. (call-interactively 'org-table-align))
  12679. ((or (org-footnote-at-reference-p)
  12680. (org-footnote-at-definition-p))
  12681. (call-interactively 'org-footnote-action))
  12682. ((org-at-item-checkbox-p)
  12683. (call-interactively 'org-toggle-checkbox))
  12684. ((org-at-item-p)
  12685. (if arg
  12686. (call-interactively 'org-toggle-checkbox)
  12687. (call-interactively 'org-maybe-renumber-ordered-list)))
  12688. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12689. ;; Dynamic block
  12690. (beginning-of-line 1)
  12691. (save-excursion (org-update-dblock)))
  12692. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12693. (cond
  12694. ((equal (match-string 1) "TBLFM")
  12695. ;; Recalculate the table before this line
  12696. (save-excursion
  12697. (beginning-of-line 1)
  12698. (skip-chars-backward " \r\n\t")
  12699. (if (org-at-table-p)
  12700. (org-call-with-arg 'org-table-recalculate t))))
  12701. (t
  12702. ; (org-set-regexps-and-options)
  12703. ; (org-restart-font-lock)
  12704. (let ((org-inhibit-startup t)) (org-mode-restart))
  12705. (message "Local setup has been refreshed"))))
  12706. (t (error "C-c C-c can do nothing useful at this location.")))))
  12707. (defun org-mode-restart ()
  12708. "Restart Org-mode, to scan again for special lines.
  12709. Also updates the keyword regular expressions."
  12710. (interactive)
  12711. (org-mode)
  12712. (message "Org-mode restarted"))
  12713. (defun org-kill-note-or-show-branches ()
  12714. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12715. (interactive)
  12716. (if (not org-finish-function)
  12717. (call-interactively 'show-branches)
  12718. (let ((org-note-abort t))
  12719. (funcall org-finish-function))))
  12720. (defun org-return (&optional indent)
  12721. "Goto next table row or insert a newline.
  12722. Calls `org-table-next-row' or `newline', depending on context.
  12723. See the individual commands for more information."
  12724. (interactive)
  12725. (cond
  12726. ((bobp) (if indent (newline-and-indent) (newline)))
  12727. ((org-at-table-p)
  12728. (org-table-justify-field-maybe)
  12729. (call-interactively 'org-table-next-row))
  12730. ((and org-return-follows-link
  12731. (eq (get-text-property (point) 'face) 'org-link))
  12732. (call-interactively 'org-open-at-point))
  12733. ((and (org-at-heading-p)
  12734. (looking-at
  12735. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12736. (org-show-entry)
  12737. (end-of-line 1)
  12738. (newline))
  12739. (t (if indent (newline-and-indent) (newline)))))
  12740. (defun org-return-indent ()
  12741. "Goto next table row or insert a newline and indent.
  12742. Calls `org-table-next-row' or `newline-and-indent', depending on
  12743. context. See the individual commands for more information."
  12744. (interactive)
  12745. (org-return t))
  12746. (defun org-ctrl-c-star ()
  12747. "Compute table, or change heading status of lines.
  12748. Calls `org-table-recalculate' or `org-toggle-heading',
  12749. depending on context."
  12750. (interactive)
  12751. (cond
  12752. ((org-at-table-p)
  12753. (call-interactively 'org-table-recalculate))
  12754. (t
  12755. ;; Convert all lines in region to list items
  12756. (call-interactively 'org-toggle-heading))))
  12757. (defun org-ctrl-c-minus ()
  12758. "Insert separator line in table or modify bullet status of line.
  12759. Also turns a plain line or a region of lines into list items.
  12760. Calls `org-table-insert-hline', `org-toggle-item', or
  12761. `org-cycle-list-bullet', depending on context."
  12762. (interactive)
  12763. (cond
  12764. ((org-at-table-p)
  12765. (call-interactively 'org-table-insert-hline))
  12766. ((org-region-active-p)
  12767. (call-interactively 'org-toggle-item))
  12768. ((org-in-item-p)
  12769. (call-interactively 'org-cycle-list-bullet))
  12770. (t
  12771. (call-interactively 'org-toggle-item))))
  12772. (defun org-toggle-item ()
  12773. "Convert headings or normal lines to items, items to normal lines.
  12774. If there is no active region, only the current line is considered.
  12775. If the first line in the region is a headline, convert all headlines to items.
  12776. If the first line in the region is an item, convert all items to normal lines.
  12777. If the first line is normal text, add an item bullet to each line."
  12778. (interactive)
  12779. (let (l2 l beg end)
  12780. (if (org-region-active-p)
  12781. (setq beg (region-beginning) end (region-end))
  12782. (setq beg (point-at-bol)
  12783. end (min (1+ (point-at-eol)) (point-max))))
  12784. (save-excursion
  12785. (goto-char end)
  12786. (setq l2 (org-current-line))
  12787. (goto-char beg)
  12788. (beginning-of-line 1)
  12789. (setq l (1- (org-current-line)))
  12790. (if (org-at-item-p)
  12791. ;; We already have items, de-itemize
  12792. (while (< (setq l (1+ l)) l2)
  12793. (when (org-at-item-p)
  12794. (goto-char (match-beginning 2))
  12795. (delete-region (match-beginning 2) (match-end 2))
  12796. (and (looking-at "[ \t]+") (replace-match "")))
  12797. (beginning-of-line 2))
  12798. (if (org-on-heading-p)
  12799. ;; Headings, convert to items
  12800. (while (< (setq l (1+ l)) l2)
  12801. (if (looking-at org-outline-regexp)
  12802. (replace-match "- " t t))
  12803. (beginning-of-line 2))
  12804. ;; normal lines, turn them into items
  12805. (while (< (setq l (1+ l)) l2)
  12806. (unless (org-at-item-p)
  12807. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12808. (replace-match "\\1- \\2")))
  12809. (beginning-of-line 2)))))))
  12810. (defun org-toggle-heading (&optional nstars)
  12811. "Convert headings to normal text, or items or text to headings.
  12812. If there is no active region, only the current line is considered.
  12813. If the first line is a heading, remove the stars from all headlines
  12814. in the region.
  12815. If the first line is a plain list item, turn all plain list items into
  12816. headings.
  12817. If the first line is a normal line, turn each and every line in the region
  12818. into a heading.
  12819. When converting a line into a heading, the number of stars is chosen
  12820. such that the lines become children of the current entry. However, when
  12821. a prefix argument is given, its value determines the number of stars to add."
  12822. (interactive "P")
  12823. (let (l2 l itemp beg end)
  12824. (if (org-region-active-p)
  12825. (setq beg (region-beginning) end (region-end))
  12826. (setq beg (point-at-bol)
  12827. end (min (1+ (point-at-eol)) (point-max))))
  12828. (save-excursion
  12829. (goto-char end)
  12830. (setq l2 (org-current-line))
  12831. (goto-char beg)
  12832. (beginning-of-line 1)
  12833. (setq l (1- (org-current-line)))
  12834. (if (org-on-heading-p)
  12835. ;; We already have headlines, de-star them
  12836. (while (< (setq l (1+ l)) l2)
  12837. (when (org-on-heading-p t)
  12838. (and (looking-at outline-regexp) (replace-match "")))
  12839. (beginning-of-line 2))
  12840. (setq itemp (org-at-item-p))
  12841. (let* ((stars
  12842. (if nstars
  12843. (make-string (prefix-numeric-value current-prefix-arg)
  12844. ?*)
  12845. (save-excursion
  12846. (re-search-backward org-complex-heading-regexp nil t)
  12847. (or (match-string 1) "*"))))
  12848. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12849. (rpl (concat stars add-stars " ")))
  12850. (while (< (setq l (1+ l)) l2)
  12851. (if itemp
  12852. (and (org-at-item-p) (replace-match rpl t t))
  12853. (unless (org-on-heading-p)
  12854. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12855. (replace-match (concat rpl (match-string 2))))))
  12856. (beginning-of-line 2)))))))
  12857. (defun org-meta-return (&optional arg)
  12858. "Insert a new heading or wrap a region in a table.
  12859. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12860. See the individual commands for more information."
  12861. (interactive "P")
  12862. (cond
  12863. ((run-hook-with-args-until-success 'org-metareturn-hook))
  12864. ((org-at-table-p)
  12865. (call-interactively 'org-table-wrap-region))
  12866. (t (call-interactively 'org-insert-heading))))
  12867. ;;; Menu entries
  12868. ;; Define the Org-mode menus
  12869. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12870. '("Tbl"
  12871. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12872. ["Next Field" org-cycle (org-at-table-p)]
  12873. ["Previous Field" org-shifttab (org-at-table-p)]
  12874. ["Next Row" org-return (org-at-table-p)]
  12875. "--"
  12876. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12877. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12878. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12879. "--"
  12880. ("Column"
  12881. ["Move Column Left" org-metaleft (org-at-table-p)]
  12882. ["Move Column Right" org-metaright (org-at-table-p)]
  12883. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12884. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12885. ("Row"
  12886. ["Move Row Up" org-metaup (org-at-table-p)]
  12887. ["Move Row Down" org-metadown (org-at-table-p)]
  12888. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12889. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12890. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12891. "--"
  12892. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12893. ("Rectangle"
  12894. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12895. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12896. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12897. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12898. "--"
  12899. ("Calculate"
  12900. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12901. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12902. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12903. "--"
  12904. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12905. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12906. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12907. "--"
  12908. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12909. "--"
  12910. ["Sum Column/Rectangle" org-table-sum
  12911. (or (org-at-table-p) (org-region-active-p))]
  12912. ["Which Column?" org-table-current-column (org-at-table-p)])
  12913. ["Debug Formulas"
  12914. org-table-toggle-formula-debugger
  12915. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12916. ["Show Col/Row Numbers"
  12917. org-table-toggle-coordinate-overlays
  12918. :style toggle
  12919. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12920. "--"
  12921. ["Create" org-table-create (and (not (org-at-table-p))
  12922. org-enable-table-editor)]
  12923. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12924. ["Import from File" org-table-import (not (org-at-table-p))]
  12925. ["Export to File" org-table-export (org-at-table-p)]
  12926. "--"
  12927. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12928. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12929. '("Org"
  12930. ("Show/Hide"
  12931. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12932. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12933. ["Sparse Tree..." org-sparse-tree t]
  12934. ["Reveal Context" org-reveal t]
  12935. ["Show All" show-all t]
  12936. "--"
  12937. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12938. "--"
  12939. ["New Heading" org-insert-heading t]
  12940. ("Navigate Headings"
  12941. ["Up" outline-up-heading t]
  12942. ["Next" outline-next-visible-heading t]
  12943. ["Previous" outline-previous-visible-heading t]
  12944. ["Next Same Level" outline-forward-same-level t]
  12945. ["Previous Same Level" outline-backward-same-level t]
  12946. "--"
  12947. ["Jump" org-goto t])
  12948. ("Edit Structure"
  12949. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12950. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12951. "--"
  12952. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12953. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12954. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12955. "--"
  12956. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12957. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12958. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12959. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12960. "--"
  12961. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12962. "--"
  12963. ["Convert to odd levels" org-convert-to-odd-levels t]
  12964. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12965. ("Editing"
  12966. ["Emphasis..." org-emphasize t]
  12967. ["Edit Source Example" org-edit-special t]
  12968. "--"
  12969. ["Footnote new/jump" org-footnote-action t]
  12970. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12971. ("Archive"
  12972. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12973. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12974. ; :active t :keys "C-u C-c C-x C-a"]
  12975. ["Sparse trees open ARCHIVE trees"
  12976. (setq org-sparse-tree-open-archived-trees
  12977. (not org-sparse-tree-open-archived-trees))
  12978. :style toggle :selected org-sparse-tree-open-archived-trees]
  12979. ["Cycling opens ARCHIVE trees"
  12980. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12981. :style toggle :selected org-cycle-open-archived-trees]
  12982. "--"
  12983. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12984. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12985. ; ["Check and Move Children" (org-archive-subtree '(4))
  12986. ; :active t :keys "C-u C-c C-x C-s"]
  12987. )
  12988. "--"
  12989. ("TODO Lists"
  12990. ["TODO/DONE/-" org-todo t]
  12991. ("Select keyword"
  12992. ["Next keyword" org-shiftright (org-on-heading-p)]
  12993. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12994. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12995. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12996. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12997. ["Show TODO Tree" org-show-todo-tree t]
  12998. ["Global TODO list" org-todo-list t]
  12999. "--"
  13000. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13001. :selected org-enforce-todo-dependencies :style toggle :active t]
  13002. "Settings for tree at point"
  13003. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13004. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13005. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13006. ["Do Children parallel" org-toggle-ordered-property :style radio
  13007. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13008. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13009. "--"
  13010. ["Set Priority" org-priority t]
  13011. ["Priority Up" org-shiftup t]
  13012. ["Priority Down" org-shiftdown t])
  13013. ("TAGS and Properties"
  13014. ["Set Tags" org-set-tags-command t]
  13015. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13016. "--"
  13017. ["Set property" org-set-property t]
  13018. ["Column view of properties" org-columns t]
  13019. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13020. ("Dates and Scheduling"
  13021. ["Timestamp" org-time-stamp t]
  13022. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13023. ("Change Date"
  13024. ["1 Day Later" org-shiftright t]
  13025. ["1 Day Earlier" org-shiftleft t]
  13026. ["1 ... Later" org-shiftup t]
  13027. ["1 ... Earlier" org-shiftdown t])
  13028. ["Compute Time Range" org-evaluate-time-range t]
  13029. ["Schedule Item" org-schedule t]
  13030. ["Deadline" org-deadline t]
  13031. "--"
  13032. ["Custom time format" org-toggle-time-stamp-overlays
  13033. :style radio :selected org-display-custom-times]
  13034. "--"
  13035. ["Goto Calendar" org-goto-calendar t]
  13036. ["Date from Calendar" org-date-from-calendar t]
  13037. "--"
  13038. ["Start/Restart Timer" org-timer-start t]
  13039. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13040. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13041. ["Insert Timer String" org-timer t]
  13042. ["Insert Timer Item" org-timer-item t])
  13043. ("Logging work"
  13044. ["Clock in" org-clock-in t]
  13045. ["Clock out" org-clock-out t]
  13046. ["Clock cancel" org-clock-cancel t]
  13047. ["Goto running clock" org-clock-goto t]
  13048. ["Display times" org-clock-display t]
  13049. ["Create clock table" org-clock-report t]
  13050. "--"
  13051. ["Record DONE time"
  13052. (progn (setq org-log-done (not org-log-done))
  13053. (message "Switching to %s will %s record a timestamp"
  13054. (car org-done-keywords)
  13055. (if org-log-done "automatically" "not")))
  13056. :style toggle :selected org-log-done])
  13057. "--"
  13058. ["Agenda Command..." org-agenda t]
  13059. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13060. ("File List for Agenda")
  13061. ("Special views current file"
  13062. ["TODO Tree" org-show-todo-tree t]
  13063. ["Check Deadlines" org-check-deadlines t]
  13064. ["Timeline" org-timeline t]
  13065. ["Tags Tree" org-tags-sparse-tree t])
  13066. "--"
  13067. ("Hyperlinks"
  13068. ["Store Link (Global)" org-store-link t]
  13069. ["Insert Link" org-insert-link t]
  13070. ["Follow Link" org-open-at-point t]
  13071. "--"
  13072. ["Next link" org-next-link t]
  13073. ["Previous link" org-previous-link t]
  13074. "--"
  13075. ["Descriptive Links"
  13076. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13077. :style radio
  13078. :selected (member '(org-link) buffer-invisibility-spec)]
  13079. ["Literal Links"
  13080. (progn
  13081. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13082. :style radio
  13083. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13084. "--"
  13085. ["Export/Publish..." org-export t]
  13086. ("LaTeX"
  13087. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13088. :selected org-cdlatex-mode]
  13089. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13090. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13091. ["Modify math symbol" org-cdlatex-math-modify
  13092. (org-inside-LaTeX-fragment-p)]
  13093. ["Export LaTeX fragments as images"
  13094. (if (featurep 'org-exp)
  13095. (setq org-export-with-LaTeX-fragments
  13096. (not org-export-with-LaTeX-fragments))
  13097. (require 'org-exp))
  13098. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13099. org-export-with-LaTeX-fragments)])
  13100. "--"
  13101. ("Documentation"
  13102. ["Show Version" org-version t]
  13103. ["Info Documentation" org-info t])
  13104. ("Customize"
  13105. ["Browse Org Group" org-customize t]
  13106. "--"
  13107. ["Expand This Menu" org-create-customize-menu
  13108. (fboundp 'customize-menu-create)])
  13109. "--"
  13110. ("Refresh/Reload"
  13111. ["Refresh setup current buffer" org-mode-restart t]
  13112. ["Reload Org (after update)" org-reload t]
  13113. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13114. ))
  13115. (defun org-info (&optional node)
  13116. "Read documentation for Org-mode in the info system.
  13117. With optional NODE, go directly to that node."
  13118. (interactive)
  13119. (info (format "(org)%s" (or node ""))))
  13120. (defun org-install-agenda-files-menu ()
  13121. (let ((bl (buffer-list)))
  13122. (save-excursion
  13123. (while bl
  13124. (set-buffer (pop bl))
  13125. (if (org-mode-p) (setq bl nil)))
  13126. (when (org-mode-p)
  13127. (easy-menu-change
  13128. '("Org") "File List for Agenda"
  13129. (append
  13130. (list
  13131. ["Edit File List" (org-edit-agenda-file-list) t]
  13132. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13133. ["Remove Current File from List" org-remove-file t]
  13134. ["Cycle through agenda files" org-cycle-agenda-files t]
  13135. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13136. "--")
  13137. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13138. ;;;; Documentation
  13139. ;;;###autoload
  13140. (defun org-require-autoloaded-modules ()
  13141. (interactive)
  13142. (mapc 'require
  13143. '(org-agenda org-archive org-attach org-clock org-colview
  13144. org-exp org-id org-export-latex org-publish
  13145. org-remember org-table org-timer)))
  13146. ;;;###autoload
  13147. (defun org-reload (&optional uncompiled)
  13148. "Reload all org lisp files.
  13149. With prefix arg UNCOMPILED, load the uncompiled versions."
  13150. (interactive "P")
  13151. (require 'find-func)
  13152. (let* ((dir (file-name-directory (find-library-name "org")))
  13153. (files (directory-files dir t "\\.el\\'"))
  13154. (remove-re (concat (if (featurep 'xemacs)
  13155. "org-colview" "org-colview-xemacs")
  13156. "\\'")))
  13157. (setq files (mapcar 'file-name-sans-extension files))
  13158. (setq files (mapcar
  13159. (lambda (x) (if (string-match remove-re x) nil x))
  13160. files))
  13161. (setq files (delq nil files))
  13162. (mapc
  13163. (lambda (f)
  13164. (if (and (not uncompiled)
  13165. (file-exists-p (concat f ".elc")))
  13166. (load (concat f ".elc") nil nil t)
  13167. (load (concat f ".el") nil nil t)))
  13168. files)))
  13169. ;;;###autoload
  13170. (defun org-customize ()
  13171. "Call the customize function with org as argument."
  13172. (interactive)
  13173. (org-load-modules-maybe)
  13174. (org-require-autoloaded-modules)
  13175. (customize-browse 'org))
  13176. (defun org-create-customize-menu ()
  13177. "Create a full customization menu for Org-mode, insert it into the menu."
  13178. (interactive)
  13179. (org-load-modules-maybe)
  13180. (org-require-autoloaded-modules)
  13181. (if (fboundp 'customize-menu-create)
  13182. (progn
  13183. (easy-menu-change
  13184. '("Org") "Customize"
  13185. `(["Browse Org group" org-customize t]
  13186. "--"
  13187. ,(customize-menu-create 'org)
  13188. ["Set" Custom-set t]
  13189. ["Save" Custom-save t]
  13190. ["Reset to Current" Custom-reset-current t]
  13191. ["Reset to Saved" Custom-reset-saved t]
  13192. ["Reset to Standard Settings" Custom-reset-standard t]))
  13193. (message "\"Org\"-menu now contains full customization menu"))
  13194. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13195. ;;;; Miscellaneous stuff
  13196. ;;; Generally useful functions
  13197. (defun org-find-text-property-in-string (prop s)
  13198. "Return the first non-nil value of property PROP in string S."
  13199. (or (get-text-property 0 prop s)
  13200. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13201. prop s)))
  13202. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13203. "Display the given MESSAGE as a warning."
  13204. (if (fboundp 'display-warning)
  13205. (display-warning 'org message
  13206. (if (featurep 'xemacs)
  13207. 'warning
  13208. :warning))
  13209. (let ((buf (get-buffer-create "*Org warnings*")))
  13210. (with-current-buffer buf
  13211. (goto-char (point-max))
  13212. (insert "Warning (Org): " message)
  13213. (unless (bolp)
  13214. (newline)))
  13215. (display-buffer buf)
  13216. (sit-for 0))))
  13217. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13218. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13219. (if (and marker (marker-buffer marker)
  13220. (buffer-live-p (marker-buffer marker)))
  13221. (progn
  13222. (switch-to-buffer (marker-buffer marker))
  13223. (if (or (> marker (point-max)) (< marker (point-min)))
  13224. (widen))
  13225. (goto-char marker)
  13226. (org-show-context 'org-goto))
  13227. (if bookmark
  13228. (bookmark-jump bookmark)
  13229. (error "Cannot find location"))))
  13230. (defun org-quote-csv-field (s)
  13231. "Quote field for inclusion in CSV material."
  13232. (if (string-match "[\",]" s)
  13233. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13234. s))
  13235. (defun org-plist-delete (plist property)
  13236. "Delete PROPERTY from PLIST.
  13237. This is in contrast to merely setting it to 0."
  13238. (let (p)
  13239. (while plist
  13240. (if (not (eq property (car plist)))
  13241. (setq p (plist-put p (car plist) (nth 1 plist))))
  13242. (setq plist (cddr plist)))
  13243. p))
  13244. (defun org-force-self-insert (N)
  13245. "Needed to enforce self-insert under remapping."
  13246. (interactive "p")
  13247. (self-insert-command N))
  13248. (defun org-string-width (s)
  13249. "Compute width of string, ignoring invisible characters.
  13250. This ignores character with invisibility property `org-link', and also
  13251. characters with property `org-cwidth', because these will become invisible
  13252. upon the next fontification round."
  13253. (let (b l)
  13254. (when (or (eq t buffer-invisibility-spec)
  13255. (assq 'org-link buffer-invisibility-spec))
  13256. (while (setq b (text-property-any 0 (length s)
  13257. 'invisible 'org-link s))
  13258. (setq s (concat (substring s 0 b)
  13259. (substring s (or (next-single-property-change
  13260. b 'invisible s) (length s)))))))
  13261. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13262. (setq s (concat (substring s 0 b)
  13263. (substring s (or (next-single-property-change
  13264. b 'org-cwidth s) (length s))))))
  13265. (setq l (string-width s) b -1)
  13266. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13267. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13268. l))
  13269. (defun org-get-indentation (&optional line)
  13270. "Get the indentation of the current line, interpreting tabs.
  13271. When LINE is given, assume it represents a line and compute its indentation."
  13272. (if line
  13273. (if (string-match "^ *" (org-remove-tabs line))
  13274. (match-end 0))
  13275. (save-excursion
  13276. (beginning-of-line 1)
  13277. (skip-chars-forward " \t")
  13278. (current-column))))
  13279. (defun org-remove-tabs (s &optional width)
  13280. "Replace tabulators in S with spaces.
  13281. Assumes that s is a single line, starting in column 0."
  13282. (setq width (or width tab-width))
  13283. (while (string-match "\t" s)
  13284. (setq s (replace-match
  13285. (make-string
  13286. (- (* width (/ (+ (match-beginning 0) width) width))
  13287. (match-beginning 0)) ?\ )
  13288. t t s)))
  13289. s)
  13290. (defun org-fix-indentation (line ind)
  13291. "Fix indentation in LINE.
  13292. IND is a cons cell with target and minimum indentation.
  13293. If the current indentation in LINE is smaller than the minimum,
  13294. leave it alone. If it is larger than ind, set it to the target."
  13295. (let* ((l (org-remove-tabs line))
  13296. (i (org-get-indentation l))
  13297. (i1 (car ind)) (i2 (cdr ind)))
  13298. (if (>= i i2) (setq l (substring line i2)))
  13299. (if (> i1 0)
  13300. (concat (make-string i1 ?\ ) l)
  13301. l)))
  13302. (defun org-base-buffer (buffer)
  13303. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13304. (if (not buffer)
  13305. buffer
  13306. (or (buffer-base-buffer buffer)
  13307. buffer)))
  13308. (defun org-trim (s)
  13309. "Remove whitespace at beginning and end of string."
  13310. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13311. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13312. s)
  13313. (defun org-wrap (string &optional width lines)
  13314. "Wrap string to either a number of lines, or a width in characters.
  13315. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13316. that costs. If there is a word longer than WIDTH, the text is actually
  13317. wrapped to the length of that word.
  13318. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13319. many lines, whatever width that takes.
  13320. The return value is a list of lines, without newlines at the end."
  13321. (let* ((words (org-split-string string "[ \t\n]+"))
  13322. (maxword (apply 'max (mapcar 'org-string-width words)))
  13323. w ll)
  13324. (cond (width
  13325. (org-do-wrap words (max maxword width)))
  13326. (lines
  13327. (setq w maxword)
  13328. (setq ll (org-do-wrap words maxword))
  13329. (if (<= (length ll) lines)
  13330. ll
  13331. (setq ll words)
  13332. (while (> (length ll) lines)
  13333. (setq w (1+ w))
  13334. (setq ll (org-do-wrap words w)))
  13335. ll))
  13336. (t (error "Cannot wrap this")))))
  13337. (defun org-do-wrap (words width)
  13338. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13339. (let (lines line)
  13340. (while words
  13341. (setq line (pop words))
  13342. (while (and words (< (+ (length line) (length (car words))) width))
  13343. (setq line (concat line " " (pop words))))
  13344. (setq lines (push line lines)))
  13345. (nreverse lines)))
  13346. (defun org-split-string (string &optional separators)
  13347. "Splits STRING into substrings at SEPARATORS.
  13348. No empty strings are returned if there are matches at the beginning
  13349. and end of string."
  13350. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13351. (start 0)
  13352. notfirst
  13353. (list nil))
  13354. (while (and (string-match rexp string
  13355. (if (and notfirst
  13356. (= start (match-beginning 0))
  13357. (< start (length string)))
  13358. (1+ start) start))
  13359. (< (match-beginning 0) (length string)))
  13360. (setq notfirst t)
  13361. (or (eq (match-beginning 0) 0)
  13362. (and (eq (match-beginning 0) (match-end 0))
  13363. (eq (match-beginning 0) start))
  13364. (setq list
  13365. (cons (substring string start (match-beginning 0))
  13366. list)))
  13367. (setq start (match-end 0)))
  13368. (or (eq start (length string))
  13369. (setq list
  13370. (cons (substring string start)
  13371. list)))
  13372. (nreverse list)))
  13373. (defun org-context ()
  13374. "Return a list of contexts of the current cursor position.
  13375. If several contexts apply, all are returned.
  13376. Each context entry is a list with a symbol naming the context, and
  13377. two positions indicating start and end of the context. Possible
  13378. contexts are:
  13379. :headline anywhere in a headline
  13380. :headline-stars on the leading stars in a headline
  13381. :todo-keyword on a TODO keyword (including DONE) in a headline
  13382. :tags on the TAGS in a headline
  13383. :priority on the priority cookie in a headline
  13384. :item on the first line of a plain list item
  13385. :item-bullet on the bullet/number of a plain list item
  13386. :checkbox on the checkbox in a plain list item
  13387. :table in an org-mode table
  13388. :table-special on a special filed in a table
  13389. :table-table in a table.el table
  13390. :link on a hyperlink
  13391. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13392. :target on a <<target>>
  13393. :radio-target on a <<<radio-target>>>
  13394. :latex-fragment on a LaTeX fragment
  13395. :latex-preview on a LaTeX fragment with overlayed preview image
  13396. This function expects the position to be visible because it uses font-lock
  13397. faces as a help to recognize the following contexts: :table-special, :link,
  13398. and :keyword."
  13399. (let* ((f (get-text-property (point) 'face))
  13400. (faces (if (listp f) f (list f)))
  13401. (p (point)) clist o)
  13402. ;; First the large context
  13403. (cond
  13404. ((org-on-heading-p t)
  13405. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13406. (when (progn
  13407. (beginning-of-line 1)
  13408. (looking-at org-todo-line-tags-regexp))
  13409. (push (org-point-in-group p 1 :headline-stars) clist)
  13410. (push (org-point-in-group p 2 :todo-keyword) clist)
  13411. (push (org-point-in-group p 4 :tags) clist))
  13412. (goto-char p)
  13413. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13414. (if (looking-at "\\[#[A-Z0-9]\\]")
  13415. (push (org-point-in-group p 0 :priority) clist)))
  13416. ((org-at-item-p)
  13417. (push (org-point-in-group p 2 :item-bullet) clist)
  13418. (push (list :item (point-at-bol)
  13419. (save-excursion (org-end-of-item) (point)))
  13420. clist)
  13421. (and (org-at-item-checkbox-p)
  13422. (push (org-point-in-group p 0 :checkbox) clist)))
  13423. ((org-at-table-p)
  13424. (push (list :table (org-table-begin) (org-table-end)) clist)
  13425. (if (memq 'org-formula faces)
  13426. (push (list :table-special
  13427. (previous-single-property-change p 'face)
  13428. (next-single-property-change p 'face)) clist)))
  13429. ((org-at-table-p 'any)
  13430. (push (list :table-table) clist)))
  13431. (goto-char p)
  13432. ;; Now the small context
  13433. (cond
  13434. ((org-at-timestamp-p)
  13435. (push (org-point-in-group p 0 :timestamp) clist))
  13436. ((memq 'org-link faces)
  13437. (push (list :link
  13438. (previous-single-property-change p 'face)
  13439. (next-single-property-change p 'face)) clist))
  13440. ((memq 'org-special-keyword faces)
  13441. (push (list :keyword
  13442. (previous-single-property-change p 'face)
  13443. (next-single-property-change p 'face)) clist))
  13444. ((org-on-target-p)
  13445. (push (org-point-in-group p 0 :target) clist)
  13446. (goto-char (1- (match-beginning 0)))
  13447. (if (looking-at org-radio-target-regexp)
  13448. (push (org-point-in-group p 0 :radio-target) clist))
  13449. (goto-char p))
  13450. ((setq o (car (delq nil
  13451. (mapcar
  13452. (lambda (x)
  13453. (if (memq x org-latex-fragment-image-overlays) x))
  13454. (org-overlays-at (point))))))
  13455. (push (list :latex-fragment
  13456. (org-overlay-start o) (org-overlay-end o)) clist)
  13457. (push (list :latex-preview
  13458. (org-overlay-start o) (org-overlay-end o)) clist))
  13459. ((org-inside-LaTeX-fragment-p)
  13460. ;; FIXME: positions wrong.
  13461. (push (list :latex-fragment (point) (point)) clist)))
  13462. (setq clist (nreverse (delq nil clist)))
  13463. clist))
  13464. ;; FIXME: Compare with at-regexp-p Do we need both?
  13465. (defun org-in-regexp (re &optional nlines visually)
  13466. "Check if point is inside a match of regexp.
  13467. Normally only the current line is checked, but you can include NLINES extra
  13468. lines both before and after point into the search.
  13469. If VISUALLY is set, require that the cursor is not after the match but
  13470. really on, so that the block visually is on the match."
  13471. (catch 'exit
  13472. (let ((pos (point))
  13473. (eol (point-at-eol (+ 1 (or nlines 0))))
  13474. (inc (if visually 1 0)))
  13475. (save-excursion
  13476. (beginning-of-line (- 1 (or nlines 0)))
  13477. (while (re-search-forward re eol t)
  13478. (if (and (<= (match-beginning 0) pos)
  13479. (>= (+ inc (match-end 0)) pos))
  13480. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13481. (defun org-at-regexp-p (regexp)
  13482. "Is point inside a match of REGEXP in the current line?"
  13483. (catch 'exit
  13484. (save-excursion
  13485. (let ((pos (point)) (end (point-at-eol)))
  13486. (beginning-of-line 1)
  13487. (while (re-search-forward regexp end t)
  13488. (if (and (<= (match-beginning 0) pos)
  13489. (>= (match-end 0) pos))
  13490. (throw 'exit t)))
  13491. nil))))
  13492. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13493. "Call `multi-occur' with buffers for all agenda files."
  13494. (interactive "sOrg-files matching: \np")
  13495. (let* ((files (org-agenda-files))
  13496. (tnames (mapcar 'file-truename files))
  13497. (extra org-agenda-text-search-extra-files)
  13498. f)
  13499. (when (eq (car extra) 'agenda-archives)
  13500. (setq extra (cdr extra))
  13501. (setq files (org-add-archive-files files)))
  13502. (while (setq f (pop extra))
  13503. (unless (member (file-truename f) tnames)
  13504. (add-to-list 'files f 'append)
  13505. (add-to-list 'tnames (file-truename f) 'append)))
  13506. (multi-occur
  13507. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13508. regexp)))
  13509. (if (boundp 'occur-mode-find-occurrence-hook)
  13510. ;; Emacs 23
  13511. (add-hook 'occur-mode-find-occurrence-hook
  13512. (lambda ()
  13513. (when (org-mode-p)
  13514. (org-reveal))))
  13515. ;; Emacs 22
  13516. (defadvice occur-mode-goto-occurrence
  13517. (after org-occur-reveal activate)
  13518. (and (org-mode-p) (org-reveal)))
  13519. (defadvice occur-mode-goto-occurrence-other-window
  13520. (after org-occur-reveal activate)
  13521. (and (org-mode-p) (org-reveal)))
  13522. (defadvice occur-mode-display-occurrence
  13523. (after org-occur-reveal activate)
  13524. (when (org-mode-p)
  13525. (let ((pos (occur-mode-find-occurrence)))
  13526. (with-current-buffer (marker-buffer pos)
  13527. (save-excursion
  13528. (goto-char pos)
  13529. (org-reveal)))))))
  13530. (defun org-uniquify (list)
  13531. "Remove duplicate elements from LIST."
  13532. (let (res)
  13533. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13534. res))
  13535. (defun org-delete-all (elts list)
  13536. "Remove all elements in ELTS from LIST."
  13537. (while elts
  13538. (setq list (delete (pop elts) list)))
  13539. list)
  13540. (defun org-back-over-empty-lines ()
  13541. "Move backwards over whitespace, to the beginning of the first empty line.
  13542. Returns the number of empty lines passed."
  13543. (let ((pos (point)))
  13544. (skip-chars-backward " \t\n\r")
  13545. (beginning-of-line 2)
  13546. (goto-char (min (point) pos))
  13547. (count-lines (point) pos)))
  13548. (defun org-skip-whitespace ()
  13549. (skip-chars-forward " \t\n\r"))
  13550. (defun org-point-in-group (point group &optional context)
  13551. "Check if POINT is in match-group GROUP.
  13552. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13553. match. If the match group does ot exist or point is not inside it,
  13554. return nil."
  13555. (and (match-beginning group)
  13556. (>= point (match-beginning group))
  13557. (<= point (match-end group))
  13558. (if context
  13559. (list context (match-beginning group) (match-end group))
  13560. t)))
  13561. (defun org-switch-to-buffer-other-window (&rest args)
  13562. "Switch to buffer in a second window on the current frame.
  13563. In particular, do not allow pop-up frames."
  13564. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13565. special-display-function)
  13566. (apply 'switch-to-buffer-other-window args)))
  13567. (defun org-combine-plists (&rest plists)
  13568. "Create a single property list from all plists in PLISTS.
  13569. The process starts by copying the first list, and then setting properties
  13570. from the other lists. Settings in the last list are the most significant
  13571. ones and overrule settings in the other lists."
  13572. (let ((rtn (copy-sequence (pop plists)))
  13573. p v ls)
  13574. (while plists
  13575. (setq ls (pop plists))
  13576. (while ls
  13577. (setq p (pop ls) v (pop ls))
  13578. (setq rtn (plist-put rtn p v))))
  13579. rtn))
  13580. (defun org-move-line-down (arg)
  13581. "Move the current line down. With prefix argument, move it past ARG lines."
  13582. (interactive "p")
  13583. (let ((col (current-column))
  13584. beg end pos)
  13585. (beginning-of-line 1) (setq beg (point))
  13586. (beginning-of-line 2) (setq end (point))
  13587. (beginning-of-line (+ 1 arg))
  13588. (setq pos (move-marker (make-marker) (point)))
  13589. (insert (delete-and-extract-region beg end))
  13590. (goto-char pos)
  13591. (org-move-to-column col)))
  13592. (defun org-move-line-up (arg)
  13593. "Move the current line up. With prefix argument, move it past ARG lines."
  13594. (interactive "p")
  13595. (let ((col (current-column))
  13596. beg end pos)
  13597. (beginning-of-line 1) (setq beg (point))
  13598. (beginning-of-line 2) (setq end (point))
  13599. (beginning-of-line (- arg))
  13600. (setq pos (move-marker (make-marker) (point)))
  13601. (insert (delete-and-extract-region beg end))
  13602. (goto-char pos)
  13603. (org-move-to-column col)))
  13604. (defun org-replace-escapes (string table)
  13605. "Replace %-escapes in STRING with values in TABLE.
  13606. TABLE is an association list with keys like \"%a\" and string values.
  13607. The sequences in STRING may contain normal field width and padding information,
  13608. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13609. so values can contain further %-escapes if they are define later in TABLE."
  13610. (let ((case-fold-search nil)
  13611. e re rpl)
  13612. (while (setq e (pop table))
  13613. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13614. (while (string-match re string)
  13615. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13616. (cdr e)))
  13617. (setq string (replace-match rpl t t string))))
  13618. string))
  13619. (defun org-sublist (list start end)
  13620. "Return a section of LIST, from START to END.
  13621. Counting starts at 1."
  13622. (let (rtn (c start))
  13623. (setq list (nthcdr (1- start) list))
  13624. (while (and list (<= c end))
  13625. (push (pop list) rtn)
  13626. (setq c (1+ c)))
  13627. (nreverse rtn)))
  13628. (defun org-find-base-buffer-visiting (file)
  13629. "Like `find-buffer-visiting' but alway return the base buffer and
  13630. not an indirect buffer."
  13631. (let ((buf (find-buffer-visiting file)))
  13632. (if buf
  13633. (or (buffer-base-buffer buf) buf)
  13634. nil)))
  13635. (defun org-image-file-name-regexp (&optional extensions)
  13636. "Return regexp matching the file names of images.
  13637. If EXTENSIONS is given, only match these."
  13638. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13639. (image-file-name-regexp)
  13640. (let ((image-file-name-extensions
  13641. (or extensions
  13642. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13643. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13644. (concat "\\."
  13645. (regexp-opt (nconc (mapcar 'upcase
  13646. image-file-name-extensions)
  13647. image-file-name-extensions)
  13648. t)
  13649. "\\'"))))
  13650. (defun org-file-image-p (file &optional extensions)
  13651. "Return non-nil if FILE is an image."
  13652. (save-match-data
  13653. (string-match (org-image-file-name-regexp extensions) file)))
  13654. (defun org-get-cursor-date ()
  13655. "Return the date at cursor in as a time.
  13656. This works in the calendar and in the agenda, anywhere else it just
  13657. returns the current time."
  13658. (let (date day defd)
  13659. (cond
  13660. ((eq major-mode 'calendar-mode)
  13661. (setq date (calendar-cursor-to-date)
  13662. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13663. ((eq major-mode 'org-agenda-mode)
  13664. (setq day (get-text-property (point) 'day))
  13665. (if day
  13666. (setq date (calendar-gregorian-from-absolute day)
  13667. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13668. (nth 2 date))))))
  13669. (or defd (current-time))))
  13670. (defvar org-agenda-action-marker (make-marker)
  13671. "Marker pointing to the entry for the next agenda action.")
  13672. (defun org-mark-entry-for-agenda-action ()
  13673. "Mark the current entry as target of an agenda action.
  13674. Agenda actions are actions executed from the agenda with the key `k',
  13675. which make use of the date at the cursor."
  13676. (interactive)
  13677. (move-marker org-agenda-action-marker
  13678. (save-excursion (org-back-to-heading t) (point))
  13679. (current-buffer))
  13680. (message
  13681. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13682. ;;; Paragraph filling stuff.
  13683. ;; We want this to be just right, so use the full arsenal.
  13684. (defun org-indent-line-function ()
  13685. "Indent line like previous, but further if previous was headline or item."
  13686. (interactive)
  13687. (let* ((pos (point))
  13688. (itemp (org-at-item-p))
  13689. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13690. column bpos bcol tpos tcol bullet btype bullet-type)
  13691. ;; Find the previous relevant line
  13692. (beginning-of-line 1)
  13693. (cond
  13694. ((looking-at "#") (setq column 0))
  13695. ((looking-at "\\*+ ") (setq column 0))
  13696. ((and (looking-at "[ \t]*:END:")
  13697. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13698. (save-excursion
  13699. (goto-char (1- (match-beginning 1)))
  13700. (setq column (current-column))))
  13701. (t
  13702. (beginning-of-line 0)
  13703. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13704. (not (looking-at "[ \t]*:END:"))
  13705. (not (looking-at org-drawer-regexp)))
  13706. (beginning-of-line 0))
  13707. (cond
  13708. ((looking-at "\\*+[ \t]+")
  13709. (if (not org-adapt-indentation)
  13710. (setq column 0)
  13711. (goto-char (match-end 0))
  13712. (setq column (current-column))))
  13713. ((looking-at org-drawer-regexp)
  13714. (goto-char (1- (match-beginning 1)))
  13715. (setq column (current-column)))
  13716. ((looking-at "\\([ \t]*\\):END:")
  13717. (goto-char (match-end 1))
  13718. (setq column (current-column)))
  13719. ((org-in-item-p)
  13720. (org-beginning-of-item)
  13721. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13722. (setq bpos (match-beginning 1) tpos (match-end 0)
  13723. bcol (progn (goto-char bpos) (current-column))
  13724. tcol (progn (goto-char tpos) (current-column))
  13725. bullet (match-string 1)
  13726. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13727. (if (> tcol (+ bcol org-description-max-indent))
  13728. (setq tcol (+ bcol 5)))
  13729. (if (not itemp)
  13730. (setq column tcol)
  13731. (goto-char pos)
  13732. (beginning-of-line 1)
  13733. (if (looking-at "\\S-")
  13734. (progn
  13735. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13736. (setq bullet (match-string 1)
  13737. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13738. (setq column (if (equal btype bullet-type) bcol tcol)))
  13739. (setq column (org-get-indentation)))))
  13740. (t (setq column (org-get-indentation))))))
  13741. (goto-char pos)
  13742. (if (<= (current-column) (current-indentation))
  13743. (org-indent-line-to column)
  13744. (save-excursion (org-indent-line-to column)))
  13745. (setq column (current-column))
  13746. (beginning-of-line 1)
  13747. (if (looking-at
  13748. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13749. (replace-match (concat "\\1" (format org-property-format
  13750. (match-string 2) (match-string 3)))
  13751. t nil))
  13752. (org-move-to-column column)))
  13753. (defun org-set-autofill-regexps ()
  13754. (interactive)
  13755. ;; In the paragraph separator we include headlines, because filling
  13756. ;; text in a line directly attached to a headline would otherwise
  13757. ;; fill the headline as well.
  13758. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13759. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13760. ;; The paragraph starter includes hand-formatted lists.
  13761. (org-set-local 'paragraph-start
  13762. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13763. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13764. ;; But only if the user has not turned off tables or fixed-width regions
  13765. (org-set-local
  13766. 'auto-fill-inhibit-regexp
  13767. (concat "\\*+ \\|#\\+"
  13768. "\\|[ \t]*" org-keyword-time-regexp
  13769. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13770. (concat
  13771. "\\|[ \t]*["
  13772. (if org-enable-table-editor "|" "")
  13773. (if org-enable-fixed-width-editor ":" "")
  13774. "]"))))
  13775. ;; We use our own fill-paragraph function, to make sure that tables
  13776. ;; and fixed-width regions are not wrapped. That function will pass
  13777. ;; through to `fill-paragraph' when appropriate.
  13778. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13779. ; Adaptive filling: To get full control, first make sure that
  13780. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13781. (org-set-local 'adaptive-fill-regexp "\000")
  13782. (org-set-local 'adaptive-fill-function
  13783. 'org-adaptive-fill-function)
  13784. (org-set-local
  13785. 'align-mode-rules-list
  13786. '((org-in-buffer-settings
  13787. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13788. (modes . '(org-mode))))))
  13789. (defun org-fill-paragraph (&optional justify)
  13790. "Re-align a table, pass through to fill-paragraph if no table."
  13791. (let ((table-p (org-at-table-p))
  13792. (table.el-p (org-at-table.el-p)))
  13793. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13794. (save-excursion (goto-char (point-at-bol))
  13795. (looking-at outline-regexp)))
  13796. t) ; skip headlines
  13797. (table.el-p t) ; skip table.el tables
  13798. (table-p (org-table-align) t) ; align org-mode tables
  13799. (t nil)))) ; call paragraph-fill
  13800. ;; For reference, this is the default value of adaptive-fill-regexp
  13801. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13802. (defun org-adaptive-fill-function ()
  13803. "Return a fill prefix for org-mode files.
  13804. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13805. work correctly."
  13806. (cond ((looking-at "#[ \t]+")
  13807. (match-string 0))
  13808. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13809. (save-excursion
  13810. (if (> (match-end 1) (+ (match-beginning 1)
  13811. org-description-max-indent))
  13812. (goto-char (+ (match-beginning 1) 5))
  13813. (goto-char (match-end 0)))
  13814. (make-string (current-column) ?\ )))
  13815. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13816. (save-excursion
  13817. (goto-char (match-end 0))
  13818. (make-string (current-column) ?\ )))
  13819. (t nil)))
  13820. ;;; Other stuff.
  13821. (defun org-toggle-fixed-width-section (arg)
  13822. "Toggle the fixed-width export.
  13823. If there is no active region, the QUOTE keyword at the current headline is
  13824. inserted or removed. When present, it causes the text between this headline
  13825. and the next to be exported as fixed-width text, and unmodified.
  13826. If there is an active region, this command adds or removes a colon as the
  13827. first character of this line. If the first character of a line is a colon,
  13828. this line is also exported in fixed-width font."
  13829. (interactive "P")
  13830. (let* ((cc 0)
  13831. (regionp (org-region-active-p))
  13832. (beg (if regionp (region-beginning) (point)))
  13833. (end (if regionp (region-end)))
  13834. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13835. (case-fold-search nil)
  13836. (re "[ \t]*\\(:\\)")
  13837. off)
  13838. (if regionp
  13839. (save-excursion
  13840. (goto-char beg)
  13841. (setq cc (current-column))
  13842. (beginning-of-line 1)
  13843. (setq off (looking-at re))
  13844. (while (> nlines 0)
  13845. (setq nlines (1- nlines))
  13846. (beginning-of-line 1)
  13847. (cond
  13848. (arg
  13849. (org-move-to-column cc t)
  13850. (insert ":\n")
  13851. (forward-line -1))
  13852. ((and off (looking-at re))
  13853. (replace-match "" t t nil 1))
  13854. ((not off) (org-move-to-column cc t) (insert ":")))
  13855. (forward-line 1)))
  13856. (save-excursion
  13857. (org-back-to-heading)
  13858. (if (looking-at (concat outline-regexp
  13859. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13860. (replace-match "" t t nil 1)
  13861. (if (looking-at outline-regexp)
  13862. (progn
  13863. (goto-char (match-end 0))
  13864. (insert org-quote-string " "))))))))
  13865. ;;;; Functions extending outline functionality
  13866. (defun org-beginning-of-line (&optional arg)
  13867. "Go to the beginning of the current line. If that is invisible, continue
  13868. to a visible line beginning. This makes the function of C-a more intuitive.
  13869. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13870. first attempt, and only move to after the tags when the cursor is already
  13871. beyond the end of the headline."
  13872. (interactive "P")
  13873. (let ((pos (point))
  13874. (special (if (consp org-special-ctrl-a/e)
  13875. (car org-special-ctrl-a/e)
  13876. org-special-ctrl-a/e))
  13877. refpos)
  13878. (beginning-of-line 1)
  13879. (if (and arg (fboundp 'move-beginning-of-line))
  13880. (call-interactively 'move-beginning-of-line)
  13881. (if (bobp)
  13882. nil
  13883. (backward-char 1)
  13884. (if (org-invisible-p)
  13885. (while (and (not (bobp)) (org-invisible-p))
  13886. (backward-char 1)
  13887. (beginning-of-line 1))
  13888. (forward-char 1))))
  13889. (when special
  13890. (cond
  13891. ((and (looking-at org-complex-heading-regexp)
  13892. (= (char-after (match-end 1)) ?\ ))
  13893. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13894. (point-at-eol)))
  13895. (goto-char
  13896. (if (eq special t)
  13897. (cond ((> pos refpos) refpos)
  13898. ((= pos (point)) refpos)
  13899. (t (point)))
  13900. (cond ((> pos (point)) (point))
  13901. ((not (eq last-command this-command)) (point))
  13902. (t refpos)))))
  13903. ((org-at-item-p)
  13904. (goto-char
  13905. (if (eq special t)
  13906. (cond ((> pos (match-end 4)) (match-end 4))
  13907. ((= pos (point)) (match-end 4))
  13908. (t (point)))
  13909. (cond ((> pos (point)) (point))
  13910. ((not (eq last-command this-command)) (point))
  13911. (t (match-end 4))))))))
  13912. (org-no-warnings
  13913. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13914. (defun org-end-of-line (&optional arg)
  13915. "Go to the end of the line.
  13916. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13917. first attempt, and only move to after the tags when the cursor is already
  13918. beyond the end of the headline."
  13919. (interactive "P")
  13920. (let ((special (if (consp org-special-ctrl-a/e)
  13921. (cdr org-special-ctrl-a/e)
  13922. org-special-ctrl-a/e)))
  13923. (if (or (not special)
  13924. (not (org-on-heading-p))
  13925. arg)
  13926. (call-interactively (if (fboundp 'move-end-of-line)
  13927. 'move-end-of-line
  13928. 'end-of-line))
  13929. (let ((pos (point)))
  13930. (beginning-of-line 1)
  13931. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13932. (if (eq special t)
  13933. (if (or (< pos (match-beginning 1))
  13934. (= pos (match-end 0)))
  13935. (goto-char (match-beginning 1))
  13936. (goto-char (match-end 0)))
  13937. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13938. (goto-char (match-end 0))
  13939. (goto-char (match-beginning 1))))
  13940. (call-interactively (if (fboundp 'move-end-of-line)
  13941. 'move-end-of-line
  13942. 'end-of-line)))))
  13943. (org-no-warnings
  13944. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13945. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13946. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13947. (defun org-kill-line (&optional arg)
  13948. "Kill line, to tags or end of line."
  13949. (interactive "P")
  13950. (cond
  13951. ((or (not org-special-ctrl-k)
  13952. (bolp)
  13953. (not (org-on-heading-p)))
  13954. (call-interactively 'kill-line))
  13955. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13956. (kill-region (point) (match-beginning 1))
  13957. (org-set-tags nil t))
  13958. (t (kill-region (point) (point-at-eol)))))
  13959. (define-key org-mode-map "\C-k" 'org-kill-line)
  13960. (defun org-yank (&optional arg)
  13961. "Yank. If the kill is a subtree, treat it specially.
  13962. This command will look at the current kill and check if is a single
  13963. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13964. cursor is at the beginning of a line or after the stars of a currently
  13965. empty headline, then the yank is handled specially. How exactly depends
  13966. on the value of the following variables, both set by default.
  13967. org-yank-folded-subtrees
  13968. When set, the subtree(s) will be folded after insertion, but only
  13969. if doing so would now swallow text after the yanked text.
  13970. org-yank-adjusted-subtrees
  13971. When set, the subtree will be promoted or demoted in order to
  13972. fit into the local outline tree structure, which means that the level
  13973. will be adjusted so that it becomes the smaller one of the two
  13974. *visible* surrounding headings.
  13975. Any prefix to this command will cause `yank' to be called directly with
  13976. no special treatment. In particular, a simple `C-u' prefix will just
  13977. plainly yank the text as it is.
  13978. \[1] Basically, the test checks if the first non-white line is a heading
  13979. and if there are no other headings with fewer stars."
  13980. (interactive "P")
  13981. (setq this-command 'yank)
  13982. (if arg
  13983. (call-interactively 'yank)
  13984. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13985. (and (org-kill-is-subtree-p)
  13986. (or (bolp)
  13987. (and (looking-at "[ \t]*$")
  13988. (string-match
  13989. "\\`\\*+\\'"
  13990. (buffer-substring (point-at-bol) (point)))))))
  13991. swallowp)
  13992. (cond
  13993. ((and subtreep org-yank-folded-subtrees)
  13994. (let ((beg (point))
  13995. end)
  13996. (if (and subtreep org-yank-adjusted-subtrees)
  13997. (org-paste-subtree nil nil 'for-yank)
  13998. (call-interactively 'yank))
  13999. (setq end (point))
  14000. (goto-char beg)
  14001. (when (and (bolp) subtreep
  14002. (not (setq swallowp
  14003. (org-yank-folding-would-swallow-text beg end))))
  14004. (or (looking-at outline-regexp)
  14005. (re-search-forward (concat "^" outline-regexp) end t))
  14006. (while (and (< (point) end) (looking-at outline-regexp))
  14007. (hide-subtree)
  14008. (org-cycle-show-empty-lines 'folded)
  14009. (condition-case nil
  14010. (outline-forward-same-level 1)
  14011. (error (goto-char end)))))
  14012. (when swallowp
  14013. (message
  14014. "Yanked text not folded because that would swallow text"))
  14015. (goto-char end)
  14016. (skip-chars-forward " \t\n\r")
  14017. (beginning-of-line 1)
  14018. (push-mark beg 'nomsg)))
  14019. ((and subtreep org-yank-adjusted-subtrees)
  14020. (let ((beg (point-at-bol)))
  14021. (org-paste-subtree nil nil 'for-yank)
  14022. (push-mark beg 'nomsg)))
  14023. (t
  14024. (call-interactively 'yank))))))
  14025. (defun org-yank-folding-would-swallow-text (beg end)
  14026. "Would hide-subtree at BEG swallow any text after END?"
  14027. (let (level)
  14028. (save-excursion
  14029. (goto-char beg)
  14030. (when (or (looking-at outline-regexp)
  14031. (re-search-forward (concat "^" outline-regexp) end t))
  14032. (setq level (org-outline-level)))
  14033. (goto-char end)
  14034. (skip-chars-forward " \t\r\n\v\f")
  14035. (if (or (eobp)
  14036. (and (bolp) (looking-at org-outline-regexp)
  14037. (<= (org-outline-level) level)))
  14038. nil ; Nothing would be swallowed
  14039. t)))) ; something would swallow
  14040. (define-key org-mode-map "\C-y" 'org-yank)
  14041. (defun org-invisible-p ()
  14042. "Check if point is at a character currently not visible."
  14043. ;; Early versions of noutline don't have `outline-invisible-p'.
  14044. (if (fboundp 'outline-invisible-p)
  14045. (outline-invisible-p)
  14046. (get-char-property (point) 'invisible)))
  14047. (defun org-invisible-p2 ()
  14048. "Check if point is at a character currently not visible."
  14049. (save-excursion
  14050. (if (and (eolp) (not (bobp))) (backward-char 1))
  14051. ;; Early versions of noutline don't have `outline-invisible-p'.
  14052. (if (fboundp 'outline-invisible-p)
  14053. (outline-invisible-p)
  14054. (get-char-property (point) 'invisible))))
  14055. (defun org-back-to-heading (&optional invisible-ok)
  14056. "Call `outline-back-to-heading', but provide a better error message."
  14057. (condition-case nil
  14058. (outline-back-to-heading invisible-ok)
  14059. (error (error "Before first headline at position %d in buffer %s"
  14060. (point) (current-buffer)))))
  14061. (defun org-before-first-heading-p ()
  14062. "Before first heading?"
  14063. (save-excursion
  14064. (null (re-search-backward "^\\*+ " nil t))))
  14065. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14066. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14067. (defun org-at-heading-or-item-p ()
  14068. (or (org-on-heading-p) (org-at-item-p)))
  14069. (defun org-on-target-p ()
  14070. (or (org-in-regexp org-radio-target-regexp)
  14071. (org-in-regexp org-target-regexp)))
  14072. (defun org-up-heading-all (arg)
  14073. "Move to the heading line of which the present line is a subheading.
  14074. This function considers both visible and invisible heading lines.
  14075. With argument, move up ARG levels."
  14076. (if (fboundp 'outline-up-heading-all)
  14077. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14078. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14079. (defun org-up-heading-safe ()
  14080. "Move to the heading line of which the present line is a subheading.
  14081. This version will not throw an error. It will return the level of the
  14082. headline found, or nil if no higher level is found."
  14083. (let (start-level re)
  14084. (org-back-to-heading t)
  14085. (setq start-level (funcall outline-level))
  14086. (if (equal start-level 1)
  14087. nil
  14088. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14089. (if (re-search-backward re nil t)
  14090. (funcall outline-level)))))
  14091. (defun org-first-sibling-p ()
  14092. "Is this heading the first child of its parents?"
  14093. (interactive)
  14094. (let ((re (concat "^" outline-regexp))
  14095. level l)
  14096. (unless (org-at-heading-p t)
  14097. (error "Not at a heading"))
  14098. (setq level (funcall outline-level))
  14099. (save-excursion
  14100. (if (not (re-search-backward re nil t))
  14101. t
  14102. (setq l (funcall outline-level))
  14103. (< l level)))))
  14104. (defun org-goto-sibling (&optional previous)
  14105. "Goto the next sibling, even if it is invisible.
  14106. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14107. when a sibling was found. When none is found, return nil and don't
  14108. move point."
  14109. (let ((fun (if previous 're-search-backward 're-search-forward))
  14110. (pos (point))
  14111. (re (concat "^" outline-regexp))
  14112. level l)
  14113. (when (condition-case nil (org-back-to-heading t) (error nil))
  14114. (setq level (funcall outline-level))
  14115. (catch 'exit
  14116. (or previous (forward-char 1))
  14117. (while (funcall fun re nil t)
  14118. (setq l (funcall outline-level))
  14119. (when (< l level) (goto-char pos) (throw 'exit nil))
  14120. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14121. (goto-char pos)
  14122. nil))))
  14123. (defun org-show-siblings ()
  14124. "Show all siblings of the current headline."
  14125. (save-excursion
  14126. (while (org-goto-sibling) (org-flag-heading nil)))
  14127. (save-excursion
  14128. (while (org-goto-sibling 'previous)
  14129. (org-flag-heading nil))))
  14130. (defun org-show-hidden-entry ()
  14131. "Show an entry where even the heading is hidden."
  14132. (save-excursion
  14133. (org-show-entry)))
  14134. (defun org-flag-heading (flag &optional entry)
  14135. "Flag the current heading. FLAG non-nil means make invisible.
  14136. When ENTRY is non-nil, show the entire entry."
  14137. (save-excursion
  14138. (org-back-to-heading t)
  14139. ;; Check if we should show the entire entry
  14140. (if entry
  14141. (progn
  14142. (org-show-entry)
  14143. (save-excursion
  14144. (and (outline-next-heading)
  14145. (org-flag-heading nil))))
  14146. (outline-flag-region (max (point-min) (1- (point)))
  14147. (save-excursion (outline-end-of-heading) (point))
  14148. flag))))
  14149. (defun org-forward-same-level (arg)
  14150. "Move forward to the ARG'th subheading at same level as this one.
  14151. Stop at the first and last subheadings of a superior heading.
  14152. This is like outline-forward-same-level, but invisible headings are ok."
  14153. (interactive "p")
  14154. (org-back-to-heading t)
  14155. (while (> arg 0)
  14156. (let ((point-to-move-to (save-excursion
  14157. (org-get-next-sibling))))
  14158. (if point-to-move-to
  14159. (progn
  14160. (goto-char point-to-move-to)
  14161. (setq arg (1- arg)))
  14162. (progn
  14163. (setq arg 0)
  14164. (error "No following same-level heading"))))))
  14165. (defun org-get-next-sibling ()
  14166. "Move to next heading of the same level, and return point.
  14167. If there is no such heading, return nil.
  14168. This is like outline-next-sibling, but invisible headings are ok."
  14169. (let ((level (funcall outline-level)))
  14170. (outline-next-heading)
  14171. (while (and (not (eobp)) (> (funcall outline-level) level))
  14172. (outline-next-heading))
  14173. (if (or (eobp) (< (funcall outline-level) level))
  14174. nil
  14175. (point))))
  14176. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14177. ;; This is an exact copy of the original function, but it uses
  14178. ;; `org-back-to-heading', to make it work also in invisible
  14179. ;; trees. And is uses an invisible-OK argument.
  14180. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14181. (org-back-to-heading invisible-OK)
  14182. (let ((first t)
  14183. (level (funcall outline-level)))
  14184. (while (and (not (eobp))
  14185. (or first (> (funcall outline-level) level)))
  14186. (setq first nil)
  14187. (outline-next-heading))
  14188. (unless to-heading
  14189. (if (memq (preceding-char) '(?\n ?\^M))
  14190. (progn
  14191. ;; Go to end of line before heading
  14192. (forward-char -1)
  14193. (if (memq (preceding-char) '(?\n ?\^M))
  14194. ;; leave blank line before heading
  14195. (forward-char -1))))))
  14196. (point))
  14197. (defun org-show-subtree ()
  14198. "Show everything after this heading at deeper levels."
  14199. (outline-flag-region
  14200. (point)
  14201. (save-excursion
  14202. (outline-end-of-subtree) (outline-next-heading) (point))
  14203. nil))
  14204. (defun org-show-entry ()
  14205. "Show the body directly following this heading.
  14206. Show the heading too, if it is currently invisible."
  14207. (interactive)
  14208. (save-excursion
  14209. (condition-case nil
  14210. (progn
  14211. (org-back-to-heading t)
  14212. (outline-flag-region
  14213. (max (point-min) (1- (point)))
  14214. (save-excursion
  14215. (if (re-search-forward
  14216. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14217. (match-beginning 1)
  14218. (point-max)))
  14219. nil))
  14220. (error nil))))
  14221. (defun org-make-options-regexp (kwds &optional extra)
  14222. "Make a regular expression for keyword lines."
  14223. (concat
  14224. "^"
  14225. "#?[ \t]*\\+\\("
  14226. (mapconcat 'regexp-quote kwds "\\|")
  14227. (if extra (concat "\\|" extra))
  14228. "\\):[ \t]*"
  14229. "\\(.+\\)"))
  14230. ;; Make isearch reveal the necessary context
  14231. (defun org-isearch-end ()
  14232. "Reveal context after isearch exits."
  14233. (when isearch-success ; only if search was successful
  14234. (if (featurep 'xemacs)
  14235. ;; Under XEmacs, the hook is run in the correct place,
  14236. ;; we directly show the context.
  14237. (org-show-context 'isearch)
  14238. ;; In Emacs the hook runs *before* restoring the overlays.
  14239. ;; So we have to use a one-time post-command-hook to do this.
  14240. ;; (Emacs 22 has a special variable, see function `org-mode')
  14241. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14242. isearch-mode-end-hook-quit)
  14243. ;; Only when the isearch was not quitted.
  14244. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14245. 'append 'local)))))
  14246. (defun org-isearch-post-command ()
  14247. "Remove self from hook, and show context."
  14248. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14249. (org-show-context 'isearch))
  14250. ;;;; Integration with and fixes for other packages
  14251. ;;; Imenu support
  14252. (defvar org-imenu-markers nil
  14253. "All markers currently used by Imenu.")
  14254. (make-variable-buffer-local 'org-imenu-markers)
  14255. (defun org-imenu-new-marker (&optional pos)
  14256. "Return a new marker for use by Imenu, and remember the marker."
  14257. (let ((m (make-marker)))
  14258. (move-marker m (or pos (point)))
  14259. (push m org-imenu-markers)
  14260. m))
  14261. (defun org-imenu-get-tree ()
  14262. "Produce the index for Imenu."
  14263. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14264. (setq org-imenu-markers nil)
  14265. (let* ((n org-imenu-depth)
  14266. (re (concat "^" outline-regexp))
  14267. (subs (make-vector (1+ n) nil))
  14268. (last-level 0)
  14269. m level head)
  14270. (save-excursion
  14271. (save-restriction
  14272. (widen)
  14273. (goto-char (point-max))
  14274. (while (re-search-backward re nil t)
  14275. (setq level (org-reduced-level (funcall outline-level)))
  14276. (when (<= level n)
  14277. (looking-at org-complex-heading-regexp)
  14278. (setq head (org-link-display-format
  14279. (org-match-string-no-properties 4))
  14280. m (org-imenu-new-marker))
  14281. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14282. (if (>= level last-level)
  14283. (push (cons head m) (aref subs level))
  14284. (push (cons head (aref subs (1+ level))) (aref subs level))
  14285. (loop for i from (1+ level) to n do (aset subs i nil)))
  14286. (setq last-level level)))))
  14287. (aref subs 1)))
  14288. (eval-after-load "imenu"
  14289. '(progn
  14290. (add-hook 'imenu-after-jump-hook
  14291. (lambda ()
  14292. (if (eq major-mode 'org-mode)
  14293. (org-show-context 'org-goto))))))
  14294. (defun org-link-display-format (link)
  14295. "Replace a link with either the description, or the link target
  14296. if no description is present"
  14297. (save-match-data
  14298. (if (string-match org-bracket-link-analytic-regexp link)
  14299. (replace-match (or (match-string 5 link)
  14300. (concat (match-string 1 link)
  14301. (match-string 3 link)))
  14302. nil nil link)
  14303. link)))
  14304. ;; Speedbar support
  14305. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14306. "Overlay marking the agenda restriction line in speedbar.")
  14307. (org-overlay-put org-speedbar-restriction-lock-overlay
  14308. 'face 'org-agenda-restriction-lock)
  14309. (org-overlay-put org-speedbar-restriction-lock-overlay
  14310. 'help-echo "Agendas are currently limited to this item.")
  14311. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14312. (defun org-speedbar-set-agenda-restriction ()
  14313. "Restrict future agenda commands to the location at point in speedbar.
  14314. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14315. (interactive)
  14316. (require 'org-agenda)
  14317. (let (p m tp np dir txt)
  14318. (cond
  14319. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14320. 'org-imenu t))
  14321. (setq m (get-text-property p 'org-imenu-marker))
  14322. (save-excursion
  14323. (save-restriction
  14324. (set-buffer (marker-buffer m))
  14325. (goto-char m)
  14326. (org-agenda-set-restriction-lock 'subtree))))
  14327. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14328. 'speedbar-function 'speedbar-find-file))
  14329. (setq tp (previous-single-property-change
  14330. (1+ p) 'speedbar-function)
  14331. np (next-single-property-change
  14332. tp 'speedbar-function)
  14333. dir (speedbar-line-directory)
  14334. txt (buffer-substring-no-properties (or tp (point-min))
  14335. (or np (point-max))))
  14336. (save-excursion
  14337. (save-restriction
  14338. (set-buffer (find-file-noselect
  14339. (let ((default-directory dir))
  14340. (expand-file-name txt))))
  14341. (unless (org-mode-p)
  14342. (error "Cannot restrict to non-Org-mode file"))
  14343. (org-agenda-set-restriction-lock 'file))))
  14344. (t (error "Don't know how to restrict Org-mode's agenda")))
  14345. (org-move-overlay org-speedbar-restriction-lock-overlay
  14346. (point-at-bol) (point-at-eol))
  14347. (setq current-prefix-arg nil)
  14348. (org-agenda-maybe-redo)))
  14349. (eval-after-load "speedbar"
  14350. '(progn
  14351. (speedbar-add-supported-extension ".org")
  14352. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14353. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14354. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14355. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14356. (add-hook 'speedbar-visiting-tag-hook
  14357. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14358. ;;; Fixes and Hacks for problems with other packages
  14359. ;; Make flyspell not check words in links, to not mess up our keymap
  14360. (defun org-mode-flyspell-verify ()
  14361. "Don't let flyspell put overlays at active buttons."
  14362. (not (get-text-property (point) 'keymap)))
  14363. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14364. (eval-after-load "bookmark"
  14365. '(if (boundp 'bookmark-after-jump-hook)
  14366. ;; We can use the hook
  14367. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14368. ;; Hook not available, use advice
  14369. (defadvice bookmark-jump (after org-make-visible activate)
  14370. "Make the position visible."
  14371. (org-bookmark-jump-unhide))))
  14372. ;; Make sure saveplace show the location if it was hidden
  14373. (eval-after-load "saveplace"
  14374. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14375. "Make the position visible."
  14376. (org-bookmark-jump-unhide)))
  14377. (defun org-bookmark-jump-unhide ()
  14378. "Unhide the current position, to show the bookmark location."
  14379. (and (org-mode-p)
  14380. (or (org-invisible-p)
  14381. (save-excursion (goto-char (max (point-min) (1- (point))))
  14382. (org-invisible-p)))
  14383. (org-show-context 'bookmark-jump)))
  14384. ;; Make session.el ignore our circular variable
  14385. (eval-after-load "session"
  14386. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14387. ;;;; Experimental code
  14388. (defun org-closed-in-range ()
  14389. "Sparse tree of items closed in a certain time range.
  14390. Still experimental, may disappear in the future."
  14391. (interactive)
  14392. ;; Get the time interval from the user.
  14393. (let* ((time1 (time-to-seconds
  14394. (org-read-date nil 'to-time nil "Starting date: ")))
  14395. (time2 (time-to-seconds
  14396. (org-read-date nil 'to-time nil "End date:")))
  14397. ;; callback function
  14398. (callback (lambda ()
  14399. (let ((time
  14400. (time-to-seconds
  14401. (apply 'encode-time
  14402. (org-parse-time-string
  14403. (match-string 1))))))
  14404. ;; check if time in interval
  14405. (and (>= time time1) (<= time time2))))))
  14406. ;; make tree, check each match with the callback
  14407. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14408. ;;;; Finish up
  14409. (provide 'org)
  14410. (run-hooks 'org-load-hook)
  14411. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14412. ;;; org.el ends here