org.el 561 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.19d
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.19d"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  163. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  164. (const :tag "C eval: Include command output as text" org-eval)
  165. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  166. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  167. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  168. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  169. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  170. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  171. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  172. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  173. (const :tag "C registry: A registry for Org links" org-registry)
  174. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  175. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  176. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  177. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  178. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  179. (defcustom org-support-shift-select nil
  180. "Non-nil means, make shift-cursor commands select text when possible.
  181. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  182. selecting a region, or enlarge thusly regions started in this way.
  183. In Org-mode, in special contexts, these same keys are used for other
  184. purposes, important enough to compete with shift selection. Org tries
  185. to balance these needs by supporting `shift-select-mode' outside these
  186. special contexts, under control of this variable.
  187. The default of this variable is nil, to avoid confusing behavior. Shifted
  188. cursor keys will then execute Org commands in the following contexts:
  189. - on a headline, changing TODO state (left/right) and priority (up/down)
  190. - on a time stamp, changing the time
  191. - in a plain list item, changing the bullet type
  192. - in a property definition line, switching between allowed values
  193. - in the BEGIN line of a clock table (changing the time block).
  194. Outside these contexts, the commands will throw an error.
  195. When this variable is t and the cursor is not in a special context,
  196. Org-mode will support shift-selection for making and enlarging regions.
  197. To make this more effective, the bullet cycling will no longer happen
  198. anywhere in an item line, but only if the cursor is exactly on the bullet.
  199. If you set this variable to the symbol `always', then the keys
  200. will not be special in headlines, property lines, and item lines, to make
  201. shift selection work there as well. If this is what you want, you can
  202. use the following alternative commands: `C-c C-t' and `C-c ,' to
  203. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  204. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  205. edited by hand or in column view.
  206. However, when the cursor is on a timestamp, shift-cursor commands
  207. will still edit the time stamp - this is just too good to give up.
  208. XEmacs user should have this variable set to nil, because shift-select-mode
  209. is Emacs 23 only."
  210. :group 'org
  211. :type '(choice
  212. (const :tag "Never" nil)
  213. (const :tag "When outside special context" t)
  214. (const :tag "Everywhere except timestamps" always)))
  215. (defgroup org-startup nil
  216. "Options concerning startup of Org-mode."
  217. :tag "Org Startup"
  218. :group 'org)
  219. (defcustom org-startup-folded t
  220. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  221. This can also be configured on a per-file basis by adding one of
  222. the following lines anywhere in the buffer:
  223. #+STARTUP: fold
  224. #+STARTUP: nofold
  225. #+STARTUP: content"
  226. :group 'org-startup
  227. :type '(choice
  228. (const :tag "nofold: show all" nil)
  229. (const :tag "fold: overview" t)
  230. (const :tag "content: all headlines" content)))
  231. (defcustom org-startup-truncated t
  232. "Non-nil means, entering Org-mode will set `truncate-lines'.
  233. This is useful since some lines containing links can be very long and
  234. uninteresting. Also tables look terrible when wrapped."
  235. :group 'org-startup
  236. :type 'boolean)
  237. (defcustom org-startup-align-all-tables nil
  238. "Non-nil means, align all tables when visiting a file.
  239. This is useful when the column width in tables is forced with <N> cookies
  240. in table fields. Such tables will look correct only after the first re-align.
  241. This can also be configured on a per-file basis by adding one of
  242. the following lines anywhere in the buffer:
  243. #+STARTUP: align
  244. #+STARTUP: noalign"
  245. :group 'org-startup
  246. :type 'boolean)
  247. (defcustom org-insert-mode-line-in-empty-file nil
  248. "Non-nil means insert the first line setting Org-mode in empty files.
  249. When the function `org-mode' is called interactively in an empty file, this
  250. normally means that the file name does not automatically trigger Org-mode.
  251. To ensure that the file will always be in Org-mode in the future, a
  252. line enforcing Org-mode will be inserted into the buffer, if this option
  253. has been set."
  254. :group 'org-startup
  255. :type 'boolean)
  256. (defcustom org-replace-disputed-keys nil
  257. "Non-nil means use alternative key bindings for some keys.
  258. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  259. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  260. If you want to use Org-mode together with one of these other modes,
  261. or more generally if you would like to move some Org-mode commands to
  262. other keys, set this variable and configure the keys with the variable
  263. `org-disputed-keys'.
  264. This option is only relevant at load-time of Org-mode, and must be set
  265. *before* org.el is loaded. Changing it requires a restart of Emacs to
  266. become effective."
  267. :group 'org-startup
  268. :type 'boolean)
  269. (defcustom org-use-extra-keys nil
  270. "Non-nil means use extra key sequence definitions for certain
  271. commands. This happens automatically if you run XEmacs or if
  272. window-system is nil. This variable lets you do the same
  273. manually. You must set it before loading org.
  274. Example: on Carbon Emacs 22 running graphically, with an external
  275. keyboard on a Powerbook, the default way of setting M-left might
  276. not work for either Alt or ESC. Setting this variable will make
  277. it work for ESC."
  278. :group 'org-startup
  279. :type 'boolean)
  280. (if (fboundp 'defvaralias)
  281. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  282. (defcustom org-disputed-keys
  283. '(([(shift up)] . [(meta p)])
  284. ([(shift down)] . [(meta n)])
  285. ([(shift left)] . [(meta -)])
  286. ([(shift right)] . [(meta +)])
  287. ([(control shift right)] . [(meta shift +)])
  288. ([(control shift left)] . [(meta shift -)]))
  289. "Keys for which Org-mode and other modes compete.
  290. This is an alist, cars are the default keys, second element specifies
  291. the alternative to use when `org-replace-disputed-keys' is t.
  292. Keys can be specified in any syntax supported by `define-key'.
  293. The value of this option takes effect only at Org-mode's startup,
  294. therefore you'll have to restart Emacs to apply it after changing."
  295. :group 'org-startup
  296. :type 'alist)
  297. (defun org-key (key)
  298. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  299. Or return the original if not disputed."
  300. (if org-replace-disputed-keys
  301. (let* ((nkey (key-description key))
  302. (x (org-find-if (lambda (x)
  303. (equal (key-description (car x)) nkey))
  304. org-disputed-keys)))
  305. (if x (cdr x) key))
  306. key))
  307. (defun org-find-if (predicate seq)
  308. (catch 'exit
  309. (while seq
  310. (if (funcall predicate (car seq))
  311. (throw 'exit (car seq))
  312. (pop seq)))))
  313. (defun org-defkey (keymap key def)
  314. "Define a key, possibly translated, as returned by `org-key'."
  315. (define-key keymap (org-key key) def))
  316. (defcustom org-ellipsis nil
  317. "The ellipsis to use in the Org-mode outline.
  318. When nil, just use the standard three dots. When a string, use that instead,
  319. When a face, use the standard 3 dots, but with the specified face.
  320. The change affects only Org-mode (which will then use its own display table).
  321. Changing this requires executing `M-x org-mode' in a buffer to become
  322. effective."
  323. :group 'org-startup
  324. :type '(choice (const :tag "Default" nil)
  325. (face :tag "Face" :value org-warning)
  326. (string :tag "String" :value "...#")))
  327. (defvar org-display-table nil
  328. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  329. (defgroup org-keywords nil
  330. "Keywords in Org-mode."
  331. :tag "Org Keywords"
  332. :group 'org)
  333. (defcustom org-deadline-string "DEADLINE:"
  334. "String to mark deadline entries.
  335. A deadline is this string, followed by a time stamp. Should be a word,
  336. terminated by a colon. You can insert a schedule keyword and
  337. a timestamp with \\[org-deadline].
  338. Changes become only effective after restarting Emacs."
  339. :group 'org-keywords
  340. :type 'string)
  341. (defcustom org-scheduled-string "SCHEDULED:"
  342. "String to mark scheduled TODO entries.
  343. A schedule is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-schedule].
  346. Changes become only effective after restarting Emacs."
  347. :group 'org-keywords
  348. :type 'string)
  349. (defcustom org-closed-string "CLOSED:"
  350. "String used as the prefix for timestamps logging closing a TODO entry."
  351. :group 'org-keywords
  352. :type 'string)
  353. (defcustom org-clock-string "CLOCK:"
  354. "String used as prefix for timestamps clocking work hours on an item."
  355. :group 'org-keywords
  356. :type 'string)
  357. (defcustom org-comment-string "COMMENT"
  358. "Entries starting with this keyword will never be exported.
  359. An entry can be toggled between COMMENT and normal with
  360. \\[org-toggle-comment].
  361. Changes become only effective after restarting Emacs."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-quote-string "QUOTE"
  365. "Entries starting with this keyword will be exported in fixed-width font.
  366. Quoting applies only to the text in the entry following the headline, and does
  367. not extend beyond the next headline, even if that is lower level.
  368. An entry can be toggled between QUOTE and normal with
  369. \\[org-toggle-fixed-width-section]."
  370. :group 'org-keywords
  371. :type 'string)
  372. (defconst org-repeat-re
  373. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  374. "Regular expression for specifying repeated events.
  375. After a match, group 1 contains the repeat expression.")
  376. (defgroup org-structure nil
  377. "Options concerning the general structure of Org-mode files."
  378. :tag "Org Structure"
  379. :group 'org)
  380. (defgroup org-reveal-location nil
  381. "Options about how to make context of a location visible."
  382. :tag "Org Reveal Location"
  383. :group 'org-structure)
  384. (defconst org-context-choice
  385. '(choice
  386. (const :tag "Always" t)
  387. (const :tag "Never" nil)
  388. (repeat :greedy t :tag "Individual contexts"
  389. (cons
  390. (choice :tag "Context"
  391. (const agenda)
  392. (const org-goto)
  393. (const occur-tree)
  394. (const tags-tree)
  395. (const link-search)
  396. (const mark-goto)
  397. (const bookmark-jump)
  398. (const isearch)
  399. (const default))
  400. (boolean))))
  401. "Contexts for the reveal options.")
  402. (defcustom org-show-hierarchy-above '((default . t))
  403. "Non-nil means, show full hierarchy when revealing a location.
  404. Org-mode often shows locations in an org-mode file which might have
  405. been invisible before. When this is set, the hierarchy of headings
  406. above the exposed location is shown.
  407. Turning this off for example for sparse trees makes them very compact.
  408. Instead of t, this can also be an alist specifying this option for different
  409. contexts. Valid contexts are
  410. agenda when exposing an entry from the agenda
  411. org-goto when using the command `org-goto' on key C-c C-j
  412. occur-tree when using the command `org-occur' on key C-c /
  413. tags-tree when constructing a sparse tree based on tags matches
  414. link-search when exposing search matches associated with a link
  415. mark-goto when exposing the jump goal of a mark
  416. bookmark-jump when exposing a bookmark location
  417. isearch when exiting from an incremental search
  418. default default for all contexts not set explicitly"
  419. :group 'org-reveal-location
  420. :type org-context-choice)
  421. (defcustom org-show-following-heading '((default . nil))
  422. "Non-nil means, show following heading when revealing a location.
  423. Org-mode often shows locations in an org-mode file which might have
  424. been invisible before. When this is set, the heading following the
  425. match is shown.
  426. Turning this off for example for sparse trees makes them very compact,
  427. but makes it harder to edit the location of the match. In such a case,
  428. use the command \\[org-reveal] to show more context.
  429. Instead of t, this can also be an alist specifying this option for different
  430. contexts. See `org-show-hierarchy-above' for valid contexts."
  431. :group 'org-reveal-location
  432. :type org-context-choice)
  433. (defcustom org-show-siblings '((default . nil) (isearch t))
  434. "Non-nil means, show all sibling heading when revealing a location.
  435. Org-mode often shows locations in an org-mode file which might have
  436. been invisible before. When this is set, the sibling of the current entry
  437. heading are all made visible. If `org-show-hierarchy-above' is t,
  438. the same happens on each level of the hierarchy above the current entry.
  439. By default this is on for the isearch context, off for all other contexts.
  440. Turning this off for example for sparse trees makes them very compact,
  441. but makes it harder to edit the location of the match. In such a case,
  442. use the command \\[org-reveal] to show more context.
  443. Instead of t, this can also be an alist specifying this option for different
  444. contexts. See `org-show-hierarchy-above' for valid contexts."
  445. :group 'org-reveal-location
  446. :type org-context-choice)
  447. (defcustom org-show-entry-below '((default . nil))
  448. "Non-nil means, show the entry below a headline when revealing a location.
  449. Org-mode often shows locations in an org-mode file which might have
  450. been invisible before. When this is set, the text below the headline that is
  451. exposed is also shown.
  452. By default this is off for all contexts.
  453. Instead of t, this can also be an alist specifying this option for different
  454. contexts. See `org-show-hierarchy-above' for valid contexts."
  455. :group 'org-reveal-location
  456. :type org-context-choice)
  457. (defcustom org-indirect-buffer-display 'other-window
  458. "How should indirect tree buffers be displayed?
  459. This applies to indirect buffers created with the commands
  460. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  461. Valid values are:
  462. current-window Display in the current window
  463. other-window Just display in another window.
  464. dedicated-frame Create one new frame, and re-use it each time.
  465. new-frame Make a new frame each time. Note that in this case
  466. previously-made indirect buffers are kept, and you need to
  467. kill these buffers yourself."
  468. :group 'org-structure
  469. :group 'org-agenda-windows
  470. :type '(choice
  471. (const :tag "In current window" current-window)
  472. (const :tag "In current frame, other window" other-window)
  473. (const :tag "Each time a new frame" new-frame)
  474. (const :tag "One dedicated frame" dedicated-frame)))
  475. (defgroup org-cycle nil
  476. "Options concerning visibility cycling in Org-mode."
  477. :tag "Org Cycle"
  478. :group 'org-structure)
  479. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  480. "Names of drawers. Drawers are not opened by cycling on the headline above.
  481. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  482. this:
  483. :DRAWERNAME:
  484. .....
  485. :END:
  486. The drawer \"PROPERTIES\" is special for capturing properties through
  487. the property API.
  488. Drawers can be defined on the per-file basis with a line like:
  489. #+DRAWERS: HIDDEN STATE PROPERTIES"
  490. :group 'org-structure
  491. :type '(repeat (string :tag "Drawer Name")))
  492. (defcustom org-cycle-global-at-bob nil
  493. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  494. This makes it possible to do global cycling without having to use S-TAB or
  495. C-u TAB. For this special case to work, the first line of the buffer
  496. must not be a headline - it may be empty ot some other text. When used in
  497. this way, `org-cycle-hook' is disables temporarily, to make sure the
  498. cursor stays at the beginning of the buffer.
  499. When this option is nil, don't do anything special at the beginning
  500. of the buffer."
  501. :group 'org-cycle
  502. :type 'boolean)
  503. (defcustom org-cycle-emulate-tab t
  504. "Where should `org-cycle' emulate TAB.
  505. nil Never
  506. white Only in completely white lines
  507. whitestart Only at the beginning of lines, before the first non-white char
  508. t Everywhere except in headlines
  509. exc-hl-bol Everywhere except at the start of a headline
  510. If TAB is used in a place where it does not emulate TAB, the current subtree
  511. visibility is cycled."
  512. :group 'org-cycle
  513. :type '(choice (const :tag "Never" nil)
  514. (const :tag "Only in completely white lines" white)
  515. (const :tag "Before first char in a line" whitestart)
  516. (const :tag "Everywhere except in headlines" t)
  517. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  518. ))
  519. (defcustom org-cycle-separator-lines 2
  520. "Number of empty lines needed to keep an empty line between collapsed trees.
  521. If you leave an empty line between the end of a subtree and the following
  522. headline, this empty line is hidden when the subtree is folded.
  523. Org-mode will leave (exactly) one empty line visible if the number of
  524. empty lines is equal or larger to the number given in this variable.
  525. So the default 2 means, at least 2 empty lines after the end of a subtree
  526. are needed to produce free space between a collapsed subtree and the
  527. following headline.
  528. Special case: when 0, never leave empty lines in collapsed view."
  529. :group 'org-cycle
  530. :type 'integer)
  531. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  532. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  533. org-cycle-hide-drawers
  534. org-cycle-show-empty-lines
  535. org-optimize-window-after-visibility-change)
  536. "Hook that is run after `org-cycle' has changed the buffer visibility.
  537. The function(s) in this hook must accept a single argument which indicates
  538. the new state that was set by the most recent `org-cycle' command. The
  539. argument is a symbol. After a global state change, it can have the values
  540. `overview', `content', or `all'. After a local state change, it can have
  541. the values `folded', `children', or `subtree'."
  542. :group 'org-cycle
  543. :type 'hook)
  544. (defgroup org-edit-structure nil
  545. "Options concerning structure editing in Org-mode."
  546. :tag "Org Edit Structure"
  547. :group 'org-structure)
  548. (defcustom org-odd-levels-only nil
  549. "Non-nil means, skip even levels and only use odd levels for the outline.
  550. This has the effect that two stars are being added/taken away in
  551. promotion/demotion commands. It also influences how levels are
  552. handled by the exporters.
  553. Changing it requires restart of `font-lock-mode' to become effective
  554. for fontification also in regions already fontified.
  555. You may also set this on a per-file basis by adding one of the following
  556. lines to the buffer:
  557. #+STARTUP: odd
  558. #+STARTUP: oddeven"
  559. :group 'org-edit-structure
  560. :group 'org-font-lock
  561. :type 'boolean)
  562. (defcustom org-adapt-indentation t
  563. "Non-nil means, adapt indentation when promoting and demoting.
  564. When this is set and the *entire* text in an entry is indented, the
  565. indentation is increased by one space in a demotion command, and
  566. decreased by one in a promotion command. If any line in the entry
  567. body starts at column 0, indentation is not changed at all."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defcustom org-special-ctrl-a/e nil
  571. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  572. When t, `C-a' will bring back the cursor to the beginning of the
  573. headline text, i.e. after the stars and after a possible TODO keyword.
  574. In an item, this will be the position after the bullet.
  575. When the cursor is already at that position, another `C-a' will bring
  576. it to the beginning of the line.
  577. `C-e' will jump to the end of the headline, ignoring the presence of tags
  578. in the headline. A second `C-e' will then jump to the true end of the
  579. line, after any tags.
  580. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  581. and only a directly following, identical keypress will bring the cursor
  582. to the special positions."
  583. :group 'org-edit-structure
  584. :type '(choice
  585. (const :tag "off" nil)
  586. (const :tag "after bullet first" t)
  587. (const :tag "border first" reversed)))
  588. (if (fboundp 'defvaralias)
  589. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  590. (defcustom org-special-ctrl-k nil
  591. "Non-nil means `C-k' will behave specially in headlines.
  592. When nil, `C-k' will call the default `kill-line' command.
  593. When t, the following will happen while the cursor is in the headline:
  594. - When the cursor is at the beginning of a headline, kill the entire
  595. line and possible the folded subtree below the line.
  596. - When in the middle of the headline text, kill the headline up to the tags.
  597. - When after the headline text, kill the tags."
  598. :group 'org-edit-structure
  599. :type 'boolean)
  600. (defcustom org-yank-folded-subtrees t
  601. "Non-nil means, when yanking subtrees, fold them.
  602. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  603. it starts with a heading and all other headings in it are either children
  604. or siblings, then fold all the subtrees. However, do this only if no
  605. text after the yank would be swallowed into a folded tree by this action."
  606. :group 'org-edit-structure
  607. :type 'boolean)
  608. (defcustom org-yank-adjusted-subtrees t
  609. "Non-nil means, when yanking subtrees, adjust the level.
  610. With this setting, `org-paste-subtree' is used to insert the subtree, see
  611. this function for details."
  612. :group 'org-edit-structure
  613. :type 'boolean)
  614. (defcustom org-M-RET-may-split-line '((default . t))
  615. "Non-nil means, M-RET will split the line at the cursor position.
  616. When nil, it will go to the end of the line before making a
  617. new line.
  618. You may also set this option in a different way for different
  619. contexts. Valid contexts are:
  620. headline when creating a new headline
  621. item when creating a new item
  622. table in a table field
  623. default the value to be used for all contexts not explicitly
  624. customized"
  625. :group 'org-structure
  626. :group 'org-table
  627. :type '(choice
  628. (const :tag "Always" t)
  629. (const :tag "Never" nil)
  630. (repeat :greedy t :tag "Individual contexts"
  631. (cons
  632. (choice :tag "Context"
  633. (const headline)
  634. (const item)
  635. (const table)
  636. (const default))
  637. (boolean)))))
  638. (defcustom org-insert-heading-respect-content nil
  639. "Non-nil means, insert new headings after the current subtree.
  640. When nil, the new heading is created directly after the current line.
  641. The commands \\[org-insert-heading-respect-content] and
  642. \\[org-insert-todo-heading-respect-content] turn this variable on
  643. for the duration of the command."
  644. :group 'org-structure
  645. :type 'boolean)
  646. (defcustom org-blank-before-new-entry '((heading . auto)
  647. (plain-list-item . auto))
  648. "Should `org-insert-heading' leave a blank line before new heading/item?
  649. The value is an alist, with `heading' and `plain-list-item' as car,
  650. and a boolean flag as cdr."
  651. :group 'org-edit-structure
  652. :type '(list
  653. (cons (const heading)
  654. (choice (const :tag "Never" nil)
  655. (const :tag "Always" t)
  656. (const :tag "Auto" auto)))
  657. (cons (const plain-list-item)
  658. (choice (const :tag "Never" nil)
  659. (const :tag "Always" t)
  660. (const :tag "Auto" auto)))))
  661. (defcustom org-insert-heading-hook nil
  662. "Hook being run after inserting a new heading."
  663. :group 'org-edit-structure
  664. :type 'hook)
  665. (defcustom org-enable-fixed-width-editor t
  666. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  667. This currently only means, they are never auto-wrapped.
  668. When nil, such lines will be treated like ordinary lines.
  669. See also the QUOTE keyword."
  670. :group 'org-edit-structure
  671. :type 'boolean)
  672. (defcustom org-edit-src-region-extra nil
  673. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  674. For examples see the function `org-edit-src-find-region-and-lang'.
  675. The regular expression identifying the begin marker should end with a newline,
  676. and the regexp marking the end line should start with a newline, to make sure
  677. there are kept outside the narrowed region."
  678. :group 'org-edit-structure
  679. :type '(repeat
  680. (list
  681. (regexp :tag "begin regexp")
  682. (regexp :tag "end regexp")
  683. (choice :tag "language"
  684. (string :tag "specify")
  685. (integer :tag "from match group")
  686. (const :tag "from `lang' element")
  687. (const :tag "from `style' element")))))
  688. (defcustom org-coderef-label-format "(ref:%s)"
  689. "The default coderef format.
  690. This format string will be used to search for coderef labels in literal
  691. examples (EXAMPLE and SRC blocks). The format can be overwritten
  692. an individual literal example with the -f option, like
  693. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  694. ...
  695. #+END_SRC
  696. If you want to use this for HTML export, make sure that the format does
  697. not introduce special font-locking, and avoid the HTML special
  698. characters `<', `>', and `&'. The reason for this restriction is that
  699. the labels are searched for only after htmlize has done its job."
  700. :group 'org-edit-structure ; FIXME this is not in the right group
  701. :type 'string)
  702. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  703. "The mode that should be used to edit fixed-width regions.
  704. These are the regions where each line starts with a colon."
  705. :group 'org-edit-structure
  706. :type '(choice
  707. (const artist-mode)
  708. (const picture-mode)
  709. (const fundamental-mode)
  710. (function :tag "Other (specify)")))
  711. (defcustom org-goto-auto-isearch t
  712. "Non-nil means, typing characters in org-goto starts incremental search."
  713. :group 'org-edit-structure
  714. :type 'boolean)
  715. (defgroup org-sparse-trees nil
  716. "Options concerning sparse trees in Org-mode."
  717. :tag "Org Sparse Trees"
  718. :group 'org-structure)
  719. (defcustom org-highlight-sparse-tree-matches t
  720. "Non-nil means, highlight all matches that define a sparse tree.
  721. The highlights will automatically disappear the next time the buffer is
  722. changed by an edit command."
  723. :group 'org-sparse-trees
  724. :type 'boolean)
  725. (defcustom org-remove-highlights-with-change t
  726. "Non-nil means, any change to the buffer will remove temporary highlights.
  727. Such highlights are created by `org-occur' and `org-clock-display'.
  728. When nil, `C-c C-c needs to be used to get rid of the highlights.
  729. The highlights created by `org-preview-latex-fragment' always need
  730. `C-c C-c' to be removed."
  731. :group 'org-sparse-trees
  732. :group 'org-time
  733. :type 'boolean)
  734. (defcustom org-occur-hook '(org-first-headline-recenter)
  735. "Hook that is run after `org-occur' has constructed a sparse tree.
  736. This can be used to recenter the window to show as much of the structure
  737. as possible."
  738. :group 'org-sparse-trees
  739. :type 'hook)
  740. (defgroup org-imenu-and-speedbar nil
  741. "Options concerning imenu and speedbar in Org-mode."
  742. :tag "Org Imenu and Speedbar"
  743. :group 'org-structure)
  744. (defcustom org-imenu-depth 2
  745. "The maximum level for Imenu access to Org-mode headlines.
  746. This also applied for speedbar access."
  747. :group 'org-imenu-and-speedbar
  748. :type 'number)
  749. (defgroup org-table nil
  750. "Options concerning tables in Org-mode."
  751. :tag "Org Table"
  752. :group 'org)
  753. (defcustom org-enable-table-editor 'optimized
  754. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  755. When nil, such lines will be treated like ordinary lines.
  756. When equal to the symbol `optimized', the table editor will be optimized to
  757. do the following:
  758. - Automatic overwrite mode in front of whitespace in table fields.
  759. This makes the structure of the table stay in tact as long as the edited
  760. field does not exceed the column width.
  761. - Minimize the number of realigns. Normally, the table is aligned each time
  762. TAB or RET are pressed to move to another field. With optimization this
  763. happens only if changes to a field might have changed the column width.
  764. Optimization requires replacing the functions `self-insert-command',
  765. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  766. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  767. very good at guessing when a re-align will be necessary, but you can always
  768. force one with \\[org-ctrl-c-ctrl-c].
  769. If you would like to use the optimized version in Org-mode, but the
  770. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  771. This variable can be used to turn on and off the table editor during a session,
  772. but in order to toggle optimization, a restart is required.
  773. See also the variable `org-table-auto-blank-field'."
  774. :group 'org-table
  775. :type '(choice
  776. (const :tag "off" nil)
  777. (const :tag "on" t)
  778. (const :tag "on, optimized" optimized)))
  779. (defcustom org-table-tab-recognizes-table.el t
  780. "Non-nil means, TAB will automatically notice a table.el table.
  781. When it sees such a table, it moves point into it and - if necessary -
  782. calls `table-recognize-table'."
  783. :group 'org-table-editing
  784. :type 'boolean)
  785. (defgroup org-link nil
  786. "Options concerning links in Org-mode."
  787. :tag "Org Link"
  788. :group 'org)
  789. (defvar org-link-abbrev-alist-local nil
  790. "Buffer-local version of `org-link-abbrev-alist', which see.
  791. The value of this is taken from the #+LINK lines.")
  792. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  793. (defcustom org-link-abbrev-alist nil
  794. "Alist of link abbreviations.
  795. The car of each element is a string, to be replaced at the start of a link.
  796. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  797. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  798. [[linkkey:tag][description]]
  799. If REPLACE is a string, the tag will simply be appended to create the link.
  800. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  801. the placeholder \"%h\" will cause a url-encoded version of the tag to
  802. be inserted at that point (see the function `url-hexify-string').
  803. REPLACE may also be a function that will be called with the tag as the
  804. only argument to create the link, which should be returned as a string.
  805. See the manual for examples."
  806. :group 'org-link
  807. :type '(repeat
  808. (cons
  809. (string :tag "Protocol")
  810. (choice
  811. (string :tag "Format")
  812. (function)))))
  813. (defcustom org-descriptive-links t
  814. "Non-nil means, hide link part and only show description of bracket links.
  815. Bracket links are like [[link][description]]. This variable sets the initial
  816. state in new org-mode buffers. The setting can then be toggled on a
  817. per-buffer basis from the Org->Hyperlinks menu."
  818. :group 'org-link
  819. :type 'boolean)
  820. (defcustom org-link-file-path-type 'adaptive
  821. "How the path name in file links should be stored.
  822. Valid values are:
  823. relative Relative to the current directory, i.e. the directory of the file
  824. into which the link is being inserted.
  825. absolute Absolute path, if possible with ~ for home directory.
  826. noabbrev Absolute path, no abbreviation of home directory.
  827. adaptive Use relative path for files in the current directory and sub-
  828. directories of it. For other files, use an absolute path."
  829. :group 'org-link
  830. :type '(choice
  831. (const relative)
  832. (const absolute)
  833. (const noabbrev)
  834. (const adaptive)))
  835. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  836. "Types of links that should be activated in Org-mode files.
  837. This is a list of symbols, each leading to the activation of a certain link
  838. type. In principle, it does not hurt to turn on most link types - there may
  839. be a small gain when turning off unused link types. The types are:
  840. bracket The recommended [[link][description]] or [[link]] links with hiding.
  841. angular Links in angular brackets that may contain whitespace like
  842. <bbdb:Carsten Dominik>.
  843. plain Plain links in normal text, no whitespace, like http://google.com.
  844. radio Text that is matched by a radio target, see manual for details.
  845. tag Tag settings in a headline (link to tag search).
  846. date Time stamps (link to calendar).
  847. footnote Footnote labels.
  848. Changing this variable requires a restart of Emacs to become effective."
  849. :group 'org-link
  850. :type '(set :greedy t
  851. (const :tag "Double bracket links (new style)" bracket)
  852. (const :tag "Angular bracket links (old style)" angular)
  853. (const :tag "Plain text links" plain)
  854. (const :tag "Radio target matches" radio)
  855. (const :tag "Tags" tag)
  856. (const :tag "Timestamps" date)
  857. (const :tag "Footnotes" footnote)))
  858. (defcustom org-make-link-description-function nil
  859. "Function to use to generate link descriptions from links. If
  860. nil the link location will be used. This function must take two
  861. parameters; the first is the link and the second the description
  862. org-insert-link has generated, and should return the description
  863. to use."
  864. :group 'org-link
  865. :type 'function)
  866. (defgroup org-link-store nil
  867. "Options concerning storing links in Org-mode."
  868. :tag "Org Store Link"
  869. :group 'org-link)
  870. (defcustom org-email-link-description-format "Email %c: %.30s"
  871. "Format of the description part of a link to an email or usenet message.
  872. The following %-escapes will be replaced by corresponding information:
  873. %F full \"From\" field
  874. %f name, taken from \"From\" field, address if no name
  875. %T full \"To\" field
  876. %t first name in \"To\" field, address if no name
  877. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  878. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  879. %s subject
  880. %m message-id.
  881. You may use normal field width specification between the % and the letter.
  882. This is for example useful to limit the length of the subject.
  883. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  884. :group 'org-link-store
  885. :type 'string)
  886. (defcustom org-from-is-user-regexp
  887. (let (r1 r2)
  888. (when (and user-mail-address (not (string= user-mail-address "")))
  889. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  890. (when (and user-full-name (not (string= user-full-name "")))
  891. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  892. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  893. "Regexp matched against the \"From:\" header of an email or usenet message.
  894. It should match if the message is from the user him/herself."
  895. :group 'org-link-store
  896. :type 'regexp)
  897. (defcustom org-link-to-org-use-id 'create-if-interactive
  898. "Non-nil means, storing a link to an Org file will use entry IDs.
  899. Note that before this variable is even considered, org-id must be loaded,
  900. to please customize `org-modules' and turn it on.
  901. The variable can have the following values:
  902. t Create an ID if needed to make a link to the current entry.
  903. create-if-interactive
  904. If `org-store-link' is called directly (interactively, as a user
  905. command), do create an ID to support the link. But when doing the
  906. job for remember, only use the ID if it already exists. The
  907. purpose of this setting is to avoid proliferation of unwanted
  908. IDs, just because you happen to be in an Org file when you
  909. call `org-remember' that automatically and preemptively
  910. creates a link. If you do want to get an ID link in a remember
  911. template to an entry not having an ID, create it first by
  912. explicitly creating a link to it, using `C-c C-l' first.
  913. use-existing
  914. Use existing ID, do not create one.
  915. nil Never use an ID to make a link, instead link using a text search for
  916. the headline text."
  917. :group 'org-link-store
  918. :type '(choice
  919. (const :tag "Create ID to make link" t)
  920. (const :tag "Create if string link interactively"
  921. 'create-if-interactive)
  922. (const :tag "Only use existing" 'use-existing)
  923. (const :tag "Do not use ID to create link" nil)))
  924. (defcustom org-context-in-file-links t
  925. "Non-nil means, file links from `org-store-link' contain context.
  926. A search string will be added to the file name with :: as separator and
  927. used to find the context when the link is activated by the command
  928. `org-open-at-point'.
  929. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  930. negates this setting for the duration of the command."
  931. :group 'org-link-store
  932. :type 'boolean)
  933. (defcustom org-keep-stored-link-after-insertion nil
  934. "Non-nil means, keep link in list for entire session.
  935. The command `org-store-link' adds a link pointing to the current
  936. location to an internal list. These links accumulate during a session.
  937. The command `org-insert-link' can be used to insert links into any
  938. Org-mode file (offering completion for all stored links). When this
  939. option is nil, every link which has been inserted once using \\[org-insert-link]
  940. will be removed from the list, to make completing the unused links
  941. more efficient."
  942. :group 'org-link-store
  943. :type 'boolean)
  944. (defgroup org-link-follow nil
  945. "Options concerning following links in Org-mode."
  946. :tag "Org Follow Link"
  947. :group 'org-link)
  948. (defcustom org-link-translation-function nil
  949. "Function to translate links with different syntax to Org syntax.
  950. This can be used to translate links created for example by the Planner
  951. or emacs-wiki packages to Org syntax.
  952. The function must accept two parameters, a TYPE containing the link
  953. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  954. which is everything after the link protocol. It should return a cons
  955. with possibly modified values of type and path.
  956. Org contains a function for this, so if you set this variable to
  957. `org-translate-link-from-planner', you should be able follow many
  958. links created by planner."
  959. :group 'org-link-follow
  960. :type 'function)
  961. (defcustom org-follow-link-hook nil
  962. "Hook that is run after a link has been followed."
  963. :group 'org-link-follow
  964. :type 'hook)
  965. (defcustom org-tab-follows-link nil
  966. "Non-nil means, on links TAB will follow the link.
  967. Needs to be set before org.el is loaded."
  968. :group 'org-link-follow
  969. :type 'boolean)
  970. (defcustom org-return-follows-link nil
  971. "Non-nil means, on links RET will follow the link.
  972. Needs to be set before org.el is loaded."
  973. :group 'org-link-follow
  974. :type 'boolean)
  975. (defcustom org-mouse-1-follows-link
  976. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  977. "Non-nil means, mouse-1 on a link will follow the link.
  978. A longer mouse click will still set point. Does not work on XEmacs.
  979. Needs to be set before org.el is loaded."
  980. :group 'org-link-follow
  981. :type 'boolean)
  982. (defcustom org-mark-ring-length 4
  983. "Number of different positions to be recorded in the ring
  984. Changing this requires a restart of Emacs to work correctly."
  985. :group 'org-link-follow
  986. :type 'integer)
  987. (defcustom org-link-frame-setup
  988. '((vm . vm-visit-folder-other-frame)
  989. (gnus . gnus-other-frame)
  990. (file . find-file-other-window))
  991. "Setup the frame configuration for following links.
  992. When following a link with Emacs, it may often be useful to display
  993. this link in another window or frame. This variable can be used to
  994. set this up for the different types of links.
  995. For VM, use any of
  996. `vm-visit-folder'
  997. `vm-visit-folder-other-frame'
  998. For Gnus, use any of
  999. `gnus'
  1000. `gnus-other-frame'
  1001. `org-gnus-no-new-news'
  1002. For FILE, use any of
  1003. `find-file'
  1004. `find-file-other-window'
  1005. `find-file-other-frame'
  1006. For the calendar, use the variable `calendar-setup'.
  1007. For BBDB, it is currently only possible to display the matches in
  1008. another window."
  1009. :group 'org-link-follow
  1010. :type '(list
  1011. (cons (const vm)
  1012. (choice
  1013. (const vm-visit-folder)
  1014. (const vm-visit-folder-other-window)
  1015. (const vm-visit-folder-other-frame)))
  1016. (cons (const gnus)
  1017. (choice
  1018. (const gnus)
  1019. (const gnus-other-frame)
  1020. (const org-gnus-no-new-news)))
  1021. (cons (const file)
  1022. (choice
  1023. (const find-file)
  1024. (const find-file-other-window)
  1025. (const find-file-other-frame)))))
  1026. (defcustom org-display-internal-link-with-indirect-buffer nil
  1027. "Non-nil means, use indirect buffer to display infile links.
  1028. Activating internal links (from one location in a file to another location
  1029. in the same file) normally just jumps to the location. When the link is
  1030. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1031. another window. When this option is set, the other window actually displays
  1032. an indirect buffer clone of the current buffer, to avoid any visibility
  1033. changes to the current buffer."
  1034. :group 'org-link-follow
  1035. :type 'boolean)
  1036. (defcustom org-open-non-existing-files nil
  1037. "Non-nil means, `org-open-file' will open non-existing files.
  1038. When nil, an error will be generated."
  1039. :group 'org-link-follow
  1040. :type 'boolean)
  1041. (defcustom org-open-directory-means-index-dot-org nil
  1042. "Non-nil means, a link to a directory really means to index.org.
  1043. When nil, following a directory link will run dired or open a finder/explorer
  1044. window on that directory."
  1045. :group 'org-link-follow
  1046. :type 'boolean)
  1047. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1048. "Function and arguments to call for following mailto links.
  1049. This is a list with the first element being a lisp function, and the
  1050. remaining elements being arguments to the function. In string arguments,
  1051. %a will be replaced by the address, and %s will be replaced by the subject
  1052. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1053. :group 'org-link-follow
  1054. :type '(choice
  1055. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1056. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1057. (const :tag "message-mail" (message-mail "%a" "%s"))
  1058. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1059. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1060. "Non-nil means, ask for confirmation before executing shell links.
  1061. Shell links can be dangerous: just think about a link
  1062. [[shell:rm -rf ~/*][Google Search]]
  1063. This link would show up in your Org-mode document as \"Google Search\",
  1064. but really it would remove your entire home directory.
  1065. Therefore we advise against setting this variable to nil.
  1066. Just change it to `y-or-n-p' of you want to confirm with a
  1067. single keystroke rather than having to type \"yes\"."
  1068. :group 'org-link-follow
  1069. :type '(choice
  1070. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1071. (const :tag "with y-or-n (faster)" y-or-n-p)
  1072. (const :tag "no confirmation (dangerous)" nil)))
  1073. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1074. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1075. Elisp links can be dangerous: just think about a link
  1076. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1077. This link would show up in your Org-mode document as \"Google Search\",
  1078. but really it would remove your entire home directory.
  1079. Therefore we advise against setting this variable to nil.
  1080. Just change it to `y-or-n-p' of you want to confirm with a
  1081. single keystroke rather than having to type \"yes\"."
  1082. :group 'org-link-follow
  1083. :type '(choice
  1084. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1085. (const :tag "with y-or-n (faster)" y-or-n-p)
  1086. (const :tag "no confirmation (dangerous)" nil)))
  1087. (defconst org-file-apps-defaults-gnu
  1088. '((remote . emacs)
  1089. (system . mailcap)
  1090. (t . mailcap))
  1091. "Default file applications on a UNIX or GNU/Linux system.
  1092. See `org-file-apps'.")
  1093. (defconst org-file-apps-defaults-macosx
  1094. '((remote . emacs)
  1095. (t . "open %s")
  1096. (system . "open %s")
  1097. ("ps.gz" . "gv %s")
  1098. ("eps.gz" . "gv %s")
  1099. ("dvi" . "xdvi %s")
  1100. ("fig" . "xfig %s"))
  1101. "Default file applications on a MacOS X system.
  1102. The system \"open\" is known as a default, but we use X11 applications
  1103. for some files for which the OS does not have a good default.
  1104. See `org-file-apps'.")
  1105. (defconst org-file-apps-defaults-windowsnt
  1106. (list
  1107. '(remote . emacs)
  1108. (cons t
  1109. (list (if (featurep 'xemacs)
  1110. 'mswindows-shell-execute
  1111. 'w32-shell-execute)
  1112. "open" 'file))
  1113. (cons 'system
  1114. (list (if (featurep 'xemacs)
  1115. 'mswindows-shell-execute
  1116. 'w32-shell-execute)
  1117. "open" 'file)))
  1118. "Default file applications on a Windows NT system.
  1119. The system \"open\" is used for most files.
  1120. See `org-file-apps'.")
  1121. (defcustom org-file-apps
  1122. '(
  1123. (auto-mode . emacs)
  1124. ("\\.x?html?\\'" . default)
  1125. ("\\.pdf\\'" . default)
  1126. )
  1127. "External applications for opening `file:path' items in a document.
  1128. Org-mode uses system defaults for different file types, but
  1129. you can use this variable to set the application for a given file
  1130. extension. The entries in this list are cons cells where the car identifies
  1131. files and the cdr the corresponding command. Possible values for the
  1132. file identifier are
  1133. \"regex\" Regular expression matched against the file name. For backward
  1134. compatibility, this can also be a string with only alphanumeric
  1135. characters, which is then interpreted as an extension.
  1136. `directory' Matches a directory
  1137. `remote' Matches a remote file, accessible through tramp or efs.
  1138. Remote files most likely should be visited through Emacs
  1139. because external applications cannot handle such paths.
  1140. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1141. so all files Emacs knows how to handle. Using this with
  1142. command `emacs' will open most files in Emacs. Beware that this
  1143. will also open html files inside Emacs, unless you add
  1144. (\"html\" . default) to the list as well.
  1145. t Default for files not matched by any of the other options.
  1146. `system' The system command to open files, like `open' on Windows
  1147. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1148. that will be selected if you call `C-c C-o' with a double
  1149. `C-u C-u' prefix.
  1150. Possible values for the command are:
  1151. `emacs' The file will be visited by the current Emacs process.
  1152. `default' Use the default application for this file type, which is the
  1153. association for t in the list, most likely in the system-specific
  1154. part.
  1155. This can be used to overrule an unwanted setting in the
  1156. system-specific variable.
  1157. `system' Use the system command for opening files, like \"open\".
  1158. This command is specified by the entry whose car is `system'.
  1159. Most likely, the system-specific version of this variable
  1160. does define this command, but you can overrule/replace it
  1161. here.
  1162. string A command to be executed by a shell; %s will be replaced
  1163. by the path to the file.
  1164. sexp A Lisp form which will be evaluated. The file path will
  1165. be available in the Lisp variable `file'.
  1166. For more examples, see the system specific constants
  1167. `org-file-apps-defaults-macosx'
  1168. `org-file-apps-defaults-windowsnt'
  1169. `org-file-apps-defaults-gnu'."
  1170. :group 'org-link-follow
  1171. :type '(repeat
  1172. (cons (choice :value ""
  1173. (string :tag "Extension")
  1174. (const :tag "System command to open files" system)
  1175. (const :tag "Default for unrecognized files" t)
  1176. (const :tag "Remote file" remote)
  1177. (const :tag "Links to a directory" directory)
  1178. (const :tag "Any files that have Emacs modes"
  1179. auto-mode))
  1180. (choice :value ""
  1181. (const :tag "Visit with Emacs" emacs)
  1182. (const :tag "Use default" default)
  1183. (const :tag "Use the system command" system)
  1184. (string :tag "Command")
  1185. (sexp :tag "Lisp form")))))
  1186. (defgroup org-refile nil
  1187. "Options concerning refiling entries in Org-mode."
  1188. :tag "Org Refile"
  1189. :group 'org)
  1190. (defcustom org-directory "~/org"
  1191. "Directory with org files.
  1192. This directory will be used as default to prompt for org files.
  1193. Used by the hooks for remember.el."
  1194. :group 'org-refile
  1195. :group 'org-remember
  1196. :type 'directory)
  1197. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1198. "Default target for storing notes.
  1199. Used by the hooks for remember.el. This can be a string, or nil to mean
  1200. the value of `remember-data-file'.
  1201. You can set this on a per-template basis with the variable
  1202. `org-remember-templates'."
  1203. :group 'org-refile
  1204. :group 'org-remember
  1205. :type '(choice
  1206. (const :tag "Default from remember-data-file" nil)
  1207. file))
  1208. (defcustom org-goto-interface 'outline
  1209. "The default interface to be used for `org-goto'.
  1210. Allowed values are:
  1211. outline The interface shows an outline of the relevant file
  1212. and the correct heading is found by moving through
  1213. the outline or by searching with incremental search.
  1214. outline-path-completion Headlines in the current buffer are offered via
  1215. completion. This is the interface also used by
  1216. the refile command."
  1217. :group 'org-refile
  1218. :type '(choice
  1219. (const :tag "Outline" outline)
  1220. (const :tag "Outline-path-completion" outline-path-completion)))
  1221. (defcustom org-goto-max-level 5
  1222. "Maximum level to be considered when running org-goto with refile interface."
  1223. :group 'org-refile
  1224. :type 'number)
  1225. (defcustom org-reverse-note-order nil
  1226. "Non-nil means, store new notes at the beginning of a file or entry.
  1227. When nil, new notes will be filed to the end of a file or entry.
  1228. This can also be a list with cons cells of regular expressions that
  1229. are matched against file names, and values."
  1230. :group 'org-remember
  1231. :group 'org-refile
  1232. :type '(choice
  1233. (const :tag "Reverse always" t)
  1234. (const :tag "Reverse never" nil)
  1235. (repeat :tag "By file name regexp"
  1236. (cons regexp boolean))))
  1237. (defcustom org-refile-targets nil
  1238. "Targets for refiling entries with \\[org-refile].
  1239. This is list of cons cells. Each cell contains:
  1240. - a specification of the files to be considered, either a list of files,
  1241. or a symbol whose function or variable value will be used to retrieve
  1242. a file name or a list of file names. If you use `org-agenda-files' for
  1243. that, all agenda files will be scanned for targets. Nil means, consider
  1244. headings in the current buffer.
  1245. - A specification of how to select find candidate refile targets. This
  1246. may be any of
  1247. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1248. This tag has to be present in all target headlines, inheritance will
  1249. not be considered.
  1250. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1251. todo keyword.
  1252. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1253. headlines that are refiling targets.
  1254. - a cons cell (:level . N). Any headline of level N is considered a target.
  1255. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1256. When this variable is nil, all top-level headlines in the current buffer
  1257. are used, equivalent to the value `((nil . (:level . 1))'."
  1258. :group 'org-refile
  1259. :type '(repeat
  1260. (cons
  1261. (choice :value org-agenda-files
  1262. (const :tag "All agenda files" org-agenda-files)
  1263. (const :tag "Current buffer" nil)
  1264. (function) (variable) (file))
  1265. (choice :tag "Identify target headline by"
  1266. (cons :tag "Specific tag" (const :value :tag) (string))
  1267. (cons :tag "TODO keyword" (const :value :todo) (string))
  1268. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1269. (cons :tag "Level number" (const :value :level) (integer))
  1270. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1271. (defcustom org-refile-use-outline-path nil
  1272. "Non-nil means, provide refile targets as paths.
  1273. So a level 3 headline will be available as level1/level2/level3.
  1274. When the value is `file', also include the file name (without directory)
  1275. into the path. When `full-file-path', include the full file path."
  1276. :group 'org-refile
  1277. :type '(choice
  1278. (const :tag "Not" nil)
  1279. (const :tag "Yes" t)
  1280. (const :tag "Start with file name" file)
  1281. (const :tag "Start with full file path" full-file-path)))
  1282. (defcustom org-outline-path-complete-in-steps t
  1283. "Non-nil means, complete the outline path in hierarchical steps.
  1284. When Org-mode uses the refile interface to select an outline path
  1285. \(see variable `org-refile-use-outline-path'), the completion of
  1286. the path can be done is a single go, or if can be done in steps down
  1287. the headline hierarchy. Going in steps is probably the best if you
  1288. do not use a special completion package like `ido' or `icicles'.
  1289. However, when using these packages, going in one step can be very
  1290. fast, while still showing the whole path to the entry."
  1291. :group 'org-refile
  1292. :type 'boolean)
  1293. (defgroup org-todo nil
  1294. "Options concerning TODO items in Org-mode."
  1295. :tag "Org TODO"
  1296. :group 'org)
  1297. (defgroup org-progress nil
  1298. "Options concerning Progress logging in Org-mode."
  1299. :tag "Org Progress"
  1300. :group 'org-time)
  1301. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1302. "List of TODO entry keyword sequences and their interpretation.
  1303. \\<org-mode-map>This is a list of sequences.
  1304. Each sequence starts with a symbol, either `sequence' or `type',
  1305. indicating if the keywords should be interpreted as a sequence of
  1306. action steps, or as different types of TODO items. The first
  1307. keywords are states requiring action - these states will select a headline
  1308. for inclusion into the global TODO list Org-mode produces. If one of
  1309. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1310. signify that no further action is necessary. If \"|\" is not found,
  1311. the last keyword is treated as the only DONE state of the sequence.
  1312. The command \\[org-todo] cycles an entry through these states, and one
  1313. additional state where no keyword is present. For details about this
  1314. cycling, see the manual.
  1315. TODO keywords and interpretation can also be set on a per-file basis with
  1316. the special #+SEQ_TODO and #+TYP_TODO lines.
  1317. Each keyword can optionally specify a character for fast state selection
  1318. \(in combination with the variable `org-use-fast-todo-selection')
  1319. and specifiers for state change logging, using the same syntax
  1320. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1321. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1322. indicates to record a time stamp each time this state is selected.
  1323. Each keyword may also specify if a timestamp or a note should be
  1324. recorded when entering or leaving the state, by adding additional
  1325. characters in the parenthesis after the keyword. This looks like this:
  1326. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1327. record only the time of the state change. With X and Y being either
  1328. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1329. Y when leaving the state if and only if the *target* state does not
  1330. define X. You may omit any of the fast-selection key or X or /Y,
  1331. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1332. For backward compatibility, this variable may also be just a list
  1333. of keywords - in this case the interpretation (sequence or type) will be
  1334. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1335. :group 'org-todo
  1336. :group 'org-keywords
  1337. :type '(choice
  1338. (repeat :tag "Old syntax, just keywords"
  1339. (string :tag "Keyword"))
  1340. (repeat :tag "New syntax"
  1341. (cons
  1342. (choice
  1343. :tag "Interpretation"
  1344. (const :tag "Sequence (cycling hits every state)" sequence)
  1345. (const :tag "Type (cycling directly to DONE)" type))
  1346. (repeat
  1347. (string :tag "Keyword"))))))
  1348. (defvar org-todo-keywords-1 nil
  1349. "All TODO and DONE keywords active in a buffer.")
  1350. (make-variable-buffer-local 'org-todo-keywords-1)
  1351. (defvar org-todo-keywords-for-agenda nil)
  1352. (defvar org-done-keywords-for-agenda nil)
  1353. (defvar org-todo-keyword-alist-for-agenda nil)
  1354. (defvar org-tag-alist-for-agenda nil)
  1355. (defvar org-agenda-contributing-files nil)
  1356. (defvar org-not-done-keywords nil)
  1357. (make-variable-buffer-local 'org-not-done-keywords)
  1358. (defvar org-done-keywords nil)
  1359. (make-variable-buffer-local 'org-done-keywords)
  1360. (defvar org-todo-heads nil)
  1361. (make-variable-buffer-local 'org-todo-heads)
  1362. (defvar org-todo-sets nil)
  1363. (make-variable-buffer-local 'org-todo-sets)
  1364. (defvar org-todo-log-states nil)
  1365. (make-variable-buffer-local 'org-todo-log-states)
  1366. (defvar org-todo-kwd-alist nil)
  1367. (make-variable-buffer-local 'org-todo-kwd-alist)
  1368. (defvar org-todo-key-alist nil)
  1369. (make-variable-buffer-local 'org-todo-key-alist)
  1370. (defvar org-todo-key-trigger nil)
  1371. (make-variable-buffer-local 'org-todo-key-trigger)
  1372. (defcustom org-todo-interpretation 'sequence
  1373. "Controls how TODO keywords are interpreted.
  1374. This variable is in principle obsolete and is only used for
  1375. backward compatibility, if the interpretation of todo keywords is
  1376. not given already in `org-todo-keywords'. See that variable for
  1377. more information."
  1378. :group 'org-todo
  1379. :group 'org-keywords
  1380. :type '(choice (const sequence)
  1381. (const type)))
  1382. (defcustom org-use-fast-todo-selection 'prefix
  1383. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1384. This variable describes if and under what circumstances the cycling
  1385. mechanism for TODO keywords will be replaced by a single-key, direct
  1386. selection scheme.
  1387. When nil, fast selection is never used.
  1388. When the symbol `prefix', it will be used when `org-todo' is called with
  1389. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1390. in an agenda buffer.
  1391. When t, fast selection is used by default. In this case, the prefix
  1392. argument forces cycling instead.
  1393. In all cases, the special interface is only used if access keys have actually
  1394. been assigned by the user, i.e. if keywords in the configuration are followed
  1395. by a letter in parenthesis, like TODO(t)."
  1396. :group 'org-todo
  1397. :type '(choice
  1398. (const :tag "Never" nil)
  1399. (const :tag "By default" t)
  1400. (const :tag "Only with C-u C-c C-t" prefix)))
  1401. (defcustom org-provide-todo-statistics t
  1402. "Non-nil means, update todo statistics after insert and toggle.
  1403. When this is set, todo statistics is updated in the parent of the current
  1404. entry each time a todo state is changed."
  1405. :group 'org-todo
  1406. :type 'boolean)
  1407. (defcustom org-after-todo-state-change-hook nil
  1408. "Hook which is run after the state of a TODO item was changed.
  1409. The new state (a string with a TODO keyword, or nil) is available in the
  1410. Lisp variable `state'."
  1411. :group 'org-todo
  1412. :type 'hook)
  1413. (defcustom org-todo-state-tags-triggers nil
  1414. "Tag changes that should be triggered by TODO state changes.
  1415. This is a list. Each entry is
  1416. (state-change (tag . flag) .......)
  1417. State-change can be a string with a state, and empty string to indicate the
  1418. state that has no TODO keyword, or it can be one of the symbols `todo'
  1419. or `done', meaning any not-done or done state, respectively."
  1420. :group 'org-todo
  1421. :group 'org-tags
  1422. :type '(repeat
  1423. (cons (choice :tag "When changing to"
  1424. (const :tag "Not-done state" todo)
  1425. (const :tag "Done state" done)
  1426. (string :tag "State"))
  1427. (repeat
  1428. (cons :tag "Tag action"
  1429. (string :tag "Tag")
  1430. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1431. (defcustom org-log-done nil
  1432. "Information to record when a task moves to the DONE state.
  1433. Possible values are:
  1434. nil Don't add anything, just change the keyword
  1435. time Add a time stamp to the task
  1436. note Prompt a closing note and add it with template `org-log-note-headings'
  1437. This option can also be set with on a per-file-basis with
  1438. #+STARTUP: nologdone
  1439. #+STARTUP: logdone
  1440. #+STARTUP: lognotedone
  1441. You can have local logging settings for a subtree by setting the LOGGING
  1442. property to one or more of these keywords."
  1443. :group 'org-todo
  1444. :group 'org-progress
  1445. :type '(choice
  1446. (const :tag "No logging" nil)
  1447. (const :tag "Record CLOSED timestamp" time)
  1448. (const :tag "Record CLOSED timestamp with closing note." note)))
  1449. ;; Normalize old uses of org-log-done.
  1450. (cond
  1451. ((eq org-log-done t) (setq org-log-done 'time))
  1452. ((and (listp org-log-done) (memq 'done org-log-done))
  1453. (setq org-log-done 'note)))
  1454. (defcustom org-log-note-clock-out nil
  1455. "Non-nil means, record a note when clocking out of an item.
  1456. This can also be configured on a per-file basis by adding one of
  1457. the following lines anywhere in the buffer:
  1458. #+STARTUP: lognoteclock-out
  1459. #+STARTUP: nolognoteclock-out"
  1460. :group 'org-todo
  1461. :group 'org-progress
  1462. :type 'boolean)
  1463. (defcustom org-log-done-with-time t
  1464. "Non-nil means, the CLOSED time stamp will contain date and time.
  1465. When nil, only the date will be recorded."
  1466. :group 'org-progress
  1467. :type 'boolean)
  1468. (defcustom org-log-note-headings
  1469. '((done . "CLOSING NOTE %t")
  1470. (state . "State %-12s %t")
  1471. (note . "Note taken on %t")
  1472. (clock-out . ""))
  1473. "Headings for notes added to entries.
  1474. The value is an alist, with the car being a symbol indicating the note
  1475. context, and the cdr is the heading to be used. The heading may also be the
  1476. empty string.
  1477. %t in the heading will be replaced by a time stamp.
  1478. %s will be replaced by the new TODO state, in double quotes.
  1479. %u will be replaced by the user name.
  1480. %U will be replaced by the full user name."
  1481. :group 'org-todo
  1482. :group 'org-progress
  1483. :type '(list :greedy t
  1484. (cons (const :tag "Heading when closing an item" done) string)
  1485. (cons (const :tag
  1486. "Heading when changing todo state (todo sequence only)"
  1487. state) string)
  1488. (cons (const :tag "Heading when just taking a note" note) string)
  1489. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1490. (unless (assq 'note org-log-note-headings)
  1491. (push '(note . "%t") org-log-note-headings))
  1492. (defcustom org-log-state-notes-insert-after-drawers nil
  1493. "Non-nil means, insert state change notes after any drawers in entry.
  1494. Only the drawers that *immediately* follow the headline and the
  1495. deadline/scheduled line are skipped.
  1496. When nil, insert notes right after the heading and perhaps the line
  1497. with deadline/scheduling if present."
  1498. :group 'org-todo
  1499. :group 'org-progress
  1500. :type 'boolean)
  1501. (defcustom org-log-states-order-reversed t
  1502. "Non-nil means, the latest state change note will be directly after heading.
  1503. When nil, the notes will be orderer according to time."
  1504. :group 'org-todo
  1505. :group 'org-progress
  1506. :type 'boolean)
  1507. (defcustom org-log-repeat 'time
  1508. "Non-nil means, record moving through the DONE state when triggering repeat.
  1509. An auto-repeating tasks is immediately switched back to TODO when marked
  1510. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1511. the TODO keyword definition, or recording a closing note by setting
  1512. `org-log-done', there will be no record of the task moving through DONE.
  1513. This variable forces taking a note anyway. Possible values are:
  1514. nil Don't force a record
  1515. time Record a time stamp
  1516. note Record a note
  1517. This option can also be set with on a per-file-basis with
  1518. #+STARTUP: logrepeat
  1519. #+STARTUP: lognoterepeat
  1520. #+STARTUP: nologrepeat
  1521. You can have local logging settings for a subtree by setting the LOGGING
  1522. property to one or more of these keywords."
  1523. :group 'org-todo
  1524. :group 'org-progress
  1525. :type '(choice
  1526. (const :tag "Don't force a record" nil)
  1527. (const :tag "Force recording the DONE state" time)
  1528. (const :tag "Force recording a note with the DONE state" note)))
  1529. (defgroup org-priorities nil
  1530. "Priorities in Org-mode."
  1531. :tag "Org Priorities"
  1532. :group 'org-todo)
  1533. (defcustom org-highest-priority ?A
  1534. "The highest priority of TODO items. A character like ?A, ?B etc.
  1535. Must have a smaller ASCII number than `org-lowest-priority'."
  1536. :group 'org-priorities
  1537. :type 'character)
  1538. (defcustom org-lowest-priority ?C
  1539. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1540. Must have a larger ASCII number than `org-highest-priority'."
  1541. :group 'org-priorities
  1542. :type 'character)
  1543. (defcustom org-default-priority ?B
  1544. "The default priority of TODO items.
  1545. This is the priority an item get if no explicit priority is given."
  1546. :group 'org-priorities
  1547. :type 'character)
  1548. (defcustom org-priority-start-cycle-with-default t
  1549. "Non-nil means, start with default priority when starting to cycle.
  1550. When this is nil, the first step in the cycle will be (depending on the
  1551. command used) one higher or lower that the default priority."
  1552. :group 'org-priorities
  1553. :type 'boolean)
  1554. (defgroup org-time nil
  1555. "Options concerning time stamps and deadlines in Org-mode."
  1556. :tag "Org Time"
  1557. :group 'org)
  1558. (defcustom org-insert-labeled-timestamps-at-point nil
  1559. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1560. When nil, these labeled time stamps are forces into the second line of an
  1561. entry, just after the headline. When scheduling from the global TODO list,
  1562. the time stamp will always be forced into the second line."
  1563. :group 'org-time
  1564. :type 'boolean)
  1565. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1566. "Formats for `format-time-string' which are used for time stamps.
  1567. It is not recommended to change this constant.")
  1568. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1569. "Number of minutes to round time stamps to.
  1570. These are two values, the first applies when first creating a time stamp.
  1571. The second applies when changing it with the commands `S-up' and `S-down'.
  1572. When changing the time stamp, this means that it will change in steps
  1573. of N minutes, as given by the second value.
  1574. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1575. numbers should be factors of 60, so for example 5, 10, 15.
  1576. When this is larger than 1, you can still force an exact time-stamp by using
  1577. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1578. and by using a prefix arg to `S-up/down' to specify the exact number
  1579. of minutes to shift."
  1580. :group 'org-time
  1581. :get '(lambda (var) ; Make sure all entries have 5 elements
  1582. (if (integerp (default-value var))
  1583. (list (default-value var) 5)
  1584. (default-value var)))
  1585. :type '(list
  1586. (integer :tag "when inserting times")
  1587. (integer :tag "when modifying times")))
  1588. ;; Normalize old customizations of this variable.
  1589. (when (integerp org-time-stamp-rounding-minutes)
  1590. (setq org-time-stamp-rounding-minutes
  1591. (list org-time-stamp-rounding-minutes
  1592. org-time-stamp-rounding-minutes)))
  1593. (defcustom org-display-custom-times nil
  1594. "Non-nil means, overlay custom formats over all time stamps.
  1595. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1596. To turn this on on a per-file basis, insert anywhere in the file:
  1597. #+STARTUP: customtime"
  1598. :group 'org-time
  1599. :set 'set-default
  1600. :type 'sexp)
  1601. (make-variable-buffer-local 'org-display-custom-times)
  1602. (defcustom org-time-stamp-custom-formats
  1603. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1604. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1605. These are overlayed over the default ISO format if the variable
  1606. `org-display-custom-times' is set. Time like %H:%M should be at the
  1607. end of the second format."
  1608. :group 'org-time
  1609. :type 'sexp)
  1610. (defun org-time-stamp-format (&optional long inactive)
  1611. "Get the right format for a time string."
  1612. (let ((f (if long (cdr org-time-stamp-formats)
  1613. (car org-time-stamp-formats))))
  1614. (if inactive
  1615. (concat "[" (substring f 1 -1) "]")
  1616. f)))
  1617. (defcustom org-time-clocksum-format "%d:%02d"
  1618. "The format string used when creating CLOCKSUM lines, or when
  1619. org-mode generates a time duration."
  1620. :group 'org-time
  1621. :type 'string)
  1622. (defcustom org-deadline-warning-days 14
  1623. "No. of days before expiration during which a deadline becomes active.
  1624. This variable governs the display in sparse trees and in the agenda.
  1625. When 0 or negative, it means use this number (the absolute value of it)
  1626. even if a deadline has a different individual lead time specified."
  1627. :group 'org-time
  1628. :group 'org-agenda-daily/weekly
  1629. :type 'number)
  1630. (defcustom org-read-date-prefer-future t
  1631. "Non-nil means, assume future for incomplete date input from user.
  1632. This affects the following situations:
  1633. 1. The user gives a day, but no month.
  1634. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1635. read this as the third of *next* month. However, if you enter \"17\",
  1636. it will be considered as *this* month.
  1637. 2. The user gives a month but not a year.
  1638. For example, if it is april and you enter \"feb 2\", this will be read
  1639. as feb 2, *next* year. \"May 5\", however, will be this year.
  1640. Currently this does not work for ISO week specifications.
  1641. When this option is nil, the current month and year will always be used
  1642. as defaults."
  1643. :group 'org-time
  1644. :type 'boolean)
  1645. (defcustom org-read-date-display-live t
  1646. "Non-nil means, display current interpretation of date prompt live.
  1647. This display will be in an overlay, in the minibuffer."
  1648. :group 'org-time
  1649. :type 'boolean)
  1650. (defcustom org-read-date-popup-calendar t
  1651. "Non-nil means, pop up a calendar when prompting for a date.
  1652. In the calendar, the date can be selected with mouse-1. However, the
  1653. minibuffer will also be active, and you can simply enter the date as well.
  1654. When nil, only the minibuffer will be available."
  1655. :group 'org-time
  1656. :type 'boolean)
  1657. (if (fboundp 'defvaralias)
  1658. (defvaralias 'org-popup-calendar-for-date-prompt
  1659. 'org-read-date-popup-calendar))
  1660. (defcustom org-extend-today-until 0
  1661. "The hour when your day really ends. Must be an integer.
  1662. This has influence for the following applications:
  1663. - When switching the agenda to \"today\". It it is still earlier than
  1664. the time given here, the day recognized as TODAY is actually yesterday.
  1665. - When a date is read from the user and it is still before the time given
  1666. here, the current date and time will be assumed to be yesterday, 23:59.
  1667. Also, timestamps inserted in remember templates follow this rule.
  1668. IMPORTANT: This is a feature whose implementation is and likely will
  1669. remain incomplete. Really, it is only here because past midnight seems to
  1670. be the favorite working time of John Wiegley :-)"
  1671. :group 'org-time
  1672. :type 'number)
  1673. (defcustom org-edit-timestamp-down-means-later nil
  1674. "Non-nil means, S-down will increase the time in a time stamp.
  1675. When nil, S-up will increase."
  1676. :group 'org-time
  1677. :type 'boolean)
  1678. (defcustom org-calendar-follow-timestamp-change t
  1679. "Non-nil means, make the calendar window follow timestamp changes.
  1680. When a timestamp is modified and the calendar window is visible, it will be
  1681. moved to the new date."
  1682. :group 'org-time
  1683. :type 'boolean)
  1684. (defgroup org-tags nil
  1685. "Options concerning tags in Org-mode."
  1686. :tag "Org Tags"
  1687. :group 'org)
  1688. (defcustom org-tag-alist nil
  1689. "List of tags allowed in Org-mode files.
  1690. When this list is nil, Org-mode will base TAG input on what is already in the
  1691. buffer.
  1692. The value of this variable is an alist, the car of each entry must be a
  1693. keyword as a string, the cdr may be a character that is used to select
  1694. that tag through the fast-tag-selection interface.
  1695. See the manual for details."
  1696. :group 'org-tags
  1697. :type '(repeat
  1698. (choice
  1699. (cons (string :tag "Tag name")
  1700. (character :tag "Access char"))
  1701. (const :tag "Start radio group" (:startgroup))
  1702. (const :tag "End radio group" (:endgroup)))))
  1703. (defvar org-file-tags nil
  1704. "List of tags that can be inherited by all entries in the file.
  1705. The tags will be inherited if the variable `org-use-tag-inheritance'
  1706. says they should be.
  1707. This variable is populated from #+TAG lines.")
  1708. (defcustom org-use-fast-tag-selection 'auto
  1709. "Non-nil means, use fast tag selection scheme.
  1710. This is a special interface to select and deselect tags with single keys.
  1711. When nil, fast selection is never used.
  1712. When the symbol `auto', fast selection is used if and only if selection
  1713. characters for tags have been configured, either through the variable
  1714. `org-tag-alist' or through a #+TAGS line in the buffer.
  1715. When t, fast selection is always used and selection keys are assigned
  1716. automatically if necessary."
  1717. :group 'org-tags
  1718. :type '(choice
  1719. (const :tag "Always" t)
  1720. (const :tag "Never" nil)
  1721. (const :tag "When selection characters are configured" 'auto)))
  1722. (defcustom org-fast-tag-selection-single-key nil
  1723. "Non-nil means, fast tag selection exits after first change.
  1724. When nil, you have to press RET to exit it.
  1725. During fast tag selection, you can toggle this flag with `C-c'.
  1726. This variable can also have the value `expert'. In this case, the window
  1727. displaying the tags menu is not even shown, until you press C-c again."
  1728. :group 'org-tags
  1729. :type '(choice
  1730. (const :tag "No" nil)
  1731. (const :tag "Yes" t)
  1732. (const :tag "Expert" expert)))
  1733. (defvar org-fast-tag-selection-include-todo nil
  1734. "Non-nil means, fast tags selection interface will also offer TODO states.
  1735. This is an undocumented feature, you should not rely on it.")
  1736. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1737. "The column to which tags should be indented in a headline.
  1738. If this number is positive, it specifies the column. If it is negative,
  1739. it means that the tags should be flushright to that column. For example,
  1740. -80 works well for a normal 80 character screen."
  1741. :group 'org-tags
  1742. :type 'integer)
  1743. (defcustom org-auto-align-tags t
  1744. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1745. These operations change the length of a headline and therefore shift
  1746. the tags around. With this options turned on, after each such operation
  1747. the tags are again aligned to `org-tags-column'."
  1748. :group 'org-tags
  1749. :type 'boolean)
  1750. (defcustom org-use-tag-inheritance t
  1751. "Non-nil means, tags in levels apply also for sublevels.
  1752. When nil, only the tags directly given in a specific line apply there.
  1753. This may also be a list of tags that should be inherited, or a regexp that
  1754. matches tags that should be inherited. Additional control is possible
  1755. with the variable `org-tags-exclude-from-inheritance' which gives an
  1756. explicit list of tags to be excluded from inheritance., even if the value of
  1757. `org-use-tag-inheritance' would select it for inheritance.
  1758. If this option is t, a match early-on in a tree can lead to a large
  1759. number of matches in the subtree when constructing the agenda or creating
  1760. a sparse tree. If you only want to see the first match in a tree during
  1761. a search, check out the variable `org-tags-match-list-sublevels'."
  1762. :group 'org-tags
  1763. :type '(choice
  1764. (const :tag "Not" nil)
  1765. (const :tag "Always" t)
  1766. (repeat :tag "Specific tags" (string :tag "Tag"))
  1767. (regexp :tag "Tags matched by regexp")))
  1768. (defcustom org-tags-exclude-from-inheritance nil
  1769. "List of tags that should never be inherited.
  1770. This is a way to exclude a few tags from inheritance. For way to do
  1771. the opposite, to actively allow inheritance for selected tags,
  1772. see the variable `org-use-tag-inheritance'."
  1773. :group 'org-tags
  1774. :type '(repeat (string :tag "Tag")))
  1775. (defun org-tag-inherit-p (tag)
  1776. "Check if TAG is one that should be inherited."
  1777. (cond
  1778. ((member tag org-tags-exclude-from-inheritance) nil)
  1779. ((eq org-use-tag-inheritance t) t)
  1780. ((not org-use-tag-inheritance) nil)
  1781. ((stringp org-use-tag-inheritance)
  1782. (string-match org-use-tag-inheritance tag))
  1783. ((listp org-use-tag-inheritance)
  1784. (member tag org-use-tag-inheritance))
  1785. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1786. (defcustom org-tags-match-list-sublevels t
  1787. "Non-nil means list also sublevels of headlines matching tag search.
  1788. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1789. the sublevels of a headline matching a tag search often also match
  1790. the same search. Listing all of them can create very long lists.
  1791. Setting this variable to nil causes subtrees of a match to be skipped.
  1792. This option is off by default, because inheritance in on. If you turn
  1793. inheritance off, you very likely want to turn this option on.
  1794. As a special case, if the tag search is restricted to TODO items, the
  1795. value of this variable is ignored and sublevels are always checked, to
  1796. make sure all corresponding TODO items find their way into the list.
  1797. This variable is semi-obsolete and probably should always be true. It
  1798. is better to limit inheritance to certain tags using the variables
  1799. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1800. :group 'org-tags
  1801. :type 'boolean)
  1802. (defvar org-tags-history nil
  1803. "History of minibuffer reads for tags.")
  1804. (defvar org-last-tags-completion-table nil
  1805. "The last used completion table for tags.")
  1806. (defvar org-after-tags-change-hook nil
  1807. "Hook that is run after the tags in a line have changed.")
  1808. (defgroup org-properties nil
  1809. "Options concerning properties in Org-mode."
  1810. :tag "Org Properties"
  1811. :group 'org)
  1812. (defcustom org-property-format "%-10s %s"
  1813. "How property key/value pairs should be formatted by `indent-line'.
  1814. When `indent-line' hits a property definition, it will format the line
  1815. according to this format, mainly to make sure that the values are
  1816. lined-up with respect to each other."
  1817. :group 'org-properties
  1818. :type 'string)
  1819. (defcustom org-use-property-inheritance nil
  1820. "Non-nil means, properties apply also for sublevels.
  1821. This setting is chiefly used during property searches. Turning it on can
  1822. cause significant overhead when doing a search, which is why it is not
  1823. on by default.
  1824. When nil, only the properties directly given in the current entry count.
  1825. When t, every property is inherited. The value may also be a list of
  1826. properties that should have inheritance, or a regular expression matching
  1827. properties that should be inherited.
  1828. However, note that some special properties use inheritance under special
  1829. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1830. and the properties ending in \"_ALL\" when they are used as descriptor
  1831. for valid values of a property.
  1832. Note for programmers:
  1833. When querying an entry with `org-entry-get', you can control if inheritance
  1834. should be used. By default, `org-entry-get' looks only at the local
  1835. properties. You can request inheritance by setting the inherit argument
  1836. to t (to force inheritance) or to `selective' (to respect the setting
  1837. in this variable)."
  1838. :group 'org-properties
  1839. :type '(choice
  1840. (const :tag "Not" nil)
  1841. (const :tag "Always" t)
  1842. (repeat :tag "Specific properties" (string :tag "Property"))
  1843. (regexp :tag "Properties matched by regexp")))
  1844. (defun org-property-inherit-p (property)
  1845. "Check if PROPERTY is one that should be inherited."
  1846. (cond
  1847. ((eq org-use-property-inheritance t) t)
  1848. ((not org-use-property-inheritance) nil)
  1849. ((stringp org-use-property-inheritance)
  1850. (string-match org-use-property-inheritance property))
  1851. ((listp org-use-property-inheritance)
  1852. (member property org-use-property-inheritance))
  1853. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1854. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1855. "The default column format, if no other format has been defined.
  1856. This variable can be set on the per-file basis by inserting a line
  1857. #+COLUMNS: %25ITEM ....."
  1858. :group 'org-properties
  1859. :type 'string)
  1860. (defcustom org-columns-ellipses ".."
  1861. "The ellipses to be used when a field in column view is truncated.
  1862. When this is the empty string, as many characters as possible are shown,
  1863. but then there will be no visual indication that the field has been truncated.
  1864. When this is a string of length N, the last N characters of a truncated
  1865. field are replaced by this string. If the column is narrower than the
  1866. ellipses string, only part of the ellipses string will be shown."
  1867. :group 'org-properties
  1868. :type 'string)
  1869. (defcustom org-columns-modify-value-for-display-function nil
  1870. "Function that modifies values for display in column view.
  1871. For example, it can be used to cut out a certain part from a time stamp.
  1872. The function must take 2 arguments:
  1873. column-title The title of the column (*not* the property name)
  1874. value The value that should be modified.
  1875. The function should return the value that should be displayed,
  1876. or nil if the normal value should be used."
  1877. :group 'org-properties
  1878. :type 'function)
  1879. (defcustom org-effort-property "Effort"
  1880. "The property that is being used to keep track of effort estimates.
  1881. Effort estimates given in this property need to have the format H:MM."
  1882. :group 'org-properties
  1883. :group 'org-progress
  1884. :type '(string :tag "Property"))
  1885. (defconst org-global-properties-fixed
  1886. '(("VISIBILITY_ALL" . "folded children content all"))
  1887. "List of property/value pairs that can be inherited by any entry.
  1888. These are fixed values, for the preset properties.")
  1889. (defcustom org-global-properties nil
  1890. "List of property/value pairs that can be inherited by any entry.
  1891. You can set buffer-local values for the same purpose in the variable
  1892. `org-file-properties' this by adding lines like
  1893. #+PROPERTY: NAME VALUE"
  1894. :group 'org-properties
  1895. :type '(repeat
  1896. (cons (string :tag "Property")
  1897. (string :tag "Value"))))
  1898. (defvar org-file-properties nil
  1899. "List of property/value pairs that can be inherited by any entry.
  1900. Valid for the current buffer.
  1901. This variable is populated from #+PROPERTY lines.")
  1902. (make-variable-buffer-local 'org-file-properties)
  1903. (defgroup org-agenda nil
  1904. "Options concerning agenda views in Org-mode."
  1905. :tag "Org Agenda"
  1906. :group 'org)
  1907. (defvar org-category nil
  1908. "Variable used by org files to set a category for agenda display.
  1909. Such files should use a file variable to set it, for example
  1910. # -*- mode: org; org-category: \"ELisp\"
  1911. or contain a special line
  1912. #+CATEGORY: ELisp
  1913. If the file does not specify a category, then file's base name
  1914. is used instead.")
  1915. (make-variable-buffer-local 'org-category)
  1916. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1917. (defcustom org-agenda-files nil
  1918. "The files to be used for agenda display.
  1919. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1920. \\[org-remove-file]. You can also use customize to edit the list.
  1921. If an entry is a directory, all files in that directory that are matched by
  1922. `org-agenda-file-regexp' will be part of the file list.
  1923. If the value of the variable is not a list but a single file name, then
  1924. the list of agenda files is actually stored and maintained in that file, one
  1925. agenda file per line."
  1926. :group 'org-agenda
  1927. :type '(choice
  1928. (repeat :tag "List of files and directories" file)
  1929. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1930. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1931. "Regular expression to match files for `org-agenda-files'.
  1932. If any element in the list in that variable contains a directory instead
  1933. of a normal file, all files in that directory that are matched by this
  1934. regular expression will be included."
  1935. :group 'org-agenda
  1936. :type 'regexp)
  1937. (defcustom org-agenda-text-search-extra-files nil
  1938. "List of extra files to be searched by text search commands.
  1939. These files will be search in addition to the agenda files by the
  1940. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1941. Note that these files will only be searched for text search commands,
  1942. not for the other agenda views like todo lists, tag searches or the weekly
  1943. agenda. This variable is intended to list notes and possibly archive files
  1944. that should also be searched by these two commands.
  1945. In fact, if the first element in the list is the symbol `agenda-archives',
  1946. than all archive files of all agenda files will be added to the search
  1947. scope."
  1948. :group 'org-agenda
  1949. :type '(set :greedy t
  1950. (const :tag "Agenda Archives" agenda-archives)
  1951. (repeat :inline t (file))))
  1952. (if (fboundp 'defvaralias)
  1953. (defvaralias 'org-agenda-multi-occur-extra-files
  1954. 'org-agenda-text-search-extra-files))
  1955. (defcustom org-agenda-skip-unavailable-files nil
  1956. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1957. A nil value means to remove them, after a query, from the list."
  1958. :group 'org-agenda
  1959. :type 'boolean)
  1960. (defcustom org-calendar-to-agenda-key [?c]
  1961. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1962. The command `org-calendar-goto-agenda' will be bound to this key. The
  1963. default is the character `c' because then `c' can be used to switch back and
  1964. forth between agenda and calendar."
  1965. :group 'org-agenda
  1966. :type 'sexp)
  1967. (defcustom org-calendar-agenda-action-key [?k]
  1968. "The key to be installed in `calendar-mode-map' for agenda-action.
  1969. The command `org-agenda-action' will be bound to this key. The
  1970. default is the character `k' because we use the same key in the agenda."
  1971. :group 'org-agenda
  1972. :type 'sexp)
  1973. (eval-after-load "calendar"
  1974. '(progn
  1975. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1976. 'org-calendar-goto-agenda)
  1977. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1978. 'org-agenda-action)))
  1979. (defgroup org-latex nil
  1980. "Options for embedding LaTeX code into Org-mode."
  1981. :tag "Org LaTeX"
  1982. :group 'org)
  1983. (defcustom org-format-latex-options
  1984. '(:foreground default :background default :scale 1.0
  1985. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1986. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  1987. "Options for creating images from LaTeX fragments.
  1988. This is a property list with the following properties:
  1989. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1990. `default' means use the foreground of the default face.
  1991. :background the background color, or \"Transparent\".
  1992. `default' means use the background of the default face.
  1993. :scale a scaling factor for the size of the images.
  1994. :html-foreground, :html-background, :html-scale
  1995. the same numbers for HTML export.
  1996. :matchers a list indicating which matchers should be used to
  1997. find LaTeX fragments. Valid members of this list are:
  1998. \"begin\" find environments
  1999. \"$1\" find single characters surrounded by $.$
  2000. \"$\" find math expressions surrounded by $...$
  2001. \"$$\" find math expressions surrounded by $$....$$
  2002. \"\\(\" find math expressions surrounded by \\(...\\)
  2003. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2004. :group 'org-latex
  2005. :type 'plist)
  2006. (defcustom org-format-latex-header "\\documentclass{article}
  2007. \\usepackage{fullpage} % do not remove
  2008. \\usepackage{amssymb}
  2009. \\usepackage[usenames]{color}
  2010. \\usepackage{amsmath}
  2011. \\usepackage{latexsym}
  2012. \\usepackage[mathscr]{eucal}
  2013. \\pagestyle{empty} % do not remove"
  2014. "The document header used for processing LaTeX fragments."
  2015. :group 'org-latex
  2016. :type 'string)
  2017. (defgroup org-font-lock nil
  2018. "Font-lock settings for highlighting in Org-mode."
  2019. :tag "Org Font Lock"
  2020. :group 'org)
  2021. (defcustom org-level-color-stars-only nil
  2022. "Non-nil means fontify only the stars in each headline.
  2023. When nil, the entire headline is fontified.
  2024. Changing it requires restart of `font-lock-mode' to become effective
  2025. also in regions already fontified."
  2026. :group 'org-font-lock
  2027. :type 'boolean)
  2028. (defcustom org-hide-leading-stars nil
  2029. "Non-nil means, hide the first N-1 stars in a headline.
  2030. This works by using the face `org-hide' for these stars. This
  2031. face is white for a light background, and black for a dark
  2032. background. You may have to customize the face `org-hide' to
  2033. make this work.
  2034. Changing it requires restart of `font-lock-mode' to become effective
  2035. also in regions already fontified.
  2036. You may also set this on a per-file basis by adding one of the following
  2037. lines to the buffer:
  2038. #+STARTUP: hidestars
  2039. #+STARTUP: showstars"
  2040. :group 'org-font-lock
  2041. :type 'boolean)
  2042. (defcustom org-fontify-done-headline nil
  2043. "Non-nil means, change the face of a headline if it is marked DONE.
  2044. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2045. When this is non-nil, the headline after the keyword is set to the
  2046. `org-headline-done' as an additional indication."
  2047. :group 'org-font-lock
  2048. :type 'boolean)
  2049. (defcustom org-fontify-emphasized-text t
  2050. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2051. Changing this variable requires a restart of Emacs to take effect."
  2052. :group 'org-font-lock
  2053. :type 'boolean)
  2054. (defcustom org-highlight-latex-fragments-and-specials nil
  2055. "Non-nil means, fontify what is treated specially by the exporters."
  2056. :group 'org-font-lock
  2057. :type 'boolean)
  2058. (defcustom org-hide-emphasis-markers nil
  2059. "Non-nil mean font-lock should hide the emphasis marker characters."
  2060. :group 'org-font-lock
  2061. :type 'boolean)
  2062. (defvar org-emph-re nil
  2063. "Regular expression for matching emphasis.")
  2064. (defvar org-verbatim-re nil
  2065. "Regular expression for matching verbatim text.")
  2066. (defvar org-emphasis-regexp-components) ; defined just below
  2067. (defvar org-emphasis-alist) ; defined just below
  2068. (defun org-set-emph-re (var val)
  2069. "Set variable and compute the emphasis regular expression."
  2070. (set var val)
  2071. (when (and (boundp 'org-emphasis-alist)
  2072. (boundp 'org-emphasis-regexp-components)
  2073. org-emphasis-alist org-emphasis-regexp-components)
  2074. (let* ((e org-emphasis-regexp-components)
  2075. (pre (car e))
  2076. (post (nth 1 e))
  2077. (border (nth 2 e))
  2078. (body (nth 3 e))
  2079. (nl (nth 4 e))
  2080. (body1 (concat body "*?"))
  2081. (markers (mapconcat 'car org-emphasis-alist ""))
  2082. (vmarkers (mapconcat
  2083. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2084. org-emphasis-alist "")))
  2085. ;; make sure special characters appear at the right position in the class
  2086. (if (string-match "\\^" markers)
  2087. (setq markers (concat (replace-match "" t t markers) "^")))
  2088. (if (string-match "-" markers)
  2089. (setq markers (concat (replace-match "" t t markers) "-")))
  2090. (if (string-match "\\^" vmarkers)
  2091. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2092. (if (string-match "-" vmarkers)
  2093. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2094. (if (> nl 0)
  2095. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2096. (int-to-string nl) "\\}")))
  2097. ;; Make the regexp
  2098. (setq org-emph-re
  2099. (concat "\\([" pre "]\\|^\\)"
  2100. "\\("
  2101. "\\([" markers "]\\)"
  2102. "\\("
  2103. "[^" border "]\\|"
  2104. "[^" border "]"
  2105. body1
  2106. "[^" border "]"
  2107. "\\)"
  2108. "\\3\\)"
  2109. "\\([" post "]\\|$\\)"))
  2110. (setq org-verbatim-re
  2111. (concat "\\([" pre "]\\|^\\)"
  2112. "\\("
  2113. "\\([" vmarkers "]\\)"
  2114. "\\("
  2115. "[^" border "]\\|"
  2116. "[^" border "]"
  2117. body1
  2118. "[^" border "]"
  2119. "\\)"
  2120. "\\3\\)"
  2121. "\\([" post "]\\|$\\)")))))
  2122. (defcustom org-emphasis-regexp-components
  2123. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2124. "Components used to build the regular expression for emphasis.
  2125. This is a list with 6 entries. Terminology: In an emphasis string
  2126. like \" *strong word* \", we call the initial space PREMATCH, the final
  2127. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2128. and \"trong wor\" is the body. The different components in this variable
  2129. specify what is allowed/forbidden in each part:
  2130. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2131. post Chars allowed as postmatch. End of line will be allowed too.
  2132. border The chars *forbidden* as border characters.
  2133. body-regexp A regexp like \".\" to match a body character. Don't use
  2134. non-shy groups here, and don't allow newline here.
  2135. newline The maximum number of newlines allowed in an emphasis exp.
  2136. Use customize to modify this, or restart Emacs after changing it."
  2137. :group 'org-font-lock
  2138. :set 'org-set-emph-re
  2139. :type '(list
  2140. (sexp :tag "Allowed chars in pre ")
  2141. (sexp :tag "Allowed chars in post ")
  2142. (sexp :tag "Forbidden chars in border ")
  2143. (sexp :tag "Regexp for body ")
  2144. (integer :tag "number of newlines allowed")
  2145. (option (boolean :tag "Please ignore this button"))))
  2146. (defcustom org-emphasis-alist
  2147. `(("*" bold "<b>" "</b>")
  2148. ("/" italic "<i>" "</i>")
  2149. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2150. ("=" org-code "<code>" "</code>" verbatim)
  2151. ("~" org-verbatim "<code>" "</code>" verbatim)
  2152. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2153. "<del>" "</del>")
  2154. )
  2155. "Special syntax for emphasized text.
  2156. Text starting and ending with a special character will be emphasized, for
  2157. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2158. characters, the face to be used by font-lock for highlighting in Org-mode
  2159. Emacs buffers, and the HTML tags to be used for this.
  2160. Use customize to modify this, or restart Emacs after changing it."
  2161. :group 'org-font-lock
  2162. :set 'org-set-emph-re
  2163. :type '(repeat
  2164. (list
  2165. (string :tag "Marker character")
  2166. (choice
  2167. (face :tag "Font-lock-face")
  2168. (plist :tag "Face property list"))
  2169. (string :tag "HTML start tag")
  2170. (string :tag "HTML end tag")
  2171. (option (const verbatim)))))
  2172. ;;; Miscellaneous options
  2173. (defgroup org-completion nil
  2174. "Completion in Org-mode."
  2175. :tag "Org Completion"
  2176. :group 'org)
  2177. (defcustom org-completion-use-ido nil
  2178. "Non-nil means, use ido completion wherever possible.
  2179. Note that `ido-mode' must be active for this variable to be relevant.
  2180. If you decide to turn this variable on, you might well want to turn off
  2181. `org-outline-path-complete-in-steps'."
  2182. :group 'org-completion
  2183. :type 'boolean)
  2184. (defcustom org-completion-fallback-command 'hippie-expand
  2185. "The expansion command called by \\[org-complete] in normal context.
  2186. Normal means, no org-mode-specific context."
  2187. :group 'org-completion
  2188. :type 'function)
  2189. ;;; Functions and variables from ther packages
  2190. ;; Declared here to avoid compiler warnings
  2191. ;; XEmacs only
  2192. (defvar outline-mode-menu-heading)
  2193. (defvar outline-mode-menu-show)
  2194. (defvar outline-mode-menu-hide)
  2195. (defvar zmacs-regions) ; XEmacs regions
  2196. ;; Emacs only
  2197. (defvar mark-active)
  2198. ;; Various packages
  2199. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2200. (declare-function calendar-forward-day "cal-move" (arg))
  2201. (declare-function calendar-goto-date "cal-move" (date))
  2202. (declare-function calendar-goto-today "cal-move" ())
  2203. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2204. (defvar calc-embedded-close-formula)
  2205. (defvar calc-embedded-open-formula)
  2206. (declare-function cdlatex-tab "ext:cdlatex" ())
  2207. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2208. (defvar font-lock-unfontify-region-function)
  2209. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2210. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2211. (defvar iswitchb-temp-buflist)
  2212. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2213. (defvar org-agenda-tags-todo-honor-ignore-options)
  2214. (declare-function org-agenda-skip "org-agenda" ())
  2215. (declare-function org-format-agenda-item "org-agenda"
  2216. (extra txt &optional category tags dotime noprefix remove-re))
  2217. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2218. (declare-function org-agenda-change-all-lines "org-agenda"
  2219. (newhead hdmarker &optional fixface just-this))
  2220. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2221. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2222. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2223. (beg end))
  2224. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2225. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2226. "org-agenda" (&optional end))
  2227. (declare-function parse-time-string "parse-time" (string))
  2228. (declare-function remember "remember" (&optional initial))
  2229. (declare-function remember-buffer-desc "remember" ())
  2230. (declare-function remember-finalize "remember" ())
  2231. (defvar remember-save-after-remembering)
  2232. (defvar remember-data-file)
  2233. (defvar remember-register)
  2234. (defvar remember-buffer)
  2235. (defvar remember-handler-functions)
  2236. (defvar remember-annotation-functions)
  2237. (defvar texmathp-why)
  2238. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2239. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2240. (defvar w3m-current-url)
  2241. (defvar w3m-current-title)
  2242. (defvar org-latex-regexps)
  2243. ;;; Autoload and prepare some org modules
  2244. ;; Some table stuff that needs to be defined here, because it is used
  2245. ;; by the functions setting up org-mode or checking for table context.
  2246. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2247. "Detects an org-type or table-type table.")
  2248. (defconst org-table-line-regexp "^[ \t]*|"
  2249. "Detects an org-type table line.")
  2250. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2251. "Detects an org-type table line.")
  2252. (defconst org-table-hline-regexp "^[ \t]*|-"
  2253. "Detects an org-type table hline.")
  2254. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2255. "Detects a table-type table hline.")
  2256. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2257. "Searching from within a table (any type) this finds the first line
  2258. outside the table.")
  2259. ;; Autoload the functions in org-table.el that are needed by functions here.
  2260. (eval-and-compile
  2261. (org-autoload "org-table"
  2262. '(org-table-align org-table-begin org-table-blank-field
  2263. org-table-convert org-table-convert-region org-table-copy-down
  2264. org-table-copy-region org-table-create
  2265. org-table-create-or-convert-from-region
  2266. org-table-create-with-table.el org-table-current-dline
  2267. org-table-cut-region org-table-delete-column org-table-edit-field
  2268. org-table-edit-formulas org-table-end org-table-eval-formula
  2269. org-table-export org-table-field-info
  2270. org-table-get-stored-formulas org-table-goto-column
  2271. org-table-hline-and-move org-table-import org-table-insert-column
  2272. org-table-insert-hline org-table-insert-row org-table-iterate
  2273. org-table-justify-field-maybe org-table-kill-row
  2274. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2275. org-table-move-column org-table-move-column-left
  2276. org-table-move-column-right org-table-move-row
  2277. org-table-move-row-down org-table-move-row-up
  2278. org-table-next-field org-table-next-row org-table-paste-rectangle
  2279. org-table-previous-field org-table-recalculate
  2280. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2281. org-table-toggle-coordinate-overlays
  2282. org-table-toggle-formula-debugger org-table-wrap-region
  2283. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2284. (defun org-at-table-p (&optional table-type)
  2285. "Return t if the cursor is inside an org-type table.
  2286. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2287. (if org-enable-table-editor
  2288. (save-excursion
  2289. (beginning-of-line 1)
  2290. (looking-at (if table-type org-table-any-line-regexp
  2291. org-table-line-regexp)))
  2292. nil))
  2293. (defsubst org-table-p () (org-at-table-p))
  2294. (defun org-at-table.el-p ()
  2295. "Return t if and only if we are at a table.el table."
  2296. (and (org-at-table-p 'any)
  2297. (save-excursion
  2298. (goto-char (org-table-begin 'any))
  2299. (looking-at org-table1-hline-regexp))))
  2300. (defun org-table-recognize-table.el ()
  2301. "If there is a table.el table nearby, recognize it and move into it."
  2302. (if org-table-tab-recognizes-table.el
  2303. (if (org-at-table.el-p)
  2304. (progn
  2305. (beginning-of-line 1)
  2306. (if (looking-at org-table-dataline-regexp)
  2307. nil
  2308. (if (looking-at org-table1-hline-regexp)
  2309. (progn
  2310. (beginning-of-line 2)
  2311. (if (looking-at org-table-any-border-regexp)
  2312. (beginning-of-line -1)))))
  2313. (if (re-search-forward "|" (org-table-end t) t)
  2314. (progn
  2315. (require 'table)
  2316. (if (table--at-cell-p (point))
  2317. t
  2318. (message "recognizing table.el table...")
  2319. (table-recognize-table)
  2320. (message "recognizing table.el table...done")))
  2321. (error "This should not happen..."))
  2322. t)
  2323. nil)
  2324. nil))
  2325. (defun org-at-table-hline-p ()
  2326. "Return t if the cursor is inside a hline in a table."
  2327. (if org-enable-table-editor
  2328. (save-excursion
  2329. (beginning-of-line 1)
  2330. (looking-at org-table-hline-regexp))
  2331. nil))
  2332. (defvar org-table-clean-did-remove-column nil)
  2333. (defun org-table-map-tables (function)
  2334. "Apply FUNCTION to the start of all tables in the buffer."
  2335. (save-excursion
  2336. (save-restriction
  2337. (widen)
  2338. (goto-char (point-min))
  2339. (while (re-search-forward org-table-any-line-regexp nil t)
  2340. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2341. (beginning-of-line 1)
  2342. (if (looking-at org-table-line-regexp)
  2343. (save-excursion (funcall function)))
  2344. (re-search-forward org-table-any-border-regexp nil 1))))
  2345. (message "Mapping tables: done"))
  2346. ;; Declare and autoload functions from org-exp.el
  2347. (declare-function org-default-export-plist "org-exp")
  2348. (declare-function org-infile-export-plist "org-exp")
  2349. (declare-function org-get-current-options "org-exp")
  2350. (eval-and-compile
  2351. (org-autoload "org-exp"
  2352. '(org-export org-export-as-ascii org-export-visible
  2353. org-insert-export-options-template org-export-as-html-and-open
  2354. org-export-as-html-batch org-export-as-html-to-buffer
  2355. org-replace-region-by-html org-export-region-as-html
  2356. org-export-as-html org-export-icalendar-this-file
  2357. org-export-icalendar-all-agenda-files
  2358. org-table-clean-before-export
  2359. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2360. ;; Declare and autoload functions from org-agenda.el
  2361. (eval-and-compile
  2362. (org-autoload "org-agenda"
  2363. '(org-agenda org-agenda-list org-search-view
  2364. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2365. org-diary org-agenda-to-appt
  2366. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2367. ;; Autoload org-remember
  2368. (eval-and-compile
  2369. (org-autoload "org-remember"
  2370. '(org-remember-insinuate org-remember-annotation
  2371. org-remember-apply-template org-remember org-remember-handler)))
  2372. ;; Autoload org-clock.el
  2373. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2374. (beg end))
  2375. (declare-function org-clock-update-mode-line "org-clock" ())
  2376. (defvar org-clock-start-time)
  2377. (defvar org-clock-marker (make-marker)
  2378. "Marker recording the last clock-in.")
  2379. (eval-and-compile
  2380. (org-autoload
  2381. "org-clock"
  2382. '(org-clock-in org-clock-out org-clock-cancel
  2383. org-clock-goto org-clock-sum org-clock-display
  2384. org-clock-remove-overlays org-clock-report
  2385. org-clocktable-shift org-dblock-write:clocktable
  2386. org-get-clocktable)))
  2387. (defun org-clock-update-time-maybe ()
  2388. "If this is a CLOCK line, update it and return t.
  2389. Otherwise, return nil."
  2390. (interactive)
  2391. (save-excursion
  2392. (beginning-of-line 1)
  2393. (skip-chars-forward " \t")
  2394. (when (looking-at org-clock-string)
  2395. (let ((re (concat "[ \t]*" org-clock-string
  2396. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2397. "\\([ \t]*=>.*\\)?\\)?"))
  2398. ts te h m s neg)
  2399. (cond
  2400. ((not (looking-at re))
  2401. nil)
  2402. ((not (match-end 2))
  2403. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2404. (> org-clock-marker (point))
  2405. (<= org-clock-marker (point-at-eol)))
  2406. ;; The clock is running here
  2407. (setq org-clock-start-time
  2408. (apply 'encode-time
  2409. (org-parse-time-string (match-string 1))))
  2410. (org-clock-update-mode-line)))
  2411. (t
  2412. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2413. (end-of-line 1)
  2414. (setq ts (match-string 1)
  2415. te (match-string 3))
  2416. (setq s (- (time-to-seconds
  2417. (apply 'encode-time (org-parse-time-string te)))
  2418. (time-to-seconds
  2419. (apply 'encode-time (org-parse-time-string ts))))
  2420. neg (< s 0)
  2421. s (abs s)
  2422. h (floor (/ s 3600))
  2423. s (- s (* 3600 h))
  2424. m (floor (/ s 60))
  2425. s (- s (* 60 s)))
  2426. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2427. t))))))
  2428. (defun org-check-running-clock ()
  2429. "Check if the current buffer contains the running clock.
  2430. If yes, offer to stop it and to save the buffer with the changes."
  2431. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2432. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2433. (buffer-name))))
  2434. (org-clock-out)
  2435. (when (y-or-n-p "Save changed buffer?")
  2436. (save-buffer))))
  2437. (defun org-clocktable-try-shift (dir n)
  2438. "Check if this line starts a clock table, if yes, shift the time block."
  2439. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2440. (org-clocktable-shift dir n)))
  2441. ;; Autoload org-timer.el
  2442. ;(declare-function org-timer "org-timer")
  2443. (eval-and-compile
  2444. (org-autoload
  2445. "org-timer"
  2446. '(org-timer-start org-timer org-timer-item
  2447. org-timer-change-times-in-region)))
  2448. ;; Autoload archiving code
  2449. ;; The stuff that is needed for cycling and tags has to be defined here.
  2450. (defgroup org-archive nil
  2451. "Options concerning archiving in Org-mode."
  2452. :tag "Org Archive"
  2453. :group 'org-structure)
  2454. (defcustom org-archive-location "%s_archive::"
  2455. "The location where subtrees should be archived.
  2456. The value of this variable is a string, consisting of two parts,
  2457. separated by a double-colon. The first part is a filename and
  2458. the second part is a headline.
  2459. When the filename is omitted, archiving happens in the same file.
  2460. %s in the filename will be replaced by the current file
  2461. name (without the directory part). Archiving to a different file
  2462. is useful to keep archived entries from contributing to the
  2463. Org-mode Agenda.
  2464. The archived entries will be filed as subtrees of the specified
  2465. headline. When the headline is omitted, the subtrees are simply
  2466. filed away at the end of the file, as top-level entries. Also in
  2467. the heading you can use %s to represent the file name, this can be
  2468. useful when using the same archive for a number of different files.
  2469. Here are a few examples:
  2470. \"%s_archive::\"
  2471. If the current file is Projects.org, archive in file
  2472. Projects.org_archive, as top-level trees. This is the default.
  2473. \"::* Archived Tasks\"
  2474. Archive in the current file, under the top-level headline
  2475. \"* Archived Tasks\".
  2476. \"~/org/archive.org::\"
  2477. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2478. \"~/org/archive.org::From %s\"
  2479. Archive in file ~/org/archive.org (absolute path), und headlines
  2480. \"From FILENAME\" where file name is the current file name.
  2481. \"basement::** Finished Tasks\"
  2482. Archive in file ./basement (relative path), as level 3 trees
  2483. below the level 2 heading \"** Finished Tasks\".
  2484. You may set this option on a per-file basis by adding to the buffer a
  2485. line like
  2486. #+ARCHIVE: basement::** Finished Tasks
  2487. You may also define it locally for a subtree by setting an ARCHIVE property
  2488. in the entry. If such a property is found in an entry, or anywhere up
  2489. the hierarchy, it will be used."
  2490. :group 'org-archive
  2491. :type 'string)
  2492. (defcustom org-archive-tag "ARCHIVE"
  2493. "The tag that marks a subtree as archived.
  2494. An archived subtree does not open during visibility cycling, and does
  2495. not contribute to the agenda listings.
  2496. After changing this, font-lock must be restarted in the relevant buffers to
  2497. get the proper fontification."
  2498. :group 'org-archive
  2499. :group 'org-keywords
  2500. :type 'string)
  2501. (defcustom org-agenda-skip-archived-trees t
  2502. "Non-nil means, the agenda will skip any items located in archived trees.
  2503. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2504. variable is no longer recommended, you should leave it at the value t.
  2505. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2506. :group 'org-archive
  2507. :group 'org-agenda-skip
  2508. :type 'boolean)
  2509. (defcustom org-cycle-open-archived-trees nil
  2510. "Non-nil means, `org-cycle' will open archived trees.
  2511. An archived tree is a tree marked with the tag ARCHIVE.
  2512. When nil, archived trees will stay folded. You can still open them with
  2513. normal outline commands like `show-all', but not with the cycling commands."
  2514. :group 'org-archive
  2515. :group 'org-cycle
  2516. :type 'boolean)
  2517. (defcustom org-sparse-tree-open-archived-trees nil
  2518. "Non-nil means sparse tree construction shows matches in archived trees.
  2519. When nil, matches in these trees are highlighted, but the trees are kept in
  2520. collapsed state."
  2521. :group 'org-archive
  2522. :group 'org-sparse-trees
  2523. :type 'boolean)
  2524. (defun org-cycle-hide-archived-subtrees (state)
  2525. "Re-hide all archived subtrees after a visibility state change."
  2526. (when (and (not org-cycle-open-archived-trees)
  2527. (not (memq state '(overview folded))))
  2528. (save-excursion
  2529. (let* ((globalp (memq state '(contents all)))
  2530. (beg (if globalp (point-min) (point)))
  2531. (end (if globalp (point-max) (org-end-of-subtree t))))
  2532. (org-hide-archived-subtrees beg end)
  2533. (goto-char beg)
  2534. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2535. (message "%s" (substitute-command-keys
  2536. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2537. (defun org-force-cycle-archived ()
  2538. "Cycle subtree even if it is archived."
  2539. (interactive)
  2540. (setq this-command 'org-cycle)
  2541. (let ((org-cycle-open-archived-trees t))
  2542. (call-interactively 'org-cycle)))
  2543. (defun org-hide-archived-subtrees (beg end)
  2544. "Re-hide all archived subtrees after a visibility state change."
  2545. (save-excursion
  2546. (let* ((re (concat ":" org-archive-tag ":")))
  2547. (goto-char beg)
  2548. (while (re-search-forward re end t)
  2549. (and (org-on-heading-p) (hide-subtree))
  2550. (org-end-of-subtree t)))))
  2551. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2552. (eval-and-compile
  2553. (org-autoload "org-archive"
  2554. '(org-add-archive-files org-archive-subtree
  2555. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2556. ;; Autoload Column View Code
  2557. (declare-function org-columns-number-to-string "org-colview")
  2558. (declare-function org-columns-get-format-and-top-level "org-colview")
  2559. (declare-function org-columns-compute "org-colview")
  2560. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2561. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2562. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2563. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2564. ;; Autoload ID code
  2565. (declare-function org-id-store-link "org-id")
  2566. (org-autoload "org-id"
  2567. '(org-id-get-create org-id-new org-id-copy org-id-get
  2568. org-id-get-with-outline-path-completion
  2569. org-id-get-with-outline-drilling
  2570. org-id-goto org-id-find org-id-store-link))
  2571. ;;; Variables for pre-computed regular expressions, all buffer local
  2572. (defvar org-drawer-regexp nil
  2573. "Matches first line of a hidden block.")
  2574. (make-variable-buffer-local 'org-drawer-regexp)
  2575. (defvar org-todo-regexp nil
  2576. "Matches any of the TODO state keywords.")
  2577. (make-variable-buffer-local 'org-todo-regexp)
  2578. (defvar org-not-done-regexp nil
  2579. "Matches any of the TODO state keywords except the last one.")
  2580. (make-variable-buffer-local 'org-not-done-regexp)
  2581. (defvar org-todo-line-regexp nil
  2582. "Matches a headline and puts TODO state into group 2 if present.")
  2583. (make-variable-buffer-local 'org-todo-line-regexp)
  2584. (defvar org-complex-heading-regexp nil
  2585. "Matches a headline and puts everything into groups:
  2586. group 1: the stars
  2587. group 2: The todo keyword, maybe
  2588. group 3: Priority cookie
  2589. group 4: True headline
  2590. group 5: Tags")
  2591. (make-variable-buffer-local 'org-complex-heading-regexp)
  2592. (defvar org-todo-line-tags-regexp nil
  2593. "Matches a headline and puts TODO state into group 2 if present.
  2594. Also put tags into group 4 if tags are present.")
  2595. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2596. (defvar org-nl-done-regexp nil
  2597. "Matches newline followed by a headline with the DONE keyword.")
  2598. (make-variable-buffer-local 'org-nl-done-regexp)
  2599. (defvar org-looking-at-done-regexp nil
  2600. "Matches the DONE keyword a point.")
  2601. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2602. (defvar org-ds-keyword-length 12
  2603. "Maximum length of the Deadline and SCHEDULED keywords.")
  2604. (make-variable-buffer-local 'org-ds-keyword-length)
  2605. (defvar org-deadline-regexp nil
  2606. "Matches the DEADLINE keyword.")
  2607. (make-variable-buffer-local 'org-deadline-regexp)
  2608. (defvar org-deadline-time-regexp nil
  2609. "Matches the DEADLINE keyword together with a time stamp.")
  2610. (make-variable-buffer-local 'org-deadline-time-regexp)
  2611. (defvar org-deadline-line-regexp nil
  2612. "Matches the DEADLINE keyword and the rest of the line.")
  2613. (make-variable-buffer-local 'org-deadline-line-regexp)
  2614. (defvar org-scheduled-regexp nil
  2615. "Matches the SCHEDULED keyword.")
  2616. (make-variable-buffer-local 'org-scheduled-regexp)
  2617. (defvar org-scheduled-time-regexp nil
  2618. "Matches the SCHEDULED keyword together with a time stamp.")
  2619. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2620. (defvar org-closed-time-regexp nil
  2621. "Matches the CLOSED keyword together with a time stamp.")
  2622. (make-variable-buffer-local 'org-closed-time-regexp)
  2623. (defvar org-keyword-time-regexp nil
  2624. "Matches any of the 4 keywords, together with the time stamp.")
  2625. (make-variable-buffer-local 'org-keyword-time-regexp)
  2626. (defvar org-keyword-time-not-clock-regexp nil
  2627. "Matches any of the 3 keywords, together with the time stamp.")
  2628. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2629. (defvar org-maybe-keyword-time-regexp nil
  2630. "Matches a timestamp, possibly preceeded by a keyword.")
  2631. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2632. (defvar org-planning-or-clock-line-re nil
  2633. "Matches a line with planning or clock info.")
  2634. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2635. (defconst org-plain-time-of-day-regexp
  2636. (concat
  2637. "\\(\\<[012]?[0-9]"
  2638. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2639. "\\(--?"
  2640. "\\(\\<[012]?[0-9]"
  2641. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2642. "\\)?")
  2643. "Regular expression to match a plain time or time range.
  2644. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2645. groups carry important information:
  2646. 0 the full match
  2647. 1 the first time, range or not
  2648. 8 the second time, if it is a range.")
  2649. (defconst org-plain-time-extension-regexp
  2650. (concat
  2651. "\\(\\<[012]?[0-9]"
  2652. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2653. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2654. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2655. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2656. groups carry important information:
  2657. 0 the full match
  2658. 7 hours of duration
  2659. 9 minutes of duration")
  2660. (defconst org-stamp-time-of-day-regexp
  2661. (concat
  2662. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2663. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2664. "\\(--?"
  2665. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2666. "Regular expression to match a timestamp time or time range.
  2667. After a match, the following groups carry important information:
  2668. 0 the full match
  2669. 1 date plus weekday, for backreferencing to make sure both times on same day
  2670. 2 the first time, range or not
  2671. 4 the second time, if it is a range.")
  2672. (defconst org-startup-options
  2673. '(("fold" org-startup-folded t)
  2674. ("overview" org-startup-folded t)
  2675. ("nofold" org-startup-folded nil)
  2676. ("showall" org-startup-folded nil)
  2677. ("content" org-startup-folded content)
  2678. ("hidestars" org-hide-leading-stars t)
  2679. ("showstars" org-hide-leading-stars nil)
  2680. ("odd" org-odd-levels-only t)
  2681. ("oddeven" org-odd-levels-only nil)
  2682. ("align" org-startup-align-all-tables t)
  2683. ("noalign" org-startup-align-all-tables nil)
  2684. ("customtime" org-display-custom-times t)
  2685. ("logdone" org-log-done time)
  2686. ("lognotedone" org-log-done note)
  2687. ("nologdone" org-log-done nil)
  2688. ("lognoteclock-out" org-log-note-clock-out t)
  2689. ("nolognoteclock-out" org-log-note-clock-out nil)
  2690. ("logrepeat" org-log-repeat state)
  2691. ("lognoterepeat" org-log-repeat note)
  2692. ("nologrepeat" org-log-repeat nil)
  2693. ("fninline" org-footnote-define-inline t)
  2694. ("nofninline" org-footnote-define-inline nil)
  2695. ("fnlocal" org-footnote-section nil)
  2696. ("fnauto" org-footnote-auto-label t)
  2697. ("fnprompt" org-footnote-auto-label nil)
  2698. ("fnconfirm" org-footnote-auto-label confirm)
  2699. ("fnplain" org-footnote-auto-label plain)
  2700. ("constcgs" constants-unit-system cgs)
  2701. ("constSI" constants-unit-system SI))
  2702. "Variable associated with STARTUP options for org-mode.
  2703. Each element is a list of three items: The startup options as written
  2704. in the #+STARTUP line, the corresponding variable, and the value to
  2705. set this variable to if the option is found. An optional forth element PUSH
  2706. means to push this value onto the list in the variable.")
  2707. (defun org-set-regexps-and-options ()
  2708. "Precompute regular expressions for current buffer."
  2709. (when (org-mode-p)
  2710. (org-set-local 'org-todo-kwd-alist nil)
  2711. (org-set-local 'org-todo-key-alist nil)
  2712. (org-set-local 'org-todo-key-trigger nil)
  2713. (org-set-local 'org-todo-keywords-1 nil)
  2714. (org-set-local 'org-done-keywords nil)
  2715. (org-set-local 'org-todo-heads nil)
  2716. (org-set-local 'org-todo-sets nil)
  2717. (org-set-local 'org-todo-log-states nil)
  2718. (org-set-local 'org-file-properties nil)
  2719. (org-set-local 'org-file-tags nil)
  2720. (let ((re (org-make-options-regexp
  2721. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2722. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2723. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2724. (splitre "[ \t]+")
  2725. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2726. tail sep kws1 prio props ftags drawers
  2727. ext-setup-or-nil setup-contents (start 0))
  2728. (save-excursion
  2729. (save-restriction
  2730. (widen)
  2731. (goto-char (point-min))
  2732. (while (or (and ext-setup-or-nil
  2733. (string-match re ext-setup-or-nil start)
  2734. (setq start (match-end 0)))
  2735. (and (setq ext-setup-or-nil nil start 0)
  2736. (re-search-forward re nil t)))
  2737. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2738. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2739. (cond
  2740. ((equal key "CATEGORY")
  2741. (if (string-match "[ \t]+$" value)
  2742. (setq value (replace-match "" t t value)))
  2743. (setq cat value))
  2744. ((member key '("SEQ_TODO" "TODO"))
  2745. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2746. ((equal key "TYP_TODO")
  2747. (push (cons 'type (org-split-string value splitre)) kwds))
  2748. ((equal key "TAGS")
  2749. (setq tags (append tags (org-split-string value splitre))))
  2750. ((equal key "COLUMNS")
  2751. (org-set-local 'org-columns-default-format value))
  2752. ((equal key "LINK")
  2753. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2754. (push (cons (match-string 1 value)
  2755. (org-trim (match-string 2 value)))
  2756. links)))
  2757. ((equal key "PRIORITIES")
  2758. (setq prio (org-split-string value " +")))
  2759. ((equal key "PROPERTY")
  2760. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2761. (push (cons (match-string 1 value) (match-string 2 value))
  2762. props)))
  2763. ((equal key "FILETAGS")
  2764. (when (string-match "\\S-" value)
  2765. (setq ftags
  2766. (append
  2767. ftags
  2768. (apply 'append
  2769. (mapcar (lambda (x) (org-split-string x ":"))
  2770. (org-split-string value)))))))
  2771. ((equal key "DRAWERS")
  2772. (setq drawers (org-split-string value splitre)))
  2773. ((equal key "CONSTANTS")
  2774. (setq const (append const (org-split-string value splitre))))
  2775. ((equal key "STARTUP")
  2776. (let ((opts (org-split-string value splitre))
  2777. l var val)
  2778. (while (setq l (pop opts))
  2779. (when (setq l (assoc l org-startup-options))
  2780. (setq var (nth 1 l) val (nth 2 l))
  2781. (if (not (nth 3 l))
  2782. (set (make-local-variable var) val)
  2783. (if (not (listp (symbol-value var)))
  2784. (set (make-local-variable var) nil))
  2785. (set (make-local-variable var) (symbol-value var))
  2786. (add-to-list var val))))))
  2787. ((equal key "ARCHIVE")
  2788. (string-match " *$" value)
  2789. (setq arch (replace-match "" t t value))
  2790. (remove-text-properties 0 (length arch)
  2791. '(face t fontified t) arch))
  2792. ((equal key "SETUPFILE")
  2793. (setq setup-contents (org-file-contents
  2794. (expand-file-name
  2795. (org-remove-double-quotes value))
  2796. 'noerror))
  2797. (if (not ext-setup-or-nil)
  2798. (setq ext-setup-or-nil setup-contents start 0)
  2799. (setq ext-setup-or-nil
  2800. (concat (substring ext-setup-or-nil 0 start)
  2801. "\n" setup-contents "\n"
  2802. (substring ext-setup-or-nil start)))))
  2803. ))))
  2804. (when cat
  2805. (org-set-local 'org-category (intern cat))
  2806. (push (cons "CATEGORY" cat) props))
  2807. (when prio
  2808. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2809. (setq prio (mapcar 'string-to-char prio))
  2810. (org-set-local 'org-highest-priority (nth 0 prio))
  2811. (org-set-local 'org-lowest-priority (nth 1 prio))
  2812. (org-set-local 'org-default-priority (nth 2 prio)))
  2813. (and props (org-set-local 'org-file-properties (nreverse props)))
  2814. (and ftags (org-set-local 'org-file-tags ftags))
  2815. (and drawers (org-set-local 'org-drawers drawers))
  2816. (and arch (org-set-local 'org-archive-location arch))
  2817. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2818. ;; Process the TODO keywords
  2819. (unless kwds
  2820. ;; Use the global values as if they had been given locally.
  2821. (setq kwds (default-value 'org-todo-keywords))
  2822. (if (stringp (car kwds))
  2823. (setq kwds (list (cons org-todo-interpretation
  2824. (default-value 'org-todo-keywords)))))
  2825. (setq kwds (reverse kwds)))
  2826. (setq kwds (nreverse kwds))
  2827. (let (inter kws kw)
  2828. (while (setq kws (pop kwds))
  2829. (setq inter (pop kws) sep (member "|" kws)
  2830. kws0 (delete "|" (copy-sequence kws))
  2831. kwsa nil
  2832. kws1 (mapcar
  2833. (lambda (x)
  2834. ;; 1 2
  2835. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2836. (progn
  2837. (setq kw (match-string 1 x)
  2838. key (and (match-end 2) (match-string 2 x))
  2839. log (org-extract-log-state-settings x))
  2840. (push (cons kw (and key (string-to-char key))) kwsa)
  2841. (and log (push log org-todo-log-states))
  2842. kw)
  2843. (error "Invalid TODO keyword %s" x)))
  2844. kws0)
  2845. kwsa (if kwsa (append '((:startgroup))
  2846. (nreverse kwsa)
  2847. '((:endgroup))))
  2848. hw (car kws1)
  2849. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2850. tail (list inter hw (car dws) (org-last dws)))
  2851. (add-to-list 'org-todo-heads hw 'append)
  2852. (push kws1 org-todo-sets)
  2853. (setq org-done-keywords (append org-done-keywords dws nil))
  2854. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2855. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2856. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2857. (setq org-todo-sets (nreverse org-todo-sets)
  2858. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2859. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2860. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2861. ;; Process the constants
  2862. (when const
  2863. (let (e cst)
  2864. (while (setq e (pop const))
  2865. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2866. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2867. (setq org-table-formula-constants-local cst)))
  2868. ;; Process the tags.
  2869. (when tags
  2870. (let (e tgs)
  2871. (while (setq e (pop tags))
  2872. (cond
  2873. ((equal e "{") (push '(:startgroup) tgs))
  2874. ((equal e "}") (push '(:endgroup) tgs))
  2875. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2876. (push (cons (match-string 1 e)
  2877. (string-to-char (match-string 2 e)))
  2878. tgs))
  2879. (t (push (list e) tgs))))
  2880. (org-set-local 'org-tag-alist nil)
  2881. (while (setq e (pop tgs))
  2882. (or (and (stringp (car e))
  2883. (assoc (car e) org-tag-alist))
  2884. (push e org-tag-alist)))))
  2885. ;; Compute the regular expressions and other local variables
  2886. (if (not org-done-keywords)
  2887. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2888. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2889. (length org-scheduled-string)
  2890. (length org-clock-string)
  2891. (length org-closed-string)))
  2892. org-drawer-regexp
  2893. (concat "^[ \t]*:\\("
  2894. (mapconcat 'regexp-quote org-drawers "\\|")
  2895. "\\):[ \t]*$")
  2896. org-not-done-keywords
  2897. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2898. org-todo-regexp
  2899. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2900. "\\|") "\\)\\>")
  2901. org-not-done-regexp
  2902. (concat "\\<\\("
  2903. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2904. "\\)\\>")
  2905. org-todo-line-regexp
  2906. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2907. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2908. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2909. org-complex-heading-regexp
  2910. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2911. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2912. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2913. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2914. org-nl-done-regexp
  2915. (concat "\n\\*+[ \t]+"
  2916. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2917. "\\)" "\\>")
  2918. org-todo-line-tags-regexp
  2919. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2920. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2921. (org-re
  2922. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2923. org-looking-at-done-regexp
  2924. (concat "^" "\\(?:"
  2925. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2926. "\\>")
  2927. org-deadline-regexp (concat "\\<" org-deadline-string)
  2928. org-deadline-time-regexp
  2929. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2930. org-deadline-line-regexp
  2931. (concat "\\<\\(" org-deadline-string "\\).*")
  2932. org-scheduled-regexp
  2933. (concat "\\<" org-scheduled-string)
  2934. org-scheduled-time-regexp
  2935. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2936. org-closed-time-regexp
  2937. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2938. org-keyword-time-regexp
  2939. (concat "\\<\\(" org-scheduled-string
  2940. "\\|" org-deadline-string
  2941. "\\|" org-closed-string
  2942. "\\|" org-clock-string "\\)"
  2943. " *[[<]\\([^]>]+\\)[]>]")
  2944. org-keyword-time-not-clock-regexp
  2945. (concat "\\<\\(" org-scheduled-string
  2946. "\\|" org-deadline-string
  2947. "\\|" org-closed-string
  2948. "\\)"
  2949. " *[[<]\\([^]>]+\\)[]>]")
  2950. org-maybe-keyword-time-regexp
  2951. (concat "\\(\\<\\(" org-scheduled-string
  2952. "\\|" org-deadline-string
  2953. "\\|" org-closed-string
  2954. "\\|" org-clock-string "\\)\\)?"
  2955. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2956. org-planning-or-clock-line-re
  2957. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2958. "\\|" org-deadline-string
  2959. "\\|" org-closed-string "\\|" org-clock-string
  2960. "\\)\\>\\)")
  2961. )
  2962. (org-compute-latex-and-specials-regexp)
  2963. (org-set-font-lock-defaults))))
  2964. (defun org-file-contents (file &optional noerror)
  2965. "Return the contents of FILE, as a string."
  2966. (if (or (not file)
  2967. (not (file-readable-p file)))
  2968. (if noerror
  2969. (progn
  2970. (message "Cannot read file %s" file)
  2971. (ding) (sit-for 2)
  2972. "")
  2973. (error "Cannot read file %s" file))
  2974. (with-temp-buffer
  2975. (insert-file-contents file)
  2976. (buffer-string))))
  2977. (defun org-extract-log-state-settings (x)
  2978. "Extract the log state setting from a TODO keyword string.
  2979. This will extract info from a string like \"WAIT(w@/!)\"."
  2980. (let (kw key log1 log2)
  2981. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2982. (setq kw (match-string 1 x)
  2983. key (and (match-end 2) (match-string 2 x))
  2984. log1 (and (match-end 3) (match-string 3 x))
  2985. log2 (and (match-end 4) (match-string 4 x)))
  2986. (and (or log1 log2)
  2987. (list kw
  2988. (and log1 (if (equal log1 "!") 'time 'note))
  2989. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2990. (defun org-remove-keyword-keys (list)
  2991. "Remove a pair of parenthesis at the end of each string in LIST."
  2992. (mapcar (lambda (x)
  2993. (if (string-match "(.*)$" x)
  2994. (substring x 0 (match-beginning 0))
  2995. x))
  2996. list))
  2997. ;; FIXME: this could be done much better, using second characters etc.
  2998. (defun org-assign-fast-keys (alist)
  2999. "Assign fast keys to a keyword-key alist.
  3000. Respect keys that are already there."
  3001. (let (new e k c c1 c2 (char ?a))
  3002. (while (setq e (pop alist))
  3003. (cond
  3004. ((equal e '(:startgroup)) (push e new))
  3005. ((equal e '(:endgroup)) (push e new))
  3006. (t
  3007. (setq k (car e) c2 nil)
  3008. (if (cdr e)
  3009. (setq c (cdr e))
  3010. ;; automatically assign a character.
  3011. (setq c1 (string-to-char
  3012. (downcase (substring
  3013. k (if (= (string-to-char k) ?@) 1 0)))))
  3014. (if (or (rassoc c1 new) (rassoc c1 alist))
  3015. (while (or (rassoc char new) (rassoc char alist))
  3016. (setq char (1+ char)))
  3017. (setq c2 c1))
  3018. (setq c (or c2 char)))
  3019. (push (cons k c) new))))
  3020. (nreverse new)))
  3021. ;;; Some variables used in various places
  3022. (defvar org-window-configuration nil
  3023. "Used in various places to store a window configuration.")
  3024. (defvar org-finish-function nil
  3025. "Function to be called when `C-c C-c' is used.
  3026. This is for getting out of special buffers like remember.")
  3027. ;; FIXME: Occasionally check by commenting these, to make sure
  3028. ;; no other functions uses these, forgetting to let-bind them.
  3029. (defvar entry)
  3030. (defvar state)
  3031. (defvar last-state)
  3032. (defvar date)
  3033. (defvar description)
  3034. ;; Defined somewhere in this file, but used before definition.
  3035. (defvar org-html-entities)
  3036. (defvar org-struct-menu)
  3037. (defvar org-org-menu)
  3038. (defvar org-tbl-menu)
  3039. (defvar org-agenda-keymap)
  3040. ;;;; Define the Org-mode
  3041. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3042. (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."))
  3043. ;; We use a before-change function to check if a table might need
  3044. ;; an update.
  3045. (defvar org-table-may-need-update t
  3046. "Indicates that a table might need an update.
  3047. This variable is set by `org-before-change-function'.
  3048. `org-table-align' sets it back to nil.")
  3049. (defun org-before-change-function (beg end)
  3050. "Every change indicates that a table might need an update."
  3051. (setq org-table-may-need-update t))
  3052. (defvar org-mode-map)
  3053. (defvar org-mode-hook nil
  3054. "Mode hook for Org-mode, run after the mode was turned on.")
  3055. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3056. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3057. (defvar org-table-buffer-is-an nil)
  3058. (defconst org-outline-regexp "\\*+ ")
  3059. ;;;###autoload
  3060. (define-derived-mode org-mode outline-mode "Org"
  3061. "Outline-based notes management and organizer, alias
  3062. \"Carsten's outline-mode for keeping track of everything.\"
  3063. Org-mode develops organizational tasks around a NOTES file which
  3064. contains information about projects as plain text. Org-mode is
  3065. implemented on top of outline-mode, which is ideal to keep the content
  3066. of large files well structured. It supports ToDo items, deadlines and
  3067. time stamps, which magically appear in the diary listing of the Emacs
  3068. calendar. Tables are easily created with a built-in table editor.
  3069. Plain text URL-like links connect to websites, emails (VM), Usenet
  3070. messages (Gnus), BBDB entries, and any files related to the project.
  3071. For printing and sharing of notes, an Org-mode file (or a part of it)
  3072. can be exported as a structured ASCII or HTML file.
  3073. The following commands are available:
  3074. \\{org-mode-map}"
  3075. ;; Get rid of Outline menus, they are not needed
  3076. ;; Need to do this here because define-derived-mode sets up
  3077. ;; the keymap so late. Still, it is a waste to call this each time
  3078. ;; we switch another buffer into org-mode.
  3079. (if (featurep 'xemacs)
  3080. (when (boundp 'outline-mode-menu-heading)
  3081. ;; Assume this is Greg's port, it used easymenu
  3082. (easy-menu-remove outline-mode-menu-heading)
  3083. (easy-menu-remove outline-mode-menu-show)
  3084. (easy-menu-remove outline-mode-menu-hide))
  3085. (define-key org-mode-map [menu-bar headings] 'undefined)
  3086. (define-key org-mode-map [menu-bar hide] 'undefined)
  3087. (define-key org-mode-map [menu-bar show] 'undefined))
  3088. (org-load-modules-maybe)
  3089. (easy-menu-add org-org-menu)
  3090. (easy-menu-add org-tbl-menu)
  3091. (org-install-agenda-files-menu)
  3092. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3093. (org-add-to-invisibility-spec '(org-cwidth))
  3094. (when (featurep 'xemacs)
  3095. (org-set-local 'line-move-ignore-invisible t))
  3096. (org-set-local 'outline-regexp org-outline-regexp)
  3097. (org-set-local 'outline-level 'org-outline-level)
  3098. (when (and org-ellipsis
  3099. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3100. (fboundp 'make-glyph-code))
  3101. (unless org-display-table
  3102. (setq org-display-table (make-display-table)))
  3103. (set-display-table-slot
  3104. org-display-table 4
  3105. (vconcat (mapcar
  3106. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3107. org-ellipsis)))
  3108. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3109. (setq buffer-display-table org-display-table))
  3110. (org-set-regexps-and-options)
  3111. (when (and org-tag-faces (not org-tags-special-faces-re))
  3112. ;; tag faces set outside customize.... force initialization.
  3113. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3114. ;; Calc embedded
  3115. (org-set-local 'calc-embedded-open-mode "# ")
  3116. (modify-syntax-entry ?# "<")
  3117. (modify-syntax-entry ?@ "w")
  3118. (if org-startup-truncated (setq truncate-lines t))
  3119. (org-set-local 'font-lock-unfontify-region-function
  3120. 'org-unfontify-region)
  3121. ;; Activate before-change-function
  3122. (org-set-local 'org-table-may-need-update t)
  3123. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3124. 'local)
  3125. ;; Check for running clock before killing a buffer
  3126. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3127. ;; Paragraphs and auto-filling
  3128. (org-set-autofill-regexps)
  3129. (setq indent-line-function 'org-indent-line-function)
  3130. (org-update-radio-target-regexp)
  3131. ;; Comment characters
  3132. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3133. (org-set-local 'comment-padding " ")
  3134. ;; Align options lines
  3135. (org-set-local
  3136. 'align-mode-rules-list
  3137. '((org-in-buffer-settings
  3138. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3139. (modes . '(org-mode)))))
  3140. ;; Imenu
  3141. (org-set-local 'imenu-create-index-function
  3142. 'org-imenu-get-tree)
  3143. ;; Make isearch reveal context
  3144. (if (or (featurep 'xemacs)
  3145. (not (boundp 'outline-isearch-open-invisible-function)))
  3146. ;; Emacs 21 and XEmacs make use of the hook
  3147. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3148. ;; Emacs 22 deals with this through a special variable
  3149. (org-set-local 'outline-isearch-open-invisible-function
  3150. (lambda (&rest ignore) (org-show-context 'isearch))))
  3151. ;; If empty file that did not turn on org-mode automatically, make it to.
  3152. (if (and org-insert-mode-line-in-empty-file
  3153. (interactive-p)
  3154. (= (point-min) (point-max)))
  3155. (insert "# -*- mode: org -*-\n\n"))
  3156. (unless org-inhibit-startup
  3157. (when org-startup-align-all-tables
  3158. (let ((bmp (buffer-modified-p)))
  3159. (org-table-map-tables 'org-table-align)
  3160. (set-buffer-modified-p bmp)))
  3161. (org-set-startup-visibility)))
  3162. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3163. (defun org-current-time ()
  3164. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3165. (if (> (car org-time-stamp-rounding-minutes) 1)
  3166. (let ((r (car org-time-stamp-rounding-minutes))
  3167. (time (decode-time)))
  3168. (apply 'encode-time
  3169. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3170. (nthcdr 2 time))))
  3171. (current-time)))
  3172. ;;;; Font-Lock stuff, including the activators
  3173. (defvar org-mouse-map (make-sparse-keymap))
  3174. (org-defkey org-mouse-map
  3175. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3176. (org-defkey org-mouse-map
  3177. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3178. (when org-mouse-1-follows-link
  3179. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3180. (when org-tab-follows-link
  3181. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3182. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3183. (when org-return-follows-link
  3184. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3185. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3186. (require 'font-lock)
  3187. (defconst org-non-link-chars "]\t\n\r<>")
  3188. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3189. "shell" "elisp"))
  3190. (defvar org-link-types-re nil
  3191. "Matches a link that has a url-like prefix like \"http:\"")
  3192. (defvar org-link-re-with-space nil
  3193. "Matches a link with spaces, optional angular brackets around it.")
  3194. (defvar org-link-re-with-space2 nil
  3195. "Matches a link with spaces, optional angular brackets around it.")
  3196. (defvar org-link-re-with-space3 nil
  3197. "Matches a link with spaces, only for internal part in bracket links.")
  3198. (defvar org-angle-link-re nil
  3199. "Matches link with angular brackets, spaces are allowed.")
  3200. (defvar org-plain-link-re nil
  3201. "Matches plain link, without spaces.")
  3202. (defvar org-bracket-link-regexp nil
  3203. "Matches a link in double brackets.")
  3204. (defvar org-bracket-link-analytic-regexp nil
  3205. "Regular expression used to analyze links.
  3206. Here is what the match groups contain after a match:
  3207. 1: http:
  3208. 2: http
  3209. 3: path
  3210. 4: [desc]
  3211. 5: desc")
  3212. (defvar org-bracket-link-analytic-regexp++ nil
  3213. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3214. (defvar org-any-link-re nil
  3215. "Regular expression matching any link.")
  3216. (defun org-make-link-regexps ()
  3217. "Update the link regular expressions.
  3218. This should be called after the variable `org-link-types' has changed."
  3219. (setq org-link-types-re
  3220. (concat
  3221. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3222. org-link-re-with-space
  3223. (concat
  3224. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3225. "\\([^" org-non-link-chars " ]"
  3226. "[^" org-non-link-chars "]*"
  3227. "[^" org-non-link-chars " ]\\)>?")
  3228. org-link-re-with-space2
  3229. (concat
  3230. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3231. "\\([^" org-non-link-chars " ]"
  3232. "[^\t\n\r]*"
  3233. "[^" org-non-link-chars " ]\\)>?")
  3234. org-link-re-with-space3
  3235. (concat
  3236. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3237. "\\([^" org-non-link-chars " ]"
  3238. "[^\t\n\r]*\\)")
  3239. org-angle-link-re
  3240. (concat
  3241. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3242. "\\([^" org-non-link-chars " ]"
  3243. "[^" org-non-link-chars "]*"
  3244. "\\)>")
  3245. org-plain-link-re
  3246. (concat
  3247. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3248. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3249. org-bracket-link-regexp
  3250. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3251. org-bracket-link-analytic-regexp
  3252. (concat
  3253. "\\[\\["
  3254. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3255. "\\([^]]+\\)"
  3256. "\\]"
  3257. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3258. "\\]")
  3259. org-bracket-link-analytic-regexp++
  3260. (concat
  3261. "\\[\\["
  3262. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3263. "\\([^]]+\\)"
  3264. "\\]"
  3265. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3266. "\\]")
  3267. org-any-link-re
  3268. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3269. org-angle-link-re "\\)\\|\\("
  3270. org-plain-link-re "\\)")))
  3271. (org-make-link-regexps)
  3272. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3273. "Regular expression for fast time stamp matching.")
  3274. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3275. "Regular expression for fast time stamp matching.")
  3276. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3277. "Regular expression matching time strings for analysis.
  3278. This one does not require the space after the date, so it can be used
  3279. on a string that terminates immediately after the date.")
  3280. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3281. "Regular expression matching time strings for analysis.")
  3282. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3283. "Regular expression matching time stamps, with groups.")
  3284. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3285. "Regular expression matching time stamps (also [..]), with groups.")
  3286. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3287. "Regular expression matching a time stamp range.")
  3288. (defconst org-tr-regexp-both
  3289. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3290. "Regular expression matching a time stamp range.")
  3291. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3292. org-ts-regexp "\\)?")
  3293. "Regular expression matching a time stamp or time stamp range.")
  3294. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3295. org-ts-regexp-both "\\)?")
  3296. "Regular expression matching a time stamp or time stamp range.
  3297. The time stamps may be either active or inactive.")
  3298. (defvar org-emph-face nil)
  3299. (defun org-do-emphasis-faces (limit)
  3300. "Run through the buffer and add overlays to links."
  3301. (let (rtn)
  3302. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3303. (if (not (= (char-after (match-beginning 3))
  3304. (char-after (match-beginning 4))))
  3305. (progn
  3306. (setq rtn t)
  3307. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3308. 'face
  3309. (nth 1 (assoc (match-string 3)
  3310. org-emphasis-alist)))
  3311. (add-text-properties (match-beginning 2) (match-end 2)
  3312. '(font-lock-multiline t))
  3313. (when org-hide-emphasis-markers
  3314. (add-text-properties (match-end 4) (match-beginning 5)
  3315. '(invisible org-link))
  3316. (add-text-properties (match-beginning 3) (match-end 3)
  3317. '(invisible org-link)))))
  3318. (backward-char 1))
  3319. rtn))
  3320. (defun org-emphasize (&optional char)
  3321. "Insert or change an emphasis, i.e. a font like bold or italic.
  3322. If there is an active region, change that region to a new emphasis.
  3323. If there is no region, just insert the marker characters and position
  3324. the cursor between them.
  3325. CHAR should be either the marker character, or the first character of the
  3326. HTML tag associated with that emphasis. If CHAR is a space, the means
  3327. to remove the emphasis of the selected region.
  3328. If char is not given (for example in an interactive call) it
  3329. will be prompted for."
  3330. (interactive)
  3331. (let ((eal org-emphasis-alist) e det
  3332. (erc org-emphasis-regexp-components)
  3333. (prompt "")
  3334. (string "") beg end move tag c s)
  3335. (if (org-region-active-p)
  3336. (setq beg (region-beginning) end (region-end)
  3337. string (buffer-substring beg end))
  3338. (setq move t))
  3339. (while (setq e (pop eal))
  3340. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3341. c (aref tag 0))
  3342. (push (cons c (string-to-char (car e))) det)
  3343. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3344. (substring tag 1)))))
  3345. (setq det (nreverse det))
  3346. (unless char
  3347. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3348. (setq char (read-char-exclusive)))
  3349. (setq char (or (cdr (assoc char det)) char))
  3350. (if (equal char ?\ )
  3351. (setq s "" move nil)
  3352. (unless (assoc (char-to-string char) org-emphasis-alist)
  3353. (error "No such emphasis marker: \"%c\"" char))
  3354. (setq s (char-to-string char)))
  3355. (while (and (> (length string) 1)
  3356. (equal (substring string 0 1) (substring string -1))
  3357. (assoc (substring string 0 1) org-emphasis-alist))
  3358. (setq string (substring string 1 -1)))
  3359. (setq string (concat s string s))
  3360. (if beg (delete-region beg end))
  3361. (unless (or (bolp)
  3362. (string-match (concat "[" (nth 0 erc) "\n]")
  3363. (char-to-string (char-before (point)))))
  3364. (insert " "))
  3365. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3366. (char-to-string (char-after (point))))
  3367. (insert " ") (backward-char 1))
  3368. (insert string)
  3369. (and move (backward-char 1))))
  3370. (defconst org-nonsticky-props
  3371. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3372. (defun org-activate-plain-links (limit)
  3373. "Run through the buffer and add overlays to links."
  3374. (catch 'exit
  3375. (let (f)
  3376. (while (re-search-forward org-plain-link-re limit t)
  3377. (setq f (get-text-property (match-beginning 0) 'face))
  3378. (if (or (eq f 'org-tag)
  3379. (and (listp f) (memq 'org-tag f)))
  3380. nil
  3381. (add-text-properties (match-beginning 0) (match-end 0)
  3382. (list 'mouse-face 'highlight
  3383. 'rear-nonsticky org-nonsticky-props
  3384. 'keymap org-mouse-map
  3385. ))
  3386. (throw 'exit t))))))
  3387. (defun org-activate-code (limit)
  3388. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3389. (progn
  3390. (remove-text-properties (match-beginning 0) (match-end 0)
  3391. '(display t invisible t intangible t))
  3392. t)))
  3393. (defun org-activate-angle-links (limit)
  3394. "Run through the buffer and add overlays to links."
  3395. (if (re-search-forward org-angle-link-re limit t)
  3396. (progn
  3397. (add-text-properties (match-beginning 0) (match-end 0)
  3398. (list 'mouse-face 'highlight
  3399. 'rear-nonsticky org-nonsticky-props
  3400. 'keymap org-mouse-map
  3401. ))
  3402. t)))
  3403. (defun org-activate-footnote-links (limit)
  3404. "Run through the buffer and add overlays to links."
  3405. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3406. limit t)
  3407. (progn
  3408. (add-text-properties (match-beginning 2) (match-end 2)
  3409. (list 'mouse-face 'highlight
  3410. 'rear-nonsticky org-nonsticky-props
  3411. 'keymap org-mouse-map
  3412. 'help-echo
  3413. (if (= (point-at-bol) (match-beginning 2))
  3414. "Footnote definition"
  3415. "Footnote reference")
  3416. ))
  3417. t)))
  3418. (defun org-activate-bracket-links (limit)
  3419. "Run through the buffer and add overlays to bracketed links."
  3420. (if (re-search-forward org-bracket-link-regexp limit t)
  3421. (let* ((help (concat "LINK: "
  3422. (org-match-string-no-properties 1)))
  3423. ;; FIXME: above we should remove the escapes.
  3424. ;; but that requires another match, protecting match data,
  3425. ;; a lot of overhead for font-lock.
  3426. (ip (org-maybe-intangible
  3427. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3428. 'keymap org-mouse-map 'mouse-face 'highlight
  3429. 'font-lock-multiline t 'help-echo help)))
  3430. (vp (list 'rear-nonsticky org-nonsticky-props
  3431. 'keymap org-mouse-map 'mouse-face 'highlight
  3432. ' font-lock-multiline t 'help-echo help)))
  3433. ;; We need to remove the invisible property here. Table narrowing
  3434. ;; may have made some of this invisible.
  3435. (remove-text-properties (match-beginning 0) (match-end 0)
  3436. '(invisible nil))
  3437. (if (match-end 3)
  3438. (progn
  3439. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3440. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3441. (add-text-properties (match-end 3) (match-end 0) ip))
  3442. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3443. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3444. (add-text-properties (match-end 1) (match-end 0) ip))
  3445. t)))
  3446. (defun org-activate-dates (limit)
  3447. "Run through the buffer and add overlays to dates."
  3448. (if (re-search-forward org-tsr-regexp-both limit t)
  3449. (progn
  3450. (add-text-properties (match-beginning 0) (match-end 0)
  3451. (list 'mouse-face 'highlight
  3452. 'rear-nonsticky org-nonsticky-props
  3453. 'keymap org-mouse-map))
  3454. (when org-display-custom-times
  3455. (if (match-end 3)
  3456. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3457. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3458. t)))
  3459. (defvar org-target-link-regexp nil
  3460. "Regular expression matching radio targets in plain text.")
  3461. (make-variable-buffer-local 'org-target-link-regexp)
  3462. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3463. "Regular expression matching a link target.")
  3464. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3465. "Regular expression matching a radio target.")
  3466. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3467. "Regular expression matching any target.")
  3468. (defun org-activate-target-links (limit)
  3469. "Run through the buffer and add overlays to target matches."
  3470. (when org-target-link-regexp
  3471. (let ((case-fold-search t))
  3472. (if (re-search-forward org-target-link-regexp limit t)
  3473. (progn
  3474. (add-text-properties (match-beginning 0) (match-end 0)
  3475. (list 'mouse-face 'highlight
  3476. 'rear-nonsticky org-nonsticky-props
  3477. 'keymap org-mouse-map
  3478. 'help-echo "Radio target link"
  3479. 'org-linked-text t))
  3480. t)))))
  3481. (defun org-update-radio-target-regexp ()
  3482. "Find all radio targets in this file and update the regular expression."
  3483. (interactive)
  3484. (when (memq 'radio org-activate-links)
  3485. (setq org-target-link-regexp
  3486. (org-make-target-link-regexp (org-all-targets 'radio)))
  3487. (org-restart-font-lock)))
  3488. (defun org-hide-wide-columns (limit)
  3489. (let (s e)
  3490. (setq s (text-property-any (point) (or limit (point-max))
  3491. 'org-cwidth t))
  3492. (when s
  3493. (setq e (next-single-property-change s 'org-cwidth))
  3494. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3495. (goto-char e)
  3496. t)))
  3497. (defvar org-latex-and-specials-regexp nil
  3498. "Regular expression for highlighting export special stuff.")
  3499. (defvar org-match-substring-regexp)
  3500. (defvar org-match-substring-with-braces-regexp)
  3501. (defvar org-export-html-special-string-regexps)
  3502. (defun org-compute-latex-and-specials-regexp ()
  3503. "Compute regular expression for stuff treated specially by exporters."
  3504. (if (not org-highlight-latex-fragments-and-specials)
  3505. (org-set-local 'org-latex-and-specials-regexp nil)
  3506. (require 'org-exp)
  3507. (let*
  3508. ((matchers (plist-get org-format-latex-options :matchers))
  3509. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3510. org-latex-regexps)))
  3511. (options (org-combine-plists (org-default-export-plist)
  3512. (org-infile-export-plist)))
  3513. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3514. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3515. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3516. (org-export-html-expand (plist-get options :expand-quoted-html))
  3517. (org-export-with-special-strings (plist-get options :special-strings))
  3518. (re-sub
  3519. (cond
  3520. ((equal org-export-with-sub-superscripts '{})
  3521. (list org-match-substring-with-braces-regexp))
  3522. (org-export-with-sub-superscripts
  3523. (list org-match-substring-regexp))
  3524. (t nil)))
  3525. (re-latex
  3526. (if org-export-with-LaTeX-fragments
  3527. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3528. (re-macros
  3529. (if org-export-with-TeX-macros
  3530. (list (concat "\\\\"
  3531. (regexp-opt
  3532. (append (mapcar 'car org-html-entities)
  3533. (if (boundp 'org-latex-entities)
  3534. org-latex-entities nil))
  3535. 'words))) ; FIXME
  3536. ))
  3537. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3538. (re-special (if org-export-with-special-strings
  3539. (mapcar (lambda (x) (car x))
  3540. org-export-html-special-string-regexps)))
  3541. (re-rest
  3542. (delq nil
  3543. (list
  3544. (if org-export-html-expand "@<[^>\n]+>")
  3545. ))))
  3546. (org-set-local
  3547. 'org-latex-and-specials-regexp
  3548. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3549. re-rest) "\\|")))))
  3550. (defun org-do-latex-and-special-faces (limit)
  3551. "Run through the buffer and add overlays to links."
  3552. (when org-latex-and-specials-regexp
  3553. (let (rtn d)
  3554. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3555. limit t))
  3556. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3557. 'face))
  3558. '(org-code org-verbatim underline)))
  3559. (progn
  3560. (setq rtn t
  3561. d (cond ((member (char-after (1+ (match-beginning 0)))
  3562. '(?_ ?^)) 1)
  3563. (t 0)))
  3564. (font-lock-prepend-text-property
  3565. (+ d (match-beginning 0)) (match-end 0)
  3566. 'face 'org-latex-and-export-specials)
  3567. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3568. '(font-lock-multiline t)))))
  3569. rtn)))
  3570. (defun org-restart-font-lock ()
  3571. "Restart font-lock-mode, to force refontification."
  3572. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3573. (font-lock-mode -1)
  3574. (font-lock-mode 1)))
  3575. (defun org-all-targets (&optional radio)
  3576. "Return a list of all targets in this file.
  3577. With optional argument RADIO, only find radio targets."
  3578. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3579. rtn)
  3580. (save-excursion
  3581. (goto-char (point-min))
  3582. (while (re-search-forward re nil t)
  3583. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3584. rtn)))
  3585. (defun org-make-target-link-regexp (targets)
  3586. "Make regular expression matching all strings in TARGETS.
  3587. The regular expression finds the targets also if there is a line break
  3588. between words."
  3589. (and targets
  3590. (concat
  3591. "\\<\\("
  3592. (mapconcat
  3593. (lambda (x)
  3594. (while (string-match " +" x)
  3595. (setq x (replace-match "\\s-+" t t x)))
  3596. x)
  3597. targets
  3598. "\\|")
  3599. "\\)\\>")))
  3600. (defun org-activate-tags (limit)
  3601. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3602. (progn
  3603. (add-text-properties (match-beginning 1) (match-end 1)
  3604. (list 'mouse-face 'highlight
  3605. 'rear-nonsticky org-nonsticky-props
  3606. 'keymap org-mouse-map))
  3607. t)))
  3608. (defun org-outline-level ()
  3609. (save-excursion
  3610. (looking-at outline-regexp)
  3611. (if (match-beginning 1)
  3612. (+ (org-get-string-indentation (match-string 1)) 1000)
  3613. (1- (- (match-end 0) (match-beginning 0))))))
  3614. (defvar org-font-lock-keywords nil)
  3615. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3616. "Regular expression matching a property line.")
  3617. (defvar org-font-lock-hook nil
  3618. "Functions to be called for special font lock stuff.")
  3619. (defun org-font-lock-hook (limit)
  3620. (run-hook-with-args 'org-font-lock-hook limit))
  3621. (defun org-set-font-lock-defaults ()
  3622. (let* ((em org-fontify-emphasized-text)
  3623. (lk org-activate-links)
  3624. (org-font-lock-extra-keywords
  3625. (list
  3626. ;; Call the hook
  3627. '(org-font-lock-hook)
  3628. ;; Headlines
  3629. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3630. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3631. ;; Table lines
  3632. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3633. (1 'org-table t))
  3634. ;; Table internals
  3635. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3636. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3637. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3638. ;; Drawers
  3639. (list org-drawer-regexp '(0 'org-special-keyword t))
  3640. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3641. ;; Properties
  3642. (list org-property-re
  3643. '(1 'org-special-keyword t)
  3644. '(3 'org-property-value t))
  3645. (if org-format-transports-properties-p
  3646. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3647. ;; Links
  3648. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3649. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3650. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3651. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3652. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3653. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3654. (if (memq 'footnote lk) '(org-activate-footnote-links
  3655. (2 'org-footnote t)))
  3656. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3657. '(org-hide-wide-columns (0 nil append))
  3658. ;; TODO lines
  3659. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3660. '(1 (org-get-todo-face 1) t))
  3661. ;; DONE
  3662. (if org-fontify-done-headline
  3663. (list (concat "^[*]+ +\\<\\("
  3664. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3665. "\\)\\(.*\\)")
  3666. '(2 'org-headline-done t))
  3667. nil)
  3668. ;; Priorities
  3669. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3670. ;; Tags
  3671. '(org-font-lock-add-tag-faces)
  3672. ;; Special keywords
  3673. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3674. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3675. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3676. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3677. ;; Emphasis
  3678. (if em
  3679. (if (featurep 'xemacs)
  3680. '(org-do-emphasis-faces (0 nil append))
  3681. '(org-do-emphasis-faces)))
  3682. ;; Checkboxes
  3683. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3684. 2 'bold prepend)
  3685. (if org-provide-checkbox-statistics
  3686. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3687. (0 (org-get-checkbox-statistics-face) t)))
  3688. ;; Description list items
  3689. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3690. 2 'bold prepend)
  3691. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3692. '(1 'org-archived prepend))
  3693. ;; Specials
  3694. '(org-do-latex-and-special-faces)
  3695. ;; Code
  3696. '(org-activate-code (1 'org-code t))
  3697. ;; COMMENT
  3698. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3699. "\\|" org-quote-string "\\)\\>")
  3700. '(1 'org-special-keyword t))
  3701. '("^#.*" (0 'font-lock-comment-face t))
  3702. )))
  3703. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3704. ;; Now set the full font-lock-keywords
  3705. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3706. (org-set-local 'font-lock-defaults
  3707. '(org-font-lock-keywords t nil nil backward-paragraph))
  3708. (kill-local-variable 'font-lock-keywords) nil))
  3709. (defvar org-m nil)
  3710. (defvar org-l nil)
  3711. (defvar org-f nil)
  3712. (defun org-get-level-face (n)
  3713. "Get the right face for match N in font-lock matching of headlines."
  3714. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3715. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3716. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3717. (cond
  3718. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3719. ((eq n 2) org-f)
  3720. (t (if org-level-color-stars-only nil org-f))))
  3721. (defun org-get-todo-face (kwd)
  3722. "Get the right face for a TODO keyword KWD.
  3723. If KWD is a number, get the corresponding match group."
  3724. (if (numberp kwd) (setq kwd (match-string kwd)))
  3725. (or (cdr (assoc kwd org-todo-keyword-faces))
  3726. (and (member kwd org-done-keywords) 'org-done)
  3727. 'org-todo))
  3728. (defun org-font-lock-add-tag-faces (limit)
  3729. "Add the special tag faces."
  3730. (when (and org-tag-faces org-tags-special-faces-re)
  3731. (while (re-search-forward org-tags-special-faces-re limit t)
  3732. (add-text-properties (match-beginning 1) (match-end 1)
  3733. (list 'face (org-get-tag-face 1)
  3734. 'font-lock-fontified t))
  3735. (backward-char 1))))
  3736. (defun org-get-tag-face (kwd)
  3737. "Get the right face for a TODO keyword KWD.
  3738. If KWD is a number, get the corresponding match group."
  3739. (if (numberp kwd) (setq kwd (match-string kwd)))
  3740. (or (cdr (assoc kwd org-tag-faces))
  3741. 'org-tag))
  3742. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3743. "Remove fontification and activation overlays from links."
  3744. (font-lock-default-unfontify-region beg end)
  3745. (let* ((buffer-undo-list t)
  3746. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3747. (inhibit-modification-hooks t)
  3748. deactivate-mark buffer-file-name buffer-file-truename)
  3749. (remove-text-properties beg end
  3750. '(mouse-face t keymap t org-linked-text t
  3751. invisible t intangible t))))
  3752. ;;;; Visibility cycling, including org-goto and indirect buffer
  3753. ;;; Cycling
  3754. (defvar org-cycle-global-status nil)
  3755. (make-variable-buffer-local 'org-cycle-global-status)
  3756. (defvar org-cycle-subtree-status nil)
  3757. (make-variable-buffer-local 'org-cycle-subtree-status)
  3758. ;;;###autoload
  3759. (defun org-cycle (&optional arg)
  3760. "Visibility cycling for Org-mode.
  3761. - When this function is called with a prefix argument, rotate the entire
  3762. buffer through 3 states (global cycling)
  3763. 1. OVERVIEW: Show only top-level headlines.
  3764. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3765. 3. SHOW ALL: Show everything.
  3766. When called with two C-u C-u prefixes, switch to the startup visibility,
  3767. determined by the variable `org-startup-folded', and by any VISIBILITY
  3768. properties in the buffer.
  3769. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3770. including drawers.
  3771. - When point is at the beginning of a headline, rotate the subtree started
  3772. by this line through 3 different states (local cycling)
  3773. 1. FOLDED: Only the main headline is shown.
  3774. 2. CHILDREN: The main headline and the direct children are shown.
  3775. From this state, you can move to one of the children
  3776. and zoom in further.
  3777. 3. SUBTREE: Show the entire subtree, including body text.
  3778. - When there is a numeric prefix, go up to a heading with level ARG, do
  3779. a `show-subtree' and return to the previous cursor position. If ARG
  3780. is negative, go up that many levels.
  3781. - When point is not at the beginning of a headline, execute the global
  3782. binding for TAB, which is re-indenting the line. See the option
  3783. `org-cycle-emulate-tab' for details.
  3784. - Special case: if point is at the beginning of the buffer and there is
  3785. no headline in line 1, this function will act as if called with prefix arg.
  3786. But only if also the variable `org-cycle-global-at-bob' is t."
  3787. (interactive "P")
  3788. (org-load-modules-maybe)
  3789. (let* ((outline-regexp
  3790. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3791. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3792. outline-regexp))
  3793. (bob-special (and org-cycle-global-at-bob (bobp)
  3794. (not (looking-at outline-regexp))))
  3795. (org-cycle-hook
  3796. (if bob-special
  3797. (delq 'org-optimize-window-after-visibility-change
  3798. (copy-sequence org-cycle-hook))
  3799. org-cycle-hook))
  3800. (pos (point)))
  3801. (if (or bob-special (equal arg '(4)))
  3802. ;; special case: use global cycling
  3803. (setq arg t))
  3804. (cond
  3805. ((equal arg '(16))
  3806. (org-set-startup-visibility)
  3807. (message "Startup visibility, plus VISIBILITY properties"))
  3808. ((equal arg '(64))
  3809. (show-all)
  3810. (message "Entire buffer visible, including drawers"))
  3811. ((org-at-table-p 'any)
  3812. ;; Enter the table or move to the next field in the table
  3813. (or (org-table-recognize-table.el)
  3814. (progn
  3815. (if arg (org-table-edit-field t)
  3816. (org-table-justify-field-maybe)
  3817. (call-interactively 'org-table-next-field)))))
  3818. ((eq arg t) ;; Global cycling
  3819. (cond
  3820. ((and (eq last-command this-command)
  3821. (eq org-cycle-global-status 'overview))
  3822. ;; We just created the overview - now do table of contents
  3823. ;; This can be slow in very large buffers, so indicate action
  3824. (message "CONTENTS...")
  3825. (org-content)
  3826. (message "CONTENTS...done")
  3827. (setq org-cycle-global-status 'contents)
  3828. (run-hook-with-args 'org-cycle-hook 'contents))
  3829. ((and (eq last-command this-command)
  3830. (eq org-cycle-global-status 'contents))
  3831. ;; We just showed the table of contents - now show everything
  3832. (show-all)
  3833. (message "SHOW ALL")
  3834. (setq org-cycle-global-status 'all)
  3835. (run-hook-with-args 'org-cycle-hook 'all))
  3836. (t
  3837. ;; Default action: go to overview
  3838. (org-overview)
  3839. (message "OVERVIEW")
  3840. (setq org-cycle-global-status 'overview)
  3841. (run-hook-with-args 'org-cycle-hook 'overview))))
  3842. ((and org-drawers org-drawer-regexp
  3843. (save-excursion
  3844. (beginning-of-line 1)
  3845. (looking-at org-drawer-regexp)))
  3846. ;; Toggle block visibility
  3847. (org-flag-drawer
  3848. (not (get-char-property (match-end 0) 'invisible))))
  3849. ((integerp arg)
  3850. ;; Show-subtree, ARG levels up from here.
  3851. (save-excursion
  3852. (org-back-to-heading)
  3853. (outline-up-heading (if (< arg 0) (- arg)
  3854. (- (funcall outline-level) arg)))
  3855. (org-show-subtree)))
  3856. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3857. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3858. ;; At a heading: rotate between three different views
  3859. (org-back-to-heading)
  3860. (let ((goal-column 0) eoh eol eos)
  3861. ;; First, some boundaries
  3862. (save-excursion
  3863. (org-back-to-heading)
  3864. (save-excursion
  3865. (beginning-of-line 2)
  3866. (while (and (not (eobp)) ;; this is like `next-line'
  3867. (get-char-property (1- (point)) 'invisible))
  3868. (beginning-of-line 2)) (setq eol (point)))
  3869. (outline-end-of-heading) (setq eoh (point))
  3870. (org-end-of-subtree t)
  3871. (unless (eobp)
  3872. (skip-chars-forward " \t\n")
  3873. (beginning-of-line 1) ; in case this is an item
  3874. )
  3875. (setq eos (1- (point))))
  3876. ;; Find out what to do next and set `this-command'
  3877. (cond
  3878. ((= eos eoh)
  3879. ;; Nothing is hidden behind this heading
  3880. (message "EMPTY ENTRY")
  3881. (setq org-cycle-subtree-status nil)
  3882. (save-excursion
  3883. (goto-char eos)
  3884. (outline-next-heading)
  3885. (if (org-invisible-p) (org-flag-heading nil))))
  3886. ((or (>= eol eos)
  3887. (not (string-match "\\S-" (buffer-substring eol eos))))
  3888. ;; Entire subtree is hidden in one line: open it
  3889. (org-show-entry)
  3890. (show-children)
  3891. (message "CHILDREN")
  3892. (save-excursion
  3893. (goto-char eos)
  3894. (outline-next-heading)
  3895. (if (org-invisible-p) (org-flag-heading nil)))
  3896. (setq org-cycle-subtree-status 'children)
  3897. (run-hook-with-args 'org-cycle-hook 'children))
  3898. ((and (eq last-command this-command)
  3899. (eq org-cycle-subtree-status 'children))
  3900. ;; We just showed the children, now show everything.
  3901. (org-show-subtree)
  3902. (message "SUBTREE")
  3903. (setq org-cycle-subtree-status 'subtree)
  3904. (run-hook-with-args 'org-cycle-hook 'subtree))
  3905. (t
  3906. ;; Default action: hide the subtree.
  3907. (hide-subtree)
  3908. (message "FOLDED")
  3909. (setq org-cycle-subtree-status 'folded)
  3910. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3911. ;; TAB emulation and template completion
  3912. (buffer-read-only (org-back-to-heading))
  3913. ((org-try-structure-completion))
  3914. ((org-try-cdlatex-tab))
  3915. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3916. (or (not (bolp))
  3917. (not (looking-at outline-regexp))))
  3918. (call-interactively (global-key-binding "\t")))
  3919. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3920. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3921. (or (and (eq org-cycle-emulate-tab 'white)
  3922. (= (match-end 0) (point-at-eol)))
  3923. (and (eq org-cycle-emulate-tab 'whitestart)
  3924. (>= (match-end 0) pos))))
  3925. t
  3926. (eq org-cycle-emulate-tab t))
  3927. (call-interactively (global-key-binding "\t")))
  3928. (t (save-excursion
  3929. (org-back-to-heading)
  3930. (org-cycle))))))
  3931. ;;;###autoload
  3932. (defun org-global-cycle (&optional arg)
  3933. "Cycle the global visibility. For details see `org-cycle'.
  3934. With C-u prefix arg, switch to startup visibility.
  3935. With a numeric prefix, show all headlines up to that level."
  3936. (interactive "P")
  3937. (let ((org-cycle-include-plain-lists
  3938. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3939. (cond
  3940. ((integerp arg)
  3941. (show-all)
  3942. (hide-sublevels arg)
  3943. (setq org-cycle-global-status 'contents))
  3944. ((equal arg '(4))
  3945. (org-set-startup-visibility)
  3946. (message "Startup visibility, plus VISIBILITY properties."))
  3947. (t
  3948. (org-cycle '(4))))))
  3949. (defun org-set-startup-visibility ()
  3950. "Set the visibility required by startup options and properties."
  3951. (cond
  3952. ((eq org-startup-folded t)
  3953. (org-cycle '(4)))
  3954. ((eq org-startup-folded 'content)
  3955. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3956. (org-cycle '(4)) (org-cycle '(4)))))
  3957. (org-set-visibility-according-to-property 'no-cleanup)
  3958. (org-cycle-hide-archived-subtrees 'all)
  3959. (org-cycle-hide-drawers 'all)
  3960. (org-cycle-show-empty-lines 'all))
  3961. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3962. "Switch subtree visibilities according to :VISIBILITY: property."
  3963. (interactive)
  3964. (let (org-show-entry-below state)
  3965. (save-excursion
  3966. (goto-char (point-min))
  3967. (while (re-search-forward
  3968. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3969. nil t)
  3970. (setq state (match-string 1))
  3971. (save-excursion
  3972. (org-back-to-heading t)
  3973. (hide-subtree)
  3974. (org-reveal)
  3975. (cond
  3976. ((equal state '("fold" "folded"))
  3977. (hide-subtree))
  3978. ((equal state "children")
  3979. (org-show-hidden-entry)
  3980. (show-children))
  3981. ((equal state "content")
  3982. (save-excursion
  3983. (save-restriction
  3984. (org-narrow-to-subtree)
  3985. (org-content))))
  3986. ((member state '("all" "showall"))
  3987. (show-subtree)))))
  3988. (unless no-cleanup
  3989. (org-cycle-hide-archived-subtrees 'all)
  3990. (org-cycle-hide-drawers 'all)
  3991. (org-cycle-show-empty-lines 'all)))))
  3992. (defun org-overview ()
  3993. "Switch to overview mode, showing only top-level headlines.
  3994. Really, this shows all headlines with level equal or greater than the level
  3995. of the first headline in the buffer. This is important, because if the
  3996. first headline is not level one, then (hide-sublevels 1) gives confusing
  3997. results."
  3998. (interactive)
  3999. (let ((level (save-excursion
  4000. (goto-char (point-min))
  4001. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4002. (progn
  4003. (goto-char (match-beginning 0))
  4004. (funcall outline-level))))))
  4005. (and level (hide-sublevels level))))
  4006. (defun org-content (&optional arg)
  4007. "Show all headlines in the buffer, like a table of contents.
  4008. With numerical argument N, show content up to level N."
  4009. (interactive "P")
  4010. (save-excursion
  4011. ;; Visit all headings and show their offspring
  4012. (and (integerp arg) (org-overview))
  4013. (goto-char (point-max))
  4014. (catch 'exit
  4015. (while (and (progn (condition-case nil
  4016. (outline-previous-visible-heading 1)
  4017. (error (goto-char (point-min))))
  4018. t)
  4019. (looking-at outline-regexp))
  4020. (if (integerp arg)
  4021. (show-children (1- arg))
  4022. (show-branches))
  4023. (if (bobp) (throw 'exit nil))))))
  4024. (defun org-optimize-window-after-visibility-change (state)
  4025. "Adjust the window after a change in outline visibility.
  4026. This function is the default value of the hook `org-cycle-hook'."
  4027. (when (get-buffer-window (current-buffer))
  4028. (cond
  4029. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4030. ; ((eq state 'overview) (org-beginning-of-line))
  4031. ((eq state 'content) nil)
  4032. ((eq state 'all) nil)
  4033. ((eq state 'folded) nil)
  4034. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4035. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4036. (defun org-compact-display-after-subtree-move ()
  4037. "Show a compacter version of the tree of the entry's parent."
  4038. (save-excursion
  4039. (if (org-up-heading-safe)
  4040. (progn
  4041. (hide-subtree)
  4042. (show-entry)
  4043. (show-children)
  4044. (org-cycle-show-empty-lines 'children)
  4045. (org-cycle-hide-drawers 'children))
  4046. (org-overview))))
  4047. (defun org-cycle-show-empty-lines (state)
  4048. "Show empty lines above all visible headlines.
  4049. The region to be covered depends on STATE when called through
  4050. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4051. entire buffer covered. Note that an empty line is only shown if there
  4052. are at least `org-cycle-separator-lines' empty lines before the headline."
  4053. (when (> org-cycle-separator-lines 0)
  4054. (save-excursion
  4055. (let* ((n org-cycle-separator-lines)
  4056. (re (cond
  4057. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4058. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4059. (t (let ((ns (number-to-string (- n 2))))
  4060. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4061. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4062. beg end)
  4063. (cond
  4064. ((memq state '(overview contents t))
  4065. (setq beg (point-min) end (point-max)))
  4066. ((memq state '(children folded))
  4067. (setq beg (point) end (progn (org-end-of-subtree t t)
  4068. (beginning-of-line 2)
  4069. (point)))))
  4070. (when beg
  4071. (goto-char beg)
  4072. (while (re-search-forward re end t)
  4073. (if (not (get-char-property (match-end 1) 'invisible))
  4074. (outline-flag-region
  4075. (match-beginning 1) (match-end 1) nil)))))))
  4076. ;; Never hide empty lines at the end of the file.
  4077. (save-excursion
  4078. (goto-char (point-max))
  4079. (outline-previous-heading)
  4080. (outline-end-of-heading)
  4081. (if (and (looking-at "[ \t\n]+")
  4082. (= (match-end 0) (point-max)))
  4083. (outline-flag-region (point) (match-end 0) nil))))
  4084. (defun org-show-empty-lines-in-parent ()
  4085. "Move to the parent and re-show empty lines before visible headlines."
  4086. (save-excursion
  4087. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4088. (org-cycle-show-empty-lines context))))
  4089. (defun org-cycle-hide-drawers (state)
  4090. "Re-hide all drawers after a visibility state change."
  4091. (when (and (org-mode-p)
  4092. (not (memq state '(overview folded))))
  4093. (save-excursion
  4094. (let* ((globalp (memq state '(contents all)))
  4095. (beg (if globalp (point-min) (point)))
  4096. (end (if globalp (point-max) (org-end-of-subtree t))))
  4097. (goto-char beg)
  4098. (while (re-search-forward org-drawer-regexp end t)
  4099. (org-flag-drawer t))))))
  4100. (defun org-flag-drawer (flag)
  4101. (save-excursion
  4102. (beginning-of-line 1)
  4103. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4104. (let ((b (match-end 0))
  4105. (outline-regexp org-outline-regexp))
  4106. (if (re-search-forward
  4107. "^[ \t]*:END:"
  4108. (save-excursion (outline-next-heading) (point)) t)
  4109. (outline-flag-region b (point-at-eol) flag)
  4110. (error ":END: line missing"))))))
  4111. (defun org-subtree-end-visible-p ()
  4112. "Is the end of the current subtree visible?"
  4113. (pos-visible-in-window-p
  4114. (save-excursion (org-end-of-subtree t) (point))))
  4115. (defun org-first-headline-recenter (&optional N)
  4116. "Move cursor to the first headline and recenter the headline.
  4117. Optional argument N means, put the headline into the Nth line of the window."
  4118. (goto-char (point-min))
  4119. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4120. (beginning-of-line)
  4121. (recenter (prefix-numeric-value N))))
  4122. ;;; Org-goto
  4123. (defvar org-goto-window-configuration nil)
  4124. (defvar org-goto-marker nil)
  4125. (defvar org-goto-map
  4126. (let ((map (make-sparse-keymap)))
  4127. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4128. (while (setq cmd (pop cmds))
  4129. (substitute-key-definition cmd cmd map global-map)))
  4130. (suppress-keymap map)
  4131. (org-defkey map "\C-m" 'org-goto-ret)
  4132. (org-defkey map [(return)] 'org-goto-ret)
  4133. (org-defkey map [(left)] 'org-goto-left)
  4134. (org-defkey map [(right)] 'org-goto-right)
  4135. (org-defkey map [(control ?g)] 'org-goto-quit)
  4136. (org-defkey map "\C-i" 'org-cycle)
  4137. (org-defkey map [(tab)] 'org-cycle)
  4138. (org-defkey map [(down)] 'outline-next-visible-heading)
  4139. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4140. (if org-goto-auto-isearch
  4141. (if (fboundp 'define-key-after)
  4142. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4143. nil)
  4144. (org-defkey map "q" 'org-goto-quit)
  4145. (org-defkey map "n" 'outline-next-visible-heading)
  4146. (org-defkey map "p" 'outline-previous-visible-heading)
  4147. (org-defkey map "f" 'outline-forward-same-level)
  4148. (org-defkey map "b" 'outline-backward-same-level)
  4149. (org-defkey map "u" 'outline-up-heading))
  4150. (org-defkey map "/" 'org-occur)
  4151. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4152. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4153. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4154. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4155. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4156. map))
  4157. (defconst org-goto-help
  4158. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4159. RET=jump to location [Q]uit and return to previous location
  4160. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4161. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4162. ;; FIXME: Docstring doe not mention both interfaces
  4163. (defun org-goto (&optional alternative-interface)
  4164. "Look up a different location in the current file, keeping current visibility.
  4165. When you want look-up or go to a different location in a document, the
  4166. fastest way is often to fold the entire buffer and then dive into the tree.
  4167. This method has the disadvantage, that the previous location will be folded,
  4168. which may not be what you want.
  4169. This command works around this by showing a copy of the current buffer
  4170. in an indirect buffer, in overview mode. You can dive into the tree in
  4171. that copy, use org-occur and incremental search to find a location.
  4172. When pressing RET or `Q', the command returns to the original buffer in
  4173. which the visibility is still unchanged. After RET is will also jump to
  4174. the location selected in the indirect buffer and expose the
  4175. the headline hierarchy above."
  4176. (interactive "P")
  4177. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4178. (org-refile-use-outline-path t)
  4179. (interface
  4180. (if (not alternative-interface)
  4181. org-goto-interface
  4182. (if (eq org-goto-interface 'outline)
  4183. 'outline-path-completion
  4184. 'outline)))
  4185. (org-goto-start-pos (point))
  4186. (selected-point
  4187. (if (eq interface 'outline)
  4188. (car (org-get-location (current-buffer) org-goto-help))
  4189. (nth 3 (org-refile-get-location "Goto: ")))))
  4190. (if selected-point
  4191. (progn
  4192. (org-mark-ring-push org-goto-start-pos)
  4193. (goto-char selected-point)
  4194. (if (or (org-invisible-p) (org-invisible-p2))
  4195. (org-show-context 'org-goto)))
  4196. (message "Quit"))))
  4197. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4198. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4199. (defvar org-goto-local-auto-isearch-map) ; defined below
  4200. (defun org-get-location (buf help)
  4201. "Let the user select a location in the Org-mode buffer BUF.
  4202. This function uses a recursive edit. It returns the selected position
  4203. or nil."
  4204. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4205. (isearch-hide-immediately nil)
  4206. (isearch-search-fun-function
  4207. (lambda () 'org-goto-local-search-headings))
  4208. (org-goto-selected-point org-goto-exit-command))
  4209. (save-excursion
  4210. (save-window-excursion
  4211. (delete-other-windows)
  4212. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4213. (switch-to-buffer
  4214. (condition-case nil
  4215. (make-indirect-buffer (current-buffer) "*org-goto*")
  4216. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4217. (with-output-to-temp-buffer "*Help*"
  4218. (princ help))
  4219. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4220. (setq buffer-read-only nil)
  4221. (let ((org-startup-truncated t)
  4222. (org-startup-folded nil)
  4223. (org-startup-align-all-tables nil))
  4224. (org-mode)
  4225. (org-overview))
  4226. (setq buffer-read-only t)
  4227. (if (and (boundp 'org-goto-start-pos)
  4228. (integer-or-marker-p org-goto-start-pos))
  4229. (let ((org-show-hierarchy-above t)
  4230. (org-show-siblings t)
  4231. (org-show-following-heading t))
  4232. (goto-char org-goto-start-pos)
  4233. (and (org-invisible-p) (org-show-context)))
  4234. (goto-char (point-min)))
  4235. (org-beginning-of-line)
  4236. (message "Select location and press RET")
  4237. (use-local-map org-goto-map)
  4238. (recursive-edit)
  4239. ))
  4240. (kill-buffer "*org-goto*")
  4241. (cons org-goto-selected-point org-goto-exit-command)))
  4242. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4243. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4244. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4245. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4246. (defun org-goto-local-search-headings (string bound noerror)
  4247. "Search and make sure that any matches are in headlines."
  4248. (catch 'return
  4249. (while (if isearch-forward
  4250. (search-forward string bound noerror)
  4251. (search-backward string bound noerror))
  4252. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4253. (and (member :headline context)
  4254. (not (member :tags context))))
  4255. (throw 'return (point))))))
  4256. (defun org-goto-local-auto-isearch ()
  4257. "Start isearch."
  4258. (interactive)
  4259. (goto-char (point-min))
  4260. (let ((keys (this-command-keys)))
  4261. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4262. (isearch-mode t)
  4263. (isearch-process-search-char (string-to-char keys)))))
  4264. (defun org-goto-ret (&optional arg)
  4265. "Finish `org-goto' by going to the new location."
  4266. (interactive "P")
  4267. (setq org-goto-selected-point (point)
  4268. org-goto-exit-command 'return)
  4269. (throw 'exit nil))
  4270. (defun org-goto-left ()
  4271. "Finish `org-goto' by going to the new location."
  4272. (interactive)
  4273. (if (org-on-heading-p)
  4274. (progn
  4275. (beginning-of-line 1)
  4276. (setq org-goto-selected-point (point)
  4277. org-goto-exit-command 'left)
  4278. (throw 'exit nil))
  4279. (error "Not on a heading")))
  4280. (defun org-goto-right ()
  4281. "Finish `org-goto' by going to the new location."
  4282. (interactive)
  4283. (if (org-on-heading-p)
  4284. (progn
  4285. (setq org-goto-selected-point (point)
  4286. org-goto-exit-command 'right)
  4287. (throw 'exit nil))
  4288. (error "Not on a heading")))
  4289. (defun org-goto-quit ()
  4290. "Finish `org-goto' without cursor motion."
  4291. (interactive)
  4292. (setq org-goto-selected-point nil)
  4293. (setq org-goto-exit-command 'quit)
  4294. (throw 'exit nil))
  4295. ;;; Indirect buffer display of subtrees
  4296. (defvar org-indirect-dedicated-frame nil
  4297. "This is the frame being used for indirect tree display.")
  4298. (defvar org-last-indirect-buffer nil)
  4299. (defun org-tree-to-indirect-buffer (&optional arg)
  4300. "Create indirect buffer and narrow it to current subtree.
  4301. With numerical prefix ARG, go up to this level and then take that tree.
  4302. If ARG is negative, go up that many levels.
  4303. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4304. indirect buffer previously made with this command, to avoid proliferation of
  4305. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4306. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4307. is kept so that you can work with several indirect buffers at the same time.
  4308. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4309. requests that a new frame be made for the new buffer, so that the dedicated
  4310. frame is not changed."
  4311. (interactive "P")
  4312. (let ((cbuf (current-buffer))
  4313. (cwin (selected-window))
  4314. (pos (point))
  4315. beg end level heading ibuf)
  4316. (save-excursion
  4317. (org-back-to-heading t)
  4318. (when (numberp arg)
  4319. (setq level (org-outline-level))
  4320. (if (< arg 0) (setq arg (+ level arg)))
  4321. (while (> (setq level (org-outline-level)) arg)
  4322. (outline-up-heading 1 t)))
  4323. (setq beg (point)
  4324. heading (org-get-heading))
  4325. (org-end-of-subtree t) (setq end (point)))
  4326. (if (and (buffer-live-p org-last-indirect-buffer)
  4327. (not (eq org-indirect-buffer-display 'new-frame))
  4328. (not arg))
  4329. (kill-buffer org-last-indirect-buffer))
  4330. (setq ibuf (org-get-indirect-buffer cbuf)
  4331. org-last-indirect-buffer ibuf)
  4332. (cond
  4333. ((or (eq org-indirect-buffer-display 'new-frame)
  4334. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4335. (select-frame (make-frame))
  4336. (delete-other-windows)
  4337. (switch-to-buffer ibuf)
  4338. (org-set-frame-title heading))
  4339. ((eq org-indirect-buffer-display 'dedicated-frame)
  4340. (raise-frame
  4341. (select-frame (or (and org-indirect-dedicated-frame
  4342. (frame-live-p org-indirect-dedicated-frame)
  4343. org-indirect-dedicated-frame)
  4344. (setq org-indirect-dedicated-frame (make-frame)))))
  4345. (delete-other-windows)
  4346. (switch-to-buffer ibuf)
  4347. (org-set-frame-title (concat "Indirect: " heading)))
  4348. ((eq org-indirect-buffer-display 'current-window)
  4349. (switch-to-buffer ibuf))
  4350. ((eq org-indirect-buffer-display 'other-window)
  4351. (pop-to-buffer ibuf))
  4352. (t (error "Invalid value.")))
  4353. (if (featurep 'xemacs)
  4354. (save-excursion (org-mode) (turn-on-font-lock)))
  4355. (narrow-to-region beg end)
  4356. (show-all)
  4357. (goto-char pos)
  4358. (and (window-live-p cwin) (select-window cwin))))
  4359. (defun org-get-indirect-buffer (&optional buffer)
  4360. (setq buffer (or buffer (current-buffer)))
  4361. (let ((n 1) (base (buffer-name buffer)) bname)
  4362. (while (buffer-live-p
  4363. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4364. (setq n (1+ n)))
  4365. (condition-case nil
  4366. (make-indirect-buffer buffer bname 'clone)
  4367. (error (make-indirect-buffer buffer bname)))))
  4368. (defun org-set-frame-title (title)
  4369. "Set the title of the current frame to the string TITLE."
  4370. ;; FIXME: how to name a single frame in XEmacs???
  4371. (unless (featurep 'xemacs)
  4372. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4373. ;;;; Structure editing
  4374. ;;; Inserting headlines
  4375. (defun org-previous-line-empty-p ()
  4376. (save-excursion
  4377. (and (not (bobp))
  4378. (or (beginning-of-line 0) t)
  4379. (save-match-data
  4380. (looking-at "[ \t]*$")))))
  4381. (defun org-insert-heading (&optional force-heading)
  4382. "Insert a new heading or item with same depth at point.
  4383. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4384. If point is at the beginning of a headline, insert a sibling before the
  4385. current headline. If point is not at the beginning, do not split the line,
  4386. but create the new headline after the current line."
  4387. (interactive "P")
  4388. (if (= (buffer-size) 0)
  4389. (insert "\n* ")
  4390. (when (or force-heading (not (org-insert-item)))
  4391. (let* ((empty-line-p nil)
  4392. (head (save-excursion
  4393. (condition-case nil
  4394. (progn
  4395. (org-back-to-heading)
  4396. (setq empty-line-p (org-previous-line-empty-p))
  4397. (match-string 0))
  4398. (error "*"))))
  4399. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4400. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4401. pos hide-previous previous-pos)
  4402. (cond
  4403. ((and (org-on-heading-p) (bolp)
  4404. (or (bobp)
  4405. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4406. ;; insert before the current line
  4407. (open-line (if blank 2 1)))
  4408. ((and (bolp)
  4409. (or (bobp)
  4410. (save-excursion
  4411. (backward-char 1) (not (org-invisible-p)))))
  4412. ;; insert right here
  4413. nil)
  4414. (t
  4415. ;; somewhere in the line
  4416. (save-excursion
  4417. (setq previous-pos (point-at-bol))
  4418. (end-of-line)
  4419. (setq hide-previous (org-invisible-p)))
  4420. (and org-insert-heading-respect-content (org-show-subtree))
  4421. (let ((split
  4422. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4423. (save-excursion
  4424. (let ((p (point)))
  4425. (goto-char (point-at-bol))
  4426. (and (looking-at org-complex-heading-regexp)
  4427. (> p (match-beginning 4)))))))
  4428. tags pos)
  4429. (cond
  4430. (org-insert-heading-respect-content
  4431. (org-end-of-subtree nil t)
  4432. (or (bolp) (newline))
  4433. (or (org-previous-line-empty-p)
  4434. (and blank (newline)))
  4435. (open-line 1))
  4436. ((org-on-heading-p)
  4437. (when hide-previous
  4438. (show-children)
  4439. (org-show-entry))
  4440. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4441. (setq tags (and (match-end 2) (match-string 2)))
  4442. (and (match-end 1)
  4443. (delete-region (match-beginning 1) (match-end 1)))
  4444. (setq pos (point-at-bol))
  4445. (or split (end-of-line 1))
  4446. (delete-horizontal-space)
  4447. (newline (if blank 2 1))
  4448. (when tags
  4449. (save-excursion
  4450. (goto-char pos)
  4451. (end-of-line 1)
  4452. (insert " " tags)
  4453. (org-set-tags nil 'align))))
  4454. (t
  4455. (or split (end-of-line 1))
  4456. (newline (if blank 2 1)))))))
  4457. (insert head) (just-one-space)
  4458. (setq pos (point))
  4459. (end-of-line 1)
  4460. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4461. (when (and org-insert-heading-respect-content hide-previous)
  4462. (save-excursion
  4463. (goto-char previous-pos)
  4464. (hide-subtree)))
  4465. (run-hooks 'org-insert-heading-hook)))))
  4466. (defun org-get-heading (&optional no-tags)
  4467. "Return the heading of the current entry, without the stars."
  4468. (save-excursion
  4469. (org-back-to-heading t)
  4470. (if (looking-at
  4471. (if no-tags
  4472. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4473. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4474. (match-string 1) "")))
  4475. (defun org-heading-components ()
  4476. "Return the components of the current heading.
  4477. This is a list with the following elements:
  4478. - the level as an integer
  4479. - the reduced level, different if `org-odd-levels-only' is set.
  4480. - the TODO keyword, or nil
  4481. - the priority character, like ?A, or nil if no priority is given
  4482. - the headline text itself, or the tags string if no headline text
  4483. - the tags string, or nil."
  4484. (save-excursion
  4485. (org-back-to-heading t)
  4486. (if (looking-at org-complex-heading-regexp)
  4487. (list (length (match-string 1))
  4488. (org-reduced-level (length (match-string 1)))
  4489. (org-match-string-no-properties 2)
  4490. (and (match-end 3) (aref (match-string 3) 2))
  4491. (org-match-string-no-properties 4)
  4492. (org-match-string-no-properties 5)))))
  4493. (defun org-insert-heading-after-current ()
  4494. "Insert a new heading with same level as current, after current subtree."
  4495. (interactive)
  4496. (org-back-to-heading)
  4497. (org-insert-heading)
  4498. (org-move-subtree-down)
  4499. (end-of-line 1))
  4500. (defun org-insert-heading-respect-content ()
  4501. (interactive)
  4502. (let ((org-insert-heading-respect-content t))
  4503. (org-insert-heading t)))
  4504. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4505. (interactive "P")
  4506. (let ((org-insert-heading-respect-content t))
  4507. (org-insert-todo-heading force-state t)))
  4508. (defun org-insert-todo-heading (arg &optional force-heading)
  4509. "Insert a new heading with the same level and TODO state as current heading.
  4510. If the heading has no TODO state, or if the state is DONE, use the first
  4511. state (TODO by default). Also with prefix arg, force first state."
  4512. (interactive "P")
  4513. (when (or force-heading (not (org-insert-item 'checkbox)))
  4514. (org-insert-heading force-heading)
  4515. (save-excursion
  4516. (org-back-to-heading)
  4517. (outline-previous-heading)
  4518. (looking-at org-todo-line-regexp))
  4519. (if (or arg
  4520. (not (match-beginning 2))
  4521. (member (match-string 2) org-done-keywords))
  4522. (insert (car org-todo-keywords-1) " ")
  4523. (insert (match-string 2) " "))
  4524. (when org-provide-todo-statistics
  4525. (org-update-parent-todo-statistics))))
  4526. (defun org-insert-subheading (arg)
  4527. "Insert a new subheading and demote it.
  4528. Works for outline headings and for plain lists alike."
  4529. (interactive "P")
  4530. (org-insert-heading arg)
  4531. (cond
  4532. ((org-on-heading-p) (org-do-demote))
  4533. ((org-at-item-p) (org-indent-item 1))))
  4534. (defun org-insert-todo-subheading (arg)
  4535. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4536. Works for outline headings and for plain lists alike."
  4537. (interactive "P")
  4538. (org-insert-todo-heading arg)
  4539. (cond
  4540. ((org-on-heading-p) (org-do-demote))
  4541. ((org-at-item-p) (org-indent-item 1))))
  4542. ;;; Promotion and Demotion
  4543. (defun org-promote-subtree ()
  4544. "Promote the entire subtree.
  4545. See also `org-promote'."
  4546. (interactive)
  4547. (save-excursion
  4548. (org-map-tree 'org-promote))
  4549. (org-fix-position-after-promote))
  4550. (defun org-demote-subtree ()
  4551. "Demote the entire subtree. See `org-demote'.
  4552. See also `org-promote'."
  4553. (interactive)
  4554. (save-excursion
  4555. (org-map-tree 'org-demote))
  4556. (org-fix-position-after-promote))
  4557. (defun org-do-promote ()
  4558. "Promote the current heading higher up the tree.
  4559. If the region is active in `transient-mark-mode', promote all headings
  4560. in the region."
  4561. (interactive)
  4562. (save-excursion
  4563. (if (org-region-active-p)
  4564. (org-map-region 'org-promote (region-beginning) (region-end))
  4565. (org-promote)))
  4566. (org-fix-position-after-promote))
  4567. (defun org-do-demote ()
  4568. "Demote the current heading lower down the tree.
  4569. If the region is active in `transient-mark-mode', demote all headings
  4570. in the region."
  4571. (interactive)
  4572. (save-excursion
  4573. (if (org-region-active-p)
  4574. (org-map-region 'org-demote (region-beginning) (region-end))
  4575. (org-demote)))
  4576. (org-fix-position-after-promote))
  4577. (defun org-fix-position-after-promote ()
  4578. "Make sure that after pro/demotion cursor position is right."
  4579. (let ((pos (point)))
  4580. (when (save-excursion
  4581. (beginning-of-line 1)
  4582. (looking-at org-todo-line-regexp)
  4583. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4584. (cond ((eobp) (insert " "))
  4585. ((eolp) (insert " "))
  4586. ((equal (char-after) ?\ ) (forward-char 1))))))
  4587. (defun org-reduced-level (l)
  4588. "Compute the effective level of a heading.
  4589. This takes into account the setting of `org-odd-levels-only'."
  4590. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4591. (defun org-get-valid-level (level &optional change)
  4592. "Rectify a level change under the influence of `org-odd-levels-only'
  4593. LEVEL is a current level, CHANGE is by how much the level should be
  4594. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4595. even level numbers will become the next higher odd number."
  4596. (if org-odd-levels-only
  4597. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4598. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4599. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4600. (max 1 (+ level change))))
  4601. (if (boundp 'define-obsolete-function-alias)
  4602. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4603. (define-obsolete-function-alias 'org-get-legal-level
  4604. 'org-get-valid-level)
  4605. (define-obsolete-function-alias 'org-get-legal-level
  4606. 'org-get-valid-level "23.1")))
  4607. (defun org-promote ()
  4608. "Promote the current heading higher up the tree.
  4609. If the region is active in `transient-mark-mode', promote all headings
  4610. in the region."
  4611. (org-back-to-heading t)
  4612. (let* ((level (save-match-data (funcall outline-level)))
  4613. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4614. (diff (abs (- level (length up-head) -1))))
  4615. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4616. (replace-match up-head nil t)
  4617. ;; Fixup tag positioning
  4618. (and org-auto-align-tags (org-set-tags nil t))
  4619. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4620. (defun org-demote ()
  4621. "Demote the current heading lower down the tree.
  4622. If the region is active in `transient-mark-mode', demote all headings
  4623. in the region."
  4624. (org-back-to-heading t)
  4625. (let* ((level (save-match-data (funcall outline-level)))
  4626. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4627. (diff (abs (- level (length down-head) -1))))
  4628. (replace-match down-head nil t)
  4629. ;; Fixup tag positioning
  4630. (and org-auto-align-tags (org-set-tags nil t))
  4631. (if org-adapt-indentation (org-fixup-indentation diff))))
  4632. (defun org-map-tree (fun)
  4633. "Call FUN for every heading underneath the current one."
  4634. (org-back-to-heading)
  4635. (let ((level (funcall outline-level)))
  4636. (save-excursion
  4637. (funcall fun)
  4638. (while (and (progn
  4639. (outline-next-heading)
  4640. (> (funcall outline-level) level))
  4641. (not (eobp)))
  4642. (funcall fun)))))
  4643. (defun org-map-region (fun beg end)
  4644. "Call FUN for every heading between BEG and END."
  4645. (let ((org-ignore-region t))
  4646. (save-excursion
  4647. (setq end (copy-marker end))
  4648. (goto-char beg)
  4649. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4650. (< (point) end))
  4651. (funcall fun))
  4652. (while (and (progn
  4653. (outline-next-heading)
  4654. (< (point) end))
  4655. (not (eobp)))
  4656. (funcall fun)))))
  4657. (defun org-fixup-indentation (diff)
  4658. "Change the indentation in the current entry by DIFF
  4659. However, if any line in the current entry has no indentation, or if it
  4660. would end up with no indentation after the change, nothing at all is done."
  4661. (save-excursion
  4662. (let ((end (save-excursion (outline-next-heading)
  4663. (point-marker)))
  4664. (prohibit (if (> diff 0)
  4665. "^\\S-"
  4666. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4667. col)
  4668. (unless (save-excursion (end-of-line 1)
  4669. (re-search-forward prohibit end t))
  4670. (while (and (< (point) end)
  4671. (re-search-forward "^[ \t]+" end t))
  4672. (goto-char (match-end 0))
  4673. (setq col (current-column))
  4674. (if (< diff 0) (replace-match ""))
  4675. (org-indent-to-column (+ diff col))))
  4676. (move-marker end nil))))
  4677. (defun org-convert-to-odd-levels ()
  4678. "Convert an org-mode file with all levels allowed to one with odd levels.
  4679. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4680. level 5 etc."
  4681. (interactive)
  4682. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4683. (let ((org-odd-levels-only nil) n)
  4684. (save-excursion
  4685. (goto-char (point-min))
  4686. (while (re-search-forward "^\\*\\*+ " nil t)
  4687. (setq n (- (length (match-string 0)) 2))
  4688. (while (>= (setq n (1- n)) 0)
  4689. (org-demote))
  4690. (end-of-line 1))))))
  4691. (defun org-convert-to-oddeven-levels ()
  4692. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4693. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4694. section with an even level, conversion would destroy the structure of the file. An error
  4695. is signaled in this case."
  4696. (interactive)
  4697. (goto-char (point-min))
  4698. ;; First check if there are no even levels
  4699. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4700. (org-show-context t)
  4701. (error "Not all levels are odd in this file. Conversion not possible."))
  4702. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4703. (let ((org-odd-levels-only nil) n)
  4704. (save-excursion
  4705. (goto-char (point-min))
  4706. (while (re-search-forward "^\\*\\*+ " nil t)
  4707. (setq n (/ (1- (length (match-string 0))) 2))
  4708. (while (>= (setq n (1- n)) 0)
  4709. (org-promote))
  4710. (end-of-line 1))))))
  4711. (defun org-tr-level (n)
  4712. "Make N odd if required."
  4713. (if org-odd-levels-only (1+ (/ n 2)) n))
  4714. ;;; Vertical tree motion, cutting and pasting of subtrees
  4715. (defun org-move-subtree-up (&optional arg)
  4716. "Move the current subtree up past ARG headlines of the same level."
  4717. (interactive "p")
  4718. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4719. (defun org-move-subtree-down (&optional arg)
  4720. "Move the current subtree down past ARG headlines of the same level."
  4721. (interactive "p")
  4722. (setq arg (prefix-numeric-value arg))
  4723. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4724. 'outline-get-last-sibling))
  4725. (ins-point (make-marker))
  4726. (cnt (abs arg))
  4727. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4728. ;; Select the tree
  4729. (org-back-to-heading)
  4730. (setq beg0 (point))
  4731. (save-excursion
  4732. (setq ne-beg (org-back-over-empty-lines))
  4733. (setq beg (point)))
  4734. (save-match-data
  4735. (save-excursion (outline-end-of-heading)
  4736. (setq folded (org-invisible-p)))
  4737. (outline-end-of-subtree))
  4738. (outline-next-heading)
  4739. (setq ne-end (org-back-over-empty-lines))
  4740. (setq end (point))
  4741. (goto-char beg0)
  4742. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4743. ;; include less whitespace
  4744. (save-excursion
  4745. (goto-char beg)
  4746. (forward-line (- ne-beg ne-end))
  4747. (setq beg (point))))
  4748. ;; Find insertion point, with error handling
  4749. (while (> cnt 0)
  4750. (or (and (funcall movfunc) (looking-at outline-regexp))
  4751. (progn (goto-char beg0)
  4752. (error "Cannot move past superior level or buffer limit")))
  4753. (setq cnt (1- cnt)))
  4754. (if (> arg 0)
  4755. ;; Moving forward - still need to move over subtree
  4756. (progn (org-end-of-subtree t t)
  4757. (save-excursion
  4758. (org-back-over-empty-lines)
  4759. (or (bolp) (newline)))))
  4760. (setq ne-ins (org-back-over-empty-lines))
  4761. (move-marker ins-point (point))
  4762. (setq txt (buffer-substring beg end))
  4763. (org-save-markers-in-region beg end)
  4764. (delete-region beg end)
  4765. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4766. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4767. (let ((bbb (point)))
  4768. (insert-before-markers txt)
  4769. (org-reinstall-markers-in-region bbb)
  4770. (move-marker ins-point bbb))
  4771. (or (bolp) (insert "\n"))
  4772. (setq ins-end (point))
  4773. (goto-char ins-point)
  4774. (org-skip-whitespace)
  4775. (when (and (< arg 0)
  4776. (org-first-sibling-p)
  4777. (> ne-ins ne-beg))
  4778. ;; Move whitespace back to beginning
  4779. (save-excursion
  4780. (goto-char ins-end)
  4781. (let ((kill-whole-line t))
  4782. (kill-line (- ne-ins ne-beg)) (point)))
  4783. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4784. (move-marker ins-point nil)
  4785. (org-compact-display-after-subtree-move)
  4786. (org-show-empty-lines-in-parent)
  4787. (unless folded
  4788. (org-show-entry)
  4789. (show-children)
  4790. (org-cycle-hide-drawers 'children))))
  4791. (defvar org-subtree-clip ""
  4792. "Clipboard for cut and paste of subtrees.
  4793. This is actually only a copy of the kill, because we use the normal kill
  4794. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4795. (defvar org-subtree-clip-folded nil
  4796. "Was the last copied subtree folded?
  4797. This is used to fold the tree back after pasting.")
  4798. (defun org-cut-subtree (&optional n)
  4799. "Cut the current subtree into the clipboard.
  4800. With prefix arg N, cut this many sequential subtrees.
  4801. This is a short-hand for marking the subtree and then cutting it."
  4802. (interactive "p")
  4803. (org-copy-subtree n 'cut))
  4804. (defun org-copy-subtree (&optional n cut force-store-markers)
  4805. "Cut the current subtree into the clipboard.
  4806. With prefix arg N, cut this many sequential subtrees.
  4807. This is a short-hand for marking the subtree and then copying it.
  4808. If CUT is non-nil, actually cut the subtree.
  4809. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4810. of some markers in the region, even if CUT is non-nil. This is
  4811. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4812. (interactive "p")
  4813. (let (beg end folded (beg0 (point)))
  4814. (if (interactive-p)
  4815. (org-back-to-heading nil) ; take what looks like a subtree
  4816. (org-back-to-heading t)) ; take what is really there
  4817. (org-back-over-empty-lines)
  4818. (setq beg (point))
  4819. (skip-chars-forward " \t\r\n")
  4820. (save-match-data
  4821. (save-excursion (outline-end-of-heading)
  4822. (setq folded (org-invisible-p)))
  4823. (condition-case nil
  4824. (outline-forward-same-level (1- n))
  4825. (error nil))
  4826. (org-end-of-subtree t t))
  4827. (org-back-over-empty-lines)
  4828. (setq end (point))
  4829. (goto-char beg0)
  4830. (when (> end beg)
  4831. (setq org-subtree-clip-folded folded)
  4832. (when (or cut force-store-markers)
  4833. (org-save-markers-in-region beg end))
  4834. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4835. (setq org-subtree-clip (current-kill 0))
  4836. (message "%s: Subtree(s) with %d characters"
  4837. (if cut "Cut" "Copied")
  4838. (length org-subtree-clip)))))
  4839. (defun org-paste-subtree (&optional level tree for-yank)
  4840. "Paste the clipboard as a subtree, with modification of headline level.
  4841. The entire subtree is promoted or demoted in order to match a new headline
  4842. level.
  4843. If the cursor is at the beginning of a headline, the same level as
  4844. that headline is used to paste the tree
  4845. If not, the new level is derived from the *visible* headings
  4846. before and after the insertion point, and taken to be the inferior headline
  4847. level of the two. So if the previous visible heading is level 3 and the
  4848. next is level 4 (or vice versa), level 4 will be used for insertion.
  4849. This makes sure that the subtree remains an independent subtree and does
  4850. not swallow low level entries.
  4851. You can also force a different level, either by using a numeric prefix
  4852. argument, or by inserting the heading marker by hand. For example, if the
  4853. cursor is after \"*****\", then the tree will be shifted to level 5.
  4854. If optional TREE is given, use this text instead of the kill ring.
  4855. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4856. move back over whitespace before inserting, and move point to the end of
  4857. the inserted text when done."
  4858. (interactive "P")
  4859. (unless (org-kill-is-subtree-p tree)
  4860. (error "%s"
  4861. (substitute-command-keys
  4862. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4863. (let* ((visp (not (org-invisible-p)))
  4864. (txt (or tree (and kill-ring (current-kill 0))))
  4865. (^re (concat "^\\(" outline-regexp "\\)"))
  4866. (re (concat "\\(" outline-regexp "\\)"))
  4867. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4868. (old-level (if (string-match ^re txt)
  4869. (- (match-end 0) (match-beginning 0) 1)
  4870. -1))
  4871. (force-level (cond (level (prefix-numeric-value level))
  4872. ((and (looking-at "[ \t]*$")
  4873. (string-match
  4874. ^re_ (buffer-substring
  4875. (point-at-bol) (point))))
  4876. (- (match-end 1) (match-beginning 1)))
  4877. ((and (bolp)
  4878. (looking-at org-outline-regexp))
  4879. (- (match-end 0) (point) 1))
  4880. (t nil)))
  4881. (previous-level (save-excursion
  4882. (condition-case nil
  4883. (progn
  4884. (outline-previous-visible-heading 1)
  4885. (if (looking-at re)
  4886. (- (match-end 0) (match-beginning 0) 1)
  4887. 1))
  4888. (error 1))))
  4889. (next-level (save-excursion
  4890. (condition-case nil
  4891. (progn
  4892. (or (looking-at outline-regexp)
  4893. (outline-next-visible-heading 1))
  4894. (if (looking-at re)
  4895. (- (match-end 0) (match-beginning 0) 1)
  4896. 1))
  4897. (error 1))))
  4898. (new-level (or force-level (max previous-level next-level)))
  4899. (shift (if (or (= old-level -1)
  4900. (= new-level -1)
  4901. (= old-level new-level))
  4902. 0
  4903. (- new-level old-level)))
  4904. (delta (if (> shift 0) -1 1))
  4905. (func (if (> shift 0) 'org-demote 'org-promote))
  4906. (org-odd-levels-only nil)
  4907. beg end newend)
  4908. ;; Remove the forced level indicator
  4909. (if force-level
  4910. (delete-region (point-at-bol) (point)))
  4911. ;; Paste
  4912. (beginning-of-line 1)
  4913. (unless for-yank (org-back-over-empty-lines))
  4914. (setq beg (point))
  4915. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4916. (insert-before-markers txt)
  4917. (unless (string-match "\n\\'" txt) (insert "\n"))
  4918. (setq newend (point))
  4919. (org-reinstall-markers-in-region beg)
  4920. (setq end (point))
  4921. (goto-char beg)
  4922. (skip-chars-forward " \t\n\r")
  4923. (setq beg (point))
  4924. (if (and (org-invisible-p) visp)
  4925. (save-excursion (outline-show-heading)))
  4926. ;; Shift if necessary
  4927. (unless (= shift 0)
  4928. (save-restriction
  4929. (narrow-to-region beg end)
  4930. (while (not (= shift 0))
  4931. (org-map-region func (point-min) (point-max))
  4932. (setq shift (+ delta shift)))
  4933. (goto-char (point-min))
  4934. (setq newend (point-max))))
  4935. (when (or (interactive-p) for-yank)
  4936. (message "Clipboard pasted as level %d subtree" new-level))
  4937. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4938. kill-ring
  4939. (eq org-subtree-clip (current-kill 0))
  4940. org-subtree-clip-folded)
  4941. ;; The tree was folded before it was killed/copied
  4942. (hide-subtree))
  4943. (and for-yank (goto-char newend))))
  4944. (defun org-kill-is-subtree-p (&optional txt)
  4945. "Check if the current kill is an outline subtree, or a set of trees.
  4946. Returns nil if kill does not start with a headline, or if the first
  4947. headline level is not the largest headline level in the tree.
  4948. So this will actually accept several entries of equal levels as well,
  4949. which is OK for `org-paste-subtree'.
  4950. If optional TXT is given, check this string instead of the current kill."
  4951. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4952. (start-level (and kill
  4953. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4954. org-outline-regexp "\\)")
  4955. kill)
  4956. (- (match-end 2) (match-beginning 2) 1)))
  4957. (re (concat "^" org-outline-regexp))
  4958. (start (1+ (or (match-beginning 2) -1))))
  4959. (if (not start-level)
  4960. (progn
  4961. nil) ;; does not even start with a heading
  4962. (catch 'exit
  4963. (while (setq start (string-match re kill (1+ start)))
  4964. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4965. (throw 'exit nil)))
  4966. t))))
  4967. (defvar org-markers-to-move nil
  4968. "Markers that should be moved with a cut-and-paste operation.
  4969. Those markers are stored together with their positions relative to
  4970. the start of the region.")
  4971. (defun org-save-markers-in-region (beg end)
  4972. "Check markers in region.
  4973. If these markers are between BEG and END, record their position relative
  4974. to BEG, so that after moving the block of text, we can put the markers back
  4975. into place.
  4976. This function gets called just before an entry or tree gets cut from the
  4977. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4978. called immediately, to move the markers with the entries."
  4979. (setq org-markers-to-move nil)
  4980. (when (featurep 'org-clock)
  4981. (org-clock-save-markers-for-cut-and-paste beg end))
  4982. (when (featurep 'org-agenda)
  4983. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4984. (defun org-check-and-save-marker (marker beg end)
  4985. "Check if MARKER is between BEG and END.
  4986. If yes, remember the marker and the distance to BEG."
  4987. (when (and (marker-buffer marker)
  4988. (equal (marker-buffer marker) (current-buffer)))
  4989. (if (and (>= marker beg) (< marker end))
  4990. (push (cons marker (- marker beg)) org-markers-to-move))))
  4991. (defun org-reinstall-markers-in-region (beg)
  4992. "Move all remembered markers to their position relative to BEG."
  4993. (mapc (lambda (x)
  4994. (move-marker (car x) (+ beg (cdr x))))
  4995. org-markers-to-move)
  4996. (setq org-markers-to-move nil))
  4997. (defun org-narrow-to-subtree ()
  4998. "Narrow buffer to the current subtree."
  4999. (interactive)
  5000. (save-excursion
  5001. (save-match-data
  5002. (narrow-to-region
  5003. (progn (org-back-to-heading) (point))
  5004. (progn (org-end-of-subtree t) (point))))))
  5005. ;;; Outline Sorting
  5006. (defun org-sort (with-case)
  5007. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5008. Optional argument WITH-CASE means sort case-sensitively."
  5009. (interactive "P")
  5010. (if (org-at-table-p)
  5011. (org-call-with-arg 'org-table-sort-lines with-case)
  5012. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5013. (defun org-sort-remove-invisible (s)
  5014. (remove-text-properties 0 (length s) org-rm-props s)
  5015. (while (string-match org-bracket-link-regexp s)
  5016. (setq s (replace-match (if (match-end 2)
  5017. (match-string 3 s)
  5018. (match-string 1 s)) t t s)))
  5019. s)
  5020. (defvar org-priority-regexp) ; defined later in the file
  5021. (defun org-sort-entries-or-items
  5022. (&optional with-case sorting-type getkey-func compare-func property)
  5023. "Sort entries on a certain level of an outline tree.
  5024. If there is an active region, the entries in the region are sorted.
  5025. Else, if the cursor is before the first entry, sort the top-level items.
  5026. Else, the children of the entry at point are sorted.
  5027. Sorting can be alphabetically, numerically, and by date/time as given by
  5028. the first time stamp in the entry. The command prompts for the sorting
  5029. type unless it has been given to the function through the SORTING-TYPE
  5030. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5031. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5032. called with point at the beginning of the record. It must return either
  5033. a string or a number that should serve as the sorting key for that record.
  5034. Comparing entries ignores case by default. However, with an optional argument
  5035. WITH-CASE, the sorting considers case as well."
  5036. (interactive "P")
  5037. (let ((case-func (if with-case 'identity 'downcase))
  5038. start beg end stars re re2
  5039. txt what tmp plain-list-p)
  5040. ;; Find beginning and end of region to sort
  5041. (cond
  5042. ((org-region-active-p)
  5043. ;; we will sort the region
  5044. (setq end (region-end)
  5045. what "region")
  5046. (goto-char (region-beginning))
  5047. (if (not (org-on-heading-p)) (outline-next-heading))
  5048. (setq start (point)))
  5049. ((org-at-item-p)
  5050. ;; we will sort this plain list
  5051. (org-beginning-of-item-list) (setq start (point))
  5052. (org-end-of-item-list) (setq end (point))
  5053. (goto-char start)
  5054. (setq plain-list-p t
  5055. what "plain list"))
  5056. ((or (org-on-heading-p)
  5057. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5058. ;; we will sort the children of the current headline
  5059. (org-back-to-heading)
  5060. (setq start (point)
  5061. end (progn (org-end-of-subtree t t)
  5062. (org-back-over-empty-lines)
  5063. (point))
  5064. what "children")
  5065. (goto-char start)
  5066. (show-subtree)
  5067. (outline-next-heading))
  5068. (t
  5069. ;; we will sort the top-level entries in this file
  5070. (goto-char (point-min))
  5071. (or (org-on-heading-p) (outline-next-heading))
  5072. (setq start (point) end (point-max) what "top-level")
  5073. (goto-char start)
  5074. (show-all)))
  5075. (setq beg (point))
  5076. (if (>= beg end) (error "Nothing to sort"))
  5077. (unless plain-list-p
  5078. (looking-at "\\(\\*+\\)")
  5079. (setq stars (match-string 1)
  5080. re (concat "^" (regexp-quote stars) " +")
  5081. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5082. txt (buffer-substring beg end))
  5083. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5084. (if (and (not (equal stars "*")) (string-match re2 txt))
  5085. (error "Region to sort contains a level above the first entry")))
  5086. (unless sorting-type
  5087. (message
  5088. (if plain-list-p
  5089. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5090. "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:")
  5091. what)
  5092. (setq sorting-type (read-char-exclusive))
  5093. (and (= (downcase sorting-type) ?f)
  5094. (setq getkey-func
  5095. (org-ido-completing-read "Sort using function: "
  5096. obarray 'fboundp t nil nil))
  5097. (setq getkey-func (intern getkey-func)))
  5098. (and (= (downcase sorting-type) ?r)
  5099. (setq property
  5100. (org-ido-completing-read "Property: "
  5101. (mapcar 'list (org-buffer-property-keys t))
  5102. nil t))))
  5103. (message "Sorting entries...")
  5104. (save-restriction
  5105. (narrow-to-region start end)
  5106. (let ((dcst (downcase sorting-type))
  5107. (now (current-time)))
  5108. (sort-subr
  5109. (/= dcst sorting-type)
  5110. ;; This function moves to the beginning character of the "record" to
  5111. ;; be sorted.
  5112. (if plain-list-p
  5113. (lambda nil
  5114. (if (org-at-item-p) t (goto-char (point-max))))
  5115. (lambda nil
  5116. (if (re-search-forward re nil t)
  5117. (goto-char (match-beginning 0))
  5118. (goto-char (point-max)))))
  5119. ;; This function moves to the last character of the "record" being
  5120. ;; sorted.
  5121. (if plain-list-p
  5122. 'org-end-of-item
  5123. (lambda nil
  5124. (save-match-data
  5125. (condition-case nil
  5126. (outline-forward-same-level 1)
  5127. (error
  5128. (goto-char (point-max)))))))
  5129. ;; This function returns the value that gets sorted against.
  5130. (if plain-list-p
  5131. (lambda nil
  5132. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5133. (cond
  5134. ((= dcst ?n)
  5135. (string-to-number (buffer-substring (match-end 0)
  5136. (point-at-eol))))
  5137. ((= dcst ?a)
  5138. (buffer-substring (match-end 0) (point-at-eol)))
  5139. ((= dcst ?t)
  5140. (if (re-search-forward org-ts-regexp
  5141. (point-at-eol) t)
  5142. (org-time-string-to-time (match-string 0))
  5143. now))
  5144. ((= dcst ?f)
  5145. (if getkey-func
  5146. (progn
  5147. (setq tmp (funcall getkey-func))
  5148. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5149. tmp)
  5150. (error "Invalid key function `%s'" getkey-func)))
  5151. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5152. (lambda nil
  5153. (cond
  5154. ((= dcst ?n)
  5155. (if (looking-at org-complex-heading-regexp)
  5156. (string-to-number (match-string 4))
  5157. nil))
  5158. ((= dcst ?a)
  5159. (if (looking-at org-complex-heading-regexp)
  5160. (funcall case-func (match-string 4))
  5161. nil))
  5162. ((= dcst ?t)
  5163. (if (re-search-forward org-ts-regexp
  5164. (save-excursion
  5165. (forward-line 2)
  5166. (point)) t)
  5167. (org-time-string-to-time (match-string 0))
  5168. now))
  5169. ((= dcst ?p)
  5170. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5171. (string-to-char (match-string 2))
  5172. org-default-priority))
  5173. ((= dcst ?r)
  5174. (or (org-entry-get nil property) ""))
  5175. ((= dcst ?o)
  5176. (if (looking-at org-complex-heading-regexp)
  5177. (- 9999 (length (member (match-string 2)
  5178. org-todo-keywords-1)))))
  5179. ((= dcst ?f)
  5180. (if getkey-func
  5181. (progn
  5182. (setq tmp (funcall getkey-func))
  5183. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5184. tmp)
  5185. (error "Invalid key function `%s'" getkey-func)))
  5186. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5187. nil
  5188. (cond
  5189. ((= dcst ?a) 'string<)
  5190. ((= dcst ?t) 'time-less-p)
  5191. ((= dcst ?f) compare-func)
  5192. (t nil)))))
  5193. (message "Sorting entries...done")))
  5194. (defun org-do-sort (table what &optional with-case sorting-type)
  5195. "Sort TABLE of WHAT according to SORTING-TYPE.
  5196. The user will be prompted for the SORTING-TYPE if the call to this
  5197. function does not specify it. WHAT is only for the prompt, to indicate
  5198. what is being sorted. The sorting key will be extracted from
  5199. the car of the elements of the table.
  5200. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5201. (unless sorting-type
  5202. (message
  5203. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5204. what)
  5205. (setq sorting-type (read-char-exclusive)))
  5206. (let ((dcst (downcase sorting-type))
  5207. extractfun comparefun)
  5208. ;; Define the appropriate functions
  5209. (cond
  5210. ((= dcst ?n)
  5211. (setq extractfun 'string-to-number
  5212. comparefun (if (= dcst sorting-type) '< '>)))
  5213. ((= dcst ?a)
  5214. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5215. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5216. comparefun (if (= dcst sorting-type)
  5217. 'string<
  5218. (lambda (a b) (and (not (string< a b))
  5219. (not (string= a b)))))))
  5220. ((= dcst ?t)
  5221. (setq extractfun
  5222. (lambda (x)
  5223. (if (string-match org-ts-regexp x)
  5224. (time-to-seconds
  5225. (org-time-string-to-time (match-string 0 x)))
  5226. 0))
  5227. comparefun (if (= dcst sorting-type) '< '>)))
  5228. (t (error "Invalid sorting type `%c'" sorting-type)))
  5229. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5230. table)
  5231. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5232. ;;; Editing source examples
  5233. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5234. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5235. (defvar org-edit-src-force-single-line nil)
  5236. (defvar org-edit-src-from-org-mode nil)
  5237. (defvar org-edit-src-picture nil)
  5238. (define-minor-mode org-exit-edit-mode
  5239. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5240. (defun org-edit-src-code ()
  5241. "Edit the source code example at point.
  5242. An indirect buffer is created, and that buffer is then narrowed to the
  5243. example at point and switched to the correct language mode. When done,
  5244. exit by killing the buffer with \\[org-edit-src-exit]."
  5245. (interactive)
  5246. (let ((line (org-current-line))
  5247. (case-fold-search t)
  5248. (msg (substitute-command-keys
  5249. "Edit, then exit with C-c ' (C-c and single quote)"))
  5250. (info (org-edit-src-find-region-and-lang))
  5251. (org-mode-p (eq major-mode 'org-mode))
  5252. beg end lang lang-f single lfmt)
  5253. (if (not info)
  5254. nil
  5255. (setq beg (nth 0 info)
  5256. end (nth 1 info)
  5257. lang (nth 2 info)
  5258. single (nth 3 info)
  5259. lfmt (nth 4 info)
  5260. lang-f (intern (concat lang "-mode")))
  5261. (unless (functionp lang-f)
  5262. (error "No such language mode: %s" lang-f))
  5263. (goto-line line)
  5264. (if (get-buffer "*Org Edit Src Example*")
  5265. (kill-buffer "*Org Edit Src Example*"))
  5266. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5267. "*Org Edit Src Example*"))
  5268. (narrow-to-region beg end)
  5269. (remove-text-properties beg end '(display nil invisible nil
  5270. intangible nil))
  5271. (let ((org-inhibit-startup t))
  5272. (funcall lang-f))
  5273. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5274. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5275. (when lfmt
  5276. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5277. (when org-mode-p
  5278. (goto-char (point-min))
  5279. (while (re-search-forward "^," nil t)
  5280. (replace-match "")))
  5281. (goto-line line)
  5282. (org-exit-edit-mode)
  5283. (org-set-local 'header-line-format msg)
  5284. (message "%s" msg)
  5285. t)))
  5286. (defun org-edit-fixed-width-region ()
  5287. "Edit the fixed-width ascii drawing at point.
  5288. This must be a region where each line starts with a colon followed by
  5289. a space character.
  5290. An indirect buffer is created, and that buffer is then narrowed to the
  5291. example at point and switched to artist-mode. When done,
  5292. exit by killing the buffer with \\[org-edit-src-exit]."
  5293. (interactive)
  5294. (let ((line (org-current-line))
  5295. (case-fold-search t)
  5296. (msg (substitute-command-keys
  5297. "Edit, then exit with C-c ' (C-c and single quote)"))
  5298. (org-mode-p (eq major-mode 'org-mode))
  5299. beg end)
  5300. (beginning-of-line 1)
  5301. (if (looking-at "[ \t]*[^:\n \t]")
  5302. nil
  5303. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5304. (setq beg (point) end beg)
  5305. (save-excursion
  5306. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5307. (setq beg (point-at-bol 2))
  5308. (setq beg (point))))
  5309. (save-excursion
  5310. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5311. (setq end (1- (match-beginning 0)))
  5312. (setq end (point))))
  5313. (goto-line line))
  5314. (if (get-buffer "*Org Edit Picture*")
  5315. (kill-buffer "*Org Edit Picture*"))
  5316. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5317. "*Org Edit Picture*"))
  5318. (narrow-to-region beg end)
  5319. (remove-text-properties beg end '(display nil invisible nil
  5320. intangible nil))
  5321. (when (fboundp 'font-lock-unfontify-region)
  5322. (font-lock-unfontify-region (point-min) (point-max)))
  5323. (cond
  5324. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5325. (fundamental-mode)
  5326. (artist-mode 1))
  5327. (t (funcall org-edit-fixed-width-region-mode)))
  5328. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5329. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5330. (set (make-local-variable 'org-edit-src-picture) t)
  5331. (goto-char (point-min))
  5332. (while (re-search-forward "^[ \t]*: ?" nil t)
  5333. (replace-match ""))
  5334. (goto-line line)
  5335. (org-exit-edit-mode)
  5336. (org-set-local 'header-line-format msg)
  5337. (message "%s" msg)
  5338. t)))
  5339. (defun org-edit-src-find-region-and-lang ()
  5340. "Find the region and language for a local edit.
  5341. Return a list with beginning and end of the region, a string representing
  5342. the language, a switch telling of the content should be in a single line."
  5343. (let ((re-list
  5344. (append
  5345. org-edit-src-region-extra
  5346. '(
  5347. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5348. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5349. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5350. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5351. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5352. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5353. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5354. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5355. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5356. ("^#\\+html:" "\n" "html" single-line)
  5357. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5358. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5359. ("^#\\+latex:" "\n" "latex" single-line)
  5360. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5361. ("^#\\+ascii:" "\n" "ascii" single-line)
  5362. )))
  5363. (pos (point))
  5364. re1 re2 single beg end lang lfmt match-re1)
  5365. (catch 'exit
  5366. (while (setq entry (pop re-list))
  5367. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5368. single (nth 3 entry))
  5369. (save-excursion
  5370. (if (or (looking-at re1)
  5371. (re-search-backward re1 nil t))
  5372. (progn
  5373. (setq match-re1 (match-string 0))
  5374. (setq beg (match-end 0)
  5375. lang (org-edit-src-get-lang lang)
  5376. lfmt (org-edit-src-get-label-format match-re1))
  5377. (if (and (re-search-forward re2 nil t)
  5378. (>= (match-end 0) pos))
  5379. (throw 'exit (list beg (match-beginning 0)
  5380. lang single lfmt))))
  5381. (if (or (looking-at re2)
  5382. (re-search-forward re2 nil t))
  5383. (progn
  5384. (setq end (match-beginning 0))
  5385. (if (and (re-search-backward re1 nil t)
  5386. (<= (match-beginning 0) pos))
  5387. (progn
  5388. (setq lfmt (org-edit-src-get-label-format
  5389. (match-string 0)))
  5390. (throw 'exit
  5391. (list (match-end 0) end
  5392. (org-edit-src-get-lang lang)
  5393. single lfmt))))))))))))
  5394. (defun org-edit-src-get-lang (lang)
  5395. "Extract the src language."
  5396. (let ((m (match-string 0)))
  5397. (cond
  5398. ((stringp lang) lang)
  5399. ((integerp lang) (match-string lang))
  5400. ((and (eq lang 'lang)
  5401. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5402. (match-string 1 m))
  5403. ((and (eq lang 'style)
  5404. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5405. (match-string 1 m))
  5406. (t "fundamental"))))
  5407. (defun org-edit-src-get-label-format (s)
  5408. "Extract the label format."
  5409. (save-match-data
  5410. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5411. (match-string 1 s))))
  5412. (defun org-edit-src-exit ()
  5413. "Exit special edit and protect problematic lines."
  5414. (interactive)
  5415. (unless (buffer-base-buffer (current-buffer))
  5416. (error "This is not an indirect buffer, something is wrong..."))
  5417. (unless (> (point-min) 1)
  5418. (error "This buffer is not narrowed, something is wrong..."))
  5419. (goto-char (point-min))
  5420. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5421. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5422. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5423. (goto-char (point-min))
  5424. (while (re-search-forward "\n" nil t)
  5425. (replace-match " "))
  5426. (goto-char (point-min))
  5427. (if (looking-at "\\s-*") (replace-match " "))
  5428. (if (re-search-forward "\\s-+\\'" nil t)
  5429. (replace-match "")))
  5430. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5431. (goto-char (point-min))
  5432. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5433. (replace-match ",\\1"))
  5434. (when font-lock-mode
  5435. (font-lock-unfontify-region (point-min) (point-max)))
  5436. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5437. (when (org-bound-and-true-p org-edit-src-picture)
  5438. (untabify (point-min) (point-max))
  5439. (goto-char (point-min))
  5440. (while (re-search-forward "^" nil t)
  5441. (replace-match ": "))
  5442. (when font-lock-mode
  5443. (font-lock-unfontify-region (point-min) (point-max)))
  5444. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5445. (kill-buffer (current-buffer))
  5446. (and (org-mode-p) (org-restart-font-lock)))
  5447. ;;; The orgstruct minor mode
  5448. ;; Define a minor mode which can be used in other modes in order to
  5449. ;; integrate the org-mode structure editing commands.
  5450. ;; This is really a hack, because the org-mode structure commands use
  5451. ;; keys which normally belong to the major mode. Here is how it
  5452. ;; works: The minor mode defines all the keys necessary to operate the
  5453. ;; structure commands, but wraps the commands into a function which
  5454. ;; tests if the cursor is currently at a headline or a plain list
  5455. ;; item. If that is the case, the structure command is used,
  5456. ;; temporarily setting many Org-mode variables like regular
  5457. ;; expressions for filling etc. However, when any of those keys is
  5458. ;; used at a different location, function uses `key-binding' to look
  5459. ;; up if the key has an associated command in another currently active
  5460. ;; keymap (minor modes, major mode, global), and executes that
  5461. ;; command. There might be problems if any of the keys is otherwise
  5462. ;; used as a prefix key.
  5463. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5464. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5465. ;; addresses this by checking explicitly for both bindings.
  5466. (defvar orgstruct-mode-map (make-sparse-keymap)
  5467. "Keymap for the minor `orgstruct-mode'.")
  5468. (defvar org-local-vars nil
  5469. "List of local variables, for use by `orgstruct-mode'")
  5470. ;;;###autoload
  5471. (define-minor-mode orgstruct-mode
  5472. "Toggle the minor more `orgstruct-mode'.
  5473. This mode is for using Org-mode structure commands in other modes.
  5474. The following key behave as if Org-mode was active, if the cursor
  5475. is on a headline, or on a plain list item (both in the definition
  5476. of Org-mode).
  5477. M-up Move entry/item up
  5478. M-down Move entry/item down
  5479. M-left Promote
  5480. M-right Demote
  5481. M-S-up Move entry/item up
  5482. M-S-down Move entry/item down
  5483. M-S-left Promote subtree
  5484. M-S-right Demote subtree
  5485. M-q Fill paragraph and items like in Org-mode
  5486. C-c ^ Sort entries
  5487. C-c - Cycle list bullet
  5488. TAB Cycle item visibility
  5489. M-RET Insert new heading/item
  5490. S-M-RET Insert new TODO heading / Checkbox item
  5491. C-c C-c Set tags / toggle checkbox"
  5492. nil " OrgStruct" nil
  5493. (org-load-modules-maybe)
  5494. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5495. ;;;###autoload
  5496. (defun turn-on-orgstruct ()
  5497. "Unconditionally turn on `orgstruct-mode'."
  5498. (orgstruct-mode 1))
  5499. ;;;###autoload
  5500. (defun turn-on-orgstruct++ ()
  5501. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5502. In addition to setting orgstruct-mode, this also exports all indentation and
  5503. autofilling variables from org-mode into the buffer. Note that turning
  5504. off orgstruct-mode will *not* remove these additional settings."
  5505. (orgstruct-mode 1)
  5506. (let (var val)
  5507. (mapc
  5508. (lambda (x)
  5509. (when (string-match
  5510. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5511. (symbol-name (car x)))
  5512. (setq var (car x) val (nth 1 x))
  5513. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5514. org-local-vars)))
  5515. (defun orgstruct-error ()
  5516. "Error when there is no default binding for a structure key."
  5517. (interactive)
  5518. (error "This key has no function outside structure elements"))
  5519. (defun orgstruct-setup ()
  5520. "Setup orgstruct keymaps."
  5521. (let ((nfunc 0)
  5522. (bindings
  5523. (list
  5524. '([(meta up)] org-metaup)
  5525. '([(meta down)] org-metadown)
  5526. '([(meta left)] org-metaleft)
  5527. '([(meta right)] org-metaright)
  5528. '([(meta shift up)] org-shiftmetaup)
  5529. '([(meta shift down)] org-shiftmetadown)
  5530. '([(meta shift left)] org-shiftmetaleft)
  5531. '([(meta shift right)] org-shiftmetaright)
  5532. '([(shift up)] org-shiftup)
  5533. '([(shift down)] org-shiftdown)
  5534. '([(shift left)] org-shiftleft)
  5535. '([(shift right)] org-shiftright)
  5536. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5537. '("\M-q" fill-paragraph)
  5538. '("\C-c^" org-sort)
  5539. '("\C-c-" org-cycle-list-bullet)))
  5540. elt key fun cmd)
  5541. (while (setq elt (pop bindings))
  5542. (setq nfunc (1+ nfunc))
  5543. (setq key (org-key (car elt))
  5544. fun (nth 1 elt)
  5545. cmd (orgstruct-make-binding fun nfunc key))
  5546. (org-defkey orgstruct-mode-map key cmd))
  5547. ;; Special treatment needed for TAB and RET
  5548. (org-defkey orgstruct-mode-map [(tab)]
  5549. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5550. (org-defkey orgstruct-mode-map "\C-i"
  5551. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5552. (org-defkey orgstruct-mode-map "\M-\C-m"
  5553. (orgstruct-make-binding 'org-insert-heading 105
  5554. "\M-\C-m" [(meta return)]))
  5555. (org-defkey orgstruct-mode-map [(meta return)]
  5556. (orgstruct-make-binding 'org-insert-heading 106
  5557. [(meta return)] "\M-\C-m"))
  5558. (org-defkey orgstruct-mode-map [(shift meta return)]
  5559. (orgstruct-make-binding 'org-insert-todo-heading 107
  5560. [(meta return)] "\M-\C-m"))
  5561. (unless org-local-vars
  5562. (setq org-local-vars (org-get-local-variables)))
  5563. t))
  5564. (defun orgstruct-make-binding (fun n &rest keys)
  5565. "Create a function for binding in the structure minor mode.
  5566. FUN is the command to call inside a table. N is used to create a unique
  5567. command name. KEYS are keys that should be checked in for a command
  5568. to execute outside of tables."
  5569. (eval
  5570. (list 'defun
  5571. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5572. '(arg)
  5573. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5574. "Outside of structure, run the binding of `"
  5575. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5576. "'.")
  5577. '(interactive "p")
  5578. (list 'if
  5579. '(org-context-p 'headline 'item)
  5580. (list 'org-run-like-in-org-mode (list 'quote fun))
  5581. (list 'let '(orgstruct-mode)
  5582. (list 'call-interactively
  5583. (append '(or)
  5584. (mapcar (lambda (k)
  5585. (list 'key-binding k))
  5586. keys)
  5587. '('orgstruct-error))))))))
  5588. (defun org-context-p (&rest contexts)
  5589. "Check if local context is any of CONTEXTS.
  5590. Possible values in the list of contexts are `table', `headline', and `item'."
  5591. (let ((pos (point)))
  5592. (goto-char (point-at-bol))
  5593. (prog1 (or (and (memq 'table contexts)
  5594. (looking-at "[ \t]*|"))
  5595. (and (memq 'headline contexts)
  5596. ;;????????? (looking-at "\\*+"))
  5597. (looking-at outline-regexp))
  5598. (and (memq 'item contexts)
  5599. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5600. (goto-char pos))))
  5601. (defun org-get-local-variables ()
  5602. "Return a list of all local variables in an org-mode buffer."
  5603. (let (varlist)
  5604. (with-current-buffer (get-buffer-create "*Org tmp*")
  5605. (erase-buffer)
  5606. (org-mode)
  5607. (setq varlist (buffer-local-variables)))
  5608. (kill-buffer "*Org tmp*")
  5609. (delq nil
  5610. (mapcar
  5611. (lambda (x)
  5612. (setq x
  5613. (if (symbolp x)
  5614. (list x)
  5615. (list (car x) (list 'quote (cdr x)))))
  5616. (if (string-match
  5617. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5618. (symbol-name (car x)))
  5619. x nil))
  5620. varlist))))
  5621. ;;;###autoload
  5622. (defun org-run-like-in-org-mode (cmd)
  5623. (org-load-modules-maybe)
  5624. (unless org-local-vars
  5625. (setq org-local-vars (org-get-local-variables)))
  5626. (eval (list 'let org-local-vars
  5627. (list 'call-interactively (list 'quote cmd)))))
  5628. ;;;; Archiving
  5629. (defun org-get-category (&optional pos)
  5630. "Get the category applying to position POS."
  5631. (get-text-property (or pos (point)) 'org-category))
  5632. (defun org-refresh-category-properties ()
  5633. "Refresh category text properties in the buffer."
  5634. (let ((def-cat (cond
  5635. ((null org-category)
  5636. (if buffer-file-name
  5637. (file-name-sans-extension
  5638. (file-name-nondirectory buffer-file-name))
  5639. "???"))
  5640. ((symbolp org-category) (symbol-name org-category))
  5641. (t org-category)))
  5642. beg end cat pos optionp)
  5643. (org-unmodified
  5644. (save-excursion
  5645. (save-restriction
  5646. (widen)
  5647. (goto-char (point-min))
  5648. (put-text-property (point) (point-max) 'org-category def-cat)
  5649. (while (re-search-forward
  5650. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5651. (setq pos (match-end 0)
  5652. optionp (equal (char-after (match-beginning 0)) ?#)
  5653. cat (org-trim (match-string 2)))
  5654. (if optionp
  5655. (setq beg (point-at-bol) end (point-max))
  5656. (org-back-to-heading t)
  5657. (setq beg (point) end (org-end-of-subtree t t)))
  5658. (put-text-property beg end 'org-category cat)
  5659. (goto-char pos)))))))
  5660. ;;;; Link Stuff
  5661. ;;; Link abbreviations
  5662. (defun org-link-expand-abbrev (link)
  5663. "Apply replacements as defined in `org-link-abbrev-alist."
  5664. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5665. (let* ((key (match-string 1 link))
  5666. (as (or (assoc key org-link-abbrev-alist-local)
  5667. (assoc key org-link-abbrev-alist)))
  5668. (tag (and (match-end 2) (match-string 3 link)))
  5669. rpl)
  5670. (if (not as)
  5671. link
  5672. (setq rpl (cdr as))
  5673. (cond
  5674. ((symbolp rpl) (funcall rpl tag))
  5675. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5676. ((string-match "%h" rpl)
  5677. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5678. (t (concat rpl tag)))))
  5679. link))
  5680. ;;; Storing and inserting links
  5681. (defvar org-insert-link-history nil
  5682. "Minibuffer history for links inserted with `org-insert-link'.")
  5683. (defvar org-stored-links nil
  5684. "Contains the links stored with `org-store-link'.")
  5685. (defvar org-store-link-plist nil
  5686. "Plist with info about the most recently link created with `org-store-link'.")
  5687. (defvar org-link-protocols nil
  5688. "Link protocols added to Org-mode using `org-add-link-type'.")
  5689. (defvar org-store-link-functions nil
  5690. "List of functions that are called to create and store a link.
  5691. Each function will be called in turn until one returns a non-nil
  5692. value. Each function should check if it is responsible for creating
  5693. this link (for example by looking at the major mode).
  5694. If not, it must exit and return nil.
  5695. If yes, it should return a non-nil value after a calling
  5696. `org-store-link-props' with a list of properties and values.
  5697. Special properties are:
  5698. :type The link prefix. like \"http\". This must be given.
  5699. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5700. This is obligatory as well.
  5701. :description Optional default description for the second pair
  5702. of brackets in an Org-mode link. The user can still change
  5703. this when inserting this link into an Org-mode buffer.
  5704. In addition to these, any additional properties can be specified
  5705. and then used in remember templates.")
  5706. (defun org-add-link-type (type &optional follow export)
  5707. "Add TYPE to the list of `org-link-types'.
  5708. Re-compute all regular expressions depending on `org-link-types'
  5709. FOLLOW and EXPORT are two functions.
  5710. FOLLOW should take the link path as the single argument and do whatever
  5711. is necessary to follow the link, for example find a file or display
  5712. a mail message.
  5713. EXPORT should format the link path for export to one of the export formats.
  5714. It should be a function accepting three arguments:
  5715. path the path of the link, the text after the prefix (like \"http:\")
  5716. desc the description of the link, if any, nil if there was no description
  5717. format the export format, a symbol like `html' or `latex'.
  5718. The function may use the FORMAT information to return different values
  5719. depending on the format. The return value will be put literally into
  5720. the exported file.
  5721. Org-mode has a built-in default for exporting links. If you are happy with
  5722. this default, there is no need to define an export function for the link
  5723. type. For a simple example of an export function, see `org-bbdb.el'."
  5724. (add-to-list 'org-link-types type t)
  5725. (org-make-link-regexps)
  5726. (if (assoc type org-link-protocols)
  5727. (setcdr (assoc type org-link-protocols) (list follow export))
  5728. (push (list type follow export) org-link-protocols)))
  5729. ;;;###autoload
  5730. (defun org-store-link (arg)
  5731. "\\<org-mode-map>Store an org-link to the current location.
  5732. This link is added to `org-stored-links' and can later be inserted
  5733. into an org-buffer with \\[org-insert-link].
  5734. For some link types, a prefix arg is interpreted:
  5735. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5736. For file links, arg negates `org-context-in-file-links'."
  5737. (interactive "P")
  5738. (org-load-modules-maybe)
  5739. (setq org-store-link-plist nil) ; reset
  5740. (let (link cpltxt desc description search txt)
  5741. (cond
  5742. ((run-hook-with-args-until-success 'org-store-link-functions)
  5743. (setq link (plist-get org-store-link-plist :link)
  5744. desc (or (plist-get org-store-link-plist :description) link)))
  5745. ((equal (buffer-name) "*Org Edit Src Example*")
  5746. (let (label gc)
  5747. (while (or (not label)
  5748. (save-excursion
  5749. (save-restriction
  5750. (widen)
  5751. (goto-char (point-min))
  5752. (re-search-forward
  5753. (regexp-quote (format org-coderef-label-format label))
  5754. nil t))))
  5755. (when label (message "Label exists already") (sit-for 2))
  5756. (setq label (read-string "Code line label: " label)))
  5757. (end-of-line 1)
  5758. (setq link (format org-coderef-label-format label))
  5759. (setq gc (- 79 (length link)))
  5760. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5761. (insert link)
  5762. (setq link (concat "(" label ")") desc nil)))
  5763. ((eq major-mode 'calendar-mode)
  5764. (let ((cd (calendar-cursor-to-date)))
  5765. (setq link
  5766. (format-time-string
  5767. (car org-time-stamp-formats)
  5768. (apply 'encode-time
  5769. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5770. nil nil nil))))
  5771. (org-store-link-props :type "calendar" :date cd)))
  5772. ((eq major-mode 'w3-mode)
  5773. (setq cpltxt (url-view-url t)
  5774. link (org-make-link cpltxt))
  5775. (org-store-link-props :type "w3" :url (url-view-url t)))
  5776. ((eq major-mode 'w3m-mode)
  5777. (setq cpltxt (or w3m-current-title w3m-current-url)
  5778. link (org-make-link w3m-current-url))
  5779. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5780. ((setq search (run-hook-with-args-until-success
  5781. 'org-create-file-search-functions))
  5782. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5783. "::" search))
  5784. (setq cpltxt (or description link)))
  5785. ((eq major-mode 'image-mode)
  5786. (setq cpltxt (concat "file:"
  5787. (abbreviate-file-name buffer-file-name))
  5788. link (org-make-link cpltxt))
  5789. (org-store-link-props :type "image" :file buffer-file-name))
  5790. ((eq major-mode 'dired-mode)
  5791. ;; link to the file in the current line
  5792. (setq cpltxt (concat "file:"
  5793. (abbreviate-file-name
  5794. (expand-file-name
  5795. (dired-get-filename nil t))))
  5796. link (org-make-link cpltxt)))
  5797. ((and buffer-file-name (org-mode-p))
  5798. (cond
  5799. ((org-in-regexp "<<\\(.*?\\)>>")
  5800. (setq cpltxt
  5801. (concat "file:"
  5802. (abbreviate-file-name buffer-file-name)
  5803. "::" (match-string 1))
  5804. link (org-make-link cpltxt)))
  5805. ((and (featurep 'org-id)
  5806. (or (eq org-link-to-org-use-id t)
  5807. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5808. (interactive-p))
  5809. (and org-link-to-org-use-id
  5810. (condition-case nil
  5811. (org-entry-get nil "ID")
  5812. (error nil)))))
  5813. ;; We can make a link using the ID.
  5814. (setq link (condition-case nil
  5815. (prog1 (org-id-store-link)
  5816. (setq desc (plist-get org-store-link-plist
  5817. :description)))
  5818. (error
  5819. ;; probably before first headline, link to file only
  5820. (concat "file:"
  5821. (abbreviate-file-name buffer-file-name))))))
  5822. (t
  5823. ;; Just link to current headline
  5824. (setq cpltxt (concat "file:"
  5825. (abbreviate-file-name buffer-file-name)))
  5826. ;; Add a context search string
  5827. (when (org-xor org-context-in-file-links arg)
  5828. (setq txt (cond
  5829. ((org-on-heading-p) nil)
  5830. ((org-region-active-p)
  5831. (buffer-substring (region-beginning) (region-end)))
  5832. (t nil)))
  5833. (when (or (null txt) (string-match "\\S-" txt))
  5834. (setq cpltxt
  5835. (concat cpltxt "::"
  5836. (condition-case nil
  5837. (org-make-org-heading-search-string txt)
  5838. (error "")))
  5839. desc "NONE")))
  5840. (if (string-match "::\\'" cpltxt)
  5841. (setq cpltxt (substring cpltxt 0 -2)))
  5842. (setq link (org-make-link cpltxt)))))
  5843. ((buffer-file-name (buffer-base-buffer))
  5844. ;; Just link to this file here.
  5845. (setq cpltxt (concat "file:"
  5846. (abbreviate-file-name
  5847. (buffer-file-name (buffer-base-buffer)))))
  5848. ;; Add a context string
  5849. (when (org-xor org-context-in-file-links arg)
  5850. (setq txt (if (org-region-active-p)
  5851. (buffer-substring (region-beginning) (region-end))
  5852. (buffer-substring (point-at-bol) (point-at-eol))))
  5853. ;; Only use search option if there is some text.
  5854. (when (string-match "\\S-" txt)
  5855. (setq cpltxt
  5856. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5857. desc "NONE")))
  5858. (setq link (org-make-link cpltxt)))
  5859. ((interactive-p)
  5860. (error "Cannot link to a buffer which is not visiting a file"))
  5861. (t (setq link nil)))
  5862. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5863. (setq link (or link cpltxt)
  5864. desc (or desc cpltxt))
  5865. (if (equal desc "NONE") (setq desc nil))
  5866. (if (and (interactive-p) link)
  5867. (progn
  5868. (setq org-stored-links
  5869. (cons (list link desc) org-stored-links))
  5870. (message "Stored: %s" (or desc link)))
  5871. (and link (org-make-link-string link desc)))))
  5872. (defun org-store-link-props (&rest plist)
  5873. "Store link properties, extract names and addresses."
  5874. (let (x adr)
  5875. (when (setq x (plist-get plist :from))
  5876. (setq adr (mail-extract-address-components x))
  5877. (setq plist (plist-put plist :fromname (car adr)))
  5878. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5879. (when (setq x (plist-get plist :to))
  5880. (setq adr (mail-extract-address-components x))
  5881. (setq plist (plist-put plist :toname (car adr)))
  5882. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5883. (let ((from (plist-get plist :from))
  5884. (to (plist-get plist :to)))
  5885. (when (and from to org-from-is-user-regexp)
  5886. (setq plist
  5887. (plist-put plist :fromto
  5888. (if (string-match org-from-is-user-regexp from)
  5889. (concat "to %t")
  5890. (concat "from %f"))))))
  5891. (setq org-store-link-plist plist))
  5892. (defun org-add-link-props (&rest plist)
  5893. "Add these properties to the link property list."
  5894. (let (key value)
  5895. (while plist
  5896. (setq key (pop plist) value (pop plist))
  5897. (setq org-store-link-plist
  5898. (plist-put org-store-link-plist key value)))))
  5899. (defun org-email-link-description (&optional fmt)
  5900. "Return the description part of an email link.
  5901. This takes information from `org-store-link-plist' and formats it
  5902. according to FMT (default from `org-email-link-description-format')."
  5903. (setq fmt (or fmt org-email-link-description-format))
  5904. (let* ((p org-store-link-plist)
  5905. (to (plist-get p :toaddress))
  5906. (from (plist-get p :fromaddress))
  5907. (table
  5908. (list
  5909. (cons "%c" (plist-get p :fromto))
  5910. (cons "%F" (plist-get p :from))
  5911. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5912. (cons "%T" (plist-get p :to))
  5913. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5914. (cons "%s" (plist-get p :subject))
  5915. (cons "%m" (plist-get p :message-id)))))
  5916. (when (string-match "%c" fmt)
  5917. ;; Check if the user wrote this message
  5918. (if (and org-from-is-user-regexp from to
  5919. (save-match-data (string-match org-from-is-user-regexp from)))
  5920. (setq fmt (replace-match "to %t" t t fmt))
  5921. (setq fmt (replace-match "from %f" t t fmt))))
  5922. (org-replace-escapes fmt table)))
  5923. (defun org-make-org-heading-search-string (&optional string heading)
  5924. "Make search string for STRING or current headline."
  5925. (interactive)
  5926. (let ((s (or string (org-get-heading))))
  5927. (unless (and string (not heading))
  5928. ;; We are using a headline, clean up garbage in there.
  5929. (if (string-match org-todo-regexp s)
  5930. (setq s (replace-match "" t t s)))
  5931. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5932. (setq s (replace-match "" t t s)))
  5933. (setq s (org-trim s))
  5934. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5935. org-comment-string "\\)") s)
  5936. (setq s (replace-match "" t t s)))
  5937. (while (string-match org-ts-regexp s)
  5938. (setq s (replace-match "" t t s))))
  5939. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5940. (setq s (replace-match " " t t s)))
  5941. (or string (setq s (concat "*" s))) ; Add * for headlines
  5942. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5943. (defun org-make-link (&rest strings)
  5944. "Concatenate STRINGS."
  5945. (apply 'concat strings))
  5946. (defun org-make-link-string (link &optional description)
  5947. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5948. (unless (string-match "\\S-" link)
  5949. (error "Empty link"))
  5950. (when (stringp description)
  5951. ;; Remove brackets from the description, they are fatal.
  5952. (while (string-match "\\[" description)
  5953. (setq description (replace-match "{" t t description)))
  5954. (while (string-match "\\]" description)
  5955. (setq description (replace-match "}" t t description))))
  5956. (when (equal (org-link-escape link) description)
  5957. ;; No description needed, it is identical
  5958. (setq description nil))
  5959. (when (and (not description)
  5960. (not (equal link (org-link-escape link))))
  5961. (setq description (org-extract-attributes link)))
  5962. (concat "[[" (org-link-escape link) "]"
  5963. (if description (concat "[" description "]") "")
  5964. "]"))
  5965. (defconst org-link-escape-chars
  5966. '((?\ . "%20")
  5967. (?\[ . "%5B")
  5968. (?\] . "%5D")
  5969. (?\340 . "%E0") ; `a
  5970. (?\342 . "%E2") ; ^a
  5971. (?\347 . "%E7") ; ,c
  5972. (?\350 . "%E8") ; `e
  5973. (?\351 . "%E9") ; 'e
  5974. (?\352 . "%EA") ; ^e
  5975. (?\356 . "%EE") ; ^i
  5976. (?\364 . "%F4") ; ^o
  5977. (?\371 . "%F9") ; `u
  5978. (?\373 . "%FB") ; ^u
  5979. (?\; . "%3B")
  5980. (?? . "%3F")
  5981. (?= . "%3D")
  5982. (?+ . "%2B")
  5983. )
  5984. "Association list of escapes for some characters problematic in links.
  5985. This is the list that is used for internal purposes.")
  5986. (defconst org-link-escape-chars-browser
  5987. '((?\ . "%20")) ; 32 for the SPC char
  5988. "Association list of escapes for some characters problematic in links.
  5989. This is the list that is used before handing over to the browser.")
  5990. (defun org-link-escape (text &optional table)
  5991. "Escape characters in TEXT that are problematic for links."
  5992. (setq table (or table org-link-escape-chars))
  5993. (when text
  5994. (let ((re (mapconcat (lambda (x) (regexp-quote
  5995. (char-to-string (car x))))
  5996. table "\\|")))
  5997. (while (string-match re text)
  5998. (setq text
  5999. (replace-match
  6000. (cdr (assoc (string-to-char (match-string 0 text))
  6001. table))
  6002. t t text)))
  6003. text)))
  6004. (defun org-link-unescape (text &optional table)
  6005. "Reverse the action of `org-link-escape'."
  6006. (setq table (or table org-link-escape-chars))
  6007. (when text
  6008. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6009. table "\\|")))
  6010. (while (string-match re text)
  6011. (setq text
  6012. (replace-match
  6013. (char-to-string (car (rassoc (match-string 0 text) table)))
  6014. t t text)))
  6015. text)))
  6016. (defun org-xor (a b)
  6017. "Exclusive or."
  6018. (if a (not b) b))
  6019. (defun org-fixup-message-id-for-http (s)
  6020. "Replace special characters in a message id, so it can be used in an http query."
  6021. (while (string-match "<" s)
  6022. (setq s (replace-match "%3C" t t s)))
  6023. (while (string-match ">" s)
  6024. (setq s (replace-match "%3E" t t s)))
  6025. (while (string-match "@" s)
  6026. (setq s (replace-match "%40" t t s)))
  6027. s)
  6028. ;;;###autoload
  6029. (defun org-insert-link-global ()
  6030. "Insert a link like Org-mode does.
  6031. This command can be called in any mode to insert a link in Org-mode syntax."
  6032. (interactive)
  6033. (org-load-modules-maybe)
  6034. (org-run-like-in-org-mode 'org-insert-link))
  6035. (defun org-insert-link (&optional complete-file link-location)
  6036. "Insert a link. At the prompt, enter the link.
  6037. Completion can be used to insert any of the link protocol prefixes like
  6038. http or ftp in use.
  6039. The history can be used to select a link previously stored with
  6040. `org-store-link'. When the empty string is entered (i.e. if you just
  6041. press RET at the prompt), the link defaults to the most recently
  6042. stored link. As SPC triggers completion in the minibuffer, you need to
  6043. use M-SPC or C-q SPC to force the insertion of a space character.
  6044. You will also be prompted for a description, and if one is given, it will
  6045. be displayed in the buffer instead of the link.
  6046. If there is already a link at point, this command will allow you to edit link
  6047. and description parts.
  6048. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6049. be selected using completion. The path to the file will be relative to the
  6050. current directory if the file is in the current directory or a subdirectory.
  6051. Otherwise, the link will be the absolute path as completed in the minibuffer
  6052. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6053. option `org-link-file-path-type'.
  6054. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6055. the current directory or below.
  6056. With three \\[universal-argument] prefixes, negate the meaning of
  6057. `org-keep-stored-link-after-insertion'.
  6058. If `org-make-link-description-function' is non-nil, this function will be
  6059. called with the link target, and the result will be the default
  6060. link description.
  6061. If the LINK-LOCATION parameter is non-nil, this value will be
  6062. used as the link location instead of reading one interactively."
  6063. (interactive "P")
  6064. (let* ((wcf (current-window-configuration))
  6065. (region (if (org-region-active-p)
  6066. (buffer-substring (region-beginning) (region-end))))
  6067. (remove (and region (list (region-beginning) (region-end))))
  6068. (desc region)
  6069. tmphist ; byte-compile incorrectly complains about this
  6070. (link link-location)
  6071. entry file)
  6072. (cond
  6073. (link-location) ; specified by arg, just use it.
  6074. ((org-in-regexp org-bracket-link-regexp 1)
  6075. ;; We do have a link at point, and we are going to edit it.
  6076. (setq remove (list (match-beginning 0) (match-end 0)))
  6077. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6078. (setq link (read-string "Link: "
  6079. (org-link-unescape
  6080. (org-match-string-no-properties 1)))))
  6081. ((or (org-in-regexp org-angle-link-re)
  6082. (org-in-regexp org-plain-link-re))
  6083. ;; Convert to bracket link
  6084. (setq remove (list (match-beginning 0) (match-end 0))
  6085. link (read-string "Link: "
  6086. (org-remove-angle-brackets (match-string 0)))))
  6087. ((member complete-file '((4) (16)))
  6088. ;; Completing read for file names.
  6089. (setq file (read-file-name "File: "))
  6090. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6091. (pwd1 (file-name-as-directory (abbreviate-file-name
  6092. (expand-file-name ".")))))
  6093. (cond
  6094. ((equal complete-file '(16))
  6095. (setq link (org-make-link
  6096. "file:"
  6097. (abbreviate-file-name (expand-file-name file)))))
  6098. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6099. (setq link (org-make-link "file:" (match-string 1 file))))
  6100. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6101. (expand-file-name file))
  6102. (setq link (org-make-link
  6103. "file:" (match-string 1 (expand-file-name file)))))
  6104. (t (setq link (org-make-link "file:" file))))))
  6105. (t
  6106. ;; Read link, with completion for stored links.
  6107. (with-output-to-temp-buffer "*Org Links*"
  6108. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6109. (when org-stored-links
  6110. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6111. (princ (mapconcat
  6112. (lambda (x)
  6113. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6114. (reverse org-stored-links) "\n"))))
  6115. (let ((cw (selected-window)))
  6116. (select-window (get-buffer-window "*Org Links*"))
  6117. (setq truncate-lines t)
  6118. (org-fit-window-to-buffer)
  6119. (select-window cw))
  6120. ;; Fake a link history, containing the stored links.
  6121. (setq tmphist (append (mapcar 'car org-stored-links)
  6122. org-insert-link-history))
  6123. (unwind-protect
  6124. (setq link
  6125. (let ((org-completion-use-ido nil))
  6126. (org-completing-read
  6127. "Link: "
  6128. (append
  6129. (mapcar (lambda (x) (list (concat (car x) ":")))
  6130. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6131. (mapcar (lambda (x) (list (concat x ":")))
  6132. org-link-types))
  6133. nil nil nil
  6134. 'tmphist
  6135. (or (car (car org-stored-links))))))
  6136. (set-window-configuration wcf)
  6137. (kill-buffer "*Org Links*"))
  6138. (setq entry (assoc link org-stored-links))
  6139. (or entry (push link org-insert-link-history))
  6140. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6141. (not org-keep-stored-link-after-insertion))
  6142. (setq org-stored-links (delq (assoc link org-stored-links)
  6143. org-stored-links)))
  6144. (setq desc (or desc (nth 1 entry)))))
  6145. (if (string-match org-plain-link-re link)
  6146. ;; URL-like link, normalize the use of angular brackets.
  6147. (setq link (org-make-link (org-remove-angle-brackets link))))
  6148. ;; Check if we are linking to the current file with a search option
  6149. ;; If yes, simplify the link by using only the search option.
  6150. (when (and buffer-file-name
  6151. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6152. (let* ((path (match-string 1 link))
  6153. (case-fold-search nil)
  6154. (search (match-string 2 link)))
  6155. (save-match-data
  6156. (if (equal (file-truename buffer-file-name) (file-truename path))
  6157. ;; We are linking to this same file, with a search option
  6158. (setq link search)))))
  6159. ;; Check if we can/should use a relative path. If yes, simplify the link
  6160. (when (string-match "^file:\\(.*\\)" link)
  6161. (let* ((path (match-string 1 link))
  6162. (origpath path)
  6163. (case-fold-search nil))
  6164. (cond
  6165. ((or (eq org-link-file-path-type 'absolute)
  6166. (equal complete-file '(16)))
  6167. (setq path (abbreviate-file-name (expand-file-name path))))
  6168. ((eq org-link-file-path-type 'noabbrev)
  6169. (setq path (expand-file-name path)))
  6170. ((eq org-link-file-path-type 'relative)
  6171. (setq path (file-relative-name path)))
  6172. (t
  6173. (save-match-data
  6174. (if (string-match (concat "^" (regexp-quote
  6175. (file-name-as-directory
  6176. (expand-file-name "."))))
  6177. (expand-file-name path))
  6178. ;; We are linking a file with relative path name.
  6179. (setq path (substring (expand-file-name path)
  6180. (match-end 0)))
  6181. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6182. (setq link (concat "file:" path))
  6183. (if (equal desc origpath)
  6184. (setq desc path))))
  6185. (if org-make-link-description-function
  6186. (setq desc (funcall org-make-link-description-function link desc)))
  6187. (setq desc (read-string "Description: " desc))
  6188. (unless (string-match "\\S-" desc) (setq desc nil))
  6189. (if remove (apply 'delete-region remove))
  6190. (insert (org-make-link-string link desc))))
  6191. (defun org-completing-read (&rest args)
  6192. "Completing-read with SPACE being a normal character."
  6193. (let ((minibuffer-local-completion-map
  6194. (copy-keymap minibuffer-local-completion-map)))
  6195. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6196. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6197. (apply 'org-ido-completing-read args)))
  6198. (defun org-ido-completing-read (&rest args)
  6199. "Completing-read using `ido-mode' speedups if available"
  6200. (if (and org-completion-use-ido
  6201. (fboundp 'ido-completing-read)
  6202. (boundp 'ido-mode) ido-mode
  6203. (listp (second args)))
  6204. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6205. (apply 'completing-read args)))
  6206. (defun org-extract-attributes (s)
  6207. "Extract the attributes cookie from a string and set as text property."
  6208. (let (a attr (start 0) key value)
  6209. (save-match-data
  6210. (when (string-match "{{\\([^}]+\\)}}$" s)
  6211. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6212. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6213. (setq key (match-string 1 a) value (match-string 2 a)
  6214. start (match-end 0)
  6215. attr (plist-put attr (intern key) value))))
  6216. (org-add-props s nil 'org-attr attr))
  6217. s))
  6218. (defun org-attributes-to-string (plist)
  6219. "Format a property list into an HTML attribute list."
  6220. (let ((s "") key value)
  6221. (while plist
  6222. (setq key (pop plist) value (pop plist))
  6223. (and value
  6224. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6225. s))
  6226. ;;; Opening/following a link
  6227. (defvar org-link-search-failed nil)
  6228. (defun org-next-link ()
  6229. "Move forward to the next link.
  6230. If the link is in hidden text, expose it."
  6231. (interactive)
  6232. (when (and org-link-search-failed (eq this-command last-command))
  6233. (goto-char (point-min))
  6234. (message "Link search wrapped back to beginning of buffer"))
  6235. (setq org-link-search-failed nil)
  6236. (let* ((pos (point))
  6237. (ct (org-context))
  6238. (a (assoc :link ct)))
  6239. (if a (goto-char (nth 2 a)))
  6240. (if (re-search-forward org-any-link-re nil t)
  6241. (progn
  6242. (goto-char (match-beginning 0))
  6243. (if (org-invisible-p) (org-show-context)))
  6244. (goto-char pos)
  6245. (setq org-link-search-failed t)
  6246. (error "No further link found"))))
  6247. (defun org-previous-link ()
  6248. "Move backward to the previous link.
  6249. If the link is in hidden text, expose it."
  6250. (interactive)
  6251. (when (and org-link-search-failed (eq this-command last-command))
  6252. (goto-char (point-max))
  6253. (message "Link search wrapped back to end of buffer"))
  6254. (setq org-link-search-failed nil)
  6255. (let* ((pos (point))
  6256. (ct (org-context))
  6257. (a (assoc :link ct)))
  6258. (if a (goto-char (nth 1 a)))
  6259. (if (re-search-backward org-any-link-re nil t)
  6260. (progn
  6261. (goto-char (match-beginning 0))
  6262. (if (org-invisible-p) (org-show-context)))
  6263. (goto-char pos)
  6264. (setq org-link-search-failed t)
  6265. (error "No further link found"))))
  6266. (defun org-translate-link (s)
  6267. "Translate a link string if a translation function has been defined."
  6268. (if (and org-link-translation-function
  6269. (fboundp org-link-translation-function)
  6270. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6271. (progn
  6272. (setq s (funcall org-link-translation-function
  6273. (match-string 1) (match-string 2)))
  6274. (concat (car s) ":" (cdr s)))
  6275. s))
  6276. (defun org-translate-link-from-planner (type path)
  6277. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6278. This is still an experimental function, your mileage may vary."
  6279. (cond
  6280. ((member type '("http" "https" "news" "ftp"))
  6281. ;; standard Internet links are the same.
  6282. nil)
  6283. ((and (equal type "irc") (string-match "^//" path))
  6284. ;; Planner has two / at the beginning of an irc link, we have 1.
  6285. ;; We should have zero, actually....
  6286. (setq path (substring path 1)))
  6287. ((and (equal type "lisp") (string-match "^/" path))
  6288. ;; Planner has a slash, we do not.
  6289. (setq type "elisp" path (substring path 1)))
  6290. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6291. ;; A typical message link. Planner has the id after the fina slash,
  6292. ;; we separate it with a hash mark
  6293. (setq path (concat (match-string 1 path) "#"
  6294. (org-remove-angle-brackets (match-string 2 path)))))
  6295. )
  6296. (cons type path))
  6297. (defun org-find-file-at-mouse (ev)
  6298. "Open file link or URL at mouse."
  6299. (interactive "e")
  6300. (mouse-set-point ev)
  6301. (org-open-at-point 'in-emacs))
  6302. (defun org-open-at-mouse (ev)
  6303. "Open file link or URL at mouse."
  6304. (interactive "e")
  6305. (mouse-set-point ev)
  6306. (if (eq major-mode 'org-agenda-mode)
  6307. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6308. (org-open-at-point))
  6309. (defvar org-window-config-before-follow-link nil
  6310. "The window configuration before following a link.
  6311. This is saved in case the need arises to restore it.")
  6312. (defvar org-open-link-marker (make-marker)
  6313. "Marker pointing to the location where `org-open-at-point; was called.")
  6314. ;;;###autoload
  6315. (defun org-open-at-point-global ()
  6316. "Follow a link like Org-mode does.
  6317. This command can be called in any mode to follow a link that has
  6318. Org-mode syntax."
  6319. (interactive)
  6320. (org-run-like-in-org-mode 'org-open-at-point))
  6321. ;;;###autoload
  6322. (defun org-open-link-from-string (s &optional arg)
  6323. "Open a link in the string S, as if it was in Org-mode."
  6324. (interactive "sLink: \nP")
  6325. (with-temp-buffer
  6326. (let ((org-inhibit-startup t))
  6327. (org-mode)
  6328. (insert s)
  6329. (goto-char (point-min))
  6330. (org-open-at-point arg))))
  6331. (defun org-open-at-point (&optional in-emacs)
  6332. "Open link at or after point.
  6333. If there is no link at point, this function will search forward up to
  6334. the end of the current subtree.
  6335. Normally, files will be opened by an appropriate application. If the
  6336. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6337. With a double prefix argument, try to open outside of Emacs, in the
  6338. application the system uses for this file type."
  6339. (interactive "P")
  6340. (org-load-modules-maybe)
  6341. (move-marker org-open-link-marker (point))
  6342. (setq org-window-config-before-follow-link (current-window-configuration))
  6343. (org-remove-occur-highlights nil nil t)
  6344. (cond
  6345. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6346. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6347. (org-footnote-action))
  6348. (t
  6349. (let (type path link line search (pos (point)))
  6350. (catch 'match
  6351. (save-excursion
  6352. (skip-chars-forward "^]\n\r")
  6353. (when (org-in-regexp org-bracket-link-regexp)
  6354. (setq link (org-extract-attributes
  6355. (org-link-unescape (org-match-string-no-properties 1))))
  6356. (while (string-match " *\n *" link)
  6357. (setq link (replace-match " " t t link)))
  6358. (setq link (org-link-expand-abbrev link))
  6359. (cond
  6360. ((or (file-name-absolute-p link)
  6361. (string-match "^\\.\\.?/" link))
  6362. (setq type "file" path link))
  6363. ((string-match org-link-re-with-space3 link)
  6364. (setq type (match-string 1 link) path (match-string 2 link)))
  6365. (t (setq type "thisfile" path link)))
  6366. (throw 'match t)))
  6367. (when (get-text-property (point) 'org-linked-text)
  6368. (setq type "thisfile"
  6369. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6370. (1+ (point)) (point))
  6371. path (buffer-substring
  6372. (previous-single-property-change pos 'org-linked-text)
  6373. (next-single-property-change pos 'org-linked-text)))
  6374. (throw 'match t))
  6375. (save-excursion
  6376. (when (or (org-in-regexp org-angle-link-re)
  6377. (org-in-regexp org-plain-link-re))
  6378. (setq type (match-string 1) path (match-string 2))
  6379. (throw 'match t)))
  6380. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6381. (setq type "tree-match"
  6382. path (match-string 1))
  6383. (throw 'match t))
  6384. (save-excursion
  6385. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6386. (setq type "tags"
  6387. path (match-string 1))
  6388. (while (string-match ":" path)
  6389. (setq path (replace-match "+" t t path)))
  6390. (throw 'match t))))
  6391. (unless path
  6392. (error "No link found"))
  6393. ;; Remove any trailing spaces in path
  6394. (if (string-match " +\\'" path)
  6395. (setq path (replace-match "" t t path)))
  6396. (if (and org-link-translation-function
  6397. (fboundp org-link-translation-function))
  6398. ;; Check if we need to translate the link
  6399. (let ((tmp (funcall org-link-translation-function type path)))
  6400. (setq type (car tmp) path (cdr tmp))))
  6401. (cond
  6402. ((assoc type org-link-protocols)
  6403. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6404. ((equal type "mailto")
  6405. (let ((cmd (car org-link-mailto-program))
  6406. (args (cdr org-link-mailto-program)) args1
  6407. (address path) (subject "") a)
  6408. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6409. (setq address (match-string 1 path)
  6410. subject (org-link-escape (match-string 2 path))))
  6411. (while args
  6412. (cond
  6413. ((not (stringp (car args))) (push (pop args) args1))
  6414. (t (setq a (pop args))
  6415. (if (string-match "%a" a)
  6416. (setq a (replace-match address t t a)))
  6417. (if (string-match "%s" a)
  6418. (setq a (replace-match subject t t a)))
  6419. (push a args1))))
  6420. (apply cmd (nreverse args1))))
  6421. ((member type '("http" "https" "ftp" "news"))
  6422. (browse-url (concat type ":" (org-link-escape
  6423. path org-link-escape-chars-browser))))
  6424. ((member type '("message"))
  6425. (browse-url (concat type ":" path)))
  6426. ((string= type "tags")
  6427. (org-tags-view in-emacs path))
  6428. ((string= type "thisfile")
  6429. (if in-emacs
  6430. (switch-to-buffer-other-window
  6431. (org-get-buffer-for-internal-link (current-buffer)))
  6432. (org-mark-ring-push))
  6433. (let ((cmd `(org-link-search
  6434. ,path
  6435. ,(cond ((equal in-emacs '(4)) 'occur)
  6436. ((equal in-emacs '(16)) 'org-occur)
  6437. (t nil))
  6438. ,pos)))
  6439. (condition-case nil (eval cmd)
  6440. (error (progn (widen) (eval cmd))))))
  6441. ((string= type "tree-match")
  6442. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6443. ((string= type "file")
  6444. (if (string-match "::\\([0-9]+\\)\\'" path)
  6445. (setq line (string-to-number (match-string 1 path))
  6446. path (substring path 0 (match-beginning 0)))
  6447. (if (string-match "::\\(.+\\)\\'" path)
  6448. (setq search (match-string 1 path)
  6449. path (substring path 0 (match-beginning 0)))))
  6450. (if (string-match "[*?{]" (file-name-nondirectory path))
  6451. (dired path)
  6452. (org-open-file path in-emacs line search)))
  6453. ((string= type "news")
  6454. (require 'org-gnus)
  6455. (org-gnus-follow-link path))
  6456. ((string= type "shell")
  6457. (let ((cmd path))
  6458. (if (or (not org-confirm-shell-link-function)
  6459. (funcall org-confirm-shell-link-function
  6460. (format "Execute \"%s\" in shell? "
  6461. (org-add-props cmd nil
  6462. 'face 'org-warning))))
  6463. (progn
  6464. (message "Executing %s" cmd)
  6465. (shell-command cmd))
  6466. (error "Abort"))))
  6467. ((string= type "elisp")
  6468. (let ((cmd path))
  6469. (if (or (not org-confirm-elisp-link-function)
  6470. (funcall org-confirm-elisp-link-function
  6471. (format "Execute \"%s\" as elisp? "
  6472. (org-add-props cmd nil
  6473. 'face 'org-warning))))
  6474. (message "%s => %s" cmd
  6475. (if (equal (string-to-char cmd) ?\()
  6476. (eval (read cmd))
  6477. (call-interactively (read cmd))))
  6478. (error "Abort"))))
  6479. (t
  6480. (browse-url-at-point))))))
  6481. (move-marker org-open-link-marker nil)
  6482. (run-hook-with-args 'org-follow-link-hook))
  6483. ;;;; Time estimates
  6484. (defun org-get-effort (&optional pom)
  6485. "Get the effort estimate for the current entry."
  6486. (org-entry-get pom org-effort-property))
  6487. ;;; File search
  6488. (defvar org-create-file-search-functions nil
  6489. "List of functions to construct the right search string for a file link.
  6490. These functions are called in turn with point at the location to
  6491. which the link should point.
  6492. A function in the hook should first test if it would like to
  6493. handle this file type, for example by checking the major-mode or
  6494. the file extension. If it decides not to handle this file, it
  6495. should just return nil to give other functions a chance. If it
  6496. does handle the file, it must return the search string to be used
  6497. when following the link. The search string will be part of the
  6498. file link, given after a double colon, and `org-open-at-point'
  6499. will automatically search for it. If special measures must be
  6500. taken to make the search successful, another function should be
  6501. added to the companion hook `org-execute-file-search-functions',
  6502. which see.
  6503. A function in this hook may also use `setq' to set the variable
  6504. `description' to provide a suggestion for the descriptive text to
  6505. be used for this link when it gets inserted into an Org-mode
  6506. buffer with \\[org-insert-link].")
  6507. (defvar org-execute-file-search-functions nil
  6508. "List of functions to execute a file search triggered by a link.
  6509. Functions added to this hook must accept a single argument, the
  6510. search string that was part of the file link, the part after the
  6511. double colon. The function must first check if it would like to
  6512. handle this search, for example by checking the major-mode or the
  6513. file extension. If it decides not to handle this search, it
  6514. should just return nil to give other functions a chance. If it
  6515. does handle the search, it must return a non-nil value to keep
  6516. other functions from trying.
  6517. Each function can access the current prefix argument through the
  6518. variable `current-prefix-argument'. Note that a single prefix is
  6519. used to force opening a link in Emacs, so it may be good to only
  6520. use a numeric or double prefix to guide the search function.
  6521. In case this is needed, a function in this hook can also restore
  6522. the window configuration before `org-open-at-point' was called using:
  6523. (set-window-configuration org-window-config-before-follow-link)")
  6524. (defun org-link-search (s &optional type avoid-pos)
  6525. "Search for a link search option.
  6526. If S is surrounded by forward slashes, it is interpreted as a
  6527. regular expression. In org-mode files, this will create an `org-occur'
  6528. sparse tree. In ordinary files, `occur' will be used to list matches.
  6529. If the current buffer is in `dired-mode', grep will be used to search
  6530. in all files. If AVOID-POS is given, ignore matches near that position."
  6531. (let ((case-fold-search t)
  6532. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6533. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6534. (append '(("") (" ") ("\t") ("\n"))
  6535. org-emphasis-alist)
  6536. "\\|") "\\)"))
  6537. (pos (point))
  6538. (pre nil) (post nil)
  6539. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6540. (cond
  6541. ;; First check if there are any special
  6542. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6543. ;; Now try the builtin stuff
  6544. ((save-excursion
  6545. (goto-char (point-min))
  6546. (and
  6547. (re-search-forward
  6548. (concat "<<" (regexp-quote s0) ">>") nil t)
  6549. (setq type 'dedicated
  6550. pos (match-beginning 0))))
  6551. ;; There is an exact target for this
  6552. (goto-char pos))
  6553. ((and (string-match "^(\\(.*\\))$" s0)
  6554. (save-excursion
  6555. (goto-char (point-min))
  6556. (and
  6557. (re-search-forward
  6558. (concat "[^[]" (regexp-quote
  6559. (format org-coderef-label-format
  6560. (match-string 1 s0))))
  6561. nil t)
  6562. (setq type 'dedicated
  6563. pos (1+ (match-beginning 0))))))
  6564. ;; There is a coderef target for this
  6565. (goto-char pos))
  6566. ((string-match "^/\\(.*\\)/$" s)
  6567. ;; A regular expression
  6568. (cond
  6569. ((org-mode-p)
  6570. (org-occur (match-string 1 s)))
  6571. ;;((eq major-mode 'dired-mode)
  6572. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6573. (t (org-do-occur (match-string 1 s)))))
  6574. (t
  6575. ;; A normal search strings
  6576. (when (equal (string-to-char s) ?*)
  6577. ;; Anchor on headlines, post may include tags.
  6578. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6579. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6580. s (substring s 1)))
  6581. (remove-text-properties
  6582. 0 (length s)
  6583. '(face nil mouse-face nil keymap nil fontified nil) s)
  6584. ;; Make a series of regular expressions to find a match
  6585. (setq words (org-split-string s "[ \n\r\t]+")
  6586. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6587. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6588. "\\)" markers)
  6589. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6590. re2a (concat "[ \t\r\n]" re2a_)
  6591. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6592. re4 (concat "[^a-zA-Z_]" re4_)
  6593. re1 (concat pre re2 post)
  6594. re3 (concat pre (if pre re4_ re4) post)
  6595. re5 (concat pre ".*" re4)
  6596. re2 (concat pre re2)
  6597. re2a (concat pre (if pre re2a_ re2a))
  6598. re4 (concat pre (if pre re4_ re4))
  6599. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6600. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6601. re5 "\\)"
  6602. ))
  6603. (cond
  6604. ((eq type 'org-occur) (org-occur reall))
  6605. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6606. (t (goto-char (point-min))
  6607. (setq type 'fuzzy)
  6608. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6609. (org-search-not-self 1 re1 nil t)
  6610. (org-search-not-self 1 re2 nil t)
  6611. (org-search-not-self 1 re2a nil t)
  6612. (org-search-not-self 1 re3 nil t)
  6613. (org-search-not-self 1 re4 nil t)
  6614. (org-search-not-self 1 re5 nil t)
  6615. )
  6616. (goto-char (match-beginning 1))
  6617. (goto-char pos)
  6618. (error "No match")))))
  6619. (t
  6620. ;; Normal string-search
  6621. (goto-char (point-min))
  6622. (if (search-forward s nil t)
  6623. (goto-char (match-beginning 0))
  6624. (error "No match"))))
  6625. (and (org-mode-p) (org-show-context 'link-search))
  6626. type))
  6627. (defun org-search-not-self (group &rest args)
  6628. "Execute `re-search-forward', but only accept matches that do not
  6629. enclose the position of `org-open-link-marker'."
  6630. (let ((m org-open-link-marker))
  6631. (catch 'exit
  6632. (while (apply 're-search-forward args)
  6633. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6634. (goto-char (match-end group))
  6635. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6636. (> (match-beginning 0) (marker-position m))
  6637. (< (match-end 0) (marker-position m)))
  6638. (save-match-data
  6639. (or (not (org-in-regexp
  6640. org-bracket-link-analytic-regexp 1))
  6641. (not (match-end 4)) ; no description
  6642. (and (<= (match-beginning 4) (point))
  6643. (>= (match-end 4) (point))))))
  6644. (throw 'exit (point))))))))
  6645. (defun org-get-buffer-for-internal-link (buffer)
  6646. "Return a buffer to be used for displaying the link target of internal links."
  6647. (cond
  6648. ((not org-display-internal-link-with-indirect-buffer)
  6649. buffer)
  6650. ((string-match "(Clone)$" (buffer-name buffer))
  6651. (message "Buffer is already a clone, not making another one")
  6652. ;; we also do not modify visibility in this case
  6653. buffer)
  6654. (t ; make a new indirect buffer for displaying the link
  6655. (let* ((bn (buffer-name buffer))
  6656. (ibn (concat bn "(Clone)"))
  6657. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6658. (with-current-buffer ib (org-overview))
  6659. ib))))
  6660. (defun org-do-occur (regexp &optional cleanup)
  6661. "Call the Emacs command `occur'.
  6662. If CLEANUP is non-nil, remove the printout of the regular expression
  6663. in the *Occur* buffer. This is useful if the regex is long and not useful
  6664. to read."
  6665. (occur regexp)
  6666. (when cleanup
  6667. (let ((cwin (selected-window)) win beg end)
  6668. (when (setq win (get-buffer-window "*Occur*"))
  6669. (select-window win))
  6670. (goto-char (point-min))
  6671. (when (re-search-forward "match[a-z]+" nil t)
  6672. (setq beg (match-end 0))
  6673. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6674. (setq end (1- (match-beginning 0)))))
  6675. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6676. (goto-char (point-min))
  6677. (select-window cwin))))
  6678. ;;; The mark ring for links jumps
  6679. (defvar org-mark-ring nil
  6680. "Mark ring for positions before jumps in Org-mode.")
  6681. (defvar org-mark-ring-last-goto nil
  6682. "Last position in the mark ring used to go back.")
  6683. ;; Fill and close the ring
  6684. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6685. (loop for i from 1 to org-mark-ring-length do
  6686. (push (make-marker) org-mark-ring))
  6687. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6688. org-mark-ring)
  6689. (defun org-mark-ring-push (&optional pos buffer)
  6690. "Put the current position or POS into the mark ring and rotate it."
  6691. (interactive)
  6692. (setq pos (or pos (point)))
  6693. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6694. (move-marker (car org-mark-ring)
  6695. (or pos (point))
  6696. (or buffer (current-buffer)))
  6697. (message "%s"
  6698. (substitute-command-keys
  6699. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6700. (defun org-mark-ring-goto (&optional n)
  6701. "Jump to the previous position in the mark ring.
  6702. With prefix arg N, jump back that many stored positions. When
  6703. called several times in succession, walk through the entire ring.
  6704. Org-mode commands jumping to a different position in the current file,
  6705. or to another Org-mode file, automatically push the old position
  6706. onto the ring."
  6707. (interactive "p")
  6708. (let (p m)
  6709. (if (eq last-command this-command)
  6710. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6711. (setq p org-mark-ring))
  6712. (setq org-mark-ring-last-goto p)
  6713. (setq m (car p))
  6714. (switch-to-buffer (marker-buffer m))
  6715. (goto-char m)
  6716. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6717. (defun org-remove-angle-brackets (s)
  6718. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6719. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6720. s)
  6721. (defun org-add-angle-brackets (s)
  6722. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6723. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6724. s)
  6725. (defun org-remove-double-quotes (s)
  6726. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6727. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6728. s)
  6729. ;;; Following specific links
  6730. (defun org-follow-timestamp-link ()
  6731. (cond
  6732. ((org-at-date-range-p t)
  6733. (let ((org-agenda-start-on-weekday)
  6734. (t1 (match-string 1))
  6735. (t2 (match-string 2)))
  6736. (setq t1 (time-to-days (org-time-string-to-time t1))
  6737. t2 (time-to-days (org-time-string-to-time t2)))
  6738. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6739. ((org-at-timestamp-p t)
  6740. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6741. (substring (match-string 1) 0 10)))
  6742. 1))
  6743. (t (error "This should not happen"))))
  6744. ;;; Following file links
  6745. (defvar org-wait nil)
  6746. (defun org-open-file (path &optional in-emacs line search)
  6747. "Open the file at PATH.
  6748. First, this expands any special file name abbreviations. Then the
  6749. configuration variable `org-file-apps' is checked if it contains an
  6750. entry for this file type, and if yes, the corresponding command is launched.
  6751. If no application is found, Emacs simply visits the file.
  6752. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6753. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6754. and o use an external application to visit the file.
  6755. Optional LINE specifies a line to go to, optional SEARCH a string to
  6756. search for. If LINE or SEARCH is given, the file will always be
  6757. opened in Emacs.
  6758. If the file does not exist, an error is thrown."
  6759. (setq in-emacs (or in-emacs line search))
  6760. (let* ((file (if (equal path "")
  6761. buffer-file-name
  6762. (substitute-in-file-name (expand-file-name path))))
  6763. (apps (append org-file-apps (org-default-apps)))
  6764. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6765. (dirp (if remp nil (file-directory-p file)))
  6766. (file (if (and dirp org-open-directory-means-index-dot-org)
  6767. (concat (file-name-as-directory file) "index.org")
  6768. file))
  6769. (a-m-a-p (assq 'auto-mode apps))
  6770. (dfile (downcase file))
  6771. (old-buffer (current-buffer))
  6772. (old-pos (point))
  6773. (old-mode major-mode)
  6774. ext cmd)
  6775. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6776. (setq ext (match-string 1 dfile))
  6777. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6778. (setq ext (match-string 1 dfile))))
  6779. (cond
  6780. ((equal in-emacs '(16))
  6781. (setq cmd (cdr (assoc 'system apps))))
  6782. (in-emacs (setq cmd 'emacs))
  6783. (t
  6784. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6785. (and dirp (cdr (assoc 'directory apps)))
  6786. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6787. 'string-match)
  6788. (cdr (assoc ext apps))
  6789. (cdr (assoc t apps))))))
  6790. (when (eq cmd 'system)
  6791. (setq cmd (cdr (assoc 'system apps))))
  6792. (when (eq cmd 'default)
  6793. (setq cmd (cdr (assoc t apps))))
  6794. (when (eq cmd 'mailcap)
  6795. (require 'mailcap)
  6796. (mailcap-parse-mailcaps)
  6797. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6798. (command (mailcap-mime-info mime-type)))
  6799. (if (stringp command)
  6800. (setq cmd command)
  6801. (setq cmd 'emacs))))
  6802. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6803. (not (file-exists-p file))
  6804. (not org-open-non-existing-files))
  6805. (error "No such file: %s" file))
  6806. (cond
  6807. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6808. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6809. (while (string-match "['\"]%s['\"]" cmd)
  6810. (setq cmd (replace-match "%s" t t cmd)))
  6811. (while (string-match "%s" cmd)
  6812. (setq cmd (replace-match
  6813. (save-match-data
  6814. (shell-quote-argument
  6815. (convert-standard-filename file)))
  6816. t t cmd)))
  6817. (save-window-excursion
  6818. (start-process-shell-command cmd nil cmd)
  6819. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6820. ))
  6821. ((or (stringp cmd)
  6822. (eq cmd 'emacs))
  6823. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6824. (widen)
  6825. (if line (goto-line line)
  6826. (if search (org-link-search search))))
  6827. ((consp cmd)
  6828. (let ((file (convert-standard-filename file)))
  6829. (eval cmd)))
  6830. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6831. (and (org-mode-p) (eq old-mode 'org-mode)
  6832. (or (not (equal old-buffer (current-buffer)))
  6833. (not (equal old-pos (point))))
  6834. (org-mark-ring-push old-pos old-buffer))))
  6835. (defun org-default-apps ()
  6836. "Return the default applications for this operating system."
  6837. (cond
  6838. ((eq system-type 'darwin)
  6839. org-file-apps-defaults-macosx)
  6840. ((eq system-type 'windows-nt)
  6841. org-file-apps-defaults-windowsnt)
  6842. (t org-file-apps-defaults-gnu)))
  6843. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6844. "Convert extensions to regular expressions in the cars of LIST.
  6845. Also, weed out any non-string entries, because the return value is used
  6846. only for regexp matching.
  6847. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6848. point to the symbol `emacs', indicating that the file should
  6849. be opened in Emacs."
  6850. (append
  6851. (delq nil
  6852. (mapcar (lambda (x)
  6853. (if (not (stringp (car x)))
  6854. nil
  6855. (if (string-match "\\W" (car x))
  6856. x
  6857. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6858. list))
  6859. (if add-auto-mode
  6860. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6861. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6862. (defun org-file-remote-p (file)
  6863. "Test whether FILE specifies a location on a remote system.
  6864. Return non-nil if the location is indeed remote.
  6865. For example, the filename \"/user@host:/foo\" specifies a location
  6866. on the system \"/user@host:\"."
  6867. (cond ((fboundp 'file-remote-p)
  6868. (file-remote-p file))
  6869. ((fboundp 'tramp-handle-file-remote-p)
  6870. (tramp-handle-file-remote-p file))
  6871. ((and (boundp 'ange-ftp-name-format)
  6872. (string-match (car ange-ftp-name-format) file))
  6873. t)
  6874. (t nil)))
  6875. ;;;; Refiling
  6876. (defun org-get-org-file ()
  6877. "Read a filename, with default directory `org-directory'."
  6878. (let ((default (or org-default-notes-file remember-data-file)))
  6879. (read-file-name (format "File name [%s]: " default)
  6880. (file-name-as-directory org-directory)
  6881. default)))
  6882. (defun org-notes-order-reversed-p ()
  6883. "Check if the current file should receive notes in reversed order."
  6884. (cond
  6885. ((not org-reverse-note-order) nil)
  6886. ((eq t org-reverse-note-order) t)
  6887. ((not (listp org-reverse-note-order)) nil)
  6888. (t (catch 'exit
  6889. (let ((all org-reverse-note-order)
  6890. entry)
  6891. (while (setq entry (pop all))
  6892. (if (string-match (car entry) buffer-file-name)
  6893. (throw 'exit (cdr entry))))
  6894. nil)))))
  6895. (defvar org-refile-target-table nil
  6896. "The list of refile targets, created by `org-refile'.")
  6897. (defvar org-agenda-new-buffers nil
  6898. "Buffers created to visit agenda files.")
  6899. (defun org-get-refile-targets (&optional default-buffer)
  6900. "Produce a table with refile targets."
  6901. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6902. targets txt re files f desc descre fast-path-p level)
  6903. (message "Getting targets...")
  6904. (with-current-buffer (or default-buffer (current-buffer))
  6905. (while (setq entry (pop entries))
  6906. (setq files (car entry) desc (cdr entry))
  6907. (setq fast-path-p nil)
  6908. (cond
  6909. ((null files) (setq files (list (current-buffer))))
  6910. ((eq files 'org-agenda-files)
  6911. (setq files (org-agenda-files 'unrestricted)))
  6912. ((and (symbolp files) (fboundp files))
  6913. (setq files (funcall files)))
  6914. ((and (symbolp files) (boundp files))
  6915. (setq files (symbol-value files))))
  6916. (if (stringp files) (setq files (list files)))
  6917. (cond
  6918. ((eq (car desc) :tag)
  6919. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6920. ((eq (car desc) :todo)
  6921. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6922. ((eq (car desc) :regexp)
  6923. (setq descre (cdr desc)))
  6924. ((eq (car desc) :level)
  6925. (setq descre (concat "^\\*\\{" (number-to-string
  6926. (if org-odd-levels-only
  6927. (1- (* 2 (cdr desc)))
  6928. (cdr desc)))
  6929. "\\}[ \t]")))
  6930. ((eq (car desc) :maxlevel)
  6931. (setq fast-path-p t)
  6932. (setq descre (concat "^\\*\\{1," (number-to-string
  6933. (if org-odd-levels-only
  6934. (1- (* 2 (cdr desc)))
  6935. (cdr desc)))
  6936. "\\}[ \t]")))
  6937. (t (error "Bad refiling target description %s" desc)))
  6938. (while (setq f (pop files))
  6939. (save-excursion
  6940. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6941. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6942. (setq f (expand-file-name f))
  6943. (save-excursion
  6944. (save-restriction
  6945. (widen)
  6946. (goto-char (point-min))
  6947. (while (re-search-forward descre nil t)
  6948. (goto-char (point-at-bol))
  6949. (when (looking-at org-complex-heading-regexp)
  6950. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  6951. txt (org-link-display-format (match-string 4))
  6952. re (concat "^" (regexp-quote
  6953. (buffer-substring (match-beginning 1)
  6954. (match-end 4)))))
  6955. (if (match-end 5) (setq re (concat re "[ \t]+"
  6956. (regexp-quote
  6957. (match-string 5)))))
  6958. (setq re (concat re "[ \t]*$"))
  6959. (when org-refile-use-outline-path
  6960. (setq txt (mapconcat 'org-protect-slash
  6961. (append
  6962. (if (eq org-refile-use-outline-path 'file)
  6963. (list (file-name-nondirectory
  6964. (buffer-file-name (buffer-base-buffer))))
  6965. (if (eq org-refile-use-outline-path 'full-file-path)
  6966. (list (buffer-file-name (buffer-base-buffer)))))
  6967. (org-get-outline-path fast-path-p level txt)
  6968. (list txt))
  6969. "/")))
  6970. (push (list txt f re (point)) targets))
  6971. (goto-char (point-at-eol))))))))
  6972. (message "Getting targets...done")
  6973. (nreverse targets))))
  6974. (defun org-protect-slash (s)
  6975. (while (string-match "/" s)
  6976. (setq s (replace-match "\\" t t s)))
  6977. s)
  6978. (defvar org-olpa (make-vector 20 nil))
  6979. (defun org-get-outline-path (&optional fastp level heading)
  6980. "Return the outline path to the current entry, as a list."
  6981. (if fastp
  6982. (progn
  6983. (if (> level 19)
  6984. (error "Outline path failure, more than 19 levels."))
  6985. (loop for i from level upto 19 do
  6986. (aset org-olpa i nil))
  6987. (prog1
  6988. (delq nil (append org-olpa nil))
  6989. (aset org-olpa level heading)))
  6990. (let (rtn)
  6991. (save-excursion
  6992. (while (org-up-heading-safe)
  6993. (when (looking-at org-complex-heading-regexp)
  6994. (push (org-match-string-no-properties 4) rtn)))
  6995. rtn))))
  6996. (defvar org-refile-history nil
  6997. "History for refiling operations.")
  6998. (defun org-refile (&optional goto default-buffer)
  6999. "Move the entry at point to another heading.
  7000. The list of target headings is compiled using the information in
  7001. `org-refile-targets', which see. This list is created before each use
  7002. and will therefore always be up-to-date.
  7003. At the target location, the entry is filed as a subitem of the target heading.
  7004. Depending on `org-reverse-note-order', the new subitem will either be the
  7005. first or the last subitem.
  7006. If there is an active region, all entries in that region will be moved.
  7007. However, the region must fulfil the requirement that the first heading
  7008. is the first one sets the top-level of the moved text - at most siblings
  7009. below it are allowed.
  7010. With prefix arg GOTO, the command will only visit the target location,
  7011. not actually move anything.
  7012. With a double prefix `C-u C-u', go to the location where the last refiling
  7013. operation has put the subtree."
  7014. (interactive "P")
  7015. (let* ((cbuf (current-buffer))
  7016. (regionp (org-region-active-p))
  7017. (region-start (and regionp (region-beginning)))
  7018. (region-end (and regionp (region-end)))
  7019. (region-length (and regionp (- region-end region-start)))
  7020. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7021. pos it nbuf file re level reversed)
  7022. (when regionp (goto-char region-start)
  7023. (unless (org-kill-is-subtree-p
  7024. (buffer-substring region-start region-end))
  7025. (error "The region is not a (sequence of) subtree(s)")))
  7026. (if (equal goto '(16))
  7027. (org-refile-goto-last-stored)
  7028. (when (setq it (org-refile-get-location
  7029. (if goto "Goto: " "Refile to: ") default-buffer))
  7030. (setq file (nth 1 it)
  7031. re (nth 2 it)
  7032. pos (nth 3 it))
  7033. (if (and (equal (buffer-file-name) file)
  7034. (if regionp
  7035. (and (>= pos region-start)
  7036. (<= pos region-end))
  7037. (and (>= pos (point))
  7038. (< pos (save-excursion
  7039. (org-end-of-subtree t t))))))
  7040. (error "Cannot refile to position inside the tree or region"))
  7041. (setq nbuf (or (find-buffer-visiting file)
  7042. (find-file-noselect file)))
  7043. (if goto
  7044. (progn
  7045. (switch-to-buffer nbuf)
  7046. (goto-char pos)
  7047. (org-show-context 'org-goto))
  7048. (if regionp
  7049. (progn
  7050. (kill-new (buffer-substring region-start region-end))
  7051. (org-save-markers-in-region region-start region-end))
  7052. (org-copy-subtree 1 nil t))
  7053. (save-excursion
  7054. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7055. (find-file-noselect file))))
  7056. (setq reversed (org-notes-order-reversed-p))
  7057. (save-excursion
  7058. (save-restriction
  7059. (widen)
  7060. (goto-char pos)
  7061. (looking-at outline-regexp)
  7062. (setq level (org-get-valid-level (funcall outline-level) 1))
  7063. (goto-char
  7064. (if reversed
  7065. (or (outline-next-heading) (point-max))
  7066. (or (save-excursion (outline-get-next-sibling))
  7067. (org-end-of-subtree t t)
  7068. (point-max))))
  7069. (if (not (bolp)) (newline))
  7070. (bookmark-set "org-refile-last-stored")
  7071. (org-paste-subtree level))))
  7072. (if regionp
  7073. (delete-region (point) (+ (point) region-length))
  7074. (org-cut-subtree))
  7075. (setq org-markers-to-move nil)
  7076. (message "Refiled to \"%s\"" (car it)))))))
  7077. (defun org-refile-goto-last-stored ()
  7078. "Go to the location where the last refile was stored."
  7079. (interactive)
  7080. (bookmark-jump "org-refile-last-stored")
  7081. (message "This is the location of the last refile"))
  7082. (defun org-refile-get-location (&optional prompt default-buffer)
  7083. "Prompt the user for a refile location, using PROMPT."
  7084. (let ((org-refile-targets org-refile-targets)
  7085. (org-refile-use-outline-path org-refile-use-outline-path))
  7086. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7087. (unless org-refile-target-table
  7088. (error "No refile targets"))
  7089. (let* ((cbuf (current-buffer))
  7090. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7091. (cfunc (if (and org-refile-use-outline-path
  7092. org-outline-path-complete-in-steps)
  7093. 'org-olpath-completing-read
  7094. 'org-ido-completing-read))
  7095. (extra (if org-refile-use-outline-path "/" ""))
  7096. (filename (and cfn (expand-file-name cfn)))
  7097. (tbl (mapcar
  7098. (lambda (x)
  7099. (if (not (equal filename (nth 1 x)))
  7100. (cons (concat (car x) extra " ("
  7101. (file-name-nondirectory (nth 1 x)) ")")
  7102. (cdr x))
  7103. (cons (concat (car x) extra) (cdr x))))
  7104. org-refile-target-table))
  7105. (completion-ignore-case t))
  7106. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7107. tbl)))
  7108. (defun org-olpath-completing-read (prompt collection &rest args)
  7109. "Read an outline path like a file name."
  7110. (let ((thetable collection))
  7111. (apply
  7112. 'org-ido-completing-read prompt
  7113. (lambda (string predicate &optional flag)
  7114. (let (rtn r f (l (length string)))
  7115. (cond
  7116. ((eq flag nil)
  7117. ;; try completion
  7118. (try-completion string thetable))
  7119. ((eq flag t)
  7120. ;; all-completions
  7121. (setq rtn (all-completions string thetable predicate))
  7122. (mapcar
  7123. (lambda (x)
  7124. (setq r (substring x l))
  7125. (if (string-match " ([^)]*)$" x)
  7126. (setq f (match-string 0 x))
  7127. (setq f ""))
  7128. (if (string-match "/" r)
  7129. (concat string (substring r 0 (match-end 0)) f)
  7130. x))
  7131. rtn))
  7132. ((eq flag 'lambda)
  7133. ;; exact match?
  7134. (assoc string thetable)))
  7135. ))
  7136. args)))
  7137. ;;;; Dynamic blocks
  7138. (defun org-find-dblock (name)
  7139. "Find the first dynamic block with name NAME in the buffer.
  7140. If not found, stay at current position and return nil."
  7141. (let (pos)
  7142. (save-excursion
  7143. (goto-char (point-min))
  7144. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7145. nil t)
  7146. (match-beginning 0))))
  7147. (if pos (goto-char pos))
  7148. pos))
  7149. (defconst org-dblock-start-re
  7150. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7151. "Matches the startline of a dynamic block, with parameters.")
  7152. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7153. "Matches the end of a dynamic block.")
  7154. (defun org-create-dblock (plist)
  7155. "Create a dynamic block section, with parameters taken from PLIST.
  7156. PLIST must contain a :name entry which is used as name of the block."
  7157. (unless (bolp) (newline))
  7158. (let ((name (plist-get plist :name)))
  7159. (insert "#+BEGIN: " name)
  7160. (while plist
  7161. (if (eq (car plist) :name)
  7162. (setq plist (cddr plist))
  7163. (insert " " (prin1-to-string (pop plist)))))
  7164. (insert "\n\n#+END:\n")
  7165. (beginning-of-line -2)))
  7166. (defun org-prepare-dblock ()
  7167. "Prepare dynamic block for refresh.
  7168. This empties the block, puts the cursor at the insert position and returns
  7169. the property list including an extra property :name with the block name."
  7170. (unless (looking-at org-dblock-start-re)
  7171. (error "Not at a dynamic block"))
  7172. (let* ((begdel (1+ (match-end 0)))
  7173. (name (org-no-properties (match-string 1)))
  7174. (params (append (list :name name)
  7175. (read (concat "(" (match-string 3) ")")))))
  7176. (unless (re-search-forward org-dblock-end-re nil t)
  7177. (error "Dynamic block not terminated"))
  7178. (setq params
  7179. (append params
  7180. (list :content (buffer-substring
  7181. begdel (match-beginning 0)))))
  7182. (delete-region begdel (match-beginning 0))
  7183. (goto-char begdel)
  7184. (open-line 1)
  7185. params))
  7186. (defun org-map-dblocks (&optional command)
  7187. "Apply COMMAND to all dynamic blocks in the current buffer.
  7188. If COMMAND is not given, use `org-update-dblock'."
  7189. (let ((cmd (or command 'org-update-dblock))
  7190. pos)
  7191. (save-excursion
  7192. (goto-char (point-min))
  7193. (while (re-search-forward org-dblock-start-re nil t)
  7194. (goto-char (setq pos (match-beginning 0)))
  7195. (condition-case nil
  7196. (funcall cmd)
  7197. (error (message "Error during update of dynamic block")))
  7198. (goto-char pos)
  7199. (unless (re-search-forward org-dblock-end-re nil t)
  7200. (error "Dynamic block not terminated"))))))
  7201. (defun org-dblock-update (&optional arg)
  7202. "User command for updating dynamic blocks.
  7203. Update the dynamic block at point. With prefix ARG, update all dynamic
  7204. blocks in the buffer."
  7205. (interactive "P")
  7206. (if arg
  7207. (org-update-all-dblocks)
  7208. (or (looking-at org-dblock-start-re)
  7209. (org-beginning-of-dblock))
  7210. (org-update-dblock)))
  7211. (defun org-update-dblock ()
  7212. "Update the dynamic block at point
  7213. This means to empty the block, parse for parameters and then call
  7214. the correct writing function."
  7215. (save-window-excursion
  7216. (let* ((pos (point))
  7217. (line (org-current-line))
  7218. (params (org-prepare-dblock))
  7219. (name (plist-get params :name))
  7220. (cmd (intern (concat "org-dblock-write:" name))))
  7221. (message "Updating dynamic block `%s' at line %d..." name line)
  7222. (funcall cmd params)
  7223. (message "Updating dynamic block `%s' at line %d...done" name line)
  7224. (goto-char pos))))
  7225. (defun org-beginning-of-dblock ()
  7226. "Find the beginning of the dynamic block at point.
  7227. Error if there is no such block at point."
  7228. (let ((pos (point))
  7229. beg)
  7230. (end-of-line 1)
  7231. (if (and (re-search-backward org-dblock-start-re nil t)
  7232. (setq beg (match-beginning 0))
  7233. (re-search-forward org-dblock-end-re nil t)
  7234. (> (match-end 0) pos))
  7235. (goto-char beg)
  7236. (goto-char pos)
  7237. (error "Not in a dynamic block"))))
  7238. (defun org-update-all-dblocks ()
  7239. "Update all dynamic blocks in the buffer.
  7240. This function can be used in a hook."
  7241. (when (org-mode-p)
  7242. (org-map-dblocks 'org-update-dblock)))
  7243. ;;;; Completion
  7244. (defconst org-additional-option-like-keywords
  7245. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7246. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7247. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7248. "BEGIN_QUOTE" "END_QUOTE"
  7249. "BEGIN_VERSE" "END_VERSE"
  7250. "BEGIN_SRC" "END_SRC"
  7251. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7252. (defcustom org-structure-template-alist
  7253. '(
  7254. ("s" "#+begin_src ?\n\n#+end_src"
  7255. "<src lang=\"?\">\n\n</src>")
  7256. ("e" "#+begin_example\n?\n#+end_example"
  7257. "<example>\n?\n</example>")
  7258. ("q" "#+begin_quote\n?\n#+end_quote"
  7259. "<quote>\n?\n</quote>")
  7260. ("v" "#+begin_verse\n?\n#+end_verse"
  7261. "<verse>\n?\n/verse>")
  7262. ("l" "#+begin_latex\n?\n#+end_latex"
  7263. "<literal style=\"latex\">\n?\n</literal>")
  7264. ("L" "#+latex: "
  7265. "<literal style=\"latex\">?</literal>")
  7266. ("h" "#+begin_html\n?\n#+end_html"
  7267. "<literal style=\"html\">\n?\n</literal>")
  7268. ("H" "#+html: "
  7269. "<literal style=\"html\">?</literal>")
  7270. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7271. ("A" "#+ascii: ")
  7272. ("i" "#+include %file ?"
  7273. "<include file=%file markup=\"?\">")
  7274. )
  7275. "Structure completion elements.
  7276. This is a list of abbreviation keys and values. The value gets inserted
  7277. it you type @samp{.} followed by the key and then the completion key,
  7278. usually `M-TAB'. %file will be replaced by a file name after prompting
  7279. for the file using completion.
  7280. There are two templates for each key, the first uses the original Org syntax,
  7281. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7282. the default when the /org-mtags.el/ module has been loaded. See also the
  7283. variable `org-mtags-prefer-muse-templates'.
  7284. This is an experimental feature, it is undecided if it is going to stay in."
  7285. :group 'org-completion
  7286. :type '(repeat
  7287. (string :tag "Key")
  7288. (string :tag "Template")
  7289. (string :tag "Muse Template")))
  7290. (defun org-try-structure-completion ()
  7291. "Try to complete a structure template before point.
  7292. This looks for strings like \"<e\" on an otherwise empty line and
  7293. expands them."
  7294. (let ((l (buffer-substring (point-at-bol) (point)))
  7295. a)
  7296. (when (and (looking-at "[ \t]*$")
  7297. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7298. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7299. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7300. (match-beginning 1)) a)
  7301. t)))
  7302. (defun org-complete-expand-structure-template (start cell)
  7303. "Expand a structure template."
  7304. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7305. (rpl (nth (if musep 2 1) cell)))
  7306. (delete-region start (point))
  7307. (when (string-match "\\`#\\+" rpl)
  7308. (cond
  7309. ((bolp))
  7310. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7311. (delete-region (point-at-bol) (point)))
  7312. (t (newline))))
  7313. (setq start (point))
  7314. (if (string-match "%file" rpl)
  7315. (setq rpl (replace-match
  7316. (concat
  7317. "\""
  7318. (save-match-data
  7319. (abbreviate-file-name (read-file-name "Include file: ")))
  7320. "\"")
  7321. t t rpl)))
  7322. (insert rpl)
  7323. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7324. (defun org-complete (&optional arg)
  7325. "Perform completion on word at point.
  7326. At the beginning of a headline, this completes TODO keywords as given in
  7327. `org-todo-keywords'.
  7328. If the current word is preceded by a backslash, completes the TeX symbols
  7329. that are supported for HTML support.
  7330. If the current word is preceded by \"#+\", completes special words for
  7331. setting file options.
  7332. In the line after \"#+STARTUP:, complete valid keywords.\"
  7333. At all other locations, this simply calls the value of
  7334. `org-completion-fallback-command'."
  7335. (interactive "P")
  7336. (org-without-partial-completion
  7337. (catch 'exit
  7338. (let* ((a nil)
  7339. (end (point))
  7340. (beg1 (save-excursion
  7341. (skip-chars-backward (org-re "[:alnum:]_@"))
  7342. (point)))
  7343. (beg (save-excursion
  7344. (skip-chars-backward "a-zA-Z0-9_:$")
  7345. (point)))
  7346. (confirm (lambda (x) (stringp (car x))))
  7347. (searchhead (equal (char-before beg) ?*))
  7348. (struct
  7349. (when (and (member (char-before beg1) '(?. ?<))
  7350. (setq a (assoc (buffer-substring beg1 (point))
  7351. org-structure-template-alist)))
  7352. (org-complete-expand-structure-template (1- beg1) a)
  7353. (throw 'exit t)))
  7354. (tag (and (equal (char-before beg1) ?:)
  7355. (equal (char-after (point-at-bol)) ?*)))
  7356. (prop (and (equal (char-before beg1) ?:)
  7357. (not (equal (char-after (point-at-bol)) ?*))))
  7358. (texp (equal (char-before beg) ?\\))
  7359. (link (equal (char-before beg) ?\[))
  7360. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7361. beg)
  7362. "#+"))
  7363. (startup (string-match "^#\\+STARTUP:.*"
  7364. (buffer-substring (point-at-bol) (point))))
  7365. (completion-ignore-case opt)
  7366. (type nil)
  7367. (tbl nil)
  7368. (table (cond
  7369. (opt
  7370. (setq type :opt)
  7371. (require 'org-exp)
  7372. (append
  7373. (mapcar
  7374. (lambda (x)
  7375. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7376. (cons (match-string 2 x) (match-string 1 x)))
  7377. (org-split-string (org-get-current-options) "\n"))
  7378. (mapcar 'list org-additional-option-like-keywords)))
  7379. (startup
  7380. (setq type :startup)
  7381. org-startup-options)
  7382. (link (append org-link-abbrev-alist-local
  7383. org-link-abbrev-alist))
  7384. (texp
  7385. (setq type :tex)
  7386. org-html-entities)
  7387. ((string-match "\\`\\*+[ \t]+\\'"
  7388. (buffer-substring (point-at-bol) beg))
  7389. (setq type :todo)
  7390. (mapcar 'list org-todo-keywords-1))
  7391. (searchhead
  7392. (setq type :searchhead)
  7393. (save-excursion
  7394. (goto-char (point-min))
  7395. (while (re-search-forward org-todo-line-regexp nil t)
  7396. (push (list
  7397. (org-make-org-heading-search-string
  7398. (match-string 3) t))
  7399. tbl)))
  7400. tbl)
  7401. (tag (setq type :tag beg beg1)
  7402. (or org-tag-alist (org-get-buffer-tags)))
  7403. (prop (setq type :prop beg beg1)
  7404. (mapcar 'list (org-buffer-property-keys nil t t)))
  7405. (t (progn
  7406. (call-interactively org-completion-fallback-command)
  7407. (throw 'exit nil)))))
  7408. (pattern (buffer-substring-no-properties beg end))
  7409. (completion (try-completion pattern table confirm)))
  7410. (cond ((eq completion t)
  7411. (if (not (assoc (upcase pattern) table))
  7412. (message "Already complete")
  7413. (if (and (equal type :opt)
  7414. (not (member (car (assoc (upcase pattern) table))
  7415. org-additional-option-like-keywords)))
  7416. (insert (substring (cdr (assoc (upcase pattern) table))
  7417. (length pattern)))
  7418. (if (memq type '(:tag :prop)) (insert ":")))))
  7419. ((null completion)
  7420. (message "Can't find completion for \"%s\"" pattern)
  7421. (ding))
  7422. ((not (string= pattern completion))
  7423. (delete-region beg end)
  7424. (if (string-match " +$" completion)
  7425. (setq completion (replace-match "" t t completion)))
  7426. (insert completion)
  7427. (if (get-buffer-window "*Completions*")
  7428. (delete-window (get-buffer-window "*Completions*")))
  7429. (if (assoc completion table)
  7430. (if (eq type :todo) (insert " ")
  7431. (if (memq type '(:tag :prop)) (insert ":"))))
  7432. (if (and (equal type :opt) (assoc completion table))
  7433. (message "%s" (substitute-command-keys
  7434. "Press \\[org-complete] again to insert example settings"))))
  7435. (t
  7436. (message "Making completion list...")
  7437. (let ((list (sort (all-completions pattern table confirm)
  7438. 'string<)))
  7439. (with-output-to-temp-buffer "*Completions*"
  7440. (condition-case nil
  7441. ;; Protection needed for XEmacs and emacs 21
  7442. (display-completion-list list pattern)
  7443. (error (display-completion-list list)))))
  7444. (message "Making completion list...%s" "done")))))))
  7445. ;;;; TODO, DEADLINE, Comments
  7446. (defun org-toggle-comment ()
  7447. "Change the COMMENT state of an entry."
  7448. (interactive)
  7449. (save-excursion
  7450. (org-back-to-heading)
  7451. (let (case-fold-search)
  7452. (if (looking-at (concat outline-regexp
  7453. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7454. (replace-match "" t t nil 1)
  7455. (if (looking-at outline-regexp)
  7456. (progn
  7457. (goto-char (match-end 0))
  7458. (insert org-comment-string " ")))))))
  7459. (defvar org-last-todo-state-is-todo nil
  7460. "This is non-nil when the last TODO state change led to a TODO state.
  7461. If the last change removed the TODO tag or switched to DONE, then
  7462. this is nil.")
  7463. (defvar org-setting-tags nil) ; dynamically skipped
  7464. (defun org-parse-local-options (string var)
  7465. "Parse STRING for startup setting relevant for variable VAR."
  7466. (let ((rtn (symbol-value var))
  7467. e opts)
  7468. (save-match-data
  7469. (if (or (not string) (not (string-match "\\S-" string)))
  7470. rtn
  7471. (setq opts (delq nil (mapcar (lambda (x)
  7472. (setq e (assoc x org-startup-options))
  7473. (if (eq (nth 1 e) var) e nil))
  7474. (org-split-string string "[ \t]+"))))
  7475. (if (not opts)
  7476. rtn
  7477. (setq rtn nil)
  7478. (while (setq e (pop opts))
  7479. (if (not (nth 3 e))
  7480. (setq rtn (nth 2 e))
  7481. (if (not (listp rtn)) (setq rtn nil))
  7482. (push (nth 2 e) rtn)))
  7483. rtn)))))
  7484. (defvar org-blocker-hook nil
  7485. "Hook for functions that are allowed to block a state change.
  7486. Each function gets as its single argument a property list, see
  7487. `org-trigger-hook' for more information about this list.
  7488. If any of the functions in this hook returns nil, the state change
  7489. is blocked.")
  7490. (defvar org-trigger-hook nil
  7491. "Hook for functions that are triggered by a state change.
  7492. Each function gets as its single argument a property list with at least
  7493. the following elements:
  7494. (:type type-of-change :position pos-at-entry-start
  7495. :from old-state :to new-state)
  7496. Depending on the type, more properties may be present.
  7497. This mechanism is currently implemented for:
  7498. TODO state changes
  7499. ------------------
  7500. :type todo-state-change
  7501. :from previous state (keyword as a string), or nil
  7502. :to new state (keyword as a string), or nil")
  7503. (defvar org-agenda-headline-snapshot-before-repeat)
  7504. (defun org-todo (&optional arg)
  7505. "Change the TODO state of an item.
  7506. The state of an item is given by a keyword at the start of the heading,
  7507. like
  7508. *** TODO Write paper
  7509. *** DONE Call mom
  7510. The different keywords are specified in the variable `org-todo-keywords'.
  7511. By default the available states are \"TODO\" and \"DONE\".
  7512. So for this example: when the item starts with TODO, it is changed to DONE.
  7513. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7514. DONE are present, add TODO at the beginning of the heading.
  7515. With C-u prefix arg, use completion to determine the new state.
  7516. With numeric prefix arg, switch to that state.
  7517. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7518. For calling through lisp, arg is also interpreted in the following way:
  7519. 'none -> empty state
  7520. \"\"(empty string) -> switch to empty state
  7521. 'done -> switch to DONE
  7522. 'nextset -> switch to the next set of keywords
  7523. 'previousset -> switch to the previous set of keywords
  7524. \"WAITING\" -> switch to the specified keyword, but only if it
  7525. really is a member of `org-todo-keywords'."
  7526. (interactive "P")
  7527. (if (equal arg '(16)) (setq arg 'nextset))
  7528. (save-excursion
  7529. (catch 'exit
  7530. (org-back-to-heading)
  7531. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7532. (or (looking-at (concat " +" org-todo-regexp " *"))
  7533. (looking-at " *"))
  7534. (let* ((match-data (match-data))
  7535. (startpos (point-at-bol))
  7536. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7537. (org-log-done org-log-done)
  7538. (org-log-repeat org-log-repeat)
  7539. (org-todo-log-states org-todo-log-states)
  7540. (this (match-string 1))
  7541. (hl-pos (match-beginning 0))
  7542. (head (org-get-todo-sequence-head this))
  7543. (ass (assoc head org-todo-kwd-alist))
  7544. (interpret (nth 1 ass))
  7545. (done-word (nth 3 ass))
  7546. (final-done-word (nth 4 ass))
  7547. (last-state (or this ""))
  7548. (completion-ignore-case t)
  7549. (member (member this org-todo-keywords-1))
  7550. (tail (cdr member))
  7551. (state (cond
  7552. ((and org-todo-key-trigger
  7553. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7554. (and (not arg) org-use-fast-todo-selection
  7555. (not (eq org-use-fast-todo-selection 'prefix)))))
  7556. ;; Use fast selection
  7557. (org-fast-todo-selection))
  7558. ((and (equal arg '(4))
  7559. (or (not org-use-fast-todo-selection)
  7560. (not org-todo-key-trigger)))
  7561. ;; Read a state with completion
  7562. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7563. org-todo-keywords-1)
  7564. nil t))
  7565. ((eq arg 'right)
  7566. (if this
  7567. (if tail (car tail) nil)
  7568. (car org-todo-keywords-1)))
  7569. ((eq arg 'left)
  7570. (if (equal member org-todo-keywords-1)
  7571. nil
  7572. (if this
  7573. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7574. org-todo-keywords-1)
  7575. (org-last org-todo-keywords-1))))
  7576. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7577. (setq arg nil))) ; hack to fall back to cycling
  7578. (arg
  7579. ;; user or caller requests a specific state
  7580. (cond
  7581. ((equal arg "") nil)
  7582. ((eq arg 'none) nil)
  7583. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7584. ((eq arg 'nextset)
  7585. (or (car (cdr (member head org-todo-heads)))
  7586. (car org-todo-heads)))
  7587. ((eq arg 'previousset)
  7588. (let ((org-todo-heads (reverse org-todo-heads)))
  7589. (or (car (cdr (member head org-todo-heads)))
  7590. (car org-todo-heads))))
  7591. ((car (member arg org-todo-keywords-1)))
  7592. ((nth (1- (prefix-numeric-value arg))
  7593. org-todo-keywords-1))))
  7594. ((null member) (or head (car org-todo-keywords-1)))
  7595. ((equal this final-done-word) nil) ;; -> make empty
  7596. ((null tail) nil) ;; -> first entry
  7597. ((eq interpret 'sequence)
  7598. (car tail))
  7599. ((memq interpret '(type priority))
  7600. (if (eq this-command last-command)
  7601. (car tail)
  7602. (if (> (length tail) 0)
  7603. (or done-word (car org-done-keywords))
  7604. nil)))
  7605. (t nil)))
  7606. (next (if state (concat " " state " ") " "))
  7607. (change-plist (list :type 'todo-state-change :from this :to state
  7608. :position startpos))
  7609. dolog now-done-p)
  7610. (when org-blocker-hook
  7611. (unless (save-excursion
  7612. (save-match-data
  7613. (run-hook-with-args-until-failure
  7614. 'org-blocker-hook change-plist)))
  7615. (if (interactive-p)
  7616. (error "TODO state change from %s to %s blocked" this state)
  7617. ;; fail silently
  7618. (message "TODO state change from %s to %s blocked" this state)
  7619. (throw 'exit nil))))
  7620. (store-match-data match-data)
  7621. (replace-match next t t)
  7622. (unless (pos-visible-in-window-p hl-pos)
  7623. (message "TODO state changed to %s" (org-trim next)))
  7624. (unless head
  7625. (setq head (org-get-todo-sequence-head state)
  7626. ass (assoc head org-todo-kwd-alist)
  7627. interpret (nth 1 ass)
  7628. done-word (nth 3 ass)
  7629. final-done-word (nth 4 ass)))
  7630. (when (memq arg '(nextset previousset))
  7631. (message "Keyword-Set %d/%d: %s"
  7632. (- (length org-todo-sets) -1
  7633. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7634. (length org-todo-sets)
  7635. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7636. (setq org-last-todo-state-is-todo
  7637. (not (member state org-done-keywords)))
  7638. (setq now-done-p (and (member state org-done-keywords)
  7639. (not (member this org-done-keywords))))
  7640. (and logging (org-local-logging logging))
  7641. (when (and (or org-todo-log-states org-log-done)
  7642. (not (memq arg '(nextset previousset))))
  7643. ;; we need to look at recording a time and note
  7644. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7645. (nth 2 (assoc this org-todo-log-states))))
  7646. (when (and state
  7647. (member state org-not-done-keywords)
  7648. (not (member this org-not-done-keywords)))
  7649. ;; This is now a todo state and was not one before
  7650. ;; If there was a CLOSED time stamp, get rid of it.
  7651. (org-add-planning-info nil nil 'closed))
  7652. (when (and now-done-p org-log-done)
  7653. ;; It is now done, and it was not done before
  7654. (org-add-planning-info 'closed (org-current-time))
  7655. (if (and (not dolog) (eq 'note org-log-done))
  7656. (org-add-log-setup 'done state 'findpos 'note)))
  7657. (when (and state dolog)
  7658. ;; This is a non-nil state, and we need to log it
  7659. (org-add-log-setup 'state state 'findpos dolog)))
  7660. ;; Fixup tag positioning
  7661. (org-todo-trigger-tag-changes state)
  7662. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7663. (when org-provide-todo-statistics
  7664. (org-update-parent-todo-statistics))
  7665. (run-hooks 'org-after-todo-state-change-hook)
  7666. (if (and arg (not (member state org-done-keywords)))
  7667. (setq head (org-get-todo-sequence-head state)))
  7668. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7669. ;; Do we need to trigger a repeat?
  7670. (when now-done-p
  7671. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7672. ;; This is for the agenda, take a snapshot of the headline.
  7673. (save-match-data
  7674. (setq org-agenda-headline-snapshot-before-repeat
  7675. (org-get-heading))))
  7676. (org-auto-repeat-maybe state))
  7677. ;; Fixup cursor location if close to the keyword
  7678. (if (and (outline-on-heading-p)
  7679. (not (bolp))
  7680. (save-excursion (beginning-of-line 1)
  7681. (looking-at org-todo-line-regexp))
  7682. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7683. (progn
  7684. (goto-char (or (match-end 2) (match-end 1)))
  7685. (just-one-space)))
  7686. (when org-trigger-hook
  7687. (save-excursion
  7688. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7689. (defun org-update-parent-todo-statistics ()
  7690. "Update any statistics cookie in the parent of the current headline."
  7691. (interactive)
  7692. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7693. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7694. (catch 'exit
  7695. (save-excursion
  7696. (setq level (org-up-heading-safe))
  7697. (unless (and level
  7698. (re-search-forward box-re (point-at-eol) t))
  7699. (throw 'exit nil))
  7700. (setq is-percent (match-end 2))
  7701. (save-match-data
  7702. (unless (outline-next-heading) (throw 'exit nil))
  7703. (while (looking-at org-todo-line-regexp)
  7704. (setq kwd (match-string 2))
  7705. (and kwd (setq cnt-all (1+ cnt-all)))
  7706. (and (member kwd org-done-keywords)
  7707. (setq cnt-done (1+ cnt-done)))
  7708. (condition-case nil
  7709. (org-forward-same-level 1)
  7710. (error (end-of-line 1)))))
  7711. (replace-match
  7712. (if is-percent
  7713. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7714. (format "[%d/%d]" cnt-done cnt-all)))
  7715. (run-hook-with-args 'org-after-todo-statistics-hook
  7716. cnt-done (- cnt-all cnt-done))))))
  7717. (defvar org-after-todo-statistics-hook nil
  7718. "Hook that is called after a TODO statistics cookie has been updated.
  7719. Each function is called with two arguments: the number of not-done entries
  7720. and the number of done entries.
  7721. For example, the following function, when added to this hook, will switch
  7722. an entry to DONE when all children are done, and back to TODO when new
  7723. entries are set to a TODO status. Note that this hook is only called
  7724. when there is a statistics cookie in the headline!
  7725. (defun org-summary-todo (n-done n-not-done)
  7726. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7727. (let (org-log-done org-log-states) ; turn off logging
  7728. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7729. ")
  7730. (defun org-todo-trigger-tag-changes (state)
  7731. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7732. (let ((l org-todo-state-tags-triggers)
  7733. changes)
  7734. (when (or (not state) (equal state ""))
  7735. (setq changes (append changes (cdr (assoc "" l)))))
  7736. (when (and (stringp state) (> (length state) 0))
  7737. (setq changes (append changes (cdr (assoc state l)))))
  7738. (when (member state org-not-done-keywords)
  7739. (setq changes (append changes (cdr (assoc 'todo l)))))
  7740. (when (member state org-done-keywords)
  7741. (setq changes (append changes (cdr (assoc 'done l)))))
  7742. (dolist (c changes)
  7743. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7744. (defun org-local-logging (value)
  7745. "Get logging settings from a property VALUE."
  7746. (let* (words w a)
  7747. ;; directly set the variables, they are already local.
  7748. (setq org-log-done nil
  7749. org-log-repeat nil
  7750. org-todo-log-states nil)
  7751. (setq words (org-split-string value))
  7752. (while (setq w (pop words))
  7753. (cond
  7754. ((setq a (assoc w org-startup-options))
  7755. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7756. (set (nth 1 a) (nth 2 a))))
  7757. ((setq a (org-extract-log-state-settings w))
  7758. (and (member (car a) org-todo-keywords-1)
  7759. (push a org-todo-log-states)))))))
  7760. (defun org-get-todo-sequence-head (kwd)
  7761. "Return the head of the TODO sequence to which KWD belongs.
  7762. If KWD is not set, check if there is a text property remembering the
  7763. right sequence."
  7764. (let (p)
  7765. (cond
  7766. ((not kwd)
  7767. (or (get-text-property (point-at-bol) 'org-todo-head)
  7768. (progn
  7769. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7770. nil (point-at-eol)))
  7771. (get-text-property p 'org-todo-head))))
  7772. ((not (member kwd org-todo-keywords-1))
  7773. (car org-todo-keywords-1))
  7774. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7775. (defun org-fast-todo-selection ()
  7776. "Fast TODO keyword selection with single keys.
  7777. Returns the new TODO keyword, or nil if no state change should occur."
  7778. (let* ((fulltable org-todo-key-alist)
  7779. (done-keywords org-done-keywords) ;; needed for the faces.
  7780. (maxlen (apply 'max (mapcar
  7781. (lambda (x)
  7782. (if (stringp (car x)) (string-width (car x)) 0))
  7783. fulltable)))
  7784. (expert nil)
  7785. (fwidth (+ maxlen 3 1 3))
  7786. (ncol (/ (- (window-width) 4) fwidth))
  7787. tg cnt e c tbl
  7788. groups ingroup)
  7789. (save-window-excursion
  7790. (if expert
  7791. (set-buffer (get-buffer-create " *Org todo*"))
  7792. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7793. (erase-buffer)
  7794. (org-set-local 'org-done-keywords done-keywords)
  7795. (setq tbl fulltable cnt 0)
  7796. (while (setq e (pop tbl))
  7797. (cond
  7798. ((equal e '(:startgroup))
  7799. (push '() groups) (setq ingroup t)
  7800. (when (not (= cnt 0))
  7801. (setq cnt 0)
  7802. (insert "\n"))
  7803. (insert "{ "))
  7804. ((equal e '(:endgroup))
  7805. (setq ingroup nil cnt 0)
  7806. (insert "}\n"))
  7807. (t
  7808. (setq tg (car e) c (cdr e))
  7809. (if ingroup (push tg (car groups)))
  7810. (setq tg (org-add-props tg nil 'face
  7811. (org-get-todo-face tg)))
  7812. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7813. (insert "[" c "] " tg (make-string
  7814. (- fwidth 4 (length tg)) ?\ ))
  7815. (when (= (setq cnt (1+ cnt)) ncol)
  7816. (insert "\n")
  7817. (if ingroup (insert " "))
  7818. (setq cnt 0)))))
  7819. (insert "\n")
  7820. (goto-char (point-min))
  7821. (if (not expert) (org-fit-window-to-buffer))
  7822. (message "[a-z..]:Set [SPC]:clear")
  7823. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7824. (cond
  7825. ((or (= c ?\C-g)
  7826. (and (= c ?q) (not (rassoc c fulltable))))
  7827. (setq quit-flag t))
  7828. ((= c ?\ ) nil)
  7829. ((setq e (rassoc c fulltable) tg (car e))
  7830. tg)
  7831. (t (setq quit-flag t))))))
  7832. (defun org-entry-is-todo-p ()
  7833. (member (org-get-todo-state) org-not-done-keywords))
  7834. (defun org-entry-is-done-p ()
  7835. (member (org-get-todo-state) org-done-keywords))
  7836. (defun org-get-todo-state ()
  7837. (save-excursion
  7838. (org-back-to-heading t)
  7839. (and (looking-at org-todo-line-regexp)
  7840. (match-end 2)
  7841. (match-string 2))))
  7842. (defun org-at-date-range-p (&optional inactive-ok)
  7843. "Is the cursor inside a date range?"
  7844. (interactive)
  7845. (save-excursion
  7846. (catch 'exit
  7847. (let ((pos (point)))
  7848. (skip-chars-backward "^[<\r\n")
  7849. (skip-chars-backward "<[")
  7850. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7851. (>= (match-end 0) pos)
  7852. (throw 'exit t))
  7853. (skip-chars-backward "^<[\r\n")
  7854. (skip-chars-backward "<[")
  7855. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7856. (>= (match-end 0) pos)
  7857. (throw 'exit t)))
  7858. nil)))
  7859. (defun org-get-repeat ()
  7860. "Check if there is a deadline/schedule with repeater in this entry."
  7861. (save-match-data
  7862. (save-excursion
  7863. (org-back-to-heading t)
  7864. (if (re-search-forward
  7865. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7866. (match-string 1)))))
  7867. (defvar org-last-changed-timestamp)
  7868. (defvar org-last-inserted-timestamp)
  7869. (defvar org-log-post-message)
  7870. (defvar org-log-note-purpose)
  7871. (defvar org-log-note-how)
  7872. (defvar org-log-note-extra)
  7873. (defun org-auto-repeat-maybe (done-word)
  7874. "Check if the current headline contains a repeated deadline/schedule.
  7875. If yes, set TODO state back to what it was and change the base date
  7876. of repeating deadline/scheduled time stamps to new date.
  7877. This function is run automatically after each state change to a DONE state."
  7878. ;; last-state is dynamically scoped into this function
  7879. (let* ((repeat (org-get-repeat))
  7880. (aa (assoc last-state org-todo-kwd-alist))
  7881. (interpret (nth 1 aa))
  7882. (head (nth 2 aa))
  7883. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7884. (msg "Entry repeats: ")
  7885. (org-log-done nil)
  7886. (org-todo-log-states nil)
  7887. (nshiftmax 10) (nshift 0)
  7888. re type n what ts time)
  7889. (when repeat
  7890. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7891. (org-todo (if (eq interpret 'type) last-state head))
  7892. (when org-log-repeat
  7893. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7894. (memq 'org-add-log-note post-command-hook))
  7895. ;; OK, we are already setup for some record
  7896. (if (eq org-log-repeat 'note)
  7897. ;; make sure we take a note, not only a time stamp
  7898. (setq org-log-note-how 'note))
  7899. ;; Set up for taking a record
  7900. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7901. 'findpos org-log-repeat)))
  7902. (org-back-to-heading t)
  7903. (org-add-planning-info nil nil 'closed)
  7904. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7905. org-deadline-time-regexp "\\)\\|\\("
  7906. org-ts-regexp "\\)"))
  7907. (while (re-search-forward
  7908. re (save-excursion (outline-next-heading) (point)) t)
  7909. (setq type (if (match-end 1) org-scheduled-string
  7910. (if (match-end 3) org-deadline-string "Plain:"))
  7911. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  7912. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7913. (setq n (string-to-number (match-string 2 ts))
  7914. what (match-string 3 ts))
  7915. (if (equal what "w") (setq n (* n 7) what "d"))
  7916. ;; Preparation, see if we need to modify the start date for the change
  7917. (when (match-end 1)
  7918. (setq time (save-match-data (org-time-string-to-time ts)))
  7919. (cond
  7920. ((equal (match-string 1 ts) ".")
  7921. ;; Shift starting date to today
  7922. (org-timestamp-change
  7923. (- (time-to-days (current-time)) (time-to-days time))
  7924. 'day))
  7925. ((equal (match-string 1 ts) "+")
  7926. (while (or (= nshift 0)
  7927. (<= (time-to-days time) (time-to-days (current-time))))
  7928. (when (= (incf nshift) nshiftmax)
  7929. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7930. (error "Abort")))
  7931. (org-timestamp-change n (cdr (assoc what whata)))
  7932. (org-at-timestamp-p t)
  7933. (setq ts (match-string 1))
  7934. (setq time (save-match-data (org-time-string-to-time ts))))
  7935. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7936. ;; rematch, so that we have everything in place for the real shift
  7937. (org-at-timestamp-p t)
  7938. (setq ts (match-string 1))
  7939. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7940. (org-timestamp-change n (cdr (assoc what whata)))
  7941. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7942. (setq org-log-post-message msg)
  7943. (message "%s" msg))))
  7944. (defun org-show-todo-tree (arg)
  7945. "Make a compact tree which shows all headlines marked with TODO.
  7946. The tree will show the lines where the regexp matches, and all higher
  7947. headlines above the match.
  7948. With a \\[universal-argument] prefix, also show the DONE entries.
  7949. With a numeric prefix N, construct a sparse tree for the Nth element
  7950. of `org-todo-keywords-1'."
  7951. (interactive "P")
  7952. (let ((case-fold-search nil)
  7953. (kwd-re
  7954. (cond ((null arg) org-not-done-regexp)
  7955. ((equal arg '(4))
  7956. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7957. (mapcar 'list org-todo-keywords-1))))
  7958. (concat "\\("
  7959. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7960. "\\)\\>")))
  7961. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7962. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7963. org-todo-keywords-1)))
  7964. (t (error "Invalid prefix argument: %s" arg)))))
  7965. (message "%d TODO entries found"
  7966. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7967. (defun org-deadline (&optional remove time)
  7968. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7969. With argument REMOVE, remove any deadline from the item.
  7970. When TIME is set, it should be an internal time specification, and the
  7971. scheduling will use the corresponding date."
  7972. (interactive "P")
  7973. (if remove
  7974. (progn
  7975. (org-remove-timestamp-with-keyword org-deadline-string)
  7976. (message "Item no longer has a deadline."))
  7977. (if (org-get-repeat)
  7978. (error "Cannot change deadline on task with repeater, please do that by hand")
  7979. (org-add-planning-info 'deadline time 'closed)
  7980. (message "Deadline on %s" org-last-inserted-timestamp))))
  7981. (defun org-schedule (&optional remove time)
  7982. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7983. With argument REMOVE, remove any scheduling date from the item.
  7984. When TIME is set, it should be an internal time specification, and the
  7985. scheduling will use the corresponding date."
  7986. (interactive "P")
  7987. (if remove
  7988. (progn
  7989. (org-remove-timestamp-with-keyword org-scheduled-string)
  7990. (message "Item is no longer scheduled."))
  7991. (if (org-get-repeat)
  7992. (error "Cannot reschedule task with repeater, please do that by hand")
  7993. (org-add-planning-info 'scheduled time 'closed)
  7994. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7995. (defun org-remove-timestamp-with-keyword (keyword)
  7996. "Remove all time stamps with KEYWORD in the current entry."
  7997. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7998. beg)
  7999. (save-excursion
  8000. (org-back-to-heading t)
  8001. (setq beg (point))
  8002. (org-end-of-subtree t t)
  8003. (while (re-search-backward re beg t)
  8004. (replace-match "")
  8005. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8006. (equal (char-before) ?\ ))
  8007. (backward-delete-char 1)
  8008. (if (string-match "^[ \t]*$" (buffer-substring
  8009. (point-at-bol) (point-at-eol)))
  8010. (delete-region (point-at-bol)
  8011. (min (point-max) (1+ (point-at-eol))))))))))
  8012. (defun org-add-planning-info (what &optional time &rest remove)
  8013. "Insert new timestamp with keyword in the line directly after the headline.
  8014. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8015. If non is given, the user is prompted for a date.
  8016. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8017. be removed."
  8018. (interactive)
  8019. (let (org-time-was-given org-end-time-was-given ts
  8020. end default-time default-input)
  8021. (when (and (not time) (memq what '(scheduled deadline)))
  8022. ;; Try to get a default date/time from existing timestamp
  8023. (save-excursion
  8024. (org-back-to-heading t)
  8025. (setq end (save-excursion (outline-next-heading) (point)))
  8026. (when (re-search-forward (if (eq what 'scheduled)
  8027. org-scheduled-time-regexp
  8028. org-deadline-time-regexp)
  8029. end t)
  8030. (setq ts (match-string 1)
  8031. default-time
  8032. (apply 'encode-time (org-parse-time-string ts))
  8033. default-input (and ts (org-get-compact-tod ts))))))
  8034. (when what
  8035. ;; If necessary, get the time from the user
  8036. (setq time (or time (org-read-date nil 'to-time nil nil
  8037. default-time default-input))))
  8038. (when (and org-insert-labeled-timestamps-at-point
  8039. (member what '(scheduled deadline)))
  8040. (insert
  8041. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8042. (org-insert-time-stamp time org-time-was-given
  8043. nil nil nil (list org-end-time-was-given))
  8044. (setq what nil))
  8045. (save-excursion
  8046. (save-restriction
  8047. (let (col list elt ts buffer-invisibility-spec)
  8048. (org-back-to-heading t)
  8049. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8050. (goto-char (match-end 1))
  8051. (setq col (current-column))
  8052. (goto-char (match-end 0))
  8053. (if (eobp) (insert "\n") (forward-char 1))
  8054. (if (and (not (looking-at outline-regexp))
  8055. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8056. "[^\r\n]*"))
  8057. (not (equal (match-string 1) org-clock-string)))
  8058. (narrow-to-region (match-beginning 0) (match-end 0))
  8059. (insert-before-markers "\n")
  8060. (backward-char 1)
  8061. (narrow-to-region (point) (point))
  8062. (and org-adapt-indentation (org-indent-to-column col)))
  8063. ;; Check if we have to remove something.
  8064. (setq list (cons what remove))
  8065. (while list
  8066. (setq elt (pop list))
  8067. (goto-char (point-min))
  8068. (when (or (and (eq elt 'scheduled)
  8069. (re-search-forward org-scheduled-time-regexp nil t))
  8070. (and (eq elt 'deadline)
  8071. (re-search-forward org-deadline-time-regexp nil t))
  8072. (and (eq elt 'closed)
  8073. (re-search-forward org-closed-time-regexp nil t)))
  8074. (replace-match "")
  8075. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8076. (if (looking-at " +") (replace-match ""))))
  8077. (goto-char (point-max))
  8078. (when what
  8079. (insert
  8080. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8081. (cond ((eq what 'scheduled) org-scheduled-string)
  8082. ((eq what 'deadline) org-deadline-string)
  8083. ((eq what 'closed) org-closed-string))
  8084. " ")
  8085. (setq ts (org-insert-time-stamp
  8086. time
  8087. (or org-time-was-given
  8088. (and (eq what 'closed) org-log-done-with-time))
  8089. (eq what 'closed)
  8090. nil nil (list org-end-time-was-given)))
  8091. (end-of-line 1))
  8092. (goto-char (point-min))
  8093. (widen)
  8094. (if (and (looking-at "[ \t]+\n")
  8095. (equal (char-before) ?\n))
  8096. (delete-region (1- (point)) (point-at-eol)))
  8097. ts)))))
  8098. (defvar org-log-note-marker (make-marker))
  8099. (defvar org-log-note-purpose nil)
  8100. (defvar org-log-note-state nil)
  8101. (defvar org-log-note-how nil)
  8102. (defvar org-log-note-extra nil)
  8103. (defvar org-log-note-window-configuration nil)
  8104. (defvar org-log-note-return-to (make-marker))
  8105. (defvar org-log-post-message nil
  8106. "Message to be displayed after a log note has been stored.
  8107. The auto-repeater uses this.")
  8108. (defun org-add-note ()
  8109. "Add a note to the current entry.
  8110. This is done in the same way as adding a state change note."
  8111. (interactive)
  8112. (org-add-log-setup 'note nil 'findpos nil))
  8113. (defvar org-property-end-re)
  8114. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  8115. "Set up the post command hook to take a note.
  8116. If this is about to TODO state change, the new state is expected in STATE.
  8117. When FINDPOS is non-nil, find the correct position for the note in
  8118. the current entry. If not, assume that it can be inserted at point.
  8119. HOW is an indicator what kind of note should be created.
  8120. EXTRA is additional text that will be inserted into the notes buffer."
  8121. (save-restriction
  8122. (save-excursion
  8123. (when findpos
  8124. (org-back-to-heading t)
  8125. (narrow-to-region (point) (save-excursion
  8126. (outline-next-heading) (point)))
  8127. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8128. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8129. "[^\r\n]*\\)?"))
  8130. (goto-char (match-end 0))
  8131. (when (and org-log-state-notes-insert-after-drawers
  8132. (save-excursion
  8133. (forward-line) (looking-at org-drawer-regexp)))
  8134. (progn (forward-line)
  8135. (while (looking-at org-drawer-regexp)
  8136. (goto-char (match-end 0))
  8137. (re-search-forward org-property-end-re (point-max) t)
  8138. (forward-line))
  8139. (forward-line -1)))
  8140. (unless org-log-states-order-reversed
  8141. (and (= (char-after) ?\n) (forward-char 1))
  8142. (org-skip-over-state-notes)
  8143. (skip-chars-backward " \t\n\r")))
  8144. (move-marker org-log-note-marker (point))
  8145. (setq org-log-note-purpose purpose
  8146. org-log-note-state state
  8147. org-log-note-how how
  8148. org-log-note-extra extra)
  8149. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8150. (defun org-skip-over-state-notes ()
  8151. "Skip past the list of State notes in an entry."
  8152. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8153. (while (looking-at "[ \t]*- State")
  8154. (condition-case nil
  8155. (org-next-item)
  8156. (error (org-end-of-item)))))
  8157. (defun org-add-log-note (&optional purpose)
  8158. "Pop up a window for taking a note, and add this note later at point."
  8159. (remove-hook 'post-command-hook 'org-add-log-note)
  8160. (setq org-log-note-window-configuration (current-window-configuration))
  8161. (delete-other-windows)
  8162. (move-marker org-log-note-return-to (point))
  8163. (switch-to-buffer (marker-buffer org-log-note-marker))
  8164. (goto-char org-log-note-marker)
  8165. (org-switch-to-buffer-other-window "*Org Note*")
  8166. (erase-buffer)
  8167. (if (memq org-log-note-how '(time state))
  8168. (let (current-prefix-arg) (org-store-log-note))
  8169. (let ((org-inhibit-startup t)) (org-mode))
  8170. (insert (format "# Insert note for %s.
  8171. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8172. (cond
  8173. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8174. ((eq org-log-note-purpose 'done) "closed todo item")
  8175. ((eq org-log-note-purpose 'state)
  8176. (format "state change to \"%s\"" org-log-note-state))
  8177. ((eq org-log-note-purpose 'note)
  8178. "this entry")
  8179. (t (error "This should not happen")))))
  8180. (if org-log-note-extra (insert org-log-note-extra))
  8181. (org-set-local 'org-finish-function 'org-store-log-note)))
  8182. (defvar org-note-abort nil) ; dynamically scoped
  8183. (defun org-store-log-note ()
  8184. "Finish taking a log note, and insert it to where it belongs."
  8185. (let ((txt (buffer-string))
  8186. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8187. lines ind)
  8188. (kill-buffer (current-buffer))
  8189. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8190. (setq txt (replace-match "" t t txt)))
  8191. (if (string-match "\\s-+\\'" txt)
  8192. (setq txt (replace-match "" t t txt)))
  8193. (setq lines (org-split-string txt "\n"))
  8194. (when (and note (string-match "\\S-" note))
  8195. (setq note
  8196. (org-replace-escapes
  8197. note
  8198. (list (cons "%u" (user-login-name))
  8199. (cons "%U" user-full-name)
  8200. (cons "%t" (format-time-string
  8201. (org-time-stamp-format 'long 'inactive)
  8202. (current-time)))
  8203. (cons "%s" (if org-log-note-state
  8204. (concat "\"" org-log-note-state "\"")
  8205. "")))))
  8206. (if lines (setq note (concat note " \\\\")))
  8207. (push note lines))
  8208. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8209. (when lines
  8210. (save-excursion
  8211. (set-buffer (marker-buffer org-log-note-marker))
  8212. (save-excursion
  8213. (goto-char org-log-note-marker)
  8214. (move-marker org-log-note-marker nil)
  8215. (end-of-line 1)
  8216. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8217. (indent-relative nil)
  8218. (insert "- " (pop lines))
  8219. (org-indent-line-function)
  8220. (beginning-of-line 1)
  8221. (looking-at "[ \t]*")
  8222. (setq ind (concat (match-string 0) " "))
  8223. (end-of-line 1)
  8224. (while lines (insert "\n" ind (pop lines)))))))
  8225. (set-window-configuration org-log-note-window-configuration)
  8226. (with-current-buffer (marker-buffer org-log-note-return-to)
  8227. (goto-char org-log-note-return-to))
  8228. (move-marker org-log-note-return-to nil)
  8229. (and org-log-post-message (message "%s" org-log-post-message)))
  8230. (defun org-sparse-tree (&optional arg)
  8231. "Create a sparse tree, prompt for the details.
  8232. This command can create sparse trees. You first need to select the type
  8233. of match used to create the tree:
  8234. t Show entries with a specific TODO keyword.
  8235. T Show entries selected by a tags match.
  8236. p Enter a property name and its value (both with completion on existing
  8237. names/values) and show entries with that property.
  8238. r Show entries matching a regular expression
  8239. d Show deadlines due within `org-deadline-warning-days'."
  8240. (interactive "P")
  8241. (let (ans kwd value)
  8242. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8243. (setq ans (read-char-exclusive))
  8244. (cond
  8245. ((equal ans ?d)
  8246. (call-interactively 'org-check-deadlines))
  8247. ((equal ans ?b)
  8248. (call-interactively 'org-check-before-date))
  8249. ((equal ans ?t)
  8250. (org-show-todo-tree '(4)))
  8251. ((equal ans ?T)
  8252. (call-interactively 'org-tags-sparse-tree))
  8253. ((member ans '(?p ?P))
  8254. (setq kwd (org-ido-completing-read "Property: "
  8255. (mapcar 'list (org-buffer-property-keys))))
  8256. (setq value (org-ido-completing-read "Value: "
  8257. (mapcar 'list (org-property-values kwd))))
  8258. (unless (string-match "\\`{.*}\\'" value)
  8259. (setq value (concat "\"" value "\"")))
  8260. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8261. ((member ans '(?r ?R ?/))
  8262. (call-interactively 'org-occur))
  8263. (t (error "No such sparse tree command \"%c\"" ans)))))
  8264. (defvar org-occur-highlights nil
  8265. "List of overlays used for occur matches.")
  8266. (make-variable-buffer-local 'org-occur-highlights)
  8267. (defvar org-occur-parameters nil
  8268. "Parameters of the active org-occur calls.
  8269. This is a list, each call to org-occur pushes as cons cell,
  8270. containing the regular expression and the callback, onto the list.
  8271. The list can contain several entries if `org-occur' has been called
  8272. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8273. will only contain one set of parameters. When the highlights are
  8274. removed (for example with `C-c C-c', or with the next edit (depending
  8275. on `org-remove-highlights-with-change'), this variable is emptied
  8276. as well.")
  8277. (make-variable-buffer-local 'org-occur-parameters)
  8278. (defun org-occur (regexp &optional keep-previous callback)
  8279. "Make a compact tree which shows all matches of REGEXP.
  8280. The tree will show the lines where the regexp matches, and all higher
  8281. headlines above the match. It will also show the heading after the match,
  8282. to make sure editing the matching entry is easy.
  8283. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8284. call to `org-occur' will be kept, to allow stacking of calls to this
  8285. command.
  8286. If CALLBACK is non-nil, it is a function which is called to confirm
  8287. that the match should indeed be shown."
  8288. (interactive "sRegexp: \nP")
  8289. (unless keep-previous
  8290. (org-remove-occur-highlights nil nil t))
  8291. (push (cons regexp callback) org-occur-parameters)
  8292. (let ((cnt 0))
  8293. (save-excursion
  8294. (goto-char (point-min))
  8295. (if (or (not keep-previous) ; do not want to keep
  8296. (not org-occur-highlights)) ; no previous matches
  8297. ;; hide everything
  8298. (org-overview))
  8299. (while (re-search-forward regexp nil t)
  8300. (when (or (not callback)
  8301. (save-match-data (funcall callback)))
  8302. (setq cnt (1+ cnt))
  8303. (when org-highlight-sparse-tree-matches
  8304. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8305. (org-show-context 'occur-tree))))
  8306. (when org-remove-highlights-with-change
  8307. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8308. nil 'local))
  8309. (unless org-sparse-tree-open-archived-trees
  8310. (org-hide-archived-subtrees (point-min) (point-max)))
  8311. (run-hooks 'org-occur-hook)
  8312. (if (interactive-p)
  8313. (message "%d match(es) for regexp %s" cnt regexp))
  8314. cnt))
  8315. (defun org-show-context (&optional key)
  8316. "Make sure point and context and visible.
  8317. How much context is shown depends upon the variables
  8318. `org-show-hierarchy-above', `org-show-following-heading'. and
  8319. `org-show-siblings'."
  8320. (let ((heading-p (org-on-heading-p t))
  8321. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8322. (following-p (org-get-alist-option org-show-following-heading key))
  8323. (entry-p (org-get-alist-option org-show-entry-below key))
  8324. (siblings-p (org-get-alist-option org-show-siblings key)))
  8325. (catch 'exit
  8326. ;; Show heading or entry text
  8327. (if (and heading-p (not entry-p))
  8328. (org-flag-heading nil) ; only show the heading
  8329. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8330. (org-show-hidden-entry))) ; show entire entry
  8331. (when following-p
  8332. ;; Show next sibling, or heading below text
  8333. (save-excursion
  8334. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8335. (org-flag-heading nil))))
  8336. (when siblings-p (org-show-siblings))
  8337. (when hierarchy-p
  8338. ;; show all higher headings, possibly with siblings
  8339. (save-excursion
  8340. (while (and (condition-case nil
  8341. (progn (org-up-heading-all 1) t)
  8342. (error nil))
  8343. (not (bobp)))
  8344. (org-flag-heading nil)
  8345. (when siblings-p (org-show-siblings))))))))
  8346. (defun org-reveal (&optional siblings)
  8347. "Show current entry, hierarchy above it, and the following headline.
  8348. This can be used to show a consistent set of context around locations
  8349. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8350. not t for the search context.
  8351. With optional argument SIBLINGS, on each level of the hierarchy all
  8352. siblings are shown. This repairs the tree structure to what it would
  8353. look like when opened with hierarchical calls to `org-cycle'."
  8354. (interactive "P")
  8355. (let ((org-show-hierarchy-above t)
  8356. (org-show-following-heading t)
  8357. (org-show-siblings (if siblings t org-show-siblings)))
  8358. (org-show-context nil)))
  8359. (defun org-highlight-new-match (beg end)
  8360. "Highlight from BEG to END and mark the highlight is an occur headline."
  8361. (let ((ov (org-make-overlay beg end)))
  8362. (org-overlay-put ov 'face 'secondary-selection)
  8363. (push ov org-occur-highlights)))
  8364. (defun org-remove-occur-highlights (&optional beg end noremove)
  8365. "Remove the occur highlights from the buffer.
  8366. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8367. from the `before-change-functions' in the current buffer."
  8368. (interactive)
  8369. (unless org-inhibit-highlight-removal
  8370. (mapc 'org-delete-overlay org-occur-highlights)
  8371. (setq org-occur-highlights nil)
  8372. (setq org-occur-parameters nil)
  8373. (unless noremove
  8374. (remove-hook 'before-change-functions
  8375. 'org-remove-occur-highlights 'local))))
  8376. ;;;; Priorities
  8377. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8378. "Regular expression matching the priority indicator.")
  8379. (defvar org-remove-priority-next-time nil)
  8380. (defun org-priority-up ()
  8381. "Increase the priority of the current item."
  8382. (interactive)
  8383. (org-priority 'up))
  8384. (defun org-priority-down ()
  8385. "Decrease the priority of the current item."
  8386. (interactive)
  8387. (org-priority 'down))
  8388. (defun org-priority (&optional action)
  8389. "Change the priority of an item by ARG.
  8390. ACTION can be `set', `up', `down', or a character."
  8391. (interactive)
  8392. (setq action (or action 'set))
  8393. (let (current new news have remove)
  8394. (save-excursion
  8395. (org-back-to-heading)
  8396. (if (looking-at org-priority-regexp)
  8397. (setq current (string-to-char (match-string 2))
  8398. have t)
  8399. (setq current org-default-priority))
  8400. (cond
  8401. ((or (eq action 'set)
  8402. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8403. (if (not (eq action 'set))
  8404. (setq new action)
  8405. (message "Priority %c-%c, SPC to remove: "
  8406. org-highest-priority org-lowest-priority)
  8407. (setq new (read-char-exclusive)))
  8408. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8409. (= (upcase org-lowest-priority) org-lowest-priority))
  8410. (setq new (upcase new)))
  8411. (cond ((equal new ?\ ) (setq remove t))
  8412. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8413. (error "Priority must be between `%c' and `%c'"
  8414. org-highest-priority org-lowest-priority))))
  8415. ((eq action 'up)
  8416. (if (and (not have) (eq last-command this-command))
  8417. (setq new org-lowest-priority)
  8418. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8419. org-default-priority (1- current)))))
  8420. ((eq action 'down)
  8421. (if (and (not have) (eq last-command this-command))
  8422. (setq new org-highest-priority)
  8423. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8424. org-default-priority (1+ current)))))
  8425. (t (error "Invalid action")))
  8426. (if (or (< (upcase new) org-highest-priority)
  8427. (> (upcase new) org-lowest-priority))
  8428. (setq remove t))
  8429. (setq news (format "%c" new))
  8430. (if have
  8431. (if remove
  8432. (replace-match "" t t nil 1)
  8433. (replace-match news t t nil 2))
  8434. (if remove
  8435. (error "No priority cookie found in line")
  8436. (looking-at org-todo-line-regexp)
  8437. (if (match-end 2)
  8438. (progn
  8439. (goto-char (match-end 2))
  8440. (insert " [#" news "]"))
  8441. (goto-char (match-beginning 3))
  8442. (insert "[#" news "] ")))))
  8443. (org-preserve-lc (org-set-tags nil 'align))
  8444. (if remove
  8445. (message "Priority removed")
  8446. (message "Priority of current item set to %s" news))))
  8447. (defun org-get-priority (s)
  8448. "Find priority cookie and return priority."
  8449. (save-match-data
  8450. (if (not (string-match org-priority-regexp s))
  8451. (* 1000 (- org-lowest-priority org-default-priority))
  8452. (* 1000 (- org-lowest-priority
  8453. (string-to-char (match-string 2 s)))))))
  8454. ;;;; Tags
  8455. (defvar org-agenda-archives-mode)
  8456. (defun org-scan-tags (action matcher &optional todo-only)
  8457. "Scan headline tags with inheritance and produce output ACTION.
  8458. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8459. or `agenda' to produce an entry list for an agenda view. It can also be
  8460. a Lisp form or a function that should be called at each matched headline, in
  8461. this case the return value is a list of all return values from these calls.
  8462. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8463. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8464. only lines with a TODO keyword are included in the output."
  8465. (require 'org-agenda)
  8466. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8467. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8468. (org-re
  8469. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8470. (props (list 'face 'default
  8471. 'done-face 'org-done
  8472. 'undone-face 'default
  8473. 'mouse-face 'highlight
  8474. 'org-not-done-regexp org-not-done-regexp
  8475. 'org-todo-regexp org-todo-regexp
  8476. 'keymap org-agenda-keymap
  8477. 'help-echo
  8478. (format "mouse-2 or RET jump to org file %s"
  8479. (abbreviate-file-name
  8480. (or (buffer-file-name (buffer-base-buffer))
  8481. (buffer-name (buffer-base-buffer)))))))
  8482. (case-fold-search nil)
  8483. lspos tags tags-list
  8484. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8485. (llast 0) rtn rtn1 level category i txt
  8486. todo marker entry priority)
  8487. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8488. (setq action (list 'lambda nil action)))
  8489. (save-excursion
  8490. (goto-char (point-min))
  8491. (when (eq action 'sparse-tree)
  8492. (org-overview)
  8493. (org-remove-occur-highlights))
  8494. (while (re-search-forward re nil t)
  8495. (catch :skip
  8496. (setq todo (if (match-end 1) (match-string 2))
  8497. tags (if (match-end 4) (match-string 4)))
  8498. (goto-char (setq lspos (1+ (match-beginning 0))))
  8499. (setq level (org-reduced-level (funcall outline-level))
  8500. category (org-get-category))
  8501. (setq i llast llast level)
  8502. ;; remove tag lists from same and sublevels
  8503. (while (>= i level)
  8504. (when (setq entry (assoc i tags-alist))
  8505. (setq tags-alist (delete entry tags-alist)))
  8506. (setq i (1- i)))
  8507. ;; add the next tags
  8508. (when tags
  8509. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8510. tags-alist
  8511. (cons (cons level tags) tags-alist)))
  8512. ;; compile tags for current headline
  8513. (setq tags-list
  8514. (if org-use-tag-inheritance
  8515. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8516. tags))
  8517. (when org-use-tag-inheritance
  8518. (setcdr (car tags-alist)
  8519. (mapcar (lambda (x)
  8520. (setq x (copy-sequence x))
  8521. (org-add-prop-inherited x))
  8522. (cdar tags-alist))))
  8523. (when (and tags org-use-tag-inheritance
  8524. (not (eq t org-use-tag-inheritance)))
  8525. ;; selective inheritance, remove uninherited ones
  8526. (setcdr (car tags-alist)
  8527. (org-remove-uniherited-tags (cdar tags-alist))))
  8528. (when (and (or (not todo-only)
  8529. (and (member todo org-not-done-keywords)
  8530. (or (not org-agenda-tags-todo-honor-ignore-options)
  8531. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8532. (let ((case-fold-search t)) (eval matcher))
  8533. (or
  8534. (not (member org-archive-tag tags-list))
  8535. ;; we have an archive tag, should we use this anyway?
  8536. (or (not org-agenda-skip-archived-trees)
  8537. (and (eq action 'agenda) org-agenda-archives-mode))))
  8538. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8539. ;; select this headline
  8540. (cond
  8541. ((eq action 'sparse-tree)
  8542. (and org-highlight-sparse-tree-matches
  8543. (org-get-heading) (match-end 0)
  8544. (org-highlight-new-match
  8545. (match-beginning 0) (match-beginning 1)))
  8546. (org-show-context 'tags-tree))
  8547. ((eq action 'agenda)
  8548. (setq txt (org-format-agenda-item
  8549. ""
  8550. (concat
  8551. (if org-tags-match-list-sublevels
  8552. (make-string (1- level) ?.) "")
  8553. (org-get-heading))
  8554. category (org-get-tags-at))
  8555. priority (org-get-priority txt))
  8556. (goto-char lspos)
  8557. (setq marker (org-agenda-new-marker))
  8558. (org-add-props txt props
  8559. 'org-marker marker 'org-hd-marker marker 'org-category category
  8560. 'priority priority 'type "tagsmatch")
  8561. (push txt rtn))
  8562. ((functionp action)
  8563. (save-excursion
  8564. (setq rtn1 (funcall action))
  8565. (push rtn1 rtn))
  8566. (goto-char (point-at-eol)))
  8567. (t (error "Invalid action")))
  8568. ;; if we are to skip sublevels, jump to end of subtree
  8569. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8570. (when (and (eq action 'sparse-tree)
  8571. (not org-sparse-tree-open-archived-trees))
  8572. (org-hide-archived-subtrees (point-min) (point-max)))
  8573. (nreverse rtn)))
  8574. (defun org-remove-uniherited-tags (tags)
  8575. "Remove all tags that are not inherited from the list TAGS."
  8576. (cond
  8577. ((eq org-use-tag-inheritance t)
  8578. (if org-tags-exclude-from-inheritance
  8579. (org-delete-all org-tags-exclude-from-inheritance tags)
  8580. tags))
  8581. ((not org-use-tag-inheritance) nil)
  8582. ((stringp org-use-tag-inheritance)
  8583. (delq nil (mapcar
  8584. (lambda (x)
  8585. (if (and (string-match org-use-tag-inheritance x)
  8586. (not (member x org-tags-exclude-from-inheritance)))
  8587. x nil))
  8588. tags)))
  8589. ((listp org-use-tag-inheritance)
  8590. (delq nil (mapcar
  8591. (lambda (x)
  8592. (if (member x org-use-tag-inheritance) x nil))
  8593. tags)))))
  8594. (defvar todo-only) ;; dynamically scoped
  8595. (defun org-tags-sparse-tree (&optional todo-only match)
  8596. "Create a sparse tree according to tags string MATCH.
  8597. MATCH can contain positive and negative selection of tags, like
  8598. \"+WORK+URGENT-WITHBOSS\".
  8599. If optional argument TODO-ONLY is non-nil, only select lines that are
  8600. also TODO lines."
  8601. (interactive "P")
  8602. (org-prepare-agenda-buffers (list (current-buffer)))
  8603. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8604. (defvar org-cached-props nil)
  8605. (defun org-cached-entry-get (pom property)
  8606. (if (or (eq t org-use-property-inheritance)
  8607. (and (stringp org-use-property-inheritance)
  8608. (string-match org-use-property-inheritance property))
  8609. (and (listp org-use-property-inheritance)
  8610. (member property org-use-property-inheritance)))
  8611. ;; Caching is not possible, check it directly
  8612. (org-entry-get pom property 'inherit)
  8613. ;; Get all properties, so that we can do complicated checks easily
  8614. (cdr (assoc property (or org-cached-props
  8615. (setq org-cached-props
  8616. (org-entry-properties pom)))))))
  8617. (defun org-global-tags-completion-table (&optional files)
  8618. "Return the list of all tags in all agenda buffer/files."
  8619. (save-excursion
  8620. (org-uniquify
  8621. (delq nil
  8622. (apply 'append
  8623. (mapcar
  8624. (lambda (file)
  8625. (set-buffer (find-file-noselect file))
  8626. (append (org-get-buffer-tags)
  8627. (mapcar (lambda (x) (if (stringp (car-safe x))
  8628. (list (car-safe x)) nil))
  8629. org-tag-alist)))
  8630. (if (and files (car files))
  8631. files
  8632. (org-agenda-files))))))))
  8633. (defun org-make-tags-matcher (match)
  8634. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8635. ;; todo-only is scoped dynamically into this function, and the function
  8636. ;; may change it if the matcher asks for it.
  8637. (unless match
  8638. ;; Get a new match request, with completion
  8639. (let ((org-last-tags-completion-table
  8640. (org-global-tags-completion-table)))
  8641. (setq match (org-completing-read
  8642. "Match: " 'org-tags-completion-function nil nil nil
  8643. 'org-tags-history))))
  8644. ;; Parse the string and create a lisp form
  8645. (let ((match0 match)
  8646. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8647. minus tag mm
  8648. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8649. orterms term orlist re-p str-p level-p level-op time-p
  8650. prop-p pn pv po cat-p gv rest)
  8651. (if (string-match "/+" match)
  8652. ;; match contains also a todo-matching request
  8653. (progn
  8654. (setq tagsmatch (substring match 0 (match-beginning 0))
  8655. todomatch (substring match (match-end 0)))
  8656. (if (string-match "^!" todomatch)
  8657. (setq todo-only t todomatch (substring todomatch 1)))
  8658. (if (string-match "^\\s-*$" todomatch)
  8659. (setq todomatch nil)))
  8660. ;; only matching tags
  8661. (setq tagsmatch match todomatch nil))
  8662. ;; Make the tags matcher
  8663. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8664. (setq tagsmatcher t)
  8665. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8666. (while (setq term (pop orterms))
  8667. (while (and (equal (substring term -1) "\\") orterms)
  8668. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8669. (while (string-match re term)
  8670. (setq rest (substring term (match-end 0))
  8671. minus (and (match-end 1)
  8672. (equal (match-string 1 term) "-"))
  8673. tag (match-string 2 term)
  8674. re-p (equal (string-to-char tag) ?{)
  8675. level-p (match-end 4)
  8676. prop-p (match-end 5)
  8677. mm (cond
  8678. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8679. (level-p
  8680. (setq level-op (org-op-to-function (match-string 3 term)))
  8681. `(,level-op level ,(string-to-number
  8682. (match-string 4 term))))
  8683. (prop-p
  8684. (setq pn (match-string 5 term)
  8685. po (match-string 6 term)
  8686. pv (match-string 7 term)
  8687. cat-p (equal pn "CATEGORY")
  8688. re-p (equal (string-to-char pv) ?{)
  8689. str-p (equal (string-to-char pv) ?\")
  8690. time-p (save-match-data
  8691. (string-match "^\"[[<].*[]>]\"$" pv))
  8692. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8693. (if time-p (setq pv (org-matcher-time pv)))
  8694. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8695. (cond
  8696. ((equal pn "CATEGORY")
  8697. (setq gv '(get-text-property (point) 'org-category)))
  8698. ((equal pn "TODO")
  8699. (setq gv 'todo))
  8700. (t
  8701. (setq gv `(org-cached-entry-get nil ,pn))))
  8702. (if re-p
  8703. (if (eq po 'org<>)
  8704. `(not (string-match ,pv (or ,gv "")))
  8705. `(string-match ,pv (or ,gv "")))
  8706. (if str-p
  8707. `(,po (or ,gv "") ,pv)
  8708. `(,po (string-to-number (or ,gv ""))
  8709. ,(string-to-number pv) ))))
  8710. (t `(member ,(downcase tag) tags-list)))
  8711. mm (if minus (list 'not mm) mm)
  8712. term rest)
  8713. (push mm tagsmatcher))
  8714. (push (if (> (length tagsmatcher) 1)
  8715. (cons 'and tagsmatcher)
  8716. (car tagsmatcher))
  8717. orlist)
  8718. (setq tagsmatcher nil))
  8719. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8720. (setq tagsmatcher
  8721. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8722. ;; Make the todo matcher
  8723. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8724. (setq todomatcher t)
  8725. (setq orterms (org-split-string todomatch "|") orlist nil)
  8726. (while (setq term (pop orterms))
  8727. (while (string-match re term)
  8728. (setq minus (and (match-end 1)
  8729. (equal (match-string 1 term) "-"))
  8730. kwd (match-string 2 term)
  8731. re-p (equal (string-to-char kwd) ?{)
  8732. term (substring term (match-end 0))
  8733. mm (if re-p
  8734. `(string-match ,(substring kwd 1 -1) todo)
  8735. (list 'equal 'todo kwd))
  8736. mm (if minus (list 'not mm) mm))
  8737. (push mm todomatcher))
  8738. (push (if (> (length todomatcher) 1)
  8739. (cons 'and todomatcher)
  8740. (car todomatcher))
  8741. orlist)
  8742. (setq todomatcher nil))
  8743. (setq todomatcher (if (> (length orlist) 1)
  8744. (cons 'or orlist) (car orlist))))
  8745. ;; Return the string and lisp forms of the matcher
  8746. (setq matcher (if todomatcher
  8747. (list 'and tagsmatcher todomatcher)
  8748. tagsmatcher))
  8749. (cons match0 matcher)))
  8750. (defun org-op-to-function (op &optional stringp)
  8751. "Turn an operator into the appropriate function."
  8752. (setq op
  8753. (cond
  8754. ((equal op "<" ) '(< string< org-time<))
  8755. ((equal op ">" ) '(> org-string> org-time>))
  8756. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8757. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8758. ((member op '("=" "==")) '(= string= org-time=))
  8759. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8760. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8761. (defun org<> (a b) (not (= a b)))
  8762. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8763. (defun org-string>= (a b) (not (string< a b)))
  8764. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8765. (defun org-string<> (a b) (not (string= a b)))
  8766. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  8767. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  8768. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  8769. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  8770. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  8771. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  8772. (defun org-2ft (s)
  8773. "Convert S to a floating point time.
  8774. If S is already a number, just return it. If it is a string, parse
  8775. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  8776. (cond
  8777. ((numberp s) s)
  8778. ((stringp s)
  8779. (condition-case nil
  8780. (float-time (apply 'encode-time (org-parse-time-string s)))
  8781. (error 0.)))
  8782. (t 0.)))
  8783. (defun org-time-today ()
  8784. "Time in seconds today at 0:00.
  8785. Returns the float number of seconds since the beginning of the
  8786. epoch to the beginning of today (00:00)."
  8787. (float-time (apply 'encode-time
  8788. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8789. (defun org-matcher-time (s)
  8790. "Interpret a time comparison value."
  8791. (save-match-data
  8792. (cond
  8793. ((string= s "<now>") (float-time))
  8794. ((string= s "<today>") (org-time-today))
  8795. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8796. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8797. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8798. (+ (org-time-today)
  8799. (* (string-to-number (match-string 1 s))
  8800. (cdr (assoc (match-string 2 s)
  8801. '(("d" . 86400.0) ("w" . 604800.0)
  8802. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8803. (t (org-2ft s)))))
  8804. (defun org-match-any-p (re list)
  8805. "Does re match any element of list?"
  8806. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8807. (delq nil list))
  8808. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8809. (defvar org-tags-overlay (org-make-overlay 1 1))
  8810. (org-detach-overlay org-tags-overlay)
  8811. (defun org-get-local-tags-at (&optional pos)
  8812. "Get a list of tags defined in the current headline."
  8813. (org-get-tags-at pos 'local))
  8814. (defun org-get-local-tags ()
  8815. "Get a list of tags defined in the current headline."
  8816. (org-get-tags-at nil 'local))
  8817. (defun org-get-tags-at (&optional pos local)
  8818. "Get a list of all headline tags applicable at POS.
  8819. POS defaults to point. If tags are inherited, the list contains
  8820. the targets in the same sequence as the headlines appear, i.e.
  8821. the tags of the current headline come last.
  8822. When LOCAL is non-nil, only return tags from the current headline,
  8823. ignore inherited ones."
  8824. (interactive)
  8825. (let (tags ltags lastpos parent)
  8826. (save-excursion
  8827. (save-restriction
  8828. (widen)
  8829. (goto-char (or pos (point)))
  8830. (save-match-data
  8831. (catch 'done
  8832. (condition-case nil
  8833. (progn
  8834. (org-back-to-heading t)
  8835. (while (not (equal lastpos (point)))
  8836. (setq lastpos (point))
  8837. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8838. (setq ltags (org-split-string
  8839. (org-match-string-no-properties 1) ":"))
  8840. (when parent
  8841. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8842. (setq tags (append
  8843. (if parent
  8844. (org-remove-uniherited-tags ltags)
  8845. ltags)
  8846. tags)))
  8847. (or org-use-tag-inheritance (throw 'done t))
  8848. (if local (throw 'done t))
  8849. (org-up-heading-all 1)
  8850. (setq parent t)))
  8851. (error nil)))))
  8852. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8853. (defun org-add-prop-inherited (s)
  8854. (add-text-properties 0 (length s) '(inherited t) s)
  8855. s)
  8856. (defun org-toggle-tag (tag &optional onoff)
  8857. "Toggle the tag TAG for the current line.
  8858. If ONOFF is `on' or `off', don't toggle but set to this state."
  8859. (let (res current)
  8860. (save-excursion
  8861. (org-back-to-heading t)
  8862. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8863. (point-at-eol) t)
  8864. (progn
  8865. (setq current (match-string 1))
  8866. (replace-match ""))
  8867. (setq current ""))
  8868. (setq current (nreverse (org-split-string current ":")))
  8869. (cond
  8870. ((eq onoff 'on)
  8871. (setq res t)
  8872. (or (member tag current) (push tag current)))
  8873. ((eq onoff 'off)
  8874. (or (not (member tag current)) (setq current (delete tag current))))
  8875. (t (if (member tag current)
  8876. (setq current (delete tag current))
  8877. (setq res t)
  8878. (push tag current))))
  8879. (end-of-line 1)
  8880. (if current
  8881. (progn
  8882. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8883. (org-set-tags nil t))
  8884. (delete-horizontal-space))
  8885. (run-hooks 'org-after-tags-change-hook))
  8886. res))
  8887. (defun org-align-tags-here (to-col)
  8888. ;; Assumes that this is a headline
  8889. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8890. (beginning-of-line 1)
  8891. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8892. (< pos (match-beginning 2)))
  8893. (progn
  8894. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8895. (goto-char (match-beginning 1))
  8896. (insert " ")
  8897. (delete-region (point) (1+ (match-beginning 2)))
  8898. (setq ncol (max (1+ (current-column))
  8899. (1+ col)
  8900. (if (> to-col 0)
  8901. to-col
  8902. (- (abs to-col) tags-l))))
  8903. (setq p (point))
  8904. (insert (make-string (- ncol (current-column)) ?\ ))
  8905. (setq ncol (current-column))
  8906. (when indent-tabs-mode (tabify p (point-at-eol)))
  8907. (org-move-to-column (min ncol col) t))
  8908. (goto-char pos))))
  8909. (defun org-set-tags-command (&optional arg just-align)
  8910. "Call the set-tags command for the current entry."
  8911. (interactive "P")
  8912. (if (org-on-heading-p)
  8913. (org-set-tags arg just-align)
  8914. (save-excursion
  8915. (org-back-to-heading t)
  8916. (org-set-tags arg just-align))))
  8917. (defun org-set-tags (&optional arg just-align)
  8918. "Set the tags for the current headline.
  8919. With prefix ARG, realign all tags in headings in the current buffer."
  8920. (interactive "P")
  8921. (let* ((re (concat "^" outline-regexp))
  8922. (current (org-get-tags-string))
  8923. (col (current-column))
  8924. (org-setting-tags t)
  8925. table current-tags inherited-tags ; computed below when needed
  8926. tags p0 c0 c1 rpl)
  8927. (if arg
  8928. (save-excursion
  8929. (goto-char (point-min))
  8930. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8931. (while (re-search-forward re nil t)
  8932. (org-set-tags nil t)
  8933. (end-of-line 1)))
  8934. (message "All tags realigned to column %d" org-tags-column))
  8935. (if just-align
  8936. (setq tags current)
  8937. ;; Get a new set of tags from the user
  8938. (save-excursion
  8939. (setq table (or org-tag-alist (org-get-buffer-tags))
  8940. org-last-tags-completion-table table
  8941. current-tags (org-split-string current ":")
  8942. inherited-tags (nreverse
  8943. (nthcdr (length current-tags)
  8944. (nreverse (org-get-tags-at))))
  8945. tags
  8946. (if (or (eq t org-use-fast-tag-selection)
  8947. (and org-use-fast-tag-selection
  8948. (delq nil (mapcar 'cdr table))))
  8949. (org-fast-tag-selection
  8950. current-tags inherited-tags table
  8951. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8952. (let ((org-add-colon-after-tag-completion t))
  8953. (org-trim
  8954. (org-without-partial-completion
  8955. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8956. nil nil current 'org-tags-history)))))))
  8957. (while (string-match "[-+&]+" tags)
  8958. ;; No boolean logic, just a list
  8959. (setq tags (replace-match ":" t t tags))))
  8960. (if (string-match "\\`[\t ]*\\'" tags)
  8961. (setq tags "")
  8962. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8963. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8964. ;; Insert new tags at the correct column
  8965. (beginning-of-line 1)
  8966. (cond
  8967. ((and (equal current "") (equal tags "")))
  8968. ((re-search-forward
  8969. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8970. (point-at-eol) t)
  8971. (if (equal tags "")
  8972. (setq rpl "")
  8973. (goto-char (match-beginning 0))
  8974. (setq c0 (current-column) p0 (point)
  8975. c1 (max (1+ c0) (if (> org-tags-column 0)
  8976. org-tags-column
  8977. (- (- org-tags-column) (length tags))))
  8978. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8979. (replace-match rpl t t)
  8980. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8981. tags)
  8982. (t (error "Tags alignment failed")))
  8983. (org-move-to-column col)
  8984. (unless just-align
  8985. (run-hooks 'org-after-tags-change-hook)))))
  8986. (defun org-change-tag-in-region (beg end tag off)
  8987. "Add or remove TAG for each entry in the region.
  8988. This works in the agenda, and also in an org-mode buffer."
  8989. (interactive
  8990. (list (region-beginning) (region-end)
  8991. (let ((org-last-tags-completion-table
  8992. (if (org-mode-p)
  8993. (org-get-buffer-tags)
  8994. (org-global-tags-completion-table))))
  8995. (org-ido-completing-read
  8996. "Tag: " 'org-tags-completion-function nil nil nil
  8997. 'org-tags-history))
  8998. (progn
  8999. (message "[s]et or [r]emove? ")
  9000. (equal (read-char-exclusive) ?r))))
  9001. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9002. (let ((agendap (equal major-mode 'org-agenda-mode))
  9003. l1 l2 m buf pos newhead (cnt 0))
  9004. (goto-char end)
  9005. (setq l2 (1- (org-current-line)))
  9006. (goto-char beg)
  9007. (setq l1 (org-current-line))
  9008. (loop for l from l1 to l2 do
  9009. (goto-line l)
  9010. (setq m (get-text-property (point) 'org-hd-marker))
  9011. (when (or (and (org-mode-p) (org-on-heading-p))
  9012. (and agendap m))
  9013. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9014. pos (if agendap m (point)))
  9015. (with-current-buffer buf
  9016. (save-excursion
  9017. (save-restriction
  9018. (goto-char pos)
  9019. (setq cnt (1+ cnt))
  9020. (org-toggle-tag tag (if off 'off 'on))
  9021. (setq newhead (org-get-heading)))))
  9022. (and agendap (org-agenda-change-all-lines newhead m))))
  9023. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9024. (defun org-tags-completion-function (string predicate &optional flag)
  9025. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9026. (confirm (lambda (x) (stringp (car x)))))
  9027. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9028. (setq s1 (match-string 1 string)
  9029. s2 (match-string 2 string))
  9030. (setq s1 "" s2 string))
  9031. (cond
  9032. ((eq flag nil)
  9033. ;; try completion
  9034. (setq rtn (try-completion s2 ctable confirm))
  9035. (if (stringp rtn)
  9036. (setq rtn
  9037. (concat s1 s2 (substring rtn (length s2))
  9038. (if (and org-add-colon-after-tag-completion
  9039. (assoc rtn ctable))
  9040. ":" ""))))
  9041. rtn)
  9042. ((eq flag t)
  9043. ;; all-completions
  9044. (all-completions s2 ctable confirm)
  9045. )
  9046. ((eq flag 'lambda)
  9047. ;; exact match?
  9048. (assoc s2 ctable)))
  9049. ))
  9050. (defun org-fast-tag-insert (kwd tags face &optional end)
  9051. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9052. (insert (format "%-12s" (concat kwd ":"))
  9053. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9054. (or end "")))
  9055. (defun org-fast-tag-show-exit (flag)
  9056. (save-excursion
  9057. (goto-line 3)
  9058. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9059. (replace-match ""))
  9060. (when flag
  9061. (end-of-line 1)
  9062. (org-move-to-column (- (window-width) 19) t)
  9063. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9064. (defun org-set-current-tags-overlay (current prefix)
  9065. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9066. (if (featurep 'xemacs)
  9067. (org-overlay-display org-tags-overlay (concat prefix s)
  9068. 'secondary-selection)
  9069. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9070. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9071. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9072. "Fast tag selection with single keys.
  9073. CURRENT is the current list of tags in the headline, INHERITED is the
  9074. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9075. possibly with grouping information. TODO-TABLE is a similar table with
  9076. TODO keywords, should these have keys assigned to them.
  9077. If the keys are nil, a-z are automatically assigned.
  9078. Returns the new tags string, or nil to not change the current settings."
  9079. (let* ((fulltable (append table todo-table))
  9080. (maxlen (apply 'max (mapcar
  9081. (lambda (x)
  9082. (if (stringp (car x)) (string-width (car x)) 0))
  9083. fulltable)))
  9084. (buf (current-buffer))
  9085. (expert (eq org-fast-tag-selection-single-key 'expert))
  9086. (buffer-tags nil)
  9087. (fwidth (+ maxlen 3 1 3))
  9088. (ncol (/ (- (window-width) 4) fwidth))
  9089. (i-face 'org-done)
  9090. (c-face 'org-todo)
  9091. tg cnt e c char c1 c2 ntable tbl rtn
  9092. ov-start ov-end ov-prefix
  9093. (exit-after-next org-fast-tag-selection-single-key)
  9094. (done-keywords org-done-keywords)
  9095. groups ingroup)
  9096. (save-excursion
  9097. (beginning-of-line 1)
  9098. (if (looking-at
  9099. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9100. (setq ov-start (match-beginning 1)
  9101. ov-end (match-end 1)
  9102. ov-prefix "")
  9103. (setq ov-start (1- (point-at-eol))
  9104. ov-end (1+ ov-start))
  9105. (skip-chars-forward "^\n\r")
  9106. (setq ov-prefix
  9107. (concat
  9108. (buffer-substring (1- (point)) (point))
  9109. (if (> (current-column) org-tags-column)
  9110. " "
  9111. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9112. (org-move-overlay org-tags-overlay ov-start ov-end)
  9113. (save-window-excursion
  9114. (if expert
  9115. (set-buffer (get-buffer-create " *Org tags*"))
  9116. (delete-other-windows)
  9117. (split-window-vertically)
  9118. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9119. (erase-buffer)
  9120. (org-set-local 'org-done-keywords done-keywords)
  9121. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9122. (org-fast-tag-insert "Current" current c-face "\n\n")
  9123. (org-fast-tag-show-exit exit-after-next)
  9124. (org-set-current-tags-overlay current ov-prefix)
  9125. (setq tbl fulltable char ?a cnt 0)
  9126. (while (setq e (pop tbl))
  9127. (cond
  9128. ((equal e '(:startgroup))
  9129. (push '() groups) (setq ingroup t)
  9130. (when (not (= cnt 0))
  9131. (setq cnt 0)
  9132. (insert "\n"))
  9133. (insert "{ "))
  9134. ((equal e '(:endgroup))
  9135. (setq ingroup nil cnt 0)
  9136. (insert "}\n"))
  9137. (t
  9138. (setq tg (car e) c2 nil)
  9139. (if (cdr e)
  9140. (setq c (cdr e))
  9141. ;; automatically assign a character.
  9142. (setq c1 (string-to-char
  9143. (downcase (substring
  9144. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9145. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9146. (while (or (rassoc char ntable) (rassoc char table))
  9147. (setq char (1+ char)))
  9148. (setq c2 c1))
  9149. (setq c (or c2 char)))
  9150. (if ingroup (push tg (car groups)))
  9151. (setq tg (org-add-props tg nil 'face
  9152. (cond
  9153. ((not (assoc tg table))
  9154. (org-get-todo-face tg))
  9155. ((member tg current) c-face)
  9156. ((member tg inherited) i-face)
  9157. (t nil))))
  9158. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9159. (insert "[" c "] " tg (make-string
  9160. (- fwidth 4 (length tg)) ?\ ))
  9161. (push (cons tg c) ntable)
  9162. (when (= (setq cnt (1+ cnt)) ncol)
  9163. (insert "\n")
  9164. (if ingroup (insert " "))
  9165. (setq cnt 0)))))
  9166. (setq ntable (nreverse ntable))
  9167. (insert "\n")
  9168. (goto-char (point-min))
  9169. (if (not expert) (org-fit-window-to-buffer))
  9170. (setq rtn
  9171. (catch 'exit
  9172. (while t
  9173. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9174. (if groups " [!] no groups" " [!]groups")
  9175. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9176. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9177. (cond
  9178. ((= c ?\r) (throw 'exit t))
  9179. ((= c ?!)
  9180. (setq groups (not groups))
  9181. (goto-char (point-min))
  9182. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9183. ((= c ?\C-c)
  9184. (if (not expert)
  9185. (org-fast-tag-show-exit
  9186. (setq exit-after-next (not exit-after-next)))
  9187. (setq expert nil)
  9188. (delete-other-windows)
  9189. (split-window-vertically)
  9190. (org-switch-to-buffer-other-window " *Org tags*")
  9191. (org-fit-window-to-buffer)))
  9192. ((or (= c ?\C-g)
  9193. (and (= c ?q) (not (rassoc c ntable))))
  9194. (org-detach-overlay org-tags-overlay)
  9195. (setq quit-flag t))
  9196. ((= c ?\ )
  9197. (setq current nil)
  9198. (if exit-after-next (setq exit-after-next 'now)))
  9199. ((= c ?\t)
  9200. (condition-case nil
  9201. (setq tg (org-ido-completing-read
  9202. "Tag: "
  9203. (or buffer-tags
  9204. (with-current-buffer buf
  9205. (org-get-buffer-tags)))))
  9206. (quit (setq tg "")))
  9207. (when (string-match "\\S-" tg)
  9208. (add-to-list 'buffer-tags (list tg))
  9209. (if (member tg current)
  9210. (setq current (delete tg current))
  9211. (push tg current)))
  9212. (if exit-after-next (setq exit-after-next 'now)))
  9213. ((setq e (rassoc c todo-table) tg (car e))
  9214. (with-current-buffer buf
  9215. (save-excursion (org-todo tg)))
  9216. (if exit-after-next (setq exit-after-next 'now)))
  9217. ((setq e (rassoc c ntable) tg (car e))
  9218. (if (member tg current)
  9219. (setq current (delete tg current))
  9220. (loop for g in groups do
  9221. (if (member tg g)
  9222. (mapc (lambda (x)
  9223. (setq current (delete x current)))
  9224. g)))
  9225. (push tg current))
  9226. (if exit-after-next (setq exit-after-next 'now))))
  9227. ;; Create a sorted list
  9228. (setq current
  9229. (sort current
  9230. (lambda (a b)
  9231. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9232. (if (eq exit-after-next 'now) (throw 'exit t))
  9233. (goto-char (point-min))
  9234. (beginning-of-line 2)
  9235. (delete-region (point) (point-at-eol))
  9236. (org-fast-tag-insert "Current" current c-face)
  9237. (org-set-current-tags-overlay current ov-prefix)
  9238. (while (re-search-forward
  9239. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9240. (setq tg (match-string 1))
  9241. (add-text-properties
  9242. (match-beginning 1) (match-end 1)
  9243. (list 'face
  9244. (cond
  9245. ((member tg current) c-face)
  9246. ((member tg inherited) i-face)
  9247. (t (get-text-property (match-beginning 1) 'face))))))
  9248. (goto-char (point-min)))))
  9249. (org-detach-overlay org-tags-overlay)
  9250. (if rtn
  9251. (mapconcat 'identity current ":")
  9252. nil))))
  9253. (defun org-get-tags-string ()
  9254. "Get the TAGS string in the current headline."
  9255. (unless (org-on-heading-p t)
  9256. (error "Not on a heading"))
  9257. (save-excursion
  9258. (beginning-of-line 1)
  9259. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9260. (org-match-string-no-properties 1)
  9261. "")))
  9262. (defun org-get-tags ()
  9263. "Get the list of tags specified in the current headline."
  9264. (org-split-string (org-get-tags-string) ":"))
  9265. (defun org-get-buffer-tags ()
  9266. "Get a table of all tags used in the buffer, for completion."
  9267. (let (tags)
  9268. (save-excursion
  9269. (goto-char (point-min))
  9270. (while (re-search-forward
  9271. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9272. (when (equal (char-after (point-at-bol 0)) ?*)
  9273. (mapc (lambda (x) (add-to-list 'tags x))
  9274. (org-split-string (org-match-string-no-properties 1) ":")))))
  9275. (mapcar 'list tags)))
  9276. ;;;; The mapping API
  9277. ;;;###autoload
  9278. (defun org-map-entries (func &optional match scope &rest skip)
  9279. "Call FUNC at each headline selected by MATCH in SCOPE.
  9280. FUNC is a function or a lisp form. The function will be called without
  9281. arguments, with the cursor positioned at the beginning of the headline.
  9282. The return values of all calls to the function will be collected and
  9283. returned as a list.
  9284. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9285. Only headlines that are matched by this query will be considered during
  9286. the iteration. When MATCH is nil or t, all headlines will be
  9287. visited by the iteration.
  9288. SCOPE determines the scope of this command. It can be any of:
  9289. nil The current buffer, respecting the restriction if any
  9290. tree The subtree started with the entry at point
  9291. file The current buffer, without restriction
  9292. file-with-archives
  9293. The current buffer, and any archives associated with it
  9294. agenda All agenda files
  9295. agenda-with-archives
  9296. All agenda files with any archive files associated with them
  9297. \(file1 file2 ...)
  9298. If this is a list, all files in the list will be scanned
  9299. The remaining args are treated as settings for the skipping facilities of
  9300. the scanner. The following items can be given here:
  9301. archive skip trees with the archive tag.
  9302. comment skip trees with the COMMENT keyword
  9303. function or Emacs Lisp form:
  9304. will be used as value for `org-agenda-skip-function', so whenever
  9305. the the function returns t, FUNC will not be called for that
  9306. entry and search will continue from the point where the
  9307. function leaves it."
  9308. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9309. (org-agenda-skip-archived-trees (memq 'archive skip))
  9310. (org-agenda-skip-comment-trees (memq 'comment skip))
  9311. (org-agenda-skip-function
  9312. (car (org-delete-all '(comment archive) skip)))
  9313. (org-tags-match-list-sublevels t)
  9314. matcher file res
  9315. org-todo-keywords-for-agenda
  9316. org-done-keywords-for-agenda
  9317. org-todo-keyword-alist-for-agenda
  9318. org-tag-alist-for-agenda)
  9319. (cond
  9320. ((eq match t) (setq matcher t))
  9321. ((eq match nil) (setq matcher t))
  9322. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9323. (save-excursion
  9324. (save-restriction
  9325. (when (eq scope 'tree)
  9326. (org-back-to-heading t)
  9327. (org-narrow-to-subtree)
  9328. (setq scope nil))
  9329. (if (not scope)
  9330. (progn
  9331. (org-prepare-agenda-buffers
  9332. (list (buffer-file-name (current-buffer))))
  9333. (setq res (org-scan-tags func matcher)))
  9334. ;; Get the right scope
  9335. (cond
  9336. ((and scope (listp scope) (symbolp (car scope)))
  9337. (setq scope (eval scope)))
  9338. ((eq scope 'agenda)
  9339. (setq scope (org-agenda-files t)))
  9340. ((eq scope 'agenda-with-archives)
  9341. (setq scope (org-agenda-files t))
  9342. (setq scope (org-add-archive-files scope)))
  9343. ((eq scope 'file)
  9344. (setq scope (list (buffer-file-name))))
  9345. ((eq scope 'file-with-archives)
  9346. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9347. (org-prepare-agenda-buffers scope)
  9348. (while (setq file (pop scope))
  9349. (with-current-buffer (org-find-base-buffer-visiting file)
  9350. (save-excursion
  9351. (save-restriction
  9352. (widen)
  9353. (goto-char (point-min))
  9354. (setq res (append res (org-scan-tags func matcher))))))))))
  9355. res))
  9356. ;;;; Properties
  9357. ;;; Setting and retrieving properties
  9358. (defconst org-special-properties
  9359. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9360. "TIMESTAMP" "TIMESTAMP_IA")
  9361. "The special properties valid in Org-mode.
  9362. These are properties that are not defined in the property drawer,
  9363. but in some other way.")
  9364. (defconst org-default-properties
  9365. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9366. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9367. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9368. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9369. "Some properties that are used by Org-mode for various purposes.
  9370. Being in this list makes sure that they are offered for completion.")
  9371. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9372. "Regular expression matching the first line of a property drawer.")
  9373. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9374. "Regular expression matching the first line of a property drawer.")
  9375. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9376. "Regular expression matching the first line of a property drawer.")
  9377. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9378. "Regular expression matching the first line of a property drawer.")
  9379. (defconst org-property-drawer-re
  9380. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9381. org-property-end-re "\\)\n?")
  9382. "Matches an entire property drawer.")
  9383. (defconst org-clock-drawer-re
  9384. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9385. org-property-end-re "\\)\n?")
  9386. "Matches an entire clock drawer.")
  9387. (defun org-property-action ()
  9388. "Do an action on properties."
  9389. (interactive)
  9390. (let (c)
  9391. (org-at-property-p)
  9392. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9393. (setq c (read-char-exclusive))
  9394. (cond
  9395. ((equal c ?s)
  9396. (call-interactively 'org-set-property))
  9397. ((equal c ?d)
  9398. (call-interactively 'org-delete-property))
  9399. ((equal c ?D)
  9400. (call-interactively 'org-delete-property-globally))
  9401. ((equal c ?c)
  9402. (call-interactively 'org-compute-property-at-point))
  9403. (t (error "No such property action %c" c)))))
  9404. (defun org-at-property-p ()
  9405. "Is the cursor in a property line?"
  9406. ;; FIXME: Does not check if we are actually in the drawer.
  9407. ;; FIXME: also returns true on any drawers.....
  9408. ;; This is used by C-c C-c for property action.
  9409. (save-excursion
  9410. (beginning-of-line 1)
  9411. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9412. (defun org-get-property-block (&optional beg end force)
  9413. "Return the (beg . end) range of the body of the property drawer.
  9414. BEG and END can be beginning and end of subtree, if not given
  9415. they will be found.
  9416. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9417. (catch 'exit
  9418. (save-excursion
  9419. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9420. (end (or end (progn (outline-next-heading) (point)))))
  9421. (goto-char beg)
  9422. (if (re-search-forward org-property-start-re end t)
  9423. (setq beg (1+ (match-end 0)))
  9424. (if force
  9425. (save-excursion
  9426. (org-insert-property-drawer)
  9427. (setq end (progn (outline-next-heading) (point))))
  9428. (throw 'exit nil))
  9429. (goto-char beg)
  9430. (if (re-search-forward org-property-start-re end t)
  9431. (setq beg (1+ (match-end 0)))))
  9432. (if (re-search-forward org-property-end-re end t)
  9433. (setq end (match-beginning 0))
  9434. (or force (throw 'exit nil))
  9435. (goto-char beg)
  9436. (setq end beg)
  9437. (org-indent-line-function)
  9438. (insert ":END:\n"))
  9439. (cons beg end)))))
  9440. (defun org-entry-properties (&optional pom which)
  9441. "Get all properties of the entry at point-or-marker POM.
  9442. This includes the TODO keyword, the tags, time strings for deadline,
  9443. scheduled, and clocking, and any additional properties defined in the
  9444. entry. The return value is an alist, keys may occur multiple times
  9445. if the property key was used several times.
  9446. POM may also be nil, in which case the current entry is used.
  9447. If WHICH is nil or `all', get all properties. If WHICH is
  9448. `special' or `standard', only get that subclass."
  9449. (setq which (or which 'all))
  9450. (org-with-point-at pom
  9451. (let ((clockstr (substring org-clock-string 0 -1))
  9452. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9453. beg end range props sum-props key value string clocksum)
  9454. (save-excursion
  9455. (when (condition-case nil
  9456. (and (org-mode-p) (org-back-to-heading t))
  9457. (error nil))
  9458. (setq beg (point))
  9459. (setq sum-props (get-text-property (point) 'org-summaries))
  9460. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9461. (outline-next-heading)
  9462. (setq end (point))
  9463. (when (memq which '(all special))
  9464. ;; Get the special properties, like TODO and tags
  9465. (goto-char beg)
  9466. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9467. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9468. (when (looking-at org-priority-regexp)
  9469. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9470. (when (and (setq value (org-get-tags-string))
  9471. (string-match "\\S-" value))
  9472. (push (cons "TAGS" value) props))
  9473. (when (setq value (org-get-tags-at))
  9474. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9475. props))
  9476. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9477. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9478. string (if (equal key clockstr)
  9479. (org-no-properties
  9480. (org-trim
  9481. (buffer-substring
  9482. (match-beginning 3) (goto-char (point-at-eol)))))
  9483. (substring (org-match-string-no-properties 3) 1 -1)))
  9484. (unless key
  9485. (if (= (char-after (match-beginning 3)) ?\[)
  9486. (setq key "TIMESTAMP_IA")
  9487. (setq key "TIMESTAMP")))
  9488. (when (or (equal key clockstr) (not (assoc key props)))
  9489. (push (cons key string) props)))
  9490. )
  9491. (when (memq which '(all standard))
  9492. ;; Get the standard properties, like :PORP: ...
  9493. (setq range (org-get-property-block beg end))
  9494. (when range
  9495. (goto-char (car range))
  9496. (while (re-search-forward
  9497. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9498. (cdr range) t)
  9499. (setq key (org-match-string-no-properties 1)
  9500. value (org-trim (or (org-match-string-no-properties 2) "")))
  9501. (unless (member key excluded)
  9502. (push (cons key (or value "")) props)))))
  9503. (if clocksum
  9504. (push (cons "CLOCKSUM"
  9505. (org-columns-number-to-string (/ (float clocksum) 60.)
  9506. 'add_times))
  9507. props))
  9508. (unless (assoc "CATEGORY" props)
  9509. (setq value (or (org-get-category)
  9510. (progn (org-refresh-category-properties)
  9511. (org-get-category))))
  9512. (push (cons "CATEGORY" value) props))
  9513. (append sum-props (nreverse props)))))))
  9514. (defun org-entry-get (pom property &optional inherit)
  9515. "Get value of PROPERTY for entry at point-or-marker POM.
  9516. If INHERIT is non-nil and the entry does not have the property,
  9517. then also check higher levels of the hierarchy.
  9518. If INHERIT is the symbol `selective', use inheritance only if the setting
  9519. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9520. If the property is present but empty, the return value is the empty string.
  9521. If the property is not present at all, nil is returned."
  9522. (org-with-point-at pom
  9523. (if (and inherit (if (eq inherit 'selective)
  9524. (org-property-inherit-p property)
  9525. t))
  9526. (org-entry-get-with-inheritance property)
  9527. (if (member property org-special-properties)
  9528. ;; We need a special property. Use brute force, get all properties.
  9529. (cdr (assoc property (org-entry-properties nil 'special)))
  9530. (let ((range (org-get-property-block)))
  9531. (if (and range
  9532. (goto-char (car range))
  9533. (re-search-forward
  9534. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9535. (cdr range) t))
  9536. ;; Found the property, return it.
  9537. (if (match-end 1)
  9538. (org-match-string-no-properties 1)
  9539. "")))))))
  9540. (defun org-property-or-variable-value (var &optional inherit)
  9541. "Check if there is a property fixing the value of VAR.
  9542. If yes, return this value. If not, return the current value of the variable."
  9543. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9544. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9545. (read prop)
  9546. (symbol-value var))))
  9547. (defun org-entry-delete (pom property)
  9548. "Delete the property PROPERTY from entry at point-or-marker POM."
  9549. (org-with-point-at pom
  9550. (if (member property org-special-properties)
  9551. nil ; cannot delete these properties.
  9552. (let ((range (org-get-property-block)))
  9553. (if (and range
  9554. (goto-char (car range))
  9555. (re-search-forward
  9556. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9557. (cdr range) t))
  9558. (progn
  9559. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9560. t)
  9561. nil)))))
  9562. ;; Multi-values properties are properties that contain multiple values
  9563. ;; These values are assumed to be single words, separated by whitespace.
  9564. (defun org-entry-add-to-multivalued-property (pom property value)
  9565. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9566. (let* ((old (org-entry-get pom property))
  9567. (values (and old (org-split-string old "[ \t]"))))
  9568. (setq value (org-entry-protect-space value))
  9569. (unless (member value values)
  9570. (setq values (cons value values))
  9571. (org-entry-put pom property
  9572. (mapconcat 'identity values " ")))))
  9573. (defun org-entry-remove-from-multivalued-property (pom property value)
  9574. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9575. (let* ((old (org-entry-get pom property))
  9576. (values (and old (org-split-string old "[ \t]"))))
  9577. (setq value (org-entry-protect-space value))
  9578. (when (member value values)
  9579. (setq values (delete value values))
  9580. (org-entry-put pom property
  9581. (mapconcat 'identity values " ")))))
  9582. (defun org-entry-member-in-multivalued-property (pom property value)
  9583. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9584. (let* ((old (org-entry-get pom property))
  9585. (values (and old (org-split-string old "[ \t]"))))
  9586. (setq value (org-entry-protect-space value))
  9587. (member value values)))
  9588. (defun org-entry-get-multivalued-property (pom property)
  9589. "Return a list of values in a multivalued property."
  9590. (let* ((value (org-entry-get pom property))
  9591. (values (and value (org-split-string value "[ \t]"))))
  9592. (mapcar 'org-entry-restore-space values)))
  9593. (defun org-entry-put-multivalued-property (pom property &rest values)
  9594. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9595. VALUES should be a list of strings. Spaces will be protected."
  9596. (org-entry-put pom property
  9597. (mapconcat 'org-entry-protect-space values " "))
  9598. (let* ((value (org-entry-get pom property))
  9599. (values (and value (org-split-string value "[ \t]"))))
  9600. (mapcar 'org-entry-restore-space values)))
  9601. (defun org-entry-protect-space (s)
  9602. "Protect spaces and newline in string S."
  9603. (while (string-match " " s)
  9604. (setq s (replace-match "%20" t t s)))
  9605. (while (string-match "\n" s)
  9606. (setq s (replace-match "%0A" t t s)))
  9607. s)
  9608. (defun org-entry-restore-space (s)
  9609. "Restore spaces and newline in string S."
  9610. (while (string-match "%20" s)
  9611. (setq s (replace-match " " t t s)))
  9612. (while (string-match "%0A" s)
  9613. (setq s (replace-match "\n" t t s)))
  9614. s)
  9615. (defvar org-entry-property-inherited-from (make-marker)
  9616. "Marker pointing to the entry from where a property was inherited.
  9617. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9618. location of the entry where the inheritance search matched. If there was
  9619. no match, the marker will point nowhere.
  9620. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9621. is set.")
  9622. (defun org-entry-get-with-inheritance (property)
  9623. "Get entry property, and search higher levels if not present."
  9624. (move-marker org-entry-property-inherited-from nil)
  9625. (let (tmp)
  9626. (save-excursion
  9627. (save-restriction
  9628. (widen)
  9629. (catch 'ex
  9630. (while t
  9631. (when (setq tmp (org-entry-get nil property))
  9632. (org-back-to-heading t)
  9633. (move-marker org-entry-property-inherited-from (point))
  9634. (throw 'ex tmp))
  9635. (or (org-up-heading-safe) (throw 'ex nil)))))
  9636. (or tmp
  9637. (cdr (assoc property org-file-properties))
  9638. (cdr (assoc property org-global-properties))
  9639. (cdr (assoc property org-global-properties-fixed))))))
  9640. (defun org-entry-put (pom property value)
  9641. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9642. (org-with-point-at pom
  9643. (org-back-to-heading t)
  9644. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9645. range)
  9646. (cond
  9647. ((equal property "TODO")
  9648. (when (and (stringp value) (string-match "\\S-" value)
  9649. (not (member value org-todo-keywords-1)))
  9650. (error "\"%s\" is not a valid TODO state" value))
  9651. (if (or (not value)
  9652. (not (string-match "\\S-" value)))
  9653. (setq value 'none))
  9654. (org-todo value)
  9655. (org-set-tags nil 'align))
  9656. ((equal property "PRIORITY")
  9657. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9658. (string-to-char value) ?\ ))
  9659. (org-set-tags nil 'align))
  9660. ((equal property "SCHEDULED")
  9661. (if (re-search-forward org-scheduled-time-regexp end t)
  9662. (cond
  9663. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9664. ((eq value 'later) (org-timestamp-change 1 'day))
  9665. (t (call-interactively 'org-schedule)))
  9666. (call-interactively 'org-schedule)))
  9667. ((equal property "DEADLINE")
  9668. (if (re-search-forward org-deadline-time-regexp end t)
  9669. (cond
  9670. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9671. ((eq value 'later) (org-timestamp-change 1 'day))
  9672. (t (call-interactively 'org-deadline)))
  9673. (call-interactively 'org-deadline)))
  9674. ((member property org-special-properties)
  9675. (error "The %s property can not yet be set with `org-entry-put'"
  9676. property))
  9677. (t ; a non-special property
  9678. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9679. (setq range (org-get-property-block beg end 'force))
  9680. (goto-char (car range))
  9681. (if (re-search-forward
  9682. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9683. (progn
  9684. (delete-region (match-beginning 1) (match-end 1))
  9685. (goto-char (match-beginning 1)))
  9686. (goto-char (cdr range))
  9687. (insert "\n")
  9688. (backward-char 1)
  9689. (org-indent-line-function)
  9690. (insert ":" property ":"))
  9691. (and value (insert " " value))
  9692. (org-indent-line-function)))))))
  9693. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9694. "Get all property keys in the current buffer.
  9695. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9696. like tags and TODO state.
  9697. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9698. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9699. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9700. formats in the current buffer."
  9701. (let (rtn range cfmt s p)
  9702. (save-excursion
  9703. (save-restriction
  9704. (widen)
  9705. (goto-char (point-min))
  9706. (while (re-search-forward org-property-start-re nil t)
  9707. (setq range (org-get-property-block))
  9708. (goto-char (car range))
  9709. (while (re-search-forward
  9710. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9711. (cdr range) t)
  9712. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9713. (outline-next-heading))))
  9714. (when include-specials
  9715. (setq rtn (append org-special-properties rtn)))
  9716. (when include-defaults
  9717. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9718. (when include-columns
  9719. (save-excursion
  9720. (save-restriction
  9721. (widen)
  9722. (goto-char (point-min))
  9723. (while (re-search-forward
  9724. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9725. nil t)
  9726. (setq cfmt (match-string 2) s 0)
  9727. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9728. cfmt s)
  9729. (setq s (match-end 0)
  9730. p (match-string 1 cfmt))
  9731. (unless (or (equal p "ITEM")
  9732. (member p org-special-properties))
  9733. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9734. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9735. (defun org-property-values (key)
  9736. "Return a list of all values of property KEY."
  9737. (save-excursion
  9738. (save-restriction
  9739. (widen)
  9740. (goto-char (point-min))
  9741. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9742. values)
  9743. (while (re-search-forward re nil t)
  9744. (add-to-list 'values (org-trim (match-string 1))))
  9745. (delete "" values)))))
  9746. (defun org-insert-property-drawer ()
  9747. "Insert a property drawer into the current entry."
  9748. (interactive)
  9749. (org-back-to-heading t)
  9750. (looking-at outline-regexp)
  9751. (let ((indent (- (match-end 0)(match-beginning 0)))
  9752. (beg (point))
  9753. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9754. end hiddenp)
  9755. (outline-next-heading)
  9756. (setq end (point))
  9757. (goto-char beg)
  9758. (while (re-search-forward re end t))
  9759. (setq hiddenp (org-invisible-p))
  9760. (end-of-line 1)
  9761. (and (equal (char-after) ?\n) (forward-char 1))
  9762. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9763. (beginning-of-line 2))
  9764. (org-skip-over-state-notes)
  9765. (skip-chars-backward " \t\n\r")
  9766. (if (eq (char-before) ?*) (forward-char 1))
  9767. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9768. (beginning-of-line 0)
  9769. (org-indent-to-column indent)
  9770. (beginning-of-line 2)
  9771. (org-indent-to-column indent)
  9772. (beginning-of-line 0)
  9773. (if hiddenp
  9774. (save-excursion
  9775. (org-back-to-heading t)
  9776. (hide-entry))
  9777. (org-flag-drawer t))))
  9778. (defun org-set-property (property value)
  9779. "In the current entry, set PROPERTY to VALUE.
  9780. When called interactively, this will prompt for a property name, offering
  9781. completion on existing and default properties. And then it will prompt
  9782. for a value, offering completion either on allowed values (via an inherited
  9783. xxx_ALL property) or on existing values in other instances of this property
  9784. in the current file."
  9785. (interactive
  9786. (let* ((completion-ignore-case t)
  9787. (keys (org-buffer-property-keys nil t t))
  9788. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9789. (prop (if (member prop0 keys)
  9790. prop0
  9791. (or (cdr (assoc (downcase prop0)
  9792. (mapcar (lambda (x) (cons (downcase x) x))
  9793. keys)))
  9794. prop0)))
  9795. (cur (org-entry-get nil prop))
  9796. (allowed (org-property-get-allowed-values nil prop 'table))
  9797. (existing (mapcar 'list (org-property-values prop)))
  9798. (val (if allowed
  9799. (org-completing-read "Value: " allowed nil 'req-match)
  9800. (org-completing-read
  9801. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9802. (concat "[" cur "]") "")
  9803. ": ")
  9804. existing nil nil "" nil cur))))
  9805. (list prop (if (equal val "") cur val))))
  9806. (unless (equal (org-entry-get nil property) value)
  9807. (org-entry-put nil property value)))
  9808. (defun org-delete-property (property)
  9809. "In the current entry, delete PROPERTY."
  9810. (interactive
  9811. (let* ((completion-ignore-case t)
  9812. (prop (org-ido-completing-read
  9813. "Property: " (org-entry-properties nil 'standard))))
  9814. (list prop)))
  9815. (message "Property %s %s" property
  9816. (if (org-entry-delete nil property)
  9817. "deleted"
  9818. "was not present in the entry")))
  9819. (defun org-delete-property-globally (property)
  9820. "Remove PROPERTY globally, from all entries."
  9821. (interactive
  9822. (let* ((completion-ignore-case t)
  9823. (prop (org-ido-completing-read
  9824. "Globally remove property: "
  9825. (mapcar 'list (org-buffer-property-keys)))))
  9826. (list prop)))
  9827. (save-excursion
  9828. (save-restriction
  9829. (widen)
  9830. (goto-char (point-min))
  9831. (let ((cnt 0))
  9832. (while (re-search-forward
  9833. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9834. nil t)
  9835. (setq cnt (1+ cnt))
  9836. (replace-match ""))
  9837. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9838. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9839. (defun org-compute-property-at-point ()
  9840. "Compute the property at point.
  9841. This looks for an enclosing column format, extracts the operator and
  9842. then applies it to the property in the column format's scope."
  9843. (interactive)
  9844. (unless (org-at-property-p)
  9845. (error "Not at a property"))
  9846. (let ((prop (org-match-string-no-properties 2)))
  9847. (org-columns-get-format-and-top-level)
  9848. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9849. (error "No operator defined for property %s" prop))
  9850. (org-columns-compute prop)))
  9851. (defun org-property-get-allowed-values (pom property &optional table)
  9852. "Get allowed values for the property PROPERTY.
  9853. When TABLE is non-nil, return an alist that can directly be used for
  9854. completion."
  9855. (let (vals)
  9856. (cond
  9857. ((equal property "TODO")
  9858. (setq vals (org-with-point-at pom
  9859. (append org-todo-keywords-1 '("")))))
  9860. ((equal property "PRIORITY")
  9861. (let ((n org-lowest-priority))
  9862. (while (>= n org-highest-priority)
  9863. (push (char-to-string n) vals)
  9864. (setq n (1- n)))))
  9865. ((member property org-special-properties))
  9866. (t
  9867. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9868. (when (and vals (string-match "\\S-" vals))
  9869. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9870. (setq vals (mapcar (lambda (x)
  9871. (cond ((stringp x) x)
  9872. ((numberp x) (number-to-string x))
  9873. ((symbolp x) (symbol-name x))
  9874. (t "???")))
  9875. vals)))))
  9876. (if table (mapcar 'list vals) vals)))
  9877. (defun org-property-previous-allowed-value (&optional previous)
  9878. "Switch to the next allowed value for this property."
  9879. (interactive)
  9880. (org-property-next-allowed-value t))
  9881. (defun org-property-next-allowed-value (&optional previous)
  9882. "Switch to the next allowed value for this property."
  9883. (interactive)
  9884. (unless (org-at-property-p)
  9885. (error "Not at a property"))
  9886. (let* ((key (match-string 2))
  9887. (value (match-string 3))
  9888. (allowed (or (org-property-get-allowed-values (point) key)
  9889. (and (member value '("[ ]" "[-]" "[X]"))
  9890. '("[ ]" "[X]"))))
  9891. nval)
  9892. (unless allowed
  9893. (error "Allowed values for this property have not been defined"))
  9894. (if previous (setq allowed (reverse allowed)))
  9895. (if (member value allowed)
  9896. (setq nval (car (cdr (member value allowed)))))
  9897. (setq nval (or nval (car allowed)))
  9898. (if (equal nval value)
  9899. (error "Only one allowed value for this property"))
  9900. (org-at-property-p)
  9901. (replace-match (concat " :" key ": " nval) t t)
  9902. (org-indent-line-function)
  9903. (beginning-of-line 1)
  9904. (skip-chars-forward " \t")))
  9905. (defun org-find-entry-with-id (ident)
  9906. "Locate the entry that contains the ID property with exact value IDENT.
  9907. IDENT can be a string, a symbol or a number, this function will search for
  9908. the string representation of it.
  9909. Return the position where this entry starts, or nil if there is no such entry."
  9910. (interactive "sID: ")
  9911. (let ((id (cond
  9912. ((stringp ident) ident)
  9913. ((symbol-name ident) (symbol-name ident))
  9914. ((numberp ident) (number-to-string ident))
  9915. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9916. (case-fold-search nil))
  9917. (save-excursion
  9918. (save-restriction
  9919. (widen)
  9920. (goto-char (point-min))
  9921. (when (re-search-forward
  9922. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9923. nil t)
  9924. (org-back-to-heading)
  9925. (point))))))
  9926. ;;;; Timestamps
  9927. (defvar org-last-changed-timestamp nil)
  9928. (defvar org-last-inserted-timestamp nil
  9929. "The last time stamp inserted with `org-insert-time-stamp'.")
  9930. (defvar org-time-was-given) ; dynamically scoped parameter
  9931. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9932. (defvar org-ts-what) ; dynamically scoped parameter
  9933. (defun org-time-stamp (arg &optional inactive)
  9934. "Prompt for a date/time and insert a time stamp.
  9935. If the user specifies a time like HH:MM, or if this command is called
  9936. with a prefix argument, the time stamp will contain date and time.
  9937. Otherwise, only the date will be included. All parts of a date not
  9938. specified by the user will be filled in from the current date/time.
  9939. So if you press just return without typing anything, the time stamp
  9940. will represent the current date/time. If there is already a timestamp
  9941. at the cursor, it will be modified."
  9942. (interactive "P")
  9943. (let* ((ts nil)
  9944. (default-time
  9945. ;; Default time is either today, or, when entering a range,
  9946. ;; the range start.
  9947. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9948. (save-excursion
  9949. (re-search-backward
  9950. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9951. (- (point) 20) t)))
  9952. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9953. (current-time)))
  9954. (default-input (and ts (org-get-compact-tod ts)))
  9955. org-time-was-given org-end-time-was-given time)
  9956. (cond
  9957. ((and (org-at-timestamp-p t)
  9958. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9959. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9960. (insert "--")
  9961. (setq time (let ((this-command this-command))
  9962. (org-read-date arg 'totime nil nil
  9963. default-time default-input)))
  9964. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9965. ((org-at-timestamp-p t)
  9966. (setq time (let ((this-command this-command))
  9967. (org-read-date arg 'totime nil nil default-time default-input)))
  9968. (when (org-at-timestamp-p t) ; just to get the match data
  9969. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9970. (replace-match "")
  9971. (setq org-last-changed-timestamp
  9972. (org-insert-time-stamp
  9973. time (or org-time-was-given arg)
  9974. inactive nil nil (list org-end-time-was-given))))
  9975. (message "Timestamp updated"))
  9976. (t
  9977. (setq time (let ((this-command this-command))
  9978. (org-read-date arg 'totime nil nil default-time default-input)))
  9979. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9980. nil nil (list org-end-time-was-given))))))
  9981. ;; FIXME: can we use this for something else, like computing time differences?
  9982. (defun org-get-compact-tod (s)
  9983. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9984. (let* ((t1 (match-string 1 s))
  9985. (h1 (string-to-number (match-string 2 s)))
  9986. (m1 (string-to-number (match-string 3 s)))
  9987. (t2 (and (match-end 4) (match-string 5 s)))
  9988. (h2 (and t2 (string-to-number (match-string 6 s))))
  9989. (m2 (and t2 (string-to-number (match-string 7 s))))
  9990. dh dm)
  9991. (if (not t2)
  9992. t1
  9993. (setq dh (- h2 h1) dm (- m2 m1))
  9994. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9995. (concat t1 "+" (number-to-string dh)
  9996. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9997. (defun org-time-stamp-inactive (&optional arg)
  9998. "Insert an inactive time stamp.
  9999. An inactive time stamp is enclosed in square brackets instead of angle
  10000. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10001. does not link to the calendar and cannot be changed with the S-cursor keys.
  10002. So these are more for recording a certain time/date."
  10003. (interactive "P")
  10004. (org-time-stamp arg 'inactive))
  10005. (defvar org-date-ovl (org-make-overlay 1 1))
  10006. (org-overlay-put org-date-ovl 'face 'org-warning)
  10007. (org-detach-overlay org-date-ovl)
  10008. (defvar org-ans1) ; dynamically scoped parameter
  10009. (defvar org-ans2) ; dynamically scoped parameter
  10010. (defvar org-plain-time-of-day-regexp) ; defined below
  10011. (defvar org-overriding-default-time nil) ; dynamically scoped
  10012. (defvar org-read-date-overlay nil)
  10013. (defvar org-dcst nil) ; dynamically scoped
  10014. (defun org-read-date (&optional with-time to-time from-string prompt
  10015. default-time default-input)
  10016. "Read a date, possibly a time, and make things smooth for the user.
  10017. The prompt will suggest to enter an ISO date, but you can also enter anything
  10018. which will at least partially be understood by `parse-time-string'.
  10019. Unrecognized parts of the date will default to the current day, month, year,
  10020. hour and minute. If this command is called to replace a timestamp at point,
  10021. of to enter the second timestamp of a range, the default time is taken from the
  10022. existing stamp. For example,
  10023. 3-2-5 --> 2003-02-05
  10024. feb 15 --> currentyear-02-15
  10025. sep 12 9 --> 2009-09-12
  10026. 12:45 --> today 12:45
  10027. 22 sept 0:34 --> currentyear-09-22 0:34
  10028. 12 --> currentyear-currentmonth-12
  10029. Fri --> nearest Friday (today or later)
  10030. etc.
  10031. Furthermore you can specify a relative date by giving, as the *first* thing
  10032. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10033. change in days weeks, months, years.
  10034. With a single plus or minus, the date is relative to today. With a double
  10035. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10036. +4d --> four days from today
  10037. +4 --> same as above
  10038. +2w --> two weeks from today
  10039. ++5 --> five days from default date
  10040. The function understands only English month and weekday abbreviations,
  10041. but this can be configured with the variables `parse-time-months' and
  10042. `parse-time-weekdays'.
  10043. While prompting, a calendar is popped up - you can also select the
  10044. date with the mouse (button 1). The calendar shows a period of three
  10045. months. To scroll it to other months, use the keys `>' and `<'.
  10046. If you don't like the calendar, turn it off with
  10047. \(setq org-read-date-popup-calendar nil)
  10048. With optional argument TO-TIME, the date will immediately be converted
  10049. to an internal time.
  10050. With an optional argument WITH-TIME, the prompt will suggest to also
  10051. insert a time. Note that when WITH-TIME is not set, you can still
  10052. enter a time, and this function will inform the calling routine about
  10053. this change. The calling routine may then choose to change the format
  10054. used to insert the time stamp into the buffer to include the time.
  10055. With optional argument FROM-STRING, read from this string instead from
  10056. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10057. the time/date that is used for everything that is not specified by the
  10058. user."
  10059. (require 'parse-time)
  10060. (let* ((org-time-stamp-rounding-minutes
  10061. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10062. (org-dcst org-display-custom-times)
  10063. (ct (org-current-time))
  10064. (def (or org-overriding-default-time default-time ct))
  10065. (defdecode (decode-time def))
  10066. (dummy (progn
  10067. (when (< (nth 2 defdecode) org-extend-today-until)
  10068. (setcar (nthcdr 2 defdecode) -1)
  10069. (setcar (nthcdr 1 defdecode) 59)
  10070. (setq def (apply 'encode-time defdecode)
  10071. defdecode (decode-time def)))))
  10072. (calendar-move-hook nil)
  10073. (calendar-view-diary-initially-flag nil)
  10074. (view-diary-entries-initially nil)
  10075. (calendar-view-holidays-initially-flag nil)
  10076. (view-calendar-holidays-initially nil)
  10077. (timestr (format-time-string
  10078. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10079. (prompt (concat (if prompt (concat prompt " ") "")
  10080. (format "Date+time [%s]: " timestr)))
  10081. ans (org-ans0 "") org-ans1 org-ans2 final)
  10082. (cond
  10083. (from-string (setq ans from-string))
  10084. (org-read-date-popup-calendar
  10085. (save-excursion
  10086. (save-window-excursion
  10087. (calendar)
  10088. (calendar-forward-day (- (time-to-days def)
  10089. (calendar-absolute-from-gregorian
  10090. (calendar-current-date))))
  10091. (org-eval-in-calendar nil t)
  10092. (let* ((old-map (current-local-map))
  10093. (map (copy-keymap calendar-mode-map))
  10094. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10095. (org-defkey map (kbd "RET") 'org-calendar-select)
  10096. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10097. 'org-calendar-select-mouse)
  10098. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10099. 'org-calendar-select-mouse)
  10100. (org-defkey minibuffer-local-map [(meta shift left)]
  10101. (lambda () (interactive)
  10102. (org-eval-in-calendar '(calendar-backward-month 1))))
  10103. (org-defkey minibuffer-local-map [(meta shift right)]
  10104. (lambda () (interactive)
  10105. (org-eval-in-calendar '(calendar-forward-month 1))))
  10106. (org-defkey minibuffer-local-map [(meta shift up)]
  10107. (lambda () (interactive)
  10108. (org-eval-in-calendar '(calendar-backward-year 1))))
  10109. (org-defkey minibuffer-local-map [(meta shift down)]
  10110. (lambda () (interactive)
  10111. (org-eval-in-calendar '(calendar-forward-year 1))))
  10112. (org-defkey minibuffer-local-map [(shift up)]
  10113. (lambda () (interactive)
  10114. (org-eval-in-calendar '(calendar-backward-week 1))))
  10115. (org-defkey minibuffer-local-map [(shift down)]
  10116. (lambda () (interactive)
  10117. (org-eval-in-calendar '(calendar-forward-week 1))))
  10118. (org-defkey minibuffer-local-map [(shift left)]
  10119. (lambda () (interactive)
  10120. (org-eval-in-calendar '(calendar-backward-day 1))))
  10121. (org-defkey minibuffer-local-map [(shift right)]
  10122. (lambda () (interactive)
  10123. (org-eval-in-calendar '(calendar-forward-day 1))))
  10124. (org-defkey minibuffer-local-map ">"
  10125. (lambda () (interactive)
  10126. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10127. (org-defkey minibuffer-local-map "<"
  10128. (lambda () (interactive)
  10129. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10130. (unwind-protect
  10131. (progn
  10132. (use-local-map map)
  10133. (add-hook 'post-command-hook 'org-read-date-display)
  10134. (setq org-ans0 (read-string prompt default-input nil nil))
  10135. ;; org-ans0: from prompt
  10136. ;; org-ans1: from mouse click
  10137. ;; org-ans2: from calendar motion
  10138. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10139. (remove-hook 'post-command-hook 'org-read-date-display)
  10140. (use-local-map old-map)
  10141. (when org-read-date-overlay
  10142. (org-delete-overlay org-read-date-overlay)
  10143. (setq org-read-date-overlay nil)))))))
  10144. (t ; Naked prompt only
  10145. (unwind-protect
  10146. (setq ans (read-string prompt default-input nil timestr))
  10147. (when org-read-date-overlay
  10148. (org-delete-overlay org-read-date-overlay)
  10149. (setq org-read-date-overlay nil)))))
  10150. (setq final (org-read-date-analyze ans def defdecode))
  10151. (if to-time
  10152. (apply 'encode-time final)
  10153. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10154. (format "%04d-%02d-%02d %02d:%02d"
  10155. (nth 5 final) (nth 4 final) (nth 3 final)
  10156. (nth 2 final) (nth 1 final))
  10157. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10158. (defvar def)
  10159. (defvar defdecode)
  10160. (defvar with-time)
  10161. (defun org-read-date-display ()
  10162. "Display the current date prompt interpretation in the minibuffer."
  10163. (when org-read-date-display-live
  10164. (when org-read-date-overlay
  10165. (org-delete-overlay org-read-date-overlay))
  10166. (let ((p (point)))
  10167. (end-of-line 1)
  10168. (while (not (equal (buffer-substring
  10169. (max (point-min) (- (point) 4)) (point))
  10170. " "))
  10171. (insert " "))
  10172. (goto-char p))
  10173. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10174. " " (or org-ans1 org-ans2)))
  10175. (org-end-time-was-given nil)
  10176. (f (org-read-date-analyze ans def defdecode))
  10177. (fmts (if org-dcst
  10178. org-time-stamp-custom-formats
  10179. org-time-stamp-formats))
  10180. (fmt (if (or with-time
  10181. (and (boundp 'org-time-was-given) org-time-was-given))
  10182. (cdr fmts)
  10183. (car fmts)))
  10184. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10185. (when (and org-end-time-was-given
  10186. (string-match org-plain-time-of-day-regexp txt))
  10187. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10188. org-end-time-was-given
  10189. (substring txt (match-end 0)))))
  10190. (setq org-read-date-overlay
  10191. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10192. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10193. (defun org-read-date-analyze (ans def defdecode)
  10194. "Analyse the combined answer of the date prompt."
  10195. ;; FIXME: cleanup and comment
  10196. (let (delta deltan deltaw deltadef year month day
  10197. hour minute second wday pm h2 m2 tl wday1
  10198. iso-year iso-weekday iso-week iso-year iso-date)
  10199. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10200. (setq ans "+0"))
  10201. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10202. (setq ans (replace-match "" t t ans)
  10203. deltan (car delta)
  10204. deltaw (nth 1 delta)
  10205. deltadef (nth 2 delta)))
  10206. ;; Check if there is an iso week date in there
  10207. ;; If yes, sore the info and postpone interpreting it until the rest
  10208. ;; of the parsing is done
  10209. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10210. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10211. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10212. iso-week (string-to-number (match-string 2 ans)))
  10213. (setq ans (replace-match "" t t ans)))
  10214. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10215. (when (string-match
  10216. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10217. (setq year (if (match-end 2)
  10218. (string-to-number (match-string 2 ans))
  10219. (string-to-number (format-time-string "%Y")))
  10220. month (string-to-number (match-string 3 ans))
  10221. day (string-to-number (match-string 4 ans)))
  10222. (if (< year 100) (setq year (+ 2000 year)))
  10223. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10224. t nil ans)))
  10225. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10226. ;; If there is a time with am/pm, and *no* time without it, we convert
  10227. ;; so that matching will be successful.
  10228. (loop for i from 1 to 2 do ; twice, for end time as well
  10229. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10230. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10231. (setq hour (string-to-number (match-string 1 ans))
  10232. minute (if (match-end 3)
  10233. (string-to-number (match-string 3 ans))
  10234. 0)
  10235. pm (equal ?p
  10236. (string-to-char (downcase (match-string 4 ans)))))
  10237. (if (and (= hour 12) (not pm))
  10238. (setq hour 0)
  10239. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10240. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10241. t t ans))))
  10242. ;; Check if a time range is given as a duration
  10243. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10244. (setq hour (string-to-number (match-string 1 ans))
  10245. h2 (+ hour (string-to-number (match-string 3 ans)))
  10246. minute (string-to-number (match-string 2 ans))
  10247. m2 (+ minute (if (match-end 5) (string-to-number
  10248. (match-string 5 ans))0)))
  10249. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10250. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10251. t t ans)))
  10252. ;; Check if there is a time range
  10253. (when (boundp 'org-end-time-was-given)
  10254. (setq org-time-was-given nil)
  10255. (when (and (string-match org-plain-time-of-day-regexp ans)
  10256. (match-end 8))
  10257. (setq org-end-time-was-given (match-string 8 ans))
  10258. (setq ans (concat (substring ans 0 (match-beginning 7))
  10259. (substring ans (match-end 7))))))
  10260. (setq tl (parse-time-string ans)
  10261. day (or (nth 3 tl) (nth 3 defdecode))
  10262. month (or (nth 4 tl)
  10263. (if (and org-read-date-prefer-future
  10264. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10265. (1+ (nth 4 defdecode))
  10266. (nth 4 defdecode)))
  10267. year (or (nth 5 tl)
  10268. (if (and org-read-date-prefer-future
  10269. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10270. (1+ (nth 5 defdecode))
  10271. (nth 5 defdecode)))
  10272. hour (or (nth 2 tl) (nth 2 defdecode))
  10273. minute (or (nth 1 tl) (nth 1 defdecode))
  10274. second (or (nth 0 tl) 0)
  10275. wday (nth 6 tl))
  10276. ;; Special date definitions below
  10277. (cond
  10278. (iso-week
  10279. ;; There was an iso week
  10280. (setq year (or iso-year year)
  10281. day (or iso-weekday wday 1)
  10282. wday nil ; to make sure that the trigger below does not match
  10283. iso-date (calendar-gregorian-from-absolute
  10284. (calendar-absolute-from-iso
  10285. (list iso-week day year))))
  10286. ; FIXME: Should we also push ISO weeks into the future?
  10287. ; (when (and org-read-date-prefer-future
  10288. ; (not iso-year)
  10289. ; (< (calendar-absolute-from-gregorian iso-date)
  10290. ; (time-to-days (current-time))))
  10291. ; (setq year (1+ year)
  10292. ; iso-date (calendar-gregorian-from-absolute
  10293. ; (calendar-absolute-from-iso
  10294. ; (list iso-week day year)))))
  10295. (setq month (car iso-date)
  10296. year (nth 2 iso-date)
  10297. day (nth 1 iso-date)))
  10298. (deltan
  10299. (unless deltadef
  10300. (let ((now (decode-time (current-time))))
  10301. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10302. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10303. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10304. ((equal deltaw "m") (setq month (+ month deltan)))
  10305. ((equal deltaw "y") (setq year (+ year deltan)))))
  10306. ((and wday (not (nth 3 tl)))
  10307. ;; Weekday was given, but no day, so pick that day in the week
  10308. ;; on or after the derived date.
  10309. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10310. (unless (equal wday wday1)
  10311. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10312. (if (and (boundp 'org-time-was-given)
  10313. (nth 2 tl))
  10314. (setq org-time-was-given t))
  10315. (if (< year 100) (setq year (+ 2000 year)))
  10316. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10317. (list second minute hour day month year)))
  10318. (defvar parse-time-weekdays)
  10319. (defun org-read-date-get-relative (s today default)
  10320. "Check string S for special relative date string.
  10321. TODAY and DEFAULT are internal times, for today and for a default.
  10322. Return shift list (N what def-flag)
  10323. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10324. N is the number of WHATs to shift.
  10325. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10326. the DEFAULT date rather than TODAY."
  10327. (when (and
  10328. (string-match
  10329. (concat
  10330. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10331. "\\([0-9]+\\)?"
  10332. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10333. "\\([ \t]\\|$\\)") s)
  10334. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10335. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10336. (string-to-char (substring (match-string 1 s) -1))
  10337. ?+))
  10338. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10339. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10340. (what (if (match-end 3) (match-string 3 s) "d"))
  10341. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10342. (date (if rel default today))
  10343. (wday (nth 6 (decode-time date)))
  10344. delta)
  10345. (if wday1
  10346. (progn
  10347. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10348. (if (= dir ?-) (setq delta (- delta 7)))
  10349. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10350. (list delta "d" rel))
  10351. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10352. (defun org-eval-in-calendar (form &optional keepdate)
  10353. "Eval FORM in the calendar window and return to current window.
  10354. Also, store the cursor date in variable org-ans2."
  10355. (let ((sw (selected-window)))
  10356. (select-window (get-buffer-window "*Calendar*"))
  10357. (eval form)
  10358. (when (and (not keepdate) (calendar-cursor-to-date))
  10359. (let* ((date (calendar-cursor-to-date))
  10360. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10361. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10362. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10363. (select-window sw)))
  10364. (defun org-calendar-select ()
  10365. "Return to `org-read-date' with the date currently selected.
  10366. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10367. (interactive)
  10368. (when (calendar-cursor-to-date)
  10369. (let* ((date (calendar-cursor-to-date))
  10370. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10371. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10372. (if (active-minibuffer-window) (exit-minibuffer))))
  10373. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10374. "Insert a date stamp for the date given by the internal TIME.
  10375. WITH-HM means, use the stamp format that includes the time of the day.
  10376. INACTIVE means use square brackets instead of angular ones, so that the
  10377. stamp will not contribute to the agenda.
  10378. PRE and POST are optional strings to be inserted before and after the
  10379. stamp.
  10380. The command returns the inserted time stamp."
  10381. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10382. stamp)
  10383. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10384. (insert-before-markers (or pre ""))
  10385. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10386. (when (listp extra)
  10387. (setq extra (car extra))
  10388. (if (and (stringp extra)
  10389. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10390. (setq extra (format "-%02d:%02d"
  10391. (string-to-number (match-string 1 extra))
  10392. (string-to-number (match-string 2 extra))))
  10393. (setq extra nil)))
  10394. (when extra
  10395. (backward-char 1)
  10396. (insert-before-markers extra)
  10397. (forward-char 1))
  10398. (insert-before-markers (or post ""))
  10399. (setq org-last-inserted-timestamp stamp)))
  10400. (defun org-toggle-time-stamp-overlays ()
  10401. "Toggle the use of custom time stamp formats."
  10402. (interactive)
  10403. (setq org-display-custom-times (not org-display-custom-times))
  10404. (unless org-display-custom-times
  10405. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10406. (while (setq p (next-single-property-change p 'display))
  10407. (if (and (get-text-property p 'display)
  10408. (eq (get-text-property p 'face) 'org-date))
  10409. (remove-text-properties
  10410. p (setq p (next-single-property-change p 'display))
  10411. '(display t))))
  10412. (set-buffer-modified-p bmp)))
  10413. (if (featurep 'xemacs)
  10414. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10415. (org-restart-font-lock)
  10416. (setq org-table-may-need-update t)
  10417. (if org-display-custom-times
  10418. (message "Time stamps are overlayed with custom format")
  10419. (message "Time stamp overlays removed")))
  10420. (defun org-display-custom-time (beg end)
  10421. "Overlay modified time stamp format over timestamp between BEG and END."
  10422. (let* ((ts (buffer-substring beg end))
  10423. t1 w1 with-hm tf time str w2 (off 0))
  10424. (save-match-data
  10425. (setq t1 (org-parse-time-string ts t))
  10426. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10427. (setq off (- (match-end 0) (match-beginning 0)))))
  10428. (setq end (- end off))
  10429. (setq w1 (- end beg)
  10430. with-hm (and (nth 1 t1) (nth 2 t1))
  10431. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10432. time (org-fix-decoded-time t1)
  10433. str (org-add-props
  10434. (format-time-string
  10435. (substring tf 1 -1) (apply 'encode-time time))
  10436. nil 'mouse-face 'highlight)
  10437. w2 (length str))
  10438. (if (not (= w2 w1))
  10439. (add-text-properties (1+ beg) (+ 2 beg)
  10440. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10441. (if (featurep 'xemacs)
  10442. (progn
  10443. (put-text-property beg end 'invisible t)
  10444. (put-text-property beg end 'end-glyph (make-glyph str)))
  10445. (put-text-property beg end 'display str))))
  10446. (defun org-translate-time (string)
  10447. "Translate all timestamps in STRING to custom format.
  10448. But do this only if the variable `org-display-custom-times' is set."
  10449. (when org-display-custom-times
  10450. (save-match-data
  10451. (let* ((start 0)
  10452. (re org-ts-regexp-both)
  10453. t1 with-hm inactive tf time str beg end)
  10454. (while (setq start (string-match re string start))
  10455. (setq beg (match-beginning 0)
  10456. end (match-end 0)
  10457. t1 (save-match-data
  10458. (org-parse-time-string (substring string beg end) t))
  10459. with-hm (and (nth 1 t1) (nth 2 t1))
  10460. inactive (equal (substring string beg (1+ beg)) "[")
  10461. tf (funcall (if with-hm 'cdr 'car)
  10462. org-time-stamp-custom-formats)
  10463. time (org-fix-decoded-time t1)
  10464. str (format-time-string
  10465. (concat
  10466. (if inactive "[" "<") (substring tf 1 -1)
  10467. (if inactive "]" ">"))
  10468. (apply 'encode-time time))
  10469. string (replace-match str t t string)
  10470. start (+ start (length str)))))))
  10471. string)
  10472. (defun org-fix-decoded-time (time)
  10473. "Set 0 instead of nil for the first 6 elements of time.
  10474. Don't touch the rest."
  10475. (let ((n 0))
  10476. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10477. (defun org-days-to-time (timestamp-string)
  10478. "Difference between TIMESTAMP-STRING and now in days."
  10479. (- (time-to-days (org-time-string-to-time timestamp-string))
  10480. (time-to-days (current-time))))
  10481. (defun org-deadline-close (timestamp-string &optional ndays)
  10482. "Is the time in TIMESTAMP-STRING close to the current date?"
  10483. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10484. (and (< (org-days-to-time timestamp-string) ndays)
  10485. (not (org-entry-is-done-p))))
  10486. (defun org-get-wdays (ts)
  10487. "Get the deadline lead time appropriate for timestring TS."
  10488. (cond
  10489. ((<= org-deadline-warning-days 0)
  10490. ;; 0 or negative, enforce this value no matter what
  10491. (- org-deadline-warning-days))
  10492. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10493. ;; lead time is specified.
  10494. (floor (* (string-to-number (match-string 1 ts))
  10495. (cdr (assoc (match-string 2 ts)
  10496. '(("d" . 1) ("w" . 7)
  10497. ("m" . 30.4) ("y" . 365.25)))))))
  10498. ;; go for the default.
  10499. (t org-deadline-warning-days)))
  10500. (defun org-calendar-select-mouse (ev)
  10501. "Return to `org-read-date' with the date currently selected.
  10502. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10503. (interactive "e")
  10504. (mouse-set-point ev)
  10505. (when (calendar-cursor-to-date)
  10506. (let* ((date (calendar-cursor-to-date))
  10507. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10508. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10509. (if (active-minibuffer-window) (exit-minibuffer))))
  10510. (defun org-check-deadlines (ndays)
  10511. "Check if there are any deadlines due or past due.
  10512. A deadline is considered due if it happens within `org-deadline-warning-days'
  10513. days from today's date. If the deadline appears in an entry marked DONE,
  10514. it is not shown. The prefix arg NDAYS can be used to test that many
  10515. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10516. (interactive "P")
  10517. (let* ((org-warn-days
  10518. (cond
  10519. ((equal ndays '(4)) 100000)
  10520. (ndays (prefix-numeric-value ndays))
  10521. (t (abs org-deadline-warning-days))))
  10522. (case-fold-search nil)
  10523. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10524. (callback
  10525. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10526. (message "%d deadlines past-due or due within %d days"
  10527. (org-occur regexp nil callback)
  10528. org-warn-days)))
  10529. (defun org-check-before-date (date)
  10530. "Check if there are deadlines or scheduled entries before DATE."
  10531. (interactive (list (org-read-date)))
  10532. (let ((case-fold-search nil)
  10533. (regexp (concat "\\<\\(" org-deadline-string
  10534. "\\|" org-scheduled-string
  10535. "\\) *<\\([^>]+\\)>"))
  10536. (callback
  10537. (lambda () (time-less-p
  10538. (org-time-string-to-time (match-string 2))
  10539. (org-time-string-to-time date)))))
  10540. (message "%d entries before %s"
  10541. (org-occur regexp nil callback) date)))
  10542. (defun org-evaluate-time-range (&optional to-buffer)
  10543. "Evaluate a time range by computing the difference between start and end.
  10544. Normally the result is just printed in the echo area, but with prefix arg
  10545. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10546. If the time range is actually in a table, the result is inserted into the
  10547. next column.
  10548. For time difference computation, a year is assumed to be exactly 365
  10549. days in order to avoid rounding problems."
  10550. (interactive "P")
  10551. (or
  10552. (org-clock-update-time-maybe)
  10553. (save-excursion
  10554. (unless (org-at-date-range-p t)
  10555. (goto-char (point-at-bol))
  10556. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10557. (if (not (org-at-date-range-p t))
  10558. (error "Not at a time-stamp range, and none found in current line")))
  10559. (let* ((ts1 (match-string 1))
  10560. (ts2 (match-string 2))
  10561. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10562. (match-end (match-end 0))
  10563. (time1 (org-time-string-to-time ts1))
  10564. (time2 (org-time-string-to-time ts2))
  10565. (t1 (time-to-seconds time1))
  10566. (t2 (time-to-seconds time2))
  10567. (diff (abs (- t2 t1)))
  10568. (negative (< (- t2 t1) 0))
  10569. ;; (ys (floor (* 365 24 60 60)))
  10570. (ds (* 24 60 60))
  10571. (hs (* 60 60))
  10572. (fy "%dy %dd %02d:%02d")
  10573. (fy1 "%dy %dd")
  10574. (fd "%dd %02d:%02d")
  10575. (fd1 "%dd")
  10576. (fh "%02d:%02d")
  10577. y d h m align)
  10578. (if havetime
  10579. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10580. y 0
  10581. d (floor (/ diff ds)) diff (mod diff ds)
  10582. h (floor (/ diff hs)) diff (mod diff hs)
  10583. m (floor (/ diff 60)))
  10584. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10585. y 0
  10586. d (floor (+ (/ diff ds) 0.5))
  10587. h 0 m 0))
  10588. (if (not to-buffer)
  10589. (message "%s" (org-make-tdiff-string y d h m))
  10590. (if (org-at-table-p)
  10591. (progn
  10592. (goto-char match-end)
  10593. (setq align t)
  10594. (and (looking-at " *|") (goto-char (match-end 0))))
  10595. (goto-char match-end))
  10596. (if (looking-at
  10597. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10598. (replace-match ""))
  10599. (if negative (insert " -"))
  10600. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10601. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10602. (insert " " (format fh h m))))
  10603. (if align (org-table-align))
  10604. (message "Time difference inserted")))))
  10605. (defun org-make-tdiff-string (y d h m)
  10606. (let ((fmt "")
  10607. (l nil))
  10608. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10609. l (push y l)))
  10610. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10611. l (push d l)))
  10612. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10613. l (push h l)))
  10614. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10615. l (push m l)))
  10616. (apply 'format fmt (nreverse l))))
  10617. (defun org-time-string-to-time (s)
  10618. (apply 'encode-time (org-parse-time-string s)))
  10619. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10620. "Convert a time stamp to an absolute day number.
  10621. If there is a specifyer for a cyclic time stamp, get the closest date to
  10622. DAYNR.
  10623. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10624. (cond
  10625. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10626. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10627. daynr
  10628. (+ daynr 1000)))
  10629. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10630. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10631. (time-to-days (current-time))) (match-string 0 s)
  10632. prefer show-all))
  10633. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10634. (defun org-days-to-iso-week (days)
  10635. "Return the iso week number."
  10636. (require 'cal-iso)
  10637. (car (calendar-iso-from-absolute days)))
  10638. (defun org-small-year-to-year (year)
  10639. "Convert 2-digit years into 4-digit years.
  10640. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10641. The year 2000 cannot be abbreviated. Any year larger than 99
  10642. is returned unchanged."
  10643. (if (< year 38)
  10644. (setq year (+ 2000 year))
  10645. (if (< year 100)
  10646. (setq year (+ 1900 year))))
  10647. year)
  10648. (defun org-time-from-absolute (d)
  10649. "Return the time corresponding to date D.
  10650. D may be an absolute day number, or a calendar-type list (month day year)."
  10651. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10652. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10653. (defun org-calendar-holiday ()
  10654. "List of holidays, for Diary display in Org-mode."
  10655. (require 'holidays)
  10656. (let ((hl (funcall
  10657. (if (fboundp 'calendar-check-holidays)
  10658. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10659. (if hl (mapconcat 'identity hl "; "))))
  10660. (defun org-diary-sexp-entry (sexp entry date)
  10661. "Process a SEXP diary ENTRY for DATE."
  10662. (require 'diary-lib)
  10663. (let ((result (if calendar-debug-sexp
  10664. (let ((stack-trace-on-error t))
  10665. (eval (car (read-from-string sexp))))
  10666. (condition-case nil
  10667. (eval (car (read-from-string sexp)))
  10668. (error
  10669. (beep)
  10670. (message "Bad sexp at line %d in %s: %s"
  10671. (org-current-line)
  10672. (buffer-file-name) sexp)
  10673. (sleep-for 2))))))
  10674. (cond ((stringp result) result)
  10675. ((and (consp result)
  10676. (stringp (cdr result))) (cdr result))
  10677. (result entry)
  10678. (t nil))))
  10679. (defun org-diary-to-ical-string (frombuf)
  10680. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10681. This uses the icalendar.el library."
  10682. (let* ((tmpdir (if (featurep 'xemacs)
  10683. (temp-directory)
  10684. temporary-file-directory))
  10685. (tmpfile (make-temp-name
  10686. (expand-file-name "orgics" tmpdir)))
  10687. buf rtn b e)
  10688. (save-excursion
  10689. (set-buffer frombuf)
  10690. (icalendar-export-region (point-min) (point-max) tmpfile)
  10691. (setq buf (find-buffer-visiting tmpfile))
  10692. (set-buffer buf)
  10693. (goto-char (point-min))
  10694. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10695. (setq b (match-beginning 0)))
  10696. (goto-char (point-max))
  10697. (if (re-search-backward "^END:VEVENT" nil t)
  10698. (setq e (match-end 0)))
  10699. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10700. (kill-buffer buf)
  10701. (delete-file tmpfile)
  10702. rtn))
  10703. (defun org-closest-date (start current change prefer show-all)
  10704. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10705. When PREFER is `past' return a date that is either CURRENT or past.
  10706. When PREFER is `future', return a date that is either CURRENT or future.
  10707. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10708. ;; Make the proper lists from the dates
  10709. (catch 'exit
  10710. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10711. dn dw sday cday n1 n2 n0
  10712. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10713. (setq start (org-date-to-gregorian start)
  10714. current (org-date-to-gregorian
  10715. (if show-all
  10716. current
  10717. (time-to-days (current-time))))
  10718. sday (calendar-absolute-from-gregorian start)
  10719. cday (calendar-absolute-from-gregorian current))
  10720. (if (<= cday sday) (throw 'exit sday))
  10721. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10722. (setq dn (string-to-number (match-string 1 change))
  10723. dw (cdr (assoc (match-string 2 change) a1)))
  10724. (error "Invalid change specifyer: %s" change))
  10725. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10726. (cond
  10727. ((eq dw 'day)
  10728. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10729. n2 (+ n1 dn)))
  10730. ((eq dw 'year)
  10731. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10732. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10733. (setq date1 (list m d y1)
  10734. n1 (calendar-absolute-from-gregorian date1)
  10735. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10736. n2 (calendar-absolute-from-gregorian date2)))
  10737. ((eq dw 'month)
  10738. ;; approx number of month between the two dates
  10739. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10740. ;; How often does dn fit in there?
  10741. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10742. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10743. m (+ m nm)
  10744. ny (floor (/ m 12))
  10745. y (+ y ny)
  10746. m (- m (* ny 12)))
  10747. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10748. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10749. (setq m2 (+ m dn) y2 y)
  10750. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10751. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10752. (while (<= n2 cday)
  10753. (setq n1 n2 m m2 y y2)
  10754. (setq m2 (+ m dn) y2 y)
  10755. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10756. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10757. ;; Make sure n1 is the earlier date
  10758. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  10759. (if show-all
  10760. (cond
  10761. ((eq prefer 'past) n1)
  10762. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10763. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10764. (cond
  10765. ((eq prefer 'past) n1)
  10766. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10767. (t (if (= cday n1) n1 n2)))))))
  10768. (defun org-date-to-gregorian (date)
  10769. "Turn any specification of DATE into a gregorian date for the calendar."
  10770. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10771. ((and (listp date) (= (length date) 3)) date)
  10772. ((stringp date)
  10773. (setq date (org-parse-time-string date))
  10774. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10775. ((listp date)
  10776. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10777. (defun org-parse-time-string (s &optional nodefault)
  10778. "Parse the standard Org-mode time string.
  10779. This should be a lot faster than the normal `parse-time-string'.
  10780. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10781. hour and minute fields will be nil if not given."
  10782. (if (string-match org-ts-regexp0 s)
  10783. (list 0
  10784. (if (or (match-beginning 8) (not nodefault))
  10785. (string-to-number (or (match-string 8 s) "0")))
  10786. (if (or (match-beginning 7) (not nodefault))
  10787. (string-to-number (or (match-string 7 s) "0")))
  10788. (string-to-number (match-string 4 s))
  10789. (string-to-number (match-string 3 s))
  10790. (string-to-number (match-string 2 s))
  10791. nil nil nil)
  10792. (make-list 9 0)))
  10793. (defun org-timestamp-up (&optional arg)
  10794. "Increase the date item at the cursor by one.
  10795. If the cursor is on the year, change the year. If it is on the month or
  10796. the day, change that.
  10797. With prefix ARG, change by that many units."
  10798. (interactive "p")
  10799. (org-timestamp-change (prefix-numeric-value arg)))
  10800. (defun org-timestamp-down (&optional arg)
  10801. "Decrease the date item at the cursor by one.
  10802. If the cursor is on the year, change the year. If it is on the month or
  10803. the day, change that.
  10804. With prefix ARG, change by that many units."
  10805. (interactive "p")
  10806. (org-timestamp-change (- (prefix-numeric-value arg))))
  10807. (defun org-timestamp-up-day (&optional arg)
  10808. "Increase the date in the time stamp by one day.
  10809. With prefix ARG, change that many days."
  10810. (interactive "p")
  10811. (if (and (not (org-at-timestamp-p t))
  10812. (org-on-heading-p))
  10813. (org-todo 'up)
  10814. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10815. (defun org-timestamp-down-day (&optional arg)
  10816. "Decrease the date in the time stamp by one day.
  10817. With prefix ARG, change that many days."
  10818. (interactive "p")
  10819. (if (and (not (org-at-timestamp-p t))
  10820. (org-on-heading-p))
  10821. (org-todo 'down)
  10822. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10823. (defun org-at-timestamp-p (&optional inactive-ok)
  10824. "Determine if the cursor is in or at a timestamp."
  10825. (interactive)
  10826. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10827. (pos (point))
  10828. (ans (or (looking-at tsr)
  10829. (save-excursion
  10830. (skip-chars-backward "^[<\n\r\t")
  10831. (if (> (point) (point-min)) (backward-char 1))
  10832. (and (looking-at tsr)
  10833. (> (- (match-end 0) pos) -1))))))
  10834. (and ans
  10835. (boundp 'org-ts-what)
  10836. (setq org-ts-what
  10837. (cond
  10838. ((= pos (match-beginning 0)) 'bracket)
  10839. ((= pos (1- (match-end 0))) 'bracket)
  10840. ((org-pos-in-match-range pos 2) 'year)
  10841. ((org-pos-in-match-range pos 3) 'month)
  10842. ((org-pos-in-match-range pos 7) 'hour)
  10843. ((org-pos-in-match-range pos 8) 'minute)
  10844. ((or (org-pos-in-match-range pos 4)
  10845. (org-pos-in-match-range pos 5)) 'day)
  10846. ((and (> pos (or (match-end 8) (match-end 5)))
  10847. (< pos (match-end 0)))
  10848. (- pos (or (match-end 8) (match-end 5))))
  10849. (t 'day))))
  10850. ans))
  10851. (defun org-toggle-timestamp-type ()
  10852. "Toggle the type (<active> or [inactive]) of a time stamp."
  10853. (interactive)
  10854. (when (org-at-timestamp-p t)
  10855. (let ((beg (match-beginning 0)) (end (match-end 0))
  10856. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10857. (save-excursion
  10858. (goto-char beg)
  10859. (while (re-search-forward "[][<>]" end t)
  10860. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10861. t t)))
  10862. (message "Timestamp is now %sactive"
  10863. (if (equal (char-after beg) ?<) "" "in")))))
  10864. (defun org-timestamp-change (n &optional what)
  10865. "Change the date in the time stamp at point.
  10866. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10867. `year', `minute', `second'. If WHAT is not given, the cursor position
  10868. in the timestamp determines what will be changed."
  10869. (let ((pos (point))
  10870. with-hm inactive
  10871. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10872. org-ts-what
  10873. extra rem
  10874. ts time time0)
  10875. (if (not (org-at-timestamp-p t))
  10876. (error "Not at a timestamp"))
  10877. (if (and (not what) (eq org-ts-what 'bracket))
  10878. (org-toggle-timestamp-type)
  10879. (if (and (not what) (not (eq org-ts-what 'day))
  10880. org-display-custom-times
  10881. (get-text-property (point) 'display)
  10882. (not (get-text-property (1- (point)) 'display)))
  10883. (setq org-ts-what 'day))
  10884. (setq org-ts-what (or what org-ts-what)
  10885. inactive (= (char-after (match-beginning 0)) ?\[)
  10886. ts (match-string 0))
  10887. (replace-match "")
  10888. (if (string-match
  10889. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10890. ts)
  10891. (setq extra (match-string 1 ts)))
  10892. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10893. (setq with-hm t))
  10894. (setq time0 (org-parse-time-string ts))
  10895. (when (and (eq org-ts-what 'minute)
  10896. (eq current-prefix-arg nil))
  10897. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10898. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10899. (setcar (cdr time0) (+ (nth 1 time0)
  10900. (if (> n 0) (- rem) (- dm rem))))))
  10901. (setq time
  10902. (encode-time (or (car time0) 0)
  10903. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10904. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10905. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10906. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10907. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10908. (nthcdr 6 time0)))
  10909. (when (integerp org-ts-what)
  10910. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10911. (if (eq what 'calendar)
  10912. (let ((cal-date (org-get-date-from-calendar)))
  10913. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10914. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10915. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10916. (setcar time0 (or (car time0) 0))
  10917. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10918. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10919. (setq time (apply 'encode-time time0))))
  10920. (setq org-last-changed-timestamp
  10921. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10922. (org-clock-update-time-maybe)
  10923. (goto-char pos)
  10924. ;; Try to recenter the calendar window, if any
  10925. (if (and org-calendar-follow-timestamp-change
  10926. (get-buffer-window "*Calendar*" t)
  10927. (memq org-ts-what '(day month year)))
  10928. (org-recenter-calendar (time-to-days time))))))
  10929. (defun org-modify-ts-extra (s pos n dm)
  10930. "Change the different parts of the lead-time and repeat fields in timestamp."
  10931. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10932. ng h m new rem)
  10933. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10934. (cond
  10935. ((or (org-pos-in-match-range pos 2)
  10936. (org-pos-in-match-range pos 3))
  10937. (setq m (string-to-number (match-string 3 s))
  10938. h (string-to-number (match-string 2 s)))
  10939. (if (org-pos-in-match-range pos 2)
  10940. (setq h (+ h n))
  10941. (setq n (* dm (org-no-warnings (signum n))))
  10942. (when (not (= 0 (setq rem (% m dm))))
  10943. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10944. (setq m (+ m n)))
  10945. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10946. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10947. (setq h (min 24 (max 0 h)))
  10948. (setq ng 1 new (format "-%02d:%02d" h m)))
  10949. ((org-pos-in-match-range pos 6)
  10950. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10951. ((org-pos-in-match-range pos 5)
  10952. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10953. ((org-pos-in-match-range pos 9)
  10954. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10955. ((org-pos-in-match-range pos 8)
  10956. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10957. (when ng
  10958. (setq s (concat
  10959. (substring s 0 (match-beginning ng))
  10960. new
  10961. (substring s (match-end ng))))))
  10962. s))
  10963. (defun org-recenter-calendar (date)
  10964. "If the calendar is visible, recenter it to DATE."
  10965. (let* ((win (selected-window))
  10966. (cwin (get-buffer-window "*Calendar*" t))
  10967. (calendar-move-hook nil))
  10968. (when cwin
  10969. (select-window cwin)
  10970. (calendar-goto-date (if (listp date) date
  10971. (calendar-gregorian-from-absolute date)))
  10972. (select-window win))))
  10973. (defun org-goto-calendar (&optional arg)
  10974. "Go to the Emacs calendar at the current date.
  10975. If there is a time stamp in the current line, go to that date.
  10976. A prefix ARG can be used to force the current date."
  10977. (interactive "P")
  10978. (let ((tsr org-ts-regexp) diff
  10979. (calendar-move-hook nil)
  10980. (calendar-view-holidays-initially-flag nil)
  10981. (view-calendar-holidays-initially nil)
  10982. (calendar-view-diary-initially-flag nil)
  10983. (view-diary-entries-initially nil))
  10984. (if (or (org-at-timestamp-p)
  10985. (save-excursion
  10986. (beginning-of-line 1)
  10987. (looking-at (concat ".*" tsr))))
  10988. (let ((d1 (time-to-days (current-time)))
  10989. (d2 (time-to-days
  10990. (org-time-string-to-time (match-string 1)))))
  10991. (setq diff (- d2 d1))))
  10992. (calendar)
  10993. (calendar-goto-today)
  10994. (if (and diff (not arg)) (calendar-forward-day diff))))
  10995. (defun org-get-date-from-calendar ()
  10996. "Return a list (month day year) of date at point in calendar."
  10997. (with-current-buffer "*Calendar*"
  10998. (save-match-data
  10999. (calendar-cursor-to-date))))
  11000. (defun org-date-from-calendar ()
  11001. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11002. If there is already a time stamp at the cursor position, update it."
  11003. (interactive)
  11004. (if (org-at-timestamp-p t)
  11005. (org-timestamp-change 0 'calendar)
  11006. (let ((cal-date (org-get-date-from-calendar)))
  11007. (org-insert-time-stamp
  11008. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11009. (defun org-minutes-to-hh:mm-string (m)
  11010. "Compute H:MM from a number of minutes."
  11011. (let ((h (/ m 60)))
  11012. (setq m (- m (* 60 h)))
  11013. (format org-time-clocksum-format h m)))
  11014. (defun org-hh:mm-string-to-minutes (s)
  11015. "Convert a string H:MM to a number of minutes."
  11016. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11017. (+ (* (string-to-number (match-string 1 s)) 60)
  11018. (string-to-number (match-string 2 s)))
  11019. 0))
  11020. ;;;; Agenda files
  11021. ;;;###autoload
  11022. (defun org-iswitchb (&optional arg)
  11023. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11024. With a prefix argument, restrict available to files.
  11025. With two prefix arguments, restrict available buffers to agenda files.
  11026. Due to some yet unresolved reason, the global function
  11027. `iswitchb-mode' needs to be active for this function to work."
  11028. (interactive "P")
  11029. (require 'iswitchb)
  11030. (let ((enabled iswitchb-mode) blist)
  11031. (or enabled (iswitchb-mode 1))
  11032. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11033. ((equal arg '(16)) (org-buffer-list 'agenda))
  11034. (t (org-buffer-list))))
  11035. (unwind-protect
  11036. (let ((iswitchb-make-buflist-hook
  11037. (lambda ()
  11038. (setq iswitchb-temp-buflist
  11039. (mapcar 'buffer-name blist)))))
  11040. (switch-to-buffer
  11041. (iswitchb-read-buffer
  11042. "Switch-to: " nil t))
  11043. (or enabled (iswitchb-mode -1))))))
  11044. ;;;###autoload
  11045. (defun org-ido-switchb (&optional arg)
  11046. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11047. With a prefix argument, restrict available to files.
  11048. With two prefix arguments, restrict available buffers to agenda files."
  11049. (interactive "P")
  11050. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11051. ((equal arg '(16)) (org-buffer-list 'agenda))
  11052. (t (org-buffer-list)))))
  11053. (switch-to-buffer
  11054. (org-ido-completing-read "Org buffer: "
  11055. (mapcar 'buffer-name blist)
  11056. nil t))))
  11057. (defun org-buffer-list (&optional predicate exclude-tmp)
  11058. "Return a list of Org buffers.
  11059. PREDICATE can be `export', `files' or `agenda'.
  11060. export restrict the list to Export buffers.
  11061. files restrict the list to buffers visiting Org files.
  11062. agenda restrict the list to buffers visiting agenda files.
  11063. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11064. (let* ((bfn nil)
  11065. (agenda-files (and (eq predicate 'agenda)
  11066. (mapcar 'file-truename (org-agenda-files t))))
  11067. (filter
  11068. (cond
  11069. ((eq predicate 'files)
  11070. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11071. ((eq predicate 'export)
  11072. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11073. ((eq predicate 'agenda)
  11074. (lambda (b)
  11075. (with-current-buffer b
  11076. (and (eq major-mode 'org-mode)
  11077. (setq bfn (buffer-file-name b))
  11078. (member (file-truename bfn) agenda-files)))))
  11079. (t (lambda (b) (with-current-buffer b
  11080. (or (eq major-mode 'org-mode)
  11081. (string-match "\*Org .*Export"
  11082. (buffer-name b)))))))))
  11083. (delq nil
  11084. (mapcar
  11085. (lambda(b)
  11086. (if (and (funcall filter b)
  11087. (or (not exclude-tmp)
  11088. (not (string-match "tmp" (buffer-name b)))))
  11089. b
  11090. nil))
  11091. (buffer-list)))))
  11092. (defun org-agenda-files (&optional unrestricted archives)
  11093. "Get the list of agenda files.
  11094. Optional UNRESTRICTED means return the full list even if a restriction
  11095. is currently in place.
  11096. When ARCHIVES is t, include all archive files hat are really being
  11097. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11098. `org-agenda-archives-mode' is t."
  11099. (let ((files
  11100. (cond
  11101. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11102. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11103. ((listp org-agenda-files) org-agenda-files)
  11104. (t (error "Invalid value of `org-agenda-files'")))))
  11105. (setq files (apply 'append
  11106. (mapcar (lambda (f)
  11107. (if (file-directory-p f)
  11108. (directory-files
  11109. f t org-agenda-file-regexp)
  11110. (list f)))
  11111. files)))
  11112. (when org-agenda-skip-unavailable-files
  11113. (setq files (delq nil
  11114. (mapcar (function
  11115. (lambda (file)
  11116. (and (file-readable-p file) file)))
  11117. files))))
  11118. (when (or (eq archives t)
  11119. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11120. (setq files (org-add-archive-files files)))
  11121. files))
  11122. (defun org-edit-agenda-file-list ()
  11123. "Edit the list of agenda files.
  11124. Depending on setup, this either uses customize to edit the variable
  11125. `org-agenda-files', or it visits the file that is holding the list. In the
  11126. latter case, the buffer is set up in a way that saving it automatically kills
  11127. the buffer and restores the previous window configuration."
  11128. (interactive)
  11129. (if (stringp org-agenda-files)
  11130. (let ((cw (current-window-configuration)))
  11131. (find-file org-agenda-files)
  11132. (org-set-local 'org-window-configuration cw)
  11133. (org-add-hook 'after-save-hook
  11134. (lambda ()
  11135. (set-window-configuration
  11136. (prog1 org-window-configuration
  11137. (kill-buffer (current-buffer))))
  11138. (org-install-agenda-files-menu)
  11139. (message "New agenda file list installed"))
  11140. nil 'local)
  11141. (message "%s" (substitute-command-keys
  11142. "Edit list and finish with \\[save-buffer]")))
  11143. (customize-variable 'org-agenda-files)))
  11144. (defun org-store-new-agenda-file-list (list)
  11145. "Set new value for the agenda file list and save it correctly."
  11146. (if (stringp org-agenda-files)
  11147. (let ((f org-agenda-files) b)
  11148. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11149. (with-temp-file f
  11150. (insert (mapconcat 'identity list "\n") "\n")))
  11151. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11152. (setq org-agenda-files list)
  11153. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11154. (defun org-read-agenda-file-list ()
  11155. "Read the list of agenda files from a file."
  11156. (when (file-directory-p org-agenda-files)
  11157. (error "`org-agenda-files' cannot be a single directory"))
  11158. (when (stringp org-agenda-files)
  11159. (with-temp-buffer
  11160. (insert-file-contents org-agenda-files)
  11161. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11162. ;;;###autoload
  11163. (defun org-cycle-agenda-files ()
  11164. "Cycle through the files in `org-agenda-files'.
  11165. If the current buffer visits an agenda file, find the next one in the list.
  11166. If the current buffer does not, find the first agenda file."
  11167. (interactive)
  11168. (let* ((fs (org-agenda-files t))
  11169. (files (append fs (list (car fs))))
  11170. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11171. file)
  11172. (unless files (error "No agenda files"))
  11173. (catch 'exit
  11174. (while (setq file (pop files))
  11175. (if (equal (file-truename file) tcf)
  11176. (when (car files)
  11177. (find-file (car files))
  11178. (throw 'exit t))))
  11179. (find-file (car fs)))
  11180. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11181. (defun org-agenda-file-to-front (&optional to-end)
  11182. "Move/add the current file to the top of the agenda file list.
  11183. If the file is not present in the list, it is added to the front. If it is
  11184. present, it is moved there. With optional argument TO-END, add/move to the
  11185. end of the list."
  11186. (interactive "P")
  11187. (let ((org-agenda-skip-unavailable-files nil)
  11188. (file-alist (mapcar (lambda (x)
  11189. (cons (file-truename x) x))
  11190. (org-agenda-files t)))
  11191. (ctf (file-truename buffer-file-name))
  11192. x had)
  11193. (setq x (assoc ctf file-alist) had x)
  11194. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11195. (if to-end
  11196. (setq file-alist (append (delq x file-alist) (list x)))
  11197. (setq file-alist (cons x (delq x file-alist))))
  11198. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11199. (org-install-agenda-files-menu)
  11200. (message "File %s to %s of agenda file list"
  11201. (if had "moved" "added") (if to-end "end" "front"))))
  11202. (defun org-remove-file (&optional file)
  11203. "Remove current file from the list of files in variable `org-agenda-files'.
  11204. These are the files which are being checked for agenda entries.
  11205. Optional argument FILE means, use this file instead of the current."
  11206. (interactive)
  11207. (let* ((org-agenda-skip-unavailable-files nil)
  11208. (file (or file buffer-file-name))
  11209. (true-file (file-truename file))
  11210. (afile (abbreviate-file-name file))
  11211. (files (delq nil (mapcar
  11212. (lambda (x)
  11213. (if (equal true-file
  11214. (file-truename x))
  11215. nil x))
  11216. (org-agenda-files t)))))
  11217. (if (not (= (length files) (length (org-agenda-files t))))
  11218. (progn
  11219. (org-store-new-agenda-file-list files)
  11220. (org-install-agenda-files-menu)
  11221. (message "Removed file: %s" afile))
  11222. (message "File was not in list: %s (not removed)" afile))))
  11223. (defun org-file-menu-entry (file)
  11224. (vector file (list 'find-file file) t))
  11225. (defun org-check-agenda-file (file)
  11226. "Make sure FILE exists. If not, ask user what to do."
  11227. (when (not (file-exists-p file))
  11228. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11229. (abbreviate-file-name file))
  11230. (let ((r (downcase (read-char-exclusive))))
  11231. (cond
  11232. ((equal r ?r)
  11233. (org-remove-file file)
  11234. (throw 'nextfile t))
  11235. (t (error "Abort"))))))
  11236. (defun org-get-agenda-file-buffer (file)
  11237. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11238. it to the list of buffers which might be released later."
  11239. (let ((buf (org-find-base-buffer-visiting file)))
  11240. (if buf
  11241. buf ; just return it
  11242. ;; Make a new buffer and remember it
  11243. (setq buf (find-file-noselect file))
  11244. (if buf (push buf org-agenda-new-buffers))
  11245. buf)))
  11246. (defun org-release-buffers (blist)
  11247. "Release all buffers in list, asking the user for confirmation when needed.
  11248. When a buffer is unmodified, it is just killed. When modified, it is saved
  11249. \(if the user agrees) and then killed."
  11250. (let (buf file)
  11251. (while (setq buf (pop blist))
  11252. (setq file (buffer-file-name buf))
  11253. (when (and (buffer-modified-p buf)
  11254. file
  11255. (y-or-n-p (format "Save file %s? " file)))
  11256. (with-current-buffer buf (save-buffer)))
  11257. (kill-buffer buf))))
  11258. (defun org-prepare-agenda-buffers (files)
  11259. "Create buffers for all agenda files, protect archived trees and comments."
  11260. (interactive)
  11261. (let ((pa '(:org-archived t))
  11262. (pc '(:org-comment t))
  11263. (pall '(:org-archived t :org-comment t))
  11264. (inhibit-read-only t)
  11265. (rea (concat ":" org-archive-tag ":"))
  11266. bmp file re)
  11267. (save-excursion
  11268. (save-restriction
  11269. (while (setq file (pop files))
  11270. (if (bufferp file)
  11271. (set-buffer file)
  11272. (org-check-agenda-file file)
  11273. (set-buffer (org-get-agenda-file-buffer file)))
  11274. (widen)
  11275. (setq bmp (buffer-modified-p))
  11276. (org-refresh-category-properties)
  11277. (setq org-todo-keywords-for-agenda
  11278. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11279. (setq org-done-keywords-for-agenda
  11280. (append org-done-keywords-for-agenda org-done-keywords))
  11281. (setq org-todo-keyword-alist-for-agenda
  11282. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11283. (setq org-tag-alist-for-agenda
  11284. (append org-tag-alist-for-agenda org-tag-alist))
  11285. (save-excursion
  11286. (remove-text-properties (point-min) (point-max) pall)
  11287. (when org-agenda-skip-archived-trees
  11288. (goto-char (point-min))
  11289. (while (re-search-forward rea nil t)
  11290. (if (org-on-heading-p t)
  11291. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11292. (goto-char (point-min))
  11293. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11294. (while (re-search-forward re nil t)
  11295. (add-text-properties
  11296. (match-beginning 0) (org-end-of-subtree t) pc)))
  11297. (set-buffer-modified-p bmp))))
  11298. (setq org-todo-keyword-alist-for-agenda
  11299. (org-uniquify org-todo-keyword-alist-for-agenda)
  11300. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11301. ;;;; Embedded LaTeX
  11302. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11303. "Keymap for the minor `org-cdlatex-mode'.")
  11304. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11305. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11306. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11307. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11308. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11309. (defvar org-cdlatex-texmathp-advice-is-done nil
  11310. "Flag remembering if we have applied the advice to texmathp already.")
  11311. (define-minor-mode org-cdlatex-mode
  11312. "Toggle the minor `org-cdlatex-mode'.
  11313. This mode supports entering LaTeX environment and math in LaTeX fragments
  11314. in Org-mode.
  11315. \\{org-cdlatex-mode-map}"
  11316. nil " OCDL" nil
  11317. (when org-cdlatex-mode (require 'cdlatex))
  11318. (unless org-cdlatex-texmathp-advice-is-done
  11319. (setq org-cdlatex-texmathp-advice-is-done t)
  11320. (defadvice texmathp (around org-math-always-on activate)
  11321. "Always return t in org-mode buffers.
  11322. This is because we want to insert math symbols without dollars even outside
  11323. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11324. an embedded LaTeX fragment, let texmathp do its job.
  11325. \\[org-cdlatex-mode-map]"
  11326. (interactive)
  11327. (let (p)
  11328. (cond
  11329. ((not (org-mode-p)) ad-do-it)
  11330. ((eq this-command 'cdlatex-math-symbol)
  11331. (setq ad-return-value t
  11332. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11333. (t
  11334. (let ((p (org-inside-LaTeX-fragment-p)))
  11335. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11336. (setq ad-return-value t
  11337. texmathp-why '("Org-mode embedded math" . 0))
  11338. (if p ad-do-it)))))))))
  11339. (defun turn-on-org-cdlatex ()
  11340. "Unconditionally turn on `org-cdlatex-mode'."
  11341. (org-cdlatex-mode 1))
  11342. (defun org-inside-LaTeX-fragment-p ()
  11343. "Test if point is inside a LaTeX fragment.
  11344. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11345. sequence appearing also before point.
  11346. Even though the matchers for math are configurable, this function assumes
  11347. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11348. delimiters are skipped when they have been removed by customization.
  11349. The return value is nil, or a cons cell with the delimiter and
  11350. and the position of this delimiter.
  11351. This function does a reasonably good job, but can locally be fooled by
  11352. for example currency specifications. For example it will assume being in
  11353. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11354. fragments that are properly closed, but during editing, we have to live
  11355. with the uncertainty caused by missing closing delimiters. This function
  11356. looks only before point, not after."
  11357. (catch 'exit
  11358. (let ((pos (point))
  11359. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11360. (lim (progn
  11361. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11362. (point)))
  11363. dd-on str (start 0) m re)
  11364. (goto-char pos)
  11365. (when dodollar
  11366. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11367. re (nth 1 (assoc "$" org-latex-regexps)))
  11368. (while (string-match re str start)
  11369. (cond
  11370. ((= (match-end 0) (length str))
  11371. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11372. ((= (match-end 0) (- (length str) 5))
  11373. (throw 'exit nil))
  11374. (t (setq start (match-end 0))))))
  11375. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11376. (goto-char pos)
  11377. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11378. (and (match-beginning 2) (throw 'exit nil))
  11379. ;; count $$
  11380. (while (re-search-backward "\\$\\$" lim t)
  11381. (setq dd-on (not dd-on)))
  11382. (goto-char pos)
  11383. (if dd-on (cons "$$" m))))))
  11384. (defun org-try-cdlatex-tab ()
  11385. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11386. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11387. - inside a LaTeX fragment, or
  11388. - after the first word in a line, where an abbreviation expansion could
  11389. insert a LaTeX environment."
  11390. (when org-cdlatex-mode
  11391. (cond
  11392. ((save-excursion
  11393. (skip-chars-backward "a-zA-Z0-9*")
  11394. (skip-chars-backward " \t")
  11395. (bolp))
  11396. (cdlatex-tab) t)
  11397. ((org-inside-LaTeX-fragment-p)
  11398. (cdlatex-tab) t)
  11399. (t nil))))
  11400. (defun org-cdlatex-underscore-caret (&optional arg)
  11401. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11402. Revert to the normal definition outside of these fragments."
  11403. (interactive "P")
  11404. (if (org-inside-LaTeX-fragment-p)
  11405. (call-interactively 'cdlatex-sub-superscript)
  11406. (let (org-cdlatex-mode)
  11407. (call-interactively (key-binding (vector last-input-event))))))
  11408. (defun org-cdlatex-math-modify (&optional arg)
  11409. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11410. Revert to the normal definition outside of these fragments."
  11411. (interactive "P")
  11412. (if (org-inside-LaTeX-fragment-p)
  11413. (call-interactively 'cdlatex-math-modify)
  11414. (let (org-cdlatex-mode)
  11415. (call-interactively (key-binding (vector last-input-event))))))
  11416. (defvar org-latex-fragment-image-overlays nil
  11417. "List of overlays carrying the images of latex fragments.")
  11418. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11419. (defun org-remove-latex-fragment-image-overlays ()
  11420. "Remove all overlays with LaTeX fragment images in current buffer."
  11421. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11422. (setq org-latex-fragment-image-overlays nil))
  11423. (defun org-preview-latex-fragment (&optional subtree)
  11424. "Preview the LaTeX fragment at point, or all locally or globally.
  11425. If the cursor is in a LaTeX fragment, create the image and overlay
  11426. it over the source code. If there is no fragment at point, display
  11427. all fragments in the current text, from one headline to the next. With
  11428. prefix SUBTREE, display all fragments in the current subtree. With a
  11429. double prefix `C-u C-u', or when the cursor is before the first headline,
  11430. display all fragments in the buffer.
  11431. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11432. (interactive "P")
  11433. (org-remove-latex-fragment-image-overlays)
  11434. (save-excursion
  11435. (save-restriction
  11436. (let (beg end at msg)
  11437. (cond
  11438. ((or (equal subtree '(16))
  11439. (not (save-excursion
  11440. (re-search-backward (concat "^" outline-regexp) nil t))))
  11441. (setq beg (point-min) end (point-max)
  11442. msg "Creating images for buffer...%s"))
  11443. ((equal subtree '(4))
  11444. (org-back-to-heading)
  11445. (setq beg (point) end (org-end-of-subtree t)
  11446. msg "Creating images for subtree...%s"))
  11447. (t
  11448. (if (setq at (org-inside-LaTeX-fragment-p))
  11449. (goto-char (max (point-min) (- (cdr at) 2)))
  11450. (org-back-to-heading))
  11451. (setq beg (point) end (progn (outline-next-heading) (point))
  11452. msg (if at "Creating image...%s"
  11453. "Creating images for entry...%s"))))
  11454. (message msg "")
  11455. (narrow-to-region beg end)
  11456. (goto-char beg)
  11457. (org-format-latex
  11458. (concat "ltxpng/" (file-name-sans-extension
  11459. (file-name-nondirectory
  11460. buffer-file-name)))
  11461. default-directory 'overlays msg at 'forbuffer)
  11462. (message msg "done. Use `C-c C-c' to remove images.")))))
  11463. (defvar org-latex-regexps
  11464. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11465. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11466. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11467. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11468. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11469. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11470. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11471. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11472. "Regular expressions for matching embedded LaTeX.")
  11473. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11474. "Replace LaTeX fragments with links to an image, and produce images."
  11475. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11476. (let* ((prefixnodir (file-name-nondirectory prefix))
  11477. (absprefix (expand-file-name prefix dir))
  11478. (todir (file-name-directory absprefix))
  11479. (opt org-format-latex-options)
  11480. (matchers (plist-get opt :matchers))
  11481. (re-list org-latex-regexps)
  11482. (cnt 0) txt link beg end re e checkdir
  11483. m n block linkfile movefile ov)
  11484. ;; Check if there are old images files with this prefix, and remove them
  11485. (when (file-directory-p todir)
  11486. (mapc 'delete-file
  11487. (directory-files
  11488. todir 'full
  11489. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11490. ;; Check the different regular expressions
  11491. (while (setq e (pop re-list))
  11492. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11493. block (if (nth 3 e) "\n\n" ""))
  11494. (when (member m matchers)
  11495. (goto-char (point-min))
  11496. (while (re-search-forward re nil t)
  11497. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11498. (setq txt (match-string n)
  11499. beg (match-beginning n) end (match-end n)
  11500. cnt (1+ cnt)
  11501. linkfile (format "%s_%04d.png" prefix cnt)
  11502. movefile (format "%s_%04d.png" absprefix cnt)
  11503. link (concat block "[[file:" linkfile "]]" block))
  11504. (if msg (message msg cnt))
  11505. (goto-char beg)
  11506. (unless checkdir ; make sure the directory exists
  11507. (setq checkdir t)
  11508. (or (file-directory-p todir) (make-directory todir)))
  11509. (org-create-formula-image
  11510. txt movefile opt forbuffer)
  11511. (if overlays
  11512. (progn
  11513. (setq ov (org-make-overlay beg end))
  11514. (if (featurep 'xemacs)
  11515. (progn
  11516. (org-overlay-put ov 'invisible t)
  11517. (org-overlay-put
  11518. ov 'end-glyph
  11519. (make-glyph (vector 'png :file movefile))))
  11520. (org-overlay-put
  11521. ov 'display
  11522. (list 'image :type 'png :file movefile :ascent 'center)))
  11523. (push ov org-latex-fragment-image-overlays)
  11524. (goto-char end))
  11525. (delete-region beg end)
  11526. (insert link))))))))
  11527. ;; This function borrows from Ganesh Swami's latex2png.el
  11528. (defun org-create-formula-image (string tofile options buffer)
  11529. (let* ((tmpdir (if (featurep 'xemacs)
  11530. (temp-directory)
  11531. temporary-file-directory))
  11532. (texfilebase (make-temp-name
  11533. (expand-file-name "orgtex" tmpdir)))
  11534. (texfile (concat texfilebase ".tex"))
  11535. (dvifile (concat texfilebase ".dvi"))
  11536. (pngfile (concat texfilebase ".png"))
  11537. (fnh (if (featurep 'xemacs)
  11538. (font-height (get-face-font 'default))
  11539. (face-attribute 'default :height nil)))
  11540. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11541. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11542. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11543. "Black"))
  11544. (bg (or (plist-get options (if buffer :background :html-background))
  11545. "Transparent")))
  11546. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11547. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11548. (with-temp-file texfile
  11549. (insert org-format-latex-header
  11550. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11551. (let ((dir default-directory))
  11552. (condition-case nil
  11553. (progn
  11554. (cd tmpdir)
  11555. (call-process "latex" nil nil nil texfile))
  11556. (error nil))
  11557. (cd dir))
  11558. (if (not (file-exists-p dvifile))
  11559. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11560. (condition-case nil
  11561. (call-process "dvipng" nil nil nil
  11562. "-E" "-fg" fg "-bg" bg
  11563. "-D" dpi
  11564. ;;"-x" scale "-y" scale
  11565. "-T" "tight"
  11566. "-o" pngfile
  11567. dvifile)
  11568. (error nil))
  11569. (if (not (file-exists-p pngfile))
  11570. (progn (message "Failed to create png file from %s" texfile) nil)
  11571. ;; Use the requested file name and clean up
  11572. (copy-file pngfile tofile 'replace)
  11573. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11574. (delete-file (concat texfilebase e)))
  11575. pngfile))))
  11576. (defun org-dvipng-color (attr)
  11577. "Return an rgb color specification for dvipng."
  11578. (apply 'format "rgb %s %s %s"
  11579. (mapcar 'org-normalize-color
  11580. (color-values (face-attribute 'default attr nil)))))
  11581. (defun org-normalize-color (value)
  11582. "Return string to be used as color value for an RGB component."
  11583. (format "%g" (/ value 65535.0)))
  11584. ;;;; Key bindings
  11585. ;; Make `C-c C-x' a prefix key
  11586. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11587. ;; TAB key with modifiers
  11588. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11589. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11590. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11591. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11592. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11593. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11594. ;; The following line is necessary under Suse GNU/Linux
  11595. (unless (featurep 'xemacs)
  11596. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11597. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11598. (define-key org-mode-map [backtab] 'org-shifttab)
  11599. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11600. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11601. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11602. ;; Cursor keys with modifiers
  11603. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11604. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11605. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11606. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11607. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11608. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11609. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11610. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11611. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11612. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11613. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11614. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11615. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11616. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11617. ;;; Extra keys for tty access.
  11618. ;; We only set them when really needed because otherwise the
  11619. ;; menus don't show the simple keys
  11620. (when (or org-use-extra-keys
  11621. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11622. (not window-system))
  11623. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11624. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11625. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11626. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11627. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11628. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11629. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11630. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11631. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11632. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11633. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11634. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11635. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11636. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11637. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11638. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11639. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11640. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11641. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11642. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11643. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11644. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11645. ;; All the other keys
  11646. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11647. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11648. (if (boundp 'narrow-map)
  11649. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11650. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11651. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11652. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11653. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11654. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11655. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11656. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11657. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11658. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11659. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11660. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11661. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11662. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11663. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11664. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11665. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11666. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11667. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11668. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11669. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11670. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11671. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11672. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11673. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11674. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11675. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11676. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11677. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11678. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11679. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11680. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11681. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11682. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11683. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11684. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11685. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11686. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11687. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11688. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11689. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11690. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11691. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11692. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11693. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11694. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11695. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11696. (org-defkey org-mode-map "\C-m" 'org-return)
  11697. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11698. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11699. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11700. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11701. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11702. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11703. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11704. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11705. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11706. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11707. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11708. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11709. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11710. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11711. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11712. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11713. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11714. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11715. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11716. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11717. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11718. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11719. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11720. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11721. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11722. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11723. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11724. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11725. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11726. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11727. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11728. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11729. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11730. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11731. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11732. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11733. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  11734. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11735. (when (featurep 'xemacs)
  11736. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11737. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11738. (defun org-self-insert-command (N)
  11739. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11740. If the cursor is in a table looking at whitespace, the whitespace is
  11741. overwritten, and the table is not marked as requiring realignment."
  11742. (interactive "p")
  11743. (if (and (org-table-p)
  11744. (progn
  11745. ;; check if we blank the field, and if that triggers align
  11746. (and (featurep 'org-table) org-table-auto-blank-field
  11747. (member last-command
  11748. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11749. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11750. ;; got extra space, this field does not determine column width
  11751. (let (org-table-may-need-update) (org-table-blank-field))
  11752. ;; no extra space, this field may determine column width
  11753. (org-table-blank-field)))
  11754. t)
  11755. (eq N 1)
  11756. (looking-at "[^|\n]* |"))
  11757. (let (org-table-may-need-update)
  11758. (goto-char (1- (match-end 0)))
  11759. (delete-backward-char 1)
  11760. (goto-char (match-beginning 0))
  11761. (self-insert-command N))
  11762. (setq org-table-may-need-update t)
  11763. (self-insert-command N)
  11764. (org-fix-tags-on-the-fly)))
  11765. (defun org-fix-tags-on-the-fly ()
  11766. (when (and (equal (char-after (point-at-bol)) ?*)
  11767. (org-on-heading-p))
  11768. (org-align-tags-here org-tags-column)))
  11769. (defun org-delete-backward-char (N)
  11770. "Like `delete-backward-char', insert whitespace at field end in tables.
  11771. When deleting backwards, in tables this function will insert whitespace in
  11772. front of the next \"|\" separator, to keep the table aligned. The table will
  11773. still be marked for re-alignment if the field did fill the entire column,
  11774. because, in this case the deletion might narrow the column."
  11775. (interactive "p")
  11776. (if (and (org-table-p)
  11777. (eq N 1)
  11778. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11779. (looking-at ".*?|"))
  11780. (let ((pos (point))
  11781. (noalign (looking-at "[^|\n\r]* |"))
  11782. (c org-table-may-need-update))
  11783. (backward-delete-char N)
  11784. (skip-chars-forward "^|")
  11785. (insert " ")
  11786. (goto-char (1- pos))
  11787. ;; noalign: if there were two spaces at the end, this field
  11788. ;; does not determine the width of the column.
  11789. (if noalign (setq org-table-may-need-update c)))
  11790. (backward-delete-char N)
  11791. (org-fix-tags-on-the-fly)))
  11792. (defun org-delete-char (N)
  11793. "Like `delete-char', but insert whitespace at field end in tables.
  11794. When deleting characters, in tables this function will insert whitespace in
  11795. front of the next \"|\" separator, to keep the table aligned. The table will
  11796. still be marked for re-alignment if the field did fill the entire column,
  11797. because, in this case the deletion might narrow the column."
  11798. (interactive "p")
  11799. (if (and (org-table-p)
  11800. (not (bolp))
  11801. (not (= (char-after) ?|))
  11802. (eq N 1))
  11803. (if (looking-at ".*?|")
  11804. (let ((pos (point))
  11805. (noalign (looking-at "[^|\n\r]* |"))
  11806. (c org-table-may-need-update))
  11807. (replace-match (concat
  11808. (substring (match-string 0) 1 -1)
  11809. " |"))
  11810. (goto-char pos)
  11811. ;; noalign: if there were two spaces at the end, this field
  11812. ;; does not determine the width of the column.
  11813. (if noalign (setq org-table-may-need-update c)))
  11814. (delete-char N))
  11815. (delete-char N)
  11816. (org-fix-tags-on-the-fly)))
  11817. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11818. (put 'org-self-insert-command 'delete-selection t)
  11819. (put 'orgtbl-self-insert-command 'delete-selection t)
  11820. (put 'org-delete-char 'delete-selection 'supersede)
  11821. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11822. (put 'org-yank 'delete-selection 'yank)
  11823. ;; Make `flyspell-mode' delay after some commands
  11824. (put 'org-self-insert-command 'flyspell-delayed t)
  11825. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11826. (put 'org-delete-char 'flyspell-delayed t)
  11827. (put 'org-delete-backward-char 'flyspell-delayed t)
  11828. ;; Make pabbrev-mode expand after org-mode commands
  11829. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11830. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11831. ;; How to do this: Measure non-white length of current string
  11832. ;; If equal to column width, we should realign.
  11833. (defun org-remap (map &rest commands)
  11834. "In MAP, remap the functions given in COMMANDS.
  11835. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11836. (let (new old)
  11837. (while commands
  11838. (setq old (pop commands) new (pop commands))
  11839. (if (fboundp 'command-remapping)
  11840. (org-defkey map (vector 'remap old) new)
  11841. (substitute-key-definition old new map global-map)))))
  11842. (when (eq org-enable-table-editor 'optimized)
  11843. ;; If the user wants maximum table support, we need to hijack
  11844. ;; some standard editing functions
  11845. (org-remap org-mode-map
  11846. 'self-insert-command 'org-self-insert-command
  11847. 'delete-char 'org-delete-char
  11848. 'delete-backward-char 'org-delete-backward-char)
  11849. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11850. (defun org-shiftcursor-error ()
  11851. "Throw an error, a modified cursor command was applied in wrong context."
  11852. (error "This command is active in special context like tables, headlines or items"))
  11853. (defun org-shiftselect-error ()
  11854. "Throw an error because Shift-Cursor command was applied in wrong context."
  11855. (if (and (boundp 'shift-select-mode) shift-select-mode)
  11856. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  11857. (error "This command works only in special context like headlines or timestamps.")))
  11858. (defun org-call-for-shift-select (cmd)
  11859. (let ((this-command-keys-shift-translated t))
  11860. (call-interactively cmd)))
  11861. (defun org-shifttab (&optional arg)
  11862. "Global visibility cycling or move to previous table field.
  11863. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11864. on context.
  11865. See the individual commands for more information."
  11866. (interactive "P")
  11867. (cond
  11868. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11869. ((integerp arg)
  11870. (message "Content view to level: %d" arg)
  11871. (org-content (prefix-numeric-value arg))
  11872. (setq org-cycle-global-status 'overview))
  11873. (t (call-interactively 'org-global-cycle))))
  11874. (defun org-shiftmetaleft ()
  11875. "Promote subtree or delete table column.
  11876. Calls `org-promote-subtree', `org-outdent-item',
  11877. or `org-table-delete-column', depending on context.
  11878. See the individual commands for more information."
  11879. (interactive)
  11880. (cond
  11881. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11882. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11883. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11884. (t (org-shiftcursor-error))))
  11885. (defun org-shiftmetaright ()
  11886. "Demote subtree or insert table column.
  11887. Calls `org-demote-subtree', `org-indent-item',
  11888. or `org-table-insert-column', depending on context.
  11889. See the individual commands for more information."
  11890. (interactive)
  11891. (cond
  11892. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11893. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11894. ((org-at-item-p) (call-interactively 'org-indent-item))
  11895. (t (org-shiftcursor-error))))
  11896. (defun org-shiftmetaup (&optional arg)
  11897. "Move subtree up or kill table row.
  11898. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11899. `org-move-item-up' depending on context. See the individual commands
  11900. for more information."
  11901. (interactive "P")
  11902. (cond
  11903. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11904. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11905. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11906. (t (org-shiftcursor-error))))
  11907. (defun org-shiftmetadown (&optional arg)
  11908. "Move subtree down or insert table row.
  11909. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11910. `org-move-item-down', depending on context. See the individual
  11911. commands for more information."
  11912. (interactive "P")
  11913. (cond
  11914. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11915. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11916. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11917. (t (org-shiftcursor-error))))
  11918. (defun org-metaleft (&optional arg)
  11919. "Promote heading or move table column to left.
  11920. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11921. With no specific context, calls the Emacs default `backward-word'.
  11922. See the individual commands for more information."
  11923. (interactive "P")
  11924. (cond
  11925. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11926. ((or (org-on-heading-p) (org-region-active-p))
  11927. (call-interactively 'org-do-promote))
  11928. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11929. (t (call-interactively 'backward-word))))
  11930. (defun org-metaright (&optional arg)
  11931. "Demote subtree or move table column to right.
  11932. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11933. With no specific context, calls the Emacs default `forward-word'.
  11934. See the individual commands for more information."
  11935. (interactive "P")
  11936. (cond
  11937. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11938. ((or (org-on-heading-p) (org-region-active-p))
  11939. (call-interactively 'org-do-demote))
  11940. ((org-at-item-p) (call-interactively 'org-indent-item))
  11941. (t (call-interactively 'forward-word))))
  11942. (defun org-metaup (&optional arg)
  11943. "Move subtree up or move table row up.
  11944. Calls `org-move-subtree-up' or `org-table-move-row' or
  11945. `org-move-item-up', depending on context. See the individual commands
  11946. for more information."
  11947. (interactive "P")
  11948. (cond
  11949. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11950. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11951. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11952. (t (transpose-lines 1) (beginning-of-line -1))))
  11953. (defun org-metadown (&optional arg)
  11954. "Move subtree down or move table row down.
  11955. Calls `org-move-subtree-down' or `org-table-move-row' or
  11956. `org-move-item-down', depending on context. See the individual
  11957. commands for more information."
  11958. (interactive "P")
  11959. (cond
  11960. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11961. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11962. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11963. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11964. (defun org-shiftup (&optional arg)
  11965. "Increase item in timestamp or increase priority of current headline.
  11966. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11967. depending on context. See the individual commands for more information."
  11968. (interactive "P")
  11969. (cond
  11970. ((and org-support-shift-select (org-region-active-p))
  11971. (org-call-for-shift-select 'previous-line))
  11972. ((org-at-timestamp-p t)
  11973. (call-interactively (if org-edit-timestamp-down-means-later
  11974. 'org-timestamp-down 'org-timestamp-up)))
  11975. ((and (not (eq org-support-shift-select 'always))
  11976. (org-on-heading-p))
  11977. (call-interactively 'org-priority-up))
  11978. ((and (not org-support-shift-select) (org-at-item-p))
  11979. (call-interactively 'org-previous-item))
  11980. ((org-clocktable-try-shift 'up arg))
  11981. (org-support-shift-select
  11982. (org-call-for-shift-select 'previous-line))
  11983. (t (org-shiftselect-error))))
  11984. (defun org-shiftdown (&optional arg)
  11985. "Decrease item in timestamp or decrease priority of current headline.
  11986. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11987. depending on context. See the individual commands for more information."
  11988. (interactive "P")
  11989. (cond
  11990. ((and org-support-shift-select (org-region-active-p))
  11991. (org-call-for-shift-select 'next-line))
  11992. ((org-at-timestamp-p t)
  11993. (call-interactively (if org-edit-timestamp-down-means-later
  11994. 'org-timestamp-up 'org-timestamp-down)))
  11995. ((and (not (eq org-support-shift-select 'always))
  11996. (org-on-heading-p))
  11997. (call-interactively 'org-priority-down))
  11998. ((and (not org-support-shift-select) (org-at-item-p))
  11999. (call-interactively 'org-next-item))
  12000. ((org-clocktable-try-shift 'down arg))
  12001. (org-support-shift-select
  12002. (org-call-for-shift-select 'next-line))
  12003. (t (org-shiftselect-error))))
  12004. (defun org-shiftright (&optional arg)
  12005. "Cycle the thing at point or in the current line, depending on context.
  12006. Depending on context, this does one of the following:
  12007. - switch a timestamp at point one day into the future
  12008. - on a headline, switch to the next TODO keyword.
  12009. - on an item, switch entire list to the next bullet type
  12010. - on a property line, switch to the next allowed value
  12011. - on a clocktable definition line, move time block into the future"
  12012. (interactive "P")
  12013. (cond
  12014. ((and org-support-shift-select (org-region-active-p))
  12015. (org-call-for-shift-select 'forward-char))
  12016. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12017. ((and (not (eq org-support-shift-select 'always))
  12018. (org-on-heading-p))
  12019. (org-call-with-arg 'org-todo 'right))
  12020. ((or (and org-support-shift-select
  12021. (not (eq org-support-shift-select 'always))
  12022. (org-at-item-bullet-p))
  12023. (and (not org-support-shift-select) (org-at-item-p)))
  12024. (org-call-with-arg 'org-cycle-list-bullet nil))
  12025. ((and (not (eq org-support-shift-select 'always))
  12026. (org-at-property-p))
  12027. (call-interactively 'org-property-next-allowed-value))
  12028. ((org-clocktable-try-shift 'right arg))
  12029. (org-support-shift-select
  12030. (org-call-for-shift-select 'forward-char))
  12031. (t (org-shiftselect-error))))
  12032. (defun org-shiftleft (&optional arg)
  12033. "Cycle the thing at point or in the current line, depending on context.
  12034. Depending on context, this does one of the following:
  12035. - switch a timestamp at point one day into the past
  12036. - on a headline, switch to the previous TODO keyword.
  12037. - on an item, switch entire list to the previous bullet type
  12038. - on a property line, switch to the previous allowed value
  12039. - on a clocktable definition line, move time block into the past"
  12040. (interactive "P")
  12041. (cond
  12042. ((and org-support-shift-select (org-region-active-p))
  12043. (org-call-for-shift-select 'backward-char))
  12044. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12045. ((and (not (eq org-support-shift-select 'always))
  12046. (org-on-heading-p))
  12047. (org-call-with-arg 'org-todo 'left))
  12048. ((or (and org-support-shift-select
  12049. (not (eq org-support-shift-select 'always))
  12050. (org-at-item-bullet-p))
  12051. (and (not org-support-shift-select) (org-at-item-p)))
  12052. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12053. ((and (not (eq org-support-shift-select 'always))
  12054. (org-at-property-p))
  12055. (call-interactively 'org-property-previous-allowed-value))
  12056. ((org-clocktable-try-shift 'left arg))
  12057. (org-support-shift-select
  12058. (org-call-for-shift-select 'backward-char))
  12059. (t (org-shiftselect-error))))
  12060. (defun org-shiftcontrolright ()
  12061. "Switch to next TODO set."
  12062. (interactive)
  12063. (cond
  12064. ((and org-support-shift-select (org-region-active-p))
  12065. (org-call-for-shift-select 'forward-word))
  12066. ((and (not (eq org-support-shift-select 'always))
  12067. (org-on-heading-p))
  12068. (org-call-with-arg 'org-todo 'nextset))
  12069. (org-support-shift-select
  12070. (org-call-for-shift-select 'forward-word))
  12071. (t (org-shiftselect-error))))
  12072. (defun org-shiftcontrolleft ()
  12073. "Switch to previous TODO set."
  12074. (interactive)
  12075. (cond
  12076. ((and org-support-shift-select (org-region-active-p))
  12077. (org-call-for-shift-select 'backward-word))
  12078. ((and (not (eq org-support-shift-select 'always))
  12079. (org-on-heading-p))
  12080. (org-call-with-arg 'org-todo 'previousset))
  12081. (org-support-shift-select
  12082. (org-call-for-shift-select 'backward-word))
  12083. (t (org-shiftselect-error))))
  12084. (defun org-ctrl-c-ret ()
  12085. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12086. (interactive)
  12087. (cond
  12088. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12089. (t (call-interactively 'org-insert-heading))))
  12090. (defun org-copy-special ()
  12091. "Copy region in table or copy current subtree.
  12092. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12093. See the individual commands for more information."
  12094. (interactive)
  12095. (call-interactively
  12096. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12097. (defun org-cut-special ()
  12098. "Cut region in table or cut current subtree.
  12099. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12100. See the individual commands for more information."
  12101. (interactive)
  12102. (call-interactively
  12103. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12104. (defun org-paste-special (arg)
  12105. "Paste rectangular region into table, or past subtree relative to level.
  12106. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12107. See the individual commands for more information."
  12108. (interactive "P")
  12109. (if (org-at-table-p)
  12110. (org-table-paste-rectangle)
  12111. (org-paste-subtree arg)))
  12112. (defun org-edit-special ()
  12113. "Call a special editor for the stuff at point.
  12114. When at a table, call the formula editor with `org-table-edit-formulas'.
  12115. When at the first line of an src example, call `org-edit-src-code'.
  12116. When in an #+include line, visit the include file. Otherwise call
  12117. `ffap' to visit the file at point."
  12118. (interactive)
  12119. (cond
  12120. ((org-at-table-p)
  12121. (call-interactively 'org-table-edit-formulas))
  12122. ((save-excursion
  12123. (beginning-of-line 1)
  12124. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12125. (find-file (org-trim (match-string 1))))
  12126. ((org-edit-src-code))
  12127. ((org-edit-fixed-width-region))
  12128. (t (call-interactively 'ffap))))
  12129. (defun org-ctrl-c-ctrl-c (&optional arg)
  12130. "Set tags in headline, or update according to changed information at point.
  12131. This command does many different things, depending on context:
  12132. - If the cursor is in a headline, prompt for tags and insert them
  12133. into the current line, aligned to `org-tags-column'. When called
  12134. with prefix arg, realign all tags in the current buffer.
  12135. - If the cursor is in one of the special #+KEYWORD lines, this
  12136. triggers scanning the buffer for these lines and updating the
  12137. information.
  12138. - If the cursor is inside a table, realign the table. This command
  12139. works even if the automatic table editor has been turned off.
  12140. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12141. the entire table.
  12142. - If the cursor is at a footnote reference or definition, jump to
  12143. the corresponding definition or references, respectively.
  12144. - If the cursor is a the beginning of a dynamic block, update it.
  12145. - If the cursor is inside a table created by the table.el package,
  12146. activate that table.
  12147. - If the current buffer is a remember buffer, close note and file
  12148. it. A prefix argument of 1 files to the default location
  12149. without further interaction. A prefix argument of 2 files to
  12150. the currently clocking task.
  12151. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12152. links in this buffer.
  12153. - If the cursor is on a numbered item in a plain list, renumber the
  12154. ordered list.
  12155. - If the cursor is on a checkbox, toggle it."
  12156. (interactive "P")
  12157. (let ((org-enable-table-editor t))
  12158. (cond
  12159. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12160. org-occur-highlights
  12161. org-latex-fragment-image-overlays)
  12162. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12163. (org-remove-occur-highlights)
  12164. (org-remove-latex-fragment-image-overlays)
  12165. (message "Temporary highlights/overlays removed from current buffer"))
  12166. ((and (local-variable-p 'org-finish-function (current-buffer))
  12167. (fboundp org-finish-function))
  12168. (funcall org-finish-function))
  12169. ((org-at-property-p)
  12170. (call-interactively 'org-property-action))
  12171. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12172. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12173. ((org-at-table.el-p)
  12174. (require 'table)
  12175. (beginning-of-line 1)
  12176. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12177. (call-interactively 'table-recognize-table))
  12178. ((org-at-table-p)
  12179. (org-table-maybe-eval-formula)
  12180. (if arg
  12181. (call-interactively 'org-table-recalculate)
  12182. (org-table-maybe-recalculate-line))
  12183. (call-interactively 'org-table-align))
  12184. ((or (org-footnote-at-reference-p)
  12185. (org-footnote-at-definition-p))
  12186. (call-interactively 'org-footnote-action))
  12187. ((org-at-item-checkbox-p)
  12188. (call-interactively 'org-toggle-checkbox))
  12189. ((org-at-item-p)
  12190. (call-interactively 'org-maybe-renumber-ordered-list))
  12191. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12192. ;; Dynamic block
  12193. (beginning-of-line 1)
  12194. (save-excursion (org-update-dblock)))
  12195. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12196. (cond
  12197. ((equal (match-string 1) "TBLFM")
  12198. ;; Recalculate the table before this line
  12199. (save-excursion
  12200. (beginning-of-line 1)
  12201. (skip-chars-backward " \r\n\t")
  12202. (if (org-at-table-p)
  12203. (org-call-with-arg 'org-table-recalculate t))))
  12204. (t
  12205. ; (org-set-regexps-and-options)
  12206. ; (org-restart-font-lock)
  12207. (let ((org-inhibit-startup t)) (org-mode-restart))
  12208. (message "Local setup has been refreshed"))))
  12209. (t (error "C-c C-c can do nothing useful at this location.")))))
  12210. (defun org-mode-restart ()
  12211. "Restart Org-mode, to scan again for special lines.
  12212. Also updates the keyword regular expressions."
  12213. (interactive)
  12214. (org-mode)
  12215. (message "Org-mode restarted"))
  12216. (defun org-kill-note-or-show-branches ()
  12217. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12218. (interactive)
  12219. (if (not org-finish-function)
  12220. (call-interactively 'show-branches)
  12221. (let ((org-note-abort t))
  12222. (funcall org-finish-function))))
  12223. (defun org-return (&optional indent)
  12224. "Goto next table row or insert a newline.
  12225. Calls `org-table-next-row' or `newline', depending on context.
  12226. See the individual commands for more information."
  12227. (interactive)
  12228. (cond
  12229. ((bobp) (if indent (newline-and-indent) (newline)))
  12230. ((and (org-at-heading-p)
  12231. (looking-at
  12232. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12233. (org-show-entry)
  12234. (end-of-line 1)
  12235. (newline))
  12236. ((org-at-table-p)
  12237. (org-table-justify-field-maybe)
  12238. (call-interactively 'org-table-next-row))
  12239. (t (if indent (newline-and-indent) (newline)))))
  12240. (defun org-return-indent ()
  12241. "Goto next table row or insert a newline and indent.
  12242. Calls `org-table-next-row' or `newline-and-indent', depending on
  12243. context. See the individual commands for more information."
  12244. (interactive)
  12245. (org-return t))
  12246. (defun org-ctrl-c-star ()
  12247. "Compute table, or change heading status of lines.
  12248. Calls `org-table-recalculate' or `org-toggle-heading',
  12249. depending on context."
  12250. (interactive)
  12251. (cond
  12252. ((org-at-table-p)
  12253. (call-interactively 'org-table-recalculate))
  12254. (t
  12255. ;; Convert all lines in region to list items
  12256. (call-interactively 'org-toggle-heading))))
  12257. (defun org-ctrl-c-minus ()
  12258. "Insert separator line in table or modify bullet status of line.
  12259. Also turns a plain line or a region of lines into list items.
  12260. Calls `org-table-insert-hline', `org-toggle-item', or
  12261. `org-cycle-list-bullet', depending on context."
  12262. (interactive)
  12263. (cond
  12264. ((org-at-table-p)
  12265. (call-interactively 'org-table-insert-hline))
  12266. ((org-region-active-p)
  12267. (call-interactively 'org-toggle-item))
  12268. ((org-in-item-p)
  12269. (call-interactively 'org-cycle-list-bullet))
  12270. (t
  12271. (call-interactively 'org-toggle-item))))
  12272. (defun org-toggle-item ()
  12273. "Convert headings or normal lines to items, items to normal lines.
  12274. If there is no active region, only the current line is considered.
  12275. If the first line in the region is a headline, convert all headlines to items.
  12276. If the first line in the region is an item, convert all items to normal lines.
  12277. If the first line is normal text, add an item bullet to each line."
  12278. (interactive)
  12279. (let (l2 l beg end)
  12280. (if (org-region-active-p)
  12281. (setq beg (region-beginning) end (region-end))
  12282. (setq beg (point-at-bol)
  12283. end (min (1+ (point-at-eol)) (point-max))))
  12284. (save-excursion
  12285. (goto-char end)
  12286. (setq l2 (org-current-line))
  12287. (goto-char beg)
  12288. (beginning-of-line 1)
  12289. (setq l (1- (org-current-line)))
  12290. (if (org-at-item-p)
  12291. ;; We already have items, de-itemize
  12292. (while (< (setq l (1+ l)) l2)
  12293. (when (org-at-item-p)
  12294. (goto-char (match-beginning 2))
  12295. (delete-region (match-beginning 2) (match-end 2))
  12296. (and (looking-at "[ \t]+") (replace-match "")))
  12297. (beginning-of-line 2))
  12298. (if (org-on-heading-p)
  12299. ;; Headings, convert to items
  12300. (while (< (setq l (1+ l)) l2)
  12301. (if (looking-at org-outline-regexp)
  12302. (replace-match "- " t t))
  12303. (beginning-of-line 2))
  12304. ;; normal lines, turn them into items
  12305. (while (< (setq l (1+ l)) l2)
  12306. (unless (org-at-item-p)
  12307. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12308. (replace-match "\\1- \\2")))
  12309. (beginning-of-line 2)))))))
  12310. (defun org-toggle-heading (&optional nstars)
  12311. "Convert headings to normal text, or items or text to headings.
  12312. If there is no active region, only the current line is considered.
  12313. If the first line is a heading, remove the stars from all headlines
  12314. in the region.
  12315. If the first line is a plain list item, turn all plain list items into
  12316. headings.
  12317. If the first line is a normal line, turn each and every line in the region
  12318. into a heading.
  12319. When converting a line into a heading, the number of stars is chosen
  12320. such that the lines become children of the current entry. However, when
  12321. a prefix argument is given, its value determines the number of stars to add."
  12322. (interactive "P")
  12323. (let (l2 l itemp beg end)
  12324. (if (org-region-active-p)
  12325. (setq beg (region-beginning) end (region-end))
  12326. (setq beg (point-at-bol)
  12327. end (min (1+ (point-at-eol)) (point-max))))
  12328. (save-excursion
  12329. (goto-char end)
  12330. (setq l2 (org-current-line))
  12331. (goto-char beg)
  12332. (beginning-of-line 1)
  12333. (setq l (1- (org-current-line)))
  12334. (if (org-on-heading-p)
  12335. ;; We already have headlines, de-star them
  12336. (while (< (setq l (1+ l)) l2)
  12337. (when (org-on-heading-p t)
  12338. (and (looking-at outline-regexp) (replace-match "")))
  12339. (beginning-of-line 2))
  12340. (setq itemp (org-at-item-p))
  12341. (let* ((stars
  12342. (if nstars
  12343. (make-string (prefix-numeric-value current-prefix-arg)
  12344. ?*)
  12345. (save-excursion
  12346. (re-search-backward org-complex-heading-regexp nil t)
  12347. (or (match-string 1) "*"))))
  12348. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12349. (rpl (concat stars add-stars " ")))
  12350. (while (< (setq l (1+ l)) l2)
  12351. (if itemp
  12352. (and (org-at-item-p) (replace-match rpl t t))
  12353. (unless (org-on-heading-p)
  12354. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12355. (replace-match (concat rpl (match-string 2))))))
  12356. (beginning-of-line 2)))))))
  12357. (defun org-meta-return (&optional arg)
  12358. "Insert a new heading or wrap a region in a table.
  12359. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12360. See the individual commands for more information."
  12361. (interactive "P")
  12362. (cond
  12363. ((org-at-table-p)
  12364. (call-interactively 'org-table-wrap-region))
  12365. (t (call-interactively 'org-insert-heading))))
  12366. ;;; Menu entries
  12367. ;; Define the Org-mode menus
  12368. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12369. '("Tbl"
  12370. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12371. ["Next Field" org-cycle (org-at-table-p)]
  12372. ["Previous Field" org-shifttab (org-at-table-p)]
  12373. ["Next Row" org-return (org-at-table-p)]
  12374. "--"
  12375. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12376. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12377. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12378. "--"
  12379. ("Column"
  12380. ["Move Column Left" org-metaleft (org-at-table-p)]
  12381. ["Move Column Right" org-metaright (org-at-table-p)]
  12382. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12383. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12384. ("Row"
  12385. ["Move Row Up" org-metaup (org-at-table-p)]
  12386. ["Move Row Down" org-metadown (org-at-table-p)]
  12387. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12388. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12389. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12390. "--"
  12391. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12392. ("Rectangle"
  12393. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12394. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12395. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12396. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12397. "--"
  12398. ("Calculate"
  12399. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12400. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12401. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12402. "--"
  12403. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12404. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12405. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12406. "--"
  12407. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12408. "--"
  12409. ["Sum Column/Rectangle" org-table-sum
  12410. (or (org-at-table-p) (org-region-active-p))]
  12411. ["Which Column?" org-table-current-column (org-at-table-p)])
  12412. ["Debug Formulas"
  12413. org-table-toggle-formula-debugger
  12414. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12415. ["Show Col/Row Numbers"
  12416. org-table-toggle-coordinate-overlays
  12417. :style toggle
  12418. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12419. "--"
  12420. ["Create" org-table-create (and (not (org-at-table-p))
  12421. org-enable-table-editor)]
  12422. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12423. ["Import from File" org-table-import (not (org-at-table-p))]
  12424. ["Export to File" org-table-export (org-at-table-p)]
  12425. "--"
  12426. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12427. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12428. '("Org"
  12429. ("Show/Hide"
  12430. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12431. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12432. ["Sparse Tree..." org-sparse-tree t]
  12433. ["Reveal Context" org-reveal t]
  12434. ["Show All" show-all t]
  12435. "--"
  12436. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12437. "--"
  12438. ["New Heading" org-insert-heading t]
  12439. ("Navigate Headings"
  12440. ["Up" outline-up-heading t]
  12441. ["Next" outline-next-visible-heading t]
  12442. ["Previous" outline-previous-visible-heading t]
  12443. ["Next Same Level" outline-forward-same-level t]
  12444. ["Previous Same Level" outline-backward-same-level t]
  12445. "--"
  12446. ["Jump" org-goto t])
  12447. ("Edit Structure"
  12448. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12449. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12450. "--"
  12451. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12452. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12453. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12454. "--"
  12455. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12456. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12457. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12458. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12459. "--"
  12460. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12461. "--"
  12462. ["Convert to odd levels" org-convert-to-odd-levels t]
  12463. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12464. ("Editing"
  12465. ["Emphasis..." org-emphasize t]
  12466. ["Edit Source Example" org-edit-special t]
  12467. "--"
  12468. ["Footnote new/jump" org-footnote-action t]
  12469. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12470. ("Archive"
  12471. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12472. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12473. ; :active t :keys "C-u C-c C-x C-a"]
  12474. ["Sparse trees open ARCHIVE trees"
  12475. (setq org-sparse-tree-open-archived-trees
  12476. (not org-sparse-tree-open-archived-trees))
  12477. :style toggle :selected org-sparse-tree-open-archived-trees]
  12478. ["Cycling opens ARCHIVE trees"
  12479. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12480. :style toggle :selected org-cycle-open-archived-trees]
  12481. "--"
  12482. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12483. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12484. ; ["Check and Move Children" (org-archive-subtree '(4))
  12485. ; :active t :keys "C-u C-c C-x C-s"]
  12486. )
  12487. "--"
  12488. ("TODO Lists"
  12489. ["TODO/DONE/-" org-todo t]
  12490. ("Select keyword"
  12491. ["Next keyword" org-shiftright (org-on-heading-p)]
  12492. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12493. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12494. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12495. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12496. ["Show TODO Tree" org-show-todo-tree t]
  12497. ["Global TODO list" org-todo-list t]
  12498. "--"
  12499. ["Set Priority" org-priority t]
  12500. ["Priority Up" org-shiftup t]
  12501. ["Priority Down" org-shiftdown t])
  12502. ("TAGS and Properties"
  12503. ["Set Tags" org-set-tags-command t]
  12504. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12505. "--"
  12506. ["Set property" org-set-property t]
  12507. ["Column view of properties" org-columns t]
  12508. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12509. ("Dates and Scheduling"
  12510. ["Timestamp" org-time-stamp t]
  12511. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12512. ("Change Date"
  12513. ["1 Day Later" org-shiftright t]
  12514. ["1 Day Earlier" org-shiftleft t]
  12515. ["1 ... Later" org-shiftup t]
  12516. ["1 ... Earlier" org-shiftdown t])
  12517. ["Compute Time Range" org-evaluate-time-range t]
  12518. ["Schedule Item" org-schedule t]
  12519. ["Deadline" org-deadline t]
  12520. "--"
  12521. ["Custom time format" org-toggle-time-stamp-overlays
  12522. :style radio :selected org-display-custom-times]
  12523. "--"
  12524. ["Goto Calendar" org-goto-calendar t]
  12525. ["Date from Calendar" org-date-from-calendar t]
  12526. "--"
  12527. ["Start/Restart Timer" org-timer-start t]
  12528. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12529. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12530. ["Insert Timer String" org-timer t]
  12531. ["Insert Timer Item" org-timer-item t])
  12532. ("Logging work"
  12533. ["Clock in" org-clock-in t]
  12534. ["Clock out" org-clock-out t]
  12535. ["Clock cancel" org-clock-cancel t]
  12536. ["Goto running clock" org-clock-goto t]
  12537. ["Display times" org-clock-display t]
  12538. ["Create clock table" org-clock-report t]
  12539. "--"
  12540. ["Record DONE time"
  12541. (progn (setq org-log-done (not org-log-done))
  12542. (message "Switching to %s will %s record a timestamp"
  12543. (car org-done-keywords)
  12544. (if org-log-done "automatically" "not")))
  12545. :style toggle :selected org-log-done])
  12546. "--"
  12547. ["Agenda Command..." org-agenda t]
  12548. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12549. ("File List for Agenda")
  12550. ("Special views current file"
  12551. ["TODO Tree" org-show-todo-tree t]
  12552. ["Check Deadlines" org-check-deadlines t]
  12553. ["Timeline" org-timeline t]
  12554. ["Tags Tree" org-tags-sparse-tree t])
  12555. "--"
  12556. ("Hyperlinks"
  12557. ["Store Link (Global)" org-store-link t]
  12558. ["Insert Link" org-insert-link t]
  12559. ["Follow Link" org-open-at-point t]
  12560. "--"
  12561. ["Next link" org-next-link t]
  12562. ["Previous link" org-previous-link t]
  12563. "--"
  12564. ["Descriptive Links"
  12565. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12566. :style radio
  12567. :selected (member '(org-link) buffer-invisibility-spec)]
  12568. ["Literal Links"
  12569. (progn
  12570. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12571. :style radio
  12572. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12573. "--"
  12574. ["Export/Publish..." org-export t]
  12575. ("LaTeX"
  12576. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12577. :selected org-cdlatex-mode]
  12578. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12579. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12580. ["Modify math symbol" org-cdlatex-math-modify
  12581. (org-inside-LaTeX-fragment-p)]
  12582. ["Export LaTeX fragments as images"
  12583. (if (featurep 'org-exp)
  12584. (setq org-export-with-LaTeX-fragments
  12585. (not org-export-with-LaTeX-fragments))
  12586. (require 'org-exp))
  12587. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12588. org-export-with-LaTeX-fragments)])
  12589. "--"
  12590. ("Documentation"
  12591. ["Show Version" org-version t]
  12592. ["Info Documentation" org-info t])
  12593. ("Customize"
  12594. ["Browse Org Group" org-customize t]
  12595. "--"
  12596. ["Expand This Menu" org-create-customize-menu
  12597. (fboundp 'customize-menu-create)])
  12598. "--"
  12599. ["Refresh setup" org-mode-restart t]
  12600. ))
  12601. (defun org-info (&optional node)
  12602. "Read documentation for Org-mode in the info system.
  12603. With optional NODE, go directly to that node."
  12604. (interactive)
  12605. (info (format "(org)%s" (or node ""))))
  12606. (defun org-install-agenda-files-menu ()
  12607. (let ((bl (buffer-list)))
  12608. (save-excursion
  12609. (while bl
  12610. (set-buffer (pop bl))
  12611. (if (org-mode-p) (setq bl nil)))
  12612. (when (org-mode-p)
  12613. (easy-menu-change
  12614. '("Org") "File List for Agenda"
  12615. (append
  12616. (list
  12617. ["Edit File List" (org-edit-agenda-file-list) t]
  12618. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12619. ["Remove Current File from List" org-remove-file t]
  12620. ["Cycle through agenda files" org-cycle-agenda-files t]
  12621. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12622. "--")
  12623. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12624. ;;;; Documentation
  12625. ;;;###autoload
  12626. (defun org-require-autoloaded-modules ()
  12627. (interactive)
  12628. (mapc 'require
  12629. '(org-agenda org-archive org-clock org-colview
  12630. org-exp org-id org-export-latex org-publish
  12631. org-remember org-table)))
  12632. ;;;###autoload
  12633. (defun org-customize ()
  12634. "Call the customize function with org as argument."
  12635. (interactive)
  12636. (org-load-modules-maybe)
  12637. (org-require-autoloaded-modules)
  12638. (customize-browse 'org))
  12639. (defun org-create-customize-menu ()
  12640. "Create a full customization menu for Org-mode, insert it into the menu."
  12641. (interactive)
  12642. (org-load-modules-maybe)
  12643. (org-require-autoloaded-modules)
  12644. (if (fboundp 'customize-menu-create)
  12645. (progn
  12646. (easy-menu-change
  12647. '("Org") "Customize"
  12648. `(["Browse Org group" org-customize t]
  12649. "--"
  12650. ,(customize-menu-create 'org)
  12651. ["Set" Custom-set t]
  12652. ["Save" Custom-save t]
  12653. ["Reset to Current" Custom-reset-current t]
  12654. ["Reset to Saved" Custom-reset-saved t]
  12655. ["Reset to Standard Settings" Custom-reset-standard t]))
  12656. (message "\"Org\"-menu now contains full customization menu"))
  12657. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12658. ;;;; Miscellaneous stuff
  12659. ;;; Generally useful functions
  12660. (defun org-find-text-property-in-string (prop s)
  12661. "Return the first non-nil value of property PROP in string S."
  12662. (or (get-text-property 0 prop s)
  12663. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12664. prop s)))
  12665. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12666. "Display the given MESSAGE as a warning."
  12667. (if (fboundp 'display-warning)
  12668. (display-warning 'org message
  12669. (if (featurep 'xemacs)
  12670. 'warning
  12671. :warning))
  12672. (let ((buf (get-buffer-create "*Org warnings*")))
  12673. (with-current-buffer buf
  12674. (goto-char (point-max))
  12675. (insert "Warning (Org): " message)
  12676. (unless (bolp)
  12677. (newline)))
  12678. (display-buffer buf)
  12679. (sit-for 0))))
  12680. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12681. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12682. (if (and marker (marker-buffer marker)
  12683. (buffer-live-p (marker-buffer marker)))
  12684. (progn
  12685. (switch-to-buffer (marker-buffer marker))
  12686. (if (or (> marker (point-max)) (< marker (point-min)))
  12687. (widen))
  12688. (goto-char marker)
  12689. (org-show-context 'org-goto))
  12690. (if bookmark
  12691. (bookmark-jump bookmark)
  12692. (error "Cannot find location"))))
  12693. (defun org-quote-csv-field (s)
  12694. "Quote field for inclusion in CSV material."
  12695. (if (string-match "[\",]" s)
  12696. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12697. s))
  12698. (defun org-plist-delete (plist property)
  12699. "Delete PROPERTY from PLIST.
  12700. This is in contrast to merely setting it to 0."
  12701. (let (p)
  12702. (while plist
  12703. (if (not (eq property (car plist)))
  12704. (setq p (plist-put p (car plist) (nth 1 plist))))
  12705. (setq plist (cddr plist)))
  12706. p))
  12707. (defun org-force-self-insert (N)
  12708. "Needed to enforce self-insert under remapping."
  12709. (interactive "p")
  12710. (self-insert-command N))
  12711. (defun org-string-width (s)
  12712. "Compute width of string, ignoring invisible characters.
  12713. This ignores character with invisibility property `org-link', and also
  12714. characters with property `org-cwidth', because these will become invisible
  12715. upon the next fontification round."
  12716. (let (b l)
  12717. (when (or (eq t buffer-invisibility-spec)
  12718. (assq 'org-link buffer-invisibility-spec))
  12719. (while (setq b (text-property-any 0 (length s)
  12720. 'invisible 'org-link s))
  12721. (setq s (concat (substring s 0 b)
  12722. (substring s (or (next-single-property-change
  12723. b 'invisible s) (length s)))))))
  12724. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12725. (setq s (concat (substring s 0 b)
  12726. (substring s (or (next-single-property-change
  12727. b 'org-cwidth s) (length s))))))
  12728. (setq l (string-width s) b -1)
  12729. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12730. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12731. l))
  12732. (defun org-get-indentation (&optional line)
  12733. "Get the indentation of the current line, interpreting tabs.
  12734. When LINE is given, assume it represents a line and compute its indentation."
  12735. (if line
  12736. (if (string-match "^ *" (org-remove-tabs line))
  12737. (match-end 0))
  12738. (save-excursion
  12739. (beginning-of-line 1)
  12740. (skip-chars-forward " \t")
  12741. (current-column))))
  12742. (defun org-remove-tabs (s &optional width)
  12743. "Replace tabulators in S with spaces.
  12744. Assumes that s is a single line, starting in column 0."
  12745. (setq width (or width tab-width))
  12746. (while (string-match "\t" s)
  12747. (setq s (replace-match
  12748. (make-string
  12749. (- (* width (/ (+ (match-beginning 0) width) width))
  12750. (match-beginning 0)) ?\ )
  12751. t t s)))
  12752. s)
  12753. (defun org-fix-indentation (line ind)
  12754. "Fix indentation in LINE.
  12755. IND is a cons cell with target and minimum indentation.
  12756. If the current indentation in LINE is smaller than the minimum,
  12757. leave it alone. If it is larger than ind, set it to the target."
  12758. (let* ((l (org-remove-tabs line))
  12759. (i (org-get-indentation l))
  12760. (i1 (car ind)) (i2 (cdr ind)))
  12761. (if (>= i i2) (setq l (substring line i2)))
  12762. (if (> i1 0)
  12763. (concat (make-string i1 ?\ ) l)
  12764. l)))
  12765. (defun org-base-buffer (buffer)
  12766. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12767. (if (not buffer)
  12768. buffer
  12769. (or (buffer-base-buffer buffer)
  12770. buffer)))
  12771. (defun org-trim (s)
  12772. "Remove whitespace at beginning and end of string."
  12773. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12774. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12775. s)
  12776. (defun org-wrap (string &optional width lines)
  12777. "Wrap string to either a number of lines, or a width in characters.
  12778. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12779. that costs. If there is a word longer than WIDTH, the text is actually
  12780. wrapped to the length of that word.
  12781. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12782. many lines, whatever width that takes.
  12783. The return value is a list of lines, without newlines at the end."
  12784. (let* ((words (org-split-string string "[ \t\n]+"))
  12785. (maxword (apply 'max (mapcar 'org-string-width words)))
  12786. w ll)
  12787. (cond (width
  12788. (org-do-wrap words (max maxword width)))
  12789. (lines
  12790. (setq w maxword)
  12791. (setq ll (org-do-wrap words maxword))
  12792. (if (<= (length ll) lines)
  12793. ll
  12794. (setq ll words)
  12795. (while (> (length ll) lines)
  12796. (setq w (1+ w))
  12797. (setq ll (org-do-wrap words w)))
  12798. ll))
  12799. (t (error "Cannot wrap this")))))
  12800. (defun org-do-wrap (words width)
  12801. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12802. (let (lines line)
  12803. (while words
  12804. (setq line (pop words))
  12805. (while (and words (< (+ (length line) (length (car words))) width))
  12806. (setq line (concat line " " (pop words))))
  12807. (setq lines (push line lines)))
  12808. (nreverse lines)))
  12809. (defun org-split-string (string &optional separators)
  12810. "Splits STRING into substrings at SEPARATORS.
  12811. No empty strings are returned if there are matches at the beginning
  12812. and end of string."
  12813. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12814. (start 0)
  12815. notfirst
  12816. (list nil))
  12817. (while (and (string-match rexp string
  12818. (if (and notfirst
  12819. (= start (match-beginning 0))
  12820. (< start (length string)))
  12821. (1+ start) start))
  12822. (< (match-beginning 0) (length string)))
  12823. (setq notfirst t)
  12824. (or (eq (match-beginning 0) 0)
  12825. (and (eq (match-beginning 0) (match-end 0))
  12826. (eq (match-beginning 0) start))
  12827. (setq list
  12828. (cons (substring string start (match-beginning 0))
  12829. list)))
  12830. (setq start (match-end 0)))
  12831. (or (eq start (length string))
  12832. (setq list
  12833. (cons (substring string start)
  12834. list)))
  12835. (nreverse list)))
  12836. (defun org-context ()
  12837. "Return a list of contexts of the current cursor position.
  12838. If several contexts apply, all are returned.
  12839. Each context entry is a list with a symbol naming the context, and
  12840. two positions indicating start and end of the context. Possible
  12841. contexts are:
  12842. :headline anywhere in a headline
  12843. :headline-stars on the leading stars in a headline
  12844. :todo-keyword on a TODO keyword (including DONE) in a headline
  12845. :tags on the TAGS in a headline
  12846. :priority on the priority cookie in a headline
  12847. :item on the first line of a plain list item
  12848. :item-bullet on the bullet/number of a plain list item
  12849. :checkbox on the checkbox in a plain list item
  12850. :table in an org-mode table
  12851. :table-special on a special filed in a table
  12852. :table-table in a table.el table
  12853. :link on a hyperlink
  12854. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12855. :target on a <<target>>
  12856. :radio-target on a <<<radio-target>>>
  12857. :latex-fragment on a LaTeX fragment
  12858. :latex-preview on a LaTeX fragment with overlayed preview image
  12859. This function expects the position to be visible because it uses font-lock
  12860. faces as a help to recognize the following contexts: :table-special, :link,
  12861. and :keyword."
  12862. (let* ((f (get-text-property (point) 'face))
  12863. (faces (if (listp f) f (list f)))
  12864. (p (point)) clist o)
  12865. ;; First the large context
  12866. (cond
  12867. ((org-on-heading-p t)
  12868. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12869. (when (progn
  12870. (beginning-of-line 1)
  12871. (looking-at org-todo-line-tags-regexp))
  12872. (push (org-point-in-group p 1 :headline-stars) clist)
  12873. (push (org-point-in-group p 2 :todo-keyword) clist)
  12874. (push (org-point-in-group p 4 :tags) clist))
  12875. (goto-char p)
  12876. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12877. (if (looking-at "\\[#[A-Z0-9]\\]")
  12878. (push (org-point-in-group p 0 :priority) clist)))
  12879. ((org-at-item-p)
  12880. (push (org-point-in-group p 2 :item-bullet) clist)
  12881. (push (list :item (point-at-bol)
  12882. (save-excursion (org-end-of-item) (point)))
  12883. clist)
  12884. (and (org-at-item-checkbox-p)
  12885. (push (org-point-in-group p 0 :checkbox) clist)))
  12886. ((org-at-table-p)
  12887. (push (list :table (org-table-begin) (org-table-end)) clist)
  12888. (if (memq 'org-formula faces)
  12889. (push (list :table-special
  12890. (previous-single-property-change p 'face)
  12891. (next-single-property-change p 'face)) clist)))
  12892. ((org-at-table-p 'any)
  12893. (push (list :table-table) clist)))
  12894. (goto-char p)
  12895. ;; Now the small context
  12896. (cond
  12897. ((org-at-timestamp-p)
  12898. (push (org-point-in-group p 0 :timestamp) clist))
  12899. ((memq 'org-link faces)
  12900. (push (list :link
  12901. (previous-single-property-change p 'face)
  12902. (next-single-property-change p 'face)) clist))
  12903. ((memq 'org-special-keyword faces)
  12904. (push (list :keyword
  12905. (previous-single-property-change p 'face)
  12906. (next-single-property-change p 'face)) clist))
  12907. ((org-on-target-p)
  12908. (push (org-point-in-group p 0 :target) clist)
  12909. (goto-char (1- (match-beginning 0)))
  12910. (if (looking-at org-radio-target-regexp)
  12911. (push (org-point-in-group p 0 :radio-target) clist))
  12912. (goto-char p))
  12913. ((setq o (car (delq nil
  12914. (mapcar
  12915. (lambda (x)
  12916. (if (memq x org-latex-fragment-image-overlays) x))
  12917. (org-overlays-at (point))))))
  12918. (push (list :latex-fragment
  12919. (org-overlay-start o) (org-overlay-end o)) clist)
  12920. (push (list :latex-preview
  12921. (org-overlay-start o) (org-overlay-end o)) clist))
  12922. ((org-inside-LaTeX-fragment-p)
  12923. ;; FIXME: positions wrong.
  12924. (push (list :latex-fragment (point) (point)) clist)))
  12925. (setq clist (nreverse (delq nil clist)))
  12926. clist))
  12927. ;; FIXME: Compare with at-regexp-p Do we need both?
  12928. (defun org-in-regexp (re &optional nlines visually)
  12929. "Check if point is inside a match of regexp.
  12930. Normally only the current line is checked, but you can include NLINES extra
  12931. lines both before and after point into the search.
  12932. If VISUALLY is set, require that the cursor is not after the match but
  12933. really on, so that the block visually is on the match."
  12934. (catch 'exit
  12935. (let ((pos (point))
  12936. (eol (point-at-eol (+ 1 (or nlines 0))))
  12937. (inc (if visually 1 0)))
  12938. (save-excursion
  12939. (beginning-of-line (- 1 (or nlines 0)))
  12940. (while (re-search-forward re eol t)
  12941. (if (and (<= (match-beginning 0) pos)
  12942. (>= (+ inc (match-end 0)) pos))
  12943. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12944. (defun org-at-regexp-p (regexp)
  12945. "Is point inside a match of REGEXP in the current line?"
  12946. (catch 'exit
  12947. (save-excursion
  12948. (let ((pos (point)) (end (point-at-eol)))
  12949. (beginning-of-line 1)
  12950. (while (re-search-forward regexp end t)
  12951. (if (and (<= (match-beginning 0) pos)
  12952. (>= (match-end 0) pos))
  12953. (throw 'exit t)))
  12954. nil))))
  12955. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12956. "Call `multi-occur' with buffers for all agenda files."
  12957. (interactive "sOrg-files matching: \np")
  12958. (let* ((files (org-agenda-files))
  12959. (tnames (mapcar 'file-truename files))
  12960. (extra org-agenda-text-search-extra-files)
  12961. f)
  12962. (when (eq (car extra) 'agenda-archives)
  12963. (setq extra (cdr extra))
  12964. (setq files (org-add-archive-files files)))
  12965. (while (setq f (pop extra))
  12966. (unless (member (file-truename f) tnames)
  12967. (add-to-list 'files f 'append)
  12968. (add-to-list 'tnames (file-truename f) 'append)))
  12969. (multi-occur
  12970. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12971. regexp)))
  12972. (if (boundp 'occur-mode-find-occurrence-hook)
  12973. ;; Emacs 23
  12974. (add-hook 'occur-mode-find-occurrence-hook
  12975. (lambda ()
  12976. (when (org-mode-p)
  12977. (org-reveal))))
  12978. ;; Emacs 22
  12979. (defadvice occur-mode-goto-occurrence
  12980. (after org-occur-reveal activate)
  12981. (and (org-mode-p) (org-reveal)))
  12982. (defadvice occur-mode-goto-occurrence-other-window
  12983. (after org-occur-reveal activate)
  12984. (and (org-mode-p) (org-reveal)))
  12985. (defadvice occur-mode-display-occurrence
  12986. (after org-occur-reveal activate)
  12987. (when (org-mode-p)
  12988. (let ((pos (occur-mode-find-occurrence)))
  12989. (with-current-buffer (marker-buffer pos)
  12990. (save-excursion
  12991. (goto-char pos)
  12992. (org-reveal)))))))
  12993. (defun org-uniquify (list)
  12994. "Remove duplicate elements from LIST."
  12995. (let (res)
  12996. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12997. res))
  12998. (defun org-delete-all (elts list)
  12999. "Remove all elements in ELTS from LIST."
  13000. (while elts
  13001. (setq list (delete (pop elts) list)))
  13002. list)
  13003. (defun org-back-over-empty-lines ()
  13004. "Move backwards over whitespace, to the beginning of the first empty line.
  13005. Returns the number of empty lines passed."
  13006. (let ((pos (point)))
  13007. (skip-chars-backward " \t\n\r")
  13008. (beginning-of-line 2)
  13009. (goto-char (min (point) pos))
  13010. (count-lines (point) pos)))
  13011. (defun org-skip-whitespace ()
  13012. (skip-chars-forward " \t\n\r"))
  13013. (defun org-point-in-group (point group &optional context)
  13014. "Check if POINT is in match-group GROUP.
  13015. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13016. match. If the match group does ot exist or point is not inside it,
  13017. return nil."
  13018. (and (match-beginning group)
  13019. (>= point (match-beginning group))
  13020. (<= point (match-end group))
  13021. (if context
  13022. (list context (match-beginning group) (match-end group))
  13023. t)))
  13024. (defun org-switch-to-buffer-other-window (&rest args)
  13025. "Switch to buffer in a second window on the current frame.
  13026. In particular, do not allow pop-up frames."
  13027. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13028. special-display-function)
  13029. (apply 'switch-to-buffer-other-window args)))
  13030. (defun org-combine-plists (&rest plists)
  13031. "Create a single property list from all plists in PLISTS.
  13032. The process starts by copying the first list, and then setting properties
  13033. from the other lists. Settings in the last list are the most significant
  13034. ones and overrule settings in the other lists."
  13035. (let ((rtn (copy-sequence (pop plists)))
  13036. p v ls)
  13037. (while plists
  13038. (setq ls (pop plists))
  13039. (while ls
  13040. (setq p (pop ls) v (pop ls))
  13041. (setq rtn (plist-put rtn p v))))
  13042. rtn))
  13043. (defun org-move-line-down (arg)
  13044. "Move the current line down. With prefix argument, move it past ARG lines."
  13045. (interactive "p")
  13046. (let ((col (current-column))
  13047. beg end pos)
  13048. (beginning-of-line 1) (setq beg (point))
  13049. (beginning-of-line 2) (setq end (point))
  13050. (beginning-of-line (+ 1 arg))
  13051. (setq pos (move-marker (make-marker) (point)))
  13052. (insert (delete-and-extract-region beg end))
  13053. (goto-char pos)
  13054. (org-move-to-column col)))
  13055. (defun org-move-line-up (arg)
  13056. "Move the current line up. With prefix argument, move it past ARG lines."
  13057. (interactive "p")
  13058. (let ((col (current-column))
  13059. beg end pos)
  13060. (beginning-of-line 1) (setq beg (point))
  13061. (beginning-of-line 2) (setq end (point))
  13062. (beginning-of-line (- arg))
  13063. (setq pos (move-marker (make-marker) (point)))
  13064. (insert (delete-and-extract-region beg end))
  13065. (goto-char pos)
  13066. (org-move-to-column col)))
  13067. (defun org-replace-escapes (string table)
  13068. "Replace %-escapes in STRING with values in TABLE.
  13069. TABLE is an association list with keys like \"%a\" and string values.
  13070. The sequences in STRING may contain normal field width and padding information,
  13071. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13072. so values can contain further %-escapes if they are define later in TABLE."
  13073. (let ((case-fold-search nil)
  13074. e re rpl)
  13075. (while (setq e (pop table))
  13076. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13077. (while (string-match re string)
  13078. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13079. (cdr e)))
  13080. (setq string (replace-match rpl t t string))))
  13081. string))
  13082. (defun org-sublist (list start end)
  13083. "Return a section of LIST, from START to END.
  13084. Counting starts at 1."
  13085. (let (rtn (c start))
  13086. (setq list (nthcdr (1- start) list))
  13087. (while (and list (<= c end))
  13088. (push (pop list) rtn)
  13089. (setq c (1+ c)))
  13090. (nreverse rtn)))
  13091. (defun org-find-base-buffer-visiting (file)
  13092. "Like `find-buffer-visiting' but alway return the base buffer and
  13093. not an indirect buffer."
  13094. (let ((buf (find-buffer-visiting file)))
  13095. (if buf
  13096. (or (buffer-base-buffer buf) buf)
  13097. nil)))
  13098. (defun org-image-file-name-regexp (&optional extensions)
  13099. "Return regexp matching the file names of images.
  13100. If EXTENSIONS is given, only match these."
  13101. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13102. (image-file-name-regexp)
  13103. (let ((image-file-name-extensions
  13104. (or extensions
  13105. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13106. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13107. (concat "\\."
  13108. (regexp-opt (nconc (mapcar 'upcase
  13109. image-file-name-extensions)
  13110. image-file-name-extensions)
  13111. t)
  13112. "\\'"))))
  13113. (defun org-file-image-p (file &optional extensions)
  13114. "Return non-nil if FILE is an image."
  13115. (save-match-data
  13116. (string-match (org-image-file-name-regexp extensions) file)))
  13117. (defun org-get-cursor-date ()
  13118. "Return the date at cursor in as a time.
  13119. This works in the calendar and in the agenda, anywhere else it just
  13120. returns the current time."
  13121. (let (date day defd)
  13122. (cond
  13123. ((eq major-mode 'calendar-mode)
  13124. (setq date (calendar-cursor-to-date)
  13125. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13126. ((eq major-mode 'org-agenda-mode)
  13127. (setq day (get-text-property (point) 'day))
  13128. (if day
  13129. (setq date (calendar-gregorian-from-absolute day)
  13130. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13131. (nth 2 date))))))
  13132. (or defd (current-time))))
  13133. (defvar org-agenda-action-marker (make-marker)
  13134. "Marker pointing to the entry for the next agenda action.")
  13135. (defun org-mark-entry-for-agenda-action ()
  13136. "Mark the current entry as target of an agenda action.
  13137. Agenda actions are actions executed from the agenda with the key `k',
  13138. which make use of the date at the cursor."
  13139. (interactive)
  13140. (move-marker org-agenda-action-marker
  13141. (save-excursion (org-back-to-heading t) (point))
  13142. (current-buffer))
  13143. (message
  13144. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13145. ;;; Paragraph filling stuff.
  13146. ;; We want this to be just right, so use the full arsenal.
  13147. (defun org-indent-line-function ()
  13148. "Indent line like previous, but further if previous was headline or item."
  13149. (interactive)
  13150. (let* ((pos (point))
  13151. (itemp (org-at-item-p))
  13152. column bpos bcol tpos tcol bullet btype bullet-type)
  13153. ;; Find the previous relevant line
  13154. (beginning-of-line 1)
  13155. (cond
  13156. ((looking-at "#") (setq column 0))
  13157. ((looking-at "\\*+ ") (setq column 0))
  13158. (t
  13159. (beginning-of-line 0)
  13160. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13161. (beginning-of-line 0))
  13162. (cond
  13163. ((looking-at "\\*+[ \t]+")
  13164. (if (not org-adapt-indentation)
  13165. (setq column 0)
  13166. (goto-char (match-end 0))
  13167. (setq column (current-column))))
  13168. ((org-in-item-p)
  13169. (org-beginning-of-item)
  13170. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13171. (setq bpos (match-beginning 1) tpos (match-end 0)
  13172. bcol (progn (goto-char bpos) (current-column))
  13173. tcol (progn (goto-char tpos) (current-column))
  13174. bullet (match-string 1)
  13175. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13176. (if (> tcol (+ bcol org-description-max-indent))
  13177. (setq tcol (+ bcol 5)))
  13178. (if (not itemp)
  13179. (setq column tcol)
  13180. (goto-char pos)
  13181. (beginning-of-line 1)
  13182. (if (looking-at "\\S-")
  13183. (progn
  13184. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13185. (setq bullet (match-string 1)
  13186. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13187. (setq column (if (equal btype bullet-type) bcol tcol)))
  13188. (setq column (org-get-indentation)))))
  13189. (t (setq column (org-get-indentation))))))
  13190. (goto-char pos)
  13191. (if (<= (current-column) (current-indentation))
  13192. (org-indent-line-to column)
  13193. (save-excursion (org-indent-line-to column)))
  13194. (setq column (current-column))
  13195. (beginning-of-line 1)
  13196. (if (looking-at
  13197. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13198. (replace-match (concat "\\1" (format org-property-format
  13199. (match-string 2) (match-string 3)))
  13200. t nil))
  13201. (org-move-to-column column)))
  13202. (defun org-set-autofill-regexps ()
  13203. (interactive)
  13204. ;; In the paragraph separator we include headlines, because filling
  13205. ;; text in a line directly attached to a headline would otherwise
  13206. ;; fill the headline as well.
  13207. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13208. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13209. ;; The paragraph starter includes hand-formatted lists.
  13210. (org-set-local 'paragraph-start
  13211. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13212. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13213. ;; But only if the user has not turned off tables or fixed-width regions
  13214. (org-set-local
  13215. 'auto-fill-inhibit-regexp
  13216. (concat "\\*+ \\|#\\+"
  13217. "\\|[ \t]*" org-keyword-time-regexp
  13218. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13219. (concat
  13220. "\\|[ \t]*["
  13221. (if org-enable-table-editor "|" "")
  13222. (if org-enable-fixed-width-editor ":" "")
  13223. "]"))))
  13224. ;; We use our own fill-paragraph function, to make sure that tables
  13225. ;; and fixed-width regions are not wrapped. That function will pass
  13226. ;; through to `fill-paragraph' when appropriate.
  13227. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13228. ; Adaptive filling: To get full control, first make sure that
  13229. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13230. (org-set-local 'adaptive-fill-regexp "\000")
  13231. (org-set-local 'adaptive-fill-function
  13232. 'org-adaptive-fill-function)
  13233. (org-set-local
  13234. 'align-mode-rules-list
  13235. '((org-in-buffer-settings
  13236. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13237. (modes . '(org-mode))))))
  13238. (defun org-fill-paragraph (&optional justify)
  13239. "Re-align a table, pass through to fill-paragraph if no table."
  13240. (let ((table-p (org-at-table-p))
  13241. (table.el-p (org-at-table.el-p)))
  13242. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13243. (save-excursion (goto-char (point-at-bol))
  13244. (looking-at outline-regexp)))
  13245. t) ; skip headlines
  13246. (table.el-p t) ; skip table.el tables
  13247. (table-p (org-table-align) t) ; align org-mode tables
  13248. (t nil)))) ; call paragraph-fill
  13249. ;; For reference, this is the default value of adaptive-fill-regexp
  13250. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13251. (defun org-adaptive-fill-function ()
  13252. "Return a fill prefix for org-mode files.
  13253. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13254. work correctly."
  13255. (cond ((looking-at "#[ \t]+")
  13256. (match-string 0))
  13257. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13258. (save-excursion
  13259. (if (> (match-end 1) (+ (match-beginning 1)
  13260. org-description-max-indent))
  13261. (goto-char (+ (match-beginning 1) 5))
  13262. (goto-char (match-end 0)))
  13263. (make-string (current-column) ?\ )))
  13264. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13265. (save-excursion
  13266. (goto-char (match-end 0))
  13267. (make-string (current-column) ?\ )))
  13268. (t nil)))
  13269. ;;; Other stuff.
  13270. (defun org-toggle-fixed-width-section (arg)
  13271. "Toggle the fixed-width export.
  13272. If there is no active region, the QUOTE keyword at the current headline is
  13273. inserted or removed. When present, it causes the text between this headline
  13274. and the next to be exported as fixed-width text, and unmodified.
  13275. If there is an active region, this command adds or removes a colon as the
  13276. first character of this line. If the first character of a line is a colon,
  13277. this line is also exported in fixed-width font."
  13278. (interactive "P")
  13279. (let* ((cc 0)
  13280. (regionp (org-region-active-p))
  13281. (beg (if regionp (region-beginning) (point)))
  13282. (end (if regionp (region-end)))
  13283. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13284. (case-fold-search nil)
  13285. (re "[ \t]*\\(:\\)")
  13286. off)
  13287. (if regionp
  13288. (save-excursion
  13289. (goto-char beg)
  13290. (setq cc (current-column))
  13291. (beginning-of-line 1)
  13292. (setq off (looking-at re))
  13293. (while (> nlines 0)
  13294. (setq nlines (1- nlines))
  13295. (beginning-of-line 1)
  13296. (cond
  13297. (arg
  13298. (org-move-to-column cc t)
  13299. (insert ":\n")
  13300. (forward-line -1))
  13301. ((and off (looking-at re))
  13302. (replace-match "" t t nil 1))
  13303. ((not off) (org-move-to-column cc t) (insert ":")))
  13304. (forward-line 1)))
  13305. (save-excursion
  13306. (org-back-to-heading)
  13307. (if (looking-at (concat outline-regexp
  13308. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13309. (replace-match "" t t nil 1)
  13310. (if (looking-at outline-regexp)
  13311. (progn
  13312. (goto-char (match-end 0))
  13313. (insert org-quote-string " "))))))))
  13314. ;;;; Functions extending outline functionality
  13315. (defun org-beginning-of-line (&optional arg)
  13316. "Go to the beginning of the current line. If that is invisible, continue
  13317. to a visible line beginning. This makes the function of C-a more intuitive.
  13318. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13319. first attempt, and only move to after the tags when the cursor is already
  13320. beyond the end of the headline."
  13321. (interactive "P")
  13322. (let ((pos (point)) refpos)
  13323. (beginning-of-line 1)
  13324. (if (bobp)
  13325. nil
  13326. (backward-char 1)
  13327. (if (org-invisible-p)
  13328. (while (and (not (bobp)) (org-invisible-p))
  13329. (backward-char 1)
  13330. (beginning-of-line 1))
  13331. (forward-char 1)))
  13332. (when org-special-ctrl-a/e
  13333. (cond
  13334. ((and (looking-at org-complex-heading-regexp)
  13335. (= (char-after (match-end 1)) ?\ ))
  13336. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13337. (point-at-eol)))
  13338. (goto-char
  13339. (if (eq org-special-ctrl-a/e t)
  13340. (cond ((> pos refpos) refpos)
  13341. ((= pos (point)) refpos)
  13342. (t (point)))
  13343. (cond ((> pos (point)) (point))
  13344. ((not (eq last-command this-command)) (point))
  13345. (t refpos)))))
  13346. ((org-at-item-p)
  13347. (goto-char
  13348. (if (eq org-special-ctrl-a/e t)
  13349. (cond ((> pos (match-end 4)) (match-end 4))
  13350. ((= pos (point)) (match-end 4))
  13351. (t (point)))
  13352. (cond ((> pos (point)) (point))
  13353. ((not (eq last-command this-command)) (point))
  13354. (t (match-end 4))))))))
  13355. (org-no-warnings
  13356. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13357. (defun org-end-of-line (&optional arg)
  13358. "Go to the end of the line.
  13359. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13360. first attempt, and only move to after the tags when the cursor is already
  13361. beyond the end of the headline."
  13362. (interactive "P")
  13363. (if (or (not org-special-ctrl-a/e)
  13364. (not (org-on-heading-p)))
  13365. (end-of-line arg)
  13366. (let ((pos (point)))
  13367. (beginning-of-line 1)
  13368. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13369. (if (eq org-special-ctrl-a/e t)
  13370. (if (or (< pos (match-beginning 1))
  13371. (= pos (match-end 0)))
  13372. (goto-char (match-beginning 1))
  13373. (goto-char (match-end 0)))
  13374. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13375. (goto-char (match-end 0))
  13376. (goto-char (match-beginning 1))))
  13377. (end-of-line arg))))
  13378. (org-no-warnings
  13379. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13380. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13381. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13382. (defun org-kill-line (&optional arg)
  13383. "Kill line, to tags or end of line."
  13384. (interactive "P")
  13385. (cond
  13386. ((or (not org-special-ctrl-k)
  13387. (bolp)
  13388. (not (org-on-heading-p)))
  13389. (call-interactively 'kill-line))
  13390. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13391. (kill-region (point) (match-beginning 1))
  13392. (org-set-tags nil t))
  13393. (t (kill-region (point) (point-at-eol)))))
  13394. (define-key org-mode-map "\C-k" 'org-kill-line)
  13395. (defun org-yank (&optional arg)
  13396. "Yank. If the kill is a subtree, treat it specially.
  13397. This command will look at the current kill and check if is a single
  13398. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13399. cursor is at the beginning of a line or after the stars of a currently
  13400. empty headline, then the yank is handled specially. How exactly depends
  13401. on the value of the following variables, both set by default.
  13402. org-yank-folded-subtrees
  13403. When set, the subtree(s) will be folded after insertion, but only
  13404. if doing so would now swallow text after the yanked text.
  13405. org-yank-adjusted-subtrees
  13406. When set, the subtree will be promoted or demoted in order to
  13407. fit into the local outline tree structure, which means that the level
  13408. will be adjusted so that it becomes the smaller one of the two
  13409. *visible* surrounding headings.
  13410. Any prefix to this command will cause `yank' to be called directly with
  13411. no special treatment. In particular, a simple `C-u' prefix will just
  13412. plainly yank the text as it is.
  13413. \[1] Basically, the test checks if the first non-white line is a heading
  13414. and if there are no other headings with fewer stars."
  13415. (interactive "P")
  13416. (setq this-command 'yank)
  13417. (if arg
  13418. (call-interactively 'yank)
  13419. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13420. (and (org-kill-is-subtree-p)
  13421. (or (bolp)
  13422. (and (looking-at "[ \t]*$")
  13423. (string-match
  13424. "\\`\\*+\\'"
  13425. (buffer-substring (point-at-bol) (point)))))))
  13426. swallowp)
  13427. (cond
  13428. ((and subtreep org-yank-folded-subtrees)
  13429. (let ((beg (point))
  13430. end)
  13431. (if (and subtreep org-yank-adjusted-subtrees)
  13432. (org-paste-subtree nil nil 'for-yank)
  13433. (call-interactively 'yank))
  13434. (setq end (point))
  13435. (goto-char beg)
  13436. (when (and (bolp) subtreep
  13437. (not (setq swallowp
  13438. (org-yank-folding-would-swallow-text beg end))))
  13439. (or (looking-at outline-regexp)
  13440. (re-search-forward (concat "^" outline-regexp) end t))
  13441. (while (and (< (point) end) (looking-at outline-regexp))
  13442. (hide-subtree)
  13443. (org-cycle-show-empty-lines 'folded)
  13444. (condition-case nil
  13445. (outline-forward-same-level 1)
  13446. (error (goto-char end)))))
  13447. (when swallowp
  13448. (message
  13449. "Yanked text not folded because that would swallow text"))
  13450. (goto-char end)
  13451. (skip-chars-forward " \t\n\r")
  13452. (beginning-of-line 1)
  13453. (push-mark beg 'nomsg)))
  13454. ((and subtreep org-yank-adjusted-subtrees)
  13455. (let ((beg (point-at-bol)))
  13456. (org-paste-subtree nil nil 'for-yank)
  13457. (push-mark beg 'nomsg)))
  13458. (t
  13459. (call-interactively 'yank))))))
  13460. (defun org-yank-folding-would-swallow-text (beg end)
  13461. "Would hide-subtree at BEG swallow any text after END?"
  13462. (let (level)
  13463. (save-excursion
  13464. (goto-char beg)
  13465. (when (or (looking-at outline-regexp)
  13466. (re-search-forward (concat "^" outline-regexp) end t))
  13467. (setq level (org-outline-level)))
  13468. (goto-char end)
  13469. (skip-chars-forward " \t\r\n\v\f")
  13470. (if (or (eobp)
  13471. (and (bolp) (looking-at org-outline-regexp)
  13472. (<= (org-outline-level) level)))
  13473. nil ; Nothing would be swallowed
  13474. t)))) ; something would swallow
  13475. (define-key org-mode-map "\C-y" 'org-yank)
  13476. (defun org-invisible-p ()
  13477. "Check if point is at a character currently not visible."
  13478. ;; Early versions of noutline don't have `outline-invisible-p'.
  13479. (if (fboundp 'outline-invisible-p)
  13480. (outline-invisible-p)
  13481. (get-char-property (point) 'invisible)))
  13482. (defun org-invisible-p2 ()
  13483. "Check if point is at a character currently not visible."
  13484. (save-excursion
  13485. (if (and (eolp) (not (bobp))) (backward-char 1))
  13486. ;; Early versions of noutline don't have `outline-invisible-p'.
  13487. (if (fboundp 'outline-invisible-p)
  13488. (outline-invisible-p)
  13489. (get-char-property (point) 'invisible))))
  13490. (defun org-back-to-heading (&optional invisible-ok)
  13491. "Call `outline-back-to-heading', but provide a better error message."
  13492. (condition-case nil
  13493. (outline-back-to-heading invisible-ok)
  13494. (error (error "Before first headline at position %d in buffer %s"
  13495. (point) (current-buffer)))))
  13496. (defun org-before-first-heading-p ()
  13497. "Before first heading?"
  13498. (save-excursion
  13499. (null (re-search-backward "^\\*+ " nil t))))
  13500. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13501. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13502. (defun org-at-heading-or-item-p ()
  13503. (or (org-on-heading-p) (org-at-item-p)))
  13504. (defun org-on-target-p ()
  13505. (or (org-in-regexp org-radio-target-regexp)
  13506. (org-in-regexp org-target-regexp)))
  13507. (defun org-up-heading-all (arg)
  13508. "Move to the heading line of which the present line is a subheading.
  13509. This function considers both visible and invisible heading lines.
  13510. With argument, move up ARG levels."
  13511. (if (fboundp 'outline-up-heading-all)
  13512. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13513. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13514. (defun org-up-heading-safe ()
  13515. "Move to the heading line of which the present line is a subheading.
  13516. This version will not throw an error. It will return the level of the
  13517. headline found, or nil if no higher level is found."
  13518. (let (start-level re)
  13519. (org-back-to-heading t)
  13520. (setq start-level (funcall outline-level))
  13521. (if (equal start-level 1)
  13522. nil
  13523. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13524. (if (re-search-backward re nil t)
  13525. (funcall outline-level)))))
  13526. (defun org-first-sibling-p ()
  13527. "Is this heading the first child of its parents?"
  13528. (interactive)
  13529. (let ((re (concat "^" outline-regexp))
  13530. level l)
  13531. (unless (org-at-heading-p t)
  13532. (error "Not at a heading"))
  13533. (setq level (funcall outline-level))
  13534. (save-excursion
  13535. (if (not (re-search-backward re nil t))
  13536. t
  13537. (setq l (funcall outline-level))
  13538. (< l level)))))
  13539. (defun org-goto-sibling (&optional previous)
  13540. "Goto the next sibling, even if it is invisible.
  13541. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13542. when a sibling was found. When none is found, return nil and don't
  13543. move point."
  13544. (let ((fun (if previous 're-search-backward 're-search-forward))
  13545. (pos (point))
  13546. (re (concat "^" outline-regexp))
  13547. level l)
  13548. (when (condition-case nil (org-back-to-heading t) (error nil))
  13549. (setq level (funcall outline-level))
  13550. (catch 'exit
  13551. (or previous (forward-char 1))
  13552. (while (funcall fun re nil t)
  13553. (setq l (funcall outline-level))
  13554. (when (< l level) (goto-char pos) (throw 'exit nil))
  13555. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13556. (goto-char pos)
  13557. nil))))
  13558. (defun org-show-siblings ()
  13559. "Show all siblings of the current headline."
  13560. (save-excursion
  13561. (while (org-goto-sibling) (org-flag-heading nil)))
  13562. (save-excursion
  13563. (while (org-goto-sibling 'previous)
  13564. (org-flag-heading nil))))
  13565. (defun org-show-hidden-entry ()
  13566. "Show an entry where even the heading is hidden."
  13567. (save-excursion
  13568. (org-show-entry)))
  13569. (defun org-flag-heading (flag &optional entry)
  13570. "Flag the current heading. FLAG non-nil means make invisible.
  13571. When ENTRY is non-nil, show the entire entry."
  13572. (save-excursion
  13573. (org-back-to-heading t)
  13574. ;; Check if we should show the entire entry
  13575. (if entry
  13576. (progn
  13577. (org-show-entry)
  13578. (save-excursion
  13579. (and (outline-next-heading)
  13580. (org-flag-heading nil))))
  13581. (outline-flag-region (max (point-min) (1- (point)))
  13582. (save-excursion (outline-end-of-heading) (point))
  13583. flag))))
  13584. (defun org-forward-same-level (arg)
  13585. "Move forward to the ARG'th subheading at same level as this one.
  13586. Stop at the first and last subheadings of a superior heading.
  13587. This is like outline-forward-same-level, but invisible headings are ok."
  13588. (interactive "p")
  13589. (org-back-to-heading t)
  13590. (while (> arg 0)
  13591. (let ((point-to-move-to (save-excursion
  13592. (org-get-next-sibling))))
  13593. (if point-to-move-to
  13594. (progn
  13595. (goto-char point-to-move-to)
  13596. (setq arg (1- arg)))
  13597. (progn
  13598. (setq arg 0)
  13599. (error "No following same-level heading"))))))
  13600. (defun org-get-next-sibling ()
  13601. "Move to next heading of the same level, and return point.
  13602. If there is no such heading, return nil.
  13603. This is like outline-next-sibling, but invisible headings are ok."
  13604. (let ((level (funcall outline-level)))
  13605. (outline-next-heading)
  13606. (while (and (not (eobp)) (> (funcall outline-level) level))
  13607. (outline-next-heading))
  13608. (if (or (eobp) (< (funcall outline-level) level))
  13609. nil
  13610. (point))))
  13611. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13612. ;; This is an exact copy of the original function, but it uses
  13613. ;; `org-back-to-heading', to make it work also in invisible
  13614. ;; trees. And is uses an invisible-OK argument.
  13615. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13616. (org-back-to-heading invisible-OK)
  13617. (let ((first t)
  13618. (level (funcall outline-level)))
  13619. (while (and (not (eobp))
  13620. (or first (> (funcall outline-level) level)))
  13621. (setq first nil)
  13622. (outline-next-heading))
  13623. (unless to-heading
  13624. (if (memq (preceding-char) '(?\n ?\^M))
  13625. (progn
  13626. ;; Go to end of line before heading
  13627. (forward-char -1)
  13628. (if (memq (preceding-char) '(?\n ?\^M))
  13629. ;; leave blank line before heading
  13630. (forward-char -1))))))
  13631. (point))
  13632. (defun org-show-subtree ()
  13633. "Show everything after this heading at deeper levels."
  13634. (outline-flag-region
  13635. (point)
  13636. (save-excursion
  13637. (outline-end-of-subtree) (outline-next-heading) (point))
  13638. nil))
  13639. (defun org-show-entry ()
  13640. "Show the body directly following this heading.
  13641. Show the heading too, if it is currently invisible."
  13642. (interactive)
  13643. (save-excursion
  13644. (condition-case nil
  13645. (progn
  13646. (org-back-to-heading t)
  13647. (outline-flag-region
  13648. (max (point-min) (1- (point)))
  13649. (save-excursion
  13650. (re-search-forward
  13651. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13652. (or (match-beginning 1) (point-max)))
  13653. nil))
  13654. (error nil))))
  13655. (defun org-make-options-regexp (kwds)
  13656. "Make a regular expression for keyword lines."
  13657. (concat
  13658. "^"
  13659. "#?[ \t]*\\+\\("
  13660. (mapconcat 'regexp-quote kwds "\\|")
  13661. "\\):[ \t]*"
  13662. "\\(.+\\)"))
  13663. ;; Make isearch reveal the necessary context
  13664. (defun org-isearch-end ()
  13665. "Reveal context after isearch exits."
  13666. (when isearch-success ; only if search was successful
  13667. (if (featurep 'xemacs)
  13668. ;; Under XEmacs, the hook is run in the correct place,
  13669. ;; we directly show the context.
  13670. (org-show-context 'isearch)
  13671. ;; In Emacs the hook runs *before* restoring the overlays.
  13672. ;; So we have to use a one-time post-command-hook to do this.
  13673. ;; (Emacs 22 has a special variable, see function `org-mode')
  13674. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13675. isearch-mode-end-hook-quit)
  13676. ;; Only when the isearch was not quitted.
  13677. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13678. 'append 'local)))))
  13679. (defun org-isearch-post-command ()
  13680. "Remove self from hook, and show context."
  13681. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13682. (org-show-context 'isearch))
  13683. ;;;; Integration with and fixes for other packages
  13684. ;;; Imenu support
  13685. (defvar org-imenu-markers nil
  13686. "All markers currently used by Imenu.")
  13687. (make-variable-buffer-local 'org-imenu-markers)
  13688. (defun org-imenu-new-marker (&optional pos)
  13689. "Return a new marker for use by Imenu, and remember the marker."
  13690. (let ((m (make-marker)))
  13691. (move-marker m (or pos (point)))
  13692. (push m org-imenu-markers)
  13693. m))
  13694. (defun org-imenu-get-tree ()
  13695. "Produce the index for Imenu."
  13696. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13697. (setq org-imenu-markers nil)
  13698. (let* ((n org-imenu-depth)
  13699. (re (concat "^" outline-regexp))
  13700. (subs (make-vector (1+ n) nil))
  13701. (last-level 0)
  13702. m level head)
  13703. (save-excursion
  13704. (save-restriction
  13705. (widen)
  13706. (goto-char (point-max))
  13707. (while (re-search-backward re nil t)
  13708. (setq level (org-reduced-level (funcall outline-level)))
  13709. (when (<= level n)
  13710. (looking-at org-complex-heading-regexp)
  13711. (setq head (org-link-display-format
  13712. (org-match-string-no-properties 4))
  13713. m (org-imenu-new-marker))
  13714. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13715. (if (>= level last-level)
  13716. (push (cons head m) (aref subs level))
  13717. (push (cons head (aref subs (1+ level))) (aref subs level))
  13718. (loop for i from (1+ level) to n do (aset subs i nil)))
  13719. (setq last-level level)))))
  13720. (aref subs 1)))
  13721. (eval-after-load "imenu"
  13722. '(progn
  13723. (add-hook 'imenu-after-jump-hook
  13724. (lambda ()
  13725. (if (eq major-mode 'org-mode)
  13726. (org-show-context 'org-goto))))))
  13727. (defun org-link-display-format (link)
  13728. "Replace a link with either the description, or the link target
  13729. if no description is present"
  13730. (save-match-data
  13731. (if (string-match org-bracket-link-analytic-regexp link)
  13732. (replace-match (or (match-string 5 link)
  13733. (concat (match-string 1 link)
  13734. (match-string 3 link)))
  13735. nil nil link)
  13736. link)))
  13737. ;; Speedbar support
  13738. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13739. "Overlay marking the agenda restriction line in speedbar.")
  13740. (org-overlay-put org-speedbar-restriction-lock-overlay
  13741. 'face 'org-agenda-restriction-lock)
  13742. (org-overlay-put org-speedbar-restriction-lock-overlay
  13743. 'help-echo "Agendas are currently limited to this item.")
  13744. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13745. (defun org-speedbar-set-agenda-restriction ()
  13746. "Restrict future agenda commands to the location at point in speedbar.
  13747. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13748. (interactive)
  13749. (require 'org-agenda)
  13750. (let (p m tp np dir txt)
  13751. (cond
  13752. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13753. 'org-imenu t))
  13754. (setq m (get-text-property p 'org-imenu-marker))
  13755. (save-excursion
  13756. (save-restriction
  13757. (set-buffer (marker-buffer m))
  13758. (goto-char m)
  13759. (org-agenda-set-restriction-lock 'subtree))))
  13760. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13761. 'speedbar-function 'speedbar-find-file))
  13762. (setq tp (previous-single-property-change
  13763. (1+ p) 'speedbar-function)
  13764. np (next-single-property-change
  13765. tp 'speedbar-function)
  13766. dir (speedbar-line-directory)
  13767. txt (buffer-substring-no-properties (or tp (point-min))
  13768. (or np (point-max))))
  13769. (save-excursion
  13770. (save-restriction
  13771. (set-buffer (find-file-noselect
  13772. (let ((default-directory dir))
  13773. (expand-file-name txt))))
  13774. (unless (org-mode-p)
  13775. (error "Cannot restrict to non-Org-mode file"))
  13776. (org-agenda-set-restriction-lock 'file))))
  13777. (t (error "Don't know how to restrict Org-mode's agenda")))
  13778. (org-move-overlay org-speedbar-restriction-lock-overlay
  13779. (point-at-bol) (point-at-eol))
  13780. (setq current-prefix-arg nil)
  13781. (org-agenda-maybe-redo)))
  13782. (eval-after-load "speedbar"
  13783. '(progn
  13784. (speedbar-add-supported-extension ".org")
  13785. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13786. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13787. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13788. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13789. (add-hook 'speedbar-visiting-tag-hook
  13790. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13791. ;;; Fixes and Hacks for problems with other packages
  13792. ;; Make flyspell not check words in links, to not mess up our keymap
  13793. (defun org-mode-flyspell-verify ()
  13794. "Don't let flyspell put overlays at active buttons."
  13795. (not (get-text-property (point) 'keymap)))
  13796. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13797. (eval-after-load "bookmark"
  13798. '(if (boundp 'bookmark-after-jump-hook)
  13799. ;; We can use the hook
  13800. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13801. ;; Hook not available, use advice
  13802. (defadvice bookmark-jump (after org-make-visible activate)
  13803. "Make the position visible."
  13804. (org-bookmark-jump-unhide))))
  13805. ;; Make sure saveplace show the location if it was hidden
  13806. (eval-after-load "saveplace"
  13807. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13808. "Make the position visible."
  13809. (org-bookmark-jump-unhide)))
  13810. (defun org-bookmark-jump-unhide ()
  13811. "Unhide the current position, to show the bookmark location."
  13812. (and (org-mode-p)
  13813. (or (org-invisible-p)
  13814. (save-excursion (goto-char (max (point-min) (1- (point))))
  13815. (org-invisible-p)))
  13816. (org-show-context 'bookmark-jump)))
  13817. ;; Make session.el ignore our circular variable
  13818. (eval-after-load "session"
  13819. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13820. ;;;; Experimental code
  13821. (defun org-closed-in-range ()
  13822. "Sparse tree of items closed in a certain time range.
  13823. Still experimental, may disappear in the future."
  13824. (interactive)
  13825. ;; Get the time interval from the user.
  13826. (let* ((time1 (time-to-seconds
  13827. (org-read-date nil 'to-time nil "Starting date: ")))
  13828. (time2 (time-to-seconds
  13829. (org-read-date nil 'to-time nil "End date:")))
  13830. ;; callback function
  13831. (callback (lambda ()
  13832. (let ((time
  13833. (time-to-seconds
  13834. (apply 'encode-time
  13835. (org-parse-time-string
  13836. (match-string 1))))))
  13837. ;; check if time in interval
  13838. (and (>= time time1) (<= time time2))))))
  13839. ;; make tree, check each match with the callback
  13840. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13841. ;;;; Finish up
  13842. (provide 'org)
  13843. (run-hooks 'org-load-hook)
  13844. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13845. ;;; org.el ends here