org.el 614 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479
  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.26trans
  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.26trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  185. (defcustom org-support-shift-select nil
  186. "Non-nil means, make shift-cursor commands select text when possible.
  187. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  188. selecting a region, or enlarge thusly regions started in this way.
  189. In Org-mode, in special contexts, these same keys are used for other
  190. purposes, important enough to compete with shift selection. Org tries
  191. to balance these needs by supporting `shift-select-mode' outside these
  192. special contexts, under control of this variable.
  193. The default of this variable is nil, to avoid confusing behavior. Shifted
  194. cursor keys will then execute Org commands in the following contexts:
  195. - on a headline, changing TODO state (left/right) and priority (up/down)
  196. - on a time stamp, changing the time
  197. - in a plain list item, changing the bullet type
  198. - in a property definition line, switching between allowed values
  199. - in the BEGIN line of a clock table (changing the time block).
  200. Outside these contexts, the commands will throw an error.
  201. When this variable is t and the cursor is not in a special context,
  202. Org-mode will support shift-selection for making and enlarging regions.
  203. To make this more effective, the bullet cycling will no longer happen
  204. anywhere in an item line, but only if the cursor is exactly on the bullet.
  205. If you set this variable to the symbol `always', then the keys
  206. will not be special in headlines, property lines, and item lines, to make
  207. shift selection work there as well. If this is what you want, you can
  208. use the following alternative commands: `C-c C-t' and `C-c ,' to
  209. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  210. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  211. edited by hand or in column view.
  212. However, when the cursor is on a timestamp, shift-cursor commands
  213. will still edit the time stamp - this is just too good to give up.
  214. XEmacs user should have this variable set to nil, because shift-select-mode
  215. is Emacs 23 only."
  216. :group 'org
  217. :type '(choice
  218. (const :tag "Never" nil)
  219. (const :tag "When outside special context" t)
  220. (const :tag "Everywhere except timestamps" always)))
  221. (defgroup org-startup nil
  222. "Options concerning startup of Org-mode."
  223. :tag "Org Startup"
  224. :group 'org)
  225. (defcustom org-startup-folded t
  226. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  227. This can also be configured on a per-file basis by adding one of
  228. the following lines anywhere in the buffer:
  229. #+STARTUP: fold
  230. #+STARTUP: nofold
  231. #+STARTUP: content"
  232. :group 'org-startup
  233. :type '(choice
  234. (const :tag "nofold: show all" nil)
  235. (const :tag "fold: overview" t)
  236. (const :tag "content: all headlines" content)))
  237. (defcustom org-startup-truncated t
  238. "Non-nil means, entering Org-mode will set `truncate-lines'.
  239. This is useful since some lines containing links can be very long and
  240. uninteresting. Also tables look terrible when wrapped."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-startup-align-all-tables nil
  244. "Non-nil means, align all tables when visiting a file.
  245. This is useful when the column width in tables is forced with <N> cookies
  246. in table fields. Such tables will look correct only after the first re-align.
  247. This can also be configured on a per-file basis by adding one of
  248. the following lines anywhere in the buffer:
  249. #+STARTUP: align
  250. #+STARTUP: noalign"
  251. :group 'org-startup
  252. :type 'boolean)
  253. (defcustom org-insert-mode-line-in-empty-file nil
  254. "Non-nil means insert the first line setting Org-mode in empty files.
  255. When the function `org-mode' is called interactively in an empty file, this
  256. normally means that the file name does not automatically trigger Org-mode.
  257. To ensure that the file will always be in Org-mode in the future, a
  258. line enforcing Org-mode will be inserted into the buffer, if this option
  259. has been set."
  260. :group 'org-startup
  261. :type 'boolean)
  262. (defcustom org-replace-disputed-keys nil
  263. "Non-nil means use alternative key bindings for some keys.
  264. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  265. These keys are also used by other packages like shift-selection-mode'
  266. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  267. If you want to use Org-mode together with one of these other modes,
  268. or more generally if you would like to move some Org-mode commands to
  269. other keys, set this variable and configure the keys with the variable
  270. `org-disputed-keys'.
  271. This option is only relevant at load-time of Org-mode, and must be set
  272. *before* org.el is loaded. Changing it requires a restart of Emacs to
  273. become effective."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-use-extra-keys nil
  277. "Non-nil means use extra key sequence definitions for certain
  278. commands. This happens automatically if you run XEmacs or if
  279. window-system is nil. This variable lets you do the same
  280. manually. You must set it before loading org.
  281. Example: on Carbon Emacs 22 running graphically, with an external
  282. keyboard on a Powerbook, the default way of setting M-left might
  283. not work for either Alt or ESC. Setting this variable will make
  284. it work for ESC."
  285. :group 'org-startup
  286. :type 'boolean)
  287. (if (fboundp 'defvaralias)
  288. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  289. (defcustom org-disputed-keys
  290. '(([(shift up)] . [(meta p)])
  291. ([(shift down)] . [(meta n)])
  292. ([(shift left)] . [(meta -)])
  293. ([(shift right)] . [(meta +)])
  294. ([(control shift right)] . [(meta shift +)])
  295. ([(control shift left)] . [(meta shift -)]))
  296. "Keys for which Org-mode and other modes compete.
  297. This is an alist, cars are the default keys, second element specifies
  298. the alternative to use when `org-replace-disputed-keys' is t.
  299. Keys can be specified in any syntax supported by `define-key'.
  300. The value of this option takes effect only at Org-mode's startup,
  301. therefore you'll have to restart Emacs to apply it after changing."
  302. :group 'org-startup
  303. :type 'alist)
  304. (defun org-key (key)
  305. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  306. Or return the original if not disputed."
  307. (if org-replace-disputed-keys
  308. (let* ((nkey (key-description key))
  309. (x (org-find-if (lambda (x)
  310. (equal (key-description (car x)) nkey))
  311. org-disputed-keys)))
  312. (if x (cdr x) key))
  313. key))
  314. (defun org-find-if (predicate seq)
  315. (catch 'exit
  316. (while seq
  317. (if (funcall predicate (car seq))
  318. (throw 'exit (car seq))
  319. (pop seq)))))
  320. (defun org-defkey (keymap key def)
  321. "Define a key, possibly translated, as returned by `org-key'."
  322. (define-key keymap (org-key key) def))
  323. (defcustom org-ellipsis nil
  324. "The ellipsis to use in the Org-mode outline.
  325. When nil, just use the standard three dots. When a string, use that instead,
  326. When a face, use the standard 3 dots, but with the specified face.
  327. The change affects only Org-mode (which will then use its own display table).
  328. Changing this requires executing `M-x org-mode' in a buffer to become
  329. effective."
  330. :group 'org-startup
  331. :type '(choice (const :tag "Default" nil)
  332. (face :tag "Face" :value org-warning)
  333. (string :tag "String" :value "...#")))
  334. (defvar org-display-table nil
  335. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  336. (defgroup org-keywords nil
  337. "Keywords in Org-mode."
  338. :tag "Org Keywords"
  339. :group 'org)
  340. (defcustom org-deadline-string "DEADLINE:"
  341. "String to mark deadline entries.
  342. A deadline is this string, followed by a time stamp. Should be a word,
  343. terminated by a colon. You can insert a schedule keyword and
  344. a timestamp with \\[org-deadline].
  345. Changes become only effective after restarting Emacs."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defcustom org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].
  353. Changes become only effective after restarting Emacs."
  354. :group 'org-keywords
  355. :type 'string)
  356. (defcustom org-closed-string "CLOSED:"
  357. "String used as the prefix for timestamps logging closing a TODO entry."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-clock-string "CLOCK:"
  361. "String used as prefix for timestamps clocking work hours on an item."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-comment-string "COMMENT"
  365. "Entries starting with this keyword will never be exported.
  366. An entry can be toggled between COMMENT and normal with
  367. \\[org-toggle-comment].
  368. Changes become only effective after restarting Emacs."
  369. :group 'org-keywords
  370. :type 'string)
  371. (defcustom org-quote-string "QUOTE"
  372. "Entries starting with this keyword will be exported in fixed-width font.
  373. Quoting applies only to the text in the entry following the headline, and does
  374. not extend beyond the next headline, even if that is lower level.
  375. An entry can be toggled between QUOTE and normal with
  376. \\[org-toggle-fixed-width-section]."
  377. :group 'org-keywords
  378. :type 'string)
  379. (defconst org-repeat-re
  380. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  381. "Regular expression for specifying repeated events.
  382. After a match, group 1 contains the repeat expression.")
  383. (defgroup org-structure nil
  384. "Options concerning the general structure of Org-mode files."
  385. :tag "Org Structure"
  386. :group 'org)
  387. (defgroup org-reveal-location nil
  388. "Options about how to make context of a location visible."
  389. :tag "Org Reveal Location"
  390. :group 'org-structure)
  391. (defconst org-context-choice
  392. '(choice
  393. (const :tag "Always" t)
  394. (const :tag "Never" nil)
  395. (repeat :greedy t :tag "Individual contexts"
  396. (cons
  397. (choice :tag "Context"
  398. (const agenda)
  399. (const org-goto)
  400. (const occur-tree)
  401. (const tags-tree)
  402. (const link-search)
  403. (const mark-goto)
  404. (const bookmark-jump)
  405. (const isearch)
  406. (const default))
  407. (boolean))))
  408. "Contexts for the reveal options.")
  409. (defcustom org-show-hierarchy-above '((default . t))
  410. "Non-nil means, show full hierarchy when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the hierarchy of headings
  413. above the exposed location is shown.
  414. Turning this off for example for sparse trees makes them very compact.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. Valid contexts are
  417. agenda when exposing an entry from the agenda
  418. org-goto when using the command `org-goto' on key C-c C-j
  419. occur-tree when using the command `org-occur' on key C-c /
  420. tags-tree when constructing a sparse tree based on tags matches
  421. link-search when exposing search matches associated with a link
  422. mark-goto when exposing the jump goal of a mark
  423. bookmark-jump when exposing a bookmark location
  424. isearch when exiting from an incremental search
  425. default default for all contexts not set explicitly"
  426. :group 'org-reveal-location
  427. :type org-context-choice)
  428. (defcustom org-show-following-heading '((default . nil))
  429. "Non-nil means, show following heading when revealing a location.
  430. Org-mode often shows locations in an org-mode file which might have
  431. been invisible before. When this is set, the heading following the
  432. match is shown.
  433. Turning this off for example for sparse trees makes them very compact,
  434. but makes it harder to edit the location of the match. In such a case,
  435. use the command \\[org-reveal] to show more context.
  436. Instead of t, this can also be an alist specifying this option for different
  437. contexts. See `org-show-hierarchy-above' for valid contexts."
  438. :group 'org-reveal-location
  439. :type org-context-choice)
  440. (defcustom org-show-siblings '((default . nil) (isearch t))
  441. "Non-nil means, show all sibling heading when revealing a location.
  442. Org-mode often shows locations in an org-mode file which might have
  443. been invisible before. When this is set, the sibling of the current entry
  444. heading are all made visible. If `org-show-hierarchy-above' is t,
  445. the same happens on each level of the hierarchy above the current entry.
  446. By default this is on for the isearch context, off for all other contexts.
  447. Turning this off for example for sparse trees makes them very compact,
  448. but makes it harder to edit the location of the match. In such a case,
  449. use the command \\[org-reveal] to show more context.
  450. Instead of t, this can also be an alist specifying this option for different
  451. contexts. See `org-show-hierarchy-above' for valid contexts."
  452. :group 'org-reveal-location
  453. :type org-context-choice)
  454. (defcustom org-show-entry-below '((default . nil))
  455. "Non-nil means, show the entry below a headline when revealing a location.
  456. Org-mode often shows locations in an org-mode file which might have
  457. been invisible before. When this is set, the text below the headline that is
  458. exposed is also shown.
  459. By default this is off for all contexts.
  460. Instead of t, this can also be an alist specifying this option for different
  461. contexts. See `org-show-hierarchy-above' for valid contexts."
  462. :group 'org-reveal-location
  463. :type org-context-choice)
  464. (defcustom org-indirect-buffer-display 'other-window
  465. "How should indirect tree buffers be displayed?
  466. This applies to indirect buffers created with the commands
  467. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  468. Valid values are:
  469. current-window Display in the current window
  470. other-window Just display in another window.
  471. dedicated-frame Create one new frame, and re-use it each time.
  472. new-frame Make a new frame each time. Note that in this case
  473. previously-made indirect buffers are kept, and you need to
  474. kill these buffers yourself."
  475. :group 'org-structure
  476. :group 'org-agenda-windows
  477. :type '(choice
  478. (const :tag "In current window" current-window)
  479. (const :tag "In current frame, other window" other-window)
  480. (const :tag "Each time a new frame" new-frame)
  481. (const :tag "One dedicated frame" dedicated-frame)))
  482. (defgroup org-cycle nil
  483. "Options concerning visibility cycling in Org-mode."
  484. :tag "Org Cycle"
  485. :group 'org-structure)
  486. (defcustom org-cycle-max-level nil
  487. "Maximum level which should still be subject to visibility cycling.
  488. Levels higher than this will, for cycling, be treated as text, not a headline.
  489. When `org-odd-levels-only' is set, a value of N in this variable actually
  490. means 2N-1 stars as the limiting headline.
  491. When nil, cycle all levels.
  492. Note that the limiting level of cycling is also influenced by
  493. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  494. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  495. than its value."
  496. :group 'org-cycle
  497. :type '(choice
  498. (const :tag "No limit" nil)
  499. (integer :tag "Maximum level")))
  500. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  501. "Names of drawers. Drawers are not opened by cycling on the headline above.
  502. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  503. this:
  504. :DRAWERNAME:
  505. .....
  506. :END:
  507. The drawer \"PROPERTIES\" is special for capturing properties through
  508. the property API.
  509. Drawers can be defined on the per-file basis with a line like:
  510. #+DRAWERS: HIDDEN STATE PROPERTIES"
  511. :group 'org-structure
  512. :group 'org-cycle
  513. :type '(repeat (string :tag "Drawer Name")))
  514. (defcustom org-cycle-global-at-bob nil
  515. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  516. This makes it possible to do global cycling without having to use S-TAB or
  517. C-u TAB. For this special case to work, the first line of the buffer
  518. must not be a headline - it may be empty or some other text. When used in
  519. this way, `org-cycle-hook' is disables temporarily, to make sure the
  520. cursor stays at the beginning of the buffer.
  521. When this option is nil, don't do anything special at the beginning
  522. of the buffer."
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-emulate-tab t
  526. "Where should `org-cycle' emulate TAB.
  527. nil Never
  528. white Only in completely white lines
  529. whitestart Only at the beginning of lines, before the first non-white char
  530. t Everywhere except in headlines
  531. exc-hl-bol Everywhere except at the start of a headline
  532. If TAB is used in a place where it does not emulate TAB, the current subtree
  533. visibility is cycled."
  534. :group 'org-cycle
  535. :type '(choice (const :tag "Never" nil)
  536. (const :tag "Only in completely white lines" white)
  537. (const :tag "Before first char in a line" whitestart)
  538. (const :tag "Everywhere except in headlines" t)
  539. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  540. ))
  541. (defcustom org-cycle-separator-lines 2
  542. "Number of empty lines needed to keep an empty line between collapsed trees.
  543. If you leave an empty line between the end of a subtree and the following
  544. headline, this empty line is hidden when the subtree is folded.
  545. Org-mode will leave (exactly) one empty line visible if the number of
  546. empty lines is equal or larger to the number given in this variable.
  547. So the default 2 means, at least 2 empty lines after the end of a subtree
  548. are needed to produce free space between a collapsed subtree and the
  549. following headline.
  550. Special case: when 0, never leave empty lines in collapsed view."
  551. :group 'org-cycle
  552. :type 'integer)
  553. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  554. (defcustom org-pre-cycle-hook nil
  555. "Hook that is run before visibility cycling is happening.
  556. The function(s) in this hook must accept a single argument which indicates
  557. the new state that will be set right after running this hook. The
  558. argument is a symbol. Before a global state change, it can have the values
  559. `overview', `content', or `all'. Before a local state change, it can have
  560. the values `folded', `children', or `subtree'."
  561. :group 'org-cycle
  562. :type 'hook)
  563. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  564. org-cycle-hide-drawers
  565. org-cycle-show-empty-lines
  566. org-optimize-window-after-visibility-change)
  567. "Hook that is run after `org-cycle' has changed the buffer visibility.
  568. The function(s) in this hook must accept a single argument which indicates
  569. the new state that was set by the most recent `org-cycle' command. The
  570. argument is a symbol. After a global state change, it can have the values
  571. `overview', `content', or `all'. After a local state change, it can have
  572. the values `folded', `children', or `subtree'."
  573. :group 'org-cycle
  574. :type 'hook)
  575. (defgroup org-edit-structure nil
  576. "Options concerning structure editing in Org-mode."
  577. :tag "Org Edit Structure"
  578. :group 'org-structure)
  579. (defcustom org-odd-levels-only nil
  580. "Non-nil means, skip even levels and only use odd levels for the outline.
  581. This has the effect that two stars are being added/taken away in
  582. promotion/demotion commands. It also influences how levels are
  583. handled by the exporters.
  584. Changing it requires restart of `font-lock-mode' to become effective
  585. for fontification also in regions already fontified.
  586. You may also set this on a per-file basis by adding one of the following
  587. lines to the buffer:
  588. #+STARTUP: odd
  589. #+STARTUP: oddeven"
  590. :group 'org-edit-structure
  591. :group 'org-font-lock
  592. :type 'boolean)
  593. (defcustom org-adapt-indentation t
  594. "Non-nil means, adapt indentation when promoting and demoting.
  595. When this is set and the *entire* text in an entry is indented, the
  596. indentation is increased by one space in a demotion command, and
  597. decreased by one in a promotion command. If any line in the entry
  598. body starts at column 0, indentation is not changed at all."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-special-ctrl-a/e nil
  602. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  603. When t, `C-a' will bring back the cursor to the beginning of the
  604. headline text, i.e. after the stars and after a possible TODO keyword.
  605. In an item, this will be the position after the bullet.
  606. When the cursor is already at that position, another `C-a' will bring
  607. it to the beginning of the line.
  608. `C-e' will jump to the end of the headline, ignoring the presence of tags
  609. in the headline. A second `C-e' will then jump to the true end of the
  610. line, after any tags.
  611. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  612. and only a directly following, identical keypress will bring the cursor
  613. to the special positions.
  614. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  615. set separately."
  616. :group 'org-edit-structure
  617. :type '(choice
  618. (const :tag "off" nil)
  619. (const :tag "after stars/bullet and before tags first" t)
  620. (const :tag "true line boundary first" reversed)
  621. (cons :tag "Set C-a and C-e separately"
  622. (choice :tag "Special C-a"
  623. (const :tag "off" nil)
  624. (const :tag "after stars/bullet first" t)
  625. (const :tag "before stars/bullet first" reversed))
  626. (choice :tag "Special C-e"
  627. (const :tag "off" nil)
  628. (const :tag "before tags first" t)
  629. (const :tag "after tags first" reversed)))))
  630. (if (fboundp 'defvaralias)
  631. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  632. (defcustom org-special-ctrl-k nil
  633. "Non-nil means `C-k' will behave specially in headlines.
  634. When nil, `C-k' will call the default `kill-line' command.
  635. When t, the following will happen while the cursor is in the headline:
  636. - When the cursor is at the beginning of a headline, kill the entire
  637. line and possible the folded subtree below the line.
  638. - When in the middle of the headline text, kill the headline up to the tags.
  639. - When after the headline text, kill the tags."
  640. :group 'org-edit-structure
  641. :type 'boolean)
  642. (defcustom org-yank-folded-subtrees t
  643. "Non-nil means, when yanking subtrees, fold them.
  644. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  645. it starts with a heading and all other headings in it are either children
  646. or siblings, then fold all the subtrees. However, do this only if no
  647. text after the yank would be swallowed into a folded tree by this action."
  648. :group 'org-edit-structure
  649. :type 'boolean)
  650. (defcustom org-yank-adjusted-subtrees nil
  651. "Non-nil means, when yanking subtrees, adjust the level.
  652. With this setting, `org-paste-subtree' is used to insert the subtree, see
  653. this function for details."
  654. :group 'org-edit-structure
  655. :type 'boolean)
  656. (defcustom org-M-RET-may-split-line '((default . t))
  657. "Non-nil means, M-RET will split the line at the cursor position.
  658. When nil, it will go to the end of the line before making a
  659. new line.
  660. You may also set this option in a different way for different
  661. contexts. Valid contexts are:
  662. headline when creating a new headline
  663. item when creating a new item
  664. table in a table field
  665. default the value to be used for all contexts not explicitly
  666. customized"
  667. :group 'org-structure
  668. :group 'org-table
  669. :type '(choice
  670. (const :tag "Always" t)
  671. (const :tag "Never" nil)
  672. (repeat :greedy t :tag "Individual contexts"
  673. (cons
  674. (choice :tag "Context"
  675. (const headline)
  676. (const item)
  677. (const table)
  678. (const default))
  679. (boolean)))))
  680. (defcustom org-insert-heading-respect-content nil
  681. "Non-nil means, insert new headings after the current subtree.
  682. When nil, the new heading is created directly after the current line.
  683. The commands \\[org-insert-heading-respect-content] and
  684. \\[org-insert-todo-heading-respect-content] turn this variable on
  685. for the duration of the command."
  686. :group 'org-structure
  687. :type 'boolean)
  688. (defcustom org-blank-before-new-entry '((heading . auto)
  689. (plain-list-item . auto))
  690. "Should `org-insert-heading' leave a blank line before new heading/item?
  691. The value is an alist, with `heading' and `plain-list-item' as car,
  692. and a boolean flag as cdr. For plain lists, if the variable
  693. `org-empty-line-terminates-plain-lists' is set, the setting here
  694. is ignored and no empty line is inserted, to keep the list in tact."
  695. :group 'org-edit-structure
  696. :type '(list
  697. (cons (const heading)
  698. (choice (const :tag "Never" nil)
  699. (const :tag "Always" t)
  700. (const :tag "Auto" auto)))
  701. (cons (const plain-list-item)
  702. (choice (const :tag "Never" nil)
  703. (const :tag "Always" t)
  704. (const :tag "Auto" auto)))))
  705. (defcustom org-insert-heading-hook nil
  706. "Hook being run after inserting a new heading."
  707. :group 'org-edit-structure
  708. :type 'hook)
  709. (defcustom org-enable-fixed-width-editor t
  710. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  711. This currently only means, they are never auto-wrapped.
  712. When nil, such lines will be treated like ordinary lines.
  713. See also the QUOTE keyword."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defcustom org-edit-src-region-extra nil
  717. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  718. For examples see the function `org-edit-src-find-region-and-lang'.
  719. The regular expression identifying the begin marker should end with a newline,
  720. and the regexp marking the end line should start with a newline, to make sure
  721. there are kept outside the narrowed region."
  722. :group 'org-edit-structure
  723. :type '(repeat
  724. (list
  725. (regexp :tag "begin regexp")
  726. (regexp :tag "end regexp")
  727. (choice :tag "language"
  728. (string :tag "specify")
  729. (integer :tag "from match group")
  730. (const :tag "from `lang' element")
  731. (const :tag "from `style' element")))))
  732. (defcustom org-coderef-label-format "(ref:%s)"
  733. "The default coderef format.
  734. This format string will be used to search for coderef labels in literal
  735. examples (EXAMPLE and SRC blocks). The format can be overwritten
  736. an individual literal example with the -f option, like
  737. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  738. ...
  739. #+END_SRC
  740. If you want to use this for HTML export, make sure that the format does
  741. not introduce special font-locking, and avoid the HTML special
  742. characters `<', `>', and `&'. The reason for this restriction is that
  743. the labels are searched for only after htmlize has done its job."
  744. :group 'org-edit-structure ; FIXME this is not in the right group
  745. :type 'string)
  746. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  747. "The mode that should be used to edit fixed-width regions.
  748. These are the regions where each line starts with a colon."
  749. :group 'org-edit-structure
  750. :type '(choice
  751. (const artist-mode)
  752. (const picture-mode)
  753. (const fundamental-mode)
  754. (function :tag "Other (specify)")))
  755. (defcustom org-edit-src-persistent-message t
  756. "Non-nil means show persistent exit help message while editing src examples.
  757. The message is shown in the header-line, which will be created in the
  758. first line of the window showing the editing buffer.
  759. When nil, the message will only be shown intermittently in the echo area."
  760. :group 'org-edit-structure
  761. :type 'boolean)
  762. (defcustom org-goto-auto-isearch t
  763. "Non-nil means, typing characters in org-goto starts incremental search."
  764. :group 'org-edit-structure
  765. :type 'boolean)
  766. (defgroup org-sparse-trees nil
  767. "Options concerning sparse trees in Org-mode."
  768. :tag "Org Sparse Trees"
  769. :group 'org-structure)
  770. (defcustom org-highlight-sparse-tree-matches t
  771. "Non-nil means, highlight all matches that define a sparse tree.
  772. The highlights will automatically disappear the next time the buffer is
  773. changed by an edit command."
  774. :group 'org-sparse-trees
  775. :type 'boolean)
  776. (defcustom org-remove-highlights-with-change t
  777. "Non-nil means, any change to the buffer will remove temporary highlights.
  778. Such highlights are created by `org-occur' and `org-clock-display'.
  779. When nil, `C-c C-c needs to be used to get rid of the highlights.
  780. The highlights created by `org-preview-latex-fragment' always need
  781. `C-c C-c' to be removed."
  782. :group 'org-sparse-trees
  783. :group 'org-time
  784. :type 'boolean)
  785. (defcustom org-occur-hook '(org-first-headline-recenter)
  786. "Hook that is run after `org-occur' has constructed a sparse tree.
  787. This can be used to recenter the window to show as much of the structure
  788. as possible."
  789. :group 'org-sparse-trees
  790. :type 'hook)
  791. (defgroup org-imenu-and-speedbar nil
  792. "Options concerning imenu and speedbar in Org-mode."
  793. :tag "Org Imenu and Speedbar"
  794. :group 'org-structure)
  795. (defcustom org-imenu-depth 2
  796. "The maximum level for Imenu access to Org-mode headlines.
  797. This also applied for speedbar access."
  798. :group 'org-imenu-and-speedbar
  799. :type 'integer)
  800. (defgroup org-table nil
  801. "Options concerning tables in Org-mode."
  802. :tag "Org Table"
  803. :group 'org)
  804. (defcustom org-enable-table-editor 'optimized
  805. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  806. When nil, such lines will be treated like ordinary lines.
  807. When equal to the symbol `optimized', the table editor will be optimized to
  808. do the following:
  809. - Automatic overwrite mode in front of whitespace in table fields.
  810. This makes the structure of the table stay in tact as long as the edited
  811. field does not exceed the column width.
  812. - Minimize the number of realigns. Normally, the table is aligned each time
  813. TAB or RET are pressed to move to another field. With optimization this
  814. happens only if changes to a field might have changed the column width.
  815. Optimization requires replacing the functions `self-insert-command',
  816. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  817. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  818. very good at guessing when a re-align will be necessary, but you can always
  819. force one with \\[org-ctrl-c-ctrl-c].
  820. If you would like to use the optimized version in Org-mode, but the
  821. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  822. This variable can be used to turn on and off the table editor during a session,
  823. but in order to toggle optimization, a restart is required.
  824. See also the variable `org-table-auto-blank-field'."
  825. :group 'org-table
  826. :type '(choice
  827. (const :tag "off" nil)
  828. (const :tag "on" t)
  829. (const :tag "on, optimized" optimized)))
  830. (defcustom org-self-insert-cluster-for-undo t
  831. "Non-nil means cluster self-insert commands for undo when possible.
  832. If this is set, then, like in the Emacs command loop, 20 consequtive
  833. characters will be undone together.
  834. This is configurable, because there is some impact on typing performance."
  835. :group 'org-table
  836. :type 'boolean)
  837. (defcustom org-table-tab-recognizes-table.el t
  838. "Non-nil means, TAB will automatically notice a table.el table.
  839. When it sees such a table, it moves point into it and - if necessary -
  840. calls `table-recognize-table'."
  841. :group 'org-table-editing
  842. :type 'boolean)
  843. (defgroup org-link nil
  844. "Options concerning links in Org-mode."
  845. :tag "Org Link"
  846. :group 'org)
  847. (defvar org-link-abbrev-alist-local nil
  848. "Buffer-local version of `org-link-abbrev-alist', which see.
  849. The value of this is taken from the #+LINK lines.")
  850. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  851. (defcustom org-link-abbrev-alist nil
  852. "Alist of link abbreviations.
  853. The car of each element is a string, to be replaced at the start of a link.
  854. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  855. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  856. [[linkkey:tag][description]]
  857. The 'linkkey' must be a word word, starting with a letter, followed
  858. by letters, numbers, '-' or '_'.
  859. If REPLACE is a string, the tag will simply be appended to create the link.
  860. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  861. the placeholder \"%h\" will cause a url-encoded version of the tag to
  862. be inserted at that point (see the function `url-hexify-string').
  863. REPLACE may also be a function that will be called with the tag as the
  864. only argument to create the link, which should be returned as a string.
  865. See the manual for examples."
  866. :group 'org-link
  867. :type '(repeat
  868. (cons
  869. (string :tag "Protocol")
  870. (choice
  871. (string :tag "Format")
  872. (function)))))
  873. (defcustom org-descriptive-links t
  874. "Non-nil means, hide link part and only show description of bracket links.
  875. Bracket links are like [[link][description]]. This variable sets the initial
  876. state in new org-mode buffers. The setting can then be toggled on a
  877. per-buffer basis from the Org->Hyperlinks menu."
  878. :group 'org-link
  879. :type 'boolean)
  880. (defcustom org-link-file-path-type 'adaptive
  881. "How the path name in file links should be stored.
  882. Valid values are:
  883. relative Relative to the current directory, i.e. the directory of the file
  884. into which the link is being inserted.
  885. absolute Absolute path, if possible with ~ for home directory.
  886. noabbrev Absolute path, no abbreviation of home directory.
  887. adaptive Use relative path for files in the current directory and sub-
  888. directories of it. For other files, use an absolute path."
  889. :group 'org-link
  890. :type '(choice
  891. (const relative)
  892. (const absolute)
  893. (const noabbrev)
  894. (const adaptive)))
  895. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  896. "Types of links that should be activated in Org-mode files.
  897. This is a list of symbols, each leading to the activation of a certain link
  898. type. In principle, it does not hurt to turn on most link types - there may
  899. be a small gain when turning off unused link types. The types are:
  900. bracket The recommended [[link][description]] or [[link]] links with hiding.
  901. angular Links in angular brackets that may contain whitespace like
  902. <bbdb:Carsten Dominik>.
  903. plain Plain links in normal text, no whitespace, like http://google.com.
  904. radio Text that is matched by a radio target, see manual for details.
  905. tag Tag settings in a headline (link to tag search).
  906. date Time stamps (link to calendar).
  907. footnote Footnote labels.
  908. Changing this variable requires a restart of Emacs to become effective."
  909. :group 'org-link
  910. :type '(set :greedy t
  911. (const :tag "Double bracket links (new style)" bracket)
  912. (const :tag "Angular bracket links (old style)" angular)
  913. (const :tag "Plain text links" plain)
  914. (const :tag "Radio target matches" radio)
  915. (const :tag "Tags" tag)
  916. (const :tag "Timestamps" date)
  917. (const :tag "Footnotes" footnote)))
  918. (defcustom org-make-link-description-function nil
  919. "Function to use to generate link descriptions from links. If
  920. nil the link location will be used. This function must take two
  921. parameters; the first is the link and the second the description
  922. org-insert-link has generated, and should return the description
  923. to use."
  924. :group 'org-link
  925. :type 'function)
  926. (defgroup org-link-store nil
  927. "Options concerning storing links in Org-mode."
  928. :tag "Org Store Link"
  929. :group 'org-link)
  930. (defcustom org-email-link-description-format "Email %c: %.30s"
  931. "Format of the description part of a link to an email or usenet message.
  932. The following %-escapes will be replaced by corresponding information:
  933. %F full \"From\" field
  934. %f name, taken from \"From\" field, address if no name
  935. %T full \"To\" field
  936. %t first name in \"To\" field, address if no name
  937. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  938. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  939. %s subject
  940. %m message-id.
  941. You may use normal field width specification between the % and the letter.
  942. This is for example useful to limit the length of the subject.
  943. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  944. :group 'org-link-store
  945. :type 'string)
  946. (defcustom org-from-is-user-regexp
  947. (let (r1 r2)
  948. (when (and user-mail-address (not (string= user-mail-address "")))
  949. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  950. (when (and user-full-name (not (string= user-full-name "")))
  951. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  952. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  953. "Regexp matched against the \"From:\" header of an email or usenet message.
  954. It should match if the message is from the user him/herself."
  955. :group 'org-link-store
  956. :type 'regexp)
  957. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  958. "Non-nil means, storing a link to an Org file will use entry IDs.
  959. Note that before this variable is even considered, org-id must be loaded,
  960. to please customize `org-modules' and turn it on.
  961. The variable can have the following values:
  962. t Create an ID if needed to make a link to the current entry.
  963. create-if-interactive
  964. If `org-store-link' is called directly (interactively, as a user
  965. command), do create an ID to support the link. But when doing the
  966. job for remember, only use the ID if it already exists. The
  967. purpose of this setting is to avoid proliferation of unwanted
  968. IDs, just because you happen to be in an Org file when you
  969. call `org-remember' that automatically and preemptively
  970. creates a link. If you do want to get an ID link in a remember
  971. template to an entry not having an ID, create it first by
  972. explicitly creating a link to it, using `C-c C-l' first.
  973. use-existing
  974. Use existing ID, do not create one.
  975. nil Never use an ID to make a link, instead link using a text search for
  976. the headline text."
  977. :group 'org-link-store
  978. :type '(choice
  979. (const :tag "Create ID to make link" t)
  980. (const :tag "Create if storing link interactively"
  981. create-if-interactive)
  982. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  983. create-if-interactive-and-no-custom-id)
  984. (const :tag "Only use existing" use-existing)
  985. (const :tag "Do not use ID to create link" nil)))
  986. (defcustom org-context-in-file-links t
  987. "Non-nil means, file links from `org-store-link' contain context.
  988. A search string will be added to the file name with :: as separator and
  989. used to find the context when the link is activated by the command
  990. `org-open-at-point'.
  991. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  992. negates this setting for the duration of the command."
  993. :group 'org-link-store
  994. :type 'boolean)
  995. (defcustom org-keep-stored-link-after-insertion nil
  996. "Non-nil means, keep link in list for entire session.
  997. The command `org-store-link' adds a link pointing to the current
  998. location to an internal list. These links accumulate during a session.
  999. The command `org-insert-link' can be used to insert links into any
  1000. Org-mode file (offering completion for all stored links). When this
  1001. option is nil, every link which has been inserted once using \\[org-insert-link]
  1002. will be removed from the list, to make completing the unused links
  1003. more efficient."
  1004. :group 'org-link-store
  1005. :type 'boolean)
  1006. (defgroup org-link-follow nil
  1007. "Options concerning following links in Org-mode."
  1008. :tag "Org Follow Link"
  1009. :group 'org-link)
  1010. (defcustom org-link-translation-function nil
  1011. "Function to translate links with different syntax to Org syntax.
  1012. This can be used to translate links created for example by the Planner
  1013. or emacs-wiki packages to Org syntax.
  1014. The function must accept two parameters, a TYPE containing the link
  1015. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1016. which is everything after the link protocol. It should return a cons
  1017. with possibly modified values of type and path.
  1018. Org contains a function for this, so if you set this variable to
  1019. `org-translate-link-from-planner', you should be able follow many
  1020. links created by planner."
  1021. :group 'org-link-follow
  1022. :type 'function)
  1023. (defcustom org-follow-link-hook nil
  1024. "Hook that is run after a link has been followed."
  1025. :group 'org-link-follow
  1026. :type 'hook)
  1027. (defcustom org-tab-follows-link nil
  1028. "Non-nil means, on links TAB will follow the link.
  1029. Needs to be set before org.el is loaded.
  1030. This really should not be used, it does not make sense, and the
  1031. implementation is bad."
  1032. :group 'org-link-follow
  1033. :type 'boolean)
  1034. (defcustom org-return-follows-link nil
  1035. "Non-nil means, on links RET will follow the link.
  1036. Needs to be set before org.el is loaded."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-mouse-1-follows-link
  1040. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1041. "Non-nil means, mouse-1 on a link will follow the link.
  1042. A longer mouse click will still set point. Does not work on XEmacs.
  1043. Needs to be set before org.el is loaded."
  1044. :group 'org-link-follow
  1045. :type 'boolean)
  1046. (defcustom org-mark-ring-length 4
  1047. "Number of different positions to be recorded in the ring
  1048. Changing this requires a restart of Emacs to work correctly."
  1049. :group 'org-link-follow
  1050. :type 'integer)
  1051. (defcustom org-link-frame-setup
  1052. '((vm . vm-visit-folder-other-frame)
  1053. (gnus . gnus-other-frame)
  1054. (file . find-file-other-window))
  1055. "Setup the frame configuration for following links.
  1056. When following a link with Emacs, it may often be useful to display
  1057. this link in another window or frame. This variable can be used to
  1058. set this up for the different types of links.
  1059. For VM, use any of
  1060. `vm-visit-folder'
  1061. `vm-visit-folder-other-frame'
  1062. For Gnus, use any of
  1063. `gnus'
  1064. `gnus-other-frame'
  1065. `org-gnus-no-new-news'
  1066. For FILE, use any of
  1067. `find-file'
  1068. `find-file-other-window'
  1069. `find-file-other-frame'
  1070. For the calendar, use the variable `calendar-setup'.
  1071. For BBDB, it is currently only possible to display the matches in
  1072. another window."
  1073. :group 'org-link-follow
  1074. :type '(list
  1075. (cons (const vm)
  1076. (choice
  1077. (const vm-visit-folder)
  1078. (const vm-visit-folder-other-window)
  1079. (const vm-visit-folder-other-frame)))
  1080. (cons (const gnus)
  1081. (choice
  1082. (const gnus)
  1083. (const gnus-other-frame)
  1084. (const org-gnus-no-new-news)))
  1085. (cons (const file)
  1086. (choice
  1087. (const find-file)
  1088. (const find-file-other-window)
  1089. (const find-file-other-frame)))))
  1090. (defcustom org-display-internal-link-with-indirect-buffer nil
  1091. "Non-nil means, use indirect buffer to display infile links.
  1092. Activating internal links (from one location in a file to another location
  1093. in the same file) normally just jumps to the location. When the link is
  1094. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1095. another window. When this option is set, the other window actually displays
  1096. an indirect buffer clone of the current buffer, to avoid any visibility
  1097. changes to the current buffer."
  1098. :group 'org-link-follow
  1099. :type 'boolean)
  1100. (defcustom org-open-non-existing-files nil
  1101. "Non-nil means, `org-open-file' will open non-existing files.
  1102. When nil, an error will be generated."
  1103. :group 'org-link-follow
  1104. :type 'boolean)
  1105. (defcustom org-open-directory-means-index-dot-org nil
  1106. "Non-nil means, a link to a directory really means to index.org.
  1107. When nil, following a directory link will run dired or open a finder/explorer
  1108. window on that directory."
  1109. :group 'org-link-follow
  1110. :type 'boolean)
  1111. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1112. "Function and arguments to call for following mailto links.
  1113. This is a list with the first element being a lisp function, and the
  1114. remaining elements being arguments to the function. In string arguments,
  1115. %a will be replaced by the address, and %s will be replaced by the subject
  1116. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1117. :group 'org-link-follow
  1118. :type '(choice
  1119. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1120. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1121. (const :tag "message-mail" (message-mail "%a" "%s"))
  1122. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1123. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1124. "Non-nil means, ask for confirmation before executing shell links.
  1125. Shell links can be dangerous: just think about a link
  1126. [[shell:rm -rf ~/*][Google Search]]
  1127. This link would show up in your Org-mode document as \"Google Search\",
  1128. but really it would remove your entire home directory.
  1129. Therefore we advise against setting this variable to nil.
  1130. Just change it to `y-or-n-p' if you want to confirm with a
  1131. single keystroke rather than having to type \"yes\"."
  1132. :group 'org-link-follow
  1133. :type '(choice
  1134. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1135. (const :tag "with y-or-n (faster)" y-or-n-p)
  1136. (const :tag "no confirmation (dangerous)" nil)))
  1137. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1138. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1139. Elisp links can be dangerous: just think about a link
  1140. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1141. This link would show up in your Org-mode document as \"Google Search\",
  1142. but really it would remove your entire home directory.
  1143. Therefore we advise against setting this variable to nil.
  1144. Just change it to `y-or-n-p' if you want to confirm with a
  1145. single keystroke rather than having to type \"yes\"."
  1146. :group 'org-link-follow
  1147. :type '(choice
  1148. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1149. (const :tag "with y-or-n (faster)" y-or-n-p)
  1150. (const :tag "no confirmation (dangerous)" nil)))
  1151. (defconst org-file-apps-defaults-gnu
  1152. '((remote . emacs)
  1153. (system . mailcap)
  1154. (t . mailcap))
  1155. "Default file applications on a UNIX or GNU/Linux system.
  1156. See `org-file-apps'.")
  1157. (defconst org-file-apps-defaults-macosx
  1158. '((remote . emacs)
  1159. (t . "open %s")
  1160. (system . "open %s")
  1161. ("ps.gz" . "gv %s")
  1162. ("eps.gz" . "gv %s")
  1163. ("dvi" . "xdvi %s")
  1164. ("fig" . "xfig %s"))
  1165. "Default file applications on a MacOS X system.
  1166. The system \"open\" is known as a default, but we use X11 applications
  1167. for some files for which the OS does not have a good default.
  1168. See `org-file-apps'.")
  1169. (defconst org-file-apps-defaults-windowsnt
  1170. (list
  1171. '(remote . emacs)
  1172. (cons t
  1173. (list (if (featurep 'xemacs)
  1174. 'mswindows-shell-execute
  1175. 'w32-shell-execute)
  1176. "open" 'file))
  1177. (cons 'system
  1178. (list (if (featurep 'xemacs)
  1179. 'mswindows-shell-execute
  1180. 'w32-shell-execute)
  1181. "open" 'file)))
  1182. "Default file applications on a Windows NT system.
  1183. The system \"open\" is used for most files.
  1184. See `org-file-apps'.")
  1185. (defcustom org-file-apps
  1186. '(
  1187. (auto-mode . emacs)
  1188. ("\\.x?html?\\'" . default)
  1189. ("\\.pdf\\'" . default)
  1190. )
  1191. "External applications for opening `file:path' items in a document.
  1192. Org-mode uses system defaults for different file types, but
  1193. you can use this variable to set the application for a given file
  1194. extension. The entries in this list are cons cells where the car identifies
  1195. files and the cdr the corresponding command. Possible values for the
  1196. file identifier are
  1197. \"regex\" Regular expression matched against the file name. For backward
  1198. compatibility, this can also be a string with only alphanumeric
  1199. characters, which is then interpreted as an extension.
  1200. `directory' Matches a directory
  1201. `remote' Matches a remote file, accessible through tramp or efs.
  1202. Remote files most likely should be visited through Emacs
  1203. because external applications cannot handle such paths.
  1204. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1205. so all files Emacs knows how to handle. Using this with
  1206. command `emacs' will open most files in Emacs. Beware that this
  1207. will also open html files inside Emacs, unless you add
  1208. (\"html\" . default) to the list as well.
  1209. t Default for files not matched by any of the other options.
  1210. `system' The system command to open files, like `open' on Windows
  1211. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1212. that will be selected if you call `C-c C-o' with a double
  1213. `C-u C-u' prefix.
  1214. Possible values for the command are:
  1215. `emacs' The file will be visited by the current Emacs process.
  1216. `default' Use the default application for this file type, which is the
  1217. association for t in the list, most likely in the system-specific
  1218. part.
  1219. This can be used to overrule an unwanted setting in the
  1220. system-specific variable.
  1221. `system' Use the system command for opening files, like \"open\".
  1222. This command is specified by the entry whose car is `system'.
  1223. Most likely, the system-specific version of this variable
  1224. does define this command, but you can overrule/replace it
  1225. here.
  1226. string A command to be executed by a shell; %s will be replaced
  1227. by the path to the file.
  1228. sexp A Lisp form which will be evaluated. The file path will
  1229. be available in the Lisp variable `file'.
  1230. For more examples, see the system specific constants
  1231. `org-file-apps-defaults-macosx'
  1232. `org-file-apps-defaults-windowsnt'
  1233. `org-file-apps-defaults-gnu'."
  1234. :group 'org-link-follow
  1235. :type '(repeat
  1236. (cons (choice :value ""
  1237. (string :tag "Extension")
  1238. (const :tag "System command to open files" system)
  1239. (const :tag "Default for unrecognized files" t)
  1240. (const :tag "Remote file" remote)
  1241. (const :tag "Links to a directory" directory)
  1242. (const :tag "Any files that have Emacs modes"
  1243. auto-mode))
  1244. (choice :value ""
  1245. (const :tag "Visit with Emacs" emacs)
  1246. (const :tag "Use default" default)
  1247. (const :tag "Use the system command" system)
  1248. (string :tag "Command")
  1249. (sexp :tag "Lisp form")))))
  1250. (defgroup org-refile nil
  1251. "Options concerning refiling entries in Org-mode."
  1252. :tag "Org Refile"
  1253. :group 'org)
  1254. (defcustom org-directory "~/org"
  1255. "Directory with org files.
  1256. This is just a default location to look for Org files. There is no need
  1257. at all to put your files into this directory. It is only used in the
  1258. following situations:
  1259. 1. When a remember template specifies a target file that is not an
  1260. absolute path. The path will then be interpreted relative to
  1261. `org-directory'
  1262. 2. When a remember note is filed away in an interactive way (when exiting the
  1263. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1264. with `org-directory' as the default path."
  1265. :group 'org-refile
  1266. :group 'org-remember
  1267. :type 'directory)
  1268. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1269. "Default target for storing notes.
  1270. Used by the hooks for remember.el. This can be a string, or nil to mean
  1271. the value of `remember-data-file'.
  1272. You can set this on a per-template basis with the variable
  1273. `org-remember-templates'."
  1274. :group 'org-refile
  1275. :group 'org-remember
  1276. :type '(choice
  1277. (const :tag "Default from remember-data-file" nil)
  1278. file))
  1279. (defcustom org-goto-interface 'outline
  1280. "The default interface to be used for `org-goto'.
  1281. Allowed values are:
  1282. outline The interface shows an outline of the relevant file
  1283. and the correct heading is found by moving through
  1284. the outline or by searching with incremental search.
  1285. outline-path-completion Headlines in the current buffer are offered via
  1286. completion. This is the interface also used by
  1287. the refile command."
  1288. :group 'org-refile
  1289. :type '(choice
  1290. (const :tag "Outline" outline)
  1291. (const :tag "Outline-path-completion" outline-path-completion)))
  1292. (defcustom org-goto-max-level 5
  1293. "Maximum level to be considered when running org-goto with refile interface."
  1294. :group 'org-refile
  1295. :type 'integer)
  1296. (defcustom org-reverse-note-order nil
  1297. "Non-nil means, store new notes at the beginning of a file or entry.
  1298. When nil, new notes will be filed to the end of a file or entry.
  1299. This can also be a list with cons cells of regular expressions that
  1300. are matched against file names, and values."
  1301. :group 'org-remember
  1302. :group 'org-refile
  1303. :type '(choice
  1304. (const :tag "Reverse always" t)
  1305. (const :tag "Reverse never" nil)
  1306. (repeat :tag "By file name regexp"
  1307. (cons regexp boolean))))
  1308. (defcustom org-refile-targets nil
  1309. "Targets for refiling entries with \\[org-refile].
  1310. This is list of cons cells. Each cell contains:
  1311. - a specification of the files to be considered, either a list of files,
  1312. or a symbol whose function or variable value will be used to retrieve
  1313. a file name or a list of file names. If you use `org-agenda-files' for
  1314. that, all agenda files will be scanned for targets. Nil means, consider
  1315. headings in the current buffer.
  1316. - A specification of how to find candidate refile targets. This may be
  1317. any of:
  1318. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1319. This tag has to be present in all target headlines, inheritance will
  1320. not be considered.
  1321. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1322. todo keyword.
  1323. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1324. headlines that are refiling targets.
  1325. - a cons cell (:level . N). Any headline of level N is considered a target.
  1326. Note that, when `org-odd-levels-only' is set, level corresponds to
  1327. order in hierarchy, not to the number of stars.
  1328. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1329. Note that, when `org-odd-levels-only' is set, level corresponds to
  1330. order in hierarchy, not to the number of stars.
  1331. You can set the variable `org-refile-target-verify-function' to a function
  1332. to verify each headline found by the simple critery above.
  1333. When this variable is nil, all top-level headlines in the current buffer
  1334. are used, equivalent to the value `((nil . (:level . 1))'."
  1335. :group 'org-refile
  1336. :type '(repeat
  1337. (cons
  1338. (choice :value org-agenda-files
  1339. (const :tag "All agenda files" org-agenda-files)
  1340. (const :tag "Current buffer" nil)
  1341. (function) (variable) (file))
  1342. (choice :tag "Identify target headline by"
  1343. (cons :tag "Specific tag" (const :value :tag) (string))
  1344. (cons :tag "TODO keyword" (const :value :todo) (string))
  1345. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1346. (cons :tag "Level number" (const :value :level) (integer))
  1347. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1348. (defcustom org-refile-target-verify-function nil
  1349. "Function to verify if the headline at point should be a refile target.
  1350. The function will be called without arguments, with point at the
  1351. beginning of the headline. It should return t and leave point
  1352. where it is if the headline is a valid target for refiling.
  1353. If the target should not be selected, the function must return nil.
  1354. In addition to this, it may move point to a place from where the search
  1355. should be continued. For example, the function may decide that the entire
  1356. subtree of the current entry should be excluded and move point to the end
  1357. of the subtree."
  1358. :group 'org-refile
  1359. :type 'function)
  1360. (defcustom org-refile-use-outline-path nil
  1361. "Non-nil means, provide refile targets as paths.
  1362. So a level 3 headline will be available as level1/level2/level3.
  1363. When the value is `file', also include the file name (without directory)
  1364. into the path. In this case, you can also stop the completion after
  1365. the file name, to get entries inserted as top level in the file.
  1366. When `full-file-path', include the full file path."
  1367. :group 'org-refile
  1368. :type '(choice
  1369. (const :tag "Not" nil)
  1370. (const :tag "Yes" t)
  1371. (const :tag "Start with file name" file)
  1372. (const :tag "Start with full file path" full-file-path)))
  1373. (defcustom org-outline-path-complete-in-steps t
  1374. "Non-nil means, complete the outline path in hierarchical steps.
  1375. When Org-mode uses the refile interface to select an outline path
  1376. \(see variable `org-refile-use-outline-path'), the completion of
  1377. the path can be done is a single go, or if can be done in steps down
  1378. the headline hierarchy. Going in steps is probably the best if you
  1379. do not use a special completion package like `ido' or `icicles'.
  1380. However, when using these packages, going in one step can be very
  1381. fast, while still showing the whole path to the entry."
  1382. :group 'org-refile
  1383. :type 'boolean)
  1384. (defcustom org-refile-allow-creating-parent-nodes nil
  1385. "Non-nil means, allow to create new nodes as refile targets.
  1386. New nodes are then created by adding \"/new node name\" to the completion
  1387. of an existing node. When the value of this variable is `confirm',
  1388. new node creation must be confirmed by the user (recommended)
  1389. When nil, the completion must match an existing entry.
  1390. Note that, if the new heading is not seen by the criteria
  1391. listed in `org-refile-targets', multiple instances of the same
  1392. heading would be created by trying again to file under the new
  1393. heading."
  1394. :group 'org-refile
  1395. :type '(choice
  1396. (const :tag "Never" nil)
  1397. (const :tag "Always" t)
  1398. (const :tag "Prompt for confirmation" confirm)))
  1399. (defgroup org-todo nil
  1400. "Options concerning TODO items in Org-mode."
  1401. :tag "Org TODO"
  1402. :group 'org)
  1403. (defgroup org-progress nil
  1404. "Options concerning Progress logging in Org-mode."
  1405. :tag "Org Progress"
  1406. :group 'org-time)
  1407. (defvar org-todo-interpretation-widgets
  1408. '(
  1409. (:tag "Sequence (cycling hits every state)" sequence)
  1410. (:tag "Type (cycling directly to DONE)" type))
  1411. "The available interpretation symbols for customizing
  1412. `org-todo-keywords'.
  1413. Interested libraries should add to this list.")
  1414. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1415. "List of TODO entry keyword sequences and their interpretation.
  1416. \\<org-mode-map>This is a list of sequences.
  1417. Each sequence starts with a symbol, either `sequence' or `type',
  1418. indicating if the keywords should be interpreted as a sequence of
  1419. action steps, or as different types of TODO items. The first
  1420. keywords are states requiring action - these states will select a headline
  1421. for inclusion into the global TODO list Org-mode produces. If one of
  1422. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1423. signify that no further action is necessary. If \"|\" is not found,
  1424. the last keyword is treated as the only DONE state of the sequence.
  1425. The command \\[org-todo] cycles an entry through these states, and one
  1426. additional state where no keyword is present. For details about this
  1427. cycling, see the manual.
  1428. TODO keywords and interpretation can also be set on a per-file basis with
  1429. the special #+SEQ_TODO and #+TYP_TODO lines.
  1430. Each keyword can optionally specify a character for fast state selection
  1431. \(in combination with the variable `org-use-fast-todo-selection')
  1432. and specifiers for state change logging, using the same syntax
  1433. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1434. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1435. indicates to record a time stamp each time this state is selected.
  1436. Each keyword may also specify if a timestamp or a note should be
  1437. recorded when entering or leaving the state, by adding additional
  1438. characters in the parenthesis after the keyword. This looks like this:
  1439. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1440. record only the time of the state change. With X and Y being either
  1441. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1442. Y when leaving the state if and only if the *target* state does not
  1443. define X. You may omit any of the fast-selection key or X or /Y,
  1444. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1445. For backward compatibility, this variable may also be just a list
  1446. of keywords - in this case the interpretation (sequence or type) will be
  1447. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1448. :group 'org-todo
  1449. :group 'org-keywords
  1450. :type '(choice
  1451. (repeat :tag "Old syntax, just keywords"
  1452. (string :tag "Keyword"))
  1453. (repeat :tag "New syntax"
  1454. (cons
  1455. (choice
  1456. :tag "Interpretation"
  1457. ;;Quick and dirty way to see
  1458. ;;`org-todo-interpretations'. This takes the
  1459. ;;place of item arguments
  1460. :convert-widget
  1461. (lambda (widget)
  1462. (widget-put widget
  1463. :args (mapcar
  1464. #'(lambda (x)
  1465. (widget-convert
  1466. (cons 'const x)))
  1467. org-todo-interpretation-widgets))
  1468. widget))
  1469. (repeat
  1470. (string :tag "Keyword"))))))
  1471. (defvar org-todo-keywords-1 nil
  1472. "All TODO and DONE keywords active in a buffer.")
  1473. (make-variable-buffer-local 'org-todo-keywords-1)
  1474. (defvar org-todo-keywords-for-agenda nil)
  1475. (defvar org-done-keywords-for-agenda nil)
  1476. (defvar org-todo-keyword-alist-for-agenda nil)
  1477. (defvar org-tag-alist-for-agenda nil)
  1478. (defvar org-agenda-contributing-files nil)
  1479. (defvar org-not-done-keywords nil)
  1480. (make-variable-buffer-local 'org-not-done-keywords)
  1481. (defvar org-done-keywords nil)
  1482. (make-variable-buffer-local 'org-done-keywords)
  1483. (defvar org-todo-heads nil)
  1484. (make-variable-buffer-local 'org-todo-heads)
  1485. (defvar org-todo-sets nil)
  1486. (make-variable-buffer-local 'org-todo-sets)
  1487. (defvar org-todo-log-states nil)
  1488. (make-variable-buffer-local 'org-todo-log-states)
  1489. (defvar org-todo-kwd-alist nil)
  1490. (make-variable-buffer-local 'org-todo-kwd-alist)
  1491. (defvar org-todo-key-alist nil)
  1492. (make-variable-buffer-local 'org-todo-key-alist)
  1493. (defvar org-todo-key-trigger nil)
  1494. (make-variable-buffer-local 'org-todo-key-trigger)
  1495. (defcustom org-todo-interpretation 'sequence
  1496. "Controls how TODO keywords are interpreted.
  1497. This variable is in principle obsolete and is only used for
  1498. backward compatibility, if the interpretation of todo keywords is
  1499. not given already in `org-todo-keywords'. See that variable for
  1500. more information."
  1501. :group 'org-todo
  1502. :group 'org-keywords
  1503. :type '(choice (const sequence)
  1504. (const type)))
  1505. (defcustom org-use-fast-todo-selection t
  1506. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1507. This variable describes if and under what circumstances the cycling
  1508. mechanism for TODO keywords will be replaced by a single-key, direct
  1509. selection scheme.
  1510. When nil, fast selection is never used.
  1511. When the symbol `prefix', it will be used when `org-todo' is called with
  1512. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1513. in an agenda buffer.
  1514. When t, fast selection is used by default. In this case, the prefix
  1515. argument forces cycling instead.
  1516. In all cases, the special interface is only used if access keys have actually
  1517. been assigned by the user, i.e. if keywords in the configuration are followed
  1518. by a letter in parenthesis, like TODO(t)."
  1519. :group 'org-todo
  1520. :type '(choice
  1521. (const :tag "Never" nil)
  1522. (const :tag "By default" t)
  1523. (const :tag "Only with C-u C-c C-t" prefix)))
  1524. (defcustom org-provide-todo-statistics t
  1525. "Non-nil means, update todo statistics after insert and toggle.
  1526. When this is set, todo statistics is updated in the parent of the current
  1527. entry each time a todo state is changed."
  1528. :group 'org-todo
  1529. :type 'boolean)
  1530. (defcustom org-after-todo-state-change-hook nil
  1531. "Hook which is run after the state of a TODO item was changed.
  1532. The new state (a string with a TODO keyword, or nil) is available in the
  1533. Lisp variable `state'."
  1534. :group 'org-todo
  1535. :type 'hook)
  1536. (defvar org-blocker-hook nil
  1537. "Hook for functions that are allowed to block a state change.
  1538. Each function gets as its single argument a property list, see
  1539. `org-trigger-hook' for more information about this list.
  1540. If any of the functions in this hook returns nil, the state change
  1541. is blocked.")
  1542. (defvar org-trigger-hook nil
  1543. "Hook for functions that are triggered by a state change.
  1544. Each function gets as its single argument a property list with at least
  1545. the following elements:
  1546. (:type type-of-change :position pos-at-entry-start
  1547. :from old-state :to new-state)
  1548. Depending on the type, more properties may be present.
  1549. This mechanism is currently implemented for:
  1550. TODO state changes
  1551. ------------------
  1552. :type todo-state-change
  1553. :from previous state (keyword as a string), or nil, or a symbol
  1554. 'todo' or 'done', to indicate the general type of state.
  1555. :to new state, like in :from")
  1556. (defcustom org-enforce-todo-dependencies nil
  1557. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1558. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1559. be blocked if any prior sibling is not yet done.
  1560. Finally, if the parent is blocked because of ordered siblings of its own,
  1561. the child will also be blocked.
  1562. This variable needs to be set before org.el is loaded, and you need to
  1563. restart Emacs after a change to make the change effective. The only way
  1564. to change is while Emacs is running is through the customize interface."
  1565. :set (lambda (var val)
  1566. (set var val)
  1567. (if val
  1568. (add-hook 'org-blocker-hook
  1569. 'org-block-todo-from-children-or-siblings-or-parent)
  1570. (remove-hook 'org-blocker-hook
  1571. 'org-block-todo-from-children-or-siblings-or-parent)))
  1572. :group 'org-todo
  1573. :type 'boolean)
  1574. (defcustom org-enforce-todo-checkbox-dependencies nil
  1575. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1576. When this is nil, checkboxes have no influence on switching TODO states.
  1577. When non-nil, you first need to check off all check boxes before the TODO
  1578. entry can be switched to DONE.
  1579. This variable needs to be set before org.el is loaded, and you need to
  1580. restart Emacs after a change to make the change effective. The only way
  1581. to change is while Emacs is running is through the customize interface."
  1582. :set (lambda (var val)
  1583. (set var val)
  1584. (if val
  1585. (add-hook 'org-blocker-hook
  1586. 'org-block-todo-from-checkboxes)
  1587. (remove-hook 'org-blocker-hook
  1588. 'org-block-todo-from-checkboxes)))
  1589. :group 'org-todo
  1590. :type 'boolean)
  1591. (defcustom org-todo-state-tags-triggers nil
  1592. "Tag changes that should be triggered by TODO state changes.
  1593. This is a list. Each entry is
  1594. (state-change (tag . flag) .......)
  1595. State-change can be a string with a state, and empty string to indicate the
  1596. state that has no TODO keyword, or it can be one of the symbols `todo'
  1597. or `done', meaning any not-done or done state, respectively."
  1598. :group 'org-todo
  1599. :group 'org-tags
  1600. :type '(repeat
  1601. (cons (choice :tag "When changing to"
  1602. (const :tag "Not-done state" todo)
  1603. (const :tag "Done state" done)
  1604. (string :tag "State"))
  1605. (repeat
  1606. (cons :tag "Tag action"
  1607. (string :tag "Tag")
  1608. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1609. (defcustom org-log-done nil
  1610. "Information to record when a task moves to the DONE state.
  1611. Possible values are:
  1612. nil Don't add anything, just change the keyword
  1613. time Add a time stamp to the task
  1614. note Prompt a closing note and add it with template `org-log-note-headings'
  1615. This option can also be set with on a per-file-basis with
  1616. #+STARTUP: nologdone
  1617. #+STARTUP: logdone
  1618. #+STARTUP: lognotedone
  1619. You can have local logging settings for a subtree by setting the LOGGING
  1620. property to one or more of these keywords."
  1621. :group 'org-todo
  1622. :group 'org-progress
  1623. :type '(choice
  1624. (const :tag "No logging" nil)
  1625. (const :tag "Record CLOSED timestamp" time)
  1626. (const :tag "Record CLOSED timestamp with closing note." note)))
  1627. ;; Normalize old uses of org-log-done.
  1628. (cond
  1629. ((eq org-log-done t) (setq org-log-done 'time))
  1630. ((and (listp org-log-done) (memq 'done org-log-done))
  1631. (setq org-log-done 'note)))
  1632. (defcustom org-log-note-clock-out nil
  1633. "Non-nil means, record a note when clocking out of an item.
  1634. This can also be configured on a per-file basis by adding one of
  1635. the following lines anywhere in the buffer:
  1636. #+STARTUP: lognoteclock-out
  1637. #+STARTUP: nolognoteclock-out"
  1638. :group 'org-todo
  1639. :group 'org-progress
  1640. :type 'boolean)
  1641. (defcustom org-log-done-with-time t
  1642. "Non-nil means, the CLOSED time stamp will contain date and time.
  1643. When nil, only the date will be recorded."
  1644. :group 'org-progress
  1645. :type 'boolean)
  1646. (defcustom org-log-note-headings
  1647. '((done . "CLOSING NOTE %t")
  1648. (state . "State %-12s from %-12S %t")
  1649. (note . "Note taken on %t")
  1650. (clock-out . ""))
  1651. "Headings for notes added to entries.
  1652. The value is an alist, with the car being a symbol indicating the note
  1653. context, and the cdr is the heading to be used. The heading may also be the
  1654. empty string.
  1655. %t in the heading will be replaced by a time stamp.
  1656. %s will be replaced by the new TODO state, in double quotes.
  1657. %S will be replaced by the old TODO state, in double quotes.
  1658. %u will be replaced by the user name.
  1659. %U will be replaced by the full user name."
  1660. :group 'org-todo
  1661. :group 'org-progress
  1662. :type '(list :greedy t
  1663. (cons (const :tag "Heading when closing an item" done) string)
  1664. (cons (const :tag
  1665. "Heading when changing todo state (todo sequence only)"
  1666. state) string)
  1667. (cons (const :tag "Heading when just taking a note" note) string)
  1668. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1669. (unless (assq 'note org-log-note-headings)
  1670. (push '(note . "%t") org-log-note-headings))
  1671. (defcustom org-log-into-drawer nil
  1672. "Non-nil means, insert state change notes and time stamps into a drawer.
  1673. When nil, state changes notes will be inserted after the headline and
  1674. any scheduling and clock lines, but not inside a drawer.
  1675. The value of this variable should be the name of the drawer to use.
  1676. LOGBOOK is proposed at the default drawer for this purpose, you can
  1677. also set this to a string to define the drawer of your choice.
  1678. A value of t is also allowed, representing \"LOGBOOK\".
  1679. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1680. will be ignored."
  1681. :group 'org-todo
  1682. :group 'org-progress
  1683. :type '(choice
  1684. (const :tag "Not into a drawer" nil)
  1685. (const :tag "LOGBOOK" t)
  1686. (string :tag "Other")))
  1687. (if (fboundp 'defvaralias)
  1688. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1689. (defcustom org-log-state-notes-insert-after-drawers nil
  1690. "Non-nil means, insert state change notes after any drawers in entry.
  1691. Only the drawers that *immediately* follow the headline and the
  1692. deadline/scheduled line are skipped.
  1693. When nil, insert notes right after the heading and perhaps the line
  1694. with deadline/scheduling if present.
  1695. This variable will have no effect if `org-log-into-drawer' is
  1696. set."
  1697. :group 'org-todo
  1698. :group 'org-progress
  1699. :type 'boolean)
  1700. (defcustom org-log-states-order-reversed t
  1701. "Non-nil means, the latest state change note will be directly after heading.
  1702. When nil, the notes will be orderer according to time."
  1703. :group 'org-todo
  1704. :group 'org-progress
  1705. :type 'boolean)
  1706. (defcustom org-log-repeat 'time
  1707. "Non-nil means, record moving through the DONE state when triggering repeat.
  1708. An auto-repeating tasks is immediately switched back to TODO when marked
  1709. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1710. the TODO keyword definition, or recording a closing note by setting
  1711. `org-log-done', there will be no record of the task moving through DONE.
  1712. This variable forces taking a note anyway. Possible values are:
  1713. nil Don't force a record
  1714. time Record a time stamp
  1715. note Record a note
  1716. This option can also be set with on a per-file-basis with
  1717. #+STARTUP: logrepeat
  1718. #+STARTUP: lognoterepeat
  1719. #+STARTUP: nologrepeat
  1720. You can have local logging settings for a subtree by setting the LOGGING
  1721. property to one or more of these keywords."
  1722. :group 'org-todo
  1723. :group 'org-progress
  1724. :type '(choice
  1725. (const :tag "Don't force a record" nil)
  1726. (const :tag "Force recording the DONE state" time)
  1727. (const :tag "Force recording a note with the DONE state" note)))
  1728. (defgroup org-priorities nil
  1729. "Priorities in Org-mode."
  1730. :tag "Org Priorities"
  1731. :group 'org-todo)
  1732. (defcustom org-highest-priority ?A
  1733. "The highest priority of TODO items. A character like ?A, ?B etc.
  1734. Must have a smaller ASCII number than `org-lowest-priority'."
  1735. :group 'org-priorities
  1736. :type 'character)
  1737. (defcustom org-lowest-priority ?C
  1738. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1739. Must have a larger ASCII number than `org-highest-priority'."
  1740. :group 'org-priorities
  1741. :type 'character)
  1742. (defcustom org-default-priority ?B
  1743. "The default priority of TODO items.
  1744. This is the priority an item get if no explicit priority is given."
  1745. :group 'org-priorities
  1746. :type 'character)
  1747. (defcustom org-priority-start-cycle-with-default t
  1748. "Non-nil means, start with default priority when starting to cycle.
  1749. When this is nil, the first step in the cycle will be (depending on the
  1750. command used) one higher or lower that the default priority."
  1751. :group 'org-priorities
  1752. :type 'boolean)
  1753. (defgroup org-time nil
  1754. "Options concerning time stamps and deadlines in Org-mode."
  1755. :tag "Org Time"
  1756. :group 'org)
  1757. (defcustom org-insert-labeled-timestamps-at-point nil
  1758. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1759. When nil, these labeled time stamps are forces into the second line of an
  1760. entry, just after the headline. When scheduling from the global TODO list,
  1761. the time stamp will always be forced into the second line."
  1762. :group 'org-time
  1763. :type 'boolean)
  1764. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1765. "Formats for `format-time-string' which are used for time stamps.
  1766. It is not recommended to change this constant.")
  1767. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1768. "Number of minutes to round time stamps to.
  1769. These are two values, the first applies when first creating a time stamp.
  1770. The second applies when changing it with the commands `S-up' and `S-down'.
  1771. When changing the time stamp, this means that it will change in steps
  1772. of N minutes, as given by the second value.
  1773. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1774. numbers should be factors of 60, so for example 5, 10, 15.
  1775. When this is larger than 1, you can still force an exact time-stamp by using
  1776. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1777. and by using a prefix arg to `S-up/down' to specify the exact number
  1778. of minutes to shift."
  1779. :group 'org-time
  1780. :get '(lambda (var) ; Make sure all entries have 5 elements
  1781. (if (integerp (default-value var))
  1782. (list (default-value var) 5)
  1783. (default-value var)))
  1784. :type '(list
  1785. (integer :tag "when inserting times")
  1786. (integer :tag "when modifying times")))
  1787. ;; Normalize old customizations of this variable.
  1788. (when (integerp org-time-stamp-rounding-minutes)
  1789. (setq org-time-stamp-rounding-minutes
  1790. (list org-time-stamp-rounding-minutes
  1791. org-time-stamp-rounding-minutes)))
  1792. (defcustom org-display-custom-times nil
  1793. "Non-nil means, overlay custom formats over all time stamps.
  1794. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1795. To turn this on on a per-file basis, insert anywhere in the file:
  1796. #+STARTUP: customtime"
  1797. :group 'org-time
  1798. :set 'set-default
  1799. :type 'sexp)
  1800. (make-variable-buffer-local 'org-display-custom-times)
  1801. (defcustom org-time-stamp-custom-formats
  1802. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1803. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1804. These are overlayed over the default ISO format if the variable
  1805. `org-display-custom-times' is set. Time like %H:%M should be at the
  1806. end of the second format. The custom formats are also honored by export
  1807. commands, if custom time display is turned on at the time of export."
  1808. :group 'org-time
  1809. :type 'sexp)
  1810. (defun org-time-stamp-format (&optional long inactive)
  1811. "Get the right format for a time string."
  1812. (let ((f (if long (cdr org-time-stamp-formats)
  1813. (car org-time-stamp-formats))))
  1814. (if inactive
  1815. (concat "[" (substring f 1 -1) "]")
  1816. f)))
  1817. (defcustom org-time-clocksum-format "%d:%02d"
  1818. "The format string used when creating CLOCKSUM lines, or when
  1819. org-mode generates a time duration."
  1820. :group 'org-time
  1821. :type 'string)
  1822. (defcustom org-deadline-warning-days 14
  1823. "No. of days before expiration during which a deadline becomes active.
  1824. This variable governs the display in sparse trees and in the agenda.
  1825. When 0 or negative, it means use this number (the absolute value of it)
  1826. even if a deadline has a different individual lead time specified.
  1827. Custom commands can set this variable in the options section."
  1828. :group 'org-time
  1829. :group 'org-agenda-daily/weekly
  1830. :type 'integer)
  1831. (defcustom org-read-date-prefer-future t
  1832. "Non-nil means, assume future for incomplete date input from user.
  1833. This affects the following situations:
  1834. 1. The user gives a day, but no month.
  1835. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1836. read this as the third of *next* month. However, if you enter \"17\",
  1837. it will be considered as *this* month.
  1838. 2. The user gives a month but not a year.
  1839. For example, if it is april and you enter \"feb 2\", this will be read
  1840. as feb 2, *next* year. \"May 5\", however, will be this year.
  1841. Currently this does not work for ISO week specifications.
  1842. When this option is nil, the current month and year will always be used
  1843. as defaults."
  1844. :group 'org-time
  1845. :type 'boolean)
  1846. (defcustom org-read-date-display-live t
  1847. "Non-nil means, display current interpretation of date prompt live.
  1848. This display will be in an overlay, in the minibuffer."
  1849. :group 'org-time
  1850. :type 'boolean)
  1851. (defcustom org-read-date-popup-calendar t
  1852. "Non-nil means, pop up a calendar when prompting for a date.
  1853. In the calendar, the date can be selected with mouse-1. However, the
  1854. minibuffer will also be active, and you can simply enter the date as well.
  1855. When nil, only the minibuffer will be available."
  1856. :group 'org-time
  1857. :type 'boolean)
  1858. (if (fboundp 'defvaralias)
  1859. (defvaralias 'org-popup-calendar-for-date-prompt
  1860. 'org-read-date-popup-calendar))
  1861. (defcustom org-read-date-minibuffer-setup-hook nil
  1862. "Hook to be used to set up keys for the date/time interface.
  1863. Add key definitions to `minibuffer-local-map', which will be a temporary
  1864. copy."
  1865. :group 'org-time
  1866. :type 'hook)
  1867. (defcustom org-extend-today-until 0
  1868. "The hour when your day really ends. Must be an integer.
  1869. This has influence for the following applications:
  1870. - When switching the agenda to \"today\". It it is still earlier than
  1871. the time given here, the day recognized as TODAY is actually yesterday.
  1872. - When a date is read from the user and it is still before the time given
  1873. here, the current date and time will be assumed to be yesterday, 23:59.
  1874. Also, timestamps inserted in remember templates follow this rule.
  1875. IMPORTANT: This is a feature whose implementation is and likely will
  1876. remain incomplete. Really, it is only here because past midnight seems to
  1877. be the favorite working time of John Wiegley :-)"
  1878. :group 'org-time
  1879. :type 'integer)
  1880. (defcustom org-edit-timestamp-down-means-later nil
  1881. "Non-nil means, S-down will increase the time in a time stamp.
  1882. When nil, S-up will increase."
  1883. :group 'org-time
  1884. :type 'boolean)
  1885. (defcustom org-calendar-follow-timestamp-change t
  1886. "Non-nil means, make the calendar window follow timestamp changes.
  1887. When a timestamp is modified and the calendar window is visible, it will be
  1888. moved to the new date."
  1889. :group 'org-time
  1890. :type 'boolean)
  1891. (defgroup org-tags nil
  1892. "Options concerning tags in Org-mode."
  1893. :tag "Org Tags"
  1894. :group 'org)
  1895. (defcustom org-tag-alist nil
  1896. "List of tags allowed in Org-mode files.
  1897. When this list is nil, Org-mode will base TAG input on what is already in the
  1898. buffer.
  1899. The value of this variable is an alist, the car of each entry must be a
  1900. keyword as a string, the cdr may be a character that is used to select
  1901. that tag through the fast-tag-selection interface.
  1902. See the manual for details."
  1903. :group 'org-tags
  1904. :type '(repeat
  1905. (choice
  1906. (cons (string :tag "Tag name")
  1907. (character :tag "Access char"))
  1908. (const :tag "Start radio group" (:startgroup))
  1909. (const :tag "End radio group" (:endgroup))
  1910. (const :tag "New line" (:newline)))))
  1911. (defcustom org-tag-persistent-alist nil
  1912. "List of tags that will always appear in all Org-mode files.
  1913. This is in addition to any in buffer settings or customizations
  1914. of `org-tag-alist'.
  1915. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1916. The value of this variable is an alist, the car of each entry must be a
  1917. keyword as a string, the cdr may be a character that is used to select
  1918. that tag through the fast-tag-selection interface.
  1919. See the manual for details.
  1920. To disable these tags on a per-file basis, insert anywhere in the file:
  1921. #+STARTUP: noptag"
  1922. :group 'org-tags
  1923. :type '(repeat
  1924. (choice
  1925. (cons (string :tag "Tag name")
  1926. (character :tag "Access char"))
  1927. (const :tag "Start radio group" (:startgroup))
  1928. (const :tag "End radio group" (:endgroup))
  1929. (const :tag "New line" (:newline)))))
  1930. (defvar org-file-tags nil
  1931. "List of tags that can be inherited by all entries in the file.
  1932. The tags will be inherited if the variable `org-use-tag-inheritance'
  1933. says they should be.
  1934. This variable is populated from #+TAG lines.")
  1935. (defcustom org-use-fast-tag-selection 'auto
  1936. "Non-nil means, use fast tag selection scheme.
  1937. This is a special interface to select and deselect tags with single keys.
  1938. When nil, fast selection is never used.
  1939. When the symbol `auto', fast selection is used if and only if selection
  1940. characters for tags have been configured, either through the variable
  1941. `org-tag-alist' or through a #+TAGS line in the buffer.
  1942. When t, fast selection is always used and selection keys are assigned
  1943. automatically if necessary."
  1944. :group 'org-tags
  1945. :type '(choice
  1946. (const :tag "Always" t)
  1947. (const :tag "Never" nil)
  1948. (const :tag "When selection characters are configured" 'auto)))
  1949. (defcustom org-fast-tag-selection-single-key nil
  1950. "Non-nil means, fast tag selection exits after first change.
  1951. When nil, you have to press RET to exit it.
  1952. During fast tag selection, you can toggle this flag with `C-c'.
  1953. This variable can also have the value `expert'. In this case, the window
  1954. displaying the tags menu is not even shown, until you press C-c again."
  1955. :group 'org-tags
  1956. :type '(choice
  1957. (const :tag "No" nil)
  1958. (const :tag "Yes" t)
  1959. (const :tag "Expert" expert)))
  1960. (defvar org-fast-tag-selection-include-todo nil
  1961. "Non-nil means, fast tags selection interface will also offer TODO states.
  1962. This is an undocumented feature, you should not rely on it.")
  1963. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1964. "The column to which tags should be indented in a headline.
  1965. If this number is positive, it specifies the column. If it is negative,
  1966. it means that the tags should be flushright to that column. For example,
  1967. -80 works well for a normal 80 character screen."
  1968. :group 'org-tags
  1969. :type 'integer)
  1970. (defcustom org-auto-align-tags t
  1971. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1972. These operations change the length of a headline and therefore shift
  1973. the tags around. With this options turned on, after each such operation
  1974. the tags are again aligned to `org-tags-column'."
  1975. :group 'org-tags
  1976. :type 'boolean)
  1977. (defcustom org-use-tag-inheritance t
  1978. "Non-nil means, tags in levels apply also for sublevels.
  1979. When nil, only the tags directly given in a specific line apply there.
  1980. This may also be a list of tags that should be inherited, or a regexp that
  1981. matches tags that should be inherited. Additional control is possible
  1982. with the variable `org-tags-exclude-from-inheritance' which gives an
  1983. explicit list of tags to be excluded from inheritance., even if the value of
  1984. `org-use-tag-inheritance' would select it for inheritance.
  1985. If this option is t, a match early-on in a tree can lead to a large
  1986. number of matches in the subtree when constructing the agenda or creating
  1987. a sparse tree. If you only want to see the first match in a tree during
  1988. a search, check out the variable `org-tags-match-list-sublevels'."
  1989. :group 'org-tags
  1990. :type '(choice
  1991. (const :tag "Not" nil)
  1992. (const :tag "Always" t)
  1993. (repeat :tag "Specific tags" (string :tag "Tag"))
  1994. (regexp :tag "Tags matched by regexp")))
  1995. (defcustom org-tags-exclude-from-inheritance nil
  1996. "List of tags that should never be inherited.
  1997. This is a way to exclude a few tags from inheritance. For way to do
  1998. the opposite, to actively allow inheritance for selected tags,
  1999. see the variable `org-use-tag-inheritance'."
  2000. :group 'org-tags
  2001. :type '(repeat (string :tag "Tag")))
  2002. (defun org-tag-inherit-p (tag)
  2003. "Check if TAG is one that should be inherited."
  2004. (cond
  2005. ((member tag org-tags-exclude-from-inheritance) nil)
  2006. ((eq org-use-tag-inheritance t) t)
  2007. ((not org-use-tag-inheritance) nil)
  2008. ((stringp org-use-tag-inheritance)
  2009. (string-match org-use-tag-inheritance tag))
  2010. ((listp org-use-tag-inheritance)
  2011. (member tag org-use-tag-inheritance))
  2012. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2013. (defcustom org-tags-match-list-sublevels t
  2014. "Non-nil means list also sublevels of headlines matching a search.
  2015. This variable applies to tags/property searches, and also to stuck
  2016. projects because this search is based on a tags match as well.
  2017. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2018. the sublevels of a headline matching a tag search often also match
  2019. the same search. Listing all of them can create very long lists.
  2020. Setting this variable to nil causes subtrees of a match to be skipped.
  2021. As a special case, if the tag search is restricted to TODO items, the
  2022. value of this variable is ignored and sublevels are always checked, to
  2023. make sure all corresponding TODO items find their way into the list.
  2024. This variable is semi-obsolete and probably should always be true. It
  2025. is better to limit inheritance to certain tags using the variables
  2026. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2027. :group 'org-tags
  2028. :type '(choice
  2029. (const :tag "No, don't list them" nil)
  2030. (const :tag "Yes, do list them" t)
  2031. (const :tag "List them, indented with leading dots" indented)))
  2032. (defvar org-tags-history nil
  2033. "History of minibuffer reads for tags.")
  2034. (defvar org-last-tags-completion-table nil
  2035. "The last used completion table for tags.")
  2036. (defvar org-after-tags-change-hook nil
  2037. "Hook that is run after the tags in a line have changed.")
  2038. (defgroup org-properties nil
  2039. "Options concerning properties in Org-mode."
  2040. :tag "Org Properties"
  2041. :group 'org)
  2042. (defcustom org-property-format "%-10s %s"
  2043. "How property key/value pairs should be formatted by `indent-line'.
  2044. When `indent-line' hits a property definition, it will format the line
  2045. according to this format, mainly to make sure that the values are
  2046. lined-up with respect to each other."
  2047. :group 'org-properties
  2048. :type 'string)
  2049. (defcustom org-use-property-inheritance nil
  2050. "Non-nil means, properties apply also for sublevels.
  2051. This setting is chiefly used during property searches. Turning it on can
  2052. cause significant overhead when doing a search, which is why it is not
  2053. on by default.
  2054. When nil, only the properties directly given in the current entry count.
  2055. When t, every property is inherited. The value may also be a list of
  2056. properties that should have inheritance, or a regular expression matching
  2057. properties that should be inherited.
  2058. However, note that some special properties use inheritance under special
  2059. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2060. and the properties ending in \"_ALL\" when they are used as descriptor
  2061. for valid values of a property.
  2062. Note for programmers:
  2063. When querying an entry with `org-entry-get', you can control if inheritance
  2064. should be used. By default, `org-entry-get' looks only at the local
  2065. properties. You can request inheritance by setting the inherit argument
  2066. to t (to force inheritance) or to `selective' (to respect the setting
  2067. in this variable)."
  2068. :group 'org-properties
  2069. :type '(choice
  2070. (const :tag "Not" nil)
  2071. (const :tag "Always" t)
  2072. (repeat :tag "Specific properties" (string :tag "Property"))
  2073. (regexp :tag "Properties matched by regexp")))
  2074. (defun org-property-inherit-p (property)
  2075. "Check if PROPERTY is one that should be inherited."
  2076. (cond
  2077. ((eq org-use-property-inheritance t) t)
  2078. ((not org-use-property-inheritance) nil)
  2079. ((stringp org-use-property-inheritance)
  2080. (string-match org-use-property-inheritance property))
  2081. ((listp org-use-property-inheritance)
  2082. (member property org-use-property-inheritance))
  2083. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2084. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2085. "The default column format, if no other format has been defined.
  2086. This variable can be set on the per-file basis by inserting a line
  2087. #+COLUMNS: %25ITEM ....."
  2088. :group 'org-properties
  2089. :type 'string)
  2090. (defcustom org-columns-ellipses ".."
  2091. "The ellipses to be used when a field in column view is truncated.
  2092. When this is the empty string, as many characters as possible are shown,
  2093. but then there will be no visual indication that the field has been truncated.
  2094. When this is a string of length N, the last N characters of a truncated
  2095. field are replaced by this string. If the column is narrower than the
  2096. ellipses string, only part of the ellipses string will be shown."
  2097. :group 'org-properties
  2098. :type 'string)
  2099. (defcustom org-columns-modify-value-for-display-function nil
  2100. "Function that modifies values for display in column view.
  2101. For example, it can be used to cut out a certain part from a time stamp.
  2102. The function must take 2 arguments:
  2103. column-title The title of the column (*not* the property name)
  2104. value The value that should be modified.
  2105. The function should return the value that should be displayed,
  2106. or nil if the normal value should be used."
  2107. :group 'org-properties
  2108. :type 'function)
  2109. (defcustom org-effort-property "Effort"
  2110. "The property that is being used to keep track of effort estimates.
  2111. Effort estimates given in this property need to have the format H:MM."
  2112. :group 'org-properties
  2113. :group 'org-progress
  2114. :type '(string :tag "Property"))
  2115. (defconst org-global-properties-fixed
  2116. '(("VISIBILITY_ALL" . "folded children content all"))
  2117. "List of property/value pairs that can be inherited by any entry.
  2118. These are fixed values, for the preset properties. The user variable
  2119. that can be used to add to this list is `org-global-properties'.
  2120. The entries in this list are cons cells where the car is a property
  2121. name and cdr is a string with the value. If the value represents
  2122. multiple items like an \"_ALL\" property, separate the items by
  2123. spaces.")
  2124. (defcustom org-global-properties nil
  2125. "List of property/value pairs that can be inherited by any entry.
  2126. This list will be combined with the constant `org-global-properties-fixed'.
  2127. The entries in this list are cons cells where the car is a property
  2128. name and cdr is a string with the value.
  2129. You can set buffer-local values for the same purpose in the variable
  2130. `org-file-properties' this by adding lines like
  2131. #+PROPERTY: NAME VALUE"
  2132. :group 'org-properties
  2133. :type '(repeat
  2134. (cons (string :tag "Property")
  2135. (string :tag "Value"))))
  2136. (defvar org-file-properties nil
  2137. "List of property/value pairs that can be inherited by any entry.
  2138. Valid for the current buffer.
  2139. This variable is populated from #+PROPERTY lines.")
  2140. (make-variable-buffer-local 'org-file-properties)
  2141. (defgroup org-agenda nil
  2142. "Options concerning agenda views in Org-mode."
  2143. :tag "Org Agenda"
  2144. :group 'org)
  2145. (defvar org-category nil
  2146. "Variable used by org files to set a category for agenda display.
  2147. Such files should use a file variable to set it, for example
  2148. # -*- mode: org; org-category: \"ELisp\"
  2149. or contain a special line
  2150. #+CATEGORY: ELisp
  2151. If the file does not specify a category, then file's base name
  2152. is used instead.")
  2153. (make-variable-buffer-local 'org-category)
  2154. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2155. (defcustom org-agenda-files nil
  2156. "The files to be used for agenda display.
  2157. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2158. \\[org-remove-file]. You can also use customize to edit the list.
  2159. If an entry is a directory, all files in that directory that are matched by
  2160. `org-agenda-file-regexp' will be part of the file list.
  2161. If the value of the variable is not a list but a single file name, then
  2162. the list of agenda files is actually stored and maintained in that file, one
  2163. agenda file per line."
  2164. :group 'org-agenda
  2165. :type '(choice
  2166. (repeat :tag "List of files and directories" file)
  2167. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2168. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2169. "Regular expression to match files for `org-agenda-files'.
  2170. If any element in the list in that variable contains a directory instead
  2171. of a normal file, all files in that directory that are matched by this
  2172. regular expression will be included."
  2173. :group 'org-agenda
  2174. :type 'regexp)
  2175. (defcustom org-agenda-text-search-extra-files nil
  2176. "List of extra files to be searched by text search commands.
  2177. These files will be search in addition to the agenda files by the
  2178. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2179. Note that these files will only be searched for text search commands,
  2180. not for the other agenda views like todo lists, tag searches or the weekly
  2181. agenda. This variable is intended to list notes and possibly archive files
  2182. that should also be searched by these two commands.
  2183. In fact, if the first element in the list is the symbol `agenda-archives',
  2184. than all archive files of all agenda files will be added to the search
  2185. scope."
  2186. :group 'org-agenda
  2187. :type '(set :greedy t
  2188. (const :tag "Agenda Archives" agenda-archives)
  2189. (repeat :inline t (file))))
  2190. (if (fboundp 'defvaralias)
  2191. (defvaralias 'org-agenda-multi-occur-extra-files
  2192. 'org-agenda-text-search-extra-files))
  2193. (defcustom org-agenda-skip-unavailable-files nil
  2194. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2195. A nil value means to remove them, after a query, from the list."
  2196. :group 'org-agenda
  2197. :type 'boolean)
  2198. (defcustom org-calendar-to-agenda-key [?c]
  2199. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2200. The command `org-calendar-goto-agenda' will be bound to this key. The
  2201. default is the character `c' because then `c' can be used to switch back and
  2202. forth between agenda and calendar."
  2203. :group 'org-agenda
  2204. :type 'sexp)
  2205. (defcustom org-calendar-agenda-action-key [?k]
  2206. "The key to be installed in `calendar-mode-map' for agenda-action.
  2207. The command `org-agenda-action' will be bound to this key. The
  2208. default is the character `k' because we use the same key in the agenda."
  2209. :group 'org-agenda
  2210. :type 'sexp)
  2211. (eval-after-load "calendar"
  2212. '(progn
  2213. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2214. 'org-calendar-goto-agenda)
  2215. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2216. 'org-agenda-action)))
  2217. (defgroup org-latex nil
  2218. "Options for embedding LaTeX code into Org-mode."
  2219. :tag "Org LaTeX"
  2220. :group 'org)
  2221. (defcustom org-format-latex-options
  2222. '(:foreground default :background default :scale 1.0
  2223. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2224. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2225. "Options for creating images from LaTeX fragments.
  2226. This is a property list with the following properties:
  2227. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2228. `default' means use the foreground of the default face.
  2229. :background the background color, or \"Transparent\".
  2230. `default' means use the background of the default face.
  2231. :scale a scaling factor for the size of the images.
  2232. :html-foreground, :html-background, :html-scale
  2233. the same numbers for HTML export.
  2234. :matchers a list indicating which matchers should be used to
  2235. find LaTeX fragments. Valid members of this list are:
  2236. \"begin\" find environments
  2237. \"$1\" find single characters surrounded by $.$
  2238. \"$\" find math expressions surrounded by $...$
  2239. \"$$\" find math expressions surrounded by $$....$$
  2240. \"\\(\" find math expressions surrounded by \\(...\\)
  2241. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2242. :group 'org-latex
  2243. :type 'plist)
  2244. (defcustom org-format-latex-header "\\documentclass{article}
  2245. \\usepackage{fullpage} % do not remove
  2246. \\usepackage{amssymb}
  2247. \\usepackage[usenames]{color}
  2248. \\usepackage{amsmath}
  2249. \\usepackage{latexsym}
  2250. \\usepackage[mathscr]{eucal}
  2251. \\pagestyle{empty} % do not remove"
  2252. "The document header used for processing LaTeX fragments."
  2253. :group 'org-latex
  2254. :type 'string)
  2255. (defgroup org-font-lock nil
  2256. "Font-lock settings for highlighting in Org-mode."
  2257. :tag "Org Font Lock"
  2258. :group 'org)
  2259. (defcustom org-level-color-stars-only nil
  2260. "Non-nil means fontify only the stars in each headline.
  2261. When nil, the entire headline is fontified.
  2262. Changing it requires restart of `font-lock-mode' to become effective
  2263. also in regions already fontified."
  2264. :group 'org-font-lock
  2265. :type 'boolean)
  2266. (defcustom org-hide-leading-stars nil
  2267. "Non-nil means, hide the first N-1 stars in a headline.
  2268. This works by using the face `org-hide' for these stars. This
  2269. face is white for a light background, and black for a dark
  2270. background. You may have to customize the face `org-hide' to
  2271. make this work.
  2272. Changing it requires restart of `font-lock-mode' to become effective
  2273. also in regions already fontified.
  2274. You may also set this on a per-file basis by adding one of the following
  2275. lines to the buffer:
  2276. #+STARTUP: hidestars
  2277. #+STARTUP: showstars"
  2278. :group 'org-font-lock
  2279. :type 'boolean)
  2280. (defcustom org-fontify-done-headline nil
  2281. "Non-nil means, change the face of a headline if it is marked DONE.
  2282. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2283. When this is non-nil, the headline after the keyword is set to the
  2284. `org-headline-done' as an additional indication."
  2285. :group 'org-font-lock
  2286. :type 'boolean)
  2287. (defcustom org-fontify-emphasized-text t
  2288. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2289. Changing this variable requires a restart of Emacs to take effect."
  2290. :group 'org-font-lock
  2291. :type 'boolean)
  2292. (defcustom org-highlight-latex-fragments-and-specials nil
  2293. "Non-nil means, fontify what is treated specially by the exporters."
  2294. :group 'org-font-lock
  2295. :type 'boolean)
  2296. (defcustom org-hide-emphasis-markers nil
  2297. "Non-nil mean font-lock should hide the emphasis marker characters."
  2298. :group 'org-font-lock
  2299. :type 'boolean)
  2300. (defvar org-emph-re nil
  2301. "Regular expression for matching emphasis.")
  2302. (defvar org-verbatim-re nil
  2303. "Regular expression for matching verbatim text.")
  2304. (defvar org-emphasis-regexp-components) ; defined just below
  2305. (defvar org-emphasis-alist) ; defined just below
  2306. (defun org-set-emph-re (var val)
  2307. "Set variable and compute the emphasis regular expression."
  2308. (set var val)
  2309. (when (and (boundp 'org-emphasis-alist)
  2310. (boundp 'org-emphasis-regexp-components)
  2311. org-emphasis-alist org-emphasis-regexp-components)
  2312. (let* ((e org-emphasis-regexp-components)
  2313. (pre (car e))
  2314. (post (nth 1 e))
  2315. (border (nth 2 e))
  2316. (body (nth 3 e))
  2317. (nl (nth 4 e))
  2318. (body1 (concat body "*?"))
  2319. (markers (mapconcat 'car org-emphasis-alist ""))
  2320. (vmarkers (mapconcat
  2321. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2322. org-emphasis-alist "")))
  2323. ;; make sure special characters appear at the right position in the class
  2324. (if (string-match "\\^" markers)
  2325. (setq markers (concat (replace-match "" t t markers) "^")))
  2326. (if (string-match "-" markers)
  2327. (setq markers (concat (replace-match "" t t markers) "-")))
  2328. (if (string-match "\\^" vmarkers)
  2329. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2330. (if (string-match "-" vmarkers)
  2331. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2332. (if (> nl 0)
  2333. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2334. (int-to-string nl) "\\}")))
  2335. ;; Make the regexp
  2336. (setq org-emph-re
  2337. (concat "\\([" pre "]\\|^\\)"
  2338. "\\("
  2339. "\\([" markers "]\\)"
  2340. "\\("
  2341. "[^" border "]\\|"
  2342. "[^" border "]"
  2343. body1
  2344. "[^" border "]"
  2345. "\\)"
  2346. "\\3\\)"
  2347. "\\([" post "]\\|$\\)"))
  2348. (setq org-verbatim-re
  2349. (concat "\\([" pre "]\\|^\\)"
  2350. "\\("
  2351. "\\([" vmarkers "]\\)"
  2352. "\\("
  2353. "[^" border "]\\|"
  2354. "[^" border "]"
  2355. body1
  2356. "[^" border "]"
  2357. "\\)"
  2358. "\\3\\)"
  2359. "\\([" post "]\\|$\\)")))))
  2360. (defcustom org-emphasis-regexp-components
  2361. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2362. "Components used to build the regular expression for emphasis.
  2363. This is a list with 6 entries. Terminology: In an emphasis string
  2364. like \" *strong word* \", we call the initial space PREMATCH, the final
  2365. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2366. and \"trong wor\" is the body. The different components in this variable
  2367. specify what is allowed/forbidden in each part:
  2368. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2369. post Chars allowed as postmatch. End of line will be allowed too.
  2370. border The chars *forbidden* as border characters.
  2371. body-regexp A regexp like \".\" to match a body character. Don't use
  2372. non-shy groups here, and don't allow newline here.
  2373. newline The maximum number of newlines allowed in an emphasis exp.
  2374. Use customize to modify this, or restart Emacs after changing it."
  2375. :group 'org-font-lock
  2376. :set 'org-set-emph-re
  2377. :type '(list
  2378. (sexp :tag "Allowed chars in pre ")
  2379. (sexp :tag "Allowed chars in post ")
  2380. (sexp :tag "Forbidden chars in border ")
  2381. (sexp :tag "Regexp for body ")
  2382. (integer :tag "number of newlines allowed")
  2383. (option (boolean :tag "Please ignore this button"))))
  2384. (defcustom org-emphasis-alist
  2385. `(("*" bold "<b>" "</b>")
  2386. ("/" italic "<i>" "</i>")
  2387. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2388. ("=" org-code "<code>" "</code>" verbatim)
  2389. ("~" org-verbatim "<code>" "</code>" verbatim)
  2390. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2391. "<del>" "</del>")
  2392. )
  2393. "Special syntax for emphasized text.
  2394. Text starting and ending with a special character will be emphasized, for
  2395. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2396. characters, the face to be used by font-lock for highlighting in Org-mode
  2397. Emacs buffers, and the HTML tags to be used for this.
  2398. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2399. Use customize to modify this, or restart Emacs after changing it."
  2400. :group 'org-font-lock
  2401. :set 'org-set-emph-re
  2402. :type '(repeat
  2403. (list
  2404. (string :tag "Marker character")
  2405. (choice
  2406. (face :tag "Font-lock-face")
  2407. (plist :tag "Face property list"))
  2408. (string :tag "HTML start tag")
  2409. (string :tag "HTML end tag")
  2410. (option (const verbatim)))))
  2411. ;;; Miscellaneous options
  2412. (defgroup org-completion nil
  2413. "Completion in Org-mode."
  2414. :tag "Org Completion"
  2415. :group 'org)
  2416. (defcustom org-completion-use-ido nil
  2417. "Non-nil means, use ido completion wherever possible.
  2418. Note that `ido-mode' must be active for this variable to be relevant.
  2419. If you decide to turn this variable on, you might well want to turn off
  2420. `org-outline-path-complete-in-steps'."
  2421. :group 'org-completion
  2422. :type 'boolean)
  2423. (defcustom org-completion-fallback-command 'hippie-expand
  2424. "The expansion command called by \\[org-complete] in normal context.
  2425. Normal means, no org-mode-specific context."
  2426. :group 'org-completion
  2427. :type 'function)
  2428. ;;; Functions and variables from ther packages
  2429. ;; Declared here to avoid compiler warnings
  2430. ;; XEmacs only
  2431. (defvar outline-mode-menu-heading)
  2432. (defvar outline-mode-menu-show)
  2433. (defvar outline-mode-menu-hide)
  2434. (defvar zmacs-regions) ; XEmacs regions
  2435. ;; Emacs only
  2436. (defvar mark-active)
  2437. ;; Various packages
  2438. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2439. (declare-function calendar-forward-day "cal-move" (arg))
  2440. (declare-function calendar-goto-date "cal-move" (date))
  2441. (declare-function calendar-goto-today "cal-move" ())
  2442. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2443. (defvar calc-embedded-close-formula)
  2444. (defvar calc-embedded-open-formula)
  2445. (declare-function cdlatex-tab "ext:cdlatex" ())
  2446. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2447. (defvar font-lock-unfontify-region-function)
  2448. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2449. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2450. (defvar iswitchb-temp-buflist)
  2451. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2452. (defvar org-agenda-tags-todo-honor-ignore-options)
  2453. (declare-function org-agenda-skip "org-agenda" ())
  2454. (declare-function org-format-agenda-item "org-agenda"
  2455. (extra txt &optional category tags dotime noprefix remove-re))
  2456. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2457. (declare-function org-agenda-change-all-lines "org-agenda"
  2458. (newhead hdmarker &optional fixface just-this))
  2459. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2460. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2461. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2462. (beg end))
  2463. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2464. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2465. "org-agenda" (&optional end))
  2466. (declare-function parse-time-string "parse-time" (string))
  2467. (declare-function remember "remember" (&optional initial))
  2468. (declare-function remember-buffer-desc "remember" ())
  2469. (declare-function remember-finalize "remember" ())
  2470. (defvar remember-save-after-remembering)
  2471. (defvar remember-data-file)
  2472. (defvar remember-register)
  2473. (defvar remember-buffer)
  2474. (defvar remember-handler-functions)
  2475. (defvar remember-annotation-functions)
  2476. (defvar texmathp-why)
  2477. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2478. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2479. (defvar w3m-current-url)
  2480. (defvar w3m-current-title)
  2481. (defvar org-latex-regexps)
  2482. ;;; Autoload and prepare some org modules
  2483. ;; Some table stuff that needs to be defined here, because it is used
  2484. ;; by the functions setting up org-mode or checking for table context.
  2485. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2486. "Detects an org-type or table-type table.")
  2487. (defconst org-table-line-regexp "^[ \t]*|"
  2488. "Detects an org-type table line.")
  2489. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2490. "Detects an org-type table line.")
  2491. (defconst org-table-hline-regexp "^[ \t]*|-"
  2492. "Detects an org-type table hline.")
  2493. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2494. "Detects a table-type table hline.")
  2495. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2496. "Searching from within a table (any type) this finds the first line
  2497. outside the table.")
  2498. ;; Autoload the functions in org-table.el that are needed by functions here.
  2499. (eval-and-compile
  2500. (org-autoload "org-table"
  2501. '(org-table-align org-table-begin org-table-blank-field
  2502. org-table-convert org-table-convert-region org-table-copy-down
  2503. org-table-copy-region org-table-create
  2504. org-table-create-or-convert-from-region
  2505. org-table-create-with-table.el org-table-current-dline
  2506. org-table-cut-region org-table-delete-column org-table-edit-field
  2507. org-table-edit-formulas org-table-end org-table-eval-formula
  2508. org-table-export org-table-field-info
  2509. org-table-get-stored-formulas org-table-goto-column
  2510. org-table-hline-and-move org-table-import org-table-insert-column
  2511. org-table-insert-hline org-table-insert-row org-table-iterate
  2512. org-table-justify-field-maybe org-table-kill-row
  2513. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2514. org-table-move-column org-table-move-column-left
  2515. org-table-move-column-right org-table-move-row
  2516. org-table-move-row-down org-table-move-row-up
  2517. org-table-next-field org-table-next-row org-table-paste-rectangle
  2518. org-table-previous-field org-table-recalculate
  2519. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2520. org-table-toggle-coordinate-overlays
  2521. org-table-toggle-formula-debugger org-table-wrap-region
  2522. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2523. (defun org-at-table-p (&optional table-type)
  2524. "Return t if the cursor is inside an org-type table.
  2525. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2526. (if org-enable-table-editor
  2527. (save-excursion
  2528. (beginning-of-line 1)
  2529. (looking-at (if table-type org-table-any-line-regexp
  2530. org-table-line-regexp)))
  2531. nil))
  2532. (defsubst org-table-p () (org-at-table-p))
  2533. (defun org-at-table.el-p ()
  2534. "Return t if and only if we are at a table.el table."
  2535. (and (org-at-table-p 'any)
  2536. (save-excursion
  2537. (goto-char (org-table-begin 'any))
  2538. (looking-at org-table1-hline-regexp))))
  2539. (defun org-table-recognize-table.el ()
  2540. "If there is a table.el table nearby, recognize it and move into it."
  2541. (if org-table-tab-recognizes-table.el
  2542. (if (org-at-table.el-p)
  2543. (progn
  2544. (beginning-of-line 1)
  2545. (if (looking-at org-table-dataline-regexp)
  2546. nil
  2547. (if (looking-at org-table1-hline-regexp)
  2548. (progn
  2549. (beginning-of-line 2)
  2550. (if (looking-at org-table-any-border-regexp)
  2551. (beginning-of-line -1)))))
  2552. (if (re-search-forward "|" (org-table-end t) t)
  2553. (progn
  2554. (require 'table)
  2555. (if (table--at-cell-p (point))
  2556. t
  2557. (message "recognizing table.el table...")
  2558. (table-recognize-table)
  2559. (message "recognizing table.el table...done")))
  2560. (error "This should not happen..."))
  2561. t)
  2562. nil)
  2563. nil))
  2564. (defun org-at-table-hline-p ()
  2565. "Return t if the cursor is inside a hline in a table."
  2566. (if org-enable-table-editor
  2567. (save-excursion
  2568. (beginning-of-line 1)
  2569. (looking-at org-table-hline-regexp))
  2570. nil))
  2571. (defvar org-table-clean-did-remove-column nil)
  2572. (defun org-table-map-tables (function)
  2573. "Apply FUNCTION to the start of all tables in the buffer."
  2574. (save-excursion
  2575. (save-restriction
  2576. (widen)
  2577. (goto-char (point-min))
  2578. (while (re-search-forward org-table-any-line-regexp nil t)
  2579. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2580. (beginning-of-line 1)
  2581. (if (looking-at org-table-line-regexp)
  2582. (save-excursion (funcall function)))
  2583. (re-search-forward org-table-any-border-regexp nil 1))))
  2584. (message "Mapping tables: done"))
  2585. ;; Declare and autoload functions from org-exp.el & Co
  2586. (declare-function org-default-export-plist "org-exp")
  2587. (declare-function org-infile-export-plist "org-exp")
  2588. (declare-function org-get-current-options "org-exp")
  2589. (eval-and-compile
  2590. (org-autoload "org-exp"
  2591. '(org-export org-export-visible
  2592. org-insert-export-options-template
  2593. org-table-clean-before-export))
  2594. (org-autoload "org-ascii"
  2595. '(org-export-as-ascii org-export-ascii-preprocess
  2596. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2597. org-export-region-as-ascii))
  2598. (org-autoload "org-html"
  2599. '(org-export-as-html-and-open
  2600. org-export-as-html-batch org-export-as-html-to-buffer
  2601. org-replace-region-by-html org-export-region-as-html
  2602. org-export-as-html))
  2603. (org-autoload "org-icalendar"
  2604. '(org-export-icalendar-this-file
  2605. org-export-icalendar-all-agenda-files
  2606. org-export-icalendar-combine-agenda-files))
  2607. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2608. ;; Declare and autoload functions from org-agenda.el
  2609. (eval-and-compile
  2610. (org-autoload "org-agenda"
  2611. '(org-agenda org-agenda-list org-search-view
  2612. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2613. org-diary org-agenda-to-appt
  2614. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2615. ;; Autoload org-remember
  2616. (eval-and-compile
  2617. (org-autoload "org-remember"
  2618. '(org-remember-insinuate org-remember-annotation
  2619. org-remember-apply-template org-remember org-remember-handler)))
  2620. ;; Autoload org-clock.el
  2621. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2622. (beg end))
  2623. (declare-function org-clock-update-mode-line "org-clock" ())
  2624. (defvar org-clock-start-time)
  2625. (defvar org-clock-marker (make-marker)
  2626. "Marker recording the last clock-in.")
  2627. (eval-and-compile
  2628. (org-autoload
  2629. "org-clock"
  2630. '(org-clock-in org-clock-out org-clock-cancel
  2631. org-clock-goto org-clock-sum org-clock-display
  2632. org-clock-remove-overlays org-clock-report
  2633. org-clocktable-shift org-dblock-write:clocktable
  2634. org-get-clocktable)))
  2635. (defun org-clock-update-time-maybe ()
  2636. "If this is a CLOCK line, update it and return t.
  2637. Otherwise, return nil."
  2638. (interactive)
  2639. (save-excursion
  2640. (beginning-of-line 1)
  2641. (skip-chars-forward " \t")
  2642. (when (looking-at org-clock-string)
  2643. (let ((re (concat "[ \t]*" org-clock-string
  2644. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2645. "\\([ \t]*=>.*\\)?\\)?"))
  2646. ts te h m s neg)
  2647. (cond
  2648. ((not (looking-at re))
  2649. nil)
  2650. ((not (match-end 2))
  2651. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2652. (> org-clock-marker (point))
  2653. (<= org-clock-marker (point-at-eol)))
  2654. ;; The clock is running here
  2655. (setq org-clock-start-time
  2656. (apply 'encode-time
  2657. (org-parse-time-string (match-string 1))))
  2658. (org-clock-update-mode-line)))
  2659. (t
  2660. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2661. (end-of-line 1)
  2662. (setq ts (match-string 1)
  2663. te (match-string 3))
  2664. (setq s (- (time-to-seconds
  2665. (apply 'encode-time (org-parse-time-string te)))
  2666. (time-to-seconds
  2667. (apply 'encode-time (org-parse-time-string ts))))
  2668. neg (< s 0)
  2669. s (abs s)
  2670. h (floor (/ s 3600))
  2671. s (- s (* 3600 h))
  2672. m (floor (/ s 60))
  2673. s (- s (* 60 s)))
  2674. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2675. t))))))
  2676. (defun org-check-running-clock ()
  2677. "Check if the current buffer contains the running clock.
  2678. If yes, offer to stop it and to save the buffer with the changes."
  2679. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2680. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2681. (buffer-name))))
  2682. (org-clock-out)
  2683. (when (y-or-n-p "Save changed buffer?")
  2684. (save-buffer))))
  2685. (defun org-clocktable-try-shift (dir n)
  2686. "Check if this line starts a clock table, if yes, shift the time block."
  2687. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2688. (org-clocktable-shift dir n)))
  2689. ;; Autoload org-timer.el
  2690. (eval-and-compile
  2691. (org-autoload
  2692. "org-timer"
  2693. '(org-timer-start org-timer org-timer-item
  2694. org-timer-change-times-in-region)))
  2695. ;; Autoload org-feed.el
  2696. (eval-and-compile
  2697. (org-autoload
  2698. "org-feed"
  2699. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2700. ;; Autoload archiving code
  2701. ;; The stuff that is needed for cycling and tags has to be defined here.
  2702. (defgroup org-archive nil
  2703. "Options concerning archiving in Org-mode."
  2704. :tag "Org Archive"
  2705. :group 'org-structure)
  2706. (defcustom org-archive-location "%s_archive::"
  2707. "The location where subtrees should be archived.
  2708. The value of this variable is a string, consisting of two parts,
  2709. separated by a double-colon. The first part is a filename and
  2710. the second part is a headline.
  2711. When the filename is omitted, archiving happens in the same file.
  2712. %s in the filename will be replaced by the current file
  2713. name (without the directory part). Archiving to a different file
  2714. is useful to keep archived entries from contributing to the
  2715. Org-mode Agenda.
  2716. The archived entries will be filed as subtrees of the specified
  2717. headline. When the headline is omitted, the subtrees are simply
  2718. filed away at the end of the file, as top-level entries. Also in
  2719. the heading you can use %s to represent the file name, this can be
  2720. useful when using the same archive for a number of different files.
  2721. Here are a few examples:
  2722. \"%s_archive::\"
  2723. If the current file is Projects.org, archive in file
  2724. Projects.org_archive, as top-level trees. This is the default.
  2725. \"::* Archived Tasks\"
  2726. Archive in the current file, under the top-level headline
  2727. \"* Archived Tasks\".
  2728. \"~/org/archive.org::\"
  2729. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2730. \"~/org/archive.org::From %s\"
  2731. Archive in file ~/org/archive.org (absolute path), und headlines
  2732. \"From FILENAME\" where file name is the current file name.
  2733. \"basement::** Finished Tasks\"
  2734. Archive in file ./basement (relative path), as level 3 trees
  2735. below the level 2 heading \"** Finished Tasks\".
  2736. You may set this option on a per-file basis by adding to the buffer a
  2737. line like
  2738. #+ARCHIVE: basement::** Finished Tasks
  2739. You may also define it locally for a subtree by setting an ARCHIVE property
  2740. in the entry. If such a property is found in an entry, or anywhere up
  2741. the hierarchy, it will be used."
  2742. :group 'org-archive
  2743. :type 'string)
  2744. (defcustom org-archive-tag "ARCHIVE"
  2745. "The tag that marks a subtree as archived.
  2746. An archived subtree does not open during visibility cycling, and does
  2747. not contribute to the agenda listings.
  2748. After changing this, font-lock must be restarted in the relevant buffers to
  2749. get the proper fontification."
  2750. :group 'org-archive
  2751. :group 'org-keywords
  2752. :type 'string)
  2753. (defcustom org-agenda-skip-archived-trees t
  2754. "Non-nil means, the agenda will skip any items located in archived trees.
  2755. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2756. variable is no longer recommended, you should leave it at the value t.
  2757. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2758. :group 'org-archive
  2759. :group 'org-agenda-skip
  2760. :type 'boolean)
  2761. (defcustom org-columns-skip-arrchived-trees t
  2762. "Non-nil means, irgnore archived trees when creating column view."
  2763. :group 'org-archive
  2764. :group 'org-properties
  2765. :type 'boolean)
  2766. (defcustom org-cycle-open-archived-trees nil
  2767. "Non-nil means, `org-cycle' will open archived trees.
  2768. An archived tree is a tree marked with the tag ARCHIVE.
  2769. When nil, archived trees will stay folded. You can still open them with
  2770. normal outline commands like `show-all', but not with the cycling commands."
  2771. :group 'org-archive
  2772. :group 'org-cycle
  2773. :type 'boolean)
  2774. (defcustom org-sparse-tree-open-archived-trees nil
  2775. "Non-nil means sparse tree construction shows matches in archived trees.
  2776. When nil, matches in these trees are highlighted, but the trees are kept in
  2777. collapsed state."
  2778. :group 'org-archive
  2779. :group 'org-sparse-trees
  2780. :type 'boolean)
  2781. (defun org-cycle-hide-archived-subtrees (state)
  2782. "Re-hide all archived subtrees after a visibility state change."
  2783. (when (and (not org-cycle-open-archived-trees)
  2784. (not (memq state '(overview folded))))
  2785. (save-excursion
  2786. (let* ((globalp (memq state '(contents all)))
  2787. (beg (if globalp (point-min) (point)))
  2788. (end (if globalp (point-max) (org-end-of-subtree t))))
  2789. (org-hide-archived-subtrees beg end)
  2790. (goto-char beg)
  2791. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2792. (message "%s" (substitute-command-keys
  2793. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2794. (defun org-force-cycle-archived ()
  2795. "Cycle subtree even if it is archived."
  2796. (interactive)
  2797. (setq this-command 'org-cycle)
  2798. (let ((org-cycle-open-archived-trees t))
  2799. (call-interactively 'org-cycle)))
  2800. (defun org-hide-archived-subtrees (beg end)
  2801. "Re-hide all archived subtrees after a visibility state change."
  2802. (save-excursion
  2803. (let* ((re (concat ":" org-archive-tag ":")))
  2804. (goto-char beg)
  2805. (while (re-search-forward re end t)
  2806. (and (org-on-heading-p) (hide-subtree))
  2807. (org-end-of-subtree t)))))
  2808. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2809. (eval-and-compile
  2810. (org-autoload "org-archive"
  2811. '(org-add-archive-files org-archive-subtree
  2812. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2813. ;; Autoload Column View Code
  2814. (declare-function org-columns-number-to-string "org-colview")
  2815. (declare-function org-columns-get-format-and-top-level "org-colview")
  2816. (declare-function org-columns-compute "org-colview")
  2817. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2818. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2819. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2820. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2821. ;; Autoload ID code
  2822. (declare-function org-id-store-link "org-id")
  2823. (declare-function org-id-locations-load "org-id")
  2824. (declare-function org-id-locations-save "org-id")
  2825. (defvar org-id-track-globally)
  2826. (org-autoload "org-id"
  2827. '(org-id-get-create org-id-new org-id-copy org-id-get
  2828. org-id-get-with-outline-path-completion
  2829. org-id-get-with-outline-drilling
  2830. org-id-goto org-id-find org-id-store-link))
  2831. ;; Autoload Plotting Code
  2832. (org-autoload "org-plot"
  2833. '(org-plot/gnuplot))
  2834. ;;; Variables for pre-computed regular expressions, all buffer local
  2835. (defvar org-drawer-regexp nil
  2836. "Matches first line of a hidden block.")
  2837. (make-variable-buffer-local 'org-drawer-regexp)
  2838. (defvar org-todo-regexp nil
  2839. "Matches any of the TODO state keywords.")
  2840. (make-variable-buffer-local 'org-todo-regexp)
  2841. (defvar org-not-done-regexp nil
  2842. "Matches any of the TODO state keywords except the last one.")
  2843. (make-variable-buffer-local 'org-not-done-regexp)
  2844. (defvar org-not-done-heading-regexp nil
  2845. "Matches a TODO headline that is not done.")
  2846. (make-variable-buffer-local 'org-not-done-regexp)
  2847. (defvar org-todo-line-regexp nil
  2848. "Matches a headline and puts TODO state into group 2 if present.")
  2849. (make-variable-buffer-local 'org-todo-line-regexp)
  2850. (defvar org-complex-heading-regexp nil
  2851. "Matches a headline and puts everything into groups:
  2852. group 1: the stars
  2853. group 2: The todo keyword, maybe
  2854. group 3: Priority cookie
  2855. group 4: True headline
  2856. group 5: Tags")
  2857. (make-variable-buffer-local 'org-complex-heading-regexp)
  2858. (defvar org-todo-line-tags-regexp nil
  2859. "Matches a headline and puts TODO state into group 2 if present.
  2860. Also put tags into group 4 if tags are present.")
  2861. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2862. (defvar org-nl-done-regexp nil
  2863. "Matches newline followed by a headline with the DONE keyword.")
  2864. (make-variable-buffer-local 'org-nl-done-regexp)
  2865. (defvar org-looking-at-done-regexp nil
  2866. "Matches the DONE keyword a point.")
  2867. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2868. (defvar org-ds-keyword-length 12
  2869. "Maximum length of the Deadline and SCHEDULED keywords.")
  2870. (make-variable-buffer-local 'org-ds-keyword-length)
  2871. (defvar org-deadline-regexp nil
  2872. "Matches the DEADLINE keyword.")
  2873. (make-variable-buffer-local 'org-deadline-regexp)
  2874. (defvar org-deadline-time-regexp nil
  2875. "Matches the DEADLINE keyword together with a time stamp.")
  2876. (make-variable-buffer-local 'org-deadline-time-regexp)
  2877. (defvar org-deadline-line-regexp nil
  2878. "Matches the DEADLINE keyword and the rest of the line.")
  2879. (make-variable-buffer-local 'org-deadline-line-regexp)
  2880. (defvar org-scheduled-regexp nil
  2881. "Matches the SCHEDULED keyword.")
  2882. (make-variable-buffer-local 'org-scheduled-regexp)
  2883. (defvar org-scheduled-time-regexp nil
  2884. "Matches the SCHEDULED keyword together with a time stamp.")
  2885. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2886. (defvar org-closed-time-regexp nil
  2887. "Matches the CLOSED keyword together with a time stamp.")
  2888. (make-variable-buffer-local 'org-closed-time-regexp)
  2889. (defvar org-keyword-time-regexp nil
  2890. "Matches any of the 4 keywords, together with the time stamp.")
  2891. (make-variable-buffer-local 'org-keyword-time-regexp)
  2892. (defvar org-keyword-time-not-clock-regexp nil
  2893. "Matches any of the 3 keywords, together with the time stamp.")
  2894. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2895. (defvar org-maybe-keyword-time-regexp nil
  2896. "Matches a timestamp, possibly preceeded by a keyword.")
  2897. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2898. (defvar org-planning-or-clock-line-re nil
  2899. "Matches a line with planning or clock info.")
  2900. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2901. (defconst org-plain-time-of-day-regexp
  2902. (concat
  2903. "\\(\\<[012]?[0-9]"
  2904. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2905. "\\(--?"
  2906. "\\(\\<[012]?[0-9]"
  2907. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2908. "\\)?")
  2909. "Regular expression to match a plain time or time range.
  2910. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2911. groups carry important information:
  2912. 0 the full match
  2913. 1 the first time, range or not
  2914. 8 the second time, if it is a range.")
  2915. (defconst org-plain-time-extension-regexp
  2916. (concat
  2917. "\\(\\<[012]?[0-9]"
  2918. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2919. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2920. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2921. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2922. groups carry important information:
  2923. 0 the full match
  2924. 7 hours of duration
  2925. 9 minutes of duration")
  2926. (defconst org-stamp-time-of-day-regexp
  2927. (concat
  2928. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2929. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2930. "\\(--?"
  2931. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2932. "Regular expression to match a timestamp time or time range.
  2933. After a match, the following groups carry important information:
  2934. 0 the full match
  2935. 1 date plus weekday, for backreferencing to make sure both times on same day
  2936. 2 the first time, range or not
  2937. 4 the second time, if it is a range.")
  2938. (defconst org-startup-options
  2939. '(("fold" org-startup-folded t)
  2940. ("overview" org-startup-folded t)
  2941. ("nofold" org-startup-folded nil)
  2942. ("showall" org-startup-folded nil)
  2943. ("content" org-startup-folded content)
  2944. ("hidestars" org-hide-leading-stars t)
  2945. ("showstars" org-hide-leading-stars nil)
  2946. ("odd" org-odd-levels-only t)
  2947. ("oddeven" org-odd-levels-only nil)
  2948. ("align" org-startup-align-all-tables t)
  2949. ("noalign" org-startup-align-all-tables nil)
  2950. ("customtime" org-display-custom-times t)
  2951. ("logdone" org-log-done time)
  2952. ("lognotedone" org-log-done note)
  2953. ("nologdone" org-log-done nil)
  2954. ("lognoteclock-out" org-log-note-clock-out t)
  2955. ("nolognoteclock-out" org-log-note-clock-out nil)
  2956. ("logrepeat" org-log-repeat state)
  2957. ("lognoterepeat" org-log-repeat note)
  2958. ("nologrepeat" org-log-repeat nil)
  2959. ("fninline" org-footnote-define-inline t)
  2960. ("nofninline" org-footnote-define-inline nil)
  2961. ("fnlocal" org-footnote-section nil)
  2962. ("fnauto" org-footnote-auto-label t)
  2963. ("fnprompt" org-footnote-auto-label nil)
  2964. ("fnconfirm" org-footnote-auto-label confirm)
  2965. ("fnplain" org-footnote-auto-label plain)
  2966. ("constcgs" constants-unit-system cgs)
  2967. ("constSI" constants-unit-system SI)
  2968. ("noptag" org-tag-persistent-alist nil))
  2969. "Variable associated with STARTUP options for org-mode.
  2970. Each element is a list of three items: The startup options as written
  2971. in the #+STARTUP line, the corresponding variable, and the value to
  2972. set this variable to if the option is found. An optional forth element PUSH
  2973. means to push this value onto the list in the variable.")
  2974. (defun org-set-regexps-and-options ()
  2975. "Precompute regular expressions for current buffer."
  2976. (when (org-mode-p)
  2977. (org-set-local 'org-todo-kwd-alist nil)
  2978. (org-set-local 'org-todo-key-alist nil)
  2979. (org-set-local 'org-todo-key-trigger nil)
  2980. (org-set-local 'org-todo-keywords-1 nil)
  2981. (org-set-local 'org-done-keywords nil)
  2982. (org-set-local 'org-todo-heads nil)
  2983. (org-set-local 'org-todo-sets nil)
  2984. (org-set-local 'org-todo-log-states nil)
  2985. (org-set-local 'org-file-properties nil)
  2986. (org-set-local 'org-file-tags nil)
  2987. (let ((re (org-make-options-regexp
  2988. '("CATEGORY" "TODO" "COLUMNS"
  2989. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2990. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2991. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2992. (splitre "[ \t]+")
  2993. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2994. tail sep kws1 prio props ftags drawers
  2995. ext-setup-or-nil setup-contents (start 0))
  2996. (save-excursion
  2997. (save-restriction
  2998. (widen)
  2999. (goto-char (point-min))
  3000. (while (or (and ext-setup-or-nil
  3001. (string-match re ext-setup-or-nil start)
  3002. (setq start (match-end 0)))
  3003. (and (setq ext-setup-or-nil nil start 0)
  3004. (re-search-forward re nil t)))
  3005. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3006. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3007. (cond
  3008. ((equal key "CATEGORY")
  3009. (if (string-match "[ \t]+$" value)
  3010. (setq value (replace-match "" t t value)))
  3011. (setq cat value))
  3012. ((member key '("SEQ_TODO" "TODO"))
  3013. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3014. ((equal key "TYP_TODO")
  3015. (push (cons 'type (org-split-string value splitre)) kwds))
  3016. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3017. ;; general TODO-like setup
  3018. (push (cons (intern (downcase (match-string 1 key)))
  3019. (org-split-string value splitre)) kwds))
  3020. ((equal key "TAGS")
  3021. (setq tags (append tags (if tags '("\\n") nil)
  3022. (org-split-string value splitre))))
  3023. ((equal key "COLUMNS")
  3024. (org-set-local 'org-columns-default-format value))
  3025. ((equal key "LINK")
  3026. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3027. (push (cons (match-string 1 value)
  3028. (org-trim (match-string 2 value)))
  3029. links)))
  3030. ((equal key "PRIORITIES")
  3031. (setq prio (org-split-string value " +")))
  3032. ((equal key "PROPERTY")
  3033. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3034. (push (cons (match-string 1 value) (match-string 2 value))
  3035. props)))
  3036. ((equal key "FILETAGS")
  3037. (when (string-match "\\S-" value)
  3038. (setq ftags
  3039. (append
  3040. ftags
  3041. (apply 'append
  3042. (mapcar (lambda (x) (org-split-string x ":"))
  3043. (org-split-string value)))))))
  3044. ((equal key "DRAWERS")
  3045. (setq drawers (org-split-string value splitre)))
  3046. ((equal key "CONSTANTS")
  3047. (setq const (append const (org-split-string value splitre))))
  3048. ((equal key "STARTUP")
  3049. (let ((opts (org-split-string value splitre))
  3050. l var val)
  3051. (while (setq l (pop opts))
  3052. (when (setq l (assoc l org-startup-options))
  3053. (setq var (nth 1 l) val (nth 2 l))
  3054. (if (not (nth 3 l))
  3055. (set (make-local-variable var) val)
  3056. (if (not (listp (symbol-value var)))
  3057. (set (make-local-variable var) nil))
  3058. (set (make-local-variable var) (symbol-value var))
  3059. (add-to-list var val))))))
  3060. ((equal key "ARCHIVE")
  3061. (string-match " *$" value)
  3062. (setq arch (replace-match "" t t value))
  3063. (remove-text-properties 0 (length arch)
  3064. '(face t fontified t) arch))
  3065. ((equal key "SETUPFILE")
  3066. (setq setup-contents (org-file-contents
  3067. (expand-file-name
  3068. (org-remove-double-quotes value))
  3069. 'noerror))
  3070. (if (not ext-setup-or-nil)
  3071. (setq ext-setup-or-nil setup-contents start 0)
  3072. (setq ext-setup-or-nil
  3073. (concat (substring ext-setup-or-nil 0 start)
  3074. "\n" setup-contents "\n"
  3075. (substring ext-setup-or-nil start)))))
  3076. ))))
  3077. (when cat
  3078. (org-set-local 'org-category (intern cat))
  3079. (push (cons "CATEGORY" cat) props))
  3080. (when prio
  3081. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3082. (setq prio (mapcar 'string-to-char prio))
  3083. (org-set-local 'org-highest-priority (nth 0 prio))
  3084. (org-set-local 'org-lowest-priority (nth 1 prio))
  3085. (org-set-local 'org-default-priority (nth 2 prio)))
  3086. (and props (org-set-local 'org-file-properties (nreverse props)))
  3087. (and ftags (org-set-local 'org-file-tags
  3088. (mapcar 'org-add-prop-inherited ftags)))
  3089. (and drawers (org-set-local 'org-drawers drawers))
  3090. (and arch (org-set-local 'org-archive-location arch))
  3091. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3092. ;; Process the TODO keywords
  3093. (unless kwds
  3094. ;; Use the global values as if they had been given locally.
  3095. (setq kwds (default-value 'org-todo-keywords))
  3096. (if (stringp (car kwds))
  3097. (setq kwds (list (cons org-todo-interpretation
  3098. (default-value 'org-todo-keywords)))))
  3099. (setq kwds (reverse kwds)))
  3100. (setq kwds (nreverse kwds))
  3101. (let (inter kws kw)
  3102. (while (setq kws (pop kwds))
  3103. (let ((kws (or
  3104. (run-hook-with-args-until-success
  3105. 'org-todo-setup-filter-hook kws)
  3106. kws)))
  3107. (setq inter (pop kws) sep (member "|" kws)
  3108. kws0 (delete "|" (copy-sequence kws))
  3109. kwsa nil
  3110. kws1 (mapcar
  3111. (lambda (x)
  3112. ;; 1 2
  3113. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3114. (progn
  3115. (setq kw (match-string 1 x)
  3116. key (and (match-end 2) (match-string 2 x))
  3117. log (org-extract-log-state-settings x))
  3118. (push (cons kw (and key (string-to-char key))) kwsa)
  3119. (and log (push log org-todo-log-states))
  3120. kw)
  3121. (error "Invalid TODO keyword %s" x)))
  3122. kws0)
  3123. kwsa (if kwsa (append '((:startgroup))
  3124. (nreverse kwsa)
  3125. '((:endgroup))))
  3126. hw (car kws1)
  3127. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3128. tail (list inter hw (car dws) (org-last dws))))
  3129. (add-to-list 'org-todo-heads hw 'append)
  3130. (push kws1 org-todo-sets)
  3131. (setq org-done-keywords (append org-done-keywords dws nil))
  3132. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3133. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3134. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3135. (setq org-todo-sets (nreverse org-todo-sets)
  3136. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3137. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3138. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3139. ;; Process the constants
  3140. (when const
  3141. (let (e cst)
  3142. (while (setq e (pop const))
  3143. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3144. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3145. (setq org-table-formula-constants-local cst)))
  3146. ;; Process the tags.
  3147. (when tags
  3148. (let (e tgs)
  3149. (while (setq e (pop tags))
  3150. (cond
  3151. ((equal e "{") (push '(:startgroup) tgs))
  3152. ((equal e "}") (push '(:endgroup) tgs))
  3153. ((equal e "\\n") (push '(:newline) tgs))
  3154. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3155. (push (cons (match-string 1 e)
  3156. (string-to-char (match-string 2 e)))
  3157. tgs))
  3158. (t (push (list e) tgs))))
  3159. (org-set-local 'org-tag-alist nil)
  3160. (while (setq e (pop tgs))
  3161. (or (and (stringp (car e))
  3162. (assoc (car e) org-tag-alist))
  3163. (push e org-tag-alist)))))
  3164. ;; Compute the regular expressions and other local variables
  3165. (if (not org-done-keywords)
  3166. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3167. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3168. (length org-scheduled-string)
  3169. (length org-clock-string)
  3170. (length org-closed-string)))
  3171. org-drawer-regexp
  3172. (concat "^[ \t]*:\\("
  3173. (mapconcat 'regexp-quote org-drawers "\\|")
  3174. "\\):[ \t]*$")
  3175. org-not-done-keywords
  3176. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3177. org-todo-regexp
  3178. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3179. "\\|") "\\)\\>")
  3180. org-not-done-regexp
  3181. (concat "\\<\\("
  3182. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3183. "\\)\\>")
  3184. org-not-done-heading-regexp
  3185. (concat "^\\(\\*+\\)[ \t]+\\("
  3186. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3187. "\\)\\>")
  3188. org-todo-line-regexp
  3189. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3190. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3191. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3192. org-complex-heading-regexp
  3193. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3194. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3195. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3196. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3197. org-nl-done-regexp
  3198. (concat "\n\\*+[ \t]+"
  3199. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3200. "\\)" "\\>")
  3201. org-todo-line-tags-regexp
  3202. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3203. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3204. (org-re
  3205. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3206. org-looking-at-done-regexp
  3207. (concat "^" "\\(?:"
  3208. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3209. "\\>")
  3210. org-deadline-regexp (concat "\\<" org-deadline-string)
  3211. org-deadline-time-regexp
  3212. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3213. org-deadline-line-regexp
  3214. (concat "\\<\\(" org-deadline-string "\\).*")
  3215. org-scheduled-regexp
  3216. (concat "\\<" org-scheduled-string)
  3217. org-scheduled-time-regexp
  3218. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3219. org-closed-time-regexp
  3220. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3221. org-keyword-time-regexp
  3222. (concat "\\<\\(" org-scheduled-string
  3223. "\\|" org-deadline-string
  3224. "\\|" org-closed-string
  3225. "\\|" org-clock-string "\\)"
  3226. " *[[<]\\([^]>]+\\)[]>]")
  3227. org-keyword-time-not-clock-regexp
  3228. (concat "\\<\\(" org-scheduled-string
  3229. "\\|" org-deadline-string
  3230. "\\|" org-closed-string
  3231. "\\)"
  3232. " *[[<]\\([^]>]+\\)[]>]")
  3233. org-maybe-keyword-time-regexp
  3234. (concat "\\(\\<\\(" org-scheduled-string
  3235. "\\|" org-deadline-string
  3236. "\\|" org-closed-string
  3237. "\\|" org-clock-string "\\)\\)?"
  3238. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3239. org-planning-or-clock-line-re
  3240. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3241. "\\|" org-deadline-string
  3242. "\\|" org-closed-string "\\|" org-clock-string
  3243. "\\)\\>\\)")
  3244. )
  3245. (org-compute-latex-and-specials-regexp)
  3246. (org-set-font-lock-defaults))))
  3247. (defun org-file-contents (file &optional noerror)
  3248. "Return the contents of FILE, as a string."
  3249. (if (or (not file)
  3250. (not (file-readable-p file)))
  3251. (if noerror
  3252. (progn
  3253. (message "Cannot read file %s" file)
  3254. (ding) (sit-for 2)
  3255. "")
  3256. (error "Cannot read file %s" file))
  3257. (with-temp-buffer
  3258. (insert-file-contents file)
  3259. (buffer-string))))
  3260. (defun org-extract-log-state-settings (x)
  3261. "Extract the log state setting from a TODO keyword string.
  3262. This will extract info from a string like \"WAIT(w@/!)\"."
  3263. (let (kw key log1 log2)
  3264. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3265. (setq kw (match-string 1 x)
  3266. key (and (match-end 2) (match-string 2 x))
  3267. log1 (and (match-end 3) (match-string 3 x))
  3268. log2 (and (match-end 4) (match-string 4 x)))
  3269. (and (or log1 log2)
  3270. (list kw
  3271. (and log1 (if (equal log1 "!") 'time 'note))
  3272. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3273. (defun org-remove-keyword-keys (list)
  3274. "Remove a pair of parenthesis at the end of each string in LIST."
  3275. (mapcar (lambda (x)
  3276. (if (string-match "(.*)$" x)
  3277. (substring x 0 (match-beginning 0))
  3278. x))
  3279. list))
  3280. ;; FIXME: this could be done much better, using second characters etc.
  3281. (defun org-assign-fast-keys (alist)
  3282. "Assign fast keys to a keyword-key alist.
  3283. Respect keys that are already there."
  3284. (let (new e k c c1 c2 (char ?a))
  3285. (while (setq e (pop alist))
  3286. (cond
  3287. ((equal e '(:startgroup)) (push e new))
  3288. ((equal e '(:endgroup)) (push e new))
  3289. ((equal e '(:newline)) (push e new))
  3290. (t
  3291. (setq k (car e) c2 nil)
  3292. (if (cdr e)
  3293. (setq c (cdr e))
  3294. ;; automatically assign a character.
  3295. (setq c1 (string-to-char
  3296. (downcase (substring
  3297. k (if (= (string-to-char k) ?@) 1 0)))))
  3298. (if (or (rassoc c1 new) (rassoc c1 alist))
  3299. (while (or (rassoc char new) (rassoc char alist))
  3300. (setq char (1+ char)))
  3301. (setq c2 c1))
  3302. (setq c (or c2 char)))
  3303. (push (cons k c) new))))
  3304. (nreverse new)))
  3305. ;;; Some variables used in various places
  3306. (defvar org-window-configuration nil
  3307. "Used in various places to store a window configuration.")
  3308. (defvar org-finish-function nil
  3309. "Function to be called when `C-c C-c' is used.
  3310. This is for getting out of special buffers like remember.")
  3311. ;; FIXME: Occasionally check by commenting these, to make sure
  3312. ;; no other functions uses these, forgetting to let-bind them.
  3313. (defvar entry)
  3314. (defvar state)
  3315. (defvar last-state)
  3316. (defvar date)
  3317. (defvar description)
  3318. ;; Defined somewhere in this file, but used before definition.
  3319. (defvar org-html-entities)
  3320. (defvar org-struct-menu)
  3321. (defvar org-org-menu)
  3322. (defvar org-tbl-menu)
  3323. (defvar org-agenda-keymap)
  3324. ;;;; Define the Org-mode
  3325. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3326. (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."))
  3327. ;; We use a before-change function to check if a table might need
  3328. ;; an update.
  3329. (defvar org-table-may-need-update t
  3330. "Indicates that a table might need an update.
  3331. This variable is set by `org-before-change-function'.
  3332. `org-table-align' sets it back to nil.")
  3333. (defun org-before-change-function (beg end)
  3334. "Every change indicates that a table might need an update."
  3335. (setq org-table-may-need-update t))
  3336. (defvar org-mode-map)
  3337. (defvar org-mode-hook nil
  3338. "Mode hook for Org-mode, run after the mode was turned on.")
  3339. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3340. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3341. (defvar org-table-buffer-is-an nil)
  3342. (defconst org-outline-regexp "\\*+ ")
  3343. ;;;###autoload
  3344. (define-derived-mode org-mode outline-mode "Org"
  3345. "Outline-based notes management and organizer, alias
  3346. \"Carsten's outline-mode for keeping track of everything.\"
  3347. Org-mode develops organizational tasks around a NOTES file which
  3348. contains information about projects as plain text. Org-mode is
  3349. implemented on top of outline-mode, which is ideal to keep the content
  3350. of large files well structured. It supports ToDo items, deadlines and
  3351. time stamps, which magically appear in the diary listing of the Emacs
  3352. calendar. Tables are easily created with a built-in table editor.
  3353. Plain text URL-like links connect to websites, emails (VM), Usenet
  3354. messages (Gnus), BBDB entries, and any files related to the project.
  3355. For printing and sharing of notes, an Org-mode file (or a part of it)
  3356. can be exported as a structured ASCII or HTML file.
  3357. The following commands are available:
  3358. \\{org-mode-map}"
  3359. ;; Get rid of Outline menus, they are not needed
  3360. ;; Need to do this here because define-derived-mode sets up
  3361. ;; the keymap so late. Still, it is a waste to call this each time
  3362. ;; we switch another buffer into org-mode.
  3363. (if (featurep 'xemacs)
  3364. (when (boundp 'outline-mode-menu-heading)
  3365. ;; Assume this is Greg's port, it used easymenu
  3366. (easy-menu-remove outline-mode-menu-heading)
  3367. (easy-menu-remove outline-mode-menu-show)
  3368. (easy-menu-remove outline-mode-menu-hide))
  3369. (define-key org-mode-map [menu-bar headings] 'undefined)
  3370. (define-key org-mode-map [menu-bar hide] 'undefined)
  3371. (define-key org-mode-map [menu-bar show] 'undefined))
  3372. (org-load-modules-maybe)
  3373. (easy-menu-add org-org-menu)
  3374. (easy-menu-add org-tbl-menu)
  3375. (org-install-agenda-files-menu)
  3376. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3377. (org-add-to-invisibility-spec '(org-cwidth))
  3378. (when (featurep 'xemacs)
  3379. (org-set-local 'line-move-ignore-invisible t))
  3380. (org-set-local 'outline-regexp org-outline-regexp)
  3381. (org-set-local 'outline-level 'org-outline-level)
  3382. (when (and org-ellipsis
  3383. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3384. (fboundp 'make-glyph-code))
  3385. (unless org-display-table
  3386. (setq org-display-table (make-display-table)))
  3387. (set-display-table-slot
  3388. org-display-table 4
  3389. (vconcat (mapcar
  3390. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3391. org-ellipsis)))
  3392. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3393. (setq buffer-display-table org-display-table))
  3394. (org-set-regexps-and-options)
  3395. (when (and org-tag-faces (not org-tags-special-faces-re))
  3396. ;; tag faces set outside customize.... force initialization.
  3397. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3398. ;; Calc embedded
  3399. (org-set-local 'calc-embedded-open-mode "# ")
  3400. (modify-syntax-entry ?# "<")
  3401. (modify-syntax-entry ?@ "w")
  3402. (if org-startup-truncated (setq truncate-lines t))
  3403. (org-set-local 'font-lock-unfontify-region-function
  3404. 'org-unfontify-region)
  3405. ;; Activate before-change-function
  3406. (org-set-local 'org-table-may-need-update t)
  3407. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3408. 'local)
  3409. ;; Check for running clock before killing a buffer
  3410. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3411. ;; Paragraphs and auto-filling
  3412. (org-set-autofill-regexps)
  3413. (setq indent-line-function 'org-indent-line-function)
  3414. (org-update-radio-target-regexp)
  3415. ;; Make sure dependence stuff works reliably, even for users who set it
  3416. ;; too late :-(
  3417. (if org-enforce-todo-dependencies
  3418. (add-hook 'org-blocker-hook
  3419. 'org-block-todo-from-children-or-siblings-or-parent)
  3420. (remove-hook 'org-blocker-hook
  3421. 'org-block-todo-from-children-or-siblings-or-parent))
  3422. (if org-enforce-todo-checkbox-dependencies
  3423. (add-hook 'org-blocker-hook
  3424. 'org-block-todo-from-checkboxes)
  3425. (remove-hook 'org-blocker-hook
  3426. 'org-block-todo-from-checkboxes))
  3427. ;; Comment characters
  3428. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3429. (org-set-local 'comment-padding " ")
  3430. ;; Align options lines
  3431. (org-set-local
  3432. 'align-mode-rules-list
  3433. '((org-in-buffer-settings
  3434. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3435. (modes . '(org-mode)))))
  3436. ;; Imenu
  3437. (org-set-local 'imenu-create-index-function
  3438. 'org-imenu-get-tree)
  3439. ;; Make isearch reveal context
  3440. (if (or (featurep 'xemacs)
  3441. (not (boundp 'outline-isearch-open-invisible-function)))
  3442. ;; Emacs 21 and XEmacs make use of the hook
  3443. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3444. ;; Emacs 22 deals with this through a special variable
  3445. (org-set-local 'outline-isearch-open-invisible-function
  3446. (lambda (&rest ignore) (org-show-context 'isearch))))
  3447. ;; If empty file that did not turn on org-mode automatically, make it to.
  3448. (if (and org-insert-mode-line-in-empty-file
  3449. (interactive-p)
  3450. (= (point-min) (point-max)))
  3451. (insert "# -*- mode: org -*-\n\n"))
  3452. (unless org-inhibit-startup
  3453. (when org-startup-align-all-tables
  3454. (let ((bmp (buffer-modified-p)))
  3455. (org-table-map-tables 'org-table-align)
  3456. (set-buffer-modified-p bmp)))
  3457. (org-set-startup-visibility)))
  3458. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3459. (defun org-current-time ()
  3460. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3461. (if (> (car org-time-stamp-rounding-minutes) 1)
  3462. (let ((r (car org-time-stamp-rounding-minutes))
  3463. (time (decode-time)))
  3464. (apply 'encode-time
  3465. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3466. (nthcdr 2 time))))
  3467. (current-time)))
  3468. ;;;; Font-Lock stuff, including the activators
  3469. (defvar org-mouse-map (make-sparse-keymap))
  3470. (org-defkey org-mouse-map
  3471. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3472. (org-defkey org-mouse-map
  3473. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3474. (when org-mouse-1-follows-link
  3475. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3476. (when org-tab-follows-link
  3477. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3478. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3479. (require 'font-lock)
  3480. (defconst org-non-link-chars "]\t\n\r<>")
  3481. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3482. "shell" "elisp"))
  3483. (defvar org-link-types-re nil
  3484. "Matches a link that has a url-like prefix like \"http:\"")
  3485. (defvar org-link-re-with-space nil
  3486. "Matches a link with spaces, optional angular brackets around it.")
  3487. (defvar org-link-re-with-space2 nil
  3488. "Matches a link with spaces, optional angular brackets around it.")
  3489. (defvar org-link-re-with-space3 nil
  3490. "Matches a link with spaces, only for internal part in bracket links.")
  3491. (defvar org-angle-link-re nil
  3492. "Matches link with angular brackets, spaces are allowed.")
  3493. (defvar org-plain-link-re nil
  3494. "Matches plain link, without spaces.")
  3495. (defvar org-bracket-link-regexp nil
  3496. "Matches a link in double brackets.")
  3497. (defvar org-bracket-link-analytic-regexp nil
  3498. "Regular expression used to analyze links.
  3499. Here is what the match groups contain after a match:
  3500. 1: http:
  3501. 2: http
  3502. 3: path
  3503. 4: [desc]
  3504. 5: desc")
  3505. (defvar org-bracket-link-analytic-regexp++ nil
  3506. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3507. (defvar org-any-link-re nil
  3508. "Regular expression matching any link.")
  3509. (defun org-make-link-regexps ()
  3510. "Update the link regular expressions.
  3511. This should be called after the variable `org-link-types' has changed."
  3512. (setq org-link-types-re
  3513. (concat
  3514. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3515. org-link-re-with-space
  3516. (concat
  3517. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3518. "\\([^" org-non-link-chars " ]"
  3519. "[^" org-non-link-chars "]*"
  3520. "[^" org-non-link-chars " ]\\)>?")
  3521. org-link-re-with-space2
  3522. (concat
  3523. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3524. "\\([^" org-non-link-chars " ]"
  3525. "[^\t\n\r]*"
  3526. "[^" org-non-link-chars " ]\\)>?")
  3527. org-link-re-with-space3
  3528. (concat
  3529. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3530. "\\([^" org-non-link-chars " ]"
  3531. "[^\t\n\r]*\\)")
  3532. org-angle-link-re
  3533. (concat
  3534. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3535. "\\([^" org-non-link-chars " ]"
  3536. "[^" org-non-link-chars "]*"
  3537. "\\)>")
  3538. org-plain-link-re
  3539. (concat
  3540. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3541. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3542. org-bracket-link-regexp
  3543. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3544. org-bracket-link-analytic-regexp
  3545. (concat
  3546. "\\[\\["
  3547. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3548. "\\([^]]+\\)"
  3549. "\\]"
  3550. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3551. "\\]")
  3552. org-bracket-link-analytic-regexp++
  3553. (concat
  3554. "\\[\\["
  3555. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3556. "\\([^]]+\\)"
  3557. "\\]"
  3558. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3559. "\\]")
  3560. org-any-link-re
  3561. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3562. org-angle-link-re "\\)\\|\\("
  3563. org-plain-link-re "\\)")))
  3564. (org-make-link-regexps)
  3565. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3566. "Regular expression for fast time stamp matching.")
  3567. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3568. "Regular expression for fast time stamp matching.")
  3569. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3570. "Regular expression matching time strings for analysis.
  3571. This one does not require the space after the date, so it can be used
  3572. on a string that terminates immediately after the date.")
  3573. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3574. "Regular expression matching time strings for analysis.")
  3575. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3576. "Regular expression matching time stamps, with groups.")
  3577. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3578. "Regular expression matching time stamps (also [..]), with groups.")
  3579. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3580. "Regular expression matching a time stamp range.")
  3581. (defconst org-tr-regexp-both
  3582. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3583. "Regular expression matching a time stamp range.")
  3584. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3585. org-ts-regexp "\\)?")
  3586. "Regular expression matching a time stamp or time stamp range.")
  3587. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3588. org-ts-regexp-both "\\)?")
  3589. "Regular expression matching a time stamp or time stamp range.
  3590. The time stamps may be either active or inactive.")
  3591. (defvar org-emph-face nil)
  3592. (defun org-do-emphasis-faces (limit)
  3593. "Run through the buffer and add overlays to links."
  3594. (let (rtn)
  3595. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3596. (if (not (= (char-after (match-beginning 3))
  3597. (char-after (match-beginning 4))))
  3598. (progn
  3599. (setq rtn t)
  3600. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3601. 'face
  3602. (nth 1 (assoc (match-string 3)
  3603. org-emphasis-alist)))
  3604. (add-text-properties (match-beginning 2) (match-end 2)
  3605. '(font-lock-multiline t))
  3606. (when org-hide-emphasis-markers
  3607. (add-text-properties (match-end 4) (match-beginning 5)
  3608. '(invisible org-link))
  3609. (add-text-properties (match-beginning 3) (match-end 3)
  3610. '(invisible org-link)))))
  3611. (backward-char 1))
  3612. rtn))
  3613. (defun org-emphasize (&optional char)
  3614. "Insert or change an emphasis, i.e. a font like bold or italic.
  3615. If there is an active region, change that region to a new emphasis.
  3616. If there is no region, just insert the marker characters and position
  3617. the cursor between them.
  3618. CHAR should be either the marker character, or the first character of the
  3619. HTML tag associated with that emphasis. If CHAR is a space, the means
  3620. to remove the emphasis of the selected region.
  3621. If char is not given (for example in an interactive call) it
  3622. will be prompted for."
  3623. (interactive)
  3624. (let ((eal org-emphasis-alist) e det
  3625. (erc org-emphasis-regexp-components)
  3626. (prompt "")
  3627. (string "") beg end move tag c s)
  3628. (if (org-region-active-p)
  3629. (setq beg (region-beginning) end (region-end)
  3630. string (buffer-substring beg end))
  3631. (setq move t))
  3632. (while (setq e (pop eal))
  3633. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3634. c (aref tag 0))
  3635. (push (cons c (string-to-char (car e))) det)
  3636. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3637. (substring tag 1)))))
  3638. (setq det (nreverse det))
  3639. (unless char
  3640. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3641. (setq char (read-char-exclusive)))
  3642. (setq char (or (cdr (assoc char det)) char))
  3643. (if (equal char ?\ )
  3644. (setq s "" move nil)
  3645. (unless (assoc (char-to-string char) org-emphasis-alist)
  3646. (error "No such emphasis marker: \"%c\"" char))
  3647. (setq s (char-to-string char)))
  3648. (while (and (> (length string) 1)
  3649. (equal (substring string 0 1) (substring string -1))
  3650. (assoc (substring string 0 1) org-emphasis-alist))
  3651. (setq string (substring string 1 -1)))
  3652. (setq string (concat s string s))
  3653. (if beg (delete-region beg end))
  3654. (unless (or (bolp)
  3655. (string-match (concat "[" (nth 0 erc) "\n]")
  3656. (char-to-string (char-before (point)))))
  3657. (insert " "))
  3658. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3659. (char-to-string (char-after (point))))
  3660. (insert " ") (backward-char 1))
  3661. (insert string)
  3662. (and move (backward-char 1))))
  3663. (defconst org-nonsticky-props
  3664. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3665. (defsubst org-rear-nonsticky-at (pos)
  3666. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3667. (defun org-activate-plain-links (limit)
  3668. "Run through the buffer and add overlays to links."
  3669. (catch 'exit
  3670. (let (f)
  3671. (while (re-search-forward org-plain-link-re limit t)
  3672. (setq f (get-text-property (match-beginning 0) 'face))
  3673. (if (or (eq f 'org-tag)
  3674. (and (listp f) (memq 'org-tag f)))
  3675. nil
  3676. (add-text-properties (match-beginning 0) (match-end 0)
  3677. (list 'mouse-face 'highlight
  3678. 'keymap org-mouse-map))
  3679. (org-rear-nonsticky-at (match-end 0))
  3680. (throw 'exit t))))))
  3681. (defun org-activate-code (limit)
  3682. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3683. (progn
  3684. (remove-text-properties (match-beginning 0) (match-end 0)
  3685. '(display t invisible t intangible t))
  3686. t)))
  3687. (defun org-activate-angle-links (limit)
  3688. "Run through the buffer and add overlays to links."
  3689. (if (re-search-forward org-angle-link-re limit t)
  3690. (progn
  3691. (add-text-properties (match-beginning 0) (match-end 0)
  3692. (list 'mouse-face 'highlight
  3693. 'keymap org-mouse-map))
  3694. (org-rear-nonsticky-at (match-end 0))
  3695. t)))
  3696. (defun org-activate-footnote-links (limit)
  3697. "Run through the buffer and add overlays to links."
  3698. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3699. limit t)
  3700. (progn
  3701. (add-text-properties (match-beginning 2) (match-end 2)
  3702. (list 'mouse-face 'highlight
  3703. 'keymap org-mouse-map
  3704. 'help-echo
  3705. (if (= (point-at-bol) (match-beginning 2))
  3706. "Footnote definition"
  3707. "Footnote reference")
  3708. ))
  3709. (org-rear-nonsticky-at (match-end 2))
  3710. t)))
  3711. (defun org-activate-bracket-links (limit)
  3712. "Run through the buffer and add overlays to bracketed links."
  3713. (if (re-search-forward org-bracket-link-regexp limit t)
  3714. (let* ((help (concat "LINK: "
  3715. (org-match-string-no-properties 1)))
  3716. ;; FIXME: above we should remove the escapes.
  3717. ;; but that requires another match, protecting match data,
  3718. ;; a lot of overhead for font-lock.
  3719. (ip (org-maybe-intangible
  3720. (list 'invisible 'org-link
  3721. 'keymap org-mouse-map 'mouse-face 'highlight
  3722. 'font-lock-multiline t 'help-echo help)))
  3723. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3724. 'font-lock-multiline t 'help-echo help)))
  3725. ;; We need to remove the invisible property here. Table narrowing
  3726. ;; may have made some of this invisible.
  3727. (remove-text-properties (match-beginning 0) (match-end 0)
  3728. '(invisible nil))
  3729. (if (match-end 3)
  3730. (progn
  3731. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3732. (org-rear-nonsticky-at (match-beginning 3))
  3733. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3734. (org-rear-nonsticky-at (match-end 3))
  3735. (add-text-properties (match-end 3) (match-end 0) ip)
  3736. (org-rear-nonsticky-at (match-end 0)))
  3737. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3738. (org-rear-nonsticky-at (match-beginning 1))
  3739. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3740. (org-rear-nonsticky-at (match-end 1))
  3741. (add-text-properties (match-end 1) (match-end 0) ip)
  3742. (org-rear-nonsticky-at (match-end 0)))
  3743. t)))
  3744. (defun org-activate-dates (limit)
  3745. "Run through the buffer and add overlays to dates."
  3746. (if (re-search-forward org-tsr-regexp-both limit t)
  3747. (progn
  3748. (add-text-properties (match-beginning 0) (match-end 0)
  3749. (list 'mouse-face 'highlight
  3750. 'keymap org-mouse-map))
  3751. (org-rear-nonsticky-at (match-end 0))
  3752. (when org-display-custom-times
  3753. (if (match-end 3)
  3754. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3755. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3756. t)))
  3757. (defvar org-target-link-regexp nil
  3758. "Regular expression matching radio targets in plain text.")
  3759. (make-variable-buffer-local 'org-target-link-regexp)
  3760. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3761. "Regular expression matching a link target.")
  3762. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3763. "Regular expression matching a radio target.")
  3764. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3765. "Regular expression matching any target.")
  3766. (defun org-activate-target-links (limit)
  3767. "Run through the buffer and add overlays to target matches."
  3768. (when org-target-link-regexp
  3769. (let ((case-fold-search t))
  3770. (if (re-search-forward org-target-link-regexp limit t)
  3771. (progn
  3772. (add-text-properties (match-beginning 0) (match-end 0)
  3773. (list 'mouse-face 'highlight
  3774. 'keymap org-mouse-map
  3775. 'help-echo "Radio target link"
  3776. 'org-linked-text t))
  3777. (org-rear-nonsticky-at (match-end 0))
  3778. t)))))
  3779. (defun org-update-radio-target-regexp ()
  3780. "Find all radio targets in this file and update the regular expression."
  3781. (interactive)
  3782. (when (memq 'radio org-activate-links)
  3783. (setq org-target-link-regexp
  3784. (org-make-target-link-regexp (org-all-targets 'radio)))
  3785. (org-restart-font-lock)))
  3786. (defun org-hide-wide-columns (limit)
  3787. (let (s e)
  3788. (setq s (text-property-any (point) (or limit (point-max))
  3789. 'org-cwidth t))
  3790. (when s
  3791. (setq e (next-single-property-change s 'org-cwidth))
  3792. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3793. (goto-char e)
  3794. t)))
  3795. (defvar org-latex-and-specials-regexp nil
  3796. "Regular expression for highlighting export special stuff.")
  3797. (defvar org-match-substring-regexp)
  3798. (defvar org-match-substring-with-braces-regexp)
  3799. (defvar org-export-html-special-string-regexps)
  3800. (defun org-compute-latex-and-specials-regexp ()
  3801. "Compute regular expression for stuff treated specially by exporters."
  3802. (if (not org-highlight-latex-fragments-and-specials)
  3803. (org-set-local 'org-latex-and-specials-regexp nil)
  3804. (require 'org-exp)
  3805. (let*
  3806. ((matchers (plist-get org-format-latex-options :matchers))
  3807. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3808. org-latex-regexps)))
  3809. (options (org-combine-plists (org-default-export-plist)
  3810. (org-infile-export-plist)))
  3811. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3812. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3813. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3814. (org-export-html-expand (plist-get options :expand-quoted-html))
  3815. (org-export-with-special-strings (plist-get options :special-strings))
  3816. (re-sub
  3817. (cond
  3818. ((equal org-export-with-sub-superscripts '{})
  3819. (list org-match-substring-with-braces-regexp))
  3820. (org-export-with-sub-superscripts
  3821. (list org-match-substring-regexp))
  3822. (t nil)))
  3823. (re-latex
  3824. (if org-export-with-LaTeX-fragments
  3825. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3826. (re-macros
  3827. (if org-export-with-TeX-macros
  3828. (list (concat "\\\\"
  3829. (regexp-opt
  3830. (append (mapcar 'car org-html-entities)
  3831. (if (boundp 'org-latex-entities)
  3832. org-latex-entities nil))
  3833. 'words))) ; FIXME
  3834. ))
  3835. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3836. (re-special (if org-export-with-special-strings
  3837. (mapcar (lambda (x) (car x))
  3838. org-export-html-special-string-regexps)))
  3839. (re-rest
  3840. (delq nil
  3841. (list
  3842. (if org-export-html-expand "@<[^>\n]+>")
  3843. ))))
  3844. (org-set-local
  3845. 'org-latex-and-specials-regexp
  3846. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3847. re-rest) "\\|")))))
  3848. (defun org-do-latex-and-special-faces (limit)
  3849. "Run through the buffer and add overlays to links."
  3850. (when org-latex-and-specials-regexp
  3851. (let (rtn d)
  3852. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3853. limit t))
  3854. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3855. 'face))
  3856. '(org-code org-verbatim underline)))
  3857. (progn
  3858. (setq rtn t
  3859. d (cond ((member (char-after (1+ (match-beginning 0)))
  3860. '(?_ ?^)) 1)
  3861. (t 0)))
  3862. (font-lock-prepend-text-property
  3863. (+ d (match-beginning 0)) (match-end 0)
  3864. 'face 'org-latex-and-export-specials)
  3865. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3866. '(font-lock-multiline t)))))
  3867. rtn)))
  3868. (defun org-restart-font-lock ()
  3869. "Restart font-lock-mode, to force refontification."
  3870. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3871. (font-lock-mode -1)
  3872. (font-lock-mode 1)))
  3873. (defun org-all-targets (&optional radio)
  3874. "Return a list of all targets in this file.
  3875. With optional argument RADIO, only find radio targets."
  3876. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3877. rtn)
  3878. (save-excursion
  3879. (goto-char (point-min))
  3880. (while (re-search-forward re nil t)
  3881. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3882. rtn)))
  3883. (defun org-make-target-link-regexp (targets)
  3884. "Make regular expression matching all strings in TARGETS.
  3885. The regular expression finds the targets also if there is a line break
  3886. between words."
  3887. (and targets
  3888. (concat
  3889. "\\<\\("
  3890. (mapconcat
  3891. (lambda (x)
  3892. (while (string-match " +" x)
  3893. (setq x (replace-match "\\s-+" t t x)))
  3894. x)
  3895. targets
  3896. "\\|")
  3897. "\\)\\>")))
  3898. (defun org-activate-tags (limit)
  3899. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3900. (progn
  3901. (add-text-properties (match-beginning 1) (match-end 1)
  3902. (list 'mouse-face 'highlight
  3903. 'keymap org-mouse-map))
  3904. (org-rear-nonsticky-at (match-end 1))
  3905. t)))
  3906. (defun org-outline-level ()
  3907. (save-excursion
  3908. (looking-at outline-regexp)
  3909. (if (match-beginning 1)
  3910. (+ (org-get-string-indentation (match-string 1)) 1000)
  3911. (1- (- (match-end 0) (match-beginning 0))))))
  3912. (defvar org-font-lock-keywords nil)
  3913. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3914. "Regular expression matching a property line.")
  3915. (defvar org-font-lock-hook nil
  3916. "Functions to be called for special font lock stuff.")
  3917. (defun org-font-lock-hook (limit)
  3918. (run-hook-with-args 'org-font-lock-hook limit))
  3919. (defun org-set-font-lock-defaults ()
  3920. (let* ((em org-fontify-emphasized-text)
  3921. (lk org-activate-links)
  3922. (org-font-lock-extra-keywords
  3923. (list
  3924. ;; Call the hook
  3925. '(org-font-lock-hook)
  3926. ;; Headlines
  3927. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3928. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3929. ;; Table lines
  3930. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3931. (1 'org-table t))
  3932. ;; Table internals
  3933. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3934. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3935. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3936. ;; Drawers
  3937. (list org-drawer-regexp '(0 'org-special-keyword t))
  3938. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3939. ;; Properties
  3940. (list org-property-re
  3941. '(1 'org-special-keyword t)
  3942. '(3 'org-property-value t))
  3943. (if org-format-transports-properties-p
  3944. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3945. ;; Links
  3946. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3947. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3948. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3949. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3950. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3951. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3952. (if (memq 'footnote lk) '(org-activate-footnote-links
  3953. (2 'org-footnote t)))
  3954. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3955. '(org-hide-wide-columns (0 nil append))
  3956. ;; TODO lines
  3957. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  3958. '(1 (org-get-todo-face 1) t))
  3959. ;; DONE
  3960. (if org-fontify-done-headline
  3961. (list (concat "^[*]+ +\\<\\("
  3962. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3963. "\\)\\(.*\\)")
  3964. '(2 'org-headline-done t))
  3965. nil)
  3966. ;; Priorities
  3967. '(org-font-lock-add-priority-faces)
  3968. ;; Tags
  3969. '(org-font-lock-add-tag-faces)
  3970. ;; Special keywords
  3971. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3972. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3973. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3974. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3975. ;; Emphasis
  3976. (if em
  3977. (if (featurep 'xemacs)
  3978. '(org-do-emphasis-faces (0 nil append))
  3979. '(org-do-emphasis-faces)))
  3980. ;; Checkboxes
  3981. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3982. 2 'org-checkbox prepend)
  3983. (if org-provide-checkbox-statistics
  3984. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3985. (0 (org-get-checkbox-statistics-face) t)))
  3986. ;; Description list items
  3987. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3988. 2 'bold prepend)
  3989. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3990. '(1 'org-archived prepend))
  3991. ;; Specials
  3992. '(org-do-latex-and-special-faces)
  3993. ;; Code
  3994. '(org-activate-code (1 'org-code t))
  3995. ;; COMMENT
  3996. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3997. "\\|" org-quote-string "\\)\\>")
  3998. '(1 'org-special-keyword t))
  3999. '("^#.*" (0 'font-lock-comment-face t))
  4000. )))
  4001. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4002. ;; Now set the full font-lock-keywords
  4003. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4004. (org-set-local 'font-lock-defaults
  4005. '(org-font-lock-keywords t nil nil backward-paragraph))
  4006. (kill-local-variable 'font-lock-keywords) nil))
  4007. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4008. "Fontify string S like in Org-mode"
  4009. (with-temp-buffer
  4010. (insert s)
  4011. (let ((org-odd-levels-only odd-levels))
  4012. (org-mode)
  4013. (font-lock-fontify-buffer)
  4014. (buffer-string))))
  4015. (defvar org-m nil)
  4016. (defvar org-l nil)
  4017. (defvar org-f nil)
  4018. (defun org-get-level-face (n)
  4019. "Get the right face for match N in font-lock matching of headlines."
  4020. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4021. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4022. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4023. (cond
  4024. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4025. ((eq n 2) org-f)
  4026. (t (if org-level-color-stars-only nil org-f))))
  4027. (defun org-get-todo-face (kwd)
  4028. "Get the right face for a TODO keyword KWD.
  4029. If KWD is a number, get the corresponding match group."
  4030. (if (numberp kwd) (setq kwd (match-string kwd)))
  4031. (or (cdr (assoc kwd org-todo-keyword-faces))
  4032. (and (member kwd org-done-keywords) 'org-done)
  4033. 'org-todo))
  4034. (defun org-font-lock-add-tag-faces (limit)
  4035. "Add the special tag faces."
  4036. (when (and org-tag-faces org-tags-special-faces-re)
  4037. (while (re-search-forward org-tags-special-faces-re limit t)
  4038. (add-text-properties (match-beginning 1) (match-end 1)
  4039. (list 'face (org-get-tag-face 1)
  4040. 'font-lock-fontified t))
  4041. (backward-char 1))))
  4042. (defun org-font-lock-add-priority-faces (limit)
  4043. "Add the special priority faces."
  4044. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4045. (add-text-properties
  4046. (match-beginning 0) (match-end 0)
  4047. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4048. org-priority-faces))
  4049. 'org-special-keyword)
  4050. 'font-lock-fontified t))))
  4051. (defun org-get-tag-face (kwd)
  4052. "Get the right face for a TODO keyword KWD.
  4053. If KWD is a number, get the corresponding match group."
  4054. (if (numberp kwd) (setq kwd (match-string kwd)))
  4055. (or (cdr (assoc kwd org-tag-faces))
  4056. 'org-tag))
  4057. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4058. "Remove fontification and activation overlays from links."
  4059. (font-lock-default-unfontify-region beg end)
  4060. (let* ((buffer-undo-list t)
  4061. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4062. (inhibit-modification-hooks t)
  4063. deactivate-mark buffer-file-name buffer-file-truename)
  4064. (remove-text-properties beg end
  4065. '(mouse-face t keymap t org-linked-text t
  4066. invisible t intangible t))))
  4067. ;;;; Visibility cycling, including org-goto and indirect buffer
  4068. ;;; Cycling
  4069. (defvar org-cycle-global-status nil)
  4070. (make-variable-buffer-local 'org-cycle-global-status)
  4071. (defvar org-cycle-subtree-status nil)
  4072. (make-variable-buffer-local 'org-cycle-subtree-status)
  4073. ;;;###autoload
  4074. (defvar org-inlinetask-min-level)
  4075. (defun org-cycle (&optional arg)
  4076. "TAB-action and visibility cycling for Org-mode.
  4077. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4078. is outine visibility cycling, but it also invokes other actions
  4079. in special contexts.
  4080. - When this function is called with a prefix argument, rotate the entire
  4081. buffer through 3 states (global cycling)
  4082. 1. OVERVIEW: Show only top-level headlines.
  4083. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4084. 3. SHOW ALL: Show everything.
  4085. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4086. determined by the variable `org-startup-folded', and by any VISIBILITY
  4087. properties in the buffer.
  4088. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4089. including any drawers.
  4090. - When inside a table, re-align the table and move to the next field.
  4091. - When point is at the beginning of a headline, rotate the subtree started
  4092. by this line through 3 different states (local cycling)
  4093. 1. FOLDED: Only the main headline is shown.
  4094. 2. CHILDREN: The main headline and the direct children are shown.
  4095. From this state, you can move to one of the children
  4096. and zoom in further.
  4097. 3. SUBTREE: Show the entire subtree, including body text.
  4098. - When there is a numeric prefix, go up to a heading with level ARG, do
  4099. a `show-subtree' and return to the previous cursor position. If ARG
  4100. is negative, go up that many levels.
  4101. - When point is not at the beginning of a headline, execute the global
  4102. binding for TAB, which is re-indenting the line. See the option
  4103. `org-cycle-emulate-tab' for details.
  4104. - Special case: if point is at the beginning of the buffer and there is
  4105. no headline in line 1, this function will act as if called with prefix arg.
  4106. But only if also the variable `org-cycle-global-at-bob' is t."
  4107. (interactive "P")
  4108. (org-load-modules-maybe)
  4109. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4110. (let* ((limit-level
  4111. (or org-cycle-max-level
  4112. (and (boundp 'org-inlinetask-min-level)
  4113. org-inlinetask-min-level
  4114. (1- org-inlinetask-min-level))))
  4115. (nstars (and limit-level
  4116. (if org-odd-levels-only
  4117. (and limit-level (1- (* limit-level 2)))
  4118. limit-level)))
  4119. (outline-regexp
  4120. (cond
  4121. ((not (org-mode-p)) outline-regexp)
  4122. (org-cycle-include-plain-lists
  4123. (concat "\\(?:\\*"
  4124. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4125. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4126. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4127. (bob-special (and org-cycle-global-at-bob (bobp)
  4128. (not (looking-at outline-regexp))))
  4129. (org-cycle-hook
  4130. (if bob-special
  4131. (delq 'org-optimize-window-after-visibility-change
  4132. (copy-sequence org-cycle-hook))
  4133. org-cycle-hook))
  4134. (pos (point)))
  4135. (if (or bob-special (equal arg '(4)))
  4136. ;; special case: use global cycling
  4137. (setq arg t))
  4138. (cond
  4139. ((equal arg '(16))
  4140. (org-set-startup-visibility)
  4141. (message "Startup visibility, plus VISIBILITY properties"))
  4142. ((equal arg '(64))
  4143. (show-all)
  4144. (message "Entire buffer visible, including drawers"))
  4145. ((org-at-table-p 'any)
  4146. ;; Enter the table or move to the next field in the table
  4147. (or (org-table-recognize-table.el)
  4148. (progn
  4149. (if arg (org-table-edit-field t)
  4150. (org-table-justify-field-maybe)
  4151. (call-interactively 'org-table-next-field)))))
  4152. ((run-hook-with-args-until-success
  4153. 'org-tab-after-check-for-table-hook))
  4154. ((eq arg t) ;; Global cycling
  4155. (org-cycle-internal-global))
  4156. ((and org-drawers org-drawer-regexp
  4157. (save-excursion
  4158. (beginning-of-line 1)
  4159. (looking-at org-drawer-regexp)))
  4160. ;; Toggle block visibility
  4161. (org-flag-drawer
  4162. (not (get-char-property (match-end 0) 'invisible))))
  4163. ((integerp arg)
  4164. ;; Show-subtree, ARG levels up from here.
  4165. (save-excursion
  4166. (org-back-to-heading)
  4167. (outline-up-heading (if (< arg 0) (- arg)
  4168. (- (funcall outline-level) arg)))
  4169. (org-show-subtree)))
  4170. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4171. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4172. (org-cycle-internal-local))
  4173. ;; TAB emulation and template completion
  4174. (buffer-read-only (org-back-to-heading))
  4175. ((run-hook-with-args-until-success
  4176. 'org-tab-after-check-for-cycling-hook))
  4177. ((org-try-structure-completion))
  4178. ((org-try-cdlatex-tab))
  4179. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4180. (or (not (bolp))
  4181. (not (looking-at outline-regexp))))
  4182. (call-interactively (global-key-binding "\t")))
  4183. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4184. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4185. (or (and (eq org-cycle-emulate-tab 'white)
  4186. (= (match-end 0) (point-at-eol)))
  4187. (and (eq org-cycle-emulate-tab 'whitestart)
  4188. (>= (match-end 0) pos))))
  4189. t
  4190. (eq org-cycle-emulate-tab t))
  4191. (call-interactively (global-key-binding "\t")))
  4192. (t (save-excursion
  4193. (org-back-to-heading)
  4194. (org-cycle)))))))
  4195. (defun org-cycle-internal-global ()
  4196. "Do the global cycling action."
  4197. (cond
  4198. ((and (eq last-command this-command)
  4199. (eq org-cycle-global-status 'overview))
  4200. ;; We just created the overview - now do table of contents
  4201. ;; This can be slow in very large buffers, so indicate action
  4202. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4203. (message "CONTENTS...")
  4204. (org-content)
  4205. (message "CONTENTS...done")
  4206. (setq org-cycle-global-status 'contents)
  4207. (run-hook-with-args 'org-cycle-hook 'contents))
  4208. ((and (eq last-command this-command)
  4209. (eq org-cycle-global-status 'contents))
  4210. ;; We just showed the table of contents - now show everything
  4211. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4212. (show-all)
  4213. (message "SHOW ALL")
  4214. (setq org-cycle-global-status 'all)
  4215. (run-hook-with-args 'org-cycle-hook 'all))
  4216. (t
  4217. ;; Default action: go to overview
  4218. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4219. (org-overview)
  4220. (message "OVERVIEW")
  4221. (setq org-cycle-global-status 'overview)
  4222. (run-hook-with-args 'org-cycle-hook 'overview))))
  4223. (defun org-cycle-internal-local ()
  4224. "Do the local cycling action."
  4225. (org-back-to-heading)
  4226. (let ((goal-column 0) eoh eol eos)
  4227. ;; First, some boundaries
  4228. (save-excursion
  4229. (org-back-to-heading)
  4230. (save-excursion
  4231. (beginning-of-line 2)
  4232. (while (and (not (eobp)) ;; this is like `next-line'
  4233. (get-char-property (1- (point)) 'invisible))
  4234. (beginning-of-line 2)) (setq eol (point)))
  4235. (outline-end-of-heading) (setq eoh (point))
  4236. (org-end-of-subtree t)
  4237. (unless (eobp)
  4238. (skip-chars-forward " \t\n")
  4239. (beginning-of-line 1) ; in case this is an item
  4240. )
  4241. (setq eos (1- (point))))
  4242. ;; Find out what to do next and set `this-command'
  4243. (cond
  4244. ((= eos eoh)
  4245. ;; Nothing is hidden behind this heading
  4246. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4247. (message "EMPTY ENTRY")
  4248. (setq org-cycle-subtree-status nil)
  4249. (save-excursion
  4250. (goto-char eos)
  4251. (outline-next-heading)
  4252. (if (org-invisible-p) (org-flag-heading nil))))
  4253. ((or (>= eol eos)
  4254. (not (string-match "\\S-" (buffer-substring eol eos))))
  4255. ;; Entire subtree is hidden in one line: open it
  4256. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4257. (org-show-entry)
  4258. (show-children)
  4259. (message "CHILDREN")
  4260. (save-excursion
  4261. (goto-char eos)
  4262. (outline-next-heading)
  4263. (if (org-invisible-p) (org-flag-heading nil)))
  4264. (setq org-cycle-subtree-status 'children)
  4265. (run-hook-with-args 'org-cycle-hook 'children))
  4266. ((and (eq last-command this-command)
  4267. (eq org-cycle-subtree-status 'children))
  4268. ;; We just showed the children, now show everything.
  4269. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4270. (org-show-subtree)
  4271. (message "SUBTREE")
  4272. (setq org-cycle-subtree-status 'subtree)
  4273. (run-hook-with-args 'org-cycle-hook 'subtree))
  4274. (t
  4275. ;; Default action: hide the subtree.
  4276. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4277. (hide-subtree)
  4278. (message "FOLDED")
  4279. (setq org-cycle-subtree-status 'folded)
  4280. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4281. ;;;###autoload
  4282. (defun org-global-cycle (&optional arg)
  4283. "Cycle the global visibility. For details see `org-cycle'.
  4284. With C-u prefix arg, switch to startup visibility.
  4285. With a numeric prefix, show all headlines up to that level."
  4286. (interactive "P")
  4287. (let ((org-cycle-include-plain-lists
  4288. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4289. (cond
  4290. ((integerp arg)
  4291. (show-all)
  4292. (hide-sublevels arg)
  4293. (setq org-cycle-global-status 'contents))
  4294. ((equal arg '(4))
  4295. (org-set-startup-visibility)
  4296. (message "Startup visibility, plus VISIBILITY properties."))
  4297. (t
  4298. (org-cycle '(4))))))
  4299. (defun org-set-startup-visibility ()
  4300. "Set the visibility required by startup options and properties."
  4301. (cond
  4302. ((eq org-startup-folded t)
  4303. (org-cycle '(4)))
  4304. ((eq org-startup-folded 'content)
  4305. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4306. (org-cycle '(4)) (org-cycle '(4)))))
  4307. (org-set-visibility-according-to-property 'no-cleanup)
  4308. (org-cycle-hide-archived-subtrees 'all)
  4309. (org-cycle-hide-drawers 'all)
  4310. (org-cycle-show-empty-lines 'all))
  4311. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4312. "Switch subtree visibilities according to :VISIBILITY: property."
  4313. (interactive)
  4314. (let (org-show-entry-below state)
  4315. (save-excursion
  4316. (goto-char (point-min))
  4317. (while (re-search-forward
  4318. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4319. nil t)
  4320. (setq state (match-string 1))
  4321. (save-excursion
  4322. (org-back-to-heading t)
  4323. (hide-subtree)
  4324. (org-reveal)
  4325. (cond
  4326. ((equal state '("fold" "folded"))
  4327. (hide-subtree))
  4328. ((equal state "children")
  4329. (org-show-hidden-entry)
  4330. (show-children))
  4331. ((equal state "content")
  4332. (save-excursion
  4333. (save-restriction
  4334. (org-narrow-to-subtree)
  4335. (org-content))))
  4336. ((member state '("all" "showall"))
  4337. (show-subtree)))))
  4338. (unless no-cleanup
  4339. (org-cycle-hide-archived-subtrees 'all)
  4340. (org-cycle-hide-drawers 'all)
  4341. (org-cycle-show-empty-lines 'all)))))
  4342. (defun org-overview ()
  4343. "Switch to overview mode, showing only top-level headlines.
  4344. Really, this shows all headlines with level equal or greater than the level
  4345. of the first headline in the buffer. This is important, because if the
  4346. first headline is not level one, then (hide-sublevels 1) gives confusing
  4347. results."
  4348. (interactive)
  4349. (let ((level (save-excursion
  4350. (goto-char (point-min))
  4351. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4352. (progn
  4353. (goto-char (match-beginning 0))
  4354. (funcall outline-level))))))
  4355. (and level (hide-sublevels level))))
  4356. (defun org-content (&optional arg)
  4357. "Show all headlines in the buffer, like a table of contents.
  4358. With numerical argument N, show content up to level N."
  4359. (interactive "P")
  4360. (save-excursion
  4361. ;; Visit all headings and show their offspring
  4362. (and (integerp arg) (org-overview))
  4363. (goto-char (point-max))
  4364. (catch 'exit
  4365. (while (and (progn (condition-case nil
  4366. (outline-previous-visible-heading 1)
  4367. (error (goto-char (point-min))))
  4368. t)
  4369. (looking-at outline-regexp))
  4370. (if (integerp arg)
  4371. (show-children (1- arg))
  4372. (show-branches))
  4373. (if (bobp) (throw 'exit nil))))))
  4374. (defun org-optimize-window-after-visibility-change (state)
  4375. "Adjust the window after a change in outline visibility.
  4376. This function is the default value of the hook `org-cycle-hook'."
  4377. (when (get-buffer-window (current-buffer))
  4378. (cond
  4379. ((eq state 'content) nil)
  4380. ((eq state 'all) nil)
  4381. ((eq state 'folded) nil)
  4382. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4383. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4384. (defun org-compact-display-after-subtree-move ()
  4385. "Show a compacter version of the tree of the entry's parent."
  4386. (save-excursion
  4387. (if (org-up-heading-safe)
  4388. (progn
  4389. (hide-subtree)
  4390. (show-entry)
  4391. (show-children)
  4392. (org-cycle-show-empty-lines 'children)
  4393. (org-cycle-hide-drawers 'children))
  4394. (org-overview))))
  4395. (defun org-cycle-show-empty-lines (state)
  4396. "Show empty lines above all visible headlines.
  4397. The region to be covered depends on STATE when called through
  4398. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4399. entire buffer covered. Note that an empty line is only shown if there
  4400. are at least `org-cycle-separator-lines' empty lines before the headline."
  4401. (when (> org-cycle-separator-lines 0)
  4402. (save-excursion
  4403. (let* ((n org-cycle-separator-lines)
  4404. (re (cond
  4405. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4406. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4407. (t (let ((ns (number-to-string (- n 2))))
  4408. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4409. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4410. beg end)
  4411. (cond
  4412. ((memq state '(overview contents t))
  4413. (setq beg (point-min) end (point-max)))
  4414. ((memq state '(children folded))
  4415. (setq beg (point) end (progn (org-end-of-subtree t t)
  4416. (beginning-of-line 2)
  4417. (point)))))
  4418. (when beg
  4419. (goto-char beg)
  4420. (while (re-search-forward re end t)
  4421. (if (not (get-char-property (match-end 1) 'invisible))
  4422. (outline-flag-region
  4423. (match-beginning 1) (match-end 1) nil)))))))
  4424. ;; Never hide empty lines at the end of the file.
  4425. (save-excursion
  4426. (goto-char (point-max))
  4427. (outline-previous-heading)
  4428. (outline-end-of-heading)
  4429. (if (and (looking-at "[ \t\n]+")
  4430. (= (match-end 0) (point-max)))
  4431. (outline-flag-region (point) (match-end 0) nil))))
  4432. (defun org-show-empty-lines-in-parent ()
  4433. "Move to the parent and re-show empty lines before visible headlines."
  4434. (save-excursion
  4435. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4436. (org-cycle-show-empty-lines context))))
  4437. (defun org-cycle-hide-drawers (state)
  4438. "Re-hide all drawers after a visibility state change."
  4439. (when (and (org-mode-p)
  4440. (not (memq state '(overview folded))))
  4441. (save-excursion
  4442. (let* ((globalp (memq state '(contents all)))
  4443. (beg (if globalp (point-min) (point)))
  4444. (end (if globalp (point-max) (org-end-of-subtree t))))
  4445. (goto-char beg)
  4446. (while (re-search-forward org-drawer-regexp end t)
  4447. (org-flag-drawer t))))))
  4448. (defun org-flag-drawer (flag)
  4449. (save-excursion
  4450. (beginning-of-line 1)
  4451. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4452. (let ((b (match-end 0))
  4453. (outline-regexp org-outline-regexp))
  4454. (if (re-search-forward
  4455. "^[ \t]*:END:"
  4456. (save-excursion (outline-next-heading) (point)) t)
  4457. (outline-flag-region b (point-at-eol) flag)
  4458. (error ":END: line missing"))))))
  4459. (defun org-subtree-end-visible-p ()
  4460. "Is the end of the current subtree visible?"
  4461. (pos-visible-in-window-p
  4462. (save-excursion (org-end-of-subtree t) (point))))
  4463. (defun org-first-headline-recenter (&optional N)
  4464. "Move cursor to the first headline and recenter the headline.
  4465. Optional argument N means, put the headline into the Nth line of the window."
  4466. (goto-char (point-min))
  4467. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4468. (beginning-of-line)
  4469. (recenter (prefix-numeric-value N))))
  4470. ;;; Org-goto
  4471. (defvar org-goto-window-configuration nil)
  4472. (defvar org-goto-marker nil)
  4473. (defvar org-goto-map
  4474. (let ((map (make-sparse-keymap)))
  4475. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4476. (while (setq cmd (pop cmds))
  4477. (substitute-key-definition cmd cmd map global-map)))
  4478. (suppress-keymap map)
  4479. (org-defkey map "\C-m" 'org-goto-ret)
  4480. (org-defkey map [(return)] 'org-goto-ret)
  4481. (org-defkey map [(left)] 'org-goto-left)
  4482. (org-defkey map [(right)] 'org-goto-right)
  4483. (org-defkey map [(control ?g)] 'org-goto-quit)
  4484. (org-defkey map "\C-i" 'org-cycle)
  4485. (org-defkey map [(tab)] 'org-cycle)
  4486. (org-defkey map [(down)] 'outline-next-visible-heading)
  4487. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4488. (if org-goto-auto-isearch
  4489. (if (fboundp 'define-key-after)
  4490. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4491. nil)
  4492. (org-defkey map "q" 'org-goto-quit)
  4493. (org-defkey map "n" 'outline-next-visible-heading)
  4494. (org-defkey map "p" 'outline-previous-visible-heading)
  4495. (org-defkey map "f" 'outline-forward-same-level)
  4496. (org-defkey map "b" 'outline-backward-same-level)
  4497. (org-defkey map "u" 'outline-up-heading))
  4498. (org-defkey map "/" 'org-occur)
  4499. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4500. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4501. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4502. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4503. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4504. map))
  4505. (defconst org-goto-help
  4506. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4507. RET=jump to location [Q]uit and return to previous location
  4508. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4509. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4510. ;; FIXME: Docstring doe not mention both interfaces
  4511. (defun org-goto (&optional alternative-interface)
  4512. "Look up a different location in the current file, keeping current visibility.
  4513. When you want look-up or go to a different location in a document, the
  4514. fastest way is often to fold the entire buffer and then dive into the tree.
  4515. This method has the disadvantage, that the previous location will be folded,
  4516. which may not be what you want.
  4517. This command works around this by showing a copy of the current buffer
  4518. in an indirect buffer, in overview mode. You can dive into the tree in
  4519. that copy, use org-occur and incremental search to find a location.
  4520. When pressing RET or `Q', the command returns to the original buffer in
  4521. which the visibility is still unchanged. After RET is will also jump to
  4522. the location selected in the indirect buffer and expose the
  4523. the headline hierarchy above."
  4524. (interactive "P")
  4525. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4526. (org-refile-use-outline-path t)
  4527. (org-refile-target-verify-function nil)
  4528. (interface
  4529. (if (not alternative-interface)
  4530. org-goto-interface
  4531. (if (eq org-goto-interface 'outline)
  4532. 'outline-path-completion
  4533. 'outline)))
  4534. (org-goto-start-pos (point))
  4535. (selected-point
  4536. (if (eq interface 'outline)
  4537. (car (org-get-location (current-buffer) org-goto-help))
  4538. (nth 3 (org-refile-get-location "Goto: ")))))
  4539. (if selected-point
  4540. (progn
  4541. (org-mark-ring-push org-goto-start-pos)
  4542. (goto-char selected-point)
  4543. (if (or (org-invisible-p) (org-invisible-p2))
  4544. (org-show-context 'org-goto)))
  4545. (message "Quit"))))
  4546. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4547. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4548. (defvar org-goto-local-auto-isearch-map) ; defined below
  4549. (defun org-get-location (buf help)
  4550. "Let the user select a location in the Org-mode buffer BUF.
  4551. This function uses a recursive edit. It returns the selected position
  4552. or nil."
  4553. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4554. (isearch-hide-immediately nil)
  4555. (isearch-search-fun-function
  4556. (lambda () 'org-goto-local-search-headings))
  4557. (org-goto-selected-point org-goto-exit-command))
  4558. (save-excursion
  4559. (save-window-excursion
  4560. (delete-other-windows)
  4561. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4562. (switch-to-buffer
  4563. (condition-case nil
  4564. (make-indirect-buffer (current-buffer) "*org-goto*")
  4565. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4566. (with-output-to-temp-buffer "*Help*"
  4567. (princ help))
  4568. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4569. (setq buffer-read-only nil)
  4570. (let ((org-startup-truncated t)
  4571. (org-startup-folded nil)
  4572. (org-startup-align-all-tables nil))
  4573. (org-mode)
  4574. (org-overview))
  4575. (setq buffer-read-only t)
  4576. (if (and (boundp 'org-goto-start-pos)
  4577. (integer-or-marker-p org-goto-start-pos))
  4578. (let ((org-show-hierarchy-above t)
  4579. (org-show-siblings t)
  4580. (org-show-following-heading t))
  4581. (goto-char org-goto-start-pos)
  4582. (and (org-invisible-p) (org-show-context)))
  4583. (goto-char (point-min)))
  4584. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4585. (message "Select location and press RET")
  4586. (use-local-map org-goto-map)
  4587. (recursive-edit)
  4588. ))
  4589. (kill-buffer "*org-goto*")
  4590. (cons org-goto-selected-point org-goto-exit-command)))
  4591. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4592. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4593. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4594. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4595. (defun org-goto-local-search-headings (string bound noerror)
  4596. "Search and make sure that any matches are in headlines."
  4597. (catch 'return
  4598. (while (if isearch-forward
  4599. (search-forward string bound noerror)
  4600. (search-backward string bound noerror))
  4601. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4602. (and (member :headline context)
  4603. (not (member :tags context))))
  4604. (throw 'return (point))))))
  4605. (defun org-goto-local-auto-isearch ()
  4606. "Start isearch."
  4607. (interactive)
  4608. (goto-char (point-min))
  4609. (let ((keys (this-command-keys)))
  4610. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4611. (isearch-mode t)
  4612. (isearch-process-search-char (string-to-char keys)))))
  4613. (defun org-goto-ret (&optional arg)
  4614. "Finish `org-goto' by going to the new location."
  4615. (interactive "P")
  4616. (setq org-goto-selected-point (point)
  4617. org-goto-exit-command 'return)
  4618. (throw 'exit nil))
  4619. (defun org-goto-left ()
  4620. "Finish `org-goto' by going to the new location."
  4621. (interactive)
  4622. (if (org-on-heading-p)
  4623. (progn
  4624. (beginning-of-line 1)
  4625. (setq org-goto-selected-point (point)
  4626. org-goto-exit-command 'left)
  4627. (throw 'exit nil))
  4628. (error "Not on a heading")))
  4629. (defun org-goto-right ()
  4630. "Finish `org-goto' by going to the new location."
  4631. (interactive)
  4632. (if (org-on-heading-p)
  4633. (progn
  4634. (setq org-goto-selected-point (point)
  4635. org-goto-exit-command 'right)
  4636. (throw 'exit nil))
  4637. (error "Not on a heading")))
  4638. (defun org-goto-quit ()
  4639. "Finish `org-goto' without cursor motion."
  4640. (interactive)
  4641. (setq org-goto-selected-point nil)
  4642. (setq org-goto-exit-command 'quit)
  4643. (throw 'exit nil))
  4644. ;;; Indirect buffer display of subtrees
  4645. (defvar org-indirect-dedicated-frame nil
  4646. "This is the frame being used for indirect tree display.")
  4647. (defvar org-last-indirect-buffer nil)
  4648. (defun org-tree-to-indirect-buffer (&optional arg)
  4649. "Create indirect buffer and narrow it to current subtree.
  4650. With numerical prefix ARG, go up to this level and then take that tree.
  4651. If ARG is negative, go up that many levels.
  4652. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4653. indirect buffer previously made with this command, to avoid proliferation of
  4654. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4655. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4656. is kept so that you can work with several indirect buffers at the same time.
  4657. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4658. requests that a new frame be made for the new buffer, so that the dedicated
  4659. frame is not changed."
  4660. (interactive "P")
  4661. (let ((cbuf (current-buffer))
  4662. (cwin (selected-window))
  4663. (pos (point))
  4664. beg end level heading ibuf)
  4665. (save-excursion
  4666. (org-back-to-heading t)
  4667. (when (numberp arg)
  4668. (setq level (org-outline-level))
  4669. (if (< arg 0) (setq arg (+ level arg)))
  4670. (while (> (setq level (org-outline-level)) arg)
  4671. (outline-up-heading 1 t)))
  4672. (setq beg (point)
  4673. heading (org-get-heading))
  4674. (org-end-of-subtree t) (setq end (point)))
  4675. (if (and (buffer-live-p org-last-indirect-buffer)
  4676. (not (eq org-indirect-buffer-display 'new-frame))
  4677. (not arg))
  4678. (kill-buffer org-last-indirect-buffer))
  4679. (setq ibuf (org-get-indirect-buffer cbuf)
  4680. org-last-indirect-buffer ibuf)
  4681. (cond
  4682. ((or (eq org-indirect-buffer-display 'new-frame)
  4683. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4684. (select-frame (make-frame))
  4685. (delete-other-windows)
  4686. (switch-to-buffer ibuf)
  4687. (org-set-frame-title heading))
  4688. ((eq org-indirect-buffer-display 'dedicated-frame)
  4689. (raise-frame
  4690. (select-frame (or (and org-indirect-dedicated-frame
  4691. (frame-live-p org-indirect-dedicated-frame)
  4692. org-indirect-dedicated-frame)
  4693. (setq org-indirect-dedicated-frame (make-frame)))))
  4694. (delete-other-windows)
  4695. (switch-to-buffer ibuf)
  4696. (org-set-frame-title (concat "Indirect: " heading)))
  4697. ((eq org-indirect-buffer-display 'current-window)
  4698. (switch-to-buffer ibuf))
  4699. ((eq org-indirect-buffer-display 'other-window)
  4700. (pop-to-buffer ibuf))
  4701. (t (error "Invalid value.")))
  4702. (if (featurep 'xemacs)
  4703. (save-excursion (org-mode) (turn-on-font-lock)))
  4704. (narrow-to-region beg end)
  4705. (show-all)
  4706. (goto-char pos)
  4707. (and (window-live-p cwin) (select-window cwin))))
  4708. (defun org-get-indirect-buffer (&optional buffer)
  4709. (setq buffer (or buffer (current-buffer)))
  4710. (let ((n 1) (base (buffer-name buffer)) bname)
  4711. (while (buffer-live-p
  4712. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4713. (setq n (1+ n)))
  4714. (condition-case nil
  4715. (make-indirect-buffer buffer bname 'clone)
  4716. (error (make-indirect-buffer buffer bname)))))
  4717. (defun org-set-frame-title (title)
  4718. "Set the title of the current frame to the string TITLE."
  4719. ;; FIXME: how to name a single frame in XEmacs???
  4720. (unless (featurep 'xemacs)
  4721. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4722. ;;;; Structure editing
  4723. ;;; Inserting headlines
  4724. (defun org-previous-line-empty-p ()
  4725. (save-excursion
  4726. (and (not (bobp))
  4727. (or (beginning-of-line 0) t)
  4728. (save-match-data
  4729. (looking-at "[ \t]*$")))))
  4730. (defun org-insert-heading (&optional force-heading)
  4731. "Insert a new heading or item with same depth at point.
  4732. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4733. If point is at the beginning of a headline, insert a sibling before the
  4734. current headline. If point is not at the beginning, do not split the line,
  4735. but create the new headline after the current line."
  4736. (interactive "P")
  4737. (if (= (buffer-size) 0)
  4738. (insert "\n* ")
  4739. (when (or force-heading (not (org-insert-item)))
  4740. (let* ((empty-line-p nil)
  4741. (head (save-excursion
  4742. (condition-case nil
  4743. (progn
  4744. (org-back-to-heading)
  4745. (setq empty-line-p (org-previous-line-empty-p))
  4746. (match-string 0))
  4747. (error "*"))))
  4748. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4749. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4750. pos hide-previous previous-pos)
  4751. (cond
  4752. ((and (org-on-heading-p) (bolp)
  4753. (or (bobp)
  4754. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4755. ;; insert before the current line
  4756. (open-line (if blank 2 1)))
  4757. ((and (bolp)
  4758. (or (bobp)
  4759. (save-excursion
  4760. (backward-char 1) (not (org-invisible-p)))))
  4761. ;; insert right here
  4762. nil)
  4763. (t
  4764. ;; somewhere in the line
  4765. (save-excursion
  4766. (setq previous-pos (point-at-bol))
  4767. (end-of-line)
  4768. (setq hide-previous (org-invisible-p)))
  4769. (and org-insert-heading-respect-content (org-show-subtree))
  4770. (let ((split
  4771. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4772. (save-excursion
  4773. (let ((p (point)))
  4774. (goto-char (point-at-bol))
  4775. (and (looking-at org-complex-heading-regexp)
  4776. (> p (match-beginning 4)))))))
  4777. tags pos)
  4778. (cond
  4779. (org-insert-heading-respect-content
  4780. (org-end-of-subtree nil t)
  4781. (or (bolp) (newline))
  4782. (or (org-previous-line-empty-p)
  4783. (and blank (newline)))
  4784. (open-line 1))
  4785. ((org-on-heading-p)
  4786. (when hide-previous
  4787. (show-children)
  4788. (org-show-entry))
  4789. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4790. (setq tags (and (match-end 2) (match-string 2)))
  4791. (and (match-end 1)
  4792. (delete-region (match-beginning 1) (match-end 1)))
  4793. (setq pos (point-at-bol))
  4794. (or split (end-of-line 1))
  4795. (delete-horizontal-space)
  4796. (newline (if blank 2 1))
  4797. (when tags
  4798. (save-excursion
  4799. (goto-char pos)
  4800. (end-of-line 1)
  4801. (insert " " tags)
  4802. (org-set-tags nil 'align))))
  4803. (t
  4804. (or split (end-of-line 1))
  4805. (newline (if blank 2 1)))))))
  4806. (insert head) (just-one-space)
  4807. (setq pos (point))
  4808. (end-of-line 1)
  4809. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4810. (when (and org-insert-heading-respect-content hide-previous)
  4811. (save-excursion
  4812. (goto-char previous-pos)
  4813. (hide-subtree)))
  4814. (run-hooks 'org-insert-heading-hook)))))
  4815. (defun org-get-heading (&optional no-tags)
  4816. "Return the heading of the current entry, without the stars."
  4817. (save-excursion
  4818. (org-back-to-heading t)
  4819. (if (looking-at
  4820. (if no-tags
  4821. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4822. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4823. (match-string 1) "")))
  4824. (defun org-heading-components ()
  4825. "Return the components of the current heading.
  4826. This is a list with the following elements:
  4827. - the level as an integer
  4828. - the reduced level, different if `org-odd-levels-only' is set.
  4829. - the TODO keyword, or nil
  4830. - the priority character, like ?A, or nil if no priority is given
  4831. - the headline text itself, or the tags string if no headline text
  4832. - the tags string, or nil."
  4833. (save-excursion
  4834. (org-back-to-heading t)
  4835. (if (looking-at org-complex-heading-regexp)
  4836. (list (length (match-string 1))
  4837. (org-reduced-level (length (match-string 1)))
  4838. (org-match-string-no-properties 2)
  4839. (and (match-end 3) (aref (match-string 3) 2))
  4840. (org-match-string-no-properties 4)
  4841. (org-match-string-no-properties 5)))))
  4842. (defun org-get-entry ()
  4843. "Get the entry text, after heading, entire subtree."
  4844. (save-excursion
  4845. (org-back-to-heading t)
  4846. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  4847. (defun org-insert-heading-after-current ()
  4848. "Insert a new heading with same level as current, after current subtree."
  4849. (interactive)
  4850. (org-back-to-heading)
  4851. (org-insert-heading)
  4852. (org-move-subtree-down)
  4853. (end-of-line 1))
  4854. (defun org-insert-heading-respect-content ()
  4855. (interactive)
  4856. (let ((org-insert-heading-respect-content t))
  4857. (org-insert-heading t)))
  4858. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4859. (interactive "P")
  4860. (let ((org-insert-heading-respect-content t))
  4861. (org-insert-todo-heading force-state t)))
  4862. (defun org-insert-todo-heading (arg &optional force-heading)
  4863. "Insert a new heading with the same level and TODO state as current heading.
  4864. If the heading has no TODO state, or if the state is DONE, use the first
  4865. state (TODO by default). Also with prefix arg, force first state."
  4866. (interactive "P")
  4867. (when (or force-heading (not (org-insert-item 'checkbox)))
  4868. (org-insert-heading force-heading)
  4869. (save-excursion
  4870. (org-back-to-heading)
  4871. (outline-previous-heading)
  4872. (looking-at org-todo-line-regexp))
  4873. (let*
  4874. ((new-mark-x
  4875. (if (or arg
  4876. (not (match-beginning 2))
  4877. (member (match-string 2) org-done-keywords))
  4878. (car org-todo-keywords-1)
  4879. (match-string 2)))
  4880. (new-mark
  4881. (or
  4882. (run-hook-with-args-until-success
  4883. 'org-todo-get-default-hook new-mark-x nil)
  4884. new-mark-x)))
  4885. (beginning-of-line 1)
  4886. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4887. (insert new-mark " ")))
  4888. (when org-provide-todo-statistics
  4889. (org-update-parent-todo-statistics))))
  4890. (defun org-insert-subheading (arg)
  4891. "Insert a new subheading and demote it.
  4892. Works for outline headings and for plain lists alike."
  4893. (interactive "P")
  4894. (org-insert-heading arg)
  4895. (cond
  4896. ((org-on-heading-p) (org-do-demote))
  4897. ((org-at-item-p) (org-indent-item 1))))
  4898. (defun org-insert-todo-subheading (arg)
  4899. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4900. Works for outline headings and for plain lists alike."
  4901. (interactive "P")
  4902. (org-insert-todo-heading arg)
  4903. (cond
  4904. ((org-on-heading-p) (org-do-demote))
  4905. ((org-at-item-p) (org-indent-item 1))))
  4906. ;;; Promotion and Demotion
  4907. (defun org-promote-subtree ()
  4908. "Promote the entire subtree.
  4909. See also `org-promote'."
  4910. (interactive)
  4911. (save-excursion
  4912. (org-map-tree 'org-promote))
  4913. (org-fix-position-after-promote))
  4914. (defun org-demote-subtree ()
  4915. "Demote the entire subtree. See `org-demote'.
  4916. See also `org-promote'."
  4917. (interactive)
  4918. (save-excursion
  4919. (org-map-tree 'org-demote))
  4920. (org-fix-position-after-promote))
  4921. (defun org-do-promote ()
  4922. "Promote the current heading higher up the tree.
  4923. If the region is active in `transient-mark-mode', promote all headings
  4924. in the region."
  4925. (interactive)
  4926. (save-excursion
  4927. (if (org-region-active-p)
  4928. (org-map-region 'org-promote (region-beginning) (region-end))
  4929. (org-promote)))
  4930. (org-fix-position-after-promote))
  4931. (defun org-do-demote ()
  4932. "Demote the current heading lower down the tree.
  4933. If the region is active in `transient-mark-mode', demote all headings
  4934. in the region."
  4935. (interactive)
  4936. (save-excursion
  4937. (if (org-region-active-p)
  4938. (org-map-region 'org-demote (region-beginning) (region-end))
  4939. (org-demote)))
  4940. (org-fix-position-after-promote))
  4941. (defun org-fix-position-after-promote ()
  4942. "Make sure that after pro/demotion cursor position is right."
  4943. (let ((pos (point)))
  4944. (when (save-excursion
  4945. (beginning-of-line 1)
  4946. (looking-at org-todo-line-regexp)
  4947. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4948. (cond ((eobp) (insert " "))
  4949. ((eolp) (insert " "))
  4950. ((equal (char-after) ?\ ) (forward-char 1))))))
  4951. (defun org-reduced-level (l)
  4952. "Compute the effective level of a heading.
  4953. This takes into account the setting of `org-odd-levels-only'."
  4954. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4955. (defun org-get-valid-level (level &optional change)
  4956. "Rectify a level change under the influence of `org-odd-levels-only'
  4957. LEVEL is a current level, CHANGE is by how much the level should be
  4958. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4959. even level numbers will become the next higher odd number."
  4960. (if org-odd-levels-only
  4961. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4962. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4963. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4964. (max 1 (+ level (or change 0)))))
  4965. (if (boundp 'define-obsolete-function-alias)
  4966. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4967. (define-obsolete-function-alias 'org-get-legal-level
  4968. 'org-get-valid-level)
  4969. (define-obsolete-function-alias 'org-get-legal-level
  4970. 'org-get-valid-level "23.1")))
  4971. (defun org-promote ()
  4972. "Promote the current heading higher up the tree.
  4973. If the region is active in `transient-mark-mode', promote all headings
  4974. in the region."
  4975. (org-back-to-heading t)
  4976. (let* ((level (save-match-data (funcall outline-level)))
  4977. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4978. (diff (abs (- level (length up-head) -1))))
  4979. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4980. (replace-match up-head nil t)
  4981. ;; Fixup tag positioning
  4982. (and org-auto-align-tags (org-set-tags nil t))
  4983. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4984. (defun org-demote ()
  4985. "Demote the current heading lower down the tree.
  4986. If the region is active in `transient-mark-mode', demote all headings
  4987. in the region."
  4988. (org-back-to-heading t)
  4989. (let* ((level (save-match-data (funcall outline-level)))
  4990. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4991. (diff (abs (- level (length down-head) -1))))
  4992. (replace-match down-head nil t)
  4993. ;; Fixup tag positioning
  4994. (and org-auto-align-tags (org-set-tags nil t))
  4995. (if org-adapt-indentation (org-fixup-indentation diff))))
  4996. (defun org-map-tree (fun)
  4997. "Call FUN for every heading underneath the current one."
  4998. (org-back-to-heading)
  4999. (let ((level (funcall outline-level)))
  5000. (save-excursion
  5001. (funcall fun)
  5002. (while (and (progn
  5003. (outline-next-heading)
  5004. (> (funcall outline-level) level))
  5005. (not (eobp)))
  5006. (funcall fun)))))
  5007. (defun org-map-region (fun beg end)
  5008. "Call FUN for every heading between BEG and END."
  5009. (let ((org-ignore-region t))
  5010. (save-excursion
  5011. (setq end (copy-marker end))
  5012. (goto-char beg)
  5013. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5014. (< (point) end))
  5015. (funcall fun))
  5016. (while (and (progn
  5017. (outline-next-heading)
  5018. (< (point) end))
  5019. (not (eobp)))
  5020. (funcall fun)))))
  5021. (defun org-fixup-indentation (diff)
  5022. "Change the indentation in the current entry by DIFF
  5023. However, if any line in the current entry has no indentation, or if it
  5024. would end up with no indentation after the change, nothing at all is done."
  5025. (save-excursion
  5026. (let ((end (save-excursion (outline-next-heading)
  5027. (point-marker)))
  5028. (prohibit (if (> diff 0)
  5029. "^\\S-"
  5030. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5031. col)
  5032. (unless (save-excursion (end-of-line 1)
  5033. (re-search-forward prohibit end t))
  5034. (while (and (< (point) end)
  5035. (re-search-forward "^[ \t]+" end t))
  5036. (goto-char (match-end 0))
  5037. (setq col (current-column))
  5038. (if (< diff 0) (replace-match ""))
  5039. (org-indent-to-column (+ diff col))))
  5040. (move-marker end nil))))
  5041. (defun org-convert-to-odd-levels ()
  5042. "Convert an org-mode file with all levels allowed to one with odd levels.
  5043. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5044. level 5 etc."
  5045. (interactive)
  5046. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5047. (let ((org-odd-levels-only nil) n)
  5048. (save-excursion
  5049. (goto-char (point-min))
  5050. (while (re-search-forward "^\\*\\*+ " nil t)
  5051. (setq n (- (length (match-string 0)) 2))
  5052. (while (>= (setq n (1- n)) 0)
  5053. (org-demote))
  5054. (end-of-line 1))))))
  5055. (defun org-convert-to-oddeven-levels ()
  5056. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5057. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5058. section with an even level, conversion would destroy the structure of the file. An error
  5059. is signaled in this case."
  5060. (interactive)
  5061. (goto-char (point-min))
  5062. ;; First check if there are no even levels
  5063. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5064. (org-show-context t)
  5065. (error "Not all levels are odd in this file. Conversion not possible."))
  5066. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5067. (let ((org-odd-levels-only nil) n)
  5068. (save-excursion
  5069. (goto-char (point-min))
  5070. (while (re-search-forward "^\\*\\*+ " nil t)
  5071. (setq n (/ (1- (length (match-string 0))) 2))
  5072. (while (>= (setq n (1- n)) 0)
  5073. (org-promote))
  5074. (end-of-line 1))))))
  5075. (defun org-tr-level (n)
  5076. "Make N odd if required."
  5077. (if org-odd-levels-only (1+ (/ n 2)) n))
  5078. ;;; Vertical tree motion, cutting and pasting of subtrees
  5079. (defun org-move-subtree-up (&optional arg)
  5080. "Move the current subtree up past ARG headlines of the same level."
  5081. (interactive "p")
  5082. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5083. (defun org-move-subtree-down (&optional arg)
  5084. "Move the current subtree down past ARG headlines of the same level."
  5085. (interactive "p")
  5086. (setq arg (prefix-numeric-value arg))
  5087. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5088. 'outline-get-last-sibling))
  5089. (ins-point (make-marker))
  5090. (cnt (abs arg))
  5091. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5092. ;; Select the tree
  5093. (org-back-to-heading)
  5094. (setq beg0 (point))
  5095. (save-excursion
  5096. (setq ne-beg (org-back-over-empty-lines))
  5097. (setq beg (point)))
  5098. (save-match-data
  5099. (save-excursion (outline-end-of-heading)
  5100. (setq folded (org-invisible-p)))
  5101. (outline-end-of-subtree))
  5102. (outline-next-heading)
  5103. (setq ne-end (org-back-over-empty-lines))
  5104. (setq end (point))
  5105. (goto-char beg0)
  5106. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5107. ;; include less whitespace
  5108. (save-excursion
  5109. (goto-char beg)
  5110. (forward-line (- ne-beg ne-end))
  5111. (setq beg (point))))
  5112. ;; Find insertion point, with error handling
  5113. (while (> cnt 0)
  5114. (or (and (funcall movfunc) (looking-at outline-regexp))
  5115. (progn (goto-char beg0)
  5116. (error "Cannot move past superior level or buffer limit")))
  5117. (setq cnt (1- cnt)))
  5118. (if (> arg 0)
  5119. ;; Moving forward - still need to move over subtree
  5120. (progn (org-end-of-subtree t t)
  5121. (save-excursion
  5122. (org-back-over-empty-lines)
  5123. (or (bolp) (newline)))))
  5124. (setq ne-ins (org-back-over-empty-lines))
  5125. (move-marker ins-point (point))
  5126. (setq txt (buffer-substring beg end))
  5127. (org-save-markers-in-region beg end)
  5128. (delete-region beg end)
  5129. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5130. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5131. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5132. (let ((bbb (point)))
  5133. (insert-before-markers txt)
  5134. (org-reinstall-markers-in-region bbb)
  5135. (move-marker ins-point bbb))
  5136. (or (bolp) (insert "\n"))
  5137. (setq ins-end (point))
  5138. (goto-char ins-point)
  5139. (org-skip-whitespace)
  5140. (when (and (< arg 0)
  5141. (org-first-sibling-p)
  5142. (> ne-ins ne-beg))
  5143. ;; Move whitespace back to beginning
  5144. (save-excursion
  5145. (goto-char ins-end)
  5146. (let ((kill-whole-line t))
  5147. (kill-line (- ne-ins ne-beg)) (point)))
  5148. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5149. (move-marker ins-point nil)
  5150. (org-compact-display-after-subtree-move)
  5151. (org-show-empty-lines-in-parent)
  5152. (unless folded
  5153. (org-show-entry)
  5154. (show-children)
  5155. (org-cycle-hide-drawers 'children))))
  5156. (defvar org-subtree-clip ""
  5157. "Clipboard for cut and paste of subtrees.
  5158. This is actually only a copy of the kill, because we use the normal kill
  5159. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5160. (defvar org-subtree-clip-folded nil
  5161. "Was the last copied subtree folded?
  5162. This is used to fold the tree back after pasting.")
  5163. (defun org-cut-subtree (&optional n)
  5164. "Cut the current subtree into the clipboard.
  5165. With prefix arg N, cut this many sequential subtrees.
  5166. This is a short-hand for marking the subtree and then cutting it."
  5167. (interactive "p")
  5168. (org-copy-subtree n 'cut))
  5169. (defun org-copy-subtree (&optional n cut force-store-markers)
  5170. "Cut the current subtree into the clipboard.
  5171. With prefix arg N, cut this many sequential subtrees.
  5172. This is a short-hand for marking the subtree and then copying it.
  5173. If CUT is non-nil, actually cut the subtree.
  5174. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5175. of some markers in the region, even if CUT is non-nil. This is
  5176. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5177. (interactive "p")
  5178. (let (beg end folded (beg0 (point)))
  5179. (if (interactive-p)
  5180. (org-back-to-heading nil) ; take what looks like a subtree
  5181. (org-back-to-heading t)) ; take what is really there
  5182. (org-back-over-empty-lines)
  5183. (setq beg (point))
  5184. (skip-chars-forward " \t\r\n")
  5185. (save-match-data
  5186. (save-excursion (outline-end-of-heading)
  5187. (setq folded (org-invisible-p)))
  5188. (condition-case nil
  5189. (outline-forward-same-level (1- n))
  5190. (error nil))
  5191. (org-end-of-subtree t t))
  5192. (org-back-over-empty-lines)
  5193. (setq end (point))
  5194. (goto-char beg0)
  5195. (when (> end beg)
  5196. (setq org-subtree-clip-folded folded)
  5197. (when (or cut force-store-markers)
  5198. (org-save-markers-in-region beg end))
  5199. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5200. (setq org-subtree-clip (current-kill 0))
  5201. (message "%s: Subtree(s) with %d characters"
  5202. (if cut "Cut" "Copied")
  5203. (length org-subtree-clip)))))
  5204. (defun org-paste-subtree (&optional level tree for-yank)
  5205. "Paste the clipboard as a subtree, with modification of headline level.
  5206. The entire subtree is promoted or demoted in order to match a new headline
  5207. level.
  5208. If the cursor is at the beginning of a headline, the same level as
  5209. that headline is used to paste the tree
  5210. If not, the new level is derived from the *visible* headings
  5211. before and after the insertion point, and taken to be the inferior headline
  5212. level of the two. So if the previous visible heading is level 3 and the
  5213. next is level 4 (or vice versa), level 4 will be used for insertion.
  5214. This makes sure that the subtree remains an independent subtree and does
  5215. not swallow low level entries.
  5216. You can also force a different level, either by using a numeric prefix
  5217. argument, or by inserting the heading marker by hand. For example, if the
  5218. cursor is after \"*****\", then the tree will be shifted to level 5.
  5219. If optional TREE is given, use this text instead of the kill ring.
  5220. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5221. move back over whitespace before inserting, and move point to the end of
  5222. the inserted text when done."
  5223. (interactive "P")
  5224. (unless (org-kill-is-subtree-p tree)
  5225. (error "%s"
  5226. (substitute-command-keys
  5227. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5228. (let* ((visp (not (org-invisible-p)))
  5229. (txt (or tree (and kill-ring (current-kill 0))))
  5230. (^re (concat "^\\(" outline-regexp "\\)"))
  5231. (re (concat "\\(" outline-regexp "\\)"))
  5232. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5233. (old-level (if (string-match ^re txt)
  5234. (- (match-end 0) (match-beginning 0) 1)
  5235. -1))
  5236. (force-level (cond (level (prefix-numeric-value level))
  5237. ((and (looking-at "[ \t]*$")
  5238. (string-match
  5239. ^re_ (buffer-substring
  5240. (point-at-bol) (point))))
  5241. (- (match-end 1) (match-beginning 1)))
  5242. ((and (bolp)
  5243. (looking-at org-outline-regexp))
  5244. (- (match-end 0) (point) 1))
  5245. (t nil)))
  5246. (previous-level (save-excursion
  5247. (condition-case nil
  5248. (progn
  5249. (outline-previous-visible-heading 1)
  5250. (if (looking-at re)
  5251. (- (match-end 0) (match-beginning 0) 1)
  5252. 1))
  5253. (error 1))))
  5254. (next-level (save-excursion
  5255. (condition-case nil
  5256. (progn
  5257. (or (looking-at outline-regexp)
  5258. (outline-next-visible-heading 1))
  5259. (if (looking-at re)
  5260. (- (match-end 0) (match-beginning 0) 1)
  5261. 1))
  5262. (error 1))))
  5263. (new-level (or force-level (max previous-level next-level)))
  5264. (shift (if (or (= old-level -1)
  5265. (= new-level -1)
  5266. (= old-level new-level))
  5267. 0
  5268. (- new-level old-level)))
  5269. (delta (if (> shift 0) -1 1))
  5270. (func (if (> shift 0) 'org-demote 'org-promote))
  5271. (org-odd-levels-only nil)
  5272. beg end newend)
  5273. ;; Remove the forced level indicator
  5274. (if force-level
  5275. (delete-region (point-at-bol) (point)))
  5276. ;; Paste
  5277. (beginning-of-line 1)
  5278. (unless for-yank (org-back-over-empty-lines))
  5279. (setq beg (point))
  5280. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5281. (insert-before-markers txt)
  5282. (unless (string-match "\n\\'" txt) (insert "\n"))
  5283. (setq newend (point))
  5284. (org-reinstall-markers-in-region beg)
  5285. (setq end (point))
  5286. (goto-char beg)
  5287. (skip-chars-forward " \t\n\r")
  5288. (setq beg (point))
  5289. (if (and (org-invisible-p) visp)
  5290. (save-excursion (outline-show-heading)))
  5291. ;; Shift if necessary
  5292. (unless (= shift 0)
  5293. (save-restriction
  5294. (narrow-to-region beg end)
  5295. (while (not (= shift 0))
  5296. (org-map-region func (point-min) (point-max))
  5297. (setq shift (+ delta shift)))
  5298. (goto-char (point-min))
  5299. (setq newend (point-max))))
  5300. (when (or (interactive-p) for-yank)
  5301. (message "Clipboard pasted as level %d subtree" new-level))
  5302. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5303. kill-ring
  5304. (eq org-subtree-clip (current-kill 0))
  5305. org-subtree-clip-folded)
  5306. ;; The tree was folded before it was killed/copied
  5307. (hide-subtree))
  5308. (and for-yank (goto-char newend))))
  5309. (defun org-kill-is-subtree-p (&optional txt)
  5310. "Check if the current kill is an outline subtree, or a set of trees.
  5311. Returns nil if kill does not start with a headline, or if the first
  5312. headline level is not the largest headline level in the tree.
  5313. So this will actually accept several entries of equal levels as well,
  5314. which is OK for `org-paste-subtree'.
  5315. If optional TXT is given, check this string instead of the current kill."
  5316. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5317. (start-level (and kill
  5318. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5319. org-outline-regexp "\\)")
  5320. kill)
  5321. (- (match-end 2) (match-beginning 2) 1)))
  5322. (re (concat "^" org-outline-regexp))
  5323. (start (1+ (or (match-beginning 2) -1))))
  5324. (if (not start-level)
  5325. (progn
  5326. nil) ;; does not even start with a heading
  5327. (catch 'exit
  5328. (while (setq start (string-match re kill (1+ start)))
  5329. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5330. (throw 'exit nil)))
  5331. t))))
  5332. (defvar org-markers-to-move nil
  5333. "Markers that should be moved with a cut-and-paste operation.
  5334. Those markers are stored together with their positions relative to
  5335. the start of the region.")
  5336. (defun org-save-markers-in-region (beg end)
  5337. "Check markers in region.
  5338. If these markers are between BEG and END, record their position relative
  5339. to BEG, so that after moving the block of text, we can put the markers back
  5340. into place.
  5341. This function gets called just before an entry or tree gets cut from the
  5342. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5343. called immediately, to move the markers with the entries."
  5344. (setq org-markers-to-move nil)
  5345. (when (featurep 'org-clock)
  5346. (org-clock-save-markers-for-cut-and-paste beg end))
  5347. (when (featurep 'org-agenda)
  5348. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5349. (defun org-check-and-save-marker (marker beg end)
  5350. "Check if MARKER is between BEG and END.
  5351. If yes, remember the marker and the distance to BEG."
  5352. (when (and (marker-buffer marker)
  5353. (equal (marker-buffer marker) (current-buffer)))
  5354. (if (and (>= marker beg) (< marker end))
  5355. (push (cons marker (- marker beg)) org-markers-to-move))))
  5356. (defun org-reinstall-markers-in-region (beg)
  5357. "Move all remembered markers to their position relative to BEG."
  5358. (mapc (lambda (x)
  5359. (move-marker (car x) (+ beg (cdr x))))
  5360. org-markers-to-move)
  5361. (setq org-markers-to-move nil))
  5362. (defun org-narrow-to-subtree ()
  5363. "Narrow buffer to the current subtree."
  5364. (interactive)
  5365. (save-excursion
  5366. (save-match-data
  5367. (narrow-to-region
  5368. (progn (org-back-to-heading) (point))
  5369. (progn (org-end-of-subtree t) (point))))))
  5370. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5371. "Clone the task (subtree) at point N times.
  5372. The clones will be inserted as siblings.
  5373. In interactive use, the user will be prompted for the number of clones
  5374. to be produced, and for a time SHIFT, which may be a repeater as used
  5375. in time stamps, for example `+3d'.
  5376. When a valid repeater is given and the entry contains any time stamps,
  5377. the clones will become a sequence in time, with time stamps in the
  5378. subtree shifted for each clone produced. If SHIFT is nil or the
  5379. empty string, time stamps will be left alone.
  5380. If the original subtree did contain time stamps with a repeater,
  5381. the following will happen:
  5382. - the repeater will be removed in each clone
  5383. - an additional clone will be produced, with the current, unshifted
  5384. date(s) in the entry.
  5385. - the original entry will be placed *after* all the clones, with
  5386. repeater intact.
  5387. - the start days in the repeater in the original entry will be shifted
  5388. to past the last clone.
  5389. I this way you can spell out a number of instances of a repeating task,
  5390. and still retain the repeater to cover future instances of the task."
  5391. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5392. (let (beg end template task
  5393. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5394. (if (not (and (integerp n) (> n 0)))
  5395. (error "Invalid number of replications %s" n))
  5396. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5397. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5398. shift)))
  5399. (error "Invalid shift specification %s" shift))
  5400. (when doshift
  5401. (setq shift-n (string-to-number (match-string 1 shift))
  5402. shift-what (cdr (assoc (match-string 2 shift)
  5403. '(("d" . day) ("w" . week)
  5404. ("m" . month) ("y" . year))))))
  5405. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5406. (setq nmin 1 nmax n)
  5407. (org-back-to-heading t)
  5408. (setq beg (point))
  5409. (org-end-of-subtree t t)
  5410. (setq end (point))
  5411. (setq template (buffer-substring beg end))
  5412. (when (and doshift
  5413. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5414. (delete-region beg end)
  5415. (setq end beg)
  5416. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5417. (goto-char end)
  5418. (loop for n from nmin to nmax do
  5419. (if (not doshift)
  5420. (setq task template)
  5421. (with-temp-buffer
  5422. (insert template)
  5423. (org-mode)
  5424. (goto-char (point-min))
  5425. (while (re-search-forward org-ts-regexp nil t)
  5426. (org-timestamp-change (* n shift-n) shift-what))
  5427. (unless (= n n-no-remove)
  5428. (goto-char (point-min))
  5429. (while (re-search-forward org-ts-regexp nil t)
  5430. (save-excursion
  5431. (goto-char (match-beginning 0))
  5432. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5433. (delete-region (match-beginning 1) (match-end 1))))))
  5434. (setq task (buffer-string))))
  5435. (insert task))
  5436. (goto-char beg)))
  5437. ;;; Outline Sorting
  5438. (defun org-sort (with-case)
  5439. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5440. Optional argument WITH-CASE means sort case-sensitively.
  5441. With a double prefix argument, also remove duplicate entries."
  5442. (interactive "P")
  5443. (if (org-at-table-p)
  5444. (org-call-with-arg 'org-table-sort-lines with-case)
  5445. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5446. (defun org-sort-remove-invisible (s)
  5447. (remove-text-properties 0 (length s) org-rm-props s)
  5448. (while (string-match org-bracket-link-regexp s)
  5449. (setq s (replace-match (if (match-end 2)
  5450. (match-string 3 s)
  5451. (match-string 1 s)) t t s)))
  5452. s)
  5453. (defvar org-priority-regexp) ; defined later in the file
  5454. (defun org-sort-entries-or-items
  5455. (&optional with-case sorting-type getkey-func compare-func property)
  5456. "Sort entries on a certain level of an outline tree, or plain list items.
  5457. If there is an active region, the entries in the region are sorted.
  5458. Else, if the cursor is before the first entry, sort the top-level items.
  5459. Else, the children of the entry at point are sorted.
  5460. If the cursor is at the first item in a plain list, the list items will be
  5461. sorted.
  5462. Sorting can be alphabetically, numerically, by date/time as given by
  5463. a time stamp, by a property or by priority.
  5464. The command prompts for the sorting type unless it has been given to the
  5465. function through the SORTING-TYPE argument, which needs to a character,
  5466. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5467. precise meaning of each character:
  5468. n Numerically, by converting the beginning of the entry/item to a number.
  5469. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5470. t By date/time, either the first active time stamp in the entry, or, if
  5471. none exist, by the first inactive one.
  5472. In items, only the first line will be chekced.
  5473. s By the scheduled date/time.
  5474. d By deadline date/time.
  5475. c By creation time, which is assumed to be the first inactive time stamp
  5476. at the beginning of a line.
  5477. p By priority according to the cookie.
  5478. r By the value of a property.
  5479. Capital letters will reverse the sort order.
  5480. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5481. called with point at the beginning of the record. It must return either
  5482. a string or a number that should serve as the sorting key for that record.
  5483. Comparing entries ignores case by default. However, with an optional argument
  5484. WITH-CASE, the sorting considers case as well."
  5485. (interactive "P")
  5486. (let ((case-func (if with-case 'identity 'downcase))
  5487. start beg end stars re re2
  5488. txt what tmp plain-list-p)
  5489. ;; Find beginning and end of region to sort
  5490. (cond
  5491. ((org-region-active-p)
  5492. ;; we will sort the region
  5493. (setq end (region-end)
  5494. what "region")
  5495. (goto-char (region-beginning))
  5496. (if (not (org-on-heading-p)) (outline-next-heading))
  5497. (setq start (point)))
  5498. ((org-at-item-p)
  5499. ;; we will sort this plain list
  5500. (org-beginning-of-item-list) (setq start (point))
  5501. (org-end-of-item-list) (setq end (point))
  5502. (goto-char start)
  5503. (setq plain-list-p t
  5504. what "plain list"))
  5505. ((or (org-on-heading-p)
  5506. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5507. ;; we will sort the children of the current headline
  5508. (org-back-to-heading)
  5509. (setq start (point)
  5510. end (progn (org-end-of-subtree t t)
  5511. (org-back-over-empty-lines)
  5512. (point))
  5513. what "children")
  5514. (goto-char start)
  5515. (show-subtree)
  5516. (outline-next-heading))
  5517. (t
  5518. ;; we will sort the top-level entries in this file
  5519. (goto-char (point-min))
  5520. (or (org-on-heading-p) (outline-next-heading))
  5521. (setq start (point) end (point-max) what "top-level")
  5522. (goto-char start)
  5523. (show-all)))
  5524. (setq beg (point))
  5525. (if (>= beg end) (error "Nothing to sort"))
  5526. (unless plain-list-p
  5527. (looking-at "\\(\\*+\\)")
  5528. (setq stars (match-string 1)
  5529. re (concat "^" (regexp-quote stars) " +")
  5530. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5531. txt (buffer-substring beg end))
  5532. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5533. (if (and (not (equal stars "*")) (string-match re2 txt))
  5534. (error "Region to sort contains a level above the first entry")))
  5535. (unless sorting-type
  5536. (message
  5537. (if plain-list-p
  5538. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5539. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5540. [t]ime [s ]cheduled [d]eadline [c]reated
  5541. A/N/T/S/D/C/P/O/F means reversed:")
  5542. what)
  5543. (setq sorting-type (read-char-exclusive))
  5544. (and (= (downcase sorting-type) ?f)
  5545. (setq getkey-func
  5546. (org-ido-completing-read "Sort using function: "
  5547. obarray 'fboundp t nil nil))
  5548. (setq getkey-func (intern getkey-func)))
  5549. (and (= (downcase sorting-type) ?r)
  5550. (setq property
  5551. (org-ido-completing-read "Property: "
  5552. (mapcar 'list (org-buffer-property-keys t))
  5553. nil t))))
  5554. (message "Sorting entries...")
  5555. (save-restriction
  5556. (narrow-to-region start end)
  5557. (let ((dcst (downcase sorting-type))
  5558. (case-fold-search nil)
  5559. (now (current-time)))
  5560. (sort-subr
  5561. (/= dcst sorting-type)
  5562. ;; This function moves to the beginning character of the "record" to
  5563. ;; be sorted.
  5564. (if plain-list-p
  5565. (lambda nil
  5566. (if (org-at-item-p) t (goto-char (point-max))))
  5567. (lambda nil
  5568. (if (re-search-forward re nil t)
  5569. (goto-char (match-beginning 0))
  5570. (goto-char (point-max)))))
  5571. ;; This function moves to the last character of the "record" being
  5572. ;; sorted.
  5573. (if plain-list-p
  5574. 'org-end-of-item
  5575. (lambda nil
  5576. (save-match-data
  5577. (condition-case nil
  5578. (outline-forward-same-level 1)
  5579. (error
  5580. (goto-char (point-max)))))))
  5581. ;; This function returns the value that gets sorted against.
  5582. (if plain-list-p
  5583. (lambda nil
  5584. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5585. (cond
  5586. ((= dcst ?n)
  5587. (string-to-number (buffer-substring (match-end 0)
  5588. (point-at-eol))))
  5589. ((= dcst ?a)
  5590. (buffer-substring (match-end 0) (point-at-eol)))
  5591. ((= dcst ?t)
  5592. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5593. (re-search-forward org-ts-regexp-both
  5594. (point-at-eol) t))
  5595. (time-to-seconds
  5596. (org-time-string-to-time (match-string 0)))
  5597. (time-to-seconds now)))
  5598. ((= dcst ?f)
  5599. (if getkey-func
  5600. (progn
  5601. (setq tmp (funcall getkey-func))
  5602. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5603. tmp)
  5604. (error "Invalid key function `%s'" getkey-func)))
  5605. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5606. (lambda nil
  5607. (cond
  5608. ((= dcst ?n)
  5609. (if (looking-at org-complex-heading-regexp)
  5610. (string-to-number (match-string 4))
  5611. nil))
  5612. ((= dcst ?a)
  5613. (if (looking-at org-complex-heading-regexp)
  5614. (funcall case-func (match-string 4))
  5615. nil))
  5616. ((= dcst ?t)
  5617. (let ((end (save-excursion (outline-next-heading) (point))))
  5618. (if (or (re-search-forward org-ts-regexp end t)
  5619. (re-search-forward org-ts-regexp-both end t))
  5620. (org-time-string-to-time (match-string 0))
  5621. (org-time-string-to-time now))))
  5622. ((= dcst ?c)
  5623. (let ((end (save-excursion (outline-next-heading) (point))))
  5624. (if (re-search-forward
  5625. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5626. end t)
  5627. (time-to-seconds (org-time-string-to-time
  5628. (match-string 0)))
  5629. (time-to-seconds now))))
  5630. ((= dcst ?s)
  5631. (let ((end (save-excursion (outline-next-heading) (point))))
  5632. (if (re-search-forward org-scheduled-time-regexp end t)
  5633. (org-time-string-to-time (match-string 1))
  5634. (org-time-string-to-time now))))
  5635. ((= dcst ?d)
  5636. (let ((end (save-excursion (outline-next-heading) (point))))
  5637. (if (re-search-forward org-deadline-time-regexp end t)
  5638. (org-time-string-to-time (match-string 1))
  5639. (org-time-string-to-time now))))
  5640. ((= dcst ?p)
  5641. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5642. (string-to-char (match-string 2))
  5643. org-default-priority))
  5644. ((= dcst ?r)
  5645. (or (org-entry-get nil property) ""))
  5646. ((= dcst ?o)
  5647. (if (looking-at org-complex-heading-regexp)
  5648. (- 9999 (length (member (match-string 2)
  5649. org-todo-keywords-1)))))
  5650. ((= dcst ?f)
  5651. (if getkey-func
  5652. (progn
  5653. (setq tmp (funcall getkey-func))
  5654. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5655. tmp)
  5656. (error "Invalid key function `%s'" getkey-func)))
  5657. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5658. nil
  5659. (cond
  5660. ((= dcst ?a) 'string<)
  5661. ((= dcst ?t) 'time-less-p)
  5662. ((= dcst ?f) compare-func)
  5663. ((= dcst ?p) '<)
  5664. (t nil)))))
  5665. (message "Sorting entries...done")))
  5666. (defun org-do-sort (table what &optional with-case sorting-type)
  5667. "Sort TABLE of WHAT according to SORTING-TYPE.
  5668. The user will be prompted for the SORTING-TYPE if the call to this
  5669. function does not specify it. WHAT is only for the prompt, to indicate
  5670. what is being sorted. The sorting key will be extracted from
  5671. the car of the elements of the table.
  5672. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5673. (unless sorting-type
  5674. (message
  5675. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5676. what)
  5677. (setq sorting-type (read-char-exclusive)))
  5678. (let ((dcst (downcase sorting-type))
  5679. extractfun comparefun)
  5680. ;; Define the appropriate functions
  5681. (cond
  5682. ((= dcst ?n)
  5683. (setq extractfun 'string-to-number
  5684. comparefun (if (= dcst sorting-type) '< '>)))
  5685. ((= dcst ?a)
  5686. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5687. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5688. comparefun (if (= dcst sorting-type)
  5689. 'string<
  5690. (lambda (a b) (and (not (string< a b))
  5691. (not (string= a b)))))))
  5692. ((= dcst ?t)
  5693. (setq extractfun
  5694. (lambda (x)
  5695. (if (or (string-match org-ts-regexp x)
  5696. (string-match org-ts-regexp-both x))
  5697. (time-to-seconds
  5698. (org-time-string-to-time (match-string 0 x)))
  5699. 0))
  5700. comparefun (if (= dcst sorting-type) '< '>)))
  5701. (t (error "Invalid sorting type `%c'" sorting-type)))
  5702. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5703. table)
  5704. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5705. ;;; Editing source examples
  5706. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5707. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5708. (defvar org-edit-src-force-single-line nil)
  5709. (defvar org-edit-src-from-org-mode nil)
  5710. (defvar org-edit-src-picture nil)
  5711. (defvar org-edit-src-beg-marker nil)
  5712. (defvar org-edit-src-end-marker nil)
  5713. (define-minor-mode org-exit-edit-mode
  5714. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5715. (defun org-edit-src-code ()
  5716. "Edit the source code example at point.
  5717. The example is copied to a separate buffer, and that buffer is switched
  5718. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  5719. This will remove the original code in the Org buffer, and replace it with
  5720. the edited version."
  5721. (interactive)
  5722. (let ((line (org-current-line))
  5723. (case-fold-search t)
  5724. (msg (substitute-command-keys
  5725. "Edit, then exit with C-c ' (C-c and single quote)"))
  5726. (info (org-edit-src-find-region-and-lang))
  5727. (org-mode-p (eq major-mode 'org-mode))
  5728. (beg (make-marker))
  5729. (end (make-marker))
  5730. lang lang-f single lfmt code begline)
  5731. (if (not info)
  5732. nil
  5733. (setq beg (move-marker beg (nth 0 info))
  5734. end (move-marker end (nth 1 info))
  5735. code (buffer-substring-no-properties beg end)
  5736. lang (nth 2 info)
  5737. single (nth 3 info)
  5738. lfmt (nth 4 info)
  5739. lang-f (intern (concat lang "-mode"))
  5740. begline (save-excursion (goto-char beg) (org-current-line)))
  5741. (unless (functionp lang-f)
  5742. (error "No such language mode: %s" lang-f))
  5743. (goto-line line)
  5744. (if (get-buffer "*Org Edit Src Example*")
  5745. (kill-buffer "*Org Edit Src Example*"))
  5746. (switch-to-buffer (get-buffer-create "*Org Edit Src Example*"))
  5747. (insert code)
  5748. (remove-text-properties (point-min) (point-max)
  5749. '(display nil invisible nil intangible nil))
  5750. (let ((org-inhibit-startup t))
  5751. (funcall lang-f))
  5752. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5753. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5754. (when lfmt
  5755. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5756. (when org-mode-p
  5757. (goto-char (point-min))
  5758. (while (re-search-forward "^," nil t)
  5759. (replace-match "")))
  5760. (goto-line (1+ (- line begline)))
  5761. (org-exit-edit-mode)
  5762. (org-set-local 'org-edit-src-beg-marker beg)
  5763. (org-set-local 'org-edit-src-end-marker end)
  5764. (and org-edit-src-persistent-message
  5765. (org-set-local 'header-line-format msg))
  5766. (message "%s" msg)
  5767. t)))
  5768. (defun org-edit-fixed-width-region ()
  5769. "Edit the fixed-width ascii drawing at point.
  5770. This must be a region where each line starts with a colon followed by
  5771. a space character.
  5772. An new buffer is created and the fixed-width region is copied into it,
  5773. and the buffer is switched into `artist-mode' for editing. When done,
  5774. exit with \\[org-edit-src-exit]. The edited text will then replace
  5775. the fragment in the Org-mode buffer."
  5776. (interactive)
  5777. (let ((line (org-current-line))
  5778. (case-fold-search t)
  5779. (msg (substitute-command-keys
  5780. "Edit, then exit with C-c ' (C-c and single quote)"))
  5781. (org-mode-p (eq major-mode 'org-mode))
  5782. (beg (make-marker))
  5783. (end (make-marker))
  5784. beg1 end1 code begline)
  5785. (beginning-of-line 1)
  5786. (if (looking-at "[ \t]*[^:\n \t]")
  5787. nil
  5788. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5789. (setq beg1 (point) end1 beg1)
  5790. (save-excursion
  5791. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5792. (setq beg1 (point-at-bol 2))
  5793. (setq beg1 (point))))
  5794. (save-excursion
  5795. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5796. (setq end1 (1- (match-beginning 0)))
  5797. (setq end1 (point))))
  5798. (goto-line line))
  5799. (setq beg (move-marker beg beg1)
  5800. end (move-marker end end1)
  5801. code (buffer-substring-no-properties beg end)
  5802. begline (save-excursion (goto-char beg) (org-current-line)))
  5803. (if (get-buffer "*Org Edit Picture*")
  5804. (kill-buffer "*Org Edit Picture*"))
  5805. (switch-to-buffer (get-buffer-create "*Org Edit Picture*"))
  5806. (insert code)
  5807. (remove-text-properties (point-min) (point-max)
  5808. '(display nil invisible nil intangible nil))
  5809. (cond
  5810. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5811. (fundamental-mode)
  5812. (artist-mode 1))
  5813. (t (funcall org-edit-fixed-width-region-mode)))
  5814. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5815. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5816. (set (make-local-variable 'org-edit-src-picture) t)
  5817. (goto-char (point-min))
  5818. (while (re-search-forward "^[ \t]*: ?" nil t)
  5819. (replace-match ""))
  5820. (goto-line (1+ (- line begline)))
  5821. (org-exit-edit-mode)
  5822. (org-set-local 'org-edit-src-beg-marker beg)
  5823. (org-set-local 'org-edit-src-end-marker end)
  5824. (and org-edit-src-persistent-message
  5825. (org-set-local 'header-line-format msg))
  5826. (message "%s" msg)
  5827. t)))
  5828. (defun org-edit-src-find-region-and-lang ()
  5829. "Find the region and language for a local edit.
  5830. Return a list with beginning and end of the region, a string representing
  5831. the language, a switch telling of the content should be in a single line."
  5832. (let ((re-list
  5833. (append
  5834. org-edit-src-region-extra
  5835. '(
  5836. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5837. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5838. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5839. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5840. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5841. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5842. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5843. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5844. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5845. ("^#\\+html:" "\n" "html" single-line)
  5846. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5847. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5848. ("^#\\+latex:" "\n" "latex" single-line)
  5849. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5850. ("^#\\+ascii:" "\n" "ascii" single-line)
  5851. )))
  5852. (pos (point))
  5853. re1 re2 single beg end lang lfmt match-re1)
  5854. (catch 'exit
  5855. (while (setq entry (pop re-list))
  5856. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5857. single (nth 3 entry))
  5858. (save-excursion
  5859. (if (or (looking-at re1)
  5860. (re-search-backward re1 nil t))
  5861. (progn
  5862. (setq match-re1 (match-string 0))
  5863. (setq beg (match-end 0)
  5864. lang (org-edit-src-get-lang lang)
  5865. lfmt (org-edit-src-get-label-format match-re1))
  5866. (if (and (re-search-forward re2 nil t)
  5867. (>= (match-end 0) pos))
  5868. (throw 'exit (list beg (match-beginning 0)
  5869. lang single lfmt))))
  5870. (if (or (looking-at re2)
  5871. (re-search-forward re2 nil t))
  5872. (progn
  5873. (setq end (match-beginning 0))
  5874. (if (and (re-search-backward re1 nil t)
  5875. (<= (match-beginning 0) pos))
  5876. (progn
  5877. (setq lfmt (org-edit-src-get-label-format
  5878. (match-string 0)))
  5879. (throw 'exit
  5880. (list (match-end 0) end
  5881. (org-edit-src-get-lang lang)
  5882. single lfmt))))))))))))
  5883. (defun org-edit-src-get-lang (lang)
  5884. "Extract the src language."
  5885. (let ((m (match-string 0)))
  5886. (cond
  5887. ((stringp lang) lang)
  5888. ((integerp lang) (match-string lang))
  5889. ((and (eq lang 'lang)
  5890. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5891. (match-string 1 m))
  5892. ((and (eq lang 'style)
  5893. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5894. (match-string 1 m))
  5895. (t "fundamental"))))
  5896. (defun org-edit-src-get-label-format (s)
  5897. "Extract the label format."
  5898. (save-match-data
  5899. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5900. (match-string 1 s))))
  5901. (defun org-edit-src-exit ()
  5902. "Exit special edit and protect problematic lines."
  5903. (interactive)
  5904. (unless (member (buffer-name)
  5905. '("*Org Edit Src Example*" "*Org Edit Picture*"))
  5906. (error "This is not an sub-editing buffer, something is wrong..."))
  5907. (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line)
  5908. 1
  5909. (org-current-line)))
  5910. (beg org-edit-src-beg-marker)
  5911. (end org-edit-src-end-marker)
  5912. (buffer (current-buffer))
  5913. code)
  5914. (goto-char (point-min))
  5915. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5916. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5917. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5918. (goto-char (point-min))
  5919. (while (re-search-forward "\n" nil t)
  5920. (replace-match " "))
  5921. (goto-char (point-min))
  5922. (if (looking-at "\\s-*") (replace-match " "))
  5923. (if (re-search-forward "\\s-+\\'" nil t)
  5924. (replace-match "")))
  5925. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5926. (goto-char (point-min))
  5927. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5928. (replace-match ",\\1"))
  5929. (when font-lock-mode
  5930. (font-lock-unfontify-region (point-min) (point-max)))
  5931. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5932. (when (org-bound-and-true-p org-edit-src-picture)
  5933. (untabify (point-min) (point-max))
  5934. (goto-char (point-min))
  5935. (while (re-search-forward "^" nil t)
  5936. (replace-match ": "))
  5937. (when font-lock-mode
  5938. (font-lock-unfontify-region (point-min) (point-max)))
  5939. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5940. (setq code (buffer-string))
  5941. (switch-to-buffer (marker-buffer beg))
  5942. (kill-buffer buffer)
  5943. (goto-char beg)
  5944. (delete-region beg end)
  5945. (insert code)
  5946. (goto-char beg)
  5947. (goto-line (1- (+ (org-current-line) line)))
  5948. (move-marker beg nil)
  5949. (move-marker end nil)))
  5950. ;;; The orgstruct minor mode
  5951. ;; Define a minor mode which can be used in other modes in order to
  5952. ;; integrate the org-mode structure editing commands.
  5953. ;; This is really a hack, because the org-mode structure commands use
  5954. ;; keys which normally belong to the major mode. Here is how it
  5955. ;; works: The minor mode defines all the keys necessary to operate the
  5956. ;; structure commands, but wraps the commands into a function which
  5957. ;; tests if the cursor is currently at a headline or a plain list
  5958. ;; item. If that is the case, the structure command is used,
  5959. ;; temporarily setting many Org-mode variables like regular
  5960. ;; expressions for filling etc. However, when any of those keys is
  5961. ;; used at a different location, function uses `key-binding' to look
  5962. ;; up if the key has an associated command in another currently active
  5963. ;; keymap (minor modes, major mode, global), and executes that
  5964. ;; command. There might be problems if any of the keys is otherwise
  5965. ;; used as a prefix key.
  5966. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5967. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5968. ;; addresses this by checking explicitly for both bindings.
  5969. (defvar orgstruct-mode-map (make-sparse-keymap)
  5970. "Keymap for the minor `orgstruct-mode'.")
  5971. (defvar org-local-vars nil
  5972. "List of local variables, for use by `orgstruct-mode'")
  5973. ;;;###autoload
  5974. (define-minor-mode orgstruct-mode
  5975. "Toggle the minor more `orgstruct-mode'.
  5976. This mode is for using Org-mode structure commands in other modes.
  5977. The following key behave as if Org-mode was active, if the cursor
  5978. is on a headline, or on a plain list item (both in the definition
  5979. of Org-mode).
  5980. M-up Move entry/item up
  5981. M-down Move entry/item down
  5982. M-left Promote
  5983. M-right Demote
  5984. M-S-up Move entry/item up
  5985. M-S-down Move entry/item down
  5986. M-S-left Promote subtree
  5987. M-S-right Demote subtree
  5988. M-q Fill paragraph and items like in Org-mode
  5989. C-c ^ Sort entries
  5990. C-c - Cycle list bullet
  5991. TAB Cycle item visibility
  5992. M-RET Insert new heading/item
  5993. S-M-RET Insert new TODO heading / Checkbox item
  5994. C-c C-c Set tags / toggle checkbox"
  5995. nil " OrgStruct" nil
  5996. (org-load-modules-maybe)
  5997. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5998. ;;;###autoload
  5999. (defun turn-on-orgstruct ()
  6000. "Unconditionally turn on `orgstruct-mode'."
  6001. (orgstruct-mode 1))
  6002. (defun orgstruct++-mode (&optional arg)
  6003. "Toggle `orgstruct-mode', the enhanced version of it.
  6004. In addition to setting orgstruct-mode, this also exports all indentation and
  6005. autofilling variables from org-mode into the buffer. It will also
  6006. recognize item context in multiline items.
  6007. Note that turning off orgstruct-mode will *not* remove the
  6008. indentation/paragraph settings. This can only be done by refreshing the
  6009. major mode, for example with \[normal-mode]."
  6010. (interactive "P")
  6011. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6012. (if (< arg 1)
  6013. (orgstruct-mode -1)
  6014. (orgstruct-mode 1)
  6015. (let (var val)
  6016. (mapc
  6017. (lambda (x)
  6018. (when (string-match
  6019. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6020. (symbol-name (car x)))
  6021. (setq var (car x) val (nth 1 x))
  6022. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6023. org-local-vars)
  6024. (org-set-local 'orgstruct-is-++ t))))
  6025. (defvar orgstruct-is-++ nil
  6026. "Is orgstruct-mode in ++ version in the current-buffer?")
  6027. (make-variable-buffer-local 'orgstruct-is-++)
  6028. ;;;###autoload
  6029. (defun turn-on-orgstruct++ ()
  6030. "Unconditionally turn on `orgstruct++-mode'."
  6031. (orgstruct++-mode 1))
  6032. (defun orgstruct-error ()
  6033. "Error when there is no default binding for a structure key."
  6034. (interactive)
  6035. (error "This key has no function outside structure elements"))
  6036. (defun orgstruct-setup ()
  6037. "Setup orgstruct keymaps."
  6038. (let ((nfunc 0)
  6039. (bindings
  6040. (list
  6041. '([(meta up)] org-metaup)
  6042. '([(meta down)] org-metadown)
  6043. '([(meta left)] org-metaleft)
  6044. '([(meta right)] org-metaright)
  6045. '([(meta shift up)] org-shiftmetaup)
  6046. '([(meta shift down)] org-shiftmetadown)
  6047. '([(meta shift left)] org-shiftmetaleft)
  6048. '([(meta shift right)] org-shiftmetaright)
  6049. '([(shift up)] org-shiftup)
  6050. '([(shift down)] org-shiftdown)
  6051. '([(shift left)] org-shiftleft)
  6052. '([(shift right)] org-shiftright)
  6053. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6054. '("\M-q" fill-paragraph)
  6055. '("\C-c^" org-sort)
  6056. '("\C-c-" org-cycle-list-bullet)))
  6057. elt key fun cmd)
  6058. (while (setq elt (pop bindings))
  6059. (setq nfunc (1+ nfunc))
  6060. (setq key (org-key (car elt))
  6061. fun (nth 1 elt)
  6062. cmd (orgstruct-make-binding fun nfunc key))
  6063. (org-defkey orgstruct-mode-map key cmd))
  6064. ;; Special treatment needed for TAB and RET
  6065. (org-defkey orgstruct-mode-map [(tab)]
  6066. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6067. (org-defkey orgstruct-mode-map "\C-i"
  6068. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6069. (org-defkey orgstruct-mode-map "\M-\C-m"
  6070. (orgstruct-make-binding 'org-insert-heading 105
  6071. "\M-\C-m" [(meta return)]))
  6072. (org-defkey orgstruct-mode-map [(meta return)]
  6073. (orgstruct-make-binding 'org-insert-heading 106
  6074. [(meta return)] "\M-\C-m"))
  6075. (org-defkey orgstruct-mode-map [(shift meta return)]
  6076. (orgstruct-make-binding 'org-insert-todo-heading 107
  6077. [(meta return)] "\M-\C-m"))
  6078. (unless org-local-vars
  6079. (setq org-local-vars (org-get-local-variables)))
  6080. t))
  6081. (defun orgstruct-make-binding (fun n &rest keys)
  6082. "Create a function for binding in the structure minor mode.
  6083. FUN is the command to call inside a table. N is used to create a unique
  6084. command name. KEYS are keys that should be checked in for a command
  6085. to execute outside of tables."
  6086. (eval
  6087. (list 'defun
  6088. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6089. '(arg)
  6090. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6091. "Outside of structure, run the binding of `"
  6092. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6093. "'.")
  6094. '(interactive "p")
  6095. (list 'if
  6096. `(org-context-p 'headline 'item
  6097. (and orgstruct-is-++
  6098. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6099. 'item-body))
  6100. (list 'org-run-like-in-org-mode (list 'quote fun))
  6101. (list 'let '(orgstruct-mode)
  6102. (list 'call-interactively
  6103. (append '(or)
  6104. (mapcar (lambda (k)
  6105. (list 'key-binding k))
  6106. keys)
  6107. '('orgstruct-error))))))))
  6108. (defun org-context-p (&rest contexts)
  6109. "Check if local context is any of CONTEXTS.
  6110. Possible values in the list of contexts are `table', `headline', and `item'."
  6111. (let ((pos (point)))
  6112. (goto-char (point-at-bol))
  6113. (prog1 (or (and (memq 'table contexts)
  6114. (looking-at "[ \t]*|"))
  6115. (and (memq 'headline contexts)
  6116. ;;????????? (looking-at "\\*+"))
  6117. (looking-at outline-regexp))
  6118. (and (memq 'item contexts)
  6119. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6120. (and (memq 'item-body contexts)
  6121. (org-in-item-p)))
  6122. (goto-char pos))))
  6123. (defun org-get-local-variables ()
  6124. "Return a list of all local variables in an org-mode buffer."
  6125. (let (varlist)
  6126. (with-current-buffer (get-buffer-create "*Org tmp*")
  6127. (erase-buffer)
  6128. (org-mode)
  6129. (setq varlist (buffer-local-variables)))
  6130. (kill-buffer "*Org tmp*")
  6131. (delq nil
  6132. (mapcar
  6133. (lambda (x)
  6134. (setq x
  6135. (if (symbolp x)
  6136. (list x)
  6137. (list (car x) (list 'quote (cdr x)))))
  6138. (if (string-match
  6139. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6140. (symbol-name (car x)))
  6141. x nil))
  6142. varlist))))
  6143. ;;;###autoload
  6144. (defun org-run-like-in-org-mode (cmd)
  6145. "Run a command, pretending that the current buffer is in Org-mode.
  6146. This will temporarily bind local variables that are typically bound in
  6147. Org-mode to the values they have in Org-mode, and then interactively
  6148. call CMD."
  6149. (org-load-modules-maybe)
  6150. (unless org-local-vars
  6151. (setq org-local-vars (org-get-local-variables)))
  6152. (eval (list 'let org-local-vars
  6153. (list 'call-interactively (list 'quote cmd)))))
  6154. ;;;; Archiving
  6155. (defun org-get-category (&optional pos)
  6156. "Get the category applying to position POS."
  6157. (get-text-property (or pos (point)) 'org-category))
  6158. (defun org-refresh-category-properties ()
  6159. "Refresh category text properties in the buffer."
  6160. (let ((def-cat (cond
  6161. ((null org-category)
  6162. (if buffer-file-name
  6163. (file-name-sans-extension
  6164. (file-name-nondirectory buffer-file-name))
  6165. "???"))
  6166. ((symbolp org-category) (symbol-name org-category))
  6167. (t org-category)))
  6168. beg end cat pos optionp)
  6169. (org-unmodified
  6170. (save-excursion
  6171. (save-restriction
  6172. (widen)
  6173. (goto-char (point-min))
  6174. (put-text-property (point) (point-max) 'org-category def-cat)
  6175. (while (re-search-forward
  6176. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6177. (setq pos (match-end 0)
  6178. optionp (equal (char-after (match-beginning 0)) ?#)
  6179. cat (org-trim (match-string 2)))
  6180. (if optionp
  6181. (setq beg (point-at-bol) end (point-max))
  6182. (org-back-to-heading t)
  6183. (setq beg (point) end (org-end-of-subtree t t)))
  6184. (put-text-property beg end 'org-category cat)
  6185. (goto-char pos)))))))
  6186. ;;;; Link Stuff
  6187. ;;; Link abbreviations
  6188. (defun org-link-expand-abbrev (link)
  6189. "Apply replacements as defined in `org-link-abbrev-alist."
  6190. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6191. (let* ((key (match-string 1 link))
  6192. (as (or (assoc key org-link-abbrev-alist-local)
  6193. (assoc key org-link-abbrev-alist)))
  6194. (tag (and (match-end 2) (match-string 3 link)))
  6195. rpl)
  6196. (if (not as)
  6197. link
  6198. (setq rpl (cdr as))
  6199. (cond
  6200. ((symbolp rpl) (funcall rpl tag))
  6201. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6202. ((string-match "%h" rpl)
  6203. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6204. (t (concat rpl tag)))))
  6205. link))
  6206. ;;; Storing and inserting links
  6207. (defvar org-insert-link-history nil
  6208. "Minibuffer history for links inserted with `org-insert-link'.")
  6209. (defvar org-stored-links nil
  6210. "Contains the links stored with `org-store-link'.")
  6211. (defvar org-store-link-plist nil
  6212. "Plist with info about the most recently link created with `org-store-link'.")
  6213. (defvar org-link-protocols nil
  6214. "Link protocols added to Org-mode using `org-add-link-type'.")
  6215. (defvar org-store-link-functions nil
  6216. "List of functions that are called to create and store a link.
  6217. Each function will be called in turn until one returns a non-nil
  6218. value. Each function should check if it is responsible for creating
  6219. this link (for example by looking at the major mode).
  6220. If not, it must exit and return nil.
  6221. If yes, it should return a non-nil value after a calling
  6222. `org-store-link-props' with a list of properties and values.
  6223. Special properties are:
  6224. :type The link prefix. like \"http\". This must be given.
  6225. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6226. This is obligatory as well.
  6227. :description Optional default description for the second pair
  6228. of brackets in an Org-mode link. The user can still change
  6229. this when inserting this link into an Org-mode buffer.
  6230. In addition to these, any additional properties can be specified
  6231. and then used in remember templates.")
  6232. (defun org-add-link-type (type &optional follow export)
  6233. "Add TYPE to the list of `org-link-types'.
  6234. Re-compute all regular expressions depending on `org-link-types'
  6235. FOLLOW and EXPORT are two functions.
  6236. FOLLOW should take the link path as the single argument and do whatever
  6237. is necessary to follow the link, for example find a file or display
  6238. a mail message.
  6239. EXPORT should format the link path for export to one of the export formats.
  6240. It should be a function accepting three arguments:
  6241. path the path of the link, the text after the prefix (like \"http:\")
  6242. desc the description of the link, if any, nil if there was no description
  6243. format the export format, a symbol like `html' or `latex'.
  6244. The function may use the FORMAT information to return different values
  6245. depending on the format. The return value will be put literally into
  6246. the exported file.
  6247. Org-mode has a built-in default for exporting links. If you are happy with
  6248. this default, there is no need to define an export function for the link
  6249. type. For a simple example of an export function, see `org-bbdb.el'."
  6250. (add-to-list 'org-link-types type t)
  6251. (org-make-link-regexps)
  6252. (if (assoc type org-link-protocols)
  6253. (setcdr (assoc type org-link-protocols) (list follow export))
  6254. (push (list type follow export) org-link-protocols)))
  6255. ;;;###autoload
  6256. (defun org-store-link (arg)
  6257. "\\<org-mode-map>Store an org-link to the current location.
  6258. This link is added to `org-stored-links' and can later be inserted
  6259. into an org-buffer with \\[org-insert-link].
  6260. For some link types, a prefix arg is interpreted:
  6261. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6262. For file links, arg negates `org-context-in-file-links'."
  6263. (interactive "P")
  6264. (org-load-modules-maybe)
  6265. (setq org-store-link-plist nil) ; reset
  6266. (let (link cpltxt desc description search txt custom-id)
  6267. (cond
  6268. ((run-hook-with-args-until-success 'org-store-link-functions)
  6269. (setq link (plist-get org-store-link-plist :link)
  6270. desc (or (plist-get org-store-link-plist :description) link)))
  6271. ((equal (buffer-name) "*Org Edit Src Example*")
  6272. (let (label gc)
  6273. (while (or (not label)
  6274. (save-excursion
  6275. (save-restriction
  6276. (widen)
  6277. (goto-char (point-min))
  6278. (re-search-forward
  6279. (regexp-quote (format org-coderef-label-format label))
  6280. nil t))))
  6281. (when label (message "Label exists already") (sit-for 2))
  6282. (setq label (read-string "Code line label: " label)))
  6283. (end-of-line 1)
  6284. (setq link (format org-coderef-label-format label))
  6285. (setq gc (- 79 (length link)))
  6286. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6287. (insert link)
  6288. (setq link (concat "(" label ")") desc nil)))
  6289. ((eq major-mode 'calendar-mode)
  6290. (let ((cd (calendar-cursor-to-date)))
  6291. (setq link
  6292. (format-time-string
  6293. (car org-time-stamp-formats)
  6294. (apply 'encode-time
  6295. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6296. nil nil nil))))
  6297. (org-store-link-props :type "calendar" :date cd)))
  6298. ((eq major-mode 'w3-mode)
  6299. (setq cpltxt (if (and (buffer-name)
  6300. (not (string-match "Untitled" (buffer-name))))
  6301. (buffer-name)
  6302. (url-view-url t))
  6303. link (org-make-link (url-view-url t)))
  6304. (org-store-link-props :type "w3" :url (url-view-url t)))
  6305. ((eq major-mode 'w3m-mode)
  6306. (setq cpltxt (or w3m-current-title w3m-current-url)
  6307. link (org-make-link w3m-current-url))
  6308. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6309. ((setq search (run-hook-with-args-until-success
  6310. 'org-create-file-search-functions))
  6311. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6312. "::" search))
  6313. (setq cpltxt (or description link)))
  6314. ((eq major-mode 'image-mode)
  6315. (setq cpltxt (concat "file:"
  6316. (abbreviate-file-name buffer-file-name))
  6317. link (org-make-link cpltxt))
  6318. (org-store-link-props :type "image" :file buffer-file-name))
  6319. ((eq major-mode 'dired-mode)
  6320. ;; link to the file in the current line
  6321. (setq cpltxt (concat "file:"
  6322. (abbreviate-file-name
  6323. (expand-file-name
  6324. (dired-get-filename nil t))))
  6325. link (org-make-link cpltxt)))
  6326. ((and buffer-file-name (org-mode-p))
  6327. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6328. (cond
  6329. ((org-in-regexp "<<\\(.*?\\)>>")
  6330. (setq cpltxt
  6331. (concat "file:"
  6332. (abbreviate-file-name buffer-file-name)
  6333. "::" (match-string 1))
  6334. link (org-make-link cpltxt)))
  6335. ((and (featurep 'org-id)
  6336. (or (eq org-link-to-org-use-id t)
  6337. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6338. (interactive-p))
  6339. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6340. (interactive-p)
  6341. (not custom-id))
  6342. (and org-link-to-org-use-id
  6343. (condition-case nil
  6344. (org-entry-get nil "ID")
  6345. (error nil)))))
  6346. ;; We can make a link using the ID.
  6347. (setq link (condition-case nil
  6348. (prog1 (org-id-store-link)
  6349. (setq desc (plist-get org-store-link-plist
  6350. :description)))
  6351. (error
  6352. ;; probably before first headline, link to file only
  6353. (concat "file:"
  6354. (abbreviate-file-name buffer-file-name))))))
  6355. (t
  6356. ;; Just link to current headline
  6357. (setq cpltxt (concat "file:"
  6358. (abbreviate-file-name buffer-file-name)))
  6359. ;; Add a context search string
  6360. (when (org-xor org-context-in-file-links arg)
  6361. (setq txt (cond
  6362. ((org-on-heading-p) nil)
  6363. ((org-region-active-p)
  6364. (buffer-substring (region-beginning) (region-end)))
  6365. (t nil)))
  6366. (when (or (null txt) (string-match "\\S-" txt))
  6367. (setq cpltxt
  6368. (concat cpltxt "::"
  6369. (condition-case nil
  6370. (org-make-org-heading-search-string txt)
  6371. (error "")))
  6372. desc "NONE")))
  6373. (if (string-match "::\\'" cpltxt)
  6374. (setq cpltxt (substring cpltxt 0 -2)))
  6375. (setq link (org-make-link cpltxt)))))
  6376. ((buffer-file-name (buffer-base-buffer))
  6377. ;; Just link to this file here.
  6378. (setq cpltxt (concat "file:"
  6379. (abbreviate-file-name
  6380. (buffer-file-name (buffer-base-buffer)))))
  6381. ;; Add a context string
  6382. (when (org-xor org-context-in-file-links arg)
  6383. (setq txt (if (org-region-active-p)
  6384. (buffer-substring (region-beginning) (region-end))
  6385. (buffer-substring (point-at-bol) (point-at-eol))))
  6386. ;; Only use search option if there is some text.
  6387. (when (string-match "\\S-" txt)
  6388. (setq cpltxt
  6389. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6390. desc "NONE")))
  6391. (setq link (org-make-link cpltxt)))
  6392. ((interactive-p)
  6393. (error "Cannot link to a buffer which is not visiting a file"))
  6394. (t (setq link nil)))
  6395. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6396. (setq link (or link cpltxt)
  6397. desc (or desc cpltxt))
  6398. (if (equal desc "NONE") (setq desc nil))
  6399. (if (and (interactive-p) link)
  6400. (progn
  6401. (setq org-stored-links
  6402. (cons (list link desc) org-stored-links))
  6403. (message "Stored: %s" (or desc link))
  6404. (when custom-id
  6405. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6406. "::#" custom-id))
  6407. (setq org-stored-links
  6408. (cons (list link desc) org-stored-links))))
  6409. (and link (org-make-link-string link desc)))))
  6410. (defun org-store-link-props (&rest plist)
  6411. "Store link properties, extract names and addresses."
  6412. (let (x adr)
  6413. (when (setq x (plist-get plist :from))
  6414. (setq adr (mail-extract-address-components x))
  6415. (setq plist (plist-put plist :fromname (car adr)))
  6416. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6417. (when (setq x (plist-get plist :to))
  6418. (setq adr (mail-extract-address-components x))
  6419. (setq plist (plist-put plist :toname (car adr)))
  6420. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6421. (let ((from (plist-get plist :from))
  6422. (to (plist-get plist :to)))
  6423. (when (and from to org-from-is-user-regexp)
  6424. (setq plist
  6425. (plist-put plist :fromto
  6426. (if (string-match org-from-is-user-regexp from)
  6427. (concat "to %t")
  6428. (concat "from %f"))))))
  6429. (setq org-store-link-plist plist))
  6430. (defun org-add-link-props (&rest plist)
  6431. "Add these properties to the link property list."
  6432. (let (key value)
  6433. (while plist
  6434. (setq key (pop plist) value (pop plist))
  6435. (setq org-store-link-plist
  6436. (plist-put org-store-link-plist key value)))))
  6437. (defun org-email-link-description (&optional fmt)
  6438. "Return the description part of an email link.
  6439. This takes information from `org-store-link-plist' and formats it
  6440. according to FMT (default from `org-email-link-description-format')."
  6441. (setq fmt (or fmt org-email-link-description-format))
  6442. (let* ((p org-store-link-plist)
  6443. (to (plist-get p :toaddress))
  6444. (from (plist-get p :fromaddress))
  6445. (table
  6446. (list
  6447. (cons "%c" (plist-get p :fromto))
  6448. (cons "%F" (plist-get p :from))
  6449. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6450. (cons "%T" (plist-get p :to))
  6451. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6452. (cons "%s" (plist-get p :subject))
  6453. (cons "%m" (plist-get p :message-id)))))
  6454. (when (string-match "%c" fmt)
  6455. ;; Check if the user wrote this message
  6456. (if (and org-from-is-user-regexp from to
  6457. (save-match-data (string-match org-from-is-user-regexp from)))
  6458. (setq fmt (replace-match "to %t" t t fmt))
  6459. (setq fmt (replace-match "from %f" t t fmt))))
  6460. (org-replace-escapes fmt table)))
  6461. (defun org-make-org-heading-search-string (&optional string heading)
  6462. "Make search string for STRING or current headline."
  6463. (interactive)
  6464. (let ((s (or string (org-get-heading))))
  6465. (unless (and string (not heading))
  6466. ;; We are using a headline, clean up garbage in there.
  6467. (if (string-match org-todo-regexp s)
  6468. (setq s (replace-match "" t t s)))
  6469. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6470. (setq s (replace-match "" t t s)))
  6471. (setq s (org-trim s))
  6472. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6473. org-comment-string "\\)") s)
  6474. (setq s (replace-match "" t t s)))
  6475. (while (string-match org-ts-regexp s)
  6476. (setq s (replace-match "" t t s))))
  6477. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6478. (setq s (replace-match " " t t s)))
  6479. (or string (setq s (concat "*" s))) ; Add * for headlines
  6480. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6481. (defun org-make-link (&rest strings)
  6482. "Concatenate STRINGS."
  6483. (apply 'concat strings))
  6484. (defun org-make-link-string (link &optional description)
  6485. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6486. (unless (string-match "\\S-" link)
  6487. (error "Empty link"))
  6488. (when (stringp description)
  6489. ;; Remove brackets from the description, they are fatal.
  6490. (while (string-match "\\[" description)
  6491. (setq description (replace-match "{" t t description)))
  6492. (while (string-match "\\]" description)
  6493. (setq description (replace-match "}" t t description))))
  6494. (when (equal (org-link-escape link) description)
  6495. ;; No description needed, it is identical
  6496. (setq description nil))
  6497. (when (and (not description)
  6498. (not (equal link (org-link-escape link))))
  6499. (setq description (org-extract-attributes link)))
  6500. (concat "[[" (org-link-escape link) "]"
  6501. (if description (concat "[" description "]") "")
  6502. "]"))
  6503. (defconst org-link-escape-chars
  6504. '((?\ . "%20")
  6505. (?\[ . "%5B")
  6506. (?\] . "%5D")
  6507. (?\340 . "%E0") ; `a
  6508. (?\342 . "%E2") ; ^a
  6509. (?\347 . "%E7") ; ,c
  6510. (?\350 . "%E8") ; `e
  6511. (?\351 . "%E9") ; 'e
  6512. (?\352 . "%EA") ; ^e
  6513. (?\356 . "%EE") ; ^i
  6514. (?\364 . "%F4") ; ^o
  6515. (?\371 . "%F9") ; `u
  6516. (?\373 . "%FB") ; ^u
  6517. (?\; . "%3B")
  6518. (?? . "%3F")
  6519. (?= . "%3D")
  6520. (?+ . "%2B")
  6521. )
  6522. "Association list of escapes for some characters problematic in links.
  6523. This is the list that is used for internal purposes.")
  6524. (defvar org-url-encoding-use-url-hexify nil)
  6525. (defconst org-link-escape-chars-browser
  6526. '((?\ . "%20")) ; 32 for the SPC char
  6527. "Association list of escapes for some characters problematic in links.
  6528. This is the list that is used before handing over to the browser.")
  6529. (defun org-link-escape (text &optional table)
  6530. "Escape characters in TEXT that are problematic for links."
  6531. (if org-url-encoding-use-url-hexify
  6532. (url-hexify-string text)
  6533. (setq table (or table org-link-escape-chars))
  6534. (when text
  6535. (let ((re (mapconcat (lambda (x) (regexp-quote
  6536. (char-to-string (car x))))
  6537. table "\\|")))
  6538. (while (string-match re text)
  6539. (setq text
  6540. (replace-match
  6541. (cdr (assoc (string-to-char (match-string 0 text))
  6542. table))
  6543. t t text)))
  6544. text))))
  6545. (defun org-link-unescape (text &optional table)
  6546. "Reverse the action of `org-link-escape'."
  6547. (if org-url-encoding-use-url-hexify
  6548. (url-unhex-string text)
  6549. (setq table (or table org-link-escape-chars))
  6550. (when text
  6551. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6552. table "\\|")))
  6553. (while (string-match re text)
  6554. (setq text
  6555. (replace-match
  6556. (char-to-string (car (rassoc (match-string 0 text) table)))
  6557. t t text)))
  6558. text))))
  6559. (defun org-xor (a b)
  6560. "Exclusive or."
  6561. (if a (not b) b))
  6562. (defun org-fixup-message-id-for-http (s)
  6563. "Replace special characters in a message id, so it can be used in an http query."
  6564. (while (string-match "<" s)
  6565. (setq s (replace-match "%3C" t t s)))
  6566. (while (string-match ">" s)
  6567. (setq s (replace-match "%3E" t t s)))
  6568. (while (string-match "@" s)
  6569. (setq s (replace-match "%40" t t s)))
  6570. s)
  6571. ;;;###autoload
  6572. (defun org-insert-link-global ()
  6573. "Insert a link like Org-mode does.
  6574. This command can be called in any mode to insert a link in Org-mode syntax."
  6575. (interactive)
  6576. (org-load-modules-maybe)
  6577. (org-run-like-in-org-mode 'org-insert-link))
  6578. (defun org-insert-link (&optional complete-file link-location)
  6579. "Insert a link. At the prompt, enter the link.
  6580. Completion can be used to insert any of the link protocol prefixes like
  6581. http or ftp in use.
  6582. The history can be used to select a link previously stored with
  6583. `org-store-link'. When the empty string is entered (i.e. if you just
  6584. press RET at the prompt), the link defaults to the most recently
  6585. stored link. As SPC triggers completion in the minibuffer, you need to
  6586. use M-SPC or C-q SPC to force the insertion of a space character.
  6587. You will also be prompted for a description, and if one is given, it will
  6588. be displayed in the buffer instead of the link.
  6589. If there is already a link at point, this command will allow you to edit link
  6590. and description parts.
  6591. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6592. be selected using completion. The path to the file will be relative to the
  6593. current directory if the file is in the current directory or a subdirectory.
  6594. Otherwise, the link will be the absolute path as completed in the minibuffer
  6595. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6596. option `org-link-file-path-type'.
  6597. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6598. the current directory or below.
  6599. With three \\[universal-argument] prefixes, negate the meaning of
  6600. `org-keep-stored-link-after-insertion'.
  6601. If `org-make-link-description-function' is non-nil, this function will be
  6602. called with the link target, and the result will be the default
  6603. link description.
  6604. If the LINK-LOCATION parameter is non-nil, this value will be
  6605. used as the link location instead of reading one interactively."
  6606. (interactive "P")
  6607. (let* ((wcf (current-window-configuration))
  6608. (region (if (org-region-active-p)
  6609. (buffer-substring (region-beginning) (region-end))))
  6610. (remove (and region (list (region-beginning) (region-end))))
  6611. (desc region)
  6612. tmphist ; byte-compile incorrectly complains about this
  6613. (link link-location)
  6614. entry file)
  6615. (cond
  6616. (link-location) ; specified by arg, just use it.
  6617. ((org-in-regexp org-bracket-link-regexp 1)
  6618. ;; We do have a link at point, and we are going to edit it.
  6619. (setq remove (list (match-beginning 0) (match-end 0)))
  6620. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6621. (setq link (read-string "Link: "
  6622. (org-link-unescape
  6623. (org-match-string-no-properties 1)))))
  6624. ((or (org-in-regexp org-angle-link-re)
  6625. (org-in-regexp org-plain-link-re))
  6626. ;; Convert to bracket link
  6627. (setq remove (list (match-beginning 0) (match-end 0))
  6628. link (read-string "Link: "
  6629. (org-remove-angle-brackets (match-string 0)))))
  6630. ((member complete-file '((4) (16)))
  6631. ;; Completing read for file names.
  6632. (setq file (read-file-name "File: "))
  6633. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6634. (pwd1 (file-name-as-directory (abbreviate-file-name
  6635. (expand-file-name ".")))))
  6636. (cond
  6637. ((equal complete-file '(16))
  6638. (setq link (org-make-link
  6639. "file:"
  6640. (abbreviate-file-name (expand-file-name file)))))
  6641. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6642. (setq link (org-make-link "file:" (match-string 1 file))))
  6643. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6644. (expand-file-name file))
  6645. (setq link (org-make-link
  6646. "file:" (match-string 1 (expand-file-name file)))))
  6647. (t (setq link (org-make-link "file:" file))))))
  6648. (t
  6649. ;; Read link, with completion for stored links.
  6650. (with-output-to-temp-buffer "*Org Links*"
  6651. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6652. (when org-stored-links
  6653. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6654. (princ (mapconcat
  6655. (lambda (x)
  6656. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6657. (reverse org-stored-links) "\n"))))
  6658. (let ((cw (selected-window)))
  6659. (select-window (get-buffer-window "*Org Links*"))
  6660. (setq truncate-lines t)
  6661. (org-fit-window-to-buffer)
  6662. (select-window cw))
  6663. ;; Fake a link history, containing the stored links.
  6664. (setq tmphist (append (mapcar 'car org-stored-links)
  6665. org-insert-link-history))
  6666. (unwind-protect
  6667. (setq link
  6668. (let ((org-completion-use-ido nil))
  6669. (org-completing-read
  6670. "Link: "
  6671. (append
  6672. (mapcar (lambda (x) (list (concat (car x) ":")))
  6673. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6674. (mapcar (lambda (x) (list (concat x ":")))
  6675. org-link-types))
  6676. nil nil nil
  6677. 'tmphist
  6678. (or (car (car org-stored-links))))))
  6679. (set-window-configuration wcf)
  6680. (kill-buffer "*Org Links*"))
  6681. (setq entry (assoc link org-stored-links))
  6682. (or entry (push link org-insert-link-history))
  6683. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6684. (not org-keep-stored-link-after-insertion))
  6685. (setq org-stored-links (delq (assoc link org-stored-links)
  6686. org-stored-links)))
  6687. (setq desc (or desc (nth 1 entry)))))
  6688. (if (string-match org-plain-link-re link)
  6689. ;; URL-like link, normalize the use of angular brackets.
  6690. (setq link (org-make-link (org-remove-angle-brackets link))))
  6691. ;; Check if we are linking to the current file with a search option
  6692. ;; If yes, simplify the link by using only the search option.
  6693. (when (and buffer-file-name
  6694. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6695. (let* ((path (match-string 1 link))
  6696. (case-fold-search nil)
  6697. (search (match-string 2 link)))
  6698. (save-match-data
  6699. (if (equal (file-truename buffer-file-name) (file-truename path))
  6700. ;; We are linking to this same file, with a search option
  6701. (setq link search)))))
  6702. ;; Check if we can/should use a relative path. If yes, simplify the link
  6703. (when (string-match "^file:\\(.*\\)" link)
  6704. (let* ((path (match-string 1 link))
  6705. (origpath path)
  6706. (case-fold-search nil))
  6707. (cond
  6708. ((or (eq org-link-file-path-type 'absolute)
  6709. (equal complete-file '(16)))
  6710. (setq path (abbreviate-file-name (expand-file-name path))))
  6711. ((eq org-link-file-path-type 'noabbrev)
  6712. (setq path (expand-file-name path)))
  6713. ((eq org-link-file-path-type 'relative)
  6714. (setq path (file-relative-name path)))
  6715. (t
  6716. (save-match-data
  6717. (if (string-match (concat "^" (regexp-quote
  6718. (file-name-as-directory
  6719. (expand-file-name "."))))
  6720. (expand-file-name path))
  6721. ;; We are linking a file with relative path name.
  6722. (setq path (substring (expand-file-name path)
  6723. (match-end 0)))
  6724. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6725. (setq link (concat "file:" path))
  6726. (if (equal desc origpath)
  6727. (setq desc path))))
  6728. (if org-make-link-description-function
  6729. (setq desc (funcall org-make-link-description-function link desc)))
  6730. (setq desc (read-string "Description: " desc))
  6731. (unless (string-match "\\S-" desc) (setq desc nil))
  6732. (if remove (apply 'delete-region remove))
  6733. (insert (org-make-link-string link desc))))
  6734. (defun org-completing-read (&rest args)
  6735. "Completing-read with SPACE being a normal character."
  6736. (let ((minibuffer-local-completion-map
  6737. (copy-keymap minibuffer-local-completion-map)))
  6738. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6739. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6740. (apply 'org-ido-completing-read args)))
  6741. (defun org-completing-read-no-ido (&rest args)
  6742. (let (org-completion-use-ido)
  6743. (apply 'org-completing-read args)))
  6744. (defun org-ido-completing-read (&rest args)
  6745. "Completing-read using `ido-mode' speedups if available"
  6746. (if (and org-completion-use-ido
  6747. (fboundp 'ido-completing-read)
  6748. (boundp 'ido-mode) ido-mode
  6749. (listp (second args)))
  6750. (let ((ido-enter-matching-directory nil))
  6751. (apply 'ido-completing-read (concat (car args))
  6752. (mapcar (lambda (x) (car x)) (nth 1 args))
  6753. (cddr args)))
  6754. (apply 'completing-read args)))
  6755. (defun org-extract-attributes (s)
  6756. "Extract the attributes cookie from a string and set as text property."
  6757. (let (a attr (start 0) key value)
  6758. (save-match-data
  6759. (when (string-match "{{\\([^}]+\\)}}$" s)
  6760. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6761. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6762. (setq key (match-string 1 a) value (match-string 2 a)
  6763. start (match-end 0)
  6764. attr (plist-put attr (intern key) value))))
  6765. (org-add-props s nil 'org-attr attr))
  6766. s))
  6767. (defun org-extract-attributes-from-string (tag)
  6768. (let (key value attr)
  6769. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6770. (setq key (match-string 1 tag) value (match-string 2 tag)
  6771. tag (replace-match "" t t tag)
  6772. attr (plist-put attr (intern key) value)))
  6773. (cons tag attr)))
  6774. (defun org-attributes-to-string (plist)
  6775. "Format a property list into an HTML attribute list."
  6776. (let ((s "") key value)
  6777. (while plist
  6778. (setq key (pop plist) value (pop plist))
  6779. (and value
  6780. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6781. s))
  6782. ;;; Opening/following a link
  6783. (defvar org-link-search-failed nil)
  6784. (defun org-next-link ()
  6785. "Move forward to the next link.
  6786. If the link is in hidden text, expose it."
  6787. (interactive)
  6788. (when (and org-link-search-failed (eq this-command last-command))
  6789. (goto-char (point-min))
  6790. (message "Link search wrapped back to beginning of buffer"))
  6791. (setq org-link-search-failed nil)
  6792. (let* ((pos (point))
  6793. (ct (org-context))
  6794. (a (assoc :link ct)))
  6795. (if a (goto-char (nth 2 a)))
  6796. (if (re-search-forward org-any-link-re nil t)
  6797. (progn
  6798. (goto-char (match-beginning 0))
  6799. (if (org-invisible-p) (org-show-context)))
  6800. (goto-char pos)
  6801. (setq org-link-search-failed t)
  6802. (error "No further link found"))))
  6803. (defun org-previous-link ()
  6804. "Move backward to the previous link.
  6805. If the link is in hidden text, expose it."
  6806. (interactive)
  6807. (when (and org-link-search-failed (eq this-command last-command))
  6808. (goto-char (point-max))
  6809. (message "Link search wrapped back to end of buffer"))
  6810. (setq org-link-search-failed nil)
  6811. (let* ((pos (point))
  6812. (ct (org-context))
  6813. (a (assoc :link ct)))
  6814. (if a (goto-char (nth 1 a)))
  6815. (if (re-search-backward org-any-link-re nil t)
  6816. (progn
  6817. (goto-char (match-beginning 0))
  6818. (if (org-invisible-p) (org-show-context)))
  6819. (goto-char pos)
  6820. (setq org-link-search-failed t)
  6821. (error "No further link found"))))
  6822. (defun org-translate-link (s)
  6823. "Translate a link string if a translation function has been defined."
  6824. (if (and org-link-translation-function
  6825. (fboundp org-link-translation-function)
  6826. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6827. (progn
  6828. (setq s (funcall org-link-translation-function
  6829. (match-string 1) (match-string 2)))
  6830. (concat (car s) ":" (cdr s)))
  6831. s))
  6832. (defun org-translate-link-from-planner (type path)
  6833. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6834. This is still an experimental function, your mileage may vary."
  6835. (cond
  6836. ((member type '("http" "https" "news" "ftp"))
  6837. ;; standard Internet links are the same.
  6838. nil)
  6839. ((and (equal type "irc") (string-match "^//" path))
  6840. ;; Planner has two / at the beginning of an irc link, we have 1.
  6841. ;; We should have zero, actually....
  6842. (setq path (substring path 1)))
  6843. ((and (equal type "lisp") (string-match "^/" path))
  6844. ;; Planner has a slash, we do not.
  6845. (setq type "elisp" path (substring path 1)))
  6846. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6847. ;; A typical message link. Planner has the id after the fina slash,
  6848. ;; we separate it with a hash mark
  6849. (setq path (concat (match-string 1 path) "#"
  6850. (org-remove-angle-brackets (match-string 2 path)))))
  6851. )
  6852. (cons type path))
  6853. (defun org-find-file-at-mouse (ev)
  6854. "Open file link or URL at mouse."
  6855. (interactive "e")
  6856. (mouse-set-point ev)
  6857. (org-open-at-point 'in-emacs))
  6858. (defun org-open-at-mouse (ev)
  6859. "Open file link or URL at mouse."
  6860. (interactive "e")
  6861. (mouse-set-point ev)
  6862. (if (eq major-mode 'org-agenda-mode)
  6863. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6864. (org-open-at-point))
  6865. (defvar org-window-config-before-follow-link nil
  6866. "The window configuration before following a link.
  6867. This is saved in case the need arises to restore it.")
  6868. (defvar org-open-link-marker (make-marker)
  6869. "Marker pointing to the location where `org-open-at-point; was called.")
  6870. ;;;###autoload
  6871. (defun org-open-at-point-global ()
  6872. "Follow a link like Org-mode does.
  6873. This command can be called in any mode to follow a link that has
  6874. Org-mode syntax."
  6875. (interactive)
  6876. (org-run-like-in-org-mode 'org-open-at-point))
  6877. ;;;###autoload
  6878. (defun org-open-link-from-string (s &optional arg)
  6879. "Open a link in the string S, as if it was in Org-mode."
  6880. (interactive "sLink: \nP")
  6881. (let ((reference-buffer (current-buffer)))
  6882. (with-temp-buffer
  6883. (let ((org-inhibit-startup t))
  6884. (org-mode)
  6885. (insert s)
  6886. (goto-char (point-min))
  6887. (org-open-at-point arg reference-buffer)))))
  6888. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6889. "Open link at or after point.
  6890. If there is no link at point, this function will search forward up to
  6891. the end of the current line.
  6892. Normally, files will be opened by an appropriate application. If the
  6893. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6894. With a double prefix argument, try to open outside of Emacs, in the
  6895. application the system uses for this file type."
  6896. (interactive "P")
  6897. (org-load-modules-maybe)
  6898. (move-marker org-open-link-marker (point))
  6899. (setq org-window-config-before-follow-link (current-window-configuration))
  6900. (org-remove-occur-highlights nil nil t)
  6901. (cond
  6902. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6903. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6904. (org-footnote-action))
  6905. (t
  6906. (let (type path link line search (pos (point)))
  6907. (catch 'match
  6908. (save-excursion
  6909. (skip-chars-forward "^]\n\r")
  6910. (when (org-in-regexp org-bracket-link-regexp)
  6911. (setq link (org-extract-attributes
  6912. (org-link-unescape (org-match-string-no-properties 1))))
  6913. (while (string-match " *\n *" link)
  6914. (setq link (replace-match " " t t link)))
  6915. (setq link (org-link-expand-abbrev link))
  6916. (cond
  6917. ((or (file-name-absolute-p link)
  6918. (string-match "^\\.\\.?/" link))
  6919. (setq type "file" path link))
  6920. ((string-match org-link-re-with-space3 link)
  6921. (setq type (match-string 1 link) path (match-string 2 link)))
  6922. (t (setq type "thisfile" path link)))
  6923. (throw 'match t)))
  6924. (when (get-text-property (point) 'org-linked-text)
  6925. (setq type "thisfile"
  6926. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6927. (1+ (point)) (point))
  6928. path (buffer-substring
  6929. (previous-single-property-change pos 'org-linked-text)
  6930. (next-single-property-change pos 'org-linked-text)))
  6931. (throw 'match t))
  6932. (save-excursion
  6933. (when (or (org-in-regexp org-angle-link-re)
  6934. (org-in-regexp org-plain-link-re))
  6935. (setq type (match-string 1) path (match-string 2))
  6936. (throw 'match t)))
  6937. (save-excursion
  6938. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6939. (setq type "tags"
  6940. path (match-string 1))
  6941. (while (string-match ":" path)
  6942. (setq path (replace-match "+" t t path)))
  6943. (throw 'match t)))
  6944. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6945. (setq type "tree-match"
  6946. path (match-string 1))
  6947. (throw 'match t)))
  6948. (unless path
  6949. (error "No link found"))
  6950. ;; switch back to reference buffer
  6951. ;; needed when if called in a temporary buffer through
  6952. ;; org-open-link-from-string
  6953. (and reference-buffer (switch-to-buffer reference-buffer))
  6954. ;; Remove any trailing spaces in path
  6955. (if (string-match " +\\'" path)
  6956. (setq path (replace-match "" t t path)))
  6957. (if (and org-link-translation-function
  6958. (fboundp org-link-translation-function))
  6959. ;; Check if we need to translate the link
  6960. (let ((tmp (funcall org-link-translation-function type path)))
  6961. (setq type (car tmp) path (cdr tmp))))
  6962. (cond
  6963. ((assoc type org-link-protocols)
  6964. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6965. ((equal type "mailto")
  6966. (let ((cmd (car org-link-mailto-program))
  6967. (args (cdr org-link-mailto-program)) args1
  6968. (address path) (subject "") a)
  6969. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6970. (setq address (match-string 1 path)
  6971. subject (org-link-escape (match-string 2 path))))
  6972. (while args
  6973. (cond
  6974. ((not (stringp (car args))) (push (pop args) args1))
  6975. (t (setq a (pop args))
  6976. (if (string-match "%a" a)
  6977. (setq a (replace-match address t t a)))
  6978. (if (string-match "%s" a)
  6979. (setq a (replace-match subject t t a)))
  6980. (push a args1))))
  6981. (apply cmd (nreverse args1))))
  6982. ((member type '("http" "https" "ftp" "news"))
  6983. (browse-url (concat type ":" (org-link-escape
  6984. path org-link-escape-chars-browser))))
  6985. ((member type '("message"))
  6986. (browse-url (concat type ":" path)))
  6987. ((string= type "tags")
  6988. (org-tags-view in-emacs path))
  6989. ((string= type "thisfile")
  6990. (if in-emacs
  6991. (switch-to-buffer-other-window
  6992. (org-get-buffer-for-internal-link (current-buffer)))
  6993. (org-mark-ring-push))
  6994. (let ((cmd `(org-link-search
  6995. ,path
  6996. ,(cond ((equal in-emacs '(4)) 'occur)
  6997. ((equal in-emacs '(16)) 'org-occur)
  6998. (t nil))
  6999. ,pos)))
  7000. (condition-case nil (eval cmd)
  7001. (error (progn (widen) (eval cmd))))))
  7002. ((string= type "tree-match")
  7003. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7004. ((string= type "file")
  7005. (if (string-match "::\\([0-9]+\\)\\'" path)
  7006. (setq line (string-to-number (match-string 1 path))
  7007. path (substring path 0 (match-beginning 0)))
  7008. (if (string-match "::\\(.+\\)\\'" path)
  7009. (setq search (match-string 1 path)
  7010. path (substring path 0 (match-beginning 0)))))
  7011. (if (string-match "[*?{]" (file-name-nondirectory path))
  7012. (dired path)
  7013. (org-open-file path in-emacs line search)))
  7014. ((string= type "news")
  7015. (require 'org-gnus)
  7016. (org-gnus-follow-link path))
  7017. ((string= type "shell")
  7018. (let ((cmd path))
  7019. (if (or (not org-confirm-shell-link-function)
  7020. (funcall org-confirm-shell-link-function
  7021. (format "Execute \"%s\" in shell? "
  7022. (org-add-props cmd nil
  7023. 'face 'org-warning))))
  7024. (progn
  7025. (message "Executing %s" cmd)
  7026. (shell-command cmd))
  7027. (error "Abort"))))
  7028. ((string= type "elisp")
  7029. (let ((cmd path))
  7030. (if (or (not org-confirm-elisp-link-function)
  7031. (funcall org-confirm-elisp-link-function
  7032. (format "Execute \"%s\" as elisp? "
  7033. (org-add-props cmd nil
  7034. 'face 'org-warning))))
  7035. (message "%s => %s" cmd
  7036. (if (equal (string-to-char cmd) ?\()
  7037. (eval (read cmd))
  7038. (call-interactively (read cmd))))
  7039. (error "Abort"))))
  7040. (t
  7041. (browse-url-at-point))))))
  7042. (move-marker org-open-link-marker nil)
  7043. (run-hook-with-args 'org-follow-link-hook))
  7044. ;;;; Time estimates
  7045. (defun org-get-effort (&optional pom)
  7046. "Get the effort estimate for the current entry."
  7047. (org-entry-get pom org-effort-property))
  7048. ;;; File search
  7049. (defvar org-create-file-search-functions nil
  7050. "List of functions to construct the right search string for a file link.
  7051. These functions are called in turn with point at the location to
  7052. which the link should point.
  7053. A function in the hook should first test if it would like to
  7054. handle this file type, for example by checking the major-mode or
  7055. the file extension. If it decides not to handle this file, it
  7056. should just return nil to give other functions a chance. If it
  7057. does handle the file, it must return the search string to be used
  7058. when following the link. The search string will be part of the
  7059. file link, given after a double colon, and `org-open-at-point'
  7060. will automatically search for it. If special measures must be
  7061. taken to make the search successful, another function should be
  7062. added to the companion hook `org-execute-file-search-functions',
  7063. which see.
  7064. A function in this hook may also use `setq' to set the variable
  7065. `description' to provide a suggestion for the descriptive text to
  7066. be used for this link when it gets inserted into an Org-mode
  7067. buffer with \\[org-insert-link].")
  7068. (defvar org-execute-file-search-functions nil
  7069. "List of functions to execute a file search triggered by a link.
  7070. Functions added to this hook must accept a single argument, the
  7071. search string that was part of the file link, the part after the
  7072. double colon. The function must first check if it would like to
  7073. handle this search, for example by checking the major-mode or the
  7074. file extension. If it decides not to handle this search, it
  7075. should just return nil to give other functions a chance. If it
  7076. does handle the search, it must return a non-nil value to keep
  7077. other functions from trying.
  7078. Each function can access the current prefix argument through the
  7079. variable `current-prefix-argument'. Note that a single prefix is
  7080. used to force opening a link in Emacs, so it may be good to only
  7081. use a numeric or double prefix to guide the search function.
  7082. In case this is needed, a function in this hook can also restore
  7083. the window configuration before `org-open-at-point' was called using:
  7084. (set-window-configuration org-window-config-before-follow-link)")
  7085. (defun org-link-search (s &optional type avoid-pos)
  7086. "Search for a link search option.
  7087. If S is surrounded by forward slashes, it is interpreted as a
  7088. regular expression. In org-mode files, this will create an `org-occur'
  7089. sparse tree. In ordinary files, `occur' will be used to list matches.
  7090. If the current buffer is in `dired-mode', grep will be used to search
  7091. in all files. If AVOID-POS is given, ignore matches near that position."
  7092. (let ((case-fold-search t)
  7093. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7094. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7095. (append '(("") (" ") ("\t") ("\n"))
  7096. org-emphasis-alist)
  7097. "\\|") "\\)"))
  7098. (pos (point))
  7099. (pre nil) (post nil)
  7100. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7101. (cond
  7102. ;; First check if there are any special
  7103. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7104. ;; Now try the builtin stuff
  7105. ((and (equal (string-to-char s0) ?#)
  7106. (> (length s0) 1)
  7107. (save-excursion
  7108. (goto-char (point-min))
  7109. (and
  7110. (re-search-forward
  7111. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7112. (setq type 'dedicated
  7113. pos (match-beginning 0))))
  7114. ;; There is an exact target for this
  7115. (goto-char pos)
  7116. (org-back-to-heading t)))
  7117. ((save-excursion
  7118. (goto-char (point-min))
  7119. (and
  7120. (re-search-forward
  7121. (concat "<<" (regexp-quote s0) ">>") nil t)
  7122. (setq type 'dedicated
  7123. pos (match-beginning 0))))
  7124. ;; There is an exact target for this
  7125. (goto-char pos))
  7126. ((and (string-match "^(\\(.*\\))$" s0)
  7127. (save-excursion
  7128. (goto-char (point-min))
  7129. (and
  7130. (re-search-forward
  7131. (concat "[^[]" (regexp-quote
  7132. (format org-coderef-label-format
  7133. (match-string 1 s0))))
  7134. nil t)
  7135. (setq type 'dedicated
  7136. pos (1+ (match-beginning 0))))))
  7137. ;; There is a coderef target for this
  7138. (goto-char pos))
  7139. ((string-match "^/\\(.*\\)/$" s)
  7140. ;; A regular expression
  7141. (cond
  7142. ((org-mode-p)
  7143. (org-occur (match-string 1 s)))
  7144. ;;((eq major-mode 'dired-mode)
  7145. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7146. (t (org-do-occur (match-string 1 s)))))
  7147. (t
  7148. ;; A normal search strings
  7149. (when (equal (string-to-char s) ?*)
  7150. ;; Anchor on headlines, post may include tags.
  7151. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7152. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7153. s (substring s 1)))
  7154. (remove-text-properties
  7155. 0 (length s)
  7156. '(face nil mouse-face nil keymap nil fontified nil) s)
  7157. ;; Make a series of regular expressions to find a match
  7158. (setq words (org-split-string s "[ \n\r\t]+")
  7159. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7160. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7161. "\\)" markers)
  7162. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7163. re2a (concat "[ \t\r\n]" re2a_)
  7164. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7165. re4 (concat "[^a-zA-Z_]" re4_)
  7166. re1 (concat pre re2 post)
  7167. re3 (concat pre (if pre re4_ re4) post)
  7168. re5 (concat pre ".*" re4)
  7169. re2 (concat pre re2)
  7170. re2a (concat pre (if pre re2a_ re2a))
  7171. re4 (concat pre (if pre re4_ re4))
  7172. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7173. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7174. re5 "\\)"
  7175. ))
  7176. (cond
  7177. ((eq type 'org-occur) (org-occur reall))
  7178. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7179. (t (goto-char (point-min))
  7180. (setq type 'fuzzy)
  7181. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7182. (org-search-not-self 1 re1 nil t)
  7183. (org-search-not-self 1 re2 nil t)
  7184. (org-search-not-self 1 re2a nil t)
  7185. (org-search-not-self 1 re3 nil t)
  7186. (org-search-not-self 1 re4 nil t)
  7187. (org-search-not-self 1 re5 nil t)
  7188. )
  7189. (goto-char (match-beginning 1))
  7190. (goto-char pos)
  7191. (error "No match")))))
  7192. (t
  7193. ;; Normal string-search
  7194. (goto-char (point-min))
  7195. (if (search-forward s nil t)
  7196. (goto-char (match-beginning 0))
  7197. (error "No match"))))
  7198. (and (org-mode-p) (org-show-context 'link-search))
  7199. type))
  7200. (defun org-search-not-self (group &rest args)
  7201. "Execute `re-search-forward', but only accept matches that do not
  7202. enclose the position of `org-open-link-marker'."
  7203. (let ((m org-open-link-marker))
  7204. (catch 'exit
  7205. (while (apply 're-search-forward args)
  7206. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7207. (goto-char (match-end group))
  7208. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7209. (> (match-beginning 0) (marker-position m))
  7210. (< (match-end 0) (marker-position m)))
  7211. (save-match-data
  7212. (or (not (org-in-regexp
  7213. org-bracket-link-analytic-regexp 1))
  7214. (not (match-end 4)) ; no description
  7215. (and (<= (match-beginning 4) (point))
  7216. (>= (match-end 4) (point))))))
  7217. (throw 'exit (point))))))))
  7218. (defun org-get-buffer-for-internal-link (buffer)
  7219. "Return a buffer to be used for displaying the link target of internal links."
  7220. (cond
  7221. ((not org-display-internal-link-with-indirect-buffer)
  7222. buffer)
  7223. ((string-match "(Clone)$" (buffer-name buffer))
  7224. (message "Buffer is already a clone, not making another one")
  7225. ;; we also do not modify visibility in this case
  7226. buffer)
  7227. (t ; make a new indirect buffer for displaying the link
  7228. (let* ((bn (buffer-name buffer))
  7229. (ibn (concat bn "(Clone)"))
  7230. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7231. (with-current-buffer ib (org-overview))
  7232. ib))))
  7233. (defun org-do-occur (regexp &optional cleanup)
  7234. "Call the Emacs command `occur'.
  7235. If CLEANUP is non-nil, remove the printout of the regular expression
  7236. in the *Occur* buffer. This is useful if the regex is long and not useful
  7237. to read."
  7238. (occur regexp)
  7239. (when cleanup
  7240. (let ((cwin (selected-window)) win beg end)
  7241. (when (setq win (get-buffer-window "*Occur*"))
  7242. (select-window win))
  7243. (goto-char (point-min))
  7244. (when (re-search-forward "match[a-z]+" nil t)
  7245. (setq beg (match-end 0))
  7246. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7247. (setq end (1- (match-beginning 0)))))
  7248. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7249. (goto-char (point-min))
  7250. (select-window cwin))))
  7251. ;;; The mark ring for links jumps
  7252. (defvar org-mark-ring nil
  7253. "Mark ring for positions before jumps in Org-mode.")
  7254. (defvar org-mark-ring-last-goto nil
  7255. "Last position in the mark ring used to go back.")
  7256. ;; Fill and close the ring
  7257. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7258. (loop for i from 1 to org-mark-ring-length do
  7259. (push (make-marker) org-mark-ring))
  7260. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7261. org-mark-ring)
  7262. (defun org-mark-ring-push (&optional pos buffer)
  7263. "Put the current position or POS into the mark ring and rotate it."
  7264. (interactive)
  7265. (setq pos (or pos (point)))
  7266. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7267. (move-marker (car org-mark-ring)
  7268. (or pos (point))
  7269. (or buffer (current-buffer)))
  7270. (message "%s"
  7271. (substitute-command-keys
  7272. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7273. (defun org-mark-ring-goto (&optional n)
  7274. "Jump to the previous position in the mark ring.
  7275. With prefix arg N, jump back that many stored positions. When
  7276. called several times in succession, walk through the entire ring.
  7277. Org-mode commands jumping to a different position in the current file,
  7278. or to another Org-mode file, automatically push the old position
  7279. onto the ring."
  7280. (interactive "p")
  7281. (let (p m)
  7282. (if (eq last-command this-command)
  7283. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7284. (setq p org-mark-ring))
  7285. (setq org-mark-ring-last-goto p)
  7286. (setq m (car p))
  7287. (switch-to-buffer (marker-buffer m))
  7288. (goto-char m)
  7289. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7290. (defun org-remove-angle-brackets (s)
  7291. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7292. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7293. s)
  7294. (defun org-add-angle-brackets (s)
  7295. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7296. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7297. s)
  7298. (defun org-remove-double-quotes (s)
  7299. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7300. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7301. s)
  7302. ;;; Following specific links
  7303. (defun org-follow-timestamp-link ()
  7304. (cond
  7305. ((org-at-date-range-p t)
  7306. (let ((org-agenda-start-on-weekday)
  7307. (t1 (match-string 1))
  7308. (t2 (match-string 2)))
  7309. (setq t1 (time-to-days (org-time-string-to-time t1))
  7310. t2 (time-to-days (org-time-string-to-time t2)))
  7311. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7312. ((org-at-timestamp-p t)
  7313. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7314. (substring (match-string 1) 0 10)))
  7315. 1))
  7316. (t (error "This should not happen"))))
  7317. ;;; Following file links
  7318. (defvar org-wait nil)
  7319. (defun org-open-file (path &optional in-emacs line search)
  7320. "Open the file at PATH.
  7321. First, this expands any special file name abbreviations. Then the
  7322. configuration variable `org-file-apps' is checked if it contains an
  7323. entry for this file type, and if yes, the corresponding command is launched.
  7324. If no application is found, Emacs simply visits the file.
  7325. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7326. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7327. and o use an external application to visit the file.
  7328. Optional LINE specifies a line to go to, optional SEARCH a string to
  7329. search for. If LINE or SEARCH is given, the file will always be
  7330. opened in Emacs.
  7331. If the file does not exist, an error is thrown."
  7332. (setq in-emacs (or in-emacs line search))
  7333. (let* ((file (if (equal path "")
  7334. buffer-file-name
  7335. (substitute-in-file-name (expand-file-name path))))
  7336. (apps (append org-file-apps (org-default-apps)))
  7337. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7338. (dirp (if remp nil (file-directory-p file)))
  7339. (file (if (and dirp org-open-directory-means-index-dot-org)
  7340. (concat (file-name-as-directory file) "index.org")
  7341. file))
  7342. (a-m-a-p (assq 'auto-mode apps))
  7343. (dfile (downcase file))
  7344. (old-buffer (current-buffer))
  7345. (old-pos (point))
  7346. (old-mode major-mode)
  7347. ext cmd)
  7348. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7349. (setq ext (match-string 1 dfile))
  7350. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7351. (setq ext (match-string 1 dfile))))
  7352. (cond
  7353. ((equal in-emacs '(16))
  7354. (setq cmd (cdr (assoc 'system apps))))
  7355. (in-emacs (setq cmd 'emacs))
  7356. (t
  7357. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7358. (and dirp (cdr (assoc 'directory apps)))
  7359. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7360. 'string-match)
  7361. (cdr (assoc ext apps))
  7362. (cdr (assoc t apps))))))
  7363. (when (eq cmd 'system)
  7364. (setq cmd (cdr (assoc 'system apps))))
  7365. (when (eq cmd 'default)
  7366. (setq cmd (cdr (assoc t apps))))
  7367. (when (eq cmd 'mailcap)
  7368. (require 'mailcap)
  7369. (mailcap-parse-mailcaps)
  7370. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7371. (command (mailcap-mime-info mime-type)))
  7372. (if (stringp command)
  7373. (setq cmd command)
  7374. (setq cmd 'emacs))))
  7375. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7376. (not (file-exists-p file))
  7377. (not org-open-non-existing-files))
  7378. (error "No such file: %s" file))
  7379. (cond
  7380. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7381. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7382. (while (string-match "['\"]%s['\"]" cmd)
  7383. (setq cmd (replace-match "%s" t t cmd)))
  7384. (while (string-match "%s" cmd)
  7385. (setq cmd (replace-match
  7386. (save-match-data
  7387. (shell-quote-argument
  7388. (convert-standard-filename file)))
  7389. t t cmd)))
  7390. (save-window-excursion
  7391. (start-process-shell-command cmd nil cmd)
  7392. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7393. ))
  7394. ((or (stringp cmd)
  7395. (eq cmd 'emacs))
  7396. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7397. (widen)
  7398. (if line (goto-line line)
  7399. (if search (org-link-search search))))
  7400. ((consp cmd)
  7401. (let ((file (convert-standard-filename file)))
  7402. (eval cmd)))
  7403. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7404. (and (org-mode-p) (eq old-mode 'org-mode)
  7405. (or (not (equal old-buffer (current-buffer)))
  7406. (not (equal old-pos (point))))
  7407. (org-mark-ring-push old-pos old-buffer))))
  7408. (defun org-default-apps ()
  7409. "Return the default applications for this operating system."
  7410. (cond
  7411. ((eq system-type 'darwin)
  7412. org-file-apps-defaults-macosx)
  7413. ((eq system-type 'windows-nt)
  7414. org-file-apps-defaults-windowsnt)
  7415. (t org-file-apps-defaults-gnu)))
  7416. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7417. "Convert extensions to regular expressions in the cars of LIST.
  7418. Also, weed out any non-string entries, because the return value is used
  7419. only for regexp matching.
  7420. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7421. point to the symbol `emacs', indicating that the file should
  7422. be opened in Emacs."
  7423. (append
  7424. (delq nil
  7425. (mapcar (lambda (x)
  7426. (if (not (stringp (car x)))
  7427. nil
  7428. (if (string-match "\\W" (car x))
  7429. x
  7430. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7431. list))
  7432. (if add-auto-mode
  7433. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7434. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7435. (defun org-file-remote-p (file)
  7436. "Test whether FILE specifies a location on a remote system.
  7437. Return non-nil if the location is indeed remote.
  7438. For example, the filename \"/user@host:/foo\" specifies a location
  7439. on the system \"/user@host:\"."
  7440. (cond ((fboundp 'file-remote-p)
  7441. (file-remote-p file))
  7442. ((fboundp 'tramp-handle-file-remote-p)
  7443. (tramp-handle-file-remote-p file))
  7444. ((and (boundp 'ange-ftp-name-format)
  7445. (string-match (car ange-ftp-name-format) file))
  7446. t)
  7447. (t nil)))
  7448. ;;;; Refiling
  7449. (defun org-get-org-file ()
  7450. "Read a filename, with default directory `org-directory'."
  7451. (let ((default (or org-default-notes-file remember-data-file)))
  7452. (read-file-name (format "File name [%s]: " default)
  7453. (file-name-as-directory org-directory)
  7454. default)))
  7455. (defun org-notes-order-reversed-p ()
  7456. "Check if the current file should receive notes in reversed order."
  7457. (cond
  7458. ((not org-reverse-note-order) nil)
  7459. ((eq t org-reverse-note-order) t)
  7460. ((not (listp org-reverse-note-order)) nil)
  7461. (t (catch 'exit
  7462. (let ((all org-reverse-note-order)
  7463. entry)
  7464. (while (setq entry (pop all))
  7465. (if (string-match (car entry) buffer-file-name)
  7466. (throw 'exit (cdr entry))))
  7467. nil)))))
  7468. (defvar org-refile-target-table nil
  7469. "The list of refile targets, created by `org-refile'.")
  7470. (defvar org-agenda-new-buffers nil
  7471. "Buffers created to visit agenda files.")
  7472. (defun org-get-refile-targets (&optional default-buffer)
  7473. "Produce a table with refile targets."
  7474. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7475. targets txt re files f desc descre fast-path-p level pos0)
  7476. (message "Getting targets...")
  7477. (with-current-buffer (or default-buffer (current-buffer))
  7478. (while (setq entry (pop entries))
  7479. (setq files (car entry) desc (cdr entry))
  7480. (setq fast-path-p nil)
  7481. (cond
  7482. ((null files) (setq files (list (current-buffer))))
  7483. ((eq files 'org-agenda-files)
  7484. (setq files (org-agenda-files 'unrestricted)))
  7485. ((and (symbolp files) (fboundp files))
  7486. (setq files (funcall files)))
  7487. ((and (symbolp files) (boundp files))
  7488. (setq files (symbol-value files))))
  7489. (if (stringp files) (setq files (list files)))
  7490. (cond
  7491. ((eq (car desc) :tag)
  7492. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7493. ((eq (car desc) :todo)
  7494. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7495. ((eq (car desc) :regexp)
  7496. (setq descre (cdr desc)))
  7497. ((eq (car desc) :level)
  7498. (setq descre (concat "^\\*\\{" (number-to-string
  7499. (if org-odd-levels-only
  7500. (1- (* 2 (cdr desc)))
  7501. (cdr desc)))
  7502. "\\}[ \t]")))
  7503. ((eq (car desc) :maxlevel)
  7504. (setq fast-path-p t)
  7505. (setq descre (concat "^\\*\\{1," (number-to-string
  7506. (if org-odd-levels-only
  7507. (1- (* 2 (cdr desc)))
  7508. (cdr desc)))
  7509. "\\}[ \t]")))
  7510. (t (error "Bad refiling target description %s" desc)))
  7511. (while (setq f (pop files))
  7512. (save-excursion
  7513. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7514. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7515. (setq f (expand-file-name f))
  7516. (if (eq org-refile-use-outline-path 'file)
  7517. (push (list (file-name-nondirectory f) f nil nil) targets))
  7518. (save-excursion
  7519. (save-restriction
  7520. (widen)
  7521. (goto-char (point-min))
  7522. (while (re-search-forward descre nil t)
  7523. (goto-char (setq pos0 (point-at-bol)))
  7524. (catch 'next
  7525. (when org-refile-target-verify-function
  7526. (save-match-data
  7527. (or (funcall org-refile-target-verify-function)
  7528. (throw 'next t))))
  7529. (when (looking-at org-complex-heading-regexp)
  7530. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7531. txt (org-link-display-format (match-string 4))
  7532. re (concat "^" (regexp-quote
  7533. (buffer-substring (match-beginning 1)
  7534. (match-end 4)))))
  7535. (if (match-end 5) (setq re (concat re "[ \t]+"
  7536. (regexp-quote
  7537. (match-string 5)))))
  7538. (setq re (concat re "[ \t]*$"))
  7539. (when org-refile-use-outline-path
  7540. (setq txt (mapconcat 'org-protect-slash
  7541. (append
  7542. (if (eq org-refile-use-outline-path 'file)
  7543. (list (file-name-nondirectory
  7544. (buffer-file-name (buffer-base-buffer))))
  7545. (if (eq org-refile-use-outline-path 'full-file-path)
  7546. (list (buffer-file-name (buffer-base-buffer)))))
  7547. (org-get-outline-path fast-path-p level txt)
  7548. (list txt))
  7549. "/")))
  7550. (push (list txt f re (point)) targets)))
  7551. (when (= (point) pos0)
  7552. ;; verification function has not moved point
  7553. (goto-char (point-at-eol))))))))))
  7554. (message "Getting targets...done")
  7555. (nreverse targets)))
  7556. (defun org-protect-slash (s)
  7557. (while (string-match "/" s)
  7558. (setq s (replace-match "\\" t t s)))
  7559. s)
  7560. (defvar org-olpa (make-vector 20 nil))
  7561. (defun org-get-outline-path (&optional fastp level heading)
  7562. "Return the outline path to the current entry, as a list."
  7563. (if fastp
  7564. (progn
  7565. (if (> level 19)
  7566. (error "Outline path failure, more than 19 levels."))
  7567. (loop for i from level upto 19 do
  7568. (aset org-olpa i nil))
  7569. (prog1
  7570. (delq nil (append org-olpa nil))
  7571. (aset org-olpa level heading)))
  7572. (let (rtn)
  7573. (save-excursion
  7574. (while (org-up-heading-safe)
  7575. (when (looking-at org-complex-heading-regexp)
  7576. (push (org-match-string-no-properties 4) rtn)))
  7577. rtn))))
  7578. (defvar org-refile-history nil
  7579. "History for refiling operations.")
  7580. (defun org-refile (&optional goto default-buffer)
  7581. "Move the entry at point to another heading.
  7582. The list of target headings is compiled using the information in
  7583. `org-refile-targets', which see. This list is created before each use
  7584. and will therefore always be up-to-date.
  7585. At the target location, the entry is filed as a subitem of the target heading.
  7586. Depending on `org-reverse-note-order', the new subitem will either be the
  7587. first or the last subitem.
  7588. If there is an active region, all entries in that region will be moved.
  7589. However, the region must fulfil the requirement that the first heading
  7590. is the first one sets the top-level of the moved text - at most siblings
  7591. below it are allowed.
  7592. With prefix arg GOTO, the command will only visit the target location,
  7593. not actually move anything.
  7594. With a double prefix `C-u C-u', go to the location where the last refiling
  7595. operation has put the subtree.
  7596. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7597. (interactive "P")
  7598. (let* ((cbuf (current-buffer))
  7599. (regionp (org-region-active-p))
  7600. (region-start (and regionp (region-beginning)))
  7601. (region-end (and regionp (region-end)))
  7602. (region-length (and regionp (- region-end region-start)))
  7603. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7604. pos it nbuf file re level reversed)
  7605. (when regionp (goto-char region-start)
  7606. (unless (org-kill-is-subtree-p
  7607. (buffer-substring region-start region-end))
  7608. (error "The region is not a (sequence of) subtree(s)")))
  7609. (if (equal goto '(16))
  7610. (org-refile-goto-last-stored)
  7611. (when (setq it (org-refile-get-location
  7612. (if goto "Goto: " "Refile to: ") default-buffer
  7613. org-refile-allow-creating-parent-nodes))
  7614. (setq file (nth 1 it)
  7615. re (nth 2 it)
  7616. pos (nth 3 it))
  7617. (if (and (not goto)
  7618. pos
  7619. (equal (buffer-file-name) file)
  7620. (if regionp
  7621. (and (>= pos region-start)
  7622. (<= pos region-end))
  7623. (and (>= pos (point))
  7624. (< pos (save-excursion
  7625. (org-end-of-subtree t t))))))
  7626. (error "Cannot refile to position inside the tree or region"))
  7627. (setq nbuf (or (find-buffer-visiting file)
  7628. (find-file-noselect file)))
  7629. (if goto
  7630. (progn
  7631. (switch-to-buffer nbuf)
  7632. (goto-char pos)
  7633. (org-show-context 'org-goto))
  7634. (if regionp
  7635. (progn
  7636. (kill-new (buffer-substring region-start region-end))
  7637. (org-save-markers-in-region region-start region-end))
  7638. (org-copy-subtree 1 nil t))
  7639. (save-excursion
  7640. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7641. (find-file-noselect file))))
  7642. (setq reversed (org-notes-order-reversed-p))
  7643. (save-excursion
  7644. (save-restriction
  7645. (widen)
  7646. (if pos
  7647. (progn
  7648. (goto-char pos)
  7649. (looking-at outline-regexp)
  7650. (setq level (org-get-valid-level (funcall outline-level) 1))
  7651. (goto-char
  7652. (if reversed
  7653. (or (outline-next-heading) (point-max))
  7654. (or (save-excursion (outline-get-next-sibling))
  7655. (org-end-of-subtree t t)
  7656. (point-max)))))
  7657. (setq level 1)
  7658. (if (not reversed)
  7659. (goto-char (point-max))
  7660. (goto-char (point-min))
  7661. (or (outline-next-heading) (goto-char (point-max)))))
  7662. (if (not (bolp)) (newline))
  7663. (bookmark-set "org-refile-last-stored")
  7664. (org-paste-subtree level))))
  7665. (if regionp
  7666. (delete-region (point) (+ (point) region-length))
  7667. (org-cut-subtree))
  7668. (setq org-markers-to-move nil)
  7669. (message "Refiled to \"%s\"" (car it))))))
  7670. (org-reveal))
  7671. (defun org-refile-goto-last-stored ()
  7672. "Go to the location where the last refile was stored."
  7673. (interactive)
  7674. (bookmark-jump "org-refile-last-stored")
  7675. (message "This is the location of the last refile"))
  7676. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7677. "Prompt the user for a refile location, using PROMPT."
  7678. (let ((org-refile-targets org-refile-targets)
  7679. (org-refile-use-outline-path org-refile-use-outline-path))
  7680. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7681. (unless org-refile-target-table
  7682. (error "No refile targets"))
  7683. (let* ((cbuf (current-buffer))
  7684. (partial-completion-mode nil)
  7685. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7686. (cfunc (if (and org-refile-use-outline-path
  7687. org-outline-path-complete-in-steps)
  7688. 'org-olpath-completing-read
  7689. 'org-ido-completing-read))
  7690. (extra (if org-refile-use-outline-path "/" ""))
  7691. (filename (and cfn (expand-file-name cfn)))
  7692. (tbl (mapcar
  7693. (lambda (x)
  7694. (if (and (not (member org-refile-use-outline-path
  7695. '(file full-file-path)))
  7696. (not (equal filename (nth 1 x))))
  7697. (cons (concat (car x) extra " ("
  7698. (file-name-nondirectory (nth 1 x)) ")")
  7699. (cdr x))
  7700. (cons (concat (car x) extra) (cdr x))))
  7701. org-refile-target-table))
  7702. (completion-ignore-case t)
  7703. pa answ parent-target child parent)
  7704. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7705. nil 'org-refile-history))
  7706. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7707. (if pa
  7708. pa
  7709. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7710. (setq parent (match-string 1 answ)
  7711. child (match-string 2 answ))
  7712. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7713. (when (and parent-target
  7714. (or (eq new-nodes t)
  7715. (and (eq new-nodes 'confirm)
  7716. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7717. (org-refile-new-child parent-target child))))))
  7718. (defun org-refile-new-child (parent-target child)
  7719. "Use refile target PARENT-TARGET to add new CHILD below it."
  7720. (unless parent-target
  7721. (error "Cannot find parent for new node"))
  7722. (let ((file (nth 1 parent-target))
  7723. (pos (nth 3 parent-target))
  7724. level)
  7725. (with-current-buffer (or (find-buffer-visiting file)
  7726. (find-file-noselect file))
  7727. (save-excursion
  7728. (save-restriction
  7729. (widen)
  7730. (if pos
  7731. (goto-char pos)
  7732. (goto-char (point-max))
  7733. (if (not (bolp)) (newline)))
  7734. (when (looking-at outline-regexp)
  7735. (setq level (funcall outline-level))
  7736. (org-end-of-subtree t t))
  7737. (org-back-over-empty-lines)
  7738. (insert "\n" (make-string
  7739. (if pos (org-get-valid-level level 1) 1) ?*)
  7740. " " child "\n")
  7741. (beginning-of-line 0)
  7742. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7743. (defun org-olpath-completing-read (prompt collection &rest args)
  7744. "Read an outline path like a file name."
  7745. (let ((thetable collection)
  7746. (org-completion-use-ido nil)) ; does not work with ido.
  7747. (apply
  7748. 'org-ido-completing-read prompt
  7749. (lambda (string predicate &optional flag)
  7750. (let (rtn r f (l (length string)))
  7751. (cond
  7752. ((eq flag nil)
  7753. ;; try completion
  7754. (try-completion string thetable))
  7755. ((eq flag t)
  7756. ;; all-completions
  7757. (setq rtn (all-completions string thetable predicate))
  7758. (mapcar
  7759. (lambda (x)
  7760. (setq r (substring x l))
  7761. (if (string-match " ([^)]*)$" x)
  7762. (setq f (match-string 0 x))
  7763. (setq f ""))
  7764. (if (string-match "/" r)
  7765. (concat string (substring r 0 (match-end 0)) f)
  7766. x))
  7767. rtn))
  7768. ((eq flag 'lambda)
  7769. ;; exact match?
  7770. (assoc string thetable)))
  7771. ))
  7772. args)))
  7773. ;;;; Dynamic blocks
  7774. (defun org-find-dblock (name)
  7775. "Find the first dynamic block with name NAME in the buffer.
  7776. If not found, stay at current position and return nil."
  7777. (let (pos)
  7778. (save-excursion
  7779. (goto-char (point-min))
  7780. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7781. nil t)
  7782. (match-beginning 0))))
  7783. (if pos (goto-char pos))
  7784. pos))
  7785. (defconst org-dblock-start-re
  7786. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7787. "Matches the startline of a dynamic block, with parameters.")
  7788. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7789. "Matches the end of a dynamic block.")
  7790. (defun org-create-dblock (plist)
  7791. "Create a dynamic block section, with parameters taken from PLIST.
  7792. PLIST must contain a :name entry which is used as name of the block."
  7793. (unless (bolp) (newline))
  7794. (let ((name (plist-get plist :name)))
  7795. (insert "#+BEGIN: " name)
  7796. (while plist
  7797. (if (eq (car plist) :name)
  7798. (setq plist (cddr plist))
  7799. (insert " " (prin1-to-string (pop plist)))))
  7800. (insert "\n\n#+END:\n")
  7801. (beginning-of-line -2)))
  7802. (defun org-prepare-dblock ()
  7803. "Prepare dynamic block for refresh.
  7804. This empties the block, puts the cursor at the insert position and returns
  7805. the property list including an extra property :name with the block name."
  7806. (unless (looking-at org-dblock-start-re)
  7807. (error "Not at a dynamic block"))
  7808. (let* ((begdel (1+ (match-end 0)))
  7809. (name (org-no-properties (match-string 1)))
  7810. (params (append (list :name name)
  7811. (read (concat "(" (match-string 3) ")")))))
  7812. (unless (re-search-forward org-dblock-end-re nil t)
  7813. (error "Dynamic block not terminated"))
  7814. (setq params
  7815. (append params
  7816. (list :content (buffer-substring
  7817. begdel (match-beginning 0)))))
  7818. (delete-region begdel (match-beginning 0))
  7819. (goto-char begdel)
  7820. (open-line 1)
  7821. params))
  7822. (defun org-map-dblocks (&optional command)
  7823. "Apply COMMAND to all dynamic blocks in the current buffer.
  7824. If COMMAND is not given, use `org-update-dblock'."
  7825. (let ((cmd (or command 'org-update-dblock))
  7826. pos)
  7827. (save-excursion
  7828. (goto-char (point-min))
  7829. (while (re-search-forward org-dblock-start-re nil t)
  7830. (goto-char (setq pos (match-beginning 0)))
  7831. (condition-case nil
  7832. (funcall cmd)
  7833. (error (message "Error during update of dynamic block")))
  7834. (goto-char pos)
  7835. (unless (re-search-forward org-dblock-end-re nil t)
  7836. (error "Dynamic block not terminated"))))))
  7837. (defun org-dblock-update (&optional arg)
  7838. "User command for updating dynamic blocks.
  7839. Update the dynamic block at point. With prefix ARG, update all dynamic
  7840. blocks in the buffer."
  7841. (interactive "P")
  7842. (if arg
  7843. (org-update-all-dblocks)
  7844. (or (looking-at org-dblock-start-re)
  7845. (org-beginning-of-dblock))
  7846. (org-update-dblock)))
  7847. (defun org-update-dblock ()
  7848. "Update the dynamic block at point
  7849. This means to empty the block, parse for parameters and then call
  7850. the correct writing function."
  7851. (save-window-excursion
  7852. (let* ((pos (point))
  7853. (line (org-current-line))
  7854. (params (org-prepare-dblock))
  7855. (name (plist-get params :name))
  7856. (cmd (intern (concat "org-dblock-write:" name))))
  7857. (message "Updating dynamic block `%s' at line %d..." name line)
  7858. (funcall cmd params)
  7859. (message "Updating dynamic block `%s' at line %d...done" name line)
  7860. (goto-char pos))))
  7861. (defun org-beginning-of-dblock ()
  7862. "Find the beginning of the dynamic block at point.
  7863. Error if there is no such block at point."
  7864. (let ((pos (point))
  7865. beg)
  7866. (end-of-line 1)
  7867. (if (and (re-search-backward org-dblock-start-re nil t)
  7868. (setq beg (match-beginning 0))
  7869. (re-search-forward org-dblock-end-re nil t)
  7870. (> (match-end 0) pos))
  7871. (goto-char beg)
  7872. (goto-char pos)
  7873. (error "Not in a dynamic block"))))
  7874. (defun org-update-all-dblocks ()
  7875. "Update all dynamic blocks in the buffer.
  7876. This function can be used in a hook."
  7877. (when (org-mode-p)
  7878. (org-map-dblocks 'org-update-dblock)))
  7879. ;;;; Completion
  7880. (defconst org-additional-option-like-keywords
  7881. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7882. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7883. "LATEX_HEADER:"
  7884. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7885. "BEGIN_QUOTE" "END_QUOTE"
  7886. "BEGIN_VERSE" "END_VERSE"
  7887. "BEGIN_CENTER" "END_CENTER"
  7888. "BEGIN_SRC" "END_SRC"
  7889. "CATEGORY" "COLUMNS"
  7890. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7891. (defcustom org-structure-template-alist
  7892. '(
  7893. ("s" "#+begin_src ?\n\n#+end_src"
  7894. "<src lang=\"?\">\n\n</src>")
  7895. ("e" "#+begin_example\n?\n#+end_example"
  7896. "<example>\n?\n</example>")
  7897. ("q" "#+begin_quote\n?\n#+end_quote"
  7898. "<quote>\n?\n</quote>")
  7899. ("v" "#+begin_verse\n?\n#+end_verse"
  7900. "<verse>\n?\n/verse>")
  7901. ("c" "#+begin_center\n?\n#+end_center"
  7902. "<center>\n?\n/center>")
  7903. ("l" "#+begin_latex\n?\n#+end_latex"
  7904. "<literal style=\"latex\">\n?\n</literal>")
  7905. ("L" "#+latex: "
  7906. "<literal style=\"latex\">?</literal>")
  7907. ("h" "#+begin_html\n?\n#+end_html"
  7908. "<literal style=\"html\">\n?\n</literal>")
  7909. ("H" "#+html: "
  7910. "<literal style=\"html\">?</literal>")
  7911. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7912. ("A" "#+ascii: ")
  7913. ("i" "#+include %file ?"
  7914. "<include file=%file markup=\"?\">")
  7915. )
  7916. "Structure completion elements.
  7917. This is a list of abbreviation keys and values. The value gets inserted
  7918. it you type @samp{.} followed by the key and then the completion key,
  7919. usually `M-TAB'. %file will be replaced by a file name after prompting
  7920. for the file using completion.
  7921. There are two templates for each key, the first uses the original Org syntax,
  7922. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7923. the default when the /org-mtags.el/ module has been loaded. See also the
  7924. variable `org-mtags-prefer-muse-templates'.
  7925. This is an experimental feature, it is undecided if it is going to stay in."
  7926. :group 'org-completion
  7927. :type '(repeat
  7928. (string :tag "Key")
  7929. (string :tag "Template")
  7930. (string :tag "Muse Template")))
  7931. (defun org-try-structure-completion ()
  7932. "Try to complete a structure template before point.
  7933. This looks for strings like \"<e\" on an otherwise empty line and
  7934. expands them."
  7935. (let ((l (buffer-substring (point-at-bol) (point)))
  7936. a)
  7937. (when (and (looking-at "[ \t]*$")
  7938. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7939. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7940. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7941. (match-beginning 1)) a)
  7942. t)))
  7943. (defun org-complete-expand-structure-template (start cell)
  7944. "Expand a structure template."
  7945. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7946. (rpl (nth (if musep 2 1) cell)))
  7947. (delete-region start (point))
  7948. (when (string-match "\\`#\\+" rpl)
  7949. (cond
  7950. ((bolp))
  7951. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7952. (delete-region (point-at-bol) (point)))
  7953. (t (newline))))
  7954. (setq start (point))
  7955. (if (string-match "%file" rpl)
  7956. (setq rpl (replace-match
  7957. (concat
  7958. "\""
  7959. (save-match-data
  7960. (abbreviate-file-name (read-file-name "Include file: ")))
  7961. "\"")
  7962. t t rpl)))
  7963. (insert rpl)
  7964. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7965. (defun org-complete (&optional arg)
  7966. "Perform completion on word at point.
  7967. At the beginning of a headline, this completes TODO keywords as given in
  7968. `org-todo-keywords'.
  7969. If the current word is preceded by a backslash, completes the TeX symbols
  7970. that are supported for HTML support.
  7971. If the current word is preceded by \"#+\", completes special words for
  7972. setting file options.
  7973. In the line after \"#+STARTUP:, complete valid keywords.\"
  7974. At all other locations, this simply calls the value of
  7975. `org-completion-fallback-command'."
  7976. (interactive "P")
  7977. (org-without-partial-completion
  7978. (catch 'exit
  7979. (let* ((a nil)
  7980. (end (point))
  7981. (beg1 (save-excursion
  7982. (skip-chars-backward (org-re "[:alnum:]_@"))
  7983. (point)))
  7984. (beg (save-excursion
  7985. (skip-chars-backward "a-zA-Z0-9_:$")
  7986. (point)))
  7987. (confirm (lambda (x) (stringp (car x))))
  7988. (searchhead (equal (char-before beg) ?*))
  7989. (struct
  7990. (when (and (member (char-before beg1) '(?. ?<))
  7991. (setq a (assoc (buffer-substring beg1 (point))
  7992. org-structure-template-alist)))
  7993. (org-complete-expand-structure-template (1- beg1) a)
  7994. (throw 'exit t)))
  7995. (tag (and (equal (char-before beg1) ?:)
  7996. (equal (char-after (point-at-bol)) ?*)))
  7997. (prop (and (equal (char-before beg1) ?:)
  7998. (not (equal (char-after (point-at-bol)) ?*))))
  7999. (texp (equal (char-before beg) ?\\))
  8000. (link (equal (char-before beg) ?\[))
  8001. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8002. beg)
  8003. "#+"))
  8004. (startup (string-match "^#\\+STARTUP:.*"
  8005. (buffer-substring (point-at-bol) (point))))
  8006. (completion-ignore-case opt)
  8007. (type nil)
  8008. (tbl nil)
  8009. (table (cond
  8010. (opt
  8011. (setq type :opt)
  8012. (require 'org-exp)
  8013. (append
  8014. (mapcar
  8015. (lambda (x)
  8016. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8017. (cons (match-string 2 x) (match-string 1 x)))
  8018. (org-split-string (org-get-current-options) "\n"))
  8019. (mapcar 'list org-additional-option-like-keywords)))
  8020. (startup
  8021. (setq type :startup)
  8022. org-startup-options)
  8023. (link (append org-link-abbrev-alist-local
  8024. org-link-abbrev-alist))
  8025. (texp
  8026. (setq type :tex)
  8027. org-html-entities)
  8028. ((string-match "\\`\\*+[ \t]+\\'"
  8029. (buffer-substring (point-at-bol) beg))
  8030. (setq type :todo)
  8031. (mapcar 'list org-todo-keywords-1))
  8032. (searchhead
  8033. (setq type :searchhead)
  8034. (save-excursion
  8035. (goto-char (point-min))
  8036. (while (re-search-forward org-todo-line-regexp nil t)
  8037. (push (list
  8038. (org-make-org-heading-search-string
  8039. (match-string 3) t))
  8040. tbl)))
  8041. tbl)
  8042. (tag (setq type :tag beg beg1)
  8043. (or org-tag-alist (org-get-buffer-tags)))
  8044. (prop (setq type :prop beg beg1)
  8045. (mapcar 'list (org-buffer-property-keys nil t t)))
  8046. (t (progn
  8047. (call-interactively org-completion-fallback-command)
  8048. (throw 'exit nil)))))
  8049. (pattern (buffer-substring-no-properties beg end))
  8050. (completion (try-completion pattern table confirm)))
  8051. (cond ((eq completion t)
  8052. (if (not (assoc (upcase pattern) table))
  8053. (message "Already complete")
  8054. (if (and (equal type :opt)
  8055. (not (member (car (assoc (upcase pattern) table))
  8056. org-additional-option-like-keywords)))
  8057. (insert (substring (cdr (assoc (upcase pattern) table))
  8058. (length pattern)))
  8059. (if (memq type '(:tag :prop)) (insert ":")))))
  8060. ((null completion)
  8061. (message "Can't find completion for \"%s\"" pattern)
  8062. (ding))
  8063. ((not (string= pattern completion))
  8064. (delete-region beg end)
  8065. (if (string-match " +$" completion)
  8066. (setq completion (replace-match "" t t completion)))
  8067. (insert completion)
  8068. (if (get-buffer-window "*Completions*")
  8069. (delete-window (get-buffer-window "*Completions*")))
  8070. (if (assoc completion table)
  8071. (if (eq type :todo) (insert " ")
  8072. (if (memq type '(:tag :prop)) (insert ":"))))
  8073. (if (and (equal type :opt) (assoc completion table))
  8074. (message "%s" (substitute-command-keys
  8075. "Press \\[org-complete] again to insert example settings"))))
  8076. (t
  8077. (message "Making completion list...")
  8078. (let ((list (sort (all-completions pattern table confirm)
  8079. 'string<)))
  8080. (with-output-to-temp-buffer "*Completions*"
  8081. (condition-case nil
  8082. ;; Protection needed for XEmacs and emacs 21
  8083. (display-completion-list list pattern)
  8084. (error (display-completion-list list)))))
  8085. (message "Making completion list...%s" "done")))))))
  8086. ;;;; TODO, DEADLINE, Comments
  8087. (defun org-toggle-comment ()
  8088. "Change the COMMENT state of an entry."
  8089. (interactive)
  8090. (save-excursion
  8091. (org-back-to-heading)
  8092. (let (case-fold-search)
  8093. (if (looking-at (concat outline-regexp
  8094. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8095. (replace-match "" t t nil 1)
  8096. (if (looking-at outline-regexp)
  8097. (progn
  8098. (goto-char (match-end 0))
  8099. (insert org-comment-string " ")))))))
  8100. (defvar org-last-todo-state-is-todo nil
  8101. "This is non-nil when the last TODO state change led to a TODO state.
  8102. If the last change removed the TODO tag or switched to DONE, then
  8103. this is nil.")
  8104. (defvar org-setting-tags nil) ; dynamically skipped
  8105. (defun org-parse-local-options (string var)
  8106. "Parse STRING for startup setting relevant for variable VAR."
  8107. (let ((rtn (symbol-value var))
  8108. e opts)
  8109. (save-match-data
  8110. (if (or (not string) (not (string-match "\\S-" string)))
  8111. rtn
  8112. (setq opts (delq nil (mapcar (lambda (x)
  8113. (setq e (assoc x org-startup-options))
  8114. (if (eq (nth 1 e) var) e nil))
  8115. (org-split-string string "[ \t]+"))))
  8116. (if (not opts)
  8117. rtn
  8118. (setq rtn nil)
  8119. (while (setq e (pop opts))
  8120. (if (not (nth 3 e))
  8121. (setq rtn (nth 2 e))
  8122. (if (not (listp rtn)) (setq rtn nil))
  8123. (push (nth 2 e) rtn)))
  8124. rtn)))))
  8125. (defvar org-todo-setup-filter-hook nil
  8126. "Hook for functions that pre-filter todo specs.
  8127. Each function takes a todo spec and returns either `nil' or the spec
  8128. transformed into canonical form." )
  8129. (defvar org-todo-get-default-hook nil
  8130. "Hook for functions that get a default item for todo.
  8131. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8132. `nil' or a string to be used for the todo mark." )
  8133. (defvar org-agenda-headline-snapshot-before-repeat)
  8134. (defun org-todo (&optional arg)
  8135. "Change the TODO state of an item.
  8136. The state of an item is given by a keyword at the start of the heading,
  8137. like
  8138. *** TODO Write paper
  8139. *** DONE Call mom
  8140. The different keywords are specified in the variable `org-todo-keywords'.
  8141. By default the available states are \"TODO\" and \"DONE\".
  8142. So for this example: when the item starts with TODO, it is changed to DONE.
  8143. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8144. DONE are present, add TODO at the beginning of the heading.
  8145. With C-u prefix arg, use completion to determine the new state.
  8146. With numeric prefix arg, switch to that state.
  8147. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8148. With a tripple C-u prefix, circumvent any state blocking.
  8149. For calling through lisp, arg is also interpreted in the following way:
  8150. 'none -> empty state
  8151. \"\"(empty string) -> switch to empty state
  8152. 'done -> switch to DONE
  8153. 'nextset -> switch to the next set of keywords
  8154. 'previousset -> switch to the previous set of keywords
  8155. \"WAITING\" -> switch to the specified keyword, but only if it
  8156. really is a member of `org-todo-keywords'."
  8157. (interactive "P")
  8158. (if (equal arg '(16)) (setq arg 'nextset))
  8159. (let ((org-blocker-hook org-blocker-hook)
  8160. (case-fold-search nil))
  8161. (when (equal arg '(64))
  8162. (setq arg nil org-blocker-hook nil))
  8163. (when (and org-blocker-hook
  8164. (org-entry-get nil "NOBLOCKING"))
  8165. (setq org-blocker-hook nil))
  8166. (save-excursion
  8167. (catch 'exit
  8168. (org-back-to-heading)
  8169. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8170. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8171. (looking-at " *"))
  8172. (let* ((match-data (match-data))
  8173. (startpos (point-at-bol))
  8174. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8175. (org-log-done org-log-done)
  8176. (org-log-repeat org-log-repeat)
  8177. (org-todo-log-states org-todo-log-states)
  8178. (this (match-string 1))
  8179. (hl-pos (match-beginning 0))
  8180. (head (org-get-todo-sequence-head this))
  8181. (ass (assoc head org-todo-kwd-alist))
  8182. (interpret (nth 1 ass))
  8183. (done-word (nth 3 ass))
  8184. (final-done-word (nth 4 ass))
  8185. (last-state (or this ""))
  8186. (completion-ignore-case t)
  8187. (member (member this org-todo-keywords-1))
  8188. (tail (cdr member))
  8189. (state (cond
  8190. ((and org-todo-key-trigger
  8191. (or (and (equal arg '(4))
  8192. (eq org-use-fast-todo-selection 'prefix))
  8193. (and (not arg) org-use-fast-todo-selection
  8194. (not (eq org-use-fast-todo-selection
  8195. 'prefix)))))
  8196. ;; Use fast selection
  8197. (org-fast-todo-selection))
  8198. ((and (equal arg '(4))
  8199. (or (not org-use-fast-todo-selection)
  8200. (not org-todo-key-trigger)))
  8201. ;; Read a state with completion
  8202. (org-ido-completing-read
  8203. "State: " (mapcar (lambda(x) (list x))
  8204. org-todo-keywords-1)
  8205. nil t))
  8206. ((eq arg 'right)
  8207. (if this
  8208. (if tail (car tail) nil)
  8209. (car org-todo-keywords-1)))
  8210. ((eq arg 'left)
  8211. (if (equal member org-todo-keywords-1)
  8212. nil
  8213. (if this
  8214. (nth (- (length org-todo-keywords-1)
  8215. (length tail) 2)
  8216. org-todo-keywords-1)
  8217. (org-last org-todo-keywords-1))))
  8218. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8219. (setq arg nil))) ; hack to fall back to cycling
  8220. (arg
  8221. ;; user or caller requests a specific state
  8222. (cond
  8223. ((equal arg "") nil)
  8224. ((eq arg 'none) nil)
  8225. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8226. ((eq arg 'nextset)
  8227. (or (car (cdr (member head org-todo-heads)))
  8228. (car org-todo-heads)))
  8229. ((eq arg 'previousset)
  8230. (let ((org-todo-heads (reverse org-todo-heads)))
  8231. (or (car (cdr (member head org-todo-heads)))
  8232. (car org-todo-heads))))
  8233. ((car (member arg org-todo-keywords-1)))
  8234. ((nth (1- (prefix-numeric-value arg))
  8235. org-todo-keywords-1))))
  8236. ((null member) (or head (car org-todo-keywords-1)))
  8237. ((equal this final-done-word) nil) ;; -> make empty
  8238. ((null tail) nil) ;; -> first entry
  8239. ((memq interpret '(type priority))
  8240. (if (eq this-command last-command)
  8241. (car tail)
  8242. (if (> (length tail) 0)
  8243. (or done-word (car org-done-keywords))
  8244. nil)))
  8245. (t
  8246. (car tail))))
  8247. (state (or
  8248. (run-hook-with-args-until-success
  8249. 'org-todo-get-default-hook state last-state)
  8250. state))
  8251. (next (if state (concat " " state " ") " "))
  8252. (change-plist (list :type 'todo-state-change :from this :to state
  8253. :position startpos))
  8254. dolog now-done-p)
  8255. (when org-blocker-hook
  8256. (setq org-last-todo-state-is-todo
  8257. (not (member this org-done-keywords)))
  8258. (unless (save-excursion
  8259. (save-match-data
  8260. (run-hook-with-args-until-failure
  8261. 'org-blocker-hook change-plist)))
  8262. (if (interactive-p)
  8263. (error "TODO state change from %s to %s blocked" this state)
  8264. ;; fail silently
  8265. (message "TODO state change from %s to %s blocked" this state)
  8266. (throw 'exit nil))))
  8267. (store-match-data match-data)
  8268. (replace-match next t t)
  8269. (unless (pos-visible-in-window-p hl-pos)
  8270. (message "TODO state changed to %s" (org-trim next)))
  8271. (unless head
  8272. (setq head (org-get-todo-sequence-head state)
  8273. ass (assoc head org-todo-kwd-alist)
  8274. interpret (nth 1 ass)
  8275. done-word (nth 3 ass)
  8276. final-done-word (nth 4 ass)))
  8277. (when (memq arg '(nextset previousset))
  8278. (message "Keyword-Set %d/%d: %s"
  8279. (- (length org-todo-sets) -1
  8280. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8281. (length org-todo-sets)
  8282. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8283. (setq org-last-todo-state-is-todo
  8284. (not (member state org-done-keywords)))
  8285. (setq now-done-p (and (member state org-done-keywords)
  8286. (not (member this org-done-keywords))))
  8287. (and logging (org-local-logging logging))
  8288. (when (and (or org-todo-log-states org-log-done)
  8289. (not (memq arg '(nextset previousset))))
  8290. ;; we need to look at recording a time and note
  8291. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8292. (nth 2 (assoc this org-todo-log-states))))
  8293. (when (and state
  8294. (member state org-not-done-keywords)
  8295. (not (member this org-not-done-keywords)))
  8296. ;; This is now a todo state and was not one before
  8297. ;; If there was a CLOSED time stamp, get rid of it.
  8298. (org-add-planning-info nil nil 'closed))
  8299. (when (and now-done-p org-log-done)
  8300. ;; It is now done, and it was not done before
  8301. (org-add-planning-info 'closed (org-current-time))
  8302. (if (and (not dolog) (eq 'note org-log-done))
  8303. (org-add-log-setup 'done state this 'findpos 'note)))
  8304. (when (and state dolog)
  8305. ;; This is a non-nil state, and we need to log it
  8306. (org-add-log-setup 'state state this 'findpos dolog)))
  8307. ;; Fixup tag positioning
  8308. (org-todo-trigger-tag-changes state)
  8309. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8310. (when org-provide-todo-statistics
  8311. (org-update-parent-todo-statistics))
  8312. (run-hooks 'org-after-todo-state-change-hook)
  8313. (if (and arg (not (member state org-done-keywords)))
  8314. (setq head (org-get-todo-sequence-head state)))
  8315. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8316. ;; Do we need to trigger a repeat?
  8317. (when now-done-p
  8318. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8319. ;; This is for the agenda, take a snapshot of the headline.
  8320. (save-match-data
  8321. (setq org-agenda-headline-snapshot-before-repeat
  8322. (org-get-heading))))
  8323. (org-auto-repeat-maybe state))
  8324. ;; Fixup cursor location if close to the keyword
  8325. (if (and (outline-on-heading-p)
  8326. (not (bolp))
  8327. (save-excursion (beginning-of-line 1)
  8328. (looking-at org-todo-line-regexp))
  8329. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8330. (progn
  8331. (goto-char (or (match-end 2) (match-end 1)))
  8332. (and (looking-at " ") (just-one-space))))
  8333. (when org-trigger-hook
  8334. (save-excursion
  8335. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8336. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8337. "Block turning an entry into a TODO, using the hierarchy.
  8338. This checks whether the current task should be blocked from state
  8339. changes. Such blocking occurs when:
  8340. 1. The task has children which are not all in a completed state.
  8341. 2. A task has a parent with the property :ORDERED:, and there
  8342. are siblings prior to the current task with incomplete
  8343. status.
  8344. 3. The parent of the task is blocked because it has siblings that should
  8345. be done first, or is child of a block grandparent TODO entry."
  8346. (catch 'dont-block
  8347. ;; If this is not a todo state change, or if this entry is already DONE,
  8348. ;; do not block
  8349. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8350. (member (plist-get change-plist :from)
  8351. (cons 'done org-done-keywords))
  8352. (member (plist-get change-plist :to)
  8353. (cons 'todo org-not-done-keywords)))
  8354. (throw 'dont-block t))
  8355. ;; If this task has children, and any are undone, it's blocked
  8356. (save-excursion
  8357. (org-back-to-heading t)
  8358. (let ((this-level (funcall outline-level)))
  8359. (outline-next-heading)
  8360. (let ((child-level (funcall outline-level)))
  8361. (while (and (not (eobp))
  8362. (> child-level this-level))
  8363. ;; this todo has children, check whether they are all
  8364. ;; completed
  8365. (if (and (not (org-entry-is-done-p))
  8366. (org-entry-is-todo-p))
  8367. (throw 'dont-block nil))
  8368. (outline-next-heading)
  8369. (setq child-level (funcall outline-level))))))
  8370. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8371. ;; any previous siblings are undone, it's blocked
  8372. (save-excursion
  8373. (org-back-to-heading t)
  8374. (let* ((pos (point))
  8375. (parent-pos (and (org-up-heading-safe) (point))))
  8376. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8377. (when (and (org-entry-get (point) "ORDERED")
  8378. (forward-line 1)
  8379. (re-search-forward org-not-done-heading-regexp pos t))
  8380. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8381. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8382. (while t
  8383. (goto-char parent-pos)
  8384. (if (not (looking-at org-not-done-heading-regexp))
  8385. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8386. (setq pos (point))
  8387. (setq parent-pos (and (org-up-heading-safe) (point)))
  8388. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8389. (when (and (org-entry-get (point) "ORDERED")
  8390. (forward-line 1)
  8391. (re-search-forward org-not-done-heading-regexp pos t))
  8392. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8393. (defcustom org-track-ordered-property-with-tag nil
  8394. "Should the ORDERED property also be shown as a tag?
  8395. The ORDERED property decides if an entry should require subtasks to be
  8396. completed in sequence. Since a property is not very visible, setting
  8397. this option means that toggling the ORDERED property with the command
  8398. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8399. not relevant for the behavior, but it makes things more visible.
  8400. Note that toggling the tag with tags commands will not change the property
  8401. and therefore not influence behavior!
  8402. This can be t, meaning the tag ORDERED should be used, It can also be a
  8403. string to select a different tag for this task."
  8404. :group 'org-todo
  8405. :type '(choice
  8406. (const :tag "No tracking" nil)
  8407. (const :tag "Track with ORDERED tag" t)
  8408. (string :tag "Use other tag")))
  8409. (defun org-toggle-ordered-property ()
  8410. "Toggle the ORDERED property of the current entry.
  8411. For better visibility, you can track the value of this property with a tag.
  8412. See variable `org-track-ordered-property-with-tag'."
  8413. (interactive)
  8414. (let* ((t1 org-track-ordered-property-with-tag)
  8415. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8416. (save-excursion
  8417. (org-back-to-heading)
  8418. (if (org-entry-get nil "ORDERED")
  8419. (progn
  8420. (org-delete-property "ORDERED")
  8421. (and tag (org-toggle-tag tag 'off))
  8422. (message "Subtasks can be completed in arbitrary order"))
  8423. (org-entry-put nil "ORDERED" "t")
  8424. (and tag (org-toggle-tag tag 'on))
  8425. (message "Subtasks must be completed in sequence")))))
  8426. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8427. (defun org-block-todo-from-checkboxes (change-plist)
  8428. "Block turning an entry into a TODO, using checkboxes.
  8429. This checks whether the current task should be blocked from state
  8430. changes because there are uncheckd boxes in this entry."
  8431. (catch 'dont-block
  8432. ;; If this is not a todo state change, or if this entry is already DONE,
  8433. ;; do not block
  8434. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8435. (member (plist-get change-plist :from)
  8436. (cons 'done org-done-keywords))
  8437. (member (plist-get change-plist :to)
  8438. (cons 'todo org-not-done-keywords)))
  8439. (throw 'dont-block t))
  8440. ;; If this task has checkboxes that are not checked, it's blocked
  8441. (save-excursion
  8442. (org-back-to-heading t)
  8443. (let ((beg (point)) end)
  8444. (outline-next-heading)
  8445. (setq end (point))
  8446. (goto-char beg)
  8447. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8448. end t)
  8449. (progn
  8450. (if (boundp 'org-blocked-by-checkboxes)
  8451. (setq org-blocked-by-checkboxes t))
  8452. (throw 'dont-block nil)))))
  8453. t)) ; do not block
  8454. (defun org-update-parent-todo-statistics ()
  8455. "Update any statistics cookie in the parent of the current headline."
  8456. (interactive)
  8457. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8458. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8459. (catch 'exit
  8460. (save-excursion
  8461. (setq level (org-up-heading-safe))
  8462. (unless (and level
  8463. (not (equal (downcase
  8464. (or (org-entry-get
  8465. nil "COOKIE_DATA")
  8466. ""))
  8467. "checkbox")))
  8468. (throw 'exit nil))
  8469. (while (re-search-forward box-re (point-at-eol) t)
  8470. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8471. (setq is-percent (match-end 2))
  8472. (save-match-data
  8473. (unless (outline-next-heading) (throw 'exit nil))
  8474. (while (looking-at org-todo-line-regexp)
  8475. (setq kwd (match-string 2))
  8476. (and kwd (setq cnt-all (1+ cnt-all)))
  8477. (and (member kwd org-done-keywords)
  8478. (setq cnt-done (1+ cnt-done)))
  8479. (condition-case nil
  8480. (org-forward-same-level 1)
  8481. (error (end-of-line 1)))))
  8482. (replace-match
  8483. (if is-percent
  8484. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8485. (format "[%d/%d]" cnt-done cnt-all))))
  8486. (when cookie-present
  8487. (run-hook-with-args 'org-after-todo-statistics-hook
  8488. cnt-done (- cnt-all cnt-done)))))))
  8489. (defvar org-after-todo-statistics-hook nil
  8490. "Hook that is called after a TODO statistics cookie has been updated.
  8491. Each function is called with two arguments: the number of not-done entries
  8492. and the number of done entries.
  8493. For example, the following function, when added to this hook, will switch
  8494. an entry to DONE when all children are done, and back to TODO when new
  8495. entries are set to a TODO status. Note that this hook is only called
  8496. when there is a statistics cookie in the headline!
  8497. (defun org-summary-todo (n-done n-not-done)
  8498. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8499. (let (org-log-done org-log-states) ; turn off logging
  8500. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8501. ")
  8502. (defun org-todo-trigger-tag-changes (state)
  8503. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8504. (let ((l org-todo-state-tags-triggers)
  8505. changes)
  8506. (when (or (not state) (equal state ""))
  8507. (setq changes (append changes (cdr (assoc "" l)))))
  8508. (when (and (stringp state) (> (length state) 0))
  8509. (setq changes (append changes (cdr (assoc state l)))))
  8510. (when (member state org-not-done-keywords)
  8511. (setq changes (append changes (cdr (assoc 'todo l)))))
  8512. (when (member state org-done-keywords)
  8513. (setq changes (append changes (cdr (assoc 'done l)))))
  8514. (dolist (c changes)
  8515. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8516. (defun org-local-logging (value)
  8517. "Get logging settings from a property VALUE."
  8518. (let* (words w a)
  8519. ;; directly set the variables, they are already local.
  8520. (setq org-log-done nil
  8521. org-log-repeat nil
  8522. org-todo-log-states nil)
  8523. (setq words (org-split-string value))
  8524. (while (setq w (pop words))
  8525. (cond
  8526. ((setq a (assoc w org-startup-options))
  8527. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8528. (set (nth 1 a) (nth 2 a))))
  8529. ((setq a (org-extract-log-state-settings w))
  8530. (and (member (car a) org-todo-keywords-1)
  8531. (push a org-todo-log-states)))))))
  8532. (defun org-get-todo-sequence-head (kwd)
  8533. "Return the head of the TODO sequence to which KWD belongs.
  8534. If KWD is not set, check if there is a text property remembering the
  8535. right sequence."
  8536. (let (p)
  8537. (cond
  8538. ((not kwd)
  8539. (or (get-text-property (point-at-bol) 'org-todo-head)
  8540. (progn
  8541. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8542. nil (point-at-eol)))
  8543. (get-text-property p 'org-todo-head))))
  8544. ((not (member kwd org-todo-keywords-1))
  8545. (car org-todo-keywords-1))
  8546. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8547. (defun org-fast-todo-selection ()
  8548. "Fast TODO keyword selection with single keys.
  8549. Returns the new TODO keyword, or nil if no state change should occur."
  8550. (let* ((fulltable org-todo-key-alist)
  8551. (done-keywords org-done-keywords) ;; needed for the faces.
  8552. (maxlen (apply 'max (mapcar
  8553. (lambda (x)
  8554. (if (stringp (car x)) (string-width (car x)) 0))
  8555. fulltable)))
  8556. (expert nil)
  8557. (fwidth (+ maxlen 3 1 3))
  8558. (ncol (/ (- (window-width) 4) fwidth))
  8559. tg cnt e c tbl
  8560. groups ingroup)
  8561. (save-excursion
  8562. (save-window-excursion
  8563. (if expert
  8564. (set-buffer (get-buffer-create " *Org todo*"))
  8565. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8566. (erase-buffer)
  8567. (org-set-local 'org-done-keywords done-keywords)
  8568. (setq tbl fulltable cnt 0)
  8569. (while (setq e (pop tbl))
  8570. (cond
  8571. ((equal e '(:startgroup))
  8572. (push '() groups) (setq ingroup t)
  8573. (when (not (= cnt 0))
  8574. (setq cnt 0)
  8575. (insert "\n"))
  8576. (insert "{ "))
  8577. ((equal e '(:endgroup))
  8578. (setq ingroup nil cnt 0)
  8579. (insert "}\n"))
  8580. ((equal e '(:newline))
  8581. (when (not (= cnt 0))
  8582. (setq cnt 0)
  8583. (insert "\n")
  8584. (setq e (car tbl))
  8585. (while (equal (car tbl) '(:newline))
  8586. (insert "\n")
  8587. (setq tbl (cdr tbl)))))
  8588. (t
  8589. (setq tg (car e) c (cdr e))
  8590. (if ingroup (push tg (car groups)))
  8591. (setq tg (org-add-props tg nil 'face
  8592. (org-get-todo-face tg)))
  8593. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8594. (insert "[" c "] " tg (make-string
  8595. (- fwidth 4 (length tg)) ?\ ))
  8596. (when (= (setq cnt (1+ cnt)) ncol)
  8597. (insert "\n")
  8598. (if ingroup (insert " "))
  8599. (setq cnt 0)))))
  8600. (insert "\n")
  8601. (goto-char (point-min))
  8602. (if (not expert) (org-fit-window-to-buffer))
  8603. (message "[a-z..]:Set [SPC]:clear")
  8604. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8605. (cond
  8606. ((or (= c ?\C-g)
  8607. (and (= c ?q) (not (rassoc c fulltable))))
  8608. (setq quit-flag t))
  8609. ((= c ?\ ) nil)
  8610. ((setq e (rassoc c fulltable) tg (car e))
  8611. tg)
  8612. (t (setq quit-flag t)))))))
  8613. (defun org-entry-is-todo-p ()
  8614. (member (org-get-todo-state) org-not-done-keywords))
  8615. (defun org-entry-is-done-p ()
  8616. (member (org-get-todo-state) org-done-keywords))
  8617. (defun org-get-todo-state ()
  8618. (save-excursion
  8619. (org-back-to-heading t)
  8620. (and (looking-at org-todo-line-regexp)
  8621. (match-end 2)
  8622. (match-string 2))))
  8623. (defun org-at-date-range-p (&optional inactive-ok)
  8624. "Is the cursor inside a date range?"
  8625. (interactive)
  8626. (save-excursion
  8627. (catch 'exit
  8628. (let ((pos (point)))
  8629. (skip-chars-backward "^[<\r\n")
  8630. (skip-chars-backward "<[")
  8631. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8632. (>= (match-end 0) pos)
  8633. (throw 'exit t))
  8634. (skip-chars-backward "^<[\r\n")
  8635. (skip-chars-backward "<[")
  8636. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8637. (>= (match-end 0) pos)
  8638. (throw 'exit t)))
  8639. nil)))
  8640. (defun org-get-repeat ()
  8641. "Check if there is a deadline/schedule with repeater in this entry."
  8642. (save-match-data
  8643. (save-excursion
  8644. (org-back-to-heading t)
  8645. (if (re-search-forward
  8646. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8647. (match-string 1)))))
  8648. (defvar org-last-changed-timestamp)
  8649. (defvar org-last-inserted-timestamp)
  8650. (defvar org-log-post-message)
  8651. (defvar org-log-note-purpose)
  8652. (defvar org-log-note-how)
  8653. (defvar org-log-note-extra)
  8654. (defun org-auto-repeat-maybe (done-word)
  8655. "Check if the current headline contains a repeated deadline/schedule.
  8656. If yes, set TODO state back to what it was and change the base date
  8657. of repeating deadline/scheduled time stamps to new date.
  8658. This function is run automatically after each state change to a DONE state."
  8659. ;; last-state is dynamically scoped into this function
  8660. (let* ((repeat (org-get-repeat))
  8661. (aa (assoc last-state org-todo-kwd-alist))
  8662. (interpret (nth 1 aa))
  8663. (head (nth 2 aa))
  8664. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8665. (msg "Entry repeats: ")
  8666. (org-log-done nil)
  8667. (org-todo-log-states nil)
  8668. (nshiftmax 10) (nshift 0)
  8669. re type n what ts time)
  8670. (when repeat
  8671. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8672. (org-todo (if (eq interpret 'type) last-state head))
  8673. (when org-log-repeat
  8674. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8675. (memq 'org-add-log-note post-command-hook))
  8676. ;; OK, we are already setup for some record
  8677. (if (eq org-log-repeat 'note)
  8678. ;; make sure we take a note, not only a time stamp
  8679. (setq org-log-note-how 'note))
  8680. ;; Set up for taking a record
  8681. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8682. last-state
  8683. 'findpos org-log-repeat)))
  8684. (org-back-to-heading t)
  8685. (org-add-planning-info nil nil 'closed)
  8686. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8687. org-deadline-time-regexp "\\)\\|\\("
  8688. org-ts-regexp "\\)"))
  8689. (while (re-search-forward
  8690. re (save-excursion (outline-next-heading) (point)) t)
  8691. (setq type (if (match-end 1) org-scheduled-string
  8692. (if (match-end 3) org-deadline-string "Plain:"))
  8693. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8694. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8695. (setq n (string-to-number (match-string 2 ts))
  8696. what (match-string 3 ts))
  8697. (if (equal what "w") (setq n (* n 7) what "d"))
  8698. ;; Preparation, see if we need to modify the start date for the change
  8699. (when (match-end 1)
  8700. (setq time (save-match-data (org-time-string-to-time ts)))
  8701. (cond
  8702. ((equal (match-string 1 ts) ".")
  8703. ;; Shift starting date to today
  8704. (org-timestamp-change
  8705. (- (time-to-days (current-time)) (time-to-days time))
  8706. 'day))
  8707. ((equal (match-string 1 ts) "+")
  8708. (while (or (= nshift 0)
  8709. (<= (time-to-days time) (time-to-days (current-time))))
  8710. (when (= (incf nshift) nshiftmax)
  8711. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8712. (error "Abort")))
  8713. (org-timestamp-change n (cdr (assoc what whata)))
  8714. (org-at-timestamp-p t)
  8715. (setq ts (match-string 1))
  8716. (setq time (save-match-data (org-time-string-to-time ts))))
  8717. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8718. ;; rematch, so that we have everything in place for the real shift
  8719. (org-at-timestamp-p t)
  8720. (setq ts (match-string 1))
  8721. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8722. (org-timestamp-change n (cdr (assoc what whata)))
  8723. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8724. (setq org-log-post-message msg)
  8725. (message "%s" msg))))
  8726. (defun org-show-todo-tree (arg)
  8727. "Make a compact tree which shows all headlines marked with TODO.
  8728. The tree will show the lines where the regexp matches, and all higher
  8729. headlines above the match.
  8730. With a \\[universal-argument] prefix, also show the DONE entries.
  8731. With a numeric prefix N, construct a sparse tree for the Nth element
  8732. of `org-todo-keywords-1'."
  8733. (interactive "P")
  8734. (let ((case-fold-search nil)
  8735. (kwd-re
  8736. (cond ((null arg) org-not-done-regexp)
  8737. ((equal arg '(4))
  8738. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8739. (mapcar 'list org-todo-keywords-1))))
  8740. (concat "\\("
  8741. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8742. "\\)\\>")))
  8743. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8744. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8745. org-todo-keywords-1)))
  8746. (t (error "Invalid prefix argument: %s" arg)))))
  8747. (message "%d TODO entries found"
  8748. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8749. (defun org-deadline (&optional remove time)
  8750. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8751. With argument REMOVE, remove any deadline from the item.
  8752. When TIME is set, it should be an internal time specification, and the
  8753. scheduling will use the corresponding date."
  8754. (interactive "P")
  8755. (if remove
  8756. (progn
  8757. (org-remove-timestamp-with-keyword org-deadline-string)
  8758. (message "Item no longer has a deadline."))
  8759. (if (org-get-repeat)
  8760. (error "Cannot change deadline on task with repeater, please do that by hand")
  8761. (org-add-planning-info 'deadline time 'closed)
  8762. (message "Deadline on %s" org-last-inserted-timestamp))))
  8763. (defun org-schedule (&optional remove time)
  8764. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8765. With argument REMOVE, remove any scheduling date from the item.
  8766. When TIME is set, it should be an internal time specification, and the
  8767. scheduling will use the corresponding date."
  8768. (interactive "P")
  8769. (if remove
  8770. (progn
  8771. (org-remove-timestamp-with-keyword org-scheduled-string)
  8772. (message "Item is no longer scheduled."))
  8773. (if (org-get-repeat)
  8774. (error "Cannot reschedule task with repeater, please do that by hand")
  8775. (org-add-planning-info 'scheduled time 'closed)
  8776. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8777. (defun org-get-scheduled-time (pom &optional inherit)
  8778. "Get the scheduled time as a time tuple, of a format suitable
  8779. for calling org-schedule with, or if there is no scheduling,
  8780. returns nil."
  8781. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8782. (when time
  8783. (apply 'encode-time (org-parse-time-string time)))))
  8784. (defun org-get-deadline-time (pom &optional inherit)
  8785. "Get the deadine as a time tuple, of a format suitable for
  8786. calling org-deadlin with, or if there is no scheduling, returns
  8787. nil."
  8788. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8789. (when time
  8790. (apply 'encode-time (org-parse-time-string time)))))
  8791. (defun org-remove-timestamp-with-keyword (keyword)
  8792. "Remove all time stamps with KEYWORD in the current entry."
  8793. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8794. beg)
  8795. (save-excursion
  8796. (org-back-to-heading t)
  8797. (setq beg (point))
  8798. (org-end-of-subtree t t)
  8799. (while (re-search-backward re beg t)
  8800. (replace-match "")
  8801. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8802. (equal (char-before) ?\ ))
  8803. (backward-delete-char 1)
  8804. (if (string-match "^[ \t]*$" (buffer-substring
  8805. (point-at-bol) (point-at-eol)))
  8806. (delete-region (point-at-bol)
  8807. (min (point-max) (1+ (point-at-eol))))))))))
  8808. (defun org-add-planning-info (what &optional time &rest remove)
  8809. "Insert new timestamp with keyword in the line directly after the headline.
  8810. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8811. If non is given, the user is prompted for a date.
  8812. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8813. be removed."
  8814. (interactive)
  8815. (let (org-time-was-given org-end-time-was-given ts
  8816. end default-time default-input)
  8817. (catch 'exit
  8818. (when (and (not time) (memq what '(scheduled deadline)))
  8819. ;; Try to get a default date/time from existing timestamp
  8820. (save-excursion
  8821. (org-back-to-heading t)
  8822. (setq end (save-excursion (outline-next-heading) (point)))
  8823. (when (re-search-forward (if (eq what 'scheduled)
  8824. org-scheduled-time-regexp
  8825. org-deadline-time-regexp)
  8826. end t)
  8827. (setq ts (match-string 1)
  8828. default-time
  8829. (apply 'encode-time (org-parse-time-string ts))
  8830. default-input (and ts (org-get-compact-tod ts))))))
  8831. (when what
  8832. ;; If necessary, get the time from the user
  8833. (setq time (or time (org-read-date nil 'to-time nil nil
  8834. default-time default-input))))
  8835. (when (and org-insert-labeled-timestamps-at-point
  8836. (member what '(scheduled deadline)))
  8837. (insert
  8838. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8839. (org-insert-time-stamp time org-time-was-given
  8840. nil nil nil (list org-end-time-was-given))
  8841. (setq what nil))
  8842. (save-excursion
  8843. (save-restriction
  8844. (let (col list elt ts buffer-invisibility-spec)
  8845. (org-back-to-heading t)
  8846. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8847. (goto-char (match-end 1))
  8848. (setq col (current-column))
  8849. (goto-char (match-end 0))
  8850. (if (eobp) (insert "\n") (forward-char 1))
  8851. (when (and (not what)
  8852. (not (looking-at
  8853. (concat "[ \t]*"
  8854. org-keyword-time-not-clock-regexp))))
  8855. ;; Nothing to add, nothing to remove...... :-)
  8856. (throw 'exit nil))
  8857. (if (and (not (looking-at outline-regexp))
  8858. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8859. "[^\r\n]*"))
  8860. (not (equal (match-string 1) org-clock-string)))
  8861. (narrow-to-region (match-beginning 0) (match-end 0))
  8862. (insert-before-markers "\n")
  8863. (backward-char 1)
  8864. (narrow-to-region (point) (point))
  8865. (and org-adapt-indentation (org-indent-to-column col)))
  8866. ;; Check if we have to remove something.
  8867. (setq list (cons what remove))
  8868. (while list
  8869. (setq elt (pop list))
  8870. (goto-char (point-min))
  8871. (when (or (and (eq elt 'scheduled)
  8872. (re-search-forward org-scheduled-time-regexp nil t))
  8873. (and (eq elt 'deadline)
  8874. (re-search-forward org-deadline-time-regexp nil t))
  8875. (and (eq elt 'closed)
  8876. (re-search-forward org-closed-time-regexp nil t)))
  8877. (replace-match "")
  8878. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8879. (if (looking-at " +") (replace-match ""))))
  8880. (goto-char (point-max))
  8881. (when what
  8882. (insert
  8883. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8884. (cond ((eq what 'scheduled) org-scheduled-string)
  8885. ((eq what 'deadline) org-deadline-string)
  8886. ((eq what 'closed) org-closed-string))
  8887. " ")
  8888. (setq ts (org-insert-time-stamp
  8889. time
  8890. (or org-time-was-given
  8891. (and (eq what 'closed) org-log-done-with-time))
  8892. (eq what 'closed)
  8893. nil nil (list org-end-time-was-given)))
  8894. (end-of-line 1))
  8895. (goto-char (point-min))
  8896. (widen)
  8897. (if (and (looking-at "[ \t]+\n")
  8898. (equal (char-before) ?\n))
  8899. (delete-region (1- (point)) (point-at-eol)))
  8900. ts))))))
  8901. (defvar org-log-note-marker (make-marker))
  8902. (defvar org-log-note-purpose nil)
  8903. (defvar org-log-note-state nil)
  8904. (defvar org-log-note-previous-state nil)
  8905. (defvar org-log-note-how nil)
  8906. (defvar org-log-note-extra nil)
  8907. (defvar org-log-note-window-configuration nil)
  8908. (defvar org-log-note-return-to (make-marker))
  8909. (defvar org-log-post-message nil
  8910. "Message to be displayed after a log note has been stored.
  8911. The auto-repeater uses this.")
  8912. (defun org-add-note ()
  8913. "Add a note to the current entry.
  8914. This is done in the same way as adding a state change note."
  8915. (interactive)
  8916. (org-add-log-setup 'note nil nil 'findpos nil))
  8917. (defvar org-property-end-re)
  8918. (defun org-add-log-setup (&optional purpose state prev-state
  8919. findpos how &optional extra)
  8920. "Set up the post command hook to take a note.
  8921. If this is about to TODO state change, the new state is expected in STATE.
  8922. When FINDPOS is non-nil, find the correct position for the note in
  8923. the current entry. If not, assume that it can be inserted at point.
  8924. HOW is an indicator what kind of note should be created.
  8925. EXTRA is additional text that will be inserted into the notes buffer."
  8926. (let ((drawer (cond ((stringp org-log-into-drawer)
  8927. org-log-into-drawer)
  8928. (org-log-into-drawer "LOGBOOK")
  8929. (t nil))))
  8930. (save-restriction
  8931. (save-excursion
  8932. (when findpos
  8933. (org-back-to-heading t)
  8934. (narrow-to-region (point) (save-excursion
  8935. (outline-next-heading) (point)))
  8936. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8937. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8938. "[^\r\n]*\\)?"))
  8939. (goto-char (match-end 0))
  8940. (cond
  8941. (drawer
  8942. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8943. nil t)
  8944. (progn
  8945. (goto-char (match-end 0))
  8946. (or org-log-states-order-reversed
  8947. (and (re-search-forward org-property-end-re nil t)
  8948. (goto-char (1- (match-beginning 0))))))
  8949. (insert "\n:" drawer ":\n:END:")
  8950. (beginning-of-line 0)
  8951. (org-indent-line-function)
  8952. (beginning-of-line 2)
  8953. (org-indent-line-function)
  8954. (end-of-line 0)))
  8955. ((and org-log-state-notes-insert-after-drawers
  8956. (save-excursion
  8957. (forward-line) (looking-at org-drawer-regexp)))
  8958. (forward-line)
  8959. (while (looking-at org-drawer-regexp)
  8960. (goto-char (match-end 0))
  8961. (re-search-forward org-property-end-re (point-max) t)
  8962. (forward-line))
  8963. (forward-line -1)))
  8964. (unless org-log-states-order-reversed
  8965. (and (= (char-after) ?\n) (forward-char 1))
  8966. (org-skip-over-state-notes)
  8967. (skip-chars-backward " \t\n\r")))
  8968. (move-marker org-log-note-marker (point))
  8969. (setq org-log-note-purpose purpose
  8970. org-log-note-state state
  8971. org-log-note-previous-state prev-state
  8972. org-log-note-how how
  8973. org-log-note-extra extra)
  8974. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8975. (defun org-skip-over-state-notes ()
  8976. "Skip past the list of State notes in an entry."
  8977. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8978. (while (looking-at "[ \t]*- State")
  8979. (condition-case nil
  8980. (org-next-item)
  8981. (error (org-end-of-item)))))
  8982. (defun org-add-log-note (&optional purpose)
  8983. "Pop up a window for taking a note, and add this note later at point."
  8984. (remove-hook 'post-command-hook 'org-add-log-note)
  8985. (setq org-log-note-window-configuration (current-window-configuration))
  8986. (delete-other-windows)
  8987. (move-marker org-log-note-return-to (point))
  8988. (switch-to-buffer (marker-buffer org-log-note-marker))
  8989. (goto-char org-log-note-marker)
  8990. (org-switch-to-buffer-other-window "*Org Note*")
  8991. (erase-buffer)
  8992. (if (memq org-log-note-how '(time state))
  8993. (let (current-prefix-arg) (org-store-log-note))
  8994. (let ((org-inhibit-startup t)) (org-mode))
  8995. (insert (format "# Insert note for %s.
  8996. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8997. (cond
  8998. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8999. ((eq org-log-note-purpose 'done) "closed todo item")
  9000. ((eq org-log-note-purpose 'state)
  9001. (format "state change from \"%s\" to \"%s\""
  9002. (or org-log-note-previous-state "")
  9003. (or org-log-note-state "")))
  9004. ((eq org-log-note-purpose 'note)
  9005. "this entry")
  9006. (t (error "This should not happen")))))
  9007. (if org-log-note-extra (insert org-log-note-extra))
  9008. (org-set-local 'org-finish-function 'org-store-log-note)))
  9009. (defvar org-note-abort nil) ; dynamically scoped
  9010. (defun org-store-log-note ()
  9011. "Finish taking a log note, and insert it to where it belongs."
  9012. (let ((txt (buffer-string))
  9013. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9014. lines ind)
  9015. (kill-buffer (current-buffer))
  9016. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9017. (setq txt (replace-match "" t t txt)))
  9018. (if (string-match "\\s-+\\'" txt)
  9019. (setq txt (replace-match "" t t txt)))
  9020. (setq lines (org-split-string txt "\n"))
  9021. (when (and note (string-match "\\S-" note))
  9022. (setq note
  9023. (org-replace-escapes
  9024. note
  9025. (list (cons "%u" (user-login-name))
  9026. (cons "%U" user-full-name)
  9027. (cons "%t" (format-time-string
  9028. (org-time-stamp-format 'long 'inactive)
  9029. (current-time)))
  9030. (cons "%s" (if org-log-note-state
  9031. (concat "\"" org-log-note-state "\"")
  9032. ""))
  9033. (cons "%S" (if org-log-note-previous-state
  9034. (concat "\"" org-log-note-previous-state "\"")
  9035. "\"\"")))))
  9036. (if lines (setq note (concat note " \\\\")))
  9037. (push note lines))
  9038. (when (or current-prefix-arg org-note-abort)
  9039. (when org-log-into-drawer
  9040. (org-remove-empty-drawer-at
  9041. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9042. org-log-note-marker))
  9043. (setq lines nil))
  9044. (when lines
  9045. (save-excursion
  9046. (set-buffer (marker-buffer org-log-note-marker))
  9047. (save-excursion
  9048. (goto-char org-log-note-marker)
  9049. (move-marker org-log-note-marker nil)
  9050. (end-of-line 1)
  9051. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9052. (insert "- " (pop lines))
  9053. (org-indent-line-function)
  9054. (beginning-of-line 1)
  9055. (looking-at "[ \t]*")
  9056. (setq ind (concat (match-string 0) " "))
  9057. (end-of-line 1)
  9058. (while lines (insert "\n" ind (pop lines)))
  9059. (message "Note stored")
  9060. (org-back-to-heading t)
  9061. (org-cycle-hide-drawers 'children)))))
  9062. (set-window-configuration org-log-note-window-configuration)
  9063. (with-current-buffer (marker-buffer org-log-note-return-to)
  9064. (goto-char org-log-note-return-to))
  9065. (move-marker org-log-note-return-to nil)
  9066. (and org-log-post-message (message "%s" org-log-post-message)))
  9067. (defun org-remove-empty-drawer-at (drawer pos)
  9068. "Remove an emptyr DARWER drawer at position POS.
  9069. POS may also be a marker."
  9070. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9071. (save-excursion
  9072. (save-restriction
  9073. (widen)
  9074. (goto-char pos)
  9075. (if (org-in-regexp
  9076. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9077. (replace-match ""))))))
  9078. (defun org-sparse-tree (&optional arg)
  9079. "Create a sparse tree, prompt for the details.
  9080. This command can create sparse trees. You first need to select the type
  9081. of match used to create the tree:
  9082. t Show entries with a specific TODO keyword.
  9083. m Show entries selected by a tags/property match.
  9084. p Enter a property name and its value (both with completion on existing
  9085. names/values) and show entries with that property.
  9086. r Show entries matching a regular expression.
  9087. d Show deadlines due within `org-deadline-warning-days'.
  9088. b Show deadlines and scheduled items before a date.
  9089. a Show deadlines and scheduled items after a date."
  9090. (interactive "P")
  9091. (let (ans kwd value)
  9092. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9093. (setq ans (read-char-exclusive))
  9094. (cond
  9095. ((equal ans ?d)
  9096. (call-interactively 'org-check-deadlines))
  9097. ((equal ans ?b)
  9098. (call-interactively 'org-check-before-date))
  9099. ((equal ans ?a)
  9100. (call-interactively 'org-check-after-date))
  9101. ((equal ans ?t)
  9102. (org-show-todo-tree '(4)))
  9103. ((member ans '(?T ?m))
  9104. (call-interactively 'org-match-sparse-tree))
  9105. ((member ans '(?p ?P))
  9106. (setq kwd (org-ido-completing-read "Property: "
  9107. (mapcar 'list (org-buffer-property-keys))))
  9108. (setq value (org-ido-completing-read "Value: "
  9109. (mapcar 'list (org-property-values kwd))))
  9110. (unless (string-match "\\`{.*}\\'" value)
  9111. (setq value (concat "\"" value "\"")))
  9112. (org-match-sparse-tree arg (concat kwd "=" value)))
  9113. ((member ans '(?r ?R ?/))
  9114. (call-interactively 'org-occur))
  9115. (t (error "No such sparse tree command \"%c\"" ans)))))
  9116. (defvar org-occur-highlights nil
  9117. "List of overlays used for occur matches.")
  9118. (make-variable-buffer-local 'org-occur-highlights)
  9119. (defvar org-occur-parameters nil
  9120. "Parameters of the active org-occur calls.
  9121. This is a list, each call to org-occur pushes as cons cell,
  9122. containing the regular expression and the callback, onto the list.
  9123. The list can contain several entries if `org-occur' has been called
  9124. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9125. will only contain one set of parameters. When the highlights are
  9126. removed (for example with `C-c C-c', or with the next edit (depending
  9127. on `org-remove-highlights-with-change'), this variable is emptied
  9128. as well.")
  9129. (make-variable-buffer-local 'org-occur-parameters)
  9130. (defun org-occur (regexp &optional keep-previous callback)
  9131. "Make a compact tree which shows all matches of REGEXP.
  9132. The tree will show the lines where the regexp matches, and all higher
  9133. headlines above the match. It will also show the heading after the match,
  9134. to make sure editing the matching entry is easy.
  9135. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9136. call to `org-occur' will be kept, to allow stacking of calls to this
  9137. command.
  9138. If CALLBACK is non-nil, it is a function which is called to confirm
  9139. that the match should indeed be shown."
  9140. (interactive "sRegexp: \nP")
  9141. (unless keep-previous
  9142. (org-remove-occur-highlights nil nil t))
  9143. (push (cons regexp callback) org-occur-parameters)
  9144. (let ((cnt 0))
  9145. (save-excursion
  9146. (goto-char (point-min))
  9147. (if (or (not keep-previous) ; do not want to keep
  9148. (not org-occur-highlights)) ; no previous matches
  9149. ;; hide everything
  9150. (org-overview))
  9151. (while (re-search-forward regexp nil t)
  9152. (when (or (not callback)
  9153. (save-match-data (funcall callback)))
  9154. (setq cnt (1+ cnt))
  9155. (when org-highlight-sparse-tree-matches
  9156. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9157. (org-show-context 'occur-tree))))
  9158. (when org-remove-highlights-with-change
  9159. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9160. nil 'local))
  9161. (unless org-sparse-tree-open-archived-trees
  9162. (org-hide-archived-subtrees (point-min) (point-max)))
  9163. (run-hooks 'org-occur-hook)
  9164. (if (interactive-p)
  9165. (message "%d match(es) for regexp %s" cnt regexp))
  9166. cnt))
  9167. (defun org-show-context (&optional key)
  9168. "Make sure point and context and visible.
  9169. How much context is shown depends upon the variables
  9170. `org-show-hierarchy-above', `org-show-following-heading'. and
  9171. `org-show-siblings'."
  9172. (let ((heading-p (org-on-heading-p t))
  9173. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9174. (following-p (org-get-alist-option org-show-following-heading key))
  9175. (entry-p (org-get-alist-option org-show-entry-below key))
  9176. (siblings-p (org-get-alist-option org-show-siblings key)))
  9177. (catch 'exit
  9178. ;; Show heading or entry text
  9179. (if (and heading-p (not entry-p))
  9180. (org-flag-heading nil) ; only show the heading
  9181. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9182. (org-show-hidden-entry))) ; show entire entry
  9183. (when following-p
  9184. ;; Show next sibling, or heading below text
  9185. (save-excursion
  9186. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9187. (org-flag-heading nil))))
  9188. (when siblings-p (org-show-siblings))
  9189. (when hierarchy-p
  9190. ;; show all higher headings, possibly with siblings
  9191. (save-excursion
  9192. (while (and (condition-case nil
  9193. (progn (org-up-heading-all 1) t)
  9194. (error nil))
  9195. (not (bobp)))
  9196. (org-flag-heading nil)
  9197. (when siblings-p (org-show-siblings))))))))
  9198. (defun org-reveal (&optional siblings)
  9199. "Show current entry, hierarchy above it, and the following headline.
  9200. This can be used to show a consistent set of context around locations
  9201. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9202. not t for the search context.
  9203. With optional argument SIBLINGS, on each level of the hierarchy all
  9204. siblings are shown. This repairs the tree structure to what it would
  9205. look like when opened with hierarchical calls to `org-cycle'."
  9206. (interactive "P")
  9207. (let ((org-show-hierarchy-above t)
  9208. (org-show-following-heading t)
  9209. (org-show-siblings (if siblings t org-show-siblings)))
  9210. (org-show-context nil)))
  9211. (defun org-highlight-new-match (beg end)
  9212. "Highlight from BEG to END and mark the highlight is an occur headline."
  9213. (let ((ov (org-make-overlay beg end)))
  9214. (org-overlay-put ov 'face 'secondary-selection)
  9215. (push ov org-occur-highlights)))
  9216. (defun org-remove-occur-highlights (&optional beg end noremove)
  9217. "Remove the occur highlights from the buffer.
  9218. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9219. from the `before-change-functions' in the current buffer."
  9220. (interactive)
  9221. (unless org-inhibit-highlight-removal
  9222. (mapc 'org-delete-overlay org-occur-highlights)
  9223. (setq org-occur-highlights nil)
  9224. (setq org-occur-parameters nil)
  9225. (unless noremove
  9226. (remove-hook 'before-change-functions
  9227. 'org-remove-occur-highlights 'local))))
  9228. ;;;; Priorities
  9229. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9230. "Regular expression matching the priority indicator.")
  9231. (defvar org-remove-priority-next-time nil)
  9232. (defun org-priority-up ()
  9233. "Increase the priority of the current item."
  9234. (interactive)
  9235. (org-priority 'up))
  9236. (defun org-priority-down ()
  9237. "Decrease the priority of the current item."
  9238. (interactive)
  9239. (org-priority 'down))
  9240. (defun org-priority (&optional action)
  9241. "Change the priority of an item by ARG.
  9242. ACTION can be `set', `up', `down', or a character."
  9243. (interactive)
  9244. (setq action (or action 'set))
  9245. (let (current new news have remove)
  9246. (save-excursion
  9247. (org-back-to-heading t)
  9248. (if (looking-at org-priority-regexp)
  9249. (setq current (string-to-char (match-string 2))
  9250. have t)
  9251. (setq current org-default-priority))
  9252. (cond
  9253. ((or (eq action 'set)
  9254. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9255. (if (not (eq action 'set))
  9256. (setq new action)
  9257. (message "Priority %c-%c, SPC to remove: "
  9258. org-highest-priority org-lowest-priority)
  9259. (setq new (read-char-exclusive)))
  9260. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9261. (= (upcase org-lowest-priority) org-lowest-priority))
  9262. (setq new (upcase new)))
  9263. (cond ((equal new ?\ ) (setq remove t))
  9264. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9265. (error "Priority must be between `%c' and `%c'"
  9266. org-highest-priority org-lowest-priority))))
  9267. ((eq action 'up)
  9268. (if (and (not have) (eq last-command this-command))
  9269. (setq new org-lowest-priority)
  9270. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9271. org-default-priority (1- current)))))
  9272. ((eq action 'down)
  9273. (if (and (not have) (eq last-command this-command))
  9274. (setq new org-highest-priority)
  9275. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9276. org-default-priority (1+ current)))))
  9277. (t (error "Invalid action")))
  9278. (if (or (< (upcase new) org-highest-priority)
  9279. (> (upcase new) org-lowest-priority))
  9280. (setq remove t))
  9281. (setq news (format "%c" new))
  9282. (if have
  9283. (if remove
  9284. (replace-match "" t t nil 1)
  9285. (replace-match news t t nil 2))
  9286. (if remove
  9287. (error "No priority cookie found in line")
  9288. (let ((case-fold-search nil))
  9289. (looking-at org-todo-line-regexp))
  9290. (if (match-end 2)
  9291. (progn
  9292. (goto-char (match-end 2))
  9293. (insert " [#" news "]"))
  9294. (goto-char (match-beginning 3))
  9295. (insert "[#" news "] ")))))
  9296. (org-preserve-lc (org-set-tags nil 'align))
  9297. (if remove
  9298. (message "Priority removed")
  9299. (message "Priority of current item set to %s" news))))
  9300. (defun org-get-priority (s)
  9301. "Find priority cookie and return priority."
  9302. (save-match-data
  9303. (if (not (string-match org-priority-regexp s))
  9304. (* 1000 (- org-lowest-priority org-default-priority))
  9305. (* 1000 (- org-lowest-priority
  9306. (string-to-char (match-string 2 s)))))))
  9307. ;;;; Tags
  9308. (defvar org-agenda-archives-mode)
  9309. (defvar org-map-continue-from nil
  9310. "Position from where mapping should continue.
  9311. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9312. (defvar org-scanner-tags nil
  9313. "The current tag list while the tags scanner is running.")
  9314. (defvar org-trust-scanner-tags nil
  9315. "Should `org-get-tags-at' use the tags fro the scanner.
  9316. This is for internal dynamical scoping only.
  9317. When this is non-nil, the function `org-get-tags-at' will return the value
  9318. of `org-scanner-tags' instead of building the list by itself. This
  9319. can lead to large speed-ups when the tags scanner is used in a file with
  9320. many entries, and when the list of tags is retrieved, for example to
  9321. obtain a list of properties. Building the tags list for each entry in such
  9322. a file becomes an N^2 operation - but with this variable set, it scales
  9323. as N.")
  9324. (defun org-scan-tags (action matcher &optional todo-only)
  9325. "Scan headline tags with inheritance and produce output ACTION.
  9326. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9327. or `agenda' to produce an entry list for an agenda view. It can also be
  9328. a Lisp form or a function that should be called at each matched headline, in
  9329. this case the return value is a list of all return values from these calls.
  9330. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9331. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9332. only lines with a TODO keyword are included in the output."
  9333. (require 'org-agenda)
  9334. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9335. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9336. (org-re
  9337. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9338. (props (list 'face 'default
  9339. 'done-face 'org-agenda-done
  9340. 'undone-face 'default
  9341. 'mouse-face 'highlight
  9342. 'org-not-done-regexp org-not-done-regexp
  9343. 'org-todo-regexp org-todo-regexp
  9344. 'keymap org-agenda-keymap
  9345. 'help-echo
  9346. (format "mouse-2 or RET jump to org file %s"
  9347. (abbreviate-file-name
  9348. (or (buffer-file-name (buffer-base-buffer))
  9349. (buffer-name (buffer-base-buffer)))))))
  9350. (case-fold-search nil)
  9351. (org-map-continue-from nil)
  9352. lspos tags tags-list
  9353. (tags-alist (list (cons 0 org-file-tags)))
  9354. (llast 0) rtn rtn1 level category i txt
  9355. todo marker entry priority)
  9356. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9357. (setq action (list 'lambda nil action)))
  9358. (save-excursion
  9359. (goto-char (point-min))
  9360. (when (eq action 'sparse-tree)
  9361. (org-overview)
  9362. (org-remove-occur-highlights))
  9363. (while (re-search-forward re nil t)
  9364. (catch :skip
  9365. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9366. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9367. (goto-char (setq lspos (match-beginning 0)))
  9368. (setq level (org-reduced-level (funcall outline-level))
  9369. category (org-get-category))
  9370. (setq i llast llast level)
  9371. ;; remove tag lists from same and sublevels
  9372. (while (>= i level)
  9373. (when (setq entry (assoc i tags-alist))
  9374. (setq tags-alist (delete entry tags-alist)))
  9375. (setq i (1- i)))
  9376. ;; add the next tags
  9377. (when tags
  9378. (setq tags (org-split-string tags ":")
  9379. tags-alist
  9380. (cons (cons level tags) tags-alist)))
  9381. ;; compile tags for current headline
  9382. (setq tags-list
  9383. (if org-use-tag-inheritance
  9384. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9385. tags)
  9386. org-scanner-tags tags-list)
  9387. (when org-use-tag-inheritance
  9388. (setcdr (car tags-alist)
  9389. (mapcar (lambda (x)
  9390. (setq x (copy-sequence x))
  9391. (org-add-prop-inherited x))
  9392. (cdar tags-alist))))
  9393. (when (and tags org-use-tag-inheritance
  9394. (or (not (eq t org-use-tag-inheritance))
  9395. org-tags-exclude-from-inheritance))
  9396. ;; selective inheritance, remove uninherited ones
  9397. (setcdr (car tags-alist)
  9398. (org-remove-uniherited-tags (cdar tags-alist))))
  9399. (when (and (or (not todo-only)
  9400. (and (member todo org-not-done-keywords)
  9401. (or (not org-agenda-tags-todo-honor-ignore-options)
  9402. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9403. (let ((case-fold-search t)) (eval matcher))
  9404. (or
  9405. (not (member org-archive-tag tags-list))
  9406. ;; we have an archive tag, should we use this anyway?
  9407. (or (not org-agenda-skip-archived-trees)
  9408. (and (eq action 'agenda) org-agenda-archives-mode))))
  9409. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9410. ;; select this headline
  9411. (cond
  9412. ((eq action 'sparse-tree)
  9413. (and org-highlight-sparse-tree-matches
  9414. (org-get-heading) (match-end 0)
  9415. (org-highlight-new-match
  9416. (match-beginning 0) (match-beginning 1)))
  9417. (org-show-context 'tags-tree))
  9418. ((eq action 'agenda)
  9419. (setq txt (org-format-agenda-item
  9420. ""
  9421. (concat
  9422. (if (eq org-tags-match-list-sublevels 'indented)
  9423. (make-string (1- level) ?.) "")
  9424. (org-get-heading))
  9425. category
  9426. tags-list
  9427. )
  9428. priority (org-get-priority txt))
  9429. (goto-char lspos)
  9430. (setq marker (org-agenda-new-marker))
  9431. (org-add-props txt props
  9432. 'org-marker marker 'org-hd-marker marker 'org-category category
  9433. 'todo-state todo
  9434. 'priority priority 'type "tagsmatch")
  9435. (push txt rtn))
  9436. ((functionp action)
  9437. (setq org-map-continue-from nil)
  9438. (save-excursion
  9439. (setq rtn1 (funcall action))
  9440. (push rtn1 rtn)))
  9441. (t (error "Invalid action")))
  9442. ;; if we are to skip sublevels, jump to end of subtree
  9443. (unless org-tags-match-list-sublevels
  9444. (org-end-of-subtree t)
  9445. (backward-char 1))))
  9446. ;; Get the correct position from where to continue
  9447. (if org-map-continue-from
  9448. (goto-char org-map-continue-from)
  9449. (and (= (point) lspos) (end-of-line 1)))))
  9450. (when (and (eq action 'sparse-tree)
  9451. (not org-sparse-tree-open-archived-trees))
  9452. (org-hide-archived-subtrees (point-min) (point-max)))
  9453. (nreverse rtn)))
  9454. (defun org-remove-uniherited-tags (tags)
  9455. "Remove all tags that are not inherited from the list TAGS."
  9456. (cond
  9457. ((eq org-use-tag-inheritance t)
  9458. (if org-tags-exclude-from-inheritance
  9459. (org-delete-all org-tags-exclude-from-inheritance tags)
  9460. tags))
  9461. ((not org-use-tag-inheritance) nil)
  9462. ((stringp org-use-tag-inheritance)
  9463. (delq nil (mapcar
  9464. (lambda (x)
  9465. (if (and (string-match org-use-tag-inheritance x)
  9466. (not (member x org-tags-exclude-from-inheritance)))
  9467. x nil))
  9468. tags)))
  9469. ((listp org-use-tag-inheritance)
  9470. (delq nil (mapcar
  9471. (lambda (x)
  9472. (if (member x org-use-tag-inheritance) x nil))
  9473. tags)))))
  9474. (defvar todo-only) ;; dynamically scoped
  9475. (defun org-match-sparse-tree (&optional todo-only match)
  9476. "Create a sparse tree according to tags string MATCH.
  9477. MATCH can contain positive and negative selection of tags, like
  9478. \"+WORK+URGENT-WITHBOSS\".
  9479. If optional argument TODO-ONLY is non-nil, only select lines that are
  9480. also TODO lines."
  9481. (interactive "P")
  9482. (org-prepare-agenda-buffers (list (current-buffer)))
  9483. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9484. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9485. (defvar org-cached-props nil)
  9486. (defun org-cached-entry-get (pom property)
  9487. (if (or (eq t org-use-property-inheritance)
  9488. (and (stringp org-use-property-inheritance)
  9489. (string-match org-use-property-inheritance property))
  9490. (and (listp org-use-property-inheritance)
  9491. (member property org-use-property-inheritance)))
  9492. ;; Caching is not possible, check it directly
  9493. (org-entry-get pom property 'inherit)
  9494. ;; Get all properties, so that we can do complicated checks easily
  9495. (cdr (assoc property (or org-cached-props
  9496. (setq org-cached-props
  9497. (org-entry-properties pom)))))))
  9498. (defun org-global-tags-completion-table (&optional files)
  9499. "Return the list of all tags in all agenda buffer/files."
  9500. (save-excursion
  9501. (org-uniquify
  9502. (delq nil
  9503. (apply 'append
  9504. (mapcar
  9505. (lambda (file)
  9506. (set-buffer (find-file-noselect file))
  9507. (append (org-get-buffer-tags)
  9508. (mapcar (lambda (x) (if (stringp (car-safe x))
  9509. (list (car-safe x)) nil))
  9510. org-tag-alist)))
  9511. (if (and files (car files))
  9512. files
  9513. (org-agenda-files))))))))
  9514. (defun org-make-tags-matcher (match)
  9515. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9516. ;; todo-only is scoped dynamically into this function, and the function
  9517. ;; may change it if the matcher asks for it.
  9518. (unless match
  9519. ;; Get a new match request, with completion
  9520. (let ((org-last-tags-completion-table
  9521. (org-global-tags-completion-table)))
  9522. (setq match (org-completing-read-no-ido
  9523. "Match: " 'org-tags-completion-function nil nil nil
  9524. 'org-tags-history))))
  9525. ;; Parse the string and create a lisp form
  9526. (let ((match0 match)
  9527. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9528. minus tag mm
  9529. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9530. orterms term orlist re-p str-p level-p level-op time-p
  9531. prop-p pn pv po cat-p gv rest)
  9532. (if (string-match "/+" match)
  9533. ;; match contains also a todo-matching request
  9534. (progn
  9535. (setq tagsmatch (substring match 0 (match-beginning 0))
  9536. todomatch (substring match (match-end 0)))
  9537. (if (string-match "^!" todomatch)
  9538. (setq todo-only t todomatch (substring todomatch 1)))
  9539. (if (string-match "^\\s-*$" todomatch)
  9540. (setq todomatch nil)))
  9541. ;; only matching tags
  9542. (setq tagsmatch match todomatch nil))
  9543. ;; Make the tags matcher
  9544. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9545. (setq tagsmatcher t)
  9546. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9547. (while (setq term (pop orterms))
  9548. (while (and (equal (substring term -1) "\\") orterms)
  9549. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9550. (while (string-match re term)
  9551. (setq rest (substring term (match-end 0))
  9552. minus (and (match-end 1)
  9553. (equal (match-string 1 term) "-"))
  9554. tag (match-string 2 term)
  9555. re-p (equal (string-to-char tag) ?{)
  9556. level-p (match-end 4)
  9557. prop-p (match-end 5)
  9558. mm (cond
  9559. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9560. (level-p
  9561. (setq level-op (org-op-to-function (match-string 3 term)))
  9562. `(,level-op level ,(string-to-number
  9563. (match-string 4 term))))
  9564. (prop-p
  9565. (setq pn (match-string 5 term)
  9566. po (match-string 6 term)
  9567. pv (match-string 7 term)
  9568. cat-p (equal pn "CATEGORY")
  9569. re-p (equal (string-to-char pv) ?{)
  9570. str-p (equal (string-to-char pv) ?\")
  9571. time-p (save-match-data
  9572. (string-match "^\"[[<].*[]>]\"$" pv))
  9573. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9574. (if time-p (setq pv (org-matcher-time pv)))
  9575. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9576. (cond
  9577. ((equal pn "CATEGORY")
  9578. (setq gv '(get-text-property (point) 'org-category)))
  9579. ((equal pn "TODO")
  9580. (setq gv 'todo))
  9581. (t
  9582. (setq gv `(org-cached-entry-get nil ,pn))))
  9583. (if re-p
  9584. (if (eq po 'org<>)
  9585. `(not (string-match ,pv (or ,gv "")))
  9586. `(string-match ,pv (or ,gv "")))
  9587. (if str-p
  9588. `(,po (or ,gv "") ,pv)
  9589. `(,po (string-to-number (or ,gv ""))
  9590. ,(string-to-number pv) ))))
  9591. (t `(member ,tag tags-list)))
  9592. mm (if minus (list 'not mm) mm)
  9593. term rest)
  9594. (push mm tagsmatcher))
  9595. (push (if (> (length tagsmatcher) 1)
  9596. (cons 'and tagsmatcher)
  9597. (car tagsmatcher))
  9598. orlist)
  9599. (setq tagsmatcher nil))
  9600. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9601. (setq tagsmatcher
  9602. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9603. ;; Make the todo matcher
  9604. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9605. (setq todomatcher t)
  9606. (setq orterms (org-split-string todomatch "|") orlist nil)
  9607. (while (setq term (pop orterms))
  9608. (while (string-match re term)
  9609. (setq minus (and (match-end 1)
  9610. (equal (match-string 1 term) "-"))
  9611. kwd (match-string 2 term)
  9612. re-p (equal (string-to-char kwd) ?{)
  9613. term (substring term (match-end 0))
  9614. mm (if re-p
  9615. `(string-match ,(substring kwd 1 -1) todo)
  9616. (list 'equal 'todo kwd))
  9617. mm (if minus (list 'not mm) mm))
  9618. (push mm todomatcher))
  9619. (push (if (> (length todomatcher) 1)
  9620. (cons 'and todomatcher)
  9621. (car todomatcher))
  9622. orlist)
  9623. (setq todomatcher nil))
  9624. (setq todomatcher (if (> (length orlist) 1)
  9625. (cons 'or orlist) (car orlist))))
  9626. ;; Return the string and lisp forms of the matcher
  9627. (setq matcher (if todomatcher
  9628. (list 'and tagsmatcher todomatcher)
  9629. tagsmatcher))
  9630. (cons match0 matcher)))
  9631. (defun org-op-to-function (op &optional stringp)
  9632. "Turn an operator into the appropriate function."
  9633. (setq op
  9634. (cond
  9635. ((equal op "<" ) '(< string< org-time<))
  9636. ((equal op ">" ) '(> org-string> org-time>))
  9637. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9638. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9639. ((member op '("=" "==")) '(= string= org-time=))
  9640. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9641. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9642. (defun org<> (a b) (not (= a b)))
  9643. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9644. (defun org-string>= (a b) (not (string< a b)))
  9645. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9646. (defun org-string<> (a b) (not (string= a b)))
  9647. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9648. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9649. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9650. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9651. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9652. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9653. (defun org-2ft (s)
  9654. "Convert S to a floating point time.
  9655. If S is already a number, just return it. If it is a string, parse
  9656. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9657. (cond
  9658. ((numberp s) s)
  9659. ((stringp s)
  9660. (condition-case nil
  9661. (float-time (apply 'encode-time (org-parse-time-string s)))
  9662. (error 0.)))
  9663. (t 0.)))
  9664. (defun org-time-today ()
  9665. "Time in seconds today at 0:00.
  9666. Returns the float number of seconds since the beginning of the
  9667. epoch to the beginning of today (00:00)."
  9668. (float-time (apply 'encode-time
  9669. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9670. (defun org-matcher-time (s)
  9671. "Interpret a time comparison value."
  9672. (save-match-data
  9673. (cond
  9674. ((string= s "<now>") (float-time))
  9675. ((string= s "<today>") (org-time-today))
  9676. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9677. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9678. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9679. (+ (org-time-today)
  9680. (* (string-to-number (match-string 1 s))
  9681. (cdr (assoc (match-string 2 s)
  9682. '(("d" . 86400.0) ("w" . 604800.0)
  9683. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9684. (t (org-2ft s)))))
  9685. (defun org-match-any-p (re list)
  9686. "Does re match any element of list?"
  9687. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9688. (delq nil list))
  9689. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9690. (defvar org-tags-overlay (org-make-overlay 1 1))
  9691. (org-detach-overlay org-tags-overlay)
  9692. (defun org-get-local-tags-at (&optional pos)
  9693. "Get a list of tags defined in the current headline."
  9694. (org-get-tags-at pos 'local))
  9695. (defun org-get-local-tags ()
  9696. "Get a list of tags defined in the current headline."
  9697. (org-get-tags-at nil 'local))
  9698. (defun org-get-tags-at (&optional pos local)
  9699. "Get a list of all headline tags applicable at POS.
  9700. POS defaults to point. If tags are inherited, the list contains
  9701. the targets in the same sequence as the headlines appear, i.e.
  9702. the tags of the current headline come last.
  9703. When LOCAL is non-nil, only return tags from the current headline,
  9704. ignore inherited ones."
  9705. (interactive)
  9706. (if (and org-trust-scanner-tags
  9707. (or (not pos) (equal pos (point)))
  9708. (not local))
  9709. org-scanner-tags
  9710. (let (tags ltags lastpos parent)
  9711. (save-excursion
  9712. (save-restriction
  9713. (widen)
  9714. (goto-char (or pos (point)))
  9715. (save-match-data
  9716. (catch 'done
  9717. (condition-case nil
  9718. (progn
  9719. (org-back-to-heading t)
  9720. (while (not (equal lastpos (point)))
  9721. (setq lastpos (point))
  9722. (when (looking-at
  9723. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9724. (setq ltags (org-split-string
  9725. (org-match-string-no-properties 1) ":"))
  9726. (when parent
  9727. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9728. (setq tags (append
  9729. (if parent
  9730. (org-remove-uniherited-tags ltags)
  9731. ltags)
  9732. tags)))
  9733. (or org-use-tag-inheritance (throw 'done t))
  9734. (if local (throw 'done t))
  9735. (or (org-up-heading-safe) (error nil))
  9736. (setq parent t)))
  9737. (error nil)))))
  9738. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9739. (defun org-add-prop-inherited (s)
  9740. (add-text-properties 0 (length s) '(inherited t) s)
  9741. s)
  9742. (defun org-toggle-tag (tag &optional onoff)
  9743. "Toggle the tag TAG for the current line.
  9744. If ONOFF is `on' or `off', don't toggle but set to this state."
  9745. (let (res current)
  9746. (save-excursion
  9747. (org-back-to-heading t)
  9748. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9749. (point-at-eol) t)
  9750. (progn
  9751. (setq current (match-string 1))
  9752. (replace-match ""))
  9753. (setq current ""))
  9754. (setq current (nreverse (org-split-string current ":")))
  9755. (cond
  9756. ((eq onoff 'on)
  9757. (setq res t)
  9758. (or (member tag current) (push tag current)))
  9759. ((eq onoff 'off)
  9760. (or (not (member tag current)) (setq current (delete tag current))))
  9761. (t (if (member tag current)
  9762. (setq current (delete tag current))
  9763. (setq res t)
  9764. (push tag current))))
  9765. (end-of-line 1)
  9766. (if current
  9767. (progn
  9768. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9769. (org-set-tags nil t))
  9770. (delete-horizontal-space))
  9771. (run-hooks 'org-after-tags-change-hook))
  9772. res))
  9773. (defun org-align-tags-here (to-col)
  9774. ;; Assumes that this is a headline
  9775. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9776. (beginning-of-line 1)
  9777. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9778. (< pos (match-beginning 2)))
  9779. (progn
  9780. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9781. (goto-char (match-beginning 1))
  9782. (insert " ")
  9783. (delete-region (point) (1+ (match-beginning 2)))
  9784. (setq ncol (max (1+ (current-column))
  9785. (1+ col)
  9786. (if (> to-col 0)
  9787. to-col
  9788. (- (abs to-col) tags-l))))
  9789. (setq p (point))
  9790. (insert (make-string (- ncol (current-column)) ?\ ))
  9791. (setq ncol (current-column))
  9792. (when indent-tabs-mode (tabify p (point-at-eol)))
  9793. (org-move-to-column (min ncol col) t))
  9794. (goto-char pos))))
  9795. (defun org-set-tags-command (&optional arg just-align)
  9796. "Call the set-tags command for the current entry."
  9797. (interactive "P")
  9798. (if (org-on-heading-p)
  9799. (org-set-tags arg just-align)
  9800. (save-excursion
  9801. (org-back-to-heading t)
  9802. (org-set-tags arg just-align))))
  9803. (defun org-set-tags (&optional arg just-align)
  9804. "Set the tags for the current headline.
  9805. With prefix ARG, realign all tags in headings in the current buffer."
  9806. (interactive "P")
  9807. (let* ((re (concat "^" outline-regexp))
  9808. (current (org-get-tags-string))
  9809. (col (current-column))
  9810. (org-setting-tags t)
  9811. table current-tags inherited-tags ; computed below when needed
  9812. tags p0 c0 c1 rpl)
  9813. (if arg
  9814. (save-excursion
  9815. (goto-char (point-min))
  9816. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9817. (while (re-search-forward re nil t)
  9818. (org-set-tags nil t)
  9819. (end-of-line 1)))
  9820. (message "All tags realigned to column %d" org-tags-column))
  9821. (if just-align
  9822. (setq tags current)
  9823. ;; Get a new set of tags from the user
  9824. (save-excursion
  9825. (setq table (append org-tag-persistent-alist
  9826. (or org-tag-alist (org-get-buffer-tags)))
  9827. org-last-tags-completion-table table
  9828. current-tags (org-split-string current ":")
  9829. inherited-tags (nreverse
  9830. (nthcdr (length current-tags)
  9831. (nreverse (org-get-tags-at))))
  9832. tags
  9833. (if (or (eq t org-use-fast-tag-selection)
  9834. (and org-use-fast-tag-selection
  9835. (delq nil (mapcar 'cdr table))))
  9836. (org-fast-tag-selection
  9837. current-tags inherited-tags table
  9838. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9839. (let ((org-add-colon-after-tag-completion t))
  9840. (org-trim
  9841. (org-without-partial-completion
  9842. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9843. nil nil current 'org-tags-history)))))))
  9844. (while (string-match "[-+&]+" tags)
  9845. ;; No boolean logic, just a list
  9846. (setq tags (replace-match ":" t t tags))))
  9847. (if (string-match "\\`[\t ]*\\'" tags)
  9848. (setq tags "")
  9849. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9850. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9851. ;; Insert new tags at the correct column
  9852. (beginning-of-line 1)
  9853. (cond
  9854. ((and (equal current "") (equal tags "")))
  9855. ((re-search-forward
  9856. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9857. (point-at-eol) t)
  9858. (if (equal tags "")
  9859. (setq rpl "")
  9860. (goto-char (match-beginning 0))
  9861. (setq c0 (current-column) p0 (point)
  9862. c1 (max (1+ c0) (if (> org-tags-column 0)
  9863. org-tags-column
  9864. (- (- org-tags-column) (length tags))))
  9865. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9866. (replace-match rpl t t)
  9867. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9868. tags)
  9869. (t (error "Tags alignment failed")))
  9870. (org-move-to-column col)
  9871. (unless just-align
  9872. (run-hooks 'org-after-tags-change-hook)))))
  9873. (defun org-change-tag-in-region (beg end tag off)
  9874. "Add or remove TAG for each entry in the region.
  9875. This works in the agenda, and also in an org-mode buffer."
  9876. (interactive
  9877. (list (region-beginning) (region-end)
  9878. (let ((org-last-tags-completion-table
  9879. (if (org-mode-p)
  9880. (org-get-buffer-tags)
  9881. (org-global-tags-completion-table))))
  9882. (org-ido-completing-read
  9883. "Tag: " 'org-tags-completion-function nil nil nil
  9884. 'org-tags-history))
  9885. (progn
  9886. (message "[s]et or [r]emove? ")
  9887. (equal (read-char-exclusive) ?r))))
  9888. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9889. (let ((agendap (equal major-mode 'org-agenda-mode))
  9890. l1 l2 m buf pos newhead (cnt 0))
  9891. (goto-char end)
  9892. (setq l2 (1- (org-current-line)))
  9893. (goto-char beg)
  9894. (setq l1 (org-current-line))
  9895. (loop for l from l1 to l2 do
  9896. (goto-line l)
  9897. (setq m (get-text-property (point) 'org-hd-marker))
  9898. (when (or (and (org-mode-p) (org-on-heading-p))
  9899. (and agendap m))
  9900. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9901. pos (if agendap m (point)))
  9902. (with-current-buffer buf
  9903. (save-excursion
  9904. (save-restriction
  9905. (goto-char pos)
  9906. (setq cnt (1+ cnt))
  9907. (org-toggle-tag tag (if off 'off 'on))
  9908. (setq newhead (org-get-heading)))))
  9909. (and agendap (org-agenda-change-all-lines newhead m))))
  9910. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9911. (defun org-tags-completion-function (string predicate &optional flag)
  9912. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9913. (confirm (lambda (x) (stringp (car x)))))
  9914. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9915. (setq s1 (match-string 1 string)
  9916. s2 (match-string 2 string))
  9917. (setq s1 "" s2 string))
  9918. (cond
  9919. ((eq flag nil)
  9920. ;; try completion
  9921. (setq rtn (try-completion s2 ctable confirm))
  9922. (if (stringp rtn)
  9923. (setq rtn
  9924. (concat s1 s2 (substring rtn (length s2))
  9925. (if (and org-add-colon-after-tag-completion
  9926. (assoc rtn ctable))
  9927. ":" ""))))
  9928. rtn)
  9929. ((eq flag t)
  9930. ;; all-completions
  9931. (all-completions s2 ctable confirm)
  9932. )
  9933. ((eq flag 'lambda)
  9934. ;; exact match?
  9935. (assoc s2 ctable)))
  9936. ))
  9937. (defun org-fast-tag-insert (kwd tags face &optional end)
  9938. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9939. (insert (format "%-12s" (concat kwd ":"))
  9940. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9941. (or end "")))
  9942. (defun org-fast-tag-show-exit (flag)
  9943. (save-excursion
  9944. (goto-line 3)
  9945. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9946. (replace-match ""))
  9947. (when flag
  9948. (end-of-line 1)
  9949. (org-move-to-column (- (window-width) 19) t)
  9950. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9951. (defun org-set-current-tags-overlay (current prefix)
  9952. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9953. (if (featurep 'xemacs)
  9954. (org-overlay-display org-tags-overlay (concat prefix s)
  9955. 'secondary-selection)
  9956. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9957. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9958. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9959. "Fast tag selection with single keys.
  9960. CURRENT is the current list of tags in the headline, INHERITED is the
  9961. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9962. possibly with grouping information. TODO-TABLE is a similar table with
  9963. TODO keywords, should these have keys assigned to them.
  9964. If the keys are nil, a-z are automatically assigned.
  9965. Returns the new tags string, or nil to not change the current settings."
  9966. (let* ((fulltable (append table todo-table))
  9967. (maxlen (apply 'max (mapcar
  9968. (lambda (x)
  9969. (if (stringp (car x)) (string-width (car x)) 0))
  9970. fulltable)))
  9971. (buf (current-buffer))
  9972. (expert (eq org-fast-tag-selection-single-key 'expert))
  9973. (buffer-tags nil)
  9974. (fwidth (+ maxlen 3 1 3))
  9975. (ncol (/ (- (window-width) 4) fwidth))
  9976. (i-face 'org-done)
  9977. (c-face 'org-todo)
  9978. tg cnt e c char c1 c2 ntable tbl rtn
  9979. ov-start ov-end ov-prefix
  9980. (exit-after-next org-fast-tag-selection-single-key)
  9981. (done-keywords org-done-keywords)
  9982. groups ingroup)
  9983. (save-excursion
  9984. (beginning-of-line 1)
  9985. (if (looking-at
  9986. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9987. (setq ov-start (match-beginning 1)
  9988. ov-end (match-end 1)
  9989. ov-prefix "")
  9990. (setq ov-start (1- (point-at-eol))
  9991. ov-end (1+ ov-start))
  9992. (skip-chars-forward "^\n\r")
  9993. (setq ov-prefix
  9994. (concat
  9995. (buffer-substring (1- (point)) (point))
  9996. (if (> (current-column) org-tags-column)
  9997. " "
  9998. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9999. (org-move-overlay org-tags-overlay ov-start ov-end)
  10000. (save-window-excursion
  10001. (if expert
  10002. (set-buffer (get-buffer-create " *Org tags*"))
  10003. (delete-other-windows)
  10004. (split-window-vertically)
  10005. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10006. (erase-buffer)
  10007. (org-set-local 'org-done-keywords done-keywords)
  10008. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10009. (org-fast-tag-insert "Current" current c-face "\n\n")
  10010. (org-fast-tag-show-exit exit-after-next)
  10011. (org-set-current-tags-overlay current ov-prefix)
  10012. (setq tbl fulltable char ?a cnt 0)
  10013. (while (setq e (pop tbl))
  10014. (cond
  10015. ((equal e '(:startgroup))
  10016. (push '() groups) (setq ingroup t)
  10017. (when (not (= cnt 0))
  10018. (setq cnt 0)
  10019. (insert "\n"))
  10020. (insert "{ "))
  10021. ((equal e '(:endgroup))
  10022. (setq ingroup nil cnt 0)
  10023. (insert "}\n"))
  10024. ((equal e '(:newline))
  10025. (when (not (= cnt 0))
  10026. (setq cnt 0)
  10027. (insert "\n")
  10028. (setq e (car tbl))
  10029. (while (equal (car tbl) '(:newline))
  10030. (insert "\n")
  10031. (setq tbl (cdr tbl)))))
  10032. (t
  10033. (setq tg (car e) c2 nil)
  10034. (if (cdr e)
  10035. (setq c (cdr e))
  10036. ;; automatically assign a character.
  10037. (setq c1 (string-to-char
  10038. (downcase (substring
  10039. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10040. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10041. (while (or (rassoc char ntable) (rassoc char table))
  10042. (setq char (1+ char)))
  10043. (setq c2 c1))
  10044. (setq c (or c2 char)))
  10045. (if ingroup (push tg (car groups)))
  10046. (setq tg (org-add-props tg nil 'face
  10047. (cond
  10048. ((not (assoc tg table))
  10049. (org-get-todo-face tg))
  10050. ((member tg current) c-face)
  10051. ((member tg inherited) i-face)
  10052. (t nil))))
  10053. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10054. (insert "[" c "] " tg (make-string
  10055. (- fwidth 4 (length tg)) ?\ ))
  10056. (push (cons tg c) ntable)
  10057. (when (= (setq cnt (1+ cnt)) ncol)
  10058. (insert "\n")
  10059. (if ingroup (insert " "))
  10060. (setq cnt 0)))))
  10061. (setq ntable (nreverse ntable))
  10062. (insert "\n")
  10063. (goto-char (point-min))
  10064. (if (not expert) (org-fit-window-to-buffer))
  10065. (setq rtn
  10066. (catch 'exit
  10067. (while t
  10068. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10069. (if groups " [!] no groups" " [!]groups")
  10070. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10071. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10072. (cond
  10073. ((= c ?\r) (throw 'exit t))
  10074. ((= c ?!)
  10075. (setq groups (not groups))
  10076. (goto-char (point-min))
  10077. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10078. ((= c ?\C-c)
  10079. (if (not expert)
  10080. (org-fast-tag-show-exit
  10081. (setq exit-after-next (not exit-after-next)))
  10082. (setq expert nil)
  10083. (delete-other-windows)
  10084. (split-window-vertically)
  10085. (org-switch-to-buffer-other-window " *Org tags*")
  10086. (org-fit-window-to-buffer)))
  10087. ((or (= c ?\C-g)
  10088. (and (= c ?q) (not (rassoc c ntable))))
  10089. (org-detach-overlay org-tags-overlay)
  10090. (setq quit-flag t))
  10091. ((= c ?\ )
  10092. (setq current nil)
  10093. (if exit-after-next (setq exit-after-next 'now)))
  10094. ((= c ?\t)
  10095. (condition-case nil
  10096. (setq tg (org-ido-completing-read
  10097. "Tag: "
  10098. (or buffer-tags
  10099. (with-current-buffer buf
  10100. (org-get-buffer-tags)))))
  10101. (quit (setq tg "")))
  10102. (when (string-match "\\S-" tg)
  10103. (add-to-list 'buffer-tags (list tg))
  10104. (if (member tg current)
  10105. (setq current (delete tg current))
  10106. (push tg current)))
  10107. (if exit-after-next (setq exit-after-next 'now)))
  10108. ((setq e (rassoc c todo-table) tg (car e))
  10109. (with-current-buffer buf
  10110. (save-excursion (org-todo tg)))
  10111. (if exit-after-next (setq exit-after-next 'now)))
  10112. ((setq e (rassoc c ntable) tg (car e))
  10113. (if (member tg current)
  10114. (setq current (delete tg current))
  10115. (loop for g in groups do
  10116. (if (member tg g)
  10117. (mapc (lambda (x)
  10118. (setq current (delete x current)))
  10119. g)))
  10120. (push tg current))
  10121. (if exit-after-next (setq exit-after-next 'now))))
  10122. ;; Create a sorted list
  10123. (setq current
  10124. (sort current
  10125. (lambda (a b)
  10126. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10127. (if (eq exit-after-next 'now) (throw 'exit t))
  10128. (goto-char (point-min))
  10129. (beginning-of-line 2)
  10130. (delete-region (point) (point-at-eol))
  10131. (org-fast-tag-insert "Current" current c-face)
  10132. (org-set-current-tags-overlay current ov-prefix)
  10133. (while (re-search-forward
  10134. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10135. (setq tg (match-string 1))
  10136. (add-text-properties
  10137. (match-beginning 1) (match-end 1)
  10138. (list 'face
  10139. (cond
  10140. ((member tg current) c-face)
  10141. ((member tg inherited) i-face)
  10142. (t (get-text-property (match-beginning 1) 'face))))))
  10143. (goto-char (point-min)))))
  10144. (org-detach-overlay org-tags-overlay)
  10145. (if rtn
  10146. (mapconcat 'identity current ":")
  10147. nil))))
  10148. (defun org-get-tags-string ()
  10149. "Get the TAGS string in the current headline."
  10150. (unless (org-on-heading-p t)
  10151. (error "Not on a heading"))
  10152. (save-excursion
  10153. (beginning-of-line 1)
  10154. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10155. (org-match-string-no-properties 1)
  10156. "")))
  10157. (defun org-get-tags ()
  10158. "Get the list of tags specified in the current headline."
  10159. (org-split-string (org-get-tags-string) ":"))
  10160. (defun org-get-buffer-tags ()
  10161. "Get a table of all tags used in the buffer, for completion."
  10162. (let (tags)
  10163. (save-excursion
  10164. (goto-char (point-min))
  10165. (while (re-search-forward
  10166. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10167. (when (equal (char-after (point-at-bol 0)) ?*)
  10168. (mapc (lambda (x) (add-to-list 'tags x))
  10169. (org-split-string (org-match-string-no-properties 1) ":")))))
  10170. (mapcar 'list tags)))
  10171. ;;;; The mapping API
  10172. ;;;###autoload
  10173. (defun org-map-entries (func &optional match scope &rest skip)
  10174. "Call FUNC at each headline selected by MATCH in SCOPE.
  10175. FUNC is a function or a lisp form. The function will be called without
  10176. arguments, with the cursor positioned at the beginning of the headline.
  10177. The return values of all calls to the function will be collected and
  10178. returned as a list.
  10179. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10180. does not need to preserve point. After evaluaton, the cursor will be
  10181. moved to the end of the line (presumably of the headline of the
  10182. processed entry) and search continues from there. Under some
  10183. circumstances, this may not produce the wanted results. For example,
  10184. if you have removed (e.g. archived) the current (sub)tree it could
  10185. mean that the next entry will be skipped entirely. In such cases, you
  10186. can specify the position from where search should continue by making
  10187. FUNC set the variable `org-map-continue-from' to the desired buffer
  10188. position.
  10189. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10190. Only headlines that are matched by this query will be considered during
  10191. the iteration. When MATCH is nil or t, all headlines will be
  10192. visited by the iteration.
  10193. SCOPE determines the scope of this command. It can be any of:
  10194. nil The current buffer, respecting the restriction if any
  10195. tree The subtree started with the entry at point
  10196. file The current buffer, without restriction
  10197. file-with-archives
  10198. The current buffer, and any archives associated with it
  10199. agenda All agenda files
  10200. agenda-with-archives
  10201. All agenda files with any archive files associated with them
  10202. \(file1 file2 ...)
  10203. If this is a list, all files in the list will be scanned
  10204. The remaining args are treated as settings for the skipping facilities of
  10205. the scanner. The following items can be given here:
  10206. archive skip trees with the archive tag.
  10207. comment skip trees with the COMMENT keyword
  10208. function or Emacs Lisp form:
  10209. will be used as value for `org-agenda-skip-function', so whenever
  10210. the the function returns t, FUNC will not be called for that
  10211. entry and search will continue from the point where the
  10212. function leaves it.
  10213. If your function needs to retrieve the tags including inherited tags
  10214. at the *current* entry, you can use the value of the variable
  10215. `org-scanner-tags' which will be much faster than getting the value
  10216. with `org-get-tags-at'. If your function gets properties with
  10217. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10218. to t around the call to `org-entry-properties' to get the same speedup.
  10219. Note that if your function moves around to retrieve tags and properties at
  10220. a *different* entry, you cannot use these techniques."
  10221. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10222. (org-agenda-skip-archived-trees (memq 'archive skip))
  10223. (org-agenda-skip-comment-trees (memq 'comment skip))
  10224. (org-agenda-skip-function
  10225. (car (org-delete-all '(comment archive) skip)))
  10226. (org-tags-match-list-sublevels t)
  10227. matcher file res
  10228. org-todo-keywords-for-agenda
  10229. org-done-keywords-for-agenda
  10230. org-todo-keyword-alist-for-agenda
  10231. org-tag-alist-for-agenda)
  10232. (cond
  10233. ((eq match t) (setq matcher t))
  10234. ((eq match nil) (setq matcher t))
  10235. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10236. (save-excursion
  10237. (save-restriction
  10238. (when (eq scope 'tree)
  10239. (org-back-to-heading t)
  10240. (org-narrow-to-subtree)
  10241. (setq scope nil))
  10242. (if (not scope)
  10243. (progn
  10244. (org-prepare-agenda-buffers
  10245. (list (buffer-file-name (current-buffer))))
  10246. (setq res (org-scan-tags func matcher)))
  10247. ;; Get the right scope
  10248. (cond
  10249. ((and scope (listp scope) (symbolp (car scope)))
  10250. (setq scope (eval scope)))
  10251. ((eq scope 'agenda)
  10252. (setq scope (org-agenda-files t)))
  10253. ((eq scope 'agenda-with-archives)
  10254. (setq scope (org-agenda-files t))
  10255. (setq scope (org-add-archive-files scope)))
  10256. ((eq scope 'file)
  10257. (setq scope (list (buffer-file-name))))
  10258. ((eq scope 'file-with-archives)
  10259. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10260. (org-prepare-agenda-buffers scope)
  10261. (while (setq file (pop scope))
  10262. (with-current-buffer (org-find-base-buffer-visiting file)
  10263. (save-excursion
  10264. (save-restriction
  10265. (widen)
  10266. (goto-char (point-min))
  10267. (setq res (append res (org-scan-tags func matcher))))))))))
  10268. res))
  10269. ;;;; Properties
  10270. ;;; Setting and retrieving properties
  10271. (defconst org-special-properties
  10272. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10273. "TIMESTAMP" "TIMESTAMP_IA")
  10274. "The special properties valid in Org-mode.
  10275. These are properties that are not defined in the property drawer,
  10276. but in some other way.")
  10277. (defconst org-default-properties
  10278. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10279. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10280. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10281. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10282. "ORDERED" "NOBLOCKING" "COOKIE_DATA")
  10283. "Some properties that are used by Org-mode for various purposes.
  10284. Being in this list makes sure that they are offered for completion.")
  10285. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10286. "Regular expression matching the first line of a property drawer.")
  10287. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10288. "Regular expression matching the first line of a property drawer.")
  10289. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10290. "Regular expression matching the first line of a property drawer.")
  10291. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10292. "Regular expression matching the first line of a property drawer.")
  10293. (defconst org-property-drawer-re
  10294. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10295. org-property-end-re "\\)\n?")
  10296. "Matches an entire property drawer.")
  10297. (defconst org-clock-drawer-re
  10298. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10299. org-property-end-re "\\)\n?")
  10300. "Matches an entire clock drawer.")
  10301. (defun org-property-action ()
  10302. "Do an action on properties."
  10303. (interactive)
  10304. (let (c)
  10305. (org-at-property-p)
  10306. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10307. (setq c (read-char-exclusive))
  10308. (cond
  10309. ((equal c ?s)
  10310. (call-interactively 'org-set-property))
  10311. ((equal c ?d)
  10312. (call-interactively 'org-delete-property))
  10313. ((equal c ?D)
  10314. (call-interactively 'org-delete-property-globally))
  10315. ((equal c ?c)
  10316. (call-interactively 'org-compute-property-at-point))
  10317. (t (error "No such property action %c" c)))))
  10318. (defun org-at-property-p ()
  10319. "Is the cursor in a property line?"
  10320. ;; FIXME: Does not check if we are actually in the drawer.
  10321. ;; FIXME: also returns true on any drawers.....
  10322. ;; This is used by C-c C-c for property action.
  10323. (save-excursion
  10324. (beginning-of-line 1)
  10325. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10326. (defun org-get-property-block (&optional beg end force)
  10327. "Return the (beg . end) range of the body of the property drawer.
  10328. BEG and END can be beginning and end of subtree, if not given
  10329. they will be found.
  10330. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10331. (catch 'exit
  10332. (save-excursion
  10333. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10334. (end (or end (progn (outline-next-heading) (point)))))
  10335. (goto-char beg)
  10336. (if (re-search-forward org-property-start-re end t)
  10337. (setq beg (1+ (match-end 0)))
  10338. (if force
  10339. (save-excursion
  10340. (org-insert-property-drawer)
  10341. (setq end (progn (outline-next-heading) (point))))
  10342. (throw 'exit nil))
  10343. (goto-char beg)
  10344. (if (re-search-forward org-property-start-re end t)
  10345. (setq beg (1+ (match-end 0)))))
  10346. (if (re-search-forward org-property-end-re end t)
  10347. (setq end (match-beginning 0))
  10348. (or force (throw 'exit nil))
  10349. (goto-char beg)
  10350. (setq end beg)
  10351. (org-indent-line-function)
  10352. (insert ":END:\n"))
  10353. (cons beg end)))))
  10354. (defun org-entry-properties (&optional pom which)
  10355. "Get all properties of the entry at point-or-marker POM.
  10356. This includes the TODO keyword, the tags, time strings for deadline,
  10357. scheduled, and clocking, and any additional properties defined in the
  10358. entry. The return value is an alist, keys may occur multiple times
  10359. if the property key was used several times.
  10360. POM may also be nil, in which case the current entry is used.
  10361. If WHICH is nil or `all', get all properties. If WHICH is
  10362. `special' or `standard', only get that subclass."
  10363. (setq which (or which 'all))
  10364. (org-with-point-at pom
  10365. (let ((clockstr (substring org-clock-string 0 -1))
  10366. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10367. beg end range props sum-props key value string clocksum)
  10368. (save-excursion
  10369. (when (condition-case nil
  10370. (and (org-mode-p) (org-back-to-heading t))
  10371. (error nil))
  10372. (setq beg (point))
  10373. (setq sum-props (get-text-property (point) 'org-summaries))
  10374. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10375. (outline-next-heading)
  10376. (setq end (point))
  10377. (when (memq which '(all special))
  10378. ;; Get the special properties, like TODO and tags
  10379. (goto-char beg)
  10380. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10381. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10382. (when (looking-at org-priority-regexp)
  10383. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10384. (when (and (setq value (org-get-tags-string))
  10385. (string-match "\\S-" value))
  10386. (push (cons "TAGS" value) props))
  10387. (when (setq value (org-get-tags-at))
  10388. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10389. props))
  10390. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10391. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10392. string (if (equal key clockstr)
  10393. (org-no-properties
  10394. (org-trim
  10395. (buffer-substring
  10396. (match-beginning 3) (goto-char (point-at-eol)))))
  10397. (substring (org-match-string-no-properties 3) 1 -1)))
  10398. (unless key
  10399. (if (= (char-after (match-beginning 3)) ?\[)
  10400. (setq key "TIMESTAMP_IA")
  10401. (setq key "TIMESTAMP")))
  10402. (when (or (equal key clockstr) (not (assoc key props)))
  10403. (push (cons key string) props)))
  10404. )
  10405. (when (memq which '(all standard))
  10406. ;; Get the standard properties, like :PROP: ...
  10407. (setq range (org-get-property-block beg end))
  10408. (when range
  10409. (goto-char (car range))
  10410. (while (re-search-forward
  10411. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10412. (cdr range) t)
  10413. (setq key (org-match-string-no-properties 1)
  10414. value (org-trim (or (org-match-string-no-properties 2) "")))
  10415. (unless (member key excluded)
  10416. (push (cons key (or value "")) props)))))
  10417. (if clocksum
  10418. (push (cons "CLOCKSUM"
  10419. (org-columns-number-to-string (/ (float clocksum) 60.)
  10420. 'add_times))
  10421. props))
  10422. (unless (assoc "CATEGORY" props)
  10423. (setq value (or (org-get-category)
  10424. (progn (org-refresh-category-properties)
  10425. (org-get-category))))
  10426. (push (cons "CATEGORY" value) props))
  10427. (append sum-props (nreverse props)))))))
  10428. (defun org-entry-get (pom property &optional inherit)
  10429. "Get value of PROPERTY for entry at point-or-marker POM.
  10430. If INHERIT is non-nil and the entry does not have the property,
  10431. then also check higher levels of the hierarchy.
  10432. If INHERIT is the symbol `selective', use inheritance only if the setting
  10433. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10434. If the property is present but empty, the return value is the empty string.
  10435. If the property is not present at all, nil is returned."
  10436. (org-with-point-at pom
  10437. (if (and inherit (if (eq inherit 'selective)
  10438. (org-property-inherit-p property)
  10439. t))
  10440. (org-entry-get-with-inheritance property)
  10441. (if (member property org-special-properties)
  10442. ;; We need a special property. Use brute force, get all properties.
  10443. (cdr (assoc property (org-entry-properties nil 'special)))
  10444. (let ((range (org-get-property-block)))
  10445. (if (and range
  10446. (goto-char (car range))
  10447. (re-search-forward
  10448. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10449. (cdr range) t))
  10450. ;; Found the property, return it.
  10451. (if (match-end 1)
  10452. (org-match-string-no-properties 1)
  10453. "")))))))
  10454. (defun org-property-or-variable-value (var &optional inherit)
  10455. "Check if there is a property fixing the value of VAR.
  10456. If yes, return this value. If not, return the current value of the variable."
  10457. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10458. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10459. (read prop)
  10460. (symbol-value var))))
  10461. (defun org-entry-delete (pom property)
  10462. "Delete the property PROPERTY from entry at point-or-marker POM."
  10463. (org-with-point-at pom
  10464. (if (member property org-special-properties)
  10465. nil ; cannot delete these properties.
  10466. (let ((range (org-get-property-block)))
  10467. (if (and range
  10468. (goto-char (car range))
  10469. (re-search-forward
  10470. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10471. (cdr range) t))
  10472. (progn
  10473. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10474. t)
  10475. nil)))))
  10476. ;; Multi-values properties are properties that contain multiple values
  10477. ;; These values are assumed to be single words, separated by whitespace.
  10478. (defun org-entry-add-to-multivalued-property (pom property value)
  10479. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10480. (let* ((old (org-entry-get pom property))
  10481. (values (and old (org-split-string old "[ \t]"))))
  10482. (setq value (org-entry-protect-space value))
  10483. (unless (member value values)
  10484. (setq values (cons value values))
  10485. (org-entry-put pom property
  10486. (mapconcat 'identity values " ")))))
  10487. (defun org-entry-remove-from-multivalued-property (pom property value)
  10488. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10489. (let* ((old (org-entry-get pom property))
  10490. (values (and old (org-split-string old "[ \t]"))))
  10491. (setq value (org-entry-protect-space value))
  10492. (when (member value values)
  10493. (setq values (delete value values))
  10494. (org-entry-put pom property
  10495. (mapconcat 'identity values " ")))))
  10496. (defun org-entry-member-in-multivalued-property (pom property value)
  10497. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10498. (let* ((old (org-entry-get pom property))
  10499. (values (and old (org-split-string old "[ \t]"))))
  10500. (setq value (org-entry-protect-space value))
  10501. (member value values)))
  10502. (defun org-entry-get-multivalued-property (pom property)
  10503. "Return a list of values in a multivalued property."
  10504. (let* ((value (org-entry-get pom property))
  10505. (values (and value (org-split-string value "[ \t]"))))
  10506. (mapcar 'org-entry-restore-space values)))
  10507. (defun org-entry-put-multivalued-property (pom property &rest values)
  10508. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10509. VALUES should be a list of strings. Spaces will be protected."
  10510. (org-entry-put pom property
  10511. (mapconcat 'org-entry-protect-space values " "))
  10512. (let* ((value (org-entry-get pom property))
  10513. (values (and value (org-split-string value "[ \t]"))))
  10514. (mapcar 'org-entry-restore-space values)))
  10515. (defun org-entry-protect-space (s)
  10516. "Protect spaces and newline in string S."
  10517. (while (string-match " " s)
  10518. (setq s (replace-match "%20" t t s)))
  10519. (while (string-match "\n" s)
  10520. (setq s (replace-match "%0A" t t s)))
  10521. s)
  10522. (defun org-entry-restore-space (s)
  10523. "Restore spaces and newline in string S."
  10524. (while (string-match "%20" s)
  10525. (setq s (replace-match " " t t s)))
  10526. (while (string-match "%0A" s)
  10527. (setq s (replace-match "\n" t t s)))
  10528. s)
  10529. (defvar org-entry-property-inherited-from (make-marker)
  10530. "Marker pointing to the entry from where a property was inherited.
  10531. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10532. location of the entry where the inheritance search matched. If there was
  10533. no match, the marker will point nowhere.
  10534. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10535. is set.")
  10536. (defun org-entry-get-with-inheritance (property)
  10537. "Get entry property, and search higher levels if not present."
  10538. (move-marker org-entry-property-inherited-from nil)
  10539. (let (tmp)
  10540. (save-excursion
  10541. (save-restriction
  10542. (widen)
  10543. (catch 'ex
  10544. (while t
  10545. (when (setq tmp (org-entry-get nil property))
  10546. (org-back-to-heading t)
  10547. (move-marker org-entry-property-inherited-from (point))
  10548. (throw 'ex tmp))
  10549. (or (org-up-heading-safe) (throw 'ex nil)))))
  10550. (or tmp
  10551. (cdr (assoc property org-file-properties))
  10552. (cdr (assoc property org-global-properties))
  10553. (cdr (assoc property org-global-properties-fixed))))))
  10554. (defun org-entry-put (pom property value)
  10555. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10556. (org-with-point-at pom
  10557. (org-back-to-heading t)
  10558. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10559. range)
  10560. (cond
  10561. ((equal property "TODO")
  10562. (when (and (stringp value) (string-match "\\S-" value)
  10563. (not (member value org-todo-keywords-1)))
  10564. (error "\"%s\" is not a valid TODO state" value))
  10565. (if (or (not value)
  10566. (not (string-match "\\S-" value)))
  10567. (setq value 'none))
  10568. (org-todo value)
  10569. (org-set-tags nil 'align))
  10570. ((equal property "PRIORITY")
  10571. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10572. (string-to-char value) ?\ ))
  10573. (org-set-tags nil 'align))
  10574. ((equal property "SCHEDULED")
  10575. (if (re-search-forward org-scheduled-time-regexp end t)
  10576. (cond
  10577. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10578. ((eq value 'later) (org-timestamp-change 1 'day))
  10579. (t (call-interactively 'org-schedule)))
  10580. (call-interactively 'org-schedule)))
  10581. ((equal property "DEADLINE")
  10582. (if (re-search-forward org-deadline-time-regexp end t)
  10583. (cond
  10584. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10585. ((eq value 'later) (org-timestamp-change 1 'day))
  10586. (t (call-interactively 'org-deadline)))
  10587. (call-interactively 'org-deadline)))
  10588. ((member property org-special-properties)
  10589. (error "The %s property can not yet be set with `org-entry-put'"
  10590. property))
  10591. (t ; a non-special property
  10592. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10593. (setq range (org-get-property-block beg end 'force))
  10594. (goto-char (car range))
  10595. (if (re-search-forward
  10596. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10597. (progn
  10598. (delete-region (match-beginning 1) (match-end 1))
  10599. (goto-char (match-beginning 1)))
  10600. (goto-char (cdr range))
  10601. (insert "\n")
  10602. (backward-char 1)
  10603. (org-indent-line-function)
  10604. (insert ":" property ":"))
  10605. (and value (insert " " value))
  10606. (org-indent-line-function)))))))
  10607. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10608. "Get all property keys in the current buffer.
  10609. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10610. like tags and TODO state.
  10611. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10612. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10613. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10614. formats in the current buffer."
  10615. (let (rtn range cfmt s p)
  10616. (save-excursion
  10617. (save-restriction
  10618. (widen)
  10619. (goto-char (point-min))
  10620. (while (re-search-forward org-property-start-re nil t)
  10621. (setq range (org-get-property-block))
  10622. (goto-char (car range))
  10623. (while (re-search-forward
  10624. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10625. (cdr range) t)
  10626. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10627. (outline-next-heading))))
  10628. (when include-specials
  10629. (setq rtn (append org-special-properties rtn)))
  10630. (when include-defaults
  10631. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10632. (when include-columns
  10633. (save-excursion
  10634. (save-restriction
  10635. (widen)
  10636. (goto-char (point-min))
  10637. (while (re-search-forward
  10638. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10639. nil t)
  10640. (setq cfmt (match-string 2) s 0)
  10641. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10642. cfmt s)
  10643. (setq s (match-end 0)
  10644. p (match-string 1 cfmt))
  10645. (unless (or (equal p "ITEM")
  10646. (member p org-special-properties))
  10647. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10648. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10649. (defun org-property-values (key)
  10650. "Return a list of all values of property KEY."
  10651. (save-excursion
  10652. (save-restriction
  10653. (widen)
  10654. (goto-char (point-min))
  10655. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10656. values)
  10657. (while (re-search-forward re nil t)
  10658. (add-to-list 'values (org-trim (match-string 1))))
  10659. (delete "" values)))))
  10660. (defun org-insert-property-drawer ()
  10661. "Insert a property drawer into the current entry."
  10662. (interactive)
  10663. (org-back-to-heading t)
  10664. (looking-at outline-regexp)
  10665. (let ((indent (- (match-end 0)(match-beginning 0)))
  10666. (beg (point))
  10667. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10668. end hiddenp)
  10669. (outline-next-heading)
  10670. (setq end (point))
  10671. (goto-char beg)
  10672. (while (re-search-forward re end t))
  10673. (setq hiddenp (org-invisible-p))
  10674. (end-of-line 1)
  10675. (and (equal (char-after) ?\n) (forward-char 1))
  10676. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10677. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10678. ;; just skip this line
  10679. (beginning-of-line 2)
  10680. ;; Drawer start, find the end
  10681. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10682. (beginning-of-line 1)))
  10683. (org-skip-over-state-notes)
  10684. (skip-chars-backward " \t\n\r")
  10685. (if (eq (char-before) ?*) (forward-char 1))
  10686. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10687. (beginning-of-line 0)
  10688. (org-indent-to-column indent)
  10689. (beginning-of-line 2)
  10690. (org-indent-to-column indent)
  10691. (beginning-of-line 0)
  10692. (if hiddenp
  10693. (save-excursion
  10694. (org-back-to-heading t)
  10695. (hide-entry))
  10696. (org-flag-drawer t))))
  10697. (defun org-set-property (property value)
  10698. "In the current entry, set PROPERTY to VALUE.
  10699. When called interactively, this will prompt for a property name, offering
  10700. completion on existing and default properties. And then it will prompt
  10701. for a value, offering completion either on allowed values (via an inherited
  10702. xxx_ALL property) or on existing values in other instances of this property
  10703. in the current file."
  10704. (interactive
  10705. (let* ((completion-ignore-case t)
  10706. (keys (org-buffer-property-keys nil t t))
  10707. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10708. (prop (if (member prop0 keys)
  10709. prop0
  10710. (or (cdr (assoc (downcase prop0)
  10711. (mapcar (lambda (x) (cons (downcase x) x))
  10712. keys)))
  10713. prop0)))
  10714. (cur (org-entry-get nil prop))
  10715. (allowed (org-property-get-allowed-values nil prop 'table))
  10716. (existing (mapcar 'list (org-property-values prop)))
  10717. (val (if allowed
  10718. (org-completing-read "Value: " allowed nil 'req-match)
  10719. (let (org-completion-use-ido)
  10720. (org-completing-read
  10721. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10722. (concat "[" cur "]") "")
  10723. ": ")
  10724. existing nil nil "" nil cur)))))
  10725. (list prop (if (equal val "") cur val))))
  10726. (unless (equal (org-entry-get nil property) value)
  10727. (org-entry-put nil property value)))
  10728. (defun org-delete-property (property)
  10729. "In the current entry, delete PROPERTY."
  10730. (interactive
  10731. (let* ((completion-ignore-case t)
  10732. (prop (org-ido-completing-read
  10733. "Property: " (org-entry-properties nil 'standard))))
  10734. (list prop)))
  10735. (message "Property %s %s" property
  10736. (if (org-entry-delete nil property)
  10737. "deleted"
  10738. "was not present in the entry")))
  10739. (defun org-delete-property-globally (property)
  10740. "Remove PROPERTY globally, from all entries."
  10741. (interactive
  10742. (let* ((completion-ignore-case t)
  10743. (prop (org-ido-completing-read
  10744. "Globally remove property: "
  10745. (mapcar 'list (org-buffer-property-keys)))))
  10746. (list prop)))
  10747. (save-excursion
  10748. (save-restriction
  10749. (widen)
  10750. (goto-char (point-min))
  10751. (let ((cnt 0))
  10752. (while (re-search-forward
  10753. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10754. nil t)
  10755. (setq cnt (1+ cnt))
  10756. (replace-match ""))
  10757. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10758. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10759. (defun org-compute-property-at-point ()
  10760. "Compute the property at point.
  10761. This looks for an enclosing column format, extracts the operator and
  10762. then applies it to the property in the column format's scope."
  10763. (interactive)
  10764. (unless (org-at-property-p)
  10765. (error "Not at a property"))
  10766. (let ((prop (org-match-string-no-properties 2)))
  10767. (org-columns-get-format-and-top-level)
  10768. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10769. (error "No operator defined for property %s" prop))
  10770. (org-columns-compute prop)))
  10771. (defun org-property-get-allowed-values (pom property &optional table)
  10772. "Get allowed values for the property PROPERTY.
  10773. When TABLE is non-nil, return an alist that can directly be used for
  10774. completion."
  10775. (let (vals)
  10776. (cond
  10777. ((equal property "TODO")
  10778. (setq vals (org-with-point-at pom
  10779. (append org-todo-keywords-1 '("")))))
  10780. ((equal property "PRIORITY")
  10781. (let ((n org-lowest-priority))
  10782. (while (>= n org-highest-priority)
  10783. (push (char-to-string n) vals)
  10784. (setq n (1- n)))))
  10785. ((member property org-special-properties))
  10786. (t
  10787. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10788. (when (and vals (string-match "\\S-" vals))
  10789. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10790. (setq vals (mapcar (lambda (x)
  10791. (cond ((stringp x) x)
  10792. ((numberp x) (number-to-string x))
  10793. ((symbolp x) (symbol-name x))
  10794. (t "???")))
  10795. vals)))))
  10796. (if table (mapcar 'list vals) vals)))
  10797. (defun org-property-previous-allowed-value (&optional previous)
  10798. "Switch to the next allowed value for this property."
  10799. (interactive)
  10800. (org-property-next-allowed-value t))
  10801. (defun org-property-next-allowed-value (&optional previous)
  10802. "Switch to the next allowed value for this property."
  10803. (interactive)
  10804. (unless (org-at-property-p)
  10805. (error "Not at a property"))
  10806. (let* ((key (match-string 2))
  10807. (value (match-string 3))
  10808. (allowed (or (org-property-get-allowed-values (point) key)
  10809. (and (member value '("[ ]" "[-]" "[X]"))
  10810. '("[ ]" "[X]"))))
  10811. nval)
  10812. (unless allowed
  10813. (error "Allowed values for this property have not been defined"))
  10814. (if previous (setq allowed (reverse allowed)))
  10815. (if (member value allowed)
  10816. (setq nval (car (cdr (member value allowed)))))
  10817. (setq nval (or nval (car allowed)))
  10818. (if (equal nval value)
  10819. (error "Only one allowed value for this property"))
  10820. (org-at-property-p)
  10821. (replace-match (concat " :" key ": " nval) t t)
  10822. (org-indent-line-function)
  10823. (beginning-of-line 1)
  10824. (skip-chars-forward " \t")))
  10825. (defun org-find-entry-with-id (ident)
  10826. "Locate the entry that contains the ID property with exact value IDENT.
  10827. IDENT can be a string, a symbol or a number, this function will search for
  10828. the string representation of it.
  10829. Return the position where this entry starts, or nil if there is no such entry."
  10830. (interactive "sID: ")
  10831. (let ((id (cond
  10832. ((stringp ident) ident)
  10833. ((symbol-name ident) (symbol-name ident))
  10834. ((numberp ident) (number-to-string ident))
  10835. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10836. (case-fold-search nil))
  10837. (save-excursion
  10838. (save-restriction
  10839. (widen)
  10840. (goto-char (point-min))
  10841. (when (re-search-forward
  10842. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10843. nil t)
  10844. (org-back-to-heading)
  10845. (point))))))
  10846. ;;;; Timestamps
  10847. (defvar org-last-changed-timestamp nil)
  10848. (defvar org-last-inserted-timestamp nil
  10849. "The last time stamp inserted with `org-insert-time-stamp'.")
  10850. (defvar org-time-was-given) ; dynamically scoped parameter
  10851. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10852. (defvar org-ts-what) ; dynamically scoped parameter
  10853. (defun org-time-stamp (arg &optional inactive)
  10854. "Prompt for a date/time and insert a time stamp.
  10855. If the user specifies a time like HH:MM, or if this command is called
  10856. with a prefix argument, the time stamp will contain date and time.
  10857. Otherwise, only the date will be included. All parts of a date not
  10858. specified by the user will be filled in from the current date/time.
  10859. So if you press just return without typing anything, the time stamp
  10860. will represent the current date/time. If there is already a timestamp
  10861. at the cursor, it will be modified."
  10862. (interactive "P")
  10863. (let* ((ts nil)
  10864. (default-time
  10865. ;; Default time is either today, or, when entering a range,
  10866. ;; the range start.
  10867. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10868. (save-excursion
  10869. (re-search-backward
  10870. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10871. (- (point) 20) t)))
  10872. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10873. (current-time)))
  10874. (default-input (and ts (org-get-compact-tod ts)))
  10875. org-time-was-given org-end-time-was-given time)
  10876. (cond
  10877. ((and (org-at-timestamp-p t)
  10878. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10879. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10880. (insert "--")
  10881. (setq time (let ((this-command this-command))
  10882. (org-read-date arg 'totime nil nil
  10883. default-time default-input)))
  10884. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10885. ((org-at-timestamp-p t)
  10886. (setq time (let ((this-command this-command))
  10887. (org-read-date arg 'totime nil nil default-time default-input)))
  10888. (when (org-at-timestamp-p t) ; just to get the match data
  10889. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10890. (replace-match "")
  10891. (setq org-last-changed-timestamp
  10892. (org-insert-time-stamp
  10893. time (or org-time-was-given arg)
  10894. inactive nil nil (list org-end-time-was-given))))
  10895. (message "Timestamp updated"))
  10896. (t
  10897. (setq time (let ((this-command this-command))
  10898. (org-read-date arg 'totime nil nil default-time default-input)))
  10899. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10900. nil nil (list org-end-time-was-given))))))
  10901. ;; FIXME: can we use this for something else, like computing time differences?
  10902. (defun org-get-compact-tod (s)
  10903. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10904. (let* ((t1 (match-string 1 s))
  10905. (h1 (string-to-number (match-string 2 s)))
  10906. (m1 (string-to-number (match-string 3 s)))
  10907. (t2 (and (match-end 4) (match-string 5 s)))
  10908. (h2 (and t2 (string-to-number (match-string 6 s))))
  10909. (m2 (and t2 (string-to-number (match-string 7 s))))
  10910. dh dm)
  10911. (if (not t2)
  10912. t1
  10913. (setq dh (- h2 h1) dm (- m2 m1))
  10914. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10915. (concat t1 "+" (number-to-string dh)
  10916. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10917. (defun org-time-stamp-inactive (&optional arg)
  10918. "Insert an inactive time stamp.
  10919. An inactive time stamp is enclosed in square brackets instead of angle
  10920. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10921. does not link to the calendar and cannot be changed with the S-cursor keys.
  10922. So these are more for recording a certain time/date."
  10923. (interactive "P")
  10924. (org-time-stamp arg 'inactive))
  10925. (defvar org-date-ovl (org-make-overlay 1 1))
  10926. (org-overlay-put org-date-ovl 'face 'org-warning)
  10927. (org-detach-overlay org-date-ovl)
  10928. (defvar org-ans1) ; dynamically scoped parameter
  10929. (defvar org-ans2) ; dynamically scoped parameter
  10930. (defvar org-plain-time-of-day-regexp) ; defined below
  10931. (defvar org-overriding-default-time nil) ; dynamically scoped
  10932. (defvar org-read-date-overlay nil)
  10933. (defvar org-dcst nil) ; dynamically scoped
  10934. (defvar org-read-date-history nil)
  10935. (defun org-read-date (&optional with-time to-time from-string prompt
  10936. default-time default-input)
  10937. "Read a date, possibly a time, and make things smooth for the user.
  10938. The prompt will suggest to enter an ISO date, but you can also enter anything
  10939. which will at least partially be understood by `parse-time-string'.
  10940. Unrecognized parts of the date will default to the current day, month, year,
  10941. hour and minute. If this command is called to replace a timestamp at point,
  10942. of to enter the second timestamp of a range, the default time is taken from the
  10943. existing stamp. For example,
  10944. 3-2-5 --> 2003-02-05
  10945. feb 15 --> currentyear-02-15
  10946. sep 12 9 --> 2009-09-12
  10947. 12:45 --> today 12:45
  10948. 22 sept 0:34 --> currentyear-09-22 0:34
  10949. 12 --> currentyear-currentmonth-12
  10950. Fri --> nearest Friday (today or later)
  10951. etc.
  10952. Furthermore you can specify a relative date by giving, as the *first* thing
  10953. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10954. change in days weeks, months, years.
  10955. With a single plus or minus, the date is relative to today. With a double
  10956. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10957. +4d --> four days from today
  10958. +4 --> same as above
  10959. +2w --> two weeks from today
  10960. ++5 --> five days from default date
  10961. The function understands only English month and weekday abbreviations,
  10962. but this can be configured with the variables `parse-time-months' and
  10963. `parse-time-weekdays'.
  10964. While prompting, a calendar is popped up - you can also select the
  10965. date with the mouse (button 1). The calendar shows a period of three
  10966. months. To scroll it to other months, use the keys `>' and `<'.
  10967. If you don't like the calendar, turn it off with
  10968. \(setq org-read-date-popup-calendar nil)
  10969. With optional argument TO-TIME, the date will immediately be converted
  10970. to an internal time.
  10971. With an optional argument WITH-TIME, the prompt will suggest to also
  10972. insert a time. Note that when WITH-TIME is not set, you can still
  10973. enter a time, and this function will inform the calling routine about
  10974. this change. The calling routine may then choose to change the format
  10975. used to insert the time stamp into the buffer to include the time.
  10976. With optional argument FROM-STRING, read from this string instead from
  10977. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10978. the time/date that is used for everything that is not specified by the
  10979. user."
  10980. (require 'parse-time)
  10981. (let* ((org-time-stamp-rounding-minutes
  10982. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10983. (org-dcst org-display-custom-times)
  10984. (ct (org-current-time))
  10985. (def (or org-overriding-default-time default-time ct))
  10986. (defdecode (decode-time def))
  10987. (dummy (progn
  10988. (when (< (nth 2 defdecode) org-extend-today-until)
  10989. (setcar (nthcdr 2 defdecode) -1)
  10990. (setcar (nthcdr 1 defdecode) 59)
  10991. (setq def (apply 'encode-time defdecode)
  10992. defdecode (decode-time def)))))
  10993. (calendar-move-hook nil)
  10994. (calendar-view-diary-initially-flag nil)
  10995. (view-diary-entries-initially nil)
  10996. (calendar-view-holidays-initially-flag nil)
  10997. (view-calendar-holidays-initially nil)
  10998. (timestr (format-time-string
  10999. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11000. (prompt (concat (if prompt (concat prompt " ") "")
  11001. (format "Date+time [%s]: " timestr)))
  11002. ans (org-ans0 "") org-ans1 org-ans2 final)
  11003. (cond
  11004. (from-string (setq ans from-string))
  11005. (org-read-date-popup-calendar
  11006. (save-excursion
  11007. (save-window-excursion
  11008. (calendar)
  11009. (calendar-forward-day (- (time-to-days def)
  11010. (calendar-absolute-from-gregorian
  11011. (calendar-current-date))))
  11012. (org-eval-in-calendar nil t)
  11013. (let* ((old-map (current-local-map))
  11014. (map (copy-keymap calendar-mode-map))
  11015. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11016. (org-defkey map (kbd "RET") 'org-calendar-select)
  11017. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11018. 'org-calendar-select-mouse)
  11019. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11020. 'org-calendar-select-mouse)
  11021. (org-defkey minibuffer-local-map [(meta shift left)]
  11022. (lambda () (interactive)
  11023. (org-eval-in-calendar '(calendar-backward-month 1))))
  11024. (org-defkey minibuffer-local-map [(meta shift right)]
  11025. (lambda () (interactive)
  11026. (org-eval-in-calendar '(calendar-forward-month 1))))
  11027. (org-defkey minibuffer-local-map [(meta shift up)]
  11028. (lambda () (interactive)
  11029. (org-eval-in-calendar '(calendar-backward-year 1))))
  11030. (org-defkey minibuffer-local-map [(meta shift down)]
  11031. (lambda () (interactive)
  11032. (org-eval-in-calendar '(calendar-forward-year 1))))
  11033. (org-defkey minibuffer-local-map [(shift up)]
  11034. (lambda () (interactive)
  11035. (org-eval-in-calendar '(calendar-backward-week 1))))
  11036. (org-defkey minibuffer-local-map [(shift down)]
  11037. (lambda () (interactive)
  11038. (org-eval-in-calendar '(calendar-forward-week 1))))
  11039. (org-defkey minibuffer-local-map [(shift left)]
  11040. (lambda () (interactive)
  11041. (org-eval-in-calendar '(calendar-backward-day 1))))
  11042. (org-defkey minibuffer-local-map [(shift right)]
  11043. (lambda () (interactive)
  11044. (org-eval-in-calendar '(calendar-forward-day 1))))
  11045. (org-defkey minibuffer-local-map ">"
  11046. (lambda () (interactive)
  11047. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11048. (org-defkey minibuffer-local-map "<"
  11049. (lambda () (interactive)
  11050. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11051. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11052. (unwind-protect
  11053. (progn
  11054. (use-local-map map)
  11055. (add-hook 'post-command-hook 'org-read-date-display)
  11056. (setq org-ans0 (read-string prompt default-input
  11057. 'org-read-date-history nil))
  11058. ;; org-ans0: from prompt
  11059. ;; org-ans1: from mouse click
  11060. ;; org-ans2: from calendar motion
  11061. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11062. (remove-hook 'post-command-hook 'org-read-date-display)
  11063. (use-local-map old-map)
  11064. (when org-read-date-overlay
  11065. (org-delete-overlay org-read-date-overlay)
  11066. (setq org-read-date-overlay nil)))))))
  11067. (t ; Naked prompt only
  11068. (unwind-protect
  11069. (setq ans (read-string prompt default-input
  11070. 'org-read-date-history timestr))
  11071. (when org-read-date-overlay
  11072. (org-delete-overlay org-read-date-overlay)
  11073. (setq org-read-date-overlay nil)))))
  11074. (setq final (org-read-date-analyze ans def defdecode))
  11075. (if to-time
  11076. (apply 'encode-time final)
  11077. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11078. (format "%04d-%02d-%02d %02d:%02d"
  11079. (nth 5 final) (nth 4 final) (nth 3 final)
  11080. (nth 2 final) (nth 1 final))
  11081. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11082. (defvar def)
  11083. (defvar defdecode)
  11084. (defvar with-time)
  11085. (defun org-read-date-display ()
  11086. "Display the current date prompt interpretation in the minibuffer."
  11087. (when org-read-date-display-live
  11088. (when org-read-date-overlay
  11089. (org-delete-overlay org-read-date-overlay))
  11090. (let ((p (point)))
  11091. (end-of-line 1)
  11092. (while (not (equal (buffer-substring
  11093. (max (point-min) (- (point) 4)) (point))
  11094. " "))
  11095. (insert " "))
  11096. (goto-char p))
  11097. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11098. " " (or org-ans1 org-ans2)))
  11099. (org-end-time-was-given nil)
  11100. (f (org-read-date-analyze ans def defdecode))
  11101. (fmts (if org-dcst
  11102. org-time-stamp-custom-formats
  11103. org-time-stamp-formats))
  11104. (fmt (if (or with-time
  11105. (and (boundp 'org-time-was-given) org-time-was-given))
  11106. (cdr fmts)
  11107. (car fmts)))
  11108. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11109. (when (and org-end-time-was-given
  11110. (string-match org-plain-time-of-day-regexp txt))
  11111. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11112. org-end-time-was-given
  11113. (substring txt (match-end 0)))))
  11114. (setq org-read-date-overlay
  11115. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11116. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11117. (defun org-read-date-analyze (ans def defdecode)
  11118. "Analyse the combined answer of the date prompt."
  11119. ;; FIXME: cleanup and comment
  11120. (let (delta deltan deltaw deltadef year month day
  11121. hour minute second wday pm h2 m2 tl wday1
  11122. iso-year iso-weekday iso-week iso-year iso-date)
  11123. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11124. (setq ans "+0"))
  11125. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11126. (setq ans (replace-match "" t t ans)
  11127. deltan (car delta)
  11128. deltaw (nth 1 delta)
  11129. deltadef (nth 2 delta)))
  11130. ;; Check if there is an iso week date in there
  11131. ;; If yes, sore the info and postpone interpreting it until the rest
  11132. ;; of the parsing is done
  11133. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11134. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11135. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11136. iso-week (string-to-number (match-string 2 ans)))
  11137. (setq ans (replace-match "" t t ans)))
  11138. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11139. (when (string-match
  11140. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11141. (setq year (if (match-end 2)
  11142. (string-to-number (match-string 2 ans))
  11143. (string-to-number (format-time-string "%Y")))
  11144. month (string-to-number (match-string 3 ans))
  11145. day (string-to-number (match-string 4 ans)))
  11146. (if (< year 100) (setq year (+ 2000 year)))
  11147. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11148. t nil ans)))
  11149. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11150. ;; If there is a time with am/pm, and *no* time without it, we convert
  11151. ;; so that matching will be successful.
  11152. (loop for i from 1 to 2 do ; twice, for end time as well
  11153. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11154. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11155. (setq hour (string-to-number (match-string 1 ans))
  11156. minute (if (match-end 3)
  11157. (string-to-number (match-string 3 ans))
  11158. 0)
  11159. pm (equal ?p
  11160. (string-to-char (downcase (match-string 4 ans)))))
  11161. (if (and (= hour 12) (not pm))
  11162. (setq hour 0)
  11163. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11164. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11165. t t ans))))
  11166. ;; Check if a time range is given as a duration
  11167. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11168. (setq hour (string-to-number (match-string 1 ans))
  11169. h2 (+ hour (string-to-number (match-string 3 ans)))
  11170. minute (string-to-number (match-string 2 ans))
  11171. m2 (+ minute (if (match-end 5) (string-to-number
  11172. (match-string 5 ans))0)))
  11173. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11174. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11175. t t ans)))
  11176. ;; Check if there is a time range
  11177. (when (boundp 'org-end-time-was-given)
  11178. (setq org-time-was-given nil)
  11179. (when (and (string-match org-plain-time-of-day-regexp ans)
  11180. (match-end 8))
  11181. (setq org-end-time-was-given (match-string 8 ans))
  11182. (setq ans (concat (substring ans 0 (match-beginning 7))
  11183. (substring ans (match-end 7))))))
  11184. (setq tl (parse-time-string ans)
  11185. day (or (nth 3 tl) (nth 3 defdecode))
  11186. month (or (nth 4 tl)
  11187. (if (and org-read-date-prefer-future
  11188. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11189. (1+ (nth 4 defdecode))
  11190. (nth 4 defdecode)))
  11191. year (or (nth 5 tl)
  11192. (if (and org-read-date-prefer-future
  11193. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11194. (1+ (nth 5 defdecode))
  11195. (nth 5 defdecode)))
  11196. hour (or (nth 2 tl) (nth 2 defdecode))
  11197. minute (or (nth 1 tl) (nth 1 defdecode))
  11198. second (or (nth 0 tl) 0)
  11199. wday (nth 6 tl))
  11200. ;; Special date definitions below
  11201. (cond
  11202. (iso-week
  11203. ;; There was an iso week
  11204. (setq year (or iso-year year)
  11205. day (or iso-weekday wday 1)
  11206. wday nil ; to make sure that the trigger below does not match
  11207. iso-date (calendar-gregorian-from-absolute
  11208. (calendar-absolute-from-iso
  11209. (list iso-week day year))))
  11210. ; FIXME: Should we also push ISO weeks into the future?
  11211. ; (when (and org-read-date-prefer-future
  11212. ; (not iso-year)
  11213. ; (< (calendar-absolute-from-gregorian iso-date)
  11214. ; (time-to-days (current-time))))
  11215. ; (setq year (1+ year)
  11216. ; iso-date (calendar-gregorian-from-absolute
  11217. ; (calendar-absolute-from-iso
  11218. ; (list iso-week day year)))))
  11219. (setq month (car iso-date)
  11220. year (nth 2 iso-date)
  11221. day (nth 1 iso-date)))
  11222. (deltan
  11223. (unless deltadef
  11224. (let ((now (decode-time (current-time))))
  11225. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11226. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11227. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11228. ((equal deltaw "m") (setq month (+ month deltan)))
  11229. ((equal deltaw "y") (setq year (+ year deltan)))))
  11230. ((and wday (not (nth 3 tl)))
  11231. ;; Weekday was given, but no day, so pick that day in the week
  11232. ;; on or after the derived date.
  11233. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11234. (unless (equal wday wday1)
  11235. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11236. (if (and (boundp 'org-time-was-given)
  11237. (nth 2 tl))
  11238. (setq org-time-was-given t))
  11239. (if (< year 100) (setq year (+ 2000 year)))
  11240. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11241. (list second minute hour day month year)))
  11242. (defvar parse-time-weekdays)
  11243. (defun org-read-date-get-relative (s today default)
  11244. "Check string S for special relative date string.
  11245. TODAY and DEFAULT are internal times, for today and for a default.
  11246. Return shift list (N what def-flag)
  11247. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11248. N is the number of WHATs to shift.
  11249. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11250. the DEFAULT date rather than TODAY."
  11251. (when (and
  11252. (string-match
  11253. (concat
  11254. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11255. "\\([0-9]+\\)?"
  11256. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11257. "\\([ \t]\\|$\\)") s)
  11258. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11259. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11260. (string-to-char (substring (match-string 1 s) -1))
  11261. ?+))
  11262. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11263. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11264. (what (if (match-end 3) (match-string 3 s) "d"))
  11265. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11266. (date (if rel default today))
  11267. (wday (nth 6 (decode-time date)))
  11268. delta)
  11269. (if wday1
  11270. (progn
  11271. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11272. (if (= dir ?-) (setq delta (- delta 7)))
  11273. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11274. (list delta "d" rel))
  11275. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11276. (defun org-eval-in-calendar (form &optional keepdate)
  11277. "Eval FORM in the calendar window and return to current window.
  11278. Also, store the cursor date in variable org-ans2."
  11279. (let ((sw (selected-window)))
  11280. (select-window (get-buffer-window "*Calendar*"))
  11281. (eval form)
  11282. (when (and (not keepdate) (calendar-cursor-to-date))
  11283. (let* ((date (calendar-cursor-to-date))
  11284. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11285. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11286. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11287. (select-window sw)))
  11288. (defun org-calendar-select ()
  11289. "Return to `org-read-date' with the date currently selected.
  11290. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11291. (interactive)
  11292. (when (calendar-cursor-to-date)
  11293. (let* ((date (calendar-cursor-to-date))
  11294. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11295. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11296. (if (active-minibuffer-window) (exit-minibuffer))))
  11297. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11298. "Insert a date stamp for the date given by the internal TIME.
  11299. WITH-HM means, use the stamp format that includes the time of the day.
  11300. INACTIVE means use square brackets instead of angular ones, so that the
  11301. stamp will not contribute to the agenda.
  11302. PRE and POST are optional strings to be inserted before and after the
  11303. stamp.
  11304. The command returns the inserted time stamp."
  11305. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11306. stamp)
  11307. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11308. (insert-before-markers (or pre ""))
  11309. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11310. (when (listp extra)
  11311. (setq extra (car extra))
  11312. (if (and (stringp extra)
  11313. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11314. (setq extra (format "-%02d:%02d"
  11315. (string-to-number (match-string 1 extra))
  11316. (string-to-number (match-string 2 extra))))
  11317. (setq extra nil)))
  11318. (when extra
  11319. (backward-char 1)
  11320. (insert-before-markers extra)
  11321. (forward-char 1))
  11322. (insert-before-markers (or post ""))
  11323. (setq org-last-inserted-timestamp stamp)))
  11324. (defun org-toggle-time-stamp-overlays ()
  11325. "Toggle the use of custom time stamp formats."
  11326. (interactive)
  11327. (setq org-display-custom-times (not org-display-custom-times))
  11328. (unless org-display-custom-times
  11329. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11330. (while (setq p (next-single-property-change p 'display))
  11331. (if (and (get-text-property p 'display)
  11332. (eq (get-text-property p 'face) 'org-date))
  11333. (remove-text-properties
  11334. p (setq p (next-single-property-change p 'display))
  11335. '(display t))))
  11336. (set-buffer-modified-p bmp)))
  11337. (if (featurep 'xemacs)
  11338. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11339. (org-restart-font-lock)
  11340. (setq org-table-may-need-update t)
  11341. (if org-display-custom-times
  11342. (message "Time stamps are overlayed with custom format")
  11343. (message "Time stamp overlays removed")))
  11344. (defun org-display-custom-time (beg end)
  11345. "Overlay modified time stamp format over timestamp between BEG and END."
  11346. (let* ((ts (buffer-substring beg end))
  11347. t1 w1 with-hm tf time str w2 (off 0))
  11348. (save-match-data
  11349. (setq t1 (org-parse-time-string ts t))
  11350. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11351. (setq off (- (match-end 0) (match-beginning 0)))))
  11352. (setq end (- end off))
  11353. (setq w1 (- end beg)
  11354. with-hm (and (nth 1 t1) (nth 2 t1))
  11355. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11356. time (org-fix-decoded-time t1)
  11357. str (org-add-props
  11358. (format-time-string
  11359. (substring tf 1 -1) (apply 'encode-time time))
  11360. nil 'mouse-face 'highlight)
  11361. w2 (length str))
  11362. (if (not (= w2 w1))
  11363. (add-text-properties (1+ beg) (+ 2 beg)
  11364. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11365. (if (featurep 'xemacs)
  11366. (progn
  11367. (put-text-property beg end 'invisible t)
  11368. (put-text-property beg end 'end-glyph (make-glyph str)))
  11369. (put-text-property beg end 'display str))))
  11370. (defun org-translate-time (string)
  11371. "Translate all timestamps in STRING to custom format.
  11372. But do this only if the variable `org-display-custom-times' is set."
  11373. (when org-display-custom-times
  11374. (save-match-data
  11375. (let* ((start 0)
  11376. (re org-ts-regexp-both)
  11377. t1 with-hm inactive tf time str beg end)
  11378. (while (setq start (string-match re string start))
  11379. (setq beg (match-beginning 0)
  11380. end (match-end 0)
  11381. t1 (save-match-data
  11382. (org-parse-time-string (substring string beg end) t))
  11383. with-hm (and (nth 1 t1) (nth 2 t1))
  11384. inactive (equal (substring string beg (1+ beg)) "[")
  11385. tf (funcall (if with-hm 'cdr 'car)
  11386. org-time-stamp-custom-formats)
  11387. time (org-fix-decoded-time t1)
  11388. str (format-time-string
  11389. (concat
  11390. (if inactive "[" "<") (substring tf 1 -1)
  11391. (if inactive "]" ">"))
  11392. (apply 'encode-time time))
  11393. string (replace-match str t t string)
  11394. start (+ start (length str)))))))
  11395. string)
  11396. (defun org-fix-decoded-time (time)
  11397. "Set 0 instead of nil for the first 6 elements of time.
  11398. Don't touch the rest."
  11399. (let ((n 0))
  11400. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11401. (defun org-days-to-time (timestamp-string)
  11402. "Difference between TIMESTAMP-STRING and now in days."
  11403. (- (time-to-days (org-time-string-to-time timestamp-string))
  11404. (time-to-days (current-time))))
  11405. (defun org-deadline-close (timestamp-string &optional ndays)
  11406. "Is the time in TIMESTAMP-STRING close to the current date?"
  11407. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11408. (and (< (org-days-to-time timestamp-string) ndays)
  11409. (not (org-entry-is-done-p))))
  11410. (defun org-get-wdays (ts)
  11411. "Get the deadline lead time appropriate for timestring TS."
  11412. (cond
  11413. ((<= org-deadline-warning-days 0)
  11414. ;; 0 or negative, enforce this value no matter what
  11415. (- org-deadline-warning-days))
  11416. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11417. ;; lead time is specified.
  11418. (floor (* (string-to-number (match-string 1 ts))
  11419. (cdr (assoc (match-string 2 ts)
  11420. '(("d" . 1) ("w" . 7)
  11421. ("m" . 30.4) ("y" . 365.25)))))))
  11422. ;; go for the default.
  11423. (t org-deadline-warning-days)))
  11424. (defun org-calendar-select-mouse (ev)
  11425. "Return to `org-read-date' with the date currently selected.
  11426. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11427. (interactive "e")
  11428. (mouse-set-point ev)
  11429. (when (calendar-cursor-to-date)
  11430. (let* ((date (calendar-cursor-to-date))
  11431. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11432. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11433. (if (active-minibuffer-window) (exit-minibuffer))))
  11434. (defun org-check-deadlines (ndays)
  11435. "Check if there are any deadlines due or past due.
  11436. A deadline is considered due if it happens within `org-deadline-warning-days'
  11437. days from today's date. If the deadline appears in an entry marked DONE,
  11438. it is not shown. The prefix arg NDAYS can be used to test that many
  11439. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11440. (interactive "P")
  11441. (let* ((org-warn-days
  11442. (cond
  11443. ((equal ndays '(4)) 100000)
  11444. (ndays (prefix-numeric-value ndays))
  11445. (t (abs org-deadline-warning-days))))
  11446. (case-fold-search nil)
  11447. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11448. (callback
  11449. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11450. (message "%d deadlines past-due or due within %d days"
  11451. (org-occur regexp nil callback)
  11452. org-warn-days)))
  11453. (defun org-check-before-date (date)
  11454. "Check if there are deadlines or scheduled entries before DATE."
  11455. (interactive (list (org-read-date)))
  11456. (let ((case-fold-search nil)
  11457. (regexp (concat "\\<\\(" org-deadline-string
  11458. "\\|" org-scheduled-string
  11459. "\\) *<\\([^>]+\\)>"))
  11460. (callback
  11461. (lambda () (time-less-p
  11462. (org-time-string-to-time (match-string 2))
  11463. (org-time-string-to-time date)))))
  11464. (message "%d entries before %s"
  11465. (org-occur regexp nil callback) date)))
  11466. (defun org-check-after-date (date)
  11467. "Check if there are deadlines or scheduled entries after DATE."
  11468. (interactive (list (org-read-date)))
  11469. (let ((case-fold-search nil)
  11470. (regexp (concat "\\<\\(" org-deadline-string
  11471. "\\|" org-scheduled-string
  11472. "\\) *<\\([^>]+\\)>"))
  11473. (callback
  11474. (lambda () (not
  11475. (time-less-p
  11476. (org-time-string-to-time (match-string 2))
  11477. (org-time-string-to-time date))))))
  11478. (message "%d entries after %s"
  11479. (org-occur regexp nil callback) date)))
  11480. (defun org-evaluate-time-range (&optional to-buffer)
  11481. "Evaluate a time range by computing the difference between start and end.
  11482. Normally the result is just printed in the echo area, but with prefix arg
  11483. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11484. If the time range is actually in a table, the result is inserted into the
  11485. next column.
  11486. For time difference computation, a year is assumed to be exactly 365
  11487. days in order to avoid rounding problems."
  11488. (interactive "P")
  11489. (or
  11490. (org-clock-update-time-maybe)
  11491. (save-excursion
  11492. (unless (org-at-date-range-p t)
  11493. (goto-char (point-at-bol))
  11494. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11495. (if (not (org-at-date-range-p t))
  11496. (error "Not at a time-stamp range, and none found in current line")))
  11497. (let* ((ts1 (match-string 1))
  11498. (ts2 (match-string 2))
  11499. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11500. (match-end (match-end 0))
  11501. (time1 (org-time-string-to-time ts1))
  11502. (time2 (org-time-string-to-time ts2))
  11503. (t1 (time-to-seconds time1))
  11504. (t2 (time-to-seconds time2))
  11505. (diff (abs (- t2 t1)))
  11506. (negative (< (- t2 t1) 0))
  11507. ;; (ys (floor (* 365 24 60 60)))
  11508. (ds (* 24 60 60))
  11509. (hs (* 60 60))
  11510. (fy "%dy %dd %02d:%02d")
  11511. (fy1 "%dy %dd")
  11512. (fd "%dd %02d:%02d")
  11513. (fd1 "%dd")
  11514. (fh "%02d:%02d")
  11515. y d h m align)
  11516. (if havetime
  11517. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11518. y 0
  11519. d (floor (/ diff ds)) diff (mod diff ds)
  11520. h (floor (/ diff hs)) diff (mod diff hs)
  11521. m (floor (/ diff 60)))
  11522. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11523. y 0
  11524. d (floor (+ (/ diff ds) 0.5))
  11525. h 0 m 0))
  11526. (if (not to-buffer)
  11527. (message "%s" (org-make-tdiff-string y d h m))
  11528. (if (org-at-table-p)
  11529. (progn
  11530. (goto-char match-end)
  11531. (setq align t)
  11532. (and (looking-at " *|") (goto-char (match-end 0))))
  11533. (goto-char match-end))
  11534. (if (looking-at
  11535. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11536. (replace-match ""))
  11537. (if negative (insert " -"))
  11538. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11539. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11540. (insert " " (format fh h m))))
  11541. (if align (org-table-align))
  11542. (message "Time difference inserted")))))
  11543. (defun org-make-tdiff-string (y d h m)
  11544. (let ((fmt "")
  11545. (l nil))
  11546. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11547. l (push y l)))
  11548. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11549. l (push d l)))
  11550. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11551. l (push h l)))
  11552. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11553. l (push m l)))
  11554. (apply 'format fmt (nreverse l))))
  11555. (defun org-time-string-to-time (s)
  11556. (apply 'encode-time (org-parse-time-string s)))
  11557. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11558. "Convert a time stamp to an absolute day number.
  11559. If there is a specifyer for a cyclic time stamp, get the closest date to
  11560. DAYNR.
  11561. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11562. (cond
  11563. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11564. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11565. daynr
  11566. (+ daynr 1000)))
  11567. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11568. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11569. (time-to-days (current-time))) (match-string 0 s)
  11570. prefer show-all))
  11571. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11572. (defun org-days-to-iso-week (days)
  11573. "Return the iso week number."
  11574. (require 'cal-iso)
  11575. (car (calendar-iso-from-absolute days)))
  11576. (defun org-small-year-to-year (year)
  11577. "Convert 2-digit years into 4-digit years.
  11578. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11579. The year 2000 cannot be abbreviated. Any year larger than 99
  11580. is returned unchanged."
  11581. (if (< year 38)
  11582. (setq year (+ 2000 year))
  11583. (if (< year 100)
  11584. (setq year (+ 1900 year))))
  11585. year)
  11586. (defun org-time-from-absolute (d)
  11587. "Return the time corresponding to date D.
  11588. D may be an absolute day number, or a calendar-type list (month day year)."
  11589. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11590. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11591. (defun org-calendar-holiday ()
  11592. "List of holidays, for Diary display in Org-mode."
  11593. (require 'holidays)
  11594. (let ((hl (funcall
  11595. (if (fboundp 'calendar-check-holidays)
  11596. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11597. (if hl (mapconcat 'identity hl "; "))))
  11598. (defun org-diary-sexp-entry (sexp entry date)
  11599. "Process a SEXP diary ENTRY for DATE."
  11600. (require 'diary-lib)
  11601. (let ((result (if calendar-debug-sexp
  11602. (let ((stack-trace-on-error t))
  11603. (eval (car (read-from-string sexp))))
  11604. (condition-case nil
  11605. (eval (car (read-from-string sexp)))
  11606. (error
  11607. (beep)
  11608. (message "Bad sexp at line %d in %s: %s"
  11609. (org-current-line)
  11610. (buffer-file-name) sexp)
  11611. (sleep-for 2))))))
  11612. (cond ((stringp result) result)
  11613. ((and (consp result)
  11614. (stringp (cdr result))) (cdr result))
  11615. (result entry)
  11616. (t nil))))
  11617. (defun org-diary-to-ical-string (frombuf)
  11618. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11619. This uses the icalendar.el library."
  11620. (let* ((tmpdir (if (featurep 'xemacs)
  11621. (temp-directory)
  11622. temporary-file-directory))
  11623. (tmpfile (make-temp-name
  11624. (expand-file-name "orgics" tmpdir)))
  11625. buf rtn b e)
  11626. (save-excursion
  11627. (set-buffer frombuf)
  11628. (icalendar-export-region (point-min) (point-max) tmpfile)
  11629. (setq buf (find-buffer-visiting tmpfile))
  11630. (set-buffer buf)
  11631. (goto-char (point-min))
  11632. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11633. (setq b (match-beginning 0)))
  11634. (goto-char (point-max))
  11635. (if (re-search-backward "^END:VEVENT" nil t)
  11636. (setq e (match-end 0)))
  11637. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11638. (kill-buffer buf)
  11639. (delete-file tmpfile)
  11640. rtn))
  11641. (defun org-closest-date (start current change prefer show-all)
  11642. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11643. When PREFER is `past' return a date that is either CURRENT or past.
  11644. When PREFER is `future', return a date that is either CURRENT or future.
  11645. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11646. ;; Make the proper lists from the dates
  11647. (catch 'exit
  11648. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11649. dn dw sday cday n1 n2 n0
  11650. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11651. (setq start (org-date-to-gregorian start)
  11652. current (org-date-to-gregorian
  11653. (if show-all
  11654. current
  11655. (time-to-days (current-time))))
  11656. sday (calendar-absolute-from-gregorian start)
  11657. cday (calendar-absolute-from-gregorian current))
  11658. (if (<= cday sday) (throw 'exit sday))
  11659. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11660. (setq dn (string-to-number (match-string 1 change))
  11661. dw (cdr (assoc (match-string 2 change) a1)))
  11662. (error "Invalid change specifyer: %s" change))
  11663. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11664. (cond
  11665. ((eq dw 'day)
  11666. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11667. n2 (+ n1 dn)))
  11668. ((eq dw 'year)
  11669. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11670. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11671. (setq date1 (list m d y1)
  11672. n1 (calendar-absolute-from-gregorian date1)
  11673. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11674. n2 (calendar-absolute-from-gregorian date2)))
  11675. ((eq dw 'month)
  11676. ;; approx number of month between the two dates
  11677. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11678. ;; How often does dn fit in there?
  11679. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11680. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11681. m (+ m nm)
  11682. ny (floor (/ m 12))
  11683. y (+ y ny)
  11684. m (- m (* ny 12)))
  11685. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11686. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11687. (setq m2 (+ m dn) y2 y)
  11688. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11689. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11690. (while (<= n2 cday)
  11691. (setq n1 n2 m m2 y y2)
  11692. (setq m2 (+ m dn) y2 y)
  11693. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11694. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11695. ;; Make sure n1 is the earlier date
  11696. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11697. (if show-all
  11698. (cond
  11699. ((eq prefer 'past) n1)
  11700. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11701. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11702. (cond
  11703. ((eq prefer 'past) n1)
  11704. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11705. (t (if (= cday n1) n1 n2)))))))
  11706. (defun org-date-to-gregorian (date)
  11707. "Turn any specification of DATE into a gregorian date for the calendar."
  11708. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11709. ((and (listp date) (= (length date) 3)) date)
  11710. ((stringp date)
  11711. (setq date (org-parse-time-string date))
  11712. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11713. ((listp date)
  11714. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11715. (defun org-parse-time-string (s &optional nodefault)
  11716. "Parse the standard Org-mode time string.
  11717. This should be a lot faster than the normal `parse-time-string'.
  11718. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11719. hour and minute fields will be nil if not given."
  11720. (if (string-match org-ts-regexp0 s)
  11721. (list 0
  11722. (if (or (match-beginning 8) (not nodefault))
  11723. (string-to-number (or (match-string 8 s) "0")))
  11724. (if (or (match-beginning 7) (not nodefault))
  11725. (string-to-number (or (match-string 7 s) "0")))
  11726. (string-to-number (match-string 4 s))
  11727. (string-to-number (match-string 3 s))
  11728. (string-to-number (match-string 2 s))
  11729. nil nil nil)
  11730. (make-list 9 0)))
  11731. (defun org-timestamp-up (&optional arg)
  11732. "Increase the date item at the cursor by one.
  11733. If the cursor is on the year, change the year. If it is on the month or
  11734. the day, change that.
  11735. With prefix ARG, change by that many units."
  11736. (interactive "p")
  11737. (org-timestamp-change (prefix-numeric-value arg)))
  11738. (defun org-timestamp-down (&optional arg)
  11739. "Decrease the date item at the cursor by one.
  11740. If the cursor is on the year, change the year. If it is on the month or
  11741. the day, change that.
  11742. With prefix ARG, change by that many units."
  11743. (interactive "p")
  11744. (org-timestamp-change (- (prefix-numeric-value arg))))
  11745. (defun org-timestamp-up-day (&optional arg)
  11746. "Increase the date in the time stamp by one day.
  11747. With prefix ARG, change that many days."
  11748. (interactive "p")
  11749. (if (and (not (org-at-timestamp-p t))
  11750. (org-on-heading-p))
  11751. (org-todo 'up)
  11752. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11753. (defun org-timestamp-down-day (&optional arg)
  11754. "Decrease the date in the time stamp by one day.
  11755. With prefix ARG, change that many days."
  11756. (interactive "p")
  11757. (if (and (not (org-at-timestamp-p t))
  11758. (org-on-heading-p))
  11759. (org-todo 'down)
  11760. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11761. (defun org-at-timestamp-p (&optional inactive-ok)
  11762. "Determine if the cursor is in or at a timestamp."
  11763. (interactive)
  11764. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11765. (pos (point))
  11766. (ans (or (looking-at tsr)
  11767. (save-excursion
  11768. (skip-chars-backward "^[<\n\r\t")
  11769. (if (> (point) (point-min)) (backward-char 1))
  11770. (and (looking-at tsr)
  11771. (> (- (match-end 0) pos) -1))))))
  11772. (and ans
  11773. (boundp 'org-ts-what)
  11774. (setq org-ts-what
  11775. (cond
  11776. ((= pos (match-beginning 0)) 'bracket)
  11777. ((= pos (1- (match-end 0))) 'bracket)
  11778. ((org-pos-in-match-range pos 2) 'year)
  11779. ((org-pos-in-match-range pos 3) 'month)
  11780. ((org-pos-in-match-range pos 7) 'hour)
  11781. ((org-pos-in-match-range pos 8) 'minute)
  11782. ((or (org-pos-in-match-range pos 4)
  11783. (org-pos-in-match-range pos 5)) 'day)
  11784. ((and (> pos (or (match-end 8) (match-end 5)))
  11785. (< pos (match-end 0)))
  11786. (- pos (or (match-end 8) (match-end 5))))
  11787. (t 'day))))
  11788. ans))
  11789. (defun org-toggle-timestamp-type ()
  11790. "Toggle the type (<active> or [inactive]) of a time stamp."
  11791. (interactive)
  11792. (when (org-at-timestamp-p t)
  11793. (let ((beg (match-beginning 0)) (end (match-end 0))
  11794. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11795. (save-excursion
  11796. (goto-char beg)
  11797. (while (re-search-forward "[][<>]" end t)
  11798. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11799. t t)))
  11800. (message "Timestamp is now %sactive"
  11801. (if (equal (char-after beg) ?<) "" "in")))))
  11802. (defun org-timestamp-change (n &optional what)
  11803. "Change the date in the time stamp at point.
  11804. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11805. `year', `minute', `second'. If WHAT is not given, the cursor position
  11806. in the timestamp determines what will be changed."
  11807. (let ((pos (point))
  11808. with-hm inactive
  11809. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11810. org-ts-what
  11811. extra rem
  11812. ts time time0)
  11813. (if (not (org-at-timestamp-p t))
  11814. (error "Not at a timestamp"))
  11815. (if (and (not what) (eq org-ts-what 'bracket))
  11816. (org-toggle-timestamp-type)
  11817. (if (and (not what) (not (eq org-ts-what 'day))
  11818. org-display-custom-times
  11819. (get-text-property (point) 'display)
  11820. (not (get-text-property (1- (point)) 'display)))
  11821. (setq org-ts-what 'day))
  11822. (setq org-ts-what (or what org-ts-what)
  11823. inactive (= (char-after (match-beginning 0)) ?\[)
  11824. ts (match-string 0))
  11825. (replace-match "")
  11826. (if (string-match
  11827. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11828. ts)
  11829. (setq extra (match-string 1 ts)))
  11830. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11831. (setq with-hm t))
  11832. (setq time0 (org-parse-time-string ts))
  11833. (when (and (eq org-ts-what 'minute)
  11834. (eq current-prefix-arg nil))
  11835. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11836. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11837. (setcar (cdr time0) (+ (nth 1 time0)
  11838. (if (> n 0) (- rem) (- dm rem))))))
  11839. (setq time
  11840. (encode-time (or (car time0) 0)
  11841. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11842. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11843. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11844. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11845. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11846. (nthcdr 6 time0)))
  11847. (when (and (member org-ts-what '(hour minute))
  11848. extra
  11849. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11850. (setq extra (org-modify-ts-extra
  11851. extra
  11852. (if (eq org-ts-what 'hour) 2 5)
  11853. n dm)))
  11854. (when (integerp org-ts-what)
  11855. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11856. (if (eq what 'calendar)
  11857. (let ((cal-date (org-get-date-from-calendar)))
  11858. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11859. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11860. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11861. (setcar time0 (or (car time0) 0))
  11862. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11863. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11864. (setq time (apply 'encode-time time0))))
  11865. (setq org-last-changed-timestamp
  11866. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11867. (org-clock-update-time-maybe)
  11868. (goto-char pos)
  11869. ;; Try to recenter the calendar window, if any
  11870. (if (and org-calendar-follow-timestamp-change
  11871. (get-buffer-window "*Calendar*" t)
  11872. (memq org-ts-what '(day month year)))
  11873. (org-recenter-calendar (time-to-days time))))))
  11874. (defun org-modify-ts-extra (s pos n dm)
  11875. "Change the different parts of the lead-time and repeat fields in timestamp."
  11876. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11877. ng h m new rem)
  11878. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11879. (cond
  11880. ((or (org-pos-in-match-range pos 2)
  11881. (org-pos-in-match-range pos 3))
  11882. (setq m (string-to-number (match-string 3 s))
  11883. h (string-to-number (match-string 2 s)))
  11884. (if (org-pos-in-match-range pos 2)
  11885. (setq h (+ h n))
  11886. (setq n (* dm (org-no-warnings (signum n))))
  11887. (when (not (= 0 (setq rem (% m dm))))
  11888. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11889. (setq m (+ m n)))
  11890. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11891. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11892. (setq h (min 24 (max 0 h)))
  11893. (setq ng 1 new (format "-%02d:%02d" h m)))
  11894. ((org-pos-in-match-range pos 6)
  11895. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11896. ((org-pos-in-match-range pos 5)
  11897. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11898. ((org-pos-in-match-range pos 9)
  11899. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11900. ((org-pos-in-match-range pos 8)
  11901. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11902. (when ng
  11903. (setq s (concat
  11904. (substring s 0 (match-beginning ng))
  11905. new
  11906. (substring s (match-end ng))))))
  11907. s))
  11908. (defun org-recenter-calendar (date)
  11909. "If the calendar is visible, recenter it to DATE."
  11910. (let* ((win (selected-window))
  11911. (cwin (get-buffer-window "*Calendar*" t))
  11912. (calendar-move-hook nil))
  11913. (when cwin
  11914. (select-window cwin)
  11915. (calendar-goto-date (if (listp date) date
  11916. (calendar-gregorian-from-absolute date)))
  11917. (select-window win))))
  11918. (defun org-goto-calendar (&optional arg)
  11919. "Go to the Emacs calendar at the current date.
  11920. If there is a time stamp in the current line, go to that date.
  11921. A prefix ARG can be used to force the current date."
  11922. (interactive "P")
  11923. (let ((tsr org-ts-regexp) diff
  11924. (calendar-move-hook nil)
  11925. (calendar-view-holidays-initially-flag nil)
  11926. (view-calendar-holidays-initially nil)
  11927. (calendar-view-diary-initially-flag nil)
  11928. (view-diary-entries-initially nil))
  11929. (if (or (org-at-timestamp-p)
  11930. (save-excursion
  11931. (beginning-of-line 1)
  11932. (looking-at (concat ".*" tsr))))
  11933. (let ((d1 (time-to-days (current-time)))
  11934. (d2 (time-to-days
  11935. (org-time-string-to-time (match-string 1)))))
  11936. (setq diff (- d2 d1))))
  11937. (calendar)
  11938. (calendar-goto-today)
  11939. (if (and diff (not arg)) (calendar-forward-day diff))))
  11940. (defun org-get-date-from-calendar ()
  11941. "Return a list (month day year) of date at point in calendar."
  11942. (with-current-buffer "*Calendar*"
  11943. (save-match-data
  11944. (calendar-cursor-to-date))))
  11945. (defun org-date-from-calendar ()
  11946. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11947. If there is already a time stamp at the cursor position, update it."
  11948. (interactive)
  11949. (if (org-at-timestamp-p t)
  11950. (org-timestamp-change 0 'calendar)
  11951. (let ((cal-date (org-get-date-from-calendar)))
  11952. (org-insert-time-stamp
  11953. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11954. (defun org-minutes-to-hh:mm-string (m)
  11955. "Compute H:MM from a number of minutes."
  11956. (let ((h (/ m 60)))
  11957. (setq m (- m (* 60 h)))
  11958. (format org-time-clocksum-format h m)))
  11959. (defun org-hh:mm-string-to-minutes (s)
  11960. "Convert a string H:MM to a number of minutes."
  11961. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11962. (+ (* (string-to-number (match-string 1 s)) 60)
  11963. (string-to-number (match-string 2 s)))
  11964. 0))
  11965. ;;;; Files
  11966. (defun org-save-all-org-buffers ()
  11967. "Save all Org-mode buffers without user confirmation."
  11968. (interactive)
  11969. (message "Saving all Org-mode buffers...")
  11970. (save-some-buffers t 'org-mode-p)
  11971. (when (featurep 'org-id) (org-id-locations-save))
  11972. (message "Saving all Org-mode buffers... done"))
  11973. (defun org-revert-all-org-buffers ()
  11974. "Revert all Org-mode buffers.
  11975. Prompt for confirmation when there are unsaved changes.
  11976. Be sure you know what you are doing before letting this function
  11977. overwrite your changes.
  11978. This function is useful in a setup where one tracks org files
  11979. with a version control system, to revert on one machine after pulling
  11980. changes from another. I believe the procedure must be like this:
  11981. 1. M-x org-save-all-org-buffers
  11982. 2. Pull changes from the other machine, resolve conflicts
  11983. 3. M-x org-revert-all-org-buffers"
  11984. (interactive)
  11985. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11986. (error "Abort"))
  11987. (save-excursion
  11988. (save-window-excursion
  11989. (mapc
  11990. (lambda (b)
  11991. (when (and (with-current-buffer b (org-mode-p))
  11992. (with-current-buffer b buffer-file-name))
  11993. (switch-to-buffer b)
  11994. (revert-buffer t 'no-confirm)))
  11995. (buffer-list))
  11996. (when (and (featurep 'org-id) org-id-track-globally)
  11997. (org-id-locations-load)))))
  11998. ;;;; Agenda files
  11999. ;;;###autoload
  12000. (defun org-iswitchb (&optional arg)
  12001. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12002. With a prefix argument, restrict available to files.
  12003. With two prefix arguments, restrict available buffers to agenda files.
  12004. Due to some yet unresolved reason, the global function
  12005. `iswitchb-mode' needs to be active for this function to work."
  12006. (interactive "P")
  12007. (require 'iswitchb)
  12008. (let ((enabled iswitchb-mode) blist)
  12009. (or enabled (iswitchb-mode 1))
  12010. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12011. ((equal arg '(16)) (org-buffer-list 'agenda))
  12012. (t (org-buffer-list))))
  12013. (unwind-protect
  12014. (let ((iswitchb-make-buflist-hook
  12015. (lambda ()
  12016. (setq iswitchb-temp-buflist
  12017. (mapcar 'buffer-name blist)))))
  12018. (switch-to-buffer
  12019. (iswitchb-read-buffer
  12020. "Switch-to: " nil t))
  12021. (or enabled (iswitchb-mode -1))))))
  12022. ;;;###autoload
  12023. (defun org-ido-switchb (&optional arg)
  12024. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12025. With a prefix argument, restrict available to files.
  12026. With two prefix arguments, restrict available buffers to agenda files."
  12027. (interactive "P")
  12028. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12029. ((equal arg '(16)) (org-buffer-list 'agenda))
  12030. (t (org-buffer-list)))))
  12031. (switch-to-buffer
  12032. (org-ido-completing-read "Org buffer: "
  12033. (mapcar 'list (mapcar 'buffer-name blist))
  12034. nil t))))
  12035. (defun org-buffer-list (&optional predicate exclude-tmp)
  12036. "Return a list of Org buffers.
  12037. PREDICATE can be `export', `files' or `agenda'.
  12038. export restrict the list to Export buffers.
  12039. files restrict the list to buffers visiting Org files.
  12040. agenda restrict the list to buffers visiting agenda files.
  12041. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12042. (let* ((bfn nil)
  12043. (agenda-files (and (eq predicate 'agenda)
  12044. (mapcar 'file-truename (org-agenda-files t))))
  12045. (filter
  12046. (cond
  12047. ((eq predicate 'files)
  12048. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12049. ((eq predicate 'export)
  12050. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12051. ((eq predicate 'agenda)
  12052. (lambda (b)
  12053. (with-current-buffer b
  12054. (and (eq major-mode 'org-mode)
  12055. (setq bfn (buffer-file-name b))
  12056. (member (file-truename bfn) agenda-files)))))
  12057. (t (lambda (b) (with-current-buffer b
  12058. (or (eq major-mode 'org-mode)
  12059. (string-match "\*Org .*Export"
  12060. (buffer-name b)))))))))
  12061. (delq nil
  12062. (mapcar
  12063. (lambda(b)
  12064. (if (and (funcall filter b)
  12065. (or (not exclude-tmp)
  12066. (not (string-match "tmp" (buffer-name b)))))
  12067. b
  12068. nil))
  12069. (buffer-list)))))
  12070. (defun org-agenda-files (&optional unrestricted archives)
  12071. "Get the list of agenda files.
  12072. Optional UNRESTRICTED means return the full list even if a restriction
  12073. is currently in place.
  12074. When ARCHIVES is t, include all archive files hat are really being
  12075. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12076. `org-agenda-archives-mode' is t."
  12077. (let ((files
  12078. (cond
  12079. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12080. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12081. ((listp org-agenda-files) org-agenda-files)
  12082. (t (error "Invalid value of `org-agenda-files'")))))
  12083. (setq files (apply 'append
  12084. (mapcar (lambda (f)
  12085. (if (file-directory-p f)
  12086. (directory-files
  12087. f t org-agenda-file-regexp)
  12088. (list f)))
  12089. files)))
  12090. (when org-agenda-skip-unavailable-files
  12091. (setq files (delq nil
  12092. (mapcar (function
  12093. (lambda (file)
  12094. (and (file-readable-p file) file)))
  12095. files))))
  12096. (when (or (eq archives t)
  12097. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12098. (setq files (org-add-archive-files files)))
  12099. files))
  12100. (defun org-edit-agenda-file-list ()
  12101. "Edit the list of agenda files.
  12102. Depending on setup, this either uses customize to edit the variable
  12103. `org-agenda-files', or it visits the file that is holding the list. In the
  12104. latter case, the buffer is set up in a way that saving it automatically kills
  12105. the buffer and restores the previous window configuration."
  12106. (interactive)
  12107. (if (stringp org-agenda-files)
  12108. (let ((cw (current-window-configuration)))
  12109. (find-file org-agenda-files)
  12110. (org-set-local 'org-window-configuration cw)
  12111. (org-add-hook 'after-save-hook
  12112. (lambda ()
  12113. (set-window-configuration
  12114. (prog1 org-window-configuration
  12115. (kill-buffer (current-buffer))))
  12116. (org-install-agenda-files-menu)
  12117. (message "New agenda file list installed"))
  12118. nil 'local)
  12119. (message "%s" (substitute-command-keys
  12120. "Edit list and finish with \\[save-buffer]")))
  12121. (customize-variable 'org-agenda-files)))
  12122. (defun org-store-new-agenda-file-list (list)
  12123. "Set new value for the agenda file list and save it correctly."
  12124. (if (stringp org-agenda-files)
  12125. (let ((f org-agenda-files) b)
  12126. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12127. (with-temp-file f
  12128. (insert (mapconcat 'identity list "\n") "\n")))
  12129. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12130. (setq org-agenda-files list)
  12131. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12132. (defun org-read-agenda-file-list ()
  12133. "Read the list of agenda files from a file."
  12134. (when (file-directory-p org-agenda-files)
  12135. (error "`org-agenda-files' cannot be a single directory"))
  12136. (when (stringp org-agenda-files)
  12137. (with-temp-buffer
  12138. (insert-file-contents org-agenda-files)
  12139. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12140. ;;;###autoload
  12141. (defun org-cycle-agenda-files ()
  12142. "Cycle through the files in `org-agenda-files'.
  12143. If the current buffer visits an agenda file, find the next one in the list.
  12144. If the current buffer does not, find the first agenda file."
  12145. (interactive)
  12146. (let* ((fs (org-agenda-files t))
  12147. (files (append fs (list (car fs))))
  12148. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12149. file)
  12150. (unless files (error "No agenda files"))
  12151. (catch 'exit
  12152. (while (setq file (pop files))
  12153. (if (equal (file-truename file) tcf)
  12154. (when (car files)
  12155. (find-file (car files))
  12156. (throw 'exit t))))
  12157. (find-file (car fs)))
  12158. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12159. (defun org-agenda-file-to-front (&optional to-end)
  12160. "Move/add the current file to the top of the agenda file list.
  12161. If the file is not present in the list, it is added to the front. If it is
  12162. present, it is moved there. With optional argument TO-END, add/move to the
  12163. end of the list."
  12164. (interactive "P")
  12165. (let ((org-agenda-skip-unavailable-files nil)
  12166. (file-alist (mapcar (lambda (x)
  12167. (cons (file-truename x) x))
  12168. (org-agenda-files t)))
  12169. (ctf (file-truename buffer-file-name))
  12170. x had)
  12171. (setq x (assoc ctf file-alist) had x)
  12172. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12173. (if to-end
  12174. (setq file-alist (append (delq x file-alist) (list x)))
  12175. (setq file-alist (cons x (delq x file-alist))))
  12176. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12177. (org-install-agenda-files-menu)
  12178. (message "File %s to %s of agenda file list"
  12179. (if had "moved" "added") (if to-end "end" "front"))))
  12180. (defun org-remove-file (&optional file)
  12181. "Remove current file from the list of files in variable `org-agenda-files'.
  12182. These are the files which are being checked for agenda entries.
  12183. Optional argument FILE means, use this file instead of the current."
  12184. (interactive)
  12185. (let* ((org-agenda-skip-unavailable-files nil)
  12186. (file (or file buffer-file-name))
  12187. (true-file (file-truename file))
  12188. (afile (abbreviate-file-name file))
  12189. (files (delq nil (mapcar
  12190. (lambda (x)
  12191. (if (equal true-file
  12192. (file-truename x))
  12193. nil x))
  12194. (org-agenda-files t)))))
  12195. (if (not (= (length files) (length (org-agenda-files t))))
  12196. (progn
  12197. (org-store-new-agenda-file-list files)
  12198. (org-install-agenda-files-menu)
  12199. (message "Removed file: %s" afile))
  12200. (message "File was not in list: %s (not removed)" afile))))
  12201. (defun org-file-menu-entry (file)
  12202. (vector file (list 'find-file file) t))
  12203. (defun org-check-agenda-file (file)
  12204. "Make sure FILE exists. If not, ask user what to do."
  12205. (when (not (file-exists-p file))
  12206. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12207. (abbreviate-file-name file))
  12208. (let ((r (downcase (read-char-exclusive))))
  12209. (cond
  12210. ((equal r ?r)
  12211. (org-remove-file file)
  12212. (throw 'nextfile t))
  12213. (t (error "Abort"))))))
  12214. (defun org-get-agenda-file-buffer (file)
  12215. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12216. it to the list of buffers which might be released later."
  12217. (let ((buf (org-find-base-buffer-visiting file)))
  12218. (if buf
  12219. buf ; just return it
  12220. ;; Make a new buffer and remember it
  12221. (setq buf (find-file-noselect file))
  12222. (if buf (push buf org-agenda-new-buffers))
  12223. buf)))
  12224. (defun org-release-buffers (blist)
  12225. "Release all buffers in list, asking the user for confirmation when needed.
  12226. When a buffer is unmodified, it is just killed. When modified, it is saved
  12227. \(if the user agrees) and then killed."
  12228. (let (buf file)
  12229. (while (setq buf (pop blist))
  12230. (setq file (buffer-file-name buf))
  12231. (when (and (buffer-modified-p buf)
  12232. file
  12233. (y-or-n-p (format "Save file %s? " file)))
  12234. (with-current-buffer buf (save-buffer)))
  12235. (kill-buffer buf))))
  12236. (defun org-prepare-agenda-buffers (files)
  12237. "Create buffers for all agenda files, protect archived trees and comments."
  12238. (interactive)
  12239. (let ((pa '(:org-archived t))
  12240. (pc '(:org-comment t))
  12241. (pall '(:org-archived t :org-comment t))
  12242. (inhibit-read-only t)
  12243. (rea (concat ":" org-archive-tag ":"))
  12244. bmp file re)
  12245. (save-excursion
  12246. (save-restriction
  12247. (while (setq file (pop files))
  12248. (catch 'nextfile
  12249. (if (bufferp file)
  12250. (set-buffer file)
  12251. (org-check-agenda-file file)
  12252. (set-buffer (org-get-agenda-file-buffer file)))
  12253. (widen)
  12254. (setq bmp (buffer-modified-p))
  12255. (org-refresh-category-properties)
  12256. (setq org-todo-keywords-for-agenda
  12257. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12258. (setq org-done-keywords-for-agenda
  12259. (append org-done-keywords-for-agenda org-done-keywords))
  12260. (setq org-todo-keyword-alist-for-agenda
  12261. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12262. (setq org-tag-alist-for-agenda
  12263. (append org-tag-alist-for-agenda org-tag-alist))
  12264. (save-excursion
  12265. (remove-text-properties (point-min) (point-max) pall)
  12266. (when org-agenda-skip-archived-trees
  12267. (goto-char (point-min))
  12268. (while (re-search-forward rea nil t)
  12269. (if (org-on-heading-p t)
  12270. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12271. (goto-char (point-min))
  12272. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12273. (while (re-search-forward re nil t)
  12274. (add-text-properties
  12275. (match-beginning 0) (org-end-of-subtree t) pc)))
  12276. (set-buffer-modified-p bmp)))))
  12277. (setq org-todo-keyword-alist-for-agenda
  12278. (org-uniquify org-todo-keyword-alist-for-agenda)
  12279. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12280. ;;;; Embedded LaTeX
  12281. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12282. "Keymap for the minor `org-cdlatex-mode'.")
  12283. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12284. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12285. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12286. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12287. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12288. (defvar org-cdlatex-texmathp-advice-is-done nil
  12289. "Flag remembering if we have applied the advice to texmathp already.")
  12290. (define-minor-mode org-cdlatex-mode
  12291. "Toggle the minor `org-cdlatex-mode'.
  12292. This mode supports entering LaTeX environment and math in LaTeX fragments
  12293. in Org-mode.
  12294. \\{org-cdlatex-mode-map}"
  12295. nil " OCDL" nil
  12296. (when org-cdlatex-mode (require 'cdlatex))
  12297. (unless org-cdlatex-texmathp-advice-is-done
  12298. (setq org-cdlatex-texmathp-advice-is-done t)
  12299. (defadvice texmathp (around org-math-always-on activate)
  12300. "Always return t in org-mode buffers.
  12301. This is because we want to insert math symbols without dollars even outside
  12302. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12303. an embedded LaTeX fragment, let texmathp do its job.
  12304. \\[org-cdlatex-mode-map]"
  12305. (interactive)
  12306. (let (p)
  12307. (cond
  12308. ((not (org-mode-p)) ad-do-it)
  12309. ((eq this-command 'cdlatex-math-symbol)
  12310. (setq ad-return-value t
  12311. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12312. (t
  12313. (let ((p (org-inside-LaTeX-fragment-p)))
  12314. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12315. (setq ad-return-value t
  12316. texmathp-why '("Org-mode embedded math" . 0))
  12317. (if p ad-do-it)))))))))
  12318. (defun turn-on-org-cdlatex ()
  12319. "Unconditionally turn on `org-cdlatex-mode'."
  12320. (org-cdlatex-mode 1))
  12321. (defun org-inside-LaTeX-fragment-p ()
  12322. "Test if point is inside a LaTeX fragment.
  12323. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12324. sequence appearing also before point.
  12325. Even though the matchers for math are configurable, this function assumes
  12326. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12327. delimiters are skipped when they have been removed by customization.
  12328. The return value is nil, or a cons cell with the delimiter and
  12329. and the position of this delimiter.
  12330. This function does a reasonably good job, but can locally be fooled by
  12331. for example currency specifications. For example it will assume being in
  12332. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12333. fragments that are properly closed, but during editing, we have to live
  12334. with the uncertainty caused by missing closing delimiters. This function
  12335. looks only before point, not after."
  12336. (catch 'exit
  12337. (let ((pos (point))
  12338. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12339. (lim (progn
  12340. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12341. (point)))
  12342. dd-on str (start 0) m re)
  12343. (goto-char pos)
  12344. (when dodollar
  12345. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12346. re (nth 1 (assoc "$" org-latex-regexps)))
  12347. (while (string-match re str start)
  12348. (cond
  12349. ((= (match-end 0) (length str))
  12350. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12351. ((= (match-end 0) (- (length str) 5))
  12352. (throw 'exit nil))
  12353. (t (setq start (match-end 0))))))
  12354. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12355. (goto-char pos)
  12356. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12357. (and (match-beginning 2) (throw 'exit nil))
  12358. ;; count $$
  12359. (while (re-search-backward "\\$\\$" lim t)
  12360. (setq dd-on (not dd-on)))
  12361. (goto-char pos)
  12362. (if dd-on (cons "$$" m))))))
  12363. (defun org-try-cdlatex-tab ()
  12364. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12365. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12366. - inside a LaTeX fragment, or
  12367. - after the first word in a line, where an abbreviation expansion could
  12368. insert a LaTeX environment."
  12369. (when org-cdlatex-mode
  12370. (cond
  12371. ((save-excursion
  12372. (skip-chars-backward "a-zA-Z0-9*")
  12373. (skip-chars-backward " \t")
  12374. (bolp))
  12375. (cdlatex-tab) t)
  12376. ((org-inside-LaTeX-fragment-p)
  12377. (cdlatex-tab) t)
  12378. (t nil))))
  12379. (defun org-cdlatex-underscore-caret (&optional arg)
  12380. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12381. Revert to the normal definition outside of these fragments."
  12382. (interactive "P")
  12383. (if (org-inside-LaTeX-fragment-p)
  12384. (call-interactively 'cdlatex-sub-superscript)
  12385. (let (org-cdlatex-mode)
  12386. (call-interactively (key-binding (vector last-input-event))))))
  12387. (defun org-cdlatex-math-modify (&optional arg)
  12388. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12389. Revert to the normal definition outside of these fragments."
  12390. (interactive "P")
  12391. (if (org-inside-LaTeX-fragment-p)
  12392. (call-interactively 'cdlatex-math-modify)
  12393. (let (org-cdlatex-mode)
  12394. (call-interactively (key-binding (vector last-input-event))))))
  12395. (defvar org-latex-fragment-image-overlays nil
  12396. "List of overlays carrying the images of latex fragments.")
  12397. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12398. (defun org-remove-latex-fragment-image-overlays ()
  12399. "Remove all overlays with LaTeX fragment images in current buffer."
  12400. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12401. (setq org-latex-fragment-image-overlays nil))
  12402. (defun org-preview-latex-fragment (&optional subtree)
  12403. "Preview the LaTeX fragment at point, or all locally or globally.
  12404. If the cursor is in a LaTeX fragment, create the image and overlay
  12405. it over the source code. If there is no fragment at point, display
  12406. all fragments in the current text, from one headline to the next. With
  12407. prefix SUBTREE, display all fragments in the current subtree. With a
  12408. double prefix `C-u C-u', or when the cursor is before the first headline,
  12409. display all fragments in the buffer.
  12410. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12411. (interactive "P")
  12412. (org-remove-latex-fragment-image-overlays)
  12413. (save-excursion
  12414. (save-restriction
  12415. (let (beg end at msg)
  12416. (cond
  12417. ((or (equal subtree '(16))
  12418. (not (save-excursion
  12419. (re-search-backward (concat "^" outline-regexp) nil t))))
  12420. (setq beg (point-min) end (point-max)
  12421. msg "Creating images for buffer...%s"))
  12422. ((equal subtree '(4))
  12423. (org-back-to-heading)
  12424. (setq beg (point) end (org-end-of-subtree t)
  12425. msg "Creating images for subtree...%s"))
  12426. (t
  12427. (if (setq at (org-inside-LaTeX-fragment-p))
  12428. (goto-char (max (point-min) (- (cdr at) 2)))
  12429. (org-back-to-heading))
  12430. (setq beg (point) end (progn (outline-next-heading) (point))
  12431. msg (if at "Creating image...%s"
  12432. "Creating images for entry...%s"))))
  12433. (message msg "")
  12434. (narrow-to-region beg end)
  12435. (goto-char beg)
  12436. (org-format-latex
  12437. (concat "ltxpng/" (file-name-sans-extension
  12438. (file-name-nondirectory
  12439. buffer-file-name)))
  12440. default-directory 'overlays msg at 'forbuffer)
  12441. (message msg "done. Use `C-c C-c' to remove images.")))))
  12442. (defvar org-latex-regexps
  12443. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12444. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12445. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12446. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12447. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12448. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12449. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12450. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12451. "Regular expressions for matching embedded LaTeX.")
  12452. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12453. "Replace LaTeX fragments with links to an image, and produce images."
  12454. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12455. (let* ((prefixnodir (file-name-nondirectory prefix))
  12456. (absprefix (expand-file-name prefix dir))
  12457. (todir (file-name-directory absprefix))
  12458. (opt org-format-latex-options)
  12459. (matchers (plist-get opt :matchers))
  12460. (re-list org-latex-regexps)
  12461. (cnt 0) txt link beg end re e checkdir
  12462. executables-checked
  12463. m n block linkfile movefile ov)
  12464. ;; Check if there are old images files with this prefix, and remove them
  12465. (when (file-directory-p todir)
  12466. (mapc 'delete-file
  12467. (directory-files
  12468. todir 'full
  12469. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12470. ;; Check the different regular expressions
  12471. (while (setq e (pop re-list))
  12472. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12473. block (if (nth 3 e) "\n\n" ""))
  12474. (when (member m matchers)
  12475. (goto-char (point-min))
  12476. (while (re-search-forward re nil t)
  12477. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12478. (not (get-text-property (match-beginning n)
  12479. 'org-protected)))
  12480. (setq txt (match-string n)
  12481. beg (match-beginning n) end (match-end n)
  12482. cnt (1+ cnt)
  12483. linkfile (format "%s_%04d.png" prefix cnt)
  12484. movefile (format "%s_%04d.png" absprefix cnt)
  12485. link (concat block "[[file:" linkfile "]]" block))
  12486. (if msg (message msg cnt))
  12487. (goto-char beg)
  12488. (unless checkdir ; make sure the directory exists
  12489. (setq checkdir t)
  12490. (or (file-directory-p todir) (make-directory todir)))
  12491. (unless executables-checked
  12492. (org-check-external-command
  12493. "latex" "needed to convert LaTeX fragments to images")
  12494. (org-check-external-command
  12495. "dvipng" "needed to convert LaTeX fragments to images")
  12496. (setq executables-checked t))
  12497. (org-create-formula-image
  12498. txt movefile opt forbuffer)
  12499. (if overlays
  12500. (progn
  12501. (setq ov (org-make-overlay beg end))
  12502. (if (featurep 'xemacs)
  12503. (progn
  12504. (org-overlay-put ov 'invisible t)
  12505. (org-overlay-put
  12506. ov 'end-glyph
  12507. (make-glyph (vector 'png :file movefile))))
  12508. (org-overlay-put
  12509. ov 'display
  12510. (list 'image :type 'png :file movefile :ascent 'center)))
  12511. (push ov org-latex-fragment-image-overlays)
  12512. (goto-char end))
  12513. (delete-region beg end)
  12514. (insert link))))))))
  12515. ;; This function borrows from Ganesh Swami's latex2png.el
  12516. (defun org-create-formula-image (string tofile options buffer)
  12517. (let* ((tmpdir (if (featurep 'xemacs)
  12518. (temp-directory)
  12519. temporary-file-directory))
  12520. (texfilebase (make-temp-name
  12521. (expand-file-name "orgtex" tmpdir)))
  12522. (texfile (concat texfilebase ".tex"))
  12523. (dvifile (concat texfilebase ".dvi"))
  12524. (pngfile (concat texfilebase ".png"))
  12525. (fnh (if (featurep 'xemacs)
  12526. (font-height (get-face-font 'default))
  12527. (face-attribute 'default :height nil)))
  12528. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12529. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12530. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12531. "Black"))
  12532. (bg (or (plist-get options (if buffer :background :html-background))
  12533. "Transparent")))
  12534. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12535. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12536. (with-temp-file texfile
  12537. (insert org-format-latex-header
  12538. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12539. (let ((dir default-directory))
  12540. (condition-case nil
  12541. (progn
  12542. (cd tmpdir)
  12543. (call-process "latex" nil nil nil texfile))
  12544. (error nil))
  12545. (cd dir))
  12546. (if (not (file-exists-p dvifile))
  12547. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12548. (condition-case nil
  12549. (call-process "dvipng" nil nil nil
  12550. "-E" "-fg" fg "-bg" bg
  12551. "-D" dpi
  12552. ;;"-x" scale "-y" scale
  12553. "-T" "tight"
  12554. "-o" pngfile
  12555. dvifile)
  12556. (error nil))
  12557. (if (not (file-exists-p pngfile))
  12558. (progn (message "Failed to create png file from %s" texfile) nil)
  12559. ;; Use the requested file name and clean up
  12560. (copy-file pngfile tofile 'replace)
  12561. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12562. (delete-file (concat texfilebase e)))
  12563. pngfile))))
  12564. (defun org-dvipng-color (attr)
  12565. "Return an rgb color specification for dvipng."
  12566. (apply 'format "rgb %s %s %s"
  12567. (mapcar 'org-normalize-color
  12568. (color-values (face-attribute 'default attr nil)))))
  12569. (defun org-normalize-color (value)
  12570. "Return string to be used as color value for an RGB component."
  12571. (format "%g" (/ value 65535.0)))
  12572. ;;;; Key bindings
  12573. ;; Make `C-c C-x' a prefix key
  12574. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12575. ;; TAB key with modifiers
  12576. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12577. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12578. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12579. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12580. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12581. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12582. ;; The following line is necessary under Suse GNU/Linux
  12583. (unless (featurep 'xemacs)
  12584. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12585. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12586. (define-key org-mode-map [backtab] 'org-shifttab)
  12587. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12588. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12589. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12590. ;; Cursor keys with modifiers
  12591. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12592. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12593. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12594. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12595. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12596. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12597. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12598. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12599. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12600. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12601. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12602. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12603. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12604. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12605. ;;; Extra keys for tty access.
  12606. ;; We only set them when really needed because otherwise the
  12607. ;; menus don't show the simple keys
  12608. (when (or org-use-extra-keys
  12609. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12610. (not window-system))
  12611. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12612. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12613. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12614. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12615. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12616. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12617. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12618. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12619. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12620. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12621. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12622. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12623. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12624. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12625. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12626. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12627. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12628. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12629. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12630. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12631. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12632. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12633. ;; All the other keys
  12634. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12635. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12636. (if (boundp 'narrow-map)
  12637. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12638. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12639. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12640. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12641. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12642. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12643. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12644. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12645. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12646. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12647. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12648. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12649. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12650. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12651. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12652. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12653. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12654. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12655. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12656. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12657. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12658. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12659. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12660. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12661. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12662. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12663. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12664. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12665. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12666. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12667. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12668. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12669. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12670. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12671. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12672. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12673. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12674. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12675. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12676. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12677. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12678. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12679. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12680. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12681. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12682. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12683. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12684. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12685. (org-defkey org-mode-map "\C-m" 'org-return)
  12686. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12687. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12688. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12689. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12690. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12691. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12692. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12693. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12694. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12695. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12696. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12697. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12698. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12699. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12700. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12701. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12702. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12703. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12704. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12705. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12706. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12707. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12708. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12709. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12710. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12711. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12712. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12713. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12714. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12715. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12716. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12717. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12718. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12719. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12720. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12721. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12722. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12723. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12724. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12725. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12726. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12727. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12728. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12729. (when (featurep 'xemacs)
  12730. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12731. (defvar org-self-insert-command-undo-counter 0)
  12732. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12733. (defun org-self-insert-command (N)
  12734. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12735. If the cursor is in a table looking at whitespace, the whitespace is
  12736. overwritten, and the table is not marked as requiring realignment."
  12737. (interactive "p")
  12738. (if (and
  12739. (org-table-p)
  12740. (progn
  12741. ;; check if we blank the field, and if that triggers align
  12742. (and (featurep 'org-table) org-table-auto-blank-field
  12743. (member last-command
  12744. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12745. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12746. ;; got extra space, this field does not determine column width
  12747. (let (org-table-may-need-update) (org-table-blank-field))
  12748. ;; no extra space, this field may determine column width
  12749. (org-table-blank-field)))
  12750. t)
  12751. (eq N 1)
  12752. (looking-at "[^|\n]* |"))
  12753. (let (org-table-may-need-update)
  12754. (goto-char (1- (match-end 0)))
  12755. (delete-backward-char 1)
  12756. (goto-char (match-beginning 0))
  12757. (self-insert-command N))
  12758. (setq org-table-may-need-update t)
  12759. (self-insert-command N)
  12760. (org-fix-tags-on-the-fly)
  12761. (if org-self-insert-cluster-for-undo
  12762. (if (not (eq last-command 'org-self-insert-command))
  12763. (setq org-self-insert-command-undo-counter 1)
  12764. (if (>= org-self-insert-command-undo-counter 20)
  12765. (setq org-self-insert-command-undo-counter 1)
  12766. (and (> org-self-insert-command-undo-counter 0)
  12767. buffer-undo-list
  12768. (not (cadr buffer-undo-list)) ; remove nil entry
  12769. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12770. (setq org-self-insert-command-undo-counter
  12771. (1+ org-self-insert-command-undo-counter)))))))
  12772. (defun org-fix-tags-on-the-fly ()
  12773. (when (and (equal (char-after (point-at-bol)) ?*)
  12774. (org-on-heading-p))
  12775. (org-align-tags-here org-tags-column)))
  12776. (defun org-delete-backward-char (N)
  12777. "Like `delete-backward-char', insert whitespace at field end in tables.
  12778. When deleting backwards, in tables this function will insert whitespace in
  12779. front of the next \"|\" separator, to keep the table aligned. The table will
  12780. still be marked for re-alignment if the field did fill the entire column,
  12781. because, in this case the deletion might narrow the column."
  12782. (interactive "p")
  12783. (if (and (org-table-p)
  12784. (eq N 1)
  12785. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12786. (looking-at ".*?|"))
  12787. (let ((pos (point))
  12788. (noalign (looking-at "[^|\n\r]* |"))
  12789. (c org-table-may-need-update))
  12790. (backward-delete-char N)
  12791. (skip-chars-forward "^|")
  12792. (insert " ")
  12793. (goto-char (1- pos))
  12794. ;; noalign: if there were two spaces at the end, this field
  12795. ;; does not determine the width of the column.
  12796. (if noalign (setq org-table-may-need-update c)))
  12797. (backward-delete-char N)
  12798. (org-fix-tags-on-the-fly)))
  12799. (defun org-delete-char (N)
  12800. "Like `delete-char', but insert whitespace at field end in tables.
  12801. When deleting characters, in tables this function will insert whitespace in
  12802. front of the next \"|\" separator, to keep the table aligned. The table will
  12803. still be marked for re-alignment if the field did fill the entire column,
  12804. because, in this case the deletion might narrow the column."
  12805. (interactive "p")
  12806. (if (and (org-table-p)
  12807. (not (bolp))
  12808. (not (= (char-after) ?|))
  12809. (eq N 1))
  12810. (if (looking-at ".*?|")
  12811. (let ((pos (point))
  12812. (noalign (looking-at "[^|\n\r]* |"))
  12813. (c org-table-may-need-update))
  12814. (replace-match (concat
  12815. (substring (match-string 0) 1 -1)
  12816. " |"))
  12817. (goto-char pos)
  12818. ;; noalign: if there were two spaces at the end, this field
  12819. ;; does not determine the width of the column.
  12820. (if noalign (setq org-table-may-need-update c)))
  12821. (delete-char N))
  12822. (delete-char N)
  12823. (org-fix-tags-on-the-fly)))
  12824. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12825. (put 'org-self-insert-command 'delete-selection t)
  12826. (put 'orgtbl-self-insert-command 'delete-selection t)
  12827. (put 'org-delete-char 'delete-selection 'supersede)
  12828. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12829. (put 'org-yank 'delete-selection 'yank)
  12830. ;; Make `flyspell-mode' delay after some commands
  12831. (put 'org-self-insert-command 'flyspell-delayed t)
  12832. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12833. (put 'org-delete-char 'flyspell-delayed t)
  12834. (put 'org-delete-backward-char 'flyspell-delayed t)
  12835. ;; Make pabbrev-mode expand after org-mode commands
  12836. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12837. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12838. ;; How to do this: Measure non-white length of current string
  12839. ;; If equal to column width, we should realign.
  12840. (defun org-remap (map &rest commands)
  12841. "In MAP, remap the functions given in COMMANDS.
  12842. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12843. (let (new old)
  12844. (while commands
  12845. (setq old (pop commands) new (pop commands))
  12846. (if (fboundp 'command-remapping)
  12847. (org-defkey map (vector 'remap old) new)
  12848. (substitute-key-definition old new map global-map)))))
  12849. (when (eq org-enable-table-editor 'optimized)
  12850. ;; If the user wants maximum table support, we need to hijack
  12851. ;; some standard editing functions
  12852. (org-remap org-mode-map
  12853. 'self-insert-command 'org-self-insert-command
  12854. 'delete-char 'org-delete-char
  12855. 'delete-backward-char 'org-delete-backward-char)
  12856. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12857. (defvar org-ctrl-c-ctrl-c-hook nil
  12858. "Hook for functions attaching themselves to `C-c C-c'.
  12859. This can be used to add additional functionality to the C-c C-c key which
  12860. executes context-dependent commands.
  12861. Each function will be called with no arguments. The function must check
  12862. if the context is appropriate for it to act. If yes, it should do its
  12863. thing and then return a non-nil value. If the context is wrong,
  12864. just do nothing and return nil.")
  12865. (defvar org-tab-first-hook nil
  12866. "Hook for functions to attach themselves to TAB.
  12867. See `org-ctrl-c-ctrl-c-hook' for more information.
  12868. This hook runs as the first action when TAB is pressed, even before
  12869. `org-cycle' messes around with the `outline-regexp' to cater for
  12870. inline tasks and plain list item folding.
  12871. If any function in this hook returns t, not other actions like table
  12872. field motion visibility cycling will be done.")
  12873. (defvar org-tab-after-check-for-table-hook nil
  12874. "Hook for functions to attach themselves to TAB.
  12875. See `org-ctrl-c-ctrl-c-hook' for more information.
  12876. This hook runs after it has been established that the cursor is not in a
  12877. table, but before checking if the cursor is in a headline or if global cycling
  12878. should be done.
  12879. If any function in this hook returns t, not other actions like visibility
  12880. cycling will be done.")
  12881. (defvar org-tab-after-check-for-cycling-hook nil
  12882. "Hook for functions to attach themselves to TAB.
  12883. See `org-ctrl-c-ctrl-c-hook' for more information.
  12884. This hook runs after it has been established that not table field motion and
  12885. not visibility should be done because of current context. This is probably
  12886. the place where a package like yasnippets can hook in.")
  12887. (defvar org-metaleft-hook nil
  12888. "Hook for functions attaching themselves to `M-left'.
  12889. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12890. (defvar org-metaright-hook nil
  12891. "Hook for functions attaching themselves to `M-right'.
  12892. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12893. (defvar org-metaup-hook nil
  12894. "Hook for functions attaching themselves to `M-up'.
  12895. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12896. (defvar org-metadown-hook nil
  12897. "Hook for functions attaching themselves to `M-down'.
  12898. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12899. (defvar org-shiftmetaleft-hook nil
  12900. "Hook for functions attaching themselves to `M-S-left'.
  12901. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12902. (defvar org-shiftmetaright-hook nil
  12903. "Hook for functions attaching themselves to `M-S-right'.
  12904. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12905. (defvar org-shiftmetaup-hook nil
  12906. "Hook for functions attaching themselves to `M-S-up'.
  12907. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12908. (defvar org-shiftmetadown-hook nil
  12909. "Hook for functions attaching themselves to `M-S-down'.
  12910. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12911. (defvar org-metareturn-hook nil
  12912. "Hook for functions attaching themselves to `M-RET'.
  12913. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12914. (defun org-modifier-cursor-error ()
  12915. "Throw an error, a modified cursor command was applied in wrong context."
  12916. (error "This command is active in special context like tables, headlines or items"))
  12917. (defun org-shiftselect-error ()
  12918. "Throw an error because Shift-Cursor command was applied in wrong context."
  12919. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12920. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12921. (error "This command works only in special context like headlines or timestamps.")))
  12922. (defun org-call-for-shift-select (cmd)
  12923. (let ((this-command-keys-shift-translated t))
  12924. (call-interactively cmd)))
  12925. (defun org-shifttab (&optional arg)
  12926. "Global visibility cycling or move to previous table field.
  12927. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12928. on context.
  12929. See the individual commands for more information."
  12930. (interactive "P")
  12931. (cond
  12932. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12933. ((integerp arg)
  12934. (message "Content view to level: %d" arg)
  12935. (org-content (prefix-numeric-value arg))
  12936. (setq org-cycle-global-status 'overview))
  12937. (t (call-interactively 'org-global-cycle))))
  12938. (defun org-shiftmetaleft ()
  12939. "Promote subtree or delete table column.
  12940. Calls `org-promote-subtree', `org-outdent-item',
  12941. or `org-table-delete-column', depending on context.
  12942. See the individual commands for more information."
  12943. (interactive)
  12944. (cond
  12945. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12946. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12947. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12948. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12949. (t (org-modifier-cursor-error))))
  12950. (defun org-shiftmetaright ()
  12951. "Demote subtree or insert table column.
  12952. Calls `org-demote-subtree', `org-indent-item',
  12953. or `org-table-insert-column', depending on context.
  12954. See the individual commands for more information."
  12955. (interactive)
  12956. (cond
  12957. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12958. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12959. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12960. ((org-at-item-p) (call-interactively 'org-indent-item))
  12961. (t (org-modifier-cursor-error))))
  12962. (defun org-shiftmetaup (&optional arg)
  12963. "Move subtree up or kill table row.
  12964. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12965. `org-move-item-up' depending on context. See the individual commands
  12966. for more information."
  12967. (interactive "P")
  12968. (cond
  12969. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12970. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12971. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12972. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12973. (t (org-modifier-cursor-error))))
  12974. (defun org-shiftmetadown (&optional arg)
  12975. "Move subtree down or insert table row.
  12976. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12977. `org-move-item-down', depending on context. See the individual
  12978. commands for more information."
  12979. (interactive "P")
  12980. (cond
  12981. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12982. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12983. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12984. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12985. (t (org-modifier-cursor-error))))
  12986. (defun org-metaleft (&optional arg)
  12987. "Promote heading or move table column to left.
  12988. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12989. With no specific context, calls the Emacs default `backward-word'.
  12990. See the individual commands for more information."
  12991. (interactive "P")
  12992. (cond
  12993. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12994. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12995. ((or (org-on-heading-p)
  12996. (and (org-region-active-p)
  12997. (save-excursion
  12998. (goto-char (region-beginning))
  12999. (org-on-heading-p))))
  13000. (call-interactively 'org-do-promote))
  13001. ((or (org-at-item-p)
  13002. (and (org-region-active-p)
  13003. (save-excursion
  13004. (goto-char (region-beginning))
  13005. (org-at-item-p))))
  13006. (call-interactively 'org-outdent-item))
  13007. (t (call-interactively 'backward-word))))
  13008. (defun org-metaright (&optional arg)
  13009. "Demote subtree or move table column to right.
  13010. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13011. With no specific context, calls the Emacs default `forward-word'.
  13012. See the individual commands for more information."
  13013. (interactive "P")
  13014. (cond
  13015. ((run-hook-with-args-until-success 'org-metaright-hook))
  13016. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13017. ((or (org-on-heading-p)
  13018. (and (org-region-active-p)
  13019. (save-excursion
  13020. (goto-char (region-beginning))
  13021. (org-on-heading-p))))
  13022. (call-interactively 'org-do-demote))
  13023. ((or (org-at-item-p)
  13024. (and (org-region-active-p)
  13025. (save-excursion
  13026. (goto-char (region-beginning))
  13027. (org-at-item-p))))
  13028. (call-interactively 'org-indent-item))
  13029. (t (call-interactively 'forward-word))))
  13030. (defun org-metaup (&optional arg)
  13031. "Move subtree up or move table row up.
  13032. Calls `org-move-subtree-up' or `org-table-move-row' or
  13033. `org-move-item-up', depending on context. See the individual commands
  13034. for more information."
  13035. (interactive "P")
  13036. (cond
  13037. ((run-hook-with-args-until-success 'org-metaup-hook))
  13038. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13039. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13040. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13041. (t (transpose-lines 1) (beginning-of-line -1))))
  13042. (defun org-metadown (&optional arg)
  13043. "Move subtree down or move table row down.
  13044. Calls `org-move-subtree-down' or `org-table-move-row' or
  13045. `org-move-item-down', depending on context. See the individual
  13046. commands for more information."
  13047. (interactive "P")
  13048. (cond
  13049. ((run-hook-with-args-until-success 'org-metadown-hook))
  13050. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13051. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13052. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13053. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13054. (defun org-shiftup (&optional arg)
  13055. "Increase item in timestamp or increase priority of current headline.
  13056. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13057. depending on context. See the individual commands for more information."
  13058. (interactive "P")
  13059. (cond
  13060. ((and org-support-shift-select (org-region-active-p))
  13061. (org-call-for-shift-select 'previous-line))
  13062. ((org-at-timestamp-p t)
  13063. (call-interactively (if org-edit-timestamp-down-means-later
  13064. 'org-timestamp-down 'org-timestamp-up)))
  13065. ((and (not (eq org-support-shift-select 'always))
  13066. (org-on-heading-p))
  13067. (call-interactively 'org-priority-up))
  13068. ((and (not org-support-shift-select) (org-at-item-p))
  13069. (call-interactively 'org-previous-item))
  13070. ((org-clocktable-try-shift 'up arg))
  13071. (org-support-shift-select
  13072. (org-call-for-shift-select 'previous-line))
  13073. (t (org-shiftselect-error))))
  13074. (defun org-shiftdown (&optional arg)
  13075. "Decrease item in timestamp or decrease priority of current headline.
  13076. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13077. depending on context. See the individual commands for more information."
  13078. (interactive "P")
  13079. (cond
  13080. ((and org-support-shift-select (org-region-active-p))
  13081. (org-call-for-shift-select 'next-line))
  13082. ((org-at-timestamp-p t)
  13083. (call-interactively (if org-edit-timestamp-down-means-later
  13084. 'org-timestamp-up 'org-timestamp-down)))
  13085. ((and (not (eq org-support-shift-select 'always))
  13086. (org-on-heading-p))
  13087. (call-interactively 'org-priority-down))
  13088. ((and (not org-support-shift-select) (org-at-item-p))
  13089. (call-interactively 'org-next-item))
  13090. ((org-clocktable-try-shift 'down arg))
  13091. (org-support-shift-select
  13092. (org-call-for-shift-select 'next-line))
  13093. (t (org-shiftselect-error))))
  13094. (defun org-shiftright (&optional arg)
  13095. "Cycle the thing at point or in the current line, depending on context.
  13096. Depending on context, this does one of the following:
  13097. - switch a timestamp at point one day into the future
  13098. - on a headline, switch to the next TODO keyword.
  13099. - on an item, switch entire list to the next bullet type
  13100. - on a property line, switch to the next allowed value
  13101. - on a clocktable definition line, move time block into the future"
  13102. (interactive "P")
  13103. (cond
  13104. ((and org-support-shift-select (org-region-active-p))
  13105. (org-call-for-shift-select 'forward-char))
  13106. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13107. ((and (not (eq org-support-shift-select 'always))
  13108. (org-on-heading-p))
  13109. (org-call-with-arg 'org-todo 'right))
  13110. ((or (and org-support-shift-select
  13111. (not (eq org-support-shift-select 'always))
  13112. (org-at-item-bullet-p))
  13113. (and (not org-support-shift-select) (org-at-item-p)))
  13114. (org-call-with-arg 'org-cycle-list-bullet nil))
  13115. ((and (not (eq org-support-shift-select 'always))
  13116. (org-at-property-p))
  13117. (call-interactively 'org-property-next-allowed-value))
  13118. ((org-clocktable-try-shift 'right arg))
  13119. (org-support-shift-select
  13120. (org-call-for-shift-select 'forward-char))
  13121. (t (org-shiftselect-error))))
  13122. (defun org-shiftleft (&optional arg)
  13123. "Cycle the thing at point or in the current line, depending on context.
  13124. Depending on context, this does one of the following:
  13125. - switch a timestamp at point one day into the past
  13126. - on a headline, switch to the previous TODO keyword.
  13127. - on an item, switch entire list to the previous bullet type
  13128. - on a property line, switch to the previous allowed value
  13129. - on a clocktable definition line, move time block into the past"
  13130. (interactive "P")
  13131. (cond
  13132. ((and org-support-shift-select (org-region-active-p))
  13133. (org-call-for-shift-select 'backward-char))
  13134. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13135. ((and (not (eq org-support-shift-select 'always))
  13136. (org-on-heading-p))
  13137. (org-call-with-arg 'org-todo 'left))
  13138. ((or (and org-support-shift-select
  13139. (not (eq org-support-shift-select 'always))
  13140. (org-at-item-bullet-p))
  13141. (and (not org-support-shift-select) (org-at-item-p)))
  13142. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13143. ((and (not (eq org-support-shift-select 'always))
  13144. (org-at-property-p))
  13145. (call-interactively 'org-property-previous-allowed-value))
  13146. ((org-clocktable-try-shift 'left arg))
  13147. (org-support-shift-select
  13148. (org-call-for-shift-select 'backward-char))
  13149. (t (org-shiftselect-error))))
  13150. (defun org-shiftcontrolright ()
  13151. "Switch to next TODO set."
  13152. (interactive)
  13153. (cond
  13154. ((and org-support-shift-select (org-region-active-p))
  13155. (org-call-for-shift-select 'forward-word))
  13156. ((and (not (eq org-support-shift-select 'always))
  13157. (org-on-heading-p))
  13158. (org-call-with-arg 'org-todo 'nextset))
  13159. (org-support-shift-select
  13160. (org-call-for-shift-select 'forward-word))
  13161. (t (org-shiftselect-error))))
  13162. (defun org-shiftcontrolleft ()
  13163. "Switch to previous TODO set."
  13164. (interactive)
  13165. (cond
  13166. ((and org-support-shift-select (org-region-active-p))
  13167. (org-call-for-shift-select 'backward-word))
  13168. ((and (not (eq org-support-shift-select 'always))
  13169. (org-on-heading-p))
  13170. (org-call-with-arg 'org-todo 'previousset))
  13171. (org-support-shift-select
  13172. (org-call-for-shift-select 'backward-word))
  13173. (t (org-shiftselect-error))))
  13174. (defun org-ctrl-c-ret ()
  13175. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13176. (interactive)
  13177. (cond
  13178. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13179. (t (call-interactively 'org-insert-heading))))
  13180. (defun org-copy-special ()
  13181. "Copy region in table or copy current subtree.
  13182. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13183. See the individual commands for more information."
  13184. (interactive)
  13185. (call-interactively
  13186. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13187. (defun org-cut-special ()
  13188. "Cut region in table or cut current subtree.
  13189. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13190. See the individual commands for more information."
  13191. (interactive)
  13192. (call-interactively
  13193. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13194. (defun org-paste-special (arg)
  13195. "Paste rectangular region into table, or past subtree relative to level.
  13196. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13197. See the individual commands for more information."
  13198. (interactive "P")
  13199. (if (org-at-table-p)
  13200. (org-table-paste-rectangle)
  13201. (org-paste-subtree arg)))
  13202. (defun org-edit-special ()
  13203. "Call a special editor for the stuff at point.
  13204. When at a table, call the formula editor with `org-table-edit-formulas'.
  13205. When at the first line of an src example, call `org-edit-src-code'.
  13206. When in an #+include line, visit the include file. Otherwise call
  13207. `ffap' to visit the file at point."
  13208. (interactive)
  13209. (cond
  13210. ((org-at-table-p)
  13211. (call-interactively 'org-table-edit-formulas))
  13212. ((save-excursion
  13213. (beginning-of-line 1)
  13214. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13215. (find-file (org-trim (match-string 1))))
  13216. ((org-edit-src-code))
  13217. ((org-edit-fixed-width-region))
  13218. (t (call-interactively 'ffap))))
  13219. (defun org-ctrl-c-ctrl-c (&optional arg)
  13220. "Set tags in headline, or update according to changed information at point.
  13221. This command does many different things, depending on context:
  13222. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13223. this is what we do.
  13224. - If the cursor is in a headline, prompt for tags and insert them
  13225. into the current line, aligned to `org-tags-column'. When called
  13226. with prefix arg, realign all tags in the current buffer.
  13227. - If the cursor is in one of the special #+KEYWORD lines, this
  13228. triggers scanning the buffer for these lines and updating the
  13229. information.
  13230. - If the cursor is inside a table, realign the table. This command
  13231. works even if the automatic table editor has been turned off.
  13232. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13233. the entire table.
  13234. - If the cursor is at a footnote reference or definition, jump to
  13235. the corresponding definition or references, respectively.
  13236. - If the cursor is a the beginning of a dynamic block, update it.
  13237. - If the cursor is inside a table created by the table.el package,
  13238. activate that table.
  13239. - If the current buffer is a remember buffer, close note and file
  13240. it. A prefix argument of 1 files to the default location
  13241. without further interaction. A prefix argument of 2 files to
  13242. the currently clocking task.
  13243. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13244. links in this buffer.
  13245. - If the cursor is on a numbered item in a plain list, renumber the
  13246. ordered list.
  13247. - If the cursor is on a checkbox, toggle it."
  13248. (interactive "P")
  13249. (let ((org-enable-table-editor t))
  13250. (cond
  13251. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13252. org-occur-highlights
  13253. org-latex-fragment-image-overlays)
  13254. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13255. (org-remove-occur-highlights)
  13256. (org-remove-latex-fragment-image-overlays)
  13257. (message "Temporary highlights/overlays removed from current buffer"))
  13258. ((and (local-variable-p 'org-finish-function (current-buffer))
  13259. (fboundp org-finish-function))
  13260. (funcall org-finish-function))
  13261. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13262. ((org-at-property-p)
  13263. (call-interactively 'org-property-action))
  13264. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13265. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13266. ((org-at-table.el-p)
  13267. (require 'table)
  13268. (beginning-of-line 1)
  13269. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13270. (call-interactively 'table-recognize-table))
  13271. ((org-at-table-p)
  13272. (org-table-maybe-eval-formula)
  13273. (if arg
  13274. (call-interactively 'org-table-recalculate)
  13275. (org-table-maybe-recalculate-line))
  13276. (call-interactively 'org-table-align))
  13277. ((or (org-footnote-at-reference-p)
  13278. (org-footnote-at-definition-p))
  13279. (call-interactively 'org-footnote-action))
  13280. ((org-at-item-checkbox-p)
  13281. (call-interactively 'org-toggle-checkbox))
  13282. ((org-at-item-p)
  13283. (if arg
  13284. (call-interactively 'org-toggle-checkbox)
  13285. (call-interactively 'org-maybe-renumber-ordered-list)))
  13286. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13287. ;; Dynamic block
  13288. (beginning-of-line 1)
  13289. (save-excursion (org-update-dblock)))
  13290. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13291. (cond
  13292. ((equal (match-string 1) "TBLFM")
  13293. ;; Recalculate the table before this line
  13294. (save-excursion
  13295. (beginning-of-line 1)
  13296. (skip-chars-backward " \r\n\t")
  13297. (if (org-at-table-p)
  13298. (org-call-with-arg 'org-table-recalculate t))))
  13299. (t
  13300. ; (org-set-regexps-and-options)
  13301. ; (org-restart-font-lock)
  13302. (let ((org-inhibit-startup t)) (org-mode-restart))
  13303. (message "Local setup has been refreshed"))))
  13304. (t (error "C-c C-c can do nothing useful at this location.")))))
  13305. (defun org-mode-restart ()
  13306. "Restart Org-mode, to scan again for special lines.
  13307. Also updates the keyword regular expressions."
  13308. (interactive)
  13309. (org-mode)
  13310. (message "Org-mode restarted"))
  13311. (defun org-kill-note-or-show-branches ()
  13312. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13313. (interactive)
  13314. (if (not org-finish-function)
  13315. (call-interactively 'show-branches)
  13316. (let ((org-note-abort t))
  13317. (funcall org-finish-function))))
  13318. (defun org-return (&optional indent)
  13319. "Goto next table row or insert a newline.
  13320. Calls `org-table-next-row' or `newline', depending on context.
  13321. See the individual commands for more information."
  13322. (interactive)
  13323. (cond
  13324. ((bobp) (if indent (newline-and-indent) (newline)))
  13325. ((org-at-table-p)
  13326. (org-table-justify-field-maybe)
  13327. (call-interactively 'org-table-next-row))
  13328. ((and org-return-follows-link
  13329. (eq (get-text-property (point) 'face) 'org-link))
  13330. (call-interactively 'org-open-at-point))
  13331. ((and (org-at-heading-p)
  13332. (looking-at
  13333. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13334. (org-show-entry)
  13335. (end-of-line 1)
  13336. (newline))
  13337. (t (if indent (newline-and-indent) (newline)))))
  13338. (defun org-return-indent ()
  13339. "Goto next table row or insert a newline and indent.
  13340. Calls `org-table-next-row' or `newline-and-indent', depending on
  13341. context. See the individual commands for more information."
  13342. (interactive)
  13343. (org-return t))
  13344. (defun org-ctrl-c-star ()
  13345. "Compute table, or change heading status of lines.
  13346. Calls `org-table-recalculate' or `org-toggle-heading',
  13347. depending on context."
  13348. (interactive)
  13349. (cond
  13350. ((org-at-table-p)
  13351. (call-interactively 'org-table-recalculate))
  13352. (t
  13353. ;; Convert all lines in region to list items
  13354. (call-interactively 'org-toggle-heading))))
  13355. (defun org-ctrl-c-minus ()
  13356. "Insert separator line in table or modify bullet status of line.
  13357. Also turns a plain line or a region of lines into list items.
  13358. Calls `org-table-insert-hline', `org-toggle-item', or
  13359. `org-cycle-list-bullet', depending on context."
  13360. (interactive)
  13361. (cond
  13362. ((org-at-table-p)
  13363. (call-interactively 'org-table-insert-hline))
  13364. ((org-region-active-p)
  13365. (call-interactively 'org-toggle-item))
  13366. ((org-in-item-p)
  13367. (call-interactively 'org-cycle-list-bullet))
  13368. (t
  13369. (call-interactively 'org-toggle-item))))
  13370. (defun org-toggle-item ()
  13371. "Convert headings or normal lines to items, items to normal lines.
  13372. If there is no active region, only the current line is considered.
  13373. If the first line in the region is a headline, convert all headlines to items.
  13374. If the first line in the region is an item, convert all items to normal lines.
  13375. If the first line is normal text, add an item bullet to each line."
  13376. (interactive)
  13377. (let (l2 l beg end)
  13378. (if (org-region-active-p)
  13379. (setq beg (region-beginning) end (region-end))
  13380. (setq beg (point-at-bol)
  13381. end (min (1+ (point-at-eol)) (point-max))))
  13382. (save-excursion
  13383. (goto-char end)
  13384. (setq l2 (org-current-line))
  13385. (goto-char beg)
  13386. (beginning-of-line 1)
  13387. (setq l (1- (org-current-line)))
  13388. (if (org-at-item-p)
  13389. ;; We already have items, de-itemize
  13390. (while (< (setq l (1+ l)) l2)
  13391. (when (org-at-item-p)
  13392. (goto-char (match-beginning 2))
  13393. (delete-region (match-beginning 2) (match-end 2))
  13394. (and (looking-at "[ \t]+") (replace-match "")))
  13395. (beginning-of-line 2))
  13396. (if (org-on-heading-p)
  13397. ;; Headings, convert to items
  13398. (while (< (setq l (1+ l)) l2)
  13399. (if (looking-at org-outline-regexp)
  13400. (replace-match "- " t t))
  13401. (beginning-of-line 2))
  13402. ;; normal lines, turn them into items
  13403. (while (< (setq l (1+ l)) l2)
  13404. (unless (org-at-item-p)
  13405. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13406. (replace-match "\\1- \\2")))
  13407. (beginning-of-line 2)))))))
  13408. (defun org-toggle-heading (&optional nstars)
  13409. "Convert headings to normal text, or items or text to headings.
  13410. If there is no active region, only the current line is considered.
  13411. If the first line is a heading, remove the stars from all headlines
  13412. in the region.
  13413. If the first line is a plain list item, turn all plain list items into
  13414. headings.
  13415. If the first line is a normal line, turn each and every line in the region
  13416. into a heading.
  13417. When converting a line into a heading, the number of stars is chosen
  13418. such that the lines become children of the current entry. However, when
  13419. a prefix argument is given, its value determines the number of stars to add."
  13420. (interactive "P")
  13421. (let (l2 l itemp beg end)
  13422. (if (org-region-active-p)
  13423. (setq beg (region-beginning) end (region-end))
  13424. (setq beg (point-at-bol)
  13425. end (min (1+ (point-at-eol)) (point-max))))
  13426. (save-excursion
  13427. (goto-char end)
  13428. (setq l2 (org-current-line))
  13429. (goto-char beg)
  13430. (beginning-of-line 1)
  13431. (setq l (1- (org-current-line)))
  13432. (if (org-on-heading-p)
  13433. ;; We already have headlines, de-star them
  13434. (while (< (setq l (1+ l)) l2)
  13435. (when (org-on-heading-p t)
  13436. (and (looking-at outline-regexp) (replace-match "")))
  13437. (beginning-of-line 2))
  13438. (setq itemp (org-at-item-p))
  13439. (let* ((stars
  13440. (if nstars
  13441. (make-string (prefix-numeric-value current-prefix-arg)
  13442. ?*)
  13443. (save-excursion
  13444. (re-search-backward org-complex-heading-regexp nil t)
  13445. (or (match-string 1) ""))))
  13446. (add-stars (cond (nstars "")
  13447. ((equal stars "") "*")
  13448. (org-odd-levels-only "**")
  13449. (t "*")))
  13450. (rpl (concat stars add-stars " ")))
  13451. (while (< (setq l (1+ l)) l2)
  13452. (if itemp
  13453. (and (org-at-item-p) (replace-match rpl t t))
  13454. (unless (org-on-heading-p)
  13455. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13456. (replace-match (concat rpl (match-string 2))))))
  13457. (beginning-of-line 2)))))))
  13458. (defun org-meta-return (&optional arg)
  13459. "Insert a new heading or wrap a region in a table.
  13460. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13461. See the individual commands for more information."
  13462. (interactive "P")
  13463. (cond
  13464. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13465. ((org-at-table-p)
  13466. (call-interactively 'org-table-wrap-region))
  13467. (t (call-interactively 'org-insert-heading))))
  13468. ;;; Menu entries
  13469. ;; Define the Org-mode menus
  13470. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13471. '("Tbl"
  13472. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13473. ["Next Field" org-cycle (org-at-table-p)]
  13474. ["Previous Field" org-shifttab (org-at-table-p)]
  13475. ["Next Row" org-return (org-at-table-p)]
  13476. "--"
  13477. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13478. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13479. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13480. "--"
  13481. ("Column"
  13482. ["Move Column Left" org-metaleft (org-at-table-p)]
  13483. ["Move Column Right" org-metaright (org-at-table-p)]
  13484. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13485. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13486. ("Row"
  13487. ["Move Row Up" org-metaup (org-at-table-p)]
  13488. ["Move Row Down" org-metadown (org-at-table-p)]
  13489. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13490. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13491. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13492. "--"
  13493. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13494. ("Rectangle"
  13495. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13496. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13497. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13498. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13499. "--"
  13500. ("Calculate"
  13501. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13502. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13503. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13504. "--"
  13505. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13506. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13507. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13508. "--"
  13509. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13510. "--"
  13511. ["Sum Column/Rectangle" org-table-sum
  13512. (or (org-at-table-p) (org-region-active-p))]
  13513. ["Which Column?" org-table-current-column (org-at-table-p)])
  13514. ["Debug Formulas"
  13515. org-table-toggle-formula-debugger
  13516. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13517. ["Show Col/Row Numbers"
  13518. org-table-toggle-coordinate-overlays
  13519. :style toggle
  13520. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13521. "--"
  13522. ["Create" org-table-create (and (not (org-at-table-p))
  13523. org-enable-table-editor)]
  13524. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13525. ["Import from File" org-table-import (not (org-at-table-p))]
  13526. ["Export to File" org-table-export (org-at-table-p)]
  13527. "--"
  13528. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13529. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13530. '("Org"
  13531. ("Show/Hide"
  13532. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13533. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13534. ["Sparse Tree..." org-sparse-tree t]
  13535. ["Reveal Context" org-reveal t]
  13536. ["Show All" show-all t]
  13537. "--"
  13538. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13539. "--"
  13540. ["New Heading" org-insert-heading t]
  13541. ("Navigate Headings"
  13542. ["Up" outline-up-heading t]
  13543. ["Next" outline-next-visible-heading t]
  13544. ["Previous" outline-previous-visible-heading t]
  13545. ["Next Same Level" outline-forward-same-level t]
  13546. ["Previous Same Level" outline-backward-same-level t]
  13547. "--"
  13548. ["Jump" org-goto t])
  13549. ("Edit Structure"
  13550. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13551. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13552. "--"
  13553. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13554. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13555. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13556. "--"
  13557. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13558. "--"
  13559. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13560. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13561. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13562. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13563. "--"
  13564. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13565. "--"
  13566. ["Convert to odd levels" org-convert-to-odd-levels t]
  13567. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13568. ("Editing"
  13569. ["Emphasis..." org-emphasize t]
  13570. ["Edit Source Example" org-edit-special t]
  13571. "--"
  13572. ["Footnote new/jump" org-footnote-action t]
  13573. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13574. ("Archive"
  13575. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13576. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13577. ; :active t :keys "C-u C-c C-x C-a"]
  13578. ["Sparse trees open ARCHIVE trees"
  13579. (setq org-sparse-tree-open-archived-trees
  13580. (not org-sparse-tree-open-archived-trees))
  13581. :style toggle :selected org-sparse-tree-open-archived-trees]
  13582. ["Cycling opens ARCHIVE trees"
  13583. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13584. :style toggle :selected org-cycle-open-archived-trees]
  13585. "--"
  13586. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13587. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13588. ; ["Check and Move Children" (org-archive-subtree '(4))
  13589. ; :active t :keys "C-u C-c C-x C-s"]
  13590. )
  13591. "--"
  13592. ("Hyperlinks"
  13593. ["Store Link (Global)" org-store-link t]
  13594. ["Insert Link" org-insert-link t]
  13595. ["Follow Link" org-open-at-point t]
  13596. "--"
  13597. ["Next link" org-next-link t]
  13598. ["Previous link" org-previous-link t]
  13599. "--"
  13600. ["Descriptive Links"
  13601. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13602. :style radio
  13603. :selected (member '(org-link) buffer-invisibility-spec)]
  13604. ["Literal Links"
  13605. (progn
  13606. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13607. :style radio
  13608. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13609. "--"
  13610. ("TODO Lists"
  13611. ["TODO/DONE/-" org-todo t]
  13612. ("Select keyword"
  13613. ["Next keyword" org-shiftright (org-on-heading-p)]
  13614. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13615. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13616. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13617. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13618. ["Show TODO Tree" org-show-todo-tree t]
  13619. ["Global TODO list" org-todo-list t]
  13620. "--"
  13621. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13622. :selected org-enforce-todo-dependencies :style toggle :active t]
  13623. "Settings for tree at point"
  13624. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13625. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13626. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13627. ["Do Children parallel" org-toggle-ordered-property :style radio
  13628. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13629. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13630. "--"
  13631. ["Set Priority" org-priority t]
  13632. ["Priority Up" org-shiftup t]
  13633. ["Priority Down" org-shiftdown t]
  13634. "--"
  13635. ["Get news from all feeds" org-feed-update-all t]
  13636. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13637. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13638. ("TAGS and Properties"
  13639. ["Set Tags" org-set-tags-command t]
  13640. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13641. "--"
  13642. ["Set property" org-set-property t]
  13643. ["Column view of properties" org-columns t]
  13644. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13645. ("Dates and Scheduling"
  13646. ["Timestamp" org-time-stamp t]
  13647. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13648. ("Change Date"
  13649. ["1 Day Later" org-shiftright t]
  13650. ["1 Day Earlier" org-shiftleft t]
  13651. ["1 ... Later" org-shiftup t]
  13652. ["1 ... Earlier" org-shiftdown t])
  13653. ["Compute Time Range" org-evaluate-time-range t]
  13654. ["Schedule Item" org-schedule t]
  13655. ["Deadline" org-deadline t]
  13656. "--"
  13657. ["Custom time format" org-toggle-time-stamp-overlays
  13658. :style radio :selected org-display-custom-times]
  13659. "--"
  13660. ["Goto Calendar" org-goto-calendar t]
  13661. ["Date from Calendar" org-date-from-calendar t]
  13662. "--"
  13663. ["Start/Restart Timer" org-timer-start t]
  13664. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13665. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13666. ["Insert Timer String" org-timer t]
  13667. ["Insert Timer Item" org-timer-item t])
  13668. ("Logging work"
  13669. ["Clock in" org-clock-in t]
  13670. ["Clock out" org-clock-out t]
  13671. ["Clock cancel" org-clock-cancel t]
  13672. ["Goto running clock" org-clock-goto t]
  13673. ["Display times" org-clock-display t]
  13674. ["Create clock table" org-clock-report t]
  13675. "--"
  13676. ["Record DONE time"
  13677. (progn (setq org-log-done (not org-log-done))
  13678. (message "Switching to %s will %s record a timestamp"
  13679. (car org-done-keywords)
  13680. (if org-log-done "automatically" "not")))
  13681. :style toggle :selected org-log-done])
  13682. "--"
  13683. ["Agenda Command..." org-agenda t]
  13684. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13685. ("File List for Agenda")
  13686. ("Special views current file"
  13687. ["TODO Tree" org-show-todo-tree t]
  13688. ["Check Deadlines" org-check-deadlines t]
  13689. ["Timeline" org-timeline t]
  13690. ["Tags/Property tree" org-match-sparse-tree t])
  13691. "--"
  13692. ["Export/Publish..." org-export t]
  13693. ("LaTeX"
  13694. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13695. :selected org-cdlatex-mode]
  13696. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13697. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13698. ["Modify math symbol" org-cdlatex-math-modify
  13699. (org-inside-LaTeX-fragment-p)]
  13700. ["Insert citation" org-reftex-citation t]
  13701. "--"
  13702. ["Export LaTeX fragments as images"
  13703. (if (featurep 'org-exp)
  13704. (setq org-export-with-LaTeX-fragments
  13705. (not org-export-with-LaTeX-fragments))
  13706. (require 'org-exp))
  13707. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13708. org-export-with-LaTeX-fragments)])
  13709. "--"
  13710. ("Documentation"
  13711. ["Show Version" org-version t]
  13712. ["Info Documentation" org-info t])
  13713. ("Customize"
  13714. ["Browse Org Group" org-customize t]
  13715. "--"
  13716. ["Expand This Menu" org-create-customize-menu
  13717. (fboundp 'customize-menu-create)])
  13718. "--"
  13719. ("Refresh/Reload"
  13720. ["Refresh setup current buffer" org-mode-restart t]
  13721. ["Reload Org (after update)" org-reload t]
  13722. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13723. ))
  13724. (defun org-info (&optional node)
  13725. "Read documentation for Org-mode in the info system.
  13726. With optional NODE, go directly to that node."
  13727. (interactive)
  13728. (info (format "(org)%s" (or node ""))))
  13729. (defun org-install-agenda-files-menu ()
  13730. (let ((bl (buffer-list)))
  13731. (save-excursion
  13732. (while bl
  13733. (set-buffer (pop bl))
  13734. (if (org-mode-p) (setq bl nil)))
  13735. (when (org-mode-p)
  13736. (easy-menu-change
  13737. '("Org") "File List for Agenda"
  13738. (append
  13739. (list
  13740. ["Edit File List" (org-edit-agenda-file-list) t]
  13741. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13742. ["Remove Current File from List" org-remove-file t]
  13743. ["Cycle through agenda files" org-cycle-agenda-files t]
  13744. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13745. "--")
  13746. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13747. ;;;; Documentation
  13748. ;;;###autoload
  13749. (defun org-require-autoloaded-modules ()
  13750. (interactive)
  13751. (mapc 'require
  13752. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13753. org-docbook org-exp org-html org-icalendar
  13754. org-id org-latex
  13755. org-publish org-remember org-table
  13756. org-timer org-xoxo)))
  13757. ;;;###autoload
  13758. (defun org-reload (&optional uncompiled)
  13759. "Reload all org lisp files.
  13760. With prefix arg UNCOMPILED, load the uncompiled versions."
  13761. (interactive "P")
  13762. (require 'find-func)
  13763. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13764. (dir-org (file-name-directory (org-find-library-name "org")))
  13765. (dir-org-contrib (ignore-errors
  13766. (file-name-directory
  13767. (org-find-library-name "org-contribdir"))))
  13768. (files
  13769. (append (directory-files dir-org t file-re)
  13770. (and dir-org-contrib
  13771. (directory-files dir-org-contrib t file-re))))
  13772. (remove-re (concat (if (featurep 'xemacs)
  13773. "org-colview" "org-colview-xemacs")
  13774. "\\'")))
  13775. (setq files (mapcar 'file-name-sans-extension files))
  13776. (setq files (mapcar
  13777. (lambda (x) (if (string-match remove-re x) nil x))
  13778. files))
  13779. (setq files (delq nil files))
  13780. (mapc
  13781. (lambda (f)
  13782. (when (featurep (intern (file-name-nondirectory f)))
  13783. (if (and (not uncompiled)
  13784. (file-exists-p (concat f ".elc")))
  13785. (load (concat f ".elc") nil nil t)
  13786. (load (concat f ".el") nil nil t))))
  13787. files))
  13788. (org-version))
  13789. ;;;###autoload
  13790. (defun org-customize ()
  13791. "Call the customize function with org as argument."
  13792. (interactive)
  13793. (org-load-modules-maybe)
  13794. (org-require-autoloaded-modules)
  13795. (customize-browse 'org))
  13796. (defun org-create-customize-menu ()
  13797. "Create a full customization menu for Org-mode, insert it into the menu."
  13798. (interactive)
  13799. (org-load-modules-maybe)
  13800. (org-require-autoloaded-modules)
  13801. (if (fboundp 'customize-menu-create)
  13802. (progn
  13803. (easy-menu-change
  13804. '("Org") "Customize"
  13805. `(["Browse Org group" org-customize t]
  13806. "--"
  13807. ,(customize-menu-create 'org)
  13808. ["Set" Custom-set t]
  13809. ["Save" Custom-save t]
  13810. ["Reset to Current" Custom-reset-current t]
  13811. ["Reset to Saved" Custom-reset-saved t]
  13812. ["Reset to Standard Settings" Custom-reset-standard t]))
  13813. (message "\"Org\"-menu now contains full customization menu"))
  13814. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13815. ;;;; Miscellaneous stuff
  13816. ;;; Generally useful functions
  13817. (defun org-find-text-property-in-string (prop s)
  13818. "Return the first non-nil value of property PROP in string S."
  13819. (or (get-text-property 0 prop s)
  13820. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13821. prop s)))
  13822. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13823. "Display the given MESSAGE as a warning."
  13824. (if (fboundp 'display-warning)
  13825. (display-warning 'org message
  13826. (if (featurep 'xemacs)
  13827. 'warning
  13828. :warning))
  13829. (let ((buf (get-buffer-create "*Org warnings*")))
  13830. (with-current-buffer buf
  13831. (goto-char (point-max))
  13832. (insert "Warning (Org): " message)
  13833. (unless (bolp)
  13834. (newline)))
  13835. (display-buffer buf)
  13836. (sit-for 0))))
  13837. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13838. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13839. (if (and marker (marker-buffer marker)
  13840. (buffer-live-p (marker-buffer marker)))
  13841. (progn
  13842. (switch-to-buffer (marker-buffer marker))
  13843. (if (or (> marker (point-max)) (< marker (point-min)))
  13844. (widen))
  13845. (goto-char marker)
  13846. (org-show-context 'org-goto))
  13847. (if bookmark
  13848. (bookmark-jump bookmark)
  13849. (error "Cannot find location"))))
  13850. (defun org-quote-csv-field (s)
  13851. "Quote field for inclusion in CSV material."
  13852. (if (string-match "[\",]" s)
  13853. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13854. s))
  13855. (defun org-plist-delete (plist property)
  13856. "Delete PROPERTY from PLIST.
  13857. This is in contrast to merely setting it to 0."
  13858. (let (p)
  13859. (while plist
  13860. (if (not (eq property (car plist)))
  13861. (setq p (plist-put p (car plist) (nth 1 plist))))
  13862. (setq plist (cddr plist)))
  13863. p))
  13864. (defun org-force-self-insert (N)
  13865. "Needed to enforce self-insert under remapping."
  13866. (interactive "p")
  13867. (self-insert-command N))
  13868. (defun org-string-width (s)
  13869. "Compute width of string, ignoring invisible characters.
  13870. This ignores character with invisibility property `org-link', and also
  13871. characters with property `org-cwidth', because these will become invisible
  13872. upon the next fontification round."
  13873. (let (b l)
  13874. (when (or (eq t buffer-invisibility-spec)
  13875. (assq 'org-link buffer-invisibility-spec))
  13876. (while (setq b (text-property-any 0 (length s)
  13877. 'invisible 'org-link s))
  13878. (setq s (concat (substring s 0 b)
  13879. (substring s (or (next-single-property-change
  13880. b 'invisible s) (length s)))))))
  13881. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13882. (setq s (concat (substring s 0 b)
  13883. (substring s (or (next-single-property-change
  13884. b 'org-cwidth s) (length s))))))
  13885. (setq l (string-width s) b -1)
  13886. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13887. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13888. l))
  13889. (defun org-get-indentation (&optional line)
  13890. "Get the indentation of the current line, interpreting tabs.
  13891. When LINE is given, assume it represents a line and compute its indentation."
  13892. (if line
  13893. (if (string-match "^ *" (org-remove-tabs line))
  13894. (match-end 0))
  13895. (save-excursion
  13896. (beginning-of-line 1)
  13897. (skip-chars-forward " \t")
  13898. (current-column))))
  13899. (defun org-remove-tabs (s &optional width)
  13900. "Replace tabulators in S with spaces.
  13901. Assumes that s is a single line, starting in column 0."
  13902. (setq width (or width tab-width))
  13903. (while (string-match "\t" s)
  13904. (setq s (replace-match
  13905. (make-string
  13906. (- (* width (/ (+ (match-beginning 0) width) width))
  13907. (match-beginning 0)) ?\ )
  13908. t t s)))
  13909. s)
  13910. (defun org-fix-indentation (line ind)
  13911. "Fix indentation in LINE.
  13912. IND is a cons cell with target and minimum indentation.
  13913. If the current indentation in LINE is smaller than the minimum,
  13914. leave it alone. If it is larger than ind, set it to the target."
  13915. (let* ((l (org-remove-tabs line))
  13916. (i (org-get-indentation l))
  13917. (i1 (car ind)) (i2 (cdr ind)))
  13918. (if (>= i i2) (setq l (substring line i2)))
  13919. (if (> i1 0)
  13920. (concat (make-string i1 ?\ ) l)
  13921. l)))
  13922. (defun org-base-buffer (buffer)
  13923. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13924. (if (not buffer)
  13925. buffer
  13926. (or (buffer-base-buffer buffer)
  13927. buffer)))
  13928. (defun org-trim (s)
  13929. "Remove whitespace at beginning and end of string."
  13930. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13931. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13932. s)
  13933. (defun org-wrap (string &optional width lines)
  13934. "Wrap string to either a number of lines, or a width in characters.
  13935. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13936. that costs. If there is a word longer than WIDTH, the text is actually
  13937. wrapped to the length of that word.
  13938. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13939. many lines, whatever width that takes.
  13940. The return value is a list of lines, without newlines at the end."
  13941. (let* ((words (org-split-string string "[ \t\n]+"))
  13942. (maxword (apply 'max (mapcar 'org-string-width words)))
  13943. w ll)
  13944. (cond (width
  13945. (org-do-wrap words (max maxword width)))
  13946. (lines
  13947. (setq w maxword)
  13948. (setq ll (org-do-wrap words maxword))
  13949. (if (<= (length ll) lines)
  13950. ll
  13951. (setq ll words)
  13952. (while (> (length ll) lines)
  13953. (setq w (1+ w))
  13954. (setq ll (org-do-wrap words w)))
  13955. ll))
  13956. (t (error "Cannot wrap this")))))
  13957. (defun org-do-wrap (words width)
  13958. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13959. (let (lines line)
  13960. (while words
  13961. (setq line (pop words))
  13962. (while (and words (< (+ (length line) (length (car words))) width))
  13963. (setq line (concat line " " (pop words))))
  13964. (setq lines (push line lines)))
  13965. (nreverse lines)))
  13966. (defun org-split-string (string &optional separators)
  13967. "Splits STRING into substrings at SEPARATORS.
  13968. No empty strings are returned if there are matches at the beginning
  13969. and end of string."
  13970. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13971. (start 0)
  13972. notfirst
  13973. (list nil))
  13974. (while (and (string-match rexp string
  13975. (if (and notfirst
  13976. (= start (match-beginning 0))
  13977. (< start (length string)))
  13978. (1+ start) start))
  13979. (< (match-beginning 0) (length string)))
  13980. (setq notfirst t)
  13981. (or (eq (match-beginning 0) 0)
  13982. (and (eq (match-beginning 0) (match-end 0))
  13983. (eq (match-beginning 0) start))
  13984. (setq list
  13985. (cons (substring string start (match-beginning 0))
  13986. list)))
  13987. (setq start (match-end 0)))
  13988. (or (eq start (length string))
  13989. (setq list
  13990. (cons (substring string start)
  13991. list)))
  13992. (nreverse list)))
  13993. (defun org-uuidgen-p (s)
  13994. "Is S an ID created by UUIDGEN?"
  13995. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13996. (defun org-context ()
  13997. "Return a list of contexts of the current cursor position.
  13998. If several contexts apply, all are returned.
  13999. Each context entry is a list with a symbol naming the context, and
  14000. two positions indicating start and end of the context. Possible
  14001. contexts are:
  14002. :headline anywhere in a headline
  14003. :headline-stars on the leading stars in a headline
  14004. :todo-keyword on a TODO keyword (including DONE) in a headline
  14005. :tags on the TAGS in a headline
  14006. :priority on the priority cookie in a headline
  14007. :item on the first line of a plain list item
  14008. :item-bullet on the bullet/number of a plain list item
  14009. :checkbox on the checkbox in a plain list item
  14010. :table in an org-mode table
  14011. :table-special on a special filed in a table
  14012. :table-table in a table.el table
  14013. :link on a hyperlink
  14014. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14015. :target on a <<target>>
  14016. :radio-target on a <<<radio-target>>>
  14017. :latex-fragment on a LaTeX fragment
  14018. :latex-preview on a LaTeX fragment with overlayed preview image
  14019. This function expects the position to be visible because it uses font-lock
  14020. faces as a help to recognize the following contexts: :table-special, :link,
  14021. and :keyword."
  14022. (let* ((f (get-text-property (point) 'face))
  14023. (faces (if (listp f) f (list f)))
  14024. (p (point)) clist o)
  14025. ;; First the large context
  14026. (cond
  14027. ((org-on-heading-p t)
  14028. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14029. (when (progn
  14030. (beginning-of-line 1)
  14031. (looking-at org-todo-line-tags-regexp))
  14032. (push (org-point-in-group p 1 :headline-stars) clist)
  14033. (push (org-point-in-group p 2 :todo-keyword) clist)
  14034. (push (org-point-in-group p 4 :tags) clist))
  14035. (goto-char p)
  14036. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14037. (if (looking-at "\\[#[A-Z0-9]\\]")
  14038. (push (org-point-in-group p 0 :priority) clist)))
  14039. ((org-at-item-p)
  14040. (push (org-point-in-group p 2 :item-bullet) clist)
  14041. (push (list :item (point-at-bol)
  14042. (save-excursion (org-end-of-item) (point)))
  14043. clist)
  14044. (and (org-at-item-checkbox-p)
  14045. (push (org-point-in-group p 0 :checkbox) clist)))
  14046. ((org-at-table-p)
  14047. (push (list :table (org-table-begin) (org-table-end)) clist)
  14048. (if (memq 'org-formula faces)
  14049. (push (list :table-special
  14050. (previous-single-property-change p 'face)
  14051. (next-single-property-change p 'face)) clist)))
  14052. ((org-at-table-p 'any)
  14053. (push (list :table-table) clist)))
  14054. (goto-char p)
  14055. ;; Now the small context
  14056. (cond
  14057. ((org-at-timestamp-p)
  14058. (push (org-point-in-group p 0 :timestamp) clist))
  14059. ((memq 'org-link faces)
  14060. (push (list :link
  14061. (previous-single-property-change p 'face)
  14062. (next-single-property-change p 'face)) clist))
  14063. ((memq 'org-special-keyword faces)
  14064. (push (list :keyword
  14065. (previous-single-property-change p 'face)
  14066. (next-single-property-change p 'face)) clist))
  14067. ((org-on-target-p)
  14068. (push (org-point-in-group p 0 :target) clist)
  14069. (goto-char (1- (match-beginning 0)))
  14070. (if (looking-at org-radio-target-regexp)
  14071. (push (org-point-in-group p 0 :radio-target) clist))
  14072. (goto-char p))
  14073. ((setq o (car (delq nil
  14074. (mapcar
  14075. (lambda (x)
  14076. (if (memq x org-latex-fragment-image-overlays) x))
  14077. (org-overlays-at (point))))))
  14078. (push (list :latex-fragment
  14079. (org-overlay-start o) (org-overlay-end o)) clist)
  14080. (push (list :latex-preview
  14081. (org-overlay-start o) (org-overlay-end o)) clist))
  14082. ((org-inside-LaTeX-fragment-p)
  14083. ;; FIXME: positions wrong.
  14084. (push (list :latex-fragment (point) (point)) clist)))
  14085. (setq clist (nreverse (delq nil clist)))
  14086. clist))
  14087. ;; FIXME: Compare with at-regexp-p Do we need both?
  14088. (defun org-in-regexp (re &optional nlines visually)
  14089. "Check if point is inside a match of regexp.
  14090. Normally only the current line is checked, but you can include NLINES extra
  14091. lines both before and after point into the search.
  14092. If VISUALLY is set, require that the cursor is not after the match but
  14093. really on, so that the block visually is on the match."
  14094. (catch 'exit
  14095. (let ((pos (point))
  14096. (eol (point-at-eol (+ 1 (or nlines 0))))
  14097. (inc (if visually 1 0)))
  14098. (save-excursion
  14099. (beginning-of-line (- 1 (or nlines 0)))
  14100. (while (re-search-forward re eol t)
  14101. (if (and (<= (match-beginning 0) pos)
  14102. (>= (+ inc (match-end 0)) pos))
  14103. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14104. (defun org-at-regexp-p (regexp)
  14105. "Is point inside a match of REGEXP in the current line?"
  14106. (catch 'exit
  14107. (save-excursion
  14108. (let ((pos (point)) (end (point-at-eol)))
  14109. (beginning-of-line 1)
  14110. (while (re-search-forward regexp end t)
  14111. (if (and (<= (match-beginning 0) pos)
  14112. (>= (match-end 0) pos))
  14113. (throw 'exit t)))
  14114. nil))))
  14115. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14116. "Call `multi-occur' with buffers for all agenda files."
  14117. (interactive "sOrg-files matching: \np")
  14118. (let* ((files (org-agenda-files))
  14119. (tnames (mapcar 'file-truename files))
  14120. (extra org-agenda-text-search-extra-files)
  14121. f)
  14122. (when (eq (car extra) 'agenda-archives)
  14123. (setq extra (cdr extra))
  14124. (setq files (org-add-archive-files files)))
  14125. (while (setq f (pop extra))
  14126. (unless (member (file-truename f) tnames)
  14127. (add-to-list 'files f 'append)
  14128. (add-to-list 'tnames (file-truename f) 'append)))
  14129. (multi-occur
  14130. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14131. regexp)))
  14132. (if (boundp 'occur-mode-find-occurrence-hook)
  14133. ;; Emacs 23
  14134. (add-hook 'occur-mode-find-occurrence-hook
  14135. (lambda ()
  14136. (when (org-mode-p)
  14137. (org-reveal))))
  14138. ;; Emacs 22
  14139. (defadvice occur-mode-goto-occurrence
  14140. (after org-occur-reveal activate)
  14141. (and (org-mode-p) (org-reveal)))
  14142. (defadvice occur-mode-goto-occurrence-other-window
  14143. (after org-occur-reveal activate)
  14144. (and (org-mode-p) (org-reveal)))
  14145. (defadvice occur-mode-display-occurrence
  14146. (after org-occur-reveal activate)
  14147. (when (org-mode-p)
  14148. (let ((pos (occur-mode-find-occurrence)))
  14149. (with-current-buffer (marker-buffer pos)
  14150. (save-excursion
  14151. (goto-char pos)
  14152. (org-reveal)))))))
  14153. (defun org-uniquify (list)
  14154. "Remove duplicate elements from LIST."
  14155. (let (res)
  14156. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14157. res))
  14158. (defun org-delete-all (elts list)
  14159. "Remove all elements in ELTS from LIST."
  14160. (while elts
  14161. (setq list (delete (pop elts) list)))
  14162. list)
  14163. (defun org-back-over-empty-lines ()
  14164. "Move backwards over whitespace, to the beginning of the first empty line.
  14165. Returns the number of empty lines passed."
  14166. (let ((pos (point)))
  14167. (skip-chars-backward " \t\n\r")
  14168. (beginning-of-line 2)
  14169. (goto-char (min (point) pos))
  14170. (count-lines (point) pos)))
  14171. (defun org-skip-whitespace ()
  14172. (skip-chars-forward " \t\n\r"))
  14173. (defun org-point-in-group (point group &optional context)
  14174. "Check if POINT is in match-group GROUP.
  14175. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14176. match. If the match group does ot exist or point is not inside it,
  14177. return nil."
  14178. (and (match-beginning group)
  14179. (>= point (match-beginning group))
  14180. (<= point (match-end group))
  14181. (if context
  14182. (list context (match-beginning group) (match-end group))
  14183. t)))
  14184. (defun org-switch-to-buffer-other-window (&rest args)
  14185. "Switch to buffer in a second window on the current frame.
  14186. In particular, do not allow pop-up frames."
  14187. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14188. special-display-function)
  14189. (apply 'switch-to-buffer-other-window args)))
  14190. (defun org-combine-plists (&rest plists)
  14191. "Create a single property list from all plists in PLISTS.
  14192. The process starts by copying the first list, and then setting properties
  14193. from the other lists. Settings in the last list are the most significant
  14194. ones and overrule settings in the other lists."
  14195. (let ((rtn (copy-sequence (pop plists)))
  14196. p v ls)
  14197. (while plists
  14198. (setq ls (pop plists))
  14199. (while ls
  14200. (setq p (pop ls) v (pop ls))
  14201. (setq rtn (plist-put rtn p v))))
  14202. rtn))
  14203. (defun org-move-line-down (arg)
  14204. "Move the current line down. With prefix argument, move it past ARG lines."
  14205. (interactive "p")
  14206. (let ((col (current-column))
  14207. beg end pos)
  14208. (beginning-of-line 1) (setq beg (point))
  14209. (beginning-of-line 2) (setq end (point))
  14210. (beginning-of-line (+ 1 arg))
  14211. (setq pos (move-marker (make-marker) (point)))
  14212. (insert (delete-and-extract-region beg end))
  14213. (goto-char pos)
  14214. (org-move-to-column col)))
  14215. (defun org-move-line-up (arg)
  14216. "Move the current line up. With prefix argument, move it past ARG lines."
  14217. (interactive "p")
  14218. (let ((col (current-column))
  14219. beg end pos)
  14220. (beginning-of-line 1) (setq beg (point))
  14221. (beginning-of-line 2) (setq end (point))
  14222. (beginning-of-line (- arg))
  14223. (setq pos (move-marker (make-marker) (point)))
  14224. (insert (delete-and-extract-region beg end))
  14225. (goto-char pos)
  14226. (org-move-to-column col)))
  14227. (defun org-replace-escapes (string table)
  14228. "Replace %-escapes in STRING with values in TABLE.
  14229. TABLE is an association list with keys like \"%a\" and string values.
  14230. The sequences in STRING may contain normal field width and padding information,
  14231. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14232. so values can contain further %-escapes if they are define later in TABLE."
  14233. (let ((case-fold-search nil)
  14234. e re rpl)
  14235. (while (setq e (pop table))
  14236. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14237. (while (string-match re string)
  14238. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14239. (cdr e)))
  14240. (setq string (replace-match rpl t t string))))
  14241. string))
  14242. (defun org-sublist (list start end)
  14243. "Return a section of LIST, from START to END.
  14244. Counting starts at 1."
  14245. (let (rtn (c start))
  14246. (setq list (nthcdr (1- start) list))
  14247. (while (and list (<= c end))
  14248. (push (pop list) rtn)
  14249. (setq c (1+ c)))
  14250. (nreverse rtn)))
  14251. (defun org-find-base-buffer-visiting (file)
  14252. "Like `find-buffer-visiting' but always return the base buffer and
  14253. not an indirect buffer."
  14254. (let ((buf (or (get-file-buffer file)
  14255. (find-buffer-visiting file))))
  14256. (if buf
  14257. (or (buffer-base-buffer buf) buf)
  14258. nil)))
  14259. (defun org-image-file-name-regexp (&optional extensions)
  14260. "Return regexp matching the file names of images.
  14261. If EXTENSIONS is given, only match these."
  14262. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14263. (image-file-name-regexp)
  14264. (let ((image-file-name-extensions
  14265. (or extensions
  14266. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14267. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14268. (concat "\\."
  14269. (regexp-opt (nconc (mapcar 'upcase
  14270. image-file-name-extensions)
  14271. image-file-name-extensions)
  14272. t)
  14273. "\\'"))))
  14274. (defun org-file-image-p (file &optional extensions)
  14275. "Return non-nil if FILE is an image."
  14276. (save-match-data
  14277. (string-match (org-image-file-name-regexp extensions) file)))
  14278. (defun org-get-cursor-date ()
  14279. "Return the date at cursor in as a time.
  14280. This works in the calendar and in the agenda, anywhere else it just
  14281. returns the current time."
  14282. (let (date day defd)
  14283. (cond
  14284. ((eq major-mode 'calendar-mode)
  14285. (setq date (calendar-cursor-to-date)
  14286. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14287. ((eq major-mode 'org-agenda-mode)
  14288. (setq day (get-text-property (point) 'day))
  14289. (if day
  14290. (setq date (calendar-gregorian-from-absolute day)
  14291. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14292. (nth 2 date))))))
  14293. (or defd (current-time))))
  14294. (defvar org-agenda-action-marker (make-marker)
  14295. "Marker pointing to the entry for the next agenda action.")
  14296. (defun org-mark-entry-for-agenda-action ()
  14297. "Mark the current entry as target of an agenda action.
  14298. Agenda actions are actions executed from the agenda with the key `k',
  14299. which make use of the date at the cursor."
  14300. (interactive)
  14301. (move-marker org-agenda-action-marker
  14302. (save-excursion (org-back-to-heading t) (point))
  14303. (current-buffer))
  14304. (message
  14305. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14306. ;;; Paragraph filling stuff.
  14307. ;; We want this to be just right, so use the full arsenal.
  14308. (defun org-indent-line-function ()
  14309. "Indent line like previous, but further if previous was headline or item."
  14310. (interactive)
  14311. (let* ((pos (point))
  14312. (itemp (org-at-item-p))
  14313. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14314. column bpos bcol tpos tcol bullet btype bullet-type)
  14315. ;; Find the previous relevant line
  14316. (beginning-of-line 1)
  14317. (cond
  14318. ((looking-at "#") (setq column 0))
  14319. ((looking-at "\\*+ ") (setq column 0))
  14320. ((and (looking-at "[ \t]*:END:")
  14321. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14322. (save-excursion
  14323. (goto-char (1- (match-beginning 1)))
  14324. (setq column (current-column))))
  14325. (t
  14326. (beginning-of-line 0)
  14327. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14328. (not (looking-at "[ \t]*:END:"))
  14329. (not (looking-at org-drawer-regexp)))
  14330. (beginning-of-line 0))
  14331. (cond
  14332. ((looking-at "\\*+[ \t]+")
  14333. (if (not org-adapt-indentation)
  14334. (setq column 0)
  14335. (goto-char (match-end 0))
  14336. (setq column (current-column))))
  14337. ((looking-at org-drawer-regexp)
  14338. (goto-char (1- (match-beginning 1)))
  14339. (setq column (current-column)))
  14340. ((looking-at "\\([ \t]*\\):END:")
  14341. (goto-char (match-end 1))
  14342. (setq column (current-column)))
  14343. ((org-in-item-p)
  14344. (org-beginning-of-item)
  14345. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14346. (setq bpos (match-beginning 1) tpos (match-end 0)
  14347. bcol (progn (goto-char bpos) (current-column))
  14348. tcol (progn (goto-char tpos) (current-column))
  14349. bullet (match-string 1)
  14350. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14351. (if (> tcol (+ bcol org-description-max-indent))
  14352. (setq tcol (+ bcol 5)))
  14353. (if (not itemp)
  14354. (setq column tcol)
  14355. (goto-char pos)
  14356. (beginning-of-line 1)
  14357. (if (looking-at "\\S-")
  14358. (progn
  14359. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14360. (setq bullet (match-string 1)
  14361. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14362. (setq column (if (equal btype bullet-type) bcol tcol)))
  14363. (setq column (org-get-indentation)))))
  14364. (t (setq column (org-get-indentation))))))
  14365. (goto-char pos)
  14366. (if (<= (current-column) (current-indentation))
  14367. (org-indent-line-to column)
  14368. (save-excursion (org-indent-line-to column)))
  14369. (setq column (current-column))
  14370. (beginning-of-line 1)
  14371. (if (looking-at
  14372. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14373. (replace-match (concat "\\1" (format org-property-format
  14374. (match-string 2) (match-string 3)))
  14375. t nil))
  14376. (org-move-to-column column)))
  14377. (defun org-set-autofill-regexps ()
  14378. (interactive)
  14379. ;; In the paragraph separator we include headlines, because filling
  14380. ;; text in a line directly attached to a headline would otherwise
  14381. ;; fill the headline as well.
  14382. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14383. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14384. ;; The paragraph starter includes hand-formatted lists.
  14385. (org-set-local 'paragraph-start
  14386. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14387. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14388. ;; But only if the user has not turned off tables or fixed-width regions
  14389. (org-set-local
  14390. 'auto-fill-inhibit-regexp
  14391. (concat "\\*+ \\|#\\+"
  14392. "\\|[ \t]*" org-keyword-time-regexp
  14393. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14394. (concat
  14395. "\\|[ \t]*["
  14396. (if org-enable-table-editor "|" "")
  14397. (if org-enable-fixed-width-editor ":" "")
  14398. "]"))))
  14399. ;; We use our own fill-paragraph function, to make sure that tables
  14400. ;; and fixed-width regions are not wrapped. That function will pass
  14401. ;; through to `fill-paragraph' when appropriate.
  14402. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14403. ; Adaptive filling: To get full control, first make sure that
  14404. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14405. (org-set-local 'adaptive-fill-regexp "\000")
  14406. (org-set-local 'adaptive-fill-function
  14407. 'org-adaptive-fill-function)
  14408. (org-set-local
  14409. 'align-mode-rules-list
  14410. '((org-in-buffer-settings
  14411. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14412. (modes . '(org-mode))))))
  14413. (defun org-fill-paragraph (&optional justify)
  14414. "Re-align a table, pass through to fill-paragraph if no table."
  14415. (let ((table-p (org-at-table-p))
  14416. (table.el-p (org-at-table.el-p)))
  14417. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14418. (save-excursion (goto-char (point-at-bol))
  14419. (looking-at outline-regexp)))
  14420. t) ; skip headlines
  14421. (table.el-p t) ; skip table.el tables
  14422. (table-p (org-table-align) t) ; align org-mode tables
  14423. (t nil)))) ; call paragraph-fill
  14424. ;; For reference, this is the default value of adaptive-fill-regexp
  14425. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14426. (defun org-adaptive-fill-function ()
  14427. "Return a fill prefix for org-mode files.
  14428. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14429. work correctly."
  14430. (cond ((looking-at "#[ \t]+")
  14431. (match-string 0))
  14432. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14433. (save-excursion
  14434. (if (> (match-end 1) (+ (match-beginning 1)
  14435. org-description-max-indent))
  14436. (goto-char (+ (match-beginning 1) 5))
  14437. (goto-char (match-end 0)))
  14438. (make-string (current-column) ?\ )))
  14439. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14440. (save-excursion
  14441. (goto-char (match-end 0))
  14442. (make-string (current-column) ?\ )))
  14443. (t nil)))
  14444. ;;; Other stuff.
  14445. (defun org-toggle-fixed-width-section (arg)
  14446. "Toggle the fixed-width export.
  14447. If there is no active region, the QUOTE keyword at the current headline is
  14448. inserted or removed. When present, it causes the text between this headline
  14449. and the next to be exported as fixed-width text, and unmodified.
  14450. If there is an active region, this command adds or removes a colon as the
  14451. first character of this line. If the first character of a line is a colon,
  14452. this line is also exported in fixed-width font."
  14453. (interactive "P")
  14454. (let* ((cc 0)
  14455. (regionp (org-region-active-p))
  14456. (beg (if regionp (region-beginning) (point)))
  14457. (end (if regionp (region-end)))
  14458. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14459. (case-fold-search nil)
  14460. (re "[ \t]*\\(: \\)")
  14461. off)
  14462. (if regionp
  14463. (save-excursion
  14464. (goto-char beg)
  14465. (setq cc (current-column))
  14466. (beginning-of-line 1)
  14467. (setq off (looking-at re))
  14468. (while (> nlines 0)
  14469. (setq nlines (1- nlines))
  14470. (beginning-of-line 1)
  14471. (cond
  14472. (arg
  14473. (org-move-to-column cc t)
  14474. (insert ": \n")
  14475. (forward-line -1))
  14476. ((and off (looking-at re))
  14477. (replace-match "" t t nil 1))
  14478. ((not off) (org-move-to-column cc t) (insert ": ")))
  14479. (forward-line 1)))
  14480. (save-excursion
  14481. (org-back-to-heading)
  14482. (if (looking-at (concat outline-regexp
  14483. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14484. (replace-match "" t t nil 1)
  14485. (if (looking-at outline-regexp)
  14486. (progn
  14487. (goto-char (match-end 0))
  14488. (insert org-quote-string " "))))))))
  14489. (defun org-reftex-citation ()
  14490. "Use reftex-citation to insert a citation into the buffer.
  14491. This looks for a line like
  14492. #+BIBLIOGRAPHY: foo plain option:-d
  14493. and derives from it that foo.bib is the bbliography file relevant
  14494. for this document. It then installs the necessary environment for RefTeX
  14495. to work in this buffer and calls `reftex-citation' to insert a citation
  14496. into the buffer.
  14497. Export of such citations to both LaTeX and HTML is handled by the contributed
  14498. package org-exp-bibtex by Taru Karttunen."
  14499. (interactive)
  14500. (let ((reftex-docstruct-symbol 'rds)
  14501. (reftex-cite-format "\\cite{%l}")
  14502. rds bib)
  14503. (save-excursion
  14504. (save-restriction
  14505. (widen)
  14506. (let ((case-fold-search t)
  14507. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14508. (if (not (save-excursion
  14509. (or (re-search-forward re nil t)
  14510. (re-search-backward re nil t))))
  14511. (error "No bibliography defined in file")
  14512. (setq bib (concat (match-string 1) ".bib")
  14513. rds (list (list 'bib bib)))))))
  14514. (call-interactively 'reftex-citation)))
  14515. ;;;; Functions extending outline functionality
  14516. (defun org-beginning-of-line (&optional arg)
  14517. "Go to the beginning of the current line. If that is invisible, continue
  14518. to a visible line beginning. This makes the function of C-a more intuitive.
  14519. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14520. first attempt, and only move to after the tags when the cursor is already
  14521. beyond the end of the headline."
  14522. (interactive "P")
  14523. (let ((pos (point))
  14524. (special (if (consp org-special-ctrl-a/e)
  14525. (car org-special-ctrl-a/e)
  14526. org-special-ctrl-a/e))
  14527. refpos)
  14528. (beginning-of-line 1)
  14529. (if (and arg (fboundp 'move-beginning-of-line))
  14530. (call-interactively 'move-beginning-of-line)
  14531. (if (bobp)
  14532. nil
  14533. (backward-char 1)
  14534. (if (org-invisible-p)
  14535. (while (and (not (bobp)) (org-invisible-p))
  14536. (backward-char 1)
  14537. (beginning-of-line 1))
  14538. (forward-char 1))))
  14539. (when special
  14540. (cond
  14541. ((and (looking-at org-complex-heading-regexp)
  14542. (= (char-after (match-end 1)) ?\ ))
  14543. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14544. (point-at-eol)))
  14545. (goto-char
  14546. (if (eq special t)
  14547. (cond ((> pos refpos) refpos)
  14548. ((= pos (point)) refpos)
  14549. (t (point)))
  14550. (cond ((> pos (point)) (point))
  14551. ((not (eq last-command this-command)) (point))
  14552. (t refpos)))))
  14553. ((org-at-item-p)
  14554. (goto-char
  14555. (if (eq special t)
  14556. (cond ((> pos (match-end 4)) (match-end 4))
  14557. ((= pos (point)) (match-end 4))
  14558. (t (point)))
  14559. (cond ((> pos (point)) (point))
  14560. ((not (eq last-command this-command)) (point))
  14561. (t (match-end 4))))))))
  14562. (org-no-warnings
  14563. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14564. (defun org-end-of-line (&optional arg)
  14565. "Go to the end of the line.
  14566. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14567. first attempt, and only move to after the tags when the cursor is already
  14568. beyond the end of the headline."
  14569. (interactive "P")
  14570. (let ((special (if (consp org-special-ctrl-a/e)
  14571. (cdr org-special-ctrl-a/e)
  14572. org-special-ctrl-a/e)))
  14573. (if (or (not special)
  14574. (not (org-on-heading-p))
  14575. arg)
  14576. (call-interactively (if (fboundp 'move-end-of-line)
  14577. 'move-end-of-line
  14578. 'end-of-line))
  14579. (let ((pos (point)))
  14580. (beginning-of-line 1)
  14581. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14582. (if (eq special t)
  14583. (if (or (< pos (match-beginning 1))
  14584. (= pos (match-end 0)))
  14585. (goto-char (match-beginning 1))
  14586. (goto-char (match-end 0)))
  14587. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14588. (goto-char (match-end 0))
  14589. (goto-char (match-beginning 1))))
  14590. (call-interactively (if (fboundp 'move-end-of-line)
  14591. 'move-end-of-line
  14592. 'end-of-line)))))
  14593. (org-no-warnings
  14594. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14595. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14596. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14597. (defun org-backward-sentence (&optional arg)
  14598. "Go to beginning of sentence, or beginning of table field.
  14599. This will call `backward-sentence' or `org-table-beginning-of-field',
  14600. depending on context."
  14601. (interactive "P")
  14602. (cond
  14603. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14604. (t (call-interactively 'backward-sentence))))
  14605. (defun org-forward-sentence (&optional arg)
  14606. "Go to end of sentence, or end of table field.
  14607. This will call `forward-sentence' or `org-table-end-of-field',
  14608. depending on context."
  14609. (interactive "P")
  14610. (cond
  14611. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14612. (t (call-interactively 'forward-sentence))))
  14613. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14614. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14615. (defun org-kill-line (&optional arg)
  14616. "Kill line, to tags or end of line."
  14617. (interactive "P")
  14618. (cond
  14619. ((or (not org-special-ctrl-k)
  14620. (bolp)
  14621. (not (org-on-heading-p)))
  14622. (call-interactively 'kill-line))
  14623. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14624. (kill-region (point) (match-beginning 1))
  14625. (org-set-tags nil t))
  14626. (t (kill-region (point) (point-at-eol)))))
  14627. (define-key org-mode-map "\C-k" 'org-kill-line)
  14628. (defun org-yank (&optional arg)
  14629. "Yank. If the kill is a subtree, treat it specially.
  14630. This command will look at the current kill and check if is a single
  14631. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14632. cursor is at the beginning of a line or after the stars of a currently
  14633. empty headline, then the yank is handled specially. How exactly depends
  14634. on the value of the following variables, both set by default.
  14635. org-yank-folded-subtrees
  14636. When set, the subtree(s) will be folded after insertion, but only
  14637. if doing so would now swallow text after the yanked text.
  14638. org-yank-adjusted-subtrees
  14639. When set, the subtree will be promoted or demoted in order to
  14640. fit into the local outline tree structure, which means that the level
  14641. will be adjusted so that it becomes the smaller one of the two
  14642. *visible* surrounding headings.
  14643. Any prefix to this command will cause `yank' to be called directly with
  14644. no special treatment. In particular, a simple `C-u' prefix will just
  14645. plainly yank the text as it is.
  14646. \[1] Basically, the test checks if the first non-white line is a heading
  14647. and if there are no other headings with fewer stars."
  14648. (interactive "P")
  14649. (setq this-command 'yank)
  14650. (if arg
  14651. (call-interactively 'yank)
  14652. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14653. (and (org-kill-is-subtree-p)
  14654. (or (bolp)
  14655. (and (looking-at "[ \t]*$")
  14656. (string-match
  14657. "\\`\\*+\\'"
  14658. (buffer-substring (point-at-bol) (point)))))))
  14659. swallowp)
  14660. (cond
  14661. ((and subtreep org-yank-folded-subtrees)
  14662. (let ((beg (point))
  14663. end)
  14664. (if (and subtreep org-yank-adjusted-subtrees)
  14665. (org-paste-subtree nil nil 'for-yank)
  14666. (call-interactively 'yank))
  14667. (setq end (point))
  14668. (goto-char beg)
  14669. (when (and (bolp) subtreep
  14670. (not (setq swallowp
  14671. (org-yank-folding-would-swallow-text beg end))))
  14672. (or (looking-at outline-regexp)
  14673. (re-search-forward (concat "^" outline-regexp) end t))
  14674. (while (and (< (point) end) (looking-at outline-regexp))
  14675. (hide-subtree)
  14676. (org-cycle-show-empty-lines 'folded)
  14677. (condition-case nil
  14678. (outline-forward-same-level 1)
  14679. (error (goto-char end)))))
  14680. (when swallowp
  14681. (message
  14682. "Yanked text not folded because that would swallow text"))
  14683. (goto-char end)
  14684. (skip-chars-forward " \t\n\r")
  14685. (beginning-of-line 1)
  14686. (push-mark beg 'nomsg)))
  14687. ((and subtreep org-yank-adjusted-subtrees)
  14688. (let ((beg (point-at-bol)))
  14689. (org-paste-subtree nil nil 'for-yank)
  14690. (push-mark beg 'nomsg)))
  14691. (t
  14692. (call-interactively 'yank))))))
  14693. (defun org-yank-folding-would-swallow-text (beg end)
  14694. "Would hide-subtree at BEG swallow any text after END?"
  14695. (let (level)
  14696. (save-excursion
  14697. (goto-char beg)
  14698. (when (or (looking-at outline-regexp)
  14699. (re-search-forward (concat "^" outline-regexp) end t))
  14700. (setq level (org-outline-level)))
  14701. (goto-char end)
  14702. (skip-chars-forward " \t\r\n\v\f")
  14703. (if (or (eobp)
  14704. (and (bolp) (looking-at org-outline-regexp)
  14705. (<= (org-outline-level) level)))
  14706. nil ; Nothing would be swallowed
  14707. t)))) ; something would swallow
  14708. (define-key org-mode-map "\C-y" 'org-yank)
  14709. (defun org-invisible-p ()
  14710. "Check if point is at a character currently not visible."
  14711. ;; Early versions of noutline don't have `outline-invisible-p'.
  14712. (if (fboundp 'outline-invisible-p)
  14713. (outline-invisible-p)
  14714. (get-char-property (point) 'invisible)))
  14715. (defun org-invisible-p2 ()
  14716. "Check if point is at a character currently not visible."
  14717. (save-excursion
  14718. (if (and (eolp) (not (bobp))) (backward-char 1))
  14719. ;; Early versions of noutline don't have `outline-invisible-p'.
  14720. (if (fboundp 'outline-invisible-p)
  14721. (outline-invisible-p)
  14722. (get-char-property (point) 'invisible))))
  14723. (defun org-back-to-heading (&optional invisible-ok)
  14724. "Call `outline-back-to-heading', but provide a better error message."
  14725. (condition-case nil
  14726. (outline-back-to-heading invisible-ok)
  14727. (error (error "Before first headline at position %d in buffer %s"
  14728. (point) (current-buffer)))))
  14729. (defun org-before-first-heading-p ()
  14730. "Before first heading?"
  14731. (save-excursion
  14732. (null (re-search-backward "^\\*+ " nil t))))
  14733. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14734. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14735. (defun org-at-heading-or-item-p ()
  14736. (or (org-on-heading-p) (org-at-item-p)))
  14737. (defun org-on-target-p ()
  14738. (or (org-in-regexp org-radio-target-regexp)
  14739. (org-in-regexp org-target-regexp)))
  14740. (defun org-up-heading-all (arg)
  14741. "Move to the heading line of which the present line is a subheading.
  14742. This function considers both visible and invisible heading lines.
  14743. With argument, move up ARG levels."
  14744. (if (fboundp 'outline-up-heading-all)
  14745. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14746. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14747. (defun org-up-heading-safe ()
  14748. "Move to the heading line of which the present line is a subheading.
  14749. This version will not throw an error. It will return the level of the
  14750. headline found, or nil if no higher level is found.
  14751. Also, this function will be a lot faster than `outline-up-heading',
  14752. because it relies on stars being the outline starters. This can really
  14753. make a significant difference in outlines with very many siblings."
  14754. (let (start-level re)
  14755. (org-back-to-heading t)
  14756. (setq start-level (funcall outline-level))
  14757. (if (equal start-level 1)
  14758. nil
  14759. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14760. (if (re-search-backward re nil t)
  14761. (funcall outline-level)))))
  14762. (defun org-first-sibling-p ()
  14763. "Is this heading the first child of its parents?"
  14764. (interactive)
  14765. (let ((re (concat "^" outline-regexp))
  14766. level l)
  14767. (unless (org-at-heading-p t)
  14768. (error "Not at a heading"))
  14769. (setq level (funcall outline-level))
  14770. (save-excursion
  14771. (if (not (re-search-backward re nil t))
  14772. t
  14773. (setq l (funcall outline-level))
  14774. (< l level)))))
  14775. (defun org-goto-sibling (&optional previous)
  14776. "Goto the next sibling, even if it is invisible.
  14777. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14778. when a sibling was found. When none is found, return nil and don't
  14779. move point."
  14780. (let ((fun (if previous 're-search-backward 're-search-forward))
  14781. (pos (point))
  14782. (re (concat "^" outline-regexp))
  14783. level l)
  14784. (when (condition-case nil (org-back-to-heading t) (error nil))
  14785. (setq level (funcall outline-level))
  14786. (catch 'exit
  14787. (or previous (forward-char 1))
  14788. (while (funcall fun re nil t)
  14789. (setq l (funcall outline-level))
  14790. (when (< l level) (goto-char pos) (throw 'exit nil))
  14791. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14792. (goto-char pos)
  14793. nil))))
  14794. (defun org-show-siblings ()
  14795. "Show all siblings of the current headline."
  14796. (save-excursion
  14797. (while (org-goto-sibling) (org-flag-heading nil)))
  14798. (save-excursion
  14799. (while (org-goto-sibling 'previous)
  14800. (org-flag-heading nil))))
  14801. (defun org-show-hidden-entry ()
  14802. "Show an entry where even the heading is hidden."
  14803. (save-excursion
  14804. (org-show-entry)))
  14805. (defun org-flag-heading (flag &optional entry)
  14806. "Flag the current heading. FLAG non-nil means make invisible.
  14807. When ENTRY is non-nil, show the entire entry."
  14808. (save-excursion
  14809. (org-back-to-heading t)
  14810. ;; Check if we should show the entire entry
  14811. (if entry
  14812. (progn
  14813. (org-show-entry)
  14814. (save-excursion
  14815. (and (outline-next-heading)
  14816. (org-flag-heading nil))))
  14817. (outline-flag-region (max (point-min) (1- (point)))
  14818. (save-excursion (outline-end-of-heading) (point))
  14819. flag))))
  14820. (defun org-forward-same-level (arg)
  14821. "Move forward to the ARG'th subheading at same level as this one.
  14822. Stop at the first and last subheadings of a superior heading.
  14823. This is like outline-forward-same-level, but invisible headings are ok."
  14824. (interactive "p")
  14825. (org-back-to-heading t)
  14826. (while (> arg 0)
  14827. (let ((point-to-move-to (save-excursion
  14828. (org-get-next-sibling))))
  14829. (if point-to-move-to
  14830. (progn
  14831. (goto-char point-to-move-to)
  14832. (setq arg (1- arg)))
  14833. (progn
  14834. (setq arg 0)
  14835. (error "No following same-level heading"))))))
  14836. (defun org-get-next-sibling ()
  14837. "Move to next heading of the same level, and return point.
  14838. If there is no such heading, return nil.
  14839. This is like outline-next-sibling, but invisible headings are ok."
  14840. (let ((level (funcall outline-level)))
  14841. (outline-next-heading)
  14842. (while (and (not (eobp)) (> (funcall outline-level) level))
  14843. (outline-next-heading))
  14844. (if (or (eobp) (< (funcall outline-level) level))
  14845. nil
  14846. (point))))
  14847. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14848. ;; This contains an exact copy of the original function, but it uses
  14849. ;; `org-back-to-heading', to make it work also in invisible
  14850. ;; trees. And is uses an invisible-OK argument.
  14851. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14852. ;; Furthermore, when used inside Org, finding the end of a large subtree
  14853. ;; with many children and grandchildren etc, this can be much faster
  14854. ;; than the outline version.
  14855. (org-back-to-heading invisible-OK)
  14856. (let ((first t)
  14857. (level (funcall outline-level)))
  14858. (if (and (org-mode-p) (< level 1000))
  14859. ;; A true heading (not a plain list item), in Org-mode
  14860. ;; This means we can easily find the end by looking
  14861. ;; only for the right number of stars. Using a regexp to do
  14862. ;; this is so much faster than using a Lisp loop.
  14863. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  14864. (forward-char 1)
  14865. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  14866. ;; something else, do it the slow way
  14867. (while (and (not (eobp))
  14868. (or first (> (funcall outline-level) level)))
  14869. (setq first nil)
  14870. (outline-next-heading)))
  14871. (unless to-heading
  14872. (if (memq (preceding-char) '(?\n ?\^M))
  14873. (progn
  14874. ;; Go to end of line before heading
  14875. (forward-char -1)
  14876. (if (memq (preceding-char) '(?\n ?\^M))
  14877. ;; leave blank line before heading
  14878. (forward-char -1))))))
  14879. (point))
  14880. (defun org-show-subtree ()
  14881. "Show everything after this heading at deeper levels."
  14882. (outline-flag-region
  14883. (point)
  14884. (save-excursion
  14885. (outline-end-of-subtree) (outline-next-heading) (point))
  14886. nil))
  14887. (defun org-show-entry ()
  14888. "Show the body directly following this heading.
  14889. Show the heading too, if it is currently invisible."
  14890. (interactive)
  14891. (save-excursion
  14892. (condition-case nil
  14893. (progn
  14894. (org-back-to-heading t)
  14895. (outline-flag-region
  14896. (max (point-min) (1- (point)))
  14897. (save-excursion
  14898. (if (re-search-forward
  14899. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14900. (match-beginning 1)
  14901. (point-max)))
  14902. nil))
  14903. (error nil))))
  14904. (defun org-make-options-regexp (kwds &optional extra)
  14905. "Make a regular expression for keyword lines."
  14906. (concat
  14907. "^"
  14908. "#?[ \t]*\\+\\("
  14909. (mapconcat 'regexp-quote kwds "\\|")
  14910. (if extra (concat "\\|" extra))
  14911. "\\):[ \t]*"
  14912. "\\(.+\\)"))
  14913. ;; Make isearch reveal the necessary context
  14914. (defun org-isearch-end ()
  14915. "Reveal context after isearch exits."
  14916. (when isearch-success ; only if search was successful
  14917. (if (featurep 'xemacs)
  14918. ;; Under XEmacs, the hook is run in the correct place,
  14919. ;; we directly show the context.
  14920. (org-show-context 'isearch)
  14921. ;; In Emacs the hook runs *before* restoring the overlays.
  14922. ;; So we have to use a one-time post-command-hook to do this.
  14923. ;; (Emacs 22 has a special variable, see function `org-mode')
  14924. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14925. isearch-mode-end-hook-quit)
  14926. ;; Only when the isearch was not quitted.
  14927. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14928. 'append 'local)))))
  14929. (defun org-isearch-post-command ()
  14930. "Remove self from hook, and show context."
  14931. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14932. (org-show-context 'isearch))
  14933. ;;;; Integration with and fixes for other packages
  14934. ;;; Imenu support
  14935. (defvar org-imenu-markers nil
  14936. "All markers currently used by Imenu.")
  14937. (make-variable-buffer-local 'org-imenu-markers)
  14938. (defun org-imenu-new-marker (&optional pos)
  14939. "Return a new marker for use by Imenu, and remember the marker."
  14940. (let ((m (make-marker)))
  14941. (move-marker m (or pos (point)))
  14942. (push m org-imenu-markers)
  14943. m))
  14944. (defun org-imenu-get-tree ()
  14945. "Produce the index for Imenu."
  14946. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14947. (setq org-imenu-markers nil)
  14948. (let* ((n org-imenu-depth)
  14949. (re (concat "^" outline-regexp))
  14950. (subs (make-vector (1+ n) nil))
  14951. (last-level 0)
  14952. m level head)
  14953. (save-excursion
  14954. (save-restriction
  14955. (widen)
  14956. (goto-char (point-max))
  14957. (while (re-search-backward re nil t)
  14958. (setq level (org-reduced-level (funcall outline-level)))
  14959. (when (<= level n)
  14960. (looking-at org-complex-heading-regexp)
  14961. (setq head (org-link-display-format
  14962. (org-match-string-no-properties 4))
  14963. m (org-imenu-new-marker))
  14964. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14965. (if (>= level last-level)
  14966. (push (cons head m) (aref subs level))
  14967. (push (cons head (aref subs (1+ level))) (aref subs level))
  14968. (loop for i from (1+ level) to n do (aset subs i nil)))
  14969. (setq last-level level)))))
  14970. (aref subs 1)))
  14971. (eval-after-load "imenu"
  14972. '(progn
  14973. (add-hook 'imenu-after-jump-hook
  14974. (lambda ()
  14975. (if (eq major-mode 'org-mode)
  14976. (org-show-context 'org-goto))))))
  14977. (defun org-link-display-format (link)
  14978. "Replace a link with either the description, or the link target
  14979. if no description is present"
  14980. (save-match-data
  14981. (if (string-match org-bracket-link-analytic-regexp link)
  14982. (replace-match (or (match-string 5 link)
  14983. (concat (match-string 1 link)
  14984. (match-string 3 link)))
  14985. nil nil link)
  14986. link)))
  14987. ;; Speedbar support
  14988. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14989. "Overlay marking the agenda restriction line in speedbar.")
  14990. (org-overlay-put org-speedbar-restriction-lock-overlay
  14991. 'face 'org-agenda-restriction-lock)
  14992. (org-overlay-put org-speedbar-restriction-lock-overlay
  14993. 'help-echo "Agendas are currently limited to this item.")
  14994. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14995. (defun org-speedbar-set-agenda-restriction ()
  14996. "Restrict future agenda commands to the location at point in speedbar.
  14997. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14998. (interactive)
  14999. (require 'org-agenda)
  15000. (let (p m tp np dir txt)
  15001. (cond
  15002. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15003. 'org-imenu t))
  15004. (setq m (get-text-property p 'org-imenu-marker))
  15005. (save-excursion
  15006. (save-restriction
  15007. (set-buffer (marker-buffer m))
  15008. (goto-char m)
  15009. (org-agenda-set-restriction-lock 'subtree))))
  15010. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15011. 'speedbar-function 'speedbar-find-file))
  15012. (setq tp (previous-single-property-change
  15013. (1+ p) 'speedbar-function)
  15014. np (next-single-property-change
  15015. tp 'speedbar-function)
  15016. dir (speedbar-line-directory)
  15017. txt (buffer-substring-no-properties (or tp (point-min))
  15018. (or np (point-max))))
  15019. (save-excursion
  15020. (save-restriction
  15021. (set-buffer (find-file-noselect
  15022. (let ((default-directory dir))
  15023. (expand-file-name txt))))
  15024. (unless (org-mode-p)
  15025. (error "Cannot restrict to non-Org-mode file"))
  15026. (org-agenda-set-restriction-lock 'file))))
  15027. (t (error "Don't know how to restrict Org-mode's agenda")))
  15028. (org-move-overlay org-speedbar-restriction-lock-overlay
  15029. (point-at-bol) (point-at-eol))
  15030. (setq current-prefix-arg nil)
  15031. (org-agenda-maybe-redo)))
  15032. (eval-after-load "speedbar"
  15033. '(progn
  15034. (speedbar-add-supported-extension ".org")
  15035. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15036. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15037. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15038. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15039. (add-hook 'speedbar-visiting-tag-hook
  15040. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15041. ;;; Fixes and Hacks for problems with other packages
  15042. ;; Make flyspell not check words in links, to not mess up our keymap
  15043. (defun org-mode-flyspell-verify ()
  15044. "Don't let flyspell put overlays at active buttons."
  15045. (not (get-text-property (point) 'keymap)))
  15046. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15047. (eval-after-load "bookmark"
  15048. '(if (boundp 'bookmark-after-jump-hook)
  15049. ;; We can use the hook
  15050. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15051. ;; Hook not available, use advice
  15052. (defadvice bookmark-jump (after org-make-visible activate)
  15053. "Make the position visible."
  15054. (org-bookmark-jump-unhide))))
  15055. ;; Make sure saveplace show the location if it was hidden
  15056. (eval-after-load "saveplace"
  15057. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15058. "Make the position visible."
  15059. (org-bookmark-jump-unhide)))
  15060. (defun org-bookmark-jump-unhide ()
  15061. "Unhide the current position, to show the bookmark location."
  15062. (and (org-mode-p)
  15063. (or (org-invisible-p)
  15064. (save-excursion (goto-char (max (point-min) (1- (point))))
  15065. (org-invisible-p)))
  15066. (org-show-context 'bookmark-jump)))
  15067. ;; Make session.el ignore our circular variable
  15068. (eval-after-load "session"
  15069. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15070. ;;;; Experimental code
  15071. (defun org-closed-in-range ()
  15072. "Sparse tree of items closed in a certain time range.
  15073. Still experimental, may disappear in the future."
  15074. (interactive)
  15075. ;; Get the time interval from the user.
  15076. (let* ((time1 (time-to-seconds
  15077. (org-read-date nil 'to-time nil "Starting date: ")))
  15078. (time2 (time-to-seconds
  15079. (org-read-date nil 'to-time nil "End date:")))
  15080. ;; callback function
  15081. (callback (lambda ()
  15082. (let ((time
  15083. (time-to-seconds
  15084. (apply 'encode-time
  15085. (org-parse-time-string
  15086. (match-string 1))))))
  15087. ;; check if time in interval
  15088. (and (>= time time1) (<= time time2))))))
  15089. ;; make tree, check each match with the callback
  15090. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15091. ;;;; Finish up
  15092. (provide 'org)
  15093. (run-hooks 'org-load-hook)
  15094. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15095. ;;; org.el ends here