org.el 600 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127
  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.25c
  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.25c"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  174. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  185. (defcustom org-support-shift-select nil
  186. "Non-nil means, make shift-cursor commands select text when possible.
  187. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  188. selecting a region, or enlarge thusly regions started in this way.
  189. In Org-mode, in special contexts, these same keys are used for other
  190. purposes, important enough to compete with shift selection. Org tries
  191. to balance these needs by supporting `shift-select-mode' outside these
  192. special contexts, under control of this variable.
  193. The default of this variable is nil, to avoid confusing behavior. Shifted
  194. cursor keys will then execute Org commands in the following contexts:
  195. - on a headline, changing TODO state (left/right) and priority (up/down)
  196. - on a time stamp, changing the time
  197. - in a plain list item, changing the bullet type
  198. - in a property definition line, switching between allowed values
  199. - in the BEGIN line of a clock table (changing the time block).
  200. Outside these contexts, the commands will throw an error.
  201. When this variable is t and the cursor is not in a special context,
  202. Org-mode will support shift-selection for making and enlarging regions.
  203. To make this more effective, the bullet cycling will no longer happen
  204. anywhere in an item line, but only if the cursor is exactly on the bullet.
  205. If you set this variable to the symbol `always', then the keys
  206. will not be special in headlines, property lines, and item lines, to make
  207. shift selection work there as well. If this is what you want, you can
  208. use the following alternative commands: `C-c C-t' and `C-c ,' to
  209. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  210. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  211. edited by hand or in column view.
  212. However, when the cursor is on a timestamp, shift-cursor commands
  213. will still edit the time stamp - this is just too good to give up.
  214. XEmacs user should have this variable set to nil, because shift-select-mode
  215. is Emacs 23 only."
  216. :group 'org
  217. :type '(choice
  218. (const :tag "Never" nil)
  219. (const :tag "When outside special context" t)
  220. (const :tag "Everywhere except timestamps" always)))
  221. (defgroup org-startup nil
  222. "Options concerning startup of Org-mode."
  223. :tag "Org Startup"
  224. :group 'org)
  225. (defcustom org-startup-folded t
  226. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  227. This can also be configured on a per-file basis by adding one of
  228. the following lines anywhere in the buffer:
  229. #+STARTUP: fold
  230. #+STARTUP: nofold
  231. #+STARTUP: content"
  232. :group 'org-startup
  233. :type '(choice
  234. (const :tag "nofold: show all" nil)
  235. (const :tag "fold: overview" t)
  236. (const :tag "content: all headlines" content)))
  237. (defcustom org-startup-truncated t
  238. "Non-nil means, entering Org-mode will set `truncate-lines'.
  239. This is useful since some lines containing links can be very long and
  240. uninteresting. Also tables look terrible when wrapped."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-startup-align-all-tables nil
  244. "Non-nil means, align all tables when visiting a file.
  245. This is useful when the column width in tables is forced with <N> cookies
  246. in table fields. Such tables will look correct only after the first re-align.
  247. This can also be configured on a per-file basis by adding one of
  248. the following lines anywhere in the buffer:
  249. #+STARTUP: align
  250. #+STARTUP: noalign"
  251. :group 'org-startup
  252. :type 'boolean)
  253. (defcustom org-insert-mode-line-in-empty-file nil
  254. "Non-nil means insert the first line setting Org-mode in empty files.
  255. When the function `org-mode' is called interactively in an empty file, this
  256. normally means that the file name does not automatically trigger Org-mode.
  257. To ensure that the file will always be in Org-mode in the future, a
  258. line enforcing Org-mode will be inserted into the buffer, if this option
  259. has been set."
  260. :group 'org-startup
  261. :type 'boolean)
  262. (defcustom org-replace-disputed-keys nil
  263. "Non-nil means use alternative key bindings for some keys.
  264. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  265. These keys are also used by other packages like shift-selection-mode'
  266. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  267. If you want to use Org-mode together with one of these other modes,
  268. or more generally if you would like to move some Org-mode commands to
  269. other keys, set this variable and configure the keys with the variable
  270. `org-disputed-keys'.
  271. This option is only relevant at load-time of Org-mode, and must be set
  272. *before* org.el is loaded. Changing it requires a restart of Emacs to
  273. become effective."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-use-extra-keys nil
  277. "Non-nil means use extra key sequence definitions for certain
  278. commands. This happens automatically if you run XEmacs or if
  279. window-system is nil. This variable lets you do the same
  280. manually. You must set it before loading org.
  281. Example: on Carbon Emacs 22 running graphically, with an external
  282. keyboard on a Powerbook, the default way of setting M-left might
  283. not work for either Alt or ESC. Setting this variable will make
  284. it work for ESC."
  285. :group 'org-startup
  286. :type 'boolean)
  287. (if (fboundp 'defvaralias)
  288. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  289. (defcustom org-disputed-keys
  290. '(([(shift up)] . [(meta p)])
  291. ([(shift down)] . [(meta n)])
  292. ([(shift left)] . [(meta -)])
  293. ([(shift right)] . [(meta +)])
  294. ([(control shift right)] . [(meta shift +)])
  295. ([(control shift left)] . [(meta shift -)]))
  296. "Keys for which Org-mode and other modes compete.
  297. This is an alist, cars are the default keys, second element specifies
  298. the alternative to use when `org-replace-disputed-keys' is t.
  299. Keys can be specified in any syntax supported by `define-key'.
  300. The value of this option takes effect only at Org-mode's startup,
  301. therefore you'll have to restart Emacs to apply it after changing."
  302. :group 'org-startup
  303. :type 'alist)
  304. (defun org-key (key)
  305. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  306. Or return the original if not disputed."
  307. (if org-replace-disputed-keys
  308. (let* ((nkey (key-description key))
  309. (x (org-find-if (lambda (x)
  310. (equal (key-description (car x)) nkey))
  311. org-disputed-keys)))
  312. (if x (cdr x) key))
  313. key))
  314. (defun org-find-if (predicate seq)
  315. (catch 'exit
  316. (while seq
  317. (if (funcall predicate (car seq))
  318. (throw 'exit (car seq))
  319. (pop seq)))))
  320. (defun org-defkey (keymap key def)
  321. "Define a key, possibly translated, as returned by `org-key'."
  322. (define-key keymap (org-key key) def))
  323. (defcustom org-ellipsis nil
  324. "The ellipsis to use in the Org-mode outline.
  325. When nil, just use the standard three dots. When a string, use that instead,
  326. When a face, use the standard 3 dots, but with the specified face.
  327. The change affects only Org-mode (which will then use its own display table).
  328. Changing this requires executing `M-x org-mode' in a buffer to become
  329. effective."
  330. :group 'org-startup
  331. :type '(choice (const :tag "Default" nil)
  332. (face :tag "Face" :value org-warning)
  333. (string :tag "String" :value "...#")))
  334. (defvar org-display-table nil
  335. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  336. (defgroup org-keywords nil
  337. "Keywords in Org-mode."
  338. :tag "Org Keywords"
  339. :group 'org)
  340. (defcustom org-deadline-string "DEADLINE:"
  341. "String to mark deadline entries.
  342. A deadline is this string, followed by a time stamp. Should be a word,
  343. terminated by a colon. You can insert a schedule keyword and
  344. a timestamp with \\[org-deadline].
  345. Changes become only effective after restarting Emacs."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defcustom org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].
  353. Changes become only effective after restarting Emacs."
  354. :group 'org-keywords
  355. :type 'string)
  356. (defcustom org-closed-string "CLOSED:"
  357. "String used as the prefix for timestamps logging closing a TODO entry."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-clock-string "CLOCK:"
  361. "String used as prefix for timestamps clocking work hours on an item."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-comment-string "COMMENT"
  365. "Entries starting with this keyword will never be exported.
  366. An entry can be toggled between COMMENT and normal with
  367. \\[org-toggle-comment].
  368. Changes become only effective after restarting Emacs."
  369. :group 'org-keywords
  370. :type 'string)
  371. (defcustom org-quote-string "QUOTE"
  372. "Entries starting with this keyword will be exported in fixed-width font.
  373. Quoting applies only to the text in the entry following the headline, and does
  374. not extend beyond the next headline, even if that is lower level.
  375. An entry can be toggled between QUOTE and normal with
  376. \\[org-toggle-fixed-width-section]."
  377. :group 'org-keywords
  378. :type 'string)
  379. (defconst org-repeat-re
  380. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  381. "Regular expression for specifying repeated events.
  382. After a match, group 1 contains the repeat expression.")
  383. (defgroup org-structure nil
  384. "Options concerning the general structure of Org-mode files."
  385. :tag "Org Structure"
  386. :group 'org)
  387. (defgroup org-reveal-location nil
  388. "Options about how to make context of a location visible."
  389. :tag "Org Reveal Location"
  390. :group 'org-structure)
  391. (defconst org-context-choice
  392. '(choice
  393. (const :tag "Always" t)
  394. (const :tag "Never" nil)
  395. (repeat :greedy t :tag "Individual contexts"
  396. (cons
  397. (choice :tag "Context"
  398. (const agenda)
  399. (const org-goto)
  400. (const occur-tree)
  401. (const tags-tree)
  402. (const link-search)
  403. (const mark-goto)
  404. (const bookmark-jump)
  405. (const isearch)
  406. (const default))
  407. (boolean))))
  408. "Contexts for the reveal options.")
  409. (defcustom org-show-hierarchy-above '((default . t))
  410. "Non-nil means, show full hierarchy when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the hierarchy of headings
  413. above the exposed location is shown.
  414. Turning this off for example for sparse trees makes them very compact.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. Valid contexts are
  417. agenda when exposing an entry from the agenda
  418. org-goto when using the command `org-goto' on key C-c C-j
  419. occur-tree when using the command `org-occur' on key C-c /
  420. tags-tree when constructing a sparse tree based on tags matches
  421. link-search when exposing search matches associated with a link
  422. mark-goto when exposing the jump goal of a mark
  423. bookmark-jump when exposing a bookmark location
  424. isearch when exiting from an incremental search
  425. default default for all contexts not set explicitly"
  426. :group 'org-reveal-location
  427. :type org-context-choice)
  428. (defcustom org-show-following-heading '((default . nil))
  429. "Non-nil means, show following heading when revealing a location.
  430. Org-mode often shows locations in an org-mode file which might have
  431. been invisible before. When this is set, the heading following the
  432. match is shown.
  433. Turning this off for example for sparse trees makes them very compact,
  434. but makes it harder to edit the location of the match. In such a case,
  435. use the command \\[org-reveal] to show more context.
  436. Instead of t, this can also be an alist specifying this option for different
  437. contexts. See `org-show-hierarchy-above' for valid contexts."
  438. :group 'org-reveal-location
  439. :type org-context-choice)
  440. (defcustom org-show-siblings '((default . nil) (isearch t))
  441. "Non-nil means, show all sibling heading when revealing a location.
  442. Org-mode often shows locations in an org-mode file which might have
  443. been invisible before. When this is set, the sibling of the current entry
  444. heading are all made visible. If `org-show-hierarchy-above' is t,
  445. the same happens on each level of the hierarchy above the current entry.
  446. By default this is on for the isearch context, off for all other contexts.
  447. Turning this off for example for sparse trees makes them very compact,
  448. but makes it harder to edit the location of the match. In such a case,
  449. use the command \\[org-reveal] to show more context.
  450. Instead of t, this can also be an alist specifying this option for different
  451. contexts. See `org-show-hierarchy-above' for valid contexts."
  452. :group 'org-reveal-location
  453. :type org-context-choice)
  454. (defcustom org-show-entry-below '((default . nil))
  455. "Non-nil means, show the entry below a headline when revealing a location.
  456. Org-mode often shows locations in an org-mode file which might have
  457. been invisible before. When this is set, the text below the headline that is
  458. exposed is also shown.
  459. By default this is off for all contexts.
  460. Instead of t, this can also be an alist specifying this option for different
  461. contexts. See `org-show-hierarchy-above' for valid contexts."
  462. :group 'org-reveal-location
  463. :type org-context-choice)
  464. (defcustom org-indirect-buffer-display 'other-window
  465. "How should indirect tree buffers be displayed?
  466. This applies to indirect buffers created with the commands
  467. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  468. Valid values are:
  469. current-window Display in the current window
  470. other-window Just display in another window.
  471. dedicated-frame Create one new frame, and re-use it each time.
  472. new-frame Make a new frame each time. Note that in this case
  473. previously-made indirect buffers are kept, and you need to
  474. kill these buffers yourself."
  475. :group 'org-structure
  476. :group 'org-agenda-windows
  477. :type '(choice
  478. (const :tag "In current window" current-window)
  479. (const :tag "In current frame, other window" other-window)
  480. (const :tag "Each time a new frame" new-frame)
  481. (const :tag "One dedicated frame" dedicated-frame)))
  482. (defgroup org-cycle nil
  483. "Options concerning visibility cycling in Org-mode."
  484. :tag "Org Cycle"
  485. :group 'org-structure)
  486. (defcustom org-cycle-max-level nil
  487. "Maximum level which should still be subject to visibility cycling.
  488. Levels higher than this will, for cycling, be treated as text, not a headline.
  489. When `org-odd-levels-only' is set, a value of N in this variable actually
  490. means 2N-1 stars as the limiting headline.
  491. When nil, cycle all levels.
  492. Note that the limiting level of cycling is also influenced by
  493. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  494. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  495. than its value."
  496. :group 'org-cycle
  497. :type '(choice
  498. (const :tag "No limit" nil)
  499. (integer :tag "Maximum level")))
  500. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  501. "Names of drawers. Drawers are not opened by cycling on the headline above.
  502. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  503. this:
  504. :DRAWERNAME:
  505. .....
  506. :END:
  507. The drawer \"PROPERTIES\" is special for capturing properties through
  508. the property API.
  509. Drawers can be defined on the per-file basis with a line like:
  510. #+DRAWERS: HIDDEN STATE PROPERTIES"
  511. :group 'org-structure
  512. :group 'org-cycle
  513. :type '(repeat (string :tag "Drawer Name")))
  514. (defcustom org-cycle-global-at-bob nil
  515. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  516. This makes it possible to do global cycling without having to use S-TAB or
  517. C-u TAB. For this special case to work, the first line of the buffer
  518. must not be a headline - it may be empty or some other text. When used in
  519. this way, `org-cycle-hook' is disables temporarily, to make sure the
  520. cursor stays at the beginning of the buffer.
  521. When this option is nil, don't do anything special at the beginning
  522. of the buffer."
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-emulate-tab t
  526. "Where should `org-cycle' emulate TAB.
  527. nil Never
  528. white Only in completely white lines
  529. whitestart Only at the beginning of lines, before the first non-white char
  530. t Everywhere except in headlines
  531. exc-hl-bol Everywhere except at the start of a headline
  532. If TAB is used in a place where it does not emulate TAB, the current subtree
  533. visibility is cycled."
  534. :group 'org-cycle
  535. :type '(choice (const :tag "Never" nil)
  536. (const :tag "Only in completely white lines" white)
  537. (const :tag "Before first char in a line" whitestart)
  538. (const :tag "Everywhere except in headlines" t)
  539. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  540. ))
  541. (defcustom org-cycle-separator-lines 2
  542. "Number of empty lines needed to keep an empty line between collapsed trees.
  543. If you leave an empty line between the end of a subtree and the following
  544. headline, this empty line is hidden when the subtree is folded.
  545. Org-mode will leave (exactly) one empty line visible if the number of
  546. empty lines is equal or larger to the number given in this variable.
  547. So the default 2 means, at least 2 empty lines after the end of a subtree
  548. are needed to produce free space between a collapsed subtree and the
  549. following headline.
  550. Special case: when 0, never leave empty lines in collapsed view."
  551. :group 'org-cycle
  552. :type 'integer)
  553. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  554. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  555. org-cycle-hide-drawers
  556. org-cycle-show-empty-lines
  557. org-optimize-window-after-visibility-change)
  558. "Hook that is run after `org-cycle' has changed the buffer visibility.
  559. The function(s) in this hook must accept a single argument which indicates
  560. the new state that was set by the most recent `org-cycle' command. The
  561. argument is a symbol. After a global state change, it can have the values
  562. `overview', `content', or `all'. After a local state change, it can have
  563. the values `folded', `children', or `subtree'."
  564. :group 'org-cycle
  565. :type 'hook)
  566. (defgroup org-edit-structure nil
  567. "Options concerning structure editing in Org-mode."
  568. :tag "Org Edit Structure"
  569. :group 'org-structure)
  570. (defcustom org-odd-levels-only nil
  571. "Non-nil means, skip even levels and only use odd levels for the outline.
  572. This has the effect that two stars are being added/taken away in
  573. promotion/demotion commands. It also influences how levels are
  574. handled by the exporters.
  575. Changing it requires restart of `font-lock-mode' to become effective
  576. for fontification also in regions already fontified.
  577. You may also set this on a per-file basis by adding one of the following
  578. lines to the buffer:
  579. #+STARTUP: odd
  580. #+STARTUP: oddeven"
  581. :group 'org-edit-structure
  582. :group 'org-font-lock
  583. :type 'boolean)
  584. (defcustom org-adapt-indentation t
  585. "Non-nil means, adapt indentation when promoting and demoting.
  586. When this is set and the *entire* text in an entry is indented, the
  587. indentation is increased by one space in a demotion command, and
  588. decreased by one in a promotion command. If any line in the entry
  589. body starts at column 0, indentation is not changed at all."
  590. :group 'org-edit-structure
  591. :type 'boolean)
  592. (defcustom org-special-ctrl-a/e nil
  593. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  594. When t, `C-a' will bring back the cursor to the beginning of the
  595. headline text, i.e. after the stars and after a possible TODO keyword.
  596. In an item, this will be the position after the bullet.
  597. When the cursor is already at that position, another `C-a' will bring
  598. it to the beginning of the line.
  599. `C-e' will jump to the end of the headline, ignoring the presence of tags
  600. in the headline. A second `C-e' will then jump to the true end of the
  601. line, after any tags.
  602. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  603. and only a directly following, identical keypress will bring the cursor
  604. to the special positions.
  605. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  606. set separately."
  607. :group 'org-edit-structure
  608. :type '(choice
  609. (const :tag "off" nil)
  610. (const :tag "after stars/bullet and before tags first" t)
  611. (const :tag "true line boundary first" reversed)
  612. (cons :tag "Set C-a and C-e separately"
  613. (choice :tag "Special C-a"
  614. (const :tag "off" nil)
  615. (const :tag "after stars/bullet first" t)
  616. (const :tag "before stars/bullet first" reversed))
  617. (choice :tag "Special C-e"
  618. (const :tag "off" nil)
  619. (const :tag "before tags first" t)
  620. (const :tag "after tags first" reversed)))))
  621. (if (fboundp 'defvaralias)
  622. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  623. (defcustom org-special-ctrl-k nil
  624. "Non-nil means `C-k' will behave specially in headlines.
  625. When nil, `C-k' will call the default `kill-line' command.
  626. When t, the following will happen while the cursor is in the headline:
  627. - When the cursor is at the beginning of a headline, kill the entire
  628. line and possible the folded subtree below the line.
  629. - When in the middle of the headline text, kill the headline up to the tags.
  630. - When after the headline text, kill the tags."
  631. :group 'org-edit-structure
  632. :type 'boolean)
  633. (defcustom org-yank-folded-subtrees t
  634. "Non-nil means, when yanking subtrees, fold them.
  635. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  636. it starts with a heading and all other headings in it are either children
  637. or siblings, then fold all the subtrees. However, do this only if no
  638. text after the yank would be swallowed into a folded tree by this action."
  639. :group 'org-edit-structure
  640. :type 'boolean)
  641. (defcustom org-yank-adjusted-subtrees nil
  642. "Non-nil means, when yanking subtrees, adjust the level.
  643. With this setting, `org-paste-subtree' is used to insert the subtree, see
  644. this function for details."
  645. :group 'org-edit-structure
  646. :type 'boolean)
  647. (defcustom org-M-RET-may-split-line '((default . t))
  648. "Non-nil means, M-RET will split the line at the cursor position.
  649. When nil, it will go to the end of the line before making a
  650. new line.
  651. You may also set this option in a different way for different
  652. contexts. Valid contexts are:
  653. headline when creating a new headline
  654. item when creating a new item
  655. table in a table field
  656. default the value to be used for all contexts not explicitly
  657. customized"
  658. :group 'org-structure
  659. :group 'org-table
  660. :type '(choice
  661. (const :tag "Always" t)
  662. (const :tag "Never" nil)
  663. (repeat :greedy t :tag "Individual contexts"
  664. (cons
  665. (choice :tag "Context"
  666. (const headline)
  667. (const item)
  668. (const table)
  669. (const default))
  670. (boolean)))))
  671. (defcustom org-insert-heading-respect-content nil
  672. "Non-nil means, insert new headings after the current subtree.
  673. When nil, the new heading is created directly after the current line.
  674. The commands \\[org-insert-heading-respect-content] and
  675. \\[org-insert-todo-heading-respect-content] turn this variable on
  676. for the duration of the command."
  677. :group 'org-structure
  678. :type 'boolean)
  679. (defcustom org-blank-before-new-entry '((heading . auto)
  680. (plain-list-item . auto))
  681. "Should `org-insert-heading' leave a blank line before new heading/item?
  682. The value is an alist, with `heading' and `plain-list-item' as car,
  683. and a boolean flag as cdr. For plain lists, if the variable
  684. `org-empty-line-terminates-plain-lists' is set, the setting here
  685. is ignored and no empty line is inserted, to keep the list in tact."
  686. :group 'org-edit-structure
  687. :type '(list
  688. (cons (const heading)
  689. (choice (const :tag "Never" nil)
  690. (const :tag "Always" t)
  691. (const :tag "Auto" auto)))
  692. (cons (const plain-list-item)
  693. (choice (const :tag "Never" nil)
  694. (const :tag "Always" t)
  695. (const :tag "Auto" auto)))))
  696. (defcustom org-insert-heading-hook nil
  697. "Hook being run after inserting a new heading."
  698. :group 'org-edit-structure
  699. :type 'hook)
  700. (defcustom org-enable-fixed-width-editor t
  701. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  702. This currently only means, they are never auto-wrapped.
  703. When nil, such lines will be treated like ordinary lines.
  704. See also the QUOTE keyword."
  705. :group 'org-edit-structure
  706. :type 'boolean)
  707. (defcustom org-edit-src-region-extra nil
  708. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  709. For examples see the function `org-edit-src-find-region-and-lang'.
  710. The regular expression identifying the begin marker should end with a newline,
  711. and the regexp marking the end line should start with a newline, to make sure
  712. there are kept outside the narrowed region."
  713. :group 'org-edit-structure
  714. :type '(repeat
  715. (list
  716. (regexp :tag "begin regexp")
  717. (regexp :tag "end regexp")
  718. (choice :tag "language"
  719. (string :tag "specify")
  720. (integer :tag "from match group")
  721. (const :tag "from `lang' element")
  722. (const :tag "from `style' element")))))
  723. (defcustom org-coderef-label-format "(ref:%s)"
  724. "The default coderef format.
  725. This format string will be used to search for coderef labels in literal
  726. examples (EXAMPLE and SRC blocks). The format can be overwritten
  727. an individual literal example with the -f option, like
  728. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  729. ...
  730. #+END_SRC
  731. If you want to use this for HTML export, make sure that the format does
  732. not introduce special font-locking, and avoid the HTML special
  733. characters `<', `>', and `&'. The reason for this restriction is that
  734. the labels are searched for only after htmlize has done its job."
  735. :group 'org-edit-structure ; FIXME this is not in the right group
  736. :type 'string)
  737. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  738. "The mode that should be used to edit fixed-width regions.
  739. These are the regions where each line starts with a colon."
  740. :group 'org-edit-structure
  741. :type '(choice
  742. (const artist-mode)
  743. (const picture-mode)
  744. (const fundamental-mode)
  745. (function :tag "Other (specify)")))
  746. (defcustom org-goto-auto-isearch t
  747. "Non-nil means, typing characters in org-goto starts incremental search."
  748. :group 'org-edit-structure
  749. :type 'boolean)
  750. (defgroup org-sparse-trees nil
  751. "Options concerning sparse trees in Org-mode."
  752. :tag "Org Sparse Trees"
  753. :group 'org-structure)
  754. (defcustom org-highlight-sparse-tree-matches t
  755. "Non-nil means, highlight all matches that define a sparse tree.
  756. The highlights will automatically disappear the next time the buffer is
  757. changed by an edit command."
  758. :group 'org-sparse-trees
  759. :type 'boolean)
  760. (defcustom org-remove-highlights-with-change t
  761. "Non-nil means, any change to the buffer will remove temporary highlights.
  762. Such highlights are created by `org-occur' and `org-clock-display'.
  763. When nil, `C-c C-c needs to be used to get rid of the highlights.
  764. The highlights created by `org-preview-latex-fragment' always need
  765. `C-c C-c' to be removed."
  766. :group 'org-sparse-trees
  767. :group 'org-time
  768. :type 'boolean)
  769. (defcustom org-occur-hook '(org-first-headline-recenter)
  770. "Hook that is run after `org-occur' has constructed a sparse tree.
  771. This can be used to recenter the window to show as much of the structure
  772. as possible."
  773. :group 'org-sparse-trees
  774. :type 'hook)
  775. (defgroup org-imenu-and-speedbar nil
  776. "Options concerning imenu and speedbar in Org-mode."
  777. :tag "Org Imenu and Speedbar"
  778. :group 'org-structure)
  779. (defcustom org-imenu-depth 2
  780. "The maximum level for Imenu access to Org-mode headlines.
  781. This also applied for speedbar access."
  782. :group 'org-imenu-and-speedbar
  783. :type 'integer)
  784. (defgroup org-table nil
  785. "Options concerning tables in Org-mode."
  786. :tag "Org Table"
  787. :group 'org)
  788. (defcustom org-enable-table-editor 'optimized
  789. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  790. When nil, such lines will be treated like ordinary lines.
  791. When equal to the symbol `optimized', the table editor will be optimized to
  792. do the following:
  793. - Automatic overwrite mode in front of whitespace in table fields.
  794. This makes the structure of the table stay in tact as long as the edited
  795. field does not exceed the column width.
  796. - Minimize the number of realigns. Normally, the table is aligned each time
  797. TAB or RET are pressed to move to another field. With optimization this
  798. happens only if changes to a field might have changed the column width.
  799. Optimization requires replacing the functions `self-insert-command',
  800. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  801. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  802. very good at guessing when a re-align will be necessary, but you can always
  803. force one with \\[org-ctrl-c-ctrl-c].
  804. If you would like to use the optimized version in Org-mode, but the
  805. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  806. This variable can be used to turn on and off the table editor during a session,
  807. but in order to toggle optimization, a restart is required.
  808. See also the variable `org-table-auto-blank-field'."
  809. :group 'org-table
  810. :type '(choice
  811. (const :tag "off" nil)
  812. (const :tag "on" t)
  813. (const :tag "on, optimized" optimized)))
  814. (defcustom org-self-insert-cluster-for-undo t
  815. "Non-nil means cluster self-insert commands for undo when possible.
  816. If this is set, then, like in the Emacs command loop, 20 consequtive
  817. characters will be undone together.
  818. This is configurable, because there is some impact on typing performance."
  819. :group 'org-table
  820. :type 'boolean)
  821. (defcustom org-table-tab-recognizes-table.el t
  822. "Non-nil means, TAB will automatically notice a table.el table.
  823. When it sees such a table, it moves point into it and - if necessary -
  824. calls `table-recognize-table'."
  825. :group 'org-table-editing
  826. :type 'boolean)
  827. (defgroup org-link nil
  828. "Options concerning links in Org-mode."
  829. :tag "Org Link"
  830. :group 'org)
  831. (defvar org-link-abbrev-alist-local nil
  832. "Buffer-local version of `org-link-abbrev-alist', which see.
  833. The value of this is taken from the #+LINK lines.")
  834. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  835. (defcustom org-link-abbrev-alist nil
  836. "Alist of link abbreviations.
  837. The car of each element is a string, to be replaced at the start of a link.
  838. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  839. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  840. [[linkkey:tag][description]]
  841. The 'linkkey' must be a word word, starting with a letter, followed
  842. by letters, numbers, '-' or '_'.
  843. If REPLACE is a string, the tag will simply be appended to create the link.
  844. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  845. the placeholder \"%h\" will cause a url-encoded version of the tag to
  846. be inserted at that point (see the function `url-hexify-string').
  847. REPLACE may also be a function that will be called with the tag as the
  848. only argument to create the link, which should be returned as a string.
  849. See the manual for examples."
  850. :group 'org-link
  851. :type '(repeat
  852. (cons
  853. (string :tag "Protocol")
  854. (choice
  855. (string :tag "Format")
  856. (function)))))
  857. (defcustom org-descriptive-links t
  858. "Non-nil means, hide link part and only show description of bracket links.
  859. Bracket links are like [[link][description]]. This variable sets the initial
  860. state in new org-mode buffers. The setting can then be toggled on a
  861. per-buffer basis from the Org->Hyperlinks menu."
  862. :group 'org-link
  863. :type 'boolean)
  864. (defcustom org-link-file-path-type 'adaptive
  865. "How the path name in file links should be stored.
  866. Valid values are:
  867. relative Relative to the current directory, i.e. the directory of the file
  868. into which the link is being inserted.
  869. absolute Absolute path, if possible with ~ for home directory.
  870. noabbrev Absolute path, no abbreviation of home directory.
  871. adaptive Use relative path for files in the current directory and sub-
  872. directories of it. For other files, use an absolute path."
  873. :group 'org-link
  874. :type '(choice
  875. (const relative)
  876. (const absolute)
  877. (const noabbrev)
  878. (const adaptive)))
  879. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  880. "Types of links that should be activated in Org-mode files.
  881. This is a list of symbols, each leading to the activation of a certain link
  882. type. In principle, it does not hurt to turn on most link types - there may
  883. be a small gain when turning off unused link types. The types are:
  884. bracket The recommended [[link][description]] or [[link]] links with hiding.
  885. angular Links in angular brackets that may contain whitespace like
  886. <bbdb:Carsten Dominik>.
  887. plain Plain links in normal text, no whitespace, like http://google.com.
  888. radio Text that is matched by a radio target, see manual for details.
  889. tag Tag settings in a headline (link to tag search).
  890. date Time stamps (link to calendar).
  891. footnote Footnote labels.
  892. Changing this variable requires a restart of Emacs to become effective."
  893. :group 'org-link
  894. :type '(set :greedy t
  895. (const :tag "Double bracket links (new style)" bracket)
  896. (const :tag "Angular bracket links (old style)" angular)
  897. (const :tag "Plain text links" plain)
  898. (const :tag "Radio target matches" radio)
  899. (const :tag "Tags" tag)
  900. (const :tag "Timestamps" date)
  901. (const :tag "Footnotes" footnote)))
  902. (defcustom org-make-link-description-function nil
  903. "Function to use to generate link descriptions from links. If
  904. nil the link location will be used. This function must take two
  905. parameters; the first is the link and the second the description
  906. org-insert-link has generated, and should return the description
  907. to use."
  908. :group 'org-link
  909. :type 'function)
  910. (defgroup org-link-store nil
  911. "Options concerning storing links in Org-mode."
  912. :tag "Org Store Link"
  913. :group 'org-link)
  914. (defcustom org-email-link-description-format "Email %c: %.30s"
  915. "Format of the description part of a link to an email or usenet message.
  916. The following %-escapes will be replaced by corresponding information:
  917. %F full \"From\" field
  918. %f name, taken from \"From\" field, address if no name
  919. %T full \"To\" field
  920. %t first name in \"To\" field, address if no name
  921. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  922. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  923. %s subject
  924. %m message-id.
  925. You may use normal field width specification between the % and the letter.
  926. This is for example useful to limit the length of the subject.
  927. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  928. :group 'org-link-store
  929. :type 'string)
  930. (defcustom org-from-is-user-regexp
  931. (let (r1 r2)
  932. (when (and user-mail-address (not (string= user-mail-address "")))
  933. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  934. (when (and user-full-name (not (string= user-full-name "")))
  935. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  936. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  937. "Regexp matched against the \"From:\" header of an email or usenet message.
  938. It should match if the message is from the user him/herself."
  939. :group 'org-link-store
  940. :type 'regexp)
  941. (defcustom org-link-to-org-use-id 'create-if-interactive
  942. "Non-nil means, storing a link to an Org file will use entry IDs.
  943. Note that before this variable is even considered, org-id must be loaded,
  944. to please customize `org-modules' and turn it on.
  945. The variable can have the following values:
  946. t Create an ID if needed to make a link to the current entry.
  947. create-if-interactive
  948. If `org-store-link' is called directly (interactively, as a user
  949. command), do create an ID to support the link. But when doing the
  950. job for remember, only use the ID if it already exists. The
  951. purpose of this setting is to avoid proliferation of unwanted
  952. IDs, just because you happen to be in an Org file when you
  953. call `org-remember' that automatically and preemptively
  954. creates a link. If you do want to get an ID link in a remember
  955. template to an entry not having an ID, create it first by
  956. explicitly creating a link to it, using `C-c C-l' first.
  957. use-existing
  958. Use existing ID, do not create one.
  959. nil Never use an ID to make a link, instead link using a text search for
  960. the headline text."
  961. :group 'org-link-store
  962. :type '(choice
  963. (const :tag "Create ID to make link" t)
  964. (const :tag "Create if string link interactively"
  965. 'create-if-interactive)
  966. (const :tag "Only use existing" 'use-existing)
  967. (const :tag "Do not use ID to create link" nil)))
  968. (defcustom org-context-in-file-links t
  969. "Non-nil means, file links from `org-store-link' contain context.
  970. A search string will be added to the file name with :: as separator and
  971. used to find the context when the link is activated by the command
  972. `org-open-at-point'.
  973. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  974. negates this setting for the duration of the command."
  975. :group 'org-link-store
  976. :type 'boolean)
  977. (defcustom org-keep-stored-link-after-insertion nil
  978. "Non-nil means, keep link in list for entire session.
  979. The command `org-store-link' adds a link pointing to the current
  980. location to an internal list. These links accumulate during a session.
  981. The command `org-insert-link' can be used to insert links into any
  982. Org-mode file (offering completion for all stored links). When this
  983. option is nil, every link which has been inserted once using \\[org-insert-link]
  984. will be removed from the list, to make completing the unused links
  985. more efficient."
  986. :group 'org-link-store
  987. :type 'boolean)
  988. (defgroup org-link-follow nil
  989. "Options concerning following links in Org-mode."
  990. :tag "Org Follow Link"
  991. :group 'org-link)
  992. (defcustom org-link-translation-function nil
  993. "Function to translate links with different syntax to Org syntax.
  994. This can be used to translate links created for example by the Planner
  995. or emacs-wiki packages to Org syntax.
  996. The function must accept two parameters, a TYPE containing the link
  997. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  998. which is everything after the link protocol. It should return a cons
  999. with possibly modified values of type and path.
  1000. Org contains a function for this, so if you set this variable to
  1001. `org-translate-link-from-planner', you should be able follow many
  1002. links created by planner."
  1003. :group 'org-link-follow
  1004. :type 'function)
  1005. (defcustom org-follow-link-hook nil
  1006. "Hook that is run after a link has been followed."
  1007. :group 'org-link-follow
  1008. :type 'hook)
  1009. (defcustom org-tab-follows-link nil
  1010. "Non-nil means, on links TAB will follow the link.
  1011. Needs to be set before org.el is loaded.
  1012. This really should not be used, it does not make sense, and the
  1013. implementation is bad."
  1014. :group 'org-link-follow
  1015. :type 'boolean)
  1016. (defcustom org-return-follows-link nil
  1017. "Non-nil means, on links RET will follow the link.
  1018. Needs to be set before org.el is loaded."
  1019. :group 'org-link-follow
  1020. :type 'boolean)
  1021. (defcustom org-mouse-1-follows-link
  1022. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1023. "Non-nil means, mouse-1 on a link will follow the link.
  1024. A longer mouse click will still set point. Does not work on XEmacs.
  1025. Needs to be set before org.el is loaded."
  1026. :group 'org-link-follow
  1027. :type 'boolean)
  1028. (defcustom org-mark-ring-length 4
  1029. "Number of different positions to be recorded in the ring
  1030. Changing this requires a restart of Emacs to work correctly."
  1031. :group 'org-link-follow
  1032. :type 'integer)
  1033. (defcustom org-link-frame-setup
  1034. '((vm . vm-visit-folder-other-frame)
  1035. (gnus . gnus-other-frame)
  1036. (file . find-file-other-window))
  1037. "Setup the frame configuration for following links.
  1038. When following a link with Emacs, it may often be useful to display
  1039. this link in another window or frame. This variable can be used to
  1040. set this up for the different types of links.
  1041. For VM, use any of
  1042. `vm-visit-folder'
  1043. `vm-visit-folder-other-frame'
  1044. For Gnus, use any of
  1045. `gnus'
  1046. `gnus-other-frame'
  1047. `org-gnus-no-new-news'
  1048. For FILE, use any of
  1049. `find-file'
  1050. `find-file-other-window'
  1051. `find-file-other-frame'
  1052. For the calendar, use the variable `calendar-setup'.
  1053. For BBDB, it is currently only possible to display the matches in
  1054. another window."
  1055. :group 'org-link-follow
  1056. :type '(list
  1057. (cons (const vm)
  1058. (choice
  1059. (const vm-visit-folder)
  1060. (const vm-visit-folder-other-window)
  1061. (const vm-visit-folder-other-frame)))
  1062. (cons (const gnus)
  1063. (choice
  1064. (const gnus)
  1065. (const gnus-other-frame)
  1066. (const org-gnus-no-new-news)))
  1067. (cons (const file)
  1068. (choice
  1069. (const find-file)
  1070. (const find-file-other-window)
  1071. (const find-file-other-frame)))))
  1072. (defcustom org-display-internal-link-with-indirect-buffer nil
  1073. "Non-nil means, use indirect buffer to display infile links.
  1074. Activating internal links (from one location in a file to another location
  1075. in the same file) normally just jumps to the location. When the link is
  1076. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1077. another window. When this option is set, the other window actually displays
  1078. an indirect buffer clone of the current buffer, to avoid any visibility
  1079. changes to the current buffer."
  1080. :group 'org-link-follow
  1081. :type 'boolean)
  1082. (defcustom org-open-non-existing-files nil
  1083. "Non-nil means, `org-open-file' will open non-existing files.
  1084. When nil, an error will be generated."
  1085. :group 'org-link-follow
  1086. :type 'boolean)
  1087. (defcustom org-open-directory-means-index-dot-org nil
  1088. "Non-nil means, a link to a directory really means to index.org.
  1089. When nil, following a directory link will run dired or open a finder/explorer
  1090. window on that directory."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1094. "Function and arguments to call for following mailto links.
  1095. This is a list with the first element being a lisp function, and the
  1096. remaining elements being arguments to the function. In string arguments,
  1097. %a will be replaced by the address, and %s will be replaced by the subject
  1098. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1099. :group 'org-link-follow
  1100. :type '(choice
  1101. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1102. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1103. (const :tag "message-mail" (message-mail "%a" "%s"))
  1104. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1105. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1106. "Non-nil means, ask for confirmation before executing shell links.
  1107. Shell links can be dangerous: just think about a link
  1108. [[shell:rm -rf ~/*][Google Search]]
  1109. This link would show up in your Org-mode document as \"Google Search\",
  1110. but really it would remove your entire home directory.
  1111. Therefore we advise against setting this variable to nil.
  1112. Just change it to `y-or-n-p' if you want to confirm with a
  1113. single keystroke rather than having to type \"yes\"."
  1114. :group 'org-link-follow
  1115. :type '(choice
  1116. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1117. (const :tag "with y-or-n (faster)" y-or-n-p)
  1118. (const :tag "no confirmation (dangerous)" nil)))
  1119. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1120. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1121. Elisp links can be dangerous: just think about a link
  1122. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1123. This link would show up in your Org-mode document as \"Google Search\",
  1124. but really it would remove your entire home directory.
  1125. Therefore we advise against setting this variable to nil.
  1126. Just change it to `y-or-n-p' if you want to confirm with a
  1127. single keystroke rather than having to type \"yes\"."
  1128. :group 'org-link-follow
  1129. :type '(choice
  1130. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1131. (const :tag "with y-or-n (faster)" y-or-n-p)
  1132. (const :tag "no confirmation (dangerous)" nil)))
  1133. (defconst org-file-apps-defaults-gnu
  1134. '((remote . emacs)
  1135. (system . mailcap)
  1136. (t . mailcap))
  1137. "Default file applications on a UNIX or GNU/Linux system.
  1138. See `org-file-apps'.")
  1139. (defconst org-file-apps-defaults-macosx
  1140. '((remote . emacs)
  1141. (t . "open %s")
  1142. (system . "open %s")
  1143. ("ps.gz" . "gv %s")
  1144. ("eps.gz" . "gv %s")
  1145. ("dvi" . "xdvi %s")
  1146. ("fig" . "xfig %s"))
  1147. "Default file applications on a MacOS X system.
  1148. The system \"open\" is known as a default, but we use X11 applications
  1149. for some files for which the OS does not have a good default.
  1150. See `org-file-apps'.")
  1151. (defconst org-file-apps-defaults-windowsnt
  1152. (list
  1153. '(remote . emacs)
  1154. (cons t
  1155. (list (if (featurep 'xemacs)
  1156. 'mswindows-shell-execute
  1157. 'w32-shell-execute)
  1158. "open" 'file))
  1159. (cons 'system
  1160. (list (if (featurep 'xemacs)
  1161. 'mswindows-shell-execute
  1162. 'w32-shell-execute)
  1163. "open" 'file)))
  1164. "Default file applications on a Windows NT system.
  1165. The system \"open\" is used for most files.
  1166. See `org-file-apps'.")
  1167. (defcustom org-file-apps
  1168. '(
  1169. (auto-mode . emacs)
  1170. ("\\.x?html?\\'" . default)
  1171. ("\\.pdf\\'" . default)
  1172. )
  1173. "External applications for opening `file:path' items in a document.
  1174. Org-mode uses system defaults for different file types, but
  1175. you can use this variable to set the application for a given file
  1176. extension. The entries in this list are cons cells where the car identifies
  1177. files and the cdr the corresponding command. Possible values for the
  1178. file identifier are
  1179. \"regex\" Regular expression matched against the file name. For backward
  1180. compatibility, this can also be a string with only alphanumeric
  1181. characters, which is then interpreted as an extension.
  1182. `directory' Matches a directory
  1183. `remote' Matches a remote file, accessible through tramp or efs.
  1184. Remote files most likely should be visited through Emacs
  1185. because external applications cannot handle such paths.
  1186. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1187. so all files Emacs knows how to handle. Using this with
  1188. command `emacs' will open most files in Emacs. Beware that this
  1189. will also open html files inside Emacs, unless you add
  1190. (\"html\" . default) to the list as well.
  1191. t Default for files not matched by any of the other options.
  1192. `system' The system command to open files, like `open' on Windows
  1193. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1194. that will be selected if you call `C-c C-o' with a double
  1195. `C-u C-u' prefix.
  1196. Possible values for the command are:
  1197. `emacs' The file will be visited by the current Emacs process.
  1198. `default' Use the default application for this file type, which is the
  1199. association for t in the list, most likely in the system-specific
  1200. part.
  1201. This can be used to overrule an unwanted setting in the
  1202. system-specific variable.
  1203. `system' Use the system command for opening files, like \"open\".
  1204. This command is specified by the entry whose car is `system'.
  1205. Most likely, the system-specific version of this variable
  1206. does define this command, but you can overrule/replace it
  1207. here.
  1208. string A command to be executed by a shell; %s will be replaced
  1209. by the path to the file.
  1210. sexp A Lisp form which will be evaluated. The file path will
  1211. be available in the Lisp variable `file'.
  1212. For more examples, see the system specific constants
  1213. `org-file-apps-defaults-macosx'
  1214. `org-file-apps-defaults-windowsnt'
  1215. `org-file-apps-defaults-gnu'."
  1216. :group 'org-link-follow
  1217. :type '(repeat
  1218. (cons (choice :value ""
  1219. (string :tag "Extension")
  1220. (const :tag "System command to open files" system)
  1221. (const :tag "Default for unrecognized files" t)
  1222. (const :tag "Remote file" remote)
  1223. (const :tag "Links to a directory" directory)
  1224. (const :tag "Any files that have Emacs modes"
  1225. auto-mode))
  1226. (choice :value ""
  1227. (const :tag "Visit with Emacs" emacs)
  1228. (const :tag "Use default" default)
  1229. (const :tag "Use the system command" system)
  1230. (string :tag "Command")
  1231. (sexp :tag "Lisp form")))))
  1232. (defgroup org-refile nil
  1233. "Options concerning refiling entries in Org-mode."
  1234. :tag "Org Refile"
  1235. :group 'org)
  1236. (defcustom org-directory "~/org"
  1237. "Directory with org files.
  1238. This is just a default location to look for Org files. There is no need
  1239. at all to put your files into this directory. It is only used in the
  1240. following situations:
  1241. 1. When a remember template specifies a target file that is not an
  1242. absolute path. The path will then be interpreted relative to
  1243. `org-directory'
  1244. 2. When a remember note is filed away in an interactive way (when exiting the
  1245. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1246. with `org-directory' as the default path."
  1247. :group 'org-refile
  1248. :group 'org-remember
  1249. :type 'directory)
  1250. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1251. "Default target for storing notes.
  1252. Used by the hooks for remember.el. This can be a string, or nil to mean
  1253. the value of `remember-data-file'.
  1254. You can set this on a per-template basis with the variable
  1255. `org-remember-templates'."
  1256. :group 'org-refile
  1257. :group 'org-remember
  1258. :type '(choice
  1259. (const :tag "Default from remember-data-file" nil)
  1260. file))
  1261. (defcustom org-goto-interface 'outline
  1262. "The default interface to be used for `org-goto'.
  1263. Allowed values are:
  1264. outline The interface shows an outline of the relevant file
  1265. and the correct heading is found by moving through
  1266. the outline or by searching with incremental search.
  1267. outline-path-completion Headlines in the current buffer are offered via
  1268. completion. This is the interface also used by
  1269. the refile command."
  1270. :group 'org-refile
  1271. :type '(choice
  1272. (const :tag "Outline" outline)
  1273. (const :tag "Outline-path-completion" outline-path-completion)))
  1274. (defcustom org-goto-max-level 5
  1275. "Maximum level to be considered when running org-goto with refile interface."
  1276. :group 'org-refile
  1277. :type 'integer)
  1278. (defcustom org-reverse-note-order nil
  1279. "Non-nil means, store new notes at the beginning of a file or entry.
  1280. When nil, new notes will be filed to the end of a file or entry.
  1281. This can also be a list with cons cells of regular expressions that
  1282. are matched against file names, and values."
  1283. :group 'org-remember
  1284. :group 'org-refile
  1285. :type '(choice
  1286. (const :tag "Reverse always" t)
  1287. (const :tag "Reverse never" nil)
  1288. (repeat :tag "By file name regexp"
  1289. (cons regexp boolean))))
  1290. (defcustom org-refile-targets nil
  1291. "Targets for refiling entries with \\[org-refile].
  1292. This is list of cons cells. Each cell contains:
  1293. - a specification of the files to be considered, either a list of files,
  1294. or a symbol whose function or variable value will be used to retrieve
  1295. a file name or a list of file names. If you use `org-agenda-files' for
  1296. that, all agenda files will be scanned for targets. Nil means, consider
  1297. headings in the current buffer.
  1298. - A specification of how to find candidate refile targets. This may be
  1299. any of:
  1300. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1301. This tag has to be present in all target headlines, inheritance will
  1302. not be considered.
  1303. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1304. todo keyword.
  1305. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1306. headlines that are refiling targets.
  1307. - a cons cell (:level . N). Any headline of level N is considered a target.
  1308. Note that, when `org-odd-levels-only' is set, level corresponds to
  1309. order in hierarchy, not to the number of stars.
  1310. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1311. Note that, when `org-odd-levels-only' is set, level corresponds to
  1312. order in hierarchy, not to the number of stars.
  1313. When this variable is nil, all top-level headlines in the current buffer
  1314. are used, equivalent to the value `((nil . (:level . 1))'."
  1315. :group 'org-refile
  1316. :type '(repeat
  1317. (cons
  1318. (choice :value org-agenda-files
  1319. (const :tag "All agenda files" org-agenda-files)
  1320. (const :tag "Current buffer" nil)
  1321. (function) (variable) (file))
  1322. (choice :tag "Identify target headline by"
  1323. (cons :tag "Specific tag" (const :value :tag) (string))
  1324. (cons :tag "TODO keyword" (const :value :todo) (string))
  1325. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1326. (cons :tag "Level number" (const :value :level) (integer))
  1327. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1328. (defcustom org-refile-use-outline-path nil
  1329. "Non-nil means, provide refile targets as paths.
  1330. So a level 3 headline will be available as level1/level2/level3.
  1331. When the value is `file', also include the file name (without directory)
  1332. into the path. In this case, you can also stop the completion after
  1333. the file name, to get entries inserted as top level in the file.
  1334. When `full-file-path', include the full file path."
  1335. :group 'org-refile
  1336. :type '(choice
  1337. (const :tag "Not" nil)
  1338. (const :tag "Yes" t)
  1339. (const :tag "Start with file name" file)
  1340. (const :tag "Start with full file path" full-file-path)))
  1341. (defcustom org-outline-path-complete-in-steps t
  1342. "Non-nil means, complete the outline path in hierarchical steps.
  1343. When Org-mode uses the refile interface to select an outline path
  1344. \(see variable `org-refile-use-outline-path'), the completion of
  1345. the path can be done is a single go, or if can be done in steps down
  1346. the headline hierarchy. Going in steps is probably the best if you
  1347. do not use a special completion package like `ido' or `icicles'.
  1348. However, when using these packages, going in one step can be very
  1349. fast, while still showing the whole path to the entry."
  1350. :group 'org-refile
  1351. :type 'boolean)
  1352. (defgroup org-todo nil
  1353. "Options concerning TODO items in Org-mode."
  1354. :tag "Org TODO"
  1355. :group 'org)
  1356. (defgroup org-progress nil
  1357. "Options concerning Progress logging in Org-mode."
  1358. :tag "Org Progress"
  1359. :group 'org-time)
  1360. (defvar org-todo-interpretation-widgets
  1361. '(
  1362. (:tag "Sequence (cycling hits every state)" sequence)
  1363. (:tag "Type (cycling directly to DONE)" type))
  1364. "The available interpretation symbols for customizing
  1365. `org-todo-keywords'.
  1366. Interested libraries should add to this list.")
  1367. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1368. "List of TODO entry keyword sequences and their interpretation.
  1369. \\<org-mode-map>This is a list of sequences.
  1370. Each sequence starts with a symbol, either `sequence' or `type',
  1371. indicating if the keywords should be interpreted as a sequence of
  1372. action steps, or as different types of TODO items. The first
  1373. keywords are states requiring action - these states will select a headline
  1374. for inclusion into the global TODO list Org-mode produces. If one of
  1375. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1376. signify that no further action is necessary. If \"|\" is not found,
  1377. the last keyword is treated as the only DONE state of the sequence.
  1378. The command \\[org-todo] cycles an entry through these states, and one
  1379. additional state where no keyword is present. For details about this
  1380. cycling, see the manual.
  1381. TODO keywords and interpretation can also be set on a per-file basis with
  1382. the special #+SEQ_TODO and #+TYP_TODO lines.
  1383. Each keyword can optionally specify a character for fast state selection
  1384. \(in combination with the variable `org-use-fast-todo-selection')
  1385. and specifiers for state change logging, using the same syntax
  1386. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1387. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1388. indicates to record a time stamp each time this state is selected.
  1389. Each keyword may also specify if a timestamp or a note should be
  1390. recorded when entering or leaving the state, by adding additional
  1391. characters in the parenthesis after the keyword. This looks like this:
  1392. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1393. record only the time of the state change. With X and Y being either
  1394. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1395. Y when leaving the state if and only if the *target* state does not
  1396. define X. You may omit any of the fast-selection key or X or /Y,
  1397. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1398. For backward compatibility, this variable may also be just a list
  1399. of keywords - in this case the interpretation (sequence or type) will be
  1400. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1401. :group 'org-todo
  1402. :group 'org-keywords
  1403. :type '(choice
  1404. (repeat :tag "Old syntax, just keywords"
  1405. (string :tag "Keyword"))
  1406. (repeat :tag "New syntax"
  1407. (cons
  1408. (choice
  1409. :tag "Interpretation"
  1410. ;;Quick and dirty way to see
  1411. ;;`org-todo-interpretations'. This takes the
  1412. ;;place of item arguments
  1413. :convert-widget
  1414. (lambda (widget)
  1415. (widget-put widget
  1416. :args (mapcar
  1417. #'(lambda (x)
  1418. (widget-convert
  1419. (cons 'const x)))
  1420. org-todo-interpretation-widgets))
  1421. widget))
  1422. (repeat
  1423. (string :tag "Keyword"))))))
  1424. (defvar org-todo-keywords-1 nil
  1425. "All TODO and DONE keywords active in a buffer.")
  1426. (make-variable-buffer-local 'org-todo-keywords-1)
  1427. (defvar org-todo-keywords-for-agenda nil)
  1428. (defvar org-done-keywords-for-agenda nil)
  1429. (defvar org-todo-keyword-alist-for-agenda nil)
  1430. (defvar org-tag-alist-for-agenda nil)
  1431. (defvar org-agenda-contributing-files nil)
  1432. (defvar org-not-done-keywords nil)
  1433. (make-variable-buffer-local 'org-not-done-keywords)
  1434. (defvar org-done-keywords nil)
  1435. (make-variable-buffer-local 'org-done-keywords)
  1436. (defvar org-todo-heads nil)
  1437. (make-variable-buffer-local 'org-todo-heads)
  1438. (defvar org-todo-sets nil)
  1439. (make-variable-buffer-local 'org-todo-sets)
  1440. (defvar org-todo-log-states nil)
  1441. (make-variable-buffer-local 'org-todo-log-states)
  1442. (defvar org-todo-kwd-alist nil)
  1443. (make-variable-buffer-local 'org-todo-kwd-alist)
  1444. (defvar org-todo-key-alist nil)
  1445. (make-variable-buffer-local 'org-todo-key-alist)
  1446. (defvar org-todo-key-trigger nil)
  1447. (make-variable-buffer-local 'org-todo-key-trigger)
  1448. (defcustom org-todo-interpretation 'sequence
  1449. "Controls how TODO keywords are interpreted.
  1450. This variable is in principle obsolete and is only used for
  1451. backward compatibility, if the interpretation of todo keywords is
  1452. not given already in `org-todo-keywords'. See that variable for
  1453. more information."
  1454. :group 'org-todo
  1455. :group 'org-keywords
  1456. :type '(choice (const sequence)
  1457. (const type)))
  1458. (defcustom org-use-fast-todo-selection t
  1459. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1460. This variable describes if and under what circumstances the cycling
  1461. mechanism for TODO keywords will be replaced by a single-key, direct
  1462. selection scheme.
  1463. When nil, fast selection is never used.
  1464. When the symbol `prefix', it will be used when `org-todo' is called with
  1465. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1466. in an agenda buffer.
  1467. When t, fast selection is used by default. In this case, the prefix
  1468. argument forces cycling instead.
  1469. In all cases, the special interface is only used if access keys have actually
  1470. been assigned by the user, i.e. if keywords in the configuration are followed
  1471. by a letter in parenthesis, like TODO(t)."
  1472. :group 'org-todo
  1473. :type '(choice
  1474. (const :tag "Never" nil)
  1475. (const :tag "By default" t)
  1476. (const :tag "Only with C-u C-c C-t" prefix)))
  1477. (defcustom org-provide-todo-statistics t
  1478. "Non-nil means, update todo statistics after insert and toggle.
  1479. When this is set, todo statistics is updated in the parent of the current
  1480. entry each time a todo state is changed."
  1481. :group 'org-todo
  1482. :type 'boolean)
  1483. (defcustom org-after-todo-state-change-hook nil
  1484. "Hook which is run after the state of a TODO item was changed.
  1485. The new state (a string with a TODO keyword, or nil) is available in the
  1486. Lisp variable `state'."
  1487. :group 'org-todo
  1488. :type 'hook)
  1489. (defvar org-blocker-hook nil
  1490. "Hook for functions that are allowed to block a state change.
  1491. Each function gets as its single argument a property list, see
  1492. `org-trigger-hook' for more information about this list.
  1493. If any of the functions in this hook returns nil, the state change
  1494. is blocked.")
  1495. (defvar org-trigger-hook nil
  1496. "Hook for functions that are triggered by a state change.
  1497. Each function gets as its single argument a property list with at least
  1498. the following elements:
  1499. (:type type-of-change :position pos-at-entry-start
  1500. :from old-state :to new-state)
  1501. Depending on the type, more properties may be present.
  1502. This mechanism is currently implemented for:
  1503. TODO state changes
  1504. ------------------
  1505. :type todo-state-change
  1506. :from previous state (keyword as a string), or nil, or a symbol
  1507. 'todo' or 'done', to indicate the general type of state.
  1508. :to new state, like in :from")
  1509. (defcustom org-enforce-todo-dependencies nil
  1510. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1511. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1512. be blocked if any prior sibling is not yet done.
  1513. Finally, if the parent is blocked because of ordered siblings of its own,
  1514. the child will also be blocked.
  1515. This variable needs to be set before org.el is loaded, and you need to
  1516. restart Emacs after a change to make the change effective. The only way
  1517. to change is while Emacs is running is through the customize interface."
  1518. :set (lambda (var val)
  1519. (set var val)
  1520. (if val
  1521. (add-hook 'org-blocker-hook
  1522. 'org-block-todo-from-children-or-siblings-or-parent)
  1523. (remove-hook 'org-blocker-hook
  1524. 'org-block-todo-from-children-or-siblings-or-parent)))
  1525. :group 'org-todo
  1526. :type 'boolean)
  1527. (defcustom org-enforce-todo-checkbox-dependencies nil
  1528. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1529. When this is nil, checkboxes have no influence on switching TODO states.
  1530. When non-nil, you first need to check off all check boxes before the TODO
  1531. entry can be switched to DONE.
  1532. This variable needs to be set before org.el is loaded, and you need to
  1533. restart Emacs after a change to make the change effective. The only way
  1534. to change is while Emacs is running is through the customize interface."
  1535. :set (lambda (var val)
  1536. (set var val)
  1537. (if val
  1538. (add-hook 'org-blocker-hook
  1539. 'org-block-todo-from-checkboxes)
  1540. (remove-hook 'org-blocker-hook
  1541. 'org-block-todo-from-checkboxes)))
  1542. :group 'org-todo
  1543. :type 'boolean)
  1544. (defcustom org-todo-state-tags-triggers nil
  1545. "Tag changes that should be triggered by TODO state changes.
  1546. This is a list. Each entry is
  1547. (state-change (tag . flag) .......)
  1548. State-change can be a string with a state, and empty string to indicate the
  1549. state that has no TODO keyword, or it can be one of the symbols `todo'
  1550. or `done', meaning any not-done or done state, respectively."
  1551. :group 'org-todo
  1552. :group 'org-tags
  1553. :type '(repeat
  1554. (cons (choice :tag "When changing to"
  1555. (const :tag "Not-done state" todo)
  1556. (const :tag "Done state" done)
  1557. (string :tag "State"))
  1558. (repeat
  1559. (cons :tag "Tag action"
  1560. (string :tag "Tag")
  1561. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1562. (defcustom org-log-done nil
  1563. "Information to record when a task moves to the DONE state.
  1564. Possible values are:
  1565. nil Don't add anything, just change the keyword
  1566. time Add a time stamp to the task
  1567. note Prompt a closing note and add it with template `org-log-note-headings'
  1568. This option can also be set with on a per-file-basis with
  1569. #+STARTUP: nologdone
  1570. #+STARTUP: logdone
  1571. #+STARTUP: lognotedone
  1572. You can have local logging settings for a subtree by setting the LOGGING
  1573. property to one or more of these keywords."
  1574. :group 'org-todo
  1575. :group 'org-progress
  1576. :type '(choice
  1577. (const :tag "No logging" nil)
  1578. (const :tag "Record CLOSED timestamp" time)
  1579. (const :tag "Record CLOSED timestamp with closing note." note)))
  1580. ;; Normalize old uses of org-log-done.
  1581. (cond
  1582. ((eq org-log-done t) (setq org-log-done 'time))
  1583. ((and (listp org-log-done) (memq 'done org-log-done))
  1584. (setq org-log-done 'note)))
  1585. (defcustom org-log-note-clock-out nil
  1586. "Non-nil means, record a note when clocking out of an item.
  1587. This can also be configured on a per-file basis by adding one of
  1588. the following lines anywhere in the buffer:
  1589. #+STARTUP: lognoteclock-out
  1590. #+STARTUP: nolognoteclock-out"
  1591. :group 'org-todo
  1592. :group 'org-progress
  1593. :type 'boolean)
  1594. (defcustom org-log-done-with-time t
  1595. "Non-nil means, the CLOSED time stamp will contain date and time.
  1596. When nil, only the date will be recorded."
  1597. :group 'org-progress
  1598. :type 'boolean)
  1599. (defcustom org-log-note-headings
  1600. '((done . "CLOSING NOTE %t")
  1601. (state . "State %-12s from %-12S %t")
  1602. (note . "Note taken on %t")
  1603. (clock-out . ""))
  1604. "Headings for notes added to entries.
  1605. The value is an alist, with the car being a symbol indicating the note
  1606. context, and the cdr is the heading to be used. The heading may also be the
  1607. empty string.
  1608. %t in the heading will be replaced by a time stamp.
  1609. %s will be replaced by the new TODO state, in double quotes.
  1610. %S will be replaced by the old TODO state, in double quotes.
  1611. %u will be replaced by the user name.
  1612. %U will be replaced by the full user name."
  1613. :group 'org-todo
  1614. :group 'org-progress
  1615. :type '(list :greedy t
  1616. (cons (const :tag "Heading when closing an item" done) string)
  1617. (cons (const :tag
  1618. "Heading when changing todo state (todo sequence only)"
  1619. state) string)
  1620. (cons (const :tag "Heading when just taking a note" note) string)
  1621. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1622. (unless (assq 'note org-log-note-headings)
  1623. (push '(note . "%t") org-log-note-headings))
  1624. (defcustom org-log-into-drawer nil
  1625. "Non-nil means, insert state change notes and time stamps into a drawer.
  1626. When nil, state changes notes will be inserted after the headline and
  1627. any scheduling and clock lines, but not inside a drawer.
  1628. The value of this variable should be the name of the drawer to use.
  1629. LOGBOOK is proposed at the default drawer for this purpose, you can
  1630. also set this to a string to define the drawer of your choice.
  1631. A value of t is also allowed, representing \"LOGBOOK\".
  1632. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1633. will be ignored."
  1634. :group 'org-todo
  1635. :group 'org-progress
  1636. :type '(choice
  1637. (const :tag "Not into a drawer" nil)
  1638. (const :tag "LOGBOOK" t)
  1639. (string :tag "Other")))
  1640. (if (fboundp 'defvaralias)
  1641. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1642. (defcustom org-log-state-notes-insert-after-drawers nil
  1643. "Non-nil means, insert state change notes after any drawers in entry.
  1644. Only the drawers that *immediately* follow the headline and the
  1645. deadline/scheduled line are skipped.
  1646. When nil, insert notes right after the heading and perhaps the line
  1647. with deadline/scheduling if present.
  1648. This variable will have no effect if `org-log-into-drawer' is
  1649. set."
  1650. :group 'org-todo
  1651. :group 'org-progress
  1652. :type 'boolean)
  1653. (defcustom org-log-states-order-reversed t
  1654. "Non-nil means, the latest state change note will be directly after heading.
  1655. When nil, the notes will be orderer according to time."
  1656. :group 'org-todo
  1657. :group 'org-progress
  1658. :type 'boolean)
  1659. (defcustom org-log-repeat 'time
  1660. "Non-nil means, record moving through the DONE state when triggering repeat.
  1661. An auto-repeating tasks is immediately switched back to TODO when marked
  1662. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1663. the TODO keyword definition, or recording a closing note by setting
  1664. `org-log-done', there will be no record of the task moving through DONE.
  1665. This variable forces taking a note anyway. Possible values are:
  1666. nil Don't force a record
  1667. time Record a time stamp
  1668. note Record a note
  1669. This option can also be set with on a per-file-basis with
  1670. #+STARTUP: logrepeat
  1671. #+STARTUP: lognoterepeat
  1672. #+STARTUP: nologrepeat
  1673. You can have local logging settings for a subtree by setting the LOGGING
  1674. property to one or more of these keywords."
  1675. :group 'org-todo
  1676. :group 'org-progress
  1677. :type '(choice
  1678. (const :tag "Don't force a record" nil)
  1679. (const :tag "Force recording the DONE state" time)
  1680. (const :tag "Force recording a note with the DONE state" note)))
  1681. (defgroup org-priorities nil
  1682. "Priorities in Org-mode."
  1683. :tag "Org Priorities"
  1684. :group 'org-todo)
  1685. (defcustom org-highest-priority ?A
  1686. "The highest priority of TODO items. A character like ?A, ?B etc.
  1687. Must have a smaller ASCII number than `org-lowest-priority'."
  1688. :group 'org-priorities
  1689. :type 'character)
  1690. (defcustom org-lowest-priority ?C
  1691. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1692. Must have a larger ASCII number than `org-highest-priority'."
  1693. :group 'org-priorities
  1694. :type 'character)
  1695. (defcustom org-default-priority ?B
  1696. "The default priority of TODO items.
  1697. This is the priority an item get if no explicit priority is given."
  1698. :group 'org-priorities
  1699. :type 'character)
  1700. (defcustom org-priority-start-cycle-with-default t
  1701. "Non-nil means, start with default priority when starting to cycle.
  1702. When this is nil, the first step in the cycle will be (depending on the
  1703. command used) one higher or lower that the default priority."
  1704. :group 'org-priorities
  1705. :type 'boolean)
  1706. (defgroup org-time nil
  1707. "Options concerning time stamps and deadlines in Org-mode."
  1708. :tag "Org Time"
  1709. :group 'org)
  1710. (defcustom org-insert-labeled-timestamps-at-point nil
  1711. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1712. When nil, these labeled time stamps are forces into the second line of an
  1713. entry, just after the headline. When scheduling from the global TODO list,
  1714. the time stamp will always be forced into the second line."
  1715. :group 'org-time
  1716. :type 'boolean)
  1717. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1718. "Formats for `format-time-string' which are used for time stamps.
  1719. It is not recommended to change this constant.")
  1720. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1721. "Number of minutes to round time stamps to.
  1722. These are two values, the first applies when first creating a time stamp.
  1723. The second applies when changing it with the commands `S-up' and `S-down'.
  1724. When changing the time stamp, this means that it will change in steps
  1725. of N minutes, as given by the second value.
  1726. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1727. numbers should be factors of 60, so for example 5, 10, 15.
  1728. When this is larger than 1, you can still force an exact time-stamp by using
  1729. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1730. and by using a prefix arg to `S-up/down' to specify the exact number
  1731. of minutes to shift."
  1732. :group 'org-time
  1733. :get '(lambda (var) ; Make sure all entries have 5 elements
  1734. (if (integerp (default-value var))
  1735. (list (default-value var) 5)
  1736. (default-value var)))
  1737. :type '(list
  1738. (integer :tag "when inserting times")
  1739. (integer :tag "when modifying times")))
  1740. ;; Normalize old customizations of this variable.
  1741. (when (integerp org-time-stamp-rounding-minutes)
  1742. (setq org-time-stamp-rounding-minutes
  1743. (list org-time-stamp-rounding-minutes
  1744. org-time-stamp-rounding-minutes)))
  1745. (defcustom org-display-custom-times nil
  1746. "Non-nil means, overlay custom formats over all time stamps.
  1747. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1748. To turn this on on a per-file basis, insert anywhere in the file:
  1749. #+STARTUP: customtime"
  1750. :group 'org-time
  1751. :set 'set-default
  1752. :type 'sexp)
  1753. (make-variable-buffer-local 'org-display-custom-times)
  1754. (defcustom org-time-stamp-custom-formats
  1755. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1756. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1757. These are overlayed over the default ISO format if the variable
  1758. `org-display-custom-times' is set. Time like %H:%M should be at the
  1759. end of the second format."
  1760. :group 'org-time
  1761. :type 'sexp)
  1762. (defun org-time-stamp-format (&optional long inactive)
  1763. "Get the right format for a time string."
  1764. (let ((f (if long (cdr org-time-stamp-formats)
  1765. (car org-time-stamp-formats))))
  1766. (if inactive
  1767. (concat "[" (substring f 1 -1) "]")
  1768. f)))
  1769. (defcustom org-time-clocksum-format "%d:%02d"
  1770. "The format string used when creating CLOCKSUM lines, or when
  1771. org-mode generates a time duration."
  1772. :group 'org-time
  1773. :type 'string)
  1774. (defcustom org-deadline-warning-days 14
  1775. "No. of days before expiration during which a deadline becomes active.
  1776. This variable governs the display in sparse trees and in the agenda.
  1777. When 0 or negative, it means use this number (the absolute value of it)
  1778. even if a deadline has a different individual lead time specified.
  1779. Custom commands can set this variable in the options section."
  1780. :group 'org-time
  1781. :group 'org-agenda-daily/weekly
  1782. :type 'integer)
  1783. (defcustom org-read-date-prefer-future t
  1784. "Non-nil means, assume future for incomplete date input from user.
  1785. This affects the following situations:
  1786. 1. The user gives a day, but no month.
  1787. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1788. read this as the third of *next* month. However, if you enter \"17\",
  1789. it will be considered as *this* month.
  1790. 2. The user gives a month but not a year.
  1791. For example, if it is april and you enter \"feb 2\", this will be read
  1792. as feb 2, *next* year. \"May 5\", however, will be this year.
  1793. Currently this does not work for ISO week specifications.
  1794. When this option is nil, the current month and year will always be used
  1795. as defaults."
  1796. :group 'org-time
  1797. :type 'boolean)
  1798. (defcustom org-read-date-display-live t
  1799. "Non-nil means, display current interpretation of date prompt live.
  1800. This display will be in an overlay, in the minibuffer."
  1801. :group 'org-time
  1802. :type 'boolean)
  1803. (defcustom org-read-date-popup-calendar t
  1804. "Non-nil means, pop up a calendar when prompting for a date.
  1805. In the calendar, the date can be selected with mouse-1. However, the
  1806. minibuffer will also be active, and you can simply enter the date as well.
  1807. When nil, only the minibuffer will be available."
  1808. :group 'org-time
  1809. :type 'boolean)
  1810. (if (fboundp 'defvaralias)
  1811. (defvaralias 'org-popup-calendar-for-date-prompt
  1812. 'org-read-date-popup-calendar))
  1813. (defcustom org-read-date-minibuffer-setup-hook nil
  1814. "Hook to be used to set up keys for the date/time interface.
  1815. Add key definitions to `minibuffer-local-map', which will be a temporary
  1816. copy."
  1817. :group 'org-time
  1818. :type 'hook)
  1819. (defcustom org-extend-today-until 0
  1820. "The hour when your day really ends. Must be an integer.
  1821. This has influence for the following applications:
  1822. - When switching the agenda to \"today\". It it is still earlier than
  1823. the time given here, the day recognized as TODAY is actually yesterday.
  1824. - When a date is read from the user and it is still before the time given
  1825. here, the current date and time will be assumed to be yesterday, 23:59.
  1826. Also, timestamps inserted in remember templates follow this rule.
  1827. IMPORTANT: This is a feature whose implementation is and likely will
  1828. remain incomplete. Really, it is only here because past midnight seems to
  1829. be the favorite working time of John Wiegley :-)"
  1830. :group 'org-time
  1831. :type 'integer)
  1832. (defcustom org-edit-timestamp-down-means-later nil
  1833. "Non-nil means, S-down will increase the time in a time stamp.
  1834. When nil, S-up will increase."
  1835. :group 'org-time
  1836. :type 'boolean)
  1837. (defcustom org-calendar-follow-timestamp-change t
  1838. "Non-nil means, make the calendar window follow timestamp changes.
  1839. When a timestamp is modified and the calendar window is visible, it will be
  1840. moved to the new date."
  1841. :group 'org-time
  1842. :type 'boolean)
  1843. (defgroup org-tags nil
  1844. "Options concerning tags in Org-mode."
  1845. :tag "Org Tags"
  1846. :group 'org)
  1847. (defcustom org-tag-alist nil
  1848. "List of tags allowed in Org-mode files.
  1849. When this list is nil, Org-mode will base TAG input on what is already in the
  1850. buffer.
  1851. The value of this variable is an alist, the car of each entry must be a
  1852. keyword as a string, the cdr may be a character that is used to select
  1853. that tag through the fast-tag-selection interface.
  1854. See the manual for details."
  1855. :group 'org-tags
  1856. :type '(repeat
  1857. (choice
  1858. (cons (string :tag "Tag name")
  1859. (character :tag "Access char"))
  1860. (const :tag "Start radio group" (:startgroup))
  1861. (const :tag "End radio group" (:endgroup))
  1862. (const :tag "New line" (:newline)))))
  1863. (defcustom org-tag-persistent-alist nil
  1864. "List of tags that will always appear in all Org-mode files.
  1865. This is in addition to any in buffer settings or customizations
  1866. of `org-tag-alist'.
  1867. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1868. The value of this variable is an alist, the car of each entry must be a
  1869. keyword as a string, the cdr may be a character that is used to select
  1870. that tag through the fast-tag-selection interface.
  1871. See the manual for details.
  1872. To disable these tags on a per-file basis, insert anywhere in the file:
  1873. #+STARTUP: noptag"
  1874. :group 'org-tags
  1875. :type '(repeat
  1876. (choice
  1877. (cons (string :tag "Tag name")
  1878. (character :tag "Access char"))
  1879. (const :tag "Start radio group" (:startgroup))
  1880. (const :tag "End radio group" (:endgroup))
  1881. (const :tag "New line" (:newline)))))
  1882. (defvar org-file-tags nil
  1883. "List of tags that can be inherited by all entries in the file.
  1884. The tags will be inherited if the variable `org-use-tag-inheritance'
  1885. says they should be.
  1886. This variable is populated from #+TAG lines.")
  1887. (defcustom org-use-fast-tag-selection 'auto
  1888. "Non-nil means, use fast tag selection scheme.
  1889. This is a special interface to select and deselect tags with single keys.
  1890. When nil, fast selection is never used.
  1891. When the symbol `auto', fast selection is used if and only if selection
  1892. characters for tags have been configured, either through the variable
  1893. `org-tag-alist' or through a #+TAGS line in the buffer.
  1894. When t, fast selection is always used and selection keys are assigned
  1895. automatically if necessary."
  1896. :group 'org-tags
  1897. :type '(choice
  1898. (const :tag "Always" t)
  1899. (const :tag "Never" nil)
  1900. (const :tag "When selection characters are configured" 'auto)))
  1901. (defcustom org-fast-tag-selection-single-key nil
  1902. "Non-nil means, fast tag selection exits after first change.
  1903. When nil, you have to press RET to exit it.
  1904. During fast tag selection, you can toggle this flag with `C-c'.
  1905. This variable can also have the value `expert'. In this case, the window
  1906. displaying the tags menu is not even shown, until you press C-c again."
  1907. :group 'org-tags
  1908. :type '(choice
  1909. (const :tag "No" nil)
  1910. (const :tag "Yes" t)
  1911. (const :tag "Expert" expert)))
  1912. (defvar org-fast-tag-selection-include-todo nil
  1913. "Non-nil means, fast tags selection interface will also offer TODO states.
  1914. This is an undocumented feature, you should not rely on it.")
  1915. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1916. "The column to which tags should be indented in a headline.
  1917. If this number is positive, it specifies the column. If it is negative,
  1918. it means that the tags should be flushright to that column. For example,
  1919. -80 works well for a normal 80 character screen."
  1920. :group 'org-tags
  1921. :type 'integer)
  1922. (defcustom org-auto-align-tags t
  1923. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1924. These operations change the length of a headline and therefore shift
  1925. the tags around. With this options turned on, after each such operation
  1926. the tags are again aligned to `org-tags-column'."
  1927. :group 'org-tags
  1928. :type 'boolean)
  1929. (defcustom org-use-tag-inheritance t
  1930. "Non-nil means, tags in levels apply also for sublevels.
  1931. When nil, only the tags directly given in a specific line apply there.
  1932. This may also be a list of tags that should be inherited, or a regexp that
  1933. matches tags that should be inherited. Additional control is possible
  1934. with the variable `org-tags-exclude-from-inheritance' which gives an
  1935. explicit list of tags to be excluded from inheritance., even if the value of
  1936. `org-use-tag-inheritance' would select it for inheritance.
  1937. If this option is t, a match early-on in a tree can lead to a large
  1938. number of matches in the subtree when constructing the agenda or creating
  1939. a sparse tree. If you only want to see the first match in a tree during
  1940. a search, check out the variable `org-tags-match-list-sublevels'."
  1941. :group 'org-tags
  1942. :type '(choice
  1943. (const :tag "Not" nil)
  1944. (const :tag "Always" t)
  1945. (repeat :tag "Specific tags" (string :tag "Tag"))
  1946. (regexp :tag "Tags matched by regexp")))
  1947. (defcustom org-tags-exclude-from-inheritance nil
  1948. "List of tags that should never be inherited.
  1949. This is a way to exclude a few tags from inheritance. For way to do
  1950. the opposite, to actively allow inheritance for selected tags,
  1951. see the variable `org-use-tag-inheritance'."
  1952. :group 'org-tags
  1953. :type '(repeat (string :tag "Tag")))
  1954. (defun org-tag-inherit-p (tag)
  1955. "Check if TAG is one that should be inherited."
  1956. (cond
  1957. ((member tag org-tags-exclude-from-inheritance) nil)
  1958. ((eq org-use-tag-inheritance t) t)
  1959. ((not org-use-tag-inheritance) nil)
  1960. ((stringp org-use-tag-inheritance)
  1961. (string-match org-use-tag-inheritance tag))
  1962. ((listp org-use-tag-inheritance)
  1963. (member tag org-use-tag-inheritance))
  1964. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1965. (defcustom org-tags-match-list-sublevels t
  1966. "Non-nil means list also sublevels of headlines matching a search.
  1967. This variable applies to tags/property searches, and also to stuck
  1968. projects because this search is based on a tags match as well.
  1969. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1970. the sublevels of a headline matching a tag search often also match
  1971. the same search. Listing all of them can create very long lists.
  1972. Setting this variable to nil causes subtrees of a match to be skipped.
  1973. As a special case, if the tag search is restricted to TODO items, the
  1974. value of this variable is ignored and sublevels are always checked, to
  1975. make sure all corresponding TODO items find their way into the list.
  1976. This variable is semi-obsolete and probably should always be true. It
  1977. is better to limit inheritance to certain tags using the variables
  1978. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1979. :group 'org-tags
  1980. :type '(choice
  1981. (const :tag "No, don't list them" nil)
  1982. (const :tag "Yes, do list them" t)
  1983. (const :tag "List them, indented with leading dots" indented)))
  1984. (defvar org-tags-history nil
  1985. "History of minibuffer reads for tags.")
  1986. (defvar org-last-tags-completion-table nil
  1987. "The last used completion table for tags.")
  1988. (defvar org-after-tags-change-hook nil
  1989. "Hook that is run after the tags in a line have changed.")
  1990. (defgroup org-properties nil
  1991. "Options concerning properties in Org-mode."
  1992. :tag "Org Properties"
  1993. :group 'org)
  1994. (defcustom org-property-format "%-10s %s"
  1995. "How property key/value pairs should be formatted by `indent-line'.
  1996. When `indent-line' hits a property definition, it will format the line
  1997. according to this format, mainly to make sure that the values are
  1998. lined-up with respect to each other."
  1999. :group 'org-properties
  2000. :type 'string)
  2001. (defcustom org-use-property-inheritance nil
  2002. "Non-nil means, properties apply also for sublevels.
  2003. This setting is chiefly used during property searches. Turning it on can
  2004. cause significant overhead when doing a search, which is why it is not
  2005. on by default.
  2006. When nil, only the properties directly given in the current entry count.
  2007. When t, every property is inherited. The value may also be a list of
  2008. properties that should have inheritance, or a regular expression matching
  2009. properties that should be inherited.
  2010. However, note that some special properties use inheritance under special
  2011. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2012. and the properties ending in \"_ALL\" when they are used as descriptor
  2013. for valid values of a property.
  2014. Note for programmers:
  2015. When querying an entry with `org-entry-get', you can control if inheritance
  2016. should be used. By default, `org-entry-get' looks only at the local
  2017. properties. You can request inheritance by setting the inherit argument
  2018. to t (to force inheritance) or to `selective' (to respect the setting
  2019. in this variable)."
  2020. :group 'org-properties
  2021. :type '(choice
  2022. (const :tag "Not" nil)
  2023. (const :tag "Always" t)
  2024. (repeat :tag "Specific properties" (string :tag "Property"))
  2025. (regexp :tag "Properties matched by regexp")))
  2026. (defun org-property-inherit-p (property)
  2027. "Check if PROPERTY is one that should be inherited."
  2028. (cond
  2029. ((eq org-use-property-inheritance t) t)
  2030. ((not org-use-property-inheritance) nil)
  2031. ((stringp org-use-property-inheritance)
  2032. (string-match org-use-property-inheritance property))
  2033. ((listp org-use-property-inheritance)
  2034. (member property org-use-property-inheritance))
  2035. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2036. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2037. "The default column format, if no other format has been defined.
  2038. This variable can be set on the per-file basis by inserting a line
  2039. #+COLUMNS: %25ITEM ....."
  2040. :group 'org-properties
  2041. :type 'string)
  2042. (defcustom org-columns-ellipses ".."
  2043. "The ellipses to be used when a field in column view is truncated.
  2044. When this is the empty string, as many characters as possible are shown,
  2045. but then there will be no visual indication that the field has been truncated.
  2046. When this is a string of length N, the last N characters of a truncated
  2047. field are replaced by this string. If the column is narrower than the
  2048. ellipses string, only part of the ellipses string will be shown."
  2049. :group 'org-properties
  2050. :type 'string)
  2051. (defcustom org-columns-modify-value-for-display-function nil
  2052. "Function that modifies values for display in column view.
  2053. For example, it can be used to cut out a certain part from a time stamp.
  2054. The function must take 2 arguments:
  2055. column-title The title of the column (*not* the property name)
  2056. value The value that should be modified.
  2057. The function should return the value that should be displayed,
  2058. or nil if the normal value should be used."
  2059. :group 'org-properties
  2060. :type 'function)
  2061. (defcustom org-effort-property "Effort"
  2062. "The property that is being used to keep track of effort estimates.
  2063. Effort estimates given in this property need to have the format H:MM."
  2064. :group 'org-properties
  2065. :group 'org-progress
  2066. :type '(string :tag "Property"))
  2067. (defconst org-global-properties-fixed
  2068. '(("VISIBILITY_ALL" . "folded children content all"))
  2069. "List of property/value pairs that can be inherited by any entry.
  2070. These are fixed values, for the preset properties. The user variable
  2071. that can be used to add to this list is `org-global-properties'.
  2072. The entries in this list are cons cells where the car is a property
  2073. name and cdr is a string with the value. If the value represents
  2074. multiple items like an \"_ALL\" property, separate the items by
  2075. spaces.")
  2076. (defcustom org-global-properties nil
  2077. "List of property/value pairs that can be inherited by any entry.
  2078. This list will be combined with the constant `org-global-properties-fixed'.
  2079. The entries in this list are cons cells where the car is a property
  2080. name and cdr is a string with the value.
  2081. You can set buffer-local values for the same purpose in the variable
  2082. `org-file-properties' this by adding lines like
  2083. #+PROPERTY: NAME VALUE"
  2084. :group 'org-properties
  2085. :type '(repeat
  2086. (cons (string :tag "Property")
  2087. (string :tag "Value"))))
  2088. (defvar org-file-properties nil
  2089. "List of property/value pairs that can be inherited by any entry.
  2090. Valid for the current buffer.
  2091. This variable is populated from #+PROPERTY lines.")
  2092. (make-variable-buffer-local 'org-file-properties)
  2093. (defgroup org-agenda nil
  2094. "Options concerning agenda views in Org-mode."
  2095. :tag "Org Agenda"
  2096. :group 'org)
  2097. (defvar org-category nil
  2098. "Variable used by org files to set a category for agenda display.
  2099. Such files should use a file variable to set it, for example
  2100. # -*- mode: org; org-category: \"ELisp\"
  2101. or contain a special line
  2102. #+CATEGORY: ELisp
  2103. If the file does not specify a category, then file's base name
  2104. is used instead.")
  2105. (make-variable-buffer-local 'org-category)
  2106. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2107. (defcustom org-agenda-files nil
  2108. "The files to be used for agenda display.
  2109. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2110. \\[org-remove-file]. You can also use customize to edit the list.
  2111. If an entry is a directory, all files in that directory that are matched by
  2112. `org-agenda-file-regexp' will be part of the file list.
  2113. If the value of the variable is not a list but a single file name, then
  2114. the list of agenda files is actually stored and maintained in that file, one
  2115. agenda file per line."
  2116. :group 'org-agenda
  2117. :type '(choice
  2118. (repeat :tag "List of files and directories" file)
  2119. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2120. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2121. "Regular expression to match files for `org-agenda-files'.
  2122. If any element in the list in that variable contains a directory instead
  2123. of a normal file, all files in that directory that are matched by this
  2124. regular expression will be included."
  2125. :group 'org-agenda
  2126. :type 'regexp)
  2127. (defcustom org-agenda-text-search-extra-files nil
  2128. "List of extra files to be searched by text search commands.
  2129. These files will be search in addition to the agenda files by the
  2130. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2131. Note that these files will only be searched for text search commands,
  2132. not for the other agenda views like todo lists, tag searches or the weekly
  2133. agenda. This variable is intended to list notes and possibly archive files
  2134. that should also be searched by these two commands.
  2135. In fact, if the first element in the list is the symbol `agenda-archives',
  2136. than all archive files of all agenda files will be added to the search
  2137. scope."
  2138. :group 'org-agenda
  2139. :type '(set :greedy t
  2140. (const :tag "Agenda Archives" agenda-archives)
  2141. (repeat :inline t (file))))
  2142. (if (fboundp 'defvaralias)
  2143. (defvaralias 'org-agenda-multi-occur-extra-files
  2144. 'org-agenda-text-search-extra-files))
  2145. (defcustom org-agenda-skip-unavailable-files nil
  2146. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2147. A nil value means to remove them, after a query, from the list."
  2148. :group 'org-agenda
  2149. :type 'boolean)
  2150. (defcustom org-calendar-to-agenda-key [?c]
  2151. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2152. The command `org-calendar-goto-agenda' will be bound to this key. The
  2153. default is the character `c' because then `c' can be used to switch back and
  2154. forth between agenda and calendar."
  2155. :group 'org-agenda
  2156. :type 'sexp)
  2157. (defcustom org-calendar-agenda-action-key [?k]
  2158. "The key to be installed in `calendar-mode-map' for agenda-action.
  2159. The command `org-agenda-action' will be bound to this key. The
  2160. default is the character `k' because we use the same key in the agenda."
  2161. :group 'org-agenda
  2162. :type 'sexp)
  2163. (eval-after-load "calendar"
  2164. '(progn
  2165. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2166. 'org-calendar-goto-agenda)
  2167. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2168. 'org-agenda-action)))
  2169. (defgroup org-latex nil
  2170. "Options for embedding LaTeX code into Org-mode."
  2171. :tag "Org LaTeX"
  2172. :group 'org)
  2173. (defcustom org-format-latex-options
  2174. '(:foreground default :background default :scale 1.0
  2175. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2176. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2177. "Options for creating images from LaTeX fragments.
  2178. This is a property list with the following properties:
  2179. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2180. `default' means use the foreground of the default face.
  2181. :background the background color, or \"Transparent\".
  2182. `default' means use the background of the default face.
  2183. :scale a scaling factor for the size of the images.
  2184. :html-foreground, :html-background, :html-scale
  2185. the same numbers for HTML export.
  2186. :matchers a list indicating which matchers should be used to
  2187. find LaTeX fragments. Valid members of this list are:
  2188. \"begin\" find environments
  2189. \"$1\" find single characters surrounded by $.$
  2190. \"$\" find math expressions surrounded by $...$
  2191. \"$$\" find math expressions surrounded by $$....$$
  2192. \"\\(\" find math expressions surrounded by \\(...\\)
  2193. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2194. :group 'org-latex
  2195. :type 'plist)
  2196. (defcustom org-format-latex-header "\\documentclass{article}
  2197. \\usepackage{fullpage} % do not remove
  2198. \\usepackage{amssymb}
  2199. \\usepackage[usenames]{color}
  2200. \\usepackage{amsmath}
  2201. \\usepackage{latexsym}
  2202. \\usepackage[mathscr]{eucal}
  2203. \\pagestyle{empty} % do not remove"
  2204. "The document header used for processing LaTeX fragments."
  2205. :group 'org-latex
  2206. :type 'string)
  2207. (defgroup org-font-lock nil
  2208. "Font-lock settings for highlighting in Org-mode."
  2209. :tag "Org Font Lock"
  2210. :group 'org)
  2211. (defcustom org-level-color-stars-only nil
  2212. "Non-nil means fontify only the stars in each headline.
  2213. When nil, the entire headline is fontified.
  2214. Changing it requires restart of `font-lock-mode' to become effective
  2215. also in regions already fontified."
  2216. :group 'org-font-lock
  2217. :type 'boolean)
  2218. (defcustom org-hide-leading-stars nil
  2219. "Non-nil means, hide the first N-1 stars in a headline.
  2220. This works by using the face `org-hide' for these stars. This
  2221. face is white for a light background, and black for a dark
  2222. background. You may have to customize the face `org-hide' to
  2223. make this work.
  2224. Changing it requires restart of `font-lock-mode' to become effective
  2225. also in regions already fontified.
  2226. You may also set this on a per-file basis by adding one of the following
  2227. lines to the buffer:
  2228. #+STARTUP: hidestars
  2229. #+STARTUP: showstars"
  2230. :group 'org-font-lock
  2231. :type 'boolean)
  2232. (defcustom org-fontify-done-headline nil
  2233. "Non-nil means, change the face of a headline if it is marked DONE.
  2234. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2235. When this is non-nil, the headline after the keyword is set to the
  2236. `org-headline-done' as an additional indication."
  2237. :group 'org-font-lock
  2238. :type 'boolean)
  2239. (defcustom org-fontify-emphasized-text t
  2240. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2241. Changing this variable requires a restart of Emacs to take effect."
  2242. :group 'org-font-lock
  2243. :type 'boolean)
  2244. (defcustom org-highlight-latex-fragments-and-specials nil
  2245. "Non-nil means, fontify what is treated specially by the exporters."
  2246. :group 'org-font-lock
  2247. :type 'boolean)
  2248. (defcustom org-hide-emphasis-markers nil
  2249. "Non-nil mean font-lock should hide the emphasis marker characters."
  2250. :group 'org-font-lock
  2251. :type 'boolean)
  2252. (defvar org-emph-re nil
  2253. "Regular expression for matching emphasis.")
  2254. (defvar org-verbatim-re nil
  2255. "Regular expression for matching verbatim text.")
  2256. (defvar org-emphasis-regexp-components) ; defined just below
  2257. (defvar org-emphasis-alist) ; defined just below
  2258. (defun org-set-emph-re (var val)
  2259. "Set variable and compute the emphasis regular expression."
  2260. (set var val)
  2261. (when (and (boundp 'org-emphasis-alist)
  2262. (boundp 'org-emphasis-regexp-components)
  2263. org-emphasis-alist org-emphasis-regexp-components)
  2264. (let* ((e org-emphasis-regexp-components)
  2265. (pre (car e))
  2266. (post (nth 1 e))
  2267. (border (nth 2 e))
  2268. (body (nth 3 e))
  2269. (nl (nth 4 e))
  2270. (body1 (concat body "*?"))
  2271. (markers (mapconcat 'car org-emphasis-alist ""))
  2272. (vmarkers (mapconcat
  2273. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2274. org-emphasis-alist "")))
  2275. ;; make sure special characters appear at the right position in the class
  2276. (if (string-match "\\^" markers)
  2277. (setq markers (concat (replace-match "" t t markers) "^")))
  2278. (if (string-match "-" markers)
  2279. (setq markers (concat (replace-match "" t t markers) "-")))
  2280. (if (string-match "\\^" vmarkers)
  2281. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2282. (if (string-match "-" vmarkers)
  2283. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2284. (if (> nl 0)
  2285. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2286. (int-to-string nl) "\\}")))
  2287. ;; Make the regexp
  2288. (setq org-emph-re
  2289. (concat "\\([" pre "]\\|^\\)"
  2290. "\\("
  2291. "\\([" markers "]\\)"
  2292. "\\("
  2293. "[^" border "]\\|"
  2294. "[^" border "]"
  2295. body1
  2296. "[^" border "]"
  2297. "\\)"
  2298. "\\3\\)"
  2299. "\\([" post "]\\|$\\)"))
  2300. (setq org-verbatim-re
  2301. (concat "\\([" pre "]\\|^\\)"
  2302. "\\("
  2303. "\\([" vmarkers "]\\)"
  2304. "\\("
  2305. "[^" border "]\\|"
  2306. "[^" border "]"
  2307. body1
  2308. "[^" border "]"
  2309. "\\)"
  2310. "\\3\\)"
  2311. "\\([" post "]\\|$\\)")))))
  2312. (defcustom org-emphasis-regexp-components
  2313. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2314. "Components used to build the regular expression for emphasis.
  2315. This is a list with 6 entries. Terminology: In an emphasis string
  2316. like \" *strong word* \", we call the initial space PREMATCH, the final
  2317. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2318. and \"trong wor\" is the body. The different components in this variable
  2319. specify what is allowed/forbidden in each part:
  2320. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2321. post Chars allowed as postmatch. End of line will be allowed too.
  2322. border The chars *forbidden* as border characters.
  2323. body-regexp A regexp like \".\" to match a body character. Don't use
  2324. non-shy groups here, and don't allow newline here.
  2325. newline The maximum number of newlines allowed in an emphasis exp.
  2326. Use customize to modify this, or restart Emacs after changing it."
  2327. :group 'org-font-lock
  2328. :set 'org-set-emph-re
  2329. :type '(list
  2330. (sexp :tag "Allowed chars in pre ")
  2331. (sexp :tag "Allowed chars in post ")
  2332. (sexp :tag "Forbidden chars in border ")
  2333. (sexp :tag "Regexp for body ")
  2334. (integer :tag "number of newlines allowed")
  2335. (option (boolean :tag "Please ignore this button"))))
  2336. (defcustom org-emphasis-alist
  2337. `(("*" bold "<b>" "</b>")
  2338. ("/" italic "<i>" "</i>")
  2339. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2340. ("=" org-code "<code>" "</code>" verbatim)
  2341. ("~" org-verbatim "<code>" "</code>" verbatim)
  2342. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2343. "<del>" "</del>")
  2344. )
  2345. "Special syntax for emphasized text.
  2346. Text starting and ending with a special character will be emphasized, for
  2347. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2348. characters, the face to be used by font-lock for highlighting in Org-mode
  2349. Emacs buffers, and the HTML tags to be used for this.
  2350. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2351. Use customize to modify this, or restart Emacs after changing it."
  2352. :group 'org-font-lock
  2353. :set 'org-set-emph-re
  2354. :type '(repeat
  2355. (list
  2356. (string :tag "Marker character")
  2357. (choice
  2358. (face :tag "Font-lock-face")
  2359. (plist :tag "Face property list"))
  2360. (string :tag "HTML start tag")
  2361. (string :tag "HTML end tag")
  2362. (option (const verbatim)))))
  2363. ;;; Miscellaneous options
  2364. (defgroup org-completion nil
  2365. "Completion in Org-mode."
  2366. :tag "Org Completion"
  2367. :group 'org)
  2368. (defcustom org-completion-use-ido nil
  2369. "Non-nil means, use ido completion wherever possible.
  2370. Note that `ido-mode' must be active for this variable to be relevant.
  2371. If you decide to turn this variable on, you might well want to turn off
  2372. `org-outline-path-complete-in-steps'."
  2373. :group 'org-completion
  2374. :type 'boolean)
  2375. (defcustom org-completion-fallback-command 'hippie-expand
  2376. "The expansion command called by \\[org-complete] in normal context.
  2377. Normal means, no org-mode-specific context."
  2378. :group 'org-completion
  2379. :type 'function)
  2380. ;;; Functions and variables from ther packages
  2381. ;; Declared here to avoid compiler warnings
  2382. ;; XEmacs only
  2383. (defvar outline-mode-menu-heading)
  2384. (defvar outline-mode-menu-show)
  2385. (defvar outline-mode-menu-hide)
  2386. (defvar zmacs-regions) ; XEmacs regions
  2387. ;; Emacs only
  2388. (defvar mark-active)
  2389. ;; Various packages
  2390. (declare-function find-library-name "find-func" (library))
  2391. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2392. (declare-function calendar-forward-day "cal-move" (arg))
  2393. (declare-function calendar-goto-date "cal-move" (date))
  2394. (declare-function calendar-goto-today "cal-move" ())
  2395. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2396. (defvar calc-embedded-close-formula)
  2397. (defvar calc-embedded-open-formula)
  2398. (declare-function cdlatex-tab "ext:cdlatex" ())
  2399. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2400. (defvar font-lock-unfontify-region-function)
  2401. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2402. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2403. (defvar iswitchb-temp-buflist)
  2404. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2405. (defvar org-agenda-tags-todo-honor-ignore-options)
  2406. (declare-function org-agenda-skip "org-agenda" ())
  2407. (declare-function org-format-agenda-item "org-agenda"
  2408. (extra txt &optional category tags dotime noprefix remove-re))
  2409. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2410. (declare-function org-agenda-change-all-lines "org-agenda"
  2411. (newhead hdmarker &optional fixface just-this))
  2412. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2413. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2414. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2415. (beg end))
  2416. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2417. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2418. "org-agenda" (&optional end))
  2419. (declare-function parse-time-string "parse-time" (string))
  2420. (declare-function remember "remember" (&optional initial))
  2421. (declare-function remember-buffer-desc "remember" ())
  2422. (declare-function remember-finalize "remember" ())
  2423. (defvar remember-save-after-remembering)
  2424. (defvar remember-data-file)
  2425. (defvar remember-register)
  2426. (defvar remember-buffer)
  2427. (defvar remember-handler-functions)
  2428. (defvar remember-annotation-functions)
  2429. (defvar texmathp-why)
  2430. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2431. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2432. (defvar w3m-current-url)
  2433. (defvar w3m-current-title)
  2434. (defvar org-latex-regexps)
  2435. ;;; Autoload and prepare some org modules
  2436. ;; Some table stuff that needs to be defined here, because it is used
  2437. ;; by the functions setting up org-mode or checking for table context.
  2438. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2439. "Detects an org-type or table-type table.")
  2440. (defconst org-table-line-regexp "^[ \t]*|"
  2441. "Detects an org-type table line.")
  2442. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2443. "Detects an org-type table line.")
  2444. (defconst org-table-hline-regexp "^[ \t]*|-"
  2445. "Detects an org-type table hline.")
  2446. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2447. "Detects a table-type table hline.")
  2448. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2449. "Searching from within a table (any type) this finds the first line
  2450. outside the table.")
  2451. ;; Autoload the functions in org-table.el that are needed by functions here.
  2452. (eval-and-compile
  2453. (org-autoload "org-table"
  2454. '(org-table-align org-table-begin org-table-blank-field
  2455. org-table-convert org-table-convert-region org-table-copy-down
  2456. org-table-copy-region org-table-create
  2457. org-table-create-or-convert-from-region
  2458. org-table-create-with-table.el org-table-current-dline
  2459. org-table-cut-region org-table-delete-column org-table-edit-field
  2460. org-table-edit-formulas org-table-end org-table-eval-formula
  2461. org-table-export org-table-field-info
  2462. org-table-get-stored-formulas org-table-goto-column
  2463. org-table-hline-and-move org-table-import org-table-insert-column
  2464. org-table-insert-hline org-table-insert-row org-table-iterate
  2465. org-table-justify-field-maybe org-table-kill-row
  2466. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2467. org-table-move-column org-table-move-column-left
  2468. org-table-move-column-right org-table-move-row
  2469. org-table-move-row-down org-table-move-row-up
  2470. org-table-next-field org-table-next-row org-table-paste-rectangle
  2471. org-table-previous-field org-table-recalculate
  2472. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2473. org-table-toggle-coordinate-overlays
  2474. org-table-toggle-formula-debugger org-table-wrap-region
  2475. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2476. (defun org-at-table-p (&optional table-type)
  2477. "Return t if the cursor is inside an org-type table.
  2478. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2479. (if org-enable-table-editor
  2480. (save-excursion
  2481. (beginning-of-line 1)
  2482. (looking-at (if table-type org-table-any-line-regexp
  2483. org-table-line-regexp)))
  2484. nil))
  2485. (defsubst org-table-p () (org-at-table-p))
  2486. (defun org-at-table.el-p ()
  2487. "Return t if and only if we are at a table.el table."
  2488. (and (org-at-table-p 'any)
  2489. (save-excursion
  2490. (goto-char (org-table-begin 'any))
  2491. (looking-at org-table1-hline-regexp))))
  2492. (defun org-table-recognize-table.el ()
  2493. "If there is a table.el table nearby, recognize it and move into it."
  2494. (if org-table-tab-recognizes-table.el
  2495. (if (org-at-table.el-p)
  2496. (progn
  2497. (beginning-of-line 1)
  2498. (if (looking-at org-table-dataline-regexp)
  2499. nil
  2500. (if (looking-at org-table1-hline-regexp)
  2501. (progn
  2502. (beginning-of-line 2)
  2503. (if (looking-at org-table-any-border-regexp)
  2504. (beginning-of-line -1)))))
  2505. (if (re-search-forward "|" (org-table-end t) t)
  2506. (progn
  2507. (require 'table)
  2508. (if (table--at-cell-p (point))
  2509. t
  2510. (message "recognizing table.el table...")
  2511. (table-recognize-table)
  2512. (message "recognizing table.el table...done")))
  2513. (error "This should not happen..."))
  2514. t)
  2515. nil)
  2516. nil))
  2517. (defun org-at-table-hline-p ()
  2518. "Return t if the cursor is inside a hline in a table."
  2519. (if org-enable-table-editor
  2520. (save-excursion
  2521. (beginning-of-line 1)
  2522. (looking-at org-table-hline-regexp))
  2523. nil))
  2524. (defvar org-table-clean-did-remove-column nil)
  2525. (defun org-table-map-tables (function)
  2526. "Apply FUNCTION to the start of all tables in the buffer."
  2527. (save-excursion
  2528. (save-restriction
  2529. (widen)
  2530. (goto-char (point-min))
  2531. (while (re-search-forward org-table-any-line-regexp nil t)
  2532. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2533. (beginning-of-line 1)
  2534. (if (looking-at org-table-line-regexp)
  2535. (save-excursion (funcall function)))
  2536. (re-search-forward org-table-any-border-regexp nil 1))))
  2537. (message "Mapping tables: done"))
  2538. ;; Declare and autoload functions from org-exp.el
  2539. (declare-function org-default-export-plist "org-exp")
  2540. (declare-function org-infile-export-plist "org-exp")
  2541. (declare-function org-get-current-options "org-exp")
  2542. (eval-and-compile
  2543. (org-autoload "org-exp"
  2544. '(org-export org-export-as-ascii org-export-visible
  2545. org-insert-export-options-template org-export-as-html-and-open
  2546. org-export-as-html-batch org-export-as-html-to-buffer
  2547. org-replace-region-by-html org-export-region-as-html
  2548. org-export-as-html org-export-icalendar-this-file
  2549. org-export-icalendar-all-agenda-files
  2550. org-table-clean-before-export
  2551. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2552. ;; Declare and autoload functions from org-agenda.el
  2553. (eval-and-compile
  2554. (org-autoload "org-agenda"
  2555. '(org-agenda org-agenda-list org-search-view
  2556. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2557. org-diary org-agenda-to-appt
  2558. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2559. ;; Autoload org-remember
  2560. (eval-and-compile
  2561. (org-autoload "org-remember"
  2562. '(org-remember-insinuate org-remember-annotation
  2563. org-remember-apply-template org-remember org-remember-handler)))
  2564. ;; Autoload org-clock.el
  2565. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2566. (beg end))
  2567. (declare-function org-clock-update-mode-line "org-clock" ())
  2568. (defvar org-clock-start-time)
  2569. (defvar org-clock-marker (make-marker)
  2570. "Marker recording the last clock-in.")
  2571. (eval-and-compile
  2572. (org-autoload
  2573. "org-clock"
  2574. '(org-clock-in org-clock-out org-clock-cancel
  2575. org-clock-goto org-clock-sum org-clock-display
  2576. org-clock-remove-overlays org-clock-report
  2577. org-clocktable-shift org-dblock-write:clocktable
  2578. org-get-clocktable)))
  2579. (defun org-clock-update-time-maybe ()
  2580. "If this is a CLOCK line, update it and return t.
  2581. Otherwise, return nil."
  2582. (interactive)
  2583. (save-excursion
  2584. (beginning-of-line 1)
  2585. (skip-chars-forward " \t")
  2586. (when (looking-at org-clock-string)
  2587. (let ((re (concat "[ \t]*" org-clock-string
  2588. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2589. "\\([ \t]*=>.*\\)?\\)?"))
  2590. ts te h m s neg)
  2591. (cond
  2592. ((not (looking-at re))
  2593. nil)
  2594. ((not (match-end 2))
  2595. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2596. (> org-clock-marker (point))
  2597. (<= org-clock-marker (point-at-eol)))
  2598. ;; The clock is running here
  2599. (setq org-clock-start-time
  2600. (apply 'encode-time
  2601. (org-parse-time-string (match-string 1))))
  2602. (org-clock-update-mode-line)))
  2603. (t
  2604. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2605. (end-of-line 1)
  2606. (setq ts (match-string 1)
  2607. te (match-string 3))
  2608. (setq s (- (time-to-seconds
  2609. (apply 'encode-time (org-parse-time-string te)))
  2610. (time-to-seconds
  2611. (apply 'encode-time (org-parse-time-string ts))))
  2612. neg (< s 0)
  2613. s (abs s)
  2614. h (floor (/ s 3600))
  2615. s (- s (* 3600 h))
  2616. m (floor (/ s 60))
  2617. s (- s (* 60 s)))
  2618. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2619. t))))))
  2620. (defun org-check-running-clock ()
  2621. "Check if the current buffer contains the running clock.
  2622. If yes, offer to stop it and to save the buffer with the changes."
  2623. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2624. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2625. (buffer-name))))
  2626. (org-clock-out)
  2627. (when (y-or-n-p "Save changed buffer?")
  2628. (save-buffer))))
  2629. (defun org-clocktable-try-shift (dir n)
  2630. "Check if this line starts a clock table, if yes, shift the time block."
  2631. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2632. (org-clocktable-shift dir n)))
  2633. ;; Autoload org-timer.el
  2634. (eval-and-compile
  2635. (org-autoload
  2636. "org-timer"
  2637. '(org-timer-start org-timer org-timer-item
  2638. org-timer-change-times-in-region)))
  2639. ;; Autoload org-feed.el
  2640. (eval-and-compile
  2641. (org-autoload
  2642. "org-feed"
  2643. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2644. ;; Autoload archiving code
  2645. ;; The stuff that is needed for cycling and tags has to be defined here.
  2646. (defgroup org-archive nil
  2647. "Options concerning archiving in Org-mode."
  2648. :tag "Org Archive"
  2649. :group 'org-structure)
  2650. (defcustom org-archive-location "%s_archive::"
  2651. "The location where subtrees should be archived.
  2652. The value of this variable is a string, consisting of two parts,
  2653. separated by a double-colon. The first part is a filename and
  2654. the second part is a headline.
  2655. When the filename is omitted, archiving happens in the same file.
  2656. %s in the filename will be replaced by the current file
  2657. name (without the directory part). Archiving to a different file
  2658. is useful to keep archived entries from contributing to the
  2659. Org-mode Agenda.
  2660. The archived entries will be filed as subtrees of the specified
  2661. headline. When the headline is omitted, the subtrees are simply
  2662. filed away at the end of the file, as top-level entries. Also in
  2663. the heading you can use %s to represent the file name, this can be
  2664. useful when using the same archive for a number of different files.
  2665. Here are a few examples:
  2666. \"%s_archive::\"
  2667. If the current file is Projects.org, archive in file
  2668. Projects.org_archive, as top-level trees. This is the default.
  2669. \"::* Archived Tasks\"
  2670. Archive in the current file, under the top-level headline
  2671. \"* Archived Tasks\".
  2672. \"~/org/archive.org::\"
  2673. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2674. \"~/org/archive.org::From %s\"
  2675. Archive in file ~/org/archive.org (absolute path), und headlines
  2676. \"From FILENAME\" where file name is the current file name.
  2677. \"basement::** Finished Tasks\"
  2678. Archive in file ./basement (relative path), as level 3 trees
  2679. below the level 2 heading \"** Finished Tasks\".
  2680. You may set this option on a per-file basis by adding to the buffer a
  2681. line like
  2682. #+ARCHIVE: basement::** Finished Tasks
  2683. You may also define it locally for a subtree by setting an ARCHIVE property
  2684. in the entry. If such a property is found in an entry, or anywhere up
  2685. the hierarchy, it will be used."
  2686. :group 'org-archive
  2687. :type 'string)
  2688. (defcustom org-archive-tag "ARCHIVE"
  2689. "The tag that marks a subtree as archived.
  2690. An archived subtree does not open during visibility cycling, and does
  2691. not contribute to the agenda listings.
  2692. After changing this, font-lock must be restarted in the relevant buffers to
  2693. get the proper fontification."
  2694. :group 'org-archive
  2695. :group 'org-keywords
  2696. :type 'string)
  2697. (defcustom org-agenda-skip-archived-trees t
  2698. "Non-nil means, the agenda will skip any items located in archived trees.
  2699. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2700. variable is no longer recommended, you should leave it at the value t.
  2701. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2702. :group 'org-archive
  2703. :group 'org-agenda-skip
  2704. :type 'boolean)
  2705. (defcustom org-columns-skip-arrchived-trees t
  2706. "Non-nil means, irgnore archived trees when creating column view."
  2707. :group 'org-archive
  2708. :group 'org-properties
  2709. :type 'boolean)
  2710. (defcustom org-cycle-open-archived-trees nil
  2711. "Non-nil means, `org-cycle' will open archived trees.
  2712. An archived tree is a tree marked with the tag ARCHIVE.
  2713. When nil, archived trees will stay folded. You can still open them with
  2714. normal outline commands like `show-all', but not with the cycling commands."
  2715. :group 'org-archive
  2716. :group 'org-cycle
  2717. :type 'boolean)
  2718. (defcustom org-sparse-tree-open-archived-trees nil
  2719. "Non-nil means sparse tree construction shows matches in archived trees.
  2720. When nil, matches in these trees are highlighted, but the trees are kept in
  2721. collapsed state."
  2722. :group 'org-archive
  2723. :group 'org-sparse-trees
  2724. :type 'boolean)
  2725. (defun org-cycle-hide-archived-subtrees (state)
  2726. "Re-hide all archived subtrees after a visibility state change."
  2727. (when (and (not org-cycle-open-archived-trees)
  2728. (not (memq state '(overview folded))))
  2729. (save-excursion
  2730. (let* ((globalp (memq state '(contents all)))
  2731. (beg (if globalp (point-min) (point)))
  2732. (end (if globalp (point-max) (org-end-of-subtree t))))
  2733. (org-hide-archived-subtrees beg end)
  2734. (goto-char beg)
  2735. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2736. (message "%s" (substitute-command-keys
  2737. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2738. (defun org-force-cycle-archived ()
  2739. "Cycle subtree even if it is archived."
  2740. (interactive)
  2741. (setq this-command 'org-cycle)
  2742. (let ((org-cycle-open-archived-trees t))
  2743. (call-interactively 'org-cycle)))
  2744. (defun org-hide-archived-subtrees (beg end)
  2745. "Re-hide all archived subtrees after a visibility state change."
  2746. (save-excursion
  2747. (let* ((re (concat ":" org-archive-tag ":")))
  2748. (goto-char beg)
  2749. (while (re-search-forward re end t)
  2750. (and (org-on-heading-p) (hide-subtree))
  2751. (org-end-of-subtree t)))))
  2752. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2753. (eval-and-compile
  2754. (org-autoload "org-archive"
  2755. '(org-add-archive-files org-archive-subtree
  2756. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2757. ;; Autoload Column View Code
  2758. (declare-function org-columns-number-to-string "org-colview")
  2759. (declare-function org-columns-get-format-and-top-level "org-colview")
  2760. (declare-function org-columns-compute "org-colview")
  2761. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2762. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2763. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2764. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2765. ;; Autoload ID code
  2766. (declare-function org-id-store-link "org-id")
  2767. (declare-function org-id-locations-load "org-id")
  2768. (declare-function org-id-locations-save "org-id")
  2769. (defvar org-id-track-globally)
  2770. (org-autoload "org-id"
  2771. '(org-id-get-create org-id-new org-id-copy org-id-get
  2772. org-id-get-with-outline-path-completion
  2773. org-id-get-with-outline-drilling
  2774. org-id-goto org-id-find org-id-store-link))
  2775. ;; Autoload Plotting Code
  2776. (org-autoload "org-plot"
  2777. '(org-plot/gnuplot))
  2778. ;;; Variables for pre-computed regular expressions, all buffer local
  2779. (defvar org-drawer-regexp nil
  2780. "Matches first line of a hidden block.")
  2781. (make-variable-buffer-local 'org-drawer-regexp)
  2782. (defvar org-todo-regexp nil
  2783. "Matches any of the TODO state keywords.")
  2784. (make-variable-buffer-local 'org-todo-regexp)
  2785. (defvar org-not-done-regexp nil
  2786. "Matches any of the TODO state keywords except the last one.")
  2787. (make-variable-buffer-local 'org-not-done-regexp)
  2788. (defvar org-not-done-heading-regexp nil
  2789. "Matches a TODO headline that is not done.")
  2790. (make-variable-buffer-local 'org-not-done-regexp)
  2791. (defvar org-todo-line-regexp nil
  2792. "Matches a headline and puts TODO state into group 2 if present.")
  2793. (make-variable-buffer-local 'org-todo-line-regexp)
  2794. (defvar org-complex-heading-regexp nil
  2795. "Matches a headline and puts everything into groups:
  2796. group 1: the stars
  2797. group 2: The todo keyword, maybe
  2798. group 3: Priority cookie
  2799. group 4: True headline
  2800. group 5: Tags")
  2801. (make-variable-buffer-local 'org-complex-heading-regexp)
  2802. (defvar org-todo-line-tags-regexp nil
  2803. "Matches a headline and puts TODO state into group 2 if present.
  2804. Also put tags into group 4 if tags are present.")
  2805. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2806. (defvar org-nl-done-regexp nil
  2807. "Matches newline followed by a headline with the DONE keyword.")
  2808. (make-variable-buffer-local 'org-nl-done-regexp)
  2809. (defvar org-looking-at-done-regexp nil
  2810. "Matches the DONE keyword a point.")
  2811. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2812. (defvar org-ds-keyword-length 12
  2813. "Maximum length of the Deadline and SCHEDULED keywords.")
  2814. (make-variable-buffer-local 'org-ds-keyword-length)
  2815. (defvar org-deadline-regexp nil
  2816. "Matches the DEADLINE keyword.")
  2817. (make-variable-buffer-local 'org-deadline-regexp)
  2818. (defvar org-deadline-time-regexp nil
  2819. "Matches the DEADLINE keyword together with a time stamp.")
  2820. (make-variable-buffer-local 'org-deadline-time-regexp)
  2821. (defvar org-deadline-line-regexp nil
  2822. "Matches the DEADLINE keyword and the rest of the line.")
  2823. (make-variable-buffer-local 'org-deadline-line-regexp)
  2824. (defvar org-scheduled-regexp nil
  2825. "Matches the SCHEDULED keyword.")
  2826. (make-variable-buffer-local 'org-scheduled-regexp)
  2827. (defvar org-scheduled-time-regexp nil
  2828. "Matches the SCHEDULED keyword together with a time stamp.")
  2829. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2830. (defvar org-closed-time-regexp nil
  2831. "Matches the CLOSED keyword together with a time stamp.")
  2832. (make-variable-buffer-local 'org-closed-time-regexp)
  2833. (defvar org-keyword-time-regexp nil
  2834. "Matches any of the 4 keywords, together with the time stamp.")
  2835. (make-variable-buffer-local 'org-keyword-time-regexp)
  2836. (defvar org-keyword-time-not-clock-regexp nil
  2837. "Matches any of the 3 keywords, together with the time stamp.")
  2838. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2839. (defvar org-maybe-keyword-time-regexp nil
  2840. "Matches a timestamp, possibly preceeded by a keyword.")
  2841. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2842. (defvar org-planning-or-clock-line-re nil
  2843. "Matches a line with planning or clock info.")
  2844. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2845. (defconst org-plain-time-of-day-regexp
  2846. (concat
  2847. "\\(\\<[012]?[0-9]"
  2848. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2849. "\\(--?"
  2850. "\\(\\<[012]?[0-9]"
  2851. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2852. "\\)?")
  2853. "Regular expression to match a plain time or time range.
  2854. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2855. groups carry important information:
  2856. 0 the full match
  2857. 1 the first time, range or not
  2858. 8 the second time, if it is a range.")
  2859. (defconst org-plain-time-extension-regexp
  2860. (concat
  2861. "\\(\\<[012]?[0-9]"
  2862. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2863. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2864. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2865. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2866. groups carry important information:
  2867. 0 the full match
  2868. 7 hours of duration
  2869. 9 minutes of duration")
  2870. (defconst org-stamp-time-of-day-regexp
  2871. (concat
  2872. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2873. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2874. "\\(--?"
  2875. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2876. "Regular expression to match a timestamp time or time range.
  2877. After a match, the following groups carry important information:
  2878. 0 the full match
  2879. 1 date plus weekday, for backreferencing to make sure both times on same day
  2880. 2 the first time, range or not
  2881. 4 the second time, if it is a range.")
  2882. (defconst org-startup-options
  2883. '(("fold" org-startup-folded t)
  2884. ("overview" org-startup-folded t)
  2885. ("nofold" org-startup-folded nil)
  2886. ("showall" org-startup-folded nil)
  2887. ("content" org-startup-folded content)
  2888. ("hidestars" org-hide-leading-stars t)
  2889. ("showstars" org-hide-leading-stars nil)
  2890. ("odd" org-odd-levels-only t)
  2891. ("oddeven" org-odd-levels-only nil)
  2892. ("align" org-startup-align-all-tables t)
  2893. ("noalign" org-startup-align-all-tables nil)
  2894. ("customtime" org-display-custom-times t)
  2895. ("logdone" org-log-done time)
  2896. ("lognotedone" org-log-done note)
  2897. ("nologdone" org-log-done nil)
  2898. ("lognoteclock-out" org-log-note-clock-out t)
  2899. ("nolognoteclock-out" org-log-note-clock-out nil)
  2900. ("logrepeat" org-log-repeat state)
  2901. ("lognoterepeat" org-log-repeat note)
  2902. ("nologrepeat" org-log-repeat nil)
  2903. ("fninline" org-footnote-define-inline t)
  2904. ("nofninline" org-footnote-define-inline nil)
  2905. ("fnlocal" org-footnote-section nil)
  2906. ("fnauto" org-footnote-auto-label t)
  2907. ("fnprompt" org-footnote-auto-label nil)
  2908. ("fnconfirm" org-footnote-auto-label confirm)
  2909. ("fnplain" org-footnote-auto-label plain)
  2910. ("constcgs" constants-unit-system cgs)
  2911. ("constSI" constants-unit-system SI)
  2912. ("noptag" org-tag-persistent-alist nil))
  2913. "Variable associated with STARTUP options for org-mode.
  2914. Each element is a list of three items: The startup options as written
  2915. in the #+STARTUP line, the corresponding variable, and the value to
  2916. set this variable to if the option is found. An optional forth element PUSH
  2917. means to push this value onto the list in the variable.")
  2918. (defun org-set-regexps-and-options ()
  2919. "Precompute regular expressions for current buffer."
  2920. (when (org-mode-p)
  2921. (org-set-local 'org-todo-kwd-alist nil)
  2922. (org-set-local 'org-todo-key-alist nil)
  2923. (org-set-local 'org-todo-key-trigger nil)
  2924. (org-set-local 'org-todo-keywords-1 nil)
  2925. (org-set-local 'org-done-keywords nil)
  2926. (org-set-local 'org-todo-heads nil)
  2927. (org-set-local 'org-todo-sets nil)
  2928. (org-set-local 'org-todo-log-states nil)
  2929. (org-set-local 'org-file-properties nil)
  2930. (org-set-local 'org-file-tags nil)
  2931. (let ((re (org-make-options-regexp
  2932. '("CATEGORY" "TODO" "COLUMNS"
  2933. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2934. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2935. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2936. (splitre "[ \t]+")
  2937. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2938. tail sep kws1 prio props ftags drawers
  2939. ext-setup-or-nil setup-contents (start 0))
  2940. (save-excursion
  2941. (save-restriction
  2942. (widen)
  2943. (goto-char (point-min))
  2944. (while (or (and ext-setup-or-nil
  2945. (string-match re ext-setup-or-nil start)
  2946. (setq start (match-end 0)))
  2947. (and (setq ext-setup-or-nil nil start 0)
  2948. (re-search-forward re nil t)))
  2949. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2950. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2951. (cond
  2952. ((equal key "CATEGORY")
  2953. (if (string-match "[ \t]+$" value)
  2954. (setq value (replace-match "" t t value)))
  2955. (setq cat value))
  2956. ((member key '("SEQ_TODO" "TODO"))
  2957. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2958. ((equal key "TYP_TODO")
  2959. (push (cons 'type (org-split-string value splitre)) kwds))
  2960. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2961. ;; general TODO-like setup
  2962. (push (cons (intern (downcase (match-string 1 key)))
  2963. (org-split-string value splitre)) kwds))
  2964. ((equal key "TAGS")
  2965. (setq tags (append tags (if tags '("\\n") nil)
  2966. (org-split-string value splitre))))
  2967. ((equal key "COLUMNS")
  2968. (org-set-local 'org-columns-default-format value))
  2969. ((equal key "LINK")
  2970. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2971. (push (cons (match-string 1 value)
  2972. (org-trim (match-string 2 value)))
  2973. links)))
  2974. ((equal key "PRIORITIES")
  2975. (setq prio (org-split-string value " +")))
  2976. ((equal key "PROPERTY")
  2977. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2978. (push (cons (match-string 1 value) (match-string 2 value))
  2979. props)))
  2980. ((equal key "FILETAGS")
  2981. (when (string-match "\\S-" value)
  2982. (setq ftags
  2983. (append
  2984. ftags
  2985. (apply 'append
  2986. (mapcar (lambda (x) (org-split-string x ":"))
  2987. (org-split-string value)))))))
  2988. ((equal key "DRAWERS")
  2989. (setq drawers (org-split-string value splitre)))
  2990. ((equal key "CONSTANTS")
  2991. (setq const (append const (org-split-string value splitre))))
  2992. ((equal key "STARTUP")
  2993. (let ((opts (org-split-string value splitre))
  2994. l var val)
  2995. (while (setq l (pop opts))
  2996. (when (setq l (assoc l org-startup-options))
  2997. (setq var (nth 1 l) val (nth 2 l))
  2998. (if (not (nth 3 l))
  2999. (set (make-local-variable var) val)
  3000. (if (not (listp (symbol-value var)))
  3001. (set (make-local-variable var) nil))
  3002. (set (make-local-variable var) (symbol-value var))
  3003. (add-to-list var val))))))
  3004. ((equal key "ARCHIVE")
  3005. (string-match " *$" value)
  3006. (setq arch (replace-match "" t t value))
  3007. (remove-text-properties 0 (length arch)
  3008. '(face t fontified t) arch))
  3009. ((equal key "SETUPFILE")
  3010. (setq setup-contents (org-file-contents
  3011. (expand-file-name
  3012. (org-remove-double-quotes value))
  3013. 'noerror))
  3014. (if (not ext-setup-or-nil)
  3015. (setq ext-setup-or-nil setup-contents start 0)
  3016. (setq ext-setup-or-nil
  3017. (concat (substring ext-setup-or-nil 0 start)
  3018. "\n" setup-contents "\n"
  3019. (substring ext-setup-or-nil start)))))
  3020. ))))
  3021. (when cat
  3022. (org-set-local 'org-category (intern cat))
  3023. (push (cons "CATEGORY" cat) props))
  3024. (when prio
  3025. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3026. (setq prio (mapcar 'string-to-char prio))
  3027. (org-set-local 'org-highest-priority (nth 0 prio))
  3028. (org-set-local 'org-lowest-priority (nth 1 prio))
  3029. (org-set-local 'org-default-priority (nth 2 prio)))
  3030. (and props (org-set-local 'org-file-properties (nreverse props)))
  3031. (and ftags (org-set-local 'org-file-tags
  3032. (mapcar 'org-add-prop-inherited ftags)))
  3033. (and drawers (org-set-local 'org-drawers drawers))
  3034. (and arch (org-set-local 'org-archive-location arch))
  3035. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3036. ;; Process the TODO keywords
  3037. (unless kwds
  3038. ;; Use the global values as if they had been given locally.
  3039. (setq kwds (default-value 'org-todo-keywords))
  3040. (if (stringp (car kwds))
  3041. (setq kwds (list (cons org-todo-interpretation
  3042. (default-value 'org-todo-keywords)))))
  3043. (setq kwds (reverse kwds)))
  3044. (setq kwds (nreverse kwds))
  3045. (let (inter kws kw)
  3046. (while (setq kws (pop kwds))
  3047. (let ((kws (or
  3048. (run-hook-with-args-until-success
  3049. 'org-todo-setup-filter-hook kws)
  3050. kws)))
  3051. (setq inter (pop kws) sep (member "|" kws)
  3052. kws0 (delete "|" (copy-sequence kws))
  3053. kwsa nil
  3054. kws1 (mapcar
  3055. (lambda (x)
  3056. ;; 1 2
  3057. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3058. (progn
  3059. (setq kw (match-string 1 x)
  3060. key (and (match-end 2) (match-string 2 x))
  3061. log (org-extract-log-state-settings x))
  3062. (push (cons kw (and key (string-to-char key))) kwsa)
  3063. (and log (push log org-todo-log-states))
  3064. kw)
  3065. (error "Invalid TODO keyword %s" x)))
  3066. kws0)
  3067. kwsa (if kwsa (append '((:startgroup))
  3068. (nreverse kwsa)
  3069. '((:endgroup))))
  3070. hw (car kws1)
  3071. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3072. tail (list inter hw (car dws) (org-last dws))))
  3073. (add-to-list 'org-todo-heads hw 'append)
  3074. (push kws1 org-todo-sets)
  3075. (setq org-done-keywords (append org-done-keywords dws nil))
  3076. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3077. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3078. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3079. (setq org-todo-sets (nreverse org-todo-sets)
  3080. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3081. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3082. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3083. ;; Process the constants
  3084. (when const
  3085. (let (e cst)
  3086. (while (setq e (pop const))
  3087. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3088. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3089. (setq org-table-formula-constants-local cst)))
  3090. ;; Process the tags.
  3091. (when tags
  3092. (let (e tgs)
  3093. (while (setq e (pop tags))
  3094. (cond
  3095. ((equal e "{") (push '(:startgroup) tgs))
  3096. ((equal e "}") (push '(:endgroup) tgs))
  3097. ((equal e "\\n") (push '(:newline) tgs))
  3098. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3099. (push (cons (match-string 1 e)
  3100. (string-to-char (match-string 2 e)))
  3101. tgs))
  3102. (t (push (list e) tgs))))
  3103. (org-set-local 'org-tag-alist nil)
  3104. (while (setq e (pop tgs))
  3105. (or (and (stringp (car e))
  3106. (assoc (car e) org-tag-alist))
  3107. (push e org-tag-alist)))))
  3108. ;; Compute the regular expressions and other local variables
  3109. (if (not org-done-keywords)
  3110. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3111. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3112. (length org-scheduled-string)
  3113. (length org-clock-string)
  3114. (length org-closed-string)))
  3115. org-drawer-regexp
  3116. (concat "^[ \t]*:\\("
  3117. (mapconcat 'regexp-quote org-drawers "\\|")
  3118. "\\):[ \t]*$")
  3119. org-not-done-keywords
  3120. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3121. org-todo-regexp
  3122. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3123. "\\|") "\\)\\>")
  3124. org-not-done-regexp
  3125. (concat "\\<\\("
  3126. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3127. "\\)\\>")
  3128. org-not-done-heading-regexp
  3129. (concat "^\\(\\*+\\)[ \t]+\\("
  3130. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3131. "\\)\\>")
  3132. org-todo-line-regexp
  3133. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3134. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3135. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3136. org-complex-heading-regexp
  3137. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3138. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3139. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3140. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3141. org-nl-done-regexp
  3142. (concat "\n\\*+[ \t]+"
  3143. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3144. "\\)" "\\>")
  3145. org-todo-line-tags-regexp
  3146. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3147. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3148. (org-re
  3149. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3150. org-looking-at-done-regexp
  3151. (concat "^" "\\(?:"
  3152. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3153. "\\>")
  3154. org-deadline-regexp (concat "\\<" org-deadline-string)
  3155. org-deadline-time-regexp
  3156. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3157. org-deadline-line-regexp
  3158. (concat "\\<\\(" org-deadline-string "\\).*")
  3159. org-scheduled-regexp
  3160. (concat "\\<" org-scheduled-string)
  3161. org-scheduled-time-regexp
  3162. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3163. org-closed-time-regexp
  3164. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3165. org-keyword-time-regexp
  3166. (concat "\\<\\(" org-scheduled-string
  3167. "\\|" org-deadline-string
  3168. "\\|" org-closed-string
  3169. "\\|" org-clock-string "\\)"
  3170. " *[[<]\\([^]>]+\\)[]>]")
  3171. org-keyword-time-not-clock-regexp
  3172. (concat "\\<\\(" org-scheduled-string
  3173. "\\|" org-deadline-string
  3174. "\\|" org-closed-string
  3175. "\\)"
  3176. " *[[<]\\([^]>]+\\)[]>]")
  3177. org-maybe-keyword-time-regexp
  3178. (concat "\\(\\<\\(" org-scheduled-string
  3179. "\\|" org-deadline-string
  3180. "\\|" org-closed-string
  3181. "\\|" org-clock-string "\\)\\)?"
  3182. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3183. org-planning-or-clock-line-re
  3184. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3185. "\\|" org-deadline-string
  3186. "\\|" org-closed-string "\\|" org-clock-string
  3187. "\\)\\>\\)")
  3188. )
  3189. (org-compute-latex-and-specials-regexp)
  3190. (org-set-font-lock-defaults))))
  3191. (defun org-file-contents (file &optional noerror)
  3192. "Return the contents of FILE, as a string."
  3193. (if (or (not file)
  3194. (not (file-readable-p file)))
  3195. (if noerror
  3196. (progn
  3197. (message "Cannot read file %s" file)
  3198. (ding) (sit-for 2)
  3199. "")
  3200. (error "Cannot read file %s" file))
  3201. (with-temp-buffer
  3202. (insert-file-contents file)
  3203. (buffer-string))))
  3204. (defun org-extract-log-state-settings (x)
  3205. "Extract the log state setting from a TODO keyword string.
  3206. This will extract info from a string like \"WAIT(w@/!)\"."
  3207. (let (kw key log1 log2)
  3208. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3209. (setq kw (match-string 1 x)
  3210. key (and (match-end 2) (match-string 2 x))
  3211. log1 (and (match-end 3) (match-string 3 x))
  3212. log2 (and (match-end 4) (match-string 4 x)))
  3213. (and (or log1 log2)
  3214. (list kw
  3215. (and log1 (if (equal log1 "!") 'time 'note))
  3216. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3217. (defun org-remove-keyword-keys (list)
  3218. "Remove a pair of parenthesis at the end of each string in LIST."
  3219. (mapcar (lambda (x)
  3220. (if (string-match "(.*)$" x)
  3221. (substring x 0 (match-beginning 0))
  3222. x))
  3223. list))
  3224. ;; FIXME: this could be done much better, using second characters etc.
  3225. (defun org-assign-fast-keys (alist)
  3226. "Assign fast keys to a keyword-key alist.
  3227. Respect keys that are already there."
  3228. (let (new e k c c1 c2 (char ?a))
  3229. (while (setq e (pop alist))
  3230. (cond
  3231. ((equal e '(:startgroup)) (push e new))
  3232. ((equal e '(:endgroup)) (push e new))
  3233. ((equal e '(:newline)) (push e new))
  3234. (t
  3235. (setq k (car e) c2 nil)
  3236. (if (cdr e)
  3237. (setq c (cdr e))
  3238. ;; automatically assign a character.
  3239. (setq c1 (string-to-char
  3240. (downcase (substring
  3241. k (if (= (string-to-char k) ?@) 1 0)))))
  3242. (if (or (rassoc c1 new) (rassoc c1 alist))
  3243. (while (or (rassoc char new) (rassoc char alist))
  3244. (setq char (1+ char)))
  3245. (setq c2 c1))
  3246. (setq c (or c2 char)))
  3247. (push (cons k c) new))))
  3248. (nreverse new)))
  3249. ;;; Some variables used in various places
  3250. (defvar org-window-configuration nil
  3251. "Used in various places to store a window configuration.")
  3252. (defvar org-finish-function nil
  3253. "Function to be called when `C-c C-c' is used.
  3254. This is for getting out of special buffers like remember.")
  3255. ;; FIXME: Occasionally check by commenting these, to make sure
  3256. ;; no other functions uses these, forgetting to let-bind them.
  3257. (defvar entry)
  3258. (defvar state)
  3259. (defvar last-state)
  3260. (defvar date)
  3261. (defvar description)
  3262. ;; Defined somewhere in this file, but used before definition.
  3263. (defvar org-html-entities)
  3264. (defvar org-struct-menu)
  3265. (defvar org-org-menu)
  3266. (defvar org-tbl-menu)
  3267. (defvar org-agenda-keymap)
  3268. ;;;; Define the Org-mode
  3269. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3270. (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."))
  3271. ;; We use a before-change function to check if a table might need
  3272. ;; an update.
  3273. (defvar org-table-may-need-update t
  3274. "Indicates that a table might need an update.
  3275. This variable is set by `org-before-change-function'.
  3276. `org-table-align' sets it back to nil.")
  3277. (defun org-before-change-function (beg end)
  3278. "Every change indicates that a table might need an update."
  3279. (setq org-table-may-need-update t))
  3280. (defvar org-mode-map)
  3281. (defvar org-mode-hook nil
  3282. "Mode hook for Org-mode, run after the mode was turned on.")
  3283. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3284. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3285. (defvar org-table-buffer-is-an nil)
  3286. (defconst org-outline-regexp "\\*+ ")
  3287. ;;;###autoload
  3288. (define-derived-mode org-mode outline-mode "Org"
  3289. "Outline-based notes management and organizer, alias
  3290. \"Carsten's outline-mode for keeping track of everything.\"
  3291. Org-mode develops organizational tasks around a NOTES file which
  3292. contains information about projects as plain text. Org-mode is
  3293. implemented on top of outline-mode, which is ideal to keep the content
  3294. of large files well structured. It supports ToDo items, deadlines and
  3295. time stamps, which magically appear in the diary listing of the Emacs
  3296. calendar. Tables are easily created with a built-in table editor.
  3297. Plain text URL-like links connect to websites, emails (VM), Usenet
  3298. messages (Gnus), BBDB entries, and any files related to the project.
  3299. For printing and sharing of notes, an Org-mode file (or a part of it)
  3300. can be exported as a structured ASCII or HTML file.
  3301. The following commands are available:
  3302. \\{org-mode-map}"
  3303. ;; Get rid of Outline menus, they are not needed
  3304. ;; Need to do this here because define-derived-mode sets up
  3305. ;; the keymap so late. Still, it is a waste to call this each time
  3306. ;; we switch another buffer into org-mode.
  3307. (if (featurep 'xemacs)
  3308. (when (boundp 'outline-mode-menu-heading)
  3309. ;; Assume this is Greg's port, it used easymenu
  3310. (easy-menu-remove outline-mode-menu-heading)
  3311. (easy-menu-remove outline-mode-menu-show)
  3312. (easy-menu-remove outline-mode-menu-hide))
  3313. (define-key org-mode-map [menu-bar headings] 'undefined)
  3314. (define-key org-mode-map [menu-bar hide] 'undefined)
  3315. (define-key org-mode-map [menu-bar show] 'undefined))
  3316. (org-load-modules-maybe)
  3317. (easy-menu-add org-org-menu)
  3318. (easy-menu-add org-tbl-menu)
  3319. (org-install-agenda-files-menu)
  3320. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3321. (org-add-to-invisibility-spec '(org-cwidth))
  3322. (when (featurep 'xemacs)
  3323. (org-set-local 'line-move-ignore-invisible t))
  3324. (org-set-local 'outline-regexp org-outline-regexp)
  3325. (org-set-local 'outline-level 'org-outline-level)
  3326. (when (and org-ellipsis
  3327. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3328. (fboundp 'make-glyph-code))
  3329. (unless org-display-table
  3330. (setq org-display-table (make-display-table)))
  3331. (set-display-table-slot
  3332. org-display-table 4
  3333. (vconcat (mapcar
  3334. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3335. org-ellipsis)))
  3336. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3337. (setq buffer-display-table org-display-table))
  3338. (org-set-regexps-and-options)
  3339. (when (and org-tag-faces (not org-tags-special-faces-re))
  3340. ;; tag faces set outside customize.... force initialization.
  3341. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3342. ;; Calc embedded
  3343. (org-set-local 'calc-embedded-open-mode "# ")
  3344. (modify-syntax-entry ?# "<")
  3345. (modify-syntax-entry ?@ "w")
  3346. (if org-startup-truncated (setq truncate-lines t))
  3347. (org-set-local 'font-lock-unfontify-region-function
  3348. 'org-unfontify-region)
  3349. ;; Activate before-change-function
  3350. (org-set-local 'org-table-may-need-update t)
  3351. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3352. 'local)
  3353. ;; Check for running clock before killing a buffer
  3354. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3355. ;; Paragraphs and auto-filling
  3356. (org-set-autofill-regexps)
  3357. (setq indent-line-function 'org-indent-line-function)
  3358. (org-update-radio-target-regexp)
  3359. ;; Make sure dependence stuff works reliably, even for users who set it
  3360. ;; too late :-(
  3361. (if org-enforce-todo-dependencies
  3362. (add-hook 'org-blocker-hook
  3363. 'org-block-todo-from-children-or-siblings-or-parent)
  3364. (remove-hook 'org-blocker-hook
  3365. 'org-block-todo-from-children-or-siblings-or-parent))
  3366. (if org-enforce-todo-checkbox-dependencies
  3367. (add-hook 'org-blocker-hook
  3368. 'org-block-todo-from-checkboxes)
  3369. (remove-hook 'org-blocker-hook
  3370. 'org-block-todo-from-checkboxes))
  3371. ;; Comment characters
  3372. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3373. (org-set-local 'comment-padding " ")
  3374. ;; Align options lines
  3375. (org-set-local
  3376. 'align-mode-rules-list
  3377. '((org-in-buffer-settings
  3378. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3379. (modes . '(org-mode)))))
  3380. ;; Imenu
  3381. (org-set-local 'imenu-create-index-function
  3382. 'org-imenu-get-tree)
  3383. ;; Make isearch reveal context
  3384. (if (or (featurep 'xemacs)
  3385. (not (boundp 'outline-isearch-open-invisible-function)))
  3386. ;; Emacs 21 and XEmacs make use of the hook
  3387. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3388. ;; Emacs 22 deals with this through a special variable
  3389. (org-set-local 'outline-isearch-open-invisible-function
  3390. (lambda (&rest ignore) (org-show-context 'isearch))))
  3391. ;; If empty file that did not turn on org-mode automatically, make it to.
  3392. (if (and org-insert-mode-line-in-empty-file
  3393. (interactive-p)
  3394. (= (point-min) (point-max)))
  3395. (insert "# -*- mode: org -*-\n\n"))
  3396. (unless org-inhibit-startup
  3397. (when org-startup-align-all-tables
  3398. (let ((bmp (buffer-modified-p)))
  3399. (org-table-map-tables 'org-table-align)
  3400. (set-buffer-modified-p bmp)))
  3401. (org-set-startup-visibility)))
  3402. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3403. (defun org-current-time ()
  3404. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3405. (if (> (car org-time-stamp-rounding-minutes) 1)
  3406. (let ((r (car org-time-stamp-rounding-minutes))
  3407. (time (decode-time)))
  3408. (apply 'encode-time
  3409. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3410. (nthcdr 2 time))))
  3411. (current-time)))
  3412. ;;;; Font-Lock stuff, including the activators
  3413. (defvar org-mouse-map (make-sparse-keymap))
  3414. (org-defkey org-mouse-map
  3415. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3416. (org-defkey org-mouse-map
  3417. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3418. (when org-mouse-1-follows-link
  3419. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3420. (when org-tab-follows-link
  3421. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3422. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3423. (require 'font-lock)
  3424. (defconst org-non-link-chars "]\t\n\r<>")
  3425. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3426. "shell" "elisp"))
  3427. (defvar org-link-types-re nil
  3428. "Matches a link that has a url-like prefix like \"http:\"")
  3429. (defvar org-link-re-with-space nil
  3430. "Matches a link with spaces, optional angular brackets around it.")
  3431. (defvar org-link-re-with-space2 nil
  3432. "Matches a link with spaces, optional angular brackets around it.")
  3433. (defvar org-link-re-with-space3 nil
  3434. "Matches a link with spaces, only for internal part in bracket links.")
  3435. (defvar org-angle-link-re nil
  3436. "Matches link with angular brackets, spaces are allowed.")
  3437. (defvar org-plain-link-re nil
  3438. "Matches plain link, without spaces.")
  3439. (defvar org-bracket-link-regexp nil
  3440. "Matches a link in double brackets.")
  3441. (defvar org-bracket-link-analytic-regexp nil
  3442. "Regular expression used to analyze links.
  3443. Here is what the match groups contain after a match:
  3444. 1: http:
  3445. 2: http
  3446. 3: path
  3447. 4: [desc]
  3448. 5: desc")
  3449. (defvar org-bracket-link-analytic-regexp++ nil
  3450. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3451. (defvar org-any-link-re nil
  3452. "Regular expression matching any link.")
  3453. (defun org-make-link-regexps ()
  3454. "Update the link regular expressions.
  3455. This should be called after the variable `org-link-types' has changed."
  3456. (setq org-link-types-re
  3457. (concat
  3458. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3459. org-link-re-with-space
  3460. (concat
  3461. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3462. "\\([^" org-non-link-chars " ]"
  3463. "[^" org-non-link-chars "]*"
  3464. "[^" org-non-link-chars " ]\\)>?")
  3465. org-link-re-with-space2
  3466. (concat
  3467. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3468. "\\([^" org-non-link-chars " ]"
  3469. "[^\t\n\r]*"
  3470. "[^" org-non-link-chars " ]\\)>?")
  3471. org-link-re-with-space3
  3472. (concat
  3473. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3474. "\\([^" org-non-link-chars " ]"
  3475. "[^\t\n\r]*\\)")
  3476. org-angle-link-re
  3477. (concat
  3478. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3479. "\\([^" org-non-link-chars " ]"
  3480. "[^" org-non-link-chars "]*"
  3481. "\\)>")
  3482. org-plain-link-re
  3483. (concat
  3484. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3485. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3486. org-bracket-link-regexp
  3487. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3488. org-bracket-link-analytic-regexp
  3489. (concat
  3490. "\\[\\["
  3491. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3492. "\\([^]]+\\)"
  3493. "\\]"
  3494. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3495. "\\]")
  3496. org-bracket-link-analytic-regexp++
  3497. (concat
  3498. "\\[\\["
  3499. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3500. "\\([^]]+\\)"
  3501. "\\]"
  3502. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3503. "\\]")
  3504. org-any-link-re
  3505. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3506. org-angle-link-re "\\)\\|\\("
  3507. org-plain-link-re "\\)")))
  3508. (org-make-link-regexps)
  3509. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3510. "Regular expression for fast time stamp matching.")
  3511. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3512. "Regular expression for fast time stamp matching.")
  3513. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3514. "Regular expression matching time strings for analysis.
  3515. This one does not require the space after the date, so it can be used
  3516. on a string that terminates immediately after the date.")
  3517. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3518. "Regular expression matching time strings for analysis.")
  3519. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3520. "Regular expression matching time stamps, with groups.")
  3521. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3522. "Regular expression matching time stamps (also [..]), with groups.")
  3523. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3524. "Regular expression matching a time stamp range.")
  3525. (defconst org-tr-regexp-both
  3526. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3527. "Regular expression matching a time stamp range.")
  3528. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3529. org-ts-regexp "\\)?")
  3530. "Regular expression matching a time stamp or time stamp range.")
  3531. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3532. org-ts-regexp-both "\\)?")
  3533. "Regular expression matching a time stamp or time stamp range.
  3534. The time stamps may be either active or inactive.")
  3535. (defvar org-emph-face nil)
  3536. (defun org-do-emphasis-faces (limit)
  3537. "Run through the buffer and add overlays to links."
  3538. (let (rtn)
  3539. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3540. (if (not (= (char-after (match-beginning 3))
  3541. (char-after (match-beginning 4))))
  3542. (progn
  3543. (setq rtn t)
  3544. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3545. 'face
  3546. (nth 1 (assoc (match-string 3)
  3547. org-emphasis-alist)))
  3548. (add-text-properties (match-beginning 2) (match-end 2)
  3549. '(font-lock-multiline t))
  3550. (when org-hide-emphasis-markers
  3551. (add-text-properties (match-end 4) (match-beginning 5)
  3552. '(invisible org-link))
  3553. (add-text-properties (match-beginning 3) (match-end 3)
  3554. '(invisible org-link)))))
  3555. (backward-char 1))
  3556. rtn))
  3557. (defun org-emphasize (&optional char)
  3558. "Insert or change an emphasis, i.e. a font like bold or italic.
  3559. If there is an active region, change that region to a new emphasis.
  3560. If there is no region, just insert the marker characters and position
  3561. the cursor between them.
  3562. CHAR should be either the marker character, or the first character of the
  3563. HTML tag associated with that emphasis. If CHAR is a space, the means
  3564. to remove the emphasis of the selected region.
  3565. If char is not given (for example in an interactive call) it
  3566. will be prompted for."
  3567. (interactive)
  3568. (let ((eal org-emphasis-alist) e det
  3569. (erc org-emphasis-regexp-components)
  3570. (prompt "")
  3571. (string "") beg end move tag c s)
  3572. (if (org-region-active-p)
  3573. (setq beg (region-beginning) end (region-end)
  3574. string (buffer-substring beg end))
  3575. (setq move t))
  3576. (while (setq e (pop eal))
  3577. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3578. c (aref tag 0))
  3579. (push (cons c (string-to-char (car e))) det)
  3580. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3581. (substring tag 1)))))
  3582. (setq det (nreverse det))
  3583. (unless char
  3584. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3585. (setq char (read-char-exclusive)))
  3586. (setq char (or (cdr (assoc char det)) char))
  3587. (if (equal char ?\ )
  3588. (setq s "" move nil)
  3589. (unless (assoc (char-to-string char) org-emphasis-alist)
  3590. (error "No such emphasis marker: \"%c\"" char))
  3591. (setq s (char-to-string char)))
  3592. (while (and (> (length string) 1)
  3593. (equal (substring string 0 1) (substring string -1))
  3594. (assoc (substring string 0 1) org-emphasis-alist))
  3595. (setq string (substring string 1 -1)))
  3596. (setq string (concat s string s))
  3597. (if beg (delete-region beg end))
  3598. (unless (or (bolp)
  3599. (string-match (concat "[" (nth 0 erc) "\n]")
  3600. (char-to-string (char-before (point)))))
  3601. (insert " "))
  3602. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3603. (char-to-string (char-after (point))))
  3604. (insert " ") (backward-char 1))
  3605. (insert string)
  3606. (and move (backward-char 1))))
  3607. (defconst org-nonsticky-props
  3608. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3609. (defun org-activate-plain-links (limit)
  3610. "Run through the buffer and add overlays to links."
  3611. (catch 'exit
  3612. (let (f)
  3613. (while (re-search-forward org-plain-link-re limit t)
  3614. (setq f (get-text-property (match-beginning 0) 'face))
  3615. (if (or (eq f 'org-tag)
  3616. (and (listp f) (memq 'org-tag f)))
  3617. nil
  3618. (add-text-properties (match-beginning 0) (match-end 0)
  3619. (list 'mouse-face 'highlight
  3620. 'rear-nonsticky org-nonsticky-props
  3621. 'keymap org-mouse-map
  3622. ))
  3623. (throw 'exit t))))))
  3624. (defun org-activate-code (limit)
  3625. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3626. (progn
  3627. (remove-text-properties (match-beginning 0) (match-end 0)
  3628. '(display t invisible t intangible t))
  3629. t)))
  3630. (defun org-activate-angle-links (limit)
  3631. "Run through the buffer and add overlays to links."
  3632. (if (re-search-forward org-angle-link-re limit t)
  3633. (progn
  3634. (add-text-properties (match-beginning 0) (match-end 0)
  3635. (list 'mouse-face 'highlight
  3636. 'rear-nonsticky org-nonsticky-props
  3637. 'keymap org-mouse-map
  3638. ))
  3639. t)))
  3640. (defun org-activate-footnote-links (limit)
  3641. "Run through the buffer and add overlays to links."
  3642. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3643. limit t)
  3644. (progn
  3645. (add-text-properties (match-beginning 2) (match-end 2)
  3646. (list 'mouse-face 'highlight
  3647. 'rear-nonsticky org-nonsticky-props
  3648. 'keymap org-mouse-map
  3649. 'help-echo
  3650. (if (= (point-at-bol) (match-beginning 2))
  3651. "Footnote definition"
  3652. "Footnote reference")
  3653. ))
  3654. t)))
  3655. (defun org-activate-bracket-links (limit)
  3656. "Run through the buffer and add overlays to bracketed links."
  3657. (if (re-search-forward org-bracket-link-regexp limit t)
  3658. (let* ((help (concat "LINK: "
  3659. (org-match-string-no-properties 1)))
  3660. ;; FIXME: above we should remove the escapes.
  3661. ;; but that requires another match, protecting match data,
  3662. ;; a lot of overhead for font-lock.
  3663. (ip (org-maybe-intangible
  3664. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3665. 'keymap org-mouse-map 'mouse-face 'highlight
  3666. 'font-lock-multiline t 'help-echo help)))
  3667. (vp (list 'rear-nonsticky org-nonsticky-props
  3668. 'keymap org-mouse-map 'mouse-face 'highlight
  3669. 'font-lock-multiline t 'help-echo help)))
  3670. ;; We need to remove the invisible property here. Table narrowing
  3671. ;; may have made some of this invisible.
  3672. (remove-text-properties (match-beginning 0) (match-end 0)
  3673. '(invisible nil))
  3674. (if (match-end 3)
  3675. (progn
  3676. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3677. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3678. (add-text-properties (match-end 3) (match-end 0) ip))
  3679. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3680. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3681. (add-text-properties (match-end 1) (match-end 0) ip))
  3682. t)))
  3683. (defun org-activate-dates (limit)
  3684. "Run through the buffer and add overlays to dates."
  3685. (if (re-search-forward org-tsr-regexp-both limit t)
  3686. (progn
  3687. (add-text-properties (match-beginning 0) (match-end 0)
  3688. (list 'mouse-face 'highlight
  3689. 'rear-nonsticky org-nonsticky-props
  3690. 'keymap org-mouse-map))
  3691. (when org-display-custom-times
  3692. (if (match-end 3)
  3693. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3694. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3695. t)))
  3696. (defvar org-target-link-regexp nil
  3697. "Regular expression matching radio targets in plain text.")
  3698. (make-variable-buffer-local 'org-target-link-regexp)
  3699. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3700. "Regular expression matching a link target.")
  3701. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3702. "Regular expression matching a radio target.")
  3703. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3704. "Regular expression matching any target.")
  3705. (defun org-activate-target-links (limit)
  3706. "Run through the buffer and add overlays to target matches."
  3707. (when org-target-link-regexp
  3708. (let ((case-fold-search t))
  3709. (if (re-search-forward org-target-link-regexp limit t)
  3710. (progn
  3711. (add-text-properties (match-beginning 0) (match-end 0)
  3712. (list 'mouse-face 'highlight
  3713. 'rear-nonsticky org-nonsticky-props
  3714. 'keymap org-mouse-map
  3715. 'help-echo "Radio target link"
  3716. 'org-linked-text t))
  3717. t)))))
  3718. (defun org-update-radio-target-regexp ()
  3719. "Find all radio targets in this file and update the regular expression."
  3720. (interactive)
  3721. (when (memq 'radio org-activate-links)
  3722. (setq org-target-link-regexp
  3723. (org-make-target-link-regexp (org-all-targets 'radio)))
  3724. (org-restart-font-lock)))
  3725. (defun org-hide-wide-columns (limit)
  3726. (let (s e)
  3727. (setq s (text-property-any (point) (or limit (point-max))
  3728. 'org-cwidth t))
  3729. (when s
  3730. (setq e (next-single-property-change s 'org-cwidth))
  3731. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3732. (goto-char e)
  3733. t)))
  3734. (defvar org-latex-and-specials-regexp nil
  3735. "Regular expression for highlighting export special stuff.")
  3736. (defvar org-match-substring-regexp)
  3737. (defvar org-match-substring-with-braces-regexp)
  3738. (defvar org-export-html-special-string-regexps)
  3739. (defun org-compute-latex-and-specials-regexp ()
  3740. "Compute regular expression for stuff treated specially by exporters."
  3741. (if (not org-highlight-latex-fragments-and-specials)
  3742. (org-set-local 'org-latex-and-specials-regexp nil)
  3743. (require 'org-exp)
  3744. (let*
  3745. ((matchers (plist-get org-format-latex-options :matchers))
  3746. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3747. org-latex-regexps)))
  3748. (options (org-combine-plists (org-default-export-plist)
  3749. (org-infile-export-plist)))
  3750. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3751. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3752. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3753. (org-export-html-expand (plist-get options :expand-quoted-html))
  3754. (org-export-with-special-strings (plist-get options :special-strings))
  3755. (re-sub
  3756. (cond
  3757. ((equal org-export-with-sub-superscripts '{})
  3758. (list org-match-substring-with-braces-regexp))
  3759. (org-export-with-sub-superscripts
  3760. (list org-match-substring-regexp))
  3761. (t nil)))
  3762. (re-latex
  3763. (if org-export-with-LaTeX-fragments
  3764. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3765. (re-macros
  3766. (if org-export-with-TeX-macros
  3767. (list (concat "\\\\"
  3768. (regexp-opt
  3769. (append (mapcar 'car org-html-entities)
  3770. (if (boundp 'org-latex-entities)
  3771. org-latex-entities nil))
  3772. 'words))) ; FIXME
  3773. ))
  3774. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3775. (re-special (if org-export-with-special-strings
  3776. (mapcar (lambda (x) (car x))
  3777. org-export-html-special-string-regexps)))
  3778. (re-rest
  3779. (delq nil
  3780. (list
  3781. (if org-export-html-expand "@<[^>\n]+>")
  3782. ))))
  3783. (org-set-local
  3784. 'org-latex-and-specials-regexp
  3785. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3786. re-rest) "\\|")))))
  3787. (defun org-do-latex-and-special-faces (limit)
  3788. "Run through the buffer and add overlays to links."
  3789. (when org-latex-and-specials-regexp
  3790. (let (rtn d)
  3791. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3792. limit t))
  3793. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3794. 'face))
  3795. '(org-code org-verbatim underline)))
  3796. (progn
  3797. (setq rtn t
  3798. d (cond ((member (char-after (1+ (match-beginning 0)))
  3799. '(?_ ?^)) 1)
  3800. (t 0)))
  3801. (font-lock-prepend-text-property
  3802. (+ d (match-beginning 0)) (match-end 0)
  3803. 'face 'org-latex-and-export-specials)
  3804. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3805. '(font-lock-multiline t)))))
  3806. rtn)))
  3807. (defun org-restart-font-lock ()
  3808. "Restart font-lock-mode, to force refontification."
  3809. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3810. (font-lock-mode -1)
  3811. (font-lock-mode 1)))
  3812. (defun org-all-targets (&optional radio)
  3813. "Return a list of all targets in this file.
  3814. With optional argument RADIO, only find radio targets."
  3815. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3816. rtn)
  3817. (save-excursion
  3818. (goto-char (point-min))
  3819. (while (re-search-forward re nil t)
  3820. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3821. rtn)))
  3822. (defun org-make-target-link-regexp (targets)
  3823. "Make regular expression matching all strings in TARGETS.
  3824. The regular expression finds the targets also if there is a line break
  3825. between words."
  3826. (and targets
  3827. (concat
  3828. "\\<\\("
  3829. (mapconcat
  3830. (lambda (x)
  3831. (while (string-match " +" x)
  3832. (setq x (replace-match "\\s-+" t t x)))
  3833. x)
  3834. targets
  3835. "\\|")
  3836. "\\)\\>")))
  3837. (defun org-activate-tags (limit)
  3838. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3839. (progn
  3840. (add-text-properties (match-beginning 1) (match-end 1)
  3841. (list 'mouse-face 'highlight
  3842. 'rear-nonsticky org-nonsticky-props
  3843. 'keymap org-mouse-map))
  3844. t)))
  3845. (defun org-outline-level ()
  3846. (save-excursion
  3847. (looking-at outline-regexp)
  3848. (if (match-beginning 1)
  3849. (+ (org-get-string-indentation (match-string 1)) 1000)
  3850. (1- (- (match-end 0) (match-beginning 0))))))
  3851. (defvar org-font-lock-keywords nil)
  3852. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3853. "Regular expression matching a property line.")
  3854. (defvar org-font-lock-hook nil
  3855. "Functions to be called for special font lock stuff.")
  3856. (defun org-font-lock-hook (limit)
  3857. (run-hook-with-args 'org-font-lock-hook limit))
  3858. (defun org-set-font-lock-defaults ()
  3859. (let* ((em org-fontify-emphasized-text)
  3860. (lk org-activate-links)
  3861. (org-font-lock-extra-keywords
  3862. (list
  3863. ;; Call the hook
  3864. '(org-font-lock-hook)
  3865. ;; Headlines
  3866. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3867. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3868. ;; Table lines
  3869. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3870. (1 'org-table t))
  3871. ;; Table internals
  3872. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3873. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3874. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3875. ;; Drawers
  3876. (list org-drawer-regexp '(0 'org-special-keyword t))
  3877. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3878. ;; Properties
  3879. (list org-property-re
  3880. '(1 'org-special-keyword t)
  3881. '(3 'org-property-value t))
  3882. (if org-format-transports-properties-p
  3883. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3884. ;; Links
  3885. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3886. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3887. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3888. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3889. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3890. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3891. (if (memq 'footnote lk) '(org-activate-footnote-links
  3892. (2 'org-footnote t)))
  3893. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3894. '(org-hide-wide-columns (0 nil append))
  3895. ;; TODO lines
  3896. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  3897. '(1 (org-get-todo-face 1) t))
  3898. ;; DONE
  3899. (if org-fontify-done-headline
  3900. (list (concat "^[*]+ +\\<\\("
  3901. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3902. "\\)\\(.*\\)")
  3903. '(2 'org-headline-done t))
  3904. nil)
  3905. ;; Priorities
  3906. '(org-font-lock-add-priority-faces)
  3907. ;; Tags
  3908. '(org-font-lock-add-tag-faces)
  3909. ;; Special keywords
  3910. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3911. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3912. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3913. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3914. ;; Emphasis
  3915. (if em
  3916. (if (featurep 'xemacs)
  3917. '(org-do-emphasis-faces (0 nil append))
  3918. '(org-do-emphasis-faces)))
  3919. ;; Checkboxes
  3920. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3921. 2 'org-checkbox prepend)
  3922. (if org-provide-checkbox-statistics
  3923. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3924. (0 (org-get-checkbox-statistics-face) t)))
  3925. ;; Description list items
  3926. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3927. 2 'bold prepend)
  3928. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3929. '(1 'org-archived prepend))
  3930. ;; Specials
  3931. '(org-do-latex-and-special-faces)
  3932. ;; Code
  3933. '(org-activate-code (1 'org-code t))
  3934. ;; COMMENT
  3935. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3936. "\\|" org-quote-string "\\)\\>")
  3937. '(1 'org-special-keyword t))
  3938. '("^#.*" (0 'font-lock-comment-face t))
  3939. )))
  3940. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3941. ;; Now set the full font-lock-keywords
  3942. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3943. (org-set-local 'font-lock-defaults
  3944. '(org-font-lock-keywords t nil nil backward-paragraph))
  3945. (kill-local-variable 'font-lock-keywords) nil))
  3946. (defvar org-m nil)
  3947. (defvar org-l nil)
  3948. (defvar org-f nil)
  3949. (defun org-get-level-face (n)
  3950. "Get the right face for match N in font-lock matching of headlines."
  3951. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3952. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3953. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3954. (cond
  3955. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3956. ((eq n 2) org-f)
  3957. (t (if org-level-color-stars-only nil org-f))))
  3958. (defun org-get-todo-face (kwd)
  3959. "Get the right face for a TODO keyword KWD.
  3960. If KWD is a number, get the corresponding match group."
  3961. (if (numberp kwd) (setq kwd (match-string kwd)))
  3962. (or (cdr (assoc kwd org-todo-keyword-faces))
  3963. (and (member kwd org-done-keywords) 'org-done)
  3964. 'org-todo))
  3965. (defun org-font-lock-add-tag-faces (limit)
  3966. "Add the special tag faces."
  3967. (when (and org-tag-faces org-tags-special-faces-re)
  3968. (while (re-search-forward org-tags-special-faces-re limit t)
  3969. (add-text-properties (match-beginning 1) (match-end 1)
  3970. (list 'face (org-get-tag-face 1)
  3971. 'font-lock-fontified t))
  3972. (backward-char 1))))
  3973. (defun org-font-lock-add-priority-faces (limit)
  3974. "Add the special priority faces."
  3975. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  3976. (add-text-properties
  3977. (match-beginning 0) (match-end 0)
  3978. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  3979. org-priority-faces))
  3980. 'org-special-keyword)
  3981. 'font-lock-fontified t))))
  3982. (defun org-get-tag-face (kwd)
  3983. "Get the right face for a TODO keyword KWD.
  3984. If KWD is a number, get the corresponding match group."
  3985. (if (numberp kwd) (setq kwd (match-string kwd)))
  3986. (or (cdr (assoc kwd org-tag-faces))
  3987. 'org-tag))
  3988. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3989. "Remove fontification and activation overlays from links."
  3990. (font-lock-default-unfontify-region beg end)
  3991. (let* ((buffer-undo-list t)
  3992. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3993. (inhibit-modification-hooks t)
  3994. deactivate-mark buffer-file-name buffer-file-truename)
  3995. (remove-text-properties beg end
  3996. '(mouse-face t keymap t org-linked-text t
  3997. invisible t intangible t))))
  3998. ;;;; Visibility cycling, including org-goto and indirect buffer
  3999. ;;; Cycling
  4000. (defvar org-cycle-global-status nil)
  4001. (make-variable-buffer-local 'org-cycle-global-status)
  4002. (defvar org-cycle-subtree-status nil)
  4003. (make-variable-buffer-local 'org-cycle-subtree-status)
  4004. ;;;###autoload
  4005. (defvar org-inlinetask-min-level)
  4006. (defun org-cycle (&optional arg)
  4007. "Visibility cycling for Org-mode.
  4008. - When this function is called with a prefix argument, rotate the entire
  4009. buffer through 3 states (global cycling)
  4010. 1. OVERVIEW: Show only top-level headlines.
  4011. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4012. 3. SHOW ALL: Show everything.
  4013. When called with two C-u C-u prefixes, switch to the startup visibility,
  4014. determined by the variable `org-startup-folded', and by any VISIBILITY
  4015. properties in the buffer.
  4016. When called with three C-u C-u C-u prefixed, show the entire buffer,
  4017. including drawers.
  4018. - When point is at the beginning of a headline, rotate the subtree started
  4019. by this line through 3 different states (local cycling)
  4020. 1. FOLDED: Only the main headline is shown.
  4021. 2. CHILDREN: The main headline and the direct children are shown.
  4022. From this state, you can move to one of the children
  4023. and zoom in further.
  4024. 3. SUBTREE: Show the entire subtree, including body text.
  4025. - When there is a numeric prefix, go up to a heading with level ARG, do
  4026. a `show-subtree' and return to the previous cursor position. If ARG
  4027. is negative, go up that many levels.
  4028. - When point is not at the beginning of a headline, execute the global
  4029. binding for TAB, which is re-indenting the line. See the option
  4030. `org-cycle-emulate-tab' for details.
  4031. - Special case: if point is at the beginning of the buffer and there is
  4032. no headline in line 1, this function will act as if called with prefix arg.
  4033. But only if also the variable `org-cycle-global-at-bob' is t."
  4034. (interactive "P")
  4035. (org-load-modules-maybe)
  4036. (let* ((limit-level
  4037. (or org-cycle-max-level
  4038. (and (boundp 'org-inlinetask-min-level)
  4039. org-inlinetask-min-level
  4040. (1- org-inlinetask-min-level))))
  4041. (nstars (and limit-level
  4042. (if org-odd-levels-only
  4043. (and limit-level (1- (* limit-level 2)))
  4044. limit-level)))
  4045. (outline-regexp
  4046. (cond
  4047. ((not (org-mode-p)) outline-regexp)
  4048. (org-cycle-include-plain-lists
  4049. (concat "\\(?:\\*"
  4050. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4051. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4052. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4053. (bob-special (and org-cycle-global-at-bob (bobp)
  4054. (not (looking-at outline-regexp))))
  4055. (org-cycle-hook
  4056. (if bob-special
  4057. (delq 'org-optimize-window-after-visibility-change
  4058. (copy-sequence org-cycle-hook))
  4059. org-cycle-hook))
  4060. (pos (point)))
  4061. (if (or bob-special (equal arg '(4)))
  4062. ;; special case: use global cycling
  4063. (setq arg t))
  4064. (cond
  4065. ((equal arg '(16))
  4066. (org-set-startup-visibility)
  4067. (message "Startup visibility, plus VISIBILITY properties"))
  4068. ((equal arg '(64))
  4069. (show-all)
  4070. (message "Entire buffer visible, including drawers"))
  4071. ((org-at-table-p 'any)
  4072. ;; Enter the table or move to the next field in the table
  4073. (or (org-table-recognize-table.el)
  4074. (progn
  4075. (if arg (org-table-edit-field t)
  4076. (org-table-justify-field-maybe)
  4077. (call-interactively 'org-table-next-field)))))
  4078. ((eq arg t) ;; Global cycling
  4079. (cond
  4080. ((and (eq last-command this-command)
  4081. (eq org-cycle-global-status 'overview))
  4082. ;; We just created the overview - now do table of contents
  4083. ;; This can be slow in very large buffers, so indicate action
  4084. (message "CONTENTS...")
  4085. (org-content)
  4086. (message "CONTENTS...done")
  4087. (setq org-cycle-global-status 'contents)
  4088. (run-hook-with-args 'org-cycle-hook 'contents))
  4089. ((and (eq last-command this-command)
  4090. (eq org-cycle-global-status 'contents))
  4091. ;; We just showed the table of contents - now show everything
  4092. (show-all)
  4093. (message "SHOW ALL")
  4094. (setq org-cycle-global-status 'all)
  4095. (run-hook-with-args 'org-cycle-hook 'all))
  4096. (t
  4097. ;; Default action: go to overview
  4098. (org-overview)
  4099. (message "OVERVIEW")
  4100. (setq org-cycle-global-status 'overview)
  4101. (run-hook-with-args 'org-cycle-hook 'overview))))
  4102. ((and org-drawers org-drawer-regexp
  4103. (save-excursion
  4104. (beginning-of-line 1)
  4105. (looking-at org-drawer-regexp)))
  4106. ;; Toggle block visibility
  4107. (org-flag-drawer
  4108. (not (get-char-property (match-end 0) 'invisible))))
  4109. ((integerp arg)
  4110. ;; Show-subtree, ARG levels up from here.
  4111. (save-excursion
  4112. (org-back-to-heading)
  4113. (outline-up-heading (if (< arg 0) (- arg)
  4114. (- (funcall outline-level) arg)))
  4115. (org-show-subtree)))
  4116. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4117. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4118. ;; At a heading: rotate between three different views
  4119. (org-back-to-heading)
  4120. (let ((goal-column 0) eoh eol eos)
  4121. ;; First, some boundaries
  4122. (save-excursion
  4123. (org-back-to-heading)
  4124. (save-excursion
  4125. (beginning-of-line 2)
  4126. (while (and (not (eobp)) ;; this is like `next-line'
  4127. (get-char-property (1- (point)) 'invisible))
  4128. (beginning-of-line 2)) (setq eol (point)))
  4129. (outline-end-of-heading) (setq eoh (point))
  4130. (org-end-of-subtree t)
  4131. (unless (eobp)
  4132. (skip-chars-forward " \t\n")
  4133. (beginning-of-line 1) ; in case this is an item
  4134. )
  4135. (setq eos (1- (point))))
  4136. ;; Find out what to do next and set `this-command'
  4137. (cond
  4138. ((= eos eoh)
  4139. ;; Nothing is hidden behind this heading
  4140. (message "EMPTY ENTRY")
  4141. (setq org-cycle-subtree-status nil)
  4142. (save-excursion
  4143. (goto-char eos)
  4144. (outline-next-heading)
  4145. (if (org-invisible-p) (org-flag-heading nil))))
  4146. ((or (>= eol eos)
  4147. (not (string-match "\\S-" (buffer-substring eol eos))))
  4148. ;; Entire subtree is hidden in one line: open it
  4149. (org-show-entry)
  4150. (show-children)
  4151. (message "CHILDREN")
  4152. (save-excursion
  4153. (goto-char eos)
  4154. (outline-next-heading)
  4155. (if (org-invisible-p) (org-flag-heading nil)))
  4156. (setq org-cycle-subtree-status 'children)
  4157. (run-hook-with-args 'org-cycle-hook 'children))
  4158. ((and (eq last-command this-command)
  4159. (eq org-cycle-subtree-status 'children))
  4160. ;; We just showed the children, now show everything.
  4161. (org-show-subtree)
  4162. (message "SUBTREE")
  4163. (setq org-cycle-subtree-status 'subtree)
  4164. (run-hook-with-args 'org-cycle-hook 'subtree))
  4165. (t
  4166. ;; Default action: hide the subtree.
  4167. (hide-subtree)
  4168. (message "FOLDED")
  4169. (setq org-cycle-subtree-status 'folded)
  4170. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4171. ;; TAB emulation and template completion
  4172. (buffer-read-only (org-back-to-heading))
  4173. ((org-try-structure-completion))
  4174. ((org-try-cdlatex-tab))
  4175. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4176. (or (not (bolp))
  4177. (not (looking-at outline-regexp))))
  4178. (call-interactively (global-key-binding "\t")))
  4179. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4180. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4181. (or (and (eq org-cycle-emulate-tab 'white)
  4182. (= (match-end 0) (point-at-eol)))
  4183. (and (eq org-cycle-emulate-tab 'whitestart)
  4184. (>= (match-end 0) pos))))
  4185. t
  4186. (eq org-cycle-emulate-tab t))
  4187. (call-interactively (global-key-binding "\t")))
  4188. (t (save-excursion
  4189. (org-back-to-heading)
  4190. (org-cycle))))))
  4191. ;;;###autoload
  4192. (defun org-global-cycle (&optional arg)
  4193. "Cycle the global visibility. For details see `org-cycle'.
  4194. With C-u prefix arg, switch to startup visibility.
  4195. With a numeric prefix, show all headlines up to that level."
  4196. (interactive "P")
  4197. (let ((org-cycle-include-plain-lists
  4198. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4199. (cond
  4200. ((integerp arg)
  4201. (show-all)
  4202. (hide-sublevels arg)
  4203. (setq org-cycle-global-status 'contents))
  4204. ((equal arg '(4))
  4205. (org-set-startup-visibility)
  4206. (message "Startup visibility, plus VISIBILITY properties."))
  4207. (t
  4208. (org-cycle '(4))))))
  4209. (defun org-set-startup-visibility ()
  4210. "Set the visibility required by startup options and properties."
  4211. (cond
  4212. ((eq org-startup-folded t)
  4213. (org-cycle '(4)))
  4214. ((eq org-startup-folded 'content)
  4215. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4216. (org-cycle '(4)) (org-cycle '(4)))))
  4217. (org-set-visibility-according-to-property 'no-cleanup)
  4218. (org-cycle-hide-archived-subtrees 'all)
  4219. (org-cycle-hide-drawers 'all)
  4220. (org-cycle-show-empty-lines 'all))
  4221. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4222. "Switch subtree visibilities according to :VISIBILITY: property."
  4223. (interactive)
  4224. (let (org-show-entry-below state)
  4225. (save-excursion
  4226. (goto-char (point-min))
  4227. (while (re-search-forward
  4228. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4229. nil t)
  4230. (setq state (match-string 1))
  4231. (save-excursion
  4232. (org-back-to-heading t)
  4233. (hide-subtree)
  4234. (org-reveal)
  4235. (cond
  4236. ((equal state '("fold" "folded"))
  4237. (hide-subtree))
  4238. ((equal state "children")
  4239. (org-show-hidden-entry)
  4240. (show-children))
  4241. ((equal state "content")
  4242. (save-excursion
  4243. (save-restriction
  4244. (org-narrow-to-subtree)
  4245. (org-content))))
  4246. ((member state '("all" "showall"))
  4247. (show-subtree)))))
  4248. (unless no-cleanup
  4249. (org-cycle-hide-archived-subtrees 'all)
  4250. (org-cycle-hide-drawers 'all)
  4251. (org-cycle-show-empty-lines 'all)))))
  4252. (defun org-overview ()
  4253. "Switch to overview mode, showing only top-level headlines.
  4254. Really, this shows all headlines with level equal or greater than the level
  4255. of the first headline in the buffer. This is important, because if the
  4256. first headline is not level one, then (hide-sublevels 1) gives confusing
  4257. results."
  4258. (interactive)
  4259. (let ((level (save-excursion
  4260. (goto-char (point-min))
  4261. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4262. (progn
  4263. (goto-char (match-beginning 0))
  4264. (funcall outline-level))))))
  4265. (and level (hide-sublevels level))))
  4266. (defun org-content (&optional arg)
  4267. "Show all headlines in the buffer, like a table of contents.
  4268. With numerical argument N, show content up to level N."
  4269. (interactive "P")
  4270. (save-excursion
  4271. ;; Visit all headings and show their offspring
  4272. (and (integerp arg) (org-overview))
  4273. (goto-char (point-max))
  4274. (catch 'exit
  4275. (while (and (progn (condition-case nil
  4276. (outline-previous-visible-heading 1)
  4277. (error (goto-char (point-min))))
  4278. t)
  4279. (looking-at outline-regexp))
  4280. (if (integerp arg)
  4281. (show-children (1- arg))
  4282. (show-branches))
  4283. (if (bobp) (throw 'exit nil))))))
  4284. (defun org-optimize-window-after-visibility-change (state)
  4285. "Adjust the window after a change in outline visibility.
  4286. This function is the default value of the hook `org-cycle-hook'."
  4287. (when (get-buffer-window (current-buffer))
  4288. (cond
  4289. ((eq state 'content) nil)
  4290. ((eq state 'all) nil)
  4291. ((eq state 'folded) nil)
  4292. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4293. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4294. (defun org-compact-display-after-subtree-move ()
  4295. "Show a compacter version of the tree of the entry's parent."
  4296. (save-excursion
  4297. (if (org-up-heading-safe)
  4298. (progn
  4299. (hide-subtree)
  4300. (show-entry)
  4301. (show-children)
  4302. (org-cycle-show-empty-lines 'children)
  4303. (org-cycle-hide-drawers 'children))
  4304. (org-overview))))
  4305. (defun org-cycle-show-empty-lines (state)
  4306. "Show empty lines above all visible headlines.
  4307. The region to be covered depends on STATE when called through
  4308. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4309. entire buffer covered. Note that an empty line is only shown if there
  4310. are at least `org-cycle-separator-lines' empty lines before the headline."
  4311. (when (> org-cycle-separator-lines 0)
  4312. (save-excursion
  4313. (let* ((n org-cycle-separator-lines)
  4314. (re (cond
  4315. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4316. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4317. (t (let ((ns (number-to-string (- n 2))))
  4318. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4319. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4320. beg end)
  4321. (cond
  4322. ((memq state '(overview contents t))
  4323. (setq beg (point-min) end (point-max)))
  4324. ((memq state '(children folded))
  4325. (setq beg (point) end (progn (org-end-of-subtree t t)
  4326. (beginning-of-line 2)
  4327. (point)))))
  4328. (when beg
  4329. (goto-char beg)
  4330. (while (re-search-forward re end t)
  4331. (if (not (get-char-property (match-end 1) 'invisible))
  4332. (outline-flag-region
  4333. (match-beginning 1) (match-end 1) nil)))))))
  4334. ;; Never hide empty lines at the end of the file.
  4335. (save-excursion
  4336. (goto-char (point-max))
  4337. (outline-previous-heading)
  4338. (outline-end-of-heading)
  4339. (if (and (looking-at "[ \t\n]+")
  4340. (= (match-end 0) (point-max)))
  4341. (outline-flag-region (point) (match-end 0) nil))))
  4342. (defun org-show-empty-lines-in-parent ()
  4343. "Move to the parent and re-show empty lines before visible headlines."
  4344. (save-excursion
  4345. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4346. (org-cycle-show-empty-lines context))))
  4347. (defun org-cycle-hide-drawers (state)
  4348. "Re-hide all drawers after a visibility state change."
  4349. (when (and (org-mode-p)
  4350. (not (memq state '(overview folded))))
  4351. (save-excursion
  4352. (let* ((globalp (memq state '(contents all)))
  4353. (beg (if globalp (point-min) (point)))
  4354. (end (if globalp (point-max) (org-end-of-subtree t))))
  4355. (goto-char beg)
  4356. (while (re-search-forward org-drawer-regexp end t)
  4357. (org-flag-drawer t))))))
  4358. (defun org-flag-drawer (flag)
  4359. (save-excursion
  4360. (beginning-of-line 1)
  4361. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4362. (let ((b (match-end 0))
  4363. (outline-regexp org-outline-regexp))
  4364. (if (re-search-forward
  4365. "^[ \t]*:END:"
  4366. (save-excursion (outline-next-heading) (point)) t)
  4367. (outline-flag-region b (point-at-eol) flag)
  4368. (error ":END: line missing"))))))
  4369. (defun org-subtree-end-visible-p ()
  4370. "Is the end of the current subtree visible?"
  4371. (pos-visible-in-window-p
  4372. (save-excursion (org-end-of-subtree t) (point))))
  4373. (defun org-first-headline-recenter (&optional N)
  4374. "Move cursor to the first headline and recenter the headline.
  4375. Optional argument N means, put the headline into the Nth line of the window."
  4376. (goto-char (point-min))
  4377. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4378. (beginning-of-line)
  4379. (recenter (prefix-numeric-value N))))
  4380. ;;; Org-goto
  4381. (defvar org-goto-window-configuration nil)
  4382. (defvar org-goto-marker nil)
  4383. (defvar org-goto-map
  4384. (let ((map (make-sparse-keymap)))
  4385. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4386. (while (setq cmd (pop cmds))
  4387. (substitute-key-definition cmd cmd map global-map)))
  4388. (suppress-keymap map)
  4389. (org-defkey map "\C-m" 'org-goto-ret)
  4390. (org-defkey map [(return)] 'org-goto-ret)
  4391. (org-defkey map [(left)] 'org-goto-left)
  4392. (org-defkey map [(right)] 'org-goto-right)
  4393. (org-defkey map [(control ?g)] 'org-goto-quit)
  4394. (org-defkey map "\C-i" 'org-cycle)
  4395. (org-defkey map [(tab)] 'org-cycle)
  4396. (org-defkey map [(down)] 'outline-next-visible-heading)
  4397. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4398. (if org-goto-auto-isearch
  4399. (if (fboundp 'define-key-after)
  4400. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4401. nil)
  4402. (org-defkey map "q" 'org-goto-quit)
  4403. (org-defkey map "n" 'outline-next-visible-heading)
  4404. (org-defkey map "p" 'outline-previous-visible-heading)
  4405. (org-defkey map "f" 'outline-forward-same-level)
  4406. (org-defkey map "b" 'outline-backward-same-level)
  4407. (org-defkey map "u" 'outline-up-heading))
  4408. (org-defkey map "/" 'org-occur)
  4409. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4410. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4411. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4412. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4413. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4414. map))
  4415. (defconst org-goto-help
  4416. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4417. RET=jump to location [Q]uit and return to previous location
  4418. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4419. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4420. ;; FIXME: Docstring doe not mention both interfaces
  4421. (defun org-goto (&optional alternative-interface)
  4422. "Look up a different location in the current file, keeping current visibility.
  4423. When you want look-up or go to a different location in a document, the
  4424. fastest way is often to fold the entire buffer and then dive into the tree.
  4425. This method has the disadvantage, that the previous location will be folded,
  4426. which may not be what you want.
  4427. This command works around this by showing a copy of the current buffer
  4428. in an indirect buffer, in overview mode. You can dive into the tree in
  4429. that copy, use org-occur and incremental search to find a location.
  4430. When pressing RET or `Q', the command returns to the original buffer in
  4431. which the visibility is still unchanged. After RET is will also jump to
  4432. the location selected in the indirect buffer and expose the
  4433. the headline hierarchy above."
  4434. (interactive "P")
  4435. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4436. (org-refile-use-outline-path t)
  4437. (interface
  4438. (if (not alternative-interface)
  4439. org-goto-interface
  4440. (if (eq org-goto-interface 'outline)
  4441. 'outline-path-completion
  4442. 'outline)))
  4443. (org-goto-start-pos (point))
  4444. (selected-point
  4445. (if (eq interface 'outline)
  4446. (car (org-get-location (current-buffer) org-goto-help))
  4447. (nth 3 (org-refile-get-location "Goto: ")))))
  4448. (if selected-point
  4449. (progn
  4450. (org-mark-ring-push org-goto-start-pos)
  4451. (goto-char selected-point)
  4452. (if (or (org-invisible-p) (org-invisible-p2))
  4453. (org-show-context 'org-goto)))
  4454. (message "Quit"))))
  4455. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4456. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4457. (defvar org-goto-local-auto-isearch-map) ; defined below
  4458. (defun org-get-location (buf help)
  4459. "Let the user select a location in the Org-mode buffer BUF.
  4460. This function uses a recursive edit. It returns the selected position
  4461. or nil."
  4462. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4463. (isearch-hide-immediately nil)
  4464. (isearch-search-fun-function
  4465. (lambda () 'org-goto-local-search-headings))
  4466. (org-goto-selected-point org-goto-exit-command))
  4467. (save-excursion
  4468. (save-window-excursion
  4469. (delete-other-windows)
  4470. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4471. (switch-to-buffer
  4472. (condition-case nil
  4473. (make-indirect-buffer (current-buffer) "*org-goto*")
  4474. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4475. (with-output-to-temp-buffer "*Help*"
  4476. (princ help))
  4477. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4478. (setq buffer-read-only nil)
  4479. (let ((org-startup-truncated t)
  4480. (org-startup-folded nil)
  4481. (org-startup-align-all-tables nil))
  4482. (org-mode)
  4483. (org-overview))
  4484. (setq buffer-read-only t)
  4485. (if (and (boundp 'org-goto-start-pos)
  4486. (integer-or-marker-p org-goto-start-pos))
  4487. (let ((org-show-hierarchy-above t)
  4488. (org-show-siblings t)
  4489. (org-show-following-heading t))
  4490. (goto-char org-goto-start-pos)
  4491. (and (org-invisible-p) (org-show-context)))
  4492. (goto-char (point-min)))
  4493. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4494. (message "Select location and press RET")
  4495. (use-local-map org-goto-map)
  4496. (recursive-edit)
  4497. ))
  4498. (kill-buffer "*org-goto*")
  4499. (cons org-goto-selected-point org-goto-exit-command)))
  4500. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4501. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4502. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4503. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4504. (defun org-goto-local-search-headings (string bound noerror)
  4505. "Search and make sure that any matches are in headlines."
  4506. (catch 'return
  4507. (while (if isearch-forward
  4508. (search-forward string bound noerror)
  4509. (search-backward string bound noerror))
  4510. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4511. (and (member :headline context)
  4512. (not (member :tags context))))
  4513. (throw 'return (point))))))
  4514. (defun org-goto-local-auto-isearch ()
  4515. "Start isearch."
  4516. (interactive)
  4517. (goto-char (point-min))
  4518. (let ((keys (this-command-keys)))
  4519. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4520. (isearch-mode t)
  4521. (isearch-process-search-char (string-to-char keys)))))
  4522. (defun org-goto-ret (&optional arg)
  4523. "Finish `org-goto' by going to the new location."
  4524. (interactive "P")
  4525. (setq org-goto-selected-point (point)
  4526. org-goto-exit-command 'return)
  4527. (throw 'exit nil))
  4528. (defun org-goto-left ()
  4529. "Finish `org-goto' by going to the new location."
  4530. (interactive)
  4531. (if (org-on-heading-p)
  4532. (progn
  4533. (beginning-of-line 1)
  4534. (setq org-goto-selected-point (point)
  4535. org-goto-exit-command 'left)
  4536. (throw 'exit nil))
  4537. (error "Not on a heading")))
  4538. (defun org-goto-right ()
  4539. "Finish `org-goto' by going to the new location."
  4540. (interactive)
  4541. (if (org-on-heading-p)
  4542. (progn
  4543. (setq org-goto-selected-point (point)
  4544. org-goto-exit-command 'right)
  4545. (throw 'exit nil))
  4546. (error "Not on a heading")))
  4547. (defun org-goto-quit ()
  4548. "Finish `org-goto' without cursor motion."
  4549. (interactive)
  4550. (setq org-goto-selected-point nil)
  4551. (setq org-goto-exit-command 'quit)
  4552. (throw 'exit nil))
  4553. ;;; Indirect buffer display of subtrees
  4554. (defvar org-indirect-dedicated-frame nil
  4555. "This is the frame being used for indirect tree display.")
  4556. (defvar org-last-indirect-buffer nil)
  4557. (defun org-tree-to-indirect-buffer (&optional arg)
  4558. "Create indirect buffer and narrow it to current subtree.
  4559. With numerical prefix ARG, go up to this level and then take that tree.
  4560. If ARG is negative, go up that many levels.
  4561. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4562. indirect buffer previously made with this command, to avoid proliferation of
  4563. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4564. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4565. is kept so that you can work with several indirect buffers at the same time.
  4566. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4567. requests that a new frame be made for the new buffer, so that the dedicated
  4568. frame is not changed."
  4569. (interactive "P")
  4570. (let ((cbuf (current-buffer))
  4571. (cwin (selected-window))
  4572. (pos (point))
  4573. beg end level heading ibuf)
  4574. (save-excursion
  4575. (org-back-to-heading t)
  4576. (when (numberp arg)
  4577. (setq level (org-outline-level))
  4578. (if (< arg 0) (setq arg (+ level arg)))
  4579. (while (> (setq level (org-outline-level)) arg)
  4580. (outline-up-heading 1 t)))
  4581. (setq beg (point)
  4582. heading (org-get-heading))
  4583. (org-end-of-subtree t) (setq end (point)))
  4584. (if (and (buffer-live-p org-last-indirect-buffer)
  4585. (not (eq org-indirect-buffer-display 'new-frame))
  4586. (not arg))
  4587. (kill-buffer org-last-indirect-buffer))
  4588. (setq ibuf (org-get-indirect-buffer cbuf)
  4589. org-last-indirect-buffer ibuf)
  4590. (cond
  4591. ((or (eq org-indirect-buffer-display 'new-frame)
  4592. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4593. (select-frame (make-frame))
  4594. (delete-other-windows)
  4595. (switch-to-buffer ibuf)
  4596. (org-set-frame-title heading))
  4597. ((eq org-indirect-buffer-display 'dedicated-frame)
  4598. (raise-frame
  4599. (select-frame (or (and org-indirect-dedicated-frame
  4600. (frame-live-p org-indirect-dedicated-frame)
  4601. org-indirect-dedicated-frame)
  4602. (setq org-indirect-dedicated-frame (make-frame)))))
  4603. (delete-other-windows)
  4604. (switch-to-buffer ibuf)
  4605. (org-set-frame-title (concat "Indirect: " heading)))
  4606. ((eq org-indirect-buffer-display 'current-window)
  4607. (switch-to-buffer ibuf))
  4608. ((eq org-indirect-buffer-display 'other-window)
  4609. (pop-to-buffer ibuf))
  4610. (t (error "Invalid value.")))
  4611. (if (featurep 'xemacs)
  4612. (save-excursion (org-mode) (turn-on-font-lock)))
  4613. (narrow-to-region beg end)
  4614. (show-all)
  4615. (goto-char pos)
  4616. (and (window-live-p cwin) (select-window cwin))))
  4617. (defun org-get-indirect-buffer (&optional buffer)
  4618. (setq buffer (or buffer (current-buffer)))
  4619. (let ((n 1) (base (buffer-name buffer)) bname)
  4620. (while (buffer-live-p
  4621. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4622. (setq n (1+ n)))
  4623. (condition-case nil
  4624. (make-indirect-buffer buffer bname 'clone)
  4625. (error (make-indirect-buffer buffer bname)))))
  4626. (defun org-set-frame-title (title)
  4627. "Set the title of the current frame to the string TITLE."
  4628. ;; FIXME: how to name a single frame in XEmacs???
  4629. (unless (featurep 'xemacs)
  4630. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4631. ;;;; Structure editing
  4632. ;;; Inserting headlines
  4633. (defun org-previous-line-empty-p ()
  4634. (save-excursion
  4635. (and (not (bobp))
  4636. (or (beginning-of-line 0) t)
  4637. (save-match-data
  4638. (looking-at "[ \t]*$")))))
  4639. (defun org-insert-heading (&optional force-heading)
  4640. "Insert a new heading or item with same depth at point.
  4641. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4642. If point is at the beginning of a headline, insert a sibling before the
  4643. current headline. If point is not at the beginning, do not split the line,
  4644. but create the new headline after the current line."
  4645. (interactive "P")
  4646. (if (= (buffer-size) 0)
  4647. (insert "\n* ")
  4648. (when (or force-heading (not (org-insert-item)))
  4649. (let* ((empty-line-p nil)
  4650. (head (save-excursion
  4651. (condition-case nil
  4652. (progn
  4653. (org-back-to-heading)
  4654. (setq empty-line-p (org-previous-line-empty-p))
  4655. (match-string 0))
  4656. (error "*"))))
  4657. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4658. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4659. pos hide-previous previous-pos)
  4660. (cond
  4661. ((and (org-on-heading-p) (bolp)
  4662. (or (bobp)
  4663. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4664. ;; insert before the current line
  4665. (open-line (if blank 2 1)))
  4666. ((and (bolp)
  4667. (or (bobp)
  4668. (save-excursion
  4669. (backward-char 1) (not (org-invisible-p)))))
  4670. ;; insert right here
  4671. nil)
  4672. (t
  4673. ;; somewhere in the line
  4674. (save-excursion
  4675. (setq previous-pos (point-at-bol))
  4676. (end-of-line)
  4677. (setq hide-previous (org-invisible-p)))
  4678. (and org-insert-heading-respect-content (org-show-subtree))
  4679. (let ((split
  4680. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4681. (save-excursion
  4682. (let ((p (point)))
  4683. (goto-char (point-at-bol))
  4684. (and (looking-at org-complex-heading-regexp)
  4685. (> p (match-beginning 4)))))))
  4686. tags pos)
  4687. (cond
  4688. (org-insert-heading-respect-content
  4689. (org-end-of-subtree nil t)
  4690. (or (bolp) (newline))
  4691. (or (org-previous-line-empty-p)
  4692. (and blank (newline)))
  4693. (open-line 1))
  4694. ((org-on-heading-p)
  4695. (when hide-previous
  4696. (show-children)
  4697. (org-show-entry))
  4698. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4699. (setq tags (and (match-end 2) (match-string 2)))
  4700. (and (match-end 1)
  4701. (delete-region (match-beginning 1) (match-end 1)))
  4702. (setq pos (point-at-bol))
  4703. (or split (end-of-line 1))
  4704. (delete-horizontal-space)
  4705. (newline (if blank 2 1))
  4706. (when tags
  4707. (save-excursion
  4708. (goto-char pos)
  4709. (end-of-line 1)
  4710. (insert " " tags)
  4711. (org-set-tags nil 'align))))
  4712. (t
  4713. (or split (end-of-line 1))
  4714. (newline (if blank 2 1)))))))
  4715. (insert head) (just-one-space)
  4716. (setq pos (point))
  4717. (end-of-line 1)
  4718. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4719. (when (and org-insert-heading-respect-content hide-previous)
  4720. (save-excursion
  4721. (goto-char previous-pos)
  4722. (hide-subtree)))
  4723. (run-hooks 'org-insert-heading-hook)))))
  4724. (defun org-get-heading (&optional no-tags)
  4725. "Return the heading of the current entry, without the stars."
  4726. (save-excursion
  4727. (org-back-to-heading t)
  4728. (if (looking-at
  4729. (if no-tags
  4730. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4731. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4732. (match-string 1) "")))
  4733. (defun org-heading-components ()
  4734. "Return the components of the current heading.
  4735. This is a list with the following elements:
  4736. - the level as an integer
  4737. - the reduced level, different if `org-odd-levels-only' is set.
  4738. - the TODO keyword, or nil
  4739. - the priority character, like ?A, or nil if no priority is given
  4740. - the headline text itself, or the tags string if no headline text
  4741. - the tags string, or nil."
  4742. (save-excursion
  4743. (org-back-to-heading t)
  4744. (if (looking-at org-complex-heading-regexp)
  4745. (list (length (match-string 1))
  4746. (org-reduced-level (length (match-string 1)))
  4747. (org-match-string-no-properties 2)
  4748. (and (match-end 3) (aref (match-string 3) 2))
  4749. (org-match-string-no-properties 4)
  4750. (org-match-string-no-properties 5)))))
  4751. (defun org-insert-heading-after-current ()
  4752. "Insert a new heading with same level as current, after current subtree."
  4753. (interactive)
  4754. (org-back-to-heading)
  4755. (org-insert-heading)
  4756. (org-move-subtree-down)
  4757. (end-of-line 1))
  4758. (defun org-insert-heading-respect-content ()
  4759. (interactive)
  4760. (let ((org-insert-heading-respect-content t))
  4761. (org-insert-heading t)))
  4762. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4763. (interactive "P")
  4764. (let ((org-insert-heading-respect-content t))
  4765. (org-insert-todo-heading force-state t)))
  4766. (defun org-insert-todo-heading (arg &optional force-heading)
  4767. "Insert a new heading with the same level and TODO state as current heading.
  4768. If the heading has no TODO state, or if the state is DONE, use the first
  4769. state (TODO by default). Also with prefix arg, force first state."
  4770. (interactive "P")
  4771. (when (or force-heading (not (org-insert-item 'checkbox)))
  4772. (org-insert-heading force-heading)
  4773. (save-excursion
  4774. (org-back-to-heading)
  4775. (outline-previous-heading)
  4776. (looking-at org-todo-line-regexp))
  4777. (let*
  4778. ((new-mark-x
  4779. (if (or arg
  4780. (not (match-beginning 2))
  4781. (member (match-string 2) org-done-keywords))
  4782. (car org-todo-keywords-1)
  4783. (match-string 2)))
  4784. (new-mark
  4785. (or
  4786. (run-hook-with-args-until-success
  4787. 'org-todo-get-default-hook new-mark-x nil)
  4788. new-mark-x)))
  4789. (beginning-of-line 1)
  4790. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4791. (insert new-mark " ")))
  4792. (when org-provide-todo-statistics
  4793. (org-update-parent-todo-statistics))))
  4794. (defun org-insert-subheading (arg)
  4795. "Insert a new subheading and demote it.
  4796. Works for outline headings and for plain lists alike."
  4797. (interactive "P")
  4798. (org-insert-heading arg)
  4799. (cond
  4800. ((org-on-heading-p) (org-do-demote))
  4801. ((org-at-item-p) (org-indent-item 1))))
  4802. (defun org-insert-todo-subheading (arg)
  4803. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4804. Works for outline headings and for plain lists alike."
  4805. (interactive "P")
  4806. (org-insert-todo-heading arg)
  4807. (cond
  4808. ((org-on-heading-p) (org-do-demote))
  4809. ((org-at-item-p) (org-indent-item 1))))
  4810. ;;; Promotion and Demotion
  4811. (defun org-promote-subtree ()
  4812. "Promote the entire subtree.
  4813. See also `org-promote'."
  4814. (interactive)
  4815. (save-excursion
  4816. (org-map-tree 'org-promote))
  4817. (org-fix-position-after-promote))
  4818. (defun org-demote-subtree ()
  4819. "Demote the entire subtree. See `org-demote'.
  4820. See also `org-promote'."
  4821. (interactive)
  4822. (save-excursion
  4823. (org-map-tree 'org-demote))
  4824. (org-fix-position-after-promote))
  4825. (defun org-do-promote ()
  4826. "Promote the current heading higher up the tree.
  4827. If the region is active in `transient-mark-mode', promote all headings
  4828. in the region."
  4829. (interactive)
  4830. (save-excursion
  4831. (if (org-region-active-p)
  4832. (org-map-region 'org-promote (region-beginning) (region-end))
  4833. (org-promote)))
  4834. (org-fix-position-after-promote))
  4835. (defun org-do-demote ()
  4836. "Demote the current heading lower down the tree.
  4837. If the region is active in `transient-mark-mode', demote all headings
  4838. in the region."
  4839. (interactive)
  4840. (save-excursion
  4841. (if (org-region-active-p)
  4842. (org-map-region 'org-demote (region-beginning) (region-end))
  4843. (org-demote)))
  4844. (org-fix-position-after-promote))
  4845. (defun org-fix-position-after-promote ()
  4846. "Make sure that after pro/demotion cursor position is right."
  4847. (let ((pos (point)))
  4848. (when (save-excursion
  4849. (beginning-of-line 1)
  4850. (looking-at org-todo-line-regexp)
  4851. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4852. (cond ((eobp) (insert " "))
  4853. ((eolp) (insert " "))
  4854. ((equal (char-after) ?\ ) (forward-char 1))))))
  4855. (defun org-reduced-level (l)
  4856. "Compute the effective level of a heading.
  4857. This takes into account the setting of `org-odd-levels-only'."
  4858. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4859. (defun org-get-valid-level (level &optional change)
  4860. "Rectify a level change under the influence of `org-odd-levels-only'
  4861. LEVEL is a current level, CHANGE is by how much the level should be
  4862. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4863. even level numbers will become the next higher odd number."
  4864. (if org-odd-levels-only
  4865. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4866. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4867. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4868. (max 1 (+ level (or change 0)))))
  4869. (if (boundp 'define-obsolete-function-alias)
  4870. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4871. (define-obsolete-function-alias 'org-get-legal-level
  4872. 'org-get-valid-level)
  4873. (define-obsolete-function-alias 'org-get-legal-level
  4874. 'org-get-valid-level "23.1")))
  4875. (defun org-promote ()
  4876. "Promote the current heading higher up the tree.
  4877. If the region is active in `transient-mark-mode', promote all headings
  4878. in the region."
  4879. (org-back-to-heading t)
  4880. (let* ((level (save-match-data (funcall outline-level)))
  4881. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4882. (diff (abs (- level (length up-head) -1))))
  4883. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4884. (replace-match up-head nil t)
  4885. ;; Fixup tag positioning
  4886. (and org-auto-align-tags (org-set-tags nil t))
  4887. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4888. (defun org-demote ()
  4889. "Demote the current heading lower down the tree.
  4890. If the region is active in `transient-mark-mode', demote all headings
  4891. in the region."
  4892. (org-back-to-heading t)
  4893. (let* ((level (save-match-data (funcall outline-level)))
  4894. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4895. (diff (abs (- level (length down-head) -1))))
  4896. (replace-match down-head nil t)
  4897. ;; Fixup tag positioning
  4898. (and org-auto-align-tags (org-set-tags nil t))
  4899. (if org-adapt-indentation (org-fixup-indentation diff))))
  4900. (defun org-map-tree (fun)
  4901. "Call FUN for every heading underneath the current one."
  4902. (org-back-to-heading)
  4903. (let ((level (funcall outline-level)))
  4904. (save-excursion
  4905. (funcall fun)
  4906. (while (and (progn
  4907. (outline-next-heading)
  4908. (> (funcall outline-level) level))
  4909. (not (eobp)))
  4910. (funcall fun)))))
  4911. (defun org-map-region (fun beg end)
  4912. "Call FUN for every heading between BEG and END."
  4913. (let ((org-ignore-region t))
  4914. (save-excursion
  4915. (setq end (copy-marker end))
  4916. (goto-char beg)
  4917. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4918. (< (point) end))
  4919. (funcall fun))
  4920. (while (and (progn
  4921. (outline-next-heading)
  4922. (< (point) end))
  4923. (not (eobp)))
  4924. (funcall fun)))))
  4925. (defun org-fixup-indentation (diff)
  4926. "Change the indentation in the current entry by DIFF
  4927. However, if any line in the current entry has no indentation, or if it
  4928. would end up with no indentation after the change, nothing at all is done."
  4929. (save-excursion
  4930. (let ((end (save-excursion (outline-next-heading)
  4931. (point-marker)))
  4932. (prohibit (if (> diff 0)
  4933. "^\\S-"
  4934. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4935. col)
  4936. (unless (save-excursion (end-of-line 1)
  4937. (re-search-forward prohibit end t))
  4938. (while (and (< (point) end)
  4939. (re-search-forward "^[ \t]+" end t))
  4940. (goto-char (match-end 0))
  4941. (setq col (current-column))
  4942. (if (< diff 0) (replace-match ""))
  4943. (org-indent-to-column (+ diff col))))
  4944. (move-marker end nil))))
  4945. (defun org-convert-to-odd-levels ()
  4946. "Convert an org-mode file with all levels allowed to one with odd levels.
  4947. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4948. level 5 etc."
  4949. (interactive)
  4950. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4951. (let ((org-odd-levels-only nil) n)
  4952. (save-excursion
  4953. (goto-char (point-min))
  4954. (while (re-search-forward "^\\*\\*+ " nil t)
  4955. (setq n (- (length (match-string 0)) 2))
  4956. (while (>= (setq n (1- n)) 0)
  4957. (org-demote))
  4958. (end-of-line 1))))))
  4959. (defun org-convert-to-oddeven-levels ()
  4960. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4961. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4962. section with an even level, conversion would destroy the structure of the file. An error
  4963. is signaled in this case."
  4964. (interactive)
  4965. (goto-char (point-min))
  4966. ;; First check if there are no even levels
  4967. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4968. (org-show-context t)
  4969. (error "Not all levels are odd in this file. Conversion not possible."))
  4970. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4971. (let ((org-odd-levels-only nil) n)
  4972. (save-excursion
  4973. (goto-char (point-min))
  4974. (while (re-search-forward "^\\*\\*+ " nil t)
  4975. (setq n (/ (1- (length (match-string 0))) 2))
  4976. (while (>= (setq n (1- n)) 0)
  4977. (org-promote))
  4978. (end-of-line 1))))))
  4979. (defun org-tr-level (n)
  4980. "Make N odd if required."
  4981. (if org-odd-levels-only (1+ (/ n 2)) n))
  4982. ;;; Vertical tree motion, cutting and pasting of subtrees
  4983. (defun org-move-subtree-up (&optional arg)
  4984. "Move the current subtree up past ARG headlines of the same level."
  4985. (interactive "p")
  4986. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4987. (defun org-move-subtree-down (&optional arg)
  4988. "Move the current subtree down past ARG headlines of the same level."
  4989. (interactive "p")
  4990. (setq arg (prefix-numeric-value arg))
  4991. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4992. 'outline-get-last-sibling))
  4993. (ins-point (make-marker))
  4994. (cnt (abs arg))
  4995. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4996. ;; Select the tree
  4997. (org-back-to-heading)
  4998. (setq beg0 (point))
  4999. (save-excursion
  5000. (setq ne-beg (org-back-over-empty-lines))
  5001. (setq beg (point)))
  5002. (save-match-data
  5003. (save-excursion (outline-end-of-heading)
  5004. (setq folded (org-invisible-p)))
  5005. (outline-end-of-subtree))
  5006. (outline-next-heading)
  5007. (setq ne-end (org-back-over-empty-lines))
  5008. (setq end (point))
  5009. (goto-char beg0)
  5010. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5011. ;; include less whitespace
  5012. (save-excursion
  5013. (goto-char beg)
  5014. (forward-line (- ne-beg ne-end))
  5015. (setq beg (point))))
  5016. ;; Find insertion point, with error handling
  5017. (while (> cnt 0)
  5018. (or (and (funcall movfunc) (looking-at outline-regexp))
  5019. (progn (goto-char beg0)
  5020. (error "Cannot move past superior level or buffer limit")))
  5021. (setq cnt (1- cnt)))
  5022. (if (> arg 0)
  5023. ;; Moving forward - still need to move over subtree
  5024. (progn (org-end-of-subtree t t)
  5025. (save-excursion
  5026. (org-back-over-empty-lines)
  5027. (or (bolp) (newline)))))
  5028. (setq ne-ins (org-back-over-empty-lines))
  5029. (move-marker ins-point (point))
  5030. (setq txt (buffer-substring beg end))
  5031. (org-save-markers-in-region beg end)
  5032. (delete-region beg end)
  5033. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5034. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5035. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5036. (let ((bbb (point)))
  5037. (insert-before-markers txt)
  5038. (org-reinstall-markers-in-region bbb)
  5039. (move-marker ins-point bbb))
  5040. (or (bolp) (insert "\n"))
  5041. (setq ins-end (point))
  5042. (goto-char ins-point)
  5043. (org-skip-whitespace)
  5044. (when (and (< arg 0)
  5045. (org-first-sibling-p)
  5046. (> ne-ins ne-beg))
  5047. ;; Move whitespace back to beginning
  5048. (save-excursion
  5049. (goto-char ins-end)
  5050. (let ((kill-whole-line t))
  5051. (kill-line (- ne-ins ne-beg)) (point)))
  5052. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5053. (move-marker ins-point nil)
  5054. (org-compact-display-after-subtree-move)
  5055. (org-show-empty-lines-in-parent)
  5056. (unless folded
  5057. (org-show-entry)
  5058. (show-children)
  5059. (org-cycle-hide-drawers 'children))))
  5060. (defvar org-subtree-clip ""
  5061. "Clipboard for cut and paste of subtrees.
  5062. This is actually only a copy of the kill, because we use the normal kill
  5063. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5064. (defvar org-subtree-clip-folded nil
  5065. "Was the last copied subtree folded?
  5066. This is used to fold the tree back after pasting.")
  5067. (defun org-cut-subtree (&optional n)
  5068. "Cut the current subtree into the clipboard.
  5069. With prefix arg N, cut this many sequential subtrees.
  5070. This is a short-hand for marking the subtree and then cutting it."
  5071. (interactive "p")
  5072. (org-copy-subtree n 'cut))
  5073. (defun org-copy-subtree (&optional n cut force-store-markers)
  5074. "Cut the current subtree into the clipboard.
  5075. With prefix arg N, cut this many sequential subtrees.
  5076. This is a short-hand for marking the subtree and then copying it.
  5077. If CUT is non-nil, actually cut the subtree.
  5078. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5079. of some markers in the region, even if CUT is non-nil. This is
  5080. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5081. (interactive "p")
  5082. (let (beg end folded (beg0 (point)))
  5083. (if (interactive-p)
  5084. (org-back-to-heading nil) ; take what looks like a subtree
  5085. (org-back-to-heading t)) ; take what is really there
  5086. (org-back-over-empty-lines)
  5087. (setq beg (point))
  5088. (skip-chars-forward " \t\r\n")
  5089. (save-match-data
  5090. (save-excursion (outline-end-of-heading)
  5091. (setq folded (org-invisible-p)))
  5092. (condition-case nil
  5093. (outline-forward-same-level (1- n))
  5094. (error nil))
  5095. (org-end-of-subtree t t))
  5096. (org-back-over-empty-lines)
  5097. (setq end (point))
  5098. (goto-char beg0)
  5099. (when (> end beg)
  5100. (setq org-subtree-clip-folded folded)
  5101. (when (or cut force-store-markers)
  5102. (org-save-markers-in-region beg end))
  5103. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5104. (setq org-subtree-clip (current-kill 0))
  5105. (message "%s: Subtree(s) with %d characters"
  5106. (if cut "Cut" "Copied")
  5107. (length org-subtree-clip)))))
  5108. (defun org-paste-subtree (&optional level tree for-yank)
  5109. "Paste the clipboard as a subtree, with modification of headline level.
  5110. The entire subtree is promoted or demoted in order to match a new headline
  5111. level.
  5112. If the cursor is at the beginning of a headline, the same level as
  5113. that headline is used to paste the tree
  5114. If not, the new level is derived from the *visible* headings
  5115. before and after the insertion point, and taken to be the inferior headline
  5116. level of the two. So if the previous visible heading is level 3 and the
  5117. next is level 4 (or vice versa), level 4 will be used for insertion.
  5118. This makes sure that the subtree remains an independent subtree and does
  5119. not swallow low level entries.
  5120. You can also force a different level, either by using a numeric prefix
  5121. argument, or by inserting the heading marker by hand. For example, if the
  5122. cursor is after \"*****\", then the tree will be shifted to level 5.
  5123. If optional TREE is given, use this text instead of the kill ring.
  5124. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5125. move back over whitespace before inserting, and move point to the end of
  5126. the inserted text when done."
  5127. (interactive "P")
  5128. (unless (org-kill-is-subtree-p tree)
  5129. (error "%s"
  5130. (substitute-command-keys
  5131. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5132. (let* ((visp (not (org-invisible-p)))
  5133. (txt (or tree (and kill-ring (current-kill 0))))
  5134. (^re (concat "^\\(" outline-regexp "\\)"))
  5135. (re (concat "\\(" outline-regexp "\\)"))
  5136. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5137. (old-level (if (string-match ^re txt)
  5138. (- (match-end 0) (match-beginning 0) 1)
  5139. -1))
  5140. (force-level (cond (level (prefix-numeric-value level))
  5141. ((and (looking-at "[ \t]*$")
  5142. (string-match
  5143. ^re_ (buffer-substring
  5144. (point-at-bol) (point))))
  5145. (- (match-end 1) (match-beginning 1)))
  5146. ((and (bolp)
  5147. (looking-at org-outline-regexp))
  5148. (- (match-end 0) (point) 1))
  5149. (t nil)))
  5150. (previous-level (save-excursion
  5151. (condition-case nil
  5152. (progn
  5153. (outline-previous-visible-heading 1)
  5154. (if (looking-at re)
  5155. (- (match-end 0) (match-beginning 0) 1)
  5156. 1))
  5157. (error 1))))
  5158. (next-level (save-excursion
  5159. (condition-case nil
  5160. (progn
  5161. (or (looking-at outline-regexp)
  5162. (outline-next-visible-heading 1))
  5163. (if (looking-at re)
  5164. (- (match-end 0) (match-beginning 0) 1)
  5165. 1))
  5166. (error 1))))
  5167. (new-level (or force-level (max previous-level next-level)))
  5168. (shift (if (or (= old-level -1)
  5169. (= new-level -1)
  5170. (= old-level new-level))
  5171. 0
  5172. (- new-level old-level)))
  5173. (delta (if (> shift 0) -1 1))
  5174. (func (if (> shift 0) 'org-demote 'org-promote))
  5175. (org-odd-levels-only nil)
  5176. beg end newend)
  5177. ;; Remove the forced level indicator
  5178. (if force-level
  5179. (delete-region (point-at-bol) (point)))
  5180. ;; Paste
  5181. (beginning-of-line 1)
  5182. (unless for-yank (org-back-over-empty-lines))
  5183. (setq beg (point))
  5184. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5185. (insert-before-markers txt)
  5186. (unless (string-match "\n\\'" txt) (insert "\n"))
  5187. (setq newend (point))
  5188. (org-reinstall-markers-in-region beg)
  5189. (setq end (point))
  5190. (goto-char beg)
  5191. (skip-chars-forward " \t\n\r")
  5192. (setq beg (point))
  5193. (if (and (org-invisible-p) visp)
  5194. (save-excursion (outline-show-heading)))
  5195. ;; Shift if necessary
  5196. (unless (= shift 0)
  5197. (save-restriction
  5198. (narrow-to-region beg end)
  5199. (while (not (= shift 0))
  5200. (org-map-region func (point-min) (point-max))
  5201. (setq shift (+ delta shift)))
  5202. (goto-char (point-min))
  5203. (setq newend (point-max))))
  5204. (when (or (interactive-p) for-yank)
  5205. (message "Clipboard pasted as level %d subtree" new-level))
  5206. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5207. kill-ring
  5208. (eq org-subtree-clip (current-kill 0))
  5209. org-subtree-clip-folded)
  5210. ;; The tree was folded before it was killed/copied
  5211. (hide-subtree))
  5212. (and for-yank (goto-char newend))))
  5213. (defun org-kill-is-subtree-p (&optional txt)
  5214. "Check if the current kill is an outline subtree, or a set of trees.
  5215. Returns nil if kill does not start with a headline, or if the first
  5216. headline level is not the largest headline level in the tree.
  5217. So this will actually accept several entries of equal levels as well,
  5218. which is OK for `org-paste-subtree'.
  5219. If optional TXT is given, check this string instead of the current kill."
  5220. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5221. (start-level (and kill
  5222. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5223. org-outline-regexp "\\)")
  5224. kill)
  5225. (- (match-end 2) (match-beginning 2) 1)))
  5226. (re (concat "^" org-outline-regexp))
  5227. (start (1+ (or (match-beginning 2) -1))))
  5228. (if (not start-level)
  5229. (progn
  5230. nil) ;; does not even start with a heading
  5231. (catch 'exit
  5232. (while (setq start (string-match re kill (1+ start)))
  5233. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5234. (throw 'exit nil)))
  5235. t))))
  5236. (defvar org-markers-to-move nil
  5237. "Markers that should be moved with a cut-and-paste operation.
  5238. Those markers are stored together with their positions relative to
  5239. the start of the region.")
  5240. (defun org-save-markers-in-region (beg end)
  5241. "Check markers in region.
  5242. If these markers are between BEG and END, record their position relative
  5243. to BEG, so that after moving the block of text, we can put the markers back
  5244. into place.
  5245. This function gets called just before an entry or tree gets cut from the
  5246. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5247. called immediately, to move the markers with the entries."
  5248. (setq org-markers-to-move nil)
  5249. (when (featurep 'org-clock)
  5250. (org-clock-save-markers-for-cut-and-paste beg end))
  5251. (when (featurep 'org-agenda)
  5252. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5253. (defun org-check-and-save-marker (marker beg end)
  5254. "Check if MARKER is between BEG and END.
  5255. If yes, remember the marker and the distance to BEG."
  5256. (when (and (marker-buffer marker)
  5257. (equal (marker-buffer marker) (current-buffer)))
  5258. (if (and (>= marker beg) (< marker end))
  5259. (push (cons marker (- marker beg)) org-markers-to-move))))
  5260. (defun org-reinstall-markers-in-region (beg)
  5261. "Move all remembered markers to their position relative to BEG."
  5262. (mapc (lambda (x)
  5263. (move-marker (car x) (+ beg (cdr x))))
  5264. org-markers-to-move)
  5265. (setq org-markers-to-move nil))
  5266. (defun org-narrow-to-subtree ()
  5267. "Narrow buffer to the current subtree."
  5268. (interactive)
  5269. (save-excursion
  5270. (save-match-data
  5271. (narrow-to-region
  5272. (progn (org-back-to-heading) (point))
  5273. (progn (org-end-of-subtree t) (point))))))
  5274. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5275. "Clone the task (subtree) at point N times.
  5276. The clones will be inserted as siblings.
  5277. In interactive use, the user will be prompted for the number of clones
  5278. to be produced, and for a time SHIFT, which may be a repeater as used
  5279. in time stamps, for example `+3d'.
  5280. When a valid repeater is given and the entry contains any time stamps,
  5281. the clones will become a sequence in time, with time stamps in the
  5282. subtree shifted for each clone produced. If SHIFT is nil or the
  5283. empty string, time stamps will be left alone.
  5284. If the original subtree did contain time stamps with a repeater,
  5285. the following will happen:
  5286. - the repeater will be removed in each clone
  5287. - an additional clone will be produced, with the current, unshifted
  5288. date(s) in the entry.
  5289. - the original entry will be placed *after* all the clones, with
  5290. repeater intact.
  5291. - the start days in the repeater in the original entry will be shifted
  5292. to past the last clone.
  5293. I this way you can spell out a number of instances of a repeating task,
  5294. and still retain the repeater to cover future instances of the task."
  5295. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5296. (let (beg end template task
  5297. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5298. (if (not (and (integerp n) (> n 0)))
  5299. (error "Invalid number of replications %s" n))
  5300. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5301. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5302. shift)))
  5303. (error "Invalid shift specification %s" shift))
  5304. (when doshift
  5305. (setq shift-n (string-to-number (match-string 1 shift))
  5306. shift-what (cdr (assoc (match-string 2 shift)
  5307. '(("d" . day) ("w" . week)
  5308. ("m" . month) ("y" . year))))))
  5309. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5310. (setq nmin 1 nmax n)
  5311. (org-back-to-heading t)
  5312. (setq beg (point))
  5313. (org-end-of-subtree t t)
  5314. (setq end (point))
  5315. (setq template (buffer-substring beg end))
  5316. (when (and doshift
  5317. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5318. (delete-region beg end)
  5319. (setq end beg)
  5320. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5321. (goto-char end)
  5322. (loop for n from nmin to nmax do
  5323. (if (not doshift)
  5324. (setq task template)
  5325. (with-temp-buffer
  5326. (insert template)
  5327. (org-mode)
  5328. (goto-char (point-min))
  5329. (while (re-search-forward org-ts-regexp nil t)
  5330. (org-timestamp-change (* n shift-n) shift-what))
  5331. (unless (= n n-no-remove)
  5332. (goto-char (point-min))
  5333. (while (re-search-forward org-ts-regexp nil t)
  5334. (save-excursion
  5335. (goto-char (match-beginning 0))
  5336. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5337. (delete-region (match-beginning 1) (match-end 1))))))
  5338. (setq task (buffer-string))))
  5339. (insert task))
  5340. (goto-char beg)))
  5341. ;;; Outline Sorting
  5342. (defun org-sort (with-case)
  5343. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5344. Optional argument WITH-CASE means sort case-sensitively.
  5345. With a double prefix argument, also remove duplicate entries."
  5346. (interactive "P")
  5347. (if (org-at-table-p)
  5348. (org-call-with-arg 'org-table-sort-lines with-case)
  5349. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5350. (defun org-sort-remove-invisible (s)
  5351. (remove-text-properties 0 (length s) org-rm-props s)
  5352. (while (string-match org-bracket-link-regexp s)
  5353. (setq s (replace-match (if (match-end 2)
  5354. (match-string 3 s)
  5355. (match-string 1 s)) t t s)))
  5356. s)
  5357. (defvar org-priority-regexp) ; defined later in the file
  5358. (defun org-sort-entries-or-items
  5359. (&optional with-case sorting-type getkey-func compare-func property)
  5360. "Sort entries on a certain level of an outline tree.
  5361. If there is an active region, the entries in the region are sorted.
  5362. Else, if the cursor is before the first entry, sort the top-level items.
  5363. Else, the children of the entry at point are sorted.
  5364. Sorting can be alphabetically, numerically, and by date/time as given by
  5365. the first time stamp in the entry. The command prompts for the sorting
  5366. type unless it has been given to the function through the SORTING-TYPE
  5367. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5368. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5369. called with point at the beginning of the record. It must return either
  5370. a string or a number that should serve as the sorting key for that record.
  5371. Comparing entries ignores case by default. However, with an optional argument
  5372. WITH-CASE, the sorting considers case as well."
  5373. (interactive "P")
  5374. (let ((case-func (if with-case 'identity 'downcase))
  5375. start beg end stars re re2
  5376. txt what tmp plain-list-p)
  5377. ;; Find beginning and end of region to sort
  5378. (cond
  5379. ((org-region-active-p)
  5380. ;; we will sort the region
  5381. (setq end (region-end)
  5382. what "region")
  5383. (goto-char (region-beginning))
  5384. (if (not (org-on-heading-p)) (outline-next-heading))
  5385. (setq start (point)))
  5386. ((org-at-item-p)
  5387. ;; we will sort this plain list
  5388. (org-beginning-of-item-list) (setq start (point))
  5389. (org-end-of-item-list) (setq end (point))
  5390. (goto-char start)
  5391. (setq plain-list-p t
  5392. what "plain list"))
  5393. ((or (org-on-heading-p)
  5394. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5395. ;; we will sort the children of the current headline
  5396. (org-back-to-heading)
  5397. (setq start (point)
  5398. end (progn (org-end-of-subtree t t)
  5399. (org-back-over-empty-lines)
  5400. (point))
  5401. what "children")
  5402. (goto-char start)
  5403. (show-subtree)
  5404. (outline-next-heading))
  5405. (t
  5406. ;; we will sort the top-level entries in this file
  5407. (goto-char (point-min))
  5408. (or (org-on-heading-p) (outline-next-heading))
  5409. (setq start (point) end (point-max) what "top-level")
  5410. (goto-char start)
  5411. (show-all)))
  5412. (setq beg (point))
  5413. (if (>= beg end) (error "Nothing to sort"))
  5414. (unless plain-list-p
  5415. (looking-at "\\(\\*+\\)")
  5416. (setq stars (match-string 1)
  5417. re (concat "^" (regexp-quote stars) " +")
  5418. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5419. txt (buffer-substring beg end))
  5420. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5421. (if (and (not (equal stars "*")) (string-match re2 txt))
  5422. (error "Region to sort contains a level above the first entry")))
  5423. (unless sorting-type
  5424. (message
  5425. (if plain-list-p
  5426. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5427. "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:")
  5428. what)
  5429. (setq sorting-type (read-char-exclusive))
  5430. (and (= (downcase sorting-type) ?f)
  5431. (setq getkey-func
  5432. (org-ido-completing-read "Sort using function: "
  5433. obarray 'fboundp t nil nil))
  5434. (setq getkey-func (intern getkey-func)))
  5435. (and (= (downcase sorting-type) ?r)
  5436. (setq property
  5437. (org-ido-completing-read "Property: "
  5438. (mapcar 'list (org-buffer-property-keys t))
  5439. nil t))))
  5440. (message "Sorting entries...")
  5441. (save-restriction
  5442. (narrow-to-region start end)
  5443. (let ((dcst (downcase sorting-type))
  5444. (now (current-time)))
  5445. (sort-subr
  5446. (/= dcst sorting-type)
  5447. ;; This function moves to the beginning character of the "record" to
  5448. ;; be sorted.
  5449. (if plain-list-p
  5450. (lambda nil
  5451. (if (org-at-item-p) t (goto-char (point-max))))
  5452. (lambda nil
  5453. (if (re-search-forward re nil t)
  5454. (goto-char (match-beginning 0))
  5455. (goto-char (point-max)))))
  5456. ;; This function moves to the last character of the "record" being
  5457. ;; sorted.
  5458. (if plain-list-p
  5459. 'org-end-of-item
  5460. (lambda nil
  5461. (save-match-data
  5462. (condition-case nil
  5463. (outline-forward-same-level 1)
  5464. (error
  5465. (goto-char (point-max)))))))
  5466. ;; This function returns the value that gets sorted against.
  5467. (if plain-list-p
  5468. (lambda nil
  5469. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5470. (cond
  5471. ((= dcst ?n)
  5472. (string-to-number (buffer-substring (match-end 0)
  5473. (point-at-eol))))
  5474. ((= dcst ?a)
  5475. (buffer-substring (match-end 0) (point-at-eol)))
  5476. ((= dcst ?t)
  5477. (if (re-search-forward org-ts-regexp
  5478. (point-at-eol) t)
  5479. (org-time-string-to-time (match-string 0))
  5480. now))
  5481. ((= dcst ?f)
  5482. (if getkey-func
  5483. (progn
  5484. (setq tmp (funcall getkey-func))
  5485. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5486. tmp)
  5487. (error "Invalid key function `%s'" getkey-func)))
  5488. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5489. (lambda nil
  5490. (cond
  5491. ((= dcst ?n)
  5492. (if (looking-at org-complex-heading-regexp)
  5493. (string-to-number (match-string 4))
  5494. nil))
  5495. ((= dcst ?a)
  5496. (if (looking-at org-complex-heading-regexp)
  5497. (funcall case-func (match-string 4))
  5498. nil))
  5499. ((= dcst ?t)
  5500. (if (re-search-forward org-ts-regexp
  5501. (save-excursion
  5502. (forward-line 2)
  5503. (point)) t)
  5504. (org-time-string-to-time (match-string 0))
  5505. now))
  5506. ((= dcst ?p)
  5507. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5508. (string-to-char (match-string 2))
  5509. org-default-priority))
  5510. ((= dcst ?r)
  5511. (or (org-entry-get nil property) ""))
  5512. ((= dcst ?o)
  5513. (if (looking-at org-complex-heading-regexp)
  5514. (- 9999 (length (member (match-string 2)
  5515. org-todo-keywords-1)))))
  5516. ((= dcst ?f)
  5517. (if getkey-func
  5518. (progn
  5519. (setq tmp (funcall getkey-func))
  5520. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5521. tmp)
  5522. (error "Invalid key function `%s'" getkey-func)))
  5523. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5524. nil
  5525. (cond
  5526. ((= dcst ?a) 'string<)
  5527. ((= dcst ?t) 'time-less-p)
  5528. ((= dcst ?f) compare-func)
  5529. (t nil)))))
  5530. (message "Sorting entries...done")))
  5531. (defun org-do-sort (table what &optional with-case sorting-type)
  5532. "Sort TABLE of WHAT according to SORTING-TYPE.
  5533. The user will be prompted for the SORTING-TYPE if the call to this
  5534. function does not specify it. WHAT is only for the prompt, to indicate
  5535. what is being sorted. The sorting key will be extracted from
  5536. the car of the elements of the table.
  5537. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5538. (unless sorting-type
  5539. (message
  5540. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5541. what)
  5542. (setq sorting-type (read-char-exclusive)))
  5543. (let ((dcst (downcase sorting-type))
  5544. extractfun comparefun)
  5545. ;; Define the appropriate functions
  5546. (cond
  5547. ((= dcst ?n)
  5548. (setq extractfun 'string-to-number
  5549. comparefun (if (= dcst sorting-type) '< '>)))
  5550. ((= dcst ?a)
  5551. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5552. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5553. comparefun (if (= dcst sorting-type)
  5554. 'string<
  5555. (lambda (a b) (and (not (string< a b))
  5556. (not (string= a b)))))))
  5557. ((= dcst ?t)
  5558. (setq extractfun
  5559. (lambda (x)
  5560. (if (or (string-match org-ts-regexp x)
  5561. (string-match org-ts-regexp-both x))
  5562. (time-to-seconds
  5563. (org-time-string-to-time (match-string 0 x)))
  5564. 0))
  5565. comparefun (if (= dcst sorting-type) '< '>)))
  5566. (t (error "Invalid sorting type `%c'" sorting-type)))
  5567. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5568. table)
  5569. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5570. ;;; Editing source examples
  5571. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5572. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5573. (defvar org-edit-src-force-single-line nil)
  5574. (defvar org-edit-src-from-org-mode nil)
  5575. (defvar org-edit-src-picture nil)
  5576. (define-minor-mode org-exit-edit-mode
  5577. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5578. (defun org-edit-src-code ()
  5579. "Edit the source code example at point.
  5580. An indirect buffer is created, and that buffer is then narrowed to the
  5581. example at point and switched to the correct language mode. When done,
  5582. exit by killing the buffer with \\[org-edit-src-exit]."
  5583. (interactive)
  5584. (let ((line (org-current-line))
  5585. (case-fold-search t)
  5586. (msg (substitute-command-keys
  5587. "Edit, then exit with C-c ' (C-c and single quote)"))
  5588. (info (org-edit-src-find-region-and-lang))
  5589. (org-mode-p (eq major-mode 'org-mode))
  5590. beg end lang lang-f single lfmt)
  5591. (if (not info)
  5592. nil
  5593. (setq beg (nth 0 info)
  5594. end (nth 1 info)
  5595. lang (nth 2 info)
  5596. single (nth 3 info)
  5597. lfmt (nth 4 info)
  5598. lang-f (intern (concat lang "-mode")))
  5599. (unless (functionp lang-f)
  5600. (error "No such language mode: %s" lang-f))
  5601. (goto-line line)
  5602. (if (get-buffer "*Org Edit Src Example*")
  5603. (kill-buffer "*Org Edit Src Example*"))
  5604. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5605. "*Org Edit Src Example*"))
  5606. (narrow-to-region beg end)
  5607. (remove-text-properties beg end '(display nil invisible nil
  5608. intangible nil))
  5609. (let ((org-inhibit-startup t))
  5610. (funcall lang-f))
  5611. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5612. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5613. (when lfmt
  5614. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5615. (when org-mode-p
  5616. (goto-char (point-min))
  5617. (while (re-search-forward "^," nil t)
  5618. (replace-match "")))
  5619. (goto-line line)
  5620. (org-exit-edit-mode)
  5621. (org-set-local 'header-line-format msg)
  5622. (message "%s" msg)
  5623. t)))
  5624. (defun org-edit-fixed-width-region ()
  5625. "Edit the fixed-width ascii drawing at point.
  5626. This must be a region where each line starts with a colon followed by
  5627. a space character.
  5628. An indirect buffer is created, and that buffer is then narrowed to the
  5629. example at point and switched to artist-mode. When done,
  5630. exit by killing the buffer with \\[org-edit-src-exit]."
  5631. (interactive)
  5632. (let ((line (org-current-line))
  5633. (case-fold-search t)
  5634. (msg (substitute-command-keys
  5635. "Edit, then exit with C-c ' (C-c and single quote)"))
  5636. (org-mode-p (eq major-mode 'org-mode))
  5637. beg end)
  5638. (beginning-of-line 1)
  5639. (if (looking-at "[ \t]*[^:\n \t]")
  5640. nil
  5641. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5642. (setq beg (point) end beg)
  5643. (save-excursion
  5644. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5645. (setq beg (point-at-bol 2))
  5646. (setq beg (point))))
  5647. (save-excursion
  5648. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5649. (setq end (1- (match-beginning 0)))
  5650. (setq end (point))))
  5651. (goto-line line))
  5652. (if (get-buffer "*Org Edit Picture*")
  5653. (kill-buffer "*Org Edit Picture*"))
  5654. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5655. "*Org Edit Picture*"))
  5656. (narrow-to-region beg end)
  5657. (remove-text-properties beg end '(display nil invisible nil
  5658. intangible nil))
  5659. (when (fboundp 'font-lock-unfontify-region)
  5660. (font-lock-unfontify-region (point-min) (point-max)))
  5661. (cond
  5662. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5663. (fundamental-mode)
  5664. (artist-mode 1))
  5665. (t (funcall org-edit-fixed-width-region-mode)))
  5666. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5667. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5668. (set (make-local-variable 'org-edit-src-picture) t)
  5669. (goto-char (point-min))
  5670. (while (re-search-forward "^[ \t]*: ?" nil t)
  5671. (replace-match ""))
  5672. (goto-line line)
  5673. (org-exit-edit-mode)
  5674. (org-set-local 'header-line-format msg)
  5675. (message "%s" msg)
  5676. t)))
  5677. (defun org-edit-src-find-region-and-lang ()
  5678. "Find the region and language for a local edit.
  5679. Return a list with beginning and end of the region, a string representing
  5680. the language, a switch telling of the content should be in a single line."
  5681. (let ((re-list
  5682. (append
  5683. org-edit-src-region-extra
  5684. '(
  5685. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5686. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5687. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5688. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5689. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5690. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5691. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5692. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5693. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5694. ("^#\\+html:" "\n" "html" single-line)
  5695. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5696. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5697. ("^#\\+latex:" "\n" "latex" single-line)
  5698. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5699. ("^#\\+ascii:" "\n" "ascii" single-line)
  5700. )))
  5701. (pos (point))
  5702. re1 re2 single beg end lang lfmt match-re1)
  5703. (catch 'exit
  5704. (while (setq entry (pop re-list))
  5705. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5706. single (nth 3 entry))
  5707. (save-excursion
  5708. (if (or (looking-at re1)
  5709. (re-search-backward re1 nil t))
  5710. (progn
  5711. (setq match-re1 (match-string 0))
  5712. (setq beg (match-end 0)
  5713. lang (org-edit-src-get-lang lang)
  5714. lfmt (org-edit-src-get-label-format match-re1))
  5715. (if (and (re-search-forward re2 nil t)
  5716. (>= (match-end 0) pos))
  5717. (throw 'exit (list beg (match-beginning 0)
  5718. lang single lfmt))))
  5719. (if (or (looking-at re2)
  5720. (re-search-forward re2 nil t))
  5721. (progn
  5722. (setq end (match-beginning 0))
  5723. (if (and (re-search-backward re1 nil t)
  5724. (<= (match-beginning 0) pos))
  5725. (progn
  5726. (setq lfmt (org-edit-src-get-label-format
  5727. (match-string 0)))
  5728. (throw 'exit
  5729. (list (match-end 0) end
  5730. (org-edit-src-get-lang lang)
  5731. single lfmt))))))))))))
  5732. (defun org-edit-src-get-lang (lang)
  5733. "Extract the src language."
  5734. (let ((m (match-string 0)))
  5735. (cond
  5736. ((stringp lang) lang)
  5737. ((integerp lang) (match-string lang))
  5738. ((and (eq lang 'lang)
  5739. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5740. (match-string 1 m))
  5741. ((and (eq lang 'style)
  5742. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5743. (match-string 1 m))
  5744. (t "fundamental"))))
  5745. (defun org-edit-src-get-label-format (s)
  5746. "Extract the label format."
  5747. (save-match-data
  5748. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5749. (match-string 1 s))))
  5750. (defun org-edit-src-exit ()
  5751. "Exit special edit and protect problematic lines."
  5752. (interactive)
  5753. (unless (buffer-base-buffer (current-buffer))
  5754. (error "This is not an indirect buffer, something is wrong..."))
  5755. (unless (> (point-min) 1)
  5756. (error "This buffer is not narrowed, something is wrong..."))
  5757. (goto-char (point-min))
  5758. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5759. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5760. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5761. (goto-char (point-min))
  5762. (while (re-search-forward "\n" nil t)
  5763. (replace-match " "))
  5764. (goto-char (point-min))
  5765. (if (looking-at "\\s-*") (replace-match " "))
  5766. (if (re-search-forward "\\s-+\\'" nil t)
  5767. (replace-match "")))
  5768. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5769. (goto-char (point-min))
  5770. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5771. (replace-match ",\\1"))
  5772. (when font-lock-mode
  5773. (font-lock-unfontify-region (point-min) (point-max)))
  5774. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5775. (when (org-bound-and-true-p org-edit-src-picture)
  5776. (untabify (point-min) (point-max))
  5777. (goto-char (point-min))
  5778. (while (re-search-forward "^" nil t)
  5779. (replace-match ": "))
  5780. (when font-lock-mode
  5781. (font-lock-unfontify-region (point-min) (point-max)))
  5782. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5783. (kill-buffer (current-buffer))
  5784. (and (org-mode-p) (org-restart-font-lock)))
  5785. ;;; The orgstruct minor mode
  5786. ;; Define a minor mode which can be used in other modes in order to
  5787. ;; integrate the org-mode structure editing commands.
  5788. ;; This is really a hack, because the org-mode structure commands use
  5789. ;; keys which normally belong to the major mode. Here is how it
  5790. ;; works: The minor mode defines all the keys necessary to operate the
  5791. ;; structure commands, but wraps the commands into a function which
  5792. ;; tests if the cursor is currently at a headline or a plain list
  5793. ;; item. If that is the case, the structure command is used,
  5794. ;; temporarily setting many Org-mode variables like regular
  5795. ;; expressions for filling etc. However, when any of those keys is
  5796. ;; used at a different location, function uses `key-binding' to look
  5797. ;; up if the key has an associated command in another currently active
  5798. ;; keymap (minor modes, major mode, global), and executes that
  5799. ;; command. There might be problems if any of the keys is otherwise
  5800. ;; used as a prefix key.
  5801. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5802. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5803. ;; addresses this by checking explicitly for both bindings.
  5804. (defvar orgstruct-mode-map (make-sparse-keymap)
  5805. "Keymap for the minor `orgstruct-mode'.")
  5806. (defvar org-local-vars nil
  5807. "List of local variables, for use by `orgstruct-mode'")
  5808. ;;;###autoload
  5809. (define-minor-mode orgstruct-mode
  5810. "Toggle the minor more `orgstruct-mode'.
  5811. This mode is for using Org-mode structure commands in other modes.
  5812. The following key behave as if Org-mode was active, if the cursor
  5813. is on a headline, or on a plain list item (both in the definition
  5814. of Org-mode).
  5815. M-up Move entry/item up
  5816. M-down Move entry/item down
  5817. M-left Promote
  5818. M-right Demote
  5819. M-S-up Move entry/item up
  5820. M-S-down Move entry/item down
  5821. M-S-left Promote subtree
  5822. M-S-right Demote subtree
  5823. M-q Fill paragraph and items like in Org-mode
  5824. C-c ^ Sort entries
  5825. C-c - Cycle list bullet
  5826. TAB Cycle item visibility
  5827. M-RET Insert new heading/item
  5828. S-M-RET Insert new TODO heading / Checkbox item
  5829. C-c C-c Set tags / toggle checkbox"
  5830. nil " OrgStruct" nil
  5831. (org-load-modules-maybe)
  5832. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5833. ;;;###autoload
  5834. (defun turn-on-orgstruct ()
  5835. "Unconditionally turn on `orgstruct-mode'."
  5836. (orgstruct-mode 1))
  5837. (defun orgstruct++-mode (&optional arg)
  5838. "Toggle `orgstruct-mode', the enhanced version of it.
  5839. In addition to setting orgstruct-mode, this also exports all indentation and
  5840. autofilling variables from org-mode into the buffer. It will also
  5841. recognize item context in multiline items.
  5842. Note that turning off orgstruct-mode will *not* remove the
  5843. indentation/paragraph settings. This can only be done by refreshing the
  5844. major mode, for example with \[normal-mode]."
  5845. (interactive "P")
  5846. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5847. (if (< arg 1)
  5848. (orgstruct-mode -1)
  5849. (orgstruct-mode 1)
  5850. (let (var val)
  5851. (mapc
  5852. (lambda (x)
  5853. (when (string-match
  5854. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5855. (symbol-name (car x)))
  5856. (setq var (car x) val (nth 1 x))
  5857. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5858. org-local-vars)
  5859. (org-set-local 'orgstruct-is-++ t))))
  5860. (defvar orgstruct-is-++ nil
  5861. "Is orgstruct-mode in ++ version in the current-buffer?")
  5862. (make-variable-buffer-local 'orgstruct-is-++)
  5863. ;;;###autoload
  5864. (defun turn-on-orgstruct++ ()
  5865. "Unconditionally turn on `orgstruct++-mode'."
  5866. (orgstruct++-mode 1))
  5867. (defun orgstruct-error ()
  5868. "Error when there is no default binding for a structure key."
  5869. (interactive)
  5870. (error "This key has no function outside structure elements"))
  5871. (defun orgstruct-setup ()
  5872. "Setup orgstruct keymaps."
  5873. (let ((nfunc 0)
  5874. (bindings
  5875. (list
  5876. '([(meta up)] org-metaup)
  5877. '([(meta down)] org-metadown)
  5878. '([(meta left)] org-metaleft)
  5879. '([(meta right)] org-metaright)
  5880. '([(meta shift up)] org-shiftmetaup)
  5881. '([(meta shift down)] org-shiftmetadown)
  5882. '([(meta shift left)] org-shiftmetaleft)
  5883. '([(meta shift right)] org-shiftmetaright)
  5884. '([(shift up)] org-shiftup)
  5885. '([(shift down)] org-shiftdown)
  5886. '([(shift left)] org-shiftleft)
  5887. '([(shift right)] org-shiftright)
  5888. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5889. '("\M-q" fill-paragraph)
  5890. '("\C-c^" org-sort)
  5891. '("\C-c-" org-cycle-list-bullet)))
  5892. elt key fun cmd)
  5893. (while (setq elt (pop bindings))
  5894. (setq nfunc (1+ nfunc))
  5895. (setq key (org-key (car elt))
  5896. fun (nth 1 elt)
  5897. cmd (orgstruct-make-binding fun nfunc key))
  5898. (org-defkey orgstruct-mode-map key cmd))
  5899. ;; Special treatment needed for TAB and RET
  5900. (org-defkey orgstruct-mode-map [(tab)]
  5901. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5902. (org-defkey orgstruct-mode-map "\C-i"
  5903. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5904. (org-defkey orgstruct-mode-map "\M-\C-m"
  5905. (orgstruct-make-binding 'org-insert-heading 105
  5906. "\M-\C-m" [(meta return)]))
  5907. (org-defkey orgstruct-mode-map [(meta return)]
  5908. (orgstruct-make-binding 'org-insert-heading 106
  5909. [(meta return)] "\M-\C-m"))
  5910. (org-defkey orgstruct-mode-map [(shift meta return)]
  5911. (orgstruct-make-binding 'org-insert-todo-heading 107
  5912. [(meta return)] "\M-\C-m"))
  5913. (unless org-local-vars
  5914. (setq org-local-vars (org-get-local-variables)))
  5915. t))
  5916. (defun orgstruct-make-binding (fun n &rest keys)
  5917. "Create a function for binding in the structure minor mode.
  5918. FUN is the command to call inside a table. N is used to create a unique
  5919. command name. KEYS are keys that should be checked in for a command
  5920. to execute outside of tables."
  5921. (eval
  5922. (list 'defun
  5923. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5924. '(arg)
  5925. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5926. "Outside of structure, run the binding of `"
  5927. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5928. "'.")
  5929. '(interactive "p")
  5930. (list 'if
  5931. `(org-context-p 'headline 'item
  5932. (and orgstruct-is-++
  5933. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5934. 'item-body))
  5935. (list 'org-run-like-in-org-mode (list 'quote fun))
  5936. (list 'let '(orgstruct-mode)
  5937. (list 'call-interactively
  5938. (append '(or)
  5939. (mapcar (lambda (k)
  5940. (list 'key-binding k))
  5941. keys)
  5942. '('orgstruct-error))))))))
  5943. (defun org-context-p (&rest contexts)
  5944. "Check if local context is any of CONTEXTS.
  5945. Possible values in the list of contexts are `table', `headline', and `item'."
  5946. (let ((pos (point)))
  5947. (goto-char (point-at-bol))
  5948. (prog1 (or (and (memq 'table contexts)
  5949. (looking-at "[ \t]*|"))
  5950. (and (memq 'headline contexts)
  5951. ;;????????? (looking-at "\\*+"))
  5952. (looking-at outline-regexp))
  5953. (and (memq 'item contexts)
  5954. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5955. (and (memq 'item-body contexts)
  5956. (org-in-item-p)))
  5957. (goto-char pos))))
  5958. (defun org-get-local-variables ()
  5959. "Return a list of all local variables in an org-mode buffer."
  5960. (let (varlist)
  5961. (with-current-buffer (get-buffer-create "*Org tmp*")
  5962. (erase-buffer)
  5963. (org-mode)
  5964. (setq varlist (buffer-local-variables)))
  5965. (kill-buffer "*Org tmp*")
  5966. (delq nil
  5967. (mapcar
  5968. (lambda (x)
  5969. (setq x
  5970. (if (symbolp x)
  5971. (list x)
  5972. (list (car x) (list 'quote (cdr x)))))
  5973. (if (string-match
  5974. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5975. (symbol-name (car x)))
  5976. x nil))
  5977. varlist))))
  5978. ;;;###autoload
  5979. (defun org-run-like-in-org-mode (cmd)
  5980. "Run a command, pretending that the current buffer is in Org-mode.
  5981. This will temporarily bind local variables that are typically bound in
  5982. Org-mode to the values they have in Org-mode, and then interactively
  5983. call CMD."
  5984. (org-load-modules-maybe)
  5985. (unless org-local-vars
  5986. (setq org-local-vars (org-get-local-variables)))
  5987. (eval (list 'let org-local-vars
  5988. (list 'call-interactively (list 'quote cmd)))))
  5989. ;;;; Archiving
  5990. (defun org-get-category (&optional pos)
  5991. "Get the category applying to position POS."
  5992. (get-text-property (or pos (point)) 'org-category))
  5993. (defun org-refresh-category-properties ()
  5994. "Refresh category text properties in the buffer."
  5995. (let ((def-cat (cond
  5996. ((null org-category)
  5997. (if buffer-file-name
  5998. (file-name-sans-extension
  5999. (file-name-nondirectory buffer-file-name))
  6000. "???"))
  6001. ((symbolp org-category) (symbol-name org-category))
  6002. (t org-category)))
  6003. beg end cat pos optionp)
  6004. (org-unmodified
  6005. (save-excursion
  6006. (save-restriction
  6007. (widen)
  6008. (goto-char (point-min))
  6009. (put-text-property (point) (point-max) 'org-category def-cat)
  6010. (while (re-search-forward
  6011. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6012. (setq pos (match-end 0)
  6013. optionp (equal (char-after (match-beginning 0)) ?#)
  6014. cat (org-trim (match-string 2)))
  6015. (if optionp
  6016. (setq beg (point-at-bol) end (point-max))
  6017. (org-back-to-heading t)
  6018. (setq beg (point) end (org-end-of-subtree t t)))
  6019. (put-text-property beg end 'org-category cat)
  6020. (goto-char pos)))))))
  6021. ;;;; Link Stuff
  6022. ;;; Link abbreviations
  6023. (defun org-link-expand-abbrev (link)
  6024. "Apply replacements as defined in `org-link-abbrev-alist."
  6025. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6026. (let* ((key (match-string 1 link))
  6027. (as (or (assoc key org-link-abbrev-alist-local)
  6028. (assoc key org-link-abbrev-alist)))
  6029. (tag (and (match-end 2) (match-string 3 link)))
  6030. rpl)
  6031. (if (not as)
  6032. link
  6033. (setq rpl (cdr as))
  6034. (cond
  6035. ((symbolp rpl) (funcall rpl tag))
  6036. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6037. ((string-match "%h" rpl)
  6038. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6039. (t (concat rpl tag)))))
  6040. link))
  6041. ;;; Storing and inserting links
  6042. (defvar org-insert-link-history nil
  6043. "Minibuffer history for links inserted with `org-insert-link'.")
  6044. (defvar org-stored-links nil
  6045. "Contains the links stored with `org-store-link'.")
  6046. (defvar org-store-link-plist nil
  6047. "Plist with info about the most recently link created with `org-store-link'.")
  6048. (defvar org-link-protocols nil
  6049. "Link protocols added to Org-mode using `org-add-link-type'.")
  6050. (defvar org-store-link-functions nil
  6051. "List of functions that are called to create and store a link.
  6052. Each function will be called in turn until one returns a non-nil
  6053. value. Each function should check if it is responsible for creating
  6054. this link (for example by looking at the major mode).
  6055. If not, it must exit and return nil.
  6056. If yes, it should return a non-nil value after a calling
  6057. `org-store-link-props' with a list of properties and values.
  6058. Special properties are:
  6059. :type The link prefix. like \"http\". This must be given.
  6060. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6061. This is obligatory as well.
  6062. :description Optional default description for the second pair
  6063. of brackets in an Org-mode link. The user can still change
  6064. this when inserting this link into an Org-mode buffer.
  6065. In addition to these, any additional properties can be specified
  6066. and then used in remember templates.")
  6067. (defun org-add-link-type (type &optional follow export)
  6068. "Add TYPE to the list of `org-link-types'.
  6069. Re-compute all regular expressions depending on `org-link-types'
  6070. FOLLOW and EXPORT are two functions.
  6071. FOLLOW should take the link path as the single argument and do whatever
  6072. is necessary to follow the link, for example find a file or display
  6073. a mail message.
  6074. EXPORT should format the link path for export to one of the export formats.
  6075. It should be a function accepting three arguments:
  6076. path the path of the link, the text after the prefix (like \"http:\")
  6077. desc the description of the link, if any, nil if there was no description
  6078. format the export format, a symbol like `html' or `latex'.
  6079. The function may use the FORMAT information to return different values
  6080. depending on the format. The return value will be put literally into
  6081. the exported file.
  6082. Org-mode has a built-in default for exporting links. If you are happy with
  6083. this default, there is no need to define an export function for the link
  6084. type. For a simple example of an export function, see `org-bbdb.el'."
  6085. (add-to-list 'org-link-types type t)
  6086. (org-make-link-regexps)
  6087. (if (assoc type org-link-protocols)
  6088. (setcdr (assoc type org-link-protocols) (list follow export))
  6089. (push (list type follow export) org-link-protocols)))
  6090. ;;;###autoload
  6091. (defun org-store-link (arg)
  6092. "\\<org-mode-map>Store an org-link to the current location.
  6093. This link is added to `org-stored-links' and can later be inserted
  6094. into an org-buffer with \\[org-insert-link].
  6095. For some link types, a prefix arg is interpreted:
  6096. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6097. For file links, arg negates `org-context-in-file-links'."
  6098. (interactive "P")
  6099. (org-load-modules-maybe)
  6100. (setq org-store-link-plist nil) ; reset
  6101. (let (link cpltxt desc description search txt)
  6102. (cond
  6103. ((run-hook-with-args-until-success 'org-store-link-functions)
  6104. (setq link (plist-get org-store-link-plist :link)
  6105. desc (or (plist-get org-store-link-plist :description) link)))
  6106. ((equal (buffer-name) "*Org Edit Src Example*")
  6107. (let (label gc)
  6108. (while (or (not label)
  6109. (save-excursion
  6110. (save-restriction
  6111. (widen)
  6112. (goto-char (point-min))
  6113. (re-search-forward
  6114. (regexp-quote (format org-coderef-label-format label))
  6115. nil t))))
  6116. (when label (message "Label exists already") (sit-for 2))
  6117. (setq label (read-string "Code line label: " label)))
  6118. (end-of-line 1)
  6119. (setq link (format org-coderef-label-format label))
  6120. (setq gc (- 79 (length link)))
  6121. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6122. (insert link)
  6123. (setq link (concat "(" label ")") desc nil)))
  6124. ((eq major-mode 'calendar-mode)
  6125. (let ((cd (calendar-cursor-to-date)))
  6126. (setq link
  6127. (format-time-string
  6128. (car org-time-stamp-formats)
  6129. (apply 'encode-time
  6130. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6131. nil nil nil))))
  6132. (org-store-link-props :type "calendar" :date cd)))
  6133. ((eq major-mode 'w3-mode)
  6134. (setq cpltxt (if (and (buffer-name)
  6135. (not (string-match "Untitled" (buffer-name))))
  6136. (buffer-name)
  6137. (url-view-url t))
  6138. link (org-make-link (url-view-url t)))
  6139. (org-store-link-props :type "w3" :url (url-view-url t)))
  6140. ((eq major-mode 'w3m-mode)
  6141. (setq cpltxt (or w3m-current-title w3m-current-url)
  6142. link (org-make-link w3m-current-url))
  6143. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6144. ((setq search (run-hook-with-args-until-success
  6145. 'org-create-file-search-functions))
  6146. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6147. "::" search))
  6148. (setq cpltxt (or description link)))
  6149. ((eq major-mode 'image-mode)
  6150. (setq cpltxt (concat "file:"
  6151. (abbreviate-file-name buffer-file-name))
  6152. link (org-make-link cpltxt))
  6153. (org-store-link-props :type "image" :file buffer-file-name))
  6154. ((eq major-mode 'dired-mode)
  6155. ;; link to the file in the current line
  6156. (setq cpltxt (concat "file:"
  6157. (abbreviate-file-name
  6158. (expand-file-name
  6159. (dired-get-filename nil t))))
  6160. link (org-make-link cpltxt)))
  6161. ((and buffer-file-name (org-mode-p))
  6162. (cond
  6163. ((org-in-regexp "<<\\(.*?\\)>>")
  6164. (setq cpltxt
  6165. (concat "file:"
  6166. (abbreviate-file-name buffer-file-name)
  6167. "::" (match-string 1))
  6168. link (org-make-link cpltxt)))
  6169. ((and (featurep 'org-id)
  6170. (or (eq org-link-to-org-use-id t)
  6171. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6172. (interactive-p))
  6173. (and org-link-to-org-use-id
  6174. (condition-case nil
  6175. (org-entry-get nil "ID")
  6176. (error nil)))))
  6177. ;; We can make a link using the ID.
  6178. (setq link (condition-case nil
  6179. (prog1 (org-id-store-link)
  6180. (setq desc (plist-get org-store-link-plist
  6181. :description)))
  6182. (error
  6183. ;; probably before first headline, link to file only
  6184. (concat "file:"
  6185. (abbreviate-file-name buffer-file-name))))))
  6186. (t
  6187. ;; Just link to current headline
  6188. (setq cpltxt (concat "file:"
  6189. (abbreviate-file-name buffer-file-name)))
  6190. ;; Add a context search string
  6191. (when (org-xor org-context-in-file-links arg)
  6192. (setq txt (cond
  6193. ((org-on-heading-p) nil)
  6194. ((org-region-active-p)
  6195. (buffer-substring (region-beginning) (region-end)))
  6196. (t nil)))
  6197. (when (or (null txt) (string-match "\\S-" txt))
  6198. (setq cpltxt
  6199. (concat cpltxt "::"
  6200. (condition-case nil
  6201. (org-make-org-heading-search-string txt)
  6202. (error "")))
  6203. desc "NONE")))
  6204. (if (string-match "::\\'" cpltxt)
  6205. (setq cpltxt (substring cpltxt 0 -2)))
  6206. (setq link (org-make-link cpltxt)))))
  6207. ((buffer-file-name (buffer-base-buffer))
  6208. ;; Just link to this file here.
  6209. (setq cpltxt (concat "file:"
  6210. (abbreviate-file-name
  6211. (buffer-file-name (buffer-base-buffer)))))
  6212. ;; Add a context string
  6213. (when (org-xor org-context-in-file-links arg)
  6214. (setq txt (if (org-region-active-p)
  6215. (buffer-substring (region-beginning) (region-end))
  6216. (buffer-substring (point-at-bol) (point-at-eol))))
  6217. ;; Only use search option if there is some text.
  6218. (when (string-match "\\S-" txt)
  6219. (setq cpltxt
  6220. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6221. desc "NONE")))
  6222. (setq link (org-make-link cpltxt)))
  6223. ((interactive-p)
  6224. (error "Cannot link to a buffer which is not visiting a file"))
  6225. (t (setq link nil)))
  6226. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6227. (setq link (or link cpltxt)
  6228. desc (or desc cpltxt))
  6229. (if (equal desc "NONE") (setq desc nil))
  6230. (if (and (interactive-p) link)
  6231. (progn
  6232. (setq org-stored-links
  6233. (cons (list link desc) org-stored-links))
  6234. (message "Stored: %s" (or desc link)))
  6235. (and link (org-make-link-string link desc)))))
  6236. (defun org-store-link-props (&rest plist)
  6237. "Store link properties, extract names and addresses."
  6238. (let (x adr)
  6239. (when (setq x (plist-get plist :from))
  6240. (setq adr (mail-extract-address-components x))
  6241. (setq plist (plist-put plist :fromname (car adr)))
  6242. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6243. (when (setq x (plist-get plist :to))
  6244. (setq adr (mail-extract-address-components x))
  6245. (setq plist (plist-put plist :toname (car adr)))
  6246. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6247. (let ((from (plist-get plist :from))
  6248. (to (plist-get plist :to)))
  6249. (when (and from to org-from-is-user-regexp)
  6250. (setq plist
  6251. (plist-put plist :fromto
  6252. (if (string-match org-from-is-user-regexp from)
  6253. (concat "to %t")
  6254. (concat "from %f"))))))
  6255. (setq org-store-link-plist plist))
  6256. (defun org-add-link-props (&rest plist)
  6257. "Add these properties to the link property list."
  6258. (let (key value)
  6259. (while plist
  6260. (setq key (pop plist) value (pop plist))
  6261. (setq org-store-link-plist
  6262. (plist-put org-store-link-plist key value)))))
  6263. (defun org-email-link-description (&optional fmt)
  6264. "Return the description part of an email link.
  6265. This takes information from `org-store-link-plist' and formats it
  6266. according to FMT (default from `org-email-link-description-format')."
  6267. (setq fmt (or fmt org-email-link-description-format))
  6268. (let* ((p org-store-link-plist)
  6269. (to (plist-get p :toaddress))
  6270. (from (plist-get p :fromaddress))
  6271. (table
  6272. (list
  6273. (cons "%c" (plist-get p :fromto))
  6274. (cons "%F" (plist-get p :from))
  6275. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6276. (cons "%T" (plist-get p :to))
  6277. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6278. (cons "%s" (plist-get p :subject))
  6279. (cons "%m" (plist-get p :message-id)))))
  6280. (when (string-match "%c" fmt)
  6281. ;; Check if the user wrote this message
  6282. (if (and org-from-is-user-regexp from to
  6283. (save-match-data (string-match org-from-is-user-regexp from)))
  6284. (setq fmt (replace-match "to %t" t t fmt))
  6285. (setq fmt (replace-match "from %f" t t fmt))))
  6286. (org-replace-escapes fmt table)))
  6287. (defun org-make-org-heading-search-string (&optional string heading)
  6288. "Make search string for STRING or current headline."
  6289. (interactive)
  6290. (let ((s (or string (org-get-heading))))
  6291. (unless (and string (not heading))
  6292. ;; We are using a headline, clean up garbage in there.
  6293. (if (string-match org-todo-regexp s)
  6294. (setq s (replace-match "" t t s)))
  6295. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6296. (setq s (replace-match "" t t s)))
  6297. (setq s (org-trim s))
  6298. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6299. org-comment-string "\\)") s)
  6300. (setq s (replace-match "" t t s)))
  6301. (while (string-match org-ts-regexp s)
  6302. (setq s (replace-match "" t t s))))
  6303. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6304. (setq s (replace-match " " t t s)))
  6305. (or string (setq s (concat "*" s))) ; Add * for headlines
  6306. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6307. (defun org-make-link (&rest strings)
  6308. "Concatenate STRINGS."
  6309. (apply 'concat strings))
  6310. (defun org-make-link-string (link &optional description)
  6311. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6312. (unless (string-match "\\S-" link)
  6313. (error "Empty link"))
  6314. (when (stringp description)
  6315. ;; Remove brackets from the description, they are fatal.
  6316. (while (string-match "\\[" description)
  6317. (setq description (replace-match "{" t t description)))
  6318. (while (string-match "\\]" description)
  6319. (setq description (replace-match "}" t t description))))
  6320. (when (equal (org-link-escape link) description)
  6321. ;; No description needed, it is identical
  6322. (setq description nil))
  6323. (when (and (not description)
  6324. (not (equal link (org-link-escape link))))
  6325. (setq description (org-extract-attributes link)))
  6326. (concat "[[" (org-link-escape link) "]"
  6327. (if description (concat "[" description "]") "")
  6328. "]"))
  6329. (defconst org-link-escape-chars
  6330. '((?\ . "%20")
  6331. (?\[ . "%5B")
  6332. (?\] . "%5D")
  6333. (?\340 . "%E0") ; `a
  6334. (?\342 . "%E2") ; ^a
  6335. (?\347 . "%E7") ; ,c
  6336. (?\350 . "%E8") ; `e
  6337. (?\351 . "%E9") ; 'e
  6338. (?\352 . "%EA") ; ^e
  6339. (?\356 . "%EE") ; ^i
  6340. (?\364 . "%F4") ; ^o
  6341. (?\371 . "%F9") ; `u
  6342. (?\373 . "%FB") ; ^u
  6343. (?\; . "%3B")
  6344. (?? . "%3F")
  6345. (?= . "%3D")
  6346. (?+ . "%2B")
  6347. )
  6348. "Association list of escapes for some characters problematic in links.
  6349. This is the list that is used for internal purposes.")
  6350. (defconst org-link-escape-chars-browser
  6351. '((?\ . "%20")) ; 32 for the SPC char
  6352. "Association list of escapes for some characters problematic in links.
  6353. This is the list that is used before handing over to the browser.")
  6354. (defun org-link-escape (text &optional table)
  6355. "Escape characters in TEXT that are problematic for links."
  6356. (setq table (or table org-link-escape-chars))
  6357. (when text
  6358. (let ((re (mapconcat (lambda (x) (regexp-quote
  6359. (char-to-string (car x))))
  6360. table "\\|")))
  6361. (while (string-match re text)
  6362. (setq text
  6363. (replace-match
  6364. (cdr (assoc (string-to-char (match-string 0 text))
  6365. table))
  6366. t t text)))
  6367. text)))
  6368. (defun org-link-unescape (text &optional table)
  6369. "Reverse the action of `org-link-escape'."
  6370. (setq table (or table org-link-escape-chars))
  6371. (when text
  6372. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6373. table "\\|")))
  6374. (while (string-match re text)
  6375. (setq text
  6376. (replace-match
  6377. (char-to-string (car (rassoc (match-string 0 text) table)))
  6378. t t text)))
  6379. text)))
  6380. (defun org-xor (a b)
  6381. "Exclusive or."
  6382. (if a (not b) b))
  6383. (defun org-fixup-message-id-for-http (s)
  6384. "Replace special characters in a message id, so it can be used in an http query."
  6385. (while (string-match "<" s)
  6386. (setq s (replace-match "%3C" t t s)))
  6387. (while (string-match ">" s)
  6388. (setq s (replace-match "%3E" t t s)))
  6389. (while (string-match "@" s)
  6390. (setq s (replace-match "%40" t t s)))
  6391. s)
  6392. ;;;###autoload
  6393. (defun org-insert-link-global ()
  6394. "Insert a link like Org-mode does.
  6395. This command can be called in any mode to insert a link in Org-mode syntax."
  6396. (interactive)
  6397. (org-load-modules-maybe)
  6398. (org-run-like-in-org-mode 'org-insert-link))
  6399. (defun org-insert-link (&optional complete-file link-location)
  6400. "Insert a link. At the prompt, enter the link.
  6401. Completion can be used to insert any of the link protocol prefixes like
  6402. http or ftp in use.
  6403. The history can be used to select a link previously stored with
  6404. `org-store-link'. When the empty string is entered (i.e. if you just
  6405. press RET at the prompt), the link defaults to the most recently
  6406. stored link. As SPC triggers completion in the minibuffer, you need to
  6407. use M-SPC or C-q SPC to force the insertion of a space character.
  6408. You will also be prompted for a description, and if one is given, it will
  6409. be displayed in the buffer instead of the link.
  6410. If there is already a link at point, this command will allow you to edit link
  6411. and description parts.
  6412. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6413. be selected using completion. The path to the file will be relative to the
  6414. current directory if the file is in the current directory or a subdirectory.
  6415. Otherwise, the link will be the absolute path as completed in the minibuffer
  6416. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6417. option `org-link-file-path-type'.
  6418. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6419. the current directory or below.
  6420. With three \\[universal-argument] prefixes, negate the meaning of
  6421. `org-keep-stored-link-after-insertion'.
  6422. If `org-make-link-description-function' is non-nil, this function will be
  6423. called with the link target, and the result will be the default
  6424. link description.
  6425. If the LINK-LOCATION parameter is non-nil, this value will be
  6426. used as the link location instead of reading one interactively."
  6427. (interactive "P")
  6428. (let* ((wcf (current-window-configuration))
  6429. (region (if (org-region-active-p)
  6430. (buffer-substring (region-beginning) (region-end))))
  6431. (remove (and region (list (region-beginning) (region-end))))
  6432. (desc region)
  6433. tmphist ; byte-compile incorrectly complains about this
  6434. (link link-location)
  6435. entry file)
  6436. (cond
  6437. (link-location) ; specified by arg, just use it.
  6438. ((org-in-regexp org-bracket-link-regexp 1)
  6439. ;; We do have a link at point, and we are going to edit it.
  6440. (setq remove (list (match-beginning 0) (match-end 0)))
  6441. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6442. (setq link (read-string "Link: "
  6443. (org-link-unescape
  6444. (org-match-string-no-properties 1)))))
  6445. ((or (org-in-regexp org-angle-link-re)
  6446. (org-in-regexp org-plain-link-re))
  6447. ;; Convert to bracket link
  6448. (setq remove (list (match-beginning 0) (match-end 0))
  6449. link (read-string "Link: "
  6450. (org-remove-angle-brackets (match-string 0)))))
  6451. ((member complete-file '((4) (16)))
  6452. ;; Completing read for file names.
  6453. (setq file (read-file-name "File: "))
  6454. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6455. (pwd1 (file-name-as-directory (abbreviate-file-name
  6456. (expand-file-name ".")))))
  6457. (cond
  6458. ((equal complete-file '(16))
  6459. (setq link (org-make-link
  6460. "file:"
  6461. (abbreviate-file-name (expand-file-name file)))))
  6462. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6463. (setq link (org-make-link "file:" (match-string 1 file))))
  6464. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6465. (expand-file-name file))
  6466. (setq link (org-make-link
  6467. "file:" (match-string 1 (expand-file-name file)))))
  6468. (t (setq link (org-make-link "file:" file))))))
  6469. (t
  6470. ;; Read link, with completion for stored links.
  6471. (with-output-to-temp-buffer "*Org Links*"
  6472. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6473. (when org-stored-links
  6474. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6475. (princ (mapconcat
  6476. (lambda (x)
  6477. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6478. (reverse org-stored-links) "\n"))))
  6479. (let ((cw (selected-window)))
  6480. (select-window (get-buffer-window "*Org Links*"))
  6481. (setq truncate-lines t)
  6482. (org-fit-window-to-buffer)
  6483. (select-window cw))
  6484. ;; Fake a link history, containing the stored links.
  6485. (setq tmphist (append (mapcar 'car org-stored-links)
  6486. org-insert-link-history))
  6487. (unwind-protect
  6488. (setq link
  6489. (let ((org-completion-use-ido nil))
  6490. (org-completing-read
  6491. "Link: "
  6492. (append
  6493. (mapcar (lambda (x) (list (concat (car x) ":")))
  6494. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6495. (mapcar (lambda (x) (list (concat x ":")))
  6496. org-link-types))
  6497. nil nil nil
  6498. 'tmphist
  6499. (or (car (car org-stored-links))))))
  6500. (set-window-configuration wcf)
  6501. (kill-buffer "*Org Links*"))
  6502. (setq entry (assoc link org-stored-links))
  6503. (or entry (push link org-insert-link-history))
  6504. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6505. (not org-keep-stored-link-after-insertion))
  6506. (setq org-stored-links (delq (assoc link org-stored-links)
  6507. org-stored-links)))
  6508. (setq desc (or desc (nth 1 entry)))))
  6509. (if (string-match org-plain-link-re link)
  6510. ;; URL-like link, normalize the use of angular brackets.
  6511. (setq link (org-make-link (org-remove-angle-brackets link))))
  6512. ;; Check if we are linking to the current file with a search option
  6513. ;; If yes, simplify the link by using only the search option.
  6514. (when (and buffer-file-name
  6515. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6516. (let* ((path (match-string 1 link))
  6517. (case-fold-search nil)
  6518. (search (match-string 2 link)))
  6519. (save-match-data
  6520. (if (equal (file-truename buffer-file-name) (file-truename path))
  6521. ;; We are linking to this same file, with a search option
  6522. (setq link search)))))
  6523. ;; Check if we can/should use a relative path. If yes, simplify the link
  6524. (when (string-match "^file:\\(.*\\)" link)
  6525. (let* ((path (match-string 1 link))
  6526. (origpath path)
  6527. (case-fold-search nil))
  6528. (cond
  6529. ((or (eq org-link-file-path-type 'absolute)
  6530. (equal complete-file '(16)))
  6531. (setq path (abbreviate-file-name (expand-file-name path))))
  6532. ((eq org-link-file-path-type 'noabbrev)
  6533. (setq path (expand-file-name path)))
  6534. ((eq org-link-file-path-type 'relative)
  6535. (setq path (file-relative-name path)))
  6536. (t
  6537. (save-match-data
  6538. (if (string-match (concat "^" (regexp-quote
  6539. (file-name-as-directory
  6540. (expand-file-name "."))))
  6541. (expand-file-name path))
  6542. ;; We are linking a file with relative path name.
  6543. (setq path (substring (expand-file-name path)
  6544. (match-end 0)))
  6545. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6546. (setq link (concat "file:" path))
  6547. (if (equal desc origpath)
  6548. (setq desc path))))
  6549. (if org-make-link-description-function
  6550. (setq desc (funcall org-make-link-description-function link desc)))
  6551. (setq desc (read-string "Description: " desc))
  6552. (unless (string-match "\\S-" desc) (setq desc nil))
  6553. (if remove (apply 'delete-region remove))
  6554. (insert (org-make-link-string link desc))))
  6555. (defun org-completing-read (&rest args)
  6556. "Completing-read with SPACE being a normal character."
  6557. (let ((minibuffer-local-completion-map
  6558. (copy-keymap minibuffer-local-completion-map)))
  6559. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6560. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6561. (apply 'org-ido-completing-read args)))
  6562. (defun org-completing-read-no-ido (&rest args)
  6563. (let (org-completion-use-ido)
  6564. (apply 'org-completing-read args)))
  6565. (defun org-ido-completing-read (&rest args)
  6566. "Completing-read using `ido-mode' speedups if available"
  6567. (if (and org-completion-use-ido
  6568. (fboundp 'ido-completing-read)
  6569. (boundp 'ido-mode) ido-mode
  6570. (listp (second args)))
  6571. (let ((ido-enter-matching-directory nil))
  6572. (apply 'ido-completing-read (concat (car args))
  6573. (mapcar (lambda (x) (car x)) (nth 1 args))
  6574. (cddr args)))
  6575. (apply 'completing-read args)))
  6576. (defun org-extract-attributes (s)
  6577. "Extract the attributes cookie from a string and set as text property."
  6578. (let (a attr (start 0) key value)
  6579. (save-match-data
  6580. (when (string-match "{{\\([^}]+\\)}}$" s)
  6581. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6582. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6583. (setq key (match-string 1 a) value (match-string 2 a)
  6584. start (match-end 0)
  6585. attr (plist-put attr (intern key) value))))
  6586. (org-add-props s nil 'org-attr attr))
  6587. s))
  6588. (defun org-extract-attributes-from-string (tag)
  6589. (let (key value attr)
  6590. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6591. (setq key (match-string 1 tag) value (match-string 2 tag)
  6592. tag (replace-match "" t t tag)
  6593. attr (plist-put attr (intern key) value)))
  6594. (cons tag attr)))
  6595. (defun org-attributes-to-string (plist)
  6596. "Format a property list into an HTML attribute list."
  6597. (let ((s "") key value)
  6598. (while plist
  6599. (setq key (pop plist) value (pop plist))
  6600. (and value
  6601. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6602. s))
  6603. ;;; Opening/following a link
  6604. (defvar org-link-search-failed nil)
  6605. (defun org-next-link ()
  6606. "Move forward to the next link.
  6607. If the link is in hidden text, expose it."
  6608. (interactive)
  6609. (when (and org-link-search-failed (eq this-command last-command))
  6610. (goto-char (point-min))
  6611. (message "Link search wrapped back to beginning of buffer"))
  6612. (setq org-link-search-failed nil)
  6613. (let* ((pos (point))
  6614. (ct (org-context))
  6615. (a (assoc :link ct)))
  6616. (if a (goto-char (nth 2 a)))
  6617. (if (re-search-forward org-any-link-re nil t)
  6618. (progn
  6619. (goto-char (match-beginning 0))
  6620. (if (org-invisible-p) (org-show-context)))
  6621. (goto-char pos)
  6622. (setq org-link-search-failed t)
  6623. (error "No further link found"))))
  6624. (defun org-previous-link ()
  6625. "Move backward to the previous link.
  6626. If the link is in hidden text, expose it."
  6627. (interactive)
  6628. (when (and org-link-search-failed (eq this-command last-command))
  6629. (goto-char (point-max))
  6630. (message "Link search wrapped back to end of buffer"))
  6631. (setq org-link-search-failed nil)
  6632. (let* ((pos (point))
  6633. (ct (org-context))
  6634. (a (assoc :link ct)))
  6635. (if a (goto-char (nth 1 a)))
  6636. (if (re-search-backward org-any-link-re nil t)
  6637. (progn
  6638. (goto-char (match-beginning 0))
  6639. (if (org-invisible-p) (org-show-context)))
  6640. (goto-char pos)
  6641. (setq org-link-search-failed t)
  6642. (error "No further link found"))))
  6643. (defun org-translate-link (s)
  6644. "Translate a link string if a translation function has been defined."
  6645. (if (and org-link-translation-function
  6646. (fboundp org-link-translation-function)
  6647. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6648. (progn
  6649. (setq s (funcall org-link-translation-function
  6650. (match-string 1) (match-string 2)))
  6651. (concat (car s) ":" (cdr s)))
  6652. s))
  6653. (defun org-translate-link-from-planner (type path)
  6654. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6655. This is still an experimental function, your mileage may vary."
  6656. (cond
  6657. ((member type '("http" "https" "news" "ftp"))
  6658. ;; standard Internet links are the same.
  6659. nil)
  6660. ((and (equal type "irc") (string-match "^//" path))
  6661. ;; Planner has two / at the beginning of an irc link, we have 1.
  6662. ;; We should have zero, actually....
  6663. (setq path (substring path 1)))
  6664. ((and (equal type "lisp") (string-match "^/" path))
  6665. ;; Planner has a slash, we do not.
  6666. (setq type "elisp" path (substring path 1)))
  6667. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6668. ;; A typical message link. Planner has the id after the fina slash,
  6669. ;; we separate it with a hash mark
  6670. (setq path (concat (match-string 1 path) "#"
  6671. (org-remove-angle-brackets (match-string 2 path)))))
  6672. )
  6673. (cons type path))
  6674. (defun org-find-file-at-mouse (ev)
  6675. "Open file link or URL at mouse."
  6676. (interactive "e")
  6677. (mouse-set-point ev)
  6678. (org-open-at-point 'in-emacs))
  6679. (defun org-open-at-mouse (ev)
  6680. "Open file link or URL at mouse."
  6681. (interactive "e")
  6682. (mouse-set-point ev)
  6683. (if (eq major-mode 'org-agenda-mode)
  6684. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6685. (org-open-at-point))
  6686. (defvar org-window-config-before-follow-link nil
  6687. "The window configuration before following a link.
  6688. This is saved in case the need arises to restore it.")
  6689. (defvar org-open-link-marker (make-marker)
  6690. "Marker pointing to the location where `org-open-at-point; was called.")
  6691. ;;;###autoload
  6692. (defun org-open-at-point-global ()
  6693. "Follow a link like Org-mode does.
  6694. This command can be called in any mode to follow a link that has
  6695. Org-mode syntax."
  6696. (interactive)
  6697. (org-run-like-in-org-mode 'org-open-at-point))
  6698. ;;;###autoload
  6699. (defun org-open-link-from-string (s &optional arg)
  6700. "Open a link in the string S, as if it was in Org-mode."
  6701. (interactive "sLink: \nP")
  6702. (let ((reference-buffer (current-buffer)))
  6703. (with-temp-buffer
  6704. (let ((org-inhibit-startup t))
  6705. (org-mode)
  6706. (insert s)
  6707. (goto-char (point-min))
  6708. (org-open-at-point arg reference-buffer)))))
  6709. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6710. "Open link at or after point.
  6711. If there is no link at point, this function will search forward up to
  6712. the end of the current subtree.
  6713. Normally, files will be opened by an appropriate application. If the
  6714. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6715. With a double prefix argument, try to open outside of Emacs, in the
  6716. application the system uses for this file type."
  6717. (interactive "P")
  6718. (org-load-modules-maybe)
  6719. (move-marker org-open-link-marker (point))
  6720. (setq org-window-config-before-follow-link (current-window-configuration))
  6721. (org-remove-occur-highlights nil nil t)
  6722. (cond
  6723. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6724. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6725. (org-footnote-action))
  6726. (t
  6727. (let (type path link line search (pos (point)))
  6728. (catch 'match
  6729. (save-excursion
  6730. (skip-chars-forward "^]\n\r")
  6731. (when (org-in-regexp org-bracket-link-regexp)
  6732. (setq link (org-extract-attributes
  6733. (org-link-unescape (org-match-string-no-properties 1))))
  6734. (while (string-match " *\n *" link)
  6735. (setq link (replace-match " " t t link)))
  6736. (setq link (org-link-expand-abbrev link))
  6737. (cond
  6738. ((or (file-name-absolute-p link)
  6739. (string-match "^\\.\\.?/" link))
  6740. (setq type "file" path link))
  6741. ((string-match org-link-re-with-space3 link)
  6742. (setq type (match-string 1 link) path (match-string 2 link)))
  6743. (t (setq type "thisfile" path link)))
  6744. (throw 'match t)))
  6745. (when (get-text-property (point) 'org-linked-text)
  6746. (setq type "thisfile"
  6747. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6748. (1+ (point)) (point))
  6749. path (buffer-substring
  6750. (previous-single-property-change pos 'org-linked-text)
  6751. (next-single-property-change pos 'org-linked-text)))
  6752. (throw 'match t))
  6753. (save-excursion
  6754. (when (or (org-in-regexp org-angle-link-re)
  6755. (org-in-regexp org-plain-link-re))
  6756. (setq type (match-string 1) path (match-string 2))
  6757. (throw 'match t)))
  6758. (save-excursion
  6759. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6760. (setq type "tags"
  6761. path (match-string 1))
  6762. (while (string-match ":" path)
  6763. (setq path (replace-match "+" t t path)))
  6764. (throw 'match t)))
  6765. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6766. (setq type "tree-match"
  6767. path (match-string 1))
  6768. (throw 'match t)))
  6769. (unless path
  6770. (error "No link found"))
  6771. ;; switch back to reference buffer
  6772. ;; needed when if called in a temporary buffer through
  6773. ;; org-open-link-from-string
  6774. (and reference-buffer (switch-to-buffer reference-buffer))
  6775. ;; Remove any trailing spaces in path
  6776. (if (string-match " +\\'" path)
  6777. (setq path (replace-match "" t t path)))
  6778. (if (and org-link-translation-function
  6779. (fboundp org-link-translation-function))
  6780. ;; Check if we need to translate the link
  6781. (let ((tmp (funcall org-link-translation-function type path)))
  6782. (setq type (car tmp) path (cdr tmp))))
  6783. (cond
  6784. ((assoc type org-link-protocols)
  6785. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6786. ((equal type "mailto")
  6787. (let ((cmd (car org-link-mailto-program))
  6788. (args (cdr org-link-mailto-program)) args1
  6789. (address path) (subject "") a)
  6790. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6791. (setq address (match-string 1 path)
  6792. subject (org-link-escape (match-string 2 path))))
  6793. (while args
  6794. (cond
  6795. ((not (stringp (car args))) (push (pop args) args1))
  6796. (t (setq a (pop args))
  6797. (if (string-match "%a" a)
  6798. (setq a (replace-match address t t a)))
  6799. (if (string-match "%s" a)
  6800. (setq a (replace-match subject t t a)))
  6801. (push a args1))))
  6802. (apply cmd (nreverse args1))))
  6803. ((member type '("http" "https" "ftp" "news"))
  6804. (browse-url (concat type ":" (org-link-escape
  6805. path org-link-escape-chars-browser))))
  6806. ((member type '("message"))
  6807. (browse-url (concat type ":" path)))
  6808. ((string= type "tags")
  6809. (org-tags-view in-emacs path))
  6810. ((string= type "thisfile")
  6811. (if in-emacs
  6812. (switch-to-buffer-other-window
  6813. (org-get-buffer-for-internal-link (current-buffer)))
  6814. (org-mark-ring-push))
  6815. (let ((cmd `(org-link-search
  6816. ,path
  6817. ,(cond ((equal in-emacs '(4)) 'occur)
  6818. ((equal in-emacs '(16)) 'org-occur)
  6819. (t nil))
  6820. ,pos)))
  6821. (condition-case nil (eval cmd)
  6822. (error (progn (widen) (eval cmd))))))
  6823. ((string= type "tree-match")
  6824. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6825. ((string= type "file")
  6826. (if (string-match "::\\([0-9]+\\)\\'" path)
  6827. (setq line (string-to-number (match-string 1 path))
  6828. path (substring path 0 (match-beginning 0)))
  6829. (if (string-match "::\\(.+\\)\\'" path)
  6830. (setq search (match-string 1 path)
  6831. path (substring path 0 (match-beginning 0)))))
  6832. (if (string-match "[*?{]" (file-name-nondirectory path))
  6833. (dired path)
  6834. (org-open-file path in-emacs line search)))
  6835. ((string= type "news")
  6836. (require 'org-gnus)
  6837. (org-gnus-follow-link path))
  6838. ((string= type "shell")
  6839. (let ((cmd path))
  6840. (if (or (not org-confirm-shell-link-function)
  6841. (funcall org-confirm-shell-link-function
  6842. (format "Execute \"%s\" in shell? "
  6843. (org-add-props cmd nil
  6844. 'face 'org-warning))))
  6845. (progn
  6846. (message "Executing %s" cmd)
  6847. (shell-command cmd))
  6848. (error "Abort"))))
  6849. ((string= type "elisp")
  6850. (let ((cmd path))
  6851. (if (or (not org-confirm-elisp-link-function)
  6852. (funcall org-confirm-elisp-link-function
  6853. (format "Execute \"%s\" as elisp? "
  6854. (org-add-props cmd nil
  6855. 'face 'org-warning))))
  6856. (message "%s => %s" cmd
  6857. (if (equal (string-to-char cmd) ?\()
  6858. (eval (read cmd))
  6859. (call-interactively (read cmd))))
  6860. (error "Abort"))))
  6861. (t
  6862. (browse-url-at-point))))))
  6863. (move-marker org-open-link-marker nil)
  6864. (run-hook-with-args 'org-follow-link-hook))
  6865. ;;;; Time estimates
  6866. (defun org-get-effort (&optional pom)
  6867. "Get the effort estimate for the current entry."
  6868. (org-entry-get pom org-effort-property))
  6869. ;;; File search
  6870. (defvar org-create-file-search-functions nil
  6871. "List of functions to construct the right search string for a file link.
  6872. These functions are called in turn with point at the location to
  6873. which the link should point.
  6874. A function in the hook should first test if it would like to
  6875. handle this file type, for example by checking the major-mode or
  6876. the file extension. If it decides not to handle this file, it
  6877. should just return nil to give other functions a chance. If it
  6878. does handle the file, it must return the search string to be used
  6879. when following the link. The search string will be part of the
  6880. file link, given after a double colon, and `org-open-at-point'
  6881. will automatically search for it. If special measures must be
  6882. taken to make the search successful, another function should be
  6883. added to the companion hook `org-execute-file-search-functions',
  6884. which see.
  6885. A function in this hook may also use `setq' to set the variable
  6886. `description' to provide a suggestion for the descriptive text to
  6887. be used for this link when it gets inserted into an Org-mode
  6888. buffer with \\[org-insert-link].")
  6889. (defvar org-execute-file-search-functions nil
  6890. "List of functions to execute a file search triggered by a link.
  6891. Functions added to this hook must accept a single argument, the
  6892. search string that was part of the file link, the part after the
  6893. double colon. The function must first check if it would like to
  6894. handle this search, for example by checking the major-mode or the
  6895. file extension. If it decides not to handle this search, it
  6896. should just return nil to give other functions a chance. If it
  6897. does handle the search, it must return a non-nil value to keep
  6898. other functions from trying.
  6899. Each function can access the current prefix argument through the
  6900. variable `current-prefix-argument'. Note that a single prefix is
  6901. used to force opening a link in Emacs, so it may be good to only
  6902. use a numeric or double prefix to guide the search function.
  6903. In case this is needed, a function in this hook can also restore
  6904. the window configuration before `org-open-at-point' was called using:
  6905. (set-window-configuration org-window-config-before-follow-link)")
  6906. (defun org-link-search (s &optional type avoid-pos)
  6907. "Search for a link search option.
  6908. If S is surrounded by forward slashes, it is interpreted as a
  6909. regular expression. In org-mode files, this will create an `org-occur'
  6910. sparse tree. In ordinary files, `occur' will be used to list matches.
  6911. If the current buffer is in `dired-mode', grep will be used to search
  6912. in all files. If AVOID-POS is given, ignore matches near that position."
  6913. (let ((case-fold-search t)
  6914. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6915. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6916. (append '(("") (" ") ("\t") ("\n"))
  6917. org-emphasis-alist)
  6918. "\\|") "\\)"))
  6919. (pos (point))
  6920. (pre nil) (post nil)
  6921. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6922. (cond
  6923. ;; First check if there are any special
  6924. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6925. ;; Now try the builtin stuff
  6926. ((save-excursion
  6927. (goto-char (point-min))
  6928. (and
  6929. (re-search-forward
  6930. (concat "<<" (regexp-quote s0) ">>") nil t)
  6931. (setq type 'dedicated
  6932. pos (match-beginning 0))))
  6933. ;; There is an exact target for this
  6934. (goto-char pos))
  6935. ((and (string-match "^(\\(.*\\))$" s0)
  6936. (save-excursion
  6937. (goto-char (point-min))
  6938. (and
  6939. (re-search-forward
  6940. (concat "[^[]" (regexp-quote
  6941. (format org-coderef-label-format
  6942. (match-string 1 s0))))
  6943. nil t)
  6944. (setq type 'dedicated
  6945. pos (1+ (match-beginning 0))))))
  6946. ;; There is a coderef target for this
  6947. (goto-char pos))
  6948. ((string-match "^/\\(.*\\)/$" s)
  6949. ;; A regular expression
  6950. (cond
  6951. ((org-mode-p)
  6952. (org-occur (match-string 1 s)))
  6953. ;;((eq major-mode 'dired-mode)
  6954. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6955. (t (org-do-occur (match-string 1 s)))))
  6956. (t
  6957. ;; A normal search strings
  6958. (when (equal (string-to-char s) ?*)
  6959. ;; Anchor on headlines, post may include tags.
  6960. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6961. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6962. s (substring s 1)))
  6963. (remove-text-properties
  6964. 0 (length s)
  6965. '(face nil mouse-face nil keymap nil fontified nil) s)
  6966. ;; Make a series of regular expressions to find a match
  6967. (setq words (org-split-string s "[ \n\r\t]+")
  6968. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6969. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6970. "\\)" markers)
  6971. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6972. re2a (concat "[ \t\r\n]" re2a_)
  6973. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6974. re4 (concat "[^a-zA-Z_]" re4_)
  6975. re1 (concat pre re2 post)
  6976. re3 (concat pre (if pre re4_ re4) post)
  6977. re5 (concat pre ".*" re4)
  6978. re2 (concat pre re2)
  6979. re2a (concat pre (if pre re2a_ re2a))
  6980. re4 (concat pre (if pre re4_ re4))
  6981. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6982. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6983. re5 "\\)"
  6984. ))
  6985. (cond
  6986. ((eq type 'org-occur) (org-occur reall))
  6987. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6988. (t (goto-char (point-min))
  6989. (setq type 'fuzzy)
  6990. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6991. (org-search-not-self 1 re1 nil t)
  6992. (org-search-not-self 1 re2 nil t)
  6993. (org-search-not-self 1 re2a nil t)
  6994. (org-search-not-self 1 re3 nil t)
  6995. (org-search-not-self 1 re4 nil t)
  6996. (org-search-not-self 1 re5 nil t)
  6997. )
  6998. (goto-char (match-beginning 1))
  6999. (goto-char pos)
  7000. (error "No match")))))
  7001. (t
  7002. ;; Normal string-search
  7003. (goto-char (point-min))
  7004. (if (search-forward s nil t)
  7005. (goto-char (match-beginning 0))
  7006. (error "No match"))))
  7007. (and (org-mode-p) (org-show-context 'link-search))
  7008. type))
  7009. (defun org-search-not-self (group &rest args)
  7010. "Execute `re-search-forward', but only accept matches that do not
  7011. enclose the position of `org-open-link-marker'."
  7012. (let ((m org-open-link-marker))
  7013. (catch 'exit
  7014. (while (apply 're-search-forward args)
  7015. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7016. (goto-char (match-end group))
  7017. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7018. (> (match-beginning 0) (marker-position m))
  7019. (< (match-end 0) (marker-position m)))
  7020. (save-match-data
  7021. (or (not (org-in-regexp
  7022. org-bracket-link-analytic-regexp 1))
  7023. (not (match-end 4)) ; no description
  7024. (and (<= (match-beginning 4) (point))
  7025. (>= (match-end 4) (point))))))
  7026. (throw 'exit (point))))))))
  7027. (defun org-get-buffer-for-internal-link (buffer)
  7028. "Return a buffer to be used for displaying the link target of internal links."
  7029. (cond
  7030. ((not org-display-internal-link-with-indirect-buffer)
  7031. buffer)
  7032. ((string-match "(Clone)$" (buffer-name buffer))
  7033. (message "Buffer is already a clone, not making another one")
  7034. ;; we also do not modify visibility in this case
  7035. buffer)
  7036. (t ; make a new indirect buffer for displaying the link
  7037. (let* ((bn (buffer-name buffer))
  7038. (ibn (concat bn "(Clone)"))
  7039. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7040. (with-current-buffer ib (org-overview))
  7041. ib))))
  7042. (defun org-do-occur (regexp &optional cleanup)
  7043. "Call the Emacs command `occur'.
  7044. If CLEANUP is non-nil, remove the printout of the regular expression
  7045. in the *Occur* buffer. This is useful if the regex is long and not useful
  7046. to read."
  7047. (occur regexp)
  7048. (when cleanup
  7049. (let ((cwin (selected-window)) win beg end)
  7050. (when (setq win (get-buffer-window "*Occur*"))
  7051. (select-window win))
  7052. (goto-char (point-min))
  7053. (when (re-search-forward "match[a-z]+" nil t)
  7054. (setq beg (match-end 0))
  7055. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7056. (setq end (1- (match-beginning 0)))))
  7057. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7058. (goto-char (point-min))
  7059. (select-window cwin))))
  7060. ;;; The mark ring for links jumps
  7061. (defvar org-mark-ring nil
  7062. "Mark ring for positions before jumps in Org-mode.")
  7063. (defvar org-mark-ring-last-goto nil
  7064. "Last position in the mark ring used to go back.")
  7065. ;; Fill and close the ring
  7066. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7067. (loop for i from 1 to org-mark-ring-length do
  7068. (push (make-marker) org-mark-ring))
  7069. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7070. org-mark-ring)
  7071. (defun org-mark-ring-push (&optional pos buffer)
  7072. "Put the current position or POS into the mark ring and rotate it."
  7073. (interactive)
  7074. (setq pos (or pos (point)))
  7075. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7076. (move-marker (car org-mark-ring)
  7077. (or pos (point))
  7078. (or buffer (current-buffer)))
  7079. (message "%s"
  7080. (substitute-command-keys
  7081. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7082. (defun org-mark-ring-goto (&optional n)
  7083. "Jump to the previous position in the mark ring.
  7084. With prefix arg N, jump back that many stored positions. When
  7085. called several times in succession, walk through the entire ring.
  7086. Org-mode commands jumping to a different position in the current file,
  7087. or to another Org-mode file, automatically push the old position
  7088. onto the ring."
  7089. (interactive "p")
  7090. (let (p m)
  7091. (if (eq last-command this-command)
  7092. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7093. (setq p org-mark-ring))
  7094. (setq org-mark-ring-last-goto p)
  7095. (setq m (car p))
  7096. (switch-to-buffer (marker-buffer m))
  7097. (goto-char m)
  7098. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7099. (defun org-remove-angle-brackets (s)
  7100. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7101. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7102. s)
  7103. (defun org-add-angle-brackets (s)
  7104. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7105. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7106. s)
  7107. (defun org-remove-double-quotes (s)
  7108. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7109. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7110. s)
  7111. ;;; Following specific links
  7112. (defun org-follow-timestamp-link ()
  7113. (cond
  7114. ((org-at-date-range-p t)
  7115. (let ((org-agenda-start-on-weekday)
  7116. (t1 (match-string 1))
  7117. (t2 (match-string 2)))
  7118. (setq t1 (time-to-days (org-time-string-to-time t1))
  7119. t2 (time-to-days (org-time-string-to-time t2)))
  7120. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7121. ((org-at-timestamp-p t)
  7122. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7123. (substring (match-string 1) 0 10)))
  7124. 1))
  7125. (t (error "This should not happen"))))
  7126. ;;; Following file links
  7127. (defvar org-wait nil)
  7128. (defun org-open-file (path &optional in-emacs line search)
  7129. "Open the file at PATH.
  7130. First, this expands any special file name abbreviations. Then the
  7131. configuration variable `org-file-apps' is checked if it contains an
  7132. entry for this file type, and if yes, the corresponding command is launched.
  7133. If no application is found, Emacs simply visits the file.
  7134. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7135. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7136. and o use an external application to visit the file.
  7137. Optional LINE specifies a line to go to, optional SEARCH a string to
  7138. search for. If LINE or SEARCH is given, the file will always be
  7139. opened in Emacs.
  7140. If the file does not exist, an error is thrown."
  7141. (setq in-emacs (or in-emacs line search))
  7142. (let* ((file (if (equal path "")
  7143. buffer-file-name
  7144. (substitute-in-file-name (expand-file-name path))))
  7145. (apps (append org-file-apps (org-default-apps)))
  7146. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7147. (dirp (if remp nil (file-directory-p file)))
  7148. (file (if (and dirp org-open-directory-means-index-dot-org)
  7149. (concat (file-name-as-directory file) "index.org")
  7150. file))
  7151. (a-m-a-p (assq 'auto-mode apps))
  7152. (dfile (downcase file))
  7153. (old-buffer (current-buffer))
  7154. (old-pos (point))
  7155. (old-mode major-mode)
  7156. ext cmd)
  7157. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7158. (setq ext (match-string 1 dfile))
  7159. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7160. (setq ext (match-string 1 dfile))))
  7161. (cond
  7162. ((equal in-emacs '(16))
  7163. (setq cmd (cdr (assoc 'system apps))))
  7164. (in-emacs (setq cmd 'emacs))
  7165. (t
  7166. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7167. (and dirp (cdr (assoc 'directory apps)))
  7168. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7169. 'string-match)
  7170. (cdr (assoc ext apps))
  7171. (cdr (assoc t apps))))))
  7172. (when (eq cmd 'system)
  7173. (setq cmd (cdr (assoc 'system apps))))
  7174. (when (eq cmd 'default)
  7175. (setq cmd (cdr (assoc t apps))))
  7176. (when (eq cmd 'mailcap)
  7177. (require 'mailcap)
  7178. (mailcap-parse-mailcaps)
  7179. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7180. (command (mailcap-mime-info mime-type)))
  7181. (if (stringp command)
  7182. (setq cmd command)
  7183. (setq cmd 'emacs))))
  7184. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7185. (not (file-exists-p file))
  7186. (not org-open-non-existing-files))
  7187. (error "No such file: %s" file))
  7188. (cond
  7189. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7190. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7191. (while (string-match "['\"]%s['\"]" cmd)
  7192. (setq cmd (replace-match "%s" t t cmd)))
  7193. (while (string-match "%s" cmd)
  7194. (setq cmd (replace-match
  7195. (save-match-data
  7196. (shell-quote-argument
  7197. (convert-standard-filename file)))
  7198. t t cmd)))
  7199. (save-window-excursion
  7200. (start-process-shell-command cmd nil cmd)
  7201. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7202. ))
  7203. ((or (stringp cmd)
  7204. (eq cmd 'emacs))
  7205. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7206. (widen)
  7207. (if line (goto-line line)
  7208. (if search (org-link-search search))))
  7209. ((consp cmd)
  7210. (let ((file (convert-standard-filename file)))
  7211. (eval cmd)))
  7212. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7213. (and (org-mode-p) (eq old-mode 'org-mode)
  7214. (or (not (equal old-buffer (current-buffer)))
  7215. (not (equal old-pos (point))))
  7216. (org-mark-ring-push old-pos old-buffer))))
  7217. (defun org-default-apps ()
  7218. "Return the default applications for this operating system."
  7219. (cond
  7220. ((eq system-type 'darwin)
  7221. org-file-apps-defaults-macosx)
  7222. ((eq system-type 'windows-nt)
  7223. org-file-apps-defaults-windowsnt)
  7224. (t org-file-apps-defaults-gnu)))
  7225. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7226. "Convert extensions to regular expressions in the cars of LIST.
  7227. Also, weed out any non-string entries, because the return value is used
  7228. only for regexp matching.
  7229. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7230. point to the symbol `emacs', indicating that the file should
  7231. be opened in Emacs."
  7232. (append
  7233. (delq nil
  7234. (mapcar (lambda (x)
  7235. (if (not (stringp (car x)))
  7236. nil
  7237. (if (string-match "\\W" (car x))
  7238. x
  7239. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7240. list))
  7241. (if add-auto-mode
  7242. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7243. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7244. (defun org-file-remote-p (file)
  7245. "Test whether FILE specifies a location on a remote system.
  7246. Return non-nil if the location is indeed remote.
  7247. For example, the filename \"/user@host:/foo\" specifies a location
  7248. on the system \"/user@host:\"."
  7249. (cond ((fboundp 'file-remote-p)
  7250. (file-remote-p file))
  7251. ((fboundp 'tramp-handle-file-remote-p)
  7252. (tramp-handle-file-remote-p file))
  7253. ((and (boundp 'ange-ftp-name-format)
  7254. (string-match (car ange-ftp-name-format) file))
  7255. t)
  7256. (t nil)))
  7257. ;;;; Refiling
  7258. (defun org-get-org-file ()
  7259. "Read a filename, with default directory `org-directory'."
  7260. (let ((default (or org-default-notes-file remember-data-file)))
  7261. (read-file-name (format "File name [%s]: " default)
  7262. (file-name-as-directory org-directory)
  7263. default)))
  7264. (defun org-notes-order-reversed-p ()
  7265. "Check if the current file should receive notes in reversed order."
  7266. (cond
  7267. ((not org-reverse-note-order) nil)
  7268. ((eq t org-reverse-note-order) t)
  7269. ((not (listp org-reverse-note-order)) nil)
  7270. (t (catch 'exit
  7271. (let ((all org-reverse-note-order)
  7272. entry)
  7273. (while (setq entry (pop all))
  7274. (if (string-match (car entry) buffer-file-name)
  7275. (throw 'exit (cdr entry))))
  7276. nil)))))
  7277. (defvar org-refile-target-table nil
  7278. "The list of refile targets, created by `org-refile'.")
  7279. (defvar org-agenda-new-buffers nil
  7280. "Buffers created to visit agenda files.")
  7281. (defun org-get-refile-targets (&optional default-buffer)
  7282. "Produce a table with refile targets."
  7283. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7284. targets txt re files f desc descre fast-path-p level)
  7285. (message "Getting targets...")
  7286. (with-current-buffer (or default-buffer (current-buffer))
  7287. (while (setq entry (pop entries))
  7288. (setq files (car entry) desc (cdr entry))
  7289. (setq fast-path-p nil)
  7290. (cond
  7291. ((null files) (setq files (list (current-buffer))))
  7292. ((eq files 'org-agenda-files)
  7293. (setq files (org-agenda-files 'unrestricted)))
  7294. ((and (symbolp files) (fboundp files))
  7295. (setq files (funcall files)))
  7296. ((and (symbolp files) (boundp files))
  7297. (setq files (symbol-value files))))
  7298. (if (stringp files) (setq files (list files)))
  7299. (cond
  7300. ((eq (car desc) :tag)
  7301. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7302. ((eq (car desc) :todo)
  7303. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7304. ((eq (car desc) :regexp)
  7305. (setq descre (cdr desc)))
  7306. ((eq (car desc) :level)
  7307. (setq descre (concat "^\\*\\{" (number-to-string
  7308. (if org-odd-levels-only
  7309. (1- (* 2 (cdr desc)))
  7310. (cdr desc)))
  7311. "\\}[ \t]")))
  7312. ((eq (car desc) :maxlevel)
  7313. (setq fast-path-p t)
  7314. (setq descre (concat "^\\*\\{1," (number-to-string
  7315. (if org-odd-levels-only
  7316. (1- (* 2 (cdr desc)))
  7317. (cdr desc)))
  7318. "\\}[ \t]")))
  7319. (t (error "Bad refiling target description %s" desc)))
  7320. (while (setq f (pop files))
  7321. (save-excursion
  7322. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7323. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7324. (setq f (expand-file-name f))
  7325. (if (eq org-refile-use-outline-path 'file)
  7326. (push (list (file-name-nondirectory f) f nil nil) targets))
  7327. (save-excursion
  7328. (save-restriction
  7329. (widen)
  7330. (goto-char (point-min))
  7331. (while (re-search-forward descre nil t)
  7332. (goto-char (point-at-bol))
  7333. (when (looking-at org-complex-heading-regexp)
  7334. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7335. txt (org-link-display-format (match-string 4))
  7336. re (concat "^" (regexp-quote
  7337. (buffer-substring (match-beginning 1)
  7338. (match-end 4)))))
  7339. (if (match-end 5) (setq re (concat re "[ \t]+"
  7340. (regexp-quote
  7341. (match-string 5)))))
  7342. (setq re (concat re "[ \t]*$"))
  7343. (when org-refile-use-outline-path
  7344. (setq txt (mapconcat 'org-protect-slash
  7345. (append
  7346. (if (eq org-refile-use-outline-path 'file)
  7347. (list (file-name-nondirectory
  7348. (buffer-file-name (buffer-base-buffer))))
  7349. (if (eq org-refile-use-outline-path 'full-file-path)
  7350. (list (buffer-file-name (buffer-base-buffer)))))
  7351. (org-get-outline-path fast-path-p level txt)
  7352. (list txt))
  7353. "/")))
  7354. (push (list txt f re (point)) targets))
  7355. (goto-char (point-at-eol))))))))
  7356. (message "Getting targets...done")
  7357. (nreverse targets))))
  7358. (defun org-protect-slash (s)
  7359. (while (string-match "/" s)
  7360. (setq s (replace-match "\\" t t s)))
  7361. s)
  7362. (defvar org-olpa (make-vector 20 nil))
  7363. (defun org-get-outline-path (&optional fastp level heading)
  7364. "Return the outline path to the current entry, as a list."
  7365. (if fastp
  7366. (progn
  7367. (if (> level 19)
  7368. (error "Outline path failure, more than 19 levels."))
  7369. (loop for i from level upto 19 do
  7370. (aset org-olpa i nil))
  7371. (prog1
  7372. (delq nil (append org-olpa nil))
  7373. (aset org-olpa level heading)))
  7374. (let (rtn)
  7375. (save-excursion
  7376. (while (org-up-heading-safe)
  7377. (when (looking-at org-complex-heading-regexp)
  7378. (push (org-match-string-no-properties 4) rtn)))
  7379. rtn))))
  7380. (defvar org-refile-history nil
  7381. "History for refiling operations.")
  7382. (defun org-refile (&optional goto default-buffer)
  7383. "Move the entry at point to another heading.
  7384. The list of target headings is compiled using the information in
  7385. `org-refile-targets', which see. This list is created before each use
  7386. and will therefore always be up-to-date.
  7387. At the target location, the entry is filed as a subitem of the target heading.
  7388. Depending on `org-reverse-note-order', the new subitem will either be the
  7389. first or the last subitem.
  7390. If there is an active region, all entries in that region will be moved.
  7391. However, the region must fulfil the requirement that the first heading
  7392. is the first one sets the top-level of the moved text - at most siblings
  7393. below it are allowed.
  7394. With prefix arg GOTO, the command will only visit the target location,
  7395. not actually move anything.
  7396. With a double prefix `C-u C-u', go to the location where the last refiling
  7397. operation has put the subtree."
  7398. (interactive "P")
  7399. (let* ((cbuf (current-buffer))
  7400. (regionp (org-region-active-p))
  7401. (region-start (and regionp (region-beginning)))
  7402. (region-end (and regionp (region-end)))
  7403. (region-length (and regionp (- region-end region-start)))
  7404. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7405. pos it nbuf file re level reversed)
  7406. (when regionp (goto-char region-start)
  7407. (unless (org-kill-is-subtree-p
  7408. (buffer-substring region-start region-end))
  7409. (error "The region is not a (sequence of) subtree(s)")))
  7410. (if (equal goto '(16))
  7411. (org-refile-goto-last-stored)
  7412. (when (setq it (org-refile-get-location
  7413. (if goto "Goto: " "Refile to: ") default-buffer))
  7414. (setq file (nth 1 it)
  7415. re (nth 2 it)
  7416. pos (nth 3 it))
  7417. (if (and (not goto)
  7418. pos
  7419. (equal (buffer-file-name) file)
  7420. (if regionp
  7421. (and (>= pos region-start)
  7422. (<= pos region-end))
  7423. (and (>= pos (point))
  7424. (< pos (save-excursion
  7425. (org-end-of-subtree t t))))))
  7426. (error "Cannot refile to position inside the tree or region"))
  7427. (setq nbuf (or (find-buffer-visiting file)
  7428. (find-file-noselect file)))
  7429. (if goto
  7430. (progn
  7431. (switch-to-buffer nbuf)
  7432. (goto-char pos)
  7433. (org-show-context 'org-goto))
  7434. (if regionp
  7435. (progn
  7436. (kill-new (buffer-substring region-start region-end))
  7437. (org-save-markers-in-region region-start region-end))
  7438. (org-copy-subtree 1 nil t))
  7439. (save-excursion
  7440. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7441. (find-file-noselect file))))
  7442. (setq reversed (org-notes-order-reversed-p))
  7443. (save-excursion
  7444. (save-restriction
  7445. (widen)
  7446. (if pos
  7447. (progn
  7448. (goto-char pos)
  7449. (looking-at outline-regexp)
  7450. (setq level (org-get-valid-level (funcall outline-level) 1))
  7451. (goto-char
  7452. (if reversed
  7453. (or (outline-next-heading) (point-max))
  7454. (or (save-excursion (outline-get-next-sibling))
  7455. (org-end-of-subtree t t)
  7456. (point-max)))))
  7457. (setq level 1)
  7458. (if (not reversed)
  7459. (goto-char (point-max))
  7460. (goto-char (point-min))
  7461. (or (outline-next-heading) (goto-char (point-max)))))
  7462. (if (not (bolp)) (newline))
  7463. (bookmark-set "org-refile-last-stored")
  7464. (org-paste-subtree level))))
  7465. (if regionp
  7466. (delete-region (point) (+ (point) region-length))
  7467. (org-cut-subtree))
  7468. (setq org-markers-to-move nil)
  7469. (message "Refiled to \"%s\"" (car it)))))))
  7470. (defun org-refile-goto-last-stored ()
  7471. "Go to the location where the last refile was stored."
  7472. (interactive)
  7473. (bookmark-jump "org-refile-last-stored")
  7474. (message "This is the location of the last refile"))
  7475. (defun org-refile-get-location (&optional prompt default-buffer)
  7476. "Prompt the user for a refile location, using PROMPT."
  7477. (let ((org-refile-targets org-refile-targets)
  7478. (org-refile-use-outline-path org-refile-use-outline-path))
  7479. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7480. (unless org-refile-target-table
  7481. (error "No refile targets"))
  7482. (let* ((cbuf (current-buffer))
  7483. (partial-completion-mode nil)
  7484. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7485. (cfunc (if (and org-refile-use-outline-path
  7486. org-outline-path-complete-in-steps)
  7487. 'org-olpath-completing-read
  7488. 'org-ido-completing-read))
  7489. (extra (if org-refile-use-outline-path "/" ""))
  7490. (filename (and cfn (expand-file-name cfn)))
  7491. (tbl (mapcar
  7492. (lambda (x)
  7493. (if (and (not (member org-refile-use-outline-path
  7494. '(file full-file-path)))
  7495. (not (equal filename (nth 1 x))))
  7496. (cons (concat (car x) extra " ("
  7497. (file-name-nondirectory (nth 1 x)) ")")
  7498. (cdr x))
  7499. (cons (concat (car x) extra) (cdr x))))
  7500. org-refile-target-table))
  7501. (completion-ignore-case t))
  7502. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7503. tbl)))
  7504. (defun org-olpath-completing-read (prompt collection &rest args)
  7505. "Read an outline path like a file name."
  7506. (let ((thetable collection)
  7507. (org-completion-use-ido nil)) ; does not work with ido.
  7508. (apply
  7509. 'org-ido-completing-read prompt
  7510. (lambda (string predicate &optional flag)
  7511. (let (rtn r f (l (length string)))
  7512. (cond
  7513. ((eq flag nil)
  7514. ;; try completion
  7515. (try-completion string thetable))
  7516. ((eq flag t)
  7517. ;; all-completions
  7518. (setq rtn (all-completions string thetable predicate))
  7519. (mapcar
  7520. (lambda (x)
  7521. (setq r (substring x l))
  7522. (if (string-match " ([^)]*)$" x)
  7523. (setq f (match-string 0 x))
  7524. (setq f ""))
  7525. (if (string-match "/" r)
  7526. (concat string (substring r 0 (match-end 0)) f)
  7527. x))
  7528. rtn))
  7529. ((eq flag 'lambda)
  7530. ;; exact match?
  7531. (assoc string thetable)))
  7532. ))
  7533. args)))
  7534. ;;;; Dynamic blocks
  7535. (defun org-find-dblock (name)
  7536. "Find the first dynamic block with name NAME in the buffer.
  7537. If not found, stay at current position and return nil."
  7538. (let (pos)
  7539. (save-excursion
  7540. (goto-char (point-min))
  7541. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7542. nil t)
  7543. (match-beginning 0))))
  7544. (if pos (goto-char pos))
  7545. pos))
  7546. (defconst org-dblock-start-re
  7547. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7548. "Matches the startline of a dynamic block, with parameters.")
  7549. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7550. "Matches the end of a dynamic block.")
  7551. (defun org-create-dblock (plist)
  7552. "Create a dynamic block section, with parameters taken from PLIST.
  7553. PLIST must contain a :name entry which is used as name of the block."
  7554. (unless (bolp) (newline))
  7555. (let ((name (plist-get plist :name)))
  7556. (insert "#+BEGIN: " name)
  7557. (while plist
  7558. (if (eq (car plist) :name)
  7559. (setq plist (cddr plist))
  7560. (insert " " (prin1-to-string (pop plist)))))
  7561. (insert "\n\n#+END:\n")
  7562. (beginning-of-line -2)))
  7563. (defun org-prepare-dblock ()
  7564. "Prepare dynamic block for refresh.
  7565. This empties the block, puts the cursor at the insert position and returns
  7566. the property list including an extra property :name with the block name."
  7567. (unless (looking-at org-dblock-start-re)
  7568. (error "Not at a dynamic block"))
  7569. (let* ((begdel (1+ (match-end 0)))
  7570. (name (org-no-properties (match-string 1)))
  7571. (params (append (list :name name)
  7572. (read (concat "(" (match-string 3) ")")))))
  7573. (unless (re-search-forward org-dblock-end-re nil t)
  7574. (error "Dynamic block not terminated"))
  7575. (setq params
  7576. (append params
  7577. (list :content (buffer-substring
  7578. begdel (match-beginning 0)))))
  7579. (delete-region begdel (match-beginning 0))
  7580. (goto-char begdel)
  7581. (open-line 1)
  7582. params))
  7583. (defun org-map-dblocks (&optional command)
  7584. "Apply COMMAND to all dynamic blocks in the current buffer.
  7585. If COMMAND is not given, use `org-update-dblock'."
  7586. (let ((cmd (or command 'org-update-dblock))
  7587. pos)
  7588. (save-excursion
  7589. (goto-char (point-min))
  7590. (while (re-search-forward org-dblock-start-re nil t)
  7591. (goto-char (setq pos (match-beginning 0)))
  7592. (condition-case nil
  7593. (funcall cmd)
  7594. (error (message "Error during update of dynamic block")))
  7595. (goto-char pos)
  7596. (unless (re-search-forward org-dblock-end-re nil t)
  7597. (error "Dynamic block not terminated"))))))
  7598. (defun org-dblock-update (&optional arg)
  7599. "User command for updating dynamic blocks.
  7600. Update the dynamic block at point. With prefix ARG, update all dynamic
  7601. blocks in the buffer."
  7602. (interactive "P")
  7603. (if arg
  7604. (org-update-all-dblocks)
  7605. (or (looking-at org-dblock-start-re)
  7606. (org-beginning-of-dblock))
  7607. (org-update-dblock)))
  7608. (defun org-update-dblock ()
  7609. "Update the dynamic block at point
  7610. This means to empty the block, parse for parameters and then call
  7611. the correct writing function."
  7612. (save-window-excursion
  7613. (let* ((pos (point))
  7614. (line (org-current-line))
  7615. (params (org-prepare-dblock))
  7616. (name (plist-get params :name))
  7617. (cmd (intern (concat "org-dblock-write:" name))))
  7618. (message "Updating dynamic block `%s' at line %d..." name line)
  7619. (funcall cmd params)
  7620. (message "Updating dynamic block `%s' at line %d...done" name line)
  7621. (goto-char pos))))
  7622. (defun org-beginning-of-dblock ()
  7623. "Find the beginning of the dynamic block at point.
  7624. Error if there is no such block at point."
  7625. (let ((pos (point))
  7626. beg)
  7627. (end-of-line 1)
  7628. (if (and (re-search-backward org-dblock-start-re nil t)
  7629. (setq beg (match-beginning 0))
  7630. (re-search-forward org-dblock-end-re nil t)
  7631. (> (match-end 0) pos))
  7632. (goto-char beg)
  7633. (goto-char pos)
  7634. (error "Not in a dynamic block"))))
  7635. (defun org-update-all-dblocks ()
  7636. "Update all dynamic blocks in the buffer.
  7637. This function can be used in a hook."
  7638. (when (org-mode-p)
  7639. (org-map-dblocks 'org-update-dblock)))
  7640. ;;;; Completion
  7641. (defconst org-additional-option-like-keywords
  7642. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7643. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7644. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7645. "BEGIN_QUOTE" "END_QUOTE"
  7646. "BEGIN_VERSE" "END_VERSE"
  7647. "BEGIN_CENTER" "END_CENTER"
  7648. "BEGIN_SRC" "END_SRC"
  7649. "CATEGORY" "COLUMNS"
  7650. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7651. (defcustom org-structure-template-alist
  7652. '(
  7653. ("s" "#+begin_src ?\n\n#+end_src"
  7654. "<src lang=\"?\">\n\n</src>")
  7655. ("e" "#+begin_example\n?\n#+end_example"
  7656. "<example>\n?\n</example>")
  7657. ("q" "#+begin_quote\n?\n#+end_quote"
  7658. "<quote>\n?\n</quote>")
  7659. ("v" "#+begin_verse\n?\n#+end_verse"
  7660. "<verse>\n?\n/verse>")
  7661. ("c" "#+begin_center\n?\n#+end_center"
  7662. "<center>\n?\n/center>")
  7663. ("l" "#+begin_latex\n?\n#+end_latex"
  7664. "<literal style=\"latex\">\n?\n</literal>")
  7665. ("L" "#+latex: "
  7666. "<literal style=\"latex\">?</literal>")
  7667. ("h" "#+begin_html\n?\n#+end_html"
  7668. "<literal style=\"html\">\n?\n</literal>")
  7669. ("H" "#+html: "
  7670. "<literal style=\"html\">?</literal>")
  7671. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7672. ("A" "#+ascii: ")
  7673. ("i" "#+include %file ?"
  7674. "<include file=%file markup=\"?\">")
  7675. )
  7676. "Structure completion elements.
  7677. This is a list of abbreviation keys and values. The value gets inserted
  7678. it you type @samp{.} followed by the key and then the completion key,
  7679. usually `M-TAB'. %file will be replaced by a file name after prompting
  7680. for the file using completion.
  7681. There are two templates for each key, the first uses the original Org syntax,
  7682. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7683. the default when the /org-mtags.el/ module has been loaded. See also the
  7684. variable `org-mtags-prefer-muse-templates'.
  7685. This is an experimental feature, it is undecided if it is going to stay in."
  7686. :group 'org-completion
  7687. :type '(repeat
  7688. (string :tag "Key")
  7689. (string :tag "Template")
  7690. (string :tag "Muse Template")))
  7691. (defun org-try-structure-completion ()
  7692. "Try to complete a structure template before point.
  7693. This looks for strings like \"<e\" on an otherwise empty line and
  7694. expands them."
  7695. (let ((l (buffer-substring (point-at-bol) (point)))
  7696. a)
  7697. (when (and (looking-at "[ \t]*$")
  7698. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7699. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7700. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7701. (match-beginning 1)) a)
  7702. t)))
  7703. (defun org-complete-expand-structure-template (start cell)
  7704. "Expand a structure template."
  7705. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7706. (rpl (nth (if musep 2 1) cell)))
  7707. (delete-region start (point))
  7708. (when (string-match "\\`#\\+" rpl)
  7709. (cond
  7710. ((bolp))
  7711. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7712. (delete-region (point-at-bol) (point)))
  7713. (t (newline))))
  7714. (setq start (point))
  7715. (if (string-match "%file" rpl)
  7716. (setq rpl (replace-match
  7717. (concat
  7718. "\""
  7719. (save-match-data
  7720. (abbreviate-file-name (read-file-name "Include file: ")))
  7721. "\"")
  7722. t t rpl)))
  7723. (insert rpl)
  7724. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7725. (defun org-complete (&optional arg)
  7726. "Perform completion on word at point.
  7727. At the beginning of a headline, this completes TODO keywords as given in
  7728. `org-todo-keywords'.
  7729. If the current word is preceded by a backslash, completes the TeX symbols
  7730. that are supported for HTML support.
  7731. If the current word is preceded by \"#+\", completes special words for
  7732. setting file options.
  7733. In the line after \"#+STARTUP:, complete valid keywords.\"
  7734. At all other locations, this simply calls the value of
  7735. `org-completion-fallback-command'."
  7736. (interactive "P")
  7737. (org-without-partial-completion
  7738. (catch 'exit
  7739. (let* ((a nil)
  7740. (end (point))
  7741. (beg1 (save-excursion
  7742. (skip-chars-backward (org-re "[:alnum:]_@"))
  7743. (point)))
  7744. (beg (save-excursion
  7745. (skip-chars-backward "a-zA-Z0-9_:$")
  7746. (point)))
  7747. (confirm (lambda (x) (stringp (car x))))
  7748. (searchhead (equal (char-before beg) ?*))
  7749. (struct
  7750. (when (and (member (char-before beg1) '(?. ?<))
  7751. (setq a (assoc (buffer-substring beg1 (point))
  7752. org-structure-template-alist)))
  7753. (org-complete-expand-structure-template (1- beg1) a)
  7754. (throw 'exit t)))
  7755. (tag (and (equal (char-before beg1) ?:)
  7756. (equal (char-after (point-at-bol)) ?*)))
  7757. (prop (and (equal (char-before beg1) ?:)
  7758. (not (equal (char-after (point-at-bol)) ?*))))
  7759. (texp (equal (char-before beg) ?\\))
  7760. (link (equal (char-before beg) ?\[))
  7761. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7762. beg)
  7763. "#+"))
  7764. (startup (string-match "^#\\+STARTUP:.*"
  7765. (buffer-substring (point-at-bol) (point))))
  7766. (completion-ignore-case opt)
  7767. (type nil)
  7768. (tbl nil)
  7769. (table (cond
  7770. (opt
  7771. (setq type :opt)
  7772. (require 'org-exp)
  7773. (append
  7774. (mapcar
  7775. (lambda (x)
  7776. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7777. (cons (match-string 2 x) (match-string 1 x)))
  7778. (org-split-string (org-get-current-options) "\n"))
  7779. (mapcar 'list org-additional-option-like-keywords)))
  7780. (startup
  7781. (setq type :startup)
  7782. org-startup-options)
  7783. (link (append org-link-abbrev-alist-local
  7784. org-link-abbrev-alist))
  7785. (texp
  7786. (setq type :tex)
  7787. org-html-entities)
  7788. ((string-match "\\`\\*+[ \t]+\\'"
  7789. (buffer-substring (point-at-bol) beg))
  7790. (setq type :todo)
  7791. (mapcar 'list org-todo-keywords-1))
  7792. (searchhead
  7793. (setq type :searchhead)
  7794. (save-excursion
  7795. (goto-char (point-min))
  7796. (while (re-search-forward org-todo-line-regexp nil t)
  7797. (push (list
  7798. (org-make-org-heading-search-string
  7799. (match-string 3) t))
  7800. tbl)))
  7801. tbl)
  7802. (tag (setq type :tag beg beg1)
  7803. (or org-tag-alist (org-get-buffer-tags)))
  7804. (prop (setq type :prop beg beg1)
  7805. (mapcar 'list (org-buffer-property-keys nil t t)))
  7806. (t (progn
  7807. (call-interactively org-completion-fallback-command)
  7808. (throw 'exit nil)))))
  7809. (pattern (buffer-substring-no-properties beg end))
  7810. (completion (try-completion pattern table confirm)))
  7811. (cond ((eq completion t)
  7812. (if (not (assoc (upcase pattern) table))
  7813. (message "Already complete")
  7814. (if (and (equal type :opt)
  7815. (not (member (car (assoc (upcase pattern) table))
  7816. org-additional-option-like-keywords)))
  7817. (insert (substring (cdr (assoc (upcase pattern) table))
  7818. (length pattern)))
  7819. (if (memq type '(:tag :prop)) (insert ":")))))
  7820. ((null completion)
  7821. (message "Can't find completion for \"%s\"" pattern)
  7822. (ding))
  7823. ((not (string= pattern completion))
  7824. (delete-region beg end)
  7825. (if (string-match " +$" completion)
  7826. (setq completion (replace-match "" t t completion)))
  7827. (insert completion)
  7828. (if (get-buffer-window "*Completions*")
  7829. (delete-window (get-buffer-window "*Completions*")))
  7830. (if (assoc completion table)
  7831. (if (eq type :todo) (insert " ")
  7832. (if (memq type '(:tag :prop)) (insert ":"))))
  7833. (if (and (equal type :opt) (assoc completion table))
  7834. (message "%s" (substitute-command-keys
  7835. "Press \\[org-complete] again to insert example settings"))))
  7836. (t
  7837. (message "Making completion list...")
  7838. (let ((list (sort (all-completions pattern table confirm)
  7839. 'string<)))
  7840. (with-output-to-temp-buffer "*Completions*"
  7841. (condition-case nil
  7842. ;; Protection needed for XEmacs and emacs 21
  7843. (display-completion-list list pattern)
  7844. (error (display-completion-list list)))))
  7845. (message "Making completion list...%s" "done")))))))
  7846. ;;;; TODO, DEADLINE, Comments
  7847. (defun org-toggle-comment ()
  7848. "Change the COMMENT state of an entry."
  7849. (interactive)
  7850. (save-excursion
  7851. (org-back-to-heading)
  7852. (let (case-fold-search)
  7853. (if (looking-at (concat outline-regexp
  7854. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7855. (replace-match "" t t nil 1)
  7856. (if (looking-at outline-regexp)
  7857. (progn
  7858. (goto-char (match-end 0))
  7859. (insert org-comment-string " ")))))))
  7860. (defvar org-last-todo-state-is-todo nil
  7861. "This is non-nil when the last TODO state change led to a TODO state.
  7862. If the last change removed the TODO tag or switched to DONE, then
  7863. this is nil.")
  7864. (defvar org-setting-tags nil) ; dynamically skipped
  7865. (defun org-parse-local-options (string var)
  7866. "Parse STRING for startup setting relevant for variable VAR."
  7867. (let ((rtn (symbol-value var))
  7868. e opts)
  7869. (save-match-data
  7870. (if (or (not string) (not (string-match "\\S-" string)))
  7871. rtn
  7872. (setq opts (delq nil (mapcar (lambda (x)
  7873. (setq e (assoc x org-startup-options))
  7874. (if (eq (nth 1 e) var) e nil))
  7875. (org-split-string string "[ \t]+"))))
  7876. (if (not opts)
  7877. rtn
  7878. (setq rtn nil)
  7879. (while (setq e (pop opts))
  7880. (if (not (nth 3 e))
  7881. (setq rtn (nth 2 e))
  7882. (if (not (listp rtn)) (setq rtn nil))
  7883. (push (nth 2 e) rtn)))
  7884. rtn)))))
  7885. (defvar org-todo-setup-filter-hook nil
  7886. "Hook for functions that pre-filter todo specs.
  7887. Each function takes a todo spec and returns either `nil' or the spec
  7888. transformed into canonical form." )
  7889. (defvar org-todo-get-default-hook nil
  7890. "Hook for functions that get a default item for todo.
  7891. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7892. `nil' or a string to be used for the todo mark." )
  7893. (defvar org-agenda-headline-snapshot-before-repeat)
  7894. (defun org-todo (&optional arg)
  7895. "Change the TODO state of an item.
  7896. The state of an item is given by a keyword at the start of the heading,
  7897. like
  7898. *** TODO Write paper
  7899. *** DONE Call mom
  7900. The different keywords are specified in the variable `org-todo-keywords'.
  7901. By default the available states are \"TODO\" and \"DONE\".
  7902. So for this example: when the item starts with TODO, it is changed to DONE.
  7903. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7904. DONE are present, add TODO at the beginning of the heading.
  7905. With C-u prefix arg, use completion to determine the new state.
  7906. With numeric prefix arg, switch to that state.
  7907. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7908. With a tripple C-u prefix, circumvent any state blocking.
  7909. For calling through lisp, arg is also interpreted in the following way:
  7910. 'none -> empty state
  7911. \"\"(empty string) -> switch to empty state
  7912. 'done -> switch to DONE
  7913. 'nextset -> switch to the next set of keywords
  7914. 'previousset -> switch to the previous set of keywords
  7915. \"WAITING\" -> switch to the specified keyword, but only if it
  7916. really is a member of `org-todo-keywords'."
  7917. (interactive "P")
  7918. (if (equal arg '(16)) (setq arg 'nextset))
  7919. (let ((org-blocker-hook org-blocker-hook)
  7920. (case-fold-search nil))
  7921. (when (equal arg '(64))
  7922. (setq arg nil org-blocker-hook nil))
  7923. (save-excursion
  7924. (catch 'exit
  7925. (org-back-to-heading)
  7926. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7927. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  7928. (looking-at " *"))
  7929. (let* ((match-data (match-data))
  7930. (startpos (point-at-bol))
  7931. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7932. (org-log-done org-log-done)
  7933. (org-log-repeat org-log-repeat)
  7934. (org-todo-log-states org-todo-log-states)
  7935. (this (match-string 1))
  7936. (hl-pos (match-beginning 0))
  7937. (head (org-get-todo-sequence-head this))
  7938. (ass (assoc head org-todo-kwd-alist))
  7939. (interpret (nth 1 ass))
  7940. (done-word (nth 3 ass))
  7941. (final-done-word (nth 4 ass))
  7942. (last-state (or this ""))
  7943. (completion-ignore-case t)
  7944. (member (member this org-todo-keywords-1))
  7945. (tail (cdr member))
  7946. (state (cond
  7947. ((and org-todo-key-trigger
  7948. (or (and (equal arg '(4))
  7949. (eq org-use-fast-todo-selection 'prefix))
  7950. (and (not arg) org-use-fast-todo-selection
  7951. (not (eq org-use-fast-todo-selection
  7952. 'prefix)))))
  7953. ;; Use fast selection
  7954. (org-fast-todo-selection))
  7955. ((and (equal arg '(4))
  7956. (or (not org-use-fast-todo-selection)
  7957. (not org-todo-key-trigger)))
  7958. ;; Read a state with completion
  7959. (org-ido-completing-read
  7960. "State: " (mapcar (lambda(x) (list x))
  7961. org-todo-keywords-1)
  7962. nil t))
  7963. ((eq arg 'right)
  7964. (if this
  7965. (if tail (car tail) nil)
  7966. (car org-todo-keywords-1)))
  7967. ((eq arg 'left)
  7968. (if (equal member org-todo-keywords-1)
  7969. nil
  7970. (if this
  7971. (nth (- (length org-todo-keywords-1)
  7972. (length tail) 2)
  7973. org-todo-keywords-1)
  7974. (org-last org-todo-keywords-1))))
  7975. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7976. (setq arg nil))) ; hack to fall back to cycling
  7977. (arg
  7978. ;; user or caller requests a specific state
  7979. (cond
  7980. ((equal arg "") nil)
  7981. ((eq arg 'none) nil)
  7982. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7983. ((eq arg 'nextset)
  7984. (or (car (cdr (member head org-todo-heads)))
  7985. (car org-todo-heads)))
  7986. ((eq arg 'previousset)
  7987. (let ((org-todo-heads (reverse org-todo-heads)))
  7988. (or (car (cdr (member head org-todo-heads)))
  7989. (car org-todo-heads))))
  7990. ((car (member arg org-todo-keywords-1)))
  7991. ((nth (1- (prefix-numeric-value arg))
  7992. org-todo-keywords-1))))
  7993. ((null member) (or head (car org-todo-keywords-1)))
  7994. ((equal this final-done-word) nil) ;; -> make empty
  7995. ((null tail) nil) ;; -> first entry
  7996. ((memq interpret '(type priority))
  7997. (if (eq this-command last-command)
  7998. (car tail)
  7999. (if (> (length tail) 0)
  8000. (or done-word (car org-done-keywords))
  8001. nil)))
  8002. (t
  8003. (car tail))))
  8004. (state (or
  8005. (run-hook-with-args-until-success
  8006. 'org-todo-get-default-hook state last-state)
  8007. state))
  8008. (next (if state (concat " " state " ") " "))
  8009. (change-plist (list :type 'todo-state-change :from this :to state
  8010. :position startpos))
  8011. dolog now-done-p)
  8012. (when org-blocker-hook
  8013. (setq org-last-todo-state-is-todo
  8014. (not (member this org-done-keywords)))
  8015. (unless (save-excursion
  8016. (save-match-data
  8017. (run-hook-with-args-until-failure
  8018. 'org-blocker-hook change-plist)))
  8019. (if (interactive-p)
  8020. (error "TODO state change from %s to %s blocked" this state)
  8021. ;; fail silently
  8022. (message "TODO state change from %s to %s blocked" this state)
  8023. (throw 'exit nil))))
  8024. (store-match-data match-data)
  8025. (replace-match next t t)
  8026. (unless (pos-visible-in-window-p hl-pos)
  8027. (message "TODO state changed to %s" (org-trim next)))
  8028. (unless head
  8029. (setq head (org-get-todo-sequence-head state)
  8030. ass (assoc head org-todo-kwd-alist)
  8031. interpret (nth 1 ass)
  8032. done-word (nth 3 ass)
  8033. final-done-word (nth 4 ass)))
  8034. (when (memq arg '(nextset previousset))
  8035. (message "Keyword-Set %d/%d: %s"
  8036. (- (length org-todo-sets) -1
  8037. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8038. (length org-todo-sets)
  8039. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8040. (setq org-last-todo-state-is-todo
  8041. (not (member state org-done-keywords)))
  8042. (setq now-done-p (and (member state org-done-keywords)
  8043. (not (member this org-done-keywords))))
  8044. (and logging (org-local-logging logging))
  8045. (when (and (or org-todo-log-states org-log-done)
  8046. (not (memq arg '(nextset previousset))))
  8047. ;; we need to look at recording a time and note
  8048. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8049. (nth 2 (assoc this org-todo-log-states))))
  8050. (when (and state
  8051. (member state org-not-done-keywords)
  8052. (not (member this org-not-done-keywords)))
  8053. ;; This is now a todo state and was not one before
  8054. ;; If there was a CLOSED time stamp, get rid of it.
  8055. (org-add-planning-info nil nil 'closed))
  8056. (when (and now-done-p org-log-done)
  8057. ;; It is now done, and it was not done before
  8058. (org-add-planning-info 'closed (org-current-time))
  8059. (if (and (not dolog) (eq 'note org-log-done))
  8060. (org-add-log-setup 'done state this 'findpos 'note)))
  8061. (when (and state dolog)
  8062. ;; This is a non-nil state, and we need to log it
  8063. (org-add-log-setup 'state state this 'findpos dolog)))
  8064. ;; Fixup tag positioning
  8065. (org-todo-trigger-tag-changes state)
  8066. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8067. (when org-provide-todo-statistics
  8068. (org-update-parent-todo-statistics))
  8069. (run-hooks 'org-after-todo-state-change-hook)
  8070. (if (and arg (not (member state org-done-keywords)))
  8071. (setq head (org-get-todo-sequence-head state)))
  8072. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8073. ;; Do we need to trigger a repeat?
  8074. (when now-done-p
  8075. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8076. ;; This is for the agenda, take a snapshot of the headline.
  8077. (save-match-data
  8078. (setq org-agenda-headline-snapshot-before-repeat
  8079. (org-get-heading))))
  8080. (org-auto-repeat-maybe state))
  8081. ;; Fixup cursor location if close to the keyword
  8082. (if (and (outline-on-heading-p)
  8083. (not (bolp))
  8084. (save-excursion (beginning-of-line 1)
  8085. (looking-at org-todo-line-regexp))
  8086. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8087. (progn
  8088. (goto-char (or (match-end 2) (match-end 1)))
  8089. (and (looking-at " ") (just-one-space))))
  8090. (when org-trigger-hook
  8091. (save-excursion
  8092. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8093. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8094. "Block turning an entry into a TODO, using the hierarchy.
  8095. This checks whether the current task should be blocked from state
  8096. changes. Such blocking occurs when:
  8097. 1. The task has children which are not all in a completed state.
  8098. 2. A task has a parent with the property :ORDERED:, and there
  8099. are siblings prior to the current task with incomplete
  8100. status.
  8101. 3. The parent of the task is blocked because it has siblings that should
  8102. be done first, or is child of a block grandparent TODO entry."
  8103. (catch 'dont-block
  8104. ;; If this is not a todo state change, or if this entry is already DONE,
  8105. ;; do not block
  8106. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8107. (member (plist-get change-plist :from)
  8108. (cons 'done org-done-keywords))
  8109. (member (plist-get change-plist :to)
  8110. (cons 'todo org-not-done-keywords)))
  8111. (throw 'dont-block t))
  8112. ;; If this task has children, and any are undone, it's blocked
  8113. (save-excursion
  8114. (org-back-to-heading t)
  8115. (let ((this-level (funcall outline-level)))
  8116. (outline-next-heading)
  8117. (let ((child-level (funcall outline-level)))
  8118. (while (and (not (eobp))
  8119. (> child-level this-level))
  8120. ;; this todo has children, check whether they are all
  8121. ;; completed
  8122. (if (and (not (org-entry-is-done-p))
  8123. (org-entry-is-todo-p))
  8124. (throw 'dont-block nil))
  8125. (outline-next-heading)
  8126. (setq child-level (funcall outline-level))))))
  8127. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8128. ;; any previous siblings are undone, it's blocked
  8129. (save-excursion
  8130. (org-back-to-heading t)
  8131. (let* ((pos (point))
  8132. (parent-pos (and (org-up-heading-safe) (point))))
  8133. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8134. (when (and (org-entry-get (point) "ORDERED")
  8135. (forward-line 1)
  8136. (re-search-forward org-not-done-heading-regexp pos t))
  8137. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8138. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8139. (while t
  8140. (goto-char parent-pos)
  8141. (if (not (looking-at org-not-done-heading-regexp))
  8142. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8143. (setq pos (point))
  8144. (setq parent-pos (and (org-up-heading-safe) (point)))
  8145. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8146. (when (and (org-entry-get (point) "ORDERED")
  8147. (forward-line 1)
  8148. (re-search-forward org-not-done-heading-regexp pos t))
  8149. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8150. (defcustom org-track-ordered-property-with-tag nil
  8151. "Should the ORDERED property also be shown as a tag?
  8152. The ORDERED property decides if an entry should require subtasks to be
  8153. completed in sequence. Since a property is not very visible, setting
  8154. this option means that toggling the ORDERED property with the command
  8155. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8156. not relevant for the behavior, but it makes things more visible.
  8157. Note that toggling the tag with tags commands will not change the property
  8158. and therefore not influence behavior!
  8159. This can be t, meaning the tag ORDERED should be used, It can also be a
  8160. string to select a different tag for this task."
  8161. :group 'org-todo
  8162. :type '(choice
  8163. (const :tag "No tracking" nil)
  8164. (const :tag "Track with ORDERED tag" t)
  8165. (string :tag "Use other tag")))
  8166. (defun org-toggle-ordered-property ()
  8167. "Toggle the ORDERED property of the current entry.
  8168. For better visibility, you can track the value of this property with a tag.
  8169. See variable `org-track-ordered-property-with-tag'."
  8170. (interactive)
  8171. (let* ((t1 org-track-ordered-property-with-tag)
  8172. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8173. (save-excursion
  8174. (org-back-to-heading)
  8175. (if (org-entry-get nil "ORDERED")
  8176. (progn
  8177. (org-delete-property "ORDERED")
  8178. (and tag (org-toggle-tag tag 'off))
  8179. (message "Subtasks can be completed in arbitrary order"))
  8180. (org-entry-put nil "ORDERED" "t")
  8181. (and tag (org-toggle-tag tag 'on))
  8182. (message "Subtasks must be completed in sequence")))))
  8183. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8184. (defun org-block-todo-from-checkboxes (change-plist)
  8185. "Block turning an entry into a TODO, using checkboxes.
  8186. This checks whether the current task should be blocked from state
  8187. changes because there are uncheckd boxes in this entry."
  8188. (catch 'dont-block
  8189. ;; If this is not a todo state change, or if this entry is already DONE,
  8190. ;; do not block
  8191. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8192. (member (plist-get change-plist :from)
  8193. (cons 'done org-done-keywords))
  8194. (member (plist-get change-plist :to)
  8195. (cons 'todo org-not-done-keywords)))
  8196. (throw 'dont-block t))
  8197. ;; If this task has checkboxes that are not checked, it's blocked
  8198. (save-excursion
  8199. (org-back-to-heading t)
  8200. (let ((beg (point)) end)
  8201. (outline-next-heading)
  8202. (setq end (point))
  8203. (goto-char beg)
  8204. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8205. end t)
  8206. (progn
  8207. (if (boundp 'org-blocked-by-checkboxes)
  8208. (setq org-blocked-by-checkboxes t))
  8209. (throw 'dont-block nil)))))
  8210. t)) ; do not block
  8211. (defun org-update-parent-todo-statistics ()
  8212. "Update any statistics cookie in the parent of the current headline."
  8213. (interactive)
  8214. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8215. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8216. (catch 'exit
  8217. (save-excursion
  8218. (setq level (org-up-heading-safe))
  8219. (unless level
  8220. (throw 'exit nil))
  8221. (while (re-search-forward box-re (point-at-eol) t)
  8222. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8223. (setq is-percent (match-end 2))
  8224. (save-match-data
  8225. (unless (outline-next-heading) (throw 'exit nil))
  8226. (while (looking-at org-todo-line-regexp)
  8227. (setq kwd (match-string 2))
  8228. (and kwd (setq cnt-all (1+ cnt-all)))
  8229. (and (member kwd org-done-keywords)
  8230. (setq cnt-done (1+ cnt-done)))
  8231. (condition-case nil
  8232. (org-forward-same-level 1)
  8233. (error (end-of-line 1)))))
  8234. (replace-match
  8235. (if is-percent
  8236. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8237. (format "[%d/%d]" cnt-done cnt-all))))
  8238. (when cookie-present
  8239. (run-hook-with-args 'org-after-todo-statistics-hook
  8240. cnt-done (- cnt-all cnt-done)))))))
  8241. (defvar org-after-todo-statistics-hook nil
  8242. "Hook that is called after a TODO statistics cookie has been updated.
  8243. Each function is called with two arguments: the number of not-done entries
  8244. and the number of done entries.
  8245. For example, the following function, when added to this hook, will switch
  8246. an entry to DONE when all children are done, and back to TODO when new
  8247. entries are set to a TODO status. Note that this hook is only called
  8248. when there is a statistics cookie in the headline!
  8249. (defun org-summary-todo (n-done n-not-done)
  8250. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8251. (let (org-log-done org-log-states) ; turn off logging
  8252. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8253. ")
  8254. (defun org-todo-trigger-tag-changes (state)
  8255. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8256. (let ((l org-todo-state-tags-triggers)
  8257. changes)
  8258. (when (or (not state) (equal state ""))
  8259. (setq changes (append changes (cdr (assoc "" l)))))
  8260. (when (and (stringp state) (> (length state) 0))
  8261. (setq changes (append changes (cdr (assoc state l)))))
  8262. (when (member state org-not-done-keywords)
  8263. (setq changes (append changes (cdr (assoc 'todo l)))))
  8264. (when (member state org-done-keywords)
  8265. (setq changes (append changes (cdr (assoc 'done l)))))
  8266. (dolist (c changes)
  8267. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8268. (defun org-local-logging (value)
  8269. "Get logging settings from a property VALUE."
  8270. (let* (words w a)
  8271. ;; directly set the variables, they are already local.
  8272. (setq org-log-done nil
  8273. org-log-repeat nil
  8274. org-todo-log-states nil)
  8275. (setq words (org-split-string value))
  8276. (while (setq w (pop words))
  8277. (cond
  8278. ((setq a (assoc w org-startup-options))
  8279. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8280. (set (nth 1 a) (nth 2 a))))
  8281. ((setq a (org-extract-log-state-settings w))
  8282. (and (member (car a) org-todo-keywords-1)
  8283. (push a org-todo-log-states)))))))
  8284. (defun org-get-todo-sequence-head (kwd)
  8285. "Return the head of the TODO sequence to which KWD belongs.
  8286. If KWD is not set, check if there is a text property remembering the
  8287. right sequence."
  8288. (let (p)
  8289. (cond
  8290. ((not kwd)
  8291. (or (get-text-property (point-at-bol) 'org-todo-head)
  8292. (progn
  8293. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8294. nil (point-at-eol)))
  8295. (get-text-property p 'org-todo-head))))
  8296. ((not (member kwd org-todo-keywords-1))
  8297. (car org-todo-keywords-1))
  8298. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8299. (defun org-fast-todo-selection ()
  8300. "Fast TODO keyword selection with single keys.
  8301. Returns the new TODO keyword, or nil if no state change should occur."
  8302. (let* ((fulltable org-todo-key-alist)
  8303. (done-keywords org-done-keywords) ;; needed for the faces.
  8304. (maxlen (apply 'max (mapcar
  8305. (lambda (x)
  8306. (if (stringp (car x)) (string-width (car x)) 0))
  8307. fulltable)))
  8308. (expert nil)
  8309. (fwidth (+ maxlen 3 1 3))
  8310. (ncol (/ (- (window-width) 4) fwidth))
  8311. tg cnt e c tbl
  8312. groups ingroup)
  8313. (save-excursion
  8314. (save-window-excursion
  8315. (if expert
  8316. (set-buffer (get-buffer-create " *Org todo*"))
  8317. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8318. (erase-buffer)
  8319. (org-set-local 'org-done-keywords done-keywords)
  8320. (setq tbl fulltable cnt 0)
  8321. (while (setq e (pop tbl))
  8322. (cond
  8323. ((equal e '(:startgroup))
  8324. (push '() groups) (setq ingroup t)
  8325. (when (not (= cnt 0))
  8326. (setq cnt 0)
  8327. (insert "\n"))
  8328. (insert "{ "))
  8329. ((equal e '(:endgroup))
  8330. (setq ingroup nil cnt 0)
  8331. (insert "}\n"))
  8332. ((equal e '(:newline))
  8333. (when (not (= cnt 0))
  8334. (setq cnt 0)
  8335. (insert "\n")
  8336. (setq e (car tbl))
  8337. (while (equal (car tbl) '(:newline))
  8338. (insert "\n")
  8339. (setq tbl (cdr tbl)))))
  8340. (t
  8341. (setq tg (car e) c (cdr e))
  8342. (if ingroup (push tg (car groups)))
  8343. (setq tg (org-add-props tg nil 'face
  8344. (org-get-todo-face tg)))
  8345. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8346. (insert "[" c "] " tg (make-string
  8347. (- fwidth 4 (length tg)) ?\ ))
  8348. (when (= (setq cnt (1+ cnt)) ncol)
  8349. (insert "\n")
  8350. (if ingroup (insert " "))
  8351. (setq cnt 0)))))
  8352. (insert "\n")
  8353. (goto-char (point-min))
  8354. (if (not expert) (org-fit-window-to-buffer))
  8355. (message "[a-z..]:Set [SPC]:clear")
  8356. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8357. (cond
  8358. ((or (= c ?\C-g)
  8359. (and (= c ?q) (not (rassoc c fulltable))))
  8360. (setq quit-flag t))
  8361. ((= c ?\ ) nil)
  8362. ((setq e (rassoc c fulltable) tg (car e))
  8363. tg)
  8364. (t (setq quit-flag t)))))))
  8365. (defun org-entry-is-todo-p ()
  8366. (member (org-get-todo-state) org-not-done-keywords))
  8367. (defun org-entry-is-done-p ()
  8368. (member (org-get-todo-state) org-done-keywords))
  8369. (defun org-get-todo-state ()
  8370. (save-excursion
  8371. (org-back-to-heading t)
  8372. (and (looking-at org-todo-line-regexp)
  8373. (match-end 2)
  8374. (match-string 2))))
  8375. (defun org-at-date-range-p (&optional inactive-ok)
  8376. "Is the cursor inside a date range?"
  8377. (interactive)
  8378. (save-excursion
  8379. (catch 'exit
  8380. (let ((pos (point)))
  8381. (skip-chars-backward "^[<\r\n")
  8382. (skip-chars-backward "<[")
  8383. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8384. (>= (match-end 0) pos)
  8385. (throw 'exit t))
  8386. (skip-chars-backward "^<[\r\n")
  8387. (skip-chars-backward "<[")
  8388. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8389. (>= (match-end 0) pos)
  8390. (throw 'exit t)))
  8391. nil)))
  8392. (defun org-get-repeat ()
  8393. "Check if there is a deadline/schedule with repeater in this entry."
  8394. (save-match-data
  8395. (save-excursion
  8396. (org-back-to-heading t)
  8397. (if (re-search-forward
  8398. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8399. (match-string 1)))))
  8400. (defvar org-last-changed-timestamp)
  8401. (defvar org-last-inserted-timestamp)
  8402. (defvar org-log-post-message)
  8403. (defvar org-log-note-purpose)
  8404. (defvar org-log-note-how)
  8405. (defvar org-log-note-extra)
  8406. (defun org-auto-repeat-maybe (done-word)
  8407. "Check if the current headline contains a repeated deadline/schedule.
  8408. If yes, set TODO state back to what it was and change the base date
  8409. of repeating deadline/scheduled time stamps to new date.
  8410. This function is run automatically after each state change to a DONE state."
  8411. ;; last-state is dynamically scoped into this function
  8412. (let* ((repeat (org-get-repeat))
  8413. (aa (assoc last-state org-todo-kwd-alist))
  8414. (interpret (nth 1 aa))
  8415. (head (nth 2 aa))
  8416. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8417. (msg "Entry repeats: ")
  8418. (org-log-done nil)
  8419. (org-todo-log-states nil)
  8420. (nshiftmax 10) (nshift 0)
  8421. re type n what ts time)
  8422. (when repeat
  8423. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8424. (org-todo (if (eq interpret 'type) last-state head))
  8425. (when org-log-repeat
  8426. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8427. (memq 'org-add-log-note post-command-hook))
  8428. ;; OK, we are already setup for some record
  8429. (if (eq org-log-repeat 'note)
  8430. ;; make sure we take a note, not only a time stamp
  8431. (setq org-log-note-how 'note))
  8432. ;; Set up for taking a record
  8433. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8434. last-state
  8435. 'findpos org-log-repeat)))
  8436. (org-back-to-heading t)
  8437. (org-add-planning-info nil nil 'closed)
  8438. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8439. org-deadline-time-regexp "\\)\\|\\("
  8440. org-ts-regexp "\\)"))
  8441. (while (re-search-forward
  8442. re (save-excursion (outline-next-heading) (point)) t)
  8443. (setq type (if (match-end 1) org-scheduled-string
  8444. (if (match-end 3) org-deadline-string "Plain:"))
  8445. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8446. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8447. (setq n (string-to-number (match-string 2 ts))
  8448. what (match-string 3 ts))
  8449. (if (equal what "w") (setq n (* n 7) what "d"))
  8450. ;; Preparation, see if we need to modify the start date for the change
  8451. (when (match-end 1)
  8452. (setq time (save-match-data (org-time-string-to-time ts)))
  8453. (cond
  8454. ((equal (match-string 1 ts) ".")
  8455. ;; Shift starting date to today
  8456. (org-timestamp-change
  8457. (- (time-to-days (current-time)) (time-to-days time))
  8458. 'day))
  8459. ((equal (match-string 1 ts) "+")
  8460. (while (or (= nshift 0)
  8461. (<= (time-to-days time) (time-to-days (current-time))))
  8462. (when (= (incf nshift) nshiftmax)
  8463. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8464. (error "Abort")))
  8465. (org-timestamp-change n (cdr (assoc what whata)))
  8466. (org-at-timestamp-p t)
  8467. (setq ts (match-string 1))
  8468. (setq time (save-match-data (org-time-string-to-time ts))))
  8469. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8470. ;; rematch, so that we have everything in place for the real shift
  8471. (org-at-timestamp-p t)
  8472. (setq ts (match-string 1))
  8473. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8474. (org-timestamp-change n (cdr (assoc what whata)))
  8475. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8476. (setq org-log-post-message msg)
  8477. (message "%s" msg))))
  8478. (defun org-show-todo-tree (arg)
  8479. "Make a compact tree which shows all headlines marked with TODO.
  8480. The tree will show the lines where the regexp matches, and all higher
  8481. headlines above the match.
  8482. With a \\[universal-argument] prefix, also show the DONE entries.
  8483. With a numeric prefix N, construct a sparse tree for the Nth element
  8484. of `org-todo-keywords-1'."
  8485. (interactive "P")
  8486. (let ((case-fold-search nil)
  8487. (kwd-re
  8488. (cond ((null arg) org-not-done-regexp)
  8489. ((equal arg '(4))
  8490. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8491. (mapcar 'list org-todo-keywords-1))))
  8492. (concat "\\("
  8493. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8494. "\\)\\>")))
  8495. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8496. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8497. org-todo-keywords-1)))
  8498. (t (error "Invalid prefix argument: %s" arg)))))
  8499. (message "%d TODO entries found"
  8500. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8501. (defun org-deadline (&optional remove time)
  8502. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8503. With argument REMOVE, remove any deadline from the item.
  8504. When TIME is set, it should be an internal time specification, and the
  8505. scheduling will use the corresponding date."
  8506. (interactive "P")
  8507. (if remove
  8508. (progn
  8509. (org-remove-timestamp-with-keyword org-deadline-string)
  8510. (message "Item no longer has a deadline."))
  8511. (if (org-get-repeat)
  8512. (error "Cannot change deadline on task with repeater, please do that by hand")
  8513. (org-add-planning-info 'deadline time 'closed)
  8514. (message "Deadline on %s" org-last-inserted-timestamp))))
  8515. (defun org-schedule (&optional remove time)
  8516. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8517. With argument REMOVE, remove any scheduling date from the item.
  8518. When TIME is set, it should be an internal time specification, and the
  8519. scheduling will use the corresponding date."
  8520. (interactive "P")
  8521. (if remove
  8522. (progn
  8523. (org-remove-timestamp-with-keyword org-scheduled-string)
  8524. (message "Item is no longer scheduled."))
  8525. (if (org-get-repeat)
  8526. (error "Cannot reschedule task with repeater, please do that by hand")
  8527. (org-add-planning-info 'scheduled time 'closed)
  8528. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8529. (defun org-get-scheduled-time (pom &optional inherit)
  8530. "Get the scheduled time as a time tuple, of a format suitable
  8531. for calling org-schedule with, or if there is no scheduling,
  8532. returns nil."
  8533. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8534. (when time
  8535. (apply 'encode-time (org-parse-time-string time)))))
  8536. (defun org-get-deadline-time (pom &optional inherit)
  8537. "Get the deadine as a time tuple, of a format suitable for
  8538. calling org-deadlin with, or if there is no scheduling, returns
  8539. nil."
  8540. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8541. (when time
  8542. (apply 'encode-time (org-parse-time-string time)))))
  8543. (defun org-remove-timestamp-with-keyword (keyword)
  8544. "Remove all time stamps with KEYWORD in the current entry."
  8545. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8546. beg)
  8547. (save-excursion
  8548. (org-back-to-heading t)
  8549. (setq beg (point))
  8550. (org-end-of-subtree t t)
  8551. (while (re-search-backward re beg t)
  8552. (replace-match "")
  8553. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8554. (equal (char-before) ?\ ))
  8555. (backward-delete-char 1)
  8556. (if (string-match "^[ \t]*$" (buffer-substring
  8557. (point-at-bol) (point-at-eol)))
  8558. (delete-region (point-at-bol)
  8559. (min (point-max) (1+ (point-at-eol))))))))))
  8560. (defun org-add-planning-info (what &optional time &rest remove)
  8561. "Insert new timestamp with keyword in the line directly after the headline.
  8562. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8563. If non is given, the user is prompted for a date.
  8564. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8565. be removed."
  8566. (interactive)
  8567. (let (org-time-was-given org-end-time-was-given ts
  8568. end default-time default-input)
  8569. (catch 'exit
  8570. (when (and (not time) (memq what '(scheduled deadline)))
  8571. ;; Try to get a default date/time from existing timestamp
  8572. (save-excursion
  8573. (org-back-to-heading t)
  8574. (setq end (save-excursion (outline-next-heading) (point)))
  8575. (when (re-search-forward (if (eq what 'scheduled)
  8576. org-scheduled-time-regexp
  8577. org-deadline-time-regexp)
  8578. end t)
  8579. (setq ts (match-string 1)
  8580. default-time
  8581. (apply 'encode-time (org-parse-time-string ts))
  8582. default-input (and ts (org-get-compact-tod ts))))))
  8583. (when what
  8584. ;; If necessary, get the time from the user
  8585. (setq time (or time (org-read-date nil 'to-time nil nil
  8586. default-time default-input))))
  8587. (when (and org-insert-labeled-timestamps-at-point
  8588. (member what '(scheduled deadline)))
  8589. (insert
  8590. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8591. (org-insert-time-stamp time org-time-was-given
  8592. nil nil nil (list org-end-time-was-given))
  8593. (setq what nil))
  8594. (save-excursion
  8595. (save-restriction
  8596. (let (col list elt ts buffer-invisibility-spec)
  8597. (org-back-to-heading t)
  8598. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8599. (goto-char (match-end 1))
  8600. (setq col (current-column))
  8601. (goto-char (match-end 0))
  8602. (if (eobp) (insert "\n") (forward-char 1))
  8603. (when (and (not what)
  8604. (not (looking-at
  8605. (concat "[ \t]*"
  8606. org-keyword-time-not-clock-regexp))))
  8607. ;; Nothing to add, nothing to remove...... :-)
  8608. (throw 'exit nil))
  8609. (if (and (not (looking-at outline-regexp))
  8610. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8611. "[^\r\n]*"))
  8612. (not (equal (match-string 1) org-clock-string)))
  8613. (narrow-to-region (match-beginning 0) (match-end 0))
  8614. (insert-before-markers "\n")
  8615. (backward-char 1)
  8616. (narrow-to-region (point) (point))
  8617. (and org-adapt-indentation (org-indent-to-column col)))
  8618. ;; Check if we have to remove something.
  8619. (setq list (cons what remove))
  8620. (while list
  8621. (setq elt (pop list))
  8622. (goto-char (point-min))
  8623. (when (or (and (eq elt 'scheduled)
  8624. (re-search-forward org-scheduled-time-regexp nil t))
  8625. (and (eq elt 'deadline)
  8626. (re-search-forward org-deadline-time-regexp nil t))
  8627. (and (eq elt 'closed)
  8628. (re-search-forward org-closed-time-regexp nil t)))
  8629. (replace-match "")
  8630. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8631. (if (looking-at " +") (replace-match ""))))
  8632. (goto-char (point-max))
  8633. (when what
  8634. (insert
  8635. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8636. (cond ((eq what 'scheduled) org-scheduled-string)
  8637. ((eq what 'deadline) org-deadline-string)
  8638. ((eq what 'closed) org-closed-string))
  8639. " ")
  8640. (setq ts (org-insert-time-stamp
  8641. time
  8642. (or org-time-was-given
  8643. (and (eq what 'closed) org-log-done-with-time))
  8644. (eq what 'closed)
  8645. nil nil (list org-end-time-was-given)))
  8646. (end-of-line 1))
  8647. (goto-char (point-min))
  8648. (widen)
  8649. (if (and (looking-at "[ \t]+\n")
  8650. (equal (char-before) ?\n))
  8651. (delete-region (1- (point)) (point-at-eol)))
  8652. ts))))))
  8653. (defvar org-log-note-marker (make-marker))
  8654. (defvar org-log-note-purpose nil)
  8655. (defvar org-log-note-state nil)
  8656. (defvar org-log-note-previous-state nil)
  8657. (defvar org-log-note-how nil)
  8658. (defvar org-log-note-extra nil)
  8659. (defvar org-log-note-window-configuration nil)
  8660. (defvar org-log-note-return-to (make-marker))
  8661. (defvar org-log-post-message nil
  8662. "Message to be displayed after a log note has been stored.
  8663. The auto-repeater uses this.")
  8664. (defun org-add-note ()
  8665. "Add a note to the current entry.
  8666. This is done in the same way as adding a state change note."
  8667. (interactive)
  8668. (org-add-log-setup 'note nil nil 'findpos nil))
  8669. (defvar org-property-end-re)
  8670. (defun org-add-log-setup (&optional purpose state prev-state
  8671. findpos how &optional extra)
  8672. "Set up the post command hook to take a note.
  8673. If this is about to TODO state change, the new state is expected in STATE.
  8674. When FINDPOS is non-nil, find the correct position for the note in
  8675. the current entry. If not, assume that it can be inserted at point.
  8676. HOW is an indicator what kind of note should be created.
  8677. EXTRA is additional text that will be inserted into the notes buffer."
  8678. (let ((drawer (cond ((stringp org-log-into-drawer)
  8679. org-log-into-drawer)
  8680. (org-log-into-drawer "LOGBOOK")
  8681. (t nil))))
  8682. (save-restriction
  8683. (save-excursion
  8684. (when findpos
  8685. (org-back-to-heading t)
  8686. (narrow-to-region (point) (save-excursion
  8687. (outline-next-heading) (point)))
  8688. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8689. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8690. "[^\r\n]*\\)?"))
  8691. (goto-char (match-end 0))
  8692. (cond
  8693. (drawer
  8694. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8695. nil t)
  8696. (progn
  8697. (goto-char (match-end 0))
  8698. (or org-log-states-order-reversed
  8699. (and (re-search-forward org-property-end-re nil t)
  8700. (goto-char (1- (match-beginning 0))))))
  8701. (insert "\n:" drawer ":\n:END:")
  8702. (beginning-of-line 0)
  8703. (org-indent-line-function)
  8704. (beginning-of-line 2)
  8705. (org-indent-line-function)
  8706. (end-of-line 0)))
  8707. ((and org-log-state-notes-insert-after-drawers
  8708. (save-excursion
  8709. (forward-line) (looking-at org-drawer-regexp)))
  8710. (forward-line)
  8711. (while (looking-at org-drawer-regexp)
  8712. (goto-char (match-end 0))
  8713. (re-search-forward org-property-end-re (point-max) t)
  8714. (forward-line))
  8715. (forward-line -1)))
  8716. (unless org-log-states-order-reversed
  8717. (and (= (char-after) ?\n) (forward-char 1))
  8718. (org-skip-over-state-notes)
  8719. (skip-chars-backward " \t\n\r")))
  8720. (move-marker org-log-note-marker (point))
  8721. (setq org-log-note-purpose purpose
  8722. org-log-note-state state
  8723. org-log-note-previous-state prev-state
  8724. org-log-note-how how
  8725. org-log-note-extra extra)
  8726. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8727. (defun org-skip-over-state-notes ()
  8728. "Skip past the list of State notes in an entry."
  8729. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8730. (while (looking-at "[ \t]*- State")
  8731. (condition-case nil
  8732. (org-next-item)
  8733. (error (org-end-of-item)))))
  8734. (defun org-add-log-note (&optional purpose)
  8735. "Pop up a window for taking a note, and add this note later at point."
  8736. (remove-hook 'post-command-hook 'org-add-log-note)
  8737. (setq org-log-note-window-configuration (current-window-configuration))
  8738. (delete-other-windows)
  8739. (move-marker org-log-note-return-to (point))
  8740. (switch-to-buffer (marker-buffer org-log-note-marker))
  8741. (goto-char org-log-note-marker)
  8742. (org-switch-to-buffer-other-window "*Org Note*")
  8743. (erase-buffer)
  8744. (if (memq org-log-note-how '(time state))
  8745. (let (current-prefix-arg) (org-store-log-note))
  8746. (let ((org-inhibit-startup t)) (org-mode))
  8747. (insert (format "# Insert note for %s.
  8748. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8749. (cond
  8750. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8751. ((eq org-log-note-purpose 'done) "closed todo item")
  8752. ((eq org-log-note-purpose 'state)
  8753. (format "state change from \"%s\" to \"%s\""
  8754. (or org-log-note-previous-state "")
  8755. (or org-log-note-state "")))
  8756. ((eq org-log-note-purpose 'note)
  8757. "this entry")
  8758. (t (error "This should not happen")))))
  8759. (if org-log-note-extra (insert org-log-note-extra))
  8760. (org-set-local 'org-finish-function 'org-store-log-note)))
  8761. (defvar org-note-abort nil) ; dynamically scoped
  8762. (defun org-store-log-note ()
  8763. "Finish taking a log note, and insert it to where it belongs."
  8764. (let ((txt (buffer-string))
  8765. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8766. lines ind)
  8767. (kill-buffer (current-buffer))
  8768. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8769. (setq txt (replace-match "" t t txt)))
  8770. (if (string-match "\\s-+\\'" txt)
  8771. (setq txt (replace-match "" t t txt)))
  8772. (setq lines (org-split-string txt "\n"))
  8773. (when (and note (string-match "\\S-" note))
  8774. (setq note
  8775. (org-replace-escapes
  8776. note
  8777. (list (cons "%u" (user-login-name))
  8778. (cons "%U" user-full-name)
  8779. (cons "%t" (format-time-string
  8780. (org-time-stamp-format 'long 'inactive)
  8781. (current-time)))
  8782. (cons "%s" (if org-log-note-state
  8783. (concat "\"" org-log-note-state "\"")
  8784. ""))
  8785. (cons "%S" (if org-log-note-previous-state
  8786. (concat "\"" org-log-note-previous-state "\"")
  8787. "\"\"")))))
  8788. (if lines (setq note (concat note " \\\\")))
  8789. (push note lines))
  8790. (when (or current-prefix-arg org-note-abort)
  8791. (when org-log-into-drawer
  8792. (org-remove-empty-drawer-at
  8793. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8794. org-log-note-marker))
  8795. (setq lines nil))
  8796. (when lines
  8797. (save-excursion
  8798. (set-buffer (marker-buffer org-log-note-marker))
  8799. (save-excursion
  8800. (goto-char org-log-note-marker)
  8801. (move-marker org-log-note-marker nil)
  8802. (end-of-line 1)
  8803. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8804. (insert "- " (pop lines))
  8805. (org-indent-line-function)
  8806. (beginning-of-line 1)
  8807. (looking-at "[ \t]*")
  8808. (setq ind (concat (match-string 0) " "))
  8809. (end-of-line 1)
  8810. (while lines (insert "\n" ind (pop lines)))
  8811. (message "Note stored")
  8812. (org-back-to-heading t)
  8813. (org-cycle-hide-drawers 'children)))))
  8814. (set-window-configuration org-log-note-window-configuration)
  8815. (with-current-buffer (marker-buffer org-log-note-return-to)
  8816. (goto-char org-log-note-return-to))
  8817. (move-marker org-log-note-return-to nil)
  8818. (and org-log-post-message (message "%s" org-log-post-message)))
  8819. (defun org-remove-empty-drawer-at (drawer pos)
  8820. "Remove an emptyr DARWER drawer at position POS.
  8821. POS may also be a marker."
  8822. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8823. (save-excursion
  8824. (save-restriction
  8825. (widen)
  8826. (goto-char pos)
  8827. (if (org-in-regexp
  8828. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8829. (replace-match ""))))))
  8830. (defun org-sparse-tree (&optional arg)
  8831. "Create a sparse tree, prompt for the details.
  8832. This command can create sparse trees. You first need to select the type
  8833. of match used to create the tree:
  8834. t Show entries with a specific TODO keyword.
  8835. m Show entries selected by a tags/property match.
  8836. p Enter a property name and its value (both with completion on existing
  8837. names/values) and show entries with that property.
  8838. r Show entries matching a regular expression.
  8839. d Show deadlines due within `org-deadline-warning-days'.
  8840. b Show deadlines and scheduled items before a date.
  8841. a Show deadlines and scheduled items after a date."
  8842. (interactive "P")
  8843. (let (ans kwd value)
  8844. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  8845. (setq ans (read-char-exclusive))
  8846. (cond
  8847. ((equal ans ?d)
  8848. (call-interactively 'org-check-deadlines))
  8849. ((equal ans ?b)
  8850. (call-interactively 'org-check-before-date))
  8851. ((equal ans ?a)
  8852. (call-interactively 'org-check-after-date))
  8853. ((equal ans ?t)
  8854. (org-show-todo-tree '(4)))
  8855. ((member ans '(?T ?m))
  8856. (call-interactively 'org-match-sparse-tree))
  8857. ((member ans '(?p ?P))
  8858. (setq kwd (org-ido-completing-read "Property: "
  8859. (mapcar 'list (org-buffer-property-keys))))
  8860. (setq value (org-ido-completing-read "Value: "
  8861. (mapcar 'list (org-property-values kwd))))
  8862. (unless (string-match "\\`{.*}\\'" value)
  8863. (setq value (concat "\"" value "\"")))
  8864. (org-match-sparse-tree arg (concat kwd "=" value)))
  8865. ((member ans '(?r ?R ?/))
  8866. (call-interactively 'org-occur))
  8867. (t (error "No such sparse tree command \"%c\"" ans)))))
  8868. (defvar org-occur-highlights nil
  8869. "List of overlays used for occur matches.")
  8870. (make-variable-buffer-local 'org-occur-highlights)
  8871. (defvar org-occur-parameters nil
  8872. "Parameters of the active org-occur calls.
  8873. This is a list, each call to org-occur pushes as cons cell,
  8874. containing the regular expression and the callback, onto the list.
  8875. The list can contain several entries if `org-occur' has been called
  8876. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8877. will only contain one set of parameters. When the highlights are
  8878. removed (for example with `C-c C-c', or with the next edit (depending
  8879. on `org-remove-highlights-with-change'), this variable is emptied
  8880. as well.")
  8881. (make-variable-buffer-local 'org-occur-parameters)
  8882. (defun org-occur (regexp &optional keep-previous callback)
  8883. "Make a compact tree which shows all matches of REGEXP.
  8884. The tree will show the lines where the regexp matches, and all higher
  8885. headlines above the match. It will also show the heading after the match,
  8886. to make sure editing the matching entry is easy.
  8887. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8888. call to `org-occur' will be kept, to allow stacking of calls to this
  8889. command.
  8890. If CALLBACK is non-nil, it is a function which is called to confirm
  8891. that the match should indeed be shown."
  8892. (interactive "sRegexp: \nP")
  8893. (unless keep-previous
  8894. (org-remove-occur-highlights nil nil t))
  8895. (push (cons regexp callback) org-occur-parameters)
  8896. (let ((cnt 0))
  8897. (save-excursion
  8898. (goto-char (point-min))
  8899. (if (or (not keep-previous) ; do not want to keep
  8900. (not org-occur-highlights)) ; no previous matches
  8901. ;; hide everything
  8902. (org-overview))
  8903. (while (re-search-forward regexp nil t)
  8904. (when (or (not callback)
  8905. (save-match-data (funcall callback)))
  8906. (setq cnt (1+ cnt))
  8907. (when org-highlight-sparse-tree-matches
  8908. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8909. (org-show-context 'occur-tree))))
  8910. (when org-remove-highlights-with-change
  8911. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8912. nil 'local))
  8913. (unless org-sparse-tree-open-archived-trees
  8914. (org-hide-archived-subtrees (point-min) (point-max)))
  8915. (run-hooks 'org-occur-hook)
  8916. (if (interactive-p)
  8917. (message "%d match(es) for regexp %s" cnt regexp))
  8918. cnt))
  8919. (defun org-show-context (&optional key)
  8920. "Make sure point and context and visible.
  8921. How much context is shown depends upon the variables
  8922. `org-show-hierarchy-above', `org-show-following-heading'. and
  8923. `org-show-siblings'."
  8924. (let ((heading-p (org-on-heading-p t))
  8925. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8926. (following-p (org-get-alist-option org-show-following-heading key))
  8927. (entry-p (org-get-alist-option org-show-entry-below key))
  8928. (siblings-p (org-get-alist-option org-show-siblings key)))
  8929. (catch 'exit
  8930. ;; Show heading or entry text
  8931. (if (and heading-p (not entry-p))
  8932. (org-flag-heading nil) ; only show the heading
  8933. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8934. (org-show-hidden-entry))) ; show entire entry
  8935. (when following-p
  8936. ;; Show next sibling, or heading below text
  8937. (save-excursion
  8938. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8939. (org-flag-heading nil))))
  8940. (when siblings-p (org-show-siblings))
  8941. (when hierarchy-p
  8942. ;; show all higher headings, possibly with siblings
  8943. (save-excursion
  8944. (while (and (condition-case nil
  8945. (progn (org-up-heading-all 1) t)
  8946. (error nil))
  8947. (not (bobp)))
  8948. (org-flag-heading nil)
  8949. (when siblings-p (org-show-siblings))))))))
  8950. (defun org-reveal (&optional siblings)
  8951. "Show current entry, hierarchy above it, and the following headline.
  8952. This can be used to show a consistent set of context around locations
  8953. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8954. not t for the search context.
  8955. With optional argument SIBLINGS, on each level of the hierarchy all
  8956. siblings are shown. This repairs the tree structure to what it would
  8957. look like when opened with hierarchical calls to `org-cycle'."
  8958. (interactive "P")
  8959. (let ((org-show-hierarchy-above t)
  8960. (org-show-following-heading t)
  8961. (org-show-siblings (if siblings t org-show-siblings)))
  8962. (org-show-context nil)))
  8963. (defun org-highlight-new-match (beg end)
  8964. "Highlight from BEG to END and mark the highlight is an occur headline."
  8965. (let ((ov (org-make-overlay beg end)))
  8966. (org-overlay-put ov 'face 'secondary-selection)
  8967. (push ov org-occur-highlights)))
  8968. (defun org-remove-occur-highlights (&optional beg end noremove)
  8969. "Remove the occur highlights from the buffer.
  8970. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8971. from the `before-change-functions' in the current buffer."
  8972. (interactive)
  8973. (unless org-inhibit-highlight-removal
  8974. (mapc 'org-delete-overlay org-occur-highlights)
  8975. (setq org-occur-highlights nil)
  8976. (setq org-occur-parameters nil)
  8977. (unless noremove
  8978. (remove-hook 'before-change-functions
  8979. 'org-remove-occur-highlights 'local))))
  8980. ;;;; Priorities
  8981. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8982. "Regular expression matching the priority indicator.")
  8983. (defvar org-remove-priority-next-time nil)
  8984. (defun org-priority-up ()
  8985. "Increase the priority of the current item."
  8986. (interactive)
  8987. (org-priority 'up))
  8988. (defun org-priority-down ()
  8989. "Decrease the priority of the current item."
  8990. (interactive)
  8991. (org-priority 'down))
  8992. (defun org-priority (&optional action)
  8993. "Change the priority of an item by ARG.
  8994. ACTION can be `set', `up', `down', or a character."
  8995. (interactive)
  8996. (setq action (or action 'set))
  8997. (let (current new news have remove)
  8998. (save-excursion
  8999. (org-back-to-heading t)
  9000. (if (looking-at org-priority-regexp)
  9001. (setq current (string-to-char (match-string 2))
  9002. have t)
  9003. (setq current org-default-priority))
  9004. (cond
  9005. ((or (eq action 'set)
  9006. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9007. (if (not (eq action 'set))
  9008. (setq new action)
  9009. (message "Priority %c-%c, SPC to remove: "
  9010. org-highest-priority org-lowest-priority)
  9011. (setq new (read-char-exclusive)))
  9012. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9013. (= (upcase org-lowest-priority) org-lowest-priority))
  9014. (setq new (upcase new)))
  9015. (cond ((equal new ?\ ) (setq remove t))
  9016. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9017. (error "Priority must be between `%c' and `%c'"
  9018. org-highest-priority org-lowest-priority))))
  9019. ((eq action 'up)
  9020. (if (and (not have) (eq last-command this-command))
  9021. (setq new org-lowest-priority)
  9022. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9023. org-default-priority (1- current)))))
  9024. ((eq action 'down)
  9025. (if (and (not have) (eq last-command this-command))
  9026. (setq new org-highest-priority)
  9027. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9028. org-default-priority (1+ current)))))
  9029. (t (error "Invalid action")))
  9030. (if (or (< (upcase new) org-highest-priority)
  9031. (> (upcase new) org-lowest-priority))
  9032. (setq remove t))
  9033. (setq news (format "%c" new))
  9034. (if have
  9035. (if remove
  9036. (replace-match "" t t nil 1)
  9037. (replace-match news t t nil 2))
  9038. (if remove
  9039. (error "No priority cookie found in line")
  9040. (looking-at org-todo-line-regexp)
  9041. (if (match-end 2)
  9042. (progn
  9043. (goto-char (match-end 2))
  9044. (insert " [#" news "]"))
  9045. (goto-char (match-beginning 3))
  9046. (insert "[#" news "] ")))))
  9047. (org-preserve-lc (org-set-tags nil 'align))
  9048. (if remove
  9049. (message "Priority removed")
  9050. (message "Priority of current item set to %s" news))))
  9051. (defun org-get-priority (s)
  9052. "Find priority cookie and return priority."
  9053. (save-match-data
  9054. (if (not (string-match org-priority-regexp s))
  9055. (* 1000 (- org-lowest-priority org-default-priority))
  9056. (* 1000 (- org-lowest-priority
  9057. (string-to-char (match-string 2 s)))))))
  9058. ;;;; Tags
  9059. (defvar org-agenda-archives-mode)
  9060. (defvar org-scanner-tags nil
  9061. "The current tag list while the tags scanner is running.")
  9062. (defvar org-trust-scanner-tags nil
  9063. "Should `org-get-tags-at' use the tags fro the scanner.
  9064. This is for internal dynamical scoping only.
  9065. When this is non-nil, the function `org-get-tags-at' will return the value
  9066. of `org-scanner-tags' instead of building the list by itself. This
  9067. can lead to large speed-ups when the tags scanner is used in a file with
  9068. many entries, and when the list of tags is retrieved, for example to
  9069. obtain a list of properties. Building the tags list for each entry in such
  9070. a file becomes an N^2 operation - but with this variable set, it scales
  9071. as N.")
  9072. (defun org-scan-tags (action matcher &optional todo-only)
  9073. "Scan headline tags with inheritance and produce output ACTION.
  9074. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9075. or `agenda' to produce an entry list for an agenda view. It can also be
  9076. a Lisp form or a function that should be called at each matched headline, in
  9077. this case the return value is a list of all return values from these calls.
  9078. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9079. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9080. only lines with a TODO keyword are included in the output."
  9081. (require 'org-agenda)
  9082. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9083. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9084. (org-re
  9085. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9086. (props (list 'face 'default
  9087. 'done-face 'org-agenda-done
  9088. 'undone-face 'default
  9089. 'mouse-face 'highlight
  9090. 'org-not-done-regexp org-not-done-regexp
  9091. 'org-todo-regexp org-todo-regexp
  9092. 'keymap org-agenda-keymap
  9093. 'help-echo
  9094. (format "mouse-2 or RET jump to org file %s"
  9095. (abbreviate-file-name
  9096. (or (buffer-file-name (buffer-base-buffer))
  9097. (buffer-name (buffer-base-buffer)))))))
  9098. (case-fold-search nil)
  9099. lspos tags tags-list
  9100. (tags-alist (list (cons 0 org-file-tags)))
  9101. (llast 0) rtn rtn1 level category i txt
  9102. todo marker entry priority)
  9103. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9104. (setq action (list 'lambda nil action)))
  9105. (save-excursion
  9106. (goto-char (point-min))
  9107. (when (eq action 'sparse-tree)
  9108. (org-overview)
  9109. (org-remove-occur-highlights))
  9110. (while (re-search-forward re nil t)
  9111. (catch :skip
  9112. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9113. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9114. (goto-char (setq lspos (1+ (match-beginning 0))))
  9115. (setq level (org-reduced-level (funcall outline-level))
  9116. category (org-get-category))
  9117. (setq i llast llast level)
  9118. ;; remove tag lists from same and sublevels
  9119. (while (>= i level)
  9120. (when (setq entry (assoc i tags-alist))
  9121. (setq tags-alist (delete entry tags-alist)))
  9122. (setq i (1- i)))
  9123. ;; add the next tags
  9124. (when tags
  9125. (setq tags (org-split-string tags ":")
  9126. tags-alist
  9127. (cons (cons level tags) tags-alist)))
  9128. ;; compile tags for current headline
  9129. (setq tags-list
  9130. (if org-use-tag-inheritance
  9131. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9132. tags)
  9133. org-scanner-tags tags-list)
  9134. (when org-use-tag-inheritance
  9135. (setcdr (car tags-alist)
  9136. (mapcar (lambda (x)
  9137. (setq x (copy-sequence x))
  9138. (org-add-prop-inherited x))
  9139. (cdar tags-alist))))
  9140. (when (and tags org-use-tag-inheritance
  9141. (or (not (eq t org-use-tag-inheritance))
  9142. org-tags-exclude-from-inheritance))
  9143. ;; selective inheritance, remove uninherited ones
  9144. (setcdr (car tags-alist)
  9145. (org-remove-uniherited-tags (cdar tags-alist))))
  9146. (when (and (or (not todo-only)
  9147. (and (member todo org-not-done-keywords)
  9148. (or (not org-agenda-tags-todo-honor-ignore-options)
  9149. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9150. (let ((case-fold-search t)) (eval matcher))
  9151. (or
  9152. (not (member org-archive-tag tags-list))
  9153. ;; we have an archive tag, should we use this anyway?
  9154. (or (not org-agenda-skip-archived-trees)
  9155. (and (eq action 'agenda) org-agenda-archives-mode))))
  9156. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9157. ;; select this headline
  9158. (cond
  9159. ((eq action 'sparse-tree)
  9160. (and org-highlight-sparse-tree-matches
  9161. (org-get-heading) (match-end 0)
  9162. (org-highlight-new-match
  9163. (match-beginning 0) (match-beginning 1)))
  9164. (org-show-context 'tags-tree))
  9165. ((eq action 'agenda)
  9166. (setq txt (org-format-agenda-item
  9167. ""
  9168. (concat
  9169. (if (eq org-tags-match-list-sublevels 'indented)
  9170. (make-string (1- level) ?.) "")
  9171. (org-get-heading))
  9172. category
  9173. tags-list
  9174. )
  9175. priority (org-get-priority txt))
  9176. (goto-char lspos)
  9177. (setq marker (org-agenda-new-marker))
  9178. (org-add-props txt props
  9179. 'org-marker marker 'org-hd-marker marker 'org-category category
  9180. 'todo-state todo
  9181. 'priority priority 'type "tagsmatch")
  9182. (push txt rtn))
  9183. ((functionp action)
  9184. (save-excursion
  9185. (setq rtn1 (funcall action))
  9186. (push rtn1 rtn))
  9187. (goto-char (point-at-eol)))
  9188. (t (error "Invalid action")))
  9189. ;; if we are to skip sublevels, jump to end of subtree
  9190. (or org-tags-match-list-sublevels (org-end-of-subtree t))))
  9191. (and (= (point) lspos) (forward-char 1))))
  9192. (when (and (eq action 'sparse-tree)
  9193. (not org-sparse-tree-open-archived-trees))
  9194. (org-hide-archived-subtrees (point-min) (point-max)))
  9195. (nreverse rtn)))
  9196. (defun org-remove-uniherited-tags (tags)
  9197. "Remove all tags that are not inherited from the list TAGS."
  9198. (cond
  9199. ((eq org-use-tag-inheritance t)
  9200. (if org-tags-exclude-from-inheritance
  9201. (org-delete-all org-tags-exclude-from-inheritance tags)
  9202. tags))
  9203. ((not org-use-tag-inheritance) nil)
  9204. ((stringp org-use-tag-inheritance)
  9205. (delq nil (mapcar
  9206. (lambda (x)
  9207. (if (and (string-match org-use-tag-inheritance x)
  9208. (not (member x org-tags-exclude-from-inheritance)))
  9209. x nil))
  9210. tags)))
  9211. ((listp org-use-tag-inheritance)
  9212. (delq nil (mapcar
  9213. (lambda (x)
  9214. (if (member x org-use-tag-inheritance) x nil))
  9215. tags)))))
  9216. (defvar todo-only) ;; dynamically scoped
  9217. (defun org-match-sparse-tree (&optional todo-only match)
  9218. "Create a sparse tree according to tags string MATCH.
  9219. MATCH can contain positive and negative selection of tags, like
  9220. \"+WORK+URGENT-WITHBOSS\".
  9221. If optional argument TODO-ONLY is non-nil, only select lines that are
  9222. also TODO lines."
  9223. (interactive "P")
  9224. (org-prepare-agenda-buffers (list (current-buffer)))
  9225. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9226. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9227. (defvar org-cached-props nil)
  9228. (defun org-cached-entry-get (pom property)
  9229. (if (or (eq t org-use-property-inheritance)
  9230. (and (stringp org-use-property-inheritance)
  9231. (string-match org-use-property-inheritance property))
  9232. (and (listp org-use-property-inheritance)
  9233. (member property org-use-property-inheritance)))
  9234. ;; Caching is not possible, check it directly
  9235. (org-entry-get pom property 'inherit)
  9236. ;; Get all properties, so that we can do complicated checks easily
  9237. (cdr (assoc property (or org-cached-props
  9238. (setq org-cached-props
  9239. (org-entry-properties pom)))))))
  9240. (defun org-global-tags-completion-table (&optional files)
  9241. "Return the list of all tags in all agenda buffer/files."
  9242. (save-excursion
  9243. (org-uniquify
  9244. (delq nil
  9245. (apply 'append
  9246. (mapcar
  9247. (lambda (file)
  9248. (set-buffer (find-file-noselect file))
  9249. (append (org-get-buffer-tags)
  9250. (mapcar (lambda (x) (if (stringp (car-safe x))
  9251. (list (car-safe x)) nil))
  9252. org-tag-alist)))
  9253. (if (and files (car files))
  9254. files
  9255. (org-agenda-files))))))))
  9256. (defun org-make-tags-matcher (match)
  9257. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9258. ;; todo-only is scoped dynamically into this function, and the function
  9259. ;; may change it if the matcher asks for it.
  9260. (unless match
  9261. ;; Get a new match request, with completion
  9262. (let ((org-last-tags-completion-table
  9263. (org-global-tags-completion-table)))
  9264. (setq match (org-completing-read-no-ido
  9265. "Match: " 'org-tags-completion-function nil nil nil
  9266. 'org-tags-history))))
  9267. ;; Parse the string and create a lisp form
  9268. (let ((match0 match)
  9269. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9270. minus tag mm
  9271. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9272. orterms term orlist re-p str-p level-p level-op time-p
  9273. prop-p pn pv po cat-p gv rest)
  9274. (if (string-match "/+" match)
  9275. ;; match contains also a todo-matching request
  9276. (progn
  9277. (setq tagsmatch (substring match 0 (match-beginning 0))
  9278. todomatch (substring match (match-end 0)))
  9279. (if (string-match "^!" todomatch)
  9280. (setq todo-only t todomatch (substring todomatch 1)))
  9281. (if (string-match "^\\s-*$" todomatch)
  9282. (setq todomatch nil)))
  9283. ;; only matching tags
  9284. (setq tagsmatch match todomatch nil))
  9285. ;; Make the tags matcher
  9286. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9287. (setq tagsmatcher t)
  9288. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9289. (while (setq term (pop orterms))
  9290. (while (and (equal (substring term -1) "\\") orterms)
  9291. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9292. (while (string-match re term)
  9293. (setq rest (substring term (match-end 0))
  9294. minus (and (match-end 1)
  9295. (equal (match-string 1 term) "-"))
  9296. tag (match-string 2 term)
  9297. re-p (equal (string-to-char tag) ?{)
  9298. level-p (match-end 4)
  9299. prop-p (match-end 5)
  9300. mm (cond
  9301. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9302. (level-p
  9303. (setq level-op (org-op-to-function (match-string 3 term)))
  9304. `(,level-op level ,(string-to-number
  9305. (match-string 4 term))))
  9306. (prop-p
  9307. (setq pn (match-string 5 term)
  9308. po (match-string 6 term)
  9309. pv (match-string 7 term)
  9310. cat-p (equal pn "CATEGORY")
  9311. re-p (equal (string-to-char pv) ?{)
  9312. str-p (equal (string-to-char pv) ?\")
  9313. time-p (save-match-data
  9314. (string-match "^\"[[<].*[]>]\"$" pv))
  9315. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9316. (if time-p (setq pv (org-matcher-time pv)))
  9317. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9318. (cond
  9319. ((equal pn "CATEGORY")
  9320. (setq gv '(get-text-property (point) 'org-category)))
  9321. ((equal pn "TODO")
  9322. (setq gv 'todo))
  9323. (t
  9324. (setq gv `(org-cached-entry-get nil ,pn))))
  9325. (if re-p
  9326. (if (eq po 'org<>)
  9327. `(not (string-match ,pv (or ,gv "")))
  9328. `(string-match ,pv (or ,gv "")))
  9329. (if str-p
  9330. `(,po (or ,gv "") ,pv)
  9331. `(,po (string-to-number (or ,gv ""))
  9332. ,(string-to-number pv) ))))
  9333. (t `(member ,tag tags-list)))
  9334. mm (if minus (list 'not mm) mm)
  9335. term rest)
  9336. (push mm tagsmatcher))
  9337. (push (if (> (length tagsmatcher) 1)
  9338. (cons 'and tagsmatcher)
  9339. (car tagsmatcher))
  9340. orlist)
  9341. (setq tagsmatcher nil))
  9342. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9343. (setq tagsmatcher
  9344. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9345. ;; Make the todo matcher
  9346. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9347. (setq todomatcher t)
  9348. (setq orterms (org-split-string todomatch "|") orlist nil)
  9349. (while (setq term (pop orterms))
  9350. (while (string-match re term)
  9351. (setq minus (and (match-end 1)
  9352. (equal (match-string 1 term) "-"))
  9353. kwd (match-string 2 term)
  9354. re-p (equal (string-to-char kwd) ?{)
  9355. term (substring term (match-end 0))
  9356. mm (if re-p
  9357. `(string-match ,(substring kwd 1 -1) todo)
  9358. (list 'equal 'todo kwd))
  9359. mm (if minus (list 'not mm) mm))
  9360. (push mm todomatcher))
  9361. (push (if (> (length todomatcher) 1)
  9362. (cons 'and todomatcher)
  9363. (car todomatcher))
  9364. orlist)
  9365. (setq todomatcher nil))
  9366. (setq todomatcher (if (> (length orlist) 1)
  9367. (cons 'or orlist) (car orlist))))
  9368. ;; Return the string and lisp forms of the matcher
  9369. (setq matcher (if todomatcher
  9370. (list 'and tagsmatcher todomatcher)
  9371. tagsmatcher))
  9372. (cons match0 matcher)))
  9373. (defun org-op-to-function (op &optional stringp)
  9374. "Turn an operator into the appropriate function."
  9375. (setq op
  9376. (cond
  9377. ((equal op "<" ) '(< string< org-time<))
  9378. ((equal op ">" ) '(> org-string> org-time>))
  9379. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9380. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9381. ((member op '("=" "==")) '(= string= org-time=))
  9382. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9383. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9384. (defun org<> (a b) (not (= a b)))
  9385. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9386. (defun org-string>= (a b) (not (string< a b)))
  9387. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9388. (defun org-string<> (a b) (not (string= a b)))
  9389. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9390. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9391. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9392. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9393. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9394. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9395. (defun org-2ft (s)
  9396. "Convert S to a floating point time.
  9397. If S is already a number, just return it. If it is a string, parse
  9398. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9399. (cond
  9400. ((numberp s) s)
  9401. ((stringp s)
  9402. (condition-case nil
  9403. (float-time (apply 'encode-time (org-parse-time-string s)))
  9404. (error 0.)))
  9405. (t 0.)))
  9406. (defun org-time-today ()
  9407. "Time in seconds today at 0:00.
  9408. Returns the float number of seconds since the beginning of the
  9409. epoch to the beginning of today (00:00)."
  9410. (float-time (apply 'encode-time
  9411. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9412. (defun org-matcher-time (s)
  9413. "Interpret a time comparison value."
  9414. (save-match-data
  9415. (cond
  9416. ((string= s "<now>") (float-time))
  9417. ((string= s "<today>") (org-time-today))
  9418. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9419. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9420. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9421. (+ (org-time-today)
  9422. (* (string-to-number (match-string 1 s))
  9423. (cdr (assoc (match-string 2 s)
  9424. '(("d" . 86400.0) ("w" . 604800.0)
  9425. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9426. (t (org-2ft s)))))
  9427. (defun org-match-any-p (re list)
  9428. "Does re match any element of list?"
  9429. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9430. (delq nil list))
  9431. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9432. (defvar org-tags-overlay (org-make-overlay 1 1))
  9433. (org-detach-overlay org-tags-overlay)
  9434. (defun org-get-local-tags-at (&optional pos)
  9435. "Get a list of tags defined in the current headline."
  9436. (org-get-tags-at pos 'local))
  9437. (defun org-get-local-tags ()
  9438. "Get a list of tags defined in the current headline."
  9439. (org-get-tags-at nil 'local))
  9440. (defun org-get-tags-at (&optional pos local)
  9441. "Get a list of all headline tags applicable at POS.
  9442. POS defaults to point. If tags are inherited, the list contains
  9443. the targets in the same sequence as the headlines appear, i.e.
  9444. the tags of the current headline come last.
  9445. When LOCAL is non-nil, only return tags from the current headline,
  9446. ignore inherited ones."
  9447. (interactive)
  9448. (if (and org-trust-scanner-tags
  9449. (or (not pos) (equal pos (point)))
  9450. (not local))
  9451. org-scanner-tags
  9452. (let (tags ltags lastpos parent)
  9453. (save-excursion
  9454. (save-restriction
  9455. (widen)
  9456. (goto-char (or pos (point)))
  9457. (save-match-data
  9458. (catch 'done
  9459. (condition-case nil
  9460. (progn
  9461. (org-back-to-heading t)
  9462. (while (not (equal lastpos (point)))
  9463. (setq lastpos (point))
  9464. (when (looking-at
  9465. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9466. (setq ltags (org-split-string
  9467. (org-match-string-no-properties 1) ":"))
  9468. (when parent
  9469. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9470. (setq tags (append
  9471. (if parent
  9472. (org-remove-uniherited-tags ltags)
  9473. ltags)
  9474. tags)))
  9475. (or org-use-tag-inheritance (throw 'done t))
  9476. (if local (throw 'done t))
  9477. (org-up-heading-all 1)
  9478. (setq parent t)))
  9479. (error nil)))))
  9480. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9481. (defun org-add-prop-inherited (s)
  9482. (add-text-properties 0 (length s) '(inherited t) s)
  9483. s)
  9484. (defun org-toggle-tag (tag &optional onoff)
  9485. "Toggle the tag TAG for the current line.
  9486. If ONOFF is `on' or `off', don't toggle but set to this state."
  9487. (let (res current)
  9488. (save-excursion
  9489. (org-back-to-heading t)
  9490. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9491. (point-at-eol) t)
  9492. (progn
  9493. (setq current (match-string 1))
  9494. (replace-match ""))
  9495. (setq current ""))
  9496. (setq current (nreverse (org-split-string current ":")))
  9497. (cond
  9498. ((eq onoff 'on)
  9499. (setq res t)
  9500. (or (member tag current) (push tag current)))
  9501. ((eq onoff 'off)
  9502. (or (not (member tag current)) (setq current (delete tag current))))
  9503. (t (if (member tag current)
  9504. (setq current (delete tag current))
  9505. (setq res t)
  9506. (push tag current))))
  9507. (end-of-line 1)
  9508. (if current
  9509. (progn
  9510. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9511. (org-set-tags nil t))
  9512. (delete-horizontal-space))
  9513. (run-hooks 'org-after-tags-change-hook))
  9514. res))
  9515. (defun org-align-tags-here (to-col)
  9516. ;; Assumes that this is a headline
  9517. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9518. (beginning-of-line 1)
  9519. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9520. (< pos (match-beginning 2)))
  9521. (progn
  9522. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9523. (goto-char (match-beginning 1))
  9524. (insert " ")
  9525. (delete-region (point) (1+ (match-beginning 2)))
  9526. (setq ncol (max (1+ (current-column))
  9527. (1+ col)
  9528. (if (> to-col 0)
  9529. to-col
  9530. (- (abs to-col) tags-l))))
  9531. (setq p (point))
  9532. (insert (make-string (- ncol (current-column)) ?\ ))
  9533. (setq ncol (current-column))
  9534. (when indent-tabs-mode (tabify p (point-at-eol)))
  9535. (org-move-to-column (min ncol col) t))
  9536. (goto-char pos))))
  9537. (defun org-set-tags-command (&optional arg just-align)
  9538. "Call the set-tags command for the current entry."
  9539. (interactive "P")
  9540. (if (org-on-heading-p)
  9541. (org-set-tags arg just-align)
  9542. (save-excursion
  9543. (org-back-to-heading t)
  9544. (org-set-tags arg just-align))))
  9545. (defun org-set-tags (&optional arg just-align)
  9546. "Set the tags for the current headline.
  9547. With prefix ARG, realign all tags in headings in the current buffer."
  9548. (interactive "P")
  9549. (let* ((re (concat "^" outline-regexp))
  9550. (current (org-get-tags-string))
  9551. (col (current-column))
  9552. (org-setting-tags t)
  9553. table current-tags inherited-tags ; computed below when needed
  9554. tags p0 c0 c1 rpl)
  9555. (if arg
  9556. (save-excursion
  9557. (goto-char (point-min))
  9558. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9559. (while (re-search-forward re nil t)
  9560. (org-set-tags nil t)
  9561. (end-of-line 1)))
  9562. (message "All tags realigned to column %d" org-tags-column))
  9563. (if just-align
  9564. (setq tags current)
  9565. ;; Get a new set of tags from the user
  9566. (save-excursion
  9567. (setq table (append org-tag-persistent-alist
  9568. (or org-tag-alist (org-get-buffer-tags)))
  9569. org-last-tags-completion-table table
  9570. current-tags (org-split-string current ":")
  9571. inherited-tags (nreverse
  9572. (nthcdr (length current-tags)
  9573. (nreverse (org-get-tags-at))))
  9574. tags
  9575. (if (or (eq t org-use-fast-tag-selection)
  9576. (and org-use-fast-tag-selection
  9577. (delq nil (mapcar 'cdr table))))
  9578. (org-fast-tag-selection
  9579. current-tags inherited-tags table
  9580. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9581. (let ((org-add-colon-after-tag-completion t))
  9582. (org-trim
  9583. (org-without-partial-completion
  9584. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9585. nil nil current 'org-tags-history)))))))
  9586. (while (string-match "[-+&]+" tags)
  9587. ;; No boolean logic, just a list
  9588. (setq tags (replace-match ":" t t tags))))
  9589. (if (string-match "\\`[\t ]*\\'" tags)
  9590. (setq tags "")
  9591. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9592. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9593. ;; Insert new tags at the correct column
  9594. (beginning-of-line 1)
  9595. (cond
  9596. ((and (equal current "") (equal tags "")))
  9597. ((re-search-forward
  9598. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9599. (point-at-eol) t)
  9600. (if (equal tags "")
  9601. (setq rpl "")
  9602. (goto-char (match-beginning 0))
  9603. (setq c0 (current-column) p0 (point)
  9604. c1 (max (1+ c0) (if (> org-tags-column 0)
  9605. org-tags-column
  9606. (- (- org-tags-column) (length tags))))
  9607. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9608. (replace-match rpl t t)
  9609. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9610. tags)
  9611. (t (error "Tags alignment failed")))
  9612. (org-move-to-column col)
  9613. (unless just-align
  9614. (run-hooks 'org-after-tags-change-hook)))))
  9615. (defun org-change-tag-in-region (beg end tag off)
  9616. "Add or remove TAG for each entry in the region.
  9617. This works in the agenda, and also in an org-mode buffer."
  9618. (interactive
  9619. (list (region-beginning) (region-end)
  9620. (let ((org-last-tags-completion-table
  9621. (if (org-mode-p)
  9622. (org-get-buffer-tags)
  9623. (org-global-tags-completion-table))))
  9624. (org-ido-completing-read
  9625. "Tag: " 'org-tags-completion-function nil nil nil
  9626. 'org-tags-history))
  9627. (progn
  9628. (message "[s]et or [r]emove? ")
  9629. (equal (read-char-exclusive) ?r))))
  9630. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9631. (let ((agendap (equal major-mode 'org-agenda-mode))
  9632. l1 l2 m buf pos newhead (cnt 0))
  9633. (goto-char end)
  9634. (setq l2 (1- (org-current-line)))
  9635. (goto-char beg)
  9636. (setq l1 (org-current-line))
  9637. (loop for l from l1 to l2 do
  9638. (goto-line l)
  9639. (setq m (get-text-property (point) 'org-hd-marker))
  9640. (when (or (and (org-mode-p) (org-on-heading-p))
  9641. (and agendap m))
  9642. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9643. pos (if agendap m (point)))
  9644. (with-current-buffer buf
  9645. (save-excursion
  9646. (save-restriction
  9647. (goto-char pos)
  9648. (setq cnt (1+ cnt))
  9649. (org-toggle-tag tag (if off 'off 'on))
  9650. (setq newhead (org-get-heading)))))
  9651. (and agendap (org-agenda-change-all-lines newhead m))))
  9652. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9653. (defun org-tags-completion-function (string predicate &optional flag)
  9654. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9655. (confirm (lambda (x) (stringp (car x)))))
  9656. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9657. (setq s1 (match-string 1 string)
  9658. s2 (match-string 2 string))
  9659. (setq s1 "" s2 string))
  9660. (cond
  9661. ((eq flag nil)
  9662. ;; try completion
  9663. (setq rtn (try-completion s2 ctable confirm))
  9664. (if (stringp rtn)
  9665. (setq rtn
  9666. (concat s1 s2 (substring rtn (length s2))
  9667. (if (and org-add-colon-after-tag-completion
  9668. (assoc rtn ctable))
  9669. ":" ""))))
  9670. rtn)
  9671. ((eq flag t)
  9672. ;; all-completions
  9673. (all-completions s2 ctable confirm)
  9674. )
  9675. ((eq flag 'lambda)
  9676. ;; exact match?
  9677. (assoc s2 ctable)))
  9678. ))
  9679. (defun org-fast-tag-insert (kwd tags face &optional end)
  9680. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9681. (insert (format "%-12s" (concat kwd ":"))
  9682. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9683. (or end "")))
  9684. (defun org-fast-tag-show-exit (flag)
  9685. (save-excursion
  9686. (goto-line 3)
  9687. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9688. (replace-match ""))
  9689. (when flag
  9690. (end-of-line 1)
  9691. (org-move-to-column (- (window-width) 19) t)
  9692. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9693. (defun org-set-current-tags-overlay (current prefix)
  9694. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9695. (if (featurep 'xemacs)
  9696. (org-overlay-display org-tags-overlay (concat prefix s)
  9697. 'secondary-selection)
  9698. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9699. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9700. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9701. "Fast tag selection with single keys.
  9702. CURRENT is the current list of tags in the headline, INHERITED is the
  9703. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9704. possibly with grouping information. TODO-TABLE is a similar table with
  9705. TODO keywords, should these have keys assigned to them.
  9706. If the keys are nil, a-z are automatically assigned.
  9707. Returns the new tags string, or nil to not change the current settings."
  9708. (let* ((fulltable (append table todo-table))
  9709. (maxlen (apply 'max (mapcar
  9710. (lambda (x)
  9711. (if (stringp (car x)) (string-width (car x)) 0))
  9712. fulltable)))
  9713. (buf (current-buffer))
  9714. (expert (eq org-fast-tag-selection-single-key 'expert))
  9715. (buffer-tags nil)
  9716. (fwidth (+ maxlen 3 1 3))
  9717. (ncol (/ (- (window-width) 4) fwidth))
  9718. (i-face 'org-done)
  9719. (c-face 'org-todo)
  9720. tg cnt e c char c1 c2 ntable tbl rtn
  9721. ov-start ov-end ov-prefix
  9722. (exit-after-next org-fast-tag-selection-single-key)
  9723. (done-keywords org-done-keywords)
  9724. groups ingroup)
  9725. (save-excursion
  9726. (beginning-of-line 1)
  9727. (if (looking-at
  9728. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9729. (setq ov-start (match-beginning 1)
  9730. ov-end (match-end 1)
  9731. ov-prefix "")
  9732. (setq ov-start (1- (point-at-eol))
  9733. ov-end (1+ ov-start))
  9734. (skip-chars-forward "^\n\r")
  9735. (setq ov-prefix
  9736. (concat
  9737. (buffer-substring (1- (point)) (point))
  9738. (if (> (current-column) org-tags-column)
  9739. " "
  9740. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9741. (org-move-overlay org-tags-overlay ov-start ov-end)
  9742. (save-window-excursion
  9743. (if expert
  9744. (set-buffer (get-buffer-create " *Org tags*"))
  9745. (delete-other-windows)
  9746. (split-window-vertically)
  9747. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9748. (erase-buffer)
  9749. (org-set-local 'org-done-keywords done-keywords)
  9750. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9751. (org-fast-tag-insert "Current" current c-face "\n\n")
  9752. (org-fast-tag-show-exit exit-after-next)
  9753. (org-set-current-tags-overlay current ov-prefix)
  9754. (setq tbl fulltable char ?a cnt 0)
  9755. (while (setq e (pop tbl))
  9756. (cond
  9757. ((equal e '(:startgroup))
  9758. (push '() groups) (setq ingroup t)
  9759. (when (not (= cnt 0))
  9760. (setq cnt 0)
  9761. (insert "\n"))
  9762. (insert "{ "))
  9763. ((equal e '(:endgroup))
  9764. (setq ingroup nil cnt 0)
  9765. (insert "}\n"))
  9766. ((equal e '(:newline))
  9767. (when (not (= cnt 0))
  9768. (setq cnt 0)
  9769. (insert "\n")
  9770. (setq e (car tbl))
  9771. (while (equal (car tbl) '(:newline))
  9772. (insert "\n")
  9773. (setq tbl (cdr tbl)))))
  9774. (t
  9775. (setq tg (car e) c2 nil)
  9776. (if (cdr e)
  9777. (setq c (cdr e))
  9778. ;; automatically assign a character.
  9779. (setq c1 (string-to-char
  9780. (downcase (substring
  9781. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9782. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9783. (while (or (rassoc char ntable) (rassoc char table))
  9784. (setq char (1+ char)))
  9785. (setq c2 c1))
  9786. (setq c (or c2 char)))
  9787. (if ingroup (push tg (car groups)))
  9788. (setq tg (org-add-props tg nil 'face
  9789. (cond
  9790. ((not (assoc tg table))
  9791. (org-get-todo-face tg))
  9792. ((member tg current) c-face)
  9793. ((member tg inherited) i-face)
  9794. (t nil))))
  9795. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9796. (insert "[" c "] " tg (make-string
  9797. (- fwidth 4 (length tg)) ?\ ))
  9798. (push (cons tg c) ntable)
  9799. (when (= (setq cnt (1+ cnt)) ncol)
  9800. (insert "\n")
  9801. (if ingroup (insert " "))
  9802. (setq cnt 0)))))
  9803. (setq ntable (nreverse ntable))
  9804. (insert "\n")
  9805. (goto-char (point-min))
  9806. (if (not expert) (org-fit-window-to-buffer))
  9807. (setq rtn
  9808. (catch 'exit
  9809. (while t
  9810. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9811. (if groups " [!] no groups" " [!]groups")
  9812. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9813. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9814. (cond
  9815. ((= c ?\r) (throw 'exit t))
  9816. ((= c ?!)
  9817. (setq groups (not groups))
  9818. (goto-char (point-min))
  9819. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9820. ((= c ?\C-c)
  9821. (if (not expert)
  9822. (org-fast-tag-show-exit
  9823. (setq exit-after-next (not exit-after-next)))
  9824. (setq expert nil)
  9825. (delete-other-windows)
  9826. (split-window-vertically)
  9827. (org-switch-to-buffer-other-window " *Org tags*")
  9828. (org-fit-window-to-buffer)))
  9829. ((or (= c ?\C-g)
  9830. (and (= c ?q) (not (rassoc c ntable))))
  9831. (org-detach-overlay org-tags-overlay)
  9832. (setq quit-flag t))
  9833. ((= c ?\ )
  9834. (setq current nil)
  9835. (if exit-after-next (setq exit-after-next 'now)))
  9836. ((= c ?\t)
  9837. (condition-case nil
  9838. (setq tg (org-ido-completing-read
  9839. "Tag: "
  9840. (or buffer-tags
  9841. (with-current-buffer buf
  9842. (org-get-buffer-tags)))))
  9843. (quit (setq tg "")))
  9844. (when (string-match "\\S-" tg)
  9845. (add-to-list 'buffer-tags (list tg))
  9846. (if (member tg current)
  9847. (setq current (delete tg current))
  9848. (push tg current)))
  9849. (if exit-after-next (setq exit-after-next 'now)))
  9850. ((setq e (rassoc c todo-table) tg (car e))
  9851. (with-current-buffer buf
  9852. (save-excursion (org-todo tg)))
  9853. (if exit-after-next (setq exit-after-next 'now)))
  9854. ((setq e (rassoc c ntable) tg (car e))
  9855. (if (member tg current)
  9856. (setq current (delete tg current))
  9857. (loop for g in groups do
  9858. (if (member tg g)
  9859. (mapc (lambda (x)
  9860. (setq current (delete x current)))
  9861. g)))
  9862. (push tg current))
  9863. (if exit-after-next (setq exit-after-next 'now))))
  9864. ;; Create a sorted list
  9865. (setq current
  9866. (sort current
  9867. (lambda (a b)
  9868. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9869. (if (eq exit-after-next 'now) (throw 'exit t))
  9870. (goto-char (point-min))
  9871. (beginning-of-line 2)
  9872. (delete-region (point) (point-at-eol))
  9873. (org-fast-tag-insert "Current" current c-face)
  9874. (org-set-current-tags-overlay current ov-prefix)
  9875. (while (re-search-forward
  9876. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9877. (setq tg (match-string 1))
  9878. (add-text-properties
  9879. (match-beginning 1) (match-end 1)
  9880. (list 'face
  9881. (cond
  9882. ((member tg current) c-face)
  9883. ((member tg inherited) i-face)
  9884. (t (get-text-property (match-beginning 1) 'face))))))
  9885. (goto-char (point-min)))))
  9886. (org-detach-overlay org-tags-overlay)
  9887. (if rtn
  9888. (mapconcat 'identity current ":")
  9889. nil))))
  9890. (defun org-get-tags-string ()
  9891. "Get the TAGS string in the current headline."
  9892. (unless (org-on-heading-p t)
  9893. (error "Not on a heading"))
  9894. (save-excursion
  9895. (beginning-of-line 1)
  9896. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9897. (org-match-string-no-properties 1)
  9898. "")))
  9899. (defun org-get-tags ()
  9900. "Get the list of tags specified in the current headline."
  9901. (org-split-string (org-get-tags-string) ":"))
  9902. (defun org-get-buffer-tags ()
  9903. "Get a table of all tags used in the buffer, for completion."
  9904. (let (tags)
  9905. (save-excursion
  9906. (goto-char (point-min))
  9907. (while (re-search-forward
  9908. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9909. (when (equal (char-after (point-at-bol 0)) ?*)
  9910. (mapc (lambda (x) (add-to-list 'tags x))
  9911. (org-split-string (org-match-string-no-properties 1) ":")))))
  9912. (mapcar 'list tags)))
  9913. ;;;; The mapping API
  9914. ;;;###autoload
  9915. (defun org-map-entries (func &optional match scope &rest skip)
  9916. "Call FUNC at each headline selected by MATCH in SCOPE.
  9917. FUNC is a function or a lisp form. The function will be called without
  9918. arguments, with the cursor positioned at the beginning of the headline.
  9919. The return values of all calls to the function will be collected and
  9920. returned as a list.
  9921. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9922. Only headlines that are matched by this query will be considered during
  9923. the iteration. When MATCH is nil or t, all headlines will be
  9924. visited by the iteration.
  9925. SCOPE determines the scope of this command. It can be any of:
  9926. nil The current buffer, respecting the restriction if any
  9927. tree The subtree started with the entry at point
  9928. file The current buffer, without restriction
  9929. file-with-archives
  9930. The current buffer, and any archives associated with it
  9931. agenda All agenda files
  9932. agenda-with-archives
  9933. All agenda files with any archive files associated with them
  9934. \(file1 file2 ...)
  9935. If this is a list, all files in the list will be scanned
  9936. The remaining args are treated as settings for the skipping facilities of
  9937. the scanner. The following items can be given here:
  9938. archive skip trees with the archive tag.
  9939. comment skip trees with the COMMENT keyword
  9940. function or Emacs Lisp form:
  9941. will be used as value for `org-agenda-skip-function', so whenever
  9942. the the function returns t, FUNC will not be called for that
  9943. entry and search will continue from the point where the
  9944. function leaves it.
  9945. If your function needs to retrieve the tags including inherited tags
  9946. at the *current* entry, you can use the value of the variable
  9947. `org-scanner-tags' which will be much faster than getting the value
  9948. with `org-get-tags-at'. If your function gets properties with
  9949. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  9950. to t around the call to `org-entry-properties' to get the same speedup.
  9951. Note that if your function moves around to retrieve tags and properties at
  9952. a *different* entry, you cannot use these techniques."
  9953. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9954. (org-agenda-skip-archived-trees (memq 'archive skip))
  9955. (org-agenda-skip-comment-trees (memq 'comment skip))
  9956. (org-agenda-skip-function
  9957. (car (org-delete-all '(comment archive) skip)))
  9958. (org-tags-match-list-sublevels t)
  9959. matcher file res
  9960. org-todo-keywords-for-agenda
  9961. org-done-keywords-for-agenda
  9962. org-todo-keyword-alist-for-agenda
  9963. org-tag-alist-for-agenda)
  9964. (cond
  9965. ((eq match t) (setq matcher t))
  9966. ((eq match nil) (setq matcher t))
  9967. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9968. (save-excursion
  9969. (save-restriction
  9970. (when (eq scope 'tree)
  9971. (org-back-to-heading t)
  9972. (org-narrow-to-subtree)
  9973. (setq scope nil))
  9974. (if (not scope)
  9975. (progn
  9976. (org-prepare-agenda-buffers
  9977. (list (buffer-file-name (current-buffer))))
  9978. (setq res (org-scan-tags func matcher)))
  9979. ;; Get the right scope
  9980. (cond
  9981. ((and scope (listp scope) (symbolp (car scope)))
  9982. (setq scope (eval scope)))
  9983. ((eq scope 'agenda)
  9984. (setq scope (org-agenda-files t)))
  9985. ((eq scope 'agenda-with-archives)
  9986. (setq scope (org-agenda-files t))
  9987. (setq scope (org-add-archive-files scope)))
  9988. ((eq scope 'file)
  9989. (setq scope (list (buffer-file-name))))
  9990. ((eq scope 'file-with-archives)
  9991. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9992. (org-prepare-agenda-buffers scope)
  9993. (while (setq file (pop scope))
  9994. (with-current-buffer (org-find-base-buffer-visiting file)
  9995. (save-excursion
  9996. (save-restriction
  9997. (widen)
  9998. (goto-char (point-min))
  9999. (setq res (append res (org-scan-tags func matcher))))))))))
  10000. res))
  10001. ;;;; Properties
  10002. ;;; Setting and retrieving properties
  10003. (defconst org-special-properties
  10004. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10005. "TIMESTAMP" "TIMESTAMP_IA")
  10006. "The special properties valid in Org-mode.
  10007. These are properties that are not defined in the property drawer,
  10008. but in some other way.")
  10009. (defconst org-default-properties
  10010. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  10011. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10012. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10013. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  10014. "Some properties that are used by Org-mode for various purposes.
  10015. Being in this list makes sure that they are offered for completion.")
  10016. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10017. "Regular expression matching the first line of a property drawer.")
  10018. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10019. "Regular expression matching the first line of a property drawer.")
  10020. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10021. "Regular expression matching the first line of a property drawer.")
  10022. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10023. "Regular expression matching the first line of a property drawer.")
  10024. (defconst org-property-drawer-re
  10025. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10026. org-property-end-re "\\)\n?")
  10027. "Matches an entire property drawer.")
  10028. (defconst org-clock-drawer-re
  10029. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10030. org-property-end-re "\\)\n?")
  10031. "Matches an entire clock drawer.")
  10032. (defun org-property-action ()
  10033. "Do an action on properties."
  10034. (interactive)
  10035. (let (c)
  10036. (org-at-property-p)
  10037. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10038. (setq c (read-char-exclusive))
  10039. (cond
  10040. ((equal c ?s)
  10041. (call-interactively 'org-set-property))
  10042. ((equal c ?d)
  10043. (call-interactively 'org-delete-property))
  10044. ((equal c ?D)
  10045. (call-interactively 'org-delete-property-globally))
  10046. ((equal c ?c)
  10047. (call-interactively 'org-compute-property-at-point))
  10048. (t (error "No such property action %c" c)))))
  10049. (defun org-at-property-p ()
  10050. "Is the cursor in a property line?"
  10051. ;; FIXME: Does not check if we are actually in the drawer.
  10052. ;; FIXME: also returns true on any drawers.....
  10053. ;; This is used by C-c C-c for property action.
  10054. (save-excursion
  10055. (beginning-of-line 1)
  10056. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10057. (defun org-get-property-block (&optional beg end force)
  10058. "Return the (beg . end) range of the body of the property drawer.
  10059. BEG and END can be beginning and end of subtree, if not given
  10060. they will be found.
  10061. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10062. (catch 'exit
  10063. (save-excursion
  10064. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10065. (end (or end (progn (outline-next-heading) (point)))))
  10066. (goto-char beg)
  10067. (if (re-search-forward org-property-start-re end t)
  10068. (setq beg (1+ (match-end 0)))
  10069. (if force
  10070. (save-excursion
  10071. (org-insert-property-drawer)
  10072. (setq end (progn (outline-next-heading) (point))))
  10073. (throw 'exit nil))
  10074. (goto-char beg)
  10075. (if (re-search-forward org-property-start-re end t)
  10076. (setq beg (1+ (match-end 0)))))
  10077. (if (re-search-forward org-property-end-re end t)
  10078. (setq end (match-beginning 0))
  10079. (or force (throw 'exit nil))
  10080. (goto-char beg)
  10081. (setq end beg)
  10082. (org-indent-line-function)
  10083. (insert ":END:\n"))
  10084. (cons beg end)))))
  10085. (defun org-entry-properties (&optional pom which)
  10086. "Get all properties of the entry at point-or-marker POM.
  10087. This includes the TODO keyword, the tags, time strings for deadline,
  10088. scheduled, and clocking, and any additional properties defined in the
  10089. entry. The return value is an alist, keys may occur multiple times
  10090. if the property key was used several times.
  10091. POM may also be nil, in which case the current entry is used.
  10092. If WHICH is nil or `all', get all properties. If WHICH is
  10093. `special' or `standard', only get that subclass."
  10094. (setq which (or which 'all))
  10095. (org-with-point-at pom
  10096. (let ((clockstr (substring org-clock-string 0 -1))
  10097. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10098. beg end range props sum-props key value string clocksum)
  10099. (save-excursion
  10100. (when (condition-case nil
  10101. (and (org-mode-p) (org-back-to-heading t))
  10102. (error nil))
  10103. (setq beg (point))
  10104. (setq sum-props (get-text-property (point) 'org-summaries))
  10105. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10106. (outline-next-heading)
  10107. (setq end (point))
  10108. (when (memq which '(all special))
  10109. ;; Get the special properties, like TODO and tags
  10110. (goto-char beg)
  10111. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10112. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10113. (when (looking-at org-priority-regexp)
  10114. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10115. (when (and (setq value (org-get-tags-string))
  10116. (string-match "\\S-" value))
  10117. (push (cons "TAGS" value) props))
  10118. (when (setq value (org-get-tags-at))
  10119. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10120. props))
  10121. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10122. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10123. string (if (equal key clockstr)
  10124. (org-no-properties
  10125. (org-trim
  10126. (buffer-substring
  10127. (match-beginning 3) (goto-char (point-at-eol)))))
  10128. (substring (org-match-string-no-properties 3) 1 -1)))
  10129. (unless key
  10130. (if (= (char-after (match-beginning 3)) ?\[)
  10131. (setq key "TIMESTAMP_IA")
  10132. (setq key "TIMESTAMP")))
  10133. (when (or (equal key clockstr) (not (assoc key props)))
  10134. (push (cons key string) props)))
  10135. )
  10136. (when (memq which '(all standard))
  10137. ;; Get the standard properties, like :PROP: ...
  10138. (setq range (org-get-property-block beg end))
  10139. (when range
  10140. (goto-char (car range))
  10141. (while (re-search-forward
  10142. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10143. (cdr range) t)
  10144. (setq key (org-match-string-no-properties 1)
  10145. value (org-trim (or (org-match-string-no-properties 2) "")))
  10146. (unless (member key excluded)
  10147. (push (cons key (or value "")) props)))))
  10148. (if clocksum
  10149. (push (cons "CLOCKSUM"
  10150. (org-columns-number-to-string (/ (float clocksum) 60.)
  10151. 'add_times))
  10152. props))
  10153. (unless (assoc "CATEGORY" props)
  10154. (setq value (or (org-get-category)
  10155. (progn (org-refresh-category-properties)
  10156. (org-get-category))))
  10157. (push (cons "CATEGORY" value) props))
  10158. (append sum-props (nreverse props)))))))
  10159. (defun org-entry-get (pom property &optional inherit)
  10160. "Get value of PROPERTY for entry at point-or-marker POM.
  10161. If INHERIT is non-nil and the entry does not have the property,
  10162. then also check higher levels of the hierarchy.
  10163. If INHERIT is the symbol `selective', use inheritance only if the setting
  10164. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10165. If the property is present but empty, the return value is the empty string.
  10166. If the property is not present at all, nil is returned."
  10167. (org-with-point-at pom
  10168. (if (and inherit (if (eq inherit 'selective)
  10169. (org-property-inherit-p property)
  10170. t))
  10171. (org-entry-get-with-inheritance property)
  10172. (if (member property org-special-properties)
  10173. ;; We need a special property. Use brute force, get all properties.
  10174. (cdr (assoc property (org-entry-properties nil 'special)))
  10175. (let ((range (org-get-property-block)))
  10176. (if (and range
  10177. (goto-char (car range))
  10178. (re-search-forward
  10179. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10180. (cdr range) t))
  10181. ;; Found the property, return it.
  10182. (if (match-end 1)
  10183. (org-match-string-no-properties 1)
  10184. "")))))))
  10185. (defun org-property-or-variable-value (var &optional inherit)
  10186. "Check if there is a property fixing the value of VAR.
  10187. If yes, return this value. If not, return the current value of the variable."
  10188. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10189. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10190. (read prop)
  10191. (symbol-value var))))
  10192. (defun org-entry-delete (pom property)
  10193. "Delete the property PROPERTY from entry at point-or-marker POM."
  10194. (org-with-point-at pom
  10195. (if (member property org-special-properties)
  10196. nil ; cannot delete these properties.
  10197. (let ((range (org-get-property-block)))
  10198. (if (and range
  10199. (goto-char (car range))
  10200. (re-search-forward
  10201. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10202. (cdr range) t))
  10203. (progn
  10204. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10205. t)
  10206. nil)))))
  10207. ;; Multi-values properties are properties that contain multiple values
  10208. ;; These values are assumed to be single words, separated by whitespace.
  10209. (defun org-entry-add-to-multivalued-property (pom property value)
  10210. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10211. (let* ((old (org-entry-get pom property))
  10212. (values (and old (org-split-string old "[ \t]"))))
  10213. (setq value (org-entry-protect-space value))
  10214. (unless (member value values)
  10215. (setq values (cons value values))
  10216. (org-entry-put pom property
  10217. (mapconcat 'identity values " ")))))
  10218. (defun org-entry-remove-from-multivalued-property (pom property value)
  10219. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10220. (let* ((old (org-entry-get pom property))
  10221. (values (and old (org-split-string old "[ \t]"))))
  10222. (setq value (org-entry-protect-space value))
  10223. (when (member value values)
  10224. (setq values (delete value values))
  10225. (org-entry-put pom property
  10226. (mapconcat 'identity values " ")))))
  10227. (defun org-entry-member-in-multivalued-property (pom property value)
  10228. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10229. (let* ((old (org-entry-get pom property))
  10230. (values (and old (org-split-string old "[ \t]"))))
  10231. (setq value (org-entry-protect-space value))
  10232. (member value values)))
  10233. (defun org-entry-get-multivalued-property (pom property)
  10234. "Return a list of values in a multivalued property."
  10235. (let* ((value (org-entry-get pom property))
  10236. (values (and value (org-split-string value "[ \t]"))))
  10237. (mapcar 'org-entry-restore-space values)))
  10238. (defun org-entry-put-multivalued-property (pom property &rest values)
  10239. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10240. VALUES should be a list of strings. Spaces will be protected."
  10241. (org-entry-put pom property
  10242. (mapconcat 'org-entry-protect-space values " "))
  10243. (let* ((value (org-entry-get pom property))
  10244. (values (and value (org-split-string value "[ \t]"))))
  10245. (mapcar 'org-entry-restore-space values)))
  10246. (defun org-entry-protect-space (s)
  10247. "Protect spaces and newline in string S."
  10248. (while (string-match " " s)
  10249. (setq s (replace-match "%20" t t s)))
  10250. (while (string-match "\n" s)
  10251. (setq s (replace-match "%0A" t t s)))
  10252. s)
  10253. (defun org-entry-restore-space (s)
  10254. "Restore spaces and newline in string S."
  10255. (while (string-match "%20" s)
  10256. (setq s (replace-match " " t t s)))
  10257. (while (string-match "%0A" s)
  10258. (setq s (replace-match "\n" t t s)))
  10259. s)
  10260. (defvar org-entry-property-inherited-from (make-marker)
  10261. "Marker pointing to the entry from where a property was inherited.
  10262. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10263. location of the entry where the inheritance search matched. If there was
  10264. no match, the marker will point nowhere.
  10265. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10266. is set.")
  10267. (defun org-entry-get-with-inheritance (property)
  10268. "Get entry property, and search higher levels if not present."
  10269. (move-marker org-entry-property-inherited-from nil)
  10270. (let (tmp)
  10271. (save-excursion
  10272. (save-restriction
  10273. (widen)
  10274. (catch 'ex
  10275. (while t
  10276. (when (setq tmp (org-entry-get nil property))
  10277. (org-back-to-heading t)
  10278. (move-marker org-entry-property-inherited-from (point))
  10279. (throw 'ex tmp))
  10280. (or (org-up-heading-safe) (throw 'ex nil)))))
  10281. (or tmp
  10282. (cdr (assoc property org-file-properties))
  10283. (cdr (assoc property org-global-properties))
  10284. (cdr (assoc property org-global-properties-fixed))))))
  10285. (defun org-entry-put (pom property value)
  10286. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10287. (org-with-point-at pom
  10288. (org-back-to-heading t)
  10289. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10290. range)
  10291. (cond
  10292. ((equal property "TODO")
  10293. (when (and (stringp value) (string-match "\\S-" value)
  10294. (not (member value org-todo-keywords-1)))
  10295. (error "\"%s\" is not a valid TODO state" value))
  10296. (if (or (not value)
  10297. (not (string-match "\\S-" value)))
  10298. (setq value 'none))
  10299. (org-todo value)
  10300. (org-set-tags nil 'align))
  10301. ((equal property "PRIORITY")
  10302. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10303. (string-to-char value) ?\ ))
  10304. (org-set-tags nil 'align))
  10305. ((equal property "SCHEDULED")
  10306. (if (re-search-forward org-scheduled-time-regexp end t)
  10307. (cond
  10308. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10309. ((eq value 'later) (org-timestamp-change 1 'day))
  10310. (t (call-interactively 'org-schedule)))
  10311. (call-interactively 'org-schedule)))
  10312. ((equal property "DEADLINE")
  10313. (if (re-search-forward org-deadline-time-regexp end t)
  10314. (cond
  10315. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10316. ((eq value 'later) (org-timestamp-change 1 'day))
  10317. (t (call-interactively 'org-deadline)))
  10318. (call-interactively 'org-deadline)))
  10319. ((member property org-special-properties)
  10320. (error "The %s property can not yet be set with `org-entry-put'"
  10321. property))
  10322. (t ; a non-special property
  10323. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10324. (setq range (org-get-property-block beg end 'force))
  10325. (goto-char (car range))
  10326. (if (re-search-forward
  10327. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10328. (progn
  10329. (delete-region (match-beginning 1) (match-end 1))
  10330. (goto-char (match-beginning 1)))
  10331. (goto-char (cdr range))
  10332. (insert "\n")
  10333. (backward-char 1)
  10334. (org-indent-line-function)
  10335. (insert ":" property ":"))
  10336. (and value (insert " " value))
  10337. (org-indent-line-function)))))))
  10338. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10339. "Get all property keys in the current buffer.
  10340. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10341. like tags and TODO state.
  10342. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10343. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10344. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10345. formats in the current buffer."
  10346. (let (rtn range cfmt s p)
  10347. (save-excursion
  10348. (save-restriction
  10349. (widen)
  10350. (goto-char (point-min))
  10351. (while (re-search-forward org-property-start-re nil t)
  10352. (setq range (org-get-property-block))
  10353. (goto-char (car range))
  10354. (while (re-search-forward
  10355. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10356. (cdr range) t)
  10357. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10358. (outline-next-heading))))
  10359. (when include-specials
  10360. (setq rtn (append org-special-properties rtn)))
  10361. (when include-defaults
  10362. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10363. (when include-columns
  10364. (save-excursion
  10365. (save-restriction
  10366. (widen)
  10367. (goto-char (point-min))
  10368. (while (re-search-forward
  10369. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10370. nil t)
  10371. (setq cfmt (match-string 2) s 0)
  10372. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10373. cfmt s)
  10374. (setq s (match-end 0)
  10375. p (match-string 1 cfmt))
  10376. (unless (or (equal p "ITEM")
  10377. (member p org-special-properties))
  10378. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10379. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10380. (defun org-property-values (key)
  10381. "Return a list of all values of property KEY."
  10382. (save-excursion
  10383. (save-restriction
  10384. (widen)
  10385. (goto-char (point-min))
  10386. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10387. values)
  10388. (while (re-search-forward re nil t)
  10389. (add-to-list 'values (org-trim (match-string 1))))
  10390. (delete "" values)))))
  10391. (defun org-insert-property-drawer ()
  10392. "Insert a property drawer into the current entry."
  10393. (interactive)
  10394. (org-back-to-heading t)
  10395. (looking-at outline-regexp)
  10396. (let ((indent (- (match-end 0)(match-beginning 0)))
  10397. (beg (point))
  10398. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10399. end hiddenp)
  10400. (outline-next-heading)
  10401. (setq end (point))
  10402. (goto-char beg)
  10403. (while (re-search-forward re end t))
  10404. (setq hiddenp (org-invisible-p))
  10405. (end-of-line 1)
  10406. (and (equal (char-after) ?\n) (forward-char 1))
  10407. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10408. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10409. ;; just skip this line
  10410. (beginning-of-line 2)
  10411. ;; Drawer start, find the end
  10412. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10413. (beginning-of-line 1)))
  10414. (org-skip-over-state-notes)
  10415. (skip-chars-backward " \t\n\r")
  10416. (if (eq (char-before) ?*) (forward-char 1))
  10417. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10418. (beginning-of-line 0)
  10419. (org-indent-to-column indent)
  10420. (beginning-of-line 2)
  10421. (org-indent-to-column indent)
  10422. (beginning-of-line 0)
  10423. (if hiddenp
  10424. (save-excursion
  10425. (org-back-to-heading t)
  10426. (hide-entry))
  10427. (org-flag-drawer t))))
  10428. (defun org-set-property (property value)
  10429. "In the current entry, set PROPERTY to VALUE.
  10430. When called interactively, this will prompt for a property name, offering
  10431. completion on existing and default properties. And then it will prompt
  10432. for a value, offering completion either on allowed values (via an inherited
  10433. xxx_ALL property) or on existing values in other instances of this property
  10434. in the current file."
  10435. (interactive
  10436. (let* ((completion-ignore-case t)
  10437. (keys (org-buffer-property-keys nil t t))
  10438. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10439. (prop (if (member prop0 keys)
  10440. prop0
  10441. (or (cdr (assoc (downcase prop0)
  10442. (mapcar (lambda (x) (cons (downcase x) x))
  10443. keys)))
  10444. prop0)))
  10445. (cur (org-entry-get nil prop))
  10446. (allowed (org-property-get-allowed-values nil prop 'table))
  10447. (existing (mapcar 'list (org-property-values prop)))
  10448. (val (if allowed
  10449. (org-completing-read "Value: " allowed nil 'req-match)
  10450. (let (org-completion-use-ido)
  10451. (org-completing-read
  10452. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10453. (concat "[" cur "]") "")
  10454. ": ")
  10455. existing nil nil "" nil cur)))))
  10456. (list prop (if (equal val "") cur val))))
  10457. (unless (equal (org-entry-get nil property) value)
  10458. (org-entry-put nil property value)))
  10459. (defun org-delete-property (property)
  10460. "In the current entry, delete PROPERTY."
  10461. (interactive
  10462. (let* ((completion-ignore-case t)
  10463. (prop (org-ido-completing-read
  10464. "Property: " (org-entry-properties nil 'standard))))
  10465. (list prop)))
  10466. (message "Property %s %s" property
  10467. (if (org-entry-delete nil property)
  10468. "deleted"
  10469. "was not present in the entry")))
  10470. (defun org-delete-property-globally (property)
  10471. "Remove PROPERTY globally, from all entries."
  10472. (interactive
  10473. (let* ((completion-ignore-case t)
  10474. (prop (org-ido-completing-read
  10475. "Globally remove property: "
  10476. (mapcar 'list (org-buffer-property-keys)))))
  10477. (list prop)))
  10478. (save-excursion
  10479. (save-restriction
  10480. (widen)
  10481. (goto-char (point-min))
  10482. (let ((cnt 0))
  10483. (while (re-search-forward
  10484. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10485. nil t)
  10486. (setq cnt (1+ cnt))
  10487. (replace-match ""))
  10488. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10489. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10490. (defun org-compute-property-at-point ()
  10491. "Compute the property at point.
  10492. This looks for an enclosing column format, extracts the operator and
  10493. then applies it to the property in the column format's scope."
  10494. (interactive)
  10495. (unless (org-at-property-p)
  10496. (error "Not at a property"))
  10497. (let ((prop (org-match-string-no-properties 2)))
  10498. (org-columns-get-format-and-top-level)
  10499. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10500. (error "No operator defined for property %s" prop))
  10501. (org-columns-compute prop)))
  10502. (defun org-property-get-allowed-values (pom property &optional table)
  10503. "Get allowed values for the property PROPERTY.
  10504. When TABLE is non-nil, return an alist that can directly be used for
  10505. completion."
  10506. (let (vals)
  10507. (cond
  10508. ((equal property "TODO")
  10509. (setq vals (org-with-point-at pom
  10510. (append org-todo-keywords-1 '("")))))
  10511. ((equal property "PRIORITY")
  10512. (let ((n org-lowest-priority))
  10513. (while (>= n org-highest-priority)
  10514. (push (char-to-string n) vals)
  10515. (setq n (1- n)))))
  10516. ((member property org-special-properties))
  10517. (t
  10518. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10519. (when (and vals (string-match "\\S-" vals))
  10520. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10521. (setq vals (mapcar (lambda (x)
  10522. (cond ((stringp x) x)
  10523. ((numberp x) (number-to-string x))
  10524. ((symbolp x) (symbol-name x))
  10525. (t "???")))
  10526. vals)))))
  10527. (if table (mapcar 'list vals) vals)))
  10528. (defun org-property-previous-allowed-value (&optional previous)
  10529. "Switch to the next allowed value for this property."
  10530. (interactive)
  10531. (org-property-next-allowed-value t))
  10532. (defun org-property-next-allowed-value (&optional previous)
  10533. "Switch to the next allowed value for this property."
  10534. (interactive)
  10535. (unless (org-at-property-p)
  10536. (error "Not at a property"))
  10537. (let* ((key (match-string 2))
  10538. (value (match-string 3))
  10539. (allowed (or (org-property-get-allowed-values (point) key)
  10540. (and (member value '("[ ]" "[-]" "[X]"))
  10541. '("[ ]" "[X]"))))
  10542. nval)
  10543. (unless allowed
  10544. (error "Allowed values for this property have not been defined"))
  10545. (if previous (setq allowed (reverse allowed)))
  10546. (if (member value allowed)
  10547. (setq nval (car (cdr (member value allowed)))))
  10548. (setq nval (or nval (car allowed)))
  10549. (if (equal nval value)
  10550. (error "Only one allowed value for this property"))
  10551. (org-at-property-p)
  10552. (replace-match (concat " :" key ": " nval) t t)
  10553. (org-indent-line-function)
  10554. (beginning-of-line 1)
  10555. (skip-chars-forward " \t")))
  10556. (defun org-find-entry-with-id (ident)
  10557. "Locate the entry that contains the ID property with exact value IDENT.
  10558. IDENT can be a string, a symbol or a number, this function will search for
  10559. the string representation of it.
  10560. Return the position where this entry starts, or nil if there is no such entry."
  10561. (interactive "sID: ")
  10562. (let ((id (cond
  10563. ((stringp ident) ident)
  10564. ((symbol-name ident) (symbol-name ident))
  10565. ((numberp ident) (number-to-string ident))
  10566. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10567. (case-fold-search nil))
  10568. (save-excursion
  10569. (save-restriction
  10570. (widen)
  10571. (goto-char (point-min))
  10572. (when (re-search-forward
  10573. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10574. nil t)
  10575. (org-back-to-heading)
  10576. (point))))))
  10577. ;;;; Timestamps
  10578. (defvar org-last-changed-timestamp nil)
  10579. (defvar org-last-inserted-timestamp nil
  10580. "The last time stamp inserted with `org-insert-time-stamp'.")
  10581. (defvar org-time-was-given) ; dynamically scoped parameter
  10582. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10583. (defvar org-ts-what) ; dynamically scoped parameter
  10584. (defun org-time-stamp (arg &optional inactive)
  10585. "Prompt for a date/time and insert a time stamp.
  10586. If the user specifies a time like HH:MM, or if this command is called
  10587. with a prefix argument, the time stamp will contain date and time.
  10588. Otherwise, only the date will be included. All parts of a date not
  10589. specified by the user will be filled in from the current date/time.
  10590. So if you press just return without typing anything, the time stamp
  10591. will represent the current date/time. If there is already a timestamp
  10592. at the cursor, it will be modified."
  10593. (interactive "P")
  10594. (let* ((ts nil)
  10595. (default-time
  10596. ;; Default time is either today, or, when entering a range,
  10597. ;; the range start.
  10598. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10599. (save-excursion
  10600. (re-search-backward
  10601. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10602. (- (point) 20) t)))
  10603. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10604. (current-time)))
  10605. (default-input (and ts (org-get-compact-tod ts)))
  10606. org-time-was-given org-end-time-was-given time)
  10607. (cond
  10608. ((and (org-at-timestamp-p t)
  10609. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10610. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10611. (insert "--")
  10612. (setq time (let ((this-command this-command))
  10613. (org-read-date arg 'totime nil nil
  10614. default-time default-input)))
  10615. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10616. ((org-at-timestamp-p t)
  10617. (setq time (let ((this-command this-command))
  10618. (org-read-date arg 'totime nil nil default-time default-input)))
  10619. (when (org-at-timestamp-p t) ; just to get the match data
  10620. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10621. (replace-match "")
  10622. (setq org-last-changed-timestamp
  10623. (org-insert-time-stamp
  10624. time (or org-time-was-given arg)
  10625. inactive nil nil (list org-end-time-was-given))))
  10626. (message "Timestamp updated"))
  10627. (t
  10628. (setq time (let ((this-command this-command))
  10629. (org-read-date arg 'totime nil nil default-time default-input)))
  10630. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10631. nil nil (list org-end-time-was-given))))))
  10632. ;; FIXME: can we use this for something else, like computing time differences?
  10633. (defun org-get-compact-tod (s)
  10634. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10635. (let* ((t1 (match-string 1 s))
  10636. (h1 (string-to-number (match-string 2 s)))
  10637. (m1 (string-to-number (match-string 3 s)))
  10638. (t2 (and (match-end 4) (match-string 5 s)))
  10639. (h2 (and t2 (string-to-number (match-string 6 s))))
  10640. (m2 (and t2 (string-to-number (match-string 7 s))))
  10641. dh dm)
  10642. (if (not t2)
  10643. t1
  10644. (setq dh (- h2 h1) dm (- m2 m1))
  10645. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10646. (concat t1 "+" (number-to-string dh)
  10647. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10648. (defun org-time-stamp-inactive (&optional arg)
  10649. "Insert an inactive time stamp.
  10650. An inactive time stamp is enclosed in square brackets instead of angle
  10651. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10652. does not link to the calendar and cannot be changed with the S-cursor keys.
  10653. So these are more for recording a certain time/date."
  10654. (interactive "P")
  10655. (org-time-stamp arg 'inactive))
  10656. (defvar org-date-ovl (org-make-overlay 1 1))
  10657. (org-overlay-put org-date-ovl 'face 'org-warning)
  10658. (org-detach-overlay org-date-ovl)
  10659. (defvar org-ans1) ; dynamically scoped parameter
  10660. (defvar org-ans2) ; dynamically scoped parameter
  10661. (defvar org-plain-time-of-day-regexp) ; defined below
  10662. (defvar org-overriding-default-time nil) ; dynamically scoped
  10663. (defvar org-read-date-overlay nil)
  10664. (defvar org-dcst nil) ; dynamically scoped
  10665. (defvar org-read-date-history nil)
  10666. (defun org-read-date (&optional with-time to-time from-string prompt
  10667. default-time default-input)
  10668. "Read a date, possibly a time, and make things smooth for the user.
  10669. The prompt will suggest to enter an ISO date, but you can also enter anything
  10670. which will at least partially be understood by `parse-time-string'.
  10671. Unrecognized parts of the date will default to the current day, month, year,
  10672. hour and minute. If this command is called to replace a timestamp at point,
  10673. of to enter the second timestamp of a range, the default time is taken from the
  10674. existing stamp. For example,
  10675. 3-2-5 --> 2003-02-05
  10676. feb 15 --> currentyear-02-15
  10677. sep 12 9 --> 2009-09-12
  10678. 12:45 --> today 12:45
  10679. 22 sept 0:34 --> currentyear-09-22 0:34
  10680. 12 --> currentyear-currentmonth-12
  10681. Fri --> nearest Friday (today or later)
  10682. etc.
  10683. Furthermore you can specify a relative date by giving, as the *first* thing
  10684. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10685. change in days weeks, months, years.
  10686. With a single plus or minus, the date is relative to today. With a double
  10687. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10688. +4d --> four days from today
  10689. +4 --> same as above
  10690. +2w --> two weeks from today
  10691. ++5 --> five days from default date
  10692. The function understands only English month and weekday abbreviations,
  10693. but this can be configured with the variables `parse-time-months' and
  10694. `parse-time-weekdays'.
  10695. While prompting, a calendar is popped up - you can also select the
  10696. date with the mouse (button 1). The calendar shows a period of three
  10697. months. To scroll it to other months, use the keys `>' and `<'.
  10698. If you don't like the calendar, turn it off with
  10699. \(setq org-read-date-popup-calendar nil)
  10700. With optional argument TO-TIME, the date will immediately be converted
  10701. to an internal time.
  10702. With an optional argument WITH-TIME, the prompt will suggest to also
  10703. insert a time. Note that when WITH-TIME is not set, you can still
  10704. enter a time, and this function will inform the calling routine about
  10705. this change. The calling routine may then choose to change the format
  10706. used to insert the time stamp into the buffer to include the time.
  10707. With optional argument FROM-STRING, read from this string instead from
  10708. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10709. the time/date that is used for everything that is not specified by the
  10710. user."
  10711. (require 'parse-time)
  10712. (let* ((org-time-stamp-rounding-minutes
  10713. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10714. (org-dcst org-display-custom-times)
  10715. (ct (org-current-time))
  10716. (def (or org-overriding-default-time default-time ct))
  10717. (defdecode (decode-time def))
  10718. (dummy (progn
  10719. (when (< (nth 2 defdecode) org-extend-today-until)
  10720. (setcar (nthcdr 2 defdecode) -1)
  10721. (setcar (nthcdr 1 defdecode) 59)
  10722. (setq def (apply 'encode-time defdecode)
  10723. defdecode (decode-time def)))))
  10724. (calendar-move-hook nil)
  10725. (calendar-view-diary-initially-flag nil)
  10726. (view-diary-entries-initially nil)
  10727. (calendar-view-holidays-initially-flag nil)
  10728. (view-calendar-holidays-initially nil)
  10729. (timestr (format-time-string
  10730. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10731. (prompt (concat (if prompt (concat prompt " ") "")
  10732. (format "Date+time [%s]: " timestr)))
  10733. ans (org-ans0 "") org-ans1 org-ans2 final)
  10734. (cond
  10735. (from-string (setq ans from-string))
  10736. (org-read-date-popup-calendar
  10737. (save-excursion
  10738. (save-window-excursion
  10739. (calendar)
  10740. (calendar-forward-day (- (time-to-days def)
  10741. (calendar-absolute-from-gregorian
  10742. (calendar-current-date))))
  10743. (org-eval-in-calendar nil t)
  10744. (let* ((old-map (current-local-map))
  10745. (map (copy-keymap calendar-mode-map))
  10746. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10747. (org-defkey map (kbd "RET") 'org-calendar-select)
  10748. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10749. 'org-calendar-select-mouse)
  10750. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10751. 'org-calendar-select-mouse)
  10752. (org-defkey minibuffer-local-map [(meta shift left)]
  10753. (lambda () (interactive)
  10754. (org-eval-in-calendar '(calendar-backward-month 1))))
  10755. (org-defkey minibuffer-local-map [(meta shift right)]
  10756. (lambda () (interactive)
  10757. (org-eval-in-calendar '(calendar-forward-month 1))))
  10758. (org-defkey minibuffer-local-map [(meta shift up)]
  10759. (lambda () (interactive)
  10760. (org-eval-in-calendar '(calendar-backward-year 1))))
  10761. (org-defkey minibuffer-local-map [(meta shift down)]
  10762. (lambda () (interactive)
  10763. (org-eval-in-calendar '(calendar-forward-year 1))))
  10764. (org-defkey minibuffer-local-map [(shift up)]
  10765. (lambda () (interactive)
  10766. (org-eval-in-calendar '(calendar-backward-week 1))))
  10767. (org-defkey minibuffer-local-map [(shift down)]
  10768. (lambda () (interactive)
  10769. (org-eval-in-calendar '(calendar-forward-week 1))))
  10770. (org-defkey minibuffer-local-map [(shift left)]
  10771. (lambda () (interactive)
  10772. (org-eval-in-calendar '(calendar-backward-day 1))))
  10773. (org-defkey minibuffer-local-map [(shift right)]
  10774. (lambda () (interactive)
  10775. (org-eval-in-calendar '(calendar-forward-day 1))))
  10776. (org-defkey minibuffer-local-map ">"
  10777. (lambda () (interactive)
  10778. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10779. (org-defkey minibuffer-local-map "<"
  10780. (lambda () (interactive)
  10781. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10782. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10783. (unwind-protect
  10784. (progn
  10785. (use-local-map map)
  10786. (add-hook 'post-command-hook 'org-read-date-display)
  10787. (setq org-ans0 (read-string prompt default-input
  10788. 'org-read-date-history nil))
  10789. ;; org-ans0: from prompt
  10790. ;; org-ans1: from mouse click
  10791. ;; org-ans2: from calendar motion
  10792. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10793. (remove-hook 'post-command-hook 'org-read-date-display)
  10794. (use-local-map old-map)
  10795. (when org-read-date-overlay
  10796. (org-delete-overlay org-read-date-overlay)
  10797. (setq org-read-date-overlay nil)))))))
  10798. (t ; Naked prompt only
  10799. (unwind-protect
  10800. (setq ans (read-string prompt default-input
  10801. 'org-read-date-history timestr))
  10802. (when org-read-date-overlay
  10803. (org-delete-overlay org-read-date-overlay)
  10804. (setq org-read-date-overlay nil)))))
  10805. (setq final (org-read-date-analyze ans def defdecode))
  10806. (if to-time
  10807. (apply 'encode-time final)
  10808. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10809. (format "%04d-%02d-%02d %02d:%02d"
  10810. (nth 5 final) (nth 4 final) (nth 3 final)
  10811. (nth 2 final) (nth 1 final))
  10812. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10813. (defvar def)
  10814. (defvar defdecode)
  10815. (defvar with-time)
  10816. (defun org-read-date-display ()
  10817. "Display the current date prompt interpretation in the minibuffer."
  10818. (when org-read-date-display-live
  10819. (when org-read-date-overlay
  10820. (org-delete-overlay org-read-date-overlay))
  10821. (let ((p (point)))
  10822. (end-of-line 1)
  10823. (while (not (equal (buffer-substring
  10824. (max (point-min) (- (point) 4)) (point))
  10825. " "))
  10826. (insert " "))
  10827. (goto-char p))
  10828. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10829. " " (or org-ans1 org-ans2)))
  10830. (org-end-time-was-given nil)
  10831. (f (org-read-date-analyze ans def defdecode))
  10832. (fmts (if org-dcst
  10833. org-time-stamp-custom-formats
  10834. org-time-stamp-formats))
  10835. (fmt (if (or with-time
  10836. (and (boundp 'org-time-was-given) org-time-was-given))
  10837. (cdr fmts)
  10838. (car fmts)))
  10839. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10840. (when (and org-end-time-was-given
  10841. (string-match org-plain-time-of-day-regexp txt))
  10842. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10843. org-end-time-was-given
  10844. (substring txt (match-end 0)))))
  10845. (setq org-read-date-overlay
  10846. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10847. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10848. (defun org-read-date-analyze (ans def defdecode)
  10849. "Analyse the combined answer of the date prompt."
  10850. ;; FIXME: cleanup and comment
  10851. (let (delta deltan deltaw deltadef year month day
  10852. hour minute second wday pm h2 m2 tl wday1
  10853. iso-year iso-weekday iso-week iso-year iso-date)
  10854. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10855. (setq ans "+0"))
  10856. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10857. (setq ans (replace-match "" t t ans)
  10858. deltan (car delta)
  10859. deltaw (nth 1 delta)
  10860. deltadef (nth 2 delta)))
  10861. ;; Check if there is an iso week date in there
  10862. ;; If yes, sore the info and postpone interpreting it until the rest
  10863. ;; of the parsing is done
  10864. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10865. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10866. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10867. iso-week (string-to-number (match-string 2 ans)))
  10868. (setq ans (replace-match "" t t ans)))
  10869. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10870. (when (string-match
  10871. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10872. (setq year (if (match-end 2)
  10873. (string-to-number (match-string 2 ans))
  10874. (string-to-number (format-time-string "%Y")))
  10875. month (string-to-number (match-string 3 ans))
  10876. day (string-to-number (match-string 4 ans)))
  10877. (if (< year 100) (setq year (+ 2000 year)))
  10878. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10879. t nil ans)))
  10880. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10881. ;; If there is a time with am/pm, and *no* time without it, we convert
  10882. ;; so that matching will be successful.
  10883. (loop for i from 1 to 2 do ; twice, for end time as well
  10884. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10885. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10886. (setq hour (string-to-number (match-string 1 ans))
  10887. minute (if (match-end 3)
  10888. (string-to-number (match-string 3 ans))
  10889. 0)
  10890. pm (equal ?p
  10891. (string-to-char (downcase (match-string 4 ans)))))
  10892. (if (and (= hour 12) (not pm))
  10893. (setq hour 0)
  10894. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10895. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10896. t t ans))))
  10897. ;; Check if a time range is given as a duration
  10898. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10899. (setq hour (string-to-number (match-string 1 ans))
  10900. h2 (+ hour (string-to-number (match-string 3 ans)))
  10901. minute (string-to-number (match-string 2 ans))
  10902. m2 (+ minute (if (match-end 5) (string-to-number
  10903. (match-string 5 ans))0)))
  10904. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10905. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10906. t t ans)))
  10907. ;; Check if there is a time range
  10908. (when (boundp 'org-end-time-was-given)
  10909. (setq org-time-was-given nil)
  10910. (when (and (string-match org-plain-time-of-day-regexp ans)
  10911. (match-end 8))
  10912. (setq org-end-time-was-given (match-string 8 ans))
  10913. (setq ans (concat (substring ans 0 (match-beginning 7))
  10914. (substring ans (match-end 7))))))
  10915. (setq tl (parse-time-string ans)
  10916. day (or (nth 3 tl) (nth 3 defdecode))
  10917. month (or (nth 4 tl)
  10918. (if (and org-read-date-prefer-future
  10919. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10920. (1+ (nth 4 defdecode))
  10921. (nth 4 defdecode)))
  10922. year (or (nth 5 tl)
  10923. (if (and org-read-date-prefer-future
  10924. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10925. (1+ (nth 5 defdecode))
  10926. (nth 5 defdecode)))
  10927. hour (or (nth 2 tl) (nth 2 defdecode))
  10928. minute (or (nth 1 tl) (nth 1 defdecode))
  10929. second (or (nth 0 tl) 0)
  10930. wday (nth 6 tl))
  10931. ;; Special date definitions below
  10932. (cond
  10933. (iso-week
  10934. ;; There was an iso week
  10935. (setq year (or iso-year year)
  10936. day (or iso-weekday wday 1)
  10937. wday nil ; to make sure that the trigger below does not match
  10938. iso-date (calendar-gregorian-from-absolute
  10939. (calendar-absolute-from-iso
  10940. (list iso-week day year))))
  10941. ; FIXME: Should we also push ISO weeks into the future?
  10942. ; (when (and org-read-date-prefer-future
  10943. ; (not iso-year)
  10944. ; (< (calendar-absolute-from-gregorian iso-date)
  10945. ; (time-to-days (current-time))))
  10946. ; (setq year (1+ year)
  10947. ; iso-date (calendar-gregorian-from-absolute
  10948. ; (calendar-absolute-from-iso
  10949. ; (list iso-week day year)))))
  10950. (setq month (car iso-date)
  10951. year (nth 2 iso-date)
  10952. day (nth 1 iso-date)))
  10953. (deltan
  10954. (unless deltadef
  10955. (let ((now (decode-time (current-time))))
  10956. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10957. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10958. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10959. ((equal deltaw "m") (setq month (+ month deltan)))
  10960. ((equal deltaw "y") (setq year (+ year deltan)))))
  10961. ((and wday (not (nth 3 tl)))
  10962. ;; Weekday was given, but no day, so pick that day in the week
  10963. ;; on or after the derived date.
  10964. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10965. (unless (equal wday wday1)
  10966. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10967. (if (and (boundp 'org-time-was-given)
  10968. (nth 2 tl))
  10969. (setq org-time-was-given t))
  10970. (if (< year 100) (setq year (+ 2000 year)))
  10971. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10972. (list second minute hour day month year)))
  10973. (defvar parse-time-weekdays)
  10974. (defun org-read-date-get-relative (s today default)
  10975. "Check string S for special relative date string.
  10976. TODAY and DEFAULT are internal times, for today and for a default.
  10977. Return shift list (N what def-flag)
  10978. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10979. N is the number of WHATs to shift.
  10980. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10981. the DEFAULT date rather than TODAY."
  10982. (when (and
  10983. (string-match
  10984. (concat
  10985. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10986. "\\([0-9]+\\)?"
  10987. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10988. "\\([ \t]\\|$\\)") s)
  10989. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10990. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10991. (string-to-char (substring (match-string 1 s) -1))
  10992. ?+))
  10993. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10994. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10995. (what (if (match-end 3) (match-string 3 s) "d"))
  10996. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10997. (date (if rel default today))
  10998. (wday (nth 6 (decode-time date)))
  10999. delta)
  11000. (if wday1
  11001. (progn
  11002. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11003. (if (= dir ?-) (setq delta (- delta 7)))
  11004. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11005. (list delta "d" rel))
  11006. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11007. (defun org-eval-in-calendar (form &optional keepdate)
  11008. "Eval FORM in the calendar window and return to current window.
  11009. Also, store the cursor date in variable org-ans2."
  11010. (let ((sw (selected-window)))
  11011. (select-window (get-buffer-window "*Calendar*"))
  11012. (eval form)
  11013. (when (and (not keepdate) (calendar-cursor-to-date))
  11014. (let* ((date (calendar-cursor-to-date))
  11015. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11016. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11017. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11018. (select-window sw)))
  11019. (defun org-calendar-select ()
  11020. "Return to `org-read-date' with the date currently selected.
  11021. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11022. (interactive)
  11023. (when (calendar-cursor-to-date)
  11024. (let* ((date (calendar-cursor-to-date))
  11025. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11026. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11027. (if (active-minibuffer-window) (exit-minibuffer))))
  11028. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11029. "Insert a date stamp for the date given by the internal TIME.
  11030. WITH-HM means, use the stamp format that includes the time of the day.
  11031. INACTIVE means use square brackets instead of angular ones, so that the
  11032. stamp will not contribute to the agenda.
  11033. PRE and POST are optional strings to be inserted before and after the
  11034. stamp.
  11035. The command returns the inserted time stamp."
  11036. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11037. stamp)
  11038. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11039. (insert-before-markers (or pre ""))
  11040. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11041. (when (listp extra)
  11042. (setq extra (car extra))
  11043. (if (and (stringp extra)
  11044. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11045. (setq extra (format "-%02d:%02d"
  11046. (string-to-number (match-string 1 extra))
  11047. (string-to-number (match-string 2 extra))))
  11048. (setq extra nil)))
  11049. (when extra
  11050. (backward-char 1)
  11051. (insert-before-markers extra)
  11052. (forward-char 1))
  11053. (insert-before-markers (or post ""))
  11054. (setq org-last-inserted-timestamp stamp)))
  11055. (defun org-toggle-time-stamp-overlays ()
  11056. "Toggle the use of custom time stamp formats."
  11057. (interactive)
  11058. (setq org-display-custom-times (not org-display-custom-times))
  11059. (unless org-display-custom-times
  11060. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11061. (while (setq p (next-single-property-change p 'display))
  11062. (if (and (get-text-property p 'display)
  11063. (eq (get-text-property p 'face) 'org-date))
  11064. (remove-text-properties
  11065. p (setq p (next-single-property-change p 'display))
  11066. '(display t))))
  11067. (set-buffer-modified-p bmp)))
  11068. (if (featurep 'xemacs)
  11069. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11070. (org-restart-font-lock)
  11071. (setq org-table-may-need-update t)
  11072. (if org-display-custom-times
  11073. (message "Time stamps are overlayed with custom format")
  11074. (message "Time stamp overlays removed")))
  11075. (defun org-display-custom-time (beg end)
  11076. "Overlay modified time stamp format over timestamp between BEG and END."
  11077. (let* ((ts (buffer-substring beg end))
  11078. t1 w1 with-hm tf time str w2 (off 0))
  11079. (save-match-data
  11080. (setq t1 (org-parse-time-string ts t))
  11081. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11082. (setq off (- (match-end 0) (match-beginning 0)))))
  11083. (setq end (- end off))
  11084. (setq w1 (- end beg)
  11085. with-hm (and (nth 1 t1) (nth 2 t1))
  11086. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11087. time (org-fix-decoded-time t1)
  11088. str (org-add-props
  11089. (format-time-string
  11090. (substring tf 1 -1) (apply 'encode-time time))
  11091. nil 'mouse-face 'highlight)
  11092. w2 (length str))
  11093. (if (not (= w2 w1))
  11094. (add-text-properties (1+ beg) (+ 2 beg)
  11095. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11096. (if (featurep 'xemacs)
  11097. (progn
  11098. (put-text-property beg end 'invisible t)
  11099. (put-text-property beg end 'end-glyph (make-glyph str)))
  11100. (put-text-property beg end 'display str))))
  11101. (defun org-translate-time (string)
  11102. "Translate all timestamps in STRING to custom format.
  11103. But do this only if the variable `org-display-custom-times' is set."
  11104. (when org-display-custom-times
  11105. (save-match-data
  11106. (let* ((start 0)
  11107. (re org-ts-regexp-both)
  11108. t1 with-hm inactive tf time str beg end)
  11109. (while (setq start (string-match re string start))
  11110. (setq beg (match-beginning 0)
  11111. end (match-end 0)
  11112. t1 (save-match-data
  11113. (org-parse-time-string (substring string beg end) t))
  11114. with-hm (and (nth 1 t1) (nth 2 t1))
  11115. inactive (equal (substring string beg (1+ beg)) "[")
  11116. tf (funcall (if with-hm 'cdr 'car)
  11117. org-time-stamp-custom-formats)
  11118. time (org-fix-decoded-time t1)
  11119. str (format-time-string
  11120. (concat
  11121. (if inactive "[" "<") (substring tf 1 -1)
  11122. (if inactive "]" ">"))
  11123. (apply 'encode-time time))
  11124. string (replace-match str t t string)
  11125. start (+ start (length str)))))))
  11126. string)
  11127. (defun org-fix-decoded-time (time)
  11128. "Set 0 instead of nil for the first 6 elements of time.
  11129. Don't touch the rest."
  11130. (let ((n 0))
  11131. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11132. (defun org-days-to-time (timestamp-string)
  11133. "Difference between TIMESTAMP-STRING and now in days."
  11134. (- (time-to-days (org-time-string-to-time timestamp-string))
  11135. (time-to-days (current-time))))
  11136. (defun org-deadline-close (timestamp-string &optional ndays)
  11137. "Is the time in TIMESTAMP-STRING close to the current date?"
  11138. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11139. (and (< (org-days-to-time timestamp-string) ndays)
  11140. (not (org-entry-is-done-p))))
  11141. (defun org-get-wdays (ts)
  11142. "Get the deadline lead time appropriate for timestring TS."
  11143. (cond
  11144. ((<= org-deadline-warning-days 0)
  11145. ;; 0 or negative, enforce this value no matter what
  11146. (- org-deadline-warning-days))
  11147. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11148. ;; lead time is specified.
  11149. (floor (* (string-to-number (match-string 1 ts))
  11150. (cdr (assoc (match-string 2 ts)
  11151. '(("d" . 1) ("w" . 7)
  11152. ("m" . 30.4) ("y" . 365.25)))))))
  11153. ;; go for the default.
  11154. (t org-deadline-warning-days)))
  11155. (defun org-calendar-select-mouse (ev)
  11156. "Return to `org-read-date' with the date currently selected.
  11157. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11158. (interactive "e")
  11159. (mouse-set-point ev)
  11160. (when (calendar-cursor-to-date)
  11161. (let* ((date (calendar-cursor-to-date))
  11162. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11163. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11164. (if (active-minibuffer-window) (exit-minibuffer))))
  11165. (defun org-check-deadlines (ndays)
  11166. "Check if there are any deadlines due or past due.
  11167. A deadline is considered due if it happens within `org-deadline-warning-days'
  11168. days from today's date. If the deadline appears in an entry marked DONE,
  11169. it is not shown. The prefix arg NDAYS can be used to test that many
  11170. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11171. (interactive "P")
  11172. (let* ((org-warn-days
  11173. (cond
  11174. ((equal ndays '(4)) 100000)
  11175. (ndays (prefix-numeric-value ndays))
  11176. (t (abs org-deadline-warning-days))))
  11177. (case-fold-search nil)
  11178. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11179. (callback
  11180. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11181. (message "%d deadlines past-due or due within %d days"
  11182. (org-occur regexp nil callback)
  11183. org-warn-days)))
  11184. (defun org-check-before-date (date)
  11185. "Check if there are deadlines or scheduled entries before DATE."
  11186. (interactive (list (org-read-date)))
  11187. (let ((case-fold-search nil)
  11188. (regexp (concat "\\<\\(" org-deadline-string
  11189. "\\|" org-scheduled-string
  11190. "\\) *<\\([^>]+\\)>"))
  11191. (callback
  11192. (lambda () (time-less-p
  11193. (org-time-string-to-time (match-string 2))
  11194. (org-time-string-to-time date)))))
  11195. (message "%d entries before %s"
  11196. (org-occur regexp nil callback) date)))
  11197. (defun org-check-after-date (date)
  11198. "Check if there are deadlines or scheduled entries after DATE."
  11199. (interactive (list (org-read-date)))
  11200. (let ((case-fold-search nil)
  11201. (regexp (concat "\\<\\(" org-deadline-string
  11202. "\\|" org-scheduled-string
  11203. "\\) *<\\([^>]+\\)>"))
  11204. (callback
  11205. (lambda () (not
  11206. (time-less-p
  11207. (org-time-string-to-time (match-string 2))
  11208. (org-time-string-to-time date))))))
  11209. (message "%d entries after %s"
  11210. (org-occur regexp nil callback) date)))
  11211. (defun org-evaluate-time-range (&optional to-buffer)
  11212. "Evaluate a time range by computing the difference between start and end.
  11213. Normally the result is just printed in the echo area, but with prefix arg
  11214. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11215. If the time range is actually in a table, the result is inserted into the
  11216. next column.
  11217. For time difference computation, a year is assumed to be exactly 365
  11218. days in order to avoid rounding problems."
  11219. (interactive "P")
  11220. (or
  11221. (org-clock-update-time-maybe)
  11222. (save-excursion
  11223. (unless (org-at-date-range-p t)
  11224. (goto-char (point-at-bol))
  11225. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11226. (if (not (org-at-date-range-p t))
  11227. (error "Not at a time-stamp range, and none found in current line")))
  11228. (let* ((ts1 (match-string 1))
  11229. (ts2 (match-string 2))
  11230. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11231. (match-end (match-end 0))
  11232. (time1 (org-time-string-to-time ts1))
  11233. (time2 (org-time-string-to-time ts2))
  11234. (t1 (time-to-seconds time1))
  11235. (t2 (time-to-seconds time2))
  11236. (diff (abs (- t2 t1)))
  11237. (negative (< (- t2 t1) 0))
  11238. ;; (ys (floor (* 365 24 60 60)))
  11239. (ds (* 24 60 60))
  11240. (hs (* 60 60))
  11241. (fy "%dy %dd %02d:%02d")
  11242. (fy1 "%dy %dd")
  11243. (fd "%dd %02d:%02d")
  11244. (fd1 "%dd")
  11245. (fh "%02d:%02d")
  11246. y d h m align)
  11247. (if havetime
  11248. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11249. y 0
  11250. d (floor (/ diff ds)) diff (mod diff ds)
  11251. h (floor (/ diff hs)) diff (mod diff hs)
  11252. m (floor (/ diff 60)))
  11253. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11254. y 0
  11255. d (floor (+ (/ diff ds) 0.5))
  11256. h 0 m 0))
  11257. (if (not to-buffer)
  11258. (message "%s" (org-make-tdiff-string y d h m))
  11259. (if (org-at-table-p)
  11260. (progn
  11261. (goto-char match-end)
  11262. (setq align t)
  11263. (and (looking-at " *|") (goto-char (match-end 0))))
  11264. (goto-char match-end))
  11265. (if (looking-at
  11266. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11267. (replace-match ""))
  11268. (if negative (insert " -"))
  11269. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11270. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11271. (insert " " (format fh h m))))
  11272. (if align (org-table-align))
  11273. (message "Time difference inserted")))))
  11274. (defun org-make-tdiff-string (y d h m)
  11275. (let ((fmt "")
  11276. (l nil))
  11277. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11278. l (push y l)))
  11279. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11280. l (push d l)))
  11281. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11282. l (push h l)))
  11283. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11284. l (push m l)))
  11285. (apply 'format fmt (nreverse l))))
  11286. (defun org-time-string-to-time (s)
  11287. (apply 'encode-time (org-parse-time-string s)))
  11288. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11289. "Convert a time stamp to an absolute day number.
  11290. If there is a specifyer for a cyclic time stamp, get the closest date to
  11291. DAYNR.
  11292. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11293. (cond
  11294. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11295. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11296. daynr
  11297. (+ daynr 1000)))
  11298. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11299. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11300. (time-to-days (current-time))) (match-string 0 s)
  11301. prefer show-all))
  11302. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11303. (defun org-days-to-iso-week (days)
  11304. "Return the iso week number."
  11305. (require 'cal-iso)
  11306. (car (calendar-iso-from-absolute days)))
  11307. (defun org-small-year-to-year (year)
  11308. "Convert 2-digit years into 4-digit years.
  11309. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11310. The year 2000 cannot be abbreviated. Any year larger than 99
  11311. is returned unchanged."
  11312. (if (< year 38)
  11313. (setq year (+ 2000 year))
  11314. (if (< year 100)
  11315. (setq year (+ 1900 year))))
  11316. year)
  11317. (defun org-time-from-absolute (d)
  11318. "Return the time corresponding to date D.
  11319. D may be an absolute day number, or a calendar-type list (month day year)."
  11320. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11321. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11322. (defun org-calendar-holiday ()
  11323. "List of holidays, for Diary display in Org-mode."
  11324. (require 'holidays)
  11325. (let ((hl (funcall
  11326. (if (fboundp 'calendar-check-holidays)
  11327. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11328. (if hl (mapconcat 'identity hl "; "))))
  11329. (defun org-diary-sexp-entry (sexp entry date)
  11330. "Process a SEXP diary ENTRY for DATE."
  11331. (require 'diary-lib)
  11332. (let ((result (if calendar-debug-sexp
  11333. (let ((stack-trace-on-error t))
  11334. (eval (car (read-from-string sexp))))
  11335. (condition-case nil
  11336. (eval (car (read-from-string sexp)))
  11337. (error
  11338. (beep)
  11339. (message "Bad sexp at line %d in %s: %s"
  11340. (org-current-line)
  11341. (buffer-file-name) sexp)
  11342. (sleep-for 2))))))
  11343. (cond ((stringp result) result)
  11344. ((and (consp result)
  11345. (stringp (cdr result))) (cdr result))
  11346. (result entry)
  11347. (t nil))))
  11348. (defun org-diary-to-ical-string (frombuf)
  11349. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11350. This uses the icalendar.el library."
  11351. (let* ((tmpdir (if (featurep 'xemacs)
  11352. (temp-directory)
  11353. temporary-file-directory))
  11354. (tmpfile (make-temp-name
  11355. (expand-file-name "orgics" tmpdir)))
  11356. buf rtn b e)
  11357. (save-excursion
  11358. (set-buffer frombuf)
  11359. (icalendar-export-region (point-min) (point-max) tmpfile)
  11360. (setq buf (find-buffer-visiting tmpfile))
  11361. (set-buffer buf)
  11362. (goto-char (point-min))
  11363. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11364. (setq b (match-beginning 0)))
  11365. (goto-char (point-max))
  11366. (if (re-search-backward "^END:VEVENT" nil t)
  11367. (setq e (match-end 0)))
  11368. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11369. (kill-buffer buf)
  11370. (delete-file tmpfile)
  11371. rtn))
  11372. (defun org-closest-date (start current change prefer show-all)
  11373. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11374. When PREFER is `past' return a date that is either CURRENT or past.
  11375. When PREFER is `future', return a date that is either CURRENT or future.
  11376. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11377. ;; Make the proper lists from the dates
  11378. (catch 'exit
  11379. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11380. dn dw sday cday n1 n2 n0
  11381. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11382. (setq start (org-date-to-gregorian start)
  11383. current (org-date-to-gregorian
  11384. (if show-all
  11385. current
  11386. (time-to-days (current-time))))
  11387. sday (calendar-absolute-from-gregorian start)
  11388. cday (calendar-absolute-from-gregorian current))
  11389. (if (<= cday sday) (throw 'exit sday))
  11390. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11391. (setq dn (string-to-number (match-string 1 change))
  11392. dw (cdr (assoc (match-string 2 change) a1)))
  11393. (error "Invalid change specifyer: %s" change))
  11394. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11395. (cond
  11396. ((eq dw 'day)
  11397. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11398. n2 (+ n1 dn)))
  11399. ((eq dw 'year)
  11400. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11401. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11402. (setq date1 (list m d y1)
  11403. n1 (calendar-absolute-from-gregorian date1)
  11404. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11405. n2 (calendar-absolute-from-gregorian date2)))
  11406. ((eq dw 'month)
  11407. ;; approx number of month between the two dates
  11408. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11409. ;; How often does dn fit in there?
  11410. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11411. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11412. m (+ m nm)
  11413. ny (floor (/ m 12))
  11414. y (+ y ny)
  11415. m (- m (* ny 12)))
  11416. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11417. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11418. (setq m2 (+ m dn) y2 y)
  11419. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11420. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11421. (while (<= n2 cday)
  11422. (setq n1 n2 m m2 y y2)
  11423. (setq m2 (+ m dn) y2 y)
  11424. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11425. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11426. ;; Make sure n1 is the earlier date
  11427. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11428. (if show-all
  11429. (cond
  11430. ((eq prefer 'past) n1)
  11431. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11432. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11433. (cond
  11434. ((eq prefer 'past) n1)
  11435. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11436. (t (if (= cday n1) n1 n2)))))))
  11437. (defun org-date-to-gregorian (date)
  11438. "Turn any specification of DATE into a gregorian date for the calendar."
  11439. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11440. ((and (listp date) (= (length date) 3)) date)
  11441. ((stringp date)
  11442. (setq date (org-parse-time-string date))
  11443. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11444. ((listp date)
  11445. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11446. (defun org-parse-time-string (s &optional nodefault)
  11447. "Parse the standard Org-mode time string.
  11448. This should be a lot faster than the normal `parse-time-string'.
  11449. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11450. hour and minute fields will be nil if not given."
  11451. (if (string-match org-ts-regexp0 s)
  11452. (list 0
  11453. (if (or (match-beginning 8) (not nodefault))
  11454. (string-to-number (or (match-string 8 s) "0")))
  11455. (if (or (match-beginning 7) (not nodefault))
  11456. (string-to-number (or (match-string 7 s) "0")))
  11457. (string-to-number (match-string 4 s))
  11458. (string-to-number (match-string 3 s))
  11459. (string-to-number (match-string 2 s))
  11460. nil nil nil)
  11461. (make-list 9 0)))
  11462. (defun org-timestamp-up (&optional arg)
  11463. "Increase the date item at the cursor by one.
  11464. If the cursor is on the year, change the year. If it is on the month or
  11465. the day, change that.
  11466. With prefix ARG, change by that many units."
  11467. (interactive "p")
  11468. (org-timestamp-change (prefix-numeric-value arg)))
  11469. (defun org-timestamp-down (&optional arg)
  11470. "Decrease the date item at the cursor by one.
  11471. If the cursor is on the year, change the year. If it is on the month or
  11472. the day, change that.
  11473. With prefix ARG, change by that many units."
  11474. (interactive "p")
  11475. (org-timestamp-change (- (prefix-numeric-value arg))))
  11476. (defun org-timestamp-up-day (&optional arg)
  11477. "Increase the date in the time stamp by one day.
  11478. With prefix ARG, change that many days."
  11479. (interactive "p")
  11480. (if (and (not (org-at-timestamp-p t))
  11481. (org-on-heading-p))
  11482. (org-todo 'up)
  11483. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11484. (defun org-timestamp-down-day (&optional arg)
  11485. "Decrease the date in the time stamp by one day.
  11486. With prefix ARG, change that many days."
  11487. (interactive "p")
  11488. (if (and (not (org-at-timestamp-p t))
  11489. (org-on-heading-p))
  11490. (org-todo 'down)
  11491. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11492. (defun org-at-timestamp-p (&optional inactive-ok)
  11493. "Determine if the cursor is in or at a timestamp."
  11494. (interactive)
  11495. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11496. (pos (point))
  11497. (ans (or (looking-at tsr)
  11498. (save-excursion
  11499. (skip-chars-backward "^[<\n\r\t")
  11500. (if (> (point) (point-min)) (backward-char 1))
  11501. (and (looking-at tsr)
  11502. (> (- (match-end 0) pos) -1))))))
  11503. (and ans
  11504. (boundp 'org-ts-what)
  11505. (setq org-ts-what
  11506. (cond
  11507. ((= pos (match-beginning 0)) 'bracket)
  11508. ((= pos (1- (match-end 0))) 'bracket)
  11509. ((org-pos-in-match-range pos 2) 'year)
  11510. ((org-pos-in-match-range pos 3) 'month)
  11511. ((org-pos-in-match-range pos 7) 'hour)
  11512. ((org-pos-in-match-range pos 8) 'minute)
  11513. ((or (org-pos-in-match-range pos 4)
  11514. (org-pos-in-match-range pos 5)) 'day)
  11515. ((and (> pos (or (match-end 8) (match-end 5)))
  11516. (< pos (match-end 0)))
  11517. (- pos (or (match-end 8) (match-end 5))))
  11518. (t 'day))))
  11519. ans))
  11520. (defun org-toggle-timestamp-type ()
  11521. "Toggle the type (<active> or [inactive]) of a time stamp."
  11522. (interactive)
  11523. (when (org-at-timestamp-p t)
  11524. (let ((beg (match-beginning 0)) (end (match-end 0))
  11525. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11526. (save-excursion
  11527. (goto-char beg)
  11528. (while (re-search-forward "[][<>]" end t)
  11529. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11530. t t)))
  11531. (message "Timestamp is now %sactive"
  11532. (if (equal (char-after beg) ?<) "" "in")))))
  11533. (defun org-timestamp-change (n &optional what)
  11534. "Change the date in the time stamp at point.
  11535. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11536. `year', `minute', `second'. If WHAT is not given, the cursor position
  11537. in the timestamp determines what will be changed."
  11538. (let ((pos (point))
  11539. with-hm inactive
  11540. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11541. org-ts-what
  11542. extra rem
  11543. ts time time0)
  11544. (if (not (org-at-timestamp-p t))
  11545. (error "Not at a timestamp"))
  11546. (if (and (not what) (eq org-ts-what 'bracket))
  11547. (org-toggle-timestamp-type)
  11548. (if (and (not what) (not (eq org-ts-what 'day))
  11549. org-display-custom-times
  11550. (get-text-property (point) 'display)
  11551. (not (get-text-property (1- (point)) 'display)))
  11552. (setq org-ts-what 'day))
  11553. (setq org-ts-what (or what org-ts-what)
  11554. inactive (= (char-after (match-beginning 0)) ?\[)
  11555. ts (match-string 0))
  11556. (replace-match "")
  11557. (if (string-match
  11558. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11559. ts)
  11560. (setq extra (match-string 1 ts)))
  11561. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11562. (setq with-hm t))
  11563. (setq time0 (org-parse-time-string ts))
  11564. (when (and (eq org-ts-what 'minute)
  11565. (eq current-prefix-arg nil))
  11566. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11567. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11568. (setcar (cdr time0) (+ (nth 1 time0)
  11569. (if (> n 0) (- rem) (- dm rem))))))
  11570. (setq time
  11571. (encode-time (or (car time0) 0)
  11572. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11573. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11574. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11575. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11576. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11577. (nthcdr 6 time0)))
  11578. (when (and (member org-ts-what '(hour minute))
  11579. extra
  11580. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11581. (setq extra (org-modify-ts-extra
  11582. extra
  11583. (if (eq org-ts-what 'hour) 2 5)
  11584. n dm)))
  11585. (when (integerp org-ts-what)
  11586. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11587. (if (eq what 'calendar)
  11588. (let ((cal-date (org-get-date-from-calendar)))
  11589. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11590. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11591. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11592. (setcar time0 (or (car time0) 0))
  11593. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11594. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11595. (setq time (apply 'encode-time time0))))
  11596. (setq org-last-changed-timestamp
  11597. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11598. (org-clock-update-time-maybe)
  11599. (goto-char pos)
  11600. ;; Try to recenter the calendar window, if any
  11601. (if (and org-calendar-follow-timestamp-change
  11602. (get-buffer-window "*Calendar*" t)
  11603. (memq org-ts-what '(day month year)))
  11604. (org-recenter-calendar (time-to-days time))))))
  11605. (defun org-modify-ts-extra (s pos n dm)
  11606. "Change the different parts of the lead-time and repeat fields in timestamp."
  11607. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11608. ng h m new rem)
  11609. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11610. (cond
  11611. ((or (org-pos-in-match-range pos 2)
  11612. (org-pos-in-match-range pos 3))
  11613. (setq m (string-to-number (match-string 3 s))
  11614. h (string-to-number (match-string 2 s)))
  11615. (if (org-pos-in-match-range pos 2)
  11616. (setq h (+ h n))
  11617. (setq n (* dm (org-no-warnings (signum n))))
  11618. (when (not (= 0 (setq rem (% m dm))))
  11619. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11620. (setq m (+ m n)))
  11621. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11622. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11623. (setq h (min 24 (max 0 h)))
  11624. (setq ng 1 new (format "-%02d:%02d" h m)))
  11625. ((org-pos-in-match-range pos 6)
  11626. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11627. ((org-pos-in-match-range pos 5)
  11628. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11629. ((org-pos-in-match-range pos 9)
  11630. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11631. ((org-pos-in-match-range pos 8)
  11632. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11633. (when ng
  11634. (setq s (concat
  11635. (substring s 0 (match-beginning ng))
  11636. new
  11637. (substring s (match-end ng))))))
  11638. s))
  11639. (defun org-recenter-calendar (date)
  11640. "If the calendar is visible, recenter it to DATE."
  11641. (let* ((win (selected-window))
  11642. (cwin (get-buffer-window "*Calendar*" t))
  11643. (calendar-move-hook nil))
  11644. (when cwin
  11645. (select-window cwin)
  11646. (calendar-goto-date (if (listp date) date
  11647. (calendar-gregorian-from-absolute date)))
  11648. (select-window win))))
  11649. (defun org-goto-calendar (&optional arg)
  11650. "Go to the Emacs calendar at the current date.
  11651. If there is a time stamp in the current line, go to that date.
  11652. A prefix ARG can be used to force the current date."
  11653. (interactive "P")
  11654. (let ((tsr org-ts-regexp) diff
  11655. (calendar-move-hook nil)
  11656. (calendar-view-holidays-initially-flag nil)
  11657. (view-calendar-holidays-initially nil)
  11658. (calendar-view-diary-initially-flag nil)
  11659. (view-diary-entries-initially nil))
  11660. (if (or (org-at-timestamp-p)
  11661. (save-excursion
  11662. (beginning-of-line 1)
  11663. (looking-at (concat ".*" tsr))))
  11664. (let ((d1 (time-to-days (current-time)))
  11665. (d2 (time-to-days
  11666. (org-time-string-to-time (match-string 1)))))
  11667. (setq diff (- d2 d1))))
  11668. (calendar)
  11669. (calendar-goto-today)
  11670. (if (and diff (not arg)) (calendar-forward-day diff))))
  11671. (defun org-get-date-from-calendar ()
  11672. "Return a list (month day year) of date at point in calendar."
  11673. (with-current-buffer "*Calendar*"
  11674. (save-match-data
  11675. (calendar-cursor-to-date))))
  11676. (defun org-date-from-calendar ()
  11677. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11678. If there is already a time stamp at the cursor position, update it."
  11679. (interactive)
  11680. (if (org-at-timestamp-p t)
  11681. (org-timestamp-change 0 'calendar)
  11682. (let ((cal-date (org-get-date-from-calendar)))
  11683. (org-insert-time-stamp
  11684. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11685. (defun org-minutes-to-hh:mm-string (m)
  11686. "Compute H:MM from a number of minutes."
  11687. (let ((h (/ m 60)))
  11688. (setq m (- m (* 60 h)))
  11689. (format org-time-clocksum-format h m)))
  11690. (defun org-hh:mm-string-to-minutes (s)
  11691. "Convert a string H:MM to a number of minutes."
  11692. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11693. (+ (* (string-to-number (match-string 1 s)) 60)
  11694. (string-to-number (match-string 2 s)))
  11695. 0))
  11696. ;;;; Files
  11697. (defun org-save-all-org-buffers ()
  11698. "Save all Org-mode buffers without user confirmation."
  11699. (interactive)
  11700. (message "Saving all Org-mode buffers...")
  11701. (save-some-buffers t 'org-mode-p)
  11702. (when (featurep 'org-id) (org-id-locations-save))
  11703. (message "Saving all Org-mode buffers... done"))
  11704. (defun org-revert-all-org-buffers ()
  11705. "Revert all Org-mode buffers.
  11706. Prompt for confirmation when there are unsaved changes.
  11707. Be sure you know what you are doing before letting this function
  11708. overwrite your changes.
  11709. This function is useful in a setup where one tracks org files
  11710. with a version control system, to revert on one machine after pulling
  11711. changes from another. I believe the procedure must be like this:
  11712. 1. M-x org-save-all-org-buffers
  11713. 2. Pull changes from the other machine, resolve conflicts
  11714. 3. M-x org-revert-all-org-buffers"
  11715. (interactive)
  11716. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11717. (error "Abort"))
  11718. (save-excursion
  11719. (save-window-excursion
  11720. (mapc
  11721. (lambda (b)
  11722. (when (and (with-current-buffer b (org-mode-p))
  11723. (with-current-buffer b buffer-file-name))
  11724. (switch-to-buffer b)
  11725. (revert-buffer t 'no-confirm)))
  11726. (buffer-list))
  11727. (when (and (featurep 'org-id) org-id-track-globally)
  11728. (org-id-locations-load)))))
  11729. ;;;; Agenda files
  11730. ;;;###autoload
  11731. (defun org-iswitchb (&optional arg)
  11732. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11733. With a prefix argument, restrict available to files.
  11734. With two prefix arguments, restrict available buffers to agenda files.
  11735. Due to some yet unresolved reason, the global function
  11736. `iswitchb-mode' needs to be active for this function to work."
  11737. (interactive "P")
  11738. (require 'iswitchb)
  11739. (let ((enabled iswitchb-mode) blist)
  11740. (or enabled (iswitchb-mode 1))
  11741. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11742. ((equal arg '(16)) (org-buffer-list 'agenda))
  11743. (t (org-buffer-list))))
  11744. (unwind-protect
  11745. (let ((iswitchb-make-buflist-hook
  11746. (lambda ()
  11747. (setq iswitchb-temp-buflist
  11748. (mapcar 'buffer-name blist)))))
  11749. (switch-to-buffer
  11750. (iswitchb-read-buffer
  11751. "Switch-to: " nil t))
  11752. (or enabled (iswitchb-mode -1))))))
  11753. ;;;###autoload
  11754. (defun org-ido-switchb (&optional arg)
  11755. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11756. With a prefix argument, restrict available to files.
  11757. With two prefix arguments, restrict available buffers to agenda files."
  11758. (interactive "P")
  11759. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11760. ((equal arg '(16)) (org-buffer-list 'agenda))
  11761. (t (org-buffer-list)))))
  11762. (switch-to-buffer
  11763. (org-ido-completing-read "Org buffer: "
  11764. (mapcar 'list (mapcar 'buffer-name blist))
  11765. nil t))))
  11766. (defun org-buffer-list (&optional predicate exclude-tmp)
  11767. "Return a list of Org buffers.
  11768. PREDICATE can be `export', `files' or `agenda'.
  11769. export restrict the list to Export buffers.
  11770. files restrict the list to buffers visiting Org files.
  11771. agenda restrict the list to buffers visiting agenda files.
  11772. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11773. (let* ((bfn nil)
  11774. (agenda-files (and (eq predicate 'agenda)
  11775. (mapcar 'file-truename (org-agenda-files t))))
  11776. (filter
  11777. (cond
  11778. ((eq predicate 'files)
  11779. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11780. ((eq predicate 'export)
  11781. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11782. ((eq predicate 'agenda)
  11783. (lambda (b)
  11784. (with-current-buffer b
  11785. (and (eq major-mode 'org-mode)
  11786. (setq bfn (buffer-file-name b))
  11787. (member (file-truename bfn) agenda-files)))))
  11788. (t (lambda (b) (with-current-buffer b
  11789. (or (eq major-mode 'org-mode)
  11790. (string-match "\*Org .*Export"
  11791. (buffer-name b)))))))))
  11792. (delq nil
  11793. (mapcar
  11794. (lambda(b)
  11795. (if (and (funcall filter b)
  11796. (or (not exclude-tmp)
  11797. (not (string-match "tmp" (buffer-name b)))))
  11798. b
  11799. nil))
  11800. (buffer-list)))))
  11801. (defun org-agenda-files (&optional unrestricted archives)
  11802. "Get the list of agenda files.
  11803. Optional UNRESTRICTED means return the full list even if a restriction
  11804. is currently in place.
  11805. When ARCHIVES is t, include all archive files hat are really being
  11806. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11807. `org-agenda-archives-mode' is t."
  11808. (let ((files
  11809. (cond
  11810. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11811. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11812. ((listp org-agenda-files) org-agenda-files)
  11813. (t (error "Invalid value of `org-agenda-files'")))))
  11814. (setq files (apply 'append
  11815. (mapcar (lambda (f)
  11816. (if (file-directory-p f)
  11817. (directory-files
  11818. f t org-agenda-file-regexp)
  11819. (list f)))
  11820. files)))
  11821. (when org-agenda-skip-unavailable-files
  11822. (setq files (delq nil
  11823. (mapcar (function
  11824. (lambda (file)
  11825. (and (file-readable-p file) file)))
  11826. files))))
  11827. (when (or (eq archives t)
  11828. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11829. (setq files (org-add-archive-files files)))
  11830. files))
  11831. (defun org-edit-agenda-file-list ()
  11832. "Edit the list of agenda files.
  11833. Depending on setup, this either uses customize to edit the variable
  11834. `org-agenda-files', or it visits the file that is holding the list. In the
  11835. latter case, the buffer is set up in a way that saving it automatically kills
  11836. the buffer and restores the previous window configuration."
  11837. (interactive)
  11838. (if (stringp org-agenda-files)
  11839. (let ((cw (current-window-configuration)))
  11840. (find-file org-agenda-files)
  11841. (org-set-local 'org-window-configuration cw)
  11842. (org-add-hook 'after-save-hook
  11843. (lambda ()
  11844. (set-window-configuration
  11845. (prog1 org-window-configuration
  11846. (kill-buffer (current-buffer))))
  11847. (org-install-agenda-files-menu)
  11848. (message "New agenda file list installed"))
  11849. nil 'local)
  11850. (message "%s" (substitute-command-keys
  11851. "Edit list and finish with \\[save-buffer]")))
  11852. (customize-variable 'org-agenda-files)))
  11853. (defun org-store-new-agenda-file-list (list)
  11854. "Set new value for the agenda file list and save it correctly."
  11855. (if (stringp org-agenda-files)
  11856. (let ((f org-agenda-files) b)
  11857. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11858. (with-temp-file f
  11859. (insert (mapconcat 'identity list "\n") "\n")))
  11860. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11861. (setq org-agenda-files list)
  11862. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11863. (defun org-read-agenda-file-list ()
  11864. "Read the list of agenda files from a file."
  11865. (when (file-directory-p org-agenda-files)
  11866. (error "`org-agenda-files' cannot be a single directory"))
  11867. (when (stringp org-agenda-files)
  11868. (with-temp-buffer
  11869. (insert-file-contents org-agenda-files)
  11870. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11871. ;;;###autoload
  11872. (defun org-cycle-agenda-files ()
  11873. "Cycle through the files in `org-agenda-files'.
  11874. If the current buffer visits an agenda file, find the next one in the list.
  11875. If the current buffer does not, find the first agenda file."
  11876. (interactive)
  11877. (let* ((fs (org-agenda-files t))
  11878. (files (append fs (list (car fs))))
  11879. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11880. file)
  11881. (unless files (error "No agenda files"))
  11882. (catch 'exit
  11883. (while (setq file (pop files))
  11884. (if (equal (file-truename file) tcf)
  11885. (when (car files)
  11886. (find-file (car files))
  11887. (throw 'exit t))))
  11888. (find-file (car fs)))
  11889. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11890. (defun org-agenda-file-to-front (&optional to-end)
  11891. "Move/add the current file to the top of the agenda file list.
  11892. If the file is not present in the list, it is added to the front. If it is
  11893. present, it is moved there. With optional argument TO-END, add/move to the
  11894. end of the list."
  11895. (interactive "P")
  11896. (let ((org-agenda-skip-unavailable-files nil)
  11897. (file-alist (mapcar (lambda (x)
  11898. (cons (file-truename x) x))
  11899. (org-agenda-files t)))
  11900. (ctf (file-truename buffer-file-name))
  11901. x had)
  11902. (setq x (assoc ctf file-alist) had x)
  11903. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11904. (if to-end
  11905. (setq file-alist (append (delq x file-alist) (list x)))
  11906. (setq file-alist (cons x (delq x file-alist))))
  11907. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11908. (org-install-agenda-files-menu)
  11909. (message "File %s to %s of agenda file list"
  11910. (if had "moved" "added") (if to-end "end" "front"))))
  11911. (defun org-remove-file (&optional file)
  11912. "Remove current file from the list of files in variable `org-agenda-files'.
  11913. These are the files which are being checked for agenda entries.
  11914. Optional argument FILE means, use this file instead of the current."
  11915. (interactive)
  11916. (let* ((org-agenda-skip-unavailable-files nil)
  11917. (file (or file buffer-file-name))
  11918. (true-file (file-truename file))
  11919. (afile (abbreviate-file-name file))
  11920. (files (delq nil (mapcar
  11921. (lambda (x)
  11922. (if (equal true-file
  11923. (file-truename x))
  11924. nil x))
  11925. (org-agenda-files t)))))
  11926. (if (not (= (length files) (length (org-agenda-files t))))
  11927. (progn
  11928. (org-store-new-agenda-file-list files)
  11929. (org-install-agenda-files-menu)
  11930. (message "Removed file: %s" afile))
  11931. (message "File was not in list: %s (not removed)" afile))))
  11932. (defun org-file-menu-entry (file)
  11933. (vector file (list 'find-file file) t))
  11934. (defun org-check-agenda-file (file)
  11935. "Make sure FILE exists. If not, ask user what to do."
  11936. (when (not (file-exists-p file))
  11937. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11938. (abbreviate-file-name file))
  11939. (let ((r (downcase (read-char-exclusive))))
  11940. (cond
  11941. ((equal r ?r)
  11942. (org-remove-file file)
  11943. (throw 'nextfile t))
  11944. (t (error "Abort"))))))
  11945. (defun org-get-agenda-file-buffer (file)
  11946. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11947. it to the list of buffers which might be released later."
  11948. (let ((buf (org-find-base-buffer-visiting file)))
  11949. (if buf
  11950. buf ; just return it
  11951. ;; Make a new buffer and remember it
  11952. (setq buf (find-file-noselect file))
  11953. (if buf (push buf org-agenda-new-buffers))
  11954. buf)))
  11955. (defun org-release-buffers (blist)
  11956. "Release all buffers in list, asking the user for confirmation when needed.
  11957. When a buffer is unmodified, it is just killed. When modified, it is saved
  11958. \(if the user agrees) and then killed."
  11959. (let (buf file)
  11960. (while (setq buf (pop blist))
  11961. (setq file (buffer-file-name buf))
  11962. (when (and (buffer-modified-p buf)
  11963. file
  11964. (y-or-n-p (format "Save file %s? " file)))
  11965. (with-current-buffer buf (save-buffer)))
  11966. (kill-buffer buf))))
  11967. (defun org-prepare-agenda-buffers (files)
  11968. "Create buffers for all agenda files, protect archived trees and comments."
  11969. (interactive)
  11970. (let ((pa '(:org-archived t))
  11971. (pc '(:org-comment t))
  11972. (pall '(:org-archived t :org-comment t))
  11973. (inhibit-read-only t)
  11974. (rea (concat ":" org-archive-tag ":"))
  11975. bmp file re)
  11976. (save-excursion
  11977. (save-restriction
  11978. (while (setq file (pop files))
  11979. (if (bufferp file)
  11980. (set-buffer file)
  11981. (org-check-agenda-file file)
  11982. (set-buffer (org-get-agenda-file-buffer file)))
  11983. (widen)
  11984. (setq bmp (buffer-modified-p))
  11985. (org-refresh-category-properties)
  11986. (setq org-todo-keywords-for-agenda
  11987. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11988. (setq org-done-keywords-for-agenda
  11989. (append org-done-keywords-for-agenda org-done-keywords))
  11990. (setq org-todo-keyword-alist-for-agenda
  11991. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11992. (setq org-tag-alist-for-agenda
  11993. (append org-tag-alist-for-agenda org-tag-alist))
  11994. (save-excursion
  11995. (remove-text-properties (point-min) (point-max) pall)
  11996. (when org-agenda-skip-archived-trees
  11997. (goto-char (point-min))
  11998. (while (re-search-forward rea nil t)
  11999. (if (org-on-heading-p t)
  12000. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12001. (goto-char (point-min))
  12002. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12003. (while (re-search-forward re nil t)
  12004. (add-text-properties
  12005. (match-beginning 0) (org-end-of-subtree t) pc)))
  12006. (set-buffer-modified-p bmp))))
  12007. (setq org-todo-keyword-alist-for-agenda
  12008. (org-uniquify org-todo-keyword-alist-for-agenda)
  12009. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12010. ;;;; Embedded LaTeX
  12011. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12012. "Keymap for the minor `org-cdlatex-mode'.")
  12013. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12014. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12015. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12016. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12017. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12018. (defvar org-cdlatex-texmathp-advice-is-done nil
  12019. "Flag remembering if we have applied the advice to texmathp already.")
  12020. (define-minor-mode org-cdlatex-mode
  12021. "Toggle the minor `org-cdlatex-mode'.
  12022. This mode supports entering LaTeX environment and math in LaTeX fragments
  12023. in Org-mode.
  12024. \\{org-cdlatex-mode-map}"
  12025. nil " OCDL" nil
  12026. (when org-cdlatex-mode (require 'cdlatex))
  12027. (unless org-cdlatex-texmathp-advice-is-done
  12028. (setq org-cdlatex-texmathp-advice-is-done t)
  12029. (defadvice texmathp (around org-math-always-on activate)
  12030. "Always return t in org-mode buffers.
  12031. This is because we want to insert math symbols without dollars even outside
  12032. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12033. an embedded LaTeX fragment, let texmathp do its job.
  12034. \\[org-cdlatex-mode-map]"
  12035. (interactive)
  12036. (let (p)
  12037. (cond
  12038. ((not (org-mode-p)) ad-do-it)
  12039. ((eq this-command 'cdlatex-math-symbol)
  12040. (setq ad-return-value t
  12041. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12042. (t
  12043. (let ((p (org-inside-LaTeX-fragment-p)))
  12044. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12045. (setq ad-return-value t
  12046. texmathp-why '("Org-mode embedded math" . 0))
  12047. (if p ad-do-it)))))))))
  12048. (defun turn-on-org-cdlatex ()
  12049. "Unconditionally turn on `org-cdlatex-mode'."
  12050. (org-cdlatex-mode 1))
  12051. (defun org-inside-LaTeX-fragment-p ()
  12052. "Test if point is inside a LaTeX fragment.
  12053. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12054. sequence appearing also before point.
  12055. Even though the matchers for math are configurable, this function assumes
  12056. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12057. delimiters are skipped when they have been removed by customization.
  12058. The return value is nil, or a cons cell with the delimiter and
  12059. and the position of this delimiter.
  12060. This function does a reasonably good job, but can locally be fooled by
  12061. for example currency specifications. For example it will assume being in
  12062. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12063. fragments that are properly closed, but during editing, we have to live
  12064. with the uncertainty caused by missing closing delimiters. This function
  12065. looks only before point, not after."
  12066. (catch 'exit
  12067. (let ((pos (point))
  12068. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12069. (lim (progn
  12070. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12071. (point)))
  12072. dd-on str (start 0) m re)
  12073. (goto-char pos)
  12074. (when dodollar
  12075. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12076. re (nth 1 (assoc "$" org-latex-regexps)))
  12077. (while (string-match re str start)
  12078. (cond
  12079. ((= (match-end 0) (length str))
  12080. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12081. ((= (match-end 0) (- (length str) 5))
  12082. (throw 'exit nil))
  12083. (t (setq start (match-end 0))))))
  12084. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12085. (goto-char pos)
  12086. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12087. (and (match-beginning 2) (throw 'exit nil))
  12088. ;; count $$
  12089. (while (re-search-backward "\\$\\$" lim t)
  12090. (setq dd-on (not dd-on)))
  12091. (goto-char pos)
  12092. (if dd-on (cons "$$" m))))))
  12093. (defun org-try-cdlatex-tab ()
  12094. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12095. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12096. - inside a LaTeX fragment, or
  12097. - after the first word in a line, where an abbreviation expansion could
  12098. insert a LaTeX environment."
  12099. (when org-cdlatex-mode
  12100. (cond
  12101. ((save-excursion
  12102. (skip-chars-backward "a-zA-Z0-9*")
  12103. (skip-chars-backward " \t")
  12104. (bolp))
  12105. (cdlatex-tab) t)
  12106. ((org-inside-LaTeX-fragment-p)
  12107. (cdlatex-tab) t)
  12108. (t nil))))
  12109. (defun org-cdlatex-underscore-caret (&optional arg)
  12110. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12111. Revert to the normal definition outside of these fragments."
  12112. (interactive "P")
  12113. (if (org-inside-LaTeX-fragment-p)
  12114. (call-interactively 'cdlatex-sub-superscript)
  12115. (let (org-cdlatex-mode)
  12116. (call-interactively (key-binding (vector last-input-event))))))
  12117. (defun org-cdlatex-math-modify (&optional arg)
  12118. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12119. Revert to the normal definition outside of these fragments."
  12120. (interactive "P")
  12121. (if (org-inside-LaTeX-fragment-p)
  12122. (call-interactively 'cdlatex-math-modify)
  12123. (let (org-cdlatex-mode)
  12124. (call-interactively (key-binding (vector last-input-event))))))
  12125. (defvar org-latex-fragment-image-overlays nil
  12126. "List of overlays carrying the images of latex fragments.")
  12127. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12128. (defun org-remove-latex-fragment-image-overlays ()
  12129. "Remove all overlays with LaTeX fragment images in current buffer."
  12130. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12131. (setq org-latex-fragment-image-overlays nil))
  12132. (defun org-preview-latex-fragment (&optional subtree)
  12133. "Preview the LaTeX fragment at point, or all locally or globally.
  12134. If the cursor is in a LaTeX fragment, create the image and overlay
  12135. it over the source code. If there is no fragment at point, display
  12136. all fragments in the current text, from one headline to the next. With
  12137. prefix SUBTREE, display all fragments in the current subtree. With a
  12138. double prefix `C-u C-u', or when the cursor is before the first headline,
  12139. display all fragments in the buffer.
  12140. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12141. (interactive "P")
  12142. (org-remove-latex-fragment-image-overlays)
  12143. (save-excursion
  12144. (save-restriction
  12145. (let (beg end at msg)
  12146. (cond
  12147. ((or (equal subtree '(16))
  12148. (not (save-excursion
  12149. (re-search-backward (concat "^" outline-regexp) nil t))))
  12150. (setq beg (point-min) end (point-max)
  12151. msg "Creating images for buffer...%s"))
  12152. ((equal subtree '(4))
  12153. (org-back-to-heading)
  12154. (setq beg (point) end (org-end-of-subtree t)
  12155. msg "Creating images for subtree...%s"))
  12156. (t
  12157. (if (setq at (org-inside-LaTeX-fragment-p))
  12158. (goto-char (max (point-min) (- (cdr at) 2)))
  12159. (org-back-to-heading))
  12160. (setq beg (point) end (progn (outline-next-heading) (point))
  12161. msg (if at "Creating image...%s"
  12162. "Creating images for entry...%s"))))
  12163. (message msg "")
  12164. (narrow-to-region beg end)
  12165. (goto-char beg)
  12166. (org-format-latex
  12167. (concat "ltxpng/" (file-name-sans-extension
  12168. (file-name-nondirectory
  12169. buffer-file-name)))
  12170. default-directory 'overlays msg at 'forbuffer)
  12171. (message msg "done. Use `C-c C-c' to remove images.")))))
  12172. (defvar org-latex-regexps
  12173. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12174. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12175. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12176. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12177. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12178. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12179. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12180. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12181. "Regular expressions for matching embedded LaTeX.")
  12182. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12183. "Replace LaTeX fragments with links to an image, and produce images."
  12184. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12185. (let* ((prefixnodir (file-name-nondirectory prefix))
  12186. (absprefix (expand-file-name prefix dir))
  12187. (todir (file-name-directory absprefix))
  12188. (opt org-format-latex-options)
  12189. (matchers (plist-get opt :matchers))
  12190. (re-list org-latex-regexps)
  12191. (cnt 0) txt link beg end re e checkdir
  12192. m n block linkfile movefile ov)
  12193. ;; Check if there are old images files with this prefix, and remove them
  12194. (when (file-directory-p todir)
  12195. (mapc 'delete-file
  12196. (directory-files
  12197. todir 'full
  12198. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12199. ;; Check the different regular expressions
  12200. (while (setq e (pop re-list))
  12201. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12202. block (if (nth 3 e) "\n\n" ""))
  12203. (when (member m matchers)
  12204. (goto-char (point-min))
  12205. (while (re-search-forward re nil t)
  12206. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12207. (not (get-text-property (match-beginning n)
  12208. 'org-protected)))
  12209. (setq txt (match-string n)
  12210. beg (match-beginning n) end (match-end n)
  12211. cnt (1+ cnt)
  12212. linkfile (format "%s_%04d.png" prefix cnt)
  12213. movefile (format "%s_%04d.png" absprefix cnt)
  12214. link (concat block "[[file:" linkfile "]]" block))
  12215. (if msg (message msg cnt))
  12216. (goto-char beg)
  12217. (unless checkdir ; make sure the directory exists
  12218. (setq checkdir t)
  12219. (or (file-directory-p todir) (make-directory todir)))
  12220. (org-create-formula-image
  12221. txt movefile opt forbuffer)
  12222. (if overlays
  12223. (progn
  12224. (setq ov (org-make-overlay beg end))
  12225. (if (featurep 'xemacs)
  12226. (progn
  12227. (org-overlay-put ov 'invisible t)
  12228. (org-overlay-put
  12229. ov 'end-glyph
  12230. (make-glyph (vector 'png :file movefile))))
  12231. (org-overlay-put
  12232. ov 'display
  12233. (list 'image :type 'png :file movefile :ascent 'center)))
  12234. (push ov org-latex-fragment-image-overlays)
  12235. (goto-char end))
  12236. (delete-region beg end)
  12237. (insert link))))))))
  12238. ;; This function borrows from Ganesh Swami's latex2png.el
  12239. (defun org-create-formula-image (string tofile options buffer)
  12240. (let* ((tmpdir (if (featurep 'xemacs)
  12241. (temp-directory)
  12242. temporary-file-directory))
  12243. (texfilebase (make-temp-name
  12244. (expand-file-name "orgtex" tmpdir)))
  12245. (texfile (concat texfilebase ".tex"))
  12246. (dvifile (concat texfilebase ".dvi"))
  12247. (pngfile (concat texfilebase ".png"))
  12248. (fnh (if (featurep 'xemacs)
  12249. (font-height (get-face-font 'default))
  12250. (face-attribute 'default :height nil)))
  12251. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12252. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12253. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12254. "Black"))
  12255. (bg (or (plist-get options (if buffer :background :html-background))
  12256. "Transparent")))
  12257. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12258. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12259. (with-temp-file texfile
  12260. (insert org-format-latex-header
  12261. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12262. (let ((dir default-directory))
  12263. (condition-case nil
  12264. (progn
  12265. (cd tmpdir)
  12266. (call-process "latex" nil nil nil texfile))
  12267. (error nil))
  12268. (cd dir))
  12269. (if (not (file-exists-p dvifile))
  12270. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12271. (condition-case nil
  12272. (call-process "dvipng" nil nil nil
  12273. "-E" "-fg" fg "-bg" bg
  12274. "-D" dpi
  12275. ;;"-x" scale "-y" scale
  12276. "-T" "tight"
  12277. "-o" pngfile
  12278. dvifile)
  12279. (error nil))
  12280. (if (not (file-exists-p pngfile))
  12281. (progn (message "Failed to create png file from %s" texfile) nil)
  12282. ;; Use the requested file name and clean up
  12283. (copy-file pngfile tofile 'replace)
  12284. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12285. (delete-file (concat texfilebase e)))
  12286. pngfile))))
  12287. (defun org-dvipng-color (attr)
  12288. "Return an rgb color specification for dvipng."
  12289. (apply 'format "rgb %s %s %s"
  12290. (mapcar 'org-normalize-color
  12291. (color-values (face-attribute 'default attr nil)))))
  12292. (defun org-normalize-color (value)
  12293. "Return string to be used as color value for an RGB component."
  12294. (format "%g" (/ value 65535.0)))
  12295. ;;;; Key bindings
  12296. ;; Make `C-c C-x' a prefix key
  12297. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12298. ;; TAB key with modifiers
  12299. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12300. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12301. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12302. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12303. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12304. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12305. ;; The following line is necessary under Suse GNU/Linux
  12306. (unless (featurep 'xemacs)
  12307. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12308. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12309. (define-key org-mode-map [backtab] 'org-shifttab)
  12310. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12311. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12312. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12313. ;; Cursor keys with modifiers
  12314. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12315. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12316. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12317. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12318. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12319. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12320. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12321. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12322. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12323. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12324. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12325. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12326. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12327. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12328. ;;; Extra keys for tty access.
  12329. ;; We only set them when really needed because otherwise the
  12330. ;; menus don't show the simple keys
  12331. (when (or org-use-extra-keys
  12332. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12333. (not window-system))
  12334. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12335. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12336. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12337. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12338. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12339. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12340. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12341. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12342. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12343. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12344. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12345. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12346. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12347. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12348. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12349. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12350. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12351. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12352. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12353. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12354. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12355. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12356. ;; All the other keys
  12357. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12358. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12359. (if (boundp 'narrow-map)
  12360. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12361. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12362. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12363. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12364. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12365. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12366. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12367. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12368. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12369. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12370. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12371. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12372. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12373. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12374. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12375. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12376. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12377. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12378. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12379. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12380. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12381. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12382. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12383. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12384. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12385. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12386. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12387. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12388. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12389. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12390. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12391. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12392. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12393. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12394. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12395. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12396. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12397. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12398. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12399. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12400. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12401. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12402. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12403. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12404. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12405. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12406. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12407. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12408. (org-defkey org-mode-map "\C-m" 'org-return)
  12409. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12410. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12411. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12412. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12413. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12414. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12415. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12416. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12417. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12418. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12419. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12420. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12421. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12422. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12423. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12424. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12425. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12426. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12427. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12428. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12429. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12430. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12431. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12432. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12433. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12434. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12435. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12436. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12437. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12438. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12439. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12440. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12441. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12442. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12443. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12444. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12445. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12446. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12447. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12448. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12449. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12450. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12451. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12452. (when (featurep 'xemacs)
  12453. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12454. (defvar org-self-insert-command-undo-counter 0)
  12455. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12456. (defun org-self-insert-command (N)
  12457. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12458. If the cursor is in a table looking at whitespace, the whitespace is
  12459. overwritten, and the table is not marked as requiring realignment."
  12460. (interactive "p")
  12461. (if (and
  12462. (org-table-p)
  12463. (progn
  12464. ;; check if we blank the field, and if that triggers align
  12465. (and (featurep 'org-table) org-table-auto-blank-field
  12466. (member last-command
  12467. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12468. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12469. ;; got extra space, this field does not determine column width
  12470. (let (org-table-may-need-update) (org-table-blank-field))
  12471. ;; no extra space, this field may determine column width
  12472. (org-table-blank-field)))
  12473. t)
  12474. (eq N 1)
  12475. (looking-at "[^|\n]* |"))
  12476. (let (org-table-may-need-update)
  12477. (goto-char (1- (match-end 0)))
  12478. (delete-backward-char 1)
  12479. (goto-char (match-beginning 0))
  12480. (self-insert-command N))
  12481. (setq org-table-may-need-update t)
  12482. (self-insert-command N)
  12483. (org-fix-tags-on-the-fly)
  12484. (if org-self-insert-cluster-for-undo
  12485. (if (not (eq last-command 'org-self-insert-command))
  12486. (setq org-self-insert-command-undo-counter 1)
  12487. (if (>= org-self-insert-command-undo-counter 20)
  12488. (setq org-self-insert-command-undo-counter 1)
  12489. (and (> org-self-insert-command-undo-counter 0)
  12490. buffer-undo-list
  12491. (not (cadr buffer-undo-list)) ; remove nil entry
  12492. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12493. (setq org-self-insert-command-undo-counter
  12494. (1+ org-self-insert-command-undo-counter)))))))
  12495. (defun org-fix-tags-on-the-fly ()
  12496. (when (and (equal (char-after (point-at-bol)) ?*)
  12497. (org-on-heading-p))
  12498. (org-align-tags-here org-tags-column)))
  12499. (defun org-delete-backward-char (N)
  12500. "Like `delete-backward-char', insert whitespace at field end in tables.
  12501. When deleting backwards, in tables this function will insert whitespace in
  12502. front of the next \"|\" separator, to keep the table aligned. The table will
  12503. still be marked for re-alignment if the field did fill the entire column,
  12504. because, in this case the deletion might narrow the column."
  12505. (interactive "p")
  12506. (if (and (org-table-p)
  12507. (eq N 1)
  12508. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12509. (looking-at ".*?|"))
  12510. (let ((pos (point))
  12511. (noalign (looking-at "[^|\n\r]* |"))
  12512. (c org-table-may-need-update))
  12513. (backward-delete-char N)
  12514. (skip-chars-forward "^|")
  12515. (insert " ")
  12516. (goto-char (1- pos))
  12517. ;; noalign: if there were two spaces at the end, this field
  12518. ;; does not determine the width of the column.
  12519. (if noalign (setq org-table-may-need-update c)))
  12520. (backward-delete-char N)
  12521. (org-fix-tags-on-the-fly)))
  12522. (defun org-delete-char (N)
  12523. "Like `delete-char', but insert whitespace at field end in tables.
  12524. When deleting characters, in tables this function will insert whitespace in
  12525. front of the next \"|\" separator, to keep the table aligned. The table will
  12526. still be marked for re-alignment if the field did fill the entire column,
  12527. because, in this case the deletion might narrow the column."
  12528. (interactive "p")
  12529. (if (and (org-table-p)
  12530. (not (bolp))
  12531. (not (= (char-after) ?|))
  12532. (eq N 1))
  12533. (if (looking-at ".*?|")
  12534. (let ((pos (point))
  12535. (noalign (looking-at "[^|\n\r]* |"))
  12536. (c org-table-may-need-update))
  12537. (replace-match (concat
  12538. (substring (match-string 0) 1 -1)
  12539. " |"))
  12540. (goto-char pos)
  12541. ;; noalign: if there were two spaces at the end, this field
  12542. ;; does not determine the width of the column.
  12543. (if noalign (setq org-table-may-need-update c)))
  12544. (delete-char N))
  12545. (delete-char N)
  12546. (org-fix-tags-on-the-fly)))
  12547. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12548. (put 'org-self-insert-command 'delete-selection t)
  12549. (put 'orgtbl-self-insert-command 'delete-selection t)
  12550. (put 'org-delete-char 'delete-selection 'supersede)
  12551. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12552. (put 'org-yank 'delete-selection 'yank)
  12553. ;; Make `flyspell-mode' delay after some commands
  12554. (put 'org-self-insert-command 'flyspell-delayed t)
  12555. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12556. (put 'org-delete-char 'flyspell-delayed t)
  12557. (put 'org-delete-backward-char 'flyspell-delayed t)
  12558. ;; Make pabbrev-mode expand after org-mode commands
  12559. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12560. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12561. ;; How to do this: Measure non-white length of current string
  12562. ;; If equal to column width, we should realign.
  12563. (defun org-remap (map &rest commands)
  12564. "In MAP, remap the functions given in COMMANDS.
  12565. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12566. (let (new old)
  12567. (while commands
  12568. (setq old (pop commands) new (pop commands))
  12569. (if (fboundp 'command-remapping)
  12570. (org-defkey map (vector 'remap old) new)
  12571. (substitute-key-definition old new map global-map)))))
  12572. (when (eq org-enable-table-editor 'optimized)
  12573. ;; If the user wants maximum table support, we need to hijack
  12574. ;; some standard editing functions
  12575. (org-remap org-mode-map
  12576. 'self-insert-command 'org-self-insert-command
  12577. 'delete-char 'org-delete-char
  12578. 'delete-backward-char 'org-delete-backward-char)
  12579. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12580. (defvar org-ctrl-c-ctrl-c-hook nil
  12581. "Hook for functions attaching themselves to `C-c C-c'.
  12582. This can be used to add additional functionality to the C-c C-c key which
  12583. executes context-dependent commands.
  12584. Each function will be called with no arguments. The function must check
  12585. if the context is appropriate for it to act. If yes, it should do its
  12586. thing and then return a non-nil value. If the context is wrong,
  12587. just do nothing.")
  12588. (defvar org-metaleft-hook nil
  12589. "Hook for functions attaching themselves to `M-left'.
  12590. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12591. (defvar org-metaright-hook nil
  12592. "Hook for functions attaching themselves to `M-right'.
  12593. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12594. (defvar org-metaup-hook nil
  12595. "Hook for functions attaching themselves to `M-up'.
  12596. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12597. (defvar org-metadown-hook nil
  12598. "Hook for functions attaching themselves to `M-down'.
  12599. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12600. (defvar org-shiftmetaleft-hook nil
  12601. "Hook for functions attaching themselves to `M-S-left'.
  12602. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12603. (defvar org-shiftmetaright-hook nil
  12604. "Hook for functions attaching themselves to `M-S-right'.
  12605. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12606. (defvar org-shiftmetaup-hook nil
  12607. "Hook for functions attaching themselves to `M-S-up'.
  12608. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12609. (defvar org-shiftmetadown-hook nil
  12610. "Hook for functions attaching themselves to `M-S-down'.
  12611. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12612. (defvar org-metareturn-hook nil
  12613. "Hook for functions attaching themselves to `M-RET'.
  12614. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12615. (defun org-modifier-cursor-error ()
  12616. "Throw an error, a modified cursor command was applied in wrong context."
  12617. (error "This command is active in special context like tables, headlines or items"))
  12618. (defun org-shiftselect-error ()
  12619. "Throw an error because Shift-Cursor command was applied in wrong context."
  12620. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12621. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12622. (error "This command works only in special context like headlines or timestamps.")))
  12623. (defun org-call-for-shift-select (cmd)
  12624. (let ((this-command-keys-shift-translated t))
  12625. (call-interactively cmd)))
  12626. (defun org-shifttab (&optional arg)
  12627. "Global visibility cycling or move to previous table field.
  12628. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12629. on context.
  12630. See the individual commands for more information."
  12631. (interactive "P")
  12632. (cond
  12633. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12634. ((integerp arg)
  12635. (message "Content view to level: %d" arg)
  12636. (org-content (prefix-numeric-value arg))
  12637. (setq org-cycle-global-status 'overview))
  12638. (t (call-interactively 'org-global-cycle))))
  12639. (defun org-shiftmetaleft ()
  12640. "Promote subtree or delete table column.
  12641. Calls `org-promote-subtree', `org-outdent-item',
  12642. or `org-table-delete-column', depending on context.
  12643. See the individual commands for more information."
  12644. (interactive)
  12645. (cond
  12646. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12647. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12648. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12649. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12650. (t (org-modifier-cursor-error))))
  12651. (defun org-shiftmetaright ()
  12652. "Demote subtree or insert table column.
  12653. Calls `org-demote-subtree', `org-indent-item',
  12654. or `org-table-insert-column', depending on context.
  12655. See the individual commands for more information."
  12656. (interactive)
  12657. (cond
  12658. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12659. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12660. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12661. ((org-at-item-p) (call-interactively 'org-indent-item))
  12662. (t (org-modifier-cursor-error))))
  12663. (defun org-shiftmetaup (&optional arg)
  12664. "Move subtree up or kill table row.
  12665. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12666. `org-move-item-up' depending on context. See the individual commands
  12667. for more information."
  12668. (interactive "P")
  12669. (cond
  12670. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12671. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12672. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12673. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12674. (t (org-modifier-cursor-error))))
  12675. (defun org-shiftmetadown (&optional arg)
  12676. "Move subtree down or insert table row.
  12677. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12678. `org-move-item-down', depending on context. See the individual
  12679. commands for more information."
  12680. (interactive "P")
  12681. (cond
  12682. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12683. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12684. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12685. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12686. (t (org-modifier-cursor-error))))
  12687. (defun org-metaleft (&optional arg)
  12688. "Promote heading or move table column to left.
  12689. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12690. With no specific context, calls the Emacs default `backward-word'.
  12691. See the individual commands for more information."
  12692. (interactive "P")
  12693. (cond
  12694. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12695. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12696. ((or (org-on-heading-p)
  12697. (and (org-region-active-p)
  12698. (save-excursion
  12699. (goto-char (region-beginning))
  12700. (org-on-heading-p))))
  12701. (call-interactively 'org-do-promote))
  12702. ((or (org-at-item-p)
  12703. (and (org-region-active-p)
  12704. (save-excursion
  12705. (goto-char (region-beginning))
  12706. (org-at-item-p))))
  12707. (call-interactively 'org-outdent-item))
  12708. (t (call-interactively 'backward-word))))
  12709. (defun org-metaright (&optional arg)
  12710. "Demote subtree or move table column to right.
  12711. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12712. With no specific context, calls the Emacs default `forward-word'.
  12713. See the individual commands for more information."
  12714. (interactive "P")
  12715. (cond
  12716. ((run-hook-with-args-until-success 'org-metaright-hook))
  12717. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12718. ((or (org-on-heading-p)
  12719. (and (org-region-active-p)
  12720. (save-excursion
  12721. (goto-char (region-beginning))
  12722. (org-on-heading-p))))
  12723. (call-interactively 'org-do-demote))
  12724. ((or (org-at-item-p)
  12725. (and (org-region-active-p)
  12726. (save-excursion
  12727. (goto-char (region-beginning))
  12728. (org-at-item-p))))
  12729. (call-interactively 'org-indent-item))
  12730. (t (call-interactively 'forward-word))))
  12731. (defun org-metaup (&optional arg)
  12732. "Move subtree up or move table row up.
  12733. Calls `org-move-subtree-up' or `org-table-move-row' or
  12734. `org-move-item-up', depending on context. See the individual commands
  12735. for more information."
  12736. (interactive "P")
  12737. (cond
  12738. ((run-hook-with-args-until-success 'org-metaup-hook))
  12739. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12740. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12741. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12742. (t (transpose-lines 1) (beginning-of-line -1))))
  12743. (defun org-metadown (&optional arg)
  12744. "Move subtree down or move table row down.
  12745. Calls `org-move-subtree-down' or `org-table-move-row' or
  12746. `org-move-item-down', depending on context. See the individual
  12747. commands for more information."
  12748. (interactive "P")
  12749. (cond
  12750. ((run-hook-with-args-until-success 'org-metadown-hook))
  12751. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12752. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12753. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12754. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12755. (defun org-shiftup (&optional arg)
  12756. "Increase item in timestamp or increase priority of current headline.
  12757. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12758. depending on context. See the individual commands for more information."
  12759. (interactive "P")
  12760. (cond
  12761. ((and org-support-shift-select (org-region-active-p))
  12762. (org-call-for-shift-select 'previous-line))
  12763. ((org-at-timestamp-p t)
  12764. (call-interactively (if org-edit-timestamp-down-means-later
  12765. 'org-timestamp-down 'org-timestamp-up)))
  12766. ((and (not (eq org-support-shift-select 'always))
  12767. (org-on-heading-p))
  12768. (call-interactively 'org-priority-up))
  12769. ((and (not org-support-shift-select) (org-at-item-p))
  12770. (call-interactively 'org-previous-item))
  12771. ((org-clocktable-try-shift 'up arg))
  12772. (org-support-shift-select
  12773. (org-call-for-shift-select 'previous-line))
  12774. (t (org-shiftselect-error))))
  12775. (defun org-shiftdown (&optional arg)
  12776. "Decrease item in timestamp or decrease priority of current headline.
  12777. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12778. depending on context. See the individual commands for more information."
  12779. (interactive "P")
  12780. (cond
  12781. ((and org-support-shift-select (org-region-active-p))
  12782. (org-call-for-shift-select 'next-line))
  12783. ((org-at-timestamp-p t)
  12784. (call-interactively (if org-edit-timestamp-down-means-later
  12785. 'org-timestamp-up 'org-timestamp-down)))
  12786. ((and (not (eq org-support-shift-select 'always))
  12787. (org-on-heading-p))
  12788. (call-interactively 'org-priority-down))
  12789. ((and (not org-support-shift-select) (org-at-item-p))
  12790. (call-interactively 'org-next-item))
  12791. ((org-clocktable-try-shift 'down arg))
  12792. (org-support-shift-select
  12793. (org-call-for-shift-select 'next-line))
  12794. (t (org-shiftselect-error))))
  12795. (defun org-shiftright (&optional arg)
  12796. "Cycle the thing at point or in the current line, depending on context.
  12797. Depending on context, this does one of the following:
  12798. - switch a timestamp at point one day into the future
  12799. - on a headline, switch to the next TODO keyword.
  12800. - on an item, switch entire list to the next bullet type
  12801. - on a property line, switch to the next allowed value
  12802. - on a clocktable definition line, move time block into the future"
  12803. (interactive "P")
  12804. (cond
  12805. ((and org-support-shift-select (org-region-active-p))
  12806. (org-call-for-shift-select 'forward-char))
  12807. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12808. ((and (not (eq org-support-shift-select 'always))
  12809. (org-on-heading-p))
  12810. (org-call-with-arg 'org-todo 'right))
  12811. ((or (and org-support-shift-select
  12812. (not (eq org-support-shift-select 'always))
  12813. (org-at-item-bullet-p))
  12814. (and (not org-support-shift-select) (org-at-item-p)))
  12815. (org-call-with-arg 'org-cycle-list-bullet nil))
  12816. ((and (not (eq org-support-shift-select 'always))
  12817. (org-at-property-p))
  12818. (call-interactively 'org-property-next-allowed-value))
  12819. ((org-clocktable-try-shift 'right arg))
  12820. (org-support-shift-select
  12821. (org-call-for-shift-select 'forward-char))
  12822. (t (org-shiftselect-error))))
  12823. (defun org-shiftleft (&optional arg)
  12824. "Cycle the thing at point or in the current line, depending on context.
  12825. Depending on context, this does one of the following:
  12826. - switch a timestamp at point one day into the past
  12827. - on a headline, switch to the previous TODO keyword.
  12828. - on an item, switch entire list to the previous bullet type
  12829. - on a property line, switch to the previous allowed value
  12830. - on a clocktable definition line, move time block into the past"
  12831. (interactive "P")
  12832. (cond
  12833. ((and org-support-shift-select (org-region-active-p))
  12834. (org-call-for-shift-select 'backward-char))
  12835. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12836. ((and (not (eq org-support-shift-select 'always))
  12837. (org-on-heading-p))
  12838. (org-call-with-arg 'org-todo 'left))
  12839. ((or (and org-support-shift-select
  12840. (not (eq org-support-shift-select 'always))
  12841. (org-at-item-bullet-p))
  12842. (and (not org-support-shift-select) (org-at-item-p)))
  12843. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12844. ((and (not (eq org-support-shift-select 'always))
  12845. (org-at-property-p))
  12846. (call-interactively 'org-property-previous-allowed-value))
  12847. ((org-clocktable-try-shift 'left arg))
  12848. (org-support-shift-select
  12849. (org-call-for-shift-select 'backward-char))
  12850. (t (org-shiftselect-error))))
  12851. (defun org-shiftcontrolright ()
  12852. "Switch to next TODO set."
  12853. (interactive)
  12854. (cond
  12855. ((and org-support-shift-select (org-region-active-p))
  12856. (org-call-for-shift-select 'forward-word))
  12857. ((and (not (eq org-support-shift-select 'always))
  12858. (org-on-heading-p))
  12859. (org-call-with-arg 'org-todo 'nextset))
  12860. (org-support-shift-select
  12861. (org-call-for-shift-select 'forward-word))
  12862. (t (org-shiftselect-error))))
  12863. (defun org-shiftcontrolleft ()
  12864. "Switch to previous TODO set."
  12865. (interactive)
  12866. (cond
  12867. ((and org-support-shift-select (org-region-active-p))
  12868. (org-call-for-shift-select 'backward-word))
  12869. ((and (not (eq org-support-shift-select 'always))
  12870. (org-on-heading-p))
  12871. (org-call-with-arg 'org-todo 'previousset))
  12872. (org-support-shift-select
  12873. (org-call-for-shift-select 'backward-word))
  12874. (t (org-shiftselect-error))))
  12875. (defun org-ctrl-c-ret ()
  12876. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12877. (interactive)
  12878. (cond
  12879. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12880. (t (call-interactively 'org-insert-heading))))
  12881. (defun org-copy-special ()
  12882. "Copy region in table or copy current subtree.
  12883. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12884. See the individual commands for more information."
  12885. (interactive)
  12886. (call-interactively
  12887. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12888. (defun org-cut-special ()
  12889. "Cut region in table or cut current subtree.
  12890. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12891. See the individual commands for more information."
  12892. (interactive)
  12893. (call-interactively
  12894. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12895. (defun org-paste-special (arg)
  12896. "Paste rectangular region into table, or past subtree relative to level.
  12897. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12898. See the individual commands for more information."
  12899. (interactive "P")
  12900. (if (org-at-table-p)
  12901. (org-table-paste-rectangle)
  12902. (org-paste-subtree arg)))
  12903. (defun org-edit-special ()
  12904. "Call a special editor for the stuff at point.
  12905. When at a table, call the formula editor with `org-table-edit-formulas'.
  12906. When at the first line of an src example, call `org-edit-src-code'.
  12907. When in an #+include line, visit the include file. Otherwise call
  12908. `ffap' to visit the file at point."
  12909. (interactive)
  12910. (cond
  12911. ((org-at-table-p)
  12912. (call-interactively 'org-table-edit-formulas))
  12913. ((save-excursion
  12914. (beginning-of-line 1)
  12915. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12916. (find-file (org-trim (match-string 1))))
  12917. ((org-edit-src-code))
  12918. ((org-edit-fixed-width-region))
  12919. (t (call-interactively 'ffap))))
  12920. (defun org-ctrl-c-ctrl-c (&optional arg)
  12921. "Set tags in headline, or update according to changed information at point.
  12922. This command does many different things, depending on context:
  12923. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12924. this is what we do.
  12925. - If the cursor is in a headline, prompt for tags and insert them
  12926. into the current line, aligned to `org-tags-column'. When called
  12927. with prefix arg, realign all tags in the current buffer.
  12928. - If the cursor is in one of the special #+KEYWORD lines, this
  12929. triggers scanning the buffer for these lines and updating the
  12930. information.
  12931. - If the cursor is inside a table, realign the table. This command
  12932. works even if the automatic table editor has been turned off.
  12933. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12934. the entire table.
  12935. - If the cursor is at a footnote reference or definition, jump to
  12936. the corresponding definition or references, respectively.
  12937. - If the cursor is a the beginning of a dynamic block, update it.
  12938. - If the cursor is inside a table created by the table.el package,
  12939. activate that table.
  12940. - If the current buffer is a remember buffer, close note and file
  12941. it. A prefix argument of 1 files to the default location
  12942. without further interaction. A prefix argument of 2 files to
  12943. the currently clocking task.
  12944. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12945. links in this buffer.
  12946. - If the cursor is on a numbered item in a plain list, renumber the
  12947. ordered list.
  12948. - If the cursor is on a checkbox, toggle it."
  12949. (interactive "P")
  12950. (let ((org-enable-table-editor t))
  12951. (cond
  12952. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12953. org-occur-highlights
  12954. org-latex-fragment-image-overlays)
  12955. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12956. (org-remove-occur-highlights)
  12957. (org-remove-latex-fragment-image-overlays)
  12958. (message "Temporary highlights/overlays removed from current buffer"))
  12959. ((and (local-variable-p 'org-finish-function (current-buffer))
  12960. (fboundp org-finish-function))
  12961. (funcall org-finish-function))
  12962. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12963. ((org-at-property-p)
  12964. (call-interactively 'org-property-action))
  12965. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12966. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12967. ((org-at-table.el-p)
  12968. (require 'table)
  12969. (beginning-of-line 1)
  12970. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12971. (call-interactively 'table-recognize-table))
  12972. ((org-at-table-p)
  12973. (org-table-maybe-eval-formula)
  12974. (if arg
  12975. (call-interactively 'org-table-recalculate)
  12976. (org-table-maybe-recalculate-line))
  12977. (call-interactively 'org-table-align))
  12978. ((or (org-footnote-at-reference-p)
  12979. (org-footnote-at-definition-p))
  12980. (call-interactively 'org-footnote-action))
  12981. ((org-at-item-checkbox-p)
  12982. (call-interactively 'org-toggle-checkbox))
  12983. ((org-at-item-p)
  12984. (if arg
  12985. (call-interactively 'org-toggle-checkbox)
  12986. (call-interactively 'org-maybe-renumber-ordered-list)))
  12987. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12988. ;; Dynamic block
  12989. (beginning-of-line 1)
  12990. (save-excursion (org-update-dblock)))
  12991. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12992. (cond
  12993. ((equal (match-string 1) "TBLFM")
  12994. ;; Recalculate the table before this line
  12995. (save-excursion
  12996. (beginning-of-line 1)
  12997. (skip-chars-backward " \r\n\t")
  12998. (if (org-at-table-p)
  12999. (org-call-with-arg 'org-table-recalculate t))))
  13000. (t
  13001. ; (org-set-regexps-and-options)
  13002. ; (org-restart-font-lock)
  13003. (let ((org-inhibit-startup t)) (org-mode-restart))
  13004. (message "Local setup has been refreshed"))))
  13005. (t (error "C-c C-c can do nothing useful at this location.")))))
  13006. (defun org-mode-restart ()
  13007. "Restart Org-mode, to scan again for special lines.
  13008. Also updates the keyword regular expressions."
  13009. (interactive)
  13010. (org-mode)
  13011. (message "Org-mode restarted"))
  13012. (defun org-kill-note-or-show-branches ()
  13013. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13014. (interactive)
  13015. (if (not org-finish-function)
  13016. (call-interactively 'show-branches)
  13017. (let ((org-note-abort t))
  13018. (funcall org-finish-function))))
  13019. (defun org-return (&optional indent)
  13020. "Goto next table row or insert a newline.
  13021. Calls `org-table-next-row' or `newline', depending on context.
  13022. See the individual commands for more information."
  13023. (interactive)
  13024. (cond
  13025. ((bobp) (if indent (newline-and-indent) (newline)))
  13026. ((org-at-table-p)
  13027. (org-table-justify-field-maybe)
  13028. (call-interactively 'org-table-next-row))
  13029. ((and org-return-follows-link
  13030. (eq (get-text-property (point) 'face) 'org-link))
  13031. (call-interactively 'org-open-at-point))
  13032. ((and (org-at-heading-p)
  13033. (looking-at
  13034. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13035. (org-show-entry)
  13036. (end-of-line 1)
  13037. (newline))
  13038. (t (if indent (newline-and-indent) (newline)))))
  13039. (defun org-return-indent ()
  13040. "Goto next table row or insert a newline and indent.
  13041. Calls `org-table-next-row' or `newline-and-indent', depending on
  13042. context. See the individual commands for more information."
  13043. (interactive)
  13044. (org-return t))
  13045. (defun org-ctrl-c-star ()
  13046. "Compute table, or change heading status of lines.
  13047. Calls `org-table-recalculate' or `org-toggle-heading',
  13048. depending on context."
  13049. (interactive)
  13050. (cond
  13051. ((org-at-table-p)
  13052. (call-interactively 'org-table-recalculate))
  13053. (t
  13054. ;; Convert all lines in region to list items
  13055. (call-interactively 'org-toggle-heading))))
  13056. (defun org-ctrl-c-minus ()
  13057. "Insert separator line in table or modify bullet status of line.
  13058. Also turns a plain line or a region of lines into list items.
  13059. Calls `org-table-insert-hline', `org-toggle-item', or
  13060. `org-cycle-list-bullet', depending on context."
  13061. (interactive)
  13062. (cond
  13063. ((org-at-table-p)
  13064. (call-interactively 'org-table-insert-hline))
  13065. ((org-region-active-p)
  13066. (call-interactively 'org-toggle-item))
  13067. ((org-in-item-p)
  13068. (call-interactively 'org-cycle-list-bullet))
  13069. (t
  13070. (call-interactively 'org-toggle-item))))
  13071. (defun org-toggle-item ()
  13072. "Convert headings or normal lines to items, items to normal lines.
  13073. If there is no active region, only the current line is considered.
  13074. If the first line in the region is a headline, convert all headlines to items.
  13075. If the first line in the region is an item, convert all items to normal lines.
  13076. If the first line is normal text, add an item bullet to each line."
  13077. (interactive)
  13078. (let (l2 l beg end)
  13079. (if (org-region-active-p)
  13080. (setq beg (region-beginning) end (region-end))
  13081. (setq beg (point-at-bol)
  13082. end (min (1+ (point-at-eol)) (point-max))))
  13083. (save-excursion
  13084. (goto-char end)
  13085. (setq l2 (org-current-line))
  13086. (goto-char beg)
  13087. (beginning-of-line 1)
  13088. (setq l (1- (org-current-line)))
  13089. (if (org-at-item-p)
  13090. ;; We already have items, de-itemize
  13091. (while (< (setq l (1+ l)) l2)
  13092. (when (org-at-item-p)
  13093. (goto-char (match-beginning 2))
  13094. (delete-region (match-beginning 2) (match-end 2))
  13095. (and (looking-at "[ \t]+") (replace-match "")))
  13096. (beginning-of-line 2))
  13097. (if (org-on-heading-p)
  13098. ;; Headings, convert to items
  13099. (while (< (setq l (1+ l)) l2)
  13100. (if (looking-at org-outline-regexp)
  13101. (replace-match "- " t t))
  13102. (beginning-of-line 2))
  13103. ;; normal lines, turn them into items
  13104. (while (< (setq l (1+ l)) l2)
  13105. (unless (org-at-item-p)
  13106. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13107. (replace-match "\\1- \\2")))
  13108. (beginning-of-line 2)))))))
  13109. (defun org-toggle-heading (&optional nstars)
  13110. "Convert headings to normal text, or items or text to headings.
  13111. If there is no active region, only the current line is considered.
  13112. If the first line is a heading, remove the stars from all headlines
  13113. in the region.
  13114. If the first line is a plain list item, turn all plain list items into
  13115. headings.
  13116. If the first line is a normal line, turn each and every line in the region
  13117. into a heading.
  13118. When converting a line into a heading, the number of stars is chosen
  13119. such that the lines become children of the current entry. However, when
  13120. a prefix argument is given, its value determines the number of stars to add."
  13121. (interactive "P")
  13122. (let (l2 l itemp beg end)
  13123. (if (org-region-active-p)
  13124. (setq beg (region-beginning) end (region-end))
  13125. (setq beg (point-at-bol)
  13126. end (min (1+ (point-at-eol)) (point-max))))
  13127. (save-excursion
  13128. (goto-char end)
  13129. (setq l2 (org-current-line))
  13130. (goto-char beg)
  13131. (beginning-of-line 1)
  13132. (setq l (1- (org-current-line)))
  13133. (if (org-on-heading-p)
  13134. ;; We already have headlines, de-star them
  13135. (while (< (setq l (1+ l)) l2)
  13136. (when (org-on-heading-p t)
  13137. (and (looking-at outline-regexp) (replace-match "")))
  13138. (beginning-of-line 2))
  13139. (setq itemp (org-at-item-p))
  13140. (let* ((stars
  13141. (if nstars
  13142. (make-string (prefix-numeric-value current-prefix-arg)
  13143. ?*)
  13144. (save-excursion
  13145. (re-search-backward org-complex-heading-regexp nil t)
  13146. (or (match-string 1) ""))))
  13147. (add-stars (cond (nstars "")
  13148. ((equal stars "") "*")
  13149. (org-odd-levels-only "**")
  13150. (t "*")))
  13151. (rpl (concat stars add-stars " ")))
  13152. (while (< (setq l (1+ l)) l2)
  13153. (if itemp
  13154. (and (org-at-item-p) (replace-match rpl t t))
  13155. (unless (org-on-heading-p)
  13156. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13157. (replace-match (concat rpl (match-string 2))))))
  13158. (beginning-of-line 2)))))))
  13159. (defun org-meta-return (&optional arg)
  13160. "Insert a new heading or wrap a region in a table.
  13161. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13162. See the individual commands for more information."
  13163. (interactive "P")
  13164. (cond
  13165. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13166. ((org-at-table-p)
  13167. (call-interactively 'org-table-wrap-region))
  13168. (t (call-interactively 'org-insert-heading))))
  13169. ;;; Menu entries
  13170. ;; Define the Org-mode menus
  13171. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13172. '("Tbl"
  13173. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13174. ["Next Field" org-cycle (org-at-table-p)]
  13175. ["Previous Field" org-shifttab (org-at-table-p)]
  13176. ["Next Row" org-return (org-at-table-p)]
  13177. "--"
  13178. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13179. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13180. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13181. "--"
  13182. ("Column"
  13183. ["Move Column Left" org-metaleft (org-at-table-p)]
  13184. ["Move Column Right" org-metaright (org-at-table-p)]
  13185. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13186. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13187. ("Row"
  13188. ["Move Row Up" org-metaup (org-at-table-p)]
  13189. ["Move Row Down" org-metadown (org-at-table-p)]
  13190. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13191. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13192. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13193. "--"
  13194. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13195. ("Rectangle"
  13196. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13197. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13198. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13199. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13200. "--"
  13201. ("Calculate"
  13202. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13203. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13204. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13205. "--"
  13206. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13207. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13208. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13209. "--"
  13210. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13211. "--"
  13212. ["Sum Column/Rectangle" org-table-sum
  13213. (or (org-at-table-p) (org-region-active-p))]
  13214. ["Which Column?" org-table-current-column (org-at-table-p)])
  13215. ["Debug Formulas"
  13216. org-table-toggle-formula-debugger
  13217. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13218. ["Show Col/Row Numbers"
  13219. org-table-toggle-coordinate-overlays
  13220. :style toggle
  13221. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13222. "--"
  13223. ["Create" org-table-create (and (not (org-at-table-p))
  13224. org-enable-table-editor)]
  13225. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13226. ["Import from File" org-table-import (not (org-at-table-p))]
  13227. ["Export to File" org-table-export (org-at-table-p)]
  13228. "--"
  13229. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13230. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13231. '("Org"
  13232. ("Show/Hide"
  13233. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13234. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13235. ["Sparse Tree..." org-sparse-tree t]
  13236. ["Reveal Context" org-reveal t]
  13237. ["Show All" show-all t]
  13238. "--"
  13239. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13240. "--"
  13241. ["New Heading" org-insert-heading t]
  13242. ("Navigate Headings"
  13243. ["Up" outline-up-heading t]
  13244. ["Next" outline-next-visible-heading t]
  13245. ["Previous" outline-previous-visible-heading t]
  13246. ["Next Same Level" outline-forward-same-level t]
  13247. ["Previous Same Level" outline-backward-same-level t]
  13248. "--"
  13249. ["Jump" org-goto t])
  13250. ("Edit Structure"
  13251. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13252. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13253. "--"
  13254. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13255. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13256. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13257. "--"
  13258. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13259. "--"
  13260. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13261. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13262. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13263. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13264. "--"
  13265. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13266. "--"
  13267. ["Convert to odd levels" org-convert-to-odd-levels t]
  13268. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13269. ("Editing"
  13270. ["Emphasis..." org-emphasize t]
  13271. ["Edit Source Example" org-edit-special t]
  13272. "--"
  13273. ["Footnote new/jump" org-footnote-action t]
  13274. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13275. ("Archive"
  13276. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13277. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13278. ; :active t :keys "C-u C-c C-x C-a"]
  13279. ["Sparse trees open ARCHIVE trees"
  13280. (setq org-sparse-tree-open-archived-trees
  13281. (not org-sparse-tree-open-archived-trees))
  13282. :style toggle :selected org-sparse-tree-open-archived-trees]
  13283. ["Cycling opens ARCHIVE trees"
  13284. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13285. :style toggle :selected org-cycle-open-archived-trees]
  13286. "--"
  13287. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13288. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13289. ; ["Check and Move Children" (org-archive-subtree '(4))
  13290. ; :active t :keys "C-u C-c C-x C-s"]
  13291. )
  13292. "--"
  13293. ("Hyperlinks"
  13294. ["Store Link (Global)" org-store-link t]
  13295. ["Insert Link" org-insert-link t]
  13296. ["Follow Link" org-open-at-point t]
  13297. "--"
  13298. ["Next link" org-next-link t]
  13299. ["Previous link" org-previous-link t]
  13300. "--"
  13301. ["Descriptive Links"
  13302. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13303. :style radio
  13304. :selected (member '(org-link) buffer-invisibility-spec)]
  13305. ["Literal Links"
  13306. (progn
  13307. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13308. :style radio
  13309. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13310. "--"
  13311. ("TODO Lists"
  13312. ["TODO/DONE/-" org-todo t]
  13313. ("Select keyword"
  13314. ["Next keyword" org-shiftright (org-on-heading-p)]
  13315. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13316. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13317. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13318. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13319. ["Show TODO Tree" org-show-todo-tree t]
  13320. ["Global TODO list" org-todo-list t]
  13321. "--"
  13322. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13323. :selected org-enforce-todo-dependencies :style toggle :active t]
  13324. "Settings for tree at point"
  13325. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13326. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13327. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13328. ["Do Children parallel" org-toggle-ordered-property :style radio
  13329. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13330. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13331. "--"
  13332. ["Set Priority" org-priority t]
  13333. ["Priority Up" org-shiftup t]
  13334. ["Priority Down" org-shiftdown t]
  13335. "--"
  13336. ["Get news from all feeds" org-feed-update-all t]
  13337. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13338. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13339. ("TAGS and Properties"
  13340. ["Set Tags" org-set-tags-command t]
  13341. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13342. "--"
  13343. ["Set property" org-set-property t]
  13344. ["Column view of properties" org-columns t]
  13345. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13346. ("Dates and Scheduling"
  13347. ["Timestamp" org-time-stamp t]
  13348. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13349. ("Change Date"
  13350. ["1 Day Later" org-shiftright t]
  13351. ["1 Day Earlier" org-shiftleft t]
  13352. ["1 ... Later" org-shiftup t]
  13353. ["1 ... Earlier" org-shiftdown t])
  13354. ["Compute Time Range" org-evaluate-time-range t]
  13355. ["Schedule Item" org-schedule t]
  13356. ["Deadline" org-deadline t]
  13357. "--"
  13358. ["Custom time format" org-toggle-time-stamp-overlays
  13359. :style radio :selected org-display-custom-times]
  13360. "--"
  13361. ["Goto Calendar" org-goto-calendar t]
  13362. ["Date from Calendar" org-date-from-calendar t]
  13363. "--"
  13364. ["Start/Restart Timer" org-timer-start t]
  13365. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13366. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13367. ["Insert Timer String" org-timer t]
  13368. ["Insert Timer Item" org-timer-item t])
  13369. ("Logging work"
  13370. ["Clock in" org-clock-in t]
  13371. ["Clock out" org-clock-out t]
  13372. ["Clock cancel" org-clock-cancel t]
  13373. ["Goto running clock" org-clock-goto t]
  13374. ["Display times" org-clock-display t]
  13375. ["Create clock table" org-clock-report t]
  13376. "--"
  13377. ["Record DONE time"
  13378. (progn (setq org-log-done (not org-log-done))
  13379. (message "Switching to %s will %s record a timestamp"
  13380. (car org-done-keywords)
  13381. (if org-log-done "automatically" "not")))
  13382. :style toggle :selected org-log-done])
  13383. "--"
  13384. ["Agenda Command..." org-agenda t]
  13385. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13386. ("File List for Agenda")
  13387. ("Special views current file"
  13388. ["TODO Tree" org-show-todo-tree t]
  13389. ["Check Deadlines" org-check-deadlines t]
  13390. ["Timeline" org-timeline t]
  13391. ["Tags/Property tree" org-match-sparse-tree t])
  13392. "--"
  13393. ["Export/Publish..." org-export t]
  13394. ("LaTeX"
  13395. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13396. :selected org-cdlatex-mode]
  13397. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13398. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13399. ["Modify math symbol" org-cdlatex-math-modify
  13400. (org-inside-LaTeX-fragment-p)]
  13401. ["Insert citation" org-reftex-citation t]
  13402. "--"
  13403. ["Export LaTeX fragments as images"
  13404. (if (featurep 'org-exp)
  13405. (setq org-export-with-LaTeX-fragments
  13406. (not org-export-with-LaTeX-fragments))
  13407. (require 'org-exp))
  13408. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13409. org-export-with-LaTeX-fragments)])
  13410. "--"
  13411. ("Documentation"
  13412. ["Show Version" org-version t]
  13413. ["Info Documentation" org-info t])
  13414. ("Customize"
  13415. ["Browse Org Group" org-customize t]
  13416. "--"
  13417. ["Expand This Menu" org-create-customize-menu
  13418. (fboundp 'customize-menu-create)])
  13419. "--"
  13420. ("Refresh/Reload"
  13421. ["Refresh setup current buffer" org-mode-restart t]
  13422. ["Reload Org (after update)" org-reload t]
  13423. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13424. ))
  13425. (defun org-info (&optional node)
  13426. "Read documentation for Org-mode in the info system.
  13427. With optional NODE, go directly to that node."
  13428. (interactive)
  13429. (info (format "(org)%s" (or node ""))))
  13430. (defun org-install-agenda-files-menu ()
  13431. (let ((bl (buffer-list)))
  13432. (save-excursion
  13433. (while bl
  13434. (set-buffer (pop bl))
  13435. (if (org-mode-p) (setq bl nil)))
  13436. (when (org-mode-p)
  13437. (easy-menu-change
  13438. '("Org") "File List for Agenda"
  13439. (append
  13440. (list
  13441. ["Edit File List" (org-edit-agenda-file-list) t]
  13442. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13443. ["Remove Current File from List" org-remove-file t]
  13444. ["Cycle through agenda files" org-cycle-agenda-files t]
  13445. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13446. "--")
  13447. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13448. ;;;; Documentation
  13449. ;;;###autoload
  13450. (defun org-require-autoloaded-modules ()
  13451. (interactive)
  13452. (mapc 'require
  13453. '(org-agenda org-archive org-attach org-clock org-colview
  13454. org-exp org-id org-export-latex org-docbook
  13455. org-publish org-remember org-table org-timer)))
  13456. ;;;###autoload
  13457. (defun org-reload (&optional uncompiled)
  13458. "Reload all org lisp files.
  13459. With prefix arg UNCOMPILED, load the uncompiled versions."
  13460. (interactive "P")
  13461. (require 'find-func)
  13462. (let* ((dir (file-name-directory (find-library-name "org")))
  13463. (files (directory-files dir t "\\.el\\'"))
  13464. (remove-re (concat (if (featurep 'xemacs)
  13465. "org-colview" "org-colview-xemacs")
  13466. "\\'")))
  13467. (setq files (mapcar 'file-name-sans-extension files))
  13468. (setq files (mapcar
  13469. (lambda (x) (if (string-match remove-re x) nil x))
  13470. files))
  13471. (setq files (delq nil files))
  13472. (mapc
  13473. (lambda (f)
  13474. (if (and (not uncompiled)
  13475. (file-exists-p (concat f ".elc")))
  13476. (load (concat f ".elc") nil nil t)
  13477. (load (concat f ".el") nil nil t)))
  13478. files)))
  13479. ;;;###autoload
  13480. (defun org-customize ()
  13481. "Call the customize function with org as argument."
  13482. (interactive)
  13483. (org-load-modules-maybe)
  13484. (org-require-autoloaded-modules)
  13485. (customize-browse 'org))
  13486. (defun org-create-customize-menu ()
  13487. "Create a full customization menu for Org-mode, insert it into the menu."
  13488. (interactive)
  13489. (org-load-modules-maybe)
  13490. (org-require-autoloaded-modules)
  13491. (if (fboundp 'customize-menu-create)
  13492. (progn
  13493. (easy-menu-change
  13494. '("Org") "Customize"
  13495. `(["Browse Org group" org-customize t]
  13496. "--"
  13497. ,(customize-menu-create 'org)
  13498. ["Set" Custom-set t]
  13499. ["Save" Custom-save t]
  13500. ["Reset to Current" Custom-reset-current t]
  13501. ["Reset to Saved" Custom-reset-saved t]
  13502. ["Reset to Standard Settings" Custom-reset-standard t]))
  13503. (message "\"Org\"-menu now contains full customization menu"))
  13504. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13505. ;;;; Miscellaneous stuff
  13506. ;;; Generally useful functions
  13507. (defun org-find-text-property-in-string (prop s)
  13508. "Return the first non-nil value of property PROP in string S."
  13509. (or (get-text-property 0 prop s)
  13510. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13511. prop s)))
  13512. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13513. "Display the given MESSAGE as a warning."
  13514. (if (fboundp 'display-warning)
  13515. (display-warning 'org message
  13516. (if (featurep 'xemacs)
  13517. 'warning
  13518. :warning))
  13519. (let ((buf (get-buffer-create "*Org warnings*")))
  13520. (with-current-buffer buf
  13521. (goto-char (point-max))
  13522. (insert "Warning (Org): " message)
  13523. (unless (bolp)
  13524. (newline)))
  13525. (display-buffer buf)
  13526. (sit-for 0))))
  13527. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13528. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13529. (if (and marker (marker-buffer marker)
  13530. (buffer-live-p (marker-buffer marker)))
  13531. (progn
  13532. (switch-to-buffer (marker-buffer marker))
  13533. (if (or (> marker (point-max)) (< marker (point-min)))
  13534. (widen))
  13535. (goto-char marker)
  13536. (org-show-context 'org-goto))
  13537. (if bookmark
  13538. (bookmark-jump bookmark)
  13539. (error "Cannot find location"))))
  13540. (defun org-quote-csv-field (s)
  13541. "Quote field for inclusion in CSV material."
  13542. (if (string-match "[\",]" s)
  13543. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13544. s))
  13545. (defun org-plist-delete (plist property)
  13546. "Delete PROPERTY from PLIST.
  13547. This is in contrast to merely setting it to 0."
  13548. (let (p)
  13549. (while plist
  13550. (if (not (eq property (car plist)))
  13551. (setq p (plist-put p (car plist) (nth 1 plist))))
  13552. (setq plist (cddr plist)))
  13553. p))
  13554. (defun org-force-self-insert (N)
  13555. "Needed to enforce self-insert under remapping."
  13556. (interactive "p")
  13557. (self-insert-command N))
  13558. (defun org-string-width (s)
  13559. "Compute width of string, ignoring invisible characters.
  13560. This ignores character with invisibility property `org-link', and also
  13561. characters with property `org-cwidth', because these will become invisible
  13562. upon the next fontification round."
  13563. (let (b l)
  13564. (when (or (eq t buffer-invisibility-spec)
  13565. (assq 'org-link buffer-invisibility-spec))
  13566. (while (setq b (text-property-any 0 (length s)
  13567. 'invisible 'org-link s))
  13568. (setq s (concat (substring s 0 b)
  13569. (substring s (or (next-single-property-change
  13570. b 'invisible s) (length s)))))))
  13571. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13572. (setq s (concat (substring s 0 b)
  13573. (substring s (or (next-single-property-change
  13574. b 'org-cwidth s) (length s))))))
  13575. (setq l (string-width s) b -1)
  13576. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13577. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13578. l))
  13579. (defun org-get-indentation (&optional line)
  13580. "Get the indentation of the current line, interpreting tabs.
  13581. When LINE is given, assume it represents a line and compute its indentation."
  13582. (if line
  13583. (if (string-match "^ *" (org-remove-tabs line))
  13584. (match-end 0))
  13585. (save-excursion
  13586. (beginning-of-line 1)
  13587. (skip-chars-forward " \t")
  13588. (current-column))))
  13589. (defun org-remove-tabs (s &optional width)
  13590. "Replace tabulators in S with spaces.
  13591. Assumes that s is a single line, starting in column 0."
  13592. (setq width (or width tab-width))
  13593. (while (string-match "\t" s)
  13594. (setq s (replace-match
  13595. (make-string
  13596. (- (* width (/ (+ (match-beginning 0) width) width))
  13597. (match-beginning 0)) ?\ )
  13598. t t s)))
  13599. s)
  13600. (defun org-fix-indentation (line ind)
  13601. "Fix indentation in LINE.
  13602. IND is a cons cell with target and minimum indentation.
  13603. If the current indentation in LINE is smaller than the minimum,
  13604. leave it alone. If it is larger than ind, set it to the target."
  13605. (let* ((l (org-remove-tabs line))
  13606. (i (org-get-indentation l))
  13607. (i1 (car ind)) (i2 (cdr ind)))
  13608. (if (>= i i2) (setq l (substring line i2)))
  13609. (if (> i1 0)
  13610. (concat (make-string i1 ?\ ) l)
  13611. l)))
  13612. (defun org-base-buffer (buffer)
  13613. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13614. (if (not buffer)
  13615. buffer
  13616. (or (buffer-base-buffer buffer)
  13617. buffer)))
  13618. (defun org-trim (s)
  13619. "Remove whitespace at beginning and end of string."
  13620. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13621. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13622. s)
  13623. (defun org-wrap (string &optional width lines)
  13624. "Wrap string to either a number of lines, or a width in characters.
  13625. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13626. that costs. If there is a word longer than WIDTH, the text is actually
  13627. wrapped to the length of that word.
  13628. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13629. many lines, whatever width that takes.
  13630. The return value is a list of lines, without newlines at the end."
  13631. (let* ((words (org-split-string string "[ \t\n]+"))
  13632. (maxword (apply 'max (mapcar 'org-string-width words)))
  13633. w ll)
  13634. (cond (width
  13635. (org-do-wrap words (max maxword width)))
  13636. (lines
  13637. (setq w maxword)
  13638. (setq ll (org-do-wrap words maxword))
  13639. (if (<= (length ll) lines)
  13640. ll
  13641. (setq ll words)
  13642. (while (> (length ll) lines)
  13643. (setq w (1+ w))
  13644. (setq ll (org-do-wrap words w)))
  13645. ll))
  13646. (t (error "Cannot wrap this")))))
  13647. (defun org-do-wrap (words width)
  13648. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13649. (let (lines line)
  13650. (while words
  13651. (setq line (pop words))
  13652. (while (and words (< (+ (length line) (length (car words))) width))
  13653. (setq line (concat line " " (pop words))))
  13654. (setq lines (push line lines)))
  13655. (nreverse lines)))
  13656. (defun org-split-string (string &optional separators)
  13657. "Splits STRING into substrings at SEPARATORS.
  13658. No empty strings are returned if there are matches at the beginning
  13659. and end of string."
  13660. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13661. (start 0)
  13662. notfirst
  13663. (list nil))
  13664. (while (and (string-match rexp string
  13665. (if (and notfirst
  13666. (= start (match-beginning 0))
  13667. (< start (length string)))
  13668. (1+ start) start))
  13669. (< (match-beginning 0) (length string)))
  13670. (setq notfirst t)
  13671. (or (eq (match-beginning 0) 0)
  13672. (and (eq (match-beginning 0) (match-end 0))
  13673. (eq (match-beginning 0) start))
  13674. (setq list
  13675. (cons (substring string start (match-beginning 0))
  13676. list)))
  13677. (setq start (match-end 0)))
  13678. (or (eq start (length string))
  13679. (setq list
  13680. (cons (substring string start)
  13681. list)))
  13682. (nreverse list)))
  13683. (defun org-uuidgen-p (s)
  13684. "Is S an ID created by UUIDGEN?"
  13685. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13686. (defun org-context ()
  13687. "Return a list of contexts of the current cursor position.
  13688. If several contexts apply, all are returned.
  13689. Each context entry is a list with a symbol naming the context, and
  13690. two positions indicating start and end of the context. Possible
  13691. contexts are:
  13692. :headline anywhere in a headline
  13693. :headline-stars on the leading stars in a headline
  13694. :todo-keyword on a TODO keyword (including DONE) in a headline
  13695. :tags on the TAGS in a headline
  13696. :priority on the priority cookie in a headline
  13697. :item on the first line of a plain list item
  13698. :item-bullet on the bullet/number of a plain list item
  13699. :checkbox on the checkbox in a plain list item
  13700. :table in an org-mode table
  13701. :table-special on a special filed in a table
  13702. :table-table in a table.el table
  13703. :link on a hyperlink
  13704. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13705. :target on a <<target>>
  13706. :radio-target on a <<<radio-target>>>
  13707. :latex-fragment on a LaTeX fragment
  13708. :latex-preview on a LaTeX fragment with overlayed preview image
  13709. This function expects the position to be visible because it uses font-lock
  13710. faces as a help to recognize the following contexts: :table-special, :link,
  13711. and :keyword."
  13712. (let* ((f (get-text-property (point) 'face))
  13713. (faces (if (listp f) f (list f)))
  13714. (p (point)) clist o)
  13715. ;; First the large context
  13716. (cond
  13717. ((org-on-heading-p t)
  13718. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13719. (when (progn
  13720. (beginning-of-line 1)
  13721. (looking-at org-todo-line-tags-regexp))
  13722. (push (org-point-in-group p 1 :headline-stars) clist)
  13723. (push (org-point-in-group p 2 :todo-keyword) clist)
  13724. (push (org-point-in-group p 4 :tags) clist))
  13725. (goto-char p)
  13726. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13727. (if (looking-at "\\[#[A-Z0-9]\\]")
  13728. (push (org-point-in-group p 0 :priority) clist)))
  13729. ((org-at-item-p)
  13730. (push (org-point-in-group p 2 :item-bullet) clist)
  13731. (push (list :item (point-at-bol)
  13732. (save-excursion (org-end-of-item) (point)))
  13733. clist)
  13734. (and (org-at-item-checkbox-p)
  13735. (push (org-point-in-group p 0 :checkbox) clist)))
  13736. ((org-at-table-p)
  13737. (push (list :table (org-table-begin) (org-table-end)) clist)
  13738. (if (memq 'org-formula faces)
  13739. (push (list :table-special
  13740. (previous-single-property-change p 'face)
  13741. (next-single-property-change p 'face)) clist)))
  13742. ((org-at-table-p 'any)
  13743. (push (list :table-table) clist)))
  13744. (goto-char p)
  13745. ;; Now the small context
  13746. (cond
  13747. ((org-at-timestamp-p)
  13748. (push (org-point-in-group p 0 :timestamp) clist))
  13749. ((memq 'org-link faces)
  13750. (push (list :link
  13751. (previous-single-property-change p 'face)
  13752. (next-single-property-change p 'face)) clist))
  13753. ((memq 'org-special-keyword faces)
  13754. (push (list :keyword
  13755. (previous-single-property-change p 'face)
  13756. (next-single-property-change p 'face)) clist))
  13757. ((org-on-target-p)
  13758. (push (org-point-in-group p 0 :target) clist)
  13759. (goto-char (1- (match-beginning 0)))
  13760. (if (looking-at org-radio-target-regexp)
  13761. (push (org-point-in-group p 0 :radio-target) clist))
  13762. (goto-char p))
  13763. ((setq o (car (delq nil
  13764. (mapcar
  13765. (lambda (x)
  13766. (if (memq x org-latex-fragment-image-overlays) x))
  13767. (org-overlays-at (point))))))
  13768. (push (list :latex-fragment
  13769. (org-overlay-start o) (org-overlay-end o)) clist)
  13770. (push (list :latex-preview
  13771. (org-overlay-start o) (org-overlay-end o)) clist))
  13772. ((org-inside-LaTeX-fragment-p)
  13773. ;; FIXME: positions wrong.
  13774. (push (list :latex-fragment (point) (point)) clist)))
  13775. (setq clist (nreverse (delq nil clist)))
  13776. clist))
  13777. ;; FIXME: Compare with at-regexp-p Do we need both?
  13778. (defun org-in-regexp (re &optional nlines visually)
  13779. "Check if point is inside a match of regexp.
  13780. Normally only the current line is checked, but you can include NLINES extra
  13781. lines both before and after point into the search.
  13782. If VISUALLY is set, require that the cursor is not after the match but
  13783. really on, so that the block visually is on the match."
  13784. (catch 'exit
  13785. (let ((pos (point))
  13786. (eol (point-at-eol (+ 1 (or nlines 0))))
  13787. (inc (if visually 1 0)))
  13788. (save-excursion
  13789. (beginning-of-line (- 1 (or nlines 0)))
  13790. (while (re-search-forward re eol t)
  13791. (if (and (<= (match-beginning 0) pos)
  13792. (>= (+ inc (match-end 0)) pos))
  13793. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13794. (defun org-at-regexp-p (regexp)
  13795. "Is point inside a match of REGEXP in the current line?"
  13796. (catch 'exit
  13797. (save-excursion
  13798. (let ((pos (point)) (end (point-at-eol)))
  13799. (beginning-of-line 1)
  13800. (while (re-search-forward regexp end t)
  13801. (if (and (<= (match-beginning 0) pos)
  13802. (>= (match-end 0) pos))
  13803. (throw 'exit t)))
  13804. nil))))
  13805. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13806. "Call `multi-occur' with buffers for all agenda files."
  13807. (interactive "sOrg-files matching: \np")
  13808. (let* ((files (org-agenda-files))
  13809. (tnames (mapcar 'file-truename files))
  13810. (extra org-agenda-text-search-extra-files)
  13811. f)
  13812. (when (eq (car extra) 'agenda-archives)
  13813. (setq extra (cdr extra))
  13814. (setq files (org-add-archive-files files)))
  13815. (while (setq f (pop extra))
  13816. (unless (member (file-truename f) tnames)
  13817. (add-to-list 'files f 'append)
  13818. (add-to-list 'tnames (file-truename f) 'append)))
  13819. (multi-occur
  13820. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13821. regexp)))
  13822. (if (boundp 'occur-mode-find-occurrence-hook)
  13823. ;; Emacs 23
  13824. (add-hook 'occur-mode-find-occurrence-hook
  13825. (lambda ()
  13826. (when (org-mode-p)
  13827. (org-reveal))))
  13828. ;; Emacs 22
  13829. (defadvice occur-mode-goto-occurrence
  13830. (after org-occur-reveal activate)
  13831. (and (org-mode-p) (org-reveal)))
  13832. (defadvice occur-mode-goto-occurrence-other-window
  13833. (after org-occur-reveal activate)
  13834. (and (org-mode-p) (org-reveal)))
  13835. (defadvice occur-mode-display-occurrence
  13836. (after org-occur-reveal activate)
  13837. (when (org-mode-p)
  13838. (let ((pos (occur-mode-find-occurrence)))
  13839. (with-current-buffer (marker-buffer pos)
  13840. (save-excursion
  13841. (goto-char pos)
  13842. (org-reveal)))))))
  13843. (defun org-uniquify (list)
  13844. "Remove duplicate elements from LIST."
  13845. (let (res)
  13846. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13847. res))
  13848. (defun org-delete-all (elts list)
  13849. "Remove all elements in ELTS from LIST."
  13850. (while elts
  13851. (setq list (delete (pop elts) list)))
  13852. list)
  13853. (defun org-back-over-empty-lines ()
  13854. "Move backwards over whitespace, to the beginning of the first empty line.
  13855. Returns the number of empty lines passed."
  13856. (let ((pos (point)))
  13857. (skip-chars-backward " \t\n\r")
  13858. (beginning-of-line 2)
  13859. (goto-char (min (point) pos))
  13860. (count-lines (point) pos)))
  13861. (defun org-skip-whitespace ()
  13862. (skip-chars-forward " \t\n\r"))
  13863. (defun org-point-in-group (point group &optional context)
  13864. "Check if POINT is in match-group GROUP.
  13865. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13866. match. If the match group does ot exist or point is not inside it,
  13867. return nil."
  13868. (and (match-beginning group)
  13869. (>= point (match-beginning group))
  13870. (<= point (match-end group))
  13871. (if context
  13872. (list context (match-beginning group) (match-end group))
  13873. t)))
  13874. (defun org-switch-to-buffer-other-window (&rest args)
  13875. "Switch to buffer in a second window on the current frame.
  13876. In particular, do not allow pop-up frames."
  13877. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13878. special-display-function)
  13879. (apply 'switch-to-buffer-other-window args)))
  13880. (defun org-combine-plists (&rest plists)
  13881. "Create a single property list from all plists in PLISTS.
  13882. The process starts by copying the first list, and then setting properties
  13883. from the other lists. Settings in the last list are the most significant
  13884. ones and overrule settings in the other lists."
  13885. (let ((rtn (copy-sequence (pop plists)))
  13886. p v ls)
  13887. (while plists
  13888. (setq ls (pop plists))
  13889. (while ls
  13890. (setq p (pop ls) v (pop ls))
  13891. (setq rtn (plist-put rtn p v))))
  13892. rtn))
  13893. (defun org-move-line-down (arg)
  13894. "Move the current line down. With prefix argument, move it past ARG lines."
  13895. (interactive "p")
  13896. (let ((col (current-column))
  13897. beg end pos)
  13898. (beginning-of-line 1) (setq beg (point))
  13899. (beginning-of-line 2) (setq end (point))
  13900. (beginning-of-line (+ 1 arg))
  13901. (setq pos (move-marker (make-marker) (point)))
  13902. (insert (delete-and-extract-region beg end))
  13903. (goto-char pos)
  13904. (org-move-to-column col)))
  13905. (defun org-move-line-up (arg)
  13906. "Move the current line up. With prefix argument, move it past ARG lines."
  13907. (interactive "p")
  13908. (let ((col (current-column))
  13909. beg end pos)
  13910. (beginning-of-line 1) (setq beg (point))
  13911. (beginning-of-line 2) (setq end (point))
  13912. (beginning-of-line (- arg))
  13913. (setq pos (move-marker (make-marker) (point)))
  13914. (insert (delete-and-extract-region beg end))
  13915. (goto-char pos)
  13916. (org-move-to-column col)))
  13917. (defun org-replace-escapes (string table)
  13918. "Replace %-escapes in STRING with values in TABLE.
  13919. TABLE is an association list with keys like \"%a\" and string values.
  13920. The sequences in STRING may contain normal field width and padding information,
  13921. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13922. so values can contain further %-escapes if they are define later in TABLE."
  13923. (let ((case-fold-search nil)
  13924. e re rpl)
  13925. (while (setq e (pop table))
  13926. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13927. (while (string-match re string)
  13928. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13929. (cdr e)))
  13930. (setq string (replace-match rpl t t string))))
  13931. string))
  13932. (defun org-sublist (list start end)
  13933. "Return a section of LIST, from START to END.
  13934. Counting starts at 1."
  13935. (let (rtn (c start))
  13936. (setq list (nthcdr (1- start) list))
  13937. (while (and list (<= c end))
  13938. (push (pop list) rtn)
  13939. (setq c (1+ c)))
  13940. (nreverse rtn)))
  13941. (defun org-find-base-buffer-visiting (file)
  13942. "Like `find-buffer-visiting' but alway return the base buffer and
  13943. not an indirect buffer."
  13944. (let ((buf (find-buffer-visiting file)))
  13945. (if buf
  13946. (or (buffer-base-buffer buf) buf)
  13947. nil)))
  13948. (defun org-image-file-name-regexp (&optional extensions)
  13949. "Return regexp matching the file names of images.
  13950. If EXTENSIONS is given, only match these."
  13951. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13952. (image-file-name-regexp)
  13953. (let ((image-file-name-extensions
  13954. (or extensions
  13955. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13956. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13957. (concat "\\."
  13958. (regexp-opt (nconc (mapcar 'upcase
  13959. image-file-name-extensions)
  13960. image-file-name-extensions)
  13961. t)
  13962. "\\'"))))
  13963. (defun org-file-image-p (file &optional extensions)
  13964. "Return non-nil if FILE is an image."
  13965. (save-match-data
  13966. (string-match (org-image-file-name-regexp extensions) file)))
  13967. (defun org-get-cursor-date ()
  13968. "Return the date at cursor in as a time.
  13969. This works in the calendar and in the agenda, anywhere else it just
  13970. returns the current time."
  13971. (let (date day defd)
  13972. (cond
  13973. ((eq major-mode 'calendar-mode)
  13974. (setq date (calendar-cursor-to-date)
  13975. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13976. ((eq major-mode 'org-agenda-mode)
  13977. (setq day (get-text-property (point) 'day))
  13978. (if day
  13979. (setq date (calendar-gregorian-from-absolute day)
  13980. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13981. (nth 2 date))))))
  13982. (or defd (current-time))))
  13983. (defvar org-agenda-action-marker (make-marker)
  13984. "Marker pointing to the entry for the next agenda action.")
  13985. (defun org-mark-entry-for-agenda-action ()
  13986. "Mark the current entry as target of an agenda action.
  13987. Agenda actions are actions executed from the agenda with the key `k',
  13988. which make use of the date at the cursor."
  13989. (interactive)
  13990. (move-marker org-agenda-action-marker
  13991. (save-excursion (org-back-to-heading t) (point))
  13992. (current-buffer))
  13993. (message
  13994. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13995. ;;; Paragraph filling stuff.
  13996. ;; We want this to be just right, so use the full arsenal.
  13997. (defun org-indent-line-function ()
  13998. "Indent line like previous, but further if previous was headline or item."
  13999. (interactive)
  14000. (let* ((pos (point))
  14001. (itemp (org-at-item-p))
  14002. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14003. column bpos bcol tpos tcol bullet btype bullet-type)
  14004. ;; Find the previous relevant line
  14005. (beginning-of-line 1)
  14006. (cond
  14007. ((looking-at "#") (setq column 0))
  14008. ((looking-at "\\*+ ") (setq column 0))
  14009. ((and (looking-at "[ \t]*:END:")
  14010. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14011. (save-excursion
  14012. (goto-char (1- (match-beginning 1)))
  14013. (setq column (current-column))))
  14014. (t
  14015. (beginning-of-line 0)
  14016. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14017. (not (looking-at "[ \t]*:END:"))
  14018. (not (looking-at org-drawer-regexp)))
  14019. (beginning-of-line 0))
  14020. (cond
  14021. ((looking-at "\\*+[ \t]+")
  14022. (if (not org-adapt-indentation)
  14023. (setq column 0)
  14024. (goto-char (match-end 0))
  14025. (setq column (current-column))))
  14026. ((looking-at org-drawer-regexp)
  14027. (goto-char (1- (match-beginning 1)))
  14028. (setq column (current-column)))
  14029. ((looking-at "\\([ \t]*\\):END:")
  14030. (goto-char (match-end 1))
  14031. (setq column (current-column)))
  14032. ((org-in-item-p)
  14033. (org-beginning-of-item)
  14034. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14035. (setq bpos (match-beginning 1) tpos (match-end 0)
  14036. bcol (progn (goto-char bpos) (current-column))
  14037. tcol (progn (goto-char tpos) (current-column))
  14038. bullet (match-string 1)
  14039. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14040. (if (> tcol (+ bcol org-description-max-indent))
  14041. (setq tcol (+ bcol 5)))
  14042. (if (not itemp)
  14043. (setq column tcol)
  14044. (goto-char pos)
  14045. (beginning-of-line 1)
  14046. (if (looking-at "\\S-")
  14047. (progn
  14048. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14049. (setq bullet (match-string 1)
  14050. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14051. (setq column (if (equal btype bullet-type) bcol tcol)))
  14052. (setq column (org-get-indentation)))))
  14053. (t (setq column (org-get-indentation))))))
  14054. (goto-char pos)
  14055. (if (<= (current-column) (current-indentation))
  14056. (org-indent-line-to column)
  14057. (save-excursion (org-indent-line-to column)))
  14058. (setq column (current-column))
  14059. (beginning-of-line 1)
  14060. (if (looking-at
  14061. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14062. (replace-match (concat "\\1" (format org-property-format
  14063. (match-string 2) (match-string 3)))
  14064. t nil))
  14065. (org-move-to-column column)))
  14066. (defun org-set-autofill-regexps ()
  14067. (interactive)
  14068. ;; In the paragraph separator we include headlines, because filling
  14069. ;; text in a line directly attached to a headline would otherwise
  14070. ;; fill the headline as well.
  14071. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14072. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14073. ;; The paragraph starter includes hand-formatted lists.
  14074. (org-set-local 'paragraph-start
  14075. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14076. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14077. ;; But only if the user has not turned off tables or fixed-width regions
  14078. (org-set-local
  14079. 'auto-fill-inhibit-regexp
  14080. (concat "\\*+ \\|#\\+"
  14081. "\\|[ \t]*" org-keyword-time-regexp
  14082. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14083. (concat
  14084. "\\|[ \t]*["
  14085. (if org-enable-table-editor "|" "")
  14086. (if org-enable-fixed-width-editor ":" "")
  14087. "]"))))
  14088. ;; We use our own fill-paragraph function, to make sure that tables
  14089. ;; and fixed-width regions are not wrapped. That function will pass
  14090. ;; through to `fill-paragraph' when appropriate.
  14091. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14092. ; Adaptive filling: To get full control, first make sure that
  14093. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14094. (org-set-local 'adaptive-fill-regexp "\000")
  14095. (org-set-local 'adaptive-fill-function
  14096. 'org-adaptive-fill-function)
  14097. (org-set-local
  14098. 'align-mode-rules-list
  14099. '((org-in-buffer-settings
  14100. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14101. (modes . '(org-mode))))))
  14102. (defun org-fill-paragraph (&optional justify)
  14103. "Re-align a table, pass through to fill-paragraph if no table."
  14104. (let ((table-p (org-at-table-p))
  14105. (table.el-p (org-at-table.el-p)))
  14106. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14107. (save-excursion (goto-char (point-at-bol))
  14108. (looking-at outline-regexp)))
  14109. t) ; skip headlines
  14110. (table.el-p t) ; skip table.el tables
  14111. (table-p (org-table-align) t) ; align org-mode tables
  14112. (t nil)))) ; call paragraph-fill
  14113. ;; For reference, this is the default value of adaptive-fill-regexp
  14114. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14115. (defun org-adaptive-fill-function ()
  14116. "Return a fill prefix for org-mode files.
  14117. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14118. work correctly."
  14119. (cond ((looking-at "#[ \t]+")
  14120. (match-string 0))
  14121. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14122. (save-excursion
  14123. (if (> (match-end 1) (+ (match-beginning 1)
  14124. org-description-max-indent))
  14125. (goto-char (+ (match-beginning 1) 5))
  14126. (goto-char (match-end 0)))
  14127. (make-string (current-column) ?\ )))
  14128. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14129. (save-excursion
  14130. (goto-char (match-end 0))
  14131. (make-string (current-column) ?\ )))
  14132. (t nil)))
  14133. ;;; Other stuff.
  14134. (defun org-toggle-fixed-width-section (arg)
  14135. "Toggle the fixed-width export.
  14136. If there is no active region, the QUOTE keyword at the current headline is
  14137. inserted or removed. When present, it causes the text between this headline
  14138. and the next to be exported as fixed-width text, and unmodified.
  14139. If there is an active region, this command adds or removes a colon as the
  14140. first character of this line. If the first character of a line is a colon,
  14141. this line is also exported in fixed-width font."
  14142. (interactive "P")
  14143. (let* ((cc 0)
  14144. (regionp (org-region-active-p))
  14145. (beg (if regionp (region-beginning) (point)))
  14146. (end (if regionp (region-end)))
  14147. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14148. (case-fold-search nil)
  14149. (re "[ \t]*\\(: \\)")
  14150. off)
  14151. (if regionp
  14152. (save-excursion
  14153. (goto-char beg)
  14154. (setq cc (current-column))
  14155. (beginning-of-line 1)
  14156. (setq off (looking-at re))
  14157. (while (> nlines 0)
  14158. (setq nlines (1- nlines))
  14159. (beginning-of-line 1)
  14160. (cond
  14161. (arg
  14162. (org-move-to-column cc t)
  14163. (insert ": \n")
  14164. (forward-line -1))
  14165. ((and off (looking-at re))
  14166. (replace-match "" t t nil 1))
  14167. ((not off) (org-move-to-column cc t) (insert ": ")))
  14168. (forward-line 1)))
  14169. (save-excursion
  14170. (org-back-to-heading)
  14171. (if (looking-at (concat outline-regexp
  14172. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14173. (replace-match "" t t nil 1)
  14174. (if (looking-at outline-regexp)
  14175. (progn
  14176. (goto-char (match-end 0))
  14177. (insert org-quote-string " "))))))))
  14178. (defun org-reftex-citation ()
  14179. "Use reftex-citation to insert a citation into the buffer.
  14180. This looks for a line like
  14181. #+BIBLIOGRAPHY: foo plain option:-d
  14182. and derives from it that foo.bib is the bbliography file relevant
  14183. for this document. It then installs the necessary environment for RefTeX
  14184. to work in this buffer and calls `reftex-citation' to insert a citation
  14185. into the buffer.
  14186. Export of such citations to both LaTeX and HTML is handled by the contributed
  14187. package org-exp-bibtex by Taru Karttunen."
  14188. (interactive)
  14189. (let ((reftex-docstruct-symbol 'rds)
  14190. (reftex-cite-format "\\cite{%l}")
  14191. rds bib)
  14192. (save-excursion
  14193. (save-restriction
  14194. (widen)
  14195. (let ((case-fold-search t)
  14196. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14197. (if (not (save-excursion
  14198. (or (re-search-forward re nil t)
  14199. (re-search-backward re nil t))))
  14200. (error "No bibliography defined in file")
  14201. (setq bib (concat (match-string 1) ".bib")
  14202. rds (list (list 'bib bib)))))))
  14203. (call-interactively 'reftex-citation)))
  14204. ;;;; Functions extending outline functionality
  14205. (defun org-beginning-of-line (&optional arg)
  14206. "Go to the beginning of the current line. If that is invisible, continue
  14207. to a visible line beginning. This makes the function of C-a more intuitive.
  14208. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14209. first attempt, and only move to after the tags when the cursor is already
  14210. beyond the end of the headline."
  14211. (interactive "P")
  14212. (let ((pos (point))
  14213. (special (if (consp org-special-ctrl-a/e)
  14214. (car org-special-ctrl-a/e)
  14215. org-special-ctrl-a/e))
  14216. refpos)
  14217. (beginning-of-line 1)
  14218. (if (and arg (fboundp 'move-beginning-of-line))
  14219. (call-interactively 'move-beginning-of-line)
  14220. (if (bobp)
  14221. nil
  14222. (backward-char 1)
  14223. (if (org-invisible-p)
  14224. (while (and (not (bobp)) (org-invisible-p))
  14225. (backward-char 1)
  14226. (beginning-of-line 1))
  14227. (forward-char 1))))
  14228. (when special
  14229. (cond
  14230. ((and (looking-at org-complex-heading-regexp)
  14231. (= (char-after (match-end 1)) ?\ ))
  14232. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14233. (point-at-eol)))
  14234. (goto-char
  14235. (if (eq special t)
  14236. (cond ((> pos refpos) refpos)
  14237. ((= pos (point)) refpos)
  14238. (t (point)))
  14239. (cond ((> pos (point)) (point))
  14240. ((not (eq last-command this-command)) (point))
  14241. (t refpos)))))
  14242. ((org-at-item-p)
  14243. (goto-char
  14244. (if (eq special t)
  14245. (cond ((> pos (match-end 4)) (match-end 4))
  14246. ((= pos (point)) (match-end 4))
  14247. (t (point)))
  14248. (cond ((> pos (point)) (point))
  14249. ((not (eq last-command this-command)) (point))
  14250. (t (match-end 4))))))))
  14251. (org-no-warnings
  14252. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14253. (defun org-end-of-line (&optional arg)
  14254. "Go to the end of the line.
  14255. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14256. first attempt, and only move to after the tags when the cursor is already
  14257. beyond the end of the headline."
  14258. (interactive "P")
  14259. (let ((special (if (consp org-special-ctrl-a/e)
  14260. (cdr org-special-ctrl-a/e)
  14261. org-special-ctrl-a/e)))
  14262. (if (or (not special)
  14263. (not (org-on-heading-p))
  14264. arg)
  14265. (call-interactively (if (fboundp 'move-end-of-line)
  14266. 'move-end-of-line
  14267. 'end-of-line))
  14268. (let ((pos (point)))
  14269. (beginning-of-line 1)
  14270. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14271. (if (eq special t)
  14272. (if (or (< pos (match-beginning 1))
  14273. (= pos (match-end 0)))
  14274. (goto-char (match-beginning 1))
  14275. (goto-char (match-end 0)))
  14276. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14277. (goto-char (match-end 0))
  14278. (goto-char (match-beginning 1))))
  14279. (call-interactively (if (fboundp 'move-end-of-line)
  14280. 'move-end-of-line
  14281. 'end-of-line)))))
  14282. (org-no-warnings
  14283. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14284. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14285. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14286. (defun org-backward-sentence (&optional arg)
  14287. "Go to beginning of sentence, or beginning of table field.
  14288. This will call `backward-sentence' or `org-table-beginning-of-field',
  14289. depending on context."
  14290. (interactive "P")
  14291. (cond
  14292. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14293. (t (call-interactively 'backward-sentence))))
  14294. (defun org-forward-sentence (&optional arg)
  14295. "Go to end of sentence, or end of table field.
  14296. This will call `forward-sentence' or `org-table-end-of-field',
  14297. depending on context."
  14298. (interactive "P")
  14299. (cond
  14300. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14301. (t (call-interactively 'forward-sentence))))
  14302. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14303. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14304. (defun org-kill-line (&optional arg)
  14305. "Kill line, to tags or end of line."
  14306. (interactive "P")
  14307. (cond
  14308. ((or (not org-special-ctrl-k)
  14309. (bolp)
  14310. (not (org-on-heading-p)))
  14311. (call-interactively 'kill-line))
  14312. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14313. (kill-region (point) (match-beginning 1))
  14314. (org-set-tags nil t))
  14315. (t (kill-region (point) (point-at-eol)))))
  14316. (define-key org-mode-map "\C-k" 'org-kill-line)
  14317. (defun org-yank (&optional arg)
  14318. "Yank. If the kill is a subtree, treat it specially.
  14319. This command will look at the current kill and check if is a single
  14320. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14321. cursor is at the beginning of a line or after the stars of a currently
  14322. empty headline, then the yank is handled specially. How exactly depends
  14323. on the value of the following variables, both set by default.
  14324. org-yank-folded-subtrees
  14325. When set, the subtree(s) will be folded after insertion, but only
  14326. if doing so would now swallow text after the yanked text.
  14327. org-yank-adjusted-subtrees
  14328. When set, the subtree will be promoted or demoted in order to
  14329. fit into the local outline tree structure, which means that the level
  14330. will be adjusted so that it becomes the smaller one of the two
  14331. *visible* surrounding headings.
  14332. Any prefix to this command will cause `yank' to be called directly with
  14333. no special treatment. In particular, a simple `C-u' prefix will just
  14334. plainly yank the text as it is.
  14335. \[1] Basically, the test checks if the first non-white line is a heading
  14336. and if there are no other headings with fewer stars."
  14337. (interactive "P")
  14338. (setq this-command 'yank)
  14339. (if arg
  14340. (call-interactively 'yank)
  14341. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14342. (and (org-kill-is-subtree-p)
  14343. (or (bolp)
  14344. (and (looking-at "[ \t]*$")
  14345. (string-match
  14346. "\\`\\*+\\'"
  14347. (buffer-substring (point-at-bol) (point)))))))
  14348. swallowp)
  14349. (cond
  14350. ((and subtreep org-yank-folded-subtrees)
  14351. (let ((beg (point))
  14352. end)
  14353. (if (and subtreep org-yank-adjusted-subtrees)
  14354. (org-paste-subtree nil nil 'for-yank)
  14355. (call-interactively 'yank))
  14356. (setq end (point))
  14357. (goto-char beg)
  14358. (when (and (bolp) subtreep
  14359. (not (setq swallowp
  14360. (org-yank-folding-would-swallow-text beg end))))
  14361. (or (looking-at outline-regexp)
  14362. (re-search-forward (concat "^" outline-regexp) end t))
  14363. (while (and (< (point) end) (looking-at outline-regexp))
  14364. (hide-subtree)
  14365. (org-cycle-show-empty-lines 'folded)
  14366. (condition-case nil
  14367. (outline-forward-same-level 1)
  14368. (error (goto-char end)))))
  14369. (when swallowp
  14370. (message
  14371. "Yanked text not folded because that would swallow text"))
  14372. (goto-char end)
  14373. (skip-chars-forward " \t\n\r")
  14374. (beginning-of-line 1)
  14375. (push-mark beg 'nomsg)))
  14376. ((and subtreep org-yank-adjusted-subtrees)
  14377. (let ((beg (point-at-bol)))
  14378. (org-paste-subtree nil nil 'for-yank)
  14379. (push-mark beg 'nomsg)))
  14380. (t
  14381. (call-interactively 'yank))))))
  14382. (defun org-yank-folding-would-swallow-text (beg end)
  14383. "Would hide-subtree at BEG swallow any text after END?"
  14384. (let (level)
  14385. (save-excursion
  14386. (goto-char beg)
  14387. (when (or (looking-at outline-regexp)
  14388. (re-search-forward (concat "^" outline-regexp) end t))
  14389. (setq level (org-outline-level)))
  14390. (goto-char end)
  14391. (skip-chars-forward " \t\r\n\v\f")
  14392. (if (or (eobp)
  14393. (and (bolp) (looking-at org-outline-regexp)
  14394. (<= (org-outline-level) level)))
  14395. nil ; Nothing would be swallowed
  14396. t)))) ; something would swallow
  14397. (define-key org-mode-map "\C-y" 'org-yank)
  14398. (defun org-invisible-p ()
  14399. "Check if point is at a character currently not visible."
  14400. ;; Early versions of noutline don't have `outline-invisible-p'.
  14401. (if (fboundp 'outline-invisible-p)
  14402. (outline-invisible-p)
  14403. (get-char-property (point) 'invisible)))
  14404. (defun org-invisible-p2 ()
  14405. "Check if point is at a character currently not visible."
  14406. (save-excursion
  14407. (if (and (eolp) (not (bobp))) (backward-char 1))
  14408. ;; Early versions of noutline don't have `outline-invisible-p'.
  14409. (if (fboundp 'outline-invisible-p)
  14410. (outline-invisible-p)
  14411. (get-char-property (point) 'invisible))))
  14412. (defun org-back-to-heading (&optional invisible-ok)
  14413. "Call `outline-back-to-heading', but provide a better error message."
  14414. (condition-case nil
  14415. (outline-back-to-heading invisible-ok)
  14416. (error (error "Before first headline at position %d in buffer %s"
  14417. (point) (current-buffer)))))
  14418. (defun org-before-first-heading-p ()
  14419. "Before first heading?"
  14420. (save-excursion
  14421. (null (re-search-backward "^\\*+ " nil t))))
  14422. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14423. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14424. (defun org-at-heading-or-item-p ()
  14425. (or (org-on-heading-p) (org-at-item-p)))
  14426. (defun org-on-target-p ()
  14427. (or (org-in-regexp org-radio-target-regexp)
  14428. (org-in-regexp org-target-regexp)))
  14429. (defun org-up-heading-all (arg)
  14430. "Move to the heading line of which the present line is a subheading.
  14431. This function considers both visible and invisible heading lines.
  14432. With argument, move up ARG levels."
  14433. (if (fboundp 'outline-up-heading-all)
  14434. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14435. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14436. (defun org-up-heading-safe ()
  14437. "Move to the heading line of which the present line is a subheading.
  14438. This version will not throw an error. It will return the level of the
  14439. headline found, or nil if no higher level is found.
  14440. Also, this function will be a lot faster than `outline-up-heading',
  14441. because it relies on stars being the outline starters. This can really
  14442. make a significant difference in outlines with very many siblings."
  14443. (let (start-level re)
  14444. (org-back-to-heading t)
  14445. (setq start-level (funcall outline-level))
  14446. (if (equal start-level 1)
  14447. nil
  14448. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14449. (if (re-search-backward re nil t)
  14450. (funcall outline-level)))))
  14451. (defun org-first-sibling-p ()
  14452. "Is this heading the first child of its parents?"
  14453. (interactive)
  14454. (let ((re (concat "^" outline-regexp))
  14455. level l)
  14456. (unless (org-at-heading-p t)
  14457. (error "Not at a heading"))
  14458. (setq level (funcall outline-level))
  14459. (save-excursion
  14460. (if (not (re-search-backward re nil t))
  14461. t
  14462. (setq l (funcall outline-level))
  14463. (< l level)))))
  14464. (defun org-goto-sibling (&optional previous)
  14465. "Goto the next sibling, even if it is invisible.
  14466. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14467. when a sibling was found. When none is found, return nil and don't
  14468. move point."
  14469. (let ((fun (if previous 're-search-backward 're-search-forward))
  14470. (pos (point))
  14471. (re (concat "^" outline-regexp))
  14472. level l)
  14473. (when (condition-case nil (org-back-to-heading t) (error nil))
  14474. (setq level (funcall outline-level))
  14475. (catch 'exit
  14476. (or previous (forward-char 1))
  14477. (while (funcall fun re nil t)
  14478. (setq l (funcall outline-level))
  14479. (when (< l level) (goto-char pos) (throw 'exit nil))
  14480. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14481. (goto-char pos)
  14482. nil))))
  14483. (defun org-show-siblings ()
  14484. "Show all siblings of the current headline."
  14485. (save-excursion
  14486. (while (org-goto-sibling) (org-flag-heading nil)))
  14487. (save-excursion
  14488. (while (org-goto-sibling 'previous)
  14489. (org-flag-heading nil))))
  14490. (defun org-show-hidden-entry ()
  14491. "Show an entry where even the heading is hidden."
  14492. (save-excursion
  14493. (org-show-entry)))
  14494. (defun org-flag-heading (flag &optional entry)
  14495. "Flag the current heading. FLAG non-nil means make invisible.
  14496. When ENTRY is non-nil, show the entire entry."
  14497. (save-excursion
  14498. (org-back-to-heading t)
  14499. ;; Check if we should show the entire entry
  14500. (if entry
  14501. (progn
  14502. (org-show-entry)
  14503. (save-excursion
  14504. (and (outline-next-heading)
  14505. (org-flag-heading nil))))
  14506. (outline-flag-region (max (point-min) (1- (point)))
  14507. (save-excursion (outline-end-of-heading) (point))
  14508. flag))))
  14509. (defun org-forward-same-level (arg)
  14510. "Move forward to the ARG'th subheading at same level as this one.
  14511. Stop at the first and last subheadings of a superior heading.
  14512. This is like outline-forward-same-level, but invisible headings are ok."
  14513. (interactive "p")
  14514. (org-back-to-heading t)
  14515. (while (> arg 0)
  14516. (let ((point-to-move-to (save-excursion
  14517. (org-get-next-sibling))))
  14518. (if point-to-move-to
  14519. (progn
  14520. (goto-char point-to-move-to)
  14521. (setq arg (1- arg)))
  14522. (progn
  14523. (setq arg 0)
  14524. (error "No following same-level heading"))))))
  14525. (defun org-get-next-sibling ()
  14526. "Move to next heading of the same level, and return point.
  14527. If there is no such heading, return nil.
  14528. This is like outline-next-sibling, but invisible headings are ok."
  14529. (let ((level (funcall outline-level)))
  14530. (outline-next-heading)
  14531. (while (and (not (eobp)) (> (funcall outline-level) level))
  14532. (outline-next-heading))
  14533. (if (or (eobp) (< (funcall outline-level) level))
  14534. nil
  14535. (point))))
  14536. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14537. ;; This is an exact copy of the original function, but it uses
  14538. ;; `org-back-to-heading', to make it work also in invisible
  14539. ;; trees. And is uses an invisible-OK argument.
  14540. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14541. (org-back-to-heading invisible-OK)
  14542. (let ((first t)
  14543. (level (funcall outline-level)))
  14544. (while (and (not (eobp))
  14545. (or first (> (funcall outline-level) level)))
  14546. (setq first nil)
  14547. (outline-next-heading))
  14548. (unless to-heading
  14549. (if (memq (preceding-char) '(?\n ?\^M))
  14550. (progn
  14551. ;; Go to end of line before heading
  14552. (forward-char -1)
  14553. (if (memq (preceding-char) '(?\n ?\^M))
  14554. ;; leave blank line before heading
  14555. (forward-char -1))))))
  14556. (point))
  14557. (defun org-show-subtree ()
  14558. "Show everything after this heading at deeper levels."
  14559. (outline-flag-region
  14560. (point)
  14561. (save-excursion
  14562. (outline-end-of-subtree) (outline-next-heading) (point))
  14563. nil))
  14564. (defun org-show-entry ()
  14565. "Show the body directly following this heading.
  14566. Show the heading too, if it is currently invisible."
  14567. (interactive)
  14568. (save-excursion
  14569. (condition-case nil
  14570. (progn
  14571. (org-back-to-heading t)
  14572. (outline-flag-region
  14573. (max (point-min) (1- (point)))
  14574. (save-excursion
  14575. (if (re-search-forward
  14576. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14577. (match-beginning 1)
  14578. (point-max)))
  14579. nil))
  14580. (error nil))))
  14581. (defun org-make-options-regexp (kwds &optional extra)
  14582. "Make a regular expression for keyword lines."
  14583. (concat
  14584. "^"
  14585. "#?[ \t]*\\+\\("
  14586. (mapconcat 'regexp-quote kwds "\\|")
  14587. (if extra (concat "\\|" extra))
  14588. "\\):[ \t]*"
  14589. "\\(.+\\)"))
  14590. ;; Make isearch reveal the necessary context
  14591. (defun org-isearch-end ()
  14592. "Reveal context after isearch exits."
  14593. (when isearch-success ; only if search was successful
  14594. (if (featurep 'xemacs)
  14595. ;; Under XEmacs, the hook is run in the correct place,
  14596. ;; we directly show the context.
  14597. (org-show-context 'isearch)
  14598. ;; In Emacs the hook runs *before* restoring the overlays.
  14599. ;; So we have to use a one-time post-command-hook to do this.
  14600. ;; (Emacs 22 has a special variable, see function `org-mode')
  14601. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14602. isearch-mode-end-hook-quit)
  14603. ;; Only when the isearch was not quitted.
  14604. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14605. 'append 'local)))))
  14606. (defun org-isearch-post-command ()
  14607. "Remove self from hook, and show context."
  14608. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14609. (org-show-context 'isearch))
  14610. ;;;; Integration with and fixes for other packages
  14611. ;;; Imenu support
  14612. (defvar org-imenu-markers nil
  14613. "All markers currently used by Imenu.")
  14614. (make-variable-buffer-local 'org-imenu-markers)
  14615. (defun org-imenu-new-marker (&optional pos)
  14616. "Return a new marker for use by Imenu, and remember the marker."
  14617. (let ((m (make-marker)))
  14618. (move-marker m (or pos (point)))
  14619. (push m org-imenu-markers)
  14620. m))
  14621. (defun org-imenu-get-tree ()
  14622. "Produce the index for Imenu."
  14623. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14624. (setq org-imenu-markers nil)
  14625. (let* ((n org-imenu-depth)
  14626. (re (concat "^" outline-regexp))
  14627. (subs (make-vector (1+ n) nil))
  14628. (last-level 0)
  14629. m level head)
  14630. (save-excursion
  14631. (save-restriction
  14632. (widen)
  14633. (goto-char (point-max))
  14634. (while (re-search-backward re nil t)
  14635. (setq level (org-reduced-level (funcall outline-level)))
  14636. (when (<= level n)
  14637. (looking-at org-complex-heading-regexp)
  14638. (setq head (org-link-display-format
  14639. (org-match-string-no-properties 4))
  14640. m (org-imenu-new-marker))
  14641. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14642. (if (>= level last-level)
  14643. (push (cons head m) (aref subs level))
  14644. (push (cons head (aref subs (1+ level))) (aref subs level))
  14645. (loop for i from (1+ level) to n do (aset subs i nil)))
  14646. (setq last-level level)))))
  14647. (aref subs 1)))
  14648. (eval-after-load "imenu"
  14649. '(progn
  14650. (add-hook 'imenu-after-jump-hook
  14651. (lambda ()
  14652. (if (eq major-mode 'org-mode)
  14653. (org-show-context 'org-goto))))))
  14654. (defun org-link-display-format (link)
  14655. "Replace a link with either the description, or the link target
  14656. if no description is present"
  14657. (save-match-data
  14658. (if (string-match org-bracket-link-analytic-regexp link)
  14659. (replace-match (or (match-string 5 link)
  14660. (concat (match-string 1 link)
  14661. (match-string 3 link)))
  14662. nil nil link)
  14663. link)))
  14664. ;; Speedbar support
  14665. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14666. "Overlay marking the agenda restriction line in speedbar.")
  14667. (org-overlay-put org-speedbar-restriction-lock-overlay
  14668. 'face 'org-agenda-restriction-lock)
  14669. (org-overlay-put org-speedbar-restriction-lock-overlay
  14670. 'help-echo "Agendas are currently limited to this item.")
  14671. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14672. (defun org-speedbar-set-agenda-restriction ()
  14673. "Restrict future agenda commands to the location at point in speedbar.
  14674. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14675. (interactive)
  14676. (require 'org-agenda)
  14677. (let (p m tp np dir txt)
  14678. (cond
  14679. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14680. 'org-imenu t))
  14681. (setq m (get-text-property p 'org-imenu-marker))
  14682. (save-excursion
  14683. (save-restriction
  14684. (set-buffer (marker-buffer m))
  14685. (goto-char m)
  14686. (org-agenda-set-restriction-lock 'subtree))))
  14687. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14688. 'speedbar-function 'speedbar-find-file))
  14689. (setq tp (previous-single-property-change
  14690. (1+ p) 'speedbar-function)
  14691. np (next-single-property-change
  14692. tp 'speedbar-function)
  14693. dir (speedbar-line-directory)
  14694. txt (buffer-substring-no-properties (or tp (point-min))
  14695. (or np (point-max))))
  14696. (save-excursion
  14697. (save-restriction
  14698. (set-buffer (find-file-noselect
  14699. (let ((default-directory dir))
  14700. (expand-file-name txt))))
  14701. (unless (org-mode-p)
  14702. (error "Cannot restrict to non-Org-mode file"))
  14703. (org-agenda-set-restriction-lock 'file))))
  14704. (t (error "Don't know how to restrict Org-mode's agenda")))
  14705. (org-move-overlay org-speedbar-restriction-lock-overlay
  14706. (point-at-bol) (point-at-eol))
  14707. (setq current-prefix-arg nil)
  14708. (org-agenda-maybe-redo)))
  14709. (eval-after-load "speedbar"
  14710. '(progn
  14711. (speedbar-add-supported-extension ".org")
  14712. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14713. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14714. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14715. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14716. (add-hook 'speedbar-visiting-tag-hook
  14717. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14718. ;;; Fixes and Hacks for problems with other packages
  14719. ;; Make flyspell not check words in links, to not mess up our keymap
  14720. (defun org-mode-flyspell-verify ()
  14721. "Don't let flyspell put overlays at active buttons."
  14722. (not (get-text-property (point) 'keymap)))
  14723. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14724. (eval-after-load "bookmark"
  14725. '(if (boundp 'bookmark-after-jump-hook)
  14726. ;; We can use the hook
  14727. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14728. ;; Hook not available, use advice
  14729. (defadvice bookmark-jump (after org-make-visible activate)
  14730. "Make the position visible."
  14731. (org-bookmark-jump-unhide))))
  14732. ;; Make sure saveplace show the location if it was hidden
  14733. (eval-after-load "saveplace"
  14734. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14735. "Make the position visible."
  14736. (org-bookmark-jump-unhide)))
  14737. (defun org-bookmark-jump-unhide ()
  14738. "Unhide the current position, to show the bookmark location."
  14739. (and (org-mode-p)
  14740. (or (org-invisible-p)
  14741. (save-excursion (goto-char (max (point-min) (1- (point))))
  14742. (org-invisible-p)))
  14743. (org-show-context 'bookmark-jump)))
  14744. ;; Make session.el ignore our circular variable
  14745. (eval-after-load "session"
  14746. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14747. ;;;; Experimental code
  14748. (defun org-closed-in-range ()
  14749. "Sparse tree of items closed in a certain time range.
  14750. Still experimental, may disappear in the future."
  14751. (interactive)
  14752. ;; Get the time interval from the user.
  14753. (let* ((time1 (time-to-seconds
  14754. (org-read-date nil 'to-time nil "Starting date: ")))
  14755. (time2 (time-to-seconds
  14756. (org-read-date nil 'to-time nil "End date:")))
  14757. ;; callback function
  14758. (callback (lambda ()
  14759. (let ((time
  14760. (time-to-seconds
  14761. (apply 'encode-time
  14762. (org-parse-time-string
  14763. (match-string 1))))))
  14764. ;; check if time in interval
  14765. (and (>= time time1) (<= time time2))))))
  14766. ;; make tree, check each match with the callback
  14767. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14768. ;;;; Finish up
  14769. (provide 'org)
  14770. (run-hooks 'org-load-hook)
  14771. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14772. ;;; org.el ends here