org.el 561 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106
  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.19b
  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.19b"
  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 started regions. In Org-mode,
  183. in special contexts, these same keys are used for other purposes, important
  184. enough to compete with shift selection. Org tries to balance these needs
  185. by supporting `shift-select-mode' outside these special contexts, under
  186. control of this variable.
  187. The default of this variable is nil, to avoid confusing behavior.
  188. When this variable is t and the cursor is not in a special context,
  189. Org-mode will support shift-selection for making and enlarging regions.
  190. Shift-cursor keys then still have special meanings at the following
  191. locations:
  192. - on a headline, changing TODO state (left/right) and priority (up/down)
  193. - on a time stamp, changing the time
  194. - in a property definition line, switching between allowed values
  195. - in the BEGIN line of a clock table (changing the time block).
  196. If you set this variable to the symbol `always', then the keys
  197. will not be special in headlines and property lines, to make shift
  198. selection work there as well. If this is what you want, you can use the
  199. following alternative commands: `C-c C-t' and `C-c ,' to change TODO
  200. state and priority, `C-u C-u C-c C-t' can be used to switch TODO sets,
  201. and properties can be edited by hand or in column view.
  202. However, when the cursor is on a timestamp, shift-cursor commands
  203. will still edit the time stamp - this is just too good to give up.
  204. XEmacs user should have this variable set to nil, because shift-select-mode
  205. is Emacs 23 only."
  206. :group 'org
  207. :type '(choice
  208. (const :tag "Not at all" nil)
  209. (const :tag "When context allows" t)
  210. (const :tag "Everywhere except timestamps" always)))
  211. (defgroup org-startup nil
  212. "Options concerning startup of Org-mode."
  213. :tag "Org Startup"
  214. :group 'org)
  215. (defcustom org-startup-folded t
  216. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  217. This can also be configured on a per-file basis by adding one of
  218. the following lines anywhere in the buffer:
  219. #+STARTUP: fold
  220. #+STARTUP: nofold
  221. #+STARTUP: content"
  222. :group 'org-startup
  223. :type '(choice
  224. (const :tag "nofold: show all" nil)
  225. (const :tag "fold: overview" t)
  226. (const :tag "content: all headlines" content)))
  227. (defcustom org-startup-truncated t
  228. "Non-nil means, entering Org-mode will set `truncate-lines'.
  229. This is useful since some lines containing links can be very long and
  230. uninteresting. Also tables look terrible when wrapped."
  231. :group 'org-startup
  232. :type 'boolean)
  233. (defcustom org-startup-align-all-tables nil
  234. "Non-nil means, align all tables when visiting a file.
  235. This is useful when the column width in tables is forced with <N> cookies
  236. in table fields. Such tables will look correct only after the first re-align.
  237. This can also be configured on a per-file basis by adding one of
  238. the following lines anywhere in the buffer:
  239. #+STARTUP: align
  240. #+STARTUP: noalign"
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-insert-mode-line-in-empty-file nil
  244. "Non-nil means insert the first line setting Org-mode in empty files.
  245. When the function `org-mode' is called interactively in an empty file, this
  246. normally means that the file name does not automatically trigger Org-mode.
  247. To ensure that the file will always be in Org-mode in the future, a
  248. line enforcing Org-mode will be inserted into the buffer, if this option
  249. has been set."
  250. :group 'org-startup
  251. :type 'boolean)
  252. (defcustom org-replace-disputed-keys nil
  253. "Non-nil means use alternative key bindings for some keys.
  254. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  255. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  256. If you want to use Org-mode together with one of these other modes,
  257. or more generally if you would like to move some Org-mode commands to
  258. other keys, set this variable and configure the keys with the variable
  259. `org-disputed-keys'.
  260. This option is only relevant at load-time of Org-mode, and must be set
  261. *before* org.el is loaded. Changing it requires a restart of Emacs to
  262. become effective."
  263. :group 'org-startup
  264. :type 'boolean)
  265. (defcustom org-use-extra-keys nil
  266. "Non-nil means use extra key sequence definitions for certain
  267. commands. This happens automatically if you run XEmacs or if
  268. window-system is nil. This variable lets you do the same
  269. manually. You must set it before loading org.
  270. Example: on Carbon Emacs 22 running graphically, with an external
  271. keyboard on a Powerbook, the default way of setting M-left might
  272. not work for either Alt or ESC. Setting this variable will make
  273. it work for ESC."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (if (fboundp 'defvaralias)
  277. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  278. (defcustom org-disputed-keys
  279. '(([(shift up)] . [(meta p)])
  280. ([(shift down)] . [(meta n)])
  281. ([(shift left)] . [(meta -)])
  282. ([(shift right)] . [(meta +)])
  283. ([(control shift right)] . [(meta shift +)])
  284. ([(control shift left)] . [(meta shift -)]))
  285. "Keys for which Org-mode and other modes compete.
  286. This is an alist, cars are the default keys, second element specifies
  287. the alternative to use when `org-replace-disputed-keys' is t.
  288. Keys can be specified in any syntax supported by `define-key'.
  289. The value of this option takes effect only at Org-mode's startup,
  290. therefore you'll have to restart Emacs to apply it after changing."
  291. :group 'org-startup
  292. :type 'alist)
  293. (defun org-key (key)
  294. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  295. Or return the original if not disputed."
  296. (if org-replace-disputed-keys
  297. (let* ((nkey (key-description key))
  298. (x (org-find-if (lambda (x)
  299. (equal (key-description (car x)) nkey))
  300. org-disputed-keys)))
  301. (if x (cdr x) key))
  302. key))
  303. (defun org-find-if (predicate seq)
  304. (catch 'exit
  305. (while seq
  306. (if (funcall predicate (car seq))
  307. (throw 'exit (car seq))
  308. (pop seq)))))
  309. (defun org-defkey (keymap key def)
  310. "Define a key, possibly translated, as returned by `org-key'."
  311. (define-key keymap (org-key key) def))
  312. (defcustom org-ellipsis nil
  313. "The ellipsis to use in the Org-mode outline.
  314. When nil, just use the standard three dots. When a string, use that instead,
  315. When a face, use the standard 3 dots, but with the specified face.
  316. The change affects only Org-mode (which will then use its own display table).
  317. Changing this requires executing `M-x org-mode' in a buffer to become
  318. effective."
  319. :group 'org-startup
  320. :type '(choice (const :tag "Default" nil)
  321. (face :tag "Face" :value org-warning)
  322. (string :tag "String" :value "...#")))
  323. (defvar org-display-table nil
  324. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  325. (defgroup org-keywords nil
  326. "Keywords in Org-mode."
  327. :tag "Org Keywords"
  328. :group 'org)
  329. (defcustom org-deadline-string "DEADLINE:"
  330. "String to mark deadline entries.
  331. A deadline is this string, followed by a time stamp. Should be a word,
  332. terminated by a colon. You can insert a schedule keyword and
  333. a timestamp with \\[org-deadline].
  334. Changes become only effective after restarting Emacs."
  335. :group 'org-keywords
  336. :type 'string)
  337. (defcustom org-scheduled-string "SCHEDULED:"
  338. "String to mark scheduled TODO entries.
  339. A schedule is this string, followed by a time stamp. Should be a word,
  340. terminated by a colon. You can insert a schedule keyword and
  341. a timestamp with \\[org-schedule].
  342. Changes become only effective after restarting Emacs."
  343. :group 'org-keywords
  344. :type 'string)
  345. (defcustom org-closed-string "CLOSED:"
  346. "String used as the prefix for timestamps logging closing a TODO entry."
  347. :group 'org-keywords
  348. :type 'string)
  349. (defcustom org-clock-string "CLOCK:"
  350. "String used as prefix for timestamps clocking work hours on an item."
  351. :group 'org-keywords
  352. :type 'string)
  353. (defcustom org-comment-string "COMMENT"
  354. "Entries starting with this keyword will never be exported.
  355. An entry can be toggled between COMMENT and normal with
  356. \\[org-toggle-comment].
  357. Changes become only effective after restarting Emacs."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-quote-string "QUOTE"
  361. "Entries starting with this keyword will be exported in fixed-width font.
  362. Quoting applies only to the text in the entry following the headline, and does
  363. not extend beyond the next headline, even if that is lower level.
  364. An entry can be toggled between QUOTE and normal with
  365. \\[org-toggle-fixed-width-section]."
  366. :group 'org-keywords
  367. :type 'string)
  368. (defconst org-repeat-re
  369. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  370. "Regular expression for specifying repeated events.
  371. After a match, group 1 contains the repeat expression.")
  372. (defgroup org-structure nil
  373. "Options concerning the general structure of Org-mode files."
  374. :tag "Org Structure"
  375. :group 'org)
  376. (defgroup org-reveal-location nil
  377. "Options about how to make context of a location visible."
  378. :tag "Org Reveal Location"
  379. :group 'org-structure)
  380. (defconst org-context-choice
  381. '(choice
  382. (const :tag "Always" t)
  383. (const :tag "Never" nil)
  384. (repeat :greedy t :tag "Individual contexts"
  385. (cons
  386. (choice :tag "Context"
  387. (const agenda)
  388. (const org-goto)
  389. (const occur-tree)
  390. (const tags-tree)
  391. (const link-search)
  392. (const mark-goto)
  393. (const bookmark-jump)
  394. (const isearch)
  395. (const default))
  396. (boolean))))
  397. "Contexts for the reveal options.")
  398. (defcustom org-show-hierarchy-above '((default . t))
  399. "Non-nil means, show full hierarchy when revealing a location.
  400. Org-mode often shows locations in an org-mode file which might have
  401. been invisible before. When this is set, the hierarchy of headings
  402. above the exposed location is shown.
  403. Turning this off for example for sparse trees makes them very compact.
  404. Instead of t, this can also be an alist specifying this option for different
  405. contexts. Valid contexts are
  406. agenda when exposing an entry from the agenda
  407. org-goto when using the command `org-goto' on key C-c C-j
  408. occur-tree when using the command `org-occur' on key C-c /
  409. tags-tree when constructing a sparse tree based on tags matches
  410. link-search when exposing search matches associated with a link
  411. mark-goto when exposing the jump goal of a mark
  412. bookmark-jump when exposing a bookmark location
  413. isearch when exiting from an incremental search
  414. default default for all contexts not set explicitly"
  415. :group 'org-reveal-location
  416. :type org-context-choice)
  417. (defcustom org-show-following-heading '((default . nil))
  418. "Non-nil means, show following heading when revealing a location.
  419. Org-mode often shows locations in an org-mode file which might have
  420. been invisible before. When this is set, the heading following the
  421. match is shown.
  422. Turning this off for example for sparse trees makes them very compact,
  423. but makes it harder to edit the location of the match. In such a case,
  424. use the command \\[org-reveal] to show more context.
  425. Instead of t, this can also be an alist specifying this option for different
  426. contexts. See `org-show-hierarchy-above' for valid contexts."
  427. :group 'org-reveal-location
  428. :type org-context-choice)
  429. (defcustom org-show-siblings '((default . nil) (isearch t))
  430. "Non-nil means, show all sibling heading when revealing a location.
  431. Org-mode often shows locations in an org-mode file which might have
  432. been invisible before. When this is set, the sibling of the current entry
  433. heading are all made visible. If `org-show-hierarchy-above' is t,
  434. the same happens on each level of the hierarchy above the current entry.
  435. By default this is on for the isearch context, off for all other contexts.
  436. Turning this off for example for sparse trees makes them very compact,
  437. but makes it harder to edit the location of the match. In such a case,
  438. use the command \\[org-reveal] to show more context.
  439. Instead of t, this can also be an alist specifying this option for different
  440. contexts. See `org-show-hierarchy-above' for valid contexts."
  441. :group 'org-reveal-location
  442. :type org-context-choice)
  443. (defcustom org-show-entry-below '((default . nil))
  444. "Non-nil means, show the entry below a headline when revealing a location.
  445. Org-mode often shows locations in an org-mode file which might have
  446. been invisible before. When this is set, the text below the headline that is
  447. exposed is also shown.
  448. By default this is off for all contexts.
  449. Instead of t, this can also be an alist specifying this option for different
  450. contexts. See `org-show-hierarchy-above' for valid contexts."
  451. :group 'org-reveal-location
  452. :type org-context-choice)
  453. (defcustom org-indirect-buffer-display 'other-window
  454. "How should indirect tree buffers be displayed?
  455. This applies to indirect buffers created with the commands
  456. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  457. Valid values are:
  458. current-window Display in the current window
  459. other-window Just display in another window.
  460. dedicated-frame Create one new frame, and re-use it each time.
  461. new-frame Make a new frame each time. Note that in this case
  462. previously-made indirect buffers are kept, and you need to
  463. kill these buffers yourself."
  464. :group 'org-structure
  465. :group 'org-agenda-windows
  466. :type '(choice
  467. (const :tag "In current window" current-window)
  468. (const :tag "In current frame, other window" other-window)
  469. (const :tag "Each time a new frame" new-frame)
  470. (const :tag "One dedicated frame" dedicated-frame)))
  471. (defgroup org-cycle nil
  472. "Options concerning visibility cycling in Org-mode."
  473. :tag "Org Cycle"
  474. :group 'org-structure)
  475. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  476. "Names of drawers. Drawers are not opened by cycling on the headline above.
  477. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  478. this:
  479. :DRAWERNAME:
  480. .....
  481. :END:
  482. The drawer \"PROPERTIES\" is special for capturing properties through
  483. the property API.
  484. Drawers can be defined on the per-file basis with a line like:
  485. #+DRAWERS: HIDDEN STATE PROPERTIES"
  486. :group 'org-structure
  487. :type '(repeat (string :tag "Drawer Name")))
  488. (defcustom org-cycle-global-at-bob nil
  489. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  490. This makes it possible to do global cycling without having to use S-TAB or
  491. C-u TAB. For this special case to work, the first line of the buffer
  492. must not be a headline - it may be empty ot some other text. When used in
  493. this way, `org-cycle-hook' is disables temporarily, to make sure the
  494. cursor stays at the beginning of the buffer.
  495. When this option is nil, don't do anything special at the beginning
  496. of the buffer."
  497. :group 'org-cycle
  498. :type 'boolean)
  499. (defcustom org-cycle-emulate-tab t
  500. "Where should `org-cycle' emulate TAB.
  501. nil Never
  502. white Only in completely white lines
  503. whitestart Only at the beginning of lines, before the first non-white char
  504. t Everywhere except in headlines
  505. exc-hl-bol Everywhere except at the start of a headline
  506. If TAB is used in a place where it does not emulate TAB, the current subtree
  507. visibility is cycled."
  508. :group 'org-cycle
  509. :type '(choice (const :tag "Never" nil)
  510. (const :tag "Only in completely white lines" white)
  511. (const :tag "Before first char in a line" whitestart)
  512. (const :tag "Everywhere except in headlines" t)
  513. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  514. ))
  515. (defcustom org-cycle-separator-lines 2
  516. "Number of empty lines needed to keep an empty line between collapsed trees.
  517. If you leave an empty line between the end of a subtree and the following
  518. headline, this empty line is hidden when the subtree is folded.
  519. Org-mode will leave (exactly) one empty line visible if the number of
  520. empty lines is equal or larger to the number given in this variable.
  521. So the default 2 means, at least 2 empty lines after the end of a subtree
  522. are needed to produce free space between a collapsed subtree and the
  523. following headline.
  524. Special case: when 0, never leave empty lines in collapsed view."
  525. :group 'org-cycle
  526. :type 'integer)
  527. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  528. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  529. org-cycle-hide-drawers
  530. org-cycle-show-empty-lines
  531. org-optimize-window-after-visibility-change)
  532. "Hook that is run after `org-cycle' has changed the buffer visibility.
  533. The function(s) in this hook must accept a single argument which indicates
  534. the new state that was set by the most recent `org-cycle' command. The
  535. argument is a symbol. After a global state change, it can have the values
  536. `overview', `content', or `all'. After a local state change, it can have
  537. the values `folded', `children', or `subtree'."
  538. :group 'org-cycle
  539. :type 'hook)
  540. (defgroup org-edit-structure nil
  541. "Options concerning structure editing in Org-mode."
  542. :tag "Org Edit Structure"
  543. :group 'org-structure)
  544. (defcustom org-odd-levels-only nil
  545. "Non-nil means, skip even levels and only use odd levels for the outline.
  546. This has the effect that two stars are being added/taken away in
  547. promotion/demotion commands. It also influences how levels are
  548. handled by the exporters.
  549. Changing it requires restart of `font-lock-mode' to become effective
  550. for fontification also in regions already fontified.
  551. You may also set this on a per-file basis by adding one of the following
  552. lines to the buffer:
  553. #+STARTUP: odd
  554. #+STARTUP: oddeven"
  555. :group 'org-edit-structure
  556. :group 'org-font-lock
  557. :type 'boolean)
  558. (defcustom org-adapt-indentation t
  559. "Non-nil means, adapt indentation when promoting and demoting.
  560. When this is set and the *entire* text in an entry is indented, the
  561. indentation is increased by one space in a demotion command, and
  562. decreased by one in a promotion command. If any line in the entry
  563. body starts at column 0, indentation is not changed at all."
  564. :group 'org-edit-structure
  565. :type 'boolean)
  566. (defcustom org-special-ctrl-a/e nil
  567. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  568. When t, `C-a' will bring back the cursor to the beginning of the
  569. headline text, i.e. after the stars and after a possible TODO keyword.
  570. In an item, this will be the position after the bullet.
  571. When the cursor is already at that position, another `C-a' will bring
  572. it to the beginning of the line.
  573. `C-e' will jump to the end of the headline, ignoring the presence of tags
  574. in the headline. A second `C-e' will then jump to the true end of the
  575. line, after any tags.
  576. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  577. and only a directly following, identical keypress will bring the cursor
  578. to the special positions."
  579. :group 'org-edit-structure
  580. :type '(choice
  581. (const :tag "off" nil)
  582. (const :tag "after bullet first" t)
  583. (const :tag "border first" reversed)))
  584. (if (fboundp 'defvaralias)
  585. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  586. (defcustom org-special-ctrl-k nil
  587. "Non-nil means `C-k' will behave specially in headlines.
  588. When nil, `C-k' will call the default `kill-line' command.
  589. When t, the following will happen while the cursor is in the headline:
  590. - When the cursor is at the beginning of a headline, kill the entire
  591. line and possible the folded subtree below the line.
  592. - When in the middle of the headline text, kill the headline up to the tags.
  593. - When after the headline text, kill the tags."
  594. :group 'org-edit-structure
  595. :type 'boolean)
  596. (defcustom org-yank-folded-subtrees t
  597. "Non-nil means, when yanking subtrees, fold them.
  598. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  599. it starts with a heading and all other headings in it are either children
  600. or siblings, then fold all the subtrees. However, do this only if no
  601. text after the yank would be swallowed into a folded tree by this action."
  602. :group 'org-edit-structure
  603. :type 'boolean)
  604. (defcustom org-yank-adjusted-subtrees t
  605. "Non-nil means, when yanking subtrees, adjust the level.
  606. With this setting, `org-paste-subtree' is used to insert the subtree, see
  607. this function for details."
  608. :group 'org-edit-structure
  609. :type 'boolean)
  610. (defcustom org-M-RET-may-split-line '((default . t))
  611. "Non-nil means, M-RET will split the line at the cursor position.
  612. When nil, it will go to the end of the line before making a
  613. new line.
  614. You may also set this option in a different way for different
  615. contexts. Valid contexts are:
  616. headline when creating a new headline
  617. item when creating a new item
  618. table in a table field
  619. default the value to be used for all contexts not explicitly
  620. customized"
  621. :group 'org-structure
  622. :group 'org-table
  623. :type '(choice
  624. (const :tag "Always" t)
  625. (const :tag "Never" nil)
  626. (repeat :greedy t :tag "Individual contexts"
  627. (cons
  628. (choice :tag "Context"
  629. (const headline)
  630. (const item)
  631. (const table)
  632. (const default))
  633. (boolean)))))
  634. (defcustom org-insert-heading-respect-content nil
  635. "Non-nil means, insert new headings after the current subtree.
  636. When nil, the new heading is created directly after the current line.
  637. The commands \\[org-insert-heading-respect-content] and
  638. \\[org-insert-todo-heading-respect-content] turn this variable on
  639. for the duration of the command."
  640. :group 'org-structure
  641. :type 'boolean)
  642. (defcustom org-blank-before-new-entry '((heading . auto)
  643. (plain-list-item . auto))
  644. "Should `org-insert-heading' leave a blank line before new heading/item?
  645. The value is an alist, with `heading' and `plain-list-item' as car,
  646. and a boolean flag as cdr."
  647. :group 'org-edit-structure
  648. :type '(list
  649. (cons (const heading)
  650. (choice (const :tag "Never" nil)
  651. (const :tag "Always" t)
  652. (const :tag "Auto" auto)))
  653. (cons (const plain-list-item)
  654. (choice (const :tag "Never" nil)
  655. (const :tag "Always" t)
  656. (const :tag "Auto" auto)))))
  657. (defcustom org-insert-heading-hook nil
  658. "Hook being run after inserting a new heading."
  659. :group 'org-edit-structure
  660. :type 'hook)
  661. (defcustom org-enable-fixed-width-editor t
  662. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  663. This currently only means, they are never auto-wrapped.
  664. When nil, such lines will be treated like ordinary lines.
  665. See also the QUOTE keyword."
  666. :group 'org-edit-structure
  667. :type 'boolean)
  668. (defcustom org-edit-src-region-extra nil
  669. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  670. For examples see the function `org-edit-src-find-region-and-lang'.
  671. The regular expression identifying the begin marker should end with a newline,
  672. and the regexp marking the end line should start with a newline, to make sure
  673. there are kept outside the narrowed region."
  674. :group 'org-edit-structure
  675. :type '(repeat
  676. (list
  677. (regexp :tag "begin regexp")
  678. (regexp :tag "end regexp")
  679. (choice :tag "language"
  680. (string :tag "specify")
  681. (integer :tag "from match group")
  682. (const :tag "from `lang' element")
  683. (const :tag "from `style' element")))))
  684. (defcustom org-coderef-label-format "(ref:%s)"
  685. "The default coderef format.
  686. This format string will be used to search for coderef labels in literal
  687. examples (EXAMPLE and SRC blocks). The format can be overwritten
  688. an individual literal example with the -f option, like
  689. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  690. ...
  691. #+END_SRC
  692. If you want to use this for HTML export, make sure that the format does
  693. not introduce special font-locking, and avoid the HTML special
  694. characters `<', `>', and `&'. The reason for this restriction is that
  695. the labels are searched for only after htmlize has done its job."
  696. :group 'org-edit-structure ; FIXME this is not in the right group
  697. :type 'string)
  698. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  699. "The mode that should be used to edit fixed-width regions.
  700. These are the regions where each line starts with a colon."
  701. :group 'org-edit-structure
  702. :type '(choice
  703. (const artist-mode)
  704. (const picture-mode)
  705. (const fundamental-mode)
  706. (function :tag "Other (specify)")))
  707. (defcustom org-goto-auto-isearch t
  708. "Non-nil means, typing characters in org-goto starts incremental search."
  709. :group 'org-edit-structure
  710. :type 'boolean)
  711. (defgroup org-sparse-trees nil
  712. "Options concerning sparse trees in Org-mode."
  713. :tag "Org Sparse Trees"
  714. :group 'org-structure)
  715. (defcustom org-highlight-sparse-tree-matches t
  716. "Non-nil means, highlight all matches that define a sparse tree.
  717. The highlights will automatically disappear the next time the buffer is
  718. changed by an edit command."
  719. :group 'org-sparse-trees
  720. :type 'boolean)
  721. (defcustom org-remove-highlights-with-change t
  722. "Non-nil means, any change to the buffer will remove temporary highlights.
  723. Such highlights are created by `org-occur' and `org-clock-display'.
  724. When nil, `C-c C-c needs to be used to get rid of the highlights.
  725. The highlights created by `org-preview-latex-fragment' always need
  726. `C-c C-c' to be removed."
  727. :group 'org-sparse-trees
  728. :group 'org-time
  729. :type 'boolean)
  730. (defcustom org-occur-hook '(org-first-headline-recenter)
  731. "Hook that is run after `org-occur' has constructed a sparse tree.
  732. This can be used to recenter the window to show as much of the structure
  733. as possible."
  734. :group 'org-sparse-trees
  735. :type 'hook)
  736. (defgroup org-imenu-and-speedbar nil
  737. "Options concerning imenu and speedbar in Org-mode."
  738. :tag "Org Imenu and Speedbar"
  739. :group 'org-structure)
  740. (defcustom org-imenu-depth 2
  741. "The maximum level for Imenu access to Org-mode headlines.
  742. This also applied for speedbar access."
  743. :group 'org-imenu-and-speedbar
  744. :type 'number)
  745. (defgroup org-table nil
  746. "Options concerning tables in Org-mode."
  747. :tag "Org Table"
  748. :group 'org)
  749. (defcustom org-enable-table-editor 'optimized
  750. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  751. When nil, such lines will be treated like ordinary lines.
  752. When equal to the symbol `optimized', the table editor will be optimized to
  753. do the following:
  754. - Automatic overwrite mode in front of whitespace in table fields.
  755. This makes the structure of the table stay in tact as long as the edited
  756. field does not exceed the column width.
  757. - Minimize the number of realigns. Normally, the table is aligned each time
  758. TAB or RET are pressed to move to another field. With optimization this
  759. happens only if changes to a field might have changed the column width.
  760. Optimization requires replacing the functions `self-insert-command',
  761. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  762. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  763. very good at guessing when a re-align will be necessary, but you can always
  764. force one with \\[org-ctrl-c-ctrl-c].
  765. If you would like to use the optimized version in Org-mode, but the
  766. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  767. This variable can be used to turn on and off the table editor during a session,
  768. but in order to toggle optimization, a restart is required.
  769. See also the variable `org-table-auto-blank-field'."
  770. :group 'org-table
  771. :type '(choice
  772. (const :tag "off" nil)
  773. (const :tag "on" t)
  774. (const :tag "on, optimized" optimized)))
  775. (defcustom org-table-tab-recognizes-table.el t
  776. "Non-nil means, TAB will automatically notice a table.el table.
  777. When it sees such a table, it moves point into it and - if necessary -
  778. calls `table-recognize-table'."
  779. :group 'org-table-editing
  780. :type 'boolean)
  781. (defgroup org-link nil
  782. "Options concerning links in Org-mode."
  783. :tag "Org Link"
  784. :group 'org)
  785. (defvar org-link-abbrev-alist-local nil
  786. "Buffer-local version of `org-link-abbrev-alist', which see.
  787. The value of this is taken from the #+LINK lines.")
  788. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  789. (defcustom org-link-abbrev-alist nil
  790. "Alist of link abbreviations.
  791. The car of each element is a string, to be replaced at the start of a link.
  792. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  793. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  794. [[linkkey:tag][description]]
  795. If REPLACE is a string, the tag will simply be appended to create the link.
  796. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  797. the placeholder \"%h\" will cause a url-encoded version of the tag to
  798. be inserted at that point (see the function `url-hexify-string').
  799. REPLACE may also be a function that will be called with the tag as the
  800. only argument to create the link, which should be returned as a string.
  801. See the manual for examples."
  802. :group 'org-link
  803. :type '(repeat
  804. (cons
  805. (string :tag "Protocol")
  806. (choice
  807. (string :tag "Format")
  808. (function)))))
  809. (defcustom org-descriptive-links t
  810. "Non-nil means, hide link part and only show description of bracket links.
  811. Bracket links are like [[link][description]]. This variable sets the initial
  812. state in new org-mode buffers. The setting can then be toggled on a
  813. per-buffer basis from the Org->Hyperlinks menu."
  814. :group 'org-link
  815. :type 'boolean)
  816. (defcustom org-link-file-path-type 'adaptive
  817. "How the path name in file links should be stored.
  818. Valid values are:
  819. relative Relative to the current directory, i.e. the directory of the file
  820. into which the link is being inserted.
  821. absolute Absolute path, if possible with ~ for home directory.
  822. noabbrev Absolute path, no abbreviation of home directory.
  823. adaptive Use relative path for files in the current directory and sub-
  824. directories of it. For other files, use an absolute path."
  825. :group 'org-link
  826. :type '(choice
  827. (const relative)
  828. (const absolute)
  829. (const noabbrev)
  830. (const adaptive)))
  831. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  832. "Types of links that should be activated in Org-mode files.
  833. This is a list of symbols, each leading to the activation of a certain link
  834. type. In principle, it does not hurt to turn on most link types - there may
  835. be a small gain when turning off unused link types. The types are:
  836. bracket The recommended [[link][description]] or [[link]] links with hiding.
  837. angular Links in angular brackets that may contain whitespace like
  838. <bbdb:Carsten Dominik>.
  839. plain Plain links in normal text, no whitespace, like http://google.com.
  840. radio Text that is matched by a radio target, see manual for details.
  841. tag Tag settings in a headline (link to tag search).
  842. date Time stamps (link to calendar).
  843. footnote Footnote labels.
  844. Changing this variable requires a restart of Emacs to become effective."
  845. :group 'org-link
  846. :type '(set :greedy t
  847. (const :tag "Double bracket links (new style)" bracket)
  848. (const :tag "Angular bracket links (old style)" angular)
  849. (const :tag "Plain text links" plain)
  850. (const :tag "Radio target matches" radio)
  851. (const :tag "Tags" tag)
  852. (const :tag "Timestamps" date)
  853. (const :tag "Footnotes" footnote)))
  854. (defcustom org-make-link-description-function nil
  855. "Function to use to generate link descriptions from links. If
  856. nil the link location will be used. This function must take two
  857. parameters; the first is the link and the second the description
  858. org-insert-link has generated, and should return the description
  859. to use."
  860. :group 'org-link
  861. :type 'function)
  862. (defgroup org-link-store nil
  863. "Options concerning storing links in Org-mode."
  864. :tag "Org Store Link"
  865. :group 'org-link)
  866. (defcustom org-email-link-description-format "Email %c: %.30s"
  867. "Format of the description part of a link to an email or usenet message.
  868. The following %-escapes will be replaced by corresponding information:
  869. %F full \"From\" field
  870. %f name, taken from \"From\" field, address if no name
  871. %T full \"To\" field
  872. %t first name in \"To\" field, address if no name
  873. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  874. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  875. %s subject
  876. %m message-id.
  877. You may use normal field width specification between the % and the letter.
  878. This is for example useful to limit the length of the subject.
  879. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  880. :group 'org-link-store
  881. :type 'string)
  882. (defcustom org-from-is-user-regexp
  883. (let (r1 r2)
  884. (when (and user-mail-address (not (string= user-mail-address "")))
  885. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  886. (when (and user-full-name (not (string= user-full-name "")))
  887. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  888. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  889. "Regexp matched against the \"From:\" header of an email or usenet message.
  890. It should match if the message is from the user him/herself."
  891. :group 'org-link-store
  892. :type 'regexp)
  893. (defcustom org-link-to-org-use-id 'create-if-interactive
  894. "Non-nil means, storing a link to an Org file will use entry IDs.
  895. Note that before this variable is even considered, org-id must be loaded,
  896. to please customize `org-modules' and turn it on.
  897. The variable can have the following values:
  898. t Create an ID if needed to make a link to the current entry.
  899. create-if-interactive
  900. If `org-store-link' is called directly (interactively, as a user
  901. command), do create an ID to support the link. But when doing the
  902. job for remember, only use the ID if it already exists. The
  903. purpose of this setting is to avoid proliferation of unwanted
  904. IDs, just because you happen to be in an Org file when you
  905. call `org-remember' that automatically and preemptively
  906. creates a link. If you do want to get an ID link in a remember
  907. template to an entry not having an ID, create it first by
  908. explicitly creating a link to it, using `C-c C-l' first.
  909. use-existing
  910. Use existing ID, do not create one.
  911. nil Never use an ID to make a link, instead link using a text search for
  912. the headline text."
  913. :group 'org-link-store
  914. :type '(choice
  915. (const :tag "Create ID to make link" t)
  916. (const :tag "Create if string link interactively"
  917. 'create-if-interactive)
  918. (const :tag "Only use existing" 'use-existing)
  919. (const :tag "Do not use ID to create link" nil)))
  920. (defcustom org-context-in-file-links t
  921. "Non-nil means, file links from `org-store-link' contain context.
  922. A search string will be added to the file name with :: as separator and
  923. used to find the context when the link is activated by the command
  924. `org-open-at-point'.
  925. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  926. negates this setting for the duration of the command."
  927. :group 'org-link-store
  928. :type 'boolean)
  929. (defcustom org-keep-stored-link-after-insertion nil
  930. "Non-nil means, keep link in list for entire session.
  931. The command `org-store-link' adds a link pointing to the current
  932. location to an internal list. These links accumulate during a session.
  933. The command `org-insert-link' can be used to insert links into any
  934. Org-mode file (offering completion for all stored links). When this
  935. option is nil, every link which has been inserted once using \\[org-insert-link]
  936. will be removed from the list, to make completing the unused links
  937. more efficient."
  938. :group 'org-link-store
  939. :type 'boolean)
  940. (defgroup org-link-follow nil
  941. "Options concerning following links in Org-mode."
  942. :tag "Org Follow Link"
  943. :group 'org-link)
  944. (defcustom org-link-translation-function nil
  945. "Function to translate links with different syntax to Org syntax.
  946. This can be used to translate links created for example by the Planner
  947. or emacs-wiki packages to Org syntax.
  948. The function must accept two parameters, a TYPE containing the link
  949. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  950. which is everything after the link protocol. It should return a cons
  951. with possibly modified values of type and path.
  952. Org contains a function for this, so if you set this variable to
  953. `org-translate-link-from-planner', you should be able follow many
  954. links created by planner."
  955. :group 'org-link-follow
  956. :type 'function)
  957. (defcustom org-follow-link-hook nil
  958. "Hook that is run after a link has been followed."
  959. :group 'org-link-follow
  960. :type 'hook)
  961. (defcustom org-tab-follows-link nil
  962. "Non-nil means, on links TAB will follow the link.
  963. Needs to be set before org.el is loaded."
  964. :group 'org-link-follow
  965. :type 'boolean)
  966. (defcustom org-return-follows-link nil
  967. "Non-nil means, on links RET will follow the link.
  968. Needs to be set before org.el is loaded."
  969. :group 'org-link-follow
  970. :type 'boolean)
  971. (defcustom org-mouse-1-follows-link
  972. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  973. "Non-nil means, mouse-1 on a link will follow the link.
  974. A longer mouse click will still set point. Does not work on XEmacs.
  975. Needs to be set before org.el is loaded."
  976. :group 'org-link-follow
  977. :type 'boolean)
  978. (defcustom org-mark-ring-length 4
  979. "Number of different positions to be recorded in the ring
  980. Changing this requires a restart of Emacs to work correctly."
  981. :group 'org-link-follow
  982. :type 'integer)
  983. (defcustom org-link-frame-setup
  984. '((vm . vm-visit-folder-other-frame)
  985. (gnus . gnus-other-frame)
  986. (file . find-file-other-window))
  987. "Setup the frame configuration for following links.
  988. When following a link with Emacs, it may often be useful to display
  989. this link in another window or frame. This variable can be used to
  990. set this up for the different types of links.
  991. For VM, use any of
  992. `vm-visit-folder'
  993. `vm-visit-folder-other-frame'
  994. For Gnus, use any of
  995. `gnus'
  996. `gnus-other-frame'
  997. `org-gnus-no-new-news'
  998. For FILE, use any of
  999. `find-file'
  1000. `find-file-other-window'
  1001. `find-file-other-frame'
  1002. For the calendar, use the variable `calendar-setup'.
  1003. For BBDB, it is currently only possible to display the matches in
  1004. another window."
  1005. :group 'org-link-follow
  1006. :type '(list
  1007. (cons (const vm)
  1008. (choice
  1009. (const vm-visit-folder)
  1010. (const vm-visit-folder-other-window)
  1011. (const vm-visit-folder-other-frame)))
  1012. (cons (const gnus)
  1013. (choice
  1014. (const gnus)
  1015. (const gnus-other-frame)
  1016. (const org-gnus-no-new-news)))
  1017. (cons (const file)
  1018. (choice
  1019. (const find-file)
  1020. (const find-file-other-window)
  1021. (const find-file-other-frame)))))
  1022. (defcustom org-display-internal-link-with-indirect-buffer nil
  1023. "Non-nil means, use indirect buffer to display infile links.
  1024. Activating internal links (from one location in a file to another location
  1025. in the same file) normally just jumps to the location. When the link is
  1026. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1027. another window. When this option is set, the other window actually displays
  1028. an indirect buffer clone of the current buffer, to avoid any visibility
  1029. changes to the current buffer."
  1030. :group 'org-link-follow
  1031. :type 'boolean)
  1032. (defcustom org-open-non-existing-files nil
  1033. "Non-nil means, `org-open-file' will open non-existing files.
  1034. When nil, an error will be generated."
  1035. :group 'org-link-follow
  1036. :type 'boolean)
  1037. (defcustom org-open-directory-means-index-dot-org nil
  1038. "Non-nil means, a link to a directory really means to index.org.
  1039. When nil, following a directory link will run dired or open a finder/explorer
  1040. window on that directory."
  1041. :group 'org-link-follow
  1042. :type 'boolean)
  1043. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1044. "Function and arguments to call for following mailto links.
  1045. This is a list with the first element being a lisp function, and the
  1046. remaining elements being arguments to the function. In string arguments,
  1047. %a will be replaced by the address, and %s will be replaced by the subject
  1048. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1049. :group 'org-link-follow
  1050. :type '(choice
  1051. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1052. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1053. (const :tag "message-mail" (message-mail "%a" "%s"))
  1054. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1055. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1056. "Non-nil means, ask for confirmation before executing shell links.
  1057. Shell links can be dangerous: just think about a link
  1058. [[shell:rm -rf ~/*][Google Search]]
  1059. This link would show up in your Org-mode document as \"Google Search\",
  1060. but really it would remove your entire home directory.
  1061. Therefore we advise against setting this variable to nil.
  1062. Just change it to `y-or-n-p' of you want to confirm with a
  1063. single keystroke rather than having to type \"yes\"."
  1064. :group 'org-link-follow
  1065. :type '(choice
  1066. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1067. (const :tag "with y-or-n (faster)" y-or-n-p)
  1068. (const :tag "no confirmation (dangerous)" nil)))
  1069. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1070. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1071. Elisp links can be dangerous: just think about a link
  1072. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1073. This link would show up in your Org-mode document as \"Google Search\",
  1074. but really it would remove your entire home directory.
  1075. Therefore we advise against setting this variable to nil.
  1076. Just change it to `y-or-n-p' of you want to confirm with a
  1077. single keystroke rather than having to type \"yes\"."
  1078. :group 'org-link-follow
  1079. :type '(choice
  1080. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1081. (const :tag "with y-or-n (faster)" y-or-n-p)
  1082. (const :tag "no confirmation (dangerous)" nil)))
  1083. (defconst org-file-apps-defaults-gnu
  1084. '((remote . emacs)
  1085. (system . mailcap)
  1086. (t . mailcap))
  1087. "Default file applications on a UNIX or GNU/Linux system.
  1088. See `org-file-apps'.")
  1089. (defconst org-file-apps-defaults-macosx
  1090. '((remote . emacs)
  1091. (t . "open %s")
  1092. (system . "open %s")
  1093. ("ps.gz" . "gv %s")
  1094. ("eps.gz" . "gv %s")
  1095. ("dvi" . "xdvi %s")
  1096. ("fig" . "xfig %s"))
  1097. "Default file applications on a MacOS X system.
  1098. The system \"open\" is known as a default, but we use X11 applications
  1099. for some files for which the OS does not have a good default.
  1100. See `org-file-apps'.")
  1101. (defconst org-file-apps-defaults-windowsnt
  1102. (list
  1103. '(remote . emacs)
  1104. (cons t
  1105. (list (if (featurep 'xemacs)
  1106. 'mswindows-shell-execute
  1107. 'w32-shell-execute)
  1108. "open" 'file))
  1109. (cons 'system
  1110. (list (if (featurep 'xemacs)
  1111. 'mswindows-shell-execute
  1112. 'w32-shell-execute)
  1113. "open" 'file)))
  1114. "Default file applications on a Windows NT system.
  1115. The system \"open\" is used for most files.
  1116. See `org-file-apps'.")
  1117. (defcustom org-file-apps
  1118. '(
  1119. (auto-mode . emacs)
  1120. ("\\.x?html?\\'" . default)
  1121. ("\\.pdf\\'" . default)
  1122. )
  1123. "External applications for opening `file:path' items in a document.
  1124. Org-mode uses system defaults for different file types, but
  1125. you can use this variable to set the application for a given file
  1126. extension. The entries in this list are cons cells where the car identifies
  1127. files and the cdr the corresponding command. Possible values for the
  1128. file identifier are
  1129. \"regex\" Regular expression matched against the file name. For backward
  1130. compatibility, this can also be a string with only alphanumeric
  1131. characters, which is then interpreted as an extension.
  1132. `directory' Matches a directory
  1133. `remote' Matches a remote file, accessible through tramp or efs.
  1134. Remote files most likely should be visited through Emacs
  1135. because external applications cannot handle such paths.
  1136. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1137. so all files Emacs knows how to handle. Using this with
  1138. command `emacs' will open most files in Emacs. Beware that this
  1139. will also open html files inside Emacs, unless you add
  1140. (\"html\" . default) to the list as well.
  1141. t Default for files not matched by any of the other options.
  1142. `system' The system command to open files, like `open' on Windows
  1143. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1144. that will be selected if you call `C-c C-o' with a double
  1145. `C-u C-u' prefix.
  1146. Possible values for the command are:
  1147. `emacs' The file will be visited by the current Emacs process.
  1148. `default' Use the default application for this file type, which is the
  1149. association for t in the list, most likely in the system-specific
  1150. part.
  1151. This can be used to overrule an unwanted setting in the
  1152. system-specific variable.
  1153. `system' Use the system command for opening files, like \"open\".
  1154. This command is specified by the entry whose car is `system'.
  1155. Most likely, the system-specific version of this variable
  1156. does define this command, but you can overrule/replace it
  1157. here.
  1158. string A command to be executed by a shell; %s will be replaced
  1159. by the path to the file.
  1160. sexp A Lisp form which will be evaluated. The file path will
  1161. be available in the Lisp variable `file'.
  1162. For more examples, see the system specific constants
  1163. `org-file-apps-defaults-macosx'
  1164. `org-file-apps-defaults-windowsnt'
  1165. `org-file-apps-defaults-gnu'."
  1166. :group 'org-link-follow
  1167. :type '(repeat
  1168. (cons (choice :value ""
  1169. (string :tag "Extension")
  1170. (const :tag "System command to open files" system)
  1171. (const :tag "Default for unrecognized files" t)
  1172. (const :tag "Remote file" remote)
  1173. (const :tag "Links to a directory" directory)
  1174. (const :tag "Any files that have Emacs modes"
  1175. auto-mode))
  1176. (choice :value ""
  1177. (const :tag "Visit with Emacs" emacs)
  1178. (const :tag "Use default" default)
  1179. (const :tag "Use the system command" system)
  1180. (string :tag "Command")
  1181. (sexp :tag "Lisp form")))))
  1182. (defgroup org-refile nil
  1183. "Options concerning refiling entries in Org-mode."
  1184. :tag "Org Refile"
  1185. :group 'org)
  1186. (defcustom org-directory "~/org"
  1187. "Directory with org files.
  1188. This directory will be used as default to prompt for org files.
  1189. Used by the hooks for remember.el."
  1190. :group 'org-refile
  1191. :group 'org-remember
  1192. :type 'directory)
  1193. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1194. "Default target for storing notes.
  1195. Used by the hooks for remember.el. This can be a string, or nil to mean
  1196. the value of `remember-data-file'.
  1197. You can set this on a per-template basis with the variable
  1198. `org-remember-templates'."
  1199. :group 'org-refile
  1200. :group 'org-remember
  1201. :type '(choice
  1202. (const :tag "Default from remember-data-file" nil)
  1203. file))
  1204. (defcustom org-goto-interface 'outline
  1205. "The default interface to be used for `org-goto'.
  1206. Allowed values are:
  1207. outline The interface shows an outline of the relevant file
  1208. and the correct heading is found by moving through
  1209. the outline or by searching with incremental search.
  1210. outline-path-completion Headlines in the current buffer are offered via
  1211. completion. This is the interface also used by
  1212. the refile command."
  1213. :group 'org-refile
  1214. :type '(choice
  1215. (const :tag "Outline" outline)
  1216. (const :tag "Outline-path-completion" outline-path-completion)))
  1217. (defcustom org-goto-max-level 5
  1218. "Maximum level to be considered when running org-goto with refile interface."
  1219. :group 'org-refile
  1220. :type 'number)
  1221. (defcustom org-reverse-note-order nil
  1222. "Non-nil means, store new notes at the beginning of a file or entry.
  1223. When nil, new notes will be filed to the end of a file or entry.
  1224. This can also be a list with cons cells of regular expressions that
  1225. are matched against file names, and values."
  1226. :group 'org-remember
  1227. :group 'org-refile
  1228. :type '(choice
  1229. (const :tag "Reverse always" t)
  1230. (const :tag "Reverse never" nil)
  1231. (repeat :tag "By file name regexp"
  1232. (cons regexp boolean))))
  1233. (defcustom org-refile-targets nil
  1234. "Targets for refiling entries with \\[org-refile].
  1235. This is list of cons cells. Each cell contains:
  1236. - a specification of the files to be considered, either a list of files,
  1237. or a symbol whose function or variable value will be used to retrieve
  1238. a file name or a list of file names. If you use `org-agenda-files' for
  1239. that, all agenda files will be scanned for targets. Nil means, consider
  1240. headings in the current buffer.
  1241. - A specification of how to select find candidate refile targets. This
  1242. may be any of
  1243. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1244. This tag has to be present in all target headlines, inheritance will
  1245. not be considered.
  1246. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1247. todo keyword.
  1248. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1249. headlines that are refiling targets.
  1250. - a cons cell (:level . N). Any headline of level N is considered a target.
  1251. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1252. When this variable is nil, all top-level headlines in the current buffer
  1253. are used, equivalent to the value `((nil . (:level . 1))'."
  1254. :group 'org-refile
  1255. :type '(repeat
  1256. (cons
  1257. (choice :value org-agenda-files
  1258. (const :tag "All agenda files" org-agenda-files)
  1259. (const :tag "Current buffer" nil)
  1260. (function) (variable) (file))
  1261. (choice :tag "Identify target headline by"
  1262. (cons :tag "Specific tag" (const :value :tag) (string))
  1263. (cons :tag "TODO keyword" (const :value :todo) (string))
  1264. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1265. (cons :tag "Level number" (const :value :level) (integer))
  1266. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1267. (defcustom org-refile-use-outline-path nil
  1268. "Non-nil means, provide refile targets as paths.
  1269. So a level 3 headline will be available as level1/level2/level3.
  1270. When the value is `file', also include the file name (without directory)
  1271. into the path. When `full-file-path', include the full file path."
  1272. :group 'org-refile
  1273. :type '(choice
  1274. (const :tag "Not" nil)
  1275. (const :tag "Yes" t)
  1276. (const :tag "Start with file name" file)
  1277. (const :tag "Start with full file path" full-file-path)))
  1278. (defcustom org-outline-path-complete-in-steps t
  1279. "Non-nil means, complete the outline path in hierarchical steps.
  1280. When Org-mode uses the refile interface to select an outline path
  1281. \(see variable `org-refile-use-outline-path'), the completion of
  1282. the path can be done is a single go, or if can be done in steps down
  1283. the headline hierarchy. Going in steps is probably the best if you
  1284. do not use a special completion package like `ido' or `icicles'.
  1285. However, when using these packages, going in one step can be very
  1286. fast, while still showing the whole path to the entry."
  1287. :group 'org-refile
  1288. :type 'boolean)
  1289. (defgroup org-todo nil
  1290. "Options concerning TODO items in Org-mode."
  1291. :tag "Org TODO"
  1292. :group 'org)
  1293. (defgroup org-progress nil
  1294. "Options concerning Progress logging in Org-mode."
  1295. :tag "Org Progress"
  1296. :group 'org-time)
  1297. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1298. "List of TODO entry keyword sequences and their interpretation.
  1299. \\<org-mode-map>This is a list of sequences.
  1300. Each sequence starts with a symbol, either `sequence' or `type',
  1301. indicating if the keywords should be interpreted as a sequence of
  1302. action steps, or as different types of TODO items. The first
  1303. keywords are states requiring action - these states will select a headline
  1304. for inclusion into the global TODO list Org-mode produces. If one of
  1305. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1306. signify that no further action is necessary. If \"|\" is not found,
  1307. the last keyword is treated as the only DONE state of the sequence.
  1308. The command \\[org-todo] cycles an entry through these states, and one
  1309. additional state where no keyword is present. For details about this
  1310. cycling, see the manual.
  1311. TODO keywords and interpretation can also be set on a per-file basis with
  1312. the special #+SEQ_TODO and #+TYP_TODO lines.
  1313. Each keyword can optionally specify a character for fast state selection
  1314. \(in combination with the variable `org-use-fast-todo-selection')
  1315. and specifiers for state change logging, using the same syntax
  1316. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1317. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1318. indicates to record a time stamp each time this state is selected.
  1319. Each keyword may also specify if a timestamp or a note should be
  1320. recorded when entering or leaving the state, by adding additional
  1321. characters in the parenthesis after the keyword. This looks like this:
  1322. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1323. record only the time of the state change. With X and Y being either
  1324. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1325. Y when leaving the state if and only if the *target* state does not
  1326. define X. You may omit any of the fast-selection key or X or /Y,
  1327. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1328. For backward compatibility, this variable may also be just a list
  1329. of keywords - in this case the interpretation (sequence or type) will be
  1330. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1331. :group 'org-todo
  1332. :group 'org-keywords
  1333. :type '(choice
  1334. (repeat :tag "Old syntax, just keywords"
  1335. (string :tag "Keyword"))
  1336. (repeat :tag "New syntax"
  1337. (cons
  1338. (choice
  1339. :tag "Interpretation"
  1340. (const :tag "Sequence (cycling hits every state)" sequence)
  1341. (const :tag "Type (cycling directly to DONE)" type))
  1342. (repeat
  1343. (string :tag "Keyword"))))))
  1344. (defvar org-todo-keywords-1 nil
  1345. "All TODO and DONE keywords active in a buffer.")
  1346. (make-variable-buffer-local 'org-todo-keywords-1)
  1347. (defvar org-todo-keywords-for-agenda nil)
  1348. (defvar org-done-keywords-for-agenda nil)
  1349. (defvar org-todo-keyword-alist-for-agenda nil)
  1350. (defvar org-tag-alist-for-agenda nil)
  1351. (defvar org-agenda-contributing-files nil)
  1352. (defvar org-not-done-keywords nil)
  1353. (make-variable-buffer-local 'org-not-done-keywords)
  1354. (defvar org-done-keywords nil)
  1355. (make-variable-buffer-local 'org-done-keywords)
  1356. (defvar org-todo-heads nil)
  1357. (make-variable-buffer-local 'org-todo-heads)
  1358. (defvar org-todo-sets nil)
  1359. (make-variable-buffer-local 'org-todo-sets)
  1360. (defvar org-todo-log-states nil)
  1361. (make-variable-buffer-local 'org-todo-log-states)
  1362. (defvar org-todo-kwd-alist nil)
  1363. (make-variable-buffer-local 'org-todo-kwd-alist)
  1364. (defvar org-todo-key-alist nil)
  1365. (make-variable-buffer-local 'org-todo-key-alist)
  1366. (defvar org-todo-key-trigger nil)
  1367. (make-variable-buffer-local 'org-todo-key-trigger)
  1368. (defcustom org-todo-interpretation 'sequence
  1369. "Controls how TODO keywords are interpreted.
  1370. This variable is in principle obsolete and is only used for
  1371. backward compatibility, if the interpretation of todo keywords is
  1372. not given already in `org-todo-keywords'. See that variable for
  1373. more information."
  1374. :group 'org-todo
  1375. :group 'org-keywords
  1376. :type '(choice (const sequence)
  1377. (const type)))
  1378. (defcustom org-use-fast-todo-selection 'prefix
  1379. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1380. This variable describes if and under what circumstances the cycling
  1381. mechanism for TODO keywords will be replaced by a single-key, direct
  1382. selection scheme.
  1383. When nil, fast selection is never used.
  1384. When the symbol `prefix', it will be used when `org-todo' is called with
  1385. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1386. in an agenda buffer.
  1387. When t, fast selection is used by default. In this case, the prefix
  1388. argument forces cycling instead.
  1389. In all cases, the special interface is only used if access keys have actually
  1390. been assigned by the user, i.e. if keywords in the configuration are followed
  1391. by a letter in parenthesis, like TODO(t)."
  1392. :group 'org-todo
  1393. :type '(choice
  1394. (const :tag "Never" nil)
  1395. (const :tag "By default" t)
  1396. (const :tag "Only with C-u C-c C-t" prefix)))
  1397. (defcustom org-provide-todo-statistics t
  1398. "Non-nil means, update todo statistics after insert and toggle.
  1399. When this is set, todo statistics is updated in the parent of the current
  1400. entry each time a todo state is changed."
  1401. :group 'org-todo
  1402. :type 'boolean)
  1403. (defcustom org-after-todo-state-change-hook nil
  1404. "Hook which is run after the state of a TODO item was changed.
  1405. The new state (a string with a TODO keyword, or nil) is available in the
  1406. Lisp variable `state'."
  1407. :group 'org-todo
  1408. :type 'hook)
  1409. (defcustom org-todo-state-tags-triggers nil
  1410. "Tag changes that should be triggered by TODO state changes.
  1411. This is a list. Each entry is
  1412. (state-change (tag . flag) .......)
  1413. State-change can be a string with a state, and empty string to indicate the
  1414. state that has no TODO keyword, or it can be one of the symbols `todo'
  1415. or `done', meaning any not-done or done state, respectively."
  1416. :group 'org-todo
  1417. :group 'org-tags
  1418. :type '(repeat
  1419. (cons (choice :tag "When changing to"
  1420. (const :tag "Not-done state" todo)
  1421. (const :tag "Done state" done)
  1422. (string :tag "State"))
  1423. (repeat
  1424. (cons :tag "Tag action"
  1425. (string :tag "Tag")
  1426. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1427. (defcustom org-log-done nil
  1428. "Information to record when a task moves to the DONE state.
  1429. Possible values are:
  1430. nil Don't add anything, just change the keyword
  1431. time Add a time stamp to the task
  1432. note Prompt a closing note and add it with template `org-log-note-headings'
  1433. This option can also be set with on a per-file-basis with
  1434. #+STARTUP: nologdone
  1435. #+STARTUP: logdone
  1436. #+STARTUP: lognotedone
  1437. You can have local logging settings for a subtree by setting the LOGGING
  1438. property to one or more of these keywords."
  1439. :group 'org-todo
  1440. :group 'org-progress
  1441. :type '(choice
  1442. (const :tag "No logging" nil)
  1443. (const :tag "Record CLOSED timestamp" time)
  1444. (const :tag "Record CLOSED timestamp with closing note." note)))
  1445. ;; Normalize old uses of org-log-done.
  1446. (cond
  1447. ((eq org-log-done t) (setq org-log-done 'time))
  1448. ((and (listp org-log-done) (memq 'done org-log-done))
  1449. (setq org-log-done 'note)))
  1450. (defcustom org-log-note-clock-out nil
  1451. "Non-nil means, record a note when clocking out of an item.
  1452. This can also be configured on a per-file basis by adding one of
  1453. the following lines anywhere in the buffer:
  1454. #+STARTUP: lognoteclock-out
  1455. #+STARTUP: nolognoteclock-out"
  1456. :group 'org-todo
  1457. :group 'org-progress
  1458. :type 'boolean)
  1459. (defcustom org-log-done-with-time t
  1460. "Non-nil means, the CLOSED time stamp will contain date and time.
  1461. When nil, only the date will be recorded."
  1462. :group 'org-progress
  1463. :type 'boolean)
  1464. (defcustom org-log-note-headings
  1465. '((done . "CLOSING NOTE %t")
  1466. (state . "State %-12s %t")
  1467. (note . "Note taken on %t")
  1468. (clock-out . ""))
  1469. "Headings for notes added to entries.
  1470. The value is an alist, with the car being a symbol indicating the note
  1471. context, and the cdr is the heading to be used. The heading may also be the
  1472. empty string.
  1473. %t in the heading will be replaced by a time stamp.
  1474. %s will be replaced by the new TODO state, in double quotes.
  1475. %u will be replaced by the user name.
  1476. %U will be replaced by the full user name."
  1477. :group 'org-todo
  1478. :group 'org-progress
  1479. :type '(list :greedy t
  1480. (cons (const :tag "Heading when closing an item" done) string)
  1481. (cons (const :tag
  1482. "Heading when changing todo state (todo sequence only)"
  1483. state) string)
  1484. (cons (const :tag "Heading when just taking a note" note) string)
  1485. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1486. (unless (assq 'note org-log-note-headings)
  1487. (push '(note . "%t") org-log-note-headings))
  1488. (defcustom org-log-state-notes-insert-after-drawers nil
  1489. "Non-nil means, insert state change notes after any drawers in entry.
  1490. Only the drawers that *immediately* follow the headline and the
  1491. deadline/scheduled line are skipped.
  1492. When nil, insert notes right after the heading and perhaps the line
  1493. with deadline/scheduling if present."
  1494. :group 'org-todo
  1495. :group 'org-progress
  1496. :type 'boolean)
  1497. (defcustom org-log-states-order-reversed t
  1498. "Non-nil means, the latest state change note will be directly after heading.
  1499. When nil, the notes will be orderer according to time."
  1500. :group 'org-todo
  1501. :group 'org-progress
  1502. :type 'boolean)
  1503. (defcustom org-log-repeat 'time
  1504. "Non-nil means, record moving through the DONE state when triggering repeat.
  1505. An auto-repeating tasks is immediately switched back to TODO when marked
  1506. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1507. the TODO keyword definition, or recording a closing note by setting
  1508. `org-log-done', there will be no record of the task moving through DONE.
  1509. This variable forces taking a note anyway. Possible values are:
  1510. nil Don't force a record
  1511. time Record a time stamp
  1512. note Record a note
  1513. This option can also be set with on a per-file-basis with
  1514. #+STARTUP: logrepeat
  1515. #+STARTUP: lognoterepeat
  1516. #+STARTUP: nologrepeat
  1517. You can have local logging settings for a subtree by setting the LOGGING
  1518. property to one or more of these keywords."
  1519. :group 'org-todo
  1520. :group 'org-progress
  1521. :type '(choice
  1522. (const :tag "Don't force a record" nil)
  1523. (const :tag "Force recording the DONE state" time)
  1524. (const :tag "Force recording a note with the DONE state" note)))
  1525. (defgroup org-priorities nil
  1526. "Priorities in Org-mode."
  1527. :tag "Org Priorities"
  1528. :group 'org-todo)
  1529. (defcustom org-highest-priority ?A
  1530. "The highest priority of TODO items. A character like ?A, ?B etc.
  1531. Must have a smaller ASCII number than `org-lowest-priority'."
  1532. :group 'org-priorities
  1533. :type 'character)
  1534. (defcustom org-lowest-priority ?C
  1535. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1536. Must have a larger ASCII number than `org-highest-priority'."
  1537. :group 'org-priorities
  1538. :type 'character)
  1539. (defcustom org-default-priority ?B
  1540. "The default priority of TODO items.
  1541. This is the priority an item get if no explicit priority is given."
  1542. :group 'org-priorities
  1543. :type 'character)
  1544. (defcustom org-priority-start-cycle-with-default t
  1545. "Non-nil means, start with default priority when starting to cycle.
  1546. When this is nil, the first step in the cycle will be (depending on the
  1547. command used) one higher or lower that the default priority."
  1548. :group 'org-priorities
  1549. :type 'boolean)
  1550. (defgroup org-time nil
  1551. "Options concerning time stamps and deadlines in Org-mode."
  1552. :tag "Org Time"
  1553. :group 'org)
  1554. (defcustom org-insert-labeled-timestamps-at-point nil
  1555. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1556. When nil, these labeled time stamps are forces into the second line of an
  1557. entry, just after the headline. When scheduling from the global TODO list,
  1558. the time stamp will always be forced into the second line."
  1559. :group 'org-time
  1560. :type 'boolean)
  1561. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1562. "Formats for `format-time-string' which are used for time stamps.
  1563. It is not recommended to change this constant.")
  1564. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1565. "Number of minutes to round time stamps to.
  1566. These are two values, the first applies when first creating a time stamp.
  1567. The second applies when changing it with the commands `S-up' and `S-down'.
  1568. When changing the time stamp, this means that it will change in steps
  1569. of N minutes, as given by the second value.
  1570. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1571. numbers should be factors of 60, so for example 5, 10, 15.
  1572. When this is larger than 1, you can still force an exact time-stamp by using
  1573. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1574. and by using a prefix arg to `S-up/down' to specify the exact number
  1575. of minutes to shift."
  1576. :group 'org-time
  1577. :get '(lambda (var) ; Make sure all entries have 5 elements
  1578. (if (integerp (default-value var))
  1579. (list (default-value var) 5)
  1580. (default-value var)))
  1581. :type '(list
  1582. (integer :tag "when inserting times")
  1583. (integer :tag "when modifying times")))
  1584. ;; Normalize old customizations of this variable.
  1585. (when (integerp org-time-stamp-rounding-minutes)
  1586. (setq org-time-stamp-rounding-minutes
  1587. (list org-time-stamp-rounding-minutes
  1588. org-time-stamp-rounding-minutes)))
  1589. (defcustom org-display-custom-times nil
  1590. "Non-nil means, overlay custom formats over all time stamps.
  1591. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1592. To turn this on on a per-file basis, insert anywhere in the file:
  1593. #+STARTUP: customtime"
  1594. :group 'org-time
  1595. :set 'set-default
  1596. :type 'sexp)
  1597. (make-variable-buffer-local 'org-display-custom-times)
  1598. (defcustom org-time-stamp-custom-formats
  1599. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1600. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1601. These are overlayed over the default ISO format if the variable
  1602. `org-display-custom-times' is set. Time like %H:%M should be at the
  1603. end of the second format."
  1604. :group 'org-time
  1605. :type 'sexp)
  1606. (defun org-time-stamp-format (&optional long inactive)
  1607. "Get the right format for a time string."
  1608. (let ((f (if long (cdr org-time-stamp-formats)
  1609. (car org-time-stamp-formats))))
  1610. (if inactive
  1611. (concat "[" (substring f 1 -1) "]")
  1612. f)))
  1613. (defcustom org-time-clocksum-format "%d:%02d"
  1614. "The format string used when creating CLOCKSUM lines, or when
  1615. org-mode generates a time duration."
  1616. :group 'org-time
  1617. :type 'string)
  1618. (defcustom org-deadline-warning-days 14
  1619. "No. of days before expiration during which a deadline becomes active.
  1620. This variable governs the display in sparse trees and in the agenda.
  1621. When 0 or negative, it means use this number (the absolute value of it)
  1622. even if a deadline has a different individual lead time specified."
  1623. :group 'org-time
  1624. :group 'org-agenda-daily/weekly
  1625. :type 'number)
  1626. (defcustom org-read-date-prefer-future t
  1627. "Non-nil means, assume future for incomplete date input from user.
  1628. This affects the following situations:
  1629. 1. The user gives a day, but no month.
  1630. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1631. read this as the third of *next* month. However, if you enter \"17\",
  1632. it will be considered as *this* month.
  1633. 2. The user gives a month but not a year.
  1634. For example, if it is april and you enter \"feb 2\", this will be read
  1635. as feb 2, *next* year. \"May 5\", however, will be this year.
  1636. Currently this does not work for ISO week specifications.
  1637. When this option is nil, the current month and year will always be used
  1638. as defaults."
  1639. :group 'org-time
  1640. :type 'boolean)
  1641. (defcustom org-read-date-display-live t
  1642. "Non-nil means, display current interpretation of date prompt live.
  1643. This display will be in an overlay, in the minibuffer."
  1644. :group 'org-time
  1645. :type 'boolean)
  1646. (defcustom org-read-date-popup-calendar t
  1647. "Non-nil means, pop up a calendar when prompting for a date.
  1648. In the calendar, the date can be selected with mouse-1. However, the
  1649. minibuffer will also be active, and you can simply enter the date as well.
  1650. When nil, only the minibuffer will be available."
  1651. :group 'org-time
  1652. :type 'boolean)
  1653. (if (fboundp 'defvaralias)
  1654. (defvaralias 'org-popup-calendar-for-date-prompt
  1655. 'org-read-date-popup-calendar))
  1656. (defcustom org-extend-today-until 0
  1657. "The hour when your day really ends. Must be an integer.
  1658. This has influence for the following applications:
  1659. - When switching the agenda to \"today\". It it is still earlier than
  1660. the time given here, the day recognized as TODAY is actually yesterday.
  1661. - When a date is read from the user and it is still before the time given
  1662. here, the current date and time will be assumed to be yesterday, 23:59.
  1663. Also, timestamps inserted in remember templates follow this rule.
  1664. IMPORTANT: This is a feature whose implementation is and likely will
  1665. remain incomplete. Really, it is only here because past midnight seems to
  1666. be the favorite working time of John Wiegley :-)"
  1667. :group 'org-time
  1668. :type 'number)
  1669. (defcustom org-edit-timestamp-down-means-later nil
  1670. "Non-nil means, S-down will increase the time in a time stamp.
  1671. When nil, S-up will increase."
  1672. :group 'org-time
  1673. :type 'boolean)
  1674. (defcustom org-calendar-follow-timestamp-change t
  1675. "Non-nil means, make the calendar window follow timestamp changes.
  1676. When a timestamp is modified and the calendar window is visible, it will be
  1677. moved to the new date."
  1678. :group 'org-time
  1679. :type 'boolean)
  1680. (defgroup org-tags nil
  1681. "Options concerning tags in Org-mode."
  1682. :tag "Org Tags"
  1683. :group 'org)
  1684. (defcustom org-tag-alist nil
  1685. "List of tags allowed in Org-mode files.
  1686. When this list is nil, Org-mode will base TAG input on what is already in the
  1687. buffer.
  1688. The value of this variable is an alist, the car of each entry must be a
  1689. keyword as a string, the cdr may be a character that is used to select
  1690. that tag through the fast-tag-selection interface.
  1691. See the manual for details."
  1692. :group 'org-tags
  1693. :type '(repeat
  1694. (choice
  1695. (cons (string :tag "Tag name")
  1696. (character :tag "Access char"))
  1697. (const :tag "Start radio group" (:startgroup))
  1698. (const :tag "End radio group" (:endgroup)))))
  1699. (defvar org-file-tags nil
  1700. "List of tags that can be inherited by all entries in the file.
  1701. The tags will be inherited if the variable `org-use-tag-inheritance'
  1702. says they should be.
  1703. This variable is populated from #+TAG lines.")
  1704. (defcustom org-use-fast-tag-selection 'auto
  1705. "Non-nil means, use fast tag selection scheme.
  1706. This is a special interface to select and deselect tags with single keys.
  1707. When nil, fast selection is never used.
  1708. When the symbol `auto', fast selection is used if and only if selection
  1709. characters for tags have been configured, either through the variable
  1710. `org-tag-alist' or through a #+TAGS line in the buffer.
  1711. When t, fast selection is always used and selection keys are assigned
  1712. automatically if necessary."
  1713. :group 'org-tags
  1714. :type '(choice
  1715. (const :tag "Always" t)
  1716. (const :tag "Never" nil)
  1717. (const :tag "When selection characters are configured" 'auto)))
  1718. (defcustom org-fast-tag-selection-single-key nil
  1719. "Non-nil means, fast tag selection exits after first change.
  1720. When nil, you have to press RET to exit it.
  1721. During fast tag selection, you can toggle this flag with `C-c'.
  1722. This variable can also have the value `expert'. In this case, the window
  1723. displaying the tags menu is not even shown, until you press C-c again."
  1724. :group 'org-tags
  1725. :type '(choice
  1726. (const :tag "No" nil)
  1727. (const :tag "Yes" t)
  1728. (const :tag "Expert" expert)))
  1729. (defvar org-fast-tag-selection-include-todo nil
  1730. "Non-nil means, fast tags selection interface will also offer TODO states.
  1731. This is an undocumented feature, you should not rely on it.")
  1732. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1733. "The column to which tags should be indented in a headline.
  1734. If this number is positive, it specifies the column. If it is negative,
  1735. it means that the tags should be flushright to that column. For example,
  1736. -80 works well for a normal 80 character screen."
  1737. :group 'org-tags
  1738. :type 'integer)
  1739. (defcustom org-auto-align-tags t
  1740. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1741. These operations change the length of a headline and therefore shift
  1742. the tags around. With this options turned on, after each such operation
  1743. the tags are again aligned to `org-tags-column'."
  1744. :group 'org-tags
  1745. :type 'boolean)
  1746. (defcustom org-use-tag-inheritance t
  1747. "Non-nil means, tags in levels apply also for sublevels.
  1748. When nil, only the tags directly given in a specific line apply there.
  1749. This may also be a list of tags that should be inherited, or a regexp that
  1750. matches tags that should be inherited. Additional control is possible
  1751. with the variable `org-tags-exclude-from-inheritance' which gives an
  1752. explicit list of tags to be excluded from inheritance., even if the value of
  1753. `org-use-tag-inheritance' would select it for inheritance.
  1754. If this option is t, a match early-on in a tree can lead to a large
  1755. number of matches in the subtree when constructing the agenda or creating
  1756. a sparse tree. If you only want to see the first match in a tree during
  1757. a search, check out the variable `org-tags-match-list-sublevels'."
  1758. :group 'org-tags
  1759. :type '(choice
  1760. (const :tag "Not" nil)
  1761. (const :tag "Always" t)
  1762. (repeat :tag "Specific tags" (string :tag "Tag"))
  1763. (regexp :tag "Tags matched by regexp")))
  1764. (defcustom org-tags-exclude-from-inheritance nil
  1765. "List of tags that should never be inherited.
  1766. This is a way to exclude a few tags from inheritance. For way to do
  1767. the opposite, to actively allow inheritance for selected tags,
  1768. see the variable `org-use-tag-inheritance'."
  1769. :group 'org-tags
  1770. :type '(repeat (string :tag "Tag")))
  1771. (defun org-tag-inherit-p (tag)
  1772. "Check if TAG is one that should be inherited."
  1773. (cond
  1774. ((member tag org-tags-exclude-from-inheritance) nil)
  1775. ((eq org-use-tag-inheritance t) t)
  1776. ((not org-use-tag-inheritance) nil)
  1777. ((stringp org-use-tag-inheritance)
  1778. (string-match org-use-tag-inheritance tag))
  1779. ((listp org-use-tag-inheritance)
  1780. (member tag org-use-tag-inheritance))
  1781. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1782. (defcustom org-tags-match-list-sublevels t
  1783. "Non-nil means list also sublevels of headlines matching tag search.
  1784. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1785. the sublevels of a headline matching a tag search often also match
  1786. the same search. Listing all of them can create very long lists.
  1787. Setting this variable to nil causes subtrees of a match to be skipped.
  1788. This option is off by default, because inheritance in on. If you turn
  1789. inheritance off, you very likely want to turn this option on.
  1790. As a special case, if the tag search is restricted to TODO items, the
  1791. value of this variable is ignored and sublevels are always checked, to
  1792. make sure all corresponding TODO items find their way into the list.
  1793. This variable is semi-obsolete and probably should always be true. It
  1794. is better to limit inheritance to certain tags using the variables
  1795. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1796. :group 'org-tags
  1797. :type 'boolean)
  1798. (defvar org-tags-history nil
  1799. "History of minibuffer reads for tags.")
  1800. (defvar org-last-tags-completion-table nil
  1801. "The last used completion table for tags.")
  1802. (defvar org-after-tags-change-hook nil
  1803. "Hook that is run after the tags in a line have changed.")
  1804. (defgroup org-properties nil
  1805. "Options concerning properties in Org-mode."
  1806. :tag "Org Properties"
  1807. :group 'org)
  1808. (defcustom org-property-format "%-10s %s"
  1809. "How property key/value pairs should be formatted by `indent-line'.
  1810. When `indent-line' hits a property definition, it will format the line
  1811. according to this format, mainly to make sure that the values are
  1812. lined-up with respect to each other."
  1813. :group 'org-properties
  1814. :type 'string)
  1815. (defcustom org-use-property-inheritance nil
  1816. "Non-nil means, properties apply also for sublevels.
  1817. This setting is chiefly used during property searches. Turning it on can
  1818. cause significant overhead when doing a search, which is why it is not
  1819. on by default.
  1820. When nil, only the properties directly given in the current entry count.
  1821. When t, every property is inherited. The value may also be a list of
  1822. properties that should have inheritance, or a regular expression matching
  1823. properties that should be inherited.
  1824. However, note that some special properties use inheritance under special
  1825. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1826. and the properties ending in \"_ALL\" when they are used as descriptor
  1827. for valid values of a property.
  1828. Note for programmers:
  1829. When querying an entry with `org-entry-get', you can control if inheritance
  1830. should be used. By default, `org-entry-get' looks only at the local
  1831. properties. You can request inheritance by setting the inherit argument
  1832. to t (to force inheritance) or to `selective' (to respect the setting
  1833. in this variable)."
  1834. :group 'org-properties
  1835. :type '(choice
  1836. (const :tag "Not" nil)
  1837. (const :tag "Always" t)
  1838. (repeat :tag "Specific properties" (string :tag "Property"))
  1839. (regexp :tag "Properties matched by regexp")))
  1840. (defun org-property-inherit-p (property)
  1841. "Check if PROPERTY is one that should be inherited."
  1842. (cond
  1843. ((eq org-use-property-inheritance t) t)
  1844. ((not org-use-property-inheritance) nil)
  1845. ((stringp org-use-property-inheritance)
  1846. (string-match org-use-property-inheritance property))
  1847. ((listp org-use-property-inheritance)
  1848. (member property org-use-property-inheritance))
  1849. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1850. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1851. "The default column format, if no other format has been defined.
  1852. This variable can be set on the per-file basis by inserting a line
  1853. #+COLUMNS: %25ITEM ....."
  1854. :group 'org-properties
  1855. :type 'string)
  1856. (defcustom org-columns-ellipses ".."
  1857. "The ellipses to be used when a field in column view is truncated.
  1858. When this is the empty string, as many characters as possible are shown,
  1859. but then there will be no visual indication that the field has been truncated.
  1860. When this is a string of length N, the last N characters of a truncated
  1861. field are replaced by this string. If the column is narrower than the
  1862. ellipses string, only part of the ellipses string will be shown."
  1863. :group 'org-properties
  1864. :type 'string)
  1865. (defcustom org-columns-modify-value-for-display-function nil
  1866. "Function that modifies values for display in column view.
  1867. For example, it can be used to cut out a certain part from a time stamp.
  1868. The function must take 2 arguments:
  1869. column-title The title of the column (*not* the property name)
  1870. value The value that should be modified.
  1871. The function should return the value that should be displayed,
  1872. or nil if the normal value should be used."
  1873. :group 'org-properties
  1874. :type 'function)
  1875. (defcustom org-effort-property "Effort"
  1876. "The property that is being used to keep track of effort estimates.
  1877. Effort estimates given in this property need to have the format H:MM."
  1878. :group 'org-properties
  1879. :group 'org-progress
  1880. :type '(string :tag "Property"))
  1881. (defconst org-global-properties-fixed
  1882. '(("VISIBILITY_ALL" . "folded children content all"))
  1883. "List of property/value pairs that can be inherited by any entry.
  1884. These are fixed values, for the preset properties.")
  1885. (defcustom org-global-properties nil
  1886. "List of property/value pairs that can be inherited by any entry.
  1887. You can set buffer-local values for the same purpose in the variable
  1888. `org-file-properties' this by adding lines like
  1889. #+PROPERTY: NAME VALUE"
  1890. :group 'org-properties
  1891. :type '(repeat
  1892. (cons (string :tag "Property")
  1893. (string :tag "Value"))))
  1894. (defvar org-file-properties nil
  1895. "List of property/value pairs that can be inherited by any entry.
  1896. Valid for the current buffer.
  1897. This variable is populated from #+PROPERTY lines.")
  1898. (make-variable-buffer-local 'org-file-properties)
  1899. (defgroup org-agenda nil
  1900. "Options concerning agenda views in Org-mode."
  1901. :tag "Org Agenda"
  1902. :group 'org)
  1903. (defvar org-category nil
  1904. "Variable used by org files to set a category for agenda display.
  1905. Such files should use a file variable to set it, for example
  1906. # -*- mode: org; org-category: \"ELisp\"
  1907. or contain a special line
  1908. #+CATEGORY: ELisp
  1909. If the file does not specify a category, then file's base name
  1910. is used instead.")
  1911. (make-variable-buffer-local 'org-category)
  1912. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1913. (defcustom org-agenda-files nil
  1914. "The files to be used for agenda display.
  1915. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1916. \\[org-remove-file]. You can also use customize to edit the list.
  1917. If an entry is a directory, all files in that directory that are matched by
  1918. `org-agenda-file-regexp' will be part of the file list.
  1919. If the value of the variable is not a list but a single file name, then
  1920. the list of agenda files is actually stored and maintained in that file, one
  1921. agenda file per line."
  1922. :group 'org-agenda
  1923. :type '(choice
  1924. (repeat :tag "List of files and directories" file)
  1925. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1926. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1927. "Regular expression to match files for `org-agenda-files'.
  1928. If any element in the list in that variable contains a directory instead
  1929. of a normal file, all files in that directory that are matched by this
  1930. regular expression will be included."
  1931. :group 'org-agenda
  1932. :type 'regexp)
  1933. (defcustom org-agenda-text-search-extra-files nil
  1934. "List of extra files to be searched by text search commands.
  1935. These files will be search in addition to the agenda files by the
  1936. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1937. Note that these files will only be searched for text search commands,
  1938. not for the other agenda views like todo lists, tag searches or the weekly
  1939. agenda. This variable is intended to list notes and possibly archive files
  1940. that should also be searched by these two commands.
  1941. In fact, if the first element in the list is the symbol `agenda-archives',
  1942. than all archive files of all agenda files will be added to the search
  1943. scope."
  1944. :group 'org-agenda
  1945. :type '(set :greedy t
  1946. (const :tag "Agenda Archives" agenda-archives)
  1947. (repeat :inline t (file))))
  1948. (if (fboundp 'defvaralias)
  1949. (defvaralias 'org-agenda-multi-occur-extra-files
  1950. 'org-agenda-text-search-extra-files))
  1951. (defcustom org-agenda-skip-unavailable-files nil
  1952. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1953. A nil value means to remove them, after a query, from the list."
  1954. :group 'org-agenda
  1955. :type 'boolean)
  1956. (defcustom org-calendar-to-agenda-key [?c]
  1957. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1958. The command `org-calendar-goto-agenda' will be bound to this key. The
  1959. default is the character `c' because then `c' can be used to switch back and
  1960. forth between agenda and calendar."
  1961. :group 'org-agenda
  1962. :type 'sexp)
  1963. (defcustom org-calendar-agenda-action-key [?k]
  1964. "The key to be installed in `calendar-mode-map' for agenda-action.
  1965. The command `org-agenda-action' will be bound to this key. The
  1966. default is the character `k' because we use the same key in the agenda."
  1967. :group 'org-agenda
  1968. :type 'sexp)
  1969. (eval-after-load "calendar"
  1970. '(progn
  1971. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1972. 'org-calendar-goto-agenda)
  1973. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1974. 'org-agenda-action)))
  1975. (defgroup org-latex nil
  1976. "Options for embedding LaTeX code into Org-mode."
  1977. :tag "Org LaTeX"
  1978. :group 'org)
  1979. (defcustom org-format-latex-options
  1980. '(:foreground default :background default :scale 1.0
  1981. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1982. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  1983. "Options for creating images from LaTeX fragments.
  1984. This is a property list with the following properties:
  1985. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1986. `default' means use the foreground of the default face.
  1987. :background the background color, or \"Transparent\".
  1988. `default' means use the background of the default face.
  1989. :scale a scaling factor for the size of the images.
  1990. :html-foreground, :html-background, :html-scale
  1991. the same numbers for HTML export.
  1992. :matchers a list indicating which matchers should be used to
  1993. find LaTeX fragments. Valid members of this list are:
  1994. \"begin\" find environments
  1995. \"$1\" find single characters surrounded by $.$
  1996. \"$\" find math expressions surrounded by $...$
  1997. \"$$\" find math expressions surrounded by $$....$$
  1998. \"\\(\" find math expressions surrounded by \\(...\\)
  1999. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2000. :group 'org-latex
  2001. :type 'plist)
  2002. (defcustom org-format-latex-header "\\documentclass{article}
  2003. \\usepackage{fullpage} % do not remove
  2004. \\usepackage{amssymb}
  2005. \\usepackage[usenames]{color}
  2006. \\usepackage{amsmath}
  2007. \\usepackage{latexsym}
  2008. \\usepackage[mathscr]{eucal}
  2009. \\pagestyle{empty} % do not remove"
  2010. "The document header used for processing LaTeX fragments."
  2011. :group 'org-latex
  2012. :type 'string)
  2013. (defgroup org-font-lock nil
  2014. "Font-lock settings for highlighting in Org-mode."
  2015. :tag "Org Font Lock"
  2016. :group 'org)
  2017. (defcustom org-level-color-stars-only nil
  2018. "Non-nil means fontify only the stars in each headline.
  2019. When nil, the entire headline is fontified.
  2020. Changing it requires restart of `font-lock-mode' to become effective
  2021. also in regions already fontified."
  2022. :group 'org-font-lock
  2023. :type 'boolean)
  2024. (defcustom org-hide-leading-stars nil
  2025. "Non-nil means, hide the first N-1 stars in a headline.
  2026. This works by using the face `org-hide' for these stars. This
  2027. face is white for a light background, and black for a dark
  2028. background. You may have to customize the face `org-hide' to
  2029. make this work.
  2030. Changing it requires restart of `font-lock-mode' to become effective
  2031. also in regions already fontified.
  2032. You may also set this on a per-file basis by adding one of the following
  2033. lines to the buffer:
  2034. #+STARTUP: hidestars
  2035. #+STARTUP: showstars"
  2036. :group 'org-font-lock
  2037. :type 'boolean)
  2038. (defcustom org-fontify-done-headline nil
  2039. "Non-nil means, change the face of a headline if it is marked DONE.
  2040. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2041. When this is non-nil, the headline after the keyword is set to the
  2042. `org-headline-done' as an additional indication."
  2043. :group 'org-font-lock
  2044. :type 'boolean)
  2045. (defcustom org-fontify-emphasized-text t
  2046. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2047. Changing this variable requires a restart of Emacs to take effect."
  2048. :group 'org-font-lock
  2049. :type 'boolean)
  2050. (defcustom org-highlight-latex-fragments-and-specials nil
  2051. "Non-nil means, fontify what is treated specially by the exporters."
  2052. :group 'org-font-lock
  2053. :type 'boolean)
  2054. (defcustom org-hide-emphasis-markers nil
  2055. "Non-nil mean font-lock should hide the emphasis marker characters."
  2056. :group 'org-font-lock
  2057. :type 'boolean)
  2058. (defvar org-emph-re nil
  2059. "Regular expression for matching emphasis.")
  2060. (defvar org-verbatim-re nil
  2061. "Regular expression for matching verbatim text.")
  2062. (defvar org-emphasis-regexp-components) ; defined just below
  2063. (defvar org-emphasis-alist) ; defined just below
  2064. (defun org-set-emph-re (var val)
  2065. "Set variable and compute the emphasis regular expression."
  2066. (set var val)
  2067. (when (and (boundp 'org-emphasis-alist)
  2068. (boundp 'org-emphasis-regexp-components)
  2069. org-emphasis-alist org-emphasis-regexp-components)
  2070. (let* ((e org-emphasis-regexp-components)
  2071. (pre (car e))
  2072. (post (nth 1 e))
  2073. (border (nth 2 e))
  2074. (body (nth 3 e))
  2075. (nl (nth 4 e))
  2076. (body1 (concat body "*?"))
  2077. (markers (mapconcat 'car org-emphasis-alist ""))
  2078. (vmarkers (mapconcat
  2079. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2080. org-emphasis-alist "")))
  2081. ;; make sure special characters appear at the right position in the class
  2082. (if (string-match "\\^" markers)
  2083. (setq markers (concat (replace-match "" t t markers) "^")))
  2084. (if (string-match "-" markers)
  2085. (setq markers (concat (replace-match "" t t markers) "-")))
  2086. (if (string-match "\\^" vmarkers)
  2087. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2088. (if (string-match "-" vmarkers)
  2089. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2090. (if (> nl 0)
  2091. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2092. (int-to-string nl) "\\}")))
  2093. ;; Make the regexp
  2094. (setq org-emph-re
  2095. (concat "\\([" pre "]\\|^\\)"
  2096. "\\("
  2097. "\\([" markers "]\\)"
  2098. "\\("
  2099. "[^" border "]\\|"
  2100. "[^" border "]"
  2101. body1
  2102. "[^" border "]"
  2103. "\\)"
  2104. "\\3\\)"
  2105. "\\([" post "]\\|$\\)"))
  2106. (setq org-verbatim-re
  2107. (concat "\\([" pre "]\\|^\\)"
  2108. "\\("
  2109. "\\([" vmarkers "]\\)"
  2110. "\\("
  2111. "[^" border "]\\|"
  2112. "[^" border "]"
  2113. body1
  2114. "[^" border "]"
  2115. "\\)"
  2116. "\\3\\)"
  2117. "\\([" post "]\\|$\\)")))))
  2118. (defcustom org-emphasis-regexp-components
  2119. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2120. "Components used to build the regular expression for emphasis.
  2121. This is a list with 6 entries. Terminology: In an emphasis string
  2122. like \" *strong word* \", we call the initial space PREMATCH, the final
  2123. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2124. and \"trong wor\" is the body. The different components in this variable
  2125. specify what is allowed/forbidden in each part:
  2126. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2127. post Chars allowed as postmatch. End of line will be allowed too.
  2128. border The chars *forbidden* as border characters.
  2129. body-regexp A regexp like \".\" to match a body character. Don't use
  2130. non-shy groups here, and don't allow newline here.
  2131. newline The maximum number of newlines allowed in an emphasis exp.
  2132. Use customize to modify this, or restart Emacs after changing it."
  2133. :group 'org-font-lock
  2134. :set 'org-set-emph-re
  2135. :type '(list
  2136. (sexp :tag "Allowed chars in pre ")
  2137. (sexp :tag "Allowed chars in post ")
  2138. (sexp :tag "Forbidden chars in border ")
  2139. (sexp :tag "Regexp for body ")
  2140. (integer :tag "number of newlines allowed")
  2141. (option (boolean :tag "Please ignore this button"))))
  2142. (defcustom org-emphasis-alist
  2143. `(("*" bold "<b>" "</b>")
  2144. ("/" italic "<i>" "</i>")
  2145. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2146. ("=" org-code "<code>" "</code>" verbatim)
  2147. ("~" org-verbatim "<code>" "</code>" verbatim)
  2148. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2149. "<del>" "</del>")
  2150. )
  2151. "Special syntax for emphasized text.
  2152. Text starting and ending with a special character will be emphasized, for
  2153. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2154. characters, the face to be used by font-lock for highlighting in Org-mode
  2155. Emacs buffers, and the HTML tags to be used for this.
  2156. Use customize to modify this, or restart Emacs after changing it."
  2157. :group 'org-font-lock
  2158. :set 'org-set-emph-re
  2159. :type '(repeat
  2160. (list
  2161. (string :tag "Marker character")
  2162. (choice
  2163. (face :tag "Font-lock-face")
  2164. (plist :tag "Face property list"))
  2165. (string :tag "HTML start tag")
  2166. (string :tag "HTML end tag")
  2167. (option (const verbatim)))))
  2168. ;;; Miscellaneous options
  2169. (defgroup org-completion nil
  2170. "Completion in Org-mode."
  2171. :tag "Org Completion"
  2172. :group 'org)
  2173. (defcustom org-completion-use-ido nil
  2174. "Non-nil means, use ido completion wherever possible.
  2175. Note that `ido-mode' must be active for this variable to be relevant.
  2176. If you decide to turn this variable on, you might well want to turn off
  2177. `org-outline-path-complete-in-steps'."
  2178. :group 'org-completion
  2179. :type 'boolean)
  2180. (defcustom org-completion-fallback-command 'hippie-expand
  2181. "The expansion command called by \\[org-complete] in normal context.
  2182. Normal means, no org-mode-specific context."
  2183. :group 'org-completion
  2184. :type 'function)
  2185. ;;; Functions and variables from ther packages
  2186. ;; Declared here to avoid compiler warnings
  2187. ;; XEmacs only
  2188. (defvar outline-mode-menu-heading)
  2189. (defvar outline-mode-menu-show)
  2190. (defvar outline-mode-menu-hide)
  2191. (defvar zmacs-regions) ; XEmacs regions
  2192. ;; Emacs only
  2193. (defvar mark-active)
  2194. ;; Various packages
  2195. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2196. (declare-function calendar-forward-day "cal-move" (arg))
  2197. (declare-function calendar-goto-date "cal-move" (date))
  2198. (declare-function calendar-goto-today "cal-move" ())
  2199. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2200. (defvar calc-embedded-close-formula)
  2201. (defvar calc-embedded-open-formula)
  2202. (declare-function cdlatex-tab "ext:cdlatex" ())
  2203. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2204. (defvar font-lock-unfontify-region-function)
  2205. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2206. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2207. (defvar iswitchb-temp-buflist)
  2208. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2209. (defvar org-agenda-tags-todo-honor-ignore-options)
  2210. (declare-function org-agenda-skip "org-agenda" ())
  2211. (declare-function org-format-agenda-item "org-agenda"
  2212. (extra txt &optional category tags dotime noprefix remove-re))
  2213. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2214. (declare-function org-agenda-change-all-lines "org-agenda"
  2215. (newhead hdmarker &optional fixface just-this))
  2216. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2217. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2218. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2219. (beg end))
  2220. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2221. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2222. "org-agenda" (&optional end))
  2223. (declare-function parse-time-string "parse-time" (string))
  2224. (declare-function remember "remember" (&optional initial))
  2225. (declare-function remember-buffer-desc "remember" ())
  2226. (declare-function remember-finalize "remember" ())
  2227. (defvar remember-save-after-remembering)
  2228. (defvar remember-data-file)
  2229. (defvar remember-register)
  2230. (defvar remember-buffer)
  2231. (defvar remember-handler-functions)
  2232. (defvar remember-annotation-functions)
  2233. (defvar texmathp-why)
  2234. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2235. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2236. (defvar w3m-current-url)
  2237. (defvar w3m-current-title)
  2238. (defvar org-latex-regexps)
  2239. ;;; Autoload and prepare some org modules
  2240. ;; Some table stuff that needs to be defined here, because it is used
  2241. ;; by the functions setting up org-mode or checking for table context.
  2242. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2243. "Detects an org-type or table-type table.")
  2244. (defconst org-table-line-regexp "^[ \t]*|"
  2245. "Detects an org-type table line.")
  2246. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2247. "Detects an org-type table line.")
  2248. (defconst org-table-hline-regexp "^[ \t]*|-"
  2249. "Detects an org-type table hline.")
  2250. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2251. "Detects a table-type table hline.")
  2252. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2253. "Searching from within a table (any type) this finds the first line
  2254. outside the table.")
  2255. ;; Autoload the functions in org-table.el that are needed by functions here.
  2256. (eval-and-compile
  2257. (org-autoload "org-table"
  2258. '(org-table-align org-table-begin org-table-blank-field
  2259. org-table-convert org-table-convert-region org-table-copy-down
  2260. org-table-copy-region org-table-create
  2261. org-table-create-or-convert-from-region
  2262. org-table-create-with-table.el org-table-current-dline
  2263. org-table-cut-region org-table-delete-column org-table-edit-field
  2264. org-table-edit-formulas org-table-end org-table-eval-formula
  2265. org-table-export org-table-field-info
  2266. org-table-get-stored-formulas org-table-goto-column
  2267. org-table-hline-and-move org-table-import org-table-insert-column
  2268. org-table-insert-hline org-table-insert-row org-table-iterate
  2269. org-table-justify-field-maybe org-table-kill-row
  2270. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2271. org-table-move-column org-table-move-column-left
  2272. org-table-move-column-right org-table-move-row
  2273. org-table-move-row-down org-table-move-row-up
  2274. org-table-next-field org-table-next-row org-table-paste-rectangle
  2275. org-table-previous-field org-table-recalculate
  2276. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2277. org-table-toggle-coordinate-overlays
  2278. org-table-toggle-formula-debugger org-table-wrap-region
  2279. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2280. (defun org-at-table-p (&optional table-type)
  2281. "Return t if the cursor is inside an org-type table.
  2282. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2283. (if org-enable-table-editor
  2284. (save-excursion
  2285. (beginning-of-line 1)
  2286. (looking-at (if table-type org-table-any-line-regexp
  2287. org-table-line-regexp)))
  2288. nil))
  2289. (defsubst org-table-p () (org-at-table-p))
  2290. (defun org-at-table.el-p ()
  2291. "Return t if and only if we are at a table.el table."
  2292. (and (org-at-table-p 'any)
  2293. (save-excursion
  2294. (goto-char (org-table-begin 'any))
  2295. (looking-at org-table1-hline-regexp))))
  2296. (defun org-table-recognize-table.el ()
  2297. "If there is a table.el table nearby, recognize it and move into it."
  2298. (if org-table-tab-recognizes-table.el
  2299. (if (org-at-table.el-p)
  2300. (progn
  2301. (beginning-of-line 1)
  2302. (if (looking-at org-table-dataline-regexp)
  2303. nil
  2304. (if (looking-at org-table1-hline-regexp)
  2305. (progn
  2306. (beginning-of-line 2)
  2307. (if (looking-at org-table-any-border-regexp)
  2308. (beginning-of-line -1)))))
  2309. (if (re-search-forward "|" (org-table-end t) t)
  2310. (progn
  2311. (require 'table)
  2312. (if (table--at-cell-p (point))
  2313. t
  2314. (message "recognizing table.el table...")
  2315. (table-recognize-table)
  2316. (message "recognizing table.el table...done")))
  2317. (error "This should not happen..."))
  2318. t)
  2319. nil)
  2320. nil))
  2321. (defun org-at-table-hline-p ()
  2322. "Return t if the cursor is inside a hline in a table."
  2323. (if org-enable-table-editor
  2324. (save-excursion
  2325. (beginning-of-line 1)
  2326. (looking-at org-table-hline-regexp))
  2327. nil))
  2328. (defvar org-table-clean-did-remove-column nil)
  2329. (defun org-table-map-tables (function)
  2330. "Apply FUNCTION to the start of all tables in the buffer."
  2331. (save-excursion
  2332. (save-restriction
  2333. (widen)
  2334. (goto-char (point-min))
  2335. (while (re-search-forward org-table-any-line-regexp nil t)
  2336. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2337. (beginning-of-line 1)
  2338. (if (looking-at org-table-line-regexp)
  2339. (save-excursion (funcall function)))
  2340. (re-search-forward org-table-any-border-regexp nil 1))))
  2341. (message "Mapping tables: done"))
  2342. ;; Declare and autoload functions from org-exp.el
  2343. (declare-function org-default-export-plist "org-exp")
  2344. (declare-function org-infile-export-plist "org-exp")
  2345. (declare-function org-get-current-options "org-exp")
  2346. (eval-and-compile
  2347. (org-autoload "org-exp"
  2348. '(org-export org-export-as-ascii org-export-visible
  2349. org-insert-export-options-template org-export-as-html-and-open
  2350. org-export-as-html-batch org-export-as-html-to-buffer
  2351. org-replace-region-by-html org-export-region-as-html
  2352. org-export-as-html org-export-icalendar-this-file
  2353. org-export-icalendar-all-agenda-files
  2354. org-table-clean-before-export
  2355. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2356. ;; Declare and autoload functions from org-agenda.el
  2357. (eval-and-compile
  2358. (org-autoload "org-agenda"
  2359. '(org-agenda org-agenda-list org-search-view
  2360. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2361. org-diary org-agenda-to-appt
  2362. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2363. ;; Autoload org-remember
  2364. (eval-and-compile
  2365. (org-autoload "org-remember"
  2366. '(org-remember-insinuate org-remember-annotation
  2367. org-remember-apply-template org-remember org-remember-handler)))
  2368. ;; Autoload org-clock.el
  2369. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2370. (beg end))
  2371. (declare-function org-clock-update-mode-line "org-clock" ())
  2372. (defvar org-clock-start-time)
  2373. (defvar org-clock-marker (make-marker)
  2374. "Marker recording the last clock-in.")
  2375. (eval-and-compile
  2376. (org-autoload
  2377. "org-clock"
  2378. '(org-clock-in org-clock-out org-clock-cancel
  2379. org-clock-goto org-clock-sum org-clock-display
  2380. org-clock-remove-overlays org-clock-report
  2381. org-clocktable-shift org-dblock-write:clocktable
  2382. org-get-clocktable)))
  2383. (defun org-clock-update-time-maybe ()
  2384. "If this is a CLOCK line, update it and return t.
  2385. Otherwise, return nil."
  2386. (interactive)
  2387. (save-excursion
  2388. (beginning-of-line 1)
  2389. (skip-chars-forward " \t")
  2390. (when (looking-at org-clock-string)
  2391. (let ((re (concat "[ \t]*" org-clock-string
  2392. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2393. "\\([ \t]*=>.*\\)?\\)?"))
  2394. ts te h m s neg)
  2395. (cond
  2396. ((not (looking-at re))
  2397. nil)
  2398. ((not (match-end 2))
  2399. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2400. (> org-clock-marker (point))
  2401. (<= org-clock-marker (point-at-eol)))
  2402. ;; The clock is running here
  2403. (setq org-clock-start-time
  2404. (apply 'encode-time
  2405. (org-parse-time-string (match-string 1))))
  2406. (org-clock-update-mode-line)))
  2407. (t
  2408. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2409. (end-of-line 1)
  2410. (setq ts (match-string 1)
  2411. te (match-string 3))
  2412. (setq s (- (time-to-seconds
  2413. (apply 'encode-time (org-parse-time-string te)))
  2414. (time-to-seconds
  2415. (apply 'encode-time (org-parse-time-string ts))))
  2416. neg (< s 0)
  2417. s (abs s)
  2418. h (floor (/ s 3600))
  2419. s (- s (* 3600 h))
  2420. m (floor (/ s 60))
  2421. s (- s (* 60 s)))
  2422. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2423. t))))))
  2424. (defun org-check-running-clock ()
  2425. "Check if the current buffer contains the running clock.
  2426. If yes, offer to stop it and to save the buffer with the changes."
  2427. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2428. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2429. (buffer-name))))
  2430. (org-clock-out)
  2431. (when (y-or-n-p "Save changed buffer?")
  2432. (save-buffer))))
  2433. (defun org-clocktable-try-shift (dir n)
  2434. "Check if this line starts a clock table, if yes, shift the time block."
  2435. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2436. (org-clocktable-shift dir n)))
  2437. ;; Autoload org-timer.el
  2438. ;(declare-function org-timer "org-timer")
  2439. (eval-and-compile
  2440. (org-autoload
  2441. "org-timer"
  2442. '(org-timer-start org-timer org-timer-item
  2443. org-timer-change-times-in-region)))
  2444. ;; Autoload archiving code
  2445. ;; The stuff that is needed for cycling and tags has to be defined here.
  2446. (defgroup org-archive nil
  2447. "Options concerning archiving in Org-mode."
  2448. :tag "Org Archive"
  2449. :group 'org-structure)
  2450. (defcustom org-archive-location "%s_archive::"
  2451. "The location where subtrees should be archived.
  2452. The value of this variable is a string, consisting of two parts,
  2453. separated by a double-colon. The first part is a filename and
  2454. the second part is a headline.
  2455. When the filename is omitted, archiving happens in the same file.
  2456. %s in the filename will be replaced by the current file
  2457. name (without the directory part). Archiving to a different file
  2458. is useful to keep archived entries from contributing to the
  2459. Org-mode Agenda.
  2460. The archived entries will be filed as subtrees of the specified
  2461. headline. When the headline is omitted, the subtrees are simply
  2462. filed away at the end of the file, as top-level entries. Also in
  2463. the heading you can use %s to represent the file name, this can be
  2464. useful when using the same archive for a number of different files.
  2465. Here are a few examples:
  2466. \"%s_archive::\"
  2467. If the current file is Projects.org, archive in file
  2468. Projects.org_archive, as top-level trees. This is the default.
  2469. \"::* Archived Tasks\"
  2470. Archive in the current file, under the top-level headline
  2471. \"* Archived Tasks\".
  2472. \"~/org/archive.org::\"
  2473. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2474. \"~/org/archive.org::From %s\"
  2475. Archive in file ~/org/archive.org (absolute path), und headlines
  2476. \"From FILENAME\" where file name is the current file name.
  2477. \"basement::** Finished Tasks\"
  2478. Archive in file ./basement (relative path), as level 3 trees
  2479. below the level 2 heading \"** Finished Tasks\".
  2480. You may set this option on a per-file basis by adding to the buffer a
  2481. line like
  2482. #+ARCHIVE: basement::** Finished Tasks
  2483. You may also define it locally for a subtree by setting an ARCHIVE property
  2484. in the entry. If such a property is found in an entry, or anywhere up
  2485. the hierarchy, it will be used."
  2486. :group 'org-archive
  2487. :type 'string)
  2488. (defcustom org-archive-tag "ARCHIVE"
  2489. "The tag that marks a subtree as archived.
  2490. An archived subtree does not open during visibility cycling, and does
  2491. not contribute to the agenda listings.
  2492. After changing this, font-lock must be restarted in the relevant buffers to
  2493. get the proper fontification."
  2494. :group 'org-archive
  2495. :group 'org-keywords
  2496. :type 'string)
  2497. (defcustom org-agenda-skip-archived-trees t
  2498. "Non-nil means, the agenda will skip any items located in archived trees.
  2499. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2500. variable is no longer recommended, you should leave it at the value t.
  2501. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2502. :group 'org-archive
  2503. :group 'org-agenda-skip
  2504. :type 'boolean)
  2505. (defcustom org-cycle-open-archived-trees nil
  2506. "Non-nil means, `org-cycle' will open archived trees.
  2507. An archived tree is a tree marked with the tag ARCHIVE.
  2508. When nil, archived trees will stay folded. You can still open them with
  2509. normal outline commands like `show-all', but not with the cycling commands."
  2510. :group 'org-archive
  2511. :group 'org-cycle
  2512. :type 'boolean)
  2513. (defcustom org-sparse-tree-open-archived-trees nil
  2514. "Non-nil means sparse tree construction shows matches in archived trees.
  2515. When nil, matches in these trees are highlighted, but the trees are kept in
  2516. collapsed state."
  2517. :group 'org-archive
  2518. :group 'org-sparse-trees
  2519. :type 'boolean)
  2520. (defun org-cycle-hide-archived-subtrees (state)
  2521. "Re-hide all archived subtrees after a visibility state change."
  2522. (when (and (not org-cycle-open-archived-trees)
  2523. (not (memq state '(overview folded))))
  2524. (save-excursion
  2525. (let* ((globalp (memq state '(contents all)))
  2526. (beg (if globalp (point-min) (point)))
  2527. (end (if globalp (point-max) (org-end-of-subtree t))))
  2528. (org-hide-archived-subtrees beg end)
  2529. (goto-char beg)
  2530. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2531. (message "%s" (substitute-command-keys
  2532. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2533. (defun org-force-cycle-archived ()
  2534. "Cycle subtree even if it is archived."
  2535. (interactive)
  2536. (setq this-command 'org-cycle)
  2537. (let ((org-cycle-open-archived-trees t))
  2538. (call-interactively 'org-cycle)))
  2539. (defun org-hide-archived-subtrees (beg end)
  2540. "Re-hide all archived subtrees after a visibility state change."
  2541. (save-excursion
  2542. (let* ((re (concat ":" org-archive-tag ":")))
  2543. (goto-char beg)
  2544. (while (re-search-forward re end t)
  2545. (and (org-on-heading-p) (hide-subtree))
  2546. (org-end-of-subtree t)))))
  2547. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2548. (eval-and-compile
  2549. (org-autoload "org-archive"
  2550. '(org-add-archive-files org-archive-subtree
  2551. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2552. ;; Autoload Column View Code
  2553. (declare-function org-columns-number-to-string "org-colview")
  2554. (declare-function org-columns-get-format-and-top-level "org-colview")
  2555. (declare-function org-columns-compute "org-colview")
  2556. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2557. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2558. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2559. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2560. ;; Autoload ID code
  2561. (declare-function org-id-store-link "org-id")
  2562. (org-autoload "org-id"
  2563. '(org-id-get-create org-id-new org-id-copy org-id-get
  2564. org-id-get-with-outline-path-completion
  2565. org-id-get-with-outline-drilling
  2566. org-id-goto org-id-find org-id-store-link))
  2567. ;;; Variables for pre-computed regular expressions, all buffer local
  2568. (defvar org-drawer-regexp nil
  2569. "Matches first line of a hidden block.")
  2570. (make-variable-buffer-local 'org-drawer-regexp)
  2571. (defvar org-todo-regexp nil
  2572. "Matches any of the TODO state keywords.")
  2573. (make-variable-buffer-local 'org-todo-regexp)
  2574. (defvar org-not-done-regexp nil
  2575. "Matches any of the TODO state keywords except the last one.")
  2576. (make-variable-buffer-local 'org-not-done-regexp)
  2577. (defvar org-todo-line-regexp nil
  2578. "Matches a headline and puts TODO state into group 2 if present.")
  2579. (make-variable-buffer-local 'org-todo-line-regexp)
  2580. (defvar org-complex-heading-regexp nil
  2581. "Matches a headline and puts everything into groups:
  2582. group 1: the stars
  2583. group 2: The todo keyword, maybe
  2584. group 3: Priority cookie
  2585. group 4: True headline
  2586. group 5: Tags")
  2587. (make-variable-buffer-local 'org-complex-heading-regexp)
  2588. (defvar org-todo-line-tags-regexp nil
  2589. "Matches a headline and puts TODO state into group 2 if present.
  2590. Also put tags into group 4 if tags are present.")
  2591. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2592. (defvar org-nl-done-regexp nil
  2593. "Matches newline followed by a headline with the DONE keyword.")
  2594. (make-variable-buffer-local 'org-nl-done-regexp)
  2595. (defvar org-looking-at-done-regexp nil
  2596. "Matches the DONE keyword a point.")
  2597. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2598. (defvar org-ds-keyword-length 12
  2599. "Maximum length of the Deadline and SCHEDULED keywords.")
  2600. (make-variable-buffer-local 'org-ds-keyword-length)
  2601. (defvar org-deadline-regexp nil
  2602. "Matches the DEADLINE keyword.")
  2603. (make-variable-buffer-local 'org-deadline-regexp)
  2604. (defvar org-deadline-time-regexp nil
  2605. "Matches the DEADLINE keyword together with a time stamp.")
  2606. (make-variable-buffer-local 'org-deadline-time-regexp)
  2607. (defvar org-deadline-line-regexp nil
  2608. "Matches the DEADLINE keyword and the rest of the line.")
  2609. (make-variable-buffer-local 'org-deadline-line-regexp)
  2610. (defvar org-scheduled-regexp nil
  2611. "Matches the SCHEDULED keyword.")
  2612. (make-variable-buffer-local 'org-scheduled-regexp)
  2613. (defvar org-scheduled-time-regexp nil
  2614. "Matches the SCHEDULED keyword together with a time stamp.")
  2615. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2616. (defvar org-closed-time-regexp nil
  2617. "Matches the CLOSED keyword together with a time stamp.")
  2618. (make-variable-buffer-local 'org-closed-time-regexp)
  2619. (defvar org-keyword-time-regexp nil
  2620. "Matches any of the 4 keywords, together with the time stamp.")
  2621. (make-variable-buffer-local 'org-keyword-time-regexp)
  2622. (defvar org-keyword-time-not-clock-regexp nil
  2623. "Matches any of the 3 keywords, together with the time stamp.")
  2624. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2625. (defvar org-maybe-keyword-time-regexp nil
  2626. "Matches a timestamp, possibly preceeded by a keyword.")
  2627. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2628. (defvar org-planning-or-clock-line-re nil
  2629. "Matches a line with planning or clock info.")
  2630. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2631. (defconst org-plain-time-of-day-regexp
  2632. (concat
  2633. "\\(\\<[012]?[0-9]"
  2634. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2635. "\\(--?"
  2636. "\\(\\<[012]?[0-9]"
  2637. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2638. "\\)?")
  2639. "Regular expression to match a plain time or time range.
  2640. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2641. groups carry important information:
  2642. 0 the full match
  2643. 1 the first time, range or not
  2644. 8 the second time, if it is a range.")
  2645. (defconst org-plain-time-extension-regexp
  2646. (concat
  2647. "\\(\\<[012]?[0-9]"
  2648. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2649. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2650. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2651. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2652. groups carry important information:
  2653. 0 the full match
  2654. 7 hours of duration
  2655. 9 minutes of duration")
  2656. (defconst org-stamp-time-of-day-regexp
  2657. (concat
  2658. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2659. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2660. "\\(--?"
  2661. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2662. "Regular expression to match a timestamp time or time range.
  2663. After a match, the following groups carry important information:
  2664. 0 the full match
  2665. 1 date plus weekday, for backreferencing to make sure both times on same day
  2666. 2 the first time, range or not
  2667. 4 the second time, if it is a range.")
  2668. (defconst org-startup-options
  2669. '(("fold" org-startup-folded t)
  2670. ("overview" org-startup-folded t)
  2671. ("nofold" org-startup-folded nil)
  2672. ("showall" org-startup-folded nil)
  2673. ("content" org-startup-folded content)
  2674. ("hidestars" org-hide-leading-stars t)
  2675. ("showstars" org-hide-leading-stars nil)
  2676. ("odd" org-odd-levels-only t)
  2677. ("oddeven" org-odd-levels-only nil)
  2678. ("align" org-startup-align-all-tables t)
  2679. ("noalign" org-startup-align-all-tables nil)
  2680. ("customtime" org-display-custom-times t)
  2681. ("logdone" org-log-done time)
  2682. ("lognotedone" org-log-done note)
  2683. ("nologdone" org-log-done nil)
  2684. ("lognoteclock-out" org-log-note-clock-out t)
  2685. ("nolognoteclock-out" org-log-note-clock-out nil)
  2686. ("logrepeat" org-log-repeat state)
  2687. ("lognoterepeat" org-log-repeat note)
  2688. ("nologrepeat" org-log-repeat nil)
  2689. ("fninline" org-footnote-define-inline t)
  2690. ("nofninline" org-footnote-define-inline nil)
  2691. ("fnlocal" org-footnote-section nil)
  2692. ("fnauto" org-footnote-auto-label t)
  2693. ("fnprompt" org-footnote-auto-label nil)
  2694. ("fnconfirm" org-footnote-auto-label confirm)
  2695. ("fnplain" org-footnote-auto-label plain)
  2696. ("constcgs" constants-unit-system cgs)
  2697. ("constSI" constants-unit-system SI))
  2698. "Variable associated with STARTUP options for org-mode.
  2699. Each element is a list of three items: The startup options as written
  2700. in the #+STARTUP line, the corresponding variable, and the value to
  2701. set this variable to if the option is found. An optional forth element PUSH
  2702. means to push this value onto the list in the variable.")
  2703. (defun org-set-regexps-and-options ()
  2704. "Precompute regular expressions for current buffer."
  2705. (when (org-mode-p)
  2706. (org-set-local 'org-todo-kwd-alist nil)
  2707. (org-set-local 'org-todo-key-alist nil)
  2708. (org-set-local 'org-todo-key-trigger nil)
  2709. (org-set-local 'org-todo-keywords-1 nil)
  2710. (org-set-local 'org-done-keywords nil)
  2711. (org-set-local 'org-todo-heads nil)
  2712. (org-set-local 'org-todo-sets nil)
  2713. (org-set-local 'org-todo-log-states nil)
  2714. (org-set-local 'org-file-properties nil)
  2715. (org-set-local 'org-file-tags nil)
  2716. (let ((re (org-make-options-regexp
  2717. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2718. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2719. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2720. (splitre "[ \t]+")
  2721. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2722. tail sep kws1 prio props ftags drawers
  2723. ext-setup-or-nil setup-contents (start 0))
  2724. (save-excursion
  2725. (save-restriction
  2726. (widen)
  2727. (goto-char (point-min))
  2728. (while (or (and ext-setup-or-nil
  2729. (string-match re ext-setup-or-nil start)
  2730. (setq start (match-end 0)))
  2731. (and (setq ext-setup-or-nil nil start 0)
  2732. (re-search-forward re nil t)))
  2733. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2734. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2735. (cond
  2736. ((equal key "CATEGORY")
  2737. (if (string-match "[ \t]+$" value)
  2738. (setq value (replace-match "" t t value)))
  2739. (setq cat value))
  2740. ((member key '("SEQ_TODO" "TODO"))
  2741. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2742. ((equal key "TYP_TODO")
  2743. (push (cons 'type (org-split-string value splitre)) kwds))
  2744. ((equal key "TAGS")
  2745. (setq tags (append tags (org-split-string value splitre))))
  2746. ((equal key "COLUMNS")
  2747. (org-set-local 'org-columns-default-format value))
  2748. ((equal key "LINK")
  2749. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2750. (push (cons (match-string 1 value)
  2751. (org-trim (match-string 2 value)))
  2752. links)))
  2753. ((equal key "PRIORITIES")
  2754. (setq prio (org-split-string value " +")))
  2755. ((equal key "PROPERTY")
  2756. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2757. (push (cons (match-string 1 value) (match-string 2 value))
  2758. props)))
  2759. ((equal key "FILETAGS")
  2760. (when (string-match "\\S-" value)
  2761. (setq ftags
  2762. (append
  2763. ftags
  2764. (apply 'append
  2765. (mapcar (lambda (x) (org-split-string x ":"))
  2766. (org-split-string value)))))))
  2767. ((equal key "DRAWERS")
  2768. (setq drawers (org-split-string value splitre)))
  2769. ((equal key "CONSTANTS")
  2770. (setq const (append const (org-split-string value splitre))))
  2771. ((equal key "STARTUP")
  2772. (let ((opts (org-split-string value splitre))
  2773. l var val)
  2774. (while (setq l (pop opts))
  2775. (when (setq l (assoc l org-startup-options))
  2776. (setq var (nth 1 l) val (nth 2 l))
  2777. (if (not (nth 3 l))
  2778. (set (make-local-variable var) val)
  2779. (if (not (listp (symbol-value var)))
  2780. (set (make-local-variable var) nil))
  2781. (set (make-local-variable var) (symbol-value var))
  2782. (add-to-list var val))))))
  2783. ((equal key "ARCHIVE")
  2784. (string-match " *$" value)
  2785. (setq arch (replace-match "" t t value))
  2786. (remove-text-properties 0 (length arch)
  2787. '(face t fontified t) arch))
  2788. ((equal key "SETUPFILE")
  2789. (setq setup-contents (org-file-contents
  2790. (expand-file-name
  2791. (org-remove-double-quotes value))
  2792. 'noerror))
  2793. (if (not ext-setup-or-nil)
  2794. (setq ext-setup-or-nil setup-contents start 0)
  2795. (setq ext-setup-or-nil
  2796. (concat (substring ext-setup-or-nil 0 start)
  2797. "\n" setup-contents "\n"
  2798. (substring ext-setup-or-nil start)))))
  2799. ))))
  2800. (when cat
  2801. (org-set-local 'org-category (intern cat))
  2802. (push (cons "CATEGORY" cat) props))
  2803. (when prio
  2804. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2805. (setq prio (mapcar 'string-to-char prio))
  2806. (org-set-local 'org-highest-priority (nth 0 prio))
  2807. (org-set-local 'org-lowest-priority (nth 1 prio))
  2808. (org-set-local 'org-default-priority (nth 2 prio)))
  2809. (and props (org-set-local 'org-file-properties (nreverse props)))
  2810. (and ftags (org-set-local 'org-file-tags ftags))
  2811. (and drawers (org-set-local 'org-drawers drawers))
  2812. (and arch (org-set-local 'org-archive-location arch))
  2813. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2814. ;; Process the TODO keywords
  2815. (unless kwds
  2816. ;; Use the global values as if they had been given locally.
  2817. (setq kwds (default-value 'org-todo-keywords))
  2818. (if (stringp (car kwds))
  2819. (setq kwds (list (cons org-todo-interpretation
  2820. (default-value 'org-todo-keywords)))))
  2821. (setq kwds (reverse kwds)))
  2822. (setq kwds (nreverse kwds))
  2823. (let (inter kws kw)
  2824. (while (setq kws (pop kwds))
  2825. (setq inter (pop kws) sep (member "|" kws)
  2826. kws0 (delete "|" (copy-sequence kws))
  2827. kwsa nil
  2828. kws1 (mapcar
  2829. (lambda (x)
  2830. ;; 1 2
  2831. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2832. (progn
  2833. (setq kw (match-string 1 x)
  2834. key (and (match-end 2) (match-string 2 x))
  2835. log (org-extract-log-state-settings x))
  2836. (push (cons kw (and key (string-to-char key))) kwsa)
  2837. (and log (push log org-todo-log-states))
  2838. kw)
  2839. (error "Invalid TODO keyword %s" x)))
  2840. kws0)
  2841. kwsa (if kwsa (append '((:startgroup))
  2842. (nreverse kwsa)
  2843. '((:endgroup))))
  2844. hw (car kws1)
  2845. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2846. tail (list inter hw (car dws) (org-last dws)))
  2847. (add-to-list 'org-todo-heads hw 'append)
  2848. (push kws1 org-todo-sets)
  2849. (setq org-done-keywords (append org-done-keywords dws nil))
  2850. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2851. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2852. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2853. (setq org-todo-sets (nreverse org-todo-sets)
  2854. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2855. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2856. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2857. ;; Process the constants
  2858. (when const
  2859. (let (e cst)
  2860. (while (setq e (pop const))
  2861. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2862. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2863. (setq org-table-formula-constants-local cst)))
  2864. ;; Process the tags.
  2865. (when tags
  2866. (let (e tgs)
  2867. (while (setq e (pop tags))
  2868. (cond
  2869. ((equal e "{") (push '(:startgroup) tgs))
  2870. ((equal e "}") (push '(:endgroup) tgs))
  2871. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2872. (push (cons (match-string 1 e)
  2873. (string-to-char (match-string 2 e)))
  2874. tgs))
  2875. (t (push (list e) tgs))))
  2876. (org-set-local 'org-tag-alist nil)
  2877. (while (setq e (pop tgs))
  2878. (or (and (stringp (car e))
  2879. (assoc (car e) org-tag-alist))
  2880. (push e org-tag-alist)))))
  2881. ;; Compute the regular expressions and other local variables
  2882. (if (not org-done-keywords)
  2883. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2884. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2885. (length org-scheduled-string)
  2886. (length org-clock-string)
  2887. (length org-closed-string)))
  2888. org-drawer-regexp
  2889. (concat "^[ \t]*:\\("
  2890. (mapconcat 'regexp-quote org-drawers "\\|")
  2891. "\\):[ \t]*$")
  2892. org-not-done-keywords
  2893. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2894. org-todo-regexp
  2895. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2896. "\\|") "\\)\\>")
  2897. org-not-done-regexp
  2898. (concat "\\<\\("
  2899. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2900. "\\)\\>")
  2901. org-todo-line-regexp
  2902. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2903. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2904. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2905. org-complex-heading-regexp
  2906. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2907. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2908. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2909. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2910. org-nl-done-regexp
  2911. (concat "\n\\*+[ \t]+"
  2912. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2913. "\\)" "\\>")
  2914. org-todo-line-tags-regexp
  2915. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2916. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2917. (org-re
  2918. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2919. org-looking-at-done-regexp
  2920. (concat "^" "\\(?:"
  2921. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2922. "\\>")
  2923. org-deadline-regexp (concat "\\<" org-deadline-string)
  2924. org-deadline-time-regexp
  2925. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2926. org-deadline-line-regexp
  2927. (concat "\\<\\(" org-deadline-string "\\).*")
  2928. org-scheduled-regexp
  2929. (concat "\\<" org-scheduled-string)
  2930. org-scheduled-time-regexp
  2931. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2932. org-closed-time-regexp
  2933. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2934. org-keyword-time-regexp
  2935. (concat "\\<\\(" org-scheduled-string
  2936. "\\|" org-deadline-string
  2937. "\\|" org-closed-string
  2938. "\\|" org-clock-string "\\)"
  2939. " *[[<]\\([^]>]+\\)[]>]")
  2940. org-keyword-time-not-clock-regexp
  2941. (concat "\\<\\(" org-scheduled-string
  2942. "\\|" org-deadline-string
  2943. "\\|" org-closed-string
  2944. "\\)"
  2945. " *[[<]\\([^]>]+\\)[]>]")
  2946. org-maybe-keyword-time-regexp
  2947. (concat "\\(\\<\\(" org-scheduled-string
  2948. "\\|" org-deadline-string
  2949. "\\|" org-closed-string
  2950. "\\|" org-clock-string "\\)\\)?"
  2951. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2952. org-planning-or-clock-line-re
  2953. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2954. "\\|" org-deadline-string
  2955. "\\|" org-closed-string "\\|" org-clock-string
  2956. "\\)\\>\\)")
  2957. )
  2958. (org-compute-latex-and-specials-regexp)
  2959. (org-set-font-lock-defaults))))
  2960. (defun org-file-contents (file &optional noerror)
  2961. "Return the contents of FILE, as a string."
  2962. (if (or (not file)
  2963. (not (file-readable-p file)))
  2964. (if noerror
  2965. (progn
  2966. (message "Cannot read file %s" file)
  2967. (ding) (sit-for 2)
  2968. "")
  2969. (error "Cannot read file %s" file))
  2970. (with-temp-buffer
  2971. (insert-file-contents file)
  2972. (buffer-string))))
  2973. (defun org-extract-log-state-settings (x)
  2974. "Extract the log state setting from a TODO keyword string.
  2975. This will extract info from a string like \"WAIT(w@/!)\"."
  2976. (let (kw key log1 log2)
  2977. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2978. (setq kw (match-string 1 x)
  2979. key (and (match-end 2) (match-string 2 x))
  2980. log1 (and (match-end 3) (match-string 3 x))
  2981. log2 (and (match-end 4) (match-string 4 x)))
  2982. (and (or log1 log2)
  2983. (list kw
  2984. (and log1 (if (equal log1 "!") 'time 'note))
  2985. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2986. (defun org-remove-keyword-keys (list)
  2987. "Remove a pair of parenthesis at the end of each string in LIST."
  2988. (mapcar (lambda (x)
  2989. (if (string-match "(.*)$" x)
  2990. (substring x 0 (match-beginning 0))
  2991. x))
  2992. list))
  2993. ;; FIXME: this could be done much better, using second characters etc.
  2994. (defun org-assign-fast-keys (alist)
  2995. "Assign fast keys to a keyword-key alist.
  2996. Respect keys that are already there."
  2997. (let (new e k c c1 c2 (char ?a))
  2998. (while (setq e (pop alist))
  2999. (cond
  3000. ((equal e '(:startgroup)) (push e new))
  3001. ((equal e '(:endgroup)) (push e new))
  3002. (t
  3003. (setq k (car e) c2 nil)
  3004. (if (cdr e)
  3005. (setq c (cdr e))
  3006. ;; automatically assign a character.
  3007. (setq c1 (string-to-char
  3008. (downcase (substring
  3009. k (if (= (string-to-char k) ?@) 1 0)))))
  3010. (if (or (rassoc c1 new) (rassoc c1 alist))
  3011. (while (or (rassoc char new) (rassoc char alist))
  3012. (setq char (1+ char)))
  3013. (setq c2 c1))
  3014. (setq c (or c2 char)))
  3015. (push (cons k c) new))))
  3016. (nreverse new)))
  3017. ;;; Some variables used in various places
  3018. (defvar org-window-configuration nil
  3019. "Used in various places to store a window configuration.")
  3020. (defvar org-finish-function nil
  3021. "Function to be called when `C-c C-c' is used.
  3022. This is for getting out of special buffers like remember.")
  3023. ;; FIXME: Occasionally check by commenting these, to make sure
  3024. ;; no other functions uses these, forgetting to let-bind them.
  3025. (defvar entry)
  3026. (defvar state)
  3027. (defvar last-state)
  3028. (defvar date)
  3029. (defvar description)
  3030. ;; Defined somewhere in this file, but used before definition.
  3031. (defvar org-html-entities)
  3032. (defvar org-struct-menu)
  3033. (defvar org-org-menu)
  3034. (defvar org-tbl-menu)
  3035. (defvar org-agenda-keymap)
  3036. ;;;; Define the Org-mode
  3037. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3038. (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."))
  3039. ;; We use a before-change function to check if a table might need
  3040. ;; an update.
  3041. (defvar org-table-may-need-update t
  3042. "Indicates that a table might need an update.
  3043. This variable is set by `org-before-change-function'.
  3044. `org-table-align' sets it back to nil.")
  3045. (defun org-before-change-function (beg end)
  3046. "Every change indicates that a table might need an update."
  3047. (setq org-table-may-need-update t))
  3048. (defvar org-mode-map)
  3049. (defvar org-mode-hook nil
  3050. "Mode hook for Org-mode, run after the mode was turned on.")
  3051. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3052. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3053. (defvar org-table-buffer-is-an nil)
  3054. (defconst org-outline-regexp "\\*+ ")
  3055. ;;;###autoload
  3056. (define-derived-mode org-mode outline-mode "Org"
  3057. "Outline-based notes management and organizer, alias
  3058. \"Carsten's outline-mode for keeping track of everything.\"
  3059. Org-mode develops organizational tasks around a NOTES file which
  3060. contains information about projects as plain text. Org-mode is
  3061. implemented on top of outline-mode, which is ideal to keep the content
  3062. of large files well structured. It supports ToDo items, deadlines and
  3063. time stamps, which magically appear in the diary listing of the Emacs
  3064. calendar. Tables are easily created with a built-in table editor.
  3065. Plain text URL-like links connect to websites, emails (VM), Usenet
  3066. messages (Gnus), BBDB entries, and any files related to the project.
  3067. For printing and sharing of notes, an Org-mode file (or a part of it)
  3068. can be exported as a structured ASCII or HTML file.
  3069. The following commands are available:
  3070. \\{org-mode-map}"
  3071. ;; Get rid of Outline menus, they are not needed
  3072. ;; Need to do this here because define-derived-mode sets up
  3073. ;; the keymap so late. Still, it is a waste to call this each time
  3074. ;; we switch another buffer into org-mode.
  3075. (if (featurep 'xemacs)
  3076. (when (boundp 'outline-mode-menu-heading)
  3077. ;; Assume this is Greg's port, it used easymenu
  3078. (easy-menu-remove outline-mode-menu-heading)
  3079. (easy-menu-remove outline-mode-menu-show)
  3080. (easy-menu-remove outline-mode-menu-hide))
  3081. (define-key org-mode-map [menu-bar headings] 'undefined)
  3082. (define-key org-mode-map [menu-bar hide] 'undefined)
  3083. (define-key org-mode-map [menu-bar show] 'undefined))
  3084. (org-load-modules-maybe)
  3085. (easy-menu-add org-org-menu)
  3086. (easy-menu-add org-tbl-menu)
  3087. (org-install-agenda-files-menu)
  3088. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3089. (org-add-to-invisibility-spec '(org-cwidth))
  3090. (when (featurep 'xemacs)
  3091. (org-set-local 'line-move-ignore-invisible t))
  3092. (org-set-local 'outline-regexp org-outline-regexp)
  3093. (org-set-local 'outline-level 'org-outline-level)
  3094. (when (and org-ellipsis
  3095. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3096. (fboundp 'make-glyph-code))
  3097. (unless org-display-table
  3098. (setq org-display-table (make-display-table)))
  3099. (set-display-table-slot
  3100. org-display-table 4
  3101. (vconcat (mapcar
  3102. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3103. org-ellipsis)))
  3104. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3105. (setq buffer-display-table org-display-table))
  3106. (org-set-regexps-and-options)
  3107. (when (and org-tag-faces (not org-tags-special-faces-re))
  3108. ;; tag faces set outside customize.... force initialization.
  3109. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3110. ;; Calc embedded
  3111. (org-set-local 'calc-embedded-open-mode "# ")
  3112. (modify-syntax-entry ?# "<")
  3113. (modify-syntax-entry ?@ "w")
  3114. (if org-startup-truncated (setq truncate-lines t))
  3115. (org-set-local 'font-lock-unfontify-region-function
  3116. 'org-unfontify-region)
  3117. ;; Activate before-change-function
  3118. (org-set-local 'org-table-may-need-update t)
  3119. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3120. 'local)
  3121. ;; Check for running clock before killing a buffer
  3122. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3123. ;; Paragraphs and auto-filling
  3124. (org-set-autofill-regexps)
  3125. (setq indent-line-function 'org-indent-line-function)
  3126. (org-update-radio-target-regexp)
  3127. ;; Comment characters
  3128. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3129. (org-set-local 'comment-padding " ")
  3130. ;; Align options lines
  3131. (org-set-local
  3132. 'align-mode-rules-list
  3133. '((org-in-buffer-settings
  3134. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3135. (modes . '(org-mode)))))
  3136. ;; Imenu
  3137. (org-set-local 'imenu-create-index-function
  3138. 'org-imenu-get-tree)
  3139. ;; Make isearch reveal context
  3140. (if (or (featurep 'xemacs)
  3141. (not (boundp 'outline-isearch-open-invisible-function)))
  3142. ;; Emacs 21 and XEmacs make use of the hook
  3143. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3144. ;; Emacs 22 deals with this through a special variable
  3145. (org-set-local 'outline-isearch-open-invisible-function
  3146. (lambda (&rest ignore) (org-show-context 'isearch))))
  3147. ;; If empty file that did not turn on org-mode automatically, make it to.
  3148. (if (and org-insert-mode-line-in-empty-file
  3149. (interactive-p)
  3150. (= (point-min) (point-max)))
  3151. (insert "# -*- mode: org -*-\n\n"))
  3152. (unless org-inhibit-startup
  3153. (when org-startup-align-all-tables
  3154. (let ((bmp (buffer-modified-p)))
  3155. (org-table-map-tables 'org-table-align)
  3156. (set-buffer-modified-p bmp)))
  3157. (org-set-startup-visibility)))
  3158. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3159. (defun org-current-time ()
  3160. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3161. (if (> (car org-time-stamp-rounding-minutes) 1)
  3162. (let ((r (car org-time-stamp-rounding-minutes))
  3163. (time (decode-time)))
  3164. (apply 'encode-time
  3165. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3166. (nthcdr 2 time))))
  3167. (current-time)))
  3168. ;;;; Font-Lock stuff, including the activators
  3169. (defvar org-mouse-map (make-sparse-keymap))
  3170. (org-defkey org-mouse-map
  3171. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3172. (org-defkey org-mouse-map
  3173. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3174. (when org-mouse-1-follows-link
  3175. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3176. (when org-tab-follows-link
  3177. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3178. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3179. (when org-return-follows-link
  3180. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3181. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3182. (require 'font-lock)
  3183. (defconst org-non-link-chars "]\t\n\r<>")
  3184. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3185. "shell" "elisp"))
  3186. (defvar org-link-types-re nil
  3187. "Matches a link that has a url-like prefix like \"http:\"")
  3188. (defvar org-link-re-with-space nil
  3189. "Matches a link with spaces, optional angular brackets around it.")
  3190. (defvar org-link-re-with-space2 nil
  3191. "Matches a link with spaces, optional angular brackets around it.")
  3192. (defvar org-link-re-with-space3 nil
  3193. "Matches a link with spaces, only for internal part in bracket links.")
  3194. (defvar org-angle-link-re nil
  3195. "Matches link with angular brackets, spaces are allowed.")
  3196. (defvar org-plain-link-re nil
  3197. "Matches plain link, without spaces.")
  3198. (defvar org-bracket-link-regexp nil
  3199. "Matches a link in double brackets.")
  3200. (defvar org-bracket-link-analytic-regexp nil
  3201. "Regular expression used to analyze links.
  3202. Here is what the match groups contain after a match:
  3203. 1: http:
  3204. 2: http
  3205. 3: path
  3206. 4: [desc]
  3207. 5: desc")
  3208. (defvar org-bracket-link-analytic-regexp++ nil
  3209. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3210. (defvar org-any-link-re nil
  3211. "Regular expression matching any link.")
  3212. (defun org-make-link-regexps ()
  3213. "Update the link regular expressions.
  3214. This should be called after the variable `org-link-types' has changed."
  3215. (setq org-link-types-re
  3216. (concat
  3217. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3218. org-link-re-with-space
  3219. (concat
  3220. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3221. "\\([^" org-non-link-chars " ]"
  3222. "[^" org-non-link-chars "]*"
  3223. "[^" org-non-link-chars " ]\\)>?")
  3224. org-link-re-with-space2
  3225. (concat
  3226. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3227. "\\([^" org-non-link-chars " ]"
  3228. "[^\t\n\r]*"
  3229. "[^" org-non-link-chars " ]\\)>?")
  3230. org-link-re-with-space3
  3231. (concat
  3232. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3233. "\\([^" org-non-link-chars " ]"
  3234. "[^\t\n\r]*\\)")
  3235. org-angle-link-re
  3236. (concat
  3237. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3238. "\\([^" org-non-link-chars " ]"
  3239. "[^" org-non-link-chars "]*"
  3240. "\\)>")
  3241. org-plain-link-re
  3242. (concat
  3243. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3244. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3245. org-bracket-link-regexp
  3246. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3247. org-bracket-link-analytic-regexp
  3248. (concat
  3249. "\\[\\["
  3250. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3251. "\\([^]]+\\)"
  3252. "\\]"
  3253. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3254. "\\]")
  3255. org-bracket-link-analytic-regexp++
  3256. (concat
  3257. "\\[\\["
  3258. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3259. "\\([^]]+\\)"
  3260. "\\]"
  3261. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3262. "\\]")
  3263. org-any-link-re
  3264. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3265. org-angle-link-re "\\)\\|\\("
  3266. org-plain-link-re "\\)")))
  3267. (org-make-link-regexps)
  3268. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3269. "Regular expression for fast time stamp matching.")
  3270. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3271. "Regular expression for fast time stamp matching.")
  3272. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3273. "Regular expression matching time strings for analysis.
  3274. This one does not require the space after the date, so it can be used
  3275. on a string that terminates immediately after the date.")
  3276. (defconst org-ts-regexp1 "\\(\\([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. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3279. "Regular expression matching time stamps, with groups.")
  3280. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3281. "Regular expression matching time stamps (also [..]), with groups.")
  3282. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3283. "Regular expression matching a time stamp range.")
  3284. (defconst org-tr-regexp-both
  3285. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3286. "Regular expression matching a time stamp range.")
  3287. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3288. org-ts-regexp "\\)?")
  3289. "Regular expression matching a time stamp or time stamp range.")
  3290. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3291. org-ts-regexp-both "\\)?")
  3292. "Regular expression matching a time stamp or time stamp range.
  3293. The time stamps may be either active or inactive.")
  3294. (defvar org-emph-face nil)
  3295. (defun org-do-emphasis-faces (limit)
  3296. "Run through the buffer and add overlays to links."
  3297. (let (rtn)
  3298. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3299. (if (not (= (char-after (match-beginning 3))
  3300. (char-after (match-beginning 4))))
  3301. (progn
  3302. (setq rtn t)
  3303. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3304. 'face
  3305. (nth 1 (assoc (match-string 3)
  3306. org-emphasis-alist)))
  3307. (add-text-properties (match-beginning 2) (match-end 2)
  3308. '(font-lock-multiline t))
  3309. (when org-hide-emphasis-markers
  3310. (add-text-properties (match-end 4) (match-beginning 5)
  3311. '(invisible org-link))
  3312. (add-text-properties (match-beginning 3) (match-end 3)
  3313. '(invisible org-link)))))
  3314. (backward-char 1))
  3315. rtn))
  3316. (defun org-emphasize (&optional char)
  3317. "Insert or change an emphasis, i.e. a font like bold or italic.
  3318. If there is an active region, change that region to a new emphasis.
  3319. If there is no region, just insert the marker characters and position
  3320. the cursor between them.
  3321. CHAR should be either the marker character, or the first character of the
  3322. HTML tag associated with that emphasis. If CHAR is a space, the means
  3323. to remove the emphasis of the selected region.
  3324. If char is not given (for example in an interactive call) it
  3325. will be prompted for."
  3326. (interactive)
  3327. (let ((eal org-emphasis-alist) e det
  3328. (erc org-emphasis-regexp-components)
  3329. (prompt "")
  3330. (string "") beg end move tag c s)
  3331. (if (org-region-active-p)
  3332. (setq beg (region-beginning) end (region-end)
  3333. string (buffer-substring beg end))
  3334. (setq move t))
  3335. (while (setq e (pop eal))
  3336. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3337. c (aref tag 0))
  3338. (push (cons c (string-to-char (car e))) det)
  3339. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3340. (substring tag 1)))))
  3341. (setq det (nreverse det))
  3342. (unless char
  3343. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3344. (setq char (read-char-exclusive)))
  3345. (setq char (or (cdr (assoc char det)) char))
  3346. (if (equal char ?\ )
  3347. (setq s "" move nil)
  3348. (unless (assoc (char-to-string char) org-emphasis-alist)
  3349. (error "No such emphasis marker: \"%c\"" char))
  3350. (setq s (char-to-string char)))
  3351. (while (and (> (length string) 1)
  3352. (equal (substring string 0 1) (substring string -1))
  3353. (assoc (substring string 0 1) org-emphasis-alist))
  3354. (setq string (substring string 1 -1)))
  3355. (setq string (concat s string s))
  3356. (if beg (delete-region beg end))
  3357. (unless (or (bolp)
  3358. (string-match (concat "[" (nth 0 erc) "\n]")
  3359. (char-to-string (char-before (point)))))
  3360. (insert " "))
  3361. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3362. (char-to-string (char-after (point))))
  3363. (insert " ") (backward-char 1))
  3364. (insert string)
  3365. (and move (backward-char 1))))
  3366. (defconst org-nonsticky-props
  3367. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3368. (defun org-activate-plain-links (limit)
  3369. "Run through the buffer and add overlays to links."
  3370. (catch 'exit
  3371. (let (f)
  3372. (while (re-search-forward org-plain-link-re limit t)
  3373. (setq f (get-text-property (match-beginning 0) 'face))
  3374. (if (or (eq f 'org-tag)
  3375. (and (listp f) (memq 'org-tag f)))
  3376. nil
  3377. (add-text-properties (match-beginning 0) (match-end 0)
  3378. (list 'mouse-face 'highlight
  3379. 'rear-nonsticky org-nonsticky-props
  3380. 'keymap org-mouse-map
  3381. ))
  3382. (throw 'exit t))))))
  3383. (defun org-activate-code (limit)
  3384. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3385. (progn
  3386. (remove-text-properties (match-beginning 0) (match-end 0)
  3387. '(display t invisible t intangible t))
  3388. t)))
  3389. (defun org-activate-angle-links (limit)
  3390. "Run through the buffer and add overlays to links."
  3391. (if (re-search-forward org-angle-link-re limit t)
  3392. (progn
  3393. (add-text-properties (match-beginning 0) (match-end 0)
  3394. (list 'mouse-face 'highlight
  3395. 'rear-nonsticky org-nonsticky-props
  3396. 'keymap org-mouse-map
  3397. ))
  3398. t)))
  3399. (defun org-activate-footnote-links (limit)
  3400. "Run through the buffer and add overlays to links."
  3401. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3402. limit t)
  3403. (progn
  3404. (add-text-properties (match-beginning 2) (match-end 2)
  3405. (list 'mouse-face 'highlight
  3406. 'rear-nonsticky org-nonsticky-props
  3407. 'keymap org-mouse-map
  3408. 'help-echo
  3409. (if (= (point-at-bol) (match-beginning 2))
  3410. "Footnote definition"
  3411. "Footnote reference")
  3412. ))
  3413. t)))
  3414. (defun org-activate-bracket-links (limit)
  3415. "Run through the buffer and add overlays to bracketed links."
  3416. (if (re-search-forward org-bracket-link-regexp limit t)
  3417. (let* ((help (concat "LINK: "
  3418. (org-match-string-no-properties 1)))
  3419. ;; FIXME: above we should remove the escapes.
  3420. ;; but that requires another match, protecting match data,
  3421. ;; a lot of overhead for font-lock.
  3422. (ip (org-maybe-intangible
  3423. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3424. 'keymap org-mouse-map 'mouse-face 'highlight
  3425. 'font-lock-multiline t 'help-echo help)))
  3426. (vp (list 'rear-nonsticky org-nonsticky-props
  3427. 'keymap org-mouse-map 'mouse-face 'highlight
  3428. ' font-lock-multiline t 'help-echo help)))
  3429. ;; We need to remove the invisible property here. Table narrowing
  3430. ;; may have made some of this invisible.
  3431. (remove-text-properties (match-beginning 0) (match-end 0)
  3432. '(invisible nil))
  3433. (if (match-end 3)
  3434. (progn
  3435. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3436. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3437. (add-text-properties (match-end 3) (match-end 0) ip))
  3438. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3439. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3440. (add-text-properties (match-end 1) (match-end 0) ip))
  3441. t)))
  3442. (defun org-activate-dates (limit)
  3443. "Run through the buffer and add overlays to dates."
  3444. (if (re-search-forward org-tsr-regexp-both limit t)
  3445. (progn
  3446. (add-text-properties (match-beginning 0) (match-end 0)
  3447. (list 'mouse-face 'highlight
  3448. 'rear-nonsticky org-nonsticky-props
  3449. 'keymap org-mouse-map))
  3450. (when org-display-custom-times
  3451. (if (match-end 3)
  3452. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3453. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3454. t)))
  3455. (defvar org-target-link-regexp nil
  3456. "Regular expression matching radio targets in plain text.")
  3457. (make-variable-buffer-local 'org-target-link-regexp)
  3458. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3459. "Regular expression matching a link target.")
  3460. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3461. "Regular expression matching a radio target.")
  3462. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3463. "Regular expression matching any target.")
  3464. (defun org-activate-target-links (limit)
  3465. "Run through the buffer and add overlays to target matches."
  3466. (when org-target-link-regexp
  3467. (let ((case-fold-search t))
  3468. (if (re-search-forward org-target-link-regexp limit t)
  3469. (progn
  3470. (add-text-properties (match-beginning 0) (match-end 0)
  3471. (list 'mouse-face 'highlight
  3472. 'rear-nonsticky org-nonsticky-props
  3473. 'keymap org-mouse-map
  3474. 'help-echo "Radio target link"
  3475. 'org-linked-text t))
  3476. t)))))
  3477. (defun org-update-radio-target-regexp ()
  3478. "Find all radio targets in this file and update the regular expression."
  3479. (interactive)
  3480. (when (memq 'radio org-activate-links)
  3481. (setq org-target-link-regexp
  3482. (org-make-target-link-regexp (org-all-targets 'radio)))
  3483. (org-restart-font-lock)))
  3484. (defun org-hide-wide-columns (limit)
  3485. (let (s e)
  3486. (setq s (text-property-any (point) (or limit (point-max))
  3487. 'org-cwidth t))
  3488. (when s
  3489. (setq e (next-single-property-change s 'org-cwidth))
  3490. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3491. (goto-char e)
  3492. t)))
  3493. (defvar org-latex-and-specials-regexp nil
  3494. "Regular expression for highlighting export special stuff.")
  3495. (defvar org-match-substring-regexp)
  3496. (defvar org-match-substring-with-braces-regexp)
  3497. (defvar org-export-html-special-string-regexps)
  3498. (defun org-compute-latex-and-specials-regexp ()
  3499. "Compute regular expression for stuff treated specially by exporters."
  3500. (if (not org-highlight-latex-fragments-and-specials)
  3501. (org-set-local 'org-latex-and-specials-regexp nil)
  3502. (require 'org-exp)
  3503. (let*
  3504. ((matchers (plist-get org-format-latex-options :matchers))
  3505. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3506. org-latex-regexps)))
  3507. (options (org-combine-plists (org-default-export-plist)
  3508. (org-infile-export-plist)))
  3509. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3510. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3511. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3512. (org-export-html-expand (plist-get options :expand-quoted-html))
  3513. (org-export-with-special-strings (plist-get options :special-strings))
  3514. (re-sub
  3515. (cond
  3516. ((equal org-export-with-sub-superscripts '{})
  3517. (list org-match-substring-with-braces-regexp))
  3518. (org-export-with-sub-superscripts
  3519. (list org-match-substring-regexp))
  3520. (t nil)))
  3521. (re-latex
  3522. (if org-export-with-LaTeX-fragments
  3523. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3524. (re-macros
  3525. (if org-export-with-TeX-macros
  3526. (list (concat "\\\\"
  3527. (regexp-opt
  3528. (append (mapcar 'car org-html-entities)
  3529. (if (boundp 'org-latex-entities)
  3530. org-latex-entities nil))
  3531. 'words))) ; FIXME
  3532. ))
  3533. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3534. (re-special (if org-export-with-special-strings
  3535. (mapcar (lambda (x) (car x))
  3536. org-export-html-special-string-regexps)))
  3537. (re-rest
  3538. (delq nil
  3539. (list
  3540. (if org-export-html-expand "@<[^>\n]+>")
  3541. ))))
  3542. (org-set-local
  3543. 'org-latex-and-specials-regexp
  3544. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3545. re-rest) "\\|")))))
  3546. (defun org-do-latex-and-special-faces (limit)
  3547. "Run through the buffer and add overlays to links."
  3548. (when org-latex-and-specials-regexp
  3549. (let (rtn d)
  3550. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3551. limit t))
  3552. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3553. 'face))
  3554. '(org-code org-verbatim underline)))
  3555. (progn
  3556. (setq rtn t
  3557. d (cond ((member (char-after (1+ (match-beginning 0)))
  3558. '(?_ ?^)) 1)
  3559. (t 0)))
  3560. (font-lock-prepend-text-property
  3561. (+ d (match-beginning 0)) (match-end 0)
  3562. 'face 'org-latex-and-export-specials)
  3563. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3564. '(font-lock-multiline t)))))
  3565. rtn)))
  3566. (defun org-restart-font-lock ()
  3567. "Restart font-lock-mode, to force refontification."
  3568. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3569. (font-lock-mode -1)
  3570. (font-lock-mode 1)))
  3571. (defun org-all-targets (&optional radio)
  3572. "Return a list of all targets in this file.
  3573. With optional argument RADIO, only find radio targets."
  3574. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3575. rtn)
  3576. (save-excursion
  3577. (goto-char (point-min))
  3578. (while (re-search-forward re nil t)
  3579. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3580. rtn)))
  3581. (defun org-make-target-link-regexp (targets)
  3582. "Make regular expression matching all strings in TARGETS.
  3583. The regular expression finds the targets also if there is a line break
  3584. between words."
  3585. (and targets
  3586. (concat
  3587. "\\<\\("
  3588. (mapconcat
  3589. (lambda (x)
  3590. (while (string-match " +" x)
  3591. (setq x (replace-match "\\s-+" t t x)))
  3592. x)
  3593. targets
  3594. "\\|")
  3595. "\\)\\>")))
  3596. (defun org-activate-tags (limit)
  3597. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3598. (progn
  3599. (add-text-properties (match-beginning 1) (match-end 1)
  3600. (list 'mouse-face 'highlight
  3601. 'rear-nonsticky org-nonsticky-props
  3602. 'keymap org-mouse-map))
  3603. t)))
  3604. (defun org-outline-level ()
  3605. (save-excursion
  3606. (looking-at outline-regexp)
  3607. (if (match-beginning 1)
  3608. (+ (org-get-string-indentation (match-string 1)) 1000)
  3609. (1- (- (match-end 0) (match-beginning 0))))))
  3610. (defvar org-font-lock-keywords nil)
  3611. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3612. "Regular expression matching a property line.")
  3613. (defvar org-font-lock-hook nil
  3614. "Functions to be called for special font lock stuff.")
  3615. (defun org-font-lock-hook (limit)
  3616. (run-hook-with-args 'org-font-lock-hook limit))
  3617. (defun org-set-font-lock-defaults ()
  3618. (let* ((em org-fontify-emphasized-text)
  3619. (lk org-activate-links)
  3620. (org-font-lock-extra-keywords
  3621. (list
  3622. ;; Call the hook
  3623. '(org-font-lock-hook)
  3624. ;; Headlines
  3625. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3626. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3627. ;; Table lines
  3628. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3629. (1 'org-table t))
  3630. ;; Table internals
  3631. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3632. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3633. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3634. ;; Drawers
  3635. (list org-drawer-regexp '(0 'org-special-keyword t))
  3636. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3637. ;; Properties
  3638. (list org-property-re
  3639. '(1 'org-special-keyword t)
  3640. '(3 'org-property-value t))
  3641. (if org-format-transports-properties-p
  3642. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3643. ;; Links
  3644. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3645. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3646. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3647. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3648. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3649. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3650. (if (memq 'footnote lk) '(org-activate-footnote-links
  3651. (2 'org-footnote t)))
  3652. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3653. '(org-hide-wide-columns (0 nil append))
  3654. ;; TODO lines
  3655. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3656. '(1 (org-get-todo-face 1) t))
  3657. ;; DONE
  3658. (if org-fontify-done-headline
  3659. (list (concat "^[*]+ +\\<\\("
  3660. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3661. "\\)\\(.*\\)")
  3662. '(2 'org-headline-done t))
  3663. nil)
  3664. ;; Priorities
  3665. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3666. ;; Tags
  3667. '(org-font-lock-add-tag-faces)
  3668. ;; Special keywords
  3669. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3670. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3671. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3672. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3673. ;; Emphasis
  3674. (if em
  3675. (if (featurep 'xemacs)
  3676. '(org-do-emphasis-faces (0 nil append))
  3677. '(org-do-emphasis-faces)))
  3678. ;; Checkboxes
  3679. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3680. 2 'bold prepend)
  3681. (if org-provide-checkbox-statistics
  3682. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3683. (0 (org-get-checkbox-statistics-face) t)))
  3684. ;; Description list items
  3685. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3686. 2 'bold prepend)
  3687. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3688. '(1 'org-archived prepend))
  3689. ;; Specials
  3690. '(org-do-latex-and-special-faces)
  3691. ;; Code
  3692. '(org-activate-code (1 'org-code t))
  3693. ;; COMMENT
  3694. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3695. "\\|" org-quote-string "\\)\\>")
  3696. '(1 'org-special-keyword t))
  3697. '("^#.*" (0 'font-lock-comment-face t))
  3698. )))
  3699. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3700. ;; Now set the full font-lock-keywords
  3701. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3702. (org-set-local 'font-lock-defaults
  3703. '(org-font-lock-keywords t nil nil backward-paragraph))
  3704. (kill-local-variable 'font-lock-keywords) nil))
  3705. (defvar org-m nil)
  3706. (defvar org-l nil)
  3707. (defvar org-f nil)
  3708. (defun org-get-level-face (n)
  3709. "Get the right face for match N in font-lock matching of headlines."
  3710. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3711. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3712. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3713. (cond
  3714. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3715. ((eq n 2) org-f)
  3716. (t (if org-level-color-stars-only nil org-f))))
  3717. (defun org-get-todo-face (kwd)
  3718. "Get the right face for a TODO keyword KWD.
  3719. If KWD is a number, get the corresponding match group."
  3720. (if (numberp kwd) (setq kwd (match-string kwd)))
  3721. (or (cdr (assoc kwd org-todo-keyword-faces))
  3722. (and (member kwd org-done-keywords) 'org-done)
  3723. 'org-todo))
  3724. (defun org-font-lock-add-tag-faces (limit)
  3725. "Add the special tag faces."
  3726. (when (and org-tag-faces org-tags-special-faces-re)
  3727. (while (re-search-forward org-tags-special-faces-re limit t)
  3728. (add-text-properties (match-beginning 1) (match-end 1)
  3729. (list 'face (org-get-tag-face 1)
  3730. 'font-lock-fontified t))
  3731. (backward-char 1))))
  3732. (defun org-get-tag-face (kwd)
  3733. "Get the right face for a TODO keyword KWD.
  3734. If KWD is a number, get the corresponding match group."
  3735. (if (numberp kwd) (setq kwd (match-string kwd)))
  3736. (or (cdr (assoc kwd org-tag-faces))
  3737. 'org-tag))
  3738. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3739. "Remove fontification and activation overlays from links."
  3740. (font-lock-default-unfontify-region beg end)
  3741. (let* ((buffer-undo-list t)
  3742. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3743. (inhibit-modification-hooks t)
  3744. deactivate-mark buffer-file-name buffer-file-truename)
  3745. (remove-text-properties beg end
  3746. '(mouse-face t keymap t org-linked-text t
  3747. invisible t intangible t))))
  3748. ;;;; Visibility cycling, including org-goto and indirect buffer
  3749. ;;; Cycling
  3750. (defvar org-cycle-global-status nil)
  3751. (make-variable-buffer-local 'org-cycle-global-status)
  3752. (defvar org-cycle-subtree-status nil)
  3753. (make-variable-buffer-local 'org-cycle-subtree-status)
  3754. ;;;###autoload
  3755. (defun org-cycle (&optional arg)
  3756. "Visibility cycling for Org-mode.
  3757. - When this function is called with a prefix argument, rotate the entire
  3758. buffer through 3 states (global cycling)
  3759. 1. OVERVIEW: Show only top-level headlines.
  3760. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3761. 3. SHOW ALL: Show everything.
  3762. When called with two C-u C-u prefixes, switch to the startup visibility,
  3763. determined by the variable `org-startup-folded', and by any VISIBILITY
  3764. properties in the buffer.
  3765. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3766. including drawers.
  3767. - When point is at the beginning of a headline, rotate the subtree started
  3768. by this line through 3 different states (local cycling)
  3769. 1. FOLDED: Only the main headline is shown.
  3770. 2. CHILDREN: The main headline and the direct children are shown.
  3771. From this state, you can move to one of the children
  3772. and zoom in further.
  3773. 3. SUBTREE: Show the entire subtree, including body text.
  3774. - When there is a numeric prefix, go up to a heading with level ARG, do
  3775. a `show-subtree' and return to the previous cursor position. If ARG
  3776. is negative, go up that many levels.
  3777. - When point is not at the beginning of a headline, execute the global
  3778. binding for TAB, which is re-indenting the line. See the option
  3779. `org-cycle-emulate-tab' for details.
  3780. - Special case: if point is at the beginning of the buffer and there is
  3781. no headline in line 1, this function will act as if called with prefix arg.
  3782. But only if also the variable `org-cycle-global-at-bob' is t."
  3783. (interactive "P")
  3784. (org-load-modules-maybe)
  3785. (let* ((outline-regexp
  3786. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3787. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3788. outline-regexp))
  3789. (bob-special (and org-cycle-global-at-bob (bobp)
  3790. (not (looking-at outline-regexp))))
  3791. (org-cycle-hook
  3792. (if bob-special
  3793. (delq 'org-optimize-window-after-visibility-change
  3794. (copy-sequence org-cycle-hook))
  3795. org-cycle-hook))
  3796. (pos (point)))
  3797. (if (or bob-special (equal arg '(4)))
  3798. ;; special case: use global cycling
  3799. (setq arg t))
  3800. (cond
  3801. ((equal arg '(16))
  3802. (org-set-startup-visibility)
  3803. (message "Startup visibility, plus VISIBILITY properties"))
  3804. ((equal arg '(64))
  3805. (show-all)
  3806. (message "Entire buffer visible, including drawers"))
  3807. ((org-at-table-p 'any)
  3808. ;; Enter the table or move to the next field in the table
  3809. (or (org-table-recognize-table.el)
  3810. (progn
  3811. (if arg (org-table-edit-field t)
  3812. (org-table-justify-field-maybe)
  3813. (call-interactively 'org-table-next-field)))))
  3814. ((eq arg t) ;; Global cycling
  3815. (cond
  3816. ((and (eq last-command this-command)
  3817. (eq org-cycle-global-status 'overview))
  3818. ;; We just created the overview - now do table of contents
  3819. ;; This can be slow in very large buffers, so indicate action
  3820. (message "CONTENTS...")
  3821. (org-content)
  3822. (message "CONTENTS...done")
  3823. (setq org-cycle-global-status 'contents)
  3824. (run-hook-with-args 'org-cycle-hook 'contents))
  3825. ((and (eq last-command this-command)
  3826. (eq org-cycle-global-status 'contents))
  3827. ;; We just showed the table of contents - now show everything
  3828. (show-all)
  3829. (message "SHOW ALL")
  3830. (setq org-cycle-global-status 'all)
  3831. (run-hook-with-args 'org-cycle-hook 'all))
  3832. (t
  3833. ;; Default action: go to overview
  3834. (org-overview)
  3835. (message "OVERVIEW")
  3836. (setq org-cycle-global-status 'overview)
  3837. (run-hook-with-args 'org-cycle-hook 'overview))))
  3838. ((and org-drawers org-drawer-regexp
  3839. (save-excursion
  3840. (beginning-of-line 1)
  3841. (looking-at org-drawer-regexp)))
  3842. ;; Toggle block visibility
  3843. (org-flag-drawer
  3844. (not (get-char-property (match-end 0) 'invisible))))
  3845. ((integerp arg)
  3846. ;; Show-subtree, ARG levels up from here.
  3847. (save-excursion
  3848. (org-back-to-heading)
  3849. (outline-up-heading (if (< arg 0) (- arg)
  3850. (- (funcall outline-level) arg)))
  3851. (org-show-subtree)))
  3852. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3853. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3854. ;; At a heading: rotate between three different views
  3855. (org-back-to-heading)
  3856. (let ((goal-column 0) eoh eol eos)
  3857. ;; First, some boundaries
  3858. (save-excursion
  3859. (org-back-to-heading)
  3860. (save-excursion
  3861. (beginning-of-line 2)
  3862. (while (and (not (eobp)) ;; this is like `next-line'
  3863. (get-char-property (1- (point)) 'invisible))
  3864. (beginning-of-line 2)) (setq eol (point)))
  3865. (outline-end-of-heading) (setq eoh (point))
  3866. (org-end-of-subtree t)
  3867. (unless (eobp)
  3868. (skip-chars-forward " \t\n")
  3869. (beginning-of-line 1) ; in case this is an item
  3870. )
  3871. (setq eos (1- (point))))
  3872. ;; Find out what to do next and set `this-command'
  3873. (cond
  3874. ((= eos eoh)
  3875. ;; Nothing is hidden behind this heading
  3876. (message "EMPTY ENTRY")
  3877. (setq org-cycle-subtree-status nil)
  3878. (save-excursion
  3879. (goto-char eos)
  3880. (outline-next-heading)
  3881. (if (org-invisible-p) (org-flag-heading nil))))
  3882. ((or (>= eol eos)
  3883. (not (string-match "\\S-" (buffer-substring eol eos))))
  3884. ;; Entire subtree is hidden in one line: open it
  3885. (org-show-entry)
  3886. (show-children)
  3887. (message "CHILDREN")
  3888. (save-excursion
  3889. (goto-char eos)
  3890. (outline-next-heading)
  3891. (if (org-invisible-p) (org-flag-heading nil)))
  3892. (setq org-cycle-subtree-status 'children)
  3893. (run-hook-with-args 'org-cycle-hook 'children))
  3894. ((and (eq last-command this-command)
  3895. (eq org-cycle-subtree-status 'children))
  3896. ;; We just showed the children, now show everything.
  3897. (org-show-subtree)
  3898. (message "SUBTREE")
  3899. (setq org-cycle-subtree-status 'subtree)
  3900. (run-hook-with-args 'org-cycle-hook 'subtree))
  3901. (t
  3902. ;; Default action: hide the subtree.
  3903. (hide-subtree)
  3904. (message "FOLDED")
  3905. (setq org-cycle-subtree-status 'folded)
  3906. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3907. ;; TAB emulation and template completion
  3908. (buffer-read-only (org-back-to-heading))
  3909. ((org-try-structure-completion))
  3910. ((org-try-cdlatex-tab))
  3911. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3912. (or (not (bolp))
  3913. (not (looking-at outline-regexp))))
  3914. (call-interactively (global-key-binding "\t")))
  3915. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3916. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3917. (or (and (eq org-cycle-emulate-tab 'white)
  3918. (= (match-end 0) (point-at-eol)))
  3919. (and (eq org-cycle-emulate-tab 'whitestart)
  3920. (>= (match-end 0) pos))))
  3921. t
  3922. (eq org-cycle-emulate-tab t))
  3923. (call-interactively (global-key-binding "\t")))
  3924. (t (save-excursion
  3925. (org-back-to-heading)
  3926. (org-cycle))))))
  3927. ;;;###autoload
  3928. (defun org-global-cycle (&optional arg)
  3929. "Cycle the global visibility. For details see `org-cycle'.
  3930. With C-u prefix arg, switch to startup visibility.
  3931. With a numeric prefix, show all headlines up to that level."
  3932. (interactive "P")
  3933. (let ((org-cycle-include-plain-lists
  3934. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3935. (cond
  3936. ((integerp arg)
  3937. (show-all)
  3938. (hide-sublevels arg)
  3939. (setq org-cycle-global-status 'contents))
  3940. ((equal arg '(4))
  3941. (org-set-startup-visibility)
  3942. (message "Startup visibility, plus VISIBILITY properties."))
  3943. (t
  3944. (org-cycle '(4))))))
  3945. (defun org-set-startup-visibility ()
  3946. "Set the visibility required by startup options and properties."
  3947. (cond
  3948. ((eq org-startup-folded t)
  3949. (org-cycle '(4)))
  3950. ((eq org-startup-folded 'content)
  3951. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3952. (org-cycle '(4)) (org-cycle '(4)))))
  3953. (org-set-visibility-according-to-property 'no-cleanup)
  3954. (org-cycle-hide-archived-subtrees 'all)
  3955. (org-cycle-hide-drawers 'all)
  3956. (org-cycle-show-empty-lines 'all))
  3957. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3958. "Switch subtree visibilities according to :VISIBILITY: property."
  3959. (interactive)
  3960. (let (state)
  3961. (save-excursion
  3962. (goto-char (point-min))
  3963. (while (re-search-forward
  3964. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3965. nil t)
  3966. (setq state (match-string 1))
  3967. (save-excursion
  3968. (org-back-to-heading t)
  3969. (hide-subtree)
  3970. (org-reveal)
  3971. (cond
  3972. ((equal state '("fold" "folded"))
  3973. (hide-subtree))
  3974. ((equal state "children")
  3975. (org-show-hidden-entry)
  3976. (show-children))
  3977. ((equal state "content")
  3978. (save-excursion
  3979. (save-restriction
  3980. (org-narrow-to-subtree)
  3981. (org-content))))
  3982. ((member state '("all" "showall"))
  3983. (show-subtree)))))
  3984. (unless no-cleanup
  3985. (org-cycle-hide-archived-subtrees 'all)
  3986. (org-cycle-hide-drawers 'all)
  3987. (org-cycle-show-empty-lines 'all)))))
  3988. (defun org-overview ()
  3989. "Switch to overview mode, showing only top-level headlines.
  3990. Really, this shows all headlines with level equal or greater than the level
  3991. of the first headline in the buffer. This is important, because if the
  3992. first headline is not level one, then (hide-sublevels 1) gives confusing
  3993. results."
  3994. (interactive)
  3995. (let ((level (save-excursion
  3996. (goto-char (point-min))
  3997. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3998. (progn
  3999. (goto-char (match-beginning 0))
  4000. (funcall outline-level))))))
  4001. (and level (hide-sublevels level))))
  4002. (defun org-content (&optional arg)
  4003. "Show all headlines in the buffer, like a table of contents.
  4004. With numerical argument N, show content up to level N."
  4005. (interactive "P")
  4006. (save-excursion
  4007. ;; Visit all headings and show their offspring
  4008. (and (integerp arg) (org-overview))
  4009. (goto-char (point-max))
  4010. (catch 'exit
  4011. (while (and (progn (condition-case nil
  4012. (outline-previous-visible-heading 1)
  4013. (error (goto-char (point-min))))
  4014. t)
  4015. (looking-at outline-regexp))
  4016. (if (integerp arg)
  4017. (show-children (1- arg))
  4018. (show-branches))
  4019. (if (bobp) (throw 'exit nil))))))
  4020. (defun org-optimize-window-after-visibility-change (state)
  4021. "Adjust the window after a change in outline visibility.
  4022. This function is the default value of the hook `org-cycle-hook'."
  4023. (when (get-buffer-window (current-buffer))
  4024. (cond
  4025. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4026. ; ((eq state 'overview) (org-beginning-of-line))
  4027. ((eq state 'content) nil)
  4028. ((eq state 'all) nil)
  4029. ((eq state 'folded) nil)
  4030. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4031. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4032. (defun org-compact-display-after-subtree-move ()
  4033. "Show a compacter version of the tree of the entry's parent."
  4034. (save-excursion
  4035. (if (org-up-heading-safe)
  4036. (progn
  4037. (hide-subtree)
  4038. (show-entry)
  4039. (show-children)
  4040. (org-cycle-show-empty-lines 'children)
  4041. (org-cycle-hide-drawers 'children))
  4042. (org-overview))))
  4043. (defun org-cycle-show-empty-lines (state)
  4044. "Show empty lines above all visible headlines.
  4045. The region to be covered depends on STATE when called through
  4046. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4047. entire buffer covered. Note that an empty line is only shown if there
  4048. are at least `org-cycle-separator-lines' empty lines before the headline."
  4049. (when (> org-cycle-separator-lines 0)
  4050. (save-excursion
  4051. (let* ((n org-cycle-separator-lines)
  4052. (re (cond
  4053. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4054. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4055. (t (let ((ns (number-to-string (- n 2))))
  4056. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4057. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4058. beg end)
  4059. (cond
  4060. ((memq state '(overview contents t))
  4061. (setq beg (point-min) end (point-max)))
  4062. ((memq state '(children folded))
  4063. (setq beg (point) end (progn (org-end-of-subtree t t)
  4064. (beginning-of-line 2)
  4065. (point)))))
  4066. (when beg
  4067. (goto-char beg)
  4068. (while (re-search-forward re end t)
  4069. (if (not (get-char-property (match-end 1) 'invisible))
  4070. (outline-flag-region
  4071. (match-beginning 1) (match-end 1) nil)))))))
  4072. ;; Never hide empty lines at the end of the file.
  4073. (save-excursion
  4074. (goto-char (point-max))
  4075. (outline-previous-heading)
  4076. (outline-end-of-heading)
  4077. (if (and (looking-at "[ \t\n]+")
  4078. (= (match-end 0) (point-max)))
  4079. (outline-flag-region (point) (match-end 0) nil))))
  4080. (defun org-show-empty-lines-in-parent ()
  4081. "Move to the parent and re-show empty lines before visible headlines."
  4082. (save-excursion
  4083. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4084. (org-cycle-show-empty-lines context))))
  4085. (defun org-cycle-hide-drawers (state)
  4086. "Re-hide all drawers after a visibility state change."
  4087. (when (and (org-mode-p)
  4088. (not (memq state '(overview folded))))
  4089. (save-excursion
  4090. (let* ((globalp (memq state '(contents all)))
  4091. (beg (if globalp (point-min) (point)))
  4092. (end (if globalp (point-max) (org-end-of-subtree t))))
  4093. (goto-char beg)
  4094. (while (re-search-forward org-drawer-regexp end t)
  4095. (org-flag-drawer t))))))
  4096. (defun org-flag-drawer (flag)
  4097. (save-excursion
  4098. (beginning-of-line 1)
  4099. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4100. (let ((b (match-end 0))
  4101. (outline-regexp org-outline-regexp))
  4102. (if (re-search-forward
  4103. "^[ \t]*:END:"
  4104. (save-excursion (outline-next-heading) (point)) t)
  4105. (outline-flag-region b (point-at-eol) flag)
  4106. (error ":END: line missing"))))))
  4107. (defun org-subtree-end-visible-p ()
  4108. "Is the end of the current subtree visible?"
  4109. (pos-visible-in-window-p
  4110. (save-excursion (org-end-of-subtree t) (point))))
  4111. (defun org-first-headline-recenter (&optional N)
  4112. "Move cursor to the first headline and recenter the headline.
  4113. Optional argument N means, put the headline into the Nth line of the window."
  4114. (goto-char (point-min))
  4115. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4116. (beginning-of-line)
  4117. (recenter (prefix-numeric-value N))))
  4118. ;;; Org-goto
  4119. (defvar org-goto-window-configuration nil)
  4120. (defvar org-goto-marker nil)
  4121. (defvar org-goto-map
  4122. (let ((map (make-sparse-keymap)))
  4123. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4124. (while (setq cmd (pop cmds))
  4125. (substitute-key-definition cmd cmd map global-map)))
  4126. (suppress-keymap map)
  4127. (org-defkey map "\C-m" 'org-goto-ret)
  4128. (org-defkey map [(return)] 'org-goto-ret)
  4129. (org-defkey map [(left)] 'org-goto-left)
  4130. (org-defkey map [(right)] 'org-goto-right)
  4131. (org-defkey map [(control ?g)] 'org-goto-quit)
  4132. (org-defkey map "\C-i" 'org-cycle)
  4133. (org-defkey map [(tab)] 'org-cycle)
  4134. (org-defkey map [(down)] 'outline-next-visible-heading)
  4135. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4136. (if org-goto-auto-isearch
  4137. (if (fboundp 'define-key-after)
  4138. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4139. nil)
  4140. (org-defkey map "q" 'org-goto-quit)
  4141. (org-defkey map "n" 'outline-next-visible-heading)
  4142. (org-defkey map "p" 'outline-previous-visible-heading)
  4143. (org-defkey map "f" 'outline-forward-same-level)
  4144. (org-defkey map "b" 'outline-backward-same-level)
  4145. (org-defkey map "u" 'outline-up-heading))
  4146. (org-defkey map "/" 'org-occur)
  4147. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4148. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4149. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4150. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4151. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4152. map))
  4153. (defconst org-goto-help
  4154. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4155. RET=jump to location [Q]uit and return to previous location
  4156. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4157. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4158. ;; FIXME: Docstring doe not mention both interfaces
  4159. (defun org-goto (&optional alternative-interface)
  4160. "Look up a different location in the current file, keeping current visibility.
  4161. When you want look-up or go to a different location in a document, the
  4162. fastest way is often to fold the entire buffer and then dive into the tree.
  4163. This method has the disadvantage, that the previous location will be folded,
  4164. which may not be what you want.
  4165. This command works around this by showing a copy of the current buffer
  4166. in an indirect buffer, in overview mode. You can dive into the tree in
  4167. that copy, use org-occur and incremental search to find a location.
  4168. When pressing RET or `Q', the command returns to the original buffer in
  4169. which the visibility is still unchanged. After RET is will also jump to
  4170. the location selected in the indirect buffer and expose the
  4171. the headline hierarchy above."
  4172. (interactive "P")
  4173. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4174. (org-refile-use-outline-path t)
  4175. (interface
  4176. (if (not alternative-interface)
  4177. org-goto-interface
  4178. (if (eq org-goto-interface 'outline)
  4179. 'outline-path-completion
  4180. 'outline)))
  4181. (org-goto-start-pos (point))
  4182. (selected-point
  4183. (if (eq interface 'outline)
  4184. (car (org-get-location (current-buffer) org-goto-help))
  4185. (nth 3 (org-refile-get-location "Goto: ")))))
  4186. (if selected-point
  4187. (progn
  4188. (org-mark-ring-push org-goto-start-pos)
  4189. (goto-char selected-point)
  4190. (if (or (org-invisible-p) (org-invisible-p2))
  4191. (org-show-context 'org-goto)))
  4192. (message "Quit"))))
  4193. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4194. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4195. (defvar org-goto-local-auto-isearch-map) ; defined below
  4196. (defun org-get-location (buf help)
  4197. "Let the user select a location in the Org-mode buffer BUF.
  4198. This function uses a recursive edit. It returns the selected position
  4199. or nil."
  4200. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4201. (isearch-hide-immediately nil)
  4202. (isearch-search-fun-function
  4203. (lambda () 'org-goto-local-search-headings))
  4204. (org-goto-selected-point org-goto-exit-command))
  4205. (save-excursion
  4206. (save-window-excursion
  4207. (delete-other-windows)
  4208. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4209. (switch-to-buffer
  4210. (condition-case nil
  4211. (make-indirect-buffer (current-buffer) "*org-goto*")
  4212. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4213. (with-output-to-temp-buffer "*Help*"
  4214. (princ help))
  4215. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4216. (setq buffer-read-only nil)
  4217. (let ((org-startup-truncated t)
  4218. (org-startup-folded nil)
  4219. (org-startup-align-all-tables nil))
  4220. (org-mode)
  4221. (org-overview))
  4222. (setq buffer-read-only t)
  4223. (if (and (boundp 'org-goto-start-pos)
  4224. (integer-or-marker-p org-goto-start-pos))
  4225. (let ((org-show-hierarchy-above t)
  4226. (org-show-siblings t)
  4227. (org-show-following-heading t))
  4228. (goto-char org-goto-start-pos)
  4229. (and (org-invisible-p) (org-show-context)))
  4230. (goto-char (point-min)))
  4231. (org-beginning-of-line)
  4232. (message "Select location and press RET")
  4233. (use-local-map org-goto-map)
  4234. (recursive-edit)
  4235. ))
  4236. (kill-buffer "*org-goto*")
  4237. (cons org-goto-selected-point org-goto-exit-command)))
  4238. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4239. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4240. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4241. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4242. (defun org-goto-local-search-headings (string bound noerror)
  4243. "Search and make sure that any matches are in headlines."
  4244. (catch 'return
  4245. (while (if isearch-forward
  4246. (search-forward string bound noerror)
  4247. (search-backward string bound noerror))
  4248. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4249. (and (member :headline context)
  4250. (not (member :tags context))))
  4251. (throw 'return (point))))))
  4252. (defun org-goto-local-auto-isearch ()
  4253. "Start isearch."
  4254. (interactive)
  4255. (goto-char (point-min))
  4256. (let ((keys (this-command-keys)))
  4257. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4258. (isearch-mode t)
  4259. (isearch-process-search-char (string-to-char keys)))))
  4260. (defun org-goto-ret (&optional arg)
  4261. "Finish `org-goto' by going to the new location."
  4262. (interactive "P")
  4263. (setq org-goto-selected-point (point)
  4264. org-goto-exit-command 'return)
  4265. (throw 'exit nil))
  4266. (defun org-goto-left ()
  4267. "Finish `org-goto' by going to the new location."
  4268. (interactive)
  4269. (if (org-on-heading-p)
  4270. (progn
  4271. (beginning-of-line 1)
  4272. (setq org-goto-selected-point (point)
  4273. org-goto-exit-command 'left)
  4274. (throw 'exit nil))
  4275. (error "Not on a heading")))
  4276. (defun org-goto-right ()
  4277. "Finish `org-goto' by going to the new location."
  4278. (interactive)
  4279. (if (org-on-heading-p)
  4280. (progn
  4281. (setq org-goto-selected-point (point)
  4282. org-goto-exit-command 'right)
  4283. (throw 'exit nil))
  4284. (error "Not on a heading")))
  4285. (defun org-goto-quit ()
  4286. "Finish `org-goto' without cursor motion."
  4287. (interactive)
  4288. (setq org-goto-selected-point nil)
  4289. (setq org-goto-exit-command 'quit)
  4290. (throw 'exit nil))
  4291. ;;; Indirect buffer display of subtrees
  4292. (defvar org-indirect-dedicated-frame nil
  4293. "This is the frame being used for indirect tree display.")
  4294. (defvar org-last-indirect-buffer nil)
  4295. (defun org-tree-to-indirect-buffer (&optional arg)
  4296. "Create indirect buffer and narrow it to current subtree.
  4297. With numerical prefix ARG, go up to this level and then take that tree.
  4298. If ARG is negative, go up that many levels.
  4299. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4300. indirect buffer previously made with this command, to avoid proliferation of
  4301. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4302. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4303. is kept so that you can work with several indirect buffers at the same time.
  4304. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4305. requests that a new frame be made for the new buffer, so that the dedicated
  4306. frame is not changed."
  4307. (interactive "P")
  4308. (let ((cbuf (current-buffer))
  4309. (cwin (selected-window))
  4310. (pos (point))
  4311. beg end level heading ibuf)
  4312. (save-excursion
  4313. (org-back-to-heading t)
  4314. (when (numberp arg)
  4315. (setq level (org-outline-level))
  4316. (if (< arg 0) (setq arg (+ level arg)))
  4317. (while (> (setq level (org-outline-level)) arg)
  4318. (outline-up-heading 1 t)))
  4319. (setq beg (point)
  4320. heading (org-get-heading))
  4321. (org-end-of-subtree t) (setq end (point)))
  4322. (if (and (buffer-live-p org-last-indirect-buffer)
  4323. (not (eq org-indirect-buffer-display 'new-frame))
  4324. (not arg))
  4325. (kill-buffer org-last-indirect-buffer))
  4326. (setq ibuf (org-get-indirect-buffer cbuf)
  4327. org-last-indirect-buffer ibuf)
  4328. (cond
  4329. ((or (eq org-indirect-buffer-display 'new-frame)
  4330. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4331. (select-frame (make-frame))
  4332. (delete-other-windows)
  4333. (switch-to-buffer ibuf)
  4334. (org-set-frame-title heading))
  4335. ((eq org-indirect-buffer-display 'dedicated-frame)
  4336. (raise-frame
  4337. (select-frame (or (and org-indirect-dedicated-frame
  4338. (frame-live-p org-indirect-dedicated-frame)
  4339. org-indirect-dedicated-frame)
  4340. (setq org-indirect-dedicated-frame (make-frame)))))
  4341. (delete-other-windows)
  4342. (switch-to-buffer ibuf)
  4343. (org-set-frame-title (concat "Indirect: " heading)))
  4344. ((eq org-indirect-buffer-display 'current-window)
  4345. (switch-to-buffer ibuf))
  4346. ((eq org-indirect-buffer-display 'other-window)
  4347. (pop-to-buffer ibuf))
  4348. (t (error "Invalid value.")))
  4349. (if (featurep 'xemacs)
  4350. (save-excursion (org-mode) (turn-on-font-lock)))
  4351. (narrow-to-region beg end)
  4352. (show-all)
  4353. (goto-char pos)
  4354. (and (window-live-p cwin) (select-window cwin))))
  4355. (defun org-get-indirect-buffer (&optional buffer)
  4356. (setq buffer (or buffer (current-buffer)))
  4357. (let ((n 1) (base (buffer-name buffer)) bname)
  4358. (while (buffer-live-p
  4359. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4360. (setq n (1+ n)))
  4361. (condition-case nil
  4362. (make-indirect-buffer buffer bname 'clone)
  4363. (error (make-indirect-buffer buffer bname)))))
  4364. (defun org-set-frame-title (title)
  4365. "Set the title of the current frame to the string TITLE."
  4366. ;; FIXME: how to name a single frame in XEmacs???
  4367. (unless (featurep 'xemacs)
  4368. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4369. ;;;; Structure editing
  4370. ;;; Inserting headlines
  4371. (defun org-previous-line-empty-p ()
  4372. (save-excursion
  4373. (and (not (bobp))
  4374. (or (beginning-of-line 0) t)
  4375. (save-match-data
  4376. (looking-at "[ \t]*$")))))
  4377. (defun org-insert-heading (&optional force-heading)
  4378. "Insert a new heading or item with same depth at point.
  4379. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4380. If point is at the beginning of a headline, insert a sibling before the
  4381. current headline. If point is not at the beginning, do not split the line,
  4382. but create the new headline after the current line."
  4383. (interactive "P")
  4384. (if (= (buffer-size) 0)
  4385. (insert "\n* ")
  4386. (when (or force-heading (not (org-insert-item)))
  4387. (let* ((empty-line-p nil)
  4388. (head (save-excursion
  4389. (condition-case nil
  4390. (progn
  4391. (org-back-to-heading)
  4392. (setq empty-line-p (org-previous-line-empty-p))
  4393. (match-string 0))
  4394. (error "*"))))
  4395. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4396. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4397. pos hide-previous previous-pos)
  4398. (cond
  4399. ((and (org-on-heading-p) (bolp)
  4400. (or (bobp)
  4401. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4402. ;; insert before the current line
  4403. (open-line (if blank 2 1)))
  4404. ((and (bolp)
  4405. (or (bobp)
  4406. (save-excursion
  4407. (backward-char 1) (not (org-invisible-p)))))
  4408. ;; insert right here
  4409. nil)
  4410. (t
  4411. ;; somewhere in the line
  4412. (save-excursion
  4413. (setq previous-pos (point-at-bol))
  4414. (end-of-line)
  4415. (setq hide-previous (org-invisible-p)))
  4416. (and org-insert-heading-respect-content (org-show-subtree))
  4417. (let ((split
  4418. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4419. (save-excursion
  4420. (let ((p (point)))
  4421. (goto-char (point-at-bol))
  4422. (and (looking-at org-complex-heading-regexp)
  4423. (> p (match-beginning 4)))))))
  4424. tags pos)
  4425. (cond
  4426. (org-insert-heading-respect-content
  4427. (org-end-of-subtree nil t)
  4428. (or (bolp) (newline))
  4429. (or (org-previous-line-empty-p)
  4430. (and blank (newline)))
  4431. (open-line 1))
  4432. ((org-on-heading-p)
  4433. (when hide-previous
  4434. (show-children)
  4435. (org-show-entry))
  4436. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4437. (setq tags (and (match-end 2) (match-string 2)))
  4438. (and (match-end 1)
  4439. (delete-region (match-beginning 1) (match-end 1)))
  4440. (setq pos (point-at-bol))
  4441. (or split (end-of-line 1))
  4442. (delete-horizontal-space)
  4443. (newline (if blank 2 1))
  4444. (when tags
  4445. (save-excursion
  4446. (goto-char pos)
  4447. (end-of-line 1)
  4448. (insert " " tags)
  4449. (org-set-tags nil 'align))))
  4450. (t
  4451. (or split (end-of-line 1))
  4452. (newline (if blank 2 1)))))))
  4453. (insert head) (just-one-space)
  4454. (setq pos (point))
  4455. (end-of-line 1)
  4456. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4457. (when (and org-insert-heading-respect-content hide-previous)
  4458. (save-excursion
  4459. (goto-char previous-pos)
  4460. (hide-subtree)))
  4461. (run-hooks 'org-insert-heading-hook)))))
  4462. (defun org-get-heading (&optional no-tags)
  4463. "Return the heading of the current entry, without the stars."
  4464. (save-excursion
  4465. (org-back-to-heading t)
  4466. (if (looking-at
  4467. (if no-tags
  4468. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4469. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4470. (match-string 1) "")))
  4471. (defun org-heading-components ()
  4472. "Return the components of the current heading.
  4473. This is a list with the following elements:
  4474. - the level as an integer
  4475. - the reduced level, different if `org-odd-levels-only' is set.
  4476. - the TODO keyword, or nil
  4477. - the priority character, like ?A, or nil if no priority is given
  4478. - the headline text itself, or the tags string if no headline text
  4479. - the tags string, or nil."
  4480. (save-excursion
  4481. (org-back-to-heading t)
  4482. (if (looking-at org-complex-heading-regexp)
  4483. (list (length (match-string 1))
  4484. (org-reduced-level (length (match-string 1)))
  4485. (org-match-string-no-properties 2)
  4486. (and (match-end 3) (aref (match-string 3) 2))
  4487. (org-match-string-no-properties 4)
  4488. (org-match-string-no-properties 5)))))
  4489. (defun org-insert-heading-after-current ()
  4490. "Insert a new heading with same level as current, after current subtree."
  4491. (interactive)
  4492. (org-back-to-heading)
  4493. (org-insert-heading)
  4494. (org-move-subtree-down)
  4495. (end-of-line 1))
  4496. (defun org-insert-heading-respect-content ()
  4497. (interactive)
  4498. (let ((org-insert-heading-respect-content t))
  4499. (org-insert-heading t)))
  4500. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4501. (interactive "P")
  4502. (let ((org-insert-heading-respect-content t))
  4503. (org-insert-todo-heading force-state t)))
  4504. (defun org-insert-todo-heading (arg &optional force-heading)
  4505. "Insert a new heading with the same level and TODO state as current heading.
  4506. If the heading has no TODO state, or if the state is DONE, use the first
  4507. state (TODO by default). Also with prefix arg, force first state."
  4508. (interactive "P")
  4509. (when (or force-heading (not (org-insert-item 'checkbox)))
  4510. (org-insert-heading force-heading)
  4511. (save-excursion
  4512. (org-back-to-heading)
  4513. (outline-previous-heading)
  4514. (looking-at org-todo-line-regexp))
  4515. (if (or arg
  4516. (not (match-beginning 2))
  4517. (member (match-string 2) org-done-keywords))
  4518. (insert (car org-todo-keywords-1) " ")
  4519. (insert (match-string 2) " "))
  4520. (when org-provide-todo-statistics
  4521. (org-update-parent-todo-statistics))))
  4522. (defun org-insert-subheading (arg)
  4523. "Insert a new subheading and demote it.
  4524. Works for outline headings and for plain lists alike."
  4525. (interactive "P")
  4526. (org-insert-heading arg)
  4527. (cond
  4528. ((org-on-heading-p) (org-do-demote))
  4529. ((org-at-item-p) (org-indent-item 1))))
  4530. (defun org-insert-todo-subheading (arg)
  4531. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4532. Works for outline headings and for plain lists alike."
  4533. (interactive "P")
  4534. (org-insert-todo-heading arg)
  4535. (cond
  4536. ((org-on-heading-p) (org-do-demote))
  4537. ((org-at-item-p) (org-indent-item 1))))
  4538. ;;; Promotion and Demotion
  4539. (defun org-promote-subtree ()
  4540. "Promote the entire subtree.
  4541. See also `org-promote'."
  4542. (interactive)
  4543. (save-excursion
  4544. (org-map-tree 'org-promote))
  4545. (org-fix-position-after-promote))
  4546. (defun org-demote-subtree ()
  4547. "Demote the entire subtree. See `org-demote'.
  4548. See also `org-promote'."
  4549. (interactive)
  4550. (save-excursion
  4551. (org-map-tree 'org-demote))
  4552. (org-fix-position-after-promote))
  4553. (defun org-do-promote ()
  4554. "Promote the current heading higher up the tree.
  4555. If the region is active in `transient-mark-mode', promote all headings
  4556. in the region."
  4557. (interactive)
  4558. (save-excursion
  4559. (if (org-region-active-p)
  4560. (org-map-region 'org-promote (region-beginning) (region-end))
  4561. (org-promote)))
  4562. (org-fix-position-after-promote))
  4563. (defun org-do-demote ()
  4564. "Demote the current heading lower down the tree.
  4565. If the region is active in `transient-mark-mode', demote all headings
  4566. in the region."
  4567. (interactive)
  4568. (save-excursion
  4569. (if (org-region-active-p)
  4570. (org-map-region 'org-demote (region-beginning) (region-end))
  4571. (org-demote)))
  4572. (org-fix-position-after-promote))
  4573. (defun org-fix-position-after-promote ()
  4574. "Make sure that after pro/demotion cursor position is right."
  4575. (let ((pos (point)))
  4576. (when (save-excursion
  4577. (beginning-of-line 1)
  4578. (looking-at org-todo-line-regexp)
  4579. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4580. (cond ((eobp) (insert " "))
  4581. ((eolp) (insert " "))
  4582. ((equal (char-after) ?\ ) (forward-char 1))))))
  4583. (defun org-reduced-level (l)
  4584. "Compute the effective level of a heading.
  4585. This takes into account the setting of `org-odd-levels-only'."
  4586. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4587. (defun org-get-valid-level (level &optional change)
  4588. "Rectify a level change under the influence of `org-odd-levels-only'
  4589. LEVEL is a current level, CHANGE is by how much the level should be
  4590. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4591. even level numbers will become the next higher odd number."
  4592. (if org-odd-levels-only
  4593. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4594. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4595. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4596. (max 1 (+ level change))))
  4597. (if (boundp 'define-obsolete-function-alias)
  4598. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4599. (define-obsolete-function-alias 'org-get-legal-level
  4600. 'org-get-valid-level)
  4601. (define-obsolete-function-alias 'org-get-legal-level
  4602. 'org-get-valid-level "23.1")))
  4603. (defun org-promote ()
  4604. "Promote the current heading higher up the tree.
  4605. If the region is active in `transient-mark-mode', promote all headings
  4606. in the region."
  4607. (org-back-to-heading t)
  4608. (let* ((level (save-match-data (funcall outline-level)))
  4609. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4610. (diff (abs (- level (length up-head) -1))))
  4611. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4612. (replace-match up-head nil t)
  4613. ;; Fixup tag positioning
  4614. (and org-auto-align-tags (org-set-tags nil t))
  4615. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4616. (defun org-demote ()
  4617. "Demote the current heading lower down the tree.
  4618. If the region is active in `transient-mark-mode', demote all headings
  4619. in the region."
  4620. (org-back-to-heading t)
  4621. (let* ((level (save-match-data (funcall outline-level)))
  4622. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4623. (diff (abs (- level (length down-head) -1))))
  4624. (replace-match down-head nil t)
  4625. ;; Fixup tag positioning
  4626. (and org-auto-align-tags (org-set-tags nil t))
  4627. (if org-adapt-indentation (org-fixup-indentation diff))))
  4628. (defun org-map-tree (fun)
  4629. "Call FUN for every heading underneath the current one."
  4630. (org-back-to-heading)
  4631. (let ((level (funcall outline-level)))
  4632. (save-excursion
  4633. (funcall fun)
  4634. (while (and (progn
  4635. (outline-next-heading)
  4636. (> (funcall outline-level) level))
  4637. (not (eobp)))
  4638. (funcall fun)))))
  4639. (defun org-map-region (fun beg end)
  4640. "Call FUN for every heading between BEG and END."
  4641. (let ((org-ignore-region t))
  4642. (save-excursion
  4643. (setq end (copy-marker end))
  4644. (goto-char beg)
  4645. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4646. (< (point) end))
  4647. (funcall fun))
  4648. (while (and (progn
  4649. (outline-next-heading)
  4650. (< (point) end))
  4651. (not (eobp)))
  4652. (funcall fun)))))
  4653. (defun org-fixup-indentation (diff)
  4654. "Change the indentation in the current entry by DIFF
  4655. However, if any line in the current entry has no indentation, or if it
  4656. would end up with no indentation after the change, nothing at all is done."
  4657. (save-excursion
  4658. (let ((end (save-excursion (outline-next-heading)
  4659. (point-marker)))
  4660. (prohibit (if (> diff 0)
  4661. "^\\S-"
  4662. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4663. col)
  4664. (unless (save-excursion (end-of-line 1)
  4665. (re-search-forward prohibit end t))
  4666. (while (and (< (point) end)
  4667. (re-search-forward "^[ \t]+" end t))
  4668. (goto-char (match-end 0))
  4669. (setq col (current-column))
  4670. (if (< diff 0) (replace-match ""))
  4671. (org-indent-to-column (+ diff col))))
  4672. (move-marker end nil))))
  4673. (defun org-convert-to-odd-levels ()
  4674. "Convert an org-mode file with all levels allowed to one with odd levels.
  4675. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4676. level 5 etc."
  4677. (interactive)
  4678. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4679. (let ((org-odd-levels-only nil) n)
  4680. (save-excursion
  4681. (goto-char (point-min))
  4682. (while (re-search-forward "^\\*\\*+ " nil t)
  4683. (setq n (- (length (match-string 0)) 2))
  4684. (while (>= (setq n (1- n)) 0)
  4685. (org-demote))
  4686. (end-of-line 1))))))
  4687. (defun org-convert-to-oddeven-levels ()
  4688. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4689. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4690. section with an even level, conversion would destroy the structure of the file. An error
  4691. is signaled in this case."
  4692. (interactive)
  4693. (goto-char (point-min))
  4694. ;; First check if there are no even levels
  4695. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4696. (org-show-context t)
  4697. (error "Not all levels are odd in this file. Conversion not possible."))
  4698. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4699. (let ((org-odd-levels-only nil) n)
  4700. (save-excursion
  4701. (goto-char (point-min))
  4702. (while (re-search-forward "^\\*\\*+ " nil t)
  4703. (setq n (/ (1- (length (match-string 0))) 2))
  4704. (while (>= (setq n (1- n)) 0)
  4705. (org-promote))
  4706. (end-of-line 1))))))
  4707. (defun org-tr-level (n)
  4708. "Make N odd if required."
  4709. (if org-odd-levels-only (1+ (/ n 2)) n))
  4710. ;;; Vertical tree motion, cutting and pasting of subtrees
  4711. (defun org-move-subtree-up (&optional arg)
  4712. "Move the current subtree up past ARG headlines of the same level."
  4713. (interactive "p")
  4714. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4715. (defun org-move-subtree-down (&optional arg)
  4716. "Move the current subtree down past ARG headlines of the same level."
  4717. (interactive "p")
  4718. (setq arg (prefix-numeric-value arg))
  4719. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4720. 'outline-get-last-sibling))
  4721. (ins-point (make-marker))
  4722. (cnt (abs arg))
  4723. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4724. ;; Select the tree
  4725. (org-back-to-heading)
  4726. (setq beg0 (point))
  4727. (save-excursion
  4728. (setq ne-beg (org-back-over-empty-lines))
  4729. (setq beg (point)))
  4730. (save-match-data
  4731. (save-excursion (outline-end-of-heading)
  4732. (setq folded (org-invisible-p)))
  4733. (outline-end-of-subtree))
  4734. (outline-next-heading)
  4735. (setq ne-end (org-back-over-empty-lines))
  4736. (setq end (point))
  4737. (goto-char beg0)
  4738. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4739. ;; include less whitespace
  4740. (save-excursion
  4741. (goto-char beg)
  4742. (forward-line (- ne-beg ne-end))
  4743. (setq beg (point))))
  4744. ;; Find insertion point, with error handling
  4745. (while (> cnt 0)
  4746. (or (and (funcall movfunc) (looking-at outline-regexp))
  4747. (progn (goto-char beg0)
  4748. (error "Cannot move past superior level or buffer limit")))
  4749. (setq cnt (1- cnt)))
  4750. (if (> arg 0)
  4751. ;; Moving forward - still need to move over subtree
  4752. (progn (org-end-of-subtree t t)
  4753. (save-excursion
  4754. (org-back-over-empty-lines)
  4755. (or (bolp) (newline)))))
  4756. (setq ne-ins (org-back-over-empty-lines))
  4757. (move-marker ins-point (point))
  4758. (setq txt (buffer-substring beg end))
  4759. (org-save-markers-in-region beg end)
  4760. (delete-region beg end)
  4761. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4762. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4763. (let ((bbb (point)))
  4764. (insert-before-markers txt)
  4765. (org-reinstall-markers-in-region bbb)
  4766. (move-marker ins-point bbb))
  4767. (or (bolp) (insert "\n"))
  4768. (setq ins-end (point))
  4769. (goto-char ins-point)
  4770. (org-skip-whitespace)
  4771. (when (and (< arg 0)
  4772. (org-first-sibling-p)
  4773. (> ne-ins ne-beg))
  4774. ;; Move whitespace back to beginning
  4775. (save-excursion
  4776. (goto-char ins-end)
  4777. (let ((kill-whole-line t))
  4778. (kill-line (- ne-ins ne-beg)) (point)))
  4779. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4780. (move-marker ins-point nil)
  4781. (org-compact-display-after-subtree-move)
  4782. (org-show-empty-lines-in-parent)
  4783. (unless folded
  4784. (org-show-entry)
  4785. (show-children)
  4786. (org-cycle-hide-drawers 'children))))
  4787. (defvar org-subtree-clip ""
  4788. "Clipboard for cut and paste of subtrees.
  4789. This is actually only a copy of the kill, because we use the normal kill
  4790. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4791. (defvar org-subtree-clip-folded nil
  4792. "Was the last copied subtree folded?
  4793. This is used to fold the tree back after pasting.")
  4794. (defun org-cut-subtree (&optional n)
  4795. "Cut the current subtree into the clipboard.
  4796. With prefix arg N, cut this many sequential subtrees.
  4797. This is a short-hand for marking the subtree and then cutting it."
  4798. (interactive "p")
  4799. (org-copy-subtree n 'cut))
  4800. (defun org-copy-subtree (&optional n cut force-store-markers)
  4801. "Cut the current subtree into the clipboard.
  4802. With prefix arg N, cut this many sequential subtrees.
  4803. This is a short-hand for marking the subtree and then copying it.
  4804. If CUT is non-nil, actually cut the subtree.
  4805. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4806. of some markers in the region, even if CUT is non-nil. This is
  4807. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4808. (interactive "p")
  4809. (let (beg end folded (beg0 (point)))
  4810. (if (interactive-p)
  4811. (org-back-to-heading nil) ; take what looks like a subtree
  4812. (org-back-to-heading t)) ; take what is really there
  4813. (org-back-over-empty-lines)
  4814. (setq beg (point))
  4815. (skip-chars-forward " \t\r\n")
  4816. (save-match-data
  4817. (save-excursion (outline-end-of-heading)
  4818. (setq folded (org-invisible-p)))
  4819. (condition-case nil
  4820. (outline-forward-same-level (1- n))
  4821. (error nil))
  4822. (org-end-of-subtree t t))
  4823. (org-back-over-empty-lines)
  4824. (setq end (point))
  4825. (goto-char beg0)
  4826. (when (> end beg)
  4827. (setq org-subtree-clip-folded folded)
  4828. (when (or cut force-store-markers)
  4829. (org-save-markers-in-region beg end))
  4830. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4831. (setq org-subtree-clip (current-kill 0))
  4832. (message "%s: Subtree(s) with %d characters"
  4833. (if cut "Cut" "Copied")
  4834. (length org-subtree-clip)))))
  4835. (defun org-paste-subtree (&optional level tree for-yank)
  4836. "Paste the clipboard as a subtree, with modification of headline level.
  4837. The entire subtree is promoted or demoted in order to match a new headline
  4838. level.
  4839. If the cursor is at the beginning of a headline, the same level as
  4840. that headline is used to paste the tree
  4841. If not, the new level is derived from the *visible* headings
  4842. before and after the insertion point, and taken to be the inferior headline
  4843. level of the two. So if the previous visible heading is level 3 and the
  4844. next is level 4 (or vice versa), level 4 will be used for insertion.
  4845. This makes sure that the subtree remains an independent subtree and does
  4846. not swallow low level entries.
  4847. You can also force a different level, either by using a numeric prefix
  4848. argument, or by inserting the heading marker by hand. For example, if the
  4849. cursor is after \"*****\", then the tree will be shifted to level 5.
  4850. If optional TREE is given, use this text instead of the kill ring.
  4851. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4852. move back over whitespace before inserting, and move point to the end of
  4853. the inserted text when done."
  4854. (interactive "P")
  4855. (unless (org-kill-is-subtree-p tree)
  4856. (error "%s"
  4857. (substitute-command-keys
  4858. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4859. (let* ((visp (not (org-invisible-p)))
  4860. (txt (or tree (and kill-ring (current-kill 0))))
  4861. (^re (concat "^\\(" outline-regexp "\\)"))
  4862. (re (concat "\\(" outline-regexp "\\)"))
  4863. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4864. (old-level (if (string-match ^re txt)
  4865. (- (match-end 0) (match-beginning 0) 1)
  4866. -1))
  4867. (force-level (cond (level (prefix-numeric-value level))
  4868. ((and (looking-at "[ \t]*$")
  4869. (string-match
  4870. ^re_ (buffer-substring
  4871. (point-at-bol) (point))))
  4872. (- (match-end 1) (match-beginning 1)))
  4873. ((and (bolp)
  4874. (looking-at org-outline-regexp))
  4875. (- (match-end 0) (point) 1))
  4876. (t nil)))
  4877. (previous-level (save-excursion
  4878. (condition-case nil
  4879. (progn
  4880. (outline-previous-visible-heading 1)
  4881. (if (looking-at re)
  4882. (- (match-end 0) (match-beginning 0) 1)
  4883. 1))
  4884. (error 1))))
  4885. (next-level (save-excursion
  4886. (condition-case nil
  4887. (progn
  4888. (or (looking-at outline-regexp)
  4889. (outline-next-visible-heading 1))
  4890. (if (looking-at re)
  4891. (- (match-end 0) (match-beginning 0) 1)
  4892. 1))
  4893. (error 1))))
  4894. (new-level (or force-level (max previous-level next-level)))
  4895. (shift (if (or (= old-level -1)
  4896. (= new-level -1)
  4897. (= old-level new-level))
  4898. 0
  4899. (- new-level old-level)))
  4900. (delta (if (> shift 0) -1 1))
  4901. (func (if (> shift 0) 'org-demote 'org-promote))
  4902. (org-odd-levels-only nil)
  4903. beg end newend)
  4904. ;; Remove the forced level indicator
  4905. (if force-level
  4906. (delete-region (point-at-bol) (point)))
  4907. ;; Paste
  4908. (beginning-of-line 1)
  4909. (unless for-yank (org-back-over-empty-lines))
  4910. (setq beg (point))
  4911. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4912. (insert-before-markers txt)
  4913. (unless (string-match "\n\\'" txt) (insert "\n"))
  4914. (setq newend (point))
  4915. (org-reinstall-markers-in-region beg)
  4916. (setq end (point))
  4917. (goto-char beg)
  4918. (skip-chars-forward " \t\n\r")
  4919. (setq beg (point))
  4920. (if (and (org-invisible-p) visp)
  4921. (save-excursion (outline-show-heading)))
  4922. ;; Shift if necessary
  4923. (unless (= shift 0)
  4924. (save-restriction
  4925. (narrow-to-region beg end)
  4926. (while (not (= shift 0))
  4927. (org-map-region func (point-min) (point-max))
  4928. (setq shift (+ delta shift)))
  4929. (goto-char (point-min))
  4930. (setq newend (point-max))))
  4931. (when (or (interactive-p) for-yank)
  4932. (message "Clipboard pasted as level %d subtree" new-level))
  4933. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4934. kill-ring
  4935. (eq org-subtree-clip (current-kill 0))
  4936. org-subtree-clip-folded)
  4937. ;; The tree was folded before it was killed/copied
  4938. (hide-subtree))
  4939. (and for-yank (goto-char newend))))
  4940. (defun org-kill-is-subtree-p (&optional txt)
  4941. "Check if the current kill is an outline subtree, or a set of trees.
  4942. Returns nil if kill does not start with a headline, or if the first
  4943. headline level is not the largest headline level in the tree.
  4944. So this will actually accept several entries of equal levels as well,
  4945. which is OK for `org-paste-subtree'.
  4946. If optional TXT is given, check this string instead of the current kill."
  4947. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4948. (start-level (and kill
  4949. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4950. org-outline-regexp "\\)")
  4951. kill)
  4952. (- (match-end 2) (match-beginning 2) 1)))
  4953. (re (concat "^" org-outline-regexp))
  4954. (start (1+ (or (match-beginning 2) -1))))
  4955. (if (not start-level)
  4956. (progn
  4957. nil) ;; does not even start with a heading
  4958. (catch 'exit
  4959. (while (setq start (string-match re kill (1+ start)))
  4960. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4961. (throw 'exit nil)))
  4962. t))))
  4963. (defvar org-markers-to-move nil
  4964. "Markers that should be moved with a cut-and-paste operation.
  4965. Those markers are stored together with their positions relative to
  4966. the start of the region.")
  4967. (defun org-save-markers-in-region (beg end)
  4968. "Check markers in region.
  4969. If these markers are between BEG and END, record their position relative
  4970. to BEG, so that after moving the block of text, we can put the markers back
  4971. into place.
  4972. This function gets called just before an entry or tree gets cut from the
  4973. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4974. called immediately, to move the markers with the entries."
  4975. (setq org-markers-to-move nil)
  4976. (when (featurep 'org-clock)
  4977. (org-clock-save-markers-for-cut-and-paste beg end))
  4978. (when (featurep 'org-agenda)
  4979. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4980. (defun org-check-and-save-marker (marker beg end)
  4981. "Check if MARKER is between BEG and END.
  4982. If yes, remember the marker and the distance to BEG."
  4983. (when (and (marker-buffer marker)
  4984. (equal (marker-buffer marker) (current-buffer)))
  4985. (if (and (>= marker beg) (< marker end))
  4986. (push (cons marker (- marker beg)) org-markers-to-move))))
  4987. (defun org-reinstall-markers-in-region (beg)
  4988. "Move all remembered markers to their position relative to BEG."
  4989. (mapc (lambda (x)
  4990. (move-marker (car x) (+ beg (cdr x))))
  4991. org-markers-to-move)
  4992. (setq org-markers-to-move nil))
  4993. (defun org-narrow-to-subtree ()
  4994. "Narrow buffer to the current subtree."
  4995. (interactive)
  4996. (save-excursion
  4997. (save-match-data
  4998. (narrow-to-region
  4999. (progn (org-back-to-heading) (point))
  5000. (progn (org-end-of-subtree t) (point))))))
  5001. ;;; Outline Sorting
  5002. (defun org-sort (with-case)
  5003. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5004. Optional argument WITH-CASE means sort case-sensitively."
  5005. (interactive "P")
  5006. (if (org-at-table-p)
  5007. (org-call-with-arg 'org-table-sort-lines with-case)
  5008. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5009. (defun org-sort-remove-invisible (s)
  5010. (remove-text-properties 0 (length s) org-rm-props s)
  5011. (while (string-match org-bracket-link-regexp s)
  5012. (setq s (replace-match (if (match-end 2)
  5013. (match-string 3 s)
  5014. (match-string 1 s)) t t s)))
  5015. s)
  5016. (defvar org-priority-regexp) ; defined later in the file
  5017. (defun org-sort-entries-or-items
  5018. (&optional with-case sorting-type getkey-func compare-func property)
  5019. "Sort entries on a certain level of an outline tree.
  5020. If there is an active region, the entries in the region are sorted.
  5021. Else, if the cursor is before the first entry, sort the top-level items.
  5022. Else, the children of the entry at point are sorted.
  5023. Sorting can be alphabetically, numerically, and by date/time as given by
  5024. the first time stamp in the entry. The command prompts for the sorting
  5025. type unless it has been given to the function through the SORTING-TYPE
  5026. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5027. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5028. called with point at the beginning of the record. It must return either
  5029. a string or a number that should serve as the sorting key for that record.
  5030. Comparing entries ignores case by default. However, with an optional argument
  5031. WITH-CASE, the sorting considers case as well."
  5032. (interactive "P")
  5033. (let ((case-func (if with-case 'identity 'downcase))
  5034. start beg end stars re re2
  5035. txt what tmp plain-list-p)
  5036. ;; Find beginning and end of region to sort
  5037. (cond
  5038. ((org-region-active-p)
  5039. ;; we will sort the region
  5040. (setq end (region-end)
  5041. what "region")
  5042. (goto-char (region-beginning))
  5043. (if (not (org-on-heading-p)) (outline-next-heading))
  5044. (setq start (point)))
  5045. ((org-at-item-p)
  5046. ;; we will sort this plain list
  5047. (org-beginning-of-item-list) (setq start (point))
  5048. (org-end-of-item-list) (setq end (point))
  5049. (goto-char start)
  5050. (setq plain-list-p t
  5051. what "plain list"))
  5052. ((or (org-on-heading-p)
  5053. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5054. ;; we will sort the children of the current headline
  5055. (org-back-to-heading)
  5056. (setq start (point)
  5057. end (progn (org-end-of-subtree t t)
  5058. (org-back-over-empty-lines)
  5059. (point))
  5060. what "children")
  5061. (goto-char start)
  5062. (show-subtree)
  5063. (outline-next-heading))
  5064. (t
  5065. ;; we will sort the top-level entries in this file
  5066. (goto-char (point-min))
  5067. (or (org-on-heading-p) (outline-next-heading))
  5068. (setq start (point) end (point-max) what "top-level")
  5069. (goto-char start)
  5070. (show-all)))
  5071. (setq beg (point))
  5072. (if (>= beg end) (error "Nothing to sort"))
  5073. (unless plain-list-p
  5074. (looking-at "\\(\\*+\\)")
  5075. (setq stars (match-string 1)
  5076. re (concat "^" (regexp-quote stars) " +")
  5077. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5078. txt (buffer-substring beg end))
  5079. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5080. (if (and (not (equal stars "*")) (string-match re2 txt))
  5081. (error "Region to sort contains a level above the first entry")))
  5082. (unless sorting-type
  5083. (message
  5084. (if plain-list-p
  5085. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5086. "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:")
  5087. what)
  5088. (setq sorting-type (read-char-exclusive))
  5089. (and (= (downcase sorting-type) ?f)
  5090. (setq getkey-func
  5091. (org-ido-completing-read "Sort using function: "
  5092. obarray 'fboundp t nil nil))
  5093. (setq getkey-func (intern getkey-func)))
  5094. (and (= (downcase sorting-type) ?r)
  5095. (setq property
  5096. (org-ido-completing-read "Property: "
  5097. (mapcar 'list (org-buffer-property-keys t))
  5098. nil t))))
  5099. (message "Sorting entries...")
  5100. (save-restriction
  5101. (narrow-to-region start end)
  5102. (let ((dcst (downcase sorting-type))
  5103. (now (current-time)))
  5104. (sort-subr
  5105. (/= dcst sorting-type)
  5106. ;; This function moves to the beginning character of the "record" to
  5107. ;; be sorted.
  5108. (if plain-list-p
  5109. (lambda nil
  5110. (if (org-at-item-p) t (goto-char (point-max))))
  5111. (lambda nil
  5112. (if (re-search-forward re nil t)
  5113. (goto-char (match-beginning 0))
  5114. (goto-char (point-max)))))
  5115. ;; This function moves to the last character of the "record" being
  5116. ;; sorted.
  5117. (if plain-list-p
  5118. 'org-end-of-item
  5119. (lambda nil
  5120. (save-match-data
  5121. (condition-case nil
  5122. (outline-forward-same-level 1)
  5123. (error
  5124. (goto-char (point-max)))))))
  5125. ;; This function returns the value that gets sorted against.
  5126. (if plain-list-p
  5127. (lambda nil
  5128. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5129. (cond
  5130. ((= dcst ?n)
  5131. (string-to-number (buffer-substring (match-end 0)
  5132. (point-at-eol))))
  5133. ((= dcst ?a)
  5134. (buffer-substring (match-end 0) (point-at-eol)))
  5135. ((= dcst ?t)
  5136. (if (re-search-forward org-ts-regexp
  5137. (point-at-eol) t)
  5138. (org-time-string-to-time (match-string 0))
  5139. now))
  5140. ((= dcst ?f)
  5141. (if getkey-func
  5142. (progn
  5143. (setq tmp (funcall getkey-func))
  5144. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5145. tmp)
  5146. (error "Invalid key function `%s'" getkey-func)))
  5147. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5148. (lambda nil
  5149. (cond
  5150. ((= dcst ?n)
  5151. (if (looking-at org-complex-heading-regexp)
  5152. (string-to-number (match-string 4))
  5153. nil))
  5154. ((= dcst ?a)
  5155. (if (looking-at org-complex-heading-regexp)
  5156. (funcall case-func (match-string 4))
  5157. nil))
  5158. ((= dcst ?t)
  5159. (if (re-search-forward org-ts-regexp
  5160. (save-excursion
  5161. (forward-line 2)
  5162. (point)) t)
  5163. (org-time-string-to-time (match-string 0))
  5164. now))
  5165. ((= dcst ?p)
  5166. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5167. (string-to-char (match-string 2))
  5168. org-default-priority))
  5169. ((= dcst ?r)
  5170. (or (org-entry-get nil property) ""))
  5171. ((= dcst ?o)
  5172. (if (looking-at org-complex-heading-regexp)
  5173. (- 9999 (length (member (match-string 2)
  5174. org-todo-keywords-1)))))
  5175. ((= dcst ?f)
  5176. (if getkey-func
  5177. (progn
  5178. (setq tmp (funcall getkey-func))
  5179. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5180. tmp)
  5181. (error "Invalid key function `%s'" getkey-func)))
  5182. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5183. nil
  5184. (cond
  5185. ((= dcst ?a) 'string<)
  5186. ((= dcst ?t) 'time-less-p)
  5187. ((= dcst ?f) compare-func)
  5188. (t nil)))))
  5189. (message "Sorting entries...done")))
  5190. (defun org-do-sort (table what &optional with-case sorting-type)
  5191. "Sort TABLE of WHAT according to SORTING-TYPE.
  5192. The user will be prompted for the SORTING-TYPE if the call to this
  5193. function does not specify it. WHAT is only for the prompt, to indicate
  5194. what is being sorted. The sorting key will be extracted from
  5195. the car of the elements of the table.
  5196. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5197. (unless sorting-type
  5198. (message
  5199. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5200. what)
  5201. (setq sorting-type (read-char-exclusive)))
  5202. (let ((dcst (downcase sorting-type))
  5203. extractfun comparefun)
  5204. ;; Define the appropriate functions
  5205. (cond
  5206. ((= dcst ?n)
  5207. (setq extractfun 'string-to-number
  5208. comparefun (if (= dcst sorting-type) '< '>)))
  5209. ((= dcst ?a)
  5210. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5211. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5212. comparefun (if (= dcst sorting-type)
  5213. 'string<
  5214. (lambda (a b) (and (not (string< a b))
  5215. (not (string= a b)))))))
  5216. ((= dcst ?t)
  5217. (setq extractfun
  5218. (lambda (x)
  5219. (if (string-match org-ts-regexp x)
  5220. (time-to-seconds
  5221. (org-time-string-to-time (match-string 0 x)))
  5222. 0))
  5223. comparefun (if (= dcst sorting-type) '< '>)))
  5224. (t (error "Invalid sorting type `%c'" sorting-type)))
  5225. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5226. table)
  5227. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5228. ;;; Editing source examples
  5229. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5230. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5231. (defvar org-edit-src-force-single-line nil)
  5232. (defvar org-edit-src-from-org-mode nil)
  5233. (defvar org-edit-src-picture nil)
  5234. (define-minor-mode org-exit-edit-mode
  5235. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5236. (defun org-edit-src-code ()
  5237. "Edit the source code example at point.
  5238. An indirect buffer is created, and that buffer is then narrowed to the
  5239. example at point and switched to the correct language mode. When done,
  5240. exit by killing the buffer with \\[org-edit-src-exit]."
  5241. (interactive)
  5242. (let ((line (org-current-line))
  5243. (case-fold-search t)
  5244. (msg (substitute-command-keys
  5245. "Edit, then exit with C-c ' (C-c and single quote)"))
  5246. (info (org-edit-src-find-region-and-lang))
  5247. (org-mode-p (eq major-mode 'org-mode))
  5248. beg end lang lang-f single lfmt)
  5249. (if (not info)
  5250. nil
  5251. (setq beg (nth 0 info)
  5252. end (nth 1 info)
  5253. lang (nth 2 info)
  5254. single (nth 3 info)
  5255. lfmt (nth 4 info)
  5256. lang-f (intern (concat lang "-mode")))
  5257. (unless (functionp lang-f)
  5258. (error "No such language mode: %s" lang-f))
  5259. (goto-line line)
  5260. (if (get-buffer "*Org Edit Src Example*")
  5261. (kill-buffer "*Org Edit Src Example*"))
  5262. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5263. "*Org Edit Src Example*"))
  5264. (narrow-to-region beg end)
  5265. (remove-text-properties beg end '(display nil invisible nil
  5266. intangible nil))
  5267. (let ((org-inhibit-startup t))
  5268. (funcall lang-f))
  5269. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5270. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5271. (when lfmt
  5272. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5273. (when org-mode-p
  5274. (goto-char (point-min))
  5275. (while (re-search-forward "^," nil t)
  5276. (replace-match "")))
  5277. (goto-line line)
  5278. (org-exit-edit-mode)
  5279. (org-set-local 'header-line-format msg)
  5280. (message "%s" msg)
  5281. t)))
  5282. (defun org-edit-fixed-width-region ()
  5283. "Edit the fixed-width ascii drawing at point.
  5284. This must be a region where each line starts with a colon followed by
  5285. a space character.
  5286. An indirect buffer is created, and that buffer is then narrowed to the
  5287. example at point and switched to artist-mode. When done,
  5288. exit by killing the buffer with \\[org-edit-src-exit]."
  5289. (interactive)
  5290. (let ((line (org-current-line))
  5291. (case-fold-search t)
  5292. (msg (substitute-command-keys
  5293. "Edit, then exit with C-c ' (C-c and single quote)"))
  5294. (org-mode-p (eq major-mode 'org-mode))
  5295. beg end)
  5296. (beginning-of-line 1)
  5297. (if (looking-at "[ \t]*[^:\n \t]")
  5298. nil
  5299. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5300. (setq beg (point) end beg)
  5301. (save-excursion
  5302. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5303. (setq beg (point-at-bol 2))
  5304. (setq beg (point))))
  5305. (save-excursion
  5306. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5307. (setq end (1- (match-beginning 0)))
  5308. (setq end (point))))
  5309. (goto-line line))
  5310. (if (get-buffer "*Org Edit Picture*")
  5311. (kill-buffer "*Org Edit Picture*"))
  5312. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5313. "*Org Edit Picture*"))
  5314. (narrow-to-region beg end)
  5315. (remove-text-properties beg end '(display nil invisible nil
  5316. intangible nil))
  5317. (when (fboundp 'font-lock-unfontify-region)
  5318. (font-lock-unfontify-region (point-min) (point-max)))
  5319. (cond
  5320. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5321. (fundamental-mode)
  5322. (artist-mode 1))
  5323. (t (funcall org-edit-fixed-width-region-mode)))
  5324. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5325. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5326. (set (make-local-variable 'org-edit-src-picture) t)
  5327. (goto-char (point-min))
  5328. (while (re-search-forward "^[ \t]*: ?" nil t)
  5329. (replace-match ""))
  5330. (goto-line line)
  5331. (org-exit-edit-mode)
  5332. (org-set-local 'header-line-format msg)
  5333. (message "%s" msg)
  5334. t)))
  5335. (defun org-edit-src-find-region-and-lang ()
  5336. "Find the region and language for a local edit.
  5337. Return a list with beginning and end of the region, a string representing
  5338. the language, a switch telling of the content should be in a single line."
  5339. (let ((re-list
  5340. (append
  5341. org-edit-src-region-extra
  5342. '(
  5343. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5344. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5345. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5346. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5347. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5348. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5349. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5350. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5351. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5352. ("^#\\+html:" "\n" "html" single-line)
  5353. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5354. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5355. ("^#\\+latex:" "\n" "latex" single-line)
  5356. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5357. ("^#\\+ascii:" "\n" "ascii" single-line)
  5358. )))
  5359. (pos (point))
  5360. re1 re2 single beg end lang lfmt match-re1)
  5361. (catch 'exit
  5362. (while (setq entry (pop re-list))
  5363. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5364. single (nth 3 entry))
  5365. (save-excursion
  5366. (if (or (looking-at re1)
  5367. (re-search-backward re1 nil t))
  5368. (progn
  5369. (setq match-re1 (match-string 0))
  5370. (setq beg (match-end 0)
  5371. lang (org-edit-src-get-lang lang)
  5372. lfmt (org-edit-src-get-label-format match-re1))
  5373. (if (and (re-search-forward re2 nil t)
  5374. (>= (match-end 0) pos))
  5375. (throw 'exit (list beg (match-beginning 0)
  5376. lang single lfmt))))
  5377. (if (or (looking-at re2)
  5378. (re-search-forward re2 nil t))
  5379. (progn
  5380. (setq end (match-beginning 0))
  5381. (if (and (re-search-backward re1 nil t)
  5382. (<= (match-beginning 0) pos))
  5383. (progn
  5384. (setq lfmt (org-edit-src-get-label-format
  5385. (match-string 0)))
  5386. (throw 'exit
  5387. (list (match-end 0) end
  5388. (org-edit-src-get-lang lang)
  5389. single lfmt))))))))))))
  5390. (defun org-edit-src-get-lang (lang)
  5391. "Extract the src language."
  5392. (let ((m (match-string 0)))
  5393. (cond
  5394. ((stringp lang) lang)
  5395. ((integerp lang) (match-string lang))
  5396. ((and (eq lang 'lang)
  5397. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5398. (match-string 1 m))
  5399. ((and (eq lang 'style)
  5400. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5401. (match-string 1 m))
  5402. (t "fundamental"))))
  5403. (defun org-edit-src-get-label-format (s)
  5404. "Extract the label format."
  5405. (save-match-data
  5406. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5407. (match-string 1 s))))
  5408. (defun org-edit-src-exit ()
  5409. "Exit special edit and protect problematic lines."
  5410. (interactive)
  5411. (unless (buffer-base-buffer (current-buffer))
  5412. (error "This is not an indirect buffer, something is wrong..."))
  5413. (unless (> (point-min) 1)
  5414. (error "This buffer is not narrowed, something is wrong..."))
  5415. (goto-char (point-min))
  5416. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5417. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5418. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5419. (goto-char (point-min))
  5420. (while (re-search-forward "\n" nil t)
  5421. (replace-match " "))
  5422. (goto-char (point-min))
  5423. (if (looking-at "\\s-*") (replace-match " "))
  5424. (if (re-search-forward "\\s-+\\'" nil t)
  5425. (replace-match "")))
  5426. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5427. (goto-char (point-min))
  5428. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5429. (replace-match ",\\1"))
  5430. (when font-lock-mode
  5431. (font-lock-unfontify-region (point-min) (point-max)))
  5432. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5433. (when (org-bound-and-true-p org-edit-src-picture)
  5434. (untabify (point-min) (point-max))
  5435. (goto-char (point-min))
  5436. (while (re-search-forward "^" nil t)
  5437. (replace-match ": "))
  5438. (when font-lock-mode
  5439. (font-lock-unfontify-region (point-min) (point-max)))
  5440. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5441. (kill-buffer (current-buffer))
  5442. (and (org-mode-p) (org-restart-font-lock)))
  5443. ;;; The orgstruct minor mode
  5444. ;; Define a minor mode which can be used in other modes in order to
  5445. ;; integrate the org-mode structure editing commands.
  5446. ;; This is really a hack, because the org-mode structure commands use
  5447. ;; keys which normally belong to the major mode. Here is how it
  5448. ;; works: The minor mode defines all the keys necessary to operate the
  5449. ;; structure commands, but wraps the commands into a function which
  5450. ;; tests if the cursor is currently at a headline or a plain list
  5451. ;; item. If that is the case, the structure command is used,
  5452. ;; temporarily setting many Org-mode variables like regular
  5453. ;; expressions for filling etc. However, when any of those keys is
  5454. ;; used at a different location, function uses `key-binding' to look
  5455. ;; up if the key has an associated command in another currently active
  5456. ;; keymap (minor modes, major mode, global), and executes that
  5457. ;; command. There might be problems if any of the keys is otherwise
  5458. ;; used as a prefix key.
  5459. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5460. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5461. ;; addresses this by checking explicitly for both bindings.
  5462. (defvar orgstruct-mode-map (make-sparse-keymap)
  5463. "Keymap for the minor `orgstruct-mode'.")
  5464. (defvar org-local-vars nil
  5465. "List of local variables, for use by `orgstruct-mode'")
  5466. ;;;###autoload
  5467. (define-minor-mode orgstruct-mode
  5468. "Toggle the minor more `orgstruct-mode'.
  5469. This mode is for using Org-mode structure commands in other modes.
  5470. The following key behave as if Org-mode was active, if the cursor
  5471. is on a headline, or on a plain list item (both in the definition
  5472. of Org-mode).
  5473. M-up Move entry/item up
  5474. M-down Move entry/item down
  5475. M-left Promote
  5476. M-right Demote
  5477. M-S-up Move entry/item up
  5478. M-S-down Move entry/item down
  5479. M-S-left Promote subtree
  5480. M-S-right Demote subtree
  5481. M-q Fill paragraph and items like in Org-mode
  5482. C-c ^ Sort entries
  5483. C-c - Cycle list bullet
  5484. TAB Cycle item visibility
  5485. M-RET Insert new heading/item
  5486. S-M-RET Insert new TODO heading / Checkbox item
  5487. C-c C-c Set tags / toggle checkbox"
  5488. nil " OrgStruct" nil
  5489. (org-load-modules-maybe)
  5490. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5491. ;;;###autoload
  5492. (defun turn-on-orgstruct ()
  5493. "Unconditionally turn on `orgstruct-mode'."
  5494. (orgstruct-mode 1))
  5495. ;;;###autoload
  5496. (defun turn-on-orgstruct++ ()
  5497. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5498. In addition to setting orgstruct-mode, this also exports all indentation and
  5499. autofilling variables from org-mode into the buffer. Note that turning
  5500. off orgstruct-mode will *not* remove these additional settings."
  5501. (orgstruct-mode 1)
  5502. (let (var val)
  5503. (mapc
  5504. (lambda (x)
  5505. (when (string-match
  5506. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5507. (symbol-name (car x)))
  5508. (setq var (car x) val (nth 1 x))
  5509. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5510. org-local-vars)))
  5511. (defun orgstruct-error ()
  5512. "Error when there is no default binding for a structure key."
  5513. (interactive)
  5514. (error "This key has no function outside structure elements"))
  5515. (defun orgstruct-setup ()
  5516. "Setup orgstruct keymaps."
  5517. (let ((nfunc 0)
  5518. (bindings
  5519. (list
  5520. '([(meta up)] org-metaup)
  5521. '([(meta down)] org-metadown)
  5522. '([(meta left)] org-metaleft)
  5523. '([(meta right)] org-metaright)
  5524. '([(meta shift up)] org-shiftmetaup)
  5525. '([(meta shift down)] org-shiftmetadown)
  5526. '([(meta shift left)] org-shiftmetaleft)
  5527. '([(meta shift right)] org-shiftmetaright)
  5528. '([(shift up)] org-shiftup)
  5529. '([(shift down)] org-shiftdown)
  5530. '([(shift left)] org-shiftleft)
  5531. '([(shift right)] org-shiftright)
  5532. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5533. '("\M-q" fill-paragraph)
  5534. '("\C-c^" org-sort)
  5535. '("\C-c-" org-cycle-list-bullet)))
  5536. elt key fun cmd)
  5537. (while (setq elt (pop bindings))
  5538. (setq nfunc (1+ nfunc))
  5539. (setq key (org-key (car elt))
  5540. fun (nth 1 elt)
  5541. cmd (orgstruct-make-binding fun nfunc key))
  5542. (org-defkey orgstruct-mode-map key cmd))
  5543. ;; Special treatment needed for TAB and RET
  5544. (org-defkey orgstruct-mode-map [(tab)]
  5545. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5546. (org-defkey orgstruct-mode-map "\C-i"
  5547. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5548. (org-defkey orgstruct-mode-map "\M-\C-m"
  5549. (orgstruct-make-binding 'org-insert-heading 105
  5550. "\M-\C-m" [(meta return)]))
  5551. (org-defkey orgstruct-mode-map [(meta return)]
  5552. (orgstruct-make-binding 'org-insert-heading 106
  5553. [(meta return)] "\M-\C-m"))
  5554. (org-defkey orgstruct-mode-map [(shift meta return)]
  5555. (orgstruct-make-binding 'org-insert-todo-heading 107
  5556. [(meta return)] "\M-\C-m"))
  5557. (unless org-local-vars
  5558. (setq org-local-vars (org-get-local-variables)))
  5559. t))
  5560. (defun orgstruct-make-binding (fun n &rest keys)
  5561. "Create a function for binding in the structure minor mode.
  5562. FUN is the command to call inside a table. N is used to create a unique
  5563. command name. KEYS are keys that should be checked in for a command
  5564. to execute outside of tables."
  5565. (eval
  5566. (list 'defun
  5567. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5568. '(arg)
  5569. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5570. "Outside of structure, run the binding of `"
  5571. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5572. "'.")
  5573. '(interactive "p")
  5574. (list 'if
  5575. '(org-context-p 'headline 'item)
  5576. (list 'org-run-like-in-org-mode (list 'quote fun))
  5577. (list 'let '(orgstruct-mode)
  5578. (list 'call-interactively
  5579. (append '(or)
  5580. (mapcar (lambda (k)
  5581. (list 'key-binding k))
  5582. keys)
  5583. '('orgstruct-error))))))))
  5584. (defun org-context-p (&rest contexts)
  5585. "Check if local context is any of CONTEXTS.
  5586. Possible values in the list of contexts are `table', `headline', and `item'."
  5587. (let ((pos (point)))
  5588. (goto-char (point-at-bol))
  5589. (prog1 (or (and (memq 'table contexts)
  5590. (looking-at "[ \t]*|"))
  5591. (and (memq 'headline contexts)
  5592. ;;????????? (looking-at "\\*+"))
  5593. (looking-at outline-regexp))
  5594. (and (memq 'item contexts)
  5595. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5596. (goto-char pos))))
  5597. (defun org-get-local-variables ()
  5598. "Return a list of all local variables in an org-mode buffer."
  5599. (let (varlist)
  5600. (with-current-buffer (get-buffer-create "*Org tmp*")
  5601. (erase-buffer)
  5602. (org-mode)
  5603. (setq varlist (buffer-local-variables)))
  5604. (kill-buffer "*Org tmp*")
  5605. (delq nil
  5606. (mapcar
  5607. (lambda (x)
  5608. (setq x
  5609. (if (symbolp x)
  5610. (list x)
  5611. (list (car x) (list 'quote (cdr x)))))
  5612. (if (string-match
  5613. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5614. (symbol-name (car x)))
  5615. x nil))
  5616. varlist))))
  5617. ;;;###autoload
  5618. (defun org-run-like-in-org-mode (cmd)
  5619. (org-load-modules-maybe)
  5620. (unless org-local-vars
  5621. (setq org-local-vars (org-get-local-variables)))
  5622. (eval (list 'let org-local-vars
  5623. (list 'call-interactively (list 'quote cmd)))))
  5624. ;;;; Archiving
  5625. (defun org-get-category (&optional pos)
  5626. "Get the category applying to position POS."
  5627. (get-text-property (or pos (point)) 'org-category))
  5628. (defun org-refresh-category-properties ()
  5629. "Refresh category text properties in the buffer."
  5630. (let ((def-cat (cond
  5631. ((null org-category)
  5632. (if buffer-file-name
  5633. (file-name-sans-extension
  5634. (file-name-nondirectory buffer-file-name))
  5635. "???"))
  5636. ((symbolp org-category) (symbol-name org-category))
  5637. (t org-category)))
  5638. beg end cat pos optionp)
  5639. (org-unmodified
  5640. (save-excursion
  5641. (save-restriction
  5642. (widen)
  5643. (goto-char (point-min))
  5644. (put-text-property (point) (point-max) 'org-category def-cat)
  5645. (while (re-search-forward
  5646. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5647. (setq pos (match-end 0)
  5648. optionp (equal (char-after (match-beginning 0)) ?#)
  5649. cat (org-trim (match-string 2)))
  5650. (if optionp
  5651. (setq beg (point-at-bol) end (point-max))
  5652. (org-back-to-heading t)
  5653. (setq beg (point) end (org-end-of-subtree t t)))
  5654. (put-text-property beg end 'org-category cat)
  5655. (goto-char pos)))))))
  5656. ;;;; Link Stuff
  5657. ;;; Link abbreviations
  5658. (defun org-link-expand-abbrev (link)
  5659. "Apply replacements as defined in `org-link-abbrev-alist."
  5660. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5661. (let* ((key (match-string 1 link))
  5662. (as (or (assoc key org-link-abbrev-alist-local)
  5663. (assoc key org-link-abbrev-alist)))
  5664. (tag (and (match-end 2) (match-string 3 link)))
  5665. rpl)
  5666. (if (not as)
  5667. link
  5668. (setq rpl (cdr as))
  5669. (cond
  5670. ((symbolp rpl) (funcall rpl tag))
  5671. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5672. ((string-match "%h" rpl)
  5673. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5674. (t (concat rpl tag)))))
  5675. link))
  5676. ;;; Storing and inserting links
  5677. (defvar org-insert-link-history nil
  5678. "Minibuffer history for links inserted with `org-insert-link'.")
  5679. (defvar org-stored-links nil
  5680. "Contains the links stored with `org-store-link'.")
  5681. (defvar org-store-link-plist nil
  5682. "Plist with info about the most recently link created with `org-store-link'.")
  5683. (defvar org-link-protocols nil
  5684. "Link protocols added to Org-mode using `org-add-link-type'.")
  5685. (defvar org-store-link-functions nil
  5686. "List of functions that are called to create and store a link.
  5687. Each function will be called in turn until one returns a non-nil
  5688. value. Each function should check if it is responsible for creating
  5689. this link (for example by looking at the major mode).
  5690. If not, it must exit and return nil.
  5691. If yes, it should return a non-nil value after a calling
  5692. `org-store-link-props' with a list of properties and values.
  5693. Special properties are:
  5694. :type The link prefix. like \"http\". This must be given.
  5695. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5696. This is obligatory as well.
  5697. :description Optional default description for the second pair
  5698. of brackets in an Org-mode link. The user can still change
  5699. this when inserting this link into an Org-mode buffer.
  5700. In addition to these, any additional properties can be specified
  5701. and then used in remember templates.")
  5702. (defun org-add-link-type (type &optional follow export)
  5703. "Add TYPE to the list of `org-link-types'.
  5704. Re-compute all regular expressions depending on `org-link-types'
  5705. FOLLOW and EXPORT are two functions.
  5706. FOLLOW should take the link path as the single argument and do whatever
  5707. is necessary to follow the link, for example find a file or display
  5708. a mail message.
  5709. EXPORT should format the link path for export to one of the export formats.
  5710. It should be a function accepting three arguments:
  5711. path the path of the link, the text after the prefix (like \"http:\")
  5712. desc the description of the link, if any, nil if there was no description
  5713. format the export format, a symbol like `html' or `latex'.
  5714. The function may use the FORMAT information to return different values
  5715. depending on the format. The return value will be put literally into
  5716. the exported file.
  5717. Org-mode has a built-in default for exporting links. If you are happy with
  5718. this default, there is no need to define an export function for the link
  5719. type. For a simple example of an export function, see `org-bbdb.el'."
  5720. (add-to-list 'org-link-types type t)
  5721. (org-make-link-regexps)
  5722. (if (assoc type org-link-protocols)
  5723. (setcdr (assoc type org-link-protocols) (list follow export))
  5724. (push (list type follow export) org-link-protocols)))
  5725. ;;;###autoload
  5726. (defun org-store-link (arg)
  5727. "\\<org-mode-map>Store an org-link to the current location.
  5728. This link is added to `org-stored-links' and can later be inserted
  5729. into an org-buffer with \\[org-insert-link].
  5730. For some link types, a prefix arg is interpreted:
  5731. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5732. For file links, arg negates `org-context-in-file-links'."
  5733. (interactive "P")
  5734. (org-load-modules-maybe)
  5735. (setq org-store-link-plist nil) ; reset
  5736. (let (link cpltxt desc description search txt)
  5737. (cond
  5738. ((run-hook-with-args-until-success 'org-store-link-functions)
  5739. (setq link (plist-get org-store-link-plist :link)
  5740. desc (or (plist-get org-store-link-plist :description) link)))
  5741. ((equal (buffer-name) "*Org Edit Src Example*")
  5742. (let (label gc)
  5743. (while (or (not label)
  5744. (save-excursion
  5745. (save-restriction
  5746. (widen)
  5747. (goto-char (point-min))
  5748. (re-search-forward
  5749. (regexp-quote (format org-coderef-label-format label))
  5750. nil t))))
  5751. (when label (message "Label exists already") (sit-for 2))
  5752. (setq label (read-string "Code line label: " label)))
  5753. (end-of-line 1)
  5754. (setq link (format org-coderef-label-format label))
  5755. (setq gc (- 79 (length link)))
  5756. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5757. (insert link)
  5758. (setq link (concat "(" label ")") desc nil)))
  5759. ((eq major-mode 'calendar-mode)
  5760. (let ((cd (calendar-cursor-to-date)))
  5761. (setq link
  5762. (format-time-string
  5763. (car org-time-stamp-formats)
  5764. (apply 'encode-time
  5765. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5766. nil nil nil))))
  5767. (org-store-link-props :type "calendar" :date cd)))
  5768. ((eq major-mode 'w3-mode)
  5769. (setq cpltxt (url-view-url t)
  5770. link (org-make-link cpltxt))
  5771. (org-store-link-props :type "w3" :url (url-view-url t)))
  5772. ((eq major-mode 'w3m-mode)
  5773. (setq cpltxt (or w3m-current-title w3m-current-url)
  5774. link (org-make-link w3m-current-url))
  5775. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5776. ((setq search (run-hook-with-args-until-success
  5777. 'org-create-file-search-functions))
  5778. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5779. "::" search))
  5780. (setq cpltxt (or description link)))
  5781. ((eq major-mode 'image-mode)
  5782. (setq cpltxt (concat "file:"
  5783. (abbreviate-file-name buffer-file-name))
  5784. link (org-make-link cpltxt))
  5785. (org-store-link-props :type "image" :file buffer-file-name))
  5786. ((eq major-mode 'dired-mode)
  5787. ;; link to the file in the current line
  5788. (setq cpltxt (concat "file:"
  5789. (abbreviate-file-name
  5790. (expand-file-name
  5791. (dired-get-filename nil t))))
  5792. link (org-make-link cpltxt)))
  5793. ((and buffer-file-name (org-mode-p))
  5794. (cond
  5795. ((org-in-regexp "<<\\(.*?\\)>>")
  5796. (setq cpltxt
  5797. (concat "file:"
  5798. (abbreviate-file-name buffer-file-name)
  5799. "::" (match-string 1))
  5800. link (org-make-link cpltxt)))
  5801. ((and (featurep 'org-id)
  5802. (or (eq org-link-to-org-use-id t)
  5803. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5804. (interactive-p))
  5805. (and org-link-to-org-use-id
  5806. (condition-case nil
  5807. (org-entry-get nil "ID")
  5808. (error nil)))))
  5809. ;; We can make a link using the ID.
  5810. (setq link (condition-case nil
  5811. (prog1 (org-id-store-link)
  5812. (setq desc (plist-get org-store-link-plist
  5813. :description)))
  5814. (error
  5815. ;; probably before first headline, link to file only
  5816. (concat "file:"
  5817. (abbreviate-file-name buffer-file-name))))))
  5818. (t
  5819. ;; Just link to current headline
  5820. (setq cpltxt (concat "file:"
  5821. (abbreviate-file-name buffer-file-name)))
  5822. ;; Add a context search string
  5823. (when (org-xor org-context-in-file-links arg)
  5824. (setq txt (cond
  5825. ((org-on-heading-p) nil)
  5826. ((org-region-active-p)
  5827. (buffer-substring (region-beginning) (region-end)))
  5828. (t nil)))
  5829. (when (or (null txt) (string-match "\\S-" txt))
  5830. (setq cpltxt
  5831. (concat cpltxt "::"
  5832. (condition-case nil
  5833. (org-make-org-heading-search-string txt)
  5834. (error "")))
  5835. desc "NONE")))
  5836. (if (string-match "::\\'" cpltxt)
  5837. (setq cpltxt (substring cpltxt 0 -2)))
  5838. (setq link (org-make-link cpltxt)))))
  5839. ((buffer-file-name (buffer-base-buffer))
  5840. ;; Just link to this file here.
  5841. (setq cpltxt (concat "file:"
  5842. (abbreviate-file-name
  5843. (buffer-file-name (buffer-base-buffer)))))
  5844. ;; Add a context string
  5845. (when (org-xor org-context-in-file-links arg)
  5846. (setq txt (if (org-region-active-p)
  5847. (buffer-substring (region-beginning) (region-end))
  5848. (buffer-substring (point-at-bol) (point-at-eol))))
  5849. ;; Only use search option if there is some text.
  5850. (when (string-match "\\S-" txt)
  5851. (setq cpltxt
  5852. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5853. desc "NONE")))
  5854. (setq link (org-make-link cpltxt)))
  5855. ((interactive-p)
  5856. (error "Cannot link to a buffer which is not visiting a file"))
  5857. (t (setq link nil)))
  5858. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5859. (setq link (or link cpltxt)
  5860. desc (or desc cpltxt))
  5861. (if (equal desc "NONE") (setq desc nil))
  5862. (if (and (interactive-p) link)
  5863. (progn
  5864. (setq org-stored-links
  5865. (cons (list link desc) org-stored-links))
  5866. (message "Stored: %s" (or desc link)))
  5867. (and link (org-make-link-string link desc)))))
  5868. (defun org-store-link-props (&rest plist)
  5869. "Store link properties, extract names and addresses."
  5870. (let (x adr)
  5871. (when (setq x (plist-get plist :from))
  5872. (setq adr (mail-extract-address-components x))
  5873. (setq plist (plist-put plist :fromname (car adr)))
  5874. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5875. (when (setq x (plist-get plist :to))
  5876. (setq adr (mail-extract-address-components x))
  5877. (setq plist (plist-put plist :toname (car adr)))
  5878. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5879. (let ((from (plist-get plist :from))
  5880. (to (plist-get plist :to)))
  5881. (when (and from to org-from-is-user-regexp)
  5882. (setq plist
  5883. (plist-put plist :fromto
  5884. (if (string-match org-from-is-user-regexp from)
  5885. (concat "to %t")
  5886. (concat "from %f"))))))
  5887. (setq org-store-link-plist plist))
  5888. (defun org-add-link-props (&rest plist)
  5889. "Add these properties to the link property list."
  5890. (let (key value)
  5891. (while plist
  5892. (setq key (pop plist) value (pop plist))
  5893. (setq org-store-link-plist
  5894. (plist-put org-store-link-plist key value)))))
  5895. (defun org-email-link-description (&optional fmt)
  5896. "Return the description part of an email link.
  5897. This takes information from `org-store-link-plist' and formats it
  5898. according to FMT (default from `org-email-link-description-format')."
  5899. (setq fmt (or fmt org-email-link-description-format))
  5900. (let* ((p org-store-link-plist)
  5901. (to (plist-get p :toaddress))
  5902. (from (plist-get p :fromaddress))
  5903. (table
  5904. (list
  5905. (cons "%c" (plist-get p :fromto))
  5906. (cons "%F" (plist-get p :from))
  5907. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5908. (cons "%T" (plist-get p :to))
  5909. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5910. (cons "%s" (plist-get p :subject))
  5911. (cons "%m" (plist-get p :message-id)))))
  5912. (when (string-match "%c" fmt)
  5913. ;; Check if the user wrote this message
  5914. (if (and org-from-is-user-regexp from to
  5915. (save-match-data (string-match org-from-is-user-regexp from)))
  5916. (setq fmt (replace-match "to %t" t t fmt))
  5917. (setq fmt (replace-match "from %f" t t fmt))))
  5918. (org-replace-escapes fmt table)))
  5919. (defun org-make-org-heading-search-string (&optional string heading)
  5920. "Make search string for STRING or current headline."
  5921. (interactive)
  5922. (let ((s (or string (org-get-heading))))
  5923. (unless (and string (not heading))
  5924. ;; We are using a headline, clean up garbage in there.
  5925. (if (string-match org-todo-regexp s)
  5926. (setq s (replace-match "" t t s)))
  5927. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5928. (setq s (replace-match "" t t s)))
  5929. (setq s (org-trim s))
  5930. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5931. org-comment-string "\\)") s)
  5932. (setq s (replace-match "" t t s)))
  5933. (while (string-match org-ts-regexp s)
  5934. (setq s (replace-match "" t t s))))
  5935. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5936. (setq s (replace-match " " t t s)))
  5937. (or string (setq s (concat "*" s))) ; Add * for headlines
  5938. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5939. (defun org-make-link (&rest strings)
  5940. "Concatenate STRINGS."
  5941. (apply 'concat strings))
  5942. (defun org-make-link-string (link &optional description)
  5943. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5944. (unless (string-match "\\S-" link)
  5945. (error "Empty link"))
  5946. (when (stringp description)
  5947. ;; Remove brackets from the description, they are fatal.
  5948. (while (string-match "\\[" description)
  5949. (setq description (replace-match "{" t t description)))
  5950. (while (string-match "\\]" description)
  5951. (setq description (replace-match "}" t t description))))
  5952. (when (equal (org-link-escape link) description)
  5953. ;; No description needed, it is identical
  5954. (setq description nil))
  5955. (when (and (not description)
  5956. (not (equal link (org-link-escape link))))
  5957. (setq description (org-extract-attributes link)))
  5958. (concat "[[" (org-link-escape link) "]"
  5959. (if description (concat "[" description "]") "")
  5960. "]"))
  5961. (defconst org-link-escape-chars
  5962. '((?\ . "%20")
  5963. (?\[ . "%5B")
  5964. (?\] . "%5D")
  5965. (?\340 . "%E0") ; `a
  5966. (?\342 . "%E2") ; ^a
  5967. (?\347 . "%E7") ; ,c
  5968. (?\350 . "%E8") ; `e
  5969. (?\351 . "%E9") ; 'e
  5970. (?\352 . "%EA") ; ^e
  5971. (?\356 . "%EE") ; ^i
  5972. (?\364 . "%F4") ; ^o
  5973. (?\371 . "%F9") ; `u
  5974. (?\373 . "%FB") ; ^u
  5975. (?\; . "%3B")
  5976. (?? . "%3F")
  5977. (?= . "%3D")
  5978. (?+ . "%2B")
  5979. )
  5980. "Association list of escapes for some characters problematic in links.
  5981. This is the list that is used for internal purposes.")
  5982. (defconst org-link-escape-chars-browser
  5983. '((?\ . "%20")) ; 32 for the SPC char
  5984. "Association list of escapes for some characters problematic in links.
  5985. This is the list that is used before handing over to the browser.")
  5986. (defun org-link-escape (text &optional table)
  5987. "Escape characters in TEXT that are problematic for links."
  5988. (setq table (or table org-link-escape-chars))
  5989. (when text
  5990. (let ((re (mapconcat (lambda (x) (regexp-quote
  5991. (char-to-string (car x))))
  5992. table "\\|")))
  5993. (while (string-match re text)
  5994. (setq text
  5995. (replace-match
  5996. (cdr (assoc (string-to-char (match-string 0 text))
  5997. table))
  5998. t t text)))
  5999. text)))
  6000. (defun org-link-unescape (text &optional table)
  6001. "Reverse the action of `org-link-escape'."
  6002. (setq table (or table org-link-escape-chars))
  6003. (when text
  6004. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6005. table "\\|")))
  6006. (while (string-match re text)
  6007. (setq text
  6008. (replace-match
  6009. (char-to-string (car (rassoc (match-string 0 text) table)))
  6010. t t text)))
  6011. text)))
  6012. (defun org-xor (a b)
  6013. "Exclusive or."
  6014. (if a (not b) b))
  6015. (defun org-fixup-message-id-for-http (s)
  6016. "Replace special characters in a message id, so it can be used in an http query."
  6017. (while (string-match "<" s)
  6018. (setq s (replace-match "%3C" t t s)))
  6019. (while (string-match ">" s)
  6020. (setq s (replace-match "%3E" t t s)))
  6021. (while (string-match "@" s)
  6022. (setq s (replace-match "%40" t t s)))
  6023. s)
  6024. ;;;###autoload
  6025. (defun org-insert-link-global ()
  6026. "Insert a link like Org-mode does.
  6027. This command can be called in any mode to insert a link in Org-mode syntax."
  6028. (interactive)
  6029. (org-load-modules-maybe)
  6030. (org-run-like-in-org-mode 'org-insert-link))
  6031. (defun org-insert-link (&optional complete-file link-location)
  6032. "Insert a link. At the prompt, enter the link.
  6033. Completion can be used to insert any of the link protocol prefixes like
  6034. http or ftp in use.
  6035. The history can be used to select a link previously stored with
  6036. `org-store-link'. When the empty string is entered (i.e. if you just
  6037. press RET at the prompt), the link defaults to the most recently
  6038. stored link. As SPC triggers completion in the minibuffer, you need to
  6039. use M-SPC or C-q SPC to force the insertion of a space character.
  6040. You will also be prompted for a description, and if one is given, it will
  6041. be displayed in the buffer instead of the link.
  6042. If there is already a link at point, this command will allow you to edit link
  6043. and description parts.
  6044. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6045. be selected using completion. The path to the file will be relative to the
  6046. current directory if the file is in the current directory or a subdirectory.
  6047. Otherwise, the link will be the absolute path as completed in the minibuffer
  6048. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6049. option `org-link-file-path-type'.
  6050. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6051. the current directory or below.
  6052. With three \\[universal-argument] prefixes, negate the meaning of
  6053. `org-keep-stored-link-after-insertion'.
  6054. If `org-make-link-description-function' is non-nil, this function will be
  6055. called with the link target, and the result will be the default
  6056. link description.
  6057. If the LINK-LOCATION parameter is non-nil, this value will be
  6058. used as the link location instead of reading one interactively."
  6059. (interactive "P")
  6060. (let* ((wcf (current-window-configuration))
  6061. (region (if (org-region-active-p)
  6062. (buffer-substring (region-beginning) (region-end))))
  6063. (remove (and region (list (region-beginning) (region-end))))
  6064. (desc region)
  6065. tmphist ; byte-compile incorrectly complains about this
  6066. (link link-location)
  6067. entry file)
  6068. (cond
  6069. (link-location) ; specified by arg, just use it.
  6070. ((org-in-regexp org-bracket-link-regexp 1)
  6071. ;; We do have a link at point, and we are going to edit it.
  6072. (setq remove (list (match-beginning 0) (match-end 0)))
  6073. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6074. (setq link (read-string "Link: "
  6075. (org-link-unescape
  6076. (org-match-string-no-properties 1)))))
  6077. ((or (org-in-regexp org-angle-link-re)
  6078. (org-in-regexp org-plain-link-re))
  6079. ;; Convert to bracket link
  6080. (setq remove (list (match-beginning 0) (match-end 0))
  6081. link (read-string "Link: "
  6082. (org-remove-angle-brackets (match-string 0)))))
  6083. ((member complete-file '((4) (16)))
  6084. ;; Completing read for file names.
  6085. (setq file (read-file-name "File: "))
  6086. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6087. (pwd1 (file-name-as-directory (abbreviate-file-name
  6088. (expand-file-name ".")))))
  6089. (cond
  6090. ((equal complete-file '(16))
  6091. (setq link (org-make-link
  6092. "file:"
  6093. (abbreviate-file-name (expand-file-name file)))))
  6094. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6095. (setq link (org-make-link "file:" (match-string 1 file))))
  6096. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6097. (expand-file-name file))
  6098. (setq link (org-make-link
  6099. "file:" (match-string 1 (expand-file-name file)))))
  6100. (t (setq link (org-make-link "file:" file))))))
  6101. (t
  6102. ;; Read link, with completion for stored links.
  6103. (with-output-to-temp-buffer "*Org Links*"
  6104. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6105. (when org-stored-links
  6106. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6107. (princ (mapconcat
  6108. (lambda (x)
  6109. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6110. (reverse org-stored-links) "\n"))))
  6111. (let ((cw (selected-window)))
  6112. (select-window (get-buffer-window "*Org Links*"))
  6113. (setq truncate-lines t)
  6114. (org-fit-window-to-buffer)
  6115. (select-window cw))
  6116. ;; Fake a link history, containing the stored links.
  6117. (setq tmphist (append (mapcar 'car org-stored-links)
  6118. org-insert-link-history))
  6119. (unwind-protect
  6120. (setq link
  6121. (let ((org-completion-use-ido nil))
  6122. (org-completing-read
  6123. "Link: "
  6124. (append
  6125. (mapcar (lambda (x) (list (concat (car x) ":")))
  6126. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6127. (mapcar (lambda (x) (list (concat x ":")))
  6128. org-link-types))
  6129. nil nil nil
  6130. 'tmphist
  6131. (or (car (car org-stored-links))))))
  6132. (set-window-configuration wcf)
  6133. (kill-buffer "*Org Links*"))
  6134. (setq entry (assoc link org-stored-links))
  6135. (or entry (push link org-insert-link-history))
  6136. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6137. (not org-keep-stored-link-after-insertion))
  6138. (setq org-stored-links (delq (assoc link org-stored-links)
  6139. org-stored-links)))
  6140. (setq desc (or desc (nth 1 entry)))))
  6141. (if (string-match org-plain-link-re link)
  6142. ;; URL-like link, normalize the use of angular brackets.
  6143. (setq link (org-make-link (org-remove-angle-brackets link))))
  6144. ;; Check if we are linking to the current file with a search option
  6145. ;; If yes, simplify the link by using only the search option.
  6146. (when (and buffer-file-name
  6147. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6148. (let* ((path (match-string 1 link))
  6149. (case-fold-search nil)
  6150. (search (match-string 2 link)))
  6151. (save-match-data
  6152. (if (equal (file-truename buffer-file-name) (file-truename path))
  6153. ;; We are linking to this same file, with a search option
  6154. (setq link search)))))
  6155. ;; Check if we can/should use a relative path. If yes, simplify the link
  6156. (when (string-match "^file:\\(.*\\)" link)
  6157. (let* ((path (match-string 1 link))
  6158. (origpath path)
  6159. (case-fold-search nil))
  6160. (cond
  6161. ((or (eq org-link-file-path-type 'absolute)
  6162. (equal complete-file '(16)))
  6163. (setq path (abbreviate-file-name (expand-file-name path))))
  6164. ((eq org-link-file-path-type 'noabbrev)
  6165. (setq path (expand-file-name path)))
  6166. ((eq org-link-file-path-type 'relative)
  6167. (setq path (file-relative-name path)))
  6168. (t
  6169. (save-match-data
  6170. (if (string-match (concat "^" (regexp-quote
  6171. (file-name-as-directory
  6172. (expand-file-name "."))))
  6173. (expand-file-name path))
  6174. ;; We are linking a file with relative path name.
  6175. (setq path (substring (expand-file-name path)
  6176. (match-end 0)))
  6177. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6178. (setq link (concat "file:" path))
  6179. (if (equal desc origpath)
  6180. (setq desc path))))
  6181. (if org-make-link-description-function
  6182. (setq desc (funcall org-make-link-description-function link desc)))
  6183. (setq desc (read-string "Description: " desc))
  6184. (unless (string-match "\\S-" desc) (setq desc nil))
  6185. (if remove (apply 'delete-region remove))
  6186. (insert (org-make-link-string link desc))))
  6187. (defun org-completing-read (&rest args)
  6188. "Completing-read with SPACE being a normal character."
  6189. (let ((minibuffer-local-completion-map
  6190. (copy-keymap minibuffer-local-completion-map)))
  6191. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6192. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6193. (apply 'org-ido-completing-read args)))
  6194. (defun org-ido-completing-read (&rest args)
  6195. "Completing-read using `ido-mode' speedups if available"
  6196. (if (and org-completion-use-ido
  6197. (fboundp 'ido-completing-read)
  6198. (boundp 'ido-mode) ido-mode
  6199. (listp (second args)))
  6200. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6201. (apply 'completing-read args)))
  6202. (defun org-extract-attributes (s)
  6203. "Extract the attributes cookie from a string and set as text property."
  6204. (let (a attr (start 0) key value)
  6205. (save-match-data
  6206. (when (string-match "{{\\([^}]+\\)}}$" s)
  6207. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6208. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6209. (setq key (match-string 1 a) value (match-string 2 a)
  6210. start (match-end 0)
  6211. attr (plist-put attr (intern key) value))))
  6212. (org-add-props s nil 'org-attr attr))
  6213. s))
  6214. (defun org-attributes-to-string (plist)
  6215. "Format a property list into an HTML attribute list."
  6216. (let ((s "") key value)
  6217. (while plist
  6218. (setq key (pop plist) value (pop plist))
  6219. (and value
  6220. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6221. s))
  6222. ;;; Opening/following a link
  6223. (defvar org-link-search-failed nil)
  6224. (defun org-next-link ()
  6225. "Move forward to the next link.
  6226. If the link is in hidden text, expose it."
  6227. (interactive)
  6228. (when (and org-link-search-failed (eq this-command last-command))
  6229. (goto-char (point-min))
  6230. (message "Link search wrapped back to beginning of buffer"))
  6231. (setq org-link-search-failed nil)
  6232. (let* ((pos (point))
  6233. (ct (org-context))
  6234. (a (assoc :link ct)))
  6235. (if a (goto-char (nth 2 a)))
  6236. (if (re-search-forward org-any-link-re nil t)
  6237. (progn
  6238. (goto-char (match-beginning 0))
  6239. (if (org-invisible-p) (org-show-context)))
  6240. (goto-char pos)
  6241. (setq org-link-search-failed t)
  6242. (error "No further link found"))))
  6243. (defun org-previous-link ()
  6244. "Move backward to the previous link.
  6245. If the link is in hidden text, expose it."
  6246. (interactive)
  6247. (when (and org-link-search-failed (eq this-command last-command))
  6248. (goto-char (point-max))
  6249. (message "Link search wrapped back to end of buffer"))
  6250. (setq org-link-search-failed nil)
  6251. (let* ((pos (point))
  6252. (ct (org-context))
  6253. (a (assoc :link ct)))
  6254. (if a (goto-char (nth 1 a)))
  6255. (if (re-search-backward org-any-link-re nil t)
  6256. (progn
  6257. (goto-char (match-beginning 0))
  6258. (if (org-invisible-p) (org-show-context)))
  6259. (goto-char pos)
  6260. (setq org-link-search-failed t)
  6261. (error "No further link found"))))
  6262. (defun org-translate-link (s)
  6263. "Translate a link string if a translation function has been defined."
  6264. (if (and org-link-translation-function
  6265. (fboundp org-link-translation-function)
  6266. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6267. (progn
  6268. (setq s (funcall org-link-translation-function
  6269. (match-string 1) (match-string 2)))
  6270. (concat (car s) ":" (cdr s)))
  6271. s))
  6272. (defun org-translate-link-from-planner (type path)
  6273. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6274. This is still an experimental function, your mileage may vary."
  6275. (cond
  6276. ((member type '("http" "https" "news" "ftp"))
  6277. ;; standard Internet links are the same.
  6278. nil)
  6279. ((and (equal type "irc") (string-match "^//" path))
  6280. ;; Planner has two / at the beginning of an irc link, we have 1.
  6281. ;; We should have zero, actually....
  6282. (setq path (substring path 1)))
  6283. ((and (equal type "lisp") (string-match "^/" path))
  6284. ;; Planner has a slash, we do not.
  6285. (setq type "elisp" path (substring path 1)))
  6286. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6287. ;; A typical message link. Planner has the id after the fina slash,
  6288. ;; we separate it with a hash mark
  6289. (setq path (concat (match-string 1 path) "#"
  6290. (org-remove-angle-brackets (match-string 2 path)))))
  6291. )
  6292. (cons type path))
  6293. (defun org-find-file-at-mouse (ev)
  6294. "Open file link or URL at mouse."
  6295. (interactive "e")
  6296. (mouse-set-point ev)
  6297. (org-open-at-point 'in-emacs))
  6298. (defun org-open-at-mouse (ev)
  6299. "Open file link or URL at mouse."
  6300. (interactive "e")
  6301. (mouse-set-point ev)
  6302. (if (eq major-mode 'org-agenda-mode)
  6303. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6304. (org-open-at-point))
  6305. (defvar org-window-config-before-follow-link nil
  6306. "The window configuration before following a link.
  6307. This is saved in case the need arises to restore it.")
  6308. (defvar org-open-link-marker (make-marker)
  6309. "Marker pointing to the location where `org-open-at-point; was called.")
  6310. ;;;###autoload
  6311. (defun org-open-at-point-global ()
  6312. "Follow a link like Org-mode does.
  6313. This command can be called in any mode to follow a link that has
  6314. Org-mode syntax."
  6315. (interactive)
  6316. (org-run-like-in-org-mode 'org-open-at-point))
  6317. ;;;###autoload
  6318. (defun org-open-link-from-string (s &optional arg)
  6319. "Open a link in the string S, as if it was in Org-mode."
  6320. (interactive "sLink: \nP")
  6321. (with-temp-buffer
  6322. (let ((org-inhibit-startup t))
  6323. (org-mode)
  6324. (insert s)
  6325. (goto-char (point-min))
  6326. (org-open-at-point arg))))
  6327. (defun org-open-at-point (&optional in-emacs)
  6328. "Open link at or after point.
  6329. If there is no link at point, this function will search forward up to
  6330. the end of the current subtree.
  6331. Normally, files will be opened by an appropriate application. If the
  6332. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6333. With a double prefix argument, try to open outside of Emacs, in the
  6334. application the system uses for this file type."
  6335. (interactive "P")
  6336. (org-load-modules-maybe)
  6337. (move-marker org-open-link-marker (point))
  6338. (setq org-window-config-before-follow-link (current-window-configuration))
  6339. (org-remove-occur-highlights nil nil t)
  6340. (cond
  6341. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6342. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6343. (org-footnote-action))
  6344. (t
  6345. (let (type path link line search (pos (point)))
  6346. (catch 'match
  6347. (save-excursion
  6348. (skip-chars-forward "^]\n\r")
  6349. (when (org-in-regexp org-bracket-link-regexp)
  6350. (setq link (org-extract-attributes
  6351. (org-link-unescape (org-match-string-no-properties 1))))
  6352. (while (string-match " *\n *" link)
  6353. (setq link (replace-match " " t t link)))
  6354. (setq link (org-link-expand-abbrev link))
  6355. (cond
  6356. ((or (file-name-absolute-p link)
  6357. (string-match "^\\.\\.?/" link))
  6358. (setq type "file" path link))
  6359. ((string-match org-link-re-with-space3 link)
  6360. (setq type (match-string 1 link) path (match-string 2 link)))
  6361. (t (setq type "thisfile" path link)))
  6362. (throw 'match t)))
  6363. (when (get-text-property (point) 'org-linked-text)
  6364. (setq type "thisfile"
  6365. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6366. (1+ (point)) (point))
  6367. path (buffer-substring
  6368. (previous-single-property-change pos 'org-linked-text)
  6369. (next-single-property-change pos 'org-linked-text)))
  6370. (throw 'match t))
  6371. (save-excursion
  6372. (when (or (org-in-regexp org-angle-link-re)
  6373. (org-in-regexp org-plain-link-re))
  6374. (setq type (match-string 1) path (match-string 2))
  6375. (throw 'match t)))
  6376. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6377. (setq type "tree-match"
  6378. path (match-string 1))
  6379. (throw 'match t))
  6380. (save-excursion
  6381. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6382. (setq type "tags"
  6383. path (match-string 1))
  6384. (while (string-match ":" path)
  6385. (setq path (replace-match "+" t t path)))
  6386. (throw 'match t))))
  6387. (unless path
  6388. (error "No link found"))
  6389. ;; Remove any trailing spaces in path
  6390. (if (string-match " +\\'" path)
  6391. (setq path (replace-match "" t t path)))
  6392. (if (and org-link-translation-function
  6393. (fboundp org-link-translation-function))
  6394. ;; Check if we need to translate the link
  6395. (let ((tmp (funcall org-link-translation-function type path)))
  6396. (setq type (car tmp) path (cdr tmp))))
  6397. (cond
  6398. ((assoc type org-link-protocols)
  6399. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6400. ((equal type "mailto")
  6401. (let ((cmd (car org-link-mailto-program))
  6402. (args (cdr org-link-mailto-program)) args1
  6403. (address path) (subject "") a)
  6404. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6405. (setq address (match-string 1 path)
  6406. subject (org-link-escape (match-string 2 path))))
  6407. (while args
  6408. (cond
  6409. ((not (stringp (car args))) (push (pop args) args1))
  6410. (t (setq a (pop args))
  6411. (if (string-match "%a" a)
  6412. (setq a (replace-match address t t a)))
  6413. (if (string-match "%s" a)
  6414. (setq a (replace-match subject t t a)))
  6415. (push a args1))))
  6416. (apply cmd (nreverse args1))))
  6417. ((member type '("http" "https" "ftp" "news"))
  6418. (browse-url (concat type ":" (org-link-escape
  6419. path org-link-escape-chars-browser))))
  6420. ((member type '("message"))
  6421. (browse-url (concat type ":" path)))
  6422. ((string= type "tags")
  6423. (org-tags-view in-emacs path))
  6424. ((string= type "thisfile")
  6425. (if in-emacs
  6426. (switch-to-buffer-other-window
  6427. (org-get-buffer-for-internal-link (current-buffer)))
  6428. (org-mark-ring-push))
  6429. (let ((cmd `(org-link-search
  6430. ,path
  6431. ,(cond ((equal in-emacs '(4)) 'occur)
  6432. ((equal in-emacs '(16)) 'org-occur)
  6433. (t nil))
  6434. ,pos)))
  6435. (condition-case nil (eval cmd)
  6436. (error (progn (widen) (eval cmd))))))
  6437. ((string= type "tree-match")
  6438. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6439. ((string= type "file")
  6440. (if (string-match "::\\([0-9]+\\)\\'" path)
  6441. (setq line (string-to-number (match-string 1 path))
  6442. path (substring path 0 (match-beginning 0)))
  6443. (if (string-match "::\\(.+\\)\\'" path)
  6444. (setq search (match-string 1 path)
  6445. path (substring path 0 (match-beginning 0)))))
  6446. (if (string-match "[*?{]" (file-name-nondirectory path))
  6447. (dired path)
  6448. (org-open-file path in-emacs line search)))
  6449. ((string= type "news")
  6450. (require 'org-gnus)
  6451. (org-gnus-follow-link path))
  6452. ((string= type "shell")
  6453. (let ((cmd path))
  6454. (if (or (not org-confirm-shell-link-function)
  6455. (funcall org-confirm-shell-link-function
  6456. (format "Execute \"%s\" in shell? "
  6457. (org-add-props cmd nil
  6458. 'face 'org-warning))))
  6459. (progn
  6460. (message "Executing %s" cmd)
  6461. (shell-command cmd))
  6462. (error "Abort"))))
  6463. ((string= type "elisp")
  6464. (let ((cmd path))
  6465. (if (or (not org-confirm-elisp-link-function)
  6466. (funcall org-confirm-elisp-link-function
  6467. (format "Execute \"%s\" as elisp? "
  6468. (org-add-props cmd nil
  6469. 'face 'org-warning))))
  6470. (message "%s => %s" cmd
  6471. (if (equal (string-to-char cmd) ?\()
  6472. (eval (read cmd))
  6473. (call-interactively (read cmd))))
  6474. (error "Abort"))))
  6475. (t
  6476. (browse-url-at-point))))))
  6477. (move-marker org-open-link-marker nil)
  6478. (run-hook-with-args 'org-follow-link-hook))
  6479. ;;;; Time estimates
  6480. (defun org-get-effort (&optional pom)
  6481. "Get the effort estimate for the current entry."
  6482. (org-entry-get pom org-effort-property))
  6483. ;;; File search
  6484. (defvar org-create-file-search-functions nil
  6485. "List of functions to construct the right search string for a file link.
  6486. These functions are called in turn with point at the location to
  6487. which the link should point.
  6488. A function in the hook should first test if it would like to
  6489. handle this file type, for example by checking the major-mode or
  6490. the file extension. If it decides not to handle this file, it
  6491. should just return nil to give other functions a chance. If it
  6492. does handle the file, it must return the search string to be used
  6493. when following the link. The search string will be part of the
  6494. file link, given after a double colon, and `org-open-at-point'
  6495. will automatically search for it. If special measures must be
  6496. taken to make the search successful, another function should be
  6497. added to the companion hook `org-execute-file-search-functions',
  6498. which see.
  6499. A function in this hook may also use `setq' to set the variable
  6500. `description' to provide a suggestion for the descriptive text to
  6501. be used for this link when it gets inserted into an Org-mode
  6502. buffer with \\[org-insert-link].")
  6503. (defvar org-execute-file-search-functions nil
  6504. "List of functions to execute a file search triggered by a link.
  6505. Functions added to this hook must accept a single argument, the
  6506. search string that was part of the file link, the part after the
  6507. double colon. The function must first check if it would like to
  6508. handle this search, for example by checking the major-mode or the
  6509. file extension. If it decides not to handle this search, it
  6510. should just return nil to give other functions a chance. If it
  6511. does handle the search, it must return a non-nil value to keep
  6512. other functions from trying.
  6513. Each function can access the current prefix argument through the
  6514. variable `current-prefix-argument'. Note that a single prefix is
  6515. used to force opening a link in Emacs, so it may be good to only
  6516. use a numeric or double prefix to guide the search function.
  6517. In case this is needed, a function in this hook can also restore
  6518. the window configuration before `org-open-at-point' was called using:
  6519. (set-window-configuration org-window-config-before-follow-link)")
  6520. (defun org-link-search (s &optional type avoid-pos)
  6521. "Search for a link search option.
  6522. If S is surrounded by forward slashes, it is interpreted as a
  6523. regular expression. In org-mode files, this will create an `org-occur'
  6524. sparse tree. In ordinary files, `occur' will be used to list matches.
  6525. If the current buffer is in `dired-mode', grep will be used to search
  6526. in all files. If AVOID-POS is given, ignore matches near that position."
  6527. (let ((case-fold-search t)
  6528. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6529. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6530. (append '(("") (" ") ("\t") ("\n"))
  6531. org-emphasis-alist)
  6532. "\\|") "\\)"))
  6533. (pos (point))
  6534. (pre nil) (post nil)
  6535. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6536. (cond
  6537. ;; First check if there are any special
  6538. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6539. ;; Now try the builtin stuff
  6540. ((save-excursion
  6541. (goto-char (point-min))
  6542. (and
  6543. (re-search-forward
  6544. (concat "<<" (regexp-quote s0) ">>") nil t)
  6545. (setq type 'dedicated
  6546. pos (match-beginning 0))))
  6547. ;; There is an exact target for this
  6548. (goto-char pos))
  6549. ((and (string-match "^(\\(.*\\))$" s0)
  6550. (save-excursion
  6551. (goto-char (point-min))
  6552. (and
  6553. (re-search-forward
  6554. (concat "[^[]" (regexp-quote
  6555. (format org-coderef-label-format
  6556. (match-string 1 s0))))
  6557. nil t)
  6558. (setq type 'dedicated
  6559. pos (1+ (match-beginning 0))))))
  6560. ;; There is a coderef target for this
  6561. (goto-char pos))
  6562. ((string-match "^/\\(.*\\)/$" s)
  6563. ;; A regular expression
  6564. (cond
  6565. ((org-mode-p)
  6566. (org-occur (match-string 1 s)))
  6567. ;;((eq major-mode 'dired-mode)
  6568. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6569. (t (org-do-occur (match-string 1 s)))))
  6570. (t
  6571. ;; A normal search strings
  6572. (when (equal (string-to-char s) ?*)
  6573. ;; Anchor on headlines, post may include tags.
  6574. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6575. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6576. s (substring s 1)))
  6577. (remove-text-properties
  6578. 0 (length s)
  6579. '(face nil mouse-face nil keymap nil fontified nil) s)
  6580. ;; Make a series of regular expressions to find a match
  6581. (setq words (org-split-string s "[ \n\r\t]+")
  6582. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6583. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6584. "\\)" markers)
  6585. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6586. re2a (concat "[ \t\r\n]" re2a_)
  6587. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6588. re4 (concat "[^a-zA-Z_]" re4_)
  6589. re1 (concat pre re2 post)
  6590. re3 (concat pre (if pre re4_ re4) post)
  6591. re5 (concat pre ".*" re4)
  6592. re2 (concat pre re2)
  6593. re2a (concat pre (if pre re2a_ re2a))
  6594. re4 (concat pre (if pre re4_ re4))
  6595. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6596. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6597. re5 "\\)"
  6598. ))
  6599. (cond
  6600. ((eq type 'org-occur) (org-occur reall))
  6601. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6602. (t (goto-char (point-min))
  6603. (setq type 'fuzzy)
  6604. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6605. (org-search-not-self 1 re1 nil t)
  6606. (org-search-not-self 1 re2 nil t)
  6607. (org-search-not-self 1 re2a nil t)
  6608. (org-search-not-self 1 re3 nil t)
  6609. (org-search-not-self 1 re4 nil t)
  6610. (org-search-not-self 1 re5 nil t)
  6611. )
  6612. (goto-char (match-beginning 1))
  6613. (goto-char pos)
  6614. (error "No match")))))
  6615. (t
  6616. ;; Normal string-search
  6617. (goto-char (point-min))
  6618. (if (search-forward s nil t)
  6619. (goto-char (match-beginning 0))
  6620. (error "No match"))))
  6621. (and (org-mode-p) (org-show-context 'link-search))
  6622. type))
  6623. (defun org-search-not-self (group &rest args)
  6624. "Execute `re-search-forward', but only accept matches that do not
  6625. enclose the position of `org-open-link-marker'."
  6626. (let ((m org-open-link-marker))
  6627. (catch 'exit
  6628. (while (apply 're-search-forward args)
  6629. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6630. (goto-char (match-end group))
  6631. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6632. (> (match-beginning 0) (marker-position m))
  6633. (< (match-end 0) (marker-position m)))
  6634. (save-match-data
  6635. (or (not (org-in-regexp
  6636. org-bracket-link-analytic-regexp 1))
  6637. (not (match-end 4)) ; no description
  6638. (and (<= (match-beginning 4) (point))
  6639. (>= (match-end 4) (point))))))
  6640. (throw 'exit (point))))))))
  6641. (defun org-get-buffer-for-internal-link (buffer)
  6642. "Return a buffer to be used for displaying the link target of internal links."
  6643. (cond
  6644. ((not org-display-internal-link-with-indirect-buffer)
  6645. buffer)
  6646. ((string-match "(Clone)$" (buffer-name buffer))
  6647. (message "Buffer is already a clone, not making another one")
  6648. ;; we also do not modify visibility in this case
  6649. buffer)
  6650. (t ; make a new indirect buffer for displaying the link
  6651. (let* ((bn (buffer-name buffer))
  6652. (ibn (concat bn "(Clone)"))
  6653. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6654. (with-current-buffer ib (org-overview))
  6655. ib))))
  6656. (defun org-do-occur (regexp &optional cleanup)
  6657. "Call the Emacs command `occur'.
  6658. If CLEANUP is non-nil, remove the printout of the regular expression
  6659. in the *Occur* buffer. This is useful if the regex is long and not useful
  6660. to read."
  6661. (occur regexp)
  6662. (when cleanup
  6663. (let ((cwin (selected-window)) win beg end)
  6664. (when (setq win (get-buffer-window "*Occur*"))
  6665. (select-window win))
  6666. (goto-char (point-min))
  6667. (when (re-search-forward "match[a-z]+" nil t)
  6668. (setq beg (match-end 0))
  6669. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6670. (setq end (1- (match-beginning 0)))))
  6671. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6672. (goto-char (point-min))
  6673. (select-window cwin))))
  6674. ;;; The mark ring for links jumps
  6675. (defvar org-mark-ring nil
  6676. "Mark ring for positions before jumps in Org-mode.")
  6677. (defvar org-mark-ring-last-goto nil
  6678. "Last position in the mark ring used to go back.")
  6679. ;; Fill and close the ring
  6680. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6681. (loop for i from 1 to org-mark-ring-length do
  6682. (push (make-marker) org-mark-ring))
  6683. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6684. org-mark-ring)
  6685. (defun org-mark-ring-push (&optional pos buffer)
  6686. "Put the current position or POS into the mark ring and rotate it."
  6687. (interactive)
  6688. (setq pos (or pos (point)))
  6689. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6690. (move-marker (car org-mark-ring)
  6691. (or pos (point))
  6692. (or buffer (current-buffer)))
  6693. (message "%s"
  6694. (substitute-command-keys
  6695. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6696. (defun org-mark-ring-goto (&optional n)
  6697. "Jump to the previous position in the mark ring.
  6698. With prefix arg N, jump back that many stored positions. When
  6699. called several times in succession, walk through the entire ring.
  6700. Org-mode commands jumping to a different position in the current file,
  6701. or to another Org-mode file, automatically push the old position
  6702. onto the ring."
  6703. (interactive "p")
  6704. (let (p m)
  6705. (if (eq last-command this-command)
  6706. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6707. (setq p org-mark-ring))
  6708. (setq org-mark-ring-last-goto p)
  6709. (setq m (car p))
  6710. (switch-to-buffer (marker-buffer m))
  6711. (goto-char m)
  6712. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6713. (defun org-remove-angle-brackets (s)
  6714. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6715. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6716. s)
  6717. (defun org-add-angle-brackets (s)
  6718. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6719. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6720. s)
  6721. (defun org-remove-double-quotes (s)
  6722. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6723. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6724. s)
  6725. ;;; Following specific links
  6726. (defun org-follow-timestamp-link ()
  6727. (cond
  6728. ((org-at-date-range-p t)
  6729. (let ((org-agenda-start-on-weekday)
  6730. (t1 (match-string 1))
  6731. (t2 (match-string 2)))
  6732. (setq t1 (time-to-days (org-time-string-to-time t1))
  6733. t2 (time-to-days (org-time-string-to-time t2)))
  6734. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6735. ((org-at-timestamp-p t)
  6736. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6737. (substring (match-string 1) 0 10)))
  6738. 1))
  6739. (t (error "This should not happen"))))
  6740. ;;; Following file links
  6741. (defvar org-wait nil)
  6742. (defun org-open-file (path &optional in-emacs line search)
  6743. "Open the file at PATH.
  6744. First, this expands any special file name abbreviations. Then the
  6745. configuration variable `org-file-apps' is checked if it contains an
  6746. entry for this file type, and if yes, the corresponding command is launched.
  6747. If no application is found, Emacs simply visits the file.
  6748. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6749. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6750. and o use an external application to visit the file.
  6751. Optional LINE specifies a line to go to, optional SEARCH a string to
  6752. search for. If LINE or SEARCH is given, the file will always be
  6753. opened in Emacs.
  6754. If the file does not exist, an error is thrown."
  6755. (setq in-emacs (or in-emacs line search))
  6756. (let* ((file (if (equal path "")
  6757. buffer-file-name
  6758. (substitute-in-file-name (expand-file-name path))))
  6759. (apps (append org-file-apps (org-default-apps)))
  6760. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6761. (dirp (if remp nil (file-directory-p file)))
  6762. (file (if (and dirp org-open-directory-means-index-dot-org)
  6763. (concat (file-name-as-directory file) "index.org")
  6764. file))
  6765. (a-m-a-p (assq 'auto-mode apps))
  6766. (dfile (downcase file))
  6767. (old-buffer (current-buffer))
  6768. (old-pos (point))
  6769. (old-mode major-mode)
  6770. ext cmd)
  6771. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6772. (setq ext (match-string 1 dfile))
  6773. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6774. (setq ext (match-string 1 dfile))))
  6775. (cond
  6776. ((equal in-emacs '(16))
  6777. (setq cmd (cdr (assoc 'system apps))))
  6778. (in-emacs (setq cmd 'emacs))
  6779. (t
  6780. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6781. (and dirp (cdr (assoc 'directory apps)))
  6782. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6783. 'string-match)
  6784. (cdr (assoc ext apps))
  6785. (cdr (assoc t apps))))))
  6786. (when (eq cmd 'system)
  6787. (setq cmd (cdr (assoc 'system apps))))
  6788. (when (eq cmd 'default)
  6789. (setq cmd (cdr (assoc t apps))))
  6790. (when (eq cmd 'mailcap)
  6791. (require 'mailcap)
  6792. (mailcap-parse-mailcaps)
  6793. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6794. (command (mailcap-mime-info mime-type)))
  6795. (if (stringp command)
  6796. (setq cmd command)
  6797. (setq cmd 'emacs))))
  6798. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6799. (not (file-exists-p file))
  6800. (not org-open-non-existing-files))
  6801. (error "No such file: %s" file))
  6802. (cond
  6803. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6804. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6805. (while (string-match "['\"]%s['\"]" cmd)
  6806. (setq cmd (replace-match "%s" t t cmd)))
  6807. (while (string-match "%s" cmd)
  6808. (setq cmd (replace-match
  6809. (save-match-data
  6810. (shell-quote-argument
  6811. (convert-standard-filename file)))
  6812. t t cmd)))
  6813. (save-window-excursion
  6814. (start-process-shell-command cmd nil cmd)
  6815. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6816. ))
  6817. ((or (stringp cmd)
  6818. (eq cmd 'emacs))
  6819. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6820. (widen)
  6821. (if line (goto-line line)
  6822. (if search (org-link-search search))))
  6823. ((consp cmd)
  6824. (let ((file (convert-standard-filename file)))
  6825. (eval cmd)))
  6826. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6827. (and (org-mode-p) (eq old-mode 'org-mode)
  6828. (or (not (equal old-buffer (current-buffer)))
  6829. (not (equal old-pos (point))))
  6830. (org-mark-ring-push old-pos old-buffer))))
  6831. (defun org-default-apps ()
  6832. "Return the default applications for this operating system."
  6833. (cond
  6834. ((eq system-type 'darwin)
  6835. org-file-apps-defaults-macosx)
  6836. ((eq system-type 'windows-nt)
  6837. org-file-apps-defaults-windowsnt)
  6838. (t org-file-apps-defaults-gnu)))
  6839. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6840. "Convert extensions to regular expressions in the cars of LIST.
  6841. Also, weed out any non-string entries, because the return value is used
  6842. only for regexp matching.
  6843. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6844. point to the symbol `emacs', indicating that the file should
  6845. be opened in Emacs."
  6846. (append
  6847. (delq nil
  6848. (mapcar (lambda (x)
  6849. (if (not (stringp (car x)))
  6850. nil
  6851. (if (string-match "\\W" (car x))
  6852. x
  6853. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6854. list))
  6855. (if add-auto-mode
  6856. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6857. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6858. (defun org-file-remote-p (file)
  6859. "Test whether FILE specifies a location on a remote system.
  6860. Return non-nil if the location is indeed remote.
  6861. For example, the filename \"/user@host:/foo\" specifies a location
  6862. on the system \"/user@host:\"."
  6863. (cond ((fboundp 'file-remote-p)
  6864. (file-remote-p file))
  6865. ((fboundp 'tramp-handle-file-remote-p)
  6866. (tramp-handle-file-remote-p file))
  6867. ((and (boundp 'ange-ftp-name-format)
  6868. (string-match (car ange-ftp-name-format) file))
  6869. t)
  6870. (t nil)))
  6871. ;;;; Refiling
  6872. (defun org-get-org-file ()
  6873. "Read a filename, with default directory `org-directory'."
  6874. (let ((default (or org-default-notes-file remember-data-file)))
  6875. (read-file-name (format "File name [%s]: " default)
  6876. (file-name-as-directory org-directory)
  6877. default)))
  6878. (defun org-notes-order-reversed-p ()
  6879. "Check if the current file should receive notes in reversed order."
  6880. (cond
  6881. ((not org-reverse-note-order) nil)
  6882. ((eq t org-reverse-note-order) t)
  6883. ((not (listp org-reverse-note-order)) nil)
  6884. (t (catch 'exit
  6885. (let ((all org-reverse-note-order)
  6886. entry)
  6887. (while (setq entry (pop all))
  6888. (if (string-match (car entry) buffer-file-name)
  6889. (throw 'exit (cdr entry))))
  6890. nil)))))
  6891. (defvar org-refile-target-table nil
  6892. "The list of refile targets, created by `org-refile'.")
  6893. (defvar org-agenda-new-buffers nil
  6894. "Buffers created to visit agenda files.")
  6895. (defun org-get-refile-targets (&optional default-buffer)
  6896. "Produce a table with refile targets."
  6897. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6898. targets txt re files f desc descre fast-path-p level)
  6899. (message "Getting targets...")
  6900. (with-current-buffer (or default-buffer (current-buffer))
  6901. (while (setq entry (pop entries))
  6902. (setq files (car entry) desc (cdr entry))
  6903. (setq fast-path-p nil)
  6904. (cond
  6905. ((null files) (setq files (list (current-buffer))))
  6906. ((eq files 'org-agenda-files)
  6907. (setq files (org-agenda-files 'unrestricted)))
  6908. ((and (symbolp files) (fboundp files))
  6909. (setq files (funcall files)))
  6910. ((and (symbolp files) (boundp files))
  6911. (setq files (symbol-value files))))
  6912. (if (stringp files) (setq files (list files)))
  6913. (cond
  6914. ((eq (car desc) :tag)
  6915. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6916. ((eq (car desc) :todo)
  6917. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6918. ((eq (car desc) :regexp)
  6919. (setq descre (cdr desc)))
  6920. ((eq (car desc) :level)
  6921. (setq descre (concat "^\\*\\{" (number-to-string
  6922. (if org-odd-levels-only
  6923. (1- (* 2 (cdr desc)))
  6924. (cdr desc)))
  6925. "\\}[ \t]")))
  6926. ((eq (car desc) :maxlevel)
  6927. (setq fast-path-p t)
  6928. (setq descre (concat "^\\*\\{1," (number-to-string
  6929. (if org-odd-levels-only
  6930. (1- (* 2 (cdr desc)))
  6931. (cdr desc)))
  6932. "\\}[ \t]")))
  6933. (t (error "Bad refiling target description %s" desc)))
  6934. (while (setq f (pop files))
  6935. (save-excursion
  6936. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6937. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6938. (setq f (expand-file-name f))
  6939. (save-excursion
  6940. (save-restriction
  6941. (widen)
  6942. (goto-char (point-min))
  6943. (while (re-search-forward descre nil t)
  6944. (goto-char (point-at-bol))
  6945. (when (looking-at org-complex-heading-regexp)
  6946. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  6947. txt (org-link-display-format (match-string 4))
  6948. re (concat "^" (regexp-quote
  6949. (buffer-substring (match-beginning 1)
  6950. (match-end 4)))))
  6951. (if (match-end 5) (setq re (concat re "[ \t]+"
  6952. (regexp-quote
  6953. (match-string 5)))))
  6954. (setq re (concat re "[ \t]*$"))
  6955. (when org-refile-use-outline-path
  6956. (setq txt (mapconcat 'org-protect-slash
  6957. (append
  6958. (if (eq org-refile-use-outline-path 'file)
  6959. (list (file-name-nondirectory
  6960. (buffer-file-name (buffer-base-buffer))))
  6961. (if (eq org-refile-use-outline-path 'full-file-path)
  6962. (list (buffer-file-name (buffer-base-buffer)))))
  6963. (org-get-outline-path fast-path-p level txt)
  6964. (list txt))
  6965. "/")))
  6966. (push (list txt f re (point)) targets))
  6967. (goto-char (point-at-eol))))))))
  6968. (message "Getting targets...done")
  6969. (nreverse targets))))
  6970. (defun org-protect-slash (s)
  6971. (while (string-match "/" s)
  6972. (setq s (replace-match "\\" t t s)))
  6973. s)
  6974. (defvar org-olpa (make-vector 20 nil))
  6975. (defun org-get-outline-path (&optional fastp level heading)
  6976. "Return the outline path to the current entry, as a list."
  6977. (if fastp
  6978. (progn
  6979. (if (> level 19)
  6980. (error "Outline path failure, more than 19 levels."))
  6981. (loop for i from level upto 19 do
  6982. (aset org-olpa i nil))
  6983. (prog1
  6984. (delq nil (append org-olpa nil))
  6985. (aset org-olpa level heading)))
  6986. (let (rtn)
  6987. (save-excursion
  6988. (while (org-up-heading-safe)
  6989. (when (looking-at org-complex-heading-regexp)
  6990. (push (org-match-string-no-properties 4) rtn)))
  6991. rtn))))
  6992. (defvar org-refile-history nil
  6993. "History for refiling operations.")
  6994. (defun org-refile (&optional goto default-buffer)
  6995. "Move the entry at point to another heading.
  6996. The list of target headings is compiled using the information in
  6997. `org-refile-targets', which see. This list is created before each use
  6998. and will therefore always be up-to-date.
  6999. At the target location, the entry is filed as a subitem of the target heading.
  7000. Depending on `org-reverse-note-order', the new subitem will either be the
  7001. first or the last subitem.
  7002. If there is an active region, all entries in that region will be moved.
  7003. However, the region must fulfil the requirement that the first heading
  7004. is the first one sets the top-level of the moved text - at most siblings
  7005. below it are allowed.
  7006. With prefix arg GOTO, the command will only visit the target location,
  7007. not actually move anything.
  7008. With a double prefix `C-u C-u', go to the location where the last refiling
  7009. operation has put the subtree."
  7010. (interactive "P")
  7011. (let* ((cbuf (current-buffer))
  7012. (regionp (org-region-active-p))
  7013. (region-start (and regionp (region-beginning)))
  7014. (region-end (and regionp (region-end)))
  7015. (region-length (and regionp (- region-end region-start)))
  7016. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7017. pos it nbuf file re level reversed)
  7018. (when regionp (goto-char region-start)
  7019. (unless (org-kill-is-subtree-p
  7020. (buffer-substring region-start region-end))
  7021. (error "The region is not a (sequence of) subtree(s)")))
  7022. (if (equal goto '(16))
  7023. (org-refile-goto-last-stored)
  7024. (when (setq it (org-refile-get-location
  7025. (if goto "Goto: " "Refile to: ") default-buffer))
  7026. (setq file (nth 1 it)
  7027. re (nth 2 it)
  7028. pos (nth 3 it))
  7029. (if (and (equal (buffer-file-name) file)
  7030. (if regionp
  7031. (and (>= pos region-start)
  7032. (<= pos region-end))
  7033. (and (>= pos (point))
  7034. (< pos (save-excursion
  7035. (org-end-of-subtree t t))))))
  7036. (error "Cannot refile to position inside the tree or region"))
  7037. (setq nbuf (or (find-buffer-visiting file)
  7038. (find-file-noselect file)))
  7039. (if goto
  7040. (progn
  7041. (switch-to-buffer nbuf)
  7042. (goto-char pos)
  7043. (org-show-context 'org-goto))
  7044. (if regionp
  7045. (progn
  7046. (kill-new (buffer-substring region-start region-end))
  7047. (org-save-markers-in-region region-start region-end))
  7048. (org-copy-subtree 1 nil t))
  7049. (save-excursion
  7050. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7051. (find-file-noselect file))))
  7052. (setq reversed (org-notes-order-reversed-p))
  7053. (save-excursion
  7054. (save-restriction
  7055. (widen)
  7056. (goto-char pos)
  7057. (looking-at outline-regexp)
  7058. (setq level (org-get-valid-level (funcall outline-level) 1))
  7059. (goto-char
  7060. (if reversed
  7061. (or (outline-next-heading) (point-max))
  7062. (or (save-excursion (outline-get-next-sibling))
  7063. (org-end-of-subtree t t)
  7064. (point-max))))
  7065. (if (not (bolp)) (newline))
  7066. (bookmark-set "org-refile-last-stored")
  7067. (org-paste-subtree level))))
  7068. (if regionp
  7069. (delete-region (point) (+ (point) region-length))
  7070. (org-cut-subtree))
  7071. (setq org-markers-to-move nil)
  7072. (message "Refiled to \"%s\"" (car it)))))))
  7073. (defun org-refile-goto-last-stored ()
  7074. "Go to the location where the last refile was stored."
  7075. (interactive)
  7076. (bookmark-jump "org-refile-last-stored")
  7077. (message "This is the location of the last refile"))
  7078. (defun org-refile-get-location (&optional prompt default-buffer)
  7079. "Prompt the user for a refile location, using PROMPT."
  7080. (let ((org-refile-targets org-refile-targets)
  7081. (org-refile-use-outline-path org-refile-use-outline-path))
  7082. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7083. (unless org-refile-target-table
  7084. (error "No refile targets"))
  7085. (let* ((cbuf (current-buffer))
  7086. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7087. (cfunc (if (and org-refile-use-outline-path
  7088. org-outline-path-complete-in-steps)
  7089. 'org-olpath-completing-read
  7090. 'org-ido-completing-read))
  7091. (extra (if org-refile-use-outline-path "/" ""))
  7092. (filename (and cfn (expand-file-name cfn)))
  7093. (tbl (mapcar
  7094. (lambda (x)
  7095. (if (not (equal filename (nth 1 x)))
  7096. (cons (concat (car x) extra " ("
  7097. (file-name-nondirectory (nth 1 x)) ")")
  7098. (cdr x))
  7099. (cons (concat (car x) extra) (cdr x))))
  7100. org-refile-target-table))
  7101. (completion-ignore-case t))
  7102. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7103. tbl)))
  7104. (defun org-olpath-completing-read (prompt collection &rest args)
  7105. "Read an outline path like a file name."
  7106. (let ((thetable collection))
  7107. (apply
  7108. 'org-ido-completing-read prompt
  7109. (lambda (string predicate &optional flag)
  7110. (let (rtn r f (l (length string)))
  7111. (cond
  7112. ((eq flag nil)
  7113. ;; try completion
  7114. (try-completion string thetable))
  7115. ((eq flag t)
  7116. ;; all-completions
  7117. (setq rtn (all-completions string thetable predicate))
  7118. (mapcar
  7119. (lambda (x)
  7120. (setq r (substring x l))
  7121. (if (string-match " ([^)]*)$" x)
  7122. (setq f (match-string 0 x))
  7123. (setq f ""))
  7124. (if (string-match "/" r)
  7125. (concat string (substring r 0 (match-end 0)) f)
  7126. x))
  7127. rtn))
  7128. ((eq flag 'lambda)
  7129. ;; exact match?
  7130. (assoc string thetable)))
  7131. ))
  7132. args)))
  7133. ;;;; Dynamic blocks
  7134. (defun org-find-dblock (name)
  7135. "Find the first dynamic block with name NAME in the buffer.
  7136. If not found, stay at current position and return nil."
  7137. (let (pos)
  7138. (save-excursion
  7139. (goto-char (point-min))
  7140. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7141. nil t)
  7142. (match-beginning 0))))
  7143. (if pos (goto-char pos))
  7144. pos))
  7145. (defconst org-dblock-start-re
  7146. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7147. "Matches the startline of a dynamic block, with parameters.")
  7148. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7149. "Matches the end of a dynamic block.")
  7150. (defun org-create-dblock (plist)
  7151. "Create a dynamic block section, with parameters taken from PLIST.
  7152. PLIST must contain a :name entry which is used as name of the block."
  7153. (unless (bolp) (newline))
  7154. (let ((name (plist-get plist :name)))
  7155. (insert "#+BEGIN: " name)
  7156. (while plist
  7157. (if (eq (car plist) :name)
  7158. (setq plist (cddr plist))
  7159. (insert " " (prin1-to-string (pop plist)))))
  7160. (insert "\n\n#+END:\n")
  7161. (beginning-of-line -2)))
  7162. (defun org-prepare-dblock ()
  7163. "Prepare dynamic block for refresh.
  7164. This empties the block, puts the cursor at the insert position and returns
  7165. the property list including an extra property :name with the block name."
  7166. (unless (looking-at org-dblock-start-re)
  7167. (error "Not at a dynamic block"))
  7168. (let* ((begdel (1+ (match-end 0)))
  7169. (name (org-no-properties (match-string 1)))
  7170. (params (append (list :name name)
  7171. (read (concat "(" (match-string 3) ")")))))
  7172. (unless (re-search-forward org-dblock-end-re nil t)
  7173. (error "Dynamic block not terminated"))
  7174. (setq params
  7175. (append params
  7176. (list :content (buffer-substring
  7177. begdel (match-beginning 0)))))
  7178. (delete-region begdel (match-beginning 0))
  7179. (goto-char begdel)
  7180. (open-line 1)
  7181. params))
  7182. (defun org-map-dblocks (&optional command)
  7183. "Apply COMMAND to all dynamic blocks in the current buffer.
  7184. If COMMAND is not given, use `org-update-dblock'."
  7185. (let ((cmd (or command 'org-update-dblock))
  7186. pos)
  7187. (save-excursion
  7188. (goto-char (point-min))
  7189. (while (re-search-forward org-dblock-start-re nil t)
  7190. (goto-char (setq pos (match-beginning 0)))
  7191. (condition-case nil
  7192. (funcall cmd)
  7193. (error (message "Error during update of dynamic block")))
  7194. (goto-char pos)
  7195. (unless (re-search-forward org-dblock-end-re nil t)
  7196. (error "Dynamic block not terminated"))))))
  7197. (defun org-dblock-update (&optional arg)
  7198. "User command for updating dynamic blocks.
  7199. Update the dynamic block at point. With prefix ARG, update all dynamic
  7200. blocks in the buffer."
  7201. (interactive "P")
  7202. (if arg
  7203. (org-update-all-dblocks)
  7204. (or (looking-at org-dblock-start-re)
  7205. (org-beginning-of-dblock))
  7206. (org-update-dblock)))
  7207. (defun org-update-dblock ()
  7208. "Update the dynamic block at point
  7209. This means to empty the block, parse for parameters and then call
  7210. the correct writing function."
  7211. (save-window-excursion
  7212. (let* ((pos (point))
  7213. (line (org-current-line))
  7214. (params (org-prepare-dblock))
  7215. (name (plist-get params :name))
  7216. (cmd (intern (concat "org-dblock-write:" name))))
  7217. (message "Updating dynamic block `%s' at line %d..." name line)
  7218. (funcall cmd params)
  7219. (message "Updating dynamic block `%s' at line %d...done" name line)
  7220. (goto-char pos))))
  7221. (defun org-beginning-of-dblock ()
  7222. "Find the beginning of the dynamic block at point.
  7223. Error if there is no such block at point."
  7224. (let ((pos (point))
  7225. beg)
  7226. (end-of-line 1)
  7227. (if (and (re-search-backward org-dblock-start-re nil t)
  7228. (setq beg (match-beginning 0))
  7229. (re-search-forward org-dblock-end-re nil t)
  7230. (> (match-end 0) pos))
  7231. (goto-char beg)
  7232. (goto-char pos)
  7233. (error "Not in a dynamic block"))))
  7234. (defun org-update-all-dblocks ()
  7235. "Update all dynamic blocks in the buffer.
  7236. This function can be used in a hook."
  7237. (when (org-mode-p)
  7238. (org-map-dblocks 'org-update-dblock)))
  7239. ;;;; Completion
  7240. (defconst org-additional-option-like-keywords
  7241. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7242. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7243. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7244. "BEGIN_QUOTE" "END_QUOTE"
  7245. "BEGIN_VERSE" "END_VERSE"
  7246. "BEGIN_SRC" "END_SRC"
  7247. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7248. (defcustom org-structure-template-alist
  7249. '(
  7250. ("s" "#+begin_src ?\n\n#+end_src"
  7251. "<src lang=\"?\">\n\n</src>")
  7252. ("e" "#+begin_example\n?\n#+end_example"
  7253. "<example>\n?\n</example>")
  7254. ("q" "#+begin_quote\n?\n#+end_quote"
  7255. "<quote>\n?\n</quote>")
  7256. ("v" "#+begin_verse\n?\n#+end_verse"
  7257. "<verse>\n?\n/verse>")
  7258. ("l" "#+begin_latex\n?\n#+end_latex"
  7259. "<literal style=\"latex\">\n?\n</literal>")
  7260. ("L" "#+latex: "
  7261. "<literal style=\"latex\">?</literal>")
  7262. ("h" "#+begin_html\n?\n#+end_html"
  7263. "<literal style=\"html\">\n?\n</literal>")
  7264. ("H" "#+html: "
  7265. "<literal style=\"html\">?</literal>")
  7266. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7267. ("A" "#+ascii: ")
  7268. ("i" "#+include %file ?"
  7269. "<include file=%file markup=\"?\">")
  7270. )
  7271. "Structure completion elements.
  7272. This is a list of abbreviation keys and values. The value gets inserted
  7273. it you type @samp{.} followed by the key and then the completion key,
  7274. usually `M-TAB'. %file will be replaced by a file name after prompting
  7275. for the file using completion.
  7276. There are two templates for each key, the first uses the original Org syntax,
  7277. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7278. the default when the /org-mtags.el/ module has been loaded. See also the
  7279. variable `org-mtags-prefer-muse-templates'.
  7280. This is an experimental feature, it is undecided if it is going to stay in."
  7281. :group 'org-completion
  7282. :type '(repeat
  7283. (string :tag "Key")
  7284. (string :tag "Template")
  7285. (string :tag "Muse Template")))
  7286. (defun org-try-structure-completion ()
  7287. "Try to complete a structure template before point.
  7288. This looks for strings like \"<e\" on an otherwise empty line and
  7289. expands them."
  7290. (let ((l (buffer-substring (point-at-bol) (point)))
  7291. a)
  7292. (when (and (looking-at "[ \t]*$")
  7293. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7294. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7295. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7296. (match-beginning 1)) a)
  7297. t)))
  7298. (defun org-complete-expand-structure-template (start cell)
  7299. "Expand a structure template."
  7300. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7301. (rpl (nth (if musep 2 1) cell)))
  7302. (delete-region start (point))
  7303. (when (string-match "\\`#\\+" rpl)
  7304. (cond
  7305. ((bolp))
  7306. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7307. (delete-region (point-at-bol) (point)))
  7308. (t (newline))))
  7309. (setq start (point))
  7310. (if (string-match "%file" rpl)
  7311. (setq rpl (replace-match
  7312. (concat
  7313. "\""
  7314. (save-match-data
  7315. (abbreviate-file-name (read-file-name "Include file: ")))
  7316. "\"")
  7317. t t rpl)))
  7318. (insert rpl)
  7319. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7320. (defun org-complete (&optional arg)
  7321. "Perform completion on word at point.
  7322. At the beginning of a headline, this completes TODO keywords as given in
  7323. `org-todo-keywords'.
  7324. If the current word is preceded by a backslash, completes the TeX symbols
  7325. that are supported for HTML support.
  7326. If the current word is preceded by \"#+\", completes special words for
  7327. setting file options.
  7328. In the line after \"#+STARTUP:, complete valid keywords.\"
  7329. At all other locations, this simply calls the value of
  7330. `org-completion-fallback-command'."
  7331. (interactive "P")
  7332. (org-without-partial-completion
  7333. (catch 'exit
  7334. (let* ((a nil)
  7335. (end (point))
  7336. (beg1 (save-excursion
  7337. (skip-chars-backward (org-re "[:alnum:]_@"))
  7338. (point)))
  7339. (beg (save-excursion
  7340. (skip-chars-backward "a-zA-Z0-9_:$")
  7341. (point)))
  7342. (confirm (lambda (x) (stringp (car x))))
  7343. (searchhead (equal (char-before beg) ?*))
  7344. (struct
  7345. (when (and (member (char-before beg1) '(?. ?<))
  7346. (setq a (assoc (buffer-substring beg1 (point))
  7347. org-structure-template-alist)))
  7348. (org-complete-expand-structure-template (1- beg1) a)
  7349. (throw 'exit t)))
  7350. (tag (and (equal (char-before beg1) ?:)
  7351. (equal (char-after (point-at-bol)) ?*)))
  7352. (prop (and (equal (char-before beg1) ?:)
  7353. (not (equal (char-after (point-at-bol)) ?*))))
  7354. (texp (equal (char-before beg) ?\\))
  7355. (link (equal (char-before beg) ?\[))
  7356. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7357. beg)
  7358. "#+"))
  7359. (startup (string-match "^#\\+STARTUP:.*"
  7360. (buffer-substring (point-at-bol) (point))))
  7361. (completion-ignore-case opt)
  7362. (type nil)
  7363. (tbl nil)
  7364. (table (cond
  7365. (opt
  7366. (setq type :opt)
  7367. (require 'org-exp)
  7368. (append
  7369. (mapcar
  7370. (lambda (x)
  7371. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7372. (cons (match-string 2 x) (match-string 1 x)))
  7373. (org-split-string (org-get-current-options) "\n"))
  7374. (mapcar 'list org-additional-option-like-keywords)))
  7375. (startup
  7376. (setq type :startup)
  7377. org-startup-options)
  7378. (link (append org-link-abbrev-alist-local
  7379. org-link-abbrev-alist))
  7380. (texp
  7381. (setq type :tex)
  7382. org-html-entities)
  7383. ((string-match "\\`\\*+[ \t]+\\'"
  7384. (buffer-substring (point-at-bol) beg))
  7385. (setq type :todo)
  7386. (mapcar 'list org-todo-keywords-1))
  7387. (searchhead
  7388. (setq type :searchhead)
  7389. (save-excursion
  7390. (goto-char (point-min))
  7391. (while (re-search-forward org-todo-line-regexp nil t)
  7392. (push (list
  7393. (org-make-org-heading-search-string
  7394. (match-string 3) t))
  7395. tbl)))
  7396. tbl)
  7397. (tag (setq type :tag beg beg1)
  7398. (or org-tag-alist (org-get-buffer-tags)))
  7399. (prop (setq type :prop beg beg1)
  7400. (mapcar 'list (org-buffer-property-keys nil t t)))
  7401. (t (progn
  7402. (call-interactively org-completion-fallback-command)
  7403. (throw 'exit nil)))))
  7404. (pattern (buffer-substring-no-properties beg end))
  7405. (completion (try-completion pattern table confirm)))
  7406. (cond ((eq completion t)
  7407. (if (not (assoc (upcase pattern) table))
  7408. (message "Already complete")
  7409. (if (and (equal type :opt)
  7410. (not (member (car (assoc (upcase pattern) table))
  7411. org-additional-option-like-keywords)))
  7412. (insert (substring (cdr (assoc (upcase pattern) table))
  7413. (length pattern)))
  7414. (if (memq type '(:tag :prop)) (insert ":")))))
  7415. ((null completion)
  7416. (message "Can't find completion for \"%s\"" pattern)
  7417. (ding))
  7418. ((not (string= pattern completion))
  7419. (delete-region beg end)
  7420. (if (string-match " +$" completion)
  7421. (setq completion (replace-match "" t t completion)))
  7422. (insert completion)
  7423. (if (get-buffer-window "*Completions*")
  7424. (delete-window (get-buffer-window "*Completions*")))
  7425. (if (assoc completion table)
  7426. (if (eq type :todo) (insert " ")
  7427. (if (memq type '(:tag :prop)) (insert ":"))))
  7428. (if (and (equal type :opt) (assoc completion table))
  7429. (message "%s" (substitute-command-keys
  7430. "Press \\[org-complete] again to insert example settings"))))
  7431. (t
  7432. (message "Making completion list...")
  7433. (let ((list (sort (all-completions pattern table confirm)
  7434. 'string<)))
  7435. (with-output-to-temp-buffer "*Completions*"
  7436. (condition-case nil
  7437. ;; Protection needed for XEmacs and emacs 21
  7438. (display-completion-list list pattern)
  7439. (error (display-completion-list list)))))
  7440. (message "Making completion list...%s" "done")))))))
  7441. ;;;; TODO, DEADLINE, Comments
  7442. (defun org-toggle-comment ()
  7443. "Change the COMMENT state of an entry."
  7444. (interactive)
  7445. (save-excursion
  7446. (org-back-to-heading)
  7447. (let (case-fold-search)
  7448. (if (looking-at (concat outline-regexp
  7449. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7450. (replace-match "" t t nil 1)
  7451. (if (looking-at outline-regexp)
  7452. (progn
  7453. (goto-char (match-end 0))
  7454. (insert org-comment-string " ")))))))
  7455. (defvar org-last-todo-state-is-todo nil
  7456. "This is non-nil when the last TODO state change led to a TODO state.
  7457. If the last change removed the TODO tag or switched to DONE, then
  7458. this is nil.")
  7459. (defvar org-setting-tags nil) ; dynamically skipped
  7460. (defun org-parse-local-options (string var)
  7461. "Parse STRING for startup setting relevant for variable VAR."
  7462. (let ((rtn (symbol-value var))
  7463. e opts)
  7464. (save-match-data
  7465. (if (or (not string) (not (string-match "\\S-" string)))
  7466. rtn
  7467. (setq opts (delq nil (mapcar (lambda (x)
  7468. (setq e (assoc x org-startup-options))
  7469. (if (eq (nth 1 e) var) e nil))
  7470. (org-split-string string "[ \t]+"))))
  7471. (if (not opts)
  7472. rtn
  7473. (setq rtn nil)
  7474. (while (setq e (pop opts))
  7475. (if (not (nth 3 e))
  7476. (setq rtn (nth 2 e))
  7477. (if (not (listp rtn)) (setq rtn nil))
  7478. (push (nth 2 e) rtn)))
  7479. rtn)))))
  7480. (defvar org-blocker-hook nil
  7481. "Hook for functions that are allowed to block a state change.
  7482. Each function gets as its single argument a property list, see
  7483. `org-trigger-hook' for more information about this list.
  7484. If any of the functions in this hook returns nil, the state change
  7485. is blocked.")
  7486. (defvar org-trigger-hook nil
  7487. "Hook for functions that are triggered by a state change.
  7488. Each function gets as its single argument a property list with at least
  7489. the following elements:
  7490. (:type type-of-change :position pos-at-entry-start
  7491. :from old-state :to new-state)
  7492. Depending on the type, more properties may be present.
  7493. This mechanism is currently implemented for:
  7494. TODO state changes
  7495. ------------------
  7496. :type todo-state-change
  7497. :from previous state (keyword as a string), or nil
  7498. :to new state (keyword as a string), or nil")
  7499. (defvar org-agenda-headline-snapshot-before-repeat)
  7500. (defun org-todo (&optional arg)
  7501. "Change the TODO state of an item.
  7502. The state of an item is given by a keyword at the start of the heading,
  7503. like
  7504. *** TODO Write paper
  7505. *** DONE Call mom
  7506. The different keywords are specified in the variable `org-todo-keywords'.
  7507. By default the available states are \"TODO\" and \"DONE\".
  7508. So for this example: when the item starts with TODO, it is changed to DONE.
  7509. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7510. DONE are present, add TODO at the beginning of the heading.
  7511. With C-u prefix arg, use completion to determine the new state.
  7512. With numeric prefix arg, switch to that state.
  7513. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7514. For calling through lisp, arg is also interpreted in the following way:
  7515. 'none -> empty state
  7516. \"\"(empty string) -> switch to empty state
  7517. 'done -> switch to DONE
  7518. 'nextset -> switch to the next set of keywords
  7519. 'previousset -> switch to the previous set of keywords
  7520. \"WAITING\" -> switch to the specified keyword, but only if it
  7521. really is a member of `org-todo-keywords'."
  7522. (interactive "P")
  7523. (if (equal arg '(16)) (setq arg 'nextset))
  7524. (save-excursion
  7525. (catch 'exit
  7526. (org-back-to-heading)
  7527. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7528. (or (looking-at (concat " +" org-todo-regexp " *"))
  7529. (looking-at " *"))
  7530. (let* ((match-data (match-data))
  7531. (startpos (point-at-bol))
  7532. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7533. (org-log-done org-log-done)
  7534. (org-log-repeat org-log-repeat)
  7535. (org-todo-log-states org-todo-log-states)
  7536. (this (match-string 1))
  7537. (hl-pos (match-beginning 0))
  7538. (head (org-get-todo-sequence-head this))
  7539. (ass (assoc head org-todo-kwd-alist))
  7540. (interpret (nth 1 ass))
  7541. (done-word (nth 3 ass))
  7542. (final-done-word (nth 4 ass))
  7543. (last-state (or this ""))
  7544. (completion-ignore-case t)
  7545. (member (member this org-todo-keywords-1))
  7546. (tail (cdr member))
  7547. (state (cond
  7548. ((and org-todo-key-trigger
  7549. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7550. (and (not arg) org-use-fast-todo-selection
  7551. (not (eq org-use-fast-todo-selection 'prefix)))))
  7552. ;; Use fast selection
  7553. (org-fast-todo-selection))
  7554. ((and (equal arg '(4))
  7555. (or (not org-use-fast-todo-selection)
  7556. (not org-todo-key-trigger)))
  7557. ;; Read a state with completion
  7558. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7559. org-todo-keywords-1)
  7560. nil t))
  7561. ((eq arg 'right)
  7562. (if this
  7563. (if tail (car tail) nil)
  7564. (car org-todo-keywords-1)))
  7565. ((eq arg 'left)
  7566. (if (equal member org-todo-keywords-1)
  7567. nil
  7568. (if this
  7569. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7570. org-todo-keywords-1)
  7571. (org-last org-todo-keywords-1))))
  7572. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7573. (setq arg nil))) ; hack to fall back to cycling
  7574. (arg
  7575. ;; user or caller requests a specific state
  7576. (cond
  7577. ((equal arg "") nil)
  7578. ((eq arg 'none) nil)
  7579. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7580. ((eq arg 'nextset)
  7581. (or (car (cdr (member head org-todo-heads)))
  7582. (car org-todo-heads)))
  7583. ((eq arg 'previousset)
  7584. (let ((org-todo-heads (reverse org-todo-heads)))
  7585. (or (car (cdr (member head org-todo-heads)))
  7586. (car org-todo-heads))))
  7587. ((car (member arg org-todo-keywords-1)))
  7588. ((nth (1- (prefix-numeric-value arg))
  7589. org-todo-keywords-1))))
  7590. ((null member) (or head (car org-todo-keywords-1)))
  7591. ((equal this final-done-word) nil) ;; -> make empty
  7592. ((null tail) nil) ;; -> first entry
  7593. ((eq interpret 'sequence)
  7594. (car tail))
  7595. ((memq interpret '(type priority))
  7596. (if (eq this-command last-command)
  7597. (car tail)
  7598. (if (> (length tail) 0)
  7599. (or done-word (car org-done-keywords))
  7600. nil)))
  7601. (t nil)))
  7602. (next (if state (concat " " state " ") " "))
  7603. (change-plist (list :type 'todo-state-change :from this :to state
  7604. :position startpos))
  7605. dolog now-done-p)
  7606. (when org-blocker-hook
  7607. (unless (save-excursion
  7608. (save-match-data
  7609. (run-hook-with-args-until-failure
  7610. 'org-blocker-hook change-plist)))
  7611. (if (interactive-p)
  7612. (error "TODO state change from %s to %s blocked" this state)
  7613. ;; fail silently
  7614. (message "TODO state change from %s to %s blocked" this state)
  7615. (throw 'exit nil))))
  7616. (store-match-data match-data)
  7617. (replace-match next t t)
  7618. (unless (pos-visible-in-window-p hl-pos)
  7619. (message "TODO state changed to %s" (org-trim next)))
  7620. (unless head
  7621. (setq head (org-get-todo-sequence-head state)
  7622. ass (assoc head org-todo-kwd-alist)
  7623. interpret (nth 1 ass)
  7624. done-word (nth 3 ass)
  7625. final-done-word (nth 4 ass)))
  7626. (when (memq arg '(nextset previousset))
  7627. (message "Keyword-Set %d/%d: %s"
  7628. (- (length org-todo-sets) -1
  7629. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7630. (length org-todo-sets)
  7631. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7632. (setq org-last-todo-state-is-todo
  7633. (not (member state org-done-keywords)))
  7634. (setq now-done-p (and (member state org-done-keywords)
  7635. (not (member this org-done-keywords))))
  7636. (and logging (org-local-logging logging))
  7637. (when (and (or org-todo-log-states org-log-done)
  7638. (not (memq arg '(nextset previousset))))
  7639. ;; we need to look at recording a time and note
  7640. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7641. (nth 2 (assoc this org-todo-log-states))))
  7642. (when (and state
  7643. (member state org-not-done-keywords)
  7644. (not (member this org-not-done-keywords)))
  7645. ;; This is now a todo state and was not one before
  7646. ;; If there was a CLOSED time stamp, get rid of it.
  7647. (org-add-planning-info nil nil 'closed))
  7648. (when (and now-done-p org-log-done)
  7649. ;; It is now done, and it was not done before
  7650. (org-add-planning-info 'closed (org-current-time))
  7651. (if (and (not dolog) (eq 'note org-log-done))
  7652. (org-add-log-setup 'done state 'findpos 'note)))
  7653. (when (and state dolog)
  7654. ;; This is a non-nil state, and we need to log it
  7655. (org-add-log-setup 'state state 'findpos dolog)))
  7656. ;; Fixup tag positioning
  7657. (org-todo-trigger-tag-changes state)
  7658. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7659. (when org-provide-todo-statistics
  7660. (org-update-parent-todo-statistics))
  7661. (run-hooks 'org-after-todo-state-change-hook)
  7662. (if (and arg (not (member state org-done-keywords)))
  7663. (setq head (org-get-todo-sequence-head state)))
  7664. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7665. ;; Do we need to trigger a repeat?
  7666. (when now-done-p
  7667. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7668. ;; This is for the agenda, take a snapshot of the headline.
  7669. (save-match-data
  7670. (setq org-agenda-headline-snapshot-before-repeat
  7671. (org-get-heading))))
  7672. (org-auto-repeat-maybe state))
  7673. ;; Fixup cursor location if close to the keyword
  7674. (if (and (outline-on-heading-p)
  7675. (not (bolp))
  7676. (save-excursion (beginning-of-line 1)
  7677. (looking-at org-todo-line-regexp))
  7678. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7679. (progn
  7680. (goto-char (or (match-end 2) (match-end 1)))
  7681. (just-one-space)))
  7682. (when org-trigger-hook
  7683. (save-excursion
  7684. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7685. (defun org-update-parent-todo-statistics ()
  7686. "Update any statistics cookie in the parent of the current headline."
  7687. (interactive)
  7688. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7689. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7690. (catch 'exit
  7691. (save-excursion
  7692. (setq level (org-up-heading-safe))
  7693. (unless (and level
  7694. (re-search-forward box-re (point-at-eol) t))
  7695. (throw 'exit nil))
  7696. (setq is-percent (match-end 2))
  7697. (save-match-data
  7698. (unless (outline-next-heading) (throw 'exit nil))
  7699. (while (looking-at org-todo-line-regexp)
  7700. (setq kwd (match-string 2))
  7701. (and kwd (setq cnt-all (1+ cnt-all)))
  7702. (and (member kwd org-done-keywords)
  7703. (setq cnt-done (1+ cnt-done)))
  7704. (condition-case nil
  7705. (org-forward-same-level 1)
  7706. (error (end-of-line 1)))))
  7707. (replace-match
  7708. (if is-percent
  7709. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7710. (format "[%d/%d]" cnt-done cnt-all)))
  7711. (run-hook-with-args 'org-after-todo-statistics-hook
  7712. cnt-done (- cnt-all cnt-done))))))
  7713. (defvar org-after-todo-statistics-hook nil
  7714. "Hook that is called after a TODO statistics cookie has been updated.
  7715. Each function is called with two arguments: the number of not-done entries
  7716. and the number of done entries.
  7717. For example, the following function, when added to this hook, will switch
  7718. an entry to DONE when all children are done, and back to TODO when new
  7719. entries are set to a TODO status. Note that this hook is only called
  7720. when there is a statistics cookie in the headline!
  7721. (defun org-summary-todo (n-done n-not-done)
  7722. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7723. (let (org-log-done org-log-states) ; turn off logging
  7724. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7725. ")
  7726. (defun org-todo-trigger-tag-changes (state)
  7727. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7728. (let ((l org-todo-state-tags-triggers)
  7729. changes)
  7730. (when (or (not state) (equal state ""))
  7731. (setq changes (append changes (cdr (assoc "" l)))))
  7732. (when (and (stringp state) (> (length state) 0))
  7733. (setq changes (append changes (cdr (assoc state l)))))
  7734. (when (member state org-not-done-keywords)
  7735. (setq changes (append changes (cdr (assoc 'todo l)))))
  7736. (when (member state org-done-keywords)
  7737. (setq changes (append changes (cdr (assoc 'done l)))))
  7738. (dolist (c changes)
  7739. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7740. (defun org-local-logging (value)
  7741. "Get logging settings from a property VALUE."
  7742. (let* (words w a)
  7743. ;; directly set the variables, they are already local.
  7744. (setq org-log-done nil
  7745. org-log-repeat nil
  7746. org-todo-log-states nil)
  7747. (setq words (org-split-string value))
  7748. (while (setq w (pop words))
  7749. (cond
  7750. ((setq a (assoc w org-startup-options))
  7751. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7752. (set (nth 1 a) (nth 2 a))))
  7753. ((setq a (org-extract-log-state-settings w))
  7754. (and (member (car a) org-todo-keywords-1)
  7755. (push a org-todo-log-states)))))))
  7756. (defun org-get-todo-sequence-head (kwd)
  7757. "Return the head of the TODO sequence to which KWD belongs.
  7758. If KWD is not set, check if there is a text property remembering the
  7759. right sequence."
  7760. (let (p)
  7761. (cond
  7762. ((not kwd)
  7763. (or (get-text-property (point-at-bol) 'org-todo-head)
  7764. (progn
  7765. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7766. nil (point-at-eol)))
  7767. (get-text-property p 'org-todo-head))))
  7768. ((not (member kwd org-todo-keywords-1))
  7769. (car org-todo-keywords-1))
  7770. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7771. (defun org-fast-todo-selection ()
  7772. "Fast TODO keyword selection with single keys.
  7773. Returns the new TODO keyword, or nil if no state change should occur."
  7774. (let* ((fulltable org-todo-key-alist)
  7775. (done-keywords org-done-keywords) ;; needed for the faces.
  7776. (maxlen (apply 'max (mapcar
  7777. (lambda (x)
  7778. (if (stringp (car x)) (string-width (car x)) 0))
  7779. fulltable)))
  7780. (expert nil)
  7781. (fwidth (+ maxlen 3 1 3))
  7782. (ncol (/ (- (window-width) 4) fwidth))
  7783. tg cnt e c tbl
  7784. groups ingroup)
  7785. (save-window-excursion
  7786. (if expert
  7787. (set-buffer (get-buffer-create " *Org todo*"))
  7788. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7789. (erase-buffer)
  7790. (org-set-local 'org-done-keywords done-keywords)
  7791. (setq tbl fulltable cnt 0)
  7792. (while (setq e (pop tbl))
  7793. (cond
  7794. ((equal e '(:startgroup))
  7795. (push '() groups) (setq ingroup t)
  7796. (when (not (= cnt 0))
  7797. (setq cnt 0)
  7798. (insert "\n"))
  7799. (insert "{ "))
  7800. ((equal e '(:endgroup))
  7801. (setq ingroup nil cnt 0)
  7802. (insert "}\n"))
  7803. (t
  7804. (setq tg (car e) c (cdr e))
  7805. (if ingroup (push tg (car groups)))
  7806. (setq tg (org-add-props tg nil 'face
  7807. (org-get-todo-face tg)))
  7808. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7809. (insert "[" c "] " tg (make-string
  7810. (- fwidth 4 (length tg)) ?\ ))
  7811. (when (= (setq cnt (1+ cnt)) ncol)
  7812. (insert "\n")
  7813. (if ingroup (insert " "))
  7814. (setq cnt 0)))))
  7815. (insert "\n")
  7816. (goto-char (point-min))
  7817. (if (not expert) (org-fit-window-to-buffer))
  7818. (message "[a-z..]:Set [SPC]:clear")
  7819. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7820. (cond
  7821. ((or (= c ?\C-g)
  7822. (and (= c ?q) (not (rassoc c fulltable))))
  7823. (setq quit-flag t))
  7824. ((= c ?\ ) nil)
  7825. ((setq e (rassoc c fulltable) tg (car e))
  7826. tg)
  7827. (t (setq quit-flag t))))))
  7828. (defun org-entry-is-todo-p ()
  7829. (member (org-get-todo-state) org-not-done-keywords))
  7830. (defun org-entry-is-done-p ()
  7831. (member (org-get-todo-state) org-done-keywords))
  7832. (defun org-get-todo-state ()
  7833. (save-excursion
  7834. (org-back-to-heading t)
  7835. (and (looking-at org-todo-line-regexp)
  7836. (match-end 2)
  7837. (match-string 2))))
  7838. (defun org-at-date-range-p (&optional inactive-ok)
  7839. "Is the cursor inside a date range?"
  7840. (interactive)
  7841. (save-excursion
  7842. (catch 'exit
  7843. (let ((pos (point)))
  7844. (skip-chars-backward "^[<\r\n")
  7845. (skip-chars-backward "<[")
  7846. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7847. (>= (match-end 0) pos)
  7848. (throw 'exit t))
  7849. (skip-chars-backward "^<[\r\n")
  7850. (skip-chars-backward "<[")
  7851. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7852. (>= (match-end 0) pos)
  7853. (throw 'exit t)))
  7854. nil)))
  7855. (defun org-get-repeat ()
  7856. "Check if there is a deadline/schedule with repeater in this entry."
  7857. (save-match-data
  7858. (save-excursion
  7859. (org-back-to-heading t)
  7860. (if (re-search-forward
  7861. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7862. (match-string 1)))))
  7863. (defvar org-last-changed-timestamp)
  7864. (defvar org-last-inserted-timestamp)
  7865. (defvar org-log-post-message)
  7866. (defvar org-log-note-purpose)
  7867. (defvar org-log-note-how)
  7868. (defvar org-log-note-extra)
  7869. (defun org-auto-repeat-maybe (done-word)
  7870. "Check if the current headline contains a repeated deadline/schedule.
  7871. If yes, set TODO state back to what it was and change the base date
  7872. of repeating deadline/scheduled time stamps to new date.
  7873. This function is run automatically after each state change to a DONE state."
  7874. ;; last-state is dynamically scoped into this function
  7875. (let* ((repeat (org-get-repeat))
  7876. (aa (assoc last-state org-todo-kwd-alist))
  7877. (interpret (nth 1 aa))
  7878. (head (nth 2 aa))
  7879. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7880. (msg "Entry repeats: ")
  7881. (org-log-done nil)
  7882. (org-todo-log-states nil)
  7883. (nshiftmax 10) (nshift 0)
  7884. re type n what ts time)
  7885. (when repeat
  7886. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7887. (org-todo (if (eq interpret 'type) last-state head))
  7888. (when org-log-repeat
  7889. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7890. (memq 'org-add-log-note post-command-hook))
  7891. ;; OK, we are already setup for some record
  7892. (if (eq org-log-repeat 'note)
  7893. ;; make sure we take a note, not only a time stamp
  7894. (setq org-log-note-how 'note))
  7895. ;; Set up for taking a record
  7896. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7897. 'findpos org-log-repeat)))
  7898. (org-back-to-heading t)
  7899. (org-add-planning-info nil nil 'closed)
  7900. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7901. org-deadline-time-regexp "\\)\\|\\("
  7902. org-ts-regexp "\\)"))
  7903. (while (re-search-forward
  7904. re (save-excursion (outline-next-heading) (point)) t)
  7905. (setq type (if (match-end 1) org-scheduled-string
  7906. (if (match-end 3) org-deadline-string "Plain:"))
  7907. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  7908. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7909. (setq n (string-to-number (match-string 2 ts))
  7910. what (match-string 3 ts))
  7911. (if (equal what "w") (setq n (* n 7) what "d"))
  7912. ;; Preparation, see if we need to modify the start date for the change
  7913. (when (match-end 1)
  7914. (setq time (save-match-data (org-time-string-to-time ts)))
  7915. (cond
  7916. ((equal (match-string 1 ts) ".")
  7917. ;; Shift starting date to today
  7918. (org-timestamp-change
  7919. (- (time-to-days (current-time)) (time-to-days time))
  7920. 'day))
  7921. ((equal (match-string 1 ts) "+")
  7922. (while (or (= nshift 0)
  7923. (<= (time-to-days time) (time-to-days (current-time))))
  7924. (when (= (incf nshift) nshiftmax)
  7925. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7926. (error "Abort")))
  7927. (org-timestamp-change n (cdr (assoc what whata)))
  7928. (org-at-timestamp-p t)
  7929. (setq ts (match-string 1))
  7930. (setq time (save-match-data (org-time-string-to-time ts))))
  7931. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7932. ;; rematch, so that we have everything in place for the real shift
  7933. (org-at-timestamp-p t)
  7934. (setq ts (match-string 1))
  7935. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7936. (org-timestamp-change n (cdr (assoc what whata)))
  7937. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7938. (setq org-log-post-message msg)
  7939. (message "%s" msg))))
  7940. (defun org-show-todo-tree (arg)
  7941. "Make a compact tree which shows all headlines marked with TODO.
  7942. The tree will show the lines where the regexp matches, and all higher
  7943. headlines above the match.
  7944. With a \\[universal-argument] prefix, also show the DONE entries.
  7945. With a numeric prefix N, construct a sparse tree for the Nth element
  7946. of `org-todo-keywords-1'."
  7947. (interactive "P")
  7948. (let ((case-fold-search nil)
  7949. (kwd-re
  7950. (cond ((null arg) org-not-done-regexp)
  7951. ((equal arg '(4))
  7952. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7953. (mapcar 'list org-todo-keywords-1))))
  7954. (concat "\\("
  7955. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7956. "\\)\\>")))
  7957. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7958. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7959. org-todo-keywords-1)))
  7960. (t (error "Invalid prefix argument: %s" arg)))))
  7961. (message "%d TODO entries found"
  7962. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7963. (defun org-deadline (&optional remove time)
  7964. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7965. With argument REMOVE, remove any deadline from the item.
  7966. When TIME is set, it should be an internal time specification, and the
  7967. scheduling will use the corresponding date."
  7968. (interactive "P")
  7969. (if remove
  7970. (progn
  7971. (org-remove-timestamp-with-keyword org-deadline-string)
  7972. (message "Item no longer has a deadline."))
  7973. (if (org-get-repeat)
  7974. (error "Cannot change deadline on task with repeater, please do that by hand")
  7975. (org-add-planning-info 'deadline time 'closed)
  7976. (message "Deadline on %s" org-last-inserted-timestamp))))
  7977. (defun org-schedule (&optional remove time)
  7978. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7979. With argument REMOVE, remove any scheduling date from the item.
  7980. When TIME is set, it should be an internal time specification, and the
  7981. scheduling will use the corresponding date."
  7982. (interactive "P")
  7983. (if remove
  7984. (progn
  7985. (org-remove-timestamp-with-keyword org-scheduled-string)
  7986. (message "Item is no longer scheduled."))
  7987. (if (org-get-repeat)
  7988. (error "Cannot reschedule task with repeater, please do that by hand")
  7989. (org-add-planning-info 'scheduled time 'closed)
  7990. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7991. (defun org-remove-timestamp-with-keyword (keyword)
  7992. "Remove all time stamps with KEYWORD in the current entry."
  7993. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7994. beg)
  7995. (save-excursion
  7996. (org-back-to-heading t)
  7997. (setq beg (point))
  7998. (org-end-of-subtree t t)
  7999. (while (re-search-backward re beg t)
  8000. (replace-match "")
  8001. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8002. (equal (char-before) ?\ ))
  8003. (backward-delete-char 1)
  8004. (if (string-match "^[ \t]*$" (buffer-substring
  8005. (point-at-bol) (point-at-eol)))
  8006. (delete-region (point-at-bol)
  8007. (min (point-max) (1+ (point-at-eol))))))))))
  8008. (defun org-add-planning-info (what &optional time &rest remove)
  8009. "Insert new timestamp with keyword in the line directly after the headline.
  8010. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8011. If non is given, the user is prompted for a date.
  8012. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8013. be removed."
  8014. (interactive)
  8015. (let (org-time-was-given org-end-time-was-given ts
  8016. end default-time default-input)
  8017. (when (and (not time) (memq what '(scheduled deadline)))
  8018. ;; Try to get a default date/time from existing timestamp
  8019. (save-excursion
  8020. (org-back-to-heading t)
  8021. (setq end (save-excursion (outline-next-heading) (point)))
  8022. (when (re-search-forward (if (eq what 'scheduled)
  8023. org-scheduled-time-regexp
  8024. org-deadline-time-regexp)
  8025. end t)
  8026. (setq ts (match-string 1)
  8027. default-time
  8028. (apply 'encode-time (org-parse-time-string ts))
  8029. default-input (and ts (org-get-compact-tod ts))))))
  8030. (when what
  8031. ;; If necessary, get the time from the user
  8032. (setq time (or time (org-read-date nil 'to-time nil nil
  8033. default-time default-input))))
  8034. (when (and org-insert-labeled-timestamps-at-point
  8035. (member what '(scheduled deadline)))
  8036. (insert
  8037. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8038. (org-insert-time-stamp time org-time-was-given
  8039. nil nil nil (list org-end-time-was-given))
  8040. (setq what nil))
  8041. (save-excursion
  8042. (save-restriction
  8043. (let (col list elt ts buffer-invisibility-spec)
  8044. (org-back-to-heading t)
  8045. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8046. (goto-char (match-end 1))
  8047. (setq col (current-column))
  8048. (goto-char (match-end 0))
  8049. (if (eobp) (insert "\n") (forward-char 1))
  8050. (if (and (not (looking-at outline-regexp))
  8051. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8052. "[^\r\n]*"))
  8053. (not (equal (match-string 1) org-clock-string)))
  8054. (narrow-to-region (match-beginning 0) (match-end 0))
  8055. (insert-before-markers "\n")
  8056. (backward-char 1)
  8057. (narrow-to-region (point) (point))
  8058. (and org-adapt-indentation (org-indent-to-column col)))
  8059. ;; Check if we have to remove something.
  8060. (setq list (cons what remove))
  8061. (while list
  8062. (setq elt (pop list))
  8063. (goto-char (point-min))
  8064. (when (or (and (eq elt 'scheduled)
  8065. (re-search-forward org-scheduled-time-regexp nil t))
  8066. (and (eq elt 'deadline)
  8067. (re-search-forward org-deadline-time-regexp nil t))
  8068. (and (eq elt 'closed)
  8069. (re-search-forward org-closed-time-regexp nil t)))
  8070. (replace-match "")
  8071. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8072. (if (looking-at " +") (replace-match ""))))
  8073. (goto-char (point-max))
  8074. (when what
  8075. (insert
  8076. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8077. (cond ((eq what 'scheduled) org-scheduled-string)
  8078. ((eq what 'deadline) org-deadline-string)
  8079. ((eq what 'closed) org-closed-string))
  8080. " ")
  8081. (setq ts (org-insert-time-stamp
  8082. time
  8083. (or org-time-was-given
  8084. (and (eq what 'closed) org-log-done-with-time))
  8085. (eq what 'closed)
  8086. nil nil (list org-end-time-was-given)))
  8087. (end-of-line 1))
  8088. (goto-char (point-min))
  8089. (widen)
  8090. (if (and (looking-at "[ \t]+\n")
  8091. (equal (char-before) ?\n))
  8092. (delete-region (1- (point)) (point-at-eol)))
  8093. ts)))))
  8094. (defvar org-log-note-marker (make-marker))
  8095. (defvar org-log-note-purpose nil)
  8096. (defvar org-log-note-state nil)
  8097. (defvar org-log-note-how nil)
  8098. (defvar org-log-note-extra nil)
  8099. (defvar org-log-note-window-configuration nil)
  8100. (defvar org-log-note-return-to (make-marker))
  8101. (defvar org-log-post-message nil
  8102. "Message to be displayed after a log note has been stored.
  8103. The auto-repeater uses this.")
  8104. (defun org-add-note ()
  8105. "Add a note to the current entry.
  8106. This is done in the same way as adding a state change note."
  8107. (interactive)
  8108. (org-add-log-setup 'note nil 'findpos nil))
  8109. (defvar org-property-end-re)
  8110. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  8111. "Set up the post command hook to take a note.
  8112. If this is about to TODO state change, the new state is expected in STATE.
  8113. When FINDPOS is non-nil, find the correct position for the note in
  8114. the current entry. If not, assume that it can be inserted at point.
  8115. HOW is an indicator what kind of note should be created.
  8116. EXTRA is additional text that will be inserted into the notes buffer."
  8117. (save-restriction
  8118. (save-excursion
  8119. (when findpos
  8120. (org-back-to-heading t)
  8121. (narrow-to-region (point) (save-excursion
  8122. (outline-next-heading) (point)))
  8123. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8124. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8125. "[^\r\n]*\\)?"))
  8126. (goto-char (match-end 0))
  8127. (when (and org-log-state-notes-insert-after-drawers
  8128. (save-excursion
  8129. (forward-line) (looking-at org-drawer-regexp)))
  8130. (progn (forward-line)
  8131. (while (looking-at org-drawer-regexp)
  8132. (goto-char (match-end 0))
  8133. (re-search-forward org-property-end-re (point-max) t)
  8134. (forward-line))
  8135. (forward-line -1)))
  8136. (unless org-log-states-order-reversed
  8137. (and (= (char-after) ?\n) (forward-char 1))
  8138. (org-skip-over-state-notes)
  8139. (skip-chars-backward " \t\n\r")))
  8140. (move-marker org-log-note-marker (point))
  8141. (setq org-log-note-purpose purpose
  8142. org-log-note-state state
  8143. org-log-note-how how
  8144. org-log-note-extra extra)
  8145. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8146. (defun org-skip-over-state-notes ()
  8147. "Skip past the list of State notes in an entry."
  8148. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8149. (while (looking-at "[ \t]*- State")
  8150. (condition-case nil
  8151. (org-next-item)
  8152. (error (org-end-of-item)))))
  8153. (defun org-add-log-note (&optional purpose)
  8154. "Pop up a window for taking a note, and add this note later at point."
  8155. (remove-hook 'post-command-hook 'org-add-log-note)
  8156. (setq org-log-note-window-configuration (current-window-configuration))
  8157. (delete-other-windows)
  8158. (move-marker org-log-note-return-to (point))
  8159. (switch-to-buffer (marker-buffer org-log-note-marker))
  8160. (goto-char org-log-note-marker)
  8161. (org-switch-to-buffer-other-window "*Org Note*")
  8162. (erase-buffer)
  8163. (if (memq org-log-note-how '(time state))
  8164. (let (current-prefix-arg) (org-store-log-note))
  8165. (let ((org-inhibit-startup t)) (org-mode))
  8166. (insert (format "# Insert note for %s.
  8167. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8168. (cond
  8169. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8170. ((eq org-log-note-purpose 'done) "closed todo item")
  8171. ((eq org-log-note-purpose 'state)
  8172. (format "state change to \"%s\"" org-log-note-state))
  8173. ((eq org-log-note-purpose 'note)
  8174. "this entry")
  8175. (t (error "This should not happen")))))
  8176. (if org-log-note-extra (insert org-log-note-extra))
  8177. (org-set-local 'org-finish-function 'org-store-log-note)))
  8178. (defvar org-note-abort nil) ; dynamically scoped
  8179. (defun org-store-log-note ()
  8180. "Finish taking a log note, and insert it to where it belongs."
  8181. (let ((txt (buffer-string))
  8182. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8183. lines ind)
  8184. (kill-buffer (current-buffer))
  8185. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8186. (setq txt (replace-match "" t t txt)))
  8187. (if (string-match "\\s-+\\'" txt)
  8188. (setq txt (replace-match "" t t txt)))
  8189. (setq lines (org-split-string txt "\n"))
  8190. (when (and note (string-match "\\S-" note))
  8191. (setq note
  8192. (org-replace-escapes
  8193. note
  8194. (list (cons "%u" (user-login-name))
  8195. (cons "%U" user-full-name)
  8196. (cons "%t" (format-time-string
  8197. (org-time-stamp-format 'long 'inactive)
  8198. (current-time)))
  8199. (cons "%s" (if org-log-note-state
  8200. (concat "\"" org-log-note-state "\"")
  8201. "")))))
  8202. (if lines (setq note (concat note " \\\\")))
  8203. (push note lines))
  8204. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8205. (when lines
  8206. (save-excursion
  8207. (set-buffer (marker-buffer org-log-note-marker))
  8208. (save-excursion
  8209. (goto-char org-log-note-marker)
  8210. (move-marker org-log-note-marker nil)
  8211. (end-of-line 1)
  8212. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8213. (indent-relative nil)
  8214. (insert "- " (pop lines))
  8215. (org-indent-line-function)
  8216. (beginning-of-line 1)
  8217. (looking-at "[ \t]*")
  8218. (setq ind (concat (match-string 0) " "))
  8219. (end-of-line 1)
  8220. (while lines (insert "\n" ind (pop lines)))))))
  8221. (set-window-configuration org-log-note-window-configuration)
  8222. (with-current-buffer (marker-buffer org-log-note-return-to)
  8223. (goto-char org-log-note-return-to))
  8224. (move-marker org-log-note-return-to nil)
  8225. (and org-log-post-message (message "%s" org-log-post-message)))
  8226. (defun org-sparse-tree (&optional arg)
  8227. "Create a sparse tree, prompt for the details.
  8228. This command can create sparse trees. You first need to select the type
  8229. of match used to create the tree:
  8230. t Show entries with a specific TODO keyword.
  8231. T Show entries selected by a tags match.
  8232. p Enter a property name and its value (both with completion on existing
  8233. names/values) and show entries with that property.
  8234. r Show entries matching a regular expression
  8235. d Show deadlines due within `org-deadline-warning-days'."
  8236. (interactive "P")
  8237. (let (ans kwd value)
  8238. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8239. (setq ans (read-char-exclusive))
  8240. (cond
  8241. ((equal ans ?d)
  8242. (call-interactively 'org-check-deadlines))
  8243. ((equal ans ?b)
  8244. (call-interactively 'org-check-before-date))
  8245. ((equal ans ?t)
  8246. (org-show-todo-tree '(4)))
  8247. ((equal ans ?T)
  8248. (call-interactively 'org-tags-sparse-tree))
  8249. ((member ans '(?p ?P))
  8250. (setq kwd (org-ido-completing-read "Property: "
  8251. (mapcar 'list (org-buffer-property-keys))))
  8252. (setq value (org-ido-completing-read "Value: "
  8253. (mapcar 'list (org-property-values kwd))))
  8254. (unless (string-match "\\`{.*}\\'" value)
  8255. (setq value (concat "\"" value "\"")))
  8256. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8257. ((member ans '(?r ?R ?/))
  8258. (call-interactively 'org-occur))
  8259. (t (error "No such sparse tree command \"%c\"" ans)))))
  8260. (defvar org-occur-highlights nil
  8261. "List of overlays used for occur matches.")
  8262. (make-variable-buffer-local 'org-occur-highlights)
  8263. (defvar org-occur-parameters nil
  8264. "Parameters of the active org-occur calls.
  8265. This is a list, each call to org-occur pushes as cons cell,
  8266. containing the regular expression and the callback, onto the list.
  8267. The list can contain several entries if `org-occur' has been called
  8268. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8269. will only contain one set of parameters. When the highlights are
  8270. removed (for example with `C-c C-c', or with the next edit (depending
  8271. on `org-remove-highlights-with-change'), this variable is emptied
  8272. as well.")
  8273. (make-variable-buffer-local 'org-occur-parameters)
  8274. (defun org-occur (regexp &optional keep-previous callback)
  8275. "Make a compact tree which shows all matches of REGEXP.
  8276. The tree will show the lines where the regexp matches, and all higher
  8277. headlines above the match. It will also show the heading after the match,
  8278. to make sure editing the matching entry is easy.
  8279. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8280. call to `org-occur' will be kept, to allow stacking of calls to this
  8281. command.
  8282. If CALLBACK is non-nil, it is a function which is called to confirm
  8283. that the match should indeed be shown."
  8284. (interactive "sRegexp: \nP")
  8285. (unless keep-previous
  8286. (org-remove-occur-highlights nil nil t))
  8287. (push (cons regexp callback) org-occur-parameters)
  8288. (let ((cnt 0))
  8289. (save-excursion
  8290. (goto-char (point-min))
  8291. (if (or (not keep-previous) ; do not want to keep
  8292. (not org-occur-highlights)) ; no previous matches
  8293. ;; hide everything
  8294. (org-overview))
  8295. (while (re-search-forward regexp nil t)
  8296. (when (or (not callback)
  8297. (save-match-data (funcall callback)))
  8298. (setq cnt (1+ cnt))
  8299. (when org-highlight-sparse-tree-matches
  8300. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8301. (org-show-context 'occur-tree))))
  8302. (when org-remove-highlights-with-change
  8303. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8304. nil 'local))
  8305. (unless org-sparse-tree-open-archived-trees
  8306. (org-hide-archived-subtrees (point-min) (point-max)))
  8307. (run-hooks 'org-occur-hook)
  8308. (if (interactive-p)
  8309. (message "%d match(es) for regexp %s" cnt regexp))
  8310. cnt))
  8311. (defun org-show-context (&optional key)
  8312. "Make sure point and context and visible.
  8313. How much context is shown depends upon the variables
  8314. `org-show-hierarchy-above', `org-show-following-heading'. and
  8315. `org-show-siblings'."
  8316. (let ((heading-p (org-on-heading-p t))
  8317. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8318. (following-p (org-get-alist-option org-show-following-heading key))
  8319. (entry-p (org-get-alist-option org-show-entry-below key))
  8320. (siblings-p (org-get-alist-option org-show-siblings key)))
  8321. (catch 'exit
  8322. ;; Show heading or entry text
  8323. (if (and heading-p (not entry-p))
  8324. (org-flag-heading nil) ; only show the heading
  8325. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8326. (org-show-hidden-entry))) ; show entire entry
  8327. (when following-p
  8328. ;; Show next sibling, or heading below text
  8329. (save-excursion
  8330. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8331. (org-flag-heading nil))))
  8332. (when siblings-p (org-show-siblings))
  8333. (when hierarchy-p
  8334. ;; show all higher headings, possibly with siblings
  8335. (save-excursion
  8336. (while (and (condition-case nil
  8337. (progn (org-up-heading-all 1) t)
  8338. (error nil))
  8339. (not (bobp)))
  8340. (org-flag-heading nil)
  8341. (when siblings-p (org-show-siblings))))))))
  8342. (defun org-reveal (&optional siblings)
  8343. "Show current entry, hierarchy above it, and the following headline.
  8344. This can be used to show a consistent set of context around locations
  8345. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8346. not t for the search context.
  8347. With optional argument SIBLINGS, on each level of the hierarchy all
  8348. siblings are shown. This repairs the tree structure to what it would
  8349. look like when opened with hierarchical calls to `org-cycle'."
  8350. (interactive "P")
  8351. (let ((org-show-hierarchy-above t)
  8352. (org-show-following-heading t)
  8353. (org-show-siblings (if siblings t org-show-siblings)))
  8354. (org-show-context nil)))
  8355. (defun org-highlight-new-match (beg end)
  8356. "Highlight from BEG to END and mark the highlight is an occur headline."
  8357. (let ((ov (org-make-overlay beg end)))
  8358. (org-overlay-put ov 'face 'secondary-selection)
  8359. (push ov org-occur-highlights)))
  8360. (defun org-remove-occur-highlights (&optional beg end noremove)
  8361. "Remove the occur highlights from the buffer.
  8362. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8363. from the `before-change-functions' in the current buffer."
  8364. (interactive)
  8365. (unless org-inhibit-highlight-removal
  8366. (mapc 'org-delete-overlay org-occur-highlights)
  8367. (setq org-occur-highlights nil)
  8368. (setq org-occur-parameters nil)
  8369. (unless noremove
  8370. (remove-hook 'before-change-functions
  8371. 'org-remove-occur-highlights 'local))))
  8372. ;;;; Priorities
  8373. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8374. "Regular expression matching the priority indicator.")
  8375. (defvar org-remove-priority-next-time nil)
  8376. (defun org-priority-up ()
  8377. "Increase the priority of the current item."
  8378. (interactive)
  8379. (org-priority 'up))
  8380. (defun org-priority-down ()
  8381. "Decrease the priority of the current item."
  8382. (interactive)
  8383. (org-priority 'down))
  8384. (defun org-priority (&optional action)
  8385. "Change the priority of an item by ARG.
  8386. ACTION can be `set', `up', `down', or a character."
  8387. (interactive)
  8388. (setq action (or action 'set))
  8389. (let (current new news have remove)
  8390. (save-excursion
  8391. (org-back-to-heading)
  8392. (if (looking-at org-priority-regexp)
  8393. (setq current (string-to-char (match-string 2))
  8394. have t)
  8395. (setq current org-default-priority))
  8396. (cond
  8397. ((or (eq action 'set)
  8398. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8399. (if (not (eq action 'set))
  8400. (setq new action)
  8401. (message "Priority %c-%c, SPC to remove: "
  8402. org-highest-priority org-lowest-priority)
  8403. (setq new (read-char-exclusive)))
  8404. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8405. (= (upcase org-lowest-priority) org-lowest-priority))
  8406. (setq new (upcase new)))
  8407. (cond ((equal new ?\ ) (setq remove t))
  8408. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8409. (error "Priority must be between `%c' and `%c'"
  8410. org-highest-priority org-lowest-priority))))
  8411. ((eq action 'up)
  8412. (if (and (not have) (eq last-command this-command))
  8413. (setq new org-lowest-priority)
  8414. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8415. org-default-priority (1- current)))))
  8416. ((eq action 'down)
  8417. (if (and (not have) (eq last-command this-command))
  8418. (setq new org-highest-priority)
  8419. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8420. org-default-priority (1+ current)))))
  8421. (t (error "Invalid action")))
  8422. (if (or (< (upcase new) org-highest-priority)
  8423. (> (upcase new) org-lowest-priority))
  8424. (setq remove t))
  8425. (setq news (format "%c" new))
  8426. (if have
  8427. (if remove
  8428. (replace-match "" t t nil 1)
  8429. (replace-match news t t nil 2))
  8430. (if remove
  8431. (error "No priority cookie found in line")
  8432. (looking-at org-todo-line-regexp)
  8433. (if (match-end 2)
  8434. (progn
  8435. (goto-char (match-end 2))
  8436. (insert " [#" news "]"))
  8437. (goto-char (match-beginning 3))
  8438. (insert "[#" news "] ")))))
  8439. (org-preserve-lc (org-set-tags nil 'align))
  8440. (if remove
  8441. (message "Priority removed")
  8442. (message "Priority of current item set to %s" news))))
  8443. (defun org-get-priority (s)
  8444. "Find priority cookie and return priority."
  8445. (save-match-data
  8446. (if (not (string-match org-priority-regexp s))
  8447. (* 1000 (- org-lowest-priority org-default-priority))
  8448. (* 1000 (- org-lowest-priority
  8449. (string-to-char (match-string 2 s)))))))
  8450. ;;;; Tags
  8451. (defvar org-agenda-archives-mode)
  8452. (defun org-scan-tags (action matcher &optional todo-only)
  8453. "Scan headline tags with inheritance and produce output ACTION.
  8454. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8455. or `agenda' to produce an entry list for an agenda view. It can also be
  8456. a Lisp form or a function that should be called at each matched headline, in
  8457. this case the return value is a list of all return values from these calls.
  8458. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8459. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8460. only lines with a TODO keyword are included in the output."
  8461. (require 'org-agenda)
  8462. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8463. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8464. (org-re
  8465. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8466. (props (list 'face 'default
  8467. 'done-face 'org-done
  8468. 'undone-face 'default
  8469. 'mouse-face 'highlight
  8470. 'org-not-done-regexp org-not-done-regexp
  8471. 'org-todo-regexp org-todo-regexp
  8472. 'keymap org-agenda-keymap
  8473. 'help-echo
  8474. (format "mouse-2 or RET jump to org file %s"
  8475. (abbreviate-file-name
  8476. (or (buffer-file-name (buffer-base-buffer))
  8477. (buffer-name (buffer-base-buffer)))))))
  8478. (case-fold-search nil)
  8479. lspos tags tags-list
  8480. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8481. (llast 0) rtn rtn1 level category i txt
  8482. todo marker entry priority)
  8483. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8484. (setq action (list 'lambda nil action)))
  8485. (save-excursion
  8486. (goto-char (point-min))
  8487. (when (eq action 'sparse-tree)
  8488. (org-overview)
  8489. (org-remove-occur-highlights))
  8490. (while (re-search-forward re nil t)
  8491. (catch :skip
  8492. (setq todo (if (match-end 1) (match-string 2))
  8493. tags (if (match-end 4) (match-string 4)))
  8494. (goto-char (setq lspos (1+ (match-beginning 0))))
  8495. (setq level (org-reduced-level (funcall outline-level))
  8496. category (org-get-category))
  8497. (setq i llast llast level)
  8498. ;; remove tag lists from same and sublevels
  8499. (while (>= i level)
  8500. (when (setq entry (assoc i tags-alist))
  8501. (setq tags-alist (delete entry tags-alist)))
  8502. (setq i (1- i)))
  8503. ;; add the next tags
  8504. (when tags
  8505. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8506. tags-alist
  8507. (cons (cons level tags) tags-alist)))
  8508. ;; compile tags for current headline
  8509. (setq tags-list
  8510. (if org-use-tag-inheritance
  8511. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8512. tags))
  8513. (when org-use-tag-inheritance
  8514. (setcdr (car tags-alist)
  8515. (mapcar (lambda (x)
  8516. (setq x (copy-sequence x))
  8517. (org-add-prop-inherited x))
  8518. (cdar tags-alist))))
  8519. (when (and tags org-use-tag-inheritance
  8520. (not (eq t org-use-tag-inheritance)))
  8521. ;; selective inheritance, remove uninherited ones
  8522. (setcdr (car tags-alist)
  8523. (org-remove-uniherited-tags (cdar tags-alist))))
  8524. (when (and (or (not todo-only)
  8525. (and (member todo org-not-done-keywords)
  8526. (or (not org-agenda-tags-todo-honor-ignore-options)
  8527. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8528. (let ((case-fold-search t)) (eval matcher))
  8529. (or
  8530. (not (member org-archive-tag tags-list))
  8531. ;; we have an archive tag, should we use this anyway?
  8532. (or (not org-agenda-skip-archived-trees)
  8533. (and (eq action 'agenda) org-agenda-archives-mode))))
  8534. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8535. ;; select this headline
  8536. (cond
  8537. ((eq action 'sparse-tree)
  8538. (and org-highlight-sparse-tree-matches
  8539. (org-get-heading) (match-end 0)
  8540. (org-highlight-new-match
  8541. (match-beginning 0) (match-beginning 1)))
  8542. (org-show-context 'tags-tree))
  8543. ((eq action 'agenda)
  8544. (setq txt (org-format-agenda-item
  8545. ""
  8546. (concat
  8547. (if org-tags-match-list-sublevels
  8548. (make-string (1- level) ?.) "")
  8549. (org-get-heading))
  8550. category (org-get-tags-at))
  8551. priority (org-get-priority txt))
  8552. (goto-char lspos)
  8553. (setq marker (org-agenda-new-marker))
  8554. (org-add-props txt props
  8555. 'org-marker marker 'org-hd-marker marker 'org-category category
  8556. 'priority priority 'type "tagsmatch")
  8557. (push txt rtn))
  8558. ((functionp action)
  8559. (save-excursion
  8560. (setq rtn1 (funcall action))
  8561. (push rtn1 rtn))
  8562. (goto-char (point-at-eol)))
  8563. (t (error "Invalid action")))
  8564. ;; if we are to skip sublevels, jump to end of subtree
  8565. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8566. (when (and (eq action 'sparse-tree)
  8567. (not org-sparse-tree-open-archived-trees))
  8568. (org-hide-archived-subtrees (point-min) (point-max)))
  8569. (nreverse rtn)))
  8570. (defun org-remove-uniherited-tags (tags)
  8571. "Remove all tags that are not inherited from the list TAGS."
  8572. (cond
  8573. ((eq org-use-tag-inheritance t)
  8574. (if org-tags-exclude-from-inheritance
  8575. (org-delete-all org-tags-exclude-from-inheritance tags)
  8576. tags))
  8577. ((not org-use-tag-inheritance) nil)
  8578. ((stringp org-use-tag-inheritance)
  8579. (delq nil (mapcar
  8580. (lambda (x)
  8581. (if (and (string-match org-use-tag-inheritance x)
  8582. (not (member x org-tags-exclude-from-inheritance)))
  8583. x nil))
  8584. tags)))
  8585. ((listp org-use-tag-inheritance)
  8586. (delq nil (mapcar
  8587. (lambda (x)
  8588. (if (member x org-use-tag-inheritance) x nil))
  8589. tags)))))
  8590. (defvar todo-only) ;; dynamically scoped
  8591. (defun org-tags-sparse-tree (&optional todo-only match)
  8592. "Create a sparse tree according to tags string MATCH.
  8593. MATCH can contain positive and negative selection of tags, like
  8594. \"+WORK+URGENT-WITHBOSS\".
  8595. If optional argument TODO-ONLY is non-nil, only select lines that are
  8596. also TODO lines."
  8597. (interactive "P")
  8598. (org-prepare-agenda-buffers (list (current-buffer)))
  8599. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8600. (defvar org-cached-props nil)
  8601. (defun org-cached-entry-get (pom property)
  8602. (if (or (eq t org-use-property-inheritance)
  8603. (and (stringp org-use-property-inheritance)
  8604. (string-match org-use-property-inheritance property))
  8605. (and (listp org-use-property-inheritance)
  8606. (member property org-use-property-inheritance)))
  8607. ;; Caching is not possible, check it directly
  8608. (org-entry-get pom property 'inherit)
  8609. ;; Get all properties, so that we can do complicated checks easily
  8610. (cdr (assoc property (or org-cached-props
  8611. (setq org-cached-props
  8612. (org-entry-properties pom)))))))
  8613. (defun org-global-tags-completion-table (&optional files)
  8614. "Return the list of all tags in all agenda buffer/files."
  8615. (save-excursion
  8616. (org-uniquify
  8617. (delq nil
  8618. (apply 'append
  8619. (mapcar
  8620. (lambda (file)
  8621. (set-buffer (find-file-noselect file))
  8622. (append (org-get-buffer-tags)
  8623. (mapcar (lambda (x) (if (stringp (car-safe x))
  8624. (list (car-safe x)) nil))
  8625. org-tag-alist)))
  8626. (if (and files (car files))
  8627. files
  8628. (org-agenda-files))))))))
  8629. (defun org-make-tags-matcher (match)
  8630. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8631. ;; todo-only is scoped dynamically into this function, and the function
  8632. ;; may change it if the matcher asks for it.
  8633. (unless match
  8634. ;; Get a new match request, with completion
  8635. (let ((org-last-tags-completion-table
  8636. (org-global-tags-completion-table)))
  8637. (setq match (org-completing-read
  8638. "Match: " 'org-tags-completion-function nil nil nil
  8639. 'org-tags-history))))
  8640. ;; Parse the string and create a lisp form
  8641. (let ((match0 match)
  8642. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8643. minus tag mm
  8644. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8645. orterms term orlist re-p str-p level-p level-op time-p
  8646. prop-p pn pv po cat-p gv rest)
  8647. (if (string-match "/+" match)
  8648. ;; match contains also a todo-matching request
  8649. (progn
  8650. (setq tagsmatch (substring match 0 (match-beginning 0))
  8651. todomatch (substring match (match-end 0)))
  8652. (if (string-match "^!" todomatch)
  8653. (setq todo-only t todomatch (substring todomatch 1)))
  8654. (if (string-match "^\\s-*$" todomatch)
  8655. (setq todomatch nil)))
  8656. ;; only matching tags
  8657. (setq tagsmatch match todomatch nil))
  8658. ;; Make the tags matcher
  8659. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8660. (setq tagsmatcher t)
  8661. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8662. (while (setq term (pop orterms))
  8663. (while (and (equal (substring term -1) "\\") orterms)
  8664. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8665. (while (string-match re term)
  8666. (setq rest (substring term (match-end 0))
  8667. minus (and (match-end 1)
  8668. (equal (match-string 1 term) "-"))
  8669. tag (match-string 2 term)
  8670. re-p (equal (string-to-char tag) ?{)
  8671. level-p (match-end 4)
  8672. prop-p (match-end 5)
  8673. mm (cond
  8674. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8675. (level-p
  8676. (setq level-op (org-op-to-function (match-string 3 term)))
  8677. `(,level-op level ,(string-to-number
  8678. (match-string 4 term))))
  8679. (prop-p
  8680. (setq pn (match-string 5 term)
  8681. po (match-string 6 term)
  8682. pv (match-string 7 term)
  8683. cat-p (equal pn "CATEGORY")
  8684. re-p (equal (string-to-char pv) ?{)
  8685. str-p (equal (string-to-char pv) ?\")
  8686. time-p (save-match-data
  8687. (string-match "^\"[[<].*[]>]\"$" pv))
  8688. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8689. (if time-p (setq pv (org-matcher-time pv)))
  8690. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8691. (cond
  8692. ((equal pn "CATEGORY")
  8693. (setq gv '(get-text-property (point) 'org-category)))
  8694. ((equal pn "TODO")
  8695. (setq gv 'todo))
  8696. (t
  8697. (setq gv `(org-cached-entry-get nil ,pn))))
  8698. (if re-p
  8699. (if (eq po 'org<>)
  8700. `(not (string-match ,pv (or ,gv "")))
  8701. `(string-match ,pv (or ,gv "")))
  8702. (if str-p
  8703. `(,po (or ,gv "") ,pv)
  8704. `(,po (string-to-number (or ,gv ""))
  8705. ,(string-to-number pv) ))))
  8706. (t `(member ,(downcase tag) tags-list)))
  8707. mm (if minus (list 'not mm) mm)
  8708. term rest)
  8709. (push mm tagsmatcher))
  8710. (push (if (> (length tagsmatcher) 1)
  8711. (cons 'and tagsmatcher)
  8712. (car tagsmatcher))
  8713. orlist)
  8714. (setq tagsmatcher nil))
  8715. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8716. (setq tagsmatcher
  8717. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8718. ;; Make the todo matcher
  8719. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8720. (setq todomatcher t)
  8721. (setq orterms (org-split-string todomatch "|") orlist nil)
  8722. (while (setq term (pop orterms))
  8723. (while (string-match re term)
  8724. (setq minus (and (match-end 1)
  8725. (equal (match-string 1 term) "-"))
  8726. kwd (match-string 2 term)
  8727. re-p (equal (string-to-char kwd) ?{)
  8728. term (substring term (match-end 0))
  8729. mm (if re-p
  8730. `(string-match ,(substring kwd 1 -1) todo)
  8731. (list 'equal 'todo kwd))
  8732. mm (if minus (list 'not mm) mm))
  8733. (push mm todomatcher))
  8734. (push (if (> (length todomatcher) 1)
  8735. (cons 'and todomatcher)
  8736. (car todomatcher))
  8737. orlist)
  8738. (setq todomatcher nil))
  8739. (setq todomatcher (if (> (length orlist) 1)
  8740. (cons 'or orlist) (car orlist))))
  8741. ;; Return the string and lisp forms of the matcher
  8742. (setq matcher (if todomatcher
  8743. (list 'and tagsmatcher todomatcher)
  8744. tagsmatcher))
  8745. (cons match0 matcher)))
  8746. (defun org-op-to-function (op &optional stringp)
  8747. "Turn an operator into the appropriate function."
  8748. (setq op
  8749. (cond
  8750. ((equal op "<" ) '(< string< org-time<))
  8751. ((equal op ">" ) '(> org-string> org-time>))
  8752. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8753. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8754. ((member op '("=" "==")) '(= string= org-time=))
  8755. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8756. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8757. (defun org<> (a b) (not (= a b)))
  8758. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8759. (defun org-string>= (a b) (not (string< a b)))
  8760. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8761. (defun org-string<> (a b) (not (string= a b)))
  8762. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  8763. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  8764. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  8765. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> 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) (org<> a b)))
  8768. (defun org-2ft (s)
  8769. "Convert S to a floating point time.
  8770. If S is already a number, just return it. If it is a string, parse
  8771. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  8772. (cond
  8773. ((numberp s) s)
  8774. ((stringp s)
  8775. (condition-case nil
  8776. (float-time (apply 'encode-time (org-parse-time-string s)))
  8777. (error 0.)))
  8778. (t 0.)))
  8779. (defun org-time-today ()
  8780. "Time in seconds today at 0:00.
  8781. Returns the float number of seconds since the beginning of the
  8782. epoch to the beginning of today (00:00)."
  8783. (float-time (apply 'encode-time
  8784. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8785. (defun org-matcher-time (s)
  8786. "Interpret a time comparison value."
  8787. (save-match-data
  8788. (cond
  8789. ((string= s "<now>") (float-time))
  8790. ((string= s "<today>") (org-time-today))
  8791. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8792. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8793. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8794. (+ (org-time-today)
  8795. (* (string-to-number (match-string 1 s))
  8796. (cdr (assoc (match-string 2 s)
  8797. '(("d" . 86400.0) ("w" . 604800.0)
  8798. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8799. (t (org-2ft s)))))
  8800. (defun org-match-any-p (re list)
  8801. "Does re match any element of list?"
  8802. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8803. (delq nil list))
  8804. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8805. (defvar org-tags-overlay (org-make-overlay 1 1))
  8806. (org-detach-overlay org-tags-overlay)
  8807. (defun org-get-local-tags-at (&optional pos)
  8808. "Get a list of tags defined in the current headline."
  8809. (org-get-tags-at pos 'local))
  8810. (defun org-get-local-tags ()
  8811. "Get a list of tags defined in the current headline."
  8812. (org-get-tags-at nil 'local))
  8813. (defun org-get-tags-at (&optional pos local)
  8814. "Get a list of all headline tags applicable at POS.
  8815. POS defaults to point. If tags are inherited, the list contains
  8816. the targets in the same sequence as the headlines appear, i.e.
  8817. the tags of the current headline come last.
  8818. When LOCAL is non-nil, only return tags from the current headline,
  8819. ignore inherited ones."
  8820. (interactive)
  8821. (let (tags ltags lastpos parent)
  8822. (save-excursion
  8823. (save-restriction
  8824. (widen)
  8825. (goto-char (or pos (point)))
  8826. (save-match-data
  8827. (catch 'done
  8828. (condition-case nil
  8829. (progn
  8830. (org-back-to-heading t)
  8831. (while (not (equal lastpos (point)))
  8832. (setq lastpos (point))
  8833. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8834. (setq ltags (org-split-string
  8835. (org-match-string-no-properties 1) ":"))
  8836. (when parent
  8837. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8838. (setq tags (append
  8839. (if parent
  8840. (org-remove-uniherited-tags ltags)
  8841. ltags)
  8842. tags)))
  8843. (or org-use-tag-inheritance (throw 'done t))
  8844. (if local (throw 'done t))
  8845. (org-up-heading-all 1)
  8846. (setq parent t)))
  8847. (error nil)))))
  8848. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8849. (defun org-add-prop-inherited (s)
  8850. (add-text-properties 0 (length s) '(inherited t) s)
  8851. s)
  8852. (defun org-toggle-tag (tag &optional onoff)
  8853. "Toggle the tag TAG for the current line.
  8854. If ONOFF is `on' or `off', don't toggle but set to this state."
  8855. (let (res current)
  8856. (save-excursion
  8857. (org-back-to-heading t)
  8858. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8859. (point-at-eol) t)
  8860. (progn
  8861. (setq current (match-string 1))
  8862. (replace-match ""))
  8863. (setq current ""))
  8864. (setq current (nreverse (org-split-string current ":")))
  8865. (cond
  8866. ((eq onoff 'on)
  8867. (setq res t)
  8868. (or (member tag current) (push tag current)))
  8869. ((eq onoff 'off)
  8870. (or (not (member tag current)) (setq current (delete tag current))))
  8871. (t (if (member tag current)
  8872. (setq current (delete tag current))
  8873. (setq res t)
  8874. (push tag current))))
  8875. (end-of-line 1)
  8876. (if current
  8877. (progn
  8878. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8879. (org-set-tags nil t))
  8880. (delete-horizontal-space))
  8881. (run-hooks 'org-after-tags-change-hook))
  8882. res))
  8883. (defun org-align-tags-here (to-col)
  8884. ;; Assumes that this is a headline
  8885. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8886. (beginning-of-line 1)
  8887. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8888. (< pos (match-beginning 2)))
  8889. (progn
  8890. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8891. (goto-char (match-beginning 1))
  8892. (insert " ")
  8893. (delete-region (point) (1+ (match-beginning 2)))
  8894. (setq ncol (max (1+ (current-column))
  8895. (1+ col)
  8896. (if (> to-col 0)
  8897. to-col
  8898. (- (abs to-col) tags-l))))
  8899. (setq p (point))
  8900. (insert (make-string (- ncol (current-column)) ?\ ))
  8901. (setq ncol (current-column))
  8902. (when indent-tabs-mode (tabify p (point-at-eol)))
  8903. (org-move-to-column (min ncol col) t))
  8904. (goto-char pos))))
  8905. (defun org-set-tags-command (&optional arg just-align)
  8906. "Call the set-tags command for the current entry."
  8907. (interactive "P")
  8908. (if (org-on-heading-p)
  8909. (org-set-tags arg just-align)
  8910. (save-excursion
  8911. (org-back-to-heading t)
  8912. (org-set-tags arg just-align))))
  8913. (defun org-set-tags (&optional arg just-align)
  8914. "Set the tags for the current headline.
  8915. With prefix ARG, realign all tags in headings in the current buffer."
  8916. (interactive "P")
  8917. (let* ((re (concat "^" outline-regexp))
  8918. (current (org-get-tags-string))
  8919. (col (current-column))
  8920. (org-setting-tags t)
  8921. table current-tags inherited-tags ; computed below when needed
  8922. tags p0 c0 c1 rpl)
  8923. (if arg
  8924. (save-excursion
  8925. (goto-char (point-min))
  8926. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8927. (while (re-search-forward re nil t)
  8928. (org-set-tags nil t)
  8929. (end-of-line 1)))
  8930. (message "All tags realigned to column %d" org-tags-column))
  8931. (if just-align
  8932. (setq tags current)
  8933. ;; Get a new set of tags from the user
  8934. (save-excursion
  8935. (setq table (or org-tag-alist (org-get-buffer-tags))
  8936. org-last-tags-completion-table table
  8937. current-tags (org-split-string current ":")
  8938. inherited-tags (nreverse
  8939. (nthcdr (length current-tags)
  8940. (nreverse (org-get-tags-at))))
  8941. tags
  8942. (if (or (eq t org-use-fast-tag-selection)
  8943. (and org-use-fast-tag-selection
  8944. (delq nil (mapcar 'cdr table))))
  8945. (org-fast-tag-selection
  8946. current-tags inherited-tags table
  8947. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8948. (let ((org-add-colon-after-tag-completion t))
  8949. (org-trim
  8950. (org-without-partial-completion
  8951. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8952. nil nil current 'org-tags-history)))))))
  8953. (while (string-match "[-+&]+" tags)
  8954. ;; No boolean logic, just a list
  8955. (setq tags (replace-match ":" t t tags))))
  8956. (if (string-match "\\`[\t ]*\\'" tags)
  8957. (setq tags "")
  8958. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8959. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8960. ;; Insert new tags at the correct column
  8961. (beginning-of-line 1)
  8962. (cond
  8963. ((and (equal current "") (equal tags "")))
  8964. ((re-search-forward
  8965. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8966. (point-at-eol) t)
  8967. (if (equal tags "")
  8968. (setq rpl "")
  8969. (goto-char (match-beginning 0))
  8970. (setq c0 (current-column) p0 (point)
  8971. c1 (max (1+ c0) (if (> org-tags-column 0)
  8972. org-tags-column
  8973. (- (- org-tags-column) (length tags))))
  8974. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8975. (replace-match rpl t t)
  8976. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8977. tags)
  8978. (t (error "Tags alignment failed")))
  8979. (org-move-to-column col)
  8980. (unless just-align
  8981. (run-hooks 'org-after-tags-change-hook)))))
  8982. (defun org-change-tag-in-region (beg end tag off)
  8983. "Add or remove TAG for each entry in the region.
  8984. This works in the agenda, and also in an org-mode buffer."
  8985. (interactive
  8986. (list (region-beginning) (region-end)
  8987. (let ((org-last-tags-completion-table
  8988. (if (org-mode-p)
  8989. (org-get-buffer-tags)
  8990. (org-global-tags-completion-table))))
  8991. (org-ido-completing-read
  8992. "Tag: " 'org-tags-completion-function nil nil nil
  8993. 'org-tags-history))
  8994. (progn
  8995. (message "[s]et or [r]emove? ")
  8996. (equal (read-char-exclusive) ?r))))
  8997. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8998. (let ((agendap (equal major-mode 'org-agenda-mode))
  8999. l1 l2 m buf pos newhead (cnt 0))
  9000. (goto-char end)
  9001. (setq l2 (1- (org-current-line)))
  9002. (goto-char beg)
  9003. (setq l1 (org-current-line))
  9004. (loop for l from l1 to l2 do
  9005. (goto-line l)
  9006. (setq m (get-text-property (point) 'org-hd-marker))
  9007. (when (or (and (org-mode-p) (org-on-heading-p))
  9008. (and agendap m))
  9009. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9010. pos (if agendap m (point)))
  9011. (with-current-buffer buf
  9012. (save-excursion
  9013. (save-restriction
  9014. (goto-char pos)
  9015. (setq cnt (1+ cnt))
  9016. (org-toggle-tag tag (if off 'off 'on))
  9017. (setq newhead (org-get-heading)))))
  9018. (and agendap (org-agenda-change-all-lines newhead m))))
  9019. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9020. (defun org-tags-completion-function (string predicate &optional flag)
  9021. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9022. (confirm (lambda (x) (stringp (car x)))))
  9023. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9024. (setq s1 (match-string 1 string)
  9025. s2 (match-string 2 string))
  9026. (setq s1 "" s2 string))
  9027. (cond
  9028. ((eq flag nil)
  9029. ;; try completion
  9030. (setq rtn (try-completion s2 ctable confirm))
  9031. (if (stringp rtn)
  9032. (setq rtn
  9033. (concat s1 s2 (substring rtn (length s2))
  9034. (if (and org-add-colon-after-tag-completion
  9035. (assoc rtn ctable))
  9036. ":" ""))))
  9037. rtn)
  9038. ((eq flag t)
  9039. ;; all-completions
  9040. (all-completions s2 ctable confirm)
  9041. )
  9042. ((eq flag 'lambda)
  9043. ;; exact match?
  9044. (assoc s2 ctable)))
  9045. ))
  9046. (defun org-fast-tag-insert (kwd tags face &optional end)
  9047. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9048. (insert (format "%-12s" (concat kwd ":"))
  9049. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9050. (or end "")))
  9051. (defun org-fast-tag-show-exit (flag)
  9052. (save-excursion
  9053. (goto-line 3)
  9054. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9055. (replace-match ""))
  9056. (when flag
  9057. (end-of-line 1)
  9058. (org-move-to-column (- (window-width) 19) t)
  9059. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9060. (defun org-set-current-tags-overlay (current prefix)
  9061. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9062. (if (featurep 'xemacs)
  9063. (org-overlay-display org-tags-overlay (concat prefix s)
  9064. 'secondary-selection)
  9065. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9066. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9067. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9068. "Fast tag selection with single keys.
  9069. CURRENT is the current list of tags in the headline, INHERITED is the
  9070. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9071. possibly with grouping information. TODO-TABLE is a similar table with
  9072. TODO keywords, should these have keys assigned to them.
  9073. If the keys are nil, a-z are automatically assigned.
  9074. Returns the new tags string, or nil to not change the current settings."
  9075. (let* ((fulltable (append table todo-table))
  9076. (maxlen (apply 'max (mapcar
  9077. (lambda (x)
  9078. (if (stringp (car x)) (string-width (car x)) 0))
  9079. fulltable)))
  9080. (buf (current-buffer))
  9081. (expert (eq org-fast-tag-selection-single-key 'expert))
  9082. (buffer-tags nil)
  9083. (fwidth (+ maxlen 3 1 3))
  9084. (ncol (/ (- (window-width) 4) fwidth))
  9085. (i-face 'org-done)
  9086. (c-face 'org-todo)
  9087. tg cnt e c char c1 c2 ntable tbl rtn
  9088. ov-start ov-end ov-prefix
  9089. (exit-after-next org-fast-tag-selection-single-key)
  9090. (done-keywords org-done-keywords)
  9091. groups ingroup)
  9092. (save-excursion
  9093. (beginning-of-line 1)
  9094. (if (looking-at
  9095. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9096. (setq ov-start (match-beginning 1)
  9097. ov-end (match-end 1)
  9098. ov-prefix "")
  9099. (setq ov-start (1- (point-at-eol))
  9100. ov-end (1+ ov-start))
  9101. (skip-chars-forward "^\n\r")
  9102. (setq ov-prefix
  9103. (concat
  9104. (buffer-substring (1- (point)) (point))
  9105. (if (> (current-column) org-tags-column)
  9106. " "
  9107. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9108. (org-move-overlay org-tags-overlay ov-start ov-end)
  9109. (save-window-excursion
  9110. (if expert
  9111. (set-buffer (get-buffer-create " *Org tags*"))
  9112. (delete-other-windows)
  9113. (split-window-vertically)
  9114. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9115. (erase-buffer)
  9116. (org-set-local 'org-done-keywords done-keywords)
  9117. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9118. (org-fast-tag-insert "Current" current c-face "\n\n")
  9119. (org-fast-tag-show-exit exit-after-next)
  9120. (org-set-current-tags-overlay current ov-prefix)
  9121. (setq tbl fulltable char ?a cnt 0)
  9122. (while (setq e (pop tbl))
  9123. (cond
  9124. ((equal e '(:startgroup))
  9125. (push '() groups) (setq ingroup t)
  9126. (when (not (= cnt 0))
  9127. (setq cnt 0)
  9128. (insert "\n"))
  9129. (insert "{ "))
  9130. ((equal e '(:endgroup))
  9131. (setq ingroup nil cnt 0)
  9132. (insert "}\n"))
  9133. (t
  9134. (setq tg (car e) c2 nil)
  9135. (if (cdr e)
  9136. (setq c (cdr e))
  9137. ;; automatically assign a character.
  9138. (setq c1 (string-to-char
  9139. (downcase (substring
  9140. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9141. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9142. (while (or (rassoc char ntable) (rassoc char table))
  9143. (setq char (1+ char)))
  9144. (setq c2 c1))
  9145. (setq c (or c2 char)))
  9146. (if ingroup (push tg (car groups)))
  9147. (setq tg (org-add-props tg nil 'face
  9148. (cond
  9149. ((not (assoc tg table))
  9150. (org-get-todo-face tg))
  9151. ((member tg current) c-face)
  9152. ((member tg inherited) i-face)
  9153. (t nil))))
  9154. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9155. (insert "[" c "] " tg (make-string
  9156. (- fwidth 4 (length tg)) ?\ ))
  9157. (push (cons tg c) ntable)
  9158. (when (= (setq cnt (1+ cnt)) ncol)
  9159. (insert "\n")
  9160. (if ingroup (insert " "))
  9161. (setq cnt 0)))))
  9162. (setq ntable (nreverse ntable))
  9163. (insert "\n")
  9164. (goto-char (point-min))
  9165. (if (not expert) (org-fit-window-to-buffer))
  9166. (setq rtn
  9167. (catch 'exit
  9168. (while t
  9169. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9170. (if groups " [!] no groups" " [!]groups")
  9171. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9172. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9173. (cond
  9174. ((= c ?\r) (throw 'exit t))
  9175. ((= c ?!)
  9176. (setq groups (not groups))
  9177. (goto-char (point-min))
  9178. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9179. ((= c ?\C-c)
  9180. (if (not expert)
  9181. (org-fast-tag-show-exit
  9182. (setq exit-after-next (not exit-after-next)))
  9183. (setq expert nil)
  9184. (delete-other-windows)
  9185. (split-window-vertically)
  9186. (org-switch-to-buffer-other-window " *Org tags*")
  9187. (org-fit-window-to-buffer)))
  9188. ((or (= c ?\C-g)
  9189. (and (= c ?q) (not (rassoc c ntable))))
  9190. (org-detach-overlay org-tags-overlay)
  9191. (setq quit-flag t))
  9192. ((= c ?\ )
  9193. (setq current nil)
  9194. (if exit-after-next (setq exit-after-next 'now)))
  9195. ((= c ?\t)
  9196. (condition-case nil
  9197. (setq tg (org-ido-completing-read
  9198. "Tag: "
  9199. (or buffer-tags
  9200. (with-current-buffer buf
  9201. (org-get-buffer-tags)))))
  9202. (quit (setq tg "")))
  9203. (when (string-match "\\S-" tg)
  9204. (add-to-list 'buffer-tags (list tg))
  9205. (if (member tg current)
  9206. (setq current (delete tg current))
  9207. (push tg current)))
  9208. (if exit-after-next (setq exit-after-next 'now)))
  9209. ((setq e (rassoc c todo-table) tg (car e))
  9210. (with-current-buffer buf
  9211. (save-excursion (org-todo tg)))
  9212. (if exit-after-next (setq exit-after-next 'now)))
  9213. ((setq e (rassoc c ntable) tg (car e))
  9214. (if (member tg current)
  9215. (setq current (delete tg current))
  9216. (loop for g in groups do
  9217. (if (member tg g)
  9218. (mapc (lambda (x)
  9219. (setq current (delete x current)))
  9220. g)))
  9221. (push tg current))
  9222. (if exit-after-next (setq exit-after-next 'now))))
  9223. ;; Create a sorted list
  9224. (setq current
  9225. (sort current
  9226. (lambda (a b)
  9227. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9228. (if (eq exit-after-next 'now) (throw 'exit t))
  9229. (goto-char (point-min))
  9230. (beginning-of-line 2)
  9231. (delete-region (point) (point-at-eol))
  9232. (org-fast-tag-insert "Current" current c-face)
  9233. (org-set-current-tags-overlay current ov-prefix)
  9234. (while (re-search-forward
  9235. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9236. (setq tg (match-string 1))
  9237. (add-text-properties
  9238. (match-beginning 1) (match-end 1)
  9239. (list 'face
  9240. (cond
  9241. ((member tg current) c-face)
  9242. ((member tg inherited) i-face)
  9243. (t (get-text-property (match-beginning 1) 'face))))))
  9244. (goto-char (point-min)))))
  9245. (org-detach-overlay org-tags-overlay)
  9246. (if rtn
  9247. (mapconcat 'identity current ":")
  9248. nil))))
  9249. (defun org-get-tags-string ()
  9250. "Get the TAGS string in the current headline."
  9251. (unless (org-on-heading-p t)
  9252. (error "Not on a heading"))
  9253. (save-excursion
  9254. (beginning-of-line 1)
  9255. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9256. (org-match-string-no-properties 1)
  9257. "")))
  9258. (defun org-get-tags ()
  9259. "Get the list of tags specified in the current headline."
  9260. (org-split-string (org-get-tags-string) ":"))
  9261. (defun org-get-buffer-tags ()
  9262. "Get a table of all tags used in the buffer, for completion."
  9263. (let (tags)
  9264. (save-excursion
  9265. (goto-char (point-min))
  9266. (while (re-search-forward
  9267. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9268. (when (equal (char-after (point-at-bol 0)) ?*)
  9269. (mapc (lambda (x) (add-to-list 'tags x))
  9270. (org-split-string (org-match-string-no-properties 1) ":")))))
  9271. (mapcar 'list tags)))
  9272. ;;;; The mapping API
  9273. ;;;###autoload
  9274. (defun org-map-entries (func &optional match scope &rest skip)
  9275. "Call FUNC at each headline selected by MATCH in SCOPE.
  9276. FUNC is a function or a lisp form. The function will be called without
  9277. arguments, with the cursor positioned at the beginning of the headline.
  9278. The return values of all calls to the function will be collected and
  9279. returned as a list.
  9280. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9281. Only headlines that are matched by this query will be considered during
  9282. the iteration. When MATCH is nil or t, all headlines will be
  9283. visited by the iteration.
  9284. SCOPE determines the scope of this command. It can be any of:
  9285. nil The current buffer, respecting the restriction if any
  9286. tree The subtree started with the entry at point
  9287. file The current buffer, without restriction
  9288. file-with-archives
  9289. The current buffer, and any archives associated with it
  9290. agenda All agenda files
  9291. agenda-with-archives
  9292. All agenda files with any archive files associated with them
  9293. \(file1 file2 ...)
  9294. If this is a list, all files in the list will be scanned
  9295. The remaining args are treated as settings for the skipping facilities of
  9296. the scanner. The following items can be given here:
  9297. archive skip trees with the archive tag.
  9298. comment skip trees with the COMMENT keyword
  9299. function or Emacs Lisp form:
  9300. will be used as value for `org-agenda-skip-function', so whenever
  9301. the the function returns t, FUNC will not be called for that
  9302. entry and search will continue from the point where the
  9303. function leaves it."
  9304. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9305. (org-agenda-skip-archived-trees (memq 'archive skip))
  9306. (org-agenda-skip-comment-trees (memq 'comment skip))
  9307. (org-agenda-skip-function
  9308. (car (org-delete-all '(comment archive) skip)))
  9309. (org-tags-match-list-sublevels t)
  9310. matcher file res
  9311. org-todo-keywords-for-agenda
  9312. org-done-keywords-for-agenda
  9313. org-todo-keyword-alist-for-agenda
  9314. org-tag-alist-for-agenda)
  9315. (cond
  9316. ((eq match t) (setq matcher t))
  9317. ((eq match nil) (setq matcher t))
  9318. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9319. (save-excursion
  9320. (save-restriction
  9321. (when (eq scope 'tree)
  9322. (org-back-to-heading t)
  9323. (org-narrow-to-subtree)
  9324. (setq scope nil))
  9325. (if (not scope)
  9326. (progn
  9327. (org-prepare-agenda-buffers
  9328. (list (buffer-file-name (current-buffer))))
  9329. (setq res (org-scan-tags func matcher)))
  9330. ;; Get the right scope
  9331. (cond
  9332. ((and scope (listp scope) (symbolp (car scope)))
  9333. (setq scope (eval scope)))
  9334. ((eq scope 'agenda)
  9335. (setq scope (org-agenda-files t)))
  9336. ((eq scope 'agenda-with-archives)
  9337. (setq scope (org-agenda-files t))
  9338. (setq scope (org-add-archive-files scope)))
  9339. ((eq scope 'file)
  9340. (setq scope (list (buffer-file-name))))
  9341. ((eq scope 'file-with-archives)
  9342. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9343. (org-prepare-agenda-buffers scope)
  9344. (while (setq file (pop scope))
  9345. (with-current-buffer (org-find-base-buffer-visiting file)
  9346. (save-excursion
  9347. (save-restriction
  9348. (widen)
  9349. (goto-char (point-min))
  9350. (setq res (append res (org-scan-tags func matcher))))))))))
  9351. res))
  9352. ;;;; Properties
  9353. ;;; Setting and retrieving properties
  9354. (defconst org-special-properties
  9355. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9356. "TIMESTAMP" "TIMESTAMP_IA")
  9357. "The special properties valid in Org-mode.
  9358. These are properties that are not defined in the property drawer,
  9359. but in some other way.")
  9360. (defconst org-default-properties
  9361. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9362. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9363. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9364. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9365. "Some properties that are used by Org-mode for various purposes.
  9366. Being in this list makes sure that they are offered for completion.")
  9367. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9368. "Regular expression matching the first line of a property drawer.")
  9369. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9370. "Regular expression matching the first line of a property drawer.")
  9371. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9372. "Regular expression matching the first line of a property drawer.")
  9373. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9374. "Regular expression matching the first line of a property drawer.")
  9375. (defconst org-property-drawer-re
  9376. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9377. org-property-end-re "\\)\n?")
  9378. "Matches an entire property drawer.")
  9379. (defconst org-clock-drawer-re
  9380. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9381. org-property-end-re "\\)\n?")
  9382. "Matches an entire clock drawer.")
  9383. (defun org-property-action ()
  9384. "Do an action on properties."
  9385. (interactive)
  9386. (let (c)
  9387. (org-at-property-p)
  9388. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9389. (setq c (read-char-exclusive))
  9390. (cond
  9391. ((equal c ?s)
  9392. (call-interactively 'org-set-property))
  9393. ((equal c ?d)
  9394. (call-interactively 'org-delete-property))
  9395. ((equal c ?D)
  9396. (call-interactively 'org-delete-property-globally))
  9397. ((equal c ?c)
  9398. (call-interactively 'org-compute-property-at-point))
  9399. (t (error "No such property action %c" c)))))
  9400. (defun org-at-property-p ()
  9401. "Is the cursor in a property line?"
  9402. ;; FIXME: Does not check if we are actually in the drawer.
  9403. ;; FIXME: also returns true on any drawers.....
  9404. ;; This is used by C-c C-c for property action.
  9405. (save-excursion
  9406. (beginning-of-line 1)
  9407. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9408. (defun org-get-property-block (&optional beg end force)
  9409. "Return the (beg . end) range of the body of the property drawer.
  9410. BEG and END can be beginning and end of subtree, if not given
  9411. they will be found.
  9412. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9413. (catch 'exit
  9414. (save-excursion
  9415. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9416. (end (or end (progn (outline-next-heading) (point)))))
  9417. (goto-char beg)
  9418. (if (re-search-forward org-property-start-re end t)
  9419. (setq beg (1+ (match-end 0)))
  9420. (if force
  9421. (save-excursion
  9422. (org-insert-property-drawer)
  9423. (setq end (progn (outline-next-heading) (point))))
  9424. (throw 'exit nil))
  9425. (goto-char beg)
  9426. (if (re-search-forward org-property-start-re end t)
  9427. (setq beg (1+ (match-end 0)))))
  9428. (if (re-search-forward org-property-end-re end t)
  9429. (setq end (match-beginning 0))
  9430. (or force (throw 'exit nil))
  9431. (goto-char beg)
  9432. (setq end beg)
  9433. (org-indent-line-function)
  9434. (insert ":END:\n"))
  9435. (cons beg end)))))
  9436. (defun org-entry-properties (&optional pom which)
  9437. "Get all properties of the entry at point-or-marker POM.
  9438. This includes the TODO keyword, the tags, time strings for deadline,
  9439. scheduled, and clocking, and any additional properties defined in the
  9440. entry. The return value is an alist, keys may occur multiple times
  9441. if the property key was used several times.
  9442. POM may also be nil, in which case the current entry is used.
  9443. If WHICH is nil or `all', get all properties. If WHICH is
  9444. `special' or `standard', only get that subclass."
  9445. (setq which (or which 'all))
  9446. (org-with-point-at pom
  9447. (let ((clockstr (substring org-clock-string 0 -1))
  9448. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9449. beg end range props sum-props key value string clocksum)
  9450. (save-excursion
  9451. (when (condition-case nil
  9452. (and (org-mode-p) (org-back-to-heading t))
  9453. (error nil))
  9454. (setq beg (point))
  9455. (setq sum-props (get-text-property (point) 'org-summaries))
  9456. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9457. (outline-next-heading)
  9458. (setq end (point))
  9459. (when (memq which '(all special))
  9460. ;; Get the special properties, like TODO and tags
  9461. (goto-char beg)
  9462. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9463. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9464. (when (looking-at org-priority-regexp)
  9465. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9466. (when (and (setq value (org-get-tags-string))
  9467. (string-match "\\S-" value))
  9468. (push (cons "TAGS" value) props))
  9469. (when (setq value (org-get-tags-at))
  9470. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9471. props))
  9472. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9473. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9474. string (if (equal key clockstr)
  9475. (org-no-properties
  9476. (org-trim
  9477. (buffer-substring
  9478. (match-beginning 3) (goto-char (point-at-eol)))))
  9479. (substring (org-match-string-no-properties 3) 1 -1)))
  9480. (unless key
  9481. (if (= (char-after (match-beginning 3)) ?\[)
  9482. (setq key "TIMESTAMP_IA")
  9483. (setq key "TIMESTAMP")))
  9484. (when (or (equal key clockstr) (not (assoc key props)))
  9485. (push (cons key string) props)))
  9486. )
  9487. (when (memq which '(all standard))
  9488. ;; Get the standard properties, like :PORP: ...
  9489. (setq range (org-get-property-block beg end))
  9490. (when range
  9491. (goto-char (car range))
  9492. (while (re-search-forward
  9493. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9494. (cdr range) t)
  9495. (setq key (org-match-string-no-properties 1)
  9496. value (org-trim (or (org-match-string-no-properties 2) "")))
  9497. (unless (member key excluded)
  9498. (push (cons key (or value "")) props)))))
  9499. (if clocksum
  9500. (push (cons "CLOCKSUM"
  9501. (org-columns-number-to-string (/ (float clocksum) 60.)
  9502. 'add_times))
  9503. props))
  9504. (unless (assoc "CATEGORY" props)
  9505. (setq value (or (org-get-category)
  9506. (progn (org-refresh-category-properties)
  9507. (org-get-category))))
  9508. (push (cons "CATEGORY" value) props))
  9509. (append sum-props (nreverse props)))))))
  9510. (defun org-entry-get (pom property &optional inherit)
  9511. "Get value of PROPERTY for entry at point-or-marker POM.
  9512. If INHERIT is non-nil and the entry does not have the property,
  9513. then also check higher levels of the hierarchy.
  9514. If INHERIT is the symbol `selective', use inheritance only if the setting
  9515. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9516. If the property is present but empty, the return value is the empty string.
  9517. If the property is not present at all, nil is returned."
  9518. (org-with-point-at pom
  9519. (if (and inherit (if (eq inherit 'selective)
  9520. (org-property-inherit-p property)
  9521. t))
  9522. (org-entry-get-with-inheritance property)
  9523. (if (member property org-special-properties)
  9524. ;; We need a special property. Use brute force, get all properties.
  9525. (cdr (assoc property (org-entry-properties nil 'special)))
  9526. (let ((range (org-get-property-block)))
  9527. (if (and range
  9528. (goto-char (car range))
  9529. (re-search-forward
  9530. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9531. (cdr range) t))
  9532. ;; Found the property, return it.
  9533. (if (match-end 1)
  9534. (org-match-string-no-properties 1)
  9535. "")))))))
  9536. (defun org-property-or-variable-value (var &optional inherit)
  9537. "Check if there is a property fixing the value of VAR.
  9538. If yes, return this value. If not, return the current value of the variable."
  9539. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9540. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9541. (read prop)
  9542. (symbol-value var))))
  9543. (defun org-entry-delete (pom property)
  9544. "Delete the property PROPERTY from entry at point-or-marker POM."
  9545. (org-with-point-at pom
  9546. (if (member property org-special-properties)
  9547. nil ; cannot delete these properties.
  9548. (let ((range (org-get-property-block)))
  9549. (if (and range
  9550. (goto-char (car range))
  9551. (re-search-forward
  9552. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9553. (cdr range) t))
  9554. (progn
  9555. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9556. t)
  9557. nil)))))
  9558. ;; Multi-values properties are properties that contain multiple values
  9559. ;; These values are assumed to be single words, separated by whitespace.
  9560. (defun org-entry-add-to-multivalued-property (pom property value)
  9561. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9562. (let* ((old (org-entry-get pom property))
  9563. (values (and old (org-split-string old "[ \t]"))))
  9564. (setq value (org-entry-protect-space value))
  9565. (unless (member value values)
  9566. (setq values (cons value values))
  9567. (org-entry-put pom property
  9568. (mapconcat 'identity values " ")))))
  9569. (defun org-entry-remove-from-multivalued-property (pom property value)
  9570. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9571. (let* ((old (org-entry-get pom property))
  9572. (values (and old (org-split-string old "[ \t]"))))
  9573. (setq value (org-entry-protect-space value))
  9574. (when (member value values)
  9575. (setq values (delete value values))
  9576. (org-entry-put pom property
  9577. (mapconcat 'identity values " ")))))
  9578. (defun org-entry-member-in-multivalued-property (pom property value)
  9579. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9580. (let* ((old (org-entry-get pom property))
  9581. (values (and old (org-split-string old "[ \t]"))))
  9582. (setq value (org-entry-protect-space value))
  9583. (member value values)))
  9584. (defun org-entry-get-multivalued-property (pom property)
  9585. "Return a list of values in a multivalued property."
  9586. (let* ((value (org-entry-get pom property))
  9587. (values (and value (org-split-string value "[ \t]"))))
  9588. (mapcar 'org-entry-restore-space values)))
  9589. (defun org-entry-put-multivalued-property (pom property &rest values)
  9590. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9591. VALUES should be a list of strings. Spaces will be protected."
  9592. (org-entry-put pom property
  9593. (mapconcat 'org-entry-protect-space values " "))
  9594. (let* ((value (org-entry-get pom property))
  9595. (values (and value (org-split-string value "[ \t]"))))
  9596. (mapcar 'org-entry-restore-space values)))
  9597. (defun org-entry-protect-space (s)
  9598. "Protect spaces and newline in string S."
  9599. (while (string-match " " s)
  9600. (setq s (replace-match "%20" t t s)))
  9601. (while (string-match "\n" s)
  9602. (setq s (replace-match "%0A" t t s)))
  9603. s)
  9604. (defun org-entry-restore-space (s)
  9605. "Restore spaces and newline in string S."
  9606. (while (string-match "%20" s)
  9607. (setq s (replace-match " " t t s)))
  9608. (while (string-match "%0A" s)
  9609. (setq s (replace-match "\n" t t s)))
  9610. s)
  9611. (defvar org-entry-property-inherited-from (make-marker)
  9612. "Marker pointing to the entry from where a property was inherited.
  9613. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9614. location of the entry where the inheritance search matched. If there was
  9615. no match, the marker will point nowhere.
  9616. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9617. is set.")
  9618. (defun org-entry-get-with-inheritance (property)
  9619. "Get entry property, and search higher levels if not present."
  9620. (move-marker org-entry-property-inherited-from nil)
  9621. (let (tmp)
  9622. (save-excursion
  9623. (save-restriction
  9624. (widen)
  9625. (catch 'ex
  9626. (while t
  9627. (when (setq tmp (org-entry-get nil property))
  9628. (org-back-to-heading t)
  9629. (move-marker org-entry-property-inherited-from (point))
  9630. (throw 'ex tmp))
  9631. (or (org-up-heading-safe) (throw 'ex nil)))))
  9632. (or tmp
  9633. (cdr (assoc property org-file-properties))
  9634. (cdr (assoc property org-global-properties))
  9635. (cdr (assoc property org-global-properties-fixed))))))
  9636. (defun org-entry-put (pom property value)
  9637. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9638. (org-with-point-at pom
  9639. (org-back-to-heading t)
  9640. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9641. range)
  9642. (cond
  9643. ((equal property "TODO")
  9644. (when (and (stringp value) (string-match "\\S-" value)
  9645. (not (member value org-todo-keywords-1)))
  9646. (error "\"%s\" is not a valid TODO state" value))
  9647. (if (or (not value)
  9648. (not (string-match "\\S-" value)))
  9649. (setq value 'none))
  9650. (org-todo value)
  9651. (org-set-tags nil 'align))
  9652. ((equal property "PRIORITY")
  9653. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9654. (string-to-char value) ?\ ))
  9655. (org-set-tags nil 'align))
  9656. ((equal property "SCHEDULED")
  9657. (if (re-search-forward org-scheduled-time-regexp end t)
  9658. (cond
  9659. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9660. ((eq value 'later) (org-timestamp-change 1 'day))
  9661. (t (call-interactively 'org-schedule)))
  9662. (call-interactively 'org-schedule)))
  9663. ((equal property "DEADLINE")
  9664. (if (re-search-forward org-deadline-time-regexp end t)
  9665. (cond
  9666. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9667. ((eq value 'later) (org-timestamp-change 1 'day))
  9668. (t (call-interactively 'org-deadline)))
  9669. (call-interactively 'org-deadline)))
  9670. ((member property org-special-properties)
  9671. (error "The %s property can not yet be set with `org-entry-put'"
  9672. property))
  9673. (t ; a non-special property
  9674. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9675. (setq range (org-get-property-block beg end 'force))
  9676. (goto-char (car range))
  9677. (if (re-search-forward
  9678. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9679. (progn
  9680. (delete-region (match-beginning 1) (match-end 1))
  9681. (goto-char (match-beginning 1)))
  9682. (goto-char (cdr range))
  9683. (insert "\n")
  9684. (backward-char 1)
  9685. (org-indent-line-function)
  9686. (insert ":" property ":"))
  9687. (and value (insert " " value))
  9688. (org-indent-line-function)))))))
  9689. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9690. "Get all property keys in the current buffer.
  9691. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9692. like tags and TODO state.
  9693. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9694. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9695. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9696. formats in the current buffer."
  9697. (let (rtn range cfmt s p)
  9698. (save-excursion
  9699. (save-restriction
  9700. (widen)
  9701. (goto-char (point-min))
  9702. (while (re-search-forward org-property-start-re nil t)
  9703. (setq range (org-get-property-block))
  9704. (goto-char (car range))
  9705. (while (re-search-forward
  9706. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9707. (cdr range) t)
  9708. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9709. (outline-next-heading))))
  9710. (when include-specials
  9711. (setq rtn (append org-special-properties rtn)))
  9712. (when include-defaults
  9713. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9714. (when include-columns
  9715. (save-excursion
  9716. (save-restriction
  9717. (widen)
  9718. (goto-char (point-min))
  9719. (while (re-search-forward
  9720. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9721. nil t)
  9722. (setq cfmt (match-string 2) s 0)
  9723. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9724. cfmt s)
  9725. (setq s (match-end 0)
  9726. p (match-string 1 cfmt))
  9727. (unless (or (equal p "ITEM")
  9728. (member p org-special-properties))
  9729. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9730. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9731. (defun org-property-values (key)
  9732. "Return a list of all values of property KEY."
  9733. (save-excursion
  9734. (save-restriction
  9735. (widen)
  9736. (goto-char (point-min))
  9737. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9738. values)
  9739. (while (re-search-forward re nil t)
  9740. (add-to-list 'values (org-trim (match-string 1))))
  9741. (delete "" values)))))
  9742. (defun org-insert-property-drawer ()
  9743. "Insert a property drawer into the current entry."
  9744. (interactive)
  9745. (org-back-to-heading t)
  9746. (looking-at outline-regexp)
  9747. (let ((indent (- (match-end 0)(match-beginning 0)))
  9748. (beg (point))
  9749. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9750. end hiddenp)
  9751. (outline-next-heading)
  9752. (setq end (point))
  9753. (goto-char beg)
  9754. (while (re-search-forward re end t))
  9755. (setq hiddenp (org-invisible-p))
  9756. (end-of-line 1)
  9757. (and (equal (char-after) ?\n) (forward-char 1))
  9758. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9759. (beginning-of-line 2))
  9760. (org-skip-over-state-notes)
  9761. (skip-chars-backward " \t\n\r")
  9762. (if (eq (char-before) ?*) (forward-char 1))
  9763. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9764. (beginning-of-line 0)
  9765. (org-indent-to-column indent)
  9766. (beginning-of-line 2)
  9767. (org-indent-to-column indent)
  9768. (beginning-of-line 0)
  9769. (if hiddenp
  9770. (save-excursion
  9771. (org-back-to-heading t)
  9772. (hide-entry))
  9773. (org-flag-drawer t))))
  9774. (defun org-set-property (property value)
  9775. "In the current entry, set PROPERTY to VALUE.
  9776. When called interactively, this will prompt for a property name, offering
  9777. completion on existing and default properties. And then it will prompt
  9778. for a value, offering completion either on allowed values (via an inherited
  9779. xxx_ALL property) or on existing values in other instances of this property
  9780. in the current file."
  9781. (interactive
  9782. (let* ((completion-ignore-case t)
  9783. (keys (org-buffer-property-keys nil t t))
  9784. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9785. (prop (if (member prop0 keys)
  9786. prop0
  9787. (or (cdr (assoc (downcase prop0)
  9788. (mapcar (lambda (x) (cons (downcase x) x))
  9789. keys)))
  9790. prop0)))
  9791. (cur (org-entry-get nil prop))
  9792. (allowed (org-property-get-allowed-values nil prop 'table))
  9793. (existing (mapcar 'list (org-property-values prop)))
  9794. (val (if allowed
  9795. (org-completing-read "Value: " allowed nil 'req-match)
  9796. (org-completing-read
  9797. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9798. (concat "[" cur "]") "")
  9799. ": ")
  9800. existing nil nil "" nil cur))))
  9801. (list prop (if (equal val "") cur val))))
  9802. (unless (equal (org-entry-get nil property) value)
  9803. (org-entry-put nil property value)))
  9804. (defun org-delete-property (property)
  9805. "In the current entry, delete PROPERTY."
  9806. (interactive
  9807. (let* ((completion-ignore-case t)
  9808. (prop (org-ido-completing-read
  9809. "Property: " (org-entry-properties nil 'standard))))
  9810. (list prop)))
  9811. (message "Property %s %s" property
  9812. (if (org-entry-delete nil property)
  9813. "deleted"
  9814. "was not present in the entry")))
  9815. (defun org-delete-property-globally (property)
  9816. "Remove PROPERTY globally, from all entries."
  9817. (interactive
  9818. (let* ((completion-ignore-case t)
  9819. (prop (org-ido-completing-read
  9820. "Globally remove property: "
  9821. (mapcar 'list (org-buffer-property-keys)))))
  9822. (list prop)))
  9823. (save-excursion
  9824. (save-restriction
  9825. (widen)
  9826. (goto-char (point-min))
  9827. (let ((cnt 0))
  9828. (while (re-search-forward
  9829. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9830. nil t)
  9831. (setq cnt (1+ cnt))
  9832. (replace-match ""))
  9833. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9834. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9835. (defun org-compute-property-at-point ()
  9836. "Compute the property at point.
  9837. This looks for an enclosing column format, extracts the operator and
  9838. then applies it to the property in the column format's scope."
  9839. (interactive)
  9840. (unless (org-at-property-p)
  9841. (error "Not at a property"))
  9842. (let ((prop (org-match-string-no-properties 2)))
  9843. (org-columns-get-format-and-top-level)
  9844. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9845. (error "No operator defined for property %s" prop))
  9846. (org-columns-compute prop)))
  9847. (defun org-property-get-allowed-values (pom property &optional table)
  9848. "Get allowed values for the property PROPERTY.
  9849. When TABLE is non-nil, return an alist that can directly be used for
  9850. completion."
  9851. (let (vals)
  9852. (cond
  9853. ((equal property "TODO")
  9854. (setq vals (org-with-point-at pom
  9855. (append org-todo-keywords-1 '("")))))
  9856. ((equal property "PRIORITY")
  9857. (let ((n org-lowest-priority))
  9858. (while (>= n org-highest-priority)
  9859. (push (char-to-string n) vals)
  9860. (setq n (1- n)))))
  9861. ((member property org-special-properties))
  9862. (t
  9863. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9864. (when (and vals (string-match "\\S-" vals))
  9865. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9866. (setq vals (mapcar (lambda (x)
  9867. (cond ((stringp x) x)
  9868. ((numberp x) (number-to-string x))
  9869. ((symbolp x) (symbol-name x))
  9870. (t "???")))
  9871. vals)))))
  9872. (if table (mapcar 'list vals) vals)))
  9873. (defun org-property-previous-allowed-value (&optional previous)
  9874. "Switch to the next allowed value for this property."
  9875. (interactive)
  9876. (org-property-next-allowed-value t))
  9877. (defun org-property-next-allowed-value (&optional previous)
  9878. "Switch to the next allowed value for this property."
  9879. (interactive)
  9880. (unless (org-at-property-p)
  9881. (error "Not at a property"))
  9882. (let* ((key (match-string 2))
  9883. (value (match-string 3))
  9884. (allowed (or (org-property-get-allowed-values (point) key)
  9885. (and (member value '("[ ]" "[-]" "[X]"))
  9886. '("[ ]" "[X]"))))
  9887. nval)
  9888. (unless allowed
  9889. (error "Allowed values for this property have not been defined"))
  9890. (if previous (setq allowed (reverse allowed)))
  9891. (if (member value allowed)
  9892. (setq nval (car (cdr (member value allowed)))))
  9893. (setq nval (or nval (car allowed)))
  9894. (if (equal nval value)
  9895. (error "Only one allowed value for this property"))
  9896. (org-at-property-p)
  9897. (replace-match (concat " :" key ": " nval) t t)
  9898. (org-indent-line-function)
  9899. (beginning-of-line 1)
  9900. (skip-chars-forward " \t")))
  9901. (defun org-find-entry-with-id (ident)
  9902. "Locate the entry that contains the ID property with exact value IDENT.
  9903. IDENT can be a string, a symbol or a number, this function will search for
  9904. the string representation of it.
  9905. Return the position where this entry starts, or nil if there is no such entry."
  9906. (interactive "sID: ")
  9907. (let ((id (cond
  9908. ((stringp ident) ident)
  9909. ((symbol-name ident) (symbol-name ident))
  9910. ((numberp ident) (number-to-string ident))
  9911. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9912. (case-fold-search nil))
  9913. (save-excursion
  9914. (save-restriction
  9915. (widen)
  9916. (goto-char (point-min))
  9917. (when (re-search-forward
  9918. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9919. nil t)
  9920. (org-back-to-heading)
  9921. (point))))))
  9922. ;;;; Timestamps
  9923. (defvar org-last-changed-timestamp nil)
  9924. (defvar org-last-inserted-timestamp nil
  9925. "The last time stamp inserted with `org-insert-time-stamp'.")
  9926. (defvar org-time-was-given) ; dynamically scoped parameter
  9927. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9928. (defvar org-ts-what) ; dynamically scoped parameter
  9929. (defun org-time-stamp (arg &optional inactive)
  9930. "Prompt for a date/time and insert a time stamp.
  9931. If the user specifies a time like HH:MM, or if this command is called
  9932. with a prefix argument, the time stamp will contain date and time.
  9933. Otherwise, only the date will be included. All parts of a date not
  9934. specified by the user will be filled in from the current date/time.
  9935. So if you press just return without typing anything, the time stamp
  9936. will represent the current date/time. If there is already a timestamp
  9937. at the cursor, it will be modified."
  9938. (interactive "P")
  9939. (let* ((ts nil)
  9940. (default-time
  9941. ;; Default time is either today, or, when entering a range,
  9942. ;; the range start.
  9943. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9944. (save-excursion
  9945. (re-search-backward
  9946. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9947. (- (point) 20) t)))
  9948. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9949. (current-time)))
  9950. (default-input (and ts (org-get-compact-tod ts)))
  9951. org-time-was-given org-end-time-was-given time)
  9952. (cond
  9953. ((and (org-at-timestamp-p t)
  9954. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9955. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9956. (insert "--")
  9957. (setq time (let ((this-command this-command))
  9958. (org-read-date arg 'totime nil nil
  9959. default-time default-input)))
  9960. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9961. ((org-at-timestamp-p t)
  9962. (setq time (let ((this-command this-command))
  9963. (org-read-date arg 'totime nil nil default-time default-input)))
  9964. (when (org-at-timestamp-p t) ; just to get the match data
  9965. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9966. (replace-match "")
  9967. (setq org-last-changed-timestamp
  9968. (org-insert-time-stamp
  9969. time (or org-time-was-given arg)
  9970. inactive nil nil (list org-end-time-was-given))))
  9971. (message "Timestamp updated"))
  9972. (t
  9973. (setq time (let ((this-command this-command))
  9974. (org-read-date arg 'totime nil nil default-time default-input)))
  9975. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9976. nil nil (list org-end-time-was-given))))))
  9977. ;; FIXME: can we use this for something else, like computing time differences?
  9978. (defun org-get-compact-tod (s)
  9979. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9980. (let* ((t1 (match-string 1 s))
  9981. (h1 (string-to-number (match-string 2 s)))
  9982. (m1 (string-to-number (match-string 3 s)))
  9983. (t2 (and (match-end 4) (match-string 5 s)))
  9984. (h2 (and t2 (string-to-number (match-string 6 s))))
  9985. (m2 (and t2 (string-to-number (match-string 7 s))))
  9986. dh dm)
  9987. (if (not t2)
  9988. t1
  9989. (setq dh (- h2 h1) dm (- m2 m1))
  9990. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9991. (concat t1 "+" (number-to-string dh)
  9992. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9993. (defun org-time-stamp-inactive (&optional arg)
  9994. "Insert an inactive time stamp.
  9995. An inactive time stamp is enclosed in square brackets instead of angle
  9996. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9997. does not link to the calendar and cannot be changed with the S-cursor keys.
  9998. So these are more for recording a certain time/date."
  9999. (interactive "P")
  10000. (org-time-stamp arg 'inactive))
  10001. (defvar org-date-ovl (org-make-overlay 1 1))
  10002. (org-overlay-put org-date-ovl 'face 'org-warning)
  10003. (org-detach-overlay org-date-ovl)
  10004. (defvar org-ans1) ; dynamically scoped parameter
  10005. (defvar org-ans2) ; dynamically scoped parameter
  10006. (defvar org-plain-time-of-day-regexp) ; defined below
  10007. (defvar org-overriding-default-time nil) ; dynamically scoped
  10008. (defvar org-read-date-overlay nil)
  10009. (defvar org-dcst nil) ; dynamically scoped
  10010. (defun org-read-date (&optional with-time to-time from-string prompt
  10011. default-time default-input)
  10012. "Read a date, possibly a time, and make things smooth for the user.
  10013. The prompt will suggest to enter an ISO date, but you can also enter anything
  10014. which will at least partially be understood by `parse-time-string'.
  10015. Unrecognized parts of the date will default to the current day, month, year,
  10016. hour and minute. If this command is called to replace a timestamp at point,
  10017. of to enter the second timestamp of a range, the default time is taken from the
  10018. existing stamp. For example,
  10019. 3-2-5 --> 2003-02-05
  10020. feb 15 --> currentyear-02-15
  10021. sep 12 9 --> 2009-09-12
  10022. 12:45 --> today 12:45
  10023. 22 sept 0:34 --> currentyear-09-22 0:34
  10024. 12 --> currentyear-currentmonth-12
  10025. Fri --> nearest Friday (today or later)
  10026. etc.
  10027. Furthermore you can specify a relative date by giving, as the *first* thing
  10028. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10029. change in days weeks, months, years.
  10030. With a single plus or minus, the date is relative to today. With a double
  10031. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10032. +4d --> four days from today
  10033. +4 --> same as above
  10034. +2w --> two weeks from today
  10035. ++5 --> five days from default date
  10036. The function understands only English month and weekday abbreviations,
  10037. but this can be configured with the variables `parse-time-months' and
  10038. `parse-time-weekdays'.
  10039. While prompting, a calendar is popped up - you can also select the
  10040. date with the mouse (button 1). The calendar shows a period of three
  10041. months. To scroll it to other months, use the keys `>' and `<'.
  10042. If you don't like the calendar, turn it off with
  10043. \(setq org-read-date-popup-calendar nil)
  10044. With optional argument TO-TIME, the date will immediately be converted
  10045. to an internal time.
  10046. With an optional argument WITH-TIME, the prompt will suggest to also
  10047. insert a time. Note that when WITH-TIME is not set, you can still
  10048. enter a time, and this function will inform the calling routine about
  10049. this change. The calling routine may then choose to change the format
  10050. used to insert the time stamp into the buffer to include the time.
  10051. With optional argument FROM-STRING, read from this string instead from
  10052. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10053. the time/date that is used for everything that is not specified by the
  10054. user."
  10055. (require 'parse-time)
  10056. (let* ((org-time-stamp-rounding-minutes
  10057. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10058. (org-dcst org-display-custom-times)
  10059. (ct (org-current-time))
  10060. (def (or org-overriding-default-time default-time ct))
  10061. (defdecode (decode-time def))
  10062. (dummy (progn
  10063. (when (< (nth 2 defdecode) org-extend-today-until)
  10064. (setcar (nthcdr 2 defdecode) -1)
  10065. (setcar (nthcdr 1 defdecode) 59)
  10066. (setq def (apply 'encode-time defdecode)
  10067. defdecode (decode-time def)))))
  10068. (calendar-move-hook nil)
  10069. (calendar-view-diary-initially-flag nil)
  10070. (view-diary-entries-initially nil)
  10071. (calendar-view-holidays-initially-flag nil)
  10072. (view-calendar-holidays-initially nil)
  10073. (timestr (format-time-string
  10074. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10075. (prompt (concat (if prompt (concat prompt " ") "")
  10076. (format "Date+time [%s]: " timestr)))
  10077. ans (org-ans0 "") org-ans1 org-ans2 final)
  10078. (cond
  10079. (from-string (setq ans from-string))
  10080. (org-read-date-popup-calendar
  10081. (save-excursion
  10082. (save-window-excursion
  10083. (calendar)
  10084. (calendar-forward-day (- (time-to-days def)
  10085. (calendar-absolute-from-gregorian
  10086. (calendar-current-date))))
  10087. (org-eval-in-calendar nil t)
  10088. (let* ((old-map (current-local-map))
  10089. (map (copy-keymap calendar-mode-map))
  10090. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10091. (org-defkey map (kbd "RET") 'org-calendar-select)
  10092. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10093. 'org-calendar-select-mouse)
  10094. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10095. 'org-calendar-select-mouse)
  10096. (org-defkey minibuffer-local-map [(meta shift left)]
  10097. (lambda () (interactive)
  10098. (org-eval-in-calendar '(calendar-backward-month 1))))
  10099. (org-defkey minibuffer-local-map [(meta shift right)]
  10100. (lambda () (interactive)
  10101. (org-eval-in-calendar '(calendar-forward-month 1))))
  10102. (org-defkey minibuffer-local-map [(meta shift up)]
  10103. (lambda () (interactive)
  10104. (org-eval-in-calendar '(calendar-backward-year 1))))
  10105. (org-defkey minibuffer-local-map [(meta shift down)]
  10106. (lambda () (interactive)
  10107. (org-eval-in-calendar '(calendar-forward-year 1))))
  10108. (org-defkey minibuffer-local-map [(shift up)]
  10109. (lambda () (interactive)
  10110. (org-eval-in-calendar '(calendar-backward-week 1))))
  10111. (org-defkey minibuffer-local-map [(shift down)]
  10112. (lambda () (interactive)
  10113. (org-eval-in-calendar '(calendar-forward-week 1))))
  10114. (org-defkey minibuffer-local-map [(shift left)]
  10115. (lambda () (interactive)
  10116. (org-eval-in-calendar '(calendar-backward-day 1))))
  10117. (org-defkey minibuffer-local-map [(shift right)]
  10118. (lambda () (interactive)
  10119. (org-eval-in-calendar '(calendar-forward-day 1))))
  10120. (org-defkey minibuffer-local-map ">"
  10121. (lambda () (interactive)
  10122. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10123. (org-defkey minibuffer-local-map "<"
  10124. (lambda () (interactive)
  10125. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10126. (unwind-protect
  10127. (progn
  10128. (use-local-map map)
  10129. (add-hook 'post-command-hook 'org-read-date-display)
  10130. (setq org-ans0 (read-string prompt default-input nil nil))
  10131. ;; org-ans0: from prompt
  10132. ;; org-ans1: from mouse click
  10133. ;; org-ans2: from calendar motion
  10134. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10135. (remove-hook 'post-command-hook 'org-read-date-display)
  10136. (use-local-map old-map)
  10137. (when org-read-date-overlay
  10138. (org-delete-overlay org-read-date-overlay)
  10139. (setq org-read-date-overlay nil)))))))
  10140. (t ; Naked prompt only
  10141. (unwind-protect
  10142. (setq ans (read-string prompt default-input nil timestr))
  10143. (when org-read-date-overlay
  10144. (org-delete-overlay org-read-date-overlay)
  10145. (setq org-read-date-overlay nil)))))
  10146. (setq final (org-read-date-analyze ans def defdecode))
  10147. (if to-time
  10148. (apply 'encode-time final)
  10149. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10150. (format "%04d-%02d-%02d %02d:%02d"
  10151. (nth 5 final) (nth 4 final) (nth 3 final)
  10152. (nth 2 final) (nth 1 final))
  10153. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10154. (defvar def)
  10155. (defvar defdecode)
  10156. (defvar with-time)
  10157. (defun org-read-date-display ()
  10158. "Display the current date prompt interpretation in the minibuffer."
  10159. (when org-read-date-display-live
  10160. (when org-read-date-overlay
  10161. (org-delete-overlay org-read-date-overlay))
  10162. (let ((p (point)))
  10163. (end-of-line 1)
  10164. (while (not (equal (buffer-substring
  10165. (max (point-min) (- (point) 4)) (point))
  10166. " "))
  10167. (insert " "))
  10168. (goto-char p))
  10169. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10170. " " (or org-ans1 org-ans2)))
  10171. (org-end-time-was-given nil)
  10172. (f (org-read-date-analyze ans def defdecode))
  10173. (fmts (if org-dcst
  10174. org-time-stamp-custom-formats
  10175. org-time-stamp-formats))
  10176. (fmt (if (or with-time
  10177. (and (boundp 'org-time-was-given) org-time-was-given))
  10178. (cdr fmts)
  10179. (car fmts)))
  10180. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10181. (when (and org-end-time-was-given
  10182. (string-match org-plain-time-of-day-regexp txt))
  10183. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10184. org-end-time-was-given
  10185. (substring txt (match-end 0)))))
  10186. (setq org-read-date-overlay
  10187. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10188. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10189. (defun org-read-date-analyze (ans def defdecode)
  10190. "Analyse the combined answer of the date prompt."
  10191. ;; FIXME: cleanup and comment
  10192. (let (delta deltan deltaw deltadef year month day
  10193. hour minute second wday pm h2 m2 tl wday1
  10194. iso-year iso-weekday iso-week iso-year iso-date)
  10195. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10196. (setq ans "+0"))
  10197. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10198. (setq ans (replace-match "" t t ans)
  10199. deltan (car delta)
  10200. deltaw (nth 1 delta)
  10201. deltadef (nth 2 delta)))
  10202. ;; Check if there is an iso week date in there
  10203. ;; If yes, sore the info and postpone interpreting it until the rest
  10204. ;; of the parsing is done
  10205. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10206. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10207. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10208. iso-week (string-to-number (match-string 2 ans)))
  10209. (setq ans (replace-match "" t t ans)))
  10210. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10211. (when (string-match
  10212. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10213. (setq year (if (match-end 2)
  10214. (string-to-number (match-string 2 ans))
  10215. (string-to-number (format-time-string "%Y")))
  10216. month (string-to-number (match-string 3 ans))
  10217. day (string-to-number (match-string 4 ans)))
  10218. (if (< year 100) (setq year (+ 2000 year)))
  10219. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10220. t nil ans)))
  10221. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10222. ;; If there is a time with am/pm, and *no* time without it, we convert
  10223. ;; so that matching will be successful.
  10224. (loop for i from 1 to 2 do ; twice, for end time as well
  10225. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10226. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10227. (setq hour (string-to-number (match-string 1 ans))
  10228. minute (if (match-end 3)
  10229. (string-to-number (match-string 3 ans))
  10230. 0)
  10231. pm (equal ?p
  10232. (string-to-char (downcase (match-string 4 ans)))))
  10233. (if (and (= hour 12) (not pm))
  10234. (setq hour 0)
  10235. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10236. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10237. t t ans))))
  10238. ;; Check if a time range is given as a duration
  10239. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10240. (setq hour (string-to-number (match-string 1 ans))
  10241. h2 (+ hour (string-to-number (match-string 3 ans)))
  10242. minute (string-to-number (match-string 2 ans))
  10243. m2 (+ minute (if (match-end 5) (string-to-number
  10244. (match-string 5 ans))0)))
  10245. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10246. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10247. t t ans)))
  10248. ;; Check if there is a time range
  10249. (when (boundp 'org-end-time-was-given)
  10250. (setq org-time-was-given nil)
  10251. (when (and (string-match org-plain-time-of-day-regexp ans)
  10252. (match-end 8))
  10253. (setq org-end-time-was-given (match-string 8 ans))
  10254. (setq ans (concat (substring ans 0 (match-beginning 7))
  10255. (substring ans (match-end 7))))))
  10256. (setq tl (parse-time-string ans)
  10257. day (or (nth 3 tl) (nth 3 defdecode))
  10258. month (or (nth 4 tl)
  10259. (if (and org-read-date-prefer-future
  10260. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10261. (1+ (nth 4 defdecode))
  10262. (nth 4 defdecode)))
  10263. year (or (nth 5 tl)
  10264. (if (and org-read-date-prefer-future
  10265. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10266. (1+ (nth 5 defdecode))
  10267. (nth 5 defdecode)))
  10268. hour (or (nth 2 tl) (nth 2 defdecode))
  10269. minute (or (nth 1 tl) (nth 1 defdecode))
  10270. second (or (nth 0 tl) 0)
  10271. wday (nth 6 tl))
  10272. ;; Special date definitions below
  10273. (cond
  10274. (iso-week
  10275. ;; There was an iso week
  10276. (setq year (or iso-year year)
  10277. day (or iso-weekday wday 1)
  10278. wday nil ; to make sure that the trigger below does not match
  10279. iso-date (calendar-gregorian-from-absolute
  10280. (calendar-absolute-from-iso
  10281. (list iso-week day year))))
  10282. ; FIXME: Should we also push ISO weeks into the future?
  10283. ; (when (and org-read-date-prefer-future
  10284. ; (not iso-year)
  10285. ; (< (calendar-absolute-from-gregorian iso-date)
  10286. ; (time-to-days (current-time))))
  10287. ; (setq year (1+ year)
  10288. ; iso-date (calendar-gregorian-from-absolute
  10289. ; (calendar-absolute-from-iso
  10290. ; (list iso-week day year)))))
  10291. (setq month (car iso-date)
  10292. year (nth 2 iso-date)
  10293. day (nth 1 iso-date)))
  10294. (deltan
  10295. (unless deltadef
  10296. (let ((now (decode-time (current-time))))
  10297. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10298. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10299. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10300. ((equal deltaw "m") (setq month (+ month deltan)))
  10301. ((equal deltaw "y") (setq year (+ year deltan)))))
  10302. ((and wday (not (nth 3 tl)))
  10303. ;; Weekday was given, but no day, so pick that day in the week
  10304. ;; on or after the derived date.
  10305. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10306. (unless (equal wday wday1)
  10307. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10308. (if (and (boundp 'org-time-was-given)
  10309. (nth 2 tl))
  10310. (setq org-time-was-given t))
  10311. (if (< year 100) (setq year (+ 2000 year)))
  10312. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10313. (list second minute hour day month year)))
  10314. (defvar parse-time-weekdays)
  10315. (defun org-read-date-get-relative (s today default)
  10316. "Check string S for special relative date string.
  10317. TODAY and DEFAULT are internal times, for today and for a default.
  10318. Return shift list (N what def-flag)
  10319. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10320. N is the number of WHATs to shift.
  10321. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10322. the DEFAULT date rather than TODAY."
  10323. (when (and
  10324. (string-match
  10325. (concat
  10326. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10327. "\\([0-9]+\\)?"
  10328. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10329. "\\([ \t]\\|$\\)") s)
  10330. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10331. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10332. (string-to-char (substring (match-string 1 s) -1))
  10333. ?+))
  10334. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10335. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10336. (what (if (match-end 3) (match-string 3 s) "d"))
  10337. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10338. (date (if rel default today))
  10339. (wday (nth 6 (decode-time date)))
  10340. delta)
  10341. (if wday1
  10342. (progn
  10343. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10344. (if (= dir ?-) (setq delta (- delta 7)))
  10345. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10346. (list delta "d" rel))
  10347. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10348. (defun org-eval-in-calendar (form &optional keepdate)
  10349. "Eval FORM in the calendar window and return to current window.
  10350. Also, store the cursor date in variable org-ans2."
  10351. (let ((sw (selected-window)))
  10352. (select-window (get-buffer-window "*Calendar*"))
  10353. (eval form)
  10354. (when (and (not keepdate) (calendar-cursor-to-date))
  10355. (let* ((date (calendar-cursor-to-date))
  10356. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10357. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10358. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10359. (select-window sw)))
  10360. (defun org-calendar-select ()
  10361. "Return to `org-read-date' with the date currently selected.
  10362. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10363. (interactive)
  10364. (when (calendar-cursor-to-date)
  10365. (let* ((date (calendar-cursor-to-date))
  10366. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10367. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10368. (if (active-minibuffer-window) (exit-minibuffer))))
  10369. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10370. "Insert a date stamp for the date given by the internal TIME.
  10371. WITH-HM means, use the stamp format that includes the time of the day.
  10372. INACTIVE means use square brackets instead of angular ones, so that the
  10373. stamp will not contribute to the agenda.
  10374. PRE and POST are optional strings to be inserted before and after the
  10375. stamp.
  10376. The command returns the inserted time stamp."
  10377. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10378. stamp)
  10379. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10380. (insert-before-markers (or pre ""))
  10381. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10382. (when (listp extra)
  10383. (setq extra (car extra))
  10384. (if (and (stringp extra)
  10385. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10386. (setq extra (format "-%02d:%02d"
  10387. (string-to-number (match-string 1 extra))
  10388. (string-to-number (match-string 2 extra))))
  10389. (setq extra nil)))
  10390. (when extra
  10391. (backward-char 1)
  10392. (insert-before-markers extra)
  10393. (forward-char 1))
  10394. (insert-before-markers (or post ""))
  10395. (setq org-last-inserted-timestamp stamp)))
  10396. (defun org-toggle-time-stamp-overlays ()
  10397. "Toggle the use of custom time stamp formats."
  10398. (interactive)
  10399. (setq org-display-custom-times (not org-display-custom-times))
  10400. (unless org-display-custom-times
  10401. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10402. (while (setq p (next-single-property-change p 'display))
  10403. (if (and (get-text-property p 'display)
  10404. (eq (get-text-property p 'face) 'org-date))
  10405. (remove-text-properties
  10406. p (setq p (next-single-property-change p 'display))
  10407. '(display t))))
  10408. (set-buffer-modified-p bmp)))
  10409. (if (featurep 'xemacs)
  10410. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10411. (org-restart-font-lock)
  10412. (setq org-table-may-need-update t)
  10413. (if org-display-custom-times
  10414. (message "Time stamps are overlayed with custom format")
  10415. (message "Time stamp overlays removed")))
  10416. (defun org-display-custom-time (beg end)
  10417. "Overlay modified time stamp format over timestamp between BEG and END."
  10418. (let* ((ts (buffer-substring beg end))
  10419. t1 w1 with-hm tf time str w2 (off 0))
  10420. (save-match-data
  10421. (setq t1 (org-parse-time-string ts t))
  10422. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10423. (setq off (- (match-end 0) (match-beginning 0)))))
  10424. (setq end (- end off))
  10425. (setq w1 (- end beg)
  10426. with-hm (and (nth 1 t1) (nth 2 t1))
  10427. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10428. time (org-fix-decoded-time t1)
  10429. str (org-add-props
  10430. (format-time-string
  10431. (substring tf 1 -1) (apply 'encode-time time))
  10432. nil 'mouse-face 'highlight)
  10433. w2 (length str))
  10434. (if (not (= w2 w1))
  10435. (add-text-properties (1+ beg) (+ 2 beg)
  10436. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10437. (if (featurep 'xemacs)
  10438. (progn
  10439. (put-text-property beg end 'invisible t)
  10440. (put-text-property beg end 'end-glyph (make-glyph str)))
  10441. (put-text-property beg end 'display str))))
  10442. (defun org-translate-time (string)
  10443. "Translate all timestamps in STRING to custom format.
  10444. But do this only if the variable `org-display-custom-times' is set."
  10445. (when org-display-custom-times
  10446. (save-match-data
  10447. (let* ((start 0)
  10448. (re org-ts-regexp-both)
  10449. t1 with-hm inactive tf time str beg end)
  10450. (while (setq start (string-match re string start))
  10451. (setq beg (match-beginning 0)
  10452. end (match-end 0)
  10453. t1 (save-match-data
  10454. (org-parse-time-string (substring string beg end) t))
  10455. with-hm (and (nth 1 t1) (nth 2 t1))
  10456. inactive (equal (substring string beg (1+ beg)) "[")
  10457. tf (funcall (if with-hm 'cdr 'car)
  10458. org-time-stamp-custom-formats)
  10459. time (org-fix-decoded-time t1)
  10460. str (format-time-string
  10461. (concat
  10462. (if inactive "[" "<") (substring tf 1 -1)
  10463. (if inactive "]" ">"))
  10464. (apply 'encode-time time))
  10465. string (replace-match str t t string)
  10466. start (+ start (length str)))))))
  10467. string)
  10468. (defun org-fix-decoded-time (time)
  10469. "Set 0 instead of nil for the first 6 elements of time.
  10470. Don't touch the rest."
  10471. (let ((n 0))
  10472. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10473. (defun org-days-to-time (timestamp-string)
  10474. "Difference between TIMESTAMP-STRING and now in days."
  10475. (- (time-to-days (org-time-string-to-time timestamp-string))
  10476. (time-to-days (current-time))))
  10477. (defun org-deadline-close (timestamp-string &optional ndays)
  10478. "Is the time in TIMESTAMP-STRING close to the current date?"
  10479. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10480. (and (< (org-days-to-time timestamp-string) ndays)
  10481. (not (org-entry-is-done-p))))
  10482. (defun org-get-wdays (ts)
  10483. "Get the deadline lead time appropriate for timestring TS."
  10484. (cond
  10485. ((<= org-deadline-warning-days 0)
  10486. ;; 0 or negative, enforce this value no matter what
  10487. (- org-deadline-warning-days))
  10488. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10489. ;; lead time is specified.
  10490. (floor (* (string-to-number (match-string 1 ts))
  10491. (cdr (assoc (match-string 2 ts)
  10492. '(("d" . 1) ("w" . 7)
  10493. ("m" . 30.4) ("y" . 365.25)))))))
  10494. ;; go for the default.
  10495. (t org-deadline-warning-days)))
  10496. (defun org-calendar-select-mouse (ev)
  10497. "Return to `org-read-date' with the date currently selected.
  10498. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10499. (interactive "e")
  10500. (mouse-set-point ev)
  10501. (when (calendar-cursor-to-date)
  10502. (let* ((date (calendar-cursor-to-date))
  10503. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10504. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10505. (if (active-minibuffer-window) (exit-minibuffer))))
  10506. (defun org-check-deadlines (ndays)
  10507. "Check if there are any deadlines due or past due.
  10508. A deadline is considered due if it happens within `org-deadline-warning-days'
  10509. days from today's date. If the deadline appears in an entry marked DONE,
  10510. it is not shown. The prefix arg NDAYS can be used to test that many
  10511. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10512. (interactive "P")
  10513. (let* ((org-warn-days
  10514. (cond
  10515. ((equal ndays '(4)) 100000)
  10516. (ndays (prefix-numeric-value ndays))
  10517. (t (abs org-deadline-warning-days))))
  10518. (case-fold-search nil)
  10519. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10520. (callback
  10521. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10522. (message "%d deadlines past-due or due within %d days"
  10523. (org-occur regexp nil callback)
  10524. org-warn-days)))
  10525. (defun org-check-before-date (date)
  10526. "Check if there are deadlines or scheduled entries before DATE."
  10527. (interactive (list (org-read-date)))
  10528. (let ((case-fold-search nil)
  10529. (regexp (concat "\\<\\(" org-deadline-string
  10530. "\\|" org-scheduled-string
  10531. "\\) *<\\([^>]+\\)>"))
  10532. (callback
  10533. (lambda () (time-less-p
  10534. (org-time-string-to-time (match-string 2))
  10535. (org-time-string-to-time date)))))
  10536. (message "%d entries before %s"
  10537. (org-occur regexp nil callback) date)))
  10538. (defun org-evaluate-time-range (&optional to-buffer)
  10539. "Evaluate a time range by computing the difference between start and end.
  10540. Normally the result is just printed in the echo area, but with prefix arg
  10541. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10542. If the time range is actually in a table, the result is inserted into the
  10543. next column.
  10544. For time difference computation, a year is assumed to be exactly 365
  10545. days in order to avoid rounding problems."
  10546. (interactive "P")
  10547. (or
  10548. (org-clock-update-time-maybe)
  10549. (save-excursion
  10550. (unless (org-at-date-range-p t)
  10551. (goto-char (point-at-bol))
  10552. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10553. (if (not (org-at-date-range-p t))
  10554. (error "Not at a time-stamp range, and none found in current line")))
  10555. (let* ((ts1 (match-string 1))
  10556. (ts2 (match-string 2))
  10557. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10558. (match-end (match-end 0))
  10559. (time1 (org-time-string-to-time ts1))
  10560. (time2 (org-time-string-to-time ts2))
  10561. (t1 (time-to-seconds time1))
  10562. (t2 (time-to-seconds time2))
  10563. (diff (abs (- t2 t1)))
  10564. (negative (< (- t2 t1) 0))
  10565. ;; (ys (floor (* 365 24 60 60)))
  10566. (ds (* 24 60 60))
  10567. (hs (* 60 60))
  10568. (fy "%dy %dd %02d:%02d")
  10569. (fy1 "%dy %dd")
  10570. (fd "%dd %02d:%02d")
  10571. (fd1 "%dd")
  10572. (fh "%02d:%02d")
  10573. y d h m align)
  10574. (if havetime
  10575. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10576. y 0
  10577. d (floor (/ diff ds)) diff (mod diff ds)
  10578. h (floor (/ diff hs)) diff (mod diff hs)
  10579. m (floor (/ diff 60)))
  10580. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10581. y 0
  10582. d (floor (+ (/ diff ds) 0.5))
  10583. h 0 m 0))
  10584. (if (not to-buffer)
  10585. (message "%s" (org-make-tdiff-string y d h m))
  10586. (if (org-at-table-p)
  10587. (progn
  10588. (goto-char match-end)
  10589. (setq align t)
  10590. (and (looking-at " *|") (goto-char (match-end 0))))
  10591. (goto-char match-end))
  10592. (if (looking-at
  10593. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10594. (replace-match ""))
  10595. (if negative (insert " -"))
  10596. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10597. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10598. (insert " " (format fh h m))))
  10599. (if align (org-table-align))
  10600. (message "Time difference inserted")))))
  10601. (defun org-make-tdiff-string (y d h m)
  10602. (let ((fmt "")
  10603. (l nil))
  10604. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10605. l (push y l)))
  10606. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10607. l (push d l)))
  10608. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10609. l (push h l)))
  10610. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10611. l (push m l)))
  10612. (apply 'format fmt (nreverse l))))
  10613. (defun org-time-string-to-time (s)
  10614. (apply 'encode-time (org-parse-time-string s)))
  10615. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10616. "Convert a time stamp to an absolute day number.
  10617. If there is a specifyer for a cyclic time stamp, get the closest date to
  10618. DAYNR.
  10619. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10620. (cond
  10621. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10622. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10623. daynr
  10624. (+ daynr 1000)))
  10625. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10626. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10627. (time-to-days (current-time))) (match-string 0 s)
  10628. prefer show-all))
  10629. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10630. (defun org-days-to-iso-week (days)
  10631. "Return the iso week number."
  10632. (require 'cal-iso)
  10633. (car (calendar-iso-from-absolute days)))
  10634. (defun org-small-year-to-year (year)
  10635. "Convert 2-digit years into 4-digit years.
  10636. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10637. The year 2000 cannot be abbreviated. Any year larger than 99
  10638. is returned unchanged."
  10639. (if (< year 38)
  10640. (setq year (+ 2000 year))
  10641. (if (< year 100)
  10642. (setq year (+ 1900 year))))
  10643. year)
  10644. (defun org-time-from-absolute (d)
  10645. "Return the time corresponding to date D.
  10646. D may be an absolute day number, or a calendar-type list (month day year)."
  10647. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10648. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10649. (defun org-calendar-holiday ()
  10650. "List of holidays, for Diary display in Org-mode."
  10651. (require 'holidays)
  10652. (let ((hl (funcall
  10653. (if (fboundp 'calendar-check-holidays)
  10654. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10655. (if hl (mapconcat 'identity hl "; "))))
  10656. (defun org-diary-sexp-entry (sexp entry date)
  10657. "Process a SEXP diary ENTRY for DATE."
  10658. (require 'diary-lib)
  10659. (let ((result (if calendar-debug-sexp
  10660. (let ((stack-trace-on-error t))
  10661. (eval (car (read-from-string sexp))))
  10662. (condition-case nil
  10663. (eval (car (read-from-string sexp)))
  10664. (error
  10665. (beep)
  10666. (message "Bad sexp at line %d in %s: %s"
  10667. (org-current-line)
  10668. (buffer-file-name) sexp)
  10669. (sleep-for 2))))))
  10670. (cond ((stringp result) result)
  10671. ((and (consp result)
  10672. (stringp (cdr result))) (cdr result))
  10673. (result entry)
  10674. (t nil))))
  10675. (defun org-diary-to-ical-string (frombuf)
  10676. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10677. This uses the icalendar.el library."
  10678. (let* ((tmpdir (if (featurep 'xemacs)
  10679. (temp-directory)
  10680. temporary-file-directory))
  10681. (tmpfile (make-temp-name
  10682. (expand-file-name "orgics" tmpdir)))
  10683. buf rtn b e)
  10684. (save-excursion
  10685. (set-buffer frombuf)
  10686. (icalendar-export-region (point-min) (point-max) tmpfile)
  10687. (setq buf (find-buffer-visiting tmpfile))
  10688. (set-buffer buf)
  10689. (goto-char (point-min))
  10690. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10691. (setq b (match-beginning 0)))
  10692. (goto-char (point-max))
  10693. (if (re-search-backward "^END:VEVENT" nil t)
  10694. (setq e (match-end 0)))
  10695. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10696. (kill-buffer buf)
  10697. (delete-file tmpfile)
  10698. rtn))
  10699. (defun org-closest-date (start current change prefer show-all)
  10700. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10701. When PREFER is `past' return a date that is either CURRENT or past.
  10702. When PREFER is `future', return a date that is either CURRENT or future.
  10703. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10704. ;; Make the proper lists from the dates
  10705. (catch 'exit
  10706. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10707. dn dw sday cday n1 n2 n0
  10708. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10709. (setq start (org-date-to-gregorian start)
  10710. current (org-date-to-gregorian
  10711. (if show-all
  10712. current
  10713. (time-to-days (current-time))))
  10714. sday (calendar-absolute-from-gregorian start)
  10715. cday (calendar-absolute-from-gregorian current))
  10716. (if (<= cday sday) (throw 'exit sday))
  10717. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10718. (setq dn (string-to-number (match-string 1 change))
  10719. dw (cdr (assoc (match-string 2 change) a1)))
  10720. (error "Invalid change specifyer: %s" change))
  10721. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10722. (cond
  10723. ((eq dw 'day)
  10724. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10725. n2 (+ n1 dn)))
  10726. ((eq dw 'year)
  10727. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10728. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10729. (setq date1 (list m d y1)
  10730. n1 (calendar-absolute-from-gregorian date1)
  10731. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10732. n2 (calendar-absolute-from-gregorian date2)))
  10733. ((eq dw 'month)
  10734. ;; approx number of month between the two dates
  10735. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10736. ;; How often does dn fit in there?
  10737. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10738. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10739. m (+ m nm)
  10740. ny (floor (/ m 12))
  10741. y (+ y ny)
  10742. m (- m (* ny 12)))
  10743. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10744. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10745. (setq m2 (+ m dn) y2 y)
  10746. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10747. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10748. (while (<= n2 cday)
  10749. (setq n1 n2 m m2 y y2)
  10750. (setq m2 (+ m dn) y2 y)
  10751. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10752. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10753. ;; Make sure n1 is the earlier date
  10754. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  10755. (if show-all
  10756. (cond
  10757. ((eq prefer 'past) n1)
  10758. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10759. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10760. (cond
  10761. ((eq prefer 'past) n1)
  10762. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10763. (t (if (= cday n1) n1 n2)))))))
  10764. (defun org-date-to-gregorian (date)
  10765. "Turn any specification of DATE into a gregorian date for the calendar."
  10766. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10767. ((and (listp date) (= (length date) 3)) date)
  10768. ((stringp date)
  10769. (setq date (org-parse-time-string date))
  10770. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10771. ((listp date)
  10772. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10773. (defun org-parse-time-string (s &optional nodefault)
  10774. "Parse the standard Org-mode time string.
  10775. This should be a lot faster than the normal `parse-time-string'.
  10776. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10777. hour and minute fields will be nil if not given."
  10778. (if (string-match org-ts-regexp0 s)
  10779. (list 0
  10780. (if (or (match-beginning 8) (not nodefault))
  10781. (string-to-number (or (match-string 8 s) "0")))
  10782. (if (or (match-beginning 7) (not nodefault))
  10783. (string-to-number (or (match-string 7 s) "0")))
  10784. (string-to-number (match-string 4 s))
  10785. (string-to-number (match-string 3 s))
  10786. (string-to-number (match-string 2 s))
  10787. nil nil nil)
  10788. (make-list 9 0)))
  10789. (defun org-timestamp-up (&optional arg)
  10790. "Increase the date item at the cursor by one.
  10791. If the cursor is on the year, change the year. If it is on the month or
  10792. the day, change that.
  10793. With prefix ARG, change by that many units."
  10794. (interactive "p")
  10795. (org-timestamp-change (prefix-numeric-value arg)))
  10796. (defun org-timestamp-down (&optional arg)
  10797. "Decrease the date item at the cursor by one.
  10798. If the cursor is on the year, change the year. If it is on the month or
  10799. the day, change that.
  10800. With prefix ARG, change by that many units."
  10801. (interactive "p")
  10802. (org-timestamp-change (- (prefix-numeric-value arg))))
  10803. (defun org-timestamp-up-day (&optional arg)
  10804. "Increase the date in the time stamp by one day.
  10805. With prefix ARG, change that many days."
  10806. (interactive "p")
  10807. (if (and (not (org-at-timestamp-p t))
  10808. (org-on-heading-p))
  10809. (org-todo 'up)
  10810. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10811. (defun org-timestamp-down-day (&optional arg)
  10812. "Decrease the date in the time stamp by one day.
  10813. With prefix ARG, change that many days."
  10814. (interactive "p")
  10815. (if (and (not (org-at-timestamp-p t))
  10816. (org-on-heading-p))
  10817. (org-todo 'down)
  10818. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10819. (defun org-at-timestamp-p (&optional inactive-ok)
  10820. "Determine if the cursor is in or at a timestamp."
  10821. (interactive)
  10822. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10823. (pos (point))
  10824. (ans (or (looking-at tsr)
  10825. (save-excursion
  10826. (skip-chars-backward "^[<\n\r\t")
  10827. (if (> (point) (point-min)) (backward-char 1))
  10828. (and (looking-at tsr)
  10829. (> (- (match-end 0) pos) -1))))))
  10830. (and ans
  10831. (boundp 'org-ts-what)
  10832. (setq org-ts-what
  10833. (cond
  10834. ((= pos (match-beginning 0)) 'bracket)
  10835. ((= pos (1- (match-end 0))) 'bracket)
  10836. ((org-pos-in-match-range pos 2) 'year)
  10837. ((org-pos-in-match-range pos 3) 'month)
  10838. ((org-pos-in-match-range pos 7) 'hour)
  10839. ((org-pos-in-match-range pos 8) 'minute)
  10840. ((or (org-pos-in-match-range pos 4)
  10841. (org-pos-in-match-range pos 5)) 'day)
  10842. ((and (> pos (or (match-end 8) (match-end 5)))
  10843. (< pos (match-end 0)))
  10844. (- pos (or (match-end 8) (match-end 5))))
  10845. (t 'day))))
  10846. ans))
  10847. (defun org-toggle-timestamp-type ()
  10848. "Toggle the type (<active> or [inactive]) of a time stamp."
  10849. (interactive)
  10850. (when (org-at-timestamp-p t)
  10851. (let ((beg (match-beginning 0)) (end (match-end 0))
  10852. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10853. (save-excursion
  10854. (goto-char beg)
  10855. (while (re-search-forward "[][<>]" end t)
  10856. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10857. t t)))
  10858. (message "Timestamp is now %sactive"
  10859. (if (equal (char-after beg) ?<) "" "in")))))
  10860. (defun org-timestamp-change (n &optional what)
  10861. "Change the date in the time stamp at point.
  10862. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10863. `year', `minute', `second'. If WHAT is not given, the cursor position
  10864. in the timestamp determines what will be changed."
  10865. (let ((pos (point))
  10866. with-hm inactive
  10867. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10868. org-ts-what
  10869. extra rem
  10870. ts time time0)
  10871. (if (not (org-at-timestamp-p t))
  10872. (error "Not at a timestamp"))
  10873. (if (and (not what) (eq org-ts-what 'bracket))
  10874. (org-toggle-timestamp-type)
  10875. (if (and (not what) (not (eq org-ts-what 'day))
  10876. org-display-custom-times
  10877. (get-text-property (point) 'display)
  10878. (not (get-text-property (1- (point)) 'display)))
  10879. (setq org-ts-what 'day))
  10880. (setq org-ts-what (or what org-ts-what)
  10881. inactive (= (char-after (match-beginning 0)) ?\[)
  10882. ts (match-string 0))
  10883. (replace-match "")
  10884. (if (string-match
  10885. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10886. ts)
  10887. (setq extra (match-string 1 ts)))
  10888. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10889. (setq with-hm t))
  10890. (setq time0 (org-parse-time-string ts))
  10891. (when (and (eq org-ts-what 'minute)
  10892. (eq current-prefix-arg nil))
  10893. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10894. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10895. (setcar (cdr time0) (+ (nth 1 time0)
  10896. (if (> n 0) (- rem) (- dm rem))))))
  10897. (setq time
  10898. (encode-time (or (car time0) 0)
  10899. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10900. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10901. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10902. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10903. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10904. (nthcdr 6 time0)))
  10905. (when (integerp org-ts-what)
  10906. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10907. (if (eq what 'calendar)
  10908. (let ((cal-date (org-get-date-from-calendar)))
  10909. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10910. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10911. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10912. (setcar time0 (or (car time0) 0))
  10913. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10914. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10915. (setq time (apply 'encode-time time0))))
  10916. (setq org-last-changed-timestamp
  10917. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10918. (org-clock-update-time-maybe)
  10919. (goto-char pos)
  10920. ;; Try to recenter the calendar window, if any
  10921. (if (and org-calendar-follow-timestamp-change
  10922. (get-buffer-window "*Calendar*" t)
  10923. (memq org-ts-what '(day month year)))
  10924. (org-recenter-calendar (time-to-days time))))))
  10925. (defun org-modify-ts-extra (s pos n dm)
  10926. "Change the different parts of the lead-time and repeat fields in timestamp."
  10927. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10928. ng h m new rem)
  10929. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10930. (cond
  10931. ((or (org-pos-in-match-range pos 2)
  10932. (org-pos-in-match-range pos 3))
  10933. (setq m (string-to-number (match-string 3 s))
  10934. h (string-to-number (match-string 2 s)))
  10935. (if (org-pos-in-match-range pos 2)
  10936. (setq h (+ h n))
  10937. (setq n (* dm (org-no-warnings (signum n))))
  10938. (when (not (= 0 (setq rem (% m dm))))
  10939. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10940. (setq m (+ m n)))
  10941. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10942. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10943. (setq h (min 24 (max 0 h)))
  10944. (setq ng 1 new (format "-%02d:%02d" h m)))
  10945. ((org-pos-in-match-range pos 6)
  10946. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10947. ((org-pos-in-match-range pos 5)
  10948. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10949. ((org-pos-in-match-range pos 9)
  10950. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10951. ((org-pos-in-match-range pos 8)
  10952. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10953. (when ng
  10954. (setq s (concat
  10955. (substring s 0 (match-beginning ng))
  10956. new
  10957. (substring s (match-end ng))))))
  10958. s))
  10959. (defun org-recenter-calendar (date)
  10960. "If the calendar is visible, recenter it to DATE."
  10961. (let* ((win (selected-window))
  10962. (cwin (get-buffer-window "*Calendar*" t))
  10963. (calendar-move-hook nil))
  10964. (when cwin
  10965. (select-window cwin)
  10966. (calendar-goto-date (if (listp date) date
  10967. (calendar-gregorian-from-absolute date)))
  10968. (select-window win))))
  10969. (defun org-goto-calendar (&optional arg)
  10970. "Go to the Emacs calendar at the current date.
  10971. If there is a time stamp in the current line, go to that date.
  10972. A prefix ARG can be used to force the current date."
  10973. (interactive "P")
  10974. (let ((tsr org-ts-regexp) diff
  10975. (calendar-move-hook nil)
  10976. (calendar-view-holidays-initially-flag nil)
  10977. (view-calendar-holidays-initially nil)
  10978. (calendar-view-diary-initially-flag nil)
  10979. (view-diary-entries-initially nil))
  10980. (if (or (org-at-timestamp-p)
  10981. (save-excursion
  10982. (beginning-of-line 1)
  10983. (looking-at (concat ".*" tsr))))
  10984. (let ((d1 (time-to-days (current-time)))
  10985. (d2 (time-to-days
  10986. (org-time-string-to-time (match-string 1)))))
  10987. (setq diff (- d2 d1))))
  10988. (calendar)
  10989. (calendar-goto-today)
  10990. (if (and diff (not arg)) (calendar-forward-day diff))))
  10991. (defun org-get-date-from-calendar ()
  10992. "Return a list (month day year) of date at point in calendar."
  10993. (with-current-buffer "*Calendar*"
  10994. (save-match-data
  10995. (calendar-cursor-to-date))))
  10996. (defun org-date-from-calendar ()
  10997. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10998. If there is already a time stamp at the cursor position, update it."
  10999. (interactive)
  11000. (if (org-at-timestamp-p t)
  11001. (org-timestamp-change 0 'calendar)
  11002. (let ((cal-date (org-get-date-from-calendar)))
  11003. (org-insert-time-stamp
  11004. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11005. (defun org-minutes-to-hh:mm-string (m)
  11006. "Compute H:MM from a number of minutes."
  11007. (let ((h (/ m 60)))
  11008. (setq m (- m (* 60 h)))
  11009. (format org-time-clocksum-format h m)))
  11010. (defun org-hh:mm-string-to-minutes (s)
  11011. "Convert a string H:MM to a number of minutes."
  11012. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11013. (+ (* (string-to-number (match-string 1 s)) 60)
  11014. (string-to-number (match-string 2 s)))
  11015. 0))
  11016. ;;;; Agenda files
  11017. ;;;###autoload
  11018. (defun org-iswitchb (&optional arg)
  11019. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11020. With a prefix argument, restrict available to files.
  11021. With two prefix arguments, restrict available buffers to agenda files.
  11022. Due to some yet unresolved reason, the global function
  11023. `iswitchb-mode' needs to be active for this function to work."
  11024. (interactive "P")
  11025. (require 'iswitchb)
  11026. (let ((enabled iswitchb-mode) blist)
  11027. (or enabled (iswitchb-mode 1))
  11028. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11029. ((equal arg '(16)) (org-buffer-list 'agenda))
  11030. (t (org-buffer-list))))
  11031. (unwind-protect
  11032. (let ((iswitchb-make-buflist-hook
  11033. (lambda ()
  11034. (setq iswitchb-temp-buflist
  11035. (mapcar 'buffer-name blist)))))
  11036. (switch-to-buffer
  11037. (iswitchb-read-buffer
  11038. "Switch-to: " nil t))
  11039. (or enabled (iswitchb-mode -1))))))
  11040. ;;;###autoload
  11041. (defun org-ido-switchb (&optional arg)
  11042. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11043. With a prefix argument, restrict available to files.
  11044. With two prefix arguments, restrict available buffers to agenda files."
  11045. (interactive "P")
  11046. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11047. ((equal arg '(16)) (org-buffer-list 'agenda))
  11048. (t (org-buffer-list)))))
  11049. (switch-to-buffer
  11050. (org-ido-completing-read "Org buffer: "
  11051. (mapcar 'buffer-name blist)
  11052. nil t))))
  11053. (defun org-buffer-list (&optional predicate exclude-tmp)
  11054. "Return a list of Org buffers.
  11055. PREDICATE can be `export', `files' or `agenda'.
  11056. export restrict the list to Export buffers.
  11057. files restrict the list to buffers visiting Org files.
  11058. agenda restrict the list to buffers visiting agenda files.
  11059. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11060. (let* ((bfn nil)
  11061. (agenda-files (and (eq predicate 'agenda)
  11062. (mapcar 'file-truename (org-agenda-files t))))
  11063. (filter
  11064. (cond
  11065. ((eq predicate 'files)
  11066. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11067. ((eq predicate 'export)
  11068. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11069. ((eq predicate 'agenda)
  11070. (lambda (b)
  11071. (with-current-buffer b
  11072. (and (eq major-mode 'org-mode)
  11073. (setq bfn (buffer-file-name b))
  11074. (member (file-truename bfn) agenda-files)))))
  11075. (t (lambda (b) (with-current-buffer b
  11076. (or (eq major-mode 'org-mode)
  11077. (string-match "\*Org .*Export"
  11078. (buffer-name b)))))))))
  11079. (delq nil
  11080. (mapcar
  11081. (lambda(b)
  11082. (if (and (funcall filter b)
  11083. (or (not exclude-tmp)
  11084. (not (string-match "tmp" (buffer-name b)))))
  11085. b
  11086. nil))
  11087. (buffer-list)))))
  11088. (defun org-agenda-files (&optional unrestricted archives)
  11089. "Get the list of agenda files.
  11090. Optional UNRESTRICTED means return the full list even if a restriction
  11091. is currently in place.
  11092. When ARCHIVES is t, include all archive files hat are really being
  11093. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11094. `org-agenda-archives-mode' is t."
  11095. (let ((files
  11096. (cond
  11097. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11098. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11099. ((listp org-agenda-files) org-agenda-files)
  11100. (t (error "Invalid value of `org-agenda-files'")))))
  11101. (setq files (apply 'append
  11102. (mapcar (lambda (f)
  11103. (if (file-directory-p f)
  11104. (directory-files
  11105. f t org-agenda-file-regexp)
  11106. (list f)))
  11107. files)))
  11108. (when org-agenda-skip-unavailable-files
  11109. (setq files (delq nil
  11110. (mapcar (function
  11111. (lambda (file)
  11112. (and (file-readable-p file) file)))
  11113. files))))
  11114. (when (or (eq archives t)
  11115. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11116. (setq files (org-add-archive-files files)))
  11117. files))
  11118. (defun org-edit-agenda-file-list ()
  11119. "Edit the list of agenda files.
  11120. Depending on setup, this either uses customize to edit the variable
  11121. `org-agenda-files', or it visits the file that is holding the list. In the
  11122. latter case, the buffer is set up in a way that saving it automatically kills
  11123. the buffer and restores the previous window configuration."
  11124. (interactive)
  11125. (if (stringp org-agenda-files)
  11126. (let ((cw (current-window-configuration)))
  11127. (find-file org-agenda-files)
  11128. (org-set-local 'org-window-configuration cw)
  11129. (org-add-hook 'after-save-hook
  11130. (lambda ()
  11131. (set-window-configuration
  11132. (prog1 org-window-configuration
  11133. (kill-buffer (current-buffer))))
  11134. (org-install-agenda-files-menu)
  11135. (message "New agenda file list installed"))
  11136. nil 'local)
  11137. (message "%s" (substitute-command-keys
  11138. "Edit list and finish with \\[save-buffer]")))
  11139. (customize-variable 'org-agenda-files)))
  11140. (defun org-store-new-agenda-file-list (list)
  11141. "Set new value for the agenda file list and save it correctly."
  11142. (if (stringp org-agenda-files)
  11143. (let ((f org-agenda-files) b)
  11144. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11145. (with-temp-file f
  11146. (insert (mapconcat 'identity list "\n") "\n")))
  11147. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11148. (setq org-agenda-files list)
  11149. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11150. (defun org-read-agenda-file-list ()
  11151. "Read the list of agenda files from a file."
  11152. (when (file-directory-p org-agenda-files)
  11153. (error "`org-agenda-files' cannot be a single directory"))
  11154. (when (stringp org-agenda-files)
  11155. (with-temp-buffer
  11156. (insert-file-contents org-agenda-files)
  11157. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11158. ;;;###autoload
  11159. (defun org-cycle-agenda-files ()
  11160. "Cycle through the files in `org-agenda-files'.
  11161. If the current buffer visits an agenda file, find the next one in the list.
  11162. If the current buffer does not, find the first agenda file."
  11163. (interactive)
  11164. (let* ((fs (org-agenda-files t))
  11165. (files (append fs (list (car fs))))
  11166. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11167. file)
  11168. (unless files (error "No agenda files"))
  11169. (catch 'exit
  11170. (while (setq file (pop files))
  11171. (if (equal (file-truename file) tcf)
  11172. (when (car files)
  11173. (find-file (car files))
  11174. (throw 'exit t))))
  11175. (find-file (car fs)))
  11176. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11177. (defun org-agenda-file-to-front (&optional to-end)
  11178. "Move/add the current file to the top of the agenda file list.
  11179. If the file is not present in the list, it is added to the front. If it is
  11180. present, it is moved there. With optional argument TO-END, add/move to the
  11181. end of the list."
  11182. (interactive "P")
  11183. (let ((org-agenda-skip-unavailable-files nil)
  11184. (file-alist (mapcar (lambda (x)
  11185. (cons (file-truename x) x))
  11186. (org-agenda-files t)))
  11187. (ctf (file-truename buffer-file-name))
  11188. x had)
  11189. (setq x (assoc ctf file-alist) had x)
  11190. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11191. (if to-end
  11192. (setq file-alist (append (delq x file-alist) (list x)))
  11193. (setq file-alist (cons x (delq x file-alist))))
  11194. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11195. (org-install-agenda-files-menu)
  11196. (message "File %s to %s of agenda file list"
  11197. (if had "moved" "added") (if to-end "end" "front"))))
  11198. (defun org-remove-file (&optional file)
  11199. "Remove current file from the list of files in variable `org-agenda-files'.
  11200. These are the files which are being checked for agenda entries.
  11201. Optional argument FILE means, use this file instead of the current."
  11202. (interactive)
  11203. (let* ((org-agenda-skip-unavailable-files nil)
  11204. (file (or file buffer-file-name))
  11205. (true-file (file-truename file))
  11206. (afile (abbreviate-file-name file))
  11207. (files (delq nil (mapcar
  11208. (lambda (x)
  11209. (if (equal true-file
  11210. (file-truename x))
  11211. nil x))
  11212. (org-agenda-files t)))))
  11213. (if (not (= (length files) (length (org-agenda-files t))))
  11214. (progn
  11215. (org-store-new-agenda-file-list files)
  11216. (org-install-agenda-files-menu)
  11217. (message "Removed file: %s" afile))
  11218. (message "File was not in list: %s (not removed)" afile))))
  11219. (defun org-file-menu-entry (file)
  11220. (vector file (list 'find-file file) t))
  11221. (defun org-check-agenda-file (file)
  11222. "Make sure FILE exists. If not, ask user what to do."
  11223. (when (not (file-exists-p file))
  11224. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11225. (abbreviate-file-name file))
  11226. (let ((r (downcase (read-char-exclusive))))
  11227. (cond
  11228. ((equal r ?r)
  11229. (org-remove-file file)
  11230. (throw 'nextfile t))
  11231. (t (error "Abort"))))))
  11232. (defun org-get-agenda-file-buffer (file)
  11233. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11234. it to the list of buffers which might be released later."
  11235. (let ((buf (org-find-base-buffer-visiting file)))
  11236. (if buf
  11237. buf ; just return it
  11238. ;; Make a new buffer and remember it
  11239. (setq buf (find-file-noselect file))
  11240. (if buf (push buf org-agenda-new-buffers))
  11241. buf)))
  11242. (defun org-release-buffers (blist)
  11243. "Release all buffers in list, asking the user for confirmation when needed.
  11244. When a buffer is unmodified, it is just killed. When modified, it is saved
  11245. \(if the user agrees) and then killed."
  11246. (let (buf file)
  11247. (while (setq buf (pop blist))
  11248. (setq file (buffer-file-name buf))
  11249. (when (and (buffer-modified-p buf)
  11250. file
  11251. (y-or-n-p (format "Save file %s? " file)))
  11252. (with-current-buffer buf (save-buffer)))
  11253. (kill-buffer buf))))
  11254. (defun org-prepare-agenda-buffers (files)
  11255. "Create buffers for all agenda files, protect archived trees and comments."
  11256. (interactive)
  11257. (let ((pa '(:org-archived t))
  11258. (pc '(:org-comment t))
  11259. (pall '(:org-archived t :org-comment t))
  11260. (inhibit-read-only t)
  11261. (rea (concat ":" org-archive-tag ":"))
  11262. bmp file re)
  11263. (save-excursion
  11264. (save-restriction
  11265. (while (setq file (pop files))
  11266. (if (bufferp file)
  11267. (set-buffer file)
  11268. (org-check-agenda-file file)
  11269. (set-buffer (org-get-agenda-file-buffer file)))
  11270. (widen)
  11271. (setq bmp (buffer-modified-p))
  11272. (org-refresh-category-properties)
  11273. (setq org-todo-keywords-for-agenda
  11274. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11275. (setq org-done-keywords-for-agenda
  11276. (append org-done-keywords-for-agenda org-done-keywords))
  11277. (setq org-todo-keyword-alist-for-agenda
  11278. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11279. (setq org-tag-alist-for-agenda
  11280. (append org-tag-alist-for-agenda org-tag-alist))
  11281. (save-excursion
  11282. (remove-text-properties (point-min) (point-max) pall)
  11283. (when org-agenda-skip-archived-trees
  11284. (goto-char (point-min))
  11285. (while (re-search-forward rea nil t)
  11286. (if (org-on-heading-p t)
  11287. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11288. (goto-char (point-min))
  11289. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11290. (while (re-search-forward re nil t)
  11291. (add-text-properties
  11292. (match-beginning 0) (org-end-of-subtree t) pc)))
  11293. (set-buffer-modified-p bmp))))
  11294. (setq org-todo-keyword-alist-for-agenda
  11295. (org-uniquify org-todo-keyword-alist-for-agenda)
  11296. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11297. ;;;; Embedded LaTeX
  11298. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11299. "Keymap for the minor `org-cdlatex-mode'.")
  11300. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11301. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11302. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11303. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11304. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11305. (defvar org-cdlatex-texmathp-advice-is-done nil
  11306. "Flag remembering if we have applied the advice to texmathp already.")
  11307. (define-minor-mode org-cdlatex-mode
  11308. "Toggle the minor `org-cdlatex-mode'.
  11309. This mode supports entering LaTeX environment and math in LaTeX fragments
  11310. in Org-mode.
  11311. \\{org-cdlatex-mode-map}"
  11312. nil " OCDL" nil
  11313. (when org-cdlatex-mode (require 'cdlatex))
  11314. (unless org-cdlatex-texmathp-advice-is-done
  11315. (setq org-cdlatex-texmathp-advice-is-done t)
  11316. (defadvice texmathp (around org-math-always-on activate)
  11317. "Always return t in org-mode buffers.
  11318. This is because we want to insert math symbols without dollars even outside
  11319. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11320. an embedded LaTeX fragment, let texmathp do its job.
  11321. \\[org-cdlatex-mode-map]"
  11322. (interactive)
  11323. (let (p)
  11324. (cond
  11325. ((not (org-mode-p)) ad-do-it)
  11326. ((eq this-command 'cdlatex-math-symbol)
  11327. (setq ad-return-value t
  11328. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11329. (t
  11330. (let ((p (org-inside-LaTeX-fragment-p)))
  11331. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11332. (setq ad-return-value t
  11333. texmathp-why '("Org-mode embedded math" . 0))
  11334. (if p ad-do-it)))))))))
  11335. (defun turn-on-org-cdlatex ()
  11336. "Unconditionally turn on `org-cdlatex-mode'."
  11337. (org-cdlatex-mode 1))
  11338. (defun org-inside-LaTeX-fragment-p ()
  11339. "Test if point is inside a LaTeX fragment.
  11340. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11341. sequence appearing also before point.
  11342. Even though the matchers for math are configurable, this function assumes
  11343. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11344. delimiters are skipped when they have been removed by customization.
  11345. The return value is nil, or a cons cell with the delimiter and
  11346. and the position of this delimiter.
  11347. This function does a reasonably good job, but can locally be fooled by
  11348. for example currency specifications. For example it will assume being in
  11349. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11350. fragments that are properly closed, but during editing, we have to live
  11351. with the uncertainty caused by missing closing delimiters. This function
  11352. looks only before point, not after."
  11353. (catch 'exit
  11354. (let ((pos (point))
  11355. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11356. (lim (progn
  11357. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11358. (point)))
  11359. dd-on str (start 0) m re)
  11360. (goto-char pos)
  11361. (when dodollar
  11362. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11363. re (nth 1 (assoc "$" org-latex-regexps)))
  11364. (while (string-match re str start)
  11365. (cond
  11366. ((= (match-end 0) (length str))
  11367. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11368. ((= (match-end 0) (- (length str) 5))
  11369. (throw 'exit nil))
  11370. (t (setq start (match-end 0))))))
  11371. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11372. (goto-char pos)
  11373. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11374. (and (match-beginning 2) (throw 'exit nil))
  11375. ;; count $$
  11376. (while (re-search-backward "\\$\\$" lim t)
  11377. (setq dd-on (not dd-on)))
  11378. (goto-char pos)
  11379. (if dd-on (cons "$$" m))))))
  11380. (defun org-try-cdlatex-tab ()
  11381. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11382. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11383. - inside a LaTeX fragment, or
  11384. - after the first word in a line, where an abbreviation expansion could
  11385. insert a LaTeX environment."
  11386. (when org-cdlatex-mode
  11387. (cond
  11388. ((save-excursion
  11389. (skip-chars-backward "a-zA-Z0-9*")
  11390. (skip-chars-backward " \t")
  11391. (bolp))
  11392. (cdlatex-tab) t)
  11393. ((org-inside-LaTeX-fragment-p)
  11394. (cdlatex-tab) t)
  11395. (t nil))))
  11396. (defun org-cdlatex-underscore-caret (&optional arg)
  11397. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11398. Revert to the normal definition outside of these fragments."
  11399. (interactive "P")
  11400. (if (org-inside-LaTeX-fragment-p)
  11401. (call-interactively 'cdlatex-sub-superscript)
  11402. (let (org-cdlatex-mode)
  11403. (call-interactively (key-binding (vector last-input-event))))))
  11404. (defun org-cdlatex-math-modify (&optional arg)
  11405. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11406. Revert to the normal definition outside of these fragments."
  11407. (interactive "P")
  11408. (if (org-inside-LaTeX-fragment-p)
  11409. (call-interactively 'cdlatex-math-modify)
  11410. (let (org-cdlatex-mode)
  11411. (call-interactively (key-binding (vector last-input-event))))))
  11412. (defvar org-latex-fragment-image-overlays nil
  11413. "List of overlays carrying the images of latex fragments.")
  11414. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11415. (defun org-remove-latex-fragment-image-overlays ()
  11416. "Remove all overlays with LaTeX fragment images in current buffer."
  11417. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11418. (setq org-latex-fragment-image-overlays nil))
  11419. (defun org-preview-latex-fragment (&optional subtree)
  11420. "Preview the LaTeX fragment at point, or all locally or globally.
  11421. If the cursor is in a LaTeX fragment, create the image and overlay
  11422. it over the source code. If there is no fragment at point, display
  11423. all fragments in the current text, from one headline to the next. With
  11424. prefix SUBTREE, display all fragments in the current subtree. With a
  11425. double prefix `C-u C-u', or when the cursor is before the first headline,
  11426. display all fragments in the buffer.
  11427. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11428. (interactive "P")
  11429. (org-remove-latex-fragment-image-overlays)
  11430. (save-excursion
  11431. (save-restriction
  11432. (let (beg end at msg)
  11433. (cond
  11434. ((or (equal subtree '(16))
  11435. (not (save-excursion
  11436. (re-search-backward (concat "^" outline-regexp) nil t))))
  11437. (setq beg (point-min) end (point-max)
  11438. msg "Creating images for buffer...%s"))
  11439. ((equal subtree '(4))
  11440. (org-back-to-heading)
  11441. (setq beg (point) end (org-end-of-subtree t)
  11442. msg "Creating images for subtree...%s"))
  11443. (t
  11444. (if (setq at (org-inside-LaTeX-fragment-p))
  11445. (goto-char (max (point-min) (- (cdr at) 2)))
  11446. (org-back-to-heading))
  11447. (setq beg (point) end (progn (outline-next-heading) (point))
  11448. msg (if at "Creating image...%s"
  11449. "Creating images for entry...%s"))))
  11450. (message msg "")
  11451. (narrow-to-region beg end)
  11452. (goto-char beg)
  11453. (org-format-latex
  11454. (concat "ltxpng/" (file-name-sans-extension
  11455. (file-name-nondirectory
  11456. buffer-file-name)))
  11457. default-directory 'overlays msg at 'forbuffer)
  11458. (message msg "done. Use `C-c C-c' to remove images.")))))
  11459. (defvar org-latex-regexps
  11460. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11461. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11462. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11463. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11464. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11465. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11466. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11467. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11468. "Regular expressions for matching embedded LaTeX.")
  11469. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11470. "Replace LaTeX fragments with links to an image, and produce images."
  11471. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11472. (let* ((prefixnodir (file-name-nondirectory prefix))
  11473. (absprefix (expand-file-name prefix dir))
  11474. (todir (file-name-directory absprefix))
  11475. (opt org-format-latex-options)
  11476. (matchers (plist-get opt :matchers))
  11477. (re-list org-latex-regexps)
  11478. (cnt 0) txt link beg end re e checkdir
  11479. m n block linkfile movefile ov)
  11480. ;; Check if there are old images files with this prefix, and remove them
  11481. (when (file-directory-p todir)
  11482. (mapc 'delete-file
  11483. (directory-files
  11484. todir 'full
  11485. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11486. ;; Check the different regular expressions
  11487. (while (setq e (pop re-list))
  11488. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11489. block (if (nth 3 e) "\n\n" ""))
  11490. (when (member m matchers)
  11491. (goto-char (point-min))
  11492. (while (re-search-forward re nil t)
  11493. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11494. (setq txt (match-string n)
  11495. beg (match-beginning n) end (match-end n)
  11496. cnt (1+ cnt)
  11497. linkfile (format "%s_%04d.png" prefix cnt)
  11498. movefile (format "%s_%04d.png" absprefix cnt)
  11499. link (concat block "[[file:" linkfile "]]" block))
  11500. (if msg (message msg cnt))
  11501. (goto-char beg)
  11502. (unless checkdir ; make sure the directory exists
  11503. (setq checkdir t)
  11504. (or (file-directory-p todir) (make-directory todir)))
  11505. (org-create-formula-image
  11506. txt movefile opt forbuffer)
  11507. (if overlays
  11508. (progn
  11509. (setq ov (org-make-overlay beg end))
  11510. (if (featurep 'xemacs)
  11511. (progn
  11512. (org-overlay-put ov 'invisible t)
  11513. (org-overlay-put
  11514. ov 'end-glyph
  11515. (make-glyph (vector 'png :file movefile))))
  11516. (org-overlay-put
  11517. ov 'display
  11518. (list 'image :type 'png :file movefile :ascent 'center)))
  11519. (push ov org-latex-fragment-image-overlays)
  11520. (goto-char end))
  11521. (delete-region beg end)
  11522. (insert link))))))))
  11523. ;; This function borrows from Ganesh Swami's latex2png.el
  11524. (defun org-create-formula-image (string tofile options buffer)
  11525. (let* ((tmpdir (if (featurep 'xemacs)
  11526. (temp-directory)
  11527. temporary-file-directory))
  11528. (texfilebase (make-temp-name
  11529. (expand-file-name "orgtex" tmpdir)))
  11530. (texfile (concat texfilebase ".tex"))
  11531. (dvifile (concat texfilebase ".dvi"))
  11532. (pngfile (concat texfilebase ".png"))
  11533. (fnh (if (featurep 'xemacs)
  11534. (font-height (get-face-font 'default))
  11535. (face-attribute 'default :height nil)))
  11536. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11537. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11538. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11539. "Black"))
  11540. (bg (or (plist-get options (if buffer :background :html-background))
  11541. "Transparent")))
  11542. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11543. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11544. (with-temp-file texfile
  11545. (insert org-format-latex-header
  11546. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11547. (let ((dir default-directory))
  11548. (condition-case nil
  11549. (progn
  11550. (cd tmpdir)
  11551. (call-process "latex" nil nil nil texfile))
  11552. (error nil))
  11553. (cd dir))
  11554. (if (not (file-exists-p dvifile))
  11555. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11556. (condition-case nil
  11557. (call-process "dvipng" nil nil nil
  11558. "-E" "-fg" fg "-bg" bg
  11559. "-D" dpi
  11560. ;;"-x" scale "-y" scale
  11561. "-T" "tight"
  11562. "-o" pngfile
  11563. dvifile)
  11564. (error nil))
  11565. (if (not (file-exists-p pngfile))
  11566. (progn (message "Failed to create png file from %s" texfile) nil)
  11567. ;; Use the requested file name and clean up
  11568. (copy-file pngfile tofile 'replace)
  11569. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11570. (delete-file (concat texfilebase e)))
  11571. pngfile))))
  11572. (defun org-dvipng-color (attr)
  11573. "Return an rgb color specification for dvipng."
  11574. (apply 'format "rgb %s %s %s"
  11575. (mapcar 'org-normalize-color
  11576. (color-values (face-attribute 'default attr nil)))))
  11577. (defun org-normalize-color (value)
  11578. "Return string to be used as color value for an RGB component."
  11579. (format "%g" (/ value 65535.0)))
  11580. ;;;; Key bindings
  11581. ;; Make `C-c C-x' a prefix key
  11582. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11583. ;; TAB key with modifiers
  11584. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11585. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11586. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11587. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11588. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11589. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11590. ;; The following line is necessary under Suse GNU/Linux
  11591. (unless (featurep 'xemacs)
  11592. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11593. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11594. (define-key org-mode-map [backtab] 'org-shifttab)
  11595. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11596. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11597. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11598. ;; Cursor keys with modifiers
  11599. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11600. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11601. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11602. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11603. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11604. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11605. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11606. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11607. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11608. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11609. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11610. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11611. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11612. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11613. ;;; Extra keys for tty access.
  11614. ;; We only set them when really needed because otherwise the
  11615. ;; menus don't show the simple keys
  11616. (when (or org-use-extra-keys
  11617. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11618. (not window-system))
  11619. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11620. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11621. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11622. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11623. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11624. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11625. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11626. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11627. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11628. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11629. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11630. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11631. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11632. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11633. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11634. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11635. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11636. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11637. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11638. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11639. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11640. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11641. ;; All the other keys
  11642. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11643. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11644. (if (boundp 'narrow-map)
  11645. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11646. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11647. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11648. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11649. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11650. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11651. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11652. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11653. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11654. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11655. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11656. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11657. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11658. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11659. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11660. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11661. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11662. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11663. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11664. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11665. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11666. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11667. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11668. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11669. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11670. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11671. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11672. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11673. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11674. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11675. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11676. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11677. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11678. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11679. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11680. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11681. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11682. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11683. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11684. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11685. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11686. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11687. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11688. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11689. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11690. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11691. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11692. (org-defkey org-mode-map "\C-m" 'org-return)
  11693. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11694. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11695. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11696. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11697. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11698. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11699. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11700. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11701. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11702. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11703. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11704. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11705. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11706. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11707. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11708. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11709. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11710. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11711. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11712. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11713. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11714. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11715. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11716. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11717. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11718. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11719. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11720. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11721. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11722. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11723. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11724. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11725. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11726. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11727. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11728. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11729. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  11730. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11731. (when (featurep 'xemacs)
  11732. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11733. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11734. (defun org-self-insert-command (N)
  11735. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11736. If the cursor is in a table looking at whitespace, the whitespace is
  11737. overwritten, and the table is not marked as requiring realignment."
  11738. (interactive "p")
  11739. (if (and (org-table-p)
  11740. (progn
  11741. ;; check if we blank the field, and if that triggers align
  11742. (and (featurep 'org-table) org-table-auto-blank-field
  11743. (member last-command
  11744. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11745. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11746. ;; got extra space, this field does not determine column width
  11747. (let (org-table-may-need-update) (org-table-blank-field))
  11748. ;; no extra space, this field may determine column width
  11749. (org-table-blank-field)))
  11750. t)
  11751. (eq N 1)
  11752. (looking-at "[^|\n]* |"))
  11753. (let (org-table-may-need-update)
  11754. (goto-char (1- (match-end 0)))
  11755. (delete-backward-char 1)
  11756. (goto-char (match-beginning 0))
  11757. (self-insert-command N))
  11758. (setq org-table-may-need-update t)
  11759. (self-insert-command N)
  11760. (org-fix-tags-on-the-fly)))
  11761. (defun org-fix-tags-on-the-fly ()
  11762. (when (and (equal (char-after (point-at-bol)) ?*)
  11763. (org-on-heading-p))
  11764. (org-align-tags-here org-tags-column)))
  11765. (defun org-delete-backward-char (N)
  11766. "Like `delete-backward-char', insert whitespace at field end in tables.
  11767. When deleting backwards, in tables this function will insert whitespace in
  11768. front of the next \"|\" separator, to keep the table aligned. The table will
  11769. still be marked for re-alignment if the field did fill the entire column,
  11770. because, in this case the deletion might narrow the column."
  11771. (interactive "p")
  11772. (if (and (org-table-p)
  11773. (eq N 1)
  11774. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11775. (looking-at ".*?|"))
  11776. (let ((pos (point))
  11777. (noalign (looking-at "[^|\n\r]* |"))
  11778. (c org-table-may-need-update))
  11779. (backward-delete-char N)
  11780. (skip-chars-forward "^|")
  11781. (insert " ")
  11782. (goto-char (1- pos))
  11783. ;; noalign: if there were two spaces at the end, this field
  11784. ;; does not determine the width of the column.
  11785. (if noalign (setq org-table-may-need-update c)))
  11786. (backward-delete-char N)
  11787. (org-fix-tags-on-the-fly)))
  11788. (defun org-delete-char (N)
  11789. "Like `delete-char', but insert whitespace at field end in tables.
  11790. When deleting characters, in tables this function will insert whitespace in
  11791. front of the next \"|\" separator, to keep the table aligned. The table will
  11792. still be marked for re-alignment if the field did fill the entire column,
  11793. because, in this case the deletion might narrow the column."
  11794. (interactive "p")
  11795. (if (and (org-table-p)
  11796. (not (bolp))
  11797. (not (= (char-after) ?|))
  11798. (eq N 1))
  11799. (if (looking-at ".*?|")
  11800. (let ((pos (point))
  11801. (noalign (looking-at "[^|\n\r]* |"))
  11802. (c org-table-may-need-update))
  11803. (replace-match (concat
  11804. (substring (match-string 0) 1 -1)
  11805. " |"))
  11806. (goto-char pos)
  11807. ;; noalign: if there were two spaces at the end, this field
  11808. ;; does not determine the width of the column.
  11809. (if noalign (setq org-table-may-need-update c)))
  11810. (delete-char N))
  11811. (delete-char N)
  11812. (org-fix-tags-on-the-fly)))
  11813. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11814. (put 'org-self-insert-command 'delete-selection t)
  11815. (put 'orgtbl-self-insert-command 'delete-selection t)
  11816. (put 'org-delete-char 'delete-selection 'supersede)
  11817. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11818. (put 'org-yank 'delete-selection 'yank)
  11819. ;; Make `flyspell-mode' delay after some commands
  11820. (put 'org-self-insert-command 'flyspell-delayed t)
  11821. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11822. (put 'org-delete-char 'flyspell-delayed t)
  11823. (put 'org-delete-backward-char 'flyspell-delayed t)
  11824. ;; Make pabbrev-mode expand after org-mode commands
  11825. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11826. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11827. ;; How to do this: Measure non-white length of current string
  11828. ;; If equal to column width, we should realign.
  11829. (defun org-remap (map &rest commands)
  11830. "In MAP, remap the functions given in COMMANDS.
  11831. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11832. (let (new old)
  11833. (while commands
  11834. (setq old (pop commands) new (pop commands))
  11835. (if (fboundp 'command-remapping)
  11836. (org-defkey map (vector 'remap old) new)
  11837. (substitute-key-definition old new map global-map)))))
  11838. (when (eq org-enable-table-editor 'optimized)
  11839. ;; If the user wants maximum table support, we need to hijack
  11840. ;; some standard editing functions
  11841. (org-remap org-mode-map
  11842. 'self-insert-command 'org-self-insert-command
  11843. 'delete-char 'org-delete-char
  11844. 'delete-backward-char 'org-delete-backward-char)
  11845. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11846. (defun org-shiftcursor-error ()
  11847. "Throw an error, a modified cursor command was applied in wrong context."
  11848. (error "This command is active in special context like tables, headlines or items"))
  11849. (defun org-shiftselect-error ()
  11850. "Throw an error because Shift-Cursor command was applied in wrong context."
  11851. (if (and (boundp 'shift-select-mode) shift-select-mode)
  11852. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  11853. (error "This command works only in special context like headlines or timestamps.")))
  11854. (defun org-call-for-shift-select (cmd)
  11855. (let ((this-command-keys-shift-translated t))
  11856. (call-interactively cmd)))
  11857. (defun org-shifttab (&optional arg)
  11858. "Global visibility cycling or move to previous table field.
  11859. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11860. on context.
  11861. See the individual commands for more information."
  11862. (interactive "P")
  11863. (cond
  11864. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11865. ((integerp arg)
  11866. (message "Content view to level: %d" arg)
  11867. (org-content (prefix-numeric-value arg))
  11868. (setq org-cycle-global-status 'overview))
  11869. (t (call-interactively 'org-global-cycle))))
  11870. (defun org-shiftmetaleft ()
  11871. "Promote subtree or delete table column.
  11872. Calls `org-promote-subtree', `org-outdent-item',
  11873. or `org-table-delete-column', depending on context.
  11874. See the individual commands for more information."
  11875. (interactive)
  11876. (cond
  11877. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11878. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11879. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11880. (t (org-shiftcursor-error))))
  11881. (defun org-shiftmetaright ()
  11882. "Demote subtree or insert table column.
  11883. Calls `org-demote-subtree', `org-indent-item',
  11884. or `org-table-insert-column', depending on context.
  11885. See the individual commands for more information."
  11886. (interactive)
  11887. (cond
  11888. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11889. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11890. ((org-at-item-p) (call-interactively 'org-indent-item))
  11891. (t (org-shiftcursor-error))))
  11892. (defun org-shiftmetaup (&optional arg)
  11893. "Move subtree up or kill table row.
  11894. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11895. `org-move-item-up' depending on context. See the individual commands
  11896. for more information."
  11897. (interactive "P")
  11898. (cond
  11899. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11900. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11901. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11902. (t (org-shiftcursor-error))))
  11903. (defun org-shiftmetadown (&optional arg)
  11904. "Move subtree down or insert table row.
  11905. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11906. `org-move-item-down', depending on context. See the individual
  11907. commands for more information."
  11908. (interactive "P")
  11909. (cond
  11910. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11911. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11912. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11913. (t (org-shiftcursor-error))))
  11914. (defun org-metaleft (&optional arg)
  11915. "Promote heading or move table column to left.
  11916. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11917. With no specific context, calls the Emacs default `backward-word'.
  11918. See the individual commands for more information."
  11919. (interactive "P")
  11920. (cond
  11921. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11922. ((or (org-on-heading-p) (org-region-active-p))
  11923. (call-interactively 'org-do-promote))
  11924. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11925. (t (call-interactively 'backward-word))))
  11926. (defun org-metaright (&optional arg)
  11927. "Demote subtree or move table column to right.
  11928. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11929. With no specific context, calls the Emacs default `forward-word'.
  11930. See the individual commands for more information."
  11931. (interactive "P")
  11932. (cond
  11933. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11934. ((or (org-on-heading-p) (org-region-active-p))
  11935. (call-interactively 'org-do-demote))
  11936. ((org-at-item-p) (call-interactively 'org-indent-item))
  11937. (t (call-interactively 'forward-word))))
  11938. (defun org-metaup (&optional arg)
  11939. "Move subtree up or move table row up.
  11940. Calls `org-move-subtree-up' or `org-table-move-row' or
  11941. `org-move-item-up', depending on context. See the individual commands
  11942. for more information."
  11943. (interactive "P")
  11944. (cond
  11945. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11946. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11947. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11948. (t (transpose-lines 1) (beginning-of-line -1))))
  11949. (defun org-metadown (&optional arg)
  11950. "Move subtree down or move table row down.
  11951. Calls `org-move-subtree-down' or `org-table-move-row' or
  11952. `org-move-item-down', depending on context. See the individual
  11953. commands for more information."
  11954. (interactive "P")
  11955. (cond
  11956. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11957. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11958. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11959. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11960. (defun org-shiftup (&optional arg)
  11961. "Increase item in timestamp or increase priority of current headline.
  11962. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11963. depending on context. See the individual commands for more information."
  11964. (interactive "P")
  11965. (cond
  11966. ((and org-support-shift-select (org-region-active-p))
  11967. (org-call-for-shift-select 'previous-line))
  11968. ((org-at-timestamp-p t)
  11969. (call-interactively (if org-edit-timestamp-down-means-later
  11970. 'org-timestamp-down 'org-timestamp-up)))
  11971. ((and (not (eq org-support-shift-select 'always))
  11972. (org-on-heading-p))
  11973. (call-interactively 'org-priority-up))
  11974. ((and (not org-support-shift-select) (org-at-item-p))
  11975. (call-interactively 'org-previous-item))
  11976. ((org-clocktable-try-shift 'up arg))
  11977. (org-support-shift-select
  11978. (org-call-for-shift-select 'previous-line))
  11979. (t (org-shiftselect-error))))
  11980. (defun org-shiftdown (&optional arg)
  11981. "Decrease item in timestamp or decrease priority of current headline.
  11982. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11983. depending on context. See the individual commands for more information."
  11984. (interactive "P")
  11985. (cond
  11986. ((and org-support-shift-select (org-region-active-p))
  11987. (org-call-for-shift-select 'next-line))
  11988. ((org-at-timestamp-p t)
  11989. (call-interactively (if org-edit-timestamp-down-means-later
  11990. 'org-timestamp-up 'org-timestamp-down)))
  11991. ((and (not (eq org-support-shift-select 'always))
  11992. (org-on-heading-p))
  11993. (call-interactively 'org-priority-down))
  11994. ((and (not org-support-shift-select) (org-at-item-p))
  11995. (call-interactively 'org-next-item))
  11996. ((org-clocktable-try-shift 'down arg))
  11997. (org-support-shift-select
  11998. (org-call-for-shift-select 'next-line))
  11999. (t (org-shiftselect-error))))
  12000. (defun org-shiftright (&optional arg)
  12001. "Cycle the thing at point or in the current line, depending on context.
  12002. Depending on context, this does one of the following:
  12003. - switch a timestamp at point one day into the future
  12004. - on a headline, switch to the next TODO keyword.
  12005. - on an item, switch entire list to the next bullet type
  12006. - on a property line, switch to the next allowed value
  12007. - on a clocktable definition line, move time block into the future"
  12008. (interactive "P")
  12009. (cond
  12010. ((and org-support-shift-select (org-region-active-p))
  12011. (org-call-for-shift-select 'forward-char))
  12012. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12013. ((and (not (eq org-support-shift-select 'always))
  12014. (org-on-heading-p))
  12015. (org-call-with-arg 'org-todo 'right))
  12016. ((or (and org-support-shift-select (org-at-item-bullet-p))
  12017. (and (not org-support-shift-select) (org-at-item-p)))
  12018. (org-call-with-arg 'org-cycle-list-bullet nil))
  12019. ((and (not (eq org-support-shift-select 'always))
  12020. (org-at-property-p))
  12021. (call-interactively 'org-property-next-allowed-value))
  12022. ((org-clocktable-try-shift 'right arg))
  12023. (org-support-shift-select
  12024. (org-call-for-shift-select 'forward-char))
  12025. (t (org-shiftselect-error))))
  12026. (defun org-shiftleft (&optional arg)
  12027. "Cycle the thing at point or in the current line, depending on context.
  12028. Depending on context, this does one of the following:
  12029. - switch a timestamp at point one day into the past
  12030. - on a headline, switch to the previous TODO keyword.
  12031. - on an item, switch entire list to the previous bullet type
  12032. - on a property line, switch to the previous allowed value
  12033. - on a clocktable definition line, move time block into the past"
  12034. (interactive "P")
  12035. (cond
  12036. ((and org-support-shift-select (org-region-active-p))
  12037. (org-call-for-shift-select 'backward-char))
  12038. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12039. ((and (not (eq org-support-shift-select 'always))
  12040. (org-on-heading-p))
  12041. (org-call-with-arg 'org-todo 'left))
  12042. ((or (and org-support-shift-select (org-at-item-bullet-p))
  12043. (and (not org-support-shift-select) (org-at-item-p)))
  12044. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12045. ((and (not (eq org-support-shift-select 'always))
  12046. (org-at-property-p))
  12047. (call-interactively 'org-property-previous-allowed-value))
  12048. ((org-clocktable-try-shift 'left arg))
  12049. (org-support-shift-select
  12050. (org-call-for-shift-select 'backward-char))
  12051. (t (org-shiftselect-error))))
  12052. (defun org-shiftcontrolright ()
  12053. "Switch to next TODO set."
  12054. (interactive)
  12055. (cond
  12056. ((and org-support-shift-select (org-region-active-p))
  12057. (org-call-for-shift-select 'forward-word))
  12058. ((and (not (eq org-support-shift-select 'always))
  12059. (org-on-heading-p))
  12060. (org-call-with-arg 'org-todo 'nextset))
  12061. (org-support-shift-select
  12062. (org-call-for-shift-select 'forward-word))
  12063. (t (org-shiftselect-error))))
  12064. (defun org-shiftcontrolleft ()
  12065. "Switch to previous TODO set."
  12066. (interactive)
  12067. (cond
  12068. ((and org-support-shift-select (org-region-active-p))
  12069. (org-call-for-shift-select 'backward-word))
  12070. ((and (not (eq org-support-shift-select 'always))
  12071. (org-on-heading-p))
  12072. (org-call-with-arg 'org-todo 'previousset))
  12073. (org-support-shift-select
  12074. (org-call-for-shift-select 'backward-word))
  12075. (t (org-shiftselect-error))))
  12076. (defun org-ctrl-c-ret ()
  12077. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12078. (interactive)
  12079. (cond
  12080. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12081. (t (call-interactively 'org-insert-heading))))
  12082. (defun org-copy-special ()
  12083. "Copy region in table or copy current subtree.
  12084. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12085. See the individual commands for more information."
  12086. (interactive)
  12087. (call-interactively
  12088. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12089. (defun org-cut-special ()
  12090. "Cut region in table or cut current subtree.
  12091. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12092. See the individual commands for more information."
  12093. (interactive)
  12094. (call-interactively
  12095. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12096. (defun org-paste-special (arg)
  12097. "Paste rectangular region into table, or past subtree relative to level.
  12098. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12099. See the individual commands for more information."
  12100. (interactive "P")
  12101. (if (org-at-table-p)
  12102. (org-table-paste-rectangle)
  12103. (org-paste-subtree arg)))
  12104. (defun org-edit-special ()
  12105. "Call a special editor for the stuff at point.
  12106. When at a table, call the formula editor with `org-table-edit-formulas'.
  12107. When at the first line of an src example, call `org-edit-src-code'.
  12108. When in an #+include line, visit the include file. Otherwise call
  12109. `ffap' to visit the file at point."
  12110. (interactive)
  12111. (cond
  12112. ((org-at-table-p)
  12113. (call-interactively 'org-table-edit-formulas))
  12114. ((save-excursion
  12115. (beginning-of-line 1)
  12116. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12117. (find-file (org-trim (match-string 1))))
  12118. ((org-edit-src-code))
  12119. ((org-edit-fixed-width-region))
  12120. (t (call-interactively 'ffap))))
  12121. (defun org-ctrl-c-ctrl-c (&optional arg)
  12122. "Set tags in headline, or update according to changed information at point.
  12123. This command does many different things, depending on context:
  12124. - If the cursor is in a headline, prompt for tags and insert them
  12125. into the current line, aligned to `org-tags-column'. When called
  12126. with prefix arg, realign all tags in the current buffer.
  12127. - If the cursor is in one of the special #+KEYWORD lines, this
  12128. triggers scanning the buffer for these lines and updating the
  12129. information.
  12130. - If the cursor is inside a table, realign the table. This command
  12131. works even if the automatic table editor has been turned off.
  12132. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12133. the entire table.
  12134. - If the cursor is at a footnote reference or definition, jump to
  12135. the corresponding definition or references, respectively.
  12136. - If the cursor is a the beginning of a dynamic block, update it.
  12137. - If the cursor is inside a table created by the table.el package,
  12138. activate that table.
  12139. - If the current buffer is a remember buffer, close note and file
  12140. it. A prefix argument of 1 files to the default location
  12141. without further interaction. A prefix argument of 2 files to
  12142. the currently clocking task.
  12143. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12144. links in this buffer.
  12145. - If the cursor is on a numbered item in a plain list, renumber the
  12146. ordered list.
  12147. - If the cursor is on a checkbox, toggle it."
  12148. (interactive "P")
  12149. (let ((org-enable-table-editor t))
  12150. (cond
  12151. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12152. org-occur-highlights
  12153. org-latex-fragment-image-overlays)
  12154. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12155. (org-remove-occur-highlights)
  12156. (org-remove-latex-fragment-image-overlays)
  12157. (message "Temporary highlights/overlays removed from current buffer"))
  12158. ((and (local-variable-p 'org-finish-function (current-buffer))
  12159. (fboundp org-finish-function))
  12160. (funcall org-finish-function))
  12161. ((org-at-property-p)
  12162. (call-interactively 'org-property-action))
  12163. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12164. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12165. ((org-at-table.el-p)
  12166. (require 'table)
  12167. (beginning-of-line 1)
  12168. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12169. (call-interactively 'table-recognize-table))
  12170. ((org-at-table-p)
  12171. (org-table-maybe-eval-formula)
  12172. (if arg
  12173. (call-interactively 'org-table-recalculate)
  12174. (org-table-maybe-recalculate-line))
  12175. (call-interactively 'org-table-align))
  12176. ((or (org-footnote-at-reference-p)
  12177. (org-footnote-at-definition-p))
  12178. (call-interactively 'org-footnote-action))
  12179. ((org-at-item-checkbox-p)
  12180. (call-interactively 'org-toggle-checkbox))
  12181. ((org-at-item-p)
  12182. (call-interactively 'org-maybe-renumber-ordered-list))
  12183. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12184. ;; Dynamic block
  12185. (beginning-of-line 1)
  12186. (save-excursion (org-update-dblock)))
  12187. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12188. (cond
  12189. ((equal (match-string 1) "TBLFM")
  12190. ;; Recalculate the table before this line
  12191. (save-excursion
  12192. (beginning-of-line 1)
  12193. (skip-chars-backward " \r\n\t")
  12194. (if (org-at-table-p)
  12195. (org-call-with-arg 'org-table-recalculate t))))
  12196. (t
  12197. ; (org-set-regexps-and-options)
  12198. ; (org-restart-font-lock)
  12199. (let ((org-inhibit-startup t)) (org-mode-restart))
  12200. (message "Local setup has been refreshed"))))
  12201. (t (error "C-c C-c can do nothing useful at this location.")))))
  12202. (defun org-mode-restart ()
  12203. "Restart Org-mode, to scan again for special lines.
  12204. Also updates the keyword regular expressions."
  12205. (interactive)
  12206. (org-mode)
  12207. (message "Org-mode restarted"))
  12208. (defun org-kill-note-or-show-branches ()
  12209. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12210. (interactive)
  12211. (if (not org-finish-function)
  12212. (call-interactively 'show-branches)
  12213. (let ((org-note-abort t))
  12214. (funcall org-finish-function))))
  12215. (defun org-return (&optional indent)
  12216. "Goto next table row or insert a newline.
  12217. Calls `org-table-next-row' or `newline', depending on context.
  12218. See the individual commands for more information."
  12219. (interactive)
  12220. (cond
  12221. ((bobp) (if indent (newline-and-indent) (newline)))
  12222. ((and (org-at-heading-p)
  12223. (looking-at
  12224. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12225. (org-show-entry)
  12226. (end-of-line 1)
  12227. (newline))
  12228. ((org-at-table-p)
  12229. (org-table-justify-field-maybe)
  12230. (call-interactively 'org-table-next-row))
  12231. (t (if indent (newline-and-indent) (newline)))))
  12232. (defun org-return-indent ()
  12233. "Goto next table row or insert a newline and indent.
  12234. Calls `org-table-next-row' or `newline-and-indent', depending on
  12235. context. See the individual commands for more information."
  12236. (interactive)
  12237. (org-return t))
  12238. (defun org-ctrl-c-star ()
  12239. "Compute table, or change heading status of lines.
  12240. Calls `org-table-recalculate' or `org-toggle-heading',
  12241. depending on context."
  12242. (interactive)
  12243. (cond
  12244. ((org-at-table-p)
  12245. (call-interactively 'org-table-recalculate))
  12246. (t
  12247. ;; Convert all lines in region to list items
  12248. (call-interactively 'org-toggle-heading))))
  12249. (defun org-ctrl-c-minus ()
  12250. "Insert separator line in table or modify bullet status of line.
  12251. Also turns a plain line or a region of lines into list items.
  12252. Calls `org-table-insert-hline', `org-toggle-item', or
  12253. `org-cycle-list-bullet', depending on context."
  12254. (interactive)
  12255. (cond
  12256. ((org-at-table-p)
  12257. (call-interactively 'org-table-insert-hline))
  12258. ((org-region-active-p)
  12259. (call-interactively 'org-toggle-item))
  12260. ((org-in-item-p)
  12261. (call-interactively 'org-cycle-list-bullet))
  12262. (t
  12263. (call-interactively 'org-toggle-item))))
  12264. (defun org-toggle-item ()
  12265. "Convert headings or normal lines to items, items to normal lines.
  12266. If there is no active region, only the current line is considered.
  12267. If the first line in the region is a headline, convert all headlines to items.
  12268. If the first line in the region is an item, convert all items to normal lines.
  12269. If the first line is normal text, add an item bullet to each line."
  12270. (interactive)
  12271. (let (l2 l beg end)
  12272. (if (org-region-active-p)
  12273. (setq beg (region-beginning) end (region-end))
  12274. (setq beg (point-at-bol)
  12275. end (min (1+ (point-at-eol)) (point-max))))
  12276. (save-excursion
  12277. (goto-char end)
  12278. (setq l2 (org-current-line))
  12279. (goto-char beg)
  12280. (beginning-of-line 1)
  12281. (setq l (1- (org-current-line)))
  12282. (if (org-at-item-p)
  12283. ;; We already have items, de-itemize
  12284. (while (< (setq l (1+ l)) l2)
  12285. (when (org-at-item-p)
  12286. (goto-char (match-beginning 2))
  12287. (delete-region (match-beginning 2) (match-end 2))
  12288. (and (looking-at "[ \t]+") (replace-match "")))
  12289. (beginning-of-line 2))
  12290. (if (org-on-heading-p)
  12291. ;; Headings, convert to items
  12292. (while (< (setq l (1+ l)) l2)
  12293. (if (looking-at org-outline-regexp)
  12294. (replace-match "- " t t))
  12295. (beginning-of-line 2))
  12296. ;; normal lines, turn them into items
  12297. (while (< (setq l (1+ l)) l2)
  12298. (unless (org-at-item-p)
  12299. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12300. (replace-match "\\1- \\2")))
  12301. (beginning-of-line 2)))))))
  12302. (defun org-toggle-heading (&optional nstars)
  12303. "Convert headings to normal text, or items or text to headings.
  12304. If there is no active region, only the current line is considered.
  12305. If the first line is a heading, remove the stars from all headlines
  12306. in the region.
  12307. If the first line is a plain list item, turn all plain list items into
  12308. headings.
  12309. If the first line is a normal line, turn each and every line in the region
  12310. into a heading.
  12311. When converting a line into a heading, the number of stars is chosen
  12312. such that the lines become children of the current entry. However, when
  12313. a prefix argument is given, its value determines the number of stars to add."
  12314. (interactive "P")
  12315. (let (l2 l itemp beg end)
  12316. (if (org-region-active-p)
  12317. (setq beg (region-beginning) end (region-end))
  12318. (setq beg (point-at-bol)
  12319. end (min (1+ (point-at-eol)) (point-max))))
  12320. (save-excursion
  12321. (goto-char end)
  12322. (setq l2 (org-current-line))
  12323. (goto-char beg)
  12324. (beginning-of-line 1)
  12325. (setq l (1- (org-current-line)))
  12326. (if (org-on-heading-p)
  12327. ;; We already have headlines, de-star them
  12328. (while (< (setq l (1+ l)) l2)
  12329. (when (org-on-heading-p t)
  12330. (and (looking-at outline-regexp) (replace-match "")))
  12331. (beginning-of-line 2))
  12332. (setq itemp (org-at-item-p))
  12333. (let* ((stars
  12334. (if nstars
  12335. (make-string (prefix-numeric-value current-prefix-arg)
  12336. ?*)
  12337. (save-excursion
  12338. (re-search-backward org-complex-heading-regexp nil t)
  12339. (or (match-string 1) "*"))))
  12340. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12341. (rpl (concat stars add-stars " ")))
  12342. (while (< (setq l (1+ l)) l2)
  12343. (if itemp
  12344. (and (org-at-item-p) (replace-match rpl t t))
  12345. (unless (org-on-heading-p)
  12346. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12347. (replace-match (concat rpl (match-string 2))))))
  12348. (beginning-of-line 2)))))))
  12349. (defun org-meta-return (&optional arg)
  12350. "Insert a new heading or wrap a region in a table.
  12351. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12352. See the individual commands for more information."
  12353. (interactive "P")
  12354. (cond
  12355. ((org-at-table-p)
  12356. (call-interactively 'org-table-wrap-region))
  12357. (t (call-interactively 'org-insert-heading))))
  12358. ;;; Menu entries
  12359. ;; Define the Org-mode menus
  12360. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12361. '("Tbl"
  12362. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12363. ["Next Field" org-cycle (org-at-table-p)]
  12364. ["Previous Field" org-shifttab (org-at-table-p)]
  12365. ["Next Row" org-return (org-at-table-p)]
  12366. "--"
  12367. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12368. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12369. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12370. "--"
  12371. ("Column"
  12372. ["Move Column Left" org-metaleft (org-at-table-p)]
  12373. ["Move Column Right" org-metaright (org-at-table-p)]
  12374. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12375. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12376. ("Row"
  12377. ["Move Row Up" org-metaup (org-at-table-p)]
  12378. ["Move Row Down" org-metadown (org-at-table-p)]
  12379. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12380. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12381. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12382. "--"
  12383. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12384. ("Rectangle"
  12385. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12386. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12387. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12388. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12389. "--"
  12390. ("Calculate"
  12391. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12392. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12393. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12394. "--"
  12395. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12396. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12397. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12398. "--"
  12399. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12400. "--"
  12401. ["Sum Column/Rectangle" org-table-sum
  12402. (or (org-at-table-p) (org-region-active-p))]
  12403. ["Which Column?" org-table-current-column (org-at-table-p)])
  12404. ["Debug Formulas"
  12405. org-table-toggle-formula-debugger
  12406. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12407. ["Show Col/Row Numbers"
  12408. org-table-toggle-coordinate-overlays
  12409. :style toggle
  12410. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12411. "--"
  12412. ["Create" org-table-create (and (not (org-at-table-p))
  12413. org-enable-table-editor)]
  12414. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12415. ["Import from File" org-table-import (not (org-at-table-p))]
  12416. ["Export to File" org-table-export (org-at-table-p)]
  12417. "--"
  12418. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12419. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12420. '("Org"
  12421. ("Show/Hide"
  12422. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12423. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12424. ["Sparse Tree..." org-sparse-tree t]
  12425. ["Reveal Context" org-reveal t]
  12426. ["Show All" show-all t]
  12427. "--"
  12428. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12429. "--"
  12430. ["New Heading" org-insert-heading t]
  12431. ("Navigate Headings"
  12432. ["Up" outline-up-heading t]
  12433. ["Next" outline-next-visible-heading t]
  12434. ["Previous" outline-previous-visible-heading t]
  12435. ["Next Same Level" outline-forward-same-level t]
  12436. ["Previous Same Level" outline-backward-same-level t]
  12437. "--"
  12438. ["Jump" org-goto t])
  12439. ("Edit Structure"
  12440. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12441. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12442. "--"
  12443. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12444. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12445. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12446. "--"
  12447. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12448. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12449. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12450. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12451. "--"
  12452. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12453. "--"
  12454. ["Convert to odd levels" org-convert-to-odd-levels t]
  12455. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12456. ("Editing"
  12457. ["Emphasis..." org-emphasize t]
  12458. ["Edit Source Example" org-edit-special t]
  12459. "--"
  12460. ["Footnote new/jump" org-footnote-action t]
  12461. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12462. ("Archive"
  12463. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12464. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12465. ; :active t :keys "C-u C-c C-x C-a"]
  12466. ["Sparse trees open ARCHIVE trees"
  12467. (setq org-sparse-tree-open-archived-trees
  12468. (not org-sparse-tree-open-archived-trees))
  12469. :style toggle :selected org-sparse-tree-open-archived-trees]
  12470. ["Cycling opens ARCHIVE trees"
  12471. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12472. :style toggle :selected org-cycle-open-archived-trees]
  12473. "--"
  12474. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12475. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12476. ; ["Check and Move Children" (org-archive-subtree '(4))
  12477. ; :active t :keys "C-u C-c C-x C-s"]
  12478. )
  12479. "--"
  12480. ("TODO Lists"
  12481. ["TODO/DONE/-" org-todo t]
  12482. ("Select keyword"
  12483. ["Next keyword" org-shiftright (org-on-heading-p)]
  12484. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12485. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12486. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12487. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12488. ["Show TODO Tree" org-show-todo-tree t]
  12489. ["Global TODO list" org-todo-list t]
  12490. "--"
  12491. ["Set Priority" org-priority t]
  12492. ["Priority Up" org-shiftup t]
  12493. ["Priority Down" org-shiftdown t])
  12494. ("TAGS and Properties"
  12495. ["Set Tags" org-set-tags-command t]
  12496. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12497. "--"
  12498. ["Set property" org-set-property t]
  12499. ["Column view of properties" org-columns t]
  12500. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12501. ("Dates and Scheduling"
  12502. ["Timestamp" org-time-stamp t]
  12503. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12504. ("Change Date"
  12505. ["1 Day Later" org-shiftright t]
  12506. ["1 Day Earlier" org-shiftleft t]
  12507. ["1 ... Later" org-shiftup t]
  12508. ["1 ... Earlier" org-shiftdown t])
  12509. ["Compute Time Range" org-evaluate-time-range t]
  12510. ["Schedule Item" org-schedule t]
  12511. ["Deadline" org-deadline t]
  12512. "--"
  12513. ["Custom time format" org-toggle-time-stamp-overlays
  12514. :style radio :selected org-display-custom-times]
  12515. "--"
  12516. ["Goto Calendar" org-goto-calendar t]
  12517. ["Date from Calendar" org-date-from-calendar t]
  12518. "--"
  12519. ["Start/Restart Timer" org-timer-start t]
  12520. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12521. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12522. ["Insert Timer String" org-timer t]
  12523. ["Insert Timer Item" org-timer-item t])
  12524. ("Logging work"
  12525. ["Clock in" org-clock-in t]
  12526. ["Clock out" org-clock-out t]
  12527. ["Clock cancel" org-clock-cancel t]
  12528. ["Goto running clock" org-clock-goto t]
  12529. ["Display times" org-clock-display t]
  12530. ["Create clock table" org-clock-report t]
  12531. "--"
  12532. ["Record DONE time"
  12533. (progn (setq org-log-done (not org-log-done))
  12534. (message "Switching to %s will %s record a timestamp"
  12535. (car org-done-keywords)
  12536. (if org-log-done "automatically" "not")))
  12537. :style toggle :selected org-log-done])
  12538. "--"
  12539. ["Agenda Command..." org-agenda t]
  12540. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12541. ("File List for Agenda")
  12542. ("Special views current file"
  12543. ["TODO Tree" org-show-todo-tree t]
  12544. ["Check Deadlines" org-check-deadlines t]
  12545. ["Timeline" org-timeline t]
  12546. ["Tags Tree" org-tags-sparse-tree t])
  12547. "--"
  12548. ("Hyperlinks"
  12549. ["Store Link (Global)" org-store-link t]
  12550. ["Insert Link" org-insert-link t]
  12551. ["Follow Link" org-open-at-point t]
  12552. "--"
  12553. ["Next link" org-next-link t]
  12554. ["Previous link" org-previous-link t]
  12555. "--"
  12556. ["Descriptive Links"
  12557. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12558. :style radio
  12559. :selected (member '(org-link) buffer-invisibility-spec)]
  12560. ["Literal Links"
  12561. (progn
  12562. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12563. :style radio
  12564. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12565. "--"
  12566. ["Export/Publish..." org-export t]
  12567. ("LaTeX"
  12568. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12569. :selected org-cdlatex-mode]
  12570. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12571. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12572. ["Modify math symbol" org-cdlatex-math-modify
  12573. (org-inside-LaTeX-fragment-p)]
  12574. ["Export LaTeX fragments as images"
  12575. (if (featurep 'org-exp)
  12576. (setq org-export-with-LaTeX-fragments
  12577. (not org-export-with-LaTeX-fragments))
  12578. (require 'org-exp))
  12579. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12580. org-export-with-LaTeX-fragments)])
  12581. "--"
  12582. ("Documentation"
  12583. ["Show Version" org-version t]
  12584. ["Info Documentation" org-info t])
  12585. ("Customize"
  12586. ["Browse Org Group" org-customize t]
  12587. "--"
  12588. ["Expand This Menu" org-create-customize-menu
  12589. (fboundp 'customize-menu-create)])
  12590. "--"
  12591. ["Refresh setup" org-mode-restart t]
  12592. ))
  12593. (defun org-info (&optional node)
  12594. "Read documentation for Org-mode in the info system.
  12595. With optional NODE, go directly to that node."
  12596. (interactive)
  12597. (info (format "(org)%s" (or node ""))))
  12598. (defun org-install-agenda-files-menu ()
  12599. (let ((bl (buffer-list)))
  12600. (save-excursion
  12601. (while bl
  12602. (set-buffer (pop bl))
  12603. (if (org-mode-p) (setq bl nil)))
  12604. (when (org-mode-p)
  12605. (easy-menu-change
  12606. '("Org") "File List for Agenda"
  12607. (append
  12608. (list
  12609. ["Edit File List" (org-edit-agenda-file-list) t]
  12610. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12611. ["Remove Current File from List" org-remove-file t]
  12612. ["Cycle through agenda files" org-cycle-agenda-files t]
  12613. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12614. "--")
  12615. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12616. ;;;; Documentation
  12617. ;;;###autoload
  12618. (defun org-require-autoloaded-modules ()
  12619. (interactive)
  12620. (mapc 'require
  12621. '(org-agenda org-archive org-clock org-colview
  12622. org-exp org-id org-export-latex org-publish
  12623. org-remember org-table)))
  12624. ;;;###autoload
  12625. (defun org-customize ()
  12626. "Call the customize function with org as argument."
  12627. (interactive)
  12628. (org-load-modules-maybe)
  12629. (org-require-autoloaded-modules)
  12630. (customize-browse 'org))
  12631. (defun org-create-customize-menu ()
  12632. "Create a full customization menu for Org-mode, insert it into the menu."
  12633. (interactive)
  12634. (org-load-modules-maybe)
  12635. (org-require-autoloaded-modules)
  12636. (if (fboundp 'customize-menu-create)
  12637. (progn
  12638. (easy-menu-change
  12639. '("Org") "Customize"
  12640. `(["Browse Org group" org-customize t]
  12641. "--"
  12642. ,(customize-menu-create 'org)
  12643. ["Set" Custom-set t]
  12644. ["Save" Custom-save t]
  12645. ["Reset to Current" Custom-reset-current t]
  12646. ["Reset to Saved" Custom-reset-saved t]
  12647. ["Reset to Standard Settings" Custom-reset-standard t]))
  12648. (message "\"Org\"-menu now contains full customization menu"))
  12649. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12650. ;;;; Miscellaneous stuff
  12651. ;;; Generally useful functions
  12652. (defun org-find-text-property-in-string (prop s)
  12653. "Return the first non-nil value of property PROP in string S."
  12654. (or (get-text-property 0 prop s)
  12655. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12656. prop s)))
  12657. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12658. "Display the given MESSAGE as a warning."
  12659. (if (fboundp 'display-warning)
  12660. (display-warning 'org message
  12661. (if (featurep 'xemacs)
  12662. 'warning
  12663. :warning))
  12664. (let ((buf (get-buffer-create "*Org warnings*")))
  12665. (with-current-buffer buf
  12666. (goto-char (point-max))
  12667. (insert "Warning (Org): " message)
  12668. (unless (bolp)
  12669. (newline)))
  12670. (display-buffer buf)
  12671. (sit-for 0))))
  12672. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12673. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12674. (if (and marker (marker-buffer marker)
  12675. (buffer-live-p (marker-buffer marker)))
  12676. (progn
  12677. (switch-to-buffer (marker-buffer marker))
  12678. (if (or (> marker (point-max)) (< marker (point-min)))
  12679. (widen))
  12680. (goto-char marker)
  12681. (org-show-context 'org-goto))
  12682. (if bookmark
  12683. (bookmark-jump bookmark)
  12684. (error "Cannot find location"))))
  12685. (defun org-quote-csv-field (s)
  12686. "Quote field for inclusion in CSV material."
  12687. (if (string-match "[\",]" s)
  12688. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12689. s))
  12690. (defun org-plist-delete (plist property)
  12691. "Delete PROPERTY from PLIST.
  12692. This is in contrast to merely setting it to 0."
  12693. (let (p)
  12694. (while plist
  12695. (if (not (eq property (car plist)))
  12696. (setq p (plist-put p (car plist) (nth 1 plist))))
  12697. (setq plist (cddr plist)))
  12698. p))
  12699. (defun org-force-self-insert (N)
  12700. "Needed to enforce self-insert under remapping."
  12701. (interactive "p")
  12702. (self-insert-command N))
  12703. (defun org-string-width (s)
  12704. "Compute width of string, ignoring invisible characters.
  12705. This ignores character with invisibility property `org-link', and also
  12706. characters with property `org-cwidth', because these will become invisible
  12707. upon the next fontification round."
  12708. (let (b l)
  12709. (when (or (eq t buffer-invisibility-spec)
  12710. (assq 'org-link buffer-invisibility-spec))
  12711. (while (setq b (text-property-any 0 (length s)
  12712. 'invisible 'org-link s))
  12713. (setq s (concat (substring s 0 b)
  12714. (substring s (or (next-single-property-change
  12715. b 'invisible s) (length s)))))))
  12716. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12717. (setq s (concat (substring s 0 b)
  12718. (substring s (or (next-single-property-change
  12719. b 'org-cwidth s) (length s))))))
  12720. (setq l (string-width s) b -1)
  12721. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12722. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12723. l))
  12724. (defun org-get-indentation (&optional line)
  12725. "Get the indentation of the current line, interpreting tabs.
  12726. When LINE is given, assume it represents a line and compute its indentation."
  12727. (if line
  12728. (if (string-match "^ *" (org-remove-tabs line))
  12729. (match-end 0))
  12730. (save-excursion
  12731. (beginning-of-line 1)
  12732. (skip-chars-forward " \t")
  12733. (current-column))))
  12734. (defun org-remove-tabs (s &optional width)
  12735. "Replace tabulators in S with spaces.
  12736. Assumes that s is a single line, starting in column 0."
  12737. (setq width (or width tab-width))
  12738. (while (string-match "\t" s)
  12739. (setq s (replace-match
  12740. (make-string
  12741. (- (* width (/ (+ (match-beginning 0) width) width))
  12742. (match-beginning 0)) ?\ )
  12743. t t s)))
  12744. s)
  12745. (defun org-fix-indentation (line ind)
  12746. "Fix indentation in LINE.
  12747. IND is a cons cell with target and minimum indentation.
  12748. If the current indentation in LINE is smaller than the minimum,
  12749. leave it alone. If it is larger than ind, set it to the target."
  12750. (let* ((l (org-remove-tabs line))
  12751. (i (org-get-indentation l))
  12752. (i1 (car ind)) (i2 (cdr ind)))
  12753. (if (>= i i2) (setq l (substring line i2)))
  12754. (if (> i1 0)
  12755. (concat (make-string i1 ?\ ) l)
  12756. l)))
  12757. (defun org-base-buffer (buffer)
  12758. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12759. (if (not buffer)
  12760. buffer
  12761. (or (buffer-base-buffer buffer)
  12762. buffer)))
  12763. (defun org-trim (s)
  12764. "Remove whitespace at beginning and end of string."
  12765. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12766. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12767. s)
  12768. (defun org-wrap (string &optional width lines)
  12769. "Wrap string to either a number of lines, or a width in characters.
  12770. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12771. that costs. If there is a word longer than WIDTH, the text is actually
  12772. wrapped to the length of that word.
  12773. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12774. many lines, whatever width that takes.
  12775. The return value is a list of lines, without newlines at the end."
  12776. (let* ((words (org-split-string string "[ \t\n]+"))
  12777. (maxword (apply 'max (mapcar 'org-string-width words)))
  12778. w ll)
  12779. (cond (width
  12780. (org-do-wrap words (max maxword width)))
  12781. (lines
  12782. (setq w maxword)
  12783. (setq ll (org-do-wrap words maxword))
  12784. (if (<= (length ll) lines)
  12785. ll
  12786. (setq ll words)
  12787. (while (> (length ll) lines)
  12788. (setq w (1+ w))
  12789. (setq ll (org-do-wrap words w)))
  12790. ll))
  12791. (t (error "Cannot wrap this")))))
  12792. (defun org-do-wrap (words width)
  12793. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12794. (let (lines line)
  12795. (while words
  12796. (setq line (pop words))
  12797. (while (and words (< (+ (length line) (length (car words))) width))
  12798. (setq line (concat line " " (pop words))))
  12799. (setq lines (push line lines)))
  12800. (nreverse lines)))
  12801. (defun org-split-string (string &optional separators)
  12802. "Splits STRING into substrings at SEPARATORS.
  12803. No empty strings are returned if there are matches at the beginning
  12804. and end of string."
  12805. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12806. (start 0)
  12807. notfirst
  12808. (list nil))
  12809. (while (and (string-match rexp string
  12810. (if (and notfirst
  12811. (= start (match-beginning 0))
  12812. (< start (length string)))
  12813. (1+ start) start))
  12814. (< (match-beginning 0) (length string)))
  12815. (setq notfirst t)
  12816. (or (eq (match-beginning 0) 0)
  12817. (and (eq (match-beginning 0) (match-end 0))
  12818. (eq (match-beginning 0) start))
  12819. (setq list
  12820. (cons (substring string start (match-beginning 0))
  12821. list)))
  12822. (setq start (match-end 0)))
  12823. (or (eq start (length string))
  12824. (setq list
  12825. (cons (substring string start)
  12826. list)))
  12827. (nreverse list)))
  12828. (defun org-context ()
  12829. "Return a list of contexts of the current cursor position.
  12830. If several contexts apply, all are returned.
  12831. Each context entry is a list with a symbol naming the context, and
  12832. two positions indicating start and end of the context. Possible
  12833. contexts are:
  12834. :headline anywhere in a headline
  12835. :headline-stars on the leading stars in a headline
  12836. :todo-keyword on a TODO keyword (including DONE) in a headline
  12837. :tags on the TAGS in a headline
  12838. :priority on the priority cookie in a headline
  12839. :item on the first line of a plain list item
  12840. :item-bullet on the bullet/number of a plain list item
  12841. :checkbox on the checkbox in a plain list item
  12842. :table in an org-mode table
  12843. :table-special on a special filed in a table
  12844. :table-table in a table.el table
  12845. :link on a hyperlink
  12846. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12847. :target on a <<target>>
  12848. :radio-target on a <<<radio-target>>>
  12849. :latex-fragment on a LaTeX fragment
  12850. :latex-preview on a LaTeX fragment with overlayed preview image
  12851. This function expects the position to be visible because it uses font-lock
  12852. faces as a help to recognize the following contexts: :table-special, :link,
  12853. and :keyword."
  12854. (let* ((f (get-text-property (point) 'face))
  12855. (faces (if (listp f) f (list f)))
  12856. (p (point)) clist o)
  12857. ;; First the large context
  12858. (cond
  12859. ((org-on-heading-p t)
  12860. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12861. (when (progn
  12862. (beginning-of-line 1)
  12863. (looking-at org-todo-line-tags-regexp))
  12864. (push (org-point-in-group p 1 :headline-stars) clist)
  12865. (push (org-point-in-group p 2 :todo-keyword) clist)
  12866. (push (org-point-in-group p 4 :tags) clist))
  12867. (goto-char p)
  12868. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12869. (if (looking-at "\\[#[A-Z0-9]\\]")
  12870. (push (org-point-in-group p 0 :priority) clist)))
  12871. ((org-at-item-p)
  12872. (push (org-point-in-group p 2 :item-bullet) clist)
  12873. (push (list :item (point-at-bol)
  12874. (save-excursion (org-end-of-item) (point)))
  12875. clist)
  12876. (and (org-at-item-checkbox-p)
  12877. (push (org-point-in-group p 0 :checkbox) clist)))
  12878. ((org-at-table-p)
  12879. (push (list :table (org-table-begin) (org-table-end)) clist)
  12880. (if (memq 'org-formula faces)
  12881. (push (list :table-special
  12882. (previous-single-property-change p 'face)
  12883. (next-single-property-change p 'face)) clist)))
  12884. ((org-at-table-p 'any)
  12885. (push (list :table-table) clist)))
  12886. (goto-char p)
  12887. ;; Now the small context
  12888. (cond
  12889. ((org-at-timestamp-p)
  12890. (push (org-point-in-group p 0 :timestamp) clist))
  12891. ((memq 'org-link faces)
  12892. (push (list :link
  12893. (previous-single-property-change p 'face)
  12894. (next-single-property-change p 'face)) clist))
  12895. ((memq 'org-special-keyword faces)
  12896. (push (list :keyword
  12897. (previous-single-property-change p 'face)
  12898. (next-single-property-change p 'face)) clist))
  12899. ((org-on-target-p)
  12900. (push (org-point-in-group p 0 :target) clist)
  12901. (goto-char (1- (match-beginning 0)))
  12902. (if (looking-at org-radio-target-regexp)
  12903. (push (org-point-in-group p 0 :radio-target) clist))
  12904. (goto-char p))
  12905. ((setq o (car (delq nil
  12906. (mapcar
  12907. (lambda (x)
  12908. (if (memq x org-latex-fragment-image-overlays) x))
  12909. (org-overlays-at (point))))))
  12910. (push (list :latex-fragment
  12911. (org-overlay-start o) (org-overlay-end o)) clist)
  12912. (push (list :latex-preview
  12913. (org-overlay-start o) (org-overlay-end o)) clist))
  12914. ((org-inside-LaTeX-fragment-p)
  12915. ;; FIXME: positions wrong.
  12916. (push (list :latex-fragment (point) (point)) clist)))
  12917. (setq clist (nreverse (delq nil clist)))
  12918. clist))
  12919. ;; FIXME: Compare with at-regexp-p Do we need both?
  12920. (defun org-in-regexp (re &optional nlines visually)
  12921. "Check if point is inside a match of regexp.
  12922. Normally only the current line is checked, but you can include NLINES extra
  12923. lines both before and after point into the search.
  12924. If VISUALLY is set, require that the cursor is not after the match but
  12925. really on, so that the block visually is on the match."
  12926. (catch 'exit
  12927. (let ((pos (point))
  12928. (eol (point-at-eol (+ 1 (or nlines 0))))
  12929. (inc (if visually 1 0)))
  12930. (save-excursion
  12931. (beginning-of-line (- 1 (or nlines 0)))
  12932. (while (re-search-forward re eol t)
  12933. (if (and (<= (match-beginning 0) pos)
  12934. (>= (+ inc (match-end 0)) pos))
  12935. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12936. (defun org-at-regexp-p (regexp)
  12937. "Is point inside a match of REGEXP in the current line?"
  12938. (catch 'exit
  12939. (save-excursion
  12940. (let ((pos (point)) (end (point-at-eol)))
  12941. (beginning-of-line 1)
  12942. (while (re-search-forward regexp end t)
  12943. (if (and (<= (match-beginning 0) pos)
  12944. (>= (match-end 0) pos))
  12945. (throw 'exit t)))
  12946. nil))))
  12947. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12948. "Call `multi-occur' with buffers for all agenda files."
  12949. (interactive "sOrg-files matching: \np")
  12950. (let* ((files (org-agenda-files))
  12951. (tnames (mapcar 'file-truename files))
  12952. (extra org-agenda-text-search-extra-files)
  12953. f)
  12954. (when (eq (car extra) 'agenda-archives)
  12955. (setq extra (cdr extra))
  12956. (setq files (org-add-archive-files files)))
  12957. (while (setq f (pop extra))
  12958. (unless (member (file-truename f) tnames)
  12959. (add-to-list 'files f 'append)
  12960. (add-to-list 'tnames (file-truename f) 'append)))
  12961. (multi-occur
  12962. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12963. regexp)))
  12964. (if (boundp 'occur-mode-find-occurrence-hook)
  12965. ;; Emacs 23
  12966. (add-hook 'occur-mode-find-occurrence-hook
  12967. (lambda ()
  12968. (when (org-mode-p)
  12969. (org-reveal))))
  12970. ;; Emacs 22
  12971. (defadvice occur-mode-goto-occurrence
  12972. (after org-occur-reveal activate)
  12973. (and (org-mode-p) (org-reveal)))
  12974. (defadvice occur-mode-goto-occurrence-other-window
  12975. (after org-occur-reveal activate)
  12976. (and (org-mode-p) (org-reveal)))
  12977. (defadvice occur-mode-display-occurrence
  12978. (after org-occur-reveal activate)
  12979. (when (org-mode-p)
  12980. (let ((pos (occur-mode-find-occurrence)))
  12981. (with-current-buffer (marker-buffer pos)
  12982. (save-excursion
  12983. (goto-char pos)
  12984. (org-reveal)))))))
  12985. (defun org-uniquify (list)
  12986. "Remove duplicate elements from LIST."
  12987. (let (res)
  12988. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12989. res))
  12990. (defun org-delete-all (elts list)
  12991. "Remove all elements in ELTS from LIST."
  12992. (while elts
  12993. (setq list (delete (pop elts) list)))
  12994. list)
  12995. (defun org-back-over-empty-lines ()
  12996. "Move backwards over whitespace, to the beginning of the first empty line.
  12997. Returns the number of empty lines passed."
  12998. (let ((pos (point)))
  12999. (skip-chars-backward " \t\n\r")
  13000. (beginning-of-line 2)
  13001. (goto-char (min (point) pos))
  13002. (count-lines (point) pos)))
  13003. (defun org-skip-whitespace ()
  13004. (skip-chars-forward " \t\n\r"))
  13005. (defun org-point-in-group (point group &optional context)
  13006. "Check if POINT is in match-group GROUP.
  13007. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13008. match. If the match group does ot exist or point is not inside it,
  13009. return nil."
  13010. (and (match-beginning group)
  13011. (>= point (match-beginning group))
  13012. (<= point (match-end group))
  13013. (if context
  13014. (list context (match-beginning group) (match-end group))
  13015. t)))
  13016. (defun org-switch-to-buffer-other-window (&rest args)
  13017. "Switch to buffer in a second window on the current frame.
  13018. In particular, do not allow pop-up frames."
  13019. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13020. special-display-function)
  13021. (apply 'switch-to-buffer-other-window args)))
  13022. (defun org-combine-plists (&rest plists)
  13023. "Create a single property list from all plists in PLISTS.
  13024. The process starts by copying the first list, and then setting properties
  13025. from the other lists. Settings in the last list are the most significant
  13026. ones and overrule settings in the other lists."
  13027. (let ((rtn (copy-sequence (pop plists)))
  13028. p v ls)
  13029. (while plists
  13030. (setq ls (pop plists))
  13031. (while ls
  13032. (setq p (pop ls) v (pop ls))
  13033. (setq rtn (plist-put rtn p v))))
  13034. rtn))
  13035. (defun org-move-line-down (arg)
  13036. "Move the current line down. With prefix argument, move it past ARG lines."
  13037. (interactive "p")
  13038. (let ((col (current-column))
  13039. beg end pos)
  13040. (beginning-of-line 1) (setq beg (point))
  13041. (beginning-of-line 2) (setq end (point))
  13042. (beginning-of-line (+ 1 arg))
  13043. (setq pos (move-marker (make-marker) (point)))
  13044. (insert (delete-and-extract-region beg end))
  13045. (goto-char pos)
  13046. (org-move-to-column col)))
  13047. (defun org-move-line-up (arg)
  13048. "Move the current line up. With prefix argument, move it past ARG lines."
  13049. (interactive "p")
  13050. (let ((col (current-column))
  13051. beg end pos)
  13052. (beginning-of-line 1) (setq beg (point))
  13053. (beginning-of-line 2) (setq end (point))
  13054. (beginning-of-line (- arg))
  13055. (setq pos (move-marker (make-marker) (point)))
  13056. (insert (delete-and-extract-region beg end))
  13057. (goto-char pos)
  13058. (org-move-to-column col)))
  13059. (defun org-replace-escapes (string table)
  13060. "Replace %-escapes in STRING with values in TABLE.
  13061. TABLE is an association list with keys like \"%a\" and string values.
  13062. The sequences in STRING may contain normal field width and padding information,
  13063. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13064. so values can contain further %-escapes if they are define later in TABLE."
  13065. (let ((case-fold-search nil)
  13066. e re rpl)
  13067. (while (setq e (pop table))
  13068. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13069. (while (string-match re string)
  13070. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13071. (cdr e)))
  13072. (setq string (replace-match rpl t t string))))
  13073. string))
  13074. (defun org-sublist (list start end)
  13075. "Return a section of LIST, from START to END.
  13076. Counting starts at 1."
  13077. (let (rtn (c start))
  13078. (setq list (nthcdr (1- start) list))
  13079. (while (and list (<= c end))
  13080. (push (pop list) rtn)
  13081. (setq c (1+ c)))
  13082. (nreverse rtn)))
  13083. (defun org-find-base-buffer-visiting (file)
  13084. "Like `find-buffer-visiting' but alway return the base buffer and
  13085. not an indirect buffer."
  13086. (let ((buf (find-buffer-visiting file)))
  13087. (if buf
  13088. (or (buffer-base-buffer buf) buf)
  13089. nil)))
  13090. (defun org-image-file-name-regexp (&optional extensions)
  13091. "Return regexp matching the file names of images.
  13092. If EXTENSIONS is given, only match these."
  13093. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13094. (image-file-name-regexp)
  13095. (let ((image-file-name-extensions
  13096. (or extensions
  13097. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13098. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13099. (concat "\\."
  13100. (regexp-opt (nconc (mapcar 'upcase
  13101. image-file-name-extensions)
  13102. image-file-name-extensions)
  13103. t)
  13104. "\\'"))))
  13105. (defun org-file-image-p (file &optional extensions)
  13106. "Return non-nil if FILE is an image."
  13107. (save-match-data
  13108. (string-match (org-image-file-name-regexp extensions) file)))
  13109. (defun org-get-cursor-date ()
  13110. "Return the date at cursor in as a time.
  13111. This works in the calendar and in the agenda, anywhere else it just
  13112. returns the current time."
  13113. (let (date day defd)
  13114. (cond
  13115. ((eq major-mode 'calendar-mode)
  13116. (setq date (calendar-cursor-to-date)
  13117. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13118. ((eq major-mode 'org-agenda-mode)
  13119. (setq day (get-text-property (point) 'day))
  13120. (if day
  13121. (setq date (calendar-gregorian-from-absolute day)
  13122. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13123. (nth 2 date))))))
  13124. (or defd (current-time))))
  13125. (defvar org-agenda-action-marker (make-marker)
  13126. "Marker pointing to the entry for the next agenda action.")
  13127. (defun org-mark-entry-for-agenda-action ()
  13128. "Mark the current entry as target of an agenda action.
  13129. Agenda actions are actions executed from the agenda with the key `k',
  13130. which make use of the date at the cursor."
  13131. (interactive)
  13132. (move-marker org-agenda-action-marker
  13133. (save-excursion (org-back-to-heading t) (point))
  13134. (current-buffer))
  13135. (message
  13136. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13137. ;;; Paragraph filling stuff.
  13138. ;; We want this to be just right, so use the full arsenal.
  13139. (defun org-indent-line-function ()
  13140. "Indent line like previous, but further if previous was headline or item."
  13141. (interactive)
  13142. (let* ((pos (point))
  13143. (itemp (org-at-item-p))
  13144. column bpos bcol tpos tcol bullet btype bullet-type)
  13145. ;; Find the previous relevant line
  13146. (beginning-of-line 1)
  13147. (cond
  13148. ((looking-at "#") (setq column 0))
  13149. ((looking-at "\\*+ ") (setq column 0))
  13150. (t
  13151. (beginning-of-line 0)
  13152. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13153. (beginning-of-line 0))
  13154. (cond
  13155. ((looking-at "\\*+[ \t]+")
  13156. (if (not org-adapt-indentation)
  13157. (setq column 0)
  13158. (goto-char (match-end 0))
  13159. (setq column (current-column))))
  13160. ((org-in-item-p)
  13161. (org-beginning-of-item)
  13162. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13163. (setq bpos (match-beginning 1) tpos (match-end 0)
  13164. bcol (progn (goto-char bpos) (current-column))
  13165. tcol (progn (goto-char tpos) (current-column))
  13166. bullet (match-string 1)
  13167. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13168. (if (> tcol (+ bcol org-description-max-indent))
  13169. (setq tcol (+ bcol 5)))
  13170. (if (not itemp)
  13171. (setq column tcol)
  13172. (goto-char pos)
  13173. (beginning-of-line 1)
  13174. (if (looking-at "\\S-")
  13175. (progn
  13176. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13177. (setq bullet (match-string 1)
  13178. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13179. (setq column (if (equal btype bullet-type) bcol tcol)))
  13180. (setq column (org-get-indentation)))))
  13181. (t (setq column (org-get-indentation))))))
  13182. (goto-char pos)
  13183. (if (<= (current-column) (current-indentation))
  13184. (org-indent-line-to column)
  13185. (save-excursion (org-indent-line-to column)))
  13186. (setq column (current-column))
  13187. (beginning-of-line 1)
  13188. (if (looking-at
  13189. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13190. (replace-match (concat "\\1" (format org-property-format
  13191. (match-string 2) (match-string 3)))
  13192. t nil))
  13193. (org-move-to-column column)))
  13194. (defun org-set-autofill-regexps ()
  13195. (interactive)
  13196. ;; In the paragraph separator we include headlines, because filling
  13197. ;; text in a line directly attached to a headline would otherwise
  13198. ;; fill the headline as well.
  13199. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13200. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13201. ;; The paragraph starter includes hand-formatted lists.
  13202. (org-set-local 'paragraph-start
  13203. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13204. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13205. ;; But only if the user has not turned off tables or fixed-width regions
  13206. (org-set-local
  13207. 'auto-fill-inhibit-regexp
  13208. (concat "\\*+ \\|#\\+"
  13209. "\\|[ \t]*" org-keyword-time-regexp
  13210. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13211. (concat
  13212. "\\|[ \t]*["
  13213. (if org-enable-table-editor "|" "")
  13214. (if org-enable-fixed-width-editor ":" "")
  13215. "]"))))
  13216. ;; We use our own fill-paragraph function, to make sure that tables
  13217. ;; and fixed-width regions are not wrapped. That function will pass
  13218. ;; through to `fill-paragraph' when appropriate.
  13219. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13220. ; Adaptive filling: To get full control, first make sure that
  13221. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13222. (org-set-local 'adaptive-fill-regexp "\000")
  13223. (org-set-local 'adaptive-fill-function
  13224. 'org-adaptive-fill-function)
  13225. (org-set-local
  13226. 'align-mode-rules-list
  13227. '((org-in-buffer-settings
  13228. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13229. (modes . '(org-mode))))))
  13230. (defun org-fill-paragraph (&optional justify)
  13231. "Re-align a table, pass through to fill-paragraph if no table."
  13232. (let ((table-p (org-at-table-p))
  13233. (table.el-p (org-at-table.el-p)))
  13234. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13235. (save-excursion (goto-char (point-at-bol))
  13236. (looking-at outline-regexp)))
  13237. t) ; skip headlines
  13238. (table.el-p t) ; skip table.el tables
  13239. (table-p (org-table-align) t) ; align org-mode tables
  13240. (t nil)))) ; call paragraph-fill
  13241. ;; For reference, this is the default value of adaptive-fill-regexp
  13242. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13243. (defun org-adaptive-fill-function ()
  13244. "Return a fill prefix for org-mode files.
  13245. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13246. work correctly."
  13247. (cond ((looking-at "#[ \t]+")
  13248. (match-string 0))
  13249. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13250. (save-excursion
  13251. (if (> (match-end 1) (+ (match-beginning 1)
  13252. org-description-max-indent))
  13253. (goto-char (+ (match-beginning 1) 5))
  13254. (goto-char (match-end 0)))
  13255. (make-string (current-column) ?\ )))
  13256. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13257. (save-excursion
  13258. (goto-char (match-end 0))
  13259. (make-string (current-column) ?\ )))
  13260. (t nil)))
  13261. ;;; Other stuff.
  13262. (defun org-toggle-fixed-width-section (arg)
  13263. "Toggle the fixed-width export.
  13264. If there is no active region, the QUOTE keyword at the current headline is
  13265. inserted or removed. When present, it causes the text between this headline
  13266. and the next to be exported as fixed-width text, and unmodified.
  13267. If there is an active region, this command adds or removes a colon as the
  13268. first character of this line. If the first character of a line is a colon,
  13269. this line is also exported in fixed-width font."
  13270. (interactive "P")
  13271. (let* ((cc 0)
  13272. (regionp (org-region-active-p))
  13273. (beg (if regionp (region-beginning) (point)))
  13274. (end (if regionp (region-end)))
  13275. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13276. (case-fold-search nil)
  13277. (re "[ \t]*\\(:\\)")
  13278. off)
  13279. (if regionp
  13280. (save-excursion
  13281. (goto-char beg)
  13282. (setq cc (current-column))
  13283. (beginning-of-line 1)
  13284. (setq off (looking-at re))
  13285. (while (> nlines 0)
  13286. (setq nlines (1- nlines))
  13287. (beginning-of-line 1)
  13288. (cond
  13289. (arg
  13290. (org-move-to-column cc t)
  13291. (insert ":\n")
  13292. (forward-line -1))
  13293. ((and off (looking-at re))
  13294. (replace-match "" t t nil 1))
  13295. ((not off) (org-move-to-column cc t) (insert ":")))
  13296. (forward-line 1)))
  13297. (save-excursion
  13298. (org-back-to-heading)
  13299. (if (looking-at (concat outline-regexp
  13300. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13301. (replace-match "" t t nil 1)
  13302. (if (looking-at outline-regexp)
  13303. (progn
  13304. (goto-char (match-end 0))
  13305. (insert org-quote-string " "))))))))
  13306. ;;;; Functions extending outline functionality
  13307. (defun org-beginning-of-line (&optional arg)
  13308. "Go to the beginning of the current line. If that is invisible, continue
  13309. to a visible line beginning. This makes the function of C-a more intuitive.
  13310. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13311. first attempt, and only move to after the tags when the cursor is already
  13312. beyond the end of the headline."
  13313. (interactive "P")
  13314. (let ((pos (point)) refpos)
  13315. (beginning-of-line 1)
  13316. (if (bobp)
  13317. nil
  13318. (backward-char 1)
  13319. (if (org-invisible-p)
  13320. (while (and (not (bobp)) (org-invisible-p))
  13321. (backward-char 1)
  13322. (beginning-of-line 1))
  13323. (forward-char 1)))
  13324. (when org-special-ctrl-a/e
  13325. (cond
  13326. ((and (looking-at org-complex-heading-regexp)
  13327. (= (char-after (match-end 1)) ?\ ))
  13328. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13329. (point-at-eol)))
  13330. (goto-char
  13331. (if (eq org-special-ctrl-a/e t)
  13332. (cond ((> pos refpos) refpos)
  13333. ((= pos (point)) refpos)
  13334. (t (point)))
  13335. (cond ((> pos (point)) (point))
  13336. ((not (eq last-command this-command)) (point))
  13337. (t refpos)))))
  13338. ((org-at-item-p)
  13339. (goto-char
  13340. (if (eq org-special-ctrl-a/e t)
  13341. (cond ((> pos (match-end 4)) (match-end 4))
  13342. ((= pos (point)) (match-end 4))
  13343. (t (point)))
  13344. (cond ((> pos (point)) (point))
  13345. ((not (eq last-command this-command)) (point))
  13346. (t (match-end 4))))))))
  13347. (org-no-warnings
  13348. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13349. (defun org-end-of-line (&optional arg)
  13350. "Go to the end of the line.
  13351. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13352. first attempt, and only move to after the tags when the cursor is already
  13353. beyond the end of the headline."
  13354. (interactive "P")
  13355. (if (or (not org-special-ctrl-a/e)
  13356. (not (org-on-heading-p)))
  13357. (end-of-line arg)
  13358. (let ((pos (point)))
  13359. (beginning-of-line 1)
  13360. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13361. (if (eq org-special-ctrl-a/e t)
  13362. (if (or (< pos (match-beginning 1))
  13363. (= pos (match-end 0)))
  13364. (goto-char (match-beginning 1))
  13365. (goto-char (match-end 0)))
  13366. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13367. (goto-char (match-end 0))
  13368. (goto-char (match-beginning 1))))
  13369. (end-of-line arg))))
  13370. (org-no-warnings
  13371. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13372. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13373. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13374. (defun org-kill-line (&optional arg)
  13375. "Kill line, to tags or end of line."
  13376. (interactive "P")
  13377. (cond
  13378. ((or (not org-special-ctrl-k)
  13379. (bolp)
  13380. (not (org-on-heading-p)))
  13381. (call-interactively 'kill-line))
  13382. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13383. (kill-region (point) (match-beginning 1))
  13384. (org-set-tags nil t))
  13385. (t (kill-region (point) (point-at-eol)))))
  13386. (define-key org-mode-map "\C-k" 'org-kill-line)
  13387. (defun org-yank (&optional arg)
  13388. "Yank. If the kill is a subtree, treat it specially.
  13389. This command will look at the current kill and check if is a single
  13390. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13391. cursor is at the beginning of a line or after the stars of a currently
  13392. empty headline, then the yank is handled specially. How exactly depends
  13393. on the value of the following variables, both set by default.
  13394. org-yank-folded-subtrees
  13395. When set, the subtree(s) will be folded after insertion, but only
  13396. if doing so would now swallow text after the yanked text.
  13397. org-yank-adjusted-subtrees
  13398. When set, the subtree will be promoted or demoted in order to
  13399. fit into the local outline tree structure, which means that the level
  13400. will be adjusted so that it becomes the smaller one of the two
  13401. *visible* surrounding headings.
  13402. Any prefix to this command will cause `yank' to be called directly with
  13403. no special treatment. In particular, a simple `C-u' prefix will just
  13404. plainly yank the text as it is.
  13405. \[1] Basically, the test checks if the first non-white line is a heading
  13406. and if there are no other headings with fewer stars."
  13407. (interactive "P")
  13408. (setq this-command 'yank)
  13409. (if arg
  13410. (call-interactively 'yank)
  13411. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13412. (and (org-kill-is-subtree-p)
  13413. (or (bolp)
  13414. (and (looking-at "[ \t]*$")
  13415. (string-match
  13416. "\\`\\*+\\'"
  13417. (buffer-substring (point-at-bol) (point)))))))
  13418. swallowp)
  13419. (cond
  13420. ((and subtreep org-yank-folded-subtrees)
  13421. (let ((beg (point))
  13422. end)
  13423. (if (and subtreep org-yank-adjusted-subtrees)
  13424. (org-paste-subtree nil nil 'for-yank)
  13425. (call-interactively 'yank))
  13426. (setq end (point))
  13427. (goto-char beg)
  13428. (when (and (bolp) subtreep
  13429. (not (setq swallowp
  13430. (org-yank-folding-would-swallow-text beg end))))
  13431. (or (looking-at outline-regexp)
  13432. (re-search-forward (concat "^" outline-regexp) end t))
  13433. (while (and (< (point) end) (looking-at outline-regexp))
  13434. (hide-subtree)
  13435. (org-cycle-show-empty-lines 'folded)
  13436. (condition-case nil
  13437. (outline-forward-same-level 1)
  13438. (error (goto-char end)))))
  13439. (when swallowp
  13440. (message
  13441. "Yanked text not folded because that would swallow text"))
  13442. (goto-char end)
  13443. (skip-chars-forward " \t\n\r")
  13444. (beginning-of-line 1)
  13445. (push-mark beg 'nomsg)))
  13446. ((and subtreep org-yank-adjusted-subtrees)
  13447. (let ((beg (point-at-bol)))
  13448. (org-paste-subtree nil nil 'for-yank)
  13449. (push-mark beg 'nomsg)))
  13450. (t
  13451. (call-interactively 'yank))))))
  13452. (defun org-yank-folding-would-swallow-text (beg end)
  13453. "Would hide-subtree at BEG swallow any text after END?"
  13454. (let (level)
  13455. (save-excursion
  13456. (goto-char beg)
  13457. (when (or (looking-at outline-regexp)
  13458. (re-search-forward (concat "^" outline-regexp) end t))
  13459. (setq level (org-outline-level)))
  13460. (goto-char end)
  13461. (skip-chars-forward " \t\r\n\v\f")
  13462. (if (or (eobp)
  13463. (and (bolp) (looking-at org-outline-regexp)
  13464. (<= (org-outline-level) level)))
  13465. nil ; Nothing would be swallowed
  13466. t)))) ; something would swallow
  13467. (define-key org-mode-map "\C-y" 'org-yank)
  13468. (defun org-invisible-p ()
  13469. "Check if point is at a character currently not visible."
  13470. ;; Early versions of noutline don't have `outline-invisible-p'.
  13471. (if (fboundp 'outline-invisible-p)
  13472. (outline-invisible-p)
  13473. (get-char-property (point) 'invisible)))
  13474. (defun org-invisible-p2 ()
  13475. "Check if point is at a character currently not visible."
  13476. (save-excursion
  13477. (if (and (eolp) (not (bobp))) (backward-char 1))
  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-back-to-heading (&optional invisible-ok)
  13483. "Call `outline-back-to-heading', but provide a better error message."
  13484. (condition-case nil
  13485. (outline-back-to-heading invisible-ok)
  13486. (error (error "Before first headline at position %d in buffer %s"
  13487. (point) (current-buffer)))))
  13488. (defun org-before-first-heading-p ()
  13489. "Before first heading?"
  13490. (save-excursion
  13491. (null (re-search-backward "^\\*+ " nil t))))
  13492. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13493. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13494. (defun org-at-heading-or-item-p ()
  13495. (or (org-on-heading-p) (org-at-item-p)))
  13496. (defun org-on-target-p ()
  13497. (or (org-in-regexp org-radio-target-regexp)
  13498. (org-in-regexp org-target-regexp)))
  13499. (defun org-up-heading-all (arg)
  13500. "Move to the heading line of which the present line is a subheading.
  13501. This function considers both visible and invisible heading lines.
  13502. With argument, move up ARG levels."
  13503. (if (fboundp 'outline-up-heading-all)
  13504. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13505. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13506. (defun org-up-heading-safe ()
  13507. "Move to the heading line of which the present line is a subheading.
  13508. This version will not throw an error. It will return the level of the
  13509. headline found, or nil if no higher level is found."
  13510. (let (start-level re)
  13511. (org-back-to-heading t)
  13512. (setq start-level (funcall outline-level))
  13513. (if (equal start-level 1)
  13514. nil
  13515. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13516. (if (re-search-backward re nil t)
  13517. (funcall outline-level)))))
  13518. (defun org-first-sibling-p ()
  13519. "Is this heading the first child of its parents?"
  13520. (interactive)
  13521. (let ((re (concat "^" outline-regexp))
  13522. level l)
  13523. (unless (org-at-heading-p t)
  13524. (error "Not at a heading"))
  13525. (setq level (funcall outline-level))
  13526. (save-excursion
  13527. (if (not (re-search-backward re nil t))
  13528. t
  13529. (setq l (funcall outline-level))
  13530. (< l level)))))
  13531. (defun org-goto-sibling (&optional previous)
  13532. "Goto the next sibling, even if it is invisible.
  13533. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13534. when a sibling was found. When none is found, return nil and don't
  13535. move point."
  13536. (let ((fun (if previous 're-search-backward 're-search-forward))
  13537. (pos (point))
  13538. (re (concat "^" outline-regexp))
  13539. level l)
  13540. (when (condition-case nil (org-back-to-heading t) (error nil))
  13541. (setq level (funcall outline-level))
  13542. (catch 'exit
  13543. (or previous (forward-char 1))
  13544. (while (funcall fun re nil t)
  13545. (setq l (funcall outline-level))
  13546. (when (< l level) (goto-char pos) (throw 'exit nil))
  13547. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13548. (goto-char pos)
  13549. nil))))
  13550. (defun org-show-siblings ()
  13551. "Show all siblings of the current headline."
  13552. (save-excursion
  13553. (while (org-goto-sibling) (org-flag-heading nil)))
  13554. (save-excursion
  13555. (while (org-goto-sibling 'previous)
  13556. (org-flag-heading nil))))
  13557. (defun org-show-hidden-entry ()
  13558. "Show an entry where even the heading is hidden."
  13559. (save-excursion
  13560. (org-show-entry)))
  13561. (defun org-flag-heading (flag &optional entry)
  13562. "Flag the current heading. FLAG non-nil means make invisible.
  13563. When ENTRY is non-nil, show the entire entry."
  13564. (save-excursion
  13565. (org-back-to-heading t)
  13566. ;; Check if we should show the entire entry
  13567. (if entry
  13568. (progn
  13569. (org-show-entry)
  13570. (save-excursion
  13571. (and (outline-next-heading)
  13572. (org-flag-heading nil))))
  13573. (outline-flag-region (max (point-min) (1- (point)))
  13574. (save-excursion (outline-end-of-heading) (point))
  13575. flag))))
  13576. (defun org-forward-same-level (arg)
  13577. "Move forward to the ARG'th subheading at same level as this one.
  13578. Stop at the first and last subheadings of a superior heading.
  13579. This is like outline-forward-same-level, but invisible headings are ok."
  13580. (interactive "p")
  13581. (org-back-to-heading t)
  13582. (while (> arg 0)
  13583. (let ((point-to-move-to (save-excursion
  13584. (org-get-next-sibling))))
  13585. (if point-to-move-to
  13586. (progn
  13587. (goto-char point-to-move-to)
  13588. (setq arg (1- arg)))
  13589. (progn
  13590. (setq arg 0)
  13591. (error "No following same-level heading"))))))
  13592. (defun org-get-next-sibling ()
  13593. "Move to next heading of the same level, and return point.
  13594. If there is no such heading, return nil.
  13595. This is like outline-next-sibling, but invisible headings are ok."
  13596. (let ((level (funcall outline-level)))
  13597. (outline-next-heading)
  13598. (while (and (not (eobp)) (> (funcall outline-level) level))
  13599. (outline-next-heading))
  13600. (if (or (eobp) (< (funcall outline-level) level))
  13601. nil
  13602. (point))))
  13603. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13604. ;; This is an exact copy of the original function, but it uses
  13605. ;; `org-back-to-heading', to make it work also in invisible
  13606. ;; trees. And is uses an invisible-OK argument.
  13607. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13608. (org-back-to-heading invisible-OK)
  13609. (let ((first t)
  13610. (level (funcall outline-level)))
  13611. (while (and (not (eobp))
  13612. (or first (> (funcall outline-level) level)))
  13613. (setq first nil)
  13614. (outline-next-heading))
  13615. (unless to-heading
  13616. (if (memq (preceding-char) '(?\n ?\^M))
  13617. (progn
  13618. ;; Go to end of line before heading
  13619. (forward-char -1)
  13620. (if (memq (preceding-char) '(?\n ?\^M))
  13621. ;; leave blank line before heading
  13622. (forward-char -1))))))
  13623. (point))
  13624. (defun org-show-subtree ()
  13625. "Show everything after this heading at deeper levels."
  13626. (outline-flag-region
  13627. (point)
  13628. (save-excursion
  13629. (outline-end-of-subtree) (outline-next-heading) (point))
  13630. nil))
  13631. (defun org-show-entry ()
  13632. "Show the body directly following this heading.
  13633. Show the heading too, if it is currently invisible."
  13634. (interactive)
  13635. (save-excursion
  13636. (condition-case nil
  13637. (progn
  13638. (org-back-to-heading t)
  13639. (outline-flag-region
  13640. (max (point-min) (1- (point)))
  13641. (save-excursion
  13642. (re-search-forward
  13643. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13644. (or (match-beginning 1) (point-max)))
  13645. nil))
  13646. (error nil))))
  13647. (defun org-make-options-regexp (kwds)
  13648. "Make a regular expression for keyword lines."
  13649. (concat
  13650. "^"
  13651. "#?[ \t]*\\+\\("
  13652. (mapconcat 'regexp-quote kwds "\\|")
  13653. "\\):[ \t]*"
  13654. "\\(.+\\)"))
  13655. ;; Make isearch reveal the necessary context
  13656. (defun org-isearch-end ()
  13657. "Reveal context after isearch exits."
  13658. (when isearch-success ; only if search was successful
  13659. (if (featurep 'xemacs)
  13660. ;; Under XEmacs, the hook is run in the correct place,
  13661. ;; we directly show the context.
  13662. (org-show-context 'isearch)
  13663. ;; In Emacs the hook runs *before* restoring the overlays.
  13664. ;; So we have to use a one-time post-command-hook to do this.
  13665. ;; (Emacs 22 has a special variable, see function `org-mode')
  13666. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13667. isearch-mode-end-hook-quit)
  13668. ;; Only when the isearch was not quitted.
  13669. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13670. 'append 'local)))))
  13671. (defun org-isearch-post-command ()
  13672. "Remove self from hook, and show context."
  13673. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13674. (org-show-context 'isearch))
  13675. ;;;; Integration with and fixes for other packages
  13676. ;;; Imenu support
  13677. (defvar org-imenu-markers nil
  13678. "All markers currently used by Imenu.")
  13679. (make-variable-buffer-local 'org-imenu-markers)
  13680. (defun org-imenu-new-marker (&optional pos)
  13681. "Return a new marker for use by Imenu, and remember the marker."
  13682. (let ((m (make-marker)))
  13683. (move-marker m (or pos (point)))
  13684. (push m org-imenu-markers)
  13685. m))
  13686. (defun org-imenu-get-tree ()
  13687. "Produce the index for Imenu."
  13688. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13689. (setq org-imenu-markers nil)
  13690. (let* ((n org-imenu-depth)
  13691. (re (concat "^" outline-regexp))
  13692. (subs (make-vector (1+ n) nil))
  13693. (last-level 0)
  13694. m level head)
  13695. (save-excursion
  13696. (save-restriction
  13697. (widen)
  13698. (goto-char (point-max))
  13699. (while (re-search-backward re nil t)
  13700. (setq level (org-reduced-level (funcall outline-level)))
  13701. (when (<= level n)
  13702. (looking-at org-complex-heading-regexp)
  13703. (setq head (org-link-display-format
  13704. (org-match-string-no-properties 4))
  13705. m (org-imenu-new-marker))
  13706. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13707. (if (>= level last-level)
  13708. (push (cons head m) (aref subs level))
  13709. (push (cons head (aref subs (1+ level))) (aref subs level))
  13710. (loop for i from (1+ level) to n do (aset subs i nil)))
  13711. (setq last-level level)))))
  13712. (aref subs 1)))
  13713. (eval-after-load "imenu"
  13714. '(progn
  13715. (add-hook 'imenu-after-jump-hook
  13716. (lambda ()
  13717. (if (eq major-mode 'org-mode)
  13718. (org-show-context 'org-goto))))))
  13719. (defun org-link-display-format (link)
  13720. "Replace a link with either the description, or the link target
  13721. if no description is present"
  13722. (save-match-data
  13723. (if (string-match org-bracket-link-analytic-regexp link)
  13724. (replace-match (or (match-string 5 link)
  13725. (concat (match-string 1 link)
  13726. (match-string 3 link)))
  13727. nil nil link)
  13728. link)))
  13729. ;; Speedbar support
  13730. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13731. "Overlay marking the agenda restriction line in speedbar.")
  13732. (org-overlay-put org-speedbar-restriction-lock-overlay
  13733. 'face 'org-agenda-restriction-lock)
  13734. (org-overlay-put org-speedbar-restriction-lock-overlay
  13735. 'help-echo "Agendas are currently limited to this item.")
  13736. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13737. (defun org-speedbar-set-agenda-restriction ()
  13738. "Restrict future agenda commands to the location at point in speedbar.
  13739. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13740. (interactive)
  13741. (require 'org-agenda)
  13742. (let (p m tp np dir txt)
  13743. (cond
  13744. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13745. 'org-imenu t))
  13746. (setq m (get-text-property p 'org-imenu-marker))
  13747. (save-excursion
  13748. (save-restriction
  13749. (set-buffer (marker-buffer m))
  13750. (goto-char m)
  13751. (org-agenda-set-restriction-lock 'subtree))))
  13752. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13753. 'speedbar-function 'speedbar-find-file))
  13754. (setq tp (previous-single-property-change
  13755. (1+ p) 'speedbar-function)
  13756. np (next-single-property-change
  13757. tp 'speedbar-function)
  13758. dir (speedbar-line-directory)
  13759. txt (buffer-substring-no-properties (or tp (point-min))
  13760. (or np (point-max))))
  13761. (save-excursion
  13762. (save-restriction
  13763. (set-buffer (find-file-noselect
  13764. (let ((default-directory dir))
  13765. (expand-file-name txt))))
  13766. (unless (org-mode-p)
  13767. (error "Cannot restrict to non-Org-mode file"))
  13768. (org-agenda-set-restriction-lock 'file))))
  13769. (t (error "Don't know how to restrict Org-mode's agenda")))
  13770. (org-move-overlay org-speedbar-restriction-lock-overlay
  13771. (point-at-bol) (point-at-eol))
  13772. (setq current-prefix-arg nil)
  13773. (org-agenda-maybe-redo)))
  13774. (eval-after-load "speedbar"
  13775. '(progn
  13776. (speedbar-add-supported-extension ".org")
  13777. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13778. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13779. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13780. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13781. (add-hook 'speedbar-visiting-tag-hook
  13782. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13783. ;;; Fixes and Hacks for problems with other packages
  13784. ;; Make flyspell not check words in links, to not mess up our keymap
  13785. (defun org-mode-flyspell-verify ()
  13786. "Don't let flyspell put overlays at active buttons."
  13787. (not (get-text-property (point) 'keymap)))
  13788. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13789. (eval-after-load "bookmark"
  13790. '(if (boundp 'bookmark-after-jump-hook)
  13791. ;; We can use the hook
  13792. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13793. ;; Hook not available, use advice
  13794. (defadvice bookmark-jump (after org-make-visible activate)
  13795. "Make the position visible."
  13796. (org-bookmark-jump-unhide))))
  13797. ;; Make sure saveplace show the location if it was hidden
  13798. (eval-after-load "saveplace"
  13799. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13800. "Make the position visible."
  13801. (org-bookmark-jump-unhide)))
  13802. (defun org-bookmark-jump-unhide ()
  13803. "Unhide the current position, to show the bookmark location."
  13804. (and (org-mode-p)
  13805. (or (org-invisible-p)
  13806. (save-excursion (goto-char (max (point-min) (1- (point))))
  13807. (org-invisible-p)))
  13808. (org-show-context 'bookmark-jump)))
  13809. ;; Make session.el ignore our circular variable
  13810. (eval-after-load "session"
  13811. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13812. ;;;; Experimental code
  13813. (defun org-closed-in-range ()
  13814. "Sparse tree of items closed in a certain time range.
  13815. Still experimental, may disappear in the future."
  13816. (interactive)
  13817. ;; Get the time interval from the user.
  13818. (let* ((time1 (time-to-seconds
  13819. (org-read-date nil 'to-time nil "Starting date: ")))
  13820. (time2 (time-to-seconds
  13821. (org-read-date nil 'to-time nil "End date:")))
  13822. ;; callback function
  13823. (callback (lambda ()
  13824. (let ((time
  13825. (time-to-seconds
  13826. (apply 'encode-time
  13827. (org-parse-time-string
  13828. (match-string 1))))))
  13829. ;; check if time in interval
  13830. (and (>= time time1) (<= time time2))))))
  13831. ;; make tree, check each match with the callback
  13832. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13833. ;;;; Finish up
  13834. (provide 'org)
  13835. (run-hooks 'org-load-hook)
  13836. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13837. ;;; org.el ends here