org.el 603 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.25trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.25trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  185. (defcustom org-support-shift-select nil
  186. "Non-nil means, make shift-cursor commands select text when possible.
  187. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  188. selecting a region, or enlarge thusly regions started in this way.
  189. In Org-mode, in special contexts, these same keys are used for other
  190. purposes, important enough to compete with shift selection. Org tries
  191. to balance these needs by supporting `shift-select-mode' outside these
  192. special contexts, under control of this variable.
  193. The default of this variable is nil, to avoid confusing behavior. Shifted
  194. cursor keys will then execute Org commands in the following contexts:
  195. - on a headline, changing TODO state (left/right) and priority (up/down)
  196. - on a time stamp, changing the time
  197. - in a plain list item, changing the bullet type
  198. - in a property definition line, switching between allowed values
  199. - in the BEGIN line of a clock table (changing the time block).
  200. Outside these contexts, the commands will throw an error.
  201. When this variable is t and the cursor is not in a special context,
  202. Org-mode will support shift-selection for making and enlarging regions.
  203. To make this more effective, the bullet cycling will no longer happen
  204. anywhere in an item line, but only if the cursor is exactly on the bullet.
  205. If you set this variable to the symbol `always', then the keys
  206. will not be special in headlines, property lines, and item lines, to make
  207. shift selection work there as well. If this is what you want, you can
  208. use the following alternative commands: `C-c C-t' and `C-c ,' to
  209. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  210. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  211. edited by hand or in column view.
  212. However, when the cursor is on a timestamp, shift-cursor commands
  213. will still edit the time stamp - this is just too good to give up.
  214. XEmacs user should have this variable set to nil, because shift-select-mode
  215. is Emacs 23 only."
  216. :group 'org
  217. :type '(choice
  218. (const :tag "Never" nil)
  219. (const :tag "When outside special context" t)
  220. (const :tag "Everywhere except timestamps" always)))
  221. (defgroup org-startup nil
  222. "Options concerning startup of Org-mode."
  223. :tag "Org Startup"
  224. :group 'org)
  225. (defcustom org-startup-folded t
  226. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  227. This can also be configured on a per-file basis by adding one of
  228. the following lines anywhere in the buffer:
  229. #+STARTUP: fold
  230. #+STARTUP: nofold
  231. #+STARTUP: content"
  232. :group 'org-startup
  233. :type '(choice
  234. (const :tag "nofold: show all" nil)
  235. (const :tag "fold: overview" t)
  236. (const :tag "content: all headlines" content)))
  237. (defcustom org-startup-truncated t
  238. "Non-nil means, entering Org-mode will set `truncate-lines'.
  239. This is useful since some lines containing links can be very long and
  240. uninteresting. Also tables look terrible when wrapped."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-startup-align-all-tables nil
  244. "Non-nil means, align all tables when visiting a file.
  245. This is useful when the column width in tables is forced with <N> cookies
  246. in table fields. Such tables will look correct only after the first re-align.
  247. This can also be configured on a per-file basis by adding one of
  248. the following lines anywhere in the buffer:
  249. #+STARTUP: align
  250. #+STARTUP: noalign"
  251. :group 'org-startup
  252. :type 'boolean)
  253. (defcustom org-insert-mode-line-in-empty-file nil
  254. "Non-nil means insert the first line setting Org-mode in empty files.
  255. When the function `org-mode' is called interactively in an empty file, this
  256. normally means that the file name does not automatically trigger Org-mode.
  257. To ensure that the file will always be in Org-mode in the future, a
  258. line enforcing Org-mode will be inserted into the buffer, if this option
  259. has been set."
  260. :group 'org-startup
  261. :type 'boolean)
  262. (defcustom org-replace-disputed-keys nil
  263. "Non-nil means use alternative key bindings for some keys.
  264. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  265. These keys are also used by other packages like shift-selection-mode'
  266. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  267. If you want to use Org-mode together with one of these other modes,
  268. or more generally if you would like to move some Org-mode commands to
  269. other keys, set this variable and configure the keys with the variable
  270. `org-disputed-keys'.
  271. This option is only relevant at load-time of Org-mode, and must be set
  272. *before* org.el is loaded. Changing it requires a restart of Emacs to
  273. become effective."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-use-extra-keys nil
  277. "Non-nil means use extra key sequence definitions for certain
  278. commands. This happens automatically if you run XEmacs or if
  279. window-system is nil. This variable lets you do the same
  280. manually. You must set it before loading org.
  281. Example: on Carbon Emacs 22 running graphically, with an external
  282. keyboard on a Powerbook, the default way of setting M-left might
  283. not work for either Alt or ESC. Setting this variable will make
  284. it work for ESC."
  285. :group 'org-startup
  286. :type 'boolean)
  287. (if (fboundp 'defvaralias)
  288. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  289. (defcustom org-disputed-keys
  290. '(([(shift up)] . [(meta p)])
  291. ([(shift down)] . [(meta n)])
  292. ([(shift left)] . [(meta -)])
  293. ([(shift right)] . [(meta +)])
  294. ([(control shift right)] . [(meta shift +)])
  295. ([(control shift left)] . [(meta shift -)]))
  296. "Keys for which Org-mode and other modes compete.
  297. This is an alist, cars are the default keys, second element specifies
  298. the alternative to use when `org-replace-disputed-keys' is t.
  299. Keys can be specified in any syntax supported by `define-key'.
  300. The value of this option takes effect only at Org-mode's startup,
  301. therefore you'll have to restart Emacs to apply it after changing."
  302. :group 'org-startup
  303. :type 'alist)
  304. (defun org-key (key)
  305. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  306. Or return the original if not disputed."
  307. (if org-replace-disputed-keys
  308. (let* ((nkey (key-description key))
  309. (x (org-find-if (lambda (x)
  310. (equal (key-description (car x)) nkey))
  311. org-disputed-keys)))
  312. (if x (cdr x) key))
  313. key))
  314. (defun org-find-if (predicate seq)
  315. (catch 'exit
  316. (while seq
  317. (if (funcall predicate (car seq))
  318. (throw 'exit (car seq))
  319. (pop seq)))))
  320. (defun org-defkey (keymap key def)
  321. "Define a key, possibly translated, as returned by `org-key'."
  322. (define-key keymap (org-key key) def))
  323. (defcustom org-ellipsis nil
  324. "The ellipsis to use in the Org-mode outline.
  325. When nil, just use the standard three dots. When a string, use that instead,
  326. When a face, use the standard 3 dots, but with the specified face.
  327. The change affects only Org-mode (which will then use its own display table).
  328. Changing this requires executing `M-x org-mode' in a buffer to become
  329. effective."
  330. :group 'org-startup
  331. :type '(choice (const :tag "Default" nil)
  332. (face :tag "Face" :value org-warning)
  333. (string :tag "String" :value "...#")))
  334. (defvar org-display-table nil
  335. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  336. (defgroup org-keywords nil
  337. "Keywords in Org-mode."
  338. :tag "Org Keywords"
  339. :group 'org)
  340. (defcustom org-deadline-string "DEADLINE:"
  341. "String to mark deadline entries.
  342. A deadline is this string, followed by a time stamp. Should be a word,
  343. terminated by a colon. You can insert a schedule keyword and
  344. a timestamp with \\[org-deadline].
  345. Changes become only effective after restarting Emacs."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defcustom org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].
  353. Changes become only effective after restarting Emacs."
  354. :group 'org-keywords
  355. :type 'string)
  356. (defcustom org-closed-string "CLOSED:"
  357. "String used as the prefix for timestamps logging closing a TODO entry."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-clock-string "CLOCK:"
  361. "String used as prefix for timestamps clocking work hours on an item."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-comment-string "COMMENT"
  365. "Entries starting with this keyword will never be exported.
  366. An entry can be toggled between COMMENT and normal with
  367. \\[org-toggle-comment].
  368. Changes become only effective after restarting Emacs."
  369. :group 'org-keywords
  370. :type 'string)
  371. (defcustom org-quote-string "QUOTE"
  372. "Entries starting with this keyword will be exported in fixed-width font.
  373. Quoting applies only to the text in the entry following the headline, and does
  374. not extend beyond the next headline, even if that is lower level.
  375. An entry can be toggled between QUOTE and normal with
  376. \\[org-toggle-fixed-width-section]."
  377. :group 'org-keywords
  378. :type 'string)
  379. (defconst org-repeat-re
  380. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  381. "Regular expression for specifying repeated events.
  382. After a match, group 1 contains the repeat expression.")
  383. (defgroup org-structure nil
  384. "Options concerning the general structure of Org-mode files."
  385. :tag "Org Structure"
  386. :group 'org)
  387. (defgroup org-reveal-location nil
  388. "Options about how to make context of a location visible."
  389. :tag "Org Reveal Location"
  390. :group 'org-structure)
  391. (defconst org-context-choice
  392. '(choice
  393. (const :tag "Always" t)
  394. (const :tag "Never" nil)
  395. (repeat :greedy t :tag "Individual contexts"
  396. (cons
  397. (choice :tag "Context"
  398. (const agenda)
  399. (const org-goto)
  400. (const occur-tree)
  401. (const tags-tree)
  402. (const link-search)
  403. (const mark-goto)
  404. (const bookmark-jump)
  405. (const isearch)
  406. (const default))
  407. (boolean))))
  408. "Contexts for the reveal options.")
  409. (defcustom org-show-hierarchy-above '((default . t))
  410. "Non-nil means, show full hierarchy when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the hierarchy of headings
  413. above the exposed location is shown.
  414. Turning this off for example for sparse trees makes them very compact.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. Valid contexts are
  417. agenda when exposing an entry from the agenda
  418. org-goto when using the command `org-goto' on key C-c C-j
  419. occur-tree when using the command `org-occur' on key C-c /
  420. tags-tree when constructing a sparse tree based on tags matches
  421. link-search when exposing search matches associated with a link
  422. mark-goto when exposing the jump goal of a mark
  423. bookmark-jump when exposing a bookmark location
  424. isearch when exiting from an incremental search
  425. default default for all contexts not set explicitly"
  426. :group 'org-reveal-location
  427. :type org-context-choice)
  428. (defcustom org-show-following-heading '((default . nil))
  429. "Non-nil means, show following heading when revealing a location.
  430. Org-mode often shows locations in an org-mode file which might have
  431. been invisible before. When this is set, the heading following the
  432. match is shown.
  433. Turning this off for example for sparse trees makes them very compact,
  434. but makes it harder to edit the location of the match. In such a case,
  435. use the command \\[org-reveal] to show more context.
  436. Instead of t, this can also be an alist specifying this option for different
  437. contexts. See `org-show-hierarchy-above' for valid contexts."
  438. :group 'org-reveal-location
  439. :type org-context-choice)
  440. (defcustom org-show-siblings '((default . nil) (isearch t))
  441. "Non-nil means, show all sibling heading when revealing a location.
  442. Org-mode often shows locations in an org-mode file which might have
  443. been invisible before. When this is set, the sibling of the current entry
  444. heading are all made visible. If `org-show-hierarchy-above' is t,
  445. the same happens on each level of the hierarchy above the current entry.
  446. By default this is on for the isearch context, off for all other contexts.
  447. Turning this off for example for sparse trees makes them very compact,
  448. but makes it harder to edit the location of the match. In such a case,
  449. use the command \\[org-reveal] to show more context.
  450. Instead of t, this can also be an alist specifying this option for different
  451. contexts. See `org-show-hierarchy-above' for valid contexts."
  452. :group 'org-reveal-location
  453. :type org-context-choice)
  454. (defcustom org-show-entry-below '((default . nil))
  455. "Non-nil means, show the entry below a headline when revealing a location.
  456. Org-mode often shows locations in an org-mode file which might have
  457. been invisible before. When this is set, the text below the headline that is
  458. exposed is also shown.
  459. By default this is off for all contexts.
  460. Instead of t, this can also be an alist specifying this option for different
  461. contexts. See `org-show-hierarchy-above' for valid contexts."
  462. :group 'org-reveal-location
  463. :type org-context-choice)
  464. (defcustom org-indirect-buffer-display 'other-window
  465. "How should indirect tree buffers be displayed?
  466. This applies to indirect buffers created with the commands
  467. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  468. Valid values are:
  469. current-window Display in the current window
  470. other-window Just display in another window.
  471. dedicated-frame Create one new frame, and re-use it each time.
  472. new-frame Make a new frame each time. Note that in this case
  473. previously-made indirect buffers are kept, and you need to
  474. kill these buffers yourself."
  475. :group 'org-structure
  476. :group 'org-agenda-windows
  477. :type '(choice
  478. (const :tag "In current window" current-window)
  479. (const :tag "In current frame, other window" other-window)
  480. (const :tag "Each time a new frame" new-frame)
  481. (const :tag "One dedicated frame" dedicated-frame)))
  482. (defgroup org-cycle nil
  483. "Options concerning visibility cycling in Org-mode."
  484. :tag "Org Cycle"
  485. :group 'org-structure)
  486. (defcustom org-cycle-max-level nil
  487. "Maximum level which should still be subject to visibility cycling.
  488. Levels higher than this will, for cycling, be treated as text, not a headline.
  489. When `org-odd-levels-only' is set, a value of N in this variable actually
  490. means 2N-1 stars as the limiting headline.
  491. When nil, cycle all levels.
  492. Note that the limiting level of cycling is also influenced by
  493. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  494. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  495. than its value."
  496. :group 'org-cycle
  497. :type '(choice
  498. (const :tag "No limit" nil)
  499. (integer :tag "Maximum level")))
  500. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  501. "Names of drawers. Drawers are not opened by cycling on the headline above.
  502. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  503. this:
  504. :DRAWERNAME:
  505. .....
  506. :END:
  507. The drawer \"PROPERTIES\" is special for capturing properties through
  508. the property API.
  509. Drawers can be defined on the per-file basis with a line like:
  510. #+DRAWERS: HIDDEN STATE PROPERTIES"
  511. :group 'org-structure
  512. :group 'org-cycle
  513. :type '(repeat (string :tag "Drawer Name")))
  514. (defcustom org-cycle-global-at-bob nil
  515. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  516. This makes it possible to do global cycling without having to use S-TAB or
  517. C-u TAB. For this special case to work, the first line of the buffer
  518. must not be a headline - it may be empty or some other text. When used in
  519. this way, `org-cycle-hook' is disables temporarily, to make sure the
  520. cursor stays at the beginning of the buffer.
  521. When this option is nil, don't do anything special at the beginning
  522. of the buffer."
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-emulate-tab t
  526. "Where should `org-cycle' emulate TAB.
  527. nil Never
  528. white Only in completely white lines
  529. whitestart Only at the beginning of lines, before the first non-white char
  530. t Everywhere except in headlines
  531. exc-hl-bol Everywhere except at the start of a headline
  532. If TAB is used in a place where it does not emulate TAB, the current subtree
  533. visibility is cycled."
  534. :group 'org-cycle
  535. :type '(choice (const :tag "Never" nil)
  536. (const :tag "Only in completely white lines" white)
  537. (const :tag "Before first char in a line" whitestart)
  538. (const :tag "Everywhere except in headlines" t)
  539. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  540. ))
  541. (defcustom org-cycle-separator-lines 2
  542. "Number of empty lines needed to keep an empty line between collapsed trees.
  543. If you leave an empty line between the end of a subtree and the following
  544. headline, this empty line is hidden when the subtree is folded.
  545. Org-mode will leave (exactly) one empty line visible if the number of
  546. empty lines is equal or larger to the number given in this variable.
  547. So the default 2 means, at least 2 empty lines after the end of a subtree
  548. are needed to produce free space between a collapsed subtree and the
  549. following headline.
  550. Special case: when 0, never leave empty lines in collapsed view."
  551. :group 'org-cycle
  552. :type 'integer)
  553. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  554. (defcustom org-pre-cycle-hook nil
  555. "Hook that is run before visibility cycling is happening.
  556. The function(s) in this hook must accept a single argument which indicates
  557. the new state that will be set right after running this hook. The
  558. argument is a symbol. Before a global state change, it can have the values
  559. `overview', `content', or `all'. Before a local state change, it can have
  560. the values `folded', `children', or `subtree'."
  561. :group 'org-cycle
  562. :type 'hook)
  563. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  564. org-cycle-hide-drawers
  565. org-cycle-show-empty-lines
  566. org-optimize-window-after-visibility-change)
  567. "Hook that is run after `org-cycle' has changed the buffer visibility.
  568. The function(s) in this hook must accept a single argument which indicates
  569. the new state that was set by the most recent `org-cycle' command. The
  570. argument is a symbol. After a global state change, it can have the values
  571. `overview', `content', or `all'. After a local state change, it can have
  572. the values `folded', `children', or `subtree'."
  573. :group 'org-cycle
  574. :type 'hook)
  575. (defgroup org-edit-structure nil
  576. "Options concerning structure editing in Org-mode."
  577. :tag "Org Edit Structure"
  578. :group 'org-structure)
  579. (defcustom org-odd-levels-only nil
  580. "Non-nil means, skip even levels and only use odd levels for the outline.
  581. This has the effect that two stars are being added/taken away in
  582. promotion/demotion commands. It also influences how levels are
  583. handled by the exporters.
  584. Changing it requires restart of `font-lock-mode' to become effective
  585. for fontification also in regions already fontified.
  586. You may also set this on a per-file basis by adding one of the following
  587. lines to the buffer:
  588. #+STARTUP: odd
  589. #+STARTUP: oddeven"
  590. :group 'org-edit-structure
  591. :group 'org-font-lock
  592. :type 'boolean)
  593. (defcustom org-adapt-indentation t
  594. "Non-nil means, adapt indentation when promoting and demoting.
  595. When this is set and the *entire* text in an entry is indented, the
  596. indentation is increased by one space in a demotion command, and
  597. decreased by one in a promotion command. If any line in the entry
  598. body starts at column 0, indentation is not changed at all."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-special-ctrl-a/e nil
  602. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  603. When t, `C-a' will bring back the cursor to the beginning of the
  604. headline text, i.e. after the stars and after a possible TODO keyword.
  605. In an item, this will be the position after the bullet.
  606. When the cursor is already at that position, another `C-a' will bring
  607. it to the beginning of the line.
  608. `C-e' will jump to the end of the headline, ignoring the presence of tags
  609. in the headline. A second `C-e' will then jump to the true end of the
  610. line, after any tags.
  611. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  612. and only a directly following, identical keypress will bring the cursor
  613. to the special positions.
  614. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  615. set separately."
  616. :group 'org-edit-structure
  617. :type '(choice
  618. (const :tag "off" nil)
  619. (const :tag "after stars/bullet and before tags first" t)
  620. (const :tag "true line boundary first" reversed)
  621. (cons :tag "Set C-a and C-e separately"
  622. (choice :tag "Special C-a"
  623. (const :tag "off" nil)
  624. (const :tag "after stars/bullet first" t)
  625. (const :tag "before stars/bullet first" reversed))
  626. (choice :tag "Special C-e"
  627. (const :tag "off" nil)
  628. (const :tag "before tags first" t)
  629. (const :tag "after tags first" reversed)))))
  630. (if (fboundp 'defvaralias)
  631. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  632. (defcustom org-special-ctrl-k nil
  633. "Non-nil means `C-k' will behave specially in headlines.
  634. When nil, `C-k' will call the default `kill-line' command.
  635. When t, the following will happen while the cursor is in the headline:
  636. - When the cursor is at the beginning of a headline, kill the entire
  637. line and possible the folded subtree below the line.
  638. - When in the middle of the headline text, kill the headline up to the tags.
  639. - When after the headline text, kill the tags."
  640. :group 'org-edit-structure
  641. :type 'boolean)
  642. (defcustom org-yank-folded-subtrees t
  643. "Non-nil means, when yanking subtrees, fold them.
  644. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  645. it starts with a heading and all other headings in it are either children
  646. or siblings, then fold all the subtrees. However, do this only if no
  647. text after the yank would be swallowed into a folded tree by this action."
  648. :group 'org-edit-structure
  649. :type 'boolean)
  650. (defcustom org-yank-adjusted-subtrees nil
  651. "Non-nil means, when yanking subtrees, adjust the level.
  652. With this setting, `org-paste-subtree' is used to insert the subtree, see
  653. this function for details."
  654. :group 'org-edit-structure
  655. :type 'boolean)
  656. (defcustom org-M-RET-may-split-line '((default . t))
  657. "Non-nil means, M-RET will split the line at the cursor position.
  658. When nil, it will go to the end of the line before making a
  659. new line.
  660. You may also set this option in a different way for different
  661. contexts. Valid contexts are:
  662. headline when creating a new headline
  663. item when creating a new item
  664. table in a table field
  665. default the value to be used for all contexts not explicitly
  666. customized"
  667. :group 'org-structure
  668. :group 'org-table
  669. :type '(choice
  670. (const :tag "Always" t)
  671. (const :tag "Never" nil)
  672. (repeat :greedy t :tag "Individual contexts"
  673. (cons
  674. (choice :tag "Context"
  675. (const headline)
  676. (const item)
  677. (const table)
  678. (const default))
  679. (boolean)))))
  680. (defcustom org-insert-heading-respect-content nil
  681. "Non-nil means, insert new headings after the current subtree.
  682. When nil, the new heading is created directly after the current line.
  683. The commands \\[org-insert-heading-respect-content] and
  684. \\[org-insert-todo-heading-respect-content] turn this variable on
  685. for the duration of the command."
  686. :group 'org-structure
  687. :type 'boolean)
  688. (defcustom org-blank-before-new-entry '((heading . auto)
  689. (plain-list-item . auto))
  690. "Should `org-insert-heading' leave a blank line before new heading/item?
  691. The value is an alist, with `heading' and `plain-list-item' as car,
  692. and a boolean flag as cdr. For plain lists, if the variable
  693. `org-empty-line-terminates-plain-lists' is set, the setting here
  694. is ignored and no empty line is inserted, to keep the list in tact."
  695. :group 'org-edit-structure
  696. :type '(list
  697. (cons (const heading)
  698. (choice (const :tag "Never" nil)
  699. (const :tag "Always" t)
  700. (const :tag "Auto" auto)))
  701. (cons (const plain-list-item)
  702. (choice (const :tag "Never" nil)
  703. (const :tag "Always" t)
  704. (const :tag "Auto" auto)))))
  705. (defcustom org-insert-heading-hook nil
  706. "Hook being run after inserting a new heading."
  707. :group 'org-edit-structure
  708. :type 'hook)
  709. (defcustom org-enable-fixed-width-editor t
  710. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  711. This currently only means, they are never auto-wrapped.
  712. When nil, such lines will be treated like ordinary lines.
  713. See also the QUOTE keyword."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defcustom org-edit-src-region-extra nil
  717. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  718. For examples see the function `org-edit-src-find-region-and-lang'.
  719. The regular expression identifying the begin marker should end with a newline,
  720. and the regexp marking the end line should start with a newline, to make sure
  721. there are kept outside the narrowed region."
  722. :group 'org-edit-structure
  723. :type '(repeat
  724. (list
  725. (regexp :tag "begin regexp")
  726. (regexp :tag "end regexp")
  727. (choice :tag "language"
  728. (string :tag "specify")
  729. (integer :tag "from match group")
  730. (const :tag "from `lang' element")
  731. (const :tag "from `style' element")))))
  732. (defcustom org-coderef-label-format "(ref:%s)"
  733. "The default coderef format.
  734. This format string will be used to search for coderef labels in literal
  735. examples (EXAMPLE and SRC blocks). The format can be overwritten
  736. an individual literal example with the -f option, like
  737. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  738. ...
  739. #+END_SRC
  740. If you want to use this for HTML export, make sure that the format does
  741. not introduce special font-locking, and avoid the HTML special
  742. characters `<', `>', and `&'. The reason for this restriction is that
  743. the labels are searched for only after htmlize has done its job."
  744. :group 'org-edit-structure ; FIXME this is not in the right group
  745. :type 'string)
  746. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  747. "The mode that should be used to edit fixed-width regions.
  748. These are the regions where each line starts with a colon."
  749. :group 'org-edit-structure
  750. :type '(choice
  751. (const artist-mode)
  752. (const picture-mode)
  753. (const fundamental-mode)
  754. (function :tag "Other (specify)")))
  755. (defcustom org-goto-auto-isearch t
  756. "Non-nil means, typing characters in org-goto starts incremental search."
  757. :group 'org-edit-structure
  758. :type 'boolean)
  759. (defgroup org-sparse-trees nil
  760. "Options concerning sparse trees in Org-mode."
  761. :tag "Org Sparse Trees"
  762. :group 'org-structure)
  763. (defcustom org-highlight-sparse-tree-matches t
  764. "Non-nil means, highlight all matches that define a sparse tree.
  765. The highlights will automatically disappear the next time the buffer is
  766. changed by an edit command."
  767. :group 'org-sparse-trees
  768. :type 'boolean)
  769. (defcustom org-remove-highlights-with-change t
  770. "Non-nil means, any change to the buffer will remove temporary highlights.
  771. Such highlights are created by `org-occur' and `org-clock-display'.
  772. When nil, `C-c C-c needs to be used to get rid of the highlights.
  773. The highlights created by `org-preview-latex-fragment' always need
  774. `C-c C-c' to be removed."
  775. :group 'org-sparse-trees
  776. :group 'org-time
  777. :type 'boolean)
  778. (defcustom org-occur-hook '(org-first-headline-recenter)
  779. "Hook that is run after `org-occur' has constructed a sparse tree.
  780. This can be used to recenter the window to show as much of the structure
  781. as possible."
  782. :group 'org-sparse-trees
  783. :type 'hook)
  784. (defgroup org-imenu-and-speedbar nil
  785. "Options concerning imenu and speedbar in Org-mode."
  786. :tag "Org Imenu and Speedbar"
  787. :group 'org-structure)
  788. (defcustom org-imenu-depth 2
  789. "The maximum level for Imenu access to Org-mode headlines.
  790. This also applied for speedbar access."
  791. :group 'org-imenu-and-speedbar
  792. :type 'integer)
  793. (defgroup org-table nil
  794. "Options concerning tables in Org-mode."
  795. :tag "Org Table"
  796. :group 'org)
  797. (defcustom org-enable-table-editor 'optimized
  798. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  799. When nil, such lines will be treated like ordinary lines.
  800. When equal to the symbol `optimized', the table editor will be optimized to
  801. do the following:
  802. - Automatic overwrite mode in front of whitespace in table fields.
  803. This makes the structure of the table stay in tact as long as the edited
  804. field does not exceed the column width.
  805. - Minimize the number of realigns. Normally, the table is aligned each time
  806. TAB or RET are pressed to move to another field. With optimization this
  807. happens only if changes to a field might have changed the column width.
  808. Optimization requires replacing the functions `self-insert-command',
  809. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  810. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  811. very good at guessing when a re-align will be necessary, but you can always
  812. force one with \\[org-ctrl-c-ctrl-c].
  813. If you would like to use the optimized version in Org-mode, but the
  814. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  815. This variable can be used to turn on and off the table editor during a session,
  816. but in order to toggle optimization, a restart is required.
  817. See also the variable `org-table-auto-blank-field'."
  818. :group 'org-table
  819. :type '(choice
  820. (const :tag "off" nil)
  821. (const :tag "on" t)
  822. (const :tag "on, optimized" optimized)))
  823. (defcustom org-self-insert-cluster-for-undo t
  824. "Non-nil means cluster self-insert commands for undo when possible.
  825. If this is set, then, like in the Emacs command loop, 20 consequtive
  826. characters will be undone together.
  827. This is configurable, because there is some impact on typing performance."
  828. :group 'org-table
  829. :type 'boolean)
  830. (defcustom org-table-tab-recognizes-table.el t
  831. "Non-nil means, TAB will automatically notice a table.el table.
  832. When it sees such a table, it moves point into it and - if necessary -
  833. calls `table-recognize-table'."
  834. :group 'org-table-editing
  835. :type 'boolean)
  836. (defgroup org-link nil
  837. "Options concerning links in Org-mode."
  838. :tag "Org Link"
  839. :group 'org)
  840. (defvar org-link-abbrev-alist-local nil
  841. "Buffer-local version of `org-link-abbrev-alist', which see.
  842. The value of this is taken from the #+LINK lines.")
  843. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  844. (defcustom org-link-abbrev-alist nil
  845. "Alist of link abbreviations.
  846. The car of each element is a string, to be replaced at the start of a link.
  847. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  848. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  849. [[linkkey:tag][description]]
  850. The 'linkkey' must be a word word, starting with a letter, followed
  851. by letters, numbers, '-' or '_'.
  852. If REPLACE is a string, the tag will simply be appended to create the link.
  853. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  854. the placeholder \"%h\" will cause a url-encoded version of the tag to
  855. be inserted at that point (see the function `url-hexify-string').
  856. REPLACE may also be a function that will be called with the tag as the
  857. only argument to create the link, which should be returned as a string.
  858. See the manual for examples."
  859. :group 'org-link
  860. :type '(repeat
  861. (cons
  862. (string :tag "Protocol")
  863. (choice
  864. (string :tag "Format")
  865. (function)))))
  866. (defcustom org-descriptive-links t
  867. "Non-nil means, hide link part and only show description of bracket links.
  868. Bracket links are like [[link][description]]. This variable sets the initial
  869. state in new org-mode buffers. The setting can then be toggled on a
  870. per-buffer basis from the Org->Hyperlinks menu."
  871. :group 'org-link
  872. :type 'boolean)
  873. (defcustom org-link-file-path-type 'adaptive
  874. "How the path name in file links should be stored.
  875. Valid values are:
  876. relative Relative to the current directory, i.e. the directory of the file
  877. into which the link is being inserted.
  878. absolute Absolute path, if possible with ~ for home directory.
  879. noabbrev Absolute path, no abbreviation of home directory.
  880. adaptive Use relative path for files in the current directory and sub-
  881. directories of it. For other files, use an absolute path."
  882. :group 'org-link
  883. :type '(choice
  884. (const relative)
  885. (const absolute)
  886. (const noabbrev)
  887. (const adaptive)))
  888. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  889. "Types of links that should be activated in Org-mode files.
  890. This is a list of symbols, each leading to the activation of a certain link
  891. type. In principle, it does not hurt to turn on most link types - there may
  892. be a small gain when turning off unused link types. The types are:
  893. bracket The recommended [[link][description]] or [[link]] links with hiding.
  894. angular Links in angular brackets that may contain whitespace like
  895. <bbdb:Carsten Dominik>.
  896. plain Plain links in normal text, no whitespace, like http://google.com.
  897. radio Text that is matched by a radio target, see manual for details.
  898. tag Tag settings in a headline (link to tag search).
  899. date Time stamps (link to calendar).
  900. footnote Footnote labels.
  901. Changing this variable requires a restart of Emacs to become effective."
  902. :group 'org-link
  903. :type '(set :greedy t
  904. (const :tag "Double bracket links (new style)" bracket)
  905. (const :tag "Angular bracket links (old style)" angular)
  906. (const :tag "Plain text links" plain)
  907. (const :tag "Radio target matches" radio)
  908. (const :tag "Tags" tag)
  909. (const :tag "Timestamps" date)
  910. (const :tag "Footnotes" footnote)))
  911. (defcustom org-make-link-description-function nil
  912. "Function to use to generate link descriptions from links. If
  913. nil the link location will be used. This function must take two
  914. parameters; the first is the link and the second the description
  915. org-insert-link has generated, and should return the description
  916. to use."
  917. :group 'org-link
  918. :type 'function)
  919. (defgroup org-link-store nil
  920. "Options concerning storing links in Org-mode."
  921. :tag "Org Store Link"
  922. :group 'org-link)
  923. (defcustom org-email-link-description-format "Email %c: %.30s"
  924. "Format of the description part of a link to an email or usenet message.
  925. The following %-escapes will be replaced by corresponding information:
  926. %F full \"From\" field
  927. %f name, taken from \"From\" field, address if no name
  928. %T full \"To\" field
  929. %t first name in \"To\" field, address if no name
  930. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  931. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  932. %s subject
  933. %m message-id.
  934. You may use normal field width specification between the % and the letter.
  935. This is for example useful to limit the length of the subject.
  936. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  937. :group 'org-link-store
  938. :type 'string)
  939. (defcustom org-from-is-user-regexp
  940. (let (r1 r2)
  941. (when (and user-mail-address (not (string= user-mail-address "")))
  942. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  943. (when (and user-full-name (not (string= user-full-name "")))
  944. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  945. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  946. "Regexp matched against the \"From:\" header of an email or usenet message.
  947. It should match if the message is from the user him/herself."
  948. :group 'org-link-store
  949. :type 'regexp)
  950. (defcustom org-link-to-org-use-id 'create-if-interactive
  951. "Non-nil means, storing a link to an Org file will use entry IDs.
  952. Note that before this variable is even considered, org-id must be loaded,
  953. to please customize `org-modules' and turn it on.
  954. The variable can have the following values:
  955. t Create an ID if needed to make a link to the current entry.
  956. create-if-interactive
  957. If `org-store-link' is called directly (interactively, as a user
  958. command), do create an ID to support the link. But when doing the
  959. job for remember, only use the ID if it already exists. The
  960. purpose of this setting is to avoid proliferation of unwanted
  961. IDs, just because you happen to be in an Org file when you
  962. call `org-remember' that automatically and preemptively
  963. creates a link. If you do want to get an ID link in a remember
  964. template to an entry not having an ID, create it first by
  965. explicitly creating a link to it, using `C-c C-l' first.
  966. use-existing
  967. Use existing ID, do not create one.
  968. nil Never use an ID to make a link, instead link using a text search for
  969. the headline text."
  970. :group 'org-link-store
  971. :type '(choice
  972. (const :tag "Create ID to make link" t)
  973. (const :tag "Create if string link interactively"
  974. 'create-if-interactive)
  975. (const :tag "Only use existing" 'use-existing)
  976. (const :tag "Do not use ID to create link" nil)))
  977. (defcustom org-context-in-file-links t
  978. "Non-nil means, file links from `org-store-link' contain context.
  979. A search string will be added to the file name with :: as separator and
  980. used to find the context when the link is activated by the command
  981. `org-open-at-point'.
  982. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  983. negates this setting for the duration of the command."
  984. :group 'org-link-store
  985. :type 'boolean)
  986. (defcustom org-keep-stored-link-after-insertion nil
  987. "Non-nil means, keep link in list for entire session.
  988. The command `org-store-link' adds a link pointing to the current
  989. location to an internal list. These links accumulate during a session.
  990. The command `org-insert-link' can be used to insert links into any
  991. Org-mode file (offering completion for all stored links). When this
  992. option is nil, every link which has been inserted once using \\[org-insert-link]
  993. will be removed from the list, to make completing the unused links
  994. more efficient."
  995. :group 'org-link-store
  996. :type 'boolean)
  997. (defgroup org-link-follow nil
  998. "Options concerning following links in Org-mode."
  999. :tag "Org Follow Link"
  1000. :group 'org-link)
  1001. (defcustom org-link-translation-function nil
  1002. "Function to translate links with different syntax to Org syntax.
  1003. This can be used to translate links created for example by the Planner
  1004. or emacs-wiki packages to Org syntax.
  1005. The function must accept two parameters, a TYPE containing the link
  1006. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1007. which is everything after the link protocol. It should return a cons
  1008. with possibly modified values of type and path.
  1009. Org contains a function for this, so if you set this variable to
  1010. `org-translate-link-from-planner', you should be able follow many
  1011. links created by planner."
  1012. :group 'org-link-follow
  1013. :type 'function)
  1014. (defcustom org-follow-link-hook nil
  1015. "Hook that is run after a link has been followed."
  1016. :group 'org-link-follow
  1017. :type 'hook)
  1018. (defcustom org-tab-follows-link nil
  1019. "Non-nil means, on links TAB will follow the link.
  1020. Needs to be set before org.el is loaded.
  1021. This really should not be used, it does not make sense, and the
  1022. implementation is bad."
  1023. :group 'org-link-follow
  1024. :type 'boolean)
  1025. (defcustom org-return-follows-link nil
  1026. "Non-nil means, on links RET will follow the link.
  1027. Needs to be set before org.el is loaded."
  1028. :group 'org-link-follow
  1029. :type 'boolean)
  1030. (defcustom org-mouse-1-follows-link
  1031. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1032. "Non-nil means, mouse-1 on a link will follow the link.
  1033. A longer mouse click will still set point. Does not work on XEmacs.
  1034. Needs to be set before org.el is loaded."
  1035. :group 'org-link-follow
  1036. :type 'boolean)
  1037. (defcustom org-mark-ring-length 4
  1038. "Number of different positions to be recorded in the ring
  1039. Changing this requires a restart of Emacs to work correctly."
  1040. :group 'org-link-follow
  1041. :type 'integer)
  1042. (defcustom org-link-frame-setup
  1043. '((vm . vm-visit-folder-other-frame)
  1044. (gnus . gnus-other-frame)
  1045. (file . find-file-other-window))
  1046. "Setup the frame configuration for following links.
  1047. When following a link with Emacs, it may often be useful to display
  1048. this link in another window or frame. This variable can be used to
  1049. set this up for the different types of links.
  1050. For VM, use any of
  1051. `vm-visit-folder'
  1052. `vm-visit-folder-other-frame'
  1053. For Gnus, use any of
  1054. `gnus'
  1055. `gnus-other-frame'
  1056. `org-gnus-no-new-news'
  1057. For FILE, use any of
  1058. `find-file'
  1059. `find-file-other-window'
  1060. `find-file-other-frame'
  1061. For the calendar, use the variable `calendar-setup'.
  1062. For BBDB, it is currently only possible to display the matches in
  1063. another window."
  1064. :group 'org-link-follow
  1065. :type '(list
  1066. (cons (const vm)
  1067. (choice
  1068. (const vm-visit-folder)
  1069. (const vm-visit-folder-other-window)
  1070. (const vm-visit-folder-other-frame)))
  1071. (cons (const gnus)
  1072. (choice
  1073. (const gnus)
  1074. (const gnus-other-frame)
  1075. (const org-gnus-no-new-news)))
  1076. (cons (const file)
  1077. (choice
  1078. (const find-file)
  1079. (const find-file-other-window)
  1080. (const find-file-other-frame)))))
  1081. (defcustom org-display-internal-link-with-indirect-buffer nil
  1082. "Non-nil means, use indirect buffer to display infile links.
  1083. Activating internal links (from one location in a file to another location
  1084. in the same file) normally just jumps to the location. When the link is
  1085. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1086. another window. When this option is set, the other window actually displays
  1087. an indirect buffer clone of the current buffer, to avoid any visibility
  1088. changes to the current buffer."
  1089. :group 'org-link-follow
  1090. :type 'boolean)
  1091. (defcustom org-open-non-existing-files nil
  1092. "Non-nil means, `org-open-file' will open non-existing files.
  1093. When nil, an error will be generated."
  1094. :group 'org-link-follow
  1095. :type 'boolean)
  1096. (defcustom org-open-directory-means-index-dot-org nil
  1097. "Non-nil means, a link to a directory really means to index.org.
  1098. When nil, following a directory link will run dired or open a finder/explorer
  1099. window on that directory."
  1100. :group 'org-link-follow
  1101. :type 'boolean)
  1102. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1103. "Function and arguments to call for following mailto links.
  1104. This is a list with the first element being a lisp function, and the
  1105. remaining elements being arguments to the function. In string arguments,
  1106. %a will be replaced by the address, and %s will be replaced by the subject
  1107. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1108. :group 'org-link-follow
  1109. :type '(choice
  1110. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1111. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1112. (const :tag "message-mail" (message-mail "%a" "%s"))
  1113. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1114. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1115. "Non-nil means, ask for confirmation before executing shell links.
  1116. Shell links can be dangerous: just think about a link
  1117. [[shell:rm -rf ~/*][Google Search]]
  1118. This link would show up in your Org-mode document as \"Google Search\",
  1119. but really it would remove your entire home directory.
  1120. Therefore we advise against setting this variable to nil.
  1121. Just change it to `y-or-n-p' if you want to confirm with a
  1122. single keystroke rather than having to type \"yes\"."
  1123. :group 'org-link-follow
  1124. :type '(choice
  1125. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1126. (const :tag "with y-or-n (faster)" y-or-n-p)
  1127. (const :tag "no confirmation (dangerous)" nil)))
  1128. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1129. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1130. Elisp links can be dangerous: just think about a link
  1131. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1132. This link would show up in your Org-mode document as \"Google Search\",
  1133. but really it would remove your entire home directory.
  1134. Therefore we advise against setting this variable to nil.
  1135. Just change it to `y-or-n-p' if you want to confirm with a
  1136. single keystroke rather than having to type \"yes\"."
  1137. :group 'org-link-follow
  1138. :type '(choice
  1139. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1140. (const :tag "with y-or-n (faster)" y-or-n-p)
  1141. (const :tag "no confirmation (dangerous)" nil)))
  1142. (defconst org-file-apps-defaults-gnu
  1143. '((remote . emacs)
  1144. (system . mailcap)
  1145. (t . mailcap))
  1146. "Default file applications on a UNIX or GNU/Linux system.
  1147. See `org-file-apps'.")
  1148. (defconst org-file-apps-defaults-macosx
  1149. '((remote . emacs)
  1150. (t . "open %s")
  1151. (system . "open %s")
  1152. ("ps.gz" . "gv %s")
  1153. ("eps.gz" . "gv %s")
  1154. ("dvi" . "xdvi %s")
  1155. ("fig" . "xfig %s"))
  1156. "Default file applications on a MacOS X system.
  1157. The system \"open\" is known as a default, but we use X11 applications
  1158. for some files for which the OS does not have a good default.
  1159. See `org-file-apps'.")
  1160. (defconst org-file-apps-defaults-windowsnt
  1161. (list
  1162. '(remote . emacs)
  1163. (cons t
  1164. (list (if (featurep 'xemacs)
  1165. 'mswindows-shell-execute
  1166. 'w32-shell-execute)
  1167. "open" 'file))
  1168. (cons 'system
  1169. (list (if (featurep 'xemacs)
  1170. 'mswindows-shell-execute
  1171. 'w32-shell-execute)
  1172. "open" 'file)))
  1173. "Default file applications on a Windows NT system.
  1174. The system \"open\" is used for most files.
  1175. See `org-file-apps'.")
  1176. (defcustom org-file-apps
  1177. '(
  1178. (auto-mode . emacs)
  1179. ("\\.x?html?\\'" . default)
  1180. ("\\.pdf\\'" . default)
  1181. )
  1182. "External applications for opening `file:path' items in a document.
  1183. Org-mode uses system defaults for different file types, but
  1184. you can use this variable to set the application for a given file
  1185. extension. The entries in this list are cons cells where the car identifies
  1186. files and the cdr the corresponding command. Possible values for the
  1187. file identifier are
  1188. \"regex\" Regular expression matched against the file name. For backward
  1189. compatibility, this can also be a string with only alphanumeric
  1190. characters, which is then interpreted as an extension.
  1191. `directory' Matches a directory
  1192. `remote' Matches a remote file, accessible through tramp or efs.
  1193. Remote files most likely should be visited through Emacs
  1194. because external applications cannot handle such paths.
  1195. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1196. so all files Emacs knows how to handle. Using this with
  1197. command `emacs' will open most files in Emacs. Beware that this
  1198. will also open html files inside Emacs, unless you add
  1199. (\"html\" . default) to the list as well.
  1200. t Default for files not matched by any of the other options.
  1201. `system' The system command to open files, like `open' on Windows
  1202. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1203. that will be selected if you call `C-c C-o' with a double
  1204. `C-u C-u' prefix.
  1205. Possible values for the command are:
  1206. `emacs' The file will be visited by the current Emacs process.
  1207. `default' Use the default application for this file type, which is the
  1208. association for t in the list, most likely in the system-specific
  1209. part.
  1210. This can be used to overrule an unwanted setting in the
  1211. system-specific variable.
  1212. `system' Use the system command for opening files, like \"open\".
  1213. This command is specified by the entry whose car is `system'.
  1214. Most likely, the system-specific version of this variable
  1215. does define this command, but you can overrule/replace it
  1216. here.
  1217. string A command to be executed by a shell; %s will be replaced
  1218. by the path to the file.
  1219. sexp A Lisp form which will be evaluated. The file path will
  1220. be available in the Lisp variable `file'.
  1221. For more examples, see the system specific constants
  1222. `org-file-apps-defaults-macosx'
  1223. `org-file-apps-defaults-windowsnt'
  1224. `org-file-apps-defaults-gnu'."
  1225. :group 'org-link-follow
  1226. :type '(repeat
  1227. (cons (choice :value ""
  1228. (string :tag "Extension")
  1229. (const :tag "System command to open files" system)
  1230. (const :tag "Default for unrecognized files" t)
  1231. (const :tag "Remote file" remote)
  1232. (const :tag "Links to a directory" directory)
  1233. (const :tag "Any files that have Emacs modes"
  1234. auto-mode))
  1235. (choice :value ""
  1236. (const :tag "Visit with Emacs" emacs)
  1237. (const :tag "Use default" default)
  1238. (const :tag "Use the system command" system)
  1239. (string :tag "Command")
  1240. (sexp :tag "Lisp form")))))
  1241. (defgroup org-refile nil
  1242. "Options concerning refiling entries in Org-mode."
  1243. :tag "Org Refile"
  1244. :group 'org)
  1245. (defcustom org-directory "~/org"
  1246. "Directory with org files.
  1247. This is just a default location to look for Org files. There is no need
  1248. at all to put your files into this directory. It is only used in the
  1249. following situations:
  1250. 1. When a remember template specifies a target file that is not an
  1251. absolute path. The path will then be interpreted relative to
  1252. `org-directory'
  1253. 2. When a remember note is filed away in an interactive way (when exiting the
  1254. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1255. with `org-directory' as the default path."
  1256. :group 'org-refile
  1257. :group 'org-remember
  1258. :type 'directory)
  1259. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1260. "Default target for storing notes.
  1261. Used by the hooks for remember.el. This can be a string, or nil to mean
  1262. the value of `remember-data-file'.
  1263. You can set this on a per-template basis with the variable
  1264. `org-remember-templates'."
  1265. :group 'org-refile
  1266. :group 'org-remember
  1267. :type '(choice
  1268. (const :tag "Default from remember-data-file" nil)
  1269. file))
  1270. (defcustom org-goto-interface 'outline
  1271. "The default interface to be used for `org-goto'.
  1272. Allowed values are:
  1273. outline The interface shows an outline of the relevant file
  1274. and the correct heading is found by moving through
  1275. the outline or by searching with incremental search.
  1276. outline-path-completion Headlines in the current buffer are offered via
  1277. completion. This is the interface also used by
  1278. the refile command."
  1279. :group 'org-refile
  1280. :type '(choice
  1281. (const :tag "Outline" outline)
  1282. (const :tag "Outline-path-completion" outline-path-completion)))
  1283. (defcustom org-goto-max-level 5
  1284. "Maximum level to be considered when running org-goto with refile interface."
  1285. :group 'org-refile
  1286. :type 'integer)
  1287. (defcustom org-reverse-note-order nil
  1288. "Non-nil means, store new notes at the beginning of a file or entry.
  1289. When nil, new notes will be filed to the end of a file or entry.
  1290. This can also be a list with cons cells of regular expressions that
  1291. are matched against file names, and values."
  1292. :group 'org-remember
  1293. :group 'org-refile
  1294. :type '(choice
  1295. (const :tag "Reverse always" t)
  1296. (const :tag "Reverse never" nil)
  1297. (repeat :tag "By file name regexp"
  1298. (cons regexp boolean))))
  1299. (defcustom org-refile-targets nil
  1300. "Targets for refiling entries with \\[org-refile].
  1301. This is list of cons cells. Each cell contains:
  1302. - a specification of the files to be considered, either a list of files,
  1303. or a symbol whose function or variable value will be used to retrieve
  1304. a file name or a list of file names. If you use `org-agenda-files' for
  1305. that, all agenda files will be scanned for targets. Nil means, consider
  1306. headings in the current buffer.
  1307. - A specification of how to find candidate refile targets. This may be
  1308. any of:
  1309. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1310. This tag has to be present in all target headlines, inheritance will
  1311. not be considered.
  1312. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1313. todo keyword.
  1314. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1315. headlines that are refiling targets.
  1316. - a cons cell (:level . N). Any headline of level N is considered a target.
  1317. Note that, when `org-odd-levels-only' is set, level corresponds to
  1318. order in hierarchy, not to the number of stars.
  1319. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1320. Note that, when `org-odd-levels-only' is set, level corresponds to
  1321. order in hierarchy, not to the number of stars.
  1322. When this variable is nil, all top-level headlines in the current buffer
  1323. are used, equivalent to the value `((nil . (:level . 1))'."
  1324. :group 'org-refile
  1325. :type '(repeat
  1326. (cons
  1327. (choice :value org-agenda-files
  1328. (const :tag "All agenda files" org-agenda-files)
  1329. (const :tag "Current buffer" nil)
  1330. (function) (variable) (file))
  1331. (choice :tag "Identify target headline by"
  1332. (cons :tag "Specific tag" (const :value :tag) (string))
  1333. (cons :tag "TODO keyword" (const :value :todo) (string))
  1334. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1335. (cons :tag "Level number" (const :value :level) (integer))
  1336. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1337. (defcustom org-refile-use-outline-path nil
  1338. "Non-nil means, provide refile targets as paths.
  1339. So a level 3 headline will be available as level1/level2/level3.
  1340. When the value is `file', also include the file name (without directory)
  1341. into the path. In this case, you can also stop the completion after
  1342. the file name, to get entries inserted as top level in the file.
  1343. When `full-file-path', include the full file path."
  1344. :group 'org-refile
  1345. :type '(choice
  1346. (const :tag "Not" nil)
  1347. (const :tag "Yes" t)
  1348. (const :tag "Start with file name" file)
  1349. (const :tag "Start with full file path" full-file-path)))
  1350. (defcustom org-outline-path-complete-in-steps t
  1351. "Non-nil means, complete the outline path in hierarchical steps.
  1352. When Org-mode uses the refile interface to select an outline path
  1353. \(see variable `org-refile-use-outline-path'), the completion of
  1354. the path can be done is a single go, or if can be done in steps down
  1355. the headline hierarchy. Going in steps is probably the best if you
  1356. do not use a special completion package like `ido' or `icicles'.
  1357. However, when using these packages, going in one step can be very
  1358. fast, while still showing the whole path to the entry."
  1359. :group 'org-refile
  1360. :type 'boolean)
  1361. (defgroup org-todo nil
  1362. "Options concerning TODO items in Org-mode."
  1363. :tag "Org TODO"
  1364. :group 'org)
  1365. (defgroup org-progress nil
  1366. "Options concerning Progress logging in Org-mode."
  1367. :tag "Org Progress"
  1368. :group 'org-time)
  1369. (defvar org-todo-interpretation-widgets
  1370. '(
  1371. (:tag "Sequence (cycling hits every state)" sequence)
  1372. (:tag "Type (cycling directly to DONE)" type))
  1373. "The available interpretation symbols for customizing
  1374. `org-todo-keywords'.
  1375. Interested libraries should add to this list.")
  1376. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1377. "List of TODO entry keyword sequences and their interpretation.
  1378. \\<org-mode-map>This is a list of sequences.
  1379. Each sequence starts with a symbol, either `sequence' or `type',
  1380. indicating if the keywords should be interpreted as a sequence of
  1381. action steps, or as different types of TODO items. The first
  1382. keywords are states requiring action - these states will select a headline
  1383. for inclusion into the global TODO list Org-mode produces. If one of
  1384. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1385. signify that no further action is necessary. If \"|\" is not found,
  1386. the last keyword is treated as the only DONE state of the sequence.
  1387. The command \\[org-todo] cycles an entry through these states, and one
  1388. additional state where no keyword is present. For details about this
  1389. cycling, see the manual.
  1390. TODO keywords and interpretation can also be set on a per-file basis with
  1391. the special #+SEQ_TODO and #+TYP_TODO lines.
  1392. Each keyword can optionally specify a character for fast state selection
  1393. \(in combination with the variable `org-use-fast-todo-selection')
  1394. and specifiers for state change logging, using the same syntax
  1395. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1396. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1397. indicates to record a time stamp each time this state is selected.
  1398. Each keyword may also specify if a timestamp or a note should be
  1399. recorded when entering or leaving the state, by adding additional
  1400. characters in the parenthesis after the keyword. This looks like this:
  1401. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1402. record only the time of the state change. With X and Y being either
  1403. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1404. Y when leaving the state if and only if the *target* state does not
  1405. define X. You may omit any of the fast-selection key or X or /Y,
  1406. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1407. For backward compatibility, this variable may also be just a list
  1408. of keywords - in this case the interpretation (sequence or type) will be
  1409. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1410. :group 'org-todo
  1411. :group 'org-keywords
  1412. :type '(choice
  1413. (repeat :tag "Old syntax, just keywords"
  1414. (string :tag "Keyword"))
  1415. (repeat :tag "New syntax"
  1416. (cons
  1417. (choice
  1418. :tag "Interpretation"
  1419. ;;Quick and dirty way to see
  1420. ;;`org-todo-interpretations'. This takes the
  1421. ;;place of item arguments
  1422. :convert-widget
  1423. (lambda (widget)
  1424. (widget-put widget
  1425. :args (mapcar
  1426. #'(lambda (x)
  1427. (widget-convert
  1428. (cons 'const x)))
  1429. org-todo-interpretation-widgets))
  1430. widget))
  1431. (repeat
  1432. (string :tag "Keyword"))))))
  1433. (defvar org-todo-keywords-1 nil
  1434. "All TODO and DONE keywords active in a buffer.")
  1435. (make-variable-buffer-local 'org-todo-keywords-1)
  1436. (defvar org-todo-keywords-for-agenda nil)
  1437. (defvar org-done-keywords-for-agenda nil)
  1438. (defvar org-todo-keyword-alist-for-agenda nil)
  1439. (defvar org-tag-alist-for-agenda nil)
  1440. (defvar org-agenda-contributing-files nil)
  1441. (defvar org-not-done-keywords nil)
  1442. (make-variable-buffer-local 'org-not-done-keywords)
  1443. (defvar org-done-keywords nil)
  1444. (make-variable-buffer-local 'org-done-keywords)
  1445. (defvar org-todo-heads nil)
  1446. (make-variable-buffer-local 'org-todo-heads)
  1447. (defvar org-todo-sets nil)
  1448. (make-variable-buffer-local 'org-todo-sets)
  1449. (defvar org-todo-log-states nil)
  1450. (make-variable-buffer-local 'org-todo-log-states)
  1451. (defvar org-todo-kwd-alist nil)
  1452. (make-variable-buffer-local 'org-todo-kwd-alist)
  1453. (defvar org-todo-key-alist nil)
  1454. (make-variable-buffer-local 'org-todo-key-alist)
  1455. (defvar org-todo-key-trigger nil)
  1456. (make-variable-buffer-local 'org-todo-key-trigger)
  1457. (defcustom org-todo-interpretation 'sequence
  1458. "Controls how TODO keywords are interpreted.
  1459. This variable is in principle obsolete and is only used for
  1460. backward compatibility, if the interpretation of todo keywords is
  1461. not given already in `org-todo-keywords'. See that variable for
  1462. more information."
  1463. :group 'org-todo
  1464. :group 'org-keywords
  1465. :type '(choice (const sequence)
  1466. (const type)))
  1467. (defcustom org-use-fast-todo-selection t
  1468. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1469. This variable describes if and under what circumstances the cycling
  1470. mechanism for TODO keywords will be replaced by a single-key, direct
  1471. selection scheme.
  1472. When nil, fast selection is never used.
  1473. When the symbol `prefix', it will be used when `org-todo' is called with
  1474. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1475. in an agenda buffer.
  1476. When t, fast selection is used by default. In this case, the prefix
  1477. argument forces cycling instead.
  1478. In all cases, the special interface is only used if access keys have actually
  1479. been assigned by the user, i.e. if keywords in the configuration are followed
  1480. by a letter in parenthesis, like TODO(t)."
  1481. :group 'org-todo
  1482. :type '(choice
  1483. (const :tag "Never" nil)
  1484. (const :tag "By default" t)
  1485. (const :tag "Only with C-u C-c C-t" prefix)))
  1486. (defcustom org-provide-todo-statistics t
  1487. "Non-nil means, update todo statistics after insert and toggle.
  1488. When this is set, todo statistics is updated in the parent of the current
  1489. entry each time a todo state is changed."
  1490. :group 'org-todo
  1491. :type 'boolean)
  1492. (defcustom org-after-todo-state-change-hook nil
  1493. "Hook which is run after the state of a TODO item was changed.
  1494. The new state (a string with a TODO keyword, or nil) is available in the
  1495. Lisp variable `state'."
  1496. :group 'org-todo
  1497. :type 'hook)
  1498. (defvar org-blocker-hook nil
  1499. "Hook for functions that are allowed to block a state change.
  1500. Each function gets as its single argument a property list, see
  1501. `org-trigger-hook' for more information about this list.
  1502. If any of the functions in this hook returns nil, the state change
  1503. is blocked.")
  1504. (defvar org-trigger-hook nil
  1505. "Hook for functions that are triggered by a state change.
  1506. Each function gets as its single argument a property list with at least
  1507. the following elements:
  1508. (:type type-of-change :position pos-at-entry-start
  1509. :from old-state :to new-state)
  1510. Depending on the type, more properties may be present.
  1511. This mechanism is currently implemented for:
  1512. TODO state changes
  1513. ------------------
  1514. :type todo-state-change
  1515. :from previous state (keyword as a string), or nil, or a symbol
  1516. 'todo' or 'done', to indicate the general type of state.
  1517. :to new state, like in :from")
  1518. (defcustom org-enforce-todo-dependencies nil
  1519. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1520. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1521. be blocked if any prior sibling is not yet done.
  1522. Finally, if the parent is blocked because of ordered siblings of its own,
  1523. the child will also be blocked.
  1524. This variable needs to be set before org.el is loaded, and you need to
  1525. restart Emacs after a change to make the change effective. The only way
  1526. to change is while Emacs is running is through the customize interface."
  1527. :set (lambda (var val)
  1528. (set var val)
  1529. (if val
  1530. (add-hook 'org-blocker-hook
  1531. 'org-block-todo-from-children-or-siblings-or-parent)
  1532. (remove-hook 'org-blocker-hook
  1533. 'org-block-todo-from-children-or-siblings-or-parent)))
  1534. :group 'org-todo
  1535. :type 'boolean)
  1536. (defcustom org-enforce-todo-checkbox-dependencies nil
  1537. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1538. When this is nil, checkboxes have no influence on switching TODO states.
  1539. When non-nil, you first need to check off all check boxes before the TODO
  1540. entry can be switched to DONE.
  1541. This variable needs to be set before org.el is loaded, and you need to
  1542. restart Emacs after a change to make the change effective. The only way
  1543. to change is while Emacs is running is through the customize interface."
  1544. :set (lambda (var val)
  1545. (set var val)
  1546. (if val
  1547. (add-hook 'org-blocker-hook
  1548. 'org-block-todo-from-checkboxes)
  1549. (remove-hook 'org-blocker-hook
  1550. 'org-block-todo-from-checkboxes)))
  1551. :group 'org-todo
  1552. :type 'boolean)
  1553. (defcustom org-todo-state-tags-triggers nil
  1554. "Tag changes that should be triggered by TODO state changes.
  1555. This is a list. Each entry is
  1556. (state-change (tag . flag) .......)
  1557. State-change can be a string with a state, and empty string to indicate the
  1558. state that has no TODO keyword, or it can be one of the symbols `todo'
  1559. or `done', meaning any not-done or done state, respectively."
  1560. :group 'org-todo
  1561. :group 'org-tags
  1562. :type '(repeat
  1563. (cons (choice :tag "When changing to"
  1564. (const :tag "Not-done state" todo)
  1565. (const :tag "Done state" done)
  1566. (string :tag "State"))
  1567. (repeat
  1568. (cons :tag "Tag action"
  1569. (string :tag "Tag")
  1570. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1571. (defcustom org-log-done nil
  1572. "Information to record when a task moves to the DONE state.
  1573. Possible values are:
  1574. nil Don't add anything, just change the keyword
  1575. time Add a time stamp to the task
  1576. note Prompt a closing note and add it with template `org-log-note-headings'
  1577. This option can also be set with on a per-file-basis with
  1578. #+STARTUP: nologdone
  1579. #+STARTUP: logdone
  1580. #+STARTUP: lognotedone
  1581. You can have local logging settings for a subtree by setting the LOGGING
  1582. property to one or more of these keywords."
  1583. :group 'org-todo
  1584. :group 'org-progress
  1585. :type '(choice
  1586. (const :tag "No logging" nil)
  1587. (const :tag "Record CLOSED timestamp" time)
  1588. (const :tag "Record CLOSED timestamp with closing note." note)))
  1589. ;; Normalize old uses of org-log-done.
  1590. (cond
  1591. ((eq org-log-done t) (setq org-log-done 'time))
  1592. ((and (listp org-log-done) (memq 'done org-log-done))
  1593. (setq org-log-done 'note)))
  1594. (defcustom org-log-note-clock-out nil
  1595. "Non-nil means, record a note when clocking out of an item.
  1596. This can also be configured on a per-file basis by adding one of
  1597. the following lines anywhere in the buffer:
  1598. #+STARTUP: lognoteclock-out
  1599. #+STARTUP: nolognoteclock-out"
  1600. :group 'org-todo
  1601. :group 'org-progress
  1602. :type 'boolean)
  1603. (defcustom org-log-done-with-time t
  1604. "Non-nil means, the CLOSED time stamp will contain date and time.
  1605. When nil, only the date will be recorded."
  1606. :group 'org-progress
  1607. :type 'boolean)
  1608. (defcustom org-log-note-headings
  1609. '((done . "CLOSING NOTE %t")
  1610. (state . "State %-12s from %-12S %t")
  1611. (note . "Note taken on %t")
  1612. (clock-out . ""))
  1613. "Headings for notes added to entries.
  1614. The value is an alist, with the car being a symbol indicating the note
  1615. context, and the cdr is the heading to be used. The heading may also be the
  1616. empty string.
  1617. %t in the heading will be replaced by a time stamp.
  1618. %s will be replaced by the new TODO state, in double quotes.
  1619. %S will be replaced by the old TODO state, in double quotes.
  1620. %u will be replaced by the user name.
  1621. %U will be replaced by the full user name."
  1622. :group 'org-todo
  1623. :group 'org-progress
  1624. :type '(list :greedy t
  1625. (cons (const :tag "Heading when closing an item" done) string)
  1626. (cons (const :tag
  1627. "Heading when changing todo state (todo sequence only)"
  1628. state) string)
  1629. (cons (const :tag "Heading when just taking a note" note) string)
  1630. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1631. (unless (assq 'note org-log-note-headings)
  1632. (push '(note . "%t") org-log-note-headings))
  1633. (defcustom org-log-into-drawer nil
  1634. "Non-nil means, insert state change notes and time stamps into a drawer.
  1635. When nil, state changes notes will be inserted after the headline and
  1636. any scheduling and clock lines, but not inside a drawer.
  1637. The value of this variable should be the name of the drawer to use.
  1638. LOGBOOK is proposed at the default drawer for this purpose, you can
  1639. also set this to a string to define the drawer of your choice.
  1640. A value of t is also allowed, representing \"LOGBOOK\".
  1641. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1642. will be ignored."
  1643. :group 'org-todo
  1644. :group 'org-progress
  1645. :type '(choice
  1646. (const :tag "Not into a drawer" nil)
  1647. (const :tag "LOGBOOK" t)
  1648. (string :tag "Other")))
  1649. (if (fboundp 'defvaralias)
  1650. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1651. (defcustom org-log-state-notes-insert-after-drawers nil
  1652. "Non-nil means, insert state change notes after any drawers in entry.
  1653. Only the drawers that *immediately* follow the headline and the
  1654. deadline/scheduled line are skipped.
  1655. When nil, insert notes right after the heading and perhaps the line
  1656. with deadline/scheduling if present.
  1657. This variable will have no effect if `org-log-into-drawer' is
  1658. set."
  1659. :group 'org-todo
  1660. :group 'org-progress
  1661. :type 'boolean)
  1662. (defcustom org-log-states-order-reversed t
  1663. "Non-nil means, the latest state change note will be directly after heading.
  1664. When nil, the notes will be orderer according to time."
  1665. :group 'org-todo
  1666. :group 'org-progress
  1667. :type 'boolean)
  1668. (defcustom org-log-repeat 'time
  1669. "Non-nil means, record moving through the DONE state when triggering repeat.
  1670. An auto-repeating tasks is immediately switched back to TODO when marked
  1671. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1672. the TODO keyword definition, or recording a closing note by setting
  1673. `org-log-done', there will be no record of the task moving through DONE.
  1674. This variable forces taking a note anyway. Possible values are:
  1675. nil Don't force a record
  1676. time Record a time stamp
  1677. note Record a note
  1678. This option can also be set with on a per-file-basis with
  1679. #+STARTUP: logrepeat
  1680. #+STARTUP: lognoterepeat
  1681. #+STARTUP: nologrepeat
  1682. You can have local logging settings for a subtree by setting the LOGGING
  1683. property to one or more of these keywords."
  1684. :group 'org-todo
  1685. :group 'org-progress
  1686. :type '(choice
  1687. (const :tag "Don't force a record" nil)
  1688. (const :tag "Force recording the DONE state" time)
  1689. (const :tag "Force recording a note with the DONE state" note)))
  1690. (defgroup org-priorities nil
  1691. "Priorities in Org-mode."
  1692. :tag "Org Priorities"
  1693. :group 'org-todo)
  1694. (defcustom org-highest-priority ?A
  1695. "The highest priority of TODO items. A character like ?A, ?B etc.
  1696. Must have a smaller ASCII number than `org-lowest-priority'."
  1697. :group 'org-priorities
  1698. :type 'character)
  1699. (defcustom org-lowest-priority ?C
  1700. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1701. Must have a larger ASCII number than `org-highest-priority'."
  1702. :group 'org-priorities
  1703. :type 'character)
  1704. (defcustom org-default-priority ?B
  1705. "The default priority of TODO items.
  1706. This is the priority an item get if no explicit priority is given."
  1707. :group 'org-priorities
  1708. :type 'character)
  1709. (defcustom org-priority-start-cycle-with-default t
  1710. "Non-nil means, start with default priority when starting to cycle.
  1711. When this is nil, the first step in the cycle will be (depending on the
  1712. command used) one higher or lower that the default priority."
  1713. :group 'org-priorities
  1714. :type 'boolean)
  1715. (defgroup org-time nil
  1716. "Options concerning time stamps and deadlines in Org-mode."
  1717. :tag "Org Time"
  1718. :group 'org)
  1719. (defcustom org-insert-labeled-timestamps-at-point nil
  1720. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1721. When nil, these labeled time stamps are forces into the second line of an
  1722. entry, just after the headline. When scheduling from the global TODO list,
  1723. the time stamp will always be forced into the second line."
  1724. :group 'org-time
  1725. :type 'boolean)
  1726. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1727. "Formats for `format-time-string' which are used for time stamps.
  1728. It is not recommended to change this constant.")
  1729. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1730. "Number of minutes to round time stamps to.
  1731. These are two values, the first applies when first creating a time stamp.
  1732. The second applies when changing it with the commands `S-up' and `S-down'.
  1733. When changing the time stamp, this means that it will change in steps
  1734. of N minutes, as given by the second value.
  1735. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1736. numbers should be factors of 60, so for example 5, 10, 15.
  1737. When this is larger than 1, you can still force an exact time-stamp by using
  1738. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1739. and by using a prefix arg to `S-up/down' to specify the exact number
  1740. of minutes to shift."
  1741. :group 'org-time
  1742. :get '(lambda (var) ; Make sure all entries have 5 elements
  1743. (if (integerp (default-value var))
  1744. (list (default-value var) 5)
  1745. (default-value var)))
  1746. :type '(list
  1747. (integer :tag "when inserting times")
  1748. (integer :tag "when modifying times")))
  1749. ;; Normalize old customizations of this variable.
  1750. (when (integerp org-time-stamp-rounding-minutes)
  1751. (setq org-time-stamp-rounding-minutes
  1752. (list org-time-stamp-rounding-minutes
  1753. org-time-stamp-rounding-minutes)))
  1754. (defcustom org-display-custom-times nil
  1755. "Non-nil means, overlay custom formats over all time stamps.
  1756. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1757. To turn this on on a per-file basis, insert anywhere in the file:
  1758. #+STARTUP: customtime"
  1759. :group 'org-time
  1760. :set 'set-default
  1761. :type 'sexp)
  1762. (make-variable-buffer-local 'org-display-custom-times)
  1763. (defcustom org-time-stamp-custom-formats
  1764. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1765. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1766. These are overlayed over the default ISO format if the variable
  1767. `org-display-custom-times' is set. Time like %H:%M should be at the
  1768. end of the second format. The custom formats are also honored by export
  1769. commands, if custom time display is turned on at the time of export."
  1770. :group 'org-time
  1771. :type 'sexp)
  1772. (defun org-time-stamp-format (&optional long inactive)
  1773. "Get the right format for a time string."
  1774. (let ((f (if long (cdr org-time-stamp-formats)
  1775. (car org-time-stamp-formats))))
  1776. (if inactive
  1777. (concat "[" (substring f 1 -1) "]")
  1778. f)))
  1779. (defcustom org-time-clocksum-format "%d:%02d"
  1780. "The format string used when creating CLOCKSUM lines, or when
  1781. org-mode generates a time duration."
  1782. :group 'org-time
  1783. :type 'string)
  1784. (defcustom org-deadline-warning-days 14
  1785. "No. of days before expiration during which a deadline becomes active.
  1786. This variable governs the display in sparse trees and in the agenda.
  1787. When 0 or negative, it means use this number (the absolute value of it)
  1788. even if a deadline has a different individual lead time specified.
  1789. Custom commands can set this variable in the options section."
  1790. :group 'org-time
  1791. :group 'org-agenda-daily/weekly
  1792. :type 'integer)
  1793. (defcustom org-read-date-prefer-future t
  1794. "Non-nil means, assume future for incomplete date input from user.
  1795. This affects the following situations:
  1796. 1. The user gives a day, but no month.
  1797. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1798. read this as the third of *next* month. However, if you enter \"17\",
  1799. it will be considered as *this* month.
  1800. 2. The user gives a month but not a year.
  1801. For example, if it is april and you enter \"feb 2\", this will be read
  1802. as feb 2, *next* year. \"May 5\", however, will be this year.
  1803. Currently this does not work for ISO week specifications.
  1804. When this option is nil, the current month and year will always be used
  1805. as defaults."
  1806. :group 'org-time
  1807. :type 'boolean)
  1808. (defcustom org-read-date-display-live t
  1809. "Non-nil means, display current interpretation of date prompt live.
  1810. This display will be in an overlay, in the minibuffer."
  1811. :group 'org-time
  1812. :type 'boolean)
  1813. (defcustom org-read-date-popup-calendar t
  1814. "Non-nil means, pop up a calendar when prompting for a date.
  1815. In the calendar, the date can be selected with mouse-1. However, the
  1816. minibuffer will also be active, and you can simply enter the date as well.
  1817. When nil, only the minibuffer will be available."
  1818. :group 'org-time
  1819. :type 'boolean)
  1820. (if (fboundp 'defvaralias)
  1821. (defvaralias 'org-popup-calendar-for-date-prompt
  1822. 'org-read-date-popup-calendar))
  1823. (defcustom org-read-date-minibuffer-setup-hook nil
  1824. "Hook to be used to set up keys for the date/time interface.
  1825. Add key definitions to `minibuffer-local-map', which will be a temporary
  1826. copy."
  1827. :group 'org-time
  1828. :type 'hook)
  1829. (defcustom org-extend-today-until 0
  1830. "The hour when your day really ends. Must be an integer.
  1831. This has influence for the following applications:
  1832. - When switching the agenda to \"today\". It it is still earlier than
  1833. the time given here, the day recognized as TODAY is actually yesterday.
  1834. - When a date is read from the user and it is still before the time given
  1835. here, the current date and time will be assumed to be yesterday, 23:59.
  1836. Also, timestamps inserted in remember templates follow this rule.
  1837. IMPORTANT: This is a feature whose implementation is and likely will
  1838. remain incomplete. Really, it is only here because past midnight seems to
  1839. be the favorite working time of John Wiegley :-)"
  1840. :group 'org-time
  1841. :type 'integer)
  1842. (defcustom org-edit-timestamp-down-means-later nil
  1843. "Non-nil means, S-down will increase the time in a time stamp.
  1844. When nil, S-up will increase."
  1845. :group 'org-time
  1846. :type 'boolean)
  1847. (defcustom org-calendar-follow-timestamp-change t
  1848. "Non-nil means, make the calendar window follow timestamp changes.
  1849. When a timestamp is modified and the calendar window is visible, it will be
  1850. moved to the new date."
  1851. :group 'org-time
  1852. :type 'boolean)
  1853. (defgroup org-tags nil
  1854. "Options concerning tags in Org-mode."
  1855. :tag "Org Tags"
  1856. :group 'org)
  1857. (defcustom org-tag-alist nil
  1858. "List of tags allowed in Org-mode files.
  1859. When this list is nil, Org-mode will base TAG input on what is already in the
  1860. buffer.
  1861. The value of this variable is an alist, the car of each entry must be a
  1862. keyword as a string, the cdr may be a character that is used to select
  1863. that tag through the fast-tag-selection interface.
  1864. See the manual for details."
  1865. :group 'org-tags
  1866. :type '(repeat
  1867. (choice
  1868. (cons (string :tag "Tag name")
  1869. (character :tag "Access char"))
  1870. (const :tag "Start radio group" (:startgroup))
  1871. (const :tag "End radio group" (:endgroup))
  1872. (const :tag "New line" (:newline)))))
  1873. (defcustom org-tag-persistent-alist nil
  1874. "List of tags that will always appear in all Org-mode files.
  1875. This is in addition to any in buffer settings or customizations
  1876. of `org-tag-alist'.
  1877. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1878. The value of this variable is an alist, the car of each entry must be a
  1879. keyword as a string, the cdr may be a character that is used to select
  1880. that tag through the fast-tag-selection interface.
  1881. See the manual for details.
  1882. To disable these tags on a per-file basis, insert anywhere in the file:
  1883. #+STARTUP: noptag"
  1884. :group 'org-tags
  1885. :type '(repeat
  1886. (choice
  1887. (cons (string :tag "Tag name")
  1888. (character :tag "Access char"))
  1889. (const :tag "Start radio group" (:startgroup))
  1890. (const :tag "End radio group" (:endgroup))
  1891. (const :tag "New line" (:newline)))))
  1892. (defvar org-file-tags nil
  1893. "List of tags that can be inherited by all entries in the file.
  1894. The tags will be inherited if the variable `org-use-tag-inheritance'
  1895. says they should be.
  1896. This variable is populated from #+TAG lines.")
  1897. (defcustom org-use-fast-tag-selection 'auto
  1898. "Non-nil means, use fast tag selection scheme.
  1899. This is a special interface to select and deselect tags with single keys.
  1900. When nil, fast selection is never used.
  1901. When the symbol `auto', fast selection is used if and only if selection
  1902. characters for tags have been configured, either through the variable
  1903. `org-tag-alist' or through a #+TAGS line in the buffer.
  1904. When t, fast selection is always used and selection keys are assigned
  1905. automatically if necessary."
  1906. :group 'org-tags
  1907. :type '(choice
  1908. (const :tag "Always" t)
  1909. (const :tag "Never" nil)
  1910. (const :tag "When selection characters are configured" 'auto)))
  1911. (defcustom org-fast-tag-selection-single-key nil
  1912. "Non-nil means, fast tag selection exits after first change.
  1913. When nil, you have to press RET to exit it.
  1914. During fast tag selection, you can toggle this flag with `C-c'.
  1915. This variable can also have the value `expert'. In this case, the window
  1916. displaying the tags menu is not even shown, until you press C-c again."
  1917. :group 'org-tags
  1918. :type '(choice
  1919. (const :tag "No" nil)
  1920. (const :tag "Yes" t)
  1921. (const :tag "Expert" expert)))
  1922. (defvar org-fast-tag-selection-include-todo nil
  1923. "Non-nil means, fast tags selection interface will also offer TODO states.
  1924. This is an undocumented feature, you should not rely on it.")
  1925. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1926. "The column to which tags should be indented in a headline.
  1927. If this number is positive, it specifies the column. If it is negative,
  1928. it means that the tags should be flushright to that column. For example,
  1929. -80 works well for a normal 80 character screen."
  1930. :group 'org-tags
  1931. :type 'integer)
  1932. (defcustom org-auto-align-tags t
  1933. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1934. These operations change the length of a headline and therefore shift
  1935. the tags around. With this options turned on, after each such operation
  1936. the tags are again aligned to `org-tags-column'."
  1937. :group 'org-tags
  1938. :type 'boolean)
  1939. (defcustom org-use-tag-inheritance t
  1940. "Non-nil means, tags in levels apply also for sublevels.
  1941. When nil, only the tags directly given in a specific line apply there.
  1942. This may also be a list of tags that should be inherited, or a regexp that
  1943. matches tags that should be inherited. Additional control is possible
  1944. with the variable `org-tags-exclude-from-inheritance' which gives an
  1945. explicit list of tags to be excluded from inheritance., even if the value of
  1946. `org-use-tag-inheritance' would select it for inheritance.
  1947. If this option is t, a match early-on in a tree can lead to a large
  1948. number of matches in the subtree when constructing the agenda or creating
  1949. a sparse tree. If you only want to see the first match in a tree during
  1950. a search, check out the variable `org-tags-match-list-sublevels'."
  1951. :group 'org-tags
  1952. :type '(choice
  1953. (const :tag "Not" nil)
  1954. (const :tag "Always" t)
  1955. (repeat :tag "Specific tags" (string :tag "Tag"))
  1956. (regexp :tag "Tags matched by regexp")))
  1957. (defcustom org-tags-exclude-from-inheritance nil
  1958. "List of tags that should never be inherited.
  1959. This is a way to exclude a few tags from inheritance. For way to do
  1960. the opposite, to actively allow inheritance for selected tags,
  1961. see the variable `org-use-tag-inheritance'."
  1962. :group 'org-tags
  1963. :type '(repeat (string :tag "Tag")))
  1964. (defun org-tag-inherit-p (tag)
  1965. "Check if TAG is one that should be inherited."
  1966. (cond
  1967. ((member tag org-tags-exclude-from-inheritance) nil)
  1968. ((eq org-use-tag-inheritance t) t)
  1969. ((not org-use-tag-inheritance) nil)
  1970. ((stringp org-use-tag-inheritance)
  1971. (string-match org-use-tag-inheritance tag))
  1972. ((listp org-use-tag-inheritance)
  1973. (member tag org-use-tag-inheritance))
  1974. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1975. (defcustom org-tags-match-list-sublevels t
  1976. "Non-nil means list also sublevels of headlines matching a search.
  1977. This variable applies to tags/property searches, and also to stuck
  1978. projects because this search is based on a tags match as well.
  1979. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1980. the sublevels of a headline matching a tag search often also match
  1981. the same search. Listing all of them can create very long lists.
  1982. Setting this variable to nil causes subtrees of a match to be skipped.
  1983. As a special case, if the tag search is restricted to TODO items, the
  1984. value of this variable is ignored and sublevels are always checked, to
  1985. make sure all corresponding TODO items find their way into the list.
  1986. This variable is semi-obsolete and probably should always be true. It
  1987. is better to limit inheritance to certain tags using the variables
  1988. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1989. :group 'org-tags
  1990. :type '(choice
  1991. (const :tag "No, don't list them" nil)
  1992. (const :tag "Yes, do list them" t)
  1993. (const :tag "List them, indented with leading dots" indented)))
  1994. (defvar org-tags-history nil
  1995. "History of minibuffer reads for tags.")
  1996. (defvar org-last-tags-completion-table nil
  1997. "The last used completion table for tags.")
  1998. (defvar org-after-tags-change-hook nil
  1999. "Hook that is run after the tags in a line have changed.")
  2000. (defgroup org-properties nil
  2001. "Options concerning properties in Org-mode."
  2002. :tag "Org Properties"
  2003. :group 'org)
  2004. (defcustom org-property-format "%-10s %s"
  2005. "How property key/value pairs should be formatted by `indent-line'.
  2006. When `indent-line' hits a property definition, it will format the line
  2007. according to this format, mainly to make sure that the values are
  2008. lined-up with respect to each other."
  2009. :group 'org-properties
  2010. :type 'string)
  2011. (defcustom org-use-property-inheritance nil
  2012. "Non-nil means, properties apply also for sublevels.
  2013. This setting is chiefly used during property searches. Turning it on can
  2014. cause significant overhead when doing a search, which is why it is not
  2015. on by default.
  2016. When nil, only the properties directly given in the current entry count.
  2017. When t, every property is inherited. The value may also be a list of
  2018. properties that should have inheritance, or a regular expression matching
  2019. properties that should be inherited.
  2020. However, note that some special properties use inheritance under special
  2021. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2022. and the properties ending in \"_ALL\" when they are used as descriptor
  2023. for valid values of a property.
  2024. Note for programmers:
  2025. When querying an entry with `org-entry-get', you can control if inheritance
  2026. should be used. By default, `org-entry-get' looks only at the local
  2027. properties. You can request inheritance by setting the inherit argument
  2028. to t (to force inheritance) or to `selective' (to respect the setting
  2029. in this variable)."
  2030. :group 'org-properties
  2031. :type '(choice
  2032. (const :tag "Not" nil)
  2033. (const :tag "Always" t)
  2034. (repeat :tag "Specific properties" (string :tag "Property"))
  2035. (regexp :tag "Properties matched by regexp")))
  2036. (defun org-property-inherit-p (property)
  2037. "Check if PROPERTY is one that should be inherited."
  2038. (cond
  2039. ((eq org-use-property-inheritance t) t)
  2040. ((not org-use-property-inheritance) nil)
  2041. ((stringp org-use-property-inheritance)
  2042. (string-match org-use-property-inheritance property))
  2043. ((listp org-use-property-inheritance)
  2044. (member property org-use-property-inheritance))
  2045. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2046. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2047. "The default column format, if no other format has been defined.
  2048. This variable can be set on the per-file basis by inserting a line
  2049. #+COLUMNS: %25ITEM ....."
  2050. :group 'org-properties
  2051. :type 'string)
  2052. (defcustom org-columns-ellipses ".."
  2053. "The ellipses to be used when a field in column view is truncated.
  2054. When this is the empty string, as many characters as possible are shown,
  2055. but then there will be no visual indication that the field has been truncated.
  2056. When this is a string of length N, the last N characters of a truncated
  2057. field are replaced by this string. If the column is narrower than the
  2058. ellipses string, only part of the ellipses string will be shown."
  2059. :group 'org-properties
  2060. :type 'string)
  2061. (defcustom org-columns-modify-value-for-display-function nil
  2062. "Function that modifies values for display in column view.
  2063. For example, it can be used to cut out a certain part from a time stamp.
  2064. The function must take 2 arguments:
  2065. column-title The title of the column (*not* the property name)
  2066. value The value that should be modified.
  2067. The function should return the value that should be displayed,
  2068. or nil if the normal value should be used."
  2069. :group 'org-properties
  2070. :type 'function)
  2071. (defcustom org-effort-property "Effort"
  2072. "The property that is being used to keep track of effort estimates.
  2073. Effort estimates given in this property need to have the format H:MM."
  2074. :group 'org-properties
  2075. :group 'org-progress
  2076. :type '(string :tag "Property"))
  2077. (defconst org-global-properties-fixed
  2078. '(("VISIBILITY_ALL" . "folded children content all"))
  2079. "List of property/value pairs that can be inherited by any entry.
  2080. These are fixed values, for the preset properties. The user variable
  2081. that can be used to add to this list is `org-global-properties'.
  2082. The entries in this list are cons cells where the car is a property
  2083. name and cdr is a string with the value. If the value represents
  2084. multiple items like an \"_ALL\" property, separate the items by
  2085. spaces.")
  2086. (defcustom org-global-properties nil
  2087. "List of property/value pairs that can be inherited by any entry.
  2088. This list will be combined with the constant `org-global-properties-fixed'.
  2089. The entries in this list are cons cells where the car is a property
  2090. name and cdr is a string with the value.
  2091. You can set buffer-local values for the same purpose in the variable
  2092. `org-file-properties' this by adding lines like
  2093. #+PROPERTY: NAME VALUE"
  2094. :group 'org-properties
  2095. :type '(repeat
  2096. (cons (string :tag "Property")
  2097. (string :tag "Value"))))
  2098. (defvar org-file-properties nil
  2099. "List of property/value pairs that can be inherited by any entry.
  2100. Valid for the current buffer.
  2101. This variable is populated from #+PROPERTY lines.")
  2102. (make-variable-buffer-local 'org-file-properties)
  2103. (defgroup org-agenda nil
  2104. "Options concerning agenda views in Org-mode."
  2105. :tag "Org Agenda"
  2106. :group 'org)
  2107. (defvar org-category nil
  2108. "Variable used by org files to set a category for agenda display.
  2109. Such files should use a file variable to set it, for example
  2110. # -*- mode: org; org-category: \"ELisp\"
  2111. or contain a special line
  2112. #+CATEGORY: ELisp
  2113. If the file does not specify a category, then file's base name
  2114. is used instead.")
  2115. (make-variable-buffer-local 'org-category)
  2116. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2117. (defcustom org-agenda-files nil
  2118. "The files to be used for agenda display.
  2119. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2120. \\[org-remove-file]. You can also use customize to edit the list.
  2121. If an entry is a directory, all files in that directory that are matched by
  2122. `org-agenda-file-regexp' will be part of the file list.
  2123. If the value of the variable is not a list but a single file name, then
  2124. the list of agenda files is actually stored and maintained in that file, one
  2125. agenda file per line."
  2126. :group 'org-agenda
  2127. :type '(choice
  2128. (repeat :tag "List of files and directories" file)
  2129. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2130. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2131. "Regular expression to match files for `org-agenda-files'.
  2132. If any element in the list in that variable contains a directory instead
  2133. of a normal file, all files in that directory that are matched by this
  2134. regular expression will be included."
  2135. :group 'org-agenda
  2136. :type 'regexp)
  2137. (defcustom org-agenda-text-search-extra-files nil
  2138. "List of extra files to be searched by text search commands.
  2139. These files will be search in addition to the agenda files by the
  2140. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2141. Note that these files will only be searched for text search commands,
  2142. not for the other agenda views like todo lists, tag searches or the weekly
  2143. agenda. This variable is intended to list notes and possibly archive files
  2144. that should also be searched by these two commands.
  2145. In fact, if the first element in the list is the symbol `agenda-archives',
  2146. than all archive files of all agenda files will be added to the search
  2147. scope."
  2148. :group 'org-agenda
  2149. :type '(set :greedy t
  2150. (const :tag "Agenda Archives" agenda-archives)
  2151. (repeat :inline t (file))))
  2152. (if (fboundp 'defvaralias)
  2153. (defvaralias 'org-agenda-multi-occur-extra-files
  2154. 'org-agenda-text-search-extra-files))
  2155. (defcustom org-agenda-skip-unavailable-files nil
  2156. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2157. A nil value means to remove them, after a query, from the list."
  2158. :group 'org-agenda
  2159. :type 'boolean)
  2160. (defcustom org-calendar-to-agenda-key [?c]
  2161. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2162. The command `org-calendar-goto-agenda' will be bound to this key. The
  2163. default is the character `c' because then `c' can be used to switch back and
  2164. forth between agenda and calendar."
  2165. :group 'org-agenda
  2166. :type 'sexp)
  2167. (defcustom org-calendar-agenda-action-key [?k]
  2168. "The key to be installed in `calendar-mode-map' for agenda-action.
  2169. The command `org-agenda-action' will be bound to this key. The
  2170. default is the character `k' because we use the same key in the agenda."
  2171. :group 'org-agenda
  2172. :type 'sexp)
  2173. (eval-after-load "calendar"
  2174. '(progn
  2175. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2176. 'org-calendar-goto-agenda)
  2177. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2178. 'org-agenda-action)))
  2179. (defgroup org-latex nil
  2180. "Options for embedding LaTeX code into Org-mode."
  2181. :tag "Org LaTeX"
  2182. :group 'org)
  2183. (defcustom org-format-latex-options
  2184. '(:foreground default :background default :scale 1.0
  2185. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2186. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2187. "Options for creating images from LaTeX fragments.
  2188. This is a property list with the following properties:
  2189. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2190. `default' means use the foreground of the default face.
  2191. :background the background color, or \"Transparent\".
  2192. `default' means use the background of the default face.
  2193. :scale a scaling factor for the size of the images.
  2194. :html-foreground, :html-background, :html-scale
  2195. the same numbers for HTML export.
  2196. :matchers a list indicating which matchers should be used to
  2197. find LaTeX fragments. Valid members of this list are:
  2198. \"begin\" find environments
  2199. \"$1\" find single characters surrounded by $.$
  2200. \"$\" find math expressions surrounded by $...$
  2201. \"$$\" find math expressions surrounded by $$....$$
  2202. \"\\(\" find math expressions surrounded by \\(...\\)
  2203. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2204. :group 'org-latex
  2205. :type 'plist)
  2206. (defcustom org-format-latex-header "\\documentclass{article}
  2207. \\usepackage{fullpage} % do not remove
  2208. \\usepackage{amssymb}
  2209. \\usepackage[usenames]{color}
  2210. \\usepackage{amsmath}
  2211. \\usepackage{latexsym}
  2212. \\usepackage[mathscr]{eucal}
  2213. \\pagestyle{empty} % do not remove"
  2214. "The document header used for processing LaTeX fragments."
  2215. :group 'org-latex
  2216. :type 'string)
  2217. (defgroup org-font-lock nil
  2218. "Font-lock settings for highlighting in Org-mode."
  2219. :tag "Org Font Lock"
  2220. :group 'org)
  2221. (defcustom org-level-color-stars-only nil
  2222. "Non-nil means fontify only the stars in each headline.
  2223. When nil, the entire headline is fontified.
  2224. Changing it requires restart of `font-lock-mode' to become effective
  2225. also in regions already fontified."
  2226. :group 'org-font-lock
  2227. :type 'boolean)
  2228. (defcustom org-hide-leading-stars nil
  2229. "Non-nil means, hide the first N-1 stars in a headline.
  2230. This works by using the face `org-hide' for these stars. This
  2231. face is white for a light background, and black for a dark
  2232. background. You may have to customize the face `org-hide' to
  2233. make this work.
  2234. Changing it requires restart of `font-lock-mode' to become effective
  2235. also in regions already fontified.
  2236. You may also set this on a per-file basis by adding one of the following
  2237. lines to the buffer:
  2238. #+STARTUP: hidestars
  2239. #+STARTUP: showstars"
  2240. :group 'org-font-lock
  2241. :type 'boolean)
  2242. (defcustom org-fontify-done-headline nil
  2243. "Non-nil means, change the face of a headline if it is marked DONE.
  2244. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2245. When this is non-nil, the headline after the keyword is set to the
  2246. `org-headline-done' as an additional indication."
  2247. :group 'org-font-lock
  2248. :type 'boolean)
  2249. (defcustom org-fontify-emphasized-text t
  2250. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2251. Changing this variable requires a restart of Emacs to take effect."
  2252. :group 'org-font-lock
  2253. :type 'boolean)
  2254. (defcustom org-highlight-latex-fragments-and-specials nil
  2255. "Non-nil means, fontify what is treated specially by the exporters."
  2256. :group 'org-font-lock
  2257. :type 'boolean)
  2258. (defcustom org-hide-emphasis-markers nil
  2259. "Non-nil mean font-lock should hide the emphasis marker characters."
  2260. :group 'org-font-lock
  2261. :type 'boolean)
  2262. (defvar org-emph-re nil
  2263. "Regular expression for matching emphasis.")
  2264. (defvar org-verbatim-re nil
  2265. "Regular expression for matching verbatim text.")
  2266. (defvar org-emphasis-regexp-components) ; defined just below
  2267. (defvar org-emphasis-alist) ; defined just below
  2268. (defun org-set-emph-re (var val)
  2269. "Set variable and compute the emphasis regular expression."
  2270. (set var val)
  2271. (when (and (boundp 'org-emphasis-alist)
  2272. (boundp 'org-emphasis-regexp-components)
  2273. org-emphasis-alist org-emphasis-regexp-components)
  2274. (let* ((e org-emphasis-regexp-components)
  2275. (pre (car e))
  2276. (post (nth 1 e))
  2277. (border (nth 2 e))
  2278. (body (nth 3 e))
  2279. (nl (nth 4 e))
  2280. (body1 (concat body "*?"))
  2281. (markers (mapconcat 'car org-emphasis-alist ""))
  2282. (vmarkers (mapconcat
  2283. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2284. org-emphasis-alist "")))
  2285. ;; make sure special characters appear at the right position in the class
  2286. (if (string-match "\\^" markers)
  2287. (setq markers (concat (replace-match "" t t markers) "^")))
  2288. (if (string-match "-" markers)
  2289. (setq markers (concat (replace-match "" t t markers) "-")))
  2290. (if (string-match "\\^" vmarkers)
  2291. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2292. (if (string-match "-" vmarkers)
  2293. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2294. (if (> nl 0)
  2295. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2296. (int-to-string nl) "\\}")))
  2297. ;; Make the regexp
  2298. (setq org-emph-re
  2299. (concat "\\([" pre "]\\|^\\)"
  2300. "\\("
  2301. "\\([" markers "]\\)"
  2302. "\\("
  2303. "[^" border "]\\|"
  2304. "[^" border "]"
  2305. body1
  2306. "[^" border "]"
  2307. "\\)"
  2308. "\\3\\)"
  2309. "\\([" post "]\\|$\\)"))
  2310. (setq org-verbatim-re
  2311. (concat "\\([" pre "]\\|^\\)"
  2312. "\\("
  2313. "\\([" vmarkers "]\\)"
  2314. "\\("
  2315. "[^" border "]\\|"
  2316. "[^" border "]"
  2317. body1
  2318. "[^" border "]"
  2319. "\\)"
  2320. "\\3\\)"
  2321. "\\([" post "]\\|$\\)")))))
  2322. (defcustom org-emphasis-regexp-components
  2323. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2324. "Components used to build the regular expression for emphasis.
  2325. This is a list with 6 entries. Terminology: In an emphasis string
  2326. like \" *strong word* \", we call the initial space PREMATCH, the final
  2327. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2328. and \"trong wor\" is the body. The different components in this variable
  2329. specify what is allowed/forbidden in each part:
  2330. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2331. post Chars allowed as postmatch. End of line will be allowed too.
  2332. border The chars *forbidden* as border characters.
  2333. body-regexp A regexp like \".\" to match a body character. Don't use
  2334. non-shy groups here, and don't allow newline here.
  2335. newline The maximum number of newlines allowed in an emphasis exp.
  2336. Use customize to modify this, or restart Emacs after changing it."
  2337. :group 'org-font-lock
  2338. :set 'org-set-emph-re
  2339. :type '(list
  2340. (sexp :tag "Allowed chars in pre ")
  2341. (sexp :tag "Allowed chars in post ")
  2342. (sexp :tag "Forbidden chars in border ")
  2343. (sexp :tag "Regexp for body ")
  2344. (integer :tag "number of newlines allowed")
  2345. (option (boolean :tag "Please ignore this button"))))
  2346. (defcustom org-emphasis-alist
  2347. `(("*" bold "<b>" "</b>")
  2348. ("/" italic "<i>" "</i>")
  2349. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2350. ("=" org-code "<code>" "</code>" verbatim)
  2351. ("~" org-verbatim "<code>" "</code>" verbatim)
  2352. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2353. "<del>" "</del>")
  2354. )
  2355. "Special syntax for emphasized text.
  2356. Text starting and ending with a special character will be emphasized, for
  2357. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2358. characters, the face to be used by font-lock for highlighting in Org-mode
  2359. Emacs buffers, and the HTML tags to be used for this.
  2360. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2361. Use customize to modify this, or restart Emacs after changing it."
  2362. :group 'org-font-lock
  2363. :set 'org-set-emph-re
  2364. :type '(repeat
  2365. (list
  2366. (string :tag "Marker character")
  2367. (choice
  2368. (face :tag "Font-lock-face")
  2369. (plist :tag "Face property list"))
  2370. (string :tag "HTML start tag")
  2371. (string :tag "HTML end tag")
  2372. (option (const verbatim)))))
  2373. ;;; Miscellaneous options
  2374. (defgroup org-completion nil
  2375. "Completion in Org-mode."
  2376. :tag "Org Completion"
  2377. :group 'org)
  2378. (defcustom org-completion-use-ido nil
  2379. "Non-nil means, use ido completion wherever possible.
  2380. Note that `ido-mode' must be active for this variable to be relevant.
  2381. If you decide to turn this variable on, you might well want to turn off
  2382. `org-outline-path-complete-in-steps'."
  2383. :group 'org-completion
  2384. :type 'boolean)
  2385. (defcustom org-completion-fallback-command 'hippie-expand
  2386. "The expansion command called by \\[org-complete] in normal context.
  2387. Normal means, no org-mode-specific context."
  2388. :group 'org-completion
  2389. :type 'function)
  2390. ;;; Functions and variables from ther packages
  2391. ;; Declared here to avoid compiler warnings
  2392. ;; XEmacs only
  2393. (defvar outline-mode-menu-heading)
  2394. (defvar outline-mode-menu-show)
  2395. (defvar outline-mode-menu-hide)
  2396. (defvar zmacs-regions) ; XEmacs regions
  2397. ;; Emacs only
  2398. (defvar mark-active)
  2399. ;; Various packages
  2400. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2401. (declare-function calendar-forward-day "cal-move" (arg))
  2402. (declare-function calendar-goto-date "cal-move" (date))
  2403. (declare-function calendar-goto-today "cal-move" ())
  2404. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2405. (defvar calc-embedded-close-formula)
  2406. (defvar calc-embedded-open-formula)
  2407. (declare-function cdlatex-tab "ext:cdlatex" ())
  2408. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2409. (defvar font-lock-unfontify-region-function)
  2410. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2411. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2412. (defvar iswitchb-temp-buflist)
  2413. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2414. (defvar org-agenda-tags-todo-honor-ignore-options)
  2415. (declare-function org-agenda-skip "org-agenda" ())
  2416. (declare-function org-format-agenda-item "org-agenda"
  2417. (extra txt &optional category tags dotime noprefix remove-re))
  2418. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2419. (declare-function org-agenda-change-all-lines "org-agenda"
  2420. (newhead hdmarker &optional fixface just-this))
  2421. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2422. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2423. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2424. (beg end))
  2425. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2426. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2427. "org-agenda" (&optional end))
  2428. (declare-function parse-time-string "parse-time" (string))
  2429. (declare-function remember "remember" (&optional initial))
  2430. (declare-function remember-buffer-desc "remember" ())
  2431. (declare-function remember-finalize "remember" ())
  2432. (defvar remember-save-after-remembering)
  2433. (defvar remember-data-file)
  2434. (defvar remember-register)
  2435. (defvar remember-buffer)
  2436. (defvar remember-handler-functions)
  2437. (defvar remember-annotation-functions)
  2438. (defvar texmathp-why)
  2439. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2440. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2441. (defvar w3m-current-url)
  2442. (defvar w3m-current-title)
  2443. (defvar org-latex-regexps)
  2444. ;;; Autoload and prepare some org modules
  2445. ;; Some table stuff that needs to be defined here, because it is used
  2446. ;; by the functions setting up org-mode or checking for table context.
  2447. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2448. "Detects an org-type or table-type table.")
  2449. (defconst org-table-line-regexp "^[ \t]*|"
  2450. "Detects an org-type table line.")
  2451. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2452. "Detects an org-type table line.")
  2453. (defconst org-table-hline-regexp "^[ \t]*|-"
  2454. "Detects an org-type table hline.")
  2455. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2456. "Detects a table-type table hline.")
  2457. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2458. "Searching from within a table (any type) this finds the first line
  2459. outside the table.")
  2460. ;; Autoload the functions in org-table.el that are needed by functions here.
  2461. (eval-and-compile
  2462. (org-autoload "org-table"
  2463. '(org-table-align org-table-begin org-table-blank-field
  2464. org-table-convert org-table-convert-region org-table-copy-down
  2465. org-table-copy-region org-table-create
  2466. org-table-create-or-convert-from-region
  2467. org-table-create-with-table.el org-table-current-dline
  2468. org-table-cut-region org-table-delete-column org-table-edit-field
  2469. org-table-edit-formulas org-table-end org-table-eval-formula
  2470. org-table-export org-table-field-info
  2471. org-table-get-stored-formulas org-table-goto-column
  2472. org-table-hline-and-move org-table-import org-table-insert-column
  2473. org-table-insert-hline org-table-insert-row org-table-iterate
  2474. org-table-justify-field-maybe org-table-kill-row
  2475. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2476. org-table-move-column org-table-move-column-left
  2477. org-table-move-column-right org-table-move-row
  2478. org-table-move-row-down org-table-move-row-up
  2479. org-table-next-field org-table-next-row org-table-paste-rectangle
  2480. org-table-previous-field org-table-recalculate
  2481. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2482. org-table-toggle-coordinate-overlays
  2483. org-table-toggle-formula-debugger org-table-wrap-region
  2484. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2485. (defun org-at-table-p (&optional table-type)
  2486. "Return t if the cursor is inside an org-type table.
  2487. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2488. (if org-enable-table-editor
  2489. (save-excursion
  2490. (beginning-of-line 1)
  2491. (looking-at (if table-type org-table-any-line-regexp
  2492. org-table-line-regexp)))
  2493. nil))
  2494. (defsubst org-table-p () (org-at-table-p))
  2495. (defun org-at-table.el-p ()
  2496. "Return t if and only if we are at a table.el table."
  2497. (and (org-at-table-p 'any)
  2498. (save-excursion
  2499. (goto-char (org-table-begin 'any))
  2500. (looking-at org-table1-hline-regexp))))
  2501. (defun org-table-recognize-table.el ()
  2502. "If there is a table.el table nearby, recognize it and move into it."
  2503. (if org-table-tab-recognizes-table.el
  2504. (if (org-at-table.el-p)
  2505. (progn
  2506. (beginning-of-line 1)
  2507. (if (looking-at org-table-dataline-regexp)
  2508. nil
  2509. (if (looking-at org-table1-hline-regexp)
  2510. (progn
  2511. (beginning-of-line 2)
  2512. (if (looking-at org-table-any-border-regexp)
  2513. (beginning-of-line -1)))))
  2514. (if (re-search-forward "|" (org-table-end t) t)
  2515. (progn
  2516. (require 'table)
  2517. (if (table--at-cell-p (point))
  2518. t
  2519. (message "recognizing table.el table...")
  2520. (table-recognize-table)
  2521. (message "recognizing table.el table...done")))
  2522. (error "This should not happen..."))
  2523. t)
  2524. nil)
  2525. nil))
  2526. (defun org-at-table-hline-p ()
  2527. "Return t if the cursor is inside a hline in a table."
  2528. (if org-enable-table-editor
  2529. (save-excursion
  2530. (beginning-of-line 1)
  2531. (looking-at org-table-hline-regexp))
  2532. nil))
  2533. (defvar org-table-clean-did-remove-column nil)
  2534. (defun org-table-map-tables (function)
  2535. "Apply FUNCTION to the start of all tables in the buffer."
  2536. (save-excursion
  2537. (save-restriction
  2538. (widen)
  2539. (goto-char (point-min))
  2540. (while (re-search-forward org-table-any-line-regexp nil t)
  2541. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2542. (beginning-of-line 1)
  2543. (if (looking-at org-table-line-regexp)
  2544. (save-excursion (funcall function)))
  2545. (re-search-forward org-table-any-border-regexp nil 1))))
  2546. (message "Mapping tables: done"))
  2547. ;; Declare and autoload functions from org-exp.el & Co
  2548. (declare-function org-default-export-plist "org-exp")
  2549. (declare-function org-infile-export-plist "org-exp")
  2550. (declare-function org-get-current-options "org-exp")
  2551. (eval-and-compile
  2552. (org-autoload "org-exp"
  2553. '(org-export org-export-visible
  2554. org-insert-export-options-template
  2555. org-table-clean-before-export))
  2556. (org-autoload "org-ascii"
  2557. '(org-export-as-ascii org-export-ascii-preprocess))
  2558. (org-autoload "org-html"
  2559. '(org-export-as-html-and-open
  2560. org-export-as-html-batch org-export-as-html-to-buffer
  2561. org-replace-region-by-html org-export-region-as-html
  2562. org-export-as-html))
  2563. (org-autoload "org-icalendar"
  2564. '(org-export-icalendar-this-file
  2565. org-export-icalendar-all-agenda-files
  2566. org-export-icalendar-combine-agenda-files))
  2567. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2568. ;; Declare and autoload functions from org-agenda.el
  2569. (eval-and-compile
  2570. (org-autoload "org-agenda"
  2571. '(org-agenda org-agenda-list org-search-view
  2572. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2573. org-diary org-agenda-to-appt
  2574. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2575. ;; Autoload org-remember
  2576. (eval-and-compile
  2577. (org-autoload "org-remember"
  2578. '(org-remember-insinuate org-remember-annotation
  2579. org-remember-apply-template org-remember org-remember-handler)))
  2580. ;; Autoload org-clock.el
  2581. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2582. (beg end))
  2583. (declare-function org-clock-update-mode-line "org-clock" ())
  2584. (defvar org-clock-start-time)
  2585. (defvar org-clock-marker (make-marker)
  2586. "Marker recording the last clock-in.")
  2587. (eval-and-compile
  2588. (org-autoload
  2589. "org-clock"
  2590. '(org-clock-in org-clock-out org-clock-cancel
  2591. org-clock-goto org-clock-sum org-clock-display
  2592. org-clock-remove-overlays org-clock-report
  2593. org-clocktable-shift org-dblock-write:clocktable
  2594. org-get-clocktable)))
  2595. (defun org-clock-update-time-maybe ()
  2596. "If this is a CLOCK line, update it and return t.
  2597. Otherwise, return nil."
  2598. (interactive)
  2599. (save-excursion
  2600. (beginning-of-line 1)
  2601. (skip-chars-forward " \t")
  2602. (when (looking-at org-clock-string)
  2603. (let ((re (concat "[ \t]*" org-clock-string
  2604. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2605. "\\([ \t]*=>.*\\)?\\)?"))
  2606. ts te h m s neg)
  2607. (cond
  2608. ((not (looking-at re))
  2609. nil)
  2610. ((not (match-end 2))
  2611. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2612. (> org-clock-marker (point))
  2613. (<= org-clock-marker (point-at-eol)))
  2614. ;; The clock is running here
  2615. (setq org-clock-start-time
  2616. (apply 'encode-time
  2617. (org-parse-time-string (match-string 1))))
  2618. (org-clock-update-mode-line)))
  2619. (t
  2620. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2621. (end-of-line 1)
  2622. (setq ts (match-string 1)
  2623. te (match-string 3))
  2624. (setq s (- (time-to-seconds
  2625. (apply 'encode-time (org-parse-time-string te)))
  2626. (time-to-seconds
  2627. (apply 'encode-time (org-parse-time-string ts))))
  2628. neg (< s 0)
  2629. s (abs s)
  2630. h (floor (/ s 3600))
  2631. s (- s (* 3600 h))
  2632. m (floor (/ s 60))
  2633. s (- s (* 60 s)))
  2634. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2635. t))))))
  2636. (defun org-check-running-clock ()
  2637. "Check if the current buffer contains the running clock.
  2638. If yes, offer to stop it and to save the buffer with the changes."
  2639. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2640. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2641. (buffer-name))))
  2642. (org-clock-out)
  2643. (when (y-or-n-p "Save changed buffer?")
  2644. (save-buffer))))
  2645. (defun org-clocktable-try-shift (dir n)
  2646. "Check if this line starts a clock table, if yes, shift the time block."
  2647. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2648. (org-clocktable-shift dir n)))
  2649. ;; Autoload org-timer.el
  2650. (eval-and-compile
  2651. (org-autoload
  2652. "org-timer"
  2653. '(org-timer-start org-timer org-timer-item
  2654. org-timer-change-times-in-region)))
  2655. ;; Autoload org-feed.el
  2656. (eval-and-compile
  2657. (org-autoload
  2658. "org-feed"
  2659. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2660. ;; Autoload archiving code
  2661. ;; The stuff that is needed for cycling and tags has to be defined here.
  2662. (defgroup org-archive nil
  2663. "Options concerning archiving in Org-mode."
  2664. :tag "Org Archive"
  2665. :group 'org-structure)
  2666. (defcustom org-archive-location "%s_archive::"
  2667. "The location where subtrees should be archived.
  2668. The value of this variable is a string, consisting of two parts,
  2669. separated by a double-colon. The first part is a filename and
  2670. the second part is a headline.
  2671. When the filename is omitted, archiving happens in the same file.
  2672. %s in the filename will be replaced by the current file
  2673. name (without the directory part). Archiving to a different file
  2674. is useful to keep archived entries from contributing to the
  2675. Org-mode Agenda.
  2676. The archived entries will be filed as subtrees of the specified
  2677. headline. When the headline is omitted, the subtrees are simply
  2678. filed away at the end of the file, as top-level entries. Also in
  2679. the heading you can use %s to represent the file name, this can be
  2680. useful when using the same archive for a number of different files.
  2681. Here are a few examples:
  2682. \"%s_archive::\"
  2683. If the current file is Projects.org, archive in file
  2684. Projects.org_archive, as top-level trees. This is the default.
  2685. \"::* Archived Tasks\"
  2686. Archive in the current file, under the top-level headline
  2687. \"* Archived Tasks\".
  2688. \"~/org/archive.org::\"
  2689. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2690. \"~/org/archive.org::From %s\"
  2691. Archive in file ~/org/archive.org (absolute path), und headlines
  2692. \"From FILENAME\" where file name is the current file name.
  2693. \"basement::** Finished Tasks\"
  2694. Archive in file ./basement (relative path), as level 3 trees
  2695. below the level 2 heading \"** Finished Tasks\".
  2696. You may set this option on a per-file basis by adding to the buffer a
  2697. line like
  2698. #+ARCHIVE: basement::** Finished Tasks
  2699. You may also define it locally for a subtree by setting an ARCHIVE property
  2700. in the entry. If such a property is found in an entry, or anywhere up
  2701. the hierarchy, it will be used."
  2702. :group 'org-archive
  2703. :type 'string)
  2704. (defcustom org-archive-tag "ARCHIVE"
  2705. "The tag that marks a subtree as archived.
  2706. An archived subtree does not open during visibility cycling, and does
  2707. not contribute to the agenda listings.
  2708. After changing this, font-lock must be restarted in the relevant buffers to
  2709. get the proper fontification."
  2710. :group 'org-archive
  2711. :group 'org-keywords
  2712. :type 'string)
  2713. (defcustom org-agenda-skip-archived-trees t
  2714. "Non-nil means, the agenda will skip any items located in archived trees.
  2715. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2716. variable is no longer recommended, you should leave it at the value t.
  2717. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2718. :group 'org-archive
  2719. :group 'org-agenda-skip
  2720. :type 'boolean)
  2721. (defcustom org-columns-skip-arrchived-trees t
  2722. "Non-nil means, irgnore archived trees when creating column view."
  2723. :group 'org-archive
  2724. :group 'org-properties
  2725. :type 'boolean)
  2726. (defcustom org-cycle-open-archived-trees nil
  2727. "Non-nil means, `org-cycle' will open archived trees.
  2728. An archived tree is a tree marked with the tag ARCHIVE.
  2729. When nil, archived trees will stay folded. You can still open them with
  2730. normal outline commands like `show-all', but not with the cycling commands."
  2731. :group 'org-archive
  2732. :group 'org-cycle
  2733. :type 'boolean)
  2734. (defcustom org-sparse-tree-open-archived-trees nil
  2735. "Non-nil means sparse tree construction shows matches in archived trees.
  2736. When nil, matches in these trees are highlighted, but the trees are kept in
  2737. collapsed state."
  2738. :group 'org-archive
  2739. :group 'org-sparse-trees
  2740. :type 'boolean)
  2741. (defun org-cycle-hide-archived-subtrees (state)
  2742. "Re-hide all archived subtrees after a visibility state change."
  2743. (when (and (not org-cycle-open-archived-trees)
  2744. (not (memq state '(overview folded))))
  2745. (save-excursion
  2746. (let* ((globalp (memq state '(contents all)))
  2747. (beg (if globalp (point-min) (point)))
  2748. (end (if globalp (point-max) (org-end-of-subtree t))))
  2749. (org-hide-archived-subtrees beg end)
  2750. (goto-char beg)
  2751. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2752. (message "%s" (substitute-command-keys
  2753. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2754. (defun org-force-cycle-archived ()
  2755. "Cycle subtree even if it is archived."
  2756. (interactive)
  2757. (setq this-command 'org-cycle)
  2758. (let ((org-cycle-open-archived-trees t))
  2759. (call-interactively 'org-cycle)))
  2760. (defun org-hide-archived-subtrees (beg end)
  2761. "Re-hide all archived subtrees after a visibility state change."
  2762. (save-excursion
  2763. (let* ((re (concat ":" org-archive-tag ":")))
  2764. (goto-char beg)
  2765. (while (re-search-forward re end t)
  2766. (and (org-on-heading-p) (hide-subtree))
  2767. (org-end-of-subtree t)))))
  2768. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2769. (eval-and-compile
  2770. (org-autoload "org-archive"
  2771. '(org-add-archive-files org-archive-subtree
  2772. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2773. ;; Autoload Column View Code
  2774. (declare-function org-columns-number-to-string "org-colview")
  2775. (declare-function org-columns-get-format-and-top-level "org-colview")
  2776. (declare-function org-columns-compute "org-colview")
  2777. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2778. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2779. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2780. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2781. ;; Autoload ID code
  2782. (declare-function org-id-store-link "org-id")
  2783. (declare-function org-id-locations-load "org-id")
  2784. (declare-function org-id-locations-save "org-id")
  2785. (defvar org-id-track-globally)
  2786. (org-autoload "org-id"
  2787. '(org-id-get-create org-id-new org-id-copy org-id-get
  2788. org-id-get-with-outline-path-completion
  2789. org-id-get-with-outline-drilling
  2790. org-id-goto org-id-find org-id-store-link))
  2791. ;; Autoload Plotting Code
  2792. (org-autoload "org-plot"
  2793. '(org-plot/gnuplot))
  2794. ;;; Variables for pre-computed regular expressions, all buffer local
  2795. (defvar org-drawer-regexp nil
  2796. "Matches first line of a hidden block.")
  2797. (make-variable-buffer-local 'org-drawer-regexp)
  2798. (defvar org-todo-regexp nil
  2799. "Matches any of the TODO state keywords.")
  2800. (make-variable-buffer-local 'org-todo-regexp)
  2801. (defvar org-not-done-regexp nil
  2802. "Matches any of the TODO state keywords except the last one.")
  2803. (make-variable-buffer-local 'org-not-done-regexp)
  2804. (defvar org-not-done-heading-regexp nil
  2805. "Matches a TODO headline that is not done.")
  2806. (make-variable-buffer-local 'org-not-done-regexp)
  2807. (defvar org-todo-line-regexp nil
  2808. "Matches a headline and puts TODO state into group 2 if present.")
  2809. (make-variable-buffer-local 'org-todo-line-regexp)
  2810. (defvar org-complex-heading-regexp nil
  2811. "Matches a headline and puts everything into groups:
  2812. group 1: the stars
  2813. group 2: The todo keyword, maybe
  2814. group 3: Priority cookie
  2815. group 4: True headline
  2816. group 5: Tags")
  2817. (make-variable-buffer-local 'org-complex-heading-regexp)
  2818. (defvar org-todo-line-tags-regexp nil
  2819. "Matches a headline and puts TODO state into group 2 if present.
  2820. Also put tags into group 4 if tags are present.")
  2821. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2822. (defvar org-nl-done-regexp nil
  2823. "Matches newline followed by a headline with the DONE keyword.")
  2824. (make-variable-buffer-local 'org-nl-done-regexp)
  2825. (defvar org-looking-at-done-regexp nil
  2826. "Matches the DONE keyword a point.")
  2827. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2828. (defvar org-ds-keyword-length 12
  2829. "Maximum length of the Deadline and SCHEDULED keywords.")
  2830. (make-variable-buffer-local 'org-ds-keyword-length)
  2831. (defvar org-deadline-regexp nil
  2832. "Matches the DEADLINE keyword.")
  2833. (make-variable-buffer-local 'org-deadline-regexp)
  2834. (defvar org-deadline-time-regexp nil
  2835. "Matches the DEADLINE keyword together with a time stamp.")
  2836. (make-variable-buffer-local 'org-deadline-time-regexp)
  2837. (defvar org-deadline-line-regexp nil
  2838. "Matches the DEADLINE keyword and the rest of the line.")
  2839. (make-variable-buffer-local 'org-deadline-line-regexp)
  2840. (defvar org-scheduled-regexp nil
  2841. "Matches the SCHEDULED keyword.")
  2842. (make-variable-buffer-local 'org-scheduled-regexp)
  2843. (defvar org-scheduled-time-regexp nil
  2844. "Matches the SCHEDULED keyword together with a time stamp.")
  2845. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2846. (defvar org-closed-time-regexp nil
  2847. "Matches the CLOSED keyword together with a time stamp.")
  2848. (make-variable-buffer-local 'org-closed-time-regexp)
  2849. (defvar org-keyword-time-regexp nil
  2850. "Matches any of the 4 keywords, together with the time stamp.")
  2851. (make-variable-buffer-local 'org-keyword-time-regexp)
  2852. (defvar org-keyword-time-not-clock-regexp nil
  2853. "Matches any of the 3 keywords, together with the time stamp.")
  2854. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2855. (defvar org-maybe-keyword-time-regexp nil
  2856. "Matches a timestamp, possibly preceeded by a keyword.")
  2857. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2858. (defvar org-planning-or-clock-line-re nil
  2859. "Matches a line with planning or clock info.")
  2860. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2861. (defconst org-plain-time-of-day-regexp
  2862. (concat
  2863. "\\(\\<[012]?[0-9]"
  2864. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2865. "\\(--?"
  2866. "\\(\\<[012]?[0-9]"
  2867. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2868. "\\)?")
  2869. "Regular expression to match a plain time or time range.
  2870. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2871. groups carry important information:
  2872. 0 the full match
  2873. 1 the first time, range or not
  2874. 8 the second time, if it is a range.")
  2875. (defconst org-plain-time-extension-regexp
  2876. (concat
  2877. "\\(\\<[012]?[0-9]"
  2878. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2879. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2880. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2881. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2882. groups carry important information:
  2883. 0 the full match
  2884. 7 hours of duration
  2885. 9 minutes of duration")
  2886. (defconst org-stamp-time-of-day-regexp
  2887. (concat
  2888. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2889. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2890. "\\(--?"
  2891. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2892. "Regular expression to match a timestamp time or time range.
  2893. After a match, the following groups carry important information:
  2894. 0 the full match
  2895. 1 date plus weekday, for backreferencing to make sure both times on same day
  2896. 2 the first time, range or not
  2897. 4 the second time, if it is a range.")
  2898. (defconst org-startup-options
  2899. '(("fold" org-startup-folded t)
  2900. ("overview" org-startup-folded t)
  2901. ("nofold" org-startup-folded nil)
  2902. ("showall" org-startup-folded nil)
  2903. ("content" org-startup-folded content)
  2904. ("hidestars" org-hide-leading-stars t)
  2905. ("showstars" org-hide-leading-stars nil)
  2906. ("odd" org-odd-levels-only t)
  2907. ("oddeven" org-odd-levels-only nil)
  2908. ("align" org-startup-align-all-tables t)
  2909. ("noalign" org-startup-align-all-tables nil)
  2910. ("customtime" org-display-custom-times t)
  2911. ("logdone" org-log-done time)
  2912. ("lognotedone" org-log-done note)
  2913. ("nologdone" org-log-done nil)
  2914. ("lognoteclock-out" org-log-note-clock-out t)
  2915. ("nolognoteclock-out" org-log-note-clock-out nil)
  2916. ("logrepeat" org-log-repeat state)
  2917. ("lognoterepeat" org-log-repeat note)
  2918. ("nologrepeat" org-log-repeat nil)
  2919. ("fninline" org-footnote-define-inline t)
  2920. ("nofninline" org-footnote-define-inline nil)
  2921. ("fnlocal" org-footnote-section nil)
  2922. ("fnauto" org-footnote-auto-label t)
  2923. ("fnprompt" org-footnote-auto-label nil)
  2924. ("fnconfirm" org-footnote-auto-label confirm)
  2925. ("fnplain" org-footnote-auto-label plain)
  2926. ("constcgs" constants-unit-system cgs)
  2927. ("constSI" constants-unit-system SI)
  2928. ("noptag" org-tag-persistent-alist nil))
  2929. "Variable associated with STARTUP options for org-mode.
  2930. Each element is a list of three items: The startup options as written
  2931. in the #+STARTUP line, the corresponding variable, and the value to
  2932. set this variable to if the option is found. An optional forth element PUSH
  2933. means to push this value onto the list in the variable.")
  2934. (defun org-set-regexps-and-options ()
  2935. "Precompute regular expressions for current buffer."
  2936. (when (org-mode-p)
  2937. (org-set-local 'org-todo-kwd-alist nil)
  2938. (org-set-local 'org-todo-key-alist nil)
  2939. (org-set-local 'org-todo-key-trigger nil)
  2940. (org-set-local 'org-todo-keywords-1 nil)
  2941. (org-set-local 'org-done-keywords nil)
  2942. (org-set-local 'org-todo-heads nil)
  2943. (org-set-local 'org-todo-sets nil)
  2944. (org-set-local 'org-todo-log-states nil)
  2945. (org-set-local 'org-file-properties nil)
  2946. (org-set-local 'org-file-tags nil)
  2947. (let ((re (org-make-options-regexp
  2948. '("CATEGORY" "TODO" "COLUMNS"
  2949. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2950. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2951. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2952. (splitre "[ \t]+")
  2953. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2954. tail sep kws1 prio props ftags drawers
  2955. ext-setup-or-nil setup-contents (start 0))
  2956. (save-excursion
  2957. (save-restriction
  2958. (widen)
  2959. (goto-char (point-min))
  2960. (while (or (and ext-setup-or-nil
  2961. (string-match re ext-setup-or-nil start)
  2962. (setq start (match-end 0)))
  2963. (and (setq ext-setup-or-nil nil start 0)
  2964. (re-search-forward re nil t)))
  2965. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2966. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2967. (cond
  2968. ((equal key "CATEGORY")
  2969. (if (string-match "[ \t]+$" value)
  2970. (setq value (replace-match "" t t value)))
  2971. (setq cat value))
  2972. ((member key '("SEQ_TODO" "TODO"))
  2973. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2974. ((equal key "TYP_TODO")
  2975. (push (cons 'type (org-split-string value splitre)) kwds))
  2976. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2977. ;; general TODO-like setup
  2978. (push (cons (intern (downcase (match-string 1 key)))
  2979. (org-split-string value splitre)) kwds))
  2980. ((equal key "TAGS")
  2981. (setq tags (append tags (if tags '("\\n") nil)
  2982. (org-split-string value splitre))))
  2983. ((equal key "COLUMNS")
  2984. (org-set-local 'org-columns-default-format value))
  2985. ((equal key "LINK")
  2986. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2987. (push (cons (match-string 1 value)
  2988. (org-trim (match-string 2 value)))
  2989. links)))
  2990. ((equal key "PRIORITIES")
  2991. (setq prio (org-split-string value " +")))
  2992. ((equal key "PROPERTY")
  2993. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2994. (push (cons (match-string 1 value) (match-string 2 value))
  2995. props)))
  2996. ((equal key "FILETAGS")
  2997. (when (string-match "\\S-" value)
  2998. (setq ftags
  2999. (append
  3000. ftags
  3001. (apply 'append
  3002. (mapcar (lambda (x) (org-split-string x ":"))
  3003. (org-split-string value)))))))
  3004. ((equal key "DRAWERS")
  3005. (setq drawers (org-split-string value splitre)))
  3006. ((equal key "CONSTANTS")
  3007. (setq const (append const (org-split-string value splitre))))
  3008. ((equal key "STARTUP")
  3009. (let ((opts (org-split-string value splitre))
  3010. l var val)
  3011. (while (setq l (pop opts))
  3012. (when (setq l (assoc l org-startup-options))
  3013. (setq var (nth 1 l) val (nth 2 l))
  3014. (if (not (nth 3 l))
  3015. (set (make-local-variable var) val)
  3016. (if (not (listp (symbol-value var)))
  3017. (set (make-local-variable var) nil))
  3018. (set (make-local-variable var) (symbol-value var))
  3019. (add-to-list var val))))))
  3020. ((equal key "ARCHIVE")
  3021. (string-match " *$" value)
  3022. (setq arch (replace-match "" t t value))
  3023. (remove-text-properties 0 (length arch)
  3024. '(face t fontified t) arch))
  3025. ((equal key "SETUPFILE")
  3026. (setq setup-contents (org-file-contents
  3027. (expand-file-name
  3028. (org-remove-double-quotes value))
  3029. 'noerror))
  3030. (if (not ext-setup-or-nil)
  3031. (setq ext-setup-or-nil setup-contents start 0)
  3032. (setq ext-setup-or-nil
  3033. (concat (substring ext-setup-or-nil 0 start)
  3034. "\n" setup-contents "\n"
  3035. (substring ext-setup-or-nil start)))))
  3036. ))))
  3037. (when cat
  3038. (org-set-local 'org-category (intern cat))
  3039. (push (cons "CATEGORY" cat) props))
  3040. (when prio
  3041. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3042. (setq prio (mapcar 'string-to-char prio))
  3043. (org-set-local 'org-highest-priority (nth 0 prio))
  3044. (org-set-local 'org-lowest-priority (nth 1 prio))
  3045. (org-set-local 'org-default-priority (nth 2 prio)))
  3046. (and props (org-set-local 'org-file-properties (nreverse props)))
  3047. (and ftags (org-set-local 'org-file-tags
  3048. (mapcar 'org-add-prop-inherited ftags)))
  3049. (and drawers (org-set-local 'org-drawers drawers))
  3050. (and arch (org-set-local 'org-archive-location arch))
  3051. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3052. ;; Process the TODO keywords
  3053. (unless kwds
  3054. ;; Use the global values as if they had been given locally.
  3055. (setq kwds (default-value 'org-todo-keywords))
  3056. (if (stringp (car kwds))
  3057. (setq kwds (list (cons org-todo-interpretation
  3058. (default-value 'org-todo-keywords)))))
  3059. (setq kwds (reverse kwds)))
  3060. (setq kwds (nreverse kwds))
  3061. (let (inter kws kw)
  3062. (while (setq kws (pop kwds))
  3063. (let ((kws (or
  3064. (run-hook-with-args-until-success
  3065. 'org-todo-setup-filter-hook kws)
  3066. kws)))
  3067. (setq inter (pop kws) sep (member "|" kws)
  3068. kws0 (delete "|" (copy-sequence kws))
  3069. kwsa nil
  3070. kws1 (mapcar
  3071. (lambda (x)
  3072. ;; 1 2
  3073. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3074. (progn
  3075. (setq kw (match-string 1 x)
  3076. key (and (match-end 2) (match-string 2 x))
  3077. log (org-extract-log-state-settings x))
  3078. (push (cons kw (and key (string-to-char key))) kwsa)
  3079. (and log (push log org-todo-log-states))
  3080. kw)
  3081. (error "Invalid TODO keyword %s" x)))
  3082. kws0)
  3083. kwsa (if kwsa (append '((:startgroup))
  3084. (nreverse kwsa)
  3085. '((:endgroup))))
  3086. hw (car kws1)
  3087. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3088. tail (list inter hw (car dws) (org-last dws))))
  3089. (add-to-list 'org-todo-heads hw 'append)
  3090. (push kws1 org-todo-sets)
  3091. (setq org-done-keywords (append org-done-keywords dws nil))
  3092. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3093. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3094. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3095. (setq org-todo-sets (nreverse org-todo-sets)
  3096. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3097. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3098. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3099. ;; Process the constants
  3100. (when const
  3101. (let (e cst)
  3102. (while (setq e (pop const))
  3103. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3104. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3105. (setq org-table-formula-constants-local cst)))
  3106. ;; Process the tags.
  3107. (when tags
  3108. (let (e tgs)
  3109. (while (setq e (pop tags))
  3110. (cond
  3111. ((equal e "{") (push '(:startgroup) tgs))
  3112. ((equal e "}") (push '(:endgroup) tgs))
  3113. ((equal e "\\n") (push '(:newline) tgs))
  3114. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3115. (push (cons (match-string 1 e)
  3116. (string-to-char (match-string 2 e)))
  3117. tgs))
  3118. (t (push (list e) tgs))))
  3119. (org-set-local 'org-tag-alist nil)
  3120. (while (setq e (pop tgs))
  3121. (or (and (stringp (car e))
  3122. (assoc (car e) org-tag-alist))
  3123. (push e org-tag-alist)))))
  3124. ;; Compute the regular expressions and other local variables
  3125. (if (not org-done-keywords)
  3126. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3127. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3128. (length org-scheduled-string)
  3129. (length org-clock-string)
  3130. (length org-closed-string)))
  3131. org-drawer-regexp
  3132. (concat "^[ \t]*:\\("
  3133. (mapconcat 'regexp-quote org-drawers "\\|")
  3134. "\\):[ \t]*$")
  3135. org-not-done-keywords
  3136. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3137. org-todo-regexp
  3138. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3139. "\\|") "\\)\\>")
  3140. org-not-done-regexp
  3141. (concat "\\<\\("
  3142. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3143. "\\)\\>")
  3144. org-not-done-heading-regexp
  3145. (concat "^\\(\\*+\\)[ \t]+\\("
  3146. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3147. "\\)\\>")
  3148. org-todo-line-regexp
  3149. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3150. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3151. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3152. org-complex-heading-regexp
  3153. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3154. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3155. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3156. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3157. org-nl-done-regexp
  3158. (concat "\n\\*+[ \t]+"
  3159. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3160. "\\)" "\\>")
  3161. org-todo-line-tags-regexp
  3162. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3163. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3164. (org-re
  3165. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3166. org-looking-at-done-regexp
  3167. (concat "^" "\\(?:"
  3168. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3169. "\\>")
  3170. org-deadline-regexp (concat "\\<" org-deadline-string)
  3171. org-deadline-time-regexp
  3172. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3173. org-deadline-line-regexp
  3174. (concat "\\<\\(" org-deadline-string "\\).*")
  3175. org-scheduled-regexp
  3176. (concat "\\<" org-scheduled-string)
  3177. org-scheduled-time-regexp
  3178. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3179. org-closed-time-regexp
  3180. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3181. org-keyword-time-regexp
  3182. (concat "\\<\\(" org-scheduled-string
  3183. "\\|" org-deadline-string
  3184. "\\|" org-closed-string
  3185. "\\|" org-clock-string "\\)"
  3186. " *[[<]\\([^]>]+\\)[]>]")
  3187. org-keyword-time-not-clock-regexp
  3188. (concat "\\<\\(" org-scheduled-string
  3189. "\\|" org-deadline-string
  3190. "\\|" org-closed-string
  3191. "\\)"
  3192. " *[[<]\\([^]>]+\\)[]>]")
  3193. org-maybe-keyword-time-regexp
  3194. (concat "\\(\\<\\(" org-scheduled-string
  3195. "\\|" org-deadline-string
  3196. "\\|" org-closed-string
  3197. "\\|" org-clock-string "\\)\\)?"
  3198. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3199. org-planning-or-clock-line-re
  3200. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3201. "\\|" org-deadline-string
  3202. "\\|" org-closed-string "\\|" org-clock-string
  3203. "\\)\\>\\)")
  3204. )
  3205. (org-compute-latex-and-specials-regexp)
  3206. (org-set-font-lock-defaults))))
  3207. (defun org-file-contents (file &optional noerror)
  3208. "Return the contents of FILE, as a string."
  3209. (if (or (not file)
  3210. (not (file-readable-p file)))
  3211. (if noerror
  3212. (progn
  3213. (message "Cannot read file %s" file)
  3214. (ding) (sit-for 2)
  3215. "")
  3216. (error "Cannot read file %s" file))
  3217. (with-temp-buffer
  3218. (insert-file-contents file)
  3219. (buffer-string))))
  3220. (defun org-extract-log-state-settings (x)
  3221. "Extract the log state setting from a TODO keyword string.
  3222. This will extract info from a string like \"WAIT(w@/!)\"."
  3223. (let (kw key log1 log2)
  3224. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3225. (setq kw (match-string 1 x)
  3226. key (and (match-end 2) (match-string 2 x))
  3227. log1 (and (match-end 3) (match-string 3 x))
  3228. log2 (and (match-end 4) (match-string 4 x)))
  3229. (and (or log1 log2)
  3230. (list kw
  3231. (and log1 (if (equal log1 "!") 'time 'note))
  3232. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3233. (defun org-remove-keyword-keys (list)
  3234. "Remove a pair of parenthesis at the end of each string in LIST."
  3235. (mapcar (lambda (x)
  3236. (if (string-match "(.*)$" x)
  3237. (substring x 0 (match-beginning 0))
  3238. x))
  3239. list))
  3240. ;; FIXME: this could be done much better, using second characters etc.
  3241. (defun org-assign-fast-keys (alist)
  3242. "Assign fast keys to a keyword-key alist.
  3243. Respect keys that are already there."
  3244. (let (new e k c c1 c2 (char ?a))
  3245. (while (setq e (pop alist))
  3246. (cond
  3247. ((equal e '(:startgroup)) (push e new))
  3248. ((equal e '(:endgroup)) (push e new))
  3249. ((equal e '(:newline)) (push e new))
  3250. (t
  3251. (setq k (car e) c2 nil)
  3252. (if (cdr e)
  3253. (setq c (cdr e))
  3254. ;; automatically assign a character.
  3255. (setq c1 (string-to-char
  3256. (downcase (substring
  3257. k (if (= (string-to-char k) ?@) 1 0)))))
  3258. (if (or (rassoc c1 new) (rassoc c1 alist))
  3259. (while (or (rassoc char new) (rassoc char alist))
  3260. (setq char (1+ char)))
  3261. (setq c2 c1))
  3262. (setq c (or c2 char)))
  3263. (push (cons k c) new))))
  3264. (nreverse new)))
  3265. ;;; Some variables used in various places
  3266. (defvar org-window-configuration nil
  3267. "Used in various places to store a window configuration.")
  3268. (defvar org-finish-function nil
  3269. "Function to be called when `C-c C-c' is used.
  3270. This is for getting out of special buffers like remember.")
  3271. ;; FIXME: Occasionally check by commenting these, to make sure
  3272. ;; no other functions uses these, forgetting to let-bind them.
  3273. (defvar entry)
  3274. (defvar state)
  3275. (defvar last-state)
  3276. (defvar date)
  3277. (defvar description)
  3278. ;; Defined somewhere in this file, but used before definition.
  3279. (defvar org-html-entities)
  3280. (defvar org-struct-menu)
  3281. (defvar org-org-menu)
  3282. (defvar org-tbl-menu)
  3283. (defvar org-agenda-keymap)
  3284. ;;;; Define the Org-mode
  3285. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3286. (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."))
  3287. ;; We use a before-change function to check if a table might need
  3288. ;; an update.
  3289. (defvar org-table-may-need-update t
  3290. "Indicates that a table might need an update.
  3291. This variable is set by `org-before-change-function'.
  3292. `org-table-align' sets it back to nil.")
  3293. (defun org-before-change-function (beg end)
  3294. "Every change indicates that a table might need an update."
  3295. (setq org-table-may-need-update t))
  3296. (defvar org-mode-map)
  3297. (defvar org-mode-hook nil
  3298. "Mode hook for Org-mode, run after the mode was turned on.")
  3299. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3300. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3301. (defvar org-table-buffer-is-an nil)
  3302. (defconst org-outline-regexp "\\*+ ")
  3303. ;;;###autoload
  3304. (define-derived-mode org-mode outline-mode "Org"
  3305. "Outline-based notes management and organizer, alias
  3306. \"Carsten's outline-mode for keeping track of everything.\"
  3307. Org-mode develops organizational tasks around a NOTES file which
  3308. contains information about projects as plain text. Org-mode is
  3309. implemented on top of outline-mode, which is ideal to keep the content
  3310. of large files well structured. It supports ToDo items, deadlines and
  3311. time stamps, which magically appear in the diary listing of the Emacs
  3312. calendar. Tables are easily created with a built-in table editor.
  3313. Plain text URL-like links connect to websites, emails (VM), Usenet
  3314. messages (Gnus), BBDB entries, and any files related to the project.
  3315. For printing and sharing of notes, an Org-mode file (or a part of it)
  3316. can be exported as a structured ASCII or HTML file.
  3317. The following commands are available:
  3318. \\{org-mode-map}"
  3319. ;; Get rid of Outline menus, they are not needed
  3320. ;; Need to do this here because define-derived-mode sets up
  3321. ;; the keymap so late. Still, it is a waste to call this each time
  3322. ;; we switch another buffer into org-mode.
  3323. (if (featurep 'xemacs)
  3324. (when (boundp 'outline-mode-menu-heading)
  3325. ;; Assume this is Greg's port, it used easymenu
  3326. (easy-menu-remove outline-mode-menu-heading)
  3327. (easy-menu-remove outline-mode-menu-show)
  3328. (easy-menu-remove outline-mode-menu-hide))
  3329. (define-key org-mode-map [menu-bar headings] 'undefined)
  3330. (define-key org-mode-map [menu-bar hide] 'undefined)
  3331. (define-key org-mode-map [menu-bar show] 'undefined))
  3332. (org-load-modules-maybe)
  3333. (easy-menu-add org-org-menu)
  3334. (easy-menu-add org-tbl-menu)
  3335. (org-install-agenda-files-menu)
  3336. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3337. (org-add-to-invisibility-spec '(org-cwidth))
  3338. (when (featurep 'xemacs)
  3339. (org-set-local 'line-move-ignore-invisible t))
  3340. (org-set-local 'outline-regexp org-outline-regexp)
  3341. (org-set-local 'outline-level 'org-outline-level)
  3342. (when (and org-ellipsis
  3343. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3344. (fboundp 'make-glyph-code))
  3345. (unless org-display-table
  3346. (setq org-display-table (make-display-table)))
  3347. (set-display-table-slot
  3348. org-display-table 4
  3349. (vconcat (mapcar
  3350. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3351. org-ellipsis)))
  3352. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3353. (setq buffer-display-table org-display-table))
  3354. (org-set-regexps-and-options)
  3355. (when (and org-tag-faces (not org-tags-special-faces-re))
  3356. ;; tag faces set outside customize.... force initialization.
  3357. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3358. ;; Calc embedded
  3359. (org-set-local 'calc-embedded-open-mode "# ")
  3360. (modify-syntax-entry ?# "<")
  3361. (modify-syntax-entry ?@ "w")
  3362. (if org-startup-truncated (setq truncate-lines t))
  3363. (org-set-local 'font-lock-unfontify-region-function
  3364. 'org-unfontify-region)
  3365. ;; Activate before-change-function
  3366. (org-set-local 'org-table-may-need-update t)
  3367. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3368. 'local)
  3369. ;; Check for running clock before killing a buffer
  3370. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3371. ;; Paragraphs and auto-filling
  3372. (org-set-autofill-regexps)
  3373. (setq indent-line-function 'org-indent-line-function)
  3374. (org-update-radio-target-regexp)
  3375. ;; Make sure dependence stuff works reliably, even for users who set it
  3376. ;; too late :-(
  3377. (if org-enforce-todo-dependencies
  3378. (add-hook 'org-blocker-hook
  3379. 'org-block-todo-from-children-or-siblings-or-parent)
  3380. (remove-hook 'org-blocker-hook
  3381. 'org-block-todo-from-children-or-siblings-or-parent))
  3382. (if org-enforce-todo-checkbox-dependencies
  3383. (add-hook 'org-blocker-hook
  3384. 'org-block-todo-from-checkboxes)
  3385. (remove-hook 'org-blocker-hook
  3386. 'org-block-todo-from-checkboxes))
  3387. ;; Comment characters
  3388. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3389. (org-set-local 'comment-padding " ")
  3390. ;; Align options lines
  3391. (org-set-local
  3392. 'align-mode-rules-list
  3393. '((org-in-buffer-settings
  3394. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3395. (modes . '(org-mode)))))
  3396. ;; Imenu
  3397. (org-set-local 'imenu-create-index-function
  3398. 'org-imenu-get-tree)
  3399. ;; Make isearch reveal context
  3400. (if (or (featurep 'xemacs)
  3401. (not (boundp 'outline-isearch-open-invisible-function)))
  3402. ;; Emacs 21 and XEmacs make use of the hook
  3403. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3404. ;; Emacs 22 deals with this through a special variable
  3405. (org-set-local 'outline-isearch-open-invisible-function
  3406. (lambda (&rest ignore) (org-show-context 'isearch))))
  3407. ;; If empty file that did not turn on org-mode automatically, make it to.
  3408. (if (and org-insert-mode-line-in-empty-file
  3409. (interactive-p)
  3410. (= (point-min) (point-max)))
  3411. (insert "# -*- mode: org -*-\n\n"))
  3412. (unless org-inhibit-startup
  3413. (when org-startup-align-all-tables
  3414. (let ((bmp (buffer-modified-p)))
  3415. (org-table-map-tables 'org-table-align)
  3416. (set-buffer-modified-p bmp)))
  3417. (org-set-startup-visibility)))
  3418. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3419. (defun org-current-time ()
  3420. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3421. (if (> (car org-time-stamp-rounding-minutes) 1)
  3422. (let ((r (car org-time-stamp-rounding-minutes))
  3423. (time (decode-time)))
  3424. (apply 'encode-time
  3425. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3426. (nthcdr 2 time))))
  3427. (current-time)))
  3428. ;;;; Font-Lock stuff, including the activators
  3429. (defvar org-mouse-map (make-sparse-keymap))
  3430. (org-defkey org-mouse-map
  3431. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3432. (org-defkey org-mouse-map
  3433. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3434. (when org-mouse-1-follows-link
  3435. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3436. (when org-tab-follows-link
  3437. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3438. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3439. (require 'font-lock)
  3440. (defconst org-non-link-chars "]\t\n\r<>")
  3441. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3442. "shell" "elisp"))
  3443. (defvar org-link-types-re nil
  3444. "Matches a link that has a url-like prefix like \"http:\"")
  3445. (defvar org-link-re-with-space nil
  3446. "Matches a link with spaces, optional angular brackets around it.")
  3447. (defvar org-link-re-with-space2 nil
  3448. "Matches a link with spaces, optional angular brackets around it.")
  3449. (defvar org-link-re-with-space3 nil
  3450. "Matches a link with spaces, only for internal part in bracket links.")
  3451. (defvar org-angle-link-re nil
  3452. "Matches link with angular brackets, spaces are allowed.")
  3453. (defvar org-plain-link-re nil
  3454. "Matches plain link, without spaces.")
  3455. (defvar org-bracket-link-regexp nil
  3456. "Matches a link in double brackets.")
  3457. (defvar org-bracket-link-analytic-regexp nil
  3458. "Regular expression used to analyze links.
  3459. Here is what the match groups contain after a match:
  3460. 1: http:
  3461. 2: http
  3462. 3: path
  3463. 4: [desc]
  3464. 5: desc")
  3465. (defvar org-bracket-link-analytic-regexp++ nil
  3466. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3467. (defvar org-any-link-re nil
  3468. "Regular expression matching any link.")
  3469. (defun org-make-link-regexps ()
  3470. "Update the link regular expressions.
  3471. This should be called after the variable `org-link-types' has changed."
  3472. (setq org-link-types-re
  3473. (concat
  3474. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3475. org-link-re-with-space
  3476. (concat
  3477. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3478. "\\([^" org-non-link-chars " ]"
  3479. "[^" org-non-link-chars "]*"
  3480. "[^" org-non-link-chars " ]\\)>?")
  3481. org-link-re-with-space2
  3482. (concat
  3483. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3484. "\\([^" org-non-link-chars " ]"
  3485. "[^\t\n\r]*"
  3486. "[^" org-non-link-chars " ]\\)>?")
  3487. org-link-re-with-space3
  3488. (concat
  3489. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3490. "\\([^" org-non-link-chars " ]"
  3491. "[^\t\n\r]*\\)")
  3492. org-angle-link-re
  3493. (concat
  3494. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3495. "\\([^" org-non-link-chars " ]"
  3496. "[^" org-non-link-chars "]*"
  3497. "\\)>")
  3498. org-plain-link-re
  3499. (concat
  3500. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3501. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3502. org-bracket-link-regexp
  3503. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3504. org-bracket-link-analytic-regexp
  3505. (concat
  3506. "\\[\\["
  3507. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3508. "\\([^]]+\\)"
  3509. "\\]"
  3510. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3511. "\\]")
  3512. org-bracket-link-analytic-regexp++
  3513. (concat
  3514. "\\[\\["
  3515. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3516. "\\([^]]+\\)"
  3517. "\\]"
  3518. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3519. "\\]")
  3520. org-any-link-re
  3521. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3522. org-angle-link-re "\\)\\|\\("
  3523. org-plain-link-re "\\)")))
  3524. (org-make-link-regexps)
  3525. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3526. "Regular expression for fast time stamp matching.")
  3527. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3528. "Regular expression for fast time stamp matching.")
  3529. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3530. "Regular expression matching time strings for analysis.
  3531. This one does not require the space after the date, so it can be used
  3532. on a string that terminates immediately after the date.")
  3533. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3534. "Regular expression matching time strings for analysis.")
  3535. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3536. "Regular expression matching time stamps, with groups.")
  3537. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3538. "Regular expression matching time stamps (also [..]), with groups.")
  3539. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3540. "Regular expression matching a time stamp range.")
  3541. (defconst org-tr-regexp-both
  3542. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3543. "Regular expression matching a time stamp range.")
  3544. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3545. org-ts-regexp "\\)?")
  3546. "Regular expression matching a time stamp or time stamp range.")
  3547. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3548. org-ts-regexp-both "\\)?")
  3549. "Regular expression matching a time stamp or time stamp range.
  3550. The time stamps may be either active or inactive.")
  3551. (defvar org-emph-face nil)
  3552. (defun org-do-emphasis-faces (limit)
  3553. "Run through the buffer and add overlays to links."
  3554. (let (rtn)
  3555. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3556. (if (not (= (char-after (match-beginning 3))
  3557. (char-after (match-beginning 4))))
  3558. (progn
  3559. (setq rtn t)
  3560. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3561. 'face
  3562. (nth 1 (assoc (match-string 3)
  3563. org-emphasis-alist)))
  3564. (add-text-properties (match-beginning 2) (match-end 2)
  3565. '(font-lock-multiline t))
  3566. (when org-hide-emphasis-markers
  3567. (add-text-properties (match-end 4) (match-beginning 5)
  3568. '(invisible org-link))
  3569. (add-text-properties (match-beginning 3) (match-end 3)
  3570. '(invisible org-link)))))
  3571. (backward-char 1))
  3572. rtn))
  3573. (defun org-emphasize (&optional char)
  3574. "Insert or change an emphasis, i.e. a font like bold or italic.
  3575. If there is an active region, change that region to a new emphasis.
  3576. If there is no region, just insert the marker characters and position
  3577. the cursor between them.
  3578. CHAR should be either the marker character, or the first character of the
  3579. HTML tag associated with that emphasis. If CHAR is a space, the means
  3580. to remove the emphasis of the selected region.
  3581. If char is not given (for example in an interactive call) it
  3582. will be prompted for."
  3583. (interactive)
  3584. (let ((eal org-emphasis-alist) e det
  3585. (erc org-emphasis-regexp-components)
  3586. (prompt "")
  3587. (string "") beg end move tag c s)
  3588. (if (org-region-active-p)
  3589. (setq beg (region-beginning) end (region-end)
  3590. string (buffer-substring beg end))
  3591. (setq move t))
  3592. (while (setq e (pop eal))
  3593. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3594. c (aref tag 0))
  3595. (push (cons c (string-to-char (car e))) det)
  3596. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3597. (substring tag 1)))))
  3598. (setq det (nreverse det))
  3599. (unless char
  3600. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3601. (setq char (read-char-exclusive)))
  3602. (setq char (or (cdr (assoc char det)) char))
  3603. (if (equal char ?\ )
  3604. (setq s "" move nil)
  3605. (unless (assoc (char-to-string char) org-emphasis-alist)
  3606. (error "No such emphasis marker: \"%c\"" char))
  3607. (setq s (char-to-string char)))
  3608. (while (and (> (length string) 1)
  3609. (equal (substring string 0 1) (substring string -1))
  3610. (assoc (substring string 0 1) org-emphasis-alist))
  3611. (setq string (substring string 1 -1)))
  3612. (setq string (concat s string s))
  3613. (if beg (delete-region beg end))
  3614. (unless (or (bolp)
  3615. (string-match (concat "[" (nth 0 erc) "\n]")
  3616. (char-to-string (char-before (point)))))
  3617. (insert " "))
  3618. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3619. (char-to-string (char-after (point))))
  3620. (insert " ") (backward-char 1))
  3621. (insert string)
  3622. (and move (backward-char 1))))
  3623. (defconst org-nonsticky-props
  3624. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3625. (defun org-activate-plain-links (limit)
  3626. "Run through the buffer and add overlays to links."
  3627. (catch 'exit
  3628. (let (f)
  3629. (while (re-search-forward org-plain-link-re limit t)
  3630. (setq f (get-text-property (match-beginning 0) 'face))
  3631. (if (or (eq f 'org-tag)
  3632. (and (listp f) (memq 'org-tag f)))
  3633. nil
  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. (throw 'exit t))))))
  3640. (defun org-activate-code (limit)
  3641. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3642. (progn
  3643. (remove-text-properties (match-beginning 0) (match-end 0)
  3644. '(display t invisible t intangible t))
  3645. t)))
  3646. (defun org-activate-angle-links (limit)
  3647. "Run through the buffer and add overlays to links."
  3648. (if (re-search-forward org-angle-link-re limit t)
  3649. (progn
  3650. (add-text-properties (match-beginning 0) (match-end 0)
  3651. (list 'mouse-face 'highlight
  3652. 'rear-nonsticky org-nonsticky-props
  3653. 'keymap org-mouse-map
  3654. ))
  3655. t)))
  3656. (defun org-activate-footnote-links (limit)
  3657. "Run through the buffer and add overlays to links."
  3658. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3659. limit t)
  3660. (progn
  3661. (add-text-properties (match-beginning 2) (match-end 2)
  3662. (list 'mouse-face 'highlight
  3663. 'rear-nonsticky org-nonsticky-props
  3664. 'keymap org-mouse-map
  3665. 'help-echo
  3666. (if (= (point-at-bol) (match-beginning 2))
  3667. "Footnote definition"
  3668. "Footnote reference")
  3669. ))
  3670. t)))
  3671. (defun org-activate-bracket-links (limit)
  3672. "Run through the buffer and add overlays to bracketed links."
  3673. (if (re-search-forward org-bracket-link-regexp limit t)
  3674. (let* ((help (concat "LINK: "
  3675. (org-match-string-no-properties 1)))
  3676. ;; FIXME: above we should remove the escapes.
  3677. ;; but that requires another match, protecting match data,
  3678. ;; a lot of overhead for font-lock.
  3679. (ip (org-maybe-intangible
  3680. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3681. 'keymap org-mouse-map 'mouse-face 'highlight
  3682. 'font-lock-multiline t 'help-echo help)))
  3683. (vp (list 'rear-nonsticky org-nonsticky-props
  3684. 'keymap org-mouse-map 'mouse-face 'highlight
  3685. 'font-lock-multiline t 'help-echo help)))
  3686. ;; We need to remove the invisible property here. Table narrowing
  3687. ;; may have made some of this invisible.
  3688. (remove-text-properties (match-beginning 0) (match-end 0)
  3689. '(invisible nil))
  3690. (if (match-end 3)
  3691. (progn
  3692. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3693. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3694. (add-text-properties (match-end 3) (match-end 0) ip))
  3695. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3696. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3697. (add-text-properties (match-end 1) (match-end 0) ip))
  3698. t)))
  3699. (defun org-activate-dates (limit)
  3700. "Run through the buffer and add overlays to dates."
  3701. (if (re-search-forward org-tsr-regexp-both limit t)
  3702. (progn
  3703. (add-text-properties (match-beginning 0) (match-end 0)
  3704. (list 'mouse-face 'highlight
  3705. 'rear-nonsticky org-nonsticky-props
  3706. 'keymap org-mouse-map))
  3707. (when org-display-custom-times
  3708. (if (match-end 3)
  3709. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3710. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3711. t)))
  3712. (defvar org-target-link-regexp nil
  3713. "Regular expression matching radio targets in plain text.")
  3714. (make-variable-buffer-local 'org-target-link-regexp)
  3715. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3716. "Regular expression matching a link target.")
  3717. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3718. "Regular expression matching a radio target.")
  3719. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3720. "Regular expression matching any target.")
  3721. (defun org-activate-target-links (limit)
  3722. "Run through the buffer and add overlays to target matches."
  3723. (when org-target-link-regexp
  3724. (let ((case-fold-search t))
  3725. (if (re-search-forward org-target-link-regexp limit t)
  3726. (progn
  3727. (add-text-properties (match-beginning 0) (match-end 0)
  3728. (list 'mouse-face 'highlight
  3729. 'rear-nonsticky org-nonsticky-props
  3730. 'keymap org-mouse-map
  3731. 'help-echo "Radio target link"
  3732. 'org-linked-text t))
  3733. t)))))
  3734. (defun org-update-radio-target-regexp ()
  3735. "Find all radio targets in this file and update the regular expression."
  3736. (interactive)
  3737. (when (memq 'radio org-activate-links)
  3738. (setq org-target-link-regexp
  3739. (org-make-target-link-regexp (org-all-targets 'radio)))
  3740. (org-restart-font-lock)))
  3741. (defun org-hide-wide-columns (limit)
  3742. (let (s e)
  3743. (setq s (text-property-any (point) (or limit (point-max))
  3744. 'org-cwidth t))
  3745. (when s
  3746. (setq e (next-single-property-change s 'org-cwidth))
  3747. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3748. (goto-char e)
  3749. t)))
  3750. (defvar org-latex-and-specials-regexp nil
  3751. "Regular expression for highlighting export special stuff.")
  3752. (defvar org-match-substring-regexp)
  3753. (defvar org-match-substring-with-braces-regexp)
  3754. (defvar org-export-html-special-string-regexps)
  3755. (defun org-compute-latex-and-specials-regexp ()
  3756. "Compute regular expression for stuff treated specially by exporters."
  3757. (if (not org-highlight-latex-fragments-and-specials)
  3758. (org-set-local 'org-latex-and-specials-regexp nil)
  3759. (require 'org-exp)
  3760. (let*
  3761. ((matchers (plist-get org-format-latex-options :matchers))
  3762. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3763. org-latex-regexps)))
  3764. (options (org-combine-plists (org-default-export-plist)
  3765. (org-infile-export-plist)))
  3766. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3767. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3768. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3769. (org-export-html-expand (plist-get options :expand-quoted-html))
  3770. (org-export-with-special-strings (plist-get options :special-strings))
  3771. (re-sub
  3772. (cond
  3773. ((equal org-export-with-sub-superscripts '{})
  3774. (list org-match-substring-with-braces-regexp))
  3775. (org-export-with-sub-superscripts
  3776. (list org-match-substring-regexp))
  3777. (t nil)))
  3778. (re-latex
  3779. (if org-export-with-LaTeX-fragments
  3780. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3781. (re-macros
  3782. (if org-export-with-TeX-macros
  3783. (list (concat "\\\\"
  3784. (regexp-opt
  3785. (append (mapcar 'car org-html-entities)
  3786. (if (boundp 'org-latex-entities)
  3787. org-latex-entities nil))
  3788. 'words))) ; FIXME
  3789. ))
  3790. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3791. (re-special (if org-export-with-special-strings
  3792. (mapcar (lambda (x) (car x))
  3793. org-export-html-special-string-regexps)))
  3794. (re-rest
  3795. (delq nil
  3796. (list
  3797. (if org-export-html-expand "@<[^>\n]+>")
  3798. ))))
  3799. (org-set-local
  3800. 'org-latex-and-specials-regexp
  3801. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3802. re-rest) "\\|")))))
  3803. (defun org-do-latex-and-special-faces (limit)
  3804. "Run through the buffer and add overlays to links."
  3805. (when org-latex-and-specials-regexp
  3806. (let (rtn d)
  3807. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3808. limit t))
  3809. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3810. 'face))
  3811. '(org-code org-verbatim underline)))
  3812. (progn
  3813. (setq rtn t
  3814. d (cond ((member (char-after (1+ (match-beginning 0)))
  3815. '(?_ ?^)) 1)
  3816. (t 0)))
  3817. (font-lock-prepend-text-property
  3818. (+ d (match-beginning 0)) (match-end 0)
  3819. 'face 'org-latex-and-export-specials)
  3820. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3821. '(font-lock-multiline t)))))
  3822. rtn)))
  3823. (defun org-restart-font-lock ()
  3824. "Restart font-lock-mode, to force refontification."
  3825. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3826. (font-lock-mode -1)
  3827. (font-lock-mode 1)))
  3828. (defun org-all-targets (&optional radio)
  3829. "Return a list of all targets in this file.
  3830. With optional argument RADIO, only find radio targets."
  3831. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3832. rtn)
  3833. (save-excursion
  3834. (goto-char (point-min))
  3835. (while (re-search-forward re nil t)
  3836. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3837. rtn)))
  3838. (defun org-make-target-link-regexp (targets)
  3839. "Make regular expression matching all strings in TARGETS.
  3840. The regular expression finds the targets also if there is a line break
  3841. between words."
  3842. (and targets
  3843. (concat
  3844. "\\<\\("
  3845. (mapconcat
  3846. (lambda (x)
  3847. (while (string-match " +" x)
  3848. (setq x (replace-match "\\s-+" t t x)))
  3849. x)
  3850. targets
  3851. "\\|")
  3852. "\\)\\>")))
  3853. (defun org-activate-tags (limit)
  3854. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3855. (progn
  3856. (add-text-properties (match-beginning 1) (match-end 1)
  3857. (list 'mouse-face 'highlight
  3858. 'rear-nonsticky org-nonsticky-props
  3859. 'keymap org-mouse-map))
  3860. t)))
  3861. (defun org-outline-level ()
  3862. (save-excursion
  3863. (looking-at outline-regexp)
  3864. (if (match-beginning 1)
  3865. (+ (org-get-string-indentation (match-string 1)) 1000)
  3866. (1- (- (match-end 0) (match-beginning 0))))))
  3867. (defvar org-font-lock-keywords nil)
  3868. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3869. "Regular expression matching a property line.")
  3870. (defvar org-font-lock-hook nil
  3871. "Functions to be called for special font lock stuff.")
  3872. (defun org-font-lock-hook (limit)
  3873. (run-hook-with-args 'org-font-lock-hook limit))
  3874. (defun org-set-font-lock-defaults ()
  3875. (let* ((em org-fontify-emphasized-text)
  3876. (lk org-activate-links)
  3877. (org-font-lock-extra-keywords
  3878. (list
  3879. ;; Call the hook
  3880. '(org-font-lock-hook)
  3881. ;; Headlines
  3882. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3883. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3884. ;; Table lines
  3885. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3886. (1 'org-table t))
  3887. ;; Table internals
  3888. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3889. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3890. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3891. ;; Drawers
  3892. (list org-drawer-regexp '(0 'org-special-keyword t))
  3893. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3894. ;; Properties
  3895. (list org-property-re
  3896. '(1 'org-special-keyword t)
  3897. '(3 'org-property-value t))
  3898. (if org-format-transports-properties-p
  3899. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3900. ;; Links
  3901. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3902. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3903. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3904. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3905. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3906. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3907. (if (memq 'footnote lk) '(org-activate-footnote-links
  3908. (2 'org-footnote t)))
  3909. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3910. '(org-hide-wide-columns (0 nil append))
  3911. ;; TODO lines
  3912. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  3913. '(1 (org-get-todo-face 1) t))
  3914. ;; DONE
  3915. (if org-fontify-done-headline
  3916. (list (concat "^[*]+ +\\<\\("
  3917. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3918. "\\)\\(.*\\)")
  3919. '(2 'org-headline-done t))
  3920. nil)
  3921. ;; Priorities
  3922. '(org-font-lock-add-priority-faces)
  3923. ;; Tags
  3924. '(org-font-lock-add-tag-faces)
  3925. ;; Special keywords
  3926. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3927. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3928. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3929. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3930. ;; Emphasis
  3931. (if em
  3932. (if (featurep 'xemacs)
  3933. '(org-do-emphasis-faces (0 nil append))
  3934. '(org-do-emphasis-faces)))
  3935. ;; Checkboxes
  3936. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3937. 2 'org-checkbox prepend)
  3938. (if org-provide-checkbox-statistics
  3939. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3940. (0 (org-get-checkbox-statistics-face) t)))
  3941. ;; Description list items
  3942. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3943. 2 'bold prepend)
  3944. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3945. '(1 'org-archived prepend))
  3946. ;; Specials
  3947. '(org-do-latex-and-special-faces)
  3948. ;; Code
  3949. '(org-activate-code (1 'org-code t))
  3950. ;; COMMENT
  3951. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3952. "\\|" org-quote-string "\\)\\>")
  3953. '(1 'org-special-keyword t))
  3954. '("^#.*" (0 'font-lock-comment-face t))
  3955. )))
  3956. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3957. ;; Now set the full font-lock-keywords
  3958. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3959. (org-set-local 'font-lock-defaults
  3960. '(org-font-lock-keywords t nil nil backward-paragraph))
  3961. (kill-local-variable 'font-lock-keywords) nil))
  3962. (defvar org-m nil)
  3963. (defvar org-l nil)
  3964. (defvar org-f nil)
  3965. (defun org-get-level-face (n)
  3966. "Get the right face for match N in font-lock matching of headlines."
  3967. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3968. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3969. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3970. (cond
  3971. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3972. ((eq n 2) org-f)
  3973. (t (if org-level-color-stars-only nil org-f))))
  3974. (defun org-get-todo-face (kwd)
  3975. "Get the right face for a TODO keyword KWD.
  3976. If KWD is a number, get the corresponding match group."
  3977. (if (numberp kwd) (setq kwd (match-string kwd)))
  3978. (or (cdr (assoc kwd org-todo-keyword-faces))
  3979. (and (member kwd org-done-keywords) 'org-done)
  3980. 'org-todo))
  3981. (defun org-font-lock-add-tag-faces (limit)
  3982. "Add the special tag faces."
  3983. (when (and org-tag-faces org-tags-special-faces-re)
  3984. (while (re-search-forward org-tags-special-faces-re limit t)
  3985. (add-text-properties (match-beginning 1) (match-end 1)
  3986. (list 'face (org-get-tag-face 1)
  3987. 'font-lock-fontified t))
  3988. (backward-char 1))))
  3989. (defun org-font-lock-add-priority-faces (limit)
  3990. "Add the special priority faces."
  3991. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  3992. (add-text-properties
  3993. (match-beginning 0) (match-end 0)
  3994. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  3995. org-priority-faces))
  3996. 'org-special-keyword)
  3997. 'font-lock-fontified t))))
  3998. (defun org-get-tag-face (kwd)
  3999. "Get the right face for a TODO keyword KWD.
  4000. If KWD is a number, get the corresponding match group."
  4001. (if (numberp kwd) (setq kwd (match-string kwd)))
  4002. (or (cdr (assoc kwd org-tag-faces))
  4003. 'org-tag))
  4004. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4005. "Remove fontification and activation overlays from links."
  4006. (font-lock-default-unfontify-region beg end)
  4007. (let* ((buffer-undo-list t)
  4008. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4009. (inhibit-modification-hooks t)
  4010. deactivate-mark buffer-file-name buffer-file-truename)
  4011. (remove-text-properties beg end
  4012. '(mouse-face t keymap t org-linked-text t
  4013. invisible t intangible t))))
  4014. ;;;; Visibility cycling, including org-goto and indirect buffer
  4015. ;;; Cycling
  4016. (defvar org-cycle-global-status nil)
  4017. (make-variable-buffer-local 'org-cycle-global-status)
  4018. (defvar org-cycle-subtree-status nil)
  4019. (make-variable-buffer-local 'org-cycle-subtree-status)
  4020. ;;;###autoload
  4021. (defvar org-inlinetask-min-level)
  4022. (defun org-cycle (&optional arg)
  4023. "Visibility cycling for Org-mode.
  4024. - When this function is called with a prefix argument, rotate the entire
  4025. buffer through 3 states (global cycling)
  4026. 1. OVERVIEW: Show only top-level headlines.
  4027. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4028. 3. SHOW ALL: Show everything.
  4029. When called with two C-u C-u prefixes, switch to the startup visibility,
  4030. determined by the variable `org-startup-folded', and by any VISIBILITY
  4031. properties in the buffer.
  4032. When called with three C-u C-u C-u prefixed, show the entire buffer,
  4033. including drawers.
  4034. - When point is at the beginning of a headline, rotate the subtree started
  4035. by this line through 3 different states (local cycling)
  4036. 1. FOLDED: Only the main headline is shown.
  4037. 2. CHILDREN: The main headline and the direct children are shown.
  4038. From this state, you can move to one of the children
  4039. and zoom in further.
  4040. 3. SUBTREE: Show the entire subtree, including body text.
  4041. - When there is a numeric prefix, go up to a heading with level ARG, do
  4042. a `show-subtree' and return to the previous cursor position. If ARG
  4043. is negative, go up that many levels.
  4044. - When point is not at the beginning of a headline, execute the global
  4045. binding for TAB, which is re-indenting the line. See the option
  4046. `org-cycle-emulate-tab' for details.
  4047. - Special case: if point is at the beginning of the buffer and there is
  4048. no headline in line 1, this function will act as if called with prefix arg.
  4049. But only if also the variable `org-cycle-global-at-bob' is t."
  4050. (interactive "P")
  4051. (org-load-modules-maybe)
  4052. (let* ((limit-level
  4053. (or org-cycle-max-level
  4054. (and (boundp 'org-inlinetask-min-level)
  4055. org-inlinetask-min-level
  4056. (1- org-inlinetask-min-level))))
  4057. (nstars (and limit-level
  4058. (if org-odd-levels-only
  4059. (and limit-level (1- (* limit-level 2)))
  4060. limit-level)))
  4061. (outline-regexp
  4062. (cond
  4063. ((not (org-mode-p)) outline-regexp)
  4064. (org-cycle-include-plain-lists
  4065. (concat "\\(?:\\*"
  4066. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4067. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4068. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4069. (bob-special (and org-cycle-global-at-bob (bobp)
  4070. (not (looking-at outline-regexp))))
  4071. (org-cycle-hook
  4072. (if bob-special
  4073. (delq 'org-optimize-window-after-visibility-change
  4074. (copy-sequence org-cycle-hook))
  4075. org-cycle-hook))
  4076. (pos (point)))
  4077. (if (or bob-special (equal arg '(4)))
  4078. ;; special case: use global cycling
  4079. (setq arg t))
  4080. (cond
  4081. ((equal arg '(16))
  4082. (org-set-startup-visibility)
  4083. (message "Startup visibility, plus VISIBILITY properties"))
  4084. ((equal arg '(64))
  4085. (show-all)
  4086. (message "Entire buffer visible, including drawers"))
  4087. ((org-at-table-p 'any)
  4088. ;; Enter the table or move to the next field in the table
  4089. (or (org-table-recognize-table.el)
  4090. (progn
  4091. (if arg (org-table-edit-field t)
  4092. (org-table-justify-field-maybe)
  4093. (call-interactively 'org-table-next-field)))))
  4094. ((eq arg t) ;; Global cycling
  4095. (cond
  4096. ((and (eq last-command this-command)
  4097. (eq org-cycle-global-status 'overview))
  4098. ;; We just created the overview - now do table of contents
  4099. ;; This can be slow in very large buffers, so indicate action
  4100. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4101. (message "CONTENTS...")
  4102. (org-content)
  4103. (message "CONTENTS...done")
  4104. (setq org-cycle-global-status 'contents)
  4105. (run-hook-with-args 'org-cycle-hook 'contents))
  4106. ((and (eq last-command this-command)
  4107. (eq org-cycle-global-status 'contents))
  4108. ;; We just showed the table of contents - now show everything
  4109. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4110. (show-all)
  4111. (message "SHOW ALL")
  4112. (setq org-cycle-global-status 'all)
  4113. (run-hook-with-args 'org-cycle-hook 'all))
  4114. (t
  4115. ;; Default action: go to overview
  4116. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4117. (org-overview)
  4118. (message "OVERVIEW")
  4119. (setq org-cycle-global-status 'overview)
  4120. (run-hook-with-args 'org-cycle-hook 'overview))))
  4121. ((and org-drawers org-drawer-regexp
  4122. (save-excursion
  4123. (beginning-of-line 1)
  4124. (looking-at org-drawer-regexp)))
  4125. ;; Toggle block visibility
  4126. (org-flag-drawer
  4127. (not (get-char-property (match-end 0) 'invisible))))
  4128. ((integerp arg)
  4129. ;; Show-subtree, ARG levels up from here.
  4130. (save-excursion
  4131. (org-back-to-heading)
  4132. (outline-up-heading (if (< arg 0) (- arg)
  4133. (- (funcall outline-level) arg)))
  4134. (org-show-subtree)))
  4135. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4136. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4137. ;; At a heading: rotate between three different views
  4138. (org-back-to-heading)
  4139. (let ((goal-column 0) eoh eol eos)
  4140. ;; First, some boundaries
  4141. (save-excursion
  4142. (org-back-to-heading)
  4143. (save-excursion
  4144. (beginning-of-line 2)
  4145. (while (and (not (eobp)) ;; this is like `next-line'
  4146. (get-char-property (1- (point)) 'invisible))
  4147. (beginning-of-line 2)) (setq eol (point)))
  4148. (outline-end-of-heading) (setq eoh (point))
  4149. (org-end-of-subtree t)
  4150. (unless (eobp)
  4151. (skip-chars-forward " \t\n")
  4152. (beginning-of-line 1) ; in case this is an item
  4153. )
  4154. (setq eos (1- (point))))
  4155. ;; Find out what to do next and set `this-command'
  4156. (cond
  4157. ((= eos eoh)
  4158. ;; Nothing is hidden behind this heading
  4159. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4160. (message "EMPTY ENTRY")
  4161. (setq org-cycle-subtree-status nil)
  4162. (save-excursion
  4163. (goto-char eos)
  4164. (outline-next-heading)
  4165. (if (org-invisible-p) (org-flag-heading nil))))
  4166. ((or (>= eol eos)
  4167. (not (string-match "\\S-" (buffer-substring eol eos))))
  4168. ;; Entire subtree is hidden in one line: open it
  4169. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4170. (org-show-entry)
  4171. (show-children)
  4172. (message "CHILDREN")
  4173. (save-excursion
  4174. (goto-char eos)
  4175. (outline-next-heading)
  4176. (if (org-invisible-p) (org-flag-heading nil)))
  4177. (setq org-cycle-subtree-status 'children)
  4178. (run-hook-with-args 'org-cycle-hook 'children))
  4179. ((and (eq last-command this-command)
  4180. (eq org-cycle-subtree-status 'children))
  4181. ;; We just showed the children, now show everything.
  4182. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4183. (org-show-subtree)
  4184. (message "SUBTREE")
  4185. (setq org-cycle-subtree-status 'subtree)
  4186. (run-hook-with-args 'org-cycle-hook 'subtree))
  4187. (t
  4188. ;; Default action: hide the subtree.
  4189. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4190. (hide-subtree)
  4191. (message "FOLDED")
  4192. (setq org-cycle-subtree-status 'folded)
  4193. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4194. ;; TAB emulation and template completion
  4195. (buffer-read-only (org-back-to-heading))
  4196. ((org-try-structure-completion))
  4197. ((org-try-cdlatex-tab))
  4198. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4199. (or (not (bolp))
  4200. (not (looking-at outline-regexp))))
  4201. (call-interactively (global-key-binding "\t")))
  4202. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4203. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4204. (or (and (eq org-cycle-emulate-tab 'white)
  4205. (= (match-end 0) (point-at-eol)))
  4206. (and (eq org-cycle-emulate-tab 'whitestart)
  4207. (>= (match-end 0) pos))))
  4208. t
  4209. (eq org-cycle-emulate-tab t))
  4210. (call-interactively (global-key-binding "\t")))
  4211. (t (save-excursion
  4212. (org-back-to-heading)
  4213. (org-cycle))))))
  4214. ;;;###autoload
  4215. (defun org-global-cycle (&optional arg)
  4216. "Cycle the global visibility. For details see `org-cycle'.
  4217. With C-u prefix arg, switch to startup visibility.
  4218. With a numeric prefix, show all headlines up to that level."
  4219. (interactive "P")
  4220. (let ((org-cycle-include-plain-lists
  4221. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4222. (cond
  4223. ((integerp arg)
  4224. (show-all)
  4225. (hide-sublevels arg)
  4226. (setq org-cycle-global-status 'contents))
  4227. ((equal arg '(4))
  4228. (org-set-startup-visibility)
  4229. (message "Startup visibility, plus VISIBILITY properties."))
  4230. (t
  4231. (org-cycle '(4))))))
  4232. (defun org-set-startup-visibility ()
  4233. "Set the visibility required by startup options and properties."
  4234. (cond
  4235. ((eq org-startup-folded t)
  4236. (org-cycle '(4)))
  4237. ((eq org-startup-folded 'content)
  4238. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4239. (org-cycle '(4)) (org-cycle '(4)))))
  4240. (org-set-visibility-according-to-property 'no-cleanup)
  4241. (org-cycle-hide-archived-subtrees 'all)
  4242. (org-cycle-hide-drawers 'all)
  4243. (org-cycle-show-empty-lines 'all))
  4244. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4245. "Switch subtree visibilities according to :VISIBILITY: property."
  4246. (interactive)
  4247. (let (org-show-entry-below state)
  4248. (save-excursion
  4249. (goto-char (point-min))
  4250. (while (re-search-forward
  4251. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4252. nil t)
  4253. (setq state (match-string 1))
  4254. (save-excursion
  4255. (org-back-to-heading t)
  4256. (hide-subtree)
  4257. (org-reveal)
  4258. (cond
  4259. ((equal state '("fold" "folded"))
  4260. (hide-subtree))
  4261. ((equal state "children")
  4262. (org-show-hidden-entry)
  4263. (show-children))
  4264. ((equal state "content")
  4265. (save-excursion
  4266. (save-restriction
  4267. (org-narrow-to-subtree)
  4268. (org-content))))
  4269. ((member state '("all" "showall"))
  4270. (show-subtree)))))
  4271. (unless no-cleanup
  4272. (org-cycle-hide-archived-subtrees 'all)
  4273. (org-cycle-hide-drawers 'all)
  4274. (org-cycle-show-empty-lines 'all)))))
  4275. (defun org-overview ()
  4276. "Switch to overview mode, showing only top-level headlines.
  4277. Really, this shows all headlines with level equal or greater than the level
  4278. of the first headline in the buffer. This is important, because if the
  4279. first headline is not level one, then (hide-sublevels 1) gives confusing
  4280. results."
  4281. (interactive)
  4282. (let ((level (save-excursion
  4283. (goto-char (point-min))
  4284. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4285. (progn
  4286. (goto-char (match-beginning 0))
  4287. (funcall outline-level))))))
  4288. (and level (hide-sublevels level))))
  4289. (defun org-content (&optional arg)
  4290. "Show all headlines in the buffer, like a table of contents.
  4291. With numerical argument N, show content up to level N."
  4292. (interactive "P")
  4293. (save-excursion
  4294. ;; Visit all headings and show their offspring
  4295. (and (integerp arg) (org-overview))
  4296. (goto-char (point-max))
  4297. (catch 'exit
  4298. (while (and (progn (condition-case nil
  4299. (outline-previous-visible-heading 1)
  4300. (error (goto-char (point-min))))
  4301. t)
  4302. (looking-at outline-regexp))
  4303. (if (integerp arg)
  4304. (show-children (1- arg))
  4305. (show-branches))
  4306. (if (bobp) (throw 'exit nil))))))
  4307. (defun org-optimize-window-after-visibility-change (state)
  4308. "Adjust the window after a change in outline visibility.
  4309. This function is the default value of the hook `org-cycle-hook'."
  4310. (when (get-buffer-window (current-buffer))
  4311. (cond
  4312. ((eq state 'content) nil)
  4313. ((eq state 'all) nil)
  4314. ((eq state 'folded) nil)
  4315. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4316. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4317. (defun org-compact-display-after-subtree-move ()
  4318. "Show a compacter version of the tree of the entry's parent."
  4319. (save-excursion
  4320. (if (org-up-heading-safe)
  4321. (progn
  4322. (hide-subtree)
  4323. (show-entry)
  4324. (show-children)
  4325. (org-cycle-show-empty-lines 'children)
  4326. (org-cycle-hide-drawers 'children))
  4327. (org-overview))))
  4328. (defun org-cycle-show-empty-lines (state)
  4329. "Show empty lines above all visible headlines.
  4330. The region to be covered depends on STATE when called through
  4331. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4332. entire buffer covered. Note that an empty line is only shown if there
  4333. are at least `org-cycle-separator-lines' empty lines before the headline."
  4334. (when (> org-cycle-separator-lines 0)
  4335. (save-excursion
  4336. (let* ((n org-cycle-separator-lines)
  4337. (re (cond
  4338. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4339. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4340. (t (let ((ns (number-to-string (- n 2))))
  4341. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4342. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4343. beg end)
  4344. (cond
  4345. ((memq state '(overview contents t))
  4346. (setq beg (point-min) end (point-max)))
  4347. ((memq state '(children folded))
  4348. (setq beg (point) end (progn (org-end-of-subtree t t)
  4349. (beginning-of-line 2)
  4350. (point)))))
  4351. (when beg
  4352. (goto-char beg)
  4353. (while (re-search-forward re end t)
  4354. (if (not (get-char-property (match-end 1) 'invisible))
  4355. (outline-flag-region
  4356. (match-beginning 1) (match-end 1) nil)))))))
  4357. ;; Never hide empty lines at the end of the file.
  4358. (save-excursion
  4359. (goto-char (point-max))
  4360. (outline-previous-heading)
  4361. (outline-end-of-heading)
  4362. (if (and (looking-at "[ \t\n]+")
  4363. (= (match-end 0) (point-max)))
  4364. (outline-flag-region (point) (match-end 0) nil))))
  4365. (defun org-show-empty-lines-in-parent ()
  4366. "Move to the parent and re-show empty lines before visible headlines."
  4367. (save-excursion
  4368. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4369. (org-cycle-show-empty-lines context))))
  4370. (defun org-cycle-hide-drawers (state)
  4371. "Re-hide all drawers after a visibility state change."
  4372. (when (and (org-mode-p)
  4373. (not (memq state '(overview folded))))
  4374. (save-excursion
  4375. (let* ((globalp (memq state '(contents all)))
  4376. (beg (if globalp (point-min) (point)))
  4377. (end (if globalp (point-max) (org-end-of-subtree t))))
  4378. (goto-char beg)
  4379. (while (re-search-forward org-drawer-regexp end t)
  4380. (org-flag-drawer t))))))
  4381. (defun org-flag-drawer (flag)
  4382. (save-excursion
  4383. (beginning-of-line 1)
  4384. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4385. (let ((b (match-end 0))
  4386. (outline-regexp org-outline-regexp))
  4387. (if (re-search-forward
  4388. "^[ \t]*:END:"
  4389. (save-excursion (outline-next-heading) (point)) t)
  4390. (outline-flag-region b (point-at-eol) flag)
  4391. (error ":END: line missing"))))))
  4392. (defun org-subtree-end-visible-p ()
  4393. "Is the end of the current subtree visible?"
  4394. (pos-visible-in-window-p
  4395. (save-excursion (org-end-of-subtree t) (point))))
  4396. (defun org-first-headline-recenter (&optional N)
  4397. "Move cursor to the first headline and recenter the headline.
  4398. Optional argument N means, put the headline into the Nth line of the window."
  4399. (goto-char (point-min))
  4400. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4401. (beginning-of-line)
  4402. (recenter (prefix-numeric-value N))))
  4403. ;;; Org-goto
  4404. (defvar org-goto-window-configuration nil)
  4405. (defvar org-goto-marker nil)
  4406. (defvar org-goto-map
  4407. (let ((map (make-sparse-keymap)))
  4408. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4409. (while (setq cmd (pop cmds))
  4410. (substitute-key-definition cmd cmd map global-map)))
  4411. (suppress-keymap map)
  4412. (org-defkey map "\C-m" 'org-goto-ret)
  4413. (org-defkey map [(return)] 'org-goto-ret)
  4414. (org-defkey map [(left)] 'org-goto-left)
  4415. (org-defkey map [(right)] 'org-goto-right)
  4416. (org-defkey map [(control ?g)] 'org-goto-quit)
  4417. (org-defkey map "\C-i" 'org-cycle)
  4418. (org-defkey map [(tab)] 'org-cycle)
  4419. (org-defkey map [(down)] 'outline-next-visible-heading)
  4420. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4421. (if org-goto-auto-isearch
  4422. (if (fboundp 'define-key-after)
  4423. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4424. nil)
  4425. (org-defkey map "q" 'org-goto-quit)
  4426. (org-defkey map "n" 'outline-next-visible-heading)
  4427. (org-defkey map "p" 'outline-previous-visible-heading)
  4428. (org-defkey map "f" 'outline-forward-same-level)
  4429. (org-defkey map "b" 'outline-backward-same-level)
  4430. (org-defkey map "u" 'outline-up-heading))
  4431. (org-defkey map "/" 'org-occur)
  4432. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4433. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4434. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4435. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4436. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4437. map))
  4438. (defconst org-goto-help
  4439. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4440. RET=jump to location [Q]uit and return to previous location
  4441. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4442. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4443. ;; FIXME: Docstring doe not mention both interfaces
  4444. (defun org-goto (&optional alternative-interface)
  4445. "Look up a different location in the current file, keeping current visibility.
  4446. When you want look-up or go to a different location in a document, the
  4447. fastest way is often to fold the entire buffer and then dive into the tree.
  4448. This method has the disadvantage, that the previous location will be folded,
  4449. which may not be what you want.
  4450. This command works around this by showing a copy of the current buffer
  4451. in an indirect buffer, in overview mode. You can dive into the tree in
  4452. that copy, use org-occur and incremental search to find a location.
  4453. When pressing RET or `Q', the command returns to the original buffer in
  4454. which the visibility is still unchanged. After RET is will also jump to
  4455. the location selected in the indirect buffer and expose the
  4456. the headline hierarchy above."
  4457. (interactive "P")
  4458. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4459. (org-refile-use-outline-path t)
  4460. (interface
  4461. (if (not alternative-interface)
  4462. org-goto-interface
  4463. (if (eq org-goto-interface 'outline)
  4464. 'outline-path-completion
  4465. 'outline)))
  4466. (org-goto-start-pos (point))
  4467. (selected-point
  4468. (if (eq interface 'outline)
  4469. (car (org-get-location (current-buffer) org-goto-help))
  4470. (nth 3 (org-refile-get-location "Goto: ")))))
  4471. (if selected-point
  4472. (progn
  4473. (org-mark-ring-push org-goto-start-pos)
  4474. (goto-char selected-point)
  4475. (if (or (org-invisible-p) (org-invisible-p2))
  4476. (org-show-context 'org-goto)))
  4477. (message "Quit"))))
  4478. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4479. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4480. (defvar org-goto-local-auto-isearch-map) ; defined below
  4481. (defun org-get-location (buf help)
  4482. "Let the user select a location in the Org-mode buffer BUF.
  4483. This function uses a recursive edit. It returns the selected position
  4484. or nil."
  4485. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4486. (isearch-hide-immediately nil)
  4487. (isearch-search-fun-function
  4488. (lambda () 'org-goto-local-search-headings))
  4489. (org-goto-selected-point org-goto-exit-command))
  4490. (save-excursion
  4491. (save-window-excursion
  4492. (delete-other-windows)
  4493. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4494. (switch-to-buffer
  4495. (condition-case nil
  4496. (make-indirect-buffer (current-buffer) "*org-goto*")
  4497. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4498. (with-output-to-temp-buffer "*Help*"
  4499. (princ help))
  4500. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4501. (setq buffer-read-only nil)
  4502. (let ((org-startup-truncated t)
  4503. (org-startup-folded nil)
  4504. (org-startup-align-all-tables nil))
  4505. (org-mode)
  4506. (org-overview))
  4507. (setq buffer-read-only t)
  4508. (if (and (boundp 'org-goto-start-pos)
  4509. (integer-or-marker-p org-goto-start-pos))
  4510. (let ((org-show-hierarchy-above t)
  4511. (org-show-siblings t)
  4512. (org-show-following-heading t))
  4513. (goto-char org-goto-start-pos)
  4514. (and (org-invisible-p) (org-show-context)))
  4515. (goto-char (point-min)))
  4516. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4517. (message "Select location and press RET")
  4518. (use-local-map org-goto-map)
  4519. (recursive-edit)
  4520. ))
  4521. (kill-buffer "*org-goto*")
  4522. (cons org-goto-selected-point org-goto-exit-command)))
  4523. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4524. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4525. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4526. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4527. (defun org-goto-local-search-headings (string bound noerror)
  4528. "Search and make sure that any matches are in headlines."
  4529. (catch 'return
  4530. (while (if isearch-forward
  4531. (search-forward string bound noerror)
  4532. (search-backward string bound noerror))
  4533. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4534. (and (member :headline context)
  4535. (not (member :tags context))))
  4536. (throw 'return (point))))))
  4537. (defun org-goto-local-auto-isearch ()
  4538. "Start isearch."
  4539. (interactive)
  4540. (goto-char (point-min))
  4541. (let ((keys (this-command-keys)))
  4542. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4543. (isearch-mode t)
  4544. (isearch-process-search-char (string-to-char keys)))))
  4545. (defun org-goto-ret (&optional arg)
  4546. "Finish `org-goto' by going to the new location."
  4547. (interactive "P")
  4548. (setq org-goto-selected-point (point)
  4549. org-goto-exit-command 'return)
  4550. (throw 'exit nil))
  4551. (defun org-goto-left ()
  4552. "Finish `org-goto' by going to the new location."
  4553. (interactive)
  4554. (if (org-on-heading-p)
  4555. (progn
  4556. (beginning-of-line 1)
  4557. (setq org-goto-selected-point (point)
  4558. org-goto-exit-command 'left)
  4559. (throw 'exit nil))
  4560. (error "Not on a heading")))
  4561. (defun org-goto-right ()
  4562. "Finish `org-goto' by going to the new location."
  4563. (interactive)
  4564. (if (org-on-heading-p)
  4565. (progn
  4566. (setq org-goto-selected-point (point)
  4567. org-goto-exit-command 'right)
  4568. (throw 'exit nil))
  4569. (error "Not on a heading")))
  4570. (defun org-goto-quit ()
  4571. "Finish `org-goto' without cursor motion."
  4572. (interactive)
  4573. (setq org-goto-selected-point nil)
  4574. (setq org-goto-exit-command 'quit)
  4575. (throw 'exit nil))
  4576. ;;; Indirect buffer display of subtrees
  4577. (defvar org-indirect-dedicated-frame nil
  4578. "This is the frame being used for indirect tree display.")
  4579. (defvar org-last-indirect-buffer nil)
  4580. (defun org-tree-to-indirect-buffer (&optional arg)
  4581. "Create indirect buffer and narrow it to current subtree.
  4582. With numerical prefix ARG, go up to this level and then take that tree.
  4583. If ARG is negative, go up that many levels.
  4584. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4585. indirect buffer previously made with this command, to avoid proliferation of
  4586. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4587. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4588. is kept so that you can work with several indirect buffers at the same time.
  4589. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4590. requests that a new frame be made for the new buffer, so that the dedicated
  4591. frame is not changed."
  4592. (interactive "P")
  4593. (let ((cbuf (current-buffer))
  4594. (cwin (selected-window))
  4595. (pos (point))
  4596. beg end level heading ibuf)
  4597. (save-excursion
  4598. (org-back-to-heading t)
  4599. (when (numberp arg)
  4600. (setq level (org-outline-level))
  4601. (if (< arg 0) (setq arg (+ level arg)))
  4602. (while (> (setq level (org-outline-level)) arg)
  4603. (outline-up-heading 1 t)))
  4604. (setq beg (point)
  4605. heading (org-get-heading))
  4606. (org-end-of-subtree t) (setq end (point)))
  4607. (if (and (buffer-live-p org-last-indirect-buffer)
  4608. (not (eq org-indirect-buffer-display 'new-frame))
  4609. (not arg))
  4610. (kill-buffer org-last-indirect-buffer))
  4611. (setq ibuf (org-get-indirect-buffer cbuf)
  4612. org-last-indirect-buffer ibuf)
  4613. (cond
  4614. ((or (eq org-indirect-buffer-display 'new-frame)
  4615. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4616. (select-frame (make-frame))
  4617. (delete-other-windows)
  4618. (switch-to-buffer ibuf)
  4619. (org-set-frame-title heading))
  4620. ((eq org-indirect-buffer-display 'dedicated-frame)
  4621. (raise-frame
  4622. (select-frame (or (and org-indirect-dedicated-frame
  4623. (frame-live-p org-indirect-dedicated-frame)
  4624. org-indirect-dedicated-frame)
  4625. (setq org-indirect-dedicated-frame (make-frame)))))
  4626. (delete-other-windows)
  4627. (switch-to-buffer ibuf)
  4628. (org-set-frame-title (concat "Indirect: " heading)))
  4629. ((eq org-indirect-buffer-display 'current-window)
  4630. (switch-to-buffer ibuf))
  4631. ((eq org-indirect-buffer-display 'other-window)
  4632. (pop-to-buffer ibuf))
  4633. (t (error "Invalid value.")))
  4634. (if (featurep 'xemacs)
  4635. (save-excursion (org-mode) (turn-on-font-lock)))
  4636. (narrow-to-region beg end)
  4637. (show-all)
  4638. (goto-char pos)
  4639. (and (window-live-p cwin) (select-window cwin))))
  4640. (defun org-get-indirect-buffer (&optional buffer)
  4641. (setq buffer (or buffer (current-buffer)))
  4642. (let ((n 1) (base (buffer-name buffer)) bname)
  4643. (while (buffer-live-p
  4644. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4645. (setq n (1+ n)))
  4646. (condition-case nil
  4647. (make-indirect-buffer buffer bname 'clone)
  4648. (error (make-indirect-buffer buffer bname)))))
  4649. (defun org-set-frame-title (title)
  4650. "Set the title of the current frame to the string TITLE."
  4651. ;; FIXME: how to name a single frame in XEmacs???
  4652. (unless (featurep 'xemacs)
  4653. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4654. ;;;; Structure editing
  4655. ;;; Inserting headlines
  4656. (defun org-previous-line-empty-p ()
  4657. (save-excursion
  4658. (and (not (bobp))
  4659. (or (beginning-of-line 0) t)
  4660. (save-match-data
  4661. (looking-at "[ \t]*$")))))
  4662. (defun org-insert-heading (&optional force-heading)
  4663. "Insert a new heading or item with same depth at point.
  4664. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4665. If point is at the beginning of a headline, insert a sibling before the
  4666. current headline. If point is not at the beginning, do not split the line,
  4667. but create the new headline after the current line."
  4668. (interactive "P")
  4669. (if (= (buffer-size) 0)
  4670. (insert "\n* ")
  4671. (when (or force-heading (not (org-insert-item)))
  4672. (let* ((empty-line-p nil)
  4673. (head (save-excursion
  4674. (condition-case nil
  4675. (progn
  4676. (org-back-to-heading)
  4677. (setq empty-line-p (org-previous-line-empty-p))
  4678. (match-string 0))
  4679. (error "*"))))
  4680. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4681. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4682. pos hide-previous previous-pos)
  4683. (cond
  4684. ((and (org-on-heading-p) (bolp)
  4685. (or (bobp)
  4686. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4687. ;; insert before the current line
  4688. (open-line (if blank 2 1)))
  4689. ((and (bolp)
  4690. (or (bobp)
  4691. (save-excursion
  4692. (backward-char 1) (not (org-invisible-p)))))
  4693. ;; insert right here
  4694. nil)
  4695. (t
  4696. ;; somewhere in the line
  4697. (save-excursion
  4698. (setq previous-pos (point-at-bol))
  4699. (end-of-line)
  4700. (setq hide-previous (org-invisible-p)))
  4701. (and org-insert-heading-respect-content (org-show-subtree))
  4702. (let ((split
  4703. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4704. (save-excursion
  4705. (let ((p (point)))
  4706. (goto-char (point-at-bol))
  4707. (and (looking-at org-complex-heading-regexp)
  4708. (> p (match-beginning 4)))))))
  4709. tags pos)
  4710. (cond
  4711. (org-insert-heading-respect-content
  4712. (org-end-of-subtree nil t)
  4713. (or (bolp) (newline))
  4714. (or (org-previous-line-empty-p)
  4715. (and blank (newline)))
  4716. (open-line 1))
  4717. ((org-on-heading-p)
  4718. (when hide-previous
  4719. (show-children)
  4720. (org-show-entry))
  4721. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4722. (setq tags (and (match-end 2) (match-string 2)))
  4723. (and (match-end 1)
  4724. (delete-region (match-beginning 1) (match-end 1)))
  4725. (setq pos (point-at-bol))
  4726. (or split (end-of-line 1))
  4727. (delete-horizontal-space)
  4728. (newline (if blank 2 1))
  4729. (when tags
  4730. (save-excursion
  4731. (goto-char pos)
  4732. (end-of-line 1)
  4733. (insert " " tags)
  4734. (org-set-tags nil 'align))))
  4735. (t
  4736. (or split (end-of-line 1))
  4737. (newline (if blank 2 1)))))))
  4738. (insert head) (just-one-space)
  4739. (setq pos (point))
  4740. (end-of-line 1)
  4741. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4742. (when (and org-insert-heading-respect-content hide-previous)
  4743. (save-excursion
  4744. (goto-char previous-pos)
  4745. (hide-subtree)))
  4746. (run-hooks 'org-insert-heading-hook)))))
  4747. (defun org-get-heading (&optional no-tags)
  4748. "Return the heading of the current entry, without the stars."
  4749. (save-excursion
  4750. (org-back-to-heading t)
  4751. (if (looking-at
  4752. (if no-tags
  4753. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4754. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4755. (match-string 1) "")))
  4756. (defun org-heading-components ()
  4757. "Return the components of the current heading.
  4758. This is a list with the following elements:
  4759. - the level as an integer
  4760. - the reduced level, different if `org-odd-levels-only' is set.
  4761. - the TODO keyword, or nil
  4762. - the priority character, like ?A, or nil if no priority is given
  4763. - the headline text itself, or the tags string if no headline text
  4764. - the tags string, or nil."
  4765. (save-excursion
  4766. (org-back-to-heading t)
  4767. (if (looking-at org-complex-heading-regexp)
  4768. (list (length (match-string 1))
  4769. (org-reduced-level (length (match-string 1)))
  4770. (org-match-string-no-properties 2)
  4771. (and (match-end 3) (aref (match-string 3) 2))
  4772. (org-match-string-no-properties 4)
  4773. (org-match-string-no-properties 5)))))
  4774. (defun org-get-entry ()
  4775. "Get the entry text, after heading, entire subtree."
  4776. (save-excursion
  4777. (org-back-to-heading t)
  4778. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  4779. (defun org-insert-heading-after-current ()
  4780. "Insert a new heading with same level as current, after current subtree."
  4781. (interactive)
  4782. (org-back-to-heading)
  4783. (org-insert-heading)
  4784. (org-move-subtree-down)
  4785. (end-of-line 1))
  4786. (defun org-insert-heading-respect-content ()
  4787. (interactive)
  4788. (let ((org-insert-heading-respect-content t))
  4789. (org-insert-heading t)))
  4790. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4791. (interactive "P")
  4792. (let ((org-insert-heading-respect-content t))
  4793. (org-insert-todo-heading force-state t)))
  4794. (defun org-insert-todo-heading (arg &optional force-heading)
  4795. "Insert a new heading with the same level and TODO state as current heading.
  4796. If the heading has no TODO state, or if the state is DONE, use the first
  4797. state (TODO by default). Also with prefix arg, force first state."
  4798. (interactive "P")
  4799. (when (or force-heading (not (org-insert-item 'checkbox)))
  4800. (org-insert-heading force-heading)
  4801. (save-excursion
  4802. (org-back-to-heading)
  4803. (outline-previous-heading)
  4804. (looking-at org-todo-line-regexp))
  4805. (let*
  4806. ((new-mark-x
  4807. (if (or arg
  4808. (not (match-beginning 2))
  4809. (member (match-string 2) org-done-keywords))
  4810. (car org-todo-keywords-1)
  4811. (match-string 2)))
  4812. (new-mark
  4813. (or
  4814. (run-hook-with-args-until-success
  4815. 'org-todo-get-default-hook new-mark-x nil)
  4816. new-mark-x)))
  4817. (beginning-of-line 1)
  4818. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4819. (insert new-mark " ")))
  4820. (when org-provide-todo-statistics
  4821. (org-update-parent-todo-statistics))))
  4822. (defun org-insert-subheading (arg)
  4823. "Insert a new subheading and demote it.
  4824. Works for outline headings and for plain lists alike."
  4825. (interactive "P")
  4826. (org-insert-heading arg)
  4827. (cond
  4828. ((org-on-heading-p) (org-do-demote))
  4829. ((org-at-item-p) (org-indent-item 1))))
  4830. (defun org-insert-todo-subheading (arg)
  4831. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4832. Works for outline headings and for plain lists alike."
  4833. (interactive "P")
  4834. (org-insert-todo-heading arg)
  4835. (cond
  4836. ((org-on-heading-p) (org-do-demote))
  4837. ((org-at-item-p) (org-indent-item 1))))
  4838. ;;; Promotion and Demotion
  4839. (defun org-promote-subtree ()
  4840. "Promote the entire subtree.
  4841. See also `org-promote'."
  4842. (interactive)
  4843. (save-excursion
  4844. (org-map-tree 'org-promote))
  4845. (org-fix-position-after-promote))
  4846. (defun org-demote-subtree ()
  4847. "Demote the entire subtree. See `org-demote'.
  4848. See also `org-promote'."
  4849. (interactive)
  4850. (save-excursion
  4851. (org-map-tree 'org-demote))
  4852. (org-fix-position-after-promote))
  4853. (defun org-do-promote ()
  4854. "Promote the current heading higher up the tree.
  4855. If the region is active in `transient-mark-mode', promote all headings
  4856. in the region."
  4857. (interactive)
  4858. (save-excursion
  4859. (if (org-region-active-p)
  4860. (org-map-region 'org-promote (region-beginning) (region-end))
  4861. (org-promote)))
  4862. (org-fix-position-after-promote))
  4863. (defun org-do-demote ()
  4864. "Demote the current heading lower down the tree.
  4865. If the region is active in `transient-mark-mode', demote all headings
  4866. in the region."
  4867. (interactive)
  4868. (save-excursion
  4869. (if (org-region-active-p)
  4870. (org-map-region 'org-demote (region-beginning) (region-end))
  4871. (org-demote)))
  4872. (org-fix-position-after-promote))
  4873. (defun org-fix-position-after-promote ()
  4874. "Make sure that after pro/demotion cursor position is right."
  4875. (let ((pos (point)))
  4876. (when (save-excursion
  4877. (beginning-of-line 1)
  4878. (looking-at org-todo-line-regexp)
  4879. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4880. (cond ((eobp) (insert " "))
  4881. ((eolp) (insert " "))
  4882. ((equal (char-after) ?\ ) (forward-char 1))))))
  4883. (defun org-reduced-level (l)
  4884. "Compute the effective level of a heading.
  4885. This takes into account the setting of `org-odd-levels-only'."
  4886. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4887. (defun org-get-valid-level (level &optional change)
  4888. "Rectify a level change under the influence of `org-odd-levels-only'
  4889. LEVEL is a current level, CHANGE is by how much the level should be
  4890. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4891. even level numbers will become the next higher odd number."
  4892. (if org-odd-levels-only
  4893. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4894. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4895. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4896. (max 1 (+ level (or change 0)))))
  4897. (if (boundp 'define-obsolete-function-alias)
  4898. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4899. (define-obsolete-function-alias 'org-get-legal-level
  4900. 'org-get-valid-level)
  4901. (define-obsolete-function-alias 'org-get-legal-level
  4902. 'org-get-valid-level "23.1")))
  4903. (defun org-promote ()
  4904. "Promote the current heading higher up the tree.
  4905. If the region is active in `transient-mark-mode', promote all headings
  4906. in the region."
  4907. (org-back-to-heading t)
  4908. (let* ((level (save-match-data (funcall outline-level)))
  4909. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4910. (diff (abs (- level (length up-head) -1))))
  4911. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4912. (replace-match up-head nil t)
  4913. ;; Fixup tag positioning
  4914. (and org-auto-align-tags (org-set-tags nil t))
  4915. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4916. (defun org-demote ()
  4917. "Demote the current heading lower down the tree.
  4918. If the region is active in `transient-mark-mode', demote all headings
  4919. in the region."
  4920. (org-back-to-heading t)
  4921. (let* ((level (save-match-data (funcall outline-level)))
  4922. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4923. (diff (abs (- level (length down-head) -1))))
  4924. (replace-match down-head nil t)
  4925. ;; Fixup tag positioning
  4926. (and org-auto-align-tags (org-set-tags nil t))
  4927. (if org-adapt-indentation (org-fixup-indentation diff))))
  4928. (defun org-map-tree (fun)
  4929. "Call FUN for every heading underneath the current one."
  4930. (org-back-to-heading)
  4931. (let ((level (funcall outline-level)))
  4932. (save-excursion
  4933. (funcall fun)
  4934. (while (and (progn
  4935. (outline-next-heading)
  4936. (> (funcall outline-level) level))
  4937. (not (eobp)))
  4938. (funcall fun)))))
  4939. (defun org-map-region (fun beg end)
  4940. "Call FUN for every heading between BEG and END."
  4941. (let ((org-ignore-region t))
  4942. (save-excursion
  4943. (setq end (copy-marker end))
  4944. (goto-char beg)
  4945. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4946. (< (point) end))
  4947. (funcall fun))
  4948. (while (and (progn
  4949. (outline-next-heading)
  4950. (< (point) end))
  4951. (not (eobp)))
  4952. (funcall fun)))))
  4953. (defun org-fixup-indentation (diff)
  4954. "Change the indentation in the current entry by DIFF
  4955. However, if any line in the current entry has no indentation, or if it
  4956. would end up with no indentation after the change, nothing at all is done."
  4957. (save-excursion
  4958. (let ((end (save-excursion (outline-next-heading)
  4959. (point-marker)))
  4960. (prohibit (if (> diff 0)
  4961. "^\\S-"
  4962. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4963. col)
  4964. (unless (save-excursion (end-of-line 1)
  4965. (re-search-forward prohibit end t))
  4966. (while (and (< (point) end)
  4967. (re-search-forward "^[ \t]+" end t))
  4968. (goto-char (match-end 0))
  4969. (setq col (current-column))
  4970. (if (< diff 0) (replace-match ""))
  4971. (org-indent-to-column (+ diff col))))
  4972. (move-marker end nil))))
  4973. (defun org-convert-to-odd-levels ()
  4974. "Convert an org-mode file with all levels allowed to one with odd levels.
  4975. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4976. level 5 etc."
  4977. (interactive)
  4978. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4979. (let ((org-odd-levels-only nil) n)
  4980. (save-excursion
  4981. (goto-char (point-min))
  4982. (while (re-search-forward "^\\*\\*+ " nil t)
  4983. (setq n (- (length (match-string 0)) 2))
  4984. (while (>= (setq n (1- n)) 0)
  4985. (org-demote))
  4986. (end-of-line 1))))))
  4987. (defun org-convert-to-oddeven-levels ()
  4988. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4989. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4990. section with an even level, conversion would destroy the structure of the file. An error
  4991. is signaled in this case."
  4992. (interactive)
  4993. (goto-char (point-min))
  4994. ;; First check if there are no even levels
  4995. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4996. (org-show-context t)
  4997. (error "Not all levels are odd in this file. Conversion not possible."))
  4998. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4999. (let ((org-odd-levels-only nil) n)
  5000. (save-excursion
  5001. (goto-char (point-min))
  5002. (while (re-search-forward "^\\*\\*+ " nil t)
  5003. (setq n (/ (1- (length (match-string 0))) 2))
  5004. (while (>= (setq n (1- n)) 0)
  5005. (org-promote))
  5006. (end-of-line 1))))))
  5007. (defun org-tr-level (n)
  5008. "Make N odd if required."
  5009. (if org-odd-levels-only (1+ (/ n 2)) n))
  5010. ;;; Vertical tree motion, cutting and pasting of subtrees
  5011. (defun org-move-subtree-up (&optional arg)
  5012. "Move the current subtree up past ARG headlines of the same level."
  5013. (interactive "p")
  5014. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5015. (defun org-move-subtree-down (&optional arg)
  5016. "Move the current subtree down past ARG headlines of the same level."
  5017. (interactive "p")
  5018. (setq arg (prefix-numeric-value arg))
  5019. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5020. 'outline-get-last-sibling))
  5021. (ins-point (make-marker))
  5022. (cnt (abs arg))
  5023. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5024. ;; Select the tree
  5025. (org-back-to-heading)
  5026. (setq beg0 (point))
  5027. (save-excursion
  5028. (setq ne-beg (org-back-over-empty-lines))
  5029. (setq beg (point)))
  5030. (save-match-data
  5031. (save-excursion (outline-end-of-heading)
  5032. (setq folded (org-invisible-p)))
  5033. (outline-end-of-subtree))
  5034. (outline-next-heading)
  5035. (setq ne-end (org-back-over-empty-lines))
  5036. (setq end (point))
  5037. (goto-char beg0)
  5038. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5039. ;; include less whitespace
  5040. (save-excursion
  5041. (goto-char beg)
  5042. (forward-line (- ne-beg ne-end))
  5043. (setq beg (point))))
  5044. ;; Find insertion point, with error handling
  5045. (while (> cnt 0)
  5046. (or (and (funcall movfunc) (looking-at outline-regexp))
  5047. (progn (goto-char beg0)
  5048. (error "Cannot move past superior level or buffer limit")))
  5049. (setq cnt (1- cnt)))
  5050. (if (> arg 0)
  5051. ;; Moving forward - still need to move over subtree
  5052. (progn (org-end-of-subtree t t)
  5053. (save-excursion
  5054. (org-back-over-empty-lines)
  5055. (or (bolp) (newline)))))
  5056. (setq ne-ins (org-back-over-empty-lines))
  5057. (move-marker ins-point (point))
  5058. (setq txt (buffer-substring beg end))
  5059. (org-save-markers-in-region beg end)
  5060. (delete-region beg end)
  5061. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5062. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5063. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5064. (let ((bbb (point)))
  5065. (insert-before-markers txt)
  5066. (org-reinstall-markers-in-region bbb)
  5067. (move-marker ins-point bbb))
  5068. (or (bolp) (insert "\n"))
  5069. (setq ins-end (point))
  5070. (goto-char ins-point)
  5071. (org-skip-whitespace)
  5072. (when (and (< arg 0)
  5073. (org-first-sibling-p)
  5074. (> ne-ins ne-beg))
  5075. ;; Move whitespace back to beginning
  5076. (save-excursion
  5077. (goto-char ins-end)
  5078. (let ((kill-whole-line t))
  5079. (kill-line (- ne-ins ne-beg)) (point)))
  5080. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5081. (move-marker ins-point nil)
  5082. (org-compact-display-after-subtree-move)
  5083. (org-show-empty-lines-in-parent)
  5084. (unless folded
  5085. (org-show-entry)
  5086. (show-children)
  5087. (org-cycle-hide-drawers 'children))))
  5088. (defvar org-subtree-clip ""
  5089. "Clipboard for cut and paste of subtrees.
  5090. This is actually only a copy of the kill, because we use the normal kill
  5091. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5092. (defvar org-subtree-clip-folded nil
  5093. "Was the last copied subtree folded?
  5094. This is used to fold the tree back after pasting.")
  5095. (defun org-cut-subtree (&optional n)
  5096. "Cut the current subtree into the clipboard.
  5097. With prefix arg N, cut this many sequential subtrees.
  5098. This is a short-hand for marking the subtree and then cutting it."
  5099. (interactive "p")
  5100. (org-copy-subtree n 'cut))
  5101. (defun org-copy-subtree (&optional n cut force-store-markers)
  5102. "Cut the current subtree into the clipboard.
  5103. With prefix arg N, cut this many sequential subtrees.
  5104. This is a short-hand for marking the subtree and then copying it.
  5105. If CUT is non-nil, actually cut the subtree.
  5106. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5107. of some markers in the region, even if CUT is non-nil. This is
  5108. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5109. (interactive "p")
  5110. (let (beg end folded (beg0 (point)))
  5111. (if (interactive-p)
  5112. (org-back-to-heading nil) ; take what looks like a subtree
  5113. (org-back-to-heading t)) ; take what is really there
  5114. (org-back-over-empty-lines)
  5115. (setq beg (point))
  5116. (skip-chars-forward " \t\r\n")
  5117. (save-match-data
  5118. (save-excursion (outline-end-of-heading)
  5119. (setq folded (org-invisible-p)))
  5120. (condition-case nil
  5121. (outline-forward-same-level (1- n))
  5122. (error nil))
  5123. (org-end-of-subtree t t))
  5124. (org-back-over-empty-lines)
  5125. (setq end (point))
  5126. (goto-char beg0)
  5127. (when (> end beg)
  5128. (setq org-subtree-clip-folded folded)
  5129. (when (or cut force-store-markers)
  5130. (org-save-markers-in-region beg end))
  5131. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5132. (setq org-subtree-clip (current-kill 0))
  5133. (message "%s: Subtree(s) with %d characters"
  5134. (if cut "Cut" "Copied")
  5135. (length org-subtree-clip)))))
  5136. (defun org-paste-subtree (&optional level tree for-yank)
  5137. "Paste the clipboard as a subtree, with modification of headline level.
  5138. The entire subtree is promoted or demoted in order to match a new headline
  5139. level.
  5140. If the cursor is at the beginning of a headline, the same level as
  5141. that headline is used to paste the tree
  5142. If not, the new level is derived from the *visible* headings
  5143. before and after the insertion point, and taken to be the inferior headline
  5144. level of the two. So if the previous visible heading is level 3 and the
  5145. next is level 4 (or vice versa), level 4 will be used for insertion.
  5146. This makes sure that the subtree remains an independent subtree and does
  5147. not swallow low level entries.
  5148. You can also force a different level, either by using a numeric prefix
  5149. argument, or by inserting the heading marker by hand. For example, if the
  5150. cursor is after \"*****\", then the tree will be shifted to level 5.
  5151. If optional TREE is given, use this text instead of the kill ring.
  5152. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5153. move back over whitespace before inserting, and move point to the end of
  5154. the inserted text when done."
  5155. (interactive "P")
  5156. (unless (org-kill-is-subtree-p tree)
  5157. (error "%s"
  5158. (substitute-command-keys
  5159. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5160. (let* ((visp (not (org-invisible-p)))
  5161. (txt (or tree (and kill-ring (current-kill 0))))
  5162. (^re (concat "^\\(" outline-regexp "\\)"))
  5163. (re (concat "\\(" outline-regexp "\\)"))
  5164. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5165. (old-level (if (string-match ^re txt)
  5166. (- (match-end 0) (match-beginning 0) 1)
  5167. -1))
  5168. (force-level (cond (level (prefix-numeric-value level))
  5169. ((and (looking-at "[ \t]*$")
  5170. (string-match
  5171. ^re_ (buffer-substring
  5172. (point-at-bol) (point))))
  5173. (- (match-end 1) (match-beginning 1)))
  5174. ((and (bolp)
  5175. (looking-at org-outline-regexp))
  5176. (- (match-end 0) (point) 1))
  5177. (t nil)))
  5178. (previous-level (save-excursion
  5179. (condition-case nil
  5180. (progn
  5181. (outline-previous-visible-heading 1)
  5182. (if (looking-at re)
  5183. (- (match-end 0) (match-beginning 0) 1)
  5184. 1))
  5185. (error 1))))
  5186. (next-level (save-excursion
  5187. (condition-case nil
  5188. (progn
  5189. (or (looking-at outline-regexp)
  5190. (outline-next-visible-heading 1))
  5191. (if (looking-at re)
  5192. (- (match-end 0) (match-beginning 0) 1)
  5193. 1))
  5194. (error 1))))
  5195. (new-level (or force-level (max previous-level next-level)))
  5196. (shift (if (or (= old-level -1)
  5197. (= new-level -1)
  5198. (= old-level new-level))
  5199. 0
  5200. (- new-level old-level)))
  5201. (delta (if (> shift 0) -1 1))
  5202. (func (if (> shift 0) 'org-demote 'org-promote))
  5203. (org-odd-levels-only nil)
  5204. beg end newend)
  5205. ;; Remove the forced level indicator
  5206. (if force-level
  5207. (delete-region (point-at-bol) (point)))
  5208. ;; Paste
  5209. (beginning-of-line 1)
  5210. (unless for-yank (org-back-over-empty-lines))
  5211. (setq beg (point))
  5212. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5213. (insert-before-markers txt)
  5214. (unless (string-match "\n\\'" txt) (insert "\n"))
  5215. (setq newend (point))
  5216. (org-reinstall-markers-in-region beg)
  5217. (setq end (point))
  5218. (goto-char beg)
  5219. (skip-chars-forward " \t\n\r")
  5220. (setq beg (point))
  5221. (if (and (org-invisible-p) visp)
  5222. (save-excursion (outline-show-heading)))
  5223. ;; Shift if necessary
  5224. (unless (= shift 0)
  5225. (save-restriction
  5226. (narrow-to-region beg end)
  5227. (while (not (= shift 0))
  5228. (org-map-region func (point-min) (point-max))
  5229. (setq shift (+ delta shift)))
  5230. (goto-char (point-min))
  5231. (setq newend (point-max))))
  5232. (when (or (interactive-p) for-yank)
  5233. (message "Clipboard pasted as level %d subtree" new-level))
  5234. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5235. kill-ring
  5236. (eq org-subtree-clip (current-kill 0))
  5237. org-subtree-clip-folded)
  5238. ;; The tree was folded before it was killed/copied
  5239. (hide-subtree))
  5240. (and for-yank (goto-char newend))))
  5241. (defun org-kill-is-subtree-p (&optional txt)
  5242. "Check if the current kill is an outline subtree, or a set of trees.
  5243. Returns nil if kill does not start with a headline, or if the first
  5244. headline level is not the largest headline level in the tree.
  5245. So this will actually accept several entries of equal levels as well,
  5246. which is OK for `org-paste-subtree'.
  5247. If optional TXT is given, check this string instead of the current kill."
  5248. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5249. (start-level (and kill
  5250. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5251. org-outline-regexp "\\)")
  5252. kill)
  5253. (- (match-end 2) (match-beginning 2) 1)))
  5254. (re (concat "^" org-outline-regexp))
  5255. (start (1+ (or (match-beginning 2) -1))))
  5256. (if (not start-level)
  5257. (progn
  5258. nil) ;; does not even start with a heading
  5259. (catch 'exit
  5260. (while (setq start (string-match re kill (1+ start)))
  5261. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5262. (throw 'exit nil)))
  5263. t))))
  5264. (defvar org-markers-to-move nil
  5265. "Markers that should be moved with a cut-and-paste operation.
  5266. Those markers are stored together with their positions relative to
  5267. the start of the region.")
  5268. (defun org-save-markers-in-region (beg end)
  5269. "Check markers in region.
  5270. If these markers are between BEG and END, record their position relative
  5271. to BEG, so that after moving the block of text, we can put the markers back
  5272. into place.
  5273. This function gets called just before an entry or tree gets cut from the
  5274. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5275. called immediately, to move the markers with the entries."
  5276. (setq org-markers-to-move nil)
  5277. (when (featurep 'org-clock)
  5278. (org-clock-save-markers-for-cut-and-paste beg end))
  5279. (when (featurep 'org-agenda)
  5280. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5281. (defun org-check-and-save-marker (marker beg end)
  5282. "Check if MARKER is between BEG and END.
  5283. If yes, remember the marker and the distance to BEG."
  5284. (when (and (marker-buffer marker)
  5285. (equal (marker-buffer marker) (current-buffer)))
  5286. (if (and (>= marker beg) (< marker end))
  5287. (push (cons marker (- marker beg)) org-markers-to-move))))
  5288. (defun org-reinstall-markers-in-region (beg)
  5289. "Move all remembered markers to their position relative to BEG."
  5290. (mapc (lambda (x)
  5291. (move-marker (car x) (+ beg (cdr x))))
  5292. org-markers-to-move)
  5293. (setq org-markers-to-move nil))
  5294. (defun org-narrow-to-subtree ()
  5295. "Narrow buffer to the current subtree."
  5296. (interactive)
  5297. (save-excursion
  5298. (save-match-data
  5299. (narrow-to-region
  5300. (progn (org-back-to-heading) (point))
  5301. (progn (org-end-of-subtree t) (point))))))
  5302. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5303. "Clone the task (subtree) at point N times.
  5304. The clones will be inserted as siblings.
  5305. In interactive use, the user will be prompted for the number of clones
  5306. to be produced, and for a time SHIFT, which may be a repeater as used
  5307. in time stamps, for example `+3d'.
  5308. When a valid repeater is given and the entry contains any time stamps,
  5309. the clones will become a sequence in time, with time stamps in the
  5310. subtree shifted for each clone produced. If SHIFT is nil or the
  5311. empty string, time stamps will be left alone.
  5312. If the original subtree did contain time stamps with a repeater,
  5313. the following will happen:
  5314. - the repeater will be removed in each clone
  5315. - an additional clone will be produced, with the current, unshifted
  5316. date(s) in the entry.
  5317. - the original entry will be placed *after* all the clones, with
  5318. repeater intact.
  5319. - the start days in the repeater in the original entry will be shifted
  5320. to past the last clone.
  5321. I this way you can spell out a number of instances of a repeating task,
  5322. and still retain the repeater to cover future instances of the task."
  5323. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5324. (let (beg end template task
  5325. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5326. (if (not (and (integerp n) (> n 0)))
  5327. (error "Invalid number of replications %s" n))
  5328. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5329. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5330. shift)))
  5331. (error "Invalid shift specification %s" shift))
  5332. (when doshift
  5333. (setq shift-n (string-to-number (match-string 1 shift))
  5334. shift-what (cdr (assoc (match-string 2 shift)
  5335. '(("d" . day) ("w" . week)
  5336. ("m" . month) ("y" . year))))))
  5337. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5338. (setq nmin 1 nmax n)
  5339. (org-back-to-heading t)
  5340. (setq beg (point))
  5341. (org-end-of-subtree t t)
  5342. (setq end (point))
  5343. (setq template (buffer-substring beg end))
  5344. (when (and doshift
  5345. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5346. (delete-region beg end)
  5347. (setq end beg)
  5348. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5349. (goto-char end)
  5350. (loop for n from nmin to nmax do
  5351. (if (not doshift)
  5352. (setq task template)
  5353. (with-temp-buffer
  5354. (insert template)
  5355. (org-mode)
  5356. (goto-char (point-min))
  5357. (while (re-search-forward org-ts-regexp nil t)
  5358. (org-timestamp-change (* n shift-n) shift-what))
  5359. (unless (= n n-no-remove)
  5360. (goto-char (point-min))
  5361. (while (re-search-forward org-ts-regexp nil t)
  5362. (save-excursion
  5363. (goto-char (match-beginning 0))
  5364. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5365. (delete-region (match-beginning 1) (match-end 1))))))
  5366. (setq task (buffer-string))))
  5367. (insert task))
  5368. (goto-char beg)))
  5369. ;;; Outline Sorting
  5370. (defun org-sort (with-case)
  5371. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5372. Optional argument WITH-CASE means sort case-sensitively.
  5373. With a double prefix argument, also remove duplicate entries."
  5374. (interactive "P")
  5375. (if (org-at-table-p)
  5376. (org-call-with-arg 'org-table-sort-lines with-case)
  5377. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5378. (defun org-sort-remove-invisible (s)
  5379. (remove-text-properties 0 (length s) org-rm-props s)
  5380. (while (string-match org-bracket-link-regexp s)
  5381. (setq s (replace-match (if (match-end 2)
  5382. (match-string 3 s)
  5383. (match-string 1 s)) t t s)))
  5384. s)
  5385. (defvar org-priority-regexp) ; defined later in the file
  5386. (defun org-sort-entries-or-items
  5387. (&optional with-case sorting-type getkey-func compare-func property)
  5388. "Sort entries on a certain level of an outline tree.
  5389. If there is an active region, the entries in the region are sorted.
  5390. Else, if the cursor is before the first entry, sort the top-level items.
  5391. Else, the children of the entry at point are sorted.
  5392. Sorting can be alphabetically, numerically, and by date/time as given by
  5393. the first time stamp in the entry. The command prompts for the sorting
  5394. type unless it has been given to the function through the SORTING-TYPE
  5395. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5396. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5397. called with point at the beginning of the record. It must return either
  5398. a string or a number that should serve as the sorting key for that record.
  5399. Comparing entries ignores case by default. However, with an optional argument
  5400. WITH-CASE, the sorting considers case as well."
  5401. (interactive "P")
  5402. (let ((case-func (if with-case 'identity 'downcase))
  5403. start beg end stars re re2
  5404. txt what tmp plain-list-p)
  5405. ;; Find beginning and end of region to sort
  5406. (cond
  5407. ((org-region-active-p)
  5408. ;; we will sort the region
  5409. (setq end (region-end)
  5410. what "region")
  5411. (goto-char (region-beginning))
  5412. (if (not (org-on-heading-p)) (outline-next-heading))
  5413. (setq start (point)))
  5414. ((org-at-item-p)
  5415. ;; we will sort this plain list
  5416. (org-beginning-of-item-list) (setq start (point))
  5417. (org-end-of-item-list) (setq end (point))
  5418. (goto-char start)
  5419. (setq plain-list-p t
  5420. what "plain list"))
  5421. ((or (org-on-heading-p)
  5422. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5423. ;; we will sort the children of the current headline
  5424. (org-back-to-heading)
  5425. (setq start (point)
  5426. end (progn (org-end-of-subtree t t)
  5427. (org-back-over-empty-lines)
  5428. (point))
  5429. what "children")
  5430. (goto-char start)
  5431. (show-subtree)
  5432. (outline-next-heading))
  5433. (t
  5434. ;; we will sort the top-level entries in this file
  5435. (goto-char (point-min))
  5436. (or (org-on-heading-p) (outline-next-heading))
  5437. (setq start (point) end (point-max) what "top-level")
  5438. (goto-char start)
  5439. (show-all)))
  5440. (setq beg (point))
  5441. (if (>= beg end) (error "Nothing to sort"))
  5442. (unless plain-list-p
  5443. (looking-at "\\(\\*+\\)")
  5444. (setq stars (match-string 1)
  5445. re (concat "^" (regexp-quote stars) " +")
  5446. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5447. txt (buffer-substring beg end))
  5448. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5449. (if (and (not (equal stars "*")) (string-match re2 txt))
  5450. (error "Region to sort contains a level above the first entry")))
  5451. (unless sorting-type
  5452. (message
  5453. (if plain-list-p
  5454. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5455. "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:")
  5456. what)
  5457. (setq sorting-type (read-char-exclusive))
  5458. (and (= (downcase sorting-type) ?f)
  5459. (setq getkey-func
  5460. (org-ido-completing-read "Sort using function: "
  5461. obarray 'fboundp t nil nil))
  5462. (setq getkey-func (intern getkey-func)))
  5463. (and (= (downcase sorting-type) ?r)
  5464. (setq property
  5465. (org-ido-completing-read "Property: "
  5466. (mapcar 'list (org-buffer-property-keys t))
  5467. nil t))))
  5468. (message "Sorting entries...")
  5469. (save-restriction
  5470. (narrow-to-region start end)
  5471. (let ((dcst (downcase sorting-type))
  5472. (case-fold-search nil)
  5473. (now (current-time)))
  5474. (sort-subr
  5475. (/= dcst sorting-type)
  5476. ;; This function moves to the beginning character of the "record" to
  5477. ;; be sorted.
  5478. (if plain-list-p
  5479. (lambda nil
  5480. (if (org-at-item-p) t (goto-char (point-max))))
  5481. (lambda nil
  5482. (if (re-search-forward re nil t)
  5483. (goto-char (match-beginning 0))
  5484. (goto-char (point-max)))))
  5485. ;; This function moves to the last character of the "record" being
  5486. ;; sorted.
  5487. (if plain-list-p
  5488. 'org-end-of-item
  5489. (lambda nil
  5490. (save-match-data
  5491. (condition-case nil
  5492. (outline-forward-same-level 1)
  5493. (error
  5494. (goto-char (point-max)))))))
  5495. ;; This function returns the value that gets sorted against.
  5496. (if plain-list-p
  5497. (lambda nil
  5498. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5499. (cond
  5500. ((= dcst ?n)
  5501. (string-to-number (buffer-substring (match-end 0)
  5502. (point-at-eol))))
  5503. ((= dcst ?a)
  5504. (buffer-substring (match-end 0) (point-at-eol)))
  5505. ((= dcst ?t)
  5506. (if (re-search-forward org-ts-regexp
  5507. (point-at-eol) t)
  5508. (org-time-string-to-time (match-string 0))
  5509. now))
  5510. ((= dcst ?f)
  5511. (if getkey-func
  5512. (progn
  5513. (setq tmp (funcall getkey-func))
  5514. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5515. tmp)
  5516. (error "Invalid key function `%s'" getkey-func)))
  5517. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5518. (lambda nil
  5519. (cond
  5520. ((= dcst ?n)
  5521. (if (looking-at org-complex-heading-regexp)
  5522. (string-to-number (match-string 4))
  5523. nil))
  5524. ((= dcst ?a)
  5525. (if (looking-at org-complex-heading-regexp)
  5526. (funcall case-func (match-string 4))
  5527. nil))
  5528. ((= dcst ?t)
  5529. (if (re-search-forward org-ts-regexp
  5530. (save-excursion
  5531. (forward-line 2)
  5532. (point)) t)
  5533. (org-time-string-to-time (match-string 0))
  5534. now))
  5535. ((= dcst ?p)
  5536. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5537. (string-to-char (match-string 2))
  5538. org-default-priority))
  5539. ((= dcst ?r)
  5540. (or (org-entry-get nil property) ""))
  5541. ((= dcst ?o)
  5542. (if (looking-at org-complex-heading-regexp)
  5543. (- 9999 (length (member (match-string 2)
  5544. org-todo-keywords-1)))))
  5545. ((= dcst ?f)
  5546. (if getkey-func
  5547. (progn
  5548. (setq tmp (funcall getkey-func))
  5549. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5550. tmp)
  5551. (error "Invalid key function `%s'" getkey-func)))
  5552. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5553. nil
  5554. (cond
  5555. ((= dcst ?a) 'string<)
  5556. ((= dcst ?t) 'time-less-p)
  5557. ((= dcst ?f) compare-func)
  5558. (t nil)))))
  5559. (message "Sorting entries...done")))
  5560. (defun org-do-sort (table what &optional with-case sorting-type)
  5561. "Sort TABLE of WHAT according to SORTING-TYPE.
  5562. The user will be prompted for the SORTING-TYPE if the call to this
  5563. function does not specify it. WHAT is only for the prompt, to indicate
  5564. what is being sorted. The sorting key will be extracted from
  5565. the car of the elements of the table.
  5566. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5567. (unless sorting-type
  5568. (message
  5569. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5570. what)
  5571. (setq sorting-type (read-char-exclusive)))
  5572. (let ((dcst (downcase sorting-type))
  5573. extractfun comparefun)
  5574. ;; Define the appropriate functions
  5575. (cond
  5576. ((= dcst ?n)
  5577. (setq extractfun 'string-to-number
  5578. comparefun (if (= dcst sorting-type) '< '>)))
  5579. ((= dcst ?a)
  5580. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5581. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5582. comparefun (if (= dcst sorting-type)
  5583. 'string<
  5584. (lambda (a b) (and (not (string< a b))
  5585. (not (string= a b)))))))
  5586. ((= dcst ?t)
  5587. (setq extractfun
  5588. (lambda (x)
  5589. (if (or (string-match org-ts-regexp x)
  5590. (string-match org-ts-regexp-both x))
  5591. (time-to-seconds
  5592. (org-time-string-to-time (match-string 0 x)))
  5593. 0))
  5594. comparefun (if (= dcst sorting-type) '< '>)))
  5595. (t (error "Invalid sorting type `%c'" sorting-type)))
  5596. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5597. table)
  5598. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5599. ;;; Editing source examples
  5600. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5601. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5602. (defvar org-edit-src-force-single-line nil)
  5603. (defvar org-edit-src-from-org-mode nil)
  5604. (defvar org-edit-src-picture nil)
  5605. (define-minor-mode org-exit-edit-mode
  5606. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5607. (defun org-edit-src-code ()
  5608. "Edit the source code example at point.
  5609. An indirect buffer is created, and that buffer is then narrowed to the
  5610. example at point and switched to the correct language mode. When done,
  5611. exit by killing the buffer with \\[org-edit-src-exit]."
  5612. (interactive)
  5613. (let ((line (org-current-line))
  5614. (case-fold-search t)
  5615. (msg (substitute-command-keys
  5616. "Edit, then exit with C-c ' (C-c and single quote)"))
  5617. (info (org-edit-src-find-region-and-lang))
  5618. (org-mode-p (eq major-mode 'org-mode))
  5619. beg end lang lang-f single lfmt)
  5620. (if (not info)
  5621. nil
  5622. (setq beg (nth 0 info)
  5623. end (nth 1 info)
  5624. lang (nth 2 info)
  5625. single (nth 3 info)
  5626. lfmt (nth 4 info)
  5627. lang-f (intern (concat lang "-mode")))
  5628. (unless (functionp lang-f)
  5629. (error "No such language mode: %s" lang-f))
  5630. (goto-line line)
  5631. (if (get-buffer "*Org Edit Src Example*")
  5632. (kill-buffer "*Org Edit Src Example*"))
  5633. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5634. "*Org Edit Src Example*"))
  5635. (narrow-to-region beg end)
  5636. (remove-text-properties beg end '(display nil invisible nil
  5637. intangible nil))
  5638. (let ((org-inhibit-startup t))
  5639. (funcall lang-f))
  5640. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5641. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5642. (when lfmt
  5643. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5644. (when org-mode-p
  5645. (goto-char (point-min))
  5646. (while (re-search-forward "^," nil t)
  5647. (replace-match "")))
  5648. (goto-line line)
  5649. (org-exit-edit-mode)
  5650. (org-set-local 'header-line-format msg)
  5651. (message "%s" msg)
  5652. t)))
  5653. (defun org-edit-fixed-width-region ()
  5654. "Edit the fixed-width ascii drawing at point.
  5655. This must be a region where each line starts with a colon followed by
  5656. a space character.
  5657. An indirect buffer is created, and that buffer is then narrowed to the
  5658. example at point and switched to artist-mode. When done,
  5659. exit by killing the buffer with \\[org-edit-src-exit]."
  5660. (interactive)
  5661. (let ((line (org-current-line))
  5662. (case-fold-search t)
  5663. (msg (substitute-command-keys
  5664. "Edit, then exit with C-c ' (C-c and single quote)"))
  5665. (org-mode-p (eq major-mode 'org-mode))
  5666. beg end)
  5667. (beginning-of-line 1)
  5668. (if (looking-at "[ \t]*[^:\n \t]")
  5669. nil
  5670. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5671. (setq beg (point) end beg)
  5672. (save-excursion
  5673. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5674. (setq beg (point-at-bol 2))
  5675. (setq beg (point))))
  5676. (save-excursion
  5677. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5678. (setq end (1- (match-beginning 0)))
  5679. (setq end (point))))
  5680. (goto-line line))
  5681. (if (get-buffer "*Org Edit Picture*")
  5682. (kill-buffer "*Org Edit Picture*"))
  5683. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5684. "*Org Edit Picture*"))
  5685. (narrow-to-region beg end)
  5686. (remove-text-properties beg end '(display nil invisible nil
  5687. intangible nil))
  5688. (when (fboundp 'font-lock-unfontify-region)
  5689. (font-lock-unfontify-region (point-min) (point-max)))
  5690. (cond
  5691. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5692. (fundamental-mode)
  5693. (artist-mode 1))
  5694. (t (funcall org-edit-fixed-width-region-mode)))
  5695. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5696. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5697. (set (make-local-variable 'org-edit-src-picture) t)
  5698. (goto-char (point-min))
  5699. (while (re-search-forward "^[ \t]*: ?" nil t)
  5700. (replace-match ""))
  5701. (goto-line line)
  5702. (org-exit-edit-mode)
  5703. (org-set-local 'header-line-format msg)
  5704. (message "%s" msg)
  5705. t)))
  5706. (defun org-edit-src-find-region-and-lang ()
  5707. "Find the region and language for a local edit.
  5708. Return a list with beginning and end of the region, a string representing
  5709. the language, a switch telling of the content should be in a single line."
  5710. (let ((re-list
  5711. (append
  5712. org-edit-src-region-extra
  5713. '(
  5714. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5715. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5716. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5717. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5718. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5719. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5720. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5721. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5722. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5723. ("^#\\+html:" "\n" "html" single-line)
  5724. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5725. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5726. ("^#\\+latex:" "\n" "latex" single-line)
  5727. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5728. ("^#\\+ascii:" "\n" "ascii" single-line)
  5729. )))
  5730. (pos (point))
  5731. re1 re2 single beg end lang lfmt match-re1)
  5732. (catch 'exit
  5733. (while (setq entry (pop re-list))
  5734. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5735. single (nth 3 entry))
  5736. (save-excursion
  5737. (if (or (looking-at re1)
  5738. (re-search-backward re1 nil t))
  5739. (progn
  5740. (setq match-re1 (match-string 0))
  5741. (setq beg (match-end 0)
  5742. lang (org-edit-src-get-lang lang)
  5743. lfmt (org-edit-src-get-label-format match-re1))
  5744. (if (and (re-search-forward re2 nil t)
  5745. (>= (match-end 0) pos))
  5746. (throw 'exit (list beg (match-beginning 0)
  5747. lang single lfmt))))
  5748. (if (or (looking-at re2)
  5749. (re-search-forward re2 nil t))
  5750. (progn
  5751. (setq end (match-beginning 0))
  5752. (if (and (re-search-backward re1 nil t)
  5753. (<= (match-beginning 0) pos))
  5754. (progn
  5755. (setq lfmt (org-edit-src-get-label-format
  5756. (match-string 0)))
  5757. (throw 'exit
  5758. (list (match-end 0) end
  5759. (org-edit-src-get-lang lang)
  5760. single lfmt))))))))))))
  5761. (defun org-edit-src-get-lang (lang)
  5762. "Extract the src language."
  5763. (let ((m (match-string 0)))
  5764. (cond
  5765. ((stringp lang) lang)
  5766. ((integerp lang) (match-string lang))
  5767. ((and (eq lang 'lang)
  5768. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5769. (match-string 1 m))
  5770. ((and (eq lang 'style)
  5771. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5772. (match-string 1 m))
  5773. (t "fundamental"))))
  5774. (defun org-edit-src-get-label-format (s)
  5775. "Extract the label format."
  5776. (save-match-data
  5777. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5778. (match-string 1 s))))
  5779. (defun org-edit-src-exit ()
  5780. "Exit special edit and protect problematic lines."
  5781. (interactive)
  5782. (unless (buffer-base-buffer (current-buffer))
  5783. (error "This is not an indirect buffer, something is wrong..."))
  5784. (unless (> (point-min) 1)
  5785. (error "This buffer is not narrowed, something is wrong..."))
  5786. (goto-char (point-min))
  5787. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5788. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5789. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5790. (goto-char (point-min))
  5791. (while (re-search-forward "\n" nil t)
  5792. (replace-match " "))
  5793. (goto-char (point-min))
  5794. (if (looking-at "\\s-*") (replace-match " "))
  5795. (if (re-search-forward "\\s-+\\'" nil t)
  5796. (replace-match "")))
  5797. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5798. (goto-char (point-min))
  5799. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5800. (replace-match ",\\1"))
  5801. (when font-lock-mode
  5802. (font-lock-unfontify-region (point-min) (point-max)))
  5803. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5804. (when (org-bound-and-true-p org-edit-src-picture)
  5805. (untabify (point-min) (point-max))
  5806. (goto-char (point-min))
  5807. (while (re-search-forward "^" nil t)
  5808. (replace-match ": "))
  5809. (when font-lock-mode
  5810. (font-lock-unfontify-region (point-min) (point-max)))
  5811. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5812. (kill-buffer (current-buffer))
  5813. (and (org-mode-p) (org-restart-font-lock)))
  5814. ;;; The orgstruct minor mode
  5815. ;; Define a minor mode which can be used in other modes in order to
  5816. ;; integrate the org-mode structure editing commands.
  5817. ;; This is really a hack, because the org-mode structure commands use
  5818. ;; keys which normally belong to the major mode. Here is how it
  5819. ;; works: The minor mode defines all the keys necessary to operate the
  5820. ;; structure commands, but wraps the commands into a function which
  5821. ;; tests if the cursor is currently at a headline or a plain list
  5822. ;; item. If that is the case, the structure command is used,
  5823. ;; temporarily setting many Org-mode variables like regular
  5824. ;; expressions for filling etc. However, when any of those keys is
  5825. ;; used at a different location, function uses `key-binding' to look
  5826. ;; up if the key has an associated command in another currently active
  5827. ;; keymap (minor modes, major mode, global), and executes that
  5828. ;; command. There might be problems if any of the keys is otherwise
  5829. ;; used as a prefix key.
  5830. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5831. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5832. ;; addresses this by checking explicitly for both bindings.
  5833. (defvar orgstruct-mode-map (make-sparse-keymap)
  5834. "Keymap for the minor `orgstruct-mode'.")
  5835. (defvar org-local-vars nil
  5836. "List of local variables, for use by `orgstruct-mode'")
  5837. ;;;###autoload
  5838. (define-minor-mode orgstruct-mode
  5839. "Toggle the minor more `orgstruct-mode'.
  5840. This mode is for using Org-mode structure commands in other modes.
  5841. The following key behave as if Org-mode was active, if the cursor
  5842. is on a headline, or on a plain list item (both in the definition
  5843. of Org-mode).
  5844. M-up Move entry/item up
  5845. M-down Move entry/item down
  5846. M-left Promote
  5847. M-right Demote
  5848. M-S-up Move entry/item up
  5849. M-S-down Move entry/item down
  5850. M-S-left Promote subtree
  5851. M-S-right Demote subtree
  5852. M-q Fill paragraph and items like in Org-mode
  5853. C-c ^ Sort entries
  5854. C-c - Cycle list bullet
  5855. TAB Cycle item visibility
  5856. M-RET Insert new heading/item
  5857. S-M-RET Insert new TODO heading / Checkbox item
  5858. C-c C-c Set tags / toggle checkbox"
  5859. nil " OrgStruct" nil
  5860. (org-load-modules-maybe)
  5861. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5862. ;;;###autoload
  5863. (defun turn-on-orgstruct ()
  5864. "Unconditionally turn on `orgstruct-mode'."
  5865. (orgstruct-mode 1))
  5866. (defun orgstruct++-mode (&optional arg)
  5867. "Toggle `orgstruct-mode', the enhanced version of it.
  5868. In addition to setting orgstruct-mode, this also exports all indentation and
  5869. autofilling variables from org-mode into the buffer. It will also
  5870. recognize item context in multiline items.
  5871. Note that turning off orgstruct-mode will *not* remove the
  5872. indentation/paragraph settings. This can only be done by refreshing the
  5873. major mode, for example with \[normal-mode]."
  5874. (interactive "P")
  5875. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5876. (if (< arg 1)
  5877. (orgstruct-mode -1)
  5878. (orgstruct-mode 1)
  5879. (let (var val)
  5880. (mapc
  5881. (lambda (x)
  5882. (when (string-match
  5883. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5884. (symbol-name (car x)))
  5885. (setq var (car x) val (nth 1 x))
  5886. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5887. org-local-vars)
  5888. (org-set-local 'orgstruct-is-++ t))))
  5889. (defvar orgstruct-is-++ nil
  5890. "Is orgstruct-mode in ++ version in the current-buffer?")
  5891. (make-variable-buffer-local 'orgstruct-is-++)
  5892. ;;;###autoload
  5893. (defun turn-on-orgstruct++ ()
  5894. "Unconditionally turn on `orgstruct++-mode'."
  5895. (orgstruct++-mode 1))
  5896. (defun orgstruct-error ()
  5897. "Error when there is no default binding for a structure key."
  5898. (interactive)
  5899. (error "This key has no function outside structure elements"))
  5900. (defun orgstruct-setup ()
  5901. "Setup orgstruct keymaps."
  5902. (let ((nfunc 0)
  5903. (bindings
  5904. (list
  5905. '([(meta up)] org-metaup)
  5906. '([(meta down)] org-metadown)
  5907. '([(meta left)] org-metaleft)
  5908. '([(meta right)] org-metaright)
  5909. '([(meta shift up)] org-shiftmetaup)
  5910. '([(meta shift down)] org-shiftmetadown)
  5911. '([(meta shift left)] org-shiftmetaleft)
  5912. '([(meta shift right)] org-shiftmetaright)
  5913. '([(shift up)] org-shiftup)
  5914. '([(shift down)] org-shiftdown)
  5915. '([(shift left)] org-shiftleft)
  5916. '([(shift right)] org-shiftright)
  5917. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5918. '("\M-q" fill-paragraph)
  5919. '("\C-c^" org-sort)
  5920. '("\C-c-" org-cycle-list-bullet)))
  5921. elt key fun cmd)
  5922. (while (setq elt (pop bindings))
  5923. (setq nfunc (1+ nfunc))
  5924. (setq key (org-key (car elt))
  5925. fun (nth 1 elt)
  5926. cmd (orgstruct-make-binding fun nfunc key))
  5927. (org-defkey orgstruct-mode-map key cmd))
  5928. ;; Special treatment needed for TAB and RET
  5929. (org-defkey orgstruct-mode-map [(tab)]
  5930. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5931. (org-defkey orgstruct-mode-map "\C-i"
  5932. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5933. (org-defkey orgstruct-mode-map "\M-\C-m"
  5934. (orgstruct-make-binding 'org-insert-heading 105
  5935. "\M-\C-m" [(meta return)]))
  5936. (org-defkey orgstruct-mode-map [(meta return)]
  5937. (orgstruct-make-binding 'org-insert-heading 106
  5938. [(meta return)] "\M-\C-m"))
  5939. (org-defkey orgstruct-mode-map [(shift meta return)]
  5940. (orgstruct-make-binding 'org-insert-todo-heading 107
  5941. [(meta return)] "\M-\C-m"))
  5942. (unless org-local-vars
  5943. (setq org-local-vars (org-get-local-variables)))
  5944. t))
  5945. (defun orgstruct-make-binding (fun n &rest keys)
  5946. "Create a function for binding in the structure minor mode.
  5947. FUN is the command to call inside a table. N is used to create a unique
  5948. command name. KEYS are keys that should be checked in for a command
  5949. to execute outside of tables."
  5950. (eval
  5951. (list 'defun
  5952. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5953. '(arg)
  5954. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5955. "Outside of structure, run the binding of `"
  5956. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5957. "'.")
  5958. '(interactive "p")
  5959. (list 'if
  5960. `(org-context-p 'headline 'item
  5961. (and orgstruct-is-++
  5962. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5963. 'item-body))
  5964. (list 'org-run-like-in-org-mode (list 'quote fun))
  5965. (list 'let '(orgstruct-mode)
  5966. (list 'call-interactively
  5967. (append '(or)
  5968. (mapcar (lambda (k)
  5969. (list 'key-binding k))
  5970. keys)
  5971. '('orgstruct-error))))))))
  5972. (defun org-context-p (&rest contexts)
  5973. "Check if local context is any of CONTEXTS.
  5974. Possible values in the list of contexts are `table', `headline', and `item'."
  5975. (let ((pos (point)))
  5976. (goto-char (point-at-bol))
  5977. (prog1 (or (and (memq 'table contexts)
  5978. (looking-at "[ \t]*|"))
  5979. (and (memq 'headline contexts)
  5980. ;;????????? (looking-at "\\*+"))
  5981. (looking-at outline-regexp))
  5982. (and (memq 'item contexts)
  5983. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5984. (and (memq 'item-body contexts)
  5985. (org-in-item-p)))
  5986. (goto-char pos))))
  5987. (defun org-get-local-variables ()
  5988. "Return a list of all local variables in an org-mode buffer."
  5989. (let (varlist)
  5990. (with-current-buffer (get-buffer-create "*Org tmp*")
  5991. (erase-buffer)
  5992. (org-mode)
  5993. (setq varlist (buffer-local-variables)))
  5994. (kill-buffer "*Org tmp*")
  5995. (delq nil
  5996. (mapcar
  5997. (lambda (x)
  5998. (setq x
  5999. (if (symbolp x)
  6000. (list x)
  6001. (list (car x) (list 'quote (cdr x)))))
  6002. (if (string-match
  6003. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6004. (symbol-name (car x)))
  6005. x nil))
  6006. varlist))))
  6007. ;;;###autoload
  6008. (defun org-run-like-in-org-mode (cmd)
  6009. "Run a command, pretending that the current buffer is in Org-mode.
  6010. This will temporarily bind local variables that are typically bound in
  6011. Org-mode to the values they have in Org-mode, and then interactively
  6012. call CMD."
  6013. (org-load-modules-maybe)
  6014. (unless org-local-vars
  6015. (setq org-local-vars (org-get-local-variables)))
  6016. (eval (list 'let org-local-vars
  6017. (list 'call-interactively (list 'quote cmd)))))
  6018. ;;;; Archiving
  6019. (defun org-get-category (&optional pos)
  6020. "Get the category applying to position POS."
  6021. (get-text-property (or pos (point)) 'org-category))
  6022. (defun org-refresh-category-properties ()
  6023. "Refresh category text properties in the buffer."
  6024. (let ((def-cat (cond
  6025. ((null org-category)
  6026. (if buffer-file-name
  6027. (file-name-sans-extension
  6028. (file-name-nondirectory buffer-file-name))
  6029. "???"))
  6030. ((symbolp org-category) (symbol-name org-category))
  6031. (t org-category)))
  6032. beg end cat pos optionp)
  6033. (org-unmodified
  6034. (save-excursion
  6035. (save-restriction
  6036. (widen)
  6037. (goto-char (point-min))
  6038. (put-text-property (point) (point-max) 'org-category def-cat)
  6039. (while (re-search-forward
  6040. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6041. (setq pos (match-end 0)
  6042. optionp (equal (char-after (match-beginning 0)) ?#)
  6043. cat (org-trim (match-string 2)))
  6044. (if optionp
  6045. (setq beg (point-at-bol) end (point-max))
  6046. (org-back-to-heading t)
  6047. (setq beg (point) end (org-end-of-subtree t t)))
  6048. (put-text-property beg end 'org-category cat)
  6049. (goto-char pos)))))))
  6050. ;;;; Link Stuff
  6051. ;;; Link abbreviations
  6052. (defun org-link-expand-abbrev (link)
  6053. "Apply replacements as defined in `org-link-abbrev-alist."
  6054. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6055. (let* ((key (match-string 1 link))
  6056. (as (or (assoc key org-link-abbrev-alist-local)
  6057. (assoc key org-link-abbrev-alist)))
  6058. (tag (and (match-end 2) (match-string 3 link)))
  6059. rpl)
  6060. (if (not as)
  6061. link
  6062. (setq rpl (cdr as))
  6063. (cond
  6064. ((symbolp rpl) (funcall rpl tag))
  6065. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6066. ((string-match "%h" rpl)
  6067. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6068. (t (concat rpl tag)))))
  6069. link))
  6070. ;;; Storing and inserting links
  6071. (defvar org-insert-link-history nil
  6072. "Minibuffer history for links inserted with `org-insert-link'.")
  6073. (defvar org-stored-links nil
  6074. "Contains the links stored with `org-store-link'.")
  6075. (defvar org-store-link-plist nil
  6076. "Plist with info about the most recently link created with `org-store-link'.")
  6077. (defvar org-link-protocols nil
  6078. "Link protocols added to Org-mode using `org-add-link-type'.")
  6079. (defvar org-store-link-functions nil
  6080. "List of functions that are called to create and store a link.
  6081. Each function will be called in turn until one returns a non-nil
  6082. value. Each function should check if it is responsible for creating
  6083. this link (for example by looking at the major mode).
  6084. If not, it must exit and return nil.
  6085. If yes, it should return a non-nil value after a calling
  6086. `org-store-link-props' with a list of properties and values.
  6087. Special properties are:
  6088. :type The link prefix. like \"http\". This must be given.
  6089. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6090. This is obligatory as well.
  6091. :description Optional default description for the second pair
  6092. of brackets in an Org-mode link. The user can still change
  6093. this when inserting this link into an Org-mode buffer.
  6094. In addition to these, any additional properties can be specified
  6095. and then used in remember templates.")
  6096. (defun org-add-link-type (type &optional follow export)
  6097. "Add TYPE to the list of `org-link-types'.
  6098. Re-compute all regular expressions depending on `org-link-types'
  6099. FOLLOW and EXPORT are two functions.
  6100. FOLLOW should take the link path as the single argument and do whatever
  6101. is necessary to follow the link, for example find a file or display
  6102. a mail message.
  6103. EXPORT should format the link path for export to one of the export formats.
  6104. It should be a function accepting three arguments:
  6105. path the path of the link, the text after the prefix (like \"http:\")
  6106. desc the description of the link, if any, nil if there was no description
  6107. format the export format, a symbol like `html' or `latex'.
  6108. The function may use the FORMAT information to return different values
  6109. depending on the format. The return value will be put literally into
  6110. the exported file.
  6111. Org-mode has a built-in default for exporting links. If you are happy with
  6112. this default, there is no need to define an export function for the link
  6113. type. For a simple example of an export function, see `org-bbdb.el'."
  6114. (add-to-list 'org-link-types type t)
  6115. (org-make-link-regexps)
  6116. (if (assoc type org-link-protocols)
  6117. (setcdr (assoc type org-link-protocols) (list follow export))
  6118. (push (list type follow export) org-link-protocols)))
  6119. ;;;###autoload
  6120. (defun org-store-link (arg)
  6121. "\\<org-mode-map>Store an org-link to the current location.
  6122. This link is added to `org-stored-links' and can later be inserted
  6123. into an org-buffer with \\[org-insert-link].
  6124. For some link types, a prefix arg is interpreted:
  6125. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6126. For file links, arg negates `org-context-in-file-links'."
  6127. (interactive "P")
  6128. (org-load-modules-maybe)
  6129. (setq org-store-link-plist nil) ; reset
  6130. (let (link cpltxt desc description search txt)
  6131. (cond
  6132. ((run-hook-with-args-until-success 'org-store-link-functions)
  6133. (setq link (plist-get org-store-link-plist :link)
  6134. desc (or (plist-get org-store-link-plist :description) link)))
  6135. ((equal (buffer-name) "*Org Edit Src Example*")
  6136. (let (label gc)
  6137. (while (or (not label)
  6138. (save-excursion
  6139. (save-restriction
  6140. (widen)
  6141. (goto-char (point-min))
  6142. (re-search-forward
  6143. (regexp-quote (format org-coderef-label-format label))
  6144. nil t))))
  6145. (when label (message "Label exists already") (sit-for 2))
  6146. (setq label (read-string "Code line label: " label)))
  6147. (end-of-line 1)
  6148. (setq link (format org-coderef-label-format label))
  6149. (setq gc (- 79 (length link)))
  6150. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6151. (insert link)
  6152. (setq link (concat "(" label ")") desc nil)))
  6153. ((eq major-mode 'calendar-mode)
  6154. (let ((cd (calendar-cursor-to-date)))
  6155. (setq link
  6156. (format-time-string
  6157. (car org-time-stamp-formats)
  6158. (apply 'encode-time
  6159. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6160. nil nil nil))))
  6161. (org-store-link-props :type "calendar" :date cd)))
  6162. ((eq major-mode 'w3-mode)
  6163. (setq cpltxt (if (and (buffer-name)
  6164. (not (string-match "Untitled" (buffer-name))))
  6165. (buffer-name)
  6166. (url-view-url t))
  6167. link (org-make-link (url-view-url t)))
  6168. (org-store-link-props :type "w3" :url (url-view-url t)))
  6169. ((eq major-mode 'w3m-mode)
  6170. (setq cpltxt (or w3m-current-title w3m-current-url)
  6171. link (org-make-link w3m-current-url))
  6172. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6173. ((setq search (run-hook-with-args-until-success
  6174. 'org-create-file-search-functions))
  6175. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6176. "::" search))
  6177. (setq cpltxt (or description link)))
  6178. ((eq major-mode 'image-mode)
  6179. (setq cpltxt (concat "file:"
  6180. (abbreviate-file-name buffer-file-name))
  6181. link (org-make-link cpltxt))
  6182. (org-store-link-props :type "image" :file buffer-file-name))
  6183. ((eq major-mode 'dired-mode)
  6184. ;; link to the file in the current line
  6185. (setq cpltxt (concat "file:"
  6186. (abbreviate-file-name
  6187. (expand-file-name
  6188. (dired-get-filename nil t))))
  6189. link (org-make-link cpltxt)))
  6190. ((and buffer-file-name (org-mode-p))
  6191. (cond
  6192. ((org-in-regexp "<<\\(.*?\\)>>")
  6193. (setq cpltxt
  6194. (concat "file:"
  6195. (abbreviate-file-name buffer-file-name)
  6196. "::" (match-string 1))
  6197. link (org-make-link cpltxt)))
  6198. ((and (featurep 'org-id)
  6199. (or (eq org-link-to-org-use-id t)
  6200. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6201. (interactive-p))
  6202. (and org-link-to-org-use-id
  6203. (condition-case nil
  6204. (org-entry-get nil "ID")
  6205. (error nil)))))
  6206. ;; We can make a link using the ID.
  6207. (setq link (condition-case nil
  6208. (prog1 (org-id-store-link)
  6209. (setq desc (plist-get org-store-link-plist
  6210. :description)))
  6211. (error
  6212. ;; probably before first headline, link to file only
  6213. (concat "file:"
  6214. (abbreviate-file-name buffer-file-name))))))
  6215. (t
  6216. ;; Just link to current headline
  6217. (setq cpltxt (concat "file:"
  6218. (abbreviate-file-name buffer-file-name)))
  6219. ;; Add a context search string
  6220. (when (org-xor org-context-in-file-links arg)
  6221. (setq txt (cond
  6222. ((org-on-heading-p) nil)
  6223. ((org-region-active-p)
  6224. (buffer-substring (region-beginning) (region-end)))
  6225. (t nil)))
  6226. (when (or (null txt) (string-match "\\S-" txt))
  6227. (setq cpltxt
  6228. (concat cpltxt "::"
  6229. (condition-case nil
  6230. (org-make-org-heading-search-string txt)
  6231. (error "")))
  6232. desc "NONE")))
  6233. (if (string-match "::\\'" cpltxt)
  6234. (setq cpltxt (substring cpltxt 0 -2)))
  6235. (setq link (org-make-link cpltxt)))))
  6236. ((buffer-file-name (buffer-base-buffer))
  6237. ;; Just link to this file here.
  6238. (setq cpltxt (concat "file:"
  6239. (abbreviate-file-name
  6240. (buffer-file-name (buffer-base-buffer)))))
  6241. ;; Add a context string
  6242. (when (org-xor org-context-in-file-links arg)
  6243. (setq txt (if (org-region-active-p)
  6244. (buffer-substring (region-beginning) (region-end))
  6245. (buffer-substring (point-at-bol) (point-at-eol))))
  6246. ;; Only use search option if there is some text.
  6247. (when (string-match "\\S-" txt)
  6248. (setq cpltxt
  6249. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6250. desc "NONE")))
  6251. (setq link (org-make-link cpltxt)))
  6252. ((interactive-p)
  6253. (error "Cannot link to a buffer which is not visiting a file"))
  6254. (t (setq link nil)))
  6255. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6256. (setq link (or link cpltxt)
  6257. desc (or desc cpltxt))
  6258. (if (equal desc "NONE") (setq desc nil))
  6259. (if (and (interactive-p) link)
  6260. (progn
  6261. (setq org-stored-links
  6262. (cons (list link desc) org-stored-links))
  6263. (message "Stored: %s" (or desc link)))
  6264. (and link (org-make-link-string link desc)))))
  6265. (defun org-store-link-props (&rest plist)
  6266. "Store link properties, extract names and addresses."
  6267. (let (x adr)
  6268. (when (setq x (plist-get plist :from))
  6269. (setq adr (mail-extract-address-components x))
  6270. (setq plist (plist-put plist :fromname (car adr)))
  6271. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6272. (when (setq x (plist-get plist :to))
  6273. (setq adr (mail-extract-address-components x))
  6274. (setq plist (plist-put plist :toname (car adr)))
  6275. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6276. (let ((from (plist-get plist :from))
  6277. (to (plist-get plist :to)))
  6278. (when (and from to org-from-is-user-regexp)
  6279. (setq plist
  6280. (plist-put plist :fromto
  6281. (if (string-match org-from-is-user-regexp from)
  6282. (concat "to %t")
  6283. (concat "from %f"))))))
  6284. (setq org-store-link-plist plist))
  6285. (defun org-add-link-props (&rest plist)
  6286. "Add these properties to the link property list."
  6287. (let (key value)
  6288. (while plist
  6289. (setq key (pop plist) value (pop plist))
  6290. (setq org-store-link-plist
  6291. (plist-put org-store-link-plist key value)))))
  6292. (defun org-email-link-description (&optional fmt)
  6293. "Return the description part of an email link.
  6294. This takes information from `org-store-link-plist' and formats it
  6295. according to FMT (default from `org-email-link-description-format')."
  6296. (setq fmt (or fmt org-email-link-description-format))
  6297. (let* ((p org-store-link-plist)
  6298. (to (plist-get p :toaddress))
  6299. (from (plist-get p :fromaddress))
  6300. (table
  6301. (list
  6302. (cons "%c" (plist-get p :fromto))
  6303. (cons "%F" (plist-get p :from))
  6304. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6305. (cons "%T" (plist-get p :to))
  6306. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6307. (cons "%s" (plist-get p :subject))
  6308. (cons "%m" (plist-get p :message-id)))))
  6309. (when (string-match "%c" fmt)
  6310. ;; Check if the user wrote this message
  6311. (if (and org-from-is-user-regexp from to
  6312. (save-match-data (string-match org-from-is-user-regexp from)))
  6313. (setq fmt (replace-match "to %t" t t fmt))
  6314. (setq fmt (replace-match "from %f" t t fmt))))
  6315. (org-replace-escapes fmt table)))
  6316. (defun org-make-org-heading-search-string (&optional string heading)
  6317. "Make search string for STRING or current headline."
  6318. (interactive)
  6319. (let ((s (or string (org-get-heading))))
  6320. (unless (and string (not heading))
  6321. ;; We are using a headline, clean up garbage in there.
  6322. (if (string-match org-todo-regexp s)
  6323. (setq s (replace-match "" t t s)))
  6324. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6325. (setq s (replace-match "" t t s)))
  6326. (setq s (org-trim s))
  6327. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6328. org-comment-string "\\)") s)
  6329. (setq s (replace-match "" t t s)))
  6330. (while (string-match org-ts-regexp s)
  6331. (setq s (replace-match "" t t s))))
  6332. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6333. (setq s (replace-match " " t t s)))
  6334. (or string (setq s (concat "*" s))) ; Add * for headlines
  6335. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6336. (defun org-make-link (&rest strings)
  6337. "Concatenate STRINGS."
  6338. (apply 'concat strings))
  6339. (defun org-make-link-string (link &optional description)
  6340. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6341. (unless (string-match "\\S-" link)
  6342. (error "Empty link"))
  6343. (when (stringp description)
  6344. ;; Remove brackets from the description, they are fatal.
  6345. (while (string-match "\\[" description)
  6346. (setq description (replace-match "{" t t description)))
  6347. (while (string-match "\\]" description)
  6348. (setq description (replace-match "}" t t description))))
  6349. (when (equal (org-link-escape link) description)
  6350. ;; No description needed, it is identical
  6351. (setq description nil))
  6352. (when (and (not description)
  6353. (not (equal link (org-link-escape link))))
  6354. (setq description (org-extract-attributes link)))
  6355. (concat "[[" (org-link-escape link) "]"
  6356. (if description (concat "[" description "]") "")
  6357. "]"))
  6358. (defconst org-link-escape-chars
  6359. '((?\ . "%20")
  6360. (?\[ . "%5B")
  6361. (?\] . "%5D")
  6362. (?\340 . "%E0") ; `a
  6363. (?\342 . "%E2") ; ^a
  6364. (?\347 . "%E7") ; ,c
  6365. (?\350 . "%E8") ; `e
  6366. (?\351 . "%E9") ; 'e
  6367. (?\352 . "%EA") ; ^e
  6368. (?\356 . "%EE") ; ^i
  6369. (?\364 . "%F4") ; ^o
  6370. (?\371 . "%F9") ; `u
  6371. (?\373 . "%FB") ; ^u
  6372. (?\; . "%3B")
  6373. (?? . "%3F")
  6374. (?= . "%3D")
  6375. (?+ . "%2B")
  6376. )
  6377. "Association list of escapes for some characters problematic in links.
  6378. This is the list that is used for internal purposes.")
  6379. (defvar org-url-encoding-use-url-hexify nil)
  6380. (defconst org-link-escape-chars-browser
  6381. '((?\ . "%20")) ; 32 for the SPC char
  6382. "Association list of escapes for some characters problematic in links.
  6383. This is the list that is used before handing over to the browser.")
  6384. (defun org-link-escape (text &optional table)
  6385. "Escape characters in TEXT that are problematic for links."
  6386. (if org-url-encoding-use-url-hexify
  6387. (url-hexify-string text)
  6388. (setq table (or table org-link-escape-chars))
  6389. (when text
  6390. (let ((re (mapconcat (lambda (x) (regexp-quote
  6391. (char-to-string (car x))))
  6392. table "\\|")))
  6393. (while (string-match re text)
  6394. (setq text
  6395. (replace-match
  6396. (cdr (assoc (string-to-char (match-string 0 text))
  6397. table))
  6398. t t text)))
  6399. text))))
  6400. (defun org-link-unescape (text &optional table)
  6401. "Reverse the action of `org-link-escape'."
  6402. (if org-url-encoding-use-url-hexify
  6403. (url-unhex-string text)
  6404. (setq table (or table org-link-escape-chars))
  6405. (when text
  6406. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6407. table "\\|")))
  6408. (while (string-match re text)
  6409. (setq text
  6410. (replace-match
  6411. (char-to-string (car (rassoc (match-string 0 text) table)))
  6412. t t text)))
  6413. text))))
  6414. (defun org-xor (a b)
  6415. "Exclusive or."
  6416. (if a (not b) b))
  6417. (defun org-fixup-message-id-for-http (s)
  6418. "Replace special characters in a message id, so it can be used in an http query."
  6419. (while (string-match "<" s)
  6420. (setq s (replace-match "%3C" t t s)))
  6421. (while (string-match ">" s)
  6422. (setq s (replace-match "%3E" t t s)))
  6423. (while (string-match "@" s)
  6424. (setq s (replace-match "%40" t t s)))
  6425. s)
  6426. ;;;###autoload
  6427. (defun org-insert-link-global ()
  6428. "Insert a link like Org-mode does.
  6429. This command can be called in any mode to insert a link in Org-mode syntax."
  6430. (interactive)
  6431. (org-load-modules-maybe)
  6432. (org-run-like-in-org-mode 'org-insert-link))
  6433. (defun org-insert-link (&optional complete-file link-location)
  6434. "Insert a link. At the prompt, enter the link.
  6435. Completion can be used to insert any of the link protocol prefixes like
  6436. http or ftp in use.
  6437. The history can be used to select a link previously stored with
  6438. `org-store-link'. When the empty string is entered (i.e. if you just
  6439. press RET at the prompt), the link defaults to the most recently
  6440. stored link. As SPC triggers completion in the minibuffer, you need to
  6441. use M-SPC or C-q SPC to force the insertion of a space character.
  6442. You will also be prompted for a description, and if one is given, it will
  6443. be displayed in the buffer instead of the link.
  6444. If there is already a link at point, this command will allow you to edit link
  6445. and description parts.
  6446. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6447. be selected using completion. The path to the file will be relative to the
  6448. current directory if the file is in the current directory or a subdirectory.
  6449. Otherwise, the link will be the absolute path as completed in the minibuffer
  6450. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6451. option `org-link-file-path-type'.
  6452. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6453. the current directory or below.
  6454. With three \\[universal-argument] prefixes, negate the meaning of
  6455. `org-keep-stored-link-after-insertion'.
  6456. If `org-make-link-description-function' is non-nil, this function will be
  6457. called with the link target, and the result will be the default
  6458. link description.
  6459. If the LINK-LOCATION parameter is non-nil, this value will be
  6460. used as the link location instead of reading one interactively."
  6461. (interactive "P")
  6462. (let* ((wcf (current-window-configuration))
  6463. (region (if (org-region-active-p)
  6464. (buffer-substring (region-beginning) (region-end))))
  6465. (remove (and region (list (region-beginning) (region-end))))
  6466. (desc region)
  6467. tmphist ; byte-compile incorrectly complains about this
  6468. (link link-location)
  6469. entry file)
  6470. (cond
  6471. (link-location) ; specified by arg, just use it.
  6472. ((org-in-regexp org-bracket-link-regexp 1)
  6473. ;; We do have a link at point, and we are going to edit it.
  6474. (setq remove (list (match-beginning 0) (match-end 0)))
  6475. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6476. (setq link (read-string "Link: "
  6477. (org-link-unescape
  6478. (org-match-string-no-properties 1)))))
  6479. ((or (org-in-regexp org-angle-link-re)
  6480. (org-in-regexp org-plain-link-re))
  6481. ;; Convert to bracket link
  6482. (setq remove (list (match-beginning 0) (match-end 0))
  6483. link (read-string "Link: "
  6484. (org-remove-angle-brackets (match-string 0)))))
  6485. ((member complete-file '((4) (16)))
  6486. ;; Completing read for file names.
  6487. (setq file (read-file-name "File: "))
  6488. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6489. (pwd1 (file-name-as-directory (abbreviate-file-name
  6490. (expand-file-name ".")))))
  6491. (cond
  6492. ((equal complete-file '(16))
  6493. (setq link (org-make-link
  6494. "file:"
  6495. (abbreviate-file-name (expand-file-name file)))))
  6496. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6497. (setq link (org-make-link "file:" (match-string 1 file))))
  6498. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6499. (expand-file-name file))
  6500. (setq link (org-make-link
  6501. "file:" (match-string 1 (expand-file-name file)))))
  6502. (t (setq link (org-make-link "file:" file))))))
  6503. (t
  6504. ;; Read link, with completion for stored links.
  6505. (with-output-to-temp-buffer "*Org Links*"
  6506. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6507. (when org-stored-links
  6508. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6509. (princ (mapconcat
  6510. (lambda (x)
  6511. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6512. (reverse org-stored-links) "\n"))))
  6513. (let ((cw (selected-window)))
  6514. (select-window (get-buffer-window "*Org Links*"))
  6515. (setq truncate-lines t)
  6516. (org-fit-window-to-buffer)
  6517. (select-window cw))
  6518. ;; Fake a link history, containing the stored links.
  6519. (setq tmphist (append (mapcar 'car org-stored-links)
  6520. org-insert-link-history))
  6521. (unwind-protect
  6522. (setq link
  6523. (let ((org-completion-use-ido nil))
  6524. (org-completing-read
  6525. "Link: "
  6526. (append
  6527. (mapcar (lambda (x) (list (concat (car x) ":")))
  6528. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6529. (mapcar (lambda (x) (list (concat x ":")))
  6530. org-link-types))
  6531. nil nil nil
  6532. 'tmphist
  6533. (or (car (car org-stored-links))))))
  6534. (set-window-configuration wcf)
  6535. (kill-buffer "*Org Links*"))
  6536. (setq entry (assoc link org-stored-links))
  6537. (or entry (push link org-insert-link-history))
  6538. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6539. (not org-keep-stored-link-after-insertion))
  6540. (setq org-stored-links (delq (assoc link org-stored-links)
  6541. org-stored-links)))
  6542. (setq desc (or desc (nth 1 entry)))))
  6543. (if (string-match org-plain-link-re link)
  6544. ;; URL-like link, normalize the use of angular brackets.
  6545. (setq link (org-make-link (org-remove-angle-brackets link))))
  6546. ;; Check if we are linking to the current file with a search option
  6547. ;; If yes, simplify the link by using only the search option.
  6548. (when (and buffer-file-name
  6549. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6550. (let* ((path (match-string 1 link))
  6551. (case-fold-search nil)
  6552. (search (match-string 2 link)))
  6553. (save-match-data
  6554. (if (equal (file-truename buffer-file-name) (file-truename path))
  6555. ;; We are linking to this same file, with a search option
  6556. (setq link search)))))
  6557. ;; Check if we can/should use a relative path. If yes, simplify the link
  6558. (when (string-match "^file:\\(.*\\)" link)
  6559. (let* ((path (match-string 1 link))
  6560. (origpath path)
  6561. (case-fold-search nil))
  6562. (cond
  6563. ((or (eq org-link-file-path-type 'absolute)
  6564. (equal complete-file '(16)))
  6565. (setq path (abbreviate-file-name (expand-file-name path))))
  6566. ((eq org-link-file-path-type 'noabbrev)
  6567. (setq path (expand-file-name path)))
  6568. ((eq org-link-file-path-type 'relative)
  6569. (setq path (file-relative-name path)))
  6570. (t
  6571. (save-match-data
  6572. (if (string-match (concat "^" (regexp-quote
  6573. (file-name-as-directory
  6574. (expand-file-name "."))))
  6575. (expand-file-name path))
  6576. ;; We are linking a file with relative path name.
  6577. (setq path (substring (expand-file-name path)
  6578. (match-end 0)))
  6579. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6580. (setq link (concat "file:" path))
  6581. (if (equal desc origpath)
  6582. (setq desc path))))
  6583. (if org-make-link-description-function
  6584. (setq desc (funcall org-make-link-description-function link desc)))
  6585. (setq desc (read-string "Description: " desc))
  6586. (unless (string-match "\\S-" desc) (setq desc nil))
  6587. (if remove (apply 'delete-region remove))
  6588. (insert (org-make-link-string link desc))))
  6589. (defun org-completing-read (&rest args)
  6590. "Completing-read with SPACE being a normal character."
  6591. (let ((minibuffer-local-completion-map
  6592. (copy-keymap minibuffer-local-completion-map)))
  6593. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6594. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6595. (apply 'org-ido-completing-read args)))
  6596. (defun org-completing-read-no-ido (&rest args)
  6597. (let (org-completion-use-ido)
  6598. (apply 'org-completing-read args)))
  6599. (defun org-ido-completing-read (&rest args)
  6600. "Completing-read using `ido-mode' speedups if available"
  6601. (if (and org-completion-use-ido
  6602. (fboundp 'ido-completing-read)
  6603. (boundp 'ido-mode) ido-mode
  6604. (listp (second args)))
  6605. (let ((ido-enter-matching-directory nil))
  6606. (apply 'ido-completing-read (concat (car args))
  6607. (mapcar (lambda (x) (car x)) (nth 1 args))
  6608. (cddr args)))
  6609. (apply 'completing-read args)))
  6610. (defun org-extract-attributes (s)
  6611. "Extract the attributes cookie from a string and set as text property."
  6612. (let (a attr (start 0) key value)
  6613. (save-match-data
  6614. (when (string-match "{{\\([^}]+\\)}}$" s)
  6615. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6616. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6617. (setq key (match-string 1 a) value (match-string 2 a)
  6618. start (match-end 0)
  6619. attr (plist-put attr (intern key) value))))
  6620. (org-add-props s nil 'org-attr attr))
  6621. s))
  6622. (defun org-extract-attributes-from-string (tag)
  6623. (let (key value attr)
  6624. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6625. (setq key (match-string 1 tag) value (match-string 2 tag)
  6626. tag (replace-match "" t t tag)
  6627. attr (plist-put attr (intern key) value)))
  6628. (cons tag attr)))
  6629. (defun org-attributes-to-string (plist)
  6630. "Format a property list into an HTML attribute list."
  6631. (let ((s "") key value)
  6632. (while plist
  6633. (setq key (pop plist) value (pop plist))
  6634. (and value
  6635. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6636. s))
  6637. ;;; Opening/following a link
  6638. (defvar org-link-search-failed nil)
  6639. (defun org-next-link ()
  6640. "Move forward to the next link.
  6641. If the link is in hidden text, expose it."
  6642. (interactive)
  6643. (when (and org-link-search-failed (eq this-command last-command))
  6644. (goto-char (point-min))
  6645. (message "Link search wrapped back to beginning of buffer"))
  6646. (setq org-link-search-failed nil)
  6647. (let* ((pos (point))
  6648. (ct (org-context))
  6649. (a (assoc :link ct)))
  6650. (if a (goto-char (nth 2 a)))
  6651. (if (re-search-forward org-any-link-re nil t)
  6652. (progn
  6653. (goto-char (match-beginning 0))
  6654. (if (org-invisible-p) (org-show-context)))
  6655. (goto-char pos)
  6656. (setq org-link-search-failed t)
  6657. (error "No further link found"))))
  6658. (defun org-previous-link ()
  6659. "Move backward to the previous link.
  6660. If the link is in hidden text, expose it."
  6661. (interactive)
  6662. (when (and org-link-search-failed (eq this-command last-command))
  6663. (goto-char (point-max))
  6664. (message "Link search wrapped back to end of buffer"))
  6665. (setq org-link-search-failed nil)
  6666. (let* ((pos (point))
  6667. (ct (org-context))
  6668. (a (assoc :link ct)))
  6669. (if a (goto-char (nth 1 a)))
  6670. (if (re-search-backward org-any-link-re nil t)
  6671. (progn
  6672. (goto-char (match-beginning 0))
  6673. (if (org-invisible-p) (org-show-context)))
  6674. (goto-char pos)
  6675. (setq org-link-search-failed t)
  6676. (error "No further link found"))))
  6677. (defun org-translate-link (s)
  6678. "Translate a link string if a translation function has been defined."
  6679. (if (and org-link-translation-function
  6680. (fboundp org-link-translation-function)
  6681. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6682. (progn
  6683. (setq s (funcall org-link-translation-function
  6684. (match-string 1) (match-string 2)))
  6685. (concat (car s) ":" (cdr s)))
  6686. s))
  6687. (defun org-translate-link-from-planner (type path)
  6688. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6689. This is still an experimental function, your mileage may vary."
  6690. (cond
  6691. ((member type '("http" "https" "news" "ftp"))
  6692. ;; standard Internet links are the same.
  6693. nil)
  6694. ((and (equal type "irc") (string-match "^//" path))
  6695. ;; Planner has two / at the beginning of an irc link, we have 1.
  6696. ;; We should have zero, actually....
  6697. (setq path (substring path 1)))
  6698. ((and (equal type "lisp") (string-match "^/" path))
  6699. ;; Planner has a slash, we do not.
  6700. (setq type "elisp" path (substring path 1)))
  6701. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6702. ;; A typical message link. Planner has the id after the fina slash,
  6703. ;; we separate it with a hash mark
  6704. (setq path (concat (match-string 1 path) "#"
  6705. (org-remove-angle-brackets (match-string 2 path)))))
  6706. )
  6707. (cons type path))
  6708. (defun org-find-file-at-mouse (ev)
  6709. "Open file link or URL at mouse."
  6710. (interactive "e")
  6711. (mouse-set-point ev)
  6712. (org-open-at-point 'in-emacs))
  6713. (defun org-open-at-mouse (ev)
  6714. "Open file link or URL at mouse."
  6715. (interactive "e")
  6716. (mouse-set-point ev)
  6717. (if (eq major-mode 'org-agenda-mode)
  6718. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6719. (org-open-at-point))
  6720. (defvar org-window-config-before-follow-link nil
  6721. "The window configuration before following a link.
  6722. This is saved in case the need arises to restore it.")
  6723. (defvar org-open-link-marker (make-marker)
  6724. "Marker pointing to the location where `org-open-at-point; was called.")
  6725. ;;;###autoload
  6726. (defun org-open-at-point-global ()
  6727. "Follow a link like Org-mode does.
  6728. This command can be called in any mode to follow a link that has
  6729. Org-mode syntax."
  6730. (interactive)
  6731. (org-run-like-in-org-mode 'org-open-at-point))
  6732. ;;;###autoload
  6733. (defun org-open-link-from-string (s &optional arg)
  6734. "Open a link in the string S, as if it was in Org-mode."
  6735. (interactive "sLink: \nP")
  6736. (let ((reference-buffer (current-buffer)))
  6737. (with-temp-buffer
  6738. (let ((org-inhibit-startup t))
  6739. (org-mode)
  6740. (insert s)
  6741. (goto-char (point-min))
  6742. (org-open-at-point arg reference-buffer)))))
  6743. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6744. "Open link at or after point.
  6745. If there is no link at point, this function will search forward up to
  6746. the end of the current line.
  6747. Normally, files will be opened by an appropriate application. If the
  6748. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6749. With a double prefix argument, try to open outside of Emacs, in the
  6750. application the system uses for this file type."
  6751. (interactive "P")
  6752. (org-load-modules-maybe)
  6753. (move-marker org-open-link-marker (point))
  6754. (setq org-window-config-before-follow-link (current-window-configuration))
  6755. (org-remove-occur-highlights nil nil t)
  6756. (cond
  6757. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6758. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6759. (org-footnote-action))
  6760. (t
  6761. (let (type path link line search (pos (point)))
  6762. (catch 'match
  6763. (save-excursion
  6764. (skip-chars-forward "^]\n\r")
  6765. (when (org-in-regexp org-bracket-link-regexp)
  6766. (setq link (org-extract-attributes
  6767. (org-link-unescape (org-match-string-no-properties 1))))
  6768. (while (string-match " *\n *" link)
  6769. (setq link (replace-match " " t t link)))
  6770. (setq link (org-link-expand-abbrev link))
  6771. (cond
  6772. ((or (file-name-absolute-p link)
  6773. (string-match "^\\.\\.?/" link))
  6774. (setq type "file" path link))
  6775. ((string-match org-link-re-with-space3 link)
  6776. (setq type (match-string 1 link) path (match-string 2 link)))
  6777. (t (setq type "thisfile" path link)))
  6778. (throw 'match t)))
  6779. (when (get-text-property (point) 'org-linked-text)
  6780. (setq type "thisfile"
  6781. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6782. (1+ (point)) (point))
  6783. path (buffer-substring
  6784. (previous-single-property-change pos 'org-linked-text)
  6785. (next-single-property-change pos 'org-linked-text)))
  6786. (throw 'match t))
  6787. (save-excursion
  6788. (when (or (org-in-regexp org-angle-link-re)
  6789. (org-in-regexp org-plain-link-re))
  6790. (setq type (match-string 1) path (match-string 2))
  6791. (throw 'match t)))
  6792. (save-excursion
  6793. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6794. (setq type "tags"
  6795. path (match-string 1))
  6796. (while (string-match ":" path)
  6797. (setq path (replace-match "+" t t path)))
  6798. (throw 'match t)))
  6799. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6800. (setq type "tree-match"
  6801. path (match-string 1))
  6802. (throw 'match t)))
  6803. (unless path
  6804. (error "No link found"))
  6805. ;; switch back to reference buffer
  6806. ;; needed when if called in a temporary buffer through
  6807. ;; org-open-link-from-string
  6808. (and reference-buffer (switch-to-buffer reference-buffer))
  6809. ;; Remove any trailing spaces in path
  6810. (if (string-match " +\\'" path)
  6811. (setq path (replace-match "" t t path)))
  6812. (if (and org-link-translation-function
  6813. (fboundp org-link-translation-function))
  6814. ;; Check if we need to translate the link
  6815. (let ((tmp (funcall org-link-translation-function type path)))
  6816. (setq type (car tmp) path (cdr tmp))))
  6817. (cond
  6818. ((assoc type org-link-protocols)
  6819. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6820. ((equal type "mailto")
  6821. (let ((cmd (car org-link-mailto-program))
  6822. (args (cdr org-link-mailto-program)) args1
  6823. (address path) (subject "") a)
  6824. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6825. (setq address (match-string 1 path)
  6826. subject (org-link-escape (match-string 2 path))))
  6827. (while args
  6828. (cond
  6829. ((not (stringp (car args))) (push (pop args) args1))
  6830. (t (setq a (pop args))
  6831. (if (string-match "%a" a)
  6832. (setq a (replace-match address t t a)))
  6833. (if (string-match "%s" a)
  6834. (setq a (replace-match subject t t a)))
  6835. (push a args1))))
  6836. (apply cmd (nreverse args1))))
  6837. ((member type '("http" "https" "ftp" "news"))
  6838. (browse-url (concat type ":" (org-link-escape
  6839. path org-link-escape-chars-browser))))
  6840. ((member type '("message"))
  6841. (browse-url (concat type ":" path)))
  6842. ((string= type "tags")
  6843. (org-tags-view in-emacs path))
  6844. ((string= type "thisfile")
  6845. (if in-emacs
  6846. (switch-to-buffer-other-window
  6847. (org-get-buffer-for-internal-link (current-buffer)))
  6848. (org-mark-ring-push))
  6849. (let ((cmd `(org-link-search
  6850. ,path
  6851. ,(cond ((equal in-emacs '(4)) 'occur)
  6852. ((equal in-emacs '(16)) 'org-occur)
  6853. (t nil))
  6854. ,pos)))
  6855. (condition-case nil (eval cmd)
  6856. (error (progn (widen) (eval cmd))))))
  6857. ((string= type "tree-match")
  6858. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6859. ((string= type "file")
  6860. (if (string-match "::\\([0-9]+\\)\\'" path)
  6861. (setq line (string-to-number (match-string 1 path))
  6862. path (substring path 0 (match-beginning 0)))
  6863. (if (string-match "::\\(.+\\)\\'" path)
  6864. (setq search (match-string 1 path)
  6865. path (substring path 0 (match-beginning 0)))))
  6866. (if (string-match "[*?{]" (file-name-nondirectory path))
  6867. (dired path)
  6868. (org-open-file path in-emacs line search)))
  6869. ((string= type "news")
  6870. (require 'org-gnus)
  6871. (org-gnus-follow-link path))
  6872. ((string= type "shell")
  6873. (let ((cmd path))
  6874. (if (or (not org-confirm-shell-link-function)
  6875. (funcall org-confirm-shell-link-function
  6876. (format "Execute \"%s\" in shell? "
  6877. (org-add-props cmd nil
  6878. 'face 'org-warning))))
  6879. (progn
  6880. (message "Executing %s" cmd)
  6881. (shell-command cmd))
  6882. (error "Abort"))))
  6883. ((string= type "elisp")
  6884. (let ((cmd path))
  6885. (if (or (not org-confirm-elisp-link-function)
  6886. (funcall org-confirm-elisp-link-function
  6887. (format "Execute \"%s\" as elisp? "
  6888. (org-add-props cmd nil
  6889. 'face 'org-warning))))
  6890. (message "%s => %s" cmd
  6891. (if (equal (string-to-char cmd) ?\()
  6892. (eval (read cmd))
  6893. (call-interactively (read cmd))))
  6894. (error "Abort"))))
  6895. (t
  6896. (browse-url-at-point))))))
  6897. (move-marker org-open-link-marker nil)
  6898. (run-hook-with-args 'org-follow-link-hook))
  6899. ;;;; Time estimates
  6900. (defun org-get-effort (&optional pom)
  6901. "Get the effort estimate for the current entry."
  6902. (org-entry-get pom org-effort-property))
  6903. ;;; File search
  6904. (defvar org-create-file-search-functions nil
  6905. "List of functions to construct the right search string for a file link.
  6906. These functions are called in turn with point at the location to
  6907. which the link should point.
  6908. A function in the hook should first test if it would like to
  6909. handle this file type, for example by checking the major-mode or
  6910. the file extension. If it decides not to handle this file, it
  6911. should just return nil to give other functions a chance. If it
  6912. does handle the file, it must return the search string to be used
  6913. when following the link. The search string will be part of the
  6914. file link, given after a double colon, and `org-open-at-point'
  6915. will automatically search for it. If special measures must be
  6916. taken to make the search successful, another function should be
  6917. added to the companion hook `org-execute-file-search-functions',
  6918. which see.
  6919. A function in this hook may also use `setq' to set the variable
  6920. `description' to provide a suggestion for the descriptive text to
  6921. be used for this link when it gets inserted into an Org-mode
  6922. buffer with \\[org-insert-link].")
  6923. (defvar org-execute-file-search-functions nil
  6924. "List of functions to execute a file search triggered by a link.
  6925. Functions added to this hook must accept a single argument, the
  6926. search string that was part of the file link, the part after the
  6927. double colon. The function must first check if it would like to
  6928. handle this search, for example by checking the major-mode or the
  6929. file extension. If it decides not to handle this search, it
  6930. should just return nil to give other functions a chance. If it
  6931. does handle the search, it must return a non-nil value to keep
  6932. other functions from trying.
  6933. Each function can access the current prefix argument through the
  6934. variable `current-prefix-argument'. Note that a single prefix is
  6935. used to force opening a link in Emacs, so it may be good to only
  6936. use a numeric or double prefix to guide the search function.
  6937. In case this is needed, a function in this hook can also restore
  6938. the window configuration before `org-open-at-point' was called using:
  6939. (set-window-configuration org-window-config-before-follow-link)")
  6940. (defun org-link-search (s &optional type avoid-pos)
  6941. "Search for a link search option.
  6942. If S is surrounded by forward slashes, it is interpreted as a
  6943. regular expression. In org-mode files, this will create an `org-occur'
  6944. sparse tree. In ordinary files, `occur' will be used to list matches.
  6945. If the current buffer is in `dired-mode', grep will be used to search
  6946. in all files. If AVOID-POS is given, ignore matches near that position."
  6947. (let ((case-fold-search t)
  6948. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6949. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6950. (append '(("") (" ") ("\t") ("\n"))
  6951. org-emphasis-alist)
  6952. "\\|") "\\)"))
  6953. (pos (point))
  6954. (pre nil) (post nil)
  6955. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6956. (cond
  6957. ;; First check if there are any special
  6958. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6959. ;; Now try the builtin stuff
  6960. ((save-excursion
  6961. (goto-char (point-min))
  6962. (and
  6963. (re-search-forward
  6964. (concat "<<" (regexp-quote s0) ">>") nil t)
  6965. (setq type 'dedicated
  6966. pos (match-beginning 0))))
  6967. ;; There is an exact target for this
  6968. (goto-char pos))
  6969. ((and (string-match "^(\\(.*\\))$" s0)
  6970. (save-excursion
  6971. (goto-char (point-min))
  6972. (and
  6973. (re-search-forward
  6974. (concat "[^[]" (regexp-quote
  6975. (format org-coderef-label-format
  6976. (match-string 1 s0))))
  6977. nil t)
  6978. (setq type 'dedicated
  6979. pos (1+ (match-beginning 0))))))
  6980. ;; There is a coderef target for this
  6981. (goto-char pos))
  6982. ((string-match "^/\\(.*\\)/$" s)
  6983. ;; A regular expression
  6984. (cond
  6985. ((org-mode-p)
  6986. (org-occur (match-string 1 s)))
  6987. ;;((eq major-mode 'dired-mode)
  6988. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6989. (t (org-do-occur (match-string 1 s)))))
  6990. (t
  6991. ;; A normal search strings
  6992. (when (equal (string-to-char s) ?*)
  6993. ;; Anchor on headlines, post may include tags.
  6994. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6995. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6996. s (substring s 1)))
  6997. (remove-text-properties
  6998. 0 (length s)
  6999. '(face nil mouse-face nil keymap nil fontified nil) s)
  7000. ;; Make a series of regular expressions to find a match
  7001. (setq words (org-split-string s "[ \n\r\t]+")
  7002. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7003. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7004. "\\)" markers)
  7005. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7006. re2a (concat "[ \t\r\n]" re2a_)
  7007. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7008. re4 (concat "[^a-zA-Z_]" re4_)
  7009. re1 (concat pre re2 post)
  7010. re3 (concat pre (if pre re4_ re4) post)
  7011. re5 (concat pre ".*" re4)
  7012. re2 (concat pre re2)
  7013. re2a (concat pre (if pre re2a_ re2a))
  7014. re4 (concat pre (if pre re4_ re4))
  7015. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7016. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7017. re5 "\\)"
  7018. ))
  7019. (cond
  7020. ((eq type 'org-occur) (org-occur reall))
  7021. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7022. (t (goto-char (point-min))
  7023. (setq type 'fuzzy)
  7024. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7025. (org-search-not-self 1 re1 nil t)
  7026. (org-search-not-self 1 re2 nil t)
  7027. (org-search-not-self 1 re2a nil t)
  7028. (org-search-not-self 1 re3 nil t)
  7029. (org-search-not-self 1 re4 nil t)
  7030. (org-search-not-self 1 re5 nil t)
  7031. )
  7032. (goto-char (match-beginning 1))
  7033. (goto-char pos)
  7034. (error "No match")))))
  7035. (t
  7036. ;; Normal string-search
  7037. (goto-char (point-min))
  7038. (if (search-forward s nil t)
  7039. (goto-char (match-beginning 0))
  7040. (error "No match"))))
  7041. (and (org-mode-p) (org-show-context 'link-search))
  7042. type))
  7043. (defun org-search-not-self (group &rest args)
  7044. "Execute `re-search-forward', but only accept matches that do not
  7045. enclose the position of `org-open-link-marker'."
  7046. (let ((m org-open-link-marker))
  7047. (catch 'exit
  7048. (while (apply 're-search-forward args)
  7049. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7050. (goto-char (match-end group))
  7051. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7052. (> (match-beginning 0) (marker-position m))
  7053. (< (match-end 0) (marker-position m)))
  7054. (save-match-data
  7055. (or (not (org-in-regexp
  7056. org-bracket-link-analytic-regexp 1))
  7057. (not (match-end 4)) ; no description
  7058. (and (<= (match-beginning 4) (point))
  7059. (>= (match-end 4) (point))))))
  7060. (throw 'exit (point))))))))
  7061. (defun org-get-buffer-for-internal-link (buffer)
  7062. "Return a buffer to be used for displaying the link target of internal links."
  7063. (cond
  7064. ((not org-display-internal-link-with-indirect-buffer)
  7065. buffer)
  7066. ((string-match "(Clone)$" (buffer-name buffer))
  7067. (message "Buffer is already a clone, not making another one")
  7068. ;; we also do not modify visibility in this case
  7069. buffer)
  7070. (t ; make a new indirect buffer for displaying the link
  7071. (let* ((bn (buffer-name buffer))
  7072. (ibn (concat bn "(Clone)"))
  7073. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7074. (with-current-buffer ib (org-overview))
  7075. ib))))
  7076. (defun org-do-occur (regexp &optional cleanup)
  7077. "Call the Emacs command `occur'.
  7078. If CLEANUP is non-nil, remove the printout of the regular expression
  7079. in the *Occur* buffer. This is useful if the regex is long and not useful
  7080. to read."
  7081. (occur regexp)
  7082. (when cleanup
  7083. (let ((cwin (selected-window)) win beg end)
  7084. (when (setq win (get-buffer-window "*Occur*"))
  7085. (select-window win))
  7086. (goto-char (point-min))
  7087. (when (re-search-forward "match[a-z]+" nil t)
  7088. (setq beg (match-end 0))
  7089. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7090. (setq end (1- (match-beginning 0)))))
  7091. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7092. (goto-char (point-min))
  7093. (select-window cwin))))
  7094. ;;; The mark ring for links jumps
  7095. (defvar org-mark-ring nil
  7096. "Mark ring for positions before jumps in Org-mode.")
  7097. (defvar org-mark-ring-last-goto nil
  7098. "Last position in the mark ring used to go back.")
  7099. ;; Fill and close the ring
  7100. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7101. (loop for i from 1 to org-mark-ring-length do
  7102. (push (make-marker) org-mark-ring))
  7103. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7104. org-mark-ring)
  7105. (defun org-mark-ring-push (&optional pos buffer)
  7106. "Put the current position or POS into the mark ring and rotate it."
  7107. (interactive)
  7108. (setq pos (or pos (point)))
  7109. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7110. (move-marker (car org-mark-ring)
  7111. (or pos (point))
  7112. (or buffer (current-buffer)))
  7113. (message "%s"
  7114. (substitute-command-keys
  7115. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7116. (defun org-mark-ring-goto (&optional n)
  7117. "Jump to the previous position in the mark ring.
  7118. With prefix arg N, jump back that many stored positions. When
  7119. called several times in succession, walk through the entire ring.
  7120. Org-mode commands jumping to a different position in the current file,
  7121. or to another Org-mode file, automatically push the old position
  7122. onto the ring."
  7123. (interactive "p")
  7124. (let (p m)
  7125. (if (eq last-command this-command)
  7126. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7127. (setq p org-mark-ring))
  7128. (setq org-mark-ring-last-goto p)
  7129. (setq m (car p))
  7130. (switch-to-buffer (marker-buffer m))
  7131. (goto-char m)
  7132. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7133. (defun org-remove-angle-brackets (s)
  7134. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7135. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7136. s)
  7137. (defun org-add-angle-brackets (s)
  7138. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7139. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7140. s)
  7141. (defun org-remove-double-quotes (s)
  7142. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7143. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7144. s)
  7145. ;;; Following specific links
  7146. (defun org-follow-timestamp-link ()
  7147. (cond
  7148. ((org-at-date-range-p t)
  7149. (let ((org-agenda-start-on-weekday)
  7150. (t1 (match-string 1))
  7151. (t2 (match-string 2)))
  7152. (setq t1 (time-to-days (org-time-string-to-time t1))
  7153. t2 (time-to-days (org-time-string-to-time t2)))
  7154. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7155. ((org-at-timestamp-p t)
  7156. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7157. (substring (match-string 1) 0 10)))
  7158. 1))
  7159. (t (error "This should not happen"))))
  7160. ;;; Following file links
  7161. (defvar org-wait nil)
  7162. (defun org-open-file (path &optional in-emacs line search)
  7163. "Open the file at PATH.
  7164. First, this expands any special file name abbreviations. Then the
  7165. configuration variable `org-file-apps' is checked if it contains an
  7166. entry for this file type, and if yes, the corresponding command is launched.
  7167. If no application is found, Emacs simply visits the file.
  7168. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7169. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7170. and o use an external application to visit the file.
  7171. Optional LINE specifies a line to go to, optional SEARCH a string to
  7172. search for. If LINE or SEARCH is given, the file will always be
  7173. opened in Emacs.
  7174. If the file does not exist, an error is thrown."
  7175. (setq in-emacs (or in-emacs line search))
  7176. (let* ((file (if (equal path "")
  7177. buffer-file-name
  7178. (substitute-in-file-name (expand-file-name path))))
  7179. (apps (append org-file-apps (org-default-apps)))
  7180. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7181. (dirp (if remp nil (file-directory-p file)))
  7182. (file (if (and dirp org-open-directory-means-index-dot-org)
  7183. (concat (file-name-as-directory file) "index.org")
  7184. file))
  7185. (a-m-a-p (assq 'auto-mode apps))
  7186. (dfile (downcase file))
  7187. (old-buffer (current-buffer))
  7188. (old-pos (point))
  7189. (old-mode major-mode)
  7190. ext cmd)
  7191. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7192. (setq ext (match-string 1 dfile))
  7193. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7194. (setq ext (match-string 1 dfile))))
  7195. (cond
  7196. ((equal in-emacs '(16))
  7197. (setq cmd (cdr (assoc 'system apps))))
  7198. (in-emacs (setq cmd 'emacs))
  7199. (t
  7200. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7201. (and dirp (cdr (assoc 'directory apps)))
  7202. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7203. 'string-match)
  7204. (cdr (assoc ext apps))
  7205. (cdr (assoc t apps))))))
  7206. (when (eq cmd 'system)
  7207. (setq cmd (cdr (assoc 'system apps))))
  7208. (when (eq cmd 'default)
  7209. (setq cmd (cdr (assoc t apps))))
  7210. (when (eq cmd 'mailcap)
  7211. (require 'mailcap)
  7212. (mailcap-parse-mailcaps)
  7213. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7214. (command (mailcap-mime-info mime-type)))
  7215. (if (stringp command)
  7216. (setq cmd command)
  7217. (setq cmd 'emacs))))
  7218. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7219. (not (file-exists-p file))
  7220. (not org-open-non-existing-files))
  7221. (error "No such file: %s" file))
  7222. (cond
  7223. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7224. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7225. (while (string-match "['\"]%s['\"]" cmd)
  7226. (setq cmd (replace-match "%s" t t cmd)))
  7227. (while (string-match "%s" cmd)
  7228. (setq cmd (replace-match
  7229. (save-match-data
  7230. (shell-quote-argument
  7231. (convert-standard-filename file)))
  7232. t t cmd)))
  7233. (save-window-excursion
  7234. (start-process-shell-command cmd nil cmd)
  7235. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7236. ))
  7237. ((or (stringp cmd)
  7238. (eq cmd 'emacs))
  7239. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7240. (widen)
  7241. (if line (goto-line line)
  7242. (if search (org-link-search search))))
  7243. ((consp cmd)
  7244. (let ((file (convert-standard-filename file)))
  7245. (eval cmd)))
  7246. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7247. (and (org-mode-p) (eq old-mode 'org-mode)
  7248. (or (not (equal old-buffer (current-buffer)))
  7249. (not (equal old-pos (point))))
  7250. (org-mark-ring-push old-pos old-buffer))))
  7251. (defun org-default-apps ()
  7252. "Return the default applications for this operating system."
  7253. (cond
  7254. ((eq system-type 'darwin)
  7255. org-file-apps-defaults-macosx)
  7256. ((eq system-type 'windows-nt)
  7257. org-file-apps-defaults-windowsnt)
  7258. (t org-file-apps-defaults-gnu)))
  7259. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7260. "Convert extensions to regular expressions in the cars of LIST.
  7261. Also, weed out any non-string entries, because the return value is used
  7262. only for regexp matching.
  7263. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7264. point to the symbol `emacs', indicating that the file should
  7265. be opened in Emacs."
  7266. (append
  7267. (delq nil
  7268. (mapcar (lambda (x)
  7269. (if (not (stringp (car x)))
  7270. nil
  7271. (if (string-match "\\W" (car x))
  7272. x
  7273. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7274. list))
  7275. (if add-auto-mode
  7276. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7277. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7278. (defun org-file-remote-p (file)
  7279. "Test whether FILE specifies a location on a remote system.
  7280. Return non-nil if the location is indeed remote.
  7281. For example, the filename \"/user@host:/foo\" specifies a location
  7282. on the system \"/user@host:\"."
  7283. (cond ((fboundp 'file-remote-p)
  7284. (file-remote-p file))
  7285. ((fboundp 'tramp-handle-file-remote-p)
  7286. (tramp-handle-file-remote-p file))
  7287. ((and (boundp 'ange-ftp-name-format)
  7288. (string-match (car ange-ftp-name-format) file))
  7289. t)
  7290. (t nil)))
  7291. ;;;; Refiling
  7292. (defun org-get-org-file ()
  7293. "Read a filename, with default directory `org-directory'."
  7294. (let ((default (or org-default-notes-file remember-data-file)))
  7295. (read-file-name (format "File name [%s]: " default)
  7296. (file-name-as-directory org-directory)
  7297. default)))
  7298. (defun org-notes-order-reversed-p ()
  7299. "Check if the current file should receive notes in reversed order."
  7300. (cond
  7301. ((not org-reverse-note-order) nil)
  7302. ((eq t org-reverse-note-order) t)
  7303. ((not (listp org-reverse-note-order)) nil)
  7304. (t (catch 'exit
  7305. (let ((all org-reverse-note-order)
  7306. entry)
  7307. (while (setq entry (pop all))
  7308. (if (string-match (car entry) buffer-file-name)
  7309. (throw 'exit (cdr entry))))
  7310. nil)))))
  7311. (defvar org-refile-target-table nil
  7312. "The list of refile targets, created by `org-refile'.")
  7313. (defvar org-agenda-new-buffers nil
  7314. "Buffers created to visit agenda files.")
  7315. (defun org-get-refile-targets (&optional default-buffer)
  7316. "Produce a table with refile targets."
  7317. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7318. targets txt re files f desc descre fast-path-p level)
  7319. (message "Getting targets...")
  7320. (with-current-buffer (or default-buffer (current-buffer))
  7321. (while (setq entry (pop entries))
  7322. (setq files (car entry) desc (cdr entry))
  7323. (setq fast-path-p nil)
  7324. (cond
  7325. ((null files) (setq files (list (current-buffer))))
  7326. ((eq files 'org-agenda-files)
  7327. (setq files (org-agenda-files 'unrestricted)))
  7328. ((and (symbolp files) (fboundp files))
  7329. (setq files (funcall files)))
  7330. ((and (symbolp files) (boundp files))
  7331. (setq files (symbol-value files))))
  7332. (if (stringp files) (setq files (list files)))
  7333. (cond
  7334. ((eq (car desc) :tag)
  7335. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7336. ((eq (car desc) :todo)
  7337. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7338. ((eq (car desc) :regexp)
  7339. (setq descre (cdr desc)))
  7340. ((eq (car desc) :level)
  7341. (setq descre (concat "^\\*\\{" (number-to-string
  7342. (if org-odd-levels-only
  7343. (1- (* 2 (cdr desc)))
  7344. (cdr desc)))
  7345. "\\}[ \t]")))
  7346. ((eq (car desc) :maxlevel)
  7347. (setq fast-path-p t)
  7348. (setq descre (concat "^\\*\\{1," (number-to-string
  7349. (if org-odd-levels-only
  7350. (1- (* 2 (cdr desc)))
  7351. (cdr desc)))
  7352. "\\}[ \t]")))
  7353. (t (error "Bad refiling target description %s" desc)))
  7354. (while (setq f (pop files))
  7355. (save-excursion
  7356. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7357. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7358. (setq f (expand-file-name f))
  7359. (if (eq org-refile-use-outline-path 'file)
  7360. (push (list (file-name-nondirectory f) f nil nil) targets))
  7361. (save-excursion
  7362. (save-restriction
  7363. (widen)
  7364. (goto-char (point-min))
  7365. (while (re-search-forward descre nil t)
  7366. (goto-char (point-at-bol))
  7367. (when (looking-at org-complex-heading-regexp)
  7368. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7369. txt (org-link-display-format (match-string 4))
  7370. re (concat "^" (regexp-quote
  7371. (buffer-substring (match-beginning 1)
  7372. (match-end 4)))))
  7373. (if (match-end 5) (setq re (concat re "[ \t]+"
  7374. (regexp-quote
  7375. (match-string 5)))))
  7376. (setq re (concat re "[ \t]*$"))
  7377. (when org-refile-use-outline-path
  7378. (setq txt (mapconcat 'org-protect-slash
  7379. (append
  7380. (if (eq org-refile-use-outline-path 'file)
  7381. (list (file-name-nondirectory
  7382. (buffer-file-name (buffer-base-buffer))))
  7383. (if (eq org-refile-use-outline-path 'full-file-path)
  7384. (list (buffer-file-name (buffer-base-buffer)))))
  7385. (org-get-outline-path fast-path-p level txt)
  7386. (list txt))
  7387. "/")))
  7388. (push (list txt f re (point)) targets))
  7389. (goto-char (point-at-eol))))))))
  7390. (message "Getting targets...done")
  7391. (nreverse targets))))
  7392. (defun org-protect-slash (s)
  7393. (while (string-match "/" s)
  7394. (setq s (replace-match "\\" t t s)))
  7395. s)
  7396. (defvar org-olpa (make-vector 20 nil))
  7397. (defun org-get-outline-path (&optional fastp level heading)
  7398. "Return the outline path to the current entry, as a list."
  7399. (if fastp
  7400. (progn
  7401. (if (> level 19)
  7402. (error "Outline path failure, more than 19 levels."))
  7403. (loop for i from level upto 19 do
  7404. (aset org-olpa i nil))
  7405. (prog1
  7406. (delq nil (append org-olpa nil))
  7407. (aset org-olpa level heading)))
  7408. (let (rtn)
  7409. (save-excursion
  7410. (while (org-up-heading-safe)
  7411. (when (looking-at org-complex-heading-regexp)
  7412. (push (org-match-string-no-properties 4) rtn)))
  7413. rtn))))
  7414. (defvar org-refile-history nil
  7415. "History for refiling operations.")
  7416. (defun org-refile (&optional goto default-buffer)
  7417. "Move the entry at point to another heading.
  7418. The list of target headings is compiled using the information in
  7419. `org-refile-targets', which see. This list is created before each use
  7420. and will therefore always be up-to-date.
  7421. At the target location, the entry is filed as a subitem of the target heading.
  7422. Depending on `org-reverse-note-order', the new subitem will either be the
  7423. first or the last subitem.
  7424. If there is an active region, all entries in that region will be moved.
  7425. However, the region must fulfil the requirement that the first heading
  7426. is the first one sets the top-level of the moved text - at most siblings
  7427. below it are allowed.
  7428. With prefix arg GOTO, the command will only visit the target location,
  7429. not actually move anything.
  7430. With a double prefix `C-u C-u', go to the location where the last refiling
  7431. operation has put the subtree.
  7432. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7433. (interactive "P")
  7434. (let* ((cbuf (current-buffer))
  7435. (regionp (org-region-active-p))
  7436. (region-start (and regionp (region-beginning)))
  7437. (region-end (and regionp (region-end)))
  7438. (region-length (and regionp (- region-end region-start)))
  7439. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7440. pos it nbuf file re level reversed)
  7441. (when regionp (goto-char region-start)
  7442. (unless (org-kill-is-subtree-p
  7443. (buffer-substring region-start region-end))
  7444. (error "The region is not a (sequence of) subtree(s)")))
  7445. (if (equal goto '(16))
  7446. (org-refile-goto-last-stored)
  7447. (when (setq it (org-refile-get-location
  7448. (if goto "Goto: " "Refile to: ") default-buffer))
  7449. (setq file (nth 1 it)
  7450. re (nth 2 it)
  7451. pos (nth 3 it))
  7452. (if (and (not goto)
  7453. pos
  7454. (equal (buffer-file-name) file)
  7455. (if regionp
  7456. (and (>= pos region-start)
  7457. (<= pos region-end))
  7458. (and (>= pos (point))
  7459. (< pos (save-excursion
  7460. (org-end-of-subtree t t))))))
  7461. (error "Cannot refile to position inside the tree or region"))
  7462. (setq nbuf (or (find-buffer-visiting file)
  7463. (find-file-noselect file)))
  7464. (if goto
  7465. (progn
  7466. (switch-to-buffer nbuf)
  7467. (goto-char pos)
  7468. (org-show-context 'org-goto))
  7469. (if regionp
  7470. (progn
  7471. (kill-new (buffer-substring region-start region-end))
  7472. (org-save-markers-in-region region-start region-end))
  7473. (org-copy-subtree 1 nil t))
  7474. (save-excursion
  7475. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7476. (find-file-noselect file))))
  7477. (setq reversed (org-notes-order-reversed-p))
  7478. (save-excursion
  7479. (save-restriction
  7480. (widen)
  7481. (if pos
  7482. (progn
  7483. (goto-char pos)
  7484. (looking-at outline-regexp)
  7485. (setq level (org-get-valid-level (funcall outline-level) 1))
  7486. (goto-char
  7487. (if reversed
  7488. (or (outline-next-heading) (point-max))
  7489. (or (save-excursion (outline-get-next-sibling))
  7490. (org-end-of-subtree t t)
  7491. (point-max)))))
  7492. (setq level 1)
  7493. (if (not reversed)
  7494. (goto-char (point-max))
  7495. (goto-char (point-min))
  7496. (or (outline-next-heading) (goto-char (point-max)))))
  7497. (if (not (bolp)) (newline))
  7498. (bookmark-set "org-refile-last-stored")
  7499. (org-paste-subtree level))))
  7500. (if regionp
  7501. (delete-region (point) (+ (point) region-length))
  7502. (org-cut-subtree))
  7503. (setq org-markers-to-move nil)
  7504. (message "Refiled to \"%s\"" (car it)))))))
  7505. (defun org-refile-goto-last-stored ()
  7506. "Go to the location where the last refile was stored."
  7507. (interactive)
  7508. (bookmark-jump "org-refile-last-stored")
  7509. (message "This is the location of the last refile"))
  7510. (defun org-refile-get-location (&optional prompt default-buffer)
  7511. "Prompt the user for a refile location, using PROMPT."
  7512. (let ((org-refile-targets org-refile-targets)
  7513. (org-refile-use-outline-path org-refile-use-outline-path))
  7514. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7515. (unless org-refile-target-table
  7516. (error "No refile targets"))
  7517. (let* ((cbuf (current-buffer))
  7518. (partial-completion-mode nil)
  7519. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7520. (cfunc (if (and org-refile-use-outline-path
  7521. org-outline-path-complete-in-steps)
  7522. 'org-olpath-completing-read
  7523. 'org-ido-completing-read))
  7524. (extra (if org-refile-use-outline-path "/" ""))
  7525. (filename (and cfn (expand-file-name cfn)))
  7526. (tbl (mapcar
  7527. (lambda (x)
  7528. (if (and (not (member org-refile-use-outline-path
  7529. '(file full-file-path)))
  7530. (not (equal filename (nth 1 x))))
  7531. (cons (concat (car x) extra " ("
  7532. (file-name-nondirectory (nth 1 x)) ")")
  7533. (cdr x))
  7534. (cons (concat (car x) extra) (cdr x))))
  7535. org-refile-target-table))
  7536. (completion-ignore-case t))
  7537. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7538. tbl)))
  7539. (defun org-olpath-completing-read (prompt collection &rest args)
  7540. "Read an outline path like a file name."
  7541. (let ((thetable collection)
  7542. (org-completion-use-ido nil)) ; does not work with ido.
  7543. (apply
  7544. 'org-ido-completing-read prompt
  7545. (lambda (string predicate &optional flag)
  7546. (let (rtn r f (l (length string)))
  7547. (cond
  7548. ((eq flag nil)
  7549. ;; try completion
  7550. (try-completion string thetable))
  7551. ((eq flag t)
  7552. ;; all-completions
  7553. (setq rtn (all-completions string thetable predicate))
  7554. (mapcar
  7555. (lambda (x)
  7556. (setq r (substring x l))
  7557. (if (string-match " ([^)]*)$" x)
  7558. (setq f (match-string 0 x))
  7559. (setq f ""))
  7560. (if (string-match "/" r)
  7561. (concat string (substring r 0 (match-end 0)) f)
  7562. x))
  7563. rtn))
  7564. ((eq flag 'lambda)
  7565. ;; exact match?
  7566. (assoc string thetable)))
  7567. ))
  7568. args)))
  7569. ;;;; Dynamic blocks
  7570. (defun org-find-dblock (name)
  7571. "Find the first dynamic block with name NAME in the buffer.
  7572. If not found, stay at current position and return nil."
  7573. (let (pos)
  7574. (save-excursion
  7575. (goto-char (point-min))
  7576. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7577. nil t)
  7578. (match-beginning 0))))
  7579. (if pos (goto-char pos))
  7580. pos))
  7581. (defconst org-dblock-start-re
  7582. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7583. "Matches the startline of a dynamic block, with parameters.")
  7584. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7585. "Matches the end of a dynamic block.")
  7586. (defun org-create-dblock (plist)
  7587. "Create a dynamic block section, with parameters taken from PLIST.
  7588. PLIST must contain a :name entry which is used as name of the block."
  7589. (unless (bolp) (newline))
  7590. (let ((name (plist-get plist :name)))
  7591. (insert "#+BEGIN: " name)
  7592. (while plist
  7593. (if (eq (car plist) :name)
  7594. (setq plist (cddr plist))
  7595. (insert " " (prin1-to-string (pop plist)))))
  7596. (insert "\n\n#+END:\n")
  7597. (beginning-of-line -2)))
  7598. (defun org-prepare-dblock ()
  7599. "Prepare dynamic block for refresh.
  7600. This empties the block, puts the cursor at the insert position and returns
  7601. the property list including an extra property :name with the block name."
  7602. (unless (looking-at org-dblock-start-re)
  7603. (error "Not at a dynamic block"))
  7604. (let* ((begdel (1+ (match-end 0)))
  7605. (name (org-no-properties (match-string 1)))
  7606. (params (append (list :name name)
  7607. (read (concat "(" (match-string 3) ")")))))
  7608. (unless (re-search-forward org-dblock-end-re nil t)
  7609. (error "Dynamic block not terminated"))
  7610. (setq params
  7611. (append params
  7612. (list :content (buffer-substring
  7613. begdel (match-beginning 0)))))
  7614. (delete-region begdel (match-beginning 0))
  7615. (goto-char begdel)
  7616. (open-line 1)
  7617. params))
  7618. (defun org-map-dblocks (&optional command)
  7619. "Apply COMMAND to all dynamic blocks in the current buffer.
  7620. If COMMAND is not given, use `org-update-dblock'."
  7621. (let ((cmd (or command 'org-update-dblock))
  7622. pos)
  7623. (save-excursion
  7624. (goto-char (point-min))
  7625. (while (re-search-forward org-dblock-start-re nil t)
  7626. (goto-char (setq pos (match-beginning 0)))
  7627. (condition-case nil
  7628. (funcall cmd)
  7629. (error (message "Error during update of dynamic block")))
  7630. (goto-char pos)
  7631. (unless (re-search-forward org-dblock-end-re nil t)
  7632. (error "Dynamic block not terminated"))))))
  7633. (defun org-dblock-update (&optional arg)
  7634. "User command for updating dynamic blocks.
  7635. Update the dynamic block at point. With prefix ARG, update all dynamic
  7636. blocks in the buffer."
  7637. (interactive "P")
  7638. (if arg
  7639. (org-update-all-dblocks)
  7640. (or (looking-at org-dblock-start-re)
  7641. (org-beginning-of-dblock))
  7642. (org-update-dblock)))
  7643. (defun org-update-dblock ()
  7644. "Update the dynamic block at point
  7645. This means to empty the block, parse for parameters and then call
  7646. the correct writing function."
  7647. (save-window-excursion
  7648. (let* ((pos (point))
  7649. (line (org-current-line))
  7650. (params (org-prepare-dblock))
  7651. (name (plist-get params :name))
  7652. (cmd (intern (concat "org-dblock-write:" name))))
  7653. (message "Updating dynamic block `%s' at line %d..." name line)
  7654. (funcall cmd params)
  7655. (message "Updating dynamic block `%s' at line %d...done" name line)
  7656. (goto-char pos))))
  7657. (defun org-beginning-of-dblock ()
  7658. "Find the beginning of the dynamic block at point.
  7659. Error if there is no such block at point."
  7660. (let ((pos (point))
  7661. beg)
  7662. (end-of-line 1)
  7663. (if (and (re-search-backward org-dblock-start-re nil t)
  7664. (setq beg (match-beginning 0))
  7665. (re-search-forward org-dblock-end-re nil t)
  7666. (> (match-end 0) pos))
  7667. (goto-char beg)
  7668. (goto-char pos)
  7669. (error "Not in a dynamic block"))))
  7670. (defun org-update-all-dblocks ()
  7671. "Update all dynamic blocks in the buffer.
  7672. This function can be used in a hook."
  7673. (when (org-mode-p)
  7674. (org-map-dblocks 'org-update-dblock)))
  7675. ;;;; Completion
  7676. (defconst org-additional-option-like-keywords
  7677. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7678. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7679. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7680. "BEGIN_QUOTE" "END_QUOTE"
  7681. "BEGIN_VERSE" "END_VERSE"
  7682. "BEGIN_CENTER" "END_CENTER"
  7683. "BEGIN_SRC" "END_SRC"
  7684. "CATEGORY" "COLUMNS"
  7685. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7686. (defcustom org-structure-template-alist
  7687. '(
  7688. ("s" "#+begin_src ?\n\n#+end_src"
  7689. "<src lang=\"?\">\n\n</src>")
  7690. ("e" "#+begin_example\n?\n#+end_example"
  7691. "<example>\n?\n</example>")
  7692. ("q" "#+begin_quote\n?\n#+end_quote"
  7693. "<quote>\n?\n</quote>")
  7694. ("v" "#+begin_verse\n?\n#+end_verse"
  7695. "<verse>\n?\n/verse>")
  7696. ("c" "#+begin_center\n?\n#+end_center"
  7697. "<center>\n?\n/center>")
  7698. ("l" "#+begin_latex\n?\n#+end_latex"
  7699. "<literal style=\"latex\">\n?\n</literal>")
  7700. ("L" "#+latex: "
  7701. "<literal style=\"latex\">?</literal>")
  7702. ("h" "#+begin_html\n?\n#+end_html"
  7703. "<literal style=\"html\">\n?\n</literal>")
  7704. ("H" "#+html: "
  7705. "<literal style=\"html\">?</literal>")
  7706. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7707. ("A" "#+ascii: ")
  7708. ("i" "#+include %file ?"
  7709. "<include file=%file markup=\"?\">")
  7710. )
  7711. "Structure completion elements.
  7712. This is a list of abbreviation keys and values. The value gets inserted
  7713. it you type @samp{.} followed by the key and then the completion key,
  7714. usually `M-TAB'. %file will be replaced by a file name after prompting
  7715. for the file using completion.
  7716. There are two templates for each key, the first uses the original Org syntax,
  7717. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7718. the default when the /org-mtags.el/ module has been loaded. See also the
  7719. variable `org-mtags-prefer-muse-templates'.
  7720. This is an experimental feature, it is undecided if it is going to stay in."
  7721. :group 'org-completion
  7722. :type '(repeat
  7723. (string :tag "Key")
  7724. (string :tag "Template")
  7725. (string :tag "Muse Template")))
  7726. (defun org-try-structure-completion ()
  7727. "Try to complete a structure template before point.
  7728. This looks for strings like \"<e\" on an otherwise empty line and
  7729. expands them."
  7730. (let ((l (buffer-substring (point-at-bol) (point)))
  7731. a)
  7732. (when (and (looking-at "[ \t]*$")
  7733. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7734. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7735. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7736. (match-beginning 1)) a)
  7737. t)))
  7738. (defun org-complete-expand-structure-template (start cell)
  7739. "Expand a structure template."
  7740. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7741. (rpl (nth (if musep 2 1) cell)))
  7742. (delete-region start (point))
  7743. (when (string-match "\\`#\\+" rpl)
  7744. (cond
  7745. ((bolp))
  7746. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7747. (delete-region (point-at-bol) (point)))
  7748. (t (newline))))
  7749. (setq start (point))
  7750. (if (string-match "%file" rpl)
  7751. (setq rpl (replace-match
  7752. (concat
  7753. "\""
  7754. (save-match-data
  7755. (abbreviate-file-name (read-file-name "Include file: ")))
  7756. "\"")
  7757. t t rpl)))
  7758. (insert rpl)
  7759. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7760. (defun org-complete (&optional arg)
  7761. "Perform completion on word at point.
  7762. At the beginning of a headline, this completes TODO keywords as given in
  7763. `org-todo-keywords'.
  7764. If the current word is preceded by a backslash, completes the TeX symbols
  7765. that are supported for HTML support.
  7766. If the current word is preceded by \"#+\", completes special words for
  7767. setting file options.
  7768. In the line after \"#+STARTUP:, complete valid keywords.\"
  7769. At all other locations, this simply calls the value of
  7770. `org-completion-fallback-command'."
  7771. (interactive "P")
  7772. (org-without-partial-completion
  7773. (catch 'exit
  7774. (let* ((a nil)
  7775. (end (point))
  7776. (beg1 (save-excursion
  7777. (skip-chars-backward (org-re "[:alnum:]_@"))
  7778. (point)))
  7779. (beg (save-excursion
  7780. (skip-chars-backward "a-zA-Z0-9_:$")
  7781. (point)))
  7782. (confirm (lambda (x) (stringp (car x))))
  7783. (searchhead (equal (char-before beg) ?*))
  7784. (struct
  7785. (when (and (member (char-before beg1) '(?. ?<))
  7786. (setq a (assoc (buffer-substring beg1 (point))
  7787. org-structure-template-alist)))
  7788. (org-complete-expand-structure-template (1- beg1) a)
  7789. (throw 'exit t)))
  7790. (tag (and (equal (char-before beg1) ?:)
  7791. (equal (char-after (point-at-bol)) ?*)))
  7792. (prop (and (equal (char-before beg1) ?:)
  7793. (not (equal (char-after (point-at-bol)) ?*))))
  7794. (texp (equal (char-before beg) ?\\))
  7795. (link (equal (char-before beg) ?\[))
  7796. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7797. beg)
  7798. "#+"))
  7799. (startup (string-match "^#\\+STARTUP:.*"
  7800. (buffer-substring (point-at-bol) (point))))
  7801. (completion-ignore-case opt)
  7802. (type nil)
  7803. (tbl nil)
  7804. (table (cond
  7805. (opt
  7806. (setq type :opt)
  7807. (require 'org-exp)
  7808. (append
  7809. (mapcar
  7810. (lambda (x)
  7811. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7812. (cons (match-string 2 x) (match-string 1 x)))
  7813. (org-split-string (org-get-current-options) "\n"))
  7814. (mapcar 'list org-additional-option-like-keywords)))
  7815. (startup
  7816. (setq type :startup)
  7817. org-startup-options)
  7818. (link (append org-link-abbrev-alist-local
  7819. org-link-abbrev-alist))
  7820. (texp
  7821. (setq type :tex)
  7822. org-html-entities)
  7823. ((string-match "\\`\\*+[ \t]+\\'"
  7824. (buffer-substring (point-at-bol) beg))
  7825. (setq type :todo)
  7826. (mapcar 'list org-todo-keywords-1))
  7827. (searchhead
  7828. (setq type :searchhead)
  7829. (save-excursion
  7830. (goto-char (point-min))
  7831. (while (re-search-forward org-todo-line-regexp nil t)
  7832. (push (list
  7833. (org-make-org-heading-search-string
  7834. (match-string 3) t))
  7835. tbl)))
  7836. tbl)
  7837. (tag (setq type :tag beg beg1)
  7838. (or org-tag-alist (org-get-buffer-tags)))
  7839. (prop (setq type :prop beg beg1)
  7840. (mapcar 'list (org-buffer-property-keys nil t t)))
  7841. (t (progn
  7842. (call-interactively org-completion-fallback-command)
  7843. (throw 'exit nil)))))
  7844. (pattern (buffer-substring-no-properties beg end))
  7845. (completion (try-completion pattern table confirm)))
  7846. (cond ((eq completion t)
  7847. (if (not (assoc (upcase pattern) table))
  7848. (message "Already complete")
  7849. (if (and (equal type :opt)
  7850. (not (member (car (assoc (upcase pattern) table))
  7851. org-additional-option-like-keywords)))
  7852. (insert (substring (cdr (assoc (upcase pattern) table))
  7853. (length pattern)))
  7854. (if (memq type '(:tag :prop)) (insert ":")))))
  7855. ((null completion)
  7856. (message "Can't find completion for \"%s\"" pattern)
  7857. (ding))
  7858. ((not (string= pattern completion))
  7859. (delete-region beg end)
  7860. (if (string-match " +$" completion)
  7861. (setq completion (replace-match "" t t completion)))
  7862. (insert completion)
  7863. (if (get-buffer-window "*Completions*")
  7864. (delete-window (get-buffer-window "*Completions*")))
  7865. (if (assoc completion table)
  7866. (if (eq type :todo) (insert " ")
  7867. (if (memq type '(:tag :prop)) (insert ":"))))
  7868. (if (and (equal type :opt) (assoc completion table))
  7869. (message "%s" (substitute-command-keys
  7870. "Press \\[org-complete] again to insert example settings"))))
  7871. (t
  7872. (message "Making completion list...")
  7873. (let ((list (sort (all-completions pattern table confirm)
  7874. 'string<)))
  7875. (with-output-to-temp-buffer "*Completions*"
  7876. (condition-case nil
  7877. ;; Protection needed for XEmacs and emacs 21
  7878. (display-completion-list list pattern)
  7879. (error (display-completion-list list)))))
  7880. (message "Making completion list...%s" "done")))))))
  7881. ;;;; TODO, DEADLINE, Comments
  7882. (defun org-toggle-comment ()
  7883. "Change the COMMENT state of an entry."
  7884. (interactive)
  7885. (save-excursion
  7886. (org-back-to-heading)
  7887. (let (case-fold-search)
  7888. (if (looking-at (concat outline-regexp
  7889. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7890. (replace-match "" t t nil 1)
  7891. (if (looking-at outline-regexp)
  7892. (progn
  7893. (goto-char (match-end 0))
  7894. (insert org-comment-string " ")))))))
  7895. (defvar org-last-todo-state-is-todo nil
  7896. "This is non-nil when the last TODO state change led to a TODO state.
  7897. If the last change removed the TODO tag or switched to DONE, then
  7898. this is nil.")
  7899. (defvar org-setting-tags nil) ; dynamically skipped
  7900. (defun org-parse-local-options (string var)
  7901. "Parse STRING for startup setting relevant for variable VAR."
  7902. (let ((rtn (symbol-value var))
  7903. e opts)
  7904. (save-match-data
  7905. (if (or (not string) (not (string-match "\\S-" string)))
  7906. rtn
  7907. (setq opts (delq nil (mapcar (lambda (x)
  7908. (setq e (assoc x org-startup-options))
  7909. (if (eq (nth 1 e) var) e nil))
  7910. (org-split-string string "[ \t]+"))))
  7911. (if (not opts)
  7912. rtn
  7913. (setq rtn nil)
  7914. (while (setq e (pop opts))
  7915. (if (not (nth 3 e))
  7916. (setq rtn (nth 2 e))
  7917. (if (not (listp rtn)) (setq rtn nil))
  7918. (push (nth 2 e) rtn)))
  7919. rtn)))))
  7920. (defvar org-todo-setup-filter-hook nil
  7921. "Hook for functions that pre-filter todo specs.
  7922. Each function takes a todo spec and returns either `nil' or the spec
  7923. transformed into canonical form." )
  7924. (defvar org-todo-get-default-hook nil
  7925. "Hook for functions that get a default item for todo.
  7926. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7927. `nil' or a string to be used for the todo mark." )
  7928. (defvar org-agenda-headline-snapshot-before-repeat)
  7929. (defun org-todo (&optional arg)
  7930. "Change the TODO state of an item.
  7931. The state of an item is given by a keyword at the start of the heading,
  7932. like
  7933. *** TODO Write paper
  7934. *** DONE Call mom
  7935. The different keywords are specified in the variable `org-todo-keywords'.
  7936. By default the available states are \"TODO\" and \"DONE\".
  7937. So for this example: when the item starts with TODO, it is changed to DONE.
  7938. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7939. DONE are present, add TODO at the beginning of the heading.
  7940. With C-u prefix arg, use completion to determine the new state.
  7941. With numeric prefix arg, switch to that state.
  7942. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7943. With a tripple C-u prefix, circumvent any state blocking.
  7944. For calling through lisp, arg is also interpreted in the following way:
  7945. 'none -> empty state
  7946. \"\"(empty string) -> switch to empty state
  7947. 'done -> switch to DONE
  7948. 'nextset -> switch to the next set of keywords
  7949. 'previousset -> switch to the previous set of keywords
  7950. \"WAITING\" -> switch to the specified keyword, but only if it
  7951. really is a member of `org-todo-keywords'."
  7952. (interactive "P")
  7953. (if (equal arg '(16)) (setq arg 'nextset))
  7954. (let ((org-blocker-hook org-blocker-hook)
  7955. (case-fold-search nil))
  7956. (when (equal arg '(64))
  7957. (setq arg nil org-blocker-hook nil))
  7958. (when (and org-blocker-hook
  7959. (org-entry-get nil "NOBLOCKING"))
  7960. (setq org-blocker-hook nil))
  7961. (save-excursion
  7962. (catch 'exit
  7963. (org-back-to-heading)
  7964. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7965. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  7966. (looking-at " *"))
  7967. (let* ((match-data (match-data))
  7968. (startpos (point-at-bol))
  7969. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7970. (org-log-done org-log-done)
  7971. (org-log-repeat org-log-repeat)
  7972. (org-todo-log-states org-todo-log-states)
  7973. (this (match-string 1))
  7974. (hl-pos (match-beginning 0))
  7975. (head (org-get-todo-sequence-head this))
  7976. (ass (assoc head org-todo-kwd-alist))
  7977. (interpret (nth 1 ass))
  7978. (done-word (nth 3 ass))
  7979. (final-done-word (nth 4 ass))
  7980. (last-state (or this ""))
  7981. (completion-ignore-case t)
  7982. (member (member this org-todo-keywords-1))
  7983. (tail (cdr member))
  7984. (state (cond
  7985. ((and org-todo-key-trigger
  7986. (or (and (equal arg '(4))
  7987. (eq org-use-fast-todo-selection 'prefix))
  7988. (and (not arg) org-use-fast-todo-selection
  7989. (not (eq org-use-fast-todo-selection
  7990. 'prefix)))))
  7991. ;; Use fast selection
  7992. (org-fast-todo-selection))
  7993. ((and (equal arg '(4))
  7994. (or (not org-use-fast-todo-selection)
  7995. (not org-todo-key-trigger)))
  7996. ;; Read a state with completion
  7997. (org-ido-completing-read
  7998. "State: " (mapcar (lambda(x) (list x))
  7999. org-todo-keywords-1)
  8000. nil t))
  8001. ((eq arg 'right)
  8002. (if this
  8003. (if tail (car tail) nil)
  8004. (car org-todo-keywords-1)))
  8005. ((eq arg 'left)
  8006. (if (equal member org-todo-keywords-1)
  8007. nil
  8008. (if this
  8009. (nth (- (length org-todo-keywords-1)
  8010. (length tail) 2)
  8011. org-todo-keywords-1)
  8012. (org-last org-todo-keywords-1))))
  8013. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8014. (setq arg nil))) ; hack to fall back to cycling
  8015. (arg
  8016. ;; user or caller requests a specific state
  8017. (cond
  8018. ((equal arg "") nil)
  8019. ((eq arg 'none) nil)
  8020. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8021. ((eq arg 'nextset)
  8022. (or (car (cdr (member head org-todo-heads)))
  8023. (car org-todo-heads)))
  8024. ((eq arg 'previousset)
  8025. (let ((org-todo-heads (reverse org-todo-heads)))
  8026. (or (car (cdr (member head org-todo-heads)))
  8027. (car org-todo-heads))))
  8028. ((car (member arg org-todo-keywords-1)))
  8029. ((nth (1- (prefix-numeric-value arg))
  8030. org-todo-keywords-1))))
  8031. ((null member) (or head (car org-todo-keywords-1)))
  8032. ((equal this final-done-word) nil) ;; -> make empty
  8033. ((null tail) nil) ;; -> first entry
  8034. ((memq interpret '(type priority))
  8035. (if (eq this-command last-command)
  8036. (car tail)
  8037. (if (> (length tail) 0)
  8038. (or done-word (car org-done-keywords))
  8039. nil)))
  8040. (t
  8041. (car tail))))
  8042. (state (or
  8043. (run-hook-with-args-until-success
  8044. 'org-todo-get-default-hook state last-state)
  8045. state))
  8046. (next (if state (concat " " state " ") " "))
  8047. (change-plist (list :type 'todo-state-change :from this :to state
  8048. :position startpos))
  8049. dolog now-done-p)
  8050. (when org-blocker-hook
  8051. (setq org-last-todo-state-is-todo
  8052. (not (member this org-done-keywords)))
  8053. (unless (save-excursion
  8054. (save-match-data
  8055. (run-hook-with-args-until-failure
  8056. 'org-blocker-hook change-plist)))
  8057. (if (interactive-p)
  8058. (error "TODO state change from %s to %s blocked" this state)
  8059. ;; fail silently
  8060. (message "TODO state change from %s to %s blocked" this state)
  8061. (throw 'exit nil))))
  8062. (store-match-data match-data)
  8063. (replace-match next t t)
  8064. (unless (pos-visible-in-window-p hl-pos)
  8065. (message "TODO state changed to %s" (org-trim next)))
  8066. (unless head
  8067. (setq head (org-get-todo-sequence-head state)
  8068. ass (assoc head org-todo-kwd-alist)
  8069. interpret (nth 1 ass)
  8070. done-word (nth 3 ass)
  8071. final-done-word (nth 4 ass)))
  8072. (when (memq arg '(nextset previousset))
  8073. (message "Keyword-Set %d/%d: %s"
  8074. (- (length org-todo-sets) -1
  8075. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8076. (length org-todo-sets)
  8077. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8078. (setq org-last-todo-state-is-todo
  8079. (not (member state org-done-keywords)))
  8080. (setq now-done-p (and (member state org-done-keywords)
  8081. (not (member this org-done-keywords))))
  8082. (and logging (org-local-logging logging))
  8083. (when (and (or org-todo-log-states org-log-done)
  8084. (not (memq arg '(nextset previousset))))
  8085. ;; we need to look at recording a time and note
  8086. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8087. (nth 2 (assoc this org-todo-log-states))))
  8088. (when (and state
  8089. (member state org-not-done-keywords)
  8090. (not (member this org-not-done-keywords)))
  8091. ;; This is now a todo state and was not one before
  8092. ;; If there was a CLOSED time stamp, get rid of it.
  8093. (org-add-planning-info nil nil 'closed))
  8094. (when (and now-done-p org-log-done)
  8095. ;; It is now done, and it was not done before
  8096. (org-add-planning-info 'closed (org-current-time))
  8097. (if (and (not dolog) (eq 'note org-log-done))
  8098. (org-add-log-setup 'done state this 'findpos 'note)))
  8099. (when (and state dolog)
  8100. ;; This is a non-nil state, and we need to log it
  8101. (org-add-log-setup 'state state this 'findpos dolog)))
  8102. ;; Fixup tag positioning
  8103. (org-todo-trigger-tag-changes state)
  8104. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8105. (when org-provide-todo-statistics
  8106. (org-update-parent-todo-statistics))
  8107. (run-hooks 'org-after-todo-state-change-hook)
  8108. (if (and arg (not (member state org-done-keywords)))
  8109. (setq head (org-get-todo-sequence-head state)))
  8110. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8111. ;; Do we need to trigger a repeat?
  8112. (when now-done-p
  8113. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8114. ;; This is for the agenda, take a snapshot of the headline.
  8115. (save-match-data
  8116. (setq org-agenda-headline-snapshot-before-repeat
  8117. (org-get-heading))))
  8118. (org-auto-repeat-maybe state))
  8119. ;; Fixup cursor location if close to the keyword
  8120. (if (and (outline-on-heading-p)
  8121. (not (bolp))
  8122. (save-excursion (beginning-of-line 1)
  8123. (looking-at org-todo-line-regexp))
  8124. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8125. (progn
  8126. (goto-char (or (match-end 2) (match-end 1)))
  8127. (and (looking-at " ") (just-one-space))))
  8128. (when org-trigger-hook
  8129. (save-excursion
  8130. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8131. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8132. "Block turning an entry into a TODO, using the hierarchy.
  8133. This checks whether the current task should be blocked from state
  8134. changes. Such blocking occurs when:
  8135. 1. The task has children which are not all in a completed state.
  8136. 2. A task has a parent with the property :ORDERED:, and there
  8137. are siblings prior to the current task with incomplete
  8138. status.
  8139. 3. The parent of the task is blocked because it has siblings that should
  8140. be done first, or is child of a block grandparent TODO entry."
  8141. (catch 'dont-block
  8142. ;; If this is not a todo state change, or if this entry is already DONE,
  8143. ;; do not block
  8144. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8145. (member (plist-get change-plist :from)
  8146. (cons 'done org-done-keywords))
  8147. (member (plist-get change-plist :to)
  8148. (cons 'todo org-not-done-keywords)))
  8149. (throw 'dont-block t))
  8150. ;; If this task has children, and any are undone, it's blocked
  8151. (save-excursion
  8152. (org-back-to-heading t)
  8153. (let ((this-level (funcall outline-level)))
  8154. (outline-next-heading)
  8155. (let ((child-level (funcall outline-level)))
  8156. (while (and (not (eobp))
  8157. (> child-level this-level))
  8158. ;; this todo has children, check whether they are all
  8159. ;; completed
  8160. (if (and (not (org-entry-is-done-p))
  8161. (org-entry-is-todo-p))
  8162. (throw 'dont-block nil))
  8163. (outline-next-heading)
  8164. (setq child-level (funcall outline-level))))))
  8165. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8166. ;; any previous siblings are undone, it's blocked
  8167. (save-excursion
  8168. (org-back-to-heading t)
  8169. (let* ((pos (point))
  8170. (parent-pos (and (org-up-heading-safe) (point))))
  8171. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8172. (when (and (org-entry-get (point) "ORDERED")
  8173. (forward-line 1)
  8174. (re-search-forward org-not-done-heading-regexp pos t))
  8175. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8176. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8177. (while t
  8178. (goto-char parent-pos)
  8179. (if (not (looking-at org-not-done-heading-regexp))
  8180. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8181. (setq pos (point))
  8182. (setq parent-pos (and (org-up-heading-safe) (point)))
  8183. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8184. (when (and (org-entry-get (point) "ORDERED")
  8185. (forward-line 1)
  8186. (re-search-forward org-not-done-heading-regexp pos t))
  8187. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8188. (defcustom org-track-ordered-property-with-tag nil
  8189. "Should the ORDERED property also be shown as a tag?
  8190. The ORDERED property decides if an entry should require subtasks to be
  8191. completed in sequence. Since a property is not very visible, setting
  8192. this option means that toggling the ORDERED property with the command
  8193. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8194. not relevant for the behavior, but it makes things more visible.
  8195. Note that toggling the tag with tags commands will not change the property
  8196. and therefore not influence behavior!
  8197. This can be t, meaning the tag ORDERED should be used, It can also be a
  8198. string to select a different tag for this task."
  8199. :group 'org-todo
  8200. :type '(choice
  8201. (const :tag "No tracking" nil)
  8202. (const :tag "Track with ORDERED tag" t)
  8203. (string :tag "Use other tag")))
  8204. (defun org-toggle-ordered-property ()
  8205. "Toggle the ORDERED property of the current entry.
  8206. For better visibility, you can track the value of this property with a tag.
  8207. See variable `org-track-ordered-property-with-tag'."
  8208. (interactive)
  8209. (let* ((t1 org-track-ordered-property-with-tag)
  8210. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8211. (save-excursion
  8212. (org-back-to-heading)
  8213. (if (org-entry-get nil "ORDERED")
  8214. (progn
  8215. (org-delete-property "ORDERED")
  8216. (and tag (org-toggle-tag tag 'off))
  8217. (message "Subtasks can be completed in arbitrary order"))
  8218. (org-entry-put nil "ORDERED" "t")
  8219. (and tag (org-toggle-tag tag 'on))
  8220. (message "Subtasks must be completed in sequence")))))
  8221. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8222. (defun org-block-todo-from-checkboxes (change-plist)
  8223. "Block turning an entry into a TODO, using checkboxes.
  8224. This checks whether the current task should be blocked from state
  8225. changes because there are uncheckd boxes in this entry."
  8226. (catch 'dont-block
  8227. ;; If this is not a todo state change, or if this entry is already DONE,
  8228. ;; do not block
  8229. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8230. (member (plist-get change-plist :from)
  8231. (cons 'done org-done-keywords))
  8232. (member (plist-get change-plist :to)
  8233. (cons 'todo org-not-done-keywords)))
  8234. (throw 'dont-block t))
  8235. ;; If this task has checkboxes that are not checked, it's blocked
  8236. (save-excursion
  8237. (org-back-to-heading t)
  8238. (let ((beg (point)) end)
  8239. (outline-next-heading)
  8240. (setq end (point))
  8241. (goto-char beg)
  8242. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8243. end t)
  8244. (progn
  8245. (if (boundp 'org-blocked-by-checkboxes)
  8246. (setq org-blocked-by-checkboxes t))
  8247. (throw 'dont-block nil)))))
  8248. t)) ; do not block
  8249. (defun org-update-parent-todo-statistics ()
  8250. "Update any statistics cookie in the parent of the current headline."
  8251. (interactive)
  8252. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8253. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8254. (catch 'exit
  8255. (save-excursion
  8256. (setq level (org-up-heading-safe))
  8257. (unless level
  8258. (throw 'exit nil))
  8259. (while (re-search-forward box-re (point-at-eol) t)
  8260. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8261. (setq is-percent (match-end 2))
  8262. (save-match-data
  8263. (unless (outline-next-heading) (throw 'exit nil))
  8264. (while (looking-at org-todo-line-regexp)
  8265. (setq kwd (match-string 2))
  8266. (and kwd (setq cnt-all (1+ cnt-all)))
  8267. (and (member kwd org-done-keywords)
  8268. (setq cnt-done (1+ cnt-done)))
  8269. (condition-case nil
  8270. (org-forward-same-level 1)
  8271. (error (end-of-line 1)))))
  8272. (replace-match
  8273. (if is-percent
  8274. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8275. (format "[%d/%d]" cnt-done cnt-all))))
  8276. (when cookie-present
  8277. (run-hook-with-args 'org-after-todo-statistics-hook
  8278. cnt-done (- cnt-all cnt-done)))))))
  8279. (defvar org-after-todo-statistics-hook nil
  8280. "Hook that is called after a TODO statistics cookie has been updated.
  8281. Each function is called with two arguments: the number of not-done entries
  8282. and the number of done entries.
  8283. For example, the following function, when added to this hook, will switch
  8284. an entry to DONE when all children are done, and back to TODO when new
  8285. entries are set to a TODO status. Note that this hook is only called
  8286. when there is a statistics cookie in the headline!
  8287. (defun org-summary-todo (n-done n-not-done)
  8288. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8289. (let (org-log-done org-log-states) ; turn off logging
  8290. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8291. ")
  8292. (defun org-todo-trigger-tag-changes (state)
  8293. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8294. (let ((l org-todo-state-tags-triggers)
  8295. changes)
  8296. (when (or (not state) (equal state ""))
  8297. (setq changes (append changes (cdr (assoc "" l)))))
  8298. (when (and (stringp state) (> (length state) 0))
  8299. (setq changes (append changes (cdr (assoc state l)))))
  8300. (when (member state org-not-done-keywords)
  8301. (setq changes (append changes (cdr (assoc 'todo l)))))
  8302. (when (member state org-done-keywords)
  8303. (setq changes (append changes (cdr (assoc 'done l)))))
  8304. (dolist (c changes)
  8305. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8306. (defun org-local-logging (value)
  8307. "Get logging settings from a property VALUE."
  8308. (let* (words w a)
  8309. ;; directly set the variables, they are already local.
  8310. (setq org-log-done nil
  8311. org-log-repeat nil
  8312. org-todo-log-states nil)
  8313. (setq words (org-split-string value))
  8314. (while (setq w (pop words))
  8315. (cond
  8316. ((setq a (assoc w org-startup-options))
  8317. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8318. (set (nth 1 a) (nth 2 a))))
  8319. ((setq a (org-extract-log-state-settings w))
  8320. (and (member (car a) org-todo-keywords-1)
  8321. (push a org-todo-log-states)))))))
  8322. (defun org-get-todo-sequence-head (kwd)
  8323. "Return the head of the TODO sequence to which KWD belongs.
  8324. If KWD is not set, check if there is a text property remembering the
  8325. right sequence."
  8326. (let (p)
  8327. (cond
  8328. ((not kwd)
  8329. (or (get-text-property (point-at-bol) 'org-todo-head)
  8330. (progn
  8331. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8332. nil (point-at-eol)))
  8333. (get-text-property p 'org-todo-head))))
  8334. ((not (member kwd org-todo-keywords-1))
  8335. (car org-todo-keywords-1))
  8336. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8337. (defun org-fast-todo-selection ()
  8338. "Fast TODO keyword selection with single keys.
  8339. Returns the new TODO keyword, or nil if no state change should occur."
  8340. (let* ((fulltable org-todo-key-alist)
  8341. (done-keywords org-done-keywords) ;; needed for the faces.
  8342. (maxlen (apply 'max (mapcar
  8343. (lambda (x)
  8344. (if (stringp (car x)) (string-width (car x)) 0))
  8345. fulltable)))
  8346. (expert nil)
  8347. (fwidth (+ maxlen 3 1 3))
  8348. (ncol (/ (- (window-width) 4) fwidth))
  8349. tg cnt e c tbl
  8350. groups ingroup)
  8351. (save-excursion
  8352. (save-window-excursion
  8353. (if expert
  8354. (set-buffer (get-buffer-create " *Org todo*"))
  8355. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8356. (erase-buffer)
  8357. (org-set-local 'org-done-keywords done-keywords)
  8358. (setq tbl fulltable cnt 0)
  8359. (while (setq e (pop tbl))
  8360. (cond
  8361. ((equal e '(:startgroup))
  8362. (push '() groups) (setq ingroup t)
  8363. (when (not (= cnt 0))
  8364. (setq cnt 0)
  8365. (insert "\n"))
  8366. (insert "{ "))
  8367. ((equal e '(:endgroup))
  8368. (setq ingroup nil cnt 0)
  8369. (insert "}\n"))
  8370. ((equal e '(:newline))
  8371. (when (not (= cnt 0))
  8372. (setq cnt 0)
  8373. (insert "\n")
  8374. (setq e (car tbl))
  8375. (while (equal (car tbl) '(:newline))
  8376. (insert "\n")
  8377. (setq tbl (cdr tbl)))))
  8378. (t
  8379. (setq tg (car e) c (cdr e))
  8380. (if ingroup (push tg (car groups)))
  8381. (setq tg (org-add-props tg nil 'face
  8382. (org-get-todo-face tg)))
  8383. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8384. (insert "[" c "] " tg (make-string
  8385. (- fwidth 4 (length tg)) ?\ ))
  8386. (when (= (setq cnt (1+ cnt)) ncol)
  8387. (insert "\n")
  8388. (if ingroup (insert " "))
  8389. (setq cnt 0)))))
  8390. (insert "\n")
  8391. (goto-char (point-min))
  8392. (if (not expert) (org-fit-window-to-buffer))
  8393. (message "[a-z..]:Set [SPC]:clear")
  8394. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8395. (cond
  8396. ((or (= c ?\C-g)
  8397. (and (= c ?q) (not (rassoc c fulltable))))
  8398. (setq quit-flag t))
  8399. ((= c ?\ ) nil)
  8400. ((setq e (rassoc c fulltable) tg (car e))
  8401. tg)
  8402. (t (setq quit-flag t)))))))
  8403. (defun org-entry-is-todo-p ()
  8404. (member (org-get-todo-state) org-not-done-keywords))
  8405. (defun org-entry-is-done-p ()
  8406. (member (org-get-todo-state) org-done-keywords))
  8407. (defun org-get-todo-state ()
  8408. (save-excursion
  8409. (org-back-to-heading t)
  8410. (and (looking-at org-todo-line-regexp)
  8411. (match-end 2)
  8412. (match-string 2))))
  8413. (defun org-at-date-range-p (&optional inactive-ok)
  8414. "Is the cursor inside a date range?"
  8415. (interactive)
  8416. (save-excursion
  8417. (catch 'exit
  8418. (let ((pos (point)))
  8419. (skip-chars-backward "^[<\r\n")
  8420. (skip-chars-backward "<[")
  8421. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8422. (>= (match-end 0) pos)
  8423. (throw 'exit t))
  8424. (skip-chars-backward "^<[\r\n")
  8425. (skip-chars-backward "<[")
  8426. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8427. (>= (match-end 0) pos)
  8428. (throw 'exit t)))
  8429. nil)))
  8430. (defun org-get-repeat ()
  8431. "Check if there is a deadline/schedule with repeater in this entry."
  8432. (save-match-data
  8433. (save-excursion
  8434. (org-back-to-heading t)
  8435. (if (re-search-forward
  8436. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8437. (match-string 1)))))
  8438. (defvar org-last-changed-timestamp)
  8439. (defvar org-last-inserted-timestamp)
  8440. (defvar org-log-post-message)
  8441. (defvar org-log-note-purpose)
  8442. (defvar org-log-note-how)
  8443. (defvar org-log-note-extra)
  8444. (defun org-auto-repeat-maybe (done-word)
  8445. "Check if the current headline contains a repeated deadline/schedule.
  8446. If yes, set TODO state back to what it was and change the base date
  8447. of repeating deadline/scheduled time stamps to new date.
  8448. This function is run automatically after each state change to a DONE state."
  8449. ;; last-state is dynamically scoped into this function
  8450. (let* ((repeat (org-get-repeat))
  8451. (aa (assoc last-state org-todo-kwd-alist))
  8452. (interpret (nth 1 aa))
  8453. (head (nth 2 aa))
  8454. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8455. (msg "Entry repeats: ")
  8456. (org-log-done nil)
  8457. (org-todo-log-states nil)
  8458. (nshiftmax 10) (nshift 0)
  8459. re type n what ts time)
  8460. (when repeat
  8461. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8462. (org-todo (if (eq interpret 'type) last-state head))
  8463. (when org-log-repeat
  8464. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8465. (memq 'org-add-log-note post-command-hook))
  8466. ;; OK, we are already setup for some record
  8467. (if (eq org-log-repeat 'note)
  8468. ;; make sure we take a note, not only a time stamp
  8469. (setq org-log-note-how 'note))
  8470. ;; Set up for taking a record
  8471. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8472. last-state
  8473. 'findpos org-log-repeat)))
  8474. (org-back-to-heading t)
  8475. (org-add-planning-info nil nil 'closed)
  8476. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8477. org-deadline-time-regexp "\\)\\|\\("
  8478. org-ts-regexp "\\)"))
  8479. (while (re-search-forward
  8480. re (save-excursion (outline-next-heading) (point)) t)
  8481. (setq type (if (match-end 1) org-scheduled-string
  8482. (if (match-end 3) org-deadline-string "Plain:"))
  8483. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8484. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8485. (setq n (string-to-number (match-string 2 ts))
  8486. what (match-string 3 ts))
  8487. (if (equal what "w") (setq n (* n 7) what "d"))
  8488. ;; Preparation, see if we need to modify the start date for the change
  8489. (when (match-end 1)
  8490. (setq time (save-match-data (org-time-string-to-time ts)))
  8491. (cond
  8492. ((equal (match-string 1 ts) ".")
  8493. ;; Shift starting date to today
  8494. (org-timestamp-change
  8495. (- (time-to-days (current-time)) (time-to-days time))
  8496. 'day))
  8497. ((equal (match-string 1 ts) "+")
  8498. (while (or (= nshift 0)
  8499. (<= (time-to-days time) (time-to-days (current-time))))
  8500. (when (= (incf nshift) nshiftmax)
  8501. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8502. (error "Abort")))
  8503. (org-timestamp-change n (cdr (assoc what whata)))
  8504. (org-at-timestamp-p t)
  8505. (setq ts (match-string 1))
  8506. (setq time (save-match-data (org-time-string-to-time ts))))
  8507. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8508. ;; rematch, so that we have everything in place for the real shift
  8509. (org-at-timestamp-p t)
  8510. (setq ts (match-string 1))
  8511. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8512. (org-timestamp-change n (cdr (assoc what whata)))
  8513. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8514. (setq org-log-post-message msg)
  8515. (message "%s" msg))))
  8516. (defun org-show-todo-tree (arg)
  8517. "Make a compact tree which shows all headlines marked with TODO.
  8518. The tree will show the lines where the regexp matches, and all higher
  8519. headlines above the match.
  8520. With a \\[universal-argument] prefix, also show the DONE entries.
  8521. With a numeric prefix N, construct a sparse tree for the Nth element
  8522. of `org-todo-keywords-1'."
  8523. (interactive "P")
  8524. (let ((case-fold-search nil)
  8525. (kwd-re
  8526. (cond ((null arg) org-not-done-regexp)
  8527. ((equal arg '(4))
  8528. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8529. (mapcar 'list org-todo-keywords-1))))
  8530. (concat "\\("
  8531. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8532. "\\)\\>")))
  8533. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8534. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8535. org-todo-keywords-1)))
  8536. (t (error "Invalid prefix argument: %s" arg)))))
  8537. (message "%d TODO entries found"
  8538. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8539. (defun org-deadline (&optional remove time)
  8540. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8541. With argument REMOVE, remove any deadline from the item.
  8542. When TIME is set, it should be an internal time specification, and the
  8543. scheduling will use the corresponding date."
  8544. (interactive "P")
  8545. (if remove
  8546. (progn
  8547. (org-remove-timestamp-with-keyword org-deadline-string)
  8548. (message "Item no longer has a deadline."))
  8549. (if (org-get-repeat)
  8550. (error "Cannot change deadline on task with repeater, please do that by hand")
  8551. (org-add-planning-info 'deadline time 'closed)
  8552. (message "Deadline on %s" org-last-inserted-timestamp))))
  8553. (defun org-schedule (&optional remove time)
  8554. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8555. With argument REMOVE, remove any scheduling date from the item.
  8556. When TIME is set, it should be an internal time specification, and the
  8557. scheduling will use the corresponding date."
  8558. (interactive "P")
  8559. (if remove
  8560. (progn
  8561. (org-remove-timestamp-with-keyword org-scheduled-string)
  8562. (message "Item is no longer scheduled."))
  8563. (if (org-get-repeat)
  8564. (error "Cannot reschedule task with repeater, please do that by hand")
  8565. (org-add-planning-info 'scheduled time 'closed)
  8566. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8567. (defun org-get-scheduled-time (pom &optional inherit)
  8568. "Get the scheduled time as a time tuple, of a format suitable
  8569. for calling org-schedule with, or if there is no scheduling,
  8570. returns nil."
  8571. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8572. (when time
  8573. (apply 'encode-time (org-parse-time-string time)))))
  8574. (defun org-get-deadline-time (pom &optional inherit)
  8575. "Get the deadine as a time tuple, of a format suitable for
  8576. calling org-deadlin with, or if there is no scheduling, returns
  8577. nil."
  8578. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8579. (when time
  8580. (apply 'encode-time (org-parse-time-string time)))))
  8581. (defun org-remove-timestamp-with-keyword (keyword)
  8582. "Remove all time stamps with KEYWORD in the current entry."
  8583. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8584. beg)
  8585. (save-excursion
  8586. (org-back-to-heading t)
  8587. (setq beg (point))
  8588. (org-end-of-subtree t t)
  8589. (while (re-search-backward re beg t)
  8590. (replace-match "")
  8591. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8592. (equal (char-before) ?\ ))
  8593. (backward-delete-char 1)
  8594. (if (string-match "^[ \t]*$" (buffer-substring
  8595. (point-at-bol) (point-at-eol)))
  8596. (delete-region (point-at-bol)
  8597. (min (point-max) (1+ (point-at-eol))))))))))
  8598. (defun org-add-planning-info (what &optional time &rest remove)
  8599. "Insert new timestamp with keyword in the line directly after the headline.
  8600. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8601. If non is given, the user is prompted for a date.
  8602. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8603. be removed."
  8604. (interactive)
  8605. (let (org-time-was-given org-end-time-was-given ts
  8606. end default-time default-input)
  8607. (catch 'exit
  8608. (when (and (not time) (memq what '(scheduled deadline)))
  8609. ;; Try to get a default date/time from existing timestamp
  8610. (save-excursion
  8611. (org-back-to-heading t)
  8612. (setq end (save-excursion (outline-next-heading) (point)))
  8613. (when (re-search-forward (if (eq what 'scheduled)
  8614. org-scheduled-time-regexp
  8615. org-deadline-time-regexp)
  8616. end t)
  8617. (setq ts (match-string 1)
  8618. default-time
  8619. (apply 'encode-time (org-parse-time-string ts))
  8620. default-input (and ts (org-get-compact-tod ts))))))
  8621. (when what
  8622. ;; If necessary, get the time from the user
  8623. (setq time (or time (org-read-date nil 'to-time nil nil
  8624. default-time default-input))))
  8625. (when (and org-insert-labeled-timestamps-at-point
  8626. (member what '(scheduled deadline)))
  8627. (insert
  8628. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8629. (org-insert-time-stamp time org-time-was-given
  8630. nil nil nil (list org-end-time-was-given))
  8631. (setq what nil))
  8632. (save-excursion
  8633. (save-restriction
  8634. (let (col list elt ts buffer-invisibility-spec)
  8635. (org-back-to-heading t)
  8636. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8637. (goto-char (match-end 1))
  8638. (setq col (current-column))
  8639. (goto-char (match-end 0))
  8640. (if (eobp) (insert "\n") (forward-char 1))
  8641. (when (and (not what)
  8642. (not (looking-at
  8643. (concat "[ \t]*"
  8644. org-keyword-time-not-clock-regexp))))
  8645. ;; Nothing to add, nothing to remove...... :-)
  8646. (throw 'exit nil))
  8647. (if (and (not (looking-at outline-regexp))
  8648. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8649. "[^\r\n]*"))
  8650. (not (equal (match-string 1) org-clock-string)))
  8651. (narrow-to-region (match-beginning 0) (match-end 0))
  8652. (insert-before-markers "\n")
  8653. (backward-char 1)
  8654. (narrow-to-region (point) (point))
  8655. (and org-adapt-indentation (org-indent-to-column col)))
  8656. ;; Check if we have to remove something.
  8657. (setq list (cons what remove))
  8658. (while list
  8659. (setq elt (pop list))
  8660. (goto-char (point-min))
  8661. (when (or (and (eq elt 'scheduled)
  8662. (re-search-forward org-scheduled-time-regexp nil t))
  8663. (and (eq elt 'deadline)
  8664. (re-search-forward org-deadline-time-regexp nil t))
  8665. (and (eq elt 'closed)
  8666. (re-search-forward org-closed-time-regexp nil t)))
  8667. (replace-match "")
  8668. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8669. (if (looking-at " +") (replace-match ""))))
  8670. (goto-char (point-max))
  8671. (when what
  8672. (insert
  8673. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8674. (cond ((eq what 'scheduled) org-scheduled-string)
  8675. ((eq what 'deadline) org-deadline-string)
  8676. ((eq what 'closed) org-closed-string))
  8677. " ")
  8678. (setq ts (org-insert-time-stamp
  8679. time
  8680. (or org-time-was-given
  8681. (and (eq what 'closed) org-log-done-with-time))
  8682. (eq what 'closed)
  8683. nil nil (list org-end-time-was-given)))
  8684. (end-of-line 1))
  8685. (goto-char (point-min))
  8686. (widen)
  8687. (if (and (looking-at "[ \t]+\n")
  8688. (equal (char-before) ?\n))
  8689. (delete-region (1- (point)) (point-at-eol)))
  8690. ts))))))
  8691. (defvar org-log-note-marker (make-marker))
  8692. (defvar org-log-note-purpose nil)
  8693. (defvar org-log-note-state nil)
  8694. (defvar org-log-note-previous-state nil)
  8695. (defvar org-log-note-how nil)
  8696. (defvar org-log-note-extra nil)
  8697. (defvar org-log-note-window-configuration nil)
  8698. (defvar org-log-note-return-to (make-marker))
  8699. (defvar org-log-post-message nil
  8700. "Message to be displayed after a log note has been stored.
  8701. The auto-repeater uses this.")
  8702. (defun org-add-note ()
  8703. "Add a note to the current entry.
  8704. This is done in the same way as adding a state change note."
  8705. (interactive)
  8706. (org-add-log-setup 'note nil nil 'findpos nil))
  8707. (defvar org-property-end-re)
  8708. (defun org-add-log-setup (&optional purpose state prev-state
  8709. findpos how &optional extra)
  8710. "Set up the post command hook to take a note.
  8711. If this is about to TODO state change, the new state is expected in STATE.
  8712. When FINDPOS is non-nil, find the correct position for the note in
  8713. the current entry. If not, assume that it can be inserted at point.
  8714. HOW is an indicator what kind of note should be created.
  8715. EXTRA is additional text that will be inserted into the notes buffer."
  8716. (let ((drawer (cond ((stringp org-log-into-drawer)
  8717. org-log-into-drawer)
  8718. (org-log-into-drawer "LOGBOOK")
  8719. (t nil))))
  8720. (save-restriction
  8721. (save-excursion
  8722. (when findpos
  8723. (org-back-to-heading t)
  8724. (narrow-to-region (point) (save-excursion
  8725. (outline-next-heading) (point)))
  8726. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8727. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8728. "[^\r\n]*\\)?"))
  8729. (goto-char (match-end 0))
  8730. (cond
  8731. (drawer
  8732. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8733. nil t)
  8734. (progn
  8735. (goto-char (match-end 0))
  8736. (or org-log-states-order-reversed
  8737. (and (re-search-forward org-property-end-re nil t)
  8738. (goto-char (1- (match-beginning 0))))))
  8739. (insert "\n:" drawer ":\n:END:")
  8740. (beginning-of-line 0)
  8741. (org-indent-line-function)
  8742. (beginning-of-line 2)
  8743. (org-indent-line-function)
  8744. (end-of-line 0)))
  8745. ((and org-log-state-notes-insert-after-drawers
  8746. (save-excursion
  8747. (forward-line) (looking-at org-drawer-regexp)))
  8748. (forward-line)
  8749. (while (looking-at org-drawer-regexp)
  8750. (goto-char (match-end 0))
  8751. (re-search-forward org-property-end-re (point-max) t)
  8752. (forward-line))
  8753. (forward-line -1)))
  8754. (unless org-log-states-order-reversed
  8755. (and (= (char-after) ?\n) (forward-char 1))
  8756. (org-skip-over-state-notes)
  8757. (skip-chars-backward " \t\n\r")))
  8758. (move-marker org-log-note-marker (point))
  8759. (setq org-log-note-purpose purpose
  8760. org-log-note-state state
  8761. org-log-note-previous-state prev-state
  8762. org-log-note-how how
  8763. org-log-note-extra extra)
  8764. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8765. (defun org-skip-over-state-notes ()
  8766. "Skip past the list of State notes in an entry."
  8767. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8768. (while (looking-at "[ \t]*- State")
  8769. (condition-case nil
  8770. (org-next-item)
  8771. (error (org-end-of-item)))))
  8772. (defun org-add-log-note (&optional purpose)
  8773. "Pop up a window for taking a note, and add this note later at point."
  8774. (remove-hook 'post-command-hook 'org-add-log-note)
  8775. (setq org-log-note-window-configuration (current-window-configuration))
  8776. (delete-other-windows)
  8777. (move-marker org-log-note-return-to (point))
  8778. (switch-to-buffer (marker-buffer org-log-note-marker))
  8779. (goto-char org-log-note-marker)
  8780. (org-switch-to-buffer-other-window "*Org Note*")
  8781. (erase-buffer)
  8782. (if (memq org-log-note-how '(time state))
  8783. (let (current-prefix-arg) (org-store-log-note))
  8784. (let ((org-inhibit-startup t)) (org-mode))
  8785. (insert (format "# Insert note for %s.
  8786. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8787. (cond
  8788. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8789. ((eq org-log-note-purpose 'done) "closed todo item")
  8790. ((eq org-log-note-purpose 'state)
  8791. (format "state change from \"%s\" to \"%s\""
  8792. (or org-log-note-previous-state "")
  8793. (or org-log-note-state "")))
  8794. ((eq org-log-note-purpose 'note)
  8795. "this entry")
  8796. (t (error "This should not happen")))))
  8797. (if org-log-note-extra (insert org-log-note-extra))
  8798. (org-set-local 'org-finish-function 'org-store-log-note)))
  8799. (defvar org-note-abort nil) ; dynamically scoped
  8800. (defun org-store-log-note ()
  8801. "Finish taking a log note, and insert it to where it belongs."
  8802. (let ((txt (buffer-string))
  8803. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8804. lines ind)
  8805. (kill-buffer (current-buffer))
  8806. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8807. (setq txt (replace-match "" t t txt)))
  8808. (if (string-match "\\s-+\\'" txt)
  8809. (setq txt (replace-match "" t t txt)))
  8810. (setq lines (org-split-string txt "\n"))
  8811. (when (and note (string-match "\\S-" note))
  8812. (setq note
  8813. (org-replace-escapes
  8814. note
  8815. (list (cons "%u" (user-login-name))
  8816. (cons "%U" user-full-name)
  8817. (cons "%t" (format-time-string
  8818. (org-time-stamp-format 'long 'inactive)
  8819. (current-time)))
  8820. (cons "%s" (if org-log-note-state
  8821. (concat "\"" org-log-note-state "\"")
  8822. ""))
  8823. (cons "%S" (if org-log-note-previous-state
  8824. (concat "\"" org-log-note-previous-state "\"")
  8825. "\"\"")))))
  8826. (if lines (setq note (concat note " \\\\")))
  8827. (push note lines))
  8828. (when (or current-prefix-arg org-note-abort)
  8829. (when org-log-into-drawer
  8830. (org-remove-empty-drawer-at
  8831. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8832. org-log-note-marker))
  8833. (setq lines nil))
  8834. (when lines
  8835. (save-excursion
  8836. (set-buffer (marker-buffer org-log-note-marker))
  8837. (save-excursion
  8838. (goto-char org-log-note-marker)
  8839. (move-marker org-log-note-marker nil)
  8840. (end-of-line 1)
  8841. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8842. (insert "- " (pop lines))
  8843. (org-indent-line-function)
  8844. (beginning-of-line 1)
  8845. (looking-at "[ \t]*")
  8846. (setq ind (concat (match-string 0) " "))
  8847. (end-of-line 1)
  8848. (while lines (insert "\n" ind (pop lines)))
  8849. (message "Note stored")
  8850. (org-back-to-heading t)
  8851. (org-cycle-hide-drawers 'children)))))
  8852. (set-window-configuration org-log-note-window-configuration)
  8853. (with-current-buffer (marker-buffer org-log-note-return-to)
  8854. (goto-char org-log-note-return-to))
  8855. (move-marker org-log-note-return-to nil)
  8856. (and org-log-post-message (message "%s" org-log-post-message)))
  8857. (defun org-remove-empty-drawer-at (drawer pos)
  8858. "Remove an emptyr DARWER drawer at position POS.
  8859. POS may also be a marker."
  8860. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8861. (save-excursion
  8862. (save-restriction
  8863. (widen)
  8864. (goto-char pos)
  8865. (if (org-in-regexp
  8866. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8867. (replace-match ""))))))
  8868. (defun org-sparse-tree (&optional arg)
  8869. "Create a sparse tree, prompt for the details.
  8870. This command can create sparse trees. You first need to select the type
  8871. of match used to create the tree:
  8872. t Show entries with a specific TODO keyword.
  8873. m Show entries selected by a tags/property match.
  8874. p Enter a property name and its value (both with completion on existing
  8875. names/values) and show entries with that property.
  8876. r Show entries matching a regular expression.
  8877. d Show deadlines due within `org-deadline-warning-days'.
  8878. b Show deadlines and scheduled items before a date.
  8879. a Show deadlines and scheduled items after a date."
  8880. (interactive "P")
  8881. (let (ans kwd value)
  8882. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  8883. (setq ans (read-char-exclusive))
  8884. (cond
  8885. ((equal ans ?d)
  8886. (call-interactively 'org-check-deadlines))
  8887. ((equal ans ?b)
  8888. (call-interactively 'org-check-before-date))
  8889. ((equal ans ?a)
  8890. (call-interactively 'org-check-after-date))
  8891. ((equal ans ?t)
  8892. (org-show-todo-tree '(4)))
  8893. ((member ans '(?T ?m))
  8894. (call-interactively 'org-match-sparse-tree))
  8895. ((member ans '(?p ?P))
  8896. (setq kwd (org-ido-completing-read "Property: "
  8897. (mapcar 'list (org-buffer-property-keys))))
  8898. (setq value (org-ido-completing-read "Value: "
  8899. (mapcar 'list (org-property-values kwd))))
  8900. (unless (string-match "\\`{.*}\\'" value)
  8901. (setq value (concat "\"" value "\"")))
  8902. (org-match-sparse-tree arg (concat kwd "=" value)))
  8903. ((member ans '(?r ?R ?/))
  8904. (call-interactively 'org-occur))
  8905. (t (error "No such sparse tree command \"%c\"" ans)))))
  8906. (defvar org-occur-highlights nil
  8907. "List of overlays used for occur matches.")
  8908. (make-variable-buffer-local 'org-occur-highlights)
  8909. (defvar org-occur-parameters nil
  8910. "Parameters of the active org-occur calls.
  8911. This is a list, each call to org-occur pushes as cons cell,
  8912. containing the regular expression and the callback, onto the list.
  8913. The list can contain several entries if `org-occur' has been called
  8914. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8915. will only contain one set of parameters. When the highlights are
  8916. removed (for example with `C-c C-c', or with the next edit (depending
  8917. on `org-remove-highlights-with-change'), this variable is emptied
  8918. as well.")
  8919. (make-variable-buffer-local 'org-occur-parameters)
  8920. (defun org-occur (regexp &optional keep-previous callback)
  8921. "Make a compact tree which shows all matches of REGEXP.
  8922. The tree will show the lines where the regexp matches, and all higher
  8923. headlines above the match. It will also show the heading after the match,
  8924. to make sure editing the matching entry is easy.
  8925. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8926. call to `org-occur' will be kept, to allow stacking of calls to this
  8927. command.
  8928. If CALLBACK is non-nil, it is a function which is called to confirm
  8929. that the match should indeed be shown."
  8930. (interactive "sRegexp: \nP")
  8931. (unless keep-previous
  8932. (org-remove-occur-highlights nil nil t))
  8933. (push (cons regexp callback) org-occur-parameters)
  8934. (let ((cnt 0))
  8935. (save-excursion
  8936. (goto-char (point-min))
  8937. (if (or (not keep-previous) ; do not want to keep
  8938. (not org-occur-highlights)) ; no previous matches
  8939. ;; hide everything
  8940. (org-overview))
  8941. (while (re-search-forward regexp nil t)
  8942. (when (or (not callback)
  8943. (save-match-data (funcall callback)))
  8944. (setq cnt (1+ cnt))
  8945. (when org-highlight-sparse-tree-matches
  8946. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8947. (org-show-context 'occur-tree))))
  8948. (when org-remove-highlights-with-change
  8949. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8950. nil 'local))
  8951. (unless org-sparse-tree-open-archived-trees
  8952. (org-hide-archived-subtrees (point-min) (point-max)))
  8953. (run-hooks 'org-occur-hook)
  8954. (if (interactive-p)
  8955. (message "%d match(es) for regexp %s" cnt regexp))
  8956. cnt))
  8957. (defun org-show-context (&optional key)
  8958. "Make sure point and context and visible.
  8959. How much context is shown depends upon the variables
  8960. `org-show-hierarchy-above', `org-show-following-heading'. and
  8961. `org-show-siblings'."
  8962. (let ((heading-p (org-on-heading-p t))
  8963. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8964. (following-p (org-get-alist-option org-show-following-heading key))
  8965. (entry-p (org-get-alist-option org-show-entry-below key))
  8966. (siblings-p (org-get-alist-option org-show-siblings key)))
  8967. (catch 'exit
  8968. ;; Show heading or entry text
  8969. (if (and heading-p (not entry-p))
  8970. (org-flag-heading nil) ; only show the heading
  8971. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8972. (org-show-hidden-entry))) ; show entire entry
  8973. (when following-p
  8974. ;; Show next sibling, or heading below text
  8975. (save-excursion
  8976. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8977. (org-flag-heading nil))))
  8978. (when siblings-p (org-show-siblings))
  8979. (when hierarchy-p
  8980. ;; show all higher headings, possibly with siblings
  8981. (save-excursion
  8982. (while (and (condition-case nil
  8983. (progn (org-up-heading-all 1) t)
  8984. (error nil))
  8985. (not (bobp)))
  8986. (org-flag-heading nil)
  8987. (when siblings-p (org-show-siblings))))))))
  8988. (defun org-reveal (&optional siblings)
  8989. "Show current entry, hierarchy above it, and the following headline.
  8990. This can be used to show a consistent set of context around locations
  8991. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8992. not t for the search context.
  8993. With optional argument SIBLINGS, on each level of the hierarchy all
  8994. siblings are shown. This repairs the tree structure to what it would
  8995. look like when opened with hierarchical calls to `org-cycle'."
  8996. (interactive "P")
  8997. (let ((org-show-hierarchy-above t)
  8998. (org-show-following-heading t)
  8999. (org-show-siblings (if siblings t org-show-siblings)))
  9000. (org-show-context nil)))
  9001. (defun org-highlight-new-match (beg end)
  9002. "Highlight from BEG to END and mark the highlight is an occur headline."
  9003. (let ((ov (org-make-overlay beg end)))
  9004. (org-overlay-put ov 'face 'secondary-selection)
  9005. (push ov org-occur-highlights)))
  9006. (defun org-remove-occur-highlights (&optional beg end noremove)
  9007. "Remove the occur highlights from the buffer.
  9008. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9009. from the `before-change-functions' in the current buffer."
  9010. (interactive)
  9011. (unless org-inhibit-highlight-removal
  9012. (mapc 'org-delete-overlay org-occur-highlights)
  9013. (setq org-occur-highlights nil)
  9014. (setq org-occur-parameters nil)
  9015. (unless noremove
  9016. (remove-hook 'before-change-functions
  9017. 'org-remove-occur-highlights 'local))))
  9018. ;;;; Priorities
  9019. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9020. "Regular expression matching the priority indicator.")
  9021. (defvar org-remove-priority-next-time nil)
  9022. (defun org-priority-up ()
  9023. "Increase the priority of the current item."
  9024. (interactive)
  9025. (org-priority 'up))
  9026. (defun org-priority-down ()
  9027. "Decrease the priority of the current item."
  9028. (interactive)
  9029. (org-priority 'down))
  9030. (defun org-priority (&optional action)
  9031. "Change the priority of an item by ARG.
  9032. ACTION can be `set', `up', `down', or a character."
  9033. (interactive)
  9034. (setq action (or action 'set))
  9035. (let (current new news have remove)
  9036. (save-excursion
  9037. (org-back-to-heading t)
  9038. (if (looking-at org-priority-regexp)
  9039. (setq current (string-to-char (match-string 2))
  9040. have t)
  9041. (setq current org-default-priority))
  9042. (cond
  9043. ((or (eq action 'set)
  9044. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9045. (if (not (eq action 'set))
  9046. (setq new action)
  9047. (message "Priority %c-%c, SPC to remove: "
  9048. org-highest-priority org-lowest-priority)
  9049. (setq new (read-char-exclusive)))
  9050. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9051. (= (upcase org-lowest-priority) org-lowest-priority))
  9052. (setq new (upcase new)))
  9053. (cond ((equal new ?\ ) (setq remove t))
  9054. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9055. (error "Priority must be between `%c' and `%c'"
  9056. org-highest-priority org-lowest-priority))))
  9057. ((eq action 'up)
  9058. (if (and (not have) (eq last-command this-command))
  9059. (setq new org-lowest-priority)
  9060. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9061. org-default-priority (1- current)))))
  9062. ((eq action 'down)
  9063. (if (and (not have) (eq last-command this-command))
  9064. (setq new org-highest-priority)
  9065. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9066. org-default-priority (1+ current)))))
  9067. (t (error "Invalid action")))
  9068. (if (or (< (upcase new) org-highest-priority)
  9069. (> (upcase new) org-lowest-priority))
  9070. (setq remove t))
  9071. (setq news (format "%c" new))
  9072. (if have
  9073. (if remove
  9074. (replace-match "" t t nil 1)
  9075. (replace-match news t t nil 2))
  9076. (if remove
  9077. (error "No priority cookie found in line")
  9078. (let ((case-fold-search nil))
  9079. (looking-at org-todo-line-regexp))
  9080. (if (match-end 2)
  9081. (progn
  9082. (goto-char (match-end 2))
  9083. (insert " [#" news "]"))
  9084. (goto-char (match-beginning 3))
  9085. (insert "[#" news "] ")))))
  9086. (org-preserve-lc (org-set-tags nil 'align))
  9087. (if remove
  9088. (message "Priority removed")
  9089. (message "Priority of current item set to %s" news))))
  9090. (defun org-get-priority (s)
  9091. "Find priority cookie and return priority."
  9092. (save-match-data
  9093. (if (not (string-match org-priority-regexp s))
  9094. (* 1000 (- org-lowest-priority org-default-priority))
  9095. (* 1000 (- org-lowest-priority
  9096. (string-to-char (match-string 2 s)))))))
  9097. ;;;; Tags
  9098. (defvar org-agenda-archives-mode)
  9099. (defvar org-map-continue-from nil
  9100. "Position from where mapping should continue.
  9101. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9102. (defvar org-scanner-tags nil
  9103. "The current tag list while the tags scanner is running.")
  9104. (defvar org-trust-scanner-tags nil
  9105. "Should `org-get-tags-at' use the tags fro the scanner.
  9106. This is for internal dynamical scoping only.
  9107. When this is non-nil, the function `org-get-tags-at' will return the value
  9108. of `org-scanner-tags' instead of building the list by itself. This
  9109. can lead to large speed-ups when the tags scanner is used in a file with
  9110. many entries, and when the list of tags is retrieved, for example to
  9111. obtain a list of properties. Building the tags list for each entry in such
  9112. a file becomes an N^2 operation - but with this variable set, it scales
  9113. as N.")
  9114. (defun org-scan-tags (action matcher &optional todo-only)
  9115. "Scan headline tags with inheritance and produce output ACTION.
  9116. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9117. or `agenda' to produce an entry list for an agenda view. It can also be
  9118. a Lisp form or a function that should be called at each matched headline, in
  9119. this case the return value is a list of all return values from these calls.
  9120. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9121. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9122. only lines with a TODO keyword are included in the output."
  9123. (require 'org-agenda)
  9124. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9125. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9126. (org-re
  9127. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9128. (props (list 'face 'default
  9129. 'done-face 'org-agenda-done
  9130. 'undone-face 'default
  9131. 'mouse-face 'highlight
  9132. 'org-not-done-regexp org-not-done-regexp
  9133. 'org-todo-regexp org-todo-regexp
  9134. 'keymap org-agenda-keymap
  9135. 'help-echo
  9136. (format "mouse-2 or RET jump to org file %s"
  9137. (abbreviate-file-name
  9138. (or (buffer-file-name (buffer-base-buffer))
  9139. (buffer-name (buffer-base-buffer)))))))
  9140. (case-fold-search nil)
  9141. (org-map-continue-from nil)
  9142. lspos tags tags-list
  9143. (tags-alist (list (cons 0 org-file-tags)))
  9144. (llast 0) rtn rtn1 level category i txt
  9145. todo marker entry priority)
  9146. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9147. (setq action (list 'lambda nil action)))
  9148. (save-excursion
  9149. (goto-char (point-min))
  9150. (when (eq action 'sparse-tree)
  9151. (org-overview)
  9152. (org-remove-occur-highlights))
  9153. (while (re-search-forward re nil t)
  9154. (catch :skip
  9155. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9156. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9157. (goto-char (setq lspos (match-beginning 0)))
  9158. (setq level (org-reduced-level (funcall outline-level))
  9159. category (org-get-category))
  9160. (setq i llast llast level)
  9161. ;; remove tag lists from same and sublevels
  9162. (while (>= i level)
  9163. (when (setq entry (assoc i tags-alist))
  9164. (setq tags-alist (delete entry tags-alist)))
  9165. (setq i (1- i)))
  9166. ;; add the next tags
  9167. (when tags
  9168. (setq tags (org-split-string tags ":")
  9169. tags-alist
  9170. (cons (cons level tags) tags-alist)))
  9171. ;; compile tags for current headline
  9172. (setq tags-list
  9173. (if org-use-tag-inheritance
  9174. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9175. tags)
  9176. org-scanner-tags tags-list)
  9177. (when org-use-tag-inheritance
  9178. (setcdr (car tags-alist)
  9179. (mapcar (lambda (x)
  9180. (setq x (copy-sequence x))
  9181. (org-add-prop-inherited x))
  9182. (cdar tags-alist))))
  9183. (when (and tags org-use-tag-inheritance
  9184. (or (not (eq t org-use-tag-inheritance))
  9185. org-tags-exclude-from-inheritance))
  9186. ;; selective inheritance, remove uninherited ones
  9187. (setcdr (car tags-alist)
  9188. (org-remove-uniherited-tags (cdar tags-alist))))
  9189. (when (and (or (not todo-only)
  9190. (and (member todo org-not-done-keywords)
  9191. (or (not org-agenda-tags-todo-honor-ignore-options)
  9192. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9193. (let ((case-fold-search t)) (eval matcher))
  9194. (or
  9195. (not (member org-archive-tag tags-list))
  9196. ;; we have an archive tag, should we use this anyway?
  9197. (or (not org-agenda-skip-archived-trees)
  9198. (and (eq action 'agenda) org-agenda-archives-mode))))
  9199. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9200. ;; select this headline
  9201. (cond
  9202. ((eq action 'sparse-tree)
  9203. (and org-highlight-sparse-tree-matches
  9204. (org-get-heading) (match-end 0)
  9205. (org-highlight-new-match
  9206. (match-beginning 0) (match-beginning 1)))
  9207. (org-show-context 'tags-tree))
  9208. ((eq action 'agenda)
  9209. (setq txt (org-format-agenda-item
  9210. ""
  9211. (concat
  9212. (if (eq org-tags-match-list-sublevels 'indented)
  9213. (make-string (1- level) ?.) "")
  9214. (org-get-heading))
  9215. category
  9216. tags-list
  9217. )
  9218. priority (org-get-priority txt))
  9219. (goto-char lspos)
  9220. (setq marker (org-agenda-new-marker))
  9221. (org-add-props txt props
  9222. 'org-marker marker 'org-hd-marker marker 'org-category category
  9223. 'todo-state todo
  9224. 'priority priority 'type "tagsmatch")
  9225. (push txt rtn))
  9226. ((functionp action)
  9227. (setq org-map-continue-from nil)
  9228. (save-excursion
  9229. (setq rtn1 (funcall action))
  9230. (push rtn1 rtn)))
  9231. (t (error "Invalid action")))
  9232. ;; if we are to skip sublevels, jump to end of subtree
  9233. (unless org-tags-match-list-sublevels
  9234. (org-end-of-subtree t)
  9235. (backward-char 1))))
  9236. ;; Get the correct position from where to continue
  9237. (if org-map-continue-from
  9238. (goto-char org-map-continue-from)
  9239. (and (= (point) lspos) (end-of-line 1)))))
  9240. (when (and (eq action 'sparse-tree)
  9241. (not org-sparse-tree-open-archived-trees))
  9242. (org-hide-archived-subtrees (point-min) (point-max)))
  9243. (nreverse rtn)))
  9244. (defun org-remove-uniherited-tags (tags)
  9245. "Remove all tags that are not inherited from the list TAGS."
  9246. (cond
  9247. ((eq org-use-tag-inheritance t)
  9248. (if org-tags-exclude-from-inheritance
  9249. (org-delete-all org-tags-exclude-from-inheritance tags)
  9250. tags))
  9251. ((not org-use-tag-inheritance) nil)
  9252. ((stringp org-use-tag-inheritance)
  9253. (delq nil (mapcar
  9254. (lambda (x)
  9255. (if (and (string-match org-use-tag-inheritance x)
  9256. (not (member x org-tags-exclude-from-inheritance)))
  9257. x nil))
  9258. tags)))
  9259. ((listp org-use-tag-inheritance)
  9260. (delq nil (mapcar
  9261. (lambda (x)
  9262. (if (member x org-use-tag-inheritance) x nil))
  9263. tags)))))
  9264. (defvar todo-only) ;; dynamically scoped
  9265. (defun org-match-sparse-tree (&optional todo-only match)
  9266. "Create a sparse tree according to tags string MATCH.
  9267. MATCH can contain positive and negative selection of tags, like
  9268. \"+WORK+URGENT-WITHBOSS\".
  9269. If optional argument TODO-ONLY is non-nil, only select lines that are
  9270. also TODO lines."
  9271. (interactive "P")
  9272. (org-prepare-agenda-buffers (list (current-buffer)))
  9273. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9274. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9275. (defvar org-cached-props nil)
  9276. (defun org-cached-entry-get (pom property)
  9277. (if (or (eq t org-use-property-inheritance)
  9278. (and (stringp org-use-property-inheritance)
  9279. (string-match org-use-property-inheritance property))
  9280. (and (listp org-use-property-inheritance)
  9281. (member property org-use-property-inheritance)))
  9282. ;; Caching is not possible, check it directly
  9283. (org-entry-get pom property 'inherit)
  9284. ;; Get all properties, so that we can do complicated checks easily
  9285. (cdr (assoc property (or org-cached-props
  9286. (setq org-cached-props
  9287. (org-entry-properties pom)))))))
  9288. (defun org-global-tags-completion-table (&optional files)
  9289. "Return the list of all tags in all agenda buffer/files."
  9290. (save-excursion
  9291. (org-uniquify
  9292. (delq nil
  9293. (apply 'append
  9294. (mapcar
  9295. (lambda (file)
  9296. (set-buffer (find-file-noselect file))
  9297. (append (org-get-buffer-tags)
  9298. (mapcar (lambda (x) (if (stringp (car-safe x))
  9299. (list (car-safe x)) nil))
  9300. org-tag-alist)))
  9301. (if (and files (car files))
  9302. files
  9303. (org-agenda-files))))))))
  9304. (defun org-make-tags-matcher (match)
  9305. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9306. ;; todo-only is scoped dynamically into this function, and the function
  9307. ;; may change it if the matcher asks for it.
  9308. (unless match
  9309. ;; Get a new match request, with completion
  9310. (let ((org-last-tags-completion-table
  9311. (org-global-tags-completion-table)))
  9312. (setq match (org-completing-read-no-ido
  9313. "Match: " 'org-tags-completion-function nil nil nil
  9314. 'org-tags-history))))
  9315. ;; Parse the string and create a lisp form
  9316. (let ((match0 match)
  9317. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9318. minus tag mm
  9319. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9320. orterms term orlist re-p str-p level-p level-op time-p
  9321. prop-p pn pv po cat-p gv rest)
  9322. (if (string-match "/+" match)
  9323. ;; match contains also a todo-matching request
  9324. (progn
  9325. (setq tagsmatch (substring match 0 (match-beginning 0))
  9326. todomatch (substring match (match-end 0)))
  9327. (if (string-match "^!" todomatch)
  9328. (setq todo-only t todomatch (substring todomatch 1)))
  9329. (if (string-match "^\\s-*$" todomatch)
  9330. (setq todomatch nil)))
  9331. ;; only matching tags
  9332. (setq tagsmatch match todomatch nil))
  9333. ;; Make the tags matcher
  9334. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9335. (setq tagsmatcher t)
  9336. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9337. (while (setq term (pop orterms))
  9338. (while (and (equal (substring term -1) "\\") orterms)
  9339. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9340. (while (string-match re term)
  9341. (setq rest (substring term (match-end 0))
  9342. minus (and (match-end 1)
  9343. (equal (match-string 1 term) "-"))
  9344. tag (match-string 2 term)
  9345. re-p (equal (string-to-char tag) ?{)
  9346. level-p (match-end 4)
  9347. prop-p (match-end 5)
  9348. mm (cond
  9349. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9350. (level-p
  9351. (setq level-op (org-op-to-function (match-string 3 term)))
  9352. `(,level-op level ,(string-to-number
  9353. (match-string 4 term))))
  9354. (prop-p
  9355. (setq pn (match-string 5 term)
  9356. po (match-string 6 term)
  9357. pv (match-string 7 term)
  9358. cat-p (equal pn "CATEGORY")
  9359. re-p (equal (string-to-char pv) ?{)
  9360. str-p (equal (string-to-char pv) ?\")
  9361. time-p (save-match-data
  9362. (string-match "^\"[[<].*[]>]\"$" pv))
  9363. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9364. (if time-p (setq pv (org-matcher-time pv)))
  9365. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9366. (cond
  9367. ((equal pn "CATEGORY")
  9368. (setq gv '(get-text-property (point) 'org-category)))
  9369. ((equal pn "TODO")
  9370. (setq gv 'todo))
  9371. (t
  9372. (setq gv `(org-cached-entry-get nil ,pn))))
  9373. (if re-p
  9374. (if (eq po 'org<>)
  9375. `(not (string-match ,pv (or ,gv "")))
  9376. `(string-match ,pv (or ,gv "")))
  9377. (if str-p
  9378. `(,po (or ,gv "") ,pv)
  9379. `(,po (string-to-number (or ,gv ""))
  9380. ,(string-to-number pv) ))))
  9381. (t `(member ,tag tags-list)))
  9382. mm (if minus (list 'not mm) mm)
  9383. term rest)
  9384. (push mm tagsmatcher))
  9385. (push (if (> (length tagsmatcher) 1)
  9386. (cons 'and tagsmatcher)
  9387. (car tagsmatcher))
  9388. orlist)
  9389. (setq tagsmatcher nil))
  9390. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9391. (setq tagsmatcher
  9392. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9393. ;; Make the todo matcher
  9394. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9395. (setq todomatcher t)
  9396. (setq orterms (org-split-string todomatch "|") orlist nil)
  9397. (while (setq term (pop orterms))
  9398. (while (string-match re term)
  9399. (setq minus (and (match-end 1)
  9400. (equal (match-string 1 term) "-"))
  9401. kwd (match-string 2 term)
  9402. re-p (equal (string-to-char kwd) ?{)
  9403. term (substring term (match-end 0))
  9404. mm (if re-p
  9405. `(string-match ,(substring kwd 1 -1) todo)
  9406. (list 'equal 'todo kwd))
  9407. mm (if minus (list 'not mm) mm))
  9408. (push mm todomatcher))
  9409. (push (if (> (length todomatcher) 1)
  9410. (cons 'and todomatcher)
  9411. (car todomatcher))
  9412. orlist)
  9413. (setq todomatcher nil))
  9414. (setq todomatcher (if (> (length orlist) 1)
  9415. (cons 'or orlist) (car orlist))))
  9416. ;; Return the string and lisp forms of the matcher
  9417. (setq matcher (if todomatcher
  9418. (list 'and tagsmatcher todomatcher)
  9419. tagsmatcher))
  9420. (cons match0 matcher)))
  9421. (defun org-op-to-function (op &optional stringp)
  9422. "Turn an operator into the appropriate function."
  9423. (setq op
  9424. (cond
  9425. ((equal op "<" ) '(< string< org-time<))
  9426. ((equal op ">" ) '(> org-string> org-time>))
  9427. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9428. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9429. ((member op '("=" "==")) '(= string= org-time=))
  9430. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9431. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9432. (defun org<> (a b) (not (= a b)))
  9433. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9434. (defun org-string>= (a b) (not (string< a b)))
  9435. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9436. (defun org-string<> (a b) (not (string= a b)))
  9437. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9438. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9439. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9440. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9441. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9442. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9443. (defun org-2ft (s)
  9444. "Convert S to a floating point time.
  9445. If S is already a number, just return it. If it is a string, parse
  9446. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9447. (cond
  9448. ((numberp s) s)
  9449. ((stringp s)
  9450. (condition-case nil
  9451. (float-time (apply 'encode-time (org-parse-time-string s)))
  9452. (error 0.)))
  9453. (t 0.)))
  9454. (defun org-time-today ()
  9455. "Time in seconds today at 0:00.
  9456. Returns the float number of seconds since the beginning of the
  9457. epoch to the beginning of today (00:00)."
  9458. (float-time (apply 'encode-time
  9459. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9460. (defun org-matcher-time (s)
  9461. "Interpret a time comparison value."
  9462. (save-match-data
  9463. (cond
  9464. ((string= s "<now>") (float-time))
  9465. ((string= s "<today>") (org-time-today))
  9466. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9467. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9468. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9469. (+ (org-time-today)
  9470. (* (string-to-number (match-string 1 s))
  9471. (cdr (assoc (match-string 2 s)
  9472. '(("d" . 86400.0) ("w" . 604800.0)
  9473. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9474. (t (org-2ft s)))))
  9475. (defun org-match-any-p (re list)
  9476. "Does re match any element of list?"
  9477. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9478. (delq nil list))
  9479. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9480. (defvar org-tags-overlay (org-make-overlay 1 1))
  9481. (org-detach-overlay org-tags-overlay)
  9482. (defun org-get-local-tags-at (&optional pos)
  9483. "Get a list of tags defined in the current headline."
  9484. (org-get-tags-at pos 'local))
  9485. (defun org-get-local-tags ()
  9486. "Get a list of tags defined in the current headline."
  9487. (org-get-tags-at nil 'local))
  9488. (defun org-get-tags-at (&optional pos local)
  9489. "Get a list of all headline tags applicable at POS.
  9490. POS defaults to point. If tags are inherited, the list contains
  9491. the targets in the same sequence as the headlines appear, i.e.
  9492. the tags of the current headline come last.
  9493. When LOCAL is non-nil, only return tags from the current headline,
  9494. ignore inherited ones."
  9495. (interactive)
  9496. (if (and org-trust-scanner-tags
  9497. (or (not pos) (equal pos (point)))
  9498. (not local))
  9499. org-scanner-tags
  9500. (let (tags ltags lastpos parent)
  9501. (save-excursion
  9502. (save-restriction
  9503. (widen)
  9504. (goto-char (or pos (point)))
  9505. (save-match-data
  9506. (catch 'done
  9507. (condition-case nil
  9508. (progn
  9509. (org-back-to-heading t)
  9510. (while (not (equal lastpos (point)))
  9511. (setq lastpos (point))
  9512. (when (looking-at
  9513. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9514. (setq ltags (org-split-string
  9515. (org-match-string-no-properties 1) ":"))
  9516. (when parent
  9517. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9518. (setq tags (append
  9519. (if parent
  9520. (org-remove-uniherited-tags ltags)
  9521. ltags)
  9522. tags)))
  9523. (or org-use-tag-inheritance (throw 'done t))
  9524. (if local (throw 'done t))
  9525. (org-up-heading-all 1)
  9526. (setq parent t)))
  9527. (error nil)))))
  9528. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9529. (defun org-add-prop-inherited (s)
  9530. (add-text-properties 0 (length s) '(inherited t) s)
  9531. s)
  9532. (defun org-toggle-tag (tag &optional onoff)
  9533. "Toggle the tag TAG for the current line.
  9534. If ONOFF is `on' or `off', don't toggle but set to this state."
  9535. (let (res current)
  9536. (save-excursion
  9537. (org-back-to-heading t)
  9538. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9539. (point-at-eol) t)
  9540. (progn
  9541. (setq current (match-string 1))
  9542. (replace-match ""))
  9543. (setq current ""))
  9544. (setq current (nreverse (org-split-string current ":")))
  9545. (cond
  9546. ((eq onoff 'on)
  9547. (setq res t)
  9548. (or (member tag current) (push tag current)))
  9549. ((eq onoff 'off)
  9550. (or (not (member tag current)) (setq current (delete tag current))))
  9551. (t (if (member tag current)
  9552. (setq current (delete tag current))
  9553. (setq res t)
  9554. (push tag current))))
  9555. (end-of-line 1)
  9556. (if current
  9557. (progn
  9558. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9559. (org-set-tags nil t))
  9560. (delete-horizontal-space))
  9561. (run-hooks 'org-after-tags-change-hook))
  9562. res))
  9563. (defun org-align-tags-here (to-col)
  9564. ;; Assumes that this is a headline
  9565. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9566. (beginning-of-line 1)
  9567. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9568. (< pos (match-beginning 2)))
  9569. (progn
  9570. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9571. (goto-char (match-beginning 1))
  9572. (insert " ")
  9573. (delete-region (point) (1+ (match-beginning 2)))
  9574. (setq ncol (max (1+ (current-column))
  9575. (1+ col)
  9576. (if (> to-col 0)
  9577. to-col
  9578. (- (abs to-col) tags-l))))
  9579. (setq p (point))
  9580. (insert (make-string (- ncol (current-column)) ?\ ))
  9581. (setq ncol (current-column))
  9582. (when indent-tabs-mode (tabify p (point-at-eol)))
  9583. (org-move-to-column (min ncol col) t))
  9584. (goto-char pos))))
  9585. (defun org-set-tags-command (&optional arg just-align)
  9586. "Call the set-tags command for the current entry."
  9587. (interactive "P")
  9588. (if (org-on-heading-p)
  9589. (org-set-tags arg just-align)
  9590. (save-excursion
  9591. (org-back-to-heading t)
  9592. (org-set-tags arg just-align))))
  9593. (defun org-set-tags (&optional arg just-align)
  9594. "Set the tags for the current headline.
  9595. With prefix ARG, realign all tags in headings in the current buffer."
  9596. (interactive "P")
  9597. (let* ((re (concat "^" outline-regexp))
  9598. (current (org-get-tags-string))
  9599. (col (current-column))
  9600. (org-setting-tags t)
  9601. table current-tags inherited-tags ; computed below when needed
  9602. tags p0 c0 c1 rpl)
  9603. (if arg
  9604. (save-excursion
  9605. (goto-char (point-min))
  9606. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9607. (while (re-search-forward re nil t)
  9608. (org-set-tags nil t)
  9609. (end-of-line 1)))
  9610. (message "All tags realigned to column %d" org-tags-column))
  9611. (if just-align
  9612. (setq tags current)
  9613. ;; Get a new set of tags from the user
  9614. (save-excursion
  9615. (setq table (append org-tag-persistent-alist
  9616. (or org-tag-alist (org-get-buffer-tags)))
  9617. org-last-tags-completion-table table
  9618. current-tags (org-split-string current ":")
  9619. inherited-tags (nreverse
  9620. (nthcdr (length current-tags)
  9621. (nreverse (org-get-tags-at))))
  9622. tags
  9623. (if (or (eq t org-use-fast-tag-selection)
  9624. (and org-use-fast-tag-selection
  9625. (delq nil (mapcar 'cdr table))))
  9626. (org-fast-tag-selection
  9627. current-tags inherited-tags table
  9628. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9629. (let ((org-add-colon-after-tag-completion t))
  9630. (org-trim
  9631. (org-without-partial-completion
  9632. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9633. nil nil current 'org-tags-history)))))))
  9634. (while (string-match "[-+&]+" tags)
  9635. ;; No boolean logic, just a list
  9636. (setq tags (replace-match ":" t t tags))))
  9637. (if (string-match "\\`[\t ]*\\'" tags)
  9638. (setq tags "")
  9639. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9640. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9641. ;; Insert new tags at the correct column
  9642. (beginning-of-line 1)
  9643. (cond
  9644. ((and (equal current "") (equal tags "")))
  9645. ((re-search-forward
  9646. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9647. (point-at-eol) t)
  9648. (if (equal tags "")
  9649. (setq rpl "")
  9650. (goto-char (match-beginning 0))
  9651. (setq c0 (current-column) p0 (point)
  9652. c1 (max (1+ c0) (if (> org-tags-column 0)
  9653. org-tags-column
  9654. (- (- org-tags-column) (length tags))))
  9655. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9656. (replace-match rpl t t)
  9657. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9658. tags)
  9659. (t (error "Tags alignment failed")))
  9660. (org-move-to-column col)
  9661. (unless just-align
  9662. (run-hooks 'org-after-tags-change-hook)))))
  9663. (defun org-change-tag-in-region (beg end tag off)
  9664. "Add or remove TAG for each entry in the region.
  9665. This works in the agenda, and also in an org-mode buffer."
  9666. (interactive
  9667. (list (region-beginning) (region-end)
  9668. (let ((org-last-tags-completion-table
  9669. (if (org-mode-p)
  9670. (org-get-buffer-tags)
  9671. (org-global-tags-completion-table))))
  9672. (org-ido-completing-read
  9673. "Tag: " 'org-tags-completion-function nil nil nil
  9674. 'org-tags-history))
  9675. (progn
  9676. (message "[s]et or [r]emove? ")
  9677. (equal (read-char-exclusive) ?r))))
  9678. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9679. (let ((agendap (equal major-mode 'org-agenda-mode))
  9680. l1 l2 m buf pos newhead (cnt 0))
  9681. (goto-char end)
  9682. (setq l2 (1- (org-current-line)))
  9683. (goto-char beg)
  9684. (setq l1 (org-current-line))
  9685. (loop for l from l1 to l2 do
  9686. (goto-line l)
  9687. (setq m (get-text-property (point) 'org-hd-marker))
  9688. (when (or (and (org-mode-p) (org-on-heading-p))
  9689. (and agendap m))
  9690. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9691. pos (if agendap m (point)))
  9692. (with-current-buffer buf
  9693. (save-excursion
  9694. (save-restriction
  9695. (goto-char pos)
  9696. (setq cnt (1+ cnt))
  9697. (org-toggle-tag tag (if off 'off 'on))
  9698. (setq newhead (org-get-heading)))))
  9699. (and agendap (org-agenda-change-all-lines newhead m))))
  9700. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9701. (defun org-tags-completion-function (string predicate &optional flag)
  9702. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9703. (confirm (lambda (x) (stringp (car x)))))
  9704. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9705. (setq s1 (match-string 1 string)
  9706. s2 (match-string 2 string))
  9707. (setq s1 "" s2 string))
  9708. (cond
  9709. ((eq flag nil)
  9710. ;; try completion
  9711. (setq rtn (try-completion s2 ctable confirm))
  9712. (if (stringp rtn)
  9713. (setq rtn
  9714. (concat s1 s2 (substring rtn (length s2))
  9715. (if (and org-add-colon-after-tag-completion
  9716. (assoc rtn ctable))
  9717. ":" ""))))
  9718. rtn)
  9719. ((eq flag t)
  9720. ;; all-completions
  9721. (all-completions s2 ctable confirm)
  9722. )
  9723. ((eq flag 'lambda)
  9724. ;; exact match?
  9725. (assoc s2 ctable)))
  9726. ))
  9727. (defun org-fast-tag-insert (kwd tags face &optional end)
  9728. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9729. (insert (format "%-12s" (concat kwd ":"))
  9730. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9731. (or end "")))
  9732. (defun org-fast-tag-show-exit (flag)
  9733. (save-excursion
  9734. (goto-line 3)
  9735. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9736. (replace-match ""))
  9737. (when flag
  9738. (end-of-line 1)
  9739. (org-move-to-column (- (window-width) 19) t)
  9740. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9741. (defun org-set-current-tags-overlay (current prefix)
  9742. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9743. (if (featurep 'xemacs)
  9744. (org-overlay-display org-tags-overlay (concat prefix s)
  9745. 'secondary-selection)
  9746. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9747. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9748. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9749. "Fast tag selection with single keys.
  9750. CURRENT is the current list of tags in the headline, INHERITED is the
  9751. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9752. possibly with grouping information. TODO-TABLE is a similar table with
  9753. TODO keywords, should these have keys assigned to them.
  9754. If the keys are nil, a-z are automatically assigned.
  9755. Returns the new tags string, or nil to not change the current settings."
  9756. (let* ((fulltable (append table todo-table))
  9757. (maxlen (apply 'max (mapcar
  9758. (lambda (x)
  9759. (if (stringp (car x)) (string-width (car x)) 0))
  9760. fulltable)))
  9761. (buf (current-buffer))
  9762. (expert (eq org-fast-tag-selection-single-key 'expert))
  9763. (buffer-tags nil)
  9764. (fwidth (+ maxlen 3 1 3))
  9765. (ncol (/ (- (window-width) 4) fwidth))
  9766. (i-face 'org-done)
  9767. (c-face 'org-todo)
  9768. tg cnt e c char c1 c2 ntable tbl rtn
  9769. ov-start ov-end ov-prefix
  9770. (exit-after-next org-fast-tag-selection-single-key)
  9771. (done-keywords org-done-keywords)
  9772. groups ingroup)
  9773. (save-excursion
  9774. (beginning-of-line 1)
  9775. (if (looking-at
  9776. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9777. (setq ov-start (match-beginning 1)
  9778. ov-end (match-end 1)
  9779. ov-prefix "")
  9780. (setq ov-start (1- (point-at-eol))
  9781. ov-end (1+ ov-start))
  9782. (skip-chars-forward "^\n\r")
  9783. (setq ov-prefix
  9784. (concat
  9785. (buffer-substring (1- (point)) (point))
  9786. (if (> (current-column) org-tags-column)
  9787. " "
  9788. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9789. (org-move-overlay org-tags-overlay ov-start ov-end)
  9790. (save-window-excursion
  9791. (if expert
  9792. (set-buffer (get-buffer-create " *Org tags*"))
  9793. (delete-other-windows)
  9794. (split-window-vertically)
  9795. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9796. (erase-buffer)
  9797. (org-set-local 'org-done-keywords done-keywords)
  9798. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9799. (org-fast-tag-insert "Current" current c-face "\n\n")
  9800. (org-fast-tag-show-exit exit-after-next)
  9801. (org-set-current-tags-overlay current ov-prefix)
  9802. (setq tbl fulltable char ?a cnt 0)
  9803. (while (setq e (pop tbl))
  9804. (cond
  9805. ((equal e '(:startgroup))
  9806. (push '() groups) (setq ingroup t)
  9807. (when (not (= cnt 0))
  9808. (setq cnt 0)
  9809. (insert "\n"))
  9810. (insert "{ "))
  9811. ((equal e '(:endgroup))
  9812. (setq ingroup nil cnt 0)
  9813. (insert "}\n"))
  9814. ((equal e '(:newline))
  9815. (when (not (= cnt 0))
  9816. (setq cnt 0)
  9817. (insert "\n")
  9818. (setq e (car tbl))
  9819. (while (equal (car tbl) '(:newline))
  9820. (insert "\n")
  9821. (setq tbl (cdr tbl)))))
  9822. (t
  9823. (setq tg (car e) c2 nil)
  9824. (if (cdr e)
  9825. (setq c (cdr e))
  9826. ;; automatically assign a character.
  9827. (setq c1 (string-to-char
  9828. (downcase (substring
  9829. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9830. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9831. (while (or (rassoc char ntable) (rassoc char table))
  9832. (setq char (1+ char)))
  9833. (setq c2 c1))
  9834. (setq c (or c2 char)))
  9835. (if ingroup (push tg (car groups)))
  9836. (setq tg (org-add-props tg nil 'face
  9837. (cond
  9838. ((not (assoc tg table))
  9839. (org-get-todo-face tg))
  9840. ((member tg current) c-face)
  9841. ((member tg inherited) i-face)
  9842. (t nil))))
  9843. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9844. (insert "[" c "] " tg (make-string
  9845. (- fwidth 4 (length tg)) ?\ ))
  9846. (push (cons tg c) ntable)
  9847. (when (= (setq cnt (1+ cnt)) ncol)
  9848. (insert "\n")
  9849. (if ingroup (insert " "))
  9850. (setq cnt 0)))))
  9851. (setq ntable (nreverse ntable))
  9852. (insert "\n")
  9853. (goto-char (point-min))
  9854. (if (not expert) (org-fit-window-to-buffer))
  9855. (setq rtn
  9856. (catch 'exit
  9857. (while t
  9858. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9859. (if groups " [!] no groups" " [!]groups")
  9860. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9861. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9862. (cond
  9863. ((= c ?\r) (throw 'exit t))
  9864. ((= c ?!)
  9865. (setq groups (not groups))
  9866. (goto-char (point-min))
  9867. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9868. ((= c ?\C-c)
  9869. (if (not expert)
  9870. (org-fast-tag-show-exit
  9871. (setq exit-after-next (not exit-after-next)))
  9872. (setq expert nil)
  9873. (delete-other-windows)
  9874. (split-window-vertically)
  9875. (org-switch-to-buffer-other-window " *Org tags*")
  9876. (org-fit-window-to-buffer)))
  9877. ((or (= c ?\C-g)
  9878. (and (= c ?q) (not (rassoc c ntable))))
  9879. (org-detach-overlay org-tags-overlay)
  9880. (setq quit-flag t))
  9881. ((= c ?\ )
  9882. (setq current nil)
  9883. (if exit-after-next (setq exit-after-next 'now)))
  9884. ((= c ?\t)
  9885. (condition-case nil
  9886. (setq tg (org-ido-completing-read
  9887. "Tag: "
  9888. (or buffer-tags
  9889. (with-current-buffer buf
  9890. (org-get-buffer-tags)))))
  9891. (quit (setq tg "")))
  9892. (when (string-match "\\S-" tg)
  9893. (add-to-list 'buffer-tags (list tg))
  9894. (if (member tg current)
  9895. (setq current (delete tg current))
  9896. (push tg current)))
  9897. (if exit-after-next (setq exit-after-next 'now)))
  9898. ((setq e (rassoc c todo-table) tg (car e))
  9899. (with-current-buffer buf
  9900. (save-excursion (org-todo tg)))
  9901. (if exit-after-next (setq exit-after-next 'now)))
  9902. ((setq e (rassoc c ntable) tg (car e))
  9903. (if (member tg current)
  9904. (setq current (delete tg current))
  9905. (loop for g in groups do
  9906. (if (member tg g)
  9907. (mapc (lambda (x)
  9908. (setq current (delete x current)))
  9909. g)))
  9910. (push tg current))
  9911. (if exit-after-next (setq exit-after-next 'now))))
  9912. ;; Create a sorted list
  9913. (setq current
  9914. (sort current
  9915. (lambda (a b)
  9916. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9917. (if (eq exit-after-next 'now) (throw 'exit t))
  9918. (goto-char (point-min))
  9919. (beginning-of-line 2)
  9920. (delete-region (point) (point-at-eol))
  9921. (org-fast-tag-insert "Current" current c-face)
  9922. (org-set-current-tags-overlay current ov-prefix)
  9923. (while (re-search-forward
  9924. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9925. (setq tg (match-string 1))
  9926. (add-text-properties
  9927. (match-beginning 1) (match-end 1)
  9928. (list 'face
  9929. (cond
  9930. ((member tg current) c-face)
  9931. ((member tg inherited) i-face)
  9932. (t (get-text-property (match-beginning 1) 'face))))))
  9933. (goto-char (point-min)))))
  9934. (org-detach-overlay org-tags-overlay)
  9935. (if rtn
  9936. (mapconcat 'identity current ":")
  9937. nil))))
  9938. (defun org-get-tags-string ()
  9939. "Get the TAGS string in the current headline."
  9940. (unless (org-on-heading-p t)
  9941. (error "Not on a heading"))
  9942. (save-excursion
  9943. (beginning-of-line 1)
  9944. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9945. (org-match-string-no-properties 1)
  9946. "")))
  9947. (defun org-get-tags ()
  9948. "Get the list of tags specified in the current headline."
  9949. (org-split-string (org-get-tags-string) ":"))
  9950. (defun org-get-buffer-tags ()
  9951. "Get a table of all tags used in the buffer, for completion."
  9952. (let (tags)
  9953. (save-excursion
  9954. (goto-char (point-min))
  9955. (while (re-search-forward
  9956. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9957. (when (equal (char-after (point-at-bol 0)) ?*)
  9958. (mapc (lambda (x) (add-to-list 'tags x))
  9959. (org-split-string (org-match-string-no-properties 1) ":")))))
  9960. (mapcar 'list tags)))
  9961. ;;;; The mapping API
  9962. ;;;###autoload
  9963. (defun org-map-entries (func &optional match scope &rest skip)
  9964. "Call FUNC at each headline selected by MATCH in SCOPE.
  9965. FUNC is a function or a lisp form. The function will be called without
  9966. arguments, with the cursor positioned at the beginning of the headline.
  9967. The return values of all calls to the function will be collected and
  9968. returned as a list.
  9969. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  9970. does not need to preserve point. After evaluaton, the cursor will be
  9971. moved to the end of the line (presumably of the headline of the
  9972. processed entry) and search continues from there. Under some
  9973. circumstances, this may not produce the wanted results. For example,
  9974. if you have removed (e.g. archived) the current (sub)tree it could
  9975. mean that the next entry will be skipped entirely. In such cases, you
  9976. can specify the position from where search should continue by making
  9977. FUNC set the variable `org-map-continue-from' to the desired buffer
  9978. position.
  9979. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9980. Only headlines that are matched by this query will be considered during
  9981. the iteration. When MATCH is nil or t, all headlines will be
  9982. visited by the iteration.
  9983. SCOPE determines the scope of this command. It can be any of:
  9984. nil The current buffer, respecting the restriction if any
  9985. tree The subtree started with the entry at point
  9986. file The current buffer, without restriction
  9987. file-with-archives
  9988. The current buffer, and any archives associated with it
  9989. agenda All agenda files
  9990. agenda-with-archives
  9991. All agenda files with any archive files associated with them
  9992. \(file1 file2 ...)
  9993. If this is a list, all files in the list will be scanned
  9994. The remaining args are treated as settings for the skipping facilities of
  9995. the scanner. The following items can be given here:
  9996. archive skip trees with the archive tag.
  9997. comment skip trees with the COMMENT keyword
  9998. function or Emacs Lisp form:
  9999. will be used as value for `org-agenda-skip-function', so whenever
  10000. the the function returns t, FUNC will not be called for that
  10001. entry and search will continue from the point where the
  10002. function leaves it.
  10003. If your function needs to retrieve the tags including inherited tags
  10004. at the *current* entry, you can use the value of the variable
  10005. `org-scanner-tags' which will be much faster than getting the value
  10006. with `org-get-tags-at'. If your function gets properties with
  10007. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10008. to t around the call to `org-entry-properties' to get the same speedup.
  10009. Note that if your function moves around to retrieve tags and properties at
  10010. a *different* entry, you cannot use these techniques."
  10011. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10012. (org-agenda-skip-archived-trees (memq 'archive skip))
  10013. (org-agenda-skip-comment-trees (memq 'comment skip))
  10014. (org-agenda-skip-function
  10015. (car (org-delete-all '(comment archive) skip)))
  10016. (org-tags-match-list-sublevels t)
  10017. matcher file res
  10018. org-todo-keywords-for-agenda
  10019. org-done-keywords-for-agenda
  10020. org-todo-keyword-alist-for-agenda
  10021. org-tag-alist-for-agenda)
  10022. (cond
  10023. ((eq match t) (setq matcher t))
  10024. ((eq match nil) (setq matcher t))
  10025. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10026. (save-excursion
  10027. (save-restriction
  10028. (when (eq scope 'tree)
  10029. (org-back-to-heading t)
  10030. (org-narrow-to-subtree)
  10031. (setq scope nil))
  10032. (if (not scope)
  10033. (progn
  10034. (org-prepare-agenda-buffers
  10035. (list (buffer-file-name (current-buffer))))
  10036. (setq res (org-scan-tags func matcher)))
  10037. ;; Get the right scope
  10038. (cond
  10039. ((and scope (listp scope) (symbolp (car scope)))
  10040. (setq scope (eval scope)))
  10041. ((eq scope 'agenda)
  10042. (setq scope (org-agenda-files t)))
  10043. ((eq scope 'agenda-with-archives)
  10044. (setq scope (org-agenda-files t))
  10045. (setq scope (org-add-archive-files scope)))
  10046. ((eq scope 'file)
  10047. (setq scope (list (buffer-file-name))))
  10048. ((eq scope 'file-with-archives)
  10049. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10050. (org-prepare-agenda-buffers scope)
  10051. (while (setq file (pop scope))
  10052. (with-current-buffer (org-find-base-buffer-visiting file)
  10053. (save-excursion
  10054. (save-restriction
  10055. (widen)
  10056. (goto-char (point-min))
  10057. (setq res (append res (org-scan-tags func matcher))))))))))
  10058. res))
  10059. ;;;; Properties
  10060. ;;; Setting and retrieving properties
  10061. (defconst org-special-properties
  10062. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10063. "TIMESTAMP" "TIMESTAMP_IA")
  10064. "The special properties valid in Org-mode.
  10065. These are properties that are not defined in the property drawer,
  10066. but in some other way.")
  10067. (defconst org-default-properties
  10068. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  10069. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10070. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10071. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  10072. "Some properties that are used by Org-mode for various purposes.
  10073. Being in this list makes sure that they are offered for completion.")
  10074. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10075. "Regular expression matching the first line of a property drawer.")
  10076. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10077. "Regular expression matching the first line of a property drawer.")
  10078. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10079. "Regular expression matching the first line of a property drawer.")
  10080. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10081. "Regular expression matching the first line of a property drawer.")
  10082. (defconst org-property-drawer-re
  10083. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10084. org-property-end-re "\\)\n?")
  10085. "Matches an entire property drawer.")
  10086. (defconst org-clock-drawer-re
  10087. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10088. org-property-end-re "\\)\n?")
  10089. "Matches an entire clock drawer.")
  10090. (defun org-property-action ()
  10091. "Do an action on properties."
  10092. (interactive)
  10093. (let (c)
  10094. (org-at-property-p)
  10095. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10096. (setq c (read-char-exclusive))
  10097. (cond
  10098. ((equal c ?s)
  10099. (call-interactively 'org-set-property))
  10100. ((equal c ?d)
  10101. (call-interactively 'org-delete-property))
  10102. ((equal c ?D)
  10103. (call-interactively 'org-delete-property-globally))
  10104. ((equal c ?c)
  10105. (call-interactively 'org-compute-property-at-point))
  10106. (t (error "No such property action %c" c)))))
  10107. (defun org-at-property-p ()
  10108. "Is the cursor in a property line?"
  10109. ;; FIXME: Does not check if we are actually in the drawer.
  10110. ;; FIXME: also returns true on any drawers.....
  10111. ;; This is used by C-c C-c for property action.
  10112. (save-excursion
  10113. (beginning-of-line 1)
  10114. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10115. (defun org-get-property-block (&optional beg end force)
  10116. "Return the (beg . end) range of the body of the property drawer.
  10117. BEG and END can be beginning and end of subtree, if not given
  10118. they will be found.
  10119. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10120. (catch 'exit
  10121. (save-excursion
  10122. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10123. (end (or end (progn (outline-next-heading) (point)))))
  10124. (goto-char beg)
  10125. (if (re-search-forward org-property-start-re end t)
  10126. (setq beg (1+ (match-end 0)))
  10127. (if force
  10128. (save-excursion
  10129. (org-insert-property-drawer)
  10130. (setq end (progn (outline-next-heading) (point))))
  10131. (throw 'exit nil))
  10132. (goto-char beg)
  10133. (if (re-search-forward org-property-start-re end t)
  10134. (setq beg (1+ (match-end 0)))))
  10135. (if (re-search-forward org-property-end-re end t)
  10136. (setq end (match-beginning 0))
  10137. (or force (throw 'exit nil))
  10138. (goto-char beg)
  10139. (setq end beg)
  10140. (org-indent-line-function)
  10141. (insert ":END:\n"))
  10142. (cons beg end)))))
  10143. (defun org-entry-properties (&optional pom which)
  10144. "Get all properties of the entry at point-or-marker POM.
  10145. This includes the TODO keyword, the tags, time strings for deadline,
  10146. scheduled, and clocking, and any additional properties defined in the
  10147. entry. The return value is an alist, keys may occur multiple times
  10148. if the property key was used several times.
  10149. POM may also be nil, in which case the current entry is used.
  10150. If WHICH is nil or `all', get all properties. If WHICH is
  10151. `special' or `standard', only get that subclass."
  10152. (setq which (or which 'all))
  10153. (org-with-point-at pom
  10154. (let ((clockstr (substring org-clock-string 0 -1))
  10155. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10156. beg end range props sum-props key value string clocksum)
  10157. (save-excursion
  10158. (when (condition-case nil
  10159. (and (org-mode-p) (org-back-to-heading t))
  10160. (error nil))
  10161. (setq beg (point))
  10162. (setq sum-props (get-text-property (point) 'org-summaries))
  10163. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10164. (outline-next-heading)
  10165. (setq end (point))
  10166. (when (memq which '(all special))
  10167. ;; Get the special properties, like TODO and tags
  10168. (goto-char beg)
  10169. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10170. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10171. (when (looking-at org-priority-regexp)
  10172. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10173. (when (and (setq value (org-get-tags-string))
  10174. (string-match "\\S-" value))
  10175. (push (cons "TAGS" value) props))
  10176. (when (setq value (org-get-tags-at))
  10177. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10178. props))
  10179. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10180. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10181. string (if (equal key clockstr)
  10182. (org-no-properties
  10183. (org-trim
  10184. (buffer-substring
  10185. (match-beginning 3) (goto-char (point-at-eol)))))
  10186. (substring (org-match-string-no-properties 3) 1 -1)))
  10187. (unless key
  10188. (if (= (char-after (match-beginning 3)) ?\[)
  10189. (setq key "TIMESTAMP_IA")
  10190. (setq key "TIMESTAMP")))
  10191. (when (or (equal key clockstr) (not (assoc key props)))
  10192. (push (cons key string) props)))
  10193. )
  10194. (when (memq which '(all standard))
  10195. ;; Get the standard properties, like :PROP: ...
  10196. (setq range (org-get-property-block beg end))
  10197. (when range
  10198. (goto-char (car range))
  10199. (while (re-search-forward
  10200. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10201. (cdr range) t)
  10202. (setq key (org-match-string-no-properties 1)
  10203. value (org-trim (or (org-match-string-no-properties 2) "")))
  10204. (unless (member key excluded)
  10205. (push (cons key (or value "")) props)))))
  10206. (if clocksum
  10207. (push (cons "CLOCKSUM"
  10208. (org-columns-number-to-string (/ (float clocksum) 60.)
  10209. 'add_times))
  10210. props))
  10211. (unless (assoc "CATEGORY" props)
  10212. (setq value (or (org-get-category)
  10213. (progn (org-refresh-category-properties)
  10214. (org-get-category))))
  10215. (push (cons "CATEGORY" value) props))
  10216. (append sum-props (nreverse props)))))))
  10217. (defun org-entry-get (pom property &optional inherit)
  10218. "Get value of PROPERTY for entry at point-or-marker POM.
  10219. If INHERIT is non-nil and the entry does not have the property,
  10220. then also check higher levels of the hierarchy.
  10221. If INHERIT is the symbol `selective', use inheritance only if the setting
  10222. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10223. If the property is present but empty, the return value is the empty string.
  10224. If the property is not present at all, nil is returned."
  10225. (org-with-point-at pom
  10226. (if (and inherit (if (eq inherit 'selective)
  10227. (org-property-inherit-p property)
  10228. t))
  10229. (org-entry-get-with-inheritance property)
  10230. (if (member property org-special-properties)
  10231. ;; We need a special property. Use brute force, get all properties.
  10232. (cdr (assoc property (org-entry-properties nil 'special)))
  10233. (let ((range (org-get-property-block)))
  10234. (if (and range
  10235. (goto-char (car range))
  10236. (re-search-forward
  10237. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10238. (cdr range) t))
  10239. ;; Found the property, return it.
  10240. (if (match-end 1)
  10241. (org-match-string-no-properties 1)
  10242. "")))))))
  10243. (defun org-property-or-variable-value (var &optional inherit)
  10244. "Check if there is a property fixing the value of VAR.
  10245. If yes, return this value. If not, return the current value of the variable."
  10246. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10247. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10248. (read prop)
  10249. (symbol-value var))))
  10250. (defun org-entry-delete (pom property)
  10251. "Delete the property PROPERTY from entry at point-or-marker POM."
  10252. (org-with-point-at pom
  10253. (if (member property org-special-properties)
  10254. nil ; cannot delete these properties.
  10255. (let ((range (org-get-property-block)))
  10256. (if (and range
  10257. (goto-char (car range))
  10258. (re-search-forward
  10259. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10260. (cdr range) t))
  10261. (progn
  10262. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10263. t)
  10264. nil)))))
  10265. ;; Multi-values properties are properties that contain multiple values
  10266. ;; These values are assumed to be single words, separated by whitespace.
  10267. (defun org-entry-add-to-multivalued-property (pom property value)
  10268. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10269. (let* ((old (org-entry-get pom property))
  10270. (values (and old (org-split-string old "[ \t]"))))
  10271. (setq value (org-entry-protect-space value))
  10272. (unless (member value values)
  10273. (setq values (cons value values))
  10274. (org-entry-put pom property
  10275. (mapconcat 'identity values " ")))))
  10276. (defun org-entry-remove-from-multivalued-property (pom property value)
  10277. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10278. (let* ((old (org-entry-get pom property))
  10279. (values (and old (org-split-string old "[ \t]"))))
  10280. (setq value (org-entry-protect-space value))
  10281. (when (member value values)
  10282. (setq values (delete value values))
  10283. (org-entry-put pom property
  10284. (mapconcat 'identity values " ")))))
  10285. (defun org-entry-member-in-multivalued-property (pom property value)
  10286. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10287. (let* ((old (org-entry-get pom property))
  10288. (values (and old (org-split-string old "[ \t]"))))
  10289. (setq value (org-entry-protect-space value))
  10290. (member value values)))
  10291. (defun org-entry-get-multivalued-property (pom property)
  10292. "Return a list of values in a multivalued property."
  10293. (let* ((value (org-entry-get pom property))
  10294. (values (and value (org-split-string value "[ \t]"))))
  10295. (mapcar 'org-entry-restore-space values)))
  10296. (defun org-entry-put-multivalued-property (pom property &rest values)
  10297. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10298. VALUES should be a list of strings. Spaces will be protected."
  10299. (org-entry-put pom property
  10300. (mapconcat 'org-entry-protect-space values " "))
  10301. (let* ((value (org-entry-get pom property))
  10302. (values (and value (org-split-string value "[ \t]"))))
  10303. (mapcar 'org-entry-restore-space values)))
  10304. (defun org-entry-protect-space (s)
  10305. "Protect spaces and newline in string S."
  10306. (while (string-match " " s)
  10307. (setq s (replace-match "%20" t t s)))
  10308. (while (string-match "\n" s)
  10309. (setq s (replace-match "%0A" t t s)))
  10310. s)
  10311. (defun org-entry-restore-space (s)
  10312. "Restore spaces and newline in string S."
  10313. (while (string-match "%20" s)
  10314. (setq s (replace-match " " t t s)))
  10315. (while (string-match "%0A" s)
  10316. (setq s (replace-match "\n" t t s)))
  10317. s)
  10318. (defvar org-entry-property-inherited-from (make-marker)
  10319. "Marker pointing to the entry from where a property was inherited.
  10320. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10321. location of the entry where the inheritance search matched. If there was
  10322. no match, the marker will point nowhere.
  10323. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10324. is set.")
  10325. (defun org-entry-get-with-inheritance (property)
  10326. "Get entry property, and search higher levels if not present."
  10327. (move-marker org-entry-property-inherited-from nil)
  10328. (let (tmp)
  10329. (save-excursion
  10330. (save-restriction
  10331. (widen)
  10332. (catch 'ex
  10333. (while t
  10334. (when (setq tmp (org-entry-get nil property))
  10335. (org-back-to-heading t)
  10336. (move-marker org-entry-property-inherited-from (point))
  10337. (throw 'ex tmp))
  10338. (or (org-up-heading-safe) (throw 'ex nil)))))
  10339. (or tmp
  10340. (cdr (assoc property org-file-properties))
  10341. (cdr (assoc property org-global-properties))
  10342. (cdr (assoc property org-global-properties-fixed))))))
  10343. (defun org-entry-put (pom property value)
  10344. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10345. (org-with-point-at pom
  10346. (org-back-to-heading t)
  10347. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10348. range)
  10349. (cond
  10350. ((equal property "TODO")
  10351. (when (and (stringp value) (string-match "\\S-" value)
  10352. (not (member value org-todo-keywords-1)))
  10353. (error "\"%s\" is not a valid TODO state" value))
  10354. (if (or (not value)
  10355. (not (string-match "\\S-" value)))
  10356. (setq value 'none))
  10357. (org-todo value)
  10358. (org-set-tags nil 'align))
  10359. ((equal property "PRIORITY")
  10360. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10361. (string-to-char value) ?\ ))
  10362. (org-set-tags nil 'align))
  10363. ((equal property "SCHEDULED")
  10364. (if (re-search-forward org-scheduled-time-regexp end t)
  10365. (cond
  10366. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10367. ((eq value 'later) (org-timestamp-change 1 'day))
  10368. (t (call-interactively 'org-schedule)))
  10369. (call-interactively 'org-schedule)))
  10370. ((equal property "DEADLINE")
  10371. (if (re-search-forward org-deadline-time-regexp end t)
  10372. (cond
  10373. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10374. ((eq value 'later) (org-timestamp-change 1 'day))
  10375. (t (call-interactively 'org-deadline)))
  10376. (call-interactively 'org-deadline)))
  10377. ((member property org-special-properties)
  10378. (error "The %s property can not yet be set with `org-entry-put'"
  10379. property))
  10380. (t ; a non-special property
  10381. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10382. (setq range (org-get-property-block beg end 'force))
  10383. (goto-char (car range))
  10384. (if (re-search-forward
  10385. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10386. (progn
  10387. (delete-region (match-beginning 1) (match-end 1))
  10388. (goto-char (match-beginning 1)))
  10389. (goto-char (cdr range))
  10390. (insert "\n")
  10391. (backward-char 1)
  10392. (org-indent-line-function)
  10393. (insert ":" property ":"))
  10394. (and value (insert " " value))
  10395. (org-indent-line-function)))))))
  10396. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10397. "Get all property keys in the current buffer.
  10398. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10399. like tags and TODO state.
  10400. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10401. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10402. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10403. formats in the current buffer."
  10404. (let (rtn range cfmt s p)
  10405. (save-excursion
  10406. (save-restriction
  10407. (widen)
  10408. (goto-char (point-min))
  10409. (while (re-search-forward org-property-start-re nil t)
  10410. (setq range (org-get-property-block))
  10411. (goto-char (car range))
  10412. (while (re-search-forward
  10413. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10414. (cdr range) t)
  10415. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10416. (outline-next-heading))))
  10417. (when include-specials
  10418. (setq rtn (append org-special-properties rtn)))
  10419. (when include-defaults
  10420. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10421. (when include-columns
  10422. (save-excursion
  10423. (save-restriction
  10424. (widen)
  10425. (goto-char (point-min))
  10426. (while (re-search-forward
  10427. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10428. nil t)
  10429. (setq cfmt (match-string 2) s 0)
  10430. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10431. cfmt s)
  10432. (setq s (match-end 0)
  10433. p (match-string 1 cfmt))
  10434. (unless (or (equal p "ITEM")
  10435. (member p org-special-properties))
  10436. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10437. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10438. (defun org-property-values (key)
  10439. "Return a list of all values of property KEY."
  10440. (save-excursion
  10441. (save-restriction
  10442. (widen)
  10443. (goto-char (point-min))
  10444. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10445. values)
  10446. (while (re-search-forward re nil t)
  10447. (add-to-list 'values (org-trim (match-string 1))))
  10448. (delete "" values)))))
  10449. (defun org-insert-property-drawer ()
  10450. "Insert a property drawer into the current entry."
  10451. (interactive)
  10452. (org-back-to-heading t)
  10453. (looking-at outline-regexp)
  10454. (let ((indent (- (match-end 0)(match-beginning 0)))
  10455. (beg (point))
  10456. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10457. end hiddenp)
  10458. (outline-next-heading)
  10459. (setq end (point))
  10460. (goto-char beg)
  10461. (while (re-search-forward re end t))
  10462. (setq hiddenp (org-invisible-p))
  10463. (end-of-line 1)
  10464. (and (equal (char-after) ?\n) (forward-char 1))
  10465. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10466. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10467. ;; just skip this line
  10468. (beginning-of-line 2)
  10469. ;; Drawer start, find the end
  10470. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10471. (beginning-of-line 1)))
  10472. (org-skip-over-state-notes)
  10473. (skip-chars-backward " \t\n\r")
  10474. (if (eq (char-before) ?*) (forward-char 1))
  10475. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10476. (beginning-of-line 0)
  10477. (org-indent-to-column indent)
  10478. (beginning-of-line 2)
  10479. (org-indent-to-column indent)
  10480. (beginning-of-line 0)
  10481. (if hiddenp
  10482. (save-excursion
  10483. (org-back-to-heading t)
  10484. (hide-entry))
  10485. (org-flag-drawer t))))
  10486. (defun org-set-property (property value)
  10487. "In the current entry, set PROPERTY to VALUE.
  10488. When called interactively, this will prompt for a property name, offering
  10489. completion on existing and default properties. And then it will prompt
  10490. for a value, offering completion either on allowed values (via an inherited
  10491. xxx_ALL property) or on existing values in other instances of this property
  10492. in the current file."
  10493. (interactive
  10494. (let* ((completion-ignore-case t)
  10495. (keys (org-buffer-property-keys nil t t))
  10496. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10497. (prop (if (member prop0 keys)
  10498. prop0
  10499. (or (cdr (assoc (downcase prop0)
  10500. (mapcar (lambda (x) (cons (downcase x) x))
  10501. keys)))
  10502. prop0)))
  10503. (cur (org-entry-get nil prop))
  10504. (allowed (org-property-get-allowed-values nil prop 'table))
  10505. (existing (mapcar 'list (org-property-values prop)))
  10506. (val (if allowed
  10507. (org-completing-read "Value: " allowed nil 'req-match)
  10508. (let (org-completion-use-ido)
  10509. (org-completing-read
  10510. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10511. (concat "[" cur "]") "")
  10512. ": ")
  10513. existing nil nil "" nil cur)))))
  10514. (list prop (if (equal val "") cur val))))
  10515. (unless (equal (org-entry-get nil property) value)
  10516. (org-entry-put nil property value)))
  10517. (defun org-delete-property (property)
  10518. "In the current entry, delete PROPERTY."
  10519. (interactive
  10520. (let* ((completion-ignore-case t)
  10521. (prop (org-ido-completing-read
  10522. "Property: " (org-entry-properties nil 'standard))))
  10523. (list prop)))
  10524. (message "Property %s %s" property
  10525. (if (org-entry-delete nil property)
  10526. "deleted"
  10527. "was not present in the entry")))
  10528. (defun org-delete-property-globally (property)
  10529. "Remove PROPERTY globally, from all entries."
  10530. (interactive
  10531. (let* ((completion-ignore-case t)
  10532. (prop (org-ido-completing-read
  10533. "Globally remove property: "
  10534. (mapcar 'list (org-buffer-property-keys)))))
  10535. (list prop)))
  10536. (save-excursion
  10537. (save-restriction
  10538. (widen)
  10539. (goto-char (point-min))
  10540. (let ((cnt 0))
  10541. (while (re-search-forward
  10542. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10543. nil t)
  10544. (setq cnt (1+ cnt))
  10545. (replace-match ""))
  10546. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10547. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10548. (defun org-compute-property-at-point ()
  10549. "Compute the property at point.
  10550. This looks for an enclosing column format, extracts the operator and
  10551. then applies it to the property in the column format's scope."
  10552. (interactive)
  10553. (unless (org-at-property-p)
  10554. (error "Not at a property"))
  10555. (let ((prop (org-match-string-no-properties 2)))
  10556. (org-columns-get-format-and-top-level)
  10557. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10558. (error "No operator defined for property %s" prop))
  10559. (org-columns-compute prop)))
  10560. (defun org-property-get-allowed-values (pom property &optional table)
  10561. "Get allowed values for the property PROPERTY.
  10562. When TABLE is non-nil, return an alist that can directly be used for
  10563. completion."
  10564. (let (vals)
  10565. (cond
  10566. ((equal property "TODO")
  10567. (setq vals (org-with-point-at pom
  10568. (append org-todo-keywords-1 '("")))))
  10569. ((equal property "PRIORITY")
  10570. (let ((n org-lowest-priority))
  10571. (while (>= n org-highest-priority)
  10572. (push (char-to-string n) vals)
  10573. (setq n (1- n)))))
  10574. ((member property org-special-properties))
  10575. (t
  10576. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10577. (when (and vals (string-match "\\S-" vals))
  10578. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10579. (setq vals (mapcar (lambda (x)
  10580. (cond ((stringp x) x)
  10581. ((numberp x) (number-to-string x))
  10582. ((symbolp x) (symbol-name x))
  10583. (t "???")))
  10584. vals)))))
  10585. (if table (mapcar 'list vals) vals)))
  10586. (defun org-property-previous-allowed-value (&optional previous)
  10587. "Switch to the next allowed value for this property."
  10588. (interactive)
  10589. (org-property-next-allowed-value t))
  10590. (defun org-property-next-allowed-value (&optional previous)
  10591. "Switch to the next allowed value for this property."
  10592. (interactive)
  10593. (unless (org-at-property-p)
  10594. (error "Not at a property"))
  10595. (let* ((key (match-string 2))
  10596. (value (match-string 3))
  10597. (allowed (or (org-property-get-allowed-values (point) key)
  10598. (and (member value '("[ ]" "[-]" "[X]"))
  10599. '("[ ]" "[X]"))))
  10600. nval)
  10601. (unless allowed
  10602. (error "Allowed values for this property have not been defined"))
  10603. (if previous (setq allowed (reverse allowed)))
  10604. (if (member value allowed)
  10605. (setq nval (car (cdr (member value allowed)))))
  10606. (setq nval (or nval (car allowed)))
  10607. (if (equal nval value)
  10608. (error "Only one allowed value for this property"))
  10609. (org-at-property-p)
  10610. (replace-match (concat " :" key ": " nval) t t)
  10611. (org-indent-line-function)
  10612. (beginning-of-line 1)
  10613. (skip-chars-forward " \t")))
  10614. (defun org-find-entry-with-id (ident)
  10615. "Locate the entry that contains the ID property with exact value IDENT.
  10616. IDENT can be a string, a symbol or a number, this function will search for
  10617. the string representation of it.
  10618. Return the position where this entry starts, or nil if there is no such entry."
  10619. (interactive "sID: ")
  10620. (let ((id (cond
  10621. ((stringp ident) ident)
  10622. ((symbol-name ident) (symbol-name ident))
  10623. ((numberp ident) (number-to-string ident))
  10624. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10625. (case-fold-search nil))
  10626. (save-excursion
  10627. (save-restriction
  10628. (widen)
  10629. (goto-char (point-min))
  10630. (when (re-search-forward
  10631. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10632. nil t)
  10633. (org-back-to-heading)
  10634. (point))))))
  10635. ;;;; Timestamps
  10636. (defvar org-last-changed-timestamp nil)
  10637. (defvar org-last-inserted-timestamp nil
  10638. "The last time stamp inserted with `org-insert-time-stamp'.")
  10639. (defvar org-time-was-given) ; dynamically scoped parameter
  10640. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10641. (defvar org-ts-what) ; dynamically scoped parameter
  10642. (defun org-time-stamp (arg &optional inactive)
  10643. "Prompt for a date/time and insert a time stamp.
  10644. If the user specifies a time like HH:MM, or if this command is called
  10645. with a prefix argument, the time stamp will contain date and time.
  10646. Otherwise, only the date will be included. All parts of a date not
  10647. specified by the user will be filled in from the current date/time.
  10648. So if you press just return without typing anything, the time stamp
  10649. will represent the current date/time. If there is already a timestamp
  10650. at the cursor, it will be modified."
  10651. (interactive "P")
  10652. (let* ((ts nil)
  10653. (default-time
  10654. ;; Default time is either today, or, when entering a range,
  10655. ;; the range start.
  10656. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10657. (save-excursion
  10658. (re-search-backward
  10659. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10660. (- (point) 20) t)))
  10661. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10662. (current-time)))
  10663. (default-input (and ts (org-get-compact-tod ts)))
  10664. org-time-was-given org-end-time-was-given time)
  10665. (cond
  10666. ((and (org-at-timestamp-p t)
  10667. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10668. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10669. (insert "--")
  10670. (setq time (let ((this-command this-command))
  10671. (org-read-date arg 'totime nil nil
  10672. default-time default-input)))
  10673. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10674. ((org-at-timestamp-p t)
  10675. (setq time (let ((this-command this-command))
  10676. (org-read-date arg 'totime nil nil default-time default-input)))
  10677. (when (org-at-timestamp-p t) ; just to get the match data
  10678. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10679. (replace-match "")
  10680. (setq org-last-changed-timestamp
  10681. (org-insert-time-stamp
  10682. time (or org-time-was-given arg)
  10683. inactive nil nil (list org-end-time-was-given))))
  10684. (message "Timestamp updated"))
  10685. (t
  10686. (setq time (let ((this-command this-command))
  10687. (org-read-date arg 'totime nil nil default-time default-input)))
  10688. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10689. nil nil (list org-end-time-was-given))))))
  10690. ;; FIXME: can we use this for something else, like computing time differences?
  10691. (defun org-get-compact-tod (s)
  10692. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10693. (let* ((t1 (match-string 1 s))
  10694. (h1 (string-to-number (match-string 2 s)))
  10695. (m1 (string-to-number (match-string 3 s)))
  10696. (t2 (and (match-end 4) (match-string 5 s)))
  10697. (h2 (and t2 (string-to-number (match-string 6 s))))
  10698. (m2 (and t2 (string-to-number (match-string 7 s))))
  10699. dh dm)
  10700. (if (not t2)
  10701. t1
  10702. (setq dh (- h2 h1) dm (- m2 m1))
  10703. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10704. (concat t1 "+" (number-to-string dh)
  10705. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10706. (defun org-time-stamp-inactive (&optional arg)
  10707. "Insert an inactive time stamp.
  10708. An inactive time stamp is enclosed in square brackets instead of angle
  10709. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10710. does not link to the calendar and cannot be changed with the S-cursor keys.
  10711. So these are more for recording a certain time/date."
  10712. (interactive "P")
  10713. (org-time-stamp arg 'inactive))
  10714. (defvar org-date-ovl (org-make-overlay 1 1))
  10715. (org-overlay-put org-date-ovl 'face 'org-warning)
  10716. (org-detach-overlay org-date-ovl)
  10717. (defvar org-ans1) ; dynamically scoped parameter
  10718. (defvar org-ans2) ; dynamically scoped parameter
  10719. (defvar org-plain-time-of-day-regexp) ; defined below
  10720. (defvar org-overriding-default-time nil) ; dynamically scoped
  10721. (defvar org-read-date-overlay nil)
  10722. (defvar org-dcst nil) ; dynamically scoped
  10723. (defvar org-read-date-history nil)
  10724. (defun org-read-date (&optional with-time to-time from-string prompt
  10725. default-time default-input)
  10726. "Read a date, possibly a time, and make things smooth for the user.
  10727. The prompt will suggest to enter an ISO date, but you can also enter anything
  10728. which will at least partially be understood by `parse-time-string'.
  10729. Unrecognized parts of the date will default to the current day, month, year,
  10730. hour and minute. If this command is called to replace a timestamp at point,
  10731. of to enter the second timestamp of a range, the default time is taken from the
  10732. existing stamp. For example,
  10733. 3-2-5 --> 2003-02-05
  10734. feb 15 --> currentyear-02-15
  10735. sep 12 9 --> 2009-09-12
  10736. 12:45 --> today 12:45
  10737. 22 sept 0:34 --> currentyear-09-22 0:34
  10738. 12 --> currentyear-currentmonth-12
  10739. Fri --> nearest Friday (today or later)
  10740. etc.
  10741. Furthermore you can specify a relative date by giving, as the *first* thing
  10742. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10743. change in days weeks, months, years.
  10744. With a single plus or minus, the date is relative to today. With a double
  10745. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10746. +4d --> four days from today
  10747. +4 --> same as above
  10748. +2w --> two weeks from today
  10749. ++5 --> five days from default date
  10750. The function understands only English month and weekday abbreviations,
  10751. but this can be configured with the variables `parse-time-months' and
  10752. `parse-time-weekdays'.
  10753. While prompting, a calendar is popped up - you can also select the
  10754. date with the mouse (button 1). The calendar shows a period of three
  10755. months. To scroll it to other months, use the keys `>' and `<'.
  10756. If you don't like the calendar, turn it off with
  10757. \(setq org-read-date-popup-calendar nil)
  10758. With optional argument TO-TIME, the date will immediately be converted
  10759. to an internal time.
  10760. With an optional argument WITH-TIME, the prompt will suggest to also
  10761. insert a time. Note that when WITH-TIME is not set, you can still
  10762. enter a time, and this function will inform the calling routine about
  10763. this change. The calling routine may then choose to change the format
  10764. used to insert the time stamp into the buffer to include the time.
  10765. With optional argument FROM-STRING, read from this string instead from
  10766. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10767. the time/date that is used for everything that is not specified by the
  10768. user."
  10769. (require 'parse-time)
  10770. (let* ((org-time-stamp-rounding-minutes
  10771. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10772. (org-dcst org-display-custom-times)
  10773. (ct (org-current-time))
  10774. (def (or org-overriding-default-time default-time ct))
  10775. (defdecode (decode-time def))
  10776. (dummy (progn
  10777. (when (< (nth 2 defdecode) org-extend-today-until)
  10778. (setcar (nthcdr 2 defdecode) -1)
  10779. (setcar (nthcdr 1 defdecode) 59)
  10780. (setq def (apply 'encode-time defdecode)
  10781. defdecode (decode-time def)))))
  10782. (calendar-move-hook nil)
  10783. (calendar-view-diary-initially-flag nil)
  10784. (view-diary-entries-initially nil)
  10785. (calendar-view-holidays-initially-flag nil)
  10786. (view-calendar-holidays-initially nil)
  10787. (timestr (format-time-string
  10788. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10789. (prompt (concat (if prompt (concat prompt " ") "")
  10790. (format "Date+time [%s]: " timestr)))
  10791. ans (org-ans0 "") org-ans1 org-ans2 final)
  10792. (cond
  10793. (from-string (setq ans from-string))
  10794. (org-read-date-popup-calendar
  10795. (save-excursion
  10796. (save-window-excursion
  10797. (calendar)
  10798. (calendar-forward-day (- (time-to-days def)
  10799. (calendar-absolute-from-gregorian
  10800. (calendar-current-date))))
  10801. (org-eval-in-calendar nil t)
  10802. (let* ((old-map (current-local-map))
  10803. (map (copy-keymap calendar-mode-map))
  10804. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10805. (org-defkey map (kbd "RET") 'org-calendar-select)
  10806. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10807. 'org-calendar-select-mouse)
  10808. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10809. 'org-calendar-select-mouse)
  10810. (org-defkey minibuffer-local-map [(meta shift left)]
  10811. (lambda () (interactive)
  10812. (org-eval-in-calendar '(calendar-backward-month 1))))
  10813. (org-defkey minibuffer-local-map [(meta shift right)]
  10814. (lambda () (interactive)
  10815. (org-eval-in-calendar '(calendar-forward-month 1))))
  10816. (org-defkey minibuffer-local-map [(meta shift up)]
  10817. (lambda () (interactive)
  10818. (org-eval-in-calendar '(calendar-backward-year 1))))
  10819. (org-defkey minibuffer-local-map [(meta shift down)]
  10820. (lambda () (interactive)
  10821. (org-eval-in-calendar '(calendar-forward-year 1))))
  10822. (org-defkey minibuffer-local-map [(shift up)]
  10823. (lambda () (interactive)
  10824. (org-eval-in-calendar '(calendar-backward-week 1))))
  10825. (org-defkey minibuffer-local-map [(shift down)]
  10826. (lambda () (interactive)
  10827. (org-eval-in-calendar '(calendar-forward-week 1))))
  10828. (org-defkey minibuffer-local-map [(shift left)]
  10829. (lambda () (interactive)
  10830. (org-eval-in-calendar '(calendar-backward-day 1))))
  10831. (org-defkey minibuffer-local-map [(shift right)]
  10832. (lambda () (interactive)
  10833. (org-eval-in-calendar '(calendar-forward-day 1))))
  10834. (org-defkey minibuffer-local-map ">"
  10835. (lambda () (interactive)
  10836. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10837. (org-defkey minibuffer-local-map "<"
  10838. (lambda () (interactive)
  10839. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10840. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10841. (unwind-protect
  10842. (progn
  10843. (use-local-map map)
  10844. (add-hook 'post-command-hook 'org-read-date-display)
  10845. (setq org-ans0 (read-string prompt default-input
  10846. 'org-read-date-history nil))
  10847. ;; org-ans0: from prompt
  10848. ;; org-ans1: from mouse click
  10849. ;; org-ans2: from calendar motion
  10850. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10851. (remove-hook 'post-command-hook 'org-read-date-display)
  10852. (use-local-map old-map)
  10853. (when org-read-date-overlay
  10854. (org-delete-overlay org-read-date-overlay)
  10855. (setq org-read-date-overlay nil)))))))
  10856. (t ; Naked prompt only
  10857. (unwind-protect
  10858. (setq ans (read-string prompt default-input
  10859. 'org-read-date-history timestr))
  10860. (when org-read-date-overlay
  10861. (org-delete-overlay org-read-date-overlay)
  10862. (setq org-read-date-overlay nil)))))
  10863. (setq final (org-read-date-analyze ans def defdecode))
  10864. (if to-time
  10865. (apply 'encode-time final)
  10866. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10867. (format "%04d-%02d-%02d %02d:%02d"
  10868. (nth 5 final) (nth 4 final) (nth 3 final)
  10869. (nth 2 final) (nth 1 final))
  10870. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10871. (defvar def)
  10872. (defvar defdecode)
  10873. (defvar with-time)
  10874. (defun org-read-date-display ()
  10875. "Display the current date prompt interpretation in the minibuffer."
  10876. (when org-read-date-display-live
  10877. (when org-read-date-overlay
  10878. (org-delete-overlay org-read-date-overlay))
  10879. (let ((p (point)))
  10880. (end-of-line 1)
  10881. (while (not (equal (buffer-substring
  10882. (max (point-min) (- (point) 4)) (point))
  10883. " "))
  10884. (insert " "))
  10885. (goto-char p))
  10886. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10887. " " (or org-ans1 org-ans2)))
  10888. (org-end-time-was-given nil)
  10889. (f (org-read-date-analyze ans def defdecode))
  10890. (fmts (if org-dcst
  10891. org-time-stamp-custom-formats
  10892. org-time-stamp-formats))
  10893. (fmt (if (or with-time
  10894. (and (boundp 'org-time-was-given) org-time-was-given))
  10895. (cdr fmts)
  10896. (car fmts)))
  10897. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10898. (when (and org-end-time-was-given
  10899. (string-match org-plain-time-of-day-regexp txt))
  10900. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10901. org-end-time-was-given
  10902. (substring txt (match-end 0)))))
  10903. (setq org-read-date-overlay
  10904. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10905. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10906. (defun org-read-date-analyze (ans def defdecode)
  10907. "Analyse the combined answer of the date prompt."
  10908. ;; FIXME: cleanup and comment
  10909. (let (delta deltan deltaw deltadef year month day
  10910. hour minute second wday pm h2 m2 tl wday1
  10911. iso-year iso-weekday iso-week iso-year iso-date)
  10912. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10913. (setq ans "+0"))
  10914. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10915. (setq ans (replace-match "" t t ans)
  10916. deltan (car delta)
  10917. deltaw (nth 1 delta)
  10918. deltadef (nth 2 delta)))
  10919. ;; Check if there is an iso week date in there
  10920. ;; If yes, sore the info and postpone interpreting it until the rest
  10921. ;; of the parsing is done
  10922. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10923. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10924. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10925. iso-week (string-to-number (match-string 2 ans)))
  10926. (setq ans (replace-match "" t t ans)))
  10927. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10928. (when (string-match
  10929. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10930. (setq year (if (match-end 2)
  10931. (string-to-number (match-string 2 ans))
  10932. (string-to-number (format-time-string "%Y")))
  10933. month (string-to-number (match-string 3 ans))
  10934. day (string-to-number (match-string 4 ans)))
  10935. (if (< year 100) (setq year (+ 2000 year)))
  10936. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10937. t nil ans)))
  10938. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10939. ;; If there is a time with am/pm, and *no* time without it, we convert
  10940. ;; so that matching will be successful.
  10941. (loop for i from 1 to 2 do ; twice, for end time as well
  10942. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10943. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10944. (setq hour (string-to-number (match-string 1 ans))
  10945. minute (if (match-end 3)
  10946. (string-to-number (match-string 3 ans))
  10947. 0)
  10948. pm (equal ?p
  10949. (string-to-char (downcase (match-string 4 ans)))))
  10950. (if (and (= hour 12) (not pm))
  10951. (setq hour 0)
  10952. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10953. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10954. t t ans))))
  10955. ;; Check if a time range is given as a duration
  10956. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10957. (setq hour (string-to-number (match-string 1 ans))
  10958. h2 (+ hour (string-to-number (match-string 3 ans)))
  10959. minute (string-to-number (match-string 2 ans))
  10960. m2 (+ minute (if (match-end 5) (string-to-number
  10961. (match-string 5 ans))0)))
  10962. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10963. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10964. t t ans)))
  10965. ;; Check if there is a time range
  10966. (when (boundp 'org-end-time-was-given)
  10967. (setq org-time-was-given nil)
  10968. (when (and (string-match org-plain-time-of-day-regexp ans)
  10969. (match-end 8))
  10970. (setq org-end-time-was-given (match-string 8 ans))
  10971. (setq ans (concat (substring ans 0 (match-beginning 7))
  10972. (substring ans (match-end 7))))))
  10973. (setq tl (parse-time-string ans)
  10974. day (or (nth 3 tl) (nth 3 defdecode))
  10975. month (or (nth 4 tl)
  10976. (if (and org-read-date-prefer-future
  10977. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10978. (1+ (nth 4 defdecode))
  10979. (nth 4 defdecode)))
  10980. year (or (nth 5 tl)
  10981. (if (and org-read-date-prefer-future
  10982. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10983. (1+ (nth 5 defdecode))
  10984. (nth 5 defdecode)))
  10985. hour (or (nth 2 tl) (nth 2 defdecode))
  10986. minute (or (nth 1 tl) (nth 1 defdecode))
  10987. second (or (nth 0 tl) 0)
  10988. wday (nth 6 tl))
  10989. ;; Special date definitions below
  10990. (cond
  10991. (iso-week
  10992. ;; There was an iso week
  10993. (setq year (or iso-year year)
  10994. day (or iso-weekday wday 1)
  10995. wday nil ; to make sure that the trigger below does not match
  10996. iso-date (calendar-gregorian-from-absolute
  10997. (calendar-absolute-from-iso
  10998. (list iso-week day year))))
  10999. ; FIXME: Should we also push ISO weeks into the future?
  11000. ; (when (and org-read-date-prefer-future
  11001. ; (not iso-year)
  11002. ; (< (calendar-absolute-from-gregorian iso-date)
  11003. ; (time-to-days (current-time))))
  11004. ; (setq year (1+ year)
  11005. ; iso-date (calendar-gregorian-from-absolute
  11006. ; (calendar-absolute-from-iso
  11007. ; (list iso-week day year)))))
  11008. (setq month (car iso-date)
  11009. year (nth 2 iso-date)
  11010. day (nth 1 iso-date)))
  11011. (deltan
  11012. (unless deltadef
  11013. (let ((now (decode-time (current-time))))
  11014. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11015. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11016. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11017. ((equal deltaw "m") (setq month (+ month deltan)))
  11018. ((equal deltaw "y") (setq year (+ year deltan)))))
  11019. ((and wday (not (nth 3 tl)))
  11020. ;; Weekday was given, but no day, so pick that day in the week
  11021. ;; on or after the derived date.
  11022. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11023. (unless (equal wday wday1)
  11024. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11025. (if (and (boundp 'org-time-was-given)
  11026. (nth 2 tl))
  11027. (setq org-time-was-given t))
  11028. (if (< year 100) (setq year (+ 2000 year)))
  11029. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11030. (list second minute hour day month year)))
  11031. (defvar parse-time-weekdays)
  11032. (defun org-read-date-get-relative (s today default)
  11033. "Check string S for special relative date string.
  11034. TODAY and DEFAULT are internal times, for today and for a default.
  11035. Return shift list (N what def-flag)
  11036. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11037. N is the number of WHATs to shift.
  11038. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11039. the DEFAULT date rather than TODAY."
  11040. (when (and
  11041. (string-match
  11042. (concat
  11043. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11044. "\\([0-9]+\\)?"
  11045. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11046. "\\([ \t]\\|$\\)") s)
  11047. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11048. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11049. (string-to-char (substring (match-string 1 s) -1))
  11050. ?+))
  11051. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11052. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11053. (what (if (match-end 3) (match-string 3 s) "d"))
  11054. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11055. (date (if rel default today))
  11056. (wday (nth 6 (decode-time date)))
  11057. delta)
  11058. (if wday1
  11059. (progn
  11060. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11061. (if (= dir ?-) (setq delta (- delta 7)))
  11062. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11063. (list delta "d" rel))
  11064. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11065. (defun org-eval-in-calendar (form &optional keepdate)
  11066. "Eval FORM in the calendar window and return to current window.
  11067. Also, store the cursor date in variable org-ans2."
  11068. (let ((sw (selected-window)))
  11069. (select-window (get-buffer-window "*Calendar*"))
  11070. (eval form)
  11071. (when (and (not keepdate) (calendar-cursor-to-date))
  11072. (let* ((date (calendar-cursor-to-date))
  11073. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11074. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11075. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11076. (select-window sw)))
  11077. (defun org-calendar-select ()
  11078. "Return to `org-read-date' with the date currently selected.
  11079. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11080. (interactive)
  11081. (when (calendar-cursor-to-date)
  11082. (let* ((date (calendar-cursor-to-date))
  11083. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11084. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11085. (if (active-minibuffer-window) (exit-minibuffer))))
  11086. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11087. "Insert a date stamp for the date given by the internal TIME.
  11088. WITH-HM means, use the stamp format that includes the time of the day.
  11089. INACTIVE means use square brackets instead of angular ones, so that the
  11090. stamp will not contribute to the agenda.
  11091. PRE and POST are optional strings to be inserted before and after the
  11092. stamp.
  11093. The command returns the inserted time stamp."
  11094. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11095. stamp)
  11096. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11097. (insert-before-markers (or pre ""))
  11098. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11099. (when (listp extra)
  11100. (setq extra (car extra))
  11101. (if (and (stringp extra)
  11102. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11103. (setq extra (format "-%02d:%02d"
  11104. (string-to-number (match-string 1 extra))
  11105. (string-to-number (match-string 2 extra))))
  11106. (setq extra nil)))
  11107. (when extra
  11108. (backward-char 1)
  11109. (insert-before-markers extra)
  11110. (forward-char 1))
  11111. (insert-before-markers (or post ""))
  11112. (setq org-last-inserted-timestamp stamp)))
  11113. (defun org-toggle-time-stamp-overlays ()
  11114. "Toggle the use of custom time stamp formats."
  11115. (interactive)
  11116. (setq org-display-custom-times (not org-display-custom-times))
  11117. (unless org-display-custom-times
  11118. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11119. (while (setq p (next-single-property-change p 'display))
  11120. (if (and (get-text-property p 'display)
  11121. (eq (get-text-property p 'face) 'org-date))
  11122. (remove-text-properties
  11123. p (setq p (next-single-property-change p 'display))
  11124. '(display t))))
  11125. (set-buffer-modified-p bmp)))
  11126. (if (featurep 'xemacs)
  11127. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11128. (org-restart-font-lock)
  11129. (setq org-table-may-need-update t)
  11130. (if org-display-custom-times
  11131. (message "Time stamps are overlayed with custom format")
  11132. (message "Time stamp overlays removed")))
  11133. (defun org-display-custom-time (beg end)
  11134. "Overlay modified time stamp format over timestamp between BEG and END."
  11135. (let* ((ts (buffer-substring beg end))
  11136. t1 w1 with-hm tf time str w2 (off 0))
  11137. (save-match-data
  11138. (setq t1 (org-parse-time-string ts t))
  11139. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11140. (setq off (- (match-end 0) (match-beginning 0)))))
  11141. (setq end (- end off))
  11142. (setq w1 (- end beg)
  11143. with-hm (and (nth 1 t1) (nth 2 t1))
  11144. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11145. time (org-fix-decoded-time t1)
  11146. str (org-add-props
  11147. (format-time-string
  11148. (substring tf 1 -1) (apply 'encode-time time))
  11149. nil 'mouse-face 'highlight)
  11150. w2 (length str))
  11151. (if (not (= w2 w1))
  11152. (add-text-properties (1+ beg) (+ 2 beg)
  11153. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11154. (if (featurep 'xemacs)
  11155. (progn
  11156. (put-text-property beg end 'invisible t)
  11157. (put-text-property beg end 'end-glyph (make-glyph str)))
  11158. (put-text-property beg end 'display str))))
  11159. (defun org-translate-time (string)
  11160. "Translate all timestamps in STRING to custom format.
  11161. But do this only if the variable `org-display-custom-times' is set."
  11162. (when org-display-custom-times
  11163. (save-match-data
  11164. (let* ((start 0)
  11165. (re org-ts-regexp-both)
  11166. t1 with-hm inactive tf time str beg end)
  11167. (while (setq start (string-match re string start))
  11168. (setq beg (match-beginning 0)
  11169. end (match-end 0)
  11170. t1 (save-match-data
  11171. (org-parse-time-string (substring string beg end) t))
  11172. with-hm (and (nth 1 t1) (nth 2 t1))
  11173. inactive (equal (substring string beg (1+ beg)) "[")
  11174. tf (funcall (if with-hm 'cdr 'car)
  11175. org-time-stamp-custom-formats)
  11176. time (org-fix-decoded-time t1)
  11177. str (format-time-string
  11178. (concat
  11179. (if inactive "[" "<") (substring tf 1 -1)
  11180. (if inactive "]" ">"))
  11181. (apply 'encode-time time))
  11182. string (replace-match str t t string)
  11183. start (+ start (length str)))))))
  11184. string)
  11185. (defun org-fix-decoded-time (time)
  11186. "Set 0 instead of nil for the first 6 elements of time.
  11187. Don't touch the rest."
  11188. (let ((n 0))
  11189. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11190. (defun org-days-to-time (timestamp-string)
  11191. "Difference between TIMESTAMP-STRING and now in days."
  11192. (- (time-to-days (org-time-string-to-time timestamp-string))
  11193. (time-to-days (current-time))))
  11194. (defun org-deadline-close (timestamp-string &optional ndays)
  11195. "Is the time in TIMESTAMP-STRING close to the current date?"
  11196. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11197. (and (< (org-days-to-time timestamp-string) ndays)
  11198. (not (org-entry-is-done-p))))
  11199. (defun org-get-wdays (ts)
  11200. "Get the deadline lead time appropriate for timestring TS."
  11201. (cond
  11202. ((<= org-deadline-warning-days 0)
  11203. ;; 0 or negative, enforce this value no matter what
  11204. (- org-deadline-warning-days))
  11205. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11206. ;; lead time is specified.
  11207. (floor (* (string-to-number (match-string 1 ts))
  11208. (cdr (assoc (match-string 2 ts)
  11209. '(("d" . 1) ("w" . 7)
  11210. ("m" . 30.4) ("y" . 365.25)))))))
  11211. ;; go for the default.
  11212. (t org-deadline-warning-days)))
  11213. (defun org-calendar-select-mouse (ev)
  11214. "Return to `org-read-date' with the date currently selected.
  11215. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11216. (interactive "e")
  11217. (mouse-set-point ev)
  11218. (when (calendar-cursor-to-date)
  11219. (let* ((date (calendar-cursor-to-date))
  11220. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11221. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11222. (if (active-minibuffer-window) (exit-minibuffer))))
  11223. (defun org-check-deadlines (ndays)
  11224. "Check if there are any deadlines due or past due.
  11225. A deadline is considered due if it happens within `org-deadline-warning-days'
  11226. days from today's date. If the deadline appears in an entry marked DONE,
  11227. it is not shown. The prefix arg NDAYS can be used to test that many
  11228. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11229. (interactive "P")
  11230. (let* ((org-warn-days
  11231. (cond
  11232. ((equal ndays '(4)) 100000)
  11233. (ndays (prefix-numeric-value ndays))
  11234. (t (abs org-deadline-warning-days))))
  11235. (case-fold-search nil)
  11236. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11237. (callback
  11238. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11239. (message "%d deadlines past-due or due within %d days"
  11240. (org-occur regexp nil callback)
  11241. org-warn-days)))
  11242. (defun org-check-before-date (date)
  11243. "Check if there are deadlines or scheduled entries before DATE."
  11244. (interactive (list (org-read-date)))
  11245. (let ((case-fold-search nil)
  11246. (regexp (concat "\\<\\(" org-deadline-string
  11247. "\\|" org-scheduled-string
  11248. "\\) *<\\([^>]+\\)>"))
  11249. (callback
  11250. (lambda () (time-less-p
  11251. (org-time-string-to-time (match-string 2))
  11252. (org-time-string-to-time date)))))
  11253. (message "%d entries before %s"
  11254. (org-occur regexp nil callback) date)))
  11255. (defun org-check-after-date (date)
  11256. "Check if there are deadlines or scheduled entries after DATE."
  11257. (interactive (list (org-read-date)))
  11258. (let ((case-fold-search nil)
  11259. (regexp (concat "\\<\\(" org-deadline-string
  11260. "\\|" org-scheduled-string
  11261. "\\) *<\\([^>]+\\)>"))
  11262. (callback
  11263. (lambda () (not
  11264. (time-less-p
  11265. (org-time-string-to-time (match-string 2))
  11266. (org-time-string-to-time date))))))
  11267. (message "%d entries after %s"
  11268. (org-occur regexp nil callback) date)))
  11269. (defun org-evaluate-time-range (&optional to-buffer)
  11270. "Evaluate a time range by computing the difference between start and end.
  11271. Normally the result is just printed in the echo area, but with prefix arg
  11272. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11273. If the time range is actually in a table, the result is inserted into the
  11274. next column.
  11275. For time difference computation, a year is assumed to be exactly 365
  11276. days in order to avoid rounding problems."
  11277. (interactive "P")
  11278. (or
  11279. (org-clock-update-time-maybe)
  11280. (save-excursion
  11281. (unless (org-at-date-range-p t)
  11282. (goto-char (point-at-bol))
  11283. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11284. (if (not (org-at-date-range-p t))
  11285. (error "Not at a time-stamp range, and none found in current line")))
  11286. (let* ((ts1 (match-string 1))
  11287. (ts2 (match-string 2))
  11288. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11289. (match-end (match-end 0))
  11290. (time1 (org-time-string-to-time ts1))
  11291. (time2 (org-time-string-to-time ts2))
  11292. (t1 (time-to-seconds time1))
  11293. (t2 (time-to-seconds time2))
  11294. (diff (abs (- t2 t1)))
  11295. (negative (< (- t2 t1) 0))
  11296. ;; (ys (floor (* 365 24 60 60)))
  11297. (ds (* 24 60 60))
  11298. (hs (* 60 60))
  11299. (fy "%dy %dd %02d:%02d")
  11300. (fy1 "%dy %dd")
  11301. (fd "%dd %02d:%02d")
  11302. (fd1 "%dd")
  11303. (fh "%02d:%02d")
  11304. y d h m align)
  11305. (if havetime
  11306. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11307. y 0
  11308. d (floor (/ diff ds)) diff (mod diff ds)
  11309. h (floor (/ diff hs)) diff (mod diff hs)
  11310. m (floor (/ diff 60)))
  11311. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11312. y 0
  11313. d (floor (+ (/ diff ds) 0.5))
  11314. h 0 m 0))
  11315. (if (not to-buffer)
  11316. (message "%s" (org-make-tdiff-string y d h m))
  11317. (if (org-at-table-p)
  11318. (progn
  11319. (goto-char match-end)
  11320. (setq align t)
  11321. (and (looking-at " *|") (goto-char (match-end 0))))
  11322. (goto-char match-end))
  11323. (if (looking-at
  11324. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11325. (replace-match ""))
  11326. (if negative (insert " -"))
  11327. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11328. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11329. (insert " " (format fh h m))))
  11330. (if align (org-table-align))
  11331. (message "Time difference inserted")))))
  11332. (defun org-make-tdiff-string (y d h m)
  11333. (let ((fmt "")
  11334. (l nil))
  11335. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11336. l (push y l)))
  11337. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11338. l (push d l)))
  11339. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11340. l (push h l)))
  11341. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11342. l (push m l)))
  11343. (apply 'format fmt (nreverse l))))
  11344. (defun org-time-string-to-time (s)
  11345. (apply 'encode-time (org-parse-time-string s)))
  11346. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11347. "Convert a time stamp to an absolute day number.
  11348. If there is a specifyer for a cyclic time stamp, get the closest date to
  11349. DAYNR.
  11350. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11351. (cond
  11352. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11353. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11354. daynr
  11355. (+ daynr 1000)))
  11356. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11357. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11358. (time-to-days (current-time))) (match-string 0 s)
  11359. prefer show-all))
  11360. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11361. (defun org-days-to-iso-week (days)
  11362. "Return the iso week number."
  11363. (require 'cal-iso)
  11364. (car (calendar-iso-from-absolute days)))
  11365. (defun org-small-year-to-year (year)
  11366. "Convert 2-digit years into 4-digit years.
  11367. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11368. The year 2000 cannot be abbreviated. Any year larger than 99
  11369. is returned unchanged."
  11370. (if (< year 38)
  11371. (setq year (+ 2000 year))
  11372. (if (< year 100)
  11373. (setq year (+ 1900 year))))
  11374. year)
  11375. (defun org-time-from-absolute (d)
  11376. "Return the time corresponding to date D.
  11377. D may be an absolute day number, or a calendar-type list (month day year)."
  11378. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11379. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11380. (defun org-calendar-holiday ()
  11381. "List of holidays, for Diary display in Org-mode."
  11382. (require 'holidays)
  11383. (let ((hl (funcall
  11384. (if (fboundp 'calendar-check-holidays)
  11385. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11386. (if hl (mapconcat 'identity hl "; "))))
  11387. (defun org-diary-sexp-entry (sexp entry date)
  11388. "Process a SEXP diary ENTRY for DATE."
  11389. (require 'diary-lib)
  11390. (let ((result (if calendar-debug-sexp
  11391. (let ((stack-trace-on-error t))
  11392. (eval (car (read-from-string sexp))))
  11393. (condition-case nil
  11394. (eval (car (read-from-string sexp)))
  11395. (error
  11396. (beep)
  11397. (message "Bad sexp at line %d in %s: %s"
  11398. (org-current-line)
  11399. (buffer-file-name) sexp)
  11400. (sleep-for 2))))))
  11401. (cond ((stringp result) result)
  11402. ((and (consp result)
  11403. (stringp (cdr result))) (cdr result))
  11404. (result entry)
  11405. (t nil))))
  11406. (defun org-diary-to-ical-string (frombuf)
  11407. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11408. This uses the icalendar.el library."
  11409. (let* ((tmpdir (if (featurep 'xemacs)
  11410. (temp-directory)
  11411. temporary-file-directory))
  11412. (tmpfile (make-temp-name
  11413. (expand-file-name "orgics" tmpdir)))
  11414. buf rtn b e)
  11415. (save-excursion
  11416. (set-buffer frombuf)
  11417. (icalendar-export-region (point-min) (point-max) tmpfile)
  11418. (setq buf (find-buffer-visiting tmpfile))
  11419. (set-buffer buf)
  11420. (goto-char (point-min))
  11421. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11422. (setq b (match-beginning 0)))
  11423. (goto-char (point-max))
  11424. (if (re-search-backward "^END:VEVENT" nil t)
  11425. (setq e (match-end 0)))
  11426. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11427. (kill-buffer buf)
  11428. (delete-file tmpfile)
  11429. rtn))
  11430. (defun org-closest-date (start current change prefer show-all)
  11431. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11432. When PREFER is `past' return a date that is either CURRENT or past.
  11433. When PREFER is `future', return a date that is either CURRENT or future.
  11434. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11435. ;; Make the proper lists from the dates
  11436. (catch 'exit
  11437. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11438. dn dw sday cday n1 n2 n0
  11439. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11440. (setq start (org-date-to-gregorian start)
  11441. current (org-date-to-gregorian
  11442. (if show-all
  11443. current
  11444. (time-to-days (current-time))))
  11445. sday (calendar-absolute-from-gregorian start)
  11446. cday (calendar-absolute-from-gregorian current))
  11447. (if (<= cday sday) (throw 'exit sday))
  11448. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11449. (setq dn (string-to-number (match-string 1 change))
  11450. dw (cdr (assoc (match-string 2 change) a1)))
  11451. (error "Invalid change specifyer: %s" change))
  11452. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11453. (cond
  11454. ((eq dw 'day)
  11455. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11456. n2 (+ n1 dn)))
  11457. ((eq dw 'year)
  11458. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11459. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11460. (setq date1 (list m d y1)
  11461. n1 (calendar-absolute-from-gregorian date1)
  11462. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11463. n2 (calendar-absolute-from-gregorian date2)))
  11464. ((eq dw 'month)
  11465. ;; approx number of month between the two dates
  11466. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11467. ;; How often does dn fit in there?
  11468. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11469. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11470. m (+ m nm)
  11471. ny (floor (/ m 12))
  11472. y (+ y ny)
  11473. m (- m (* ny 12)))
  11474. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11475. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11476. (setq m2 (+ m dn) y2 y)
  11477. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11478. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11479. (while (<= n2 cday)
  11480. (setq n1 n2 m m2 y y2)
  11481. (setq m2 (+ m dn) y2 y)
  11482. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11483. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11484. ;; Make sure n1 is the earlier date
  11485. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11486. (if show-all
  11487. (cond
  11488. ((eq prefer 'past) n1)
  11489. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11490. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11491. (cond
  11492. ((eq prefer 'past) n1)
  11493. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11494. (t (if (= cday n1) n1 n2)))))))
  11495. (defun org-date-to-gregorian (date)
  11496. "Turn any specification of DATE into a gregorian date for the calendar."
  11497. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11498. ((and (listp date) (= (length date) 3)) date)
  11499. ((stringp date)
  11500. (setq date (org-parse-time-string date))
  11501. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11502. ((listp date)
  11503. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11504. (defun org-parse-time-string (s &optional nodefault)
  11505. "Parse the standard Org-mode time string.
  11506. This should be a lot faster than the normal `parse-time-string'.
  11507. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11508. hour and minute fields will be nil if not given."
  11509. (if (string-match org-ts-regexp0 s)
  11510. (list 0
  11511. (if (or (match-beginning 8) (not nodefault))
  11512. (string-to-number (or (match-string 8 s) "0")))
  11513. (if (or (match-beginning 7) (not nodefault))
  11514. (string-to-number (or (match-string 7 s) "0")))
  11515. (string-to-number (match-string 4 s))
  11516. (string-to-number (match-string 3 s))
  11517. (string-to-number (match-string 2 s))
  11518. nil nil nil)
  11519. (make-list 9 0)))
  11520. (defun org-timestamp-up (&optional arg)
  11521. "Increase the date item at the cursor by one.
  11522. If the cursor is on the year, change the year. If it is on the month or
  11523. the day, change that.
  11524. With prefix ARG, change by that many units."
  11525. (interactive "p")
  11526. (org-timestamp-change (prefix-numeric-value arg)))
  11527. (defun org-timestamp-down (&optional arg)
  11528. "Decrease the date item at the cursor by one.
  11529. If the cursor is on the year, change the year. If it is on the month or
  11530. the day, change that.
  11531. With prefix ARG, change by that many units."
  11532. (interactive "p")
  11533. (org-timestamp-change (- (prefix-numeric-value arg))))
  11534. (defun org-timestamp-up-day (&optional arg)
  11535. "Increase the date in the time stamp by one day.
  11536. With prefix ARG, change that many days."
  11537. (interactive "p")
  11538. (if (and (not (org-at-timestamp-p t))
  11539. (org-on-heading-p))
  11540. (org-todo 'up)
  11541. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11542. (defun org-timestamp-down-day (&optional arg)
  11543. "Decrease the date in the time stamp by one day.
  11544. With prefix ARG, change that many days."
  11545. (interactive "p")
  11546. (if (and (not (org-at-timestamp-p t))
  11547. (org-on-heading-p))
  11548. (org-todo 'down)
  11549. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11550. (defun org-at-timestamp-p (&optional inactive-ok)
  11551. "Determine if the cursor is in or at a timestamp."
  11552. (interactive)
  11553. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11554. (pos (point))
  11555. (ans (or (looking-at tsr)
  11556. (save-excursion
  11557. (skip-chars-backward "^[<\n\r\t")
  11558. (if (> (point) (point-min)) (backward-char 1))
  11559. (and (looking-at tsr)
  11560. (> (- (match-end 0) pos) -1))))))
  11561. (and ans
  11562. (boundp 'org-ts-what)
  11563. (setq org-ts-what
  11564. (cond
  11565. ((= pos (match-beginning 0)) 'bracket)
  11566. ((= pos (1- (match-end 0))) 'bracket)
  11567. ((org-pos-in-match-range pos 2) 'year)
  11568. ((org-pos-in-match-range pos 3) 'month)
  11569. ((org-pos-in-match-range pos 7) 'hour)
  11570. ((org-pos-in-match-range pos 8) 'minute)
  11571. ((or (org-pos-in-match-range pos 4)
  11572. (org-pos-in-match-range pos 5)) 'day)
  11573. ((and (> pos (or (match-end 8) (match-end 5)))
  11574. (< pos (match-end 0)))
  11575. (- pos (or (match-end 8) (match-end 5))))
  11576. (t 'day))))
  11577. ans))
  11578. (defun org-toggle-timestamp-type ()
  11579. "Toggle the type (<active> or [inactive]) of a time stamp."
  11580. (interactive)
  11581. (when (org-at-timestamp-p t)
  11582. (let ((beg (match-beginning 0)) (end (match-end 0))
  11583. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11584. (save-excursion
  11585. (goto-char beg)
  11586. (while (re-search-forward "[][<>]" end t)
  11587. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11588. t t)))
  11589. (message "Timestamp is now %sactive"
  11590. (if (equal (char-after beg) ?<) "" "in")))))
  11591. (defun org-timestamp-change (n &optional what)
  11592. "Change the date in the time stamp at point.
  11593. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11594. `year', `minute', `second'. If WHAT is not given, the cursor position
  11595. in the timestamp determines what will be changed."
  11596. (let ((pos (point))
  11597. with-hm inactive
  11598. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11599. org-ts-what
  11600. extra rem
  11601. ts time time0)
  11602. (if (not (org-at-timestamp-p t))
  11603. (error "Not at a timestamp"))
  11604. (if (and (not what) (eq org-ts-what 'bracket))
  11605. (org-toggle-timestamp-type)
  11606. (if (and (not what) (not (eq org-ts-what 'day))
  11607. org-display-custom-times
  11608. (get-text-property (point) 'display)
  11609. (not (get-text-property (1- (point)) 'display)))
  11610. (setq org-ts-what 'day))
  11611. (setq org-ts-what (or what org-ts-what)
  11612. inactive (= (char-after (match-beginning 0)) ?\[)
  11613. ts (match-string 0))
  11614. (replace-match "")
  11615. (if (string-match
  11616. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11617. ts)
  11618. (setq extra (match-string 1 ts)))
  11619. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11620. (setq with-hm t))
  11621. (setq time0 (org-parse-time-string ts))
  11622. (when (and (eq org-ts-what 'minute)
  11623. (eq current-prefix-arg nil))
  11624. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11625. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11626. (setcar (cdr time0) (+ (nth 1 time0)
  11627. (if (> n 0) (- rem) (- dm rem))))))
  11628. (setq time
  11629. (encode-time (or (car time0) 0)
  11630. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11631. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11632. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11633. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11634. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11635. (nthcdr 6 time0)))
  11636. (when (and (member org-ts-what '(hour minute))
  11637. extra
  11638. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11639. (setq extra (org-modify-ts-extra
  11640. extra
  11641. (if (eq org-ts-what 'hour) 2 5)
  11642. n dm)))
  11643. (when (integerp org-ts-what)
  11644. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11645. (if (eq what 'calendar)
  11646. (let ((cal-date (org-get-date-from-calendar)))
  11647. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11648. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11649. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11650. (setcar time0 (or (car time0) 0))
  11651. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11652. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11653. (setq time (apply 'encode-time time0))))
  11654. (setq org-last-changed-timestamp
  11655. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11656. (org-clock-update-time-maybe)
  11657. (goto-char pos)
  11658. ;; Try to recenter the calendar window, if any
  11659. (if (and org-calendar-follow-timestamp-change
  11660. (get-buffer-window "*Calendar*" t)
  11661. (memq org-ts-what '(day month year)))
  11662. (org-recenter-calendar (time-to-days time))))))
  11663. (defun org-modify-ts-extra (s pos n dm)
  11664. "Change the different parts of the lead-time and repeat fields in timestamp."
  11665. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11666. ng h m new rem)
  11667. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11668. (cond
  11669. ((or (org-pos-in-match-range pos 2)
  11670. (org-pos-in-match-range pos 3))
  11671. (setq m (string-to-number (match-string 3 s))
  11672. h (string-to-number (match-string 2 s)))
  11673. (if (org-pos-in-match-range pos 2)
  11674. (setq h (+ h n))
  11675. (setq n (* dm (org-no-warnings (signum n))))
  11676. (when (not (= 0 (setq rem (% m dm))))
  11677. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11678. (setq m (+ m n)))
  11679. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11680. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11681. (setq h (min 24 (max 0 h)))
  11682. (setq ng 1 new (format "-%02d:%02d" h m)))
  11683. ((org-pos-in-match-range pos 6)
  11684. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11685. ((org-pos-in-match-range pos 5)
  11686. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11687. ((org-pos-in-match-range pos 9)
  11688. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11689. ((org-pos-in-match-range pos 8)
  11690. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11691. (when ng
  11692. (setq s (concat
  11693. (substring s 0 (match-beginning ng))
  11694. new
  11695. (substring s (match-end ng))))))
  11696. s))
  11697. (defun org-recenter-calendar (date)
  11698. "If the calendar is visible, recenter it to DATE."
  11699. (let* ((win (selected-window))
  11700. (cwin (get-buffer-window "*Calendar*" t))
  11701. (calendar-move-hook nil))
  11702. (when cwin
  11703. (select-window cwin)
  11704. (calendar-goto-date (if (listp date) date
  11705. (calendar-gregorian-from-absolute date)))
  11706. (select-window win))))
  11707. (defun org-goto-calendar (&optional arg)
  11708. "Go to the Emacs calendar at the current date.
  11709. If there is a time stamp in the current line, go to that date.
  11710. A prefix ARG can be used to force the current date."
  11711. (interactive "P")
  11712. (let ((tsr org-ts-regexp) diff
  11713. (calendar-move-hook nil)
  11714. (calendar-view-holidays-initially-flag nil)
  11715. (view-calendar-holidays-initially nil)
  11716. (calendar-view-diary-initially-flag nil)
  11717. (view-diary-entries-initially nil))
  11718. (if (or (org-at-timestamp-p)
  11719. (save-excursion
  11720. (beginning-of-line 1)
  11721. (looking-at (concat ".*" tsr))))
  11722. (let ((d1 (time-to-days (current-time)))
  11723. (d2 (time-to-days
  11724. (org-time-string-to-time (match-string 1)))))
  11725. (setq diff (- d2 d1))))
  11726. (calendar)
  11727. (calendar-goto-today)
  11728. (if (and diff (not arg)) (calendar-forward-day diff))))
  11729. (defun org-get-date-from-calendar ()
  11730. "Return a list (month day year) of date at point in calendar."
  11731. (with-current-buffer "*Calendar*"
  11732. (save-match-data
  11733. (calendar-cursor-to-date))))
  11734. (defun org-date-from-calendar ()
  11735. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11736. If there is already a time stamp at the cursor position, update it."
  11737. (interactive)
  11738. (if (org-at-timestamp-p t)
  11739. (org-timestamp-change 0 'calendar)
  11740. (let ((cal-date (org-get-date-from-calendar)))
  11741. (org-insert-time-stamp
  11742. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11743. (defun org-minutes-to-hh:mm-string (m)
  11744. "Compute H:MM from a number of minutes."
  11745. (let ((h (/ m 60)))
  11746. (setq m (- m (* 60 h)))
  11747. (format org-time-clocksum-format h m)))
  11748. (defun org-hh:mm-string-to-minutes (s)
  11749. "Convert a string H:MM to a number of minutes."
  11750. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11751. (+ (* (string-to-number (match-string 1 s)) 60)
  11752. (string-to-number (match-string 2 s)))
  11753. 0))
  11754. ;;;; Files
  11755. (defun org-save-all-org-buffers ()
  11756. "Save all Org-mode buffers without user confirmation."
  11757. (interactive)
  11758. (message "Saving all Org-mode buffers...")
  11759. (save-some-buffers t 'org-mode-p)
  11760. (when (featurep 'org-id) (org-id-locations-save))
  11761. (message "Saving all Org-mode buffers... done"))
  11762. (defun org-revert-all-org-buffers ()
  11763. "Revert all Org-mode buffers.
  11764. Prompt for confirmation when there are unsaved changes.
  11765. Be sure you know what you are doing before letting this function
  11766. overwrite your changes.
  11767. This function is useful in a setup where one tracks org files
  11768. with a version control system, to revert on one machine after pulling
  11769. changes from another. I believe the procedure must be like this:
  11770. 1. M-x org-save-all-org-buffers
  11771. 2. Pull changes from the other machine, resolve conflicts
  11772. 3. M-x org-revert-all-org-buffers"
  11773. (interactive)
  11774. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11775. (error "Abort"))
  11776. (save-excursion
  11777. (save-window-excursion
  11778. (mapc
  11779. (lambda (b)
  11780. (when (and (with-current-buffer b (org-mode-p))
  11781. (with-current-buffer b buffer-file-name))
  11782. (switch-to-buffer b)
  11783. (revert-buffer t 'no-confirm)))
  11784. (buffer-list))
  11785. (when (and (featurep 'org-id) org-id-track-globally)
  11786. (org-id-locations-load)))))
  11787. ;;;; Agenda files
  11788. ;;;###autoload
  11789. (defun org-iswitchb (&optional arg)
  11790. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11791. With a prefix argument, restrict available to files.
  11792. With two prefix arguments, restrict available buffers to agenda files.
  11793. Due to some yet unresolved reason, the global function
  11794. `iswitchb-mode' needs to be active for this function to work."
  11795. (interactive "P")
  11796. (require 'iswitchb)
  11797. (let ((enabled iswitchb-mode) blist)
  11798. (or enabled (iswitchb-mode 1))
  11799. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11800. ((equal arg '(16)) (org-buffer-list 'agenda))
  11801. (t (org-buffer-list))))
  11802. (unwind-protect
  11803. (let ((iswitchb-make-buflist-hook
  11804. (lambda ()
  11805. (setq iswitchb-temp-buflist
  11806. (mapcar 'buffer-name blist)))))
  11807. (switch-to-buffer
  11808. (iswitchb-read-buffer
  11809. "Switch-to: " nil t))
  11810. (or enabled (iswitchb-mode -1))))))
  11811. ;;;###autoload
  11812. (defun org-ido-switchb (&optional arg)
  11813. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11814. With a prefix argument, restrict available to files.
  11815. With two prefix arguments, restrict available buffers to agenda files."
  11816. (interactive "P")
  11817. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11818. ((equal arg '(16)) (org-buffer-list 'agenda))
  11819. (t (org-buffer-list)))))
  11820. (switch-to-buffer
  11821. (org-ido-completing-read "Org buffer: "
  11822. (mapcar 'list (mapcar 'buffer-name blist))
  11823. nil t))))
  11824. (defun org-buffer-list (&optional predicate exclude-tmp)
  11825. "Return a list of Org buffers.
  11826. PREDICATE can be `export', `files' or `agenda'.
  11827. export restrict the list to Export buffers.
  11828. files restrict the list to buffers visiting Org files.
  11829. agenda restrict the list to buffers visiting agenda files.
  11830. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11831. (let* ((bfn nil)
  11832. (agenda-files (and (eq predicate 'agenda)
  11833. (mapcar 'file-truename (org-agenda-files t))))
  11834. (filter
  11835. (cond
  11836. ((eq predicate 'files)
  11837. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11838. ((eq predicate 'export)
  11839. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11840. ((eq predicate 'agenda)
  11841. (lambda (b)
  11842. (with-current-buffer b
  11843. (and (eq major-mode 'org-mode)
  11844. (setq bfn (buffer-file-name b))
  11845. (member (file-truename bfn) agenda-files)))))
  11846. (t (lambda (b) (with-current-buffer b
  11847. (or (eq major-mode 'org-mode)
  11848. (string-match "\*Org .*Export"
  11849. (buffer-name b)))))))))
  11850. (delq nil
  11851. (mapcar
  11852. (lambda(b)
  11853. (if (and (funcall filter b)
  11854. (or (not exclude-tmp)
  11855. (not (string-match "tmp" (buffer-name b)))))
  11856. b
  11857. nil))
  11858. (buffer-list)))))
  11859. (defun org-agenda-files (&optional unrestricted archives)
  11860. "Get the list of agenda files.
  11861. Optional UNRESTRICTED means return the full list even if a restriction
  11862. is currently in place.
  11863. When ARCHIVES is t, include all archive files hat are really being
  11864. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11865. `org-agenda-archives-mode' is t."
  11866. (let ((files
  11867. (cond
  11868. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11869. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11870. ((listp org-agenda-files) org-agenda-files)
  11871. (t (error "Invalid value of `org-agenda-files'")))))
  11872. (setq files (apply 'append
  11873. (mapcar (lambda (f)
  11874. (if (file-directory-p f)
  11875. (directory-files
  11876. f t org-agenda-file-regexp)
  11877. (list f)))
  11878. files)))
  11879. (when org-agenda-skip-unavailable-files
  11880. (setq files (delq nil
  11881. (mapcar (function
  11882. (lambda (file)
  11883. (and (file-readable-p file) file)))
  11884. files))))
  11885. (when (or (eq archives t)
  11886. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11887. (setq files (org-add-archive-files files)))
  11888. files))
  11889. (defun org-edit-agenda-file-list ()
  11890. "Edit the list of agenda files.
  11891. Depending on setup, this either uses customize to edit the variable
  11892. `org-agenda-files', or it visits the file that is holding the list. In the
  11893. latter case, the buffer is set up in a way that saving it automatically kills
  11894. the buffer and restores the previous window configuration."
  11895. (interactive)
  11896. (if (stringp org-agenda-files)
  11897. (let ((cw (current-window-configuration)))
  11898. (find-file org-agenda-files)
  11899. (org-set-local 'org-window-configuration cw)
  11900. (org-add-hook 'after-save-hook
  11901. (lambda ()
  11902. (set-window-configuration
  11903. (prog1 org-window-configuration
  11904. (kill-buffer (current-buffer))))
  11905. (org-install-agenda-files-menu)
  11906. (message "New agenda file list installed"))
  11907. nil 'local)
  11908. (message "%s" (substitute-command-keys
  11909. "Edit list and finish with \\[save-buffer]")))
  11910. (customize-variable 'org-agenda-files)))
  11911. (defun org-store-new-agenda-file-list (list)
  11912. "Set new value for the agenda file list and save it correctly."
  11913. (if (stringp org-agenda-files)
  11914. (let ((f org-agenda-files) b)
  11915. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11916. (with-temp-file f
  11917. (insert (mapconcat 'identity list "\n") "\n")))
  11918. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11919. (setq org-agenda-files list)
  11920. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11921. (defun org-read-agenda-file-list ()
  11922. "Read the list of agenda files from a file."
  11923. (when (file-directory-p org-agenda-files)
  11924. (error "`org-agenda-files' cannot be a single directory"))
  11925. (when (stringp org-agenda-files)
  11926. (with-temp-buffer
  11927. (insert-file-contents org-agenda-files)
  11928. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11929. ;;;###autoload
  11930. (defun org-cycle-agenda-files ()
  11931. "Cycle through the files in `org-agenda-files'.
  11932. If the current buffer visits an agenda file, find the next one in the list.
  11933. If the current buffer does not, find the first agenda file."
  11934. (interactive)
  11935. (let* ((fs (org-agenda-files t))
  11936. (files (append fs (list (car fs))))
  11937. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11938. file)
  11939. (unless files (error "No agenda files"))
  11940. (catch 'exit
  11941. (while (setq file (pop files))
  11942. (if (equal (file-truename file) tcf)
  11943. (when (car files)
  11944. (find-file (car files))
  11945. (throw 'exit t))))
  11946. (find-file (car fs)))
  11947. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11948. (defun org-agenda-file-to-front (&optional to-end)
  11949. "Move/add the current file to the top of the agenda file list.
  11950. If the file is not present in the list, it is added to the front. If it is
  11951. present, it is moved there. With optional argument TO-END, add/move to the
  11952. end of the list."
  11953. (interactive "P")
  11954. (let ((org-agenda-skip-unavailable-files nil)
  11955. (file-alist (mapcar (lambda (x)
  11956. (cons (file-truename x) x))
  11957. (org-agenda-files t)))
  11958. (ctf (file-truename buffer-file-name))
  11959. x had)
  11960. (setq x (assoc ctf file-alist) had x)
  11961. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11962. (if to-end
  11963. (setq file-alist (append (delq x file-alist) (list x)))
  11964. (setq file-alist (cons x (delq x file-alist))))
  11965. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11966. (org-install-agenda-files-menu)
  11967. (message "File %s to %s of agenda file list"
  11968. (if had "moved" "added") (if to-end "end" "front"))))
  11969. (defun org-remove-file (&optional file)
  11970. "Remove current file from the list of files in variable `org-agenda-files'.
  11971. These are the files which are being checked for agenda entries.
  11972. Optional argument FILE means, use this file instead of the current."
  11973. (interactive)
  11974. (let* ((org-agenda-skip-unavailable-files nil)
  11975. (file (or file buffer-file-name))
  11976. (true-file (file-truename file))
  11977. (afile (abbreviate-file-name file))
  11978. (files (delq nil (mapcar
  11979. (lambda (x)
  11980. (if (equal true-file
  11981. (file-truename x))
  11982. nil x))
  11983. (org-agenda-files t)))))
  11984. (if (not (= (length files) (length (org-agenda-files t))))
  11985. (progn
  11986. (org-store-new-agenda-file-list files)
  11987. (org-install-agenda-files-menu)
  11988. (message "Removed file: %s" afile))
  11989. (message "File was not in list: %s (not removed)" afile))))
  11990. (defun org-file-menu-entry (file)
  11991. (vector file (list 'find-file file) t))
  11992. (defun org-check-agenda-file (file)
  11993. "Make sure FILE exists. If not, ask user what to do."
  11994. (when (not (file-exists-p file))
  11995. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11996. (abbreviate-file-name file))
  11997. (let ((r (downcase (read-char-exclusive))))
  11998. (cond
  11999. ((equal r ?r)
  12000. (org-remove-file file)
  12001. (throw 'nextfile t))
  12002. (t (error "Abort"))))))
  12003. (defun org-get-agenda-file-buffer (file)
  12004. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12005. it to the list of buffers which might be released later."
  12006. (let ((buf (org-find-base-buffer-visiting file)))
  12007. (if buf
  12008. buf ; just return it
  12009. ;; Make a new buffer and remember it
  12010. (setq buf (find-file-noselect file))
  12011. (if buf (push buf org-agenda-new-buffers))
  12012. buf)))
  12013. (defun org-release-buffers (blist)
  12014. "Release all buffers in list, asking the user for confirmation when needed.
  12015. When a buffer is unmodified, it is just killed. When modified, it is saved
  12016. \(if the user agrees) and then killed."
  12017. (let (buf file)
  12018. (while (setq buf (pop blist))
  12019. (setq file (buffer-file-name buf))
  12020. (when (and (buffer-modified-p buf)
  12021. file
  12022. (y-or-n-p (format "Save file %s? " file)))
  12023. (with-current-buffer buf (save-buffer)))
  12024. (kill-buffer buf))))
  12025. (defun org-prepare-agenda-buffers (files)
  12026. "Create buffers for all agenda files, protect archived trees and comments."
  12027. (interactive)
  12028. (let ((pa '(:org-archived t))
  12029. (pc '(:org-comment t))
  12030. (pall '(:org-archived t :org-comment t))
  12031. (inhibit-read-only t)
  12032. (rea (concat ":" org-archive-tag ":"))
  12033. bmp file re)
  12034. (save-excursion
  12035. (save-restriction
  12036. (while (setq file (pop files))
  12037. (if (bufferp file)
  12038. (set-buffer file)
  12039. (org-check-agenda-file file)
  12040. (set-buffer (org-get-agenda-file-buffer file)))
  12041. (widen)
  12042. (setq bmp (buffer-modified-p))
  12043. (org-refresh-category-properties)
  12044. (setq org-todo-keywords-for-agenda
  12045. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12046. (setq org-done-keywords-for-agenda
  12047. (append org-done-keywords-for-agenda org-done-keywords))
  12048. (setq org-todo-keyword-alist-for-agenda
  12049. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12050. (setq org-tag-alist-for-agenda
  12051. (append org-tag-alist-for-agenda org-tag-alist))
  12052. (save-excursion
  12053. (remove-text-properties (point-min) (point-max) pall)
  12054. (when org-agenda-skip-archived-trees
  12055. (goto-char (point-min))
  12056. (while (re-search-forward rea nil t)
  12057. (if (org-on-heading-p t)
  12058. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12059. (goto-char (point-min))
  12060. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12061. (while (re-search-forward re nil t)
  12062. (add-text-properties
  12063. (match-beginning 0) (org-end-of-subtree t) pc)))
  12064. (set-buffer-modified-p bmp))))
  12065. (setq org-todo-keyword-alist-for-agenda
  12066. (org-uniquify org-todo-keyword-alist-for-agenda)
  12067. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12068. ;;;; Embedded LaTeX
  12069. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12070. "Keymap for the minor `org-cdlatex-mode'.")
  12071. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12072. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12073. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12074. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12075. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12076. (defvar org-cdlatex-texmathp-advice-is-done nil
  12077. "Flag remembering if we have applied the advice to texmathp already.")
  12078. (define-minor-mode org-cdlatex-mode
  12079. "Toggle the minor `org-cdlatex-mode'.
  12080. This mode supports entering LaTeX environment and math in LaTeX fragments
  12081. in Org-mode.
  12082. \\{org-cdlatex-mode-map}"
  12083. nil " OCDL" nil
  12084. (when org-cdlatex-mode (require 'cdlatex))
  12085. (unless org-cdlatex-texmathp-advice-is-done
  12086. (setq org-cdlatex-texmathp-advice-is-done t)
  12087. (defadvice texmathp (around org-math-always-on activate)
  12088. "Always return t in org-mode buffers.
  12089. This is because we want to insert math symbols without dollars even outside
  12090. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12091. an embedded LaTeX fragment, let texmathp do its job.
  12092. \\[org-cdlatex-mode-map]"
  12093. (interactive)
  12094. (let (p)
  12095. (cond
  12096. ((not (org-mode-p)) ad-do-it)
  12097. ((eq this-command 'cdlatex-math-symbol)
  12098. (setq ad-return-value t
  12099. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12100. (t
  12101. (let ((p (org-inside-LaTeX-fragment-p)))
  12102. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12103. (setq ad-return-value t
  12104. texmathp-why '("Org-mode embedded math" . 0))
  12105. (if p ad-do-it)))))))))
  12106. (defun turn-on-org-cdlatex ()
  12107. "Unconditionally turn on `org-cdlatex-mode'."
  12108. (org-cdlatex-mode 1))
  12109. (defun org-inside-LaTeX-fragment-p ()
  12110. "Test if point is inside a LaTeX fragment.
  12111. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12112. sequence appearing also before point.
  12113. Even though the matchers for math are configurable, this function assumes
  12114. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12115. delimiters are skipped when they have been removed by customization.
  12116. The return value is nil, or a cons cell with the delimiter and
  12117. and the position of this delimiter.
  12118. This function does a reasonably good job, but can locally be fooled by
  12119. for example currency specifications. For example it will assume being in
  12120. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12121. fragments that are properly closed, but during editing, we have to live
  12122. with the uncertainty caused by missing closing delimiters. This function
  12123. looks only before point, not after."
  12124. (catch 'exit
  12125. (let ((pos (point))
  12126. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12127. (lim (progn
  12128. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12129. (point)))
  12130. dd-on str (start 0) m re)
  12131. (goto-char pos)
  12132. (when dodollar
  12133. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12134. re (nth 1 (assoc "$" org-latex-regexps)))
  12135. (while (string-match re str start)
  12136. (cond
  12137. ((= (match-end 0) (length str))
  12138. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12139. ((= (match-end 0) (- (length str) 5))
  12140. (throw 'exit nil))
  12141. (t (setq start (match-end 0))))))
  12142. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12143. (goto-char pos)
  12144. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12145. (and (match-beginning 2) (throw 'exit nil))
  12146. ;; count $$
  12147. (while (re-search-backward "\\$\\$" lim t)
  12148. (setq dd-on (not dd-on)))
  12149. (goto-char pos)
  12150. (if dd-on (cons "$$" m))))))
  12151. (defun org-try-cdlatex-tab ()
  12152. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12153. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12154. - inside a LaTeX fragment, or
  12155. - after the first word in a line, where an abbreviation expansion could
  12156. insert a LaTeX environment."
  12157. (when org-cdlatex-mode
  12158. (cond
  12159. ((save-excursion
  12160. (skip-chars-backward "a-zA-Z0-9*")
  12161. (skip-chars-backward " \t")
  12162. (bolp))
  12163. (cdlatex-tab) t)
  12164. ((org-inside-LaTeX-fragment-p)
  12165. (cdlatex-tab) t)
  12166. (t nil))))
  12167. (defun org-cdlatex-underscore-caret (&optional arg)
  12168. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12169. Revert to the normal definition outside of these fragments."
  12170. (interactive "P")
  12171. (if (org-inside-LaTeX-fragment-p)
  12172. (call-interactively 'cdlatex-sub-superscript)
  12173. (let (org-cdlatex-mode)
  12174. (call-interactively (key-binding (vector last-input-event))))))
  12175. (defun org-cdlatex-math-modify (&optional arg)
  12176. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12177. Revert to the normal definition outside of these fragments."
  12178. (interactive "P")
  12179. (if (org-inside-LaTeX-fragment-p)
  12180. (call-interactively 'cdlatex-math-modify)
  12181. (let (org-cdlatex-mode)
  12182. (call-interactively (key-binding (vector last-input-event))))))
  12183. (defvar org-latex-fragment-image-overlays nil
  12184. "List of overlays carrying the images of latex fragments.")
  12185. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12186. (defun org-remove-latex-fragment-image-overlays ()
  12187. "Remove all overlays with LaTeX fragment images in current buffer."
  12188. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12189. (setq org-latex-fragment-image-overlays nil))
  12190. (defun org-preview-latex-fragment (&optional subtree)
  12191. "Preview the LaTeX fragment at point, or all locally or globally.
  12192. If the cursor is in a LaTeX fragment, create the image and overlay
  12193. it over the source code. If there is no fragment at point, display
  12194. all fragments in the current text, from one headline to the next. With
  12195. prefix SUBTREE, display all fragments in the current subtree. With a
  12196. double prefix `C-u C-u', or when the cursor is before the first headline,
  12197. display all fragments in the buffer.
  12198. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12199. (interactive "P")
  12200. (org-remove-latex-fragment-image-overlays)
  12201. (save-excursion
  12202. (save-restriction
  12203. (let (beg end at msg)
  12204. (cond
  12205. ((or (equal subtree '(16))
  12206. (not (save-excursion
  12207. (re-search-backward (concat "^" outline-regexp) nil t))))
  12208. (setq beg (point-min) end (point-max)
  12209. msg "Creating images for buffer...%s"))
  12210. ((equal subtree '(4))
  12211. (org-back-to-heading)
  12212. (setq beg (point) end (org-end-of-subtree t)
  12213. msg "Creating images for subtree...%s"))
  12214. (t
  12215. (if (setq at (org-inside-LaTeX-fragment-p))
  12216. (goto-char (max (point-min) (- (cdr at) 2)))
  12217. (org-back-to-heading))
  12218. (setq beg (point) end (progn (outline-next-heading) (point))
  12219. msg (if at "Creating image...%s"
  12220. "Creating images for entry...%s"))))
  12221. (message msg "")
  12222. (narrow-to-region beg end)
  12223. (goto-char beg)
  12224. (org-format-latex
  12225. (concat "ltxpng/" (file-name-sans-extension
  12226. (file-name-nondirectory
  12227. buffer-file-name)))
  12228. default-directory 'overlays msg at 'forbuffer)
  12229. (message msg "done. Use `C-c C-c' to remove images.")))))
  12230. (defvar org-latex-regexps
  12231. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12232. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12233. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12234. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12235. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12236. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12237. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12238. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12239. "Regular expressions for matching embedded LaTeX.")
  12240. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12241. "Replace LaTeX fragments with links to an image, and produce images."
  12242. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12243. (let* ((prefixnodir (file-name-nondirectory prefix))
  12244. (absprefix (expand-file-name prefix dir))
  12245. (todir (file-name-directory absprefix))
  12246. (opt org-format-latex-options)
  12247. (matchers (plist-get opt :matchers))
  12248. (re-list org-latex-regexps)
  12249. (cnt 0) txt link beg end re e checkdir
  12250. m n block linkfile movefile ov)
  12251. ;; Check if there are old images files with this prefix, and remove them
  12252. (when (file-directory-p todir)
  12253. (mapc 'delete-file
  12254. (directory-files
  12255. todir 'full
  12256. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12257. ;; Check the different regular expressions
  12258. (while (setq e (pop re-list))
  12259. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12260. block (if (nth 3 e) "\n\n" ""))
  12261. (when (member m matchers)
  12262. (goto-char (point-min))
  12263. (while (re-search-forward re nil t)
  12264. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12265. (not (get-text-property (match-beginning n)
  12266. 'org-protected)))
  12267. (setq txt (match-string n)
  12268. beg (match-beginning n) end (match-end n)
  12269. cnt (1+ cnt)
  12270. linkfile (format "%s_%04d.png" prefix cnt)
  12271. movefile (format "%s_%04d.png" absprefix cnt)
  12272. link (concat block "[[file:" linkfile "]]" block))
  12273. (if msg (message msg cnt))
  12274. (goto-char beg)
  12275. (unless checkdir ; make sure the directory exists
  12276. (setq checkdir t)
  12277. (or (file-directory-p todir) (make-directory todir)))
  12278. (org-create-formula-image
  12279. txt movefile opt forbuffer)
  12280. (if overlays
  12281. (progn
  12282. (setq ov (org-make-overlay beg end))
  12283. (if (featurep 'xemacs)
  12284. (progn
  12285. (org-overlay-put ov 'invisible t)
  12286. (org-overlay-put
  12287. ov 'end-glyph
  12288. (make-glyph (vector 'png :file movefile))))
  12289. (org-overlay-put
  12290. ov 'display
  12291. (list 'image :type 'png :file movefile :ascent 'center)))
  12292. (push ov org-latex-fragment-image-overlays)
  12293. (goto-char end))
  12294. (delete-region beg end)
  12295. (insert link))))))))
  12296. ;; This function borrows from Ganesh Swami's latex2png.el
  12297. (defun org-create-formula-image (string tofile options buffer)
  12298. (let* ((tmpdir (if (featurep 'xemacs)
  12299. (temp-directory)
  12300. temporary-file-directory))
  12301. (texfilebase (make-temp-name
  12302. (expand-file-name "orgtex" tmpdir)))
  12303. (texfile (concat texfilebase ".tex"))
  12304. (dvifile (concat texfilebase ".dvi"))
  12305. (pngfile (concat texfilebase ".png"))
  12306. (fnh (if (featurep 'xemacs)
  12307. (font-height (get-face-font 'default))
  12308. (face-attribute 'default :height nil)))
  12309. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12310. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12311. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12312. "Black"))
  12313. (bg (or (plist-get options (if buffer :background :html-background))
  12314. "Transparent")))
  12315. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12316. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12317. (with-temp-file texfile
  12318. (insert org-format-latex-header
  12319. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12320. (let ((dir default-directory))
  12321. (condition-case nil
  12322. (progn
  12323. (cd tmpdir)
  12324. (call-process "latex" nil nil nil texfile))
  12325. (error nil))
  12326. (cd dir))
  12327. (if (not (file-exists-p dvifile))
  12328. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12329. (condition-case nil
  12330. (call-process "dvipng" nil nil nil
  12331. "-E" "-fg" fg "-bg" bg
  12332. "-D" dpi
  12333. ;;"-x" scale "-y" scale
  12334. "-T" "tight"
  12335. "-o" pngfile
  12336. dvifile)
  12337. (error nil))
  12338. (if (not (file-exists-p pngfile))
  12339. (progn (message "Failed to create png file from %s" texfile) nil)
  12340. ;; Use the requested file name and clean up
  12341. (copy-file pngfile tofile 'replace)
  12342. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12343. (delete-file (concat texfilebase e)))
  12344. pngfile))))
  12345. (defun org-dvipng-color (attr)
  12346. "Return an rgb color specification for dvipng."
  12347. (apply 'format "rgb %s %s %s"
  12348. (mapcar 'org-normalize-color
  12349. (color-values (face-attribute 'default attr nil)))))
  12350. (defun org-normalize-color (value)
  12351. "Return string to be used as color value for an RGB component."
  12352. (format "%g" (/ value 65535.0)))
  12353. ;;;; Key bindings
  12354. ;; Make `C-c C-x' a prefix key
  12355. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12356. ;; TAB key with modifiers
  12357. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12358. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12359. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12360. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12361. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12362. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12363. ;; The following line is necessary under Suse GNU/Linux
  12364. (unless (featurep 'xemacs)
  12365. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12366. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12367. (define-key org-mode-map [backtab] 'org-shifttab)
  12368. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12369. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12370. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12371. ;; Cursor keys with modifiers
  12372. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12373. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12374. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12375. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12376. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12377. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12378. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12379. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12380. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12381. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12382. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12383. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12384. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12385. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12386. ;;; Extra keys for tty access.
  12387. ;; We only set them when really needed because otherwise the
  12388. ;; menus don't show the simple keys
  12389. (when (or org-use-extra-keys
  12390. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12391. (not window-system))
  12392. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12393. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12394. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12395. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12396. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12397. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12398. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12399. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12400. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12401. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12402. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12403. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12404. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12405. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12406. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12407. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12408. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12409. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12410. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12411. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12412. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12413. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12414. ;; All the other keys
  12415. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12416. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12417. (if (boundp 'narrow-map)
  12418. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12419. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12420. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12421. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12422. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12423. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12424. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12425. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12426. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12427. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12428. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12429. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12430. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12431. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12432. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12433. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12434. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12435. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12436. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12437. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12438. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12439. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12440. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12441. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12442. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12443. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12444. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12445. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12446. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12447. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12448. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12449. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12450. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12451. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12452. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12453. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12454. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12455. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12456. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12457. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12458. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12459. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12460. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12461. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12462. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12463. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12464. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12465. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12466. (org-defkey org-mode-map "\C-m" 'org-return)
  12467. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12468. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12469. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12470. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12471. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12472. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12473. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12474. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12475. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12476. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12477. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12478. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12479. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12480. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12481. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12482. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12483. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12484. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12485. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12486. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12487. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12488. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12489. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12490. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12491. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12492. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12493. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12494. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12495. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12496. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12497. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12498. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12499. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12500. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12501. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12502. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12503. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12504. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12505. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12506. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12507. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12508. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12509. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12510. (when (featurep 'xemacs)
  12511. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12512. (defvar org-self-insert-command-undo-counter 0)
  12513. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12514. (defun org-self-insert-command (N)
  12515. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12516. If the cursor is in a table looking at whitespace, the whitespace is
  12517. overwritten, and the table is not marked as requiring realignment."
  12518. (interactive "p")
  12519. (if (and
  12520. (org-table-p)
  12521. (progn
  12522. ;; check if we blank the field, and if that triggers align
  12523. (and (featurep 'org-table) org-table-auto-blank-field
  12524. (member last-command
  12525. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12526. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12527. ;; got extra space, this field does not determine column width
  12528. (let (org-table-may-need-update) (org-table-blank-field))
  12529. ;; no extra space, this field may determine column width
  12530. (org-table-blank-field)))
  12531. t)
  12532. (eq N 1)
  12533. (looking-at "[^|\n]* |"))
  12534. (let (org-table-may-need-update)
  12535. (goto-char (1- (match-end 0)))
  12536. (delete-backward-char 1)
  12537. (goto-char (match-beginning 0))
  12538. (self-insert-command N))
  12539. (setq org-table-may-need-update t)
  12540. (self-insert-command N)
  12541. (org-fix-tags-on-the-fly)
  12542. (if org-self-insert-cluster-for-undo
  12543. (if (not (eq last-command 'org-self-insert-command))
  12544. (setq org-self-insert-command-undo-counter 1)
  12545. (if (>= org-self-insert-command-undo-counter 20)
  12546. (setq org-self-insert-command-undo-counter 1)
  12547. (and (> org-self-insert-command-undo-counter 0)
  12548. buffer-undo-list
  12549. (not (cadr buffer-undo-list)) ; remove nil entry
  12550. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12551. (setq org-self-insert-command-undo-counter
  12552. (1+ org-self-insert-command-undo-counter)))))))
  12553. (defun org-fix-tags-on-the-fly ()
  12554. (when (and (equal (char-after (point-at-bol)) ?*)
  12555. (org-on-heading-p))
  12556. (org-align-tags-here org-tags-column)))
  12557. (defun org-delete-backward-char (N)
  12558. "Like `delete-backward-char', insert whitespace at field end in tables.
  12559. When deleting backwards, in tables this function will insert whitespace in
  12560. front of the next \"|\" separator, to keep the table aligned. The table will
  12561. still be marked for re-alignment if the field did fill the entire column,
  12562. because, in this case the deletion might narrow the column."
  12563. (interactive "p")
  12564. (if (and (org-table-p)
  12565. (eq N 1)
  12566. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12567. (looking-at ".*?|"))
  12568. (let ((pos (point))
  12569. (noalign (looking-at "[^|\n\r]* |"))
  12570. (c org-table-may-need-update))
  12571. (backward-delete-char N)
  12572. (skip-chars-forward "^|")
  12573. (insert " ")
  12574. (goto-char (1- pos))
  12575. ;; noalign: if there were two spaces at the end, this field
  12576. ;; does not determine the width of the column.
  12577. (if noalign (setq org-table-may-need-update c)))
  12578. (backward-delete-char N)
  12579. (org-fix-tags-on-the-fly)))
  12580. (defun org-delete-char (N)
  12581. "Like `delete-char', but insert whitespace at field end in tables.
  12582. When deleting characters, in tables this function will insert whitespace in
  12583. front of the next \"|\" separator, to keep the table aligned. The table will
  12584. still be marked for re-alignment if the field did fill the entire column,
  12585. because, in this case the deletion might narrow the column."
  12586. (interactive "p")
  12587. (if (and (org-table-p)
  12588. (not (bolp))
  12589. (not (= (char-after) ?|))
  12590. (eq N 1))
  12591. (if (looking-at ".*?|")
  12592. (let ((pos (point))
  12593. (noalign (looking-at "[^|\n\r]* |"))
  12594. (c org-table-may-need-update))
  12595. (replace-match (concat
  12596. (substring (match-string 0) 1 -1)
  12597. " |"))
  12598. (goto-char pos)
  12599. ;; noalign: if there were two spaces at the end, this field
  12600. ;; does not determine the width of the column.
  12601. (if noalign (setq org-table-may-need-update c)))
  12602. (delete-char N))
  12603. (delete-char N)
  12604. (org-fix-tags-on-the-fly)))
  12605. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12606. (put 'org-self-insert-command 'delete-selection t)
  12607. (put 'orgtbl-self-insert-command 'delete-selection t)
  12608. (put 'org-delete-char 'delete-selection 'supersede)
  12609. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12610. (put 'org-yank 'delete-selection 'yank)
  12611. ;; Make `flyspell-mode' delay after some commands
  12612. (put 'org-self-insert-command 'flyspell-delayed t)
  12613. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12614. (put 'org-delete-char 'flyspell-delayed t)
  12615. (put 'org-delete-backward-char 'flyspell-delayed t)
  12616. ;; Make pabbrev-mode expand after org-mode commands
  12617. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12618. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12619. ;; How to do this: Measure non-white length of current string
  12620. ;; If equal to column width, we should realign.
  12621. (defun org-remap (map &rest commands)
  12622. "In MAP, remap the functions given in COMMANDS.
  12623. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12624. (let (new old)
  12625. (while commands
  12626. (setq old (pop commands) new (pop commands))
  12627. (if (fboundp 'command-remapping)
  12628. (org-defkey map (vector 'remap old) new)
  12629. (substitute-key-definition old new map global-map)))))
  12630. (when (eq org-enable-table-editor 'optimized)
  12631. ;; If the user wants maximum table support, we need to hijack
  12632. ;; some standard editing functions
  12633. (org-remap org-mode-map
  12634. 'self-insert-command 'org-self-insert-command
  12635. 'delete-char 'org-delete-char
  12636. 'delete-backward-char 'org-delete-backward-char)
  12637. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12638. (defvar org-ctrl-c-ctrl-c-hook nil
  12639. "Hook for functions attaching themselves to `C-c C-c'.
  12640. This can be used to add additional functionality to the C-c C-c key which
  12641. executes context-dependent commands.
  12642. Each function will be called with no arguments. The function must check
  12643. if the context is appropriate for it to act. If yes, it should do its
  12644. thing and then return a non-nil value. If the context is wrong,
  12645. just do nothing.")
  12646. (defvar org-metaleft-hook nil
  12647. "Hook for functions attaching themselves to `M-left'.
  12648. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12649. (defvar org-metaright-hook nil
  12650. "Hook for functions attaching themselves to `M-right'.
  12651. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12652. (defvar org-metaup-hook nil
  12653. "Hook for functions attaching themselves to `M-up'.
  12654. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12655. (defvar org-metadown-hook nil
  12656. "Hook for functions attaching themselves to `M-down'.
  12657. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12658. (defvar org-shiftmetaleft-hook nil
  12659. "Hook for functions attaching themselves to `M-S-left'.
  12660. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12661. (defvar org-shiftmetaright-hook nil
  12662. "Hook for functions attaching themselves to `M-S-right'.
  12663. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12664. (defvar org-shiftmetaup-hook nil
  12665. "Hook for functions attaching themselves to `M-S-up'.
  12666. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12667. (defvar org-shiftmetadown-hook nil
  12668. "Hook for functions attaching themselves to `M-S-down'.
  12669. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12670. (defvar org-metareturn-hook nil
  12671. "Hook for functions attaching themselves to `M-RET'.
  12672. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12673. (defun org-modifier-cursor-error ()
  12674. "Throw an error, a modified cursor command was applied in wrong context."
  12675. (error "This command is active in special context like tables, headlines or items"))
  12676. (defun org-shiftselect-error ()
  12677. "Throw an error because Shift-Cursor command was applied in wrong context."
  12678. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12679. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12680. (error "This command works only in special context like headlines or timestamps.")))
  12681. (defun org-call-for-shift-select (cmd)
  12682. (let ((this-command-keys-shift-translated t))
  12683. (call-interactively cmd)))
  12684. (defun org-shifttab (&optional arg)
  12685. "Global visibility cycling or move to previous table field.
  12686. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12687. on context.
  12688. See the individual commands for more information."
  12689. (interactive "P")
  12690. (cond
  12691. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12692. ((integerp arg)
  12693. (message "Content view to level: %d" arg)
  12694. (org-content (prefix-numeric-value arg))
  12695. (setq org-cycle-global-status 'overview))
  12696. (t (call-interactively 'org-global-cycle))))
  12697. (defun org-shiftmetaleft ()
  12698. "Promote subtree or delete table column.
  12699. Calls `org-promote-subtree', `org-outdent-item',
  12700. or `org-table-delete-column', depending on context.
  12701. See the individual commands for more information."
  12702. (interactive)
  12703. (cond
  12704. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12705. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12706. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12707. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12708. (t (org-modifier-cursor-error))))
  12709. (defun org-shiftmetaright ()
  12710. "Demote subtree or insert table column.
  12711. Calls `org-demote-subtree', `org-indent-item',
  12712. or `org-table-insert-column', depending on context.
  12713. See the individual commands for more information."
  12714. (interactive)
  12715. (cond
  12716. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12717. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12718. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12719. ((org-at-item-p) (call-interactively 'org-indent-item))
  12720. (t (org-modifier-cursor-error))))
  12721. (defun org-shiftmetaup (&optional arg)
  12722. "Move subtree up or kill table row.
  12723. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12724. `org-move-item-up' depending on context. See the individual commands
  12725. for more information."
  12726. (interactive "P")
  12727. (cond
  12728. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12729. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12730. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12731. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12732. (t (org-modifier-cursor-error))))
  12733. (defun org-shiftmetadown (&optional arg)
  12734. "Move subtree down or insert table row.
  12735. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12736. `org-move-item-down', depending on context. See the individual
  12737. commands for more information."
  12738. (interactive "P")
  12739. (cond
  12740. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12741. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12742. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12743. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12744. (t (org-modifier-cursor-error))))
  12745. (defun org-metaleft (&optional arg)
  12746. "Promote heading or move table column to left.
  12747. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12748. With no specific context, calls the Emacs default `backward-word'.
  12749. See the individual commands for more information."
  12750. (interactive "P")
  12751. (cond
  12752. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12753. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12754. ((or (org-on-heading-p)
  12755. (and (org-region-active-p)
  12756. (save-excursion
  12757. (goto-char (region-beginning))
  12758. (org-on-heading-p))))
  12759. (call-interactively 'org-do-promote))
  12760. ((or (org-at-item-p)
  12761. (and (org-region-active-p)
  12762. (save-excursion
  12763. (goto-char (region-beginning))
  12764. (org-at-item-p))))
  12765. (call-interactively 'org-outdent-item))
  12766. (t (call-interactively 'backward-word))))
  12767. (defun org-metaright (&optional arg)
  12768. "Demote subtree or move table column to right.
  12769. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12770. With no specific context, calls the Emacs default `forward-word'.
  12771. See the individual commands for more information."
  12772. (interactive "P")
  12773. (cond
  12774. ((run-hook-with-args-until-success 'org-metaright-hook))
  12775. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12776. ((or (org-on-heading-p)
  12777. (and (org-region-active-p)
  12778. (save-excursion
  12779. (goto-char (region-beginning))
  12780. (org-on-heading-p))))
  12781. (call-interactively 'org-do-demote))
  12782. ((or (org-at-item-p)
  12783. (and (org-region-active-p)
  12784. (save-excursion
  12785. (goto-char (region-beginning))
  12786. (org-at-item-p))))
  12787. (call-interactively 'org-indent-item))
  12788. (t (call-interactively 'forward-word))))
  12789. (defun org-metaup (&optional arg)
  12790. "Move subtree up or move table row up.
  12791. Calls `org-move-subtree-up' or `org-table-move-row' or
  12792. `org-move-item-up', depending on context. See the individual commands
  12793. for more information."
  12794. (interactive "P")
  12795. (cond
  12796. ((run-hook-with-args-until-success 'org-metaup-hook))
  12797. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12798. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12799. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12800. (t (transpose-lines 1) (beginning-of-line -1))))
  12801. (defun org-metadown (&optional arg)
  12802. "Move subtree down or move table row down.
  12803. Calls `org-move-subtree-down' or `org-table-move-row' or
  12804. `org-move-item-down', depending on context. See the individual
  12805. commands for more information."
  12806. (interactive "P")
  12807. (cond
  12808. ((run-hook-with-args-until-success 'org-metadown-hook))
  12809. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12810. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12811. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12812. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12813. (defun org-shiftup (&optional arg)
  12814. "Increase item in timestamp or increase priority of current headline.
  12815. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12816. depending on context. See the individual commands for more information."
  12817. (interactive "P")
  12818. (cond
  12819. ((and org-support-shift-select (org-region-active-p))
  12820. (org-call-for-shift-select 'previous-line))
  12821. ((org-at-timestamp-p t)
  12822. (call-interactively (if org-edit-timestamp-down-means-later
  12823. 'org-timestamp-down 'org-timestamp-up)))
  12824. ((and (not (eq org-support-shift-select 'always))
  12825. (org-on-heading-p))
  12826. (call-interactively 'org-priority-up))
  12827. ((and (not org-support-shift-select) (org-at-item-p))
  12828. (call-interactively 'org-previous-item))
  12829. ((org-clocktable-try-shift 'up arg))
  12830. (org-support-shift-select
  12831. (org-call-for-shift-select 'previous-line))
  12832. (t (org-shiftselect-error))))
  12833. (defun org-shiftdown (&optional arg)
  12834. "Decrease item in timestamp or decrease priority of current headline.
  12835. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12836. depending on context. See the individual commands for more information."
  12837. (interactive "P")
  12838. (cond
  12839. ((and org-support-shift-select (org-region-active-p))
  12840. (org-call-for-shift-select 'next-line))
  12841. ((org-at-timestamp-p t)
  12842. (call-interactively (if org-edit-timestamp-down-means-later
  12843. 'org-timestamp-up 'org-timestamp-down)))
  12844. ((and (not (eq org-support-shift-select 'always))
  12845. (org-on-heading-p))
  12846. (call-interactively 'org-priority-down))
  12847. ((and (not org-support-shift-select) (org-at-item-p))
  12848. (call-interactively 'org-next-item))
  12849. ((org-clocktable-try-shift 'down arg))
  12850. (org-support-shift-select
  12851. (org-call-for-shift-select 'next-line))
  12852. (t (org-shiftselect-error))))
  12853. (defun org-shiftright (&optional arg)
  12854. "Cycle the thing at point or in the current line, depending on context.
  12855. Depending on context, this does one of the following:
  12856. - switch a timestamp at point one day into the future
  12857. - on a headline, switch to the next TODO keyword.
  12858. - on an item, switch entire list to the next bullet type
  12859. - on a property line, switch to the next allowed value
  12860. - on a clocktable definition line, move time block into the future"
  12861. (interactive "P")
  12862. (cond
  12863. ((and org-support-shift-select (org-region-active-p))
  12864. (org-call-for-shift-select 'forward-char))
  12865. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12866. ((and (not (eq org-support-shift-select 'always))
  12867. (org-on-heading-p))
  12868. (org-call-with-arg 'org-todo 'right))
  12869. ((or (and org-support-shift-select
  12870. (not (eq org-support-shift-select 'always))
  12871. (org-at-item-bullet-p))
  12872. (and (not org-support-shift-select) (org-at-item-p)))
  12873. (org-call-with-arg 'org-cycle-list-bullet nil))
  12874. ((and (not (eq org-support-shift-select 'always))
  12875. (org-at-property-p))
  12876. (call-interactively 'org-property-next-allowed-value))
  12877. ((org-clocktable-try-shift 'right arg))
  12878. (org-support-shift-select
  12879. (org-call-for-shift-select 'forward-char))
  12880. (t (org-shiftselect-error))))
  12881. (defun org-shiftleft (&optional arg)
  12882. "Cycle the thing at point or in the current line, depending on context.
  12883. Depending on context, this does one of the following:
  12884. - switch a timestamp at point one day into the past
  12885. - on a headline, switch to the previous TODO keyword.
  12886. - on an item, switch entire list to the previous bullet type
  12887. - on a property line, switch to the previous allowed value
  12888. - on a clocktable definition line, move time block into the past"
  12889. (interactive "P")
  12890. (cond
  12891. ((and org-support-shift-select (org-region-active-p))
  12892. (org-call-for-shift-select 'backward-char))
  12893. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12894. ((and (not (eq org-support-shift-select 'always))
  12895. (org-on-heading-p))
  12896. (org-call-with-arg 'org-todo 'left))
  12897. ((or (and org-support-shift-select
  12898. (not (eq org-support-shift-select 'always))
  12899. (org-at-item-bullet-p))
  12900. (and (not org-support-shift-select) (org-at-item-p)))
  12901. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12902. ((and (not (eq org-support-shift-select 'always))
  12903. (org-at-property-p))
  12904. (call-interactively 'org-property-previous-allowed-value))
  12905. ((org-clocktable-try-shift 'left arg))
  12906. (org-support-shift-select
  12907. (org-call-for-shift-select 'backward-char))
  12908. (t (org-shiftselect-error))))
  12909. (defun org-shiftcontrolright ()
  12910. "Switch to next TODO set."
  12911. (interactive)
  12912. (cond
  12913. ((and org-support-shift-select (org-region-active-p))
  12914. (org-call-for-shift-select 'forward-word))
  12915. ((and (not (eq org-support-shift-select 'always))
  12916. (org-on-heading-p))
  12917. (org-call-with-arg 'org-todo 'nextset))
  12918. (org-support-shift-select
  12919. (org-call-for-shift-select 'forward-word))
  12920. (t (org-shiftselect-error))))
  12921. (defun org-shiftcontrolleft ()
  12922. "Switch to previous TODO set."
  12923. (interactive)
  12924. (cond
  12925. ((and org-support-shift-select (org-region-active-p))
  12926. (org-call-for-shift-select 'backward-word))
  12927. ((and (not (eq org-support-shift-select 'always))
  12928. (org-on-heading-p))
  12929. (org-call-with-arg 'org-todo 'previousset))
  12930. (org-support-shift-select
  12931. (org-call-for-shift-select 'backward-word))
  12932. (t (org-shiftselect-error))))
  12933. (defun org-ctrl-c-ret ()
  12934. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12935. (interactive)
  12936. (cond
  12937. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12938. (t (call-interactively 'org-insert-heading))))
  12939. (defun org-copy-special ()
  12940. "Copy region in table or copy current subtree.
  12941. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12942. See the individual commands for more information."
  12943. (interactive)
  12944. (call-interactively
  12945. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12946. (defun org-cut-special ()
  12947. "Cut region in table or cut current subtree.
  12948. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12949. See the individual commands for more information."
  12950. (interactive)
  12951. (call-interactively
  12952. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12953. (defun org-paste-special (arg)
  12954. "Paste rectangular region into table, or past subtree relative to level.
  12955. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12956. See the individual commands for more information."
  12957. (interactive "P")
  12958. (if (org-at-table-p)
  12959. (org-table-paste-rectangle)
  12960. (org-paste-subtree arg)))
  12961. (defun org-edit-special ()
  12962. "Call a special editor for the stuff at point.
  12963. When at a table, call the formula editor with `org-table-edit-formulas'.
  12964. When at the first line of an src example, call `org-edit-src-code'.
  12965. When in an #+include line, visit the include file. Otherwise call
  12966. `ffap' to visit the file at point."
  12967. (interactive)
  12968. (cond
  12969. ((org-at-table-p)
  12970. (call-interactively 'org-table-edit-formulas))
  12971. ((save-excursion
  12972. (beginning-of-line 1)
  12973. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12974. (find-file (org-trim (match-string 1))))
  12975. ((org-edit-src-code))
  12976. ((org-edit-fixed-width-region))
  12977. (t (call-interactively 'ffap))))
  12978. (defun org-ctrl-c-ctrl-c (&optional arg)
  12979. "Set tags in headline, or update according to changed information at point.
  12980. This command does many different things, depending on context:
  12981. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12982. this is what we do.
  12983. - If the cursor is in a headline, prompt for tags and insert them
  12984. into the current line, aligned to `org-tags-column'. When called
  12985. with prefix arg, realign all tags in the current buffer.
  12986. - If the cursor is in one of the special #+KEYWORD lines, this
  12987. triggers scanning the buffer for these lines and updating the
  12988. information.
  12989. - If the cursor is inside a table, realign the table. This command
  12990. works even if the automatic table editor has been turned off.
  12991. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12992. the entire table.
  12993. - If the cursor is at a footnote reference or definition, jump to
  12994. the corresponding definition or references, respectively.
  12995. - If the cursor is a the beginning of a dynamic block, update it.
  12996. - If the cursor is inside a table created by the table.el package,
  12997. activate that table.
  12998. - If the current buffer is a remember buffer, close note and file
  12999. it. A prefix argument of 1 files to the default location
  13000. without further interaction. A prefix argument of 2 files to
  13001. the currently clocking task.
  13002. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13003. links in this buffer.
  13004. - If the cursor is on a numbered item in a plain list, renumber the
  13005. ordered list.
  13006. - If the cursor is on a checkbox, toggle it."
  13007. (interactive "P")
  13008. (let ((org-enable-table-editor t))
  13009. (cond
  13010. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13011. org-occur-highlights
  13012. org-latex-fragment-image-overlays)
  13013. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13014. (org-remove-occur-highlights)
  13015. (org-remove-latex-fragment-image-overlays)
  13016. (message "Temporary highlights/overlays removed from current buffer"))
  13017. ((and (local-variable-p 'org-finish-function (current-buffer))
  13018. (fboundp org-finish-function))
  13019. (funcall org-finish-function))
  13020. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13021. ((org-at-property-p)
  13022. (call-interactively 'org-property-action))
  13023. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13024. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13025. ((org-at-table.el-p)
  13026. (require 'table)
  13027. (beginning-of-line 1)
  13028. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13029. (call-interactively 'table-recognize-table))
  13030. ((org-at-table-p)
  13031. (org-table-maybe-eval-formula)
  13032. (if arg
  13033. (call-interactively 'org-table-recalculate)
  13034. (org-table-maybe-recalculate-line))
  13035. (call-interactively 'org-table-align))
  13036. ((or (org-footnote-at-reference-p)
  13037. (org-footnote-at-definition-p))
  13038. (call-interactively 'org-footnote-action))
  13039. ((org-at-item-checkbox-p)
  13040. (call-interactively 'org-toggle-checkbox))
  13041. ((org-at-item-p)
  13042. (if arg
  13043. (call-interactively 'org-toggle-checkbox)
  13044. (call-interactively 'org-maybe-renumber-ordered-list)))
  13045. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13046. ;; Dynamic block
  13047. (beginning-of-line 1)
  13048. (save-excursion (org-update-dblock)))
  13049. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13050. (cond
  13051. ((equal (match-string 1) "TBLFM")
  13052. ;; Recalculate the table before this line
  13053. (save-excursion
  13054. (beginning-of-line 1)
  13055. (skip-chars-backward " \r\n\t")
  13056. (if (org-at-table-p)
  13057. (org-call-with-arg 'org-table-recalculate t))))
  13058. (t
  13059. ; (org-set-regexps-and-options)
  13060. ; (org-restart-font-lock)
  13061. (let ((org-inhibit-startup t)) (org-mode-restart))
  13062. (message "Local setup has been refreshed"))))
  13063. (t (error "C-c C-c can do nothing useful at this location.")))))
  13064. (defun org-mode-restart ()
  13065. "Restart Org-mode, to scan again for special lines.
  13066. Also updates the keyword regular expressions."
  13067. (interactive)
  13068. (org-mode)
  13069. (message "Org-mode restarted"))
  13070. (defun org-kill-note-or-show-branches ()
  13071. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13072. (interactive)
  13073. (if (not org-finish-function)
  13074. (call-interactively 'show-branches)
  13075. (let ((org-note-abort t))
  13076. (funcall org-finish-function))))
  13077. (defun org-return (&optional indent)
  13078. "Goto next table row or insert a newline.
  13079. Calls `org-table-next-row' or `newline', depending on context.
  13080. See the individual commands for more information."
  13081. (interactive)
  13082. (cond
  13083. ((bobp) (if indent (newline-and-indent) (newline)))
  13084. ((org-at-table-p)
  13085. (org-table-justify-field-maybe)
  13086. (call-interactively 'org-table-next-row))
  13087. ((and org-return-follows-link
  13088. (eq (get-text-property (point) 'face) 'org-link))
  13089. (call-interactively 'org-open-at-point))
  13090. ((and (org-at-heading-p)
  13091. (looking-at
  13092. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13093. (org-show-entry)
  13094. (end-of-line 1)
  13095. (newline))
  13096. (t (if indent (newline-and-indent) (newline)))))
  13097. (defun org-return-indent ()
  13098. "Goto next table row or insert a newline and indent.
  13099. Calls `org-table-next-row' or `newline-and-indent', depending on
  13100. context. See the individual commands for more information."
  13101. (interactive)
  13102. (org-return t))
  13103. (defun org-ctrl-c-star ()
  13104. "Compute table, or change heading status of lines.
  13105. Calls `org-table-recalculate' or `org-toggle-heading',
  13106. depending on context."
  13107. (interactive)
  13108. (cond
  13109. ((org-at-table-p)
  13110. (call-interactively 'org-table-recalculate))
  13111. (t
  13112. ;; Convert all lines in region to list items
  13113. (call-interactively 'org-toggle-heading))))
  13114. (defun org-ctrl-c-minus ()
  13115. "Insert separator line in table or modify bullet status of line.
  13116. Also turns a plain line or a region of lines into list items.
  13117. Calls `org-table-insert-hline', `org-toggle-item', or
  13118. `org-cycle-list-bullet', depending on context."
  13119. (interactive)
  13120. (cond
  13121. ((org-at-table-p)
  13122. (call-interactively 'org-table-insert-hline))
  13123. ((org-region-active-p)
  13124. (call-interactively 'org-toggle-item))
  13125. ((org-in-item-p)
  13126. (call-interactively 'org-cycle-list-bullet))
  13127. (t
  13128. (call-interactively 'org-toggle-item))))
  13129. (defun org-toggle-item ()
  13130. "Convert headings or normal lines to items, items to normal lines.
  13131. If there is no active region, only the current line is considered.
  13132. If the first line in the region is a headline, convert all headlines to items.
  13133. If the first line in the region is an item, convert all items to normal lines.
  13134. If the first line is normal text, add an item bullet to each line."
  13135. (interactive)
  13136. (let (l2 l beg end)
  13137. (if (org-region-active-p)
  13138. (setq beg (region-beginning) end (region-end))
  13139. (setq beg (point-at-bol)
  13140. end (min (1+ (point-at-eol)) (point-max))))
  13141. (save-excursion
  13142. (goto-char end)
  13143. (setq l2 (org-current-line))
  13144. (goto-char beg)
  13145. (beginning-of-line 1)
  13146. (setq l (1- (org-current-line)))
  13147. (if (org-at-item-p)
  13148. ;; We already have items, de-itemize
  13149. (while (< (setq l (1+ l)) l2)
  13150. (when (org-at-item-p)
  13151. (goto-char (match-beginning 2))
  13152. (delete-region (match-beginning 2) (match-end 2))
  13153. (and (looking-at "[ \t]+") (replace-match "")))
  13154. (beginning-of-line 2))
  13155. (if (org-on-heading-p)
  13156. ;; Headings, convert to items
  13157. (while (< (setq l (1+ l)) l2)
  13158. (if (looking-at org-outline-regexp)
  13159. (replace-match "- " t t))
  13160. (beginning-of-line 2))
  13161. ;; normal lines, turn them into items
  13162. (while (< (setq l (1+ l)) l2)
  13163. (unless (org-at-item-p)
  13164. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13165. (replace-match "\\1- \\2")))
  13166. (beginning-of-line 2)))))))
  13167. (defun org-toggle-heading (&optional nstars)
  13168. "Convert headings to normal text, or items or text to headings.
  13169. If there is no active region, only the current line is considered.
  13170. If the first line is a heading, remove the stars from all headlines
  13171. in the region.
  13172. If the first line is a plain list item, turn all plain list items into
  13173. headings.
  13174. If the first line is a normal line, turn each and every line in the region
  13175. into a heading.
  13176. When converting a line into a heading, the number of stars is chosen
  13177. such that the lines become children of the current entry. However, when
  13178. a prefix argument is given, its value determines the number of stars to add."
  13179. (interactive "P")
  13180. (let (l2 l itemp beg end)
  13181. (if (org-region-active-p)
  13182. (setq beg (region-beginning) end (region-end))
  13183. (setq beg (point-at-bol)
  13184. end (min (1+ (point-at-eol)) (point-max))))
  13185. (save-excursion
  13186. (goto-char end)
  13187. (setq l2 (org-current-line))
  13188. (goto-char beg)
  13189. (beginning-of-line 1)
  13190. (setq l (1- (org-current-line)))
  13191. (if (org-on-heading-p)
  13192. ;; We already have headlines, de-star them
  13193. (while (< (setq l (1+ l)) l2)
  13194. (when (org-on-heading-p t)
  13195. (and (looking-at outline-regexp) (replace-match "")))
  13196. (beginning-of-line 2))
  13197. (setq itemp (org-at-item-p))
  13198. (let* ((stars
  13199. (if nstars
  13200. (make-string (prefix-numeric-value current-prefix-arg)
  13201. ?*)
  13202. (save-excursion
  13203. (re-search-backward org-complex-heading-regexp nil t)
  13204. (or (match-string 1) ""))))
  13205. (add-stars (cond (nstars "")
  13206. ((equal stars "") "*")
  13207. (org-odd-levels-only "**")
  13208. (t "*")))
  13209. (rpl (concat stars add-stars " ")))
  13210. (while (< (setq l (1+ l)) l2)
  13211. (if itemp
  13212. (and (org-at-item-p) (replace-match rpl t t))
  13213. (unless (org-on-heading-p)
  13214. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13215. (replace-match (concat rpl (match-string 2))))))
  13216. (beginning-of-line 2)))))))
  13217. (defun org-meta-return (&optional arg)
  13218. "Insert a new heading or wrap a region in a table.
  13219. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13220. See the individual commands for more information."
  13221. (interactive "P")
  13222. (cond
  13223. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13224. ((org-at-table-p)
  13225. (call-interactively 'org-table-wrap-region))
  13226. (t (call-interactively 'org-insert-heading))))
  13227. ;;; Menu entries
  13228. ;; Define the Org-mode menus
  13229. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13230. '("Tbl"
  13231. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13232. ["Next Field" org-cycle (org-at-table-p)]
  13233. ["Previous Field" org-shifttab (org-at-table-p)]
  13234. ["Next Row" org-return (org-at-table-p)]
  13235. "--"
  13236. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13237. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13238. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13239. "--"
  13240. ("Column"
  13241. ["Move Column Left" org-metaleft (org-at-table-p)]
  13242. ["Move Column Right" org-metaright (org-at-table-p)]
  13243. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13244. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13245. ("Row"
  13246. ["Move Row Up" org-metaup (org-at-table-p)]
  13247. ["Move Row Down" org-metadown (org-at-table-p)]
  13248. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13249. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13250. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13251. "--"
  13252. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13253. ("Rectangle"
  13254. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13255. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13256. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13257. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13258. "--"
  13259. ("Calculate"
  13260. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13261. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13262. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13263. "--"
  13264. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13265. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13266. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13267. "--"
  13268. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13269. "--"
  13270. ["Sum Column/Rectangle" org-table-sum
  13271. (or (org-at-table-p) (org-region-active-p))]
  13272. ["Which Column?" org-table-current-column (org-at-table-p)])
  13273. ["Debug Formulas"
  13274. org-table-toggle-formula-debugger
  13275. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13276. ["Show Col/Row Numbers"
  13277. org-table-toggle-coordinate-overlays
  13278. :style toggle
  13279. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13280. "--"
  13281. ["Create" org-table-create (and (not (org-at-table-p))
  13282. org-enable-table-editor)]
  13283. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13284. ["Import from File" org-table-import (not (org-at-table-p))]
  13285. ["Export to File" org-table-export (org-at-table-p)]
  13286. "--"
  13287. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13288. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13289. '("Org"
  13290. ("Show/Hide"
  13291. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13292. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13293. ["Sparse Tree..." org-sparse-tree t]
  13294. ["Reveal Context" org-reveal t]
  13295. ["Show All" show-all t]
  13296. "--"
  13297. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13298. "--"
  13299. ["New Heading" org-insert-heading t]
  13300. ("Navigate Headings"
  13301. ["Up" outline-up-heading t]
  13302. ["Next" outline-next-visible-heading t]
  13303. ["Previous" outline-previous-visible-heading t]
  13304. ["Next Same Level" outline-forward-same-level t]
  13305. ["Previous Same Level" outline-backward-same-level t]
  13306. "--"
  13307. ["Jump" org-goto t])
  13308. ("Edit Structure"
  13309. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13310. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13311. "--"
  13312. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13313. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13314. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13315. "--"
  13316. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13317. "--"
  13318. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13319. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13320. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13321. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13322. "--"
  13323. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13324. "--"
  13325. ["Convert to odd levels" org-convert-to-odd-levels t]
  13326. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13327. ("Editing"
  13328. ["Emphasis..." org-emphasize t]
  13329. ["Edit Source Example" org-edit-special t]
  13330. "--"
  13331. ["Footnote new/jump" org-footnote-action t]
  13332. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13333. ("Archive"
  13334. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13335. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13336. ; :active t :keys "C-u C-c C-x C-a"]
  13337. ["Sparse trees open ARCHIVE trees"
  13338. (setq org-sparse-tree-open-archived-trees
  13339. (not org-sparse-tree-open-archived-trees))
  13340. :style toggle :selected org-sparse-tree-open-archived-trees]
  13341. ["Cycling opens ARCHIVE trees"
  13342. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13343. :style toggle :selected org-cycle-open-archived-trees]
  13344. "--"
  13345. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13346. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13347. ; ["Check and Move Children" (org-archive-subtree '(4))
  13348. ; :active t :keys "C-u C-c C-x C-s"]
  13349. )
  13350. "--"
  13351. ("Hyperlinks"
  13352. ["Store Link (Global)" org-store-link t]
  13353. ["Insert Link" org-insert-link t]
  13354. ["Follow Link" org-open-at-point t]
  13355. "--"
  13356. ["Next link" org-next-link t]
  13357. ["Previous link" org-previous-link t]
  13358. "--"
  13359. ["Descriptive Links"
  13360. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13361. :style radio
  13362. :selected (member '(org-link) buffer-invisibility-spec)]
  13363. ["Literal Links"
  13364. (progn
  13365. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13366. :style radio
  13367. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13368. "--"
  13369. ("TODO Lists"
  13370. ["TODO/DONE/-" org-todo t]
  13371. ("Select keyword"
  13372. ["Next keyword" org-shiftright (org-on-heading-p)]
  13373. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13374. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13375. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13376. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13377. ["Show TODO Tree" org-show-todo-tree t]
  13378. ["Global TODO list" org-todo-list t]
  13379. "--"
  13380. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13381. :selected org-enforce-todo-dependencies :style toggle :active t]
  13382. "Settings for tree at point"
  13383. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13384. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13385. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13386. ["Do Children parallel" org-toggle-ordered-property :style radio
  13387. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13388. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13389. "--"
  13390. ["Set Priority" org-priority t]
  13391. ["Priority Up" org-shiftup t]
  13392. ["Priority Down" org-shiftdown t]
  13393. "--"
  13394. ["Get news from all feeds" org-feed-update-all t]
  13395. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13396. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13397. ("TAGS and Properties"
  13398. ["Set Tags" org-set-tags-command t]
  13399. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13400. "--"
  13401. ["Set property" org-set-property t]
  13402. ["Column view of properties" org-columns t]
  13403. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13404. ("Dates and Scheduling"
  13405. ["Timestamp" org-time-stamp t]
  13406. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13407. ("Change Date"
  13408. ["1 Day Later" org-shiftright t]
  13409. ["1 Day Earlier" org-shiftleft t]
  13410. ["1 ... Later" org-shiftup t]
  13411. ["1 ... Earlier" org-shiftdown t])
  13412. ["Compute Time Range" org-evaluate-time-range t]
  13413. ["Schedule Item" org-schedule t]
  13414. ["Deadline" org-deadline t]
  13415. "--"
  13416. ["Custom time format" org-toggle-time-stamp-overlays
  13417. :style radio :selected org-display-custom-times]
  13418. "--"
  13419. ["Goto Calendar" org-goto-calendar t]
  13420. ["Date from Calendar" org-date-from-calendar t]
  13421. "--"
  13422. ["Start/Restart Timer" org-timer-start t]
  13423. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13424. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13425. ["Insert Timer String" org-timer t]
  13426. ["Insert Timer Item" org-timer-item t])
  13427. ("Logging work"
  13428. ["Clock in" org-clock-in t]
  13429. ["Clock out" org-clock-out t]
  13430. ["Clock cancel" org-clock-cancel t]
  13431. ["Goto running clock" org-clock-goto t]
  13432. ["Display times" org-clock-display t]
  13433. ["Create clock table" org-clock-report t]
  13434. "--"
  13435. ["Record DONE time"
  13436. (progn (setq org-log-done (not org-log-done))
  13437. (message "Switching to %s will %s record a timestamp"
  13438. (car org-done-keywords)
  13439. (if org-log-done "automatically" "not")))
  13440. :style toggle :selected org-log-done])
  13441. "--"
  13442. ["Agenda Command..." org-agenda t]
  13443. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13444. ("File List for Agenda")
  13445. ("Special views current file"
  13446. ["TODO Tree" org-show-todo-tree t]
  13447. ["Check Deadlines" org-check-deadlines t]
  13448. ["Timeline" org-timeline t]
  13449. ["Tags/Property tree" org-match-sparse-tree t])
  13450. "--"
  13451. ["Export/Publish..." org-export t]
  13452. ("LaTeX"
  13453. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13454. :selected org-cdlatex-mode]
  13455. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13456. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13457. ["Modify math symbol" org-cdlatex-math-modify
  13458. (org-inside-LaTeX-fragment-p)]
  13459. ["Insert citation" org-reftex-citation t]
  13460. "--"
  13461. ["Export LaTeX fragments as images"
  13462. (if (featurep 'org-exp)
  13463. (setq org-export-with-LaTeX-fragments
  13464. (not org-export-with-LaTeX-fragments))
  13465. (require 'org-exp))
  13466. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13467. org-export-with-LaTeX-fragments)])
  13468. "--"
  13469. ("Documentation"
  13470. ["Show Version" org-version t]
  13471. ["Info Documentation" org-info t])
  13472. ("Customize"
  13473. ["Browse Org Group" org-customize t]
  13474. "--"
  13475. ["Expand This Menu" org-create-customize-menu
  13476. (fboundp 'customize-menu-create)])
  13477. "--"
  13478. ("Refresh/Reload"
  13479. ["Refresh setup current buffer" org-mode-restart t]
  13480. ["Reload Org (after update)" org-reload t]
  13481. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13482. ))
  13483. (defun org-info (&optional node)
  13484. "Read documentation for Org-mode in the info system.
  13485. With optional NODE, go directly to that node."
  13486. (interactive)
  13487. (info (format "(org)%s" (or node ""))))
  13488. (defun org-install-agenda-files-menu ()
  13489. (let ((bl (buffer-list)))
  13490. (save-excursion
  13491. (while bl
  13492. (set-buffer (pop bl))
  13493. (if (org-mode-p) (setq bl nil)))
  13494. (when (org-mode-p)
  13495. (easy-menu-change
  13496. '("Org") "File List for Agenda"
  13497. (append
  13498. (list
  13499. ["Edit File List" (org-edit-agenda-file-list) t]
  13500. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13501. ["Remove Current File from List" org-remove-file t]
  13502. ["Cycle through agenda files" org-cycle-agenda-files t]
  13503. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13504. "--")
  13505. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13506. ;;;; Documentation
  13507. ;;;###autoload
  13508. (defun org-require-autoloaded-modules ()
  13509. (interactive)
  13510. (mapc 'require
  13511. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13512. org-docbook org-exp org-html org-icalendar
  13513. org-id org-latex
  13514. org-publish org-remember org-table
  13515. org-timer org-xoxo)))
  13516. ;;;###autoload
  13517. (defun org-reload (&optional uncompiled)
  13518. "Reload all org lisp files.
  13519. With prefix arg UNCOMPILED, load the uncompiled versions."
  13520. (interactive "P")
  13521. (require 'find-func)
  13522. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13523. (dir-org (file-name-directory (org-find-library-name "org")))
  13524. (dir-org-contrib (ignore-errors
  13525. (file-name-directory
  13526. (org-find-library-name "org-contribdir"))))
  13527. (files
  13528. (append (directory-files dir-org t file-re)
  13529. (and dir-org-contrib
  13530. (directory-files dir-org-contrib t file-re))))
  13531. (remove-re (concat (if (featurep 'xemacs)
  13532. "org-colview" "org-colview-xemacs")
  13533. "\\'")))
  13534. (setq files (mapcar 'file-name-sans-extension files))
  13535. (setq files (mapcar
  13536. (lambda (x) (if (string-match remove-re x) nil x))
  13537. files))
  13538. (setq files (delq nil files))
  13539. (mapc
  13540. (lambda (f)
  13541. (when (featurep (intern (file-name-nondirectory f)))
  13542. (if (and (not uncompiled)
  13543. (file-exists-p (concat f ".elc")))
  13544. (load (concat f ".elc") nil nil t)
  13545. (load (concat f ".el") nil nil t))))
  13546. files))
  13547. (org-version))
  13548. ;;;###autoload
  13549. (defun org-customize ()
  13550. "Call the customize function with org as argument."
  13551. (interactive)
  13552. (org-load-modules-maybe)
  13553. (org-require-autoloaded-modules)
  13554. (customize-browse 'org))
  13555. (defun org-create-customize-menu ()
  13556. "Create a full customization menu for Org-mode, insert it into the menu."
  13557. (interactive)
  13558. (org-load-modules-maybe)
  13559. (org-require-autoloaded-modules)
  13560. (if (fboundp 'customize-menu-create)
  13561. (progn
  13562. (easy-menu-change
  13563. '("Org") "Customize"
  13564. `(["Browse Org group" org-customize t]
  13565. "--"
  13566. ,(customize-menu-create 'org)
  13567. ["Set" Custom-set t]
  13568. ["Save" Custom-save t]
  13569. ["Reset to Current" Custom-reset-current t]
  13570. ["Reset to Saved" Custom-reset-saved t]
  13571. ["Reset to Standard Settings" Custom-reset-standard t]))
  13572. (message "\"Org\"-menu now contains full customization menu"))
  13573. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13574. ;;;; Miscellaneous stuff
  13575. ;;; Generally useful functions
  13576. (defun org-find-text-property-in-string (prop s)
  13577. "Return the first non-nil value of property PROP in string S."
  13578. (or (get-text-property 0 prop s)
  13579. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13580. prop s)))
  13581. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13582. "Display the given MESSAGE as a warning."
  13583. (if (fboundp 'display-warning)
  13584. (display-warning 'org message
  13585. (if (featurep 'xemacs)
  13586. 'warning
  13587. :warning))
  13588. (let ((buf (get-buffer-create "*Org warnings*")))
  13589. (with-current-buffer buf
  13590. (goto-char (point-max))
  13591. (insert "Warning (Org): " message)
  13592. (unless (bolp)
  13593. (newline)))
  13594. (display-buffer buf)
  13595. (sit-for 0))))
  13596. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13597. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13598. (if (and marker (marker-buffer marker)
  13599. (buffer-live-p (marker-buffer marker)))
  13600. (progn
  13601. (switch-to-buffer (marker-buffer marker))
  13602. (if (or (> marker (point-max)) (< marker (point-min)))
  13603. (widen))
  13604. (goto-char marker)
  13605. (org-show-context 'org-goto))
  13606. (if bookmark
  13607. (bookmark-jump bookmark)
  13608. (error "Cannot find location"))))
  13609. (defun org-quote-csv-field (s)
  13610. "Quote field for inclusion in CSV material."
  13611. (if (string-match "[\",]" s)
  13612. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13613. s))
  13614. (defun org-plist-delete (plist property)
  13615. "Delete PROPERTY from PLIST.
  13616. This is in contrast to merely setting it to 0."
  13617. (let (p)
  13618. (while plist
  13619. (if (not (eq property (car plist)))
  13620. (setq p (plist-put p (car plist) (nth 1 plist))))
  13621. (setq plist (cddr plist)))
  13622. p))
  13623. (defun org-force-self-insert (N)
  13624. "Needed to enforce self-insert under remapping."
  13625. (interactive "p")
  13626. (self-insert-command N))
  13627. (defun org-string-width (s)
  13628. "Compute width of string, ignoring invisible characters.
  13629. This ignores character with invisibility property `org-link', and also
  13630. characters with property `org-cwidth', because these will become invisible
  13631. upon the next fontification round."
  13632. (let (b l)
  13633. (when (or (eq t buffer-invisibility-spec)
  13634. (assq 'org-link buffer-invisibility-spec))
  13635. (while (setq b (text-property-any 0 (length s)
  13636. 'invisible 'org-link s))
  13637. (setq s (concat (substring s 0 b)
  13638. (substring s (or (next-single-property-change
  13639. b 'invisible s) (length s)))))))
  13640. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13641. (setq s (concat (substring s 0 b)
  13642. (substring s (or (next-single-property-change
  13643. b 'org-cwidth s) (length s))))))
  13644. (setq l (string-width s) b -1)
  13645. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13646. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13647. l))
  13648. (defun org-get-indentation (&optional line)
  13649. "Get the indentation of the current line, interpreting tabs.
  13650. When LINE is given, assume it represents a line and compute its indentation."
  13651. (if line
  13652. (if (string-match "^ *" (org-remove-tabs line))
  13653. (match-end 0))
  13654. (save-excursion
  13655. (beginning-of-line 1)
  13656. (skip-chars-forward " \t")
  13657. (current-column))))
  13658. (defun org-remove-tabs (s &optional width)
  13659. "Replace tabulators in S with spaces.
  13660. Assumes that s is a single line, starting in column 0."
  13661. (setq width (or width tab-width))
  13662. (while (string-match "\t" s)
  13663. (setq s (replace-match
  13664. (make-string
  13665. (- (* width (/ (+ (match-beginning 0) width) width))
  13666. (match-beginning 0)) ?\ )
  13667. t t s)))
  13668. s)
  13669. (defun org-fix-indentation (line ind)
  13670. "Fix indentation in LINE.
  13671. IND is a cons cell with target and minimum indentation.
  13672. If the current indentation in LINE is smaller than the minimum,
  13673. leave it alone. If it is larger than ind, set it to the target."
  13674. (let* ((l (org-remove-tabs line))
  13675. (i (org-get-indentation l))
  13676. (i1 (car ind)) (i2 (cdr ind)))
  13677. (if (>= i i2) (setq l (substring line i2)))
  13678. (if (> i1 0)
  13679. (concat (make-string i1 ?\ ) l)
  13680. l)))
  13681. (defun org-base-buffer (buffer)
  13682. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13683. (if (not buffer)
  13684. buffer
  13685. (or (buffer-base-buffer buffer)
  13686. buffer)))
  13687. (defun org-trim (s)
  13688. "Remove whitespace at beginning and end of string."
  13689. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13690. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13691. s)
  13692. (defun org-wrap (string &optional width lines)
  13693. "Wrap string to either a number of lines, or a width in characters.
  13694. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13695. that costs. If there is a word longer than WIDTH, the text is actually
  13696. wrapped to the length of that word.
  13697. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13698. many lines, whatever width that takes.
  13699. The return value is a list of lines, without newlines at the end."
  13700. (let* ((words (org-split-string string "[ \t\n]+"))
  13701. (maxword (apply 'max (mapcar 'org-string-width words)))
  13702. w ll)
  13703. (cond (width
  13704. (org-do-wrap words (max maxword width)))
  13705. (lines
  13706. (setq w maxword)
  13707. (setq ll (org-do-wrap words maxword))
  13708. (if (<= (length ll) lines)
  13709. ll
  13710. (setq ll words)
  13711. (while (> (length ll) lines)
  13712. (setq w (1+ w))
  13713. (setq ll (org-do-wrap words w)))
  13714. ll))
  13715. (t (error "Cannot wrap this")))))
  13716. (defun org-do-wrap (words width)
  13717. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13718. (let (lines line)
  13719. (while words
  13720. (setq line (pop words))
  13721. (while (and words (< (+ (length line) (length (car words))) width))
  13722. (setq line (concat line " " (pop words))))
  13723. (setq lines (push line lines)))
  13724. (nreverse lines)))
  13725. (defun org-split-string (string &optional separators)
  13726. "Splits STRING into substrings at SEPARATORS.
  13727. No empty strings are returned if there are matches at the beginning
  13728. and end of string."
  13729. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13730. (start 0)
  13731. notfirst
  13732. (list nil))
  13733. (while (and (string-match rexp string
  13734. (if (and notfirst
  13735. (= start (match-beginning 0))
  13736. (< start (length string)))
  13737. (1+ start) start))
  13738. (< (match-beginning 0) (length string)))
  13739. (setq notfirst t)
  13740. (or (eq (match-beginning 0) 0)
  13741. (and (eq (match-beginning 0) (match-end 0))
  13742. (eq (match-beginning 0) start))
  13743. (setq list
  13744. (cons (substring string start (match-beginning 0))
  13745. list)))
  13746. (setq start (match-end 0)))
  13747. (or (eq start (length string))
  13748. (setq list
  13749. (cons (substring string start)
  13750. list)))
  13751. (nreverse list)))
  13752. (defun org-uuidgen-p (s)
  13753. "Is S an ID created by UUIDGEN?"
  13754. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13755. (defun org-context ()
  13756. "Return a list of contexts of the current cursor position.
  13757. If several contexts apply, all are returned.
  13758. Each context entry is a list with a symbol naming the context, and
  13759. two positions indicating start and end of the context. Possible
  13760. contexts are:
  13761. :headline anywhere in a headline
  13762. :headline-stars on the leading stars in a headline
  13763. :todo-keyword on a TODO keyword (including DONE) in a headline
  13764. :tags on the TAGS in a headline
  13765. :priority on the priority cookie in a headline
  13766. :item on the first line of a plain list item
  13767. :item-bullet on the bullet/number of a plain list item
  13768. :checkbox on the checkbox in a plain list item
  13769. :table in an org-mode table
  13770. :table-special on a special filed in a table
  13771. :table-table in a table.el table
  13772. :link on a hyperlink
  13773. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13774. :target on a <<target>>
  13775. :radio-target on a <<<radio-target>>>
  13776. :latex-fragment on a LaTeX fragment
  13777. :latex-preview on a LaTeX fragment with overlayed preview image
  13778. This function expects the position to be visible because it uses font-lock
  13779. faces as a help to recognize the following contexts: :table-special, :link,
  13780. and :keyword."
  13781. (let* ((f (get-text-property (point) 'face))
  13782. (faces (if (listp f) f (list f)))
  13783. (p (point)) clist o)
  13784. ;; First the large context
  13785. (cond
  13786. ((org-on-heading-p t)
  13787. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13788. (when (progn
  13789. (beginning-of-line 1)
  13790. (looking-at org-todo-line-tags-regexp))
  13791. (push (org-point-in-group p 1 :headline-stars) clist)
  13792. (push (org-point-in-group p 2 :todo-keyword) clist)
  13793. (push (org-point-in-group p 4 :tags) clist))
  13794. (goto-char p)
  13795. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13796. (if (looking-at "\\[#[A-Z0-9]\\]")
  13797. (push (org-point-in-group p 0 :priority) clist)))
  13798. ((org-at-item-p)
  13799. (push (org-point-in-group p 2 :item-bullet) clist)
  13800. (push (list :item (point-at-bol)
  13801. (save-excursion (org-end-of-item) (point)))
  13802. clist)
  13803. (and (org-at-item-checkbox-p)
  13804. (push (org-point-in-group p 0 :checkbox) clist)))
  13805. ((org-at-table-p)
  13806. (push (list :table (org-table-begin) (org-table-end)) clist)
  13807. (if (memq 'org-formula faces)
  13808. (push (list :table-special
  13809. (previous-single-property-change p 'face)
  13810. (next-single-property-change p 'face)) clist)))
  13811. ((org-at-table-p 'any)
  13812. (push (list :table-table) clist)))
  13813. (goto-char p)
  13814. ;; Now the small context
  13815. (cond
  13816. ((org-at-timestamp-p)
  13817. (push (org-point-in-group p 0 :timestamp) clist))
  13818. ((memq 'org-link faces)
  13819. (push (list :link
  13820. (previous-single-property-change p 'face)
  13821. (next-single-property-change p 'face)) clist))
  13822. ((memq 'org-special-keyword faces)
  13823. (push (list :keyword
  13824. (previous-single-property-change p 'face)
  13825. (next-single-property-change p 'face)) clist))
  13826. ((org-on-target-p)
  13827. (push (org-point-in-group p 0 :target) clist)
  13828. (goto-char (1- (match-beginning 0)))
  13829. (if (looking-at org-radio-target-regexp)
  13830. (push (org-point-in-group p 0 :radio-target) clist))
  13831. (goto-char p))
  13832. ((setq o (car (delq nil
  13833. (mapcar
  13834. (lambda (x)
  13835. (if (memq x org-latex-fragment-image-overlays) x))
  13836. (org-overlays-at (point))))))
  13837. (push (list :latex-fragment
  13838. (org-overlay-start o) (org-overlay-end o)) clist)
  13839. (push (list :latex-preview
  13840. (org-overlay-start o) (org-overlay-end o)) clist))
  13841. ((org-inside-LaTeX-fragment-p)
  13842. ;; FIXME: positions wrong.
  13843. (push (list :latex-fragment (point) (point)) clist)))
  13844. (setq clist (nreverse (delq nil clist)))
  13845. clist))
  13846. ;; FIXME: Compare with at-regexp-p Do we need both?
  13847. (defun org-in-regexp (re &optional nlines visually)
  13848. "Check if point is inside a match of regexp.
  13849. Normally only the current line is checked, but you can include NLINES extra
  13850. lines both before and after point into the search.
  13851. If VISUALLY is set, require that the cursor is not after the match but
  13852. really on, so that the block visually is on the match."
  13853. (catch 'exit
  13854. (let ((pos (point))
  13855. (eol (point-at-eol (+ 1 (or nlines 0))))
  13856. (inc (if visually 1 0)))
  13857. (save-excursion
  13858. (beginning-of-line (- 1 (or nlines 0)))
  13859. (while (re-search-forward re eol t)
  13860. (if (and (<= (match-beginning 0) pos)
  13861. (>= (+ inc (match-end 0)) pos))
  13862. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13863. (defun org-at-regexp-p (regexp)
  13864. "Is point inside a match of REGEXP in the current line?"
  13865. (catch 'exit
  13866. (save-excursion
  13867. (let ((pos (point)) (end (point-at-eol)))
  13868. (beginning-of-line 1)
  13869. (while (re-search-forward regexp end t)
  13870. (if (and (<= (match-beginning 0) pos)
  13871. (>= (match-end 0) pos))
  13872. (throw 'exit t)))
  13873. nil))))
  13874. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13875. "Call `multi-occur' with buffers for all agenda files."
  13876. (interactive "sOrg-files matching: \np")
  13877. (let* ((files (org-agenda-files))
  13878. (tnames (mapcar 'file-truename files))
  13879. (extra org-agenda-text-search-extra-files)
  13880. f)
  13881. (when (eq (car extra) 'agenda-archives)
  13882. (setq extra (cdr extra))
  13883. (setq files (org-add-archive-files files)))
  13884. (while (setq f (pop extra))
  13885. (unless (member (file-truename f) tnames)
  13886. (add-to-list 'files f 'append)
  13887. (add-to-list 'tnames (file-truename f) 'append)))
  13888. (multi-occur
  13889. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13890. regexp)))
  13891. (if (boundp 'occur-mode-find-occurrence-hook)
  13892. ;; Emacs 23
  13893. (add-hook 'occur-mode-find-occurrence-hook
  13894. (lambda ()
  13895. (when (org-mode-p)
  13896. (org-reveal))))
  13897. ;; Emacs 22
  13898. (defadvice occur-mode-goto-occurrence
  13899. (after org-occur-reveal activate)
  13900. (and (org-mode-p) (org-reveal)))
  13901. (defadvice occur-mode-goto-occurrence-other-window
  13902. (after org-occur-reveal activate)
  13903. (and (org-mode-p) (org-reveal)))
  13904. (defadvice occur-mode-display-occurrence
  13905. (after org-occur-reveal activate)
  13906. (when (org-mode-p)
  13907. (let ((pos (occur-mode-find-occurrence)))
  13908. (with-current-buffer (marker-buffer pos)
  13909. (save-excursion
  13910. (goto-char pos)
  13911. (org-reveal)))))))
  13912. (defun org-uniquify (list)
  13913. "Remove duplicate elements from LIST."
  13914. (let (res)
  13915. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13916. res))
  13917. (defun org-delete-all (elts list)
  13918. "Remove all elements in ELTS from LIST."
  13919. (while elts
  13920. (setq list (delete (pop elts) list)))
  13921. list)
  13922. (defun org-back-over-empty-lines ()
  13923. "Move backwards over whitespace, to the beginning of the first empty line.
  13924. Returns the number of empty lines passed."
  13925. (let ((pos (point)))
  13926. (skip-chars-backward " \t\n\r")
  13927. (beginning-of-line 2)
  13928. (goto-char (min (point) pos))
  13929. (count-lines (point) pos)))
  13930. (defun org-skip-whitespace ()
  13931. (skip-chars-forward " \t\n\r"))
  13932. (defun org-point-in-group (point group &optional context)
  13933. "Check if POINT is in match-group GROUP.
  13934. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13935. match. If the match group does ot exist or point is not inside it,
  13936. return nil."
  13937. (and (match-beginning group)
  13938. (>= point (match-beginning group))
  13939. (<= point (match-end group))
  13940. (if context
  13941. (list context (match-beginning group) (match-end group))
  13942. t)))
  13943. (defun org-switch-to-buffer-other-window (&rest args)
  13944. "Switch to buffer in a second window on the current frame.
  13945. In particular, do not allow pop-up frames."
  13946. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13947. special-display-function)
  13948. (apply 'switch-to-buffer-other-window args)))
  13949. (defun org-combine-plists (&rest plists)
  13950. "Create a single property list from all plists in PLISTS.
  13951. The process starts by copying the first list, and then setting properties
  13952. from the other lists. Settings in the last list are the most significant
  13953. ones and overrule settings in the other lists."
  13954. (let ((rtn (copy-sequence (pop plists)))
  13955. p v ls)
  13956. (while plists
  13957. (setq ls (pop plists))
  13958. (while ls
  13959. (setq p (pop ls) v (pop ls))
  13960. (setq rtn (plist-put rtn p v))))
  13961. rtn))
  13962. (defun org-move-line-down (arg)
  13963. "Move the current line down. With prefix argument, move it past ARG lines."
  13964. (interactive "p")
  13965. (let ((col (current-column))
  13966. beg end pos)
  13967. (beginning-of-line 1) (setq beg (point))
  13968. (beginning-of-line 2) (setq end (point))
  13969. (beginning-of-line (+ 1 arg))
  13970. (setq pos (move-marker (make-marker) (point)))
  13971. (insert (delete-and-extract-region beg end))
  13972. (goto-char pos)
  13973. (org-move-to-column col)))
  13974. (defun org-move-line-up (arg)
  13975. "Move the current line up. With prefix argument, move it past ARG lines."
  13976. (interactive "p")
  13977. (let ((col (current-column))
  13978. beg end pos)
  13979. (beginning-of-line 1) (setq beg (point))
  13980. (beginning-of-line 2) (setq end (point))
  13981. (beginning-of-line (- arg))
  13982. (setq pos (move-marker (make-marker) (point)))
  13983. (insert (delete-and-extract-region beg end))
  13984. (goto-char pos)
  13985. (org-move-to-column col)))
  13986. (defun org-replace-escapes (string table)
  13987. "Replace %-escapes in STRING with values in TABLE.
  13988. TABLE is an association list with keys like \"%a\" and string values.
  13989. The sequences in STRING may contain normal field width and padding information,
  13990. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13991. so values can contain further %-escapes if they are define later in TABLE."
  13992. (let ((case-fold-search nil)
  13993. e re rpl)
  13994. (while (setq e (pop table))
  13995. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13996. (while (string-match re string)
  13997. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13998. (cdr e)))
  13999. (setq string (replace-match rpl t t string))))
  14000. string))
  14001. (defun org-sublist (list start end)
  14002. "Return a section of LIST, from START to END.
  14003. Counting starts at 1."
  14004. (let (rtn (c start))
  14005. (setq list (nthcdr (1- start) list))
  14006. (while (and list (<= c end))
  14007. (push (pop list) rtn)
  14008. (setq c (1+ c)))
  14009. (nreverse rtn)))
  14010. (defun org-find-base-buffer-visiting (file)
  14011. "Like `find-buffer-visiting' but alway return the base buffer and
  14012. not an indirect buffer."
  14013. (let ((buf (find-buffer-visiting file)))
  14014. (if buf
  14015. (or (buffer-base-buffer buf) buf)
  14016. nil)))
  14017. (defun org-image-file-name-regexp (&optional extensions)
  14018. "Return regexp matching the file names of images.
  14019. If EXTENSIONS is given, only match these."
  14020. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14021. (image-file-name-regexp)
  14022. (let ((image-file-name-extensions
  14023. (or extensions
  14024. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14025. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14026. (concat "\\."
  14027. (regexp-opt (nconc (mapcar 'upcase
  14028. image-file-name-extensions)
  14029. image-file-name-extensions)
  14030. t)
  14031. "\\'"))))
  14032. (defun org-file-image-p (file &optional extensions)
  14033. "Return non-nil if FILE is an image."
  14034. (save-match-data
  14035. (string-match (org-image-file-name-regexp extensions) file)))
  14036. (defun org-get-cursor-date ()
  14037. "Return the date at cursor in as a time.
  14038. This works in the calendar and in the agenda, anywhere else it just
  14039. returns the current time."
  14040. (let (date day defd)
  14041. (cond
  14042. ((eq major-mode 'calendar-mode)
  14043. (setq date (calendar-cursor-to-date)
  14044. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14045. ((eq major-mode 'org-agenda-mode)
  14046. (setq day (get-text-property (point) 'day))
  14047. (if day
  14048. (setq date (calendar-gregorian-from-absolute day)
  14049. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14050. (nth 2 date))))))
  14051. (or defd (current-time))))
  14052. (defvar org-agenda-action-marker (make-marker)
  14053. "Marker pointing to the entry for the next agenda action.")
  14054. (defun org-mark-entry-for-agenda-action ()
  14055. "Mark the current entry as target of an agenda action.
  14056. Agenda actions are actions executed from the agenda with the key `k',
  14057. which make use of the date at the cursor."
  14058. (interactive)
  14059. (move-marker org-agenda-action-marker
  14060. (save-excursion (org-back-to-heading t) (point))
  14061. (current-buffer))
  14062. (message
  14063. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14064. ;;; Paragraph filling stuff.
  14065. ;; We want this to be just right, so use the full arsenal.
  14066. (defun org-indent-line-function ()
  14067. "Indent line like previous, but further if previous was headline or item."
  14068. (interactive)
  14069. (let* ((pos (point))
  14070. (itemp (org-at-item-p))
  14071. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14072. column bpos bcol tpos tcol bullet btype bullet-type)
  14073. ;; Find the previous relevant line
  14074. (beginning-of-line 1)
  14075. (cond
  14076. ((looking-at "#") (setq column 0))
  14077. ((looking-at "\\*+ ") (setq column 0))
  14078. ((and (looking-at "[ \t]*:END:")
  14079. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14080. (save-excursion
  14081. (goto-char (1- (match-beginning 1)))
  14082. (setq column (current-column))))
  14083. (t
  14084. (beginning-of-line 0)
  14085. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14086. (not (looking-at "[ \t]*:END:"))
  14087. (not (looking-at org-drawer-regexp)))
  14088. (beginning-of-line 0))
  14089. (cond
  14090. ((looking-at "\\*+[ \t]+")
  14091. (if (not org-adapt-indentation)
  14092. (setq column 0)
  14093. (goto-char (match-end 0))
  14094. (setq column (current-column))))
  14095. ((looking-at org-drawer-regexp)
  14096. (goto-char (1- (match-beginning 1)))
  14097. (setq column (current-column)))
  14098. ((looking-at "\\([ \t]*\\):END:")
  14099. (goto-char (match-end 1))
  14100. (setq column (current-column)))
  14101. ((org-in-item-p)
  14102. (org-beginning-of-item)
  14103. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14104. (setq bpos (match-beginning 1) tpos (match-end 0)
  14105. bcol (progn (goto-char bpos) (current-column))
  14106. tcol (progn (goto-char tpos) (current-column))
  14107. bullet (match-string 1)
  14108. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14109. (if (> tcol (+ bcol org-description-max-indent))
  14110. (setq tcol (+ bcol 5)))
  14111. (if (not itemp)
  14112. (setq column tcol)
  14113. (goto-char pos)
  14114. (beginning-of-line 1)
  14115. (if (looking-at "\\S-")
  14116. (progn
  14117. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14118. (setq bullet (match-string 1)
  14119. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14120. (setq column (if (equal btype bullet-type) bcol tcol)))
  14121. (setq column (org-get-indentation)))))
  14122. (t (setq column (org-get-indentation))))))
  14123. (goto-char pos)
  14124. (if (<= (current-column) (current-indentation))
  14125. (org-indent-line-to column)
  14126. (save-excursion (org-indent-line-to column)))
  14127. (setq column (current-column))
  14128. (beginning-of-line 1)
  14129. (if (looking-at
  14130. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14131. (replace-match (concat "\\1" (format org-property-format
  14132. (match-string 2) (match-string 3)))
  14133. t nil))
  14134. (org-move-to-column column)))
  14135. (defun org-set-autofill-regexps ()
  14136. (interactive)
  14137. ;; In the paragraph separator we include headlines, because filling
  14138. ;; text in a line directly attached to a headline would otherwise
  14139. ;; fill the headline as well.
  14140. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14141. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14142. ;; The paragraph starter includes hand-formatted lists.
  14143. (org-set-local 'paragraph-start
  14144. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14145. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14146. ;; But only if the user has not turned off tables or fixed-width regions
  14147. (org-set-local
  14148. 'auto-fill-inhibit-regexp
  14149. (concat "\\*+ \\|#\\+"
  14150. "\\|[ \t]*" org-keyword-time-regexp
  14151. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14152. (concat
  14153. "\\|[ \t]*["
  14154. (if org-enable-table-editor "|" "")
  14155. (if org-enable-fixed-width-editor ":" "")
  14156. "]"))))
  14157. ;; We use our own fill-paragraph function, to make sure that tables
  14158. ;; and fixed-width regions are not wrapped. That function will pass
  14159. ;; through to `fill-paragraph' when appropriate.
  14160. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14161. ; Adaptive filling: To get full control, first make sure that
  14162. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14163. (org-set-local 'adaptive-fill-regexp "\000")
  14164. (org-set-local 'adaptive-fill-function
  14165. 'org-adaptive-fill-function)
  14166. (org-set-local
  14167. 'align-mode-rules-list
  14168. '((org-in-buffer-settings
  14169. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14170. (modes . '(org-mode))))))
  14171. (defun org-fill-paragraph (&optional justify)
  14172. "Re-align a table, pass through to fill-paragraph if no table."
  14173. (let ((table-p (org-at-table-p))
  14174. (table.el-p (org-at-table.el-p)))
  14175. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14176. (save-excursion (goto-char (point-at-bol))
  14177. (looking-at outline-regexp)))
  14178. t) ; skip headlines
  14179. (table.el-p t) ; skip table.el tables
  14180. (table-p (org-table-align) t) ; align org-mode tables
  14181. (t nil)))) ; call paragraph-fill
  14182. ;; For reference, this is the default value of adaptive-fill-regexp
  14183. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14184. (defun org-adaptive-fill-function ()
  14185. "Return a fill prefix for org-mode files.
  14186. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14187. work correctly."
  14188. (cond ((looking-at "#[ \t]+")
  14189. (match-string 0))
  14190. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14191. (save-excursion
  14192. (if (> (match-end 1) (+ (match-beginning 1)
  14193. org-description-max-indent))
  14194. (goto-char (+ (match-beginning 1) 5))
  14195. (goto-char (match-end 0)))
  14196. (make-string (current-column) ?\ )))
  14197. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14198. (save-excursion
  14199. (goto-char (match-end 0))
  14200. (make-string (current-column) ?\ )))
  14201. (t nil)))
  14202. ;;; Other stuff.
  14203. (defun org-toggle-fixed-width-section (arg)
  14204. "Toggle the fixed-width export.
  14205. If there is no active region, the QUOTE keyword at the current headline is
  14206. inserted or removed. When present, it causes the text between this headline
  14207. and the next to be exported as fixed-width text, and unmodified.
  14208. If there is an active region, this command adds or removes a colon as the
  14209. first character of this line. If the first character of a line is a colon,
  14210. this line is also exported in fixed-width font."
  14211. (interactive "P")
  14212. (let* ((cc 0)
  14213. (regionp (org-region-active-p))
  14214. (beg (if regionp (region-beginning) (point)))
  14215. (end (if regionp (region-end)))
  14216. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14217. (case-fold-search nil)
  14218. (re "[ \t]*\\(: \\)")
  14219. off)
  14220. (if regionp
  14221. (save-excursion
  14222. (goto-char beg)
  14223. (setq cc (current-column))
  14224. (beginning-of-line 1)
  14225. (setq off (looking-at re))
  14226. (while (> nlines 0)
  14227. (setq nlines (1- nlines))
  14228. (beginning-of-line 1)
  14229. (cond
  14230. (arg
  14231. (org-move-to-column cc t)
  14232. (insert ": \n")
  14233. (forward-line -1))
  14234. ((and off (looking-at re))
  14235. (replace-match "" t t nil 1))
  14236. ((not off) (org-move-to-column cc t) (insert ": ")))
  14237. (forward-line 1)))
  14238. (save-excursion
  14239. (org-back-to-heading)
  14240. (if (looking-at (concat outline-regexp
  14241. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14242. (replace-match "" t t nil 1)
  14243. (if (looking-at outline-regexp)
  14244. (progn
  14245. (goto-char (match-end 0))
  14246. (insert org-quote-string " "))))))))
  14247. (defun org-reftex-citation ()
  14248. "Use reftex-citation to insert a citation into the buffer.
  14249. This looks for a line like
  14250. #+BIBLIOGRAPHY: foo plain option:-d
  14251. and derives from it that foo.bib is the bbliography file relevant
  14252. for this document. It then installs the necessary environment for RefTeX
  14253. to work in this buffer and calls `reftex-citation' to insert a citation
  14254. into the buffer.
  14255. Export of such citations to both LaTeX and HTML is handled by the contributed
  14256. package org-exp-bibtex by Taru Karttunen."
  14257. (interactive)
  14258. (let ((reftex-docstruct-symbol 'rds)
  14259. (reftex-cite-format "\\cite{%l}")
  14260. rds bib)
  14261. (save-excursion
  14262. (save-restriction
  14263. (widen)
  14264. (let ((case-fold-search t)
  14265. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14266. (if (not (save-excursion
  14267. (or (re-search-forward re nil t)
  14268. (re-search-backward re nil t))))
  14269. (error "No bibliography defined in file")
  14270. (setq bib (concat (match-string 1) ".bib")
  14271. rds (list (list 'bib bib)))))))
  14272. (call-interactively 'reftex-citation)))
  14273. ;;;; Functions extending outline functionality
  14274. (defun org-beginning-of-line (&optional arg)
  14275. "Go to the beginning of the current line. If that is invisible, continue
  14276. to a visible line beginning. This makes the function of C-a more intuitive.
  14277. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14278. first attempt, and only move to after the tags when the cursor is already
  14279. beyond the end of the headline."
  14280. (interactive "P")
  14281. (let ((pos (point))
  14282. (special (if (consp org-special-ctrl-a/e)
  14283. (car org-special-ctrl-a/e)
  14284. org-special-ctrl-a/e))
  14285. refpos)
  14286. (beginning-of-line 1)
  14287. (if (and arg (fboundp 'move-beginning-of-line))
  14288. (call-interactively 'move-beginning-of-line)
  14289. (if (bobp)
  14290. nil
  14291. (backward-char 1)
  14292. (if (org-invisible-p)
  14293. (while (and (not (bobp)) (org-invisible-p))
  14294. (backward-char 1)
  14295. (beginning-of-line 1))
  14296. (forward-char 1))))
  14297. (when special
  14298. (cond
  14299. ((and (looking-at org-complex-heading-regexp)
  14300. (= (char-after (match-end 1)) ?\ ))
  14301. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14302. (point-at-eol)))
  14303. (goto-char
  14304. (if (eq special t)
  14305. (cond ((> pos refpos) refpos)
  14306. ((= pos (point)) refpos)
  14307. (t (point)))
  14308. (cond ((> pos (point)) (point))
  14309. ((not (eq last-command this-command)) (point))
  14310. (t refpos)))))
  14311. ((org-at-item-p)
  14312. (goto-char
  14313. (if (eq special t)
  14314. (cond ((> pos (match-end 4)) (match-end 4))
  14315. ((= pos (point)) (match-end 4))
  14316. (t (point)))
  14317. (cond ((> pos (point)) (point))
  14318. ((not (eq last-command this-command)) (point))
  14319. (t (match-end 4))))))))
  14320. (org-no-warnings
  14321. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14322. (defun org-end-of-line (&optional arg)
  14323. "Go to the end of the line.
  14324. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14325. first attempt, and only move to after the tags when the cursor is already
  14326. beyond the end of the headline."
  14327. (interactive "P")
  14328. (let ((special (if (consp org-special-ctrl-a/e)
  14329. (cdr org-special-ctrl-a/e)
  14330. org-special-ctrl-a/e)))
  14331. (if (or (not special)
  14332. (not (org-on-heading-p))
  14333. arg)
  14334. (call-interactively (if (fboundp 'move-end-of-line)
  14335. 'move-end-of-line
  14336. 'end-of-line))
  14337. (let ((pos (point)))
  14338. (beginning-of-line 1)
  14339. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14340. (if (eq special t)
  14341. (if (or (< pos (match-beginning 1))
  14342. (= pos (match-end 0)))
  14343. (goto-char (match-beginning 1))
  14344. (goto-char (match-end 0)))
  14345. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14346. (goto-char (match-end 0))
  14347. (goto-char (match-beginning 1))))
  14348. (call-interactively (if (fboundp 'move-end-of-line)
  14349. 'move-end-of-line
  14350. 'end-of-line)))))
  14351. (org-no-warnings
  14352. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14353. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14354. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14355. (defun org-backward-sentence (&optional arg)
  14356. "Go to beginning of sentence, or beginning of table field.
  14357. This will call `backward-sentence' or `org-table-beginning-of-field',
  14358. depending on context."
  14359. (interactive "P")
  14360. (cond
  14361. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14362. (t (call-interactively 'backward-sentence))))
  14363. (defun org-forward-sentence (&optional arg)
  14364. "Go to end of sentence, or end of table field.
  14365. This will call `forward-sentence' or `org-table-end-of-field',
  14366. depending on context."
  14367. (interactive "P")
  14368. (cond
  14369. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14370. (t (call-interactively 'forward-sentence))))
  14371. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14372. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14373. (defun org-kill-line (&optional arg)
  14374. "Kill line, to tags or end of line."
  14375. (interactive "P")
  14376. (cond
  14377. ((or (not org-special-ctrl-k)
  14378. (bolp)
  14379. (not (org-on-heading-p)))
  14380. (call-interactively 'kill-line))
  14381. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14382. (kill-region (point) (match-beginning 1))
  14383. (org-set-tags nil t))
  14384. (t (kill-region (point) (point-at-eol)))))
  14385. (define-key org-mode-map "\C-k" 'org-kill-line)
  14386. (defun org-yank (&optional arg)
  14387. "Yank. If the kill is a subtree, treat it specially.
  14388. This command will look at the current kill and check if is a single
  14389. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14390. cursor is at the beginning of a line or after the stars of a currently
  14391. empty headline, then the yank is handled specially. How exactly depends
  14392. on the value of the following variables, both set by default.
  14393. org-yank-folded-subtrees
  14394. When set, the subtree(s) will be folded after insertion, but only
  14395. if doing so would now swallow text after the yanked text.
  14396. org-yank-adjusted-subtrees
  14397. When set, the subtree will be promoted or demoted in order to
  14398. fit into the local outline tree structure, which means that the level
  14399. will be adjusted so that it becomes the smaller one of the two
  14400. *visible* surrounding headings.
  14401. Any prefix to this command will cause `yank' to be called directly with
  14402. no special treatment. In particular, a simple `C-u' prefix will just
  14403. plainly yank the text as it is.
  14404. \[1] Basically, the test checks if the first non-white line is a heading
  14405. and if there are no other headings with fewer stars."
  14406. (interactive "P")
  14407. (setq this-command 'yank)
  14408. (if arg
  14409. (call-interactively 'yank)
  14410. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14411. (and (org-kill-is-subtree-p)
  14412. (or (bolp)
  14413. (and (looking-at "[ \t]*$")
  14414. (string-match
  14415. "\\`\\*+\\'"
  14416. (buffer-substring (point-at-bol) (point)))))))
  14417. swallowp)
  14418. (cond
  14419. ((and subtreep org-yank-folded-subtrees)
  14420. (let ((beg (point))
  14421. end)
  14422. (if (and subtreep org-yank-adjusted-subtrees)
  14423. (org-paste-subtree nil nil 'for-yank)
  14424. (call-interactively 'yank))
  14425. (setq end (point))
  14426. (goto-char beg)
  14427. (when (and (bolp) subtreep
  14428. (not (setq swallowp
  14429. (org-yank-folding-would-swallow-text beg end))))
  14430. (or (looking-at outline-regexp)
  14431. (re-search-forward (concat "^" outline-regexp) end t))
  14432. (while (and (< (point) end) (looking-at outline-regexp))
  14433. (hide-subtree)
  14434. (org-cycle-show-empty-lines 'folded)
  14435. (condition-case nil
  14436. (outline-forward-same-level 1)
  14437. (error (goto-char end)))))
  14438. (when swallowp
  14439. (message
  14440. "Yanked text not folded because that would swallow text"))
  14441. (goto-char end)
  14442. (skip-chars-forward " \t\n\r")
  14443. (beginning-of-line 1)
  14444. (push-mark beg 'nomsg)))
  14445. ((and subtreep org-yank-adjusted-subtrees)
  14446. (let ((beg (point-at-bol)))
  14447. (org-paste-subtree nil nil 'for-yank)
  14448. (push-mark beg 'nomsg)))
  14449. (t
  14450. (call-interactively 'yank))))))
  14451. (defun org-yank-folding-would-swallow-text (beg end)
  14452. "Would hide-subtree at BEG swallow any text after END?"
  14453. (let (level)
  14454. (save-excursion
  14455. (goto-char beg)
  14456. (when (or (looking-at outline-regexp)
  14457. (re-search-forward (concat "^" outline-regexp) end t))
  14458. (setq level (org-outline-level)))
  14459. (goto-char end)
  14460. (skip-chars-forward " \t\r\n\v\f")
  14461. (if (or (eobp)
  14462. (and (bolp) (looking-at org-outline-regexp)
  14463. (<= (org-outline-level) level)))
  14464. nil ; Nothing would be swallowed
  14465. t)))) ; something would swallow
  14466. (define-key org-mode-map "\C-y" 'org-yank)
  14467. (defun org-invisible-p ()
  14468. "Check if point is at a character currently not visible."
  14469. ;; Early versions of noutline don't have `outline-invisible-p'.
  14470. (if (fboundp 'outline-invisible-p)
  14471. (outline-invisible-p)
  14472. (get-char-property (point) 'invisible)))
  14473. (defun org-invisible-p2 ()
  14474. "Check if point is at a character currently not visible."
  14475. (save-excursion
  14476. (if (and (eolp) (not (bobp))) (backward-char 1))
  14477. ;; Early versions of noutline don't have `outline-invisible-p'.
  14478. (if (fboundp 'outline-invisible-p)
  14479. (outline-invisible-p)
  14480. (get-char-property (point) 'invisible))))
  14481. (defun org-back-to-heading (&optional invisible-ok)
  14482. "Call `outline-back-to-heading', but provide a better error message."
  14483. (condition-case nil
  14484. (outline-back-to-heading invisible-ok)
  14485. (error (error "Before first headline at position %d in buffer %s"
  14486. (point) (current-buffer)))))
  14487. (defun org-before-first-heading-p ()
  14488. "Before first heading?"
  14489. (save-excursion
  14490. (null (re-search-backward "^\\*+ " nil t))))
  14491. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14492. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14493. (defun org-at-heading-or-item-p ()
  14494. (or (org-on-heading-p) (org-at-item-p)))
  14495. (defun org-on-target-p ()
  14496. (or (org-in-regexp org-radio-target-regexp)
  14497. (org-in-regexp org-target-regexp)))
  14498. (defun org-up-heading-all (arg)
  14499. "Move to the heading line of which the present line is a subheading.
  14500. This function considers both visible and invisible heading lines.
  14501. With argument, move up ARG levels."
  14502. (if (fboundp 'outline-up-heading-all)
  14503. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14504. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14505. (defun org-up-heading-safe ()
  14506. "Move to the heading line of which the present line is a subheading.
  14507. This version will not throw an error. It will return the level of the
  14508. headline found, or nil if no higher level is found.
  14509. Also, this function will be a lot faster than `outline-up-heading',
  14510. because it relies on stars being the outline starters. This can really
  14511. make a significant difference in outlines with very many siblings."
  14512. (let (start-level re)
  14513. (org-back-to-heading t)
  14514. (setq start-level (funcall outline-level))
  14515. (if (equal start-level 1)
  14516. nil
  14517. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14518. (if (re-search-backward re nil t)
  14519. (funcall outline-level)))))
  14520. (defun org-first-sibling-p ()
  14521. "Is this heading the first child of its parents?"
  14522. (interactive)
  14523. (let ((re (concat "^" outline-regexp))
  14524. level l)
  14525. (unless (org-at-heading-p t)
  14526. (error "Not at a heading"))
  14527. (setq level (funcall outline-level))
  14528. (save-excursion
  14529. (if (not (re-search-backward re nil t))
  14530. t
  14531. (setq l (funcall outline-level))
  14532. (< l level)))))
  14533. (defun org-goto-sibling (&optional previous)
  14534. "Goto the next sibling, even if it is invisible.
  14535. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14536. when a sibling was found. When none is found, return nil and don't
  14537. move point."
  14538. (let ((fun (if previous 're-search-backward 're-search-forward))
  14539. (pos (point))
  14540. (re (concat "^" outline-regexp))
  14541. level l)
  14542. (when (condition-case nil (org-back-to-heading t) (error nil))
  14543. (setq level (funcall outline-level))
  14544. (catch 'exit
  14545. (or previous (forward-char 1))
  14546. (while (funcall fun re nil t)
  14547. (setq l (funcall outline-level))
  14548. (when (< l level) (goto-char pos) (throw 'exit nil))
  14549. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14550. (goto-char pos)
  14551. nil))))
  14552. (defun org-show-siblings ()
  14553. "Show all siblings of the current headline."
  14554. (save-excursion
  14555. (while (org-goto-sibling) (org-flag-heading nil)))
  14556. (save-excursion
  14557. (while (org-goto-sibling 'previous)
  14558. (org-flag-heading nil))))
  14559. (defun org-show-hidden-entry ()
  14560. "Show an entry where even the heading is hidden."
  14561. (save-excursion
  14562. (org-show-entry)))
  14563. (defun org-flag-heading (flag &optional entry)
  14564. "Flag the current heading. FLAG non-nil means make invisible.
  14565. When ENTRY is non-nil, show the entire entry."
  14566. (save-excursion
  14567. (org-back-to-heading t)
  14568. ;; Check if we should show the entire entry
  14569. (if entry
  14570. (progn
  14571. (org-show-entry)
  14572. (save-excursion
  14573. (and (outline-next-heading)
  14574. (org-flag-heading nil))))
  14575. (outline-flag-region (max (point-min) (1- (point)))
  14576. (save-excursion (outline-end-of-heading) (point))
  14577. flag))))
  14578. (defun org-forward-same-level (arg)
  14579. "Move forward to the ARG'th subheading at same level as this one.
  14580. Stop at the first and last subheadings of a superior heading.
  14581. This is like outline-forward-same-level, but invisible headings are ok."
  14582. (interactive "p")
  14583. (org-back-to-heading t)
  14584. (while (> arg 0)
  14585. (let ((point-to-move-to (save-excursion
  14586. (org-get-next-sibling))))
  14587. (if point-to-move-to
  14588. (progn
  14589. (goto-char point-to-move-to)
  14590. (setq arg (1- arg)))
  14591. (progn
  14592. (setq arg 0)
  14593. (error "No following same-level heading"))))))
  14594. (defun org-get-next-sibling ()
  14595. "Move to next heading of the same level, and return point.
  14596. If there is no such heading, return nil.
  14597. This is like outline-next-sibling, but invisible headings are ok."
  14598. (let ((level (funcall outline-level)))
  14599. (outline-next-heading)
  14600. (while (and (not (eobp)) (> (funcall outline-level) level))
  14601. (outline-next-heading))
  14602. (if (or (eobp) (< (funcall outline-level) level))
  14603. nil
  14604. (point))))
  14605. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14606. ;; This is an exact copy of the original function, but it uses
  14607. ;; `org-back-to-heading', to make it work also in invisible
  14608. ;; trees. And is uses an invisible-OK argument.
  14609. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14610. (org-back-to-heading invisible-OK)
  14611. (let ((first t)
  14612. (level (funcall outline-level)))
  14613. (while (and (not (eobp))
  14614. (or first (> (funcall outline-level) level)))
  14615. (setq first nil)
  14616. (outline-next-heading))
  14617. (unless to-heading
  14618. (if (memq (preceding-char) '(?\n ?\^M))
  14619. (progn
  14620. ;; Go to end of line before heading
  14621. (forward-char -1)
  14622. (if (memq (preceding-char) '(?\n ?\^M))
  14623. ;; leave blank line before heading
  14624. (forward-char -1))))))
  14625. (point))
  14626. (defun org-show-subtree ()
  14627. "Show everything after this heading at deeper levels."
  14628. (outline-flag-region
  14629. (point)
  14630. (save-excursion
  14631. (outline-end-of-subtree) (outline-next-heading) (point))
  14632. nil))
  14633. (defun org-show-entry ()
  14634. "Show the body directly following this heading.
  14635. Show the heading too, if it is currently invisible."
  14636. (interactive)
  14637. (save-excursion
  14638. (condition-case nil
  14639. (progn
  14640. (org-back-to-heading t)
  14641. (outline-flag-region
  14642. (max (point-min) (1- (point)))
  14643. (save-excursion
  14644. (if (re-search-forward
  14645. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14646. (match-beginning 1)
  14647. (point-max)))
  14648. nil))
  14649. (error nil))))
  14650. (defun org-make-options-regexp (kwds &optional extra)
  14651. "Make a regular expression for keyword lines."
  14652. (concat
  14653. "^"
  14654. "#?[ \t]*\\+\\("
  14655. (mapconcat 'regexp-quote kwds "\\|")
  14656. (if extra (concat "\\|" extra))
  14657. "\\):[ \t]*"
  14658. "\\(.+\\)"))
  14659. ;; Make isearch reveal the necessary context
  14660. (defun org-isearch-end ()
  14661. "Reveal context after isearch exits."
  14662. (when isearch-success ; only if search was successful
  14663. (if (featurep 'xemacs)
  14664. ;; Under XEmacs, the hook is run in the correct place,
  14665. ;; we directly show the context.
  14666. (org-show-context 'isearch)
  14667. ;; In Emacs the hook runs *before* restoring the overlays.
  14668. ;; So we have to use a one-time post-command-hook to do this.
  14669. ;; (Emacs 22 has a special variable, see function `org-mode')
  14670. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14671. isearch-mode-end-hook-quit)
  14672. ;; Only when the isearch was not quitted.
  14673. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14674. 'append 'local)))))
  14675. (defun org-isearch-post-command ()
  14676. "Remove self from hook, and show context."
  14677. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14678. (org-show-context 'isearch))
  14679. ;;;; Integration with and fixes for other packages
  14680. ;;; Imenu support
  14681. (defvar org-imenu-markers nil
  14682. "All markers currently used by Imenu.")
  14683. (make-variable-buffer-local 'org-imenu-markers)
  14684. (defun org-imenu-new-marker (&optional pos)
  14685. "Return a new marker for use by Imenu, and remember the marker."
  14686. (let ((m (make-marker)))
  14687. (move-marker m (or pos (point)))
  14688. (push m org-imenu-markers)
  14689. m))
  14690. (defun org-imenu-get-tree ()
  14691. "Produce the index for Imenu."
  14692. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14693. (setq org-imenu-markers nil)
  14694. (let* ((n org-imenu-depth)
  14695. (re (concat "^" outline-regexp))
  14696. (subs (make-vector (1+ n) nil))
  14697. (last-level 0)
  14698. m level head)
  14699. (save-excursion
  14700. (save-restriction
  14701. (widen)
  14702. (goto-char (point-max))
  14703. (while (re-search-backward re nil t)
  14704. (setq level (org-reduced-level (funcall outline-level)))
  14705. (when (<= level n)
  14706. (looking-at org-complex-heading-regexp)
  14707. (setq head (org-link-display-format
  14708. (org-match-string-no-properties 4))
  14709. m (org-imenu-new-marker))
  14710. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14711. (if (>= level last-level)
  14712. (push (cons head m) (aref subs level))
  14713. (push (cons head (aref subs (1+ level))) (aref subs level))
  14714. (loop for i from (1+ level) to n do (aset subs i nil)))
  14715. (setq last-level level)))))
  14716. (aref subs 1)))
  14717. (eval-after-load "imenu"
  14718. '(progn
  14719. (add-hook 'imenu-after-jump-hook
  14720. (lambda ()
  14721. (if (eq major-mode 'org-mode)
  14722. (org-show-context 'org-goto))))))
  14723. (defun org-link-display-format (link)
  14724. "Replace a link with either the description, or the link target
  14725. if no description is present"
  14726. (save-match-data
  14727. (if (string-match org-bracket-link-analytic-regexp link)
  14728. (replace-match (or (match-string 5 link)
  14729. (concat (match-string 1 link)
  14730. (match-string 3 link)))
  14731. nil nil link)
  14732. link)))
  14733. ;; Speedbar support
  14734. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14735. "Overlay marking the agenda restriction line in speedbar.")
  14736. (org-overlay-put org-speedbar-restriction-lock-overlay
  14737. 'face 'org-agenda-restriction-lock)
  14738. (org-overlay-put org-speedbar-restriction-lock-overlay
  14739. 'help-echo "Agendas are currently limited to this item.")
  14740. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14741. (defun org-speedbar-set-agenda-restriction ()
  14742. "Restrict future agenda commands to the location at point in speedbar.
  14743. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14744. (interactive)
  14745. (require 'org-agenda)
  14746. (let (p m tp np dir txt)
  14747. (cond
  14748. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14749. 'org-imenu t))
  14750. (setq m (get-text-property p 'org-imenu-marker))
  14751. (save-excursion
  14752. (save-restriction
  14753. (set-buffer (marker-buffer m))
  14754. (goto-char m)
  14755. (org-agenda-set-restriction-lock 'subtree))))
  14756. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14757. 'speedbar-function 'speedbar-find-file))
  14758. (setq tp (previous-single-property-change
  14759. (1+ p) 'speedbar-function)
  14760. np (next-single-property-change
  14761. tp 'speedbar-function)
  14762. dir (speedbar-line-directory)
  14763. txt (buffer-substring-no-properties (or tp (point-min))
  14764. (or np (point-max))))
  14765. (save-excursion
  14766. (save-restriction
  14767. (set-buffer (find-file-noselect
  14768. (let ((default-directory dir))
  14769. (expand-file-name txt))))
  14770. (unless (org-mode-p)
  14771. (error "Cannot restrict to non-Org-mode file"))
  14772. (org-agenda-set-restriction-lock 'file))))
  14773. (t (error "Don't know how to restrict Org-mode's agenda")))
  14774. (org-move-overlay org-speedbar-restriction-lock-overlay
  14775. (point-at-bol) (point-at-eol))
  14776. (setq current-prefix-arg nil)
  14777. (org-agenda-maybe-redo)))
  14778. (eval-after-load "speedbar"
  14779. '(progn
  14780. (speedbar-add-supported-extension ".org")
  14781. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14782. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14783. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14784. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14785. (add-hook 'speedbar-visiting-tag-hook
  14786. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14787. ;;; Fixes and Hacks for problems with other packages
  14788. ;; Make flyspell not check words in links, to not mess up our keymap
  14789. (defun org-mode-flyspell-verify ()
  14790. "Don't let flyspell put overlays at active buttons."
  14791. (not (get-text-property (point) 'keymap)))
  14792. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14793. (eval-after-load "bookmark"
  14794. '(if (boundp 'bookmark-after-jump-hook)
  14795. ;; We can use the hook
  14796. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14797. ;; Hook not available, use advice
  14798. (defadvice bookmark-jump (after org-make-visible activate)
  14799. "Make the position visible."
  14800. (org-bookmark-jump-unhide))))
  14801. ;; Make sure saveplace show the location if it was hidden
  14802. (eval-after-load "saveplace"
  14803. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14804. "Make the position visible."
  14805. (org-bookmark-jump-unhide)))
  14806. (defun org-bookmark-jump-unhide ()
  14807. "Unhide the current position, to show the bookmark location."
  14808. (and (org-mode-p)
  14809. (or (org-invisible-p)
  14810. (save-excursion (goto-char (max (point-min) (1- (point))))
  14811. (org-invisible-p)))
  14812. (org-show-context 'bookmark-jump)))
  14813. ;; Make session.el ignore our circular variable
  14814. (eval-after-load "session"
  14815. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14816. ;;;; Experimental code
  14817. (defun org-closed-in-range ()
  14818. "Sparse tree of items closed in a certain time range.
  14819. Still experimental, may disappear in the future."
  14820. (interactive)
  14821. ;; Get the time interval from the user.
  14822. (let* ((time1 (time-to-seconds
  14823. (org-read-date nil 'to-time nil "Starting date: ")))
  14824. (time2 (time-to-seconds
  14825. (org-read-date nil 'to-time nil "End date:")))
  14826. ;; callback function
  14827. (callback (lambda ()
  14828. (let ((time
  14829. (time-to-seconds
  14830. (apply 'encode-time
  14831. (org-parse-time-string
  14832. (match-string 1))))))
  14833. ;; check if time in interval
  14834. (and (>= time time1) (<= time time2))))))
  14835. ;; make tree, check each match with the callback
  14836. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14837. ;;;; Finish up
  14838. (provide 'org)
  14839. (run-hooks 'org-load-hook)
  14840. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14841. ;;; org.el ends here