org.el 557 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002
  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 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.18c
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. (require 'org-list)
  84. (require 'org-footnote)
  85. ;;;; Customization variables
  86. ;;; Version
  87. (defconst org-version "6.18c"
  88. "The version number of the file org.el.")
  89. (defun org-version (&optional here)
  90. "Show the org-mode version in the echo area.
  91. With prefix arg HERE, insert it at point."
  92. (interactive "P")
  93. (let ((version (format "Org-mode version %s" org-version)))
  94. (message version)
  95. (if here
  96. (insert version))))
  97. ;;; Compatibility constants
  98. ;;; The custom variables
  99. (defgroup org nil
  100. "Outline-based notes management and organizer."
  101. :tag "Org"
  102. :group 'outlines
  103. :group 'hypermedia
  104. :group 'calendar)
  105. (defcustom org-load-hook nil
  106. "Hook that is run after org.el has been loaded."
  107. :group 'org
  108. :type 'hook)
  109. (defvar org-modules) ; defined below
  110. (defvar org-modules-loaded nil
  111. "Have the modules been loaded already?")
  112. (defun org-load-modules-maybe (&optional force)
  113. "Load all extensions listed in `org-modules'."
  114. (when (or force (not org-modules-loaded))
  115. (mapc (lambda (ext)
  116. (condition-case nil (require ext)
  117. (error (message "Problems while trying to load feature `%s'" ext))))
  118. org-modules)
  119. (setq org-modules-loaded t)))
  120. (defun org-set-modules (var value)
  121. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  122. (set var value)
  123. (when (featurep 'org)
  124. (org-load-modules-maybe 'force)))
  125. (when (org-bound-and-true-p org-modules)
  126. (let ((a (member 'org-infojs org-modules)))
  127. (and a (setcar a 'org-jsinfo))))
  128. (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)
  129. "Modules that should always be loaded together with org.el.
  130. If a description starts with <C>, the file is not part of Emacs
  131. and loading it will require that you have downloaded and properly installed
  132. the org-mode distribution.
  133. You can also use this system to load external packages (i.e. neither Org
  134. core modules, not modules from the CONTRIB directory). Just add symbols
  135. to the end of the list. If the package is called org-xyz.el, then you need
  136. to add the symbol `xyz', and the package must have a call to
  137. (provide 'org-xyz)"
  138. :group 'org
  139. :set 'org-set-modules
  140. :type
  141. '(set :greedy t
  142. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  143. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  144. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  145. (const :tag " id: Global IDs for identifying entries" org-id)
  146. (const :tag " info: Links to Info nodes" org-info)
  147. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  148. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  149. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  150. (const :tag " mew Links to Mew folders/messages" org-mew)
  151. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  152. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  153. (const :tag " vm: Links to VM folders/messages" org-vm)
  154. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  155. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  156. (const :tag " mouse: Additional mouse support" org-mouse)
  157. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  158. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  159. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  160. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  161. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  162. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  163. (const :tag "C eval: Include command output as text" org-eval)
  164. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  165. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  166. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  167. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  168. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  169. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  170. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  171. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  172. (const :tag "C registry: A registry for Org links" org-registry)
  173. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  174. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  175. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  176. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  177. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  178. (defgroup org-startup nil
  179. "Options concerning startup of Org-mode."
  180. :tag "Org Startup"
  181. :group 'org)
  182. (defcustom org-startup-folded t
  183. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  184. This can also be configured on a per-file basis by adding one of
  185. the following lines anywhere in the buffer:
  186. #+STARTUP: fold
  187. #+STARTUP: nofold
  188. #+STARTUP: content"
  189. :group 'org-startup
  190. :type '(choice
  191. (const :tag "nofold: show all" nil)
  192. (const :tag "fold: overview" t)
  193. (const :tag "content: all headlines" content)))
  194. (defcustom org-startup-truncated t
  195. "Non-nil means, entering Org-mode will set `truncate-lines'.
  196. This is useful since some lines containing links can be very long and
  197. uninteresting. Also tables look terrible when wrapped."
  198. :group 'org-startup
  199. :type 'boolean)
  200. (defcustom org-startup-align-all-tables nil
  201. "Non-nil means, align all tables when visiting a file.
  202. This is useful when the column width in tables is forced with <N> cookies
  203. in table fields. Such tables will look correct only after the first re-align.
  204. This can also be configured on a per-file basis by adding one of
  205. the following lines anywhere in the buffer:
  206. #+STARTUP: align
  207. #+STARTUP: noalign"
  208. :group 'org-startup
  209. :type 'boolean)
  210. (defcustom org-insert-mode-line-in-empty-file nil
  211. "Non-nil means insert the first line setting Org-mode in empty files.
  212. When the function `org-mode' is called interactively in an empty file, this
  213. normally means that the file name does not automatically trigger Org-mode.
  214. To ensure that the file will always be in Org-mode in the future, a
  215. line enforcing Org-mode will be inserted into the buffer, if this option
  216. has been set."
  217. :group 'org-startup
  218. :type 'boolean)
  219. (defcustom org-replace-disputed-keys nil
  220. "Non-nil means use alternative key bindings for some keys.
  221. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  222. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  223. If you want to use Org-mode together with one of these other modes,
  224. or more generally if you would like to move some Org-mode commands to
  225. other keys, set this variable and configure the keys with the variable
  226. `org-disputed-keys'.
  227. This option is only relevant at load-time of Org-mode, and must be set
  228. *before* org.el is loaded. Changing it requires a restart of Emacs to
  229. become effective."
  230. :group 'org-startup
  231. :type 'boolean)
  232. (defcustom org-use-extra-keys nil
  233. "Non-nil means use extra key sequence definitions for certain
  234. commands. This happens automatically if you run XEmacs or if
  235. window-system is nil. This variable lets you do the same
  236. manually. You must set it before loading org.
  237. Example: on Carbon Emacs 22 running graphically, with an external
  238. keyboard on a Powerbook, the default way of setting M-left might
  239. not work for either Alt or ESC. Setting this variable will make
  240. it work for ESC."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (if (fboundp 'defvaralias)
  244. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  245. (defcustom org-disputed-keys
  246. '(([(shift up)] . [(meta p)])
  247. ([(shift down)] . [(meta n)])
  248. ([(shift left)] . [(meta -)])
  249. ([(shift right)] . [(meta +)])
  250. ([(control shift right)] . [(meta shift +)])
  251. ([(control shift left)] . [(meta shift -)]))
  252. "Keys for which Org-mode and other modes compete.
  253. This is an alist, cars are the default keys, second element specifies
  254. the alternative to use when `org-replace-disputed-keys' is t.
  255. Keys can be specified in any syntax supported by `define-key'.
  256. The value of this option takes effect only at Org-mode's startup,
  257. therefore you'll have to restart Emacs to apply it after changing."
  258. :group 'org-startup
  259. :type 'alist)
  260. (defun org-key (key)
  261. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  262. Or return the original if not disputed."
  263. (if org-replace-disputed-keys
  264. (let* ((nkey (key-description key))
  265. (x (org-find-if (lambda (x)
  266. (equal (key-description (car x)) nkey))
  267. org-disputed-keys)))
  268. (if x (cdr x) key))
  269. key))
  270. (defun org-find-if (predicate seq)
  271. (catch 'exit
  272. (while seq
  273. (if (funcall predicate (car seq))
  274. (throw 'exit (car seq))
  275. (pop seq)))))
  276. (defun org-defkey (keymap key def)
  277. "Define a key, possibly translated, as returned by `org-key'."
  278. (define-key keymap (org-key key) def))
  279. (defcustom org-ellipsis nil
  280. "The ellipsis to use in the Org-mode outline.
  281. When nil, just use the standard three dots. When a string, use that instead,
  282. When a face, use the standard 3 dots, but with the specified face.
  283. The change affects only Org-mode (which will then use its own display table).
  284. Changing this requires executing `M-x org-mode' in a buffer to become
  285. effective."
  286. :group 'org-startup
  287. :type '(choice (const :tag "Default" nil)
  288. (face :tag "Face" :value org-warning)
  289. (string :tag "String" :value "...#")))
  290. (defvar org-display-table nil
  291. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  292. (defgroup org-keywords nil
  293. "Keywords in Org-mode."
  294. :tag "Org Keywords"
  295. :group 'org)
  296. (defcustom org-deadline-string "DEADLINE:"
  297. "String to mark deadline entries.
  298. A deadline is this string, followed by a time stamp. Should be a word,
  299. terminated by a colon. You can insert a schedule keyword and
  300. a timestamp with \\[org-deadline].
  301. Changes become only effective after restarting Emacs."
  302. :group 'org-keywords
  303. :type 'string)
  304. (defcustom org-scheduled-string "SCHEDULED:"
  305. "String to mark scheduled TODO entries.
  306. A schedule is this string, followed by a time stamp. Should be a word,
  307. terminated by a colon. You can insert a schedule keyword and
  308. a timestamp with \\[org-schedule].
  309. Changes become only effective after restarting Emacs."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defcustom org-closed-string "CLOSED:"
  313. "String used as the prefix for timestamps logging closing a TODO entry."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-clock-string "CLOCK:"
  317. "String used as prefix for timestamps clocking work hours on an item."
  318. :group 'org-keywords
  319. :type 'string)
  320. (defcustom org-comment-string "COMMENT"
  321. "Entries starting with this keyword will never be exported.
  322. An entry can be toggled between COMMENT and normal with
  323. \\[org-toggle-comment].
  324. Changes become only effective after restarting Emacs."
  325. :group 'org-keywords
  326. :type 'string)
  327. (defcustom org-quote-string "QUOTE"
  328. "Entries starting with this keyword will be exported in fixed-width font.
  329. Quoting applies only to the text in the entry following the headline, and does
  330. not extend beyond the next headline, even if that is lower level.
  331. An entry can be toggled between QUOTE and normal with
  332. \\[org-toggle-fixed-width-section]."
  333. :group 'org-keywords
  334. :type 'string)
  335. (defconst org-repeat-re
  336. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  337. "Regular expression for specifying repeated events.
  338. After a match, group 1 contains the repeat expression.")
  339. (defgroup org-structure nil
  340. "Options concerning the general structure of Org-mode files."
  341. :tag "Org Structure"
  342. :group 'org)
  343. (defgroup org-reveal-location nil
  344. "Options about how to make context of a location visible."
  345. :tag "Org Reveal Location"
  346. :group 'org-structure)
  347. (defconst org-context-choice
  348. '(choice
  349. (const :tag "Always" t)
  350. (const :tag "Never" nil)
  351. (repeat :greedy t :tag "Individual contexts"
  352. (cons
  353. (choice :tag "Context"
  354. (const agenda)
  355. (const org-goto)
  356. (const occur-tree)
  357. (const tags-tree)
  358. (const link-search)
  359. (const mark-goto)
  360. (const bookmark-jump)
  361. (const isearch)
  362. (const default))
  363. (boolean))))
  364. "Contexts for the reveal options.")
  365. (defcustom org-show-hierarchy-above '((default . t))
  366. "Non-nil means, show full hierarchy when revealing a location.
  367. Org-mode often shows locations in an org-mode file which might have
  368. been invisible before. When this is set, the hierarchy of headings
  369. above the exposed location is shown.
  370. Turning this off for example for sparse trees makes them very compact.
  371. Instead of t, this can also be an alist specifying this option for different
  372. contexts. Valid contexts are
  373. agenda when exposing an entry from the agenda
  374. org-goto when using the command `org-goto' on key C-c C-j
  375. occur-tree when using the command `org-occur' on key C-c /
  376. tags-tree when constructing a sparse tree based on tags matches
  377. link-search when exposing search matches associated with a link
  378. mark-goto when exposing the jump goal of a mark
  379. bookmark-jump when exposing a bookmark location
  380. isearch when exiting from an incremental search
  381. default default for all contexts not set explicitly"
  382. :group 'org-reveal-location
  383. :type org-context-choice)
  384. (defcustom org-show-following-heading '((default . nil))
  385. "Non-nil means, show following heading when revealing a location.
  386. Org-mode often shows locations in an org-mode file which might have
  387. been invisible before. When this is set, the heading following the
  388. match is shown.
  389. Turning this off for example for sparse trees makes them very compact,
  390. but makes it harder to edit the location of the match. In such a case,
  391. use the command \\[org-reveal] to show more context.
  392. Instead of t, this can also be an alist specifying this option for different
  393. contexts. See `org-show-hierarchy-above' for valid contexts."
  394. :group 'org-reveal-location
  395. :type org-context-choice)
  396. (defcustom org-show-siblings '((default . nil) (isearch t))
  397. "Non-nil means, show all sibling heading when revealing a location.
  398. Org-mode often shows locations in an org-mode file which might have
  399. been invisible before. When this is set, the sibling of the current entry
  400. heading are all made visible. If `org-show-hierarchy-above' is t,
  401. the same happens on each level of the hierarchy above the current entry.
  402. By default this is on for the isearch context, off for all other contexts.
  403. Turning this off for example for sparse trees makes them very compact,
  404. but makes it harder to edit the location of the match. In such a case,
  405. use the command \\[org-reveal] to show more context.
  406. Instead of t, this can also be an alist specifying this option for different
  407. contexts. See `org-show-hierarchy-above' for valid contexts."
  408. :group 'org-reveal-location
  409. :type org-context-choice)
  410. (defcustom org-show-entry-below '((default . nil))
  411. "Non-nil means, show the entry below a headline when revealing a location.
  412. Org-mode often shows locations in an org-mode file which might have
  413. been invisible before. When this is set, the text below the headline that is
  414. exposed is also shown.
  415. By default this is off for all contexts.
  416. Instead of t, this can also be an alist specifying this option for different
  417. contexts. See `org-show-hierarchy-above' for valid contexts."
  418. :group 'org-reveal-location
  419. :type org-context-choice)
  420. (defcustom org-indirect-buffer-display 'other-window
  421. "How should indirect tree buffers be displayed?
  422. This applies to indirect buffers created with the commands
  423. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  424. Valid values are:
  425. current-window Display in the current window
  426. other-window Just display in another window.
  427. dedicated-frame Create one new frame, and re-use it each time.
  428. new-frame Make a new frame each time. Note that in this case
  429. previously-made indirect buffers are kept, and you need to
  430. kill these buffers yourself."
  431. :group 'org-structure
  432. :group 'org-agenda-windows
  433. :type '(choice
  434. (const :tag "In current window" current-window)
  435. (const :tag "In current frame, other window" other-window)
  436. (const :tag "Each time a new frame" new-frame)
  437. (const :tag "One dedicated frame" dedicated-frame)))
  438. (defgroup org-cycle nil
  439. "Options concerning visibility cycling in Org-mode."
  440. :tag "Org Cycle"
  441. :group 'org-structure)
  442. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  443. "Names of drawers. Drawers are not opened by cycling on the headline above.
  444. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  445. this:
  446. :DRAWERNAME:
  447. .....
  448. :END:
  449. The drawer \"PROPERTIES\" is special for capturing properties through
  450. the property API.
  451. Drawers can be defined on the per-file basis with a line like:
  452. #+DRAWERS: HIDDEN STATE PROPERTIES"
  453. :group 'org-structure
  454. :type '(repeat (string :tag "Drawer Name")))
  455. (defcustom org-cycle-global-at-bob nil
  456. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  457. This makes it possible to do global cycling without having to use S-TAB or
  458. C-u TAB. For this special case to work, the first line of the buffer
  459. must not be a headline - it may be empty ot some other text. When used in
  460. this way, `org-cycle-hook' is disables temporarily, to make sure the
  461. cursor stays at the beginning of the buffer.
  462. When this option is nil, don't do anything special at the beginning
  463. of the buffer."
  464. :group 'org-cycle
  465. :type 'boolean)
  466. (defcustom org-cycle-emulate-tab t
  467. "Where should `org-cycle' emulate TAB.
  468. nil Never
  469. white Only in completely white lines
  470. whitestart Only at the beginning of lines, before the first non-white char
  471. t Everywhere except in headlines
  472. exc-hl-bol Everywhere except at the start of a headline
  473. If TAB is used in a place where it does not emulate TAB, the current subtree
  474. visibility is cycled."
  475. :group 'org-cycle
  476. :type '(choice (const :tag "Never" nil)
  477. (const :tag "Only in completely white lines" white)
  478. (const :tag "Before first char in a line" whitestart)
  479. (const :tag "Everywhere except in headlines" t)
  480. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  481. ))
  482. (defcustom org-cycle-separator-lines 2
  483. "Number of empty lines needed to keep an empty line between collapsed trees.
  484. If you leave an empty line between the end of a subtree and the following
  485. headline, this empty line is hidden when the subtree is folded.
  486. Org-mode will leave (exactly) one empty line visible if the number of
  487. empty lines is equal or larger to the number given in this variable.
  488. So the default 2 means, at least 2 empty lines after the end of a subtree
  489. are needed to produce free space between a collapsed subtree and the
  490. following headline.
  491. Special case: when 0, never leave empty lines in collapsed view."
  492. :group 'org-cycle
  493. :type 'integer)
  494. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  495. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  496. org-cycle-hide-drawers
  497. org-cycle-show-empty-lines
  498. org-optimize-window-after-visibility-change)
  499. "Hook that is run after `org-cycle' has changed the buffer visibility.
  500. The function(s) in this hook must accept a single argument which indicates
  501. the new state that was set by the most recent `org-cycle' command. The
  502. argument is a symbol. After a global state change, it can have the values
  503. `overview', `content', or `all'. After a local state change, it can have
  504. the values `folded', `children', or `subtree'."
  505. :group 'org-cycle
  506. :type 'hook)
  507. (defgroup org-edit-structure nil
  508. "Options concerning structure editing in Org-mode."
  509. :tag "Org Edit Structure"
  510. :group 'org-structure)
  511. (defcustom org-odd-levels-only nil
  512. "Non-nil means, skip even levels and only use odd levels for the outline.
  513. This has the effect that two stars are being added/taken away in
  514. promotion/demotion commands. It also influences how levels are
  515. handled by the exporters.
  516. Changing it requires restart of `font-lock-mode' to become effective
  517. for fontification also in regions already fontified.
  518. You may also set this on a per-file basis by adding one of the following
  519. lines to the buffer:
  520. #+STARTUP: odd
  521. #+STARTUP: oddeven"
  522. :group 'org-edit-structure
  523. :group 'org-font-lock
  524. :type 'boolean)
  525. (defcustom org-adapt-indentation t
  526. "Non-nil means, adapt indentation when promoting and demoting.
  527. When this is set and the *entire* text in an entry is indented, the
  528. indentation is increased by one space in a demotion command, and
  529. decreased by one in a promotion command. If any line in the entry
  530. body starts at column 0, indentation is not changed at all."
  531. :group 'org-edit-structure
  532. :type 'boolean)
  533. (defcustom org-special-ctrl-a/e nil
  534. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  535. When t, `C-a' will bring back the cursor to the beginning of the
  536. headline text, i.e. after the stars and after a possible TODO keyword.
  537. In an item, this will be the position after the bullet.
  538. When the cursor is already at that position, another `C-a' will bring
  539. it to the beginning of the line.
  540. `C-e' will jump to the end of the headline, ignoring the presence of tags
  541. in the headline. A second `C-e' will then jump to the true end of the
  542. line, after any tags.
  543. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  544. and only a directly following, identical keypress will bring the cursor
  545. to the special positions."
  546. :group 'org-edit-structure
  547. :type '(choice
  548. (const :tag "off" nil)
  549. (const :tag "after bullet first" t)
  550. (const :tag "border first" reversed)))
  551. (if (fboundp 'defvaralias)
  552. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  553. (defcustom org-special-ctrl-k nil
  554. "Non-nil means `C-k' will behave specially in headlines.
  555. When nil, `C-k' will call the default `kill-line' command.
  556. When t, the following will happen while the cursor is in the headline:
  557. - When the cursor is at the beginning of a headline, kill the entire
  558. line and possible the folded subtree below the line.
  559. - When in the middle of the headline text, kill the headline up to the tags.
  560. - When after the headline text, kill the tags."
  561. :group 'org-edit-structure
  562. :type 'boolean)
  563. (defcustom org-yank-folded-subtrees t
  564. "Non-nil means, when yanking subtrees, fold them.
  565. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  566. it starts with a heading and all other headings in it are either children
  567. or siblings, then fold all the subtrees. However, do this only if no
  568. text after the yank would be swallowed into a folded tree by this action."
  569. :group 'org-edit-structure
  570. :type 'boolean)
  571. (defcustom org-yank-adjusted-subtrees t
  572. "Non-nil means, when yanking subtrees, adjust the level.
  573. With this setting, `org-paste-subtree' is used to insert the subtree, see
  574. this function for details."
  575. :group 'org-edit-structure
  576. :type 'boolean)
  577. (defcustom org-M-RET-may-split-line '((default . t))
  578. "Non-nil means, M-RET will split the line at the cursor position.
  579. When nil, it will go to the end of the line before making a
  580. new line.
  581. You may also set this option in a different way for different
  582. contexts. Valid contexts are:
  583. headline when creating a new headline
  584. item when creating a new item
  585. table in a table field
  586. default the value to be used for all contexts not explicitly
  587. customized"
  588. :group 'org-structure
  589. :group 'org-table
  590. :type '(choice
  591. (const :tag "Always" t)
  592. (const :tag "Never" nil)
  593. (repeat :greedy t :tag "Individual contexts"
  594. (cons
  595. (choice :tag "Context"
  596. (const headline)
  597. (const item)
  598. (const table)
  599. (const default))
  600. (boolean)))))
  601. (defcustom org-insert-heading-respect-content nil
  602. "Non-nil means, insert new headings after the current subtree.
  603. When nil, the new heading is created directly after the current line.
  604. The commands \\[org-insert-heading-respect-content] and
  605. \\[org-insert-todo-heading-respect-content] turn this variable on
  606. for the duration of the command."
  607. :group 'org-structure
  608. :type 'boolean)
  609. (defcustom org-blank-before-new-entry '((heading . auto)
  610. (plain-list-item . auto))
  611. "Should `org-insert-heading' leave a blank line before new heading/item?
  612. The value is an alist, with `heading' and `plain-list-item' as car,
  613. and a boolean flag as cdr."
  614. :group 'org-edit-structure
  615. :type '(list
  616. (cons (const heading)
  617. (choice (const :tag "Never" nil)
  618. (const :tag "Always" t)
  619. (const :tag "Auto" auto)))
  620. (cons (const plain-list-item)
  621. (choice (const :tag "Never" nil)
  622. (const :tag "Always" t)
  623. (const :tag "Auto" auto)))))
  624. (defcustom org-insert-heading-hook nil
  625. "Hook being run after inserting a new heading."
  626. :group 'org-edit-structure
  627. :type 'hook)
  628. (defcustom org-enable-fixed-width-editor t
  629. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  630. This currently only means, they are never auto-wrapped.
  631. When nil, such lines will be treated like ordinary lines.
  632. See also the QUOTE keyword."
  633. :group 'org-edit-structure
  634. :type 'boolean)
  635. (defcustom org-edit-src-region-extra nil
  636. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  637. For examples see the function `org-edit-src-find-region-and-lang'.
  638. The regular expression identifying the begin marker should end with a newline,
  639. and the regexp marking the end line should start with a newline, to make sure
  640. there are kept outside the narrowed region."
  641. :group 'org-edit-structure
  642. :type '(repeat
  643. (list
  644. (regexp :tag "begin regexp")
  645. (regexp :tag "end regexp")
  646. (choice :tag "language"
  647. (string :tag "specify")
  648. (integer :tag "from match group")
  649. (const :tag "from `lang' element")
  650. (const :tag "from `style' element")))))
  651. (defcustom org-coderef-label-format "(ref:%s)"
  652. "The default coderef format.
  653. This format string will be used to search for coderef labels in literal
  654. examples (EXAMPLE and SRC blocks). The format can be overwritten
  655. an individual literal example with the -f option, like
  656. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  657. ...
  658. #+END_SRC
  659. If you want to use this for HTML export, make sure that the format does
  660. not introduce special font-locking, and avoid the HTML special
  661. characters `<', `>', and `&'. The reason for this restriction is that
  662. the labels are searched for only after htmlize has done its job."
  663. :group 'org-edit-structure ; FIXME this is not in the right group
  664. :type 'string)
  665. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  666. "The mode that should be used to edit fixed-width regions.
  667. These are the regions where each line starts with a colon."
  668. :group 'org-edit-structure
  669. :type '(choice
  670. (const artist-mode)
  671. (const picture-mode)
  672. (const fundamental-mode)
  673. (function :tag "Other (specify)")))
  674. (defcustom org-goto-auto-isearch t
  675. "Non-nil means, typing characters in org-goto starts incremental search."
  676. :group 'org-edit-structure
  677. :type 'boolean)
  678. (defgroup org-sparse-trees nil
  679. "Options concerning sparse trees in Org-mode."
  680. :tag "Org Sparse Trees"
  681. :group 'org-structure)
  682. (defcustom org-highlight-sparse-tree-matches t
  683. "Non-nil means, highlight all matches that define a sparse tree.
  684. The highlights will automatically disappear the next time the buffer is
  685. changed by an edit command."
  686. :group 'org-sparse-trees
  687. :type 'boolean)
  688. (defcustom org-remove-highlights-with-change t
  689. "Non-nil means, any change to the buffer will remove temporary highlights.
  690. Such highlights are created by `org-occur' and `org-clock-display'.
  691. When nil, `C-c C-c needs to be used to get rid of the highlights.
  692. The highlights created by `org-preview-latex-fragment' always need
  693. `C-c C-c' to be removed."
  694. :group 'org-sparse-trees
  695. :group 'org-time
  696. :type 'boolean)
  697. (defcustom org-occur-hook '(org-first-headline-recenter)
  698. "Hook that is run after `org-occur' has constructed a sparse tree.
  699. This can be used to recenter the window to show as much of the structure
  700. as possible."
  701. :group 'org-sparse-trees
  702. :type 'hook)
  703. (defgroup org-imenu-and-speedbar nil
  704. "Options concerning imenu and speedbar in Org-mode."
  705. :tag "Org Imenu and Speedbar"
  706. :group 'org-structure)
  707. (defcustom org-imenu-depth 2
  708. "The maximum level for Imenu access to Org-mode headlines.
  709. This also applied for speedbar access."
  710. :group 'org-imenu-and-speedbar
  711. :type 'number)
  712. (defgroup org-table nil
  713. "Options concerning tables in Org-mode."
  714. :tag "Org Table"
  715. :group 'org)
  716. (defcustom org-enable-table-editor 'optimized
  717. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  718. When nil, such lines will be treated like ordinary lines.
  719. When equal to the symbol `optimized', the table editor will be optimized to
  720. do the following:
  721. - Automatic overwrite mode in front of whitespace in table fields.
  722. This makes the structure of the table stay in tact as long as the edited
  723. field does not exceed the column width.
  724. - Minimize the number of realigns. Normally, the table is aligned each time
  725. TAB or RET are pressed to move to another field. With optimization this
  726. happens only if changes to a field might have changed the column width.
  727. Optimization requires replacing the functions `self-insert-command',
  728. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  729. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  730. very good at guessing when a re-align will be necessary, but you can always
  731. force one with \\[org-ctrl-c-ctrl-c].
  732. If you would like to use the optimized version in Org-mode, but the
  733. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  734. This variable can be used to turn on and off the table editor during a session,
  735. but in order to toggle optimization, a restart is required.
  736. See also the variable `org-table-auto-blank-field'."
  737. :group 'org-table
  738. :type '(choice
  739. (const :tag "off" nil)
  740. (const :tag "on" t)
  741. (const :tag "on, optimized" optimized)))
  742. (defcustom org-table-tab-recognizes-table.el t
  743. "Non-nil means, TAB will automatically notice a table.el table.
  744. When it sees such a table, it moves point into it and - if necessary -
  745. calls `table-recognize-table'."
  746. :group 'org-table-editing
  747. :type 'boolean)
  748. (defgroup org-link nil
  749. "Options concerning links in Org-mode."
  750. :tag "Org Link"
  751. :group 'org)
  752. (defvar org-link-abbrev-alist-local nil
  753. "Buffer-local version of `org-link-abbrev-alist', which see.
  754. The value of this is taken from the #+LINK lines.")
  755. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  756. (defcustom org-link-abbrev-alist nil
  757. "Alist of link abbreviations.
  758. The car of each element is a string, to be replaced at the start of a link.
  759. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  760. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  761. [[linkkey:tag][description]]
  762. If REPLACE is a string, the tag will simply be appended to create the link.
  763. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  764. the placeholder \"%h\" will cause a url-encoded version of the tag to
  765. be inserted at that point (see the function `url-hexify-string').
  766. REPLACE may also be a function that will be called with the tag as the
  767. only argument to create the link, which should be returned as a string.
  768. See the manual for examples."
  769. :group 'org-link
  770. :type '(repeat
  771. (cons
  772. (string :tag "Protocol")
  773. (choice
  774. (string :tag "Format")
  775. (function)))))
  776. (defcustom org-descriptive-links t
  777. "Non-nil means, hide link part and only show description of bracket links.
  778. Bracket links are like [[link][description]]. This variable sets the initial
  779. state in new org-mode buffers. The setting can then be toggled on a
  780. per-buffer basis from the Org->Hyperlinks menu."
  781. :group 'org-link
  782. :type 'boolean)
  783. (defcustom org-link-file-path-type 'adaptive
  784. "How the path name in file links should be stored.
  785. Valid values are:
  786. relative Relative to the current directory, i.e. the directory of the file
  787. into which the link is being inserted.
  788. absolute Absolute path, if possible with ~ for home directory.
  789. noabbrev Absolute path, no abbreviation of home directory.
  790. adaptive Use relative path for files in the current directory and sub-
  791. directories of it. For other files, use an absolute path."
  792. :group 'org-link
  793. :type '(choice
  794. (const relative)
  795. (const absolute)
  796. (const noabbrev)
  797. (const adaptive)))
  798. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  799. "Types of links that should be activated in Org-mode files.
  800. This is a list of symbols, each leading to the activation of a certain link
  801. type. In principle, it does not hurt to turn on most link types - there may
  802. be a small gain when turning off unused link types. The types are:
  803. bracket The recommended [[link][description]] or [[link]] links with hiding.
  804. angular Links in angular brackets that may contain whitespace like
  805. <bbdb:Carsten Dominik>.
  806. plain Plain links in normal text, no whitespace, like http://google.com.
  807. radio Text that is matched by a radio target, see manual for details.
  808. tag Tag settings in a headline (link to tag search).
  809. date Time stamps (link to calendar).
  810. footnote Footnote labels.
  811. Changing this variable requires a restart of Emacs to become effective."
  812. :group 'org-link
  813. :type '(set :greedy t
  814. (const :tag "Double bracket links (new style)" bracket)
  815. (const :tag "Angular bracket links (old style)" angular)
  816. (const :tag "Plain text links" plain)
  817. (const :tag "Radio target matches" radio)
  818. (const :tag "Tags" tag)
  819. (const :tag "Timestamps" date)
  820. (const :tag "Footnotes" footnote)))
  821. (defcustom org-make-link-description-function nil
  822. "Function to use to generate link descriptions from links. If
  823. nil the link location will be used. This function must take two
  824. parameters; the first is the link and the second the description
  825. org-insert-link has generated, and should return the description
  826. to use."
  827. :group 'org-link
  828. :type 'function)
  829. (defgroup org-link-store nil
  830. "Options concerning storing links in Org-mode."
  831. :tag "Org Store Link"
  832. :group 'org-link)
  833. (defcustom org-email-link-description-format "Email %c: %.30s"
  834. "Format of the description part of a link to an email or usenet message.
  835. The following %-escapes will be replaced by corresponding information:
  836. %F full \"From\" field
  837. %f name, taken from \"From\" field, address if no name
  838. %T full \"To\" field
  839. %t first name in \"To\" field, address if no name
  840. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  841. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  842. %s subject
  843. %m message-id.
  844. You may use normal field width specification between the % and the letter.
  845. This is for example useful to limit the length of the subject.
  846. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  847. :group 'org-link-store
  848. :type 'string)
  849. (defcustom org-from-is-user-regexp
  850. (let (r1 r2)
  851. (when (and user-mail-address (not (string= user-mail-address "")))
  852. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  853. (when (and user-full-name (not (string= user-full-name "")))
  854. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  855. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  856. "Regexp matched against the \"From:\" header of an email or usenet message.
  857. It should match if the message is from the user him/herself."
  858. :group 'org-link-store
  859. :type 'regexp)
  860. (defcustom org-link-to-org-use-id 'create-if-interactive
  861. "Non-nil means, storing a link to an Org file will use entry IDs.
  862. Note that before this variable is even considered, org-id must be loaded,
  863. to please customize `org-modules' and turn it on.
  864. The variable can have the following values:
  865. t Create an ID if needed to make a link to the current entry.
  866. create-if-interactive
  867. If `org-store-link' is called directly (interactively, as a user
  868. command), do create an ID to support the link. But when doing the
  869. job for remember, only use the ID if it already exists. The
  870. purpose of this setting is to avoid proliferation of unwanted
  871. IDs, just because you happen to be in an Org file when you
  872. call `org-remember' that automatically and preemptively
  873. creates a link. If you do want to get an ID link in a remember
  874. template to an entry not having an ID, create it first by
  875. explicitly creating a link to it, using `C-c C-l' first.
  876. use-existing
  877. Use existing ID, do not create one.
  878. nil Never use an ID to make a link, instead link using a text search for
  879. the headline text."
  880. :group 'org-link-store
  881. :type '(choice
  882. (const :tag "Create ID to make link" t)
  883. (const :tag "Create if string link interactively"
  884. 'create-if-interactive)
  885. (const :tag "Only use existing" 'use-existing)
  886. (const :tag "Do not use ID to create link" nil)))
  887. (defcustom org-context-in-file-links t
  888. "Non-nil means, file links from `org-store-link' contain context.
  889. A search string will be added to the file name with :: as separator and
  890. used to find the context when the link is activated by the command
  891. `org-open-at-point'.
  892. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  893. negates this setting for the duration of the command."
  894. :group 'org-link-store
  895. :type 'boolean)
  896. (defcustom org-keep-stored-link-after-insertion nil
  897. "Non-nil means, keep link in list for entire session.
  898. The command `org-store-link' adds a link pointing to the current
  899. location to an internal list. These links accumulate during a session.
  900. The command `org-insert-link' can be used to insert links into any
  901. Org-mode file (offering completion for all stored links). When this
  902. option is nil, every link which has been inserted once using \\[org-insert-link]
  903. will be removed from the list, to make completing the unused links
  904. more efficient."
  905. :group 'org-link-store
  906. :type 'boolean)
  907. (defgroup org-link-follow nil
  908. "Options concerning following links in Org-mode."
  909. :tag "Org Follow Link"
  910. :group 'org-link)
  911. (defcustom org-link-translation-function nil
  912. "Function to translate links with different syntax to Org syntax.
  913. This can be used to translate links created for example by the Planner
  914. or emacs-wiki packages to Org syntax.
  915. The function must accept two parameters, a TYPE containing the link
  916. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  917. which is everything after the link protocol. It should return a cons
  918. with possibly modified values of type and path.
  919. Org contains a function for this, so if you set this variable to
  920. `org-translate-link-from-planner', you should be able follow many
  921. links created by planner."
  922. :group 'org-link-follow
  923. :type 'function)
  924. (defcustom org-follow-link-hook nil
  925. "Hook that is run after a link has been followed."
  926. :group 'org-link-follow
  927. :type 'hook)
  928. (defcustom org-tab-follows-link nil
  929. "Non-nil means, on links TAB will follow the link.
  930. Needs to be set before org.el is loaded."
  931. :group 'org-link-follow
  932. :type 'boolean)
  933. (defcustom org-return-follows-link nil
  934. "Non-nil means, on links RET will follow the link.
  935. Needs to be set before org.el is loaded."
  936. :group 'org-link-follow
  937. :type 'boolean)
  938. (defcustom org-mouse-1-follows-link
  939. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  940. "Non-nil means, mouse-1 on a link will follow the link.
  941. A longer mouse click will still set point. Does not work on XEmacs.
  942. Needs to be set before org.el is loaded."
  943. :group 'org-link-follow
  944. :type 'boolean)
  945. (defcustom org-mark-ring-length 4
  946. "Number of different positions to be recorded in the ring
  947. Changing this requires a restart of Emacs to work correctly."
  948. :group 'org-link-follow
  949. :type 'integer)
  950. (defcustom org-link-frame-setup
  951. '((vm . vm-visit-folder-other-frame)
  952. (gnus . gnus-other-frame)
  953. (file . find-file-other-window))
  954. "Setup the frame configuration for following links.
  955. When following a link with Emacs, it may often be useful to display
  956. this link in another window or frame. This variable can be used to
  957. set this up for the different types of links.
  958. For VM, use any of
  959. `vm-visit-folder'
  960. `vm-visit-folder-other-frame'
  961. For Gnus, use any of
  962. `gnus'
  963. `gnus-other-frame'
  964. `org-gnus-no-new-news'
  965. For FILE, use any of
  966. `find-file'
  967. `find-file-other-window'
  968. `find-file-other-frame'
  969. For the calendar, use the variable `calendar-setup'.
  970. For BBDB, it is currently only possible to display the matches in
  971. another window."
  972. :group 'org-link-follow
  973. :type '(list
  974. (cons (const vm)
  975. (choice
  976. (const vm-visit-folder)
  977. (const vm-visit-folder-other-window)
  978. (const vm-visit-folder-other-frame)))
  979. (cons (const gnus)
  980. (choice
  981. (const gnus)
  982. (const gnus-other-frame)
  983. (const org-gnus-no-new-news)))
  984. (cons (const file)
  985. (choice
  986. (const find-file)
  987. (const find-file-other-window)
  988. (const find-file-other-frame)))))
  989. (defcustom org-display-internal-link-with-indirect-buffer nil
  990. "Non-nil means, use indirect buffer to display infile links.
  991. Activating internal links (from one location in a file to another location
  992. in the same file) normally just jumps to the location. When the link is
  993. activated with a C-u prefix (or with mouse-3), the link is displayed in
  994. another window. When this option is set, the other window actually displays
  995. an indirect buffer clone of the current buffer, to avoid any visibility
  996. changes to the current buffer."
  997. :group 'org-link-follow
  998. :type 'boolean)
  999. (defcustom org-open-non-existing-files nil
  1000. "Non-nil means, `org-open-file' will open non-existing files.
  1001. When nil, an error will be generated."
  1002. :group 'org-link-follow
  1003. :type 'boolean)
  1004. (defcustom org-open-directory-means-index-dot-org nil
  1005. "Non-nil means, a link to a directory really means to index.org.
  1006. When nil, following a directory link will run dired or open a finder/explorer
  1007. window on that directory."
  1008. :group 'org-link-follow
  1009. :type 'boolean)
  1010. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1011. "Function and arguments to call for following mailto links.
  1012. This is a list with the first element being a lisp function, and the
  1013. remaining elements being arguments to the function. In string arguments,
  1014. %a will be replaced by the address, and %s will be replaced by the subject
  1015. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1016. :group 'org-link-follow
  1017. :type '(choice
  1018. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1019. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1020. (const :tag "message-mail" (message-mail "%a" "%s"))
  1021. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1022. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1023. "Non-nil means, ask for confirmation before executing shell links.
  1024. Shell links can be dangerous: just think about a link
  1025. [[shell:rm -rf ~/*][Google Search]]
  1026. This link would show up in your Org-mode document as \"Google Search\",
  1027. but really it would remove your entire home directory.
  1028. Therefore we advise against setting this variable to nil.
  1029. Just change it to `y-or-n-p' of you want to confirm with a
  1030. single keystroke rather than having to type \"yes\"."
  1031. :group 'org-link-follow
  1032. :type '(choice
  1033. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1034. (const :tag "with y-or-n (faster)" y-or-n-p)
  1035. (const :tag "no confirmation (dangerous)" nil)))
  1036. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1037. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1038. Elisp links can be dangerous: just think about a link
  1039. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1040. This link would show up in your Org-mode document as \"Google Search\",
  1041. but really it would remove your entire home directory.
  1042. Therefore we advise against setting this variable to nil.
  1043. Just change it to `y-or-n-p' of you want to confirm with a
  1044. single keystroke rather than having to type \"yes\"."
  1045. :group 'org-link-follow
  1046. :type '(choice
  1047. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1048. (const :tag "with y-or-n (faster)" y-or-n-p)
  1049. (const :tag "no confirmation (dangerous)" nil)))
  1050. (defconst org-file-apps-defaults-gnu
  1051. '((remote . emacs)
  1052. (system . mailcap)
  1053. (t . mailcap))
  1054. "Default file applications on a UNIX or GNU/Linux system.
  1055. See `org-file-apps'.")
  1056. (defconst org-file-apps-defaults-macosx
  1057. '((remote . emacs)
  1058. (t . "open %s")
  1059. (system . "open %s")
  1060. ("ps.gz" . "gv %s")
  1061. ("eps.gz" . "gv %s")
  1062. ("dvi" . "xdvi %s")
  1063. ("fig" . "xfig %s"))
  1064. "Default file applications on a MacOS X system.
  1065. The system \"open\" is known as a default, but we use X11 applications
  1066. for some files for which the OS does not have a good default.
  1067. See `org-file-apps'.")
  1068. (defconst org-file-apps-defaults-windowsnt
  1069. (list
  1070. '(remote . emacs)
  1071. (cons t
  1072. (list (if (featurep 'xemacs)
  1073. 'mswindows-shell-execute
  1074. 'w32-shell-execute)
  1075. "open" 'file))
  1076. (cons 'system
  1077. (list (if (featurep 'xemacs)
  1078. 'mswindows-shell-execute
  1079. 'w32-shell-execute)
  1080. "open" 'file)))
  1081. "Default file applications on a Windows NT system.
  1082. The system \"open\" is used for most files.
  1083. See `org-file-apps'.")
  1084. (defcustom org-file-apps
  1085. '(
  1086. (auto-mode . emacs)
  1087. ("\\.x?html?\\'" . default)
  1088. ("\\.pdf\\'" . default)
  1089. )
  1090. "External applications for opening `file:path' items in a document.
  1091. Org-mode uses system defaults for different file types, but
  1092. you can use this variable to set the application for a given file
  1093. extension. The entries in this list are cons cells where the car identifies
  1094. files and the cdr the corresponding command. Possible values for the
  1095. file identifier are
  1096. \"regex\" Regular expression matched against the file name. For backward
  1097. compatibility, this can also be a string with only alphanumeric
  1098. characters, which is then interpreted as an extension.
  1099. `directory' Matches a directory
  1100. `remote' Matches a remote file, accessible through tramp or efs.
  1101. Remote files most likely should be visited through Emacs
  1102. because external applications cannot handle such paths.
  1103. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1104. so all files Emacs knows how to handle. Using this with
  1105. command `emacs' will open most files in Emacs. Beware that this
  1106. will also open html files inside Emacs, unless you add
  1107. (\"html\" . default) to the list as well.
  1108. t Default for files not matched by any of the other options.
  1109. `system' The system command to open files, like `open' on Windows
  1110. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1111. that will be selected if you call `C-c C-o' with a double
  1112. `C-u C-u' prefix.
  1113. Possible values for the command are:
  1114. `emacs' The file will be visited by the current Emacs process.
  1115. `default' Use the default application for this file type, which is the
  1116. association for t in the list, most likely in the system-specific
  1117. part.
  1118. This can be used to overrule an unwanted setting in the
  1119. system-specific variable.
  1120. `system' Use the system command for opening files, like \"open\".
  1121. This command is specified by the entry whose car is `system'.
  1122. Most likely, the system-specific version of this variable
  1123. does define this command, but you can overrule/replace it
  1124. here.
  1125. string A command to be executed by a shell; %s will be replaced
  1126. by the path to the file.
  1127. sexp A Lisp form which will be evaluated. The file path will
  1128. be available in the Lisp variable `file'.
  1129. For more examples, see the system specific constants
  1130. `org-file-apps-defaults-macosx'
  1131. `org-file-apps-defaults-windowsnt'
  1132. `org-file-apps-defaults-gnu'."
  1133. :group 'org-link-follow
  1134. :type '(repeat
  1135. (cons (choice :value ""
  1136. (string :tag "Extension")
  1137. (const :tag "System command to open files" system)
  1138. (const :tag "Default for unrecognized files" t)
  1139. (const :tag "Remote file" remote)
  1140. (const :tag "Links to a directory" directory)
  1141. (const :tag "Any files that have Emacs modes"
  1142. auto-mode))
  1143. (choice :value ""
  1144. (const :tag "Visit with Emacs" emacs)
  1145. (const :tag "Use default" default)
  1146. (const :tag "Use the system command" system)
  1147. (string :tag "Command")
  1148. (sexp :tag "Lisp form")))))
  1149. (defgroup org-refile nil
  1150. "Options concerning refiling entries in Org-mode."
  1151. :tag "Org Refile"
  1152. :group 'org)
  1153. (defcustom org-directory "~/org"
  1154. "Directory with org files.
  1155. This directory will be used as default to prompt for org files.
  1156. Used by the hooks for remember.el."
  1157. :group 'org-refile
  1158. :group 'org-remember
  1159. :type 'directory)
  1160. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1161. "Default target for storing notes.
  1162. Used by the hooks for remember.el. This can be a string, or nil to mean
  1163. the value of `remember-data-file'.
  1164. You can set this on a per-template basis with the variable
  1165. `org-remember-templates'."
  1166. :group 'org-refile
  1167. :group 'org-remember
  1168. :type '(choice
  1169. (const :tag "Default from remember-data-file" nil)
  1170. file))
  1171. (defcustom org-goto-interface 'outline
  1172. "The default interface to be used for `org-goto'.
  1173. Allowed values are:
  1174. outline The interface shows an outline of the relevant file
  1175. and the correct heading is found by moving through
  1176. the outline or by searching with incremental search.
  1177. outline-path-completion Headlines in the current buffer are offered via
  1178. completion. This is the interface also used by
  1179. the refile command."
  1180. :group 'org-refile
  1181. :type '(choice
  1182. (const :tag "Outline" outline)
  1183. (const :tag "Outline-path-completion" outline-path-completion)))
  1184. (defcustom org-goto-max-level 5
  1185. "Maximum level to be considered when running org-goto with refile interface."
  1186. :group 'org-refile
  1187. :type 'number)
  1188. (defcustom org-reverse-note-order nil
  1189. "Non-nil means, store new notes at the beginning of a file or entry.
  1190. When nil, new notes will be filed to the end of a file or entry.
  1191. This can also be a list with cons cells of regular expressions that
  1192. are matched against file names, and values."
  1193. :group 'org-remember
  1194. :group 'org-refile
  1195. :type '(choice
  1196. (const :tag "Reverse always" t)
  1197. (const :tag "Reverse never" nil)
  1198. (repeat :tag "By file name regexp"
  1199. (cons regexp boolean))))
  1200. (defcustom org-refile-targets nil
  1201. "Targets for refiling entries with \\[org-refile].
  1202. This is list of cons cells. Each cell contains:
  1203. - a specification of the files to be considered, either a list of files,
  1204. or a symbol whose function or variable value will be used to retrieve
  1205. a file name or a list of file names. If you use `org-agenda-files' for
  1206. that, all agenda files will be scanned for targets. Nil means, consider
  1207. headings in the current buffer.
  1208. - A specification of how to select find candidate refile targets. This
  1209. may be any of
  1210. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1211. This tag has to be present in all target headlines, inheritance will
  1212. not be considered.
  1213. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1214. todo keyword.
  1215. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1216. headlines that are refiling targets.
  1217. - a cons cell (:level . N). Any headline of level N is considered a target.
  1218. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1219. When this variable is nil, all top-level headlines in the current buffer
  1220. are used, equivalent to the value `((nil . (:level . 1))'."
  1221. :group 'org-refile
  1222. :type '(repeat
  1223. (cons
  1224. (choice :value org-agenda-files
  1225. (const :tag "All agenda files" org-agenda-files)
  1226. (const :tag "Current buffer" nil)
  1227. (function) (variable) (file))
  1228. (choice :tag "Identify target headline by"
  1229. (cons :tag "Specific tag" (const :value :tag) (string))
  1230. (cons :tag "TODO keyword" (const :value :todo) (string))
  1231. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1232. (cons :tag "Level number" (const :value :level) (integer))
  1233. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1234. (defcustom org-refile-use-outline-path nil
  1235. "Non-nil means, provide refile targets as paths.
  1236. So a level 3 headline will be available as level1/level2/level3.
  1237. When the value is `file', also include the file name (without directory)
  1238. into the path. When `full-file-path', include the full file path."
  1239. :group 'org-refile
  1240. :type '(choice
  1241. (const :tag "Not" nil)
  1242. (const :tag "Yes" t)
  1243. (const :tag "Start with file name" file)
  1244. (const :tag "Start with full file path" full-file-path)))
  1245. (defcustom org-outline-path-complete-in-steps t
  1246. "Non-nil means, complete the outline path in hierarchical steps.
  1247. When Org-mode uses the refile interface to select an outline path
  1248. \(see variable `org-refile-use-outline-path'), the completion of
  1249. the path can be done is a single go, or if can be done in steps down
  1250. the headline hierarchy. Going in steps is probably the best if you
  1251. do not use a special completion package like `ido' or `icicles'.
  1252. However, when using these packages, going in one step can be very
  1253. fast, while still showing the whole path to the entry."
  1254. :group 'org-refile
  1255. :type 'boolean)
  1256. (defgroup org-todo nil
  1257. "Options concerning TODO items in Org-mode."
  1258. :tag "Org TODO"
  1259. :group 'org)
  1260. (defgroup org-progress nil
  1261. "Options concerning Progress logging in Org-mode."
  1262. :tag "Org Progress"
  1263. :group 'org-time)
  1264. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1265. "List of TODO entry keyword sequences and their interpretation.
  1266. \\<org-mode-map>This is a list of sequences.
  1267. Each sequence starts with a symbol, either `sequence' or `type',
  1268. indicating if the keywords should be interpreted as a sequence of
  1269. action steps, or as different types of TODO items. The first
  1270. keywords are states requiring action - these states will select a headline
  1271. for inclusion into the global TODO list Org-mode produces. If one of
  1272. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1273. signify that no further action is necessary. If \"|\" is not found,
  1274. the last keyword is treated as the only DONE state of the sequence.
  1275. The command \\[org-todo] cycles an entry through these states, and one
  1276. additional state where no keyword is present. For details about this
  1277. cycling, see the manual.
  1278. TODO keywords and interpretation can also be set on a per-file basis with
  1279. the special #+SEQ_TODO and #+TYP_TODO lines.
  1280. Each keyword can optionally specify a character for fast state selection
  1281. \(in combination with the variable `org-use-fast-todo-selection')
  1282. and specifiers for state change logging, using the same syntax
  1283. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1284. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1285. indicates to record a time stamp each time this state is selected.
  1286. Each keyword may also specify if a timestamp or a note should be
  1287. recorded when entering or leaving the state, by adding additional
  1288. characters in the parenthesis after the keyword. This looks like this:
  1289. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1290. record only the time of the state change. With X and Y being either
  1291. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1292. Y when leaving the state if and only if the *target* state does not
  1293. define X. You may omit any of the fast-selection key or X or /Y,
  1294. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1295. For backward compatibility, this variable may also be just a list
  1296. of keywords - in this case the interpretation (sequence or type) will be
  1297. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1298. :group 'org-todo
  1299. :group 'org-keywords
  1300. :type '(choice
  1301. (repeat :tag "Old syntax, just keywords"
  1302. (string :tag "Keyword"))
  1303. (repeat :tag "New syntax"
  1304. (cons
  1305. (choice
  1306. :tag "Interpretation"
  1307. (const :tag "Sequence (cycling hits every state)" sequence)
  1308. (const :tag "Type (cycling directly to DONE)" type))
  1309. (repeat
  1310. (string :tag "Keyword"))))))
  1311. (defvar org-todo-keywords-1 nil
  1312. "All TODO and DONE keywords active in a buffer.")
  1313. (make-variable-buffer-local 'org-todo-keywords-1)
  1314. (defvar org-todo-keywords-for-agenda nil)
  1315. (defvar org-done-keywords-for-agenda nil)
  1316. (defvar org-todo-keyword-alist-for-agenda nil)
  1317. (defvar org-tag-alist-for-agenda nil)
  1318. (defvar org-agenda-contributing-files nil)
  1319. (defvar org-not-done-keywords nil)
  1320. (make-variable-buffer-local 'org-not-done-keywords)
  1321. (defvar org-done-keywords nil)
  1322. (make-variable-buffer-local 'org-done-keywords)
  1323. (defvar org-todo-heads nil)
  1324. (make-variable-buffer-local 'org-todo-heads)
  1325. (defvar org-todo-sets nil)
  1326. (make-variable-buffer-local 'org-todo-sets)
  1327. (defvar org-todo-log-states nil)
  1328. (make-variable-buffer-local 'org-todo-log-states)
  1329. (defvar org-todo-kwd-alist nil)
  1330. (make-variable-buffer-local 'org-todo-kwd-alist)
  1331. (defvar org-todo-key-alist nil)
  1332. (make-variable-buffer-local 'org-todo-key-alist)
  1333. (defvar org-todo-key-trigger nil)
  1334. (make-variable-buffer-local 'org-todo-key-trigger)
  1335. (defcustom org-todo-interpretation 'sequence
  1336. "Controls how TODO keywords are interpreted.
  1337. This variable is in principle obsolete and is only used for
  1338. backward compatibility, if the interpretation of todo keywords is
  1339. not given already in `org-todo-keywords'. See that variable for
  1340. more information."
  1341. :group 'org-todo
  1342. :group 'org-keywords
  1343. :type '(choice (const sequence)
  1344. (const type)))
  1345. (defcustom org-use-fast-todo-selection 'prefix
  1346. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1347. This variable describes if and under what circumstances the cycling
  1348. mechanism for TODO keywords will be replaced by a single-key, direct
  1349. selection scheme.
  1350. When nil, fast selection is never used.
  1351. When the symbol `prefix', it will be used when `org-todo' is called with
  1352. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1353. in an agenda buffer.
  1354. When t, fast selection is used by default. In this case, the prefix
  1355. argument forces cycling instead.
  1356. In all cases, the special interface is only used if access keys have actually
  1357. been assigned by the user, i.e. if keywords in the configuration are followed
  1358. by a letter in parenthesis, like TODO(t)."
  1359. :group 'org-todo
  1360. :type '(choice
  1361. (const :tag "Never" nil)
  1362. (const :tag "By default" t)
  1363. (const :tag "Only with C-u C-c C-t" prefix)))
  1364. (defcustom org-provide-todo-statistics t
  1365. "Non-nil means, update todo statistics after insert and toggle.
  1366. When this is set, todo statistics is updated in the parent of the current
  1367. entry each time a todo state is changed."
  1368. :group 'org-todo
  1369. :type 'boolean)
  1370. (defcustom org-after-todo-state-change-hook nil
  1371. "Hook which is run after the state of a TODO item was changed.
  1372. The new state (a string with a TODO keyword, or nil) is available in the
  1373. Lisp variable `state'."
  1374. :group 'org-todo
  1375. :type 'hook)
  1376. (defcustom org-todo-state-tags-triggers nil
  1377. "Tag changes that should be triggered by TODO state changes.
  1378. This is a list. Each entry is
  1379. (state-change (tag . flag) .......)
  1380. State-change can be a string with a state, and empty string to indicate the
  1381. state that has no TODO keyword, or it can be one of the symbols `todo'
  1382. or `done', meaning any not-done or done state, respectively."
  1383. :group 'org-todo
  1384. :group 'org-tags
  1385. :type '(repeat
  1386. (cons (choice :tag "When changing to"
  1387. (const :tag "Not-done state" todo)
  1388. (const :tag "Done state" done)
  1389. (string :tag "State"))
  1390. (repeat
  1391. (cons :tag "Tag action"
  1392. (string :tag "Tag")
  1393. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1394. (defcustom org-log-done nil
  1395. "Information to record when a task moves to the DONE state.
  1396. Possible values are:
  1397. nil Don't add anything, just change the keyword
  1398. time Add a time stamp to the task
  1399. note Prompt a closing note and add it with template `org-log-note-headings'
  1400. This option can also be set with on a per-file-basis with
  1401. #+STARTUP: nologdone
  1402. #+STARTUP: logdone
  1403. #+STARTUP: lognotedone
  1404. You can have local logging settings for a subtree by setting the LOGGING
  1405. property to one or more of these keywords."
  1406. :group 'org-todo
  1407. :group 'org-progress
  1408. :type '(choice
  1409. (const :tag "No logging" nil)
  1410. (const :tag "Record CLOSED timestamp" time)
  1411. (const :tag "Record CLOSED timestamp with closing note." note)))
  1412. ;; Normalize old uses of org-log-done.
  1413. (cond
  1414. ((eq org-log-done t) (setq org-log-done 'time))
  1415. ((and (listp org-log-done) (memq 'done org-log-done))
  1416. (setq org-log-done 'note)))
  1417. (defcustom org-log-note-clock-out nil
  1418. "Non-nil means, record a note when clocking out of an item.
  1419. This can also be configured on a per-file basis by adding one of
  1420. the following lines anywhere in the buffer:
  1421. #+STARTUP: lognoteclock-out
  1422. #+STARTUP: nolognoteclock-out"
  1423. :group 'org-todo
  1424. :group 'org-progress
  1425. :type 'boolean)
  1426. (defcustom org-log-done-with-time t
  1427. "Non-nil means, the CLOSED time stamp will contain date and time.
  1428. When nil, only the date will be recorded."
  1429. :group 'org-progress
  1430. :type 'boolean)
  1431. (defcustom org-log-note-headings
  1432. '((done . "CLOSING NOTE %t")
  1433. (state . "State %-12s %t")
  1434. (note . "Note taken on %t")
  1435. (clock-out . ""))
  1436. "Headings for notes added to entries.
  1437. The value is an alist, with the car being a symbol indicating the note
  1438. context, and the cdr is the heading to be used. The heading may also be the
  1439. empty string.
  1440. %t in the heading will be replaced by a time stamp.
  1441. %s will be replaced by the new TODO state, in double quotes.
  1442. %u will be replaced by the user name.
  1443. %U will be replaced by the full user name."
  1444. :group 'org-todo
  1445. :group 'org-progress
  1446. :type '(list :greedy t
  1447. (cons (const :tag "Heading when closing an item" done) string)
  1448. (cons (const :tag
  1449. "Heading when changing todo state (todo sequence only)"
  1450. state) string)
  1451. (cons (const :tag "Heading when just taking a note" note) string)
  1452. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1453. (unless (assq 'note org-log-note-headings)
  1454. (push '(note . "%t") org-log-note-headings))
  1455. (defcustom org-log-state-notes-insert-after-drawers nil
  1456. "Non-nil means, insert state change notes after any drawers in entry.
  1457. Only the drawers that *immediately* follow the headline and the
  1458. deadline/scheduled line are skipped.
  1459. When nil, insert notes right after the heading and perhaps the line
  1460. with deadline/scheduling if present."
  1461. :group 'org-todo
  1462. :group 'org-progress
  1463. :type 'boolean)
  1464. (defcustom org-log-states-order-reversed t
  1465. "Non-nil means, the latest state change note will be directly after heading.
  1466. When nil, the notes will be orderer according to time."
  1467. :group 'org-todo
  1468. :group 'org-progress
  1469. :type 'boolean)
  1470. (defcustom org-log-repeat 'time
  1471. "Non-nil means, record moving through the DONE state when triggering repeat.
  1472. An auto-repeating tasks is immediately switched back to TODO when marked
  1473. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1474. the TODO keyword definition, or recording a closing note by setting
  1475. `org-log-done', there will be no record of the task moving through DONE.
  1476. This variable forces taking a note anyway. Possible values are:
  1477. nil Don't force a record
  1478. time Record a time stamp
  1479. note Record a note
  1480. This option can also be set with on a per-file-basis with
  1481. #+STARTUP: logrepeat
  1482. #+STARTUP: lognoterepeat
  1483. #+STARTUP: nologrepeat
  1484. You can have local logging settings for a subtree by setting the LOGGING
  1485. property to one or more of these keywords."
  1486. :group 'org-todo
  1487. :group 'org-progress
  1488. :type '(choice
  1489. (const :tag "Don't force a record" nil)
  1490. (const :tag "Force recording the DONE state" time)
  1491. (const :tag "Force recording a note with the DONE state" note)))
  1492. (defgroup org-priorities nil
  1493. "Priorities in Org-mode."
  1494. :tag "Org Priorities"
  1495. :group 'org-todo)
  1496. (defcustom org-highest-priority ?A
  1497. "The highest priority of TODO items. A character like ?A, ?B etc.
  1498. Must have a smaller ASCII number than `org-lowest-priority'."
  1499. :group 'org-priorities
  1500. :type 'character)
  1501. (defcustom org-lowest-priority ?C
  1502. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1503. Must have a larger ASCII number than `org-highest-priority'."
  1504. :group 'org-priorities
  1505. :type 'character)
  1506. (defcustom org-default-priority ?B
  1507. "The default priority of TODO items.
  1508. This is the priority an item get if no explicit priority is given."
  1509. :group 'org-priorities
  1510. :type 'character)
  1511. (defcustom org-priority-start-cycle-with-default t
  1512. "Non-nil means, start with default priority when starting to cycle.
  1513. When this is nil, the first step in the cycle will be (depending on the
  1514. command used) one higher or lower that the default priority."
  1515. :group 'org-priorities
  1516. :type 'boolean)
  1517. (defgroup org-time nil
  1518. "Options concerning time stamps and deadlines in Org-mode."
  1519. :tag "Org Time"
  1520. :group 'org)
  1521. (defcustom org-insert-labeled-timestamps-at-point nil
  1522. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1523. When nil, these labeled time stamps are forces into the second line of an
  1524. entry, just after the headline. When scheduling from the global TODO list,
  1525. the time stamp will always be forced into the second line."
  1526. :group 'org-time
  1527. :type 'boolean)
  1528. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1529. "Formats for `format-time-string' which are used for time stamps.
  1530. It is not recommended to change this constant.")
  1531. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1532. "Number of minutes to round time stamps to.
  1533. These are two values, the first applies when first creating a time stamp.
  1534. The second applies when changing it with the commands `S-up' and `S-down'.
  1535. When changing the time stamp, this means that it will change in steps
  1536. of N minutes, as given by the second value.
  1537. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1538. numbers should be factors of 60, so for example 5, 10, 15.
  1539. When this is larger than 1, you can still force an exact time-stamp by using
  1540. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1541. and by using a prefix arg to `S-up/down' to specify the exact number
  1542. of minutes to shift."
  1543. :group 'org-time
  1544. :get '(lambda (var) ; Make sure all entries have 5 elements
  1545. (if (integerp (default-value var))
  1546. (list (default-value var) 5)
  1547. (default-value var)))
  1548. :type '(list
  1549. (integer :tag "when inserting times")
  1550. (integer :tag "when modifying times")))
  1551. ;; Normalize old customizations of this variable.
  1552. (when (integerp org-time-stamp-rounding-minutes)
  1553. (setq org-time-stamp-rounding-minutes
  1554. (list org-time-stamp-rounding-minutes
  1555. org-time-stamp-rounding-minutes)))
  1556. (defcustom org-display-custom-times nil
  1557. "Non-nil means, overlay custom formats over all time stamps.
  1558. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1559. To turn this on on a per-file basis, insert anywhere in the file:
  1560. #+STARTUP: customtime"
  1561. :group 'org-time
  1562. :set 'set-default
  1563. :type 'sexp)
  1564. (make-variable-buffer-local 'org-display-custom-times)
  1565. (defcustom org-time-stamp-custom-formats
  1566. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1567. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1568. These are overlayed over the default ISO format if the variable
  1569. `org-display-custom-times' is set. Time like %H:%M should be at the
  1570. end of the second format."
  1571. :group 'org-time
  1572. :type 'sexp)
  1573. (defun org-time-stamp-format (&optional long inactive)
  1574. "Get the right format for a time string."
  1575. (let ((f (if long (cdr org-time-stamp-formats)
  1576. (car org-time-stamp-formats))))
  1577. (if inactive
  1578. (concat "[" (substring f 1 -1) "]")
  1579. f)))
  1580. (defcustom org-time-clocksum-format "%d:%02d"
  1581. "The format string used when creating CLOCKSUM lines, or when
  1582. org-mode generates a time duration."
  1583. :group 'org-time
  1584. :type 'string)
  1585. (defcustom org-deadline-warning-days 14
  1586. "No. of days before expiration during which a deadline becomes active.
  1587. This variable governs the display in sparse trees and in the agenda.
  1588. When 0 or negative, it means use this number (the absolute value of it)
  1589. even if a deadline has a different individual lead time specified."
  1590. :group 'org-time
  1591. :group 'org-agenda-daily/weekly
  1592. :type 'number)
  1593. (defcustom org-read-date-prefer-future t
  1594. "Non-nil means, assume future for incomplete date input from user.
  1595. This affects the following situations:
  1596. 1. The user gives a day, but no month.
  1597. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1598. read this as the third of *next* month. However, if you enter \"17\",
  1599. it will be considered as *this* month.
  1600. 2. The user gives a month but not a year.
  1601. For example, if it is april and you enter \"feb 2\", this will be read
  1602. as feb 2, *next* year. \"May 5\", however, will be this year.
  1603. Currently this does not work for ISO week specifications.
  1604. When this option is nil, the current month and year will always be used
  1605. as defaults."
  1606. :group 'org-time
  1607. :type 'boolean)
  1608. (defcustom org-read-date-display-live t
  1609. "Non-nil means, display current interpretation of date prompt live.
  1610. This display will be in an overlay, in the minibuffer."
  1611. :group 'org-time
  1612. :type 'boolean)
  1613. (defcustom org-read-date-popup-calendar t
  1614. "Non-nil means, pop up a calendar when prompting for a date.
  1615. In the calendar, the date can be selected with mouse-1. However, the
  1616. minibuffer will also be active, and you can simply enter the date as well.
  1617. When nil, only the minibuffer will be available."
  1618. :group 'org-time
  1619. :type 'boolean)
  1620. (if (fboundp 'defvaralias)
  1621. (defvaralias 'org-popup-calendar-for-date-prompt
  1622. 'org-read-date-popup-calendar))
  1623. (defcustom org-extend-today-until 0
  1624. "The hour when your day really ends. Must be an integer.
  1625. This has influence for the following applications:
  1626. - When switching the agenda to \"today\". It it is still earlier than
  1627. the time given here, the day recognized as TODAY is actually yesterday.
  1628. - When a date is read from the user and it is still before the time given
  1629. here, the current date and time will be assumed to be yesterday, 23:59.
  1630. Also, timestamps inserted in remember templates follow this rule.
  1631. IMPORTANT: This is a feature whose implementation is and likely will
  1632. remain incomplete. Really, it is only here because past midnight seems to
  1633. be the favorite working time of John Wiegley :-)"
  1634. :group 'org-time
  1635. :type 'number)
  1636. (defcustom org-edit-timestamp-down-means-later nil
  1637. "Non-nil means, S-down will increase the time in a time stamp.
  1638. When nil, S-up will increase."
  1639. :group 'org-time
  1640. :type 'boolean)
  1641. (defcustom org-calendar-follow-timestamp-change t
  1642. "Non-nil means, make the calendar window follow timestamp changes.
  1643. When a timestamp is modified and the calendar window is visible, it will be
  1644. moved to the new date."
  1645. :group 'org-time
  1646. :type 'boolean)
  1647. (defgroup org-tags nil
  1648. "Options concerning tags in Org-mode."
  1649. :tag "Org Tags"
  1650. :group 'org)
  1651. (defcustom org-tag-alist nil
  1652. "List of tags allowed in Org-mode files.
  1653. When this list is nil, Org-mode will base TAG input on what is already in the
  1654. buffer.
  1655. The value of this variable is an alist, the car of each entry must be a
  1656. keyword as a string, the cdr may be a character that is used to select
  1657. that tag through the fast-tag-selection interface.
  1658. See the manual for details."
  1659. :group 'org-tags
  1660. :type '(repeat
  1661. (choice
  1662. (cons (string :tag "Tag name")
  1663. (character :tag "Access char"))
  1664. (const :tag "Start radio group" (:startgroup))
  1665. (const :tag "End radio group" (:endgroup)))))
  1666. (defvar org-file-tags nil
  1667. "List of tags that can be inherited by all entries in the file.
  1668. The tags will be inherited if the variable `org-use-tag-inheritance'
  1669. says they should be.
  1670. This variable is populated from #+TAG lines.")
  1671. (defcustom org-use-fast-tag-selection 'auto
  1672. "Non-nil means, use fast tag selection scheme.
  1673. This is a special interface to select and deselect tags with single keys.
  1674. When nil, fast selection is never used.
  1675. When the symbol `auto', fast selection is used if and only if selection
  1676. characters for tags have been configured, either through the variable
  1677. `org-tag-alist' or through a #+TAGS line in the buffer.
  1678. When t, fast selection is always used and selection keys are assigned
  1679. automatically if necessary."
  1680. :group 'org-tags
  1681. :type '(choice
  1682. (const :tag "Always" t)
  1683. (const :tag "Never" nil)
  1684. (const :tag "When selection characters are configured" 'auto)))
  1685. (defcustom org-fast-tag-selection-single-key nil
  1686. "Non-nil means, fast tag selection exits after first change.
  1687. When nil, you have to press RET to exit it.
  1688. During fast tag selection, you can toggle this flag with `C-c'.
  1689. This variable can also have the value `expert'. In this case, the window
  1690. displaying the tags menu is not even shown, until you press C-c again."
  1691. :group 'org-tags
  1692. :type '(choice
  1693. (const :tag "No" nil)
  1694. (const :tag "Yes" t)
  1695. (const :tag "Expert" expert)))
  1696. (defvar org-fast-tag-selection-include-todo nil
  1697. "Non-nil means, fast tags selection interface will also offer TODO states.
  1698. This is an undocumented feature, you should not rely on it.")
  1699. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1700. "The column to which tags should be indented in a headline.
  1701. If this number is positive, it specifies the column. If it is negative,
  1702. it means that the tags should be flushright to that column. For example,
  1703. -80 works well for a normal 80 character screen."
  1704. :group 'org-tags
  1705. :type 'integer)
  1706. (defcustom org-auto-align-tags t
  1707. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1708. These operations change the length of a headline and therefore shift
  1709. the tags around. With this options turned on, after each such operation
  1710. the tags are again aligned to `org-tags-column'."
  1711. :group 'org-tags
  1712. :type 'boolean)
  1713. (defcustom org-use-tag-inheritance t
  1714. "Non-nil means, tags in levels apply also for sublevels.
  1715. When nil, only the tags directly given in a specific line apply there.
  1716. This may also be a list of tags that should be inherited, or a regexp that
  1717. matches tags that should be inherited. Additional control is possible
  1718. with the variable `org-tags-exclude-from-inheritance' which gives an
  1719. explicit list of tags to be excluded from inheritance., even if the value of
  1720. `org-use-tag-inheritance' would select it for inheritance.
  1721. If this option is t, a match early-on in a tree can lead to a large
  1722. number of matches in the subtree when constructing the agenda or creating
  1723. a sparse tree. If you only want to see the first match in a tree during
  1724. a search, check out the variable `org-tags-match-list-sublevels'."
  1725. :group 'org-tags
  1726. :type '(choice
  1727. (const :tag "Not" nil)
  1728. (const :tag "Always" t)
  1729. (repeat :tag "Specific tags" (string :tag "Tag"))
  1730. (regexp :tag "Tags matched by regexp")))
  1731. (defcustom org-tags-exclude-from-inheritance nil
  1732. "List of tags that should never be inherited.
  1733. This is a way to exclude a few tags from inheritance. For way to do
  1734. the opposite, to actively allow inheritance for selected tags,
  1735. see the variable `org-use-tag-inheritance'."
  1736. :group 'org-tags
  1737. :type '(repeat (string :tag "Tag")))
  1738. (defun org-tag-inherit-p (tag)
  1739. "Check if TAG is one that should be inherited."
  1740. (cond
  1741. ((member tag org-tags-exclude-from-inheritance) nil)
  1742. ((eq org-use-tag-inheritance t) t)
  1743. ((not org-use-tag-inheritance) nil)
  1744. ((stringp org-use-tag-inheritance)
  1745. (string-match org-use-tag-inheritance tag))
  1746. ((listp org-use-tag-inheritance)
  1747. (member tag org-use-tag-inheritance))
  1748. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1749. (defcustom org-tags-match-list-sublevels t
  1750. "Non-nil means list also sublevels of headlines matching tag search.
  1751. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1752. the sublevels of a headline matching a tag search often also match
  1753. the same search. Listing all of them can create very long lists.
  1754. Setting this variable to nil causes subtrees of a match to be skipped.
  1755. This option is off by default, because inheritance in on. If you turn
  1756. inheritance off, you very likely want to turn this option on.
  1757. As a special case, if the tag search is restricted to TODO items, the
  1758. value of this variable is ignored and sublevels are always checked, to
  1759. make sure all corresponding TODO items find their way into the list.
  1760. This variable is semi-obsolete and probably should always be true. It
  1761. is better to limit inheritance to certain tags using the variables
  1762. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1763. :group 'org-tags
  1764. :type 'boolean)
  1765. (defvar org-tags-history nil
  1766. "History of minibuffer reads for tags.")
  1767. (defvar org-last-tags-completion-table nil
  1768. "The last used completion table for tags.")
  1769. (defvar org-after-tags-change-hook nil
  1770. "Hook that is run after the tags in a line have changed.")
  1771. (defgroup org-properties nil
  1772. "Options concerning properties in Org-mode."
  1773. :tag "Org Properties"
  1774. :group 'org)
  1775. (defcustom org-property-format "%-10s %s"
  1776. "How property key/value pairs should be formatted by `indent-line'.
  1777. When `indent-line' hits a property definition, it will format the line
  1778. according to this format, mainly to make sure that the values are
  1779. lined-up with respect to each other."
  1780. :group 'org-properties
  1781. :type 'string)
  1782. (defcustom org-use-property-inheritance nil
  1783. "Non-nil means, properties apply also for sublevels.
  1784. This setting is chiefly used during property searches. Turning it on can
  1785. cause significant overhead when doing a search, which is why it is not
  1786. on by default.
  1787. When nil, only the properties directly given in the current entry count.
  1788. When t, every property is inherited. The value may also be a list of
  1789. properties that should have inheritance, or a regular expression matching
  1790. properties that should be inherited.
  1791. However, note that some special properties use inheritance under special
  1792. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1793. and the properties ending in \"_ALL\" when they are used as descriptor
  1794. for valid values of a property.
  1795. Note for programmers:
  1796. When querying an entry with `org-entry-get', you can control if inheritance
  1797. should be used. By default, `org-entry-get' looks only at the local
  1798. properties. You can request inheritance by setting the inherit argument
  1799. to t (to force inheritance) or to `selective' (to respect the setting
  1800. in this variable)."
  1801. :group 'org-properties
  1802. :type '(choice
  1803. (const :tag "Not" nil)
  1804. (const :tag "Always" t)
  1805. (repeat :tag "Specific properties" (string :tag "Property"))
  1806. (regexp :tag "Properties matched by regexp")))
  1807. (defun org-property-inherit-p (property)
  1808. "Check if PROPERTY is one that should be inherited."
  1809. (cond
  1810. ((eq org-use-property-inheritance t) t)
  1811. ((not org-use-property-inheritance) nil)
  1812. ((stringp org-use-property-inheritance)
  1813. (string-match org-use-property-inheritance property))
  1814. ((listp org-use-property-inheritance)
  1815. (member property org-use-property-inheritance))
  1816. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1817. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1818. "The default column format, if no other format has been defined.
  1819. This variable can be set on the per-file basis by inserting a line
  1820. #+COLUMNS: %25ITEM ....."
  1821. :group 'org-properties
  1822. :type 'string)
  1823. (defcustom org-columns-ellipses ".."
  1824. "The ellipses to be used when a field in column view is truncated.
  1825. When this is the empty string, as many characters as possible are shown,
  1826. but then there will be no visual indication that the field has been truncated.
  1827. When this is a string of length N, the last N characters of a truncated
  1828. field are replaced by this string. If the column is narrower than the
  1829. ellipses string, only part of the ellipses string will be shown."
  1830. :group 'org-properties
  1831. :type 'string)
  1832. (defcustom org-columns-modify-value-for-display-function nil
  1833. "Function that modifies values for display in column view.
  1834. For example, it can be used to cut out a certain part from a time stamp.
  1835. The function must take 2 arguments:
  1836. column-title The title of the column (*not* the property name)
  1837. value The value that should be modified.
  1838. The function should return the value that should be displayed,
  1839. or nil if the normal value should be used."
  1840. :group 'org-properties
  1841. :type 'function)
  1842. (defcustom org-effort-property "Effort"
  1843. "The property that is being used to keep track of effort estimates.
  1844. Effort estimates given in this property need to have the format H:MM."
  1845. :group 'org-properties
  1846. :group 'org-progress
  1847. :type '(string :tag "Property"))
  1848. (defconst org-global-properties-fixed
  1849. '(("VISIBILITY_ALL" . "folded children content all"))
  1850. "List of property/value pairs that can be inherited by any entry.
  1851. These are fixed values, for the preset properties.")
  1852. (defcustom org-global-properties nil
  1853. "List of property/value pairs that can be inherited by any entry.
  1854. You can set buffer-local values for the same purpose in the variable
  1855. `org-file-properties' this by adding lines like
  1856. #+PROPERTY: NAME VALUE"
  1857. :group 'org-properties
  1858. :type '(repeat
  1859. (cons (string :tag "Property")
  1860. (string :tag "Value"))))
  1861. (defvar org-file-properties nil
  1862. "List of property/value pairs that can be inherited by any entry.
  1863. Valid for the current buffer.
  1864. This variable is populated from #+PROPERTY lines.")
  1865. (make-variable-buffer-local 'org-file-properties)
  1866. (defgroup org-agenda nil
  1867. "Options concerning agenda views in Org-mode."
  1868. :tag "Org Agenda"
  1869. :group 'org)
  1870. (defvar org-category nil
  1871. "Variable used by org files to set a category for agenda display.
  1872. Such files should use a file variable to set it, for example
  1873. # -*- mode: org; org-category: \"ELisp\"
  1874. or contain a special line
  1875. #+CATEGORY: ELisp
  1876. If the file does not specify a category, then file's base name
  1877. is used instead.")
  1878. (make-variable-buffer-local 'org-category)
  1879. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1880. (defcustom org-agenda-files nil
  1881. "The files to be used for agenda display.
  1882. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1883. \\[org-remove-file]. You can also use customize to edit the list.
  1884. If an entry is a directory, all files in that directory that are matched by
  1885. `org-agenda-file-regexp' will be part of the file list.
  1886. If the value of the variable is not a list but a single file name, then
  1887. the list of agenda files is actually stored and maintained in that file, one
  1888. agenda file per line."
  1889. :group 'org-agenda
  1890. :type '(choice
  1891. (repeat :tag "List of files and directories" file)
  1892. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1893. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1894. "Regular expression to match files for `org-agenda-files'.
  1895. If any element in the list in that variable contains a directory instead
  1896. of a normal file, all files in that directory that are matched by this
  1897. regular expression will be included."
  1898. :group 'org-agenda
  1899. :type 'regexp)
  1900. (defcustom org-agenda-text-search-extra-files nil
  1901. "List of extra files to be searched by text search commands.
  1902. These files will be search in addition to the agenda files by the
  1903. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1904. Note that these files will only be searched for text search commands,
  1905. not for the other agenda views like todo lists, tag searches or the weekly
  1906. agenda. This variable is intended to list notes and possibly archive files
  1907. that should also be searched by these two commands.
  1908. In fact, if the first element in the list is the symbol `agenda-archives',
  1909. than all archive files of all agenda files will be added to the search
  1910. scope."
  1911. :group 'org-agenda
  1912. :type '(set :greedy t
  1913. (const :tag "Agenda Archives" agenda-archives)
  1914. (repeat :inline t (file))))
  1915. (if (fboundp 'defvaralias)
  1916. (defvaralias 'org-agenda-multi-occur-extra-files
  1917. 'org-agenda-text-search-extra-files))
  1918. (defcustom org-agenda-skip-unavailable-files nil
  1919. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1920. A nil value means to remove them, after a query, from the list."
  1921. :group 'org-agenda
  1922. :type 'boolean)
  1923. (defcustom org-calendar-to-agenda-key [?c]
  1924. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1925. The command `org-calendar-goto-agenda' will be bound to this key. The
  1926. default is the character `c' because then `c' can be used to switch back and
  1927. forth between agenda and calendar."
  1928. :group 'org-agenda
  1929. :type 'sexp)
  1930. (defcustom org-calendar-agenda-action-key [?k]
  1931. "The key to be installed in `calendar-mode-map' for agenda-action.
  1932. The command `org-agenda-action' will be bound to this key. The
  1933. default is the character `k' because we use the same key in the agenda."
  1934. :group 'org-agenda
  1935. :type 'sexp)
  1936. (eval-after-load "calendar"
  1937. '(progn
  1938. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1939. 'org-calendar-goto-agenda)
  1940. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1941. 'org-agenda-action)))
  1942. (defgroup org-latex nil
  1943. "Options for embedding LaTeX code into Org-mode."
  1944. :tag "Org LaTeX"
  1945. :group 'org)
  1946. (defcustom org-format-latex-options
  1947. '(:foreground default :background default :scale 1.0
  1948. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1949. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  1950. "Options for creating images from LaTeX fragments.
  1951. This is a property list with the following properties:
  1952. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1953. `default' means use the foreground of the default face.
  1954. :background the background color, or \"Transparent\".
  1955. `default' means use the background of the default face.
  1956. :scale a scaling factor for the size of the images.
  1957. :html-foreground, :html-background, :html-scale
  1958. the same numbers for HTML export.
  1959. :matchers a list indicating which matchers should be used to
  1960. find LaTeX fragments. Valid members of this list are:
  1961. \"begin\" find environments
  1962. \"$1\" find single characters surrounded by $.$
  1963. \"$\" find math expressions surrounded by $...$
  1964. \"$$\" find math expressions surrounded by $$....$$
  1965. \"\\(\" find math expressions surrounded by \\(...\\)
  1966. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1967. :group 'org-latex
  1968. :type 'plist)
  1969. (defcustom org-format-latex-header "\\documentclass{article}
  1970. \\usepackage{fullpage} % do not remove
  1971. \\usepackage{amssymb}
  1972. \\usepackage[usenames]{color}
  1973. \\usepackage{amsmath}
  1974. \\usepackage{latexsym}
  1975. \\usepackage[mathscr]{eucal}
  1976. \\pagestyle{empty} % do not remove"
  1977. "The document header used for processing LaTeX fragments."
  1978. :group 'org-latex
  1979. :type 'string)
  1980. (defgroup org-font-lock nil
  1981. "Font-lock settings for highlighting in Org-mode."
  1982. :tag "Org Font Lock"
  1983. :group 'org)
  1984. (defcustom org-level-color-stars-only nil
  1985. "Non-nil means fontify only the stars in each headline.
  1986. When nil, the entire headline is fontified.
  1987. Changing it requires restart of `font-lock-mode' to become effective
  1988. also in regions already fontified."
  1989. :group 'org-font-lock
  1990. :type 'boolean)
  1991. (defcustom org-hide-leading-stars nil
  1992. "Non-nil means, hide the first N-1 stars in a headline.
  1993. This works by using the face `org-hide' for these stars. This
  1994. face is white for a light background, and black for a dark
  1995. background. You may have to customize the face `org-hide' to
  1996. make this work.
  1997. Changing it requires restart of `font-lock-mode' to become effective
  1998. also in regions already fontified.
  1999. You may also set this on a per-file basis by adding one of the following
  2000. lines to the buffer:
  2001. #+STARTUP: hidestars
  2002. #+STARTUP: showstars"
  2003. :group 'org-font-lock
  2004. :type 'boolean)
  2005. (defcustom org-fontify-done-headline nil
  2006. "Non-nil means, change the face of a headline if it is marked DONE.
  2007. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2008. When this is non-nil, the headline after the keyword is set to the
  2009. `org-headline-done' as an additional indication."
  2010. :group 'org-font-lock
  2011. :type 'boolean)
  2012. (defcustom org-fontify-emphasized-text t
  2013. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2014. Changing this variable requires a restart of Emacs to take effect."
  2015. :group 'org-font-lock
  2016. :type 'boolean)
  2017. (defcustom org-highlight-latex-fragments-and-specials nil
  2018. "Non-nil means, fontify what is treated specially by the exporters."
  2019. :group 'org-font-lock
  2020. :type 'boolean)
  2021. (defcustom org-hide-emphasis-markers nil
  2022. "Non-nil mean font-lock should hide the emphasis marker characters."
  2023. :group 'org-font-lock
  2024. :type 'boolean)
  2025. (defvar org-emph-re nil
  2026. "Regular expression for matching emphasis.")
  2027. (defvar org-verbatim-re nil
  2028. "Regular expression for matching verbatim text.")
  2029. (defvar org-emphasis-regexp-components) ; defined just below
  2030. (defvar org-emphasis-alist) ; defined just below
  2031. (defun org-set-emph-re (var val)
  2032. "Set variable and compute the emphasis regular expression."
  2033. (set var val)
  2034. (when (and (boundp 'org-emphasis-alist)
  2035. (boundp 'org-emphasis-regexp-components)
  2036. org-emphasis-alist org-emphasis-regexp-components)
  2037. (let* ((e org-emphasis-regexp-components)
  2038. (pre (car e))
  2039. (post (nth 1 e))
  2040. (border (nth 2 e))
  2041. (body (nth 3 e))
  2042. (nl (nth 4 e))
  2043. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  2044. (body1 (concat body "*?"))
  2045. (markers (mapconcat 'car org-emphasis-alist ""))
  2046. (vmarkers (mapconcat
  2047. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2048. org-emphasis-alist "")))
  2049. ;; make sure special characters appear at the right position in the class
  2050. (if (string-match "\\^" markers)
  2051. (setq markers (concat (replace-match "" t t markers) "^")))
  2052. (if (string-match "-" markers)
  2053. (setq markers (concat (replace-match "" t t markers) "-")))
  2054. (if (string-match "\\^" vmarkers)
  2055. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2056. (if (string-match "-" vmarkers)
  2057. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2058. (if (> nl 0)
  2059. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2060. (int-to-string nl) "\\}")))
  2061. ;; Make the regexp
  2062. (setq org-emph-re
  2063. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  2064. "\\("
  2065. "\\([" markers "]\\)"
  2066. "\\("
  2067. "[^" border "]\\|"
  2068. "[^" border (if (and nil stacked) markers) "]"
  2069. body1
  2070. "[^" border (if (and nil stacked) markers) "]"
  2071. "\\)"
  2072. "\\3\\)"
  2073. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  2074. (setq org-verbatim-re
  2075. (concat "\\([" pre "]\\|^\\)"
  2076. "\\("
  2077. "\\([" vmarkers "]\\)"
  2078. "\\("
  2079. "[^" border "]\\|"
  2080. "[^" border "]"
  2081. body1
  2082. "[^" border "]"
  2083. "\\)"
  2084. "\\3\\)"
  2085. "\\([" post "]\\|$\\)")))))
  2086. (defcustom org-emphasis-regexp-components
  2087. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2088. "Components used to build the regular expression for emphasis.
  2089. This is a list with 6 entries. Terminology: In an emphasis string
  2090. like \" *strong word* \", we call the initial space PREMATCH, the final
  2091. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2092. and \"trong wor\" is the body. The different components in this variable
  2093. specify what is allowed/forbidden in each part:
  2094. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2095. post Chars allowed as postmatch. End of line will be allowed too.
  2096. border The chars *forbidden* as border characters.
  2097. body-regexp A regexp like \".\" to match a body character. Don't use
  2098. non-shy groups here, and don't allow newline here.
  2099. newline The maximum number of newlines allowed in an emphasis exp.
  2100. Use customize to modify this, or restart Emacs after changing it."
  2101. :group 'org-font-lock
  2102. :set 'org-set-emph-re
  2103. :type '(list
  2104. (sexp :tag "Allowed chars in pre ")
  2105. (sexp :tag "Allowed chars in post ")
  2106. (sexp :tag "Forbidden chars in border ")
  2107. (sexp :tag "Regexp for body ")
  2108. (integer :tag "number of newlines allowed")
  2109. (option (boolean :tag "Please ignore this button"))))
  2110. (defcustom org-emphasis-alist
  2111. `(("*" bold "<b>" "</b>")
  2112. ("/" italic "<i>" "</i>")
  2113. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2114. ("=" org-code "<code>" "</code>" verbatim)
  2115. ("~" org-verbatim "<code>" "</code>" verbatim)
  2116. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2117. "<del>" "</del>")
  2118. )
  2119. "Special syntax for emphasized text.
  2120. Text starting and ending with a special character will be emphasized, for
  2121. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2122. characters, the face to be used by font-lock for highlighting in Org-mode
  2123. Emacs buffers, and the HTML tags to be used for this.
  2124. Use customize to modify this, or restart Emacs after changing it."
  2125. :group 'org-font-lock
  2126. :set 'org-set-emph-re
  2127. :type '(repeat
  2128. (list
  2129. (string :tag "Marker character")
  2130. (choice
  2131. (face :tag "Font-lock-face")
  2132. (plist :tag "Face property list"))
  2133. (string :tag "HTML start tag")
  2134. (string :tag "HTML end tag")
  2135. (option (const verbatim)))))
  2136. ;;; Miscellaneous options
  2137. (defgroup org-completion nil
  2138. "Completion in Org-mode."
  2139. :tag "Org Completion"
  2140. :group 'org)
  2141. (defcustom org-completion-use-ido nil
  2142. "Non-nil means, use ido completion wherever possible.
  2143. Note that `ido-mode' must be active for this variable to be relevant.
  2144. If you decide to turn this variable on, you might well want to turn off
  2145. `org-outline-path-complete-in-steps'."
  2146. :group 'org-completion
  2147. :type 'boolean)
  2148. (defcustom org-completion-fallback-command 'hippie-expand
  2149. "The expansion command called by \\[org-complete] in normal context.
  2150. Normal means, no org-mode-specific context."
  2151. :group 'org-completion
  2152. :type 'function)
  2153. ;;; Functions and variables from ther packages
  2154. ;; Declared here to avoid compiler warnings
  2155. ;; XEmacs only
  2156. (defvar outline-mode-menu-heading)
  2157. (defvar outline-mode-menu-show)
  2158. (defvar outline-mode-menu-hide)
  2159. (defvar zmacs-regions) ; XEmacs regions
  2160. ;; Emacs only
  2161. (defvar mark-active)
  2162. ;; Various packages
  2163. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2164. (declare-function calendar-forward-day "cal-move" (arg))
  2165. (declare-function calendar-goto-date "cal-move" (date))
  2166. (declare-function calendar-goto-today "cal-move" ())
  2167. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2168. (defvar calc-embedded-close-formula)
  2169. (defvar calc-embedded-open-formula)
  2170. (declare-function cdlatex-tab "ext:cdlatex" ())
  2171. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2172. (defvar font-lock-unfontify-region-function)
  2173. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2174. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2175. (defvar iswitchb-temp-buflist)
  2176. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2177. (declare-function org-agenda-skip "org-agenda" ())
  2178. (declare-function org-format-agenda-item "org-agenda"
  2179. (extra txt &optional category tags dotime noprefix remove-re))
  2180. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2181. (declare-function org-agenda-change-all-lines "org-agenda"
  2182. (newhead hdmarker &optional fixface just-this))
  2183. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2184. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2185. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2186. (beg end))
  2187. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2188. (declare-function parse-time-string "parse-time" (string))
  2189. (declare-function remember "remember" (&optional initial))
  2190. (declare-function remember-buffer-desc "remember" ())
  2191. (declare-function remember-finalize "remember" ())
  2192. (defvar remember-save-after-remembering)
  2193. (defvar remember-data-file)
  2194. (defvar remember-register)
  2195. (defvar remember-buffer)
  2196. (defvar remember-handler-functions)
  2197. (defvar remember-annotation-functions)
  2198. (defvar texmathp-why)
  2199. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2200. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2201. (defvar w3m-current-url)
  2202. (defvar w3m-current-title)
  2203. (defvar org-latex-regexps)
  2204. ;;; Autoload and prepare some org modules
  2205. ;; Some table stuff that needs to be defined here, because it is used
  2206. ;; by the functions setting up org-mode or checking for table context.
  2207. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2208. "Detects an org-type or table-type table.")
  2209. (defconst org-table-line-regexp "^[ \t]*|"
  2210. "Detects an org-type table line.")
  2211. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2212. "Detects an org-type table line.")
  2213. (defconst org-table-hline-regexp "^[ \t]*|-"
  2214. "Detects an org-type table hline.")
  2215. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2216. "Detects a table-type table hline.")
  2217. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2218. "Searching from within a table (any type) this finds the first line
  2219. outside the table.")
  2220. ;; Autoload the functions in org-table.el that are needed by functions here.
  2221. (eval-and-compile
  2222. (org-autoload "org-table"
  2223. '(org-table-align org-table-begin org-table-blank-field
  2224. org-table-convert org-table-convert-region org-table-copy-down
  2225. org-table-copy-region org-table-create
  2226. org-table-create-or-convert-from-region
  2227. org-table-create-with-table.el org-table-current-dline
  2228. org-table-cut-region org-table-delete-column org-table-edit-field
  2229. org-table-edit-formulas org-table-end org-table-eval-formula
  2230. org-table-export org-table-field-info
  2231. org-table-get-stored-formulas org-table-goto-column
  2232. org-table-hline-and-move org-table-import org-table-insert-column
  2233. org-table-insert-hline org-table-insert-row org-table-iterate
  2234. org-table-justify-field-maybe org-table-kill-row
  2235. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2236. org-table-move-column org-table-move-column-left
  2237. org-table-move-column-right org-table-move-row
  2238. org-table-move-row-down org-table-move-row-up
  2239. org-table-next-field org-table-next-row org-table-paste-rectangle
  2240. org-table-previous-field org-table-recalculate
  2241. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2242. org-table-toggle-coordinate-overlays
  2243. org-table-toggle-formula-debugger org-table-wrap-region
  2244. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2245. (defun org-at-table-p (&optional table-type)
  2246. "Return t if the cursor is inside an org-type table.
  2247. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2248. (if org-enable-table-editor
  2249. (save-excursion
  2250. (beginning-of-line 1)
  2251. (looking-at (if table-type org-table-any-line-regexp
  2252. org-table-line-regexp)))
  2253. nil))
  2254. (defsubst org-table-p () (org-at-table-p))
  2255. (defun org-at-table.el-p ()
  2256. "Return t if and only if we are at a table.el table."
  2257. (and (org-at-table-p 'any)
  2258. (save-excursion
  2259. (goto-char (org-table-begin 'any))
  2260. (looking-at org-table1-hline-regexp))))
  2261. (defun org-table-recognize-table.el ()
  2262. "If there is a table.el table nearby, recognize it and move into it."
  2263. (if org-table-tab-recognizes-table.el
  2264. (if (org-at-table.el-p)
  2265. (progn
  2266. (beginning-of-line 1)
  2267. (if (looking-at org-table-dataline-regexp)
  2268. nil
  2269. (if (looking-at org-table1-hline-regexp)
  2270. (progn
  2271. (beginning-of-line 2)
  2272. (if (looking-at org-table-any-border-regexp)
  2273. (beginning-of-line -1)))))
  2274. (if (re-search-forward "|" (org-table-end t) t)
  2275. (progn
  2276. (require 'table)
  2277. (if (table--at-cell-p (point))
  2278. t
  2279. (message "recognizing table.el table...")
  2280. (table-recognize-table)
  2281. (message "recognizing table.el table...done")))
  2282. (error "This should not happen..."))
  2283. t)
  2284. nil)
  2285. nil))
  2286. (defun org-at-table-hline-p ()
  2287. "Return t if the cursor is inside a hline in a table."
  2288. (if org-enable-table-editor
  2289. (save-excursion
  2290. (beginning-of-line 1)
  2291. (looking-at org-table-hline-regexp))
  2292. nil))
  2293. (defvar org-table-clean-did-remove-column nil)
  2294. (defun org-table-map-tables (function)
  2295. "Apply FUNCTION to the start of all tables in the buffer."
  2296. (save-excursion
  2297. (save-restriction
  2298. (widen)
  2299. (goto-char (point-min))
  2300. (while (re-search-forward org-table-any-line-regexp nil t)
  2301. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2302. (beginning-of-line 1)
  2303. (if (looking-at org-table-line-regexp)
  2304. (save-excursion (funcall function)))
  2305. (re-search-forward org-table-any-border-regexp nil 1))))
  2306. (message "Mapping tables: done"))
  2307. ;; Declare and autoload functions from org-exp.el
  2308. (declare-function org-default-export-plist "org-exp")
  2309. (declare-function org-infile-export-plist "org-exp")
  2310. (declare-function org-get-current-options "org-exp")
  2311. (eval-and-compile
  2312. (org-autoload "org-exp"
  2313. '(org-export org-export-as-ascii org-export-visible
  2314. org-insert-export-options-template org-export-as-html-and-open
  2315. org-export-as-html-batch org-export-as-html-to-buffer
  2316. org-replace-region-by-html org-export-region-as-html
  2317. org-export-as-html org-export-icalendar-this-file
  2318. org-export-icalendar-all-agenda-files
  2319. org-table-clean-before-export
  2320. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2321. ;; Declare and autoload functions from org-agenda.el
  2322. (eval-and-compile
  2323. (org-autoload "org-agenda"
  2324. '(org-agenda org-agenda-list org-search-view
  2325. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2326. org-diary org-agenda-to-appt)))
  2327. ;; Autoload org-remember
  2328. (eval-and-compile
  2329. (org-autoload "org-remember"
  2330. '(org-remember-insinuate org-remember-annotation
  2331. org-remember-apply-template org-remember org-remember-handler)))
  2332. ;; Autoload org-clock.el
  2333. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2334. (beg end))
  2335. (declare-function org-clock-update-mode-line "org-clock" ())
  2336. (defvar org-clock-start-time)
  2337. (defvar org-clock-marker (make-marker)
  2338. "Marker recording the last clock-in.")
  2339. (eval-and-compile
  2340. (org-autoload
  2341. "org-clock"
  2342. '(org-clock-in org-clock-out org-clock-cancel
  2343. org-clock-goto org-clock-sum org-clock-display
  2344. org-clock-remove-overlays org-clock-report
  2345. org-clocktable-shift org-dblock-write:clocktable
  2346. org-get-clocktable)))
  2347. (defun org-clock-update-time-maybe ()
  2348. "If this is a CLOCK line, update it and return t.
  2349. Otherwise, return nil."
  2350. (interactive)
  2351. (save-excursion
  2352. (beginning-of-line 1)
  2353. (skip-chars-forward " \t")
  2354. (when (looking-at org-clock-string)
  2355. (let ((re (concat "[ \t]*" org-clock-string
  2356. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2357. "\\([ \t]*=>.*\\)?\\)?"))
  2358. ts te h m s neg)
  2359. (cond
  2360. ((not (looking-at re))
  2361. nil)
  2362. ((not (match-end 2))
  2363. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2364. (> org-clock-marker (point))
  2365. (<= org-clock-marker (point-at-eol)))
  2366. ;; The clock is running here
  2367. (setq org-clock-start-time
  2368. (apply 'encode-time
  2369. (org-parse-time-string (match-string 1))))
  2370. (org-clock-update-mode-line)))
  2371. (t
  2372. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2373. (end-of-line 1)
  2374. (setq ts (match-string 1)
  2375. te (match-string 3))
  2376. (setq s (- (time-to-seconds
  2377. (apply 'encode-time (org-parse-time-string te)))
  2378. (time-to-seconds
  2379. (apply 'encode-time (org-parse-time-string ts))))
  2380. neg (< s 0)
  2381. s (abs s)
  2382. h (floor (/ s 3600))
  2383. s (- s (* 3600 h))
  2384. m (floor (/ s 60))
  2385. s (- s (* 60 s)))
  2386. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2387. t))))))
  2388. (defun org-check-running-clock ()
  2389. "Check if the current buffer contains the running clock.
  2390. If yes, offer to stop it and to save the buffer with the changes."
  2391. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2392. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2393. (buffer-name))))
  2394. (org-clock-out)
  2395. (when (y-or-n-p "Save changed buffer?")
  2396. (save-buffer))))
  2397. (defun org-clocktable-try-shift (dir n)
  2398. "Check if this line starts a clock table, if yes, shift the time block."
  2399. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2400. (org-clocktable-shift dir n)))
  2401. ;; Autoload org-timer.el
  2402. ;(declare-function org-timer "org-timer")
  2403. (eval-and-compile
  2404. (org-autoload
  2405. "org-timer"
  2406. '(org-timer-start org-timer org-timer-item
  2407. org-timer-change-times-in-region)))
  2408. ;; Autoload archiving code
  2409. ;; The stuff that is needed for cycling and tags has to be defined here.
  2410. (defgroup org-archive nil
  2411. "Options concerning archiving in Org-mode."
  2412. :tag "Org Archive"
  2413. :group 'org-structure)
  2414. (defcustom org-archive-location "%s_archive::"
  2415. "The location where subtrees should be archived.
  2416. The value of this variable is a string, consisting of two parts,
  2417. separated by a double-colon. The first part is a filename and
  2418. the second part is a headline.
  2419. When the filename is omitted, archiving happens in the same file.
  2420. %s in the filename will be replaced by the current file
  2421. name (without the directory part). Archiving to a different file
  2422. is useful to keep archived entries from contributing to the
  2423. Org-mode Agenda.
  2424. The archived entries will be filed as subtrees of the specified
  2425. headline. When the headline is omitted, the subtrees are simply
  2426. filed away at the end of the file, as top-level entries. Also in
  2427. the heading you can use %s to represent the file name, this can be
  2428. useful when using the same archive for a number of different files.
  2429. Here are a few examples:
  2430. \"%s_archive::\"
  2431. If the current file is Projects.org, archive in file
  2432. Projects.org_archive, as top-level trees. This is the default.
  2433. \"::* Archived Tasks\"
  2434. Archive in the current file, under the top-level headline
  2435. \"* Archived Tasks\".
  2436. \"~/org/archive.org::\"
  2437. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2438. \"~/org/archive.org::From %s\"
  2439. Archive in file ~/org/archive.org (absolute path), und headlines
  2440. \"From FILENAME\" where file name is the current file name.
  2441. \"basement::** Finished Tasks\"
  2442. Archive in file ./basement (relative path), as level 3 trees
  2443. below the level 2 heading \"** Finished Tasks\".
  2444. You may set this option on a per-file basis by adding to the buffer a
  2445. line like
  2446. #+ARCHIVE: basement::** Finished Tasks
  2447. You may also define it locally for a subtree by setting an ARCHIVE property
  2448. in the entry. If such a property is found in an entry, or anywhere up
  2449. the hierarchy, it will be used."
  2450. :group 'org-archive
  2451. :type 'string)
  2452. (defcustom org-archive-tag "ARCHIVE"
  2453. "The tag that marks a subtree as archived.
  2454. An archived subtree does not open during visibility cycling, and does
  2455. not contribute to the agenda listings.
  2456. After changing this, font-lock must be restarted in the relevant buffers to
  2457. get the proper fontification."
  2458. :group 'org-archive
  2459. :group 'org-keywords
  2460. :type 'string)
  2461. (defcustom org-agenda-skip-archived-trees t
  2462. "Non-nil means, the agenda will skip any items located in archived trees.
  2463. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2464. variable is no longer recommended, you should leave it at the value t.
  2465. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2466. :group 'org-archive
  2467. :group 'org-agenda-skip
  2468. :type 'boolean)
  2469. (defcustom org-cycle-open-archived-trees nil
  2470. "Non-nil means, `org-cycle' will open archived trees.
  2471. An archived tree is a tree marked with the tag ARCHIVE.
  2472. When nil, archived trees will stay folded. You can still open them with
  2473. normal outline commands like `show-all', but not with the cycling commands."
  2474. :group 'org-archive
  2475. :group 'org-cycle
  2476. :type 'boolean)
  2477. (defcustom org-sparse-tree-open-archived-trees nil
  2478. "Non-nil means sparse tree construction shows matches in archived trees.
  2479. When nil, matches in these trees are highlighted, but the trees are kept in
  2480. collapsed state."
  2481. :group 'org-archive
  2482. :group 'org-sparse-trees
  2483. :type 'boolean)
  2484. (defun org-cycle-hide-archived-subtrees (state)
  2485. "Re-hide all archived subtrees after a visibility state change."
  2486. (when (and (not org-cycle-open-archived-trees)
  2487. (not (memq state '(overview folded))))
  2488. (save-excursion
  2489. (let* ((globalp (memq state '(contents all)))
  2490. (beg (if globalp (point-min) (point)))
  2491. (end (if globalp (point-max) (org-end-of-subtree t))))
  2492. (org-hide-archived-subtrees beg end)
  2493. (goto-char beg)
  2494. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2495. (message "%s" (substitute-command-keys
  2496. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2497. (defun org-force-cycle-archived ()
  2498. "Cycle subtree even if it is archived."
  2499. (interactive)
  2500. (setq this-command 'org-cycle)
  2501. (let ((org-cycle-open-archived-trees t))
  2502. (call-interactively 'org-cycle)))
  2503. (defun org-hide-archived-subtrees (beg end)
  2504. "Re-hide all archived subtrees after a visibility state change."
  2505. (save-excursion
  2506. (let* ((re (concat ":" org-archive-tag ":")))
  2507. (goto-char beg)
  2508. (while (re-search-forward re end t)
  2509. (and (org-on-heading-p) (hide-subtree))
  2510. (org-end-of-subtree t)))))
  2511. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2512. (eval-and-compile
  2513. (org-autoload "org-archive"
  2514. '(org-add-archive-files org-archive-subtree
  2515. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2516. ;; Autoload Column View Code
  2517. (declare-function org-columns-number-to-string "org-colview")
  2518. (declare-function org-columns-get-format-and-top-level "org-colview")
  2519. (declare-function org-columns-compute "org-colview")
  2520. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2521. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2522. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2523. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2524. ;; Autoload ID code
  2525. (declare-function org-id-store-link "org-id")
  2526. (org-autoload "org-id"
  2527. '(org-id-get-create org-id-new org-id-copy org-id-get
  2528. org-id-get-with-outline-path-completion
  2529. org-id-get-with-outline-drilling
  2530. org-id-goto org-id-find org-id-store-link))
  2531. ;;; Variables for pre-computed regular expressions, all buffer local
  2532. (defvar org-drawer-regexp nil
  2533. "Matches first line of a hidden block.")
  2534. (make-variable-buffer-local 'org-drawer-regexp)
  2535. (defvar org-todo-regexp nil
  2536. "Matches any of the TODO state keywords.")
  2537. (make-variable-buffer-local 'org-todo-regexp)
  2538. (defvar org-not-done-regexp nil
  2539. "Matches any of the TODO state keywords except the last one.")
  2540. (make-variable-buffer-local 'org-not-done-regexp)
  2541. (defvar org-todo-line-regexp nil
  2542. "Matches a headline and puts TODO state into group 2 if present.")
  2543. (make-variable-buffer-local 'org-todo-line-regexp)
  2544. (defvar org-complex-heading-regexp nil
  2545. "Matches a headline and puts everything into groups:
  2546. group 1: the stars
  2547. group 2: The todo keyword, maybe
  2548. group 3: Priority cookie
  2549. group 4: True headline
  2550. group 5: Tags")
  2551. (make-variable-buffer-local 'org-complex-heading-regexp)
  2552. (defvar org-todo-line-tags-regexp nil
  2553. "Matches a headline and puts TODO state into group 2 if present.
  2554. Also put tags into group 4 if tags are present.")
  2555. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2556. (defvar org-nl-done-regexp nil
  2557. "Matches newline followed by a headline with the DONE keyword.")
  2558. (make-variable-buffer-local 'org-nl-done-regexp)
  2559. (defvar org-looking-at-done-regexp nil
  2560. "Matches the DONE keyword a point.")
  2561. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2562. (defvar org-ds-keyword-length 12
  2563. "Maximum length of the Deadline and SCHEDULED keywords.")
  2564. (make-variable-buffer-local 'org-ds-keyword-length)
  2565. (defvar org-deadline-regexp nil
  2566. "Matches the DEADLINE keyword.")
  2567. (make-variable-buffer-local 'org-deadline-regexp)
  2568. (defvar org-deadline-time-regexp nil
  2569. "Matches the DEADLINE keyword together with a time stamp.")
  2570. (make-variable-buffer-local 'org-deadline-time-regexp)
  2571. (defvar org-deadline-line-regexp nil
  2572. "Matches the DEADLINE keyword and the rest of the line.")
  2573. (make-variable-buffer-local 'org-deadline-line-regexp)
  2574. (defvar org-scheduled-regexp nil
  2575. "Matches the SCHEDULED keyword.")
  2576. (make-variable-buffer-local 'org-scheduled-regexp)
  2577. (defvar org-scheduled-time-regexp nil
  2578. "Matches the SCHEDULED keyword together with a time stamp.")
  2579. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2580. (defvar org-closed-time-regexp nil
  2581. "Matches the CLOSED keyword together with a time stamp.")
  2582. (make-variable-buffer-local 'org-closed-time-regexp)
  2583. (defvar org-keyword-time-regexp nil
  2584. "Matches any of the 4 keywords, together with the time stamp.")
  2585. (make-variable-buffer-local 'org-keyword-time-regexp)
  2586. (defvar org-keyword-time-not-clock-regexp nil
  2587. "Matches any of the 3 keywords, together with the time stamp.")
  2588. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2589. (defvar org-maybe-keyword-time-regexp nil
  2590. "Matches a timestamp, possibly preceeded by a keyword.")
  2591. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2592. (defvar org-planning-or-clock-line-re nil
  2593. "Matches a line with planning or clock info.")
  2594. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2595. (defconst org-plain-time-of-day-regexp
  2596. (concat
  2597. "\\(\\<[012]?[0-9]"
  2598. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2599. "\\(--?"
  2600. "\\(\\<[012]?[0-9]"
  2601. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2602. "\\)?")
  2603. "Regular expression to match a plain time or time range.
  2604. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2605. groups carry important information:
  2606. 0 the full match
  2607. 1 the first time, range or not
  2608. 8 the second time, if it is a range.")
  2609. (defconst org-plain-time-extension-regexp
  2610. (concat
  2611. "\\(\\<[012]?[0-9]"
  2612. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2613. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2614. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2615. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2616. groups carry important information:
  2617. 0 the full match
  2618. 7 hours of duration
  2619. 9 minutes of duration")
  2620. (defconst org-stamp-time-of-day-regexp
  2621. (concat
  2622. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2623. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2624. "\\(--?"
  2625. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2626. "Regular expression to match a timestamp time or time range.
  2627. After a match, the following groups carry important information:
  2628. 0 the full match
  2629. 1 date plus weekday, for backreferencing to make sure both times on same day
  2630. 2 the first time, range or not
  2631. 4 the second time, if it is a range.")
  2632. (defconst org-startup-options
  2633. '(("fold" org-startup-folded t)
  2634. ("overview" org-startup-folded t)
  2635. ("nofold" org-startup-folded nil)
  2636. ("showall" org-startup-folded nil)
  2637. ("content" org-startup-folded content)
  2638. ("hidestars" org-hide-leading-stars t)
  2639. ("showstars" org-hide-leading-stars nil)
  2640. ("odd" org-odd-levels-only t)
  2641. ("oddeven" org-odd-levels-only nil)
  2642. ("align" org-startup-align-all-tables t)
  2643. ("noalign" org-startup-align-all-tables nil)
  2644. ("customtime" org-display-custom-times t)
  2645. ("logdone" org-log-done time)
  2646. ("lognotedone" org-log-done note)
  2647. ("nologdone" org-log-done nil)
  2648. ("lognoteclock-out" org-log-note-clock-out t)
  2649. ("nolognoteclock-out" org-log-note-clock-out nil)
  2650. ("logrepeat" org-log-repeat state)
  2651. ("lognoterepeat" org-log-repeat note)
  2652. ("nologrepeat" org-log-repeat nil)
  2653. ("fninline" org-footnote-define-inline t)
  2654. ("nofninline" org-footnote-define-inline nil)
  2655. ("fnlocal" org-footnote-section nil)
  2656. ("fnauto" org-footnote-auto-label t)
  2657. ("fnprompt" org-footnote-auto-label nil)
  2658. ("fnconfirm" org-footnote-auto-label confirm)
  2659. ("fnplain" org-footnote-auto-label plain)
  2660. ("constcgs" constants-unit-system cgs)
  2661. ("constSI" constants-unit-system SI))
  2662. "Variable associated with STARTUP options for org-mode.
  2663. Each element is a list of three items: The startup options as written
  2664. in the #+STARTUP line, the corresponding variable, and the value to
  2665. set this variable to if the option is found. An optional forth element PUSH
  2666. means to push this value onto the list in the variable.")
  2667. (defun org-set-regexps-and-options ()
  2668. "Precompute regular expressions for current buffer."
  2669. (when (org-mode-p)
  2670. (org-set-local 'org-todo-kwd-alist nil)
  2671. (org-set-local 'org-todo-key-alist nil)
  2672. (org-set-local 'org-todo-key-trigger nil)
  2673. (org-set-local 'org-todo-keywords-1 nil)
  2674. (org-set-local 'org-done-keywords nil)
  2675. (org-set-local 'org-todo-heads nil)
  2676. (org-set-local 'org-todo-sets nil)
  2677. (org-set-local 'org-todo-log-states nil)
  2678. (org-set-local 'org-file-properties nil)
  2679. (org-set-local 'org-file-tags nil)
  2680. (let ((re (org-make-options-regexp
  2681. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2682. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2683. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2684. (splitre "[ \t]+")
  2685. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2686. tail sep kws1 prio props ftags drawers
  2687. ext-setup-or-nil setup-contents (start 0))
  2688. (save-excursion
  2689. (save-restriction
  2690. (widen)
  2691. (goto-char (point-min))
  2692. (while (or (and ext-setup-or-nil
  2693. (string-match re ext-setup-or-nil start)
  2694. (setq start (match-end 0)))
  2695. (and (setq ext-setup-or-nil nil start 0)
  2696. (re-search-forward re nil t)))
  2697. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2698. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2699. (cond
  2700. ((equal key "CATEGORY")
  2701. (if (string-match "[ \t]+$" value)
  2702. (setq value (replace-match "" t t value)))
  2703. (setq cat value))
  2704. ((member key '("SEQ_TODO" "TODO"))
  2705. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2706. ((equal key "TYP_TODO")
  2707. (push (cons 'type (org-split-string value splitre)) kwds))
  2708. ((equal key "TAGS")
  2709. (setq tags (append tags (org-split-string value splitre))))
  2710. ((equal key "COLUMNS")
  2711. (org-set-local 'org-columns-default-format value))
  2712. ((equal key "LINK")
  2713. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2714. (push (cons (match-string 1 value)
  2715. (org-trim (match-string 2 value)))
  2716. links)))
  2717. ((equal key "PRIORITIES")
  2718. (setq prio (org-split-string value " +")))
  2719. ((equal key "PROPERTY")
  2720. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2721. (push (cons (match-string 1 value) (match-string 2 value))
  2722. props)))
  2723. ((equal key "FILETAGS")
  2724. (when (string-match "\\S-" value)
  2725. (setq ftags
  2726. (append
  2727. ftags
  2728. (apply 'append
  2729. (mapcar (lambda (x) (org-split-string x ":"))
  2730. (org-split-string value)))))))
  2731. ((equal key "DRAWERS")
  2732. (setq drawers (org-split-string value splitre)))
  2733. ((equal key "CONSTANTS")
  2734. (setq const (append const (org-split-string value splitre))))
  2735. ((equal key "STARTUP")
  2736. (let ((opts (org-split-string value splitre))
  2737. l var val)
  2738. (while (setq l (pop opts))
  2739. (when (setq l (assoc l org-startup-options))
  2740. (setq var (nth 1 l) val (nth 2 l))
  2741. (if (not (nth 3 l))
  2742. (set (make-local-variable var) val)
  2743. (if (not (listp (symbol-value var)))
  2744. (set (make-local-variable var) nil))
  2745. (set (make-local-variable var) (symbol-value var))
  2746. (add-to-list var val))))))
  2747. ((equal key "ARCHIVE")
  2748. (string-match " *$" value)
  2749. (setq arch (replace-match "" t t value))
  2750. (remove-text-properties 0 (length arch)
  2751. '(face t fontified t) arch))
  2752. ((equal key "SETUPFILE")
  2753. (setq setup-contents (org-file-contents
  2754. (expand-file-name
  2755. (org-remove-double-quotes value))
  2756. 'noerror))
  2757. (if (not ext-setup-or-nil)
  2758. (setq ext-setup-or-nil setup-contents start 0)
  2759. (setq ext-setup-or-nil
  2760. (concat (substring ext-setup-or-nil 0 start)
  2761. "\n" setup-contents "\n"
  2762. (substring ext-setup-or-nil start)))))
  2763. ))))
  2764. (when cat
  2765. (org-set-local 'org-category (intern cat))
  2766. (push (cons "CATEGORY" cat) props))
  2767. (when prio
  2768. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2769. (setq prio (mapcar 'string-to-char prio))
  2770. (org-set-local 'org-highest-priority (nth 0 prio))
  2771. (org-set-local 'org-lowest-priority (nth 1 prio))
  2772. (org-set-local 'org-default-priority (nth 2 prio)))
  2773. (and props (org-set-local 'org-file-properties (nreverse props)))
  2774. (and ftags (org-set-local 'org-file-tags ftags))
  2775. (and drawers (org-set-local 'org-drawers drawers))
  2776. (and arch (org-set-local 'org-archive-location arch))
  2777. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2778. ;; Process the TODO keywords
  2779. (unless kwds
  2780. ;; Use the global values as if they had been given locally.
  2781. (setq kwds (default-value 'org-todo-keywords))
  2782. (if (stringp (car kwds))
  2783. (setq kwds (list (cons org-todo-interpretation
  2784. (default-value 'org-todo-keywords)))))
  2785. (setq kwds (reverse kwds)))
  2786. (setq kwds (nreverse kwds))
  2787. (let (inter kws kw)
  2788. (while (setq kws (pop kwds))
  2789. (setq inter (pop kws) sep (member "|" kws)
  2790. kws0 (delete "|" (copy-sequence kws))
  2791. kwsa nil
  2792. kws1 (mapcar
  2793. (lambda (x)
  2794. ;; 1 2
  2795. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2796. (progn
  2797. (setq kw (match-string 1 x)
  2798. key (and (match-end 2) (match-string 2 x))
  2799. log (org-extract-log-state-settings x))
  2800. (push (cons kw (and key (string-to-char key))) kwsa)
  2801. (and log (push log org-todo-log-states))
  2802. kw)
  2803. (error "Invalid TODO keyword %s" x)))
  2804. kws0)
  2805. kwsa (if kwsa (append '((:startgroup))
  2806. (nreverse kwsa)
  2807. '((:endgroup))))
  2808. hw (car kws1)
  2809. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2810. tail (list inter hw (car dws) (org-last dws)))
  2811. (add-to-list 'org-todo-heads hw 'append)
  2812. (push kws1 org-todo-sets)
  2813. (setq org-done-keywords (append org-done-keywords dws nil))
  2814. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2815. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2816. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2817. (setq org-todo-sets (nreverse org-todo-sets)
  2818. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2819. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2820. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2821. ;; Process the constants
  2822. (when const
  2823. (let (e cst)
  2824. (while (setq e (pop const))
  2825. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2826. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2827. (setq org-table-formula-constants-local cst)))
  2828. ;; Process the tags.
  2829. (when tags
  2830. (let (e tgs)
  2831. (while (setq e (pop tags))
  2832. (cond
  2833. ((equal e "{") (push '(:startgroup) tgs))
  2834. ((equal e "}") (push '(:endgroup) tgs))
  2835. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2836. (push (cons (match-string 1 e)
  2837. (string-to-char (match-string 2 e)))
  2838. tgs))
  2839. (t (push (list e) tgs))))
  2840. (org-set-local 'org-tag-alist nil)
  2841. (while (setq e (pop tgs))
  2842. (or (and (stringp (car e))
  2843. (assoc (car e) org-tag-alist))
  2844. (push e org-tag-alist)))))
  2845. ;; Compute the regular expressions and other local variables
  2846. (if (not org-done-keywords)
  2847. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2848. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2849. (length org-scheduled-string)
  2850. (length org-clock-string)
  2851. (length org-closed-string)))
  2852. org-drawer-regexp
  2853. (concat "^[ \t]*:\\("
  2854. (mapconcat 'regexp-quote org-drawers "\\|")
  2855. "\\):[ \t]*$")
  2856. org-not-done-keywords
  2857. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2858. org-todo-regexp
  2859. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2860. "\\|") "\\)\\>")
  2861. org-not-done-regexp
  2862. (concat "\\<\\("
  2863. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2864. "\\)\\>")
  2865. org-todo-line-regexp
  2866. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2867. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2868. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2869. org-complex-heading-regexp
  2870. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2871. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2872. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2873. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2874. org-nl-done-regexp
  2875. (concat "\n\\*+[ \t]+"
  2876. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2877. "\\)" "\\>")
  2878. org-todo-line-tags-regexp
  2879. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2880. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2881. (org-re
  2882. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2883. org-looking-at-done-regexp
  2884. (concat "^" "\\(?:"
  2885. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2886. "\\>")
  2887. org-deadline-regexp (concat "\\<" org-deadline-string)
  2888. org-deadline-time-regexp
  2889. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2890. org-deadline-line-regexp
  2891. (concat "\\<\\(" org-deadline-string "\\).*")
  2892. org-scheduled-regexp
  2893. (concat "\\<" org-scheduled-string)
  2894. org-scheduled-time-regexp
  2895. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2896. org-closed-time-regexp
  2897. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2898. org-keyword-time-regexp
  2899. (concat "\\<\\(" org-scheduled-string
  2900. "\\|" org-deadline-string
  2901. "\\|" org-closed-string
  2902. "\\|" org-clock-string "\\)"
  2903. " *[[<]\\([^]>]+\\)[]>]")
  2904. org-keyword-time-not-clock-regexp
  2905. (concat "\\<\\(" org-scheduled-string
  2906. "\\|" org-deadline-string
  2907. "\\|" org-closed-string
  2908. "\\)"
  2909. " *[[<]\\([^]>]+\\)[]>]")
  2910. org-maybe-keyword-time-regexp
  2911. (concat "\\(\\<\\(" org-scheduled-string
  2912. "\\|" org-deadline-string
  2913. "\\|" org-closed-string
  2914. "\\|" org-clock-string "\\)\\)?"
  2915. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2916. org-planning-or-clock-line-re
  2917. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2918. "\\|" org-deadline-string
  2919. "\\|" org-closed-string "\\|" org-clock-string
  2920. "\\)\\>\\)")
  2921. )
  2922. (org-compute-latex-and-specials-regexp)
  2923. (org-set-font-lock-defaults))))
  2924. (defun org-file-contents (file &optional noerror)
  2925. "Return the contents of FILE, as a string."
  2926. (if (or (not file)
  2927. (not (file-readable-p file)))
  2928. (if noerror
  2929. (progn
  2930. (message "Cannot read file %s" file)
  2931. (ding) (sit-for 2)
  2932. "")
  2933. (error "Cannot read file %s" file))
  2934. (with-temp-buffer
  2935. (insert-file-contents file)
  2936. (buffer-string))))
  2937. (defun org-extract-log-state-settings (x)
  2938. "Extract the log state setting from a TODO keyword string.
  2939. This will extract info from a string like \"WAIT(w@/!)\"."
  2940. (let (kw key log1 log2)
  2941. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2942. (setq kw (match-string 1 x)
  2943. key (and (match-end 2) (match-string 2 x))
  2944. log1 (and (match-end 3) (match-string 3 x))
  2945. log2 (and (match-end 4) (match-string 4 x)))
  2946. (and (or log1 log2)
  2947. (list kw
  2948. (and log1 (if (equal log1 "!") 'time 'note))
  2949. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2950. (defun org-remove-keyword-keys (list)
  2951. "Remove a pair of parenthesis at the end of each string in LIST."
  2952. (mapcar (lambda (x)
  2953. (if (string-match "(.*)$" x)
  2954. (substring x 0 (match-beginning 0))
  2955. x))
  2956. list))
  2957. ;; FIXME: this could be done much better, using second characters etc.
  2958. (defun org-assign-fast-keys (alist)
  2959. "Assign fast keys to a keyword-key alist.
  2960. Respect keys that are already there."
  2961. (let (new e k c c1 c2 (char ?a))
  2962. (while (setq e (pop alist))
  2963. (cond
  2964. ((equal e '(:startgroup)) (push e new))
  2965. ((equal e '(:endgroup)) (push e new))
  2966. (t
  2967. (setq k (car e) c2 nil)
  2968. (if (cdr e)
  2969. (setq c (cdr e))
  2970. ;; automatically assign a character.
  2971. (setq c1 (string-to-char
  2972. (downcase (substring
  2973. k (if (= (string-to-char k) ?@) 1 0)))))
  2974. (if (or (rassoc c1 new) (rassoc c1 alist))
  2975. (while (or (rassoc char new) (rassoc char alist))
  2976. (setq char (1+ char)))
  2977. (setq c2 c1))
  2978. (setq c (or c2 char)))
  2979. (push (cons k c) new))))
  2980. (nreverse new)))
  2981. ;;; Some variables used in various places
  2982. (defvar org-window-configuration nil
  2983. "Used in various places to store a window configuration.")
  2984. (defvar org-finish-function nil
  2985. "Function to be called when `C-c C-c' is used.
  2986. This is for getting out of special buffers like remember.")
  2987. ;; FIXME: Occasionally check by commenting these, to make sure
  2988. ;; no other functions uses these, forgetting to let-bind them.
  2989. (defvar entry)
  2990. (defvar state)
  2991. (defvar last-state)
  2992. (defvar date)
  2993. (defvar description)
  2994. ;; Defined somewhere in this file, but used before definition.
  2995. (defvar org-html-entities)
  2996. (defvar org-struct-menu)
  2997. (defvar org-org-menu)
  2998. (defvar org-tbl-menu)
  2999. (defvar org-agenda-keymap)
  3000. ;;;; Define the Org-mode
  3001. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3002. (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."))
  3003. ;; We use a before-change function to check if a table might need
  3004. ;; an update.
  3005. (defvar org-table-may-need-update t
  3006. "Indicates that a table might need an update.
  3007. This variable is set by `org-before-change-function'.
  3008. `org-table-align' sets it back to nil.")
  3009. (defun org-before-change-function (beg end)
  3010. "Every change indicates that a table might need an update."
  3011. (setq org-table-may-need-update t))
  3012. (defvar org-mode-map)
  3013. (defvar org-mode-hook nil
  3014. "Mode hook for Org-mode, run after the mode was turned on.")
  3015. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3016. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3017. (defvar org-table-buffer-is-an nil)
  3018. (defconst org-outline-regexp "\\*+ ")
  3019. ;;;###autoload
  3020. (define-derived-mode org-mode outline-mode "Org"
  3021. "Outline-based notes management and organizer, alias
  3022. \"Carsten's outline-mode for keeping track of everything.\"
  3023. Org-mode develops organizational tasks around a NOTES file which
  3024. contains information about projects as plain text. Org-mode is
  3025. implemented on top of outline-mode, which is ideal to keep the content
  3026. of large files well structured. It supports ToDo items, deadlines and
  3027. time stamps, which magically appear in the diary listing of the Emacs
  3028. calendar. Tables are easily created with a built-in table editor.
  3029. Plain text URL-like links connect to websites, emails (VM), Usenet
  3030. messages (Gnus), BBDB entries, and any files related to the project.
  3031. For printing and sharing of notes, an Org-mode file (or a part of it)
  3032. can be exported as a structured ASCII or HTML file.
  3033. The following commands are available:
  3034. \\{org-mode-map}"
  3035. ;; Get rid of Outline menus, they are not needed
  3036. ;; Need to do this here because define-derived-mode sets up
  3037. ;; the keymap so late. Still, it is a waste to call this each time
  3038. ;; we switch another buffer into org-mode.
  3039. (if (featurep 'xemacs)
  3040. (when (boundp 'outline-mode-menu-heading)
  3041. ;; Assume this is Greg's port, it used easymenu
  3042. (easy-menu-remove outline-mode-menu-heading)
  3043. (easy-menu-remove outline-mode-menu-show)
  3044. (easy-menu-remove outline-mode-menu-hide))
  3045. (define-key org-mode-map [menu-bar headings] 'undefined)
  3046. (define-key org-mode-map [menu-bar hide] 'undefined)
  3047. (define-key org-mode-map [menu-bar show] 'undefined))
  3048. (org-load-modules-maybe)
  3049. (easy-menu-add org-org-menu)
  3050. (easy-menu-add org-tbl-menu)
  3051. (org-install-agenda-files-menu)
  3052. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3053. (org-add-to-invisibility-spec '(org-cwidth))
  3054. (when (featurep 'xemacs)
  3055. (org-set-local 'line-move-ignore-invisible t))
  3056. (org-set-local 'outline-regexp org-outline-regexp)
  3057. (org-set-local 'outline-level 'org-outline-level)
  3058. (when (and org-ellipsis
  3059. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3060. (fboundp 'make-glyph-code))
  3061. (unless org-display-table
  3062. (setq org-display-table (make-display-table)))
  3063. (set-display-table-slot
  3064. org-display-table 4
  3065. (vconcat (mapcar
  3066. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3067. org-ellipsis)))
  3068. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3069. (setq buffer-display-table org-display-table))
  3070. (org-set-regexps-and-options)
  3071. (when (and org-tag-faces (not org-tags-special-faces-re))
  3072. ;; tag faces set outside customize.... force initialization.
  3073. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3074. ;; Calc embedded
  3075. (org-set-local 'calc-embedded-open-mode "# ")
  3076. (modify-syntax-entry ?# "<")
  3077. (modify-syntax-entry ?@ "w")
  3078. (if org-startup-truncated (setq truncate-lines t))
  3079. (org-set-local 'font-lock-unfontify-region-function
  3080. 'org-unfontify-region)
  3081. ;; Activate before-change-function
  3082. (org-set-local 'org-table-may-need-update t)
  3083. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3084. 'local)
  3085. ;; Check for running clock before killing a buffer
  3086. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3087. ;; Paragraphs and auto-filling
  3088. (org-set-autofill-regexps)
  3089. (setq indent-line-function 'org-indent-line-function)
  3090. (org-update-radio-target-regexp)
  3091. ;; Comment characters
  3092. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3093. (org-set-local 'comment-padding " ")
  3094. ;; Align options lines
  3095. (org-set-local
  3096. 'align-mode-rules-list
  3097. '((org-in-buffer-settings
  3098. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3099. (modes . '(org-mode)))))
  3100. ;; Imenu
  3101. (org-set-local 'imenu-create-index-function
  3102. 'org-imenu-get-tree)
  3103. ;; Make isearch reveal context
  3104. (if (or (featurep 'xemacs)
  3105. (not (boundp 'outline-isearch-open-invisible-function)))
  3106. ;; Emacs 21 and XEmacs make use of the hook
  3107. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3108. ;; Emacs 22 deals with this through a special variable
  3109. (org-set-local 'outline-isearch-open-invisible-function
  3110. (lambda (&rest ignore) (org-show-context 'isearch))))
  3111. ;; If empty file that did not turn on org-mode automatically, make it to.
  3112. (if (and org-insert-mode-line-in-empty-file
  3113. (interactive-p)
  3114. (= (point-min) (point-max)))
  3115. (insert "# -*- mode: org -*-\n\n"))
  3116. (unless org-inhibit-startup
  3117. (when org-startup-align-all-tables
  3118. (let ((bmp (buffer-modified-p)))
  3119. (org-table-map-tables 'org-table-align)
  3120. (set-buffer-modified-p bmp)))
  3121. (org-set-startup-visibility)))
  3122. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3123. (defun org-current-time ()
  3124. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3125. (if (> (car org-time-stamp-rounding-minutes) 1)
  3126. (let ((r (car org-time-stamp-rounding-minutes))
  3127. (time (decode-time)))
  3128. (apply 'encode-time
  3129. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3130. (nthcdr 2 time))))
  3131. (current-time)))
  3132. ;;;; Font-Lock stuff, including the activators
  3133. (defvar org-mouse-map (make-sparse-keymap))
  3134. (org-defkey org-mouse-map
  3135. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3136. (org-defkey org-mouse-map
  3137. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3138. (when org-mouse-1-follows-link
  3139. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3140. (when org-tab-follows-link
  3141. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3142. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3143. (when org-return-follows-link
  3144. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3145. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3146. (require 'font-lock)
  3147. (defconst org-non-link-chars "]\t\n\r<>")
  3148. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3149. "shell" "elisp"))
  3150. (defvar org-link-types-re nil
  3151. "Matches a link that has a url-like prefix like \"http:\"")
  3152. (defvar org-link-re-with-space nil
  3153. "Matches a link with spaces, optional angular brackets around it.")
  3154. (defvar org-link-re-with-space2 nil
  3155. "Matches a link with spaces, optional angular brackets around it.")
  3156. (defvar org-link-re-with-space3 nil
  3157. "Matches a link with spaces, only for internal part in bracket links.")
  3158. (defvar org-angle-link-re nil
  3159. "Matches link with angular brackets, spaces are allowed.")
  3160. (defvar org-plain-link-re nil
  3161. "Matches plain link, without spaces.")
  3162. (defvar org-bracket-link-regexp nil
  3163. "Matches a link in double brackets.")
  3164. (defvar org-bracket-link-analytic-regexp nil
  3165. "Regular expression used to analyze links.
  3166. Here is what the match groups contain after a match:
  3167. 1: http:
  3168. 2: http
  3169. 3: path
  3170. 4: [desc]
  3171. 5: desc")
  3172. (defvar org-bracket-link-analytic-regexp++ nil
  3173. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3174. (defvar org-any-link-re nil
  3175. "Regular expression matching any link.")
  3176. (defun org-make-link-regexps ()
  3177. "Update the link regular expressions.
  3178. This should be called after the variable `org-link-types' has changed."
  3179. (setq org-link-types-re
  3180. (concat
  3181. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3182. org-link-re-with-space
  3183. (concat
  3184. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3185. "\\([^" org-non-link-chars " ]"
  3186. "[^" org-non-link-chars "]*"
  3187. "[^" org-non-link-chars " ]\\)>?")
  3188. org-link-re-with-space2
  3189. (concat
  3190. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3191. "\\([^" org-non-link-chars " ]"
  3192. "[^\t\n\r]*"
  3193. "[^" org-non-link-chars " ]\\)>?")
  3194. org-link-re-with-space3
  3195. (concat
  3196. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3197. "\\([^" org-non-link-chars " ]"
  3198. "[^\t\n\r]*\\)")
  3199. org-angle-link-re
  3200. (concat
  3201. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3202. "\\([^" org-non-link-chars " ]"
  3203. "[^" org-non-link-chars "]*"
  3204. "\\)>")
  3205. org-plain-link-re
  3206. (concat
  3207. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3208. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3209. org-bracket-link-regexp
  3210. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3211. org-bracket-link-analytic-regexp
  3212. (concat
  3213. "\\[\\["
  3214. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3215. "\\([^]]+\\)"
  3216. "\\]"
  3217. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3218. "\\]")
  3219. org-bracket-link-analytic-regexp++
  3220. (concat
  3221. "\\[\\["
  3222. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3223. "\\([^]]+\\)"
  3224. "\\]"
  3225. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3226. "\\]")
  3227. org-any-link-re
  3228. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3229. org-angle-link-re "\\)\\|\\("
  3230. org-plain-link-re "\\)")))
  3231. (org-make-link-regexps)
  3232. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3233. "Regular expression for fast time stamp matching.")
  3234. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3235. "Regular expression for fast time stamp matching.")
  3236. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3237. "Regular expression matching time strings for analysis.
  3238. This one does not require the space after the date, so it can be used
  3239. on a string that terminates immediately after the date.")
  3240. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3241. "Regular expression matching time strings for analysis.")
  3242. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3243. "Regular expression matching time stamps, with groups.")
  3244. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3245. "Regular expression matching time stamps (also [..]), with groups.")
  3246. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3247. "Regular expression matching a time stamp range.")
  3248. (defconst org-tr-regexp-both
  3249. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3250. "Regular expression matching a time stamp range.")
  3251. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3252. org-ts-regexp "\\)?")
  3253. "Regular expression matching a time stamp or time stamp range.")
  3254. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3255. org-ts-regexp-both "\\)?")
  3256. "Regular expression matching a time stamp or time stamp range.
  3257. The time stamps may be either active or inactive.")
  3258. (defvar org-emph-face nil)
  3259. (defun org-do-emphasis-faces (limit)
  3260. "Run through the buffer and add overlays to links."
  3261. (let (rtn)
  3262. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3263. (if (not (= (char-after (match-beginning 3))
  3264. (char-after (match-beginning 4))))
  3265. (progn
  3266. (setq rtn t)
  3267. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3268. 'face
  3269. (nth 1 (assoc (match-string 3)
  3270. org-emphasis-alist)))
  3271. (add-text-properties (match-beginning 2) (match-end 2)
  3272. '(font-lock-multiline t))
  3273. (when org-hide-emphasis-markers
  3274. (add-text-properties (match-end 4) (match-beginning 5)
  3275. '(invisible org-link))
  3276. (add-text-properties (match-beginning 3) (match-end 3)
  3277. '(invisible org-link)))))
  3278. (backward-char 1))
  3279. rtn))
  3280. (defun org-emphasize (&optional char)
  3281. "Insert or change an emphasis, i.e. a font like bold or italic.
  3282. If there is an active region, change that region to a new emphasis.
  3283. If there is no region, just insert the marker characters and position
  3284. the cursor between them.
  3285. CHAR should be either the marker character, or the first character of the
  3286. HTML tag associated with that emphasis. If CHAR is a space, the means
  3287. to remove the emphasis of the selected region.
  3288. If char is not given (for example in an interactive call) it
  3289. will be prompted for."
  3290. (interactive)
  3291. (let ((eal org-emphasis-alist) e det
  3292. (erc org-emphasis-regexp-components)
  3293. (prompt "")
  3294. (string "") beg end move tag c s)
  3295. (if (org-region-active-p)
  3296. (setq beg (region-beginning) end (region-end)
  3297. string (buffer-substring beg end))
  3298. (setq move t))
  3299. (while (setq e (pop eal))
  3300. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3301. c (aref tag 0))
  3302. (push (cons c (string-to-char (car e))) det)
  3303. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3304. (substring tag 1)))))
  3305. (setq det (nreverse det))
  3306. (unless char
  3307. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3308. (setq char (read-char-exclusive)))
  3309. (setq char (or (cdr (assoc char det)) char))
  3310. (if (equal char ?\ )
  3311. (setq s "" move nil)
  3312. (unless (assoc (char-to-string char) org-emphasis-alist)
  3313. (error "No such emphasis marker: \"%c\"" char))
  3314. (setq s (char-to-string char)))
  3315. (while (and (> (length string) 1)
  3316. (equal (substring string 0 1) (substring string -1))
  3317. (assoc (substring string 0 1) org-emphasis-alist))
  3318. (setq string (substring string 1 -1)))
  3319. (setq string (concat s string s))
  3320. (if beg (delete-region beg end))
  3321. (unless (or (bolp)
  3322. (string-match (concat "[" (nth 0 erc) "\n]")
  3323. (char-to-string (char-before (point)))))
  3324. (insert " "))
  3325. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3326. (char-to-string (char-after (point))))
  3327. (insert " ") (backward-char 1))
  3328. (insert string)
  3329. (and move (backward-char 1))))
  3330. (defconst org-nonsticky-props
  3331. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3332. (defun org-activate-plain-links (limit)
  3333. "Run through the buffer and add overlays to links."
  3334. (catch 'exit
  3335. (let (f)
  3336. (while (re-search-forward org-plain-link-re limit t)
  3337. (setq f (get-text-property (match-beginning 0) 'face))
  3338. (if (or (eq f 'org-tag)
  3339. (and (listp f) (memq 'org-tag f)))
  3340. nil
  3341. (add-text-properties (match-beginning 0) (match-end 0)
  3342. (list 'mouse-face 'highlight
  3343. 'rear-nonsticky org-nonsticky-props
  3344. 'keymap org-mouse-map
  3345. ))
  3346. (throw 'exit t))))))
  3347. (defun org-activate-code (limit)
  3348. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3349. (progn
  3350. (remove-text-properties (match-beginning 0) (match-end 0)
  3351. '(display t invisible t intangible t))
  3352. t)))
  3353. (defun org-activate-angle-links (limit)
  3354. "Run through the buffer and add overlays to links."
  3355. (if (re-search-forward org-angle-link-re limit t)
  3356. (progn
  3357. (add-text-properties (match-beginning 0) (match-end 0)
  3358. (list 'mouse-face 'highlight
  3359. 'rear-nonsticky org-nonsticky-props
  3360. 'keymap org-mouse-map
  3361. ))
  3362. t)))
  3363. (defun org-activate-footnote-links (limit)
  3364. "Run through the buffer and add overlays to links."
  3365. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3366. limit t)
  3367. (progn
  3368. (add-text-properties (match-beginning 1) (match-end 1)
  3369. (list 'mouse-face 'highlight
  3370. 'rear-nonsticky org-nonsticky-props
  3371. 'keymap org-mouse-map
  3372. 'help-echo
  3373. (if (= (point-at-bol) (match-beginning 1))
  3374. "Footnote definition"
  3375. "Footnote reference")
  3376. ))
  3377. t)))
  3378. (defun org-activate-bracket-links (limit)
  3379. "Run through the buffer and add overlays to bracketed links."
  3380. (if (re-search-forward org-bracket-link-regexp limit t)
  3381. (let* ((help (concat "LINK: "
  3382. (org-match-string-no-properties 1)))
  3383. ;; FIXME: above we should remove the escapes.
  3384. ;; but that requires another match, protecting match data,
  3385. ;; a lot of overhead for font-lock.
  3386. (ip (org-maybe-intangible
  3387. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3388. 'keymap org-mouse-map 'mouse-face 'highlight
  3389. 'font-lock-multiline t 'help-echo help)))
  3390. (vp (list 'rear-nonsticky org-nonsticky-props
  3391. 'keymap org-mouse-map 'mouse-face 'highlight
  3392. ' font-lock-multiline t 'help-echo help)))
  3393. ;; We need to remove the invisible property here. Table narrowing
  3394. ;; may have made some of this invisible.
  3395. (remove-text-properties (match-beginning 0) (match-end 0)
  3396. '(invisible nil))
  3397. (if (match-end 3)
  3398. (progn
  3399. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3400. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3401. (add-text-properties (match-end 3) (match-end 0) ip))
  3402. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3403. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3404. (add-text-properties (match-end 1) (match-end 0) ip))
  3405. t)))
  3406. (defun org-activate-dates (limit)
  3407. "Run through the buffer and add overlays to dates."
  3408. (if (re-search-forward org-tsr-regexp-both limit t)
  3409. (progn
  3410. (add-text-properties (match-beginning 0) (match-end 0)
  3411. (list 'mouse-face 'highlight
  3412. 'rear-nonsticky org-nonsticky-props
  3413. 'keymap org-mouse-map))
  3414. (when org-display-custom-times
  3415. (if (match-end 3)
  3416. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3417. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3418. t)))
  3419. (defvar org-target-link-regexp nil
  3420. "Regular expression matching radio targets in plain text.")
  3421. (make-variable-buffer-local 'org-target-link-regexp)
  3422. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3423. "Regular expression matching a link target.")
  3424. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3425. "Regular expression matching a radio target.")
  3426. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3427. "Regular expression matching any target.")
  3428. (defun org-activate-target-links (limit)
  3429. "Run through the buffer and add overlays to target matches."
  3430. (when org-target-link-regexp
  3431. (let ((case-fold-search t))
  3432. (if (re-search-forward org-target-link-regexp limit t)
  3433. (progn
  3434. (add-text-properties (match-beginning 0) (match-end 0)
  3435. (list 'mouse-face 'highlight
  3436. 'rear-nonsticky org-nonsticky-props
  3437. 'keymap org-mouse-map
  3438. 'help-echo "Radio target link"
  3439. 'org-linked-text t))
  3440. t)))))
  3441. (defun org-update-radio-target-regexp ()
  3442. "Find all radio targets in this file and update the regular expression."
  3443. (interactive)
  3444. (when (memq 'radio org-activate-links)
  3445. (setq org-target-link-regexp
  3446. (org-make-target-link-regexp (org-all-targets 'radio)))
  3447. (org-restart-font-lock)))
  3448. (defun org-hide-wide-columns (limit)
  3449. (let (s e)
  3450. (setq s (text-property-any (point) (or limit (point-max))
  3451. 'org-cwidth t))
  3452. (when s
  3453. (setq e (next-single-property-change s 'org-cwidth))
  3454. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3455. (goto-char e)
  3456. t)))
  3457. (defvar org-latex-and-specials-regexp nil
  3458. "Regular expression for highlighting export special stuff.")
  3459. (defvar org-match-substring-regexp)
  3460. (defvar org-match-substring-with-braces-regexp)
  3461. (defvar org-export-html-special-string-regexps)
  3462. (defun org-compute-latex-and-specials-regexp ()
  3463. "Compute regular expression for stuff treated specially by exporters."
  3464. (if (not org-highlight-latex-fragments-and-specials)
  3465. (org-set-local 'org-latex-and-specials-regexp nil)
  3466. (require 'org-exp)
  3467. (let*
  3468. ((matchers (plist-get org-format-latex-options :matchers))
  3469. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3470. org-latex-regexps)))
  3471. (options (org-combine-plists (org-default-export-plist)
  3472. (org-infile-export-plist)))
  3473. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3474. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3475. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3476. (org-export-html-expand (plist-get options :expand-quoted-html))
  3477. (org-export-with-special-strings (plist-get options :special-strings))
  3478. (re-sub
  3479. (cond
  3480. ((equal org-export-with-sub-superscripts '{})
  3481. (list org-match-substring-with-braces-regexp))
  3482. (org-export-with-sub-superscripts
  3483. (list org-match-substring-regexp))
  3484. (t nil)))
  3485. (re-latex
  3486. (if org-export-with-LaTeX-fragments
  3487. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3488. (re-macros
  3489. (if org-export-with-TeX-macros
  3490. (list (concat "\\\\"
  3491. (regexp-opt
  3492. (append (mapcar 'car org-html-entities)
  3493. (if (boundp 'org-latex-entities)
  3494. org-latex-entities nil))
  3495. 'words))) ; FIXME
  3496. ))
  3497. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3498. (re-special (if org-export-with-special-strings
  3499. (mapcar (lambda (x) (car x))
  3500. org-export-html-special-string-regexps)))
  3501. (re-rest
  3502. (delq nil
  3503. (list
  3504. (if org-export-html-expand "@<[^>\n]+>")
  3505. ))))
  3506. (org-set-local
  3507. 'org-latex-and-specials-regexp
  3508. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3509. re-rest) "\\|")))))
  3510. (defun org-do-latex-and-special-faces (limit)
  3511. "Run through the buffer and add overlays to links."
  3512. (when org-latex-and-specials-regexp
  3513. (let (rtn d)
  3514. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3515. limit t))
  3516. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3517. 'face))
  3518. '(org-code org-verbatim underline)))
  3519. (progn
  3520. (setq rtn t
  3521. d (cond ((member (char-after (1+ (match-beginning 0)))
  3522. '(?_ ?^)) 1)
  3523. (t 0)))
  3524. (font-lock-prepend-text-property
  3525. (+ d (match-beginning 0)) (match-end 0)
  3526. 'face 'org-latex-and-export-specials)
  3527. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3528. '(font-lock-multiline t)))))
  3529. rtn)))
  3530. (defun org-restart-font-lock ()
  3531. "Restart font-lock-mode, to force refontification."
  3532. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3533. (font-lock-mode -1)
  3534. (font-lock-mode 1)))
  3535. (defun org-all-targets (&optional radio)
  3536. "Return a list of all targets in this file.
  3537. With optional argument RADIO, only find radio targets."
  3538. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3539. rtn)
  3540. (save-excursion
  3541. (goto-char (point-min))
  3542. (while (re-search-forward re nil t)
  3543. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3544. rtn)))
  3545. (defun org-make-target-link-regexp (targets)
  3546. "Make regular expression matching all strings in TARGETS.
  3547. The regular expression finds the targets also if there is a line break
  3548. between words."
  3549. (and targets
  3550. (concat
  3551. "\\<\\("
  3552. (mapconcat
  3553. (lambda (x)
  3554. (while (string-match " +" x)
  3555. (setq x (replace-match "\\s-+" t t x)))
  3556. x)
  3557. targets
  3558. "\\|")
  3559. "\\)\\>")))
  3560. (defun org-activate-tags (limit)
  3561. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3562. (progn
  3563. (add-text-properties (match-beginning 1) (match-end 1)
  3564. (list 'mouse-face 'highlight
  3565. 'rear-nonsticky org-nonsticky-props
  3566. 'keymap org-mouse-map))
  3567. t)))
  3568. (defun org-outline-level ()
  3569. (save-excursion
  3570. (looking-at outline-regexp)
  3571. (if (match-beginning 1)
  3572. (+ (org-get-string-indentation (match-string 1)) 1000)
  3573. (1- (- (match-end 0) (match-beginning 0))))))
  3574. (defvar org-font-lock-keywords nil)
  3575. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3576. "Regular expression matching a property line.")
  3577. (defvar org-font-lock-hook nil
  3578. "Functions to be called for special font lock stuff.")
  3579. (defun org-font-lock-hook (limit)
  3580. (run-hook-with-args 'org-font-lock-hook limit))
  3581. (defun org-set-font-lock-defaults ()
  3582. (let* ((em org-fontify-emphasized-text)
  3583. (lk org-activate-links)
  3584. (org-font-lock-extra-keywords
  3585. (list
  3586. ;; Call the hook
  3587. '(org-font-lock-hook)
  3588. ;; Headlines
  3589. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3590. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3591. ;; Table lines
  3592. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3593. (1 'org-table t))
  3594. ;; Table internals
  3595. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3596. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3597. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3598. ;; Drawers
  3599. (list org-drawer-regexp '(0 'org-special-keyword t))
  3600. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3601. ;; Properties
  3602. (list org-property-re
  3603. '(1 'org-special-keyword t)
  3604. '(3 'org-property-value t))
  3605. (if org-format-transports-properties-p
  3606. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3607. ;; Links
  3608. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3609. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3610. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3611. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3612. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3613. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3614. (if (memq 'footnote lk) '(org-activate-footnote-links
  3615. (0 'org-footnote t)))
  3616. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3617. '(org-hide-wide-columns (0 nil append))
  3618. ;; TODO lines
  3619. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3620. '(1 (org-get-todo-face 1) t))
  3621. ;; DONE
  3622. (if org-fontify-done-headline
  3623. (list (concat "^[*]+ +\\<\\("
  3624. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3625. "\\)\\(.*\\)")
  3626. '(2 'org-headline-done t))
  3627. nil)
  3628. ;; Priorities
  3629. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3630. ;; Tags
  3631. '(org-font-lock-add-tag-faces)
  3632. ;; Special keywords
  3633. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3634. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3635. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3636. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3637. ;; Emphasis
  3638. (if em
  3639. (if (featurep 'xemacs)
  3640. '(org-do-emphasis-faces (0 nil append))
  3641. '(org-do-emphasis-faces)))
  3642. ;; Checkboxes
  3643. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3644. 2 'bold prepend)
  3645. (if org-provide-checkbox-statistics
  3646. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3647. (0 (org-get-checkbox-statistics-face) t)))
  3648. ;; Description list items
  3649. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3650. 2 'bold prepend)
  3651. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3652. '(1 'org-archived prepend))
  3653. ;; Specials
  3654. '(org-do-latex-and-special-faces)
  3655. ;; Code
  3656. '(org-activate-code (1 'org-code t))
  3657. ;; COMMENT
  3658. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3659. "\\|" org-quote-string "\\)\\>")
  3660. '(1 'org-special-keyword t))
  3661. '("^#.*" (0 'font-lock-comment-face t))
  3662. )))
  3663. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3664. ;; Now set the full font-lock-keywords
  3665. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3666. (org-set-local 'font-lock-defaults
  3667. '(org-font-lock-keywords t nil nil backward-paragraph))
  3668. (kill-local-variable 'font-lock-keywords) nil))
  3669. (defvar org-m nil)
  3670. (defvar org-l nil)
  3671. (defvar org-f nil)
  3672. (defun org-get-level-face (n)
  3673. "Get the right face for match N in font-lock matching of headlines."
  3674. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3675. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3676. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3677. (cond
  3678. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3679. ((eq n 2) org-f)
  3680. (t (if org-level-color-stars-only nil org-f))))
  3681. (defun org-get-todo-face (kwd)
  3682. "Get the right face for a TODO keyword KWD.
  3683. If KWD is a number, get the corresponding match group."
  3684. (if (numberp kwd) (setq kwd (match-string kwd)))
  3685. (or (cdr (assoc kwd org-todo-keyword-faces))
  3686. (and (member kwd org-done-keywords) 'org-done)
  3687. 'org-todo))
  3688. (defun org-font-lock-add-tag-faces (limit)
  3689. "Add the special tag faces."
  3690. (when (and org-tag-faces org-tags-special-faces-re)
  3691. (while (re-search-forward org-tags-special-faces-re limit t)
  3692. (add-text-properties (match-beginning 1) (match-end 1)
  3693. (list 'face (org-get-tag-face 1)
  3694. 'font-lock-fontified t))
  3695. (backward-char 1))))
  3696. (defun org-get-tag-face (kwd)
  3697. "Get the right face for a TODO keyword KWD.
  3698. If KWD is a number, get the corresponding match group."
  3699. (if (numberp kwd) (setq kwd (match-string kwd)))
  3700. (or (cdr (assoc kwd org-tag-faces))
  3701. 'org-tag))
  3702. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3703. "Remove fontification and activation overlays from links."
  3704. (font-lock-default-unfontify-region beg end)
  3705. (let* ((buffer-undo-list t)
  3706. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3707. (inhibit-modification-hooks t)
  3708. deactivate-mark buffer-file-name buffer-file-truename)
  3709. (remove-text-properties beg end
  3710. '(mouse-face t keymap t org-linked-text t
  3711. invisible t intangible t))))
  3712. ;;;; Visibility cycling, including org-goto and indirect buffer
  3713. ;;; Cycling
  3714. (defvar org-cycle-global-status nil)
  3715. (make-variable-buffer-local 'org-cycle-global-status)
  3716. (defvar org-cycle-subtree-status nil)
  3717. (make-variable-buffer-local 'org-cycle-subtree-status)
  3718. ;;;###autoload
  3719. (defun org-cycle (&optional arg)
  3720. "Visibility cycling for Org-mode.
  3721. - When this function is called with a prefix argument, rotate the entire
  3722. buffer through 3 states (global cycling)
  3723. 1. OVERVIEW: Show only top-level headlines.
  3724. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3725. 3. SHOW ALL: Show everything.
  3726. When called with two C-u C-u prefixes, switch to the startup visibility,
  3727. determined by the variable `org-startup-folded', and by any VISIBILITY
  3728. properties in the buffer.
  3729. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3730. including drawers.
  3731. - When point is at the beginning of a headline, rotate the subtree started
  3732. by this line through 3 different states (local cycling)
  3733. 1. FOLDED: Only the main headline is shown.
  3734. 2. CHILDREN: The main headline and the direct children are shown.
  3735. From this state, you can move to one of the children
  3736. and zoom in further.
  3737. 3. SUBTREE: Show the entire subtree, including body text.
  3738. - When there is a numeric prefix, go up to a heading with level ARG, do
  3739. a `show-subtree' and return to the previous cursor position. If ARG
  3740. is negative, go up that many levels.
  3741. - When point is not at the beginning of a headline, execute the global
  3742. binding for TAB, which is re-indenting the line. See the option
  3743. `org-cycle-emulate-tab' for details.
  3744. - Special case: if point is at the beginning of the buffer and there is
  3745. no headline in line 1, this function will act as if called with prefix arg.
  3746. But only if also the variable `org-cycle-global-at-bob' is t."
  3747. (interactive "P")
  3748. (org-load-modules-maybe)
  3749. (let* ((outline-regexp
  3750. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3751. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3752. outline-regexp))
  3753. (bob-special (and org-cycle-global-at-bob (bobp)
  3754. (not (looking-at outline-regexp))))
  3755. (org-cycle-hook
  3756. (if bob-special
  3757. (delq 'org-optimize-window-after-visibility-change
  3758. (copy-sequence org-cycle-hook))
  3759. org-cycle-hook))
  3760. (pos (point)))
  3761. (if (or bob-special (equal arg '(4)))
  3762. ;; special case: use global cycling
  3763. (setq arg t))
  3764. (cond
  3765. ((equal arg '(16))
  3766. (org-set-startup-visibility)
  3767. (message "Startup visibility, plus VISIBILITY properties"))
  3768. ((equal arg '(64))
  3769. (show-all)
  3770. (message "Entire buffer visible, including drawers"))
  3771. ((org-at-table-p 'any)
  3772. ;; Enter the table or move to the next field in the table
  3773. (or (org-table-recognize-table.el)
  3774. (progn
  3775. (if arg (org-table-edit-field t)
  3776. (org-table-justify-field-maybe)
  3777. (call-interactively 'org-table-next-field)))))
  3778. ((eq arg t) ;; Global cycling
  3779. (cond
  3780. ((and (eq last-command this-command)
  3781. (eq org-cycle-global-status 'overview))
  3782. ;; We just created the overview - now do table of contents
  3783. ;; This can be slow in very large buffers, so indicate action
  3784. (message "CONTENTS...")
  3785. (org-content)
  3786. (message "CONTENTS...done")
  3787. (setq org-cycle-global-status 'contents)
  3788. (run-hook-with-args 'org-cycle-hook 'contents))
  3789. ((and (eq last-command this-command)
  3790. (eq org-cycle-global-status 'contents))
  3791. ;; We just showed the table of contents - now show everything
  3792. (show-all)
  3793. (message "SHOW ALL")
  3794. (setq org-cycle-global-status 'all)
  3795. (run-hook-with-args 'org-cycle-hook 'all))
  3796. (t
  3797. ;; Default action: go to overview
  3798. (org-overview)
  3799. (message "OVERVIEW")
  3800. (setq org-cycle-global-status 'overview)
  3801. (run-hook-with-args 'org-cycle-hook 'overview))))
  3802. ((and org-drawers org-drawer-regexp
  3803. (save-excursion
  3804. (beginning-of-line 1)
  3805. (looking-at org-drawer-regexp)))
  3806. ;; Toggle block visibility
  3807. (org-flag-drawer
  3808. (not (get-char-property (match-end 0) 'invisible))))
  3809. ((integerp arg)
  3810. ;; Show-subtree, ARG levels up from here.
  3811. (save-excursion
  3812. (org-back-to-heading)
  3813. (outline-up-heading (if (< arg 0) (- arg)
  3814. (- (funcall outline-level) arg)))
  3815. (org-show-subtree)))
  3816. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3817. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3818. ;; At a heading: rotate between three different views
  3819. (org-back-to-heading)
  3820. (let ((goal-column 0) eoh eol eos)
  3821. ;; First, some boundaries
  3822. (save-excursion
  3823. (org-back-to-heading)
  3824. (save-excursion
  3825. (beginning-of-line 2)
  3826. (while (and (not (eobp)) ;; this is like `next-line'
  3827. (get-char-property (1- (point)) 'invisible))
  3828. (beginning-of-line 2)) (setq eol (point)))
  3829. (outline-end-of-heading) (setq eoh (point))
  3830. (org-end-of-subtree t)
  3831. (unless (eobp)
  3832. (skip-chars-forward " \t\n")
  3833. (beginning-of-line 1) ; in case this is an item
  3834. )
  3835. (setq eos (1- (point))))
  3836. ;; Find out what to do next and set `this-command'
  3837. (cond
  3838. ((= eos eoh)
  3839. ;; Nothing is hidden behind this heading
  3840. (message "EMPTY ENTRY")
  3841. (setq org-cycle-subtree-status nil)
  3842. (save-excursion
  3843. (goto-char eos)
  3844. (outline-next-heading)
  3845. (if (org-invisible-p) (org-flag-heading nil))))
  3846. ((or (>= eol eos)
  3847. (not (string-match "\\S-" (buffer-substring eol eos))))
  3848. ;; Entire subtree is hidden in one line: open it
  3849. (org-show-entry)
  3850. (show-children)
  3851. (message "CHILDREN")
  3852. (save-excursion
  3853. (goto-char eos)
  3854. (outline-next-heading)
  3855. (if (org-invisible-p) (org-flag-heading nil)))
  3856. (setq org-cycle-subtree-status 'children)
  3857. (run-hook-with-args 'org-cycle-hook 'children))
  3858. ((and (eq last-command this-command)
  3859. (eq org-cycle-subtree-status 'children))
  3860. ;; We just showed the children, now show everything.
  3861. (org-show-subtree)
  3862. (message "SUBTREE")
  3863. (setq org-cycle-subtree-status 'subtree)
  3864. (run-hook-with-args 'org-cycle-hook 'subtree))
  3865. (t
  3866. ;; Default action: hide the subtree.
  3867. (hide-subtree)
  3868. (message "FOLDED")
  3869. (setq org-cycle-subtree-status 'folded)
  3870. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3871. ;; TAB emulation and template completion
  3872. (buffer-read-only (org-back-to-heading))
  3873. ((org-try-structure-completion))
  3874. ((org-try-cdlatex-tab))
  3875. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3876. (or (not (bolp))
  3877. (not (looking-at outline-regexp))))
  3878. (call-interactively (global-key-binding "\t")))
  3879. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3880. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3881. (or (and (eq org-cycle-emulate-tab 'white)
  3882. (= (match-end 0) (point-at-eol)))
  3883. (and (eq org-cycle-emulate-tab 'whitestart)
  3884. (>= (match-end 0) pos))))
  3885. t
  3886. (eq org-cycle-emulate-tab t))
  3887. (call-interactively (global-key-binding "\t")))
  3888. (t (save-excursion
  3889. (org-back-to-heading)
  3890. (org-cycle))))))
  3891. ;;;###autoload
  3892. (defun org-global-cycle (&optional arg)
  3893. "Cycle the global visibility. For details see `org-cycle'.
  3894. With C-u prefix arg, switch to startup visibility.
  3895. With a numeric prefix, show all headlines up to that level."
  3896. (interactive "P")
  3897. (let ((org-cycle-include-plain-lists
  3898. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3899. (cond
  3900. ((integerp arg)
  3901. (show-all)
  3902. (hide-sublevels arg)
  3903. (setq org-cycle-global-status 'contents))
  3904. ((equal arg '(4))
  3905. (org-set-startup-visibility)
  3906. (message "Startup visibility, plus VISIBILITY properties."))
  3907. (t
  3908. (org-cycle '(4))))))
  3909. (defun org-set-startup-visibility ()
  3910. "Set the visibility required by startup options and properties."
  3911. (cond
  3912. ((eq org-startup-folded t)
  3913. (org-cycle '(4)))
  3914. ((eq org-startup-folded 'content)
  3915. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3916. (org-cycle '(4)) (org-cycle '(4)))))
  3917. (org-set-visibility-according-to-property 'no-cleanup)
  3918. (org-cycle-hide-archived-subtrees 'all)
  3919. (org-cycle-hide-drawers 'all)
  3920. (org-cycle-show-empty-lines 'all))
  3921. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3922. "Switch subtree visibilities according to :VISIBILITY: property."
  3923. (interactive)
  3924. (let (state)
  3925. (save-excursion
  3926. (goto-char (point-min))
  3927. (while (re-search-forward
  3928. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3929. nil t)
  3930. (setq state (match-string 1))
  3931. (save-excursion
  3932. (org-back-to-heading t)
  3933. (hide-subtree)
  3934. (org-reveal)
  3935. (cond
  3936. ((equal state '("fold" "folded"))
  3937. (hide-subtree))
  3938. ((equal state "children")
  3939. (org-show-hidden-entry)
  3940. (show-children))
  3941. ((equal state "content")
  3942. (save-excursion
  3943. (save-restriction
  3944. (org-narrow-to-subtree)
  3945. (org-content))))
  3946. ((member state '("all" "showall"))
  3947. (show-subtree)))))
  3948. (unless no-cleanup
  3949. (org-cycle-hide-archived-subtrees 'all)
  3950. (org-cycle-hide-drawers 'all)
  3951. (org-cycle-show-empty-lines 'all)))))
  3952. (defun org-overview ()
  3953. "Switch to overview mode, showing only top-level headlines.
  3954. Really, this shows all headlines with level equal or greater than the level
  3955. of the first headline in the buffer. This is important, because if the
  3956. first headline is not level one, then (hide-sublevels 1) gives confusing
  3957. results."
  3958. (interactive)
  3959. (let ((level (save-excursion
  3960. (goto-char (point-min))
  3961. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3962. (progn
  3963. (goto-char (match-beginning 0))
  3964. (funcall outline-level))))))
  3965. (and level (hide-sublevels level))))
  3966. (defun org-content (&optional arg)
  3967. "Show all headlines in the buffer, like a table of contents.
  3968. With numerical argument N, show content up to level N."
  3969. (interactive "P")
  3970. (save-excursion
  3971. ;; Visit all headings and show their offspring
  3972. (and (integerp arg) (org-overview))
  3973. (goto-char (point-max))
  3974. (catch 'exit
  3975. (while (and (progn (condition-case nil
  3976. (outline-previous-visible-heading 1)
  3977. (error (goto-char (point-min))))
  3978. t)
  3979. (looking-at outline-regexp))
  3980. (if (integerp arg)
  3981. (show-children (1- arg))
  3982. (show-branches))
  3983. (if (bobp) (throw 'exit nil))))))
  3984. (defun org-optimize-window-after-visibility-change (state)
  3985. "Adjust the window after a change in outline visibility.
  3986. This function is the default value of the hook `org-cycle-hook'."
  3987. (when (get-buffer-window (current-buffer))
  3988. (cond
  3989. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3990. ; ((eq state 'overview) (org-beginning-of-line))
  3991. ((eq state 'content) nil)
  3992. ((eq state 'all) nil)
  3993. ((eq state 'folded) nil)
  3994. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3995. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3996. (defun org-compact-display-after-subtree-move ()
  3997. (let (beg end)
  3998. (save-excursion
  3999. (if (org-up-heading-safe)
  4000. (progn
  4001. (hide-subtree)
  4002. (show-entry)
  4003. (show-children)
  4004. (org-cycle-show-empty-lines 'children)
  4005. (org-cycle-hide-drawers 'children))
  4006. (org-overview)))))
  4007. (defun org-cycle-show-empty-lines (state)
  4008. "Show empty lines above all visible headlines.
  4009. The region to be covered depends on STATE when called through
  4010. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4011. entire buffer covered. Note that an empty line is only shown if there
  4012. are at least `org-cycle-separator-lines' empty lines before the headline."
  4013. (when (> org-cycle-separator-lines 0)
  4014. (save-excursion
  4015. (let* ((n org-cycle-separator-lines)
  4016. (re (cond
  4017. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4018. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4019. (t (let ((ns (number-to-string (- n 2))))
  4020. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4021. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4022. beg end)
  4023. (cond
  4024. ((memq state '(overview contents t))
  4025. (setq beg (point-min) end (point-max)))
  4026. ((memq state '(children folded))
  4027. (setq beg (point) end (progn (org-end-of-subtree t t)
  4028. (beginning-of-line 2)
  4029. (point)))))
  4030. (when beg
  4031. (goto-char beg)
  4032. (while (re-search-forward re end t)
  4033. (if (not (get-char-property (match-end 1) 'invisible))
  4034. (outline-flag-region
  4035. (match-beginning 1) (match-end 1) nil)))))))
  4036. ;; Never hide empty lines at the end of the file.
  4037. (save-excursion
  4038. (goto-char (point-max))
  4039. (outline-previous-heading)
  4040. (outline-end-of-heading)
  4041. (if (and (looking-at "[ \t\n]+")
  4042. (= (match-end 0) (point-max)))
  4043. (outline-flag-region (point) (match-end 0) nil))))
  4044. (defun org-show-empty-lines-in-parent ()
  4045. "Move to the parent and re-show empty lines before visible headlines."
  4046. (save-excursion
  4047. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4048. (org-cycle-show-empty-lines context))))
  4049. (defun org-cycle-hide-drawers (state)
  4050. "Re-hide all drawers after a visibility state change."
  4051. (when (and (org-mode-p)
  4052. (not (memq state '(overview folded))))
  4053. (save-excursion
  4054. (let* ((globalp (memq state '(contents all)))
  4055. (beg (if globalp (point-min) (point)))
  4056. (end (if globalp (point-max) (org-end-of-subtree t))))
  4057. (goto-char beg)
  4058. (while (re-search-forward org-drawer-regexp end t)
  4059. (org-flag-drawer t))))))
  4060. (defun org-flag-drawer (flag)
  4061. (save-excursion
  4062. (beginning-of-line 1)
  4063. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4064. (let ((b (match-end 0))
  4065. (outline-regexp org-outline-regexp))
  4066. (if (re-search-forward
  4067. "^[ \t]*:END:"
  4068. (save-excursion (outline-next-heading) (point)) t)
  4069. (outline-flag-region b (point-at-eol) flag)
  4070. (error ":END: line missing"))))))
  4071. (defun org-subtree-end-visible-p ()
  4072. "Is the end of the current subtree visible?"
  4073. (pos-visible-in-window-p
  4074. (save-excursion (org-end-of-subtree t) (point))))
  4075. (defun org-first-headline-recenter (&optional N)
  4076. "Move cursor to the first headline and recenter the headline.
  4077. Optional argument N means, put the headline into the Nth line of the window."
  4078. (goto-char (point-min))
  4079. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4080. (beginning-of-line)
  4081. (recenter (prefix-numeric-value N))))
  4082. ;;; Org-goto
  4083. (defvar org-goto-window-configuration nil)
  4084. (defvar org-goto-marker nil)
  4085. (defvar org-goto-map
  4086. (let ((map (make-sparse-keymap)))
  4087. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4088. (while (setq cmd (pop cmds))
  4089. (substitute-key-definition cmd cmd map global-map)))
  4090. (suppress-keymap map)
  4091. (org-defkey map "\C-m" 'org-goto-ret)
  4092. (org-defkey map [(return)] 'org-goto-ret)
  4093. (org-defkey map [(left)] 'org-goto-left)
  4094. (org-defkey map [(right)] 'org-goto-right)
  4095. (org-defkey map [(control ?g)] 'org-goto-quit)
  4096. (org-defkey map "\C-i" 'org-cycle)
  4097. (org-defkey map [(tab)] 'org-cycle)
  4098. (org-defkey map [(down)] 'outline-next-visible-heading)
  4099. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4100. (if org-goto-auto-isearch
  4101. (if (fboundp 'define-key-after)
  4102. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4103. nil)
  4104. (org-defkey map "q" 'org-goto-quit)
  4105. (org-defkey map "n" 'outline-next-visible-heading)
  4106. (org-defkey map "p" 'outline-previous-visible-heading)
  4107. (org-defkey map "f" 'outline-forward-same-level)
  4108. (org-defkey map "b" 'outline-backward-same-level)
  4109. (org-defkey map "u" 'outline-up-heading))
  4110. (org-defkey map "/" 'org-occur)
  4111. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4112. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4113. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4114. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4115. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4116. map))
  4117. (defconst org-goto-help
  4118. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4119. RET=jump to location [Q]uit and return to previous location
  4120. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4121. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4122. ;; FIXME: Docstring doe not mention both interfaces
  4123. (defun org-goto (&optional alternative-interface)
  4124. "Look up a different location in the current file, keeping current visibility.
  4125. When you want look-up or go to a different location in a document, the
  4126. fastest way is often to fold the entire buffer and then dive into the tree.
  4127. This method has the disadvantage, that the previous location will be folded,
  4128. which may not be what you want.
  4129. This command works around this by showing a copy of the current buffer
  4130. in an indirect buffer, in overview mode. You can dive into the tree in
  4131. that copy, use org-occur and incremental search to find a location.
  4132. When pressing RET or `Q', the command returns to the original buffer in
  4133. which the visibility is still unchanged. After RET is will also jump to
  4134. the location selected in the indirect buffer and expose the
  4135. the headline hierarchy above."
  4136. (interactive "P")
  4137. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4138. (org-refile-use-outline-path t)
  4139. (interface
  4140. (if (not alternative-interface)
  4141. org-goto-interface
  4142. (if (eq org-goto-interface 'outline)
  4143. 'outline-path-completion
  4144. 'outline)))
  4145. (org-goto-start-pos (point))
  4146. (selected-point
  4147. (if (eq interface 'outline)
  4148. (car (org-get-location (current-buffer) org-goto-help))
  4149. (nth 3 (org-refile-get-location "Goto: ")))))
  4150. (if selected-point
  4151. (progn
  4152. (org-mark-ring-push org-goto-start-pos)
  4153. (goto-char selected-point)
  4154. (if (or (org-invisible-p) (org-invisible-p2))
  4155. (org-show-context 'org-goto)))
  4156. (message "Quit"))))
  4157. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4158. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4159. (defvar org-goto-local-auto-isearch-map) ; defined below
  4160. (defun org-get-location (buf help)
  4161. "Let the user select a location in the Org-mode buffer BUF.
  4162. This function uses a recursive edit. It returns the selected position
  4163. or nil."
  4164. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4165. (isearch-hide-immediately nil)
  4166. (isearch-search-fun-function
  4167. (lambda () 'org-goto-local-search-headings))
  4168. (org-goto-selected-point org-goto-exit-command))
  4169. (save-excursion
  4170. (save-window-excursion
  4171. (delete-other-windows)
  4172. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4173. (switch-to-buffer
  4174. (condition-case nil
  4175. (make-indirect-buffer (current-buffer) "*org-goto*")
  4176. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4177. (with-output-to-temp-buffer "*Help*"
  4178. (princ help))
  4179. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4180. (setq buffer-read-only nil)
  4181. (let ((org-startup-truncated t)
  4182. (org-startup-folded nil)
  4183. (org-startup-align-all-tables nil))
  4184. (org-mode)
  4185. (org-overview))
  4186. (setq buffer-read-only t)
  4187. (if (and (boundp 'org-goto-start-pos)
  4188. (integer-or-marker-p org-goto-start-pos))
  4189. (let ((org-show-hierarchy-above t)
  4190. (org-show-siblings t)
  4191. (org-show-following-heading t))
  4192. (goto-char org-goto-start-pos)
  4193. (and (org-invisible-p) (org-show-context)))
  4194. (goto-char (point-min)))
  4195. (org-beginning-of-line)
  4196. (message "Select location and press RET")
  4197. (use-local-map org-goto-map)
  4198. (recursive-edit)
  4199. ))
  4200. (kill-buffer "*org-goto*")
  4201. (cons org-goto-selected-point org-goto-exit-command)))
  4202. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4203. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4204. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4205. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4206. (defun org-goto-local-search-headings (string bound noerror)
  4207. "Search and make sure that any matches are in headlines."
  4208. (catch 'return
  4209. (while (if isearch-forward
  4210. (search-forward string bound noerror)
  4211. (search-backward string bound noerror))
  4212. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4213. (and (member :headline context)
  4214. (not (member :tags context))))
  4215. (throw 'return (point))))))
  4216. (defun org-goto-local-auto-isearch ()
  4217. "Start isearch."
  4218. (interactive)
  4219. (goto-char (point-min))
  4220. (let ((keys (this-command-keys)))
  4221. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4222. (isearch-mode t)
  4223. (isearch-process-search-char (string-to-char keys)))))
  4224. (defun org-goto-ret (&optional arg)
  4225. "Finish `org-goto' by going to the new location."
  4226. (interactive "P")
  4227. (setq org-goto-selected-point (point)
  4228. org-goto-exit-command 'return)
  4229. (throw 'exit nil))
  4230. (defun org-goto-left ()
  4231. "Finish `org-goto' by going to the new location."
  4232. (interactive)
  4233. (if (org-on-heading-p)
  4234. (progn
  4235. (beginning-of-line 1)
  4236. (setq org-goto-selected-point (point)
  4237. org-goto-exit-command 'left)
  4238. (throw 'exit nil))
  4239. (error "Not on a heading")))
  4240. (defun org-goto-right ()
  4241. "Finish `org-goto' by going to the new location."
  4242. (interactive)
  4243. (if (org-on-heading-p)
  4244. (progn
  4245. (setq org-goto-selected-point (point)
  4246. org-goto-exit-command 'right)
  4247. (throw 'exit nil))
  4248. (error "Not on a heading")))
  4249. (defun org-goto-quit ()
  4250. "Finish `org-goto' without cursor motion."
  4251. (interactive)
  4252. (setq org-goto-selected-point nil)
  4253. (setq org-goto-exit-command 'quit)
  4254. (throw 'exit nil))
  4255. ;;; Indirect buffer display of subtrees
  4256. (defvar org-indirect-dedicated-frame nil
  4257. "This is the frame being used for indirect tree display.")
  4258. (defvar org-last-indirect-buffer nil)
  4259. (defun org-tree-to-indirect-buffer (&optional arg)
  4260. "Create indirect buffer and narrow it to current subtree.
  4261. With numerical prefix ARG, go up to this level and then take that tree.
  4262. If ARG is negative, go up that many levels.
  4263. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4264. indirect buffer previously made with this command, to avoid proliferation of
  4265. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4266. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4267. is kept so that you can work with several indirect buffers at the same time.
  4268. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4269. requests that a new frame be made for the new buffer, so that the dedicated
  4270. frame is not changed."
  4271. (interactive "P")
  4272. (let ((cbuf (current-buffer))
  4273. (cwin (selected-window))
  4274. (pos (point))
  4275. beg end level heading ibuf)
  4276. (save-excursion
  4277. (org-back-to-heading t)
  4278. (when (numberp arg)
  4279. (setq level (org-outline-level))
  4280. (if (< arg 0) (setq arg (+ level arg)))
  4281. (while (> (setq level (org-outline-level)) arg)
  4282. (outline-up-heading 1 t)))
  4283. (setq beg (point)
  4284. heading (org-get-heading))
  4285. (org-end-of-subtree t) (setq end (point)))
  4286. (if (and (buffer-live-p org-last-indirect-buffer)
  4287. (not (eq org-indirect-buffer-display 'new-frame))
  4288. (not arg))
  4289. (kill-buffer org-last-indirect-buffer))
  4290. (setq ibuf (org-get-indirect-buffer cbuf)
  4291. org-last-indirect-buffer ibuf)
  4292. (cond
  4293. ((or (eq org-indirect-buffer-display 'new-frame)
  4294. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4295. (select-frame (make-frame))
  4296. (delete-other-windows)
  4297. (switch-to-buffer ibuf)
  4298. (org-set-frame-title heading))
  4299. ((eq org-indirect-buffer-display 'dedicated-frame)
  4300. (raise-frame
  4301. (select-frame (or (and org-indirect-dedicated-frame
  4302. (frame-live-p org-indirect-dedicated-frame)
  4303. org-indirect-dedicated-frame)
  4304. (setq org-indirect-dedicated-frame (make-frame)))))
  4305. (delete-other-windows)
  4306. (switch-to-buffer ibuf)
  4307. (org-set-frame-title (concat "Indirect: " heading)))
  4308. ((eq org-indirect-buffer-display 'current-window)
  4309. (switch-to-buffer ibuf))
  4310. ((eq org-indirect-buffer-display 'other-window)
  4311. (pop-to-buffer ibuf))
  4312. (t (error "Invalid value.")))
  4313. (if (featurep 'xemacs)
  4314. (save-excursion (org-mode) (turn-on-font-lock)))
  4315. (narrow-to-region beg end)
  4316. (show-all)
  4317. (goto-char pos)
  4318. (and (window-live-p cwin) (select-window cwin))))
  4319. (defun org-get-indirect-buffer (&optional buffer)
  4320. (setq buffer (or buffer (current-buffer)))
  4321. (let ((n 1) (base (buffer-name buffer)) bname)
  4322. (while (buffer-live-p
  4323. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4324. (setq n (1+ n)))
  4325. (condition-case nil
  4326. (make-indirect-buffer buffer bname 'clone)
  4327. (error (make-indirect-buffer buffer bname)))))
  4328. (defun org-set-frame-title (title)
  4329. "Set the title of the current frame to the string TITLE."
  4330. ;; FIXME: how to name a single frame in XEmacs???
  4331. (unless (featurep 'xemacs)
  4332. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4333. ;;;; Structure editing
  4334. ;;; Inserting headlines
  4335. (defun org-previous-line-empty-p ()
  4336. (save-excursion
  4337. (and (not (bobp))
  4338. (or (beginning-of-line 0) t)
  4339. (save-match-data
  4340. (looking-at "[ \t]*$")))))
  4341. (defun org-insert-heading (&optional force-heading)
  4342. "Insert a new heading or item with same depth at point.
  4343. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4344. If point is at the beginning of a headline, insert a sibling before the
  4345. current headline. If point is not at the beginning, do not split the line,
  4346. but create the new headline after the current line."
  4347. (interactive "P")
  4348. (if (= (buffer-size) 0)
  4349. (insert "\n* ")
  4350. (when (or force-heading (not (org-insert-item)))
  4351. (let* ((empty-line-p nil)
  4352. (head (save-excursion
  4353. (condition-case nil
  4354. (progn
  4355. (org-back-to-heading)
  4356. (setq empty-line-p (org-previous-line-empty-p))
  4357. (match-string 0))
  4358. (error "*"))))
  4359. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4360. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4361. pos hide-previous previous-pos)
  4362. (cond
  4363. ((and (org-on-heading-p) (bolp)
  4364. (or (bobp)
  4365. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4366. ;; insert before the current line
  4367. (open-line (if blank 2 1)))
  4368. ((and (bolp)
  4369. (or (bobp)
  4370. (save-excursion
  4371. (backward-char 1) (not (org-invisible-p)))))
  4372. ;; insert right here
  4373. nil)
  4374. (t
  4375. ;; somewhere in the line
  4376. (save-excursion
  4377. (setq previous-pos (point-at-bol))
  4378. (end-of-line)
  4379. (setq hide-previous (org-invisible-p)))
  4380. (and org-insert-heading-respect-content (org-show-subtree))
  4381. (let ((split
  4382. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4383. (save-excursion
  4384. (let ((p (point)))
  4385. (goto-char (point-at-bol))
  4386. (and (looking-at org-complex-heading-regexp)
  4387. (> p (match-beginning 4)))))))
  4388. tags pos)
  4389. (cond
  4390. (org-insert-heading-respect-content
  4391. (org-end-of-subtree nil t)
  4392. (or (bolp) (newline))
  4393. (or (org-previous-line-empty-p)
  4394. (and blank (newline)))
  4395. (open-line 1))
  4396. ((org-on-heading-p)
  4397. (when hide-previous
  4398. (show-children)
  4399. (org-show-entry))
  4400. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4401. (setq tags (and (match-end 2) (match-string 2)))
  4402. (and (match-end 1)
  4403. (delete-region (match-beginning 1) (match-end 1)))
  4404. (setq pos (point-at-bol))
  4405. (or split (end-of-line 1))
  4406. (delete-horizontal-space)
  4407. (newline (if blank 2 1))
  4408. (when tags
  4409. (save-excursion
  4410. (goto-char pos)
  4411. (end-of-line 1)
  4412. (insert " " tags)
  4413. (org-set-tags nil 'align))))
  4414. (t
  4415. (or split (end-of-line 1))
  4416. (newline (if blank 2 1)))))))
  4417. (insert head) (just-one-space)
  4418. (setq pos (point))
  4419. (end-of-line 1)
  4420. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4421. (when (and org-insert-heading-respect-content hide-previous)
  4422. (save-excursion
  4423. (goto-char previous-pos)
  4424. (hide-subtree)))
  4425. (run-hooks 'org-insert-heading-hook)))))
  4426. (defun org-get-heading (&optional no-tags)
  4427. "Return the heading of the current entry, without the stars."
  4428. (save-excursion
  4429. (org-back-to-heading t)
  4430. (if (looking-at
  4431. (if no-tags
  4432. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4433. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4434. (match-string 1) "")))
  4435. (defun org-heading-components ()
  4436. "Return the components of the current heading.
  4437. This is a list with the following elements:
  4438. - the level as an integer
  4439. - the reduced level, different if `org-odd-levels-only' is set.
  4440. - the TODO keyword, or nil
  4441. - the priority character, like ?A, or nil if no priority is given
  4442. - the headline text itself, or the tags string if no headline text
  4443. - the tags string, or nil."
  4444. (save-excursion
  4445. (org-back-to-heading t)
  4446. (if (looking-at org-complex-heading-regexp)
  4447. (list (length (match-string 1))
  4448. (org-reduced-level (length (match-string 1)))
  4449. (org-match-string-no-properties 2)
  4450. (and (match-end 3) (aref (match-string 3) 2))
  4451. (org-match-string-no-properties 4)
  4452. (org-match-string-no-properties 5)))))
  4453. (defun org-insert-heading-after-current ()
  4454. "Insert a new heading with same level as current, after current subtree."
  4455. (interactive)
  4456. (org-back-to-heading)
  4457. (org-insert-heading)
  4458. (org-move-subtree-down)
  4459. (end-of-line 1))
  4460. (defun org-insert-heading-respect-content ()
  4461. (interactive)
  4462. (let ((org-insert-heading-respect-content t))
  4463. (org-insert-heading t)))
  4464. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4465. (interactive "P")
  4466. (let ((org-insert-heading-respect-content t))
  4467. (org-insert-todo-heading force-state t)))
  4468. (defun org-insert-todo-heading (arg &optional force-heading)
  4469. "Insert a new heading with the same level and TODO state as current heading.
  4470. If the heading has no TODO state, or if the state is DONE, use the first
  4471. state (TODO by default). Also with prefix arg, force first state."
  4472. (interactive "P")
  4473. (when (or force-heading (not (org-insert-item 'checkbox)))
  4474. (org-insert-heading force-heading)
  4475. (save-excursion
  4476. (org-back-to-heading)
  4477. (outline-previous-heading)
  4478. (looking-at org-todo-line-regexp))
  4479. (if (or arg
  4480. (not (match-beginning 2))
  4481. (member (match-string 2) org-done-keywords))
  4482. (insert (car org-todo-keywords-1) " ")
  4483. (insert (match-string 2) " "))
  4484. (when org-provide-todo-statistics
  4485. (org-update-parent-todo-statistics))))
  4486. (defun org-insert-subheading (arg)
  4487. "Insert a new subheading and demote it.
  4488. Works for outline headings and for plain lists alike."
  4489. (interactive "P")
  4490. (org-insert-heading arg)
  4491. (cond
  4492. ((org-on-heading-p) (org-do-demote))
  4493. ((org-at-item-p) (org-indent-item 1))))
  4494. (defun org-insert-todo-subheading (arg)
  4495. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4496. Works for outline headings and for plain lists alike."
  4497. (interactive "P")
  4498. (org-insert-todo-heading arg)
  4499. (cond
  4500. ((org-on-heading-p) (org-do-demote))
  4501. ((org-at-item-p) (org-indent-item 1))))
  4502. ;;; Promotion and Demotion
  4503. (defun org-promote-subtree ()
  4504. "Promote the entire subtree.
  4505. See also `org-promote'."
  4506. (interactive)
  4507. (save-excursion
  4508. (org-map-tree 'org-promote))
  4509. (org-fix-position-after-promote))
  4510. (defun org-demote-subtree ()
  4511. "Demote the entire subtree. See `org-demote'.
  4512. See also `org-promote'."
  4513. (interactive)
  4514. (save-excursion
  4515. (org-map-tree 'org-demote))
  4516. (org-fix-position-after-promote))
  4517. (defun org-do-promote ()
  4518. "Promote the current heading higher up the tree.
  4519. If the region is active in `transient-mark-mode', promote all headings
  4520. in the region."
  4521. (interactive)
  4522. (save-excursion
  4523. (if (org-region-active-p)
  4524. (org-map-region 'org-promote (region-beginning) (region-end))
  4525. (org-promote)))
  4526. (org-fix-position-after-promote))
  4527. (defun org-do-demote ()
  4528. "Demote the current heading lower down the tree.
  4529. If the region is active in `transient-mark-mode', demote all headings
  4530. in the region."
  4531. (interactive)
  4532. (save-excursion
  4533. (if (org-region-active-p)
  4534. (org-map-region 'org-demote (region-beginning) (region-end))
  4535. (org-demote)))
  4536. (org-fix-position-after-promote))
  4537. (defun org-fix-position-after-promote ()
  4538. "Make sure that after pro/demotion cursor position is right."
  4539. (let ((pos (point)))
  4540. (when (save-excursion
  4541. (beginning-of-line 1)
  4542. (looking-at org-todo-line-regexp)
  4543. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4544. (cond ((eobp) (insert " "))
  4545. ((eolp) (insert " "))
  4546. ((equal (char-after) ?\ ) (forward-char 1))))))
  4547. (defun org-reduced-level (l)
  4548. "Compute the effective level of a heading.
  4549. This takes into account the setting of `org-odd-levels-only'."
  4550. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4551. (defun org-get-valid-level (level &optional change)
  4552. "Rectify a level change under the influence of `org-odd-levels-only'
  4553. LEVEL is a current level, CHANGE is by how much the level should be
  4554. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4555. even level numbers will become the next higher odd number."
  4556. (if org-odd-levels-only
  4557. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4558. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4559. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4560. (max 1 (+ level change))))
  4561. (if (boundp 'define-obsolete-function-alias)
  4562. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4563. (define-obsolete-function-alias 'org-get-legal-level
  4564. 'org-get-valid-level)
  4565. (define-obsolete-function-alias 'org-get-legal-level
  4566. 'org-get-valid-level "23.1")))
  4567. (defun org-promote ()
  4568. "Promote the current heading higher up the tree.
  4569. If the region is active in `transient-mark-mode', promote all headings
  4570. in the region."
  4571. (org-back-to-heading t)
  4572. (let* ((level (save-match-data (funcall outline-level)))
  4573. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4574. (diff (abs (- level (length up-head) -1))))
  4575. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4576. (replace-match up-head nil t)
  4577. ;; Fixup tag positioning
  4578. (and org-auto-align-tags (org-set-tags nil t))
  4579. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4580. (defun org-demote ()
  4581. "Demote the current heading lower down the tree.
  4582. If the region is active in `transient-mark-mode', demote all headings
  4583. in the region."
  4584. (org-back-to-heading t)
  4585. (let* ((level (save-match-data (funcall outline-level)))
  4586. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4587. (diff (abs (- level (length down-head) -1))))
  4588. (replace-match down-head nil t)
  4589. ;; Fixup tag positioning
  4590. (and org-auto-align-tags (org-set-tags nil t))
  4591. (if org-adapt-indentation (org-fixup-indentation diff))))
  4592. (defun org-map-tree (fun)
  4593. "Call FUN for every heading underneath the current one."
  4594. (org-back-to-heading)
  4595. (let ((level (funcall outline-level)))
  4596. (save-excursion
  4597. (funcall fun)
  4598. (while (and (progn
  4599. (outline-next-heading)
  4600. (> (funcall outline-level) level))
  4601. (not (eobp)))
  4602. (funcall fun)))))
  4603. (defun org-map-region (fun beg end)
  4604. "Call FUN for every heading between BEG and END."
  4605. (let ((org-ignore-region t))
  4606. (save-excursion
  4607. (setq end (copy-marker end))
  4608. (goto-char beg)
  4609. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4610. (< (point) end))
  4611. (funcall fun))
  4612. (while (and (progn
  4613. (outline-next-heading)
  4614. (< (point) end))
  4615. (not (eobp)))
  4616. (funcall fun)))))
  4617. (defun org-fixup-indentation (diff)
  4618. "Change the indentation in the current entry by DIFF
  4619. However, if any line in the current entry has no indentation, or if it
  4620. would end up with no indentation after the change, nothing at all is done."
  4621. (save-excursion
  4622. (let ((end (save-excursion (outline-next-heading)
  4623. (point-marker)))
  4624. (prohibit (if (> diff 0)
  4625. "^\\S-"
  4626. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4627. col)
  4628. (unless (save-excursion (end-of-line 1)
  4629. (re-search-forward prohibit end t))
  4630. (while (and (< (point) end)
  4631. (re-search-forward "^[ \t]+" end t))
  4632. (goto-char (match-end 0))
  4633. (setq col (current-column))
  4634. (if (< diff 0) (replace-match ""))
  4635. (org-indent-to-column (+ diff col))))
  4636. (move-marker end nil))))
  4637. (defun org-convert-to-odd-levels ()
  4638. "Convert an org-mode file with all levels allowed to one with odd levels.
  4639. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4640. level 5 etc."
  4641. (interactive)
  4642. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4643. (let ((org-odd-levels-only nil) n)
  4644. (save-excursion
  4645. (goto-char (point-min))
  4646. (while (re-search-forward "^\\*\\*+ " nil t)
  4647. (setq n (- (length (match-string 0)) 2))
  4648. (while (>= (setq n (1- n)) 0)
  4649. (org-demote))
  4650. (end-of-line 1))))))
  4651. (defun org-convert-to-oddeven-levels ()
  4652. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4653. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4654. section with an even level, conversion would destroy the structure of the file. An error
  4655. is signaled in this case."
  4656. (interactive)
  4657. (goto-char (point-min))
  4658. ;; First check if there are no even levels
  4659. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4660. (org-show-context t)
  4661. (error "Not all levels are odd in this file. Conversion not possible."))
  4662. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4663. (let ((org-odd-levels-only nil) n)
  4664. (save-excursion
  4665. (goto-char (point-min))
  4666. (while (re-search-forward "^\\*\\*+ " nil t)
  4667. (setq n (/ (1- (length (match-string 0))) 2))
  4668. (while (>= (setq n (1- n)) 0)
  4669. (org-promote))
  4670. (end-of-line 1))))))
  4671. (defun org-tr-level (n)
  4672. "Make N odd if required."
  4673. (if org-odd-levels-only (1+ (/ n 2)) n))
  4674. ;;; Vertical tree motion, cutting and pasting of subtrees
  4675. (defun org-move-subtree-up (&optional arg)
  4676. "Move the current subtree up past ARG headlines of the same level."
  4677. (interactive "p")
  4678. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4679. (defun org-move-subtree-down (&optional arg)
  4680. "Move the current subtree down past ARG headlines of the same level."
  4681. (interactive "p")
  4682. (setq arg (prefix-numeric-value arg))
  4683. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4684. 'outline-get-last-sibling))
  4685. (ins-point (make-marker))
  4686. (cnt (abs arg))
  4687. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4688. ;; Select the tree
  4689. (org-back-to-heading)
  4690. (setq beg0 (point))
  4691. (save-excursion
  4692. (setq ne-beg (org-back-over-empty-lines))
  4693. (setq beg (point)))
  4694. (save-match-data
  4695. (save-excursion (outline-end-of-heading)
  4696. (setq folded (org-invisible-p)))
  4697. (outline-end-of-subtree))
  4698. (outline-next-heading)
  4699. (setq ne-end (org-back-over-empty-lines))
  4700. (setq end (point))
  4701. (goto-char beg0)
  4702. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4703. ;; include less whitespace
  4704. (save-excursion
  4705. (goto-char beg)
  4706. (forward-line (- ne-beg ne-end))
  4707. (setq beg (point))))
  4708. ;; Find insertion point, with error handling
  4709. (while (> cnt 0)
  4710. (or (and (funcall movfunc) (looking-at outline-regexp))
  4711. (progn (goto-char beg0)
  4712. (error "Cannot move past superior level or buffer limit")))
  4713. (setq cnt (1- cnt)))
  4714. (if (> arg 0)
  4715. ;; Moving forward - still need to move over subtree
  4716. (progn (org-end-of-subtree t t)
  4717. (save-excursion
  4718. (org-back-over-empty-lines)
  4719. (or (bolp) (newline)))))
  4720. (setq ne-ins (org-back-over-empty-lines))
  4721. (move-marker ins-point (point))
  4722. (setq txt (buffer-substring beg end))
  4723. (org-save-markers-in-region beg end)
  4724. (delete-region beg end)
  4725. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4726. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4727. (let ((bbb (point)))
  4728. (insert-before-markers txt)
  4729. (org-reinstall-markers-in-region bbb)
  4730. (move-marker ins-point bbb))
  4731. (or (bolp) (insert "\n"))
  4732. (setq ins-end (point))
  4733. (goto-char ins-point)
  4734. (org-skip-whitespace)
  4735. (when (and (< arg 0)
  4736. (org-first-sibling-p)
  4737. (> ne-ins ne-beg))
  4738. ;; Move whitespace back to beginning
  4739. (save-excursion
  4740. (goto-char ins-end)
  4741. (let ((kill-whole-line t))
  4742. (kill-line (- ne-ins ne-beg)) (point)))
  4743. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4744. (move-marker ins-point nil)
  4745. (org-compact-display-after-subtree-move)
  4746. (org-show-empty-lines-in-parent)
  4747. (unless folded
  4748. (org-show-entry)
  4749. (show-children)
  4750. (org-cycle-hide-drawers 'children))))
  4751. (defvar org-subtree-clip ""
  4752. "Clipboard for cut and paste of subtrees.
  4753. This is actually only a copy of the kill, because we use the normal kill
  4754. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4755. (defvar org-subtree-clip-folded nil
  4756. "Was the last copied subtree folded?
  4757. This is used to fold the tree back after pasting.")
  4758. (defun org-cut-subtree (&optional n)
  4759. "Cut the current subtree into the clipboard.
  4760. With prefix arg N, cut this many sequential subtrees.
  4761. This is a short-hand for marking the subtree and then cutting it."
  4762. (interactive "p")
  4763. (org-copy-subtree n 'cut))
  4764. (defun org-copy-subtree (&optional n cut force-store-markers)
  4765. "Cut the current subtree into the clipboard.
  4766. With prefix arg N, cut this many sequential subtrees.
  4767. This is a short-hand for marking the subtree and then copying it.
  4768. If CUT is non-nil, actually cut the subtree.
  4769. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4770. of some markers in the region, even if CUT is non-nil. This is
  4771. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4772. (interactive "p")
  4773. (let (beg end folded (beg0 (point)))
  4774. (if (interactive-p)
  4775. (org-back-to-heading nil) ; take what looks like a subtree
  4776. (org-back-to-heading t)) ; take what is really there
  4777. (org-back-over-empty-lines)
  4778. (setq beg (point))
  4779. (skip-chars-forward " \t\r\n")
  4780. (save-match-data
  4781. (save-excursion (outline-end-of-heading)
  4782. (setq folded (org-invisible-p)))
  4783. (condition-case nil
  4784. (outline-forward-same-level (1- n))
  4785. (error nil))
  4786. (org-end-of-subtree t t))
  4787. (org-back-over-empty-lines)
  4788. (setq end (point))
  4789. (goto-char beg0)
  4790. (when (> end beg)
  4791. (setq org-subtree-clip-folded folded)
  4792. (when (or cut force-store-markers)
  4793. (org-save-markers-in-region beg end))
  4794. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4795. (setq org-subtree-clip (current-kill 0))
  4796. (message "%s: Subtree(s) with %d characters"
  4797. (if cut "Cut" "Copied")
  4798. (length org-subtree-clip)))))
  4799. (defun org-paste-subtree (&optional level tree for-yank)
  4800. "Paste the clipboard as a subtree, with modification of headline level.
  4801. The entire subtree is promoted or demoted in order to match a new headline
  4802. level.
  4803. If the cursor is at the beginning of a headline, the same level as
  4804. that headline is used to paste the tree
  4805. If not, the new level is derived from the *visible* headings
  4806. before and after the insertion point, and taken to be the inferior headline
  4807. level of the two. So if the previous visible heading is level 3 and the
  4808. next is level 4 (or vice versa), level 4 will be used for insertion.
  4809. This makes sure that the subtree remains an independent subtree and does
  4810. not swallow low level entries.
  4811. You can also force a different level, either by using a numeric prefix
  4812. argument, or by inserting the heading marker by hand. For example, if the
  4813. cursor is after \"*****\", then the tree will be shifted to level 5.
  4814. If optional TREE is given, use this text instead of the kill ring.
  4815. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4816. move back over whitespace before inserting, and move point to the end of
  4817. the inserted text when done."
  4818. (interactive "P")
  4819. (unless (org-kill-is-subtree-p tree)
  4820. (error "%s"
  4821. (substitute-command-keys
  4822. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4823. (let* ((visp (not (org-invisible-p)))
  4824. (txt (or tree (and kill-ring (current-kill 0))))
  4825. (^re (concat "^\\(" outline-regexp "\\)"))
  4826. (re (concat "\\(" outline-regexp "\\)"))
  4827. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4828. (old-level (if (string-match ^re txt)
  4829. (- (match-end 0) (match-beginning 0) 1)
  4830. -1))
  4831. (force-level (cond (level (prefix-numeric-value level))
  4832. ((and (looking-at "[ \t]*$")
  4833. (string-match
  4834. ^re_ (buffer-substring
  4835. (point-at-bol) (point))))
  4836. (- (match-end 1) (match-beginning 1)))
  4837. ((and (bolp)
  4838. (looking-at org-outline-regexp))
  4839. (- (match-end 0) (point) 1))
  4840. (t nil)))
  4841. (previous-level (save-excursion
  4842. (condition-case nil
  4843. (progn
  4844. (outline-previous-visible-heading 1)
  4845. (if (looking-at re)
  4846. (- (match-end 0) (match-beginning 0) 1)
  4847. 1))
  4848. (error 1))))
  4849. (next-level (save-excursion
  4850. (condition-case nil
  4851. (progn
  4852. (or (looking-at outline-regexp)
  4853. (outline-next-visible-heading 1))
  4854. (if (looking-at re)
  4855. (- (match-end 0) (match-beginning 0) 1)
  4856. 1))
  4857. (error 1))))
  4858. (new-level (or force-level (max previous-level next-level)))
  4859. (shift (if (or (= old-level -1)
  4860. (= new-level -1)
  4861. (= old-level new-level))
  4862. 0
  4863. (- new-level old-level)))
  4864. (delta (if (> shift 0) -1 1))
  4865. (func (if (> shift 0) 'org-demote 'org-promote))
  4866. (org-odd-levels-only nil)
  4867. beg end newend)
  4868. ;; Remove the forced level indicator
  4869. (if force-level
  4870. (delete-region (point-at-bol) (point)))
  4871. ;; Paste
  4872. (beginning-of-line 1)
  4873. (unless for-yank (org-back-over-empty-lines))
  4874. (setq beg (point))
  4875. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4876. (insert-before-markers txt)
  4877. (unless (string-match "\n\\'" txt) (insert "\n"))
  4878. (setq newend (point))
  4879. (org-reinstall-markers-in-region beg)
  4880. (setq end (point))
  4881. (goto-char beg)
  4882. (skip-chars-forward " \t\n\r")
  4883. (setq beg (point))
  4884. (if (and (org-invisible-p) visp)
  4885. (save-excursion (outline-show-heading)))
  4886. ;; Shift if necessary
  4887. (unless (= shift 0)
  4888. (save-restriction
  4889. (narrow-to-region beg end)
  4890. (while (not (= shift 0))
  4891. (org-map-region func (point-min) (point-max))
  4892. (setq shift (+ delta shift)))
  4893. (goto-char (point-min))
  4894. (setq newend (point-max))))
  4895. (when (or (interactive-p) for-yank)
  4896. (message "Clipboard pasted as level %d subtree" new-level))
  4897. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4898. kill-ring
  4899. (eq org-subtree-clip (current-kill 0))
  4900. org-subtree-clip-folded)
  4901. ;; The tree was folded before it was killed/copied
  4902. (hide-subtree))
  4903. (and for-yank (goto-char newend))))
  4904. (defun org-kill-is-subtree-p (&optional txt)
  4905. "Check if the current kill is an outline subtree, or a set of trees.
  4906. Returns nil if kill does not start with a headline, or if the first
  4907. headline level is not the largest headline level in the tree.
  4908. So this will actually accept several entries of equal levels as well,
  4909. which is OK for `org-paste-subtree'.
  4910. If optional TXT is given, check this string instead of the current kill."
  4911. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4912. (start-level (and kill
  4913. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4914. org-outline-regexp "\\)")
  4915. kill)
  4916. (- (match-end 2) (match-beginning 2) 1)))
  4917. (re (concat "^" org-outline-regexp))
  4918. (start (1+ (or (match-beginning 2) -1))))
  4919. (if (not start-level)
  4920. (progn
  4921. nil) ;; does not even start with a heading
  4922. (catch 'exit
  4923. (while (setq start (string-match re kill (1+ start)))
  4924. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4925. (throw 'exit nil)))
  4926. t))))
  4927. (defvar org-markers-to-move nil
  4928. "Markers that should be moved with a cut-and-paste operation.
  4929. Those markers are stored together with their positions relative to
  4930. the start of the region.")
  4931. (defun org-save-markers-in-region (beg end)
  4932. "Check markers in region.
  4933. If these markers are between BEG and END, record their position relative
  4934. to BEG, so that after moving the block of text, we can put the markers back
  4935. into place.
  4936. This function gets called just before an entry or tree gets cut from the
  4937. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4938. called immediately, to move the markers with the entries."
  4939. (setq org-markers-to-move nil)
  4940. (when (featurep 'org-clock)
  4941. (org-clock-save-markers-for-cut-and-paste beg end))
  4942. (when (featurep 'org-agenda)
  4943. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4944. (defun org-check-and-save-marker (marker beg end)
  4945. "Check if MARKER is between BEG and END.
  4946. If yes, remember the marker and the distance to BEG."
  4947. (when (and (marker-buffer marker)
  4948. (equal (marker-buffer marker) (current-buffer)))
  4949. (if (and (>= marker beg) (< marker end))
  4950. (push (cons marker (- marker beg)) org-markers-to-move))))
  4951. (defun org-reinstall-markers-in-region (beg)
  4952. "Move all remembered markers to their position relative to BEG."
  4953. (mapc (lambda (x)
  4954. (move-marker (car x) (+ beg (cdr x))))
  4955. org-markers-to-move)
  4956. (setq org-markers-to-move nil))
  4957. (defun org-narrow-to-subtree ()
  4958. "Narrow buffer to the current subtree."
  4959. (interactive)
  4960. (save-excursion
  4961. (save-match-data
  4962. (narrow-to-region
  4963. (progn (org-back-to-heading) (point))
  4964. (progn (org-end-of-subtree t) (point))))))
  4965. ;;; Outline Sorting
  4966. (defun org-sort (with-case)
  4967. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4968. Optional argument WITH-CASE means sort case-sensitively."
  4969. (interactive "P")
  4970. (if (org-at-table-p)
  4971. (org-call-with-arg 'org-table-sort-lines with-case)
  4972. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4973. (defun org-sort-remove-invisible (s)
  4974. (remove-text-properties 0 (length s) org-rm-props s)
  4975. (while (string-match org-bracket-link-regexp s)
  4976. (setq s (replace-match (if (match-end 2)
  4977. (match-string 3 s)
  4978. (match-string 1 s)) t t s)))
  4979. s)
  4980. (defvar org-priority-regexp) ; defined later in the file
  4981. (defun org-sort-entries-or-items
  4982. (&optional with-case sorting-type getkey-func compare-func property)
  4983. "Sort entries on a certain level of an outline tree.
  4984. If there is an active region, the entries in the region are sorted.
  4985. Else, if the cursor is before the first entry, sort the top-level items.
  4986. Else, the children of the entry at point are sorted.
  4987. Sorting can be alphabetically, numerically, and by date/time as given by
  4988. the first time stamp in the entry. The command prompts for the sorting
  4989. type unless it has been given to the function through the SORTING-TYPE
  4990. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4991. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4992. called with point at the beginning of the record. It must return either
  4993. a string or a number that should serve as the sorting key for that record.
  4994. Comparing entries ignores case by default. However, with an optional argument
  4995. WITH-CASE, the sorting considers case as well."
  4996. (interactive "P")
  4997. (let ((case-func (if with-case 'identity 'downcase))
  4998. start beg end stars re re2
  4999. txt what tmp plain-list-p)
  5000. ;; Find beginning and end of region to sort
  5001. (cond
  5002. ((org-region-active-p)
  5003. ;; we will sort the region
  5004. (setq end (region-end)
  5005. what "region")
  5006. (goto-char (region-beginning))
  5007. (if (not (org-on-heading-p)) (outline-next-heading))
  5008. (setq start (point)))
  5009. ((org-at-item-p)
  5010. ;; we will sort this plain list
  5011. (org-beginning-of-item-list) (setq start (point))
  5012. (org-end-of-item-list) (setq end (point))
  5013. (goto-char start)
  5014. (setq plain-list-p t
  5015. what "plain list"))
  5016. ((or (org-on-heading-p)
  5017. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5018. ;; we will sort the children of the current headline
  5019. (org-back-to-heading)
  5020. (setq start (point)
  5021. end (progn (org-end-of-subtree t t)
  5022. (org-back-over-empty-lines)
  5023. (point))
  5024. what "children")
  5025. (goto-char start)
  5026. (show-subtree)
  5027. (outline-next-heading))
  5028. (t
  5029. ;; we will sort the top-level entries in this file
  5030. (goto-char (point-min))
  5031. (or (org-on-heading-p) (outline-next-heading))
  5032. (setq start (point) end (point-max) what "top-level")
  5033. (goto-char start)
  5034. (show-all)))
  5035. (setq beg (point))
  5036. (if (>= beg end) (error "Nothing to sort"))
  5037. (unless plain-list-p
  5038. (looking-at "\\(\\*+\\)")
  5039. (setq stars (match-string 1)
  5040. re (concat "^" (regexp-quote stars) " +")
  5041. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5042. txt (buffer-substring beg end))
  5043. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5044. (if (and (not (equal stars "*")) (string-match re2 txt))
  5045. (error "Region to sort contains a level above the first entry")))
  5046. (unless sorting-type
  5047. (message
  5048. (if plain-list-p
  5049. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5050. "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:")
  5051. what)
  5052. (setq sorting-type (read-char-exclusive))
  5053. (and (= (downcase sorting-type) ?f)
  5054. (setq getkey-func
  5055. (org-ido-completing-read "Sort using function: "
  5056. obarray 'fboundp t nil nil))
  5057. (setq getkey-func (intern getkey-func)))
  5058. (and (= (downcase sorting-type) ?r)
  5059. (setq property
  5060. (org-ido-completing-read "Property: "
  5061. (mapcar 'list (org-buffer-property-keys t))
  5062. nil t))))
  5063. (message "Sorting entries...")
  5064. (save-restriction
  5065. (narrow-to-region start end)
  5066. (let ((dcst (downcase sorting-type))
  5067. (now (current-time)))
  5068. (sort-subr
  5069. (/= dcst sorting-type)
  5070. ;; This function moves to the beginning character of the "record" to
  5071. ;; be sorted.
  5072. (if plain-list-p
  5073. (lambda nil
  5074. (if (org-at-item-p) t (goto-char (point-max))))
  5075. (lambda nil
  5076. (if (re-search-forward re nil t)
  5077. (goto-char (match-beginning 0))
  5078. (goto-char (point-max)))))
  5079. ;; This function moves to the last character of the "record" being
  5080. ;; sorted.
  5081. (if plain-list-p
  5082. 'org-end-of-item
  5083. (lambda nil
  5084. (save-match-data
  5085. (condition-case nil
  5086. (outline-forward-same-level 1)
  5087. (error
  5088. (goto-char (point-max)))))))
  5089. ;; This function returns the value that gets sorted against.
  5090. (if plain-list-p
  5091. (lambda nil
  5092. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5093. (cond
  5094. ((= dcst ?n)
  5095. (string-to-number (buffer-substring (match-end 0)
  5096. (point-at-eol))))
  5097. ((= dcst ?a)
  5098. (buffer-substring (match-end 0) (point-at-eol)))
  5099. ((= dcst ?t)
  5100. (if (re-search-forward org-ts-regexp
  5101. (point-at-eol) t)
  5102. (org-time-string-to-time (match-string 0))
  5103. now))
  5104. ((= dcst ?f)
  5105. (if getkey-func
  5106. (progn
  5107. (setq tmp (funcall getkey-func))
  5108. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5109. tmp)
  5110. (error "Invalid key function `%s'" getkey-func)))
  5111. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5112. (lambda nil
  5113. (cond
  5114. ((= dcst ?n)
  5115. (if (looking-at org-complex-heading-regexp)
  5116. (string-to-number (match-string 4))
  5117. nil))
  5118. ((= dcst ?a)
  5119. (if (looking-at org-complex-heading-regexp)
  5120. (funcall case-func (match-string 4))
  5121. nil))
  5122. ((= dcst ?t)
  5123. (if (re-search-forward org-ts-regexp
  5124. (save-excursion
  5125. (forward-line 2)
  5126. (point)) t)
  5127. (org-time-string-to-time (match-string 0))
  5128. now))
  5129. ((= dcst ?p)
  5130. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5131. (string-to-char (match-string 2))
  5132. org-default-priority))
  5133. ((= dcst ?r)
  5134. (or (org-entry-get nil property) ""))
  5135. ((= dcst ?o)
  5136. (if (looking-at org-complex-heading-regexp)
  5137. (- 9999 (length (member (match-string 2)
  5138. org-todo-keywords-1)))))
  5139. ((= dcst ?f)
  5140. (if getkey-func
  5141. (progn
  5142. (setq tmp (funcall getkey-func))
  5143. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5144. tmp)
  5145. (error "Invalid key function `%s'" getkey-func)))
  5146. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5147. nil
  5148. (cond
  5149. ((= dcst ?a) 'string<)
  5150. ((= dcst ?t) 'time-less-p)
  5151. ((= dcst ?f) compare-func)
  5152. (t nil)))))
  5153. (message "Sorting entries...done")))
  5154. (defun org-do-sort (table what &optional with-case sorting-type)
  5155. "Sort TABLE of WHAT according to SORTING-TYPE.
  5156. The user will be prompted for the SORTING-TYPE if the call to this
  5157. function does not specify it. WHAT is only for the prompt, to indicate
  5158. what is being sorted. The sorting key will be extracted from
  5159. the car of the elements of the table.
  5160. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5161. (unless sorting-type
  5162. (message
  5163. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5164. what)
  5165. (setq sorting-type (read-char-exclusive)))
  5166. (let ((dcst (downcase sorting-type))
  5167. extractfun comparefun)
  5168. ;; Define the appropriate functions
  5169. (cond
  5170. ((= dcst ?n)
  5171. (setq extractfun 'string-to-number
  5172. comparefun (if (= dcst sorting-type) '< '>)))
  5173. ((= dcst ?a)
  5174. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5175. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5176. comparefun (if (= dcst sorting-type)
  5177. 'string<
  5178. (lambda (a b) (and (not (string< a b))
  5179. (not (string= a b)))))))
  5180. ((= dcst ?t)
  5181. (setq extractfun
  5182. (lambda (x)
  5183. (if (string-match org-ts-regexp x)
  5184. (time-to-seconds
  5185. (org-time-string-to-time (match-string 0 x)))
  5186. 0))
  5187. comparefun (if (= dcst sorting-type) '< '>)))
  5188. (t (error "Invalid sorting type `%c'" sorting-type)))
  5189. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5190. table)
  5191. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5192. ;;; Editing source examples
  5193. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5194. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5195. (defvar org-edit-src-force-single-line nil)
  5196. (defvar org-edit-src-from-org-mode nil)
  5197. (defvar org-edit-src-picture nil)
  5198. (define-minor-mode org-exit-edit-mode
  5199. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5200. (defun org-edit-src-code ()
  5201. "Edit the source code example at point.
  5202. An indirect buffer is created, and that buffer is then narrowed to the
  5203. example at point and switched to the correct language mode. When done,
  5204. exit by killing the buffer with \\[org-edit-src-exit]."
  5205. (interactive)
  5206. (let ((line (org-current-line))
  5207. (case-fold-search t)
  5208. (msg (substitute-command-keys
  5209. "Edit, then exit with C-c ' (C-c and single quote)"))
  5210. (info (org-edit-src-find-region-and-lang))
  5211. (org-mode-p (eq major-mode 'org-mode))
  5212. beg end lang lang-f single lfmt)
  5213. (if (not info)
  5214. nil
  5215. (setq beg (nth 0 info)
  5216. end (nth 1 info)
  5217. lang (nth 2 info)
  5218. single (nth 3 info)
  5219. lfmt (nth 4 info)
  5220. lang-f (intern (concat lang "-mode")))
  5221. (unless (functionp lang-f)
  5222. (error "No such language mode: %s" lang-f))
  5223. (goto-line line)
  5224. (if (get-buffer "*Org Edit Src Example*")
  5225. (kill-buffer "*Org Edit Src Example*"))
  5226. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5227. "*Org Edit Src Example*"))
  5228. (narrow-to-region beg end)
  5229. (remove-text-properties beg end '(display nil invisible nil
  5230. intangible nil))
  5231. (let ((org-inhibit-startup t))
  5232. (funcall lang-f))
  5233. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5234. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5235. (when lfmt
  5236. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5237. (when org-mode-p
  5238. (goto-char (point-min))
  5239. (while (re-search-forward "^," nil t)
  5240. (replace-match "")))
  5241. (goto-line line)
  5242. (org-exit-edit-mode)
  5243. (org-set-local 'header-line-format msg)
  5244. (message "%s" msg)
  5245. t)))
  5246. (defun org-edit-fixed-width-region ()
  5247. "Edit the fixed-width ascii drawing at point.
  5248. This must be a region where each line starts with a colon followed by
  5249. a space character.
  5250. An indirect buffer is created, and that buffer is then narrowed to the
  5251. example at point and switched to artist-mode. When done,
  5252. exit by killing the buffer with \\[org-edit-src-exit]."
  5253. (interactive)
  5254. (let ((line (org-current-line))
  5255. (case-fold-search t)
  5256. (msg (substitute-command-keys
  5257. "Edit, then exit with C-c ' (C-c and single quote)"))
  5258. (org-mode-p (eq major-mode 'org-mode))
  5259. beg end lang lang-f)
  5260. (beginning-of-line 1)
  5261. (if (looking-at "[ \t]*[^:\n \t]")
  5262. nil
  5263. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5264. (setq beg (point) end beg)
  5265. (save-excursion
  5266. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5267. (setq beg (point-at-bol 2))
  5268. (setq beg (point))))
  5269. (save-excursion
  5270. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5271. (setq end (1- (match-beginning 0)))
  5272. (setq end (point))))
  5273. (goto-line line))
  5274. (if (get-buffer "*Org Edit Picture*")
  5275. (kill-buffer "*Org Edit Picture*"))
  5276. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5277. "*Org Edit Picture*"))
  5278. (narrow-to-region beg end)
  5279. (remove-text-properties beg end '(display nil invisible nil
  5280. intangible nil))
  5281. (when (fboundp 'font-lock-unfontify-region)
  5282. (font-lock-unfontify-region (point-min) (point-max)))
  5283. (cond
  5284. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5285. (fundamental-mode)
  5286. (artist-mode 1))
  5287. (t (funcall org-edit-fixed-width-region-mode)))
  5288. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5289. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5290. (set (make-local-variable 'org-edit-src-picture) t)
  5291. (goto-char (point-min))
  5292. (while (re-search-forward "^[ \t]*: ?" nil t)
  5293. (replace-match ""))
  5294. (goto-line line)
  5295. (org-exit-edit-mode)
  5296. (org-set-local 'header-line-format msg)
  5297. (message "%s" msg)
  5298. t)))
  5299. (defun org-edit-src-find-region-and-lang ()
  5300. "Find the region and language for a local edit.
  5301. Return a list with beginning and end of the region, a string representing
  5302. the language, a switch telling of the content should be in a single line."
  5303. (let ((re-list
  5304. (append
  5305. org-edit-src-region-extra
  5306. '(
  5307. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5308. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5309. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5310. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5311. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5312. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5313. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5314. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5315. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5316. ("^#\\+html:" "\n" "html" single-line)
  5317. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5318. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5319. ("^#\\+latex:" "\n" "latex" single-line)
  5320. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5321. ("^#\\+ascii:" "\n" "ascii" single-line)
  5322. )))
  5323. (pos (point))
  5324. re re1 re2 single beg end lang lfmt match-re1)
  5325. (catch 'exit
  5326. (while (setq entry (pop re-list))
  5327. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5328. single (nth 3 entry))
  5329. (save-excursion
  5330. (if (or (looking-at re1)
  5331. (re-search-backward re1 nil t))
  5332. (progn
  5333. (setq match-re1 (match-string 0))
  5334. (setq beg (match-end 0)
  5335. lang (org-edit-src-get-lang lang)
  5336. lfmt (org-edit-src-get-label-format match-re1))
  5337. (if (and (re-search-forward re2 nil t)
  5338. (>= (match-end 0) pos))
  5339. (throw 'exit (list beg (match-beginning 0)
  5340. lang single lfmt))))
  5341. (if (or (looking-at re2)
  5342. (re-search-forward re2 nil t))
  5343. (progn
  5344. (setq end (match-beginning 0))
  5345. (if (and (re-search-backward re1 nil t)
  5346. (<= (match-beginning 0) pos))
  5347. (progn
  5348. (setq lfmt (org-edit-src-get-label-format
  5349. (match-string 0)))
  5350. (throw 'exit
  5351. (list (match-end 0) end
  5352. (org-edit-src-get-lang lang)
  5353. single lfmt))))))))))))
  5354. (defun org-edit-src-get-lang (lang)
  5355. "Extract the src language."
  5356. (let ((m (match-string 0)))
  5357. (cond
  5358. ((stringp lang) lang)
  5359. ((integerp lang) (match-string lang))
  5360. ((and (eq lang 'lang)
  5361. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5362. (match-string 1 m))
  5363. ((and (eq lang 'style)
  5364. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5365. (match-string 1 m))
  5366. (t "fundamental"))))
  5367. (defun org-edit-src-get-label-format (s)
  5368. "Extract the label format."
  5369. (save-match-data
  5370. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5371. (match-string 1 s))))
  5372. (defun org-edit-src-exit ()
  5373. "Exit special edit and protect problematic lines."
  5374. (interactive)
  5375. (unless (buffer-base-buffer (current-buffer))
  5376. (error "This is not an indirect buffer, something is wrong..."))
  5377. (unless (> (point-min) 1)
  5378. (error "This buffer is not narrowed, something is wrong..."))
  5379. (goto-char (point-min))
  5380. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5381. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5382. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5383. (goto-char (point-min))
  5384. (while (re-search-forward "\n" nil t)
  5385. (replace-match " "))
  5386. (goto-char (point-min))
  5387. (if (looking-at "\\s-*") (replace-match " "))
  5388. (if (re-search-forward "\\s-+\\'" nil t)
  5389. (replace-match "")))
  5390. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5391. (goto-char (point-min))
  5392. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5393. (replace-match ",\\1"))
  5394. (when font-lock-mode
  5395. (font-lock-unfontify-region (point-min) (point-max)))
  5396. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5397. (when (org-bound-and-true-p org-edit-src-picture)
  5398. (untabify (point-min) (point-max))
  5399. (goto-char (point-min))
  5400. (while (re-search-forward "^" nil t)
  5401. (replace-match ": "))
  5402. (when font-lock-mode
  5403. (font-lock-unfontify-region (point-min) (point-max)))
  5404. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5405. (kill-buffer (current-buffer))
  5406. (and (org-mode-p) (org-restart-font-lock)))
  5407. ;;; The orgstruct minor mode
  5408. ;; Define a minor mode which can be used in other modes in order to
  5409. ;; integrate the org-mode structure editing commands.
  5410. ;; This is really a hack, because the org-mode structure commands use
  5411. ;; keys which normally belong to the major mode. Here is how it
  5412. ;; works: The minor mode defines all the keys necessary to operate the
  5413. ;; structure commands, but wraps the commands into a function which
  5414. ;; tests if the cursor is currently at a headline or a plain list
  5415. ;; item. If that is the case, the structure command is used,
  5416. ;; temporarily setting many Org-mode variables like regular
  5417. ;; expressions for filling etc. However, when any of those keys is
  5418. ;; used at a different location, function uses `key-binding' to look
  5419. ;; up if the key has an associated command in another currently active
  5420. ;; keymap (minor modes, major mode, global), and executes that
  5421. ;; command. There might be problems if any of the keys is otherwise
  5422. ;; used as a prefix key.
  5423. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5424. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5425. ;; addresses this by checking explicitly for both bindings.
  5426. (defvar orgstruct-mode-map (make-sparse-keymap)
  5427. "Keymap for the minor `orgstruct-mode'.")
  5428. (defvar org-local-vars nil
  5429. "List of local variables, for use by `orgstruct-mode'")
  5430. ;;;###autoload
  5431. (define-minor-mode orgstruct-mode
  5432. "Toggle the minor more `orgstruct-mode'.
  5433. This mode is for using Org-mode structure commands in other modes.
  5434. The following key behave as if Org-mode was active, if the cursor
  5435. is on a headline, or on a plain list item (both in the definition
  5436. of Org-mode).
  5437. M-up Move entry/item up
  5438. M-down Move entry/item down
  5439. M-left Promote
  5440. M-right Demote
  5441. M-S-up Move entry/item up
  5442. M-S-down Move entry/item down
  5443. M-S-left Promote subtree
  5444. M-S-right Demote subtree
  5445. M-q Fill paragraph and items like in Org-mode
  5446. C-c ^ Sort entries
  5447. C-c - Cycle list bullet
  5448. TAB Cycle item visibility
  5449. M-RET Insert new heading/item
  5450. S-M-RET Insert new TODO heading / Checkbox item
  5451. C-c C-c Set tags / toggle checkbox"
  5452. nil " OrgStruct" nil
  5453. (org-load-modules-maybe)
  5454. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5455. ;;;###autoload
  5456. (defun turn-on-orgstruct ()
  5457. "Unconditionally turn on `orgstruct-mode'."
  5458. (orgstruct-mode 1))
  5459. ;;;###autoload
  5460. (defun turn-on-orgstruct++ ()
  5461. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5462. In addition to setting orgstruct-mode, this also exports all indentation and
  5463. autofilling variables from org-mode into the buffer. Note that turning
  5464. off orgstruct-mode will *not* remove these additional settings."
  5465. (orgstruct-mode 1)
  5466. (let (var val)
  5467. (mapc
  5468. (lambda (x)
  5469. (when (string-match
  5470. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5471. (symbol-name (car x)))
  5472. (setq var (car x) val (nth 1 x))
  5473. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5474. org-local-vars)))
  5475. (defun orgstruct-error ()
  5476. "Error when there is no default binding for a structure key."
  5477. (interactive)
  5478. (error "This key has no function outside structure elements"))
  5479. (defun orgstruct-setup ()
  5480. "Setup orgstruct keymaps."
  5481. (let ((nfunc 0)
  5482. (bindings
  5483. (list
  5484. '([(meta up)] org-metaup)
  5485. '([(meta down)] org-metadown)
  5486. '([(meta left)] org-metaleft)
  5487. '([(meta right)] org-metaright)
  5488. '([(meta shift up)] org-shiftmetaup)
  5489. '([(meta shift down)] org-shiftmetadown)
  5490. '([(meta shift left)] org-shiftmetaleft)
  5491. '([(meta shift right)] org-shiftmetaright)
  5492. '([(shift up)] org-shiftup)
  5493. '([(shift down)] org-shiftdown)
  5494. '([(shift left)] org-shiftleft)
  5495. '([(shift right)] org-shiftright)
  5496. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5497. '("\M-q" fill-paragraph)
  5498. '("\C-c^" org-sort)
  5499. '("\C-c-" org-cycle-list-bullet)))
  5500. elt key fun cmd)
  5501. (while (setq elt (pop bindings))
  5502. (setq nfunc (1+ nfunc))
  5503. (setq key (org-key (car elt))
  5504. fun (nth 1 elt)
  5505. cmd (orgstruct-make-binding fun nfunc key))
  5506. (org-defkey orgstruct-mode-map key cmd))
  5507. ;; Special treatment needed for TAB and RET
  5508. (org-defkey orgstruct-mode-map [(tab)]
  5509. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5510. (org-defkey orgstruct-mode-map "\C-i"
  5511. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5512. (org-defkey orgstruct-mode-map "\M-\C-m"
  5513. (orgstruct-make-binding 'org-insert-heading 105
  5514. "\M-\C-m" [(meta return)]))
  5515. (org-defkey orgstruct-mode-map [(meta return)]
  5516. (orgstruct-make-binding 'org-insert-heading 106
  5517. [(meta return)] "\M-\C-m"))
  5518. (org-defkey orgstruct-mode-map [(shift meta return)]
  5519. (orgstruct-make-binding 'org-insert-todo-heading 107
  5520. [(meta return)] "\M-\C-m"))
  5521. (unless org-local-vars
  5522. (setq org-local-vars (org-get-local-variables)))
  5523. t))
  5524. (defun orgstruct-make-binding (fun n &rest keys)
  5525. "Create a function for binding in the structure minor mode.
  5526. FUN is the command to call inside a table. N is used to create a unique
  5527. command name. KEYS are keys that should be checked in for a command
  5528. to execute outside of tables."
  5529. (eval
  5530. (list 'defun
  5531. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5532. '(arg)
  5533. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5534. "Outside of structure, run the binding of `"
  5535. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5536. "'.")
  5537. '(interactive "p")
  5538. (list 'if
  5539. '(org-context-p 'headline 'item)
  5540. (list 'org-run-like-in-org-mode (list 'quote fun))
  5541. (list 'let '(orgstruct-mode)
  5542. (list 'call-interactively
  5543. (append '(or)
  5544. (mapcar (lambda (k)
  5545. (list 'key-binding k))
  5546. keys)
  5547. '('orgstruct-error))))))))
  5548. (defun org-context-p (&rest contexts)
  5549. "Check if local context is any of CONTEXTS.
  5550. Possible values in the list of contexts are `table', `headline', and `item'."
  5551. (let ((pos (point)))
  5552. (goto-char (point-at-bol))
  5553. (prog1 (or (and (memq 'table contexts)
  5554. (looking-at "[ \t]*|"))
  5555. (and (memq 'headline contexts)
  5556. ;;????????? (looking-at "\\*+"))
  5557. (looking-at outline-regexp))
  5558. (and (memq 'item contexts)
  5559. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5560. (goto-char pos))))
  5561. (defun org-get-local-variables ()
  5562. "Return a list of all local variables in an org-mode buffer."
  5563. (let (varlist)
  5564. (with-current-buffer (get-buffer-create "*Org tmp*")
  5565. (erase-buffer)
  5566. (org-mode)
  5567. (setq varlist (buffer-local-variables)))
  5568. (kill-buffer "*Org tmp*")
  5569. (delq nil
  5570. (mapcar
  5571. (lambda (x)
  5572. (setq x
  5573. (if (symbolp x)
  5574. (list x)
  5575. (list (car x) (list 'quote (cdr x)))))
  5576. (if (string-match
  5577. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5578. (symbol-name (car x)))
  5579. x nil))
  5580. varlist))))
  5581. ;;;###autoload
  5582. (defun org-run-like-in-org-mode (cmd)
  5583. (org-load-modules-maybe)
  5584. (unless org-local-vars
  5585. (setq org-local-vars (org-get-local-variables)))
  5586. (eval (list 'let org-local-vars
  5587. (list 'call-interactively (list 'quote cmd)))))
  5588. ;;;; Archiving
  5589. (defun org-get-category (&optional pos)
  5590. "Get the category applying to position POS."
  5591. (get-text-property (or pos (point)) 'org-category))
  5592. (defun org-refresh-category-properties ()
  5593. "Refresh category text properties in the buffer."
  5594. (let ((def-cat (cond
  5595. ((null org-category)
  5596. (if buffer-file-name
  5597. (file-name-sans-extension
  5598. (file-name-nondirectory buffer-file-name))
  5599. "???"))
  5600. ((symbolp org-category) (symbol-name org-category))
  5601. (t org-category)))
  5602. beg end cat pos optionp)
  5603. (org-unmodified
  5604. (save-excursion
  5605. (save-restriction
  5606. (widen)
  5607. (goto-char (point-min))
  5608. (put-text-property (point) (point-max) 'org-category def-cat)
  5609. (while (re-search-forward
  5610. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5611. (setq pos (match-end 0)
  5612. optionp (equal (char-after (match-beginning 0)) ?#)
  5613. cat (org-trim (match-string 2)))
  5614. (if optionp
  5615. (setq beg (point-at-bol) end (point-max))
  5616. (org-back-to-heading t)
  5617. (setq beg (point) end (org-end-of-subtree t t)))
  5618. (put-text-property beg end 'org-category cat)
  5619. (goto-char pos)))))))
  5620. ;;;; Link Stuff
  5621. ;;; Link abbreviations
  5622. (defun org-link-expand-abbrev (link)
  5623. "Apply replacements as defined in `org-link-abbrev-alist."
  5624. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5625. (let* ((key (match-string 1 link))
  5626. (as (or (assoc key org-link-abbrev-alist-local)
  5627. (assoc key org-link-abbrev-alist)))
  5628. (tag (and (match-end 2) (match-string 3 link)))
  5629. rpl)
  5630. (if (not as)
  5631. link
  5632. (setq rpl (cdr as))
  5633. (cond
  5634. ((symbolp rpl) (funcall rpl tag))
  5635. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5636. ((string-match "%h" rpl)
  5637. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5638. (t (concat rpl tag)))))
  5639. link))
  5640. ;;; Storing and inserting links
  5641. (defvar org-insert-link-history nil
  5642. "Minibuffer history for links inserted with `org-insert-link'.")
  5643. (defvar org-stored-links nil
  5644. "Contains the links stored with `org-store-link'.")
  5645. (defvar org-store-link-plist nil
  5646. "Plist with info about the most recently link created with `org-store-link'.")
  5647. (defvar org-link-protocols nil
  5648. "Link protocols added to Org-mode using `org-add-link-type'.")
  5649. (defvar org-store-link-functions nil
  5650. "List of functions that are called to create and store a link.
  5651. Each function will be called in turn until one returns a non-nil
  5652. value. Each function should check if it is responsible for creating
  5653. this link (for example by looking at the major mode).
  5654. If not, it must exit and return nil.
  5655. If yes, it should return a non-nil value after a calling
  5656. `org-store-link-props' with a list of properties and values.
  5657. Special properties are:
  5658. :type The link prefix. like \"http\". This must be given.
  5659. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5660. This is obligatory as well.
  5661. :description Optional default description for the second pair
  5662. of brackets in an Org-mode link. The user can still change
  5663. this when inserting this link into an Org-mode buffer.
  5664. In addition to these, any additional properties can be specified
  5665. and then used in remember templates.")
  5666. (defun org-add-link-type (type &optional follow export)
  5667. "Add TYPE to the list of `org-link-types'.
  5668. Re-compute all regular expressions depending on `org-link-types'
  5669. FOLLOW and EXPORT are two functions.
  5670. FOLLOW should take the link path as the single argument and do whatever
  5671. is necessary to follow the link, for example find a file or display
  5672. a mail message.
  5673. EXPORT should format the link path for export to one of the export formats.
  5674. It should be a function accepting three arguments:
  5675. path the path of the link, the text after the prefix (like \"http:\")
  5676. desc the description of the link, if any, nil if there was no description
  5677. format the export format, a symbol like `html' or `latex'.
  5678. The function may use the FORMAT information to return different values
  5679. depending on the format. The return value will be put literally into
  5680. the exported file.
  5681. Org-mode has a built-in default for exporting links. If you are happy with
  5682. this default, there is no need to define an export function for the link
  5683. type. For a simple example of an export function, see `org-bbdb.el'."
  5684. (add-to-list 'org-link-types type t)
  5685. (org-make-link-regexps)
  5686. (if (assoc type org-link-protocols)
  5687. (setcdr (assoc type org-link-protocols) (list follow export))
  5688. (push (list type follow export) org-link-protocols)))
  5689. ;;;###autoload
  5690. (defun org-store-link (arg)
  5691. "\\<org-mode-map>Store an org-link to the current location.
  5692. This link is added to `org-stored-links' and can later be inserted
  5693. into an org-buffer with \\[org-insert-link].
  5694. For some link types, a prefix arg is interpreted:
  5695. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5696. For file links, arg negates `org-context-in-file-links'."
  5697. (interactive "P")
  5698. (org-load-modules-maybe)
  5699. (setq org-store-link-plist nil) ; reset
  5700. (let (link cpltxt desc description search txt)
  5701. (cond
  5702. ((run-hook-with-args-until-success 'org-store-link-functions)
  5703. (setq link (plist-get org-store-link-plist :link)
  5704. desc (or (plist-get org-store-link-plist :description) link)))
  5705. ((equal (buffer-name) "*Org Edit Src Example*")
  5706. (let (label gc)
  5707. (while (or (not label)
  5708. (save-excursion
  5709. (save-restriction
  5710. (widen)
  5711. (goto-char (point-min))
  5712. (re-search-forward
  5713. (regexp-quote (format org-coderef-label-format label))
  5714. nil t))))
  5715. (when label (message "Label exists already") (sit-for 2))
  5716. (setq label (read-string "Code line label: " label)))
  5717. (end-of-line 1)
  5718. (setq link (format org-coderef-label-format label))
  5719. (setq gc (- 79 (length link)))
  5720. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5721. (insert link)
  5722. (setq link (concat "(" label ")") desc nil)))
  5723. ((eq major-mode 'calendar-mode)
  5724. (let ((cd (calendar-cursor-to-date)))
  5725. (setq link
  5726. (format-time-string
  5727. (car org-time-stamp-formats)
  5728. (apply 'encode-time
  5729. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5730. nil nil nil))))
  5731. (org-store-link-props :type "calendar" :date cd)))
  5732. ((eq major-mode 'w3-mode)
  5733. (setq cpltxt (url-view-url t)
  5734. link (org-make-link cpltxt))
  5735. (org-store-link-props :type "w3" :url (url-view-url t)))
  5736. ((eq major-mode 'w3m-mode)
  5737. (setq cpltxt (or w3m-current-title w3m-current-url)
  5738. link (org-make-link w3m-current-url))
  5739. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5740. ((setq search (run-hook-with-args-until-success
  5741. 'org-create-file-search-functions))
  5742. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5743. "::" search))
  5744. (setq cpltxt (or description link)))
  5745. ((eq major-mode 'image-mode)
  5746. (setq cpltxt (concat "file:"
  5747. (abbreviate-file-name buffer-file-name))
  5748. link (org-make-link cpltxt))
  5749. (org-store-link-props :type "image" :file buffer-file-name))
  5750. ((eq major-mode 'dired-mode)
  5751. ;; link to the file in the current line
  5752. (setq cpltxt (concat "file:"
  5753. (abbreviate-file-name
  5754. (expand-file-name
  5755. (dired-get-filename nil t))))
  5756. link (org-make-link cpltxt)))
  5757. ((and buffer-file-name (org-mode-p))
  5758. (cond
  5759. ((org-in-regexp "<<\\(.*?\\)>>")
  5760. (setq cpltxt
  5761. (concat "file:"
  5762. (abbreviate-file-name buffer-file-name)
  5763. "::" (match-string 1))
  5764. link (org-make-link cpltxt)))
  5765. ((and (featurep 'org-id)
  5766. (or (eq org-link-to-org-use-id t)
  5767. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5768. (interactive-p))
  5769. (and org-link-to-org-use-id
  5770. (condition-case nil
  5771. (org-entry-get nil "ID")
  5772. (error nil)))))
  5773. ;; We can make a link using the ID.
  5774. (setq link (condition-case nil
  5775. (prog1 (org-id-store-link)
  5776. (setq desc (plist-get org-store-link-plist
  5777. :description)))
  5778. (error
  5779. ;; probably before first headline, link to file only
  5780. (concat "file:"
  5781. (abbreviate-file-name buffer-file-name))))))
  5782. (t
  5783. ;; Just link to current headline
  5784. (setq cpltxt (concat "file:"
  5785. (abbreviate-file-name buffer-file-name)))
  5786. ;; Add a context search string
  5787. (when (org-xor org-context-in-file-links arg)
  5788. (setq txt (cond
  5789. ((org-on-heading-p) nil)
  5790. ((org-region-active-p)
  5791. (buffer-substring (region-beginning) (region-end)))
  5792. (t nil)))
  5793. (when (or (null txt) (string-match "\\S-" txt))
  5794. (setq cpltxt
  5795. (concat cpltxt "::"
  5796. (condition-case nil
  5797. (org-make-org-heading-search-string txt)
  5798. (error "")))
  5799. desc "NONE")))
  5800. (if (string-match "::\\'" cpltxt)
  5801. (setq cpltxt (substring cpltxt 0 -2)))
  5802. (setq link (org-make-link cpltxt)))))
  5803. ((buffer-file-name (buffer-base-buffer))
  5804. ;; Just link to this file here.
  5805. (setq cpltxt (concat "file:"
  5806. (abbreviate-file-name
  5807. (buffer-file-name (buffer-base-buffer)))))
  5808. ;; Add a context string
  5809. (when (org-xor org-context-in-file-links arg)
  5810. (setq txt (if (org-region-active-p)
  5811. (buffer-substring (region-beginning) (region-end))
  5812. (buffer-substring (point-at-bol) (point-at-eol))))
  5813. ;; Only use search option if there is some text.
  5814. (when (string-match "\\S-" txt)
  5815. (setq cpltxt
  5816. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5817. desc "NONE")))
  5818. (setq link (org-make-link cpltxt)))
  5819. ((interactive-p)
  5820. (error "Cannot link to a buffer which is not visiting a file"))
  5821. (t (setq link nil)))
  5822. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5823. (setq link (or link cpltxt)
  5824. desc (or desc cpltxt))
  5825. (if (equal desc "NONE") (setq desc nil))
  5826. (if (and (interactive-p) link)
  5827. (progn
  5828. (setq org-stored-links
  5829. (cons (list link desc) org-stored-links))
  5830. (message "Stored: %s" (or desc link)))
  5831. (and link (org-make-link-string link desc)))))
  5832. (defun org-store-link-props (&rest plist)
  5833. "Store link properties, extract names and addresses."
  5834. (let (x adr)
  5835. (when (setq x (plist-get plist :from))
  5836. (setq adr (mail-extract-address-components x))
  5837. (setq plist (plist-put plist :fromname (car adr)))
  5838. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5839. (when (setq x (plist-get plist :to))
  5840. (setq adr (mail-extract-address-components x))
  5841. (setq plist (plist-put plist :toname (car adr)))
  5842. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5843. (let ((from (plist-get plist :from))
  5844. (to (plist-get plist :to)))
  5845. (when (and from to org-from-is-user-regexp)
  5846. (setq plist
  5847. (plist-put plist :fromto
  5848. (if (string-match org-from-is-user-regexp from)
  5849. (concat "to %t")
  5850. (concat "from %f"))))))
  5851. (setq org-store-link-plist plist))
  5852. (defun org-add-link-props (&rest plist)
  5853. "Add these properties to the link property list."
  5854. (let (key value)
  5855. (while plist
  5856. (setq key (pop plist) value (pop plist))
  5857. (setq org-store-link-plist
  5858. (plist-put org-store-link-plist key value)))))
  5859. (defun org-email-link-description (&optional fmt)
  5860. "Return the description part of an email link.
  5861. This takes information from `org-store-link-plist' and formats it
  5862. according to FMT (default from `org-email-link-description-format')."
  5863. (setq fmt (or fmt org-email-link-description-format))
  5864. (let* ((p org-store-link-plist)
  5865. (to (plist-get p :toaddress))
  5866. (from (plist-get p :fromaddress))
  5867. (table
  5868. (list
  5869. (cons "%c" (plist-get p :fromto))
  5870. (cons "%F" (plist-get p :from))
  5871. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5872. (cons "%T" (plist-get p :to))
  5873. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5874. (cons "%s" (plist-get p :subject))
  5875. (cons "%m" (plist-get p :message-id)))))
  5876. (when (string-match "%c" fmt)
  5877. ;; Check if the user wrote this message
  5878. (if (and org-from-is-user-regexp from to
  5879. (save-match-data (string-match org-from-is-user-regexp from)))
  5880. (setq fmt (replace-match "to %t" t t fmt))
  5881. (setq fmt (replace-match "from %f" t t fmt))))
  5882. (org-replace-escapes fmt table)))
  5883. (defun org-make-org-heading-search-string (&optional string heading)
  5884. "Make search string for STRING or current headline."
  5885. (interactive)
  5886. (let ((s (or string (org-get-heading))))
  5887. (unless (and string (not heading))
  5888. ;; We are using a headline, clean up garbage in there.
  5889. (if (string-match org-todo-regexp s)
  5890. (setq s (replace-match "" t t s)))
  5891. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5892. (setq s (replace-match "" t t s)))
  5893. (setq s (org-trim s))
  5894. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5895. org-comment-string "\\)") s)
  5896. (setq s (replace-match "" t t s)))
  5897. (while (string-match org-ts-regexp s)
  5898. (setq s (replace-match "" t t s))))
  5899. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5900. (setq s (replace-match " " t t s)))
  5901. (or string (setq s (concat "*" s))) ; Add * for headlines
  5902. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5903. (defun org-make-link (&rest strings)
  5904. "Concatenate STRINGS."
  5905. (apply 'concat strings))
  5906. (defun org-make-link-string (link &optional description)
  5907. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5908. (unless (string-match "\\S-" link)
  5909. (error "Empty link"))
  5910. (when (stringp description)
  5911. ;; Remove brackets from the description, they are fatal.
  5912. (while (string-match "\\[" description)
  5913. (setq description (replace-match "{" t t description)))
  5914. (while (string-match "\\]" description)
  5915. (setq description (replace-match "}" t t description))))
  5916. (when (equal (org-link-escape link) description)
  5917. ;; No description needed, it is identical
  5918. (setq description nil))
  5919. (when (and (not description)
  5920. (not (equal link (org-link-escape link))))
  5921. (setq description (org-extract-attributes link)))
  5922. (concat "[[" (org-link-escape link) "]"
  5923. (if description (concat "[" description "]") "")
  5924. "]"))
  5925. (defconst org-link-escape-chars
  5926. '((?\ . "%20")
  5927. (?\[ . "%5B")
  5928. (?\] . "%5D")
  5929. (?\340 . "%E0") ; `a
  5930. (?\342 . "%E2") ; ^a
  5931. (?\347 . "%E7") ; ,c
  5932. (?\350 . "%E8") ; `e
  5933. (?\351 . "%E9") ; 'e
  5934. (?\352 . "%EA") ; ^e
  5935. (?\356 . "%EE") ; ^i
  5936. (?\364 . "%F4") ; ^o
  5937. (?\371 . "%F9") ; `u
  5938. (?\373 . "%FB") ; ^u
  5939. (?\; . "%3B")
  5940. (?? . "%3F")
  5941. (?= . "%3D")
  5942. (?+ . "%2B")
  5943. )
  5944. "Association list of escapes for some characters problematic in links.
  5945. This is the list that is used for internal purposes.")
  5946. (defconst org-link-escape-chars-browser
  5947. '((?\ . "%20")) ; 32 for the SPC char
  5948. "Association list of escapes for some characters problematic in links.
  5949. This is the list that is used before handing over to the browser.")
  5950. (defun org-link-escape (text &optional table)
  5951. "Escape characters in TEXT that are problematic for links."
  5952. (setq table (or table org-link-escape-chars))
  5953. (when text
  5954. (let ((re (mapconcat (lambda (x) (regexp-quote
  5955. (char-to-string (car x))))
  5956. table "\\|")))
  5957. (while (string-match re text)
  5958. (setq text
  5959. (replace-match
  5960. (cdr (assoc (string-to-char (match-string 0 text))
  5961. table))
  5962. t t text)))
  5963. text)))
  5964. (defun org-link-unescape (text &optional table)
  5965. "Reverse the action of `org-link-escape'."
  5966. (setq table (or table org-link-escape-chars))
  5967. (when text
  5968. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5969. table "\\|")))
  5970. (while (string-match re text)
  5971. (setq text
  5972. (replace-match
  5973. (char-to-string (car (rassoc (match-string 0 text) table)))
  5974. t t text)))
  5975. text)))
  5976. (defun org-xor (a b)
  5977. "Exclusive or."
  5978. (if a (not b) b))
  5979. (defun org-fixup-message-id-for-http (s)
  5980. "Replace special characters in a message id, so it can be used in an http query."
  5981. (while (string-match "<" s)
  5982. (setq s (replace-match "%3C" t t s)))
  5983. (while (string-match ">" s)
  5984. (setq s (replace-match "%3E" t t s)))
  5985. (while (string-match "@" s)
  5986. (setq s (replace-match "%40" t t s)))
  5987. s)
  5988. ;;;###autoload
  5989. (defun org-insert-link-global ()
  5990. "Insert a link like Org-mode does.
  5991. This command can be called in any mode to insert a link in Org-mode syntax."
  5992. (interactive)
  5993. (org-load-modules-maybe)
  5994. (org-run-like-in-org-mode 'org-insert-link))
  5995. (defun org-insert-link (&optional complete-file link-location)
  5996. "Insert a link. At the prompt, enter the link.
  5997. Completion can be used to insert any of the link protocol prefixes like
  5998. http or ftp in use.
  5999. The history can be used to select a link previously stored with
  6000. `org-store-link'. When the empty string is entered (i.e. if you just
  6001. press RET at the prompt), the link defaults to the most recently
  6002. stored link. As SPC triggers completion in the minibuffer, you need to
  6003. use M-SPC or C-q SPC to force the insertion of a space character.
  6004. You will also be prompted for a description, and if one is given, it will
  6005. be displayed in the buffer instead of the link.
  6006. If there is already a link at point, this command will allow you to edit link
  6007. and description parts.
  6008. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6009. be selected using completion. The path to the file will be relative to the
  6010. current directory if the file is in the current directory or a subdirectory.
  6011. Otherwise, the link will be the absolute path as completed in the minibuffer
  6012. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6013. option `org-link-file-path-type'.
  6014. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6015. the current directory or below.
  6016. With three \\[universal-argument] prefixes, negate the meaning of
  6017. `org-keep-stored-link-after-insertion'.
  6018. If `org-make-link-description-function' is non-nil, this function will be
  6019. called with the link target, and the result will be the default
  6020. link description.
  6021. If the LINK-LOCATION parameter is non-nil, this value will be
  6022. used as the link location instead of reading one interactively."
  6023. (interactive "P")
  6024. (let* ((wcf (current-window-configuration))
  6025. (region (if (org-region-active-p)
  6026. (buffer-substring (region-beginning) (region-end))))
  6027. (remove (and region (list (region-beginning) (region-end))))
  6028. (desc region)
  6029. tmphist ; byte-compile incorrectly complains about this
  6030. (link link-location)
  6031. entry file)
  6032. (cond
  6033. (link-location) ; specified by arg, just use it.
  6034. ((org-in-regexp org-bracket-link-regexp 1)
  6035. ;; We do have a link at point, and we are going to edit it.
  6036. (setq remove (list (match-beginning 0) (match-end 0)))
  6037. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6038. (setq link (read-string "Link: "
  6039. (org-link-unescape
  6040. (org-match-string-no-properties 1)))))
  6041. ((or (org-in-regexp org-angle-link-re)
  6042. (org-in-regexp org-plain-link-re))
  6043. ;; Convert to bracket link
  6044. (setq remove (list (match-beginning 0) (match-end 0))
  6045. link (read-string "Link: "
  6046. (org-remove-angle-brackets (match-string 0)))))
  6047. ((member complete-file '((4) (16)))
  6048. ;; Completing read for file names.
  6049. (setq file (read-file-name "File: "))
  6050. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6051. (pwd1 (file-name-as-directory (abbreviate-file-name
  6052. (expand-file-name ".")))))
  6053. (cond
  6054. ((equal complete-file '(16))
  6055. (setq link (org-make-link
  6056. "file:"
  6057. (abbreviate-file-name (expand-file-name file)))))
  6058. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6059. (setq link (org-make-link "file:" (match-string 1 file))))
  6060. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6061. (expand-file-name file))
  6062. (setq link (org-make-link
  6063. "file:" (match-string 1 (expand-file-name file)))))
  6064. (t (setq link (org-make-link "file:" file))))))
  6065. (t
  6066. ;; Read link, with completion for stored links.
  6067. (with-output-to-temp-buffer "*Org Links*"
  6068. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6069. (when org-stored-links
  6070. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6071. (princ (mapconcat
  6072. (lambda (x)
  6073. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6074. (reverse org-stored-links) "\n"))))
  6075. (let ((cw (selected-window)))
  6076. (select-window (get-buffer-window "*Org Links*"))
  6077. (setq truncate-lines t)
  6078. (org-fit-window-to-buffer)
  6079. (select-window cw))
  6080. ;; Fake a link history, containing the stored links.
  6081. (setq tmphist (append (mapcar 'car org-stored-links)
  6082. org-insert-link-history))
  6083. (unwind-protect
  6084. (setq link
  6085. (let ((org-completion-use-ido nil))
  6086. (org-completing-read
  6087. "Link: "
  6088. (append
  6089. (mapcar (lambda (x) (list (concat (car x) ":")))
  6090. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6091. (mapcar (lambda (x) (list (concat x ":")))
  6092. org-link-types))
  6093. nil nil nil
  6094. 'tmphist
  6095. (or (car (car org-stored-links))))))
  6096. (set-window-configuration wcf)
  6097. (kill-buffer "*Org Links*"))
  6098. (setq entry (assoc link org-stored-links))
  6099. (or entry (push link org-insert-link-history))
  6100. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6101. (not org-keep-stored-link-after-insertion))
  6102. (setq org-stored-links (delq (assoc link org-stored-links)
  6103. org-stored-links)))
  6104. (setq desc (or desc (nth 1 entry)))))
  6105. (if (string-match org-plain-link-re link)
  6106. ;; URL-like link, normalize the use of angular brackets.
  6107. (setq link (org-make-link (org-remove-angle-brackets link))))
  6108. ;; Check if we are linking to the current file with a search option
  6109. ;; If yes, simplify the link by using only the search option.
  6110. (when (and buffer-file-name
  6111. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6112. (let* ((path (match-string 1 link))
  6113. (case-fold-search nil)
  6114. (search (match-string 2 link)))
  6115. (save-match-data
  6116. (if (equal (file-truename buffer-file-name) (file-truename path))
  6117. ;; We are linking to this same file, with a search option
  6118. (setq link search)))))
  6119. ;; Check if we can/should use a relative path. If yes, simplify the link
  6120. (when (string-match "^file:\\(.*\\)" link)
  6121. (let* ((path (match-string 1 link))
  6122. (origpath path)
  6123. (case-fold-search nil))
  6124. (cond
  6125. ((or (eq org-link-file-path-type 'absolute)
  6126. (equal complete-file '(16)))
  6127. (setq path (abbreviate-file-name (expand-file-name path))))
  6128. ((eq org-link-file-path-type 'noabbrev)
  6129. (setq path (expand-file-name path)))
  6130. ((eq org-link-file-path-type 'relative)
  6131. (setq path (file-relative-name path)))
  6132. (t
  6133. (save-match-data
  6134. (if (string-match (concat "^" (regexp-quote
  6135. (file-name-as-directory
  6136. (expand-file-name "."))))
  6137. (expand-file-name path))
  6138. ;; We are linking a file with relative path name.
  6139. (setq path (substring (expand-file-name path)
  6140. (match-end 0)))
  6141. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6142. (setq link (concat "file:" path))
  6143. (if (equal desc origpath)
  6144. (setq desc path))))
  6145. (if org-make-link-description-function
  6146. (setq desc (funcall org-make-link-description-function link desc)))
  6147. (setq desc (read-string "Description: " desc))
  6148. (unless (string-match "\\S-" desc) (setq desc nil))
  6149. (if remove (apply 'delete-region remove))
  6150. (insert (org-make-link-string link desc))))
  6151. (defun org-completing-read (&rest args)
  6152. "Completing-read with SPACE being a normal character."
  6153. (let ((minibuffer-local-completion-map
  6154. (copy-keymap minibuffer-local-completion-map)))
  6155. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6156. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6157. (apply 'org-ido-completing-read args)))
  6158. (defun org-ido-completing-read (&rest args)
  6159. "Completing-read using `ido-mode' speedups if available"
  6160. (if (and org-completion-use-ido
  6161. (fboundp 'ido-completing-read)
  6162. (boundp 'ido-mode) ido-mode
  6163. (listp (second args)))
  6164. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6165. (apply 'completing-read args)))
  6166. (defun org-extract-attributes (s)
  6167. "Extract the attributes cookie from a string and set as text property."
  6168. (let (a attr (start 0) key value)
  6169. (save-match-data
  6170. (when (string-match "{{\\([^}]+\\)}}$" s)
  6171. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6172. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6173. (setq key (match-string 1 a) value (match-string 2 a)
  6174. start (match-end 0)
  6175. attr (plist-put attr (intern key) value))))
  6176. (org-add-props s nil 'org-attr attr))
  6177. s))
  6178. (defun org-attributes-to-string (plist)
  6179. "Format a property list into an HTML attribute list."
  6180. (let ((s "") key value)
  6181. (while plist
  6182. (setq key (pop plist) value (pop plist))
  6183. (and value
  6184. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6185. s))
  6186. ;;; Opening/following a link
  6187. (defvar org-link-search-failed nil)
  6188. (defun org-next-link ()
  6189. "Move forward to the next link.
  6190. If the link is in hidden text, expose it."
  6191. (interactive)
  6192. (when (and org-link-search-failed (eq this-command last-command))
  6193. (goto-char (point-min))
  6194. (message "Link search wrapped back to beginning of buffer"))
  6195. (setq org-link-search-failed nil)
  6196. (let* ((pos (point))
  6197. (ct (org-context))
  6198. (a (assoc :link ct)))
  6199. (if a (goto-char (nth 2 a)))
  6200. (if (re-search-forward org-any-link-re nil t)
  6201. (progn
  6202. (goto-char (match-beginning 0))
  6203. (if (org-invisible-p) (org-show-context)))
  6204. (goto-char pos)
  6205. (setq org-link-search-failed t)
  6206. (error "No further link found"))))
  6207. (defun org-previous-link ()
  6208. "Move backward to the previous link.
  6209. If the link is in hidden text, expose it."
  6210. (interactive)
  6211. (when (and org-link-search-failed (eq this-command last-command))
  6212. (goto-char (point-max))
  6213. (message "Link search wrapped back to end of buffer"))
  6214. (setq org-link-search-failed nil)
  6215. (let* ((pos (point))
  6216. (ct (org-context))
  6217. (a (assoc :link ct)))
  6218. (if a (goto-char (nth 1 a)))
  6219. (if (re-search-backward org-any-link-re nil t)
  6220. (progn
  6221. (goto-char (match-beginning 0))
  6222. (if (org-invisible-p) (org-show-context)))
  6223. (goto-char pos)
  6224. (setq org-link-search-failed t)
  6225. (error "No further link found"))))
  6226. (defun org-translate-link (s)
  6227. "Translate a link string if a translation function has been defined."
  6228. (if (and org-link-translation-function
  6229. (fboundp org-link-translation-function)
  6230. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6231. (progn
  6232. (setq s (funcall org-link-translation-function
  6233. (match-string 1) (match-string 2)))
  6234. (concat (car s) ":" (cdr s)))
  6235. s))
  6236. (defun org-translate-link-from-planner (type path)
  6237. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6238. This is still an experimental function, your mileage may vary."
  6239. (cond
  6240. ((member type '("http" "https" "news" "ftp"))
  6241. ;; standard Internet links are the same.
  6242. nil)
  6243. ((and (equal type "irc") (string-match "^//" path))
  6244. ;; Planner has two / at the beginning of an irc link, we have 1.
  6245. ;; We should have zero, actually....
  6246. (setq path (substring path 1)))
  6247. ((and (equal type "lisp") (string-match "^/" path))
  6248. ;; Planner has a slash, we do not.
  6249. (setq type "elisp" path (substring path 1)))
  6250. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6251. ;; A typical message link. Planner has the id after the fina slash,
  6252. ;; we separate it with a hash mark
  6253. (setq path (concat (match-string 1 path) "#"
  6254. (org-remove-angle-brackets (match-string 2 path)))))
  6255. )
  6256. (cons type path))
  6257. (defun org-find-file-at-mouse (ev)
  6258. "Open file link or URL at mouse."
  6259. (interactive "e")
  6260. (mouse-set-point ev)
  6261. (org-open-at-point 'in-emacs))
  6262. (defun org-open-at-mouse (ev)
  6263. "Open file link or URL at mouse."
  6264. (interactive "e")
  6265. (mouse-set-point ev)
  6266. (if (eq major-mode 'org-agenda-mode)
  6267. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6268. (org-open-at-point))
  6269. (defvar org-window-config-before-follow-link nil
  6270. "The window configuration before following a link.
  6271. This is saved in case the need arises to restore it.")
  6272. (defvar org-open-link-marker (make-marker)
  6273. "Marker pointing to the location where `org-open-at-point; was called.")
  6274. ;;;###autoload
  6275. (defun org-open-at-point-global ()
  6276. "Follow a link like Org-mode does.
  6277. This command can be called in any mode to follow a link that has
  6278. Org-mode syntax."
  6279. (interactive)
  6280. (org-run-like-in-org-mode 'org-open-at-point))
  6281. ;;;###autoload
  6282. (defun org-open-link-from-string (s &optional arg)
  6283. "Open a link in the string S, as if it was in Org-mode."
  6284. (interactive "sLink: \nP")
  6285. (with-temp-buffer
  6286. (let ((org-inhibit-startup t))
  6287. (org-mode)
  6288. (insert s)
  6289. (goto-char (point-min))
  6290. (org-open-at-point arg))))
  6291. (defun org-open-at-point (&optional in-emacs)
  6292. "Open link at or after point.
  6293. If there is no link at point, this function will search forward up to
  6294. the end of the current subtree.
  6295. Normally, files will be opened by an appropriate application. If the
  6296. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6297. With a double prefix argument, try to open outside of Emacs, in the
  6298. application the system uses for this file type."
  6299. (interactive "P")
  6300. (org-load-modules-maybe)
  6301. (move-marker org-open-link-marker (point))
  6302. (setq org-window-config-before-follow-link (current-window-configuration))
  6303. (org-remove-occur-highlights nil nil t)
  6304. (cond
  6305. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6306. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6307. (org-footnote-action))
  6308. (t
  6309. (let (type path link line search (pos (point)))
  6310. (catch 'match
  6311. (save-excursion
  6312. (skip-chars-forward "^]\n\r")
  6313. (when (org-in-regexp org-bracket-link-regexp)
  6314. (setq link (org-extract-attributes
  6315. (org-link-unescape (org-match-string-no-properties 1))))
  6316. (while (string-match " *\n *" link)
  6317. (setq link (replace-match " " t t link)))
  6318. (setq link (org-link-expand-abbrev link))
  6319. (cond
  6320. ((or (file-name-absolute-p link)
  6321. (string-match "^\\.\\.?/" link))
  6322. (setq type "file" path link))
  6323. ((string-match org-link-re-with-space3 link)
  6324. (setq type (match-string 1 link) path (match-string 2 link)))
  6325. (t (setq type "thisfile" path link)))
  6326. (throw 'match t)))
  6327. (when (get-text-property (point) 'org-linked-text)
  6328. (setq type "thisfile"
  6329. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6330. (1+ (point)) (point))
  6331. path (buffer-substring
  6332. (previous-single-property-change pos 'org-linked-text)
  6333. (next-single-property-change pos 'org-linked-text)))
  6334. (throw 'match t))
  6335. (save-excursion
  6336. (when (or (org-in-regexp org-angle-link-re)
  6337. (org-in-regexp org-plain-link-re))
  6338. (setq type (match-string 1) path (match-string 2))
  6339. (throw 'match t)))
  6340. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6341. (setq type "tree-match"
  6342. path (match-string 1))
  6343. (throw 'match t))
  6344. (save-excursion
  6345. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6346. (setq type "tags"
  6347. path (match-string 1))
  6348. (while (string-match ":" path)
  6349. (setq path (replace-match "+" t t path)))
  6350. (throw 'match t))))
  6351. (unless path
  6352. (error "No link found"))
  6353. ;; Remove any trailing spaces in path
  6354. (if (string-match " +\\'" path)
  6355. (setq path (replace-match "" t t path)))
  6356. (if (and org-link-translation-function
  6357. (fboundp org-link-translation-function))
  6358. ;; Check if we need to translate the link
  6359. (let ((tmp (funcall org-link-translation-function type path)))
  6360. (setq type (car tmp) path (cdr tmp))))
  6361. (cond
  6362. ((assoc type org-link-protocols)
  6363. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6364. ((equal type "mailto")
  6365. (let ((cmd (car org-link-mailto-program))
  6366. (args (cdr org-link-mailto-program)) args1
  6367. (address path) (subject "") a)
  6368. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6369. (setq address (match-string 1 path)
  6370. subject (org-link-escape (match-string 2 path))))
  6371. (while args
  6372. (cond
  6373. ((not (stringp (car args))) (push (pop args) args1))
  6374. (t (setq a (pop args))
  6375. (if (string-match "%a" a)
  6376. (setq a (replace-match address t t a)))
  6377. (if (string-match "%s" a)
  6378. (setq a (replace-match subject t t a)))
  6379. (push a args1))))
  6380. (apply cmd (nreverse args1))))
  6381. ((member type '("http" "https" "ftp" "news"))
  6382. (browse-url (concat type ":" (org-link-escape
  6383. path org-link-escape-chars-browser))))
  6384. ((member type '("message"))
  6385. (browse-url (concat type ":" path)))
  6386. ((string= type "tags")
  6387. (org-tags-view in-emacs path))
  6388. ((string= type "thisfile")
  6389. (if in-emacs
  6390. (switch-to-buffer-other-window
  6391. (org-get-buffer-for-internal-link (current-buffer)))
  6392. (org-mark-ring-push))
  6393. (let ((cmd `(org-link-search
  6394. ,path
  6395. ,(cond ((equal in-emacs '(4)) 'occur)
  6396. ((equal in-emacs '(16)) 'org-occur)
  6397. (t nil))
  6398. ,pos)))
  6399. (condition-case nil (eval cmd)
  6400. (error (progn (widen) (eval cmd))))))
  6401. ((string= type "tree-match")
  6402. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6403. ((string= type "file")
  6404. (if (string-match "::\\([0-9]+\\)\\'" path)
  6405. (setq line (string-to-number (match-string 1 path))
  6406. path (substring path 0 (match-beginning 0)))
  6407. (if (string-match "::\\(.+\\)\\'" path)
  6408. (setq search (match-string 1 path)
  6409. path (substring path 0 (match-beginning 0)))))
  6410. (if (string-match "[*?{]" (file-name-nondirectory path))
  6411. (dired path)
  6412. (org-open-file path in-emacs line search)))
  6413. ((string= type "news")
  6414. (require 'org-gnus)
  6415. (org-gnus-follow-link path))
  6416. ((string= type "shell")
  6417. (let ((cmd path))
  6418. (if (or (not org-confirm-shell-link-function)
  6419. (funcall org-confirm-shell-link-function
  6420. (format "Execute \"%s\" in shell? "
  6421. (org-add-props cmd nil
  6422. 'face 'org-warning))))
  6423. (progn
  6424. (message "Executing %s" cmd)
  6425. (shell-command cmd))
  6426. (error "Abort"))))
  6427. ((string= type "elisp")
  6428. (let ((cmd path))
  6429. (if (or (not org-confirm-elisp-link-function)
  6430. (funcall org-confirm-elisp-link-function
  6431. (format "Execute \"%s\" as elisp? "
  6432. (org-add-props cmd nil
  6433. 'face 'org-warning))))
  6434. (message "%s => %s" cmd
  6435. (if (equal (string-to-char cmd) ?\()
  6436. (eval (read cmd))
  6437. (call-interactively (read cmd))))
  6438. (error "Abort"))))
  6439. (t
  6440. (browse-url-at-point))))))
  6441. (move-marker org-open-link-marker nil)
  6442. (run-hook-with-args 'org-follow-link-hook))
  6443. ;;;; Time estimates
  6444. (defun org-get-effort (&optional pom)
  6445. "Get the effort estimate for the current entry."
  6446. (org-entry-get pom org-effort-property))
  6447. ;;; File search
  6448. (defvar org-create-file-search-functions nil
  6449. "List of functions to construct the right search string for a file link.
  6450. These functions are called in turn with point at the location to
  6451. which the link should point.
  6452. A function in the hook should first test if it would like to
  6453. handle this file type, for example by checking the major-mode or
  6454. the file extension. If it decides not to handle this file, it
  6455. should just return nil to give other functions a chance. If it
  6456. does handle the file, it must return the search string to be used
  6457. when following the link. The search string will be part of the
  6458. file link, given after a double colon, and `org-open-at-point'
  6459. will automatically search for it. If special measures must be
  6460. taken to make the search successful, another function should be
  6461. added to the companion hook `org-execute-file-search-functions',
  6462. which see.
  6463. A function in this hook may also use `setq' to set the variable
  6464. `description' to provide a suggestion for the descriptive text to
  6465. be used for this link when it gets inserted into an Org-mode
  6466. buffer with \\[org-insert-link].")
  6467. (defvar org-execute-file-search-functions nil
  6468. "List of functions to execute a file search triggered by a link.
  6469. Functions added to this hook must accept a single argument, the
  6470. search string that was part of the file link, the part after the
  6471. double colon. The function must first check if it would like to
  6472. handle this search, for example by checking the major-mode or the
  6473. file extension. If it decides not to handle this search, it
  6474. should just return nil to give other functions a chance. If it
  6475. does handle the search, it must return a non-nil value to keep
  6476. other functions from trying.
  6477. Each function can access the current prefix argument through the
  6478. variable `current-prefix-argument'. Note that a single prefix is
  6479. used to force opening a link in Emacs, so it may be good to only
  6480. use a numeric or double prefix to guide the search function.
  6481. In case this is needed, a function in this hook can also restore
  6482. the window configuration before `org-open-at-point' was called using:
  6483. (set-window-configuration org-window-config-before-follow-link)")
  6484. (defun org-link-search (s &optional type avoid-pos)
  6485. "Search for a link search option.
  6486. If S is surrounded by forward slashes, it is interpreted as a
  6487. regular expression. In org-mode files, this will create an `org-occur'
  6488. sparse tree. In ordinary files, `occur' will be used to list matches.
  6489. If the current buffer is in `dired-mode', grep will be used to search
  6490. in all files. If AVOID-POS is given, ignore matches near that position."
  6491. (let ((case-fold-search t)
  6492. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6493. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6494. (append '(("") (" ") ("\t") ("\n"))
  6495. org-emphasis-alist)
  6496. "\\|") "\\)"))
  6497. (pos (point))
  6498. (pre nil) (post nil)
  6499. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6500. (cond
  6501. ;; First check if there are any special
  6502. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6503. ;; Now try the builtin stuff
  6504. ((save-excursion
  6505. (goto-char (point-min))
  6506. (and
  6507. (re-search-forward
  6508. (concat "<<" (regexp-quote s0) ">>") nil t)
  6509. (setq type 'dedicated
  6510. pos (match-beginning 0))))
  6511. ;; There is an exact target for this
  6512. (goto-char pos))
  6513. ((and (string-match "^(\\(.*\\))$" s0)
  6514. (save-excursion
  6515. (goto-char (point-min))
  6516. (and
  6517. (re-search-forward
  6518. (concat "[^[]" (regexp-quote
  6519. (format org-coderef-label-format
  6520. (match-string 1 s0))))
  6521. nil t)
  6522. (setq type 'dedicated
  6523. pos (1+ (match-beginning 0))))))
  6524. ;; There is a coderef target for this
  6525. (goto-char pos))
  6526. ((string-match "^/\\(.*\\)/$" s)
  6527. ;; A regular expression
  6528. (cond
  6529. ((org-mode-p)
  6530. (org-occur (match-string 1 s)))
  6531. ;;((eq major-mode 'dired-mode)
  6532. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6533. (t (org-do-occur (match-string 1 s)))))
  6534. (t
  6535. ;; A normal search strings
  6536. (when (equal (string-to-char s) ?*)
  6537. ;; Anchor on headlines, post may include tags.
  6538. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6539. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6540. s (substring s 1)))
  6541. (remove-text-properties
  6542. 0 (length s)
  6543. '(face nil mouse-face nil keymap nil fontified nil) s)
  6544. ;; Make a series of regular expressions to find a match
  6545. (setq words (org-split-string s "[ \n\r\t]+")
  6546. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6547. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6548. "\\)" markers)
  6549. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6550. re2a (concat "[ \t\r\n]" re2a_)
  6551. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6552. re4 (concat "[^a-zA-Z_]" re4_)
  6553. re1 (concat pre re2 post)
  6554. re3 (concat pre (if pre re4_ re4) post)
  6555. re5 (concat pre ".*" re4)
  6556. re2 (concat pre re2)
  6557. re2a (concat pre (if pre re2a_ re2a))
  6558. re4 (concat pre (if pre re4_ re4))
  6559. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6560. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6561. re5 "\\)"
  6562. ))
  6563. (cond
  6564. ((eq type 'org-occur) (org-occur reall))
  6565. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6566. (t (goto-char (point-min))
  6567. (setq type 'fuzzy)
  6568. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6569. (org-search-not-self 1 re1 nil t)
  6570. (org-search-not-self 1 re2 nil t)
  6571. (org-search-not-self 1 re2a nil t)
  6572. (org-search-not-self 1 re3 nil t)
  6573. (org-search-not-self 1 re4 nil t)
  6574. (org-search-not-self 1 re5 nil t)
  6575. )
  6576. (goto-char (match-beginning 1))
  6577. (goto-char pos)
  6578. (error "No match")))))
  6579. (t
  6580. ;; Normal string-search
  6581. (goto-char (point-min))
  6582. (if (search-forward s nil t)
  6583. (goto-char (match-beginning 0))
  6584. (error "No match"))))
  6585. (and (org-mode-p) (org-show-context 'link-search))
  6586. type))
  6587. (defun org-search-not-self (group &rest args)
  6588. "Execute `re-search-forward', but only accept matches that do not
  6589. enclose the position of `org-open-link-marker'."
  6590. (let ((m org-open-link-marker))
  6591. (catch 'exit
  6592. (while (apply 're-search-forward args)
  6593. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6594. (goto-char (match-end group))
  6595. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6596. (> (match-beginning 0) (marker-position m))
  6597. (< (match-end 0) (marker-position m)))
  6598. (save-match-data
  6599. (or (not (org-in-regexp
  6600. org-bracket-link-analytic-regexp 1))
  6601. (not (match-end 4)) ; no description
  6602. (and (<= (match-beginning 4) (point))
  6603. (>= (match-end 4) (point))))))
  6604. (throw 'exit (point))))))))
  6605. (defun org-get-buffer-for-internal-link (buffer)
  6606. "Return a buffer to be used for displaying the link target of internal links."
  6607. (cond
  6608. ((not org-display-internal-link-with-indirect-buffer)
  6609. buffer)
  6610. ((string-match "(Clone)$" (buffer-name buffer))
  6611. (message "Buffer is already a clone, not making another one")
  6612. ;; we also do not modify visibility in this case
  6613. buffer)
  6614. (t ; make a new indirect buffer for displaying the link
  6615. (let* ((bn (buffer-name buffer))
  6616. (ibn (concat bn "(Clone)"))
  6617. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6618. (with-current-buffer ib (org-overview))
  6619. ib))))
  6620. (defun org-do-occur (regexp &optional cleanup)
  6621. "Call the Emacs command `occur'.
  6622. If CLEANUP is non-nil, remove the printout of the regular expression
  6623. in the *Occur* buffer. This is useful if the regex is long and not useful
  6624. to read."
  6625. (occur regexp)
  6626. (when cleanup
  6627. (let ((cwin (selected-window)) win beg end)
  6628. (when (setq win (get-buffer-window "*Occur*"))
  6629. (select-window win))
  6630. (goto-char (point-min))
  6631. (when (re-search-forward "match[a-z]+" nil t)
  6632. (setq beg (match-end 0))
  6633. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6634. (setq end (1- (match-beginning 0)))))
  6635. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6636. (goto-char (point-min))
  6637. (select-window cwin))))
  6638. ;;; The mark ring for links jumps
  6639. (defvar org-mark-ring nil
  6640. "Mark ring for positions before jumps in Org-mode.")
  6641. (defvar org-mark-ring-last-goto nil
  6642. "Last position in the mark ring used to go back.")
  6643. ;; Fill and close the ring
  6644. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6645. (loop for i from 1 to org-mark-ring-length do
  6646. (push (make-marker) org-mark-ring))
  6647. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6648. org-mark-ring)
  6649. (defun org-mark-ring-push (&optional pos buffer)
  6650. "Put the current position or POS into the mark ring and rotate it."
  6651. (interactive)
  6652. (setq pos (or pos (point)))
  6653. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6654. (move-marker (car org-mark-ring)
  6655. (or pos (point))
  6656. (or buffer (current-buffer)))
  6657. (message "%s"
  6658. (substitute-command-keys
  6659. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6660. (defun org-mark-ring-goto (&optional n)
  6661. "Jump to the previous position in the mark ring.
  6662. With prefix arg N, jump back that many stored positions. When
  6663. called several times in succession, walk through the entire ring.
  6664. Org-mode commands jumping to a different position in the current file,
  6665. or to another Org-mode file, automatically push the old position
  6666. onto the ring."
  6667. (interactive "p")
  6668. (let (p m)
  6669. (if (eq last-command this-command)
  6670. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6671. (setq p org-mark-ring))
  6672. (setq org-mark-ring-last-goto p)
  6673. (setq m (car p))
  6674. (switch-to-buffer (marker-buffer m))
  6675. (goto-char m)
  6676. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6677. (defun org-remove-angle-brackets (s)
  6678. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6679. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6680. s)
  6681. (defun org-add-angle-brackets (s)
  6682. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6683. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6684. s)
  6685. (defun org-remove-double-quotes (s)
  6686. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6687. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6688. s)
  6689. ;;; Following specific links
  6690. (defun org-follow-timestamp-link ()
  6691. (cond
  6692. ((org-at-date-range-p t)
  6693. (let ((org-agenda-start-on-weekday)
  6694. (t1 (match-string 1))
  6695. (t2 (match-string 2)))
  6696. (setq t1 (time-to-days (org-time-string-to-time t1))
  6697. t2 (time-to-days (org-time-string-to-time t2)))
  6698. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6699. ((org-at-timestamp-p t)
  6700. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6701. (substring (match-string 1) 0 10)))
  6702. 1))
  6703. (t (error "This should not happen"))))
  6704. ;;; Following file links
  6705. (defvar org-wait nil)
  6706. (defun org-open-file (path &optional in-emacs line search)
  6707. "Open the file at PATH.
  6708. First, this expands any special file name abbreviations. Then the
  6709. configuration variable `org-file-apps' is checked if it contains an
  6710. entry for this file type, and if yes, the corresponding command is launched.
  6711. If no application is found, Emacs simply visits the file.
  6712. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6713. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6714. and o use an external application to visit the file.
  6715. Optional LINE specifies a line to go to, optional SEARCH a string to
  6716. search for. If LINE or SEARCH is given, the file will always be
  6717. opened in Emacs.
  6718. If the file does not exist, an error is thrown."
  6719. (setq in-emacs (or in-emacs line search))
  6720. (let* ((file (if (equal path "")
  6721. buffer-file-name
  6722. (substitute-in-file-name (expand-file-name path))))
  6723. (apps (append org-file-apps (org-default-apps)))
  6724. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6725. (dirp (if remp nil (file-directory-p file)))
  6726. (file (if (and dirp org-open-directory-means-index-dot-org)
  6727. (concat (file-name-as-directory file) "index.org")
  6728. file))
  6729. (a-m-a-p (assq 'auto-mode apps))
  6730. (dfile (downcase file))
  6731. (old-buffer (current-buffer))
  6732. (old-pos (point))
  6733. (old-mode major-mode)
  6734. ext cmd)
  6735. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6736. (setq ext (match-string 1 dfile))
  6737. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6738. (setq ext (match-string 1 dfile))))
  6739. (cond
  6740. ((equal in-emacs '(16))
  6741. (setq cmd (cdr (assoc 'system apps))))
  6742. (in-emacs (setq cmd 'emacs))
  6743. (t
  6744. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6745. (and dirp (cdr (assoc 'directory apps)))
  6746. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6747. 'string-match)
  6748. (cdr (assoc ext apps))
  6749. (cdr (assoc t apps))))))
  6750. (when (eq cmd 'system)
  6751. (setq cmd (cdr (assoc 'system apps))))
  6752. (when (eq cmd 'default)
  6753. (setq cmd (cdr (assoc t apps))))
  6754. (when (eq cmd 'mailcap)
  6755. (require 'mailcap)
  6756. (mailcap-parse-mailcaps)
  6757. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6758. (command (mailcap-mime-info mime-type)))
  6759. (if (stringp command)
  6760. (setq cmd command)
  6761. (setq cmd 'emacs))))
  6762. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6763. (not (file-exists-p file))
  6764. (not org-open-non-existing-files))
  6765. (error "No such file: %s" file))
  6766. (cond
  6767. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6768. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6769. (while (string-match "['\"]%s['\"]" cmd)
  6770. (setq cmd (replace-match "%s" t t cmd)))
  6771. (while (string-match "%s" cmd)
  6772. (setq cmd (replace-match
  6773. (save-match-data
  6774. (shell-quote-argument
  6775. (convert-standard-filename file)))
  6776. t t cmd)))
  6777. (save-window-excursion
  6778. (start-process-shell-command cmd nil cmd)
  6779. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6780. ))
  6781. ((or (stringp cmd)
  6782. (eq cmd 'emacs))
  6783. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6784. (widen)
  6785. (if line (goto-line line)
  6786. (if search (org-link-search search))))
  6787. ((consp cmd)
  6788. (let ((file (convert-standard-filename file)))
  6789. (eval cmd)))
  6790. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6791. (and (org-mode-p) (eq old-mode 'org-mode)
  6792. (or (not (equal old-buffer (current-buffer)))
  6793. (not (equal old-pos (point))))
  6794. (org-mark-ring-push old-pos old-buffer))))
  6795. (defun org-default-apps ()
  6796. "Return the default applications for this operating system."
  6797. (cond
  6798. ((eq system-type 'darwin)
  6799. org-file-apps-defaults-macosx)
  6800. ((eq system-type 'windows-nt)
  6801. org-file-apps-defaults-windowsnt)
  6802. (t org-file-apps-defaults-gnu)))
  6803. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6804. "Convert extensions to regular expressions in the cars of LIST.
  6805. Also, weed out any non-string entries, because the return value is used
  6806. only for regexp matching.
  6807. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6808. point to the symbol `emacs', indicating that the file should
  6809. be opened in Emacs."
  6810. (append
  6811. (delq nil
  6812. (mapcar (lambda (x)
  6813. (if (not (stringp (car x)))
  6814. nil
  6815. (if (string-match "\\W" (car x))
  6816. x
  6817. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6818. list))
  6819. (if add-auto-mode
  6820. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6821. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6822. (defun org-file-remote-p (file)
  6823. "Test whether FILE specifies a location on a remote system.
  6824. Return non-nil if the location is indeed remote.
  6825. For example, the filename \"/user@host:/foo\" specifies a location
  6826. on the system \"/user@host:\"."
  6827. (cond ((fboundp 'file-remote-p)
  6828. (file-remote-p file))
  6829. ((fboundp 'tramp-handle-file-remote-p)
  6830. (tramp-handle-file-remote-p file))
  6831. ((and (boundp 'ange-ftp-name-format)
  6832. (string-match (car ange-ftp-name-format) file))
  6833. t)
  6834. (t nil)))
  6835. ;;;; Refiling
  6836. (defun org-get-org-file ()
  6837. "Read a filename, with default directory `org-directory'."
  6838. (let ((default (or org-default-notes-file remember-data-file)))
  6839. (read-file-name (format "File name [%s]: " default)
  6840. (file-name-as-directory org-directory)
  6841. default)))
  6842. (defun org-notes-order-reversed-p ()
  6843. "Check if the current file should receive notes in reversed order."
  6844. (cond
  6845. ((not org-reverse-note-order) nil)
  6846. ((eq t org-reverse-note-order) t)
  6847. ((not (listp org-reverse-note-order)) nil)
  6848. (t (catch 'exit
  6849. (let ((all org-reverse-note-order)
  6850. entry)
  6851. (while (setq entry (pop all))
  6852. (if (string-match (car entry) buffer-file-name)
  6853. (throw 'exit (cdr entry))))
  6854. nil)))))
  6855. (defvar org-refile-target-table nil
  6856. "The list of refile targets, created by `org-refile'.")
  6857. (defvar org-agenda-new-buffers nil
  6858. "Buffers created to visit agenda files.")
  6859. (defun org-get-refile-targets (&optional default-buffer)
  6860. "Produce a table with refile targets."
  6861. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6862. targets txt re files f desc descre fast-path-p level)
  6863. (message "Getting targets...")
  6864. (with-current-buffer (or default-buffer (current-buffer))
  6865. (while (setq entry (pop entries))
  6866. (setq files (car entry) desc (cdr entry))
  6867. (setq fast-path-p nil)
  6868. (cond
  6869. ((null files) (setq files (list (current-buffer))))
  6870. ((eq files 'org-agenda-files)
  6871. (setq files (org-agenda-files 'unrestricted)))
  6872. ((and (symbolp files) (fboundp files))
  6873. (setq files (funcall files)))
  6874. ((and (symbolp files) (boundp files))
  6875. (setq files (symbol-value files))))
  6876. (if (stringp files) (setq files (list files)))
  6877. (cond
  6878. ((eq (car desc) :tag)
  6879. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6880. ((eq (car desc) :todo)
  6881. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6882. ((eq (car desc) :regexp)
  6883. (setq descre (cdr desc)))
  6884. ((eq (car desc) :level)
  6885. (setq descre (concat "^\\*\\{" (number-to-string
  6886. (if org-odd-levels-only
  6887. (1- (* 2 (cdr desc)))
  6888. (cdr desc)))
  6889. "\\}[ \t]")))
  6890. ((eq (car desc) :maxlevel)
  6891. (setq fast-path-p t)
  6892. (setq descre (concat "^\\*\\{1," (number-to-string
  6893. (if org-odd-levels-only
  6894. (1- (* 2 (cdr desc)))
  6895. (cdr desc)))
  6896. "\\}[ \t]")))
  6897. (t (error "Bad refiling target description %s" desc)))
  6898. (while (setq f (pop files))
  6899. (save-excursion
  6900. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6901. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6902. (setq f (expand-file-name f))
  6903. (save-excursion
  6904. (save-restriction
  6905. (widen)
  6906. (goto-char (point-min))
  6907. (while (re-search-forward descre nil t)
  6908. (goto-char (point-at-bol))
  6909. (when (looking-at org-complex-heading-regexp)
  6910. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  6911. txt (org-link-display-format (match-string 4))
  6912. re (concat "^" (regexp-quote
  6913. (buffer-substring (match-beginning 1)
  6914. (match-end 4)))))
  6915. (if (match-end 5) (setq re (concat re "[ \t]+"
  6916. (regexp-quote
  6917. (match-string 5)))))
  6918. (setq re (concat re "[ \t]*$"))
  6919. (when org-refile-use-outline-path
  6920. (setq txt (mapconcat 'org-protect-slash
  6921. (append
  6922. (if (eq org-refile-use-outline-path 'file)
  6923. (list (file-name-nondirectory
  6924. (buffer-file-name (buffer-base-buffer))))
  6925. (if (eq org-refile-use-outline-path 'full-file-path)
  6926. (list (buffer-file-name (buffer-base-buffer)))))
  6927. (org-get-outline-path fast-path-p level txt)
  6928. (list txt))
  6929. "/")))
  6930. (push (list txt f re (point)) targets))
  6931. (goto-char (point-at-eol))))))))
  6932. (message "Getting targets...done")
  6933. (nreverse targets))))
  6934. (defun org-protect-slash (s)
  6935. (while (string-match "/" s)
  6936. (setq s (replace-match "\\" t t s)))
  6937. s)
  6938. (defvar org-olpa (make-vector 20 nil))
  6939. (defun org-get-outline-path (&optional fastp level heading)
  6940. "Return the outline path to the current entry, as a list."
  6941. (if fastp
  6942. (progn
  6943. (if (> level 19)
  6944. (error "Outline path failure, more than 19 levels."))
  6945. (loop for i from level upto 19 do
  6946. (aset org-olpa i nil))
  6947. (prog1
  6948. (delq nil (append org-olpa nil))
  6949. (aset org-olpa level heading)))
  6950. (let (rtn)
  6951. (save-excursion
  6952. (while (org-up-heading-safe)
  6953. (when (looking-at org-complex-heading-regexp)
  6954. (push (org-match-string-no-properties 4) rtn)))
  6955. rtn))))
  6956. (defvar org-refile-history nil
  6957. "History for refiling operations.")
  6958. (defun org-refile (&optional goto default-buffer)
  6959. "Move the entry at point to another heading.
  6960. The list of target headings is compiled using the information in
  6961. `org-refile-targets', which see. This list is created before each use
  6962. and will therefore always be up-to-date.
  6963. At the target location, the entry is filed as a subitem of the target heading.
  6964. Depending on `org-reverse-note-order', the new subitem will either be the
  6965. first or the last subitem.
  6966. If there is an active region, all entries in that region will be moved.
  6967. However, the region must fulfil the requirement that the first heading
  6968. is the first one sets the top-level of the moved text - at most siblings
  6969. below it are allowed.
  6970. With prefix arg GOTO, the command will only visit the target location,
  6971. not actually move anything.
  6972. With a double prefix `C-u C-u', go to the location where the last refiling
  6973. operation has put the subtree."
  6974. (interactive "P")
  6975. (let* ((cbuf (current-buffer))
  6976. (regionp (org-region-active-p))
  6977. (region-start (and regionp (region-beginning)))
  6978. (region-end (and regionp (region-end)))
  6979. (region-length (and regionp (- region-end region-start)))
  6980. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6981. pos it nbuf file re level reversed)
  6982. (when regionp (goto-char region-start)
  6983. (unless (org-kill-is-subtree-p
  6984. (buffer-substring region-start region-end))
  6985. (error "The region is not a (sequence of) subtree(s)")))
  6986. (if (equal goto '(16))
  6987. (org-refile-goto-last-stored)
  6988. (when (setq it (org-refile-get-location
  6989. (if goto "Goto: " "Refile to: ") default-buffer))
  6990. (setq file (nth 1 it)
  6991. re (nth 2 it)
  6992. pos (nth 3 it))
  6993. (if (and (equal (buffer-file-name) file)
  6994. (if regionp
  6995. (and (>= pos region-start)
  6996. (<= pos region-end))
  6997. (and (>= pos (point))
  6998. (< pos (save-excursion
  6999. (org-end-of-subtree t t))))))
  7000. (error "Cannot refile to position inside the tree or region"))
  7001. (setq nbuf (or (find-buffer-visiting file)
  7002. (find-file-noselect file)))
  7003. (if goto
  7004. (progn
  7005. (switch-to-buffer nbuf)
  7006. (goto-char pos)
  7007. (org-show-context 'org-goto))
  7008. (if regionp
  7009. (progn
  7010. (kill-new (buffer-substring region-start region-end))
  7011. (org-save-markers-in-region region-start region-end))
  7012. (org-copy-subtree 1 nil t))
  7013. (save-excursion
  7014. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7015. (find-file-noselect file))))
  7016. (setq reversed (org-notes-order-reversed-p))
  7017. (save-excursion
  7018. (save-restriction
  7019. (widen)
  7020. (goto-char pos)
  7021. (looking-at outline-regexp)
  7022. (setq level (org-get-valid-level (funcall outline-level) 1))
  7023. (goto-char
  7024. (if reversed
  7025. (or (outline-next-heading) (point-max))
  7026. (or (save-excursion (outline-get-next-sibling))
  7027. (org-end-of-subtree t t)
  7028. (point-max))))
  7029. (if (not (bolp)) (newline))
  7030. (bookmark-set "org-refile-last-stored")
  7031. (org-paste-subtree level))))
  7032. (if regionp
  7033. (delete-region (point) (+ (point) region-length))
  7034. (org-cut-subtree))
  7035. (setq org-markers-to-move nil)
  7036. (message "Refiled to \"%s\"" (car it)))))))
  7037. (defun org-refile-goto-last-stored ()
  7038. "Go to the location where the last refile was stored."
  7039. (interactive)
  7040. (bookmark-jump "org-refile-last-stored")
  7041. (message "This is the location of the last refile"))
  7042. (defun org-refile-get-location (&optional prompt default-buffer)
  7043. "Prompt the user for a refile location, using PROMPT."
  7044. (let ((org-refile-targets org-refile-targets)
  7045. (org-refile-use-outline-path org-refile-use-outline-path))
  7046. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7047. (unless org-refile-target-table
  7048. (error "No refile targets"))
  7049. (let* ((cbuf (current-buffer))
  7050. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7051. (cfunc (if (and org-refile-use-outline-path
  7052. org-outline-path-complete-in-steps)
  7053. 'org-olpath-completing-read
  7054. 'org-ido-completing-read))
  7055. (extra (if org-refile-use-outline-path "/" ""))
  7056. (filename (and cfn (expand-file-name cfn)))
  7057. (tbl (mapcar
  7058. (lambda (x)
  7059. (if (not (equal filename (nth 1 x)))
  7060. (cons (concat (car x) extra " ("
  7061. (file-name-nondirectory (nth 1 x)) ")")
  7062. (cdr x))
  7063. (cons (concat (car x) extra) (cdr x))))
  7064. org-refile-target-table))
  7065. (completion-ignore-case t))
  7066. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7067. tbl)))
  7068. (defun org-olpath-completing-read (prompt collection &rest args)
  7069. "Read an outline path like a file name."
  7070. (let ((thetable collection))
  7071. (apply
  7072. 'org-ido-completing-read prompt
  7073. (lambda (string predicate &optional flag)
  7074. (let (rtn r s f (l (length string)))
  7075. (cond
  7076. ((eq flag nil)
  7077. ;; try completion
  7078. (try-completion string thetable))
  7079. ((eq flag t)
  7080. ;; all-completions
  7081. (setq rtn (all-completions string thetable predicate))
  7082. (mapcar
  7083. (lambda (x)
  7084. (setq r (substring x l))
  7085. (if (string-match " ([^)]*)$" x)
  7086. (setq f (match-string 0 x))
  7087. (setq f ""))
  7088. (if (string-match "/" r)
  7089. (concat string (substring r 0 (match-end 0)) f)
  7090. x))
  7091. rtn))
  7092. ((eq flag 'lambda)
  7093. ;; exact match?
  7094. (assoc string thetable)))
  7095. ))
  7096. args)))
  7097. ;;;; Dynamic blocks
  7098. (defun org-find-dblock (name)
  7099. "Find the first dynamic block with name NAME in the buffer.
  7100. If not found, stay at current position and return nil."
  7101. (let (pos)
  7102. (save-excursion
  7103. (goto-char (point-min))
  7104. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7105. nil t)
  7106. (match-beginning 0))))
  7107. (if pos (goto-char pos))
  7108. pos))
  7109. (defconst org-dblock-start-re
  7110. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7111. "Matches the startline of a dynamic block, with parameters.")
  7112. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7113. "Matches the end of a dynamic block.")
  7114. (defun org-create-dblock (plist)
  7115. "Create a dynamic block section, with parameters taken from PLIST.
  7116. PLIST must contain a :name entry which is used as name of the block."
  7117. (unless (bolp) (newline))
  7118. (let ((name (plist-get plist :name)))
  7119. (insert "#+BEGIN: " name)
  7120. (while plist
  7121. (if (eq (car plist) :name)
  7122. (setq plist (cddr plist))
  7123. (insert " " (prin1-to-string (pop plist)))))
  7124. (insert "\n\n#+END:\n")
  7125. (beginning-of-line -2)))
  7126. (defun org-prepare-dblock ()
  7127. "Prepare dynamic block for refresh.
  7128. This empties the block, puts the cursor at the insert position and returns
  7129. the property list including an extra property :name with the block name."
  7130. (unless (looking-at org-dblock-start-re)
  7131. (error "Not at a dynamic block"))
  7132. (let* ((begdel (1+ (match-end 0)))
  7133. (name (org-no-properties (match-string 1)))
  7134. (params (append (list :name name)
  7135. (read (concat "(" (match-string 3) ")")))))
  7136. (unless (re-search-forward org-dblock-end-re nil t)
  7137. (error "Dynamic block not terminated"))
  7138. (setq params
  7139. (append params
  7140. (list :content (buffer-substring
  7141. begdel (match-beginning 0)))))
  7142. (delete-region begdel (match-beginning 0))
  7143. (goto-char begdel)
  7144. (open-line 1)
  7145. params))
  7146. (defun org-map-dblocks (&optional command)
  7147. "Apply COMMAND to all dynamic blocks in the current buffer.
  7148. If COMMAND is not given, use `org-update-dblock'."
  7149. (let ((cmd (or command 'org-update-dblock))
  7150. pos)
  7151. (save-excursion
  7152. (goto-char (point-min))
  7153. (while (re-search-forward org-dblock-start-re nil t)
  7154. (goto-char (setq pos (match-beginning 0)))
  7155. (condition-case nil
  7156. (funcall cmd)
  7157. (error (message "Error during update of dynamic block")))
  7158. (goto-char pos)
  7159. (unless (re-search-forward org-dblock-end-re nil t)
  7160. (error "Dynamic block not terminated"))))))
  7161. (defun org-dblock-update (&optional arg)
  7162. "User command for updating dynamic blocks.
  7163. Update the dynamic block at point. With prefix ARG, update all dynamic
  7164. blocks in the buffer."
  7165. (interactive "P")
  7166. (if arg
  7167. (org-update-all-dblocks)
  7168. (or (looking-at org-dblock-start-re)
  7169. (org-beginning-of-dblock))
  7170. (org-update-dblock)))
  7171. (defun org-update-dblock ()
  7172. "Update the dynamic block at point
  7173. This means to empty the block, parse for parameters and then call
  7174. the correct writing function."
  7175. (save-window-excursion
  7176. (let* ((pos (point))
  7177. (line (org-current-line))
  7178. (params (org-prepare-dblock))
  7179. (name (plist-get params :name))
  7180. (cmd (intern (concat "org-dblock-write:" name))))
  7181. (message "Updating dynamic block `%s' at line %d..." name line)
  7182. (funcall cmd params)
  7183. (message "Updating dynamic block `%s' at line %d...done" name line)
  7184. (goto-char pos))))
  7185. (defun org-beginning-of-dblock ()
  7186. "Find the beginning of the dynamic block at point.
  7187. Error if there is no such block at point."
  7188. (let ((pos (point))
  7189. beg)
  7190. (end-of-line 1)
  7191. (if (and (re-search-backward org-dblock-start-re nil t)
  7192. (setq beg (match-beginning 0))
  7193. (re-search-forward org-dblock-end-re nil t)
  7194. (> (match-end 0) pos))
  7195. (goto-char beg)
  7196. (goto-char pos)
  7197. (error "Not in a dynamic block"))))
  7198. (defun org-update-all-dblocks ()
  7199. "Update all dynamic blocks in the buffer.
  7200. This function can be used in a hook."
  7201. (when (org-mode-p)
  7202. (org-map-dblocks 'org-update-dblock)))
  7203. ;;;; Completion
  7204. (defconst org-additional-option-like-keywords
  7205. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7206. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7207. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7208. "BEGIN_QUOTE" "END_QUOTE"
  7209. "BEGIN_VERSE" "END_VERSE"
  7210. "BEGIN_SRC" "END_SRC"
  7211. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7212. (defcustom org-structure-template-alist
  7213. '(
  7214. ("s" "#+begin_src ?\n\n#+end_src"
  7215. "<src lang=\"?\">\n\n</src>")
  7216. ("e" "#+begin_example\n?\n#+end_example"
  7217. "<example>\n?\n</example>")
  7218. ("q" "#+begin_quote\n?\n#+end_quote"
  7219. "<quote>\n?\n</quote>")
  7220. ("v" "#+begin_verse\n?\n#+end_verse"
  7221. "<verse>\n?\n/verse>")
  7222. ("l" "#+begin_latex\n?\n#+end_latex"
  7223. "<literal style=\"latex\">\n?\n</literal>")
  7224. ("L" "#+latex: "
  7225. "<literal style=\"latex\">?</literal>")
  7226. ("h" "#+begin_html\n?\n#+end_html"
  7227. "<literal style=\"html\">\n?\n</literal>")
  7228. ("H" "#+html: "
  7229. "<literal style=\"html\">?</literal>")
  7230. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7231. ("A" "#+ascii: ")
  7232. ("i" "#+include %file ?"
  7233. "<include file=%file markup=\"?\">")
  7234. )
  7235. "Structure completion elements.
  7236. This is a list of abbreviation keys and values. The value gets inserted
  7237. it you type @samp{.} followed by the key and then the completion key,
  7238. usually `M-TAB'. %file will be replaced by a file name after prompting
  7239. for the file using completion.
  7240. There are two templates for each key, the first uses the original Org syntax,
  7241. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7242. the default when the /org-mtags.el/ module has been loaded. See also the
  7243. variable `org-mtags-prefer-muse-templates'.
  7244. This is an experimental feature, it is undecided if it is going to stay in."
  7245. :group 'org-completion
  7246. :type '(repeat
  7247. (string :tag "Key")
  7248. (string :tag "Template")
  7249. (string :tag "Muse Template")))
  7250. (defun org-try-structure-completion ()
  7251. "Try to complete a structure template before point.
  7252. This looks for strings like \"<e\" on an otherwise empty line and
  7253. expands them."
  7254. (let ((l (buffer-substring (point-at-bol) (point)))
  7255. a)
  7256. (when (and (looking-at "[ \t]*$")
  7257. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7258. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7259. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7260. (match-beginning 1)) a)
  7261. t)))
  7262. (defun org-complete-expand-structure-template (start cell)
  7263. "Expand a structure template."
  7264. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7265. (rpl (nth (if musep 2 1) cell)))
  7266. (delete-region start (point))
  7267. (when (string-match "\\`#\\+" rpl)
  7268. (cond
  7269. ((bolp))
  7270. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7271. (delete-region (point-at-bol) (point)))
  7272. (t (newline))))
  7273. (setq start (point))
  7274. (if (string-match "%file" rpl)
  7275. (setq rpl (replace-match
  7276. (concat
  7277. "\""
  7278. (save-match-data
  7279. (abbreviate-file-name (read-file-name "Include file: ")))
  7280. "\"")
  7281. t t rpl)))
  7282. (insert rpl)
  7283. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7284. (defun org-complete (&optional arg)
  7285. "Perform completion on word at point.
  7286. At the beginning of a headline, this completes TODO keywords as given in
  7287. `org-todo-keywords'.
  7288. If the current word is preceded by a backslash, completes the TeX symbols
  7289. that are supported for HTML support.
  7290. If the current word is preceded by \"#+\", completes special words for
  7291. setting file options.
  7292. In the line after \"#+STARTUP:, complete valid keywords.\"
  7293. At all other locations, this simply calls the value of
  7294. `org-completion-fallback-command'."
  7295. (interactive "P")
  7296. (org-without-partial-completion
  7297. (catch 'exit
  7298. (let* ((a nil)
  7299. (end (point))
  7300. (beg1 (save-excursion
  7301. (skip-chars-backward (org-re "[:alnum:]_@"))
  7302. (point)))
  7303. (beg (save-excursion
  7304. (skip-chars-backward "a-zA-Z0-9_:$")
  7305. (point)))
  7306. (confirm (lambda (x) (stringp (car x))))
  7307. (searchhead (equal (char-before beg) ?*))
  7308. (struct
  7309. (when (and (member (char-before beg1) '(?. ?<))
  7310. (setq a (assoc (buffer-substring beg1 (point))
  7311. org-structure-template-alist)))
  7312. (org-complete-expand-structure-template (1- beg1) a)
  7313. (throw 'exit t)))
  7314. (tag (and (equal (char-before beg1) ?:)
  7315. (equal (char-after (point-at-bol)) ?*)))
  7316. (prop (and (equal (char-before beg1) ?:)
  7317. (not (equal (char-after (point-at-bol)) ?*))))
  7318. (texp (equal (char-before beg) ?\\))
  7319. (link (equal (char-before beg) ?\[))
  7320. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7321. beg)
  7322. "#+"))
  7323. (startup (string-match "^#\\+STARTUP:.*"
  7324. (buffer-substring (point-at-bol) (point))))
  7325. (completion-ignore-case opt)
  7326. (type nil)
  7327. (tbl nil)
  7328. (table (cond
  7329. (opt
  7330. (setq type :opt)
  7331. (require 'org-exp)
  7332. (append
  7333. (mapcar
  7334. (lambda (x)
  7335. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7336. (cons (match-string 2 x) (match-string 1 x)))
  7337. (org-split-string (org-get-current-options) "\n"))
  7338. (mapcar 'list org-additional-option-like-keywords)))
  7339. (startup
  7340. (setq type :startup)
  7341. org-startup-options)
  7342. (link (append org-link-abbrev-alist-local
  7343. org-link-abbrev-alist))
  7344. (texp
  7345. (setq type :tex)
  7346. org-html-entities)
  7347. ((string-match "\\`\\*+[ \t]+\\'"
  7348. (buffer-substring (point-at-bol) beg))
  7349. (setq type :todo)
  7350. (mapcar 'list org-todo-keywords-1))
  7351. (searchhead
  7352. (setq type :searchhead)
  7353. (save-excursion
  7354. (goto-char (point-min))
  7355. (while (re-search-forward org-todo-line-regexp nil t)
  7356. (push (list
  7357. (org-make-org-heading-search-string
  7358. (match-string 3) t))
  7359. tbl)))
  7360. tbl)
  7361. (tag (setq type :tag beg beg1)
  7362. (or org-tag-alist (org-get-buffer-tags)))
  7363. (prop (setq type :prop beg beg1)
  7364. (mapcar 'list (org-buffer-property-keys nil t t)))
  7365. (t (progn
  7366. (call-interactively org-completion-fallback-command)
  7367. (throw 'exit nil)))))
  7368. (pattern (buffer-substring-no-properties beg end))
  7369. (completion (try-completion pattern table confirm)))
  7370. (cond ((eq completion t)
  7371. (if (not (assoc (upcase pattern) table))
  7372. (message "Already complete")
  7373. (if (and (equal type :opt)
  7374. (not (member (car (assoc (upcase pattern) table))
  7375. org-additional-option-like-keywords)))
  7376. (insert (substring (cdr (assoc (upcase pattern) table))
  7377. (length pattern)))
  7378. (if (memq type '(:tag :prop)) (insert ":")))))
  7379. ((null completion)
  7380. (message "Can't find completion for \"%s\"" pattern)
  7381. (ding))
  7382. ((not (string= pattern completion))
  7383. (delete-region beg end)
  7384. (if (string-match " +$" completion)
  7385. (setq completion (replace-match "" t t completion)))
  7386. (insert completion)
  7387. (if (get-buffer-window "*Completions*")
  7388. (delete-window (get-buffer-window "*Completions*")))
  7389. (if (assoc completion table)
  7390. (if (eq type :todo) (insert " ")
  7391. (if (memq type '(:tag :prop)) (insert ":"))))
  7392. (if (and (equal type :opt) (assoc completion table))
  7393. (message "%s" (substitute-command-keys
  7394. "Press \\[org-complete] again to insert example settings"))))
  7395. (t
  7396. (message "Making completion list...")
  7397. (let ((list (sort (all-completions pattern table confirm)
  7398. 'string<)))
  7399. (with-output-to-temp-buffer "*Completions*"
  7400. (condition-case nil
  7401. ;; Protection needed for XEmacs and emacs 21
  7402. (display-completion-list list pattern)
  7403. (error (display-completion-list list)))))
  7404. (message "Making completion list...%s" "done")))))))
  7405. ;;;; TODO, DEADLINE, Comments
  7406. (defun org-toggle-comment ()
  7407. "Change the COMMENT state of an entry."
  7408. (interactive)
  7409. (save-excursion
  7410. (org-back-to-heading)
  7411. (let (case-fold-search)
  7412. (if (looking-at (concat outline-regexp
  7413. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7414. (replace-match "" t t nil 1)
  7415. (if (looking-at outline-regexp)
  7416. (progn
  7417. (goto-char (match-end 0))
  7418. (insert org-comment-string " ")))))))
  7419. (defvar org-last-todo-state-is-todo nil
  7420. "This is non-nil when the last TODO state change led to a TODO state.
  7421. If the last change removed the TODO tag or switched to DONE, then
  7422. this is nil.")
  7423. (defvar org-setting-tags nil) ; dynamically skipped
  7424. (defun org-parse-local-options (string var)
  7425. "Parse STRING for startup setting relevant for variable VAR."
  7426. (let ((rtn (symbol-value var))
  7427. e opts)
  7428. (save-match-data
  7429. (if (or (not string) (not (string-match "\\S-" string)))
  7430. rtn
  7431. (setq opts (delq nil (mapcar (lambda (x)
  7432. (setq e (assoc x org-startup-options))
  7433. (if (eq (nth 1 e) var) e nil))
  7434. (org-split-string string "[ \t]+"))))
  7435. (if (not opts)
  7436. rtn
  7437. (setq rtn nil)
  7438. (while (setq e (pop opts))
  7439. (if (not (nth 3 e))
  7440. (setq rtn (nth 2 e))
  7441. (if (not (listp rtn)) (setq rtn nil))
  7442. (push (nth 2 e) rtn)))
  7443. rtn)))))
  7444. (defvar org-blocker-hook nil
  7445. "Hook for functions that are allowed to block a state change.
  7446. Each function gets as its single argument a property list, see
  7447. `org-trigger-hook' for more information about this list.
  7448. If any of the functions in this hook returns nil, the state change
  7449. is blocked.")
  7450. (defvar org-trigger-hook nil
  7451. "Hook for functions that are triggered by a state change.
  7452. Each function gets as its single argument a property list with at least
  7453. the following elements:
  7454. (:type type-of-change :position pos-at-entry-start
  7455. :from old-state :to new-state)
  7456. Depending on the type, more properties may be present.
  7457. This mechanism is currently implemented for:
  7458. TODO state changes
  7459. ------------------
  7460. :type todo-state-change
  7461. :from previous state (keyword as a string), or nil
  7462. :to new state (keyword as a string), or nil")
  7463. (defvar org-agenda-headline-snapshot-before-repeat)
  7464. (defun org-todo (&optional arg)
  7465. "Change the TODO state of an item.
  7466. The state of an item is given by a keyword at the start of the heading,
  7467. like
  7468. *** TODO Write paper
  7469. *** DONE Call mom
  7470. The different keywords are specified in the variable `org-todo-keywords'.
  7471. By default the available states are \"TODO\" and \"DONE\".
  7472. So for this example: when the item starts with TODO, it is changed to DONE.
  7473. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7474. DONE are present, add TODO at the beginning of the heading.
  7475. With C-u prefix arg, use completion to determine the new state.
  7476. With numeric prefix arg, switch to that state.
  7477. For calling through lisp, arg is also interpreted in the following way:
  7478. 'none -> empty state
  7479. \"\"(empty string) -> switch to empty state
  7480. 'done -> switch to DONE
  7481. 'nextset -> switch to the next set of keywords
  7482. 'previousset -> switch to the previous set of keywords
  7483. \"WAITING\" -> switch to the specified keyword, but only if it
  7484. really is a member of `org-todo-keywords'."
  7485. (interactive "P")
  7486. (save-excursion
  7487. (catch 'exit
  7488. (org-back-to-heading)
  7489. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7490. (or (looking-at (concat " +" org-todo-regexp " *"))
  7491. (looking-at " *"))
  7492. (let* ((match-data (match-data))
  7493. (startpos (point-at-bol))
  7494. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7495. (org-log-done org-log-done)
  7496. (org-log-repeat org-log-repeat)
  7497. (org-todo-log-states org-todo-log-states)
  7498. (this (match-string 1))
  7499. (hl-pos (match-beginning 0))
  7500. (head (org-get-todo-sequence-head this))
  7501. (ass (assoc head org-todo-kwd-alist))
  7502. (interpret (nth 1 ass))
  7503. (done-word (nth 3 ass))
  7504. (final-done-word (nth 4 ass))
  7505. (last-state (or this ""))
  7506. (completion-ignore-case t)
  7507. (member (member this org-todo-keywords-1))
  7508. (tail (cdr member))
  7509. (state (cond
  7510. ((and org-todo-key-trigger
  7511. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7512. (and (not arg) org-use-fast-todo-selection
  7513. (not (eq org-use-fast-todo-selection 'prefix)))))
  7514. ;; Use fast selection
  7515. (org-fast-todo-selection))
  7516. ((and (equal arg '(4))
  7517. (or (not org-use-fast-todo-selection)
  7518. (not org-todo-key-trigger)))
  7519. ;; Read a state with completion
  7520. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7521. org-todo-keywords-1)
  7522. nil t))
  7523. ((eq arg 'right)
  7524. (if this
  7525. (if tail (car tail) nil)
  7526. (car org-todo-keywords-1)))
  7527. ((eq arg 'left)
  7528. (if (equal member org-todo-keywords-1)
  7529. nil
  7530. (if this
  7531. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7532. org-todo-keywords-1)
  7533. (org-last org-todo-keywords-1))))
  7534. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7535. (setq arg nil))) ; hack to fall back to cycling
  7536. (arg
  7537. ;; user or caller requests a specific state
  7538. (cond
  7539. ((equal arg "") nil)
  7540. ((eq arg 'none) nil)
  7541. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7542. ((eq arg 'nextset)
  7543. (or (car (cdr (member head org-todo-heads)))
  7544. (car org-todo-heads)))
  7545. ((eq arg 'previousset)
  7546. (let ((org-todo-heads (reverse org-todo-heads)))
  7547. (or (car (cdr (member head org-todo-heads)))
  7548. (car org-todo-heads))))
  7549. ((car (member arg org-todo-keywords-1)))
  7550. ((nth (1- (prefix-numeric-value arg))
  7551. org-todo-keywords-1))))
  7552. ((null member) (or head (car org-todo-keywords-1)))
  7553. ((equal this final-done-word) nil) ;; -> make empty
  7554. ((null tail) nil) ;; -> first entry
  7555. ((eq interpret 'sequence)
  7556. (car tail))
  7557. ((memq interpret '(type priority))
  7558. (if (eq this-command last-command)
  7559. (car tail)
  7560. (if (> (length tail) 0)
  7561. (or done-word (car org-done-keywords))
  7562. nil)))
  7563. (t nil)))
  7564. (next (if state (concat " " state " ") " "))
  7565. (change-plist (list :type 'todo-state-change :from this :to state
  7566. :position startpos))
  7567. dolog now-done-p)
  7568. (when org-blocker-hook
  7569. (unless (save-excursion
  7570. (save-match-data
  7571. (run-hook-with-args-until-failure
  7572. 'org-blocker-hook change-plist)))
  7573. (if (interactive-p)
  7574. (error "TODO state change from %s to %s blocked" this state)
  7575. ;; fail silently
  7576. (message "TODO state change from %s to %s blocked" this state)
  7577. (throw 'exit nil))))
  7578. (store-match-data match-data)
  7579. (replace-match next t t)
  7580. (unless (pos-visible-in-window-p hl-pos)
  7581. (message "TODO state changed to %s" (org-trim next)))
  7582. (unless head
  7583. (setq head (org-get-todo-sequence-head state)
  7584. ass (assoc head org-todo-kwd-alist)
  7585. interpret (nth 1 ass)
  7586. done-word (nth 3 ass)
  7587. final-done-word (nth 4 ass)))
  7588. (when (memq arg '(nextset previousset))
  7589. (message "Keyword-Set %d/%d: %s"
  7590. (- (length org-todo-sets) -1
  7591. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7592. (length org-todo-sets)
  7593. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7594. (setq org-last-todo-state-is-todo
  7595. (not (member state org-done-keywords)))
  7596. (setq now-done-p (and (member state org-done-keywords)
  7597. (not (member this org-done-keywords))))
  7598. (and logging (org-local-logging logging))
  7599. (when (and (or org-todo-log-states org-log-done)
  7600. (not (memq arg '(nextset previousset))))
  7601. ;; we need to look at recording a time and note
  7602. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7603. (nth 2 (assoc this org-todo-log-states))))
  7604. (when (and state
  7605. (member state org-not-done-keywords)
  7606. (not (member this org-not-done-keywords)))
  7607. ;; This is now a todo state and was not one before
  7608. ;; If there was a CLOSED time stamp, get rid of it.
  7609. (org-add-planning-info nil nil 'closed))
  7610. (when (and now-done-p org-log-done)
  7611. ;; It is now done, and it was not done before
  7612. (org-add-planning-info 'closed (org-current-time))
  7613. (if (and (not dolog) (eq 'note org-log-done))
  7614. (org-add-log-setup 'done state 'findpos 'note)))
  7615. (when (and state dolog)
  7616. ;; This is a non-nil state, and we need to log it
  7617. (org-add-log-setup 'state state 'findpos dolog)))
  7618. ;; Fixup tag positioning
  7619. (org-todo-trigger-tag-changes state)
  7620. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7621. (when org-provide-todo-statistics
  7622. (org-update-parent-todo-statistics))
  7623. (run-hooks 'org-after-todo-state-change-hook)
  7624. (if (and arg (not (member state org-done-keywords)))
  7625. (setq head (org-get-todo-sequence-head state)))
  7626. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7627. ;; Do we need to trigger a repeat?
  7628. (when now-done-p
  7629. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7630. ;; This is for the agenda, take a snapshot of the headline.
  7631. (save-match-data
  7632. (setq org-agenda-headline-snapshot-before-repeat
  7633. (org-get-heading))))
  7634. (org-auto-repeat-maybe state))
  7635. ;; Fixup cursor location if close to the keyword
  7636. (if (and (outline-on-heading-p)
  7637. (not (bolp))
  7638. (save-excursion (beginning-of-line 1)
  7639. (looking-at org-todo-line-regexp))
  7640. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7641. (progn
  7642. (goto-char (or (match-end 2) (match-end 1)))
  7643. (just-one-space)))
  7644. (when org-trigger-hook
  7645. (save-excursion
  7646. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7647. (defun org-update-parent-todo-statistics ()
  7648. "Update any statistics cookie in the parent of the current headline."
  7649. (interactive)
  7650. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7651. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7652. (catch 'exit
  7653. (save-excursion
  7654. (setq level (org-up-heading-safe))
  7655. (unless (and level
  7656. (re-search-forward box-re (point-at-eol) t))
  7657. (throw 'exit nil))
  7658. (setq is-percent (match-end 2))
  7659. (save-match-data
  7660. (unless (outline-next-heading) (throw 'exit nil))
  7661. (while (looking-at org-todo-line-regexp)
  7662. (setq kwd (match-string 2))
  7663. (and kwd (setq cnt-all (1+ cnt-all)))
  7664. (and (member kwd org-done-keywords)
  7665. (setq cnt-done (1+ cnt-done)))
  7666. (condition-case nil
  7667. (org-forward-same-level 1)
  7668. (error (end-of-line 1)))))
  7669. (replace-match
  7670. (if is-percent
  7671. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7672. (format "[%d/%d]" cnt-done cnt-all)))
  7673. (run-hook-with-args 'org-after-todo-statistics-hook
  7674. cnt-done (- cnt-all cnt-done))))))
  7675. (defvar org-after-todo-statistics-hook nil
  7676. "Hook that is called after a TODO statistics cookie has been updated.
  7677. Each function is called with two arguments: the number of not-done entries
  7678. and the number of done entries.
  7679. For example, the following function, when added to this hook, will switch
  7680. an entry to DONE when all children are done, and back to TODO when new
  7681. entries are set to a TODO status. Note that this hook is only called
  7682. when there is a statistics cookie in the headline!
  7683. (defun org-summary-todo (n-done n-not-done)
  7684. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7685. (let (org-log-done org-log-states) ; turn off logging
  7686. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7687. ")
  7688. (defun org-todo-trigger-tag-changes (state)
  7689. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7690. (let ((l org-todo-state-tags-triggers)
  7691. changes)
  7692. (when (or (not state) (equal state ""))
  7693. (setq changes (append changes (cdr (assoc "" l)))))
  7694. (when (and (stringp state) (> (length state) 0))
  7695. (setq changes (append changes (cdr (assoc state l)))))
  7696. (when (member state org-not-done-keywords)
  7697. (setq changes (append changes (cdr (assoc 'todo l)))))
  7698. (when (member state org-done-keywords)
  7699. (setq changes (append changes (cdr (assoc 'done l)))))
  7700. (dolist (c changes)
  7701. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7702. (defun org-local-logging (value)
  7703. "Get logging settings from a property VALUE."
  7704. (let* (words w a)
  7705. ;; directly set the variables, they are already local.
  7706. (setq org-log-done nil
  7707. org-log-repeat nil
  7708. org-todo-log-states nil)
  7709. (setq words (org-split-string value))
  7710. (while (setq w (pop words))
  7711. (cond
  7712. ((setq a (assoc w org-startup-options))
  7713. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7714. (set (nth 1 a) (nth 2 a))))
  7715. ((setq a (org-extract-log-state-settings w))
  7716. (and (member (car a) org-todo-keywords-1)
  7717. (push a org-todo-log-states)))))))
  7718. (defun org-get-todo-sequence-head (kwd)
  7719. "Return the head of the TODO sequence to which KWD belongs.
  7720. If KWD is not set, check if there is a text property remembering the
  7721. right sequence."
  7722. (let (p)
  7723. (cond
  7724. ((not kwd)
  7725. (or (get-text-property (point-at-bol) 'org-todo-head)
  7726. (progn
  7727. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7728. nil (point-at-eol)))
  7729. (get-text-property p 'org-todo-head))))
  7730. ((not (member kwd org-todo-keywords-1))
  7731. (car org-todo-keywords-1))
  7732. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7733. (defun org-fast-todo-selection ()
  7734. "Fast TODO keyword selection with single keys.
  7735. Returns the new TODO keyword, or nil if no state change should occur."
  7736. (let* ((fulltable org-todo-key-alist)
  7737. (done-keywords org-done-keywords) ;; needed for the faces.
  7738. (maxlen (apply 'max (mapcar
  7739. (lambda (x)
  7740. (if (stringp (car x)) (string-width (car x)) 0))
  7741. fulltable)))
  7742. (expert nil)
  7743. (fwidth (+ maxlen 3 1 3))
  7744. (ncol (/ (- (window-width) 4) fwidth))
  7745. tg cnt e c tbl
  7746. groups ingroup)
  7747. (save-window-excursion
  7748. (if expert
  7749. (set-buffer (get-buffer-create " *Org todo*"))
  7750. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7751. (erase-buffer)
  7752. (org-set-local 'org-done-keywords done-keywords)
  7753. (setq tbl fulltable cnt 0)
  7754. (while (setq e (pop tbl))
  7755. (cond
  7756. ((equal e '(:startgroup))
  7757. (push '() groups) (setq ingroup t)
  7758. (when (not (= cnt 0))
  7759. (setq cnt 0)
  7760. (insert "\n"))
  7761. (insert "{ "))
  7762. ((equal e '(:endgroup))
  7763. (setq ingroup nil cnt 0)
  7764. (insert "}\n"))
  7765. (t
  7766. (setq tg (car e) c (cdr e))
  7767. (if ingroup (push tg (car groups)))
  7768. (setq tg (org-add-props tg nil 'face
  7769. (org-get-todo-face tg)))
  7770. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7771. (insert "[" c "] " tg (make-string
  7772. (- fwidth 4 (length tg)) ?\ ))
  7773. (when (= (setq cnt (1+ cnt)) ncol)
  7774. (insert "\n")
  7775. (if ingroup (insert " "))
  7776. (setq cnt 0)))))
  7777. (insert "\n")
  7778. (goto-char (point-min))
  7779. (if (not expert) (org-fit-window-to-buffer))
  7780. (message "[a-z..]:Set [SPC]:clear")
  7781. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7782. (cond
  7783. ((or (= c ?\C-g)
  7784. (and (= c ?q) (not (rassoc c fulltable))))
  7785. (setq quit-flag t))
  7786. ((= c ?\ ) nil)
  7787. ((setq e (rassoc c fulltable) tg (car e))
  7788. tg)
  7789. (t (setq quit-flag t))))))
  7790. (defun org-entry-is-todo-p ()
  7791. (member (org-get-todo-state) org-not-done-keywords))
  7792. (defun org-entry-is-done-p ()
  7793. (member (org-get-todo-state) org-done-keywords))
  7794. (defun org-get-todo-state ()
  7795. (save-excursion
  7796. (org-back-to-heading t)
  7797. (and (looking-at org-todo-line-regexp)
  7798. (match-end 2)
  7799. (match-string 2))))
  7800. (defun org-at-date-range-p (&optional inactive-ok)
  7801. "Is the cursor inside a date range?"
  7802. (interactive)
  7803. (save-excursion
  7804. (catch 'exit
  7805. (let ((pos (point)))
  7806. (skip-chars-backward "^[<\r\n")
  7807. (skip-chars-backward "<[")
  7808. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7809. (>= (match-end 0) pos)
  7810. (throw 'exit t))
  7811. (skip-chars-backward "^<[\r\n")
  7812. (skip-chars-backward "<[")
  7813. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7814. (>= (match-end 0) pos)
  7815. (throw 'exit t)))
  7816. nil)))
  7817. (defun org-get-repeat ()
  7818. "Check if there is a deadline/schedule with repeater in this entry."
  7819. (save-match-data
  7820. (save-excursion
  7821. (org-back-to-heading t)
  7822. (if (re-search-forward
  7823. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7824. (match-string 1)))))
  7825. (defvar org-last-changed-timestamp)
  7826. (defvar org-last-inserted-timestamp)
  7827. (defvar org-log-post-message)
  7828. (defvar org-log-note-purpose)
  7829. (defvar org-log-note-how)
  7830. (defvar org-log-note-extra)
  7831. (defun org-auto-repeat-maybe (done-word)
  7832. "Check if the current headline contains a repeated deadline/schedule.
  7833. If yes, set TODO state back to what it was and change the base date
  7834. of repeating deadline/scheduled time stamps to new date.
  7835. This function is run automatically after each state change to a DONE state."
  7836. ;; last-state is dynamically scoped into this function
  7837. (let* ((repeat (org-get-repeat))
  7838. (aa (assoc last-state org-todo-kwd-alist))
  7839. (interpret (nth 1 aa))
  7840. (head (nth 2 aa))
  7841. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7842. (msg "Entry repeats: ")
  7843. (org-log-done nil)
  7844. (org-todo-log-states nil)
  7845. (nshiftmax 10) (nshift 0)
  7846. re type n what ts mb0 time)
  7847. (when repeat
  7848. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7849. (org-todo (if (eq interpret 'type) last-state head))
  7850. (when org-log-repeat
  7851. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7852. (memq 'org-add-log-note post-command-hook))
  7853. ;; OK, we are already setup for some record
  7854. (if (eq org-log-repeat 'note)
  7855. ;; make sure we take a note, not only a time stamp
  7856. (setq org-log-note-how 'note))
  7857. ;; Set up for taking a record
  7858. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7859. 'findpos org-log-repeat)))
  7860. (org-back-to-heading t)
  7861. (org-add-planning-info nil nil 'closed)
  7862. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7863. org-deadline-time-regexp "\\)\\|\\("
  7864. org-ts-regexp "\\)"))
  7865. (while (re-search-forward
  7866. re (save-excursion (outline-next-heading) (point)) t)
  7867. (setq type (if (match-end 1) org-scheduled-string
  7868. (if (match-end 3) org-deadline-string "Plain:"))
  7869. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7870. mb0 (match-beginning 0))
  7871. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7872. (setq n (string-to-number (match-string 2 ts))
  7873. what (match-string 3 ts))
  7874. (if (equal what "w") (setq n (* n 7) what "d"))
  7875. ;; Preparation, see if we need to modify the start date for the change
  7876. (when (match-end 1)
  7877. (setq time (save-match-data (org-time-string-to-time ts)))
  7878. (cond
  7879. ((equal (match-string 1 ts) ".")
  7880. ;; Shift starting date to today
  7881. (org-timestamp-change
  7882. (- (time-to-days (current-time)) (time-to-days time))
  7883. 'day))
  7884. ((equal (match-string 1 ts) "+")
  7885. (while (or (= nshift 0)
  7886. (<= (time-to-days time) (time-to-days (current-time))))
  7887. (when (= (incf nshift) nshiftmax)
  7888. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7889. (error "Abort")))
  7890. (org-timestamp-change n (cdr (assoc what whata)))
  7891. (org-at-timestamp-p t)
  7892. (setq ts (match-string 1))
  7893. (setq time (save-match-data (org-time-string-to-time ts))))
  7894. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7895. ;; rematch, so that we have everything in place for the real shift
  7896. (org-at-timestamp-p t)
  7897. (setq ts (match-string 1))
  7898. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7899. (org-timestamp-change n (cdr (assoc what whata)))
  7900. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7901. (setq org-log-post-message msg)
  7902. (message "%s" msg))))
  7903. (defun org-show-todo-tree (arg)
  7904. "Make a compact tree which shows all headlines marked with TODO.
  7905. The tree will show the lines where the regexp matches, and all higher
  7906. headlines above the match.
  7907. With a \\[universal-argument] prefix, also show the DONE entries.
  7908. With a numeric prefix N, construct a sparse tree for the Nth element
  7909. of `org-todo-keywords-1'."
  7910. (interactive "P")
  7911. (let ((case-fold-search nil)
  7912. (kwd-re
  7913. (cond ((null arg) org-not-done-regexp)
  7914. ((equal arg '(4))
  7915. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7916. (mapcar 'list org-todo-keywords-1))))
  7917. (concat "\\("
  7918. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7919. "\\)\\>")))
  7920. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7921. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7922. org-todo-keywords-1)))
  7923. (t (error "Invalid prefix argument: %s" arg)))))
  7924. (message "%d TODO entries found"
  7925. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7926. (defun org-deadline (&optional remove time)
  7927. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7928. With argument REMOVE, remove any deadline from the item.
  7929. When TIME is set, it should be an internal time specification, and the
  7930. scheduling will use the corresponding date."
  7931. (interactive "P")
  7932. (if remove
  7933. (progn
  7934. (org-remove-timestamp-with-keyword org-deadline-string)
  7935. (message "Item no longer has a deadline."))
  7936. (if (org-get-repeat)
  7937. (error "Cannot change deadline on task with repeater, please do that by hand")
  7938. (org-add-planning-info 'deadline time 'closed)
  7939. (message "Deadline on %s" org-last-inserted-timestamp))))
  7940. (defun org-schedule (&optional remove time)
  7941. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7942. With argument REMOVE, remove any scheduling date from the item.
  7943. When TIME is set, it should be an internal time specification, and the
  7944. scheduling will use the corresponding date."
  7945. (interactive "P")
  7946. (if remove
  7947. (progn
  7948. (org-remove-timestamp-with-keyword org-scheduled-string)
  7949. (message "Item is no longer scheduled."))
  7950. (if (org-get-repeat)
  7951. (error "Cannot reschedule task with repeater, please do that by hand")
  7952. (org-add-planning-info 'scheduled time 'closed)
  7953. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7954. (defun org-remove-timestamp-with-keyword (keyword)
  7955. "Remove all time stamps with KEYWORD in the current entry."
  7956. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7957. beg)
  7958. (save-excursion
  7959. (org-back-to-heading t)
  7960. (setq beg (point))
  7961. (org-end-of-subtree t t)
  7962. (while (re-search-backward re beg t)
  7963. (replace-match "")
  7964. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7965. (equal (char-before) ?\ ))
  7966. (backward-delete-char 1)
  7967. (if (string-match "^[ \t]*$" (buffer-substring
  7968. (point-at-bol) (point-at-eol)))
  7969. (delete-region (point-at-bol)
  7970. (min (point-max) (1+ (point-at-eol))))))))))
  7971. (defun org-add-planning-info (what &optional time &rest remove)
  7972. "Insert new timestamp with keyword in the line directly after the headline.
  7973. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7974. If non is given, the user is prompted for a date.
  7975. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7976. be removed."
  7977. (interactive)
  7978. (let (org-time-was-given org-end-time-was-given ts
  7979. end default-time default-input)
  7980. (when (and (not time) (memq what '(scheduled deadline)))
  7981. ;; Try to get a default date/time from existing timestamp
  7982. (save-excursion
  7983. (org-back-to-heading t)
  7984. (setq end (save-excursion (outline-next-heading) (point)))
  7985. (when (re-search-forward (if (eq what 'scheduled)
  7986. org-scheduled-time-regexp
  7987. org-deadline-time-regexp)
  7988. end t)
  7989. (setq ts (match-string 1)
  7990. default-time
  7991. (apply 'encode-time (org-parse-time-string ts))
  7992. default-input (and ts (org-get-compact-tod ts))))))
  7993. (when what
  7994. ;; If necessary, get the time from the user
  7995. (setq time (or time (org-read-date nil 'to-time nil nil
  7996. default-time default-input))))
  7997. (when (and org-insert-labeled-timestamps-at-point
  7998. (member what '(scheduled deadline)))
  7999. (insert
  8000. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8001. (org-insert-time-stamp time org-time-was-given
  8002. nil nil nil (list org-end-time-was-given))
  8003. (setq what nil))
  8004. (save-excursion
  8005. (save-restriction
  8006. (let (col list elt ts buffer-invisibility-spec)
  8007. (org-back-to-heading t)
  8008. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8009. (goto-char (match-end 1))
  8010. (setq col (current-column))
  8011. (goto-char (match-end 0))
  8012. (if (eobp) (insert "\n") (forward-char 1))
  8013. (if (and (not (looking-at outline-regexp))
  8014. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8015. "[^\r\n]*"))
  8016. (not (equal (match-string 1) org-clock-string)))
  8017. (narrow-to-region (match-beginning 0) (match-end 0))
  8018. (insert-before-markers "\n")
  8019. (backward-char 1)
  8020. (narrow-to-region (point) (point))
  8021. (and org-adapt-indentation (org-indent-to-column col)))
  8022. ;; Check if we have to remove something.
  8023. (setq list (cons what remove))
  8024. (while list
  8025. (setq elt (pop list))
  8026. (goto-char (point-min))
  8027. (when (or (and (eq elt 'scheduled)
  8028. (re-search-forward org-scheduled-time-regexp nil t))
  8029. (and (eq elt 'deadline)
  8030. (re-search-forward org-deadline-time-regexp nil t))
  8031. (and (eq elt 'closed)
  8032. (re-search-forward org-closed-time-regexp nil t)))
  8033. (replace-match "")
  8034. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8035. (if (looking-at " +") (replace-match ""))))
  8036. (goto-char (point-max))
  8037. (when what
  8038. (insert
  8039. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8040. (cond ((eq what 'scheduled) org-scheduled-string)
  8041. ((eq what 'deadline) org-deadline-string)
  8042. ((eq what 'closed) org-closed-string))
  8043. " ")
  8044. (setq ts (org-insert-time-stamp
  8045. time
  8046. (or org-time-was-given
  8047. (and (eq what 'closed) org-log-done-with-time))
  8048. (eq what 'closed)
  8049. nil nil (list org-end-time-was-given)))
  8050. (end-of-line 1))
  8051. (goto-char (point-min))
  8052. (widen)
  8053. (if (and (looking-at "[ \t]+\n")
  8054. (equal (char-before) ?\n))
  8055. (delete-region (1- (point)) (point-at-eol)))
  8056. ts)))))
  8057. (defvar org-log-note-marker (make-marker))
  8058. (defvar org-log-note-purpose nil)
  8059. (defvar org-log-note-state nil)
  8060. (defvar org-log-note-how nil)
  8061. (defvar org-log-note-extra nil)
  8062. (defvar org-log-note-window-configuration nil)
  8063. (defvar org-log-note-return-to (make-marker))
  8064. (defvar org-log-post-message nil
  8065. "Message to be displayed after a log note has been stored.
  8066. The auto-repeater uses this.")
  8067. (defun org-add-note ()
  8068. "Add a note to the current entry.
  8069. This is done in the same way as adding a state change note."
  8070. (interactive)
  8071. (org-add-log-setup 'note nil 'findpos nil))
  8072. (defvar org-property-end-re)
  8073. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  8074. "Set up the post command hook to take a note.
  8075. If this is about to TODO state change, the new state is expected in STATE.
  8076. When FINDPOS is non-nil, find the correct position for the note in
  8077. the current entry. If not, assume that it can be inserted at point.
  8078. HOW is an indicator what kind of note should be created.
  8079. EXTRA is additional text that will be inserted into the notes buffer."
  8080. (save-restriction
  8081. (save-excursion
  8082. (when findpos
  8083. (org-back-to-heading t)
  8084. (narrow-to-region (point) (save-excursion
  8085. (outline-next-heading) (point)))
  8086. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8087. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8088. "[^\r\n]*\\)?"))
  8089. (goto-char (match-end 0))
  8090. (when (and org-log-state-notes-insert-after-drawers
  8091. (save-excursion
  8092. (forward-line) (looking-at org-drawer-regexp)))
  8093. (progn (forward-line)
  8094. (while (looking-at org-drawer-regexp)
  8095. (goto-char (match-end 0))
  8096. (re-search-forward org-property-end-re (point-max) t)
  8097. (forward-line))
  8098. (forward-line -1)))
  8099. (unless org-log-states-order-reversed
  8100. (and (= (char-after) ?\n) (forward-char 1))
  8101. (org-skip-over-state-notes)
  8102. (skip-chars-backward " \t\n\r")))
  8103. (move-marker org-log-note-marker (point))
  8104. (setq org-log-note-purpose purpose
  8105. org-log-note-state state
  8106. org-log-note-how how
  8107. org-log-note-extra extra)
  8108. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8109. (defun org-skip-over-state-notes ()
  8110. "Skip past the list of State notes in an entry."
  8111. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8112. (while (looking-at "[ \t]*- State")
  8113. (condition-case nil
  8114. (org-next-item)
  8115. (error (org-end-of-item)))))
  8116. (defun org-add-log-note (&optional purpose)
  8117. "Pop up a window for taking a note, and add this note later at point."
  8118. (remove-hook 'post-command-hook 'org-add-log-note)
  8119. (setq org-log-note-window-configuration (current-window-configuration))
  8120. (delete-other-windows)
  8121. (move-marker org-log-note-return-to (point))
  8122. (switch-to-buffer (marker-buffer org-log-note-marker))
  8123. (goto-char org-log-note-marker)
  8124. (org-switch-to-buffer-other-window "*Org Note*")
  8125. (erase-buffer)
  8126. (if (memq org-log-note-how '(time state))
  8127. (let (current-prefix-arg) (org-store-log-note))
  8128. (let ((org-inhibit-startup t)) (org-mode))
  8129. (insert (format "# Insert note for %s.
  8130. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8131. (cond
  8132. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8133. ((eq org-log-note-purpose 'done) "closed todo item")
  8134. ((eq org-log-note-purpose 'state)
  8135. (format "state change to \"%s\"" org-log-note-state))
  8136. ((eq org-log-note-purpose 'note)
  8137. "this entry")
  8138. (t (error "This should not happen")))))
  8139. (if org-log-note-extra (insert org-log-note-extra))
  8140. (org-set-local 'org-finish-function 'org-store-log-note)))
  8141. (defvar org-note-abort nil) ; dynamically scoped
  8142. (defun org-store-log-note ()
  8143. "Finish taking a log note, and insert it to where it belongs."
  8144. (let ((txt (buffer-string))
  8145. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8146. lines ind)
  8147. (kill-buffer (current-buffer))
  8148. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8149. (setq txt (replace-match "" t t txt)))
  8150. (if (string-match "\\s-+\\'" txt)
  8151. (setq txt (replace-match "" t t txt)))
  8152. (setq lines (org-split-string txt "\n"))
  8153. (when (and note (string-match "\\S-" note))
  8154. (setq note
  8155. (org-replace-escapes
  8156. note
  8157. (list (cons "%u" (user-login-name))
  8158. (cons "%U" user-full-name)
  8159. (cons "%t" (format-time-string
  8160. (org-time-stamp-format 'long 'inactive)
  8161. (current-time)))
  8162. (cons "%s" (if org-log-note-state
  8163. (concat "\"" org-log-note-state "\"")
  8164. "")))))
  8165. (if lines (setq note (concat note " \\\\")))
  8166. (push note lines))
  8167. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8168. (when lines
  8169. (save-excursion
  8170. (set-buffer (marker-buffer org-log-note-marker))
  8171. (save-excursion
  8172. (goto-char org-log-note-marker)
  8173. (move-marker org-log-note-marker nil)
  8174. (end-of-line 1)
  8175. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8176. (indent-relative nil)
  8177. (insert "- " (pop lines))
  8178. (org-indent-line-function)
  8179. (beginning-of-line 1)
  8180. (looking-at "[ \t]*")
  8181. (setq ind (concat (match-string 0) " "))
  8182. (end-of-line 1)
  8183. (while lines (insert "\n" ind (pop lines)))))))
  8184. (set-window-configuration org-log-note-window-configuration)
  8185. (with-current-buffer (marker-buffer org-log-note-return-to)
  8186. (goto-char org-log-note-return-to))
  8187. (move-marker org-log-note-return-to nil)
  8188. (and org-log-post-message (message "%s" org-log-post-message)))
  8189. (defun org-sparse-tree (&optional arg)
  8190. "Create a sparse tree, prompt for the details.
  8191. This command can create sparse trees. You first need to select the type
  8192. of match used to create the tree:
  8193. t Show entries with a specific TODO keyword.
  8194. T Show entries selected by a tags match.
  8195. p Enter a property name and its value (both with completion on existing
  8196. names/values) and show entries with that property.
  8197. r Show entries matching a regular expression
  8198. d Show deadlines due within `org-deadline-warning-days'."
  8199. (interactive "P")
  8200. (let (ans kwd value)
  8201. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8202. (setq ans (read-char-exclusive))
  8203. (cond
  8204. ((equal ans ?d)
  8205. (call-interactively 'org-check-deadlines))
  8206. ((equal ans ?b)
  8207. (call-interactively 'org-check-before-date))
  8208. ((equal ans ?t)
  8209. (org-show-todo-tree '(4)))
  8210. ((equal ans ?T)
  8211. (call-interactively 'org-tags-sparse-tree))
  8212. ((member ans '(?p ?P))
  8213. (setq kwd (org-ido-completing-read "Property: "
  8214. (mapcar 'list (org-buffer-property-keys))))
  8215. (setq value (org-ido-completing-read "Value: "
  8216. (mapcar 'list (org-property-values kwd))))
  8217. (unless (string-match "\\`{.*}\\'" value)
  8218. (setq value (concat "\"" value "\"")))
  8219. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8220. ((member ans '(?r ?R ?/))
  8221. (call-interactively 'org-occur))
  8222. (t (error "No such sparse tree command \"%c\"" ans)))))
  8223. (defvar org-occur-highlights nil
  8224. "List of overlays used for occur matches.")
  8225. (make-variable-buffer-local 'org-occur-highlights)
  8226. (defvar org-occur-parameters nil
  8227. "Parameters of the active org-occur calls.
  8228. This is a list, each call to org-occur pushes as cons cell,
  8229. containing the regular expression and the callback, onto the list.
  8230. The list can contain several entries if `org-occur' has been called
  8231. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8232. will only contain one set of parameters. When the highlights are
  8233. removed (for example with `C-c C-c', or with the next edit (depending
  8234. on `org-remove-highlights-with-change'), this variable is emptied
  8235. as well.")
  8236. (make-variable-buffer-local 'org-occur-parameters)
  8237. (defun org-occur (regexp &optional keep-previous callback)
  8238. "Make a compact tree which shows all matches of REGEXP.
  8239. The tree will show the lines where the regexp matches, and all higher
  8240. headlines above the match. It will also show the heading after the match,
  8241. to make sure editing the matching entry is easy.
  8242. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8243. call to `org-occur' will be kept, to allow stacking of calls to this
  8244. command.
  8245. If CALLBACK is non-nil, it is a function which is called to confirm
  8246. that the match should indeed be shown."
  8247. (interactive "sRegexp: \nP")
  8248. (unless keep-previous
  8249. (org-remove-occur-highlights nil nil t))
  8250. (push (cons regexp callback) org-occur-parameters)
  8251. (let ((cnt 0))
  8252. (save-excursion
  8253. (goto-char (point-min))
  8254. (if (or (not keep-previous) ; do not want to keep
  8255. (not org-occur-highlights)) ; no previous matches
  8256. ;; hide everything
  8257. (org-overview))
  8258. (while (re-search-forward regexp nil t)
  8259. (when (or (not callback)
  8260. (save-match-data (funcall callback)))
  8261. (setq cnt (1+ cnt))
  8262. (when org-highlight-sparse-tree-matches
  8263. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8264. (org-show-context 'occur-tree))))
  8265. (when org-remove-highlights-with-change
  8266. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8267. nil 'local))
  8268. (unless org-sparse-tree-open-archived-trees
  8269. (org-hide-archived-subtrees (point-min) (point-max)))
  8270. (run-hooks 'org-occur-hook)
  8271. (if (interactive-p)
  8272. (message "%d match(es) for regexp %s" cnt regexp))
  8273. cnt))
  8274. (defun org-show-context (&optional key)
  8275. "Make sure point and context and visible.
  8276. How much context is shown depends upon the variables
  8277. `org-show-hierarchy-above', `org-show-following-heading'. and
  8278. `org-show-siblings'."
  8279. (let ((heading-p (org-on-heading-p t))
  8280. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8281. (following-p (org-get-alist-option org-show-following-heading key))
  8282. (entry-p (org-get-alist-option org-show-entry-below key))
  8283. (siblings-p (org-get-alist-option org-show-siblings key)))
  8284. (catch 'exit
  8285. ;; Show heading or entry text
  8286. (if (and heading-p (not entry-p))
  8287. (org-flag-heading nil) ; only show the heading
  8288. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8289. (org-show-hidden-entry))) ; show entire entry
  8290. (when following-p
  8291. ;; Show next sibling, or heading below text
  8292. (save-excursion
  8293. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8294. (org-flag-heading nil))))
  8295. (when siblings-p (org-show-siblings))
  8296. (when hierarchy-p
  8297. ;; show all higher headings, possibly with siblings
  8298. (save-excursion
  8299. (while (and (condition-case nil
  8300. (progn (org-up-heading-all 1) t)
  8301. (error nil))
  8302. (not (bobp)))
  8303. (org-flag-heading nil)
  8304. (when siblings-p (org-show-siblings))))))))
  8305. (defun org-reveal (&optional siblings)
  8306. "Show current entry, hierarchy above it, and the following headline.
  8307. This can be used to show a consistent set of context around locations
  8308. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8309. not t for the search context.
  8310. With optional argument SIBLINGS, on each level of the hierarchy all
  8311. siblings are shown. This repairs the tree structure to what it would
  8312. look like when opened with hierarchical calls to `org-cycle'."
  8313. (interactive "P")
  8314. (let ((org-show-hierarchy-above t)
  8315. (org-show-following-heading t)
  8316. (org-show-siblings (if siblings t org-show-siblings)))
  8317. (org-show-context nil)))
  8318. (defun org-highlight-new-match (beg end)
  8319. "Highlight from BEG to END and mark the highlight is an occur headline."
  8320. (let ((ov (org-make-overlay beg end)))
  8321. (org-overlay-put ov 'face 'secondary-selection)
  8322. (push ov org-occur-highlights)))
  8323. (defun org-remove-occur-highlights (&optional beg end noremove)
  8324. "Remove the occur highlights from the buffer.
  8325. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8326. from the `before-change-functions' in the current buffer."
  8327. (interactive)
  8328. (unless org-inhibit-highlight-removal
  8329. (mapc 'org-delete-overlay org-occur-highlights)
  8330. (setq org-occur-highlights nil)
  8331. (setq org-occur-parameters nil)
  8332. (unless noremove
  8333. (remove-hook 'before-change-functions
  8334. 'org-remove-occur-highlights 'local))))
  8335. ;;;; Priorities
  8336. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8337. "Regular expression matching the priority indicator.")
  8338. (defvar org-remove-priority-next-time nil)
  8339. (defun org-priority-up ()
  8340. "Increase the priority of the current item."
  8341. (interactive)
  8342. (org-priority 'up))
  8343. (defun org-priority-down ()
  8344. "Decrease the priority of the current item."
  8345. (interactive)
  8346. (org-priority 'down))
  8347. (defun org-priority (&optional action)
  8348. "Change the priority of an item by ARG.
  8349. ACTION can be `set', `up', `down', or a character."
  8350. (interactive)
  8351. (setq action (or action 'set))
  8352. (let (current new news have remove)
  8353. (save-excursion
  8354. (org-back-to-heading)
  8355. (if (looking-at org-priority-regexp)
  8356. (setq current (string-to-char (match-string 2))
  8357. have t)
  8358. (setq current org-default-priority))
  8359. (cond
  8360. ((or (eq action 'set)
  8361. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8362. (if (not (eq action 'set))
  8363. (setq new action)
  8364. (message "Priority %c-%c, SPC to remove: "
  8365. org-highest-priority org-lowest-priority)
  8366. (setq new (read-char-exclusive)))
  8367. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8368. (= (upcase org-lowest-priority) org-lowest-priority))
  8369. (setq new (upcase new)))
  8370. (cond ((equal new ?\ ) (setq remove t))
  8371. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8372. (error "Priority must be between `%c' and `%c'"
  8373. org-highest-priority org-lowest-priority))))
  8374. ((eq action 'up)
  8375. (if (and (not have) (eq last-command this-command))
  8376. (setq new org-lowest-priority)
  8377. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8378. org-default-priority (1- current)))))
  8379. ((eq action 'down)
  8380. (if (and (not have) (eq last-command this-command))
  8381. (setq new org-highest-priority)
  8382. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8383. org-default-priority (1+ current)))))
  8384. (t (error "Invalid action")))
  8385. (if (or (< (upcase new) org-highest-priority)
  8386. (> (upcase new) org-lowest-priority))
  8387. (setq remove t))
  8388. (setq news (format "%c" new))
  8389. (if have
  8390. (if remove
  8391. (replace-match "" t t nil 1)
  8392. (replace-match news t t nil 2))
  8393. (if remove
  8394. (error "No priority cookie found in line")
  8395. (looking-at org-todo-line-regexp)
  8396. (if (match-end 2)
  8397. (progn
  8398. (goto-char (match-end 2))
  8399. (insert " [#" news "]"))
  8400. (goto-char (match-beginning 3))
  8401. (insert "[#" news "] ")))))
  8402. (org-preserve-lc (org-set-tags nil 'align))
  8403. (if remove
  8404. (message "Priority removed")
  8405. (message "Priority of current item set to %s" news))))
  8406. (defun org-get-priority (s)
  8407. "Find priority cookie and return priority."
  8408. (save-match-data
  8409. (if (not (string-match org-priority-regexp s))
  8410. (* 1000 (- org-lowest-priority org-default-priority))
  8411. (* 1000 (- org-lowest-priority
  8412. (string-to-char (match-string 2 s)))))))
  8413. ;;;; Tags
  8414. (defvar org-agenda-archives-mode)
  8415. (defun org-scan-tags (action matcher &optional todo-only)
  8416. "Scan headline tags with inheritance and produce output ACTION.
  8417. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8418. or `agenda' to produce an entry list for an agenda view. It can also be
  8419. a Lisp form or a function that should be called at each matched headline, in
  8420. this case the return value is a list of all return values from these calls.
  8421. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8422. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8423. only lines with a TODO keyword are included in the output."
  8424. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8425. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8426. (org-re
  8427. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8428. (props (list 'face 'default
  8429. 'done-face 'org-done
  8430. 'undone-face 'default
  8431. 'mouse-face 'highlight
  8432. 'org-not-done-regexp org-not-done-regexp
  8433. 'org-todo-regexp org-todo-regexp
  8434. 'keymap org-agenda-keymap
  8435. 'help-echo
  8436. (format "mouse-2 or RET jump to org file %s"
  8437. (abbreviate-file-name
  8438. (or (buffer-file-name (buffer-base-buffer))
  8439. (buffer-name (buffer-base-buffer)))))))
  8440. (case-fold-search nil)
  8441. lspos tags tags-list
  8442. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8443. (llast 0) rtn rtn1 level category i txt
  8444. todo marker entry priority)
  8445. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8446. (setq action (list 'lambda nil action)))
  8447. (save-excursion
  8448. (goto-char (point-min))
  8449. (when (eq action 'sparse-tree)
  8450. (org-overview)
  8451. (org-remove-occur-highlights))
  8452. (while (re-search-forward re nil t)
  8453. (catch :skip
  8454. (setq todo (if (match-end 1) (match-string 2))
  8455. tags (if (match-end 4) (match-string 4)))
  8456. (goto-char (setq lspos (1+ (match-beginning 0))))
  8457. (setq level (org-reduced-level (funcall outline-level))
  8458. category (org-get-category))
  8459. (setq i llast llast level)
  8460. ;; remove tag lists from same and sublevels
  8461. (while (>= i level)
  8462. (when (setq entry (assoc i tags-alist))
  8463. (setq tags-alist (delete entry tags-alist)))
  8464. (setq i (1- i)))
  8465. ;; add the next tags
  8466. (when tags
  8467. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8468. tags-alist
  8469. (cons (cons level tags) tags-alist)))
  8470. ;; compile tags for current headline
  8471. (setq tags-list
  8472. (if org-use-tag-inheritance
  8473. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8474. tags))
  8475. (when org-use-tag-inheritance
  8476. (setcdr (car tags-alist)
  8477. (mapcar (lambda (x)
  8478. (setq x (copy-sequence x))
  8479. (org-add-prop-inherited x))
  8480. (cdar tags-alist))))
  8481. (when (and tags org-use-tag-inheritance
  8482. (not (eq t org-use-tag-inheritance)))
  8483. ;; selective inheritance, remove uninherited ones
  8484. (setcdr (car tags-alist)
  8485. (org-remove-uniherited-tags (cdar tags-alist))))
  8486. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8487. (let ((case-fold-search t)) (eval matcher))
  8488. (or
  8489. (not (member org-archive-tag tags-list))
  8490. ;; we have an archive tag, should we use this anyway?
  8491. (or (not org-agenda-skip-archived-trees)
  8492. (and (eq action 'agenda) org-agenda-archives-mode))))
  8493. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8494. ;; select this headline
  8495. (cond
  8496. ((eq action 'sparse-tree)
  8497. (and org-highlight-sparse-tree-matches
  8498. (org-get-heading) (match-end 0)
  8499. (org-highlight-new-match
  8500. (match-beginning 0) (match-beginning 1)))
  8501. (org-show-context 'tags-tree))
  8502. ((eq action 'agenda)
  8503. (setq txt (org-format-agenda-item
  8504. ""
  8505. (concat
  8506. (if org-tags-match-list-sublevels
  8507. (make-string (1- level) ?.) "")
  8508. (org-get-heading))
  8509. category (org-get-tags-at))
  8510. priority (org-get-priority txt))
  8511. (goto-char lspos)
  8512. (setq marker (org-agenda-new-marker))
  8513. (org-add-props txt props
  8514. 'org-marker marker 'org-hd-marker marker 'org-category category
  8515. 'priority priority 'type "tagsmatch")
  8516. (push txt rtn))
  8517. ((functionp action)
  8518. (save-excursion
  8519. (setq rtn1 (funcall action))
  8520. (push rtn1 rtn))
  8521. (goto-char (point-at-eol)))
  8522. (t (error "Invalid action")))
  8523. ;; if we are to skip sublevels, jump to end of subtree
  8524. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8525. (when (and (eq action 'sparse-tree)
  8526. (not org-sparse-tree-open-archived-trees))
  8527. (org-hide-archived-subtrees (point-min) (point-max)))
  8528. (nreverse rtn)))
  8529. (defun org-remove-uniherited-tags (tags)
  8530. "Remove all tags that are not inherited from the list TAGS."
  8531. (cond
  8532. ((eq org-use-tag-inheritance t)
  8533. (if org-tags-exclude-from-inheritance
  8534. (org-delete-all org-tags-exclude-from-inheritance tags)
  8535. tags))
  8536. ((not org-use-tag-inheritance) nil)
  8537. ((stringp org-use-tag-inheritance)
  8538. (delq nil (mapcar
  8539. (lambda (x)
  8540. (if (and (string-match org-use-tag-inheritance x)
  8541. (not (member x org-tags-exclude-from-inheritance)))
  8542. x nil))
  8543. tags)))
  8544. ((listp org-use-tag-inheritance)
  8545. (delq nil (mapcar
  8546. (lambda (x)
  8547. (if (member x org-use-tag-inheritance) x nil))
  8548. tags)))))
  8549. (defvar todo-only) ;; dynamically scoped
  8550. (defun org-tags-sparse-tree (&optional todo-only match)
  8551. "Create a sparse tree according to tags string MATCH.
  8552. MATCH can contain positive and negative selection of tags, like
  8553. \"+WORK+URGENT-WITHBOSS\".
  8554. If optional argument TODO-ONLY is non-nil, only select lines that are
  8555. also TODO lines."
  8556. (interactive "P")
  8557. (org-prepare-agenda-buffers (list (current-buffer)))
  8558. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8559. (defvar org-cached-props nil)
  8560. (defun org-cached-entry-get (pom property)
  8561. (if (or (eq t org-use-property-inheritance)
  8562. (and (stringp org-use-property-inheritance)
  8563. (string-match org-use-property-inheritance property))
  8564. (and (listp org-use-property-inheritance)
  8565. (member property org-use-property-inheritance)))
  8566. ;; Caching is not possible, check it directly
  8567. (org-entry-get pom property 'inherit)
  8568. ;; Get all properties, so that we can do complicated checks easily
  8569. (cdr (assoc property (or org-cached-props
  8570. (setq org-cached-props
  8571. (org-entry-properties pom)))))))
  8572. (defun org-global-tags-completion-table (&optional files)
  8573. "Return the list of all tags in all agenda buffer/files."
  8574. (save-excursion
  8575. (org-uniquify
  8576. (delq nil
  8577. (apply 'append
  8578. (mapcar
  8579. (lambda (file)
  8580. (set-buffer (find-file-noselect file))
  8581. (append (org-get-buffer-tags)
  8582. (mapcar (lambda (x) (if (stringp (car-safe x))
  8583. (list (car-safe x)) nil))
  8584. org-tag-alist)))
  8585. (if (and files (car files))
  8586. files
  8587. (org-agenda-files))))))))
  8588. (defun org-make-tags-matcher (match)
  8589. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8590. ;; todo-only is scoped dynamically into this function, and the function
  8591. ;; may change it if the matcher asks for it.
  8592. (unless match
  8593. ;; Get a new match request, with completion
  8594. (let ((org-last-tags-completion-table
  8595. (org-global-tags-completion-table)))
  8596. (setq match (org-completing-read
  8597. "Match: " 'org-tags-completion-function nil nil nil
  8598. 'org-tags-history))))
  8599. ;; Parse the string and create a lisp form
  8600. (let ((match0 match)
  8601. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8602. minus tag mm
  8603. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8604. orterms term orlist re-p str-p level-p level-op time-p
  8605. prop-p pn pv po cat-p gv rest)
  8606. (if (string-match "/+" match)
  8607. ;; match contains also a todo-matching request
  8608. (progn
  8609. (setq tagsmatch (substring match 0 (match-beginning 0))
  8610. todomatch (substring match (match-end 0)))
  8611. (if (string-match "^!" todomatch)
  8612. (setq todo-only t todomatch (substring todomatch 1)))
  8613. (if (string-match "^\\s-*$" todomatch)
  8614. (setq todomatch nil)))
  8615. ;; only matching tags
  8616. (setq tagsmatch match todomatch nil))
  8617. ;; Make the tags matcher
  8618. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8619. (setq tagsmatcher t)
  8620. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8621. (while (setq term (pop orterms))
  8622. (while (and (equal (substring term -1) "\\") orterms)
  8623. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8624. (while (string-match re term)
  8625. (setq rest (substring term (match-end 0))
  8626. minus (and (match-end 1)
  8627. (equal (match-string 1 term) "-"))
  8628. tag (match-string 2 term)
  8629. re-p (equal (string-to-char tag) ?{)
  8630. level-p (match-end 4)
  8631. prop-p (match-end 5)
  8632. mm (cond
  8633. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8634. (level-p
  8635. (setq level-op (org-op-to-function (match-string 3 term)))
  8636. `(,level-op level ,(string-to-number
  8637. (match-string 4 term))))
  8638. (prop-p
  8639. (setq pn (match-string 5 term)
  8640. po (match-string 6 term)
  8641. pv (match-string 7 term)
  8642. cat-p (equal pn "CATEGORY")
  8643. re-p (equal (string-to-char pv) ?{)
  8644. str-p (equal (string-to-char pv) ?\")
  8645. time-p (save-match-data
  8646. (string-match "^\"[[<].*[]>]\"$" pv))
  8647. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8648. (if time-p (setq pv (org-matcher-time pv)))
  8649. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8650. (cond
  8651. ((equal pn "CATEGORY")
  8652. (setq gv '(get-text-property (point) 'org-category)))
  8653. ((equal pn "TODO")
  8654. (setq gv 'todo))
  8655. (t
  8656. (setq gv `(org-cached-entry-get nil ,pn))))
  8657. (if re-p
  8658. (if (eq po 'org<>)
  8659. `(not (string-match ,pv (or ,gv "")))
  8660. `(string-match ,pv (or ,gv "")))
  8661. (if str-p
  8662. `(,po (or ,gv "") ,pv)
  8663. `(,po (string-to-number (or ,gv ""))
  8664. ,(string-to-number pv) ))))
  8665. (t `(member ,(downcase tag) tags-list)))
  8666. mm (if minus (list 'not mm) mm)
  8667. term rest)
  8668. (push mm tagsmatcher))
  8669. (push (if (> (length tagsmatcher) 1)
  8670. (cons 'and tagsmatcher)
  8671. (car tagsmatcher))
  8672. orlist)
  8673. (setq tagsmatcher nil))
  8674. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8675. (setq tagsmatcher
  8676. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8677. ;; Make the todo matcher
  8678. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8679. (setq todomatcher t)
  8680. (setq orterms (org-split-string todomatch "|") orlist nil)
  8681. (while (setq term (pop orterms))
  8682. (while (string-match re term)
  8683. (setq minus (and (match-end 1)
  8684. (equal (match-string 1 term) "-"))
  8685. kwd (match-string 2 term)
  8686. re-p (equal (string-to-char kwd) ?{)
  8687. term (substring term (match-end 0))
  8688. mm (if re-p
  8689. `(string-match ,(substring kwd 1 -1) todo)
  8690. (list 'equal 'todo kwd))
  8691. mm (if minus (list 'not mm) mm))
  8692. (push mm todomatcher))
  8693. (push (if (> (length todomatcher) 1)
  8694. (cons 'and todomatcher)
  8695. (car todomatcher))
  8696. orlist)
  8697. (setq todomatcher nil))
  8698. (setq todomatcher (if (> (length orlist) 1)
  8699. (cons 'or orlist) (car orlist))))
  8700. ;; Return the string and lisp forms of the matcher
  8701. (setq matcher (if todomatcher
  8702. (list 'and tagsmatcher todomatcher)
  8703. tagsmatcher))
  8704. (cons match0 matcher)))
  8705. (defun org-op-to-function (op &optional stringp)
  8706. "Turn an operator into the appropriate function."
  8707. (setq op
  8708. (cond
  8709. ((equal op "<" ) '(< string< org-time<))
  8710. ((equal op ">" ) '(> org-string> org-time>))
  8711. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8712. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8713. ((member op '("=" "==")) '(= string= org-time=))
  8714. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8715. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8716. (defun org<> (a b) (not (= a b)))
  8717. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8718. (defun org-string>= (a b) (not (string< a b)))
  8719. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8720. (defun org-string<> (a b) (not (string= a b)))
  8721. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  8722. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  8723. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  8724. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  8725. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  8726. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  8727. (defun org-2ft (s)
  8728. "Convert S to a floating point time.
  8729. If S is already a number, just return it. If it is a string, parse
  8730. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  8731. (cond
  8732. ((numberp s) s)
  8733. ((stringp s)
  8734. (condition-case nil
  8735. (float-time (apply 'encode-time (org-parse-time-string s)))
  8736. (error 0.)))
  8737. (t 0.)))
  8738. (defun org-time-today ()
  8739. "Time in seconds today at 0:00.
  8740. Returns the float number of seconds since the beginning of the
  8741. epoch to the beginning of today (00:00)."
  8742. (float-time (apply 'encode-time
  8743. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8744. (defun org-matcher-time (s)
  8745. "Interpret a time comparison value."
  8746. (save-match-data
  8747. (cond
  8748. ((string= s "<now>") (float-time))
  8749. ((string= s "<today>") (org-time-today))
  8750. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8751. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8752. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8753. (+ (org-time-today)
  8754. (* (string-to-number (match-string 1 s))
  8755. (cdr (assoc (match-string 2 s)
  8756. '(("d" . 86400.0) ("w" . 604800.0)
  8757. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8758. (t (org-2ft s)))))
  8759. (defun org-match-any-p (re list)
  8760. "Does re match any element of list?"
  8761. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8762. (delq nil list))
  8763. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8764. (defvar org-tags-overlay (org-make-overlay 1 1))
  8765. (org-detach-overlay org-tags-overlay)
  8766. (defun org-get-local-tags-at (&optional pos)
  8767. "Get a list of tags defined in the current headline."
  8768. (org-get-tags-at pos 'local))
  8769. (defun org-get-local-tags ()
  8770. "Get a list of tags defined in the current headline."
  8771. (org-get-tags-at nil 'local))
  8772. (defun org-get-tags-at (&optional pos local)
  8773. "Get a list of all headline tags applicable at POS.
  8774. POS defaults to point. If tags are inherited, the list contains
  8775. the targets in the same sequence as the headlines appear, i.e.
  8776. the tags of the current headline come last.
  8777. When LOCAL is non-nil, only return tags from the current headline,
  8778. ignore inherited ones."
  8779. (interactive)
  8780. (let (tags ltags lastpos parent)
  8781. (save-excursion
  8782. (save-restriction
  8783. (widen)
  8784. (goto-char (or pos (point)))
  8785. (save-match-data
  8786. (catch 'done
  8787. (condition-case nil
  8788. (progn
  8789. (org-back-to-heading t)
  8790. (while (not (equal lastpos (point)))
  8791. (setq lastpos (point))
  8792. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8793. (setq ltags (org-split-string
  8794. (org-match-string-no-properties 1) ":"))
  8795. (when parent
  8796. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8797. (setq tags (append
  8798. (if parent
  8799. (org-remove-uniherited-tags ltags)
  8800. ltags)
  8801. tags)))
  8802. (or org-use-tag-inheritance (throw 'done t))
  8803. (if local (throw 'done t))
  8804. (org-up-heading-all 1)
  8805. (setq parent t)))
  8806. (error nil)))))
  8807. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8808. (defun org-add-prop-inherited (s)
  8809. (add-text-properties 0 (length s) '(inherited t) s)
  8810. s)
  8811. (defun org-toggle-tag (tag &optional onoff)
  8812. "Toggle the tag TAG for the current line.
  8813. If ONOFF is `on' or `off', don't toggle but set to this state."
  8814. (let (res current)
  8815. (save-excursion
  8816. (org-back-to-heading t)
  8817. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8818. (point-at-eol) t)
  8819. (progn
  8820. (setq current (match-string 1))
  8821. (replace-match ""))
  8822. (setq current ""))
  8823. (setq current (nreverse (org-split-string current ":")))
  8824. (cond
  8825. ((eq onoff 'on)
  8826. (setq res t)
  8827. (or (member tag current) (push tag current)))
  8828. ((eq onoff 'off)
  8829. (or (not (member tag current)) (setq current (delete tag current))))
  8830. (t (if (member tag current)
  8831. (setq current (delete tag current))
  8832. (setq res t)
  8833. (push tag current))))
  8834. (end-of-line 1)
  8835. (if current
  8836. (progn
  8837. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8838. (org-set-tags nil t))
  8839. (delete-horizontal-space))
  8840. (run-hooks 'org-after-tags-change-hook))
  8841. res))
  8842. (defun org-align-tags-here (to-col)
  8843. ;; Assumes that this is a headline
  8844. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8845. (beginning-of-line 1)
  8846. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8847. (< pos (match-beginning 2)))
  8848. (progn
  8849. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8850. (goto-char (match-beginning 1))
  8851. (insert " ")
  8852. (delete-region (point) (1+ (match-beginning 2)))
  8853. (setq ncol (max (1+ (current-column))
  8854. (1+ col)
  8855. (if (> to-col 0)
  8856. to-col
  8857. (- (abs to-col) tags-l))))
  8858. (setq p (point))
  8859. (insert (make-string (- ncol (current-column)) ?\ ))
  8860. (setq ncol (current-column))
  8861. (when indent-tabs-mode (tabify p (point-at-eol)))
  8862. (org-move-to-column (min ncol col) t))
  8863. (goto-char pos))))
  8864. (defun org-set-tags-command (&optional arg just-align)
  8865. "Call the set-tags command for the current entry."
  8866. (interactive "P")
  8867. (if (org-on-heading-p)
  8868. (org-set-tags arg just-align)
  8869. (save-excursion
  8870. (org-back-to-heading t)
  8871. (org-set-tags arg just-align))))
  8872. (defun org-set-tags (&optional arg just-align)
  8873. "Set the tags for the current headline.
  8874. With prefix ARG, realign all tags in headings in the current buffer."
  8875. (interactive "P")
  8876. (let* ((re (concat "^" outline-regexp))
  8877. (current (org-get-tags-string))
  8878. (col (current-column))
  8879. (org-setting-tags t)
  8880. table current-tags inherited-tags ; computed below when needed
  8881. tags p0 c0 c1 rpl)
  8882. (if arg
  8883. (save-excursion
  8884. (goto-char (point-min))
  8885. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8886. (while (re-search-forward re nil t)
  8887. (org-set-tags nil t)
  8888. (end-of-line 1)))
  8889. (message "All tags realigned to column %d" org-tags-column))
  8890. (if just-align
  8891. (setq tags current)
  8892. ;; Get a new set of tags from the user
  8893. (save-excursion
  8894. (setq table (or org-tag-alist (org-get-buffer-tags))
  8895. org-last-tags-completion-table table
  8896. current-tags (org-split-string current ":")
  8897. inherited-tags (nreverse
  8898. (nthcdr (length current-tags)
  8899. (nreverse (org-get-tags-at))))
  8900. tags
  8901. (if (or (eq t org-use-fast-tag-selection)
  8902. (and org-use-fast-tag-selection
  8903. (delq nil (mapcar 'cdr table))))
  8904. (org-fast-tag-selection
  8905. current-tags inherited-tags table
  8906. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8907. (let ((org-add-colon-after-tag-completion t))
  8908. (org-trim
  8909. (org-without-partial-completion
  8910. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8911. nil nil current 'org-tags-history)))))))
  8912. (while (string-match "[-+&]+" tags)
  8913. ;; No boolean logic, just a list
  8914. (setq tags (replace-match ":" t t tags))))
  8915. (if (string-match "\\`[\t ]*\\'" tags)
  8916. (setq tags "")
  8917. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8918. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8919. ;; Insert new tags at the correct column
  8920. (beginning-of-line 1)
  8921. (cond
  8922. ((and (equal current "") (equal tags "")))
  8923. ((re-search-forward
  8924. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8925. (point-at-eol) t)
  8926. (if (equal tags "")
  8927. (setq rpl "")
  8928. (goto-char (match-beginning 0))
  8929. (setq c0 (current-column) p0 (point)
  8930. c1 (max (1+ c0) (if (> org-tags-column 0)
  8931. org-tags-column
  8932. (- (- org-tags-column) (length tags))))
  8933. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8934. (replace-match rpl t t)
  8935. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8936. tags)
  8937. (t (error "Tags alignment failed")))
  8938. (org-move-to-column col)
  8939. (unless just-align
  8940. (run-hooks 'org-after-tags-change-hook)))))
  8941. (defun org-change-tag-in-region (beg end tag off)
  8942. "Add or remove TAG for each entry in the region.
  8943. This works in the agenda, and also in an org-mode buffer."
  8944. (interactive
  8945. (list (region-beginning) (region-end)
  8946. (let ((org-last-tags-completion-table
  8947. (if (org-mode-p)
  8948. (org-get-buffer-tags)
  8949. (org-global-tags-completion-table))))
  8950. (org-ido-completing-read
  8951. "Tag: " 'org-tags-completion-function nil nil nil
  8952. 'org-tags-history))
  8953. (progn
  8954. (message "[s]et or [r]emove? ")
  8955. (equal (read-char-exclusive) ?r))))
  8956. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8957. (let ((agendap (equal major-mode 'org-agenda-mode))
  8958. l1 l2 m buf pos newhead (cnt 0))
  8959. (goto-char end)
  8960. (setq l2 (1- (org-current-line)))
  8961. (goto-char beg)
  8962. (setq l1 (org-current-line))
  8963. (loop for l from l1 to l2 do
  8964. (goto-line l)
  8965. (setq m (get-text-property (point) 'org-hd-marker))
  8966. (when (or (and (org-mode-p) (org-on-heading-p))
  8967. (and agendap m))
  8968. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8969. pos (if agendap m (point)))
  8970. (with-current-buffer buf
  8971. (save-excursion
  8972. (save-restriction
  8973. (goto-char pos)
  8974. (setq cnt (1+ cnt))
  8975. (org-toggle-tag tag (if off 'off 'on))
  8976. (setq newhead (org-get-heading)))))
  8977. (and agendap (org-agenda-change-all-lines newhead m))))
  8978. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8979. (defun org-tags-completion-function (string predicate &optional flag)
  8980. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8981. (confirm (lambda (x) (stringp (car x)))))
  8982. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8983. (setq s1 (match-string 1 string)
  8984. s2 (match-string 2 string))
  8985. (setq s1 "" s2 string))
  8986. (cond
  8987. ((eq flag nil)
  8988. ;; try completion
  8989. (setq rtn (try-completion s2 ctable confirm))
  8990. (if (stringp rtn)
  8991. (setq rtn
  8992. (concat s1 s2 (substring rtn (length s2))
  8993. (if (and org-add-colon-after-tag-completion
  8994. (assoc rtn ctable))
  8995. ":" ""))))
  8996. rtn)
  8997. ((eq flag t)
  8998. ;; all-completions
  8999. (all-completions s2 ctable confirm)
  9000. )
  9001. ((eq flag 'lambda)
  9002. ;; exact match?
  9003. (assoc s2 ctable)))
  9004. ))
  9005. (defun org-fast-tag-insert (kwd tags face &optional end)
  9006. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9007. (insert (format "%-12s" (concat kwd ":"))
  9008. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9009. (or end "")))
  9010. (defun org-fast-tag-show-exit (flag)
  9011. (save-excursion
  9012. (goto-line 3)
  9013. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9014. (replace-match ""))
  9015. (when flag
  9016. (end-of-line 1)
  9017. (org-move-to-column (- (window-width) 19) t)
  9018. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9019. (defun org-set-current-tags-overlay (current prefix)
  9020. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9021. (if (featurep 'xemacs)
  9022. (org-overlay-display org-tags-overlay (concat prefix s)
  9023. 'secondary-selection)
  9024. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9025. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9026. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9027. "Fast tag selection with single keys.
  9028. CURRENT is the current list of tags in the headline, INHERITED is the
  9029. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9030. possibly with grouping information. TODO-TABLE is a similar table with
  9031. TODO keywords, should these have keys assigned to them.
  9032. If the keys are nil, a-z are automatically assigned.
  9033. Returns the new tags string, or nil to not change the current settings."
  9034. (let* ((fulltable (append table todo-table))
  9035. (maxlen (apply 'max (mapcar
  9036. (lambda (x)
  9037. (if (stringp (car x)) (string-width (car x)) 0))
  9038. fulltable)))
  9039. (buf (current-buffer))
  9040. (expert (eq org-fast-tag-selection-single-key 'expert))
  9041. (buffer-tags nil)
  9042. (fwidth (+ maxlen 3 1 3))
  9043. (ncol (/ (- (window-width) 4) fwidth))
  9044. (i-face 'org-done)
  9045. (c-face 'org-todo)
  9046. tg cnt e c char c1 c2 ntable tbl rtn
  9047. ov-start ov-end ov-prefix
  9048. (exit-after-next org-fast-tag-selection-single-key)
  9049. (done-keywords org-done-keywords)
  9050. groups ingroup)
  9051. (save-excursion
  9052. (beginning-of-line 1)
  9053. (if (looking-at
  9054. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9055. (setq ov-start (match-beginning 1)
  9056. ov-end (match-end 1)
  9057. ov-prefix "")
  9058. (setq ov-start (1- (point-at-eol))
  9059. ov-end (1+ ov-start))
  9060. (skip-chars-forward "^\n\r")
  9061. (setq ov-prefix
  9062. (concat
  9063. (buffer-substring (1- (point)) (point))
  9064. (if (> (current-column) org-tags-column)
  9065. " "
  9066. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9067. (org-move-overlay org-tags-overlay ov-start ov-end)
  9068. (save-window-excursion
  9069. (if expert
  9070. (set-buffer (get-buffer-create " *Org tags*"))
  9071. (delete-other-windows)
  9072. (split-window-vertically)
  9073. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9074. (erase-buffer)
  9075. (org-set-local 'org-done-keywords done-keywords)
  9076. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9077. (org-fast-tag-insert "Current" current c-face "\n\n")
  9078. (org-fast-tag-show-exit exit-after-next)
  9079. (org-set-current-tags-overlay current ov-prefix)
  9080. (setq tbl fulltable char ?a cnt 0)
  9081. (while (setq e (pop tbl))
  9082. (cond
  9083. ((equal e '(:startgroup))
  9084. (push '() groups) (setq ingroup t)
  9085. (when (not (= cnt 0))
  9086. (setq cnt 0)
  9087. (insert "\n"))
  9088. (insert "{ "))
  9089. ((equal e '(:endgroup))
  9090. (setq ingroup nil cnt 0)
  9091. (insert "}\n"))
  9092. (t
  9093. (setq tg (car e) c2 nil)
  9094. (if (cdr e)
  9095. (setq c (cdr e))
  9096. ;; automatically assign a character.
  9097. (setq c1 (string-to-char
  9098. (downcase (substring
  9099. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9100. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9101. (while (or (rassoc char ntable) (rassoc char table))
  9102. (setq char (1+ char)))
  9103. (setq c2 c1))
  9104. (setq c (or c2 char)))
  9105. (if ingroup (push tg (car groups)))
  9106. (setq tg (org-add-props tg nil 'face
  9107. (cond
  9108. ((not (assoc tg table))
  9109. (org-get-todo-face tg))
  9110. ((member tg current) c-face)
  9111. ((member tg inherited) i-face)
  9112. (t nil))))
  9113. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9114. (insert "[" c "] " tg (make-string
  9115. (- fwidth 4 (length tg)) ?\ ))
  9116. (push (cons tg c) ntable)
  9117. (when (= (setq cnt (1+ cnt)) ncol)
  9118. (insert "\n")
  9119. (if ingroup (insert " "))
  9120. (setq cnt 0)))))
  9121. (setq ntable (nreverse ntable))
  9122. (insert "\n")
  9123. (goto-char (point-min))
  9124. (if (not expert) (org-fit-window-to-buffer))
  9125. (setq rtn
  9126. (catch 'exit
  9127. (while t
  9128. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9129. (if groups " [!] no groups" " [!]groups")
  9130. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9131. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9132. (cond
  9133. ((= c ?\r) (throw 'exit t))
  9134. ((= c ?!)
  9135. (setq groups (not groups))
  9136. (goto-char (point-min))
  9137. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9138. ((= c ?\C-c)
  9139. (if (not expert)
  9140. (org-fast-tag-show-exit
  9141. (setq exit-after-next (not exit-after-next)))
  9142. (setq expert nil)
  9143. (delete-other-windows)
  9144. (split-window-vertically)
  9145. (org-switch-to-buffer-other-window " *Org tags*")
  9146. (org-fit-window-to-buffer)))
  9147. ((or (= c ?\C-g)
  9148. (and (= c ?q) (not (rassoc c ntable))))
  9149. (org-detach-overlay org-tags-overlay)
  9150. (setq quit-flag t))
  9151. ((= c ?\ )
  9152. (setq current nil)
  9153. (if exit-after-next (setq exit-after-next 'now)))
  9154. ((= c ?\t)
  9155. (condition-case nil
  9156. (setq tg (org-ido-completing-read
  9157. "Tag: "
  9158. (or buffer-tags
  9159. (with-current-buffer buf
  9160. (org-get-buffer-tags)))))
  9161. (quit (setq tg "")))
  9162. (when (string-match "\\S-" tg)
  9163. (add-to-list 'buffer-tags (list tg))
  9164. (if (member tg current)
  9165. (setq current (delete tg current))
  9166. (push tg current)))
  9167. (if exit-after-next (setq exit-after-next 'now)))
  9168. ((setq e (rassoc c todo-table) tg (car e))
  9169. (with-current-buffer buf
  9170. (save-excursion (org-todo tg)))
  9171. (if exit-after-next (setq exit-after-next 'now)))
  9172. ((setq e (rassoc c ntable) tg (car e))
  9173. (if (member tg current)
  9174. (setq current (delete tg current))
  9175. (loop for g in groups do
  9176. (if (member tg g)
  9177. (mapc (lambda (x)
  9178. (setq current (delete x current)))
  9179. g)))
  9180. (push tg current))
  9181. (if exit-after-next (setq exit-after-next 'now))))
  9182. ;; Create a sorted list
  9183. (setq current
  9184. (sort current
  9185. (lambda (a b)
  9186. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9187. (if (eq exit-after-next 'now) (throw 'exit t))
  9188. (goto-char (point-min))
  9189. (beginning-of-line 2)
  9190. (delete-region (point) (point-at-eol))
  9191. (org-fast-tag-insert "Current" current c-face)
  9192. (org-set-current-tags-overlay current ov-prefix)
  9193. (while (re-search-forward
  9194. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9195. (setq tg (match-string 1))
  9196. (add-text-properties
  9197. (match-beginning 1) (match-end 1)
  9198. (list 'face
  9199. (cond
  9200. ((member tg current) c-face)
  9201. ((member tg inherited) i-face)
  9202. (t (get-text-property (match-beginning 1) 'face))))))
  9203. (goto-char (point-min)))))
  9204. (org-detach-overlay org-tags-overlay)
  9205. (if rtn
  9206. (mapconcat 'identity current ":")
  9207. nil))))
  9208. (defun org-get-tags-string ()
  9209. "Get the TAGS string in the current headline."
  9210. (unless (org-on-heading-p t)
  9211. (error "Not on a heading"))
  9212. (save-excursion
  9213. (beginning-of-line 1)
  9214. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9215. (org-match-string-no-properties 1)
  9216. "")))
  9217. (defun org-get-tags ()
  9218. "Get the list of tags specified in the current headline."
  9219. (org-split-string (org-get-tags-string) ":"))
  9220. (defun org-get-buffer-tags ()
  9221. "Get a table of all tags used in the buffer, for completion."
  9222. (let (tags)
  9223. (save-excursion
  9224. (goto-char (point-min))
  9225. (while (re-search-forward
  9226. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9227. (when (equal (char-after (point-at-bol 0)) ?*)
  9228. (mapc (lambda (x) (add-to-list 'tags x))
  9229. (org-split-string (org-match-string-no-properties 1) ":")))))
  9230. (mapcar 'list tags)))
  9231. ;;;; The mapping API
  9232. ;;;###autoload
  9233. (defun org-map-entries (func &optional match scope &rest skip)
  9234. "Call FUNC at each headline selected by MATCH in SCOPE.
  9235. FUNC is a function or a lisp form. The function will be called without
  9236. arguments, with the cursor positioned at the beginning of the headline.
  9237. The return values of all calls to the function will be collected and
  9238. returned as a list.
  9239. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9240. Only headlines that are matched by this query will be considered during
  9241. the iteration. When MATCH is nil or t, all headlines will be
  9242. visited by the iteration.
  9243. SCOPE determines the scope of this command. It can be any of:
  9244. nil The current buffer, respecting the restriction if any
  9245. tree The subtree started with the entry at point
  9246. file The current buffer, without restriction
  9247. file-with-archives
  9248. The current buffer, and any archives associated with it
  9249. agenda All agenda files
  9250. agenda-with-archives
  9251. All agenda files with any archive files associated with them
  9252. \(file1 file2 ...)
  9253. If this is a list, all files in the list will be scanned
  9254. The remaining args are treated as settings for the skipping facilities of
  9255. the scanner. The following items can be given here:
  9256. archive skip trees with the archive tag.
  9257. comment skip trees with the COMMENT keyword
  9258. function or Emacs Lisp form:
  9259. will be used as value for `org-agenda-skip-function', so whenever
  9260. the the function returns t, FUNC will not be called for that
  9261. entry and search will continue from the point where the
  9262. function leaves it."
  9263. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9264. (org-agenda-skip-archived-trees (memq 'archive skip))
  9265. (org-agenda-skip-comment-trees (memq 'comment skip))
  9266. (org-agenda-skip-function
  9267. (car (org-delete-all '(comment archive) skip)))
  9268. (org-tags-match-list-sublevels t)
  9269. matcher pos file res
  9270. org-todo-keywords-for-agenda
  9271. org-done-keywords-for-agenda
  9272. org-todo-keyword-alist-for-agenda
  9273. org-tag-alist-for-agenda)
  9274. (cond
  9275. ((eq match t) (setq matcher t))
  9276. ((eq match nil) (setq matcher t))
  9277. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9278. (save-excursion
  9279. (save-restriction
  9280. (when (eq scope 'tree)
  9281. (org-back-to-heading t)
  9282. (org-narrow-to-subtree)
  9283. (setq scope nil))
  9284. (if (not scope)
  9285. (progn
  9286. (org-prepare-agenda-buffers
  9287. (list (buffer-file-name (current-buffer))))
  9288. (setq res (org-scan-tags func matcher)))
  9289. ;; Get the right scope
  9290. (setq pos (point))
  9291. (cond
  9292. ((and scope (listp scope) (symbolp (car scope)))
  9293. (setq scope (eval scope)))
  9294. ((eq scope 'agenda)
  9295. (setq scope (org-agenda-files t)))
  9296. ((eq scope 'agenda-with-archives)
  9297. (setq scope (org-agenda-files t))
  9298. (setq scope (org-add-archive-files scope)))
  9299. ((eq scope 'file)
  9300. (setq scope (list (buffer-file-name))))
  9301. ((eq scope 'file-with-archives)
  9302. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9303. (org-prepare-agenda-buffers scope)
  9304. (while (setq file (pop scope))
  9305. (with-current-buffer (org-find-base-buffer-visiting file)
  9306. (save-excursion
  9307. (save-restriction
  9308. (widen)
  9309. (goto-char (point-min))
  9310. (setq res (append res (org-scan-tags func matcher))))))))))
  9311. res))
  9312. ;;;; Properties
  9313. ;;; Setting and retrieving properties
  9314. (defconst org-special-properties
  9315. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9316. "TIMESTAMP" "TIMESTAMP_IA")
  9317. "The special properties valid in Org-mode.
  9318. These are properties that are not defined in the property drawer,
  9319. but in some other way.")
  9320. (defconst org-default-properties
  9321. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9322. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9323. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9324. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9325. "Some properties that are used by Org-mode for various purposes.
  9326. Being in this list makes sure that they are offered for completion.")
  9327. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9328. "Regular expression matching the first line of a property drawer.")
  9329. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9330. "Regular expression matching the first line of a property drawer.")
  9331. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9332. "Regular expression matching the first line of a property drawer.")
  9333. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9334. "Regular expression matching the first line of a property drawer.")
  9335. (defconst org-property-drawer-re
  9336. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9337. org-property-end-re "\\)\n?")
  9338. "Matches an entire property drawer.")
  9339. (defconst org-clock-drawer-re
  9340. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9341. org-property-end-re "\\)\n?")
  9342. "Matches an entire clock drawer.")
  9343. (defun org-property-action ()
  9344. "Do an action on properties."
  9345. (interactive)
  9346. (let (c)
  9347. (org-at-property-p)
  9348. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9349. (setq c (read-char-exclusive))
  9350. (cond
  9351. ((equal c ?s)
  9352. (call-interactively 'org-set-property))
  9353. ((equal c ?d)
  9354. (call-interactively 'org-delete-property))
  9355. ((equal c ?D)
  9356. (call-interactively 'org-delete-property-globally))
  9357. ((equal c ?c)
  9358. (call-interactively 'org-compute-property-at-point))
  9359. (t (error "No such property action %c" c)))))
  9360. (defun org-at-property-p ()
  9361. "Is the cursor in a property line?"
  9362. ;; FIXME: Does not check if we are actually in the drawer.
  9363. ;; FIXME: also returns true on any drawers.....
  9364. ;; This is used by C-c C-c for property action.
  9365. (save-excursion
  9366. (beginning-of-line 1)
  9367. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9368. (defun org-get-property-block (&optional beg end force)
  9369. "Return the (beg . end) range of the body of the property drawer.
  9370. BEG and END can be beginning and end of subtree, if not given
  9371. they will be found.
  9372. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9373. (catch 'exit
  9374. (save-excursion
  9375. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9376. (end (or end (progn (outline-next-heading) (point)))))
  9377. (goto-char beg)
  9378. (if (re-search-forward org-property-start-re end t)
  9379. (setq beg (1+ (match-end 0)))
  9380. (if force
  9381. (save-excursion
  9382. (org-insert-property-drawer)
  9383. (setq end (progn (outline-next-heading) (point))))
  9384. (throw 'exit nil))
  9385. (goto-char beg)
  9386. (if (re-search-forward org-property-start-re end t)
  9387. (setq beg (1+ (match-end 0)))))
  9388. (if (re-search-forward org-property-end-re end t)
  9389. (setq end (match-beginning 0))
  9390. (or force (throw 'exit nil))
  9391. (goto-char beg)
  9392. (setq end beg)
  9393. (org-indent-line-function)
  9394. (insert ":END:\n"))
  9395. (cons beg end)))))
  9396. (defun org-entry-properties (&optional pom which)
  9397. "Get all properties of the entry at point-or-marker POM.
  9398. This includes the TODO keyword, the tags, time strings for deadline,
  9399. scheduled, and clocking, and any additional properties defined in the
  9400. entry. The return value is an alist, keys may occur multiple times
  9401. if the property key was used several times.
  9402. POM may also be nil, in which case the current entry is used.
  9403. If WHICH is nil or `all', get all properties. If WHICH is
  9404. `special' or `standard', only get that subclass."
  9405. (setq which (or which 'all))
  9406. (org-with-point-at pom
  9407. (let ((clockstr (substring org-clock-string 0 -1))
  9408. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9409. beg end range props sum-props key value string clocksum)
  9410. (save-excursion
  9411. (when (condition-case nil
  9412. (and (org-mode-p) (org-back-to-heading t))
  9413. (error nil))
  9414. (setq beg (point))
  9415. (setq sum-props (get-text-property (point) 'org-summaries))
  9416. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9417. (outline-next-heading)
  9418. (setq end (point))
  9419. (when (memq which '(all special))
  9420. ;; Get the special properties, like TODO and tags
  9421. (goto-char beg)
  9422. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9423. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9424. (when (looking-at org-priority-regexp)
  9425. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9426. (when (and (setq value (org-get-tags-string))
  9427. (string-match "\\S-" value))
  9428. (push (cons "TAGS" value) props))
  9429. (when (setq value (org-get-tags-at))
  9430. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9431. props))
  9432. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9433. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9434. string (if (equal key clockstr)
  9435. (org-no-properties
  9436. (org-trim
  9437. (buffer-substring
  9438. (match-beginning 3) (goto-char (point-at-eol)))))
  9439. (substring (org-match-string-no-properties 3) 1 -1)))
  9440. (unless key
  9441. (if (= (char-after (match-beginning 3)) ?\[)
  9442. (setq key "TIMESTAMP_IA")
  9443. (setq key "TIMESTAMP")))
  9444. (when (or (equal key clockstr) (not (assoc key props)))
  9445. (push (cons key string) props)))
  9446. )
  9447. (when (memq which '(all standard))
  9448. ;; Get the standard properties, like :PORP: ...
  9449. (setq range (org-get-property-block beg end))
  9450. (when range
  9451. (goto-char (car range))
  9452. (while (re-search-forward
  9453. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9454. (cdr range) t)
  9455. (setq key (org-match-string-no-properties 1)
  9456. value (org-trim (or (org-match-string-no-properties 2) "")))
  9457. (unless (member key excluded)
  9458. (push (cons key (or value "")) props)))))
  9459. (if clocksum
  9460. (push (cons "CLOCKSUM"
  9461. (org-columns-number-to-string (/ (float clocksum) 60.)
  9462. 'add_times))
  9463. props))
  9464. (unless (assoc "CATEGORY" props)
  9465. (setq value (or (org-get-category)
  9466. (progn (org-refresh-category-properties)
  9467. (org-get-category))))
  9468. (push (cons "CATEGORY" value) props))
  9469. (append sum-props (nreverse props)))))))
  9470. (defun org-entry-get (pom property &optional inherit)
  9471. "Get value of PROPERTY for entry at point-or-marker POM.
  9472. If INHERIT is non-nil and the entry does not have the property,
  9473. then also check higher levels of the hierarchy.
  9474. If INHERIT is the symbol `selective', use inheritance only if the setting
  9475. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9476. If the property is present but empty, the return value is the empty string.
  9477. If the property is not present at all, nil is returned."
  9478. (org-with-point-at pom
  9479. (if (and inherit (if (eq inherit 'selective)
  9480. (org-property-inherit-p property)
  9481. t))
  9482. (org-entry-get-with-inheritance property)
  9483. (if (member property org-special-properties)
  9484. ;; We need a special property. Use brute force, get all properties.
  9485. (cdr (assoc property (org-entry-properties nil 'special)))
  9486. (let ((range (org-get-property-block)))
  9487. (if (and range
  9488. (goto-char (car range))
  9489. (re-search-forward
  9490. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9491. (cdr range) t))
  9492. ;; Found the property, return it.
  9493. (if (match-end 1)
  9494. (org-match-string-no-properties 1)
  9495. "")))))))
  9496. (defun org-property-or-variable-value (var &optional inherit)
  9497. "Check if there is a property fixing the value of VAR.
  9498. If yes, return this value. If not, return the current value of the variable."
  9499. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9500. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9501. (read prop)
  9502. (symbol-value var))))
  9503. (defun org-entry-delete (pom property)
  9504. "Delete the property PROPERTY from entry at point-or-marker POM."
  9505. (org-with-point-at pom
  9506. (if (member property org-special-properties)
  9507. nil ; cannot delete these properties.
  9508. (let ((range (org-get-property-block)))
  9509. (if (and range
  9510. (goto-char (car range))
  9511. (re-search-forward
  9512. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9513. (cdr range) t))
  9514. (progn
  9515. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9516. t)
  9517. nil)))))
  9518. ;; Multi-values properties are properties that contain multiple values
  9519. ;; These values are assumed to be single words, separated by whitespace.
  9520. (defun org-entry-add-to-multivalued-property (pom property value)
  9521. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9522. (let* ((old (org-entry-get pom property))
  9523. (values (and old (org-split-string old "[ \t]"))))
  9524. (setq value (org-entry-protect-space value))
  9525. (unless (member value values)
  9526. (setq values (cons value values))
  9527. (org-entry-put pom property
  9528. (mapconcat 'identity values " ")))))
  9529. (defun org-entry-remove-from-multivalued-property (pom property value)
  9530. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9531. (let* ((old (org-entry-get pom property))
  9532. (values (and old (org-split-string old "[ \t]"))))
  9533. (setq value (org-entry-protect-space value))
  9534. (when (member value values)
  9535. (setq values (delete value values))
  9536. (org-entry-put pom property
  9537. (mapconcat 'identity values " ")))))
  9538. (defun org-entry-member-in-multivalued-property (pom property value)
  9539. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9540. (let* ((old (org-entry-get pom property))
  9541. (values (and old (org-split-string old "[ \t]"))))
  9542. (setq value (org-entry-protect-space value))
  9543. (member value values)))
  9544. (defun org-entry-get-multivalued-property (pom property)
  9545. "Return a list of values in a multivalued property."
  9546. (let* ((value (org-entry-get pom property))
  9547. (values (and value (org-split-string value "[ \t]"))))
  9548. (mapcar 'org-entry-restore-space values)))
  9549. (defun org-entry-put-multivalued-property (pom property &rest values)
  9550. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9551. VALUES should be a list of strings. Spaces will be protected."
  9552. (org-entry-put pom property
  9553. (mapconcat 'org-entry-protect-space values " "))
  9554. (let* ((value (org-entry-get pom property))
  9555. (values (and value (org-split-string value "[ \t]"))))
  9556. (mapcar 'org-entry-restore-space values)))
  9557. (defun org-entry-protect-space (s)
  9558. "Protect spaces and newline in string S."
  9559. (while (string-match " " s)
  9560. (setq s (replace-match "%20" t t s)))
  9561. (while (string-match "\n" s)
  9562. (setq s (replace-match "%0A" t t s)))
  9563. s)
  9564. (defun org-entry-restore-space (s)
  9565. "Restore spaces and newline in string S."
  9566. (while (string-match "%20" s)
  9567. (setq s (replace-match " " t t s)))
  9568. (while (string-match "%0A" s)
  9569. (setq s (replace-match "\n" t t s)))
  9570. s)
  9571. (defvar org-entry-property-inherited-from (make-marker)
  9572. "Marker pointing to the entry from where a property was inherited.
  9573. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9574. location of the entry where the inheritance search matched. If there was
  9575. no match, the marker will point nowhere.
  9576. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9577. is set.")
  9578. (defun org-entry-get-with-inheritance (property)
  9579. "Get entry property, and search higher levels if not present."
  9580. (move-marker org-entry-property-inherited-from nil)
  9581. (let (tmp)
  9582. (save-excursion
  9583. (save-restriction
  9584. (widen)
  9585. (catch 'ex
  9586. (while t
  9587. (when (setq tmp (org-entry-get nil property))
  9588. (org-back-to-heading t)
  9589. (move-marker org-entry-property-inherited-from (point))
  9590. (throw 'ex tmp))
  9591. (or (org-up-heading-safe) (throw 'ex nil)))))
  9592. (or tmp
  9593. (cdr (assoc property org-file-properties))
  9594. (cdr (assoc property org-global-properties))
  9595. (cdr (assoc property org-global-properties-fixed))))))
  9596. (defun org-entry-put (pom property value)
  9597. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9598. (org-with-point-at pom
  9599. (org-back-to-heading t)
  9600. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9601. range)
  9602. (cond
  9603. ((equal property "TODO")
  9604. (when (and (stringp value) (string-match "\\S-" value)
  9605. (not (member value org-todo-keywords-1)))
  9606. (error "\"%s\" is not a valid TODO state" value))
  9607. (if (or (not value)
  9608. (not (string-match "\\S-" value)))
  9609. (setq value 'none))
  9610. (org-todo value)
  9611. (org-set-tags nil 'align))
  9612. ((equal property "PRIORITY")
  9613. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9614. (string-to-char value) ?\ ))
  9615. (org-set-tags nil 'align))
  9616. ((equal property "SCHEDULED")
  9617. (if (re-search-forward org-scheduled-time-regexp end t)
  9618. (cond
  9619. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9620. ((eq value 'later) (org-timestamp-change 1 'day))
  9621. (t (call-interactively 'org-schedule)))
  9622. (call-interactively 'org-schedule)))
  9623. ((equal property "DEADLINE")
  9624. (if (re-search-forward org-deadline-time-regexp end t)
  9625. (cond
  9626. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9627. ((eq value 'later) (org-timestamp-change 1 'day))
  9628. (t (call-interactively 'org-deadline)))
  9629. (call-interactively 'org-deadline)))
  9630. ((member property org-special-properties)
  9631. (error "The %s property can not yet be set with `org-entry-put'"
  9632. property))
  9633. (t ; a non-special property
  9634. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9635. (setq range (org-get-property-block beg end 'force))
  9636. (goto-char (car range))
  9637. (if (re-search-forward
  9638. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9639. (progn
  9640. (delete-region (match-beginning 1) (match-end 1))
  9641. (goto-char (match-beginning 1)))
  9642. (goto-char (cdr range))
  9643. (insert "\n")
  9644. (backward-char 1)
  9645. (org-indent-line-function)
  9646. (insert ":" property ":"))
  9647. (and value (insert " " value))
  9648. (org-indent-line-function)))))))
  9649. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9650. "Get all property keys in the current buffer.
  9651. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9652. like tags and TODO state.
  9653. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9654. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9655. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9656. formats in the current buffer."
  9657. (let (rtn range cfmt cols s p)
  9658. (save-excursion
  9659. (save-restriction
  9660. (widen)
  9661. (goto-char (point-min))
  9662. (while (re-search-forward org-property-start-re nil t)
  9663. (setq range (org-get-property-block))
  9664. (goto-char (car range))
  9665. (while (re-search-forward
  9666. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9667. (cdr range) t)
  9668. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9669. (outline-next-heading))))
  9670. (when include-specials
  9671. (setq rtn (append org-special-properties rtn)))
  9672. (when include-defaults
  9673. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9674. (when include-columns
  9675. (save-excursion
  9676. (save-restriction
  9677. (widen)
  9678. (goto-char (point-min))
  9679. (while (re-search-forward
  9680. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9681. nil t)
  9682. (setq cfmt (match-string 2) s 0)
  9683. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9684. cfmt s)
  9685. (setq s (match-end 0)
  9686. p (match-string 1 cfmt))
  9687. (unless (or (equal p "ITEM")
  9688. (member p org-special-properties))
  9689. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9690. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9691. (defun org-property-values (key)
  9692. "Return a list of all values of property KEY."
  9693. (save-excursion
  9694. (save-restriction
  9695. (widen)
  9696. (goto-char (point-min))
  9697. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9698. values)
  9699. (while (re-search-forward re nil t)
  9700. (add-to-list 'values (org-trim (match-string 1))))
  9701. (delete "" values)))))
  9702. (defun org-insert-property-drawer ()
  9703. "Insert a property drawer into the current entry."
  9704. (interactive)
  9705. (org-back-to-heading t)
  9706. (looking-at outline-regexp)
  9707. (let ((indent (- (match-end 0)(match-beginning 0)))
  9708. (beg (point))
  9709. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9710. end hiddenp)
  9711. (outline-next-heading)
  9712. (setq end (point))
  9713. (goto-char beg)
  9714. (while (re-search-forward re end t))
  9715. (setq hiddenp (org-invisible-p))
  9716. (end-of-line 1)
  9717. (and (equal (char-after) ?\n) (forward-char 1))
  9718. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9719. (beginning-of-line 2))
  9720. (org-skip-over-state-notes)
  9721. (skip-chars-backward " \t\n\r")
  9722. (if (eq (char-before) ?*) (forward-char 1))
  9723. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9724. (beginning-of-line 0)
  9725. (org-indent-to-column indent)
  9726. (beginning-of-line 2)
  9727. (org-indent-to-column indent)
  9728. (beginning-of-line 0)
  9729. (if hiddenp
  9730. (save-excursion
  9731. (org-back-to-heading t)
  9732. (hide-entry))
  9733. (org-flag-drawer t))))
  9734. (defun org-set-property (property value)
  9735. "In the current entry, set PROPERTY to VALUE.
  9736. When called interactively, this will prompt for a property name, offering
  9737. completion on existing and default properties. And then it will prompt
  9738. for a value, offering completion either on allowed values (via an inherited
  9739. xxx_ALL property) or on existing values in other instances of this property
  9740. in the current file."
  9741. (interactive
  9742. (let* ((completion-ignore-case t)
  9743. (keys (org-buffer-property-keys nil t t))
  9744. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9745. (prop (if (member prop0 keys)
  9746. prop0
  9747. (or (cdr (assoc (downcase prop0)
  9748. (mapcar (lambda (x) (cons (downcase x) x))
  9749. keys)))
  9750. prop0)))
  9751. (cur (org-entry-get nil prop))
  9752. (allowed (org-property-get-allowed-values nil prop 'table))
  9753. (existing (mapcar 'list (org-property-values prop)))
  9754. (val (if allowed
  9755. (org-completing-read "Value: " allowed nil 'req-match)
  9756. (org-completing-read
  9757. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9758. (concat "[" cur "]") "")
  9759. ": ")
  9760. existing nil nil "" nil cur))))
  9761. (list prop (if (equal val "") cur val))))
  9762. (unless (equal (org-entry-get nil property) value)
  9763. (org-entry-put nil property value)))
  9764. (defun org-delete-property (property)
  9765. "In the current entry, delete PROPERTY."
  9766. (interactive
  9767. (let* ((completion-ignore-case t)
  9768. (prop (org-ido-completing-read
  9769. "Property: " (org-entry-properties nil 'standard))))
  9770. (list prop)))
  9771. (message "Property %s %s" property
  9772. (if (org-entry-delete nil property)
  9773. "deleted"
  9774. "was not present in the entry")))
  9775. (defun org-delete-property-globally (property)
  9776. "Remove PROPERTY globally, from all entries."
  9777. (interactive
  9778. (let* ((completion-ignore-case t)
  9779. (prop (org-ido-completing-read
  9780. "Globally remove property: "
  9781. (mapcar 'list (org-buffer-property-keys)))))
  9782. (list prop)))
  9783. (save-excursion
  9784. (save-restriction
  9785. (widen)
  9786. (goto-char (point-min))
  9787. (let ((cnt 0))
  9788. (while (re-search-forward
  9789. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9790. nil t)
  9791. (setq cnt (1+ cnt))
  9792. (replace-match ""))
  9793. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9794. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9795. (defun org-compute-property-at-point ()
  9796. "Compute the property at point.
  9797. This looks for an enclosing column format, extracts the operator and
  9798. then applies it to the property in the column format's scope."
  9799. (interactive)
  9800. (unless (org-at-property-p)
  9801. (error "Not at a property"))
  9802. (let ((prop (org-match-string-no-properties 2)))
  9803. (org-columns-get-format-and-top-level)
  9804. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9805. (error "No operator defined for property %s" prop))
  9806. (org-columns-compute prop)))
  9807. (defun org-property-get-allowed-values (pom property &optional table)
  9808. "Get allowed values for the property PROPERTY.
  9809. When TABLE is non-nil, return an alist that can directly be used for
  9810. completion."
  9811. (let (vals)
  9812. (cond
  9813. ((equal property "TODO")
  9814. (setq vals (org-with-point-at pom
  9815. (append org-todo-keywords-1 '("")))))
  9816. ((equal property "PRIORITY")
  9817. (let ((n org-lowest-priority))
  9818. (while (>= n org-highest-priority)
  9819. (push (char-to-string n) vals)
  9820. (setq n (1- n)))))
  9821. ((member property org-special-properties))
  9822. (t
  9823. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9824. (when (and vals (string-match "\\S-" vals))
  9825. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9826. (setq vals (mapcar (lambda (x)
  9827. (cond ((stringp x) x)
  9828. ((numberp x) (number-to-string x))
  9829. ((symbolp x) (symbol-name x))
  9830. (t "???")))
  9831. vals)))))
  9832. (if table (mapcar 'list vals) vals)))
  9833. (defun org-property-previous-allowed-value (&optional previous)
  9834. "Switch to the next allowed value for this property."
  9835. (interactive)
  9836. (org-property-next-allowed-value t))
  9837. (defun org-property-next-allowed-value (&optional previous)
  9838. "Switch to the next allowed value for this property."
  9839. (interactive)
  9840. (unless (org-at-property-p)
  9841. (error "Not at a property"))
  9842. (let* ((key (match-string 2))
  9843. (value (match-string 3))
  9844. (allowed (or (org-property-get-allowed-values (point) key)
  9845. (and (member value '("[ ]" "[-]" "[X]"))
  9846. '("[ ]" "[X]"))))
  9847. nval)
  9848. (unless allowed
  9849. (error "Allowed values for this property have not been defined"))
  9850. (if previous (setq allowed (reverse allowed)))
  9851. (if (member value allowed)
  9852. (setq nval (car (cdr (member value allowed)))))
  9853. (setq nval (or nval (car allowed)))
  9854. (if (equal nval value)
  9855. (error "Only one allowed value for this property"))
  9856. (org-at-property-p)
  9857. (replace-match (concat " :" key ": " nval) t t)
  9858. (org-indent-line-function)
  9859. (beginning-of-line 1)
  9860. (skip-chars-forward " \t")))
  9861. (defun org-find-entry-with-id (ident)
  9862. "Locate the entry that contains the ID property with exact value IDENT.
  9863. IDENT can be a string, a symbol or a number, this function will search for
  9864. the string representation of it.
  9865. Return the position where this entry starts, or nil if there is no such entry."
  9866. (interactive "sID: ")
  9867. (let ((id (cond
  9868. ((stringp ident) ident)
  9869. ((symbol-name ident) (symbol-name ident))
  9870. ((numberp ident) (number-to-string ident))
  9871. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9872. (case-fold-search nil))
  9873. (save-excursion
  9874. (save-restriction
  9875. (widen)
  9876. (goto-char (point-min))
  9877. (when (re-search-forward
  9878. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9879. nil t)
  9880. (org-back-to-heading)
  9881. (point))))))
  9882. ;;;; Timestamps
  9883. (defvar org-last-changed-timestamp nil)
  9884. (defvar org-last-inserted-timestamp nil
  9885. "The last time stamp inserted with `org-insert-time-stamp'.")
  9886. (defvar org-time-was-given) ; dynamically scoped parameter
  9887. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9888. (defvar org-ts-what) ; dynamically scoped parameter
  9889. (defun org-time-stamp (arg &optional inactive)
  9890. "Prompt for a date/time and insert a time stamp.
  9891. If the user specifies a time like HH:MM, or if this command is called
  9892. with a prefix argument, the time stamp will contain date and time.
  9893. Otherwise, only the date will be included. All parts of a date not
  9894. specified by the user will be filled in from the current date/time.
  9895. So if you press just return without typing anything, the time stamp
  9896. will represent the current date/time. If there is already a timestamp
  9897. at the cursor, it will be modified."
  9898. (interactive "P")
  9899. (let* ((ts nil)
  9900. (default-time
  9901. ;; Default time is either today, or, when entering a range,
  9902. ;; the range start.
  9903. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9904. (save-excursion
  9905. (re-search-backward
  9906. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9907. (- (point) 20) t)))
  9908. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9909. (current-time)))
  9910. (default-input (and ts (org-get-compact-tod ts)))
  9911. org-time-was-given org-end-time-was-given time)
  9912. (cond
  9913. ((and (org-at-timestamp-p t)
  9914. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9915. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9916. (insert "--")
  9917. (setq time (let ((this-command this-command))
  9918. (org-read-date arg 'totime nil nil
  9919. default-time default-input)))
  9920. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9921. ((org-at-timestamp-p t)
  9922. (setq time (let ((this-command this-command))
  9923. (org-read-date arg 'totime nil nil default-time default-input)))
  9924. (when (org-at-timestamp-p t) ; just to get the match data
  9925. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9926. (replace-match "")
  9927. (setq org-last-changed-timestamp
  9928. (org-insert-time-stamp
  9929. time (or org-time-was-given arg)
  9930. inactive nil nil (list org-end-time-was-given))))
  9931. (message "Timestamp updated"))
  9932. (t
  9933. (setq time (let ((this-command this-command))
  9934. (org-read-date arg 'totime nil nil default-time default-input)))
  9935. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9936. nil nil (list org-end-time-was-given))))))
  9937. ;; FIXME: can we use this for something else, like computing time differences?
  9938. (defun org-get-compact-tod (s)
  9939. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9940. (let* ((t1 (match-string 1 s))
  9941. (h1 (string-to-number (match-string 2 s)))
  9942. (m1 (string-to-number (match-string 3 s)))
  9943. (t2 (and (match-end 4) (match-string 5 s)))
  9944. (h2 (and t2 (string-to-number (match-string 6 s))))
  9945. (m2 (and t2 (string-to-number (match-string 7 s))))
  9946. dh dm)
  9947. (if (not t2)
  9948. t1
  9949. (setq dh (- h2 h1) dm (- m2 m1))
  9950. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9951. (concat t1 "+" (number-to-string dh)
  9952. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9953. (defun org-time-stamp-inactive (&optional arg)
  9954. "Insert an inactive time stamp.
  9955. An inactive time stamp is enclosed in square brackets instead of angle
  9956. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9957. does not link to the calendar and cannot be changed with the S-cursor keys.
  9958. So these are more for recording a certain time/date."
  9959. (interactive "P")
  9960. (org-time-stamp arg 'inactive))
  9961. (defvar org-date-ovl (org-make-overlay 1 1))
  9962. (org-overlay-put org-date-ovl 'face 'org-warning)
  9963. (org-detach-overlay org-date-ovl)
  9964. (defvar org-ans1) ; dynamically scoped parameter
  9965. (defvar org-ans2) ; dynamically scoped parameter
  9966. (defvar org-plain-time-of-day-regexp) ; defined below
  9967. (defvar org-overriding-default-time nil) ; dynamically scoped
  9968. (defvar org-read-date-overlay nil)
  9969. (defvar org-dcst nil) ; dynamically scoped
  9970. (defun org-read-date (&optional with-time to-time from-string prompt
  9971. default-time default-input)
  9972. "Read a date, possibly a time, and make things smooth for the user.
  9973. The prompt will suggest to enter an ISO date, but you can also enter anything
  9974. which will at least partially be understood by `parse-time-string'.
  9975. Unrecognized parts of the date will default to the current day, month, year,
  9976. hour and minute. If this command is called to replace a timestamp at point,
  9977. of to enter the second timestamp of a range, the default time is taken from the
  9978. existing stamp. For example,
  9979. 3-2-5 --> 2003-02-05
  9980. feb 15 --> currentyear-02-15
  9981. sep 12 9 --> 2009-09-12
  9982. 12:45 --> today 12:45
  9983. 22 sept 0:34 --> currentyear-09-22 0:34
  9984. 12 --> currentyear-currentmonth-12
  9985. Fri --> nearest Friday (today or later)
  9986. etc.
  9987. Furthermore you can specify a relative date by giving, as the *first* thing
  9988. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9989. change in days weeks, months, years.
  9990. With a single plus or minus, the date is relative to today. With a double
  9991. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9992. +4d --> four days from today
  9993. +4 --> same as above
  9994. +2w --> two weeks from today
  9995. ++5 --> five days from default date
  9996. The function understands only English month and weekday abbreviations,
  9997. but this can be configured with the variables `parse-time-months' and
  9998. `parse-time-weekdays'.
  9999. While prompting, a calendar is popped up - you can also select the
  10000. date with the mouse (button 1). The calendar shows a period of three
  10001. months. To scroll it to other months, use the keys `>' and `<'.
  10002. If you don't like the calendar, turn it off with
  10003. \(setq org-read-date-popup-calendar nil)
  10004. With optional argument TO-TIME, the date will immediately be converted
  10005. to an internal time.
  10006. With an optional argument WITH-TIME, the prompt will suggest to also
  10007. insert a time. Note that when WITH-TIME is not set, you can still
  10008. enter a time, and this function will inform the calling routine about
  10009. this change. The calling routine may then choose to change the format
  10010. used to insert the time stamp into the buffer to include the time.
  10011. With optional argument FROM-STRING, read from this string instead from
  10012. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10013. the time/date that is used for everything that is not specified by the
  10014. user."
  10015. (require 'parse-time)
  10016. (let* ((org-time-stamp-rounding-minutes
  10017. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10018. (org-dcst org-display-custom-times)
  10019. (ct (org-current-time))
  10020. (def (or org-overriding-default-time default-time ct))
  10021. (defdecode (decode-time def))
  10022. (dummy (progn
  10023. (when (< (nth 2 defdecode) org-extend-today-until)
  10024. (setcar (nthcdr 2 defdecode) -1)
  10025. (setcar (nthcdr 1 defdecode) 59)
  10026. (setq def (apply 'encode-time defdecode)
  10027. defdecode (decode-time def)))))
  10028. (calendar-move-hook nil)
  10029. (calendar-view-diary-initially-flag nil)
  10030. (view-diary-entries-initially nil)
  10031. (calendar-view-holidays-initially-flag nil)
  10032. (view-calendar-holidays-initially nil)
  10033. (timestr (format-time-string
  10034. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10035. (prompt (concat (if prompt (concat prompt " ") "")
  10036. (format "Date+time [%s]: " timestr)))
  10037. ans (org-ans0 "") org-ans1 org-ans2 final)
  10038. (cond
  10039. (from-string (setq ans from-string))
  10040. (org-read-date-popup-calendar
  10041. (save-excursion
  10042. (save-window-excursion
  10043. (calendar)
  10044. (calendar-forward-day (- (time-to-days def)
  10045. (calendar-absolute-from-gregorian
  10046. (calendar-current-date))))
  10047. (org-eval-in-calendar nil t)
  10048. (let* ((old-map (current-local-map))
  10049. (map (copy-keymap calendar-mode-map))
  10050. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10051. (org-defkey map (kbd "RET") 'org-calendar-select)
  10052. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10053. 'org-calendar-select-mouse)
  10054. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10055. 'org-calendar-select-mouse)
  10056. (org-defkey minibuffer-local-map [(meta shift left)]
  10057. (lambda () (interactive)
  10058. (org-eval-in-calendar '(calendar-backward-month 1))))
  10059. (org-defkey minibuffer-local-map [(meta shift right)]
  10060. (lambda () (interactive)
  10061. (org-eval-in-calendar '(calendar-forward-month 1))))
  10062. (org-defkey minibuffer-local-map [(meta shift up)]
  10063. (lambda () (interactive)
  10064. (org-eval-in-calendar '(calendar-backward-year 1))))
  10065. (org-defkey minibuffer-local-map [(meta shift down)]
  10066. (lambda () (interactive)
  10067. (org-eval-in-calendar '(calendar-forward-year 1))))
  10068. (org-defkey minibuffer-local-map [(shift up)]
  10069. (lambda () (interactive)
  10070. (org-eval-in-calendar '(calendar-backward-week 1))))
  10071. (org-defkey minibuffer-local-map [(shift down)]
  10072. (lambda () (interactive)
  10073. (org-eval-in-calendar '(calendar-forward-week 1))))
  10074. (org-defkey minibuffer-local-map [(shift left)]
  10075. (lambda () (interactive)
  10076. (org-eval-in-calendar '(calendar-backward-day 1))))
  10077. (org-defkey minibuffer-local-map [(shift right)]
  10078. (lambda () (interactive)
  10079. (org-eval-in-calendar '(calendar-forward-day 1))))
  10080. (org-defkey minibuffer-local-map ">"
  10081. (lambda () (interactive)
  10082. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10083. (org-defkey minibuffer-local-map "<"
  10084. (lambda () (interactive)
  10085. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10086. (unwind-protect
  10087. (progn
  10088. (use-local-map map)
  10089. (add-hook 'post-command-hook 'org-read-date-display)
  10090. (setq org-ans0 (read-string prompt default-input nil nil))
  10091. ;; org-ans0: from prompt
  10092. ;; org-ans1: from mouse click
  10093. ;; org-ans2: from calendar motion
  10094. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10095. (remove-hook 'post-command-hook 'org-read-date-display)
  10096. (use-local-map old-map)
  10097. (when org-read-date-overlay
  10098. (org-delete-overlay org-read-date-overlay)
  10099. (setq org-read-date-overlay nil)))))))
  10100. (t ; Naked prompt only
  10101. (unwind-protect
  10102. (setq ans (read-string prompt default-input nil timestr))
  10103. (when org-read-date-overlay
  10104. (org-delete-overlay org-read-date-overlay)
  10105. (setq org-read-date-overlay nil)))))
  10106. (setq final (org-read-date-analyze ans def defdecode))
  10107. (if to-time
  10108. (apply 'encode-time final)
  10109. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10110. (format "%04d-%02d-%02d %02d:%02d"
  10111. (nth 5 final) (nth 4 final) (nth 3 final)
  10112. (nth 2 final) (nth 1 final))
  10113. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10114. (defvar def)
  10115. (defvar defdecode)
  10116. (defvar with-time)
  10117. (defun org-read-date-display ()
  10118. "Display the current date prompt interpretation in the minibuffer."
  10119. (when org-read-date-display-live
  10120. (when org-read-date-overlay
  10121. (org-delete-overlay org-read-date-overlay))
  10122. (let ((p (point)))
  10123. (end-of-line 1)
  10124. (while (not (equal (buffer-substring
  10125. (max (point-min) (- (point) 4)) (point))
  10126. " "))
  10127. (insert " "))
  10128. (goto-char p))
  10129. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10130. " " (or org-ans1 org-ans2)))
  10131. (org-end-time-was-given nil)
  10132. (f (org-read-date-analyze ans def defdecode))
  10133. (fmts (if org-dcst
  10134. org-time-stamp-custom-formats
  10135. org-time-stamp-formats))
  10136. (fmt (if (or with-time
  10137. (and (boundp 'org-time-was-given) org-time-was-given))
  10138. (cdr fmts)
  10139. (car fmts)))
  10140. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10141. (when (and org-end-time-was-given
  10142. (string-match org-plain-time-of-day-regexp txt))
  10143. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10144. org-end-time-was-given
  10145. (substring txt (match-end 0)))))
  10146. (setq org-read-date-overlay
  10147. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10148. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10149. (defun org-read-date-analyze (ans def defdecode)
  10150. "Analyse the combined answer of the date prompt."
  10151. ;; FIXME: cleanup and comment
  10152. (let (delta deltan deltaw deltadef year month day
  10153. hour minute second wday pm h2 m2 tl wday1
  10154. iso-year iso-weekday iso-week iso-year iso-date)
  10155. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10156. (setq ans "+0"))
  10157. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10158. (setq ans (replace-match "" t t ans)
  10159. deltan (car delta)
  10160. deltaw (nth 1 delta)
  10161. deltadef (nth 2 delta)))
  10162. ;; Check if there is an iso week date in there
  10163. ;; If yes, sore the info and postpone interpreting it until the rest
  10164. ;; of the parsing is done
  10165. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10166. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10167. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10168. iso-week (string-to-number (match-string 2 ans)))
  10169. (setq ans (replace-match "" t t ans)))
  10170. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10171. (when (string-match
  10172. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10173. (setq year (if (match-end 2)
  10174. (string-to-number (match-string 2 ans))
  10175. (string-to-number (format-time-string "%Y")))
  10176. month (string-to-number (match-string 3 ans))
  10177. day (string-to-number (match-string 4 ans)))
  10178. (if (< year 100) (setq year (+ 2000 year)))
  10179. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10180. t nil ans)))
  10181. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10182. ;; If there is a time with am/pm, and *no* time without it, we convert
  10183. ;; so that matching will be successful.
  10184. (loop for i from 1 to 2 do ; twice, for end time as well
  10185. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10186. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10187. (setq hour (string-to-number (match-string 1 ans))
  10188. minute (if (match-end 3)
  10189. (string-to-number (match-string 3 ans))
  10190. 0)
  10191. pm (equal ?p
  10192. (string-to-char (downcase (match-string 4 ans)))))
  10193. (if (and (= hour 12) (not pm))
  10194. (setq hour 0)
  10195. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10196. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10197. t t ans))))
  10198. ;; Check if a time range is given as a duration
  10199. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10200. (setq hour (string-to-number (match-string 1 ans))
  10201. h2 (+ hour (string-to-number (match-string 3 ans)))
  10202. minute (string-to-number (match-string 2 ans))
  10203. m2 (+ minute (if (match-end 5) (string-to-number
  10204. (match-string 5 ans))0)))
  10205. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10206. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10207. t t ans)))
  10208. ;; Check if there is a time range
  10209. (when (boundp 'org-end-time-was-given)
  10210. (setq org-time-was-given nil)
  10211. (when (and (string-match org-plain-time-of-day-regexp ans)
  10212. (match-end 8))
  10213. (setq org-end-time-was-given (match-string 8 ans))
  10214. (setq ans (concat (substring ans 0 (match-beginning 7))
  10215. (substring ans (match-end 7))))))
  10216. (setq tl (parse-time-string ans)
  10217. day (or (nth 3 tl) (nth 3 defdecode))
  10218. month (or (nth 4 tl)
  10219. (if (and org-read-date-prefer-future
  10220. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10221. (1+ (nth 4 defdecode))
  10222. (nth 4 defdecode)))
  10223. year (or (nth 5 tl)
  10224. (if (and org-read-date-prefer-future
  10225. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10226. (1+ (nth 5 defdecode))
  10227. (nth 5 defdecode)))
  10228. hour (or (nth 2 tl) (nth 2 defdecode))
  10229. minute (or (nth 1 tl) (nth 1 defdecode))
  10230. second (or (nth 0 tl) 0)
  10231. wday (nth 6 tl))
  10232. ;; Special date definitions below
  10233. (cond
  10234. (iso-week
  10235. ;; There was an iso week
  10236. (setq year (or iso-year year)
  10237. day (or iso-weekday wday 1)
  10238. wday nil ; to make sure that the trigger below does not match
  10239. iso-date (calendar-gregorian-from-absolute
  10240. (calendar-absolute-from-iso
  10241. (list iso-week day year))))
  10242. ; FIXME: Should we also push ISO weeks into the future?
  10243. ; (when (and org-read-date-prefer-future
  10244. ; (not iso-year)
  10245. ; (< (calendar-absolute-from-gregorian iso-date)
  10246. ; (time-to-days (current-time))))
  10247. ; (setq year (1+ year)
  10248. ; iso-date (calendar-gregorian-from-absolute
  10249. ; (calendar-absolute-from-iso
  10250. ; (list iso-week day year)))))
  10251. (setq month (car iso-date)
  10252. year (nth 2 iso-date)
  10253. day (nth 1 iso-date)))
  10254. (deltan
  10255. (unless deltadef
  10256. (let ((now (decode-time (current-time))))
  10257. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10258. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10259. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10260. ((equal deltaw "m") (setq month (+ month deltan)))
  10261. ((equal deltaw "y") (setq year (+ year deltan)))))
  10262. ((and wday (not (nth 3 tl)))
  10263. ;; Weekday was given, but no day, so pick that day in the week
  10264. ;; on or after the derived date.
  10265. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10266. (unless (equal wday wday1)
  10267. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10268. (if (and (boundp 'org-time-was-given)
  10269. (nth 2 tl))
  10270. (setq org-time-was-given t))
  10271. (if (< year 100) (setq year (+ 2000 year)))
  10272. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10273. (list second minute hour day month year)))
  10274. (defvar parse-time-weekdays)
  10275. (defun org-read-date-get-relative (s today default)
  10276. "Check string S for special relative date string.
  10277. TODAY and DEFAULT are internal times, for today and for a default.
  10278. Return shift list (N what def-flag)
  10279. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10280. N is the number of WHATs to shift.
  10281. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10282. the DEFAULT date rather than TODAY."
  10283. (when (and
  10284. (string-match
  10285. (concat
  10286. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10287. "\\([0-9]+\\)?"
  10288. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10289. "\\([ \t]\\|$\\)") s)
  10290. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10291. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10292. (string-to-char (substring (match-string 1 s) -1))
  10293. ?+))
  10294. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10295. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10296. (what (if (match-end 3) (match-string 3 s) "d"))
  10297. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10298. (date (if rel default today))
  10299. (wday (nth 6 (decode-time date)))
  10300. delta)
  10301. (if wday1
  10302. (progn
  10303. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10304. (if (= dir ?-) (setq delta (- delta 7)))
  10305. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10306. (list delta "d" rel))
  10307. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10308. (defun org-eval-in-calendar (form &optional keepdate)
  10309. "Eval FORM in the calendar window and return to current window.
  10310. Also, store the cursor date in variable org-ans2."
  10311. (let ((sw (selected-window)))
  10312. (select-window (get-buffer-window "*Calendar*"))
  10313. (eval form)
  10314. (when (and (not keepdate) (calendar-cursor-to-date))
  10315. (let* ((date (calendar-cursor-to-date))
  10316. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10317. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10318. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10319. (select-window sw)))
  10320. (defun org-calendar-select ()
  10321. "Return to `org-read-date' with the date currently selected.
  10322. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10323. (interactive)
  10324. (when (calendar-cursor-to-date)
  10325. (let* ((date (calendar-cursor-to-date))
  10326. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10327. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10328. (if (active-minibuffer-window) (exit-minibuffer))))
  10329. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10330. "Insert a date stamp for the date given by the internal TIME.
  10331. WITH-HM means, use the stamp format that includes the time of the day.
  10332. INACTIVE means use square brackets instead of angular ones, so that the
  10333. stamp will not contribute to the agenda.
  10334. PRE and POST are optional strings to be inserted before and after the
  10335. stamp.
  10336. The command returns the inserted time stamp."
  10337. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10338. stamp)
  10339. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10340. (insert-before-markers (or pre ""))
  10341. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10342. (when (listp extra)
  10343. (setq extra (car extra))
  10344. (if (and (stringp extra)
  10345. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10346. (setq extra (format "-%02d:%02d"
  10347. (string-to-number (match-string 1 extra))
  10348. (string-to-number (match-string 2 extra))))
  10349. (setq extra nil)))
  10350. (when extra
  10351. (backward-char 1)
  10352. (insert-before-markers extra)
  10353. (forward-char 1))
  10354. (insert-before-markers (or post ""))
  10355. (setq org-last-inserted-timestamp stamp)))
  10356. (defun org-toggle-time-stamp-overlays ()
  10357. "Toggle the use of custom time stamp formats."
  10358. (interactive)
  10359. (setq org-display-custom-times (not org-display-custom-times))
  10360. (unless org-display-custom-times
  10361. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10362. (while (setq p (next-single-property-change p 'display))
  10363. (if (and (get-text-property p 'display)
  10364. (eq (get-text-property p 'face) 'org-date))
  10365. (remove-text-properties
  10366. p (setq p (next-single-property-change p 'display))
  10367. '(display t))))
  10368. (set-buffer-modified-p bmp)))
  10369. (if (featurep 'xemacs)
  10370. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10371. (org-restart-font-lock)
  10372. (setq org-table-may-need-update t)
  10373. (if org-display-custom-times
  10374. (message "Time stamps are overlayed with custom format")
  10375. (message "Time stamp overlays removed")))
  10376. (defun org-display-custom-time (beg end)
  10377. "Overlay modified time stamp format over timestamp between BEG and END."
  10378. (let* ((ts (buffer-substring beg end))
  10379. t1 w1 with-hm tf time str w2 (off 0))
  10380. (save-match-data
  10381. (setq t1 (org-parse-time-string ts t))
  10382. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10383. (setq off (- (match-end 0) (match-beginning 0)))))
  10384. (setq end (- end off))
  10385. (setq w1 (- end beg)
  10386. with-hm (and (nth 1 t1) (nth 2 t1))
  10387. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10388. time (org-fix-decoded-time t1)
  10389. str (org-add-props
  10390. (format-time-string
  10391. (substring tf 1 -1) (apply 'encode-time time))
  10392. nil 'mouse-face 'highlight)
  10393. w2 (length str))
  10394. (if (not (= w2 w1))
  10395. (add-text-properties (1+ beg) (+ 2 beg)
  10396. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10397. (if (featurep 'xemacs)
  10398. (progn
  10399. (put-text-property beg end 'invisible t)
  10400. (put-text-property beg end 'end-glyph (make-glyph str)))
  10401. (put-text-property beg end 'display str))))
  10402. (defun org-translate-time (string)
  10403. "Translate all timestamps in STRING to custom format.
  10404. But do this only if the variable `org-display-custom-times' is set."
  10405. (when org-display-custom-times
  10406. (save-match-data
  10407. (let* ((start 0)
  10408. (re org-ts-regexp-both)
  10409. t1 with-hm inactive tf time str beg end)
  10410. (while (setq start (string-match re string start))
  10411. (setq beg (match-beginning 0)
  10412. end (match-end 0)
  10413. t1 (save-match-data
  10414. (org-parse-time-string (substring string beg end) t))
  10415. with-hm (and (nth 1 t1) (nth 2 t1))
  10416. inactive (equal (substring string beg (1+ beg)) "[")
  10417. tf (funcall (if with-hm 'cdr 'car)
  10418. org-time-stamp-custom-formats)
  10419. time (org-fix-decoded-time t1)
  10420. str (format-time-string
  10421. (concat
  10422. (if inactive "[" "<") (substring tf 1 -1)
  10423. (if inactive "]" ">"))
  10424. (apply 'encode-time time))
  10425. string (replace-match str t t string)
  10426. start (+ start (length str)))))))
  10427. string)
  10428. (defun org-fix-decoded-time (time)
  10429. "Set 0 instead of nil for the first 6 elements of time.
  10430. Don't touch the rest."
  10431. (let ((n 0))
  10432. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10433. (defun org-days-to-time (timestamp-string)
  10434. "Difference between TIMESTAMP-STRING and now in days."
  10435. (- (time-to-days (org-time-string-to-time timestamp-string))
  10436. (time-to-days (current-time))))
  10437. (defun org-deadline-close (timestamp-string &optional ndays)
  10438. "Is the time in TIMESTAMP-STRING close to the current date?"
  10439. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10440. (and (< (org-days-to-time timestamp-string) ndays)
  10441. (not (org-entry-is-done-p))))
  10442. (defun org-get-wdays (ts)
  10443. "Get the deadline lead time appropriate for timestring TS."
  10444. (cond
  10445. ((<= org-deadline-warning-days 0)
  10446. ;; 0 or negative, enforce this value no matter what
  10447. (- org-deadline-warning-days))
  10448. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10449. ;; lead time is specified.
  10450. (floor (* (string-to-number (match-string 1 ts))
  10451. (cdr (assoc (match-string 2 ts)
  10452. '(("d" . 1) ("w" . 7)
  10453. ("m" . 30.4) ("y" . 365.25)))))))
  10454. ;; go for the default.
  10455. (t org-deadline-warning-days)))
  10456. (defun org-calendar-select-mouse (ev)
  10457. "Return to `org-read-date' with the date currently selected.
  10458. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10459. (interactive "e")
  10460. (mouse-set-point ev)
  10461. (when (calendar-cursor-to-date)
  10462. (let* ((date (calendar-cursor-to-date))
  10463. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10464. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10465. (if (active-minibuffer-window) (exit-minibuffer))))
  10466. (defun org-check-deadlines (ndays)
  10467. "Check if there are any deadlines due or past due.
  10468. A deadline is considered due if it happens within `org-deadline-warning-days'
  10469. days from today's date. If the deadline appears in an entry marked DONE,
  10470. it is not shown. The prefix arg NDAYS can be used to test that many
  10471. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10472. (interactive "P")
  10473. (let* ((org-warn-days
  10474. (cond
  10475. ((equal ndays '(4)) 100000)
  10476. (ndays (prefix-numeric-value ndays))
  10477. (t (abs org-deadline-warning-days))))
  10478. (case-fold-search nil)
  10479. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10480. (callback
  10481. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10482. (message "%d deadlines past-due or due within %d days"
  10483. (org-occur regexp nil callback)
  10484. org-warn-days)))
  10485. (defun org-check-before-date (date)
  10486. "Check if there are deadlines or scheduled entries before DATE."
  10487. (interactive (list (org-read-date)))
  10488. (let ((case-fold-search nil)
  10489. (regexp (concat "\\<\\(" org-deadline-string
  10490. "\\|" org-scheduled-string
  10491. "\\) *<\\([^>]+\\)>"))
  10492. (callback
  10493. (lambda () (time-less-p
  10494. (org-time-string-to-time (match-string 2))
  10495. (org-time-string-to-time date)))))
  10496. (message "%d entries before %s"
  10497. (org-occur regexp nil callback) date)))
  10498. (defun org-evaluate-time-range (&optional to-buffer)
  10499. "Evaluate a time range by computing the difference between start and end.
  10500. Normally the result is just printed in the echo area, but with prefix arg
  10501. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10502. If the time range is actually in a table, the result is inserted into the
  10503. next column.
  10504. For time difference computation, a year is assumed to be exactly 365
  10505. days in order to avoid rounding problems."
  10506. (interactive "P")
  10507. (or
  10508. (org-clock-update-time-maybe)
  10509. (save-excursion
  10510. (unless (org-at-date-range-p t)
  10511. (goto-char (point-at-bol))
  10512. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10513. (if (not (org-at-date-range-p t))
  10514. (error "Not at a time-stamp range, and none found in current line")))
  10515. (let* ((ts1 (match-string 1))
  10516. (ts2 (match-string 2))
  10517. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10518. (match-end (match-end 0))
  10519. (time1 (org-time-string-to-time ts1))
  10520. (time2 (org-time-string-to-time ts2))
  10521. (t1 (time-to-seconds time1))
  10522. (t2 (time-to-seconds time2))
  10523. (diff (abs (- t2 t1)))
  10524. (negative (< (- t2 t1) 0))
  10525. ;; (ys (floor (* 365 24 60 60)))
  10526. (ds (* 24 60 60))
  10527. (hs (* 60 60))
  10528. (fy "%dy %dd %02d:%02d")
  10529. (fy1 "%dy %dd")
  10530. (fd "%dd %02d:%02d")
  10531. (fd1 "%dd")
  10532. (fh "%02d:%02d")
  10533. y d h m align)
  10534. (if havetime
  10535. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10536. y 0
  10537. d (floor (/ diff ds)) diff (mod diff ds)
  10538. h (floor (/ diff hs)) diff (mod diff hs)
  10539. m (floor (/ diff 60)))
  10540. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10541. y 0
  10542. d (floor (+ (/ diff ds) 0.5))
  10543. h 0 m 0))
  10544. (if (not to-buffer)
  10545. (message "%s" (org-make-tdiff-string y d h m))
  10546. (if (org-at-table-p)
  10547. (progn
  10548. (goto-char match-end)
  10549. (setq align t)
  10550. (and (looking-at " *|") (goto-char (match-end 0))))
  10551. (goto-char match-end))
  10552. (if (looking-at
  10553. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10554. (replace-match ""))
  10555. (if negative (insert " -"))
  10556. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10557. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10558. (insert " " (format fh h m))))
  10559. (if align (org-table-align))
  10560. (message "Time difference inserted")))))
  10561. (defun org-make-tdiff-string (y d h m)
  10562. (let ((fmt "")
  10563. (l nil))
  10564. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10565. l (push y l)))
  10566. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10567. l (push d l)))
  10568. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10569. l (push h l)))
  10570. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10571. l (push m l)))
  10572. (apply 'format fmt (nreverse l))))
  10573. (defun org-time-string-to-time (s)
  10574. (apply 'encode-time (org-parse-time-string s)))
  10575. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10576. "Convert a time stamp to an absolute day number.
  10577. If there is a specifyer for a cyclic time stamp, get the closest date to
  10578. DAYNR.
  10579. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10580. (cond
  10581. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10582. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10583. daynr
  10584. (+ daynr 1000)))
  10585. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10586. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10587. (time-to-days (current-time))) (match-string 0 s)
  10588. prefer show-all))
  10589. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10590. (defun org-days-to-iso-week (days)
  10591. "Return the iso week number."
  10592. (require 'cal-iso)
  10593. (car (calendar-iso-from-absolute days)))
  10594. (defun org-small-year-to-year (year)
  10595. "Convert 2-digit years into 4-digit years.
  10596. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10597. The year 2000 cannot be abbreviated. Any year larger than 99
  10598. is returned unchanged."
  10599. (if (< year 38)
  10600. (setq year (+ 2000 year))
  10601. (if (< year 100)
  10602. (setq year (+ 1900 year))))
  10603. year)
  10604. (defun org-time-from-absolute (d)
  10605. "Return the time corresponding to date D.
  10606. D may be an absolute day number, or a calendar-type list (month day year)."
  10607. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10608. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10609. (defun org-calendar-holiday ()
  10610. "List of holidays, for Diary display in Org-mode."
  10611. (require 'holidays)
  10612. (let ((hl (funcall
  10613. (if (fboundp 'calendar-check-holidays)
  10614. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10615. (if hl (mapconcat 'identity hl "; "))))
  10616. (defun org-diary-sexp-entry (sexp entry date)
  10617. "Process a SEXP diary ENTRY for DATE."
  10618. (require 'diary-lib)
  10619. (let ((result (if calendar-debug-sexp
  10620. (let ((stack-trace-on-error t))
  10621. (eval (car (read-from-string sexp))))
  10622. (condition-case nil
  10623. (eval (car (read-from-string sexp)))
  10624. (error
  10625. (beep)
  10626. (message "Bad sexp at line %d in %s: %s"
  10627. (org-current-line)
  10628. (buffer-file-name) sexp)
  10629. (sleep-for 2))))))
  10630. (cond ((stringp result) result)
  10631. ((and (consp result)
  10632. (stringp (cdr result))) (cdr result))
  10633. (result entry)
  10634. (t nil))))
  10635. (defun org-diary-to-ical-string (frombuf)
  10636. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10637. This uses the icalendar.el library."
  10638. (let* ((tmpdir (if (featurep 'xemacs)
  10639. (temp-directory)
  10640. temporary-file-directory))
  10641. (tmpfile (make-temp-name
  10642. (expand-file-name "orgics" tmpdir)))
  10643. buf rtn b e)
  10644. (save-excursion
  10645. (set-buffer frombuf)
  10646. (icalendar-export-region (point-min) (point-max) tmpfile)
  10647. (setq buf (find-buffer-visiting tmpfile))
  10648. (set-buffer buf)
  10649. (goto-char (point-min))
  10650. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10651. (setq b (match-beginning 0)))
  10652. (goto-char (point-max))
  10653. (if (re-search-backward "^END:VEVENT" nil t)
  10654. (setq e (match-end 0)))
  10655. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10656. (kill-buffer buf)
  10657. (delete-file tmpfile)
  10658. rtn))
  10659. (defun org-closest-date (start current change prefer show-all)
  10660. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10661. When PREFER is `past' return a date that is either CURRENT or past.
  10662. When PREFER is `future', return a date that is either CURRENT or future.
  10663. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10664. ;; Make the proper lists from the dates
  10665. (catch 'exit
  10666. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10667. dn dw sday cday n1 n2 n0
  10668. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10669. (setq start (org-date-to-gregorian start)
  10670. current (org-date-to-gregorian
  10671. (if show-all
  10672. current
  10673. (time-to-days (current-time))))
  10674. sday (calendar-absolute-from-gregorian start)
  10675. cday (calendar-absolute-from-gregorian current))
  10676. (if (<= cday sday) (throw 'exit sday))
  10677. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10678. (setq dn (string-to-number (match-string 1 change))
  10679. dw (cdr (assoc (match-string 2 change) a1)))
  10680. (error "Invalid change specifyer: %s" change))
  10681. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10682. (cond
  10683. ((eq dw 'day)
  10684. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10685. n2 (+ n1 dn)))
  10686. ((eq dw 'year)
  10687. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10688. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10689. (setq date1 (list m d y1)
  10690. n1 (calendar-absolute-from-gregorian date1)
  10691. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10692. n2 (calendar-absolute-from-gregorian date2)))
  10693. ((eq dw 'month)
  10694. ;; approx number of month between the two dates
  10695. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10696. ;; How often does dn fit in there?
  10697. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10698. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10699. m (+ m nm)
  10700. ny (floor (/ m 12))
  10701. y (+ y ny)
  10702. m (- m (* ny 12)))
  10703. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10704. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10705. (setq m2 (+ m dn) y2 y)
  10706. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10707. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10708. (while (<= n2 cday)
  10709. (setq n1 n2 m m2 y y2)
  10710. (setq m2 (+ m dn) y2 y)
  10711. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10712. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10713. ;; Make sure n1 is the earlier date
  10714. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  10715. (if show-all
  10716. (cond
  10717. ((eq prefer 'past) n1)
  10718. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10719. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10720. (cond
  10721. ((eq prefer 'past) n1)
  10722. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10723. (t (if (= cday n1) n1 n2)))))))
  10724. (defun org-date-to-gregorian (date)
  10725. "Turn any specification of DATE into a gregorian date for the calendar."
  10726. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10727. ((and (listp date) (= (length date) 3)) date)
  10728. ((stringp date)
  10729. (setq date (org-parse-time-string date))
  10730. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10731. ((listp date)
  10732. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10733. (defun org-parse-time-string (s &optional nodefault)
  10734. "Parse the standard Org-mode time string.
  10735. This should be a lot faster than the normal `parse-time-string'.
  10736. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10737. hour and minute fields will be nil if not given."
  10738. (if (string-match org-ts-regexp0 s)
  10739. (list 0
  10740. (if (or (match-beginning 8) (not nodefault))
  10741. (string-to-number (or (match-string 8 s) "0")))
  10742. (if (or (match-beginning 7) (not nodefault))
  10743. (string-to-number (or (match-string 7 s) "0")))
  10744. (string-to-number (match-string 4 s))
  10745. (string-to-number (match-string 3 s))
  10746. (string-to-number (match-string 2 s))
  10747. nil nil nil)
  10748. (make-list 9 0)))
  10749. (defun org-timestamp-up (&optional arg)
  10750. "Increase the date item at the cursor by one.
  10751. If the cursor is on the year, change the year. If it is on the month or
  10752. the day, change that.
  10753. With prefix ARG, change by that many units."
  10754. (interactive "p")
  10755. (org-timestamp-change (prefix-numeric-value arg)))
  10756. (defun org-timestamp-down (&optional arg)
  10757. "Decrease the date item at the cursor by one.
  10758. If the cursor is on the year, change the year. If it is on the month or
  10759. the day, change that.
  10760. With prefix ARG, change by that many units."
  10761. (interactive "p")
  10762. (org-timestamp-change (- (prefix-numeric-value arg))))
  10763. (defun org-timestamp-up-day (&optional arg)
  10764. "Increase the date in the time stamp by one day.
  10765. With prefix ARG, change that many days."
  10766. (interactive "p")
  10767. (if (and (not (org-at-timestamp-p t))
  10768. (org-on-heading-p))
  10769. (org-todo 'up)
  10770. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10771. (defun org-timestamp-down-day (&optional arg)
  10772. "Decrease the date in the time stamp by one day.
  10773. With prefix ARG, change that many days."
  10774. (interactive "p")
  10775. (if (and (not (org-at-timestamp-p t))
  10776. (org-on-heading-p))
  10777. (org-todo 'down)
  10778. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10779. (defun org-at-timestamp-p (&optional inactive-ok)
  10780. "Determine if the cursor is in or at a timestamp."
  10781. (interactive)
  10782. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10783. (pos (point))
  10784. (ans (or (looking-at tsr)
  10785. (save-excursion
  10786. (skip-chars-backward "^[<\n\r\t")
  10787. (if (> (point) (point-min)) (backward-char 1))
  10788. (and (looking-at tsr)
  10789. (> (- (match-end 0) pos) -1))))))
  10790. (and ans
  10791. (boundp 'org-ts-what)
  10792. (setq org-ts-what
  10793. (cond
  10794. ((= pos (match-beginning 0)) 'bracket)
  10795. ((= pos (1- (match-end 0))) 'bracket)
  10796. ((org-pos-in-match-range pos 2) 'year)
  10797. ((org-pos-in-match-range pos 3) 'month)
  10798. ((org-pos-in-match-range pos 7) 'hour)
  10799. ((org-pos-in-match-range pos 8) 'minute)
  10800. ((or (org-pos-in-match-range pos 4)
  10801. (org-pos-in-match-range pos 5)) 'day)
  10802. ((and (> pos (or (match-end 8) (match-end 5)))
  10803. (< pos (match-end 0)))
  10804. (- pos (or (match-end 8) (match-end 5))))
  10805. (t 'day))))
  10806. ans))
  10807. (defun org-toggle-timestamp-type ()
  10808. "Toggle the type (<active> or [inactive]) of a time stamp."
  10809. (interactive)
  10810. (when (org-at-timestamp-p t)
  10811. (let ((beg (match-beginning 0)) (end (match-end 0))
  10812. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10813. (save-excursion
  10814. (goto-char beg)
  10815. (while (re-search-forward "[][<>]" end t)
  10816. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10817. t t)))
  10818. (message "Timestamp is now %sactive"
  10819. (if (equal (char-after beg) ?<) "" "in")))))
  10820. (defun org-timestamp-change (n &optional what)
  10821. "Change the date in the time stamp at point.
  10822. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10823. `year', `minute', `second'. If WHAT is not given, the cursor position
  10824. in the timestamp determines what will be changed."
  10825. (let ((pos (point))
  10826. with-hm inactive
  10827. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10828. org-ts-what
  10829. extra rem
  10830. ts time time0)
  10831. (if (not (org-at-timestamp-p t))
  10832. (error "Not at a timestamp"))
  10833. (if (and (not what) (eq org-ts-what 'bracket))
  10834. (org-toggle-timestamp-type)
  10835. (if (and (not what) (not (eq org-ts-what 'day))
  10836. org-display-custom-times
  10837. (get-text-property (point) 'display)
  10838. (not (get-text-property (1- (point)) 'display)))
  10839. (setq org-ts-what 'day))
  10840. (setq org-ts-what (or what org-ts-what)
  10841. inactive (= (char-after (match-beginning 0)) ?\[)
  10842. ts (match-string 0))
  10843. (replace-match "")
  10844. (if (string-match
  10845. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10846. ts)
  10847. (setq extra (match-string 1 ts)))
  10848. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10849. (setq with-hm t))
  10850. (setq time0 (org-parse-time-string ts))
  10851. (when (and (eq org-ts-what 'minute)
  10852. (eq current-prefix-arg nil))
  10853. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10854. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10855. (setcar (cdr time0) (+ (nth 1 time0)
  10856. (if (> n 0) (- rem) (- dm rem))))))
  10857. (setq time
  10858. (encode-time (or (car time0) 0)
  10859. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10860. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10861. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10862. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10863. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10864. (nthcdr 6 time0)))
  10865. (when (integerp org-ts-what)
  10866. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10867. (if (eq what 'calendar)
  10868. (let ((cal-date (org-get-date-from-calendar)))
  10869. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10870. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10871. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10872. (setcar time0 (or (car time0) 0))
  10873. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10874. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10875. (setq time (apply 'encode-time time0))))
  10876. (setq org-last-changed-timestamp
  10877. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10878. (org-clock-update-time-maybe)
  10879. (goto-char pos)
  10880. ;; Try to recenter the calendar window, if any
  10881. (if (and org-calendar-follow-timestamp-change
  10882. (get-buffer-window "*Calendar*" t)
  10883. (memq org-ts-what '(day month year)))
  10884. (org-recenter-calendar (time-to-days time))))))
  10885. (defun org-modify-ts-extra (s pos n dm)
  10886. "Change the different parts of the lead-time and repeat fields in timestamp."
  10887. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10888. ng h m new rem)
  10889. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10890. (cond
  10891. ((or (org-pos-in-match-range pos 2)
  10892. (org-pos-in-match-range pos 3))
  10893. (setq m (string-to-number (match-string 3 s))
  10894. h (string-to-number (match-string 2 s)))
  10895. (if (org-pos-in-match-range pos 2)
  10896. (setq h (+ h n))
  10897. (setq n (* dm (org-no-warnings (signum n))))
  10898. (when (not (= 0 (setq rem (% m dm))))
  10899. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10900. (setq m (+ m n)))
  10901. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10902. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10903. (setq h (min 24 (max 0 h)))
  10904. (setq ng 1 new (format "-%02d:%02d" h m)))
  10905. ((org-pos-in-match-range pos 6)
  10906. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10907. ((org-pos-in-match-range pos 5)
  10908. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10909. ((org-pos-in-match-range pos 9)
  10910. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10911. ((org-pos-in-match-range pos 8)
  10912. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10913. (when ng
  10914. (setq s (concat
  10915. (substring s 0 (match-beginning ng))
  10916. new
  10917. (substring s (match-end ng))))))
  10918. s))
  10919. (defun org-recenter-calendar (date)
  10920. "If the calendar is visible, recenter it to DATE."
  10921. (let* ((win (selected-window))
  10922. (cwin (get-buffer-window "*Calendar*" t))
  10923. (calendar-move-hook nil))
  10924. (when cwin
  10925. (select-window cwin)
  10926. (calendar-goto-date (if (listp date) date
  10927. (calendar-gregorian-from-absolute date)))
  10928. (select-window win))))
  10929. (defun org-goto-calendar (&optional arg)
  10930. "Go to the Emacs calendar at the current date.
  10931. If there is a time stamp in the current line, go to that date.
  10932. A prefix ARG can be used to force the current date."
  10933. (interactive "P")
  10934. (let ((tsr org-ts-regexp) diff
  10935. (calendar-move-hook nil)
  10936. (calendar-view-holidays-initially-flag nil)
  10937. (view-calendar-holidays-initially nil)
  10938. (calendar-view-diary-initially-flag nil)
  10939. (view-diary-entries-initially nil))
  10940. (if (or (org-at-timestamp-p)
  10941. (save-excursion
  10942. (beginning-of-line 1)
  10943. (looking-at (concat ".*" tsr))))
  10944. (let ((d1 (time-to-days (current-time)))
  10945. (d2 (time-to-days
  10946. (org-time-string-to-time (match-string 1)))))
  10947. (setq diff (- d2 d1))))
  10948. (calendar)
  10949. (calendar-goto-today)
  10950. (if (and diff (not arg)) (calendar-forward-day diff))))
  10951. (defun org-get-date-from-calendar ()
  10952. "Return a list (month day year) of date at point in calendar."
  10953. (with-current-buffer "*Calendar*"
  10954. (save-match-data
  10955. (calendar-cursor-to-date))))
  10956. (defun org-date-from-calendar ()
  10957. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10958. If there is already a time stamp at the cursor position, update it."
  10959. (interactive)
  10960. (if (org-at-timestamp-p t)
  10961. (org-timestamp-change 0 'calendar)
  10962. (let ((cal-date (org-get-date-from-calendar)))
  10963. (org-insert-time-stamp
  10964. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10965. (defun org-minutes-to-hh:mm-string (m)
  10966. "Compute H:MM from a number of minutes."
  10967. (let ((h (/ m 60)))
  10968. (setq m (- m (* 60 h)))
  10969. (format org-time-clocksum-format h m)))
  10970. (defun org-hh:mm-string-to-minutes (s)
  10971. "Convert a string H:MM to a number of minutes."
  10972. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10973. (+ (* (string-to-number (match-string 1 s)) 60)
  10974. (string-to-number (match-string 2 s)))
  10975. 0))
  10976. ;;;; Agenda files
  10977. ;;;###autoload
  10978. (defun org-iswitchb (&optional arg)
  10979. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10980. With a prefix argument, restrict available to files.
  10981. With two prefix arguments, restrict available buffers to agenda files.
  10982. Due to some yet unresolved reason, the global function
  10983. `iswitchb-mode' needs to be active for this function to work."
  10984. (interactive "P")
  10985. (require 'iswitchb)
  10986. (let ((enabled iswitchb-mode) blist)
  10987. (or enabled (iswitchb-mode 1))
  10988. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10989. ((equal arg '(16)) (org-buffer-list 'agenda))
  10990. (t (org-buffer-list))))
  10991. (unwind-protect
  10992. (let ((iswitchb-make-buflist-hook
  10993. (lambda ()
  10994. (setq iswitchb-temp-buflist
  10995. (mapcar 'buffer-name blist)))))
  10996. (switch-to-buffer
  10997. (iswitchb-read-buffer
  10998. "Switch-to: " nil t))
  10999. (or enabled (iswitchb-mode -1))))))
  11000. ;;;###autoload
  11001. (defun org-ido-switchb (&optional arg)
  11002. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11003. With a prefix argument, restrict available to files.
  11004. With two prefix arguments, restrict available buffers to agenda files."
  11005. (interactive "P")
  11006. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11007. ((equal arg '(16)) (org-buffer-list 'agenda))
  11008. (t (org-buffer-list)))))
  11009. (switch-to-buffer
  11010. (org-ido-completing-read "Org buffer: "
  11011. (mapcar 'buffer-name blist)
  11012. nil t))))
  11013. (defun org-buffer-list (&optional predicate exclude-tmp)
  11014. "Return a list of Org buffers.
  11015. PREDICATE can be `export', `files' or `agenda'.
  11016. export restrict the list to Export buffers.
  11017. files restrict the list to buffers visiting Org files.
  11018. agenda restrict the list to buffers visiting agenda files.
  11019. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11020. (let* ((bfn nil)
  11021. (agenda-files (and (eq predicate 'agenda)
  11022. (mapcar 'file-truename (org-agenda-files t))))
  11023. (filter
  11024. (cond
  11025. ((eq predicate 'files)
  11026. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11027. ((eq predicate 'export)
  11028. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11029. ((eq predicate 'agenda)
  11030. (lambda (b)
  11031. (with-current-buffer b
  11032. (and (eq major-mode 'org-mode)
  11033. (setq bfn (buffer-file-name b))
  11034. (member (file-truename bfn) agenda-files)))))
  11035. (t (lambda (b) (with-current-buffer b
  11036. (or (eq major-mode 'org-mode)
  11037. (string-match "\*Org .*Export"
  11038. (buffer-name b)))))))))
  11039. (delq nil
  11040. (mapcar
  11041. (lambda(b)
  11042. (if (and (funcall filter b)
  11043. (or (not exclude-tmp)
  11044. (not (string-match "tmp" (buffer-name b)))))
  11045. b
  11046. nil))
  11047. (buffer-list)))))
  11048. (defun org-agenda-files (&optional unrestricted archives)
  11049. "Get the list of agenda files.
  11050. Optional UNRESTRICTED means return the full list even if a restriction
  11051. is currently in place.
  11052. When ARCHIVES is t, include all archive files hat are really being
  11053. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11054. `org-agenda-archives-mode' is t."
  11055. (let ((files
  11056. (cond
  11057. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11058. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11059. ((listp org-agenda-files) org-agenda-files)
  11060. (t (error "Invalid value of `org-agenda-files'")))))
  11061. (setq files (apply 'append
  11062. (mapcar (lambda (f)
  11063. (if (file-directory-p f)
  11064. (directory-files
  11065. f t org-agenda-file-regexp)
  11066. (list f)))
  11067. files)))
  11068. (when org-agenda-skip-unavailable-files
  11069. (setq files (delq nil
  11070. (mapcar (function
  11071. (lambda (file)
  11072. (and (file-readable-p file) file)))
  11073. files))))
  11074. (when (or (eq archives t)
  11075. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11076. (setq files (org-add-archive-files files)))
  11077. files))
  11078. (defun org-edit-agenda-file-list ()
  11079. "Edit the list of agenda files.
  11080. Depending on setup, this either uses customize to edit the variable
  11081. `org-agenda-files', or it visits the file that is holding the list. In the
  11082. latter case, the buffer is set up in a way that saving it automatically kills
  11083. the buffer and restores the previous window configuration."
  11084. (interactive)
  11085. (if (stringp org-agenda-files)
  11086. (let ((cw (current-window-configuration)))
  11087. (find-file org-agenda-files)
  11088. (org-set-local 'org-window-configuration cw)
  11089. (org-add-hook 'after-save-hook
  11090. (lambda ()
  11091. (set-window-configuration
  11092. (prog1 org-window-configuration
  11093. (kill-buffer (current-buffer))))
  11094. (org-install-agenda-files-menu)
  11095. (message "New agenda file list installed"))
  11096. nil 'local)
  11097. (message "%s" (substitute-command-keys
  11098. "Edit list and finish with \\[save-buffer]")))
  11099. (customize-variable 'org-agenda-files)))
  11100. (defun org-store-new-agenda-file-list (list)
  11101. "Set new value for the agenda file list and save it correctly."
  11102. (if (stringp org-agenda-files)
  11103. (let ((f org-agenda-files) b)
  11104. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11105. (with-temp-file f
  11106. (insert (mapconcat 'identity list "\n") "\n")))
  11107. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11108. (setq org-agenda-files list)
  11109. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11110. (defun org-read-agenda-file-list ()
  11111. "Read the list of agenda files from a file."
  11112. (when (file-directory-p org-agenda-files)
  11113. (error "`org-agenda-files' cannot be a single directory"))
  11114. (when (stringp org-agenda-files)
  11115. (with-temp-buffer
  11116. (insert-file-contents org-agenda-files)
  11117. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11118. ;;;###autoload
  11119. (defun org-cycle-agenda-files ()
  11120. "Cycle through the files in `org-agenda-files'.
  11121. If the current buffer visits an agenda file, find the next one in the list.
  11122. If the current buffer does not, find the first agenda file."
  11123. (interactive)
  11124. (let* ((fs (org-agenda-files t))
  11125. (files (append fs (list (car fs))))
  11126. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11127. file)
  11128. (unless files (error "No agenda files"))
  11129. (catch 'exit
  11130. (while (setq file (pop files))
  11131. (if (equal (file-truename file) tcf)
  11132. (when (car files)
  11133. (find-file (car files))
  11134. (throw 'exit t))))
  11135. (find-file (car fs)))
  11136. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11137. (defun org-agenda-file-to-front (&optional to-end)
  11138. "Move/add the current file to the top of the agenda file list.
  11139. If the file is not present in the list, it is added to the front. If it is
  11140. present, it is moved there. With optional argument TO-END, add/move to the
  11141. end of the list."
  11142. (interactive "P")
  11143. (let ((org-agenda-skip-unavailable-files nil)
  11144. (file-alist (mapcar (lambda (x)
  11145. (cons (file-truename x) x))
  11146. (org-agenda-files t)))
  11147. (ctf (file-truename buffer-file-name))
  11148. x had)
  11149. (setq x (assoc ctf file-alist) had x)
  11150. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11151. (if to-end
  11152. (setq file-alist (append (delq x file-alist) (list x)))
  11153. (setq file-alist (cons x (delq x file-alist))))
  11154. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11155. (org-install-agenda-files-menu)
  11156. (message "File %s to %s of agenda file list"
  11157. (if had "moved" "added") (if to-end "end" "front"))))
  11158. (defun org-remove-file (&optional file)
  11159. "Remove current file from the list of files in variable `org-agenda-files'.
  11160. These are the files which are being checked for agenda entries.
  11161. Optional argument FILE means, use this file instead of the current."
  11162. (interactive)
  11163. (let* ((org-agenda-skip-unavailable-files nil)
  11164. (file (or file buffer-file-name))
  11165. (true-file (file-truename file))
  11166. (afile (abbreviate-file-name file))
  11167. (files (delq nil (mapcar
  11168. (lambda (x)
  11169. (if (equal true-file
  11170. (file-truename x))
  11171. nil x))
  11172. (org-agenda-files t)))))
  11173. (if (not (= (length files) (length (org-agenda-files t))))
  11174. (progn
  11175. (org-store-new-agenda-file-list files)
  11176. (org-install-agenda-files-menu)
  11177. (message "Removed file: %s" afile))
  11178. (message "File was not in list: %s (not removed)" afile))))
  11179. (defun org-file-menu-entry (file)
  11180. (vector file (list 'find-file file) t))
  11181. (defun org-check-agenda-file (file)
  11182. "Make sure FILE exists. If not, ask user what to do."
  11183. (when (not (file-exists-p file))
  11184. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11185. (abbreviate-file-name file))
  11186. (let ((r (downcase (read-char-exclusive))))
  11187. (cond
  11188. ((equal r ?r)
  11189. (org-remove-file file)
  11190. (throw 'nextfile t))
  11191. (t (error "Abort"))))))
  11192. (defun org-get-agenda-file-buffer (file)
  11193. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11194. it to the list of buffers which might be released later."
  11195. (let ((buf (org-find-base-buffer-visiting file)))
  11196. (if buf
  11197. buf ; just return it
  11198. ;; Make a new buffer and remember it
  11199. (setq buf (find-file-noselect file))
  11200. (if buf (push buf org-agenda-new-buffers))
  11201. buf)))
  11202. (defun org-release-buffers (blist)
  11203. "Release all buffers in list, asking the user for confirmation when needed.
  11204. When a buffer is unmodified, it is just killed. When modified, it is saved
  11205. \(if the user agrees) and then killed."
  11206. (let (buf file)
  11207. (while (setq buf (pop blist))
  11208. (setq file (buffer-file-name buf))
  11209. (when (and (buffer-modified-p buf)
  11210. file
  11211. (y-or-n-p (format "Save file %s? " file)))
  11212. (with-current-buffer buf (save-buffer)))
  11213. (kill-buffer buf))))
  11214. (defun org-prepare-agenda-buffers (files)
  11215. "Create buffers for all agenda files, protect archived trees and comments."
  11216. (interactive)
  11217. (let ((pa '(:org-archived t))
  11218. (pc '(:org-comment t))
  11219. (pall '(:org-archived t :org-comment t))
  11220. (inhibit-read-only t)
  11221. (rea (concat ":" org-archive-tag ":"))
  11222. bmp file re)
  11223. (save-excursion
  11224. (save-restriction
  11225. (while (setq file (pop files))
  11226. (if (bufferp file)
  11227. (set-buffer file)
  11228. (org-check-agenda-file file)
  11229. (set-buffer (org-get-agenda-file-buffer file)))
  11230. (widen)
  11231. (setq bmp (buffer-modified-p))
  11232. (org-refresh-category-properties)
  11233. (setq org-todo-keywords-for-agenda
  11234. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11235. (setq org-done-keywords-for-agenda
  11236. (append org-done-keywords-for-agenda org-done-keywords))
  11237. (setq org-todo-keyword-alist-for-agenda
  11238. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11239. (setq org-tag-alist-for-agenda
  11240. (append org-tag-alist-for-agenda org-tag-alist))
  11241. (save-excursion
  11242. (remove-text-properties (point-min) (point-max) pall)
  11243. (when org-agenda-skip-archived-trees
  11244. (goto-char (point-min))
  11245. (while (re-search-forward rea nil t)
  11246. (if (org-on-heading-p t)
  11247. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11248. (goto-char (point-min))
  11249. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11250. (while (re-search-forward re nil t)
  11251. (add-text-properties
  11252. (match-beginning 0) (org-end-of-subtree t) pc)))
  11253. (set-buffer-modified-p bmp))))
  11254. (setq org-todo-keyword-alist-for-agenda
  11255. (org-uniquify org-todo-keyword-alist-for-agenda)
  11256. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11257. ;;;; Embedded LaTeX
  11258. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11259. "Keymap for the minor `org-cdlatex-mode'.")
  11260. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11261. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11262. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11263. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11264. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11265. (defvar org-cdlatex-texmathp-advice-is-done nil
  11266. "Flag remembering if we have applied the advice to texmathp already.")
  11267. (define-minor-mode org-cdlatex-mode
  11268. "Toggle the minor `org-cdlatex-mode'.
  11269. This mode supports entering LaTeX environment and math in LaTeX fragments
  11270. in Org-mode.
  11271. \\{org-cdlatex-mode-map}"
  11272. nil " OCDL" nil
  11273. (when org-cdlatex-mode (require 'cdlatex))
  11274. (unless org-cdlatex-texmathp-advice-is-done
  11275. (setq org-cdlatex-texmathp-advice-is-done t)
  11276. (defadvice texmathp (around org-math-always-on activate)
  11277. "Always return t in org-mode buffers.
  11278. This is because we want to insert math symbols without dollars even outside
  11279. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11280. an embedded LaTeX fragment, let texmathp do its job.
  11281. \\[org-cdlatex-mode-map]"
  11282. (interactive)
  11283. (let (p)
  11284. (cond
  11285. ((not (org-mode-p)) ad-do-it)
  11286. ((eq this-command 'cdlatex-math-symbol)
  11287. (setq ad-return-value t
  11288. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11289. (t
  11290. (let ((p (org-inside-LaTeX-fragment-p)))
  11291. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11292. (setq ad-return-value t
  11293. texmathp-why '("Org-mode embedded math" . 0))
  11294. (if p ad-do-it)))))))))
  11295. (defun turn-on-org-cdlatex ()
  11296. "Unconditionally turn on `org-cdlatex-mode'."
  11297. (org-cdlatex-mode 1))
  11298. (defun org-inside-LaTeX-fragment-p ()
  11299. "Test if point is inside a LaTeX fragment.
  11300. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11301. sequence appearing also before point.
  11302. Even though the matchers for math are configurable, this function assumes
  11303. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11304. delimiters are skipped when they have been removed by customization.
  11305. The return value is nil, or a cons cell with the delimiter and
  11306. and the position of this delimiter.
  11307. This function does a reasonably good job, but can locally be fooled by
  11308. for example currency specifications. For example it will assume being in
  11309. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11310. fragments that are properly closed, but during editing, we have to live
  11311. with the uncertainty caused by missing closing delimiters. This function
  11312. looks only before point, not after."
  11313. (catch 'exit
  11314. (let ((pos (point))
  11315. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11316. (lim (progn
  11317. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11318. (point)))
  11319. dd-on str (start 0) m re)
  11320. (goto-char pos)
  11321. (when dodollar
  11322. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11323. re (nth 1 (assoc "$" org-latex-regexps)))
  11324. (while (string-match re str start)
  11325. (cond
  11326. ((= (match-end 0) (length str))
  11327. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11328. ((= (match-end 0) (- (length str) 5))
  11329. (throw 'exit nil))
  11330. (t (setq start (match-end 0))))))
  11331. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11332. (goto-char pos)
  11333. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11334. (and (match-beginning 2) (throw 'exit nil))
  11335. ;; count $$
  11336. (while (re-search-backward "\\$\\$" lim t)
  11337. (setq dd-on (not dd-on)))
  11338. (goto-char pos)
  11339. (if dd-on (cons "$$" m))))))
  11340. (defun org-try-cdlatex-tab ()
  11341. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11342. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11343. - inside a LaTeX fragment, or
  11344. - after the first word in a line, where an abbreviation expansion could
  11345. insert a LaTeX environment."
  11346. (when org-cdlatex-mode
  11347. (cond
  11348. ((save-excursion
  11349. (skip-chars-backward "a-zA-Z0-9*")
  11350. (skip-chars-backward " \t")
  11351. (bolp))
  11352. (cdlatex-tab) t)
  11353. ((org-inside-LaTeX-fragment-p)
  11354. (cdlatex-tab) t)
  11355. (t nil))))
  11356. (defun org-cdlatex-underscore-caret (&optional arg)
  11357. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11358. Revert to the normal definition outside of these fragments."
  11359. (interactive "P")
  11360. (if (org-inside-LaTeX-fragment-p)
  11361. (call-interactively 'cdlatex-sub-superscript)
  11362. (let (org-cdlatex-mode)
  11363. (call-interactively (key-binding (vector last-input-event))))))
  11364. (defun org-cdlatex-math-modify (&optional arg)
  11365. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11366. Revert to the normal definition outside of these fragments."
  11367. (interactive "P")
  11368. (if (org-inside-LaTeX-fragment-p)
  11369. (call-interactively 'cdlatex-math-modify)
  11370. (let (org-cdlatex-mode)
  11371. (call-interactively (key-binding (vector last-input-event))))))
  11372. (defvar org-latex-fragment-image-overlays nil
  11373. "List of overlays carrying the images of latex fragments.")
  11374. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11375. (defun org-remove-latex-fragment-image-overlays ()
  11376. "Remove all overlays with LaTeX fragment images in current buffer."
  11377. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11378. (setq org-latex-fragment-image-overlays nil))
  11379. (defun org-preview-latex-fragment (&optional subtree)
  11380. "Preview the LaTeX fragment at point, or all locally or globally.
  11381. If the cursor is in a LaTeX fragment, create the image and overlay
  11382. it over the source code. If there is no fragment at point, display
  11383. all fragments in the current text, from one headline to the next. With
  11384. prefix SUBTREE, display all fragments in the current subtree. With a
  11385. double prefix `C-u C-u', or when the cursor is before the first headline,
  11386. display all fragments in the buffer.
  11387. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11388. (interactive "P")
  11389. (org-remove-latex-fragment-image-overlays)
  11390. (save-excursion
  11391. (save-restriction
  11392. (let (beg end at msg)
  11393. (cond
  11394. ((or (equal subtree '(16))
  11395. (not (save-excursion
  11396. (re-search-backward (concat "^" outline-regexp) nil t))))
  11397. (setq beg (point-min) end (point-max)
  11398. msg "Creating images for buffer...%s"))
  11399. ((equal subtree '(4))
  11400. (org-back-to-heading)
  11401. (setq beg (point) end (org-end-of-subtree t)
  11402. msg "Creating images for subtree...%s"))
  11403. (t
  11404. (if (setq at (org-inside-LaTeX-fragment-p))
  11405. (goto-char (max (point-min) (- (cdr at) 2)))
  11406. (org-back-to-heading))
  11407. (setq beg (point) end (progn (outline-next-heading) (point))
  11408. msg (if at "Creating image...%s"
  11409. "Creating images for entry...%s"))))
  11410. (message msg "")
  11411. (narrow-to-region beg end)
  11412. (goto-char beg)
  11413. (org-format-latex
  11414. (concat "ltxpng/" (file-name-sans-extension
  11415. (file-name-nondirectory
  11416. buffer-file-name)))
  11417. default-directory 'overlays msg at 'forbuffer)
  11418. (message msg "done. Use `C-c C-c' to remove images.")))))
  11419. (defvar org-latex-regexps
  11420. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11421. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11422. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11423. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11424. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11425. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11426. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11427. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11428. "Regular expressions for matching embedded LaTeX.")
  11429. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11430. "Replace LaTeX fragments with links to an image, and produce images."
  11431. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11432. (let* ((prefixnodir (file-name-nondirectory prefix))
  11433. (absprefix (expand-file-name prefix dir))
  11434. (todir (file-name-directory absprefix))
  11435. (opt org-format-latex-options)
  11436. (matchers (plist-get opt :matchers))
  11437. (re-list org-latex-regexps)
  11438. (cnt 0) txt link beg end re e checkdir
  11439. m n block linkfile movefile ov)
  11440. ;; Check if there are old images files with this prefix, and remove them
  11441. (when (file-directory-p todir)
  11442. (mapc 'delete-file
  11443. (directory-files
  11444. todir 'full
  11445. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11446. ;; Check the different regular expressions
  11447. (while (setq e (pop re-list))
  11448. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11449. block (if (nth 3 e) "\n\n" ""))
  11450. (when (member m matchers)
  11451. (goto-char (point-min))
  11452. (while (re-search-forward re nil t)
  11453. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11454. (setq txt (match-string n)
  11455. beg (match-beginning n) end (match-end n)
  11456. cnt (1+ cnt)
  11457. linkfile (format "%s_%04d.png" prefix cnt)
  11458. movefile (format "%s_%04d.png" absprefix cnt)
  11459. link (concat block "[[file:" linkfile "]]" block))
  11460. (if msg (message msg cnt))
  11461. (goto-char beg)
  11462. (unless checkdir ; make sure the directory exists
  11463. (setq checkdir t)
  11464. (or (file-directory-p todir) (make-directory todir)))
  11465. (org-create-formula-image
  11466. txt movefile opt forbuffer)
  11467. (if overlays
  11468. (progn
  11469. (setq ov (org-make-overlay beg end))
  11470. (if (featurep 'xemacs)
  11471. (progn
  11472. (org-overlay-put ov 'invisible t)
  11473. (org-overlay-put
  11474. ov 'end-glyph
  11475. (make-glyph (vector 'png :file movefile))))
  11476. (org-overlay-put
  11477. ov 'display
  11478. (list 'image :type 'png :file movefile :ascent 'center)))
  11479. (push ov org-latex-fragment-image-overlays)
  11480. (goto-char end))
  11481. (delete-region beg end)
  11482. (insert link))))))))
  11483. ;; This function borrows from Ganesh Swami's latex2png.el
  11484. (defun org-create-formula-image (string tofile options buffer)
  11485. (let* ((tmpdir (if (featurep 'xemacs)
  11486. (temp-directory)
  11487. temporary-file-directory))
  11488. (texfilebase (make-temp-name
  11489. (expand-file-name "orgtex" tmpdir)))
  11490. (texfile (concat texfilebase ".tex"))
  11491. (dvifile (concat texfilebase ".dvi"))
  11492. (pngfile (concat texfilebase ".png"))
  11493. (fnh (if (featurep 'xemacs)
  11494. (font-height (get-face-font 'default))
  11495. (face-attribute 'default :height nil)))
  11496. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11497. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11498. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11499. "Black"))
  11500. (bg (or (plist-get options (if buffer :background :html-background))
  11501. "Transparent")))
  11502. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11503. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11504. (with-temp-file texfile
  11505. (insert org-format-latex-header
  11506. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11507. (let ((dir default-directory))
  11508. (condition-case nil
  11509. (progn
  11510. (cd tmpdir)
  11511. (call-process "latex" nil nil nil texfile))
  11512. (error nil))
  11513. (cd dir))
  11514. (if (not (file-exists-p dvifile))
  11515. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11516. (condition-case nil
  11517. (call-process "dvipng" nil nil nil
  11518. "-E" "-fg" fg "-bg" bg
  11519. "-D" dpi
  11520. ;;"-x" scale "-y" scale
  11521. "-T" "tight"
  11522. "-o" pngfile
  11523. dvifile)
  11524. (error nil))
  11525. (if (not (file-exists-p pngfile))
  11526. (progn (message "Failed to create png file from %s" texfile) nil)
  11527. ;; Use the requested file name and clean up
  11528. (copy-file pngfile tofile 'replace)
  11529. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11530. (delete-file (concat texfilebase e)))
  11531. pngfile))))
  11532. (defun org-dvipng-color (attr)
  11533. "Return an rgb color specification for dvipng."
  11534. (apply 'format "rgb %s %s %s"
  11535. (mapcar 'org-normalize-color
  11536. (color-values (face-attribute 'default attr nil)))))
  11537. (defun org-normalize-color (value)
  11538. "Return string to be used as color value for an RGB component."
  11539. (format "%g" (/ value 65535.0)))
  11540. ;;;; Key bindings
  11541. ;; Make `C-c C-x' a prefix key
  11542. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11543. ;; TAB key with modifiers
  11544. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11545. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11546. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11547. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11548. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11549. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11550. ;; The following line is necessary under Suse GNU/Linux
  11551. (unless (featurep 'xemacs)
  11552. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11553. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11554. (define-key org-mode-map [backtab] 'org-shifttab)
  11555. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11556. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11557. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11558. ;; Cursor keys with modifiers
  11559. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11560. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11561. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11562. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11563. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11564. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11565. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11566. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11567. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11568. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11569. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11570. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11571. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11572. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11573. ;;; Extra keys for tty access.
  11574. ;; We only set them when really needed because otherwise the
  11575. ;; menus don't show the simple keys
  11576. (when (or org-use-extra-keys
  11577. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11578. (not window-system))
  11579. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11580. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11581. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11582. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11583. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11584. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11585. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11586. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11587. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11588. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11589. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11590. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11591. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11592. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11593. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11594. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11595. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11596. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11597. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11598. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11599. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11600. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11601. ;; All the other keys
  11602. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11603. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11604. (if (boundp 'narrow-map)
  11605. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11606. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11607. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11608. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11609. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11610. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11611. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11612. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11613. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11614. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11615. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11616. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11617. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11618. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11619. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11620. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11621. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11622. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11623. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11624. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11625. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11626. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11627. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11628. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11629. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11630. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11631. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11632. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11633. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11634. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11635. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11636. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11637. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11638. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11639. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11640. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11641. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11642. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11643. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11644. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11645. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11646. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11647. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11648. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11649. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11650. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11651. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11652. (org-defkey org-mode-map "\C-m" 'org-return)
  11653. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11654. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11655. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11656. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11657. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11658. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11659. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11660. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11661. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11662. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11663. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11664. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11665. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11666. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11667. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11668. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11669. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11670. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11671. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11672. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11673. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11674. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11675. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11676. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11677. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11678. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11679. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11680. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11681. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11682. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11683. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11684. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11685. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11686. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11687. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11688. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11689. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  11690. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11691. (when (featurep 'xemacs)
  11692. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11693. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11694. (defun org-self-insert-command (N)
  11695. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11696. If the cursor is in a table looking at whitespace, the whitespace is
  11697. overwritten, and the table is not marked as requiring realignment."
  11698. (interactive "p")
  11699. (if (and (org-table-p)
  11700. (progn
  11701. ;; check if we blank the field, and if that triggers align
  11702. (and (featurep 'org-table) org-table-auto-blank-field
  11703. (member last-command
  11704. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11705. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11706. ;; got extra space, this field does not determine column width
  11707. (let (org-table-may-need-update) (org-table-blank-field))
  11708. ;; no extra space, this field may determine column width
  11709. (org-table-blank-field)))
  11710. t)
  11711. (eq N 1)
  11712. (looking-at "[^|\n]* |"))
  11713. (let (org-table-may-need-update)
  11714. (goto-char (1- (match-end 0)))
  11715. (delete-backward-char 1)
  11716. (goto-char (match-beginning 0))
  11717. (self-insert-command N))
  11718. (setq org-table-may-need-update t)
  11719. (self-insert-command N)
  11720. (org-fix-tags-on-the-fly)))
  11721. (defun org-fix-tags-on-the-fly ()
  11722. (when (and (equal (char-after (point-at-bol)) ?*)
  11723. (org-on-heading-p))
  11724. (org-align-tags-here org-tags-column)))
  11725. (defun org-delete-backward-char (N)
  11726. "Like `delete-backward-char', insert whitespace at field end in tables.
  11727. When deleting backwards, in tables this function will insert whitespace in
  11728. front of the next \"|\" separator, to keep the table aligned. The table will
  11729. still be marked for re-alignment if the field did fill the entire column,
  11730. because, in this case the deletion might narrow the column."
  11731. (interactive "p")
  11732. (if (and (org-table-p)
  11733. (eq N 1)
  11734. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11735. (looking-at ".*?|"))
  11736. (let ((pos (point))
  11737. (noalign (looking-at "[^|\n\r]* |"))
  11738. (c org-table-may-need-update))
  11739. (backward-delete-char N)
  11740. (skip-chars-forward "^|")
  11741. (insert " ")
  11742. (goto-char (1- pos))
  11743. ;; noalign: if there were two spaces at the end, this field
  11744. ;; does not determine the width of the column.
  11745. (if noalign (setq org-table-may-need-update c)))
  11746. (backward-delete-char N)
  11747. (org-fix-tags-on-the-fly)))
  11748. (defun org-delete-char (N)
  11749. "Like `delete-char', but insert whitespace at field end in tables.
  11750. When deleting characters, in tables this function will insert whitespace in
  11751. front of the next \"|\" separator, to keep the table aligned. The table will
  11752. still be marked for re-alignment if the field did fill the entire column,
  11753. because, in this case the deletion might narrow the column."
  11754. (interactive "p")
  11755. (if (and (org-table-p)
  11756. (not (bolp))
  11757. (not (= (char-after) ?|))
  11758. (eq N 1))
  11759. (if (looking-at ".*?|")
  11760. (let ((pos (point))
  11761. (noalign (looking-at "[^|\n\r]* |"))
  11762. (c org-table-may-need-update))
  11763. (replace-match (concat
  11764. (substring (match-string 0) 1 -1)
  11765. " |"))
  11766. (goto-char pos)
  11767. ;; noalign: if there were two spaces at the end, this field
  11768. ;; does not determine the width of the column.
  11769. (if noalign (setq org-table-may-need-update c)))
  11770. (delete-char N))
  11771. (delete-char N)
  11772. (org-fix-tags-on-the-fly)))
  11773. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11774. (put 'org-self-insert-command 'delete-selection t)
  11775. (put 'orgtbl-self-insert-command 'delete-selection t)
  11776. (put 'org-delete-char 'delete-selection 'supersede)
  11777. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11778. ;; Make `flyspell-mode' delay after some commands
  11779. (put 'org-self-insert-command 'flyspell-delayed t)
  11780. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11781. (put 'org-delete-char 'flyspell-delayed t)
  11782. (put 'org-delete-backward-char 'flyspell-delayed t)
  11783. ;; Make pabbrev-mode expand after org-mode commands
  11784. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11785. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11786. ;; How to do this: Measure non-white length of current string
  11787. ;; If equal to column width, we should realign.
  11788. (defun org-remap (map &rest commands)
  11789. "In MAP, remap the functions given in COMMANDS.
  11790. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11791. (let (new old)
  11792. (while commands
  11793. (setq old (pop commands) new (pop commands))
  11794. (if (fboundp 'command-remapping)
  11795. (org-defkey map (vector 'remap old) new)
  11796. (substitute-key-definition old new map global-map)))))
  11797. (when (eq org-enable-table-editor 'optimized)
  11798. ;; If the user wants maximum table support, we need to hijack
  11799. ;; some standard editing functions
  11800. (org-remap org-mode-map
  11801. 'self-insert-command 'org-self-insert-command
  11802. 'delete-char 'org-delete-char
  11803. 'delete-backward-char 'org-delete-backward-char)
  11804. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11805. (defun org-shiftcursor-error ()
  11806. "Throw an error because Shift-Cursor command was applied in wrong context."
  11807. (error "This command is active in special context like tables, headlines or timestamps"))
  11808. (defun org-shifttab (&optional arg)
  11809. "Global visibility cycling or move to previous table field.
  11810. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11811. on context.
  11812. See the individual commands for more information."
  11813. (interactive "P")
  11814. (cond
  11815. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11816. ((integerp arg)
  11817. (message "Content view to level: %d" arg)
  11818. (org-content (prefix-numeric-value arg))
  11819. (setq org-cycle-global-status 'overview))
  11820. (t (call-interactively 'org-global-cycle))))
  11821. (defun org-shiftmetaleft ()
  11822. "Promote subtree or delete table column.
  11823. Calls `org-promote-subtree', `org-outdent-item',
  11824. or `org-table-delete-column', depending on context.
  11825. See the individual commands for more information."
  11826. (interactive)
  11827. (cond
  11828. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11829. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11830. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11831. (t (org-shiftcursor-error))))
  11832. (defun org-shiftmetaright ()
  11833. "Demote subtree or insert table column.
  11834. Calls `org-demote-subtree', `org-indent-item',
  11835. or `org-table-insert-column', depending on context.
  11836. See the individual commands for more information."
  11837. (interactive)
  11838. (cond
  11839. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11840. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11841. ((org-at-item-p) (call-interactively 'org-indent-item))
  11842. (t (org-shiftcursor-error))))
  11843. (defun org-shiftmetaup (&optional arg)
  11844. "Move subtree up or kill table row.
  11845. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11846. `org-move-item-up' depending on context. See the individual commands
  11847. for more information."
  11848. (interactive "P")
  11849. (cond
  11850. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11851. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11852. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11853. (t (org-shiftcursor-error))))
  11854. (defun org-shiftmetadown (&optional arg)
  11855. "Move subtree down or insert table row.
  11856. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11857. `org-move-item-down', depending on context. See the individual
  11858. commands for more information."
  11859. (interactive "P")
  11860. (cond
  11861. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11862. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11863. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11864. (t (org-shiftcursor-error))))
  11865. (defun org-metaleft (&optional arg)
  11866. "Promote heading or move table column to left.
  11867. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11868. With no specific context, calls the Emacs default `backward-word'.
  11869. See the individual commands for more information."
  11870. (interactive "P")
  11871. (cond
  11872. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11873. ((or (org-on-heading-p) (org-region-active-p))
  11874. (call-interactively 'org-do-promote))
  11875. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11876. (t (call-interactively 'backward-word))))
  11877. (defun org-metaright (&optional arg)
  11878. "Demote subtree or move table column to right.
  11879. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11880. With no specific context, calls the Emacs default `forward-word'.
  11881. See the individual commands for more information."
  11882. (interactive "P")
  11883. (cond
  11884. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11885. ((or (org-on-heading-p) (org-region-active-p))
  11886. (call-interactively 'org-do-demote))
  11887. ((org-at-item-p) (call-interactively 'org-indent-item))
  11888. (t (call-interactively 'forward-word))))
  11889. (defun org-metaup (&optional arg)
  11890. "Move subtree up or move table row up.
  11891. Calls `org-move-subtree-up' or `org-table-move-row' or
  11892. `org-move-item-up', depending on context. See the individual commands
  11893. for more information."
  11894. (interactive "P")
  11895. (cond
  11896. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11897. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11898. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11899. (t (transpose-lines 1) (beginning-of-line -1))))
  11900. (defun org-metadown (&optional arg)
  11901. "Move subtree down or move table row down.
  11902. Calls `org-move-subtree-down' or `org-table-move-row' or
  11903. `org-move-item-down', depending on context. See the individual
  11904. commands for more information."
  11905. (interactive "P")
  11906. (cond
  11907. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11908. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11909. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11910. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11911. (defun org-shiftup (&optional arg)
  11912. "Increase item in timestamp or increase priority of current headline.
  11913. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11914. depending on context. See the individual commands for more information."
  11915. (interactive "P")
  11916. (cond
  11917. ((org-at-timestamp-p t)
  11918. (call-interactively (if org-edit-timestamp-down-means-later
  11919. 'org-timestamp-down 'org-timestamp-up)))
  11920. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11921. ((org-at-item-p) (call-interactively 'org-previous-item))
  11922. ((org-clocktable-try-shift 'up arg))
  11923. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11924. (defun org-shiftdown (&optional arg)
  11925. "Decrease item in timestamp or decrease priority of current headline.
  11926. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11927. depending on context. See the individual commands for more information."
  11928. (interactive "P")
  11929. (cond
  11930. ((org-at-timestamp-p t)
  11931. (call-interactively (if org-edit-timestamp-down-means-later
  11932. 'org-timestamp-up 'org-timestamp-down)))
  11933. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11934. ((org-clocktable-try-shift 'down arg))
  11935. (t (call-interactively 'org-next-item))))
  11936. (defun org-shiftright (&optional arg)
  11937. "Cycle the thing at point or in the current line, depending on context.
  11938. Depending on context, this does one of the following:
  11939. - switch a timestamp at point one day into the future
  11940. - on a headline, switch to the next TODO keyword.
  11941. - on an item, switch entire list to the next bullet type
  11942. - on a property line, switch to the next allowed value
  11943. - on a clocktable definition line, move time block into the future"
  11944. (interactive "P")
  11945. (cond
  11946. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11947. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11948. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11949. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11950. ((org-clocktable-try-shift 'right arg))
  11951. (t (org-shiftcursor-error))))
  11952. (defun org-shiftleft (&optional arg)
  11953. "Cycle the thing at point or in the current line, depending on context.
  11954. Depending on context, this does one of the following:
  11955. - switch a timestamp at point one day into the past
  11956. - on a headline, switch to the previous TODO keyword.
  11957. - on an item, switch entire list to the previous bullet type
  11958. - on a property line, switch to the previous allowed value
  11959. - on a clocktable definition line, move time block into the past"
  11960. (interactive "P")
  11961. (cond
  11962. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11963. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11964. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11965. ((org-at-property-p)
  11966. (call-interactively 'org-property-previous-allowed-value))
  11967. ((org-clocktable-try-shift 'left arg))
  11968. (t (org-shiftcursor-error))))
  11969. (defun org-shiftcontrolright ()
  11970. "Switch to next TODO set."
  11971. (interactive)
  11972. (cond
  11973. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11974. (t (org-shiftcursor-error))))
  11975. (defun org-shiftcontrolleft ()
  11976. "Switch to previous TODO set."
  11977. (interactive)
  11978. (cond
  11979. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11980. (t (org-shiftcursor-error))))
  11981. (defun org-ctrl-c-ret ()
  11982. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11983. (interactive)
  11984. (cond
  11985. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11986. (t (call-interactively 'org-insert-heading))))
  11987. (defun org-copy-special ()
  11988. "Copy region in table or copy current subtree.
  11989. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11990. See the individual commands for more information."
  11991. (interactive)
  11992. (call-interactively
  11993. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11994. (defun org-cut-special ()
  11995. "Cut region in table or cut current subtree.
  11996. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11997. See the individual commands for more information."
  11998. (interactive)
  11999. (call-interactively
  12000. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12001. (defun org-paste-special (arg)
  12002. "Paste rectangular region into table, or past subtree relative to level.
  12003. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12004. See the individual commands for more information."
  12005. (interactive "P")
  12006. (if (org-at-table-p)
  12007. (org-table-paste-rectangle)
  12008. (org-paste-subtree arg)))
  12009. (defun org-edit-special ()
  12010. "Call a special editor for the stuff at point.
  12011. When at a table, call the formula editor with `org-table-edit-formulas'.
  12012. When at the first line of an src example, call `org-edit-src-code'.
  12013. When in an #+include line, visit the include file. Otherwise call
  12014. `ffap' to visit the file at point."
  12015. (interactive)
  12016. (cond
  12017. ((org-at-table-p)
  12018. (call-interactively 'org-table-edit-formulas))
  12019. ((save-excursion
  12020. (beginning-of-line 1)
  12021. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12022. (find-file (org-trim (match-string 1))))
  12023. ((org-edit-src-code))
  12024. ((org-edit-fixed-width-region))
  12025. (t (call-interactively 'ffap))))
  12026. (defun org-ctrl-c-ctrl-c (&optional arg)
  12027. "Set tags in headline, or update according to changed information at point.
  12028. This command does many different things, depending on context:
  12029. - If the cursor is in a headline, prompt for tags and insert them
  12030. into the current line, aligned to `org-tags-column'. When called
  12031. with prefix arg, realign all tags in the current buffer.
  12032. - If the cursor is in one of the special #+KEYWORD lines, this
  12033. triggers scanning the buffer for these lines and updating the
  12034. information.
  12035. - If the cursor is inside a table, realign the table. This command
  12036. works even if the automatic table editor has been turned off.
  12037. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12038. the entire table.
  12039. - If the cursor is at a footnote reference or definition, jump to
  12040. the corresponding definition or references, respectively.
  12041. - If the cursor is a the beginning of a dynamic block, update it.
  12042. - If the cursor is inside a table created by the table.el package,
  12043. activate that table.
  12044. - If the current buffer is a remember buffer, close note and file
  12045. it. A prefix argument of 1 files to the default location
  12046. without further interaction. A prefix argument of 2 files to
  12047. the currently clocking task.
  12048. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12049. links in this buffer.
  12050. - If the cursor is on a numbered item in a plain list, renumber the
  12051. ordered list.
  12052. - If the cursor is on a checkbox, toggle it."
  12053. (interactive "P")
  12054. (let ((org-enable-table-editor t))
  12055. (cond
  12056. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12057. org-occur-highlights
  12058. org-latex-fragment-image-overlays)
  12059. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12060. (org-remove-occur-highlights)
  12061. (org-remove-latex-fragment-image-overlays)
  12062. (message "Temporary highlights/overlays removed from current buffer"))
  12063. ((and (local-variable-p 'org-finish-function (current-buffer))
  12064. (fboundp org-finish-function))
  12065. (funcall org-finish-function))
  12066. ((org-at-property-p)
  12067. (call-interactively 'org-property-action))
  12068. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12069. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12070. ((org-at-table.el-p)
  12071. (require 'table)
  12072. (beginning-of-line 1)
  12073. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12074. (call-interactively 'table-recognize-table))
  12075. ((org-at-table-p)
  12076. (org-table-maybe-eval-formula)
  12077. (if arg
  12078. (call-interactively 'org-table-recalculate)
  12079. (org-table-maybe-recalculate-line))
  12080. (call-interactively 'org-table-align))
  12081. ((or (org-footnote-at-reference-p)
  12082. (org-footnote-at-definition-p))
  12083. (call-interactively 'org-footnote-action))
  12084. ((org-at-item-checkbox-p)
  12085. (call-interactively 'org-toggle-checkbox))
  12086. ((org-at-item-p)
  12087. (call-interactively 'org-maybe-renumber-ordered-list))
  12088. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12089. ;; Dynamic block
  12090. (beginning-of-line 1)
  12091. (save-excursion (org-update-dblock)))
  12092. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12093. (cond
  12094. ((equal (match-string 1) "TBLFM")
  12095. ;; Recalculate the table before this line
  12096. (save-excursion
  12097. (beginning-of-line 1)
  12098. (skip-chars-backward " \r\n\t")
  12099. (if (org-at-table-p)
  12100. (org-call-with-arg 'org-table-recalculate t))))
  12101. (t
  12102. ; (org-set-regexps-and-options)
  12103. ; (org-restart-font-lock)
  12104. (let ((org-inhibit-startup t)) (org-mode-restart))
  12105. (message "Local setup has been refreshed"))))
  12106. (t (error "C-c C-c can do nothing useful at this location.")))))
  12107. (defun org-mode-restart ()
  12108. "Restart Org-mode, to scan again for special lines.
  12109. Also updates the keyword regular expressions."
  12110. (interactive)
  12111. (org-mode)
  12112. (message "Org-mode restarted"))
  12113. (defun org-kill-note-or-show-branches ()
  12114. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12115. (interactive)
  12116. (if (not org-finish-function)
  12117. (call-interactively 'show-branches)
  12118. (let ((org-note-abort t))
  12119. (funcall org-finish-function))))
  12120. (defun org-return (&optional indent)
  12121. "Goto next table row or insert a newline.
  12122. Calls `org-table-next-row' or `newline', depending on context.
  12123. See the individual commands for more information."
  12124. (interactive)
  12125. (cond
  12126. ((bobp) (if indent (newline-and-indent) (newline)))
  12127. ((and (org-at-heading-p)
  12128. (looking-at
  12129. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12130. (org-show-entry)
  12131. (end-of-line 1)
  12132. (newline))
  12133. ((org-at-table-p)
  12134. (org-table-justify-field-maybe)
  12135. (call-interactively 'org-table-next-row))
  12136. (t (if indent (newline-and-indent) (newline)))))
  12137. (defun org-return-indent ()
  12138. "Goto next table row or insert a newline and indent.
  12139. Calls `org-table-next-row' or `newline-and-indent', depending on
  12140. context. See the individual commands for more information."
  12141. (interactive)
  12142. (org-return t))
  12143. (defun org-ctrl-c-star ()
  12144. "Compute table, or change heading status of lines.
  12145. Calls `org-table-recalculate' or `org-toggle-heading',
  12146. depending on context."
  12147. (interactive)
  12148. (cond
  12149. ((org-at-table-p)
  12150. (call-interactively 'org-table-recalculate))
  12151. (t
  12152. ;; Convert all lines in region to list items
  12153. (call-interactively 'org-toggle-heading))))
  12154. (defun org-ctrl-c-minus ()
  12155. "Insert separator line in table or modify bullet status of line.
  12156. Also turns a plain line or a region of lines into list items.
  12157. Calls `org-table-insert-hline', `org-toggle-item', or
  12158. `org-cycle-list-bullet', depending on context."
  12159. (interactive)
  12160. (cond
  12161. ((org-at-table-p)
  12162. (call-interactively 'org-table-insert-hline))
  12163. ((org-region-active-p)
  12164. (call-interactively 'org-toggle-item))
  12165. ((org-in-item-p)
  12166. (call-interactively 'org-cycle-list-bullet))
  12167. (t
  12168. (call-interactively 'org-toggle-item))))
  12169. (defun org-toggle-item ()
  12170. "Convert headings or normal lines to items, items to normal lines.
  12171. If there is no active region, only the current line is considered.
  12172. If the first line in the region is a headline, convert all headlines to items.
  12173. If the first line in the region is an item, convert all items to normal lines.
  12174. If the first line is normal text, add an item bullet to each line."
  12175. (interactive)
  12176. (let (l2 l beg end)
  12177. (if (org-region-active-p)
  12178. (setq beg (region-beginning) end (region-end))
  12179. (setq beg (point-at-bol)
  12180. end (min (1+ (point-at-eol)) (point-max))))
  12181. (save-excursion
  12182. (goto-char end)
  12183. (setq l2 (org-current-line))
  12184. (goto-char beg)
  12185. (beginning-of-line 1)
  12186. (setq l (1- (org-current-line)))
  12187. (if (org-at-item-p)
  12188. ;; We already have items, de-itemize
  12189. (while (< (setq l (1+ l)) l2)
  12190. (when (org-at-item-p)
  12191. (goto-char (match-beginning 2))
  12192. (delete-region (match-beginning 2) (match-end 2))
  12193. (and (looking-at "[ \t]+") (replace-match "")))
  12194. (beginning-of-line 2))
  12195. (if (org-on-heading-p)
  12196. ;; Headings, convert to items
  12197. (while (< (setq l (1+ l)) l2)
  12198. (if (looking-at org-outline-regexp)
  12199. (replace-match "- " t t))
  12200. (beginning-of-line 2))
  12201. ;; normal lines, turn them into items
  12202. (while (< (setq l (1+ l)) l2)
  12203. (unless (org-at-item-p)
  12204. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12205. (replace-match "\\1- \\2")))
  12206. (beginning-of-line 2)))))))
  12207. (defun org-toggle-heading (&optional nstars)
  12208. "Convert headings to normal text, or items or text to headings.
  12209. If there is no active region, only the current line is considered.
  12210. If the first line is a heading, remove the stars from all headlines
  12211. in the region.
  12212. If the first line is a plain list item, turn all plain list items into
  12213. headings.
  12214. If the first line is a normal line, turn each and every line in the region
  12215. into a heading.
  12216. When converting a line into a heading, the number of stars is chosen
  12217. such that the lines become children of the current entry. However, when
  12218. a prefix argument is given, its value determines the number of stars to add."
  12219. (interactive "P")
  12220. (let (l2 l itemp beg end)
  12221. (if (org-region-active-p)
  12222. (setq beg (region-beginning) end (region-end))
  12223. (setq beg (point-at-bol)
  12224. end (min (1+ (point-at-eol)) (point-max))))
  12225. (save-excursion
  12226. (goto-char end)
  12227. (setq l2 (org-current-line))
  12228. (goto-char beg)
  12229. (beginning-of-line 1)
  12230. (setq l (1- (org-current-line)))
  12231. (if (org-on-heading-p)
  12232. ;; We already have headlines, de-star them
  12233. (while (< (setq l (1+ l)) l2)
  12234. (when (org-on-heading-p t)
  12235. (and (looking-at outline-regexp) (replace-match "")))
  12236. (beginning-of-line 2))
  12237. (setq itemp (org-at-item-p))
  12238. (let* ((stars
  12239. (if nstars
  12240. (make-string (prefix-numeric-value current-prefix-arg)
  12241. ?*)
  12242. (save-excursion
  12243. (re-search-backward org-complex-heading-regexp nil t)
  12244. (or (match-string 1) "*"))))
  12245. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12246. (rpl (concat stars add-stars " ")))
  12247. (while (< (setq l (1+ l)) l2)
  12248. (if itemp
  12249. (and (org-at-item-p) (replace-match rpl t t))
  12250. (unless (org-on-heading-p)
  12251. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12252. (replace-match (concat rpl (match-string 2))))))
  12253. (beginning-of-line 2)))))))
  12254. (defun org-meta-return (&optional arg)
  12255. "Insert a new heading or wrap a region in a table.
  12256. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12257. See the individual commands for more information."
  12258. (interactive "P")
  12259. (cond
  12260. ((org-at-table-p)
  12261. (call-interactively 'org-table-wrap-region))
  12262. (t (call-interactively 'org-insert-heading))))
  12263. ;;; Menu entries
  12264. ;; Define the Org-mode menus
  12265. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12266. '("Tbl"
  12267. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12268. ["Next Field" org-cycle (org-at-table-p)]
  12269. ["Previous Field" org-shifttab (org-at-table-p)]
  12270. ["Next Row" org-return (org-at-table-p)]
  12271. "--"
  12272. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12273. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12274. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12275. "--"
  12276. ("Column"
  12277. ["Move Column Left" org-metaleft (org-at-table-p)]
  12278. ["Move Column Right" org-metaright (org-at-table-p)]
  12279. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12280. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12281. ("Row"
  12282. ["Move Row Up" org-metaup (org-at-table-p)]
  12283. ["Move Row Down" org-metadown (org-at-table-p)]
  12284. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12285. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12286. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12287. "--"
  12288. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12289. ("Rectangle"
  12290. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12291. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12292. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12293. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12294. "--"
  12295. ("Calculate"
  12296. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12297. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12298. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12299. "--"
  12300. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12301. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12302. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12303. "--"
  12304. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12305. "--"
  12306. ["Sum Column/Rectangle" org-table-sum
  12307. (or (org-at-table-p) (org-region-active-p))]
  12308. ["Which Column?" org-table-current-column (org-at-table-p)])
  12309. ["Debug Formulas"
  12310. org-table-toggle-formula-debugger
  12311. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12312. ["Show Col/Row Numbers"
  12313. org-table-toggle-coordinate-overlays
  12314. :style toggle
  12315. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12316. "--"
  12317. ["Create" org-table-create (and (not (org-at-table-p))
  12318. org-enable-table-editor)]
  12319. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12320. ["Import from File" org-table-import (not (org-at-table-p))]
  12321. ["Export to File" org-table-export (org-at-table-p)]
  12322. "--"
  12323. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12324. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12325. '("Org"
  12326. ("Show/Hide"
  12327. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12328. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12329. ["Sparse Tree..." org-sparse-tree t]
  12330. ["Reveal Context" org-reveal t]
  12331. ["Show All" show-all t]
  12332. "--"
  12333. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12334. "--"
  12335. ["New Heading" org-insert-heading t]
  12336. ("Navigate Headings"
  12337. ["Up" outline-up-heading t]
  12338. ["Next" outline-next-visible-heading t]
  12339. ["Previous" outline-previous-visible-heading t]
  12340. ["Next Same Level" outline-forward-same-level t]
  12341. ["Previous Same Level" outline-backward-same-level t]
  12342. "--"
  12343. ["Jump" org-goto t])
  12344. ("Edit Structure"
  12345. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12346. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12347. "--"
  12348. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12349. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12350. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12351. "--"
  12352. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12353. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12354. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12355. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12356. "--"
  12357. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12358. "--"
  12359. ["Convert to odd levels" org-convert-to-odd-levels t]
  12360. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12361. ("Editing"
  12362. ["Emphasis..." org-emphasize t]
  12363. ["Edit Source Example" org-edit-special t]
  12364. "--"
  12365. ["Footnote new/jump" org-footnote-action t]
  12366. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12367. ("Archive"
  12368. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12369. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12370. ; :active t :keys "C-u C-c C-x C-a"]
  12371. ["Sparse trees open ARCHIVE trees"
  12372. (setq org-sparse-tree-open-archived-trees
  12373. (not org-sparse-tree-open-archived-trees))
  12374. :style toggle :selected org-sparse-tree-open-archived-trees]
  12375. ["Cycling opens ARCHIVE trees"
  12376. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12377. :style toggle :selected org-cycle-open-archived-trees]
  12378. "--"
  12379. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12380. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12381. ; ["Check and Move Children" (org-archive-subtree '(4))
  12382. ; :active t :keys "C-u C-c C-x C-s"]
  12383. )
  12384. "--"
  12385. ("TODO Lists"
  12386. ["TODO/DONE/-" org-todo t]
  12387. ("Select keyword"
  12388. ["Next keyword" org-shiftright (org-on-heading-p)]
  12389. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12390. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12391. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12392. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12393. ["Show TODO Tree" org-show-todo-tree t]
  12394. ["Global TODO list" org-todo-list t]
  12395. "--"
  12396. ["Set Priority" org-priority t]
  12397. ["Priority Up" org-shiftup t]
  12398. ["Priority Down" org-shiftdown t])
  12399. ("TAGS and Properties"
  12400. ["Set Tags" org-set-tags-command t]
  12401. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12402. "--"
  12403. ["Set property" org-set-property t]
  12404. ["Column view of properties" org-columns t]
  12405. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12406. ("Dates and Scheduling"
  12407. ["Timestamp" org-time-stamp t]
  12408. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12409. ("Change Date"
  12410. ["1 Day Later" org-shiftright t]
  12411. ["1 Day Earlier" org-shiftleft t]
  12412. ["1 ... Later" org-shiftup t]
  12413. ["1 ... Earlier" org-shiftdown t])
  12414. ["Compute Time Range" org-evaluate-time-range t]
  12415. ["Schedule Item" org-schedule t]
  12416. ["Deadline" org-deadline t]
  12417. "--"
  12418. ["Custom time format" org-toggle-time-stamp-overlays
  12419. :style radio :selected org-display-custom-times]
  12420. "--"
  12421. ["Goto Calendar" org-goto-calendar t]
  12422. ["Date from Calendar" org-date-from-calendar t]
  12423. "--"
  12424. ["Start/Restart Timer" org-timer-start t]
  12425. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12426. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12427. ["Insert Timer String" org-timer t]
  12428. ["Insert Timer Item" org-timer-item t])
  12429. ("Logging work"
  12430. ["Clock in" org-clock-in t]
  12431. ["Clock out" org-clock-out t]
  12432. ["Clock cancel" org-clock-cancel t]
  12433. ["Goto running clock" org-clock-goto t]
  12434. ["Display times" org-clock-display t]
  12435. ["Create clock table" org-clock-report t]
  12436. "--"
  12437. ["Record DONE time"
  12438. (progn (setq org-log-done (not org-log-done))
  12439. (message "Switching to %s will %s record a timestamp"
  12440. (car org-done-keywords)
  12441. (if org-log-done "automatically" "not")))
  12442. :style toggle :selected org-log-done])
  12443. "--"
  12444. ["Agenda Command..." org-agenda t]
  12445. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12446. ("File List for Agenda")
  12447. ("Special views current file"
  12448. ["TODO Tree" org-show-todo-tree t]
  12449. ["Check Deadlines" org-check-deadlines t]
  12450. ["Timeline" org-timeline t]
  12451. ["Tags Tree" org-tags-sparse-tree t])
  12452. "--"
  12453. ("Hyperlinks"
  12454. ["Store Link (Global)" org-store-link t]
  12455. ["Insert Link" org-insert-link t]
  12456. ["Follow Link" org-open-at-point t]
  12457. "--"
  12458. ["Next link" org-next-link t]
  12459. ["Previous link" org-previous-link t]
  12460. "--"
  12461. ["Descriptive Links"
  12462. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12463. :style radio
  12464. :selected (member '(org-link) buffer-invisibility-spec)]
  12465. ["Literal Links"
  12466. (progn
  12467. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12468. :style radio
  12469. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12470. "--"
  12471. ["Export/Publish..." org-export t]
  12472. ("LaTeX"
  12473. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12474. :selected org-cdlatex-mode]
  12475. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12476. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12477. ["Modify math symbol" org-cdlatex-math-modify
  12478. (org-inside-LaTeX-fragment-p)]
  12479. ["Export LaTeX fragments as images"
  12480. (if (featurep 'org-exp)
  12481. (setq org-export-with-LaTeX-fragments
  12482. (not org-export-with-LaTeX-fragments))
  12483. (require 'org-exp))
  12484. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12485. org-export-with-LaTeX-fragments)])
  12486. "--"
  12487. ("Documentation"
  12488. ["Show Version" org-version t]
  12489. ["Info Documentation" org-info t])
  12490. ("Customize"
  12491. ["Browse Org Group" org-customize t]
  12492. "--"
  12493. ["Expand This Menu" org-create-customize-menu
  12494. (fboundp 'customize-menu-create)])
  12495. "--"
  12496. ["Refresh setup" org-mode-restart t]
  12497. ))
  12498. (defun org-info (&optional node)
  12499. "Read documentation for Org-mode in the info system.
  12500. With optional NODE, go directly to that node."
  12501. (interactive)
  12502. (info (format "(org)%s" (or node ""))))
  12503. (defun org-install-agenda-files-menu ()
  12504. (let ((bl (buffer-list)))
  12505. (save-excursion
  12506. (while bl
  12507. (set-buffer (pop bl))
  12508. (if (org-mode-p) (setq bl nil)))
  12509. (when (org-mode-p)
  12510. (easy-menu-change
  12511. '("Org") "File List for Agenda"
  12512. (append
  12513. (list
  12514. ["Edit File List" (org-edit-agenda-file-list) t]
  12515. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12516. ["Remove Current File from List" org-remove-file t]
  12517. ["Cycle through agenda files" org-cycle-agenda-files t]
  12518. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12519. "--")
  12520. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12521. ;;;; Documentation
  12522. ;;;###autoload
  12523. (defun org-require-autoloaded-modules ()
  12524. (interactive)
  12525. (mapc 'require
  12526. '(org-agenda org-archive org-clock org-colview
  12527. org-exp org-id org-export-latex org-publish
  12528. org-remember org-table)))
  12529. ;;;###autoload
  12530. (defun org-customize ()
  12531. "Call the customize function with org as argument."
  12532. (interactive)
  12533. (org-load-modules-maybe)
  12534. (org-require-autoloaded-modules)
  12535. (customize-browse 'org))
  12536. (defun org-create-customize-menu ()
  12537. "Create a full customization menu for Org-mode, insert it into the menu."
  12538. (interactive)
  12539. (org-load-modules-maybe)
  12540. (org-require-autoloaded-modules)
  12541. (if (fboundp 'customize-menu-create)
  12542. (progn
  12543. (easy-menu-change
  12544. '("Org") "Customize"
  12545. `(["Browse Org group" org-customize t]
  12546. "--"
  12547. ,(customize-menu-create 'org)
  12548. ["Set" Custom-set t]
  12549. ["Save" Custom-save t]
  12550. ["Reset to Current" Custom-reset-current t]
  12551. ["Reset to Saved" Custom-reset-saved t]
  12552. ["Reset to Standard Settings" Custom-reset-standard t]))
  12553. (message "\"Org\"-menu now contains full customization menu"))
  12554. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12555. ;;;; Miscellaneous stuff
  12556. ;;; Generally useful functions
  12557. (defun org-find-text-property-in-string (prop s)
  12558. "Return the first non-nil value of property PROP in string S."
  12559. (or (get-text-property 0 prop s)
  12560. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12561. prop s)))
  12562. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12563. "Display the given MESSAGE as a warning."
  12564. (if (fboundp 'display-warning)
  12565. (display-warning 'org message
  12566. (if (featurep 'xemacs)
  12567. 'warning
  12568. :warning))
  12569. (let ((buf (get-buffer-create "*Org warnings*")))
  12570. (with-current-buffer buf
  12571. (goto-char (point-max))
  12572. (insert "Warning (Org): " message)
  12573. (unless (bolp)
  12574. (newline)))
  12575. (display-buffer buf)
  12576. (sit-for 0))))
  12577. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12578. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12579. (if (and marker (marker-buffer marker)
  12580. (buffer-live-p (marker-buffer marker)))
  12581. (progn
  12582. (switch-to-buffer (marker-buffer marker))
  12583. (if (or (> marker (point-max)) (< marker (point-min)))
  12584. (widen))
  12585. (goto-char marker)
  12586. (org-show-context 'org-goto))
  12587. (if bookmark
  12588. (bookmark-jump bookmark)
  12589. (error "Cannot find location"))))
  12590. (defun org-quote-csv-field (s)
  12591. "Quote field for inclusion in CSV material."
  12592. (if (string-match "[\",]" s)
  12593. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12594. s))
  12595. (defun org-plist-delete (plist property)
  12596. "Delete PROPERTY from PLIST.
  12597. This is in contrast to merely setting it to 0."
  12598. (let (p)
  12599. (while plist
  12600. (if (not (eq property (car plist)))
  12601. (setq p (plist-put p (car plist) (nth 1 plist))))
  12602. (setq plist (cddr plist)))
  12603. p))
  12604. (defun org-force-self-insert (N)
  12605. "Needed to enforce self-insert under remapping."
  12606. (interactive "p")
  12607. (self-insert-command N))
  12608. (defun org-string-width (s)
  12609. "Compute width of string, ignoring invisible characters.
  12610. This ignores character with invisibility property `org-link', and also
  12611. characters with property `org-cwidth', because these will become invisible
  12612. upon the next fontification round."
  12613. (let (b l)
  12614. (when (or (eq t buffer-invisibility-spec)
  12615. (assq 'org-link buffer-invisibility-spec))
  12616. (while (setq b (text-property-any 0 (length s)
  12617. 'invisible 'org-link s))
  12618. (setq s (concat (substring s 0 b)
  12619. (substring s (or (next-single-property-change
  12620. b 'invisible s) (length s)))))))
  12621. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12622. (setq s (concat (substring s 0 b)
  12623. (substring s (or (next-single-property-change
  12624. b 'org-cwidth s) (length s))))))
  12625. (setq l (string-width s) b -1)
  12626. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12627. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12628. l))
  12629. (defun org-get-indentation (&optional line)
  12630. "Get the indentation of the current line, interpreting tabs.
  12631. When LINE is given, assume it represents a line and compute its indentation."
  12632. (if line
  12633. (if (string-match "^ *" (org-remove-tabs line))
  12634. (match-end 0))
  12635. (save-excursion
  12636. (beginning-of-line 1)
  12637. (skip-chars-forward " \t")
  12638. (current-column))))
  12639. (defun org-remove-tabs (s &optional width)
  12640. "Replace tabulators in S with spaces.
  12641. Assumes that s is a single line, starting in column 0."
  12642. (setq width (or width tab-width))
  12643. (while (string-match "\t" s)
  12644. (setq s (replace-match
  12645. (make-string
  12646. (- (* width (/ (+ (match-beginning 0) width) width))
  12647. (match-beginning 0)) ?\ )
  12648. t t s)))
  12649. s)
  12650. (defun org-fix-indentation (line ind)
  12651. "Fix indentation in LINE.
  12652. IND is a cons cell with target and minimum indentation.
  12653. If the current indentation in LINE is smaller than the minimum,
  12654. leave it alone. If it is larger than ind, set it to the target."
  12655. (let* ((l (org-remove-tabs line))
  12656. (i (org-get-indentation l))
  12657. (i1 (car ind)) (i2 (cdr ind)))
  12658. (if (>= i i2) (setq l (substring line i2)))
  12659. (if (> i1 0)
  12660. (concat (make-string i1 ?\ ) l)
  12661. l)))
  12662. (defun org-base-buffer (buffer)
  12663. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12664. (if (not buffer)
  12665. buffer
  12666. (or (buffer-base-buffer buffer)
  12667. buffer)))
  12668. (defun org-trim (s)
  12669. "Remove whitespace at beginning and end of string."
  12670. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12671. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12672. s)
  12673. (defun org-wrap (string &optional width lines)
  12674. "Wrap string to either a number of lines, or a width in characters.
  12675. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12676. that costs. If there is a word longer than WIDTH, the text is actually
  12677. wrapped to the length of that word.
  12678. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12679. many lines, whatever width that takes.
  12680. The return value is a list of lines, without newlines at the end."
  12681. (let* ((words (org-split-string string "[ \t\n]+"))
  12682. (maxword (apply 'max (mapcar 'org-string-width words)))
  12683. w ll)
  12684. (cond (width
  12685. (org-do-wrap words (max maxword width)))
  12686. (lines
  12687. (setq w maxword)
  12688. (setq ll (org-do-wrap words maxword))
  12689. (if (<= (length ll) lines)
  12690. ll
  12691. (setq ll words)
  12692. (while (> (length ll) lines)
  12693. (setq w (1+ w))
  12694. (setq ll (org-do-wrap words w)))
  12695. ll))
  12696. (t (error "Cannot wrap this")))))
  12697. (defun org-do-wrap (words width)
  12698. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12699. (let (lines line)
  12700. (while words
  12701. (setq line (pop words))
  12702. (while (and words (< (+ (length line) (length (car words))) width))
  12703. (setq line (concat line " " (pop words))))
  12704. (setq lines (push line lines)))
  12705. (nreverse lines)))
  12706. (defun org-split-string (string &optional separators)
  12707. "Splits STRING into substrings at SEPARATORS.
  12708. No empty strings are returned if there are matches at the beginning
  12709. and end of string."
  12710. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12711. (start 0)
  12712. notfirst
  12713. (list nil))
  12714. (while (and (string-match rexp string
  12715. (if (and notfirst
  12716. (= start (match-beginning 0))
  12717. (< start (length string)))
  12718. (1+ start) start))
  12719. (< (match-beginning 0) (length string)))
  12720. (setq notfirst t)
  12721. (or (eq (match-beginning 0) 0)
  12722. (and (eq (match-beginning 0) (match-end 0))
  12723. (eq (match-beginning 0) start))
  12724. (setq list
  12725. (cons (substring string start (match-beginning 0))
  12726. list)))
  12727. (setq start (match-end 0)))
  12728. (or (eq start (length string))
  12729. (setq list
  12730. (cons (substring string start)
  12731. list)))
  12732. (nreverse list)))
  12733. (defun org-context ()
  12734. "Return a list of contexts of the current cursor position.
  12735. If several contexts apply, all are returned.
  12736. Each context entry is a list with a symbol naming the context, and
  12737. two positions indicating start and end of the context. Possible
  12738. contexts are:
  12739. :headline anywhere in a headline
  12740. :headline-stars on the leading stars in a headline
  12741. :todo-keyword on a TODO keyword (including DONE) in a headline
  12742. :tags on the TAGS in a headline
  12743. :priority on the priority cookie in a headline
  12744. :item on the first line of a plain list item
  12745. :item-bullet on the bullet/number of a plain list item
  12746. :checkbox on the checkbox in a plain list item
  12747. :table in an org-mode table
  12748. :table-special on a special filed in a table
  12749. :table-table in a table.el table
  12750. :link on a hyperlink
  12751. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12752. :target on a <<target>>
  12753. :radio-target on a <<<radio-target>>>
  12754. :latex-fragment on a LaTeX fragment
  12755. :latex-preview on a LaTeX fragment with overlayed preview image
  12756. This function expects the position to be visible because it uses font-lock
  12757. faces as a help to recognize the following contexts: :table-special, :link,
  12758. and :keyword."
  12759. (let* ((f (get-text-property (point) 'face))
  12760. (faces (if (listp f) f (list f)))
  12761. (p (point)) clist o)
  12762. ;; First the large context
  12763. (cond
  12764. ((org-on-heading-p t)
  12765. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12766. (when (progn
  12767. (beginning-of-line 1)
  12768. (looking-at org-todo-line-tags-regexp))
  12769. (push (org-point-in-group p 1 :headline-stars) clist)
  12770. (push (org-point-in-group p 2 :todo-keyword) clist)
  12771. (push (org-point-in-group p 4 :tags) clist))
  12772. (goto-char p)
  12773. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12774. (if (looking-at "\\[#[A-Z0-9]\\]")
  12775. (push (org-point-in-group p 0 :priority) clist)))
  12776. ((org-at-item-p)
  12777. (push (org-point-in-group p 2 :item-bullet) clist)
  12778. (push (list :item (point-at-bol)
  12779. (save-excursion (org-end-of-item) (point)))
  12780. clist)
  12781. (and (org-at-item-checkbox-p)
  12782. (push (org-point-in-group p 0 :checkbox) clist)))
  12783. ((org-at-table-p)
  12784. (push (list :table (org-table-begin) (org-table-end)) clist)
  12785. (if (memq 'org-formula faces)
  12786. (push (list :table-special
  12787. (previous-single-property-change p 'face)
  12788. (next-single-property-change p 'face)) clist)))
  12789. ((org-at-table-p 'any)
  12790. (push (list :table-table) clist)))
  12791. (goto-char p)
  12792. ;; Now the small context
  12793. (cond
  12794. ((org-at-timestamp-p)
  12795. (push (org-point-in-group p 0 :timestamp) clist))
  12796. ((memq 'org-link faces)
  12797. (push (list :link
  12798. (previous-single-property-change p 'face)
  12799. (next-single-property-change p 'face)) clist))
  12800. ((memq 'org-special-keyword faces)
  12801. (push (list :keyword
  12802. (previous-single-property-change p 'face)
  12803. (next-single-property-change p 'face)) clist))
  12804. ((org-on-target-p)
  12805. (push (org-point-in-group p 0 :target) clist)
  12806. (goto-char (1- (match-beginning 0)))
  12807. (if (looking-at org-radio-target-regexp)
  12808. (push (org-point-in-group p 0 :radio-target) clist))
  12809. (goto-char p))
  12810. ((setq o (car (delq nil
  12811. (mapcar
  12812. (lambda (x)
  12813. (if (memq x org-latex-fragment-image-overlays) x))
  12814. (org-overlays-at (point))))))
  12815. (push (list :latex-fragment
  12816. (org-overlay-start o) (org-overlay-end o)) clist)
  12817. (push (list :latex-preview
  12818. (org-overlay-start o) (org-overlay-end o)) clist))
  12819. ((org-inside-LaTeX-fragment-p)
  12820. ;; FIXME: positions wrong.
  12821. (push (list :latex-fragment (point) (point)) clist)))
  12822. (setq clist (nreverse (delq nil clist)))
  12823. clist))
  12824. ;; FIXME: Compare with at-regexp-p Do we need both?
  12825. (defun org-in-regexp (re &optional nlines visually)
  12826. "Check if point is inside a match of regexp.
  12827. Normally only the current line is checked, but you can include NLINES extra
  12828. lines both before and after point into the search.
  12829. If VISUALLY is set, require that the cursor is not after the match but
  12830. really on, so that the block visually is on the match."
  12831. (catch 'exit
  12832. (let ((pos (point))
  12833. (eol (point-at-eol (+ 1 (or nlines 0))))
  12834. (inc (if visually 1 0)))
  12835. (save-excursion
  12836. (beginning-of-line (- 1 (or nlines 0)))
  12837. (while (re-search-forward re eol t)
  12838. (if (and (<= (match-beginning 0) pos)
  12839. (>= (+ inc (match-end 0)) pos))
  12840. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12841. (defun org-at-regexp-p (regexp)
  12842. "Is point inside a match of REGEXP in the current line?"
  12843. (catch 'exit
  12844. (save-excursion
  12845. (let ((pos (point)) (end (point-at-eol)))
  12846. (beginning-of-line 1)
  12847. (while (re-search-forward regexp end t)
  12848. (if (and (<= (match-beginning 0) pos)
  12849. (>= (match-end 0) pos))
  12850. (throw 'exit t)))
  12851. nil))))
  12852. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12853. "Call `multi-occur' with buffers for all agenda files."
  12854. (interactive "sOrg-files matching: \np")
  12855. (let* ((files (org-agenda-files))
  12856. (tnames (mapcar 'file-truename files))
  12857. (extra org-agenda-text-search-extra-files)
  12858. f)
  12859. (when (eq (car extra) 'agenda-archives)
  12860. (setq extra (cdr extra))
  12861. (setq files (org-add-archive-files files)))
  12862. (while (setq f (pop extra))
  12863. (unless (member (file-truename f) tnames)
  12864. (add-to-list 'files f 'append)
  12865. (add-to-list 'tnames (file-truename f) 'append)))
  12866. (multi-occur
  12867. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12868. regexp)))
  12869. (if (boundp 'occur-mode-find-occurrence-hook)
  12870. ;; Emacs 23
  12871. (add-hook 'occur-mode-find-occurrence-hook
  12872. (lambda ()
  12873. (when (org-mode-p)
  12874. (org-reveal))))
  12875. ;; Emacs 22
  12876. (defadvice occur-mode-goto-occurrence
  12877. (after org-occur-reveal activate)
  12878. (and (org-mode-p) (org-reveal)))
  12879. (defadvice occur-mode-goto-occurrence-other-window
  12880. (after org-occur-reveal activate)
  12881. (and (org-mode-p) (org-reveal)))
  12882. (defadvice occur-mode-display-occurrence
  12883. (after org-occur-reveal activate)
  12884. (when (org-mode-p)
  12885. (let ((pos (occur-mode-find-occurrence)))
  12886. (with-current-buffer (marker-buffer pos)
  12887. (save-excursion
  12888. (goto-char pos)
  12889. (org-reveal)))))))
  12890. (defun org-uniquify (list)
  12891. "Remove duplicate elements from LIST."
  12892. (let (res)
  12893. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12894. res))
  12895. (defun org-delete-all (elts list)
  12896. "Remove all elements in ELTS from LIST."
  12897. (while elts
  12898. (setq list (delete (pop elts) list)))
  12899. list)
  12900. (defun org-back-over-empty-lines ()
  12901. "Move backwards over whitespace, to the beginning of the first empty line.
  12902. Returns the number of empty lines passed."
  12903. (let ((pos (point)))
  12904. (skip-chars-backward " \t\n\r")
  12905. (beginning-of-line 2)
  12906. (goto-char (min (point) pos))
  12907. (count-lines (point) pos)))
  12908. (defun org-skip-whitespace ()
  12909. (skip-chars-forward " \t\n\r"))
  12910. (defun org-point-in-group (point group &optional context)
  12911. "Check if POINT is in match-group GROUP.
  12912. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12913. match. If the match group does ot exist or point is not inside it,
  12914. return nil."
  12915. (and (match-beginning group)
  12916. (>= point (match-beginning group))
  12917. (<= point (match-end group))
  12918. (if context
  12919. (list context (match-beginning group) (match-end group))
  12920. t)))
  12921. (defun org-switch-to-buffer-other-window (&rest args)
  12922. "Switch to buffer in a second window on the current frame.
  12923. In particular, do not allow pop-up frames."
  12924. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12925. special-display-function)
  12926. (apply 'switch-to-buffer-other-window args)))
  12927. (defun org-combine-plists (&rest plists)
  12928. "Create a single property list from all plists in PLISTS.
  12929. The process starts by copying the first list, and then setting properties
  12930. from the other lists. Settings in the last list are the most significant
  12931. ones and overrule settings in the other lists."
  12932. (let ((rtn (copy-sequence (pop plists)))
  12933. p v ls)
  12934. (while plists
  12935. (setq ls (pop plists))
  12936. (while ls
  12937. (setq p (pop ls) v (pop ls))
  12938. (setq rtn (plist-put rtn p v))))
  12939. rtn))
  12940. (defun org-move-line-down (arg)
  12941. "Move the current line down. With prefix argument, move it past ARG lines."
  12942. (interactive "p")
  12943. (let ((col (current-column))
  12944. beg end pos)
  12945. (beginning-of-line 1) (setq beg (point))
  12946. (beginning-of-line 2) (setq end (point))
  12947. (beginning-of-line (+ 1 arg))
  12948. (setq pos (move-marker (make-marker) (point)))
  12949. (insert (delete-and-extract-region beg end))
  12950. (goto-char pos)
  12951. (org-move-to-column col)))
  12952. (defun org-move-line-up (arg)
  12953. "Move the current line up. With prefix argument, move it past ARG lines."
  12954. (interactive "p")
  12955. (let ((col (current-column))
  12956. beg end pos)
  12957. (beginning-of-line 1) (setq beg (point))
  12958. (beginning-of-line 2) (setq end (point))
  12959. (beginning-of-line (- arg))
  12960. (setq pos (move-marker (make-marker) (point)))
  12961. (insert (delete-and-extract-region beg end))
  12962. (goto-char pos)
  12963. (org-move-to-column col)))
  12964. (defun org-replace-escapes (string table)
  12965. "Replace %-escapes in STRING with values in TABLE.
  12966. TABLE is an association list with keys like \"%a\" and string values.
  12967. The sequences in STRING may contain normal field width and padding information,
  12968. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12969. so values can contain further %-escapes if they are define later in TABLE."
  12970. (let ((case-fold-search nil)
  12971. e re rpl)
  12972. (while (setq e (pop table))
  12973. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12974. (while (string-match re string)
  12975. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12976. (cdr e)))
  12977. (setq string (replace-match rpl t t string))))
  12978. string))
  12979. (defun org-sublist (list start end)
  12980. "Return a section of LIST, from START to END.
  12981. Counting starts at 1."
  12982. (let (rtn (c start))
  12983. (setq list (nthcdr (1- start) list))
  12984. (while (and list (<= c end))
  12985. (push (pop list) rtn)
  12986. (setq c (1+ c)))
  12987. (nreverse rtn)))
  12988. (defun org-find-base-buffer-visiting (file)
  12989. "Like `find-buffer-visiting' but alway return the base buffer and
  12990. not an indirect buffer."
  12991. (let ((buf (find-buffer-visiting file)))
  12992. (if buf
  12993. (or (buffer-base-buffer buf) buf)
  12994. nil)))
  12995. (defun org-image-file-name-regexp (&optional extensions)
  12996. "Return regexp matching the file names of images.
  12997. If EXTENSIONS is given, only match these."
  12998. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  12999. (image-file-name-regexp)
  13000. (let ((image-file-name-extensions
  13001. (or extensions
  13002. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13003. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13004. (concat "\\."
  13005. (regexp-opt (nconc (mapcar 'upcase
  13006. image-file-name-extensions)
  13007. image-file-name-extensions)
  13008. t)
  13009. "\\'"))))
  13010. (defun org-file-image-p (file &optional extensions)
  13011. "Return non-nil if FILE is an image."
  13012. (save-match-data
  13013. (string-match (org-image-file-name-regexp extensions) file)))
  13014. (defun org-get-cursor-date ()
  13015. "Return the date at cursor in as a time.
  13016. This works in the calendar and in the agenda, anywhere else it just
  13017. returns the current time."
  13018. (let (date day defd)
  13019. (cond
  13020. ((eq major-mode 'calendar-mode)
  13021. (setq date (calendar-cursor-to-date)
  13022. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13023. ((eq major-mode 'org-agenda-mode)
  13024. (setq day (get-text-property (point) 'day))
  13025. (if day
  13026. (setq date (calendar-gregorian-from-absolute day)
  13027. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13028. (nth 2 date))))))
  13029. (or defd (current-time))))
  13030. (defvar org-agenda-action-marker (make-marker)
  13031. "Marker pointing to the entry for the next agenda action.")
  13032. (defun org-mark-entry-for-agenda-action ()
  13033. "Mark the current entry as target of an agenda action.
  13034. Agenda actions are actions executed from the agenda with the key `k',
  13035. which make use of the date at the cursor."
  13036. (interactive)
  13037. (move-marker org-agenda-action-marker
  13038. (save-excursion (org-back-to-heading t) (point))
  13039. (current-buffer))
  13040. (message
  13041. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13042. ;;; Paragraph filling stuff.
  13043. ;; We want this to be just right, so use the full arsenal.
  13044. (defun org-indent-line-function ()
  13045. "Indent line like previous, but further if previous was headline or item."
  13046. (interactive)
  13047. (let* ((pos (point))
  13048. (itemp (org-at-item-p))
  13049. column bpos bcol tpos tcol bullet btype bullet-type)
  13050. ;; Find the previous relevant line
  13051. (beginning-of-line 1)
  13052. (cond
  13053. ((looking-at "#") (setq column 0))
  13054. ((looking-at "\\*+ ") (setq column 0))
  13055. (t
  13056. (beginning-of-line 0)
  13057. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13058. (beginning-of-line 0))
  13059. (cond
  13060. ((looking-at "\\*+[ \t]+")
  13061. (if (not org-adapt-indentation)
  13062. (setq column 0)
  13063. (goto-char (match-end 0))
  13064. (setq column (current-column))))
  13065. ((org-in-item-p)
  13066. (org-beginning-of-item)
  13067. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13068. (setq bpos (match-beginning 1) tpos (match-end 0)
  13069. bcol (progn (goto-char bpos) (current-column))
  13070. tcol (progn (goto-char tpos) (current-column))
  13071. bullet (match-string 1)
  13072. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13073. (if (> tcol (+ bcol org-description-max-indent))
  13074. (setq tcol (+ bcol 5)))
  13075. (if (not itemp)
  13076. (setq column tcol)
  13077. (goto-char pos)
  13078. (beginning-of-line 1)
  13079. (if (looking-at "\\S-")
  13080. (progn
  13081. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13082. (setq bullet (match-string 1)
  13083. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13084. (setq column (if (equal btype bullet-type) bcol tcol)))
  13085. (setq column (org-get-indentation)))))
  13086. (t (setq column (org-get-indentation))))))
  13087. (goto-char pos)
  13088. (if (<= (current-column) (current-indentation))
  13089. (org-indent-line-to column)
  13090. (save-excursion (org-indent-line-to column)))
  13091. (setq column (current-column))
  13092. (beginning-of-line 1)
  13093. (if (looking-at
  13094. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13095. (replace-match (concat "\\1" (format org-property-format
  13096. (match-string 2) (match-string 3)))
  13097. t nil))
  13098. (org-move-to-column column)))
  13099. (defun org-set-autofill-regexps ()
  13100. (interactive)
  13101. ;; In the paragraph separator we include headlines, because filling
  13102. ;; text in a line directly attached to a headline would otherwise
  13103. ;; fill the headline as well.
  13104. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13105. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13106. ;; The paragraph starter includes hand-formatted lists.
  13107. (org-set-local 'paragraph-start
  13108. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13109. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13110. ;; But only if the user has not turned off tables or fixed-width regions
  13111. (org-set-local
  13112. 'auto-fill-inhibit-regexp
  13113. (concat "\\*+ \\|#\\+"
  13114. "\\|[ \t]*" org-keyword-time-regexp
  13115. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13116. (concat
  13117. "\\|[ \t]*["
  13118. (if org-enable-table-editor "|" "")
  13119. (if org-enable-fixed-width-editor ":" "")
  13120. "]"))))
  13121. ;; We use our own fill-paragraph function, to make sure that tables
  13122. ;; and fixed-width regions are not wrapped. That function will pass
  13123. ;; through to `fill-paragraph' when appropriate.
  13124. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13125. ; Adaptive filling: To get full control, first make sure that
  13126. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13127. (org-set-local 'adaptive-fill-regexp "\000")
  13128. (org-set-local 'adaptive-fill-function
  13129. 'org-adaptive-fill-function)
  13130. (org-set-local
  13131. 'align-mode-rules-list
  13132. '((org-in-buffer-settings
  13133. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13134. (modes . '(org-mode))))))
  13135. (defun org-fill-paragraph (&optional justify)
  13136. "Re-align a table, pass through to fill-paragraph if no table."
  13137. (let ((table-p (org-at-table-p))
  13138. (table.el-p (org-at-table.el-p)))
  13139. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13140. (save-excursion (goto-char (point-at-bol))
  13141. (looking-at outline-regexp)))
  13142. t) ; skip headlines
  13143. (table.el-p t) ; skip table.el tables
  13144. (table-p (org-table-align) t) ; align org-mode tables
  13145. (t nil)))) ; call paragraph-fill
  13146. ;; For reference, this is the default value of adaptive-fill-regexp
  13147. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13148. (defun org-adaptive-fill-function ()
  13149. "Return a fill prefix for org-mode files.
  13150. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13151. work correctly."
  13152. (cond ((looking-at "#[ \t]+")
  13153. (match-string 0))
  13154. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13155. (save-excursion
  13156. (if (> (match-end 1) (+ (match-beginning 1)
  13157. org-description-max-indent))
  13158. (goto-char (+ (match-beginning 1) 5))
  13159. (goto-char (match-end 0)))
  13160. (make-string (current-column) ?\ )))
  13161. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13162. (save-excursion
  13163. (goto-char (match-end 0))
  13164. (make-string (current-column) ?\ )))
  13165. (t nil)))
  13166. ;;; Other stuff.
  13167. (defun org-toggle-fixed-width-section (arg)
  13168. "Toggle the fixed-width export.
  13169. If there is no active region, the QUOTE keyword at the current headline is
  13170. inserted or removed. When present, it causes the text between this headline
  13171. and the next to be exported as fixed-width text, and unmodified.
  13172. If there is an active region, this command adds or removes a colon as the
  13173. first character of this line. If the first character of a line is a colon,
  13174. this line is also exported in fixed-width font."
  13175. (interactive "P")
  13176. (let* ((cc 0)
  13177. (regionp (org-region-active-p))
  13178. (beg (if regionp (region-beginning) (point)))
  13179. (end (if regionp (region-end)))
  13180. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13181. (case-fold-search nil)
  13182. (re "[ \t]*\\(:\\)")
  13183. off)
  13184. (if regionp
  13185. (save-excursion
  13186. (goto-char beg)
  13187. (setq cc (current-column))
  13188. (beginning-of-line 1)
  13189. (setq off (looking-at re))
  13190. (while (> nlines 0)
  13191. (setq nlines (1- nlines))
  13192. (beginning-of-line 1)
  13193. (cond
  13194. (arg
  13195. (org-move-to-column cc t)
  13196. (insert ":\n")
  13197. (forward-line -1))
  13198. ((and off (looking-at re))
  13199. (replace-match "" t t nil 1))
  13200. ((not off) (org-move-to-column cc t) (insert ":")))
  13201. (forward-line 1)))
  13202. (save-excursion
  13203. (org-back-to-heading)
  13204. (if (looking-at (concat outline-regexp
  13205. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13206. (replace-match "" t t nil 1)
  13207. (if (looking-at outline-regexp)
  13208. (progn
  13209. (goto-char (match-end 0))
  13210. (insert org-quote-string " "))))))))
  13211. ;;;; Functions extending outline functionality
  13212. (defun org-beginning-of-line (&optional arg)
  13213. "Go to the beginning of the current line. If that is invisible, continue
  13214. to a visible line beginning. This makes the function of C-a more intuitive.
  13215. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13216. first attempt, and only move to after the tags when the cursor is already
  13217. beyond the end of the headline."
  13218. (interactive "P")
  13219. (let ((pos (point)) refpos)
  13220. (beginning-of-line 1)
  13221. (if (bobp)
  13222. nil
  13223. (backward-char 1)
  13224. (if (org-invisible-p)
  13225. (while (and (not (bobp)) (org-invisible-p))
  13226. (backward-char 1)
  13227. (beginning-of-line 1))
  13228. (forward-char 1)))
  13229. (when org-special-ctrl-a/e
  13230. (cond
  13231. ((and (looking-at org-complex-heading-regexp)
  13232. (= (char-after (match-end 1)) ?\ ))
  13233. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13234. (point-at-eol)))
  13235. (goto-char
  13236. (if (eq org-special-ctrl-a/e t)
  13237. (cond ((> pos refpos) refpos)
  13238. ((= pos (point)) refpos)
  13239. (t (point)))
  13240. (cond ((> pos (point)) (point))
  13241. ((not (eq last-command this-command)) (point))
  13242. (t refpos)))))
  13243. ((org-at-item-p)
  13244. (goto-char
  13245. (if (eq org-special-ctrl-a/e t)
  13246. (cond ((> pos (match-end 4)) (match-end 4))
  13247. ((= pos (point)) (match-end 4))
  13248. (t (point)))
  13249. (cond ((> pos (point)) (point))
  13250. ((not (eq last-command this-command)) (point))
  13251. (t (match-end 4))))))))
  13252. (org-no-warnings
  13253. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13254. (defun org-end-of-line (&optional arg)
  13255. "Go to the end of the line.
  13256. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13257. first attempt, and only move to after the tags when the cursor is already
  13258. beyond the end of the headline."
  13259. (interactive "P")
  13260. (if (or (not org-special-ctrl-a/e)
  13261. (not (org-on-heading-p)))
  13262. (end-of-line arg)
  13263. (let ((pos (point)))
  13264. (beginning-of-line 1)
  13265. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13266. (if (eq org-special-ctrl-a/e t)
  13267. (if (or (< pos (match-beginning 1))
  13268. (= pos (match-end 0)))
  13269. (goto-char (match-beginning 1))
  13270. (goto-char (match-end 0)))
  13271. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13272. (goto-char (match-end 0))
  13273. (goto-char (match-beginning 1))))
  13274. (end-of-line arg))))
  13275. (org-no-warnings
  13276. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13277. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13278. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13279. (defun org-kill-line (&optional arg)
  13280. "Kill line, to tags or end of line."
  13281. (interactive "P")
  13282. (cond
  13283. ((or (not org-special-ctrl-k)
  13284. (bolp)
  13285. (not (org-on-heading-p)))
  13286. (call-interactively 'kill-line))
  13287. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13288. (kill-region (point) (match-beginning 1))
  13289. (org-set-tags nil t))
  13290. (t (kill-region (point) (point-at-eol)))))
  13291. (define-key org-mode-map "\C-k" 'org-kill-line)
  13292. (defun org-yank (&optional arg)
  13293. "Yank. If the kill is a subtree, treat it specially.
  13294. This command will look at the current kill and check if is a single
  13295. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13296. cursor is at the beginning of a line or after the stars of a currently
  13297. empty headline, then the yank is handled specially. How exactly depends
  13298. on the value of the following variables, both set by default.
  13299. org-yank-folded-subtrees
  13300. When set, the subtree(s) will be folded after insertion, but only
  13301. if doing so would now swallow text after the yanked text.
  13302. org-yank-adjusted-subtrees
  13303. When set, the subtree will be promoted or demoted in order to
  13304. fit into the local outline tree structure, which means that the level
  13305. will be adjusted so that it becomes the smaller one of the two
  13306. *visible* surrounding headings.
  13307. Any prefix to this command will cause `yank' to be called directly with
  13308. no special treatment. In particular, a simple `C-u' prefix will just
  13309. plainly yank the text as it is.
  13310. \[1] Basically, the test checks if the first non-white line is a heading
  13311. and if there are no other headings with fewer stars."
  13312. (interactive "P")
  13313. (setq this-command 'yank)
  13314. (if arg
  13315. (call-interactively 'yank)
  13316. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13317. (and (org-kill-is-subtree-p)
  13318. (or (bolp)
  13319. (and (looking-at "[ \t]*$")
  13320. (string-match
  13321. "\\`\\*+\\'"
  13322. (buffer-substring (point-at-bol) (point)))))))
  13323. swallowp)
  13324. (cond
  13325. ((and subtreep org-yank-folded-subtrees)
  13326. (let ((beg (point))
  13327. end)
  13328. (if (and subtreep org-yank-adjusted-subtrees)
  13329. (org-paste-subtree nil nil 'for-yank)
  13330. (call-interactively 'yank))
  13331. (setq end (point))
  13332. (goto-char beg)
  13333. (when (and (bolp) subtreep
  13334. (not (setq swallowp
  13335. (org-yank-folding-would-swallow-text beg end))))
  13336. (or (looking-at outline-regexp)
  13337. (re-search-forward (concat "^" outline-regexp) end t))
  13338. (while (and (< (point) end) (looking-at outline-regexp))
  13339. (hide-subtree)
  13340. (org-cycle-show-empty-lines 'folded)
  13341. (condition-case nil
  13342. (outline-forward-same-level 1)
  13343. (error (goto-char end)))))
  13344. (when swallowp
  13345. (message
  13346. "Yanked text not folded because that would swallow text"))
  13347. (goto-char end)
  13348. (skip-chars-forward " \t\n\r")
  13349. (beginning-of-line 1)
  13350. (push-mark beg 'nomsg)))
  13351. ((and subtreep org-yank-adjusted-subtrees)
  13352. (let ((beg (point-at-bol)))
  13353. (org-paste-subtree nil nil 'for-yank)
  13354. (push-mark beg 'nomsg)))
  13355. (t
  13356. (call-interactively 'yank))))))
  13357. (defun org-yank-folding-would-swallow-text (beg end)
  13358. "Would hide-subtree at BEG swallow any text after END?"
  13359. (let (level)
  13360. (save-excursion
  13361. (goto-char beg)
  13362. (when (or (looking-at outline-regexp)
  13363. (re-search-forward (concat "^" outline-regexp) end t))
  13364. (setq level (org-outline-level)))
  13365. (goto-char end)
  13366. (skip-chars-forward " \t\r\n\v\f")
  13367. (if (or (eobp)
  13368. (and (bolp) (looking-at org-outline-regexp)
  13369. (<= (org-outline-level) level)))
  13370. nil ; Nothing would be swallowed
  13371. t)))) ; something would swallow
  13372. (define-key org-mode-map "\C-y" 'org-yank)
  13373. (defun org-invisible-p ()
  13374. "Check if point is at a character currently not visible."
  13375. ;; Early versions of noutline don't have `outline-invisible-p'.
  13376. (if (fboundp 'outline-invisible-p)
  13377. (outline-invisible-p)
  13378. (get-char-property (point) 'invisible)))
  13379. (defun org-invisible-p2 ()
  13380. "Check if point is at a character currently not visible."
  13381. (save-excursion
  13382. (if (and (eolp) (not (bobp))) (backward-char 1))
  13383. ;; Early versions of noutline don't have `outline-invisible-p'.
  13384. (if (fboundp 'outline-invisible-p)
  13385. (outline-invisible-p)
  13386. (get-char-property (point) 'invisible))))
  13387. (defun org-back-to-heading (&optional invisible-ok)
  13388. "Call `outline-back-to-heading', but provide a better error message."
  13389. (condition-case nil
  13390. (outline-back-to-heading invisible-ok)
  13391. (error (error "Before first headline at position %d in buffer %s"
  13392. (point) (current-buffer)))))
  13393. (defun org-before-first-heading-p ()
  13394. "Before first heading?"
  13395. (save-excursion
  13396. (null (re-search-backward "^\\*+ " nil t))))
  13397. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13398. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13399. (defun org-at-heading-or-item-p ()
  13400. (or (org-on-heading-p) (org-at-item-p)))
  13401. (defun org-on-target-p ()
  13402. (or (org-in-regexp org-radio-target-regexp)
  13403. (org-in-regexp org-target-regexp)))
  13404. (defun org-up-heading-all (arg)
  13405. "Move to the heading line of which the present line is a subheading.
  13406. This function considers both visible and invisible heading lines.
  13407. With argument, move up ARG levels."
  13408. (if (fboundp 'outline-up-heading-all)
  13409. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13410. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13411. (defun org-up-heading-safe ()
  13412. "Move to the heading line of which the present line is a subheading.
  13413. This version will not throw an error. It will return the level of the
  13414. headline found, or nil if no higher level is found."
  13415. (let (start-level re)
  13416. (org-back-to-heading t)
  13417. (setq start-level (funcall outline-level))
  13418. (if (equal start-level 1)
  13419. nil
  13420. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13421. (if (re-search-backward re nil t)
  13422. (funcall outline-level)))))
  13423. (defun org-first-sibling-p ()
  13424. "Is this heading the first child of its parents?"
  13425. (interactive)
  13426. (let ((re (concat "^" outline-regexp))
  13427. level l)
  13428. (unless (org-at-heading-p t)
  13429. (error "Not at a heading"))
  13430. (setq level (funcall outline-level))
  13431. (save-excursion
  13432. (if (not (re-search-backward re nil t))
  13433. t
  13434. (setq l (funcall outline-level))
  13435. (< l level)))))
  13436. (defun org-goto-sibling (&optional previous)
  13437. "Goto the next sibling, even if it is invisible.
  13438. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13439. when a sibling was found. When none is found, return nil and don't
  13440. move point."
  13441. (let ((fun (if previous 're-search-backward 're-search-forward))
  13442. (pos (point))
  13443. (re (concat "^" outline-regexp))
  13444. level l)
  13445. (when (condition-case nil (org-back-to-heading t) (error nil))
  13446. (setq level (funcall outline-level))
  13447. (catch 'exit
  13448. (or previous (forward-char 1))
  13449. (while (funcall fun re nil t)
  13450. (setq l (funcall outline-level))
  13451. (when (< l level) (goto-char pos) (throw 'exit nil))
  13452. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13453. (goto-char pos)
  13454. nil))))
  13455. (defun org-show-siblings ()
  13456. "Show all siblings of the current headline."
  13457. (save-excursion
  13458. (while (org-goto-sibling) (org-flag-heading nil)))
  13459. (save-excursion
  13460. (while (org-goto-sibling 'previous)
  13461. (org-flag-heading nil))))
  13462. (defun org-show-hidden-entry ()
  13463. "Show an entry where even the heading is hidden."
  13464. (save-excursion
  13465. (org-show-entry)))
  13466. (defun org-flag-heading (flag &optional entry)
  13467. "Flag the current heading. FLAG non-nil means make invisible.
  13468. When ENTRY is non-nil, show the entire entry."
  13469. (save-excursion
  13470. (org-back-to-heading t)
  13471. ;; Check if we should show the entire entry
  13472. (if entry
  13473. (progn
  13474. (org-show-entry)
  13475. (save-excursion
  13476. (and (outline-next-heading)
  13477. (org-flag-heading nil))))
  13478. (outline-flag-region (max (point-min) (1- (point)))
  13479. (save-excursion (outline-end-of-heading) (point))
  13480. flag))))
  13481. (defun org-forward-same-level (arg)
  13482. "Move forward to the ARG'th subheading at same level as this one.
  13483. Stop at the first and last subheadings of a superior heading.
  13484. This is like outline-forward-same-level, but invisible headings are ok."
  13485. (interactive "p")
  13486. (org-back-to-heading t)
  13487. (while (> arg 0)
  13488. (let ((point-to-move-to (save-excursion
  13489. (org-get-next-sibling))))
  13490. (if point-to-move-to
  13491. (progn
  13492. (goto-char point-to-move-to)
  13493. (setq arg (1- arg)))
  13494. (progn
  13495. (setq arg 0)
  13496. (error "No following same-level heading"))))))
  13497. (defun org-get-next-sibling ()
  13498. "Move to next heading of the same level, and return point.
  13499. If there is no such heading, return nil.
  13500. This is like outline-next-sibling, but invisible headings are ok."
  13501. (let ((level (funcall outline-level)))
  13502. (outline-next-heading)
  13503. (while (and (not (eobp)) (> (funcall outline-level) level))
  13504. (outline-next-heading))
  13505. (if (or (eobp) (< (funcall outline-level) level))
  13506. nil
  13507. (point))))
  13508. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13509. ;; This is an exact copy of the original function, but it uses
  13510. ;; `org-back-to-heading', to make it work also in invisible
  13511. ;; trees. And is uses an invisible-OK argument.
  13512. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13513. (org-back-to-heading invisible-OK)
  13514. (let ((first t)
  13515. (level (funcall outline-level)))
  13516. (while (and (not (eobp))
  13517. (or first (> (funcall outline-level) level)))
  13518. (setq first nil)
  13519. (outline-next-heading))
  13520. (unless to-heading
  13521. (if (memq (preceding-char) '(?\n ?\^M))
  13522. (progn
  13523. ;; Go to end of line before heading
  13524. (forward-char -1)
  13525. (if (memq (preceding-char) '(?\n ?\^M))
  13526. ;; leave blank line before heading
  13527. (forward-char -1))))))
  13528. (point))
  13529. (defun org-show-subtree ()
  13530. "Show everything after this heading at deeper levels."
  13531. (outline-flag-region
  13532. (point)
  13533. (save-excursion
  13534. (outline-end-of-subtree) (outline-next-heading) (point))
  13535. nil))
  13536. (defun org-show-entry ()
  13537. "Show the body directly following this heading.
  13538. Show the heading too, if it is currently invisible."
  13539. (interactive)
  13540. (save-excursion
  13541. (condition-case nil
  13542. (progn
  13543. (org-back-to-heading t)
  13544. (outline-flag-region
  13545. (max (point-min) (1- (point)))
  13546. (save-excursion
  13547. (re-search-forward
  13548. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13549. (or (match-beginning 1) (point-max)))
  13550. nil))
  13551. (error nil))))
  13552. (defun org-make-options-regexp (kwds)
  13553. "Make a regular expression for keyword lines."
  13554. (concat
  13555. "^"
  13556. "#?[ \t]*\\+\\("
  13557. (mapconcat 'regexp-quote kwds "\\|")
  13558. "\\):[ \t]*"
  13559. "\\(.+\\)"))
  13560. ;; Make isearch reveal the necessary context
  13561. (defun org-isearch-end ()
  13562. "Reveal context after isearch exits."
  13563. (when isearch-success ; only if search was successful
  13564. (if (featurep 'xemacs)
  13565. ;; Under XEmacs, the hook is run in the correct place,
  13566. ;; we directly show the context.
  13567. (org-show-context 'isearch)
  13568. ;; In Emacs the hook runs *before* restoring the overlays.
  13569. ;; So we have to use a one-time post-command-hook to do this.
  13570. ;; (Emacs 22 has a special variable, see function `org-mode')
  13571. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13572. isearch-mode-end-hook-quit)
  13573. ;; Only when the isearch was not quitted.
  13574. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13575. 'append 'local)))))
  13576. (defun org-isearch-post-command ()
  13577. "Remove self from hook, and show context."
  13578. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13579. (org-show-context 'isearch))
  13580. ;;;; Integration with and fixes for other packages
  13581. ;;; Imenu support
  13582. (defvar org-imenu-markers nil
  13583. "All markers currently used by Imenu.")
  13584. (make-variable-buffer-local 'org-imenu-markers)
  13585. (defun org-imenu-new-marker (&optional pos)
  13586. "Return a new marker for use by Imenu, and remember the marker."
  13587. (let ((m (make-marker)))
  13588. (move-marker m (or pos (point)))
  13589. (push m org-imenu-markers)
  13590. m))
  13591. (defun org-imenu-get-tree ()
  13592. "Produce the index for Imenu."
  13593. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13594. (setq org-imenu-markers nil)
  13595. (let* ((n org-imenu-depth)
  13596. (re (concat "^" outline-regexp))
  13597. (subs (make-vector (1+ n) nil))
  13598. (last-level 0)
  13599. m tree level head)
  13600. (save-excursion
  13601. (save-restriction
  13602. (widen)
  13603. (goto-char (point-max))
  13604. (while (re-search-backward re nil t)
  13605. (setq level (org-reduced-level (funcall outline-level)))
  13606. (when (<= level n)
  13607. (looking-at org-complex-heading-regexp)
  13608. (setq head (org-link-display-format
  13609. (org-match-string-no-properties 4))
  13610. m (org-imenu-new-marker))
  13611. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13612. (if (>= level last-level)
  13613. (push (cons head m) (aref subs level))
  13614. (push (cons head (aref subs (1+ level))) (aref subs level))
  13615. (loop for i from (1+ level) to n do (aset subs i nil)))
  13616. (setq last-level level)))))
  13617. (aref subs 1)))
  13618. (eval-after-load "imenu"
  13619. '(progn
  13620. (add-hook 'imenu-after-jump-hook
  13621. (lambda ()
  13622. (if (eq major-mode 'org-mode)
  13623. (org-show-context 'org-goto))))))
  13624. (defun org-link-display-format (link)
  13625. "Replace a link with either the description, or the link target
  13626. if no description is present"
  13627. (save-match-data
  13628. (if (string-match org-bracket-link-analytic-regexp link)
  13629. (replace-match (or (match-string 5 link)
  13630. (concat (match-string 1 link)
  13631. (match-string 3 link)))
  13632. nil nil link)
  13633. link)))
  13634. ;; Speedbar support
  13635. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13636. "Overlay marking the agenda restriction line in speedbar.")
  13637. (org-overlay-put org-speedbar-restriction-lock-overlay
  13638. 'face 'org-agenda-restriction-lock)
  13639. (org-overlay-put org-speedbar-restriction-lock-overlay
  13640. 'help-echo "Agendas are currently limited to this item.")
  13641. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13642. (defun org-speedbar-set-agenda-restriction ()
  13643. "Restrict future agenda commands to the location at point in speedbar.
  13644. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13645. (interactive)
  13646. (require 'org-agenda)
  13647. (let (p m tp np dir txt w)
  13648. (cond
  13649. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13650. 'org-imenu t))
  13651. (setq m (get-text-property p 'org-imenu-marker))
  13652. (save-excursion
  13653. (save-restriction
  13654. (set-buffer (marker-buffer m))
  13655. (goto-char m)
  13656. (org-agenda-set-restriction-lock 'subtree))))
  13657. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13658. 'speedbar-function 'speedbar-find-file))
  13659. (setq tp (previous-single-property-change
  13660. (1+ p) 'speedbar-function)
  13661. np (next-single-property-change
  13662. tp 'speedbar-function)
  13663. dir (speedbar-line-directory)
  13664. txt (buffer-substring-no-properties (or tp (point-min))
  13665. (or np (point-max))))
  13666. (save-excursion
  13667. (save-restriction
  13668. (set-buffer (find-file-noselect
  13669. (let ((default-directory dir))
  13670. (expand-file-name txt))))
  13671. (unless (org-mode-p)
  13672. (error "Cannot restrict to non-Org-mode file"))
  13673. (org-agenda-set-restriction-lock 'file))))
  13674. (t (error "Don't know how to restrict Org-mode's agenda")))
  13675. (org-move-overlay org-speedbar-restriction-lock-overlay
  13676. (point-at-bol) (point-at-eol))
  13677. (setq current-prefix-arg nil)
  13678. (org-agenda-maybe-redo)))
  13679. (eval-after-load "speedbar"
  13680. '(progn
  13681. (speedbar-add-supported-extension ".org")
  13682. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13683. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13684. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13685. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13686. (add-hook 'speedbar-visiting-tag-hook
  13687. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13688. ;;; Fixes and Hacks for problems with other packages
  13689. ;; Make flyspell not check words in links, to not mess up our keymap
  13690. (defun org-mode-flyspell-verify ()
  13691. "Don't let flyspell put overlays at active buttons."
  13692. (not (get-text-property (point) 'keymap)))
  13693. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13694. (eval-after-load "bookmark"
  13695. '(if (boundp 'bookmark-after-jump-hook)
  13696. ;; We can use the hook
  13697. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13698. ;; Hook not available, use advice
  13699. (defadvice bookmark-jump (after org-make-visible activate)
  13700. "Make the position visible."
  13701. (org-bookmark-jump-unhide))))
  13702. ;; Make sure saveplace show the location if it was hidden
  13703. (eval-after-load "saveplace"
  13704. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13705. "Make the position visible."
  13706. (org-bookmark-jump-unhide)))
  13707. (defun org-bookmark-jump-unhide ()
  13708. "Unhide the current position, to show the bookmark location."
  13709. (and (org-mode-p)
  13710. (or (org-invisible-p)
  13711. (save-excursion (goto-char (max (point-min) (1- (point))))
  13712. (org-invisible-p)))
  13713. (org-show-context 'bookmark-jump)))
  13714. ;; Make session.el ignore our circular variable
  13715. (eval-after-load "session"
  13716. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13717. ;;;; Experimental code
  13718. (defun org-closed-in-range ()
  13719. "Sparse tree of items closed in a certain time range.
  13720. Still experimental, may disappear in the future."
  13721. (interactive)
  13722. ;; Get the time interval from the user.
  13723. (let* ((time1 (time-to-seconds
  13724. (org-read-date nil 'to-time nil "Starting date: ")))
  13725. (time2 (time-to-seconds
  13726. (org-read-date nil 'to-time nil "End date:")))
  13727. ;; callback function
  13728. (callback (lambda ()
  13729. (let ((time
  13730. (time-to-seconds
  13731. (apply 'encode-time
  13732. (org-parse-time-string
  13733. (match-string 1))))))
  13734. ;; check if time in interval
  13735. (and (>= time time1) (<= time time2))))))
  13736. ;; make tree, check each match with the callback
  13737. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13738. ;;;; Finish up
  13739. (provide 'org)
  13740. (run-hooks 'org-load-hook)
  13741. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13742. ;;; org.el ends here