org.el 584 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729
  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.23b
  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.23b"
  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. (org-load-modules-maybe)
  5814. (unless org-local-vars
  5815. (setq org-local-vars (org-get-local-variables)))
  5816. (eval (list 'let org-local-vars
  5817. (list 'call-interactively (list 'quote cmd)))))
  5818. ;;;; Archiving
  5819. (defun org-get-category (&optional pos)
  5820. "Get the category applying to position POS."
  5821. (get-text-property (or pos (point)) 'org-category))
  5822. (defun org-refresh-category-properties ()
  5823. "Refresh category text properties in the buffer."
  5824. (let ((def-cat (cond
  5825. ((null org-category)
  5826. (if buffer-file-name
  5827. (file-name-sans-extension
  5828. (file-name-nondirectory buffer-file-name))
  5829. "???"))
  5830. ((symbolp org-category) (symbol-name org-category))
  5831. (t org-category)))
  5832. beg end cat pos optionp)
  5833. (org-unmodified
  5834. (save-excursion
  5835. (save-restriction
  5836. (widen)
  5837. (goto-char (point-min))
  5838. (put-text-property (point) (point-max) 'org-category def-cat)
  5839. (while (re-search-forward
  5840. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5841. (setq pos (match-end 0)
  5842. optionp (equal (char-after (match-beginning 0)) ?#)
  5843. cat (org-trim (match-string 2)))
  5844. (if optionp
  5845. (setq beg (point-at-bol) end (point-max))
  5846. (org-back-to-heading t)
  5847. (setq beg (point) end (org-end-of-subtree t t)))
  5848. (put-text-property beg end 'org-category cat)
  5849. (goto-char pos)))))))
  5850. ;;;; Link Stuff
  5851. ;;; Link abbreviations
  5852. (defun org-link-expand-abbrev (link)
  5853. "Apply replacements as defined in `org-link-abbrev-alist."
  5854. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5855. (let* ((key (match-string 1 link))
  5856. (as (or (assoc key org-link-abbrev-alist-local)
  5857. (assoc key org-link-abbrev-alist)))
  5858. (tag (and (match-end 2) (match-string 3 link)))
  5859. rpl)
  5860. (if (not as)
  5861. link
  5862. (setq rpl (cdr as))
  5863. (cond
  5864. ((symbolp rpl) (funcall rpl tag))
  5865. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5866. ((string-match "%h" rpl)
  5867. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5868. (t (concat rpl tag)))))
  5869. link))
  5870. ;;; Storing and inserting links
  5871. (defvar org-insert-link-history nil
  5872. "Minibuffer history for links inserted with `org-insert-link'.")
  5873. (defvar org-stored-links nil
  5874. "Contains the links stored with `org-store-link'.")
  5875. (defvar org-store-link-plist nil
  5876. "Plist with info about the most recently link created with `org-store-link'.")
  5877. (defvar org-link-protocols nil
  5878. "Link protocols added to Org-mode using `org-add-link-type'.")
  5879. (defvar org-store-link-functions nil
  5880. "List of functions that are called to create and store a link.
  5881. Each function will be called in turn until one returns a non-nil
  5882. value. Each function should check if it is responsible for creating
  5883. this link (for example by looking at the major mode).
  5884. If not, it must exit and return nil.
  5885. If yes, it should return a non-nil value after a calling
  5886. `org-store-link-props' with a list of properties and values.
  5887. Special properties are:
  5888. :type The link prefix. like \"http\". This must be given.
  5889. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5890. This is obligatory as well.
  5891. :description Optional default description for the second pair
  5892. of brackets in an Org-mode link. The user can still change
  5893. this when inserting this link into an Org-mode buffer.
  5894. In addition to these, any additional properties can be specified
  5895. and then used in remember templates.")
  5896. (defun org-add-link-type (type &optional follow export)
  5897. "Add TYPE to the list of `org-link-types'.
  5898. Re-compute all regular expressions depending on `org-link-types'
  5899. FOLLOW and EXPORT are two functions.
  5900. FOLLOW should take the link path as the single argument and do whatever
  5901. is necessary to follow the link, for example find a file or display
  5902. a mail message.
  5903. EXPORT should format the link path for export to one of the export formats.
  5904. It should be a function accepting three arguments:
  5905. path the path of the link, the text after the prefix (like \"http:\")
  5906. desc the description of the link, if any, nil if there was no description
  5907. format the export format, a symbol like `html' or `latex'.
  5908. The function may use the FORMAT information to return different values
  5909. depending on the format. The return value will be put literally into
  5910. the exported file.
  5911. Org-mode has a built-in default for exporting links. If you are happy with
  5912. this default, there is no need to define an export function for the link
  5913. type. For a simple example of an export function, see `org-bbdb.el'."
  5914. (add-to-list 'org-link-types type t)
  5915. (org-make-link-regexps)
  5916. (if (assoc type org-link-protocols)
  5917. (setcdr (assoc type org-link-protocols) (list follow export))
  5918. (push (list type follow export) org-link-protocols)))
  5919. ;;;###autoload
  5920. (defun org-store-link (arg)
  5921. "\\<org-mode-map>Store an org-link to the current location.
  5922. This link is added to `org-stored-links' and can later be inserted
  5923. into an org-buffer with \\[org-insert-link].
  5924. For some link types, a prefix arg is interpreted:
  5925. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5926. For file links, arg negates `org-context-in-file-links'."
  5927. (interactive "P")
  5928. (org-load-modules-maybe)
  5929. (setq org-store-link-plist nil) ; reset
  5930. (let (link cpltxt desc description search txt)
  5931. (cond
  5932. ((run-hook-with-args-until-success 'org-store-link-functions)
  5933. (setq link (plist-get org-store-link-plist :link)
  5934. desc (or (plist-get org-store-link-plist :description) link)))
  5935. ((equal (buffer-name) "*Org Edit Src Example*")
  5936. (let (label gc)
  5937. (while (or (not label)
  5938. (save-excursion
  5939. (save-restriction
  5940. (widen)
  5941. (goto-char (point-min))
  5942. (re-search-forward
  5943. (regexp-quote (format org-coderef-label-format label))
  5944. nil t))))
  5945. (when label (message "Label exists already") (sit-for 2))
  5946. (setq label (read-string "Code line label: " label)))
  5947. (end-of-line 1)
  5948. (setq link (format org-coderef-label-format label))
  5949. (setq gc (- 79 (length link)))
  5950. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5951. (insert link)
  5952. (setq link (concat "(" label ")") desc nil)))
  5953. ((eq major-mode 'calendar-mode)
  5954. (let ((cd (calendar-cursor-to-date)))
  5955. (setq link
  5956. (format-time-string
  5957. (car org-time-stamp-formats)
  5958. (apply 'encode-time
  5959. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5960. nil nil nil))))
  5961. (org-store-link-props :type "calendar" :date cd)))
  5962. ((eq major-mode 'w3-mode)
  5963. (setq cpltxt (url-view-url t)
  5964. link (org-make-link cpltxt))
  5965. (org-store-link-props :type "w3" :url (url-view-url t)))
  5966. ((eq major-mode 'w3m-mode)
  5967. (setq cpltxt (or w3m-current-title w3m-current-url)
  5968. link (org-make-link w3m-current-url))
  5969. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5970. ((setq search (run-hook-with-args-until-success
  5971. 'org-create-file-search-functions))
  5972. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5973. "::" search))
  5974. (setq cpltxt (or description link)))
  5975. ((eq major-mode 'image-mode)
  5976. (setq cpltxt (concat "file:"
  5977. (abbreviate-file-name buffer-file-name))
  5978. link (org-make-link cpltxt))
  5979. (org-store-link-props :type "image" :file buffer-file-name))
  5980. ((eq major-mode 'dired-mode)
  5981. ;; link to the file in the current line
  5982. (setq cpltxt (concat "file:"
  5983. (abbreviate-file-name
  5984. (expand-file-name
  5985. (dired-get-filename nil t))))
  5986. link (org-make-link cpltxt)))
  5987. ((and buffer-file-name (org-mode-p))
  5988. (cond
  5989. ((org-in-regexp "<<\\(.*?\\)>>")
  5990. (setq cpltxt
  5991. (concat "file:"
  5992. (abbreviate-file-name buffer-file-name)
  5993. "::" (match-string 1))
  5994. link (org-make-link cpltxt)))
  5995. ((and (featurep 'org-id)
  5996. (or (eq org-link-to-org-use-id t)
  5997. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5998. (interactive-p))
  5999. (and org-link-to-org-use-id
  6000. (condition-case nil
  6001. (org-entry-get nil "ID")
  6002. (error nil)))))
  6003. ;; We can make a link using the ID.
  6004. (setq link (condition-case nil
  6005. (prog1 (org-id-store-link)
  6006. (setq desc (plist-get org-store-link-plist
  6007. :description)))
  6008. (error
  6009. ;; probably before first headline, link to file only
  6010. (concat "file:"
  6011. (abbreviate-file-name buffer-file-name))))))
  6012. (t
  6013. ;; Just link to current headline
  6014. (setq cpltxt (concat "file:"
  6015. (abbreviate-file-name buffer-file-name)))
  6016. ;; Add a context search string
  6017. (when (org-xor org-context-in-file-links arg)
  6018. (setq txt (cond
  6019. ((org-on-heading-p) nil)
  6020. ((org-region-active-p)
  6021. (buffer-substring (region-beginning) (region-end)))
  6022. (t nil)))
  6023. (when (or (null txt) (string-match "\\S-" txt))
  6024. (setq cpltxt
  6025. (concat cpltxt "::"
  6026. (condition-case nil
  6027. (org-make-org-heading-search-string txt)
  6028. (error "")))
  6029. desc "NONE")))
  6030. (if (string-match "::\\'" cpltxt)
  6031. (setq cpltxt (substring cpltxt 0 -2)))
  6032. (setq link (org-make-link cpltxt)))))
  6033. ((buffer-file-name (buffer-base-buffer))
  6034. ;; Just link to this file here.
  6035. (setq cpltxt (concat "file:"
  6036. (abbreviate-file-name
  6037. (buffer-file-name (buffer-base-buffer)))))
  6038. ;; Add a context string
  6039. (when (org-xor org-context-in-file-links arg)
  6040. (setq txt (if (org-region-active-p)
  6041. (buffer-substring (region-beginning) (region-end))
  6042. (buffer-substring (point-at-bol) (point-at-eol))))
  6043. ;; Only use search option if there is some text.
  6044. (when (string-match "\\S-" txt)
  6045. (setq cpltxt
  6046. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6047. desc "NONE")))
  6048. (setq link (org-make-link cpltxt)))
  6049. ((interactive-p)
  6050. (error "Cannot link to a buffer which is not visiting a file"))
  6051. (t (setq link nil)))
  6052. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6053. (setq link (or link cpltxt)
  6054. desc (or desc cpltxt))
  6055. (if (equal desc "NONE") (setq desc nil))
  6056. (if (and (interactive-p) link)
  6057. (progn
  6058. (setq org-stored-links
  6059. (cons (list link desc) org-stored-links))
  6060. (message "Stored: %s" (or desc link)))
  6061. (and link (org-make-link-string link desc)))))
  6062. (defun org-store-link-props (&rest plist)
  6063. "Store link properties, extract names and addresses."
  6064. (let (x adr)
  6065. (when (setq x (plist-get plist :from))
  6066. (setq adr (mail-extract-address-components x))
  6067. (setq plist (plist-put plist :fromname (car adr)))
  6068. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6069. (when (setq x (plist-get plist :to))
  6070. (setq adr (mail-extract-address-components x))
  6071. (setq plist (plist-put plist :toname (car adr)))
  6072. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6073. (let ((from (plist-get plist :from))
  6074. (to (plist-get plist :to)))
  6075. (when (and from to org-from-is-user-regexp)
  6076. (setq plist
  6077. (plist-put plist :fromto
  6078. (if (string-match org-from-is-user-regexp from)
  6079. (concat "to %t")
  6080. (concat "from %f"))))))
  6081. (setq org-store-link-plist plist))
  6082. (defun org-add-link-props (&rest plist)
  6083. "Add these properties to the link property list."
  6084. (let (key value)
  6085. (while plist
  6086. (setq key (pop plist) value (pop plist))
  6087. (setq org-store-link-plist
  6088. (plist-put org-store-link-plist key value)))))
  6089. (defun org-email-link-description (&optional fmt)
  6090. "Return the description part of an email link.
  6091. This takes information from `org-store-link-plist' and formats it
  6092. according to FMT (default from `org-email-link-description-format')."
  6093. (setq fmt (or fmt org-email-link-description-format))
  6094. (let* ((p org-store-link-plist)
  6095. (to (plist-get p :toaddress))
  6096. (from (plist-get p :fromaddress))
  6097. (table
  6098. (list
  6099. (cons "%c" (plist-get p :fromto))
  6100. (cons "%F" (plist-get p :from))
  6101. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6102. (cons "%T" (plist-get p :to))
  6103. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6104. (cons "%s" (plist-get p :subject))
  6105. (cons "%m" (plist-get p :message-id)))))
  6106. (when (string-match "%c" fmt)
  6107. ;; Check if the user wrote this message
  6108. (if (and org-from-is-user-regexp from to
  6109. (save-match-data (string-match org-from-is-user-regexp from)))
  6110. (setq fmt (replace-match "to %t" t t fmt))
  6111. (setq fmt (replace-match "from %f" t t fmt))))
  6112. (org-replace-escapes fmt table)))
  6113. (defun org-make-org-heading-search-string (&optional string heading)
  6114. "Make search string for STRING or current headline."
  6115. (interactive)
  6116. (let ((s (or string (org-get-heading))))
  6117. (unless (and string (not heading))
  6118. ;; We are using a headline, clean up garbage in there.
  6119. (if (string-match org-todo-regexp s)
  6120. (setq s (replace-match "" t t s)))
  6121. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6122. (setq s (replace-match "" t t s)))
  6123. (setq s (org-trim s))
  6124. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6125. org-comment-string "\\)") s)
  6126. (setq s (replace-match "" t t s)))
  6127. (while (string-match org-ts-regexp s)
  6128. (setq s (replace-match "" t t s))))
  6129. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6130. (setq s (replace-match " " t t s)))
  6131. (or string (setq s (concat "*" s))) ; Add * for headlines
  6132. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6133. (defun org-make-link (&rest strings)
  6134. "Concatenate STRINGS."
  6135. (apply 'concat strings))
  6136. (defun org-make-link-string (link &optional description)
  6137. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6138. (unless (string-match "\\S-" link)
  6139. (error "Empty link"))
  6140. (when (stringp description)
  6141. ;; Remove brackets from the description, they are fatal.
  6142. (while (string-match "\\[" description)
  6143. (setq description (replace-match "{" t t description)))
  6144. (while (string-match "\\]" description)
  6145. (setq description (replace-match "}" t t description))))
  6146. (when (equal (org-link-escape link) description)
  6147. ;; No description needed, it is identical
  6148. (setq description nil))
  6149. (when (and (not description)
  6150. (not (equal link (org-link-escape link))))
  6151. (setq description (org-extract-attributes link)))
  6152. (concat "[[" (org-link-escape link) "]"
  6153. (if description (concat "[" description "]") "")
  6154. "]"))
  6155. (defconst org-link-escape-chars
  6156. '((?\ . "%20")
  6157. (?\[ . "%5B")
  6158. (?\] . "%5D")
  6159. (?\340 . "%E0") ; `a
  6160. (?\342 . "%E2") ; ^a
  6161. (?\347 . "%E7") ; ,c
  6162. (?\350 . "%E8") ; `e
  6163. (?\351 . "%E9") ; 'e
  6164. (?\352 . "%EA") ; ^e
  6165. (?\356 . "%EE") ; ^i
  6166. (?\364 . "%F4") ; ^o
  6167. (?\371 . "%F9") ; `u
  6168. (?\373 . "%FB") ; ^u
  6169. (?\; . "%3B")
  6170. (?? . "%3F")
  6171. (?= . "%3D")
  6172. (?+ . "%2B")
  6173. )
  6174. "Association list of escapes for some characters problematic in links.
  6175. This is the list that is used for internal purposes.")
  6176. (defconst org-link-escape-chars-browser
  6177. '((?\ . "%20")) ; 32 for the SPC char
  6178. "Association list of escapes for some characters problematic in links.
  6179. This is the list that is used before handing over to the browser.")
  6180. (defun org-link-escape (text &optional table)
  6181. "Escape characters in TEXT that are problematic for links."
  6182. (setq table (or table org-link-escape-chars))
  6183. (when text
  6184. (let ((re (mapconcat (lambda (x) (regexp-quote
  6185. (char-to-string (car x))))
  6186. table "\\|")))
  6187. (while (string-match re text)
  6188. (setq text
  6189. (replace-match
  6190. (cdr (assoc (string-to-char (match-string 0 text))
  6191. table))
  6192. t t text)))
  6193. text)))
  6194. (defun org-link-unescape (text &optional table)
  6195. "Reverse the action of `org-link-escape'."
  6196. (setq table (or table org-link-escape-chars))
  6197. (when text
  6198. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6199. table "\\|")))
  6200. (while (string-match re text)
  6201. (setq text
  6202. (replace-match
  6203. (char-to-string (car (rassoc (match-string 0 text) table)))
  6204. t t text)))
  6205. text)))
  6206. (defun org-xor (a b)
  6207. "Exclusive or."
  6208. (if a (not b) b))
  6209. (defun org-fixup-message-id-for-http (s)
  6210. "Replace special characters in a message id, so it can be used in an http query."
  6211. (while (string-match "<" s)
  6212. (setq s (replace-match "%3C" t t s)))
  6213. (while (string-match ">" s)
  6214. (setq s (replace-match "%3E" t t s)))
  6215. (while (string-match "@" s)
  6216. (setq s (replace-match "%40" t t s)))
  6217. s)
  6218. ;;;###autoload
  6219. (defun org-insert-link-global ()
  6220. "Insert a link like Org-mode does.
  6221. This command can be called in any mode to insert a link in Org-mode syntax."
  6222. (interactive)
  6223. (org-load-modules-maybe)
  6224. (org-run-like-in-org-mode 'org-insert-link))
  6225. (defun org-insert-link (&optional complete-file link-location)
  6226. "Insert a link. At the prompt, enter the link.
  6227. Completion can be used to insert any of the link protocol prefixes like
  6228. http or ftp in use.
  6229. The history can be used to select a link previously stored with
  6230. `org-store-link'. When the empty string is entered (i.e. if you just
  6231. press RET at the prompt), the link defaults to the most recently
  6232. stored link. As SPC triggers completion in the minibuffer, you need to
  6233. use M-SPC or C-q SPC to force the insertion of a space character.
  6234. You will also be prompted for a description, and if one is given, it will
  6235. be displayed in the buffer instead of the link.
  6236. If there is already a link at point, this command will allow you to edit link
  6237. and description parts.
  6238. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6239. be selected using completion. The path to the file will be relative to the
  6240. current directory if the file is in the current directory or a subdirectory.
  6241. Otherwise, the link will be the absolute path as completed in the minibuffer
  6242. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6243. option `org-link-file-path-type'.
  6244. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6245. the current directory or below.
  6246. With three \\[universal-argument] prefixes, negate the meaning of
  6247. `org-keep-stored-link-after-insertion'.
  6248. If `org-make-link-description-function' is non-nil, this function will be
  6249. called with the link target, and the result will be the default
  6250. link description.
  6251. If the LINK-LOCATION parameter is non-nil, this value will be
  6252. used as the link location instead of reading one interactively."
  6253. (interactive "P")
  6254. (let* ((wcf (current-window-configuration))
  6255. (region (if (org-region-active-p)
  6256. (buffer-substring (region-beginning) (region-end))))
  6257. (remove (and region (list (region-beginning) (region-end))))
  6258. (desc region)
  6259. tmphist ; byte-compile incorrectly complains about this
  6260. (link link-location)
  6261. entry file)
  6262. (cond
  6263. (link-location) ; specified by arg, just use it.
  6264. ((org-in-regexp org-bracket-link-regexp 1)
  6265. ;; We do have a link at point, and we are going to edit it.
  6266. (setq remove (list (match-beginning 0) (match-end 0)))
  6267. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6268. (setq link (read-string "Link: "
  6269. (org-link-unescape
  6270. (org-match-string-no-properties 1)))))
  6271. ((or (org-in-regexp org-angle-link-re)
  6272. (org-in-regexp org-plain-link-re))
  6273. ;; Convert to bracket link
  6274. (setq remove (list (match-beginning 0) (match-end 0))
  6275. link (read-string "Link: "
  6276. (org-remove-angle-brackets (match-string 0)))))
  6277. ((member complete-file '((4) (16)))
  6278. ;; Completing read for file names.
  6279. (setq file (read-file-name "File: "))
  6280. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6281. (pwd1 (file-name-as-directory (abbreviate-file-name
  6282. (expand-file-name ".")))))
  6283. (cond
  6284. ((equal complete-file '(16))
  6285. (setq link (org-make-link
  6286. "file:"
  6287. (abbreviate-file-name (expand-file-name file)))))
  6288. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6289. (setq link (org-make-link "file:" (match-string 1 file))))
  6290. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6291. (expand-file-name file))
  6292. (setq link (org-make-link
  6293. "file:" (match-string 1 (expand-file-name file)))))
  6294. (t (setq link (org-make-link "file:" file))))))
  6295. (t
  6296. ;; Read link, with completion for stored links.
  6297. (with-output-to-temp-buffer "*Org Links*"
  6298. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6299. (when org-stored-links
  6300. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6301. (princ (mapconcat
  6302. (lambda (x)
  6303. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6304. (reverse org-stored-links) "\n"))))
  6305. (let ((cw (selected-window)))
  6306. (select-window (get-buffer-window "*Org Links*"))
  6307. (setq truncate-lines t)
  6308. (org-fit-window-to-buffer)
  6309. (select-window cw))
  6310. ;; Fake a link history, containing the stored links.
  6311. (setq tmphist (append (mapcar 'car org-stored-links)
  6312. org-insert-link-history))
  6313. (unwind-protect
  6314. (setq link
  6315. (let ((org-completion-use-ido nil))
  6316. (org-completing-read
  6317. "Link: "
  6318. (append
  6319. (mapcar (lambda (x) (list (concat (car x) ":")))
  6320. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6321. (mapcar (lambda (x) (list (concat x ":")))
  6322. org-link-types))
  6323. nil nil nil
  6324. 'tmphist
  6325. (or (car (car org-stored-links))))))
  6326. (set-window-configuration wcf)
  6327. (kill-buffer "*Org Links*"))
  6328. (setq entry (assoc link org-stored-links))
  6329. (or entry (push link org-insert-link-history))
  6330. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6331. (not org-keep-stored-link-after-insertion))
  6332. (setq org-stored-links (delq (assoc link org-stored-links)
  6333. org-stored-links)))
  6334. (setq desc (or desc (nth 1 entry)))))
  6335. (if (string-match org-plain-link-re link)
  6336. ;; URL-like link, normalize the use of angular brackets.
  6337. (setq link (org-make-link (org-remove-angle-brackets link))))
  6338. ;; Check if we are linking to the current file with a search option
  6339. ;; If yes, simplify the link by using only the search option.
  6340. (when (and buffer-file-name
  6341. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6342. (let* ((path (match-string 1 link))
  6343. (case-fold-search nil)
  6344. (search (match-string 2 link)))
  6345. (save-match-data
  6346. (if (equal (file-truename buffer-file-name) (file-truename path))
  6347. ;; We are linking to this same file, with a search option
  6348. (setq link search)))))
  6349. ;; Check if we can/should use a relative path. If yes, simplify the link
  6350. (when (string-match "^file:\\(.*\\)" link)
  6351. (let* ((path (match-string 1 link))
  6352. (origpath path)
  6353. (case-fold-search nil))
  6354. (cond
  6355. ((or (eq org-link-file-path-type 'absolute)
  6356. (equal complete-file '(16)))
  6357. (setq path (abbreviate-file-name (expand-file-name path))))
  6358. ((eq org-link-file-path-type 'noabbrev)
  6359. (setq path (expand-file-name path)))
  6360. ((eq org-link-file-path-type 'relative)
  6361. (setq path (file-relative-name path)))
  6362. (t
  6363. (save-match-data
  6364. (if (string-match (concat "^" (regexp-quote
  6365. (file-name-as-directory
  6366. (expand-file-name "."))))
  6367. (expand-file-name path))
  6368. ;; We are linking a file with relative path name.
  6369. (setq path (substring (expand-file-name path)
  6370. (match-end 0)))
  6371. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6372. (setq link (concat "file:" path))
  6373. (if (equal desc origpath)
  6374. (setq desc path))))
  6375. (if org-make-link-description-function
  6376. (setq desc (funcall org-make-link-description-function link desc)))
  6377. (setq desc (read-string "Description: " desc))
  6378. (unless (string-match "\\S-" desc) (setq desc nil))
  6379. (if remove (apply 'delete-region remove))
  6380. (insert (org-make-link-string link desc))))
  6381. (defun org-completing-read (&rest args)
  6382. "Completing-read with SPACE being a normal character."
  6383. (let ((minibuffer-local-completion-map
  6384. (copy-keymap minibuffer-local-completion-map)))
  6385. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6386. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6387. (apply 'org-ido-completing-read args)))
  6388. (defun org-completing-read-no-ido (&rest args)
  6389. (let (org-completion-use-ido)
  6390. (apply 'org-completing-read args)))
  6391. (defun org-ido-completing-read (&rest args)
  6392. "Completing-read using `ido-mode' speedups if available"
  6393. (if (and org-completion-use-ido
  6394. (fboundp 'ido-completing-read)
  6395. (boundp 'ido-mode) ido-mode
  6396. (listp (second args)))
  6397. (let ((ido-enter-matching-directory nil))
  6398. (apply 'ido-completing-read (concat (car args))
  6399. (mapcar (lambda (x) (car x)) (nth 1 args))
  6400. (cddr args)))
  6401. (apply 'completing-read args)))
  6402. (defun org-extract-attributes (s)
  6403. "Extract the attributes cookie from a string and set as text property."
  6404. (let (a attr (start 0) key value)
  6405. (save-match-data
  6406. (when (string-match "{{\\([^}]+\\)}}$" s)
  6407. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6408. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6409. (setq key (match-string 1 a) value (match-string 2 a)
  6410. start (match-end 0)
  6411. attr (plist-put attr (intern key) value))))
  6412. (org-add-props s nil 'org-attr attr))
  6413. s))
  6414. (defun org-attributes-to-string (plist)
  6415. "Format a property list into an HTML attribute list."
  6416. (let ((s "") key value)
  6417. (while plist
  6418. (setq key (pop plist) value (pop plist))
  6419. (and value
  6420. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6421. s))
  6422. ;;; Opening/following a link
  6423. (defvar org-link-search-failed nil)
  6424. (defun org-next-link ()
  6425. "Move forward to the next link.
  6426. If the link is in hidden text, expose it."
  6427. (interactive)
  6428. (when (and org-link-search-failed (eq this-command last-command))
  6429. (goto-char (point-min))
  6430. (message "Link search wrapped back to beginning of buffer"))
  6431. (setq org-link-search-failed nil)
  6432. (let* ((pos (point))
  6433. (ct (org-context))
  6434. (a (assoc :link ct)))
  6435. (if a (goto-char (nth 2 a)))
  6436. (if (re-search-forward org-any-link-re nil t)
  6437. (progn
  6438. (goto-char (match-beginning 0))
  6439. (if (org-invisible-p) (org-show-context)))
  6440. (goto-char pos)
  6441. (setq org-link-search-failed t)
  6442. (error "No further link found"))))
  6443. (defun org-previous-link ()
  6444. "Move backward to the previous link.
  6445. If the link is in hidden text, expose it."
  6446. (interactive)
  6447. (when (and org-link-search-failed (eq this-command last-command))
  6448. (goto-char (point-max))
  6449. (message "Link search wrapped back to end of buffer"))
  6450. (setq org-link-search-failed nil)
  6451. (let* ((pos (point))
  6452. (ct (org-context))
  6453. (a (assoc :link ct)))
  6454. (if a (goto-char (nth 1 a)))
  6455. (if (re-search-backward org-any-link-re nil t)
  6456. (progn
  6457. (goto-char (match-beginning 0))
  6458. (if (org-invisible-p) (org-show-context)))
  6459. (goto-char pos)
  6460. (setq org-link-search-failed t)
  6461. (error "No further link found"))))
  6462. (defun org-translate-link (s)
  6463. "Translate a link string if a translation function has been defined."
  6464. (if (and org-link-translation-function
  6465. (fboundp org-link-translation-function)
  6466. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6467. (progn
  6468. (setq s (funcall org-link-translation-function
  6469. (match-string 1) (match-string 2)))
  6470. (concat (car s) ":" (cdr s)))
  6471. s))
  6472. (defun org-translate-link-from-planner (type path)
  6473. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6474. This is still an experimental function, your mileage may vary."
  6475. (cond
  6476. ((member type '("http" "https" "news" "ftp"))
  6477. ;; standard Internet links are the same.
  6478. nil)
  6479. ((and (equal type "irc") (string-match "^//" path))
  6480. ;; Planner has two / at the beginning of an irc link, we have 1.
  6481. ;; We should have zero, actually....
  6482. (setq path (substring path 1)))
  6483. ((and (equal type "lisp") (string-match "^/" path))
  6484. ;; Planner has a slash, we do not.
  6485. (setq type "elisp" path (substring path 1)))
  6486. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6487. ;; A typical message link. Planner has the id after the fina slash,
  6488. ;; we separate it with a hash mark
  6489. (setq path (concat (match-string 1 path) "#"
  6490. (org-remove-angle-brackets (match-string 2 path)))))
  6491. )
  6492. (cons type path))
  6493. (defun org-find-file-at-mouse (ev)
  6494. "Open file link or URL at mouse."
  6495. (interactive "e")
  6496. (mouse-set-point ev)
  6497. (org-open-at-point 'in-emacs))
  6498. (defun org-open-at-mouse (ev)
  6499. "Open file link or URL at mouse."
  6500. (interactive "e")
  6501. (mouse-set-point ev)
  6502. (if (eq major-mode 'org-agenda-mode)
  6503. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6504. (org-open-at-point))
  6505. (defvar org-window-config-before-follow-link nil
  6506. "The window configuration before following a link.
  6507. This is saved in case the need arises to restore it.")
  6508. (defvar org-open-link-marker (make-marker)
  6509. "Marker pointing to the location where `org-open-at-point; was called.")
  6510. ;;;###autoload
  6511. (defun org-open-at-point-global ()
  6512. "Follow a link like Org-mode does.
  6513. This command can be called in any mode to follow a link that has
  6514. Org-mode syntax."
  6515. (interactive)
  6516. (org-run-like-in-org-mode 'org-open-at-point))
  6517. ;;;###autoload
  6518. (defun org-open-link-from-string (s &optional arg)
  6519. "Open a link in the string S, as if it was in Org-mode."
  6520. (interactive "sLink: \nP")
  6521. (with-temp-buffer
  6522. (let ((org-inhibit-startup t))
  6523. (org-mode)
  6524. (insert s)
  6525. (goto-char (point-min))
  6526. (org-open-at-point arg))))
  6527. (defun org-open-at-point (&optional in-emacs)
  6528. "Open link at or after point.
  6529. If there is no link at point, this function will search forward up to
  6530. the end of the current subtree.
  6531. Normally, files will be opened by an appropriate application. If the
  6532. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6533. With a double prefix argument, try to open outside of Emacs, in the
  6534. application the system uses for this file type."
  6535. (interactive "P")
  6536. (org-load-modules-maybe)
  6537. (move-marker org-open-link-marker (point))
  6538. (setq org-window-config-before-follow-link (current-window-configuration))
  6539. (org-remove-occur-highlights nil nil t)
  6540. (cond
  6541. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6542. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6543. (org-footnote-action))
  6544. (t
  6545. (let (type path link line search (pos (point)))
  6546. (catch 'match
  6547. (save-excursion
  6548. (skip-chars-forward "^]\n\r")
  6549. (when (org-in-regexp org-bracket-link-regexp)
  6550. (setq link (org-extract-attributes
  6551. (org-link-unescape (org-match-string-no-properties 1))))
  6552. (while (string-match " *\n *" link)
  6553. (setq link (replace-match " " t t link)))
  6554. (setq link (org-link-expand-abbrev link))
  6555. (cond
  6556. ((or (file-name-absolute-p link)
  6557. (string-match "^\\.\\.?/" link))
  6558. (setq type "file" path link))
  6559. ((string-match org-link-re-with-space3 link)
  6560. (setq type (match-string 1 link) path (match-string 2 link)))
  6561. (t (setq type "thisfile" path link)))
  6562. (throw 'match t)))
  6563. (when (get-text-property (point) 'org-linked-text)
  6564. (setq type "thisfile"
  6565. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6566. (1+ (point)) (point))
  6567. path (buffer-substring
  6568. (previous-single-property-change pos 'org-linked-text)
  6569. (next-single-property-change pos 'org-linked-text)))
  6570. (throw 'match t))
  6571. (save-excursion
  6572. (when (or (org-in-regexp org-angle-link-re)
  6573. (org-in-regexp org-plain-link-re))
  6574. (setq type (match-string 1) path (match-string 2))
  6575. (throw 'match t)))
  6576. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6577. (setq type "tree-match"
  6578. path (match-string 1))
  6579. (throw 'match t))
  6580. (save-excursion
  6581. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6582. (setq type "tags"
  6583. path (match-string 1))
  6584. (while (string-match ":" path)
  6585. (setq path (replace-match "+" t t path)))
  6586. (throw 'match t))))
  6587. (unless path
  6588. (error "No link found"))
  6589. ;; Remove any trailing spaces in path
  6590. (if (string-match " +\\'" path)
  6591. (setq path (replace-match "" t t path)))
  6592. (if (and org-link-translation-function
  6593. (fboundp org-link-translation-function))
  6594. ;; Check if we need to translate the link
  6595. (let ((tmp (funcall org-link-translation-function type path)))
  6596. (setq type (car tmp) path (cdr tmp))))
  6597. (cond
  6598. ((assoc type org-link-protocols)
  6599. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6600. ((equal type "mailto")
  6601. (let ((cmd (car org-link-mailto-program))
  6602. (args (cdr org-link-mailto-program)) args1
  6603. (address path) (subject "") a)
  6604. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6605. (setq address (match-string 1 path)
  6606. subject (org-link-escape (match-string 2 path))))
  6607. (while args
  6608. (cond
  6609. ((not (stringp (car args))) (push (pop args) args1))
  6610. (t (setq a (pop args))
  6611. (if (string-match "%a" a)
  6612. (setq a (replace-match address t t a)))
  6613. (if (string-match "%s" a)
  6614. (setq a (replace-match subject t t a)))
  6615. (push a args1))))
  6616. (apply cmd (nreverse args1))))
  6617. ((member type '("http" "https" "ftp" "news"))
  6618. (browse-url (concat type ":" (org-link-escape
  6619. path org-link-escape-chars-browser))))
  6620. ((member type '("message"))
  6621. (browse-url (concat type ":" path)))
  6622. ((string= type "tags")
  6623. (org-tags-view in-emacs path))
  6624. ((string= type "thisfile")
  6625. (if in-emacs
  6626. (switch-to-buffer-other-window
  6627. (org-get-buffer-for-internal-link (current-buffer)))
  6628. (org-mark-ring-push))
  6629. (let ((cmd `(org-link-search
  6630. ,path
  6631. ,(cond ((equal in-emacs '(4)) 'occur)
  6632. ((equal in-emacs '(16)) 'org-occur)
  6633. (t nil))
  6634. ,pos)))
  6635. (condition-case nil (eval cmd)
  6636. (error (progn (widen) (eval cmd))))))
  6637. ((string= type "tree-match")
  6638. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6639. ((string= type "file")
  6640. (if (string-match "::\\([0-9]+\\)\\'" path)
  6641. (setq line (string-to-number (match-string 1 path))
  6642. path (substring path 0 (match-beginning 0)))
  6643. (if (string-match "::\\(.+\\)\\'" path)
  6644. (setq search (match-string 1 path)
  6645. path (substring path 0 (match-beginning 0)))))
  6646. (if (string-match "[*?{]" (file-name-nondirectory path))
  6647. (dired path)
  6648. (org-open-file path in-emacs line search)))
  6649. ((string= type "news")
  6650. (require 'org-gnus)
  6651. (org-gnus-follow-link path))
  6652. ((string= type "shell")
  6653. (let ((cmd path))
  6654. (if (or (not org-confirm-shell-link-function)
  6655. (funcall org-confirm-shell-link-function
  6656. (format "Execute \"%s\" in shell? "
  6657. (org-add-props cmd nil
  6658. 'face 'org-warning))))
  6659. (progn
  6660. (message "Executing %s" cmd)
  6661. (shell-command cmd))
  6662. (error "Abort"))))
  6663. ((string= type "elisp")
  6664. (let ((cmd path))
  6665. (if (or (not org-confirm-elisp-link-function)
  6666. (funcall org-confirm-elisp-link-function
  6667. (format "Execute \"%s\" as elisp? "
  6668. (org-add-props cmd nil
  6669. 'face 'org-warning))))
  6670. (message "%s => %s" cmd
  6671. (if (equal (string-to-char cmd) ?\()
  6672. (eval (read cmd))
  6673. (call-interactively (read cmd))))
  6674. (error "Abort"))))
  6675. (t
  6676. (browse-url-at-point))))))
  6677. (move-marker org-open-link-marker nil)
  6678. (run-hook-with-args 'org-follow-link-hook))
  6679. ;;;; Time estimates
  6680. (defun org-get-effort (&optional pom)
  6681. "Get the effort estimate for the current entry."
  6682. (org-entry-get pom org-effort-property))
  6683. ;;; File search
  6684. (defvar org-create-file-search-functions nil
  6685. "List of functions to construct the right search string for a file link.
  6686. These functions are called in turn with point at the location to
  6687. which the link should point.
  6688. A function in the hook should first test if it would like to
  6689. handle this file type, for example by checking the major-mode or
  6690. the file extension. If it decides not to handle this file, it
  6691. should just return nil to give other functions a chance. If it
  6692. does handle the file, it must return the search string to be used
  6693. when following the link. The search string will be part of the
  6694. file link, given after a double colon, and `org-open-at-point'
  6695. will automatically search for it. If special measures must be
  6696. taken to make the search successful, another function should be
  6697. added to the companion hook `org-execute-file-search-functions',
  6698. which see.
  6699. A function in this hook may also use `setq' to set the variable
  6700. `description' to provide a suggestion for the descriptive text to
  6701. be used for this link when it gets inserted into an Org-mode
  6702. buffer with \\[org-insert-link].")
  6703. (defvar org-execute-file-search-functions nil
  6704. "List of functions to execute a file search triggered by a link.
  6705. Functions added to this hook must accept a single argument, the
  6706. search string that was part of the file link, the part after the
  6707. double colon. The function must first check if it would like to
  6708. handle this search, for example by checking the major-mode or the
  6709. file extension. If it decides not to handle this search, it
  6710. should just return nil to give other functions a chance. If it
  6711. does handle the search, it must return a non-nil value to keep
  6712. other functions from trying.
  6713. Each function can access the current prefix argument through the
  6714. variable `current-prefix-argument'. Note that a single prefix is
  6715. used to force opening a link in Emacs, so it may be good to only
  6716. use a numeric or double prefix to guide the search function.
  6717. In case this is needed, a function in this hook can also restore
  6718. the window configuration before `org-open-at-point' was called using:
  6719. (set-window-configuration org-window-config-before-follow-link)")
  6720. (defun org-link-search (s &optional type avoid-pos)
  6721. "Search for a link search option.
  6722. If S is surrounded by forward slashes, it is interpreted as a
  6723. regular expression. In org-mode files, this will create an `org-occur'
  6724. sparse tree. In ordinary files, `occur' will be used to list matches.
  6725. If the current buffer is in `dired-mode', grep will be used to search
  6726. in all files. If AVOID-POS is given, ignore matches near that position."
  6727. (let ((case-fold-search t)
  6728. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6729. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6730. (append '(("") (" ") ("\t") ("\n"))
  6731. org-emphasis-alist)
  6732. "\\|") "\\)"))
  6733. (pos (point))
  6734. (pre nil) (post nil)
  6735. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6736. (cond
  6737. ;; First check if there are any special
  6738. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6739. ;; Now try the builtin stuff
  6740. ((save-excursion
  6741. (goto-char (point-min))
  6742. (and
  6743. (re-search-forward
  6744. (concat "<<" (regexp-quote s0) ">>") nil t)
  6745. (setq type 'dedicated
  6746. pos (match-beginning 0))))
  6747. ;; There is an exact target for this
  6748. (goto-char pos))
  6749. ((and (string-match "^(\\(.*\\))$" s0)
  6750. (save-excursion
  6751. (goto-char (point-min))
  6752. (and
  6753. (re-search-forward
  6754. (concat "[^[]" (regexp-quote
  6755. (format org-coderef-label-format
  6756. (match-string 1 s0))))
  6757. nil t)
  6758. (setq type 'dedicated
  6759. pos (1+ (match-beginning 0))))))
  6760. ;; There is a coderef target for this
  6761. (goto-char pos))
  6762. ((string-match "^/\\(.*\\)/$" s)
  6763. ;; A regular expression
  6764. (cond
  6765. ((org-mode-p)
  6766. (org-occur (match-string 1 s)))
  6767. ;;((eq major-mode 'dired-mode)
  6768. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6769. (t (org-do-occur (match-string 1 s)))))
  6770. (t
  6771. ;; A normal search strings
  6772. (when (equal (string-to-char s) ?*)
  6773. ;; Anchor on headlines, post may include tags.
  6774. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6775. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6776. s (substring s 1)))
  6777. (remove-text-properties
  6778. 0 (length s)
  6779. '(face nil mouse-face nil keymap nil fontified nil) s)
  6780. ;; Make a series of regular expressions to find a match
  6781. (setq words (org-split-string s "[ \n\r\t]+")
  6782. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6783. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6784. "\\)" markers)
  6785. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6786. re2a (concat "[ \t\r\n]" re2a_)
  6787. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6788. re4 (concat "[^a-zA-Z_]" re4_)
  6789. re1 (concat pre re2 post)
  6790. re3 (concat pre (if pre re4_ re4) post)
  6791. re5 (concat pre ".*" re4)
  6792. re2 (concat pre re2)
  6793. re2a (concat pre (if pre re2a_ re2a))
  6794. re4 (concat pre (if pre re4_ re4))
  6795. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6796. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6797. re5 "\\)"
  6798. ))
  6799. (cond
  6800. ((eq type 'org-occur) (org-occur reall))
  6801. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6802. (t (goto-char (point-min))
  6803. (setq type 'fuzzy)
  6804. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6805. (org-search-not-self 1 re1 nil t)
  6806. (org-search-not-self 1 re2 nil t)
  6807. (org-search-not-self 1 re2a nil t)
  6808. (org-search-not-self 1 re3 nil t)
  6809. (org-search-not-self 1 re4 nil t)
  6810. (org-search-not-self 1 re5 nil t)
  6811. )
  6812. (goto-char (match-beginning 1))
  6813. (goto-char pos)
  6814. (error "No match")))))
  6815. (t
  6816. ;; Normal string-search
  6817. (goto-char (point-min))
  6818. (if (search-forward s nil t)
  6819. (goto-char (match-beginning 0))
  6820. (error "No match"))))
  6821. (and (org-mode-p) (org-show-context 'link-search))
  6822. type))
  6823. (defun org-search-not-self (group &rest args)
  6824. "Execute `re-search-forward', but only accept matches that do not
  6825. enclose the position of `org-open-link-marker'."
  6826. (let ((m org-open-link-marker))
  6827. (catch 'exit
  6828. (while (apply 're-search-forward args)
  6829. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6830. (goto-char (match-end group))
  6831. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6832. (> (match-beginning 0) (marker-position m))
  6833. (< (match-end 0) (marker-position m)))
  6834. (save-match-data
  6835. (or (not (org-in-regexp
  6836. org-bracket-link-analytic-regexp 1))
  6837. (not (match-end 4)) ; no description
  6838. (and (<= (match-beginning 4) (point))
  6839. (>= (match-end 4) (point))))))
  6840. (throw 'exit (point))))))))
  6841. (defun org-get-buffer-for-internal-link (buffer)
  6842. "Return a buffer to be used for displaying the link target of internal links."
  6843. (cond
  6844. ((not org-display-internal-link-with-indirect-buffer)
  6845. buffer)
  6846. ((string-match "(Clone)$" (buffer-name buffer))
  6847. (message "Buffer is already a clone, not making another one")
  6848. ;; we also do not modify visibility in this case
  6849. buffer)
  6850. (t ; make a new indirect buffer for displaying the link
  6851. (let* ((bn (buffer-name buffer))
  6852. (ibn (concat bn "(Clone)"))
  6853. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6854. (with-current-buffer ib (org-overview))
  6855. ib))))
  6856. (defun org-do-occur (regexp &optional cleanup)
  6857. "Call the Emacs command `occur'.
  6858. If CLEANUP is non-nil, remove the printout of the regular expression
  6859. in the *Occur* buffer. This is useful if the regex is long and not useful
  6860. to read."
  6861. (occur regexp)
  6862. (when cleanup
  6863. (let ((cwin (selected-window)) win beg end)
  6864. (when (setq win (get-buffer-window "*Occur*"))
  6865. (select-window win))
  6866. (goto-char (point-min))
  6867. (when (re-search-forward "match[a-z]+" nil t)
  6868. (setq beg (match-end 0))
  6869. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6870. (setq end (1- (match-beginning 0)))))
  6871. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6872. (goto-char (point-min))
  6873. (select-window cwin))))
  6874. ;;; The mark ring for links jumps
  6875. (defvar org-mark-ring nil
  6876. "Mark ring for positions before jumps in Org-mode.")
  6877. (defvar org-mark-ring-last-goto nil
  6878. "Last position in the mark ring used to go back.")
  6879. ;; Fill and close the ring
  6880. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6881. (loop for i from 1 to org-mark-ring-length do
  6882. (push (make-marker) org-mark-ring))
  6883. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6884. org-mark-ring)
  6885. (defun org-mark-ring-push (&optional pos buffer)
  6886. "Put the current position or POS into the mark ring and rotate it."
  6887. (interactive)
  6888. (setq pos (or pos (point)))
  6889. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6890. (move-marker (car org-mark-ring)
  6891. (or pos (point))
  6892. (or buffer (current-buffer)))
  6893. (message "%s"
  6894. (substitute-command-keys
  6895. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6896. (defun org-mark-ring-goto (&optional n)
  6897. "Jump to the previous position in the mark ring.
  6898. With prefix arg N, jump back that many stored positions. When
  6899. called several times in succession, walk through the entire ring.
  6900. Org-mode commands jumping to a different position in the current file,
  6901. or to another Org-mode file, automatically push the old position
  6902. onto the ring."
  6903. (interactive "p")
  6904. (let (p m)
  6905. (if (eq last-command this-command)
  6906. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6907. (setq p org-mark-ring))
  6908. (setq org-mark-ring-last-goto p)
  6909. (setq m (car p))
  6910. (switch-to-buffer (marker-buffer m))
  6911. (goto-char m)
  6912. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6913. (defun org-remove-angle-brackets (s)
  6914. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6915. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6916. s)
  6917. (defun org-add-angle-brackets (s)
  6918. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6919. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6920. s)
  6921. (defun org-remove-double-quotes (s)
  6922. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6923. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6924. s)
  6925. ;;; Following specific links
  6926. (defun org-follow-timestamp-link ()
  6927. (cond
  6928. ((org-at-date-range-p t)
  6929. (let ((org-agenda-start-on-weekday)
  6930. (t1 (match-string 1))
  6931. (t2 (match-string 2)))
  6932. (setq t1 (time-to-days (org-time-string-to-time t1))
  6933. t2 (time-to-days (org-time-string-to-time t2)))
  6934. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6935. ((org-at-timestamp-p t)
  6936. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6937. (substring (match-string 1) 0 10)))
  6938. 1))
  6939. (t (error "This should not happen"))))
  6940. ;;; Following file links
  6941. (defvar org-wait nil)
  6942. (defun org-open-file (path &optional in-emacs line search)
  6943. "Open the file at PATH.
  6944. First, this expands any special file name abbreviations. Then the
  6945. configuration variable `org-file-apps' is checked if it contains an
  6946. entry for this file type, and if yes, the corresponding command is launched.
  6947. If no application is found, Emacs simply visits the file.
  6948. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6949. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6950. and o use an external application to visit the file.
  6951. Optional LINE specifies a line to go to, optional SEARCH a string to
  6952. search for. If LINE or SEARCH is given, the file will always be
  6953. opened in Emacs.
  6954. If the file does not exist, an error is thrown."
  6955. (setq in-emacs (or in-emacs line search))
  6956. (let* ((file (if (equal path "")
  6957. buffer-file-name
  6958. (substitute-in-file-name (expand-file-name path))))
  6959. (apps (append org-file-apps (org-default-apps)))
  6960. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6961. (dirp (if remp nil (file-directory-p file)))
  6962. (file (if (and dirp org-open-directory-means-index-dot-org)
  6963. (concat (file-name-as-directory file) "index.org")
  6964. file))
  6965. (a-m-a-p (assq 'auto-mode apps))
  6966. (dfile (downcase file))
  6967. (old-buffer (current-buffer))
  6968. (old-pos (point))
  6969. (old-mode major-mode)
  6970. ext cmd)
  6971. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6972. (setq ext (match-string 1 dfile))
  6973. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6974. (setq ext (match-string 1 dfile))))
  6975. (cond
  6976. ((equal in-emacs '(16))
  6977. (setq cmd (cdr (assoc 'system apps))))
  6978. (in-emacs (setq cmd 'emacs))
  6979. (t
  6980. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6981. (and dirp (cdr (assoc 'directory apps)))
  6982. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6983. 'string-match)
  6984. (cdr (assoc ext apps))
  6985. (cdr (assoc t apps))))))
  6986. (when (eq cmd 'system)
  6987. (setq cmd (cdr (assoc 'system apps))))
  6988. (when (eq cmd 'default)
  6989. (setq cmd (cdr (assoc t apps))))
  6990. (when (eq cmd 'mailcap)
  6991. (require 'mailcap)
  6992. (mailcap-parse-mailcaps)
  6993. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6994. (command (mailcap-mime-info mime-type)))
  6995. (if (stringp command)
  6996. (setq cmd command)
  6997. (setq cmd 'emacs))))
  6998. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6999. (not (file-exists-p file))
  7000. (not org-open-non-existing-files))
  7001. (error "No such file: %s" file))
  7002. (cond
  7003. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7004. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7005. (while (string-match "['\"]%s['\"]" cmd)
  7006. (setq cmd (replace-match "%s" t t cmd)))
  7007. (while (string-match "%s" cmd)
  7008. (setq cmd (replace-match
  7009. (save-match-data
  7010. (shell-quote-argument
  7011. (convert-standard-filename file)))
  7012. t t cmd)))
  7013. (save-window-excursion
  7014. (start-process-shell-command cmd nil cmd)
  7015. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7016. ))
  7017. ((or (stringp cmd)
  7018. (eq cmd 'emacs))
  7019. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7020. (widen)
  7021. (if line (goto-line line)
  7022. (if search (org-link-search search))))
  7023. ((consp cmd)
  7024. (let ((file (convert-standard-filename file)))
  7025. (eval cmd)))
  7026. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7027. (and (org-mode-p) (eq old-mode 'org-mode)
  7028. (or (not (equal old-buffer (current-buffer)))
  7029. (not (equal old-pos (point))))
  7030. (org-mark-ring-push old-pos old-buffer))))
  7031. (defun org-default-apps ()
  7032. "Return the default applications for this operating system."
  7033. (cond
  7034. ((eq system-type 'darwin)
  7035. org-file-apps-defaults-macosx)
  7036. ((eq system-type 'windows-nt)
  7037. org-file-apps-defaults-windowsnt)
  7038. (t org-file-apps-defaults-gnu)))
  7039. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7040. "Convert extensions to regular expressions in the cars of LIST.
  7041. Also, weed out any non-string entries, because the return value is used
  7042. only for regexp matching.
  7043. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7044. point to the symbol `emacs', indicating that the file should
  7045. be opened in Emacs."
  7046. (append
  7047. (delq nil
  7048. (mapcar (lambda (x)
  7049. (if (not (stringp (car x)))
  7050. nil
  7051. (if (string-match "\\W" (car x))
  7052. x
  7053. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7054. list))
  7055. (if add-auto-mode
  7056. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7057. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7058. (defun org-file-remote-p (file)
  7059. "Test whether FILE specifies a location on a remote system.
  7060. Return non-nil if the location is indeed remote.
  7061. For example, the filename \"/user@host:/foo\" specifies a location
  7062. on the system \"/user@host:\"."
  7063. (cond ((fboundp 'file-remote-p)
  7064. (file-remote-p file))
  7065. ((fboundp 'tramp-handle-file-remote-p)
  7066. (tramp-handle-file-remote-p file))
  7067. ((and (boundp 'ange-ftp-name-format)
  7068. (string-match (car ange-ftp-name-format) file))
  7069. t)
  7070. (t nil)))
  7071. ;;;; Refiling
  7072. (defun org-get-org-file ()
  7073. "Read a filename, with default directory `org-directory'."
  7074. (let ((default (or org-default-notes-file remember-data-file)))
  7075. (read-file-name (format "File name [%s]: " default)
  7076. (file-name-as-directory org-directory)
  7077. default)))
  7078. (defun org-notes-order-reversed-p ()
  7079. "Check if the current file should receive notes in reversed order."
  7080. (cond
  7081. ((not org-reverse-note-order) nil)
  7082. ((eq t org-reverse-note-order) t)
  7083. ((not (listp org-reverse-note-order)) nil)
  7084. (t (catch 'exit
  7085. (let ((all org-reverse-note-order)
  7086. entry)
  7087. (while (setq entry (pop all))
  7088. (if (string-match (car entry) buffer-file-name)
  7089. (throw 'exit (cdr entry))))
  7090. nil)))))
  7091. (defvar org-refile-target-table nil
  7092. "The list of refile targets, created by `org-refile'.")
  7093. (defvar org-agenda-new-buffers nil
  7094. "Buffers created to visit agenda files.")
  7095. (defun org-get-refile-targets (&optional default-buffer)
  7096. "Produce a table with refile targets."
  7097. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7098. targets txt re files f desc descre fast-path-p level)
  7099. (message "Getting targets...")
  7100. (with-current-buffer (or default-buffer (current-buffer))
  7101. (while (setq entry (pop entries))
  7102. (setq files (car entry) desc (cdr entry))
  7103. (setq fast-path-p nil)
  7104. (cond
  7105. ((null files) (setq files (list (current-buffer))))
  7106. ((eq files 'org-agenda-files)
  7107. (setq files (org-agenda-files 'unrestricted)))
  7108. ((and (symbolp files) (fboundp files))
  7109. (setq files (funcall files)))
  7110. ((and (symbolp files) (boundp files))
  7111. (setq files (symbol-value files))))
  7112. (if (stringp files) (setq files (list files)))
  7113. (cond
  7114. ((eq (car desc) :tag)
  7115. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7116. ((eq (car desc) :todo)
  7117. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7118. ((eq (car desc) :regexp)
  7119. (setq descre (cdr desc)))
  7120. ((eq (car desc) :level)
  7121. (setq descre (concat "^\\*\\{" (number-to-string
  7122. (if org-odd-levels-only
  7123. (1- (* 2 (cdr desc)))
  7124. (cdr desc)))
  7125. "\\}[ \t]")))
  7126. ((eq (car desc) :maxlevel)
  7127. (setq fast-path-p t)
  7128. (setq descre (concat "^\\*\\{1," (number-to-string
  7129. (if org-odd-levels-only
  7130. (1- (* 2 (cdr desc)))
  7131. (cdr desc)))
  7132. "\\}[ \t]")))
  7133. (t (error "Bad refiling target description %s" desc)))
  7134. (while (setq f (pop files))
  7135. (save-excursion
  7136. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7137. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7138. (setq f (expand-file-name f))
  7139. (save-excursion
  7140. (save-restriction
  7141. (widen)
  7142. (goto-char (point-min))
  7143. (while (re-search-forward descre nil t)
  7144. (goto-char (point-at-bol))
  7145. (when (looking-at org-complex-heading-regexp)
  7146. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7147. txt (org-link-display-format (match-string 4))
  7148. re (concat "^" (regexp-quote
  7149. (buffer-substring (match-beginning 1)
  7150. (match-end 4)))))
  7151. (if (match-end 5) (setq re (concat re "[ \t]+"
  7152. (regexp-quote
  7153. (match-string 5)))))
  7154. (setq re (concat re "[ \t]*$"))
  7155. (when org-refile-use-outline-path
  7156. (setq txt (mapconcat 'org-protect-slash
  7157. (append
  7158. (if (eq org-refile-use-outline-path 'file)
  7159. (list (file-name-nondirectory
  7160. (buffer-file-name (buffer-base-buffer))))
  7161. (if (eq org-refile-use-outline-path 'full-file-path)
  7162. (list (buffer-file-name (buffer-base-buffer)))))
  7163. (org-get-outline-path fast-path-p level txt)
  7164. (list txt))
  7165. "/")))
  7166. (push (list txt f re (point)) targets))
  7167. (goto-char (point-at-eol))))))))
  7168. (message "Getting targets...done")
  7169. (nreverse targets))))
  7170. (defun org-protect-slash (s)
  7171. (while (string-match "/" s)
  7172. (setq s (replace-match "\\" t t s)))
  7173. s)
  7174. (defvar org-olpa (make-vector 20 nil))
  7175. (defun org-get-outline-path (&optional fastp level heading)
  7176. "Return the outline path to the current entry, as a list."
  7177. (if fastp
  7178. (progn
  7179. (if (> level 19)
  7180. (error "Outline path failure, more than 19 levels."))
  7181. (loop for i from level upto 19 do
  7182. (aset org-olpa i nil))
  7183. (prog1
  7184. (delq nil (append org-olpa nil))
  7185. (aset org-olpa level heading)))
  7186. (let (rtn)
  7187. (save-excursion
  7188. (while (org-up-heading-safe)
  7189. (when (looking-at org-complex-heading-regexp)
  7190. (push (org-match-string-no-properties 4) rtn)))
  7191. rtn))))
  7192. (defvar org-refile-history nil
  7193. "History for refiling operations.")
  7194. (defun org-refile (&optional goto default-buffer)
  7195. "Move the entry at point to another heading.
  7196. The list of target headings is compiled using the information in
  7197. `org-refile-targets', which see. This list is created before each use
  7198. and will therefore always be up-to-date.
  7199. At the target location, the entry is filed as a subitem of the target heading.
  7200. Depending on `org-reverse-note-order', the new subitem will either be the
  7201. first or the last subitem.
  7202. If there is an active region, all entries in that region will be moved.
  7203. However, the region must fulfil the requirement that the first heading
  7204. is the first one sets the top-level of the moved text - at most siblings
  7205. below it are allowed.
  7206. With prefix arg GOTO, the command will only visit the target location,
  7207. not actually move anything.
  7208. With a double prefix `C-u C-u', go to the location where the last refiling
  7209. operation has put the subtree."
  7210. (interactive "P")
  7211. (let* ((cbuf (current-buffer))
  7212. (regionp (org-region-active-p))
  7213. (region-start (and regionp (region-beginning)))
  7214. (region-end (and regionp (region-end)))
  7215. (region-length (and regionp (- region-end region-start)))
  7216. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7217. pos it nbuf file re level reversed)
  7218. (when regionp (goto-char region-start)
  7219. (unless (org-kill-is-subtree-p
  7220. (buffer-substring region-start region-end))
  7221. (error "The region is not a (sequence of) subtree(s)")))
  7222. (if (equal goto '(16))
  7223. (org-refile-goto-last-stored)
  7224. (when (setq it (org-refile-get-location
  7225. (if goto "Goto: " "Refile to: ") default-buffer))
  7226. (setq file (nth 1 it)
  7227. re (nth 2 it)
  7228. pos (nth 3 it))
  7229. (if (and (equal (buffer-file-name) file)
  7230. (if regionp
  7231. (and (>= pos region-start)
  7232. (<= pos region-end))
  7233. (and (>= pos (point))
  7234. (< pos (save-excursion
  7235. (org-end-of-subtree t t))))))
  7236. (error "Cannot refile to position inside the tree or region"))
  7237. (setq nbuf (or (find-buffer-visiting file)
  7238. (find-file-noselect file)))
  7239. (if goto
  7240. (progn
  7241. (switch-to-buffer nbuf)
  7242. (goto-char pos)
  7243. (org-show-context 'org-goto))
  7244. (if regionp
  7245. (progn
  7246. (kill-new (buffer-substring region-start region-end))
  7247. (org-save-markers-in-region region-start region-end))
  7248. (org-copy-subtree 1 nil t))
  7249. (save-excursion
  7250. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7251. (find-file-noselect file))))
  7252. (setq reversed (org-notes-order-reversed-p))
  7253. (save-excursion
  7254. (save-restriction
  7255. (widen)
  7256. (goto-char pos)
  7257. (looking-at outline-regexp)
  7258. (setq level (org-get-valid-level (funcall outline-level) 1))
  7259. (goto-char
  7260. (if reversed
  7261. (or (outline-next-heading) (point-max))
  7262. (or (save-excursion (outline-get-next-sibling))
  7263. (org-end-of-subtree t t)
  7264. (point-max))))
  7265. (if (not (bolp)) (newline))
  7266. (bookmark-set "org-refile-last-stored")
  7267. (org-paste-subtree level))))
  7268. (if regionp
  7269. (delete-region (point) (+ (point) region-length))
  7270. (org-cut-subtree))
  7271. (setq org-markers-to-move nil)
  7272. (message "Refiled to \"%s\"" (car it)))))))
  7273. (defun org-refile-goto-last-stored ()
  7274. "Go to the location where the last refile was stored."
  7275. (interactive)
  7276. (bookmark-jump "org-refile-last-stored")
  7277. (message "This is the location of the last refile"))
  7278. (defun org-refile-get-location (&optional prompt default-buffer)
  7279. "Prompt the user for a refile location, using PROMPT."
  7280. (let ((org-refile-targets org-refile-targets)
  7281. (org-refile-use-outline-path org-refile-use-outline-path))
  7282. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7283. (unless org-refile-target-table
  7284. (error "No refile targets"))
  7285. (let* ((cbuf (current-buffer))
  7286. (partial-completion-mode nil)
  7287. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7288. (cfunc (if (and org-refile-use-outline-path
  7289. org-outline-path-complete-in-steps)
  7290. 'org-olpath-completing-read
  7291. 'org-ido-completing-read))
  7292. (extra (if org-refile-use-outline-path "/" ""))
  7293. (filename (and cfn (expand-file-name cfn)))
  7294. (tbl (mapcar
  7295. (lambda (x)
  7296. (if (not (equal filename (nth 1 x)))
  7297. (cons (concat (car x) extra " ("
  7298. (file-name-nondirectory (nth 1 x)) ")")
  7299. (cdr x))
  7300. (cons (concat (car x) extra) (cdr x))))
  7301. org-refile-target-table))
  7302. (completion-ignore-case t))
  7303. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7304. tbl)))
  7305. (defun org-olpath-completing-read (prompt collection &rest args)
  7306. "Read an outline path like a file name."
  7307. (let ((thetable collection)
  7308. (org-completion-use-ido nil)) ; does not work with ido.
  7309. (apply
  7310. 'org-ido-completing-read prompt
  7311. (lambda (string predicate &optional flag)
  7312. (let (rtn r f (l (length string)))
  7313. (cond
  7314. ((eq flag nil)
  7315. ;; try completion
  7316. (try-completion string thetable))
  7317. ((eq flag t)
  7318. ;; all-completions
  7319. (setq rtn (all-completions string thetable predicate))
  7320. (mapcar
  7321. (lambda (x)
  7322. (setq r (substring x l))
  7323. (if (string-match " ([^)]*)$" x)
  7324. (setq f (match-string 0 x))
  7325. (setq f ""))
  7326. (if (string-match "/" r)
  7327. (concat string (substring r 0 (match-end 0)) f)
  7328. x))
  7329. rtn))
  7330. ((eq flag 'lambda)
  7331. ;; exact match?
  7332. (assoc string thetable)))
  7333. ))
  7334. args)))
  7335. ;;;; Dynamic blocks
  7336. (defun org-find-dblock (name)
  7337. "Find the first dynamic block with name NAME in the buffer.
  7338. If not found, stay at current position and return nil."
  7339. (let (pos)
  7340. (save-excursion
  7341. (goto-char (point-min))
  7342. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7343. nil t)
  7344. (match-beginning 0))))
  7345. (if pos (goto-char pos))
  7346. pos))
  7347. (defconst org-dblock-start-re
  7348. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7349. "Matches the startline of a dynamic block, with parameters.")
  7350. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7351. "Matches the end of a dynamic block.")
  7352. (defun org-create-dblock (plist)
  7353. "Create a dynamic block section, with parameters taken from PLIST.
  7354. PLIST must contain a :name entry which is used as name of the block."
  7355. (unless (bolp) (newline))
  7356. (let ((name (plist-get plist :name)))
  7357. (insert "#+BEGIN: " name)
  7358. (while plist
  7359. (if (eq (car plist) :name)
  7360. (setq plist (cddr plist))
  7361. (insert " " (prin1-to-string (pop plist)))))
  7362. (insert "\n\n#+END:\n")
  7363. (beginning-of-line -2)))
  7364. (defun org-prepare-dblock ()
  7365. "Prepare dynamic block for refresh.
  7366. This empties the block, puts the cursor at the insert position and returns
  7367. the property list including an extra property :name with the block name."
  7368. (unless (looking-at org-dblock-start-re)
  7369. (error "Not at a dynamic block"))
  7370. (let* ((begdel (1+ (match-end 0)))
  7371. (name (org-no-properties (match-string 1)))
  7372. (params (append (list :name name)
  7373. (read (concat "(" (match-string 3) ")")))))
  7374. (unless (re-search-forward org-dblock-end-re nil t)
  7375. (error "Dynamic block not terminated"))
  7376. (setq params
  7377. (append params
  7378. (list :content (buffer-substring
  7379. begdel (match-beginning 0)))))
  7380. (delete-region begdel (match-beginning 0))
  7381. (goto-char begdel)
  7382. (open-line 1)
  7383. params))
  7384. (defun org-map-dblocks (&optional command)
  7385. "Apply COMMAND to all dynamic blocks in the current buffer.
  7386. If COMMAND is not given, use `org-update-dblock'."
  7387. (let ((cmd (or command 'org-update-dblock))
  7388. pos)
  7389. (save-excursion
  7390. (goto-char (point-min))
  7391. (while (re-search-forward org-dblock-start-re nil t)
  7392. (goto-char (setq pos (match-beginning 0)))
  7393. (condition-case nil
  7394. (funcall cmd)
  7395. (error (message "Error during update of dynamic block")))
  7396. (goto-char pos)
  7397. (unless (re-search-forward org-dblock-end-re nil t)
  7398. (error "Dynamic block not terminated"))))))
  7399. (defun org-dblock-update (&optional arg)
  7400. "User command for updating dynamic blocks.
  7401. Update the dynamic block at point. With prefix ARG, update all dynamic
  7402. blocks in the buffer."
  7403. (interactive "P")
  7404. (if arg
  7405. (org-update-all-dblocks)
  7406. (or (looking-at org-dblock-start-re)
  7407. (org-beginning-of-dblock))
  7408. (org-update-dblock)))
  7409. (defun org-update-dblock ()
  7410. "Update the dynamic block at point
  7411. This means to empty the block, parse for parameters and then call
  7412. the correct writing function."
  7413. (save-window-excursion
  7414. (let* ((pos (point))
  7415. (line (org-current-line))
  7416. (params (org-prepare-dblock))
  7417. (name (plist-get params :name))
  7418. (cmd (intern (concat "org-dblock-write:" name))))
  7419. (message "Updating dynamic block `%s' at line %d..." name line)
  7420. (funcall cmd params)
  7421. (message "Updating dynamic block `%s' at line %d...done" name line)
  7422. (goto-char pos))))
  7423. (defun org-beginning-of-dblock ()
  7424. "Find the beginning of the dynamic block at point.
  7425. Error if there is no such block at point."
  7426. (let ((pos (point))
  7427. beg)
  7428. (end-of-line 1)
  7429. (if (and (re-search-backward org-dblock-start-re nil t)
  7430. (setq beg (match-beginning 0))
  7431. (re-search-forward org-dblock-end-re nil t)
  7432. (> (match-end 0) pos))
  7433. (goto-char beg)
  7434. (goto-char pos)
  7435. (error "Not in a dynamic block"))))
  7436. (defun org-update-all-dblocks ()
  7437. "Update all dynamic blocks in the buffer.
  7438. This function can be used in a hook."
  7439. (when (org-mode-p)
  7440. (org-map-dblocks 'org-update-dblock)))
  7441. ;;;; Completion
  7442. (defconst org-additional-option-like-keywords
  7443. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7444. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7445. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7446. "BEGIN_QUOTE" "END_QUOTE"
  7447. "BEGIN_VERSE" "END_VERSE"
  7448. "BEGIN_SRC" "END_SRC"
  7449. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7450. (defcustom org-structure-template-alist
  7451. '(
  7452. ("s" "#+begin_src ?\n\n#+end_src"
  7453. "<src lang=\"?\">\n\n</src>")
  7454. ("e" "#+begin_example\n?\n#+end_example"
  7455. "<example>\n?\n</example>")
  7456. ("q" "#+begin_quote\n?\n#+end_quote"
  7457. "<quote>\n?\n</quote>")
  7458. ("v" "#+begin_verse\n?\n#+end_verse"
  7459. "<verse>\n?\n/verse>")
  7460. ("l" "#+begin_latex\n?\n#+end_latex"
  7461. "<literal style=\"latex\">\n?\n</literal>")
  7462. ("L" "#+latex: "
  7463. "<literal style=\"latex\">?</literal>")
  7464. ("h" "#+begin_html\n?\n#+end_html"
  7465. "<literal style=\"html\">\n?\n</literal>")
  7466. ("H" "#+html: "
  7467. "<literal style=\"html\">?</literal>")
  7468. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7469. ("A" "#+ascii: ")
  7470. ("i" "#+include %file ?"
  7471. "<include file=%file markup=\"?\">")
  7472. )
  7473. "Structure completion elements.
  7474. This is a list of abbreviation keys and values. The value gets inserted
  7475. it you type @samp{.} followed by the key and then the completion key,
  7476. usually `M-TAB'. %file will be replaced by a file name after prompting
  7477. for the file using completion.
  7478. There are two templates for each key, the first uses the original Org syntax,
  7479. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7480. the default when the /org-mtags.el/ module has been loaded. See also the
  7481. variable `org-mtags-prefer-muse-templates'.
  7482. This is an experimental feature, it is undecided if it is going to stay in."
  7483. :group 'org-completion
  7484. :type '(repeat
  7485. (string :tag "Key")
  7486. (string :tag "Template")
  7487. (string :tag "Muse Template")))
  7488. (defun org-try-structure-completion ()
  7489. "Try to complete a structure template before point.
  7490. This looks for strings like \"<e\" on an otherwise empty line and
  7491. expands them."
  7492. (let ((l (buffer-substring (point-at-bol) (point)))
  7493. a)
  7494. (when (and (looking-at "[ \t]*$")
  7495. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7496. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7497. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7498. (match-beginning 1)) a)
  7499. t)))
  7500. (defun org-complete-expand-structure-template (start cell)
  7501. "Expand a structure template."
  7502. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7503. (rpl (nth (if musep 2 1) cell)))
  7504. (delete-region start (point))
  7505. (when (string-match "\\`#\\+" rpl)
  7506. (cond
  7507. ((bolp))
  7508. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7509. (delete-region (point-at-bol) (point)))
  7510. (t (newline))))
  7511. (setq start (point))
  7512. (if (string-match "%file" rpl)
  7513. (setq rpl (replace-match
  7514. (concat
  7515. "\""
  7516. (save-match-data
  7517. (abbreviate-file-name (read-file-name "Include file: ")))
  7518. "\"")
  7519. t t rpl)))
  7520. (insert rpl)
  7521. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7522. (defun org-complete (&optional arg)
  7523. "Perform completion on word at point.
  7524. At the beginning of a headline, this completes TODO keywords as given in
  7525. `org-todo-keywords'.
  7526. If the current word is preceded by a backslash, completes the TeX symbols
  7527. that are supported for HTML support.
  7528. If the current word is preceded by \"#+\", completes special words for
  7529. setting file options.
  7530. In the line after \"#+STARTUP:, complete valid keywords.\"
  7531. At all other locations, this simply calls the value of
  7532. `org-completion-fallback-command'."
  7533. (interactive "P")
  7534. (org-without-partial-completion
  7535. (catch 'exit
  7536. (let* ((a nil)
  7537. (end (point))
  7538. (beg1 (save-excursion
  7539. (skip-chars-backward (org-re "[:alnum:]_@"))
  7540. (point)))
  7541. (beg (save-excursion
  7542. (skip-chars-backward "a-zA-Z0-9_:$")
  7543. (point)))
  7544. (confirm (lambda (x) (stringp (car x))))
  7545. (searchhead (equal (char-before beg) ?*))
  7546. (struct
  7547. (when (and (member (char-before beg1) '(?. ?<))
  7548. (setq a (assoc (buffer-substring beg1 (point))
  7549. org-structure-template-alist)))
  7550. (org-complete-expand-structure-template (1- beg1) a)
  7551. (throw 'exit t)))
  7552. (tag (and (equal (char-before beg1) ?:)
  7553. (equal (char-after (point-at-bol)) ?*)))
  7554. (prop (and (equal (char-before beg1) ?:)
  7555. (not (equal (char-after (point-at-bol)) ?*))))
  7556. (texp (equal (char-before beg) ?\\))
  7557. (link (equal (char-before beg) ?\[))
  7558. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7559. beg)
  7560. "#+"))
  7561. (startup (string-match "^#\\+STARTUP:.*"
  7562. (buffer-substring (point-at-bol) (point))))
  7563. (completion-ignore-case opt)
  7564. (type nil)
  7565. (tbl nil)
  7566. (table (cond
  7567. (opt
  7568. (setq type :opt)
  7569. (require 'org-exp)
  7570. (append
  7571. (mapcar
  7572. (lambda (x)
  7573. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7574. (cons (match-string 2 x) (match-string 1 x)))
  7575. (org-split-string (org-get-current-options) "\n"))
  7576. (mapcar 'list org-additional-option-like-keywords)))
  7577. (startup
  7578. (setq type :startup)
  7579. org-startup-options)
  7580. (link (append org-link-abbrev-alist-local
  7581. org-link-abbrev-alist))
  7582. (texp
  7583. (setq type :tex)
  7584. org-html-entities)
  7585. ((string-match "\\`\\*+[ \t]+\\'"
  7586. (buffer-substring (point-at-bol) beg))
  7587. (setq type :todo)
  7588. (mapcar 'list org-todo-keywords-1))
  7589. (searchhead
  7590. (setq type :searchhead)
  7591. (save-excursion
  7592. (goto-char (point-min))
  7593. (while (re-search-forward org-todo-line-regexp nil t)
  7594. (push (list
  7595. (org-make-org-heading-search-string
  7596. (match-string 3) t))
  7597. tbl)))
  7598. tbl)
  7599. (tag (setq type :tag beg beg1)
  7600. (or org-tag-alist (org-get-buffer-tags)))
  7601. (prop (setq type :prop beg beg1)
  7602. (mapcar 'list (org-buffer-property-keys nil t t)))
  7603. (t (progn
  7604. (call-interactively org-completion-fallback-command)
  7605. (throw 'exit nil)))))
  7606. (pattern (buffer-substring-no-properties beg end))
  7607. (completion (try-completion pattern table confirm)))
  7608. (cond ((eq completion t)
  7609. (if (not (assoc (upcase pattern) table))
  7610. (message "Already complete")
  7611. (if (and (equal type :opt)
  7612. (not (member (car (assoc (upcase pattern) table))
  7613. org-additional-option-like-keywords)))
  7614. (insert (substring (cdr (assoc (upcase pattern) table))
  7615. (length pattern)))
  7616. (if (memq type '(:tag :prop)) (insert ":")))))
  7617. ((null completion)
  7618. (message "Can't find completion for \"%s\"" pattern)
  7619. (ding))
  7620. ((not (string= pattern completion))
  7621. (delete-region beg end)
  7622. (if (string-match " +$" completion)
  7623. (setq completion (replace-match "" t t completion)))
  7624. (insert completion)
  7625. (if (get-buffer-window "*Completions*")
  7626. (delete-window (get-buffer-window "*Completions*")))
  7627. (if (assoc completion table)
  7628. (if (eq type :todo) (insert " ")
  7629. (if (memq type '(:tag :prop)) (insert ":"))))
  7630. (if (and (equal type :opt) (assoc completion table))
  7631. (message "%s" (substitute-command-keys
  7632. "Press \\[org-complete] again to insert example settings"))))
  7633. (t
  7634. (message "Making completion list...")
  7635. (let ((list (sort (all-completions pattern table confirm)
  7636. 'string<)))
  7637. (with-output-to-temp-buffer "*Completions*"
  7638. (condition-case nil
  7639. ;; Protection needed for XEmacs and emacs 21
  7640. (display-completion-list list pattern)
  7641. (error (display-completion-list list)))))
  7642. (message "Making completion list...%s" "done")))))))
  7643. ;;;; TODO, DEADLINE, Comments
  7644. (defun org-toggle-comment ()
  7645. "Change the COMMENT state of an entry."
  7646. (interactive)
  7647. (save-excursion
  7648. (org-back-to-heading)
  7649. (let (case-fold-search)
  7650. (if (looking-at (concat outline-regexp
  7651. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7652. (replace-match "" t t nil 1)
  7653. (if (looking-at outline-regexp)
  7654. (progn
  7655. (goto-char (match-end 0))
  7656. (insert org-comment-string " ")))))))
  7657. (defvar org-last-todo-state-is-todo nil
  7658. "This is non-nil when the last TODO state change led to a TODO state.
  7659. If the last change removed the TODO tag or switched to DONE, then
  7660. this is nil.")
  7661. (defvar org-setting-tags nil) ; dynamically skipped
  7662. (defun org-parse-local-options (string var)
  7663. "Parse STRING for startup setting relevant for variable VAR."
  7664. (let ((rtn (symbol-value var))
  7665. e opts)
  7666. (save-match-data
  7667. (if (or (not string) (not (string-match "\\S-" string)))
  7668. rtn
  7669. (setq opts (delq nil (mapcar (lambda (x)
  7670. (setq e (assoc x org-startup-options))
  7671. (if (eq (nth 1 e) var) e nil))
  7672. (org-split-string string "[ \t]+"))))
  7673. (if (not opts)
  7674. rtn
  7675. (setq rtn nil)
  7676. (while (setq e (pop opts))
  7677. (if (not (nth 3 e))
  7678. (setq rtn (nth 2 e))
  7679. (if (not (listp rtn)) (setq rtn nil))
  7680. (push (nth 2 e) rtn)))
  7681. rtn)))))
  7682. (defvar org-todo-setup-filter-hook nil
  7683. "Hook for functions that pre-filter todo specs.
  7684. Each function takes a todo spec and returns either `nil' or the spec
  7685. transformed into canonical form." )
  7686. (defvar org-todo-get-default-hook nil
  7687. "Hook for functions that get a default item for todo.
  7688. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7689. `nil' or a string to be used for the todo mark." )
  7690. (defvar org-agenda-headline-snapshot-before-repeat)
  7691. (defun org-todo (&optional arg)
  7692. "Change the TODO state of an item.
  7693. The state of an item is given by a keyword at the start of the heading,
  7694. like
  7695. *** TODO Write paper
  7696. *** DONE Call mom
  7697. The different keywords are specified in the variable `org-todo-keywords'.
  7698. By default the available states are \"TODO\" and \"DONE\".
  7699. So for this example: when the item starts with TODO, it is changed to DONE.
  7700. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7701. DONE are present, add TODO at the beginning of the heading.
  7702. With C-u prefix arg, use completion to determine the new state.
  7703. With numeric prefix arg, switch to that state.
  7704. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7705. With a tripple C-u prefix, circumvent any state blocking.
  7706. For calling through lisp, arg is also interpreted in the following way:
  7707. 'none -> empty state
  7708. \"\"(empty string) -> switch to empty state
  7709. 'done -> switch to DONE
  7710. 'nextset -> switch to the next set of keywords
  7711. 'previousset -> switch to the previous set of keywords
  7712. \"WAITING\" -> switch to the specified keyword, but only if it
  7713. really is a member of `org-todo-keywords'."
  7714. (interactive "P")
  7715. (if (equal arg '(16)) (setq arg 'nextset))
  7716. (let ((org-blocker-hook org-blocker-hook))
  7717. (when (equal arg '(64))
  7718. (setq arg nil org-blocker-hook nil))
  7719. (save-excursion
  7720. (catch 'exit
  7721. (org-back-to-heading)
  7722. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7723. (or (looking-at (concat " +" org-todo-regexp " *"))
  7724. (looking-at " *"))
  7725. (let* ((match-data (match-data))
  7726. (startpos (point-at-bol))
  7727. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7728. (org-log-done org-log-done)
  7729. (org-log-repeat org-log-repeat)
  7730. (org-todo-log-states org-todo-log-states)
  7731. (this (match-string 1))
  7732. (hl-pos (match-beginning 0))
  7733. (head (org-get-todo-sequence-head this))
  7734. (ass (assoc head org-todo-kwd-alist))
  7735. (interpret (nth 1 ass))
  7736. (done-word (nth 3 ass))
  7737. (final-done-word (nth 4 ass))
  7738. (last-state (or this ""))
  7739. (completion-ignore-case t)
  7740. (member (member this org-todo-keywords-1))
  7741. (tail (cdr member))
  7742. (state (cond
  7743. ((and org-todo-key-trigger
  7744. (or (and (equal arg '(4))
  7745. (eq org-use-fast-todo-selection 'prefix))
  7746. (and (not arg) org-use-fast-todo-selection
  7747. (not (eq org-use-fast-todo-selection
  7748. 'prefix)))))
  7749. ;; Use fast selection
  7750. (org-fast-todo-selection))
  7751. ((and (equal arg '(4))
  7752. (or (not org-use-fast-todo-selection)
  7753. (not org-todo-key-trigger)))
  7754. ;; Read a state with completion
  7755. (org-ido-completing-read
  7756. "State: " (mapcar (lambda(x) (list x))
  7757. org-todo-keywords-1)
  7758. nil t))
  7759. ((eq arg 'right)
  7760. (if this
  7761. (if tail (car tail) nil)
  7762. (car org-todo-keywords-1)))
  7763. ((eq arg 'left)
  7764. (if (equal member org-todo-keywords-1)
  7765. nil
  7766. (if this
  7767. (nth (- (length org-todo-keywords-1)
  7768. (length tail) 2)
  7769. org-todo-keywords-1)
  7770. (org-last org-todo-keywords-1))))
  7771. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7772. (setq arg nil))) ; hack to fall back to cycling
  7773. (arg
  7774. ;; user or caller requests a specific state
  7775. (cond
  7776. ((equal arg "") nil)
  7777. ((eq arg 'none) nil)
  7778. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7779. ((eq arg 'nextset)
  7780. (or (car (cdr (member head org-todo-heads)))
  7781. (car org-todo-heads)))
  7782. ((eq arg 'previousset)
  7783. (let ((org-todo-heads (reverse org-todo-heads)))
  7784. (or (car (cdr (member head org-todo-heads)))
  7785. (car org-todo-heads))))
  7786. ((car (member arg org-todo-keywords-1)))
  7787. ((nth (1- (prefix-numeric-value arg))
  7788. org-todo-keywords-1))))
  7789. ((null member) (or head (car org-todo-keywords-1)))
  7790. ((equal this final-done-word) nil) ;; -> make empty
  7791. ((null tail) nil) ;; -> first entry
  7792. ((memq interpret '(type priority))
  7793. (if (eq this-command last-command)
  7794. (car tail)
  7795. (if (> (length tail) 0)
  7796. (or done-word (car org-done-keywords))
  7797. nil)))
  7798. (t
  7799. (car tail))))
  7800. (state (or
  7801. (run-hook-with-args-until-success
  7802. 'org-todo-get-default-hook state last-state)
  7803. state))
  7804. (next (if state (concat " " state " ") " "))
  7805. (change-plist (list :type 'todo-state-change :from this :to state
  7806. :position startpos))
  7807. dolog now-done-p)
  7808. (when org-blocker-hook
  7809. (setq org-last-todo-state-is-todo
  7810. (not (member this org-done-keywords)))
  7811. (unless (save-excursion
  7812. (save-match-data
  7813. (run-hook-with-args-until-failure
  7814. 'org-blocker-hook change-plist)))
  7815. (if (interactive-p)
  7816. (error "TODO state change from %s to %s blocked" this state)
  7817. ;; fail silently
  7818. (message "TODO state change from %s to %s blocked" this state)
  7819. (throw 'exit nil))))
  7820. (store-match-data match-data)
  7821. (replace-match next t t)
  7822. (unless (pos-visible-in-window-p hl-pos)
  7823. (message "TODO state changed to %s" (org-trim next)))
  7824. (unless head
  7825. (setq head (org-get-todo-sequence-head state)
  7826. ass (assoc head org-todo-kwd-alist)
  7827. interpret (nth 1 ass)
  7828. done-word (nth 3 ass)
  7829. final-done-word (nth 4 ass)))
  7830. (when (memq arg '(nextset previousset))
  7831. (message "Keyword-Set %d/%d: %s"
  7832. (- (length org-todo-sets) -1
  7833. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7834. (length org-todo-sets)
  7835. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7836. (setq org-last-todo-state-is-todo
  7837. (not (member state org-done-keywords)))
  7838. (setq now-done-p (and (member state org-done-keywords)
  7839. (not (member this org-done-keywords))))
  7840. (and logging (org-local-logging logging))
  7841. (when (and (or org-todo-log-states org-log-done)
  7842. (not (memq arg '(nextset previousset))))
  7843. ;; we need to look at recording a time and note
  7844. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7845. (nth 2 (assoc this org-todo-log-states))))
  7846. (when (and state
  7847. (member state org-not-done-keywords)
  7848. (not (member this org-not-done-keywords)))
  7849. ;; This is now a todo state and was not one before
  7850. ;; If there was a CLOSED time stamp, get rid of it.
  7851. (org-add-planning-info nil nil 'closed))
  7852. (when (and now-done-p org-log-done)
  7853. ;; It is now done, and it was not done before
  7854. (org-add-planning-info 'closed (org-current-time))
  7855. (if (and (not dolog) (eq 'note org-log-done))
  7856. (org-add-log-setup 'done state this 'findpos 'note)))
  7857. (when (and state dolog)
  7858. ;; This is a non-nil state, and we need to log it
  7859. (org-add-log-setup 'state state this 'findpos dolog)))
  7860. ;; Fixup tag positioning
  7861. (org-todo-trigger-tag-changes state)
  7862. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7863. (when org-provide-todo-statistics
  7864. (org-update-parent-todo-statistics))
  7865. (run-hooks 'org-after-todo-state-change-hook)
  7866. (if (and arg (not (member state org-done-keywords)))
  7867. (setq head (org-get-todo-sequence-head state)))
  7868. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7869. ;; Do we need to trigger a repeat?
  7870. (when now-done-p
  7871. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7872. ;; This is for the agenda, take a snapshot of the headline.
  7873. (save-match-data
  7874. (setq org-agenda-headline-snapshot-before-repeat
  7875. (org-get-heading))))
  7876. (org-auto-repeat-maybe state))
  7877. ;; Fixup cursor location if close to the keyword
  7878. (if (and (outline-on-heading-p)
  7879. (not (bolp))
  7880. (save-excursion (beginning-of-line 1)
  7881. (looking-at org-todo-line-regexp))
  7882. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7883. (progn
  7884. (goto-char (or (match-end 2) (match-end 1)))
  7885. (just-one-space)))
  7886. (when org-trigger-hook
  7887. (save-excursion
  7888. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7889. (defun org-block-todo-from-children-or-siblings (change-plist)
  7890. "Block turning an entry into a TODO, using the hierarchy.
  7891. This checks whether the current task should be blocked from state
  7892. changes. Such blocking occurs when:
  7893. 1. The task has children which are not all in a completed state.
  7894. 2. A task has a parent with the property :ORDERED:, and there
  7895. are siblings prior to the current task with incomplete
  7896. status."
  7897. (catch 'dont-block
  7898. ;; If this is not a todo state change, or if this entry is already DONE,
  7899. ;; do not block
  7900. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7901. (member (plist-get change-plist :from)
  7902. (cons 'done org-done-keywords))
  7903. (member (plist-get change-plist :to)
  7904. (cons 'todo org-not-done-keywords)))
  7905. (throw 'dont-block t))
  7906. ;; If this task has children, and any are undone, it's blocked
  7907. (save-excursion
  7908. (org-back-to-heading t)
  7909. (let ((this-level (funcall outline-level)))
  7910. (outline-next-heading)
  7911. (let ((child-level (funcall outline-level)))
  7912. (while (and (not (eobp))
  7913. (> child-level this-level))
  7914. ;; this todo has children, check whether they are all
  7915. ;; completed
  7916. (if (and (not (org-entry-is-done-p))
  7917. (org-entry-is-todo-p))
  7918. (throw 'dont-block nil))
  7919. (outline-next-heading)
  7920. (setq child-level (funcall outline-level))))))
  7921. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7922. ;; any previous siblings are undone, it's blocked
  7923. (save-excursion
  7924. (org-back-to-heading t)
  7925. (when (save-excursion
  7926. (ignore-errors
  7927. (org-up-heading-all 1)
  7928. (org-entry-get (point) "ORDERED")))
  7929. (let* ((this-level (funcall outline-level))
  7930. (current-level this-level))
  7931. (while (and (not (bobp))
  7932. (= current-level this-level))
  7933. (outline-previous-heading)
  7934. (setq current-level (funcall outline-level))
  7935. (if (= current-level this-level)
  7936. ;; this todo has children, check whether they are all
  7937. ;; completed
  7938. (if (and (not (org-entry-is-done-p))
  7939. (org-entry-is-todo-p))
  7940. (throw 'dont-block nil)))))))
  7941. t)) ; don't block
  7942. (defcustom org-track-ordered-property-with-tag nil
  7943. "Should the ORDERED property also be shown as a tag?
  7944. The ORDERED property decides if an entry should require subtasks to be
  7945. completed in sequence. Since a property is not very visible, setting
  7946. this option means that toggling the ORDERED property with the command
  7947. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  7948. not relevant for the behavior, but it makes things more visible.
  7949. Note that toggling the tag with tags commands will not change the property
  7950. and therefore not influence behavior!
  7951. This can be t, meaning the tag ORDERED should be used, It can also be a
  7952. string to select a different tag for this task."
  7953. :group 'org-todo
  7954. :type '(choice
  7955. (const :tag "No tracking" nil)
  7956. (const :tag "Track with ORDERED tag" t)
  7957. (string :tag "Use other tag")))
  7958. (defun org-toggle-ordered-property ()
  7959. "Toggle the ORDERED property of the current entry.
  7960. For better visibility, you can track the value of this property with a tag.
  7961. See variable `org-track-ordered-property-with-tag'."
  7962. (interactive)
  7963. (let* ((t1 org-track-ordered-property-with-tag)
  7964. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  7965. (save-excursion
  7966. (org-back-to-heading)
  7967. (if (org-entry-get nil "ORDERED")
  7968. (progn
  7969. (org-delete-property "ORDERED")
  7970. (and tag (org-toggle-tag tag 'off))
  7971. (message "Subtasks can be completed in arbitrary order"))
  7972. (org-entry-put nil "ORDERED" "t")
  7973. (and tag (org-toggle-tag tag 'on))
  7974. (message "Subtasks must be completed in sequence")))))
  7975. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  7976. (defun org-block-todo-from-checkboxes (change-plist)
  7977. "Block turning an entry into a TODO, using checkboxes.
  7978. This checks whether the current task should be blocked from state
  7979. changes because there are uncheckd boxes in this entry."
  7980. (catch 'dont-block
  7981. ;; If this is not a todo state change, or if this entry is already DONE,
  7982. ;; do not block
  7983. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7984. (member (plist-get change-plist :from)
  7985. (cons 'done org-done-keywords))
  7986. (member (plist-get change-plist :to)
  7987. (cons 'todo org-not-done-keywords)))
  7988. (throw 'dont-block t))
  7989. ;; If this task has checkboxes that are not checked, it's blocked
  7990. (save-excursion
  7991. (org-back-to-heading t)
  7992. (let ((beg (point)) end)
  7993. (outline-next-heading)
  7994. (setq end (point))
  7995. (goto-char beg)
  7996. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7997. end t)
  7998. (progn
  7999. (if (boundp 'org-blocked-by-checkboxes)
  8000. (setq org-blocked-by-checkboxes t))
  8001. (throw 'dont-block nil)))))
  8002. t)) ; do not block
  8003. (defun org-update-parent-todo-statistics ()
  8004. "Update any statistics cookie in the parent of the current headline."
  8005. (interactive)
  8006. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8007. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8008. (catch 'exit
  8009. (save-excursion
  8010. (setq level (org-up-heading-safe))
  8011. (unless level
  8012. (throw 'exit nil))
  8013. (while (re-search-forward box-re (point-at-eol) t)
  8014. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8015. (setq is-percent (match-end 2))
  8016. (save-match-data
  8017. (unless (outline-next-heading) (throw 'exit nil))
  8018. (while (looking-at org-todo-line-regexp)
  8019. (setq kwd (match-string 2))
  8020. (and kwd (setq cnt-all (1+ cnt-all)))
  8021. (and (member kwd org-done-keywords)
  8022. (setq cnt-done (1+ cnt-done)))
  8023. (condition-case nil
  8024. (org-forward-same-level 1)
  8025. (error (end-of-line 1)))))
  8026. (replace-match
  8027. (if is-percent
  8028. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8029. (format "[%d/%d]" cnt-done cnt-all))))
  8030. (when cookie-present
  8031. (run-hook-with-args 'org-after-todo-statistics-hook
  8032. cnt-done (- cnt-all cnt-done)))))))
  8033. (defvar org-after-todo-statistics-hook nil
  8034. "Hook that is called after a TODO statistics cookie has been updated.
  8035. Each function is called with two arguments: the number of not-done entries
  8036. and the number of done entries.
  8037. For example, the following function, when added to this hook, will switch
  8038. an entry to DONE when all children are done, and back to TODO when new
  8039. entries are set to a TODO status. Note that this hook is only called
  8040. when there is a statistics cookie in the headline!
  8041. (defun org-summary-todo (n-done n-not-done)
  8042. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8043. (let (org-log-done org-log-states) ; turn off logging
  8044. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8045. ")
  8046. (defun org-todo-trigger-tag-changes (state)
  8047. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8048. (let ((l org-todo-state-tags-triggers)
  8049. changes)
  8050. (when (or (not state) (equal state ""))
  8051. (setq changes (append changes (cdr (assoc "" l)))))
  8052. (when (and (stringp state) (> (length state) 0))
  8053. (setq changes (append changes (cdr (assoc state l)))))
  8054. (when (member state org-not-done-keywords)
  8055. (setq changes (append changes (cdr (assoc 'todo l)))))
  8056. (when (member state org-done-keywords)
  8057. (setq changes (append changes (cdr (assoc 'done l)))))
  8058. (dolist (c changes)
  8059. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8060. (defun org-local-logging (value)
  8061. "Get logging settings from a property VALUE."
  8062. (let* (words w a)
  8063. ;; directly set the variables, they are already local.
  8064. (setq org-log-done nil
  8065. org-log-repeat nil
  8066. org-todo-log-states nil)
  8067. (setq words (org-split-string value))
  8068. (while (setq w (pop words))
  8069. (cond
  8070. ((setq a (assoc w org-startup-options))
  8071. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8072. (set (nth 1 a) (nth 2 a))))
  8073. ((setq a (org-extract-log-state-settings w))
  8074. (and (member (car a) org-todo-keywords-1)
  8075. (push a org-todo-log-states)))))))
  8076. (defun org-get-todo-sequence-head (kwd)
  8077. "Return the head of the TODO sequence to which KWD belongs.
  8078. If KWD is not set, check if there is a text property remembering the
  8079. right sequence."
  8080. (let (p)
  8081. (cond
  8082. ((not kwd)
  8083. (or (get-text-property (point-at-bol) 'org-todo-head)
  8084. (progn
  8085. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8086. nil (point-at-eol)))
  8087. (get-text-property p 'org-todo-head))))
  8088. ((not (member kwd org-todo-keywords-1))
  8089. (car org-todo-keywords-1))
  8090. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8091. (defun org-fast-todo-selection ()
  8092. "Fast TODO keyword selection with single keys.
  8093. Returns the new TODO keyword, or nil if no state change should occur."
  8094. (let* ((fulltable org-todo-key-alist)
  8095. (done-keywords org-done-keywords) ;; needed for the faces.
  8096. (maxlen (apply 'max (mapcar
  8097. (lambda (x)
  8098. (if (stringp (car x)) (string-width (car x)) 0))
  8099. fulltable)))
  8100. (expert nil)
  8101. (fwidth (+ maxlen 3 1 3))
  8102. (ncol (/ (- (window-width) 4) fwidth))
  8103. tg cnt e c tbl
  8104. groups ingroup)
  8105. (save-excursion
  8106. (save-window-excursion
  8107. (if expert
  8108. (set-buffer (get-buffer-create " *Org todo*"))
  8109. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8110. (erase-buffer)
  8111. (org-set-local 'org-done-keywords done-keywords)
  8112. (setq tbl fulltable cnt 0)
  8113. (while (setq e (pop tbl))
  8114. (cond
  8115. ((equal e '(:startgroup))
  8116. (push '() groups) (setq ingroup t)
  8117. (when (not (= cnt 0))
  8118. (setq cnt 0)
  8119. (insert "\n"))
  8120. (insert "{ "))
  8121. ((equal e '(:endgroup))
  8122. (setq ingroup nil cnt 0)
  8123. (insert "}\n"))
  8124. ((equal e '(:newline))
  8125. (insert "\n "))
  8126. (t
  8127. (setq tg (car e) c (cdr e))
  8128. (if ingroup (push tg (car groups)))
  8129. (setq tg (org-add-props tg nil 'face
  8130. (org-get-todo-face tg)))
  8131. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8132. (insert "[" c "] " tg (make-string
  8133. (- fwidth 4 (length tg)) ?\ ))
  8134. (when (= (setq cnt (1+ cnt)) ncol)
  8135. (insert "\n")
  8136. (if ingroup (insert " "))
  8137. (setq cnt 0)))))
  8138. (insert "\n")
  8139. (goto-char (point-min))
  8140. (if (not expert) (org-fit-window-to-buffer))
  8141. (message "[a-z..]:Set [SPC]:clear")
  8142. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8143. (cond
  8144. ((or (= c ?\C-g)
  8145. (and (= c ?q) (not (rassoc c fulltable))))
  8146. (setq quit-flag t))
  8147. ((= c ?\ ) nil)
  8148. ((setq e (rassoc c fulltable) tg (car e))
  8149. tg)
  8150. (t (setq quit-flag t)))))))
  8151. (defun org-entry-is-todo-p ()
  8152. (member (org-get-todo-state) org-not-done-keywords))
  8153. (defun org-entry-is-done-p ()
  8154. (member (org-get-todo-state) org-done-keywords))
  8155. (defun org-get-todo-state ()
  8156. (save-excursion
  8157. (org-back-to-heading t)
  8158. (and (looking-at org-todo-line-regexp)
  8159. (match-end 2)
  8160. (match-string 2))))
  8161. (defun org-at-date-range-p (&optional inactive-ok)
  8162. "Is the cursor inside a date range?"
  8163. (interactive)
  8164. (save-excursion
  8165. (catch 'exit
  8166. (let ((pos (point)))
  8167. (skip-chars-backward "^[<\r\n")
  8168. (skip-chars-backward "<[")
  8169. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8170. (>= (match-end 0) pos)
  8171. (throw 'exit t))
  8172. (skip-chars-backward "^<[\r\n")
  8173. (skip-chars-backward "<[")
  8174. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8175. (>= (match-end 0) pos)
  8176. (throw 'exit t)))
  8177. nil)))
  8178. (defun org-get-repeat ()
  8179. "Check if there is a deadline/schedule with repeater in this entry."
  8180. (save-match-data
  8181. (save-excursion
  8182. (org-back-to-heading t)
  8183. (if (re-search-forward
  8184. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8185. (match-string 1)))))
  8186. (defvar org-last-changed-timestamp)
  8187. (defvar org-last-inserted-timestamp)
  8188. (defvar org-log-post-message)
  8189. (defvar org-log-note-purpose)
  8190. (defvar org-log-note-how)
  8191. (defvar org-log-note-extra)
  8192. (defun org-auto-repeat-maybe (done-word)
  8193. "Check if the current headline contains a repeated deadline/schedule.
  8194. If yes, set TODO state back to what it was and change the base date
  8195. of repeating deadline/scheduled time stamps to new date.
  8196. This function is run automatically after each state change to a DONE state."
  8197. ;; last-state is dynamically scoped into this function
  8198. (let* ((repeat (org-get-repeat))
  8199. (aa (assoc last-state org-todo-kwd-alist))
  8200. (interpret (nth 1 aa))
  8201. (head (nth 2 aa))
  8202. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8203. (msg "Entry repeats: ")
  8204. (org-log-done nil)
  8205. (org-todo-log-states nil)
  8206. (nshiftmax 10) (nshift 0)
  8207. re type n what ts time)
  8208. (when repeat
  8209. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8210. (org-todo (if (eq interpret 'type) last-state head))
  8211. (when org-log-repeat
  8212. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8213. (memq 'org-add-log-note post-command-hook))
  8214. ;; OK, we are already setup for some record
  8215. (if (eq org-log-repeat 'note)
  8216. ;; make sure we take a note, not only a time stamp
  8217. (setq org-log-note-how 'note))
  8218. ;; Set up for taking a record
  8219. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8220. last-state
  8221. 'findpos org-log-repeat)))
  8222. (org-back-to-heading t)
  8223. (org-add-planning-info nil nil 'closed)
  8224. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8225. org-deadline-time-regexp "\\)\\|\\("
  8226. org-ts-regexp "\\)"))
  8227. (while (re-search-forward
  8228. re (save-excursion (outline-next-heading) (point)) t)
  8229. (setq type (if (match-end 1) org-scheduled-string
  8230. (if (match-end 3) org-deadline-string "Plain:"))
  8231. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8232. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8233. (setq n (string-to-number (match-string 2 ts))
  8234. what (match-string 3 ts))
  8235. (if (equal what "w") (setq n (* n 7) what "d"))
  8236. ;; Preparation, see if we need to modify the start date for the change
  8237. (when (match-end 1)
  8238. (setq time (save-match-data (org-time-string-to-time ts)))
  8239. (cond
  8240. ((equal (match-string 1 ts) ".")
  8241. ;; Shift starting date to today
  8242. (org-timestamp-change
  8243. (- (time-to-days (current-time)) (time-to-days time))
  8244. 'day))
  8245. ((equal (match-string 1 ts) "+")
  8246. (while (or (= nshift 0)
  8247. (<= (time-to-days time) (time-to-days (current-time))))
  8248. (when (= (incf nshift) nshiftmax)
  8249. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8250. (error "Abort")))
  8251. (org-timestamp-change n (cdr (assoc what whata)))
  8252. (org-at-timestamp-p t)
  8253. (setq ts (match-string 1))
  8254. (setq time (save-match-data (org-time-string-to-time ts))))
  8255. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8256. ;; rematch, so that we have everything in place for the real shift
  8257. (org-at-timestamp-p t)
  8258. (setq ts (match-string 1))
  8259. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8260. (org-timestamp-change n (cdr (assoc what whata)))
  8261. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8262. (setq org-log-post-message msg)
  8263. (message "%s" msg))))
  8264. (defun org-show-todo-tree (arg)
  8265. "Make a compact tree which shows all headlines marked with TODO.
  8266. The tree will show the lines where the regexp matches, and all higher
  8267. headlines above the match.
  8268. With a \\[universal-argument] prefix, also show the DONE entries.
  8269. With a numeric prefix N, construct a sparse tree for the Nth element
  8270. of `org-todo-keywords-1'."
  8271. (interactive "P")
  8272. (let ((case-fold-search nil)
  8273. (kwd-re
  8274. (cond ((null arg) org-not-done-regexp)
  8275. ((equal arg '(4))
  8276. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8277. (mapcar 'list org-todo-keywords-1))))
  8278. (concat "\\("
  8279. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8280. "\\)\\>")))
  8281. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8282. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8283. org-todo-keywords-1)))
  8284. (t (error "Invalid prefix argument: %s" arg)))))
  8285. (message "%d TODO entries found"
  8286. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8287. (defun org-deadline (&optional remove time)
  8288. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8289. With argument REMOVE, remove any deadline from the item.
  8290. When TIME is set, it should be an internal time specification, and the
  8291. scheduling will use the corresponding date."
  8292. (interactive "P")
  8293. (if remove
  8294. (progn
  8295. (org-remove-timestamp-with-keyword org-deadline-string)
  8296. (message "Item no longer has a deadline."))
  8297. (if (org-get-repeat)
  8298. (error "Cannot change deadline on task with repeater, please do that by hand")
  8299. (org-add-planning-info 'deadline time 'closed)
  8300. (message "Deadline on %s" org-last-inserted-timestamp))))
  8301. (defun org-schedule (&optional remove time)
  8302. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8303. With argument REMOVE, remove any scheduling date from the item.
  8304. When TIME is set, it should be an internal time specification, and the
  8305. scheduling will use the corresponding date."
  8306. (interactive "P")
  8307. (if remove
  8308. (progn
  8309. (org-remove-timestamp-with-keyword org-scheduled-string)
  8310. (message "Item is no longer scheduled."))
  8311. (if (org-get-repeat)
  8312. (error "Cannot reschedule task with repeater, please do that by hand")
  8313. (org-add-planning-info 'scheduled time 'closed)
  8314. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8315. (defun org-get-scheduled-time (pom &optional inherit)
  8316. "Get the scheduled time as a time tuple, of a format suitable
  8317. for calling org-schedule with, or if there is no scheduling,
  8318. returns nil."
  8319. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8320. (when time
  8321. (apply 'encode-time (org-parse-time-string time)))))
  8322. (defun org-get-deadline-time (pom &optional inherit)
  8323. "Get the deadine as a time tuple, of a format suitable for
  8324. calling org-deadlin with, or if there is no scheduling, returns
  8325. nil."
  8326. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8327. (when time
  8328. (apply 'encode-time (org-parse-time-string time)))))
  8329. (defun org-remove-timestamp-with-keyword (keyword)
  8330. "Remove all time stamps with KEYWORD in the current entry."
  8331. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8332. beg)
  8333. (save-excursion
  8334. (org-back-to-heading t)
  8335. (setq beg (point))
  8336. (org-end-of-subtree t t)
  8337. (while (re-search-backward re beg t)
  8338. (replace-match "")
  8339. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8340. (equal (char-before) ?\ ))
  8341. (backward-delete-char 1)
  8342. (if (string-match "^[ \t]*$" (buffer-substring
  8343. (point-at-bol) (point-at-eol)))
  8344. (delete-region (point-at-bol)
  8345. (min (point-max) (1+ (point-at-eol))))))))))
  8346. (defun org-add-planning-info (what &optional time &rest remove)
  8347. "Insert new timestamp with keyword in the line directly after the headline.
  8348. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8349. If non is given, the user is prompted for a date.
  8350. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8351. be removed."
  8352. (interactive)
  8353. (let (org-time-was-given org-end-time-was-given ts
  8354. end default-time default-input)
  8355. (catch 'exit
  8356. (when (and (not time) (memq what '(scheduled deadline)))
  8357. ;; Try to get a default date/time from existing timestamp
  8358. (save-excursion
  8359. (org-back-to-heading t)
  8360. (setq end (save-excursion (outline-next-heading) (point)))
  8361. (when (re-search-forward (if (eq what 'scheduled)
  8362. org-scheduled-time-regexp
  8363. org-deadline-time-regexp)
  8364. end t)
  8365. (setq ts (match-string 1)
  8366. default-time
  8367. (apply 'encode-time (org-parse-time-string ts))
  8368. default-input (and ts (org-get-compact-tod ts))))))
  8369. (when what
  8370. ;; If necessary, get the time from the user
  8371. (setq time (or time (org-read-date nil 'to-time nil nil
  8372. default-time default-input))))
  8373. (when (and org-insert-labeled-timestamps-at-point
  8374. (member what '(scheduled deadline)))
  8375. (insert
  8376. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8377. (org-insert-time-stamp time org-time-was-given
  8378. nil nil nil (list org-end-time-was-given))
  8379. (setq what nil))
  8380. (save-excursion
  8381. (save-restriction
  8382. (let (col list elt ts buffer-invisibility-spec)
  8383. (org-back-to-heading t)
  8384. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8385. (goto-char (match-end 1))
  8386. (setq col (current-column))
  8387. (goto-char (match-end 0))
  8388. (if (eobp) (insert "\n") (forward-char 1))
  8389. (when (and (not what)
  8390. (not (looking-at org-keyword-time-not-clock-regexp)))
  8391. ;; Nothing to add, nothing to remove...... :-)
  8392. (throw 'exit nil))
  8393. (if (and (not (looking-at outline-regexp))
  8394. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8395. "[^\r\n]*"))
  8396. (not (equal (match-string 1) org-clock-string)))
  8397. (narrow-to-region (match-beginning 0) (match-end 0))
  8398. (insert-before-markers "\n")
  8399. (backward-char 1)
  8400. (narrow-to-region (point) (point))
  8401. (and org-adapt-indentation (org-indent-to-column col)))
  8402. ;; Check if we have to remove something.
  8403. (setq list (cons what remove))
  8404. (while list
  8405. (setq elt (pop list))
  8406. (goto-char (point-min))
  8407. (when (or (and (eq elt 'scheduled)
  8408. (re-search-forward org-scheduled-time-regexp nil t))
  8409. (and (eq elt 'deadline)
  8410. (re-search-forward org-deadline-time-regexp nil t))
  8411. (and (eq elt 'closed)
  8412. (re-search-forward org-closed-time-regexp nil t)))
  8413. (replace-match "")
  8414. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8415. (if (looking-at " +") (replace-match ""))))
  8416. (goto-char (point-max))
  8417. (when what
  8418. (insert
  8419. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8420. (cond ((eq what 'scheduled) org-scheduled-string)
  8421. ((eq what 'deadline) org-deadline-string)
  8422. ((eq what 'closed) org-closed-string))
  8423. " ")
  8424. (setq ts (org-insert-time-stamp
  8425. time
  8426. (or org-time-was-given
  8427. (and (eq what 'closed) org-log-done-with-time))
  8428. (eq what 'closed)
  8429. nil nil (list org-end-time-was-given)))
  8430. (end-of-line 1))
  8431. (goto-char (point-min))
  8432. (widen)
  8433. (if (and (looking-at "[ \t]+\n")
  8434. (equal (char-before) ?\n))
  8435. (delete-region (1- (point)) (point-at-eol)))
  8436. ts))))))
  8437. (defvar org-log-note-marker (make-marker))
  8438. (defvar org-log-note-purpose nil)
  8439. (defvar org-log-note-state nil)
  8440. (defvar org-log-note-previous-state nil)
  8441. (defvar org-log-note-how nil)
  8442. (defvar org-log-note-extra nil)
  8443. (defvar org-log-note-window-configuration nil)
  8444. (defvar org-log-note-return-to (make-marker))
  8445. (defvar org-log-post-message nil
  8446. "Message to be displayed after a log note has been stored.
  8447. The auto-repeater uses this.")
  8448. (defun org-add-note ()
  8449. "Add a note to the current entry.
  8450. This is done in the same way as adding a state change note."
  8451. (interactive)
  8452. (org-add-log-setup 'note nil nil 'findpos nil))
  8453. (defvar org-property-end-re)
  8454. (defun org-add-log-setup (&optional purpose state prev-state
  8455. findpos how &optional extra)
  8456. "Set up the post command hook to take a note.
  8457. If this is about to TODO state change, the new state is expected in STATE.
  8458. When FINDPOS is non-nil, find the correct position for the note in
  8459. the current entry. If not, assume that it can be inserted at point.
  8460. HOW is an indicator what kind of note should be created.
  8461. EXTRA is additional text that will be inserted into the notes buffer."
  8462. (let ((drawer (cond ((stringp org-log-into-drawer)
  8463. org-log-into-drawer)
  8464. (org-log-into-drawer "LOGBOOK")
  8465. (t nil))))
  8466. (save-restriction
  8467. (save-excursion
  8468. (when findpos
  8469. (org-back-to-heading t)
  8470. (narrow-to-region (point) (save-excursion
  8471. (outline-next-heading) (point)))
  8472. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8473. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8474. "[^\r\n]*\\)?"))
  8475. (goto-char (match-end 0))
  8476. (cond
  8477. (drawer
  8478. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8479. nil t)
  8480. (progn
  8481. (goto-char (match-end 0))
  8482. (or org-log-states-order-reversed
  8483. (and (re-search-forward org-property-end-re nil t)
  8484. (goto-char (1- (match-beginning 0))))))
  8485. (insert "\n:" drawer ":\n:END:")
  8486. (beginning-of-line 0)
  8487. (org-indent-line-function)
  8488. (beginning-of-line 2)
  8489. (org-indent-line-function)
  8490. (end-of-line 0)))
  8491. ((and org-log-state-notes-insert-after-drawers
  8492. (save-excursion
  8493. (forward-line) (looking-at org-drawer-regexp)))
  8494. (forward-line)
  8495. (while (looking-at org-drawer-regexp)
  8496. (goto-char (match-end 0))
  8497. (re-search-forward org-property-end-re (point-max) t)
  8498. (forward-line))
  8499. (forward-line -1)))
  8500. (unless org-log-states-order-reversed
  8501. (and (= (char-after) ?\n) (forward-char 1))
  8502. (org-skip-over-state-notes)
  8503. (skip-chars-backward " \t\n\r")))
  8504. (move-marker org-log-note-marker (point))
  8505. (setq org-log-note-purpose purpose
  8506. org-log-note-state state
  8507. org-log-note-previous-state prev-state
  8508. org-log-note-how how
  8509. org-log-note-extra extra)
  8510. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8511. (defun org-skip-over-state-notes ()
  8512. "Skip past the list of State notes in an entry."
  8513. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8514. (while (looking-at "[ \t]*- State")
  8515. (condition-case nil
  8516. (org-next-item)
  8517. (error (org-end-of-item)))))
  8518. (defun org-add-log-note (&optional purpose)
  8519. "Pop up a window for taking a note, and add this note later at point."
  8520. (remove-hook 'post-command-hook 'org-add-log-note)
  8521. (setq org-log-note-window-configuration (current-window-configuration))
  8522. (delete-other-windows)
  8523. (move-marker org-log-note-return-to (point))
  8524. (switch-to-buffer (marker-buffer org-log-note-marker))
  8525. (goto-char org-log-note-marker)
  8526. (org-switch-to-buffer-other-window "*Org Note*")
  8527. (erase-buffer)
  8528. (if (memq org-log-note-how '(time state))
  8529. (let (current-prefix-arg) (org-store-log-note))
  8530. (let ((org-inhibit-startup t)) (org-mode))
  8531. (insert (format "# Insert note for %s.
  8532. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8533. (cond
  8534. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8535. ((eq org-log-note-purpose 'done) "closed todo item")
  8536. ((eq org-log-note-purpose 'state)
  8537. (format "state change from \"%s\" to \"%s\""
  8538. (or org-log-note-previous-state "")
  8539. (or org-log-note-state "")))
  8540. ((eq org-log-note-purpose 'note)
  8541. "this entry")
  8542. (t (error "This should not happen")))))
  8543. (if org-log-note-extra (insert org-log-note-extra))
  8544. (org-set-local 'org-finish-function 'org-store-log-note)))
  8545. (defvar org-note-abort nil) ; dynamically scoped
  8546. (defun org-store-log-note ()
  8547. "Finish taking a log note, and insert it to where it belongs."
  8548. (let ((txt (buffer-string))
  8549. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8550. lines ind)
  8551. (kill-buffer (current-buffer))
  8552. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8553. (setq txt (replace-match "" t t txt)))
  8554. (if (string-match "\\s-+\\'" txt)
  8555. (setq txt (replace-match "" t t txt)))
  8556. (setq lines (org-split-string txt "\n"))
  8557. (when (and note (string-match "\\S-" note))
  8558. (setq note
  8559. (org-replace-escapes
  8560. note
  8561. (list (cons "%u" (user-login-name))
  8562. (cons "%U" user-full-name)
  8563. (cons "%t" (format-time-string
  8564. (org-time-stamp-format 'long 'inactive)
  8565. (current-time)))
  8566. (cons "%s" (if org-log-note-state
  8567. (concat "\"" org-log-note-state "\"")
  8568. ""))
  8569. (cons "%S" (if org-log-note-previous-state
  8570. (concat "\"" org-log-note-previous-state "\"")
  8571. "\"\"")))))
  8572. (if lines (setq note (concat note " \\\\")))
  8573. (push note lines))
  8574. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8575. (when lines
  8576. (save-excursion
  8577. (set-buffer (marker-buffer org-log-note-marker))
  8578. (save-excursion
  8579. (goto-char org-log-note-marker)
  8580. (move-marker org-log-note-marker nil)
  8581. (end-of-line 1)
  8582. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8583. (insert "- " (pop lines))
  8584. (org-indent-line-function)
  8585. (beginning-of-line 1)
  8586. (looking-at "[ \t]*")
  8587. (setq ind (concat (match-string 0) " "))
  8588. (end-of-line 1)
  8589. (while lines (insert "\n" ind (pop lines)))
  8590. (message "Note stored")
  8591. (org-back-to-heading t)
  8592. (org-cycle-hide-drawers 'children)))))
  8593. (set-window-configuration org-log-note-window-configuration)
  8594. (with-current-buffer (marker-buffer org-log-note-return-to)
  8595. (goto-char org-log-note-return-to))
  8596. (move-marker org-log-note-return-to nil)
  8597. (and org-log-post-message (message "%s" org-log-post-message)))
  8598. (defun org-sparse-tree (&optional arg)
  8599. "Create a sparse tree, prompt for the details.
  8600. This command can create sparse trees. You first need to select the type
  8601. of match used to create the tree:
  8602. t Show entries with a specific TODO keyword.
  8603. T Show entries selected by a tags match.
  8604. p Enter a property name and its value (both with completion on existing
  8605. names/values) and show entries with that property.
  8606. r Show entries matching a regular expression
  8607. d Show deadlines due within `org-deadline-warning-days'."
  8608. (interactive "P")
  8609. (let (ans kwd value)
  8610. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8611. (setq ans (read-char-exclusive))
  8612. (cond
  8613. ((equal ans ?d)
  8614. (call-interactively 'org-check-deadlines))
  8615. ((equal ans ?b)
  8616. (call-interactively 'org-check-before-date))
  8617. ((equal ans ?t)
  8618. (org-show-todo-tree '(4)))
  8619. ((equal ans ?T)
  8620. (call-interactively 'org-tags-sparse-tree))
  8621. ((member ans '(?p ?P))
  8622. (setq kwd (org-ido-completing-read "Property: "
  8623. (mapcar 'list (org-buffer-property-keys))))
  8624. (setq value (org-ido-completing-read "Value: "
  8625. (mapcar 'list (org-property-values kwd))))
  8626. (unless (string-match "\\`{.*}\\'" value)
  8627. (setq value (concat "\"" value "\"")))
  8628. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8629. ((member ans '(?r ?R ?/))
  8630. (call-interactively 'org-occur))
  8631. (t (error "No such sparse tree command \"%c\"" ans)))))
  8632. (defvar org-occur-highlights nil
  8633. "List of overlays used for occur matches.")
  8634. (make-variable-buffer-local 'org-occur-highlights)
  8635. (defvar org-occur-parameters nil
  8636. "Parameters of the active org-occur calls.
  8637. This is a list, each call to org-occur pushes as cons cell,
  8638. containing the regular expression and the callback, onto the list.
  8639. The list can contain several entries if `org-occur' has been called
  8640. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8641. will only contain one set of parameters. When the highlights are
  8642. removed (for example with `C-c C-c', or with the next edit (depending
  8643. on `org-remove-highlights-with-change'), this variable is emptied
  8644. as well.")
  8645. (make-variable-buffer-local 'org-occur-parameters)
  8646. (defun org-occur (regexp &optional keep-previous callback)
  8647. "Make a compact tree which shows all matches of REGEXP.
  8648. The tree will show the lines where the regexp matches, and all higher
  8649. headlines above the match. It will also show the heading after the match,
  8650. to make sure editing the matching entry is easy.
  8651. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8652. call to `org-occur' will be kept, to allow stacking of calls to this
  8653. command.
  8654. If CALLBACK is non-nil, it is a function which is called to confirm
  8655. that the match should indeed be shown."
  8656. (interactive "sRegexp: \nP")
  8657. (unless keep-previous
  8658. (org-remove-occur-highlights nil nil t))
  8659. (push (cons regexp callback) org-occur-parameters)
  8660. (let ((cnt 0))
  8661. (save-excursion
  8662. (goto-char (point-min))
  8663. (if (or (not keep-previous) ; do not want to keep
  8664. (not org-occur-highlights)) ; no previous matches
  8665. ;; hide everything
  8666. (org-overview))
  8667. (while (re-search-forward regexp nil t)
  8668. (when (or (not callback)
  8669. (save-match-data (funcall callback)))
  8670. (setq cnt (1+ cnt))
  8671. (when org-highlight-sparse-tree-matches
  8672. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8673. (org-show-context 'occur-tree))))
  8674. (when org-remove-highlights-with-change
  8675. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8676. nil 'local))
  8677. (unless org-sparse-tree-open-archived-trees
  8678. (org-hide-archived-subtrees (point-min) (point-max)))
  8679. (run-hooks 'org-occur-hook)
  8680. (if (interactive-p)
  8681. (message "%d match(es) for regexp %s" cnt regexp))
  8682. cnt))
  8683. (defun org-show-context (&optional key)
  8684. "Make sure point and context and visible.
  8685. How much context is shown depends upon the variables
  8686. `org-show-hierarchy-above', `org-show-following-heading'. and
  8687. `org-show-siblings'."
  8688. (let ((heading-p (org-on-heading-p t))
  8689. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8690. (following-p (org-get-alist-option org-show-following-heading key))
  8691. (entry-p (org-get-alist-option org-show-entry-below key))
  8692. (siblings-p (org-get-alist-option org-show-siblings key)))
  8693. (catch 'exit
  8694. ;; Show heading or entry text
  8695. (if (and heading-p (not entry-p))
  8696. (org-flag-heading nil) ; only show the heading
  8697. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8698. (org-show-hidden-entry))) ; show entire entry
  8699. (when following-p
  8700. ;; Show next sibling, or heading below text
  8701. (save-excursion
  8702. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8703. (org-flag-heading nil))))
  8704. (when siblings-p (org-show-siblings))
  8705. (when hierarchy-p
  8706. ;; show all higher headings, possibly with siblings
  8707. (save-excursion
  8708. (while (and (condition-case nil
  8709. (progn (org-up-heading-all 1) t)
  8710. (error nil))
  8711. (not (bobp)))
  8712. (org-flag-heading nil)
  8713. (when siblings-p (org-show-siblings))))))))
  8714. (defun org-reveal (&optional siblings)
  8715. "Show current entry, hierarchy above it, and the following headline.
  8716. This can be used to show a consistent set of context around locations
  8717. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8718. not t for the search context.
  8719. With optional argument SIBLINGS, on each level of the hierarchy all
  8720. siblings are shown. This repairs the tree structure to what it would
  8721. look like when opened with hierarchical calls to `org-cycle'."
  8722. (interactive "P")
  8723. (let ((org-show-hierarchy-above t)
  8724. (org-show-following-heading t)
  8725. (org-show-siblings (if siblings t org-show-siblings)))
  8726. (org-show-context nil)))
  8727. (defun org-highlight-new-match (beg end)
  8728. "Highlight from BEG to END and mark the highlight is an occur headline."
  8729. (let ((ov (org-make-overlay beg end)))
  8730. (org-overlay-put ov 'face 'secondary-selection)
  8731. (push ov org-occur-highlights)))
  8732. (defun org-remove-occur-highlights (&optional beg end noremove)
  8733. "Remove the occur highlights from the buffer.
  8734. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8735. from the `before-change-functions' in the current buffer."
  8736. (interactive)
  8737. (unless org-inhibit-highlight-removal
  8738. (mapc 'org-delete-overlay org-occur-highlights)
  8739. (setq org-occur-highlights nil)
  8740. (setq org-occur-parameters nil)
  8741. (unless noremove
  8742. (remove-hook 'before-change-functions
  8743. 'org-remove-occur-highlights 'local))))
  8744. ;;;; Priorities
  8745. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8746. "Regular expression matching the priority indicator.")
  8747. (defvar org-remove-priority-next-time nil)
  8748. (defun org-priority-up ()
  8749. "Increase the priority of the current item."
  8750. (interactive)
  8751. (org-priority 'up))
  8752. (defun org-priority-down ()
  8753. "Decrease the priority of the current item."
  8754. (interactive)
  8755. (org-priority 'down))
  8756. (defun org-priority (&optional action)
  8757. "Change the priority of an item by ARG.
  8758. ACTION can be `set', `up', `down', or a character."
  8759. (interactive)
  8760. (setq action (or action 'set))
  8761. (let (current new news have remove)
  8762. (save-excursion
  8763. (org-back-to-heading t)
  8764. (if (looking-at org-priority-regexp)
  8765. (setq current (string-to-char (match-string 2))
  8766. have t)
  8767. (setq current org-default-priority))
  8768. (cond
  8769. ((or (eq action 'set)
  8770. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8771. (if (not (eq action 'set))
  8772. (setq new action)
  8773. (message "Priority %c-%c, SPC to remove: "
  8774. org-highest-priority org-lowest-priority)
  8775. (setq new (read-char-exclusive)))
  8776. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8777. (= (upcase org-lowest-priority) org-lowest-priority))
  8778. (setq new (upcase new)))
  8779. (cond ((equal new ?\ ) (setq remove t))
  8780. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8781. (error "Priority must be between `%c' and `%c'"
  8782. org-highest-priority org-lowest-priority))))
  8783. ((eq action 'up)
  8784. (if (and (not have) (eq last-command this-command))
  8785. (setq new org-lowest-priority)
  8786. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8787. org-default-priority (1- current)))))
  8788. ((eq action 'down)
  8789. (if (and (not have) (eq last-command this-command))
  8790. (setq new org-highest-priority)
  8791. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8792. org-default-priority (1+ current)))))
  8793. (t (error "Invalid action")))
  8794. (if (or (< (upcase new) org-highest-priority)
  8795. (> (upcase new) org-lowest-priority))
  8796. (setq remove t))
  8797. (setq news (format "%c" new))
  8798. (if have
  8799. (if remove
  8800. (replace-match "" t t nil 1)
  8801. (replace-match news t t nil 2))
  8802. (if remove
  8803. (error "No priority cookie found in line")
  8804. (looking-at org-todo-line-regexp)
  8805. (if (match-end 2)
  8806. (progn
  8807. (goto-char (match-end 2))
  8808. (insert " [#" news "]"))
  8809. (goto-char (match-beginning 3))
  8810. (insert "[#" news "] ")))))
  8811. (org-preserve-lc (org-set-tags nil 'align))
  8812. (if remove
  8813. (message "Priority removed")
  8814. (message "Priority of current item set to %s" news))))
  8815. (defun org-get-priority (s)
  8816. "Find priority cookie and return priority."
  8817. (save-match-data
  8818. (if (not (string-match org-priority-regexp s))
  8819. (* 1000 (- org-lowest-priority org-default-priority))
  8820. (* 1000 (- org-lowest-priority
  8821. (string-to-char (match-string 2 s)))))))
  8822. ;;;; Tags
  8823. (defvar org-agenda-archives-mode)
  8824. (defun org-scan-tags (action matcher &optional todo-only)
  8825. "Scan headline tags with inheritance and produce output ACTION.
  8826. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8827. or `agenda' to produce an entry list for an agenda view. It can also be
  8828. a Lisp form or a function that should be called at each matched headline, in
  8829. this case the return value is a list of all return values from these calls.
  8830. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8831. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8832. only lines with a TODO keyword are included in the output."
  8833. (require 'org-agenda)
  8834. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8835. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8836. (org-re
  8837. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8838. (props (list 'face 'default
  8839. 'done-face 'org-done
  8840. 'undone-face 'default
  8841. 'mouse-face 'highlight
  8842. 'org-not-done-regexp org-not-done-regexp
  8843. 'org-todo-regexp org-todo-regexp
  8844. 'keymap org-agenda-keymap
  8845. 'help-echo
  8846. (format "mouse-2 or RET jump to org file %s"
  8847. (abbreviate-file-name
  8848. (or (buffer-file-name (buffer-base-buffer))
  8849. (buffer-name (buffer-base-buffer)))))))
  8850. (case-fold-search nil)
  8851. lspos tags tags-list
  8852. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8853. (llast 0) rtn rtn1 level category i txt
  8854. todo marker entry priority)
  8855. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8856. (setq action (list 'lambda nil action)))
  8857. (save-excursion
  8858. (goto-char (point-min))
  8859. (when (eq action 'sparse-tree)
  8860. (org-overview)
  8861. (org-remove-occur-highlights))
  8862. (while (re-search-forward re nil t)
  8863. (catch :skip
  8864. (setq todo (if (match-end 1) (match-string 2))
  8865. tags (if (match-end 4) (match-string 4)))
  8866. (goto-char (setq lspos (1+ (match-beginning 0))))
  8867. (setq level (org-reduced-level (funcall outline-level))
  8868. category (org-get-category))
  8869. (setq i llast llast level)
  8870. ;; remove tag lists from same and sublevels
  8871. (while (>= i level)
  8872. (when (setq entry (assoc i tags-alist))
  8873. (setq tags-alist (delete entry tags-alist)))
  8874. (setq i (1- i)))
  8875. ;; add the next tags
  8876. (when tags
  8877. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8878. tags-alist
  8879. (cons (cons level tags) tags-alist)))
  8880. ;; compile tags for current headline
  8881. (setq tags-list
  8882. (if org-use-tag-inheritance
  8883. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8884. tags))
  8885. (when org-use-tag-inheritance
  8886. (setcdr (car tags-alist)
  8887. (mapcar (lambda (x)
  8888. (setq x (copy-sequence x))
  8889. (org-add-prop-inherited x))
  8890. (cdar tags-alist))))
  8891. (when (and tags org-use-tag-inheritance
  8892. (not (eq t org-use-tag-inheritance)))
  8893. ;; selective inheritance, remove uninherited ones
  8894. (setcdr (car tags-alist)
  8895. (org-remove-uniherited-tags (cdar tags-alist))))
  8896. (when (and (or (not todo-only)
  8897. (and (member todo org-not-done-keywords)
  8898. (or (not org-agenda-tags-todo-honor-ignore-options)
  8899. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8900. (let ((case-fold-search t)) (eval matcher))
  8901. (or
  8902. (not (member org-archive-tag tags-list))
  8903. ;; we have an archive tag, should we use this anyway?
  8904. (or (not org-agenda-skip-archived-trees)
  8905. (and (eq action 'agenda) org-agenda-archives-mode))))
  8906. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8907. ;; select this headline
  8908. (cond
  8909. ((eq action 'sparse-tree)
  8910. (and org-highlight-sparse-tree-matches
  8911. (org-get-heading) (match-end 0)
  8912. (org-highlight-new-match
  8913. (match-beginning 0) (match-beginning 1)))
  8914. (org-show-context 'tags-tree))
  8915. ((eq action 'agenda)
  8916. (setq txt (org-format-agenda-item
  8917. ""
  8918. (concat
  8919. (if org-tags-match-list-sublevels
  8920. (make-string (1- level) ?.) "")
  8921. (org-get-heading))
  8922. category (org-get-tags-at))
  8923. priority (org-get-priority txt))
  8924. (goto-char lspos)
  8925. (setq marker (org-agenda-new-marker))
  8926. (org-add-props txt props
  8927. 'org-marker marker 'org-hd-marker marker 'org-category category
  8928. 'todo-state todo
  8929. 'priority priority 'type "tagsmatch")
  8930. (push txt rtn))
  8931. ((functionp action)
  8932. (save-excursion
  8933. (setq rtn1 (funcall action))
  8934. (push rtn1 rtn))
  8935. (goto-char (point-at-eol)))
  8936. (t (error "Invalid action")))
  8937. ;; if we are to skip sublevels, jump to end of subtree
  8938. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8939. (when (and (eq action 'sparse-tree)
  8940. (not org-sparse-tree-open-archived-trees))
  8941. (org-hide-archived-subtrees (point-min) (point-max)))
  8942. (nreverse rtn)))
  8943. (defun org-remove-uniherited-tags (tags)
  8944. "Remove all tags that are not inherited from the list TAGS."
  8945. (cond
  8946. ((eq org-use-tag-inheritance t)
  8947. (if org-tags-exclude-from-inheritance
  8948. (org-delete-all org-tags-exclude-from-inheritance tags)
  8949. tags))
  8950. ((not org-use-tag-inheritance) nil)
  8951. ((stringp org-use-tag-inheritance)
  8952. (delq nil (mapcar
  8953. (lambda (x)
  8954. (if (and (string-match org-use-tag-inheritance x)
  8955. (not (member x org-tags-exclude-from-inheritance)))
  8956. x nil))
  8957. tags)))
  8958. ((listp org-use-tag-inheritance)
  8959. (delq nil (mapcar
  8960. (lambda (x)
  8961. (if (member x org-use-tag-inheritance) x nil))
  8962. tags)))))
  8963. (defvar todo-only) ;; dynamically scoped
  8964. (defun org-tags-sparse-tree (&optional todo-only match)
  8965. "Create a sparse tree according to tags string MATCH.
  8966. MATCH can contain positive and negative selection of tags, like
  8967. \"+WORK+URGENT-WITHBOSS\".
  8968. If optional argument TODO-ONLY is non-nil, only select lines that are
  8969. also TODO lines."
  8970. (interactive "P")
  8971. (org-prepare-agenda-buffers (list (current-buffer)))
  8972. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8973. (defvar org-cached-props nil)
  8974. (defun org-cached-entry-get (pom property)
  8975. (if (or (eq t org-use-property-inheritance)
  8976. (and (stringp org-use-property-inheritance)
  8977. (string-match org-use-property-inheritance property))
  8978. (and (listp org-use-property-inheritance)
  8979. (member property org-use-property-inheritance)))
  8980. ;; Caching is not possible, check it directly
  8981. (org-entry-get pom property 'inherit)
  8982. ;; Get all properties, so that we can do complicated checks easily
  8983. (cdr (assoc property (or org-cached-props
  8984. (setq org-cached-props
  8985. (org-entry-properties pom)))))))
  8986. (defun org-global-tags-completion-table (&optional files)
  8987. "Return the list of all tags in all agenda buffer/files."
  8988. (save-excursion
  8989. (org-uniquify
  8990. (delq nil
  8991. (apply 'append
  8992. (mapcar
  8993. (lambda (file)
  8994. (set-buffer (find-file-noselect file))
  8995. (append (org-get-buffer-tags)
  8996. (mapcar (lambda (x) (if (stringp (car-safe x))
  8997. (list (car-safe x)) nil))
  8998. org-tag-alist)))
  8999. (if (and files (car files))
  9000. files
  9001. (org-agenda-files))))))))
  9002. (defun org-make-tags-matcher (match)
  9003. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9004. ;; todo-only is scoped dynamically into this function, and the function
  9005. ;; may change it if the matcher asks for it.
  9006. (unless match
  9007. ;; Get a new match request, with completion
  9008. (let ((org-last-tags-completion-table
  9009. (org-global-tags-completion-table)))
  9010. (setq match (org-completing-read-no-ido
  9011. "Match: " 'org-tags-completion-function nil nil nil
  9012. 'org-tags-history))))
  9013. ;; Parse the string and create a lisp form
  9014. (let ((match0 match)
  9015. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9016. minus tag mm
  9017. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9018. orterms term orlist re-p str-p level-p level-op time-p
  9019. prop-p pn pv po cat-p gv rest)
  9020. (if (string-match "/+" match)
  9021. ;; match contains also a todo-matching request
  9022. (progn
  9023. (setq tagsmatch (substring match 0 (match-beginning 0))
  9024. todomatch (substring match (match-end 0)))
  9025. (if (string-match "^!" todomatch)
  9026. (setq todo-only t todomatch (substring todomatch 1)))
  9027. (if (string-match "^\\s-*$" todomatch)
  9028. (setq todomatch nil)))
  9029. ;; only matching tags
  9030. (setq tagsmatch match todomatch nil))
  9031. ;; Make the tags matcher
  9032. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9033. (setq tagsmatcher t)
  9034. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9035. (while (setq term (pop orterms))
  9036. (while (and (equal (substring term -1) "\\") orterms)
  9037. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9038. (while (string-match re term)
  9039. (setq rest (substring term (match-end 0))
  9040. minus (and (match-end 1)
  9041. (equal (match-string 1 term) "-"))
  9042. tag (match-string 2 term)
  9043. re-p (equal (string-to-char tag) ?{)
  9044. level-p (match-end 4)
  9045. prop-p (match-end 5)
  9046. mm (cond
  9047. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9048. (level-p
  9049. (setq level-op (org-op-to-function (match-string 3 term)))
  9050. `(,level-op level ,(string-to-number
  9051. (match-string 4 term))))
  9052. (prop-p
  9053. (setq pn (match-string 5 term)
  9054. po (match-string 6 term)
  9055. pv (match-string 7 term)
  9056. cat-p (equal pn "CATEGORY")
  9057. re-p (equal (string-to-char pv) ?{)
  9058. str-p (equal (string-to-char pv) ?\")
  9059. time-p (save-match-data
  9060. (string-match "^\"[[<].*[]>]\"$" pv))
  9061. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9062. (if time-p (setq pv (org-matcher-time pv)))
  9063. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9064. (cond
  9065. ((equal pn "CATEGORY")
  9066. (setq gv '(get-text-property (point) 'org-category)))
  9067. ((equal pn "TODO")
  9068. (setq gv 'todo))
  9069. (t
  9070. (setq gv `(org-cached-entry-get nil ,pn))))
  9071. (if re-p
  9072. (if (eq po 'org<>)
  9073. `(not (string-match ,pv (or ,gv "")))
  9074. `(string-match ,pv (or ,gv "")))
  9075. (if str-p
  9076. `(,po (or ,gv "") ,pv)
  9077. `(,po (string-to-number (or ,gv ""))
  9078. ,(string-to-number pv) ))))
  9079. (t `(member ,(downcase tag) tags-list)))
  9080. mm (if minus (list 'not mm) mm)
  9081. term rest)
  9082. (push mm tagsmatcher))
  9083. (push (if (> (length tagsmatcher) 1)
  9084. (cons 'and tagsmatcher)
  9085. (car tagsmatcher))
  9086. orlist)
  9087. (setq tagsmatcher nil))
  9088. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9089. (setq tagsmatcher
  9090. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9091. ;; Make the todo matcher
  9092. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9093. (setq todomatcher t)
  9094. (setq orterms (org-split-string todomatch "|") orlist nil)
  9095. (while (setq term (pop orterms))
  9096. (while (string-match re term)
  9097. (setq minus (and (match-end 1)
  9098. (equal (match-string 1 term) "-"))
  9099. kwd (match-string 2 term)
  9100. re-p (equal (string-to-char kwd) ?{)
  9101. term (substring term (match-end 0))
  9102. mm (if re-p
  9103. `(string-match ,(substring kwd 1 -1) todo)
  9104. (list 'equal 'todo kwd))
  9105. mm (if minus (list 'not mm) mm))
  9106. (push mm todomatcher))
  9107. (push (if (> (length todomatcher) 1)
  9108. (cons 'and todomatcher)
  9109. (car todomatcher))
  9110. orlist)
  9111. (setq todomatcher nil))
  9112. (setq todomatcher (if (> (length orlist) 1)
  9113. (cons 'or orlist) (car orlist))))
  9114. ;; Return the string and lisp forms of the matcher
  9115. (setq matcher (if todomatcher
  9116. (list 'and tagsmatcher todomatcher)
  9117. tagsmatcher))
  9118. (cons match0 matcher)))
  9119. (defun org-op-to-function (op &optional stringp)
  9120. "Turn an operator into the appropriate function."
  9121. (setq op
  9122. (cond
  9123. ((equal op "<" ) '(< string< org-time<))
  9124. ((equal op ">" ) '(> org-string> org-time>))
  9125. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9126. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9127. ((member op '("=" "==")) '(= string= org-time=))
  9128. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9129. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9130. (defun org<> (a b) (not (= a b)))
  9131. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9132. (defun org-string>= (a b) (not (string< a b)))
  9133. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9134. (defun org-string<> (a b) (not (string= a b)))
  9135. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9136. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9137. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9138. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> 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) (org<> a b)))
  9141. (defun org-2ft (s)
  9142. "Convert S to a floating point time.
  9143. If S is already a number, just return it. If it is a string, parse
  9144. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9145. (cond
  9146. ((numberp s) s)
  9147. ((stringp s)
  9148. (condition-case nil
  9149. (float-time (apply 'encode-time (org-parse-time-string s)))
  9150. (error 0.)))
  9151. (t 0.)))
  9152. (defun org-time-today ()
  9153. "Time in seconds today at 0:00.
  9154. Returns the float number of seconds since the beginning of the
  9155. epoch to the beginning of today (00:00)."
  9156. (float-time (apply 'encode-time
  9157. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9158. (defun org-matcher-time (s)
  9159. "Interpret a time comparison value."
  9160. (save-match-data
  9161. (cond
  9162. ((string= s "<now>") (float-time))
  9163. ((string= s "<today>") (org-time-today))
  9164. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9165. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9166. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9167. (+ (org-time-today)
  9168. (* (string-to-number (match-string 1 s))
  9169. (cdr (assoc (match-string 2 s)
  9170. '(("d" . 86400.0) ("w" . 604800.0)
  9171. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9172. (t (org-2ft s)))))
  9173. (defun org-match-any-p (re list)
  9174. "Does re match any element of list?"
  9175. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9176. (delq nil list))
  9177. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9178. (defvar org-tags-overlay (org-make-overlay 1 1))
  9179. (org-detach-overlay org-tags-overlay)
  9180. (defun org-get-local-tags-at (&optional pos)
  9181. "Get a list of tags defined in the current headline."
  9182. (org-get-tags-at pos 'local))
  9183. (defun org-get-local-tags ()
  9184. "Get a list of tags defined in the current headline."
  9185. (org-get-tags-at nil 'local))
  9186. (defun org-get-tags-at (&optional pos local)
  9187. "Get a list of all headline tags applicable at POS.
  9188. POS defaults to point. If tags are inherited, the list contains
  9189. the targets in the same sequence as the headlines appear, i.e.
  9190. the tags of the current headline come last.
  9191. When LOCAL is non-nil, only return tags from the current headline,
  9192. ignore inherited ones."
  9193. (interactive)
  9194. (let (tags ltags lastpos parent)
  9195. (save-excursion
  9196. (save-restriction
  9197. (widen)
  9198. (goto-char (or pos (point)))
  9199. (save-match-data
  9200. (catch 'done
  9201. (condition-case nil
  9202. (progn
  9203. (org-back-to-heading t)
  9204. (while (not (equal lastpos (point)))
  9205. (setq lastpos (point))
  9206. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9207. (setq ltags (org-split-string
  9208. (org-match-string-no-properties 1) ":"))
  9209. (when parent
  9210. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9211. (setq tags (append
  9212. (if parent
  9213. (org-remove-uniherited-tags ltags)
  9214. ltags)
  9215. tags)))
  9216. (or org-use-tag-inheritance (throw 'done t))
  9217. (if local (throw 'done t))
  9218. (org-up-heading-all 1)
  9219. (setq parent t)))
  9220. (error nil)))))
  9221. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9222. (defun org-add-prop-inherited (s)
  9223. (add-text-properties 0 (length s) '(inherited t) s)
  9224. s)
  9225. (defun org-toggle-tag (tag &optional onoff)
  9226. "Toggle the tag TAG for the current line.
  9227. If ONOFF is `on' or `off', don't toggle but set to this state."
  9228. (let (res current)
  9229. (save-excursion
  9230. (org-back-to-heading t)
  9231. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9232. (point-at-eol) t)
  9233. (progn
  9234. (setq current (match-string 1))
  9235. (replace-match ""))
  9236. (setq current ""))
  9237. (setq current (nreverse (org-split-string current ":")))
  9238. (cond
  9239. ((eq onoff 'on)
  9240. (setq res t)
  9241. (or (member tag current) (push tag current)))
  9242. ((eq onoff 'off)
  9243. (or (not (member tag current)) (setq current (delete tag current))))
  9244. (t (if (member tag current)
  9245. (setq current (delete tag current))
  9246. (setq res t)
  9247. (push tag current))))
  9248. (end-of-line 1)
  9249. (if current
  9250. (progn
  9251. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9252. (org-set-tags nil t))
  9253. (delete-horizontal-space))
  9254. (run-hooks 'org-after-tags-change-hook))
  9255. res))
  9256. (defun org-align-tags-here (to-col)
  9257. ;; Assumes that this is a headline
  9258. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9259. (beginning-of-line 1)
  9260. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9261. (< pos (match-beginning 2)))
  9262. (progn
  9263. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9264. (goto-char (match-beginning 1))
  9265. (insert " ")
  9266. (delete-region (point) (1+ (match-beginning 2)))
  9267. (setq ncol (max (1+ (current-column))
  9268. (1+ col)
  9269. (if (> to-col 0)
  9270. to-col
  9271. (- (abs to-col) tags-l))))
  9272. (setq p (point))
  9273. (insert (make-string (- ncol (current-column)) ?\ ))
  9274. (setq ncol (current-column))
  9275. (when indent-tabs-mode (tabify p (point-at-eol)))
  9276. (org-move-to-column (min ncol col) t))
  9277. (goto-char pos))))
  9278. (defun org-set-tags-command (&optional arg just-align)
  9279. "Call the set-tags command for the current entry."
  9280. (interactive "P")
  9281. (if (org-on-heading-p)
  9282. (org-set-tags arg just-align)
  9283. (save-excursion
  9284. (org-back-to-heading t)
  9285. (org-set-tags arg just-align))))
  9286. (defun org-set-tags (&optional arg just-align)
  9287. "Set the tags for the current headline.
  9288. With prefix ARG, realign all tags in headings in the current buffer."
  9289. (interactive "P")
  9290. (let* ((re (concat "^" outline-regexp))
  9291. (current (org-get-tags-string))
  9292. (col (current-column))
  9293. (org-setting-tags t)
  9294. table current-tags inherited-tags ; computed below when needed
  9295. tags p0 c0 c1 rpl)
  9296. (if arg
  9297. (save-excursion
  9298. (goto-char (point-min))
  9299. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9300. (while (re-search-forward re nil t)
  9301. (org-set-tags nil t)
  9302. (end-of-line 1)))
  9303. (message "All tags realigned to column %d" org-tags-column))
  9304. (if just-align
  9305. (setq tags current)
  9306. ;; Get a new set of tags from the user
  9307. (save-excursion
  9308. (setq table (or org-tag-alist (org-get-buffer-tags))
  9309. org-last-tags-completion-table table
  9310. current-tags (org-split-string current ":")
  9311. inherited-tags (nreverse
  9312. (nthcdr (length current-tags)
  9313. (nreverse (org-get-tags-at))))
  9314. tags
  9315. (if (or (eq t org-use-fast-tag-selection)
  9316. (and org-use-fast-tag-selection
  9317. (delq nil (mapcar 'cdr table))))
  9318. (org-fast-tag-selection
  9319. current-tags inherited-tags table
  9320. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9321. (let ((org-add-colon-after-tag-completion t))
  9322. (org-trim
  9323. (org-without-partial-completion
  9324. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9325. nil nil current 'org-tags-history)))))))
  9326. (while (string-match "[-+&]+" tags)
  9327. ;; No boolean logic, just a list
  9328. (setq tags (replace-match ":" t t tags))))
  9329. (if (string-match "\\`[\t ]*\\'" tags)
  9330. (setq tags "")
  9331. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9332. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9333. ;; Insert new tags at the correct column
  9334. (beginning-of-line 1)
  9335. (cond
  9336. ((and (equal current "") (equal tags "")))
  9337. ((re-search-forward
  9338. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9339. (point-at-eol) t)
  9340. (if (equal tags "")
  9341. (setq rpl "")
  9342. (goto-char (match-beginning 0))
  9343. (setq c0 (current-column) p0 (point)
  9344. c1 (max (1+ c0) (if (> org-tags-column 0)
  9345. org-tags-column
  9346. (- (- org-tags-column) (length tags))))
  9347. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9348. (replace-match rpl t t)
  9349. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9350. tags)
  9351. (t (error "Tags alignment failed")))
  9352. (org-move-to-column col)
  9353. (unless just-align
  9354. (run-hooks 'org-after-tags-change-hook)))))
  9355. (defun org-change-tag-in-region (beg end tag off)
  9356. "Add or remove TAG for each entry in the region.
  9357. This works in the agenda, and also in an org-mode buffer."
  9358. (interactive
  9359. (list (region-beginning) (region-end)
  9360. (let ((org-last-tags-completion-table
  9361. (if (org-mode-p)
  9362. (org-get-buffer-tags)
  9363. (org-global-tags-completion-table))))
  9364. (org-ido-completing-read
  9365. "Tag: " 'org-tags-completion-function nil nil nil
  9366. 'org-tags-history))
  9367. (progn
  9368. (message "[s]et or [r]emove? ")
  9369. (equal (read-char-exclusive) ?r))))
  9370. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9371. (let ((agendap (equal major-mode 'org-agenda-mode))
  9372. l1 l2 m buf pos newhead (cnt 0))
  9373. (goto-char end)
  9374. (setq l2 (1- (org-current-line)))
  9375. (goto-char beg)
  9376. (setq l1 (org-current-line))
  9377. (loop for l from l1 to l2 do
  9378. (goto-line l)
  9379. (setq m (get-text-property (point) 'org-hd-marker))
  9380. (when (or (and (org-mode-p) (org-on-heading-p))
  9381. (and agendap m))
  9382. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9383. pos (if agendap m (point)))
  9384. (with-current-buffer buf
  9385. (save-excursion
  9386. (save-restriction
  9387. (goto-char pos)
  9388. (setq cnt (1+ cnt))
  9389. (org-toggle-tag tag (if off 'off 'on))
  9390. (setq newhead (org-get-heading)))))
  9391. (and agendap (org-agenda-change-all-lines newhead m))))
  9392. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9393. (defun org-tags-completion-function (string predicate &optional flag)
  9394. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9395. (confirm (lambda (x) (stringp (car x)))))
  9396. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9397. (setq s1 (match-string 1 string)
  9398. s2 (match-string 2 string))
  9399. (setq s1 "" s2 string))
  9400. (cond
  9401. ((eq flag nil)
  9402. ;; try completion
  9403. (setq rtn (try-completion s2 ctable confirm))
  9404. (if (stringp rtn)
  9405. (setq rtn
  9406. (concat s1 s2 (substring rtn (length s2))
  9407. (if (and org-add-colon-after-tag-completion
  9408. (assoc rtn ctable))
  9409. ":" ""))))
  9410. rtn)
  9411. ((eq flag t)
  9412. ;; all-completions
  9413. (all-completions s2 ctable confirm)
  9414. )
  9415. ((eq flag 'lambda)
  9416. ;; exact match?
  9417. (assoc s2 ctable)))
  9418. ))
  9419. (defun org-fast-tag-insert (kwd tags face &optional end)
  9420. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9421. (insert (format "%-12s" (concat kwd ":"))
  9422. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9423. (or end "")))
  9424. (defun org-fast-tag-show-exit (flag)
  9425. (save-excursion
  9426. (goto-line 3)
  9427. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9428. (replace-match ""))
  9429. (when flag
  9430. (end-of-line 1)
  9431. (org-move-to-column (- (window-width) 19) t)
  9432. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9433. (defun org-set-current-tags-overlay (current prefix)
  9434. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9435. (if (featurep 'xemacs)
  9436. (org-overlay-display org-tags-overlay (concat prefix s)
  9437. 'secondary-selection)
  9438. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9439. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9440. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9441. "Fast tag selection with single keys.
  9442. CURRENT is the current list of tags in the headline, INHERITED is the
  9443. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9444. possibly with grouping information. TODO-TABLE is a similar table with
  9445. TODO keywords, should these have keys assigned to them.
  9446. If the keys are nil, a-z are automatically assigned.
  9447. Returns the new tags string, or nil to not change the current settings."
  9448. (let* ((fulltable (append table todo-table))
  9449. (maxlen (apply 'max (mapcar
  9450. (lambda (x)
  9451. (if (stringp (car x)) (string-width (car x)) 0))
  9452. fulltable)))
  9453. (buf (current-buffer))
  9454. (expert (eq org-fast-tag-selection-single-key 'expert))
  9455. (buffer-tags nil)
  9456. (fwidth (+ maxlen 3 1 3))
  9457. (ncol (/ (- (window-width) 4) fwidth))
  9458. (i-face 'org-done)
  9459. (c-face 'org-todo)
  9460. tg cnt e c char c1 c2 ntable tbl rtn
  9461. ov-start ov-end ov-prefix
  9462. (exit-after-next org-fast-tag-selection-single-key)
  9463. (done-keywords org-done-keywords)
  9464. groups ingroup)
  9465. (save-excursion
  9466. (beginning-of-line 1)
  9467. (if (looking-at
  9468. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9469. (setq ov-start (match-beginning 1)
  9470. ov-end (match-end 1)
  9471. ov-prefix "")
  9472. (setq ov-start (1- (point-at-eol))
  9473. ov-end (1+ ov-start))
  9474. (skip-chars-forward "^\n\r")
  9475. (setq ov-prefix
  9476. (concat
  9477. (buffer-substring (1- (point)) (point))
  9478. (if (> (current-column) org-tags-column)
  9479. " "
  9480. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9481. (org-move-overlay org-tags-overlay ov-start ov-end)
  9482. (save-window-excursion
  9483. (if expert
  9484. (set-buffer (get-buffer-create " *Org tags*"))
  9485. (delete-other-windows)
  9486. (split-window-vertically)
  9487. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9488. (erase-buffer)
  9489. (org-set-local 'org-done-keywords done-keywords)
  9490. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9491. (org-fast-tag-insert "Current" current c-face "\n\n")
  9492. (org-fast-tag-show-exit exit-after-next)
  9493. (org-set-current-tags-overlay current ov-prefix)
  9494. (setq tbl fulltable char ?a cnt 0)
  9495. (while (setq e (pop tbl))
  9496. (cond
  9497. ((equal e '(:startgroup))
  9498. (push '() groups) (setq ingroup t)
  9499. (when (not (= cnt 0))
  9500. (setq cnt 0)
  9501. (insert "\n"))
  9502. (insert "{ "))
  9503. ((equal e '(:endgroup))
  9504. (setq ingroup nil cnt 0)
  9505. (insert "}\n"))
  9506. ((equal e '(:newline))
  9507. (insert "\n "))
  9508. (t
  9509. (setq tg (car e) c2 nil)
  9510. (if (cdr e)
  9511. (setq c (cdr e))
  9512. ;; automatically assign a character.
  9513. (setq c1 (string-to-char
  9514. (downcase (substring
  9515. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9516. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9517. (while (or (rassoc char ntable) (rassoc char table))
  9518. (setq char (1+ char)))
  9519. (setq c2 c1))
  9520. (setq c (or c2 char)))
  9521. (if ingroup (push tg (car groups)))
  9522. (setq tg (org-add-props tg nil 'face
  9523. (cond
  9524. ((not (assoc tg table))
  9525. (org-get-todo-face tg))
  9526. ((member tg current) c-face)
  9527. ((member tg inherited) i-face)
  9528. (t nil))))
  9529. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9530. (insert "[" c "] " tg (make-string
  9531. (- fwidth 4 (length tg)) ?\ ))
  9532. (push (cons tg c) ntable)
  9533. (when (= (setq cnt (1+ cnt)) ncol)
  9534. (insert "\n")
  9535. (if ingroup (insert " "))
  9536. (setq cnt 0)))))
  9537. (setq ntable (nreverse ntable))
  9538. (insert "\n")
  9539. (goto-char (point-min))
  9540. (if (not expert) (org-fit-window-to-buffer))
  9541. (setq rtn
  9542. (catch 'exit
  9543. (while t
  9544. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9545. (if groups " [!] no groups" " [!]groups")
  9546. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9547. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9548. (cond
  9549. ((= c ?\r) (throw 'exit t))
  9550. ((= c ?!)
  9551. (setq groups (not groups))
  9552. (goto-char (point-min))
  9553. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9554. ((= c ?\C-c)
  9555. (if (not expert)
  9556. (org-fast-tag-show-exit
  9557. (setq exit-after-next (not exit-after-next)))
  9558. (setq expert nil)
  9559. (delete-other-windows)
  9560. (split-window-vertically)
  9561. (org-switch-to-buffer-other-window " *Org tags*")
  9562. (org-fit-window-to-buffer)))
  9563. ((or (= c ?\C-g)
  9564. (and (= c ?q) (not (rassoc c ntable))))
  9565. (org-detach-overlay org-tags-overlay)
  9566. (setq quit-flag t))
  9567. ((= c ?\ )
  9568. (setq current nil)
  9569. (if exit-after-next (setq exit-after-next 'now)))
  9570. ((= c ?\t)
  9571. (condition-case nil
  9572. (setq tg (org-ido-completing-read
  9573. "Tag: "
  9574. (or buffer-tags
  9575. (with-current-buffer buf
  9576. (org-get-buffer-tags)))))
  9577. (quit (setq tg "")))
  9578. (when (string-match "\\S-" tg)
  9579. (add-to-list 'buffer-tags (list tg))
  9580. (if (member tg current)
  9581. (setq current (delete tg current))
  9582. (push tg current)))
  9583. (if exit-after-next (setq exit-after-next 'now)))
  9584. ((setq e (rassoc c todo-table) tg (car e))
  9585. (with-current-buffer buf
  9586. (save-excursion (org-todo tg)))
  9587. (if exit-after-next (setq exit-after-next 'now)))
  9588. ((setq e (rassoc c ntable) tg (car e))
  9589. (if (member tg current)
  9590. (setq current (delete tg current))
  9591. (loop for g in groups do
  9592. (if (member tg g)
  9593. (mapc (lambda (x)
  9594. (setq current (delete x current)))
  9595. g)))
  9596. (push tg current))
  9597. (if exit-after-next (setq exit-after-next 'now))))
  9598. ;; Create a sorted list
  9599. (setq current
  9600. (sort current
  9601. (lambda (a b)
  9602. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9603. (if (eq exit-after-next 'now) (throw 'exit t))
  9604. (goto-char (point-min))
  9605. (beginning-of-line 2)
  9606. (delete-region (point) (point-at-eol))
  9607. (org-fast-tag-insert "Current" current c-face)
  9608. (org-set-current-tags-overlay current ov-prefix)
  9609. (while (re-search-forward
  9610. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9611. (setq tg (match-string 1))
  9612. (add-text-properties
  9613. (match-beginning 1) (match-end 1)
  9614. (list 'face
  9615. (cond
  9616. ((member tg current) c-face)
  9617. ((member tg inherited) i-face)
  9618. (t (get-text-property (match-beginning 1) 'face))))))
  9619. (goto-char (point-min)))))
  9620. (org-detach-overlay org-tags-overlay)
  9621. (if rtn
  9622. (mapconcat 'identity current ":")
  9623. nil))))
  9624. (defun org-get-tags-string ()
  9625. "Get the TAGS string in the current headline."
  9626. (unless (org-on-heading-p t)
  9627. (error "Not on a heading"))
  9628. (save-excursion
  9629. (beginning-of-line 1)
  9630. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9631. (org-match-string-no-properties 1)
  9632. "")))
  9633. (defun org-get-tags ()
  9634. "Get the list of tags specified in the current headline."
  9635. (org-split-string (org-get-tags-string) ":"))
  9636. (defun org-get-buffer-tags ()
  9637. "Get a table of all tags used in the buffer, for completion."
  9638. (let (tags)
  9639. (save-excursion
  9640. (goto-char (point-min))
  9641. (while (re-search-forward
  9642. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9643. (when (equal (char-after (point-at-bol 0)) ?*)
  9644. (mapc (lambda (x) (add-to-list 'tags x))
  9645. (org-split-string (org-match-string-no-properties 1) ":")))))
  9646. (mapcar 'list tags)))
  9647. ;;;; The mapping API
  9648. ;;;###autoload
  9649. (defun org-map-entries (func &optional match scope &rest skip)
  9650. "Call FUNC at each headline selected by MATCH in SCOPE.
  9651. FUNC is a function or a lisp form. The function will be called without
  9652. arguments, with the cursor positioned at the beginning of the headline.
  9653. The return values of all calls to the function will be collected and
  9654. returned as a list.
  9655. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9656. Only headlines that are matched by this query will be considered during
  9657. the iteration. When MATCH is nil or t, all headlines will be
  9658. visited by the iteration.
  9659. SCOPE determines the scope of this command. It can be any of:
  9660. nil The current buffer, respecting the restriction if any
  9661. tree The subtree started with the entry at point
  9662. file The current buffer, without restriction
  9663. file-with-archives
  9664. The current buffer, and any archives associated with it
  9665. agenda All agenda files
  9666. agenda-with-archives
  9667. All agenda files with any archive files associated with them
  9668. \(file1 file2 ...)
  9669. If this is a list, all files in the list will be scanned
  9670. The remaining args are treated as settings for the skipping facilities of
  9671. the scanner. The following items can be given here:
  9672. archive skip trees with the archive tag.
  9673. comment skip trees with the COMMENT keyword
  9674. function or Emacs Lisp form:
  9675. will be used as value for `org-agenda-skip-function', so whenever
  9676. the the function returns t, FUNC will not be called for that
  9677. entry and search will continue from the point where the
  9678. function leaves it."
  9679. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9680. (org-agenda-skip-archived-trees (memq 'archive skip))
  9681. (org-agenda-skip-comment-trees (memq 'comment skip))
  9682. (org-agenda-skip-function
  9683. (car (org-delete-all '(comment archive) skip)))
  9684. (org-tags-match-list-sublevels t)
  9685. matcher file res
  9686. org-todo-keywords-for-agenda
  9687. org-done-keywords-for-agenda
  9688. org-todo-keyword-alist-for-agenda
  9689. org-tag-alist-for-agenda)
  9690. (cond
  9691. ((eq match t) (setq matcher t))
  9692. ((eq match nil) (setq matcher t))
  9693. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9694. (save-excursion
  9695. (save-restriction
  9696. (when (eq scope 'tree)
  9697. (org-back-to-heading t)
  9698. (org-narrow-to-subtree)
  9699. (setq scope nil))
  9700. (if (not scope)
  9701. (progn
  9702. (org-prepare-agenda-buffers
  9703. (list (buffer-file-name (current-buffer))))
  9704. (setq res (org-scan-tags func matcher)))
  9705. ;; Get the right scope
  9706. (cond
  9707. ((and scope (listp scope) (symbolp (car scope)))
  9708. (setq scope (eval scope)))
  9709. ((eq scope 'agenda)
  9710. (setq scope (org-agenda-files t)))
  9711. ((eq scope 'agenda-with-archives)
  9712. (setq scope (org-agenda-files t))
  9713. (setq scope (org-add-archive-files scope)))
  9714. ((eq scope 'file)
  9715. (setq scope (list (buffer-file-name))))
  9716. ((eq scope 'file-with-archives)
  9717. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9718. (org-prepare-agenda-buffers scope)
  9719. (while (setq file (pop scope))
  9720. (with-current-buffer (org-find-base-buffer-visiting file)
  9721. (save-excursion
  9722. (save-restriction
  9723. (widen)
  9724. (goto-char (point-min))
  9725. (setq res (append res (org-scan-tags func matcher))))))))))
  9726. res))
  9727. ;;;; Properties
  9728. ;;; Setting and retrieving properties
  9729. (defconst org-special-properties
  9730. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9731. "TIMESTAMP" "TIMESTAMP_IA")
  9732. "The special properties valid in Org-mode.
  9733. These are properties that are not defined in the property drawer,
  9734. but in some other way.")
  9735. (defconst org-default-properties
  9736. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9737. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9738. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9739. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9740. "Some properties that are used by Org-mode for various purposes.
  9741. Being in this list makes sure that they are offered for completion.")
  9742. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9743. "Regular expression matching the first line of a property drawer.")
  9744. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9745. "Regular expression matching the first line of a property drawer.")
  9746. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9747. "Regular expression matching the first line of a property drawer.")
  9748. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9749. "Regular expression matching the first line of a property drawer.")
  9750. (defconst org-property-drawer-re
  9751. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9752. org-property-end-re "\\)\n?")
  9753. "Matches an entire property drawer.")
  9754. (defconst org-clock-drawer-re
  9755. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9756. org-property-end-re "\\)\n?")
  9757. "Matches an entire clock drawer.")
  9758. (defun org-property-action ()
  9759. "Do an action on properties."
  9760. (interactive)
  9761. (let (c)
  9762. (org-at-property-p)
  9763. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9764. (setq c (read-char-exclusive))
  9765. (cond
  9766. ((equal c ?s)
  9767. (call-interactively 'org-set-property))
  9768. ((equal c ?d)
  9769. (call-interactively 'org-delete-property))
  9770. ((equal c ?D)
  9771. (call-interactively 'org-delete-property-globally))
  9772. ((equal c ?c)
  9773. (call-interactively 'org-compute-property-at-point))
  9774. (t (error "No such property action %c" c)))))
  9775. (defun org-at-property-p ()
  9776. "Is the cursor in a property line?"
  9777. ;; FIXME: Does not check if we are actually in the drawer.
  9778. ;; FIXME: also returns true on any drawers.....
  9779. ;; This is used by C-c C-c for property action.
  9780. (save-excursion
  9781. (beginning-of-line 1)
  9782. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9783. (defun org-get-property-block (&optional beg end force)
  9784. "Return the (beg . end) range of the body of the property drawer.
  9785. BEG and END can be beginning and end of subtree, if not given
  9786. they will be found.
  9787. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9788. (catch 'exit
  9789. (save-excursion
  9790. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9791. (end (or end (progn (outline-next-heading) (point)))))
  9792. (goto-char beg)
  9793. (if (re-search-forward org-property-start-re end t)
  9794. (setq beg (1+ (match-end 0)))
  9795. (if force
  9796. (save-excursion
  9797. (org-insert-property-drawer)
  9798. (setq end (progn (outline-next-heading) (point))))
  9799. (throw 'exit nil))
  9800. (goto-char beg)
  9801. (if (re-search-forward org-property-start-re end t)
  9802. (setq beg (1+ (match-end 0)))))
  9803. (if (re-search-forward org-property-end-re end t)
  9804. (setq end (match-beginning 0))
  9805. (or force (throw 'exit nil))
  9806. (goto-char beg)
  9807. (setq end beg)
  9808. (org-indent-line-function)
  9809. (insert ":END:\n"))
  9810. (cons beg end)))))
  9811. (defun org-entry-properties (&optional pom which)
  9812. "Get all properties of the entry at point-or-marker POM.
  9813. This includes the TODO keyword, the tags, time strings for deadline,
  9814. scheduled, and clocking, and any additional properties defined in the
  9815. entry. The return value is an alist, keys may occur multiple times
  9816. if the property key was used several times.
  9817. POM may also be nil, in which case the current entry is used.
  9818. If WHICH is nil or `all', get all properties. If WHICH is
  9819. `special' or `standard', only get that subclass."
  9820. (setq which (or which 'all))
  9821. (org-with-point-at pom
  9822. (let ((clockstr (substring org-clock-string 0 -1))
  9823. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9824. beg end range props sum-props key value string clocksum)
  9825. (save-excursion
  9826. (when (condition-case nil
  9827. (and (org-mode-p) (org-back-to-heading t))
  9828. (error nil))
  9829. (setq beg (point))
  9830. (setq sum-props (get-text-property (point) 'org-summaries))
  9831. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9832. (outline-next-heading)
  9833. (setq end (point))
  9834. (when (memq which '(all special))
  9835. ;; Get the special properties, like TODO and tags
  9836. (goto-char beg)
  9837. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9838. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9839. (when (looking-at org-priority-regexp)
  9840. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9841. (when (and (setq value (org-get-tags-string))
  9842. (string-match "\\S-" value))
  9843. (push (cons "TAGS" value) props))
  9844. (when (setq value (org-get-tags-at))
  9845. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9846. props))
  9847. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9848. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9849. string (if (equal key clockstr)
  9850. (org-no-properties
  9851. (org-trim
  9852. (buffer-substring
  9853. (match-beginning 3) (goto-char (point-at-eol)))))
  9854. (substring (org-match-string-no-properties 3) 1 -1)))
  9855. (unless key
  9856. (if (= (char-after (match-beginning 3)) ?\[)
  9857. (setq key "TIMESTAMP_IA")
  9858. (setq key "TIMESTAMP")))
  9859. (when (or (equal key clockstr) (not (assoc key props)))
  9860. (push (cons key string) props)))
  9861. )
  9862. (when (memq which '(all standard))
  9863. ;; Get the standard properties, like :PORP: ...
  9864. (setq range (org-get-property-block beg end))
  9865. (when range
  9866. (goto-char (car range))
  9867. (while (re-search-forward
  9868. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9869. (cdr range) t)
  9870. (setq key (org-match-string-no-properties 1)
  9871. value (org-trim (or (org-match-string-no-properties 2) "")))
  9872. (unless (member key excluded)
  9873. (push (cons key (or value "")) props)))))
  9874. (if clocksum
  9875. (push (cons "CLOCKSUM"
  9876. (org-columns-number-to-string (/ (float clocksum) 60.)
  9877. 'add_times))
  9878. props))
  9879. (unless (assoc "CATEGORY" props)
  9880. (setq value (or (org-get-category)
  9881. (progn (org-refresh-category-properties)
  9882. (org-get-category))))
  9883. (push (cons "CATEGORY" value) props))
  9884. (append sum-props (nreverse props)))))))
  9885. (defun org-entry-get (pom property &optional inherit)
  9886. "Get value of PROPERTY for entry at point-or-marker POM.
  9887. If INHERIT is non-nil and the entry does not have the property,
  9888. then also check higher levels of the hierarchy.
  9889. If INHERIT is the symbol `selective', use inheritance only if the setting
  9890. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9891. If the property is present but empty, the return value is the empty string.
  9892. If the property is not present at all, nil is returned."
  9893. (org-with-point-at pom
  9894. (if (and inherit (if (eq inherit 'selective)
  9895. (org-property-inherit-p property)
  9896. t))
  9897. (org-entry-get-with-inheritance property)
  9898. (if (member property org-special-properties)
  9899. ;; We need a special property. Use brute force, get all properties.
  9900. (cdr (assoc property (org-entry-properties nil 'special)))
  9901. (let ((range (org-get-property-block)))
  9902. (if (and range
  9903. (goto-char (car range))
  9904. (re-search-forward
  9905. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9906. (cdr range) t))
  9907. ;; Found the property, return it.
  9908. (if (match-end 1)
  9909. (org-match-string-no-properties 1)
  9910. "")))))))
  9911. (defun org-property-or-variable-value (var &optional inherit)
  9912. "Check if there is a property fixing the value of VAR.
  9913. If yes, return this value. If not, return the current value of the variable."
  9914. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9915. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9916. (read prop)
  9917. (symbol-value var))))
  9918. (defun org-entry-delete (pom property)
  9919. "Delete the property PROPERTY from entry at point-or-marker POM."
  9920. (org-with-point-at pom
  9921. (if (member property org-special-properties)
  9922. nil ; cannot delete these properties.
  9923. (let ((range (org-get-property-block)))
  9924. (if (and range
  9925. (goto-char (car range))
  9926. (re-search-forward
  9927. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9928. (cdr range) t))
  9929. (progn
  9930. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9931. t)
  9932. nil)))))
  9933. ;; Multi-values properties are properties that contain multiple values
  9934. ;; These values are assumed to be single words, separated by whitespace.
  9935. (defun org-entry-add-to-multivalued-property (pom property value)
  9936. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9937. (let* ((old (org-entry-get pom property))
  9938. (values (and old (org-split-string old "[ \t]"))))
  9939. (setq value (org-entry-protect-space value))
  9940. (unless (member value values)
  9941. (setq values (cons value values))
  9942. (org-entry-put pom property
  9943. (mapconcat 'identity values " ")))))
  9944. (defun org-entry-remove-from-multivalued-property (pom property value)
  9945. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9946. (let* ((old (org-entry-get pom property))
  9947. (values (and old (org-split-string old "[ \t]"))))
  9948. (setq value (org-entry-protect-space value))
  9949. (when (member value values)
  9950. (setq values (delete value values))
  9951. (org-entry-put pom property
  9952. (mapconcat 'identity values " ")))))
  9953. (defun org-entry-member-in-multivalued-property (pom property value)
  9954. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9955. (let* ((old (org-entry-get pom property))
  9956. (values (and old (org-split-string old "[ \t]"))))
  9957. (setq value (org-entry-protect-space value))
  9958. (member value values)))
  9959. (defun org-entry-get-multivalued-property (pom property)
  9960. "Return a list of values in a multivalued property."
  9961. (let* ((value (org-entry-get pom property))
  9962. (values (and value (org-split-string value "[ \t]"))))
  9963. (mapcar 'org-entry-restore-space values)))
  9964. (defun org-entry-put-multivalued-property (pom property &rest values)
  9965. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9966. VALUES should be a list of strings. Spaces will be protected."
  9967. (org-entry-put pom property
  9968. (mapconcat 'org-entry-protect-space values " "))
  9969. (let* ((value (org-entry-get pom property))
  9970. (values (and value (org-split-string value "[ \t]"))))
  9971. (mapcar 'org-entry-restore-space values)))
  9972. (defun org-entry-protect-space (s)
  9973. "Protect spaces and newline in string S."
  9974. (while (string-match " " s)
  9975. (setq s (replace-match "%20" t t s)))
  9976. (while (string-match "\n" s)
  9977. (setq s (replace-match "%0A" t t s)))
  9978. s)
  9979. (defun org-entry-restore-space (s)
  9980. "Restore spaces and newline in string S."
  9981. (while (string-match "%20" s)
  9982. (setq s (replace-match " " t t s)))
  9983. (while (string-match "%0A" s)
  9984. (setq s (replace-match "\n" t t s)))
  9985. s)
  9986. (defvar org-entry-property-inherited-from (make-marker)
  9987. "Marker pointing to the entry from where a property was inherited.
  9988. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9989. location of the entry where the inheritance search matched. If there was
  9990. no match, the marker will point nowhere.
  9991. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9992. is set.")
  9993. (defun org-entry-get-with-inheritance (property)
  9994. "Get entry property, and search higher levels if not present."
  9995. (move-marker org-entry-property-inherited-from nil)
  9996. (let (tmp)
  9997. (save-excursion
  9998. (save-restriction
  9999. (widen)
  10000. (catch 'ex
  10001. (while t
  10002. (when (setq tmp (org-entry-get nil property))
  10003. (org-back-to-heading t)
  10004. (move-marker org-entry-property-inherited-from (point))
  10005. (throw 'ex tmp))
  10006. (or (org-up-heading-safe) (throw 'ex nil)))))
  10007. (or tmp
  10008. (cdr (assoc property org-file-properties))
  10009. (cdr (assoc property org-global-properties))
  10010. (cdr (assoc property org-global-properties-fixed))))))
  10011. (defun org-entry-put (pom property value)
  10012. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10013. (org-with-point-at pom
  10014. (org-back-to-heading t)
  10015. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10016. range)
  10017. (cond
  10018. ((equal property "TODO")
  10019. (when (and (stringp value) (string-match "\\S-" value)
  10020. (not (member value org-todo-keywords-1)))
  10021. (error "\"%s\" is not a valid TODO state" value))
  10022. (if (or (not value)
  10023. (not (string-match "\\S-" value)))
  10024. (setq value 'none))
  10025. (org-todo value)
  10026. (org-set-tags nil 'align))
  10027. ((equal property "PRIORITY")
  10028. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10029. (string-to-char value) ?\ ))
  10030. (org-set-tags nil 'align))
  10031. ((equal property "SCHEDULED")
  10032. (if (re-search-forward org-scheduled-time-regexp end t)
  10033. (cond
  10034. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10035. ((eq value 'later) (org-timestamp-change 1 'day))
  10036. (t (call-interactively 'org-schedule)))
  10037. (call-interactively 'org-schedule)))
  10038. ((equal property "DEADLINE")
  10039. (if (re-search-forward org-deadline-time-regexp end t)
  10040. (cond
  10041. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10042. ((eq value 'later) (org-timestamp-change 1 'day))
  10043. (t (call-interactively 'org-deadline)))
  10044. (call-interactively 'org-deadline)))
  10045. ((member property org-special-properties)
  10046. (error "The %s property can not yet be set with `org-entry-put'"
  10047. property))
  10048. (t ; a non-special property
  10049. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10050. (setq range (org-get-property-block beg end 'force))
  10051. (goto-char (car range))
  10052. (if (re-search-forward
  10053. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10054. (progn
  10055. (delete-region (match-beginning 1) (match-end 1))
  10056. (goto-char (match-beginning 1)))
  10057. (goto-char (cdr range))
  10058. (insert "\n")
  10059. (backward-char 1)
  10060. (org-indent-line-function)
  10061. (insert ":" property ":"))
  10062. (and value (insert " " value))
  10063. (org-indent-line-function)))))))
  10064. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10065. "Get all property keys in the current buffer.
  10066. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10067. like tags and TODO state.
  10068. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10069. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10070. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10071. formats in the current buffer."
  10072. (let (rtn range cfmt s p)
  10073. (save-excursion
  10074. (save-restriction
  10075. (widen)
  10076. (goto-char (point-min))
  10077. (while (re-search-forward org-property-start-re nil t)
  10078. (setq range (org-get-property-block))
  10079. (goto-char (car range))
  10080. (while (re-search-forward
  10081. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10082. (cdr range) t)
  10083. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10084. (outline-next-heading))))
  10085. (when include-specials
  10086. (setq rtn (append org-special-properties rtn)))
  10087. (when include-defaults
  10088. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10089. (when include-columns
  10090. (save-excursion
  10091. (save-restriction
  10092. (widen)
  10093. (goto-char (point-min))
  10094. (while (re-search-forward
  10095. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10096. nil t)
  10097. (setq cfmt (match-string 2) s 0)
  10098. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10099. cfmt s)
  10100. (setq s (match-end 0)
  10101. p (match-string 1 cfmt))
  10102. (unless (or (equal p "ITEM")
  10103. (member p org-special-properties))
  10104. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10105. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10106. (defun org-property-values (key)
  10107. "Return a list of all values of property KEY."
  10108. (save-excursion
  10109. (save-restriction
  10110. (widen)
  10111. (goto-char (point-min))
  10112. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10113. values)
  10114. (while (re-search-forward re nil t)
  10115. (add-to-list 'values (org-trim (match-string 1))))
  10116. (delete "" values)))))
  10117. (defun org-insert-property-drawer ()
  10118. "Insert a property drawer into the current entry."
  10119. (interactive)
  10120. (org-back-to-heading t)
  10121. (looking-at outline-regexp)
  10122. (let ((indent (- (match-end 0)(match-beginning 0)))
  10123. (beg (point))
  10124. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10125. end hiddenp)
  10126. (outline-next-heading)
  10127. (setq end (point))
  10128. (goto-char beg)
  10129. (while (re-search-forward re end t))
  10130. (setq hiddenp (org-invisible-p))
  10131. (end-of-line 1)
  10132. (and (equal (char-after) ?\n) (forward-char 1))
  10133. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10134. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10135. ;; just skip this line
  10136. (beginning-of-line 2)
  10137. ;; Drawer start, find the end
  10138. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10139. (beginning-of-line 1)))
  10140. (org-skip-over-state-notes)
  10141. (skip-chars-backward " \t\n\r")
  10142. (if (eq (char-before) ?*) (forward-char 1))
  10143. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10144. (beginning-of-line 0)
  10145. (org-indent-to-column indent)
  10146. (beginning-of-line 2)
  10147. (org-indent-to-column indent)
  10148. (beginning-of-line 0)
  10149. (if hiddenp
  10150. (save-excursion
  10151. (org-back-to-heading t)
  10152. (hide-entry))
  10153. (org-flag-drawer t))))
  10154. (defun org-set-property (property value)
  10155. "In the current entry, set PROPERTY to VALUE.
  10156. When called interactively, this will prompt for a property name, offering
  10157. completion on existing and default properties. And then it will prompt
  10158. for a value, offering completion either on allowed values (via an inherited
  10159. xxx_ALL property) or on existing values in other instances of this property
  10160. in the current file."
  10161. (interactive
  10162. (let* ((completion-ignore-case t)
  10163. (keys (org-buffer-property-keys nil t t))
  10164. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10165. (prop (if (member prop0 keys)
  10166. prop0
  10167. (or (cdr (assoc (downcase prop0)
  10168. (mapcar (lambda (x) (cons (downcase x) x))
  10169. keys)))
  10170. prop0)))
  10171. (cur (org-entry-get nil prop))
  10172. (allowed (org-property-get-allowed-values nil prop 'table))
  10173. (existing (mapcar 'list (org-property-values prop)))
  10174. (val (if allowed
  10175. (org-completing-read "Value: " allowed nil 'req-match)
  10176. (let (org-completion-use-ido)
  10177. (org-completing-read
  10178. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10179. (concat "[" cur "]") "")
  10180. ": ")
  10181. existing nil nil "" nil cur)))))
  10182. (list prop (if (equal val "") cur val))))
  10183. (unless (equal (org-entry-get nil property) value)
  10184. (org-entry-put nil property value)))
  10185. (defun org-delete-property (property)
  10186. "In the current entry, delete PROPERTY."
  10187. (interactive
  10188. (let* ((completion-ignore-case t)
  10189. (prop (org-ido-completing-read
  10190. "Property: " (org-entry-properties nil 'standard))))
  10191. (list prop)))
  10192. (message "Property %s %s" property
  10193. (if (org-entry-delete nil property)
  10194. "deleted"
  10195. "was not present in the entry")))
  10196. (defun org-delete-property-globally (property)
  10197. "Remove PROPERTY globally, from all entries."
  10198. (interactive
  10199. (let* ((completion-ignore-case t)
  10200. (prop (org-ido-completing-read
  10201. "Globally remove property: "
  10202. (mapcar 'list (org-buffer-property-keys)))))
  10203. (list prop)))
  10204. (save-excursion
  10205. (save-restriction
  10206. (widen)
  10207. (goto-char (point-min))
  10208. (let ((cnt 0))
  10209. (while (re-search-forward
  10210. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10211. nil t)
  10212. (setq cnt (1+ cnt))
  10213. (replace-match ""))
  10214. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10215. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10216. (defun org-compute-property-at-point ()
  10217. "Compute the property at point.
  10218. This looks for an enclosing column format, extracts the operator and
  10219. then applies it to the property in the column format's scope."
  10220. (interactive)
  10221. (unless (org-at-property-p)
  10222. (error "Not at a property"))
  10223. (let ((prop (org-match-string-no-properties 2)))
  10224. (org-columns-get-format-and-top-level)
  10225. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10226. (error "No operator defined for property %s" prop))
  10227. (org-columns-compute prop)))
  10228. (defun org-property-get-allowed-values (pom property &optional table)
  10229. "Get allowed values for the property PROPERTY.
  10230. When TABLE is non-nil, return an alist that can directly be used for
  10231. completion."
  10232. (let (vals)
  10233. (cond
  10234. ((equal property "TODO")
  10235. (setq vals (org-with-point-at pom
  10236. (append org-todo-keywords-1 '("")))))
  10237. ((equal property "PRIORITY")
  10238. (let ((n org-lowest-priority))
  10239. (while (>= n org-highest-priority)
  10240. (push (char-to-string n) vals)
  10241. (setq n (1- n)))))
  10242. ((member property org-special-properties))
  10243. (t
  10244. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10245. (when (and vals (string-match "\\S-" vals))
  10246. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10247. (setq vals (mapcar (lambda (x)
  10248. (cond ((stringp x) x)
  10249. ((numberp x) (number-to-string x))
  10250. ((symbolp x) (symbol-name x))
  10251. (t "???")))
  10252. vals)))))
  10253. (if table (mapcar 'list vals) vals)))
  10254. (defun org-property-previous-allowed-value (&optional previous)
  10255. "Switch to the next allowed value for this property."
  10256. (interactive)
  10257. (org-property-next-allowed-value t))
  10258. (defun org-property-next-allowed-value (&optional previous)
  10259. "Switch to the next allowed value for this property."
  10260. (interactive)
  10261. (unless (org-at-property-p)
  10262. (error "Not at a property"))
  10263. (let* ((key (match-string 2))
  10264. (value (match-string 3))
  10265. (allowed (or (org-property-get-allowed-values (point) key)
  10266. (and (member value '("[ ]" "[-]" "[X]"))
  10267. '("[ ]" "[X]"))))
  10268. nval)
  10269. (unless allowed
  10270. (error "Allowed values for this property have not been defined"))
  10271. (if previous (setq allowed (reverse allowed)))
  10272. (if (member value allowed)
  10273. (setq nval (car (cdr (member value allowed)))))
  10274. (setq nval (or nval (car allowed)))
  10275. (if (equal nval value)
  10276. (error "Only one allowed value for this property"))
  10277. (org-at-property-p)
  10278. (replace-match (concat " :" key ": " nval) t t)
  10279. (org-indent-line-function)
  10280. (beginning-of-line 1)
  10281. (skip-chars-forward " \t")))
  10282. (defun org-find-entry-with-id (ident)
  10283. "Locate the entry that contains the ID property with exact value IDENT.
  10284. IDENT can be a string, a symbol or a number, this function will search for
  10285. the string representation of it.
  10286. Return the position where this entry starts, or nil if there is no such entry."
  10287. (interactive "sID: ")
  10288. (let ((id (cond
  10289. ((stringp ident) ident)
  10290. ((symbol-name ident) (symbol-name ident))
  10291. ((numberp ident) (number-to-string ident))
  10292. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10293. (case-fold-search nil))
  10294. (save-excursion
  10295. (save-restriction
  10296. (widen)
  10297. (goto-char (point-min))
  10298. (when (re-search-forward
  10299. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10300. nil t)
  10301. (org-back-to-heading)
  10302. (point))))))
  10303. ;;;; Timestamps
  10304. (defvar org-last-changed-timestamp nil)
  10305. (defvar org-last-inserted-timestamp nil
  10306. "The last time stamp inserted with `org-insert-time-stamp'.")
  10307. (defvar org-time-was-given) ; dynamically scoped parameter
  10308. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10309. (defvar org-ts-what) ; dynamically scoped parameter
  10310. (defun org-time-stamp (arg &optional inactive)
  10311. "Prompt for a date/time and insert a time stamp.
  10312. If the user specifies a time like HH:MM, or if this command is called
  10313. with a prefix argument, the time stamp will contain date and time.
  10314. Otherwise, only the date will be included. All parts of a date not
  10315. specified by the user will be filled in from the current date/time.
  10316. So if you press just return without typing anything, the time stamp
  10317. will represent the current date/time. If there is already a timestamp
  10318. at the cursor, it will be modified."
  10319. (interactive "P")
  10320. (let* ((ts nil)
  10321. (default-time
  10322. ;; Default time is either today, or, when entering a range,
  10323. ;; the range start.
  10324. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10325. (save-excursion
  10326. (re-search-backward
  10327. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10328. (- (point) 20) t)))
  10329. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10330. (current-time)))
  10331. (default-input (and ts (org-get-compact-tod ts)))
  10332. org-time-was-given org-end-time-was-given time)
  10333. (cond
  10334. ((and (org-at-timestamp-p t)
  10335. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10336. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10337. (insert "--")
  10338. (setq time (let ((this-command this-command))
  10339. (org-read-date arg 'totime nil nil
  10340. default-time default-input)))
  10341. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10342. ((org-at-timestamp-p t)
  10343. (setq time (let ((this-command this-command))
  10344. (org-read-date arg 'totime nil nil default-time default-input)))
  10345. (when (org-at-timestamp-p t) ; just to get the match data
  10346. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10347. (replace-match "")
  10348. (setq org-last-changed-timestamp
  10349. (org-insert-time-stamp
  10350. time (or org-time-was-given arg)
  10351. inactive nil nil (list org-end-time-was-given))))
  10352. (message "Timestamp updated"))
  10353. (t
  10354. (setq time (let ((this-command this-command))
  10355. (org-read-date arg 'totime nil nil default-time default-input)))
  10356. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10357. nil nil (list org-end-time-was-given))))))
  10358. ;; FIXME: can we use this for something else, like computing time differences?
  10359. (defun org-get-compact-tod (s)
  10360. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10361. (let* ((t1 (match-string 1 s))
  10362. (h1 (string-to-number (match-string 2 s)))
  10363. (m1 (string-to-number (match-string 3 s)))
  10364. (t2 (and (match-end 4) (match-string 5 s)))
  10365. (h2 (and t2 (string-to-number (match-string 6 s))))
  10366. (m2 (and t2 (string-to-number (match-string 7 s))))
  10367. dh dm)
  10368. (if (not t2)
  10369. t1
  10370. (setq dh (- h2 h1) dm (- m2 m1))
  10371. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10372. (concat t1 "+" (number-to-string dh)
  10373. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10374. (defun org-time-stamp-inactive (&optional arg)
  10375. "Insert an inactive time stamp.
  10376. An inactive time stamp is enclosed in square brackets instead of angle
  10377. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10378. does not link to the calendar and cannot be changed with the S-cursor keys.
  10379. So these are more for recording a certain time/date."
  10380. (interactive "P")
  10381. (org-time-stamp arg 'inactive))
  10382. (defvar org-date-ovl (org-make-overlay 1 1))
  10383. (org-overlay-put org-date-ovl 'face 'org-warning)
  10384. (org-detach-overlay org-date-ovl)
  10385. (defvar org-ans1) ; dynamically scoped parameter
  10386. (defvar org-ans2) ; dynamically scoped parameter
  10387. (defvar org-plain-time-of-day-regexp) ; defined below
  10388. (defvar org-overriding-default-time nil) ; dynamically scoped
  10389. (defvar org-read-date-overlay nil)
  10390. (defvar org-dcst nil) ; dynamically scoped
  10391. (defun org-read-date (&optional with-time to-time from-string prompt
  10392. default-time default-input)
  10393. "Read a date, possibly a time, and make things smooth for the user.
  10394. The prompt will suggest to enter an ISO date, but you can also enter anything
  10395. which will at least partially be understood by `parse-time-string'.
  10396. Unrecognized parts of the date will default to the current day, month, year,
  10397. hour and minute. If this command is called to replace a timestamp at point,
  10398. of to enter the second timestamp of a range, the default time is taken from the
  10399. existing stamp. For example,
  10400. 3-2-5 --> 2003-02-05
  10401. feb 15 --> currentyear-02-15
  10402. sep 12 9 --> 2009-09-12
  10403. 12:45 --> today 12:45
  10404. 22 sept 0:34 --> currentyear-09-22 0:34
  10405. 12 --> currentyear-currentmonth-12
  10406. Fri --> nearest Friday (today or later)
  10407. etc.
  10408. Furthermore you can specify a relative date by giving, as the *first* thing
  10409. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10410. change in days weeks, months, years.
  10411. With a single plus or minus, the date is relative to today. With a double
  10412. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10413. +4d --> four days from today
  10414. +4 --> same as above
  10415. +2w --> two weeks from today
  10416. ++5 --> five days from default date
  10417. The function understands only English month and weekday abbreviations,
  10418. but this can be configured with the variables `parse-time-months' and
  10419. `parse-time-weekdays'.
  10420. While prompting, a calendar is popped up - you can also select the
  10421. date with the mouse (button 1). The calendar shows a period of three
  10422. months. To scroll it to other months, use the keys `>' and `<'.
  10423. If you don't like the calendar, turn it off with
  10424. \(setq org-read-date-popup-calendar nil)
  10425. With optional argument TO-TIME, the date will immediately be converted
  10426. to an internal time.
  10427. With an optional argument WITH-TIME, the prompt will suggest to also
  10428. insert a time. Note that when WITH-TIME is not set, you can still
  10429. enter a time, and this function will inform the calling routine about
  10430. this change. The calling routine may then choose to change the format
  10431. used to insert the time stamp into the buffer to include the time.
  10432. With optional argument FROM-STRING, read from this string instead from
  10433. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10434. the time/date that is used for everything that is not specified by the
  10435. user."
  10436. (require 'parse-time)
  10437. (let* ((org-time-stamp-rounding-minutes
  10438. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10439. (org-dcst org-display-custom-times)
  10440. (ct (org-current-time))
  10441. (def (or org-overriding-default-time default-time ct))
  10442. (defdecode (decode-time def))
  10443. (dummy (progn
  10444. (when (< (nth 2 defdecode) org-extend-today-until)
  10445. (setcar (nthcdr 2 defdecode) -1)
  10446. (setcar (nthcdr 1 defdecode) 59)
  10447. (setq def (apply 'encode-time defdecode)
  10448. defdecode (decode-time def)))))
  10449. (calendar-move-hook nil)
  10450. (calendar-view-diary-initially-flag nil)
  10451. (view-diary-entries-initially nil)
  10452. (calendar-view-holidays-initially-flag nil)
  10453. (view-calendar-holidays-initially nil)
  10454. (timestr (format-time-string
  10455. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10456. (prompt (concat (if prompt (concat prompt " ") "")
  10457. (format "Date+time [%s]: " timestr)))
  10458. ans (org-ans0 "") org-ans1 org-ans2 final)
  10459. (cond
  10460. (from-string (setq ans from-string))
  10461. (org-read-date-popup-calendar
  10462. (save-excursion
  10463. (save-window-excursion
  10464. (calendar)
  10465. (calendar-forward-day (- (time-to-days def)
  10466. (calendar-absolute-from-gregorian
  10467. (calendar-current-date))))
  10468. (org-eval-in-calendar nil t)
  10469. (let* ((old-map (current-local-map))
  10470. (map (copy-keymap calendar-mode-map))
  10471. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10472. (org-defkey map (kbd "RET") 'org-calendar-select)
  10473. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10474. 'org-calendar-select-mouse)
  10475. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10476. 'org-calendar-select-mouse)
  10477. (org-defkey minibuffer-local-map [(meta shift left)]
  10478. (lambda () (interactive)
  10479. (org-eval-in-calendar '(calendar-backward-month 1))))
  10480. (org-defkey minibuffer-local-map [(meta shift right)]
  10481. (lambda () (interactive)
  10482. (org-eval-in-calendar '(calendar-forward-month 1))))
  10483. (org-defkey minibuffer-local-map [(meta shift up)]
  10484. (lambda () (interactive)
  10485. (org-eval-in-calendar '(calendar-backward-year 1))))
  10486. (org-defkey minibuffer-local-map [(meta shift down)]
  10487. (lambda () (interactive)
  10488. (org-eval-in-calendar '(calendar-forward-year 1))))
  10489. (org-defkey minibuffer-local-map [(shift up)]
  10490. (lambda () (interactive)
  10491. (org-eval-in-calendar '(calendar-backward-week 1))))
  10492. (org-defkey minibuffer-local-map [(shift down)]
  10493. (lambda () (interactive)
  10494. (org-eval-in-calendar '(calendar-forward-week 1))))
  10495. (org-defkey minibuffer-local-map [(shift left)]
  10496. (lambda () (interactive)
  10497. (org-eval-in-calendar '(calendar-backward-day 1))))
  10498. (org-defkey minibuffer-local-map [(shift right)]
  10499. (lambda () (interactive)
  10500. (org-eval-in-calendar '(calendar-forward-day 1))))
  10501. (org-defkey minibuffer-local-map ">"
  10502. (lambda () (interactive)
  10503. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10504. (org-defkey minibuffer-local-map "<"
  10505. (lambda () (interactive)
  10506. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10507. (unwind-protect
  10508. (progn
  10509. (use-local-map map)
  10510. (add-hook 'post-command-hook 'org-read-date-display)
  10511. (setq org-ans0 (read-string prompt default-input nil nil))
  10512. ;; org-ans0: from prompt
  10513. ;; org-ans1: from mouse click
  10514. ;; org-ans2: from calendar motion
  10515. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10516. (remove-hook 'post-command-hook 'org-read-date-display)
  10517. (use-local-map old-map)
  10518. (when org-read-date-overlay
  10519. (org-delete-overlay org-read-date-overlay)
  10520. (setq org-read-date-overlay nil)))))))
  10521. (t ; Naked prompt only
  10522. (unwind-protect
  10523. (setq ans (read-string prompt default-input nil timestr))
  10524. (when org-read-date-overlay
  10525. (org-delete-overlay org-read-date-overlay)
  10526. (setq org-read-date-overlay nil)))))
  10527. (setq final (org-read-date-analyze ans def defdecode))
  10528. (if to-time
  10529. (apply 'encode-time final)
  10530. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10531. (format "%04d-%02d-%02d %02d:%02d"
  10532. (nth 5 final) (nth 4 final) (nth 3 final)
  10533. (nth 2 final) (nth 1 final))
  10534. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10535. (defvar def)
  10536. (defvar defdecode)
  10537. (defvar with-time)
  10538. (defun org-read-date-display ()
  10539. "Display the current date prompt interpretation in the minibuffer."
  10540. (when org-read-date-display-live
  10541. (when org-read-date-overlay
  10542. (org-delete-overlay org-read-date-overlay))
  10543. (let ((p (point)))
  10544. (end-of-line 1)
  10545. (while (not (equal (buffer-substring
  10546. (max (point-min) (- (point) 4)) (point))
  10547. " "))
  10548. (insert " "))
  10549. (goto-char p))
  10550. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10551. " " (or org-ans1 org-ans2)))
  10552. (org-end-time-was-given nil)
  10553. (f (org-read-date-analyze ans def defdecode))
  10554. (fmts (if org-dcst
  10555. org-time-stamp-custom-formats
  10556. org-time-stamp-formats))
  10557. (fmt (if (or with-time
  10558. (and (boundp 'org-time-was-given) org-time-was-given))
  10559. (cdr fmts)
  10560. (car fmts)))
  10561. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10562. (when (and org-end-time-was-given
  10563. (string-match org-plain-time-of-day-regexp txt))
  10564. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10565. org-end-time-was-given
  10566. (substring txt (match-end 0)))))
  10567. (setq org-read-date-overlay
  10568. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10569. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10570. (defun org-read-date-analyze (ans def defdecode)
  10571. "Analyse the combined answer of the date prompt."
  10572. ;; FIXME: cleanup and comment
  10573. (let (delta deltan deltaw deltadef year month day
  10574. hour minute second wday pm h2 m2 tl wday1
  10575. iso-year iso-weekday iso-week iso-year iso-date)
  10576. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10577. (setq ans "+0"))
  10578. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10579. (setq ans (replace-match "" t t ans)
  10580. deltan (car delta)
  10581. deltaw (nth 1 delta)
  10582. deltadef (nth 2 delta)))
  10583. ;; Check if there is an iso week date in there
  10584. ;; If yes, sore the info and postpone interpreting it until the rest
  10585. ;; of the parsing is done
  10586. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10587. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10588. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10589. iso-week (string-to-number (match-string 2 ans)))
  10590. (setq ans (replace-match "" t t ans)))
  10591. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10592. (when (string-match
  10593. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10594. (setq year (if (match-end 2)
  10595. (string-to-number (match-string 2 ans))
  10596. (string-to-number (format-time-string "%Y")))
  10597. month (string-to-number (match-string 3 ans))
  10598. day (string-to-number (match-string 4 ans)))
  10599. (if (< year 100) (setq year (+ 2000 year)))
  10600. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10601. t nil ans)))
  10602. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10603. ;; If there is a time with am/pm, and *no* time without it, we convert
  10604. ;; so that matching will be successful.
  10605. (loop for i from 1 to 2 do ; twice, for end time as well
  10606. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10607. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10608. (setq hour (string-to-number (match-string 1 ans))
  10609. minute (if (match-end 3)
  10610. (string-to-number (match-string 3 ans))
  10611. 0)
  10612. pm (equal ?p
  10613. (string-to-char (downcase (match-string 4 ans)))))
  10614. (if (and (= hour 12) (not pm))
  10615. (setq hour 0)
  10616. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10617. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10618. t t ans))))
  10619. ;; Check if a time range is given as a duration
  10620. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10621. (setq hour (string-to-number (match-string 1 ans))
  10622. h2 (+ hour (string-to-number (match-string 3 ans)))
  10623. minute (string-to-number (match-string 2 ans))
  10624. m2 (+ minute (if (match-end 5) (string-to-number
  10625. (match-string 5 ans))0)))
  10626. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10627. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10628. t t ans)))
  10629. ;; Check if there is a time range
  10630. (when (boundp 'org-end-time-was-given)
  10631. (setq org-time-was-given nil)
  10632. (when (and (string-match org-plain-time-of-day-regexp ans)
  10633. (match-end 8))
  10634. (setq org-end-time-was-given (match-string 8 ans))
  10635. (setq ans (concat (substring ans 0 (match-beginning 7))
  10636. (substring ans (match-end 7))))))
  10637. (setq tl (parse-time-string ans)
  10638. day (or (nth 3 tl) (nth 3 defdecode))
  10639. month (or (nth 4 tl)
  10640. (if (and org-read-date-prefer-future
  10641. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10642. (1+ (nth 4 defdecode))
  10643. (nth 4 defdecode)))
  10644. year (or (nth 5 tl)
  10645. (if (and org-read-date-prefer-future
  10646. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10647. (1+ (nth 5 defdecode))
  10648. (nth 5 defdecode)))
  10649. hour (or (nth 2 tl) (nth 2 defdecode))
  10650. minute (or (nth 1 tl) (nth 1 defdecode))
  10651. second (or (nth 0 tl) 0)
  10652. wday (nth 6 tl))
  10653. ;; Special date definitions below
  10654. (cond
  10655. (iso-week
  10656. ;; There was an iso week
  10657. (setq year (or iso-year year)
  10658. day (or iso-weekday wday 1)
  10659. wday nil ; to make sure that the trigger below does not match
  10660. iso-date (calendar-gregorian-from-absolute
  10661. (calendar-absolute-from-iso
  10662. (list iso-week day year))))
  10663. ; FIXME: Should we also push ISO weeks into the future?
  10664. ; (when (and org-read-date-prefer-future
  10665. ; (not iso-year)
  10666. ; (< (calendar-absolute-from-gregorian iso-date)
  10667. ; (time-to-days (current-time))))
  10668. ; (setq year (1+ year)
  10669. ; iso-date (calendar-gregorian-from-absolute
  10670. ; (calendar-absolute-from-iso
  10671. ; (list iso-week day year)))))
  10672. (setq month (car iso-date)
  10673. year (nth 2 iso-date)
  10674. day (nth 1 iso-date)))
  10675. (deltan
  10676. (unless deltadef
  10677. (let ((now (decode-time (current-time))))
  10678. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10679. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10680. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10681. ((equal deltaw "m") (setq month (+ month deltan)))
  10682. ((equal deltaw "y") (setq year (+ year deltan)))))
  10683. ((and wday (not (nth 3 tl)))
  10684. ;; Weekday was given, but no day, so pick that day in the week
  10685. ;; on or after the derived date.
  10686. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10687. (unless (equal wday wday1)
  10688. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10689. (if (and (boundp 'org-time-was-given)
  10690. (nth 2 tl))
  10691. (setq org-time-was-given t))
  10692. (if (< year 100) (setq year (+ 2000 year)))
  10693. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10694. (list second minute hour day month year)))
  10695. (defvar parse-time-weekdays)
  10696. (defun org-read-date-get-relative (s today default)
  10697. "Check string S for special relative date string.
  10698. TODAY and DEFAULT are internal times, for today and for a default.
  10699. Return shift list (N what def-flag)
  10700. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10701. N is the number of WHATs to shift.
  10702. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10703. the DEFAULT date rather than TODAY."
  10704. (when (and
  10705. (string-match
  10706. (concat
  10707. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10708. "\\([0-9]+\\)?"
  10709. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10710. "\\([ \t]\\|$\\)") s)
  10711. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10712. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10713. (string-to-char (substring (match-string 1 s) -1))
  10714. ?+))
  10715. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10716. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10717. (what (if (match-end 3) (match-string 3 s) "d"))
  10718. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10719. (date (if rel default today))
  10720. (wday (nth 6 (decode-time date)))
  10721. delta)
  10722. (if wday1
  10723. (progn
  10724. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10725. (if (= dir ?-) (setq delta (- delta 7)))
  10726. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10727. (list delta "d" rel))
  10728. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10729. (defun org-eval-in-calendar (form &optional keepdate)
  10730. "Eval FORM in the calendar window and return to current window.
  10731. Also, store the cursor date in variable org-ans2."
  10732. (let ((sw (selected-window)))
  10733. (select-window (get-buffer-window "*Calendar*"))
  10734. (eval form)
  10735. (when (and (not keepdate) (calendar-cursor-to-date))
  10736. (let* ((date (calendar-cursor-to-date))
  10737. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10738. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10739. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10740. (select-window sw)))
  10741. (defun org-calendar-select ()
  10742. "Return to `org-read-date' with the date currently selected.
  10743. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10744. (interactive)
  10745. (when (calendar-cursor-to-date)
  10746. (let* ((date (calendar-cursor-to-date))
  10747. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10748. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10749. (if (active-minibuffer-window) (exit-minibuffer))))
  10750. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10751. "Insert a date stamp for the date given by the internal TIME.
  10752. WITH-HM means, use the stamp format that includes the time of the day.
  10753. INACTIVE means use square brackets instead of angular ones, so that the
  10754. stamp will not contribute to the agenda.
  10755. PRE and POST are optional strings to be inserted before and after the
  10756. stamp.
  10757. The command returns the inserted time stamp."
  10758. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10759. stamp)
  10760. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10761. (insert-before-markers (or pre ""))
  10762. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10763. (when (listp extra)
  10764. (setq extra (car extra))
  10765. (if (and (stringp extra)
  10766. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10767. (setq extra (format "-%02d:%02d"
  10768. (string-to-number (match-string 1 extra))
  10769. (string-to-number (match-string 2 extra))))
  10770. (setq extra nil)))
  10771. (when extra
  10772. (backward-char 1)
  10773. (insert-before-markers extra)
  10774. (forward-char 1))
  10775. (insert-before-markers (or post ""))
  10776. (setq org-last-inserted-timestamp stamp)))
  10777. (defun org-toggle-time-stamp-overlays ()
  10778. "Toggle the use of custom time stamp formats."
  10779. (interactive)
  10780. (setq org-display-custom-times (not org-display-custom-times))
  10781. (unless org-display-custom-times
  10782. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10783. (while (setq p (next-single-property-change p 'display))
  10784. (if (and (get-text-property p 'display)
  10785. (eq (get-text-property p 'face) 'org-date))
  10786. (remove-text-properties
  10787. p (setq p (next-single-property-change p 'display))
  10788. '(display t))))
  10789. (set-buffer-modified-p bmp)))
  10790. (if (featurep 'xemacs)
  10791. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10792. (org-restart-font-lock)
  10793. (setq org-table-may-need-update t)
  10794. (if org-display-custom-times
  10795. (message "Time stamps are overlayed with custom format")
  10796. (message "Time stamp overlays removed")))
  10797. (defun org-display-custom-time (beg end)
  10798. "Overlay modified time stamp format over timestamp between BEG and END."
  10799. (let* ((ts (buffer-substring beg end))
  10800. t1 w1 with-hm tf time str w2 (off 0))
  10801. (save-match-data
  10802. (setq t1 (org-parse-time-string ts t))
  10803. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10804. (setq off (- (match-end 0) (match-beginning 0)))))
  10805. (setq end (- end off))
  10806. (setq w1 (- end beg)
  10807. with-hm (and (nth 1 t1) (nth 2 t1))
  10808. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10809. time (org-fix-decoded-time t1)
  10810. str (org-add-props
  10811. (format-time-string
  10812. (substring tf 1 -1) (apply 'encode-time time))
  10813. nil 'mouse-face 'highlight)
  10814. w2 (length str))
  10815. (if (not (= w2 w1))
  10816. (add-text-properties (1+ beg) (+ 2 beg)
  10817. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10818. (if (featurep 'xemacs)
  10819. (progn
  10820. (put-text-property beg end 'invisible t)
  10821. (put-text-property beg end 'end-glyph (make-glyph str)))
  10822. (put-text-property beg end 'display str))))
  10823. (defun org-translate-time (string)
  10824. "Translate all timestamps in STRING to custom format.
  10825. But do this only if the variable `org-display-custom-times' is set."
  10826. (when org-display-custom-times
  10827. (save-match-data
  10828. (let* ((start 0)
  10829. (re org-ts-regexp-both)
  10830. t1 with-hm inactive tf time str beg end)
  10831. (while (setq start (string-match re string start))
  10832. (setq beg (match-beginning 0)
  10833. end (match-end 0)
  10834. t1 (save-match-data
  10835. (org-parse-time-string (substring string beg end) t))
  10836. with-hm (and (nth 1 t1) (nth 2 t1))
  10837. inactive (equal (substring string beg (1+ beg)) "[")
  10838. tf (funcall (if with-hm 'cdr 'car)
  10839. org-time-stamp-custom-formats)
  10840. time (org-fix-decoded-time t1)
  10841. str (format-time-string
  10842. (concat
  10843. (if inactive "[" "<") (substring tf 1 -1)
  10844. (if inactive "]" ">"))
  10845. (apply 'encode-time time))
  10846. string (replace-match str t t string)
  10847. start (+ start (length str)))))))
  10848. string)
  10849. (defun org-fix-decoded-time (time)
  10850. "Set 0 instead of nil for the first 6 elements of time.
  10851. Don't touch the rest."
  10852. (let ((n 0))
  10853. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10854. (defun org-days-to-time (timestamp-string)
  10855. "Difference between TIMESTAMP-STRING and now in days."
  10856. (- (time-to-days (org-time-string-to-time timestamp-string))
  10857. (time-to-days (current-time))))
  10858. (defun org-deadline-close (timestamp-string &optional ndays)
  10859. "Is the time in TIMESTAMP-STRING close to the current date?"
  10860. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10861. (and (< (org-days-to-time timestamp-string) ndays)
  10862. (not (org-entry-is-done-p))))
  10863. (defun org-get-wdays (ts)
  10864. "Get the deadline lead time appropriate for timestring TS."
  10865. (cond
  10866. ((<= org-deadline-warning-days 0)
  10867. ;; 0 or negative, enforce this value no matter what
  10868. (- org-deadline-warning-days))
  10869. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  10870. ;; lead time is specified.
  10871. (floor (* (string-to-number (match-string 1 ts))
  10872. (cdr (assoc (match-string 2 ts)
  10873. '(("d" . 1) ("w" . 7)
  10874. ("m" . 30.4) ("y" . 365.25)))))))
  10875. ;; go for the default.
  10876. (t org-deadline-warning-days)))
  10877. (defun org-calendar-select-mouse (ev)
  10878. "Return to `org-read-date' with the date currently selected.
  10879. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10880. (interactive "e")
  10881. (mouse-set-point ev)
  10882. (when (calendar-cursor-to-date)
  10883. (let* ((date (calendar-cursor-to-date))
  10884. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10885. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10886. (if (active-minibuffer-window) (exit-minibuffer))))
  10887. (defun org-check-deadlines (ndays)
  10888. "Check if there are any deadlines due or past due.
  10889. A deadline is considered due if it happens within `org-deadline-warning-days'
  10890. days from today's date. If the deadline appears in an entry marked DONE,
  10891. it is not shown. The prefix arg NDAYS can be used to test that many
  10892. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10893. (interactive "P")
  10894. (let* ((org-warn-days
  10895. (cond
  10896. ((equal ndays '(4)) 100000)
  10897. (ndays (prefix-numeric-value ndays))
  10898. (t (abs org-deadline-warning-days))))
  10899. (case-fold-search nil)
  10900. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10901. (callback
  10902. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10903. (message "%d deadlines past-due or due within %d days"
  10904. (org-occur regexp nil callback)
  10905. org-warn-days)))
  10906. (defun org-check-before-date (date)
  10907. "Check if there are deadlines or scheduled entries before DATE."
  10908. (interactive (list (org-read-date)))
  10909. (let ((case-fold-search nil)
  10910. (regexp (concat "\\<\\(" org-deadline-string
  10911. "\\|" org-scheduled-string
  10912. "\\) *<\\([^>]+\\)>"))
  10913. (callback
  10914. (lambda () (time-less-p
  10915. (org-time-string-to-time (match-string 2))
  10916. (org-time-string-to-time date)))))
  10917. (message "%d entries before %s"
  10918. (org-occur regexp nil callback) date)))
  10919. (defun org-evaluate-time-range (&optional to-buffer)
  10920. "Evaluate a time range by computing the difference between start and end.
  10921. Normally the result is just printed in the echo area, but with prefix arg
  10922. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10923. If the time range is actually in a table, the result is inserted into the
  10924. next column.
  10925. For time difference computation, a year is assumed to be exactly 365
  10926. days in order to avoid rounding problems."
  10927. (interactive "P")
  10928. (or
  10929. (org-clock-update-time-maybe)
  10930. (save-excursion
  10931. (unless (org-at-date-range-p t)
  10932. (goto-char (point-at-bol))
  10933. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10934. (if (not (org-at-date-range-p t))
  10935. (error "Not at a time-stamp range, and none found in current line")))
  10936. (let* ((ts1 (match-string 1))
  10937. (ts2 (match-string 2))
  10938. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10939. (match-end (match-end 0))
  10940. (time1 (org-time-string-to-time ts1))
  10941. (time2 (org-time-string-to-time ts2))
  10942. (t1 (time-to-seconds time1))
  10943. (t2 (time-to-seconds time2))
  10944. (diff (abs (- t2 t1)))
  10945. (negative (< (- t2 t1) 0))
  10946. ;; (ys (floor (* 365 24 60 60)))
  10947. (ds (* 24 60 60))
  10948. (hs (* 60 60))
  10949. (fy "%dy %dd %02d:%02d")
  10950. (fy1 "%dy %dd")
  10951. (fd "%dd %02d:%02d")
  10952. (fd1 "%dd")
  10953. (fh "%02d:%02d")
  10954. y d h m align)
  10955. (if havetime
  10956. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10957. y 0
  10958. d (floor (/ diff ds)) diff (mod diff ds)
  10959. h (floor (/ diff hs)) diff (mod diff hs)
  10960. m (floor (/ diff 60)))
  10961. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10962. y 0
  10963. d (floor (+ (/ diff ds) 0.5))
  10964. h 0 m 0))
  10965. (if (not to-buffer)
  10966. (message "%s" (org-make-tdiff-string y d h m))
  10967. (if (org-at-table-p)
  10968. (progn
  10969. (goto-char match-end)
  10970. (setq align t)
  10971. (and (looking-at " *|") (goto-char (match-end 0))))
  10972. (goto-char match-end))
  10973. (if (looking-at
  10974. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10975. (replace-match ""))
  10976. (if negative (insert " -"))
  10977. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10978. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10979. (insert " " (format fh h m))))
  10980. (if align (org-table-align))
  10981. (message "Time difference inserted")))))
  10982. (defun org-make-tdiff-string (y d h m)
  10983. (let ((fmt "")
  10984. (l nil))
  10985. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10986. l (push y l)))
  10987. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10988. l (push d l)))
  10989. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10990. l (push h l)))
  10991. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10992. l (push m l)))
  10993. (apply 'format fmt (nreverse l))))
  10994. (defun org-time-string-to-time (s)
  10995. (apply 'encode-time (org-parse-time-string s)))
  10996. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10997. "Convert a time stamp to an absolute day number.
  10998. If there is a specifyer for a cyclic time stamp, get the closest date to
  10999. DAYNR.
  11000. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11001. (cond
  11002. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11003. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11004. daynr
  11005. (+ daynr 1000)))
  11006. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11007. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11008. (time-to-days (current-time))) (match-string 0 s)
  11009. prefer show-all))
  11010. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11011. (defun org-days-to-iso-week (days)
  11012. "Return the iso week number."
  11013. (require 'cal-iso)
  11014. (car (calendar-iso-from-absolute days)))
  11015. (defun org-small-year-to-year (year)
  11016. "Convert 2-digit years into 4-digit years.
  11017. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11018. The year 2000 cannot be abbreviated. Any year larger than 99
  11019. is returned unchanged."
  11020. (if (< year 38)
  11021. (setq year (+ 2000 year))
  11022. (if (< year 100)
  11023. (setq year (+ 1900 year))))
  11024. year)
  11025. (defun org-time-from-absolute (d)
  11026. "Return the time corresponding to date D.
  11027. D may be an absolute day number, or a calendar-type list (month day year)."
  11028. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11029. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11030. (defun org-calendar-holiday ()
  11031. "List of holidays, for Diary display in Org-mode."
  11032. (require 'holidays)
  11033. (let ((hl (funcall
  11034. (if (fboundp 'calendar-check-holidays)
  11035. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11036. (if hl (mapconcat 'identity hl "; "))))
  11037. (defun org-diary-sexp-entry (sexp entry date)
  11038. "Process a SEXP diary ENTRY for DATE."
  11039. (require 'diary-lib)
  11040. (let ((result (if calendar-debug-sexp
  11041. (let ((stack-trace-on-error t))
  11042. (eval (car (read-from-string sexp))))
  11043. (condition-case nil
  11044. (eval (car (read-from-string sexp)))
  11045. (error
  11046. (beep)
  11047. (message "Bad sexp at line %d in %s: %s"
  11048. (org-current-line)
  11049. (buffer-file-name) sexp)
  11050. (sleep-for 2))))))
  11051. (cond ((stringp result) result)
  11052. ((and (consp result)
  11053. (stringp (cdr result))) (cdr result))
  11054. (result entry)
  11055. (t nil))))
  11056. (defun org-diary-to-ical-string (frombuf)
  11057. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11058. This uses the icalendar.el library."
  11059. (let* ((tmpdir (if (featurep 'xemacs)
  11060. (temp-directory)
  11061. temporary-file-directory))
  11062. (tmpfile (make-temp-name
  11063. (expand-file-name "orgics" tmpdir)))
  11064. buf rtn b e)
  11065. (save-excursion
  11066. (set-buffer frombuf)
  11067. (icalendar-export-region (point-min) (point-max) tmpfile)
  11068. (setq buf (find-buffer-visiting tmpfile))
  11069. (set-buffer buf)
  11070. (goto-char (point-min))
  11071. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11072. (setq b (match-beginning 0)))
  11073. (goto-char (point-max))
  11074. (if (re-search-backward "^END:VEVENT" nil t)
  11075. (setq e (match-end 0)))
  11076. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11077. (kill-buffer buf)
  11078. (delete-file tmpfile)
  11079. rtn))
  11080. (defun org-closest-date (start current change prefer show-all)
  11081. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11082. When PREFER is `past' return a date that is either CURRENT or past.
  11083. When PREFER is `future', return a date that is either CURRENT or future.
  11084. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11085. ;; Make the proper lists from the dates
  11086. (catch 'exit
  11087. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11088. dn dw sday cday n1 n2 n0
  11089. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11090. (setq start (org-date-to-gregorian start)
  11091. current (org-date-to-gregorian
  11092. (if show-all
  11093. current
  11094. (time-to-days (current-time))))
  11095. sday (calendar-absolute-from-gregorian start)
  11096. cday (calendar-absolute-from-gregorian current))
  11097. (if (<= cday sday) (throw 'exit sday))
  11098. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11099. (setq dn (string-to-number (match-string 1 change))
  11100. dw (cdr (assoc (match-string 2 change) a1)))
  11101. (error "Invalid change specifyer: %s" change))
  11102. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11103. (cond
  11104. ((eq dw 'day)
  11105. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11106. n2 (+ n1 dn)))
  11107. ((eq dw 'year)
  11108. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11109. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11110. (setq date1 (list m d y1)
  11111. n1 (calendar-absolute-from-gregorian date1)
  11112. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11113. n2 (calendar-absolute-from-gregorian date2)))
  11114. ((eq dw 'month)
  11115. ;; approx number of month between the two dates
  11116. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11117. ;; How often does dn fit in there?
  11118. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11119. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11120. m (+ m nm)
  11121. ny (floor (/ m 12))
  11122. y (+ y ny)
  11123. m (- m (* ny 12)))
  11124. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11125. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11126. (setq m2 (+ m dn) y2 y)
  11127. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11128. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11129. (while (<= n2 cday)
  11130. (setq n1 n2 m m2 y y2)
  11131. (setq m2 (+ m dn) y2 y)
  11132. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11133. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11134. ;; Make sure n1 is the earlier date
  11135. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11136. (if show-all
  11137. (cond
  11138. ((eq prefer 'past) n1)
  11139. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11140. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11141. (cond
  11142. ((eq prefer 'past) n1)
  11143. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11144. (t (if (= cday n1) n1 n2)))))))
  11145. (defun org-date-to-gregorian (date)
  11146. "Turn any specification of DATE into a gregorian date for the calendar."
  11147. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11148. ((and (listp date) (= (length date) 3)) date)
  11149. ((stringp date)
  11150. (setq date (org-parse-time-string date))
  11151. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11152. ((listp date)
  11153. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11154. (defun org-parse-time-string (s &optional nodefault)
  11155. "Parse the standard Org-mode time string.
  11156. This should be a lot faster than the normal `parse-time-string'.
  11157. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11158. hour and minute fields will be nil if not given."
  11159. (if (string-match org-ts-regexp0 s)
  11160. (list 0
  11161. (if (or (match-beginning 8) (not nodefault))
  11162. (string-to-number (or (match-string 8 s) "0")))
  11163. (if (or (match-beginning 7) (not nodefault))
  11164. (string-to-number (or (match-string 7 s) "0")))
  11165. (string-to-number (match-string 4 s))
  11166. (string-to-number (match-string 3 s))
  11167. (string-to-number (match-string 2 s))
  11168. nil nil nil)
  11169. (make-list 9 0)))
  11170. (defun org-timestamp-up (&optional arg)
  11171. "Increase the date item at the cursor by one.
  11172. If the cursor is on the year, change the year. If it is on the month or
  11173. the day, change that.
  11174. With prefix ARG, change by that many units."
  11175. (interactive "p")
  11176. (org-timestamp-change (prefix-numeric-value arg)))
  11177. (defun org-timestamp-down (&optional arg)
  11178. "Decrease the date item at the cursor by one.
  11179. If the cursor is on the year, change the year. If it is on the month or
  11180. the day, change that.
  11181. With prefix ARG, change by that many units."
  11182. (interactive "p")
  11183. (org-timestamp-change (- (prefix-numeric-value arg))))
  11184. (defun org-timestamp-up-day (&optional arg)
  11185. "Increase the date in the time stamp by one day.
  11186. With prefix ARG, change that many days."
  11187. (interactive "p")
  11188. (if (and (not (org-at-timestamp-p t))
  11189. (org-on-heading-p))
  11190. (org-todo 'up)
  11191. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11192. (defun org-timestamp-down-day (&optional arg)
  11193. "Decrease the date in the time stamp by one day.
  11194. With prefix ARG, change that many days."
  11195. (interactive "p")
  11196. (if (and (not (org-at-timestamp-p t))
  11197. (org-on-heading-p))
  11198. (org-todo 'down)
  11199. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11200. (defun org-at-timestamp-p (&optional inactive-ok)
  11201. "Determine if the cursor is in or at a timestamp."
  11202. (interactive)
  11203. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11204. (pos (point))
  11205. (ans (or (looking-at tsr)
  11206. (save-excursion
  11207. (skip-chars-backward "^[<\n\r\t")
  11208. (if (> (point) (point-min)) (backward-char 1))
  11209. (and (looking-at tsr)
  11210. (> (- (match-end 0) pos) -1))))))
  11211. (and ans
  11212. (boundp 'org-ts-what)
  11213. (setq org-ts-what
  11214. (cond
  11215. ((= pos (match-beginning 0)) 'bracket)
  11216. ((= pos (1- (match-end 0))) 'bracket)
  11217. ((org-pos-in-match-range pos 2) 'year)
  11218. ((org-pos-in-match-range pos 3) 'month)
  11219. ((org-pos-in-match-range pos 7) 'hour)
  11220. ((org-pos-in-match-range pos 8) 'minute)
  11221. ((or (org-pos-in-match-range pos 4)
  11222. (org-pos-in-match-range pos 5)) 'day)
  11223. ((and (> pos (or (match-end 8) (match-end 5)))
  11224. (< pos (match-end 0)))
  11225. (- pos (or (match-end 8) (match-end 5))))
  11226. (t 'day))))
  11227. ans))
  11228. (defun org-toggle-timestamp-type ()
  11229. "Toggle the type (<active> or [inactive]) of a time stamp."
  11230. (interactive)
  11231. (when (org-at-timestamp-p t)
  11232. (let ((beg (match-beginning 0)) (end (match-end 0))
  11233. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11234. (save-excursion
  11235. (goto-char beg)
  11236. (while (re-search-forward "[][<>]" end t)
  11237. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11238. t t)))
  11239. (message "Timestamp is now %sactive"
  11240. (if (equal (char-after beg) ?<) "" "in")))))
  11241. (defun org-timestamp-change (n &optional what)
  11242. "Change the date in the time stamp at point.
  11243. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11244. `year', `minute', `second'. If WHAT is not given, the cursor position
  11245. in the timestamp determines what will be changed."
  11246. (let ((pos (point))
  11247. with-hm inactive
  11248. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11249. org-ts-what
  11250. extra rem
  11251. ts time time0)
  11252. (if (not (org-at-timestamp-p t))
  11253. (error "Not at a timestamp"))
  11254. (if (and (not what) (eq org-ts-what 'bracket))
  11255. (org-toggle-timestamp-type)
  11256. (if (and (not what) (not (eq org-ts-what 'day))
  11257. org-display-custom-times
  11258. (get-text-property (point) 'display)
  11259. (not (get-text-property (1- (point)) 'display)))
  11260. (setq org-ts-what 'day))
  11261. (setq org-ts-what (or what org-ts-what)
  11262. inactive (= (char-after (match-beginning 0)) ?\[)
  11263. ts (match-string 0))
  11264. (replace-match "")
  11265. (if (string-match
  11266. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11267. ts)
  11268. (setq extra (match-string 1 ts)))
  11269. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11270. (setq with-hm t))
  11271. (setq time0 (org-parse-time-string ts))
  11272. (when (and (eq org-ts-what 'minute)
  11273. (eq current-prefix-arg nil))
  11274. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11275. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11276. (setcar (cdr time0) (+ (nth 1 time0)
  11277. (if (> n 0) (- rem) (- dm rem))))))
  11278. (setq time
  11279. (encode-time (or (car time0) 0)
  11280. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11281. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11282. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11283. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11284. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11285. (nthcdr 6 time0)))
  11286. (when (integerp org-ts-what)
  11287. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11288. (if (eq what 'calendar)
  11289. (let ((cal-date (org-get-date-from-calendar)))
  11290. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11291. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11292. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11293. (setcar time0 (or (car time0) 0))
  11294. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11295. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11296. (setq time (apply 'encode-time time0))))
  11297. (setq org-last-changed-timestamp
  11298. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11299. (org-clock-update-time-maybe)
  11300. (goto-char pos)
  11301. ;; Try to recenter the calendar window, if any
  11302. (if (and org-calendar-follow-timestamp-change
  11303. (get-buffer-window "*Calendar*" t)
  11304. (memq org-ts-what '(day month year)))
  11305. (org-recenter-calendar (time-to-days time))))))
  11306. (defun org-modify-ts-extra (s pos n dm)
  11307. "Change the different parts of the lead-time and repeat fields in timestamp."
  11308. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11309. ng h m new rem)
  11310. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11311. (cond
  11312. ((or (org-pos-in-match-range pos 2)
  11313. (org-pos-in-match-range pos 3))
  11314. (setq m (string-to-number (match-string 3 s))
  11315. h (string-to-number (match-string 2 s)))
  11316. (if (org-pos-in-match-range pos 2)
  11317. (setq h (+ h n))
  11318. (setq n (* dm (org-no-warnings (signum n))))
  11319. (when (not (= 0 (setq rem (% m dm))))
  11320. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11321. (setq m (+ m n)))
  11322. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11323. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11324. (setq h (min 24 (max 0 h)))
  11325. (setq ng 1 new (format "-%02d:%02d" h m)))
  11326. ((org-pos-in-match-range pos 6)
  11327. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11328. ((org-pos-in-match-range pos 5)
  11329. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11330. ((org-pos-in-match-range pos 9)
  11331. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11332. ((org-pos-in-match-range pos 8)
  11333. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11334. (when ng
  11335. (setq s (concat
  11336. (substring s 0 (match-beginning ng))
  11337. new
  11338. (substring s (match-end ng))))))
  11339. s))
  11340. (defun org-recenter-calendar (date)
  11341. "If the calendar is visible, recenter it to DATE."
  11342. (let* ((win (selected-window))
  11343. (cwin (get-buffer-window "*Calendar*" t))
  11344. (calendar-move-hook nil))
  11345. (when cwin
  11346. (select-window cwin)
  11347. (calendar-goto-date (if (listp date) date
  11348. (calendar-gregorian-from-absolute date)))
  11349. (select-window win))))
  11350. (defun org-goto-calendar (&optional arg)
  11351. "Go to the Emacs calendar at the current date.
  11352. If there is a time stamp in the current line, go to that date.
  11353. A prefix ARG can be used to force the current date."
  11354. (interactive "P")
  11355. (let ((tsr org-ts-regexp) diff
  11356. (calendar-move-hook nil)
  11357. (calendar-view-holidays-initially-flag nil)
  11358. (view-calendar-holidays-initially nil)
  11359. (calendar-view-diary-initially-flag nil)
  11360. (view-diary-entries-initially nil))
  11361. (if (or (org-at-timestamp-p)
  11362. (save-excursion
  11363. (beginning-of-line 1)
  11364. (looking-at (concat ".*" tsr))))
  11365. (let ((d1 (time-to-days (current-time)))
  11366. (d2 (time-to-days
  11367. (org-time-string-to-time (match-string 1)))))
  11368. (setq diff (- d2 d1))))
  11369. (calendar)
  11370. (calendar-goto-today)
  11371. (if (and diff (not arg)) (calendar-forward-day diff))))
  11372. (defun org-get-date-from-calendar ()
  11373. "Return a list (month day year) of date at point in calendar."
  11374. (with-current-buffer "*Calendar*"
  11375. (save-match-data
  11376. (calendar-cursor-to-date))))
  11377. (defun org-date-from-calendar ()
  11378. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11379. If there is already a time stamp at the cursor position, update it."
  11380. (interactive)
  11381. (if (org-at-timestamp-p t)
  11382. (org-timestamp-change 0 'calendar)
  11383. (let ((cal-date (org-get-date-from-calendar)))
  11384. (org-insert-time-stamp
  11385. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11386. (defun org-minutes-to-hh:mm-string (m)
  11387. "Compute H:MM from a number of minutes."
  11388. (let ((h (/ m 60)))
  11389. (setq m (- m (* 60 h)))
  11390. (format org-time-clocksum-format h m)))
  11391. (defun org-hh:mm-string-to-minutes (s)
  11392. "Convert a string H:MM to a number of minutes."
  11393. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11394. (+ (* (string-to-number (match-string 1 s)) 60)
  11395. (string-to-number (match-string 2 s)))
  11396. 0))
  11397. ;;;; Files
  11398. (defun org-save-all-org-buffers ()
  11399. "Save all Org-mode buffers without user confirmation."
  11400. (interactive)
  11401. (message "Saving all Org-mode buffers...")
  11402. (save-some-buffers t 'org-mode-p)
  11403. (when (featurep 'org-id) (org-id-locations-save))
  11404. (message "Saving all Org-mode buffers... done"))
  11405. (defun org-revert-all-org-buffers ()
  11406. "Revert all Org-mode buffers.
  11407. Prompt for confirmation when there are unsaved changes.
  11408. Be sure you know what you are doing before letting this function
  11409. overwrite your changes.
  11410. This function is useful in a setup where one tracks org files
  11411. with a version control system, to revert on one machine after pulling
  11412. changes from another. I believe the procedure must be like this:
  11413. 1. M-x org-save-all-org-buffers
  11414. 2. Pull changes from the other machine, resolve conflicts
  11415. 3. M-x org-revert-all-org-buffers"
  11416. (interactive)
  11417. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11418. (error "Abort"))
  11419. (save-excursion
  11420. (save-window-excursion
  11421. (mapc
  11422. (lambda (b)
  11423. (when (and (with-current-buffer b (org-mode-p))
  11424. (with-current-buffer b buffer-file-name))
  11425. (switch-to-buffer b)
  11426. (revert-buffer t 'no-confirm)))
  11427. (buffer-list))
  11428. (when (and (featurep 'org-id) org-id-track-globally)
  11429. (org-id-locations-load)))))
  11430. ;;;; Agenda files
  11431. ;;;###autoload
  11432. (defun org-iswitchb (&optional arg)
  11433. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11434. With a prefix argument, restrict available to files.
  11435. With two prefix arguments, restrict available buffers to agenda files.
  11436. Due to some yet unresolved reason, the global function
  11437. `iswitchb-mode' needs to be active for this function to work."
  11438. (interactive "P")
  11439. (require 'iswitchb)
  11440. (let ((enabled iswitchb-mode) blist)
  11441. (or enabled (iswitchb-mode 1))
  11442. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11443. ((equal arg '(16)) (org-buffer-list 'agenda))
  11444. (t (org-buffer-list))))
  11445. (unwind-protect
  11446. (let ((iswitchb-make-buflist-hook
  11447. (lambda ()
  11448. (setq iswitchb-temp-buflist
  11449. (mapcar 'buffer-name blist)))))
  11450. (switch-to-buffer
  11451. (iswitchb-read-buffer
  11452. "Switch-to: " nil t))
  11453. (or enabled (iswitchb-mode -1))))))
  11454. ;;;###autoload
  11455. (defun org-ido-switchb (&optional arg)
  11456. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11457. With a prefix argument, restrict available to files.
  11458. With two prefix arguments, restrict available buffers to agenda files."
  11459. (interactive "P")
  11460. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11461. ((equal arg '(16)) (org-buffer-list 'agenda))
  11462. (t (org-buffer-list)))))
  11463. (switch-to-buffer
  11464. (org-ido-completing-read "Org buffer: "
  11465. (mapcar 'buffer-name blist)
  11466. nil t))))
  11467. (defun org-buffer-list (&optional predicate exclude-tmp)
  11468. "Return a list of Org buffers.
  11469. PREDICATE can be `export', `files' or `agenda'.
  11470. export restrict the list to Export buffers.
  11471. files restrict the list to buffers visiting Org files.
  11472. agenda restrict the list to buffers visiting agenda files.
  11473. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11474. (let* ((bfn nil)
  11475. (agenda-files (and (eq predicate 'agenda)
  11476. (mapcar 'file-truename (org-agenda-files t))))
  11477. (filter
  11478. (cond
  11479. ((eq predicate 'files)
  11480. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11481. ((eq predicate 'export)
  11482. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11483. ((eq predicate 'agenda)
  11484. (lambda (b)
  11485. (with-current-buffer b
  11486. (and (eq major-mode 'org-mode)
  11487. (setq bfn (buffer-file-name b))
  11488. (member (file-truename bfn) agenda-files)))))
  11489. (t (lambda (b) (with-current-buffer b
  11490. (or (eq major-mode 'org-mode)
  11491. (string-match "\*Org .*Export"
  11492. (buffer-name b)))))))))
  11493. (delq nil
  11494. (mapcar
  11495. (lambda(b)
  11496. (if (and (funcall filter b)
  11497. (or (not exclude-tmp)
  11498. (not (string-match "tmp" (buffer-name b)))))
  11499. b
  11500. nil))
  11501. (buffer-list)))))
  11502. (defun org-agenda-files (&optional unrestricted archives)
  11503. "Get the list of agenda files.
  11504. Optional UNRESTRICTED means return the full list even if a restriction
  11505. is currently in place.
  11506. When ARCHIVES is t, include all archive files hat are really being
  11507. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11508. `org-agenda-archives-mode' is t."
  11509. (let ((files
  11510. (cond
  11511. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11512. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11513. ((listp org-agenda-files) org-agenda-files)
  11514. (t (error "Invalid value of `org-agenda-files'")))))
  11515. (setq files (apply 'append
  11516. (mapcar (lambda (f)
  11517. (if (file-directory-p f)
  11518. (directory-files
  11519. f t org-agenda-file-regexp)
  11520. (list f)))
  11521. files)))
  11522. (when org-agenda-skip-unavailable-files
  11523. (setq files (delq nil
  11524. (mapcar (function
  11525. (lambda (file)
  11526. (and (file-readable-p file) file)))
  11527. files))))
  11528. (when (or (eq archives t)
  11529. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11530. (setq files (org-add-archive-files files)))
  11531. files))
  11532. (defun org-edit-agenda-file-list ()
  11533. "Edit the list of agenda files.
  11534. Depending on setup, this either uses customize to edit the variable
  11535. `org-agenda-files', or it visits the file that is holding the list. In the
  11536. latter case, the buffer is set up in a way that saving it automatically kills
  11537. the buffer and restores the previous window configuration."
  11538. (interactive)
  11539. (if (stringp org-agenda-files)
  11540. (let ((cw (current-window-configuration)))
  11541. (find-file org-agenda-files)
  11542. (org-set-local 'org-window-configuration cw)
  11543. (org-add-hook 'after-save-hook
  11544. (lambda ()
  11545. (set-window-configuration
  11546. (prog1 org-window-configuration
  11547. (kill-buffer (current-buffer))))
  11548. (org-install-agenda-files-menu)
  11549. (message "New agenda file list installed"))
  11550. nil 'local)
  11551. (message "%s" (substitute-command-keys
  11552. "Edit list and finish with \\[save-buffer]")))
  11553. (customize-variable 'org-agenda-files)))
  11554. (defun org-store-new-agenda-file-list (list)
  11555. "Set new value for the agenda file list and save it correctly."
  11556. (if (stringp org-agenda-files)
  11557. (let ((f org-agenda-files) b)
  11558. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11559. (with-temp-file f
  11560. (insert (mapconcat 'identity list "\n") "\n")))
  11561. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11562. (setq org-agenda-files list)
  11563. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11564. (defun org-read-agenda-file-list ()
  11565. "Read the list of agenda files from a file."
  11566. (when (file-directory-p org-agenda-files)
  11567. (error "`org-agenda-files' cannot be a single directory"))
  11568. (when (stringp org-agenda-files)
  11569. (with-temp-buffer
  11570. (insert-file-contents org-agenda-files)
  11571. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11572. ;;;###autoload
  11573. (defun org-cycle-agenda-files ()
  11574. "Cycle through the files in `org-agenda-files'.
  11575. If the current buffer visits an agenda file, find the next one in the list.
  11576. If the current buffer does not, find the first agenda file."
  11577. (interactive)
  11578. (let* ((fs (org-agenda-files t))
  11579. (files (append fs (list (car fs))))
  11580. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11581. file)
  11582. (unless files (error "No agenda files"))
  11583. (catch 'exit
  11584. (while (setq file (pop files))
  11585. (if (equal (file-truename file) tcf)
  11586. (when (car files)
  11587. (find-file (car files))
  11588. (throw 'exit t))))
  11589. (find-file (car fs)))
  11590. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11591. (defun org-agenda-file-to-front (&optional to-end)
  11592. "Move/add the current file to the top of the agenda file list.
  11593. If the file is not present in the list, it is added to the front. If it is
  11594. present, it is moved there. With optional argument TO-END, add/move to the
  11595. end of the list."
  11596. (interactive "P")
  11597. (let ((org-agenda-skip-unavailable-files nil)
  11598. (file-alist (mapcar (lambda (x)
  11599. (cons (file-truename x) x))
  11600. (org-agenda-files t)))
  11601. (ctf (file-truename buffer-file-name))
  11602. x had)
  11603. (setq x (assoc ctf file-alist) had x)
  11604. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11605. (if to-end
  11606. (setq file-alist (append (delq x file-alist) (list x)))
  11607. (setq file-alist (cons x (delq x file-alist))))
  11608. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11609. (org-install-agenda-files-menu)
  11610. (message "File %s to %s of agenda file list"
  11611. (if had "moved" "added") (if to-end "end" "front"))))
  11612. (defun org-remove-file (&optional file)
  11613. "Remove current file from the list of files in variable `org-agenda-files'.
  11614. These are the files which are being checked for agenda entries.
  11615. Optional argument FILE means, use this file instead of the current."
  11616. (interactive)
  11617. (let* ((org-agenda-skip-unavailable-files nil)
  11618. (file (or file buffer-file-name))
  11619. (true-file (file-truename file))
  11620. (afile (abbreviate-file-name file))
  11621. (files (delq nil (mapcar
  11622. (lambda (x)
  11623. (if (equal true-file
  11624. (file-truename x))
  11625. nil x))
  11626. (org-agenda-files t)))))
  11627. (if (not (= (length files) (length (org-agenda-files t))))
  11628. (progn
  11629. (org-store-new-agenda-file-list files)
  11630. (org-install-agenda-files-menu)
  11631. (message "Removed file: %s" afile))
  11632. (message "File was not in list: %s (not removed)" afile))))
  11633. (defun org-file-menu-entry (file)
  11634. (vector file (list 'find-file file) t))
  11635. (defun org-check-agenda-file (file)
  11636. "Make sure FILE exists. If not, ask user what to do."
  11637. (when (not (file-exists-p file))
  11638. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11639. (abbreviate-file-name file))
  11640. (let ((r (downcase (read-char-exclusive))))
  11641. (cond
  11642. ((equal r ?r)
  11643. (org-remove-file file)
  11644. (throw 'nextfile t))
  11645. (t (error "Abort"))))))
  11646. (defun org-get-agenda-file-buffer (file)
  11647. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11648. it to the list of buffers which might be released later."
  11649. (let ((buf (org-find-base-buffer-visiting file)))
  11650. (if buf
  11651. buf ; just return it
  11652. ;; Make a new buffer and remember it
  11653. (setq buf (find-file-noselect file))
  11654. (if buf (push buf org-agenda-new-buffers))
  11655. buf)))
  11656. (defun org-release-buffers (blist)
  11657. "Release all buffers in list, asking the user for confirmation when needed.
  11658. When a buffer is unmodified, it is just killed. When modified, it is saved
  11659. \(if the user agrees) and then killed."
  11660. (let (buf file)
  11661. (while (setq buf (pop blist))
  11662. (setq file (buffer-file-name buf))
  11663. (when (and (buffer-modified-p buf)
  11664. file
  11665. (y-or-n-p (format "Save file %s? " file)))
  11666. (with-current-buffer buf (save-buffer)))
  11667. (kill-buffer buf))))
  11668. (defun org-prepare-agenda-buffers (files)
  11669. "Create buffers for all agenda files, protect archived trees and comments."
  11670. (interactive)
  11671. (let ((pa '(:org-archived t))
  11672. (pc '(:org-comment t))
  11673. (pall '(:org-archived t :org-comment t))
  11674. (inhibit-read-only t)
  11675. (rea (concat ":" org-archive-tag ":"))
  11676. bmp file re)
  11677. (save-excursion
  11678. (save-restriction
  11679. (while (setq file (pop files))
  11680. (if (bufferp file)
  11681. (set-buffer file)
  11682. (org-check-agenda-file file)
  11683. (set-buffer (org-get-agenda-file-buffer file)))
  11684. (widen)
  11685. (setq bmp (buffer-modified-p))
  11686. (org-refresh-category-properties)
  11687. (setq org-todo-keywords-for-agenda
  11688. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11689. (setq org-done-keywords-for-agenda
  11690. (append org-done-keywords-for-agenda org-done-keywords))
  11691. (setq org-todo-keyword-alist-for-agenda
  11692. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11693. (setq org-tag-alist-for-agenda
  11694. (append org-tag-alist-for-agenda org-tag-alist))
  11695. (save-excursion
  11696. (remove-text-properties (point-min) (point-max) pall)
  11697. (when org-agenda-skip-archived-trees
  11698. (goto-char (point-min))
  11699. (while (re-search-forward rea nil t)
  11700. (if (org-on-heading-p t)
  11701. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11702. (goto-char (point-min))
  11703. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11704. (while (re-search-forward re nil t)
  11705. (add-text-properties
  11706. (match-beginning 0) (org-end-of-subtree t) pc)))
  11707. (set-buffer-modified-p bmp))))
  11708. (setq org-todo-keyword-alist-for-agenda
  11709. (org-uniquify org-todo-keyword-alist-for-agenda)
  11710. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11711. ;;;; Embedded LaTeX
  11712. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11713. "Keymap for the minor `org-cdlatex-mode'.")
  11714. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11715. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11716. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11717. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11718. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11719. (defvar org-cdlatex-texmathp-advice-is-done nil
  11720. "Flag remembering if we have applied the advice to texmathp already.")
  11721. (define-minor-mode org-cdlatex-mode
  11722. "Toggle the minor `org-cdlatex-mode'.
  11723. This mode supports entering LaTeX environment and math in LaTeX fragments
  11724. in Org-mode.
  11725. \\{org-cdlatex-mode-map}"
  11726. nil " OCDL" nil
  11727. (when org-cdlatex-mode (require 'cdlatex))
  11728. (unless org-cdlatex-texmathp-advice-is-done
  11729. (setq org-cdlatex-texmathp-advice-is-done t)
  11730. (defadvice texmathp (around org-math-always-on activate)
  11731. "Always return t in org-mode buffers.
  11732. This is because we want to insert math symbols without dollars even outside
  11733. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11734. an embedded LaTeX fragment, let texmathp do its job.
  11735. \\[org-cdlatex-mode-map]"
  11736. (interactive)
  11737. (let (p)
  11738. (cond
  11739. ((not (org-mode-p)) ad-do-it)
  11740. ((eq this-command 'cdlatex-math-symbol)
  11741. (setq ad-return-value t
  11742. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11743. (t
  11744. (let ((p (org-inside-LaTeX-fragment-p)))
  11745. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11746. (setq ad-return-value t
  11747. texmathp-why '("Org-mode embedded math" . 0))
  11748. (if p ad-do-it)))))))))
  11749. (defun turn-on-org-cdlatex ()
  11750. "Unconditionally turn on `org-cdlatex-mode'."
  11751. (org-cdlatex-mode 1))
  11752. (defun org-inside-LaTeX-fragment-p ()
  11753. "Test if point is inside a LaTeX fragment.
  11754. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11755. sequence appearing also before point.
  11756. Even though the matchers for math are configurable, this function assumes
  11757. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11758. delimiters are skipped when they have been removed by customization.
  11759. The return value is nil, or a cons cell with the delimiter and
  11760. and the position of this delimiter.
  11761. This function does a reasonably good job, but can locally be fooled by
  11762. for example currency specifications. For example it will assume being in
  11763. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11764. fragments that are properly closed, but during editing, we have to live
  11765. with the uncertainty caused by missing closing delimiters. This function
  11766. looks only before point, not after."
  11767. (catch 'exit
  11768. (let ((pos (point))
  11769. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11770. (lim (progn
  11771. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11772. (point)))
  11773. dd-on str (start 0) m re)
  11774. (goto-char pos)
  11775. (when dodollar
  11776. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11777. re (nth 1 (assoc "$" org-latex-regexps)))
  11778. (while (string-match re str start)
  11779. (cond
  11780. ((= (match-end 0) (length str))
  11781. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11782. ((= (match-end 0) (- (length str) 5))
  11783. (throw 'exit nil))
  11784. (t (setq start (match-end 0))))))
  11785. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11786. (goto-char pos)
  11787. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11788. (and (match-beginning 2) (throw 'exit nil))
  11789. ;; count $$
  11790. (while (re-search-backward "\\$\\$" lim t)
  11791. (setq dd-on (not dd-on)))
  11792. (goto-char pos)
  11793. (if dd-on (cons "$$" m))))))
  11794. (defun org-try-cdlatex-tab ()
  11795. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11796. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11797. - inside a LaTeX fragment, or
  11798. - after the first word in a line, where an abbreviation expansion could
  11799. insert a LaTeX environment."
  11800. (when org-cdlatex-mode
  11801. (cond
  11802. ((save-excursion
  11803. (skip-chars-backward "a-zA-Z0-9*")
  11804. (skip-chars-backward " \t")
  11805. (bolp))
  11806. (cdlatex-tab) t)
  11807. ((org-inside-LaTeX-fragment-p)
  11808. (cdlatex-tab) t)
  11809. (t nil))))
  11810. (defun org-cdlatex-underscore-caret (&optional arg)
  11811. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11812. Revert to the normal definition outside of these fragments."
  11813. (interactive "P")
  11814. (if (org-inside-LaTeX-fragment-p)
  11815. (call-interactively 'cdlatex-sub-superscript)
  11816. (let (org-cdlatex-mode)
  11817. (call-interactively (key-binding (vector last-input-event))))))
  11818. (defun org-cdlatex-math-modify (&optional arg)
  11819. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11820. Revert to the normal definition outside of these fragments."
  11821. (interactive "P")
  11822. (if (org-inside-LaTeX-fragment-p)
  11823. (call-interactively 'cdlatex-math-modify)
  11824. (let (org-cdlatex-mode)
  11825. (call-interactively (key-binding (vector last-input-event))))))
  11826. (defvar org-latex-fragment-image-overlays nil
  11827. "List of overlays carrying the images of latex fragments.")
  11828. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11829. (defun org-remove-latex-fragment-image-overlays ()
  11830. "Remove all overlays with LaTeX fragment images in current buffer."
  11831. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11832. (setq org-latex-fragment-image-overlays nil))
  11833. (defun org-preview-latex-fragment (&optional subtree)
  11834. "Preview the LaTeX fragment at point, or all locally or globally.
  11835. If the cursor is in a LaTeX fragment, create the image and overlay
  11836. it over the source code. If there is no fragment at point, display
  11837. all fragments in the current text, from one headline to the next. With
  11838. prefix SUBTREE, display all fragments in the current subtree. With a
  11839. double prefix `C-u C-u', or when the cursor is before the first headline,
  11840. display all fragments in the buffer.
  11841. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11842. (interactive "P")
  11843. (org-remove-latex-fragment-image-overlays)
  11844. (save-excursion
  11845. (save-restriction
  11846. (let (beg end at msg)
  11847. (cond
  11848. ((or (equal subtree '(16))
  11849. (not (save-excursion
  11850. (re-search-backward (concat "^" outline-regexp) nil t))))
  11851. (setq beg (point-min) end (point-max)
  11852. msg "Creating images for buffer...%s"))
  11853. ((equal subtree '(4))
  11854. (org-back-to-heading)
  11855. (setq beg (point) end (org-end-of-subtree t)
  11856. msg "Creating images for subtree...%s"))
  11857. (t
  11858. (if (setq at (org-inside-LaTeX-fragment-p))
  11859. (goto-char (max (point-min) (- (cdr at) 2)))
  11860. (org-back-to-heading))
  11861. (setq beg (point) end (progn (outline-next-heading) (point))
  11862. msg (if at "Creating image...%s"
  11863. "Creating images for entry...%s"))))
  11864. (message msg "")
  11865. (narrow-to-region beg end)
  11866. (goto-char beg)
  11867. (org-format-latex
  11868. (concat "ltxpng/" (file-name-sans-extension
  11869. (file-name-nondirectory
  11870. buffer-file-name)))
  11871. default-directory 'overlays msg at 'forbuffer)
  11872. (message msg "done. Use `C-c C-c' to remove images.")))))
  11873. (defvar org-latex-regexps
  11874. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11875. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11876. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11877. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11878. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11879. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11880. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11881. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11882. "Regular expressions for matching embedded LaTeX.")
  11883. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11884. "Replace LaTeX fragments with links to an image, and produce images."
  11885. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11886. (let* ((prefixnodir (file-name-nondirectory prefix))
  11887. (absprefix (expand-file-name prefix dir))
  11888. (todir (file-name-directory absprefix))
  11889. (opt org-format-latex-options)
  11890. (matchers (plist-get opt :matchers))
  11891. (re-list org-latex-regexps)
  11892. (cnt 0) txt link beg end re e checkdir
  11893. m n block linkfile movefile ov)
  11894. ;; Check if there are old images files with this prefix, and remove them
  11895. (when (file-directory-p todir)
  11896. (mapc 'delete-file
  11897. (directory-files
  11898. todir 'full
  11899. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11900. ;; Check the different regular expressions
  11901. (while (setq e (pop re-list))
  11902. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11903. block (if (nth 3 e) "\n\n" ""))
  11904. (when (member m matchers)
  11905. (goto-char (point-min))
  11906. (while (re-search-forward re nil t)
  11907. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11908. (not (get-text-property (match-beginning n)
  11909. 'org-protected)))
  11910. (setq txt (match-string n)
  11911. beg (match-beginning n) end (match-end n)
  11912. cnt (1+ cnt)
  11913. linkfile (format "%s_%04d.png" prefix cnt)
  11914. movefile (format "%s_%04d.png" absprefix cnt)
  11915. link (concat block "[[file:" linkfile "]]" block))
  11916. (if msg (message msg cnt))
  11917. (goto-char beg)
  11918. (unless checkdir ; make sure the directory exists
  11919. (setq checkdir t)
  11920. (or (file-directory-p todir) (make-directory todir)))
  11921. (org-create-formula-image
  11922. txt movefile opt forbuffer)
  11923. (if overlays
  11924. (progn
  11925. (setq ov (org-make-overlay beg end))
  11926. (if (featurep 'xemacs)
  11927. (progn
  11928. (org-overlay-put ov 'invisible t)
  11929. (org-overlay-put
  11930. ov 'end-glyph
  11931. (make-glyph (vector 'png :file movefile))))
  11932. (org-overlay-put
  11933. ov 'display
  11934. (list 'image :type 'png :file movefile :ascent 'center)))
  11935. (push ov org-latex-fragment-image-overlays)
  11936. (goto-char end))
  11937. (delete-region beg end)
  11938. (insert link))))))))
  11939. ;; This function borrows from Ganesh Swami's latex2png.el
  11940. (defun org-create-formula-image (string tofile options buffer)
  11941. (let* ((tmpdir (if (featurep 'xemacs)
  11942. (temp-directory)
  11943. temporary-file-directory))
  11944. (texfilebase (make-temp-name
  11945. (expand-file-name "orgtex" tmpdir)))
  11946. (texfile (concat texfilebase ".tex"))
  11947. (dvifile (concat texfilebase ".dvi"))
  11948. (pngfile (concat texfilebase ".png"))
  11949. (fnh (if (featurep 'xemacs)
  11950. (font-height (get-face-font 'default))
  11951. (face-attribute 'default :height nil)))
  11952. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11953. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11954. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11955. "Black"))
  11956. (bg (or (plist-get options (if buffer :background :html-background))
  11957. "Transparent")))
  11958. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11959. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11960. (with-temp-file texfile
  11961. (insert org-format-latex-header
  11962. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11963. (let ((dir default-directory))
  11964. (condition-case nil
  11965. (progn
  11966. (cd tmpdir)
  11967. (call-process "latex" nil nil nil texfile))
  11968. (error nil))
  11969. (cd dir))
  11970. (if (not (file-exists-p dvifile))
  11971. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11972. (condition-case nil
  11973. (call-process "dvipng" nil nil nil
  11974. "-E" "-fg" fg "-bg" bg
  11975. "-D" dpi
  11976. ;;"-x" scale "-y" scale
  11977. "-T" "tight"
  11978. "-o" pngfile
  11979. dvifile)
  11980. (error nil))
  11981. (if (not (file-exists-p pngfile))
  11982. (progn (message "Failed to create png file from %s" texfile) nil)
  11983. ;; Use the requested file name and clean up
  11984. (copy-file pngfile tofile 'replace)
  11985. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11986. (delete-file (concat texfilebase e)))
  11987. pngfile))))
  11988. (defun org-dvipng-color (attr)
  11989. "Return an rgb color specification for dvipng."
  11990. (apply 'format "rgb %s %s %s"
  11991. (mapcar 'org-normalize-color
  11992. (color-values (face-attribute 'default attr nil)))))
  11993. (defun org-normalize-color (value)
  11994. "Return string to be used as color value for an RGB component."
  11995. (format "%g" (/ value 65535.0)))
  11996. ;;;; Key bindings
  11997. ;; Make `C-c C-x' a prefix key
  11998. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11999. ;; TAB key with modifiers
  12000. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12001. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12002. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12003. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12004. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12005. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12006. ;; The following line is necessary under Suse GNU/Linux
  12007. (unless (featurep 'xemacs)
  12008. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12009. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12010. (define-key org-mode-map [backtab] 'org-shifttab)
  12011. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12012. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12013. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12014. ;; Cursor keys with modifiers
  12015. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12016. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12017. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12018. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12019. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12020. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12021. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12022. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12023. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12024. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12025. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12026. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12027. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12028. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12029. ;;; Extra keys for tty access.
  12030. ;; We only set them when really needed because otherwise the
  12031. ;; menus don't show the simple keys
  12032. (when (or org-use-extra-keys
  12033. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12034. (not window-system))
  12035. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12036. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12037. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12038. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12039. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12040. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12041. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12042. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12043. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12044. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12045. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12046. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12047. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12048. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12049. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12050. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12051. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12052. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12053. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12054. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12055. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12056. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12057. ;; All the other keys
  12058. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12059. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12060. (if (boundp 'narrow-map)
  12061. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12062. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12063. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12064. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12065. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12066. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12067. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12068. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12069. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12070. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12071. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12072. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12073. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12074. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12075. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12076. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12077. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12078. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12079. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12080. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12081. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12082. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12083. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12084. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12085. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12086. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12087. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12088. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12089. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12090. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12091. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12092. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12093. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12094. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12095. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12096. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12097. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12098. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12099. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12100. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12101. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12102. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12103. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12104. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12105. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12106. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12107. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12108. (org-defkey org-mode-map "\C-m" 'org-return)
  12109. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12110. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12111. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12112. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12113. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12114. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12115. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12116. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12117. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12118. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12119. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12120. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12121. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12122. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12123. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12124. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12125. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12126. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12127. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12128. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12129. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12130. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12131. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12132. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12133. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12134. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12135. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12136. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12137. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12138. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12139. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12140. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12141. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12142. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12143. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12144. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12145. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12146. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12147. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12148. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12149. (when (featurep 'xemacs)
  12150. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12151. (defvar org-self-insert-command-undo-counter 0)
  12152. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12153. (defun org-self-insert-command (N)
  12154. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12155. If the cursor is in a table looking at whitespace, the whitespace is
  12156. overwritten, and the table is not marked as requiring realignment."
  12157. (interactive "p")
  12158. (if (and
  12159. (org-table-p)
  12160. (progn
  12161. ;; check if we blank the field, and if that triggers align
  12162. (and (featurep 'org-table) org-table-auto-blank-field
  12163. (member last-command
  12164. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12165. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12166. ;; got extra space, this field does not determine column width
  12167. (let (org-table-may-need-update) (org-table-blank-field))
  12168. ;; no extra space, this field may determine column width
  12169. (org-table-blank-field)))
  12170. t)
  12171. (eq N 1)
  12172. (looking-at "[^|\n]* |"))
  12173. (let (org-table-may-need-update)
  12174. (goto-char (1- (match-end 0)))
  12175. (delete-backward-char 1)
  12176. (goto-char (match-beginning 0))
  12177. (self-insert-command N))
  12178. (setq org-table-may-need-update t)
  12179. (self-insert-command N)
  12180. (org-fix-tags-on-the-fly)
  12181. (if org-self-insert-cluster-for-undo
  12182. (if (not (eq last-command 'org-self-insert-command))
  12183. (setq org-self-insert-command-undo-counter 1)
  12184. (if (>= org-self-insert-command-undo-counter 20)
  12185. (setq org-self-insert-command-undo-counter 1)
  12186. (and (> org-self-insert-command-undo-counter 0)
  12187. buffer-undo-list
  12188. (not (cadr buffer-undo-list)) ; remove nil entry
  12189. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12190. (setq org-self-insert-command-undo-counter
  12191. (1+ org-self-insert-command-undo-counter)))))))
  12192. (defun org-fix-tags-on-the-fly ()
  12193. (when (and (equal (char-after (point-at-bol)) ?*)
  12194. (org-on-heading-p))
  12195. (org-align-tags-here org-tags-column)))
  12196. (defun org-delete-backward-char (N)
  12197. "Like `delete-backward-char', insert whitespace at field end in tables.
  12198. When deleting backwards, in tables this function will insert whitespace in
  12199. front of the next \"|\" separator, to keep the table aligned. The table will
  12200. still be marked for re-alignment if the field did fill the entire column,
  12201. because, in this case the deletion might narrow the column."
  12202. (interactive "p")
  12203. (if (and (org-table-p)
  12204. (eq N 1)
  12205. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12206. (looking-at ".*?|"))
  12207. (let ((pos (point))
  12208. (noalign (looking-at "[^|\n\r]* |"))
  12209. (c org-table-may-need-update))
  12210. (backward-delete-char N)
  12211. (skip-chars-forward "^|")
  12212. (insert " ")
  12213. (goto-char (1- pos))
  12214. ;; noalign: if there were two spaces at the end, this field
  12215. ;; does not determine the width of the column.
  12216. (if noalign (setq org-table-may-need-update c)))
  12217. (backward-delete-char N)
  12218. (org-fix-tags-on-the-fly)))
  12219. (defun org-delete-char (N)
  12220. "Like `delete-char', but insert whitespace at field end in tables.
  12221. When deleting characters, in tables this function will insert whitespace in
  12222. front of the next \"|\" separator, to keep the table aligned. The table will
  12223. still be marked for re-alignment if the field did fill the entire column,
  12224. because, in this case the deletion might narrow the column."
  12225. (interactive "p")
  12226. (if (and (org-table-p)
  12227. (not (bolp))
  12228. (not (= (char-after) ?|))
  12229. (eq N 1))
  12230. (if (looking-at ".*?|")
  12231. (let ((pos (point))
  12232. (noalign (looking-at "[^|\n\r]* |"))
  12233. (c org-table-may-need-update))
  12234. (replace-match (concat
  12235. (substring (match-string 0) 1 -1)
  12236. " |"))
  12237. (goto-char pos)
  12238. ;; noalign: if there were two spaces at the end, this field
  12239. ;; does not determine the width of the column.
  12240. (if noalign (setq org-table-may-need-update c)))
  12241. (delete-char N))
  12242. (delete-char N)
  12243. (org-fix-tags-on-the-fly)))
  12244. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12245. (put 'org-self-insert-command 'delete-selection t)
  12246. (put 'orgtbl-self-insert-command 'delete-selection t)
  12247. (put 'org-delete-char 'delete-selection 'supersede)
  12248. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12249. (put 'org-yank 'delete-selection 'yank)
  12250. ;; Make `flyspell-mode' delay after some commands
  12251. (put 'org-self-insert-command 'flyspell-delayed t)
  12252. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12253. (put 'org-delete-char 'flyspell-delayed t)
  12254. (put 'org-delete-backward-char 'flyspell-delayed t)
  12255. ;; Make pabbrev-mode expand after org-mode commands
  12256. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12257. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12258. ;; How to do this: Measure non-white length of current string
  12259. ;; If equal to column width, we should realign.
  12260. (defun org-remap (map &rest commands)
  12261. "In MAP, remap the functions given in COMMANDS.
  12262. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12263. (let (new old)
  12264. (while commands
  12265. (setq old (pop commands) new (pop commands))
  12266. (if (fboundp 'command-remapping)
  12267. (org-defkey map (vector 'remap old) new)
  12268. (substitute-key-definition old new map global-map)))))
  12269. (when (eq org-enable-table-editor 'optimized)
  12270. ;; If the user wants maximum table support, we need to hijack
  12271. ;; some standard editing functions
  12272. (org-remap org-mode-map
  12273. 'self-insert-command 'org-self-insert-command
  12274. 'delete-char 'org-delete-char
  12275. 'delete-backward-char 'org-delete-backward-char)
  12276. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12277. (defvar org-ctrl-c-ctrl-c-hook nil
  12278. "Hook for functions attaching themselves to `C-c C-c'.
  12279. This can be used to add additional functionality to the C-c C-c key which
  12280. executes context-dependent commands.
  12281. Each function will be called with no arguments. The function must check
  12282. if the context is appropriate for it to act. If yes, it should do its
  12283. thing and then return a non-nil value. If the context is wrong,
  12284. just do nothing.")
  12285. (defvar org-metaleft-hook nil
  12286. "Hook for functions attaching themselves to `M-left'.
  12287. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12288. (defvar org-metaright-hook nil
  12289. "Hook for functions attaching themselves to `M-right'.
  12290. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12291. (defvar org-metaup-hook nil
  12292. "Hook for functions attaching themselves to `M-up'.
  12293. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12294. (defvar org-metadown-hook nil
  12295. "Hook for functions attaching themselves to `M-down'.
  12296. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12297. (defvar org-shiftmetaleft-hook nil
  12298. "Hook for functions attaching themselves to `M-S-left'.
  12299. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12300. (defvar org-shiftmetaright-hook nil
  12301. "Hook for functions attaching themselves to `M-S-right'.
  12302. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12303. (defvar org-shiftmetaup-hook nil
  12304. "Hook for functions attaching themselves to `M-S-up'.
  12305. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12306. (defvar org-shiftmetadown-hook nil
  12307. "Hook for functions attaching themselves to `M-S-down'.
  12308. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12309. (defvar org-metareturn-hook nil
  12310. "Hook for functions attaching themselves to `M-RET'.
  12311. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12312. (defun org-modifier-cursor-error ()
  12313. "Throw an error, a modified cursor command was applied in wrong context."
  12314. (error "This command is active in special context like tables, headlines or items"))
  12315. (defun org-shiftselect-error ()
  12316. "Throw an error because Shift-Cursor command was applied in wrong context."
  12317. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12318. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12319. (error "This command works only in special context like headlines or timestamps.")))
  12320. (defun org-call-for-shift-select (cmd)
  12321. (let ((this-command-keys-shift-translated t))
  12322. (call-interactively cmd)))
  12323. (defun org-shifttab (&optional arg)
  12324. "Global visibility cycling or move to previous table field.
  12325. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12326. on context.
  12327. See the individual commands for more information."
  12328. (interactive "P")
  12329. (cond
  12330. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12331. ((integerp arg)
  12332. (message "Content view to level: %d" arg)
  12333. (org-content (prefix-numeric-value arg))
  12334. (setq org-cycle-global-status 'overview))
  12335. (t (call-interactively 'org-global-cycle))))
  12336. (defun org-shiftmetaleft ()
  12337. "Promote subtree or delete table column.
  12338. Calls `org-promote-subtree', `org-outdent-item',
  12339. or `org-table-delete-column', depending on context.
  12340. See the individual commands for more information."
  12341. (interactive)
  12342. (cond
  12343. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12344. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12345. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12346. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12347. (t (org-modifier-cursor-error))))
  12348. (defun org-shiftmetaright ()
  12349. "Demote subtree or insert table column.
  12350. Calls `org-demote-subtree', `org-indent-item',
  12351. or `org-table-insert-column', depending on context.
  12352. See the individual commands for more information."
  12353. (interactive)
  12354. (cond
  12355. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12356. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12357. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12358. ((org-at-item-p) (call-interactively 'org-indent-item))
  12359. (t (org-modifier-cursor-error))))
  12360. (defun org-shiftmetaup (&optional arg)
  12361. "Move subtree up or kill table row.
  12362. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12363. `org-move-item-up' depending on context. See the individual commands
  12364. for more information."
  12365. (interactive "P")
  12366. (cond
  12367. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12368. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12369. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12370. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12371. (t (org-modifier-cursor-error))))
  12372. (defun org-shiftmetadown (&optional arg)
  12373. "Move subtree down or insert table row.
  12374. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12375. `org-move-item-down', depending on context. See the individual
  12376. commands for more information."
  12377. (interactive "P")
  12378. (cond
  12379. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12380. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12381. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12382. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12383. (t (org-modifier-cursor-error))))
  12384. (defun org-metaleft (&optional arg)
  12385. "Promote heading or move table column to left.
  12386. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12387. With no specific context, calls the Emacs default `backward-word'.
  12388. See the individual commands for more information."
  12389. (interactive "P")
  12390. (cond
  12391. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12392. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12393. ((or (org-on-heading-p)
  12394. (and (org-region-active-p)
  12395. (save-excursion
  12396. (goto-char (region-beginning))
  12397. (org-on-heading-p))))
  12398. (call-interactively 'org-do-promote))
  12399. ((or (org-at-item-p)
  12400. (and (org-region-active-p)
  12401. (save-excursion
  12402. (goto-char (region-beginning))
  12403. (org-at-item-p))))
  12404. (call-interactively 'org-outdent-item))
  12405. (t (call-interactively 'backward-word))))
  12406. (defun org-metaright (&optional arg)
  12407. "Demote subtree or move table column to right.
  12408. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12409. With no specific context, calls the Emacs default `forward-word'.
  12410. See the individual commands for more information."
  12411. (interactive "P")
  12412. (cond
  12413. ((run-hook-with-args-until-success 'org-metaright-hook))
  12414. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12415. ((or (org-on-heading-p)
  12416. (and (org-region-active-p)
  12417. (save-excursion
  12418. (goto-char (region-beginning))
  12419. (org-on-heading-p))))
  12420. (call-interactively 'org-do-demote))
  12421. ((or (org-at-item-p)
  12422. (and (org-region-active-p)
  12423. (save-excursion
  12424. (goto-char (region-beginning))
  12425. (org-at-item-p))))
  12426. (call-interactively 'org-indent-item))
  12427. (t (call-interactively 'forward-word))))
  12428. (defun org-metaup (&optional arg)
  12429. "Move subtree up or move table row up.
  12430. Calls `org-move-subtree-up' or `org-table-move-row' or
  12431. `org-move-item-up', depending on context. See the individual commands
  12432. for more information."
  12433. (interactive "P")
  12434. (cond
  12435. ((run-hook-with-args-until-success 'org-metaup-hook))
  12436. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12437. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12438. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12439. (t (transpose-lines 1) (beginning-of-line -1))))
  12440. (defun org-metadown (&optional arg)
  12441. "Move subtree down or move table row down.
  12442. Calls `org-move-subtree-down' or `org-table-move-row' or
  12443. `org-move-item-down', depending on context. See the individual
  12444. commands for more information."
  12445. (interactive "P")
  12446. (cond
  12447. ((run-hook-with-args-until-success 'org-metadown-hook))
  12448. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12449. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12450. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12451. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12452. (defun org-shiftup (&optional arg)
  12453. "Increase item in timestamp or increase priority of current headline.
  12454. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12455. depending on context. See the individual commands for more information."
  12456. (interactive "P")
  12457. (cond
  12458. ((and org-support-shift-select (org-region-active-p))
  12459. (org-call-for-shift-select 'previous-line))
  12460. ((org-at-timestamp-p t)
  12461. (call-interactively (if org-edit-timestamp-down-means-later
  12462. 'org-timestamp-down 'org-timestamp-up)))
  12463. ((and (not (eq org-support-shift-select 'always))
  12464. (org-on-heading-p))
  12465. (call-interactively 'org-priority-up))
  12466. ((and (not org-support-shift-select) (org-at-item-p))
  12467. (call-interactively 'org-previous-item))
  12468. ((org-clocktable-try-shift 'up arg))
  12469. (org-support-shift-select
  12470. (org-call-for-shift-select 'previous-line))
  12471. (t (org-shiftselect-error))))
  12472. (defun org-shiftdown (&optional arg)
  12473. "Decrease item in timestamp or decrease priority of current headline.
  12474. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12475. depending on context. See the individual commands for more information."
  12476. (interactive "P")
  12477. (cond
  12478. ((and org-support-shift-select (org-region-active-p))
  12479. (org-call-for-shift-select 'next-line))
  12480. ((org-at-timestamp-p t)
  12481. (call-interactively (if org-edit-timestamp-down-means-later
  12482. 'org-timestamp-up 'org-timestamp-down)))
  12483. ((and (not (eq org-support-shift-select 'always))
  12484. (org-on-heading-p))
  12485. (call-interactively 'org-priority-down))
  12486. ((and (not org-support-shift-select) (org-at-item-p))
  12487. (call-interactively 'org-next-item))
  12488. ((org-clocktable-try-shift 'down arg))
  12489. (org-support-shift-select
  12490. (org-call-for-shift-select 'next-line))
  12491. (t (org-shiftselect-error))))
  12492. (defun org-shiftright (&optional arg)
  12493. "Cycle the thing at point or in the current line, depending on context.
  12494. Depending on context, this does one of the following:
  12495. - switch a timestamp at point one day into the future
  12496. - on a headline, switch to the next TODO keyword.
  12497. - on an item, switch entire list to the next bullet type
  12498. - on a property line, switch to the next allowed value
  12499. - on a clocktable definition line, move time block into the future"
  12500. (interactive "P")
  12501. (cond
  12502. ((and org-support-shift-select (org-region-active-p))
  12503. (org-call-for-shift-select 'forward-char))
  12504. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12505. ((and (not (eq org-support-shift-select 'always))
  12506. (org-on-heading-p))
  12507. (org-call-with-arg 'org-todo 'right))
  12508. ((or (and org-support-shift-select
  12509. (not (eq org-support-shift-select 'always))
  12510. (org-at-item-bullet-p))
  12511. (and (not org-support-shift-select) (org-at-item-p)))
  12512. (org-call-with-arg 'org-cycle-list-bullet nil))
  12513. ((and (not (eq org-support-shift-select 'always))
  12514. (org-at-property-p))
  12515. (call-interactively 'org-property-next-allowed-value))
  12516. ((org-clocktable-try-shift 'right arg))
  12517. (org-support-shift-select
  12518. (org-call-for-shift-select 'forward-char))
  12519. (t (org-shiftselect-error))))
  12520. (defun org-shiftleft (&optional arg)
  12521. "Cycle the thing at point or in the current line, depending on context.
  12522. Depending on context, this does one of the following:
  12523. - switch a timestamp at point one day into the past
  12524. - on a headline, switch to the previous TODO keyword.
  12525. - on an item, switch entire list to the previous bullet type
  12526. - on a property line, switch to the previous allowed value
  12527. - on a clocktable definition line, move time block into the past"
  12528. (interactive "P")
  12529. (cond
  12530. ((and org-support-shift-select (org-region-active-p))
  12531. (org-call-for-shift-select 'backward-char))
  12532. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12533. ((and (not (eq org-support-shift-select 'always))
  12534. (org-on-heading-p))
  12535. (org-call-with-arg 'org-todo 'left))
  12536. ((or (and org-support-shift-select
  12537. (not (eq org-support-shift-select 'always))
  12538. (org-at-item-bullet-p))
  12539. (and (not org-support-shift-select) (org-at-item-p)))
  12540. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12541. ((and (not (eq org-support-shift-select 'always))
  12542. (org-at-property-p))
  12543. (call-interactively 'org-property-previous-allowed-value))
  12544. ((org-clocktable-try-shift 'left arg))
  12545. (org-support-shift-select
  12546. (org-call-for-shift-select 'backward-char))
  12547. (t (org-shiftselect-error))))
  12548. (defun org-shiftcontrolright ()
  12549. "Switch to next TODO set."
  12550. (interactive)
  12551. (cond
  12552. ((and org-support-shift-select (org-region-active-p))
  12553. (org-call-for-shift-select 'forward-word))
  12554. ((and (not (eq org-support-shift-select 'always))
  12555. (org-on-heading-p))
  12556. (org-call-with-arg 'org-todo 'nextset))
  12557. (org-support-shift-select
  12558. (org-call-for-shift-select 'forward-word))
  12559. (t (org-shiftselect-error))))
  12560. (defun org-shiftcontrolleft ()
  12561. "Switch to previous TODO set."
  12562. (interactive)
  12563. (cond
  12564. ((and org-support-shift-select (org-region-active-p))
  12565. (org-call-for-shift-select 'backward-word))
  12566. ((and (not (eq org-support-shift-select 'always))
  12567. (org-on-heading-p))
  12568. (org-call-with-arg 'org-todo 'previousset))
  12569. (org-support-shift-select
  12570. (org-call-for-shift-select 'backward-word))
  12571. (t (org-shiftselect-error))))
  12572. (defun org-ctrl-c-ret ()
  12573. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12574. (interactive)
  12575. (cond
  12576. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12577. (t (call-interactively 'org-insert-heading))))
  12578. (defun org-copy-special ()
  12579. "Copy region in table or copy current subtree.
  12580. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12581. See the individual commands for more information."
  12582. (interactive)
  12583. (call-interactively
  12584. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12585. (defun org-cut-special ()
  12586. "Cut region in table or cut current subtree.
  12587. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12588. See the individual commands for more information."
  12589. (interactive)
  12590. (call-interactively
  12591. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12592. (defun org-paste-special (arg)
  12593. "Paste rectangular region into table, or past subtree relative to level.
  12594. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12595. See the individual commands for more information."
  12596. (interactive "P")
  12597. (if (org-at-table-p)
  12598. (org-table-paste-rectangle)
  12599. (org-paste-subtree arg)))
  12600. (defun org-edit-special ()
  12601. "Call a special editor for the stuff at point.
  12602. When at a table, call the formula editor with `org-table-edit-formulas'.
  12603. When at the first line of an src example, call `org-edit-src-code'.
  12604. When in an #+include line, visit the include file. Otherwise call
  12605. `ffap' to visit the file at point."
  12606. (interactive)
  12607. (cond
  12608. ((org-at-table-p)
  12609. (call-interactively 'org-table-edit-formulas))
  12610. ((save-excursion
  12611. (beginning-of-line 1)
  12612. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12613. (find-file (org-trim (match-string 1))))
  12614. ((org-edit-src-code))
  12615. ((org-edit-fixed-width-region))
  12616. (t (call-interactively 'ffap))))
  12617. (defun org-ctrl-c-ctrl-c (&optional arg)
  12618. "Set tags in headline, or update according to changed information at point.
  12619. This command does many different things, depending on context:
  12620. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12621. this is what we do.
  12622. - If the cursor is in a headline, prompt for tags and insert them
  12623. into the current line, aligned to `org-tags-column'. When called
  12624. with prefix arg, realign all tags in the current buffer.
  12625. - If the cursor is in one of the special #+KEYWORD lines, this
  12626. triggers scanning the buffer for these lines and updating the
  12627. information.
  12628. - If the cursor is inside a table, realign the table. This command
  12629. works even if the automatic table editor has been turned off.
  12630. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12631. the entire table.
  12632. - If the cursor is at a footnote reference or definition, jump to
  12633. the corresponding definition or references, respectively.
  12634. - If the cursor is a the beginning of a dynamic block, update it.
  12635. - If the cursor is inside a table created by the table.el package,
  12636. activate that table.
  12637. - If the current buffer is a remember buffer, close note and file
  12638. it. A prefix argument of 1 files to the default location
  12639. without further interaction. A prefix argument of 2 files to
  12640. the currently clocking task.
  12641. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12642. links in this buffer.
  12643. - If the cursor is on a numbered item in a plain list, renumber the
  12644. ordered list.
  12645. - If the cursor is on a checkbox, toggle it."
  12646. (interactive "P")
  12647. (let ((org-enable-table-editor t))
  12648. (cond
  12649. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12650. org-occur-highlights
  12651. org-latex-fragment-image-overlays)
  12652. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12653. (org-remove-occur-highlights)
  12654. (org-remove-latex-fragment-image-overlays)
  12655. (message "Temporary highlights/overlays removed from current buffer"))
  12656. ((and (local-variable-p 'org-finish-function (current-buffer))
  12657. (fboundp org-finish-function))
  12658. (funcall org-finish-function))
  12659. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12660. ((org-at-property-p)
  12661. (call-interactively 'org-property-action))
  12662. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12663. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12664. ((org-at-table.el-p)
  12665. (require 'table)
  12666. (beginning-of-line 1)
  12667. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12668. (call-interactively 'table-recognize-table))
  12669. ((org-at-table-p)
  12670. (org-table-maybe-eval-formula)
  12671. (if arg
  12672. (call-interactively 'org-table-recalculate)
  12673. (org-table-maybe-recalculate-line))
  12674. (call-interactively 'org-table-align))
  12675. ((or (org-footnote-at-reference-p)
  12676. (org-footnote-at-definition-p))
  12677. (call-interactively 'org-footnote-action))
  12678. ((org-at-item-checkbox-p)
  12679. (call-interactively 'org-toggle-checkbox))
  12680. ((org-at-item-p)
  12681. (if arg
  12682. (call-interactively 'org-toggle-checkbox)
  12683. (call-interactively 'org-maybe-renumber-ordered-list)))
  12684. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12685. ;; Dynamic block
  12686. (beginning-of-line 1)
  12687. (save-excursion (org-update-dblock)))
  12688. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12689. (cond
  12690. ((equal (match-string 1) "TBLFM")
  12691. ;; Recalculate the table before this line
  12692. (save-excursion
  12693. (beginning-of-line 1)
  12694. (skip-chars-backward " \r\n\t")
  12695. (if (org-at-table-p)
  12696. (org-call-with-arg 'org-table-recalculate t))))
  12697. (t
  12698. ; (org-set-regexps-and-options)
  12699. ; (org-restart-font-lock)
  12700. (let ((org-inhibit-startup t)) (org-mode-restart))
  12701. (message "Local setup has been refreshed"))))
  12702. (t (error "C-c C-c can do nothing useful at this location.")))))
  12703. (defun org-mode-restart ()
  12704. "Restart Org-mode, to scan again for special lines.
  12705. Also updates the keyword regular expressions."
  12706. (interactive)
  12707. (org-mode)
  12708. (message "Org-mode restarted"))
  12709. (defun org-kill-note-or-show-branches ()
  12710. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12711. (interactive)
  12712. (if (not org-finish-function)
  12713. (call-interactively 'show-branches)
  12714. (let ((org-note-abort t))
  12715. (funcall org-finish-function))))
  12716. (defun org-return (&optional indent)
  12717. "Goto next table row or insert a newline.
  12718. Calls `org-table-next-row' or `newline', depending on context.
  12719. See the individual commands for more information."
  12720. (interactive)
  12721. (cond
  12722. ((bobp) (if indent (newline-and-indent) (newline)))
  12723. ((org-at-table-p)
  12724. (org-table-justify-field-maybe)
  12725. (call-interactively 'org-table-next-row))
  12726. ((and org-return-follows-link
  12727. (eq (get-text-property (point) 'face) 'org-link))
  12728. (call-interactively 'org-open-at-point))
  12729. ((and (org-at-heading-p)
  12730. (looking-at
  12731. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12732. (org-show-entry)
  12733. (end-of-line 1)
  12734. (newline))
  12735. (t (if indent (newline-and-indent) (newline)))))
  12736. (defun org-return-indent ()
  12737. "Goto next table row or insert a newline and indent.
  12738. Calls `org-table-next-row' or `newline-and-indent', depending on
  12739. context. See the individual commands for more information."
  12740. (interactive)
  12741. (org-return t))
  12742. (defun org-ctrl-c-star ()
  12743. "Compute table, or change heading status of lines.
  12744. Calls `org-table-recalculate' or `org-toggle-heading',
  12745. depending on context."
  12746. (interactive)
  12747. (cond
  12748. ((org-at-table-p)
  12749. (call-interactively 'org-table-recalculate))
  12750. (t
  12751. ;; Convert all lines in region to list items
  12752. (call-interactively 'org-toggle-heading))))
  12753. (defun org-ctrl-c-minus ()
  12754. "Insert separator line in table or modify bullet status of line.
  12755. Also turns a plain line or a region of lines into list items.
  12756. Calls `org-table-insert-hline', `org-toggle-item', or
  12757. `org-cycle-list-bullet', depending on context."
  12758. (interactive)
  12759. (cond
  12760. ((org-at-table-p)
  12761. (call-interactively 'org-table-insert-hline))
  12762. ((org-region-active-p)
  12763. (call-interactively 'org-toggle-item))
  12764. ((org-in-item-p)
  12765. (call-interactively 'org-cycle-list-bullet))
  12766. (t
  12767. (call-interactively 'org-toggle-item))))
  12768. (defun org-toggle-item ()
  12769. "Convert headings or normal lines to items, items to normal lines.
  12770. If there is no active region, only the current line is considered.
  12771. If the first line in the region is a headline, convert all headlines to items.
  12772. If the first line in the region is an item, convert all items to normal lines.
  12773. If the first line is normal text, add an item bullet to each line."
  12774. (interactive)
  12775. (let (l2 l beg end)
  12776. (if (org-region-active-p)
  12777. (setq beg (region-beginning) end (region-end))
  12778. (setq beg (point-at-bol)
  12779. end (min (1+ (point-at-eol)) (point-max))))
  12780. (save-excursion
  12781. (goto-char end)
  12782. (setq l2 (org-current-line))
  12783. (goto-char beg)
  12784. (beginning-of-line 1)
  12785. (setq l (1- (org-current-line)))
  12786. (if (org-at-item-p)
  12787. ;; We already have items, de-itemize
  12788. (while (< (setq l (1+ l)) l2)
  12789. (when (org-at-item-p)
  12790. (goto-char (match-beginning 2))
  12791. (delete-region (match-beginning 2) (match-end 2))
  12792. (and (looking-at "[ \t]+") (replace-match "")))
  12793. (beginning-of-line 2))
  12794. (if (org-on-heading-p)
  12795. ;; Headings, convert to items
  12796. (while (< (setq l (1+ l)) l2)
  12797. (if (looking-at org-outline-regexp)
  12798. (replace-match "- " t t))
  12799. (beginning-of-line 2))
  12800. ;; normal lines, turn them into items
  12801. (while (< (setq l (1+ l)) l2)
  12802. (unless (org-at-item-p)
  12803. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12804. (replace-match "\\1- \\2")))
  12805. (beginning-of-line 2)))))))
  12806. (defun org-toggle-heading (&optional nstars)
  12807. "Convert headings to normal text, or items or text to headings.
  12808. If there is no active region, only the current line is considered.
  12809. If the first line is a heading, remove the stars from all headlines
  12810. in the region.
  12811. If the first line is a plain list item, turn all plain list items into
  12812. headings.
  12813. If the first line is a normal line, turn each and every line in the region
  12814. into a heading.
  12815. When converting a line into a heading, the number of stars is chosen
  12816. such that the lines become children of the current entry. However, when
  12817. a prefix argument is given, its value determines the number of stars to add."
  12818. (interactive "P")
  12819. (let (l2 l itemp beg end)
  12820. (if (org-region-active-p)
  12821. (setq beg (region-beginning) end (region-end))
  12822. (setq beg (point-at-bol)
  12823. end (min (1+ (point-at-eol)) (point-max))))
  12824. (save-excursion
  12825. (goto-char end)
  12826. (setq l2 (org-current-line))
  12827. (goto-char beg)
  12828. (beginning-of-line 1)
  12829. (setq l (1- (org-current-line)))
  12830. (if (org-on-heading-p)
  12831. ;; We already have headlines, de-star them
  12832. (while (< (setq l (1+ l)) l2)
  12833. (when (org-on-heading-p t)
  12834. (and (looking-at outline-regexp) (replace-match "")))
  12835. (beginning-of-line 2))
  12836. (setq itemp (org-at-item-p))
  12837. (let* ((stars
  12838. (if nstars
  12839. (make-string (prefix-numeric-value current-prefix-arg)
  12840. ?*)
  12841. (save-excursion
  12842. (re-search-backward org-complex-heading-regexp nil t)
  12843. (or (match-string 1) "*"))))
  12844. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12845. (rpl (concat stars add-stars " ")))
  12846. (while (< (setq l (1+ l)) l2)
  12847. (if itemp
  12848. (and (org-at-item-p) (replace-match rpl t t))
  12849. (unless (org-on-heading-p)
  12850. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12851. (replace-match (concat rpl (match-string 2))))))
  12852. (beginning-of-line 2)))))))
  12853. (defun org-meta-return (&optional arg)
  12854. "Insert a new heading or wrap a region in a table.
  12855. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12856. See the individual commands for more information."
  12857. (interactive "P")
  12858. (cond
  12859. ((run-hook-with-args-until-success 'org-metareturn-hook))
  12860. ((org-at-table-p)
  12861. (call-interactively 'org-table-wrap-region))
  12862. (t (call-interactively 'org-insert-heading))))
  12863. ;;; Menu entries
  12864. ;; Define the Org-mode menus
  12865. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12866. '("Tbl"
  12867. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12868. ["Next Field" org-cycle (org-at-table-p)]
  12869. ["Previous Field" org-shifttab (org-at-table-p)]
  12870. ["Next Row" org-return (org-at-table-p)]
  12871. "--"
  12872. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12873. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12874. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12875. "--"
  12876. ("Column"
  12877. ["Move Column Left" org-metaleft (org-at-table-p)]
  12878. ["Move Column Right" org-metaright (org-at-table-p)]
  12879. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12880. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12881. ("Row"
  12882. ["Move Row Up" org-metaup (org-at-table-p)]
  12883. ["Move Row Down" org-metadown (org-at-table-p)]
  12884. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12885. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12886. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12887. "--"
  12888. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12889. ("Rectangle"
  12890. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12891. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12892. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12893. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12894. "--"
  12895. ("Calculate"
  12896. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12897. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12898. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12899. "--"
  12900. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12901. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12902. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12903. "--"
  12904. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12905. "--"
  12906. ["Sum Column/Rectangle" org-table-sum
  12907. (or (org-at-table-p) (org-region-active-p))]
  12908. ["Which Column?" org-table-current-column (org-at-table-p)])
  12909. ["Debug Formulas"
  12910. org-table-toggle-formula-debugger
  12911. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12912. ["Show Col/Row Numbers"
  12913. org-table-toggle-coordinate-overlays
  12914. :style toggle
  12915. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12916. "--"
  12917. ["Create" org-table-create (and (not (org-at-table-p))
  12918. org-enable-table-editor)]
  12919. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12920. ["Import from File" org-table-import (not (org-at-table-p))]
  12921. ["Export to File" org-table-export (org-at-table-p)]
  12922. "--"
  12923. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12924. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12925. '("Org"
  12926. ("Show/Hide"
  12927. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12928. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12929. ["Sparse Tree..." org-sparse-tree t]
  12930. ["Reveal Context" org-reveal t]
  12931. ["Show All" show-all t]
  12932. "--"
  12933. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12934. "--"
  12935. ["New Heading" org-insert-heading t]
  12936. ("Navigate Headings"
  12937. ["Up" outline-up-heading t]
  12938. ["Next" outline-next-visible-heading t]
  12939. ["Previous" outline-previous-visible-heading t]
  12940. ["Next Same Level" outline-forward-same-level t]
  12941. ["Previous Same Level" outline-backward-same-level t]
  12942. "--"
  12943. ["Jump" org-goto t])
  12944. ("Edit Structure"
  12945. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12946. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12947. "--"
  12948. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12949. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12950. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12951. "--"
  12952. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12953. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12954. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12955. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12956. "--"
  12957. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12958. "--"
  12959. ["Convert to odd levels" org-convert-to-odd-levels t]
  12960. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12961. ("Editing"
  12962. ["Emphasis..." org-emphasize t]
  12963. ["Edit Source Example" org-edit-special t]
  12964. "--"
  12965. ["Footnote new/jump" org-footnote-action t]
  12966. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12967. ("Archive"
  12968. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12969. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12970. ; :active t :keys "C-u C-c C-x C-a"]
  12971. ["Sparse trees open ARCHIVE trees"
  12972. (setq org-sparse-tree-open-archived-trees
  12973. (not org-sparse-tree-open-archived-trees))
  12974. :style toggle :selected org-sparse-tree-open-archived-trees]
  12975. ["Cycling opens ARCHIVE trees"
  12976. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12977. :style toggle :selected org-cycle-open-archived-trees]
  12978. "--"
  12979. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12980. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12981. ; ["Check and Move Children" (org-archive-subtree '(4))
  12982. ; :active t :keys "C-u C-c C-x C-s"]
  12983. )
  12984. "--"
  12985. ("TODO Lists"
  12986. ["TODO/DONE/-" org-todo t]
  12987. ("Select keyword"
  12988. ["Next keyword" org-shiftright (org-on-heading-p)]
  12989. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12990. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12991. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12992. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12993. ["Show TODO Tree" org-show-todo-tree t]
  12994. ["Global TODO list" org-todo-list t]
  12995. "--"
  12996. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12997. :selected org-enforce-todo-dependencies :style toggle :active t]
  12998. "Settings for tree at point"
  12999. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13000. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13001. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13002. ["Do Children parallel" org-toggle-ordered-property :style radio
  13003. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13004. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13005. "--"
  13006. ["Set Priority" org-priority t]
  13007. ["Priority Up" org-shiftup t]
  13008. ["Priority Down" org-shiftdown t])
  13009. ("TAGS and Properties"
  13010. ["Set Tags" org-set-tags-command t]
  13011. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13012. "--"
  13013. ["Set property" org-set-property t]
  13014. ["Column view of properties" org-columns t]
  13015. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13016. ("Dates and Scheduling"
  13017. ["Timestamp" org-time-stamp t]
  13018. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13019. ("Change Date"
  13020. ["1 Day Later" org-shiftright t]
  13021. ["1 Day Earlier" org-shiftleft t]
  13022. ["1 ... Later" org-shiftup t]
  13023. ["1 ... Earlier" org-shiftdown t])
  13024. ["Compute Time Range" org-evaluate-time-range t]
  13025. ["Schedule Item" org-schedule t]
  13026. ["Deadline" org-deadline t]
  13027. "--"
  13028. ["Custom time format" org-toggle-time-stamp-overlays
  13029. :style radio :selected org-display-custom-times]
  13030. "--"
  13031. ["Goto Calendar" org-goto-calendar t]
  13032. ["Date from Calendar" org-date-from-calendar t]
  13033. "--"
  13034. ["Start/Restart Timer" org-timer-start t]
  13035. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13036. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13037. ["Insert Timer String" org-timer t]
  13038. ["Insert Timer Item" org-timer-item t])
  13039. ("Logging work"
  13040. ["Clock in" org-clock-in t]
  13041. ["Clock out" org-clock-out t]
  13042. ["Clock cancel" org-clock-cancel t]
  13043. ["Goto running clock" org-clock-goto t]
  13044. ["Display times" org-clock-display t]
  13045. ["Create clock table" org-clock-report t]
  13046. "--"
  13047. ["Record DONE time"
  13048. (progn (setq org-log-done (not org-log-done))
  13049. (message "Switching to %s will %s record a timestamp"
  13050. (car org-done-keywords)
  13051. (if org-log-done "automatically" "not")))
  13052. :style toggle :selected org-log-done])
  13053. "--"
  13054. ["Agenda Command..." org-agenda t]
  13055. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13056. ("File List for Agenda")
  13057. ("Special views current file"
  13058. ["TODO Tree" org-show-todo-tree t]
  13059. ["Check Deadlines" org-check-deadlines t]
  13060. ["Timeline" org-timeline t]
  13061. ["Tags Tree" org-tags-sparse-tree t])
  13062. "--"
  13063. ("Hyperlinks"
  13064. ["Store Link (Global)" org-store-link t]
  13065. ["Insert Link" org-insert-link t]
  13066. ["Follow Link" org-open-at-point t]
  13067. "--"
  13068. ["Next link" org-next-link t]
  13069. ["Previous link" org-previous-link t]
  13070. "--"
  13071. ["Descriptive Links"
  13072. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13073. :style radio
  13074. :selected (member '(org-link) buffer-invisibility-spec)]
  13075. ["Literal Links"
  13076. (progn
  13077. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13078. :style radio
  13079. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13080. "--"
  13081. ["Export/Publish..." org-export t]
  13082. ("LaTeX"
  13083. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13084. :selected org-cdlatex-mode]
  13085. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13086. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13087. ["Modify math symbol" org-cdlatex-math-modify
  13088. (org-inside-LaTeX-fragment-p)]
  13089. ["Export LaTeX fragments as images"
  13090. (if (featurep 'org-exp)
  13091. (setq org-export-with-LaTeX-fragments
  13092. (not org-export-with-LaTeX-fragments))
  13093. (require 'org-exp))
  13094. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13095. org-export-with-LaTeX-fragments)])
  13096. "--"
  13097. ("Documentation"
  13098. ["Show Version" org-version t]
  13099. ["Info Documentation" org-info t])
  13100. ("Customize"
  13101. ["Browse Org Group" org-customize t]
  13102. "--"
  13103. ["Expand This Menu" org-create-customize-menu
  13104. (fboundp 'customize-menu-create)])
  13105. "--"
  13106. ("Refresh/Reload"
  13107. ["Refresh setup current buffer" org-mode-restart t]
  13108. ["Reload Org (after update)" org-reload t]
  13109. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13110. ))
  13111. (defun org-info (&optional node)
  13112. "Read documentation for Org-mode in the info system.
  13113. With optional NODE, go directly to that node."
  13114. (interactive)
  13115. (info (format "(org)%s" (or node ""))))
  13116. (defun org-install-agenda-files-menu ()
  13117. (let ((bl (buffer-list)))
  13118. (save-excursion
  13119. (while bl
  13120. (set-buffer (pop bl))
  13121. (if (org-mode-p) (setq bl nil)))
  13122. (when (org-mode-p)
  13123. (easy-menu-change
  13124. '("Org") "File List for Agenda"
  13125. (append
  13126. (list
  13127. ["Edit File List" (org-edit-agenda-file-list) t]
  13128. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13129. ["Remove Current File from List" org-remove-file t]
  13130. ["Cycle through agenda files" org-cycle-agenda-files t]
  13131. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13132. "--")
  13133. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13134. ;;;; Documentation
  13135. ;;;###autoload
  13136. (defun org-require-autoloaded-modules ()
  13137. (interactive)
  13138. (mapc 'require
  13139. '(org-agenda org-archive org-attach org-clock org-colview
  13140. org-exp org-id org-export-latex org-publish
  13141. org-remember org-table org-timer)))
  13142. ;;;###autoload
  13143. (defun org-reload (&optional uncompiled)
  13144. "Reload all org lisp files.
  13145. With prefix arg UNCOMPILED, load the uncompiled versions."
  13146. (interactive "P")
  13147. (require 'find-func)
  13148. (let* ((dir (file-name-directory (find-library-name "org")))
  13149. (files (directory-files dir t "\\.el\\'"))
  13150. (remove-re (concat (if (featurep 'xemacs)
  13151. "org-colview" "org-colview-xemacs")
  13152. "\\'")))
  13153. (setq files (mapcar 'file-name-sans-extension files))
  13154. (setq files (mapcar
  13155. (lambda (x) (if (string-match remove-re x) nil x))
  13156. files))
  13157. (setq files (delq nil files))
  13158. (mapc
  13159. (lambda (f)
  13160. (if (and (not uncompiled)
  13161. (file-exists-p (concat f ".elc")))
  13162. (load (concat f ".elc") nil nil t)
  13163. (load (concat f ".el") nil nil t)))
  13164. files)))
  13165. ;;;###autoload
  13166. (defun org-customize ()
  13167. "Call the customize function with org as argument."
  13168. (interactive)
  13169. (org-load-modules-maybe)
  13170. (org-require-autoloaded-modules)
  13171. (customize-browse 'org))
  13172. (defun org-create-customize-menu ()
  13173. "Create a full customization menu for Org-mode, insert it into the menu."
  13174. (interactive)
  13175. (org-load-modules-maybe)
  13176. (org-require-autoloaded-modules)
  13177. (if (fboundp 'customize-menu-create)
  13178. (progn
  13179. (easy-menu-change
  13180. '("Org") "Customize"
  13181. `(["Browse Org group" org-customize t]
  13182. "--"
  13183. ,(customize-menu-create 'org)
  13184. ["Set" Custom-set t]
  13185. ["Save" Custom-save t]
  13186. ["Reset to Current" Custom-reset-current t]
  13187. ["Reset to Saved" Custom-reset-saved t]
  13188. ["Reset to Standard Settings" Custom-reset-standard t]))
  13189. (message "\"Org\"-menu now contains full customization menu"))
  13190. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13191. ;;;; Miscellaneous stuff
  13192. ;;; Generally useful functions
  13193. (defun org-find-text-property-in-string (prop s)
  13194. "Return the first non-nil value of property PROP in string S."
  13195. (or (get-text-property 0 prop s)
  13196. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13197. prop s)))
  13198. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13199. "Display the given MESSAGE as a warning."
  13200. (if (fboundp 'display-warning)
  13201. (display-warning 'org message
  13202. (if (featurep 'xemacs)
  13203. 'warning
  13204. :warning))
  13205. (let ((buf (get-buffer-create "*Org warnings*")))
  13206. (with-current-buffer buf
  13207. (goto-char (point-max))
  13208. (insert "Warning (Org): " message)
  13209. (unless (bolp)
  13210. (newline)))
  13211. (display-buffer buf)
  13212. (sit-for 0))))
  13213. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13214. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13215. (if (and marker (marker-buffer marker)
  13216. (buffer-live-p (marker-buffer marker)))
  13217. (progn
  13218. (switch-to-buffer (marker-buffer marker))
  13219. (if (or (> marker (point-max)) (< marker (point-min)))
  13220. (widen))
  13221. (goto-char marker)
  13222. (org-show-context 'org-goto))
  13223. (if bookmark
  13224. (bookmark-jump bookmark)
  13225. (error "Cannot find location"))))
  13226. (defun org-quote-csv-field (s)
  13227. "Quote field for inclusion in CSV material."
  13228. (if (string-match "[\",]" s)
  13229. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13230. s))
  13231. (defun org-plist-delete (plist property)
  13232. "Delete PROPERTY from PLIST.
  13233. This is in contrast to merely setting it to 0."
  13234. (let (p)
  13235. (while plist
  13236. (if (not (eq property (car plist)))
  13237. (setq p (plist-put p (car plist) (nth 1 plist))))
  13238. (setq plist (cddr plist)))
  13239. p))
  13240. (defun org-force-self-insert (N)
  13241. "Needed to enforce self-insert under remapping."
  13242. (interactive "p")
  13243. (self-insert-command N))
  13244. (defun org-string-width (s)
  13245. "Compute width of string, ignoring invisible characters.
  13246. This ignores character with invisibility property `org-link', and also
  13247. characters with property `org-cwidth', because these will become invisible
  13248. upon the next fontification round."
  13249. (let (b l)
  13250. (when (or (eq t buffer-invisibility-spec)
  13251. (assq 'org-link buffer-invisibility-spec))
  13252. (while (setq b (text-property-any 0 (length s)
  13253. 'invisible 'org-link s))
  13254. (setq s (concat (substring s 0 b)
  13255. (substring s (or (next-single-property-change
  13256. b 'invisible s) (length s)))))))
  13257. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13258. (setq s (concat (substring s 0 b)
  13259. (substring s (or (next-single-property-change
  13260. b 'org-cwidth s) (length s))))))
  13261. (setq l (string-width s) b -1)
  13262. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13263. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13264. l))
  13265. (defun org-get-indentation (&optional line)
  13266. "Get the indentation of the current line, interpreting tabs.
  13267. When LINE is given, assume it represents a line and compute its indentation."
  13268. (if line
  13269. (if (string-match "^ *" (org-remove-tabs line))
  13270. (match-end 0))
  13271. (save-excursion
  13272. (beginning-of-line 1)
  13273. (skip-chars-forward " \t")
  13274. (current-column))))
  13275. (defun org-remove-tabs (s &optional width)
  13276. "Replace tabulators in S with spaces.
  13277. Assumes that s is a single line, starting in column 0."
  13278. (setq width (or width tab-width))
  13279. (while (string-match "\t" s)
  13280. (setq s (replace-match
  13281. (make-string
  13282. (- (* width (/ (+ (match-beginning 0) width) width))
  13283. (match-beginning 0)) ?\ )
  13284. t t s)))
  13285. s)
  13286. (defun org-fix-indentation (line ind)
  13287. "Fix indentation in LINE.
  13288. IND is a cons cell with target and minimum indentation.
  13289. If the current indentation in LINE is smaller than the minimum,
  13290. leave it alone. If it is larger than ind, set it to the target."
  13291. (let* ((l (org-remove-tabs line))
  13292. (i (org-get-indentation l))
  13293. (i1 (car ind)) (i2 (cdr ind)))
  13294. (if (>= i i2) (setq l (substring line i2)))
  13295. (if (> i1 0)
  13296. (concat (make-string i1 ?\ ) l)
  13297. l)))
  13298. (defun org-base-buffer (buffer)
  13299. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13300. (if (not buffer)
  13301. buffer
  13302. (or (buffer-base-buffer buffer)
  13303. buffer)))
  13304. (defun org-trim (s)
  13305. "Remove whitespace at beginning and end of string."
  13306. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13307. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13308. s)
  13309. (defun org-wrap (string &optional width lines)
  13310. "Wrap string to either a number of lines, or a width in characters.
  13311. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13312. that costs. If there is a word longer than WIDTH, the text is actually
  13313. wrapped to the length of that word.
  13314. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13315. many lines, whatever width that takes.
  13316. The return value is a list of lines, without newlines at the end."
  13317. (let* ((words (org-split-string string "[ \t\n]+"))
  13318. (maxword (apply 'max (mapcar 'org-string-width words)))
  13319. w ll)
  13320. (cond (width
  13321. (org-do-wrap words (max maxword width)))
  13322. (lines
  13323. (setq w maxword)
  13324. (setq ll (org-do-wrap words maxword))
  13325. (if (<= (length ll) lines)
  13326. ll
  13327. (setq ll words)
  13328. (while (> (length ll) lines)
  13329. (setq w (1+ w))
  13330. (setq ll (org-do-wrap words w)))
  13331. ll))
  13332. (t (error "Cannot wrap this")))))
  13333. (defun org-do-wrap (words width)
  13334. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13335. (let (lines line)
  13336. (while words
  13337. (setq line (pop words))
  13338. (while (and words (< (+ (length line) (length (car words))) width))
  13339. (setq line (concat line " " (pop words))))
  13340. (setq lines (push line lines)))
  13341. (nreverse lines)))
  13342. (defun org-split-string (string &optional separators)
  13343. "Splits STRING into substrings at SEPARATORS.
  13344. No empty strings are returned if there are matches at the beginning
  13345. and end of string."
  13346. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13347. (start 0)
  13348. notfirst
  13349. (list nil))
  13350. (while (and (string-match rexp string
  13351. (if (and notfirst
  13352. (= start (match-beginning 0))
  13353. (< start (length string)))
  13354. (1+ start) start))
  13355. (< (match-beginning 0) (length string)))
  13356. (setq notfirst t)
  13357. (or (eq (match-beginning 0) 0)
  13358. (and (eq (match-beginning 0) (match-end 0))
  13359. (eq (match-beginning 0) start))
  13360. (setq list
  13361. (cons (substring string start (match-beginning 0))
  13362. list)))
  13363. (setq start (match-end 0)))
  13364. (or (eq start (length string))
  13365. (setq list
  13366. (cons (substring string start)
  13367. list)))
  13368. (nreverse list)))
  13369. (defun org-context ()
  13370. "Return a list of contexts of the current cursor position.
  13371. If several contexts apply, all are returned.
  13372. Each context entry is a list with a symbol naming the context, and
  13373. two positions indicating start and end of the context. Possible
  13374. contexts are:
  13375. :headline anywhere in a headline
  13376. :headline-stars on the leading stars in a headline
  13377. :todo-keyword on a TODO keyword (including DONE) in a headline
  13378. :tags on the TAGS in a headline
  13379. :priority on the priority cookie in a headline
  13380. :item on the first line of a plain list item
  13381. :item-bullet on the bullet/number of a plain list item
  13382. :checkbox on the checkbox in a plain list item
  13383. :table in an org-mode table
  13384. :table-special on a special filed in a table
  13385. :table-table in a table.el table
  13386. :link on a hyperlink
  13387. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13388. :target on a <<target>>
  13389. :radio-target on a <<<radio-target>>>
  13390. :latex-fragment on a LaTeX fragment
  13391. :latex-preview on a LaTeX fragment with overlayed preview image
  13392. This function expects the position to be visible because it uses font-lock
  13393. faces as a help to recognize the following contexts: :table-special, :link,
  13394. and :keyword."
  13395. (let* ((f (get-text-property (point) 'face))
  13396. (faces (if (listp f) f (list f)))
  13397. (p (point)) clist o)
  13398. ;; First the large context
  13399. (cond
  13400. ((org-on-heading-p t)
  13401. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13402. (when (progn
  13403. (beginning-of-line 1)
  13404. (looking-at org-todo-line-tags-regexp))
  13405. (push (org-point-in-group p 1 :headline-stars) clist)
  13406. (push (org-point-in-group p 2 :todo-keyword) clist)
  13407. (push (org-point-in-group p 4 :tags) clist))
  13408. (goto-char p)
  13409. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13410. (if (looking-at "\\[#[A-Z0-9]\\]")
  13411. (push (org-point-in-group p 0 :priority) clist)))
  13412. ((org-at-item-p)
  13413. (push (org-point-in-group p 2 :item-bullet) clist)
  13414. (push (list :item (point-at-bol)
  13415. (save-excursion (org-end-of-item) (point)))
  13416. clist)
  13417. (and (org-at-item-checkbox-p)
  13418. (push (org-point-in-group p 0 :checkbox) clist)))
  13419. ((org-at-table-p)
  13420. (push (list :table (org-table-begin) (org-table-end)) clist)
  13421. (if (memq 'org-formula faces)
  13422. (push (list :table-special
  13423. (previous-single-property-change p 'face)
  13424. (next-single-property-change p 'face)) clist)))
  13425. ((org-at-table-p 'any)
  13426. (push (list :table-table) clist)))
  13427. (goto-char p)
  13428. ;; Now the small context
  13429. (cond
  13430. ((org-at-timestamp-p)
  13431. (push (org-point-in-group p 0 :timestamp) clist))
  13432. ((memq 'org-link faces)
  13433. (push (list :link
  13434. (previous-single-property-change p 'face)
  13435. (next-single-property-change p 'face)) clist))
  13436. ((memq 'org-special-keyword faces)
  13437. (push (list :keyword
  13438. (previous-single-property-change p 'face)
  13439. (next-single-property-change p 'face)) clist))
  13440. ((org-on-target-p)
  13441. (push (org-point-in-group p 0 :target) clist)
  13442. (goto-char (1- (match-beginning 0)))
  13443. (if (looking-at org-radio-target-regexp)
  13444. (push (org-point-in-group p 0 :radio-target) clist))
  13445. (goto-char p))
  13446. ((setq o (car (delq nil
  13447. (mapcar
  13448. (lambda (x)
  13449. (if (memq x org-latex-fragment-image-overlays) x))
  13450. (org-overlays-at (point))))))
  13451. (push (list :latex-fragment
  13452. (org-overlay-start o) (org-overlay-end o)) clist)
  13453. (push (list :latex-preview
  13454. (org-overlay-start o) (org-overlay-end o)) clist))
  13455. ((org-inside-LaTeX-fragment-p)
  13456. ;; FIXME: positions wrong.
  13457. (push (list :latex-fragment (point) (point)) clist)))
  13458. (setq clist (nreverse (delq nil clist)))
  13459. clist))
  13460. ;; FIXME: Compare with at-regexp-p Do we need both?
  13461. (defun org-in-regexp (re &optional nlines visually)
  13462. "Check if point is inside a match of regexp.
  13463. Normally only the current line is checked, but you can include NLINES extra
  13464. lines both before and after point into the search.
  13465. If VISUALLY is set, require that the cursor is not after the match but
  13466. really on, so that the block visually is on the match."
  13467. (catch 'exit
  13468. (let ((pos (point))
  13469. (eol (point-at-eol (+ 1 (or nlines 0))))
  13470. (inc (if visually 1 0)))
  13471. (save-excursion
  13472. (beginning-of-line (- 1 (or nlines 0)))
  13473. (while (re-search-forward re eol t)
  13474. (if (and (<= (match-beginning 0) pos)
  13475. (>= (+ inc (match-end 0)) pos))
  13476. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13477. (defun org-at-regexp-p (regexp)
  13478. "Is point inside a match of REGEXP in the current line?"
  13479. (catch 'exit
  13480. (save-excursion
  13481. (let ((pos (point)) (end (point-at-eol)))
  13482. (beginning-of-line 1)
  13483. (while (re-search-forward regexp end t)
  13484. (if (and (<= (match-beginning 0) pos)
  13485. (>= (match-end 0) pos))
  13486. (throw 'exit t)))
  13487. nil))))
  13488. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13489. "Call `multi-occur' with buffers for all agenda files."
  13490. (interactive "sOrg-files matching: \np")
  13491. (let* ((files (org-agenda-files))
  13492. (tnames (mapcar 'file-truename files))
  13493. (extra org-agenda-text-search-extra-files)
  13494. f)
  13495. (when (eq (car extra) 'agenda-archives)
  13496. (setq extra (cdr extra))
  13497. (setq files (org-add-archive-files files)))
  13498. (while (setq f (pop extra))
  13499. (unless (member (file-truename f) tnames)
  13500. (add-to-list 'files f 'append)
  13501. (add-to-list 'tnames (file-truename f) 'append)))
  13502. (multi-occur
  13503. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13504. regexp)))
  13505. (if (boundp 'occur-mode-find-occurrence-hook)
  13506. ;; Emacs 23
  13507. (add-hook 'occur-mode-find-occurrence-hook
  13508. (lambda ()
  13509. (when (org-mode-p)
  13510. (org-reveal))))
  13511. ;; Emacs 22
  13512. (defadvice occur-mode-goto-occurrence
  13513. (after org-occur-reveal activate)
  13514. (and (org-mode-p) (org-reveal)))
  13515. (defadvice occur-mode-goto-occurrence-other-window
  13516. (after org-occur-reveal activate)
  13517. (and (org-mode-p) (org-reveal)))
  13518. (defadvice occur-mode-display-occurrence
  13519. (after org-occur-reveal activate)
  13520. (when (org-mode-p)
  13521. (let ((pos (occur-mode-find-occurrence)))
  13522. (with-current-buffer (marker-buffer pos)
  13523. (save-excursion
  13524. (goto-char pos)
  13525. (org-reveal)))))))
  13526. (defun org-uniquify (list)
  13527. "Remove duplicate elements from LIST."
  13528. (let (res)
  13529. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13530. res))
  13531. (defun org-delete-all (elts list)
  13532. "Remove all elements in ELTS from LIST."
  13533. (while elts
  13534. (setq list (delete (pop elts) list)))
  13535. list)
  13536. (defun org-back-over-empty-lines ()
  13537. "Move backwards over whitespace, to the beginning of the first empty line.
  13538. Returns the number of empty lines passed."
  13539. (let ((pos (point)))
  13540. (skip-chars-backward " \t\n\r")
  13541. (beginning-of-line 2)
  13542. (goto-char (min (point) pos))
  13543. (count-lines (point) pos)))
  13544. (defun org-skip-whitespace ()
  13545. (skip-chars-forward " \t\n\r"))
  13546. (defun org-point-in-group (point group &optional context)
  13547. "Check if POINT is in match-group GROUP.
  13548. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13549. match. If the match group does ot exist or point is not inside it,
  13550. return nil."
  13551. (and (match-beginning group)
  13552. (>= point (match-beginning group))
  13553. (<= point (match-end group))
  13554. (if context
  13555. (list context (match-beginning group) (match-end group))
  13556. t)))
  13557. (defun org-switch-to-buffer-other-window (&rest args)
  13558. "Switch to buffer in a second window on the current frame.
  13559. In particular, do not allow pop-up frames."
  13560. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13561. special-display-function)
  13562. (apply 'switch-to-buffer-other-window args)))
  13563. (defun org-combine-plists (&rest plists)
  13564. "Create a single property list from all plists in PLISTS.
  13565. The process starts by copying the first list, and then setting properties
  13566. from the other lists. Settings in the last list are the most significant
  13567. ones and overrule settings in the other lists."
  13568. (let ((rtn (copy-sequence (pop plists)))
  13569. p v ls)
  13570. (while plists
  13571. (setq ls (pop plists))
  13572. (while ls
  13573. (setq p (pop ls) v (pop ls))
  13574. (setq rtn (plist-put rtn p v))))
  13575. rtn))
  13576. (defun org-move-line-down (arg)
  13577. "Move the current line down. With prefix argument, move it past ARG lines."
  13578. (interactive "p")
  13579. (let ((col (current-column))
  13580. beg end pos)
  13581. (beginning-of-line 1) (setq beg (point))
  13582. (beginning-of-line 2) (setq end (point))
  13583. (beginning-of-line (+ 1 arg))
  13584. (setq pos (move-marker (make-marker) (point)))
  13585. (insert (delete-and-extract-region beg end))
  13586. (goto-char pos)
  13587. (org-move-to-column col)))
  13588. (defun org-move-line-up (arg)
  13589. "Move the current line up. With prefix argument, move it past ARG lines."
  13590. (interactive "p")
  13591. (let ((col (current-column))
  13592. beg end pos)
  13593. (beginning-of-line 1) (setq beg (point))
  13594. (beginning-of-line 2) (setq end (point))
  13595. (beginning-of-line (- arg))
  13596. (setq pos (move-marker (make-marker) (point)))
  13597. (insert (delete-and-extract-region beg end))
  13598. (goto-char pos)
  13599. (org-move-to-column col)))
  13600. (defun org-replace-escapes (string table)
  13601. "Replace %-escapes in STRING with values in TABLE.
  13602. TABLE is an association list with keys like \"%a\" and string values.
  13603. The sequences in STRING may contain normal field width and padding information,
  13604. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13605. so values can contain further %-escapes if they are define later in TABLE."
  13606. (let ((case-fold-search nil)
  13607. e re rpl)
  13608. (while (setq e (pop table))
  13609. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13610. (while (string-match re string)
  13611. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13612. (cdr e)))
  13613. (setq string (replace-match rpl t t string))))
  13614. string))
  13615. (defun org-sublist (list start end)
  13616. "Return a section of LIST, from START to END.
  13617. Counting starts at 1."
  13618. (let (rtn (c start))
  13619. (setq list (nthcdr (1- start) list))
  13620. (while (and list (<= c end))
  13621. (push (pop list) rtn)
  13622. (setq c (1+ c)))
  13623. (nreverse rtn)))
  13624. (defun org-find-base-buffer-visiting (file)
  13625. "Like `find-buffer-visiting' but alway return the base buffer and
  13626. not an indirect buffer."
  13627. (let ((buf (find-buffer-visiting file)))
  13628. (if buf
  13629. (or (buffer-base-buffer buf) buf)
  13630. nil)))
  13631. (defun org-image-file-name-regexp (&optional extensions)
  13632. "Return regexp matching the file names of images.
  13633. If EXTENSIONS is given, only match these."
  13634. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13635. (image-file-name-regexp)
  13636. (let ((image-file-name-extensions
  13637. (or extensions
  13638. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13639. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13640. (concat "\\."
  13641. (regexp-opt (nconc (mapcar 'upcase
  13642. image-file-name-extensions)
  13643. image-file-name-extensions)
  13644. t)
  13645. "\\'"))))
  13646. (defun org-file-image-p (file &optional extensions)
  13647. "Return non-nil if FILE is an image."
  13648. (save-match-data
  13649. (string-match (org-image-file-name-regexp extensions) file)))
  13650. (defun org-get-cursor-date ()
  13651. "Return the date at cursor in as a time.
  13652. This works in the calendar and in the agenda, anywhere else it just
  13653. returns the current time."
  13654. (let (date day defd)
  13655. (cond
  13656. ((eq major-mode 'calendar-mode)
  13657. (setq date (calendar-cursor-to-date)
  13658. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13659. ((eq major-mode 'org-agenda-mode)
  13660. (setq day (get-text-property (point) 'day))
  13661. (if day
  13662. (setq date (calendar-gregorian-from-absolute day)
  13663. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13664. (nth 2 date))))))
  13665. (or defd (current-time))))
  13666. (defvar org-agenda-action-marker (make-marker)
  13667. "Marker pointing to the entry for the next agenda action.")
  13668. (defun org-mark-entry-for-agenda-action ()
  13669. "Mark the current entry as target of an agenda action.
  13670. Agenda actions are actions executed from the agenda with the key `k',
  13671. which make use of the date at the cursor."
  13672. (interactive)
  13673. (move-marker org-agenda-action-marker
  13674. (save-excursion (org-back-to-heading t) (point))
  13675. (current-buffer))
  13676. (message
  13677. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13678. ;;; Paragraph filling stuff.
  13679. ;; We want this to be just right, so use the full arsenal.
  13680. (defun org-indent-line-function ()
  13681. "Indent line like previous, but further if previous was headline or item."
  13682. (interactive)
  13683. (let* ((pos (point))
  13684. (itemp (org-at-item-p))
  13685. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13686. column bpos bcol tpos tcol bullet btype bullet-type)
  13687. ;; Find the previous relevant line
  13688. (beginning-of-line 1)
  13689. (cond
  13690. ((looking-at "#") (setq column 0))
  13691. ((looking-at "\\*+ ") (setq column 0))
  13692. ((and (looking-at "[ \t]*:END:")
  13693. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13694. (save-excursion
  13695. (goto-char (1- (match-beginning 1)))
  13696. (setq column (current-column))))
  13697. (t
  13698. (beginning-of-line 0)
  13699. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13700. (not (looking-at "[ \t]*:END:"))
  13701. (not (looking-at org-drawer-regexp)))
  13702. (beginning-of-line 0))
  13703. (cond
  13704. ((looking-at "\\*+[ \t]+")
  13705. (if (not org-adapt-indentation)
  13706. (setq column 0)
  13707. (goto-char (match-end 0))
  13708. (setq column (current-column))))
  13709. ((looking-at org-drawer-regexp)
  13710. (goto-char (1- (match-beginning 1)))
  13711. (setq column (current-column)))
  13712. ((looking-at "\\([ \t]*\\):END:")
  13713. (goto-char (match-end 1))
  13714. (setq column (current-column)))
  13715. ((org-in-item-p)
  13716. (org-beginning-of-item)
  13717. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13718. (setq bpos (match-beginning 1) tpos (match-end 0)
  13719. bcol (progn (goto-char bpos) (current-column))
  13720. tcol (progn (goto-char tpos) (current-column))
  13721. bullet (match-string 1)
  13722. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13723. (if (> tcol (+ bcol org-description-max-indent))
  13724. (setq tcol (+ bcol 5)))
  13725. (if (not itemp)
  13726. (setq column tcol)
  13727. (goto-char pos)
  13728. (beginning-of-line 1)
  13729. (if (looking-at "\\S-")
  13730. (progn
  13731. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13732. (setq bullet (match-string 1)
  13733. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13734. (setq column (if (equal btype bullet-type) bcol tcol)))
  13735. (setq column (org-get-indentation)))))
  13736. (t (setq column (org-get-indentation))))))
  13737. (goto-char pos)
  13738. (if (<= (current-column) (current-indentation))
  13739. (org-indent-line-to column)
  13740. (save-excursion (org-indent-line-to column)))
  13741. (setq column (current-column))
  13742. (beginning-of-line 1)
  13743. (if (looking-at
  13744. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13745. (replace-match (concat "\\1" (format org-property-format
  13746. (match-string 2) (match-string 3)))
  13747. t nil))
  13748. (org-move-to-column column)))
  13749. (defun org-set-autofill-regexps ()
  13750. (interactive)
  13751. ;; In the paragraph separator we include headlines, because filling
  13752. ;; text in a line directly attached to a headline would otherwise
  13753. ;; fill the headline as well.
  13754. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13755. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13756. ;; The paragraph starter includes hand-formatted lists.
  13757. (org-set-local 'paragraph-start
  13758. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13759. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13760. ;; But only if the user has not turned off tables or fixed-width regions
  13761. (org-set-local
  13762. 'auto-fill-inhibit-regexp
  13763. (concat "\\*+ \\|#\\+"
  13764. "\\|[ \t]*" org-keyword-time-regexp
  13765. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13766. (concat
  13767. "\\|[ \t]*["
  13768. (if org-enable-table-editor "|" "")
  13769. (if org-enable-fixed-width-editor ":" "")
  13770. "]"))))
  13771. ;; We use our own fill-paragraph function, to make sure that tables
  13772. ;; and fixed-width regions are not wrapped. That function will pass
  13773. ;; through to `fill-paragraph' when appropriate.
  13774. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13775. ; Adaptive filling: To get full control, first make sure that
  13776. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13777. (org-set-local 'adaptive-fill-regexp "\000")
  13778. (org-set-local 'adaptive-fill-function
  13779. 'org-adaptive-fill-function)
  13780. (org-set-local
  13781. 'align-mode-rules-list
  13782. '((org-in-buffer-settings
  13783. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13784. (modes . '(org-mode))))))
  13785. (defun org-fill-paragraph (&optional justify)
  13786. "Re-align a table, pass through to fill-paragraph if no table."
  13787. (let ((table-p (org-at-table-p))
  13788. (table.el-p (org-at-table.el-p)))
  13789. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13790. (save-excursion (goto-char (point-at-bol))
  13791. (looking-at outline-regexp)))
  13792. t) ; skip headlines
  13793. (table.el-p t) ; skip table.el tables
  13794. (table-p (org-table-align) t) ; align org-mode tables
  13795. (t nil)))) ; call paragraph-fill
  13796. ;; For reference, this is the default value of adaptive-fill-regexp
  13797. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13798. (defun org-adaptive-fill-function ()
  13799. "Return a fill prefix for org-mode files.
  13800. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13801. work correctly."
  13802. (cond ((looking-at "#[ \t]+")
  13803. (match-string 0))
  13804. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13805. (save-excursion
  13806. (if (> (match-end 1) (+ (match-beginning 1)
  13807. org-description-max-indent))
  13808. (goto-char (+ (match-beginning 1) 5))
  13809. (goto-char (match-end 0)))
  13810. (make-string (current-column) ?\ )))
  13811. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13812. (save-excursion
  13813. (goto-char (match-end 0))
  13814. (make-string (current-column) ?\ )))
  13815. (t nil)))
  13816. ;;; Other stuff.
  13817. (defun org-toggle-fixed-width-section (arg)
  13818. "Toggle the fixed-width export.
  13819. If there is no active region, the QUOTE keyword at the current headline is
  13820. inserted or removed. When present, it causes the text between this headline
  13821. and the next to be exported as fixed-width text, and unmodified.
  13822. If there is an active region, this command adds or removes a colon as the
  13823. first character of this line. If the first character of a line is a colon,
  13824. this line is also exported in fixed-width font."
  13825. (interactive "P")
  13826. (let* ((cc 0)
  13827. (regionp (org-region-active-p))
  13828. (beg (if regionp (region-beginning) (point)))
  13829. (end (if regionp (region-end)))
  13830. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13831. (case-fold-search nil)
  13832. (re "[ \t]*\\(:\\)")
  13833. off)
  13834. (if regionp
  13835. (save-excursion
  13836. (goto-char beg)
  13837. (setq cc (current-column))
  13838. (beginning-of-line 1)
  13839. (setq off (looking-at re))
  13840. (while (> nlines 0)
  13841. (setq nlines (1- nlines))
  13842. (beginning-of-line 1)
  13843. (cond
  13844. (arg
  13845. (org-move-to-column cc t)
  13846. (insert ":\n")
  13847. (forward-line -1))
  13848. ((and off (looking-at re))
  13849. (replace-match "" t t nil 1))
  13850. ((not off) (org-move-to-column cc t) (insert ":")))
  13851. (forward-line 1)))
  13852. (save-excursion
  13853. (org-back-to-heading)
  13854. (if (looking-at (concat outline-regexp
  13855. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13856. (replace-match "" t t nil 1)
  13857. (if (looking-at outline-regexp)
  13858. (progn
  13859. (goto-char (match-end 0))
  13860. (insert org-quote-string " "))))))))
  13861. ;;;; Functions extending outline functionality
  13862. (defun org-beginning-of-line (&optional arg)
  13863. "Go to the beginning of the current line. If that is invisible, continue
  13864. to a visible line beginning. This makes the function of C-a more intuitive.
  13865. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13866. first attempt, and only move to after the tags when the cursor is already
  13867. beyond the end of the headline."
  13868. (interactive "P")
  13869. (let ((pos (point))
  13870. (special (if (consp org-special-ctrl-a/e)
  13871. (car org-special-ctrl-a/e)
  13872. org-special-ctrl-a/e))
  13873. refpos)
  13874. (beginning-of-line 1)
  13875. (if (and arg (fboundp 'move-beginning-of-line))
  13876. (call-interactively 'move-beginning-of-line)
  13877. (if (bobp)
  13878. nil
  13879. (backward-char 1)
  13880. (if (org-invisible-p)
  13881. (while (and (not (bobp)) (org-invisible-p))
  13882. (backward-char 1)
  13883. (beginning-of-line 1))
  13884. (forward-char 1))))
  13885. (when special
  13886. (cond
  13887. ((and (looking-at org-complex-heading-regexp)
  13888. (= (char-after (match-end 1)) ?\ ))
  13889. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13890. (point-at-eol)))
  13891. (goto-char
  13892. (if (eq special t)
  13893. (cond ((> pos refpos) refpos)
  13894. ((= pos (point)) refpos)
  13895. (t (point)))
  13896. (cond ((> pos (point)) (point))
  13897. ((not (eq last-command this-command)) (point))
  13898. (t refpos)))))
  13899. ((org-at-item-p)
  13900. (goto-char
  13901. (if (eq special t)
  13902. (cond ((> pos (match-end 4)) (match-end 4))
  13903. ((= pos (point)) (match-end 4))
  13904. (t (point)))
  13905. (cond ((> pos (point)) (point))
  13906. ((not (eq last-command this-command)) (point))
  13907. (t (match-end 4))))))))
  13908. (org-no-warnings
  13909. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13910. (defun org-end-of-line (&optional arg)
  13911. "Go to the end of the line.
  13912. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13913. first attempt, and only move to after the tags when the cursor is already
  13914. beyond the end of the headline."
  13915. (interactive "P")
  13916. (let ((special (if (consp org-special-ctrl-a/e)
  13917. (cdr org-special-ctrl-a/e)
  13918. org-special-ctrl-a/e)))
  13919. (if (or (not special)
  13920. (not (org-on-heading-p))
  13921. arg)
  13922. (call-interactively (if (fboundp 'move-end-of-line)
  13923. 'move-end-of-line
  13924. 'end-of-line))
  13925. (let ((pos (point)))
  13926. (beginning-of-line 1)
  13927. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13928. (if (eq special t)
  13929. (if (or (< pos (match-beginning 1))
  13930. (= pos (match-end 0)))
  13931. (goto-char (match-beginning 1))
  13932. (goto-char (match-end 0)))
  13933. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13934. (goto-char (match-end 0))
  13935. (goto-char (match-beginning 1))))
  13936. (call-interactively (if (fboundp 'move-end-of-line)
  13937. 'move-end-of-line
  13938. 'end-of-line)))))
  13939. (org-no-warnings
  13940. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13941. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13942. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13943. (defun org-kill-line (&optional arg)
  13944. "Kill line, to tags or end of line."
  13945. (interactive "P")
  13946. (cond
  13947. ((or (not org-special-ctrl-k)
  13948. (bolp)
  13949. (not (org-on-heading-p)))
  13950. (call-interactively 'kill-line))
  13951. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13952. (kill-region (point) (match-beginning 1))
  13953. (org-set-tags nil t))
  13954. (t (kill-region (point) (point-at-eol)))))
  13955. (define-key org-mode-map "\C-k" 'org-kill-line)
  13956. (defun org-yank (&optional arg)
  13957. "Yank. If the kill is a subtree, treat it specially.
  13958. This command will look at the current kill and check if is a single
  13959. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13960. cursor is at the beginning of a line or after the stars of a currently
  13961. empty headline, then the yank is handled specially. How exactly depends
  13962. on the value of the following variables, both set by default.
  13963. org-yank-folded-subtrees
  13964. When set, the subtree(s) will be folded after insertion, but only
  13965. if doing so would now swallow text after the yanked text.
  13966. org-yank-adjusted-subtrees
  13967. When set, the subtree will be promoted or demoted in order to
  13968. fit into the local outline tree structure, which means that the level
  13969. will be adjusted so that it becomes the smaller one of the two
  13970. *visible* surrounding headings.
  13971. Any prefix to this command will cause `yank' to be called directly with
  13972. no special treatment. In particular, a simple `C-u' prefix will just
  13973. plainly yank the text as it is.
  13974. \[1] Basically, the test checks if the first non-white line is a heading
  13975. and if there are no other headings with fewer stars."
  13976. (interactive "P")
  13977. (setq this-command 'yank)
  13978. (if arg
  13979. (call-interactively 'yank)
  13980. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13981. (and (org-kill-is-subtree-p)
  13982. (or (bolp)
  13983. (and (looking-at "[ \t]*$")
  13984. (string-match
  13985. "\\`\\*+\\'"
  13986. (buffer-substring (point-at-bol) (point)))))))
  13987. swallowp)
  13988. (cond
  13989. ((and subtreep org-yank-folded-subtrees)
  13990. (let ((beg (point))
  13991. end)
  13992. (if (and subtreep org-yank-adjusted-subtrees)
  13993. (org-paste-subtree nil nil 'for-yank)
  13994. (call-interactively 'yank))
  13995. (setq end (point))
  13996. (goto-char beg)
  13997. (when (and (bolp) subtreep
  13998. (not (setq swallowp
  13999. (org-yank-folding-would-swallow-text beg end))))
  14000. (or (looking-at outline-regexp)
  14001. (re-search-forward (concat "^" outline-regexp) end t))
  14002. (while (and (< (point) end) (looking-at outline-regexp))
  14003. (hide-subtree)
  14004. (org-cycle-show-empty-lines 'folded)
  14005. (condition-case nil
  14006. (outline-forward-same-level 1)
  14007. (error (goto-char end)))))
  14008. (when swallowp
  14009. (message
  14010. "Yanked text not folded because that would swallow text"))
  14011. (goto-char end)
  14012. (skip-chars-forward " \t\n\r")
  14013. (beginning-of-line 1)
  14014. (push-mark beg 'nomsg)))
  14015. ((and subtreep org-yank-adjusted-subtrees)
  14016. (let ((beg (point-at-bol)))
  14017. (org-paste-subtree nil nil 'for-yank)
  14018. (push-mark beg 'nomsg)))
  14019. (t
  14020. (call-interactively 'yank))))))
  14021. (defun org-yank-folding-would-swallow-text (beg end)
  14022. "Would hide-subtree at BEG swallow any text after END?"
  14023. (let (level)
  14024. (save-excursion
  14025. (goto-char beg)
  14026. (when (or (looking-at outline-regexp)
  14027. (re-search-forward (concat "^" outline-regexp) end t))
  14028. (setq level (org-outline-level)))
  14029. (goto-char end)
  14030. (skip-chars-forward " \t\r\n\v\f")
  14031. (if (or (eobp)
  14032. (and (bolp) (looking-at org-outline-regexp)
  14033. (<= (org-outline-level) level)))
  14034. nil ; Nothing would be swallowed
  14035. t)))) ; something would swallow
  14036. (define-key org-mode-map "\C-y" 'org-yank)
  14037. (defun org-invisible-p ()
  14038. "Check if point is at a character currently not visible."
  14039. ;; Early versions of noutline don't have `outline-invisible-p'.
  14040. (if (fboundp 'outline-invisible-p)
  14041. (outline-invisible-p)
  14042. (get-char-property (point) 'invisible)))
  14043. (defun org-invisible-p2 ()
  14044. "Check if point is at a character currently not visible."
  14045. (save-excursion
  14046. (if (and (eolp) (not (bobp))) (backward-char 1))
  14047. ;; Early versions of noutline don't have `outline-invisible-p'.
  14048. (if (fboundp 'outline-invisible-p)
  14049. (outline-invisible-p)
  14050. (get-char-property (point) 'invisible))))
  14051. (defun org-back-to-heading (&optional invisible-ok)
  14052. "Call `outline-back-to-heading', but provide a better error message."
  14053. (condition-case nil
  14054. (outline-back-to-heading invisible-ok)
  14055. (error (error "Before first headline at position %d in buffer %s"
  14056. (point) (current-buffer)))))
  14057. (defun org-before-first-heading-p ()
  14058. "Before first heading?"
  14059. (save-excursion
  14060. (null (re-search-backward "^\\*+ " nil t))))
  14061. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14062. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14063. (defun org-at-heading-or-item-p ()
  14064. (or (org-on-heading-p) (org-at-item-p)))
  14065. (defun org-on-target-p ()
  14066. (or (org-in-regexp org-radio-target-regexp)
  14067. (org-in-regexp org-target-regexp)))
  14068. (defun org-up-heading-all (arg)
  14069. "Move to the heading line of which the present line is a subheading.
  14070. This function considers both visible and invisible heading lines.
  14071. With argument, move up ARG levels."
  14072. (if (fboundp 'outline-up-heading-all)
  14073. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14074. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14075. (defun org-up-heading-safe ()
  14076. "Move to the heading line of which the present line is a subheading.
  14077. This version will not throw an error. It will return the level of the
  14078. headline found, or nil if no higher level is found."
  14079. (let (start-level re)
  14080. (org-back-to-heading t)
  14081. (setq start-level (funcall outline-level))
  14082. (if (equal start-level 1)
  14083. nil
  14084. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14085. (if (re-search-backward re nil t)
  14086. (funcall outline-level)))))
  14087. (defun org-first-sibling-p ()
  14088. "Is this heading the first child of its parents?"
  14089. (interactive)
  14090. (let ((re (concat "^" outline-regexp))
  14091. level l)
  14092. (unless (org-at-heading-p t)
  14093. (error "Not at a heading"))
  14094. (setq level (funcall outline-level))
  14095. (save-excursion
  14096. (if (not (re-search-backward re nil t))
  14097. t
  14098. (setq l (funcall outline-level))
  14099. (< l level)))))
  14100. (defun org-goto-sibling (&optional previous)
  14101. "Goto the next sibling, even if it is invisible.
  14102. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14103. when a sibling was found. When none is found, return nil and don't
  14104. move point."
  14105. (let ((fun (if previous 're-search-backward 're-search-forward))
  14106. (pos (point))
  14107. (re (concat "^" outline-regexp))
  14108. level l)
  14109. (when (condition-case nil (org-back-to-heading t) (error nil))
  14110. (setq level (funcall outline-level))
  14111. (catch 'exit
  14112. (or previous (forward-char 1))
  14113. (while (funcall fun re nil t)
  14114. (setq l (funcall outline-level))
  14115. (when (< l level) (goto-char pos) (throw 'exit nil))
  14116. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14117. (goto-char pos)
  14118. nil))))
  14119. (defun org-show-siblings ()
  14120. "Show all siblings of the current headline."
  14121. (save-excursion
  14122. (while (org-goto-sibling) (org-flag-heading nil)))
  14123. (save-excursion
  14124. (while (org-goto-sibling 'previous)
  14125. (org-flag-heading nil))))
  14126. (defun org-show-hidden-entry ()
  14127. "Show an entry where even the heading is hidden."
  14128. (save-excursion
  14129. (org-show-entry)))
  14130. (defun org-flag-heading (flag &optional entry)
  14131. "Flag the current heading. FLAG non-nil means make invisible.
  14132. When ENTRY is non-nil, show the entire entry."
  14133. (save-excursion
  14134. (org-back-to-heading t)
  14135. ;; Check if we should show the entire entry
  14136. (if entry
  14137. (progn
  14138. (org-show-entry)
  14139. (save-excursion
  14140. (and (outline-next-heading)
  14141. (org-flag-heading nil))))
  14142. (outline-flag-region (max (point-min) (1- (point)))
  14143. (save-excursion (outline-end-of-heading) (point))
  14144. flag))))
  14145. (defun org-forward-same-level (arg)
  14146. "Move forward to the ARG'th subheading at same level as this one.
  14147. Stop at the first and last subheadings of a superior heading.
  14148. This is like outline-forward-same-level, but invisible headings are ok."
  14149. (interactive "p")
  14150. (org-back-to-heading t)
  14151. (while (> arg 0)
  14152. (let ((point-to-move-to (save-excursion
  14153. (org-get-next-sibling))))
  14154. (if point-to-move-to
  14155. (progn
  14156. (goto-char point-to-move-to)
  14157. (setq arg (1- arg)))
  14158. (progn
  14159. (setq arg 0)
  14160. (error "No following same-level heading"))))))
  14161. (defun org-get-next-sibling ()
  14162. "Move to next heading of the same level, and return point.
  14163. If there is no such heading, return nil.
  14164. This is like outline-next-sibling, but invisible headings are ok."
  14165. (let ((level (funcall outline-level)))
  14166. (outline-next-heading)
  14167. (while (and (not (eobp)) (> (funcall outline-level) level))
  14168. (outline-next-heading))
  14169. (if (or (eobp) (< (funcall outline-level) level))
  14170. nil
  14171. (point))))
  14172. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14173. ;; This is an exact copy of the original function, but it uses
  14174. ;; `org-back-to-heading', to make it work also in invisible
  14175. ;; trees. And is uses an invisible-OK argument.
  14176. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14177. (org-back-to-heading invisible-OK)
  14178. (let ((first t)
  14179. (level (funcall outline-level)))
  14180. (while (and (not (eobp))
  14181. (or first (> (funcall outline-level) level)))
  14182. (setq first nil)
  14183. (outline-next-heading))
  14184. (unless to-heading
  14185. (if (memq (preceding-char) '(?\n ?\^M))
  14186. (progn
  14187. ;; Go to end of line before heading
  14188. (forward-char -1)
  14189. (if (memq (preceding-char) '(?\n ?\^M))
  14190. ;; leave blank line before heading
  14191. (forward-char -1))))))
  14192. (point))
  14193. (defun org-show-subtree ()
  14194. "Show everything after this heading at deeper levels."
  14195. (outline-flag-region
  14196. (point)
  14197. (save-excursion
  14198. (outline-end-of-subtree) (outline-next-heading) (point))
  14199. nil))
  14200. (defun org-show-entry ()
  14201. "Show the body directly following this heading.
  14202. Show the heading too, if it is currently invisible."
  14203. (interactive)
  14204. (save-excursion
  14205. (condition-case nil
  14206. (progn
  14207. (org-back-to-heading t)
  14208. (outline-flag-region
  14209. (max (point-min) (1- (point)))
  14210. (save-excursion
  14211. (if (re-search-forward
  14212. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14213. (match-beginning 1)
  14214. (point-max)))
  14215. nil))
  14216. (error nil))))
  14217. (defun org-make-options-regexp (kwds &optional extra)
  14218. "Make a regular expression for keyword lines."
  14219. (concat
  14220. "^"
  14221. "#?[ \t]*\\+\\("
  14222. (mapconcat 'regexp-quote kwds "\\|")
  14223. (if extra (concat "\\|" extra))
  14224. "\\):[ \t]*"
  14225. "\\(.+\\)"))
  14226. ;; Make isearch reveal the necessary context
  14227. (defun org-isearch-end ()
  14228. "Reveal context after isearch exits."
  14229. (when isearch-success ; only if search was successful
  14230. (if (featurep 'xemacs)
  14231. ;; Under XEmacs, the hook is run in the correct place,
  14232. ;; we directly show the context.
  14233. (org-show-context 'isearch)
  14234. ;; In Emacs the hook runs *before* restoring the overlays.
  14235. ;; So we have to use a one-time post-command-hook to do this.
  14236. ;; (Emacs 22 has a special variable, see function `org-mode')
  14237. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14238. isearch-mode-end-hook-quit)
  14239. ;; Only when the isearch was not quitted.
  14240. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14241. 'append 'local)))))
  14242. (defun org-isearch-post-command ()
  14243. "Remove self from hook, and show context."
  14244. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14245. (org-show-context 'isearch))
  14246. ;;;; Integration with and fixes for other packages
  14247. ;;; Imenu support
  14248. (defvar org-imenu-markers nil
  14249. "All markers currently used by Imenu.")
  14250. (make-variable-buffer-local 'org-imenu-markers)
  14251. (defun org-imenu-new-marker (&optional pos)
  14252. "Return a new marker for use by Imenu, and remember the marker."
  14253. (let ((m (make-marker)))
  14254. (move-marker m (or pos (point)))
  14255. (push m org-imenu-markers)
  14256. m))
  14257. (defun org-imenu-get-tree ()
  14258. "Produce the index for Imenu."
  14259. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14260. (setq org-imenu-markers nil)
  14261. (let* ((n org-imenu-depth)
  14262. (re (concat "^" outline-regexp))
  14263. (subs (make-vector (1+ n) nil))
  14264. (last-level 0)
  14265. m level head)
  14266. (save-excursion
  14267. (save-restriction
  14268. (widen)
  14269. (goto-char (point-max))
  14270. (while (re-search-backward re nil t)
  14271. (setq level (org-reduced-level (funcall outline-level)))
  14272. (when (<= level n)
  14273. (looking-at org-complex-heading-regexp)
  14274. (setq head (org-link-display-format
  14275. (org-match-string-no-properties 4))
  14276. m (org-imenu-new-marker))
  14277. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14278. (if (>= level last-level)
  14279. (push (cons head m) (aref subs level))
  14280. (push (cons head (aref subs (1+ level))) (aref subs level))
  14281. (loop for i from (1+ level) to n do (aset subs i nil)))
  14282. (setq last-level level)))))
  14283. (aref subs 1)))
  14284. (eval-after-load "imenu"
  14285. '(progn
  14286. (add-hook 'imenu-after-jump-hook
  14287. (lambda ()
  14288. (if (eq major-mode 'org-mode)
  14289. (org-show-context 'org-goto))))))
  14290. (defun org-link-display-format (link)
  14291. "Replace a link with either the description, or the link target
  14292. if no description is present"
  14293. (save-match-data
  14294. (if (string-match org-bracket-link-analytic-regexp link)
  14295. (replace-match (or (match-string 5 link)
  14296. (concat (match-string 1 link)
  14297. (match-string 3 link)))
  14298. nil nil link)
  14299. link)))
  14300. ;; Speedbar support
  14301. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14302. "Overlay marking the agenda restriction line in speedbar.")
  14303. (org-overlay-put org-speedbar-restriction-lock-overlay
  14304. 'face 'org-agenda-restriction-lock)
  14305. (org-overlay-put org-speedbar-restriction-lock-overlay
  14306. 'help-echo "Agendas are currently limited to this item.")
  14307. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14308. (defun org-speedbar-set-agenda-restriction ()
  14309. "Restrict future agenda commands to the location at point in speedbar.
  14310. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14311. (interactive)
  14312. (require 'org-agenda)
  14313. (let (p m tp np dir txt)
  14314. (cond
  14315. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14316. 'org-imenu t))
  14317. (setq m (get-text-property p 'org-imenu-marker))
  14318. (save-excursion
  14319. (save-restriction
  14320. (set-buffer (marker-buffer m))
  14321. (goto-char m)
  14322. (org-agenda-set-restriction-lock 'subtree))))
  14323. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14324. 'speedbar-function 'speedbar-find-file))
  14325. (setq tp (previous-single-property-change
  14326. (1+ p) 'speedbar-function)
  14327. np (next-single-property-change
  14328. tp 'speedbar-function)
  14329. dir (speedbar-line-directory)
  14330. txt (buffer-substring-no-properties (or tp (point-min))
  14331. (or np (point-max))))
  14332. (save-excursion
  14333. (save-restriction
  14334. (set-buffer (find-file-noselect
  14335. (let ((default-directory dir))
  14336. (expand-file-name txt))))
  14337. (unless (org-mode-p)
  14338. (error "Cannot restrict to non-Org-mode file"))
  14339. (org-agenda-set-restriction-lock 'file))))
  14340. (t (error "Don't know how to restrict Org-mode's agenda")))
  14341. (org-move-overlay org-speedbar-restriction-lock-overlay
  14342. (point-at-bol) (point-at-eol))
  14343. (setq current-prefix-arg nil)
  14344. (org-agenda-maybe-redo)))
  14345. (eval-after-load "speedbar"
  14346. '(progn
  14347. (speedbar-add-supported-extension ".org")
  14348. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14349. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14350. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14351. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14352. (add-hook 'speedbar-visiting-tag-hook
  14353. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14354. ;;; Fixes and Hacks for problems with other packages
  14355. ;; Make flyspell not check words in links, to not mess up our keymap
  14356. (defun org-mode-flyspell-verify ()
  14357. "Don't let flyspell put overlays at active buttons."
  14358. (not (get-text-property (point) 'keymap)))
  14359. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14360. (eval-after-load "bookmark"
  14361. '(if (boundp 'bookmark-after-jump-hook)
  14362. ;; We can use the hook
  14363. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14364. ;; Hook not available, use advice
  14365. (defadvice bookmark-jump (after org-make-visible activate)
  14366. "Make the position visible."
  14367. (org-bookmark-jump-unhide))))
  14368. ;; Make sure saveplace show the location if it was hidden
  14369. (eval-after-load "saveplace"
  14370. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14371. "Make the position visible."
  14372. (org-bookmark-jump-unhide)))
  14373. (defun org-bookmark-jump-unhide ()
  14374. "Unhide the current position, to show the bookmark location."
  14375. (and (org-mode-p)
  14376. (or (org-invisible-p)
  14377. (save-excursion (goto-char (max (point-min) (1- (point))))
  14378. (org-invisible-p)))
  14379. (org-show-context 'bookmark-jump)))
  14380. ;; Make session.el ignore our circular variable
  14381. (eval-after-load "session"
  14382. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14383. ;;;; Experimental code
  14384. (defun org-closed-in-range ()
  14385. "Sparse tree of items closed in a certain time range.
  14386. Still experimental, may disappear in the future."
  14387. (interactive)
  14388. ;; Get the time interval from the user.
  14389. (let* ((time1 (time-to-seconds
  14390. (org-read-date nil 'to-time nil "Starting date: ")))
  14391. (time2 (time-to-seconds
  14392. (org-read-date nil 'to-time nil "End date:")))
  14393. ;; callback function
  14394. (callback (lambda ()
  14395. (let ((time
  14396. (time-to-seconds
  14397. (apply 'encode-time
  14398. (org-parse-time-string
  14399. (match-string 1))))))
  14400. ;; check if time in interval
  14401. (and (>= time time1) (<= time time2))))))
  14402. ;; make tree, check each match with the callback
  14403. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14404. ;;;; Finish up
  14405. (provide 'org)
  14406. (run-hooks 'org-load-hook)
  14407. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14408. ;;; org.el ends here