org.el 567 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263
  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.20h
  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.20h"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  163. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  164. (const :tag "C eval: Include command output as text" org-eval)
  165. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  166. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  167. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  168. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  169. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  170. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  171. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  172. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  173. (const :tag "C registry: A registry for Org links" org-registry)
  174. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  175. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  176. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  177. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  178. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  179. (defcustom org-support-shift-select nil
  180. "Non-nil means, make shift-cursor commands select text when possible.
  181. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  182. selecting a region, or enlarge thusly regions started in this way.
  183. In Org-mode, in special contexts, these same keys are used for other
  184. purposes, important enough to compete with shift selection. Org tries
  185. to balance these needs by supporting `shift-select-mode' outside these
  186. special contexts, under control of this variable.
  187. The default of this variable is nil, to avoid confusing behavior. Shifted
  188. cursor keys will then execute Org commands in the following contexts:
  189. - on a headline, changing TODO state (left/right) and priority (up/down)
  190. - on a time stamp, changing the time
  191. - in a plain list item, changing the bullet type
  192. - in a property definition line, switching between allowed values
  193. - in the BEGIN line of a clock table (changing the time block).
  194. Outside these contexts, the commands will throw an error.
  195. When this variable is t and the cursor is not in a special context,
  196. Org-mode will support shift-selection for making and enlarging regions.
  197. To make this more effective, the bullet cycling will no longer happen
  198. anywhere in an item line, but only if the cursor is exactly on the bullet.
  199. If you set this variable to the symbol `always', then the keys
  200. will not be special in headlines, property lines, and item lines, to make
  201. shift selection work there as well. If this is what you want, you can
  202. use the following alternative commands: `C-c C-t' and `C-c ,' to
  203. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  204. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  205. edited by hand or in column view.
  206. However, when the cursor is on a timestamp, shift-cursor commands
  207. will still edit the time stamp - this is just too good to give up.
  208. XEmacs user should have this variable set to nil, because shift-select-mode
  209. is Emacs 23 only."
  210. :group 'org
  211. :type '(choice
  212. (const :tag "Never" nil)
  213. (const :tag "When outside special context" t)
  214. (const :tag "Everywhere except timestamps" always)))
  215. (defgroup org-startup nil
  216. "Options concerning startup of Org-mode."
  217. :tag "Org Startup"
  218. :group 'org)
  219. (defcustom org-startup-folded t
  220. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  221. This can also be configured on a per-file basis by adding one of
  222. the following lines anywhere in the buffer:
  223. #+STARTUP: fold
  224. #+STARTUP: nofold
  225. #+STARTUP: content"
  226. :group 'org-startup
  227. :type '(choice
  228. (const :tag "nofold: show all" nil)
  229. (const :tag "fold: overview" t)
  230. (const :tag "content: all headlines" content)))
  231. (defcustom org-startup-truncated t
  232. "Non-nil means, entering Org-mode will set `truncate-lines'.
  233. This is useful since some lines containing links can be very long and
  234. uninteresting. Also tables look terrible when wrapped."
  235. :group 'org-startup
  236. :type 'boolean)
  237. (defcustom org-startup-align-all-tables nil
  238. "Non-nil means, align all tables when visiting a file.
  239. This is useful when the column width in tables is forced with <N> cookies
  240. in table fields. Such tables will look correct only after the first re-align.
  241. This can also be configured on a per-file basis by adding one of
  242. the following lines anywhere in the buffer:
  243. #+STARTUP: align
  244. #+STARTUP: noalign"
  245. :group 'org-startup
  246. :type 'boolean)
  247. (defcustom org-insert-mode-line-in-empty-file nil
  248. "Non-nil means insert the first line setting Org-mode in empty files.
  249. When the function `org-mode' is called interactively in an empty file, this
  250. normally means that the file name does not automatically trigger Org-mode.
  251. To ensure that the file will always be in Org-mode in the future, a
  252. line enforcing Org-mode will be inserted into the buffer, if this option
  253. has been set."
  254. :group 'org-startup
  255. :type 'boolean)
  256. (defcustom org-replace-disputed-keys nil
  257. "Non-nil means use alternative key bindings for some keys.
  258. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  259. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  260. If you want to use Org-mode together with one of these other modes,
  261. or more generally if you would like to move some Org-mode commands to
  262. other keys, set this variable and configure the keys with the variable
  263. `org-disputed-keys'.
  264. This option is only relevant at load-time of Org-mode, and must be set
  265. *before* org.el is loaded. Changing it requires a restart of Emacs to
  266. become effective."
  267. :group 'org-startup
  268. :type 'boolean)
  269. (defcustom org-use-extra-keys nil
  270. "Non-nil means use extra key sequence definitions for certain
  271. commands. This happens automatically if you run XEmacs or if
  272. window-system is nil. This variable lets you do the same
  273. manually. You must set it before loading org.
  274. Example: on Carbon Emacs 22 running graphically, with an external
  275. keyboard on a Powerbook, the default way of setting M-left might
  276. not work for either Alt or ESC. Setting this variable will make
  277. it work for ESC."
  278. :group 'org-startup
  279. :type 'boolean)
  280. (if (fboundp 'defvaralias)
  281. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  282. (defcustom org-disputed-keys
  283. '(([(shift up)] . [(meta p)])
  284. ([(shift down)] . [(meta n)])
  285. ([(shift left)] . [(meta -)])
  286. ([(shift right)] . [(meta +)])
  287. ([(control shift right)] . [(meta shift +)])
  288. ([(control shift left)] . [(meta shift -)]))
  289. "Keys for which Org-mode and other modes compete.
  290. This is an alist, cars are the default keys, second element specifies
  291. the alternative to use when `org-replace-disputed-keys' is t.
  292. Keys can be specified in any syntax supported by `define-key'.
  293. The value of this option takes effect only at Org-mode's startup,
  294. therefore you'll have to restart Emacs to apply it after changing."
  295. :group 'org-startup
  296. :type 'alist)
  297. (defun org-key (key)
  298. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  299. Or return the original if not disputed."
  300. (if org-replace-disputed-keys
  301. (let* ((nkey (key-description key))
  302. (x (org-find-if (lambda (x)
  303. (equal (key-description (car x)) nkey))
  304. org-disputed-keys)))
  305. (if x (cdr x) key))
  306. key))
  307. (defun org-find-if (predicate seq)
  308. (catch 'exit
  309. (while seq
  310. (if (funcall predicate (car seq))
  311. (throw 'exit (car seq))
  312. (pop seq)))))
  313. (defun org-defkey (keymap key def)
  314. "Define a key, possibly translated, as returned by `org-key'."
  315. (define-key keymap (org-key key) def))
  316. (defcustom org-ellipsis nil
  317. "The ellipsis to use in the Org-mode outline.
  318. When nil, just use the standard three dots. When a string, use that instead,
  319. When a face, use the standard 3 dots, but with the specified face.
  320. The change affects only Org-mode (which will then use its own display table).
  321. Changing this requires executing `M-x org-mode' in a buffer to become
  322. effective."
  323. :group 'org-startup
  324. :type '(choice (const :tag "Default" nil)
  325. (face :tag "Face" :value org-warning)
  326. (string :tag "String" :value "...#")))
  327. (defvar org-display-table nil
  328. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  329. (defgroup org-keywords nil
  330. "Keywords in Org-mode."
  331. :tag "Org Keywords"
  332. :group 'org)
  333. (defcustom org-deadline-string "DEADLINE:"
  334. "String to mark deadline entries.
  335. A deadline is this string, followed by a time stamp. Should be a word,
  336. terminated by a colon. You can insert a schedule keyword and
  337. a timestamp with \\[org-deadline].
  338. Changes become only effective after restarting Emacs."
  339. :group 'org-keywords
  340. :type 'string)
  341. (defcustom org-scheduled-string "SCHEDULED:"
  342. "String to mark scheduled TODO entries.
  343. A schedule is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-schedule].
  346. Changes become only effective after restarting Emacs."
  347. :group 'org-keywords
  348. :type 'string)
  349. (defcustom org-closed-string "CLOSED:"
  350. "String used as the prefix for timestamps logging closing a TODO entry."
  351. :group 'org-keywords
  352. :type 'string)
  353. (defcustom org-clock-string "CLOCK:"
  354. "String used as prefix for timestamps clocking work hours on an item."
  355. :group 'org-keywords
  356. :type 'string)
  357. (defcustom org-comment-string "COMMENT"
  358. "Entries starting with this keyword will never be exported.
  359. An entry can be toggled between COMMENT and normal with
  360. \\[org-toggle-comment].
  361. Changes become only effective after restarting Emacs."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-quote-string "QUOTE"
  365. "Entries starting with this keyword will be exported in fixed-width font.
  366. Quoting applies only to the text in the entry following the headline, and does
  367. not extend beyond the next headline, even if that is lower level.
  368. An entry can be toggled between QUOTE and normal with
  369. \\[org-toggle-fixed-width-section]."
  370. :group 'org-keywords
  371. :type 'string)
  372. (defconst org-repeat-re
  373. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  374. "Regular expression for specifying repeated events.
  375. After a match, group 1 contains the repeat expression.")
  376. (defgroup org-structure nil
  377. "Options concerning the general structure of Org-mode files."
  378. :tag "Org Structure"
  379. :group 'org)
  380. (defgroup org-reveal-location nil
  381. "Options about how to make context of a location visible."
  382. :tag "Org Reveal Location"
  383. :group 'org-structure)
  384. (defconst org-context-choice
  385. '(choice
  386. (const :tag "Always" t)
  387. (const :tag "Never" nil)
  388. (repeat :greedy t :tag "Individual contexts"
  389. (cons
  390. (choice :tag "Context"
  391. (const agenda)
  392. (const org-goto)
  393. (const occur-tree)
  394. (const tags-tree)
  395. (const link-search)
  396. (const mark-goto)
  397. (const bookmark-jump)
  398. (const isearch)
  399. (const default))
  400. (boolean))))
  401. "Contexts for the reveal options.")
  402. (defcustom org-show-hierarchy-above '((default . t))
  403. "Non-nil means, show full hierarchy when revealing a location.
  404. Org-mode often shows locations in an org-mode file which might have
  405. been invisible before. When this is set, the hierarchy of headings
  406. above the exposed location is shown.
  407. Turning this off for example for sparse trees makes them very compact.
  408. Instead of t, this can also be an alist specifying this option for different
  409. contexts. Valid contexts are
  410. agenda when exposing an entry from the agenda
  411. org-goto when using the command `org-goto' on key C-c C-j
  412. occur-tree when using the command `org-occur' on key C-c /
  413. tags-tree when constructing a sparse tree based on tags matches
  414. link-search when exposing search matches associated with a link
  415. mark-goto when exposing the jump goal of a mark
  416. bookmark-jump when exposing a bookmark location
  417. isearch when exiting from an incremental search
  418. default default for all contexts not set explicitly"
  419. :group 'org-reveal-location
  420. :type org-context-choice)
  421. (defcustom org-show-following-heading '((default . nil))
  422. "Non-nil means, show following heading when revealing a location.
  423. Org-mode often shows locations in an org-mode file which might have
  424. been invisible before. When this is set, the heading following the
  425. match is shown.
  426. Turning this off for example for sparse trees makes them very compact,
  427. but makes it harder to edit the location of the match. In such a case,
  428. use the command \\[org-reveal] to show more context.
  429. Instead of t, this can also be an alist specifying this option for different
  430. contexts. See `org-show-hierarchy-above' for valid contexts."
  431. :group 'org-reveal-location
  432. :type org-context-choice)
  433. (defcustom org-show-siblings '((default . nil) (isearch t))
  434. "Non-nil means, show all sibling heading when revealing a location.
  435. Org-mode often shows locations in an org-mode file which might have
  436. been invisible before. When this is set, the sibling of the current entry
  437. heading are all made visible. If `org-show-hierarchy-above' is t,
  438. the same happens on each level of the hierarchy above the current entry.
  439. By default this is on for the isearch context, off for all other contexts.
  440. Turning this off for example for sparse trees makes them very compact,
  441. but makes it harder to edit the location of the match. In such a case,
  442. use the command \\[org-reveal] to show more context.
  443. Instead of t, this can also be an alist specifying this option for different
  444. contexts. See `org-show-hierarchy-above' for valid contexts."
  445. :group 'org-reveal-location
  446. :type org-context-choice)
  447. (defcustom org-show-entry-below '((default . nil))
  448. "Non-nil means, show the entry below a headline when revealing a location.
  449. Org-mode often shows locations in an org-mode file which might have
  450. been invisible before. When this is set, the text below the headline that is
  451. exposed is also shown.
  452. By default this is off for all contexts.
  453. Instead of t, this can also be an alist specifying this option for different
  454. contexts. See `org-show-hierarchy-above' for valid contexts."
  455. :group 'org-reveal-location
  456. :type org-context-choice)
  457. (defcustom org-indirect-buffer-display 'other-window
  458. "How should indirect tree buffers be displayed?
  459. This applies to indirect buffers created with the commands
  460. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  461. Valid values are:
  462. current-window Display in the current window
  463. other-window Just display in another window.
  464. dedicated-frame Create one new frame, and re-use it each time.
  465. new-frame Make a new frame each time. Note that in this case
  466. previously-made indirect buffers are kept, and you need to
  467. kill these buffers yourself."
  468. :group 'org-structure
  469. :group 'org-agenda-windows
  470. :type '(choice
  471. (const :tag "In current window" current-window)
  472. (const :tag "In current frame, other window" other-window)
  473. (const :tag "Each time a new frame" new-frame)
  474. (const :tag "One dedicated frame" dedicated-frame)))
  475. (defgroup org-cycle nil
  476. "Options concerning visibility cycling in Org-mode."
  477. :tag "Org Cycle"
  478. :group 'org-structure)
  479. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  480. "Names of drawers. Drawers are not opened by cycling on the headline above.
  481. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  482. this:
  483. :DRAWERNAME:
  484. .....
  485. :END:
  486. The drawer \"PROPERTIES\" is special for capturing properties through
  487. the property API.
  488. Drawers can be defined on the per-file basis with a line like:
  489. #+DRAWERS: HIDDEN STATE PROPERTIES"
  490. :group 'org-structure
  491. :type '(repeat (string :tag "Drawer Name")))
  492. (defcustom org-cycle-global-at-bob nil
  493. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  494. This makes it possible to do global cycling without having to use S-TAB or
  495. C-u TAB. For this special case to work, the first line of the buffer
  496. must not be a headline - it may be empty ot some other text. When used in
  497. this way, `org-cycle-hook' is disables temporarily, to make sure the
  498. cursor stays at the beginning of the buffer.
  499. When this option is nil, don't do anything special at the beginning
  500. of the buffer."
  501. :group 'org-cycle
  502. :type 'boolean)
  503. (defcustom org-cycle-emulate-tab t
  504. "Where should `org-cycle' emulate TAB.
  505. nil Never
  506. white Only in completely white lines
  507. whitestart Only at the beginning of lines, before the first non-white char
  508. t Everywhere except in headlines
  509. exc-hl-bol Everywhere except at the start of a headline
  510. If TAB is used in a place where it does not emulate TAB, the current subtree
  511. visibility is cycled."
  512. :group 'org-cycle
  513. :type '(choice (const :tag "Never" nil)
  514. (const :tag "Only in completely white lines" white)
  515. (const :tag "Before first char in a line" whitestart)
  516. (const :tag "Everywhere except in headlines" t)
  517. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  518. ))
  519. (defcustom org-cycle-separator-lines 2
  520. "Number of empty lines needed to keep an empty line between collapsed trees.
  521. If you leave an empty line between the end of a subtree and the following
  522. headline, this empty line is hidden when the subtree is folded.
  523. Org-mode will leave (exactly) one empty line visible if the number of
  524. empty lines is equal or larger to the number given in this variable.
  525. So the default 2 means, at least 2 empty lines after the end of a subtree
  526. are needed to produce free space between a collapsed subtree and the
  527. following headline.
  528. Special case: when 0, never leave empty lines in collapsed view."
  529. :group 'org-cycle
  530. :type 'integer)
  531. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  532. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  533. org-cycle-hide-drawers
  534. org-cycle-show-empty-lines
  535. org-optimize-window-after-visibility-change)
  536. "Hook that is run after `org-cycle' has changed the buffer visibility.
  537. The function(s) in this hook must accept a single argument which indicates
  538. the new state that was set by the most recent `org-cycle' command. The
  539. argument is a symbol. After a global state change, it can have the values
  540. `overview', `content', or `all'. After a local state change, it can have
  541. the values `folded', `children', or `subtree'."
  542. :group 'org-cycle
  543. :type 'hook)
  544. (defgroup org-edit-structure nil
  545. "Options concerning structure editing in Org-mode."
  546. :tag "Org Edit Structure"
  547. :group 'org-structure)
  548. (defcustom org-odd-levels-only nil
  549. "Non-nil means, skip even levels and only use odd levels for the outline.
  550. This has the effect that two stars are being added/taken away in
  551. promotion/demotion commands. It also influences how levels are
  552. handled by the exporters.
  553. Changing it requires restart of `font-lock-mode' to become effective
  554. for fontification also in regions already fontified.
  555. You may also set this on a per-file basis by adding one of the following
  556. lines to the buffer:
  557. #+STARTUP: odd
  558. #+STARTUP: oddeven"
  559. :group 'org-edit-structure
  560. :group 'org-font-lock
  561. :type 'boolean)
  562. (defcustom org-adapt-indentation t
  563. "Non-nil means, adapt indentation when promoting and demoting.
  564. When this is set and the *entire* text in an entry is indented, the
  565. indentation is increased by one space in a demotion command, and
  566. decreased by one in a promotion command. If any line in the entry
  567. body starts at column 0, indentation is not changed at all."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defcustom org-special-ctrl-a/e nil
  571. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  572. When t, `C-a' will bring back the cursor to the beginning of the
  573. headline text, i.e. after the stars and after a possible TODO keyword.
  574. In an item, this will be the position after the bullet.
  575. When the cursor is already at that position, another `C-a' will bring
  576. it to the beginning of the line.
  577. `C-e' will jump to the end of the headline, ignoring the presence of tags
  578. in the headline. A second `C-e' will then jump to the true end of the
  579. line, after any tags.
  580. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  581. and only a directly following, identical keypress will bring the cursor
  582. to the special positions."
  583. :group 'org-edit-structure
  584. :type '(choice
  585. (const :tag "off" nil)
  586. (const :tag "after bullet first" t)
  587. (const :tag "border first" reversed)))
  588. (if (fboundp 'defvaralias)
  589. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  590. (defcustom org-special-ctrl-k nil
  591. "Non-nil means `C-k' will behave specially in headlines.
  592. When nil, `C-k' will call the default `kill-line' command.
  593. When t, the following will happen while the cursor is in the headline:
  594. - When the cursor is at the beginning of a headline, kill the entire
  595. line and possible the folded subtree below the line.
  596. - When in the middle of the headline text, kill the headline up to the tags.
  597. - When after the headline text, kill the tags."
  598. :group 'org-edit-structure
  599. :type 'boolean)
  600. (defcustom org-yank-folded-subtrees t
  601. "Non-nil means, when yanking subtrees, fold them.
  602. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  603. it starts with a heading and all other headings in it are either children
  604. or siblings, then fold all the subtrees. However, do this only if no
  605. text after the yank would be swallowed into a folded tree by this action."
  606. :group 'org-edit-structure
  607. :type 'boolean)
  608. (defcustom org-yank-adjusted-subtrees t
  609. "Non-nil means, when yanking subtrees, adjust the level.
  610. With this setting, `org-paste-subtree' is used to insert the subtree, see
  611. this function for details."
  612. :group 'org-edit-structure
  613. :type 'boolean)
  614. (defcustom org-M-RET-may-split-line '((default . t))
  615. "Non-nil means, M-RET will split the line at the cursor position.
  616. When nil, it will go to the end of the line before making a
  617. new line.
  618. You may also set this option in a different way for different
  619. contexts. Valid contexts are:
  620. headline when creating a new headline
  621. item when creating a new item
  622. table in a table field
  623. default the value to be used for all contexts not explicitly
  624. customized"
  625. :group 'org-structure
  626. :group 'org-table
  627. :type '(choice
  628. (const :tag "Always" t)
  629. (const :tag "Never" nil)
  630. (repeat :greedy t :tag "Individual contexts"
  631. (cons
  632. (choice :tag "Context"
  633. (const headline)
  634. (const item)
  635. (const table)
  636. (const default))
  637. (boolean)))))
  638. (defcustom org-insert-heading-respect-content nil
  639. "Non-nil means, insert new headings after the current subtree.
  640. When nil, the new heading is created directly after the current line.
  641. The commands \\[org-insert-heading-respect-content] and
  642. \\[org-insert-todo-heading-respect-content] turn this variable on
  643. for the duration of the command."
  644. :group 'org-structure
  645. :type 'boolean)
  646. (defcustom org-blank-before-new-entry '((heading . auto)
  647. (plain-list-item . auto))
  648. "Should `org-insert-heading' leave a blank line before new heading/item?
  649. The value is an alist, with `heading' and `plain-list-item' as car,
  650. and a boolean flag as cdr."
  651. :group 'org-edit-structure
  652. :type '(list
  653. (cons (const heading)
  654. (choice (const :tag "Never" nil)
  655. (const :tag "Always" t)
  656. (const :tag "Auto" auto)))
  657. (cons (const plain-list-item)
  658. (choice (const :tag "Never" nil)
  659. (const :tag "Always" t)
  660. (const :tag "Auto" auto)))))
  661. (defcustom org-insert-heading-hook nil
  662. "Hook being run after inserting a new heading."
  663. :group 'org-edit-structure
  664. :type 'hook)
  665. (defcustom org-enable-fixed-width-editor t
  666. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  667. This currently only means, they are never auto-wrapped.
  668. When nil, such lines will be treated like ordinary lines.
  669. See also the QUOTE keyword."
  670. :group 'org-edit-structure
  671. :type 'boolean)
  672. (defcustom org-edit-src-region-extra nil
  673. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  674. For examples see the function `org-edit-src-find-region-and-lang'.
  675. The regular expression identifying the begin marker should end with a newline,
  676. and the regexp marking the end line should start with a newline, to make sure
  677. there are kept outside the narrowed region."
  678. :group 'org-edit-structure
  679. :type '(repeat
  680. (list
  681. (regexp :tag "begin regexp")
  682. (regexp :tag "end regexp")
  683. (choice :tag "language"
  684. (string :tag "specify")
  685. (integer :tag "from match group")
  686. (const :tag "from `lang' element")
  687. (const :tag "from `style' element")))))
  688. (defcustom org-coderef-label-format "(ref:%s)"
  689. "The default coderef format.
  690. This format string will be used to search for coderef labels in literal
  691. examples (EXAMPLE and SRC blocks). The format can be overwritten
  692. an individual literal example with the -f option, like
  693. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  694. ...
  695. #+END_SRC
  696. If you want to use this for HTML export, make sure that the format does
  697. not introduce special font-locking, and avoid the HTML special
  698. characters `<', `>', and `&'. The reason for this restriction is that
  699. the labels are searched for only after htmlize has done its job."
  700. :group 'org-edit-structure ; FIXME this is not in the right group
  701. :type 'string)
  702. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  703. "The mode that should be used to edit fixed-width regions.
  704. These are the regions where each line starts with a colon."
  705. :group 'org-edit-structure
  706. :type '(choice
  707. (const artist-mode)
  708. (const picture-mode)
  709. (const fundamental-mode)
  710. (function :tag "Other (specify)")))
  711. (defcustom org-goto-auto-isearch t
  712. "Non-nil means, typing characters in org-goto starts incremental search."
  713. :group 'org-edit-structure
  714. :type 'boolean)
  715. (defgroup org-sparse-trees nil
  716. "Options concerning sparse trees in Org-mode."
  717. :tag "Org Sparse Trees"
  718. :group 'org-structure)
  719. (defcustom org-highlight-sparse-tree-matches t
  720. "Non-nil means, highlight all matches that define a sparse tree.
  721. The highlights will automatically disappear the next time the buffer is
  722. changed by an edit command."
  723. :group 'org-sparse-trees
  724. :type 'boolean)
  725. (defcustom org-remove-highlights-with-change t
  726. "Non-nil means, any change to the buffer will remove temporary highlights.
  727. Such highlights are created by `org-occur' and `org-clock-display'.
  728. When nil, `C-c C-c needs to be used to get rid of the highlights.
  729. The highlights created by `org-preview-latex-fragment' always need
  730. `C-c C-c' to be removed."
  731. :group 'org-sparse-trees
  732. :group 'org-time
  733. :type 'boolean)
  734. (defcustom org-occur-hook '(org-first-headline-recenter)
  735. "Hook that is run after `org-occur' has constructed a sparse tree.
  736. This can be used to recenter the window to show as much of the structure
  737. as possible."
  738. :group 'org-sparse-trees
  739. :type 'hook)
  740. (defgroup org-imenu-and-speedbar nil
  741. "Options concerning imenu and speedbar in Org-mode."
  742. :tag "Org Imenu and Speedbar"
  743. :group 'org-structure)
  744. (defcustom org-imenu-depth 2
  745. "The maximum level for Imenu access to Org-mode headlines.
  746. This also applied for speedbar access."
  747. :group 'org-imenu-and-speedbar
  748. :type 'number)
  749. (defgroup org-table nil
  750. "Options concerning tables in Org-mode."
  751. :tag "Org Table"
  752. :group 'org)
  753. (defcustom org-enable-table-editor 'optimized
  754. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  755. When nil, such lines will be treated like ordinary lines.
  756. When equal to the symbol `optimized', the table editor will be optimized to
  757. do the following:
  758. - Automatic overwrite mode in front of whitespace in table fields.
  759. This makes the structure of the table stay in tact as long as the edited
  760. field does not exceed the column width.
  761. - Minimize the number of realigns. Normally, the table is aligned each time
  762. TAB or RET are pressed to move to another field. With optimization this
  763. happens only if changes to a field might have changed the column width.
  764. Optimization requires replacing the functions `self-insert-command',
  765. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  766. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  767. very good at guessing when a re-align will be necessary, but you can always
  768. force one with \\[org-ctrl-c-ctrl-c].
  769. If you would like to use the optimized version in Org-mode, but the
  770. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  771. This variable can be used to turn on and off the table editor during a session,
  772. but in order to toggle optimization, a restart is required.
  773. See also the variable `org-table-auto-blank-field'."
  774. :group 'org-table
  775. :type '(choice
  776. (const :tag "off" nil)
  777. (const :tag "on" t)
  778. (const :tag "on, optimized" optimized)))
  779. (defcustom org-table-tab-recognizes-table.el t
  780. "Non-nil means, TAB will automatically notice a table.el table.
  781. When it sees such a table, it moves point into it and - if necessary -
  782. calls `table-recognize-table'."
  783. :group 'org-table-editing
  784. :type 'boolean)
  785. (defgroup org-link nil
  786. "Options concerning links in Org-mode."
  787. :tag "Org Link"
  788. :group 'org)
  789. (defvar org-link-abbrev-alist-local nil
  790. "Buffer-local version of `org-link-abbrev-alist', which see.
  791. The value of this is taken from the #+LINK lines.")
  792. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  793. (defcustom org-link-abbrev-alist nil
  794. "Alist of link abbreviations.
  795. The car of each element is a string, to be replaced at the start of a link.
  796. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  797. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  798. [[linkkey:tag][description]]
  799. If REPLACE is a string, the tag will simply be appended to create the link.
  800. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  801. the placeholder \"%h\" will cause a url-encoded version of the tag to
  802. be inserted at that point (see the function `url-hexify-string').
  803. REPLACE may also be a function that will be called with the tag as the
  804. only argument to create the link, which should be returned as a string.
  805. See the manual for examples."
  806. :group 'org-link
  807. :type '(repeat
  808. (cons
  809. (string :tag "Protocol")
  810. (choice
  811. (string :tag "Format")
  812. (function)))))
  813. (defcustom org-descriptive-links t
  814. "Non-nil means, hide link part and only show description of bracket links.
  815. Bracket links are like [[link][description]]. This variable sets the initial
  816. state in new org-mode buffers. The setting can then be toggled on a
  817. per-buffer basis from the Org->Hyperlinks menu."
  818. :group 'org-link
  819. :type 'boolean)
  820. (defcustom org-link-file-path-type 'adaptive
  821. "How the path name in file links should be stored.
  822. Valid values are:
  823. relative Relative to the current directory, i.e. the directory of the file
  824. into which the link is being inserted.
  825. absolute Absolute path, if possible with ~ for home directory.
  826. noabbrev Absolute path, no abbreviation of home directory.
  827. adaptive Use relative path for files in the current directory and sub-
  828. directories of it. For other files, use an absolute path."
  829. :group 'org-link
  830. :type '(choice
  831. (const relative)
  832. (const absolute)
  833. (const noabbrev)
  834. (const adaptive)))
  835. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  836. "Types of links that should be activated in Org-mode files.
  837. This is a list of symbols, each leading to the activation of a certain link
  838. type. In principle, it does not hurt to turn on most link types - there may
  839. be a small gain when turning off unused link types. The types are:
  840. bracket The recommended [[link][description]] or [[link]] links with hiding.
  841. angular Links in angular brackets that may contain whitespace like
  842. <bbdb:Carsten Dominik>.
  843. plain Plain links in normal text, no whitespace, like http://google.com.
  844. radio Text that is matched by a radio target, see manual for details.
  845. tag Tag settings in a headline (link to tag search).
  846. date Time stamps (link to calendar).
  847. footnote Footnote labels.
  848. Changing this variable requires a restart of Emacs to become effective."
  849. :group 'org-link
  850. :type '(set :greedy t
  851. (const :tag "Double bracket links (new style)" bracket)
  852. (const :tag "Angular bracket links (old style)" angular)
  853. (const :tag "Plain text links" plain)
  854. (const :tag "Radio target matches" radio)
  855. (const :tag "Tags" tag)
  856. (const :tag "Timestamps" date)
  857. (const :tag "Footnotes" footnote)))
  858. (defcustom org-make-link-description-function nil
  859. "Function to use to generate link descriptions from links. If
  860. nil the link location will be used. This function must take two
  861. parameters; the first is the link and the second the description
  862. org-insert-link has generated, and should return the description
  863. to use."
  864. :group 'org-link
  865. :type 'function)
  866. (defgroup org-link-store nil
  867. "Options concerning storing links in Org-mode."
  868. :tag "Org Store Link"
  869. :group 'org-link)
  870. (defcustom org-email-link-description-format "Email %c: %.30s"
  871. "Format of the description part of a link to an email or usenet message.
  872. The following %-escapes will be replaced by corresponding information:
  873. %F full \"From\" field
  874. %f name, taken from \"From\" field, address if no name
  875. %T full \"To\" field
  876. %t first name in \"To\" field, address if no name
  877. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  878. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  879. %s subject
  880. %m message-id.
  881. You may use normal field width specification between the % and the letter.
  882. This is for example useful to limit the length of the subject.
  883. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  884. :group 'org-link-store
  885. :type 'string)
  886. (defcustom org-from-is-user-regexp
  887. (let (r1 r2)
  888. (when (and user-mail-address (not (string= user-mail-address "")))
  889. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  890. (when (and user-full-name (not (string= user-full-name "")))
  891. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  892. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  893. "Regexp matched against the \"From:\" header of an email or usenet message.
  894. It should match if the message is from the user him/herself."
  895. :group 'org-link-store
  896. :type 'regexp)
  897. (defcustom org-link-to-org-use-id 'create-if-interactive
  898. "Non-nil means, storing a link to an Org file will use entry IDs.
  899. Note that before this variable is even considered, org-id must be loaded,
  900. to please customize `org-modules' and turn it on.
  901. The variable can have the following values:
  902. t Create an ID if needed to make a link to the current entry.
  903. create-if-interactive
  904. If `org-store-link' is called directly (interactively, as a user
  905. command), do create an ID to support the link. But when doing the
  906. job for remember, only use the ID if it already exists. The
  907. purpose of this setting is to avoid proliferation of unwanted
  908. IDs, just because you happen to be in an Org file when you
  909. call `org-remember' that automatically and preemptively
  910. creates a link. If you do want to get an ID link in a remember
  911. template to an entry not having an ID, create it first by
  912. explicitly creating a link to it, using `C-c C-l' first.
  913. use-existing
  914. Use existing ID, do not create one.
  915. nil Never use an ID to make a link, instead link using a text search for
  916. the headline text."
  917. :group 'org-link-store
  918. :type '(choice
  919. (const :tag "Create ID to make link" t)
  920. (const :tag "Create if string link interactively"
  921. 'create-if-interactive)
  922. (const :tag "Only use existing" 'use-existing)
  923. (const :tag "Do not use ID to create link" nil)))
  924. (defcustom org-context-in-file-links t
  925. "Non-nil means, file links from `org-store-link' contain context.
  926. A search string will be added to the file name with :: as separator and
  927. used to find the context when the link is activated by the command
  928. `org-open-at-point'.
  929. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  930. negates this setting for the duration of the command."
  931. :group 'org-link-store
  932. :type 'boolean)
  933. (defcustom org-keep-stored-link-after-insertion nil
  934. "Non-nil means, keep link in list for entire session.
  935. The command `org-store-link' adds a link pointing to the current
  936. location to an internal list. These links accumulate during a session.
  937. The command `org-insert-link' can be used to insert links into any
  938. Org-mode file (offering completion for all stored links). When this
  939. option is nil, every link which has been inserted once using \\[org-insert-link]
  940. will be removed from the list, to make completing the unused links
  941. more efficient."
  942. :group 'org-link-store
  943. :type 'boolean)
  944. (defgroup org-link-follow nil
  945. "Options concerning following links in Org-mode."
  946. :tag "Org Follow Link"
  947. :group 'org-link)
  948. (defcustom org-link-translation-function nil
  949. "Function to translate links with different syntax to Org syntax.
  950. This can be used to translate links created for example by the Planner
  951. or emacs-wiki packages to Org syntax.
  952. The function must accept two parameters, a TYPE containing the link
  953. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  954. which is everything after the link protocol. It should return a cons
  955. with possibly modified values of type and path.
  956. Org contains a function for this, so if you set this variable to
  957. `org-translate-link-from-planner', you should be able follow many
  958. links created by planner."
  959. :group 'org-link-follow
  960. :type 'function)
  961. (defcustom org-follow-link-hook nil
  962. "Hook that is run after a link has been followed."
  963. :group 'org-link-follow
  964. :type 'hook)
  965. (defcustom org-tab-follows-link nil
  966. "Non-nil means, on links TAB will follow the link.
  967. Needs to be set before org.el is loaded."
  968. :group 'org-link-follow
  969. :type 'boolean)
  970. (defcustom org-return-follows-link nil
  971. "Non-nil means, on links RET will follow the link.
  972. Needs to be set before org.el is loaded."
  973. :group 'org-link-follow
  974. :type 'boolean)
  975. (defcustom org-mouse-1-follows-link
  976. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  977. "Non-nil means, mouse-1 on a link will follow the link.
  978. A longer mouse click will still set point. Does not work on XEmacs.
  979. Needs to be set before org.el is loaded."
  980. :group 'org-link-follow
  981. :type 'boolean)
  982. (defcustom org-mark-ring-length 4
  983. "Number of different positions to be recorded in the ring
  984. Changing this requires a restart of Emacs to work correctly."
  985. :group 'org-link-follow
  986. :type 'integer)
  987. (defcustom org-link-frame-setup
  988. '((vm . vm-visit-folder-other-frame)
  989. (gnus . gnus-other-frame)
  990. (file . find-file-other-window))
  991. "Setup the frame configuration for following links.
  992. When following a link with Emacs, it may often be useful to display
  993. this link in another window or frame. This variable can be used to
  994. set this up for the different types of links.
  995. For VM, use any of
  996. `vm-visit-folder'
  997. `vm-visit-folder-other-frame'
  998. For Gnus, use any of
  999. `gnus'
  1000. `gnus-other-frame'
  1001. `org-gnus-no-new-news'
  1002. For FILE, use any of
  1003. `find-file'
  1004. `find-file-other-window'
  1005. `find-file-other-frame'
  1006. For the calendar, use the variable `calendar-setup'.
  1007. For BBDB, it is currently only possible to display the matches in
  1008. another window."
  1009. :group 'org-link-follow
  1010. :type '(list
  1011. (cons (const vm)
  1012. (choice
  1013. (const vm-visit-folder)
  1014. (const vm-visit-folder-other-window)
  1015. (const vm-visit-folder-other-frame)))
  1016. (cons (const gnus)
  1017. (choice
  1018. (const gnus)
  1019. (const gnus-other-frame)
  1020. (const org-gnus-no-new-news)))
  1021. (cons (const file)
  1022. (choice
  1023. (const find-file)
  1024. (const find-file-other-window)
  1025. (const find-file-other-frame)))))
  1026. (defcustom org-display-internal-link-with-indirect-buffer nil
  1027. "Non-nil means, use indirect buffer to display infile links.
  1028. Activating internal links (from one location in a file to another location
  1029. in the same file) normally just jumps to the location. When the link is
  1030. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1031. another window. When this option is set, the other window actually displays
  1032. an indirect buffer clone of the current buffer, to avoid any visibility
  1033. changes to the current buffer."
  1034. :group 'org-link-follow
  1035. :type 'boolean)
  1036. (defcustom org-open-non-existing-files nil
  1037. "Non-nil means, `org-open-file' will open non-existing files.
  1038. When nil, an error will be generated."
  1039. :group 'org-link-follow
  1040. :type 'boolean)
  1041. (defcustom org-open-directory-means-index-dot-org nil
  1042. "Non-nil means, a link to a directory really means to index.org.
  1043. When nil, following a directory link will run dired or open a finder/explorer
  1044. window on that directory."
  1045. :group 'org-link-follow
  1046. :type 'boolean)
  1047. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1048. "Function and arguments to call for following mailto links.
  1049. This is a list with the first element being a lisp function, and the
  1050. remaining elements being arguments to the function. In string arguments,
  1051. %a will be replaced by the address, and %s will be replaced by the subject
  1052. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1053. :group 'org-link-follow
  1054. :type '(choice
  1055. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1056. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1057. (const :tag "message-mail" (message-mail "%a" "%s"))
  1058. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1059. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1060. "Non-nil means, ask for confirmation before executing shell links.
  1061. Shell links can be dangerous: just think about a link
  1062. [[shell:rm -rf ~/*][Google Search]]
  1063. This link would show up in your Org-mode document as \"Google Search\",
  1064. but really it would remove your entire home directory.
  1065. Therefore we advise against setting this variable to nil.
  1066. Just change it to `y-or-n-p' of you want to confirm with a
  1067. single keystroke rather than having to type \"yes\"."
  1068. :group 'org-link-follow
  1069. :type '(choice
  1070. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1071. (const :tag "with y-or-n (faster)" y-or-n-p)
  1072. (const :tag "no confirmation (dangerous)" nil)))
  1073. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1074. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1075. Elisp links can be dangerous: just think about a link
  1076. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1077. This link would show up in your Org-mode document as \"Google Search\",
  1078. but really it would remove your entire home directory.
  1079. Therefore we advise against setting this variable to nil.
  1080. Just change it to `y-or-n-p' of you want to confirm with a
  1081. single keystroke rather than having to type \"yes\"."
  1082. :group 'org-link-follow
  1083. :type '(choice
  1084. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1085. (const :tag "with y-or-n (faster)" y-or-n-p)
  1086. (const :tag "no confirmation (dangerous)" nil)))
  1087. (defconst org-file-apps-defaults-gnu
  1088. '((remote . emacs)
  1089. (system . mailcap)
  1090. (t . mailcap))
  1091. "Default file applications on a UNIX or GNU/Linux system.
  1092. See `org-file-apps'.")
  1093. (defconst org-file-apps-defaults-macosx
  1094. '((remote . emacs)
  1095. (t . "open %s")
  1096. (system . "open %s")
  1097. ("ps.gz" . "gv %s")
  1098. ("eps.gz" . "gv %s")
  1099. ("dvi" . "xdvi %s")
  1100. ("fig" . "xfig %s"))
  1101. "Default file applications on a MacOS X system.
  1102. The system \"open\" is known as a default, but we use X11 applications
  1103. for some files for which the OS does not have a good default.
  1104. See `org-file-apps'.")
  1105. (defconst org-file-apps-defaults-windowsnt
  1106. (list
  1107. '(remote . emacs)
  1108. (cons t
  1109. (list (if (featurep 'xemacs)
  1110. 'mswindows-shell-execute
  1111. 'w32-shell-execute)
  1112. "open" 'file))
  1113. (cons 'system
  1114. (list (if (featurep 'xemacs)
  1115. 'mswindows-shell-execute
  1116. 'w32-shell-execute)
  1117. "open" 'file)))
  1118. "Default file applications on a Windows NT system.
  1119. The system \"open\" is used for most files.
  1120. See `org-file-apps'.")
  1121. (defcustom org-file-apps
  1122. '(
  1123. (auto-mode . emacs)
  1124. ("\\.x?html?\\'" . default)
  1125. ("\\.pdf\\'" . default)
  1126. )
  1127. "External applications for opening `file:path' items in a document.
  1128. Org-mode uses system defaults for different file types, but
  1129. you can use this variable to set the application for a given file
  1130. extension. The entries in this list are cons cells where the car identifies
  1131. files and the cdr the corresponding command. Possible values for the
  1132. file identifier are
  1133. \"regex\" Regular expression matched against the file name. For backward
  1134. compatibility, this can also be a string with only alphanumeric
  1135. characters, which is then interpreted as an extension.
  1136. `directory' Matches a directory
  1137. `remote' Matches a remote file, accessible through tramp or efs.
  1138. Remote files most likely should be visited through Emacs
  1139. because external applications cannot handle such paths.
  1140. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1141. so all files Emacs knows how to handle. Using this with
  1142. command `emacs' will open most files in Emacs. Beware that this
  1143. will also open html files inside Emacs, unless you add
  1144. (\"html\" . default) to the list as well.
  1145. t Default for files not matched by any of the other options.
  1146. `system' The system command to open files, like `open' on Windows
  1147. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1148. that will be selected if you call `C-c C-o' with a double
  1149. `C-u C-u' prefix.
  1150. Possible values for the command are:
  1151. `emacs' The file will be visited by the current Emacs process.
  1152. `default' Use the default application for this file type, which is the
  1153. association for t in the list, most likely in the system-specific
  1154. part.
  1155. This can be used to overrule an unwanted setting in the
  1156. system-specific variable.
  1157. `system' Use the system command for opening files, like \"open\".
  1158. This command is specified by the entry whose car is `system'.
  1159. Most likely, the system-specific version of this variable
  1160. does define this command, but you can overrule/replace it
  1161. here.
  1162. string A command to be executed by a shell; %s will be replaced
  1163. by the path to the file.
  1164. sexp A Lisp form which will be evaluated. The file path will
  1165. be available in the Lisp variable `file'.
  1166. For more examples, see the system specific constants
  1167. `org-file-apps-defaults-macosx'
  1168. `org-file-apps-defaults-windowsnt'
  1169. `org-file-apps-defaults-gnu'."
  1170. :group 'org-link-follow
  1171. :type '(repeat
  1172. (cons (choice :value ""
  1173. (string :tag "Extension")
  1174. (const :tag "System command to open files" system)
  1175. (const :tag "Default for unrecognized files" t)
  1176. (const :tag "Remote file" remote)
  1177. (const :tag "Links to a directory" directory)
  1178. (const :tag "Any files that have Emacs modes"
  1179. auto-mode))
  1180. (choice :value ""
  1181. (const :tag "Visit with Emacs" emacs)
  1182. (const :tag "Use default" default)
  1183. (const :tag "Use the system command" system)
  1184. (string :tag "Command")
  1185. (sexp :tag "Lisp form")))))
  1186. (defgroup org-refile nil
  1187. "Options concerning refiling entries in Org-mode."
  1188. :tag "Org Refile"
  1189. :group 'org)
  1190. (defcustom org-directory "~/org"
  1191. "Directory with org files.
  1192. This directory will be used as default to prompt for org files.
  1193. Used by the hooks for remember.el."
  1194. :group 'org-refile
  1195. :group 'org-remember
  1196. :type 'directory)
  1197. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1198. "Default target for storing notes.
  1199. Used by the hooks for remember.el. This can be a string, or nil to mean
  1200. the value of `remember-data-file'.
  1201. You can set this on a per-template basis with the variable
  1202. `org-remember-templates'."
  1203. :group 'org-refile
  1204. :group 'org-remember
  1205. :type '(choice
  1206. (const :tag "Default from remember-data-file" nil)
  1207. file))
  1208. (defcustom org-goto-interface 'outline
  1209. "The default interface to be used for `org-goto'.
  1210. Allowed values are:
  1211. outline The interface shows an outline of the relevant file
  1212. and the correct heading is found by moving through
  1213. the outline or by searching with incremental search.
  1214. outline-path-completion Headlines in the current buffer are offered via
  1215. completion. This is the interface also used by
  1216. the refile command."
  1217. :group 'org-refile
  1218. :type '(choice
  1219. (const :tag "Outline" outline)
  1220. (const :tag "Outline-path-completion" outline-path-completion)))
  1221. (defcustom org-goto-max-level 5
  1222. "Maximum level to be considered when running org-goto with refile interface."
  1223. :group 'org-refile
  1224. :type 'number)
  1225. (defcustom org-reverse-note-order nil
  1226. "Non-nil means, store new notes at the beginning of a file or entry.
  1227. When nil, new notes will be filed to the end of a file or entry.
  1228. This can also be a list with cons cells of regular expressions that
  1229. are matched against file names, and values."
  1230. :group 'org-remember
  1231. :group 'org-refile
  1232. :type '(choice
  1233. (const :tag "Reverse always" t)
  1234. (const :tag "Reverse never" nil)
  1235. (repeat :tag "By file name regexp"
  1236. (cons regexp boolean))))
  1237. (defcustom org-refile-targets nil
  1238. "Targets for refiling entries with \\[org-refile].
  1239. This is list of cons cells. Each cell contains:
  1240. - a specification of the files to be considered, either a list of files,
  1241. or a symbol whose function or variable value will be used to retrieve
  1242. a file name or a list of file names. If you use `org-agenda-files' for
  1243. that, all agenda files will be scanned for targets. Nil means, consider
  1244. headings in the current buffer.
  1245. - A specification of how to select find candidate refile targets. This
  1246. may be any of
  1247. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1248. This tag has to be present in all target headlines, inheritance will
  1249. not be considered.
  1250. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1251. todo keyword.
  1252. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1253. headlines that are refiling targets.
  1254. - a cons cell (:level . N). Any headline of level N is considered a target.
  1255. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1256. When this variable is nil, all top-level headlines in the current buffer
  1257. are used, equivalent to the value `((nil . (:level . 1))'."
  1258. :group 'org-refile
  1259. :type '(repeat
  1260. (cons
  1261. (choice :value org-agenda-files
  1262. (const :tag "All agenda files" org-agenda-files)
  1263. (const :tag "Current buffer" nil)
  1264. (function) (variable) (file))
  1265. (choice :tag "Identify target headline by"
  1266. (cons :tag "Specific tag" (const :value :tag) (string))
  1267. (cons :tag "TODO keyword" (const :value :todo) (string))
  1268. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1269. (cons :tag "Level number" (const :value :level) (integer))
  1270. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1271. (defcustom org-refile-use-outline-path nil
  1272. "Non-nil means, provide refile targets as paths.
  1273. So a level 3 headline will be available as level1/level2/level3.
  1274. When the value is `file', also include the file name (without directory)
  1275. into the path. When `full-file-path', include the full file path."
  1276. :group 'org-refile
  1277. :type '(choice
  1278. (const :tag "Not" nil)
  1279. (const :tag "Yes" t)
  1280. (const :tag "Start with file name" file)
  1281. (const :tag "Start with full file path" full-file-path)))
  1282. (defcustom org-outline-path-complete-in-steps t
  1283. "Non-nil means, complete the outline path in hierarchical steps.
  1284. When Org-mode uses the refile interface to select an outline path
  1285. \(see variable `org-refile-use-outline-path'), the completion of
  1286. the path can be done is a single go, or if can be done in steps down
  1287. the headline hierarchy. Going in steps is probably the best if you
  1288. do not use a special completion package like `ido' or `icicles'.
  1289. However, when using these packages, going in one step can be very
  1290. fast, while still showing the whole path to the entry."
  1291. :group 'org-refile
  1292. :type 'boolean)
  1293. (defgroup org-todo nil
  1294. "Options concerning TODO items in Org-mode."
  1295. :tag "Org TODO"
  1296. :group 'org)
  1297. (defgroup org-progress nil
  1298. "Options concerning Progress logging in Org-mode."
  1299. :tag "Org Progress"
  1300. :group 'org-time)
  1301. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1302. "List of TODO entry keyword sequences and their interpretation.
  1303. \\<org-mode-map>This is a list of sequences.
  1304. Each sequence starts with a symbol, either `sequence' or `type',
  1305. indicating if the keywords should be interpreted as a sequence of
  1306. action steps, or as different types of TODO items. The first
  1307. keywords are states requiring action - these states will select a headline
  1308. for inclusion into the global TODO list Org-mode produces. If one of
  1309. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1310. signify that no further action is necessary. If \"|\" is not found,
  1311. the last keyword is treated as the only DONE state of the sequence.
  1312. The command \\[org-todo] cycles an entry through these states, and one
  1313. additional state where no keyword is present. For details about this
  1314. cycling, see the manual.
  1315. TODO keywords and interpretation can also be set on a per-file basis with
  1316. the special #+SEQ_TODO and #+TYP_TODO lines.
  1317. Each keyword can optionally specify a character for fast state selection
  1318. \(in combination with the variable `org-use-fast-todo-selection')
  1319. and specifiers for state change logging, using the same syntax
  1320. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1321. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1322. indicates to record a time stamp each time this state is selected.
  1323. Each keyword may also specify if a timestamp or a note should be
  1324. recorded when entering or leaving the state, by adding additional
  1325. characters in the parenthesis after the keyword. This looks like this:
  1326. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1327. record only the time of the state change. With X and Y being either
  1328. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1329. Y when leaving the state if and only if the *target* state does not
  1330. define X. You may omit any of the fast-selection key or X or /Y,
  1331. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1332. For backward compatibility, this variable may also be just a list
  1333. of keywords - in this case the interpretation (sequence or type) will be
  1334. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1335. :group 'org-todo
  1336. :group 'org-keywords
  1337. :type '(choice
  1338. (repeat :tag "Old syntax, just keywords"
  1339. (string :tag "Keyword"))
  1340. (repeat :tag "New syntax"
  1341. (cons
  1342. (choice
  1343. :tag "Interpretation"
  1344. (const :tag "Sequence (cycling hits every state)" sequence)
  1345. (const :tag "Type (cycling directly to DONE)" type))
  1346. (repeat
  1347. (string :tag "Keyword"))))))
  1348. (defvar org-todo-keywords-1 nil
  1349. "All TODO and DONE keywords active in a buffer.")
  1350. (make-variable-buffer-local 'org-todo-keywords-1)
  1351. (defvar org-todo-keywords-for-agenda nil)
  1352. (defvar org-done-keywords-for-agenda nil)
  1353. (defvar org-todo-keyword-alist-for-agenda nil)
  1354. (defvar org-tag-alist-for-agenda nil)
  1355. (defvar org-agenda-contributing-files nil)
  1356. (defvar org-not-done-keywords nil)
  1357. (make-variable-buffer-local 'org-not-done-keywords)
  1358. (defvar org-done-keywords nil)
  1359. (make-variable-buffer-local 'org-done-keywords)
  1360. (defvar org-todo-heads nil)
  1361. (make-variable-buffer-local 'org-todo-heads)
  1362. (defvar org-todo-sets nil)
  1363. (make-variable-buffer-local 'org-todo-sets)
  1364. (defvar org-todo-log-states nil)
  1365. (make-variable-buffer-local 'org-todo-log-states)
  1366. (defvar org-todo-kwd-alist nil)
  1367. (make-variable-buffer-local 'org-todo-kwd-alist)
  1368. (defvar org-todo-key-alist nil)
  1369. (make-variable-buffer-local 'org-todo-key-alist)
  1370. (defvar org-todo-key-trigger nil)
  1371. (make-variable-buffer-local 'org-todo-key-trigger)
  1372. (defcustom org-todo-interpretation 'sequence
  1373. "Controls how TODO keywords are interpreted.
  1374. This variable is in principle obsolete and is only used for
  1375. backward compatibility, if the interpretation of todo keywords is
  1376. not given already in `org-todo-keywords'. See that variable for
  1377. more information."
  1378. :group 'org-todo
  1379. :group 'org-keywords
  1380. :type '(choice (const sequence)
  1381. (const type)))
  1382. (defcustom org-use-fast-todo-selection 'prefix
  1383. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1384. This variable describes if and under what circumstances the cycling
  1385. mechanism for TODO keywords will be replaced by a single-key, direct
  1386. selection scheme.
  1387. When nil, fast selection is never used.
  1388. When the symbol `prefix', it will be used when `org-todo' is called with
  1389. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1390. in an agenda buffer.
  1391. When t, fast selection is used by default. In this case, the prefix
  1392. argument forces cycling instead.
  1393. In all cases, the special interface is only used if access keys have actually
  1394. been assigned by the user, i.e. if keywords in the configuration are followed
  1395. by a letter in parenthesis, like TODO(t)."
  1396. :group 'org-todo
  1397. :type '(choice
  1398. (const :tag "Never" nil)
  1399. (const :tag "By default" t)
  1400. (const :tag "Only with C-u C-c C-t" prefix)))
  1401. (defcustom org-provide-todo-statistics t
  1402. "Non-nil means, update todo statistics after insert and toggle.
  1403. When this is set, todo statistics is updated in the parent of the current
  1404. entry each time a todo state is changed."
  1405. :group 'org-todo
  1406. :type 'boolean)
  1407. (defcustom org-after-todo-state-change-hook nil
  1408. "Hook which is run after the state of a TODO item was changed.
  1409. The new state (a string with a TODO keyword, or nil) is available in the
  1410. Lisp variable `state'."
  1411. :group 'org-todo
  1412. :type 'hook)
  1413. (defvar org-blocker-hook nil
  1414. "Hook for functions that are allowed to block a state change.
  1415. Each function gets as its single argument a property list, see
  1416. `org-trigger-hook' for more information about this list.
  1417. If any of the functions in this hook returns nil, the state change
  1418. is blocked.")
  1419. (defvar org-trigger-hook nil
  1420. "Hook for functions that are triggered by a state change.
  1421. Each function gets as its single argument a property list with at least
  1422. the following elements:
  1423. (:type type-of-change :position pos-at-entry-start
  1424. :from old-state :to new-state)
  1425. Depending on the type, more properties may be present.
  1426. This mechanism is currently implemented for:
  1427. TODO state changes
  1428. ------------------
  1429. :type todo-state-change
  1430. :from previous state (keyword as a string), or nil, or a symbol
  1431. 'todo' or 'done', to indicate the general type of state.
  1432. :to new state, like in :from")
  1433. (defcustom org-enforce-todo-dependencies nil
  1434. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1435. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1436. be blocked if any prior sibling is not yet done.
  1437. You need to set this variable through the customize interface, or to
  1438. restart emacs after changing the value."
  1439. :set (lambda (var val)
  1440. (set var val)
  1441. (if val
  1442. (add-hook 'org-blocker-hook
  1443. 'org-block-todo-from-children-or-siblings)
  1444. (remove-hook 'org-blocker-hook
  1445. 'org-block-todo-from-children-or-siblings)))
  1446. :group 'org-todo
  1447. :type 'boolean)
  1448. (defcustom org-enforce-todo-checkbox-dependencies nil
  1449. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1450. When this is nil, checkboxes have no influence on switching TODO states.
  1451. When non-nil, you first need to check off all check boxes before the TODO
  1452. entry can be switched to DONE.
  1453. You need to set this variable through the customize interface, or to
  1454. restart emacs after changing the value."
  1455. :set (lambda (var val)
  1456. (set var val)
  1457. (if val
  1458. (add-hook 'org-blocker-hook
  1459. 'org-block-todo-from-checkboxes)
  1460. (remove-hook 'org-blocker-hook
  1461. 'org-block-todo-from-checkboxes)))
  1462. :group 'org-todo
  1463. :type 'boolean)
  1464. (defcustom org-todo-state-tags-triggers nil
  1465. "Tag changes that should be triggered by TODO state changes.
  1466. This is a list. Each entry is
  1467. (state-change (tag . flag) .......)
  1468. State-change can be a string with a state, and empty string to indicate the
  1469. state that has no TODO keyword, or it can be one of the symbols `todo'
  1470. or `done', meaning any not-done or done state, respectively."
  1471. :group 'org-todo
  1472. :group 'org-tags
  1473. :type '(repeat
  1474. (cons (choice :tag "When changing to"
  1475. (const :tag "Not-done state" todo)
  1476. (const :tag "Done state" done)
  1477. (string :tag "State"))
  1478. (repeat
  1479. (cons :tag "Tag action"
  1480. (string :tag "Tag")
  1481. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1482. (defcustom org-log-done nil
  1483. "Information to record when a task moves to the DONE state.
  1484. Possible values are:
  1485. nil Don't add anything, just change the keyword
  1486. time Add a time stamp to the task
  1487. note Prompt a closing note and add it with template `org-log-note-headings'
  1488. This option can also be set with on a per-file-basis with
  1489. #+STARTUP: nologdone
  1490. #+STARTUP: logdone
  1491. #+STARTUP: lognotedone
  1492. You can have local logging settings for a subtree by setting the LOGGING
  1493. property to one or more of these keywords."
  1494. :group 'org-todo
  1495. :group 'org-progress
  1496. :type '(choice
  1497. (const :tag "No logging" nil)
  1498. (const :tag "Record CLOSED timestamp" time)
  1499. (const :tag "Record CLOSED timestamp with closing note." note)))
  1500. ;; Normalize old uses of org-log-done.
  1501. (cond
  1502. ((eq org-log-done t) (setq org-log-done 'time))
  1503. ((and (listp org-log-done) (memq 'done org-log-done))
  1504. (setq org-log-done 'note)))
  1505. (defcustom org-log-note-clock-out nil
  1506. "Non-nil means, record a note when clocking out of an item.
  1507. This can also be configured on a per-file basis by adding one of
  1508. the following lines anywhere in the buffer:
  1509. #+STARTUP: lognoteclock-out
  1510. #+STARTUP: nolognoteclock-out"
  1511. :group 'org-todo
  1512. :group 'org-progress
  1513. :type 'boolean)
  1514. (defcustom org-log-done-with-time t
  1515. "Non-nil means, the CLOSED time stamp will contain date and time.
  1516. When nil, only the date will be recorded."
  1517. :group 'org-progress
  1518. :type 'boolean)
  1519. (defcustom org-log-note-headings
  1520. '((done . "CLOSING NOTE %t")
  1521. (state . "State %-12s %t")
  1522. (note . "Note taken on %t")
  1523. (clock-out . ""))
  1524. "Headings for notes added to entries.
  1525. The value is an alist, with the car being a symbol indicating the note
  1526. context, and the cdr is the heading to be used. The heading may also be the
  1527. empty string.
  1528. %t in the heading will be replaced by a time stamp.
  1529. %s will be replaced by the new TODO state, in double quotes.
  1530. %u will be replaced by the user name.
  1531. %U will be replaced by the full user name."
  1532. :group 'org-todo
  1533. :group 'org-progress
  1534. :type '(list :greedy t
  1535. (cons (const :tag "Heading when closing an item" done) string)
  1536. (cons (const :tag
  1537. "Heading when changing todo state (todo sequence only)"
  1538. state) string)
  1539. (cons (const :tag "Heading when just taking a note" note) string)
  1540. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1541. (unless (assq 'note org-log-note-headings)
  1542. (push '(note . "%t") org-log-note-headings))
  1543. (defcustom org-log-state-notes-insert-after-drawers nil
  1544. "Non-nil means, insert state change notes after any drawers in entry.
  1545. Only the drawers that *immediately* follow the headline and the
  1546. deadline/scheduled line are skipped.
  1547. When nil, insert notes right after the heading and perhaps the line
  1548. with deadline/scheduling if present."
  1549. :group 'org-todo
  1550. :group 'org-progress
  1551. :type 'boolean)
  1552. (defcustom org-log-states-order-reversed t
  1553. "Non-nil means, the latest state change note will be directly after heading.
  1554. When nil, the notes will be orderer according to time."
  1555. :group 'org-todo
  1556. :group 'org-progress
  1557. :type 'boolean)
  1558. (defcustom org-log-repeat 'time
  1559. "Non-nil means, record moving through the DONE state when triggering repeat.
  1560. An auto-repeating tasks is immediately switched back to TODO when marked
  1561. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1562. the TODO keyword definition, or recording a closing note by setting
  1563. `org-log-done', there will be no record of the task moving through DONE.
  1564. This variable forces taking a note anyway. Possible values are:
  1565. nil Don't force a record
  1566. time Record a time stamp
  1567. note Record a note
  1568. This option can also be set with on a per-file-basis with
  1569. #+STARTUP: logrepeat
  1570. #+STARTUP: lognoterepeat
  1571. #+STARTUP: nologrepeat
  1572. You can have local logging settings for a subtree by setting the LOGGING
  1573. property to one or more of these keywords."
  1574. :group 'org-todo
  1575. :group 'org-progress
  1576. :type '(choice
  1577. (const :tag "Don't force a record" nil)
  1578. (const :tag "Force recording the DONE state" time)
  1579. (const :tag "Force recording a note with the DONE state" note)))
  1580. (defgroup org-priorities nil
  1581. "Priorities in Org-mode."
  1582. :tag "Org Priorities"
  1583. :group 'org-todo)
  1584. (defcustom org-highest-priority ?A
  1585. "The highest priority of TODO items. A character like ?A, ?B etc.
  1586. Must have a smaller ASCII number than `org-lowest-priority'."
  1587. :group 'org-priorities
  1588. :type 'character)
  1589. (defcustom org-lowest-priority ?C
  1590. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1591. Must have a larger ASCII number than `org-highest-priority'."
  1592. :group 'org-priorities
  1593. :type 'character)
  1594. (defcustom org-default-priority ?B
  1595. "The default priority of TODO items.
  1596. This is the priority an item get if no explicit priority is given."
  1597. :group 'org-priorities
  1598. :type 'character)
  1599. (defcustom org-priority-start-cycle-with-default t
  1600. "Non-nil means, start with default priority when starting to cycle.
  1601. When this is nil, the first step in the cycle will be (depending on the
  1602. command used) one higher or lower that the default priority."
  1603. :group 'org-priorities
  1604. :type 'boolean)
  1605. (defgroup org-time nil
  1606. "Options concerning time stamps and deadlines in Org-mode."
  1607. :tag "Org Time"
  1608. :group 'org)
  1609. (defcustom org-insert-labeled-timestamps-at-point nil
  1610. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1611. When nil, these labeled time stamps are forces into the second line of an
  1612. entry, just after the headline. When scheduling from the global TODO list,
  1613. the time stamp will always be forced into the second line."
  1614. :group 'org-time
  1615. :type 'boolean)
  1616. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1617. "Formats for `format-time-string' which are used for time stamps.
  1618. It is not recommended to change this constant.")
  1619. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1620. "Number of minutes to round time stamps to.
  1621. These are two values, the first applies when first creating a time stamp.
  1622. The second applies when changing it with the commands `S-up' and `S-down'.
  1623. When changing the time stamp, this means that it will change in steps
  1624. of N minutes, as given by the second value.
  1625. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1626. numbers should be factors of 60, so for example 5, 10, 15.
  1627. When this is larger than 1, you can still force an exact time-stamp by using
  1628. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1629. and by using a prefix arg to `S-up/down' to specify the exact number
  1630. of minutes to shift."
  1631. :group 'org-time
  1632. :get '(lambda (var) ; Make sure all entries have 5 elements
  1633. (if (integerp (default-value var))
  1634. (list (default-value var) 5)
  1635. (default-value var)))
  1636. :type '(list
  1637. (integer :tag "when inserting times")
  1638. (integer :tag "when modifying times")))
  1639. ;; Normalize old customizations of this variable.
  1640. (when (integerp org-time-stamp-rounding-minutes)
  1641. (setq org-time-stamp-rounding-minutes
  1642. (list org-time-stamp-rounding-minutes
  1643. org-time-stamp-rounding-minutes)))
  1644. (defcustom org-display-custom-times nil
  1645. "Non-nil means, overlay custom formats over all time stamps.
  1646. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1647. To turn this on on a per-file basis, insert anywhere in the file:
  1648. #+STARTUP: customtime"
  1649. :group 'org-time
  1650. :set 'set-default
  1651. :type 'sexp)
  1652. (make-variable-buffer-local 'org-display-custom-times)
  1653. (defcustom org-time-stamp-custom-formats
  1654. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1655. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1656. These are overlayed over the default ISO format if the variable
  1657. `org-display-custom-times' is set. Time like %H:%M should be at the
  1658. end of the second format."
  1659. :group 'org-time
  1660. :type 'sexp)
  1661. (defun org-time-stamp-format (&optional long inactive)
  1662. "Get the right format for a time string."
  1663. (let ((f (if long (cdr org-time-stamp-formats)
  1664. (car org-time-stamp-formats))))
  1665. (if inactive
  1666. (concat "[" (substring f 1 -1) "]")
  1667. f)))
  1668. (defcustom org-time-clocksum-format "%d:%02d"
  1669. "The format string used when creating CLOCKSUM lines, or when
  1670. org-mode generates a time duration."
  1671. :group 'org-time
  1672. :type 'string)
  1673. (defcustom org-deadline-warning-days 14
  1674. "No. of days before expiration during which a deadline becomes active.
  1675. This variable governs the display in sparse trees and in the agenda.
  1676. When 0 or negative, it means use this number (the absolute value of it)
  1677. even if a deadline has a different individual lead time specified."
  1678. :group 'org-time
  1679. :group 'org-agenda-daily/weekly
  1680. :type 'number)
  1681. (defcustom org-read-date-prefer-future t
  1682. "Non-nil means, assume future for incomplete date input from user.
  1683. This affects the following situations:
  1684. 1. The user gives a day, but no month.
  1685. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1686. read this as the third of *next* month. However, if you enter \"17\",
  1687. it will be considered as *this* month.
  1688. 2. The user gives a month but not a year.
  1689. For example, if it is april and you enter \"feb 2\", this will be read
  1690. as feb 2, *next* year. \"May 5\", however, will be this year.
  1691. Currently this does not work for ISO week specifications.
  1692. When this option is nil, the current month and year will always be used
  1693. as defaults."
  1694. :group 'org-time
  1695. :type 'boolean)
  1696. (defcustom org-read-date-display-live t
  1697. "Non-nil means, display current interpretation of date prompt live.
  1698. This display will be in an overlay, in the minibuffer."
  1699. :group 'org-time
  1700. :type 'boolean)
  1701. (defcustom org-read-date-popup-calendar t
  1702. "Non-nil means, pop up a calendar when prompting for a date.
  1703. In the calendar, the date can be selected with mouse-1. However, the
  1704. minibuffer will also be active, and you can simply enter the date as well.
  1705. When nil, only the minibuffer will be available."
  1706. :group 'org-time
  1707. :type 'boolean)
  1708. (if (fboundp 'defvaralias)
  1709. (defvaralias 'org-popup-calendar-for-date-prompt
  1710. 'org-read-date-popup-calendar))
  1711. (defcustom org-extend-today-until 0
  1712. "The hour when your day really ends. Must be an integer.
  1713. This has influence for the following applications:
  1714. - When switching the agenda to \"today\". It it is still earlier than
  1715. the time given here, the day recognized as TODAY is actually yesterday.
  1716. - When a date is read from the user and it is still before the time given
  1717. here, the current date and time will be assumed to be yesterday, 23:59.
  1718. Also, timestamps inserted in remember templates follow this rule.
  1719. IMPORTANT: This is a feature whose implementation is and likely will
  1720. remain incomplete. Really, it is only here because past midnight seems to
  1721. be the favorite working time of John Wiegley :-)"
  1722. :group 'org-time
  1723. :type 'number)
  1724. (defcustom org-edit-timestamp-down-means-later nil
  1725. "Non-nil means, S-down will increase the time in a time stamp.
  1726. When nil, S-up will increase."
  1727. :group 'org-time
  1728. :type 'boolean)
  1729. (defcustom org-calendar-follow-timestamp-change t
  1730. "Non-nil means, make the calendar window follow timestamp changes.
  1731. When a timestamp is modified and the calendar window is visible, it will be
  1732. moved to the new date."
  1733. :group 'org-time
  1734. :type 'boolean)
  1735. (defgroup org-tags nil
  1736. "Options concerning tags in Org-mode."
  1737. :tag "Org Tags"
  1738. :group 'org)
  1739. (defcustom org-tag-alist nil
  1740. "List of tags allowed in Org-mode files.
  1741. When this list is nil, Org-mode will base TAG input on what is already in the
  1742. buffer.
  1743. The value of this variable is an alist, the car of each entry must be a
  1744. keyword as a string, the cdr may be a character that is used to select
  1745. that tag through the fast-tag-selection interface.
  1746. See the manual for details."
  1747. :group 'org-tags
  1748. :type '(repeat
  1749. (choice
  1750. (cons (string :tag "Tag name")
  1751. (character :tag "Access char"))
  1752. (const :tag "Start radio group" (:startgroup))
  1753. (const :tag "End radio group" (:endgroup)))))
  1754. (defvar org-file-tags nil
  1755. "List of tags that can be inherited by all entries in the file.
  1756. The tags will be inherited if the variable `org-use-tag-inheritance'
  1757. says they should be.
  1758. This variable is populated from #+TAG lines.")
  1759. (defcustom org-use-fast-tag-selection 'auto
  1760. "Non-nil means, use fast tag selection scheme.
  1761. This is a special interface to select and deselect tags with single keys.
  1762. When nil, fast selection is never used.
  1763. When the symbol `auto', fast selection is used if and only if selection
  1764. characters for tags have been configured, either through the variable
  1765. `org-tag-alist' or through a #+TAGS line in the buffer.
  1766. When t, fast selection is always used and selection keys are assigned
  1767. automatically if necessary."
  1768. :group 'org-tags
  1769. :type '(choice
  1770. (const :tag "Always" t)
  1771. (const :tag "Never" nil)
  1772. (const :tag "When selection characters are configured" 'auto)))
  1773. (defcustom org-fast-tag-selection-single-key nil
  1774. "Non-nil means, fast tag selection exits after first change.
  1775. When nil, you have to press RET to exit it.
  1776. During fast tag selection, you can toggle this flag with `C-c'.
  1777. This variable can also have the value `expert'. In this case, the window
  1778. displaying the tags menu is not even shown, until you press C-c again."
  1779. :group 'org-tags
  1780. :type '(choice
  1781. (const :tag "No" nil)
  1782. (const :tag "Yes" t)
  1783. (const :tag "Expert" expert)))
  1784. (defvar org-fast-tag-selection-include-todo nil
  1785. "Non-nil means, fast tags selection interface will also offer TODO states.
  1786. This is an undocumented feature, you should not rely on it.")
  1787. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1788. "The column to which tags should be indented in a headline.
  1789. If this number is positive, it specifies the column. If it is negative,
  1790. it means that the tags should be flushright to that column. For example,
  1791. -80 works well for a normal 80 character screen."
  1792. :group 'org-tags
  1793. :type 'integer)
  1794. (defcustom org-auto-align-tags t
  1795. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1796. These operations change the length of a headline and therefore shift
  1797. the tags around. With this options turned on, after each such operation
  1798. the tags are again aligned to `org-tags-column'."
  1799. :group 'org-tags
  1800. :type 'boolean)
  1801. (defcustom org-use-tag-inheritance t
  1802. "Non-nil means, tags in levels apply also for sublevels.
  1803. When nil, only the tags directly given in a specific line apply there.
  1804. This may also be a list of tags that should be inherited, or a regexp that
  1805. matches tags that should be inherited. Additional control is possible
  1806. with the variable `org-tags-exclude-from-inheritance' which gives an
  1807. explicit list of tags to be excluded from inheritance., even if the value of
  1808. `org-use-tag-inheritance' would select it for inheritance.
  1809. If this option is t, a match early-on in a tree can lead to a large
  1810. number of matches in the subtree when constructing the agenda or creating
  1811. a sparse tree. If you only want to see the first match in a tree during
  1812. a search, check out the variable `org-tags-match-list-sublevels'."
  1813. :group 'org-tags
  1814. :type '(choice
  1815. (const :tag "Not" nil)
  1816. (const :tag "Always" t)
  1817. (repeat :tag "Specific tags" (string :tag "Tag"))
  1818. (regexp :tag "Tags matched by regexp")))
  1819. (defcustom org-tags-exclude-from-inheritance nil
  1820. "List of tags that should never be inherited.
  1821. This is a way to exclude a few tags from inheritance. For way to do
  1822. the opposite, to actively allow inheritance for selected tags,
  1823. see the variable `org-use-tag-inheritance'."
  1824. :group 'org-tags
  1825. :type '(repeat (string :tag "Tag")))
  1826. (defun org-tag-inherit-p (tag)
  1827. "Check if TAG is one that should be inherited."
  1828. (cond
  1829. ((member tag org-tags-exclude-from-inheritance) nil)
  1830. ((eq org-use-tag-inheritance t) t)
  1831. ((not org-use-tag-inheritance) nil)
  1832. ((stringp org-use-tag-inheritance)
  1833. (string-match org-use-tag-inheritance tag))
  1834. ((listp org-use-tag-inheritance)
  1835. (member tag org-use-tag-inheritance))
  1836. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1837. (defcustom org-tags-match-list-sublevels t
  1838. "Non-nil means list also sublevels of headlines matching tag search.
  1839. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1840. the sublevels of a headline matching a tag search often also match
  1841. the same search. Listing all of them can create very long lists.
  1842. Setting this variable to nil causes subtrees of a match to be skipped.
  1843. This option is off by default, because inheritance in on. If you turn
  1844. inheritance off, you very likely want to turn this option on.
  1845. As a special case, if the tag search is restricted to TODO items, the
  1846. value of this variable is ignored and sublevels are always checked, to
  1847. make sure all corresponding TODO items find their way into the list.
  1848. This variable is semi-obsolete and probably should always be true. It
  1849. is better to limit inheritance to certain tags using the variables
  1850. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1851. :group 'org-tags
  1852. :type 'boolean)
  1853. (defvar org-tags-history nil
  1854. "History of minibuffer reads for tags.")
  1855. (defvar org-last-tags-completion-table nil
  1856. "The last used completion table for tags.")
  1857. (defvar org-after-tags-change-hook nil
  1858. "Hook that is run after the tags in a line have changed.")
  1859. (defgroup org-properties nil
  1860. "Options concerning properties in Org-mode."
  1861. :tag "Org Properties"
  1862. :group 'org)
  1863. (defcustom org-property-format "%-10s %s"
  1864. "How property key/value pairs should be formatted by `indent-line'.
  1865. When `indent-line' hits a property definition, it will format the line
  1866. according to this format, mainly to make sure that the values are
  1867. lined-up with respect to each other."
  1868. :group 'org-properties
  1869. :type 'string)
  1870. (defcustom org-use-property-inheritance nil
  1871. "Non-nil means, properties apply also for sublevels.
  1872. This setting is chiefly used during property searches. Turning it on can
  1873. cause significant overhead when doing a search, which is why it is not
  1874. on by default.
  1875. When nil, only the properties directly given in the current entry count.
  1876. When t, every property is inherited. The value may also be a list of
  1877. properties that should have inheritance, or a regular expression matching
  1878. properties that should be inherited.
  1879. However, note that some special properties use inheritance under special
  1880. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1881. and the properties ending in \"_ALL\" when they are used as descriptor
  1882. for valid values of a property.
  1883. Note for programmers:
  1884. When querying an entry with `org-entry-get', you can control if inheritance
  1885. should be used. By default, `org-entry-get' looks only at the local
  1886. properties. You can request inheritance by setting the inherit argument
  1887. to t (to force inheritance) or to `selective' (to respect the setting
  1888. in this variable)."
  1889. :group 'org-properties
  1890. :type '(choice
  1891. (const :tag "Not" nil)
  1892. (const :tag "Always" t)
  1893. (repeat :tag "Specific properties" (string :tag "Property"))
  1894. (regexp :tag "Properties matched by regexp")))
  1895. (defun org-property-inherit-p (property)
  1896. "Check if PROPERTY is one that should be inherited."
  1897. (cond
  1898. ((eq org-use-property-inheritance t) t)
  1899. ((not org-use-property-inheritance) nil)
  1900. ((stringp org-use-property-inheritance)
  1901. (string-match org-use-property-inheritance property))
  1902. ((listp org-use-property-inheritance)
  1903. (member property org-use-property-inheritance))
  1904. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1905. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1906. "The default column format, if no other format has been defined.
  1907. This variable can be set on the per-file basis by inserting a line
  1908. #+COLUMNS: %25ITEM ....."
  1909. :group 'org-properties
  1910. :type 'string)
  1911. (defcustom org-columns-ellipses ".."
  1912. "The ellipses to be used when a field in column view is truncated.
  1913. When this is the empty string, as many characters as possible are shown,
  1914. but then there will be no visual indication that the field has been truncated.
  1915. When this is a string of length N, the last N characters of a truncated
  1916. field are replaced by this string. If the column is narrower than the
  1917. ellipses string, only part of the ellipses string will be shown."
  1918. :group 'org-properties
  1919. :type 'string)
  1920. (defcustom org-columns-modify-value-for-display-function nil
  1921. "Function that modifies values for display in column view.
  1922. For example, it can be used to cut out a certain part from a time stamp.
  1923. The function must take 2 arguments:
  1924. column-title The title of the column (*not* the property name)
  1925. value The value that should be modified.
  1926. The function should return the value that should be displayed,
  1927. or nil if the normal value should be used."
  1928. :group 'org-properties
  1929. :type 'function)
  1930. (defcustom org-effort-property "Effort"
  1931. "The property that is being used to keep track of effort estimates.
  1932. Effort estimates given in this property need to have the format H:MM."
  1933. :group 'org-properties
  1934. :group 'org-progress
  1935. :type '(string :tag "Property"))
  1936. (defconst org-global-properties-fixed
  1937. '(("VISIBILITY_ALL" . "folded children content all"))
  1938. "List of property/value pairs that can be inherited by any entry.
  1939. These are fixed values, for the preset properties.")
  1940. (defcustom org-global-properties nil
  1941. "List of property/value pairs that can be inherited by any entry.
  1942. You can set buffer-local values for the same purpose in the variable
  1943. `org-file-properties' this by adding lines like
  1944. #+PROPERTY: NAME VALUE"
  1945. :group 'org-properties
  1946. :type '(repeat
  1947. (cons (string :tag "Property")
  1948. (string :tag "Value"))))
  1949. (defvar org-file-properties nil
  1950. "List of property/value pairs that can be inherited by any entry.
  1951. Valid for the current buffer.
  1952. This variable is populated from #+PROPERTY lines.")
  1953. (make-variable-buffer-local 'org-file-properties)
  1954. (defgroup org-agenda nil
  1955. "Options concerning agenda views in Org-mode."
  1956. :tag "Org Agenda"
  1957. :group 'org)
  1958. (defvar org-category nil
  1959. "Variable used by org files to set a category for agenda display.
  1960. Such files should use a file variable to set it, for example
  1961. # -*- mode: org; org-category: \"ELisp\"
  1962. or contain a special line
  1963. #+CATEGORY: ELisp
  1964. If the file does not specify a category, then file's base name
  1965. is used instead.")
  1966. (make-variable-buffer-local 'org-category)
  1967. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1968. (defcustom org-agenda-files nil
  1969. "The files to be used for agenda display.
  1970. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1971. \\[org-remove-file]. You can also use customize to edit the list.
  1972. If an entry is a directory, all files in that directory that are matched by
  1973. `org-agenda-file-regexp' will be part of the file list.
  1974. If the value of the variable is not a list but a single file name, then
  1975. the list of agenda files is actually stored and maintained in that file, one
  1976. agenda file per line."
  1977. :group 'org-agenda
  1978. :type '(choice
  1979. (repeat :tag "List of files and directories" file)
  1980. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1981. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1982. "Regular expression to match files for `org-agenda-files'.
  1983. If any element in the list in that variable contains a directory instead
  1984. of a normal file, all files in that directory that are matched by this
  1985. regular expression will be included."
  1986. :group 'org-agenda
  1987. :type 'regexp)
  1988. (defcustom org-agenda-text-search-extra-files nil
  1989. "List of extra files to be searched by text search commands.
  1990. These files will be search in addition to the agenda files by the
  1991. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1992. Note that these files will only be searched for text search commands,
  1993. not for the other agenda views like todo lists, tag searches or the weekly
  1994. agenda. This variable is intended to list notes and possibly archive files
  1995. that should also be searched by these two commands.
  1996. In fact, if the first element in the list is the symbol `agenda-archives',
  1997. than all archive files of all agenda files will be added to the search
  1998. scope."
  1999. :group 'org-agenda
  2000. :type '(set :greedy t
  2001. (const :tag "Agenda Archives" agenda-archives)
  2002. (repeat :inline t (file))))
  2003. (if (fboundp 'defvaralias)
  2004. (defvaralias 'org-agenda-multi-occur-extra-files
  2005. 'org-agenda-text-search-extra-files))
  2006. (defcustom org-agenda-skip-unavailable-files nil
  2007. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2008. A nil value means to remove them, after a query, from the list."
  2009. :group 'org-agenda
  2010. :type 'boolean)
  2011. (defcustom org-calendar-to-agenda-key [?c]
  2012. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2013. The command `org-calendar-goto-agenda' will be bound to this key. The
  2014. default is the character `c' because then `c' can be used to switch back and
  2015. forth between agenda and calendar."
  2016. :group 'org-agenda
  2017. :type 'sexp)
  2018. (defcustom org-calendar-agenda-action-key [?k]
  2019. "The key to be installed in `calendar-mode-map' for agenda-action.
  2020. The command `org-agenda-action' will be bound to this key. The
  2021. default is the character `k' because we use the same key in the agenda."
  2022. :group 'org-agenda
  2023. :type 'sexp)
  2024. (eval-after-load "calendar"
  2025. '(progn
  2026. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2027. 'org-calendar-goto-agenda)
  2028. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2029. 'org-agenda-action)))
  2030. (defgroup org-latex nil
  2031. "Options for embedding LaTeX code into Org-mode."
  2032. :tag "Org LaTeX"
  2033. :group 'org)
  2034. (defcustom org-format-latex-options
  2035. '(:foreground default :background default :scale 1.0
  2036. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2037. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2038. "Options for creating images from LaTeX fragments.
  2039. This is a property list with the following properties:
  2040. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2041. `default' means use the foreground of the default face.
  2042. :background the background color, or \"Transparent\".
  2043. `default' means use the background of the default face.
  2044. :scale a scaling factor for the size of the images.
  2045. :html-foreground, :html-background, :html-scale
  2046. the same numbers for HTML export.
  2047. :matchers a list indicating which matchers should be used to
  2048. find LaTeX fragments. Valid members of this list are:
  2049. \"begin\" find environments
  2050. \"$1\" find single characters surrounded by $.$
  2051. \"$\" find math expressions surrounded by $...$
  2052. \"$$\" find math expressions surrounded by $$....$$
  2053. \"\\(\" find math expressions surrounded by \\(...\\)
  2054. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2055. :group 'org-latex
  2056. :type 'plist)
  2057. (defcustom org-format-latex-header "\\documentclass{article}
  2058. \\usepackage{fullpage} % do not remove
  2059. \\usepackage{amssymb}
  2060. \\usepackage[usenames]{color}
  2061. \\usepackage{amsmath}
  2062. \\usepackage{latexsym}
  2063. \\usepackage[mathscr]{eucal}
  2064. \\pagestyle{empty} % do not remove"
  2065. "The document header used for processing LaTeX fragments."
  2066. :group 'org-latex
  2067. :type 'string)
  2068. (defgroup org-font-lock nil
  2069. "Font-lock settings for highlighting in Org-mode."
  2070. :tag "Org Font Lock"
  2071. :group 'org)
  2072. (defcustom org-level-color-stars-only nil
  2073. "Non-nil means fontify only the stars in each headline.
  2074. When nil, the entire headline is fontified.
  2075. Changing it requires restart of `font-lock-mode' to become effective
  2076. also in regions already fontified."
  2077. :group 'org-font-lock
  2078. :type 'boolean)
  2079. (defcustom org-hide-leading-stars nil
  2080. "Non-nil means, hide the first N-1 stars in a headline.
  2081. This works by using the face `org-hide' for these stars. This
  2082. face is white for a light background, and black for a dark
  2083. background. You may have to customize the face `org-hide' to
  2084. make this work.
  2085. Changing it requires restart of `font-lock-mode' to become effective
  2086. also in regions already fontified.
  2087. You may also set this on a per-file basis by adding one of the following
  2088. lines to the buffer:
  2089. #+STARTUP: hidestars
  2090. #+STARTUP: showstars"
  2091. :group 'org-font-lock
  2092. :type 'boolean)
  2093. (defcustom org-fontify-done-headline nil
  2094. "Non-nil means, change the face of a headline if it is marked DONE.
  2095. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2096. When this is non-nil, the headline after the keyword is set to the
  2097. `org-headline-done' as an additional indication."
  2098. :group 'org-font-lock
  2099. :type 'boolean)
  2100. (defcustom org-fontify-emphasized-text t
  2101. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2102. Changing this variable requires a restart of Emacs to take effect."
  2103. :group 'org-font-lock
  2104. :type 'boolean)
  2105. (defcustom org-highlight-latex-fragments-and-specials nil
  2106. "Non-nil means, fontify what is treated specially by the exporters."
  2107. :group 'org-font-lock
  2108. :type 'boolean)
  2109. (defcustom org-hide-emphasis-markers nil
  2110. "Non-nil mean font-lock should hide the emphasis marker characters."
  2111. :group 'org-font-lock
  2112. :type 'boolean)
  2113. (defvar org-emph-re nil
  2114. "Regular expression for matching emphasis.")
  2115. (defvar org-verbatim-re nil
  2116. "Regular expression for matching verbatim text.")
  2117. (defvar org-emphasis-regexp-components) ; defined just below
  2118. (defvar org-emphasis-alist) ; defined just below
  2119. (defun org-set-emph-re (var val)
  2120. "Set variable and compute the emphasis regular expression."
  2121. (set var val)
  2122. (when (and (boundp 'org-emphasis-alist)
  2123. (boundp 'org-emphasis-regexp-components)
  2124. org-emphasis-alist org-emphasis-regexp-components)
  2125. (let* ((e org-emphasis-regexp-components)
  2126. (pre (car e))
  2127. (post (nth 1 e))
  2128. (border (nth 2 e))
  2129. (body (nth 3 e))
  2130. (nl (nth 4 e))
  2131. (body1 (concat body "*?"))
  2132. (markers (mapconcat 'car org-emphasis-alist ""))
  2133. (vmarkers (mapconcat
  2134. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2135. org-emphasis-alist "")))
  2136. ;; make sure special characters appear at the right position in the class
  2137. (if (string-match "\\^" markers)
  2138. (setq markers (concat (replace-match "" t t markers) "^")))
  2139. (if (string-match "-" markers)
  2140. (setq markers (concat (replace-match "" t t markers) "-")))
  2141. (if (string-match "\\^" vmarkers)
  2142. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2143. (if (string-match "-" vmarkers)
  2144. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2145. (if (> nl 0)
  2146. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2147. (int-to-string nl) "\\}")))
  2148. ;; Make the regexp
  2149. (setq org-emph-re
  2150. (concat "\\([" pre "]\\|^\\)"
  2151. "\\("
  2152. "\\([" markers "]\\)"
  2153. "\\("
  2154. "[^" border "]\\|"
  2155. "[^" border "]"
  2156. body1
  2157. "[^" border "]"
  2158. "\\)"
  2159. "\\3\\)"
  2160. "\\([" post "]\\|$\\)"))
  2161. (setq org-verbatim-re
  2162. (concat "\\([" pre "]\\|^\\)"
  2163. "\\("
  2164. "\\([" vmarkers "]\\)"
  2165. "\\("
  2166. "[^" border "]\\|"
  2167. "[^" border "]"
  2168. body1
  2169. "[^" border "]"
  2170. "\\)"
  2171. "\\3\\)"
  2172. "\\([" post "]\\|$\\)")))))
  2173. (defcustom org-emphasis-regexp-components
  2174. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2175. "Components used to build the regular expression for emphasis.
  2176. This is a list with 6 entries. Terminology: In an emphasis string
  2177. like \" *strong word* \", we call the initial space PREMATCH, the final
  2178. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2179. and \"trong wor\" is the body. The different components in this variable
  2180. specify what is allowed/forbidden in each part:
  2181. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2182. post Chars allowed as postmatch. End of line will be allowed too.
  2183. border The chars *forbidden* as border characters.
  2184. body-regexp A regexp like \".\" to match a body character. Don't use
  2185. non-shy groups here, and don't allow newline here.
  2186. newline The maximum number of newlines allowed in an emphasis exp.
  2187. Use customize to modify this, or restart Emacs after changing it."
  2188. :group 'org-font-lock
  2189. :set 'org-set-emph-re
  2190. :type '(list
  2191. (sexp :tag "Allowed chars in pre ")
  2192. (sexp :tag "Allowed chars in post ")
  2193. (sexp :tag "Forbidden chars in border ")
  2194. (sexp :tag "Regexp for body ")
  2195. (integer :tag "number of newlines allowed")
  2196. (option (boolean :tag "Please ignore this button"))))
  2197. (defcustom org-emphasis-alist
  2198. `(("*" bold "<b>" "</b>")
  2199. ("/" italic "<i>" "</i>")
  2200. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2201. ("=" org-code "<code>" "</code>" verbatim)
  2202. ("~" org-verbatim "<code>" "</code>" verbatim)
  2203. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2204. "<del>" "</del>")
  2205. )
  2206. "Special syntax for emphasized text.
  2207. Text starting and ending with a special character will be emphasized, for
  2208. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2209. characters, the face to be used by font-lock for highlighting in Org-mode
  2210. Emacs buffers, and the HTML tags to be used for this.
  2211. Use customize to modify this, or restart Emacs after changing it."
  2212. :group 'org-font-lock
  2213. :set 'org-set-emph-re
  2214. :type '(repeat
  2215. (list
  2216. (string :tag "Marker character")
  2217. (choice
  2218. (face :tag "Font-lock-face")
  2219. (plist :tag "Face property list"))
  2220. (string :tag "HTML start tag")
  2221. (string :tag "HTML end tag")
  2222. (option (const verbatim)))))
  2223. ;;; Miscellaneous options
  2224. (defgroup org-completion nil
  2225. "Completion in Org-mode."
  2226. :tag "Org Completion"
  2227. :group 'org)
  2228. (defcustom org-completion-use-ido nil
  2229. "Non-nil means, use ido completion wherever possible.
  2230. Note that `ido-mode' must be active for this variable to be relevant.
  2231. If you decide to turn this variable on, you might well want to turn off
  2232. `org-outline-path-complete-in-steps'."
  2233. :group 'org-completion
  2234. :type 'boolean)
  2235. (defcustom org-completion-fallback-command 'hippie-expand
  2236. "The expansion command called by \\[org-complete] in normal context.
  2237. Normal means, no org-mode-specific context."
  2238. :group 'org-completion
  2239. :type 'function)
  2240. ;;; Functions and variables from ther packages
  2241. ;; Declared here to avoid compiler warnings
  2242. ;; XEmacs only
  2243. (defvar outline-mode-menu-heading)
  2244. (defvar outline-mode-menu-show)
  2245. (defvar outline-mode-menu-hide)
  2246. (defvar zmacs-regions) ; XEmacs regions
  2247. ;; Emacs only
  2248. (defvar mark-active)
  2249. ;; Various packages
  2250. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2251. (declare-function calendar-forward-day "cal-move" (arg))
  2252. (declare-function calendar-goto-date "cal-move" (date))
  2253. (declare-function calendar-goto-today "cal-move" ())
  2254. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2255. (defvar calc-embedded-close-formula)
  2256. (defvar calc-embedded-open-formula)
  2257. (declare-function cdlatex-tab "ext:cdlatex" ())
  2258. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2259. (defvar font-lock-unfontify-region-function)
  2260. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2261. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2262. (defvar iswitchb-temp-buflist)
  2263. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2264. (defvar org-agenda-tags-todo-honor-ignore-options)
  2265. (declare-function org-agenda-skip "org-agenda" ())
  2266. (declare-function org-format-agenda-item "org-agenda"
  2267. (extra txt &optional category tags dotime noprefix remove-re))
  2268. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2269. (declare-function org-agenda-change-all-lines "org-agenda"
  2270. (newhead hdmarker &optional fixface just-this))
  2271. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2272. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2273. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2274. (beg end))
  2275. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2276. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2277. "org-agenda" (&optional end))
  2278. (declare-function parse-time-string "parse-time" (string))
  2279. (declare-function remember "remember" (&optional initial))
  2280. (declare-function remember-buffer-desc "remember" ())
  2281. (declare-function remember-finalize "remember" ())
  2282. (defvar remember-save-after-remembering)
  2283. (defvar remember-data-file)
  2284. (defvar remember-register)
  2285. (defvar remember-buffer)
  2286. (defvar remember-handler-functions)
  2287. (defvar remember-annotation-functions)
  2288. (defvar texmathp-why)
  2289. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2290. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2291. (defvar w3m-current-url)
  2292. (defvar w3m-current-title)
  2293. (defvar org-latex-regexps)
  2294. ;;; Autoload and prepare some org modules
  2295. ;; Some table stuff that needs to be defined here, because it is used
  2296. ;; by the functions setting up org-mode or checking for table context.
  2297. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2298. "Detects an org-type or table-type table.")
  2299. (defconst org-table-line-regexp "^[ \t]*|"
  2300. "Detects an org-type table line.")
  2301. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2302. "Detects an org-type table line.")
  2303. (defconst org-table-hline-regexp "^[ \t]*|-"
  2304. "Detects an org-type table hline.")
  2305. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2306. "Detects a table-type table hline.")
  2307. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2308. "Searching from within a table (any type) this finds the first line
  2309. outside the table.")
  2310. ;; Autoload the functions in org-table.el that are needed by functions here.
  2311. (eval-and-compile
  2312. (org-autoload "org-table"
  2313. '(org-table-align org-table-begin org-table-blank-field
  2314. org-table-convert org-table-convert-region org-table-copy-down
  2315. org-table-copy-region org-table-create
  2316. org-table-create-or-convert-from-region
  2317. org-table-create-with-table.el org-table-current-dline
  2318. org-table-cut-region org-table-delete-column org-table-edit-field
  2319. org-table-edit-formulas org-table-end org-table-eval-formula
  2320. org-table-export org-table-field-info
  2321. org-table-get-stored-formulas org-table-goto-column
  2322. org-table-hline-and-move org-table-import org-table-insert-column
  2323. org-table-insert-hline org-table-insert-row org-table-iterate
  2324. org-table-justify-field-maybe org-table-kill-row
  2325. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2326. org-table-move-column org-table-move-column-left
  2327. org-table-move-column-right org-table-move-row
  2328. org-table-move-row-down org-table-move-row-up
  2329. org-table-next-field org-table-next-row org-table-paste-rectangle
  2330. org-table-previous-field org-table-recalculate
  2331. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2332. org-table-toggle-coordinate-overlays
  2333. org-table-toggle-formula-debugger org-table-wrap-region
  2334. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2335. (defun org-at-table-p (&optional table-type)
  2336. "Return t if the cursor is inside an org-type table.
  2337. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2338. (if org-enable-table-editor
  2339. (save-excursion
  2340. (beginning-of-line 1)
  2341. (looking-at (if table-type org-table-any-line-regexp
  2342. org-table-line-regexp)))
  2343. nil))
  2344. (defsubst org-table-p () (org-at-table-p))
  2345. (defun org-at-table.el-p ()
  2346. "Return t if and only if we are at a table.el table."
  2347. (and (org-at-table-p 'any)
  2348. (save-excursion
  2349. (goto-char (org-table-begin 'any))
  2350. (looking-at org-table1-hline-regexp))))
  2351. (defun org-table-recognize-table.el ()
  2352. "If there is a table.el table nearby, recognize it and move into it."
  2353. (if org-table-tab-recognizes-table.el
  2354. (if (org-at-table.el-p)
  2355. (progn
  2356. (beginning-of-line 1)
  2357. (if (looking-at org-table-dataline-regexp)
  2358. nil
  2359. (if (looking-at org-table1-hline-regexp)
  2360. (progn
  2361. (beginning-of-line 2)
  2362. (if (looking-at org-table-any-border-regexp)
  2363. (beginning-of-line -1)))))
  2364. (if (re-search-forward "|" (org-table-end t) t)
  2365. (progn
  2366. (require 'table)
  2367. (if (table--at-cell-p (point))
  2368. t
  2369. (message "recognizing table.el table...")
  2370. (table-recognize-table)
  2371. (message "recognizing table.el table...done")))
  2372. (error "This should not happen..."))
  2373. t)
  2374. nil)
  2375. nil))
  2376. (defun org-at-table-hline-p ()
  2377. "Return t if the cursor is inside a hline in a table."
  2378. (if org-enable-table-editor
  2379. (save-excursion
  2380. (beginning-of-line 1)
  2381. (looking-at org-table-hline-regexp))
  2382. nil))
  2383. (defvar org-table-clean-did-remove-column nil)
  2384. (defun org-table-map-tables (function)
  2385. "Apply FUNCTION to the start of all tables in the buffer."
  2386. (save-excursion
  2387. (save-restriction
  2388. (widen)
  2389. (goto-char (point-min))
  2390. (while (re-search-forward org-table-any-line-regexp nil t)
  2391. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2392. (beginning-of-line 1)
  2393. (if (looking-at org-table-line-regexp)
  2394. (save-excursion (funcall function)))
  2395. (re-search-forward org-table-any-border-regexp nil 1))))
  2396. (message "Mapping tables: done"))
  2397. ;; Declare and autoload functions from org-exp.el
  2398. (declare-function org-default-export-plist "org-exp")
  2399. (declare-function org-infile-export-plist "org-exp")
  2400. (declare-function org-get-current-options "org-exp")
  2401. (eval-and-compile
  2402. (org-autoload "org-exp"
  2403. '(org-export org-export-as-ascii org-export-visible
  2404. org-insert-export-options-template org-export-as-html-and-open
  2405. org-export-as-html-batch org-export-as-html-to-buffer
  2406. org-replace-region-by-html org-export-region-as-html
  2407. org-export-as-html org-export-icalendar-this-file
  2408. org-export-icalendar-all-agenda-files
  2409. org-table-clean-before-export
  2410. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2411. ;; Declare and autoload functions from org-agenda.el
  2412. (eval-and-compile
  2413. (org-autoload "org-agenda"
  2414. '(org-agenda org-agenda-list org-search-view
  2415. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2416. org-diary org-agenda-to-appt
  2417. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2418. ;; Autoload org-remember
  2419. (eval-and-compile
  2420. (org-autoload "org-remember"
  2421. '(org-remember-insinuate org-remember-annotation
  2422. org-remember-apply-template org-remember org-remember-handler)))
  2423. ;; Autoload org-clock.el
  2424. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2425. (beg end))
  2426. (declare-function org-clock-update-mode-line "org-clock" ())
  2427. (defvar org-clock-start-time)
  2428. (defvar org-clock-marker (make-marker)
  2429. "Marker recording the last clock-in.")
  2430. (eval-and-compile
  2431. (org-autoload
  2432. "org-clock"
  2433. '(org-clock-in org-clock-out org-clock-cancel
  2434. org-clock-goto org-clock-sum org-clock-display
  2435. org-clock-remove-overlays org-clock-report
  2436. org-clocktable-shift org-dblock-write:clocktable
  2437. org-get-clocktable)))
  2438. (defun org-clock-update-time-maybe ()
  2439. "If this is a CLOCK line, update it and return t.
  2440. Otherwise, return nil."
  2441. (interactive)
  2442. (save-excursion
  2443. (beginning-of-line 1)
  2444. (skip-chars-forward " \t")
  2445. (when (looking-at org-clock-string)
  2446. (let ((re (concat "[ \t]*" org-clock-string
  2447. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2448. "\\([ \t]*=>.*\\)?\\)?"))
  2449. ts te h m s neg)
  2450. (cond
  2451. ((not (looking-at re))
  2452. nil)
  2453. ((not (match-end 2))
  2454. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2455. (> org-clock-marker (point))
  2456. (<= org-clock-marker (point-at-eol)))
  2457. ;; The clock is running here
  2458. (setq org-clock-start-time
  2459. (apply 'encode-time
  2460. (org-parse-time-string (match-string 1))))
  2461. (org-clock-update-mode-line)))
  2462. (t
  2463. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2464. (end-of-line 1)
  2465. (setq ts (match-string 1)
  2466. te (match-string 3))
  2467. (setq s (- (time-to-seconds
  2468. (apply 'encode-time (org-parse-time-string te)))
  2469. (time-to-seconds
  2470. (apply 'encode-time (org-parse-time-string ts))))
  2471. neg (< s 0)
  2472. s (abs s)
  2473. h (floor (/ s 3600))
  2474. s (- s (* 3600 h))
  2475. m (floor (/ s 60))
  2476. s (- s (* 60 s)))
  2477. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2478. t))))))
  2479. (defun org-check-running-clock ()
  2480. "Check if the current buffer contains the running clock.
  2481. If yes, offer to stop it and to save the buffer with the changes."
  2482. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2483. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2484. (buffer-name))))
  2485. (org-clock-out)
  2486. (when (y-or-n-p "Save changed buffer?")
  2487. (save-buffer))))
  2488. (defun org-clocktable-try-shift (dir n)
  2489. "Check if this line starts a clock table, if yes, shift the time block."
  2490. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2491. (org-clocktable-shift dir n)))
  2492. ;; Autoload org-timer.el
  2493. ;(declare-function org-timer "org-timer")
  2494. (eval-and-compile
  2495. (org-autoload
  2496. "org-timer"
  2497. '(org-timer-start org-timer org-timer-item
  2498. org-timer-change-times-in-region)))
  2499. ;; Autoload archiving code
  2500. ;; The stuff that is needed for cycling and tags has to be defined here.
  2501. (defgroup org-archive nil
  2502. "Options concerning archiving in Org-mode."
  2503. :tag "Org Archive"
  2504. :group 'org-structure)
  2505. (defcustom org-archive-location "%s_archive::"
  2506. "The location where subtrees should be archived.
  2507. The value of this variable is a string, consisting of two parts,
  2508. separated by a double-colon. The first part is a filename and
  2509. the second part is a headline.
  2510. When the filename is omitted, archiving happens in the same file.
  2511. %s in the filename will be replaced by the current file
  2512. name (without the directory part). Archiving to a different file
  2513. is useful to keep archived entries from contributing to the
  2514. Org-mode Agenda.
  2515. The archived entries will be filed as subtrees of the specified
  2516. headline. When the headline is omitted, the subtrees are simply
  2517. filed away at the end of the file, as top-level entries. Also in
  2518. the heading you can use %s to represent the file name, this can be
  2519. useful when using the same archive for a number of different files.
  2520. Here are a few examples:
  2521. \"%s_archive::\"
  2522. If the current file is Projects.org, archive in file
  2523. Projects.org_archive, as top-level trees. This is the default.
  2524. \"::* Archived Tasks\"
  2525. Archive in the current file, under the top-level headline
  2526. \"* Archived Tasks\".
  2527. \"~/org/archive.org::\"
  2528. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2529. \"~/org/archive.org::From %s\"
  2530. Archive in file ~/org/archive.org (absolute path), und headlines
  2531. \"From FILENAME\" where file name is the current file name.
  2532. \"basement::** Finished Tasks\"
  2533. Archive in file ./basement (relative path), as level 3 trees
  2534. below the level 2 heading \"** Finished Tasks\".
  2535. You may set this option on a per-file basis by adding to the buffer a
  2536. line like
  2537. #+ARCHIVE: basement::** Finished Tasks
  2538. You may also define it locally for a subtree by setting an ARCHIVE property
  2539. in the entry. If such a property is found in an entry, or anywhere up
  2540. the hierarchy, it will be used."
  2541. :group 'org-archive
  2542. :type 'string)
  2543. (defcustom org-archive-tag "ARCHIVE"
  2544. "The tag that marks a subtree as archived.
  2545. An archived subtree does not open during visibility cycling, and does
  2546. not contribute to the agenda listings.
  2547. After changing this, font-lock must be restarted in the relevant buffers to
  2548. get the proper fontification."
  2549. :group 'org-archive
  2550. :group 'org-keywords
  2551. :type 'string)
  2552. (defcustom org-agenda-skip-archived-trees t
  2553. "Non-nil means, the agenda will skip any items located in archived trees.
  2554. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2555. variable is no longer recommended, you should leave it at the value t.
  2556. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2557. :group 'org-archive
  2558. :group 'org-agenda-skip
  2559. :type 'boolean)
  2560. (defcustom org-cycle-open-archived-trees nil
  2561. "Non-nil means, `org-cycle' will open archived trees.
  2562. An archived tree is a tree marked with the tag ARCHIVE.
  2563. When nil, archived trees will stay folded. You can still open them with
  2564. normal outline commands like `show-all', but not with the cycling commands."
  2565. :group 'org-archive
  2566. :group 'org-cycle
  2567. :type 'boolean)
  2568. (defcustom org-sparse-tree-open-archived-trees nil
  2569. "Non-nil means sparse tree construction shows matches in archived trees.
  2570. When nil, matches in these trees are highlighted, but the trees are kept in
  2571. collapsed state."
  2572. :group 'org-archive
  2573. :group 'org-sparse-trees
  2574. :type 'boolean)
  2575. (defun org-cycle-hide-archived-subtrees (state)
  2576. "Re-hide all archived subtrees after a visibility state change."
  2577. (when (and (not org-cycle-open-archived-trees)
  2578. (not (memq state '(overview folded))))
  2579. (save-excursion
  2580. (let* ((globalp (memq state '(contents all)))
  2581. (beg (if globalp (point-min) (point)))
  2582. (end (if globalp (point-max) (org-end-of-subtree t))))
  2583. (org-hide-archived-subtrees beg end)
  2584. (goto-char beg)
  2585. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2586. (message "%s" (substitute-command-keys
  2587. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2588. (defun org-force-cycle-archived ()
  2589. "Cycle subtree even if it is archived."
  2590. (interactive)
  2591. (setq this-command 'org-cycle)
  2592. (let ((org-cycle-open-archived-trees t))
  2593. (call-interactively 'org-cycle)))
  2594. (defun org-hide-archived-subtrees (beg end)
  2595. "Re-hide all archived subtrees after a visibility state change."
  2596. (save-excursion
  2597. (let* ((re (concat ":" org-archive-tag ":")))
  2598. (goto-char beg)
  2599. (while (re-search-forward re end t)
  2600. (and (org-on-heading-p) (hide-subtree))
  2601. (org-end-of-subtree t)))))
  2602. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2603. (eval-and-compile
  2604. (org-autoload "org-archive"
  2605. '(org-add-archive-files org-archive-subtree
  2606. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2607. ;; Autoload Column View Code
  2608. (declare-function org-columns-number-to-string "org-colview")
  2609. (declare-function org-columns-get-format-and-top-level "org-colview")
  2610. (declare-function org-columns-compute "org-colview")
  2611. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2612. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2613. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2614. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2615. ;; Autoload ID code
  2616. (declare-function org-id-store-link "org-id")
  2617. (org-autoload "org-id"
  2618. '(org-id-get-create org-id-new org-id-copy org-id-get
  2619. org-id-get-with-outline-path-completion
  2620. org-id-get-with-outline-drilling
  2621. org-id-goto org-id-find org-id-store-link))
  2622. ;;; Variables for pre-computed regular expressions, all buffer local
  2623. (defvar org-drawer-regexp nil
  2624. "Matches first line of a hidden block.")
  2625. (make-variable-buffer-local 'org-drawer-regexp)
  2626. (defvar org-todo-regexp nil
  2627. "Matches any of the TODO state keywords.")
  2628. (make-variable-buffer-local 'org-todo-regexp)
  2629. (defvar org-not-done-regexp nil
  2630. "Matches any of the TODO state keywords except the last one.")
  2631. (make-variable-buffer-local 'org-not-done-regexp)
  2632. (defvar org-todo-line-regexp nil
  2633. "Matches a headline and puts TODO state into group 2 if present.")
  2634. (make-variable-buffer-local 'org-todo-line-regexp)
  2635. (defvar org-complex-heading-regexp nil
  2636. "Matches a headline and puts everything into groups:
  2637. group 1: the stars
  2638. group 2: The todo keyword, maybe
  2639. group 3: Priority cookie
  2640. group 4: True headline
  2641. group 5: Tags")
  2642. (make-variable-buffer-local 'org-complex-heading-regexp)
  2643. (defvar org-todo-line-tags-regexp nil
  2644. "Matches a headline and puts TODO state into group 2 if present.
  2645. Also put tags into group 4 if tags are present.")
  2646. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2647. (defvar org-nl-done-regexp nil
  2648. "Matches newline followed by a headline with the DONE keyword.")
  2649. (make-variable-buffer-local 'org-nl-done-regexp)
  2650. (defvar org-looking-at-done-regexp nil
  2651. "Matches the DONE keyword a point.")
  2652. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2653. (defvar org-ds-keyword-length 12
  2654. "Maximum length of the Deadline and SCHEDULED keywords.")
  2655. (make-variable-buffer-local 'org-ds-keyword-length)
  2656. (defvar org-deadline-regexp nil
  2657. "Matches the DEADLINE keyword.")
  2658. (make-variable-buffer-local 'org-deadline-regexp)
  2659. (defvar org-deadline-time-regexp nil
  2660. "Matches the DEADLINE keyword together with a time stamp.")
  2661. (make-variable-buffer-local 'org-deadline-time-regexp)
  2662. (defvar org-deadline-line-regexp nil
  2663. "Matches the DEADLINE keyword and the rest of the line.")
  2664. (make-variable-buffer-local 'org-deadline-line-regexp)
  2665. (defvar org-scheduled-regexp nil
  2666. "Matches the SCHEDULED keyword.")
  2667. (make-variable-buffer-local 'org-scheduled-regexp)
  2668. (defvar org-scheduled-time-regexp nil
  2669. "Matches the SCHEDULED keyword together with a time stamp.")
  2670. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2671. (defvar org-closed-time-regexp nil
  2672. "Matches the CLOSED keyword together with a time stamp.")
  2673. (make-variable-buffer-local 'org-closed-time-regexp)
  2674. (defvar org-keyword-time-regexp nil
  2675. "Matches any of the 4 keywords, together with the time stamp.")
  2676. (make-variable-buffer-local 'org-keyword-time-regexp)
  2677. (defvar org-keyword-time-not-clock-regexp nil
  2678. "Matches any of the 3 keywords, together with the time stamp.")
  2679. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2680. (defvar org-maybe-keyword-time-regexp nil
  2681. "Matches a timestamp, possibly preceeded by a keyword.")
  2682. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2683. (defvar org-planning-or-clock-line-re nil
  2684. "Matches a line with planning or clock info.")
  2685. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2686. (defconst org-plain-time-of-day-regexp
  2687. (concat
  2688. "\\(\\<[012]?[0-9]"
  2689. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2690. "\\(--?"
  2691. "\\(\\<[012]?[0-9]"
  2692. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2693. "\\)?")
  2694. "Regular expression to match a plain time or time range.
  2695. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2696. groups carry important information:
  2697. 0 the full match
  2698. 1 the first time, range or not
  2699. 8 the second time, if it is a range.")
  2700. (defconst org-plain-time-extension-regexp
  2701. (concat
  2702. "\\(\\<[012]?[0-9]"
  2703. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2704. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2705. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2706. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2707. groups carry important information:
  2708. 0 the full match
  2709. 7 hours of duration
  2710. 9 minutes of duration")
  2711. (defconst org-stamp-time-of-day-regexp
  2712. (concat
  2713. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2714. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2715. "\\(--?"
  2716. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2717. "Regular expression to match a timestamp time or time range.
  2718. After a match, the following groups carry important information:
  2719. 0 the full match
  2720. 1 date plus weekday, for backreferencing to make sure both times on same day
  2721. 2 the first time, range or not
  2722. 4 the second time, if it is a range.")
  2723. (defconst org-startup-options
  2724. '(("fold" org-startup-folded t)
  2725. ("overview" org-startup-folded t)
  2726. ("nofold" org-startup-folded nil)
  2727. ("showall" org-startup-folded nil)
  2728. ("content" org-startup-folded content)
  2729. ("hidestars" org-hide-leading-stars t)
  2730. ("showstars" org-hide-leading-stars nil)
  2731. ("odd" org-odd-levels-only t)
  2732. ("oddeven" org-odd-levels-only nil)
  2733. ("align" org-startup-align-all-tables t)
  2734. ("noalign" org-startup-align-all-tables nil)
  2735. ("customtime" org-display-custom-times t)
  2736. ("logdone" org-log-done time)
  2737. ("lognotedone" org-log-done note)
  2738. ("nologdone" org-log-done nil)
  2739. ("lognoteclock-out" org-log-note-clock-out t)
  2740. ("nolognoteclock-out" org-log-note-clock-out nil)
  2741. ("logrepeat" org-log-repeat state)
  2742. ("lognoterepeat" org-log-repeat note)
  2743. ("nologrepeat" org-log-repeat nil)
  2744. ("fninline" org-footnote-define-inline t)
  2745. ("nofninline" org-footnote-define-inline nil)
  2746. ("fnlocal" org-footnote-section nil)
  2747. ("fnauto" org-footnote-auto-label t)
  2748. ("fnprompt" org-footnote-auto-label nil)
  2749. ("fnconfirm" org-footnote-auto-label confirm)
  2750. ("fnplain" org-footnote-auto-label plain)
  2751. ("constcgs" constants-unit-system cgs)
  2752. ("constSI" constants-unit-system SI))
  2753. "Variable associated with STARTUP options for org-mode.
  2754. Each element is a list of three items: The startup options as written
  2755. in the #+STARTUP line, the corresponding variable, and the value to
  2756. set this variable to if the option is found. An optional forth element PUSH
  2757. means to push this value onto the list in the variable.")
  2758. (defun org-set-regexps-and-options ()
  2759. "Precompute regular expressions for current buffer."
  2760. (when (org-mode-p)
  2761. (org-set-local 'org-todo-kwd-alist nil)
  2762. (org-set-local 'org-todo-key-alist nil)
  2763. (org-set-local 'org-todo-key-trigger nil)
  2764. (org-set-local 'org-todo-keywords-1 nil)
  2765. (org-set-local 'org-done-keywords nil)
  2766. (org-set-local 'org-todo-heads nil)
  2767. (org-set-local 'org-todo-sets nil)
  2768. (org-set-local 'org-todo-log-states nil)
  2769. (org-set-local 'org-file-properties nil)
  2770. (org-set-local 'org-file-tags nil)
  2771. (let ((re (org-make-options-regexp
  2772. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2773. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2774. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2775. (splitre "[ \t]+")
  2776. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2777. tail sep kws1 prio props ftags drawers
  2778. ext-setup-or-nil setup-contents (start 0))
  2779. (save-excursion
  2780. (save-restriction
  2781. (widen)
  2782. (goto-char (point-min))
  2783. (while (or (and ext-setup-or-nil
  2784. (string-match re ext-setup-or-nil start)
  2785. (setq start (match-end 0)))
  2786. (and (setq ext-setup-or-nil nil start 0)
  2787. (re-search-forward re nil t)))
  2788. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2789. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2790. (cond
  2791. ((equal key "CATEGORY")
  2792. (if (string-match "[ \t]+$" value)
  2793. (setq value (replace-match "" t t value)))
  2794. (setq cat value))
  2795. ((member key '("SEQ_TODO" "TODO"))
  2796. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2797. ((equal key "TYP_TODO")
  2798. (push (cons 'type (org-split-string value splitre)) kwds))
  2799. ((equal key "TAGS")
  2800. (setq tags (append tags (org-split-string value splitre))))
  2801. ((equal key "COLUMNS")
  2802. (org-set-local 'org-columns-default-format value))
  2803. ((equal key "LINK")
  2804. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2805. (push (cons (match-string 1 value)
  2806. (org-trim (match-string 2 value)))
  2807. links)))
  2808. ((equal key "PRIORITIES")
  2809. (setq prio (org-split-string value " +")))
  2810. ((equal key "PROPERTY")
  2811. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2812. (push (cons (match-string 1 value) (match-string 2 value))
  2813. props)))
  2814. ((equal key "FILETAGS")
  2815. (when (string-match "\\S-" value)
  2816. (setq ftags
  2817. (append
  2818. ftags
  2819. (apply 'append
  2820. (mapcar (lambda (x) (org-split-string x ":"))
  2821. (org-split-string value)))))))
  2822. ((equal key "DRAWERS")
  2823. (setq drawers (org-split-string value splitre)))
  2824. ((equal key "CONSTANTS")
  2825. (setq const (append const (org-split-string value splitre))))
  2826. ((equal key "STARTUP")
  2827. (let ((opts (org-split-string value splitre))
  2828. l var val)
  2829. (while (setq l (pop opts))
  2830. (when (setq l (assoc l org-startup-options))
  2831. (setq var (nth 1 l) val (nth 2 l))
  2832. (if (not (nth 3 l))
  2833. (set (make-local-variable var) val)
  2834. (if (not (listp (symbol-value var)))
  2835. (set (make-local-variable var) nil))
  2836. (set (make-local-variable var) (symbol-value var))
  2837. (add-to-list var val))))))
  2838. ((equal key "ARCHIVE")
  2839. (string-match " *$" value)
  2840. (setq arch (replace-match "" t t value))
  2841. (remove-text-properties 0 (length arch)
  2842. '(face t fontified t) arch))
  2843. ((equal key "SETUPFILE")
  2844. (setq setup-contents (org-file-contents
  2845. (expand-file-name
  2846. (org-remove-double-quotes value))
  2847. 'noerror))
  2848. (if (not ext-setup-or-nil)
  2849. (setq ext-setup-or-nil setup-contents start 0)
  2850. (setq ext-setup-or-nil
  2851. (concat (substring ext-setup-or-nil 0 start)
  2852. "\n" setup-contents "\n"
  2853. (substring ext-setup-or-nil start)))))
  2854. ))))
  2855. (when cat
  2856. (org-set-local 'org-category (intern cat))
  2857. (push (cons "CATEGORY" cat) props))
  2858. (when prio
  2859. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2860. (setq prio (mapcar 'string-to-char prio))
  2861. (org-set-local 'org-highest-priority (nth 0 prio))
  2862. (org-set-local 'org-lowest-priority (nth 1 prio))
  2863. (org-set-local 'org-default-priority (nth 2 prio)))
  2864. (and props (org-set-local 'org-file-properties (nreverse props)))
  2865. (and ftags (org-set-local 'org-file-tags ftags))
  2866. (and drawers (org-set-local 'org-drawers drawers))
  2867. (and arch (org-set-local 'org-archive-location arch))
  2868. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2869. ;; Process the TODO keywords
  2870. (unless kwds
  2871. ;; Use the global values as if they had been given locally.
  2872. (setq kwds (default-value 'org-todo-keywords))
  2873. (if (stringp (car kwds))
  2874. (setq kwds (list (cons org-todo-interpretation
  2875. (default-value 'org-todo-keywords)))))
  2876. (setq kwds (reverse kwds)))
  2877. (setq kwds (nreverse kwds))
  2878. (let (inter kws kw)
  2879. (while (setq kws (pop kwds))
  2880. (setq inter (pop kws) sep (member "|" kws)
  2881. kws0 (delete "|" (copy-sequence kws))
  2882. kwsa nil
  2883. kws1 (mapcar
  2884. (lambda (x)
  2885. ;; 1 2
  2886. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2887. (progn
  2888. (setq kw (match-string 1 x)
  2889. key (and (match-end 2) (match-string 2 x))
  2890. log (org-extract-log-state-settings x))
  2891. (push (cons kw (and key (string-to-char key))) kwsa)
  2892. (and log (push log org-todo-log-states))
  2893. kw)
  2894. (error "Invalid TODO keyword %s" x)))
  2895. kws0)
  2896. kwsa (if kwsa (append '((:startgroup))
  2897. (nreverse kwsa)
  2898. '((:endgroup))))
  2899. hw (car kws1)
  2900. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2901. tail (list inter hw (car dws) (org-last dws)))
  2902. (add-to-list 'org-todo-heads hw 'append)
  2903. (push kws1 org-todo-sets)
  2904. (setq org-done-keywords (append org-done-keywords dws nil))
  2905. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2906. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2907. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2908. (setq org-todo-sets (nreverse org-todo-sets)
  2909. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2910. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2911. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2912. ;; Process the constants
  2913. (when const
  2914. (let (e cst)
  2915. (while (setq e (pop const))
  2916. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2917. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2918. (setq org-table-formula-constants-local cst)))
  2919. ;; Process the tags.
  2920. (when tags
  2921. (let (e tgs)
  2922. (while (setq e (pop tags))
  2923. (cond
  2924. ((equal e "{") (push '(:startgroup) tgs))
  2925. ((equal e "}") (push '(:endgroup) tgs))
  2926. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2927. (push (cons (match-string 1 e)
  2928. (string-to-char (match-string 2 e)))
  2929. tgs))
  2930. (t (push (list e) tgs))))
  2931. (org-set-local 'org-tag-alist nil)
  2932. (while (setq e (pop tgs))
  2933. (or (and (stringp (car e))
  2934. (assoc (car e) org-tag-alist))
  2935. (push e org-tag-alist)))))
  2936. ;; Compute the regular expressions and other local variables
  2937. (if (not org-done-keywords)
  2938. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2939. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2940. (length org-scheduled-string)
  2941. (length org-clock-string)
  2942. (length org-closed-string)))
  2943. org-drawer-regexp
  2944. (concat "^[ \t]*:\\("
  2945. (mapconcat 'regexp-quote org-drawers "\\|")
  2946. "\\):[ \t]*$")
  2947. org-not-done-keywords
  2948. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2949. org-todo-regexp
  2950. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2951. "\\|") "\\)\\>")
  2952. org-not-done-regexp
  2953. (concat "\\<\\("
  2954. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2955. "\\)\\>")
  2956. org-todo-line-regexp
  2957. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2958. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2959. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2960. org-complex-heading-regexp
  2961. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2962. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2963. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2964. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2965. org-nl-done-regexp
  2966. (concat "\n\\*+[ \t]+"
  2967. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2968. "\\)" "\\>")
  2969. org-todo-line-tags-regexp
  2970. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2971. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2972. (org-re
  2973. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2974. org-looking-at-done-regexp
  2975. (concat "^" "\\(?:"
  2976. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2977. "\\>")
  2978. org-deadline-regexp (concat "\\<" org-deadline-string)
  2979. org-deadline-time-regexp
  2980. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2981. org-deadline-line-regexp
  2982. (concat "\\<\\(" org-deadline-string "\\).*")
  2983. org-scheduled-regexp
  2984. (concat "\\<" org-scheduled-string)
  2985. org-scheduled-time-regexp
  2986. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2987. org-closed-time-regexp
  2988. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2989. org-keyword-time-regexp
  2990. (concat "\\<\\(" org-scheduled-string
  2991. "\\|" org-deadline-string
  2992. "\\|" org-closed-string
  2993. "\\|" org-clock-string "\\)"
  2994. " *[[<]\\([^]>]+\\)[]>]")
  2995. org-keyword-time-not-clock-regexp
  2996. (concat "\\<\\(" org-scheduled-string
  2997. "\\|" org-deadline-string
  2998. "\\|" org-closed-string
  2999. "\\)"
  3000. " *[[<]\\([^]>]+\\)[]>]")
  3001. org-maybe-keyword-time-regexp
  3002. (concat "\\(\\<\\(" org-scheduled-string
  3003. "\\|" org-deadline-string
  3004. "\\|" org-closed-string
  3005. "\\|" org-clock-string "\\)\\)?"
  3006. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3007. org-planning-or-clock-line-re
  3008. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3009. "\\|" org-deadline-string
  3010. "\\|" org-closed-string "\\|" org-clock-string
  3011. "\\)\\>\\)")
  3012. )
  3013. (org-compute-latex-and-specials-regexp)
  3014. (org-set-font-lock-defaults))))
  3015. (defun org-file-contents (file &optional noerror)
  3016. "Return the contents of FILE, as a string."
  3017. (if (or (not file)
  3018. (not (file-readable-p file)))
  3019. (if noerror
  3020. (progn
  3021. (message "Cannot read file %s" file)
  3022. (ding) (sit-for 2)
  3023. "")
  3024. (error "Cannot read file %s" file))
  3025. (with-temp-buffer
  3026. (insert-file-contents file)
  3027. (buffer-string))))
  3028. (defun org-extract-log-state-settings (x)
  3029. "Extract the log state setting from a TODO keyword string.
  3030. This will extract info from a string like \"WAIT(w@/!)\"."
  3031. (let (kw key log1 log2)
  3032. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3033. (setq kw (match-string 1 x)
  3034. key (and (match-end 2) (match-string 2 x))
  3035. log1 (and (match-end 3) (match-string 3 x))
  3036. log2 (and (match-end 4) (match-string 4 x)))
  3037. (and (or log1 log2)
  3038. (list kw
  3039. (and log1 (if (equal log1 "!") 'time 'note))
  3040. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3041. (defun org-remove-keyword-keys (list)
  3042. "Remove a pair of parenthesis at the end of each string in LIST."
  3043. (mapcar (lambda (x)
  3044. (if (string-match "(.*)$" x)
  3045. (substring x 0 (match-beginning 0))
  3046. x))
  3047. list))
  3048. ;; FIXME: this could be done much better, using second characters etc.
  3049. (defun org-assign-fast-keys (alist)
  3050. "Assign fast keys to a keyword-key alist.
  3051. Respect keys that are already there."
  3052. (let (new e k c c1 c2 (char ?a))
  3053. (while (setq e (pop alist))
  3054. (cond
  3055. ((equal e '(:startgroup)) (push e new))
  3056. ((equal e '(:endgroup)) (push e new))
  3057. (t
  3058. (setq k (car e) c2 nil)
  3059. (if (cdr e)
  3060. (setq c (cdr e))
  3061. ;; automatically assign a character.
  3062. (setq c1 (string-to-char
  3063. (downcase (substring
  3064. k (if (= (string-to-char k) ?@) 1 0)))))
  3065. (if (or (rassoc c1 new) (rassoc c1 alist))
  3066. (while (or (rassoc char new) (rassoc char alist))
  3067. (setq char (1+ char)))
  3068. (setq c2 c1))
  3069. (setq c (or c2 char)))
  3070. (push (cons k c) new))))
  3071. (nreverse new)))
  3072. ;;; Some variables used in various places
  3073. (defvar org-window-configuration nil
  3074. "Used in various places to store a window configuration.")
  3075. (defvar org-finish-function nil
  3076. "Function to be called when `C-c C-c' is used.
  3077. This is for getting out of special buffers like remember.")
  3078. ;; FIXME: Occasionally check by commenting these, to make sure
  3079. ;; no other functions uses these, forgetting to let-bind them.
  3080. (defvar entry)
  3081. (defvar state)
  3082. (defvar last-state)
  3083. (defvar date)
  3084. (defvar description)
  3085. ;; Defined somewhere in this file, but used before definition.
  3086. (defvar org-html-entities)
  3087. (defvar org-struct-menu)
  3088. (defvar org-org-menu)
  3089. (defvar org-tbl-menu)
  3090. (defvar org-agenda-keymap)
  3091. ;;;; Define the Org-mode
  3092. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3093. (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."))
  3094. ;; We use a before-change function to check if a table might need
  3095. ;; an update.
  3096. (defvar org-table-may-need-update t
  3097. "Indicates that a table might need an update.
  3098. This variable is set by `org-before-change-function'.
  3099. `org-table-align' sets it back to nil.")
  3100. (defun org-before-change-function (beg end)
  3101. "Every change indicates that a table might need an update."
  3102. (setq org-table-may-need-update t))
  3103. (defvar org-mode-map)
  3104. (defvar org-mode-hook nil
  3105. "Mode hook for Org-mode, run after the mode was turned on.")
  3106. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3107. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3108. (defvar org-table-buffer-is-an nil)
  3109. (defconst org-outline-regexp "\\*+ ")
  3110. ;;;###autoload
  3111. (define-derived-mode org-mode outline-mode "Org"
  3112. "Outline-based notes management and organizer, alias
  3113. \"Carsten's outline-mode for keeping track of everything.\"
  3114. Org-mode develops organizational tasks around a NOTES file which
  3115. contains information about projects as plain text. Org-mode is
  3116. implemented on top of outline-mode, which is ideal to keep the content
  3117. of large files well structured. It supports ToDo items, deadlines and
  3118. time stamps, which magically appear in the diary listing of the Emacs
  3119. calendar. Tables are easily created with a built-in table editor.
  3120. Plain text URL-like links connect to websites, emails (VM), Usenet
  3121. messages (Gnus), BBDB entries, and any files related to the project.
  3122. For printing and sharing of notes, an Org-mode file (or a part of it)
  3123. can be exported as a structured ASCII or HTML file.
  3124. The following commands are available:
  3125. \\{org-mode-map}"
  3126. ;; Get rid of Outline menus, they are not needed
  3127. ;; Need to do this here because define-derived-mode sets up
  3128. ;; the keymap so late. Still, it is a waste to call this each time
  3129. ;; we switch another buffer into org-mode.
  3130. (if (featurep 'xemacs)
  3131. (when (boundp 'outline-mode-menu-heading)
  3132. ;; Assume this is Greg's port, it used easymenu
  3133. (easy-menu-remove outline-mode-menu-heading)
  3134. (easy-menu-remove outline-mode-menu-show)
  3135. (easy-menu-remove outline-mode-menu-hide))
  3136. (define-key org-mode-map [menu-bar headings] 'undefined)
  3137. (define-key org-mode-map [menu-bar hide] 'undefined)
  3138. (define-key org-mode-map [menu-bar show] 'undefined))
  3139. (org-load-modules-maybe)
  3140. (easy-menu-add org-org-menu)
  3141. (easy-menu-add org-tbl-menu)
  3142. (org-install-agenda-files-menu)
  3143. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3144. (org-add-to-invisibility-spec '(org-cwidth))
  3145. (when (featurep 'xemacs)
  3146. (org-set-local 'line-move-ignore-invisible t))
  3147. (org-set-local 'outline-regexp org-outline-regexp)
  3148. (org-set-local 'outline-level 'org-outline-level)
  3149. (when (and org-ellipsis
  3150. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3151. (fboundp 'make-glyph-code))
  3152. (unless org-display-table
  3153. (setq org-display-table (make-display-table)))
  3154. (set-display-table-slot
  3155. org-display-table 4
  3156. (vconcat (mapcar
  3157. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3158. org-ellipsis)))
  3159. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3160. (setq buffer-display-table org-display-table))
  3161. (org-set-regexps-and-options)
  3162. (when (and org-tag-faces (not org-tags-special-faces-re))
  3163. ;; tag faces set outside customize.... force initialization.
  3164. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3165. ;; Calc embedded
  3166. (org-set-local 'calc-embedded-open-mode "# ")
  3167. (modify-syntax-entry ?# "<")
  3168. (modify-syntax-entry ?@ "w")
  3169. (if org-startup-truncated (setq truncate-lines t))
  3170. (org-set-local 'font-lock-unfontify-region-function
  3171. 'org-unfontify-region)
  3172. ;; Activate before-change-function
  3173. (org-set-local 'org-table-may-need-update t)
  3174. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3175. 'local)
  3176. ;; Check for running clock before killing a buffer
  3177. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3178. ;; Paragraphs and auto-filling
  3179. (org-set-autofill-regexps)
  3180. (setq indent-line-function 'org-indent-line-function)
  3181. (org-update-radio-target-regexp)
  3182. ;; Comment characters
  3183. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3184. (org-set-local 'comment-padding " ")
  3185. ;; Align options lines
  3186. (org-set-local
  3187. 'align-mode-rules-list
  3188. '((org-in-buffer-settings
  3189. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3190. (modes . '(org-mode)))))
  3191. ;; Imenu
  3192. (org-set-local 'imenu-create-index-function
  3193. 'org-imenu-get-tree)
  3194. ;; Make isearch reveal context
  3195. (if (or (featurep 'xemacs)
  3196. (not (boundp 'outline-isearch-open-invisible-function)))
  3197. ;; Emacs 21 and XEmacs make use of the hook
  3198. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3199. ;; Emacs 22 deals with this through a special variable
  3200. (org-set-local 'outline-isearch-open-invisible-function
  3201. (lambda (&rest ignore) (org-show-context 'isearch))))
  3202. ;; If empty file that did not turn on org-mode automatically, make it to.
  3203. (if (and org-insert-mode-line-in-empty-file
  3204. (interactive-p)
  3205. (= (point-min) (point-max)))
  3206. (insert "# -*- mode: org -*-\n\n"))
  3207. (unless org-inhibit-startup
  3208. (when org-startup-align-all-tables
  3209. (let ((bmp (buffer-modified-p)))
  3210. (org-table-map-tables 'org-table-align)
  3211. (set-buffer-modified-p bmp)))
  3212. (org-set-startup-visibility)))
  3213. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3214. (defun org-current-time ()
  3215. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3216. (if (> (car org-time-stamp-rounding-minutes) 1)
  3217. (let ((r (car org-time-stamp-rounding-minutes))
  3218. (time (decode-time)))
  3219. (apply 'encode-time
  3220. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3221. (nthcdr 2 time))))
  3222. (current-time)))
  3223. ;;;; Font-Lock stuff, including the activators
  3224. (defvar org-mouse-map (make-sparse-keymap))
  3225. (org-defkey org-mouse-map
  3226. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3227. (org-defkey org-mouse-map
  3228. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3229. (when org-mouse-1-follows-link
  3230. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3231. (when org-tab-follows-link
  3232. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3233. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3234. (when org-return-follows-link
  3235. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3236. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3237. (require 'font-lock)
  3238. (defconst org-non-link-chars "]\t\n\r<>")
  3239. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3240. "shell" "elisp"))
  3241. (defvar org-link-types-re nil
  3242. "Matches a link that has a url-like prefix like \"http:\"")
  3243. (defvar org-link-re-with-space nil
  3244. "Matches a link with spaces, optional angular brackets around it.")
  3245. (defvar org-link-re-with-space2 nil
  3246. "Matches a link with spaces, optional angular brackets around it.")
  3247. (defvar org-link-re-with-space3 nil
  3248. "Matches a link with spaces, only for internal part in bracket links.")
  3249. (defvar org-angle-link-re nil
  3250. "Matches link with angular brackets, spaces are allowed.")
  3251. (defvar org-plain-link-re nil
  3252. "Matches plain link, without spaces.")
  3253. (defvar org-bracket-link-regexp nil
  3254. "Matches a link in double brackets.")
  3255. (defvar org-bracket-link-analytic-regexp nil
  3256. "Regular expression used to analyze links.
  3257. Here is what the match groups contain after a match:
  3258. 1: http:
  3259. 2: http
  3260. 3: path
  3261. 4: [desc]
  3262. 5: desc")
  3263. (defvar org-bracket-link-analytic-regexp++ nil
  3264. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3265. (defvar org-any-link-re nil
  3266. "Regular expression matching any link.")
  3267. (defun org-make-link-regexps ()
  3268. "Update the link regular expressions.
  3269. This should be called after the variable `org-link-types' has changed."
  3270. (setq org-link-types-re
  3271. (concat
  3272. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3273. org-link-re-with-space
  3274. (concat
  3275. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3276. "\\([^" org-non-link-chars " ]"
  3277. "[^" org-non-link-chars "]*"
  3278. "[^" org-non-link-chars " ]\\)>?")
  3279. org-link-re-with-space2
  3280. (concat
  3281. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3282. "\\([^" org-non-link-chars " ]"
  3283. "[^\t\n\r]*"
  3284. "[^" org-non-link-chars " ]\\)>?")
  3285. org-link-re-with-space3
  3286. (concat
  3287. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3288. "\\([^" org-non-link-chars " ]"
  3289. "[^\t\n\r]*\\)")
  3290. org-angle-link-re
  3291. (concat
  3292. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3293. "\\([^" org-non-link-chars " ]"
  3294. "[^" org-non-link-chars "]*"
  3295. "\\)>")
  3296. org-plain-link-re
  3297. (concat
  3298. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3299. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3300. org-bracket-link-regexp
  3301. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3302. org-bracket-link-analytic-regexp
  3303. (concat
  3304. "\\[\\["
  3305. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3306. "\\([^]]+\\)"
  3307. "\\]"
  3308. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3309. "\\]")
  3310. org-bracket-link-analytic-regexp++
  3311. (concat
  3312. "\\[\\["
  3313. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3314. "\\([^]]+\\)"
  3315. "\\]"
  3316. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3317. "\\]")
  3318. org-any-link-re
  3319. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3320. org-angle-link-re "\\)\\|\\("
  3321. org-plain-link-re "\\)")))
  3322. (org-make-link-regexps)
  3323. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3324. "Regular expression for fast time stamp matching.")
  3325. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3326. "Regular expression for fast time stamp matching.")
  3327. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3328. "Regular expression matching time strings for analysis.
  3329. This one does not require the space after the date, so it can be used
  3330. on a string that terminates immediately after the date.")
  3331. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3332. "Regular expression matching time strings for analysis.")
  3333. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3334. "Regular expression matching time stamps, with groups.")
  3335. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3336. "Regular expression matching time stamps (also [..]), with groups.")
  3337. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3338. "Regular expression matching a time stamp range.")
  3339. (defconst org-tr-regexp-both
  3340. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3341. "Regular expression matching a time stamp range.")
  3342. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3343. org-ts-regexp "\\)?")
  3344. "Regular expression matching a time stamp or time stamp range.")
  3345. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3346. org-ts-regexp-both "\\)?")
  3347. "Regular expression matching a time stamp or time stamp range.
  3348. The time stamps may be either active or inactive.")
  3349. (defvar org-emph-face nil)
  3350. (defun org-do-emphasis-faces (limit)
  3351. "Run through the buffer and add overlays to links."
  3352. (let (rtn)
  3353. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3354. (if (not (= (char-after (match-beginning 3))
  3355. (char-after (match-beginning 4))))
  3356. (progn
  3357. (setq rtn t)
  3358. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3359. 'face
  3360. (nth 1 (assoc (match-string 3)
  3361. org-emphasis-alist)))
  3362. (add-text-properties (match-beginning 2) (match-end 2)
  3363. '(font-lock-multiline t))
  3364. (when org-hide-emphasis-markers
  3365. (add-text-properties (match-end 4) (match-beginning 5)
  3366. '(invisible org-link))
  3367. (add-text-properties (match-beginning 3) (match-end 3)
  3368. '(invisible org-link)))))
  3369. (backward-char 1))
  3370. rtn))
  3371. (defun org-emphasize (&optional char)
  3372. "Insert or change an emphasis, i.e. a font like bold or italic.
  3373. If there is an active region, change that region to a new emphasis.
  3374. If there is no region, just insert the marker characters and position
  3375. the cursor between them.
  3376. CHAR should be either the marker character, or the first character of the
  3377. HTML tag associated with that emphasis. If CHAR is a space, the means
  3378. to remove the emphasis of the selected region.
  3379. If char is not given (for example in an interactive call) it
  3380. will be prompted for."
  3381. (interactive)
  3382. (let ((eal org-emphasis-alist) e det
  3383. (erc org-emphasis-regexp-components)
  3384. (prompt "")
  3385. (string "") beg end move tag c s)
  3386. (if (org-region-active-p)
  3387. (setq beg (region-beginning) end (region-end)
  3388. string (buffer-substring beg end))
  3389. (setq move t))
  3390. (while (setq e (pop eal))
  3391. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3392. c (aref tag 0))
  3393. (push (cons c (string-to-char (car e))) det)
  3394. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3395. (substring tag 1)))))
  3396. (setq det (nreverse det))
  3397. (unless char
  3398. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3399. (setq char (read-char-exclusive)))
  3400. (setq char (or (cdr (assoc char det)) char))
  3401. (if (equal char ?\ )
  3402. (setq s "" move nil)
  3403. (unless (assoc (char-to-string char) org-emphasis-alist)
  3404. (error "No such emphasis marker: \"%c\"" char))
  3405. (setq s (char-to-string char)))
  3406. (while (and (> (length string) 1)
  3407. (equal (substring string 0 1) (substring string -1))
  3408. (assoc (substring string 0 1) org-emphasis-alist))
  3409. (setq string (substring string 1 -1)))
  3410. (setq string (concat s string s))
  3411. (if beg (delete-region beg end))
  3412. (unless (or (bolp)
  3413. (string-match (concat "[" (nth 0 erc) "\n]")
  3414. (char-to-string (char-before (point)))))
  3415. (insert " "))
  3416. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3417. (char-to-string (char-after (point))))
  3418. (insert " ") (backward-char 1))
  3419. (insert string)
  3420. (and move (backward-char 1))))
  3421. (defconst org-nonsticky-props
  3422. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3423. (defun org-activate-plain-links (limit)
  3424. "Run through the buffer and add overlays to links."
  3425. (catch 'exit
  3426. (let (f)
  3427. (while (re-search-forward org-plain-link-re limit t)
  3428. (setq f (get-text-property (match-beginning 0) 'face))
  3429. (if (or (eq f 'org-tag)
  3430. (and (listp f) (memq 'org-tag f)))
  3431. nil
  3432. (add-text-properties (match-beginning 0) (match-end 0)
  3433. (list 'mouse-face 'highlight
  3434. 'rear-nonsticky org-nonsticky-props
  3435. 'keymap org-mouse-map
  3436. ))
  3437. (throw 'exit t))))))
  3438. (defun org-activate-code (limit)
  3439. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3440. (progn
  3441. (remove-text-properties (match-beginning 0) (match-end 0)
  3442. '(display t invisible t intangible t))
  3443. t)))
  3444. (defun org-activate-angle-links (limit)
  3445. "Run through the buffer and add overlays to links."
  3446. (if (re-search-forward org-angle-link-re limit t)
  3447. (progn
  3448. (add-text-properties (match-beginning 0) (match-end 0)
  3449. (list 'mouse-face 'highlight
  3450. 'rear-nonsticky org-nonsticky-props
  3451. 'keymap org-mouse-map
  3452. ))
  3453. t)))
  3454. (defun org-activate-footnote-links (limit)
  3455. "Run through the buffer and add overlays to links."
  3456. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3457. limit t)
  3458. (progn
  3459. (add-text-properties (match-beginning 2) (match-end 2)
  3460. (list 'mouse-face 'highlight
  3461. 'rear-nonsticky org-nonsticky-props
  3462. 'keymap org-mouse-map
  3463. 'help-echo
  3464. (if (= (point-at-bol) (match-beginning 2))
  3465. "Footnote definition"
  3466. "Footnote reference")
  3467. ))
  3468. t)))
  3469. (defun org-activate-bracket-links (limit)
  3470. "Run through the buffer and add overlays to bracketed links."
  3471. (if (re-search-forward org-bracket-link-regexp limit t)
  3472. (let* ((help (concat "LINK: "
  3473. (org-match-string-no-properties 1)))
  3474. ;; FIXME: above we should remove the escapes.
  3475. ;; but that requires another match, protecting match data,
  3476. ;; a lot of overhead for font-lock.
  3477. (ip (org-maybe-intangible
  3478. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3479. 'keymap org-mouse-map 'mouse-face 'highlight
  3480. 'font-lock-multiline t 'help-echo help)))
  3481. (vp (list 'rear-nonsticky org-nonsticky-props
  3482. 'keymap org-mouse-map 'mouse-face 'highlight
  3483. ' font-lock-multiline t 'help-echo help)))
  3484. ;; We need to remove the invisible property here. Table narrowing
  3485. ;; may have made some of this invisible.
  3486. (remove-text-properties (match-beginning 0) (match-end 0)
  3487. '(invisible nil))
  3488. (if (match-end 3)
  3489. (progn
  3490. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3491. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3492. (add-text-properties (match-end 3) (match-end 0) ip))
  3493. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3494. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3495. (add-text-properties (match-end 1) (match-end 0) ip))
  3496. t)))
  3497. (defun org-activate-dates (limit)
  3498. "Run through the buffer and add overlays to dates."
  3499. (if (re-search-forward org-tsr-regexp-both limit t)
  3500. (progn
  3501. (add-text-properties (match-beginning 0) (match-end 0)
  3502. (list 'mouse-face 'highlight
  3503. 'rear-nonsticky org-nonsticky-props
  3504. 'keymap org-mouse-map))
  3505. (when org-display-custom-times
  3506. (if (match-end 3)
  3507. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3508. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3509. t)))
  3510. (defvar org-target-link-regexp nil
  3511. "Regular expression matching radio targets in plain text.")
  3512. (make-variable-buffer-local 'org-target-link-regexp)
  3513. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3514. "Regular expression matching a link target.")
  3515. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3516. "Regular expression matching a radio target.")
  3517. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3518. "Regular expression matching any target.")
  3519. (defun org-activate-target-links (limit)
  3520. "Run through the buffer and add overlays to target matches."
  3521. (when org-target-link-regexp
  3522. (let ((case-fold-search t))
  3523. (if (re-search-forward org-target-link-regexp limit t)
  3524. (progn
  3525. (add-text-properties (match-beginning 0) (match-end 0)
  3526. (list 'mouse-face 'highlight
  3527. 'rear-nonsticky org-nonsticky-props
  3528. 'keymap org-mouse-map
  3529. 'help-echo "Radio target link"
  3530. 'org-linked-text t))
  3531. t)))))
  3532. (defun org-update-radio-target-regexp ()
  3533. "Find all radio targets in this file and update the regular expression."
  3534. (interactive)
  3535. (when (memq 'radio org-activate-links)
  3536. (setq org-target-link-regexp
  3537. (org-make-target-link-regexp (org-all-targets 'radio)))
  3538. (org-restart-font-lock)))
  3539. (defun org-hide-wide-columns (limit)
  3540. (let (s e)
  3541. (setq s (text-property-any (point) (or limit (point-max))
  3542. 'org-cwidth t))
  3543. (when s
  3544. (setq e (next-single-property-change s 'org-cwidth))
  3545. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3546. (goto-char e)
  3547. t)))
  3548. (defvar org-latex-and-specials-regexp nil
  3549. "Regular expression for highlighting export special stuff.")
  3550. (defvar org-match-substring-regexp)
  3551. (defvar org-match-substring-with-braces-regexp)
  3552. (defvar org-export-html-special-string-regexps)
  3553. (defun org-compute-latex-and-specials-regexp ()
  3554. "Compute regular expression for stuff treated specially by exporters."
  3555. (if (not org-highlight-latex-fragments-and-specials)
  3556. (org-set-local 'org-latex-and-specials-regexp nil)
  3557. (require 'org-exp)
  3558. (let*
  3559. ((matchers (plist-get org-format-latex-options :matchers))
  3560. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3561. org-latex-regexps)))
  3562. (options (org-combine-plists (org-default-export-plist)
  3563. (org-infile-export-plist)))
  3564. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3565. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3566. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3567. (org-export-html-expand (plist-get options :expand-quoted-html))
  3568. (org-export-with-special-strings (plist-get options :special-strings))
  3569. (re-sub
  3570. (cond
  3571. ((equal org-export-with-sub-superscripts '{})
  3572. (list org-match-substring-with-braces-regexp))
  3573. (org-export-with-sub-superscripts
  3574. (list org-match-substring-regexp))
  3575. (t nil)))
  3576. (re-latex
  3577. (if org-export-with-LaTeX-fragments
  3578. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3579. (re-macros
  3580. (if org-export-with-TeX-macros
  3581. (list (concat "\\\\"
  3582. (regexp-opt
  3583. (append (mapcar 'car org-html-entities)
  3584. (if (boundp 'org-latex-entities)
  3585. org-latex-entities nil))
  3586. 'words))) ; FIXME
  3587. ))
  3588. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3589. (re-special (if org-export-with-special-strings
  3590. (mapcar (lambda (x) (car x))
  3591. org-export-html-special-string-regexps)))
  3592. (re-rest
  3593. (delq nil
  3594. (list
  3595. (if org-export-html-expand "@<[^>\n]+>")
  3596. ))))
  3597. (org-set-local
  3598. 'org-latex-and-specials-regexp
  3599. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3600. re-rest) "\\|")))))
  3601. (defun org-do-latex-and-special-faces (limit)
  3602. "Run through the buffer and add overlays to links."
  3603. (when org-latex-and-specials-regexp
  3604. (let (rtn d)
  3605. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3606. limit t))
  3607. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3608. 'face))
  3609. '(org-code org-verbatim underline)))
  3610. (progn
  3611. (setq rtn t
  3612. d (cond ((member (char-after (1+ (match-beginning 0)))
  3613. '(?_ ?^)) 1)
  3614. (t 0)))
  3615. (font-lock-prepend-text-property
  3616. (+ d (match-beginning 0)) (match-end 0)
  3617. 'face 'org-latex-and-export-specials)
  3618. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3619. '(font-lock-multiline t)))))
  3620. rtn)))
  3621. (defun org-restart-font-lock ()
  3622. "Restart font-lock-mode, to force refontification."
  3623. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3624. (font-lock-mode -1)
  3625. (font-lock-mode 1)))
  3626. (defun org-all-targets (&optional radio)
  3627. "Return a list of all targets in this file.
  3628. With optional argument RADIO, only find radio targets."
  3629. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3630. rtn)
  3631. (save-excursion
  3632. (goto-char (point-min))
  3633. (while (re-search-forward re nil t)
  3634. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3635. rtn)))
  3636. (defun org-make-target-link-regexp (targets)
  3637. "Make regular expression matching all strings in TARGETS.
  3638. The regular expression finds the targets also if there is a line break
  3639. between words."
  3640. (and targets
  3641. (concat
  3642. "\\<\\("
  3643. (mapconcat
  3644. (lambda (x)
  3645. (while (string-match " +" x)
  3646. (setq x (replace-match "\\s-+" t t x)))
  3647. x)
  3648. targets
  3649. "\\|")
  3650. "\\)\\>")))
  3651. (defun org-activate-tags (limit)
  3652. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3653. (progn
  3654. (add-text-properties (match-beginning 1) (match-end 1)
  3655. (list 'mouse-face 'highlight
  3656. 'rear-nonsticky org-nonsticky-props
  3657. 'keymap org-mouse-map))
  3658. t)))
  3659. (defun org-outline-level ()
  3660. (save-excursion
  3661. (looking-at outline-regexp)
  3662. (if (match-beginning 1)
  3663. (+ (org-get-string-indentation (match-string 1)) 1000)
  3664. (1- (- (match-end 0) (match-beginning 0))))))
  3665. (defvar org-font-lock-keywords nil)
  3666. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3667. "Regular expression matching a property line.")
  3668. (defvar org-font-lock-hook nil
  3669. "Functions to be called for special font lock stuff.")
  3670. (defun org-font-lock-hook (limit)
  3671. (run-hook-with-args 'org-font-lock-hook limit))
  3672. (defun org-set-font-lock-defaults ()
  3673. (let* ((em org-fontify-emphasized-text)
  3674. (lk org-activate-links)
  3675. (org-font-lock-extra-keywords
  3676. (list
  3677. ;; Call the hook
  3678. '(org-font-lock-hook)
  3679. ;; Headlines
  3680. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3681. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3682. ;; Table lines
  3683. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3684. (1 'org-table t))
  3685. ;; Table internals
  3686. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3687. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3688. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3689. ;; Drawers
  3690. (list org-drawer-regexp '(0 'org-special-keyword t))
  3691. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3692. ;; Properties
  3693. (list org-property-re
  3694. '(1 'org-special-keyword t)
  3695. '(3 'org-property-value t))
  3696. (if org-format-transports-properties-p
  3697. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3698. ;; Links
  3699. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3700. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3701. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3702. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3703. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3704. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3705. (if (memq 'footnote lk) '(org-activate-footnote-links
  3706. (2 'org-footnote t)))
  3707. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3708. '(org-hide-wide-columns (0 nil append))
  3709. ;; TODO lines
  3710. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3711. '(1 (org-get-todo-face 1) t))
  3712. ;; DONE
  3713. (if org-fontify-done-headline
  3714. (list (concat "^[*]+ +\\<\\("
  3715. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3716. "\\)\\(.*\\)")
  3717. '(2 'org-headline-done t))
  3718. nil)
  3719. ;; Priorities
  3720. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3721. ;; Tags
  3722. '(org-font-lock-add-tag-faces)
  3723. ;; Special keywords
  3724. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3725. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3726. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3727. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3728. ;; Emphasis
  3729. (if em
  3730. (if (featurep 'xemacs)
  3731. '(org-do-emphasis-faces (0 nil append))
  3732. '(org-do-emphasis-faces)))
  3733. ;; Checkboxes
  3734. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3735. 2 'bold prepend)
  3736. (if org-provide-checkbox-statistics
  3737. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3738. (0 (org-get-checkbox-statistics-face) t)))
  3739. ;; Description list items
  3740. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3741. 2 'bold prepend)
  3742. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3743. '(1 'org-archived prepend))
  3744. ;; Specials
  3745. '(org-do-latex-and-special-faces)
  3746. ;; Code
  3747. '(org-activate-code (1 'org-code t))
  3748. ;; COMMENT
  3749. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3750. "\\|" org-quote-string "\\)\\>")
  3751. '(1 'org-special-keyword t))
  3752. '("^#.*" (0 'font-lock-comment-face t))
  3753. )))
  3754. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3755. ;; Now set the full font-lock-keywords
  3756. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3757. (org-set-local 'font-lock-defaults
  3758. '(org-font-lock-keywords t nil nil backward-paragraph))
  3759. (kill-local-variable 'font-lock-keywords) nil))
  3760. (defvar org-m nil)
  3761. (defvar org-l nil)
  3762. (defvar org-f nil)
  3763. (defun org-get-level-face (n)
  3764. "Get the right face for match N in font-lock matching of headlines."
  3765. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3766. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3767. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3768. (cond
  3769. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3770. ((eq n 2) org-f)
  3771. (t (if org-level-color-stars-only nil org-f))))
  3772. (defun org-get-todo-face (kwd)
  3773. "Get the right face for a TODO keyword KWD.
  3774. If KWD is a number, get the corresponding match group."
  3775. (if (numberp kwd) (setq kwd (match-string kwd)))
  3776. (or (cdr (assoc kwd org-todo-keyword-faces))
  3777. (and (member kwd org-done-keywords) 'org-done)
  3778. 'org-todo))
  3779. (defun org-font-lock-add-tag-faces (limit)
  3780. "Add the special tag faces."
  3781. (when (and org-tag-faces org-tags-special-faces-re)
  3782. (while (re-search-forward org-tags-special-faces-re limit t)
  3783. (add-text-properties (match-beginning 1) (match-end 1)
  3784. (list 'face (org-get-tag-face 1)
  3785. 'font-lock-fontified t))
  3786. (backward-char 1))))
  3787. (defun org-get-tag-face (kwd)
  3788. "Get the right face for a TODO keyword KWD.
  3789. If KWD is a number, get the corresponding match group."
  3790. (if (numberp kwd) (setq kwd (match-string kwd)))
  3791. (or (cdr (assoc kwd org-tag-faces))
  3792. 'org-tag))
  3793. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3794. "Remove fontification and activation overlays from links."
  3795. (font-lock-default-unfontify-region beg end)
  3796. (let* ((buffer-undo-list t)
  3797. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3798. (inhibit-modification-hooks t)
  3799. deactivate-mark buffer-file-name buffer-file-truename)
  3800. (remove-text-properties beg end
  3801. '(mouse-face t keymap t org-linked-text t
  3802. invisible t intangible t))))
  3803. ;;;; Visibility cycling, including org-goto and indirect buffer
  3804. ;;; Cycling
  3805. (defvar org-cycle-global-status nil)
  3806. (make-variable-buffer-local 'org-cycle-global-status)
  3807. (defvar org-cycle-subtree-status nil)
  3808. (make-variable-buffer-local 'org-cycle-subtree-status)
  3809. ;;;###autoload
  3810. (defun org-cycle (&optional arg)
  3811. "Visibility cycling for Org-mode.
  3812. - When this function is called with a prefix argument, rotate the entire
  3813. buffer through 3 states (global cycling)
  3814. 1. OVERVIEW: Show only top-level headlines.
  3815. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3816. 3. SHOW ALL: Show everything.
  3817. When called with two C-u C-u prefixes, switch to the startup visibility,
  3818. determined by the variable `org-startup-folded', and by any VISIBILITY
  3819. properties in the buffer.
  3820. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3821. including drawers.
  3822. - When point is at the beginning of a headline, rotate the subtree started
  3823. by this line through 3 different states (local cycling)
  3824. 1. FOLDED: Only the main headline is shown.
  3825. 2. CHILDREN: The main headline and the direct children are shown.
  3826. From this state, you can move to one of the children
  3827. and zoom in further.
  3828. 3. SUBTREE: Show the entire subtree, including body text.
  3829. - When there is a numeric prefix, go up to a heading with level ARG, do
  3830. a `show-subtree' and return to the previous cursor position. If ARG
  3831. is negative, go up that many levels.
  3832. - When point is not at the beginning of a headline, execute the global
  3833. binding for TAB, which is re-indenting the line. See the option
  3834. `org-cycle-emulate-tab' for details.
  3835. - Special case: if point is at the beginning of the buffer and there is
  3836. no headline in line 1, this function will act as if called with prefix arg.
  3837. But only if also the variable `org-cycle-global-at-bob' is t."
  3838. (interactive "P")
  3839. (org-load-modules-maybe)
  3840. (let* ((outline-regexp
  3841. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3842. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3843. outline-regexp))
  3844. (bob-special (and org-cycle-global-at-bob (bobp)
  3845. (not (looking-at outline-regexp))))
  3846. (org-cycle-hook
  3847. (if bob-special
  3848. (delq 'org-optimize-window-after-visibility-change
  3849. (copy-sequence org-cycle-hook))
  3850. org-cycle-hook))
  3851. (pos (point)))
  3852. (if (or bob-special (equal arg '(4)))
  3853. ;; special case: use global cycling
  3854. (setq arg t))
  3855. (cond
  3856. ((equal arg '(16))
  3857. (org-set-startup-visibility)
  3858. (message "Startup visibility, plus VISIBILITY properties"))
  3859. ((equal arg '(64))
  3860. (show-all)
  3861. (message "Entire buffer visible, including drawers"))
  3862. ((org-at-table-p 'any)
  3863. ;; Enter the table or move to the next field in the table
  3864. (or (org-table-recognize-table.el)
  3865. (progn
  3866. (if arg (org-table-edit-field t)
  3867. (org-table-justify-field-maybe)
  3868. (call-interactively 'org-table-next-field)))))
  3869. ((eq arg t) ;; Global cycling
  3870. (cond
  3871. ((and (eq last-command this-command)
  3872. (eq org-cycle-global-status 'overview))
  3873. ;; We just created the overview - now do table of contents
  3874. ;; This can be slow in very large buffers, so indicate action
  3875. (message "CONTENTS...")
  3876. (org-content)
  3877. (message "CONTENTS...done")
  3878. (setq org-cycle-global-status 'contents)
  3879. (run-hook-with-args 'org-cycle-hook 'contents))
  3880. ((and (eq last-command this-command)
  3881. (eq org-cycle-global-status 'contents))
  3882. ;; We just showed the table of contents - now show everything
  3883. (show-all)
  3884. (message "SHOW ALL")
  3885. (setq org-cycle-global-status 'all)
  3886. (run-hook-with-args 'org-cycle-hook 'all))
  3887. (t
  3888. ;; Default action: go to overview
  3889. (org-overview)
  3890. (message "OVERVIEW")
  3891. (setq org-cycle-global-status 'overview)
  3892. (run-hook-with-args 'org-cycle-hook 'overview))))
  3893. ((and org-drawers org-drawer-regexp
  3894. (save-excursion
  3895. (beginning-of-line 1)
  3896. (looking-at org-drawer-regexp)))
  3897. ;; Toggle block visibility
  3898. (org-flag-drawer
  3899. (not (get-char-property (match-end 0) 'invisible))))
  3900. ((integerp arg)
  3901. ;; Show-subtree, ARG levels up from here.
  3902. (save-excursion
  3903. (org-back-to-heading)
  3904. (outline-up-heading (if (< arg 0) (- arg)
  3905. (- (funcall outline-level) arg)))
  3906. (org-show-subtree)))
  3907. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3908. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3909. ;; At a heading: rotate between three different views
  3910. (org-back-to-heading)
  3911. (let ((goal-column 0) eoh eol eos)
  3912. ;; First, some boundaries
  3913. (save-excursion
  3914. (org-back-to-heading)
  3915. (save-excursion
  3916. (beginning-of-line 2)
  3917. (while (and (not (eobp)) ;; this is like `next-line'
  3918. (get-char-property (1- (point)) 'invisible))
  3919. (beginning-of-line 2)) (setq eol (point)))
  3920. (outline-end-of-heading) (setq eoh (point))
  3921. (org-end-of-subtree t)
  3922. (unless (eobp)
  3923. (skip-chars-forward " \t\n")
  3924. (beginning-of-line 1) ; in case this is an item
  3925. )
  3926. (setq eos (1- (point))))
  3927. ;; Find out what to do next and set `this-command'
  3928. (cond
  3929. ((= eos eoh)
  3930. ;; Nothing is hidden behind this heading
  3931. (message "EMPTY ENTRY")
  3932. (setq org-cycle-subtree-status nil)
  3933. (save-excursion
  3934. (goto-char eos)
  3935. (outline-next-heading)
  3936. (if (org-invisible-p) (org-flag-heading nil))))
  3937. ((or (>= eol eos)
  3938. (not (string-match "\\S-" (buffer-substring eol eos))))
  3939. ;; Entire subtree is hidden in one line: open it
  3940. (org-show-entry)
  3941. (show-children)
  3942. (message "CHILDREN")
  3943. (save-excursion
  3944. (goto-char eos)
  3945. (outline-next-heading)
  3946. (if (org-invisible-p) (org-flag-heading nil)))
  3947. (setq org-cycle-subtree-status 'children)
  3948. (run-hook-with-args 'org-cycle-hook 'children))
  3949. ((and (eq last-command this-command)
  3950. (eq org-cycle-subtree-status 'children))
  3951. ;; We just showed the children, now show everything.
  3952. (org-show-subtree)
  3953. (message "SUBTREE")
  3954. (setq org-cycle-subtree-status 'subtree)
  3955. (run-hook-with-args 'org-cycle-hook 'subtree))
  3956. (t
  3957. ;; Default action: hide the subtree.
  3958. (hide-subtree)
  3959. (message "FOLDED")
  3960. (setq org-cycle-subtree-status 'folded)
  3961. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3962. ;; TAB emulation and template completion
  3963. (buffer-read-only (org-back-to-heading))
  3964. ((org-try-structure-completion))
  3965. ((org-try-cdlatex-tab))
  3966. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3967. (or (not (bolp))
  3968. (not (looking-at outline-regexp))))
  3969. (call-interactively (global-key-binding "\t")))
  3970. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3971. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3972. (or (and (eq org-cycle-emulate-tab 'white)
  3973. (= (match-end 0) (point-at-eol)))
  3974. (and (eq org-cycle-emulate-tab 'whitestart)
  3975. (>= (match-end 0) pos))))
  3976. t
  3977. (eq org-cycle-emulate-tab t))
  3978. (call-interactively (global-key-binding "\t")))
  3979. (t (save-excursion
  3980. (org-back-to-heading)
  3981. (org-cycle))))))
  3982. ;;;###autoload
  3983. (defun org-global-cycle (&optional arg)
  3984. "Cycle the global visibility. For details see `org-cycle'.
  3985. With C-u prefix arg, switch to startup visibility.
  3986. With a numeric prefix, show all headlines up to that level."
  3987. (interactive "P")
  3988. (let ((org-cycle-include-plain-lists
  3989. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3990. (cond
  3991. ((integerp arg)
  3992. (show-all)
  3993. (hide-sublevels arg)
  3994. (setq org-cycle-global-status 'contents))
  3995. ((equal arg '(4))
  3996. (org-set-startup-visibility)
  3997. (message "Startup visibility, plus VISIBILITY properties."))
  3998. (t
  3999. (org-cycle '(4))))))
  4000. (defun org-set-startup-visibility ()
  4001. "Set the visibility required by startup options and properties."
  4002. (cond
  4003. ((eq org-startup-folded t)
  4004. (org-cycle '(4)))
  4005. ((eq org-startup-folded 'content)
  4006. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4007. (org-cycle '(4)) (org-cycle '(4)))))
  4008. (org-set-visibility-according-to-property 'no-cleanup)
  4009. (org-cycle-hide-archived-subtrees 'all)
  4010. (org-cycle-hide-drawers 'all)
  4011. (org-cycle-show-empty-lines 'all))
  4012. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4013. "Switch subtree visibilities according to :VISIBILITY: property."
  4014. (interactive)
  4015. (let (org-show-entry-below state)
  4016. (save-excursion
  4017. (goto-char (point-min))
  4018. (while (re-search-forward
  4019. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4020. nil t)
  4021. (setq state (match-string 1))
  4022. (save-excursion
  4023. (org-back-to-heading t)
  4024. (hide-subtree)
  4025. (org-reveal)
  4026. (cond
  4027. ((equal state '("fold" "folded"))
  4028. (hide-subtree))
  4029. ((equal state "children")
  4030. (org-show-hidden-entry)
  4031. (show-children))
  4032. ((equal state "content")
  4033. (save-excursion
  4034. (save-restriction
  4035. (org-narrow-to-subtree)
  4036. (org-content))))
  4037. ((member state '("all" "showall"))
  4038. (show-subtree)))))
  4039. (unless no-cleanup
  4040. (org-cycle-hide-archived-subtrees 'all)
  4041. (org-cycle-hide-drawers 'all)
  4042. (org-cycle-show-empty-lines 'all)))))
  4043. (defun org-overview ()
  4044. "Switch to overview mode, showing only top-level headlines.
  4045. Really, this shows all headlines with level equal or greater than the level
  4046. of the first headline in the buffer. This is important, because if the
  4047. first headline is not level one, then (hide-sublevels 1) gives confusing
  4048. results."
  4049. (interactive)
  4050. (let ((level (save-excursion
  4051. (goto-char (point-min))
  4052. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4053. (progn
  4054. (goto-char (match-beginning 0))
  4055. (funcall outline-level))))))
  4056. (and level (hide-sublevels level))))
  4057. (defun org-content (&optional arg)
  4058. "Show all headlines in the buffer, like a table of contents.
  4059. With numerical argument N, show content up to level N."
  4060. (interactive "P")
  4061. (save-excursion
  4062. ;; Visit all headings and show their offspring
  4063. (and (integerp arg) (org-overview))
  4064. (goto-char (point-max))
  4065. (catch 'exit
  4066. (while (and (progn (condition-case nil
  4067. (outline-previous-visible-heading 1)
  4068. (error (goto-char (point-min))))
  4069. t)
  4070. (looking-at outline-regexp))
  4071. (if (integerp arg)
  4072. (show-children (1- arg))
  4073. (show-branches))
  4074. (if (bobp) (throw 'exit nil))))))
  4075. (defun org-optimize-window-after-visibility-change (state)
  4076. "Adjust the window after a change in outline visibility.
  4077. This function is the default value of the hook `org-cycle-hook'."
  4078. (when (get-buffer-window (current-buffer))
  4079. (cond
  4080. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4081. ; ((eq state 'overview) (org-beginning-of-line))
  4082. ((eq state 'content) nil)
  4083. ((eq state 'all) nil)
  4084. ((eq state 'folded) nil)
  4085. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4086. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4087. (defun org-compact-display-after-subtree-move ()
  4088. "Show a compacter version of the tree of the entry's parent."
  4089. (save-excursion
  4090. (if (org-up-heading-safe)
  4091. (progn
  4092. (hide-subtree)
  4093. (show-entry)
  4094. (show-children)
  4095. (org-cycle-show-empty-lines 'children)
  4096. (org-cycle-hide-drawers 'children))
  4097. (org-overview))))
  4098. (defun org-cycle-show-empty-lines (state)
  4099. "Show empty lines above all visible headlines.
  4100. The region to be covered depends on STATE when called through
  4101. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4102. entire buffer covered. Note that an empty line is only shown if there
  4103. are at least `org-cycle-separator-lines' empty lines before the headline."
  4104. (when (> org-cycle-separator-lines 0)
  4105. (save-excursion
  4106. (let* ((n org-cycle-separator-lines)
  4107. (re (cond
  4108. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4109. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4110. (t (let ((ns (number-to-string (- n 2))))
  4111. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4112. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4113. beg end)
  4114. (cond
  4115. ((memq state '(overview contents t))
  4116. (setq beg (point-min) end (point-max)))
  4117. ((memq state '(children folded))
  4118. (setq beg (point) end (progn (org-end-of-subtree t t)
  4119. (beginning-of-line 2)
  4120. (point)))))
  4121. (when beg
  4122. (goto-char beg)
  4123. (while (re-search-forward re end t)
  4124. (if (not (get-char-property (match-end 1) 'invisible))
  4125. (outline-flag-region
  4126. (match-beginning 1) (match-end 1) nil)))))))
  4127. ;; Never hide empty lines at the end of the file.
  4128. (save-excursion
  4129. (goto-char (point-max))
  4130. (outline-previous-heading)
  4131. (outline-end-of-heading)
  4132. (if (and (looking-at "[ \t\n]+")
  4133. (= (match-end 0) (point-max)))
  4134. (outline-flag-region (point) (match-end 0) nil))))
  4135. (defun org-show-empty-lines-in-parent ()
  4136. "Move to the parent and re-show empty lines before visible headlines."
  4137. (save-excursion
  4138. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4139. (org-cycle-show-empty-lines context))))
  4140. (defun org-cycle-hide-drawers (state)
  4141. "Re-hide all drawers after a visibility state change."
  4142. (when (and (org-mode-p)
  4143. (not (memq state '(overview folded))))
  4144. (save-excursion
  4145. (let* ((globalp (memq state '(contents all)))
  4146. (beg (if globalp (point-min) (point)))
  4147. (end (if globalp (point-max) (org-end-of-subtree t))))
  4148. (goto-char beg)
  4149. (while (re-search-forward org-drawer-regexp end t)
  4150. (org-flag-drawer t))))))
  4151. (defun org-flag-drawer (flag)
  4152. (save-excursion
  4153. (beginning-of-line 1)
  4154. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4155. (let ((b (match-end 0))
  4156. (outline-regexp org-outline-regexp))
  4157. (if (re-search-forward
  4158. "^[ \t]*:END:"
  4159. (save-excursion (outline-next-heading) (point)) t)
  4160. (outline-flag-region b (point-at-eol) flag)
  4161. (error ":END: line missing"))))))
  4162. (defun org-subtree-end-visible-p ()
  4163. "Is the end of the current subtree visible?"
  4164. (pos-visible-in-window-p
  4165. (save-excursion (org-end-of-subtree t) (point))))
  4166. (defun org-first-headline-recenter (&optional N)
  4167. "Move cursor to the first headline and recenter the headline.
  4168. Optional argument N means, put the headline into the Nth line of the window."
  4169. (goto-char (point-min))
  4170. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4171. (beginning-of-line)
  4172. (recenter (prefix-numeric-value N))))
  4173. ;;; Org-goto
  4174. (defvar org-goto-window-configuration nil)
  4175. (defvar org-goto-marker nil)
  4176. (defvar org-goto-map
  4177. (let ((map (make-sparse-keymap)))
  4178. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4179. (while (setq cmd (pop cmds))
  4180. (substitute-key-definition cmd cmd map global-map)))
  4181. (suppress-keymap map)
  4182. (org-defkey map "\C-m" 'org-goto-ret)
  4183. (org-defkey map [(return)] 'org-goto-ret)
  4184. (org-defkey map [(left)] 'org-goto-left)
  4185. (org-defkey map [(right)] 'org-goto-right)
  4186. (org-defkey map [(control ?g)] 'org-goto-quit)
  4187. (org-defkey map "\C-i" 'org-cycle)
  4188. (org-defkey map [(tab)] 'org-cycle)
  4189. (org-defkey map [(down)] 'outline-next-visible-heading)
  4190. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4191. (if org-goto-auto-isearch
  4192. (if (fboundp 'define-key-after)
  4193. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4194. nil)
  4195. (org-defkey map "q" 'org-goto-quit)
  4196. (org-defkey map "n" 'outline-next-visible-heading)
  4197. (org-defkey map "p" 'outline-previous-visible-heading)
  4198. (org-defkey map "f" 'outline-forward-same-level)
  4199. (org-defkey map "b" 'outline-backward-same-level)
  4200. (org-defkey map "u" 'outline-up-heading))
  4201. (org-defkey map "/" 'org-occur)
  4202. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4203. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4204. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4205. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4206. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4207. map))
  4208. (defconst org-goto-help
  4209. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4210. RET=jump to location [Q]uit and return to previous location
  4211. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4212. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4213. ;; FIXME: Docstring doe not mention both interfaces
  4214. (defun org-goto (&optional alternative-interface)
  4215. "Look up a different location in the current file, keeping current visibility.
  4216. When you want look-up or go to a different location in a document, the
  4217. fastest way is often to fold the entire buffer and then dive into the tree.
  4218. This method has the disadvantage, that the previous location will be folded,
  4219. which may not be what you want.
  4220. This command works around this by showing a copy of the current buffer
  4221. in an indirect buffer, in overview mode. You can dive into the tree in
  4222. that copy, use org-occur and incremental search to find a location.
  4223. When pressing RET or `Q', the command returns to the original buffer in
  4224. which the visibility is still unchanged. After RET is will also jump to
  4225. the location selected in the indirect buffer and expose the
  4226. the headline hierarchy above."
  4227. (interactive "P")
  4228. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4229. (org-refile-use-outline-path t)
  4230. (interface
  4231. (if (not alternative-interface)
  4232. org-goto-interface
  4233. (if (eq org-goto-interface 'outline)
  4234. 'outline-path-completion
  4235. 'outline)))
  4236. (org-goto-start-pos (point))
  4237. (selected-point
  4238. (if (eq interface 'outline)
  4239. (car (org-get-location (current-buffer) org-goto-help))
  4240. (nth 3 (org-refile-get-location "Goto: ")))))
  4241. (if selected-point
  4242. (progn
  4243. (org-mark-ring-push org-goto-start-pos)
  4244. (goto-char selected-point)
  4245. (if (or (org-invisible-p) (org-invisible-p2))
  4246. (org-show-context 'org-goto)))
  4247. (message "Quit"))))
  4248. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4249. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4250. (defvar org-goto-local-auto-isearch-map) ; defined below
  4251. (defun org-get-location (buf help)
  4252. "Let the user select a location in the Org-mode buffer BUF.
  4253. This function uses a recursive edit. It returns the selected position
  4254. or nil."
  4255. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4256. (isearch-hide-immediately nil)
  4257. (isearch-search-fun-function
  4258. (lambda () 'org-goto-local-search-headings))
  4259. (org-goto-selected-point org-goto-exit-command))
  4260. (save-excursion
  4261. (save-window-excursion
  4262. (delete-other-windows)
  4263. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4264. (switch-to-buffer
  4265. (condition-case nil
  4266. (make-indirect-buffer (current-buffer) "*org-goto*")
  4267. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4268. (with-output-to-temp-buffer "*Help*"
  4269. (princ help))
  4270. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4271. (setq buffer-read-only nil)
  4272. (let ((org-startup-truncated t)
  4273. (org-startup-folded nil)
  4274. (org-startup-align-all-tables nil))
  4275. (org-mode)
  4276. (org-overview))
  4277. (setq buffer-read-only t)
  4278. (if (and (boundp 'org-goto-start-pos)
  4279. (integer-or-marker-p org-goto-start-pos))
  4280. (let ((org-show-hierarchy-above t)
  4281. (org-show-siblings t)
  4282. (org-show-following-heading t))
  4283. (goto-char org-goto-start-pos)
  4284. (and (org-invisible-p) (org-show-context)))
  4285. (goto-char (point-min)))
  4286. (org-beginning-of-line)
  4287. (message "Select location and press RET")
  4288. (use-local-map org-goto-map)
  4289. (recursive-edit)
  4290. ))
  4291. (kill-buffer "*org-goto*")
  4292. (cons org-goto-selected-point org-goto-exit-command)))
  4293. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4294. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4295. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4296. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4297. (defun org-goto-local-search-headings (string bound noerror)
  4298. "Search and make sure that any matches are in headlines."
  4299. (catch 'return
  4300. (while (if isearch-forward
  4301. (search-forward string bound noerror)
  4302. (search-backward string bound noerror))
  4303. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4304. (and (member :headline context)
  4305. (not (member :tags context))))
  4306. (throw 'return (point))))))
  4307. (defun org-goto-local-auto-isearch ()
  4308. "Start isearch."
  4309. (interactive)
  4310. (goto-char (point-min))
  4311. (let ((keys (this-command-keys)))
  4312. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4313. (isearch-mode t)
  4314. (isearch-process-search-char (string-to-char keys)))))
  4315. (defun org-goto-ret (&optional arg)
  4316. "Finish `org-goto' by going to the new location."
  4317. (interactive "P")
  4318. (setq org-goto-selected-point (point)
  4319. org-goto-exit-command 'return)
  4320. (throw 'exit nil))
  4321. (defun org-goto-left ()
  4322. "Finish `org-goto' by going to the new location."
  4323. (interactive)
  4324. (if (org-on-heading-p)
  4325. (progn
  4326. (beginning-of-line 1)
  4327. (setq org-goto-selected-point (point)
  4328. org-goto-exit-command 'left)
  4329. (throw 'exit nil))
  4330. (error "Not on a heading")))
  4331. (defun org-goto-right ()
  4332. "Finish `org-goto' by going to the new location."
  4333. (interactive)
  4334. (if (org-on-heading-p)
  4335. (progn
  4336. (setq org-goto-selected-point (point)
  4337. org-goto-exit-command 'right)
  4338. (throw 'exit nil))
  4339. (error "Not on a heading")))
  4340. (defun org-goto-quit ()
  4341. "Finish `org-goto' without cursor motion."
  4342. (interactive)
  4343. (setq org-goto-selected-point nil)
  4344. (setq org-goto-exit-command 'quit)
  4345. (throw 'exit nil))
  4346. ;;; Indirect buffer display of subtrees
  4347. (defvar org-indirect-dedicated-frame nil
  4348. "This is the frame being used for indirect tree display.")
  4349. (defvar org-last-indirect-buffer nil)
  4350. (defun org-tree-to-indirect-buffer (&optional arg)
  4351. "Create indirect buffer and narrow it to current subtree.
  4352. With numerical prefix ARG, go up to this level and then take that tree.
  4353. If ARG is negative, go up that many levels.
  4354. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4355. indirect buffer previously made with this command, to avoid proliferation of
  4356. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4357. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4358. is kept so that you can work with several indirect buffers at the same time.
  4359. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4360. requests that a new frame be made for the new buffer, so that the dedicated
  4361. frame is not changed."
  4362. (interactive "P")
  4363. (let ((cbuf (current-buffer))
  4364. (cwin (selected-window))
  4365. (pos (point))
  4366. beg end level heading ibuf)
  4367. (save-excursion
  4368. (org-back-to-heading t)
  4369. (when (numberp arg)
  4370. (setq level (org-outline-level))
  4371. (if (< arg 0) (setq arg (+ level arg)))
  4372. (while (> (setq level (org-outline-level)) arg)
  4373. (outline-up-heading 1 t)))
  4374. (setq beg (point)
  4375. heading (org-get-heading))
  4376. (org-end-of-subtree t) (setq end (point)))
  4377. (if (and (buffer-live-p org-last-indirect-buffer)
  4378. (not (eq org-indirect-buffer-display 'new-frame))
  4379. (not arg))
  4380. (kill-buffer org-last-indirect-buffer))
  4381. (setq ibuf (org-get-indirect-buffer cbuf)
  4382. org-last-indirect-buffer ibuf)
  4383. (cond
  4384. ((or (eq org-indirect-buffer-display 'new-frame)
  4385. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4386. (select-frame (make-frame))
  4387. (delete-other-windows)
  4388. (switch-to-buffer ibuf)
  4389. (org-set-frame-title heading))
  4390. ((eq org-indirect-buffer-display 'dedicated-frame)
  4391. (raise-frame
  4392. (select-frame (or (and org-indirect-dedicated-frame
  4393. (frame-live-p org-indirect-dedicated-frame)
  4394. org-indirect-dedicated-frame)
  4395. (setq org-indirect-dedicated-frame (make-frame)))))
  4396. (delete-other-windows)
  4397. (switch-to-buffer ibuf)
  4398. (org-set-frame-title (concat "Indirect: " heading)))
  4399. ((eq org-indirect-buffer-display 'current-window)
  4400. (switch-to-buffer ibuf))
  4401. ((eq org-indirect-buffer-display 'other-window)
  4402. (pop-to-buffer ibuf))
  4403. (t (error "Invalid value.")))
  4404. (if (featurep 'xemacs)
  4405. (save-excursion (org-mode) (turn-on-font-lock)))
  4406. (narrow-to-region beg end)
  4407. (show-all)
  4408. (goto-char pos)
  4409. (and (window-live-p cwin) (select-window cwin))))
  4410. (defun org-get-indirect-buffer (&optional buffer)
  4411. (setq buffer (or buffer (current-buffer)))
  4412. (let ((n 1) (base (buffer-name buffer)) bname)
  4413. (while (buffer-live-p
  4414. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4415. (setq n (1+ n)))
  4416. (condition-case nil
  4417. (make-indirect-buffer buffer bname 'clone)
  4418. (error (make-indirect-buffer buffer bname)))))
  4419. (defun org-set-frame-title (title)
  4420. "Set the title of the current frame to the string TITLE."
  4421. ;; FIXME: how to name a single frame in XEmacs???
  4422. (unless (featurep 'xemacs)
  4423. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4424. ;;;; Structure editing
  4425. ;;; Inserting headlines
  4426. (defun org-previous-line-empty-p ()
  4427. (save-excursion
  4428. (and (not (bobp))
  4429. (or (beginning-of-line 0) t)
  4430. (save-match-data
  4431. (looking-at "[ \t]*$")))))
  4432. (defun org-insert-heading (&optional force-heading)
  4433. "Insert a new heading or item with same depth at point.
  4434. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4435. If point is at the beginning of a headline, insert a sibling before the
  4436. current headline. If point is not at the beginning, do not split the line,
  4437. but create the new headline after the current line."
  4438. (interactive "P")
  4439. (if (= (buffer-size) 0)
  4440. (insert "\n* ")
  4441. (when (or force-heading (not (org-insert-item)))
  4442. (let* ((empty-line-p nil)
  4443. (head (save-excursion
  4444. (condition-case nil
  4445. (progn
  4446. (org-back-to-heading)
  4447. (setq empty-line-p (org-previous-line-empty-p))
  4448. (match-string 0))
  4449. (error "*"))))
  4450. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4451. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4452. pos hide-previous previous-pos)
  4453. (cond
  4454. ((and (org-on-heading-p) (bolp)
  4455. (or (bobp)
  4456. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4457. ;; insert before the current line
  4458. (open-line (if blank 2 1)))
  4459. ((and (bolp)
  4460. (or (bobp)
  4461. (save-excursion
  4462. (backward-char 1) (not (org-invisible-p)))))
  4463. ;; insert right here
  4464. nil)
  4465. (t
  4466. ;; somewhere in the line
  4467. (save-excursion
  4468. (setq previous-pos (point-at-bol))
  4469. (end-of-line)
  4470. (setq hide-previous (org-invisible-p)))
  4471. (and org-insert-heading-respect-content (org-show-subtree))
  4472. (let ((split
  4473. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4474. (save-excursion
  4475. (let ((p (point)))
  4476. (goto-char (point-at-bol))
  4477. (and (looking-at org-complex-heading-regexp)
  4478. (> p (match-beginning 4)))))))
  4479. tags pos)
  4480. (cond
  4481. (org-insert-heading-respect-content
  4482. (org-end-of-subtree nil t)
  4483. (or (bolp) (newline))
  4484. (or (org-previous-line-empty-p)
  4485. (and blank (newline)))
  4486. (open-line 1))
  4487. ((org-on-heading-p)
  4488. (when hide-previous
  4489. (show-children)
  4490. (org-show-entry))
  4491. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4492. (setq tags (and (match-end 2) (match-string 2)))
  4493. (and (match-end 1)
  4494. (delete-region (match-beginning 1) (match-end 1)))
  4495. (setq pos (point-at-bol))
  4496. (or split (end-of-line 1))
  4497. (delete-horizontal-space)
  4498. (newline (if blank 2 1))
  4499. (when tags
  4500. (save-excursion
  4501. (goto-char pos)
  4502. (end-of-line 1)
  4503. (insert " " tags)
  4504. (org-set-tags nil 'align))))
  4505. (t
  4506. (or split (end-of-line 1))
  4507. (newline (if blank 2 1)))))))
  4508. (insert head) (just-one-space)
  4509. (setq pos (point))
  4510. (end-of-line 1)
  4511. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4512. (when (and org-insert-heading-respect-content hide-previous)
  4513. (save-excursion
  4514. (goto-char previous-pos)
  4515. (hide-subtree)))
  4516. (run-hooks 'org-insert-heading-hook)))))
  4517. (defun org-get-heading (&optional no-tags)
  4518. "Return the heading of the current entry, without the stars."
  4519. (save-excursion
  4520. (org-back-to-heading t)
  4521. (if (looking-at
  4522. (if no-tags
  4523. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4524. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4525. (match-string 1) "")))
  4526. (defun org-heading-components ()
  4527. "Return the components of the current heading.
  4528. This is a list with the following elements:
  4529. - the level as an integer
  4530. - the reduced level, different if `org-odd-levels-only' is set.
  4531. - the TODO keyword, or nil
  4532. - the priority character, like ?A, or nil if no priority is given
  4533. - the headline text itself, or the tags string if no headline text
  4534. - the tags string, or nil."
  4535. (save-excursion
  4536. (org-back-to-heading t)
  4537. (if (looking-at org-complex-heading-regexp)
  4538. (list (length (match-string 1))
  4539. (org-reduced-level (length (match-string 1)))
  4540. (org-match-string-no-properties 2)
  4541. (and (match-end 3) (aref (match-string 3) 2))
  4542. (org-match-string-no-properties 4)
  4543. (org-match-string-no-properties 5)))))
  4544. (defun org-insert-heading-after-current ()
  4545. "Insert a new heading with same level as current, after current subtree."
  4546. (interactive)
  4547. (org-back-to-heading)
  4548. (org-insert-heading)
  4549. (org-move-subtree-down)
  4550. (end-of-line 1))
  4551. (defun org-insert-heading-respect-content ()
  4552. (interactive)
  4553. (let ((org-insert-heading-respect-content t))
  4554. (org-insert-heading t)))
  4555. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4556. (interactive "P")
  4557. (let ((org-insert-heading-respect-content t))
  4558. (org-insert-todo-heading force-state t)))
  4559. (defun org-insert-todo-heading (arg &optional force-heading)
  4560. "Insert a new heading with the same level and TODO state as current heading.
  4561. If the heading has no TODO state, or if the state is DONE, use the first
  4562. state (TODO by default). Also with prefix arg, force first state."
  4563. (interactive "P")
  4564. (when (or force-heading (not (org-insert-item 'checkbox)))
  4565. (org-insert-heading force-heading)
  4566. (save-excursion
  4567. (org-back-to-heading)
  4568. (outline-previous-heading)
  4569. (looking-at org-todo-line-regexp))
  4570. (if (or arg
  4571. (not (match-beginning 2))
  4572. (member (match-string 2) org-done-keywords))
  4573. (insert (car org-todo-keywords-1) " ")
  4574. (insert (match-string 2) " "))
  4575. (when org-provide-todo-statistics
  4576. (org-update-parent-todo-statistics))))
  4577. (defun org-insert-subheading (arg)
  4578. "Insert a new subheading and demote it.
  4579. Works for outline headings and for plain lists alike."
  4580. (interactive "P")
  4581. (org-insert-heading arg)
  4582. (cond
  4583. ((org-on-heading-p) (org-do-demote))
  4584. ((org-at-item-p) (org-indent-item 1))))
  4585. (defun org-insert-todo-subheading (arg)
  4586. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4587. Works for outline headings and for plain lists alike."
  4588. (interactive "P")
  4589. (org-insert-todo-heading arg)
  4590. (cond
  4591. ((org-on-heading-p) (org-do-demote))
  4592. ((org-at-item-p) (org-indent-item 1))))
  4593. ;;; Promotion and Demotion
  4594. (defun org-promote-subtree ()
  4595. "Promote the entire subtree.
  4596. See also `org-promote'."
  4597. (interactive)
  4598. (save-excursion
  4599. (org-map-tree 'org-promote))
  4600. (org-fix-position-after-promote))
  4601. (defun org-demote-subtree ()
  4602. "Demote the entire subtree. See `org-demote'.
  4603. See also `org-promote'."
  4604. (interactive)
  4605. (save-excursion
  4606. (org-map-tree 'org-demote))
  4607. (org-fix-position-after-promote))
  4608. (defun org-do-promote ()
  4609. "Promote the current heading higher up the tree.
  4610. If the region is active in `transient-mark-mode', promote all headings
  4611. in the region."
  4612. (interactive)
  4613. (save-excursion
  4614. (if (org-region-active-p)
  4615. (org-map-region 'org-promote (region-beginning) (region-end))
  4616. (org-promote)))
  4617. (org-fix-position-after-promote))
  4618. (defun org-do-demote ()
  4619. "Demote the current heading lower down the tree.
  4620. If the region is active in `transient-mark-mode', demote all headings
  4621. in the region."
  4622. (interactive)
  4623. (save-excursion
  4624. (if (org-region-active-p)
  4625. (org-map-region 'org-demote (region-beginning) (region-end))
  4626. (org-demote)))
  4627. (org-fix-position-after-promote))
  4628. (defun org-fix-position-after-promote ()
  4629. "Make sure that after pro/demotion cursor position is right."
  4630. (let ((pos (point)))
  4631. (when (save-excursion
  4632. (beginning-of-line 1)
  4633. (looking-at org-todo-line-regexp)
  4634. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4635. (cond ((eobp) (insert " "))
  4636. ((eolp) (insert " "))
  4637. ((equal (char-after) ?\ ) (forward-char 1))))))
  4638. (defun org-reduced-level (l)
  4639. "Compute the effective level of a heading.
  4640. This takes into account the setting of `org-odd-levels-only'."
  4641. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4642. (defun org-get-valid-level (level &optional change)
  4643. "Rectify a level change under the influence of `org-odd-levels-only'
  4644. LEVEL is a current level, CHANGE is by how much the level should be
  4645. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4646. even level numbers will become the next higher odd number."
  4647. (if org-odd-levels-only
  4648. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4649. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4650. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4651. (max 1 (+ level change))))
  4652. (if (boundp 'define-obsolete-function-alias)
  4653. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4654. (define-obsolete-function-alias 'org-get-legal-level
  4655. 'org-get-valid-level)
  4656. (define-obsolete-function-alias 'org-get-legal-level
  4657. 'org-get-valid-level "23.1")))
  4658. (defun org-promote ()
  4659. "Promote the current heading higher up the tree.
  4660. If the region is active in `transient-mark-mode', promote all headings
  4661. in the region."
  4662. (org-back-to-heading t)
  4663. (let* ((level (save-match-data (funcall outline-level)))
  4664. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4665. (diff (abs (- level (length up-head) -1))))
  4666. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4667. (replace-match up-head nil t)
  4668. ;; Fixup tag positioning
  4669. (and org-auto-align-tags (org-set-tags nil t))
  4670. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4671. (defun org-demote ()
  4672. "Demote the current heading lower down the tree.
  4673. If the region is active in `transient-mark-mode', demote all headings
  4674. in the region."
  4675. (org-back-to-heading t)
  4676. (let* ((level (save-match-data (funcall outline-level)))
  4677. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4678. (diff (abs (- level (length down-head) -1))))
  4679. (replace-match down-head nil t)
  4680. ;; Fixup tag positioning
  4681. (and org-auto-align-tags (org-set-tags nil t))
  4682. (if org-adapt-indentation (org-fixup-indentation diff))))
  4683. (defun org-map-tree (fun)
  4684. "Call FUN for every heading underneath the current one."
  4685. (org-back-to-heading)
  4686. (let ((level (funcall outline-level)))
  4687. (save-excursion
  4688. (funcall fun)
  4689. (while (and (progn
  4690. (outline-next-heading)
  4691. (> (funcall outline-level) level))
  4692. (not (eobp)))
  4693. (funcall fun)))))
  4694. (defun org-map-region (fun beg end)
  4695. "Call FUN for every heading between BEG and END."
  4696. (let ((org-ignore-region t))
  4697. (save-excursion
  4698. (setq end (copy-marker end))
  4699. (goto-char beg)
  4700. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4701. (< (point) end))
  4702. (funcall fun))
  4703. (while (and (progn
  4704. (outline-next-heading)
  4705. (< (point) end))
  4706. (not (eobp)))
  4707. (funcall fun)))))
  4708. (defun org-fixup-indentation (diff)
  4709. "Change the indentation in the current entry by DIFF
  4710. However, if any line in the current entry has no indentation, or if it
  4711. would end up with no indentation after the change, nothing at all is done."
  4712. (save-excursion
  4713. (let ((end (save-excursion (outline-next-heading)
  4714. (point-marker)))
  4715. (prohibit (if (> diff 0)
  4716. "^\\S-"
  4717. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4718. col)
  4719. (unless (save-excursion (end-of-line 1)
  4720. (re-search-forward prohibit end t))
  4721. (while (and (< (point) end)
  4722. (re-search-forward "^[ \t]+" end t))
  4723. (goto-char (match-end 0))
  4724. (setq col (current-column))
  4725. (if (< diff 0) (replace-match ""))
  4726. (org-indent-to-column (+ diff col))))
  4727. (move-marker end nil))))
  4728. (defun org-convert-to-odd-levels ()
  4729. "Convert an org-mode file with all levels allowed to one with odd levels.
  4730. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4731. level 5 etc."
  4732. (interactive)
  4733. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4734. (let ((org-odd-levels-only nil) n)
  4735. (save-excursion
  4736. (goto-char (point-min))
  4737. (while (re-search-forward "^\\*\\*+ " nil t)
  4738. (setq n (- (length (match-string 0)) 2))
  4739. (while (>= (setq n (1- n)) 0)
  4740. (org-demote))
  4741. (end-of-line 1))))))
  4742. (defun org-convert-to-oddeven-levels ()
  4743. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4744. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4745. section with an even level, conversion would destroy the structure of the file. An error
  4746. is signaled in this case."
  4747. (interactive)
  4748. (goto-char (point-min))
  4749. ;; First check if there are no even levels
  4750. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4751. (org-show-context t)
  4752. (error "Not all levels are odd in this file. Conversion not possible."))
  4753. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4754. (let ((org-odd-levels-only nil) n)
  4755. (save-excursion
  4756. (goto-char (point-min))
  4757. (while (re-search-forward "^\\*\\*+ " nil t)
  4758. (setq n (/ (1- (length (match-string 0))) 2))
  4759. (while (>= (setq n (1- n)) 0)
  4760. (org-promote))
  4761. (end-of-line 1))))))
  4762. (defun org-tr-level (n)
  4763. "Make N odd if required."
  4764. (if org-odd-levels-only (1+ (/ n 2)) n))
  4765. ;;; Vertical tree motion, cutting and pasting of subtrees
  4766. (defun org-move-subtree-up (&optional arg)
  4767. "Move the current subtree up past ARG headlines of the same level."
  4768. (interactive "p")
  4769. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4770. (defun org-move-subtree-down (&optional arg)
  4771. "Move the current subtree down past ARG headlines of the same level."
  4772. (interactive "p")
  4773. (setq arg (prefix-numeric-value arg))
  4774. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4775. 'outline-get-last-sibling))
  4776. (ins-point (make-marker))
  4777. (cnt (abs arg))
  4778. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4779. ;; Select the tree
  4780. (org-back-to-heading)
  4781. (setq beg0 (point))
  4782. (save-excursion
  4783. (setq ne-beg (org-back-over-empty-lines))
  4784. (setq beg (point)))
  4785. (save-match-data
  4786. (save-excursion (outline-end-of-heading)
  4787. (setq folded (org-invisible-p)))
  4788. (outline-end-of-subtree))
  4789. (outline-next-heading)
  4790. (setq ne-end (org-back-over-empty-lines))
  4791. (setq end (point))
  4792. (goto-char beg0)
  4793. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4794. ;; include less whitespace
  4795. (save-excursion
  4796. (goto-char beg)
  4797. (forward-line (- ne-beg ne-end))
  4798. (setq beg (point))))
  4799. ;; Find insertion point, with error handling
  4800. (while (> cnt 0)
  4801. (or (and (funcall movfunc) (looking-at outline-regexp))
  4802. (progn (goto-char beg0)
  4803. (error "Cannot move past superior level or buffer limit")))
  4804. (setq cnt (1- cnt)))
  4805. (if (> arg 0)
  4806. ;; Moving forward - still need to move over subtree
  4807. (progn (org-end-of-subtree t t)
  4808. (save-excursion
  4809. (org-back-over-empty-lines)
  4810. (or (bolp) (newline)))))
  4811. (setq ne-ins (org-back-over-empty-lines))
  4812. (move-marker ins-point (point))
  4813. (setq txt (buffer-substring beg end))
  4814. (org-save-markers-in-region beg end)
  4815. (delete-region beg end)
  4816. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4817. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4818. (let ((bbb (point)))
  4819. (insert-before-markers txt)
  4820. (org-reinstall-markers-in-region bbb)
  4821. (move-marker ins-point bbb))
  4822. (or (bolp) (insert "\n"))
  4823. (setq ins-end (point))
  4824. (goto-char ins-point)
  4825. (org-skip-whitespace)
  4826. (when (and (< arg 0)
  4827. (org-first-sibling-p)
  4828. (> ne-ins ne-beg))
  4829. ;; Move whitespace back to beginning
  4830. (save-excursion
  4831. (goto-char ins-end)
  4832. (let ((kill-whole-line t))
  4833. (kill-line (- ne-ins ne-beg)) (point)))
  4834. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4835. (move-marker ins-point nil)
  4836. (org-compact-display-after-subtree-move)
  4837. (org-show-empty-lines-in-parent)
  4838. (unless folded
  4839. (org-show-entry)
  4840. (show-children)
  4841. (org-cycle-hide-drawers 'children))))
  4842. (defvar org-subtree-clip ""
  4843. "Clipboard for cut and paste of subtrees.
  4844. This is actually only a copy of the kill, because we use the normal kill
  4845. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4846. (defvar org-subtree-clip-folded nil
  4847. "Was the last copied subtree folded?
  4848. This is used to fold the tree back after pasting.")
  4849. (defun org-cut-subtree (&optional n)
  4850. "Cut the current subtree into the clipboard.
  4851. With prefix arg N, cut this many sequential subtrees.
  4852. This is a short-hand for marking the subtree and then cutting it."
  4853. (interactive "p")
  4854. (org-copy-subtree n 'cut))
  4855. (defun org-copy-subtree (&optional n cut force-store-markers)
  4856. "Cut the current subtree into the clipboard.
  4857. With prefix arg N, cut this many sequential subtrees.
  4858. This is a short-hand for marking the subtree and then copying it.
  4859. If CUT is non-nil, actually cut the subtree.
  4860. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4861. of some markers in the region, even if CUT is non-nil. This is
  4862. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4863. (interactive "p")
  4864. (let (beg end folded (beg0 (point)))
  4865. (if (interactive-p)
  4866. (org-back-to-heading nil) ; take what looks like a subtree
  4867. (org-back-to-heading t)) ; take what is really there
  4868. (org-back-over-empty-lines)
  4869. (setq beg (point))
  4870. (skip-chars-forward " \t\r\n")
  4871. (save-match-data
  4872. (save-excursion (outline-end-of-heading)
  4873. (setq folded (org-invisible-p)))
  4874. (condition-case nil
  4875. (outline-forward-same-level (1- n))
  4876. (error nil))
  4877. (org-end-of-subtree t t))
  4878. (org-back-over-empty-lines)
  4879. (setq end (point))
  4880. (goto-char beg0)
  4881. (when (> end beg)
  4882. (setq org-subtree-clip-folded folded)
  4883. (when (or cut force-store-markers)
  4884. (org-save-markers-in-region beg end))
  4885. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4886. (setq org-subtree-clip (current-kill 0))
  4887. (message "%s: Subtree(s) with %d characters"
  4888. (if cut "Cut" "Copied")
  4889. (length org-subtree-clip)))))
  4890. (defun org-paste-subtree (&optional level tree for-yank)
  4891. "Paste the clipboard as a subtree, with modification of headline level.
  4892. The entire subtree is promoted or demoted in order to match a new headline
  4893. level.
  4894. If the cursor is at the beginning of a headline, the same level as
  4895. that headline is used to paste the tree
  4896. If not, the new level is derived from the *visible* headings
  4897. before and after the insertion point, and taken to be the inferior headline
  4898. level of the two. So if the previous visible heading is level 3 and the
  4899. next is level 4 (or vice versa), level 4 will be used for insertion.
  4900. This makes sure that the subtree remains an independent subtree and does
  4901. not swallow low level entries.
  4902. You can also force a different level, either by using a numeric prefix
  4903. argument, or by inserting the heading marker by hand. For example, if the
  4904. cursor is after \"*****\", then the tree will be shifted to level 5.
  4905. If optional TREE is given, use this text instead of the kill ring.
  4906. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4907. move back over whitespace before inserting, and move point to the end of
  4908. the inserted text when done."
  4909. (interactive "P")
  4910. (unless (org-kill-is-subtree-p tree)
  4911. (error "%s"
  4912. (substitute-command-keys
  4913. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4914. (let* ((visp (not (org-invisible-p)))
  4915. (txt (or tree (and kill-ring (current-kill 0))))
  4916. (^re (concat "^\\(" outline-regexp "\\)"))
  4917. (re (concat "\\(" outline-regexp "\\)"))
  4918. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4919. (old-level (if (string-match ^re txt)
  4920. (- (match-end 0) (match-beginning 0) 1)
  4921. -1))
  4922. (force-level (cond (level (prefix-numeric-value level))
  4923. ((and (looking-at "[ \t]*$")
  4924. (string-match
  4925. ^re_ (buffer-substring
  4926. (point-at-bol) (point))))
  4927. (- (match-end 1) (match-beginning 1)))
  4928. ((and (bolp)
  4929. (looking-at org-outline-regexp))
  4930. (- (match-end 0) (point) 1))
  4931. (t nil)))
  4932. (previous-level (save-excursion
  4933. (condition-case nil
  4934. (progn
  4935. (outline-previous-visible-heading 1)
  4936. (if (looking-at re)
  4937. (- (match-end 0) (match-beginning 0) 1)
  4938. 1))
  4939. (error 1))))
  4940. (next-level (save-excursion
  4941. (condition-case nil
  4942. (progn
  4943. (or (looking-at outline-regexp)
  4944. (outline-next-visible-heading 1))
  4945. (if (looking-at re)
  4946. (- (match-end 0) (match-beginning 0) 1)
  4947. 1))
  4948. (error 1))))
  4949. (new-level (or force-level (max previous-level next-level)))
  4950. (shift (if (or (= old-level -1)
  4951. (= new-level -1)
  4952. (= old-level new-level))
  4953. 0
  4954. (- new-level old-level)))
  4955. (delta (if (> shift 0) -1 1))
  4956. (func (if (> shift 0) 'org-demote 'org-promote))
  4957. (org-odd-levels-only nil)
  4958. beg end newend)
  4959. ;; Remove the forced level indicator
  4960. (if force-level
  4961. (delete-region (point-at-bol) (point)))
  4962. ;; Paste
  4963. (beginning-of-line 1)
  4964. (unless for-yank (org-back-over-empty-lines))
  4965. (setq beg (point))
  4966. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4967. (insert-before-markers txt)
  4968. (unless (string-match "\n\\'" txt) (insert "\n"))
  4969. (setq newend (point))
  4970. (org-reinstall-markers-in-region beg)
  4971. (setq end (point))
  4972. (goto-char beg)
  4973. (skip-chars-forward " \t\n\r")
  4974. (setq beg (point))
  4975. (if (and (org-invisible-p) visp)
  4976. (save-excursion (outline-show-heading)))
  4977. ;; Shift if necessary
  4978. (unless (= shift 0)
  4979. (save-restriction
  4980. (narrow-to-region beg end)
  4981. (while (not (= shift 0))
  4982. (org-map-region func (point-min) (point-max))
  4983. (setq shift (+ delta shift)))
  4984. (goto-char (point-min))
  4985. (setq newend (point-max))))
  4986. (when (or (interactive-p) for-yank)
  4987. (message "Clipboard pasted as level %d subtree" new-level))
  4988. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4989. kill-ring
  4990. (eq org-subtree-clip (current-kill 0))
  4991. org-subtree-clip-folded)
  4992. ;; The tree was folded before it was killed/copied
  4993. (hide-subtree))
  4994. (and for-yank (goto-char newend))))
  4995. (defun org-kill-is-subtree-p (&optional txt)
  4996. "Check if the current kill is an outline subtree, or a set of trees.
  4997. Returns nil if kill does not start with a headline, or if the first
  4998. headline level is not the largest headline level in the tree.
  4999. So this will actually accept several entries of equal levels as well,
  5000. which is OK for `org-paste-subtree'.
  5001. If optional TXT is given, check this string instead of the current kill."
  5002. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5003. (start-level (and kill
  5004. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5005. org-outline-regexp "\\)")
  5006. kill)
  5007. (- (match-end 2) (match-beginning 2) 1)))
  5008. (re (concat "^" org-outline-regexp))
  5009. (start (1+ (or (match-beginning 2) -1))))
  5010. (if (not start-level)
  5011. (progn
  5012. nil) ;; does not even start with a heading
  5013. (catch 'exit
  5014. (while (setq start (string-match re kill (1+ start)))
  5015. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5016. (throw 'exit nil)))
  5017. t))))
  5018. (defvar org-markers-to-move nil
  5019. "Markers that should be moved with a cut-and-paste operation.
  5020. Those markers are stored together with their positions relative to
  5021. the start of the region.")
  5022. (defun org-save-markers-in-region (beg end)
  5023. "Check markers in region.
  5024. If these markers are between BEG and END, record their position relative
  5025. to BEG, so that after moving the block of text, we can put the markers back
  5026. into place.
  5027. This function gets called just before an entry or tree gets cut from the
  5028. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5029. called immediately, to move the markers with the entries."
  5030. (setq org-markers-to-move nil)
  5031. (when (featurep 'org-clock)
  5032. (org-clock-save-markers-for-cut-and-paste beg end))
  5033. (when (featurep 'org-agenda)
  5034. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5035. (defun org-check-and-save-marker (marker beg end)
  5036. "Check if MARKER is between BEG and END.
  5037. If yes, remember the marker and the distance to BEG."
  5038. (when (and (marker-buffer marker)
  5039. (equal (marker-buffer marker) (current-buffer)))
  5040. (if (and (>= marker beg) (< marker end))
  5041. (push (cons marker (- marker beg)) org-markers-to-move))))
  5042. (defun org-reinstall-markers-in-region (beg)
  5043. "Move all remembered markers to their position relative to BEG."
  5044. (mapc (lambda (x)
  5045. (move-marker (car x) (+ beg (cdr x))))
  5046. org-markers-to-move)
  5047. (setq org-markers-to-move nil))
  5048. (defun org-narrow-to-subtree ()
  5049. "Narrow buffer to the current subtree."
  5050. (interactive)
  5051. (save-excursion
  5052. (save-match-data
  5053. (narrow-to-region
  5054. (progn (org-back-to-heading) (point))
  5055. (progn (org-end-of-subtree t) (point))))))
  5056. ;;; Outline Sorting
  5057. (defun org-sort (with-case)
  5058. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5059. Optional argument WITH-CASE means sort case-sensitively."
  5060. (interactive "P")
  5061. (if (org-at-table-p)
  5062. (org-call-with-arg 'org-table-sort-lines with-case)
  5063. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5064. (defun org-sort-remove-invisible (s)
  5065. (remove-text-properties 0 (length s) org-rm-props s)
  5066. (while (string-match org-bracket-link-regexp s)
  5067. (setq s (replace-match (if (match-end 2)
  5068. (match-string 3 s)
  5069. (match-string 1 s)) t t s)))
  5070. s)
  5071. (defvar org-priority-regexp) ; defined later in the file
  5072. (defun org-sort-entries-or-items
  5073. (&optional with-case sorting-type getkey-func compare-func property)
  5074. "Sort entries on a certain level of an outline tree.
  5075. If there is an active region, the entries in the region are sorted.
  5076. Else, if the cursor is before the first entry, sort the top-level items.
  5077. Else, the children of the entry at point are sorted.
  5078. Sorting can be alphabetically, numerically, and by date/time as given by
  5079. the first time stamp in the entry. The command prompts for the sorting
  5080. type unless it has been given to the function through the SORTING-TYPE
  5081. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5082. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5083. called with point at the beginning of the record. It must return either
  5084. a string or a number that should serve as the sorting key for that record.
  5085. Comparing entries ignores case by default. However, with an optional argument
  5086. WITH-CASE, the sorting considers case as well."
  5087. (interactive "P")
  5088. (let ((case-func (if with-case 'identity 'downcase))
  5089. start beg end stars re re2
  5090. txt what tmp plain-list-p)
  5091. ;; Find beginning and end of region to sort
  5092. (cond
  5093. ((org-region-active-p)
  5094. ;; we will sort the region
  5095. (setq end (region-end)
  5096. what "region")
  5097. (goto-char (region-beginning))
  5098. (if (not (org-on-heading-p)) (outline-next-heading))
  5099. (setq start (point)))
  5100. ((org-at-item-p)
  5101. ;; we will sort this plain list
  5102. (org-beginning-of-item-list) (setq start (point))
  5103. (org-end-of-item-list) (setq end (point))
  5104. (goto-char start)
  5105. (setq plain-list-p t
  5106. what "plain list"))
  5107. ((or (org-on-heading-p)
  5108. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5109. ;; we will sort the children of the current headline
  5110. (org-back-to-heading)
  5111. (setq start (point)
  5112. end (progn (org-end-of-subtree t t)
  5113. (org-back-over-empty-lines)
  5114. (point))
  5115. what "children")
  5116. (goto-char start)
  5117. (show-subtree)
  5118. (outline-next-heading))
  5119. (t
  5120. ;; we will sort the top-level entries in this file
  5121. (goto-char (point-min))
  5122. (or (org-on-heading-p) (outline-next-heading))
  5123. (setq start (point) end (point-max) what "top-level")
  5124. (goto-char start)
  5125. (show-all)))
  5126. (setq beg (point))
  5127. (if (>= beg end) (error "Nothing to sort"))
  5128. (unless plain-list-p
  5129. (looking-at "\\(\\*+\\)")
  5130. (setq stars (match-string 1)
  5131. re (concat "^" (regexp-quote stars) " +")
  5132. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5133. txt (buffer-substring beg end))
  5134. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5135. (if (and (not (equal stars "*")) (string-match re2 txt))
  5136. (error "Region to sort contains a level above the first entry")))
  5137. (unless sorting-type
  5138. (message
  5139. (if plain-list-p
  5140. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5141. "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:")
  5142. what)
  5143. (setq sorting-type (read-char-exclusive))
  5144. (and (= (downcase sorting-type) ?f)
  5145. (setq getkey-func
  5146. (org-ido-completing-read "Sort using function: "
  5147. obarray 'fboundp t nil nil))
  5148. (setq getkey-func (intern getkey-func)))
  5149. (and (= (downcase sorting-type) ?r)
  5150. (setq property
  5151. (org-ido-completing-read "Property: "
  5152. (mapcar 'list (org-buffer-property-keys t))
  5153. nil t))))
  5154. (message "Sorting entries...")
  5155. (save-restriction
  5156. (narrow-to-region start end)
  5157. (let ((dcst (downcase sorting-type))
  5158. (now (current-time)))
  5159. (sort-subr
  5160. (/= dcst sorting-type)
  5161. ;; This function moves to the beginning character of the "record" to
  5162. ;; be sorted.
  5163. (if plain-list-p
  5164. (lambda nil
  5165. (if (org-at-item-p) t (goto-char (point-max))))
  5166. (lambda nil
  5167. (if (re-search-forward re nil t)
  5168. (goto-char (match-beginning 0))
  5169. (goto-char (point-max)))))
  5170. ;; This function moves to the last character of the "record" being
  5171. ;; sorted.
  5172. (if plain-list-p
  5173. 'org-end-of-item
  5174. (lambda nil
  5175. (save-match-data
  5176. (condition-case nil
  5177. (outline-forward-same-level 1)
  5178. (error
  5179. (goto-char (point-max)))))))
  5180. ;; This function returns the value that gets sorted against.
  5181. (if plain-list-p
  5182. (lambda nil
  5183. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5184. (cond
  5185. ((= dcst ?n)
  5186. (string-to-number (buffer-substring (match-end 0)
  5187. (point-at-eol))))
  5188. ((= dcst ?a)
  5189. (buffer-substring (match-end 0) (point-at-eol)))
  5190. ((= dcst ?t)
  5191. (if (re-search-forward org-ts-regexp
  5192. (point-at-eol) t)
  5193. (org-time-string-to-time (match-string 0))
  5194. now))
  5195. ((= dcst ?f)
  5196. (if getkey-func
  5197. (progn
  5198. (setq tmp (funcall getkey-func))
  5199. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5200. tmp)
  5201. (error "Invalid key function `%s'" getkey-func)))
  5202. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5203. (lambda nil
  5204. (cond
  5205. ((= dcst ?n)
  5206. (if (looking-at org-complex-heading-regexp)
  5207. (string-to-number (match-string 4))
  5208. nil))
  5209. ((= dcst ?a)
  5210. (if (looking-at org-complex-heading-regexp)
  5211. (funcall case-func (match-string 4))
  5212. nil))
  5213. ((= dcst ?t)
  5214. (if (re-search-forward org-ts-regexp
  5215. (save-excursion
  5216. (forward-line 2)
  5217. (point)) t)
  5218. (org-time-string-to-time (match-string 0))
  5219. now))
  5220. ((= dcst ?p)
  5221. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5222. (string-to-char (match-string 2))
  5223. org-default-priority))
  5224. ((= dcst ?r)
  5225. (or (org-entry-get nil property) ""))
  5226. ((= dcst ?o)
  5227. (if (looking-at org-complex-heading-regexp)
  5228. (- 9999 (length (member (match-string 2)
  5229. org-todo-keywords-1)))))
  5230. ((= dcst ?f)
  5231. (if getkey-func
  5232. (progn
  5233. (setq tmp (funcall getkey-func))
  5234. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5235. tmp)
  5236. (error "Invalid key function `%s'" getkey-func)))
  5237. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5238. nil
  5239. (cond
  5240. ((= dcst ?a) 'string<)
  5241. ((= dcst ?t) 'time-less-p)
  5242. ((= dcst ?f) compare-func)
  5243. (t nil)))))
  5244. (message "Sorting entries...done")))
  5245. (defun org-do-sort (table what &optional with-case sorting-type)
  5246. "Sort TABLE of WHAT according to SORTING-TYPE.
  5247. The user will be prompted for the SORTING-TYPE if the call to this
  5248. function does not specify it. WHAT is only for the prompt, to indicate
  5249. what is being sorted. The sorting key will be extracted from
  5250. the car of the elements of the table.
  5251. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5252. (unless sorting-type
  5253. (message
  5254. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5255. what)
  5256. (setq sorting-type (read-char-exclusive)))
  5257. (let ((dcst (downcase sorting-type))
  5258. extractfun comparefun)
  5259. ;; Define the appropriate functions
  5260. (cond
  5261. ((= dcst ?n)
  5262. (setq extractfun 'string-to-number
  5263. comparefun (if (= dcst sorting-type) '< '>)))
  5264. ((= dcst ?a)
  5265. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5266. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5267. comparefun (if (= dcst sorting-type)
  5268. 'string<
  5269. (lambda (a b) (and (not (string< a b))
  5270. (not (string= a b)))))))
  5271. ((= dcst ?t)
  5272. (setq extractfun
  5273. (lambda (x)
  5274. (if (string-match org-ts-regexp x)
  5275. (time-to-seconds
  5276. (org-time-string-to-time (match-string 0 x)))
  5277. 0))
  5278. comparefun (if (= dcst sorting-type) '< '>)))
  5279. (t (error "Invalid sorting type `%c'" sorting-type)))
  5280. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5281. table)
  5282. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5283. ;;; Editing source examples
  5284. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5285. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5286. (defvar org-edit-src-force-single-line nil)
  5287. (defvar org-edit-src-from-org-mode nil)
  5288. (defvar org-edit-src-picture nil)
  5289. (define-minor-mode org-exit-edit-mode
  5290. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5291. (defun org-edit-src-code ()
  5292. "Edit the source code example at point.
  5293. An indirect buffer is created, and that buffer is then narrowed to the
  5294. example at point and switched to the correct language mode. When done,
  5295. exit by killing the buffer with \\[org-edit-src-exit]."
  5296. (interactive)
  5297. (let ((line (org-current-line))
  5298. (case-fold-search t)
  5299. (msg (substitute-command-keys
  5300. "Edit, then exit with C-c ' (C-c and single quote)"))
  5301. (info (org-edit-src-find-region-and-lang))
  5302. (org-mode-p (eq major-mode 'org-mode))
  5303. beg end lang lang-f single lfmt)
  5304. (if (not info)
  5305. nil
  5306. (setq beg (nth 0 info)
  5307. end (nth 1 info)
  5308. lang (nth 2 info)
  5309. single (nth 3 info)
  5310. lfmt (nth 4 info)
  5311. lang-f (intern (concat lang "-mode")))
  5312. (unless (functionp lang-f)
  5313. (error "No such language mode: %s" lang-f))
  5314. (goto-line line)
  5315. (if (get-buffer "*Org Edit Src Example*")
  5316. (kill-buffer "*Org Edit Src Example*"))
  5317. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5318. "*Org Edit Src Example*"))
  5319. (narrow-to-region beg end)
  5320. (remove-text-properties beg end '(display nil invisible nil
  5321. intangible nil))
  5322. (let ((org-inhibit-startup t))
  5323. (funcall lang-f))
  5324. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5325. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5326. (when lfmt
  5327. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5328. (when org-mode-p
  5329. (goto-char (point-min))
  5330. (while (re-search-forward "^," nil t)
  5331. (replace-match "")))
  5332. (goto-line line)
  5333. (org-exit-edit-mode)
  5334. (org-set-local 'header-line-format msg)
  5335. (message "%s" msg)
  5336. t)))
  5337. (defun org-edit-fixed-width-region ()
  5338. "Edit the fixed-width ascii drawing at point.
  5339. This must be a region where each line starts with a colon followed by
  5340. a space character.
  5341. An indirect buffer is created, and that buffer is then narrowed to the
  5342. example at point and switched to artist-mode. When done,
  5343. exit by killing the buffer with \\[org-edit-src-exit]."
  5344. (interactive)
  5345. (let ((line (org-current-line))
  5346. (case-fold-search t)
  5347. (msg (substitute-command-keys
  5348. "Edit, then exit with C-c ' (C-c and single quote)"))
  5349. (org-mode-p (eq major-mode 'org-mode))
  5350. beg end)
  5351. (beginning-of-line 1)
  5352. (if (looking-at "[ \t]*[^:\n \t]")
  5353. nil
  5354. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5355. (setq beg (point) end beg)
  5356. (save-excursion
  5357. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5358. (setq beg (point-at-bol 2))
  5359. (setq beg (point))))
  5360. (save-excursion
  5361. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5362. (setq end (1- (match-beginning 0)))
  5363. (setq end (point))))
  5364. (goto-line line))
  5365. (if (get-buffer "*Org Edit Picture*")
  5366. (kill-buffer "*Org Edit Picture*"))
  5367. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5368. "*Org Edit Picture*"))
  5369. (narrow-to-region beg end)
  5370. (remove-text-properties beg end '(display nil invisible nil
  5371. intangible nil))
  5372. (when (fboundp 'font-lock-unfontify-region)
  5373. (font-lock-unfontify-region (point-min) (point-max)))
  5374. (cond
  5375. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5376. (fundamental-mode)
  5377. (artist-mode 1))
  5378. (t (funcall org-edit-fixed-width-region-mode)))
  5379. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5380. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5381. (set (make-local-variable 'org-edit-src-picture) t)
  5382. (goto-char (point-min))
  5383. (while (re-search-forward "^[ \t]*: ?" nil t)
  5384. (replace-match ""))
  5385. (goto-line line)
  5386. (org-exit-edit-mode)
  5387. (org-set-local 'header-line-format msg)
  5388. (message "%s" msg)
  5389. t)))
  5390. (defun org-edit-src-find-region-and-lang ()
  5391. "Find the region and language for a local edit.
  5392. Return a list with beginning and end of the region, a string representing
  5393. the language, a switch telling of the content should be in a single line."
  5394. (let ((re-list
  5395. (append
  5396. org-edit-src-region-extra
  5397. '(
  5398. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5399. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5400. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5401. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5402. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5403. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5404. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5405. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5406. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5407. ("^#\\+html:" "\n" "html" single-line)
  5408. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5409. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5410. ("^#\\+latex:" "\n" "latex" single-line)
  5411. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5412. ("^#\\+ascii:" "\n" "ascii" single-line)
  5413. )))
  5414. (pos (point))
  5415. re1 re2 single beg end lang lfmt match-re1)
  5416. (catch 'exit
  5417. (while (setq entry (pop re-list))
  5418. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5419. single (nth 3 entry))
  5420. (save-excursion
  5421. (if (or (looking-at re1)
  5422. (re-search-backward re1 nil t))
  5423. (progn
  5424. (setq match-re1 (match-string 0))
  5425. (setq beg (match-end 0)
  5426. lang (org-edit-src-get-lang lang)
  5427. lfmt (org-edit-src-get-label-format match-re1))
  5428. (if (and (re-search-forward re2 nil t)
  5429. (>= (match-end 0) pos))
  5430. (throw 'exit (list beg (match-beginning 0)
  5431. lang single lfmt))))
  5432. (if (or (looking-at re2)
  5433. (re-search-forward re2 nil t))
  5434. (progn
  5435. (setq end (match-beginning 0))
  5436. (if (and (re-search-backward re1 nil t)
  5437. (<= (match-beginning 0) pos))
  5438. (progn
  5439. (setq lfmt (org-edit-src-get-label-format
  5440. (match-string 0)))
  5441. (throw 'exit
  5442. (list (match-end 0) end
  5443. (org-edit-src-get-lang lang)
  5444. single lfmt))))))))))))
  5445. (defun org-edit-src-get-lang (lang)
  5446. "Extract the src language."
  5447. (let ((m (match-string 0)))
  5448. (cond
  5449. ((stringp lang) lang)
  5450. ((integerp lang) (match-string lang))
  5451. ((and (eq lang 'lang)
  5452. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5453. (match-string 1 m))
  5454. ((and (eq lang 'style)
  5455. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5456. (match-string 1 m))
  5457. (t "fundamental"))))
  5458. (defun org-edit-src-get-label-format (s)
  5459. "Extract the label format."
  5460. (save-match-data
  5461. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5462. (match-string 1 s))))
  5463. (defun org-edit-src-exit ()
  5464. "Exit special edit and protect problematic lines."
  5465. (interactive)
  5466. (unless (buffer-base-buffer (current-buffer))
  5467. (error "This is not an indirect buffer, something is wrong..."))
  5468. (unless (> (point-min) 1)
  5469. (error "This buffer is not narrowed, something is wrong..."))
  5470. (goto-char (point-min))
  5471. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5472. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5473. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5474. (goto-char (point-min))
  5475. (while (re-search-forward "\n" nil t)
  5476. (replace-match " "))
  5477. (goto-char (point-min))
  5478. (if (looking-at "\\s-*") (replace-match " "))
  5479. (if (re-search-forward "\\s-+\\'" nil t)
  5480. (replace-match "")))
  5481. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5482. (goto-char (point-min))
  5483. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5484. (replace-match ",\\1"))
  5485. (when font-lock-mode
  5486. (font-lock-unfontify-region (point-min) (point-max)))
  5487. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5488. (when (org-bound-and-true-p org-edit-src-picture)
  5489. (untabify (point-min) (point-max))
  5490. (goto-char (point-min))
  5491. (while (re-search-forward "^" nil t)
  5492. (replace-match ": "))
  5493. (when font-lock-mode
  5494. (font-lock-unfontify-region (point-min) (point-max)))
  5495. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5496. (kill-buffer (current-buffer))
  5497. (and (org-mode-p) (org-restart-font-lock)))
  5498. ;;; The orgstruct minor mode
  5499. ;; Define a minor mode which can be used in other modes in order to
  5500. ;; integrate the org-mode structure editing commands.
  5501. ;; This is really a hack, because the org-mode structure commands use
  5502. ;; keys which normally belong to the major mode. Here is how it
  5503. ;; works: The minor mode defines all the keys necessary to operate the
  5504. ;; structure commands, but wraps the commands into a function which
  5505. ;; tests if the cursor is currently at a headline or a plain list
  5506. ;; item. If that is the case, the structure command is used,
  5507. ;; temporarily setting many Org-mode variables like regular
  5508. ;; expressions for filling etc. However, when any of those keys is
  5509. ;; used at a different location, function uses `key-binding' to look
  5510. ;; up if the key has an associated command in another currently active
  5511. ;; keymap (minor modes, major mode, global), and executes that
  5512. ;; command. There might be problems if any of the keys is otherwise
  5513. ;; used as a prefix key.
  5514. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5515. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5516. ;; addresses this by checking explicitly for both bindings.
  5517. (defvar orgstruct-mode-map (make-sparse-keymap)
  5518. "Keymap for the minor `orgstruct-mode'.")
  5519. (defvar org-local-vars nil
  5520. "List of local variables, for use by `orgstruct-mode'")
  5521. ;;;###autoload
  5522. (define-minor-mode orgstruct-mode
  5523. "Toggle the minor more `orgstruct-mode'.
  5524. This mode is for using Org-mode structure commands in other modes.
  5525. The following key behave as if Org-mode was active, if the cursor
  5526. is on a headline, or on a plain list item (both in the definition
  5527. of Org-mode).
  5528. M-up Move entry/item up
  5529. M-down Move entry/item down
  5530. M-left Promote
  5531. M-right Demote
  5532. M-S-up Move entry/item up
  5533. M-S-down Move entry/item down
  5534. M-S-left Promote subtree
  5535. M-S-right Demote subtree
  5536. M-q Fill paragraph and items like in Org-mode
  5537. C-c ^ Sort entries
  5538. C-c - Cycle list bullet
  5539. TAB Cycle item visibility
  5540. M-RET Insert new heading/item
  5541. S-M-RET Insert new TODO heading / Checkbox item
  5542. C-c C-c Set tags / toggle checkbox"
  5543. nil " OrgStruct" nil
  5544. (org-load-modules-maybe)
  5545. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5546. ;;;###autoload
  5547. (defun turn-on-orgstruct ()
  5548. "Unconditionally turn on `orgstruct-mode'."
  5549. (orgstruct-mode 1))
  5550. ;;;###autoload
  5551. (defun turn-on-orgstruct++ ()
  5552. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5553. In addition to setting orgstruct-mode, this also exports all indentation and
  5554. autofilling variables from org-mode into the buffer. Note that turning
  5555. off orgstruct-mode will *not* remove these additional settings."
  5556. (orgstruct-mode 1)
  5557. (let (var val)
  5558. (mapc
  5559. (lambda (x)
  5560. (when (string-match
  5561. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5562. (symbol-name (car x)))
  5563. (setq var (car x) val (nth 1 x))
  5564. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5565. org-local-vars)))
  5566. (defun orgstruct-error ()
  5567. "Error when there is no default binding for a structure key."
  5568. (interactive)
  5569. (error "This key has no function outside structure elements"))
  5570. (defun orgstruct-setup ()
  5571. "Setup orgstruct keymaps."
  5572. (let ((nfunc 0)
  5573. (bindings
  5574. (list
  5575. '([(meta up)] org-metaup)
  5576. '([(meta down)] org-metadown)
  5577. '([(meta left)] org-metaleft)
  5578. '([(meta right)] org-metaright)
  5579. '([(meta shift up)] org-shiftmetaup)
  5580. '([(meta shift down)] org-shiftmetadown)
  5581. '([(meta shift left)] org-shiftmetaleft)
  5582. '([(meta shift right)] org-shiftmetaright)
  5583. '([(shift up)] org-shiftup)
  5584. '([(shift down)] org-shiftdown)
  5585. '([(shift left)] org-shiftleft)
  5586. '([(shift right)] org-shiftright)
  5587. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5588. '("\M-q" fill-paragraph)
  5589. '("\C-c^" org-sort)
  5590. '("\C-c-" org-cycle-list-bullet)))
  5591. elt key fun cmd)
  5592. (while (setq elt (pop bindings))
  5593. (setq nfunc (1+ nfunc))
  5594. (setq key (org-key (car elt))
  5595. fun (nth 1 elt)
  5596. cmd (orgstruct-make-binding fun nfunc key))
  5597. (org-defkey orgstruct-mode-map key cmd))
  5598. ;; Special treatment needed for TAB and RET
  5599. (org-defkey orgstruct-mode-map [(tab)]
  5600. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5601. (org-defkey orgstruct-mode-map "\C-i"
  5602. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5603. (org-defkey orgstruct-mode-map "\M-\C-m"
  5604. (orgstruct-make-binding 'org-insert-heading 105
  5605. "\M-\C-m" [(meta return)]))
  5606. (org-defkey orgstruct-mode-map [(meta return)]
  5607. (orgstruct-make-binding 'org-insert-heading 106
  5608. [(meta return)] "\M-\C-m"))
  5609. (org-defkey orgstruct-mode-map [(shift meta return)]
  5610. (orgstruct-make-binding 'org-insert-todo-heading 107
  5611. [(meta return)] "\M-\C-m"))
  5612. (unless org-local-vars
  5613. (setq org-local-vars (org-get-local-variables)))
  5614. t))
  5615. (defun orgstruct-make-binding (fun n &rest keys)
  5616. "Create a function for binding in the structure minor mode.
  5617. FUN is the command to call inside a table. N is used to create a unique
  5618. command name. KEYS are keys that should be checked in for a command
  5619. to execute outside of tables."
  5620. (eval
  5621. (list 'defun
  5622. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5623. '(arg)
  5624. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5625. "Outside of structure, run the binding of `"
  5626. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5627. "'.")
  5628. '(interactive "p")
  5629. (list 'if
  5630. '(org-context-p 'headline 'item)
  5631. (list 'org-run-like-in-org-mode (list 'quote fun))
  5632. (list 'let '(orgstruct-mode)
  5633. (list 'call-interactively
  5634. (append '(or)
  5635. (mapcar (lambda (k)
  5636. (list 'key-binding k))
  5637. keys)
  5638. '('orgstruct-error))))))))
  5639. (defun org-context-p (&rest contexts)
  5640. "Check if local context is any of CONTEXTS.
  5641. Possible values in the list of contexts are `table', `headline', and `item'."
  5642. (let ((pos (point)))
  5643. (goto-char (point-at-bol))
  5644. (prog1 (or (and (memq 'table contexts)
  5645. (looking-at "[ \t]*|"))
  5646. (and (memq 'headline contexts)
  5647. ;;????????? (looking-at "\\*+"))
  5648. (looking-at outline-regexp))
  5649. (and (memq 'item contexts)
  5650. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5651. (goto-char pos))))
  5652. (defun org-get-local-variables ()
  5653. "Return a list of all local variables in an org-mode buffer."
  5654. (let (varlist)
  5655. (with-current-buffer (get-buffer-create "*Org tmp*")
  5656. (erase-buffer)
  5657. (org-mode)
  5658. (setq varlist (buffer-local-variables)))
  5659. (kill-buffer "*Org tmp*")
  5660. (delq nil
  5661. (mapcar
  5662. (lambda (x)
  5663. (setq x
  5664. (if (symbolp x)
  5665. (list x)
  5666. (list (car x) (list 'quote (cdr x)))))
  5667. (if (string-match
  5668. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5669. (symbol-name (car x)))
  5670. x nil))
  5671. varlist))))
  5672. ;;;###autoload
  5673. (defun org-run-like-in-org-mode (cmd)
  5674. (org-load-modules-maybe)
  5675. (unless org-local-vars
  5676. (setq org-local-vars (org-get-local-variables)))
  5677. (eval (list 'let org-local-vars
  5678. (list 'call-interactively (list 'quote cmd)))))
  5679. ;;;; Archiving
  5680. (defun org-get-category (&optional pos)
  5681. "Get the category applying to position POS."
  5682. (get-text-property (or pos (point)) 'org-category))
  5683. (defun org-refresh-category-properties ()
  5684. "Refresh category text properties in the buffer."
  5685. (let ((def-cat (cond
  5686. ((null org-category)
  5687. (if buffer-file-name
  5688. (file-name-sans-extension
  5689. (file-name-nondirectory buffer-file-name))
  5690. "???"))
  5691. ((symbolp org-category) (symbol-name org-category))
  5692. (t org-category)))
  5693. beg end cat pos optionp)
  5694. (org-unmodified
  5695. (save-excursion
  5696. (save-restriction
  5697. (widen)
  5698. (goto-char (point-min))
  5699. (put-text-property (point) (point-max) 'org-category def-cat)
  5700. (while (re-search-forward
  5701. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5702. (setq pos (match-end 0)
  5703. optionp (equal (char-after (match-beginning 0)) ?#)
  5704. cat (org-trim (match-string 2)))
  5705. (if optionp
  5706. (setq beg (point-at-bol) end (point-max))
  5707. (org-back-to-heading t)
  5708. (setq beg (point) end (org-end-of-subtree t t)))
  5709. (put-text-property beg end 'org-category cat)
  5710. (goto-char pos)))))))
  5711. ;;;; Link Stuff
  5712. ;;; Link abbreviations
  5713. (defun org-link-expand-abbrev (link)
  5714. "Apply replacements as defined in `org-link-abbrev-alist."
  5715. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5716. (let* ((key (match-string 1 link))
  5717. (as (or (assoc key org-link-abbrev-alist-local)
  5718. (assoc key org-link-abbrev-alist)))
  5719. (tag (and (match-end 2) (match-string 3 link)))
  5720. rpl)
  5721. (if (not as)
  5722. link
  5723. (setq rpl (cdr as))
  5724. (cond
  5725. ((symbolp rpl) (funcall rpl tag))
  5726. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5727. ((string-match "%h" rpl)
  5728. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5729. (t (concat rpl tag)))))
  5730. link))
  5731. ;;; Storing and inserting links
  5732. (defvar org-insert-link-history nil
  5733. "Minibuffer history for links inserted with `org-insert-link'.")
  5734. (defvar org-stored-links nil
  5735. "Contains the links stored with `org-store-link'.")
  5736. (defvar org-store-link-plist nil
  5737. "Plist with info about the most recently link created with `org-store-link'.")
  5738. (defvar org-link-protocols nil
  5739. "Link protocols added to Org-mode using `org-add-link-type'.")
  5740. (defvar org-store-link-functions nil
  5741. "List of functions that are called to create and store a link.
  5742. Each function will be called in turn until one returns a non-nil
  5743. value. Each function should check if it is responsible for creating
  5744. this link (for example by looking at the major mode).
  5745. If not, it must exit and return nil.
  5746. If yes, it should return a non-nil value after a calling
  5747. `org-store-link-props' with a list of properties and values.
  5748. Special properties are:
  5749. :type The link prefix. like \"http\". This must be given.
  5750. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5751. This is obligatory as well.
  5752. :description Optional default description for the second pair
  5753. of brackets in an Org-mode link. The user can still change
  5754. this when inserting this link into an Org-mode buffer.
  5755. In addition to these, any additional properties can be specified
  5756. and then used in remember templates.")
  5757. (defun org-add-link-type (type &optional follow export)
  5758. "Add TYPE to the list of `org-link-types'.
  5759. Re-compute all regular expressions depending on `org-link-types'
  5760. FOLLOW and EXPORT are two functions.
  5761. FOLLOW should take the link path as the single argument and do whatever
  5762. is necessary to follow the link, for example find a file or display
  5763. a mail message.
  5764. EXPORT should format the link path for export to one of the export formats.
  5765. It should be a function accepting three arguments:
  5766. path the path of the link, the text after the prefix (like \"http:\")
  5767. desc the description of the link, if any, nil if there was no description
  5768. format the export format, a symbol like `html' or `latex'.
  5769. The function may use the FORMAT information to return different values
  5770. depending on the format. The return value will be put literally into
  5771. the exported file.
  5772. Org-mode has a built-in default for exporting links. If you are happy with
  5773. this default, there is no need to define an export function for the link
  5774. type. For a simple example of an export function, see `org-bbdb.el'."
  5775. (add-to-list 'org-link-types type t)
  5776. (org-make-link-regexps)
  5777. (if (assoc type org-link-protocols)
  5778. (setcdr (assoc type org-link-protocols) (list follow export))
  5779. (push (list type follow export) org-link-protocols)))
  5780. ;;;###autoload
  5781. (defun org-store-link (arg)
  5782. "\\<org-mode-map>Store an org-link to the current location.
  5783. This link is added to `org-stored-links' and can later be inserted
  5784. into an org-buffer with \\[org-insert-link].
  5785. For some link types, a prefix arg is interpreted:
  5786. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5787. For file links, arg negates `org-context-in-file-links'."
  5788. (interactive "P")
  5789. (org-load-modules-maybe)
  5790. (setq org-store-link-plist nil) ; reset
  5791. (let (link cpltxt desc description search txt)
  5792. (cond
  5793. ((run-hook-with-args-until-success 'org-store-link-functions)
  5794. (setq link (plist-get org-store-link-plist :link)
  5795. desc (or (plist-get org-store-link-plist :description) link)))
  5796. ((equal (buffer-name) "*Org Edit Src Example*")
  5797. (let (label gc)
  5798. (while (or (not label)
  5799. (save-excursion
  5800. (save-restriction
  5801. (widen)
  5802. (goto-char (point-min))
  5803. (re-search-forward
  5804. (regexp-quote (format org-coderef-label-format label))
  5805. nil t))))
  5806. (when label (message "Label exists already") (sit-for 2))
  5807. (setq label (read-string "Code line label: " label)))
  5808. (end-of-line 1)
  5809. (setq link (format org-coderef-label-format label))
  5810. (setq gc (- 79 (length link)))
  5811. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5812. (insert link)
  5813. (setq link (concat "(" label ")") desc nil)))
  5814. ((eq major-mode 'calendar-mode)
  5815. (let ((cd (calendar-cursor-to-date)))
  5816. (setq link
  5817. (format-time-string
  5818. (car org-time-stamp-formats)
  5819. (apply 'encode-time
  5820. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5821. nil nil nil))))
  5822. (org-store-link-props :type "calendar" :date cd)))
  5823. ((eq major-mode 'w3-mode)
  5824. (setq cpltxt (url-view-url t)
  5825. link (org-make-link cpltxt))
  5826. (org-store-link-props :type "w3" :url (url-view-url t)))
  5827. ((eq major-mode 'w3m-mode)
  5828. (setq cpltxt (or w3m-current-title w3m-current-url)
  5829. link (org-make-link w3m-current-url))
  5830. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5831. ((setq search (run-hook-with-args-until-success
  5832. 'org-create-file-search-functions))
  5833. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5834. "::" search))
  5835. (setq cpltxt (or description link)))
  5836. ((eq major-mode 'image-mode)
  5837. (setq cpltxt (concat "file:"
  5838. (abbreviate-file-name buffer-file-name))
  5839. link (org-make-link cpltxt))
  5840. (org-store-link-props :type "image" :file buffer-file-name))
  5841. ((eq major-mode 'dired-mode)
  5842. ;; link to the file in the current line
  5843. (setq cpltxt (concat "file:"
  5844. (abbreviate-file-name
  5845. (expand-file-name
  5846. (dired-get-filename nil t))))
  5847. link (org-make-link cpltxt)))
  5848. ((and buffer-file-name (org-mode-p))
  5849. (cond
  5850. ((org-in-regexp "<<\\(.*?\\)>>")
  5851. (setq cpltxt
  5852. (concat "file:"
  5853. (abbreviate-file-name buffer-file-name)
  5854. "::" (match-string 1))
  5855. link (org-make-link cpltxt)))
  5856. ((and (featurep 'org-id)
  5857. (or (eq org-link-to-org-use-id t)
  5858. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5859. (interactive-p))
  5860. (and org-link-to-org-use-id
  5861. (condition-case nil
  5862. (org-entry-get nil "ID")
  5863. (error nil)))))
  5864. ;; We can make a link using the ID.
  5865. (setq link (condition-case nil
  5866. (prog1 (org-id-store-link)
  5867. (setq desc (plist-get org-store-link-plist
  5868. :description)))
  5869. (error
  5870. ;; probably before first headline, link to file only
  5871. (concat "file:"
  5872. (abbreviate-file-name buffer-file-name))))))
  5873. (t
  5874. ;; Just link to current headline
  5875. (setq cpltxt (concat "file:"
  5876. (abbreviate-file-name buffer-file-name)))
  5877. ;; Add a context search string
  5878. (when (org-xor org-context-in-file-links arg)
  5879. (setq txt (cond
  5880. ((org-on-heading-p) nil)
  5881. ((org-region-active-p)
  5882. (buffer-substring (region-beginning) (region-end)))
  5883. (t nil)))
  5884. (when (or (null txt) (string-match "\\S-" txt))
  5885. (setq cpltxt
  5886. (concat cpltxt "::"
  5887. (condition-case nil
  5888. (org-make-org-heading-search-string txt)
  5889. (error "")))
  5890. desc "NONE")))
  5891. (if (string-match "::\\'" cpltxt)
  5892. (setq cpltxt (substring cpltxt 0 -2)))
  5893. (setq link (org-make-link cpltxt)))))
  5894. ((buffer-file-name (buffer-base-buffer))
  5895. ;; Just link to this file here.
  5896. (setq cpltxt (concat "file:"
  5897. (abbreviate-file-name
  5898. (buffer-file-name (buffer-base-buffer)))))
  5899. ;; Add a context string
  5900. (when (org-xor org-context-in-file-links arg)
  5901. (setq txt (if (org-region-active-p)
  5902. (buffer-substring (region-beginning) (region-end))
  5903. (buffer-substring (point-at-bol) (point-at-eol))))
  5904. ;; Only use search option if there is some text.
  5905. (when (string-match "\\S-" txt)
  5906. (setq cpltxt
  5907. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5908. desc "NONE")))
  5909. (setq link (org-make-link cpltxt)))
  5910. ((interactive-p)
  5911. (error "Cannot link to a buffer which is not visiting a file"))
  5912. (t (setq link nil)))
  5913. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5914. (setq link (or link cpltxt)
  5915. desc (or desc cpltxt))
  5916. (if (equal desc "NONE") (setq desc nil))
  5917. (if (and (interactive-p) link)
  5918. (progn
  5919. (setq org-stored-links
  5920. (cons (list link desc) org-stored-links))
  5921. (message "Stored: %s" (or desc link)))
  5922. (and link (org-make-link-string link desc)))))
  5923. (defun org-store-link-props (&rest plist)
  5924. "Store link properties, extract names and addresses."
  5925. (let (x adr)
  5926. (when (setq x (plist-get plist :from))
  5927. (setq adr (mail-extract-address-components x))
  5928. (setq plist (plist-put plist :fromname (car adr)))
  5929. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5930. (when (setq x (plist-get plist :to))
  5931. (setq adr (mail-extract-address-components x))
  5932. (setq plist (plist-put plist :toname (car adr)))
  5933. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5934. (let ((from (plist-get plist :from))
  5935. (to (plist-get plist :to)))
  5936. (when (and from to org-from-is-user-regexp)
  5937. (setq plist
  5938. (plist-put plist :fromto
  5939. (if (string-match org-from-is-user-regexp from)
  5940. (concat "to %t")
  5941. (concat "from %f"))))))
  5942. (setq org-store-link-plist plist))
  5943. (defun org-add-link-props (&rest plist)
  5944. "Add these properties to the link property list."
  5945. (let (key value)
  5946. (while plist
  5947. (setq key (pop plist) value (pop plist))
  5948. (setq org-store-link-plist
  5949. (plist-put org-store-link-plist key value)))))
  5950. (defun org-email-link-description (&optional fmt)
  5951. "Return the description part of an email link.
  5952. This takes information from `org-store-link-plist' and formats it
  5953. according to FMT (default from `org-email-link-description-format')."
  5954. (setq fmt (or fmt org-email-link-description-format))
  5955. (let* ((p org-store-link-plist)
  5956. (to (plist-get p :toaddress))
  5957. (from (plist-get p :fromaddress))
  5958. (table
  5959. (list
  5960. (cons "%c" (plist-get p :fromto))
  5961. (cons "%F" (plist-get p :from))
  5962. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5963. (cons "%T" (plist-get p :to))
  5964. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5965. (cons "%s" (plist-get p :subject))
  5966. (cons "%m" (plist-get p :message-id)))))
  5967. (when (string-match "%c" fmt)
  5968. ;; Check if the user wrote this message
  5969. (if (and org-from-is-user-regexp from to
  5970. (save-match-data (string-match org-from-is-user-regexp from)))
  5971. (setq fmt (replace-match "to %t" t t fmt))
  5972. (setq fmt (replace-match "from %f" t t fmt))))
  5973. (org-replace-escapes fmt table)))
  5974. (defun org-make-org-heading-search-string (&optional string heading)
  5975. "Make search string for STRING or current headline."
  5976. (interactive)
  5977. (let ((s (or string (org-get-heading))))
  5978. (unless (and string (not heading))
  5979. ;; We are using a headline, clean up garbage in there.
  5980. (if (string-match org-todo-regexp s)
  5981. (setq s (replace-match "" t t s)))
  5982. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5983. (setq s (replace-match "" t t s)))
  5984. (setq s (org-trim s))
  5985. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5986. org-comment-string "\\)") s)
  5987. (setq s (replace-match "" t t s)))
  5988. (while (string-match org-ts-regexp s)
  5989. (setq s (replace-match "" t t s))))
  5990. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5991. (setq s (replace-match " " t t s)))
  5992. (or string (setq s (concat "*" s))) ; Add * for headlines
  5993. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5994. (defun org-make-link (&rest strings)
  5995. "Concatenate STRINGS."
  5996. (apply 'concat strings))
  5997. (defun org-make-link-string (link &optional description)
  5998. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5999. (unless (string-match "\\S-" link)
  6000. (error "Empty link"))
  6001. (when (stringp description)
  6002. ;; Remove brackets from the description, they are fatal.
  6003. (while (string-match "\\[" description)
  6004. (setq description (replace-match "{" t t description)))
  6005. (while (string-match "\\]" description)
  6006. (setq description (replace-match "}" t t description))))
  6007. (when (equal (org-link-escape link) description)
  6008. ;; No description needed, it is identical
  6009. (setq description nil))
  6010. (when (and (not description)
  6011. (not (equal link (org-link-escape link))))
  6012. (setq description (org-extract-attributes link)))
  6013. (concat "[[" (org-link-escape link) "]"
  6014. (if description (concat "[" description "]") "")
  6015. "]"))
  6016. (defconst org-link-escape-chars
  6017. '((?\ . "%20")
  6018. (?\[ . "%5B")
  6019. (?\] . "%5D")
  6020. (?\340 . "%E0") ; `a
  6021. (?\342 . "%E2") ; ^a
  6022. (?\347 . "%E7") ; ,c
  6023. (?\350 . "%E8") ; `e
  6024. (?\351 . "%E9") ; 'e
  6025. (?\352 . "%EA") ; ^e
  6026. (?\356 . "%EE") ; ^i
  6027. (?\364 . "%F4") ; ^o
  6028. (?\371 . "%F9") ; `u
  6029. (?\373 . "%FB") ; ^u
  6030. (?\; . "%3B")
  6031. (?? . "%3F")
  6032. (?= . "%3D")
  6033. (?+ . "%2B")
  6034. )
  6035. "Association list of escapes for some characters problematic in links.
  6036. This is the list that is used for internal purposes.")
  6037. (defconst org-link-escape-chars-browser
  6038. '((?\ . "%20")) ; 32 for the SPC char
  6039. "Association list of escapes for some characters problematic in links.
  6040. This is the list that is used before handing over to the browser.")
  6041. (defun org-link-escape (text &optional table)
  6042. "Escape characters in TEXT that are problematic for links."
  6043. (setq table (or table org-link-escape-chars))
  6044. (when text
  6045. (let ((re (mapconcat (lambda (x) (regexp-quote
  6046. (char-to-string (car x))))
  6047. table "\\|")))
  6048. (while (string-match re text)
  6049. (setq text
  6050. (replace-match
  6051. (cdr (assoc (string-to-char (match-string 0 text))
  6052. table))
  6053. t t text)))
  6054. text)))
  6055. (defun org-link-unescape (text &optional table)
  6056. "Reverse the action of `org-link-escape'."
  6057. (setq table (or table org-link-escape-chars))
  6058. (when text
  6059. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6060. table "\\|")))
  6061. (while (string-match re text)
  6062. (setq text
  6063. (replace-match
  6064. (char-to-string (car (rassoc (match-string 0 text) table)))
  6065. t t text)))
  6066. text)))
  6067. (defun org-xor (a b)
  6068. "Exclusive or."
  6069. (if a (not b) b))
  6070. (defun org-fixup-message-id-for-http (s)
  6071. "Replace special characters in a message id, so it can be used in an http query."
  6072. (while (string-match "<" s)
  6073. (setq s (replace-match "%3C" t t s)))
  6074. (while (string-match ">" s)
  6075. (setq s (replace-match "%3E" t t s)))
  6076. (while (string-match "@" s)
  6077. (setq s (replace-match "%40" t t s)))
  6078. s)
  6079. ;;;###autoload
  6080. (defun org-insert-link-global ()
  6081. "Insert a link like Org-mode does.
  6082. This command can be called in any mode to insert a link in Org-mode syntax."
  6083. (interactive)
  6084. (org-load-modules-maybe)
  6085. (org-run-like-in-org-mode 'org-insert-link))
  6086. (defun org-insert-link (&optional complete-file link-location)
  6087. "Insert a link. At the prompt, enter the link.
  6088. Completion can be used to insert any of the link protocol prefixes like
  6089. http or ftp in use.
  6090. The history can be used to select a link previously stored with
  6091. `org-store-link'. When the empty string is entered (i.e. if you just
  6092. press RET at the prompt), the link defaults to the most recently
  6093. stored link. As SPC triggers completion in the minibuffer, you need to
  6094. use M-SPC or C-q SPC to force the insertion of a space character.
  6095. You will also be prompted for a description, and if one is given, it will
  6096. be displayed in the buffer instead of the link.
  6097. If there is already a link at point, this command will allow you to edit link
  6098. and description parts.
  6099. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6100. be selected using completion. The path to the file will be relative to the
  6101. current directory if the file is in the current directory or a subdirectory.
  6102. Otherwise, the link will be the absolute path as completed in the minibuffer
  6103. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6104. option `org-link-file-path-type'.
  6105. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6106. the current directory or below.
  6107. With three \\[universal-argument] prefixes, negate the meaning of
  6108. `org-keep-stored-link-after-insertion'.
  6109. If `org-make-link-description-function' is non-nil, this function will be
  6110. called with the link target, and the result will be the default
  6111. link description.
  6112. If the LINK-LOCATION parameter is non-nil, this value will be
  6113. used as the link location instead of reading one interactively."
  6114. (interactive "P")
  6115. (let* ((wcf (current-window-configuration))
  6116. (region (if (org-region-active-p)
  6117. (buffer-substring (region-beginning) (region-end))))
  6118. (remove (and region (list (region-beginning) (region-end))))
  6119. (desc region)
  6120. tmphist ; byte-compile incorrectly complains about this
  6121. (link link-location)
  6122. entry file)
  6123. (cond
  6124. (link-location) ; specified by arg, just use it.
  6125. ((org-in-regexp org-bracket-link-regexp 1)
  6126. ;; We do have a link at point, and we are going to edit it.
  6127. (setq remove (list (match-beginning 0) (match-end 0)))
  6128. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6129. (setq link (read-string "Link: "
  6130. (org-link-unescape
  6131. (org-match-string-no-properties 1)))))
  6132. ((or (org-in-regexp org-angle-link-re)
  6133. (org-in-regexp org-plain-link-re))
  6134. ;; Convert to bracket link
  6135. (setq remove (list (match-beginning 0) (match-end 0))
  6136. link (read-string "Link: "
  6137. (org-remove-angle-brackets (match-string 0)))))
  6138. ((member complete-file '((4) (16)))
  6139. ;; Completing read for file names.
  6140. (setq file (read-file-name "File: "))
  6141. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6142. (pwd1 (file-name-as-directory (abbreviate-file-name
  6143. (expand-file-name ".")))))
  6144. (cond
  6145. ((equal complete-file '(16))
  6146. (setq link (org-make-link
  6147. "file:"
  6148. (abbreviate-file-name (expand-file-name file)))))
  6149. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6150. (setq link (org-make-link "file:" (match-string 1 file))))
  6151. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6152. (expand-file-name file))
  6153. (setq link (org-make-link
  6154. "file:" (match-string 1 (expand-file-name file)))))
  6155. (t (setq link (org-make-link "file:" file))))))
  6156. (t
  6157. ;; Read link, with completion for stored links.
  6158. (with-output-to-temp-buffer "*Org Links*"
  6159. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6160. (when org-stored-links
  6161. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6162. (princ (mapconcat
  6163. (lambda (x)
  6164. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6165. (reverse org-stored-links) "\n"))))
  6166. (let ((cw (selected-window)))
  6167. (select-window (get-buffer-window "*Org Links*"))
  6168. (setq truncate-lines t)
  6169. (org-fit-window-to-buffer)
  6170. (select-window cw))
  6171. ;; Fake a link history, containing the stored links.
  6172. (setq tmphist (append (mapcar 'car org-stored-links)
  6173. org-insert-link-history))
  6174. (unwind-protect
  6175. (setq link
  6176. (let ((org-completion-use-ido nil))
  6177. (org-completing-read
  6178. "Link: "
  6179. (append
  6180. (mapcar (lambda (x) (list (concat (car x) ":")))
  6181. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6182. (mapcar (lambda (x) (list (concat x ":")))
  6183. org-link-types))
  6184. nil nil nil
  6185. 'tmphist
  6186. (or (car (car org-stored-links))))))
  6187. (set-window-configuration wcf)
  6188. (kill-buffer "*Org Links*"))
  6189. (setq entry (assoc link org-stored-links))
  6190. (or entry (push link org-insert-link-history))
  6191. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6192. (not org-keep-stored-link-after-insertion))
  6193. (setq org-stored-links (delq (assoc link org-stored-links)
  6194. org-stored-links)))
  6195. (setq desc (or desc (nth 1 entry)))))
  6196. (if (string-match org-plain-link-re link)
  6197. ;; URL-like link, normalize the use of angular brackets.
  6198. (setq link (org-make-link (org-remove-angle-brackets link))))
  6199. ;; Check if we are linking to the current file with a search option
  6200. ;; If yes, simplify the link by using only the search option.
  6201. (when (and buffer-file-name
  6202. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6203. (let* ((path (match-string 1 link))
  6204. (case-fold-search nil)
  6205. (search (match-string 2 link)))
  6206. (save-match-data
  6207. (if (equal (file-truename buffer-file-name) (file-truename path))
  6208. ;; We are linking to this same file, with a search option
  6209. (setq link search)))))
  6210. ;; Check if we can/should use a relative path. If yes, simplify the link
  6211. (when (string-match "^file:\\(.*\\)" link)
  6212. (let* ((path (match-string 1 link))
  6213. (origpath path)
  6214. (case-fold-search nil))
  6215. (cond
  6216. ((or (eq org-link-file-path-type 'absolute)
  6217. (equal complete-file '(16)))
  6218. (setq path (abbreviate-file-name (expand-file-name path))))
  6219. ((eq org-link-file-path-type 'noabbrev)
  6220. (setq path (expand-file-name path)))
  6221. ((eq org-link-file-path-type 'relative)
  6222. (setq path (file-relative-name path)))
  6223. (t
  6224. (save-match-data
  6225. (if (string-match (concat "^" (regexp-quote
  6226. (file-name-as-directory
  6227. (expand-file-name "."))))
  6228. (expand-file-name path))
  6229. ;; We are linking a file with relative path name.
  6230. (setq path (substring (expand-file-name path)
  6231. (match-end 0)))
  6232. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6233. (setq link (concat "file:" path))
  6234. (if (equal desc origpath)
  6235. (setq desc path))))
  6236. (if org-make-link-description-function
  6237. (setq desc (funcall org-make-link-description-function link desc)))
  6238. (setq desc (read-string "Description: " desc))
  6239. (unless (string-match "\\S-" desc) (setq desc nil))
  6240. (if remove (apply 'delete-region remove))
  6241. (insert (org-make-link-string link desc))))
  6242. (defun org-completing-read (&rest args)
  6243. "Completing-read with SPACE being a normal character."
  6244. (let ((minibuffer-local-completion-map
  6245. (copy-keymap minibuffer-local-completion-map)))
  6246. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6247. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6248. (apply 'org-ido-completing-read args)))
  6249. (defun org-ido-completing-read (&rest args)
  6250. "Completing-read using `ido-mode' speedups if available"
  6251. (if (and org-completion-use-ido
  6252. (fboundp 'ido-completing-read)
  6253. (boundp 'ido-mode) ido-mode
  6254. (listp (second args)))
  6255. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6256. (apply 'completing-read args)))
  6257. (defun org-extract-attributes (s)
  6258. "Extract the attributes cookie from a string and set as text property."
  6259. (let (a attr (start 0) key value)
  6260. (save-match-data
  6261. (when (string-match "{{\\([^}]+\\)}}$" s)
  6262. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6263. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6264. (setq key (match-string 1 a) value (match-string 2 a)
  6265. start (match-end 0)
  6266. attr (plist-put attr (intern key) value))))
  6267. (org-add-props s nil 'org-attr attr))
  6268. s))
  6269. (defun org-attributes-to-string (plist)
  6270. "Format a property list into an HTML attribute list."
  6271. (let ((s "") key value)
  6272. (while plist
  6273. (setq key (pop plist) value (pop plist))
  6274. (and value
  6275. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6276. s))
  6277. ;;; Opening/following a link
  6278. (defvar org-link-search-failed nil)
  6279. (defun org-next-link ()
  6280. "Move forward to the next link.
  6281. If the link is in hidden text, expose it."
  6282. (interactive)
  6283. (when (and org-link-search-failed (eq this-command last-command))
  6284. (goto-char (point-min))
  6285. (message "Link search wrapped back to beginning of buffer"))
  6286. (setq org-link-search-failed nil)
  6287. (let* ((pos (point))
  6288. (ct (org-context))
  6289. (a (assoc :link ct)))
  6290. (if a (goto-char (nth 2 a)))
  6291. (if (re-search-forward org-any-link-re nil t)
  6292. (progn
  6293. (goto-char (match-beginning 0))
  6294. (if (org-invisible-p) (org-show-context)))
  6295. (goto-char pos)
  6296. (setq org-link-search-failed t)
  6297. (error "No further link found"))))
  6298. (defun org-previous-link ()
  6299. "Move backward to the previous link.
  6300. If the link is in hidden text, expose it."
  6301. (interactive)
  6302. (when (and org-link-search-failed (eq this-command last-command))
  6303. (goto-char (point-max))
  6304. (message "Link search wrapped back to end of buffer"))
  6305. (setq org-link-search-failed nil)
  6306. (let* ((pos (point))
  6307. (ct (org-context))
  6308. (a (assoc :link ct)))
  6309. (if a (goto-char (nth 1 a)))
  6310. (if (re-search-backward org-any-link-re nil t)
  6311. (progn
  6312. (goto-char (match-beginning 0))
  6313. (if (org-invisible-p) (org-show-context)))
  6314. (goto-char pos)
  6315. (setq org-link-search-failed t)
  6316. (error "No further link found"))))
  6317. (defun org-translate-link (s)
  6318. "Translate a link string if a translation function has been defined."
  6319. (if (and org-link-translation-function
  6320. (fboundp org-link-translation-function)
  6321. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6322. (progn
  6323. (setq s (funcall org-link-translation-function
  6324. (match-string 1) (match-string 2)))
  6325. (concat (car s) ":" (cdr s)))
  6326. s))
  6327. (defun org-translate-link-from-planner (type path)
  6328. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6329. This is still an experimental function, your mileage may vary."
  6330. (cond
  6331. ((member type '("http" "https" "news" "ftp"))
  6332. ;; standard Internet links are the same.
  6333. nil)
  6334. ((and (equal type "irc") (string-match "^//" path))
  6335. ;; Planner has two / at the beginning of an irc link, we have 1.
  6336. ;; We should have zero, actually....
  6337. (setq path (substring path 1)))
  6338. ((and (equal type "lisp") (string-match "^/" path))
  6339. ;; Planner has a slash, we do not.
  6340. (setq type "elisp" path (substring path 1)))
  6341. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6342. ;; A typical message link. Planner has the id after the fina slash,
  6343. ;; we separate it with a hash mark
  6344. (setq path (concat (match-string 1 path) "#"
  6345. (org-remove-angle-brackets (match-string 2 path)))))
  6346. )
  6347. (cons type path))
  6348. (defun org-find-file-at-mouse (ev)
  6349. "Open file link or URL at mouse."
  6350. (interactive "e")
  6351. (mouse-set-point ev)
  6352. (org-open-at-point 'in-emacs))
  6353. (defun org-open-at-mouse (ev)
  6354. "Open file link or URL at mouse."
  6355. (interactive "e")
  6356. (mouse-set-point ev)
  6357. (if (eq major-mode 'org-agenda-mode)
  6358. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6359. (org-open-at-point))
  6360. (defvar org-window-config-before-follow-link nil
  6361. "The window configuration before following a link.
  6362. This is saved in case the need arises to restore it.")
  6363. (defvar org-open-link-marker (make-marker)
  6364. "Marker pointing to the location where `org-open-at-point; was called.")
  6365. ;;;###autoload
  6366. (defun org-open-at-point-global ()
  6367. "Follow a link like Org-mode does.
  6368. This command can be called in any mode to follow a link that has
  6369. Org-mode syntax."
  6370. (interactive)
  6371. (org-run-like-in-org-mode 'org-open-at-point))
  6372. ;;;###autoload
  6373. (defun org-open-link-from-string (s &optional arg)
  6374. "Open a link in the string S, as if it was in Org-mode."
  6375. (interactive "sLink: \nP")
  6376. (with-temp-buffer
  6377. (let ((org-inhibit-startup t))
  6378. (org-mode)
  6379. (insert s)
  6380. (goto-char (point-min))
  6381. (org-open-at-point arg))))
  6382. (defun org-open-at-point (&optional in-emacs)
  6383. "Open link at or after point.
  6384. If there is no link at point, this function will search forward up to
  6385. the end of the current subtree.
  6386. Normally, files will be opened by an appropriate application. If the
  6387. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6388. With a double prefix argument, try to open outside of Emacs, in the
  6389. application the system uses for this file type."
  6390. (interactive "P")
  6391. (org-load-modules-maybe)
  6392. (move-marker org-open-link-marker (point))
  6393. (setq org-window-config-before-follow-link (current-window-configuration))
  6394. (org-remove-occur-highlights nil nil t)
  6395. (cond
  6396. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6397. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6398. (org-footnote-action))
  6399. (t
  6400. (let (type path link line search (pos (point)))
  6401. (catch 'match
  6402. (save-excursion
  6403. (skip-chars-forward "^]\n\r")
  6404. (when (org-in-regexp org-bracket-link-regexp)
  6405. (setq link (org-extract-attributes
  6406. (org-link-unescape (org-match-string-no-properties 1))))
  6407. (while (string-match " *\n *" link)
  6408. (setq link (replace-match " " t t link)))
  6409. (setq link (org-link-expand-abbrev link))
  6410. (cond
  6411. ((or (file-name-absolute-p link)
  6412. (string-match "^\\.\\.?/" link))
  6413. (setq type "file" path link))
  6414. ((string-match org-link-re-with-space3 link)
  6415. (setq type (match-string 1 link) path (match-string 2 link)))
  6416. (t (setq type "thisfile" path link)))
  6417. (throw 'match t)))
  6418. (when (get-text-property (point) 'org-linked-text)
  6419. (setq type "thisfile"
  6420. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6421. (1+ (point)) (point))
  6422. path (buffer-substring
  6423. (previous-single-property-change pos 'org-linked-text)
  6424. (next-single-property-change pos 'org-linked-text)))
  6425. (throw 'match t))
  6426. (save-excursion
  6427. (when (or (org-in-regexp org-angle-link-re)
  6428. (org-in-regexp org-plain-link-re))
  6429. (setq type (match-string 1) path (match-string 2))
  6430. (throw 'match t)))
  6431. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6432. (setq type "tree-match"
  6433. path (match-string 1))
  6434. (throw 'match t))
  6435. (save-excursion
  6436. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6437. (setq type "tags"
  6438. path (match-string 1))
  6439. (while (string-match ":" path)
  6440. (setq path (replace-match "+" t t path)))
  6441. (throw 'match t))))
  6442. (unless path
  6443. (error "No link found"))
  6444. ;; Remove any trailing spaces in path
  6445. (if (string-match " +\\'" path)
  6446. (setq path (replace-match "" t t path)))
  6447. (if (and org-link-translation-function
  6448. (fboundp org-link-translation-function))
  6449. ;; Check if we need to translate the link
  6450. (let ((tmp (funcall org-link-translation-function type path)))
  6451. (setq type (car tmp) path (cdr tmp))))
  6452. (cond
  6453. ((assoc type org-link-protocols)
  6454. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6455. ((equal type "mailto")
  6456. (let ((cmd (car org-link-mailto-program))
  6457. (args (cdr org-link-mailto-program)) args1
  6458. (address path) (subject "") a)
  6459. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6460. (setq address (match-string 1 path)
  6461. subject (org-link-escape (match-string 2 path))))
  6462. (while args
  6463. (cond
  6464. ((not (stringp (car args))) (push (pop args) args1))
  6465. (t (setq a (pop args))
  6466. (if (string-match "%a" a)
  6467. (setq a (replace-match address t t a)))
  6468. (if (string-match "%s" a)
  6469. (setq a (replace-match subject t t a)))
  6470. (push a args1))))
  6471. (apply cmd (nreverse args1))))
  6472. ((member type '("http" "https" "ftp" "news"))
  6473. (browse-url (concat type ":" (org-link-escape
  6474. path org-link-escape-chars-browser))))
  6475. ((member type '("message"))
  6476. (browse-url (concat type ":" path)))
  6477. ((string= type "tags")
  6478. (org-tags-view in-emacs path))
  6479. ((string= type "thisfile")
  6480. (if in-emacs
  6481. (switch-to-buffer-other-window
  6482. (org-get-buffer-for-internal-link (current-buffer)))
  6483. (org-mark-ring-push))
  6484. (let ((cmd `(org-link-search
  6485. ,path
  6486. ,(cond ((equal in-emacs '(4)) 'occur)
  6487. ((equal in-emacs '(16)) 'org-occur)
  6488. (t nil))
  6489. ,pos)))
  6490. (condition-case nil (eval cmd)
  6491. (error (progn (widen) (eval cmd))))))
  6492. ((string= type "tree-match")
  6493. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6494. ((string= type "file")
  6495. (if (string-match "::\\([0-9]+\\)\\'" path)
  6496. (setq line (string-to-number (match-string 1 path))
  6497. path (substring path 0 (match-beginning 0)))
  6498. (if (string-match "::\\(.+\\)\\'" path)
  6499. (setq search (match-string 1 path)
  6500. path (substring path 0 (match-beginning 0)))))
  6501. (if (string-match "[*?{]" (file-name-nondirectory path))
  6502. (dired path)
  6503. (org-open-file path in-emacs line search)))
  6504. ((string= type "news")
  6505. (require 'org-gnus)
  6506. (org-gnus-follow-link path))
  6507. ((string= type "shell")
  6508. (let ((cmd path))
  6509. (if (or (not org-confirm-shell-link-function)
  6510. (funcall org-confirm-shell-link-function
  6511. (format "Execute \"%s\" in shell? "
  6512. (org-add-props cmd nil
  6513. 'face 'org-warning))))
  6514. (progn
  6515. (message "Executing %s" cmd)
  6516. (shell-command cmd))
  6517. (error "Abort"))))
  6518. ((string= type "elisp")
  6519. (let ((cmd path))
  6520. (if (or (not org-confirm-elisp-link-function)
  6521. (funcall org-confirm-elisp-link-function
  6522. (format "Execute \"%s\" as elisp? "
  6523. (org-add-props cmd nil
  6524. 'face 'org-warning))))
  6525. (message "%s => %s" cmd
  6526. (if (equal (string-to-char cmd) ?\()
  6527. (eval (read cmd))
  6528. (call-interactively (read cmd))))
  6529. (error "Abort"))))
  6530. (t
  6531. (browse-url-at-point))))))
  6532. (move-marker org-open-link-marker nil)
  6533. (run-hook-with-args 'org-follow-link-hook))
  6534. ;;;; Time estimates
  6535. (defun org-get-effort (&optional pom)
  6536. "Get the effort estimate for the current entry."
  6537. (org-entry-get pom org-effort-property))
  6538. ;;; File search
  6539. (defvar org-create-file-search-functions nil
  6540. "List of functions to construct the right search string for a file link.
  6541. These functions are called in turn with point at the location to
  6542. which the link should point.
  6543. A function in the hook should first test if it would like to
  6544. handle this file type, for example by checking the major-mode or
  6545. the file extension. If it decides not to handle this file, it
  6546. should just return nil to give other functions a chance. If it
  6547. does handle the file, it must return the search string to be used
  6548. when following the link. The search string will be part of the
  6549. file link, given after a double colon, and `org-open-at-point'
  6550. will automatically search for it. If special measures must be
  6551. taken to make the search successful, another function should be
  6552. added to the companion hook `org-execute-file-search-functions',
  6553. which see.
  6554. A function in this hook may also use `setq' to set the variable
  6555. `description' to provide a suggestion for the descriptive text to
  6556. be used for this link when it gets inserted into an Org-mode
  6557. buffer with \\[org-insert-link].")
  6558. (defvar org-execute-file-search-functions nil
  6559. "List of functions to execute a file search triggered by a link.
  6560. Functions added to this hook must accept a single argument, the
  6561. search string that was part of the file link, the part after the
  6562. double colon. The function must first check if it would like to
  6563. handle this search, for example by checking the major-mode or the
  6564. file extension. If it decides not to handle this search, it
  6565. should just return nil to give other functions a chance. If it
  6566. does handle the search, it must return a non-nil value to keep
  6567. other functions from trying.
  6568. Each function can access the current prefix argument through the
  6569. variable `current-prefix-argument'. Note that a single prefix is
  6570. used to force opening a link in Emacs, so it may be good to only
  6571. use a numeric or double prefix to guide the search function.
  6572. In case this is needed, a function in this hook can also restore
  6573. the window configuration before `org-open-at-point' was called using:
  6574. (set-window-configuration org-window-config-before-follow-link)")
  6575. (defun org-link-search (s &optional type avoid-pos)
  6576. "Search for a link search option.
  6577. If S is surrounded by forward slashes, it is interpreted as a
  6578. regular expression. In org-mode files, this will create an `org-occur'
  6579. sparse tree. In ordinary files, `occur' will be used to list matches.
  6580. If the current buffer is in `dired-mode', grep will be used to search
  6581. in all files. If AVOID-POS is given, ignore matches near that position."
  6582. (let ((case-fold-search t)
  6583. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6584. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6585. (append '(("") (" ") ("\t") ("\n"))
  6586. org-emphasis-alist)
  6587. "\\|") "\\)"))
  6588. (pos (point))
  6589. (pre nil) (post nil)
  6590. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6591. (cond
  6592. ;; First check if there are any special
  6593. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6594. ;; Now try the builtin stuff
  6595. ((save-excursion
  6596. (goto-char (point-min))
  6597. (and
  6598. (re-search-forward
  6599. (concat "<<" (regexp-quote s0) ">>") nil t)
  6600. (setq type 'dedicated
  6601. pos (match-beginning 0))))
  6602. ;; There is an exact target for this
  6603. (goto-char pos))
  6604. ((and (string-match "^(\\(.*\\))$" s0)
  6605. (save-excursion
  6606. (goto-char (point-min))
  6607. (and
  6608. (re-search-forward
  6609. (concat "[^[]" (regexp-quote
  6610. (format org-coderef-label-format
  6611. (match-string 1 s0))))
  6612. nil t)
  6613. (setq type 'dedicated
  6614. pos (1+ (match-beginning 0))))))
  6615. ;; There is a coderef target for this
  6616. (goto-char pos))
  6617. ((string-match "^/\\(.*\\)/$" s)
  6618. ;; A regular expression
  6619. (cond
  6620. ((org-mode-p)
  6621. (org-occur (match-string 1 s)))
  6622. ;;((eq major-mode 'dired-mode)
  6623. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6624. (t (org-do-occur (match-string 1 s)))))
  6625. (t
  6626. ;; A normal search strings
  6627. (when (equal (string-to-char s) ?*)
  6628. ;; Anchor on headlines, post may include tags.
  6629. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6630. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6631. s (substring s 1)))
  6632. (remove-text-properties
  6633. 0 (length s)
  6634. '(face nil mouse-face nil keymap nil fontified nil) s)
  6635. ;; Make a series of regular expressions to find a match
  6636. (setq words (org-split-string s "[ \n\r\t]+")
  6637. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6638. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6639. "\\)" markers)
  6640. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6641. re2a (concat "[ \t\r\n]" re2a_)
  6642. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6643. re4 (concat "[^a-zA-Z_]" re4_)
  6644. re1 (concat pre re2 post)
  6645. re3 (concat pre (if pre re4_ re4) post)
  6646. re5 (concat pre ".*" re4)
  6647. re2 (concat pre re2)
  6648. re2a (concat pre (if pre re2a_ re2a))
  6649. re4 (concat pre (if pre re4_ re4))
  6650. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6651. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6652. re5 "\\)"
  6653. ))
  6654. (cond
  6655. ((eq type 'org-occur) (org-occur reall))
  6656. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6657. (t (goto-char (point-min))
  6658. (setq type 'fuzzy)
  6659. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6660. (org-search-not-self 1 re1 nil t)
  6661. (org-search-not-self 1 re2 nil t)
  6662. (org-search-not-self 1 re2a nil t)
  6663. (org-search-not-self 1 re3 nil t)
  6664. (org-search-not-self 1 re4 nil t)
  6665. (org-search-not-self 1 re5 nil t)
  6666. )
  6667. (goto-char (match-beginning 1))
  6668. (goto-char pos)
  6669. (error "No match")))))
  6670. (t
  6671. ;; Normal string-search
  6672. (goto-char (point-min))
  6673. (if (search-forward s nil t)
  6674. (goto-char (match-beginning 0))
  6675. (error "No match"))))
  6676. (and (org-mode-p) (org-show-context 'link-search))
  6677. type))
  6678. (defun org-search-not-self (group &rest args)
  6679. "Execute `re-search-forward', but only accept matches that do not
  6680. enclose the position of `org-open-link-marker'."
  6681. (let ((m org-open-link-marker))
  6682. (catch 'exit
  6683. (while (apply 're-search-forward args)
  6684. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6685. (goto-char (match-end group))
  6686. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6687. (> (match-beginning 0) (marker-position m))
  6688. (< (match-end 0) (marker-position m)))
  6689. (save-match-data
  6690. (or (not (org-in-regexp
  6691. org-bracket-link-analytic-regexp 1))
  6692. (not (match-end 4)) ; no description
  6693. (and (<= (match-beginning 4) (point))
  6694. (>= (match-end 4) (point))))))
  6695. (throw 'exit (point))))))))
  6696. (defun org-get-buffer-for-internal-link (buffer)
  6697. "Return a buffer to be used for displaying the link target of internal links."
  6698. (cond
  6699. ((not org-display-internal-link-with-indirect-buffer)
  6700. buffer)
  6701. ((string-match "(Clone)$" (buffer-name buffer))
  6702. (message "Buffer is already a clone, not making another one")
  6703. ;; we also do not modify visibility in this case
  6704. buffer)
  6705. (t ; make a new indirect buffer for displaying the link
  6706. (let* ((bn (buffer-name buffer))
  6707. (ibn (concat bn "(Clone)"))
  6708. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6709. (with-current-buffer ib (org-overview))
  6710. ib))))
  6711. (defun org-do-occur (regexp &optional cleanup)
  6712. "Call the Emacs command `occur'.
  6713. If CLEANUP is non-nil, remove the printout of the regular expression
  6714. in the *Occur* buffer. This is useful if the regex is long and not useful
  6715. to read."
  6716. (occur regexp)
  6717. (when cleanup
  6718. (let ((cwin (selected-window)) win beg end)
  6719. (when (setq win (get-buffer-window "*Occur*"))
  6720. (select-window win))
  6721. (goto-char (point-min))
  6722. (when (re-search-forward "match[a-z]+" nil t)
  6723. (setq beg (match-end 0))
  6724. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6725. (setq end (1- (match-beginning 0)))))
  6726. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6727. (goto-char (point-min))
  6728. (select-window cwin))))
  6729. ;;; The mark ring for links jumps
  6730. (defvar org-mark-ring nil
  6731. "Mark ring for positions before jumps in Org-mode.")
  6732. (defvar org-mark-ring-last-goto nil
  6733. "Last position in the mark ring used to go back.")
  6734. ;; Fill and close the ring
  6735. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6736. (loop for i from 1 to org-mark-ring-length do
  6737. (push (make-marker) org-mark-ring))
  6738. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6739. org-mark-ring)
  6740. (defun org-mark-ring-push (&optional pos buffer)
  6741. "Put the current position or POS into the mark ring and rotate it."
  6742. (interactive)
  6743. (setq pos (or pos (point)))
  6744. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6745. (move-marker (car org-mark-ring)
  6746. (or pos (point))
  6747. (or buffer (current-buffer)))
  6748. (message "%s"
  6749. (substitute-command-keys
  6750. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6751. (defun org-mark-ring-goto (&optional n)
  6752. "Jump to the previous position in the mark ring.
  6753. With prefix arg N, jump back that many stored positions. When
  6754. called several times in succession, walk through the entire ring.
  6755. Org-mode commands jumping to a different position in the current file,
  6756. or to another Org-mode file, automatically push the old position
  6757. onto the ring."
  6758. (interactive "p")
  6759. (let (p m)
  6760. (if (eq last-command this-command)
  6761. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6762. (setq p org-mark-ring))
  6763. (setq org-mark-ring-last-goto p)
  6764. (setq m (car p))
  6765. (switch-to-buffer (marker-buffer m))
  6766. (goto-char m)
  6767. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6768. (defun org-remove-angle-brackets (s)
  6769. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6770. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6771. s)
  6772. (defun org-add-angle-brackets (s)
  6773. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6774. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6775. s)
  6776. (defun org-remove-double-quotes (s)
  6777. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6778. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6779. s)
  6780. ;;; Following specific links
  6781. (defun org-follow-timestamp-link ()
  6782. (cond
  6783. ((org-at-date-range-p t)
  6784. (let ((org-agenda-start-on-weekday)
  6785. (t1 (match-string 1))
  6786. (t2 (match-string 2)))
  6787. (setq t1 (time-to-days (org-time-string-to-time t1))
  6788. t2 (time-to-days (org-time-string-to-time t2)))
  6789. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6790. ((org-at-timestamp-p t)
  6791. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6792. (substring (match-string 1) 0 10)))
  6793. 1))
  6794. (t (error "This should not happen"))))
  6795. ;;; Following file links
  6796. (defvar org-wait nil)
  6797. (defun org-open-file (path &optional in-emacs line search)
  6798. "Open the file at PATH.
  6799. First, this expands any special file name abbreviations. Then the
  6800. configuration variable `org-file-apps' is checked if it contains an
  6801. entry for this file type, and if yes, the corresponding command is launched.
  6802. If no application is found, Emacs simply visits the file.
  6803. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6804. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6805. and o use an external application to visit the file.
  6806. Optional LINE specifies a line to go to, optional SEARCH a string to
  6807. search for. If LINE or SEARCH is given, the file will always be
  6808. opened in Emacs.
  6809. If the file does not exist, an error is thrown."
  6810. (setq in-emacs (or in-emacs line search))
  6811. (let* ((file (if (equal path "")
  6812. buffer-file-name
  6813. (substitute-in-file-name (expand-file-name path))))
  6814. (apps (append org-file-apps (org-default-apps)))
  6815. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6816. (dirp (if remp nil (file-directory-p file)))
  6817. (file (if (and dirp org-open-directory-means-index-dot-org)
  6818. (concat (file-name-as-directory file) "index.org")
  6819. file))
  6820. (a-m-a-p (assq 'auto-mode apps))
  6821. (dfile (downcase file))
  6822. (old-buffer (current-buffer))
  6823. (old-pos (point))
  6824. (old-mode major-mode)
  6825. ext cmd)
  6826. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6827. (setq ext (match-string 1 dfile))
  6828. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6829. (setq ext (match-string 1 dfile))))
  6830. (cond
  6831. ((equal in-emacs '(16))
  6832. (setq cmd (cdr (assoc 'system apps))))
  6833. (in-emacs (setq cmd 'emacs))
  6834. (t
  6835. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6836. (and dirp (cdr (assoc 'directory apps)))
  6837. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6838. 'string-match)
  6839. (cdr (assoc ext apps))
  6840. (cdr (assoc t apps))))))
  6841. (when (eq cmd 'system)
  6842. (setq cmd (cdr (assoc 'system apps))))
  6843. (when (eq cmd 'default)
  6844. (setq cmd (cdr (assoc t apps))))
  6845. (when (eq cmd 'mailcap)
  6846. (require 'mailcap)
  6847. (mailcap-parse-mailcaps)
  6848. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6849. (command (mailcap-mime-info mime-type)))
  6850. (if (stringp command)
  6851. (setq cmd command)
  6852. (setq cmd 'emacs))))
  6853. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6854. (not (file-exists-p file))
  6855. (not org-open-non-existing-files))
  6856. (error "No such file: %s" file))
  6857. (cond
  6858. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6859. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6860. (while (string-match "['\"]%s['\"]" cmd)
  6861. (setq cmd (replace-match "%s" t t cmd)))
  6862. (while (string-match "%s" cmd)
  6863. (setq cmd (replace-match
  6864. (save-match-data
  6865. (shell-quote-argument
  6866. (convert-standard-filename file)))
  6867. t t cmd)))
  6868. (save-window-excursion
  6869. (start-process-shell-command cmd nil cmd)
  6870. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6871. ))
  6872. ((or (stringp cmd)
  6873. (eq cmd 'emacs))
  6874. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6875. (widen)
  6876. (if line (goto-line line)
  6877. (if search (org-link-search search))))
  6878. ((consp cmd)
  6879. (let ((file (convert-standard-filename file)))
  6880. (eval cmd)))
  6881. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6882. (and (org-mode-p) (eq old-mode 'org-mode)
  6883. (or (not (equal old-buffer (current-buffer)))
  6884. (not (equal old-pos (point))))
  6885. (org-mark-ring-push old-pos old-buffer))))
  6886. (defun org-default-apps ()
  6887. "Return the default applications for this operating system."
  6888. (cond
  6889. ((eq system-type 'darwin)
  6890. org-file-apps-defaults-macosx)
  6891. ((eq system-type 'windows-nt)
  6892. org-file-apps-defaults-windowsnt)
  6893. (t org-file-apps-defaults-gnu)))
  6894. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6895. "Convert extensions to regular expressions in the cars of LIST.
  6896. Also, weed out any non-string entries, because the return value is used
  6897. only for regexp matching.
  6898. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6899. point to the symbol `emacs', indicating that the file should
  6900. be opened in Emacs."
  6901. (append
  6902. (delq nil
  6903. (mapcar (lambda (x)
  6904. (if (not (stringp (car x)))
  6905. nil
  6906. (if (string-match "\\W" (car x))
  6907. x
  6908. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6909. list))
  6910. (if add-auto-mode
  6911. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6912. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6913. (defun org-file-remote-p (file)
  6914. "Test whether FILE specifies a location on a remote system.
  6915. Return non-nil if the location is indeed remote.
  6916. For example, the filename \"/user@host:/foo\" specifies a location
  6917. on the system \"/user@host:\"."
  6918. (cond ((fboundp 'file-remote-p)
  6919. (file-remote-p file))
  6920. ((fboundp 'tramp-handle-file-remote-p)
  6921. (tramp-handle-file-remote-p file))
  6922. ((and (boundp 'ange-ftp-name-format)
  6923. (string-match (car ange-ftp-name-format) file))
  6924. t)
  6925. (t nil)))
  6926. ;;;; Refiling
  6927. (defun org-get-org-file ()
  6928. "Read a filename, with default directory `org-directory'."
  6929. (let ((default (or org-default-notes-file remember-data-file)))
  6930. (read-file-name (format "File name [%s]: " default)
  6931. (file-name-as-directory org-directory)
  6932. default)))
  6933. (defun org-notes-order-reversed-p ()
  6934. "Check if the current file should receive notes in reversed order."
  6935. (cond
  6936. ((not org-reverse-note-order) nil)
  6937. ((eq t org-reverse-note-order) t)
  6938. ((not (listp org-reverse-note-order)) nil)
  6939. (t (catch 'exit
  6940. (let ((all org-reverse-note-order)
  6941. entry)
  6942. (while (setq entry (pop all))
  6943. (if (string-match (car entry) buffer-file-name)
  6944. (throw 'exit (cdr entry))))
  6945. nil)))))
  6946. (defvar org-refile-target-table nil
  6947. "The list of refile targets, created by `org-refile'.")
  6948. (defvar org-agenda-new-buffers nil
  6949. "Buffers created to visit agenda files.")
  6950. (defun org-get-refile-targets (&optional default-buffer)
  6951. "Produce a table with refile targets."
  6952. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6953. targets txt re files f desc descre fast-path-p level)
  6954. (message "Getting targets...")
  6955. (with-current-buffer (or default-buffer (current-buffer))
  6956. (while (setq entry (pop entries))
  6957. (setq files (car entry) desc (cdr entry))
  6958. (setq fast-path-p nil)
  6959. (cond
  6960. ((null files) (setq files (list (current-buffer))))
  6961. ((eq files 'org-agenda-files)
  6962. (setq files (org-agenda-files 'unrestricted)))
  6963. ((and (symbolp files) (fboundp files))
  6964. (setq files (funcall files)))
  6965. ((and (symbolp files) (boundp files))
  6966. (setq files (symbol-value files))))
  6967. (if (stringp files) (setq files (list files)))
  6968. (cond
  6969. ((eq (car desc) :tag)
  6970. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6971. ((eq (car desc) :todo)
  6972. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6973. ((eq (car desc) :regexp)
  6974. (setq descre (cdr desc)))
  6975. ((eq (car desc) :level)
  6976. (setq descre (concat "^\\*\\{" (number-to-string
  6977. (if org-odd-levels-only
  6978. (1- (* 2 (cdr desc)))
  6979. (cdr desc)))
  6980. "\\}[ \t]")))
  6981. ((eq (car desc) :maxlevel)
  6982. (setq fast-path-p t)
  6983. (setq descre (concat "^\\*\\{1," (number-to-string
  6984. (if org-odd-levels-only
  6985. (1- (* 2 (cdr desc)))
  6986. (cdr desc)))
  6987. "\\}[ \t]")))
  6988. (t (error "Bad refiling target description %s" desc)))
  6989. (while (setq f (pop files))
  6990. (save-excursion
  6991. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6992. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6993. (setq f (expand-file-name f))
  6994. (save-excursion
  6995. (save-restriction
  6996. (widen)
  6997. (goto-char (point-min))
  6998. (while (re-search-forward descre nil t)
  6999. (goto-char (point-at-bol))
  7000. (when (looking-at org-complex-heading-regexp)
  7001. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7002. txt (org-link-display-format (match-string 4))
  7003. re (concat "^" (regexp-quote
  7004. (buffer-substring (match-beginning 1)
  7005. (match-end 4)))))
  7006. (if (match-end 5) (setq re (concat re "[ \t]+"
  7007. (regexp-quote
  7008. (match-string 5)))))
  7009. (setq re (concat re "[ \t]*$"))
  7010. (when org-refile-use-outline-path
  7011. (setq txt (mapconcat 'org-protect-slash
  7012. (append
  7013. (if (eq org-refile-use-outline-path 'file)
  7014. (list (file-name-nondirectory
  7015. (buffer-file-name (buffer-base-buffer))))
  7016. (if (eq org-refile-use-outline-path 'full-file-path)
  7017. (list (buffer-file-name (buffer-base-buffer)))))
  7018. (org-get-outline-path fast-path-p level txt)
  7019. (list txt))
  7020. "/")))
  7021. (push (list txt f re (point)) targets))
  7022. (goto-char (point-at-eol))))))))
  7023. (message "Getting targets...done")
  7024. (nreverse targets))))
  7025. (defun org-protect-slash (s)
  7026. (while (string-match "/" s)
  7027. (setq s (replace-match "\\" t t s)))
  7028. s)
  7029. (defvar org-olpa (make-vector 20 nil))
  7030. (defun org-get-outline-path (&optional fastp level heading)
  7031. "Return the outline path to the current entry, as a list."
  7032. (if fastp
  7033. (progn
  7034. (if (> level 19)
  7035. (error "Outline path failure, more than 19 levels."))
  7036. (loop for i from level upto 19 do
  7037. (aset org-olpa i nil))
  7038. (prog1
  7039. (delq nil (append org-olpa nil))
  7040. (aset org-olpa level heading)))
  7041. (let (rtn)
  7042. (save-excursion
  7043. (while (org-up-heading-safe)
  7044. (when (looking-at org-complex-heading-regexp)
  7045. (push (org-match-string-no-properties 4) rtn)))
  7046. rtn))))
  7047. (defvar org-refile-history nil
  7048. "History for refiling operations.")
  7049. (defun org-refile (&optional goto default-buffer)
  7050. "Move the entry at point to another heading.
  7051. The list of target headings is compiled using the information in
  7052. `org-refile-targets', which see. This list is created before each use
  7053. and will therefore always be up-to-date.
  7054. At the target location, the entry is filed as a subitem of the target heading.
  7055. Depending on `org-reverse-note-order', the new subitem will either be the
  7056. first or the last subitem.
  7057. If there is an active region, all entries in that region will be moved.
  7058. However, the region must fulfil the requirement that the first heading
  7059. is the first one sets the top-level of the moved text - at most siblings
  7060. below it are allowed.
  7061. With prefix arg GOTO, the command will only visit the target location,
  7062. not actually move anything.
  7063. With a double prefix `C-u C-u', go to the location where the last refiling
  7064. operation has put the subtree."
  7065. (interactive "P")
  7066. (let* ((cbuf (current-buffer))
  7067. (regionp (org-region-active-p))
  7068. (region-start (and regionp (region-beginning)))
  7069. (region-end (and regionp (region-end)))
  7070. (region-length (and regionp (- region-end region-start)))
  7071. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7072. pos it nbuf file re level reversed)
  7073. (when regionp (goto-char region-start)
  7074. (unless (org-kill-is-subtree-p
  7075. (buffer-substring region-start region-end))
  7076. (error "The region is not a (sequence of) subtree(s)")))
  7077. (if (equal goto '(16))
  7078. (org-refile-goto-last-stored)
  7079. (when (setq it (org-refile-get-location
  7080. (if goto "Goto: " "Refile to: ") default-buffer))
  7081. (setq file (nth 1 it)
  7082. re (nth 2 it)
  7083. pos (nth 3 it))
  7084. (if (and (equal (buffer-file-name) file)
  7085. (if regionp
  7086. (and (>= pos region-start)
  7087. (<= pos region-end))
  7088. (and (>= pos (point))
  7089. (< pos (save-excursion
  7090. (org-end-of-subtree t t))))))
  7091. (error "Cannot refile to position inside the tree or region"))
  7092. (setq nbuf (or (find-buffer-visiting file)
  7093. (find-file-noselect file)))
  7094. (if goto
  7095. (progn
  7096. (switch-to-buffer nbuf)
  7097. (goto-char pos)
  7098. (org-show-context 'org-goto))
  7099. (if regionp
  7100. (progn
  7101. (kill-new (buffer-substring region-start region-end))
  7102. (org-save-markers-in-region region-start region-end))
  7103. (org-copy-subtree 1 nil t))
  7104. (save-excursion
  7105. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7106. (find-file-noselect file))))
  7107. (setq reversed (org-notes-order-reversed-p))
  7108. (save-excursion
  7109. (save-restriction
  7110. (widen)
  7111. (goto-char pos)
  7112. (looking-at outline-regexp)
  7113. (setq level (org-get-valid-level (funcall outline-level) 1))
  7114. (goto-char
  7115. (if reversed
  7116. (or (outline-next-heading) (point-max))
  7117. (or (save-excursion (outline-get-next-sibling))
  7118. (org-end-of-subtree t t)
  7119. (point-max))))
  7120. (if (not (bolp)) (newline))
  7121. (bookmark-set "org-refile-last-stored")
  7122. (org-paste-subtree level))))
  7123. (if regionp
  7124. (delete-region (point) (+ (point) region-length))
  7125. (org-cut-subtree))
  7126. (setq org-markers-to-move nil)
  7127. (message "Refiled to \"%s\"" (car it)))))))
  7128. (defun org-refile-goto-last-stored ()
  7129. "Go to the location where the last refile was stored."
  7130. (interactive)
  7131. (bookmark-jump "org-refile-last-stored")
  7132. (message "This is the location of the last refile"))
  7133. (defun org-refile-get-location (&optional prompt default-buffer)
  7134. "Prompt the user for a refile location, using PROMPT."
  7135. (let ((org-refile-targets org-refile-targets)
  7136. (org-refile-use-outline-path org-refile-use-outline-path))
  7137. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7138. (unless org-refile-target-table
  7139. (error "No refile targets"))
  7140. (let* ((cbuf (current-buffer))
  7141. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7142. (cfunc (if (and org-refile-use-outline-path
  7143. org-outline-path-complete-in-steps)
  7144. 'org-olpath-completing-read
  7145. 'org-ido-completing-read))
  7146. (extra (if org-refile-use-outline-path "/" ""))
  7147. (filename (and cfn (expand-file-name cfn)))
  7148. (tbl (mapcar
  7149. (lambda (x)
  7150. (if (not (equal filename (nth 1 x)))
  7151. (cons (concat (car x) extra " ("
  7152. (file-name-nondirectory (nth 1 x)) ")")
  7153. (cdr x))
  7154. (cons (concat (car x) extra) (cdr x))))
  7155. org-refile-target-table))
  7156. (completion-ignore-case t))
  7157. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7158. tbl)))
  7159. (defun org-olpath-completing-read (prompt collection &rest args)
  7160. "Read an outline path like a file name."
  7161. (let ((thetable collection))
  7162. (apply
  7163. 'org-ido-completing-read prompt
  7164. (lambda (string predicate &optional flag)
  7165. (let (rtn r f (l (length string)))
  7166. (cond
  7167. ((eq flag nil)
  7168. ;; try completion
  7169. (try-completion string thetable))
  7170. ((eq flag t)
  7171. ;; all-completions
  7172. (setq rtn (all-completions string thetable predicate))
  7173. (mapcar
  7174. (lambda (x)
  7175. (setq r (substring x l))
  7176. (if (string-match " ([^)]*)$" x)
  7177. (setq f (match-string 0 x))
  7178. (setq f ""))
  7179. (if (string-match "/" r)
  7180. (concat string (substring r 0 (match-end 0)) f)
  7181. x))
  7182. rtn))
  7183. ((eq flag 'lambda)
  7184. ;; exact match?
  7185. (assoc string thetable)))
  7186. ))
  7187. args)))
  7188. ;;;; Dynamic blocks
  7189. (defun org-find-dblock (name)
  7190. "Find the first dynamic block with name NAME in the buffer.
  7191. If not found, stay at current position and return nil."
  7192. (let (pos)
  7193. (save-excursion
  7194. (goto-char (point-min))
  7195. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7196. nil t)
  7197. (match-beginning 0))))
  7198. (if pos (goto-char pos))
  7199. pos))
  7200. (defconst org-dblock-start-re
  7201. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7202. "Matches the startline of a dynamic block, with parameters.")
  7203. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7204. "Matches the end of a dynamic block.")
  7205. (defun org-create-dblock (plist)
  7206. "Create a dynamic block section, with parameters taken from PLIST.
  7207. PLIST must contain a :name entry which is used as name of the block."
  7208. (unless (bolp) (newline))
  7209. (let ((name (plist-get plist :name)))
  7210. (insert "#+BEGIN: " name)
  7211. (while plist
  7212. (if (eq (car plist) :name)
  7213. (setq plist (cddr plist))
  7214. (insert " " (prin1-to-string (pop plist)))))
  7215. (insert "\n\n#+END:\n")
  7216. (beginning-of-line -2)))
  7217. (defun org-prepare-dblock ()
  7218. "Prepare dynamic block for refresh.
  7219. This empties the block, puts the cursor at the insert position and returns
  7220. the property list including an extra property :name with the block name."
  7221. (unless (looking-at org-dblock-start-re)
  7222. (error "Not at a dynamic block"))
  7223. (let* ((begdel (1+ (match-end 0)))
  7224. (name (org-no-properties (match-string 1)))
  7225. (params (append (list :name name)
  7226. (read (concat "(" (match-string 3) ")")))))
  7227. (unless (re-search-forward org-dblock-end-re nil t)
  7228. (error "Dynamic block not terminated"))
  7229. (setq params
  7230. (append params
  7231. (list :content (buffer-substring
  7232. begdel (match-beginning 0)))))
  7233. (delete-region begdel (match-beginning 0))
  7234. (goto-char begdel)
  7235. (open-line 1)
  7236. params))
  7237. (defun org-map-dblocks (&optional command)
  7238. "Apply COMMAND to all dynamic blocks in the current buffer.
  7239. If COMMAND is not given, use `org-update-dblock'."
  7240. (let ((cmd (or command 'org-update-dblock))
  7241. pos)
  7242. (save-excursion
  7243. (goto-char (point-min))
  7244. (while (re-search-forward org-dblock-start-re nil t)
  7245. (goto-char (setq pos (match-beginning 0)))
  7246. (condition-case nil
  7247. (funcall cmd)
  7248. (error (message "Error during update of dynamic block")))
  7249. (goto-char pos)
  7250. (unless (re-search-forward org-dblock-end-re nil t)
  7251. (error "Dynamic block not terminated"))))))
  7252. (defun org-dblock-update (&optional arg)
  7253. "User command for updating dynamic blocks.
  7254. Update the dynamic block at point. With prefix ARG, update all dynamic
  7255. blocks in the buffer."
  7256. (interactive "P")
  7257. (if arg
  7258. (org-update-all-dblocks)
  7259. (or (looking-at org-dblock-start-re)
  7260. (org-beginning-of-dblock))
  7261. (org-update-dblock)))
  7262. (defun org-update-dblock ()
  7263. "Update the dynamic block at point
  7264. This means to empty the block, parse for parameters and then call
  7265. the correct writing function."
  7266. (save-window-excursion
  7267. (let* ((pos (point))
  7268. (line (org-current-line))
  7269. (params (org-prepare-dblock))
  7270. (name (plist-get params :name))
  7271. (cmd (intern (concat "org-dblock-write:" name))))
  7272. (message "Updating dynamic block `%s' at line %d..." name line)
  7273. (funcall cmd params)
  7274. (message "Updating dynamic block `%s' at line %d...done" name line)
  7275. (goto-char pos))))
  7276. (defun org-beginning-of-dblock ()
  7277. "Find the beginning of the dynamic block at point.
  7278. Error if there is no such block at point."
  7279. (let ((pos (point))
  7280. beg)
  7281. (end-of-line 1)
  7282. (if (and (re-search-backward org-dblock-start-re nil t)
  7283. (setq beg (match-beginning 0))
  7284. (re-search-forward org-dblock-end-re nil t)
  7285. (> (match-end 0) pos))
  7286. (goto-char beg)
  7287. (goto-char pos)
  7288. (error "Not in a dynamic block"))))
  7289. (defun org-update-all-dblocks ()
  7290. "Update all dynamic blocks in the buffer.
  7291. This function can be used in a hook."
  7292. (when (org-mode-p)
  7293. (org-map-dblocks 'org-update-dblock)))
  7294. ;;;; Completion
  7295. (defconst org-additional-option-like-keywords
  7296. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7297. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7298. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7299. "BEGIN_QUOTE" "END_QUOTE"
  7300. "BEGIN_VERSE" "END_VERSE"
  7301. "BEGIN_SRC" "END_SRC"
  7302. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7303. (defcustom org-structure-template-alist
  7304. '(
  7305. ("s" "#+begin_src ?\n\n#+end_src"
  7306. "<src lang=\"?\">\n\n</src>")
  7307. ("e" "#+begin_example\n?\n#+end_example"
  7308. "<example>\n?\n</example>")
  7309. ("q" "#+begin_quote\n?\n#+end_quote"
  7310. "<quote>\n?\n</quote>")
  7311. ("v" "#+begin_verse\n?\n#+end_verse"
  7312. "<verse>\n?\n/verse>")
  7313. ("l" "#+begin_latex\n?\n#+end_latex"
  7314. "<literal style=\"latex\">\n?\n</literal>")
  7315. ("L" "#+latex: "
  7316. "<literal style=\"latex\">?</literal>")
  7317. ("h" "#+begin_html\n?\n#+end_html"
  7318. "<literal style=\"html\">\n?\n</literal>")
  7319. ("H" "#+html: "
  7320. "<literal style=\"html\">?</literal>")
  7321. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7322. ("A" "#+ascii: ")
  7323. ("i" "#+include %file ?"
  7324. "<include file=%file markup=\"?\">")
  7325. )
  7326. "Structure completion elements.
  7327. This is a list of abbreviation keys and values. The value gets inserted
  7328. it you type @samp{.} followed by the key and then the completion key,
  7329. usually `M-TAB'. %file will be replaced by a file name after prompting
  7330. for the file using completion.
  7331. There are two templates for each key, the first uses the original Org syntax,
  7332. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7333. the default when the /org-mtags.el/ module has been loaded. See also the
  7334. variable `org-mtags-prefer-muse-templates'.
  7335. This is an experimental feature, it is undecided if it is going to stay in."
  7336. :group 'org-completion
  7337. :type '(repeat
  7338. (string :tag "Key")
  7339. (string :tag "Template")
  7340. (string :tag "Muse Template")))
  7341. (defun org-try-structure-completion ()
  7342. "Try to complete a structure template before point.
  7343. This looks for strings like \"<e\" on an otherwise empty line and
  7344. expands them."
  7345. (let ((l (buffer-substring (point-at-bol) (point)))
  7346. a)
  7347. (when (and (looking-at "[ \t]*$")
  7348. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7349. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7350. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7351. (match-beginning 1)) a)
  7352. t)))
  7353. (defun org-complete-expand-structure-template (start cell)
  7354. "Expand a structure template."
  7355. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7356. (rpl (nth (if musep 2 1) cell)))
  7357. (delete-region start (point))
  7358. (when (string-match "\\`#\\+" rpl)
  7359. (cond
  7360. ((bolp))
  7361. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7362. (delete-region (point-at-bol) (point)))
  7363. (t (newline))))
  7364. (setq start (point))
  7365. (if (string-match "%file" rpl)
  7366. (setq rpl (replace-match
  7367. (concat
  7368. "\""
  7369. (save-match-data
  7370. (abbreviate-file-name (read-file-name "Include file: ")))
  7371. "\"")
  7372. t t rpl)))
  7373. (insert rpl)
  7374. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7375. (defun org-complete (&optional arg)
  7376. "Perform completion on word at point.
  7377. At the beginning of a headline, this completes TODO keywords as given in
  7378. `org-todo-keywords'.
  7379. If the current word is preceded by a backslash, completes the TeX symbols
  7380. that are supported for HTML support.
  7381. If the current word is preceded by \"#+\", completes special words for
  7382. setting file options.
  7383. In the line after \"#+STARTUP:, complete valid keywords.\"
  7384. At all other locations, this simply calls the value of
  7385. `org-completion-fallback-command'."
  7386. (interactive "P")
  7387. (org-without-partial-completion
  7388. (catch 'exit
  7389. (let* ((a nil)
  7390. (end (point))
  7391. (beg1 (save-excursion
  7392. (skip-chars-backward (org-re "[:alnum:]_@"))
  7393. (point)))
  7394. (beg (save-excursion
  7395. (skip-chars-backward "a-zA-Z0-9_:$")
  7396. (point)))
  7397. (confirm (lambda (x) (stringp (car x))))
  7398. (searchhead (equal (char-before beg) ?*))
  7399. (struct
  7400. (when (and (member (char-before beg1) '(?. ?<))
  7401. (setq a (assoc (buffer-substring beg1 (point))
  7402. org-structure-template-alist)))
  7403. (org-complete-expand-structure-template (1- beg1) a)
  7404. (throw 'exit t)))
  7405. (tag (and (equal (char-before beg1) ?:)
  7406. (equal (char-after (point-at-bol)) ?*)))
  7407. (prop (and (equal (char-before beg1) ?:)
  7408. (not (equal (char-after (point-at-bol)) ?*))))
  7409. (texp (equal (char-before beg) ?\\))
  7410. (link (equal (char-before beg) ?\[))
  7411. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7412. beg)
  7413. "#+"))
  7414. (startup (string-match "^#\\+STARTUP:.*"
  7415. (buffer-substring (point-at-bol) (point))))
  7416. (completion-ignore-case opt)
  7417. (type nil)
  7418. (tbl nil)
  7419. (table (cond
  7420. (opt
  7421. (setq type :opt)
  7422. (require 'org-exp)
  7423. (append
  7424. (mapcar
  7425. (lambda (x)
  7426. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7427. (cons (match-string 2 x) (match-string 1 x)))
  7428. (org-split-string (org-get-current-options) "\n"))
  7429. (mapcar 'list org-additional-option-like-keywords)))
  7430. (startup
  7431. (setq type :startup)
  7432. org-startup-options)
  7433. (link (append org-link-abbrev-alist-local
  7434. org-link-abbrev-alist))
  7435. (texp
  7436. (setq type :tex)
  7437. org-html-entities)
  7438. ((string-match "\\`\\*+[ \t]+\\'"
  7439. (buffer-substring (point-at-bol) beg))
  7440. (setq type :todo)
  7441. (mapcar 'list org-todo-keywords-1))
  7442. (searchhead
  7443. (setq type :searchhead)
  7444. (save-excursion
  7445. (goto-char (point-min))
  7446. (while (re-search-forward org-todo-line-regexp nil t)
  7447. (push (list
  7448. (org-make-org-heading-search-string
  7449. (match-string 3) t))
  7450. tbl)))
  7451. tbl)
  7452. (tag (setq type :tag beg beg1)
  7453. (or org-tag-alist (org-get-buffer-tags)))
  7454. (prop (setq type :prop beg beg1)
  7455. (mapcar 'list (org-buffer-property-keys nil t t)))
  7456. (t (progn
  7457. (call-interactively org-completion-fallback-command)
  7458. (throw 'exit nil)))))
  7459. (pattern (buffer-substring-no-properties beg end))
  7460. (completion (try-completion pattern table confirm)))
  7461. (cond ((eq completion t)
  7462. (if (not (assoc (upcase pattern) table))
  7463. (message "Already complete")
  7464. (if (and (equal type :opt)
  7465. (not (member (car (assoc (upcase pattern) table))
  7466. org-additional-option-like-keywords)))
  7467. (insert (substring (cdr (assoc (upcase pattern) table))
  7468. (length pattern)))
  7469. (if (memq type '(:tag :prop)) (insert ":")))))
  7470. ((null completion)
  7471. (message "Can't find completion for \"%s\"" pattern)
  7472. (ding))
  7473. ((not (string= pattern completion))
  7474. (delete-region beg end)
  7475. (if (string-match " +$" completion)
  7476. (setq completion (replace-match "" t t completion)))
  7477. (insert completion)
  7478. (if (get-buffer-window "*Completions*")
  7479. (delete-window (get-buffer-window "*Completions*")))
  7480. (if (assoc completion table)
  7481. (if (eq type :todo) (insert " ")
  7482. (if (memq type '(:tag :prop)) (insert ":"))))
  7483. (if (and (equal type :opt) (assoc completion table))
  7484. (message "%s" (substitute-command-keys
  7485. "Press \\[org-complete] again to insert example settings"))))
  7486. (t
  7487. (message "Making completion list...")
  7488. (let ((list (sort (all-completions pattern table confirm)
  7489. 'string<)))
  7490. (with-output-to-temp-buffer "*Completions*"
  7491. (condition-case nil
  7492. ;; Protection needed for XEmacs and emacs 21
  7493. (display-completion-list list pattern)
  7494. (error (display-completion-list list)))))
  7495. (message "Making completion list...%s" "done")))))))
  7496. ;;;; TODO, DEADLINE, Comments
  7497. (defun org-toggle-comment ()
  7498. "Change the COMMENT state of an entry."
  7499. (interactive)
  7500. (save-excursion
  7501. (org-back-to-heading)
  7502. (let (case-fold-search)
  7503. (if (looking-at (concat outline-regexp
  7504. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7505. (replace-match "" t t nil 1)
  7506. (if (looking-at outline-regexp)
  7507. (progn
  7508. (goto-char (match-end 0))
  7509. (insert org-comment-string " ")))))))
  7510. (defvar org-last-todo-state-is-todo nil
  7511. "This is non-nil when the last TODO state change led to a TODO state.
  7512. If the last change removed the TODO tag or switched to DONE, then
  7513. this is nil.")
  7514. (defvar org-setting-tags nil) ; dynamically skipped
  7515. (defun org-parse-local-options (string var)
  7516. "Parse STRING for startup setting relevant for variable VAR."
  7517. (let ((rtn (symbol-value var))
  7518. e opts)
  7519. (save-match-data
  7520. (if (or (not string) (not (string-match "\\S-" string)))
  7521. rtn
  7522. (setq opts (delq nil (mapcar (lambda (x)
  7523. (setq e (assoc x org-startup-options))
  7524. (if (eq (nth 1 e) var) e nil))
  7525. (org-split-string string "[ \t]+"))))
  7526. (if (not opts)
  7527. rtn
  7528. (setq rtn nil)
  7529. (while (setq e (pop opts))
  7530. (if (not (nth 3 e))
  7531. (setq rtn (nth 2 e))
  7532. (if (not (listp rtn)) (setq rtn nil))
  7533. (push (nth 2 e) rtn)))
  7534. rtn)))))
  7535. (defvar org-agenda-headline-snapshot-before-repeat)
  7536. (defun org-todo (&optional arg)
  7537. "Change the TODO state of an item.
  7538. The state of an item is given by a keyword at the start of the heading,
  7539. like
  7540. *** TODO Write paper
  7541. *** DONE Call mom
  7542. The different keywords are specified in the variable `org-todo-keywords'.
  7543. By default the available states are \"TODO\" and \"DONE\".
  7544. So for this example: when the item starts with TODO, it is changed to DONE.
  7545. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7546. DONE are present, add TODO at the beginning of the heading.
  7547. With C-u prefix arg, use completion to determine the new state.
  7548. With numeric prefix arg, switch to that state.
  7549. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7550. With a tripple C-u prefix, circumvent any state blocking.
  7551. For calling through lisp, arg is also interpreted in the following way:
  7552. 'none -> empty state
  7553. \"\"(empty string) -> switch to empty state
  7554. 'done -> switch to DONE
  7555. 'nextset -> switch to the next set of keywords
  7556. 'previousset -> switch to the previous set of keywords
  7557. \"WAITING\" -> switch to the specified keyword, but only if it
  7558. really is a member of `org-todo-keywords'."
  7559. (interactive "P")
  7560. (if (equal arg '(16)) (setq arg 'nextset))
  7561. (let ((org-blocker-hook org-blocker-hook))
  7562. (when (equal arg '(64))
  7563. (setq arg nil org-blocker-hook nil))
  7564. (save-excursion
  7565. (catch 'exit
  7566. (org-back-to-heading)
  7567. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7568. (or (looking-at (concat " +" org-todo-regexp " *"))
  7569. (looking-at " *"))
  7570. (let* ((match-data (match-data))
  7571. (startpos (point-at-bol))
  7572. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7573. (org-log-done org-log-done)
  7574. (org-log-repeat org-log-repeat)
  7575. (org-todo-log-states org-todo-log-states)
  7576. (this (match-string 1))
  7577. (hl-pos (match-beginning 0))
  7578. (head (org-get-todo-sequence-head this))
  7579. (ass (assoc head org-todo-kwd-alist))
  7580. (interpret (nth 1 ass))
  7581. (done-word (nth 3 ass))
  7582. (final-done-word (nth 4 ass))
  7583. (last-state (or this ""))
  7584. (completion-ignore-case t)
  7585. (member (member this org-todo-keywords-1))
  7586. (tail (cdr member))
  7587. (state (cond
  7588. ((and org-todo-key-trigger
  7589. (or (and (equal arg '(4))
  7590. (eq org-use-fast-todo-selection 'prefix))
  7591. (and (not arg) org-use-fast-todo-selection
  7592. (not (eq org-use-fast-todo-selection
  7593. 'prefix)))))
  7594. ;; Use fast selection
  7595. (org-fast-todo-selection))
  7596. ((and (equal arg '(4))
  7597. (or (not org-use-fast-todo-selection)
  7598. (not org-todo-key-trigger)))
  7599. ;; Read a state with completion
  7600. (org-ido-completing-read
  7601. "State: " (mapcar (lambda(x) (list x))
  7602. org-todo-keywords-1)
  7603. nil t))
  7604. ((eq arg 'right)
  7605. (if this
  7606. (if tail (car tail) nil)
  7607. (car org-todo-keywords-1)))
  7608. ((eq arg 'left)
  7609. (if (equal member org-todo-keywords-1)
  7610. nil
  7611. (if this
  7612. (nth (- (length org-todo-keywords-1)
  7613. (length tail) 2)
  7614. org-todo-keywords-1)
  7615. (org-last org-todo-keywords-1))))
  7616. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7617. (setq arg nil))) ; hack to fall back to cycling
  7618. (arg
  7619. ;; user or caller requests a specific state
  7620. (cond
  7621. ((equal arg "") nil)
  7622. ((eq arg 'none) nil)
  7623. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7624. ((eq arg 'nextset)
  7625. (or (car (cdr (member head org-todo-heads)))
  7626. (car org-todo-heads)))
  7627. ((eq arg 'previousset)
  7628. (let ((org-todo-heads (reverse org-todo-heads)))
  7629. (or (car (cdr (member head org-todo-heads)))
  7630. (car org-todo-heads))))
  7631. ((car (member arg org-todo-keywords-1)))
  7632. ((nth (1- (prefix-numeric-value arg))
  7633. org-todo-keywords-1))))
  7634. ((null member) (or head (car org-todo-keywords-1)))
  7635. ((equal this final-done-word) nil) ;; -> make empty
  7636. ((null tail) nil) ;; -> first entry
  7637. ((eq interpret 'sequence)
  7638. (car tail))
  7639. ((memq interpret '(type priority))
  7640. (if (eq this-command last-command)
  7641. (car tail)
  7642. (if (> (length tail) 0)
  7643. (or done-word (car org-done-keywords))
  7644. nil)))
  7645. (t nil)))
  7646. (next (if state (concat " " state " ") " "))
  7647. (change-plist (list :type 'todo-state-change :from this :to state
  7648. :position startpos))
  7649. dolog now-done-p)
  7650. (when org-blocker-hook
  7651. (setq org-last-todo-state-is-todo
  7652. (not (member this org-done-keywords)))
  7653. (unless (save-excursion
  7654. (save-match-data
  7655. (run-hook-with-args-until-failure
  7656. 'org-blocker-hook change-plist)))
  7657. (if (interactive-p)
  7658. (error "TODO state change from %s to %s blocked" this state)
  7659. ;; fail silently
  7660. (message "TODO state change from %s to %s blocked" this state)
  7661. (throw 'exit nil))))
  7662. (store-match-data match-data)
  7663. (replace-match next t t)
  7664. (unless (pos-visible-in-window-p hl-pos)
  7665. (message "TODO state changed to %s" (org-trim next)))
  7666. (unless head
  7667. (setq head (org-get-todo-sequence-head state)
  7668. ass (assoc head org-todo-kwd-alist)
  7669. interpret (nth 1 ass)
  7670. done-word (nth 3 ass)
  7671. final-done-word (nth 4 ass)))
  7672. (when (memq arg '(nextset previousset))
  7673. (message "Keyword-Set %d/%d: %s"
  7674. (- (length org-todo-sets) -1
  7675. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7676. (length org-todo-sets)
  7677. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7678. (setq org-last-todo-state-is-todo
  7679. (not (member state org-done-keywords)))
  7680. (setq now-done-p (and (member state org-done-keywords)
  7681. (not (member this org-done-keywords))))
  7682. (and logging (org-local-logging logging))
  7683. (when (and (or org-todo-log-states org-log-done)
  7684. (not (memq arg '(nextset previousset))))
  7685. ;; we need to look at recording a time and note
  7686. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7687. (nth 2 (assoc this org-todo-log-states))))
  7688. (when (and state
  7689. (member state org-not-done-keywords)
  7690. (not (member this org-not-done-keywords)))
  7691. ;; This is now a todo state and was not one before
  7692. ;; If there was a CLOSED time stamp, get rid of it.
  7693. (org-add-planning-info nil nil 'closed))
  7694. (when (and now-done-p org-log-done)
  7695. ;; It is now done, and it was not done before
  7696. (org-add-planning-info 'closed (org-current-time))
  7697. (if (and (not dolog) (eq 'note org-log-done))
  7698. (org-add-log-setup 'done state 'findpos 'note)))
  7699. (when (and state dolog)
  7700. ;; This is a non-nil state, and we need to log it
  7701. (org-add-log-setup 'state state 'findpos dolog)))
  7702. ;; Fixup tag positioning
  7703. (org-todo-trigger-tag-changes state)
  7704. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7705. (when org-provide-todo-statistics
  7706. (org-update-parent-todo-statistics))
  7707. (run-hooks 'org-after-todo-state-change-hook)
  7708. (if (and arg (not (member state org-done-keywords)))
  7709. (setq head (org-get-todo-sequence-head state)))
  7710. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7711. ;; Do we need to trigger a repeat?
  7712. (when now-done-p
  7713. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7714. ;; This is for the agenda, take a snapshot of the headline.
  7715. (save-match-data
  7716. (setq org-agenda-headline-snapshot-before-repeat
  7717. (org-get-heading))))
  7718. (org-auto-repeat-maybe state))
  7719. ;; Fixup cursor location if close to the keyword
  7720. (if (and (outline-on-heading-p)
  7721. (not (bolp))
  7722. (save-excursion (beginning-of-line 1)
  7723. (looking-at org-todo-line-regexp))
  7724. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7725. (progn
  7726. (goto-char (or (match-end 2) (match-end 1)))
  7727. (just-one-space)))
  7728. (when org-trigger-hook
  7729. (save-excursion
  7730. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7731. (defun org-block-todo-from-children-or-siblings (change-plist)
  7732. "Block turning an entry into a TODO, using the hierarchy.
  7733. This checks whether the current task should be blocked from state
  7734. changes. Such blocking occurs when:
  7735. 1. The task has children which are not all in a completed state.
  7736. 2. A task has a parent with the property :ORDERED:, and there
  7737. are siblings prior to the current task with incomplete
  7738. status."
  7739. (catch 'dont-block
  7740. ;; If this is not a todo state change, or if this entry is already DONE,
  7741. ;; do not block
  7742. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7743. (member (plist-get change-plist :from)
  7744. (cons 'done org-done-keywords))
  7745. (member (plist-get change-plist :to)
  7746. (cons 'todo org-not-done-keywords)))
  7747. (throw 'dont-block t))
  7748. ;; If this task has children, and any are undone, it's blocked
  7749. (save-excursion
  7750. (org-back-to-heading t)
  7751. (let ((this-level (funcall outline-level)))
  7752. (outline-next-heading)
  7753. (let ((child-level (funcall outline-level)))
  7754. (while (and (not (eobp))
  7755. (> child-level this-level))
  7756. ;; this todo has children, check whether they are all
  7757. ;; completed
  7758. (if (and (not (org-entry-is-done-p))
  7759. (org-entry-is-todo-p))
  7760. (throw 'dont-block nil))
  7761. (outline-next-heading)
  7762. (setq child-level (funcall outline-level))))))
  7763. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7764. ;; any previous siblings are undone, it's blocked
  7765. (save-excursion
  7766. (org-back-to-heading t)
  7767. (when (save-excursion
  7768. (ignore-errors
  7769. (outline-up-heading 1)
  7770. (org-entry-get (point) "ORDERED")))
  7771. (let* ((this-level (funcall outline-level))
  7772. (current-level this-level))
  7773. (while (and (not (bobp))
  7774. (= current-level this-level))
  7775. (outline-previous-heading)
  7776. (setq current-level (funcall outline-level))
  7777. (if (= current-level this-level)
  7778. ;; this todo has children, check whether they are all
  7779. ;; completed
  7780. (if (and (not (org-entry-is-done-p))
  7781. (org-entry-is-todo-p))
  7782. (throw 'dont-block nil)))))))
  7783. t)) ; don't block
  7784. (defun org-toggle-ordered-property ()
  7785. "Toggle the ORDERED property of the current entry."
  7786. (interactive)
  7787. (save-excursion
  7788. (org-back-to-heading)
  7789. (if (org-entry-get nil "ORDERED")
  7790. (progn
  7791. (org-delete-property "ORDERED")
  7792. (message "Subtasks can be completed in arbitrary order or parallel"))
  7793. (org-entry-put nil "ORDERED" "t")
  7794. (message "Subtasks must be completed in sequence"))))
  7795. (defun org-block-todo-from-checkboxes (change-plist)
  7796. "Block turning an entry into a TODO, using checkboxes.
  7797. This checks whether the current task should be blocked from state
  7798. changes because there are uncheckd boxes in this entry."
  7799. (catch 'dont-block
  7800. ;; If this is not a todo state change, or if this entry is already DONE,
  7801. ;; do not block
  7802. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7803. (member (plist-get change-plist :from)
  7804. (cons 'done org-done-keywords))
  7805. (member (plist-get change-plist :to)
  7806. (cons 'todo org-not-done-keywords)))
  7807. (throw 'dont-block t))
  7808. ;; If this task has checkboxes that are not checked, it's blocked
  7809. (save-excursion
  7810. (org-back-to-heading t)
  7811. (let ((beg (point)) end)
  7812. (outline-next-heading)
  7813. (setq end (point))
  7814. (goto-char beg)
  7815. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7816. end t)
  7817. (throw 'dont-block nil))))
  7818. t)) ; do not block
  7819. (defun org-update-parent-todo-statistics ()
  7820. "Update any statistics cookie in the parent of the current headline."
  7821. (interactive)
  7822. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7823. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7824. (catch 'exit
  7825. (save-excursion
  7826. (setq level (org-up-heading-safe))
  7827. (unless level
  7828. (throw 'exit nil))
  7829. (while (re-search-forward box-re (point-at-eol) t)
  7830. (setq is-percent (match-end 2))
  7831. (save-match-data
  7832. (unless (outline-next-heading) (throw 'exit nil))
  7833. (while (looking-at org-todo-line-regexp)
  7834. (setq kwd (match-string 2))
  7835. (and kwd (setq cnt-all (1+ cnt-all)))
  7836. (and (member kwd org-done-keywords)
  7837. (setq cnt-done (1+ cnt-done)))
  7838. (condition-case nil
  7839. (org-forward-same-level 1)
  7840. (error (end-of-line 1)))))
  7841. (replace-match
  7842. (if is-percent
  7843. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7844. (format "[%d/%d]" cnt-done cnt-all))))
  7845. (run-hook-with-args 'org-after-todo-statistics-hook
  7846. cnt-done (- cnt-all cnt-done))))))
  7847. (defvar org-after-todo-statistics-hook nil
  7848. "Hook that is called after a TODO statistics cookie has been updated.
  7849. Each function is called with two arguments: the number of not-done entries
  7850. and the number of done entries.
  7851. For example, the following function, when added to this hook, will switch
  7852. an entry to DONE when all children are done, and back to TODO when new
  7853. entries are set to a TODO status. Note that this hook is only called
  7854. when there is a statistics cookie in the headline!
  7855. (defun org-summary-todo (n-done n-not-done)
  7856. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7857. (let (org-log-done org-log-states) ; turn off logging
  7858. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7859. ")
  7860. (defun org-todo-trigger-tag-changes (state)
  7861. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7862. (let ((l org-todo-state-tags-triggers)
  7863. changes)
  7864. (when (or (not state) (equal state ""))
  7865. (setq changes (append changes (cdr (assoc "" l)))))
  7866. (when (and (stringp state) (> (length state) 0))
  7867. (setq changes (append changes (cdr (assoc state l)))))
  7868. (when (member state org-not-done-keywords)
  7869. (setq changes (append changes (cdr (assoc 'todo l)))))
  7870. (when (member state org-done-keywords)
  7871. (setq changes (append changes (cdr (assoc 'done l)))))
  7872. (dolist (c changes)
  7873. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7874. (defun org-local-logging (value)
  7875. "Get logging settings from a property VALUE."
  7876. (let* (words w a)
  7877. ;; directly set the variables, they are already local.
  7878. (setq org-log-done nil
  7879. org-log-repeat nil
  7880. org-todo-log-states nil)
  7881. (setq words (org-split-string value))
  7882. (while (setq w (pop words))
  7883. (cond
  7884. ((setq a (assoc w org-startup-options))
  7885. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7886. (set (nth 1 a) (nth 2 a))))
  7887. ((setq a (org-extract-log-state-settings w))
  7888. (and (member (car a) org-todo-keywords-1)
  7889. (push a org-todo-log-states)))))))
  7890. (defun org-get-todo-sequence-head (kwd)
  7891. "Return the head of the TODO sequence to which KWD belongs.
  7892. If KWD is not set, check if there is a text property remembering the
  7893. right sequence."
  7894. (let (p)
  7895. (cond
  7896. ((not kwd)
  7897. (or (get-text-property (point-at-bol) 'org-todo-head)
  7898. (progn
  7899. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7900. nil (point-at-eol)))
  7901. (get-text-property p 'org-todo-head))))
  7902. ((not (member kwd org-todo-keywords-1))
  7903. (car org-todo-keywords-1))
  7904. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7905. (defun org-fast-todo-selection ()
  7906. "Fast TODO keyword selection with single keys.
  7907. Returns the new TODO keyword, or nil if no state change should occur."
  7908. (let* ((fulltable org-todo-key-alist)
  7909. (done-keywords org-done-keywords) ;; needed for the faces.
  7910. (maxlen (apply 'max (mapcar
  7911. (lambda (x)
  7912. (if (stringp (car x)) (string-width (car x)) 0))
  7913. fulltable)))
  7914. (expert nil)
  7915. (fwidth (+ maxlen 3 1 3))
  7916. (ncol (/ (- (window-width) 4) fwidth))
  7917. tg cnt e c tbl
  7918. groups ingroup)
  7919. (save-excursion
  7920. (save-window-excursion
  7921. (if expert
  7922. (set-buffer (get-buffer-create " *Org todo*"))
  7923. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7924. (erase-buffer)
  7925. (org-set-local 'org-done-keywords done-keywords)
  7926. (setq tbl fulltable cnt 0)
  7927. (while (setq e (pop tbl))
  7928. (cond
  7929. ((equal e '(:startgroup))
  7930. (push '() groups) (setq ingroup t)
  7931. (when (not (= cnt 0))
  7932. (setq cnt 0)
  7933. (insert "\n"))
  7934. (insert "{ "))
  7935. ((equal e '(:endgroup))
  7936. (setq ingroup nil cnt 0)
  7937. (insert "}\n"))
  7938. (t
  7939. (setq tg (car e) c (cdr e))
  7940. (if ingroup (push tg (car groups)))
  7941. (setq tg (org-add-props tg nil 'face
  7942. (org-get-todo-face tg)))
  7943. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7944. (insert "[" c "] " tg (make-string
  7945. (- fwidth 4 (length tg)) ?\ ))
  7946. (when (= (setq cnt (1+ cnt)) ncol)
  7947. (insert "\n")
  7948. (if ingroup (insert " "))
  7949. (setq cnt 0)))))
  7950. (insert "\n")
  7951. (goto-char (point-min))
  7952. (if (not expert) (org-fit-window-to-buffer))
  7953. (message "[a-z..]:Set [SPC]:clear")
  7954. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7955. (cond
  7956. ((or (= c ?\C-g)
  7957. (and (= c ?q) (not (rassoc c fulltable))))
  7958. (setq quit-flag t))
  7959. ((= c ?\ ) nil)
  7960. ((setq e (rassoc c fulltable) tg (car e))
  7961. tg)
  7962. (t (setq quit-flag t)))))))
  7963. (defun org-entry-is-todo-p ()
  7964. (member (org-get-todo-state) org-not-done-keywords))
  7965. (defun org-entry-is-done-p ()
  7966. (member (org-get-todo-state) org-done-keywords))
  7967. (defun org-get-todo-state ()
  7968. (save-excursion
  7969. (org-back-to-heading t)
  7970. (and (looking-at org-todo-line-regexp)
  7971. (match-end 2)
  7972. (match-string 2))))
  7973. (defun org-at-date-range-p (&optional inactive-ok)
  7974. "Is the cursor inside a date range?"
  7975. (interactive)
  7976. (save-excursion
  7977. (catch 'exit
  7978. (let ((pos (point)))
  7979. (skip-chars-backward "^[<\r\n")
  7980. (skip-chars-backward "<[")
  7981. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7982. (>= (match-end 0) pos)
  7983. (throw 'exit t))
  7984. (skip-chars-backward "^<[\r\n")
  7985. (skip-chars-backward "<[")
  7986. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7987. (>= (match-end 0) pos)
  7988. (throw 'exit t)))
  7989. nil)))
  7990. (defun org-get-repeat ()
  7991. "Check if there is a deadline/schedule with repeater in this entry."
  7992. (save-match-data
  7993. (save-excursion
  7994. (org-back-to-heading t)
  7995. (if (re-search-forward
  7996. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7997. (match-string 1)))))
  7998. (defvar org-last-changed-timestamp)
  7999. (defvar org-last-inserted-timestamp)
  8000. (defvar org-log-post-message)
  8001. (defvar org-log-note-purpose)
  8002. (defvar org-log-note-how)
  8003. (defvar org-log-note-extra)
  8004. (defun org-auto-repeat-maybe (done-word)
  8005. "Check if the current headline contains a repeated deadline/schedule.
  8006. If yes, set TODO state back to what it was and change the base date
  8007. of repeating deadline/scheduled time stamps to new date.
  8008. This function is run automatically after each state change to a DONE state."
  8009. ;; last-state is dynamically scoped into this function
  8010. (let* ((repeat (org-get-repeat))
  8011. (aa (assoc last-state org-todo-kwd-alist))
  8012. (interpret (nth 1 aa))
  8013. (head (nth 2 aa))
  8014. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8015. (msg "Entry repeats: ")
  8016. (org-log-done nil)
  8017. (org-todo-log-states nil)
  8018. (nshiftmax 10) (nshift 0)
  8019. re type n what ts time)
  8020. (when repeat
  8021. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8022. (org-todo (if (eq interpret 'type) last-state head))
  8023. (when org-log-repeat
  8024. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8025. (memq 'org-add-log-note post-command-hook))
  8026. ;; OK, we are already setup for some record
  8027. (if (eq org-log-repeat 'note)
  8028. ;; make sure we take a note, not only a time stamp
  8029. (setq org-log-note-how 'note))
  8030. ;; Set up for taking a record
  8031. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8032. 'findpos org-log-repeat)))
  8033. (org-back-to-heading t)
  8034. (org-add-planning-info nil nil 'closed)
  8035. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8036. org-deadline-time-regexp "\\)\\|\\("
  8037. org-ts-regexp "\\)"))
  8038. (while (re-search-forward
  8039. re (save-excursion (outline-next-heading) (point)) t)
  8040. (setq type (if (match-end 1) org-scheduled-string
  8041. (if (match-end 3) org-deadline-string "Plain:"))
  8042. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8043. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8044. (setq n (string-to-number (match-string 2 ts))
  8045. what (match-string 3 ts))
  8046. (if (equal what "w") (setq n (* n 7) what "d"))
  8047. ;; Preparation, see if we need to modify the start date for the change
  8048. (when (match-end 1)
  8049. (setq time (save-match-data (org-time-string-to-time ts)))
  8050. (cond
  8051. ((equal (match-string 1 ts) ".")
  8052. ;; Shift starting date to today
  8053. (org-timestamp-change
  8054. (- (time-to-days (current-time)) (time-to-days time))
  8055. 'day))
  8056. ((equal (match-string 1 ts) "+")
  8057. (while (or (= nshift 0)
  8058. (<= (time-to-days time) (time-to-days (current-time))))
  8059. (when (= (incf nshift) nshiftmax)
  8060. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8061. (error "Abort")))
  8062. (org-timestamp-change n (cdr (assoc what whata)))
  8063. (org-at-timestamp-p t)
  8064. (setq ts (match-string 1))
  8065. (setq time (save-match-data (org-time-string-to-time ts))))
  8066. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8067. ;; rematch, so that we have everything in place for the real shift
  8068. (org-at-timestamp-p t)
  8069. (setq ts (match-string 1))
  8070. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8071. (org-timestamp-change n (cdr (assoc what whata)))
  8072. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8073. (setq org-log-post-message msg)
  8074. (message "%s" msg))))
  8075. (defun org-show-todo-tree (arg)
  8076. "Make a compact tree which shows all headlines marked with TODO.
  8077. The tree will show the lines where the regexp matches, and all higher
  8078. headlines above the match.
  8079. With a \\[universal-argument] prefix, also show the DONE entries.
  8080. With a numeric prefix N, construct a sparse tree for the Nth element
  8081. of `org-todo-keywords-1'."
  8082. (interactive "P")
  8083. (let ((case-fold-search nil)
  8084. (kwd-re
  8085. (cond ((null arg) org-not-done-regexp)
  8086. ((equal arg '(4))
  8087. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8088. (mapcar 'list org-todo-keywords-1))))
  8089. (concat "\\("
  8090. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8091. "\\)\\>")))
  8092. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8093. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8094. org-todo-keywords-1)))
  8095. (t (error "Invalid prefix argument: %s" arg)))))
  8096. (message "%d TODO entries found"
  8097. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8098. (defun org-deadline (&optional remove time)
  8099. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8100. With argument REMOVE, remove any deadline from the item.
  8101. When TIME is set, it should be an internal time specification, and the
  8102. scheduling will use the corresponding date."
  8103. (interactive "P")
  8104. (if remove
  8105. (progn
  8106. (org-remove-timestamp-with-keyword org-deadline-string)
  8107. (message "Item no longer has a deadline."))
  8108. (if (org-get-repeat)
  8109. (error "Cannot change deadline on task with repeater, please do that by hand")
  8110. (org-add-planning-info 'deadline time 'closed)
  8111. (message "Deadline on %s" org-last-inserted-timestamp))))
  8112. (defun org-schedule (&optional remove time)
  8113. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8114. With argument REMOVE, remove any scheduling date from the item.
  8115. When TIME is set, it should be an internal time specification, and the
  8116. scheduling will use the corresponding date."
  8117. (interactive "P")
  8118. (if remove
  8119. (progn
  8120. (org-remove-timestamp-with-keyword org-scheduled-string)
  8121. (message "Item is no longer scheduled."))
  8122. (if (org-get-repeat)
  8123. (error "Cannot reschedule task with repeater, please do that by hand")
  8124. (org-add-planning-info 'scheduled time 'closed)
  8125. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8126. (defun org-remove-timestamp-with-keyword (keyword)
  8127. "Remove all time stamps with KEYWORD in the current entry."
  8128. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8129. beg)
  8130. (save-excursion
  8131. (org-back-to-heading t)
  8132. (setq beg (point))
  8133. (org-end-of-subtree t t)
  8134. (while (re-search-backward re beg t)
  8135. (replace-match "")
  8136. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8137. (equal (char-before) ?\ ))
  8138. (backward-delete-char 1)
  8139. (if (string-match "^[ \t]*$" (buffer-substring
  8140. (point-at-bol) (point-at-eol)))
  8141. (delete-region (point-at-bol)
  8142. (min (point-max) (1+ (point-at-eol))))))))))
  8143. (defun org-add-planning-info (what &optional time &rest remove)
  8144. "Insert new timestamp with keyword in the line directly after the headline.
  8145. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8146. If non is given, the user is prompted for a date.
  8147. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8148. be removed."
  8149. (interactive)
  8150. (let (org-time-was-given org-end-time-was-given ts
  8151. end default-time default-input)
  8152. (when (and (not time) (memq what '(scheduled deadline)))
  8153. ;; Try to get a default date/time from existing timestamp
  8154. (save-excursion
  8155. (org-back-to-heading t)
  8156. (setq end (save-excursion (outline-next-heading) (point)))
  8157. (when (re-search-forward (if (eq what 'scheduled)
  8158. org-scheduled-time-regexp
  8159. org-deadline-time-regexp)
  8160. end t)
  8161. (setq ts (match-string 1)
  8162. default-time
  8163. (apply 'encode-time (org-parse-time-string ts))
  8164. default-input (and ts (org-get-compact-tod ts))))))
  8165. (when what
  8166. ;; If necessary, get the time from the user
  8167. (setq time (or time (org-read-date nil 'to-time nil nil
  8168. default-time default-input))))
  8169. (when (and org-insert-labeled-timestamps-at-point
  8170. (member what '(scheduled deadline)))
  8171. (insert
  8172. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8173. (org-insert-time-stamp time org-time-was-given
  8174. nil nil nil (list org-end-time-was-given))
  8175. (setq what nil))
  8176. (save-excursion
  8177. (save-restriction
  8178. (let (col list elt ts buffer-invisibility-spec)
  8179. (org-back-to-heading t)
  8180. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8181. (goto-char (match-end 1))
  8182. (setq col (current-column))
  8183. (goto-char (match-end 0))
  8184. (if (eobp) (insert "\n") (forward-char 1))
  8185. (if (and (not (looking-at outline-regexp))
  8186. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8187. "[^\r\n]*"))
  8188. (not (equal (match-string 1) org-clock-string)))
  8189. (narrow-to-region (match-beginning 0) (match-end 0))
  8190. (insert-before-markers "\n")
  8191. (backward-char 1)
  8192. (narrow-to-region (point) (point))
  8193. (and org-adapt-indentation (org-indent-to-column col)))
  8194. ;; Check if we have to remove something.
  8195. (setq list (cons what remove))
  8196. (while list
  8197. (setq elt (pop list))
  8198. (goto-char (point-min))
  8199. (when (or (and (eq elt 'scheduled)
  8200. (re-search-forward org-scheduled-time-regexp nil t))
  8201. (and (eq elt 'deadline)
  8202. (re-search-forward org-deadline-time-regexp nil t))
  8203. (and (eq elt 'closed)
  8204. (re-search-forward org-closed-time-regexp nil t)))
  8205. (replace-match "")
  8206. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8207. (if (looking-at " +") (replace-match ""))))
  8208. (goto-char (point-max))
  8209. (when what
  8210. (insert
  8211. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8212. (cond ((eq what 'scheduled) org-scheduled-string)
  8213. ((eq what 'deadline) org-deadline-string)
  8214. ((eq what 'closed) org-closed-string))
  8215. " ")
  8216. (setq ts (org-insert-time-stamp
  8217. time
  8218. (or org-time-was-given
  8219. (and (eq what 'closed) org-log-done-with-time))
  8220. (eq what 'closed)
  8221. nil nil (list org-end-time-was-given)))
  8222. (end-of-line 1))
  8223. (goto-char (point-min))
  8224. (widen)
  8225. (if (and (looking-at "[ \t]+\n")
  8226. (equal (char-before) ?\n))
  8227. (delete-region (1- (point)) (point-at-eol)))
  8228. ts)))))
  8229. (defvar org-log-note-marker (make-marker))
  8230. (defvar org-log-note-purpose nil)
  8231. (defvar org-log-note-state nil)
  8232. (defvar org-log-note-how nil)
  8233. (defvar org-log-note-extra nil)
  8234. (defvar org-log-note-window-configuration nil)
  8235. (defvar org-log-note-return-to (make-marker))
  8236. (defvar org-log-post-message nil
  8237. "Message to be displayed after a log note has been stored.
  8238. The auto-repeater uses this.")
  8239. (defun org-add-note ()
  8240. "Add a note to the current entry.
  8241. This is done in the same way as adding a state change note."
  8242. (interactive)
  8243. (org-add-log-setup 'note nil 'findpos nil))
  8244. (defvar org-property-end-re)
  8245. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  8246. "Set up the post command hook to take a note.
  8247. If this is about to TODO state change, the new state is expected in STATE.
  8248. When FINDPOS is non-nil, find the correct position for the note in
  8249. the current entry. If not, assume that it can be inserted at point.
  8250. HOW is an indicator what kind of note should be created.
  8251. EXTRA is additional text that will be inserted into the notes buffer."
  8252. (save-restriction
  8253. (save-excursion
  8254. (when findpos
  8255. (org-back-to-heading t)
  8256. (narrow-to-region (point) (save-excursion
  8257. (outline-next-heading) (point)))
  8258. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8259. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8260. "[^\r\n]*\\)?"))
  8261. (goto-char (match-end 0))
  8262. (when (and org-log-state-notes-insert-after-drawers
  8263. (save-excursion
  8264. (forward-line) (looking-at org-drawer-regexp)))
  8265. (progn (forward-line)
  8266. (while (looking-at org-drawer-regexp)
  8267. (goto-char (match-end 0))
  8268. (re-search-forward org-property-end-re (point-max) t)
  8269. (forward-line))
  8270. (forward-line -1)))
  8271. (unless org-log-states-order-reversed
  8272. (and (= (char-after) ?\n) (forward-char 1))
  8273. (org-skip-over-state-notes)
  8274. (skip-chars-backward " \t\n\r")))
  8275. (move-marker org-log-note-marker (point))
  8276. (setq org-log-note-purpose purpose
  8277. org-log-note-state state
  8278. org-log-note-how how
  8279. org-log-note-extra extra)
  8280. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8281. (defun org-skip-over-state-notes ()
  8282. "Skip past the list of State notes in an entry."
  8283. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8284. (while (looking-at "[ \t]*- State")
  8285. (condition-case nil
  8286. (org-next-item)
  8287. (error (org-end-of-item)))))
  8288. (defun org-add-log-note (&optional purpose)
  8289. "Pop up a window for taking a note, and add this note later at point."
  8290. (remove-hook 'post-command-hook 'org-add-log-note)
  8291. (setq org-log-note-window-configuration (current-window-configuration))
  8292. (delete-other-windows)
  8293. (move-marker org-log-note-return-to (point))
  8294. (switch-to-buffer (marker-buffer org-log-note-marker))
  8295. (goto-char org-log-note-marker)
  8296. (org-switch-to-buffer-other-window "*Org Note*")
  8297. (erase-buffer)
  8298. (if (memq org-log-note-how '(time state))
  8299. (let (current-prefix-arg) (org-store-log-note))
  8300. (let ((org-inhibit-startup t)) (org-mode))
  8301. (insert (format "# Insert note for %s.
  8302. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8303. (cond
  8304. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8305. ((eq org-log-note-purpose 'done) "closed todo item")
  8306. ((eq org-log-note-purpose 'state)
  8307. (format "state change to \"%s\"" org-log-note-state))
  8308. ((eq org-log-note-purpose 'note)
  8309. "this entry")
  8310. (t (error "This should not happen")))))
  8311. (if org-log-note-extra (insert org-log-note-extra))
  8312. (org-set-local 'org-finish-function 'org-store-log-note)))
  8313. (defvar org-note-abort nil) ; dynamically scoped
  8314. (defun org-store-log-note ()
  8315. "Finish taking a log note, and insert it to where it belongs."
  8316. (let ((txt (buffer-string))
  8317. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8318. lines ind)
  8319. (kill-buffer (current-buffer))
  8320. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8321. (setq txt (replace-match "" t t txt)))
  8322. (if (string-match "\\s-+\\'" txt)
  8323. (setq txt (replace-match "" t t txt)))
  8324. (setq lines (org-split-string txt "\n"))
  8325. (when (and note (string-match "\\S-" note))
  8326. (setq note
  8327. (org-replace-escapes
  8328. note
  8329. (list (cons "%u" (user-login-name))
  8330. (cons "%U" user-full-name)
  8331. (cons "%t" (format-time-string
  8332. (org-time-stamp-format 'long 'inactive)
  8333. (current-time)))
  8334. (cons "%s" (if org-log-note-state
  8335. (concat "\"" org-log-note-state "\"")
  8336. "")))))
  8337. (if lines (setq note (concat note " \\\\")))
  8338. (push note lines))
  8339. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8340. (when lines
  8341. (save-excursion
  8342. (set-buffer (marker-buffer org-log-note-marker))
  8343. (save-excursion
  8344. (goto-char org-log-note-marker)
  8345. (move-marker org-log-note-marker nil)
  8346. (end-of-line 1)
  8347. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8348. (indent-relative nil)
  8349. (insert "- " (pop lines))
  8350. (org-indent-line-function)
  8351. (beginning-of-line 1)
  8352. (looking-at "[ \t]*")
  8353. (setq ind (concat (match-string 0) " "))
  8354. (end-of-line 1)
  8355. (while lines (insert "\n" ind (pop lines)))))))
  8356. (set-window-configuration org-log-note-window-configuration)
  8357. (with-current-buffer (marker-buffer org-log-note-return-to)
  8358. (goto-char org-log-note-return-to))
  8359. (move-marker org-log-note-return-to nil)
  8360. (and org-log-post-message (message "%s" org-log-post-message)))
  8361. (defun org-sparse-tree (&optional arg)
  8362. "Create a sparse tree, prompt for the details.
  8363. This command can create sparse trees. You first need to select the type
  8364. of match used to create the tree:
  8365. t Show entries with a specific TODO keyword.
  8366. T Show entries selected by a tags match.
  8367. p Enter a property name and its value (both with completion on existing
  8368. names/values) and show entries with that property.
  8369. r Show entries matching a regular expression
  8370. d Show deadlines due within `org-deadline-warning-days'."
  8371. (interactive "P")
  8372. (let (ans kwd value)
  8373. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8374. (setq ans (read-char-exclusive))
  8375. (cond
  8376. ((equal ans ?d)
  8377. (call-interactively 'org-check-deadlines))
  8378. ((equal ans ?b)
  8379. (call-interactively 'org-check-before-date))
  8380. ((equal ans ?t)
  8381. (org-show-todo-tree '(4)))
  8382. ((equal ans ?T)
  8383. (call-interactively 'org-tags-sparse-tree))
  8384. ((member ans '(?p ?P))
  8385. (setq kwd (org-ido-completing-read "Property: "
  8386. (mapcar 'list (org-buffer-property-keys))))
  8387. (setq value (org-ido-completing-read "Value: "
  8388. (mapcar 'list (org-property-values kwd))))
  8389. (unless (string-match "\\`{.*}\\'" value)
  8390. (setq value (concat "\"" value "\"")))
  8391. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8392. ((member ans '(?r ?R ?/))
  8393. (call-interactively 'org-occur))
  8394. (t (error "No such sparse tree command \"%c\"" ans)))))
  8395. (defvar org-occur-highlights nil
  8396. "List of overlays used for occur matches.")
  8397. (make-variable-buffer-local 'org-occur-highlights)
  8398. (defvar org-occur-parameters nil
  8399. "Parameters of the active org-occur calls.
  8400. This is a list, each call to org-occur pushes as cons cell,
  8401. containing the regular expression and the callback, onto the list.
  8402. The list can contain several entries if `org-occur' has been called
  8403. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8404. will only contain one set of parameters. When the highlights are
  8405. removed (for example with `C-c C-c', or with the next edit (depending
  8406. on `org-remove-highlights-with-change'), this variable is emptied
  8407. as well.")
  8408. (make-variable-buffer-local 'org-occur-parameters)
  8409. (defun org-occur (regexp &optional keep-previous callback)
  8410. "Make a compact tree which shows all matches of REGEXP.
  8411. The tree will show the lines where the regexp matches, and all higher
  8412. headlines above the match. It will also show the heading after the match,
  8413. to make sure editing the matching entry is easy.
  8414. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8415. call to `org-occur' will be kept, to allow stacking of calls to this
  8416. command.
  8417. If CALLBACK is non-nil, it is a function which is called to confirm
  8418. that the match should indeed be shown."
  8419. (interactive "sRegexp: \nP")
  8420. (unless keep-previous
  8421. (org-remove-occur-highlights nil nil t))
  8422. (push (cons regexp callback) org-occur-parameters)
  8423. (let ((cnt 0))
  8424. (save-excursion
  8425. (goto-char (point-min))
  8426. (if (or (not keep-previous) ; do not want to keep
  8427. (not org-occur-highlights)) ; no previous matches
  8428. ;; hide everything
  8429. (org-overview))
  8430. (while (re-search-forward regexp nil t)
  8431. (when (or (not callback)
  8432. (save-match-data (funcall callback)))
  8433. (setq cnt (1+ cnt))
  8434. (when org-highlight-sparse-tree-matches
  8435. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8436. (org-show-context 'occur-tree))))
  8437. (when org-remove-highlights-with-change
  8438. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8439. nil 'local))
  8440. (unless org-sparse-tree-open-archived-trees
  8441. (org-hide-archived-subtrees (point-min) (point-max)))
  8442. (run-hooks 'org-occur-hook)
  8443. (if (interactive-p)
  8444. (message "%d match(es) for regexp %s" cnt regexp))
  8445. cnt))
  8446. (defun org-show-context (&optional key)
  8447. "Make sure point and context and visible.
  8448. How much context is shown depends upon the variables
  8449. `org-show-hierarchy-above', `org-show-following-heading'. and
  8450. `org-show-siblings'."
  8451. (let ((heading-p (org-on-heading-p t))
  8452. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8453. (following-p (org-get-alist-option org-show-following-heading key))
  8454. (entry-p (org-get-alist-option org-show-entry-below key))
  8455. (siblings-p (org-get-alist-option org-show-siblings key)))
  8456. (catch 'exit
  8457. ;; Show heading or entry text
  8458. (if (and heading-p (not entry-p))
  8459. (org-flag-heading nil) ; only show the heading
  8460. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8461. (org-show-hidden-entry))) ; show entire entry
  8462. (when following-p
  8463. ;; Show next sibling, or heading below text
  8464. (save-excursion
  8465. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8466. (org-flag-heading nil))))
  8467. (when siblings-p (org-show-siblings))
  8468. (when hierarchy-p
  8469. ;; show all higher headings, possibly with siblings
  8470. (save-excursion
  8471. (while (and (condition-case nil
  8472. (progn (org-up-heading-all 1) t)
  8473. (error nil))
  8474. (not (bobp)))
  8475. (org-flag-heading nil)
  8476. (when siblings-p (org-show-siblings))))))))
  8477. (defun org-reveal (&optional siblings)
  8478. "Show current entry, hierarchy above it, and the following headline.
  8479. This can be used to show a consistent set of context around locations
  8480. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8481. not t for the search context.
  8482. With optional argument SIBLINGS, on each level of the hierarchy all
  8483. siblings are shown. This repairs the tree structure to what it would
  8484. look like when opened with hierarchical calls to `org-cycle'."
  8485. (interactive "P")
  8486. (let ((org-show-hierarchy-above t)
  8487. (org-show-following-heading t)
  8488. (org-show-siblings (if siblings t org-show-siblings)))
  8489. (org-show-context nil)))
  8490. (defun org-highlight-new-match (beg end)
  8491. "Highlight from BEG to END and mark the highlight is an occur headline."
  8492. (let ((ov (org-make-overlay beg end)))
  8493. (org-overlay-put ov 'face 'secondary-selection)
  8494. (push ov org-occur-highlights)))
  8495. (defun org-remove-occur-highlights (&optional beg end noremove)
  8496. "Remove the occur highlights from the buffer.
  8497. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8498. from the `before-change-functions' in the current buffer."
  8499. (interactive)
  8500. (unless org-inhibit-highlight-removal
  8501. (mapc 'org-delete-overlay org-occur-highlights)
  8502. (setq org-occur-highlights nil)
  8503. (setq org-occur-parameters nil)
  8504. (unless noremove
  8505. (remove-hook 'before-change-functions
  8506. 'org-remove-occur-highlights 'local))))
  8507. ;;;; Priorities
  8508. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8509. "Regular expression matching the priority indicator.")
  8510. (defvar org-remove-priority-next-time nil)
  8511. (defun org-priority-up ()
  8512. "Increase the priority of the current item."
  8513. (interactive)
  8514. (org-priority 'up))
  8515. (defun org-priority-down ()
  8516. "Decrease the priority of the current item."
  8517. (interactive)
  8518. (org-priority 'down))
  8519. (defun org-priority (&optional action)
  8520. "Change the priority of an item by ARG.
  8521. ACTION can be `set', `up', `down', or a character."
  8522. (interactive)
  8523. (setq action (or action 'set))
  8524. (let (current new news have remove)
  8525. (save-excursion
  8526. (org-back-to-heading)
  8527. (if (looking-at org-priority-regexp)
  8528. (setq current (string-to-char (match-string 2))
  8529. have t)
  8530. (setq current org-default-priority))
  8531. (cond
  8532. ((or (eq action 'set)
  8533. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8534. (if (not (eq action 'set))
  8535. (setq new action)
  8536. (message "Priority %c-%c, SPC to remove: "
  8537. org-highest-priority org-lowest-priority)
  8538. (setq new (read-char-exclusive)))
  8539. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8540. (= (upcase org-lowest-priority) org-lowest-priority))
  8541. (setq new (upcase new)))
  8542. (cond ((equal new ?\ ) (setq remove t))
  8543. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8544. (error "Priority must be between `%c' and `%c'"
  8545. org-highest-priority org-lowest-priority))))
  8546. ((eq action 'up)
  8547. (if (and (not have) (eq last-command this-command))
  8548. (setq new org-lowest-priority)
  8549. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8550. org-default-priority (1- current)))))
  8551. ((eq action 'down)
  8552. (if (and (not have) (eq last-command this-command))
  8553. (setq new org-highest-priority)
  8554. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8555. org-default-priority (1+ current)))))
  8556. (t (error "Invalid action")))
  8557. (if (or (< (upcase new) org-highest-priority)
  8558. (> (upcase new) org-lowest-priority))
  8559. (setq remove t))
  8560. (setq news (format "%c" new))
  8561. (if have
  8562. (if remove
  8563. (replace-match "" t t nil 1)
  8564. (replace-match news t t nil 2))
  8565. (if remove
  8566. (error "No priority cookie found in line")
  8567. (looking-at org-todo-line-regexp)
  8568. (if (match-end 2)
  8569. (progn
  8570. (goto-char (match-end 2))
  8571. (insert " [#" news "]"))
  8572. (goto-char (match-beginning 3))
  8573. (insert "[#" news "] ")))))
  8574. (org-preserve-lc (org-set-tags nil 'align))
  8575. (if remove
  8576. (message "Priority removed")
  8577. (message "Priority of current item set to %s" news))))
  8578. (defun org-get-priority (s)
  8579. "Find priority cookie and return priority."
  8580. (save-match-data
  8581. (if (not (string-match org-priority-regexp s))
  8582. (* 1000 (- org-lowest-priority org-default-priority))
  8583. (* 1000 (- org-lowest-priority
  8584. (string-to-char (match-string 2 s)))))))
  8585. ;;;; Tags
  8586. (defvar org-agenda-archives-mode)
  8587. (defun org-scan-tags (action matcher &optional todo-only)
  8588. "Scan headline tags with inheritance and produce output ACTION.
  8589. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8590. or `agenda' to produce an entry list for an agenda view. It can also be
  8591. a Lisp form or a function that should be called at each matched headline, in
  8592. this case the return value is a list of all return values from these calls.
  8593. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8594. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8595. only lines with a TODO keyword are included in the output."
  8596. (require 'org-agenda)
  8597. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8598. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8599. (org-re
  8600. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8601. (props (list 'face 'default
  8602. 'done-face 'org-done
  8603. 'undone-face 'default
  8604. 'mouse-face 'highlight
  8605. 'org-not-done-regexp org-not-done-regexp
  8606. 'org-todo-regexp org-todo-regexp
  8607. 'keymap org-agenda-keymap
  8608. 'help-echo
  8609. (format "mouse-2 or RET jump to org file %s"
  8610. (abbreviate-file-name
  8611. (or (buffer-file-name (buffer-base-buffer))
  8612. (buffer-name (buffer-base-buffer)))))))
  8613. (case-fold-search nil)
  8614. lspos tags tags-list
  8615. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8616. (llast 0) rtn rtn1 level category i txt
  8617. todo marker entry priority)
  8618. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8619. (setq action (list 'lambda nil action)))
  8620. (save-excursion
  8621. (goto-char (point-min))
  8622. (when (eq action 'sparse-tree)
  8623. (org-overview)
  8624. (org-remove-occur-highlights))
  8625. (while (re-search-forward re nil t)
  8626. (catch :skip
  8627. (setq todo (if (match-end 1) (match-string 2))
  8628. tags (if (match-end 4) (match-string 4)))
  8629. (goto-char (setq lspos (1+ (match-beginning 0))))
  8630. (setq level (org-reduced-level (funcall outline-level))
  8631. category (org-get-category))
  8632. (setq i llast llast level)
  8633. ;; remove tag lists from same and sublevels
  8634. (while (>= i level)
  8635. (when (setq entry (assoc i tags-alist))
  8636. (setq tags-alist (delete entry tags-alist)))
  8637. (setq i (1- i)))
  8638. ;; add the next tags
  8639. (when tags
  8640. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8641. tags-alist
  8642. (cons (cons level tags) tags-alist)))
  8643. ;; compile tags for current headline
  8644. (setq tags-list
  8645. (if org-use-tag-inheritance
  8646. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8647. tags))
  8648. (when org-use-tag-inheritance
  8649. (setcdr (car tags-alist)
  8650. (mapcar (lambda (x)
  8651. (setq x (copy-sequence x))
  8652. (org-add-prop-inherited x))
  8653. (cdar tags-alist))))
  8654. (when (and tags org-use-tag-inheritance
  8655. (not (eq t org-use-tag-inheritance)))
  8656. ;; selective inheritance, remove uninherited ones
  8657. (setcdr (car tags-alist)
  8658. (org-remove-uniherited-tags (cdar tags-alist))))
  8659. (when (and (or (not todo-only)
  8660. (and (member todo org-not-done-keywords)
  8661. (or (not org-agenda-tags-todo-honor-ignore-options)
  8662. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8663. (let ((case-fold-search t)) (eval matcher))
  8664. (or
  8665. (not (member org-archive-tag tags-list))
  8666. ;; we have an archive tag, should we use this anyway?
  8667. (or (not org-agenda-skip-archived-trees)
  8668. (and (eq action 'agenda) org-agenda-archives-mode))))
  8669. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8670. ;; select this headline
  8671. (cond
  8672. ((eq action 'sparse-tree)
  8673. (and org-highlight-sparse-tree-matches
  8674. (org-get-heading) (match-end 0)
  8675. (org-highlight-new-match
  8676. (match-beginning 0) (match-beginning 1)))
  8677. (org-show-context 'tags-tree))
  8678. ((eq action 'agenda)
  8679. (setq txt (org-format-agenda-item
  8680. ""
  8681. (concat
  8682. (if org-tags-match-list-sublevels
  8683. (make-string (1- level) ?.) "")
  8684. (org-get-heading))
  8685. category (org-get-tags-at))
  8686. priority (org-get-priority txt))
  8687. (goto-char lspos)
  8688. (setq marker (org-agenda-new-marker))
  8689. (org-add-props txt props
  8690. 'org-marker marker 'org-hd-marker marker 'org-category category
  8691. 'priority priority 'type "tagsmatch")
  8692. (push txt rtn))
  8693. ((functionp action)
  8694. (save-excursion
  8695. (setq rtn1 (funcall action))
  8696. (push rtn1 rtn))
  8697. (goto-char (point-at-eol)))
  8698. (t (error "Invalid action")))
  8699. ;; if we are to skip sublevels, jump to end of subtree
  8700. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8701. (when (and (eq action 'sparse-tree)
  8702. (not org-sparse-tree-open-archived-trees))
  8703. (org-hide-archived-subtrees (point-min) (point-max)))
  8704. (nreverse rtn)))
  8705. (defun org-remove-uniherited-tags (tags)
  8706. "Remove all tags that are not inherited from the list TAGS."
  8707. (cond
  8708. ((eq org-use-tag-inheritance t)
  8709. (if org-tags-exclude-from-inheritance
  8710. (org-delete-all org-tags-exclude-from-inheritance tags)
  8711. tags))
  8712. ((not org-use-tag-inheritance) nil)
  8713. ((stringp org-use-tag-inheritance)
  8714. (delq nil (mapcar
  8715. (lambda (x)
  8716. (if (and (string-match org-use-tag-inheritance x)
  8717. (not (member x org-tags-exclude-from-inheritance)))
  8718. x nil))
  8719. tags)))
  8720. ((listp org-use-tag-inheritance)
  8721. (delq nil (mapcar
  8722. (lambda (x)
  8723. (if (member x org-use-tag-inheritance) x nil))
  8724. tags)))))
  8725. (defvar todo-only) ;; dynamically scoped
  8726. (defun org-tags-sparse-tree (&optional todo-only match)
  8727. "Create a sparse tree according to tags string MATCH.
  8728. MATCH can contain positive and negative selection of tags, like
  8729. \"+WORK+URGENT-WITHBOSS\".
  8730. If optional argument TODO-ONLY is non-nil, only select lines that are
  8731. also TODO lines."
  8732. (interactive "P")
  8733. (org-prepare-agenda-buffers (list (current-buffer)))
  8734. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8735. (defvar org-cached-props nil)
  8736. (defun org-cached-entry-get (pom property)
  8737. (if (or (eq t org-use-property-inheritance)
  8738. (and (stringp org-use-property-inheritance)
  8739. (string-match org-use-property-inheritance property))
  8740. (and (listp org-use-property-inheritance)
  8741. (member property org-use-property-inheritance)))
  8742. ;; Caching is not possible, check it directly
  8743. (org-entry-get pom property 'inherit)
  8744. ;; Get all properties, so that we can do complicated checks easily
  8745. (cdr (assoc property (or org-cached-props
  8746. (setq org-cached-props
  8747. (org-entry-properties pom)))))))
  8748. (defun org-global-tags-completion-table (&optional files)
  8749. "Return the list of all tags in all agenda buffer/files."
  8750. (save-excursion
  8751. (org-uniquify
  8752. (delq nil
  8753. (apply 'append
  8754. (mapcar
  8755. (lambda (file)
  8756. (set-buffer (find-file-noselect file))
  8757. (append (org-get-buffer-tags)
  8758. (mapcar (lambda (x) (if (stringp (car-safe x))
  8759. (list (car-safe x)) nil))
  8760. org-tag-alist)))
  8761. (if (and files (car files))
  8762. files
  8763. (org-agenda-files))))))))
  8764. (defun org-make-tags-matcher (match)
  8765. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8766. ;; todo-only is scoped dynamically into this function, and the function
  8767. ;; may change it if the matcher asks for it.
  8768. (unless match
  8769. ;; Get a new match request, with completion
  8770. (let ((org-last-tags-completion-table
  8771. (org-global-tags-completion-table)))
  8772. (setq match (org-completing-read
  8773. "Match: " 'org-tags-completion-function nil nil nil
  8774. 'org-tags-history))))
  8775. ;; Parse the string and create a lisp form
  8776. (let ((match0 match)
  8777. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8778. minus tag mm
  8779. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8780. orterms term orlist re-p str-p level-p level-op time-p
  8781. prop-p pn pv po cat-p gv rest)
  8782. (if (string-match "/+" match)
  8783. ;; match contains also a todo-matching request
  8784. (progn
  8785. (setq tagsmatch (substring match 0 (match-beginning 0))
  8786. todomatch (substring match (match-end 0)))
  8787. (if (string-match "^!" todomatch)
  8788. (setq todo-only t todomatch (substring todomatch 1)))
  8789. (if (string-match "^\\s-*$" todomatch)
  8790. (setq todomatch nil)))
  8791. ;; only matching tags
  8792. (setq tagsmatch match todomatch nil))
  8793. ;; Make the tags matcher
  8794. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8795. (setq tagsmatcher t)
  8796. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8797. (while (setq term (pop orterms))
  8798. (while (and (equal (substring term -1) "\\") orterms)
  8799. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8800. (while (string-match re term)
  8801. (setq rest (substring term (match-end 0))
  8802. minus (and (match-end 1)
  8803. (equal (match-string 1 term) "-"))
  8804. tag (match-string 2 term)
  8805. re-p (equal (string-to-char tag) ?{)
  8806. level-p (match-end 4)
  8807. prop-p (match-end 5)
  8808. mm (cond
  8809. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8810. (level-p
  8811. (setq level-op (org-op-to-function (match-string 3 term)))
  8812. `(,level-op level ,(string-to-number
  8813. (match-string 4 term))))
  8814. (prop-p
  8815. (setq pn (match-string 5 term)
  8816. po (match-string 6 term)
  8817. pv (match-string 7 term)
  8818. cat-p (equal pn "CATEGORY")
  8819. re-p (equal (string-to-char pv) ?{)
  8820. str-p (equal (string-to-char pv) ?\")
  8821. time-p (save-match-data
  8822. (string-match "^\"[[<].*[]>]\"$" pv))
  8823. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8824. (if time-p (setq pv (org-matcher-time pv)))
  8825. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8826. (cond
  8827. ((equal pn "CATEGORY")
  8828. (setq gv '(get-text-property (point) 'org-category)))
  8829. ((equal pn "TODO")
  8830. (setq gv 'todo))
  8831. (t
  8832. (setq gv `(org-cached-entry-get nil ,pn))))
  8833. (if re-p
  8834. (if (eq po 'org<>)
  8835. `(not (string-match ,pv (or ,gv "")))
  8836. `(string-match ,pv (or ,gv "")))
  8837. (if str-p
  8838. `(,po (or ,gv "") ,pv)
  8839. `(,po (string-to-number (or ,gv ""))
  8840. ,(string-to-number pv) ))))
  8841. (t `(member ,(downcase tag) tags-list)))
  8842. mm (if minus (list 'not mm) mm)
  8843. term rest)
  8844. (push mm tagsmatcher))
  8845. (push (if (> (length tagsmatcher) 1)
  8846. (cons 'and tagsmatcher)
  8847. (car tagsmatcher))
  8848. orlist)
  8849. (setq tagsmatcher nil))
  8850. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8851. (setq tagsmatcher
  8852. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8853. ;; Make the todo matcher
  8854. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8855. (setq todomatcher t)
  8856. (setq orterms (org-split-string todomatch "|") orlist nil)
  8857. (while (setq term (pop orterms))
  8858. (while (string-match re term)
  8859. (setq minus (and (match-end 1)
  8860. (equal (match-string 1 term) "-"))
  8861. kwd (match-string 2 term)
  8862. re-p (equal (string-to-char kwd) ?{)
  8863. term (substring term (match-end 0))
  8864. mm (if re-p
  8865. `(string-match ,(substring kwd 1 -1) todo)
  8866. (list 'equal 'todo kwd))
  8867. mm (if minus (list 'not mm) mm))
  8868. (push mm todomatcher))
  8869. (push (if (> (length todomatcher) 1)
  8870. (cons 'and todomatcher)
  8871. (car todomatcher))
  8872. orlist)
  8873. (setq todomatcher nil))
  8874. (setq todomatcher (if (> (length orlist) 1)
  8875. (cons 'or orlist) (car orlist))))
  8876. ;; Return the string and lisp forms of the matcher
  8877. (setq matcher (if todomatcher
  8878. (list 'and tagsmatcher todomatcher)
  8879. tagsmatcher))
  8880. (cons match0 matcher)))
  8881. (defun org-op-to-function (op &optional stringp)
  8882. "Turn an operator into the appropriate function."
  8883. (setq op
  8884. (cond
  8885. ((equal op "<" ) '(< string< org-time<))
  8886. ((equal op ">" ) '(> org-string> org-time>))
  8887. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8888. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8889. ((member op '("=" "==")) '(= string= org-time=))
  8890. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8891. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8892. (defun org<> (a b) (not (= a b)))
  8893. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8894. (defun org-string>= (a b) (not (string< a b)))
  8895. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8896. (defun org-string<> (a b) (not (string= a b)))
  8897. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  8898. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  8899. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  8900. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  8901. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  8902. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  8903. (defun org-2ft (s)
  8904. "Convert S to a floating point time.
  8905. If S is already a number, just return it. If it is a string, parse
  8906. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  8907. (cond
  8908. ((numberp s) s)
  8909. ((stringp s)
  8910. (condition-case nil
  8911. (float-time (apply 'encode-time (org-parse-time-string s)))
  8912. (error 0.)))
  8913. (t 0.)))
  8914. (defun org-time-today ()
  8915. "Time in seconds today at 0:00.
  8916. Returns the float number of seconds since the beginning of the
  8917. epoch to the beginning of today (00:00)."
  8918. (float-time (apply 'encode-time
  8919. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8920. (defun org-matcher-time (s)
  8921. "Interpret a time comparison value."
  8922. (save-match-data
  8923. (cond
  8924. ((string= s "<now>") (float-time))
  8925. ((string= s "<today>") (org-time-today))
  8926. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8927. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8928. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8929. (+ (org-time-today)
  8930. (* (string-to-number (match-string 1 s))
  8931. (cdr (assoc (match-string 2 s)
  8932. '(("d" . 86400.0) ("w" . 604800.0)
  8933. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8934. (t (org-2ft s)))))
  8935. (defun org-match-any-p (re list)
  8936. "Does re match any element of list?"
  8937. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8938. (delq nil list))
  8939. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8940. (defvar org-tags-overlay (org-make-overlay 1 1))
  8941. (org-detach-overlay org-tags-overlay)
  8942. (defun org-get-local-tags-at (&optional pos)
  8943. "Get a list of tags defined in the current headline."
  8944. (org-get-tags-at pos 'local))
  8945. (defun org-get-local-tags ()
  8946. "Get a list of tags defined in the current headline."
  8947. (org-get-tags-at nil 'local))
  8948. (defun org-get-tags-at (&optional pos local)
  8949. "Get a list of all headline tags applicable at POS.
  8950. POS defaults to point. If tags are inherited, the list contains
  8951. the targets in the same sequence as the headlines appear, i.e.
  8952. the tags of the current headline come last.
  8953. When LOCAL is non-nil, only return tags from the current headline,
  8954. ignore inherited ones."
  8955. (interactive)
  8956. (let (tags ltags lastpos parent)
  8957. (save-excursion
  8958. (save-restriction
  8959. (widen)
  8960. (goto-char (or pos (point)))
  8961. (save-match-data
  8962. (catch 'done
  8963. (condition-case nil
  8964. (progn
  8965. (org-back-to-heading t)
  8966. (while (not (equal lastpos (point)))
  8967. (setq lastpos (point))
  8968. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8969. (setq ltags (org-split-string
  8970. (org-match-string-no-properties 1) ":"))
  8971. (when parent
  8972. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8973. (setq tags (append
  8974. (if parent
  8975. (org-remove-uniherited-tags ltags)
  8976. ltags)
  8977. tags)))
  8978. (or org-use-tag-inheritance (throw 'done t))
  8979. (if local (throw 'done t))
  8980. (org-up-heading-all 1)
  8981. (setq parent t)))
  8982. (error nil)))))
  8983. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8984. (defun org-add-prop-inherited (s)
  8985. (add-text-properties 0 (length s) '(inherited t) s)
  8986. s)
  8987. (defun org-toggle-tag (tag &optional onoff)
  8988. "Toggle the tag TAG for the current line.
  8989. If ONOFF is `on' or `off', don't toggle but set to this state."
  8990. (let (res current)
  8991. (save-excursion
  8992. (org-back-to-heading t)
  8993. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8994. (point-at-eol) t)
  8995. (progn
  8996. (setq current (match-string 1))
  8997. (replace-match ""))
  8998. (setq current ""))
  8999. (setq current (nreverse (org-split-string current ":")))
  9000. (cond
  9001. ((eq onoff 'on)
  9002. (setq res t)
  9003. (or (member tag current) (push tag current)))
  9004. ((eq onoff 'off)
  9005. (or (not (member tag current)) (setq current (delete tag current))))
  9006. (t (if (member tag current)
  9007. (setq current (delete tag current))
  9008. (setq res t)
  9009. (push tag current))))
  9010. (end-of-line 1)
  9011. (if current
  9012. (progn
  9013. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9014. (org-set-tags nil t))
  9015. (delete-horizontal-space))
  9016. (run-hooks 'org-after-tags-change-hook))
  9017. res))
  9018. (defun org-align-tags-here (to-col)
  9019. ;; Assumes that this is a headline
  9020. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9021. (beginning-of-line 1)
  9022. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9023. (< pos (match-beginning 2)))
  9024. (progn
  9025. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9026. (goto-char (match-beginning 1))
  9027. (insert " ")
  9028. (delete-region (point) (1+ (match-beginning 2)))
  9029. (setq ncol (max (1+ (current-column))
  9030. (1+ col)
  9031. (if (> to-col 0)
  9032. to-col
  9033. (- (abs to-col) tags-l))))
  9034. (setq p (point))
  9035. (insert (make-string (- ncol (current-column)) ?\ ))
  9036. (setq ncol (current-column))
  9037. (when indent-tabs-mode (tabify p (point-at-eol)))
  9038. (org-move-to-column (min ncol col) t))
  9039. (goto-char pos))))
  9040. (defun org-set-tags-command (&optional arg just-align)
  9041. "Call the set-tags command for the current entry."
  9042. (interactive "P")
  9043. (if (org-on-heading-p)
  9044. (org-set-tags arg just-align)
  9045. (save-excursion
  9046. (org-back-to-heading t)
  9047. (org-set-tags arg just-align))))
  9048. (defun org-set-tags (&optional arg just-align)
  9049. "Set the tags for the current headline.
  9050. With prefix ARG, realign all tags in headings in the current buffer."
  9051. (interactive "P")
  9052. (let* ((re (concat "^" outline-regexp))
  9053. (current (org-get-tags-string))
  9054. (col (current-column))
  9055. (org-setting-tags t)
  9056. table current-tags inherited-tags ; computed below when needed
  9057. tags p0 c0 c1 rpl)
  9058. (if arg
  9059. (save-excursion
  9060. (goto-char (point-min))
  9061. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9062. (while (re-search-forward re nil t)
  9063. (org-set-tags nil t)
  9064. (end-of-line 1)))
  9065. (message "All tags realigned to column %d" org-tags-column))
  9066. (if just-align
  9067. (setq tags current)
  9068. ;; Get a new set of tags from the user
  9069. (save-excursion
  9070. (setq table (or org-tag-alist (org-get-buffer-tags))
  9071. org-last-tags-completion-table table
  9072. current-tags (org-split-string current ":")
  9073. inherited-tags (nreverse
  9074. (nthcdr (length current-tags)
  9075. (nreverse (org-get-tags-at))))
  9076. tags
  9077. (if (or (eq t org-use-fast-tag-selection)
  9078. (and org-use-fast-tag-selection
  9079. (delq nil (mapcar 'cdr table))))
  9080. (org-fast-tag-selection
  9081. current-tags inherited-tags table
  9082. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9083. (let ((org-add-colon-after-tag-completion t))
  9084. (org-trim
  9085. (org-without-partial-completion
  9086. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9087. nil nil current 'org-tags-history)))))))
  9088. (while (string-match "[-+&]+" tags)
  9089. ;; No boolean logic, just a list
  9090. (setq tags (replace-match ":" t t tags))))
  9091. (if (string-match "\\`[\t ]*\\'" tags)
  9092. (setq tags "")
  9093. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9094. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9095. ;; Insert new tags at the correct column
  9096. (beginning-of-line 1)
  9097. (cond
  9098. ((and (equal current "") (equal tags "")))
  9099. ((re-search-forward
  9100. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9101. (point-at-eol) t)
  9102. (if (equal tags "")
  9103. (setq rpl "")
  9104. (goto-char (match-beginning 0))
  9105. (setq c0 (current-column) p0 (point)
  9106. c1 (max (1+ c0) (if (> org-tags-column 0)
  9107. org-tags-column
  9108. (- (- org-tags-column) (length tags))))
  9109. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9110. (replace-match rpl t t)
  9111. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9112. tags)
  9113. (t (error "Tags alignment failed")))
  9114. (org-move-to-column col)
  9115. (unless just-align
  9116. (run-hooks 'org-after-tags-change-hook)))))
  9117. (defun org-change-tag-in-region (beg end tag off)
  9118. "Add or remove TAG for each entry in the region.
  9119. This works in the agenda, and also in an org-mode buffer."
  9120. (interactive
  9121. (list (region-beginning) (region-end)
  9122. (let ((org-last-tags-completion-table
  9123. (if (org-mode-p)
  9124. (org-get-buffer-tags)
  9125. (org-global-tags-completion-table))))
  9126. (org-ido-completing-read
  9127. "Tag: " 'org-tags-completion-function nil nil nil
  9128. 'org-tags-history))
  9129. (progn
  9130. (message "[s]et or [r]emove? ")
  9131. (equal (read-char-exclusive) ?r))))
  9132. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9133. (let ((agendap (equal major-mode 'org-agenda-mode))
  9134. l1 l2 m buf pos newhead (cnt 0))
  9135. (goto-char end)
  9136. (setq l2 (1- (org-current-line)))
  9137. (goto-char beg)
  9138. (setq l1 (org-current-line))
  9139. (loop for l from l1 to l2 do
  9140. (goto-line l)
  9141. (setq m (get-text-property (point) 'org-hd-marker))
  9142. (when (or (and (org-mode-p) (org-on-heading-p))
  9143. (and agendap m))
  9144. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9145. pos (if agendap m (point)))
  9146. (with-current-buffer buf
  9147. (save-excursion
  9148. (save-restriction
  9149. (goto-char pos)
  9150. (setq cnt (1+ cnt))
  9151. (org-toggle-tag tag (if off 'off 'on))
  9152. (setq newhead (org-get-heading)))))
  9153. (and agendap (org-agenda-change-all-lines newhead m))))
  9154. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9155. (defun org-tags-completion-function (string predicate &optional flag)
  9156. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9157. (confirm (lambda (x) (stringp (car x)))))
  9158. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9159. (setq s1 (match-string 1 string)
  9160. s2 (match-string 2 string))
  9161. (setq s1 "" s2 string))
  9162. (cond
  9163. ((eq flag nil)
  9164. ;; try completion
  9165. (setq rtn (try-completion s2 ctable confirm))
  9166. (if (stringp rtn)
  9167. (setq rtn
  9168. (concat s1 s2 (substring rtn (length s2))
  9169. (if (and org-add-colon-after-tag-completion
  9170. (assoc rtn ctable))
  9171. ":" ""))))
  9172. rtn)
  9173. ((eq flag t)
  9174. ;; all-completions
  9175. (all-completions s2 ctable confirm)
  9176. )
  9177. ((eq flag 'lambda)
  9178. ;; exact match?
  9179. (assoc s2 ctable)))
  9180. ))
  9181. (defun org-fast-tag-insert (kwd tags face &optional end)
  9182. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9183. (insert (format "%-12s" (concat kwd ":"))
  9184. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9185. (or end "")))
  9186. (defun org-fast-tag-show-exit (flag)
  9187. (save-excursion
  9188. (goto-line 3)
  9189. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9190. (replace-match ""))
  9191. (when flag
  9192. (end-of-line 1)
  9193. (org-move-to-column (- (window-width) 19) t)
  9194. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9195. (defun org-set-current-tags-overlay (current prefix)
  9196. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9197. (if (featurep 'xemacs)
  9198. (org-overlay-display org-tags-overlay (concat prefix s)
  9199. 'secondary-selection)
  9200. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9201. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9202. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9203. "Fast tag selection with single keys.
  9204. CURRENT is the current list of tags in the headline, INHERITED is the
  9205. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9206. possibly with grouping information. TODO-TABLE is a similar table with
  9207. TODO keywords, should these have keys assigned to them.
  9208. If the keys are nil, a-z are automatically assigned.
  9209. Returns the new tags string, or nil to not change the current settings."
  9210. (let* ((fulltable (append table todo-table))
  9211. (maxlen (apply 'max (mapcar
  9212. (lambda (x)
  9213. (if (stringp (car x)) (string-width (car x)) 0))
  9214. fulltable)))
  9215. (buf (current-buffer))
  9216. (expert (eq org-fast-tag-selection-single-key 'expert))
  9217. (buffer-tags nil)
  9218. (fwidth (+ maxlen 3 1 3))
  9219. (ncol (/ (- (window-width) 4) fwidth))
  9220. (i-face 'org-done)
  9221. (c-face 'org-todo)
  9222. tg cnt e c char c1 c2 ntable tbl rtn
  9223. ov-start ov-end ov-prefix
  9224. (exit-after-next org-fast-tag-selection-single-key)
  9225. (done-keywords org-done-keywords)
  9226. groups ingroup)
  9227. (save-excursion
  9228. (beginning-of-line 1)
  9229. (if (looking-at
  9230. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9231. (setq ov-start (match-beginning 1)
  9232. ov-end (match-end 1)
  9233. ov-prefix "")
  9234. (setq ov-start (1- (point-at-eol))
  9235. ov-end (1+ ov-start))
  9236. (skip-chars-forward "^\n\r")
  9237. (setq ov-prefix
  9238. (concat
  9239. (buffer-substring (1- (point)) (point))
  9240. (if (> (current-column) org-tags-column)
  9241. " "
  9242. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9243. (org-move-overlay org-tags-overlay ov-start ov-end)
  9244. (save-window-excursion
  9245. (if expert
  9246. (set-buffer (get-buffer-create " *Org tags*"))
  9247. (delete-other-windows)
  9248. (split-window-vertically)
  9249. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9250. (erase-buffer)
  9251. (org-set-local 'org-done-keywords done-keywords)
  9252. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9253. (org-fast-tag-insert "Current" current c-face "\n\n")
  9254. (org-fast-tag-show-exit exit-after-next)
  9255. (org-set-current-tags-overlay current ov-prefix)
  9256. (setq tbl fulltable char ?a cnt 0)
  9257. (while (setq e (pop tbl))
  9258. (cond
  9259. ((equal e '(:startgroup))
  9260. (push '() groups) (setq ingroup t)
  9261. (when (not (= cnt 0))
  9262. (setq cnt 0)
  9263. (insert "\n"))
  9264. (insert "{ "))
  9265. ((equal e '(:endgroup))
  9266. (setq ingroup nil cnt 0)
  9267. (insert "}\n"))
  9268. (t
  9269. (setq tg (car e) c2 nil)
  9270. (if (cdr e)
  9271. (setq c (cdr e))
  9272. ;; automatically assign a character.
  9273. (setq c1 (string-to-char
  9274. (downcase (substring
  9275. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9276. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9277. (while (or (rassoc char ntable) (rassoc char table))
  9278. (setq char (1+ char)))
  9279. (setq c2 c1))
  9280. (setq c (or c2 char)))
  9281. (if ingroup (push tg (car groups)))
  9282. (setq tg (org-add-props tg nil 'face
  9283. (cond
  9284. ((not (assoc tg table))
  9285. (org-get-todo-face tg))
  9286. ((member tg current) c-face)
  9287. ((member tg inherited) i-face)
  9288. (t nil))))
  9289. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9290. (insert "[" c "] " tg (make-string
  9291. (- fwidth 4 (length tg)) ?\ ))
  9292. (push (cons tg c) ntable)
  9293. (when (= (setq cnt (1+ cnt)) ncol)
  9294. (insert "\n")
  9295. (if ingroup (insert " "))
  9296. (setq cnt 0)))))
  9297. (setq ntable (nreverse ntable))
  9298. (insert "\n")
  9299. (goto-char (point-min))
  9300. (if (not expert) (org-fit-window-to-buffer))
  9301. (setq rtn
  9302. (catch 'exit
  9303. (while t
  9304. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9305. (if groups " [!] no groups" " [!]groups")
  9306. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9307. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9308. (cond
  9309. ((= c ?\r) (throw 'exit t))
  9310. ((= c ?!)
  9311. (setq groups (not groups))
  9312. (goto-char (point-min))
  9313. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9314. ((= c ?\C-c)
  9315. (if (not expert)
  9316. (org-fast-tag-show-exit
  9317. (setq exit-after-next (not exit-after-next)))
  9318. (setq expert nil)
  9319. (delete-other-windows)
  9320. (split-window-vertically)
  9321. (org-switch-to-buffer-other-window " *Org tags*")
  9322. (org-fit-window-to-buffer)))
  9323. ((or (= c ?\C-g)
  9324. (and (= c ?q) (not (rassoc c ntable))))
  9325. (org-detach-overlay org-tags-overlay)
  9326. (setq quit-flag t))
  9327. ((= c ?\ )
  9328. (setq current nil)
  9329. (if exit-after-next (setq exit-after-next 'now)))
  9330. ((= c ?\t)
  9331. (condition-case nil
  9332. (setq tg (org-ido-completing-read
  9333. "Tag: "
  9334. (or buffer-tags
  9335. (with-current-buffer buf
  9336. (org-get-buffer-tags)))))
  9337. (quit (setq tg "")))
  9338. (when (string-match "\\S-" tg)
  9339. (add-to-list 'buffer-tags (list tg))
  9340. (if (member tg current)
  9341. (setq current (delete tg current))
  9342. (push tg current)))
  9343. (if exit-after-next (setq exit-after-next 'now)))
  9344. ((setq e (rassoc c todo-table) tg (car e))
  9345. (with-current-buffer buf
  9346. (save-excursion (org-todo tg)))
  9347. (if exit-after-next (setq exit-after-next 'now)))
  9348. ((setq e (rassoc c ntable) tg (car e))
  9349. (if (member tg current)
  9350. (setq current (delete tg current))
  9351. (loop for g in groups do
  9352. (if (member tg g)
  9353. (mapc (lambda (x)
  9354. (setq current (delete x current)))
  9355. g)))
  9356. (push tg current))
  9357. (if exit-after-next (setq exit-after-next 'now))))
  9358. ;; Create a sorted list
  9359. (setq current
  9360. (sort current
  9361. (lambda (a b)
  9362. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9363. (if (eq exit-after-next 'now) (throw 'exit t))
  9364. (goto-char (point-min))
  9365. (beginning-of-line 2)
  9366. (delete-region (point) (point-at-eol))
  9367. (org-fast-tag-insert "Current" current c-face)
  9368. (org-set-current-tags-overlay current ov-prefix)
  9369. (while (re-search-forward
  9370. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9371. (setq tg (match-string 1))
  9372. (add-text-properties
  9373. (match-beginning 1) (match-end 1)
  9374. (list 'face
  9375. (cond
  9376. ((member tg current) c-face)
  9377. ((member tg inherited) i-face)
  9378. (t (get-text-property (match-beginning 1) 'face))))))
  9379. (goto-char (point-min)))))
  9380. (org-detach-overlay org-tags-overlay)
  9381. (if rtn
  9382. (mapconcat 'identity current ":")
  9383. nil))))
  9384. (defun org-get-tags-string ()
  9385. "Get the TAGS string in the current headline."
  9386. (unless (org-on-heading-p t)
  9387. (error "Not on a heading"))
  9388. (save-excursion
  9389. (beginning-of-line 1)
  9390. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9391. (org-match-string-no-properties 1)
  9392. "")))
  9393. (defun org-get-tags ()
  9394. "Get the list of tags specified in the current headline."
  9395. (org-split-string (org-get-tags-string) ":"))
  9396. (defun org-get-buffer-tags ()
  9397. "Get a table of all tags used in the buffer, for completion."
  9398. (let (tags)
  9399. (save-excursion
  9400. (goto-char (point-min))
  9401. (while (re-search-forward
  9402. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9403. (when (equal (char-after (point-at-bol 0)) ?*)
  9404. (mapc (lambda (x) (add-to-list 'tags x))
  9405. (org-split-string (org-match-string-no-properties 1) ":")))))
  9406. (mapcar 'list tags)))
  9407. ;;;; The mapping API
  9408. ;;;###autoload
  9409. (defun org-map-entries (func &optional match scope &rest skip)
  9410. "Call FUNC at each headline selected by MATCH in SCOPE.
  9411. FUNC is a function or a lisp form. The function will be called without
  9412. arguments, with the cursor positioned at the beginning of the headline.
  9413. The return values of all calls to the function will be collected and
  9414. returned as a list.
  9415. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9416. Only headlines that are matched by this query will be considered during
  9417. the iteration. When MATCH is nil or t, all headlines will be
  9418. visited by the iteration.
  9419. SCOPE determines the scope of this command. It can be any of:
  9420. nil The current buffer, respecting the restriction if any
  9421. tree The subtree started with the entry at point
  9422. file The current buffer, without restriction
  9423. file-with-archives
  9424. The current buffer, and any archives associated with it
  9425. agenda All agenda files
  9426. agenda-with-archives
  9427. All agenda files with any archive files associated with them
  9428. \(file1 file2 ...)
  9429. If this is a list, all files in the list will be scanned
  9430. The remaining args are treated as settings for the skipping facilities of
  9431. the scanner. The following items can be given here:
  9432. archive skip trees with the archive tag.
  9433. comment skip trees with the COMMENT keyword
  9434. function or Emacs Lisp form:
  9435. will be used as value for `org-agenda-skip-function', so whenever
  9436. the the function returns t, FUNC will not be called for that
  9437. entry and search will continue from the point where the
  9438. function leaves it."
  9439. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9440. (org-agenda-skip-archived-trees (memq 'archive skip))
  9441. (org-agenda-skip-comment-trees (memq 'comment skip))
  9442. (org-agenda-skip-function
  9443. (car (org-delete-all '(comment archive) skip)))
  9444. (org-tags-match-list-sublevels t)
  9445. matcher file res
  9446. org-todo-keywords-for-agenda
  9447. org-done-keywords-for-agenda
  9448. org-todo-keyword-alist-for-agenda
  9449. org-tag-alist-for-agenda)
  9450. (cond
  9451. ((eq match t) (setq matcher t))
  9452. ((eq match nil) (setq matcher t))
  9453. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9454. (save-excursion
  9455. (save-restriction
  9456. (when (eq scope 'tree)
  9457. (org-back-to-heading t)
  9458. (org-narrow-to-subtree)
  9459. (setq scope nil))
  9460. (if (not scope)
  9461. (progn
  9462. (org-prepare-agenda-buffers
  9463. (list (buffer-file-name (current-buffer))))
  9464. (setq res (org-scan-tags func matcher)))
  9465. ;; Get the right scope
  9466. (cond
  9467. ((and scope (listp scope) (symbolp (car scope)))
  9468. (setq scope (eval scope)))
  9469. ((eq scope 'agenda)
  9470. (setq scope (org-agenda-files t)))
  9471. ((eq scope 'agenda-with-archives)
  9472. (setq scope (org-agenda-files t))
  9473. (setq scope (org-add-archive-files scope)))
  9474. ((eq scope 'file)
  9475. (setq scope (list (buffer-file-name))))
  9476. ((eq scope 'file-with-archives)
  9477. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9478. (org-prepare-agenda-buffers scope)
  9479. (while (setq file (pop scope))
  9480. (with-current-buffer (org-find-base-buffer-visiting file)
  9481. (save-excursion
  9482. (save-restriction
  9483. (widen)
  9484. (goto-char (point-min))
  9485. (setq res (append res (org-scan-tags func matcher))))))))))
  9486. res))
  9487. ;;;; Properties
  9488. ;;; Setting and retrieving properties
  9489. (defconst org-special-properties
  9490. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9491. "TIMESTAMP" "TIMESTAMP_IA")
  9492. "The special properties valid in Org-mode.
  9493. These are properties that are not defined in the property drawer,
  9494. but in some other way.")
  9495. (defconst org-default-properties
  9496. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9497. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9498. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9499. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9500. "Some properties that are used by Org-mode for various purposes.
  9501. Being in this list makes sure that they are offered for completion.")
  9502. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9503. "Regular expression matching the first line of a property drawer.")
  9504. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9505. "Regular expression matching the first line of a property drawer.")
  9506. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9507. "Regular expression matching the first line of a property drawer.")
  9508. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9509. "Regular expression matching the first line of a property drawer.")
  9510. (defconst org-property-drawer-re
  9511. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9512. org-property-end-re "\\)\n?")
  9513. "Matches an entire property drawer.")
  9514. (defconst org-clock-drawer-re
  9515. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9516. org-property-end-re "\\)\n?")
  9517. "Matches an entire clock drawer.")
  9518. (defun org-property-action ()
  9519. "Do an action on properties."
  9520. (interactive)
  9521. (let (c)
  9522. (org-at-property-p)
  9523. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9524. (setq c (read-char-exclusive))
  9525. (cond
  9526. ((equal c ?s)
  9527. (call-interactively 'org-set-property))
  9528. ((equal c ?d)
  9529. (call-interactively 'org-delete-property))
  9530. ((equal c ?D)
  9531. (call-interactively 'org-delete-property-globally))
  9532. ((equal c ?c)
  9533. (call-interactively 'org-compute-property-at-point))
  9534. (t (error "No such property action %c" c)))))
  9535. (defun org-at-property-p ()
  9536. "Is the cursor in a property line?"
  9537. ;; FIXME: Does not check if we are actually in the drawer.
  9538. ;; FIXME: also returns true on any drawers.....
  9539. ;; This is used by C-c C-c for property action.
  9540. (save-excursion
  9541. (beginning-of-line 1)
  9542. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9543. (defun org-get-property-block (&optional beg end force)
  9544. "Return the (beg . end) range of the body of the property drawer.
  9545. BEG and END can be beginning and end of subtree, if not given
  9546. they will be found.
  9547. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9548. (catch 'exit
  9549. (save-excursion
  9550. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9551. (end (or end (progn (outline-next-heading) (point)))))
  9552. (goto-char beg)
  9553. (if (re-search-forward org-property-start-re end t)
  9554. (setq beg (1+ (match-end 0)))
  9555. (if force
  9556. (save-excursion
  9557. (org-insert-property-drawer)
  9558. (setq end (progn (outline-next-heading) (point))))
  9559. (throw 'exit nil))
  9560. (goto-char beg)
  9561. (if (re-search-forward org-property-start-re end t)
  9562. (setq beg (1+ (match-end 0)))))
  9563. (if (re-search-forward org-property-end-re end t)
  9564. (setq end (match-beginning 0))
  9565. (or force (throw 'exit nil))
  9566. (goto-char beg)
  9567. (setq end beg)
  9568. (org-indent-line-function)
  9569. (insert ":END:\n"))
  9570. (cons beg end)))))
  9571. (defun org-entry-properties (&optional pom which)
  9572. "Get all properties of the entry at point-or-marker POM.
  9573. This includes the TODO keyword, the tags, time strings for deadline,
  9574. scheduled, and clocking, and any additional properties defined in the
  9575. entry. The return value is an alist, keys may occur multiple times
  9576. if the property key was used several times.
  9577. POM may also be nil, in which case the current entry is used.
  9578. If WHICH is nil or `all', get all properties. If WHICH is
  9579. `special' or `standard', only get that subclass."
  9580. (setq which (or which 'all))
  9581. (org-with-point-at pom
  9582. (let ((clockstr (substring org-clock-string 0 -1))
  9583. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9584. beg end range props sum-props key value string clocksum)
  9585. (save-excursion
  9586. (when (condition-case nil
  9587. (and (org-mode-p) (org-back-to-heading t))
  9588. (error nil))
  9589. (setq beg (point))
  9590. (setq sum-props (get-text-property (point) 'org-summaries))
  9591. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9592. (outline-next-heading)
  9593. (setq end (point))
  9594. (when (memq which '(all special))
  9595. ;; Get the special properties, like TODO and tags
  9596. (goto-char beg)
  9597. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9598. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9599. (when (looking-at org-priority-regexp)
  9600. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9601. (when (and (setq value (org-get-tags-string))
  9602. (string-match "\\S-" value))
  9603. (push (cons "TAGS" value) props))
  9604. (when (setq value (org-get-tags-at))
  9605. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9606. props))
  9607. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9608. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9609. string (if (equal key clockstr)
  9610. (org-no-properties
  9611. (org-trim
  9612. (buffer-substring
  9613. (match-beginning 3) (goto-char (point-at-eol)))))
  9614. (substring (org-match-string-no-properties 3) 1 -1)))
  9615. (unless key
  9616. (if (= (char-after (match-beginning 3)) ?\[)
  9617. (setq key "TIMESTAMP_IA")
  9618. (setq key "TIMESTAMP")))
  9619. (when (or (equal key clockstr) (not (assoc key props)))
  9620. (push (cons key string) props)))
  9621. )
  9622. (when (memq which '(all standard))
  9623. ;; Get the standard properties, like :PORP: ...
  9624. (setq range (org-get-property-block beg end))
  9625. (when range
  9626. (goto-char (car range))
  9627. (while (re-search-forward
  9628. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9629. (cdr range) t)
  9630. (setq key (org-match-string-no-properties 1)
  9631. value (org-trim (or (org-match-string-no-properties 2) "")))
  9632. (unless (member key excluded)
  9633. (push (cons key (or value "")) props)))))
  9634. (if clocksum
  9635. (push (cons "CLOCKSUM"
  9636. (org-columns-number-to-string (/ (float clocksum) 60.)
  9637. 'add_times))
  9638. props))
  9639. (unless (assoc "CATEGORY" props)
  9640. (setq value (or (org-get-category)
  9641. (progn (org-refresh-category-properties)
  9642. (org-get-category))))
  9643. (push (cons "CATEGORY" value) props))
  9644. (append sum-props (nreverse props)))))))
  9645. (defun org-entry-get (pom property &optional inherit)
  9646. "Get value of PROPERTY for entry at point-or-marker POM.
  9647. If INHERIT is non-nil and the entry does not have the property,
  9648. then also check higher levels of the hierarchy.
  9649. If INHERIT is the symbol `selective', use inheritance only if the setting
  9650. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9651. If the property is present but empty, the return value is the empty string.
  9652. If the property is not present at all, nil is returned."
  9653. (org-with-point-at pom
  9654. (if (and inherit (if (eq inherit 'selective)
  9655. (org-property-inherit-p property)
  9656. t))
  9657. (org-entry-get-with-inheritance property)
  9658. (if (member property org-special-properties)
  9659. ;; We need a special property. Use brute force, get all properties.
  9660. (cdr (assoc property (org-entry-properties nil 'special)))
  9661. (let ((range (org-get-property-block)))
  9662. (if (and range
  9663. (goto-char (car range))
  9664. (re-search-forward
  9665. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9666. (cdr range) t))
  9667. ;; Found the property, return it.
  9668. (if (match-end 1)
  9669. (org-match-string-no-properties 1)
  9670. "")))))))
  9671. (defun org-property-or-variable-value (var &optional inherit)
  9672. "Check if there is a property fixing the value of VAR.
  9673. If yes, return this value. If not, return the current value of the variable."
  9674. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9675. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9676. (read prop)
  9677. (symbol-value var))))
  9678. (defun org-entry-delete (pom property)
  9679. "Delete the property PROPERTY from entry at point-or-marker POM."
  9680. (org-with-point-at pom
  9681. (if (member property org-special-properties)
  9682. nil ; cannot delete these properties.
  9683. (let ((range (org-get-property-block)))
  9684. (if (and range
  9685. (goto-char (car range))
  9686. (re-search-forward
  9687. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9688. (cdr range) t))
  9689. (progn
  9690. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9691. t)
  9692. nil)))))
  9693. ;; Multi-values properties are properties that contain multiple values
  9694. ;; These values are assumed to be single words, separated by whitespace.
  9695. (defun org-entry-add-to-multivalued-property (pom property value)
  9696. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9697. (let* ((old (org-entry-get pom property))
  9698. (values (and old (org-split-string old "[ \t]"))))
  9699. (setq value (org-entry-protect-space value))
  9700. (unless (member value values)
  9701. (setq values (cons value values))
  9702. (org-entry-put pom property
  9703. (mapconcat 'identity values " ")))))
  9704. (defun org-entry-remove-from-multivalued-property (pom property value)
  9705. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9706. (let* ((old (org-entry-get pom property))
  9707. (values (and old (org-split-string old "[ \t]"))))
  9708. (setq value (org-entry-protect-space value))
  9709. (when (member value values)
  9710. (setq values (delete value values))
  9711. (org-entry-put pom property
  9712. (mapconcat 'identity values " ")))))
  9713. (defun org-entry-member-in-multivalued-property (pom property value)
  9714. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9715. (let* ((old (org-entry-get pom property))
  9716. (values (and old (org-split-string old "[ \t]"))))
  9717. (setq value (org-entry-protect-space value))
  9718. (member value values)))
  9719. (defun org-entry-get-multivalued-property (pom property)
  9720. "Return a list of values in a multivalued property."
  9721. (let* ((value (org-entry-get pom property))
  9722. (values (and value (org-split-string value "[ \t]"))))
  9723. (mapcar 'org-entry-restore-space values)))
  9724. (defun org-entry-put-multivalued-property (pom property &rest values)
  9725. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9726. VALUES should be a list of strings. Spaces will be protected."
  9727. (org-entry-put pom property
  9728. (mapconcat 'org-entry-protect-space values " "))
  9729. (let* ((value (org-entry-get pom property))
  9730. (values (and value (org-split-string value "[ \t]"))))
  9731. (mapcar 'org-entry-restore-space values)))
  9732. (defun org-entry-protect-space (s)
  9733. "Protect spaces and newline in string S."
  9734. (while (string-match " " s)
  9735. (setq s (replace-match "%20" t t s)))
  9736. (while (string-match "\n" s)
  9737. (setq s (replace-match "%0A" t t s)))
  9738. s)
  9739. (defun org-entry-restore-space (s)
  9740. "Restore spaces and newline in string S."
  9741. (while (string-match "%20" s)
  9742. (setq s (replace-match " " t t s)))
  9743. (while (string-match "%0A" s)
  9744. (setq s (replace-match "\n" t t s)))
  9745. s)
  9746. (defvar org-entry-property-inherited-from (make-marker)
  9747. "Marker pointing to the entry from where a property was inherited.
  9748. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9749. location of the entry where the inheritance search matched. If there was
  9750. no match, the marker will point nowhere.
  9751. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9752. is set.")
  9753. (defun org-entry-get-with-inheritance (property)
  9754. "Get entry property, and search higher levels if not present."
  9755. (move-marker org-entry-property-inherited-from nil)
  9756. (let (tmp)
  9757. (save-excursion
  9758. (save-restriction
  9759. (widen)
  9760. (catch 'ex
  9761. (while t
  9762. (when (setq tmp (org-entry-get nil property))
  9763. (org-back-to-heading t)
  9764. (move-marker org-entry-property-inherited-from (point))
  9765. (throw 'ex tmp))
  9766. (or (org-up-heading-safe) (throw 'ex nil)))))
  9767. (or tmp
  9768. (cdr (assoc property org-file-properties))
  9769. (cdr (assoc property org-global-properties))
  9770. (cdr (assoc property org-global-properties-fixed))))))
  9771. (defun org-entry-put (pom property value)
  9772. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9773. (org-with-point-at pom
  9774. (org-back-to-heading t)
  9775. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9776. range)
  9777. (cond
  9778. ((equal property "TODO")
  9779. (when (and (stringp value) (string-match "\\S-" value)
  9780. (not (member value org-todo-keywords-1)))
  9781. (error "\"%s\" is not a valid TODO state" value))
  9782. (if (or (not value)
  9783. (not (string-match "\\S-" value)))
  9784. (setq value 'none))
  9785. (org-todo value)
  9786. (org-set-tags nil 'align))
  9787. ((equal property "PRIORITY")
  9788. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9789. (string-to-char value) ?\ ))
  9790. (org-set-tags nil 'align))
  9791. ((equal property "SCHEDULED")
  9792. (if (re-search-forward org-scheduled-time-regexp end t)
  9793. (cond
  9794. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9795. ((eq value 'later) (org-timestamp-change 1 'day))
  9796. (t (call-interactively 'org-schedule)))
  9797. (call-interactively 'org-schedule)))
  9798. ((equal property "DEADLINE")
  9799. (if (re-search-forward org-deadline-time-regexp end t)
  9800. (cond
  9801. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9802. ((eq value 'later) (org-timestamp-change 1 'day))
  9803. (t (call-interactively 'org-deadline)))
  9804. (call-interactively 'org-deadline)))
  9805. ((member property org-special-properties)
  9806. (error "The %s property can not yet be set with `org-entry-put'"
  9807. property))
  9808. (t ; a non-special property
  9809. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9810. (setq range (org-get-property-block beg end 'force))
  9811. (goto-char (car range))
  9812. (if (re-search-forward
  9813. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9814. (progn
  9815. (delete-region (match-beginning 1) (match-end 1))
  9816. (goto-char (match-beginning 1)))
  9817. (goto-char (cdr range))
  9818. (insert "\n")
  9819. (backward-char 1)
  9820. (org-indent-line-function)
  9821. (insert ":" property ":"))
  9822. (and value (insert " " value))
  9823. (org-indent-line-function)))))))
  9824. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9825. "Get all property keys in the current buffer.
  9826. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9827. like tags and TODO state.
  9828. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9829. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9830. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9831. formats in the current buffer."
  9832. (let (rtn range cfmt s p)
  9833. (save-excursion
  9834. (save-restriction
  9835. (widen)
  9836. (goto-char (point-min))
  9837. (while (re-search-forward org-property-start-re nil t)
  9838. (setq range (org-get-property-block))
  9839. (goto-char (car range))
  9840. (while (re-search-forward
  9841. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9842. (cdr range) t)
  9843. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9844. (outline-next-heading))))
  9845. (when include-specials
  9846. (setq rtn (append org-special-properties rtn)))
  9847. (when include-defaults
  9848. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9849. (when include-columns
  9850. (save-excursion
  9851. (save-restriction
  9852. (widen)
  9853. (goto-char (point-min))
  9854. (while (re-search-forward
  9855. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9856. nil t)
  9857. (setq cfmt (match-string 2) s 0)
  9858. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9859. cfmt s)
  9860. (setq s (match-end 0)
  9861. p (match-string 1 cfmt))
  9862. (unless (or (equal p "ITEM")
  9863. (member p org-special-properties))
  9864. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9865. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9866. (defun org-property-values (key)
  9867. "Return a list of all values of property KEY."
  9868. (save-excursion
  9869. (save-restriction
  9870. (widen)
  9871. (goto-char (point-min))
  9872. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9873. values)
  9874. (while (re-search-forward re nil t)
  9875. (add-to-list 'values (org-trim (match-string 1))))
  9876. (delete "" values)))))
  9877. (defun org-insert-property-drawer ()
  9878. "Insert a property drawer into the current entry."
  9879. (interactive)
  9880. (org-back-to-heading t)
  9881. (looking-at outline-regexp)
  9882. (let ((indent (- (match-end 0)(match-beginning 0)))
  9883. (beg (point))
  9884. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9885. end hiddenp)
  9886. (outline-next-heading)
  9887. (setq end (point))
  9888. (goto-char beg)
  9889. (while (re-search-forward re end t))
  9890. (setq hiddenp (org-invisible-p))
  9891. (end-of-line 1)
  9892. (and (equal (char-after) ?\n) (forward-char 1))
  9893. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9894. (beginning-of-line 2))
  9895. (org-skip-over-state-notes)
  9896. (skip-chars-backward " \t\n\r")
  9897. (if (eq (char-before) ?*) (forward-char 1))
  9898. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9899. (beginning-of-line 0)
  9900. (org-indent-to-column indent)
  9901. (beginning-of-line 2)
  9902. (org-indent-to-column indent)
  9903. (beginning-of-line 0)
  9904. (if hiddenp
  9905. (save-excursion
  9906. (org-back-to-heading t)
  9907. (hide-entry))
  9908. (org-flag-drawer t))))
  9909. (defun org-set-property (property value)
  9910. "In the current entry, set PROPERTY to VALUE.
  9911. When called interactively, this will prompt for a property name, offering
  9912. completion on existing and default properties. And then it will prompt
  9913. for a value, offering completion either on allowed values (via an inherited
  9914. xxx_ALL property) or on existing values in other instances of this property
  9915. in the current file."
  9916. (interactive
  9917. (let* ((completion-ignore-case t)
  9918. (keys (org-buffer-property-keys nil t t))
  9919. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9920. (prop (if (member prop0 keys)
  9921. prop0
  9922. (or (cdr (assoc (downcase prop0)
  9923. (mapcar (lambda (x) (cons (downcase x) x))
  9924. keys)))
  9925. prop0)))
  9926. (cur (org-entry-get nil prop))
  9927. (allowed (org-property-get-allowed-values nil prop 'table))
  9928. (existing (mapcar 'list (org-property-values prop)))
  9929. (val (if allowed
  9930. (org-completing-read "Value: " allowed nil 'req-match)
  9931. (org-completing-read
  9932. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9933. (concat "[" cur "]") "")
  9934. ": ")
  9935. existing nil nil "" nil cur))))
  9936. (list prop (if (equal val "") cur val))))
  9937. (unless (equal (org-entry-get nil property) value)
  9938. (org-entry-put nil property value)))
  9939. (defun org-delete-property (property)
  9940. "In the current entry, delete PROPERTY."
  9941. (interactive
  9942. (let* ((completion-ignore-case t)
  9943. (prop (org-ido-completing-read
  9944. "Property: " (org-entry-properties nil 'standard))))
  9945. (list prop)))
  9946. (message "Property %s %s" property
  9947. (if (org-entry-delete nil property)
  9948. "deleted"
  9949. "was not present in the entry")))
  9950. (defun org-delete-property-globally (property)
  9951. "Remove PROPERTY globally, from all entries."
  9952. (interactive
  9953. (let* ((completion-ignore-case t)
  9954. (prop (org-ido-completing-read
  9955. "Globally remove property: "
  9956. (mapcar 'list (org-buffer-property-keys)))))
  9957. (list prop)))
  9958. (save-excursion
  9959. (save-restriction
  9960. (widen)
  9961. (goto-char (point-min))
  9962. (let ((cnt 0))
  9963. (while (re-search-forward
  9964. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9965. nil t)
  9966. (setq cnt (1+ cnt))
  9967. (replace-match ""))
  9968. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9969. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9970. (defun org-compute-property-at-point ()
  9971. "Compute the property at point.
  9972. This looks for an enclosing column format, extracts the operator and
  9973. then applies it to the property in the column format's scope."
  9974. (interactive)
  9975. (unless (org-at-property-p)
  9976. (error "Not at a property"))
  9977. (let ((prop (org-match-string-no-properties 2)))
  9978. (org-columns-get-format-and-top-level)
  9979. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9980. (error "No operator defined for property %s" prop))
  9981. (org-columns-compute prop)))
  9982. (defun org-property-get-allowed-values (pom property &optional table)
  9983. "Get allowed values for the property PROPERTY.
  9984. When TABLE is non-nil, return an alist that can directly be used for
  9985. completion."
  9986. (let (vals)
  9987. (cond
  9988. ((equal property "TODO")
  9989. (setq vals (org-with-point-at pom
  9990. (append org-todo-keywords-1 '("")))))
  9991. ((equal property "PRIORITY")
  9992. (let ((n org-lowest-priority))
  9993. (while (>= n org-highest-priority)
  9994. (push (char-to-string n) vals)
  9995. (setq n (1- n)))))
  9996. ((member property org-special-properties))
  9997. (t
  9998. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9999. (when (and vals (string-match "\\S-" vals))
  10000. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10001. (setq vals (mapcar (lambda (x)
  10002. (cond ((stringp x) x)
  10003. ((numberp x) (number-to-string x))
  10004. ((symbolp x) (symbol-name x))
  10005. (t "???")))
  10006. vals)))))
  10007. (if table (mapcar 'list vals) vals)))
  10008. (defun org-property-previous-allowed-value (&optional previous)
  10009. "Switch to the next allowed value for this property."
  10010. (interactive)
  10011. (org-property-next-allowed-value t))
  10012. (defun org-property-next-allowed-value (&optional previous)
  10013. "Switch to the next allowed value for this property."
  10014. (interactive)
  10015. (unless (org-at-property-p)
  10016. (error "Not at a property"))
  10017. (let* ((key (match-string 2))
  10018. (value (match-string 3))
  10019. (allowed (or (org-property-get-allowed-values (point) key)
  10020. (and (member value '("[ ]" "[-]" "[X]"))
  10021. '("[ ]" "[X]"))))
  10022. nval)
  10023. (unless allowed
  10024. (error "Allowed values for this property have not been defined"))
  10025. (if previous (setq allowed (reverse allowed)))
  10026. (if (member value allowed)
  10027. (setq nval (car (cdr (member value allowed)))))
  10028. (setq nval (or nval (car allowed)))
  10029. (if (equal nval value)
  10030. (error "Only one allowed value for this property"))
  10031. (org-at-property-p)
  10032. (replace-match (concat " :" key ": " nval) t t)
  10033. (org-indent-line-function)
  10034. (beginning-of-line 1)
  10035. (skip-chars-forward " \t")))
  10036. (defun org-find-entry-with-id (ident)
  10037. "Locate the entry that contains the ID property with exact value IDENT.
  10038. IDENT can be a string, a symbol or a number, this function will search for
  10039. the string representation of it.
  10040. Return the position where this entry starts, or nil if there is no such entry."
  10041. (interactive "sID: ")
  10042. (let ((id (cond
  10043. ((stringp ident) ident)
  10044. ((symbol-name ident) (symbol-name ident))
  10045. ((numberp ident) (number-to-string ident))
  10046. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10047. (case-fold-search nil))
  10048. (save-excursion
  10049. (save-restriction
  10050. (widen)
  10051. (goto-char (point-min))
  10052. (when (re-search-forward
  10053. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10054. nil t)
  10055. (org-back-to-heading)
  10056. (point))))))
  10057. ;;;; Timestamps
  10058. (defvar org-last-changed-timestamp nil)
  10059. (defvar org-last-inserted-timestamp nil
  10060. "The last time stamp inserted with `org-insert-time-stamp'.")
  10061. (defvar org-time-was-given) ; dynamically scoped parameter
  10062. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10063. (defvar org-ts-what) ; dynamically scoped parameter
  10064. (defun org-time-stamp (arg &optional inactive)
  10065. "Prompt for a date/time and insert a time stamp.
  10066. If the user specifies a time like HH:MM, or if this command is called
  10067. with a prefix argument, the time stamp will contain date and time.
  10068. Otherwise, only the date will be included. All parts of a date not
  10069. specified by the user will be filled in from the current date/time.
  10070. So if you press just return without typing anything, the time stamp
  10071. will represent the current date/time. If there is already a timestamp
  10072. at the cursor, it will be modified."
  10073. (interactive "P")
  10074. (let* ((ts nil)
  10075. (default-time
  10076. ;; Default time is either today, or, when entering a range,
  10077. ;; the range start.
  10078. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10079. (save-excursion
  10080. (re-search-backward
  10081. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10082. (- (point) 20) t)))
  10083. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10084. (current-time)))
  10085. (default-input (and ts (org-get-compact-tod ts)))
  10086. org-time-was-given org-end-time-was-given time)
  10087. (cond
  10088. ((and (org-at-timestamp-p t)
  10089. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10090. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10091. (insert "--")
  10092. (setq time (let ((this-command this-command))
  10093. (org-read-date arg 'totime nil nil
  10094. default-time default-input)))
  10095. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10096. ((org-at-timestamp-p t)
  10097. (setq time (let ((this-command this-command))
  10098. (org-read-date arg 'totime nil nil default-time default-input)))
  10099. (when (org-at-timestamp-p t) ; just to get the match data
  10100. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10101. (replace-match "")
  10102. (setq org-last-changed-timestamp
  10103. (org-insert-time-stamp
  10104. time (or org-time-was-given arg)
  10105. inactive nil nil (list org-end-time-was-given))))
  10106. (message "Timestamp updated"))
  10107. (t
  10108. (setq time (let ((this-command this-command))
  10109. (org-read-date arg 'totime nil nil default-time default-input)))
  10110. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10111. nil nil (list org-end-time-was-given))))))
  10112. ;; FIXME: can we use this for something else, like computing time differences?
  10113. (defun org-get-compact-tod (s)
  10114. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10115. (let* ((t1 (match-string 1 s))
  10116. (h1 (string-to-number (match-string 2 s)))
  10117. (m1 (string-to-number (match-string 3 s)))
  10118. (t2 (and (match-end 4) (match-string 5 s)))
  10119. (h2 (and t2 (string-to-number (match-string 6 s))))
  10120. (m2 (and t2 (string-to-number (match-string 7 s))))
  10121. dh dm)
  10122. (if (not t2)
  10123. t1
  10124. (setq dh (- h2 h1) dm (- m2 m1))
  10125. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10126. (concat t1 "+" (number-to-string dh)
  10127. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10128. (defun org-time-stamp-inactive (&optional arg)
  10129. "Insert an inactive time stamp.
  10130. An inactive time stamp is enclosed in square brackets instead of angle
  10131. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10132. does not link to the calendar and cannot be changed with the S-cursor keys.
  10133. So these are more for recording a certain time/date."
  10134. (interactive "P")
  10135. (org-time-stamp arg 'inactive))
  10136. (defvar org-date-ovl (org-make-overlay 1 1))
  10137. (org-overlay-put org-date-ovl 'face 'org-warning)
  10138. (org-detach-overlay org-date-ovl)
  10139. (defvar org-ans1) ; dynamically scoped parameter
  10140. (defvar org-ans2) ; dynamically scoped parameter
  10141. (defvar org-plain-time-of-day-regexp) ; defined below
  10142. (defvar org-overriding-default-time nil) ; dynamically scoped
  10143. (defvar org-read-date-overlay nil)
  10144. (defvar org-dcst nil) ; dynamically scoped
  10145. (defun org-read-date (&optional with-time to-time from-string prompt
  10146. default-time default-input)
  10147. "Read a date, possibly a time, and make things smooth for the user.
  10148. The prompt will suggest to enter an ISO date, but you can also enter anything
  10149. which will at least partially be understood by `parse-time-string'.
  10150. Unrecognized parts of the date will default to the current day, month, year,
  10151. hour and minute. If this command is called to replace a timestamp at point,
  10152. of to enter the second timestamp of a range, the default time is taken from the
  10153. existing stamp. For example,
  10154. 3-2-5 --> 2003-02-05
  10155. feb 15 --> currentyear-02-15
  10156. sep 12 9 --> 2009-09-12
  10157. 12:45 --> today 12:45
  10158. 22 sept 0:34 --> currentyear-09-22 0:34
  10159. 12 --> currentyear-currentmonth-12
  10160. Fri --> nearest Friday (today or later)
  10161. etc.
  10162. Furthermore you can specify a relative date by giving, as the *first* thing
  10163. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10164. change in days weeks, months, years.
  10165. With a single plus or minus, the date is relative to today. With a double
  10166. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10167. +4d --> four days from today
  10168. +4 --> same as above
  10169. +2w --> two weeks from today
  10170. ++5 --> five days from default date
  10171. The function understands only English month and weekday abbreviations,
  10172. but this can be configured with the variables `parse-time-months' and
  10173. `parse-time-weekdays'.
  10174. While prompting, a calendar is popped up - you can also select the
  10175. date with the mouse (button 1). The calendar shows a period of three
  10176. months. To scroll it to other months, use the keys `>' and `<'.
  10177. If you don't like the calendar, turn it off with
  10178. \(setq org-read-date-popup-calendar nil)
  10179. With optional argument TO-TIME, the date will immediately be converted
  10180. to an internal time.
  10181. With an optional argument WITH-TIME, the prompt will suggest to also
  10182. insert a time. Note that when WITH-TIME is not set, you can still
  10183. enter a time, and this function will inform the calling routine about
  10184. this change. The calling routine may then choose to change the format
  10185. used to insert the time stamp into the buffer to include the time.
  10186. With optional argument FROM-STRING, read from this string instead from
  10187. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10188. the time/date that is used for everything that is not specified by the
  10189. user."
  10190. (require 'parse-time)
  10191. (let* ((org-time-stamp-rounding-minutes
  10192. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10193. (org-dcst org-display-custom-times)
  10194. (ct (org-current-time))
  10195. (def (or org-overriding-default-time default-time ct))
  10196. (defdecode (decode-time def))
  10197. (dummy (progn
  10198. (when (< (nth 2 defdecode) org-extend-today-until)
  10199. (setcar (nthcdr 2 defdecode) -1)
  10200. (setcar (nthcdr 1 defdecode) 59)
  10201. (setq def (apply 'encode-time defdecode)
  10202. defdecode (decode-time def)))))
  10203. (calendar-move-hook nil)
  10204. (calendar-view-diary-initially-flag nil)
  10205. (view-diary-entries-initially nil)
  10206. (calendar-view-holidays-initially-flag nil)
  10207. (view-calendar-holidays-initially nil)
  10208. (timestr (format-time-string
  10209. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10210. (prompt (concat (if prompt (concat prompt " ") "")
  10211. (format "Date+time [%s]: " timestr)))
  10212. ans (org-ans0 "") org-ans1 org-ans2 final)
  10213. (cond
  10214. (from-string (setq ans from-string))
  10215. (org-read-date-popup-calendar
  10216. (save-excursion
  10217. (save-window-excursion
  10218. (calendar)
  10219. (calendar-forward-day (- (time-to-days def)
  10220. (calendar-absolute-from-gregorian
  10221. (calendar-current-date))))
  10222. (org-eval-in-calendar nil t)
  10223. (let* ((old-map (current-local-map))
  10224. (map (copy-keymap calendar-mode-map))
  10225. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10226. (org-defkey map (kbd "RET") 'org-calendar-select)
  10227. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10228. 'org-calendar-select-mouse)
  10229. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10230. 'org-calendar-select-mouse)
  10231. (org-defkey minibuffer-local-map [(meta shift left)]
  10232. (lambda () (interactive)
  10233. (org-eval-in-calendar '(calendar-backward-month 1))))
  10234. (org-defkey minibuffer-local-map [(meta shift right)]
  10235. (lambda () (interactive)
  10236. (org-eval-in-calendar '(calendar-forward-month 1))))
  10237. (org-defkey minibuffer-local-map [(meta shift up)]
  10238. (lambda () (interactive)
  10239. (org-eval-in-calendar '(calendar-backward-year 1))))
  10240. (org-defkey minibuffer-local-map [(meta shift down)]
  10241. (lambda () (interactive)
  10242. (org-eval-in-calendar '(calendar-forward-year 1))))
  10243. (org-defkey minibuffer-local-map [(shift up)]
  10244. (lambda () (interactive)
  10245. (org-eval-in-calendar '(calendar-backward-week 1))))
  10246. (org-defkey minibuffer-local-map [(shift down)]
  10247. (lambda () (interactive)
  10248. (org-eval-in-calendar '(calendar-forward-week 1))))
  10249. (org-defkey minibuffer-local-map [(shift left)]
  10250. (lambda () (interactive)
  10251. (org-eval-in-calendar '(calendar-backward-day 1))))
  10252. (org-defkey minibuffer-local-map [(shift right)]
  10253. (lambda () (interactive)
  10254. (org-eval-in-calendar '(calendar-forward-day 1))))
  10255. (org-defkey minibuffer-local-map ">"
  10256. (lambda () (interactive)
  10257. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10258. (org-defkey minibuffer-local-map "<"
  10259. (lambda () (interactive)
  10260. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10261. (unwind-protect
  10262. (progn
  10263. (use-local-map map)
  10264. (add-hook 'post-command-hook 'org-read-date-display)
  10265. (setq org-ans0 (read-string prompt default-input nil nil))
  10266. ;; org-ans0: from prompt
  10267. ;; org-ans1: from mouse click
  10268. ;; org-ans2: from calendar motion
  10269. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10270. (remove-hook 'post-command-hook 'org-read-date-display)
  10271. (use-local-map old-map)
  10272. (when org-read-date-overlay
  10273. (org-delete-overlay org-read-date-overlay)
  10274. (setq org-read-date-overlay nil)))))))
  10275. (t ; Naked prompt only
  10276. (unwind-protect
  10277. (setq ans (read-string prompt default-input nil timestr))
  10278. (when org-read-date-overlay
  10279. (org-delete-overlay org-read-date-overlay)
  10280. (setq org-read-date-overlay nil)))))
  10281. (setq final (org-read-date-analyze ans def defdecode))
  10282. (if to-time
  10283. (apply 'encode-time final)
  10284. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10285. (format "%04d-%02d-%02d %02d:%02d"
  10286. (nth 5 final) (nth 4 final) (nth 3 final)
  10287. (nth 2 final) (nth 1 final))
  10288. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10289. (defvar def)
  10290. (defvar defdecode)
  10291. (defvar with-time)
  10292. (defun org-read-date-display ()
  10293. "Display the current date prompt interpretation in the minibuffer."
  10294. (when org-read-date-display-live
  10295. (when org-read-date-overlay
  10296. (org-delete-overlay org-read-date-overlay))
  10297. (let ((p (point)))
  10298. (end-of-line 1)
  10299. (while (not (equal (buffer-substring
  10300. (max (point-min) (- (point) 4)) (point))
  10301. " "))
  10302. (insert " "))
  10303. (goto-char p))
  10304. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10305. " " (or org-ans1 org-ans2)))
  10306. (org-end-time-was-given nil)
  10307. (f (org-read-date-analyze ans def defdecode))
  10308. (fmts (if org-dcst
  10309. org-time-stamp-custom-formats
  10310. org-time-stamp-formats))
  10311. (fmt (if (or with-time
  10312. (and (boundp 'org-time-was-given) org-time-was-given))
  10313. (cdr fmts)
  10314. (car fmts)))
  10315. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10316. (when (and org-end-time-was-given
  10317. (string-match org-plain-time-of-day-regexp txt))
  10318. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10319. org-end-time-was-given
  10320. (substring txt (match-end 0)))))
  10321. (setq org-read-date-overlay
  10322. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10323. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10324. (defun org-read-date-analyze (ans def defdecode)
  10325. "Analyse the combined answer of the date prompt."
  10326. ;; FIXME: cleanup and comment
  10327. (let (delta deltan deltaw deltadef year month day
  10328. hour minute second wday pm h2 m2 tl wday1
  10329. iso-year iso-weekday iso-week iso-year iso-date)
  10330. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10331. (setq ans "+0"))
  10332. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10333. (setq ans (replace-match "" t t ans)
  10334. deltan (car delta)
  10335. deltaw (nth 1 delta)
  10336. deltadef (nth 2 delta)))
  10337. ;; Check if there is an iso week date in there
  10338. ;; If yes, sore the info and postpone interpreting it until the rest
  10339. ;; of the parsing is done
  10340. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10341. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10342. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10343. iso-week (string-to-number (match-string 2 ans)))
  10344. (setq ans (replace-match "" t t ans)))
  10345. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10346. (when (string-match
  10347. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10348. (setq year (if (match-end 2)
  10349. (string-to-number (match-string 2 ans))
  10350. (string-to-number (format-time-string "%Y")))
  10351. month (string-to-number (match-string 3 ans))
  10352. day (string-to-number (match-string 4 ans)))
  10353. (if (< year 100) (setq year (+ 2000 year)))
  10354. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10355. t nil ans)))
  10356. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10357. ;; If there is a time with am/pm, and *no* time without it, we convert
  10358. ;; so that matching will be successful.
  10359. (loop for i from 1 to 2 do ; twice, for end time as well
  10360. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10361. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10362. (setq hour (string-to-number (match-string 1 ans))
  10363. minute (if (match-end 3)
  10364. (string-to-number (match-string 3 ans))
  10365. 0)
  10366. pm (equal ?p
  10367. (string-to-char (downcase (match-string 4 ans)))))
  10368. (if (and (= hour 12) (not pm))
  10369. (setq hour 0)
  10370. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10371. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10372. t t ans))))
  10373. ;; Check if a time range is given as a duration
  10374. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10375. (setq hour (string-to-number (match-string 1 ans))
  10376. h2 (+ hour (string-to-number (match-string 3 ans)))
  10377. minute (string-to-number (match-string 2 ans))
  10378. m2 (+ minute (if (match-end 5) (string-to-number
  10379. (match-string 5 ans))0)))
  10380. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10381. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10382. t t ans)))
  10383. ;; Check if there is a time range
  10384. (when (boundp 'org-end-time-was-given)
  10385. (setq org-time-was-given nil)
  10386. (when (and (string-match org-plain-time-of-day-regexp ans)
  10387. (match-end 8))
  10388. (setq org-end-time-was-given (match-string 8 ans))
  10389. (setq ans (concat (substring ans 0 (match-beginning 7))
  10390. (substring ans (match-end 7))))))
  10391. (setq tl (parse-time-string ans)
  10392. day (or (nth 3 tl) (nth 3 defdecode))
  10393. month (or (nth 4 tl)
  10394. (if (and org-read-date-prefer-future
  10395. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10396. (1+ (nth 4 defdecode))
  10397. (nth 4 defdecode)))
  10398. year (or (nth 5 tl)
  10399. (if (and org-read-date-prefer-future
  10400. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10401. (1+ (nth 5 defdecode))
  10402. (nth 5 defdecode)))
  10403. hour (or (nth 2 tl) (nth 2 defdecode))
  10404. minute (or (nth 1 tl) (nth 1 defdecode))
  10405. second (or (nth 0 tl) 0)
  10406. wday (nth 6 tl))
  10407. ;; Special date definitions below
  10408. (cond
  10409. (iso-week
  10410. ;; There was an iso week
  10411. (setq year (or iso-year year)
  10412. day (or iso-weekday wday 1)
  10413. wday nil ; to make sure that the trigger below does not match
  10414. iso-date (calendar-gregorian-from-absolute
  10415. (calendar-absolute-from-iso
  10416. (list iso-week day year))))
  10417. ; FIXME: Should we also push ISO weeks into the future?
  10418. ; (when (and org-read-date-prefer-future
  10419. ; (not iso-year)
  10420. ; (< (calendar-absolute-from-gregorian iso-date)
  10421. ; (time-to-days (current-time))))
  10422. ; (setq year (1+ year)
  10423. ; iso-date (calendar-gregorian-from-absolute
  10424. ; (calendar-absolute-from-iso
  10425. ; (list iso-week day year)))))
  10426. (setq month (car iso-date)
  10427. year (nth 2 iso-date)
  10428. day (nth 1 iso-date)))
  10429. (deltan
  10430. (unless deltadef
  10431. (let ((now (decode-time (current-time))))
  10432. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10433. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10434. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10435. ((equal deltaw "m") (setq month (+ month deltan)))
  10436. ((equal deltaw "y") (setq year (+ year deltan)))))
  10437. ((and wday (not (nth 3 tl)))
  10438. ;; Weekday was given, but no day, so pick that day in the week
  10439. ;; on or after the derived date.
  10440. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10441. (unless (equal wday wday1)
  10442. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10443. (if (and (boundp 'org-time-was-given)
  10444. (nth 2 tl))
  10445. (setq org-time-was-given t))
  10446. (if (< year 100) (setq year (+ 2000 year)))
  10447. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10448. (list second minute hour day month year)))
  10449. (defvar parse-time-weekdays)
  10450. (defun org-read-date-get-relative (s today default)
  10451. "Check string S for special relative date string.
  10452. TODAY and DEFAULT are internal times, for today and for a default.
  10453. Return shift list (N what def-flag)
  10454. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10455. N is the number of WHATs to shift.
  10456. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10457. the DEFAULT date rather than TODAY."
  10458. (when (and
  10459. (string-match
  10460. (concat
  10461. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10462. "\\([0-9]+\\)?"
  10463. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10464. "\\([ \t]\\|$\\)") s)
  10465. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10466. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10467. (string-to-char (substring (match-string 1 s) -1))
  10468. ?+))
  10469. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10470. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10471. (what (if (match-end 3) (match-string 3 s) "d"))
  10472. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10473. (date (if rel default today))
  10474. (wday (nth 6 (decode-time date)))
  10475. delta)
  10476. (if wday1
  10477. (progn
  10478. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10479. (if (= dir ?-) (setq delta (- delta 7)))
  10480. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10481. (list delta "d" rel))
  10482. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10483. (defun org-eval-in-calendar (form &optional keepdate)
  10484. "Eval FORM in the calendar window and return to current window.
  10485. Also, store the cursor date in variable org-ans2."
  10486. (let ((sw (selected-window)))
  10487. (select-window (get-buffer-window "*Calendar*"))
  10488. (eval form)
  10489. (when (and (not keepdate) (calendar-cursor-to-date))
  10490. (let* ((date (calendar-cursor-to-date))
  10491. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10492. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10493. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10494. (select-window sw)))
  10495. (defun org-calendar-select ()
  10496. "Return to `org-read-date' with the date currently selected.
  10497. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10498. (interactive)
  10499. (when (calendar-cursor-to-date)
  10500. (let* ((date (calendar-cursor-to-date))
  10501. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10502. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10503. (if (active-minibuffer-window) (exit-minibuffer))))
  10504. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10505. "Insert a date stamp for the date given by the internal TIME.
  10506. WITH-HM means, use the stamp format that includes the time of the day.
  10507. INACTIVE means use square brackets instead of angular ones, so that the
  10508. stamp will not contribute to the agenda.
  10509. PRE and POST are optional strings to be inserted before and after the
  10510. stamp.
  10511. The command returns the inserted time stamp."
  10512. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10513. stamp)
  10514. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10515. (insert-before-markers (or pre ""))
  10516. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10517. (when (listp extra)
  10518. (setq extra (car extra))
  10519. (if (and (stringp extra)
  10520. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10521. (setq extra (format "-%02d:%02d"
  10522. (string-to-number (match-string 1 extra))
  10523. (string-to-number (match-string 2 extra))))
  10524. (setq extra nil)))
  10525. (when extra
  10526. (backward-char 1)
  10527. (insert-before-markers extra)
  10528. (forward-char 1))
  10529. (insert-before-markers (or post ""))
  10530. (setq org-last-inserted-timestamp stamp)))
  10531. (defun org-toggle-time-stamp-overlays ()
  10532. "Toggle the use of custom time stamp formats."
  10533. (interactive)
  10534. (setq org-display-custom-times (not org-display-custom-times))
  10535. (unless org-display-custom-times
  10536. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10537. (while (setq p (next-single-property-change p 'display))
  10538. (if (and (get-text-property p 'display)
  10539. (eq (get-text-property p 'face) 'org-date))
  10540. (remove-text-properties
  10541. p (setq p (next-single-property-change p 'display))
  10542. '(display t))))
  10543. (set-buffer-modified-p bmp)))
  10544. (if (featurep 'xemacs)
  10545. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10546. (org-restart-font-lock)
  10547. (setq org-table-may-need-update t)
  10548. (if org-display-custom-times
  10549. (message "Time stamps are overlayed with custom format")
  10550. (message "Time stamp overlays removed")))
  10551. (defun org-display-custom-time (beg end)
  10552. "Overlay modified time stamp format over timestamp between BEG and END."
  10553. (let* ((ts (buffer-substring beg end))
  10554. t1 w1 with-hm tf time str w2 (off 0))
  10555. (save-match-data
  10556. (setq t1 (org-parse-time-string ts t))
  10557. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10558. (setq off (- (match-end 0) (match-beginning 0)))))
  10559. (setq end (- end off))
  10560. (setq w1 (- end beg)
  10561. with-hm (and (nth 1 t1) (nth 2 t1))
  10562. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10563. time (org-fix-decoded-time t1)
  10564. str (org-add-props
  10565. (format-time-string
  10566. (substring tf 1 -1) (apply 'encode-time time))
  10567. nil 'mouse-face 'highlight)
  10568. w2 (length str))
  10569. (if (not (= w2 w1))
  10570. (add-text-properties (1+ beg) (+ 2 beg)
  10571. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10572. (if (featurep 'xemacs)
  10573. (progn
  10574. (put-text-property beg end 'invisible t)
  10575. (put-text-property beg end 'end-glyph (make-glyph str)))
  10576. (put-text-property beg end 'display str))))
  10577. (defun org-translate-time (string)
  10578. "Translate all timestamps in STRING to custom format.
  10579. But do this only if the variable `org-display-custom-times' is set."
  10580. (when org-display-custom-times
  10581. (save-match-data
  10582. (let* ((start 0)
  10583. (re org-ts-regexp-both)
  10584. t1 with-hm inactive tf time str beg end)
  10585. (while (setq start (string-match re string start))
  10586. (setq beg (match-beginning 0)
  10587. end (match-end 0)
  10588. t1 (save-match-data
  10589. (org-parse-time-string (substring string beg end) t))
  10590. with-hm (and (nth 1 t1) (nth 2 t1))
  10591. inactive (equal (substring string beg (1+ beg)) "[")
  10592. tf (funcall (if with-hm 'cdr 'car)
  10593. org-time-stamp-custom-formats)
  10594. time (org-fix-decoded-time t1)
  10595. str (format-time-string
  10596. (concat
  10597. (if inactive "[" "<") (substring tf 1 -1)
  10598. (if inactive "]" ">"))
  10599. (apply 'encode-time time))
  10600. string (replace-match str t t string)
  10601. start (+ start (length str)))))))
  10602. string)
  10603. (defun org-fix-decoded-time (time)
  10604. "Set 0 instead of nil for the first 6 elements of time.
  10605. Don't touch the rest."
  10606. (let ((n 0))
  10607. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10608. (defun org-days-to-time (timestamp-string)
  10609. "Difference between TIMESTAMP-STRING and now in days."
  10610. (- (time-to-days (org-time-string-to-time timestamp-string))
  10611. (time-to-days (current-time))))
  10612. (defun org-deadline-close (timestamp-string &optional ndays)
  10613. "Is the time in TIMESTAMP-STRING close to the current date?"
  10614. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10615. (and (< (org-days-to-time timestamp-string) ndays)
  10616. (not (org-entry-is-done-p))))
  10617. (defun org-get-wdays (ts)
  10618. "Get the deadline lead time appropriate for timestring TS."
  10619. (cond
  10620. ((<= org-deadline-warning-days 0)
  10621. ;; 0 or negative, enforce this value no matter what
  10622. (- org-deadline-warning-days))
  10623. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10624. ;; lead time is specified.
  10625. (floor (* (string-to-number (match-string 1 ts))
  10626. (cdr (assoc (match-string 2 ts)
  10627. '(("d" . 1) ("w" . 7)
  10628. ("m" . 30.4) ("y" . 365.25)))))))
  10629. ;; go for the default.
  10630. (t org-deadline-warning-days)))
  10631. (defun org-calendar-select-mouse (ev)
  10632. "Return to `org-read-date' with the date currently selected.
  10633. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10634. (interactive "e")
  10635. (mouse-set-point ev)
  10636. (when (calendar-cursor-to-date)
  10637. (let* ((date (calendar-cursor-to-date))
  10638. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10639. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10640. (if (active-minibuffer-window) (exit-minibuffer))))
  10641. (defun org-check-deadlines (ndays)
  10642. "Check if there are any deadlines due or past due.
  10643. A deadline is considered due if it happens within `org-deadline-warning-days'
  10644. days from today's date. If the deadline appears in an entry marked DONE,
  10645. it is not shown. The prefix arg NDAYS can be used to test that many
  10646. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10647. (interactive "P")
  10648. (let* ((org-warn-days
  10649. (cond
  10650. ((equal ndays '(4)) 100000)
  10651. (ndays (prefix-numeric-value ndays))
  10652. (t (abs org-deadline-warning-days))))
  10653. (case-fold-search nil)
  10654. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10655. (callback
  10656. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10657. (message "%d deadlines past-due or due within %d days"
  10658. (org-occur regexp nil callback)
  10659. org-warn-days)))
  10660. (defun org-check-before-date (date)
  10661. "Check if there are deadlines or scheduled entries before DATE."
  10662. (interactive (list (org-read-date)))
  10663. (let ((case-fold-search nil)
  10664. (regexp (concat "\\<\\(" org-deadline-string
  10665. "\\|" org-scheduled-string
  10666. "\\) *<\\([^>]+\\)>"))
  10667. (callback
  10668. (lambda () (time-less-p
  10669. (org-time-string-to-time (match-string 2))
  10670. (org-time-string-to-time date)))))
  10671. (message "%d entries before %s"
  10672. (org-occur regexp nil callback) date)))
  10673. (defun org-evaluate-time-range (&optional to-buffer)
  10674. "Evaluate a time range by computing the difference between start and end.
  10675. Normally the result is just printed in the echo area, but with prefix arg
  10676. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10677. If the time range is actually in a table, the result is inserted into the
  10678. next column.
  10679. For time difference computation, a year is assumed to be exactly 365
  10680. days in order to avoid rounding problems."
  10681. (interactive "P")
  10682. (or
  10683. (org-clock-update-time-maybe)
  10684. (save-excursion
  10685. (unless (org-at-date-range-p t)
  10686. (goto-char (point-at-bol))
  10687. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10688. (if (not (org-at-date-range-p t))
  10689. (error "Not at a time-stamp range, and none found in current line")))
  10690. (let* ((ts1 (match-string 1))
  10691. (ts2 (match-string 2))
  10692. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10693. (match-end (match-end 0))
  10694. (time1 (org-time-string-to-time ts1))
  10695. (time2 (org-time-string-to-time ts2))
  10696. (t1 (time-to-seconds time1))
  10697. (t2 (time-to-seconds time2))
  10698. (diff (abs (- t2 t1)))
  10699. (negative (< (- t2 t1) 0))
  10700. ;; (ys (floor (* 365 24 60 60)))
  10701. (ds (* 24 60 60))
  10702. (hs (* 60 60))
  10703. (fy "%dy %dd %02d:%02d")
  10704. (fy1 "%dy %dd")
  10705. (fd "%dd %02d:%02d")
  10706. (fd1 "%dd")
  10707. (fh "%02d:%02d")
  10708. y d h m align)
  10709. (if havetime
  10710. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10711. y 0
  10712. d (floor (/ diff ds)) diff (mod diff ds)
  10713. h (floor (/ diff hs)) diff (mod diff hs)
  10714. m (floor (/ diff 60)))
  10715. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10716. y 0
  10717. d (floor (+ (/ diff ds) 0.5))
  10718. h 0 m 0))
  10719. (if (not to-buffer)
  10720. (message "%s" (org-make-tdiff-string y d h m))
  10721. (if (org-at-table-p)
  10722. (progn
  10723. (goto-char match-end)
  10724. (setq align t)
  10725. (and (looking-at " *|") (goto-char (match-end 0))))
  10726. (goto-char match-end))
  10727. (if (looking-at
  10728. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10729. (replace-match ""))
  10730. (if negative (insert " -"))
  10731. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10732. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10733. (insert " " (format fh h m))))
  10734. (if align (org-table-align))
  10735. (message "Time difference inserted")))))
  10736. (defun org-make-tdiff-string (y d h m)
  10737. (let ((fmt "")
  10738. (l nil))
  10739. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10740. l (push y l)))
  10741. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10742. l (push d l)))
  10743. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10744. l (push h l)))
  10745. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10746. l (push m l)))
  10747. (apply 'format fmt (nreverse l))))
  10748. (defun org-time-string-to-time (s)
  10749. (apply 'encode-time (org-parse-time-string s)))
  10750. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10751. "Convert a time stamp to an absolute day number.
  10752. If there is a specifyer for a cyclic time stamp, get the closest date to
  10753. DAYNR.
  10754. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10755. (cond
  10756. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10757. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10758. daynr
  10759. (+ daynr 1000)))
  10760. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10761. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10762. (time-to-days (current-time))) (match-string 0 s)
  10763. prefer show-all))
  10764. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10765. (defun org-days-to-iso-week (days)
  10766. "Return the iso week number."
  10767. (require 'cal-iso)
  10768. (car (calendar-iso-from-absolute days)))
  10769. (defun org-small-year-to-year (year)
  10770. "Convert 2-digit years into 4-digit years.
  10771. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10772. The year 2000 cannot be abbreviated. Any year larger than 99
  10773. is returned unchanged."
  10774. (if (< year 38)
  10775. (setq year (+ 2000 year))
  10776. (if (< year 100)
  10777. (setq year (+ 1900 year))))
  10778. year)
  10779. (defun org-time-from-absolute (d)
  10780. "Return the time corresponding to date D.
  10781. D may be an absolute day number, or a calendar-type list (month day year)."
  10782. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10783. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10784. (defun org-calendar-holiday ()
  10785. "List of holidays, for Diary display in Org-mode."
  10786. (require 'holidays)
  10787. (let ((hl (funcall
  10788. (if (fboundp 'calendar-check-holidays)
  10789. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10790. (if hl (mapconcat 'identity hl "; "))))
  10791. (defun org-diary-sexp-entry (sexp entry date)
  10792. "Process a SEXP diary ENTRY for DATE."
  10793. (require 'diary-lib)
  10794. (let ((result (if calendar-debug-sexp
  10795. (let ((stack-trace-on-error t))
  10796. (eval (car (read-from-string sexp))))
  10797. (condition-case nil
  10798. (eval (car (read-from-string sexp)))
  10799. (error
  10800. (beep)
  10801. (message "Bad sexp at line %d in %s: %s"
  10802. (org-current-line)
  10803. (buffer-file-name) sexp)
  10804. (sleep-for 2))))))
  10805. (cond ((stringp result) result)
  10806. ((and (consp result)
  10807. (stringp (cdr result))) (cdr result))
  10808. (result entry)
  10809. (t nil))))
  10810. (defun org-diary-to-ical-string (frombuf)
  10811. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10812. This uses the icalendar.el library."
  10813. (let* ((tmpdir (if (featurep 'xemacs)
  10814. (temp-directory)
  10815. temporary-file-directory))
  10816. (tmpfile (make-temp-name
  10817. (expand-file-name "orgics" tmpdir)))
  10818. buf rtn b e)
  10819. (save-excursion
  10820. (set-buffer frombuf)
  10821. (icalendar-export-region (point-min) (point-max) tmpfile)
  10822. (setq buf (find-buffer-visiting tmpfile))
  10823. (set-buffer buf)
  10824. (goto-char (point-min))
  10825. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10826. (setq b (match-beginning 0)))
  10827. (goto-char (point-max))
  10828. (if (re-search-backward "^END:VEVENT" nil t)
  10829. (setq e (match-end 0)))
  10830. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10831. (kill-buffer buf)
  10832. (delete-file tmpfile)
  10833. rtn))
  10834. (defun org-closest-date (start current change prefer show-all)
  10835. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10836. When PREFER is `past' return a date that is either CURRENT or past.
  10837. When PREFER is `future', return a date that is either CURRENT or future.
  10838. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10839. ;; Make the proper lists from the dates
  10840. (catch 'exit
  10841. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10842. dn dw sday cday n1 n2 n0
  10843. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10844. (setq start (org-date-to-gregorian start)
  10845. current (org-date-to-gregorian
  10846. (if show-all
  10847. current
  10848. (time-to-days (current-time))))
  10849. sday (calendar-absolute-from-gregorian start)
  10850. cday (calendar-absolute-from-gregorian current))
  10851. (if (<= cday sday) (throw 'exit sday))
  10852. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10853. (setq dn (string-to-number (match-string 1 change))
  10854. dw (cdr (assoc (match-string 2 change) a1)))
  10855. (error "Invalid change specifyer: %s" change))
  10856. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10857. (cond
  10858. ((eq dw 'day)
  10859. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10860. n2 (+ n1 dn)))
  10861. ((eq dw 'year)
  10862. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10863. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10864. (setq date1 (list m d y1)
  10865. n1 (calendar-absolute-from-gregorian date1)
  10866. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10867. n2 (calendar-absolute-from-gregorian date2)))
  10868. ((eq dw 'month)
  10869. ;; approx number of month between the two dates
  10870. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10871. ;; How often does dn fit in there?
  10872. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10873. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10874. m (+ m nm)
  10875. ny (floor (/ m 12))
  10876. y (+ y ny)
  10877. m (- m (* ny 12)))
  10878. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10879. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10880. (setq m2 (+ m dn) y2 y)
  10881. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10882. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10883. (while (<= n2 cday)
  10884. (setq n1 n2 m m2 y y2)
  10885. (setq m2 (+ m dn) y2 y)
  10886. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10887. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10888. ;; Make sure n1 is the earlier date
  10889. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  10890. (if show-all
  10891. (cond
  10892. ((eq prefer 'past) n1)
  10893. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10894. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10895. (cond
  10896. ((eq prefer 'past) n1)
  10897. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10898. (t (if (= cday n1) n1 n2)))))))
  10899. (defun org-date-to-gregorian (date)
  10900. "Turn any specification of DATE into a gregorian date for the calendar."
  10901. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10902. ((and (listp date) (= (length date) 3)) date)
  10903. ((stringp date)
  10904. (setq date (org-parse-time-string date))
  10905. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10906. ((listp date)
  10907. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10908. (defun org-parse-time-string (s &optional nodefault)
  10909. "Parse the standard Org-mode time string.
  10910. This should be a lot faster than the normal `parse-time-string'.
  10911. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10912. hour and minute fields will be nil if not given."
  10913. (if (string-match org-ts-regexp0 s)
  10914. (list 0
  10915. (if (or (match-beginning 8) (not nodefault))
  10916. (string-to-number (or (match-string 8 s) "0")))
  10917. (if (or (match-beginning 7) (not nodefault))
  10918. (string-to-number (or (match-string 7 s) "0")))
  10919. (string-to-number (match-string 4 s))
  10920. (string-to-number (match-string 3 s))
  10921. (string-to-number (match-string 2 s))
  10922. nil nil nil)
  10923. (make-list 9 0)))
  10924. (defun org-timestamp-up (&optional arg)
  10925. "Increase the date item at the cursor by one.
  10926. If the cursor is on the year, change the year. If it is on the month or
  10927. the day, change that.
  10928. With prefix ARG, change by that many units."
  10929. (interactive "p")
  10930. (org-timestamp-change (prefix-numeric-value arg)))
  10931. (defun org-timestamp-down (&optional arg)
  10932. "Decrease the date item at the cursor by one.
  10933. If the cursor is on the year, change the year. If it is on the month or
  10934. the day, change that.
  10935. With prefix ARG, change by that many units."
  10936. (interactive "p")
  10937. (org-timestamp-change (- (prefix-numeric-value arg))))
  10938. (defun org-timestamp-up-day (&optional arg)
  10939. "Increase the date in the time stamp by one day.
  10940. With prefix ARG, change that many days."
  10941. (interactive "p")
  10942. (if (and (not (org-at-timestamp-p t))
  10943. (org-on-heading-p))
  10944. (org-todo 'up)
  10945. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10946. (defun org-timestamp-down-day (&optional arg)
  10947. "Decrease the date in the time stamp by one day.
  10948. With prefix ARG, change that many days."
  10949. (interactive "p")
  10950. (if (and (not (org-at-timestamp-p t))
  10951. (org-on-heading-p))
  10952. (org-todo 'down)
  10953. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10954. (defun org-at-timestamp-p (&optional inactive-ok)
  10955. "Determine if the cursor is in or at a timestamp."
  10956. (interactive)
  10957. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10958. (pos (point))
  10959. (ans (or (looking-at tsr)
  10960. (save-excursion
  10961. (skip-chars-backward "^[<\n\r\t")
  10962. (if (> (point) (point-min)) (backward-char 1))
  10963. (and (looking-at tsr)
  10964. (> (- (match-end 0) pos) -1))))))
  10965. (and ans
  10966. (boundp 'org-ts-what)
  10967. (setq org-ts-what
  10968. (cond
  10969. ((= pos (match-beginning 0)) 'bracket)
  10970. ((= pos (1- (match-end 0))) 'bracket)
  10971. ((org-pos-in-match-range pos 2) 'year)
  10972. ((org-pos-in-match-range pos 3) 'month)
  10973. ((org-pos-in-match-range pos 7) 'hour)
  10974. ((org-pos-in-match-range pos 8) 'minute)
  10975. ((or (org-pos-in-match-range pos 4)
  10976. (org-pos-in-match-range pos 5)) 'day)
  10977. ((and (> pos (or (match-end 8) (match-end 5)))
  10978. (< pos (match-end 0)))
  10979. (- pos (or (match-end 8) (match-end 5))))
  10980. (t 'day))))
  10981. ans))
  10982. (defun org-toggle-timestamp-type ()
  10983. "Toggle the type (<active> or [inactive]) of a time stamp."
  10984. (interactive)
  10985. (when (org-at-timestamp-p t)
  10986. (let ((beg (match-beginning 0)) (end (match-end 0))
  10987. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10988. (save-excursion
  10989. (goto-char beg)
  10990. (while (re-search-forward "[][<>]" end t)
  10991. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10992. t t)))
  10993. (message "Timestamp is now %sactive"
  10994. (if (equal (char-after beg) ?<) "" "in")))))
  10995. (defun org-timestamp-change (n &optional what)
  10996. "Change the date in the time stamp at point.
  10997. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10998. `year', `minute', `second'. If WHAT is not given, the cursor position
  10999. in the timestamp determines what will be changed."
  11000. (let ((pos (point))
  11001. with-hm inactive
  11002. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11003. org-ts-what
  11004. extra rem
  11005. ts time time0)
  11006. (if (not (org-at-timestamp-p t))
  11007. (error "Not at a timestamp"))
  11008. (if (and (not what) (eq org-ts-what 'bracket))
  11009. (org-toggle-timestamp-type)
  11010. (if (and (not what) (not (eq org-ts-what 'day))
  11011. org-display-custom-times
  11012. (get-text-property (point) 'display)
  11013. (not (get-text-property (1- (point)) 'display)))
  11014. (setq org-ts-what 'day))
  11015. (setq org-ts-what (or what org-ts-what)
  11016. inactive (= (char-after (match-beginning 0)) ?\[)
  11017. ts (match-string 0))
  11018. (replace-match "")
  11019. (if (string-match
  11020. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11021. ts)
  11022. (setq extra (match-string 1 ts)))
  11023. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11024. (setq with-hm t))
  11025. (setq time0 (org-parse-time-string ts))
  11026. (when (and (eq org-ts-what 'minute)
  11027. (eq current-prefix-arg nil))
  11028. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11029. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11030. (setcar (cdr time0) (+ (nth 1 time0)
  11031. (if (> n 0) (- rem) (- dm rem))))))
  11032. (setq time
  11033. (encode-time (or (car time0) 0)
  11034. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11035. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11036. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11037. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11038. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11039. (nthcdr 6 time0)))
  11040. (when (integerp org-ts-what)
  11041. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11042. (if (eq what 'calendar)
  11043. (let ((cal-date (org-get-date-from-calendar)))
  11044. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11045. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11046. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11047. (setcar time0 (or (car time0) 0))
  11048. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11049. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11050. (setq time (apply 'encode-time time0))))
  11051. (setq org-last-changed-timestamp
  11052. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11053. (org-clock-update-time-maybe)
  11054. (goto-char pos)
  11055. ;; Try to recenter the calendar window, if any
  11056. (if (and org-calendar-follow-timestamp-change
  11057. (get-buffer-window "*Calendar*" t)
  11058. (memq org-ts-what '(day month year)))
  11059. (org-recenter-calendar (time-to-days time))))))
  11060. (defun org-modify-ts-extra (s pos n dm)
  11061. "Change the different parts of the lead-time and repeat fields in timestamp."
  11062. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11063. ng h m new rem)
  11064. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11065. (cond
  11066. ((or (org-pos-in-match-range pos 2)
  11067. (org-pos-in-match-range pos 3))
  11068. (setq m (string-to-number (match-string 3 s))
  11069. h (string-to-number (match-string 2 s)))
  11070. (if (org-pos-in-match-range pos 2)
  11071. (setq h (+ h n))
  11072. (setq n (* dm (org-no-warnings (signum n))))
  11073. (when (not (= 0 (setq rem (% m dm))))
  11074. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11075. (setq m (+ m n)))
  11076. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11077. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11078. (setq h (min 24 (max 0 h)))
  11079. (setq ng 1 new (format "-%02d:%02d" h m)))
  11080. ((org-pos-in-match-range pos 6)
  11081. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11082. ((org-pos-in-match-range pos 5)
  11083. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11084. ((org-pos-in-match-range pos 9)
  11085. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11086. ((org-pos-in-match-range pos 8)
  11087. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11088. (when ng
  11089. (setq s (concat
  11090. (substring s 0 (match-beginning ng))
  11091. new
  11092. (substring s (match-end ng))))))
  11093. s))
  11094. (defun org-recenter-calendar (date)
  11095. "If the calendar is visible, recenter it to DATE."
  11096. (let* ((win (selected-window))
  11097. (cwin (get-buffer-window "*Calendar*" t))
  11098. (calendar-move-hook nil))
  11099. (when cwin
  11100. (select-window cwin)
  11101. (calendar-goto-date (if (listp date) date
  11102. (calendar-gregorian-from-absolute date)))
  11103. (select-window win))))
  11104. (defun org-goto-calendar (&optional arg)
  11105. "Go to the Emacs calendar at the current date.
  11106. If there is a time stamp in the current line, go to that date.
  11107. A prefix ARG can be used to force the current date."
  11108. (interactive "P")
  11109. (let ((tsr org-ts-regexp) diff
  11110. (calendar-move-hook nil)
  11111. (calendar-view-holidays-initially-flag nil)
  11112. (view-calendar-holidays-initially nil)
  11113. (calendar-view-diary-initially-flag nil)
  11114. (view-diary-entries-initially nil))
  11115. (if (or (org-at-timestamp-p)
  11116. (save-excursion
  11117. (beginning-of-line 1)
  11118. (looking-at (concat ".*" tsr))))
  11119. (let ((d1 (time-to-days (current-time)))
  11120. (d2 (time-to-days
  11121. (org-time-string-to-time (match-string 1)))))
  11122. (setq diff (- d2 d1))))
  11123. (calendar)
  11124. (calendar-goto-today)
  11125. (if (and diff (not arg)) (calendar-forward-day diff))))
  11126. (defun org-get-date-from-calendar ()
  11127. "Return a list (month day year) of date at point in calendar."
  11128. (with-current-buffer "*Calendar*"
  11129. (save-match-data
  11130. (calendar-cursor-to-date))))
  11131. (defun org-date-from-calendar ()
  11132. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11133. If there is already a time stamp at the cursor position, update it."
  11134. (interactive)
  11135. (if (org-at-timestamp-p t)
  11136. (org-timestamp-change 0 'calendar)
  11137. (let ((cal-date (org-get-date-from-calendar)))
  11138. (org-insert-time-stamp
  11139. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11140. (defun org-minutes-to-hh:mm-string (m)
  11141. "Compute H:MM from a number of minutes."
  11142. (let ((h (/ m 60)))
  11143. (setq m (- m (* 60 h)))
  11144. (format org-time-clocksum-format h m)))
  11145. (defun org-hh:mm-string-to-minutes (s)
  11146. "Convert a string H:MM to a number of minutes."
  11147. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11148. (+ (* (string-to-number (match-string 1 s)) 60)
  11149. (string-to-number (match-string 2 s)))
  11150. 0))
  11151. ;;;; Agenda files
  11152. ;;;###autoload
  11153. (defun org-iswitchb (&optional arg)
  11154. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11155. With a prefix argument, restrict available to files.
  11156. With two prefix arguments, restrict available buffers to agenda files.
  11157. Due to some yet unresolved reason, the global function
  11158. `iswitchb-mode' needs to be active for this function to work."
  11159. (interactive "P")
  11160. (require 'iswitchb)
  11161. (let ((enabled iswitchb-mode) blist)
  11162. (or enabled (iswitchb-mode 1))
  11163. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11164. ((equal arg '(16)) (org-buffer-list 'agenda))
  11165. (t (org-buffer-list))))
  11166. (unwind-protect
  11167. (let ((iswitchb-make-buflist-hook
  11168. (lambda ()
  11169. (setq iswitchb-temp-buflist
  11170. (mapcar 'buffer-name blist)))))
  11171. (switch-to-buffer
  11172. (iswitchb-read-buffer
  11173. "Switch-to: " nil t))
  11174. (or enabled (iswitchb-mode -1))))))
  11175. ;;;###autoload
  11176. (defun org-ido-switchb (&optional arg)
  11177. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11178. With a prefix argument, restrict available to files.
  11179. With two prefix arguments, restrict available buffers to agenda files."
  11180. (interactive "P")
  11181. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11182. ((equal arg '(16)) (org-buffer-list 'agenda))
  11183. (t (org-buffer-list)))))
  11184. (switch-to-buffer
  11185. (org-ido-completing-read "Org buffer: "
  11186. (mapcar 'buffer-name blist)
  11187. nil t))))
  11188. (defun org-buffer-list (&optional predicate exclude-tmp)
  11189. "Return a list of Org buffers.
  11190. PREDICATE can be `export', `files' or `agenda'.
  11191. export restrict the list to Export buffers.
  11192. files restrict the list to buffers visiting Org files.
  11193. agenda restrict the list to buffers visiting agenda files.
  11194. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11195. (let* ((bfn nil)
  11196. (agenda-files (and (eq predicate 'agenda)
  11197. (mapcar 'file-truename (org-agenda-files t))))
  11198. (filter
  11199. (cond
  11200. ((eq predicate 'files)
  11201. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11202. ((eq predicate 'export)
  11203. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11204. ((eq predicate 'agenda)
  11205. (lambda (b)
  11206. (with-current-buffer b
  11207. (and (eq major-mode 'org-mode)
  11208. (setq bfn (buffer-file-name b))
  11209. (member (file-truename bfn) agenda-files)))))
  11210. (t (lambda (b) (with-current-buffer b
  11211. (or (eq major-mode 'org-mode)
  11212. (string-match "\*Org .*Export"
  11213. (buffer-name b)))))))))
  11214. (delq nil
  11215. (mapcar
  11216. (lambda(b)
  11217. (if (and (funcall filter b)
  11218. (or (not exclude-tmp)
  11219. (not (string-match "tmp" (buffer-name b)))))
  11220. b
  11221. nil))
  11222. (buffer-list)))))
  11223. (defun org-agenda-files (&optional unrestricted archives)
  11224. "Get the list of agenda files.
  11225. Optional UNRESTRICTED means return the full list even if a restriction
  11226. is currently in place.
  11227. When ARCHIVES is t, include all archive files hat are really being
  11228. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11229. `org-agenda-archives-mode' is t."
  11230. (let ((files
  11231. (cond
  11232. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11233. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11234. ((listp org-agenda-files) org-agenda-files)
  11235. (t (error "Invalid value of `org-agenda-files'")))))
  11236. (setq files (apply 'append
  11237. (mapcar (lambda (f)
  11238. (if (file-directory-p f)
  11239. (directory-files
  11240. f t org-agenda-file-regexp)
  11241. (list f)))
  11242. files)))
  11243. (when org-agenda-skip-unavailable-files
  11244. (setq files (delq nil
  11245. (mapcar (function
  11246. (lambda (file)
  11247. (and (file-readable-p file) file)))
  11248. files))))
  11249. (when (or (eq archives t)
  11250. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11251. (setq files (org-add-archive-files files)))
  11252. files))
  11253. (defun org-edit-agenda-file-list ()
  11254. "Edit the list of agenda files.
  11255. Depending on setup, this either uses customize to edit the variable
  11256. `org-agenda-files', or it visits the file that is holding the list. In the
  11257. latter case, the buffer is set up in a way that saving it automatically kills
  11258. the buffer and restores the previous window configuration."
  11259. (interactive)
  11260. (if (stringp org-agenda-files)
  11261. (let ((cw (current-window-configuration)))
  11262. (find-file org-agenda-files)
  11263. (org-set-local 'org-window-configuration cw)
  11264. (org-add-hook 'after-save-hook
  11265. (lambda ()
  11266. (set-window-configuration
  11267. (prog1 org-window-configuration
  11268. (kill-buffer (current-buffer))))
  11269. (org-install-agenda-files-menu)
  11270. (message "New agenda file list installed"))
  11271. nil 'local)
  11272. (message "%s" (substitute-command-keys
  11273. "Edit list and finish with \\[save-buffer]")))
  11274. (customize-variable 'org-agenda-files)))
  11275. (defun org-store-new-agenda-file-list (list)
  11276. "Set new value for the agenda file list and save it correctly."
  11277. (if (stringp org-agenda-files)
  11278. (let ((f org-agenda-files) b)
  11279. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11280. (with-temp-file f
  11281. (insert (mapconcat 'identity list "\n") "\n")))
  11282. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11283. (setq org-agenda-files list)
  11284. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11285. (defun org-read-agenda-file-list ()
  11286. "Read the list of agenda files from a file."
  11287. (when (file-directory-p org-agenda-files)
  11288. (error "`org-agenda-files' cannot be a single directory"))
  11289. (when (stringp org-agenda-files)
  11290. (with-temp-buffer
  11291. (insert-file-contents org-agenda-files)
  11292. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11293. ;;;###autoload
  11294. (defun org-cycle-agenda-files ()
  11295. "Cycle through the files in `org-agenda-files'.
  11296. If the current buffer visits an agenda file, find the next one in the list.
  11297. If the current buffer does not, find the first agenda file."
  11298. (interactive)
  11299. (let* ((fs (org-agenda-files t))
  11300. (files (append fs (list (car fs))))
  11301. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11302. file)
  11303. (unless files (error "No agenda files"))
  11304. (catch 'exit
  11305. (while (setq file (pop files))
  11306. (if (equal (file-truename file) tcf)
  11307. (when (car files)
  11308. (find-file (car files))
  11309. (throw 'exit t))))
  11310. (find-file (car fs)))
  11311. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11312. (defun org-agenda-file-to-front (&optional to-end)
  11313. "Move/add the current file to the top of the agenda file list.
  11314. If the file is not present in the list, it is added to the front. If it is
  11315. present, it is moved there. With optional argument TO-END, add/move to the
  11316. end of the list."
  11317. (interactive "P")
  11318. (let ((org-agenda-skip-unavailable-files nil)
  11319. (file-alist (mapcar (lambda (x)
  11320. (cons (file-truename x) x))
  11321. (org-agenda-files t)))
  11322. (ctf (file-truename buffer-file-name))
  11323. x had)
  11324. (setq x (assoc ctf file-alist) had x)
  11325. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11326. (if to-end
  11327. (setq file-alist (append (delq x file-alist) (list x)))
  11328. (setq file-alist (cons x (delq x file-alist))))
  11329. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11330. (org-install-agenda-files-menu)
  11331. (message "File %s to %s of agenda file list"
  11332. (if had "moved" "added") (if to-end "end" "front"))))
  11333. (defun org-remove-file (&optional file)
  11334. "Remove current file from the list of files in variable `org-agenda-files'.
  11335. These are the files which are being checked for agenda entries.
  11336. Optional argument FILE means, use this file instead of the current."
  11337. (interactive)
  11338. (let* ((org-agenda-skip-unavailable-files nil)
  11339. (file (or file buffer-file-name))
  11340. (true-file (file-truename file))
  11341. (afile (abbreviate-file-name file))
  11342. (files (delq nil (mapcar
  11343. (lambda (x)
  11344. (if (equal true-file
  11345. (file-truename x))
  11346. nil x))
  11347. (org-agenda-files t)))))
  11348. (if (not (= (length files) (length (org-agenda-files t))))
  11349. (progn
  11350. (org-store-new-agenda-file-list files)
  11351. (org-install-agenda-files-menu)
  11352. (message "Removed file: %s" afile))
  11353. (message "File was not in list: %s (not removed)" afile))))
  11354. (defun org-file-menu-entry (file)
  11355. (vector file (list 'find-file file) t))
  11356. (defun org-check-agenda-file (file)
  11357. "Make sure FILE exists. If not, ask user what to do."
  11358. (when (not (file-exists-p file))
  11359. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11360. (abbreviate-file-name file))
  11361. (let ((r (downcase (read-char-exclusive))))
  11362. (cond
  11363. ((equal r ?r)
  11364. (org-remove-file file)
  11365. (throw 'nextfile t))
  11366. (t (error "Abort"))))))
  11367. (defun org-get-agenda-file-buffer (file)
  11368. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11369. it to the list of buffers which might be released later."
  11370. (let ((buf (org-find-base-buffer-visiting file)))
  11371. (if buf
  11372. buf ; just return it
  11373. ;; Make a new buffer and remember it
  11374. (setq buf (find-file-noselect file))
  11375. (if buf (push buf org-agenda-new-buffers))
  11376. buf)))
  11377. (defun org-release-buffers (blist)
  11378. "Release all buffers in list, asking the user for confirmation when needed.
  11379. When a buffer is unmodified, it is just killed. When modified, it is saved
  11380. \(if the user agrees) and then killed."
  11381. (let (buf file)
  11382. (while (setq buf (pop blist))
  11383. (setq file (buffer-file-name buf))
  11384. (when (and (buffer-modified-p buf)
  11385. file
  11386. (y-or-n-p (format "Save file %s? " file)))
  11387. (with-current-buffer buf (save-buffer)))
  11388. (kill-buffer buf))))
  11389. (defun org-prepare-agenda-buffers (files)
  11390. "Create buffers for all agenda files, protect archived trees and comments."
  11391. (interactive)
  11392. (let ((pa '(:org-archived t))
  11393. (pc '(:org-comment t))
  11394. (pall '(:org-archived t :org-comment t))
  11395. (inhibit-read-only t)
  11396. (rea (concat ":" org-archive-tag ":"))
  11397. bmp file re)
  11398. (save-excursion
  11399. (save-restriction
  11400. (while (setq file (pop files))
  11401. (if (bufferp file)
  11402. (set-buffer file)
  11403. (org-check-agenda-file file)
  11404. (set-buffer (org-get-agenda-file-buffer file)))
  11405. (widen)
  11406. (setq bmp (buffer-modified-p))
  11407. (org-refresh-category-properties)
  11408. (setq org-todo-keywords-for-agenda
  11409. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11410. (setq org-done-keywords-for-agenda
  11411. (append org-done-keywords-for-agenda org-done-keywords))
  11412. (setq org-todo-keyword-alist-for-agenda
  11413. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11414. (setq org-tag-alist-for-agenda
  11415. (append org-tag-alist-for-agenda org-tag-alist))
  11416. (save-excursion
  11417. (remove-text-properties (point-min) (point-max) pall)
  11418. (when org-agenda-skip-archived-trees
  11419. (goto-char (point-min))
  11420. (while (re-search-forward rea nil t)
  11421. (if (org-on-heading-p t)
  11422. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11423. (goto-char (point-min))
  11424. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11425. (while (re-search-forward re nil t)
  11426. (add-text-properties
  11427. (match-beginning 0) (org-end-of-subtree t) pc)))
  11428. (set-buffer-modified-p bmp))))
  11429. (setq org-todo-keyword-alist-for-agenda
  11430. (org-uniquify org-todo-keyword-alist-for-agenda)
  11431. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11432. ;;;; Embedded LaTeX
  11433. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11434. "Keymap for the minor `org-cdlatex-mode'.")
  11435. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11436. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11437. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11438. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11439. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11440. (defvar org-cdlatex-texmathp-advice-is-done nil
  11441. "Flag remembering if we have applied the advice to texmathp already.")
  11442. (define-minor-mode org-cdlatex-mode
  11443. "Toggle the minor `org-cdlatex-mode'.
  11444. This mode supports entering LaTeX environment and math in LaTeX fragments
  11445. in Org-mode.
  11446. \\{org-cdlatex-mode-map}"
  11447. nil " OCDL" nil
  11448. (when org-cdlatex-mode (require 'cdlatex))
  11449. (unless org-cdlatex-texmathp-advice-is-done
  11450. (setq org-cdlatex-texmathp-advice-is-done t)
  11451. (defadvice texmathp (around org-math-always-on activate)
  11452. "Always return t in org-mode buffers.
  11453. This is because we want to insert math symbols without dollars even outside
  11454. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11455. an embedded LaTeX fragment, let texmathp do its job.
  11456. \\[org-cdlatex-mode-map]"
  11457. (interactive)
  11458. (let (p)
  11459. (cond
  11460. ((not (org-mode-p)) ad-do-it)
  11461. ((eq this-command 'cdlatex-math-symbol)
  11462. (setq ad-return-value t
  11463. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11464. (t
  11465. (let ((p (org-inside-LaTeX-fragment-p)))
  11466. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11467. (setq ad-return-value t
  11468. texmathp-why '("Org-mode embedded math" . 0))
  11469. (if p ad-do-it)))))))))
  11470. (defun turn-on-org-cdlatex ()
  11471. "Unconditionally turn on `org-cdlatex-mode'."
  11472. (org-cdlatex-mode 1))
  11473. (defun org-inside-LaTeX-fragment-p ()
  11474. "Test if point is inside a LaTeX fragment.
  11475. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11476. sequence appearing also before point.
  11477. Even though the matchers for math are configurable, this function assumes
  11478. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11479. delimiters are skipped when they have been removed by customization.
  11480. The return value is nil, or a cons cell with the delimiter and
  11481. and the position of this delimiter.
  11482. This function does a reasonably good job, but can locally be fooled by
  11483. for example currency specifications. For example it will assume being in
  11484. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11485. fragments that are properly closed, but during editing, we have to live
  11486. with the uncertainty caused by missing closing delimiters. This function
  11487. looks only before point, not after."
  11488. (catch 'exit
  11489. (let ((pos (point))
  11490. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11491. (lim (progn
  11492. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11493. (point)))
  11494. dd-on str (start 0) m re)
  11495. (goto-char pos)
  11496. (when dodollar
  11497. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11498. re (nth 1 (assoc "$" org-latex-regexps)))
  11499. (while (string-match re str start)
  11500. (cond
  11501. ((= (match-end 0) (length str))
  11502. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11503. ((= (match-end 0) (- (length str) 5))
  11504. (throw 'exit nil))
  11505. (t (setq start (match-end 0))))))
  11506. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11507. (goto-char pos)
  11508. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11509. (and (match-beginning 2) (throw 'exit nil))
  11510. ;; count $$
  11511. (while (re-search-backward "\\$\\$" lim t)
  11512. (setq dd-on (not dd-on)))
  11513. (goto-char pos)
  11514. (if dd-on (cons "$$" m))))))
  11515. (defun org-try-cdlatex-tab ()
  11516. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11517. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11518. - inside a LaTeX fragment, or
  11519. - after the first word in a line, where an abbreviation expansion could
  11520. insert a LaTeX environment."
  11521. (when org-cdlatex-mode
  11522. (cond
  11523. ((save-excursion
  11524. (skip-chars-backward "a-zA-Z0-9*")
  11525. (skip-chars-backward " \t")
  11526. (bolp))
  11527. (cdlatex-tab) t)
  11528. ((org-inside-LaTeX-fragment-p)
  11529. (cdlatex-tab) t)
  11530. (t nil))))
  11531. (defun org-cdlatex-underscore-caret (&optional arg)
  11532. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11533. Revert to the normal definition outside of these fragments."
  11534. (interactive "P")
  11535. (if (org-inside-LaTeX-fragment-p)
  11536. (call-interactively 'cdlatex-sub-superscript)
  11537. (let (org-cdlatex-mode)
  11538. (call-interactively (key-binding (vector last-input-event))))))
  11539. (defun org-cdlatex-math-modify (&optional arg)
  11540. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11541. Revert to the normal definition outside of these fragments."
  11542. (interactive "P")
  11543. (if (org-inside-LaTeX-fragment-p)
  11544. (call-interactively 'cdlatex-math-modify)
  11545. (let (org-cdlatex-mode)
  11546. (call-interactively (key-binding (vector last-input-event))))))
  11547. (defvar org-latex-fragment-image-overlays nil
  11548. "List of overlays carrying the images of latex fragments.")
  11549. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11550. (defun org-remove-latex-fragment-image-overlays ()
  11551. "Remove all overlays with LaTeX fragment images in current buffer."
  11552. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11553. (setq org-latex-fragment-image-overlays nil))
  11554. (defun org-preview-latex-fragment (&optional subtree)
  11555. "Preview the LaTeX fragment at point, or all locally or globally.
  11556. If the cursor is in a LaTeX fragment, create the image and overlay
  11557. it over the source code. If there is no fragment at point, display
  11558. all fragments in the current text, from one headline to the next. With
  11559. prefix SUBTREE, display all fragments in the current subtree. With a
  11560. double prefix `C-u C-u', or when the cursor is before the first headline,
  11561. display all fragments in the buffer.
  11562. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11563. (interactive "P")
  11564. (org-remove-latex-fragment-image-overlays)
  11565. (save-excursion
  11566. (save-restriction
  11567. (let (beg end at msg)
  11568. (cond
  11569. ((or (equal subtree '(16))
  11570. (not (save-excursion
  11571. (re-search-backward (concat "^" outline-regexp) nil t))))
  11572. (setq beg (point-min) end (point-max)
  11573. msg "Creating images for buffer...%s"))
  11574. ((equal subtree '(4))
  11575. (org-back-to-heading)
  11576. (setq beg (point) end (org-end-of-subtree t)
  11577. msg "Creating images for subtree...%s"))
  11578. (t
  11579. (if (setq at (org-inside-LaTeX-fragment-p))
  11580. (goto-char (max (point-min) (- (cdr at) 2)))
  11581. (org-back-to-heading))
  11582. (setq beg (point) end (progn (outline-next-heading) (point))
  11583. msg (if at "Creating image...%s"
  11584. "Creating images for entry...%s"))))
  11585. (message msg "")
  11586. (narrow-to-region beg end)
  11587. (goto-char beg)
  11588. (org-format-latex
  11589. (concat "ltxpng/" (file-name-sans-extension
  11590. (file-name-nondirectory
  11591. buffer-file-name)))
  11592. default-directory 'overlays msg at 'forbuffer)
  11593. (message msg "done. Use `C-c C-c' to remove images.")))))
  11594. (defvar org-latex-regexps
  11595. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11596. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11597. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11598. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11599. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11600. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11601. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11602. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11603. "Regular expressions for matching embedded LaTeX.")
  11604. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11605. "Replace LaTeX fragments with links to an image, and produce images."
  11606. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11607. (let* ((prefixnodir (file-name-nondirectory prefix))
  11608. (absprefix (expand-file-name prefix dir))
  11609. (todir (file-name-directory absprefix))
  11610. (opt org-format-latex-options)
  11611. (matchers (plist-get opt :matchers))
  11612. (re-list org-latex-regexps)
  11613. (cnt 0) txt link beg end re e checkdir
  11614. m n block linkfile movefile ov)
  11615. ;; Check if there are old images files with this prefix, and remove them
  11616. (when (file-directory-p todir)
  11617. (mapc 'delete-file
  11618. (directory-files
  11619. todir 'full
  11620. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11621. ;; Check the different regular expressions
  11622. (while (setq e (pop re-list))
  11623. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11624. block (if (nth 3 e) "\n\n" ""))
  11625. (when (member m matchers)
  11626. (goto-char (point-min))
  11627. (while (re-search-forward re nil t)
  11628. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11629. (setq txt (match-string n)
  11630. beg (match-beginning n) end (match-end n)
  11631. cnt (1+ cnt)
  11632. linkfile (format "%s_%04d.png" prefix cnt)
  11633. movefile (format "%s_%04d.png" absprefix cnt)
  11634. link (concat block "[[file:" linkfile "]]" block))
  11635. (if msg (message msg cnt))
  11636. (goto-char beg)
  11637. (unless checkdir ; make sure the directory exists
  11638. (setq checkdir t)
  11639. (or (file-directory-p todir) (make-directory todir)))
  11640. (org-create-formula-image
  11641. txt movefile opt forbuffer)
  11642. (if overlays
  11643. (progn
  11644. (setq ov (org-make-overlay beg end))
  11645. (if (featurep 'xemacs)
  11646. (progn
  11647. (org-overlay-put ov 'invisible t)
  11648. (org-overlay-put
  11649. ov 'end-glyph
  11650. (make-glyph (vector 'png :file movefile))))
  11651. (org-overlay-put
  11652. ov 'display
  11653. (list 'image :type 'png :file movefile :ascent 'center)))
  11654. (push ov org-latex-fragment-image-overlays)
  11655. (goto-char end))
  11656. (delete-region beg end)
  11657. (insert link))))))))
  11658. ;; This function borrows from Ganesh Swami's latex2png.el
  11659. (defun org-create-formula-image (string tofile options buffer)
  11660. (let* ((tmpdir (if (featurep 'xemacs)
  11661. (temp-directory)
  11662. temporary-file-directory))
  11663. (texfilebase (make-temp-name
  11664. (expand-file-name "orgtex" tmpdir)))
  11665. (texfile (concat texfilebase ".tex"))
  11666. (dvifile (concat texfilebase ".dvi"))
  11667. (pngfile (concat texfilebase ".png"))
  11668. (fnh (if (featurep 'xemacs)
  11669. (font-height (get-face-font 'default))
  11670. (face-attribute 'default :height nil)))
  11671. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11672. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11673. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11674. "Black"))
  11675. (bg (or (plist-get options (if buffer :background :html-background))
  11676. "Transparent")))
  11677. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11678. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11679. (with-temp-file texfile
  11680. (insert org-format-latex-header
  11681. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11682. (let ((dir default-directory))
  11683. (condition-case nil
  11684. (progn
  11685. (cd tmpdir)
  11686. (call-process "latex" nil nil nil texfile))
  11687. (error nil))
  11688. (cd dir))
  11689. (if (not (file-exists-p dvifile))
  11690. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11691. (condition-case nil
  11692. (call-process "dvipng" nil nil nil
  11693. "-E" "-fg" fg "-bg" bg
  11694. "-D" dpi
  11695. ;;"-x" scale "-y" scale
  11696. "-T" "tight"
  11697. "-o" pngfile
  11698. dvifile)
  11699. (error nil))
  11700. (if (not (file-exists-p pngfile))
  11701. (progn (message "Failed to create png file from %s" texfile) nil)
  11702. ;; Use the requested file name and clean up
  11703. (copy-file pngfile tofile 'replace)
  11704. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11705. (delete-file (concat texfilebase e)))
  11706. pngfile))))
  11707. (defun org-dvipng-color (attr)
  11708. "Return an rgb color specification for dvipng."
  11709. (apply 'format "rgb %s %s %s"
  11710. (mapcar 'org-normalize-color
  11711. (color-values (face-attribute 'default attr nil)))))
  11712. (defun org-normalize-color (value)
  11713. "Return string to be used as color value for an RGB component."
  11714. (format "%g" (/ value 65535.0)))
  11715. ;;;; Key bindings
  11716. ;; Make `C-c C-x' a prefix key
  11717. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11718. ;; TAB key with modifiers
  11719. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11720. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11721. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11722. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11723. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11724. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11725. ;; The following line is necessary under Suse GNU/Linux
  11726. (unless (featurep 'xemacs)
  11727. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11728. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11729. (define-key org-mode-map [backtab] 'org-shifttab)
  11730. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11731. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11732. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11733. ;; Cursor keys with modifiers
  11734. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11735. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11736. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11737. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11738. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11739. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11740. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11741. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11742. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11743. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11744. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11745. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11746. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11747. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11748. ;;; Extra keys for tty access.
  11749. ;; We only set them when really needed because otherwise the
  11750. ;; menus don't show the simple keys
  11751. (when (or org-use-extra-keys
  11752. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11753. (not window-system))
  11754. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11755. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11756. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11757. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11758. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11759. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11760. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11761. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11762. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11763. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11764. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11765. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11766. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11767. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11768. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11769. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11770. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11771. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11772. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11773. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11774. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11775. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11776. ;; All the other keys
  11777. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11778. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11779. (if (boundp 'narrow-map)
  11780. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11781. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11782. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11783. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11784. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11785. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11786. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11787. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11788. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11789. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11790. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11791. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11792. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11793. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11794. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11795. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11796. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11797. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11798. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11799. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11800. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11801. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11802. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11803. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11804. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11805. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11806. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11807. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11808. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11809. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11810. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11811. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11812. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11813. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11814. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11815. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11816. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11817. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11818. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11819. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11820. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11821. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11822. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11823. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11824. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11825. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11826. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11827. (org-defkey org-mode-map "\C-m" 'org-return)
  11828. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11829. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11830. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11831. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11832. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11833. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11834. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11835. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11836. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11837. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11838. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11839. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11840. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11841. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11842. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11843. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11844. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11845. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11846. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11847. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11848. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11849. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11850. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11851. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11852. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11853. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11854. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11855. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11856. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11857. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11858. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11859. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11860. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  11861. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11862. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11863. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11864. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11865. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  11866. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11867. (when (featurep 'xemacs)
  11868. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11869. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11870. (defun org-self-insert-command (N)
  11871. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11872. If the cursor is in a table looking at whitespace, the whitespace is
  11873. overwritten, and the table is not marked as requiring realignment."
  11874. (interactive "p")
  11875. (if (and (org-table-p)
  11876. (progn
  11877. ;; check if we blank the field, and if that triggers align
  11878. (and (featurep 'org-table) org-table-auto-blank-field
  11879. (member last-command
  11880. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11881. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11882. ;; got extra space, this field does not determine column width
  11883. (let (org-table-may-need-update) (org-table-blank-field))
  11884. ;; no extra space, this field may determine column width
  11885. (org-table-blank-field)))
  11886. t)
  11887. (eq N 1)
  11888. (looking-at "[^|\n]* |"))
  11889. (let (org-table-may-need-update)
  11890. (goto-char (1- (match-end 0)))
  11891. (delete-backward-char 1)
  11892. (goto-char (match-beginning 0))
  11893. (self-insert-command N))
  11894. (setq org-table-may-need-update t)
  11895. (self-insert-command N)
  11896. (org-fix-tags-on-the-fly)))
  11897. (defun org-fix-tags-on-the-fly ()
  11898. (when (and (equal (char-after (point-at-bol)) ?*)
  11899. (org-on-heading-p))
  11900. (org-align-tags-here org-tags-column)))
  11901. (defun org-delete-backward-char (N)
  11902. "Like `delete-backward-char', insert whitespace at field end in tables.
  11903. When deleting backwards, in tables this function will insert whitespace in
  11904. front of the next \"|\" separator, to keep the table aligned. The table will
  11905. still be marked for re-alignment if the field did fill the entire column,
  11906. because, in this case the deletion might narrow the column."
  11907. (interactive "p")
  11908. (if (and (org-table-p)
  11909. (eq N 1)
  11910. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11911. (looking-at ".*?|"))
  11912. (let ((pos (point))
  11913. (noalign (looking-at "[^|\n\r]* |"))
  11914. (c org-table-may-need-update))
  11915. (backward-delete-char N)
  11916. (skip-chars-forward "^|")
  11917. (insert " ")
  11918. (goto-char (1- pos))
  11919. ;; noalign: if there were two spaces at the end, this field
  11920. ;; does not determine the width of the column.
  11921. (if noalign (setq org-table-may-need-update c)))
  11922. (backward-delete-char N)
  11923. (org-fix-tags-on-the-fly)))
  11924. (defun org-delete-char (N)
  11925. "Like `delete-char', but insert whitespace at field end in tables.
  11926. When deleting characters, in tables this function will insert whitespace in
  11927. front of the next \"|\" separator, to keep the table aligned. The table will
  11928. still be marked for re-alignment if the field did fill the entire column,
  11929. because, in this case the deletion might narrow the column."
  11930. (interactive "p")
  11931. (if (and (org-table-p)
  11932. (not (bolp))
  11933. (not (= (char-after) ?|))
  11934. (eq N 1))
  11935. (if (looking-at ".*?|")
  11936. (let ((pos (point))
  11937. (noalign (looking-at "[^|\n\r]* |"))
  11938. (c org-table-may-need-update))
  11939. (replace-match (concat
  11940. (substring (match-string 0) 1 -1)
  11941. " |"))
  11942. (goto-char pos)
  11943. ;; noalign: if there were two spaces at the end, this field
  11944. ;; does not determine the width of the column.
  11945. (if noalign (setq org-table-may-need-update c)))
  11946. (delete-char N))
  11947. (delete-char N)
  11948. (org-fix-tags-on-the-fly)))
  11949. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11950. (put 'org-self-insert-command 'delete-selection t)
  11951. (put 'orgtbl-self-insert-command 'delete-selection t)
  11952. (put 'org-delete-char 'delete-selection 'supersede)
  11953. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11954. (put 'org-yank 'delete-selection 'yank)
  11955. ;; Make `flyspell-mode' delay after some commands
  11956. (put 'org-self-insert-command 'flyspell-delayed t)
  11957. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11958. (put 'org-delete-char 'flyspell-delayed t)
  11959. (put 'org-delete-backward-char 'flyspell-delayed t)
  11960. ;; Make pabbrev-mode expand after org-mode commands
  11961. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11962. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11963. ;; How to do this: Measure non-white length of current string
  11964. ;; If equal to column width, we should realign.
  11965. (defun org-remap (map &rest commands)
  11966. "In MAP, remap the functions given in COMMANDS.
  11967. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11968. (let (new old)
  11969. (while commands
  11970. (setq old (pop commands) new (pop commands))
  11971. (if (fboundp 'command-remapping)
  11972. (org-defkey map (vector 'remap old) new)
  11973. (substitute-key-definition old new map global-map)))))
  11974. (when (eq org-enable-table-editor 'optimized)
  11975. ;; If the user wants maximum table support, we need to hijack
  11976. ;; some standard editing functions
  11977. (org-remap org-mode-map
  11978. 'self-insert-command 'org-self-insert-command
  11979. 'delete-char 'org-delete-char
  11980. 'delete-backward-char 'org-delete-backward-char)
  11981. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11982. (defun org-modifier-cursor-error ()
  11983. "Throw an error, a modified cursor command was applied in wrong context."
  11984. (error "This command is active in special context like tables, headlines or items"))
  11985. (defun org-shiftselect-error ()
  11986. "Throw an error because Shift-Cursor command was applied in wrong context."
  11987. (if (and (boundp 'shift-select-mode) shift-select-mode)
  11988. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  11989. (error "This command works only in special context like headlines or timestamps.")))
  11990. (defun org-call-for-shift-select (cmd)
  11991. (let ((this-command-keys-shift-translated t))
  11992. (call-interactively cmd)))
  11993. (defun org-shifttab (&optional arg)
  11994. "Global visibility cycling or move to previous table field.
  11995. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11996. on context.
  11997. See the individual commands for more information."
  11998. (interactive "P")
  11999. (cond
  12000. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12001. ((integerp arg)
  12002. (message "Content view to level: %d" arg)
  12003. (org-content (prefix-numeric-value arg))
  12004. (setq org-cycle-global-status 'overview))
  12005. (t (call-interactively 'org-global-cycle))))
  12006. (defun org-shiftmetaleft ()
  12007. "Promote subtree or delete table column.
  12008. Calls `org-promote-subtree', `org-outdent-item',
  12009. or `org-table-delete-column', depending on context.
  12010. See the individual commands for more information."
  12011. (interactive)
  12012. (cond
  12013. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12014. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12015. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12016. (t (org-modifier-cursor-error))))
  12017. (defun org-shiftmetaright ()
  12018. "Demote subtree or insert table column.
  12019. Calls `org-demote-subtree', `org-indent-item',
  12020. or `org-table-insert-column', depending on context.
  12021. See the individual commands for more information."
  12022. (interactive)
  12023. (cond
  12024. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12025. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12026. ((org-at-item-p) (call-interactively 'org-indent-item))
  12027. (t (org-modifier-cursor-error))))
  12028. (defun org-shiftmetaup (&optional arg)
  12029. "Move subtree up or kill table row.
  12030. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12031. `org-move-item-up' depending on context. See the individual commands
  12032. for more information."
  12033. (interactive "P")
  12034. (cond
  12035. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12036. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12037. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12038. (t (org-modifier-cursor-error))))
  12039. (defun org-shiftmetadown (&optional arg)
  12040. "Move subtree down or insert table row.
  12041. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12042. `org-move-item-down', depending on context. See the individual
  12043. commands for more information."
  12044. (interactive "P")
  12045. (cond
  12046. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12047. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12048. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12049. (t (org-modifier-cursor-error))))
  12050. (defun org-metaleft (&optional arg)
  12051. "Promote heading or move table column to left.
  12052. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12053. With no specific context, calls the Emacs default `backward-word'.
  12054. See the individual commands for more information."
  12055. (interactive "P")
  12056. (cond
  12057. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12058. ((or (org-on-heading-p) (org-region-active-p))
  12059. (call-interactively 'org-do-promote))
  12060. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12061. (t (call-interactively 'backward-word))))
  12062. (defun org-metaright (&optional arg)
  12063. "Demote subtree or move table column to right.
  12064. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12065. With no specific context, calls the Emacs default `forward-word'.
  12066. See the individual commands for more information."
  12067. (interactive "P")
  12068. (cond
  12069. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12070. ((or (org-on-heading-p) (org-region-active-p))
  12071. (call-interactively 'org-do-demote))
  12072. ((org-at-item-p) (call-interactively 'org-indent-item))
  12073. (t (call-interactively 'forward-word))))
  12074. (defun org-metaup (&optional arg)
  12075. "Move subtree up or move table row up.
  12076. Calls `org-move-subtree-up' or `org-table-move-row' or
  12077. `org-move-item-up', depending on context. See the individual commands
  12078. for more information."
  12079. (interactive "P")
  12080. (cond
  12081. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12082. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12083. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12084. (t (transpose-lines 1) (beginning-of-line -1))))
  12085. (defun org-metadown (&optional arg)
  12086. "Move subtree down or move table row down.
  12087. Calls `org-move-subtree-down' or `org-table-move-row' or
  12088. `org-move-item-down', depending on context. See the individual
  12089. commands for more information."
  12090. (interactive "P")
  12091. (cond
  12092. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12093. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12094. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12095. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12096. (defun org-shiftup (&optional arg)
  12097. "Increase item in timestamp or increase priority of current headline.
  12098. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12099. depending on context. See the individual commands for more information."
  12100. (interactive "P")
  12101. (cond
  12102. ((and org-support-shift-select (org-region-active-p))
  12103. (org-call-for-shift-select 'previous-line))
  12104. ((org-at-timestamp-p t)
  12105. (call-interactively (if org-edit-timestamp-down-means-later
  12106. 'org-timestamp-down 'org-timestamp-up)))
  12107. ((and (not (eq org-support-shift-select 'always))
  12108. (org-on-heading-p))
  12109. (call-interactively 'org-priority-up))
  12110. ((and (not org-support-shift-select) (org-at-item-p))
  12111. (call-interactively 'org-previous-item))
  12112. ((org-clocktable-try-shift 'up arg))
  12113. (org-support-shift-select
  12114. (org-call-for-shift-select 'previous-line))
  12115. (t (org-shiftselect-error))))
  12116. (defun org-shiftdown (&optional arg)
  12117. "Decrease item in timestamp or decrease priority of current headline.
  12118. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12119. depending on context. See the individual commands for more information."
  12120. (interactive "P")
  12121. (cond
  12122. ((and org-support-shift-select (org-region-active-p))
  12123. (org-call-for-shift-select 'next-line))
  12124. ((org-at-timestamp-p t)
  12125. (call-interactively (if org-edit-timestamp-down-means-later
  12126. 'org-timestamp-up 'org-timestamp-down)))
  12127. ((and (not (eq org-support-shift-select 'always))
  12128. (org-on-heading-p))
  12129. (call-interactively 'org-priority-down))
  12130. ((and (not org-support-shift-select) (org-at-item-p))
  12131. (call-interactively 'org-next-item))
  12132. ((org-clocktable-try-shift 'down arg))
  12133. (org-support-shift-select
  12134. (org-call-for-shift-select 'next-line))
  12135. (t (org-shiftselect-error))))
  12136. (defun org-shiftright (&optional arg)
  12137. "Cycle the thing at point or in the current line, depending on context.
  12138. Depending on context, this does one of the following:
  12139. - switch a timestamp at point one day into the future
  12140. - on a headline, switch to the next TODO keyword.
  12141. - on an item, switch entire list to the next bullet type
  12142. - on a property line, switch to the next allowed value
  12143. - on a clocktable definition line, move time block into the future"
  12144. (interactive "P")
  12145. (cond
  12146. ((and org-support-shift-select (org-region-active-p))
  12147. (org-call-for-shift-select 'forward-char))
  12148. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12149. ((and (not (eq org-support-shift-select 'always))
  12150. (org-on-heading-p))
  12151. (org-call-with-arg 'org-todo 'right))
  12152. ((or (and org-support-shift-select
  12153. (not (eq org-support-shift-select 'always))
  12154. (org-at-item-bullet-p))
  12155. (and (not org-support-shift-select) (org-at-item-p)))
  12156. (org-call-with-arg 'org-cycle-list-bullet nil))
  12157. ((and (not (eq org-support-shift-select 'always))
  12158. (org-at-property-p))
  12159. (call-interactively 'org-property-next-allowed-value))
  12160. ((org-clocktable-try-shift 'right arg))
  12161. (org-support-shift-select
  12162. (org-call-for-shift-select 'forward-char))
  12163. (t (org-shiftselect-error))))
  12164. (defun org-shiftleft (&optional arg)
  12165. "Cycle the thing at point or in the current line, depending on context.
  12166. Depending on context, this does one of the following:
  12167. - switch a timestamp at point one day into the past
  12168. - on a headline, switch to the previous TODO keyword.
  12169. - on an item, switch entire list to the previous bullet type
  12170. - on a property line, switch to the previous allowed value
  12171. - on a clocktable definition line, move time block into the past"
  12172. (interactive "P")
  12173. (cond
  12174. ((and org-support-shift-select (org-region-active-p))
  12175. (org-call-for-shift-select 'backward-char))
  12176. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12177. ((and (not (eq org-support-shift-select 'always))
  12178. (org-on-heading-p))
  12179. (org-call-with-arg 'org-todo 'left))
  12180. ((or (and org-support-shift-select
  12181. (not (eq org-support-shift-select 'always))
  12182. (org-at-item-bullet-p))
  12183. (and (not org-support-shift-select) (org-at-item-p)))
  12184. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12185. ((and (not (eq org-support-shift-select 'always))
  12186. (org-at-property-p))
  12187. (call-interactively 'org-property-previous-allowed-value))
  12188. ((org-clocktable-try-shift 'left arg))
  12189. (org-support-shift-select
  12190. (org-call-for-shift-select 'backward-char))
  12191. (t (org-shiftselect-error))))
  12192. (defun org-shiftcontrolright ()
  12193. "Switch to next TODO set."
  12194. (interactive)
  12195. (cond
  12196. ((and org-support-shift-select (org-region-active-p))
  12197. (org-call-for-shift-select 'forward-word))
  12198. ((and (not (eq org-support-shift-select 'always))
  12199. (org-on-heading-p))
  12200. (org-call-with-arg 'org-todo 'nextset))
  12201. (org-support-shift-select
  12202. (org-call-for-shift-select 'forward-word))
  12203. (t (org-shiftselect-error))))
  12204. (defun org-shiftcontrolleft ()
  12205. "Switch to previous TODO set."
  12206. (interactive)
  12207. (cond
  12208. ((and org-support-shift-select (org-region-active-p))
  12209. (org-call-for-shift-select 'backward-word))
  12210. ((and (not (eq org-support-shift-select 'always))
  12211. (org-on-heading-p))
  12212. (org-call-with-arg 'org-todo 'previousset))
  12213. (org-support-shift-select
  12214. (org-call-for-shift-select 'backward-word))
  12215. (t (org-shiftselect-error))))
  12216. (defun org-ctrl-c-ret ()
  12217. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12218. (interactive)
  12219. (cond
  12220. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12221. (t (call-interactively 'org-insert-heading))))
  12222. (defun org-copy-special ()
  12223. "Copy region in table or copy current subtree.
  12224. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12225. See the individual commands for more information."
  12226. (interactive)
  12227. (call-interactively
  12228. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12229. (defun org-cut-special ()
  12230. "Cut region in table or cut current subtree.
  12231. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12232. See the individual commands for more information."
  12233. (interactive)
  12234. (call-interactively
  12235. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12236. (defun org-paste-special (arg)
  12237. "Paste rectangular region into table, or past subtree relative to level.
  12238. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12239. See the individual commands for more information."
  12240. (interactive "P")
  12241. (if (org-at-table-p)
  12242. (org-table-paste-rectangle)
  12243. (org-paste-subtree arg)))
  12244. (defun org-edit-special ()
  12245. "Call a special editor for the stuff at point.
  12246. When at a table, call the formula editor with `org-table-edit-formulas'.
  12247. When at the first line of an src example, call `org-edit-src-code'.
  12248. When in an #+include line, visit the include file. Otherwise call
  12249. `ffap' to visit the file at point."
  12250. (interactive)
  12251. (cond
  12252. ((org-at-table-p)
  12253. (call-interactively 'org-table-edit-formulas))
  12254. ((save-excursion
  12255. (beginning-of-line 1)
  12256. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12257. (find-file (org-trim (match-string 1))))
  12258. ((org-edit-src-code))
  12259. ((org-edit-fixed-width-region))
  12260. (t (call-interactively 'ffap))))
  12261. (defun org-ctrl-c-ctrl-c (&optional arg)
  12262. "Set tags in headline, or update according to changed information at point.
  12263. This command does many different things, depending on context:
  12264. - If the cursor is in a headline, prompt for tags and insert them
  12265. into the current line, aligned to `org-tags-column'. When called
  12266. with prefix arg, realign all tags in the current buffer.
  12267. - If the cursor is in one of the special #+KEYWORD lines, this
  12268. triggers scanning the buffer for these lines and updating the
  12269. information.
  12270. - If the cursor is inside a table, realign the table. This command
  12271. works even if the automatic table editor has been turned off.
  12272. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12273. the entire table.
  12274. - If the cursor is at a footnote reference or definition, jump to
  12275. the corresponding definition or references, respectively.
  12276. - If the cursor is a the beginning of a dynamic block, update it.
  12277. - If the cursor is inside a table created by the table.el package,
  12278. activate that table.
  12279. - If the current buffer is a remember buffer, close note and file
  12280. it. A prefix argument of 1 files to the default location
  12281. without further interaction. A prefix argument of 2 files to
  12282. the currently clocking task.
  12283. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12284. links in this buffer.
  12285. - If the cursor is on a numbered item in a plain list, renumber the
  12286. ordered list.
  12287. - If the cursor is on a checkbox, toggle it."
  12288. (interactive "P")
  12289. (let ((org-enable-table-editor t))
  12290. (cond
  12291. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12292. org-occur-highlights
  12293. org-latex-fragment-image-overlays)
  12294. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12295. (org-remove-occur-highlights)
  12296. (org-remove-latex-fragment-image-overlays)
  12297. (message "Temporary highlights/overlays removed from current buffer"))
  12298. ((and (local-variable-p 'org-finish-function (current-buffer))
  12299. (fboundp org-finish-function))
  12300. (funcall org-finish-function))
  12301. ((org-at-property-p)
  12302. (call-interactively 'org-property-action))
  12303. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12304. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12305. ((org-at-table.el-p)
  12306. (require 'table)
  12307. (beginning-of-line 1)
  12308. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12309. (call-interactively 'table-recognize-table))
  12310. ((org-at-table-p)
  12311. (org-table-maybe-eval-formula)
  12312. (if arg
  12313. (call-interactively 'org-table-recalculate)
  12314. (org-table-maybe-recalculate-line))
  12315. (call-interactively 'org-table-align))
  12316. ((or (org-footnote-at-reference-p)
  12317. (org-footnote-at-definition-p))
  12318. (call-interactively 'org-footnote-action))
  12319. ((org-at-item-checkbox-p)
  12320. (call-interactively 'org-toggle-checkbox))
  12321. ((org-at-item-p)
  12322. (call-interactively 'org-maybe-renumber-ordered-list))
  12323. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12324. ;; Dynamic block
  12325. (beginning-of-line 1)
  12326. (save-excursion (org-update-dblock)))
  12327. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12328. (cond
  12329. ((equal (match-string 1) "TBLFM")
  12330. ;; Recalculate the table before this line
  12331. (save-excursion
  12332. (beginning-of-line 1)
  12333. (skip-chars-backward " \r\n\t")
  12334. (if (org-at-table-p)
  12335. (org-call-with-arg 'org-table-recalculate t))))
  12336. (t
  12337. ; (org-set-regexps-and-options)
  12338. ; (org-restart-font-lock)
  12339. (let ((org-inhibit-startup t)) (org-mode-restart))
  12340. (message "Local setup has been refreshed"))))
  12341. (t (error "C-c C-c can do nothing useful at this location.")))))
  12342. (defun org-mode-restart ()
  12343. "Restart Org-mode, to scan again for special lines.
  12344. Also updates the keyword regular expressions."
  12345. (interactive)
  12346. (org-mode)
  12347. (message "Org-mode restarted"))
  12348. (defun org-kill-note-or-show-branches ()
  12349. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12350. (interactive)
  12351. (if (not org-finish-function)
  12352. (call-interactively 'show-branches)
  12353. (let ((org-note-abort t))
  12354. (funcall org-finish-function))))
  12355. (defun org-return (&optional indent)
  12356. "Goto next table row or insert a newline.
  12357. Calls `org-table-next-row' or `newline', depending on context.
  12358. See the individual commands for more information."
  12359. (interactive)
  12360. (cond
  12361. ((bobp) (if indent (newline-and-indent) (newline)))
  12362. ((and (org-at-heading-p)
  12363. (looking-at
  12364. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12365. (org-show-entry)
  12366. (end-of-line 1)
  12367. (newline))
  12368. ((org-at-table-p)
  12369. (org-table-justify-field-maybe)
  12370. (call-interactively 'org-table-next-row))
  12371. (t (if indent (newline-and-indent) (newline)))))
  12372. (defun org-return-indent ()
  12373. "Goto next table row or insert a newline and indent.
  12374. Calls `org-table-next-row' or `newline-and-indent', depending on
  12375. context. See the individual commands for more information."
  12376. (interactive)
  12377. (org-return t))
  12378. (defun org-ctrl-c-star ()
  12379. "Compute table, or change heading status of lines.
  12380. Calls `org-table-recalculate' or `org-toggle-heading',
  12381. depending on context."
  12382. (interactive)
  12383. (cond
  12384. ((org-at-table-p)
  12385. (call-interactively 'org-table-recalculate))
  12386. (t
  12387. ;; Convert all lines in region to list items
  12388. (call-interactively 'org-toggle-heading))))
  12389. (defun org-ctrl-c-minus ()
  12390. "Insert separator line in table or modify bullet status of line.
  12391. Also turns a plain line or a region of lines into list items.
  12392. Calls `org-table-insert-hline', `org-toggle-item', or
  12393. `org-cycle-list-bullet', depending on context."
  12394. (interactive)
  12395. (cond
  12396. ((org-at-table-p)
  12397. (call-interactively 'org-table-insert-hline))
  12398. ((org-region-active-p)
  12399. (call-interactively 'org-toggle-item))
  12400. ((org-in-item-p)
  12401. (call-interactively 'org-cycle-list-bullet))
  12402. (t
  12403. (call-interactively 'org-toggle-item))))
  12404. (defun org-toggle-item ()
  12405. "Convert headings or normal lines to items, items to normal lines.
  12406. If there is no active region, only the current line is considered.
  12407. If the first line in the region is a headline, convert all headlines to items.
  12408. If the first line in the region is an item, convert all items to normal lines.
  12409. If the first line is normal text, add an item bullet to each line."
  12410. (interactive)
  12411. (let (l2 l beg end)
  12412. (if (org-region-active-p)
  12413. (setq beg (region-beginning) end (region-end))
  12414. (setq beg (point-at-bol)
  12415. end (min (1+ (point-at-eol)) (point-max))))
  12416. (save-excursion
  12417. (goto-char end)
  12418. (setq l2 (org-current-line))
  12419. (goto-char beg)
  12420. (beginning-of-line 1)
  12421. (setq l (1- (org-current-line)))
  12422. (if (org-at-item-p)
  12423. ;; We already have items, de-itemize
  12424. (while (< (setq l (1+ l)) l2)
  12425. (when (org-at-item-p)
  12426. (goto-char (match-beginning 2))
  12427. (delete-region (match-beginning 2) (match-end 2))
  12428. (and (looking-at "[ \t]+") (replace-match "")))
  12429. (beginning-of-line 2))
  12430. (if (org-on-heading-p)
  12431. ;; Headings, convert to items
  12432. (while (< (setq l (1+ l)) l2)
  12433. (if (looking-at org-outline-regexp)
  12434. (replace-match "- " t t))
  12435. (beginning-of-line 2))
  12436. ;; normal lines, turn them into items
  12437. (while (< (setq l (1+ l)) l2)
  12438. (unless (org-at-item-p)
  12439. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12440. (replace-match "\\1- \\2")))
  12441. (beginning-of-line 2)))))))
  12442. (defun org-toggle-heading (&optional nstars)
  12443. "Convert headings to normal text, or items or text to headings.
  12444. If there is no active region, only the current line is considered.
  12445. If the first line is a heading, remove the stars from all headlines
  12446. in the region.
  12447. If the first line is a plain list item, turn all plain list items into
  12448. headings.
  12449. If the first line is a normal line, turn each and every line in the region
  12450. into a heading.
  12451. When converting a line into a heading, the number of stars is chosen
  12452. such that the lines become children of the current entry. However, when
  12453. a prefix argument is given, its value determines the number of stars to add."
  12454. (interactive "P")
  12455. (let (l2 l itemp beg end)
  12456. (if (org-region-active-p)
  12457. (setq beg (region-beginning) end (region-end))
  12458. (setq beg (point-at-bol)
  12459. end (min (1+ (point-at-eol)) (point-max))))
  12460. (save-excursion
  12461. (goto-char end)
  12462. (setq l2 (org-current-line))
  12463. (goto-char beg)
  12464. (beginning-of-line 1)
  12465. (setq l (1- (org-current-line)))
  12466. (if (org-on-heading-p)
  12467. ;; We already have headlines, de-star them
  12468. (while (< (setq l (1+ l)) l2)
  12469. (when (org-on-heading-p t)
  12470. (and (looking-at outline-regexp) (replace-match "")))
  12471. (beginning-of-line 2))
  12472. (setq itemp (org-at-item-p))
  12473. (let* ((stars
  12474. (if nstars
  12475. (make-string (prefix-numeric-value current-prefix-arg)
  12476. ?*)
  12477. (save-excursion
  12478. (re-search-backward org-complex-heading-regexp nil t)
  12479. (or (match-string 1) "*"))))
  12480. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12481. (rpl (concat stars add-stars " ")))
  12482. (while (< (setq l (1+ l)) l2)
  12483. (if itemp
  12484. (and (org-at-item-p) (replace-match rpl t t))
  12485. (unless (org-on-heading-p)
  12486. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12487. (replace-match (concat rpl (match-string 2))))))
  12488. (beginning-of-line 2)))))))
  12489. (defun org-meta-return (&optional arg)
  12490. "Insert a new heading or wrap a region in a table.
  12491. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12492. See the individual commands for more information."
  12493. (interactive "P")
  12494. (cond
  12495. ((org-at-table-p)
  12496. (call-interactively 'org-table-wrap-region))
  12497. (t (call-interactively 'org-insert-heading))))
  12498. ;;; Menu entries
  12499. ;; Define the Org-mode menus
  12500. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12501. '("Tbl"
  12502. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12503. ["Next Field" org-cycle (org-at-table-p)]
  12504. ["Previous Field" org-shifttab (org-at-table-p)]
  12505. ["Next Row" org-return (org-at-table-p)]
  12506. "--"
  12507. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12508. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12509. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12510. "--"
  12511. ("Column"
  12512. ["Move Column Left" org-metaleft (org-at-table-p)]
  12513. ["Move Column Right" org-metaright (org-at-table-p)]
  12514. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12515. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12516. ("Row"
  12517. ["Move Row Up" org-metaup (org-at-table-p)]
  12518. ["Move Row Down" org-metadown (org-at-table-p)]
  12519. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12520. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12521. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12522. "--"
  12523. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12524. ("Rectangle"
  12525. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12526. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12527. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12528. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12529. "--"
  12530. ("Calculate"
  12531. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12532. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12533. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12534. "--"
  12535. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12536. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12537. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12538. "--"
  12539. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12540. "--"
  12541. ["Sum Column/Rectangle" org-table-sum
  12542. (or (org-at-table-p) (org-region-active-p))]
  12543. ["Which Column?" org-table-current-column (org-at-table-p)])
  12544. ["Debug Formulas"
  12545. org-table-toggle-formula-debugger
  12546. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12547. ["Show Col/Row Numbers"
  12548. org-table-toggle-coordinate-overlays
  12549. :style toggle
  12550. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12551. "--"
  12552. ["Create" org-table-create (and (not (org-at-table-p))
  12553. org-enable-table-editor)]
  12554. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12555. ["Import from File" org-table-import (not (org-at-table-p))]
  12556. ["Export to File" org-table-export (org-at-table-p)]
  12557. "--"
  12558. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12559. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12560. '("Org"
  12561. ("Show/Hide"
  12562. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12563. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12564. ["Sparse Tree..." org-sparse-tree t]
  12565. ["Reveal Context" org-reveal t]
  12566. ["Show All" show-all t]
  12567. "--"
  12568. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12569. "--"
  12570. ["New Heading" org-insert-heading t]
  12571. ("Navigate Headings"
  12572. ["Up" outline-up-heading t]
  12573. ["Next" outline-next-visible-heading t]
  12574. ["Previous" outline-previous-visible-heading t]
  12575. ["Next Same Level" outline-forward-same-level t]
  12576. ["Previous Same Level" outline-backward-same-level t]
  12577. "--"
  12578. ["Jump" org-goto t])
  12579. ("Edit Structure"
  12580. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12581. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12582. "--"
  12583. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12584. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12585. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12586. "--"
  12587. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12588. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12589. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12590. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12591. "--"
  12592. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12593. "--"
  12594. ["Convert to odd levels" org-convert-to-odd-levels t]
  12595. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12596. ("Editing"
  12597. ["Emphasis..." org-emphasize t]
  12598. ["Edit Source Example" org-edit-special t]
  12599. "--"
  12600. ["Footnote new/jump" org-footnote-action t]
  12601. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12602. ("Archive"
  12603. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12604. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12605. ; :active t :keys "C-u C-c C-x C-a"]
  12606. ["Sparse trees open ARCHIVE trees"
  12607. (setq org-sparse-tree-open-archived-trees
  12608. (not org-sparse-tree-open-archived-trees))
  12609. :style toggle :selected org-sparse-tree-open-archived-trees]
  12610. ["Cycling opens ARCHIVE trees"
  12611. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12612. :style toggle :selected org-cycle-open-archived-trees]
  12613. "--"
  12614. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12615. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12616. ; ["Check and Move Children" (org-archive-subtree '(4))
  12617. ; :active t :keys "C-u C-c C-x C-s"]
  12618. )
  12619. "--"
  12620. ("TODO Lists"
  12621. ["TODO/DONE/-" org-todo t]
  12622. ("Select keyword"
  12623. ["Next keyword" org-shiftright (org-on-heading-p)]
  12624. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12625. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12626. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12627. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12628. ["Show TODO Tree" org-show-todo-tree t]
  12629. ["Global TODO list" org-todo-list t]
  12630. "--"
  12631. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12632. :selected org-enforce-todo-dependencies :style toggle :active t]
  12633. "Settings for tree at point"
  12634. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12635. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12636. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12637. ["Do Children parallel" org-toggle-ordered-property :style radio
  12638. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12639. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12640. "--"
  12641. ["Set Priority" org-priority t]
  12642. ["Priority Up" org-shiftup t]
  12643. ["Priority Down" org-shiftdown t])
  12644. ("TAGS and Properties"
  12645. ["Set Tags" org-set-tags-command t]
  12646. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12647. "--"
  12648. ["Set property" org-set-property t]
  12649. ["Column view of properties" org-columns t]
  12650. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12651. ("Dates and Scheduling"
  12652. ["Timestamp" org-time-stamp t]
  12653. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12654. ("Change Date"
  12655. ["1 Day Later" org-shiftright t]
  12656. ["1 Day Earlier" org-shiftleft t]
  12657. ["1 ... Later" org-shiftup t]
  12658. ["1 ... Earlier" org-shiftdown t])
  12659. ["Compute Time Range" org-evaluate-time-range t]
  12660. ["Schedule Item" org-schedule t]
  12661. ["Deadline" org-deadline t]
  12662. "--"
  12663. ["Custom time format" org-toggle-time-stamp-overlays
  12664. :style radio :selected org-display-custom-times]
  12665. "--"
  12666. ["Goto Calendar" org-goto-calendar t]
  12667. ["Date from Calendar" org-date-from-calendar t]
  12668. "--"
  12669. ["Start/Restart Timer" org-timer-start t]
  12670. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12671. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12672. ["Insert Timer String" org-timer t]
  12673. ["Insert Timer Item" org-timer-item t])
  12674. ("Logging work"
  12675. ["Clock in" org-clock-in t]
  12676. ["Clock out" org-clock-out t]
  12677. ["Clock cancel" org-clock-cancel t]
  12678. ["Goto running clock" org-clock-goto t]
  12679. ["Display times" org-clock-display t]
  12680. ["Create clock table" org-clock-report t]
  12681. "--"
  12682. ["Record DONE time"
  12683. (progn (setq org-log-done (not org-log-done))
  12684. (message "Switching to %s will %s record a timestamp"
  12685. (car org-done-keywords)
  12686. (if org-log-done "automatically" "not")))
  12687. :style toggle :selected org-log-done])
  12688. "--"
  12689. ["Agenda Command..." org-agenda t]
  12690. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12691. ("File List for Agenda")
  12692. ("Special views current file"
  12693. ["TODO Tree" org-show-todo-tree t]
  12694. ["Check Deadlines" org-check-deadlines t]
  12695. ["Timeline" org-timeline t]
  12696. ["Tags Tree" org-tags-sparse-tree t])
  12697. "--"
  12698. ("Hyperlinks"
  12699. ["Store Link (Global)" org-store-link t]
  12700. ["Insert Link" org-insert-link t]
  12701. ["Follow Link" org-open-at-point t]
  12702. "--"
  12703. ["Next link" org-next-link t]
  12704. ["Previous link" org-previous-link t]
  12705. "--"
  12706. ["Descriptive Links"
  12707. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12708. :style radio
  12709. :selected (member '(org-link) buffer-invisibility-spec)]
  12710. ["Literal Links"
  12711. (progn
  12712. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12713. :style radio
  12714. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12715. "--"
  12716. ["Export/Publish..." org-export t]
  12717. ("LaTeX"
  12718. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12719. :selected org-cdlatex-mode]
  12720. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12721. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12722. ["Modify math symbol" org-cdlatex-math-modify
  12723. (org-inside-LaTeX-fragment-p)]
  12724. ["Export LaTeX fragments as images"
  12725. (if (featurep 'org-exp)
  12726. (setq org-export-with-LaTeX-fragments
  12727. (not org-export-with-LaTeX-fragments))
  12728. (require 'org-exp))
  12729. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12730. org-export-with-LaTeX-fragments)])
  12731. "--"
  12732. ("Documentation"
  12733. ["Show Version" org-version t]
  12734. ["Info Documentation" org-info t])
  12735. ("Customize"
  12736. ["Browse Org Group" org-customize t]
  12737. "--"
  12738. ["Expand This Menu" org-create-customize-menu
  12739. (fboundp 'customize-menu-create)])
  12740. "--"
  12741. ["Refresh setup" org-mode-restart t]
  12742. ))
  12743. (defun org-info (&optional node)
  12744. "Read documentation for Org-mode in the info system.
  12745. With optional NODE, go directly to that node."
  12746. (interactive)
  12747. (info (format "(org)%s" (or node ""))))
  12748. (defun org-install-agenda-files-menu ()
  12749. (let ((bl (buffer-list)))
  12750. (save-excursion
  12751. (while bl
  12752. (set-buffer (pop bl))
  12753. (if (org-mode-p) (setq bl nil)))
  12754. (when (org-mode-p)
  12755. (easy-menu-change
  12756. '("Org") "File List for Agenda"
  12757. (append
  12758. (list
  12759. ["Edit File List" (org-edit-agenda-file-list) t]
  12760. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12761. ["Remove Current File from List" org-remove-file t]
  12762. ["Cycle through agenda files" org-cycle-agenda-files t]
  12763. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12764. "--")
  12765. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12766. ;;;; Documentation
  12767. ;;;###autoload
  12768. (defun org-require-autoloaded-modules ()
  12769. (interactive)
  12770. (mapc 'require
  12771. '(org-agenda org-archive org-attach org-clock org-colview
  12772. org-exp org-id org-export-latex org-publish
  12773. org-remember org-table org-timer)))
  12774. ;;;###autoload
  12775. (defun org-customize ()
  12776. "Call the customize function with org as argument."
  12777. (interactive)
  12778. (org-load-modules-maybe)
  12779. (org-require-autoloaded-modules)
  12780. (customize-browse 'org))
  12781. (defun org-create-customize-menu ()
  12782. "Create a full customization menu for Org-mode, insert it into the menu."
  12783. (interactive)
  12784. (org-load-modules-maybe)
  12785. (org-require-autoloaded-modules)
  12786. (if (fboundp 'customize-menu-create)
  12787. (progn
  12788. (easy-menu-change
  12789. '("Org") "Customize"
  12790. `(["Browse Org group" org-customize t]
  12791. "--"
  12792. ,(customize-menu-create 'org)
  12793. ["Set" Custom-set t]
  12794. ["Save" Custom-save t]
  12795. ["Reset to Current" Custom-reset-current t]
  12796. ["Reset to Saved" Custom-reset-saved t]
  12797. ["Reset to Standard Settings" Custom-reset-standard t]))
  12798. (message "\"Org\"-menu now contains full customization menu"))
  12799. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12800. ;;;; Miscellaneous stuff
  12801. ;;; Generally useful functions
  12802. (defun org-find-text-property-in-string (prop s)
  12803. "Return the first non-nil value of property PROP in string S."
  12804. (or (get-text-property 0 prop s)
  12805. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12806. prop s)))
  12807. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12808. "Display the given MESSAGE as a warning."
  12809. (if (fboundp 'display-warning)
  12810. (display-warning 'org message
  12811. (if (featurep 'xemacs)
  12812. 'warning
  12813. :warning))
  12814. (let ((buf (get-buffer-create "*Org warnings*")))
  12815. (with-current-buffer buf
  12816. (goto-char (point-max))
  12817. (insert "Warning (Org): " message)
  12818. (unless (bolp)
  12819. (newline)))
  12820. (display-buffer buf)
  12821. (sit-for 0))))
  12822. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12823. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12824. (if (and marker (marker-buffer marker)
  12825. (buffer-live-p (marker-buffer marker)))
  12826. (progn
  12827. (switch-to-buffer (marker-buffer marker))
  12828. (if (or (> marker (point-max)) (< marker (point-min)))
  12829. (widen))
  12830. (goto-char marker)
  12831. (org-show-context 'org-goto))
  12832. (if bookmark
  12833. (bookmark-jump bookmark)
  12834. (error "Cannot find location"))))
  12835. (defun org-quote-csv-field (s)
  12836. "Quote field for inclusion in CSV material."
  12837. (if (string-match "[\",]" s)
  12838. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12839. s))
  12840. (defun org-plist-delete (plist property)
  12841. "Delete PROPERTY from PLIST.
  12842. This is in contrast to merely setting it to 0."
  12843. (let (p)
  12844. (while plist
  12845. (if (not (eq property (car plist)))
  12846. (setq p (plist-put p (car plist) (nth 1 plist))))
  12847. (setq plist (cddr plist)))
  12848. p))
  12849. (defun org-force-self-insert (N)
  12850. "Needed to enforce self-insert under remapping."
  12851. (interactive "p")
  12852. (self-insert-command N))
  12853. (defun org-string-width (s)
  12854. "Compute width of string, ignoring invisible characters.
  12855. This ignores character with invisibility property `org-link', and also
  12856. characters with property `org-cwidth', because these will become invisible
  12857. upon the next fontification round."
  12858. (let (b l)
  12859. (when (or (eq t buffer-invisibility-spec)
  12860. (assq 'org-link buffer-invisibility-spec))
  12861. (while (setq b (text-property-any 0 (length s)
  12862. 'invisible 'org-link s))
  12863. (setq s (concat (substring s 0 b)
  12864. (substring s (or (next-single-property-change
  12865. b 'invisible s) (length s)))))))
  12866. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12867. (setq s (concat (substring s 0 b)
  12868. (substring s (or (next-single-property-change
  12869. b 'org-cwidth s) (length s))))))
  12870. (setq l (string-width s) b -1)
  12871. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12872. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12873. l))
  12874. (defun org-get-indentation (&optional line)
  12875. "Get the indentation of the current line, interpreting tabs.
  12876. When LINE is given, assume it represents a line and compute its indentation."
  12877. (if line
  12878. (if (string-match "^ *" (org-remove-tabs line))
  12879. (match-end 0))
  12880. (save-excursion
  12881. (beginning-of-line 1)
  12882. (skip-chars-forward " \t")
  12883. (current-column))))
  12884. (defun org-remove-tabs (s &optional width)
  12885. "Replace tabulators in S with spaces.
  12886. Assumes that s is a single line, starting in column 0."
  12887. (setq width (or width tab-width))
  12888. (while (string-match "\t" s)
  12889. (setq s (replace-match
  12890. (make-string
  12891. (- (* width (/ (+ (match-beginning 0) width) width))
  12892. (match-beginning 0)) ?\ )
  12893. t t s)))
  12894. s)
  12895. (defun org-fix-indentation (line ind)
  12896. "Fix indentation in LINE.
  12897. IND is a cons cell with target and minimum indentation.
  12898. If the current indentation in LINE is smaller than the minimum,
  12899. leave it alone. If it is larger than ind, set it to the target."
  12900. (let* ((l (org-remove-tabs line))
  12901. (i (org-get-indentation l))
  12902. (i1 (car ind)) (i2 (cdr ind)))
  12903. (if (>= i i2) (setq l (substring line i2)))
  12904. (if (> i1 0)
  12905. (concat (make-string i1 ?\ ) l)
  12906. l)))
  12907. (defun org-base-buffer (buffer)
  12908. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12909. (if (not buffer)
  12910. buffer
  12911. (or (buffer-base-buffer buffer)
  12912. buffer)))
  12913. (defun org-trim (s)
  12914. "Remove whitespace at beginning and end of string."
  12915. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12916. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12917. s)
  12918. (defun org-wrap (string &optional width lines)
  12919. "Wrap string to either a number of lines, or a width in characters.
  12920. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12921. that costs. If there is a word longer than WIDTH, the text is actually
  12922. wrapped to the length of that word.
  12923. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12924. many lines, whatever width that takes.
  12925. The return value is a list of lines, without newlines at the end."
  12926. (let* ((words (org-split-string string "[ \t\n]+"))
  12927. (maxword (apply 'max (mapcar 'org-string-width words)))
  12928. w ll)
  12929. (cond (width
  12930. (org-do-wrap words (max maxword width)))
  12931. (lines
  12932. (setq w maxword)
  12933. (setq ll (org-do-wrap words maxword))
  12934. (if (<= (length ll) lines)
  12935. ll
  12936. (setq ll words)
  12937. (while (> (length ll) lines)
  12938. (setq w (1+ w))
  12939. (setq ll (org-do-wrap words w)))
  12940. ll))
  12941. (t (error "Cannot wrap this")))))
  12942. (defun org-do-wrap (words width)
  12943. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12944. (let (lines line)
  12945. (while words
  12946. (setq line (pop words))
  12947. (while (and words (< (+ (length line) (length (car words))) width))
  12948. (setq line (concat line " " (pop words))))
  12949. (setq lines (push line lines)))
  12950. (nreverse lines)))
  12951. (defun org-split-string (string &optional separators)
  12952. "Splits STRING into substrings at SEPARATORS.
  12953. No empty strings are returned if there are matches at the beginning
  12954. and end of string."
  12955. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12956. (start 0)
  12957. notfirst
  12958. (list nil))
  12959. (while (and (string-match rexp string
  12960. (if (and notfirst
  12961. (= start (match-beginning 0))
  12962. (< start (length string)))
  12963. (1+ start) start))
  12964. (< (match-beginning 0) (length string)))
  12965. (setq notfirst t)
  12966. (or (eq (match-beginning 0) 0)
  12967. (and (eq (match-beginning 0) (match-end 0))
  12968. (eq (match-beginning 0) start))
  12969. (setq list
  12970. (cons (substring string start (match-beginning 0))
  12971. list)))
  12972. (setq start (match-end 0)))
  12973. (or (eq start (length string))
  12974. (setq list
  12975. (cons (substring string start)
  12976. list)))
  12977. (nreverse list)))
  12978. (defun org-context ()
  12979. "Return a list of contexts of the current cursor position.
  12980. If several contexts apply, all are returned.
  12981. Each context entry is a list with a symbol naming the context, and
  12982. two positions indicating start and end of the context. Possible
  12983. contexts are:
  12984. :headline anywhere in a headline
  12985. :headline-stars on the leading stars in a headline
  12986. :todo-keyword on a TODO keyword (including DONE) in a headline
  12987. :tags on the TAGS in a headline
  12988. :priority on the priority cookie in a headline
  12989. :item on the first line of a plain list item
  12990. :item-bullet on the bullet/number of a plain list item
  12991. :checkbox on the checkbox in a plain list item
  12992. :table in an org-mode table
  12993. :table-special on a special filed in a table
  12994. :table-table in a table.el table
  12995. :link on a hyperlink
  12996. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12997. :target on a <<target>>
  12998. :radio-target on a <<<radio-target>>>
  12999. :latex-fragment on a LaTeX fragment
  13000. :latex-preview on a LaTeX fragment with overlayed preview image
  13001. This function expects the position to be visible because it uses font-lock
  13002. faces as a help to recognize the following contexts: :table-special, :link,
  13003. and :keyword."
  13004. (let* ((f (get-text-property (point) 'face))
  13005. (faces (if (listp f) f (list f)))
  13006. (p (point)) clist o)
  13007. ;; First the large context
  13008. (cond
  13009. ((org-on-heading-p t)
  13010. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13011. (when (progn
  13012. (beginning-of-line 1)
  13013. (looking-at org-todo-line-tags-regexp))
  13014. (push (org-point-in-group p 1 :headline-stars) clist)
  13015. (push (org-point-in-group p 2 :todo-keyword) clist)
  13016. (push (org-point-in-group p 4 :tags) clist))
  13017. (goto-char p)
  13018. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13019. (if (looking-at "\\[#[A-Z0-9]\\]")
  13020. (push (org-point-in-group p 0 :priority) clist)))
  13021. ((org-at-item-p)
  13022. (push (org-point-in-group p 2 :item-bullet) clist)
  13023. (push (list :item (point-at-bol)
  13024. (save-excursion (org-end-of-item) (point)))
  13025. clist)
  13026. (and (org-at-item-checkbox-p)
  13027. (push (org-point-in-group p 0 :checkbox) clist)))
  13028. ((org-at-table-p)
  13029. (push (list :table (org-table-begin) (org-table-end)) clist)
  13030. (if (memq 'org-formula faces)
  13031. (push (list :table-special
  13032. (previous-single-property-change p 'face)
  13033. (next-single-property-change p 'face)) clist)))
  13034. ((org-at-table-p 'any)
  13035. (push (list :table-table) clist)))
  13036. (goto-char p)
  13037. ;; Now the small context
  13038. (cond
  13039. ((org-at-timestamp-p)
  13040. (push (org-point-in-group p 0 :timestamp) clist))
  13041. ((memq 'org-link faces)
  13042. (push (list :link
  13043. (previous-single-property-change p 'face)
  13044. (next-single-property-change p 'face)) clist))
  13045. ((memq 'org-special-keyword faces)
  13046. (push (list :keyword
  13047. (previous-single-property-change p 'face)
  13048. (next-single-property-change p 'face)) clist))
  13049. ((org-on-target-p)
  13050. (push (org-point-in-group p 0 :target) clist)
  13051. (goto-char (1- (match-beginning 0)))
  13052. (if (looking-at org-radio-target-regexp)
  13053. (push (org-point-in-group p 0 :radio-target) clist))
  13054. (goto-char p))
  13055. ((setq o (car (delq nil
  13056. (mapcar
  13057. (lambda (x)
  13058. (if (memq x org-latex-fragment-image-overlays) x))
  13059. (org-overlays-at (point))))))
  13060. (push (list :latex-fragment
  13061. (org-overlay-start o) (org-overlay-end o)) clist)
  13062. (push (list :latex-preview
  13063. (org-overlay-start o) (org-overlay-end o)) clist))
  13064. ((org-inside-LaTeX-fragment-p)
  13065. ;; FIXME: positions wrong.
  13066. (push (list :latex-fragment (point) (point)) clist)))
  13067. (setq clist (nreverse (delq nil clist)))
  13068. clist))
  13069. ;; FIXME: Compare with at-regexp-p Do we need both?
  13070. (defun org-in-regexp (re &optional nlines visually)
  13071. "Check if point is inside a match of regexp.
  13072. Normally only the current line is checked, but you can include NLINES extra
  13073. lines both before and after point into the search.
  13074. If VISUALLY is set, require that the cursor is not after the match but
  13075. really on, so that the block visually is on the match."
  13076. (catch 'exit
  13077. (let ((pos (point))
  13078. (eol (point-at-eol (+ 1 (or nlines 0))))
  13079. (inc (if visually 1 0)))
  13080. (save-excursion
  13081. (beginning-of-line (- 1 (or nlines 0)))
  13082. (while (re-search-forward re eol t)
  13083. (if (and (<= (match-beginning 0) pos)
  13084. (>= (+ inc (match-end 0)) pos))
  13085. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13086. (defun org-at-regexp-p (regexp)
  13087. "Is point inside a match of REGEXP in the current line?"
  13088. (catch 'exit
  13089. (save-excursion
  13090. (let ((pos (point)) (end (point-at-eol)))
  13091. (beginning-of-line 1)
  13092. (while (re-search-forward regexp end t)
  13093. (if (and (<= (match-beginning 0) pos)
  13094. (>= (match-end 0) pos))
  13095. (throw 'exit t)))
  13096. nil))))
  13097. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13098. "Call `multi-occur' with buffers for all agenda files."
  13099. (interactive "sOrg-files matching: \np")
  13100. (let* ((files (org-agenda-files))
  13101. (tnames (mapcar 'file-truename files))
  13102. (extra org-agenda-text-search-extra-files)
  13103. f)
  13104. (when (eq (car extra) 'agenda-archives)
  13105. (setq extra (cdr extra))
  13106. (setq files (org-add-archive-files files)))
  13107. (while (setq f (pop extra))
  13108. (unless (member (file-truename f) tnames)
  13109. (add-to-list 'files f 'append)
  13110. (add-to-list 'tnames (file-truename f) 'append)))
  13111. (multi-occur
  13112. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13113. regexp)))
  13114. (if (boundp 'occur-mode-find-occurrence-hook)
  13115. ;; Emacs 23
  13116. (add-hook 'occur-mode-find-occurrence-hook
  13117. (lambda ()
  13118. (when (org-mode-p)
  13119. (org-reveal))))
  13120. ;; Emacs 22
  13121. (defadvice occur-mode-goto-occurrence
  13122. (after org-occur-reveal activate)
  13123. (and (org-mode-p) (org-reveal)))
  13124. (defadvice occur-mode-goto-occurrence-other-window
  13125. (after org-occur-reveal activate)
  13126. (and (org-mode-p) (org-reveal)))
  13127. (defadvice occur-mode-display-occurrence
  13128. (after org-occur-reveal activate)
  13129. (when (org-mode-p)
  13130. (let ((pos (occur-mode-find-occurrence)))
  13131. (with-current-buffer (marker-buffer pos)
  13132. (save-excursion
  13133. (goto-char pos)
  13134. (org-reveal)))))))
  13135. (defun org-uniquify (list)
  13136. "Remove duplicate elements from LIST."
  13137. (let (res)
  13138. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13139. res))
  13140. (defun org-delete-all (elts list)
  13141. "Remove all elements in ELTS from LIST."
  13142. (while elts
  13143. (setq list (delete (pop elts) list)))
  13144. list)
  13145. (defun org-back-over-empty-lines ()
  13146. "Move backwards over whitespace, to the beginning of the first empty line.
  13147. Returns the number of empty lines passed."
  13148. (let ((pos (point)))
  13149. (skip-chars-backward " \t\n\r")
  13150. (beginning-of-line 2)
  13151. (goto-char (min (point) pos))
  13152. (count-lines (point) pos)))
  13153. (defun org-skip-whitespace ()
  13154. (skip-chars-forward " \t\n\r"))
  13155. (defun org-point-in-group (point group &optional context)
  13156. "Check if POINT is in match-group GROUP.
  13157. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13158. match. If the match group does ot exist or point is not inside it,
  13159. return nil."
  13160. (and (match-beginning group)
  13161. (>= point (match-beginning group))
  13162. (<= point (match-end group))
  13163. (if context
  13164. (list context (match-beginning group) (match-end group))
  13165. t)))
  13166. (defun org-switch-to-buffer-other-window (&rest args)
  13167. "Switch to buffer in a second window on the current frame.
  13168. In particular, do not allow pop-up frames."
  13169. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13170. special-display-function)
  13171. (apply 'switch-to-buffer-other-window args)))
  13172. (defun org-combine-plists (&rest plists)
  13173. "Create a single property list from all plists in PLISTS.
  13174. The process starts by copying the first list, and then setting properties
  13175. from the other lists. Settings in the last list are the most significant
  13176. ones and overrule settings in the other lists."
  13177. (let ((rtn (copy-sequence (pop plists)))
  13178. p v ls)
  13179. (while plists
  13180. (setq ls (pop plists))
  13181. (while ls
  13182. (setq p (pop ls) v (pop ls))
  13183. (setq rtn (plist-put rtn p v))))
  13184. rtn))
  13185. (defun org-move-line-down (arg)
  13186. "Move the current line down. With prefix argument, move it past ARG lines."
  13187. (interactive "p")
  13188. (let ((col (current-column))
  13189. beg end pos)
  13190. (beginning-of-line 1) (setq beg (point))
  13191. (beginning-of-line 2) (setq end (point))
  13192. (beginning-of-line (+ 1 arg))
  13193. (setq pos (move-marker (make-marker) (point)))
  13194. (insert (delete-and-extract-region beg end))
  13195. (goto-char pos)
  13196. (org-move-to-column col)))
  13197. (defun org-move-line-up (arg)
  13198. "Move the current line up. With prefix argument, move it past ARG lines."
  13199. (interactive "p")
  13200. (let ((col (current-column))
  13201. beg end pos)
  13202. (beginning-of-line 1) (setq beg (point))
  13203. (beginning-of-line 2) (setq end (point))
  13204. (beginning-of-line (- arg))
  13205. (setq pos (move-marker (make-marker) (point)))
  13206. (insert (delete-and-extract-region beg end))
  13207. (goto-char pos)
  13208. (org-move-to-column col)))
  13209. (defun org-replace-escapes (string table)
  13210. "Replace %-escapes in STRING with values in TABLE.
  13211. TABLE is an association list with keys like \"%a\" and string values.
  13212. The sequences in STRING may contain normal field width and padding information,
  13213. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13214. so values can contain further %-escapes if they are define later in TABLE."
  13215. (let ((case-fold-search nil)
  13216. e re rpl)
  13217. (while (setq e (pop table))
  13218. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13219. (while (string-match re string)
  13220. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13221. (cdr e)))
  13222. (setq string (replace-match rpl t t string))))
  13223. string))
  13224. (defun org-sublist (list start end)
  13225. "Return a section of LIST, from START to END.
  13226. Counting starts at 1."
  13227. (let (rtn (c start))
  13228. (setq list (nthcdr (1- start) list))
  13229. (while (and list (<= c end))
  13230. (push (pop list) rtn)
  13231. (setq c (1+ c)))
  13232. (nreverse rtn)))
  13233. (defun org-find-base-buffer-visiting (file)
  13234. "Like `find-buffer-visiting' but alway return the base buffer and
  13235. not an indirect buffer."
  13236. (let ((buf (find-buffer-visiting file)))
  13237. (if buf
  13238. (or (buffer-base-buffer buf) buf)
  13239. nil)))
  13240. (defun org-image-file-name-regexp (&optional extensions)
  13241. "Return regexp matching the file names of images.
  13242. If EXTENSIONS is given, only match these."
  13243. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13244. (image-file-name-regexp)
  13245. (let ((image-file-name-extensions
  13246. (or extensions
  13247. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13248. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13249. (concat "\\."
  13250. (regexp-opt (nconc (mapcar 'upcase
  13251. image-file-name-extensions)
  13252. image-file-name-extensions)
  13253. t)
  13254. "\\'"))))
  13255. (defun org-file-image-p (file &optional extensions)
  13256. "Return non-nil if FILE is an image."
  13257. (save-match-data
  13258. (string-match (org-image-file-name-regexp extensions) file)))
  13259. (defun org-get-cursor-date ()
  13260. "Return the date at cursor in as a time.
  13261. This works in the calendar and in the agenda, anywhere else it just
  13262. returns the current time."
  13263. (let (date day defd)
  13264. (cond
  13265. ((eq major-mode 'calendar-mode)
  13266. (setq date (calendar-cursor-to-date)
  13267. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13268. ((eq major-mode 'org-agenda-mode)
  13269. (setq day (get-text-property (point) 'day))
  13270. (if day
  13271. (setq date (calendar-gregorian-from-absolute day)
  13272. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13273. (nth 2 date))))))
  13274. (or defd (current-time))))
  13275. (defvar org-agenda-action-marker (make-marker)
  13276. "Marker pointing to the entry for the next agenda action.")
  13277. (defun org-mark-entry-for-agenda-action ()
  13278. "Mark the current entry as target of an agenda action.
  13279. Agenda actions are actions executed from the agenda with the key `k',
  13280. which make use of the date at the cursor."
  13281. (interactive)
  13282. (move-marker org-agenda-action-marker
  13283. (save-excursion (org-back-to-heading t) (point))
  13284. (current-buffer))
  13285. (message
  13286. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13287. ;;; Paragraph filling stuff.
  13288. ;; We want this to be just right, so use the full arsenal.
  13289. (defun org-indent-line-function ()
  13290. "Indent line like previous, but further if previous was headline or item."
  13291. (interactive)
  13292. (let* ((pos (point))
  13293. (itemp (org-at-item-p))
  13294. column bpos bcol tpos tcol bullet btype bullet-type)
  13295. ;; Find the previous relevant line
  13296. (beginning-of-line 1)
  13297. (cond
  13298. ((looking-at "#") (setq column 0))
  13299. ((looking-at "\\*+ ") (setq column 0))
  13300. (t
  13301. (beginning-of-line 0)
  13302. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13303. (beginning-of-line 0))
  13304. (cond
  13305. ((looking-at "\\*+[ \t]+")
  13306. (if (not org-adapt-indentation)
  13307. (setq column 0)
  13308. (goto-char (match-end 0))
  13309. (setq column (current-column))))
  13310. ((org-in-item-p)
  13311. (org-beginning-of-item)
  13312. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13313. (setq bpos (match-beginning 1) tpos (match-end 0)
  13314. bcol (progn (goto-char bpos) (current-column))
  13315. tcol (progn (goto-char tpos) (current-column))
  13316. bullet (match-string 1)
  13317. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13318. (if (> tcol (+ bcol org-description-max-indent))
  13319. (setq tcol (+ bcol 5)))
  13320. (if (not itemp)
  13321. (setq column tcol)
  13322. (goto-char pos)
  13323. (beginning-of-line 1)
  13324. (if (looking-at "\\S-")
  13325. (progn
  13326. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13327. (setq bullet (match-string 1)
  13328. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13329. (setq column (if (equal btype bullet-type) bcol tcol)))
  13330. (setq column (org-get-indentation)))))
  13331. (t (setq column (org-get-indentation))))))
  13332. (goto-char pos)
  13333. (if (<= (current-column) (current-indentation))
  13334. (org-indent-line-to column)
  13335. (save-excursion (org-indent-line-to column)))
  13336. (setq column (current-column))
  13337. (beginning-of-line 1)
  13338. (if (looking-at
  13339. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13340. (replace-match (concat "\\1" (format org-property-format
  13341. (match-string 2) (match-string 3)))
  13342. t nil))
  13343. (org-move-to-column column)))
  13344. (defun org-set-autofill-regexps ()
  13345. (interactive)
  13346. ;; In the paragraph separator we include headlines, because filling
  13347. ;; text in a line directly attached to a headline would otherwise
  13348. ;; fill the headline as well.
  13349. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13350. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13351. ;; The paragraph starter includes hand-formatted lists.
  13352. (org-set-local 'paragraph-start
  13353. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13354. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13355. ;; But only if the user has not turned off tables or fixed-width regions
  13356. (org-set-local
  13357. 'auto-fill-inhibit-regexp
  13358. (concat "\\*+ \\|#\\+"
  13359. "\\|[ \t]*" org-keyword-time-regexp
  13360. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13361. (concat
  13362. "\\|[ \t]*["
  13363. (if org-enable-table-editor "|" "")
  13364. (if org-enable-fixed-width-editor ":" "")
  13365. "]"))))
  13366. ;; We use our own fill-paragraph function, to make sure that tables
  13367. ;; and fixed-width regions are not wrapped. That function will pass
  13368. ;; through to `fill-paragraph' when appropriate.
  13369. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13370. ; Adaptive filling: To get full control, first make sure that
  13371. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13372. (org-set-local 'adaptive-fill-regexp "\000")
  13373. (org-set-local 'adaptive-fill-function
  13374. 'org-adaptive-fill-function)
  13375. (org-set-local
  13376. 'align-mode-rules-list
  13377. '((org-in-buffer-settings
  13378. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13379. (modes . '(org-mode))))))
  13380. (defun org-fill-paragraph (&optional justify)
  13381. "Re-align a table, pass through to fill-paragraph if no table."
  13382. (let ((table-p (org-at-table-p))
  13383. (table.el-p (org-at-table.el-p)))
  13384. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13385. (save-excursion (goto-char (point-at-bol))
  13386. (looking-at outline-regexp)))
  13387. t) ; skip headlines
  13388. (table.el-p t) ; skip table.el tables
  13389. (table-p (org-table-align) t) ; align org-mode tables
  13390. (t nil)))) ; call paragraph-fill
  13391. ;; For reference, this is the default value of adaptive-fill-regexp
  13392. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13393. (defun org-adaptive-fill-function ()
  13394. "Return a fill prefix for org-mode files.
  13395. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13396. work correctly."
  13397. (cond ((looking-at "#[ \t]+")
  13398. (match-string 0))
  13399. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13400. (save-excursion
  13401. (if (> (match-end 1) (+ (match-beginning 1)
  13402. org-description-max-indent))
  13403. (goto-char (+ (match-beginning 1) 5))
  13404. (goto-char (match-end 0)))
  13405. (make-string (current-column) ?\ )))
  13406. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13407. (save-excursion
  13408. (goto-char (match-end 0))
  13409. (make-string (current-column) ?\ )))
  13410. (t nil)))
  13411. ;;; Other stuff.
  13412. (defun org-toggle-fixed-width-section (arg)
  13413. "Toggle the fixed-width export.
  13414. If there is no active region, the QUOTE keyword at the current headline is
  13415. inserted or removed. When present, it causes the text between this headline
  13416. and the next to be exported as fixed-width text, and unmodified.
  13417. If there is an active region, this command adds or removes a colon as the
  13418. first character of this line. If the first character of a line is a colon,
  13419. this line is also exported in fixed-width font."
  13420. (interactive "P")
  13421. (let* ((cc 0)
  13422. (regionp (org-region-active-p))
  13423. (beg (if regionp (region-beginning) (point)))
  13424. (end (if regionp (region-end)))
  13425. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13426. (case-fold-search nil)
  13427. (re "[ \t]*\\(:\\)")
  13428. off)
  13429. (if regionp
  13430. (save-excursion
  13431. (goto-char beg)
  13432. (setq cc (current-column))
  13433. (beginning-of-line 1)
  13434. (setq off (looking-at re))
  13435. (while (> nlines 0)
  13436. (setq nlines (1- nlines))
  13437. (beginning-of-line 1)
  13438. (cond
  13439. (arg
  13440. (org-move-to-column cc t)
  13441. (insert ":\n")
  13442. (forward-line -1))
  13443. ((and off (looking-at re))
  13444. (replace-match "" t t nil 1))
  13445. ((not off) (org-move-to-column cc t) (insert ":")))
  13446. (forward-line 1)))
  13447. (save-excursion
  13448. (org-back-to-heading)
  13449. (if (looking-at (concat outline-regexp
  13450. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13451. (replace-match "" t t nil 1)
  13452. (if (looking-at outline-regexp)
  13453. (progn
  13454. (goto-char (match-end 0))
  13455. (insert org-quote-string " "))))))))
  13456. ;;;; Functions extending outline functionality
  13457. (defun org-beginning-of-line (&optional arg)
  13458. "Go to the beginning of the current line. If that is invisible, continue
  13459. to a visible line beginning. This makes the function of C-a more intuitive.
  13460. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13461. first attempt, and only move to after the tags when the cursor is already
  13462. beyond the end of the headline."
  13463. (interactive "P")
  13464. (let ((pos (point)) refpos)
  13465. (beginning-of-line 1)
  13466. (if (bobp)
  13467. nil
  13468. (backward-char 1)
  13469. (if (org-invisible-p)
  13470. (while (and (not (bobp)) (org-invisible-p))
  13471. (backward-char 1)
  13472. (beginning-of-line 1))
  13473. (forward-char 1)))
  13474. (when org-special-ctrl-a/e
  13475. (cond
  13476. ((and (looking-at org-complex-heading-regexp)
  13477. (= (char-after (match-end 1)) ?\ ))
  13478. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13479. (point-at-eol)))
  13480. (goto-char
  13481. (if (eq org-special-ctrl-a/e t)
  13482. (cond ((> pos refpos) refpos)
  13483. ((= pos (point)) refpos)
  13484. (t (point)))
  13485. (cond ((> pos (point)) (point))
  13486. ((not (eq last-command this-command)) (point))
  13487. (t refpos)))))
  13488. ((org-at-item-p)
  13489. (goto-char
  13490. (if (eq org-special-ctrl-a/e t)
  13491. (cond ((> pos (match-end 4)) (match-end 4))
  13492. ((= pos (point)) (match-end 4))
  13493. (t (point)))
  13494. (cond ((> pos (point)) (point))
  13495. ((not (eq last-command this-command)) (point))
  13496. (t (match-end 4))))))))
  13497. (org-no-warnings
  13498. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13499. (defun org-end-of-line (&optional arg)
  13500. "Go to the end of the line.
  13501. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13502. first attempt, and only move to after the tags when the cursor is already
  13503. beyond the end of the headline."
  13504. (interactive "P")
  13505. (if (or (not org-special-ctrl-a/e)
  13506. (not (org-on-heading-p)))
  13507. (end-of-line arg)
  13508. (let ((pos (point)))
  13509. (beginning-of-line 1)
  13510. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13511. (if (eq org-special-ctrl-a/e t)
  13512. (if (or (< pos (match-beginning 1))
  13513. (= pos (match-end 0)))
  13514. (goto-char (match-beginning 1))
  13515. (goto-char (match-end 0)))
  13516. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13517. (goto-char (match-end 0))
  13518. (goto-char (match-beginning 1))))
  13519. (end-of-line arg))))
  13520. (org-no-warnings
  13521. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13522. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13523. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13524. (defun org-kill-line (&optional arg)
  13525. "Kill line, to tags or end of line."
  13526. (interactive "P")
  13527. (cond
  13528. ((or (not org-special-ctrl-k)
  13529. (bolp)
  13530. (not (org-on-heading-p)))
  13531. (call-interactively 'kill-line))
  13532. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13533. (kill-region (point) (match-beginning 1))
  13534. (org-set-tags nil t))
  13535. (t (kill-region (point) (point-at-eol)))))
  13536. (define-key org-mode-map "\C-k" 'org-kill-line)
  13537. (defun org-yank (&optional arg)
  13538. "Yank. If the kill is a subtree, treat it specially.
  13539. This command will look at the current kill and check if is a single
  13540. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13541. cursor is at the beginning of a line or after the stars of a currently
  13542. empty headline, then the yank is handled specially. How exactly depends
  13543. on the value of the following variables, both set by default.
  13544. org-yank-folded-subtrees
  13545. When set, the subtree(s) will be folded after insertion, but only
  13546. if doing so would now swallow text after the yanked text.
  13547. org-yank-adjusted-subtrees
  13548. When set, the subtree will be promoted or demoted in order to
  13549. fit into the local outline tree structure, which means that the level
  13550. will be adjusted so that it becomes the smaller one of the two
  13551. *visible* surrounding headings.
  13552. Any prefix to this command will cause `yank' to be called directly with
  13553. no special treatment. In particular, a simple `C-u' prefix will just
  13554. plainly yank the text as it is.
  13555. \[1] Basically, the test checks if the first non-white line is a heading
  13556. and if there are no other headings with fewer stars."
  13557. (interactive "P")
  13558. (setq this-command 'yank)
  13559. (if arg
  13560. (call-interactively 'yank)
  13561. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13562. (and (org-kill-is-subtree-p)
  13563. (or (bolp)
  13564. (and (looking-at "[ \t]*$")
  13565. (string-match
  13566. "\\`\\*+\\'"
  13567. (buffer-substring (point-at-bol) (point)))))))
  13568. swallowp)
  13569. (cond
  13570. ((and subtreep org-yank-folded-subtrees)
  13571. (let ((beg (point))
  13572. end)
  13573. (if (and subtreep org-yank-adjusted-subtrees)
  13574. (org-paste-subtree nil nil 'for-yank)
  13575. (call-interactively 'yank))
  13576. (setq end (point))
  13577. (goto-char beg)
  13578. (when (and (bolp) subtreep
  13579. (not (setq swallowp
  13580. (org-yank-folding-would-swallow-text beg end))))
  13581. (or (looking-at outline-regexp)
  13582. (re-search-forward (concat "^" outline-regexp) end t))
  13583. (while (and (< (point) end) (looking-at outline-regexp))
  13584. (hide-subtree)
  13585. (org-cycle-show-empty-lines 'folded)
  13586. (condition-case nil
  13587. (outline-forward-same-level 1)
  13588. (error (goto-char end)))))
  13589. (when swallowp
  13590. (message
  13591. "Yanked text not folded because that would swallow text"))
  13592. (goto-char end)
  13593. (skip-chars-forward " \t\n\r")
  13594. (beginning-of-line 1)
  13595. (push-mark beg 'nomsg)))
  13596. ((and subtreep org-yank-adjusted-subtrees)
  13597. (let ((beg (point-at-bol)))
  13598. (org-paste-subtree nil nil 'for-yank)
  13599. (push-mark beg 'nomsg)))
  13600. (t
  13601. (call-interactively 'yank))))))
  13602. (defun org-yank-folding-would-swallow-text (beg end)
  13603. "Would hide-subtree at BEG swallow any text after END?"
  13604. (let (level)
  13605. (save-excursion
  13606. (goto-char beg)
  13607. (when (or (looking-at outline-regexp)
  13608. (re-search-forward (concat "^" outline-regexp) end t))
  13609. (setq level (org-outline-level)))
  13610. (goto-char end)
  13611. (skip-chars-forward " \t\r\n\v\f")
  13612. (if (or (eobp)
  13613. (and (bolp) (looking-at org-outline-regexp)
  13614. (<= (org-outline-level) level)))
  13615. nil ; Nothing would be swallowed
  13616. t)))) ; something would swallow
  13617. (define-key org-mode-map "\C-y" 'org-yank)
  13618. (defun org-invisible-p ()
  13619. "Check if point is at a character currently not visible."
  13620. ;; Early versions of noutline don't have `outline-invisible-p'.
  13621. (if (fboundp 'outline-invisible-p)
  13622. (outline-invisible-p)
  13623. (get-char-property (point) 'invisible)))
  13624. (defun org-invisible-p2 ()
  13625. "Check if point is at a character currently not visible."
  13626. (save-excursion
  13627. (if (and (eolp) (not (bobp))) (backward-char 1))
  13628. ;; Early versions of noutline don't have `outline-invisible-p'.
  13629. (if (fboundp 'outline-invisible-p)
  13630. (outline-invisible-p)
  13631. (get-char-property (point) 'invisible))))
  13632. (defun org-back-to-heading (&optional invisible-ok)
  13633. "Call `outline-back-to-heading', but provide a better error message."
  13634. (condition-case nil
  13635. (outline-back-to-heading invisible-ok)
  13636. (error (error "Before first headline at position %d in buffer %s"
  13637. (point) (current-buffer)))))
  13638. (defun org-before-first-heading-p ()
  13639. "Before first heading?"
  13640. (save-excursion
  13641. (null (re-search-backward "^\\*+ " nil t))))
  13642. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13643. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13644. (defun org-at-heading-or-item-p ()
  13645. (or (org-on-heading-p) (org-at-item-p)))
  13646. (defun org-on-target-p ()
  13647. (or (org-in-regexp org-radio-target-regexp)
  13648. (org-in-regexp org-target-regexp)))
  13649. (defun org-up-heading-all (arg)
  13650. "Move to the heading line of which the present line is a subheading.
  13651. This function considers both visible and invisible heading lines.
  13652. With argument, move up ARG levels."
  13653. (if (fboundp 'outline-up-heading-all)
  13654. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13655. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13656. (defun org-up-heading-safe ()
  13657. "Move to the heading line of which the present line is a subheading.
  13658. This version will not throw an error. It will return the level of the
  13659. headline found, or nil if no higher level is found."
  13660. (let (start-level re)
  13661. (org-back-to-heading t)
  13662. (setq start-level (funcall outline-level))
  13663. (if (equal start-level 1)
  13664. nil
  13665. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13666. (if (re-search-backward re nil t)
  13667. (funcall outline-level)))))
  13668. (defun org-first-sibling-p ()
  13669. "Is this heading the first child of its parents?"
  13670. (interactive)
  13671. (let ((re (concat "^" outline-regexp))
  13672. level l)
  13673. (unless (org-at-heading-p t)
  13674. (error "Not at a heading"))
  13675. (setq level (funcall outline-level))
  13676. (save-excursion
  13677. (if (not (re-search-backward re nil t))
  13678. t
  13679. (setq l (funcall outline-level))
  13680. (< l level)))))
  13681. (defun org-goto-sibling (&optional previous)
  13682. "Goto the next sibling, even if it is invisible.
  13683. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13684. when a sibling was found. When none is found, return nil and don't
  13685. move point."
  13686. (let ((fun (if previous 're-search-backward 're-search-forward))
  13687. (pos (point))
  13688. (re (concat "^" outline-regexp))
  13689. level l)
  13690. (when (condition-case nil (org-back-to-heading t) (error nil))
  13691. (setq level (funcall outline-level))
  13692. (catch 'exit
  13693. (or previous (forward-char 1))
  13694. (while (funcall fun re nil t)
  13695. (setq l (funcall outline-level))
  13696. (when (< l level) (goto-char pos) (throw 'exit nil))
  13697. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13698. (goto-char pos)
  13699. nil))))
  13700. (defun org-show-siblings ()
  13701. "Show all siblings of the current headline."
  13702. (save-excursion
  13703. (while (org-goto-sibling) (org-flag-heading nil)))
  13704. (save-excursion
  13705. (while (org-goto-sibling 'previous)
  13706. (org-flag-heading nil))))
  13707. (defun org-show-hidden-entry ()
  13708. "Show an entry where even the heading is hidden."
  13709. (save-excursion
  13710. (org-show-entry)))
  13711. (defun org-flag-heading (flag &optional entry)
  13712. "Flag the current heading. FLAG non-nil means make invisible.
  13713. When ENTRY is non-nil, show the entire entry."
  13714. (save-excursion
  13715. (org-back-to-heading t)
  13716. ;; Check if we should show the entire entry
  13717. (if entry
  13718. (progn
  13719. (org-show-entry)
  13720. (save-excursion
  13721. (and (outline-next-heading)
  13722. (org-flag-heading nil))))
  13723. (outline-flag-region (max (point-min) (1- (point)))
  13724. (save-excursion (outline-end-of-heading) (point))
  13725. flag))))
  13726. (defun org-forward-same-level (arg)
  13727. "Move forward to the ARG'th subheading at same level as this one.
  13728. Stop at the first and last subheadings of a superior heading.
  13729. This is like outline-forward-same-level, but invisible headings are ok."
  13730. (interactive "p")
  13731. (org-back-to-heading t)
  13732. (while (> arg 0)
  13733. (let ((point-to-move-to (save-excursion
  13734. (org-get-next-sibling))))
  13735. (if point-to-move-to
  13736. (progn
  13737. (goto-char point-to-move-to)
  13738. (setq arg (1- arg)))
  13739. (progn
  13740. (setq arg 0)
  13741. (error "No following same-level heading"))))))
  13742. (defun org-get-next-sibling ()
  13743. "Move to next heading of the same level, and return point.
  13744. If there is no such heading, return nil.
  13745. This is like outline-next-sibling, but invisible headings are ok."
  13746. (let ((level (funcall outline-level)))
  13747. (outline-next-heading)
  13748. (while (and (not (eobp)) (> (funcall outline-level) level))
  13749. (outline-next-heading))
  13750. (if (or (eobp) (< (funcall outline-level) level))
  13751. nil
  13752. (point))))
  13753. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13754. ;; This is an exact copy of the original function, but it uses
  13755. ;; `org-back-to-heading', to make it work also in invisible
  13756. ;; trees. And is uses an invisible-OK argument.
  13757. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13758. (org-back-to-heading invisible-OK)
  13759. (let ((first t)
  13760. (level (funcall outline-level)))
  13761. (while (and (not (eobp))
  13762. (or first (> (funcall outline-level) level)))
  13763. (setq first nil)
  13764. (outline-next-heading))
  13765. (unless to-heading
  13766. (if (memq (preceding-char) '(?\n ?\^M))
  13767. (progn
  13768. ;; Go to end of line before heading
  13769. (forward-char -1)
  13770. (if (memq (preceding-char) '(?\n ?\^M))
  13771. ;; leave blank line before heading
  13772. (forward-char -1))))))
  13773. (point))
  13774. (defun org-show-subtree ()
  13775. "Show everything after this heading at deeper levels."
  13776. (outline-flag-region
  13777. (point)
  13778. (save-excursion
  13779. (outline-end-of-subtree) (outline-next-heading) (point))
  13780. nil))
  13781. (defun org-show-entry ()
  13782. "Show the body directly following this heading.
  13783. Show the heading too, if it is currently invisible."
  13784. (interactive)
  13785. (save-excursion
  13786. (condition-case nil
  13787. (progn
  13788. (org-back-to-heading t)
  13789. (outline-flag-region
  13790. (max (point-min) (1- (point)))
  13791. (save-excursion
  13792. (re-search-forward
  13793. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13794. (or (match-beginning 1) (point-max)))
  13795. nil))
  13796. (error nil))))
  13797. (defun org-make-options-regexp (kwds)
  13798. "Make a regular expression for keyword lines."
  13799. (concat
  13800. "^"
  13801. "#?[ \t]*\\+\\("
  13802. (mapconcat 'regexp-quote kwds "\\|")
  13803. "\\):[ \t]*"
  13804. "\\(.+\\)"))
  13805. ;; Make isearch reveal the necessary context
  13806. (defun org-isearch-end ()
  13807. "Reveal context after isearch exits."
  13808. (when isearch-success ; only if search was successful
  13809. (if (featurep 'xemacs)
  13810. ;; Under XEmacs, the hook is run in the correct place,
  13811. ;; we directly show the context.
  13812. (org-show-context 'isearch)
  13813. ;; In Emacs the hook runs *before* restoring the overlays.
  13814. ;; So we have to use a one-time post-command-hook to do this.
  13815. ;; (Emacs 22 has a special variable, see function `org-mode')
  13816. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13817. isearch-mode-end-hook-quit)
  13818. ;; Only when the isearch was not quitted.
  13819. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13820. 'append 'local)))))
  13821. (defun org-isearch-post-command ()
  13822. "Remove self from hook, and show context."
  13823. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13824. (org-show-context 'isearch))
  13825. ;;;; Integration with and fixes for other packages
  13826. ;;; Imenu support
  13827. (defvar org-imenu-markers nil
  13828. "All markers currently used by Imenu.")
  13829. (make-variable-buffer-local 'org-imenu-markers)
  13830. (defun org-imenu-new-marker (&optional pos)
  13831. "Return a new marker for use by Imenu, and remember the marker."
  13832. (let ((m (make-marker)))
  13833. (move-marker m (or pos (point)))
  13834. (push m org-imenu-markers)
  13835. m))
  13836. (defun org-imenu-get-tree ()
  13837. "Produce the index for Imenu."
  13838. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13839. (setq org-imenu-markers nil)
  13840. (let* ((n org-imenu-depth)
  13841. (re (concat "^" outline-regexp))
  13842. (subs (make-vector (1+ n) nil))
  13843. (last-level 0)
  13844. m level head)
  13845. (save-excursion
  13846. (save-restriction
  13847. (widen)
  13848. (goto-char (point-max))
  13849. (while (re-search-backward re nil t)
  13850. (setq level (org-reduced-level (funcall outline-level)))
  13851. (when (<= level n)
  13852. (looking-at org-complex-heading-regexp)
  13853. (setq head (org-link-display-format
  13854. (org-match-string-no-properties 4))
  13855. m (org-imenu-new-marker))
  13856. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13857. (if (>= level last-level)
  13858. (push (cons head m) (aref subs level))
  13859. (push (cons head (aref subs (1+ level))) (aref subs level))
  13860. (loop for i from (1+ level) to n do (aset subs i nil)))
  13861. (setq last-level level)))))
  13862. (aref subs 1)))
  13863. (eval-after-load "imenu"
  13864. '(progn
  13865. (add-hook 'imenu-after-jump-hook
  13866. (lambda ()
  13867. (if (eq major-mode 'org-mode)
  13868. (org-show-context 'org-goto))))))
  13869. (defun org-link-display-format (link)
  13870. "Replace a link with either the description, or the link target
  13871. if no description is present"
  13872. (save-match-data
  13873. (if (string-match org-bracket-link-analytic-regexp link)
  13874. (replace-match (or (match-string 5 link)
  13875. (concat (match-string 1 link)
  13876. (match-string 3 link)))
  13877. nil nil link)
  13878. link)))
  13879. ;; Speedbar support
  13880. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13881. "Overlay marking the agenda restriction line in speedbar.")
  13882. (org-overlay-put org-speedbar-restriction-lock-overlay
  13883. 'face 'org-agenda-restriction-lock)
  13884. (org-overlay-put org-speedbar-restriction-lock-overlay
  13885. 'help-echo "Agendas are currently limited to this item.")
  13886. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13887. (defun org-speedbar-set-agenda-restriction ()
  13888. "Restrict future agenda commands to the location at point in speedbar.
  13889. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13890. (interactive)
  13891. (require 'org-agenda)
  13892. (let (p m tp np dir txt)
  13893. (cond
  13894. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13895. 'org-imenu t))
  13896. (setq m (get-text-property p 'org-imenu-marker))
  13897. (save-excursion
  13898. (save-restriction
  13899. (set-buffer (marker-buffer m))
  13900. (goto-char m)
  13901. (org-agenda-set-restriction-lock 'subtree))))
  13902. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13903. 'speedbar-function 'speedbar-find-file))
  13904. (setq tp (previous-single-property-change
  13905. (1+ p) 'speedbar-function)
  13906. np (next-single-property-change
  13907. tp 'speedbar-function)
  13908. dir (speedbar-line-directory)
  13909. txt (buffer-substring-no-properties (or tp (point-min))
  13910. (or np (point-max))))
  13911. (save-excursion
  13912. (save-restriction
  13913. (set-buffer (find-file-noselect
  13914. (let ((default-directory dir))
  13915. (expand-file-name txt))))
  13916. (unless (org-mode-p)
  13917. (error "Cannot restrict to non-Org-mode file"))
  13918. (org-agenda-set-restriction-lock 'file))))
  13919. (t (error "Don't know how to restrict Org-mode's agenda")))
  13920. (org-move-overlay org-speedbar-restriction-lock-overlay
  13921. (point-at-bol) (point-at-eol))
  13922. (setq current-prefix-arg nil)
  13923. (org-agenda-maybe-redo)))
  13924. (eval-after-load "speedbar"
  13925. '(progn
  13926. (speedbar-add-supported-extension ".org")
  13927. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13928. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13929. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13930. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13931. (add-hook 'speedbar-visiting-tag-hook
  13932. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13933. ;;; Fixes and Hacks for problems with other packages
  13934. ;; Make flyspell not check words in links, to not mess up our keymap
  13935. (defun org-mode-flyspell-verify ()
  13936. "Don't let flyspell put overlays at active buttons."
  13937. (not (get-text-property (point) 'keymap)))
  13938. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13939. (eval-after-load "bookmark"
  13940. '(if (boundp 'bookmark-after-jump-hook)
  13941. ;; We can use the hook
  13942. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13943. ;; Hook not available, use advice
  13944. (defadvice bookmark-jump (after org-make-visible activate)
  13945. "Make the position visible."
  13946. (org-bookmark-jump-unhide))))
  13947. ;; Make sure saveplace show the location if it was hidden
  13948. (eval-after-load "saveplace"
  13949. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13950. "Make the position visible."
  13951. (org-bookmark-jump-unhide)))
  13952. (defun org-bookmark-jump-unhide ()
  13953. "Unhide the current position, to show the bookmark location."
  13954. (and (org-mode-p)
  13955. (or (org-invisible-p)
  13956. (save-excursion (goto-char (max (point-min) (1- (point))))
  13957. (org-invisible-p)))
  13958. (org-show-context 'bookmark-jump)))
  13959. ;; Make session.el ignore our circular variable
  13960. (eval-after-load "session"
  13961. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13962. ;;;; Experimental code
  13963. (defun org-closed-in-range ()
  13964. "Sparse tree of items closed in a certain time range.
  13965. Still experimental, may disappear in the future."
  13966. (interactive)
  13967. ;; Get the time interval from the user.
  13968. (let* ((time1 (time-to-seconds
  13969. (org-read-date nil 'to-time nil "Starting date: ")))
  13970. (time2 (time-to-seconds
  13971. (org-read-date nil 'to-time nil "End date:")))
  13972. ;; callback function
  13973. (callback (lambda ()
  13974. (let ((time
  13975. (time-to-seconds
  13976. (apply 'encode-time
  13977. (org-parse-time-string
  13978. (match-string 1))))))
  13979. ;; check if time in interval
  13980. (and (>= time time1) (<= time time2))))))
  13981. ;; make tree, check each match with the callback
  13982. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13983. ;;;; Finish up
  13984. (provide 'org)
  13985. (run-hooks 'org-load-hook)
  13986. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13987. ;;; org.el ends here