org.el 721 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301
  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, 2010
  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.36trans
  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. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (unless (boundp 'calendar-view-holidays-initially-flag)
  74. (defvaralias 'calendar-view-holidays-initially-flag
  75. 'view-calendar-holidays-initially))
  76. (unless (boundp 'calendar-view-diary-initially-flag)
  77. (defvaralias 'calendar-view-diary-initially-flag
  78. 'view-diary-entries-initially))
  79. (unless (boundp 'diary-fancy-buffer)
  80. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  81. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  82. ;; the file noutline.el being loaded.
  83. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  84. ;; We require noutline, which might be provided in outline.el
  85. (require 'outline) (require 'noutline)
  86. ;; Other stuff we need.
  87. (require 'time-date)
  88. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  89. (require 'easymenu)
  90. (require 'overlay)
  91. (require 'org-macs)
  92. (require 'org-entities)
  93. (require 'org-compat)
  94. (require 'org-faces)
  95. (require 'org-list)
  96. (require 'org-src)
  97. (require 'org-footnote)
  98. ;; babel
  99. (let* ((babel-path (expand-file-name
  100. "babel"
  101. (file-name-directory (or (buffer-file-name)
  102. load-file-name))))
  103. (babel-langs-path (expand-file-name "langs" babel-path)))
  104. (add-to-list 'load-path babel-path)
  105. (add-to-list 'load-path babel-langs-path))
  106. (require 'ob)
  107. (require 'ob-table)
  108. (require 'ob-lob)
  109. (require 'ob-ref)
  110. (require 'ob-tangle)
  111. (require 'ob-comint)
  112. (require 'ob-keys)
  113. (require 'ob-emacs-lisp)
  114. ;;;; Customization variables
  115. (defcustom org-clone-delete-id nil
  116. "Remove ID property of clones of a subtree.
  117. When non-nil, clones of a subtree don't inherit the ID property.
  118. Otherwise they inherit the ID property with a new unique
  119. identifier."
  120. :type 'boolean
  121. :group 'org-id)
  122. ;;; Version
  123. (defconst org-version "6.36trans"
  124. "The version number of the file org.el.")
  125. (defun org-version (&optional here)
  126. "Show the org-mode version in the echo area.
  127. With prefix arg HERE, insert it at point."
  128. (interactive "P")
  129. (let* ((origin default-directory)
  130. (version org-version)
  131. (git-version)
  132. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  133. (when (and (file-exists-p (expand-file-name ".git" dir))
  134. (executable-find "git"))
  135. (unwind-protect
  136. (progn
  137. (cd dir)
  138. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  139. (with-current-buffer "*Shell Command Output*"
  140. (goto-char (point-min))
  141. (setq git-version (buffer-substring (point) (point-at-eol))))
  142. (subst-char-in-string ?- ?. git-version t)
  143. (when (string-match "\\S-"
  144. (shell-command-to-string
  145. "git diff-index --name-only HEAD --"))
  146. (setq git-version (concat git-version ".dirty")))
  147. (setq version (concat version " (" git-version ")"))))
  148. (cd origin)))
  149. (setq version (format "Org-mode version %s" version))
  150. (if here (insert version))
  151. (message version)))
  152. ;;; Compatibility constants
  153. ;;; The custom variables
  154. (defgroup org nil
  155. "Outline-based notes management and organizer."
  156. :tag "Org"
  157. :group 'outlines
  158. :group 'calendar)
  159. (defcustom org-mode-hook nil
  160. "Mode hook for Org-mode, run after the mode was turned on."
  161. :group 'org
  162. :type 'hook)
  163. (defcustom org-load-hook nil
  164. "Hook that is run after org.el has been loaded."
  165. :group 'org
  166. :type 'hook)
  167. (defvar org-modules) ; defined below
  168. (defvar org-modules-loaded nil
  169. "Have the modules been loaded already?")
  170. (defun org-load-modules-maybe (&optional force)
  171. "Load all extensions listed in `org-modules'."
  172. (when (or force (not org-modules-loaded))
  173. (mapc (lambda (ext)
  174. (condition-case nil (require ext)
  175. (error (message "Problems while trying to load feature `%s'" ext))))
  176. org-modules)
  177. (setq org-modules-loaded t)))
  178. (defun org-set-modules (var value)
  179. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  180. (set var value)
  181. (when (featurep 'org)
  182. (org-load-modules-maybe 'force)))
  183. (when (org-bound-and-true-p org-modules)
  184. (let ((a (member 'org-infojs org-modules)))
  185. (and a (setcar a 'org-jsinfo))))
  186. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  187. "Modules that should always be loaded together with org.el.
  188. If a description starts with <C>, the file is not part of Emacs
  189. and loading it will require that you have downloaded and properly installed
  190. the org-mode distribution.
  191. You can also use this system to load external packages (i.e. neither Org
  192. core modules, nor modules from the CONTRIB directory). Just add symbols
  193. to the end of the list. If the package is called org-xyz.el, then you need
  194. to add the symbol `xyz', and the package must have a call to
  195. (provide 'org-xyz)"
  196. :group 'org
  197. :set 'org-set-modules
  198. :type
  199. '(set :greedy t
  200. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  201. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  202. (const :tag " crypt: Encryption of subtrees" org-crypt)
  203. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  204. (const :tag " docview: Links to doc-view buffers" org-docview)
  205. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  206. (const :tag " id: Global IDs for identifying entries" org-id)
  207. (const :tag " info: Links to Info nodes" org-info)
  208. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  209. (const :tag " habit: Track your consistency with habits" org-habit)
  210. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  211. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  212. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  213. (const :tag " mew Links to Mew folders/messages" org-mew)
  214. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  215. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  216. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  217. (const :tag " vm: Links to VM folders/messages" org-vm)
  218. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  219. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  220. (const :tag " mouse: Additional mouse support" org-mouse)
  221. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  222. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  223. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  224. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  225. (const :tag "C collector: Collect properties into tables" org-collector)
  226. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  227. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  228. (const :tag "C eval: Include command output as text" org-eval)
  229. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  230. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  231. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  232. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  233. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  234. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  235. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  236. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  237. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  238. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  239. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  240. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  241. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  242. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  243. (const :tag "C registry: A registry for Org-mode links" org-registry)
  244. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  245. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  246. (const :tag "C secretary: Team management with org-mode" org-secretary)
  247. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  248. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  249. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  250. (const :tag "C track: Keep up with Org-mode development" org-track)
  251. (const :tag "C TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  252. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  253. (defcustom org-support-shift-select nil
  254. "Non-nil means make shift-cursor commands select text when possible.
  255. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  256. selecting a region, or enlarge thusly regions started in this way.
  257. In Org-mode, in special contexts, these same keys are used for other
  258. purposes, important enough to compete with shift selection. Org tries
  259. to balance these needs by supporting `shift-select-mode' outside these
  260. special contexts, under control of this variable.
  261. The default of this variable is nil, to avoid confusing behavior. Shifted
  262. cursor keys will then execute Org commands in the following contexts:
  263. - on a headline, changing TODO state (left/right) and priority (up/down)
  264. - on a time stamp, changing the time
  265. - in a plain list item, changing the bullet type
  266. - in a property definition line, switching between allowed values
  267. - in the BEGIN line of a clock table (changing the time block).
  268. Outside these contexts, the commands will throw an error.
  269. When this variable is t and the cursor is not in a special context,
  270. Org-mode will support shift-selection for making and enlarging regions.
  271. To make this more effective, the bullet cycling will no longer happen
  272. anywhere in an item line, but only if the cursor is exactly on the bullet.
  273. If you set this variable to the symbol `always', then the keys
  274. will not be special in headlines, property lines, and item lines, to make
  275. shift selection work there as well. If this is what you want, you can
  276. use the following alternative commands: `C-c C-t' and `C-c ,' to
  277. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  278. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  279. edited by hand or in column view.
  280. However, when the cursor is on a timestamp, shift-cursor commands
  281. will still edit the time stamp - this is just too good to give up.
  282. XEmacs user should have this variable set to nil, because shift-select-mode
  283. is Emacs 23 only."
  284. :group 'org
  285. :type '(choice
  286. (const :tag "Never" nil)
  287. (const :tag "When outside special context" t)
  288. (const :tag "Everywhere except timestamps" always)))
  289. (defgroup org-startup nil
  290. "Options concerning startup of Org-mode."
  291. :tag "Org Startup"
  292. :group 'org)
  293. (defcustom org-startup-folded t
  294. "Non-nil means entering Org-mode will switch to OVERVIEW.
  295. This can also be configured on a per-file basis by adding one of
  296. the following lines anywhere in the buffer:
  297. #+STARTUP: fold (or `overview', this is equivalent)
  298. #+STARTUP: nofold (or `showall', this is equivalent)
  299. #+STARTUP: content
  300. #+STARTUP: showeverything"
  301. :group 'org-startup
  302. :type '(choice
  303. (const :tag "nofold: show all" nil)
  304. (const :tag "fold: overview" t)
  305. (const :tag "content: all headlines" content)
  306. (const :tag "show everything, even drawers" showeverything)))
  307. (defcustom org-startup-truncated t
  308. "Non-nil means entering Org-mode will set `truncate-lines'.
  309. This is useful since some lines containing links can be very long and
  310. uninteresting. Also tables look terrible when wrapped."
  311. :group 'org-startup
  312. :type 'boolean)
  313. (defcustom org-startup-indented nil
  314. "Non-nil means turn on `org-indent-mode' on startup.
  315. This can also be configured on a per-file basis by adding one of
  316. the following lines anywhere in the buffer:
  317. #+STARTUP: indent
  318. #+STARTUP: noindent"
  319. :group 'org-structure
  320. :type '(choice
  321. (const :tag "Not" nil)
  322. (const :tag "Globally (slow on startup in large files)" t)))
  323. (defcustom org-use-sub-superscripts t
  324. "Non-nil means interpret \"_\" and \"^\" for export.
  325. When this option is turned on, you can use TeX-like syntax for sub- and
  326. superscripts. Several characters after \"_\" or \"^\" will be
  327. considered as a single item - so grouping with {} is normally not
  328. needed. For example, the following things will be parsed as single
  329. sub- or superscripts.
  330. 10^24 or 10^tau several digits will be considered 1 item.
  331. 10^-12 or 10^-tau a leading sign with digits or a word
  332. x^2-y^3 will be read as x^2 - y^3, because items are
  333. terminated by almost any nonword/nondigit char.
  334. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  335. Still, ambiguity is possible - so when in doubt use {} to enclose the
  336. sub/superscript. If you set this variable to the symbol `{}',
  337. the braces are *required* in order to trigger interpretations as
  338. sub/superscript. This can be helpful in documents that need \"_\"
  339. frequently in plain text.
  340. Not all export backends support this, but HTML does.
  341. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  342. :group 'org-startup
  343. :group 'org-export-translation
  344. :type '(choice
  345. (const :tag "Always interpret" t)
  346. (const :tag "Only with braces" {})
  347. (const :tag "Never interpret" nil)))
  348. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts)
  349. (defcustom org-startup-with-beamer-mode nil
  350. "Non-nil means turn on `org-beamer-mode' on startup.
  351. This can also be configured on a per-file basis by adding one of
  352. the following lines anywhere in the buffer:
  353. #+STARTUP: beamer"
  354. :group 'org-startup
  355. :type 'boolean)
  356. (defcustom org-startup-align-all-tables nil
  357. "Non-nil means align all tables when visiting a file.
  358. This is useful when the column width in tables is forced with <N> cookies
  359. in table fields. Such tables will look correct only after the first re-align.
  360. This can also be configured on a per-file basis by adding one of
  361. the following lines anywhere in the buffer:
  362. #+STARTUP: align
  363. #+STARTUP: noalign"
  364. :group 'org-startup
  365. :type 'boolean)
  366. (defcustom org-insert-mode-line-in-empty-file nil
  367. "Non-nil means insert the first line setting Org-mode in empty files.
  368. When the function `org-mode' is called interactively in an empty file, this
  369. normally means that the file name does not automatically trigger Org-mode.
  370. To ensure that the file will always be in Org-mode in the future, a
  371. line enforcing Org-mode will be inserted into the buffer, if this option
  372. has been set."
  373. :group 'org-startup
  374. :type 'boolean)
  375. (defcustom org-replace-disputed-keys nil
  376. "Non-nil means use alternative key bindings for some keys.
  377. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  378. These keys are also used by other packages like shift-selection-mode'
  379. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  380. If you want to use Org-mode together with one of these other modes,
  381. or more generally if you would like to move some Org-mode commands to
  382. other keys, set this variable and configure the keys with the variable
  383. `org-disputed-keys'.
  384. This option is only relevant at load-time of Org-mode, and must be set
  385. *before* org.el is loaded. Changing it requires a restart of Emacs to
  386. become effective."
  387. :group 'org-startup
  388. :type 'boolean)
  389. (defcustom org-use-extra-keys nil
  390. "Non-nil means use extra key sequence definitions for certain
  391. commands. This happens automatically if you run XEmacs or if
  392. window-system is nil. This variable lets you do the same
  393. manually. You must set it before loading org.
  394. Example: on Carbon Emacs 22 running graphically, with an external
  395. keyboard on a Powerbook, the default way of setting M-left might
  396. not work for either Alt or ESC. Setting this variable will make
  397. it work for ESC."
  398. :group 'org-startup
  399. :type 'boolean)
  400. (if (fboundp 'defvaralias)
  401. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  402. (defcustom org-disputed-keys
  403. '(([(shift up)] . [(meta p)])
  404. ([(shift down)] . [(meta n)])
  405. ([(shift left)] . [(meta -)])
  406. ([(shift right)] . [(meta +)])
  407. ([(control shift right)] . [(meta shift +)])
  408. ([(control shift left)] . [(meta shift -)]))
  409. "Keys for which Org-mode and other modes compete.
  410. This is an alist, cars are the default keys, second element specifies
  411. the alternative to use when `org-replace-disputed-keys' is t.
  412. Keys can be specified in any syntax supported by `define-key'.
  413. The value of this option takes effect only at Org-mode's startup,
  414. therefore you'll have to restart Emacs to apply it after changing."
  415. :group 'org-startup
  416. :type 'alist)
  417. (defun org-key (key)
  418. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  419. Or return the original if not disputed.
  420. Also apply the trnaslations defined in `org-xemacs-key-equivalents'."
  421. (when org-replace-disputed-keys
  422. (let* ((nkey (key-description key))
  423. (x (org-find-if (lambda (x)
  424. (equal (key-description (car x)) nkey))
  425. org-disputed-keys)))
  426. (setq key (if x (cdr x) key))))
  427. (when (featurep 'xemacs)
  428. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  429. key)
  430. (defun org-find-if (predicate seq)
  431. (catch 'exit
  432. (while seq
  433. (if (funcall predicate (car seq))
  434. (throw 'exit (car seq))
  435. (pop seq)))))
  436. (defun org-defkey (keymap key def)
  437. "Define a key, possibly translated, as returned by `org-key'."
  438. (define-key keymap (org-key key) def))
  439. (defcustom org-ellipsis nil
  440. "The ellipsis to use in the Org-mode outline.
  441. When nil, just use the standard three dots. When a string, use that instead,
  442. When a face, use the standard 3 dots, but with the specified face.
  443. The change affects only Org-mode (which will then use its own display table).
  444. Changing this requires executing `M-x org-mode' in a buffer to become
  445. effective."
  446. :group 'org-startup
  447. :type '(choice (const :tag "Default" nil)
  448. (face :tag "Face" :value org-warning)
  449. (string :tag "String" :value "...#")))
  450. (defvar org-display-table nil
  451. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  452. (defgroup org-keywords nil
  453. "Keywords in Org-mode."
  454. :tag "Org Keywords"
  455. :group 'org)
  456. (defcustom org-deadline-string "DEADLINE:"
  457. "String to mark deadline entries.
  458. A deadline is this string, followed by a time stamp. Should be a word,
  459. terminated by a colon. You can insert a schedule keyword and
  460. a timestamp with \\[org-deadline].
  461. Changes become only effective after restarting Emacs."
  462. :group 'org-keywords
  463. :type 'string)
  464. (defcustom org-scheduled-string "SCHEDULED:"
  465. "String to mark scheduled TODO entries.
  466. A schedule is this string, followed by a time stamp. Should be a word,
  467. terminated by a colon. You can insert a schedule keyword and
  468. a timestamp with \\[org-schedule].
  469. Changes become only effective after restarting Emacs."
  470. :group 'org-keywords
  471. :type 'string)
  472. (defcustom org-closed-string "CLOSED:"
  473. "String used as the prefix for timestamps logging closing a TODO entry."
  474. :group 'org-keywords
  475. :type 'string)
  476. (defcustom org-clock-string "CLOCK:"
  477. "String used as prefix for timestamps clocking work hours on an item."
  478. :group 'org-keywords
  479. :type 'string)
  480. (defcustom org-comment-string "COMMENT"
  481. "Entries starting with this keyword will never be exported.
  482. An entry can be toggled between COMMENT and normal with
  483. \\[org-toggle-comment].
  484. Changes become only effective after restarting Emacs."
  485. :group 'org-keywords
  486. :type 'string)
  487. (defcustom org-quote-string "QUOTE"
  488. "Entries starting with this keyword will be exported in fixed-width font.
  489. Quoting applies only to the text in the entry following the headline, and does
  490. not extend beyond the next headline, even if that is lower level.
  491. An entry can be toggled between QUOTE and normal with
  492. \\[org-toggle-fixed-width-section]."
  493. :group 'org-keywords
  494. :type 'string)
  495. (defconst org-repeat-re
  496. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  497. "Regular expression for specifying repeated events.
  498. After a match, group 1 contains the repeat expression.")
  499. (defgroup org-structure nil
  500. "Options concerning the general structure of Org-mode files."
  501. :tag "Org Structure"
  502. :group 'org)
  503. (defgroup org-reveal-location nil
  504. "Options about how to make context of a location visible."
  505. :tag "Org Reveal Location"
  506. :group 'org-structure)
  507. (defconst org-context-choice
  508. '(choice
  509. (const :tag "Always" t)
  510. (const :tag "Never" nil)
  511. (repeat :greedy t :tag "Individual contexts"
  512. (cons
  513. (choice :tag "Context"
  514. (const agenda)
  515. (const org-goto)
  516. (const occur-tree)
  517. (const tags-tree)
  518. (const link-search)
  519. (const mark-goto)
  520. (const bookmark-jump)
  521. (const isearch)
  522. (const default))
  523. (boolean))))
  524. "Contexts for the reveal options.")
  525. (defcustom org-show-hierarchy-above '((default . t))
  526. "Non-nil means show full hierarchy when revealing a location.
  527. Org-mode often shows locations in an org-mode file which might have
  528. been invisible before. When this is set, the hierarchy of headings
  529. above the exposed location is shown.
  530. Turning this off for example for sparse trees makes them very compact.
  531. Instead of t, this can also be an alist specifying this option for different
  532. contexts. Valid contexts are
  533. agenda when exposing an entry from the agenda
  534. org-goto when using the command `org-goto' on key C-c C-j
  535. occur-tree when using the command `org-occur' on key C-c /
  536. tags-tree when constructing a sparse tree based on tags matches
  537. link-search when exposing search matches associated with a link
  538. mark-goto when exposing the jump goal of a mark
  539. bookmark-jump when exposing a bookmark location
  540. isearch when exiting from an incremental search
  541. default default for all contexts not set explicitly"
  542. :group 'org-reveal-location
  543. :type org-context-choice)
  544. (defcustom org-show-following-heading '((default . nil))
  545. "Non-nil means show following heading when revealing a location.
  546. Org-mode often shows locations in an org-mode file which might have
  547. been invisible before. When this is set, the heading following the
  548. match is shown.
  549. Turning this off for example for sparse trees makes them very compact,
  550. but makes it harder to edit the location of the match. In such a case,
  551. use the command \\[org-reveal] to show more context.
  552. Instead of t, this can also be an alist specifying this option for different
  553. contexts. See `org-show-hierarchy-above' for valid contexts."
  554. :group 'org-reveal-location
  555. :type org-context-choice)
  556. (defcustom org-show-siblings '((default . nil) (isearch t))
  557. "Non-nil means show all sibling heading when revealing a location.
  558. Org-mode often shows locations in an org-mode file which might have
  559. been invisible before. When this is set, the sibling of the current entry
  560. heading are all made visible. If `org-show-hierarchy-above' is t,
  561. the same happens on each level of the hierarchy above the current entry.
  562. By default this is on for the isearch context, off for all other contexts.
  563. Turning this off for example for sparse trees makes them very compact,
  564. but makes it harder to edit the location of the match. In such a case,
  565. use the command \\[org-reveal] to show more context.
  566. Instead of t, this can also be an alist specifying this option for different
  567. contexts. See `org-show-hierarchy-above' for valid contexts."
  568. :group 'org-reveal-location
  569. :type org-context-choice)
  570. (defcustom org-show-entry-below '((default . nil))
  571. "Non-nil means show the entry below a headline when revealing a location.
  572. Org-mode often shows locations in an org-mode file which might have
  573. been invisible before. When this is set, the text below the headline that is
  574. exposed is also shown.
  575. By default this is off for all contexts.
  576. Instead of t, this can also be an alist specifying this option for different
  577. contexts. See `org-show-hierarchy-above' for valid contexts."
  578. :group 'org-reveal-location
  579. :type org-context-choice)
  580. (defcustom org-indirect-buffer-display 'other-window
  581. "How should indirect tree buffers be displayed?
  582. This applies to indirect buffers created with the commands
  583. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  584. Valid values are:
  585. current-window Display in the current window
  586. other-window Just display in another window.
  587. dedicated-frame Create one new frame, and re-use it each time.
  588. new-frame Make a new frame each time. Note that in this case
  589. previously-made indirect buffers are kept, and you need to
  590. kill these buffers yourself."
  591. :group 'org-structure
  592. :group 'org-agenda-windows
  593. :type '(choice
  594. (const :tag "In current window" current-window)
  595. (const :tag "In current frame, other window" other-window)
  596. (const :tag "Each time a new frame" new-frame)
  597. (const :tag "One dedicated frame" dedicated-frame)))
  598. (defcustom org-use-speed-commands nil
  599. "Non-nil means activate single letter commands at beginning of a headline.
  600. This may also be a function to test for appropriate locations where speed
  601. commands should be active."
  602. :group 'org-structure
  603. :type '(choice
  604. (const :tag "Never" nil)
  605. (const :tag "At beginning of headline stars" t)
  606. (function)))
  607. (defcustom org-speed-commands-user nil
  608. "Alist of additional speed commands.
  609. This list will be checked before `org-speed-commands-default'
  610. when the variable `org-use-speed-commands' is non-nil
  611. and when the cursor is at the beginning of a headline.
  612. The car if each entry is a string with a single letter, which must
  613. be assigned to `self-insert-command' in the global map.
  614. The cdr is either a command to be called interactively, a function
  615. to be called, or a form to be evaluated.
  616. An entry that is just a list with a single string will be interpreted
  617. as a descriptive headline that will be added when listing the speed
  618. copmmands in the Help buffer using the `?' speed command."
  619. :group 'org-structure
  620. :type '(repeat :value ("k" . ignore)
  621. (choice :value ("k" . ignore)
  622. (list :tag "Descriptive Headline" (string :tag "Headline"))
  623. (cons :tag "Letter and Command"
  624. (string :tag "Command letter")
  625. (choice
  626. (function)
  627. (sexp))))))
  628. (defgroup org-cycle nil
  629. "Options concerning visibility cycling in Org-mode."
  630. :tag "Org Cycle"
  631. :group 'org-structure)
  632. (defcustom org-cycle-skip-children-state-if-no-children t
  633. "Non-nil means skip CHILDREN state in entries that don't have any."
  634. :group 'org-cycle
  635. :type 'boolean)
  636. (defcustom org-cycle-max-level nil
  637. "Maximum level which should still be subject to visibility cycling.
  638. Levels higher than this will, for cycling, be treated as text, not a headline.
  639. When `org-odd-levels-only' is set, a value of N in this variable actually
  640. means 2N-1 stars as the limiting headline.
  641. When nil, cycle all levels.
  642. Note that the limiting level of cycling is also influenced by
  643. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  644. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  645. than its value."
  646. :group 'org-cycle
  647. :type '(choice
  648. (const :tag "No limit" nil)
  649. (integer :tag "Maximum level")))
  650. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  651. "Names of drawers. Drawers are not opened by cycling on the headline above.
  652. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  653. this:
  654. :DRAWERNAME:
  655. .....
  656. :END:
  657. The drawer \"PROPERTIES\" is special for capturing properties through
  658. the property API.
  659. Drawers can be defined on the per-file basis with a line like:
  660. #+DRAWERS: HIDDEN STATE PROPERTIES"
  661. :group 'org-structure
  662. :group 'org-cycle
  663. :type '(repeat (string :tag "Drawer Name")))
  664. (defcustom org-hide-block-startup nil
  665. "Non-nil means entering Org-mode will fold all blocks.
  666. This can also be set in on a per-file basis with
  667. #+STARTUP: hideblocks
  668. #+STARTUP: showblocks"
  669. :group 'org-startup
  670. :group 'org-cycle
  671. :type 'boolean)
  672. (defcustom org-cycle-global-at-bob nil
  673. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  674. This makes it possible to do global cycling without having to use S-TAB or
  675. C-u TAB. For this special case to work, the first line of the buffer
  676. must not be a headline - it may be empty or some other text. When used in
  677. this way, `org-cycle-hook' is disables temporarily, to make sure the
  678. cursor stays at the beginning of the buffer.
  679. When this option is nil, don't do anything special at the beginning
  680. of the buffer."
  681. :group 'org-cycle
  682. :type 'boolean)
  683. (defcustom org-cycle-level-after-item/entry-creation t
  684. "Non-nil means cycle entry level or item indentation in new empty entries.
  685. When the cursor is at the end of an empty headline, i.e with only stars
  686. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  687. and then all possible anchestor states, before returning to the original state.
  688. This makes data entry extremely fast: M-RET to create a new headline,
  689. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  690. When the cursor is at the end of an empty plain list item, one TAB will
  691. make it a subitem, two or more tabs will back up to make this an item
  692. higher up in the item hierarchy."
  693. :group 'org-cycle
  694. :type 'boolean)
  695. (defcustom org-cycle-emulate-tab t
  696. "Where should `org-cycle' emulate TAB.
  697. nil Never
  698. white Only in completely white lines
  699. whitestart Only at the beginning of lines, before the first non-white char
  700. t Everywhere except in headlines
  701. exc-hl-bol Everywhere except at the start of a headline
  702. If TAB is used in a place where it does not emulate TAB, the current subtree
  703. visibility is cycled."
  704. :group 'org-cycle
  705. :type '(choice (const :tag "Never" nil)
  706. (const :tag "Only in completely white lines" white)
  707. (const :tag "Before first char in a line" whitestart)
  708. (const :tag "Everywhere except in headlines" t)
  709. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  710. ))
  711. (defcustom org-cycle-separator-lines 2
  712. "Number of empty lines needed to keep an empty line between collapsed trees.
  713. If you leave an empty line between the end of a subtree and the following
  714. headline, this empty line is hidden when the subtree is folded.
  715. Org-mode will leave (exactly) one empty line visible if the number of
  716. empty lines is equal or larger to the number given in this variable.
  717. So the default 2 means at least 2 empty lines after the end of a subtree
  718. are needed to produce free space between a collapsed subtree and the
  719. following headline.
  720. If the number is negative, and the number of empty lines is at least -N,
  721. all empty lines are shown.
  722. Special case: when 0, never leave empty lines in collapsed view."
  723. :group 'org-cycle
  724. :type 'integer)
  725. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  726. (defcustom org-pre-cycle-hook nil
  727. "Hook that is run before visibility cycling is happening.
  728. The function(s) in this hook must accept a single argument which indicates
  729. the new state that will be set right after running this hook. The
  730. argument is a symbol. Before a global state change, it can have the values
  731. `overview', `content', or `all'. Before a local state change, it can have
  732. the values `folded', `children', or `subtree'."
  733. :group 'org-cycle
  734. :type 'hook)
  735. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  736. org-cycle-hide-drawers
  737. org-cycle-show-empty-lines
  738. org-optimize-window-after-visibility-change)
  739. "Hook that is run after `org-cycle' has changed the buffer visibility.
  740. The function(s) in this hook must accept a single argument which indicates
  741. the new state that was set by the most recent `org-cycle' command. The
  742. argument is a symbol. After a global state change, it can have the values
  743. `overview', `content', or `all'. After a local state change, it can have
  744. the values `folded', `children', or `subtree'."
  745. :group 'org-cycle
  746. :type 'hook)
  747. (defgroup org-edit-structure nil
  748. "Options concerning structure editing in Org-mode."
  749. :tag "Org Edit Structure"
  750. :group 'org-structure)
  751. (defcustom org-odd-levels-only nil
  752. "Non-nil means skip even levels and only use odd levels for the outline.
  753. This has the effect that two stars are being added/taken away in
  754. promotion/demotion commands. It also influences how levels are
  755. handled by the exporters.
  756. Changing it requires restart of `font-lock-mode' to become effective
  757. for fontification also in regions already fontified.
  758. You may also set this on a per-file basis by adding one of the following
  759. lines to the buffer:
  760. #+STARTUP: odd
  761. #+STARTUP: oddeven"
  762. :group 'org-edit-structure
  763. :group 'org-appearance
  764. :type 'boolean)
  765. (defcustom org-adapt-indentation t
  766. "Non-nil means adapt indentation to outline node level.
  767. When this variable is set, Org assumes that you write outlines by
  768. indenting text in each node to align with the headline (after the stars).
  769. The following issues are influenced by this variable:
  770. - When this is set and the *entire* text in an entry is indented, the
  771. indentation is increased by one space in a demotion command, and
  772. decreased by one in a promotion command. If any line in the entry
  773. body starts with text at column 0, indentation is not changed at all.
  774. - Property drawers and planning information is inserted indented when
  775. this variable s set. When nil, they will not be indented.
  776. - TAB indents a line relative to context. The lines below a headline
  777. will be indented when this variable is set.
  778. Note that this is all about true indentation, by adding and removing
  779. space characters. See also `org-indent.el' which does level-dependent
  780. indentation in a virtual way, i.e. at display time in Emacs."
  781. :group 'org-edit-structure
  782. :type 'boolean)
  783. (defcustom org-special-ctrl-a/e nil
  784. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  785. When t, `C-a' will bring back the cursor to the beginning of the
  786. headline text, i.e. after the stars and after a possible TODO keyword.
  787. In an item, this will be the position after the bullet.
  788. When the cursor is already at that position, another `C-a' will bring
  789. it to the beginning of the line.
  790. `C-e' will jump to the end of the headline, ignoring the presence of tags
  791. in the headline. A second `C-e' will then jump to the true end of the
  792. line, after any tags. This also means that, when this variable is
  793. non-nil, `C-e' also will never jump beyond the end of the heading of a
  794. folded section, i.e. not after the ellipses.
  795. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  796. going to the true line boundary first. Only a directly following, identical
  797. keypress will bring the cursor to the special positions.
  798. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  799. set separately."
  800. :group 'org-edit-structure
  801. :type '(choice
  802. (const :tag "off" nil)
  803. (const :tag "on: after stars/bullet and before tags first" t)
  804. (const :tag "reversed: true line boundary first" reversed)
  805. (cons :tag "Set C-a and C-e separately"
  806. (choice :tag "Special C-a"
  807. (const :tag "off" nil)
  808. (const :tag "on: after stars/bullet first" t)
  809. (const :tag "reversed: before stars/bullet first" reversed))
  810. (choice :tag "Special C-e"
  811. (const :tag "off" nil)
  812. (const :tag "on: before tags first" t)
  813. (const :tag "reversed: after tags first" reversed)))))
  814. (if (fboundp 'defvaralias)
  815. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  816. (defcustom org-special-ctrl-k nil
  817. "Non-nil means `C-k' will behave specially in headlines.
  818. When nil, `C-k' will call the default `kill-line' command.
  819. When t, the following will happen while the cursor is in the headline:
  820. - When the cursor is at the beginning of a headline, kill the entire
  821. line and possible the folded subtree below the line.
  822. - When in the middle of the headline text, kill the headline up to the tags.
  823. - When after the headline text, kill the tags."
  824. :group 'org-edit-structure
  825. :type 'boolean)
  826. (defcustom org-ctrl-k-protect-subtree nil
  827. "Non-nil means, do not delete a hidden subtree with C-k.
  828. When set to the symbol `error', simply throw an error when C-k is
  829. used to kill (part-of) a headline that has hidden text behind it.
  830. Any other non-nil value will result in a query to the user, if it is
  831. OK to kill that hidden subtree. When nil, kill without remorse."
  832. :group 'org-edit-structure
  833. :type '(choice
  834. (const :tag "Do not protect hidden subtrees" nil)
  835. (const :tag "Protect hidden subtrees with a security query" t)
  836. (const :tag "Never kill a hidden subtree with C-k" error)))
  837. (defcustom org-yank-folded-subtrees t
  838. "Non-nil means when yanking subtrees, fold them.
  839. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  840. it starts with a heading and all other headings in it are either children
  841. or siblings, then fold all the subtrees. However, do this only if no
  842. text after the yank would be swallowed into a folded tree by this action."
  843. :group 'org-edit-structure
  844. :type 'boolean)
  845. (defcustom org-yank-adjusted-subtrees nil
  846. "Non-nil means when yanking subtrees, adjust the level.
  847. With this setting, `org-paste-subtree' is used to insert the subtree, see
  848. this function for details."
  849. :group 'org-edit-structure
  850. :type 'boolean)
  851. (defcustom org-M-RET-may-split-line '((default . t))
  852. "Non-nil means M-RET will split the line at the cursor position.
  853. When nil, it will go to the end of the line before making a
  854. new line.
  855. You may also set this option in a different way for different
  856. contexts. Valid contexts are:
  857. headline when creating a new headline
  858. item when creating a new item
  859. table in a table field
  860. default the value to be used for all contexts not explicitly
  861. customized"
  862. :group 'org-structure
  863. :group 'org-table
  864. :type '(choice
  865. (const :tag "Always" t)
  866. (const :tag "Never" nil)
  867. (repeat :greedy t :tag "Individual contexts"
  868. (cons
  869. (choice :tag "Context"
  870. (const headline)
  871. (const item)
  872. (const table)
  873. (const default))
  874. (boolean)))))
  875. (defcustom org-insert-heading-respect-content nil
  876. "Non-nil means insert new headings after the current subtree.
  877. When nil, the new heading is created directly after the current line.
  878. The commands \\[org-insert-heading-respect-content] and
  879. \\[org-insert-todo-heading-respect-content] turn this variable on
  880. for the duration of the command."
  881. :group 'org-structure
  882. :type 'boolean)
  883. (defcustom org-blank-before-new-entry '((heading . auto)
  884. (plain-list-item . auto))
  885. "Should `org-insert-heading' leave a blank line before new heading/item?
  886. The value is an alist, with `heading' and `plain-list-item' as car,
  887. and a boolean flag as cdr. For plain lists, if the variable
  888. `org-empty-line-terminates-plain-lists' is set, the setting here
  889. is ignored and no empty line is inserted, to keep the list in tact."
  890. :group 'org-edit-structure
  891. :type '(list
  892. (cons (const heading)
  893. (choice (const :tag "Never" nil)
  894. (const :tag "Always" t)
  895. (const :tag "Auto" auto)))
  896. (cons (const plain-list-item)
  897. (choice (const :tag "Never" nil)
  898. (const :tag "Always" t)
  899. (const :tag "Auto" auto)))))
  900. (defcustom org-insert-heading-hook nil
  901. "Hook being run after inserting a new heading."
  902. :group 'org-edit-structure
  903. :type 'hook)
  904. (defcustom org-enable-fixed-width-editor t
  905. "Non-nil means lines starting with \":\" are treated as fixed-width.
  906. This currently only means they are never auto-wrapped.
  907. When nil, such lines will be treated like ordinary lines.
  908. See also the QUOTE keyword."
  909. :group 'org-edit-structure
  910. :type 'boolean)
  911. (defcustom org-goto-auto-isearch t
  912. "Non-nil means typing characters in org-goto starts incremental search."
  913. :group 'org-edit-structure
  914. :type 'boolean)
  915. (defgroup org-sparse-trees nil
  916. "Options concerning sparse trees in Org-mode."
  917. :tag "Org Sparse Trees"
  918. :group 'org-structure)
  919. (defcustom org-highlight-sparse-tree-matches t
  920. "Non-nil means highlight all matches that define a sparse tree.
  921. The highlights will automatically disappear the next time the buffer is
  922. changed by an edit command."
  923. :group 'org-sparse-trees
  924. :type 'boolean)
  925. (defcustom org-remove-highlights-with-change t
  926. "Non-nil means any change to the buffer will remove temporary highlights.
  927. Such highlights are created by `org-occur' and `org-clock-display'.
  928. When nil, `C-c C-c needs to be used to get rid of the highlights.
  929. The highlights created by `org-preview-latex-fragment' always need
  930. `C-c C-c' to be removed."
  931. :group 'org-sparse-trees
  932. :group 'org-time
  933. :type 'boolean)
  934. (defcustom org-occur-hook '(org-first-headline-recenter)
  935. "Hook that is run after `org-occur' has constructed a sparse tree.
  936. This can be used to recenter the window to show as much of the structure
  937. as possible."
  938. :group 'org-sparse-trees
  939. :type 'hook)
  940. (defgroup org-imenu-and-speedbar nil
  941. "Options concerning imenu and speedbar in Org-mode."
  942. :tag "Org Imenu and Speedbar"
  943. :group 'org-structure)
  944. (defcustom org-imenu-depth 2
  945. "The maximum level for Imenu access to Org-mode headlines.
  946. This also applied for speedbar access."
  947. :group 'org-imenu-and-speedbar
  948. :type 'integer)
  949. (defgroup org-table nil
  950. "Options concerning tables in Org-mode."
  951. :tag "Org Table"
  952. :group 'org)
  953. (defcustom org-enable-table-editor 'optimized
  954. "Non-nil means lines starting with \"|\" are handled by the table editor.
  955. When nil, such lines will be treated like ordinary lines.
  956. When equal to the symbol `optimized', the table editor will be optimized to
  957. do the following:
  958. - Automatic overwrite mode in front of whitespace in table fields.
  959. This makes the structure of the table stay in tact as long as the edited
  960. field does not exceed the column width.
  961. - Minimize the number of realigns. Normally, the table is aligned each time
  962. TAB or RET are pressed to move to another field. With optimization this
  963. happens only if changes to a field might have changed the column width.
  964. Optimization requires replacing the functions `self-insert-command',
  965. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  966. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  967. very good at guessing when a re-align will be necessary, but you can always
  968. force one with \\[org-ctrl-c-ctrl-c].
  969. If you would like to use the optimized version in Org-mode, but the
  970. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  971. This variable can be used to turn on and off the table editor during a session,
  972. but in order to toggle optimization, a restart is required.
  973. See also the variable `org-table-auto-blank-field'."
  974. :group 'org-table
  975. :type '(choice
  976. (const :tag "off" nil)
  977. (const :tag "on" t)
  978. (const :tag "on, optimized" optimized)))
  979. (defcustom org-self-insert-cluster-for-undo t
  980. "Non-nil means cluster self-insert commands for undo when possible.
  981. If this is set, then, like in the Emacs command loop, 20 consecutive
  982. characters will be undone together.
  983. This is configurable, because there is some impact on typing performance."
  984. :group 'org-table
  985. :type 'boolean)
  986. (defcustom org-table-tab-recognizes-table.el t
  987. "Non-nil means TAB will automatically notice a table.el table.
  988. When it sees such a table, it moves point into it and - if necessary -
  989. calls `table-recognize-table'."
  990. :group 'org-table-editing
  991. :type 'boolean)
  992. (defgroup org-link nil
  993. "Options concerning links in Org-mode."
  994. :tag "Org Link"
  995. :group 'org)
  996. (defvar org-link-abbrev-alist-local nil
  997. "Buffer-local version of `org-link-abbrev-alist', which see.
  998. The value of this is taken from the #+LINK lines.")
  999. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1000. (defcustom org-link-abbrev-alist nil
  1001. "Alist of link abbreviations.
  1002. The car of each element is a string, to be replaced at the start of a link.
  1003. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1004. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1005. [[linkkey:tag][description]]
  1006. The 'linkkey' must be a word word, starting with a letter, followed
  1007. by letters, numbers, '-' or '_'.
  1008. If REPLACE is a string, the tag will simply be appended to create the link.
  1009. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1010. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1011. be inserted at that point (see the function `url-hexify-string').
  1012. REPLACE may also be a function that will be called with the tag as the
  1013. only argument to create the link, which should be returned as a string.
  1014. See the manual for examples."
  1015. :group 'org-link
  1016. :type '(repeat
  1017. (cons
  1018. (string :tag "Protocol")
  1019. (choice
  1020. (string :tag "Format")
  1021. (function)))))
  1022. (defcustom org-descriptive-links t
  1023. "Non-nil means hide link part and only show description of bracket links.
  1024. Bracket links are like [[link][description]]. This variable sets the initial
  1025. state in new org-mode buffers. The setting can then be toggled on a
  1026. per-buffer basis from the Org->Hyperlinks menu."
  1027. :group 'org-link
  1028. :type 'boolean)
  1029. (defcustom org-link-file-path-type 'adaptive
  1030. "How the path name in file links should be stored.
  1031. Valid values are:
  1032. relative Relative to the current directory, i.e. the directory of the file
  1033. into which the link is being inserted.
  1034. absolute Absolute path, if possible with ~ for home directory.
  1035. noabbrev Absolute path, no abbreviation of home directory.
  1036. adaptive Use relative path for files in the current directory and sub-
  1037. directories of it. For other files, use an absolute path."
  1038. :group 'org-link
  1039. :type '(choice
  1040. (const relative)
  1041. (const absolute)
  1042. (const noabbrev)
  1043. (const adaptive)))
  1044. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1045. "Types of links that should be activated in Org-mode files.
  1046. This is a list of symbols, each leading to the activation of a certain link
  1047. type. In principle, it does not hurt to turn on most link types - there may
  1048. be a small gain when turning off unused link types. The types are:
  1049. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1050. angular Links in angular brackets that may contain whitespace like
  1051. <bbdb:Carsten Dominik>.
  1052. plain Plain links in normal text, no whitespace, like http://google.com.
  1053. radio Text that is matched by a radio target, see manual for details.
  1054. tag Tag settings in a headline (link to tag search).
  1055. date Time stamps (link to calendar).
  1056. footnote Footnote labels.
  1057. Changing this variable requires a restart of Emacs to become effective."
  1058. :group 'org-link
  1059. :type '(set :greedy t
  1060. (const :tag "Double bracket links (new style)" bracket)
  1061. (const :tag "Angular bracket links (old style)" angular)
  1062. (const :tag "Plain text links" plain)
  1063. (const :tag "Radio target matches" radio)
  1064. (const :tag "Tags" tag)
  1065. (const :tag "Timestamps" date)
  1066. (const :tag "Footnotes" footnote)))
  1067. (defcustom org-make-link-description-function nil
  1068. "Function to use to generate link descriptions from links. If
  1069. nil the link location will be used. This function must take two
  1070. parameters; the first is the link and the second the description
  1071. org-insert-link has generated, and should return the description
  1072. to use."
  1073. :group 'org-link
  1074. :type 'function)
  1075. (defgroup org-link-store nil
  1076. "Options concerning storing links in Org-mode."
  1077. :tag "Org Store Link"
  1078. :group 'org-link)
  1079. (defcustom org-email-link-description-format "Email %c: %.30s"
  1080. "Format of the description part of a link to an email or usenet message.
  1081. The following %-escapes will be replaced by corresponding information:
  1082. %F full \"From\" field
  1083. %f name, taken from \"From\" field, address if no name
  1084. %T full \"To\" field
  1085. %t first name in \"To\" field, address if no name
  1086. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1087. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1088. %s subject
  1089. %m message-id.
  1090. You may use normal field width specification between the % and the letter.
  1091. This is for example useful to limit the length of the subject.
  1092. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1093. :group 'org-link-store
  1094. :type 'string)
  1095. (defcustom org-from-is-user-regexp
  1096. (let (r1 r2)
  1097. (when (and user-mail-address (not (string= user-mail-address "")))
  1098. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1099. (when (and user-full-name (not (string= user-full-name "")))
  1100. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1101. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1102. "Regexp matched against the \"From:\" header of an email or usenet message.
  1103. It should match if the message is from the user him/herself."
  1104. :group 'org-link-store
  1105. :type 'regexp)
  1106. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1107. "Non-nil means storing a link to an Org file will use entry IDs.
  1108. Note that before this variable is even considered, org-id must be loaded,
  1109. so please customize `org-modules' and turn it on.
  1110. The variable can have the following values:
  1111. t Create an ID if needed to make a link to the current entry.
  1112. create-if-interactive
  1113. If `org-store-link' is called directly (interactively, as a user
  1114. command), do create an ID to support the link. But when doing the
  1115. job for remember, only use the ID if it already exists. The
  1116. purpose of this setting is to avoid proliferation of unwanted
  1117. IDs, just because you happen to be in an Org file when you
  1118. call `org-remember' that automatically and preemptively
  1119. creates a link. If you do want to get an ID link in a remember
  1120. template to an entry not having an ID, create it first by
  1121. explicitly creating a link to it, using `C-c C-l' first.
  1122. create-if-interactive-and-no-custom-id
  1123. Like create-if-interactive, but do not create an ID if there is
  1124. a CUSTOM_ID property defined in the entry. This is the default.
  1125. use-existing
  1126. Use existing ID, do not create one.
  1127. nil Never use an ID to make a link, instead link using a text search for
  1128. the headline text."
  1129. :group 'org-link-store
  1130. :type '(choice
  1131. (const :tag "Create ID to make link" t)
  1132. (const :tag "Create if storing link interactively"
  1133. create-if-interactive)
  1134. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1135. create-if-interactive-and-no-custom-id)
  1136. (const :tag "Only use existing" use-existing)
  1137. (const :tag "Do not use ID to create link" nil)))
  1138. (defcustom org-context-in-file-links t
  1139. "Non-nil means file links from `org-store-link' contain context.
  1140. A search string will be added to the file name with :: as separator and
  1141. used to find the context when the link is activated by the command
  1142. `org-open-at-point'.
  1143. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1144. negates this setting for the duration of the command."
  1145. :group 'org-link-store
  1146. :type 'boolean)
  1147. (defcustom org-keep-stored-link-after-insertion nil
  1148. "Non-nil means keep link in list for entire session.
  1149. The command `org-store-link' adds a link pointing to the current
  1150. location to an internal list. These links accumulate during a session.
  1151. The command `org-insert-link' can be used to insert links into any
  1152. Org-mode file (offering completion for all stored links). When this
  1153. option is nil, every link which has been inserted once using \\[org-insert-link]
  1154. will be removed from the list, to make completing the unused links
  1155. more efficient."
  1156. :group 'org-link-store
  1157. :type 'boolean)
  1158. (defgroup org-link-follow nil
  1159. "Options concerning following links in Org-mode."
  1160. :tag "Org Follow Link"
  1161. :group 'org-link)
  1162. (defcustom org-link-translation-function nil
  1163. "Function to translate links with different syntax to Org syntax.
  1164. This can be used to translate links created for example by the Planner
  1165. or emacs-wiki packages to Org syntax.
  1166. The function must accept two parameters, a TYPE containing the link
  1167. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1168. which is everything after the link protocol. It should return a cons
  1169. with possibly modified values of type and path.
  1170. Org contains a function for this, so if you set this variable to
  1171. `org-translate-link-from-planner', you should be able follow many
  1172. links created by planner."
  1173. :group 'org-link-follow
  1174. :type 'function)
  1175. (defcustom org-follow-link-hook nil
  1176. "Hook that is run after a link has been followed."
  1177. :group 'org-link-follow
  1178. :type 'hook)
  1179. (defcustom org-tab-follows-link nil
  1180. "Non-nil means on links TAB will follow the link.
  1181. Needs to be set before org.el is loaded.
  1182. This really should not be used, it does not make sense, and the
  1183. implementation is bad."
  1184. :group 'org-link-follow
  1185. :type 'boolean)
  1186. (defcustom org-return-follows-link nil
  1187. "Non-nil means on links RET will follow the link."
  1188. :group 'org-link-follow
  1189. :type 'boolean)
  1190. (defcustom org-mouse-1-follows-link
  1191. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1192. "Non-nil means mouse-1 on a link will follow the link.
  1193. A longer mouse click will still set point. Does not work on XEmacs.
  1194. Needs to be set before org.el is loaded."
  1195. :group 'org-link-follow
  1196. :type 'boolean)
  1197. (defcustom org-mark-ring-length 4
  1198. "Number of different positions to be recorded in the ring
  1199. Changing this requires a restart of Emacs to work correctly."
  1200. :group 'org-link-follow
  1201. :type 'integer)
  1202. (defcustom org-link-frame-setup
  1203. '((vm . vm-visit-folder-other-frame)
  1204. (gnus . gnus-other-frame)
  1205. (file . find-file-other-window))
  1206. "Setup the frame configuration for following links.
  1207. When following a link with Emacs, it may often be useful to display
  1208. this link in another window or frame. This variable can be used to
  1209. set this up for the different types of links.
  1210. For VM, use any of
  1211. `vm-visit-folder'
  1212. `vm-visit-folder-other-frame'
  1213. For Gnus, use any of
  1214. `gnus'
  1215. `gnus-other-frame'
  1216. `org-gnus-no-new-news'
  1217. For FILE, use any of
  1218. `find-file'
  1219. `find-file-other-window'
  1220. `find-file-other-frame'
  1221. For the calendar, use the variable `calendar-setup'.
  1222. For BBDB, it is currently only possible to display the matches in
  1223. another window."
  1224. :group 'org-link-follow
  1225. :type '(list
  1226. (cons (const vm)
  1227. (choice
  1228. (const vm-visit-folder)
  1229. (const vm-visit-folder-other-window)
  1230. (const vm-visit-folder-other-frame)))
  1231. (cons (const gnus)
  1232. (choice
  1233. (const gnus)
  1234. (const gnus-other-frame)
  1235. (const org-gnus-no-new-news)))
  1236. (cons (const file)
  1237. (choice
  1238. (const find-file)
  1239. (const find-file-other-window)
  1240. (const find-file-other-frame)))))
  1241. (defcustom org-display-internal-link-with-indirect-buffer nil
  1242. "Non-nil means use indirect buffer to display infile links.
  1243. Activating internal links (from one location in a file to another location
  1244. in the same file) normally just jumps to the location. When the link is
  1245. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1246. another window. When this option is set, the other window actually displays
  1247. an indirect buffer clone of the current buffer, to avoid any visibility
  1248. changes to the current buffer."
  1249. :group 'org-link-follow
  1250. :type 'boolean)
  1251. (defcustom org-open-non-existing-files nil
  1252. "Non-nil means `org-open-file' will open non-existing files.
  1253. When nil, an error will be generated.
  1254. This variable applies only to external applications because they
  1255. might choke on non-existing files. If the link is to a file that
  1256. will be opened in Emacs, the variable is ignored."
  1257. :group 'org-link-follow
  1258. :type 'boolean)
  1259. (defcustom org-open-directory-means-index-dot-org nil
  1260. "Non-nil means a link to a directory really means to index.org.
  1261. When nil, following a directory link will run dired or open a finder/explorer
  1262. window on that directory."
  1263. :group 'org-link-follow
  1264. :type 'boolean)
  1265. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1266. "Function and arguments to call for following mailto links.
  1267. This is a list with the first element being a lisp function, and the
  1268. remaining elements being arguments to the function. In string arguments,
  1269. %a will be replaced by the address, and %s will be replaced by the subject
  1270. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1271. :group 'org-link-follow
  1272. :type '(choice
  1273. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1274. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1275. (const :tag "message-mail" (message-mail "%a" "%s"))
  1276. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1277. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1278. "Non-nil means ask for confirmation before executing shell links.
  1279. Shell links can be dangerous: just think about a link
  1280. [[shell:rm -rf ~/*][Google Search]]
  1281. This link would show up in your Org-mode document as \"Google Search\",
  1282. but really it would remove your entire home directory.
  1283. Therefore we advise against setting this variable to nil.
  1284. Just change it to `y-or-n-p' if you want to confirm with a
  1285. single keystroke rather than having to type \"yes\"."
  1286. :group 'org-link-follow
  1287. :type '(choice
  1288. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1289. (const :tag "with y-or-n (faster)" y-or-n-p)
  1290. (const :tag "no confirmation (dangerous)" nil)))
  1291. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1292. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1293. Elisp links can be dangerous: just think about a link
  1294. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1295. This link would show up in your Org-mode document as \"Google Search\",
  1296. but really it would remove your entire home directory.
  1297. Therefore we advise against setting this variable to nil.
  1298. Just change it to `y-or-n-p' if you want to confirm with a
  1299. single keystroke rather than having to type \"yes\"."
  1300. :group 'org-link-follow
  1301. :type '(choice
  1302. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1303. (const :tag "with y-or-n (faster)" y-or-n-p)
  1304. (const :tag "no confirmation (dangerous)" nil)))
  1305. (defconst org-file-apps-defaults-gnu
  1306. '((remote . emacs)
  1307. (system . mailcap)
  1308. (t . mailcap))
  1309. "Default file applications on a UNIX or GNU/Linux system.
  1310. See `org-file-apps'.")
  1311. (defconst org-file-apps-defaults-macosx
  1312. '((remote . emacs)
  1313. (t . "open %s")
  1314. (system . "open %s")
  1315. ("ps.gz" . "gv %s")
  1316. ("eps.gz" . "gv %s")
  1317. ("dvi" . "xdvi %s")
  1318. ("fig" . "xfig %s"))
  1319. "Default file applications on a MacOS X system.
  1320. The system \"open\" is known as a default, but we use X11 applications
  1321. for some files for which the OS does not have a good default.
  1322. See `org-file-apps'.")
  1323. (defconst org-file-apps-defaults-windowsnt
  1324. (list
  1325. '(remote . emacs)
  1326. (cons t
  1327. (list (if (featurep 'xemacs)
  1328. 'mswindows-shell-execute
  1329. 'w32-shell-execute)
  1330. "open" 'file))
  1331. (cons 'system
  1332. (list (if (featurep 'xemacs)
  1333. 'mswindows-shell-execute
  1334. 'w32-shell-execute)
  1335. "open" 'file)))
  1336. "Default file applications on a Windows NT system.
  1337. The system \"open\" is used for most files.
  1338. See `org-file-apps'.")
  1339. (defcustom org-file-apps
  1340. '(
  1341. (auto-mode . emacs)
  1342. ("\\.mm\\'" . default)
  1343. ("\\.x?html?\\'" . default)
  1344. ("\\.pdf\\'" . default)
  1345. )
  1346. "External applications for opening `file:path' items in a document.
  1347. Org-mode uses system defaults for different file types, but
  1348. you can use this variable to set the application for a given file
  1349. extension. The entries in this list are cons cells where the car identifies
  1350. files and the cdr the corresponding command. Possible values for the
  1351. file identifier are
  1352. \"string\" A string as a file identifier can be interpreted in different
  1353. ways, depending on its contents:
  1354. - Alphanumeric characters only:
  1355. Match links with this file extension.
  1356. Example: (\"pdf\" . \"evince %s\")
  1357. to open PDFs with evince.
  1358. - Regular expression: Match links where the
  1359. filename matches the regexp. If you want to
  1360. use groups here, use shy groups.
  1361. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1362. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1363. to open *.html and *.xhtml with firefox.
  1364. - Regular expression which contains (non-shy) groups:
  1365. Match links where the whole link, including \"::\", and
  1366. anything after that, matches the regexp.
  1367. In a custom command string, %1, %2, etc. are replaced with
  1368. the parts of the link that were matched by the groups.
  1369. For backwards compatibility, if a command string is given
  1370. that does not use any of the group matches, this case is
  1371. handled identically to the second one (i.e. match against
  1372. file name only).
  1373. In a custom lisp form, you can access the group matches with
  1374. (match-string n link).
  1375. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1376. to open [[file:document.pdf::5]] with evince at page 5.
  1377. `directory' Matches a directory
  1378. `remote' Matches a remote file, accessible through tramp or efs.
  1379. Remote files most likely should be visited through Emacs
  1380. because external applications cannot handle such paths.
  1381. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1382. so all files Emacs knows how to handle. Using this with
  1383. command `emacs' will open most files in Emacs. Beware that this
  1384. will also open html files inside Emacs, unless you add
  1385. (\"html\" . default) to the list as well.
  1386. t Default for files not matched by any of the other options.
  1387. `system' The system command to open files, like `open' on Windows
  1388. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1389. that will be selected if you call `C-c C-o' with a double
  1390. `C-u C-u' prefix.
  1391. Possible values for the command are:
  1392. `emacs' The file will be visited by the current Emacs process.
  1393. `default' Use the default application for this file type, which is the
  1394. association for t in the list, most likely in the system-specific
  1395. part.
  1396. This can be used to overrule an unwanted setting in the
  1397. system-specific variable.
  1398. `system' Use the system command for opening files, like \"open\".
  1399. This command is specified by the entry whose car is `system'.
  1400. Most likely, the system-specific version of this variable
  1401. does define this command, but you can overrule/replace it
  1402. here.
  1403. string A command to be executed by a shell; %s will be replaced
  1404. by the path to the file.
  1405. sexp A Lisp form which will be evaluated. The file path will
  1406. be available in the Lisp variable `file'.
  1407. For more examples, see the system specific constants
  1408. `org-file-apps-defaults-macosx'
  1409. `org-file-apps-defaults-windowsnt'
  1410. `org-file-apps-defaults-gnu'."
  1411. :group 'org-link-follow
  1412. :type '(repeat
  1413. (cons (choice :value ""
  1414. (string :tag "Extension")
  1415. (const :tag "System command to open files" system)
  1416. (const :tag "Default for unrecognized files" t)
  1417. (const :tag "Remote file" remote)
  1418. (const :tag "Links to a directory" directory)
  1419. (const :tag "Any files that have Emacs modes"
  1420. auto-mode))
  1421. (choice :value ""
  1422. (const :tag "Visit with Emacs" emacs)
  1423. (const :tag "Use default" default)
  1424. (const :tag "Use the system command" system)
  1425. (string :tag "Command")
  1426. (sexp :tag "Lisp form")))))
  1427. (defgroup org-refile nil
  1428. "Options concerning refiling entries in Org-mode."
  1429. :tag "Org Refile"
  1430. :group 'org)
  1431. (defcustom org-directory "~/org"
  1432. "Directory with org files.
  1433. This is just a default location to look for Org files. There is no need
  1434. at all to put your files into this directory. It is only used in the
  1435. following situations:
  1436. 1. When a remember template specifies a target file that is not an
  1437. absolute path. The path will then be interpreted relative to
  1438. `org-directory'
  1439. 2. When a remember note is filed away in an interactive way (when exiting the
  1440. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1441. with `org-directory' as the default path."
  1442. :group 'org-refile
  1443. :group 'org-remember
  1444. :type 'directory)
  1445. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1446. "Default target for storing notes.
  1447. Used by the hooks for remember.el. This can be a string, or nil to mean
  1448. the value of `remember-data-file'.
  1449. You can set this on a per-template basis with the variable
  1450. `org-remember-templates'."
  1451. :group 'org-refile
  1452. :group 'org-remember
  1453. :type '(choice
  1454. (const :tag "Default from remember-data-file" nil)
  1455. file))
  1456. (defcustom org-goto-interface 'outline
  1457. "The default interface to be used for `org-goto'.
  1458. Allowed values are:
  1459. outline The interface shows an outline of the relevant file
  1460. and the correct heading is found by moving through
  1461. the outline or by searching with incremental search.
  1462. outline-path-completion Headlines in the current buffer are offered via
  1463. completion. This is the interface also used by
  1464. the refile command."
  1465. :group 'org-refile
  1466. :type '(choice
  1467. (const :tag "Outline" outline)
  1468. (const :tag "Outline-path-completion" outline-path-completion)))
  1469. (defcustom org-goto-max-level 5
  1470. "Maximum level to be considered when running org-goto with refile interface."
  1471. :group 'org-refile
  1472. :type 'integer)
  1473. (defcustom org-reverse-note-order nil
  1474. "Non-nil means store new notes at the beginning of a file or entry.
  1475. When nil, new notes will be filed to the end of a file or entry.
  1476. This can also be a list with cons cells of regular expressions that
  1477. are matched against file names, and values."
  1478. :group 'org-remember
  1479. :group 'org-refile
  1480. :type '(choice
  1481. (const :tag "Reverse always" t)
  1482. (const :tag "Reverse never" nil)
  1483. (repeat :tag "By file name regexp"
  1484. (cons regexp boolean))))
  1485. (defcustom org-log-refile nil
  1486. "Information to record when a task is refiled.
  1487. Possible values are:
  1488. nil Don't add anything
  1489. time Add a time stamp to the task
  1490. note Prompt for a note and add it with template `org-log-note-headings'
  1491. This option can also be set with on a per-file-basis with
  1492. #+STARTUP: nologrefile
  1493. #+STARTUP: logrefile
  1494. #+STARTUP: lognoterefile
  1495. You can have local logging settings for a subtree by setting the LOGGING
  1496. property to one or more of these keywords.
  1497. When bulk-refiling from the agenda, the value `note' is forbidden and
  1498. will temporarily be changed to `time'."
  1499. :group 'org-refile
  1500. :group 'org-progress
  1501. :type '(choice
  1502. (const :tag "No logging" nil)
  1503. (const :tag "Record timestamp" time)
  1504. (const :tag "Record timestamp with note." note)))
  1505. (defcustom org-refile-targets nil
  1506. "Targets for refiling entries with \\[org-refile].
  1507. This is list of cons cells. Each cell contains:
  1508. - a specification of the files to be considered, either a list of files,
  1509. or a symbol whose function or variable value will be used to retrieve
  1510. a file name or a list of file names. If you use `org-agenda-files' for
  1511. that, all agenda files will be scanned for targets. Nil means consider
  1512. headings in the current buffer.
  1513. - A specification of how to find candidate refile targets. This may be
  1514. any of:
  1515. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1516. This tag has to be present in all target headlines, inheritance will
  1517. not be considered.
  1518. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1519. todo keyword.
  1520. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1521. headlines that are refiling targets.
  1522. - a cons cell (:level . N). Any headline of level N is considered a target.
  1523. Note that, when `org-odd-levels-only' is set, level corresponds to
  1524. order in hierarchy, not to the number of stars.
  1525. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1526. Note that, when `org-odd-levels-only' is set, level corresponds to
  1527. order in hierarchy, not to the number of stars.
  1528. You can set the variable `org-refile-target-verify-function' to a function
  1529. to verify each headline found by the simple critery above.
  1530. When this variable is nil, all top-level headlines in the current buffer
  1531. are used, equivalent to the value `((nil . (:level . 1))'."
  1532. :group 'org-refile
  1533. :type '(repeat
  1534. (cons
  1535. (choice :value org-agenda-files
  1536. (const :tag "All agenda files" org-agenda-files)
  1537. (const :tag "Current buffer" nil)
  1538. (function) (variable) (file))
  1539. (choice :tag "Identify target headline by"
  1540. (cons :tag "Specific tag" (const :value :tag) (string))
  1541. (cons :tag "TODO keyword" (const :value :todo) (string))
  1542. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1543. (cons :tag "Level number" (const :value :level) (integer))
  1544. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1545. (defcustom org-refile-target-verify-function nil
  1546. "Function to verify if the headline at point should be a refile target.
  1547. The function will be called without arguments, with point at the
  1548. beginning of the headline. It should return t and leave point
  1549. where it is if the headline is a valid target for refiling.
  1550. If the target should not be selected, the function must return nil.
  1551. In addition to this, it may move point to a place from where the search
  1552. should be continued. For example, the function may decide that the entire
  1553. subtree of the current entry should be excluded and move point to the end
  1554. of the subtree."
  1555. :group 'org-refile
  1556. :type 'function)
  1557. (defcustom org-refile-use-cache nil
  1558. "Non-nil means cache refile targets to speed up the process.
  1559. The cache for a particular file will be updated automatically when
  1560. the buffer has been killed, or when any of the marker used for flagging
  1561. refile targets no longer points at a live buffer.
  1562. If you have added new entries to a buffer that might themselves be targets,
  1563. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1564. find that easier, `C-u C-u C-u C-c C-w'."
  1565. :group 'org-refile
  1566. :type 'boolean)
  1567. (defcustom org-refile-use-outline-path nil
  1568. "Non-nil means provide refile targets as paths.
  1569. So a level 3 headline will be available as level1/level2/level3.
  1570. When the value is `file', also include the file name (without directory)
  1571. into the path. In this case, you can also stop the completion after
  1572. the file name, to get entries inserted as top level in the file.
  1573. When `full-file-path', include the full file path."
  1574. :group 'org-refile
  1575. :type '(choice
  1576. (const :tag "Not" nil)
  1577. (const :tag "Yes" t)
  1578. (const :tag "Start with file name" file)
  1579. (const :tag "Start with full file path" full-file-path)))
  1580. (defcustom org-outline-path-complete-in-steps t
  1581. "Non-nil means complete the outline path in hierarchical steps.
  1582. When Org-mode uses the refile interface to select an outline path
  1583. \(see variable `org-refile-use-outline-path'), the completion of
  1584. the path can be done is a single go, or if can be done in steps down
  1585. the headline hierarchy. Going in steps is probably the best if you
  1586. do not use a special completion package like `ido' or `icicles'.
  1587. However, when using these packages, going in one step can be very
  1588. fast, while still showing the whole path to the entry."
  1589. :group 'org-refile
  1590. :type 'boolean)
  1591. (defcustom org-refile-allow-creating-parent-nodes nil
  1592. "Non-nil means allow to create new nodes as refile targets.
  1593. New nodes are then created by adding \"/new node name\" to the completion
  1594. of an existing node. When the value of this variable is `confirm',
  1595. new node creation must be confirmed by the user (recommended)
  1596. When nil, the completion must match an existing entry.
  1597. Note that, if the new heading is not seen by the criteria
  1598. listed in `org-refile-targets', multiple instances of the same
  1599. heading would be created by trying again to file under the new
  1600. heading."
  1601. :group 'org-refile
  1602. :type '(choice
  1603. (const :tag "Never" nil)
  1604. (const :tag "Always" t)
  1605. (const :tag "Prompt for confirmation" confirm)))
  1606. (defgroup org-todo nil
  1607. "Options concerning TODO items in Org-mode."
  1608. :tag "Org TODO"
  1609. :group 'org)
  1610. (defgroup org-progress nil
  1611. "Options concerning Progress logging in Org-mode."
  1612. :tag "Org Progress"
  1613. :group 'org-time)
  1614. (defvar org-todo-interpretation-widgets
  1615. '(
  1616. (:tag "Sequence (cycling hits every state)" sequence)
  1617. (:tag "Type (cycling directly to DONE)" type))
  1618. "The available interpretation symbols for customizing
  1619. `org-todo-keywords'.
  1620. Interested libraries should add to this list.")
  1621. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1622. "List of TODO entry keyword sequences and their interpretation.
  1623. \\<org-mode-map>This is a list of sequences.
  1624. Each sequence starts with a symbol, either `sequence' or `type',
  1625. indicating if the keywords should be interpreted as a sequence of
  1626. action steps, or as different types of TODO items. The first
  1627. keywords are states requiring action - these states will select a headline
  1628. for inclusion into the global TODO list Org-mode produces. If one of
  1629. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1630. signify that no further action is necessary. If \"|\" is not found,
  1631. the last keyword is treated as the only DONE state of the sequence.
  1632. The command \\[org-todo] cycles an entry through these states, and one
  1633. additional state where no keyword is present. For details about this
  1634. cycling, see the manual.
  1635. TODO keywords and interpretation can also be set on a per-file basis with
  1636. the special #+SEQ_TODO and #+TYP_TODO lines.
  1637. Each keyword can optionally specify a character for fast state selection
  1638. \(in combination with the variable `org-use-fast-todo-selection')
  1639. and specifiers for state change logging, using the same syntax
  1640. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1641. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1642. indicates to record a time stamp each time this state is selected.
  1643. Each keyword may also specify if a timestamp or a note should be
  1644. recorded when entering or leaving the state, by adding additional
  1645. characters in the parenthesis after the keyword. This looks like this:
  1646. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1647. record only the time of the state change. With X and Y being either
  1648. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1649. Y when leaving the state if and only if the *target* state does not
  1650. define X. You may omit any of the fast-selection key or X or /Y,
  1651. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1652. For backward compatibility, this variable may also be just a list
  1653. of keywords - in this case the interpretation (sequence or type) will be
  1654. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1655. :group 'org-todo
  1656. :group 'org-keywords
  1657. :type '(choice
  1658. (repeat :tag "Old syntax, just keywords"
  1659. (string :tag "Keyword"))
  1660. (repeat :tag "New syntax"
  1661. (cons
  1662. (choice
  1663. :tag "Interpretation"
  1664. ;;Quick and dirty way to see
  1665. ;;`org-todo-interpretations'. This takes the
  1666. ;;place of item arguments
  1667. :convert-widget
  1668. (lambda (widget)
  1669. (widget-put widget
  1670. :args (mapcar
  1671. #'(lambda (x)
  1672. (widget-convert
  1673. (cons 'const x)))
  1674. org-todo-interpretation-widgets))
  1675. widget))
  1676. (repeat
  1677. (string :tag "Keyword"))))))
  1678. (defvar org-todo-keywords-1 nil
  1679. "All TODO and DONE keywords active in a buffer.")
  1680. (make-variable-buffer-local 'org-todo-keywords-1)
  1681. (defvar org-todo-keywords-for-agenda nil)
  1682. (defvar org-done-keywords-for-agenda nil)
  1683. (defvar org-drawers-for-agenda nil)
  1684. (defvar org-todo-keyword-alist-for-agenda nil)
  1685. (defvar org-tag-alist-for-agenda nil)
  1686. (defvar org-agenda-contributing-files nil)
  1687. (defvar org-not-done-keywords nil)
  1688. (make-variable-buffer-local 'org-not-done-keywords)
  1689. (defvar org-done-keywords nil)
  1690. (make-variable-buffer-local 'org-done-keywords)
  1691. (defvar org-todo-heads nil)
  1692. (make-variable-buffer-local 'org-todo-heads)
  1693. (defvar org-todo-sets nil)
  1694. (make-variable-buffer-local 'org-todo-sets)
  1695. (defvar org-todo-log-states nil)
  1696. (make-variable-buffer-local 'org-todo-log-states)
  1697. (defvar org-todo-kwd-alist nil)
  1698. (make-variable-buffer-local 'org-todo-kwd-alist)
  1699. (defvar org-todo-key-alist nil)
  1700. (make-variable-buffer-local 'org-todo-key-alist)
  1701. (defvar org-todo-key-trigger nil)
  1702. (make-variable-buffer-local 'org-todo-key-trigger)
  1703. (defcustom org-todo-interpretation 'sequence
  1704. "Controls how TODO keywords are interpreted.
  1705. This variable is in principle obsolete and is only used for
  1706. backward compatibility, if the interpretation of todo keywords is
  1707. not given already in `org-todo-keywords'. See that variable for
  1708. more information."
  1709. :group 'org-todo
  1710. :group 'org-keywords
  1711. :type '(choice (const sequence)
  1712. (const type)))
  1713. (defcustom org-use-fast-todo-selection t
  1714. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1715. This variable describes if and under what circumstances the cycling
  1716. mechanism for TODO keywords will be replaced by a single-key, direct
  1717. selection scheme.
  1718. When nil, fast selection is never used.
  1719. When the symbol `prefix', it will be used when `org-todo' is called with
  1720. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1721. in an agenda buffer.
  1722. When t, fast selection is used by default. In this case, the prefix
  1723. argument forces cycling instead.
  1724. In all cases, the special interface is only used if access keys have actually
  1725. been assigned by the user, i.e. if keywords in the configuration are followed
  1726. by a letter in parenthesis, like TODO(t)."
  1727. :group 'org-todo
  1728. :type '(choice
  1729. (const :tag "Never" nil)
  1730. (const :tag "By default" t)
  1731. (const :tag "Only with C-u C-c C-t" prefix)))
  1732. (defcustom org-provide-todo-statistics t
  1733. "Non-nil means update todo statistics after insert and toggle.
  1734. ALL-HEADLINES means update todo statistics by including headlines
  1735. with no TODO keyword as well, counting them as not done.
  1736. A list of TODO keywords means the same, but skip keywords that are
  1737. not in this list.
  1738. When this is set, todo statistics is updated in the parent of the
  1739. current entry each time a todo state is changed."
  1740. :group 'org-todo
  1741. :type '(choice
  1742. (const :tag "Yes, only for TODO entries" t)
  1743. (const :tag "Yes, including all entries" 'all-headlines)
  1744. (repeat :tag "Yes, for TODOs in this list"
  1745. (string :tag "TODO keyword"))
  1746. (other :tag "No TODO statistics" nil)))
  1747. (defcustom org-hierarchical-todo-statistics t
  1748. "Non-nil means TODO statistics covers just direct children.
  1749. When nil, all entries in the subtree are considered.
  1750. This has only an effect if `org-provide-todo-statistics' is set.
  1751. To set this to nil for only a single subtree, use a COOKIE_DATA
  1752. property and include the word \"recursive\" into the value."
  1753. :group 'org-todo
  1754. :type 'boolean)
  1755. (defcustom org-after-todo-state-change-hook nil
  1756. "Hook which is run after the state of a TODO item was changed.
  1757. The new state (a string with a TODO keyword, or nil) is available in the
  1758. Lisp variable `state'."
  1759. :group 'org-todo
  1760. :type 'hook)
  1761. (defvar org-blocker-hook nil
  1762. "Hook for functions that are allowed to block a state change.
  1763. Each function gets as its single argument a property list, see
  1764. `org-trigger-hook' for more information about this list.
  1765. If any of the functions in this hook returns nil, the state change
  1766. is blocked.")
  1767. (defvar org-trigger-hook nil
  1768. "Hook for functions that are triggered by a state change.
  1769. Each function gets as its single argument a property list with at least
  1770. the following elements:
  1771. (:type type-of-change :position pos-at-entry-start
  1772. :from old-state :to new-state)
  1773. Depending on the type, more properties may be present.
  1774. This mechanism is currently implemented for:
  1775. TODO state changes
  1776. ------------------
  1777. :type todo-state-change
  1778. :from previous state (keyword as a string), or nil, or a symbol
  1779. 'todo' or 'done', to indicate the general type of state.
  1780. :to new state, like in :from")
  1781. (defcustom org-enforce-todo-dependencies nil
  1782. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1783. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1784. be blocked if any prior sibling is not yet done.
  1785. Finally, if the parent is blocked because of ordered siblings of its own,
  1786. the child will also be blocked.
  1787. This variable needs to be set before org.el is loaded, and you need to
  1788. restart Emacs after a change to make the change effective. The only way
  1789. to change is while Emacs is running is through the customize interface."
  1790. :set (lambda (var val)
  1791. (set var val)
  1792. (if val
  1793. (add-hook 'org-blocker-hook
  1794. 'org-block-todo-from-children-or-siblings-or-parent)
  1795. (remove-hook 'org-blocker-hook
  1796. 'org-block-todo-from-children-or-siblings-or-parent)))
  1797. :group 'org-todo
  1798. :type 'boolean)
  1799. (defcustom org-enforce-todo-checkbox-dependencies nil
  1800. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1801. When this is nil, checkboxes have no influence on switching TODO states.
  1802. When non-nil, you first need to check off all check boxes before the TODO
  1803. entry can be switched to DONE.
  1804. This variable needs to be set before org.el is loaded, and you need to
  1805. restart Emacs after a change to make the change effective. The only way
  1806. to change is while Emacs is running is through the customize interface."
  1807. :set (lambda (var val)
  1808. (set var val)
  1809. (if val
  1810. (add-hook 'org-blocker-hook
  1811. 'org-block-todo-from-checkboxes)
  1812. (remove-hook 'org-blocker-hook
  1813. 'org-block-todo-from-checkboxes)))
  1814. :group 'org-todo
  1815. :type 'boolean)
  1816. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1817. "Non-nil means inserting a TODO heading is treated as state change.
  1818. So when the command \\[org-insert-todo-heading] is used, state change
  1819. logging will apply if appropriate. When nil, the new TODO item will
  1820. be inserted directly, and no logging will take place."
  1821. :group 'org-todo
  1822. :type 'boolean)
  1823. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1824. "Non-nil means switching TODO states with S-cursor counts as state change.
  1825. This is the default behavior. However, setting this to nil allows a
  1826. convenient way to select a TODO state and bypass any logging associated
  1827. with that."
  1828. :group 'org-todo
  1829. :type 'boolean)
  1830. (defcustom org-todo-state-tags-triggers nil
  1831. "Tag changes that should be triggered by TODO state changes.
  1832. This is a list. Each entry is
  1833. (state-change (tag . flag) .......)
  1834. State-change can be a string with a state, and empty string to indicate the
  1835. state that has no TODO keyword, or it can be one of the symbols `todo'
  1836. or `done', meaning any not-done or done state, respectively."
  1837. :group 'org-todo
  1838. :group 'org-tags
  1839. :type '(repeat
  1840. (cons (choice :tag "When changing to"
  1841. (const :tag "Not-done state" todo)
  1842. (const :tag "Done state" done)
  1843. (string :tag "State"))
  1844. (repeat
  1845. (cons :tag "Tag action"
  1846. (string :tag "Tag")
  1847. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1848. (defcustom org-log-done nil
  1849. "Information to record when a task moves to the DONE state.
  1850. Possible values are:
  1851. nil Don't add anything, just change the keyword
  1852. time Add a time stamp to the task
  1853. note Prompt for a note and add it with template `org-log-note-headings'
  1854. This option can also be set with on a per-file-basis with
  1855. #+STARTUP: nologdone
  1856. #+STARTUP: logdone
  1857. #+STARTUP: lognotedone
  1858. You can have local logging settings for a subtree by setting the LOGGING
  1859. property to one or more of these keywords."
  1860. :group 'org-todo
  1861. :group 'org-progress
  1862. :type '(choice
  1863. (const :tag "No logging" nil)
  1864. (const :tag "Record CLOSED timestamp" time)
  1865. (const :tag "Record CLOSED timestamp with note." note)))
  1866. ;; Normalize old uses of org-log-done.
  1867. (cond
  1868. ((eq org-log-done t) (setq org-log-done 'time))
  1869. ((and (listp org-log-done) (memq 'done org-log-done))
  1870. (setq org-log-done 'note)))
  1871. (defcustom org-log-reschedule nil
  1872. "Information to record when the scheduling date of a tasks is modified.
  1873. Possible values are:
  1874. nil Don't add anything, just change the date
  1875. time Add a time stamp to the task
  1876. note Prompt for a note and add it with template `org-log-note-headings'
  1877. This option can also be set with on a per-file-basis with
  1878. #+STARTUP: nologreschedule
  1879. #+STARTUP: logreschedule
  1880. #+STARTUP: lognotereschedule"
  1881. :group 'org-todo
  1882. :group 'org-progress
  1883. :type '(choice
  1884. (const :tag "No logging" nil)
  1885. (const :tag "Record timestamp" time)
  1886. (const :tag "Record timestamp with note." note)))
  1887. (defcustom org-log-redeadline nil
  1888. "Information to record when the deadline date of a tasks is modified.
  1889. Possible values are:
  1890. nil Don't add anything, just change the date
  1891. time Add a time stamp to the task
  1892. note Prompt for a note and add it with template `org-log-note-headings'
  1893. This option can also be set with on a per-file-basis with
  1894. #+STARTUP: nologredeadline
  1895. #+STARTUP: logredeadline
  1896. #+STARTUP: lognoteredeadline
  1897. You can have local logging settings for a subtree by setting the LOGGING
  1898. property to one or more of these keywords."
  1899. :group 'org-todo
  1900. :group 'org-progress
  1901. :type '(choice
  1902. (const :tag "No logging" nil)
  1903. (const :tag "Record timestamp" time)
  1904. (const :tag "Record timestamp with note." note)))
  1905. (defcustom org-log-note-clock-out nil
  1906. "Non-nil means record a note when clocking out of an item.
  1907. This can also be configured on a per-file basis by adding one of
  1908. the following lines anywhere in the buffer:
  1909. #+STARTUP: lognoteclock-out
  1910. #+STARTUP: nolognoteclock-out"
  1911. :group 'org-todo
  1912. :group 'org-progress
  1913. :type 'boolean)
  1914. (defcustom org-log-done-with-time t
  1915. "Non-nil means the CLOSED time stamp will contain date and time.
  1916. When nil, only the date will be recorded."
  1917. :group 'org-progress
  1918. :type 'boolean)
  1919. (defcustom org-log-note-headings
  1920. '((done . "CLOSING NOTE %t")
  1921. (state . "State %-12s from %-12S %t")
  1922. (note . "Note taken on %t")
  1923. (reschedule . "Rescheduled from %S on %t")
  1924. (delschedule . "Not scheduled, was %S on %t")
  1925. (redeadline . "New deadline from %S on %t")
  1926. (deldeadline . "Removed deadline, was %S on %t")
  1927. (refile . "Refiled on %t")
  1928. (clock-out . ""))
  1929. "Headings for notes added to entries.
  1930. The value is an alist, with the car being a symbol indicating the note
  1931. context, and the cdr is the heading to be used. The heading may also be the
  1932. empty string.
  1933. %t in the heading will be replaced by a time stamp.
  1934. %T will be an acive time stamp instead the default inacive one
  1935. %s will be replaced by the new TODO state, in double quotes.
  1936. %S will be replaced by the old TODO state, in double quotes.
  1937. %u will be replaced by the user name.
  1938. %U will be replaced by the full user name.
  1939. In fact, it is not a good idea to change the `state' entry, because
  1940. agenda log mode depends on the format of these entries."
  1941. :group 'org-todo
  1942. :group 'org-progress
  1943. :type '(list :greedy t
  1944. (cons (const :tag "Heading when closing an item" done) string)
  1945. (cons (const :tag
  1946. "Heading when changing todo state (todo sequence only)"
  1947. state) string)
  1948. (cons (const :tag "Heading when just taking a note" note) string)
  1949. (cons (const :tag "Heading when clocking out" clock-out) string)
  1950. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1951. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1952. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1953. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1954. (cons (const :tag "Heading when refiling" refile) string)))
  1955. (unless (assq 'note org-log-note-headings)
  1956. (push '(note . "%t") org-log-note-headings))
  1957. (defcustom org-log-into-drawer nil
  1958. "Non-nil means insert state change notes and time stamps into a drawer.
  1959. When nil, state changes notes will be inserted after the headline and
  1960. any scheduling and clock lines, but not inside a drawer.
  1961. The value of this variable should be the name of the drawer to use.
  1962. LOGBOOK is proposed at the default drawer for this purpose, you can
  1963. also set this to a string to define the drawer of your choice.
  1964. A value of t is also allowed, representing \"LOGBOOK\".
  1965. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1966. will be ignored.
  1967. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1968. a subtree."
  1969. :group 'org-todo
  1970. :group 'org-progress
  1971. :type '(choice
  1972. (const :tag "Not into a drawer" nil)
  1973. (const :tag "LOGBOOK" t)
  1974. (string :tag "Other")))
  1975. (if (fboundp 'defvaralias)
  1976. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1977. (defun org-log-into-drawer ()
  1978. "Return the value of `org-log-into-drawer', but let properties overrule.
  1979. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1980. used instead of the default value."
  1981. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1982. (cond
  1983. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1984. ((equal p "t") "LOGBOOK")
  1985. (t p))))
  1986. (defcustom org-log-state-notes-insert-after-drawers nil
  1987. "Non-nil means insert state change notes after any drawers in entry.
  1988. Only the drawers that *immediately* follow the headline and the
  1989. deadline/scheduled line are skipped.
  1990. When nil, insert notes right after the heading and perhaps the line
  1991. with deadline/scheduling if present.
  1992. This variable will have no effect if `org-log-into-drawer' is
  1993. set."
  1994. :group 'org-todo
  1995. :group 'org-progress
  1996. :type 'boolean)
  1997. (defcustom org-log-states-order-reversed t
  1998. "Non-nil means the latest state note will be directly after heading.
  1999. When nil, the state change notes will be ordered according to time."
  2000. :group 'org-todo
  2001. :group 'org-progress
  2002. :type 'boolean)
  2003. (defcustom org-todo-repeat-to-state nil
  2004. "The TODO state to which a repeater should return the repeating task.
  2005. By default this is the first task in a TODO sequence, or the previous state
  2006. in a TODO_TYP set. But you can specify another task here.
  2007. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2008. :group 'org-todo
  2009. :type '(choice (const :tag "Head of sequence" nil)
  2010. (string :tag "Specific state")))
  2011. (defcustom org-log-repeat 'time
  2012. "Non-nil means record moving through the DONE state when triggering repeat.
  2013. An auto-repeating task is immediately switched back to TODO when
  2014. marked DONE. If you are not logging state changes (by adding \"@\"
  2015. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2016. record a closing note, there will be no record of the task moving
  2017. through DONE. This variable forces taking a note anyway.
  2018. nil Don't force a record
  2019. time Record a time stamp
  2020. note Record a note
  2021. This option can also be set with on a per-file-basis with
  2022. #+STARTUP: logrepeat
  2023. #+STARTUP: lognoterepeat
  2024. #+STARTUP: nologrepeat
  2025. You can have local logging settings for a subtree by setting the LOGGING
  2026. property to one or more of these keywords."
  2027. :group 'org-todo
  2028. :group 'org-progress
  2029. :type '(choice
  2030. (const :tag "Don't force a record" nil)
  2031. (const :tag "Force recording the DONE state" time)
  2032. (const :tag "Force recording a note with the DONE state" note)))
  2033. (defgroup org-priorities nil
  2034. "Priorities in Org-mode."
  2035. :tag "Org Priorities"
  2036. :group 'org-todo)
  2037. (defcustom org-enable-priority-commands t
  2038. "Non-nil means priority commands are active.
  2039. When nil, these commands will be disabled, so that you never accidentally
  2040. set a priority."
  2041. :group 'org-priorities
  2042. :type 'boolean)
  2043. (defcustom org-highest-priority ?A
  2044. "The highest priority of TODO items. A character like ?A, ?B etc.
  2045. Must have a smaller ASCII number than `org-lowest-priority'."
  2046. :group 'org-priorities
  2047. :type 'character)
  2048. (defcustom org-lowest-priority ?C
  2049. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2050. Must have a larger ASCII number than `org-highest-priority'."
  2051. :group 'org-priorities
  2052. :type 'character)
  2053. (defcustom org-default-priority ?B
  2054. "The default priority of TODO items.
  2055. This is the priority an item get if no explicit priority is given."
  2056. :group 'org-priorities
  2057. :type 'character)
  2058. (defcustom org-priority-start-cycle-with-default t
  2059. "Non-nil means start with default priority when starting to cycle.
  2060. When this is nil, the first step in the cycle will be (depending on the
  2061. command used) one higher or lower that the default priority."
  2062. :group 'org-priorities
  2063. :type 'boolean)
  2064. (defgroup org-time nil
  2065. "Options concerning time stamps and deadlines in Org-mode."
  2066. :tag "Org Time"
  2067. :group 'org)
  2068. (defcustom org-insert-labeled-timestamps-at-point nil
  2069. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2070. When nil, these labeled time stamps are forces into the second line of an
  2071. entry, just after the headline. When scheduling from the global TODO list,
  2072. the time stamp will always be forced into the second line."
  2073. :group 'org-time
  2074. :type 'boolean)
  2075. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2076. "Formats for `format-time-string' which are used for time stamps.
  2077. It is not recommended to change this constant.")
  2078. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2079. "Number of minutes to round time stamps to.
  2080. These are two values, the first applies when first creating a time stamp.
  2081. The second applies when changing it with the commands `S-up' and `S-down'.
  2082. When changing the time stamp, this means that it will change in steps
  2083. of N minutes, as given by the second value.
  2084. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2085. numbers should be factors of 60, so for example 5, 10, 15.
  2086. When this is larger than 1, you can still force an exact time-stamp by using
  2087. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  2088. and by using a prefix arg to `S-up/down' to specify the exact number
  2089. of minutes to shift."
  2090. :group 'org-time
  2091. :get '(lambda (var) ; Make sure both elements are there
  2092. (if (integerp (default-value var))
  2093. (list (default-value var) 5)
  2094. (default-value var)))
  2095. :type '(list
  2096. (integer :tag "when inserting times")
  2097. (integer :tag "when modifying times")))
  2098. ;; Normalize old customizations of this variable.
  2099. (when (integerp org-time-stamp-rounding-minutes)
  2100. (setq org-time-stamp-rounding-minutes
  2101. (list org-time-stamp-rounding-minutes
  2102. org-time-stamp-rounding-minutes)))
  2103. (defcustom org-display-custom-times nil
  2104. "Non-nil means overlay custom formats over all time stamps.
  2105. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2106. To turn this on on a per-file basis, insert anywhere in the file:
  2107. #+STARTUP: customtime"
  2108. :group 'org-time
  2109. :set 'set-default
  2110. :type 'sexp)
  2111. (make-variable-buffer-local 'org-display-custom-times)
  2112. (defcustom org-time-stamp-custom-formats
  2113. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2114. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2115. These are overlayed over the default ISO format if the variable
  2116. `org-display-custom-times' is set. Time like %H:%M should be at the
  2117. end of the second format. The custom formats are also honored by export
  2118. commands, if custom time display is turned on at the time of export."
  2119. :group 'org-time
  2120. :type 'sexp)
  2121. (defun org-time-stamp-format (&optional long inactive)
  2122. "Get the right format for a time string."
  2123. (let ((f (if long (cdr org-time-stamp-formats)
  2124. (car org-time-stamp-formats))))
  2125. (if inactive
  2126. (concat "[" (substring f 1 -1) "]")
  2127. f)))
  2128. (defcustom org-time-clocksum-format "%d:%02d"
  2129. "The format string used when creating CLOCKSUM lines, or when
  2130. org-mode generates a time duration."
  2131. :group 'org-time
  2132. :type 'string)
  2133. (defcustom org-time-clocksum-use-fractional nil
  2134. "If non-nil, \\[org-clock-display] uses fractional times.
  2135. org-mode generates a time duration."
  2136. :group 'org-time
  2137. :type 'boolean)
  2138. (defcustom org-time-clocksum-fractional-format "%.2f"
  2139. "The format string used when creating CLOCKSUM lines, or when
  2140. org-mode generates a time duration."
  2141. :group 'org-time
  2142. :type 'string)
  2143. (defcustom org-deadline-warning-days 14
  2144. "No. of days before expiration during which a deadline becomes active.
  2145. This variable governs the display in sparse trees and in the agenda.
  2146. When 0 or negative, it means use this number (the absolute value of it)
  2147. even if a deadline has a different individual lead time specified.
  2148. Custom commands can set this variable in the options section."
  2149. :group 'org-time
  2150. :group 'org-agenda-daily/weekly
  2151. :type 'integer)
  2152. (defcustom org-read-date-prefer-future t
  2153. "Non-nil means assume future for incomplete date input from user.
  2154. This affects the following situations:
  2155. 1. The user gives a month but not a year.
  2156. For example, if it is april and you enter \"feb 2\", this will be read
  2157. as feb 2, *next* year. \"May 5\", however, will be this year.
  2158. 2. The user gives a day, but no month.
  2159. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2160. read this as the third of *next* month. However, if you enter \"17\",
  2161. it will be considered as *this* month.
  2162. If you set this variable to the symbol `time', then also the following
  2163. will work:
  2164. 3. If the user gives a time, but no day. If the time is before now,
  2165. to will be interpreted as tomorrow.
  2166. Currently none of this works for ISO week specifications.
  2167. When this option is nil, the current day, month and year will always be
  2168. used as defaults."
  2169. :group 'org-time
  2170. :type '(choice
  2171. (const :tag "Never" nil)
  2172. (const :tag "Check month and day" t)
  2173. (const :tag "Check month, day, and time" time)))
  2174. (defcustom org-read-date-display-live t
  2175. "Non-nil means display current interpretation of date prompt live.
  2176. This display will be in an overlay, in the minibuffer."
  2177. :group 'org-time
  2178. :type 'boolean)
  2179. (defcustom org-read-date-popup-calendar t
  2180. "Non-nil means pop up a calendar when prompting for a date.
  2181. In the calendar, the date can be selected with mouse-1. However, the
  2182. minibuffer will also be active, and you can simply enter the date as well.
  2183. When nil, only the minibuffer will be available."
  2184. :group 'org-time
  2185. :type 'boolean)
  2186. (if (fboundp 'defvaralias)
  2187. (defvaralias 'org-popup-calendar-for-date-prompt
  2188. 'org-read-date-popup-calendar))
  2189. (defcustom org-read-date-minibuffer-setup-hook nil
  2190. "Hook to be used to set up keys for the date/time interface.
  2191. Add key definitions to `minibuffer-local-map', which will be a temporary
  2192. copy."
  2193. :group 'org-time
  2194. :type 'hook)
  2195. (defcustom org-extend-today-until 0
  2196. "The hour when your day really ends. Must be an integer.
  2197. This has influence for the following applications:
  2198. - When switching the agenda to \"today\". It it is still earlier than
  2199. the time given here, the day recognized as TODAY is actually yesterday.
  2200. - When a date is read from the user and it is still before the time given
  2201. here, the current date and time will be assumed to be yesterday, 23:59.
  2202. Also, timestamps inserted in remember templates follow this rule.
  2203. IMPORTANT: This is a feature whose implementation is and likely will
  2204. remain incomplete. Really, it is only here because past midnight seems to
  2205. be the favorite working time of John Wiegley :-)"
  2206. :group 'org-time
  2207. :type 'integer)
  2208. (defcustom org-edit-timestamp-down-means-later nil
  2209. "Non-nil means S-down will increase the time in a time stamp.
  2210. When nil, S-up will increase."
  2211. :group 'org-time
  2212. :type 'boolean)
  2213. (defcustom org-calendar-follow-timestamp-change t
  2214. "Non-nil means make the calendar window follow timestamp changes.
  2215. When a timestamp is modified and the calendar window is visible, it will be
  2216. moved to the new date."
  2217. :group 'org-time
  2218. :type 'boolean)
  2219. (defgroup org-tags nil
  2220. "Options concerning tags in Org-mode."
  2221. :tag "Org Tags"
  2222. :group 'org)
  2223. (defcustom org-tag-alist nil
  2224. "List of tags allowed in Org-mode files.
  2225. When this list is nil, Org-mode will base TAG input on what is already in the
  2226. buffer.
  2227. The value of this variable is an alist, the car of each entry must be a
  2228. keyword as a string, the cdr may be a character that is used to select
  2229. that tag through the fast-tag-selection interface.
  2230. See the manual for details."
  2231. :group 'org-tags
  2232. :type '(repeat
  2233. (choice
  2234. (cons (string :tag "Tag name")
  2235. (character :tag "Access char"))
  2236. (list :tag "Start radio group"
  2237. (const :startgroup)
  2238. (option (string :tag "Group description")))
  2239. (list :tag "End radio group"
  2240. (const :endgroup)
  2241. (option (string :tag "Group description")))
  2242. (const :tag "New line" (:newline)))))
  2243. (defcustom org-tag-persistent-alist nil
  2244. "List of tags that will always appear in all Org-mode files.
  2245. This is in addition to any in buffer settings or customizations
  2246. of `org-tag-alist'.
  2247. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2248. The value of this variable is an alist, the car of each entry must be a
  2249. keyword as a string, the cdr may be a character that is used to select
  2250. that tag through the fast-tag-selection interface.
  2251. See the manual for details.
  2252. To disable these tags on a per-file basis, insert anywhere in the file:
  2253. #+STARTUP: noptag"
  2254. :group 'org-tags
  2255. :type '(repeat
  2256. (choice
  2257. (cons (string :tag "Tag name")
  2258. (character :tag "Access char"))
  2259. (const :tag "Start radio group" (:startgroup))
  2260. (const :tag "End radio group" (:endgroup))
  2261. (const :tag "New line" (:newline)))))
  2262. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2263. "If non-nil, always offer completion for all tags of all agenda files.
  2264. Instead of customizing this variable directly, you might want to
  2265. set it locally for remember buffers, because there no list of
  2266. tags in that file can be created dynamically (there are none).
  2267. (add-hook 'org-remember-mode-hook
  2268. (lambda ()
  2269. (set (make-local-variable
  2270. 'org-complete-tags-always-offer-all-agenda-tags)
  2271. t)))"
  2272. :group 'org-tags
  2273. :type 'boolean)
  2274. (defvar org-file-tags nil
  2275. "List of tags that can be inherited by all entries in the file.
  2276. The tags will be inherited if the variable `org-use-tag-inheritance'
  2277. says they should be.
  2278. This variable is populated from #+FILETAGS lines.")
  2279. (defcustom org-use-fast-tag-selection 'auto
  2280. "Non-nil means use fast tag selection scheme.
  2281. This is a special interface to select and deselect tags with single keys.
  2282. When nil, fast selection is never used.
  2283. When the symbol `auto', fast selection is used if and only if selection
  2284. characters for tags have been configured, either through the variable
  2285. `org-tag-alist' or through a #+TAGS line in the buffer.
  2286. When t, fast selection is always used and selection keys are assigned
  2287. automatically if necessary."
  2288. :group 'org-tags
  2289. :type '(choice
  2290. (const :tag "Always" t)
  2291. (const :tag "Never" nil)
  2292. (const :tag "When selection characters are configured" 'auto)))
  2293. (defcustom org-fast-tag-selection-single-key nil
  2294. "Non-nil means fast tag selection exits after first change.
  2295. When nil, you have to press RET to exit it.
  2296. During fast tag selection, you can toggle this flag with `C-c'.
  2297. This variable can also have the value `expert'. In this case, the window
  2298. displaying the tags menu is not even shown, until you press C-c again."
  2299. :group 'org-tags
  2300. :type '(choice
  2301. (const :tag "No" nil)
  2302. (const :tag "Yes" t)
  2303. (const :tag "Expert" expert)))
  2304. (defvar org-fast-tag-selection-include-todo nil
  2305. "Non-nil means fast tags selection interface will also offer TODO states.
  2306. This is an undocumented feature, you should not rely on it.")
  2307. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2308. "The column to which tags should be indented in a headline.
  2309. If this number is positive, it specifies the column. If it is negative,
  2310. it means that the tags should be flushright to that column. For example,
  2311. -80 works well for a normal 80 character screen."
  2312. :group 'org-tags
  2313. :type 'integer)
  2314. (defcustom org-auto-align-tags t
  2315. "Non-nil means realign tags after pro/demotion of TODO state change.
  2316. These operations change the length of a headline and therefore shift
  2317. the tags around. With this options turned on, after each such operation
  2318. the tags are again aligned to `org-tags-column'."
  2319. :group 'org-tags
  2320. :type 'boolean)
  2321. (defcustom org-use-tag-inheritance t
  2322. "Non-nil means tags in levels apply also for sublevels.
  2323. When nil, only the tags directly given in a specific line apply there.
  2324. This may also be a list of tags that should be inherited, or a regexp that
  2325. matches tags that should be inherited. Additional control is possible
  2326. with the variable `org-tags-exclude-from-inheritance' which gives an
  2327. explicit list of tags to be excluded from inheritance., even if the value of
  2328. `org-use-tag-inheritance' would select it for inheritance.
  2329. If this option is t, a match early-on in a tree can lead to a large
  2330. number of matches in the subtree when constructing the agenda or creating
  2331. a sparse tree. If you only want to see the first match in a tree during
  2332. a search, check out the variable `org-tags-match-list-sublevels'."
  2333. :group 'org-tags
  2334. :type '(choice
  2335. (const :tag "Not" nil)
  2336. (const :tag "Always" t)
  2337. (repeat :tag "Specific tags" (string :tag "Tag"))
  2338. (regexp :tag "Tags matched by regexp")))
  2339. (defcustom org-tags-exclude-from-inheritance nil
  2340. "List of tags that should never be inherited.
  2341. This is a way to exclude a few tags from inheritance. For way to do
  2342. the opposite, to actively allow inheritance for selected tags,
  2343. see the variable `org-use-tag-inheritance'."
  2344. :group 'org-tags
  2345. :type '(repeat (string :tag "Tag")))
  2346. (defun org-tag-inherit-p (tag)
  2347. "Check if TAG is one that should be inherited."
  2348. (cond
  2349. ((member tag org-tags-exclude-from-inheritance) nil)
  2350. ((eq org-use-tag-inheritance t) t)
  2351. ((not org-use-tag-inheritance) nil)
  2352. ((stringp org-use-tag-inheritance)
  2353. (string-match org-use-tag-inheritance tag))
  2354. ((listp org-use-tag-inheritance)
  2355. (member tag org-use-tag-inheritance))
  2356. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2357. (defcustom org-tags-match-list-sublevels t
  2358. "Non-nil means list also sublevels of headlines matching a search.
  2359. This variable applies to tags/property searches, and also to stuck
  2360. projects because this search is based on a tags match as well.
  2361. When set to the symbol `indented', sublevels are indented with
  2362. leading dots.
  2363. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2364. the sublevels of a headline matching a tag search often also match
  2365. the same search. Listing all of them can create very long lists.
  2366. Setting this variable to nil causes subtrees of a match to be skipped.
  2367. This variable is semi-obsolete and probably should always be true. It
  2368. is better to limit inheritance to certain tags using the variables
  2369. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2370. :group 'org-tags
  2371. :type '(choice
  2372. (const :tag "No, don't list them" nil)
  2373. (const :tag "Yes, do list them" t)
  2374. (const :tag "List them, indented with leading dots" indented)))
  2375. (defcustom org-tags-sort-function nil
  2376. "When set, tags are sorted using this function as a comparator"
  2377. :group 'org-tags
  2378. :type '(choice
  2379. (const :tag "No sorting" nil)
  2380. (const :tag "Alphabetical" string<)
  2381. (const :tag "Reverse alphabetical" string>)
  2382. (function :tag "Custom function" nil)))
  2383. (defvar org-tags-history nil
  2384. "History of minibuffer reads for tags.")
  2385. (defvar org-last-tags-completion-table nil
  2386. "The last used completion table for tags.")
  2387. (defvar org-after-tags-change-hook nil
  2388. "Hook that is run after the tags in a line have changed.")
  2389. (defgroup org-properties nil
  2390. "Options concerning properties in Org-mode."
  2391. :tag "Org Properties"
  2392. :group 'org)
  2393. (defcustom org-property-format "%-10s %s"
  2394. "How property key/value pairs should be formatted by `indent-line'.
  2395. When `indent-line' hits a property definition, it will format the line
  2396. according to this format, mainly to make sure that the values are
  2397. lined-up with respect to each other."
  2398. :group 'org-properties
  2399. :type 'string)
  2400. (defcustom org-use-property-inheritance nil
  2401. "Non-nil means properties apply also for sublevels.
  2402. This setting is chiefly used during property searches. Turning it on can
  2403. cause significant overhead when doing a search, which is why it is not
  2404. on by default.
  2405. When nil, only the properties directly given in the current entry count.
  2406. When t, every property is inherited. The value may also be a list of
  2407. properties that should have inheritance, or a regular expression matching
  2408. properties that should be inherited.
  2409. However, note that some special properties use inheritance under special
  2410. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2411. and the properties ending in \"_ALL\" when they are used as descriptor
  2412. for valid values of a property.
  2413. Note for programmers:
  2414. When querying an entry with `org-entry-get', you can control if inheritance
  2415. should be used. By default, `org-entry-get' looks only at the local
  2416. properties. You can request inheritance by setting the inherit argument
  2417. to t (to force inheritance) or to `selective' (to respect the setting
  2418. in this variable)."
  2419. :group 'org-properties
  2420. :type '(choice
  2421. (const :tag "Not" nil)
  2422. (const :tag "Always" t)
  2423. (repeat :tag "Specific properties" (string :tag "Property"))
  2424. (regexp :tag "Properties matched by regexp")))
  2425. (defun org-property-inherit-p (property)
  2426. "Check if PROPERTY is one that should be inherited."
  2427. (cond
  2428. ((eq org-use-property-inheritance t) t)
  2429. ((not org-use-property-inheritance) nil)
  2430. ((stringp org-use-property-inheritance)
  2431. (string-match org-use-property-inheritance property))
  2432. ((listp org-use-property-inheritance)
  2433. (member property org-use-property-inheritance))
  2434. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2435. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2436. "The default column format, if no other format has been defined.
  2437. This variable can be set on the per-file basis by inserting a line
  2438. #+COLUMNS: %25ITEM ....."
  2439. :group 'org-properties
  2440. :type 'string)
  2441. (defcustom org-columns-ellipses ".."
  2442. "The ellipses to be used when a field in column view is truncated.
  2443. When this is the empty string, as many characters as possible are shown,
  2444. but then there will be no visual indication that the field has been truncated.
  2445. When this is a string of length N, the last N characters of a truncated
  2446. field are replaced by this string. If the column is narrower than the
  2447. ellipses string, only part of the ellipses string will be shown."
  2448. :group 'org-properties
  2449. :type 'string)
  2450. (defcustom org-columns-modify-value-for-display-function nil
  2451. "Function that modifies values for display in column view.
  2452. For example, it can be used to cut out a certain part from a time stamp.
  2453. The function must take 2 arguments:
  2454. column-title The title of the column (*not* the property name)
  2455. value The value that should be modified.
  2456. The function should return the value that should be displayed,
  2457. or nil if the normal value should be used."
  2458. :group 'org-properties
  2459. :type 'function)
  2460. (defcustom org-effort-property "Effort"
  2461. "The property that is being used to keep track of effort estimates.
  2462. Effort estimates given in this property need to have the format H:MM."
  2463. :group 'org-properties
  2464. :group 'org-progress
  2465. :type '(string :tag "Property"))
  2466. (defconst org-global-properties-fixed
  2467. '(("VISIBILITY_ALL" . "folded children content all")
  2468. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2469. "List of property/value pairs that can be inherited by any entry.
  2470. These are fixed values, for the preset properties. The user variable
  2471. that can be used to add to this list is `org-global-properties'.
  2472. The entries in this list are cons cells where the car is a property
  2473. name and cdr is a string with the value. If the value represents
  2474. multiple items like an \"_ALL\" property, separate the items by
  2475. spaces.")
  2476. (defcustom org-global-properties nil
  2477. "List of property/value pairs that can be inherited by any entry.
  2478. This list will be combined with the constant `org-global-properties-fixed'.
  2479. The entries in this list are cons cells where the car is a property
  2480. name and cdr is a string with the value.
  2481. You can set buffer-local values for the same purpose in the variable
  2482. `org-file-properties' this by adding lines like
  2483. #+PROPERTY: NAME VALUE"
  2484. :group 'org-properties
  2485. :type '(repeat
  2486. (cons (string :tag "Property")
  2487. (string :tag "Value"))))
  2488. (defvar org-file-properties nil
  2489. "List of property/value pairs that can be inherited by any entry.
  2490. Valid for the current buffer.
  2491. This variable is populated from #+PROPERTY lines.")
  2492. (make-variable-buffer-local 'org-file-properties)
  2493. (defgroup org-agenda nil
  2494. "Options concerning agenda views in Org-mode."
  2495. :tag "Org Agenda"
  2496. :group 'org)
  2497. (defvar org-category nil
  2498. "Variable used by org files to set a category for agenda display.
  2499. Such files should use a file variable to set it, for example
  2500. # -*- mode: org; org-category: \"ELisp\"
  2501. or contain a special line
  2502. #+CATEGORY: ELisp
  2503. If the file does not specify a category, then file's base name
  2504. is used instead.")
  2505. (make-variable-buffer-local 'org-category)
  2506. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2507. (defcustom org-agenda-files nil
  2508. "The files to be used for agenda display.
  2509. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2510. \\[org-remove-file]. You can also use customize to edit the list.
  2511. If an entry is a directory, all files in that directory that are matched by
  2512. `org-agenda-file-regexp' will be part of the file list.
  2513. If the value of the variable is not a list but a single file name, then
  2514. the list of agenda files is actually stored and maintained in that file, one
  2515. agenda file per line. In this file paths can be given relative to
  2516. `org-directory'. Tilde expansion and environment variable substitution
  2517. are also made."
  2518. :group 'org-agenda
  2519. :type '(choice
  2520. (repeat :tag "List of files and directories" file)
  2521. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2522. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2523. "Regular expression to match files for `org-agenda-files'.
  2524. If any element in the list in that variable contains a directory instead
  2525. of a normal file, all files in that directory that are matched by this
  2526. regular expression will be included."
  2527. :group 'org-agenda
  2528. :type 'regexp)
  2529. (defcustom org-agenda-text-search-extra-files nil
  2530. "List of extra files to be searched by text search commands.
  2531. These files will be search in addition to the agenda files by the
  2532. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2533. Note that these files will only be searched for text search commands,
  2534. not for the other agenda views like todo lists, tag searches or the weekly
  2535. agenda. This variable is intended to list notes and possibly archive files
  2536. that should also be searched by these two commands.
  2537. In fact, if the first element in the list is the symbol `agenda-archives',
  2538. than all archive files of all agenda files will be added to the search
  2539. scope."
  2540. :group 'org-agenda
  2541. :type '(set :greedy t
  2542. (const :tag "Agenda Archives" agenda-archives)
  2543. (repeat :inline t (file))))
  2544. (if (fboundp 'defvaralias)
  2545. (defvaralias 'org-agenda-multi-occur-extra-files
  2546. 'org-agenda-text-search-extra-files))
  2547. (defcustom org-agenda-skip-unavailable-files nil
  2548. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2549. A nil value means to remove them, after a query, from the list."
  2550. :group 'org-agenda
  2551. :type 'boolean)
  2552. (defcustom org-calendar-to-agenda-key [?c]
  2553. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2554. The command `org-calendar-goto-agenda' will be bound to this key. The
  2555. default is the character `c' because then `c' can be used to switch back and
  2556. forth between agenda and calendar."
  2557. :group 'org-agenda
  2558. :type 'sexp)
  2559. (defcustom org-calendar-agenda-action-key [?k]
  2560. "The key to be installed in `calendar-mode-map' for agenda-action.
  2561. The command `org-agenda-action' will be bound to this key. The
  2562. default is the character `k' because we use the same key in the agenda."
  2563. :group 'org-agenda
  2564. :type 'sexp)
  2565. (defcustom org-calendar-insert-diary-entry-key [?i]
  2566. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2567. This option is irrelevant until `org-agenda-diary-file' has been configured
  2568. to point to an Org-mode file. When that is the case, the command
  2569. `org-agenda-diary-entry' will be bound to the key given here, by default
  2570. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2571. if you want to continue doing this, you need to change this to a different
  2572. key."
  2573. :group 'org-agenda
  2574. :type 'sexp)
  2575. (defcustom org-agenda-diary-file 'diary-file
  2576. "File to which to add new entries with the `i' key in agenda and calendar.
  2577. When this is the symbol `diary-file', the functionality in the Emacs
  2578. calendar will be used to add entries to the `diary-file'. But when this
  2579. points to a file, `org-agenda-diary-entry' will be used instead."
  2580. :group 'org-agenda
  2581. :type '(choice
  2582. (const :tag "The standard Emacs diary file" diary-file)
  2583. (file :tag "Special Org file diary entries")))
  2584. (eval-after-load "calendar"
  2585. '(progn
  2586. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2587. 'org-calendar-goto-agenda)
  2588. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2589. 'org-agenda-action)
  2590. (add-hook 'calendar-mode-hook
  2591. (lambda ()
  2592. (unless (eq org-agenda-diary-file 'diary-file)
  2593. (define-key calendar-mode-map
  2594. org-calendar-insert-diary-entry-key
  2595. 'org-agenda-diary-entry))))))
  2596. (defgroup org-latex nil
  2597. "Options for embedding LaTeX code into Org-mode."
  2598. :tag "Org LaTeX"
  2599. :group 'org)
  2600. (defcustom org-format-latex-options
  2601. '(:foreground default :background default :scale 1.0
  2602. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2603. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2604. "Options for creating images from LaTeX fragments.
  2605. This is a property list with the following properties:
  2606. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2607. `default' means use the foreground of the default face.
  2608. :background the background color, or \"Transparent\".
  2609. `default' means use the background of the default face.
  2610. :scale a scaling factor for the size of the images.
  2611. :html-foreground, :html-background, :html-scale
  2612. the same numbers for HTML export.
  2613. :matchers a list indicating which matchers should be used to
  2614. find LaTeX fragments. Valid members of this list are:
  2615. \"begin\" find environments
  2616. \"$1\" find single characters surrounded by $.$
  2617. \"$\" find math expressions surrounded by $...$
  2618. \"$$\" find math expressions surrounded by $$....$$
  2619. \"\\(\" find math expressions surrounded by \\(...\\)
  2620. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2621. :group 'org-latex
  2622. :type 'plist)
  2623. (defcustom org-format-latex-signal-error t
  2624. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2625. When nil, just push out a message."
  2626. :group 'org-latex
  2627. :type 'boolean)
  2628. (defcustom org-format-latex-header "\\documentclass{article}
  2629. \\usepackage[usenames]{color}
  2630. \\usepackage{amsmath}
  2631. \\usepackage[mathscr]{eucal}
  2632. \\pagestyle{empty} % do not remove
  2633. \[PACKAGES]
  2634. \[DEFAULT-PACKAGES]
  2635. % The settings below are copied from fullpage.sty
  2636. \\setlength{\\textwidth}{\\paperwidth}
  2637. \\addtolength{\\textwidth}{-3cm}
  2638. \\setlength{\\oddsidemargin}{1.5cm}
  2639. \\addtolength{\\oddsidemargin}{-2.54cm}
  2640. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2641. \\setlength{\\textheight}{\\paperheight}
  2642. \\addtolength{\\textheight}{-\\headheight}
  2643. \\addtolength{\\textheight}{-\\headsep}
  2644. \\addtolength{\\textheight}{-\\footskip}
  2645. \\addtolength{\\textheight}{-3cm}
  2646. \\setlength{\\topmargin}{1.5cm}
  2647. \\addtolength{\\topmargin}{-2.54cm}"
  2648. "The document header used for processing LaTeX fragments.
  2649. It is imperative that this header make sure that no page number
  2650. appears on the page. The package defined in the variables
  2651. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2652. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2653. will be appended."
  2654. :group 'org-latex
  2655. :type 'string)
  2656. (defvar org-format-latex-header-extra nil)
  2657. (defun org-set-packages-alist (var val)
  2658. "Set the packages alist and make sure it has 3 elements per entry."
  2659. (set var (mapcar (lambda (x)
  2660. (if (and (consp x) (= (length x) 2))
  2661. (list (car x) (nth 1 x) t)
  2662. x))
  2663. val)))
  2664. (defun org-get-packages-alist (var)
  2665. "Get the packages alist and make sure it has 3 elements per entry."
  2666. (mapcar (lambda (x)
  2667. (if (and (consp x) (= (length x) 2))
  2668. (list (car x) (nth 1 x) t)
  2669. x))
  2670. (default-value var)))
  2671. ;; The following variables are defined here because is it also used
  2672. ;; when formatting latex fragments. Originally it was part of the
  2673. ;; LaTeX exporter, which is why the name includes "export".
  2674. (defcustom org-export-latex-default-packages-alist
  2675. '(("AUTO" "inputenc" t)
  2676. ("T1" "fontenc" t)
  2677. ("" "fixltx2e" nil)
  2678. ("" "graphicx" t)
  2679. ("" "longtable" nil)
  2680. ("" "float" nil)
  2681. ("" "wrapfig" nil)
  2682. ("" "soul" t)
  2683. ("" "t1enc" t)
  2684. ("" "textcomp" t)
  2685. ("" "marvosym" t)
  2686. ("" "wasysym" t)
  2687. ("" "latexsym" t)
  2688. ("" "amssymb" t)
  2689. ("" "hyperref" nil)
  2690. "\\tolerance=1000"
  2691. )
  2692. "Alist of default packages to be inserted in the header.
  2693. Change this only if one of the packages here causes an incompatibility
  2694. with another package you are using.
  2695. The packages in this list are needed by one part or another of Org-mode
  2696. to function properly.
  2697. - inputenc, fontenc, t1enc: for basic font and character selection
  2698. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2699. for interpreting the entities in `org-entities'. You can skip some of these
  2700. packages if you don't use any of the symbols in it.
  2701. - graphicx: for including images
  2702. - float, wrapfig: for figure placement
  2703. - longtable: for long tables
  2704. - hyperref: for cross references
  2705. Therefore you should not modify this variable unless you know what you
  2706. are doing. The one reason to change it anyway is that you might be loading
  2707. some other package that conflicts with one of the default packages.
  2708. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2709. If SNIPPET-FLAG is t, the package also needs to be included when
  2710. compiling LaTeX snippets into images for inclusion into HTML."
  2711. :group 'org-export-latex
  2712. :set 'org-set-packages-alist
  2713. :get 'org-get-packages-alist
  2714. :type '(repeat
  2715. (choice
  2716. (list :tag "options/package pair"
  2717. (string :tag "options")
  2718. (string :tag "package")
  2719. (boolean :tag "Snippet"))
  2720. (string :tag "A line of LaTeX"))))
  2721. (defcustom org-export-latex-packages-alist nil
  2722. "Alist of packages to be inserted in every LaTeX header.
  2723. These will be inserted after `org-export-latex-default-packages-alist'.
  2724. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2725. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2726. turning LaTeX snippets into images for inclusion into HTML.
  2727. Make sure that you only list packages here which:
  2728. - you want in every file
  2729. - do not conflict with the default packages in
  2730. `org-export-latex-default-packages-alist'
  2731. - do not conflict with the setup in `org-format-latex-header'."
  2732. :group 'org-export-latex
  2733. :set 'org-set-packages-alist
  2734. :get 'org-get-packages-alist
  2735. :type '(repeat
  2736. (choice
  2737. (list :tag "options/package pair"
  2738. (string :tag "options")
  2739. (string :tag "package")
  2740. (boolean :tag "Snippet"))
  2741. (string :tag "A line of LaTeX"))))
  2742. (defgroup org-appearance nil
  2743. "Settings for Org-mode appearance."
  2744. :tag "Org Appearance"
  2745. :group 'org)
  2746. (defcustom org-level-color-stars-only nil
  2747. "Non-nil means fontify only the stars in each headline.
  2748. When nil, the entire headline is fontified.
  2749. Changing it requires restart of `font-lock-mode' to become effective
  2750. also in regions already fontified."
  2751. :group 'org-appearance
  2752. :type 'boolean)
  2753. (defcustom org-hide-leading-stars nil
  2754. "Non-nil means hide the first N-1 stars in a headline.
  2755. This works by using the face `org-hide' for these stars. This
  2756. face is white for a light background, and black for a dark
  2757. background. You may have to customize the face `org-hide' to
  2758. make this work.
  2759. Changing it requires restart of `font-lock-mode' to become effective
  2760. also in regions already fontified.
  2761. You may also set this on a per-file basis by adding one of the following
  2762. lines to the buffer:
  2763. #+STARTUP: hidestars
  2764. #+STARTUP: showstars"
  2765. :group 'org-appearance
  2766. :type 'boolean)
  2767. (defcustom org-hidden-keywords nil
  2768. "List of keywords that should be hidden when typed in the org buffer.
  2769. For example, add #+TITLE to this list in order to make the
  2770. document title appear in the buffer without the initial #+TITLE:
  2771. keyword."
  2772. :group 'org-appearance
  2773. :type '(set (const :tag "#+AUTHOR" author)
  2774. (const :tag "#+DATE" date)
  2775. (const :tag "#+EMAIL" email)
  2776. (const :tag "#+TITLE" title)))
  2777. (defcustom org-fontify-done-headline nil
  2778. "Non-nil means change the face of a headline if it is marked DONE.
  2779. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2780. When this is non-nil, the headline after the keyword is set to the
  2781. `org-headline-done' as an additional indication."
  2782. :group 'org-appearance
  2783. :type 'boolean)
  2784. (defcustom org-fontify-emphasized-text t
  2785. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2786. Changing this variable requires a restart of Emacs to take effect."
  2787. :group 'org-appearance
  2788. :type 'boolean)
  2789. (defcustom org-fontify-whole-heading-line nil
  2790. "Non-nil means fontify the whole line for headings.
  2791. This is useful when setting a background color for the
  2792. org-level-* faces."
  2793. :group 'org-appearance
  2794. :type 'boolean)
  2795. (defcustom org-highlight-latex-fragments-and-specials nil
  2796. "Non-nil means fontify what is treated specially by the exporters."
  2797. :group 'org-appearance
  2798. :type 'boolean)
  2799. (defcustom org-hide-emphasis-markers nil
  2800. "Non-nil mean font-lock should hide the emphasis marker characters."
  2801. :group 'org-appearance
  2802. :type 'boolean)
  2803. (defcustom org-pretty-entities nil
  2804. "Non-nil means show entities as UTF8 characters.
  2805. When nil, the \\name form remains in the buffer."
  2806. :group 'org-appearance
  2807. :type 'boolean)
  2808. (defcustom org-pretty-entities-include-sub-superscripts t
  2809. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2810. :group 'org-appearance
  2811. :type 'boolean)
  2812. (defvar org-emph-re nil
  2813. "Regular expression for matching emphasis.
  2814. After a match, the match groups contain these elements:
  2815. 1 The character before the proper match, or empty at beginning of line
  2816. 2 The proper match, including the leading and trailing markers
  2817. 3 The leading marker like * or /, indicating the type of highlighting
  2818. 4 The text between the emphasis markers, not including the markers
  2819. 5 The character after the match, empty at the end of a line")
  2820. (defvar org-verbatim-re nil
  2821. "Regular expression for matching verbatim text.")
  2822. (defvar org-emphasis-regexp-components) ; defined just below
  2823. (defvar org-emphasis-alist) ; defined just below
  2824. (defun org-set-emph-re (var val)
  2825. "Set variable and compute the emphasis regular expression."
  2826. (set var val)
  2827. (when (and (boundp 'org-emphasis-alist)
  2828. (boundp 'org-emphasis-regexp-components)
  2829. org-emphasis-alist org-emphasis-regexp-components)
  2830. (let* ((e org-emphasis-regexp-components)
  2831. (pre (car e))
  2832. (post (nth 1 e))
  2833. (border (nth 2 e))
  2834. (body (nth 3 e))
  2835. (nl (nth 4 e))
  2836. (body1 (concat body "*?"))
  2837. (markers (mapconcat 'car org-emphasis-alist ""))
  2838. (vmarkers (mapconcat
  2839. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2840. org-emphasis-alist "")))
  2841. ;; make sure special characters appear at the right position in the class
  2842. (if (string-match "\\^" markers)
  2843. (setq markers (concat (replace-match "" t t markers) "^")))
  2844. (if (string-match "-" markers)
  2845. (setq markers (concat (replace-match "" t t markers) "-")))
  2846. (if (string-match "\\^" vmarkers)
  2847. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2848. (if (string-match "-" vmarkers)
  2849. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2850. (if (> nl 0)
  2851. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2852. (int-to-string nl) "\\}")))
  2853. ;; Make the regexp
  2854. (setq org-emph-re
  2855. (concat "\\([" pre "]\\|^\\)"
  2856. "\\("
  2857. "\\([" markers "]\\)"
  2858. "\\("
  2859. "[^" border "]\\|"
  2860. "[^" border "]"
  2861. body1
  2862. "[^" border "]"
  2863. "\\)"
  2864. "\\3\\)"
  2865. "\\([" post "]\\|$\\)"))
  2866. (setq org-verbatim-re
  2867. (concat "\\([" pre "]\\|^\\)"
  2868. "\\("
  2869. "\\([" vmarkers "]\\)"
  2870. "\\("
  2871. "[^" border "]\\|"
  2872. "[^" border "]"
  2873. body1
  2874. "[^" border "]"
  2875. "\\)"
  2876. "\\3\\)"
  2877. "\\([" post "]\\|$\\)")))))
  2878. (defcustom org-emphasis-regexp-components
  2879. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2880. "Components used to build the regular expression for emphasis.
  2881. This is a list with 6 entries. Terminology: In an emphasis string
  2882. like \" *strong word* \", we call the initial space PREMATCH, the final
  2883. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2884. and \"trong wor\" is the body. The different components in this variable
  2885. specify what is allowed/forbidden in each part:
  2886. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2887. post Chars allowed as postmatch. End of line will be allowed too.
  2888. border The chars *forbidden* as border characters.
  2889. body-regexp A regexp like \".\" to match a body character. Don't use
  2890. non-shy groups here, and don't allow newline here.
  2891. newline The maximum number of newlines allowed in an emphasis exp.
  2892. Use customize to modify this, or restart Emacs after changing it."
  2893. :group 'org-appearance
  2894. :set 'org-set-emph-re
  2895. :type '(list
  2896. (sexp :tag "Allowed chars in pre ")
  2897. (sexp :tag "Allowed chars in post ")
  2898. (sexp :tag "Forbidden chars in border ")
  2899. (sexp :tag "Regexp for body ")
  2900. (integer :tag "number of newlines allowed")
  2901. (option (boolean :tag "Please ignore this button"))))
  2902. (defcustom org-emphasis-alist
  2903. `(("*" bold "<b>" "</b>")
  2904. ("/" italic "<i>" "</i>")
  2905. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2906. ("=" org-code "<code>" "</code>" verbatim)
  2907. ("~" org-verbatim "<code>" "</code>" verbatim)
  2908. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2909. "<del>" "</del>")
  2910. )
  2911. "Special syntax for emphasized text.
  2912. Text starting and ending with a special character will be emphasized, for
  2913. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2914. characters, the face to be used by font-lock for highlighting in Org-mode
  2915. Emacs buffers, and the HTML tags to be used for this.
  2916. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2917. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  2918. Use customize to modify this, or restart Emacs after changing it."
  2919. :group 'org-appearance
  2920. :set 'org-set-emph-re
  2921. :type '(repeat
  2922. (list
  2923. (string :tag "Marker character")
  2924. (choice
  2925. (face :tag "Font-lock-face")
  2926. (plist :tag "Face property list"))
  2927. (string :tag "HTML start tag")
  2928. (string :tag "HTML end tag")
  2929. (option (const verbatim)))))
  2930. (defvar org-protecting-blocks
  2931. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2932. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2933. This is needed for font-lock setup.")
  2934. ;;; Miscellaneous options
  2935. (defgroup org-completion nil
  2936. "Completion in Org-mode."
  2937. :tag "Org Completion"
  2938. :group 'org)
  2939. (defcustom org-completion-use-ido nil
  2940. "Non-nil means use ido completion wherever possible.
  2941. Note that `ido-mode' must be active for this variable to be relevant.
  2942. If you decide to turn this variable on, you might well want to turn off
  2943. `org-outline-path-complete-in-steps'.
  2944. See also `org-completion-use-iswitchb'."
  2945. :group 'org-completion
  2946. :type 'boolean)
  2947. (defcustom org-completion-use-iswitchb nil
  2948. "Non-nil means use iswitchb completion wherever possible.
  2949. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2950. If you decide to turn this variable on, you might well want to turn off
  2951. `org-outline-path-complete-in-steps'.
  2952. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2953. :group 'org-completion
  2954. :type 'boolean)
  2955. (defcustom org-completion-fallback-command 'hippie-expand
  2956. "The expansion command called by \\[org-complete] in normal context.
  2957. Normal means no org-mode-specific context."
  2958. :group 'org-completion
  2959. :type 'function)
  2960. ;;; Functions and variables from their packages
  2961. ;; Declared here to avoid compiler warnings
  2962. ;; XEmacs only
  2963. (defvar outline-mode-menu-heading)
  2964. (defvar outline-mode-menu-show)
  2965. (defvar outline-mode-menu-hide)
  2966. (defvar zmacs-regions) ; XEmacs regions
  2967. ;; Emacs only
  2968. (defvar mark-active)
  2969. ;; Various packages
  2970. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2971. (declare-function calendar-forward-day "cal-move" (arg))
  2972. (declare-function calendar-goto-date "cal-move" (date))
  2973. (declare-function calendar-goto-today "cal-move" ())
  2974. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2975. (defvar calc-embedded-close-formula)
  2976. (defvar calc-embedded-open-formula)
  2977. (declare-function cdlatex-tab "ext:cdlatex" ())
  2978. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2979. (defvar font-lock-unfontify-region-function)
  2980. (declare-function iswitchb-read-buffer "iswitchb"
  2981. (prompt &optional default require-match start matches-set))
  2982. (defvar iswitchb-temp-buflist)
  2983. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2984. (defvar org-agenda-tags-todo-honor-ignore-options)
  2985. (declare-function org-agenda-skip "org-agenda" ())
  2986. (declare-function
  2987. org-format-agenda-item "org-agenda"
  2988. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2989. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2990. (declare-function org-agenda-change-all-lines "org-agenda"
  2991. (newhead hdmarker &optional fixface just-this))
  2992. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2993. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2994. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2995. (beg end))
  2996. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2997. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2998. "org-agenda" (&optional end))
  2999. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3000. (declare-function org-indent-mode "org-indent" (&optional arg))
  3001. (declare-function parse-time-string "parse-time" (string))
  3002. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3003. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3004. (defvar remember-data-file)
  3005. (defvar texmathp-why)
  3006. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3007. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3008. (defvar w3m-current-url)
  3009. (defvar w3m-current-title)
  3010. (defvar org-latex-regexps)
  3011. ;;; Autoload and prepare some org modules
  3012. ;; Some table stuff that needs to be defined here, because it is used
  3013. ;; by the functions setting up org-mode or checking for table context.
  3014. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3015. "Detects an org-type or table-type table.")
  3016. (defconst org-table-line-regexp "^[ \t]*|"
  3017. "Detects an org-type table line.")
  3018. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3019. "Detects an org-type table line.")
  3020. (defconst org-table-hline-regexp "^[ \t]*|-"
  3021. "Detects an org-type table hline.")
  3022. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3023. "Detects a table-type table hline.")
  3024. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3025. "Searching from within a table (any type) this finds the first line
  3026. outside the table.")
  3027. ;; Autoload the functions in org-table.el that are needed by functions here.
  3028. (eval-and-compile
  3029. (org-autoload "org-table"
  3030. '(org-table-align org-table-begin org-table-blank-field
  3031. org-table-convert org-table-convert-region org-table-copy-down
  3032. org-table-copy-region org-table-create
  3033. org-table-create-or-convert-from-region
  3034. org-table-create-with-table.el org-table-current-dline
  3035. org-table-cut-region org-table-delete-column org-table-edit-field
  3036. org-table-edit-formulas org-table-end org-table-eval-formula
  3037. org-table-export org-table-field-info
  3038. org-table-get-stored-formulas org-table-goto-column
  3039. org-table-hline-and-move org-table-import org-table-insert-column
  3040. org-table-insert-hline org-table-insert-row org-table-iterate
  3041. org-table-justify-field-maybe org-table-kill-row
  3042. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3043. org-table-move-column org-table-move-column-left
  3044. org-table-move-column-right org-table-move-row
  3045. org-table-move-row-down org-table-move-row-up
  3046. org-table-next-field org-table-next-row org-table-paste-rectangle
  3047. org-table-previous-field org-table-recalculate
  3048. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3049. org-table-toggle-coordinate-overlays
  3050. org-table-toggle-formula-debugger org-table-wrap-region
  3051. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  3052. (defun org-at-table-p (&optional table-type)
  3053. "Return t if the cursor is inside an org-type table.
  3054. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3055. (if org-enable-table-editor
  3056. (save-excursion
  3057. (beginning-of-line 1)
  3058. (looking-at (if table-type org-table-any-line-regexp
  3059. org-table-line-regexp)))
  3060. nil))
  3061. (defsubst org-table-p () (org-at-table-p))
  3062. (defun org-at-table.el-p ()
  3063. "Return t if and only if we are at a table.el table."
  3064. (and (org-at-table-p 'any)
  3065. (save-excursion
  3066. (goto-char (org-table-begin 'any))
  3067. (looking-at org-table1-hline-regexp))))
  3068. (defun org-table-recognize-table.el ()
  3069. "If there is a table.el table nearby, recognize it and move into it."
  3070. (if org-table-tab-recognizes-table.el
  3071. (if (org-at-table.el-p)
  3072. (progn
  3073. (beginning-of-line 1)
  3074. (if (looking-at org-table-dataline-regexp)
  3075. nil
  3076. (if (looking-at org-table1-hline-regexp)
  3077. (progn
  3078. (beginning-of-line 2)
  3079. (if (looking-at org-table-any-border-regexp)
  3080. (beginning-of-line -1)))))
  3081. (if (re-search-forward "|" (org-table-end t) t)
  3082. (progn
  3083. (require 'table)
  3084. (if (table--at-cell-p (point))
  3085. t
  3086. (message "recognizing table.el table...")
  3087. (table-recognize-table)
  3088. (message "recognizing table.el table...done")))
  3089. (error "This should not happen..."))
  3090. t)
  3091. nil)
  3092. nil))
  3093. (defun org-at-table-hline-p ()
  3094. "Return t if the cursor is inside a hline in a table."
  3095. (if org-enable-table-editor
  3096. (save-excursion
  3097. (beginning-of-line 1)
  3098. (looking-at org-table-hline-regexp))
  3099. nil))
  3100. (defvar org-table-clean-did-remove-column nil)
  3101. (defun org-table-map-tables (function &optional quietly)
  3102. "Apply FUNCTION to the start of all tables in the buffer."
  3103. (save-excursion
  3104. (save-restriction
  3105. (widen)
  3106. (goto-char (point-min))
  3107. (while (re-search-forward org-table-any-line-regexp nil t)
  3108. (unless quietly
  3109. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3110. (beginning-of-line 1)
  3111. (when (looking-at org-table-line-regexp)
  3112. (save-excursion (funcall function))
  3113. (or (looking-at org-table-line-regexp)
  3114. (forward-char 1)))
  3115. (re-search-forward org-table-any-border-regexp nil 1))))
  3116. (unless quietly (message "Mapping tables: done")))
  3117. ;; Declare and autoload functions from org-exp.el & Co
  3118. (declare-function org-default-export-plist "org-exp")
  3119. (declare-function org-infile-export-plist "org-exp")
  3120. (declare-function org-get-current-options "org-exp")
  3121. (eval-and-compile
  3122. (org-autoload "org-exp"
  3123. '(org-export org-export-visible
  3124. org-insert-export-options-template
  3125. org-table-clean-before-export))
  3126. (org-autoload "org-ascii"
  3127. '(org-export-as-ascii org-export-ascii-preprocess
  3128. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3129. org-export-region-as-ascii))
  3130. (org-autoload "org-latex"
  3131. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3132. org-replace-region-by-latex org-export-region-as-latex
  3133. org-export-as-latex org-export-as-pdf
  3134. org-export-as-pdf-and-open))
  3135. (org-autoload "org-html"
  3136. '(org-export-as-html-and-open
  3137. org-export-as-html-batch org-export-as-html-to-buffer
  3138. org-replace-region-by-html org-export-region-as-html
  3139. org-export-as-html))
  3140. (org-autoload "org-docbook"
  3141. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3142. org-replace-region-by-docbook org-export-region-as-docbook
  3143. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3144. org-export-as-docbook))
  3145. (org-autoload "org-icalendar"
  3146. '(org-export-icalendar-this-file
  3147. org-export-icalendar-all-agenda-files
  3148. org-export-icalendar-combine-agenda-files))
  3149. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3150. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3151. ;; Declare and autoload functions from org-agenda.el
  3152. (eval-and-compile
  3153. (org-autoload "org-agenda"
  3154. '(org-agenda org-agenda-list org-search-view
  3155. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3156. org-diary org-agenda-to-appt
  3157. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3158. ;; Autoload org-remember
  3159. (eval-and-compile
  3160. (org-autoload "org-remember"
  3161. '(org-remember-insinuate org-remember-annotation
  3162. org-remember-apply-template org-remember org-remember-handler)))
  3163. (eval-and-compile
  3164. (org-autoload "org-capture"
  3165. '(org-capture org-capture-insert-template-here
  3166. org-capture-import-remember-templates)))
  3167. ;; Autoload org-clock.el
  3168. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3169. (beg end))
  3170. (declare-function org-clock-update-mode-line "org-clock" ())
  3171. (declare-function org-resolve-clocks "org-clock"
  3172. (&optional also-non-dangling-p prompt last-valid))
  3173. (defvar org-clock-start-time)
  3174. (defvar org-clock-marker (make-marker)
  3175. "Marker recording the last clock-in.")
  3176. (defvar org-clock-hd-marker (make-marker)
  3177. "Marker recording the last clock-in, but the headline position.")
  3178. (defvar org-clock-heading ""
  3179. "The heading of the current clock entry.")
  3180. (defun org-clock-is-active ()
  3181. "Return non-nil if clock is currently running.
  3182. The return value is actually the clock marker."
  3183. (marker-buffer org-clock-marker))
  3184. (eval-and-compile
  3185. (org-autoload
  3186. "org-clock"
  3187. '(org-clock-in org-clock-out org-clock-cancel
  3188. org-clock-goto org-clock-sum org-clock-display
  3189. org-clock-remove-overlays org-clock-report
  3190. org-clocktable-shift org-dblock-write:clocktable
  3191. org-get-clocktable org-resolve-clocks)))
  3192. (defun org-clock-update-time-maybe ()
  3193. "If this is a CLOCK line, update it and return t.
  3194. Otherwise, return nil."
  3195. (interactive)
  3196. (save-excursion
  3197. (beginning-of-line 1)
  3198. (skip-chars-forward " \t")
  3199. (when (looking-at org-clock-string)
  3200. (let ((re (concat "[ \t]*" org-clock-string
  3201. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3202. "\\([ \t]*=>.*\\)?\\)?"))
  3203. ts te h m s neg)
  3204. (cond
  3205. ((not (looking-at re))
  3206. nil)
  3207. ((not (match-end 2))
  3208. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3209. (> org-clock-marker (point))
  3210. (<= org-clock-marker (point-at-eol)))
  3211. ;; The clock is running here
  3212. (setq org-clock-start-time
  3213. (apply 'encode-time
  3214. (org-parse-time-string (match-string 1))))
  3215. (org-clock-update-mode-line)))
  3216. (t
  3217. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3218. (end-of-line 1)
  3219. (setq ts (match-string 1)
  3220. te (match-string 3))
  3221. (setq s (- (org-float-time
  3222. (apply 'encode-time (org-parse-time-string te)))
  3223. (org-float-time
  3224. (apply 'encode-time (org-parse-time-string ts))))
  3225. neg (< s 0)
  3226. s (abs s)
  3227. h (floor (/ s 3600))
  3228. s (- s (* 3600 h))
  3229. m (floor (/ s 60))
  3230. s (- s (* 60 s)))
  3231. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3232. t))))))
  3233. (defun org-check-running-clock ()
  3234. "Check if the current buffer contains the running clock.
  3235. If yes, offer to stop it and to save the buffer with the changes."
  3236. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3237. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3238. (buffer-name))))
  3239. (org-clock-out)
  3240. (when (y-or-n-p "Save changed buffer?")
  3241. (save-buffer))))
  3242. (defun org-clocktable-try-shift (dir n)
  3243. "Check if this line starts a clock table, if yes, shift the time block."
  3244. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3245. (org-clocktable-shift dir n)))
  3246. ;; Autoload org-timer.el
  3247. (eval-and-compile
  3248. (org-autoload
  3249. "org-timer"
  3250. '(org-timer-start org-timer org-timer-item
  3251. org-timer-change-times-in-region
  3252. org-timer-set-timer
  3253. org-timer-reset-timers
  3254. org-timer-show-remaining-time)))
  3255. ;; Autoload org-feed.el
  3256. (eval-and-compile
  3257. (org-autoload
  3258. "org-feed"
  3259. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3260. ;; Autoload org-indent.el
  3261. ;; Define the variable already here, to make sure we have it.
  3262. (defvar org-indent-mode nil
  3263. "Non-nil if Org-Indent mode is enabled.
  3264. Use the command `org-indent-mode' to change this variable.")
  3265. (eval-and-compile
  3266. (org-autoload
  3267. "org-indent"
  3268. '(org-indent-mode)))
  3269. ;; Autoload org-mobile.el
  3270. (eval-and-compile
  3271. (org-autoload
  3272. "org-mobile"
  3273. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3274. ;; Autoload archiving code
  3275. ;; The stuff that is needed for cycling and tags has to be defined here.
  3276. (defgroup org-archive nil
  3277. "Options concerning archiving in Org-mode."
  3278. :tag "Org Archive"
  3279. :group 'org-structure)
  3280. (defcustom org-archive-location "%s_archive::"
  3281. "The location where subtrees should be archived.
  3282. The value of this variable is a string, consisting of two parts,
  3283. separated by a double-colon. The first part is a filename and
  3284. the second part is a headline.
  3285. When the filename is omitted, archiving happens in the same file.
  3286. %s in the filename will be replaced by the current file
  3287. name (without the directory part). Archiving to a different file
  3288. is useful to keep archived entries from contributing to the
  3289. Org-mode Agenda.
  3290. The archived entries will be filed as subtrees of the specified
  3291. headline. When the headline is omitted, the subtrees are simply
  3292. filed away at the end of the file, as top-level entries. Also in
  3293. the heading you can use %s to represent the file name, this can be
  3294. useful when using the same archive for a number of different files.
  3295. Here are a few examples:
  3296. \"%s_archive::\"
  3297. If the current file is Projects.org, archive in file
  3298. Projects.org_archive, as top-level trees. This is the default.
  3299. \"::* Archived Tasks\"
  3300. Archive in the current file, under the top-level headline
  3301. \"* Archived Tasks\".
  3302. \"~/org/archive.org::\"
  3303. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3304. \"~/org/archive.org::From %s\"
  3305. Archive in file ~/org/archive.org (absolute path), under headlines
  3306. \"From FILENAME\" where file name is the current file name.
  3307. \"basement::** Finished Tasks\"
  3308. Archive in file ./basement (relative path), as level 3 trees
  3309. below the level 2 heading \"** Finished Tasks\".
  3310. You may set this option on a per-file basis by adding to the buffer a
  3311. line like
  3312. #+ARCHIVE: basement::** Finished Tasks
  3313. You may also define it locally for a subtree by setting an ARCHIVE property
  3314. in the entry. If such a property is found in an entry, or anywhere up
  3315. the hierarchy, it will be used."
  3316. :group 'org-archive
  3317. :type 'string)
  3318. (defcustom org-archive-tag "ARCHIVE"
  3319. "The tag that marks a subtree as archived.
  3320. An archived subtree does not open during visibility cycling, and does
  3321. not contribute to the agenda listings.
  3322. After changing this, font-lock must be restarted in the relevant buffers to
  3323. get the proper fontification."
  3324. :group 'org-archive
  3325. :group 'org-keywords
  3326. :type 'string)
  3327. (defcustom org-agenda-skip-archived-trees t
  3328. "Non-nil means the agenda will skip any items located in archived trees.
  3329. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3330. variable is no longer recommended, you should leave it at the value t.
  3331. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3332. :group 'org-archive
  3333. :group 'org-agenda-skip
  3334. :type 'boolean)
  3335. (defcustom org-columns-skip-archived-trees t
  3336. "Non-nil means ignore archived trees when creating column view."
  3337. :group 'org-archive
  3338. :group 'org-properties
  3339. :type 'boolean)
  3340. (defcustom org-cycle-open-archived-trees nil
  3341. "Non-nil means `org-cycle' will open archived trees.
  3342. An archived tree is a tree marked with the tag ARCHIVE.
  3343. When nil, archived trees will stay folded. You can still open them with
  3344. normal outline commands like `show-all', but not with the cycling commands."
  3345. :group 'org-archive
  3346. :group 'org-cycle
  3347. :type 'boolean)
  3348. (defcustom org-sparse-tree-open-archived-trees nil
  3349. "Non-nil means sparse tree construction shows matches in archived trees.
  3350. When nil, matches in these trees are highlighted, but the trees are kept in
  3351. collapsed state."
  3352. :group 'org-archive
  3353. :group 'org-sparse-trees
  3354. :type 'boolean)
  3355. (defun org-cycle-hide-archived-subtrees (state)
  3356. "Re-hide all archived subtrees after a visibility state change."
  3357. (when (and (not org-cycle-open-archived-trees)
  3358. (not (memq state '(overview folded))))
  3359. (save-excursion
  3360. (let* ((globalp (memq state '(contents all)))
  3361. (beg (if globalp (point-min) (point)))
  3362. (end (if globalp (point-max) (org-end-of-subtree t))))
  3363. (org-hide-archived-subtrees beg end)
  3364. (goto-char beg)
  3365. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3366. (message "%s" (substitute-command-keys
  3367. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3368. (defun org-force-cycle-archived ()
  3369. "Cycle subtree even if it is archived."
  3370. (interactive)
  3371. (setq this-command 'org-cycle)
  3372. (let ((org-cycle-open-archived-trees t))
  3373. (call-interactively 'org-cycle)))
  3374. (defun org-hide-archived-subtrees (beg end)
  3375. "Re-hide all archived subtrees after a visibility state change."
  3376. (save-excursion
  3377. (let* ((re (concat ":" org-archive-tag ":")))
  3378. (goto-char beg)
  3379. (while (re-search-forward re end t)
  3380. (when (org-on-heading-p)
  3381. (org-flag-subtree t)
  3382. (org-end-of-subtree t))))))
  3383. (defun org-flag-subtree (flag)
  3384. (save-excursion
  3385. (org-back-to-heading t)
  3386. (outline-end-of-heading)
  3387. (outline-flag-region (point)
  3388. (progn (org-end-of-subtree t) (point))
  3389. flag)))
  3390. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3391. (eval-and-compile
  3392. (org-autoload "org-archive"
  3393. '(org-add-archive-files org-archive-subtree
  3394. org-archive-to-archive-sibling org-toggle-archive-tag
  3395. org-archive-subtree-default
  3396. org-archive-subtree-default-with-confirmation)))
  3397. ;; Autoload Column View Code
  3398. (declare-function org-columns-number-to-string "org-colview")
  3399. (declare-function org-columns-get-format-and-top-level "org-colview")
  3400. (declare-function org-columns-compute "org-colview")
  3401. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3402. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3403. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3404. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3405. ;; Autoload ID code
  3406. (declare-function org-id-store-link "org-id")
  3407. (declare-function org-id-locations-load "org-id")
  3408. (declare-function org-id-locations-save "org-id")
  3409. (defvar org-id-track-globally)
  3410. (org-autoload "org-id"
  3411. '(org-id-get-create org-id-new org-id-copy org-id-get
  3412. org-id-get-with-outline-path-completion
  3413. org-id-get-with-outline-drilling
  3414. org-id-goto org-id-find org-id-store-link))
  3415. ;; Autoload Plotting Code
  3416. (org-autoload "org-plot"
  3417. '(org-plot/gnuplot))
  3418. ;;; Variables for pre-computed regular expressions, all buffer local
  3419. (defvar org-drawer-regexp nil
  3420. "Matches first line of a hidden block.")
  3421. (make-variable-buffer-local 'org-drawer-regexp)
  3422. (defvar org-todo-regexp nil
  3423. "Matches any of the TODO state keywords.")
  3424. (make-variable-buffer-local 'org-todo-regexp)
  3425. (defvar org-not-done-regexp nil
  3426. "Matches any of the TODO state keywords except the last one.")
  3427. (make-variable-buffer-local 'org-not-done-regexp)
  3428. (defvar org-not-done-heading-regexp nil
  3429. "Matches a TODO headline that is not done.")
  3430. (make-variable-buffer-local 'org-not-done-regexp)
  3431. (defvar org-todo-line-regexp nil
  3432. "Matches a headline and puts TODO state into group 2 if present.")
  3433. (make-variable-buffer-local 'org-todo-line-regexp)
  3434. (defvar org-complex-heading-regexp nil
  3435. "Matches a headline and puts everything into groups:
  3436. group 1: the stars
  3437. group 2: The todo keyword, maybe
  3438. group 3: Priority cookie
  3439. group 4: True headline
  3440. group 5: Tags")
  3441. (make-variable-buffer-local 'org-complex-heading-regexp)
  3442. (defvar org-complex-heading-regexp-format nil)
  3443. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3444. (defvar org-todo-line-tags-regexp nil
  3445. "Matches a headline and puts TODO state into group 2 if present.
  3446. Also put tags into group 4 if tags are present.")
  3447. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3448. (defvar org-nl-done-regexp nil
  3449. "Matches newline followed by a headline with the DONE keyword.")
  3450. (make-variable-buffer-local 'org-nl-done-regexp)
  3451. (defvar org-looking-at-done-regexp nil
  3452. "Matches the DONE keyword a point.")
  3453. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3454. (defvar org-ds-keyword-length 12
  3455. "Maximum length of the Deadline and SCHEDULED keywords.")
  3456. (make-variable-buffer-local 'org-ds-keyword-length)
  3457. (defvar org-deadline-regexp nil
  3458. "Matches the DEADLINE keyword.")
  3459. (make-variable-buffer-local 'org-deadline-regexp)
  3460. (defvar org-deadline-time-regexp nil
  3461. "Matches the DEADLINE keyword together with a time stamp.")
  3462. (make-variable-buffer-local 'org-deadline-time-regexp)
  3463. (defvar org-deadline-line-regexp nil
  3464. "Matches the DEADLINE keyword and the rest of the line.")
  3465. (make-variable-buffer-local 'org-deadline-line-regexp)
  3466. (defvar org-scheduled-regexp nil
  3467. "Matches the SCHEDULED keyword.")
  3468. (make-variable-buffer-local 'org-scheduled-regexp)
  3469. (defvar org-scheduled-time-regexp nil
  3470. "Matches the SCHEDULED keyword together with a time stamp.")
  3471. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3472. (defvar org-closed-time-regexp nil
  3473. "Matches the CLOSED keyword together with a time stamp.")
  3474. (make-variable-buffer-local 'org-closed-time-regexp)
  3475. (defvar org-keyword-time-regexp nil
  3476. "Matches any of the 4 keywords, together with the time stamp.")
  3477. (make-variable-buffer-local 'org-keyword-time-regexp)
  3478. (defvar org-keyword-time-not-clock-regexp nil
  3479. "Matches any of the 3 keywords, together with the time stamp.")
  3480. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3481. (defvar org-maybe-keyword-time-regexp nil
  3482. "Matches a timestamp, possibly preceeded by a keyword.")
  3483. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3484. (defvar org-planning-or-clock-line-re nil
  3485. "Matches a line with planning or clock info.")
  3486. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3487. (defvar org-all-time-keywords nil
  3488. "List of time keywords.")
  3489. (make-variable-buffer-local 'org-all-time-keywords)
  3490. (defconst org-plain-time-of-day-regexp
  3491. (concat
  3492. "\\(\\<[012]?[0-9]"
  3493. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3494. "\\(--?"
  3495. "\\(\\<[012]?[0-9]"
  3496. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3497. "\\)?")
  3498. "Regular expression to match a plain time or time range.
  3499. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3500. groups carry important information:
  3501. 0 the full match
  3502. 1 the first time, range or not
  3503. 8 the second time, if it is a range.")
  3504. (defconst org-plain-time-extension-regexp
  3505. (concat
  3506. "\\(\\<[012]?[0-9]"
  3507. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3508. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3509. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3510. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3511. groups carry important information:
  3512. 0 the full match
  3513. 7 hours of duration
  3514. 9 minutes of duration")
  3515. (defconst org-stamp-time-of-day-regexp
  3516. (concat
  3517. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3518. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3519. "\\(--?"
  3520. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3521. "Regular expression to match a timestamp time or time range.
  3522. After a match, the following groups carry important information:
  3523. 0 the full match
  3524. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3525. 2 the first time, range or not
  3526. 4 the second time, if it is a range.")
  3527. (defconst org-startup-options
  3528. '(("fold" org-startup-folded t)
  3529. ("overview" org-startup-folded t)
  3530. ("nofold" org-startup-folded nil)
  3531. ("showall" org-startup-folded nil)
  3532. ("showeverything" org-startup-folded showeverything)
  3533. ("content" org-startup-folded content)
  3534. ("indent" org-startup-indented t)
  3535. ("noindent" org-startup-indented nil)
  3536. ("hidestars" org-hide-leading-stars t)
  3537. ("showstars" org-hide-leading-stars nil)
  3538. ("odd" org-odd-levels-only t)
  3539. ("oddeven" org-odd-levels-only nil)
  3540. ("align" org-startup-align-all-tables t)
  3541. ("noalign" org-startup-align-all-tables nil)
  3542. ("customtime" org-display-custom-times t)
  3543. ("logdone" org-log-done time)
  3544. ("lognotedone" org-log-done note)
  3545. ("nologdone" org-log-done nil)
  3546. ("lognoteclock-out" org-log-note-clock-out t)
  3547. ("nolognoteclock-out" org-log-note-clock-out nil)
  3548. ("logrepeat" org-log-repeat state)
  3549. ("lognoterepeat" org-log-repeat note)
  3550. ("nologrepeat" org-log-repeat nil)
  3551. ("logreschedule" org-log-reschedule time)
  3552. ("lognotereschedule" org-log-reschedule note)
  3553. ("nologreschedule" org-log-reschedule nil)
  3554. ("logredeadline" org-log-redeadline time)
  3555. ("lognoteredeadline" org-log-redeadline note)
  3556. ("nologredeadline" org-log-redeadline nil)
  3557. ("logrefile" org-log-refile time)
  3558. ("lognoterefile" org-log-refile note)
  3559. ("nologrefile" org-log-refile nil)
  3560. ("fninline" org-footnote-define-inline t)
  3561. ("nofninline" org-footnote-define-inline nil)
  3562. ("fnlocal" org-footnote-section nil)
  3563. ("fnauto" org-footnote-auto-label t)
  3564. ("fnprompt" org-footnote-auto-label nil)
  3565. ("fnconfirm" org-footnote-auto-label confirm)
  3566. ("fnplain" org-footnote-auto-label plain)
  3567. ("fnadjust" org-footnote-auto-adjust t)
  3568. ("nofnadjust" org-footnote-auto-adjust nil)
  3569. ("constcgs" constants-unit-system cgs)
  3570. ("constSI" constants-unit-system SI)
  3571. ("noptag" org-tag-persistent-alist nil)
  3572. ("hideblocks" org-hide-block-startup t)
  3573. ("nohideblocks" org-hide-block-startup nil)
  3574. ("beamer" org-startup-with-beamer-mode t)
  3575. ("entitiespretty" org-pretty-entities t)
  3576. ("entitiesplain" org-pretty-entities nil))
  3577. "Variable associated with STARTUP options for org-mode.
  3578. Each element is a list of three items: The startup options as written
  3579. in the #+STARTUP line, the corresponding variable, and the value to
  3580. set this variable to if the option is found. An optional forth element PUSH
  3581. means to push this value onto the list in the variable.")
  3582. (defun org-set-regexps-and-options ()
  3583. "Precompute regular expressions for current buffer."
  3584. (when (org-mode-p)
  3585. (org-set-local 'org-todo-kwd-alist nil)
  3586. (org-set-local 'org-todo-key-alist nil)
  3587. (org-set-local 'org-todo-key-trigger nil)
  3588. (org-set-local 'org-todo-keywords-1 nil)
  3589. (org-set-local 'org-done-keywords nil)
  3590. (org-set-local 'org-todo-heads nil)
  3591. (org-set-local 'org-todo-sets nil)
  3592. (org-set-local 'org-todo-log-states nil)
  3593. (org-set-local 'org-file-properties nil)
  3594. (org-set-local 'org-file-tags nil)
  3595. (let ((re (org-make-options-regexp
  3596. '("CATEGORY" "TODO" "COLUMNS"
  3597. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3598. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3599. "OPTIONS")
  3600. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3601. (splitre "[ \t]+")
  3602. (scripts org-use-sub-superscripts)
  3603. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3604. tail sep kws1 prio props ftags drawers beamer-p
  3605. ext-setup-or-nil setup-contents (start 0))
  3606. (save-excursion
  3607. (save-restriction
  3608. (widen)
  3609. (goto-char (point-min))
  3610. (while (or (and ext-setup-or-nil
  3611. (string-match re ext-setup-or-nil start)
  3612. (setq start (match-end 0)))
  3613. (and (setq ext-setup-or-nil nil start 0)
  3614. (re-search-forward re nil t)))
  3615. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3616. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3617. (if (stringp value) (setq value (org-trim value)))
  3618. (cond
  3619. ((equal key "CATEGORY")
  3620. (setq cat value))
  3621. ((member key '("SEQ_TODO" "TODO"))
  3622. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3623. ((equal key "TYP_TODO")
  3624. (push (cons 'type (org-split-string value splitre)) kwds))
  3625. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3626. ;; general TODO-like setup
  3627. (push (cons (intern (downcase (match-string 1 key)))
  3628. (org-split-string value splitre)) kwds))
  3629. ((equal key "TAGS")
  3630. (setq tags (append tags (if tags '("\\n") nil)
  3631. (org-split-string value splitre))))
  3632. ((equal key "COLUMNS")
  3633. (org-set-local 'org-columns-default-format value))
  3634. ((equal key "LINK")
  3635. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3636. (push (cons (match-string 1 value)
  3637. (org-trim (match-string 2 value)))
  3638. links)))
  3639. ((equal key "PRIORITIES")
  3640. (setq prio (org-split-string value " +")))
  3641. ((equal key "PROPERTY")
  3642. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3643. (push (cons (match-string 1 value) (match-string 2 value))
  3644. props)))
  3645. ((equal key "FILETAGS")
  3646. (when (string-match "\\S-" value)
  3647. (setq ftags
  3648. (append
  3649. ftags
  3650. (apply 'append
  3651. (mapcar (lambda (x) (org-split-string x ":"))
  3652. (org-split-string value)))))))
  3653. ((equal key "DRAWERS")
  3654. (setq drawers (org-split-string value splitre)))
  3655. ((equal key "CONSTANTS")
  3656. (setq const (append const (org-split-string value splitre))))
  3657. ((equal key "STARTUP")
  3658. (let ((opts (org-split-string value splitre))
  3659. l var val)
  3660. (while (setq l (pop opts))
  3661. (when (setq l (assoc l org-startup-options))
  3662. (setq var (nth 1 l) val (nth 2 l))
  3663. (if (not (nth 3 l))
  3664. (set (make-local-variable var) val)
  3665. (if (not (listp (symbol-value var)))
  3666. (set (make-local-variable var) nil))
  3667. (set (make-local-variable var) (symbol-value var))
  3668. (add-to-list var val))))))
  3669. ((equal key "ARCHIVE")
  3670. (setq arch value)
  3671. (remove-text-properties 0 (length arch)
  3672. '(face t fontified t) arch))
  3673. ((equal key "LATEX_CLASS")
  3674. (setq beamer-p (equal value "beamer")))
  3675. ((equal key "OPTIONS")
  3676. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3677. (setq scripts (read (match-string 2 value)))))
  3678. ((equal key "SETUPFILE")
  3679. (setq setup-contents (org-file-contents
  3680. (expand-file-name
  3681. (org-remove-double-quotes value))
  3682. 'noerror))
  3683. (if (not ext-setup-or-nil)
  3684. (setq ext-setup-or-nil setup-contents start 0)
  3685. (setq ext-setup-or-nil
  3686. (concat (substring ext-setup-or-nil 0 start)
  3687. "\n" setup-contents "\n"
  3688. (substring ext-setup-or-nil start)))))
  3689. ))))
  3690. (org-set-local 'org-use-sub-superscripts scripts)
  3691. (when cat
  3692. (org-set-local 'org-category (intern cat))
  3693. (push (cons "CATEGORY" cat) props))
  3694. (when prio
  3695. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3696. (setq prio (mapcar 'string-to-char prio))
  3697. (org-set-local 'org-highest-priority (nth 0 prio))
  3698. (org-set-local 'org-lowest-priority (nth 1 prio))
  3699. (org-set-local 'org-default-priority (nth 2 prio)))
  3700. (and props (org-set-local 'org-file-properties (nreverse props)))
  3701. (and ftags (org-set-local 'org-file-tags
  3702. (mapcar 'org-add-prop-inherited ftags)))
  3703. (and drawers (org-set-local 'org-drawers drawers))
  3704. (and arch (org-set-local 'org-archive-location arch))
  3705. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3706. ;; Process the TODO keywords
  3707. (unless kwds
  3708. ;; Use the global values as if they had been given locally.
  3709. (setq kwds (default-value 'org-todo-keywords))
  3710. (if (stringp (car kwds))
  3711. (setq kwds (list (cons org-todo-interpretation
  3712. (default-value 'org-todo-keywords)))))
  3713. (setq kwds (reverse kwds)))
  3714. (setq kwds (nreverse kwds))
  3715. (let (inter kws kw)
  3716. (while (setq kws (pop kwds))
  3717. (let ((kws (or
  3718. (run-hook-with-args-until-success
  3719. 'org-todo-setup-filter-hook kws)
  3720. kws)))
  3721. (setq inter (pop kws) sep (member "|" kws)
  3722. kws0 (delete "|" (copy-sequence kws))
  3723. kwsa nil
  3724. kws1 (mapcar
  3725. (lambda (x)
  3726. ;; 1 2
  3727. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3728. (progn
  3729. (setq kw (match-string 1 x)
  3730. key (and (match-end 2) (match-string 2 x))
  3731. log (org-extract-log-state-settings x))
  3732. (push (cons kw (and key (string-to-char key))) kwsa)
  3733. (and log (push log org-todo-log-states))
  3734. kw)
  3735. (error "Invalid TODO keyword %s" x)))
  3736. kws0)
  3737. kwsa (if kwsa (append '((:startgroup))
  3738. (nreverse kwsa)
  3739. '((:endgroup))))
  3740. hw (car kws1)
  3741. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3742. tail (list inter hw (car dws) (org-last dws))))
  3743. (add-to-list 'org-todo-heads hw 'append)
  3744. (push kws1 org-todo-sets)
  3745. (setq org-done-keywords (append org-done-keywords dws nil))
  3746. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3747. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3748. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3749. (setq org-todo-sets (nreverse org-todo-sets)
  3750. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3751. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3752. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3753. ;; Process the constants
  3754. (when const
  3755. (let (e cst)
  3756. (while (setq e (pop const))
  3757. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3758. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3759. (setq org-table-formula-constants-local cst)))
  3760. ;; Process the tags.
  3761. (when tags
  3762. (let (e tgs)
  3763. (while (setq e (pop tags))
  3764. (cond
  3765. ((equal e "{") (push '(:startgroup) tgs))
  3766. ((equal e "}") (push '(:endgroup) tgs))
  3767. ((equal e "\\n") (push '(:newline) tgs))
  3768. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3769. (push (cons (match-string 1 e)
  3770. (string-to-char (match-string 2 e)))
  3771. tgs))
  3772. (t (push (list e) tgs))))
  3773. (org-set-local 'org-tag-alist nil)
  3774. (while (setq e (pop tgs))
  3775. (or (and (stringp (car e))
  3776. (assoc (car e) org-tag-alist))
  3777. (push e org-tag-alist)))))
  3778. ;; Compute the regular expressions and other local variables
  3779. (if (not org-done-keywords)
  3780. (setq org-done-keywords (and org-todo-keywords-1
  3781. (list (org-last org-todo-keywords-1)))))
  3782. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3783. (length org-scheduled-string)
  3784. (length org-clock-string)
  3785. (length org-closed-string)))
  3786. org-drawer-regexp
  3787. (concat "^[ \t]*:\\("
  3788. (mapconcat 'regexp-quote org-drawers "\\|")
  3789. "\\):[ \t]*$")
  3790. org-not-done-keywords
  3791. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3792. org-todo-regexp
  3793. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3794. "\\|") "\\)\\>")
  3795. org-not-done-regexp
  3796. (concat "\\<\\("
  3797. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3798. "\\)\\>")
  3799. org-not-done-heading-regexp
  3800. (concat "^\\(\\*+\\)[ \t]+\\("
  3801. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3802. "\\)\\>")
  3803. org-todo-line-regexp
  3804. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3805. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3806. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3807. org-complex-heading-regexp
  3808. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3809. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3810. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3811. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3812. org-complex-heading-regexp-format
  3813. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3814. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3815. "\\)\\>\\)?"
  3816. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3817. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3818. "[ \t]*\\(%s\\)"
  3819. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3820. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3821. org-nl-done-regexp
  3822. (concat "\n\\*+[ \t]+"
  3823. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3824. "\\)" "\\>")
  3825. org-todo-line-tags-regexp
  3826. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3827. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3828. (org-re
  3829. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3830. org-looking-at-done-regexp
  3831. (concat "^" "\\(?:"
  3832. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3833. "\\>")
  3834. org-deadline-regexp (concat "\\<" org-deadline-string)
  3835. org-deadline-time-regexp
  3836. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3837. org-deadline-line-regexp
  3838. (concat "\\<\\(" org-deadline-string "\\).*")
  3839. org-scheduled-regexp
  3840. (concat "\\<" org-scheduled-string)
  3841. org-scheduled-time-regexp
  3842. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3843. org-closed-time-regexp
  3844. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3845. org-keyword-time-regexp
  3846. (concat "\\<\\(" org-scheduled-string
  3847. "\\|" org-deadline-string
  3848. "\\|" org-closed-string
  3849. "\\|" org-clock-string "\\)"
  3850. " *[[<]\\([^]>]+\\)[]>]")
  3851. org-keyword-time-not-clock-regexp
  3852. (concat "\\<\\(" org-scheduled-string
  3853. "\\|" org-deadline-string
  3854. "\\|" org-closed-string
  3855. "\\)"
  3856. " *[[<]\\([^]>]+\\)[]>]")
  3857. org-maybe-keyword-time-regexp
  3858. (concat "\\(\\<\\(" org-scheduled-string
  3859. "\\|" org-deadline-string
  3860. "\\|" org-closed-string
  3861. "\\|" org-clock-string "\\)\\)?"
  3862. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3863. org-planning-or-clock-line-re
  3864. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3865. "\\|" org-deadline-string
  3866. "\\|" org-closed-string "\\|" org-clock-string
  3867. "\\)\\>\\)")
  3868. org-all-time-keywords
  3869. (mapcar (lambda (w) (substring w 0 -1))
  3870. (list org-scheduled-string org-deadline-string
  3871. org-clock-string org-closed-string))
  3872. )
  3873. (org-compute-latex-and-specials-regexp)
  3874. (org-set-font-lock-defaults))))
  3875. (defun org-file-contents (file &optional noerror)
  3876. "Return the contents of FILE, as a string."
  3877. (if (or (not file)
  3878. (not (file-readable-p file)))
  3879. (if noerror
  3880. (progn
  3881. (message "Cannot read file \"%s\"" file)
  3882. (ding) (sit-for 2)
  3883. "")
  3884. (error "Cannot read file \"%s\"" file))
  3885. (with-temp-buffer
  3886. (insert-file-contents file)
  3887. (buffer-string))))
  3888. (defun org-extract-log-state-settings (x)
  3889. "Extract the log state setting from a TODO keyword string.
  3890. This will extract info from a string like \"WAIT(w@/!)\"."
  3891. (let (kw key log1 log2)
  3892. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3893. (setq kw (match-string 1 x)
  3894. key (and (match-end 2) (match-string 2 x))
  3895. log1 (and (match-end 3) (match-string 3 x))
  3896. log2 (and (match-end 4) (match-string 4 x)))
  3897. (and (or log1 log2)
  3898. (list kw
  3899. (and log1 (if (equal log1 "!") 'time 'note))
  3900. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3901. (defun org-remove-keyword-keys (list)
  3902. "Remove a pair of parenthesis at the end of each string in LIST."
  3903. (mapcar (lambda (x)
  3904. (if (string-match "(.*)$" x)
  3905. (substring x 0 (match-beginning 0))
  3906. x))
  3907. list))
  3908. (defun org-assign-fast-keys (alist)
  3909. "Assign fast keys to a keyword-key alist.
  3910. Respect keys that are already there."
  3911. (let (new e (alt ?0))
  3912. (while (setq e (pop alist))
  3913. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3914. (cdr e)) ;; Key already assigned.
  3915. (push e new)
  3916. (let ((clist (string-to-list (downcase (car e))))
  3917. (used (append new alist)))
  3918. (when (= (car clist) ?@)
  3919. (pop clist))
  3920. (while (and clist (rassoc (car clist) used))
  3921. (pop clist))
  3922. (unless clist
  3923. (while (rassoc alt used)
  3924. (incf alt)))
  3925. (push (cons (car e) (or (car clist) alt)) new))))
  3926. (nreverse new)))
  3927. ;;; Some variables used in various places
  3928. (defvar org-window-configuration nil
  3929. "Used in various places to store a window configuration.")
  3930. (defvar org-selected-window nil
  3931. "Used in various places to store a window configuration.")
  3932. (defvar org-finish-function nil
  3933. "Function to be called when `C-c C-c' is used.
  3934. This is for getting out of special buffers like remember.")
  3935. ;; FIXME: Occasionally check by commenting these, to make sure
  3936. ;; no other functions uses these, forgetting to let-bind them.
  3937. (defvar entry)
  3938. (defvar last-state)
  3939. (defvar date)
  3940. ;; Defined somewhere in this file, but used before definition.
  3941. (defvar org-entities) ;; defined in org-entities.el
  3942. (defvar org-struct-menu)
  3943. (defvar org-org-menu)
  3944. (defvar org-tbl-menu)
  3945. ;;;; Define the Org-mode
  3946. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3947. (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."))
  3948. ;; We use a before-change function to check if a table might need
  3949. ;; an update.
  3950. (defvar org-table-may-need-update t
  3951. "Indicates that a table might need an update.
  3952. This variable is set by `org-before-change-function'.
  3953. `org-table-align' sets it back to nil.")
  3954. (defun org-before-change-function (beg end)
  3955. "Every change indicates that a table might need an update."
  3956. (setq org-table-may-need-update t))
  3957. (defvar org-mode-map)
  3958. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3959. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3960. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3961. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3962. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3963. (defvar org-table-buffer-is-an nil)
  3964. (defconst org-outline-regexp "\\*+ ")
  3965. ;;;###autoload
  3966. (define-derived-mode org-mode outline-mode "Org"
  3967. "Outline-based notes management and organizer, alias
  3968. \"Carsten's outline-mode for keeping track of everything.\"
  3969. Org-mode develops organizational tasks around a NOTES file which
  3970. contains information about projects as plain text. Org-mode is
  3971. implemented on top of outline-mode, which is ideal to keep the content
  3972. of large files well structured. It supports ToDo items, deadlines and
  3973. time stamps, which magically appear in the diary listing of the Emacs
  3974. calendar. Tables are easily created with a built-in table editor.
  3975. Plain text URL-like links connect to websites, emails (VM), Usenet
  3976. messages (Gnus), BBDB entries, and any files related to the project.
  3977. For printing and sharing of notes, an Org-mode file (or a part of it)
  3978. can be exported as a structured ASCII or HTML file.
  3979. The following commands are available:
  3980. \\{org-mode-map}"
  3981. ;; Get rid of Outline menus, they are not needed
  3982. ;; Need to do this here because define-derived-mode sets up
  3983. ;; the keymap so late. Still, it is a waste to call this each time
  3984. ;; we switch another buffer into org-mode.
  3985. (if (featurep 'xemacs)
  3986. (when (boundp 'outline-mode-menu-heading)
  3987. ;; Assume this is Greg's port, it uses easymenu
  3988. (easy-menu-remove outline-mode-menu-heading)
  3989. (easy-menu-remove outline-mode-menu-show)
  3990. (easy-menu-remove outline-mode-menu-hide))
  3991. (define-key org-mode-map [menu-bar headings] 'undefined)
  3992. (define-key org-mode-map [menu-bar hide] 'undefined)
  3993. (define-key org-mode-map [menu-bar show] 'undefined))
  3994. (org-load-modules-maybe)
  3995. (easy-menu-add org-org-menu)
  3996. (easy-menu-add org-tbl-menu)
  3997. (org-install-agenda-files-menu)
  3998. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  3999. (add-to-invisibility-spec '(org-cwidth))
  4000. (add-to-invisibility-spec '(org-hide-block . t))
  4001. (when (featurep 'xemacs)
  4002. (org-set-local 'line-move-ignore-invisible t))
  4003. (org-set-local 'outline-regexp org-outline-regexp)
  4004. (org-set-local 'outline-level 'org-outline-level)
  4005. (when (and org-ellipsis
  4006. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4007. (fboundp 'make-glyph-code))
  4008. (unless org-display-table
  4009. (setq org-display-table (make-display-table)))
  4010. (set-display-table-slot
  4011. org-display-table 4
  4012. (vconcat (mapcar
  4013. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4014. org-ellipsis)))
  4015. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4016. (setq buffer-display-table org-display-table))
  4017. (org-set-regexps-and-options)
  4018. (when (and org-tag-faces (not org-tags-special-faces-re))
  4019. ;; tag faces set outside customize.... force initialization.
  4020. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4021. ;; Calc embedded
  4022. (org-set-local 'calc-embedded-open-mode "# ")
  4023. (modify-syntax-entry ?@ "w")
  4024. (if org-startup-truncated (setq truncate-lines t))
  4025. (org-set-local 'font-lock-unfontify-region-function
  4026. 'org-unfontify-region)
  4027. ;; Activate before-change-function
  4028. (org-set-local 'org-table-may-need-update t)
  4029. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4030. 'local)
  4031. ;; Check for running clock before killing a buffer
  4032. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4033. ;; Paragraphs and auto-filling
  4034. (org-set-autofill-regexps)
  4035. (setq indent-line-function 'org-indent-line-function)
  4036. (org-update-radio-target-regexp)
  4037. ;; Beginning/end of defun
  4038. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4039. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4040. ;; Make sure dependence stuff works reliably, even for users who set it
  4041. ;; too late :-(
  4042. (if org-enforce-todo-dependencies
  4043. (add-hook 'org-blocker-hook
  4044. 'org-block-todo-from-children-or-siblings-or-parent)
  4045. (remove-hook 'org-blocker-hook
  4046. 'org-block-todo-from-children-or-siblings-or-parent))
  4047. (if org-enforce-todo-checkbox-dependencies
  4048. (add-hook 'org-blocker-hook
  4049. 'org-block-todo-from-checkboxes)
  4050. (remove-hook 'org-blocker-hook
  4051. 'org-block-todo-from-checkboxes))
  4052. ;; Comment characters
  4053. (org-set-local 'comment-start "#")
  4054. (org-set-local 'comment-padding " ")
  4055. ;; Align options lines
  4056. (org-set-local
  4057. 'align-mode-rules-list
  4058. '((org-in-buffer-settings
  4059. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4060. (modes . '(org-mode)))))
  4061. ;; Imenu
  4062. (org-set-local 'imenu-create-index-function
  4063. 'org-imenu-get-tree)
  4064. ;; Make isearch reveal context
  4065. (if (or (featurep 'xemacs)
  4066. (not (boundp 'outline-isearch-open-invisible-function)))
  4067. ;; Emacs 21 and XEmacs make use of the hook
  4068. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4069. ;; Emacs 22 deals with this through a special variable
  4070. (org-set-local 'outline-isearch-open-invisible-function
  4071. (lambda (&rest ignore) (org-show-context 'isearch))))
  4072. ;; Turn on org-beamer-mode?
  4073. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4074. ;; If empty file that did not turn on org-mode automatically, make it to.
  4075. (if (and org-insert-mode-line-in-empty-file
  4076. (interactive-p)
  4077. (= (point-min) (point-max)))
  4078. (insert "# -*- mode: org -*-\n\n"))
  4079. (unless org-inhibit-startup
  4080. (when org-startup-align-all-tables
  4081. (let ((bmp (buffer-modified-p)))
  4082. (org-table-map-tables 'org-table-align 'quietly)
  4083. (set-buffer-modified-p bmp)))
  4084. (when org-startup-indented
  4085. (require 'org-indent)
  4086. (org-indent-mode 1))
  4087. (unless org-inhibit-startup-visibility-stuff
  4088. (org-set-startup-visibility))))
  4089. (when (fboundp 'abbrev-table-put)
  4090. (abbrev-table-put org-mode-abbrev-table
  4091. :parents (list text-mode-abbrev-table)))
  4092. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4093. (defun org-current-time ()
  4094. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4095. (if (> (car org-time-stamp-rounding-minutes) 1)
  4096. (let ((r (car org-time-stamp-rounding-minutes))
  4097. (time (decode-time)))
  4098. (apply 'encode-time
  4099. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4100. (nthcdr 2 time))))
  4101. (current-time)))
  4102. ;;;; Font-Lock stuff, including the activators
  4103. (defvar org-mouse-map (make-sparse-keymap))
  4104. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4105. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4106. (when org-mouse-1-follows-link
  4107. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4108. (when org-tab-follows-link
  4109. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4110. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4111. (require 'font-lock)
  4112. (defconst org-non-link-chars "]\t\n\r<>")
  4113. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4114. "shell" "elisp" "doi"))
  4115. (defvar org-link-types-re nil
  4116. "Matches a link that has a url-like prefix like \"http:\"")
  4117. (defvar org-link-re-with-space nil
  4118. "Matches a link with spaces, optional angular brackets around it.")
  4119. (defvar org-link-re-with-space2 nil
  4120. "Matches a link with spaces, optional angular brackets around it.")
  4121. (defvar org-link-re-with-space3 nil
  4122. "Matches a link with spaces, only for internal part in bracket links.")
  4123. (defvar org-angle-link-re nil
  4124. "Matches link with angular brackets, spaces are allowed.")
  4125. (defvar org-plain-link-re nil
  4126. "Matches plain link, without spaces.")
  4127. (defvar org-bracket-link-regexp nil
  4128. "Matches a link in double brackets.")
  4129. (defvar org-bracket-link-analytic-regexp nil
  4130. "Regular expression used to analyze links.
  4131. Here is what the match groups contain after a match:
  4132. 1: http:
  4133. 2: http
  4134. 3: path
  4135. 4: [desc]
  4136. 5: desc")
  4137. (defvar org-bracket-link-analytic-regexp++ nil
  4138. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  4139. (defvar org-any-link-re nil
  4140. "Regular expression matching any link.")
  4141. (defcustom org-match-sexp-depth 3
  4142. "Number of stacked braces for sub/superscript matching.
  4143. This has to be set before loading org.el to be effective."
  4144. :group 'org-export-translation ; ??????????????????????????/
  4145. :type 'integer)
  4146. (defun org-create-multibrace-regexp (left right n)
  4147. "Create a regular expression which will match a balanced sexp.
  4148. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4149. as single character strings.
  4150. The regexp returned will match the entire expression including the
  4151. delimiters. It will also define a single group which contains the
  4152. match except for the outermost delimiters. The maximum depth of
  4153. stacked delimiters is N. Escaping delimiters is not possible."
  4154. (let* ((nothing (concat "[^" left right "]*?"))
  4155. (or "\\|")
  4156. (re nothing)
  4157. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4158. (while (> n 1)
  4159. (setq n (1- n)
  4160. re (concat re or next)
  4161. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4162. (concat left "\\(" re "\\)" right)))
  4163. (defvar org-match-substring-regexp
  4164. (concat
  4165. "\\([^\\]\\)\\([_^]\\)\\("
  4166. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4167. "\\|"
  4168. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4169. "\\|"
  4170. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4171. "The regular expression matching a sub- or superscript.")
  4172. (defvar org-match-substring-with-braces-regexp
  4173. (concat
  4174. "\\([^\\]\\)\\([_^]\\)\\("
  4175. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4176. "\\)")
  4177. "The regular expression matching a sub- or superscript, forcing braces.")
  4178. (defun org-make-link-regexps ()
  4179. "Update the link regular expressions.
  4180. This should be called after the variable `org-link-types' has changed."
  4181. (setq org-link-types-re
  4182. (concat
  4183. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4184. org-link-re-with-space
  4185. (concat
  4186. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4187. "\\([^" org-non-link-chars " ]"
  4188. "[^" org-non-link-chars "]*"
  4189. "[^" org-non-link-chars " ]\\)>?")
  4190. org-link-re-with-space2
  4191. (concat
  4192. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4193. "\\([^" org-non-link-chars " ]"
  4194. "[^\t\n\r]*"
  4195. "[^" org-non-link-chars " ]\\)>?")
  4196. org-link-re-with-space3
  4197. (concat
  4198. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4199. "\\([^" org-non-link-chars " ]"
  4200. "[^\t\n\r]*\\)")
  4201. org-angle-link-re
  4202. (concat
  4203. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4204. "\\([^" org-non-link-chars " ]"
  4205. "[^" org-non-link-chars "]*"
  4206. "\\)>")
  4207. org-plain-link-re
  4208. (concat
  4209. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4210. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4211. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4212. org-bracket-link-regexp
  4213. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4214. org-bracket-link-analytic-regexp
  4215. (concat
  4216. "\\[\\["
  4217. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4218. "\\([^]]+\\)"
  4219. "\\]"
  4220. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4221. "\\]")
  4222. org-bracket-link-analytic-regexp++
  4223. (concat
  4224. "\\[\\["
  4225. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4226. "\\([^]]+\\)"
  4227. "\\]"
  4228. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4229. "\\]")
  4230. org-any-link-re
  4231. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4232. org-angle-link-re "\\)\\|\\("
  4233. org-plain-link-re "\\)")))
  4234. (org-make-link-regexps)
  4235. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4236. "Regular expression for fast time stamp matching.")
  4237. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4238. "Regular expression for fast time stamp matching.")
  4239. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4240. "Regular expression matching time strings for analysis.
  4241. This one does not require the space after the date, so it can be used
  4242. on a string that terminates immediately after the date.")
  4243. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4244. "Regular expression matching time strings for analysis.")
  4245. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4246. "Regular expression matching time stamps, with groups.")
  4247. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4248. "Regular expression matching time stamps (also [..]), with groups.")
  4249. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4250. "Regular expression matching a time stamp range.")
  4251. (defconst org-tr-regexp-both
  4252. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4253. "Regular expression matching a time stamp range.")
  4254. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4255. org-ts-regexp "\\)?")
  4256. "Regular expression matching a time stamp or time stamp range.")
  4257. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4258. org-ts-regexp-both "\\)?")
  4259. "Regular expression matching a time stamp or time stamp range.
  4260. The time stamps may be either active or inactive.")
  4261. (defvar org-emph-face nil)
  4262. (defun org-do-emphasis-faces (limit)
  4263. "Run through the buffer and add overlays to links."
  4264. (let (rtn a)
  4265. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4266. (if (not (= (char-after (match-beginning 3))
  4267. (char-after (match-beginning 4))))
  4268. (progn
  4269. (setq rtn t)
  4270. (setq a (assoc (match-string 3) org-emphasis-alist))
  4271. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4272. 'face
  4273. (nth 1 a))
  4274. (and (nth 4 a)
  4275. (org-remove-flyspell-overlays-in
  4276. (match-beginning 0) (match-end 0)))
  4277. (add-text-properties (match-beginning 2) (match-end 2)
  4278. '(font-lock-multiline t org-emphasis t))
  4279. (when org-hide-emphasis-markers
  4280. (add-text-properties (match-end 4) (match-beginning 5)
  4281. '(invisible org-link))
  4282. (add-text-properties (match-beginning 3) (match-end 3)
  4283. '(invisible org-link)))))
  4284. (backward-char 1))
  4285. rtn))
  4286. (defun org-emphasize (&optional char)
  4287. "Insert or change an emphasis, i.e. a font like bold or italic.
  4288. If there is an active region, change that region to a new emphasis.
  4289. If there is no region, just insert the marker characters and position
  4290. the cursor between them.
  4291. CHAR should be either the marker character, or the first character of the
  4292. HTML tag associated with that emphasis. If CHAR is a space, the means
  4293. to remove the emphasis of the selected region.
  4294. If char is not given (for example in an interactive call) it
  4295. will be prompted for."
  4296. (interactive)
  4297. (let ((eal org-emphasis-alist) e det
  4298. (erc org-emphasis-regexp-components)
  4299. (prompt "")
  4300. (string "") beg end move tag c s)
  4301. (if (org-region-active-p)
  4302. (setq beg (region-beginning) end (region-end)
  4303. string (buffer-substring beg end))
  4304. (setq move t))
  4305. (while (setq e (pop eal))
  4306. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4307. c (aref tag 0))
  4308. (push (cons c (string-to-char (car e))) det)
  4309. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4310. (substring tag 1)))))
  4311. (setq det (nreverse det))
  4312. (unless char
  4313. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4314. (setq char (read-char-exclusive)))
  4315. (setq char (or (cdr (assoc char det)) char))
  4316. (if (equal char ?\ )
  4317. (setq s "" move nil)
  4318. (unless (assoc (char-to-string char) org-emphasis-alist)
  4319. (error "No such emphasis marker: \"%c\"" char))
  4320. (setq s (char-to-string char)))
  4321. (while (and (> (length string) 1)
  4322. (equal (substring string 0 1) (substring string -1))
  4323. (assoc (substring string 0 1) org-emphasis-alist))
  4324. (setq string (substring string 1 -1)))
  4325. (setq string (concat s string s))
  4326. (if beg (delete-region beg end))
  4327. (unless (or (bolp)
  4328. (string-match (concat "[" (nth 0 erc) "\n]")
  4329. (char-to-string (char-before (point)))))
  4330. (insert " "))
  4331. (unless (or (eobp)
  4332. (string-match (concat "[" (nth 1 erc) "\n]")
  4333. (char-to-string (char-after (point)))))
  4334. (insert " ") (backward-char 1))
  4335. (insert string)
  4336. (and move (backward-char 1))))
  4337. (defconst org-nonsticky-props
  4338. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4339. (defsubst org-rear-nonsticky-at (pos)
  4340. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4341. (defun org-activate-plain-links (limit)
  4342. "Run through the buffer and add overlays to links."
  4343. (catch 'exit
  4344. (let (f)
  4345. (if (re-search-forward org-plain-link-re limit t)
  4346. (progn
  4347. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4348. (setq f (get-text-property (match-beginning 0) 'face))
  4349. (if (or (eq f 'org-tag)
  4350. (and (listp f) (memq 'org-tag f)))
  4351. nil
  4352. (add-text-properties (match-beginning 0) (match-end 0)
  4353. (list 'mouse-face 'highlight
  4354. 'face 'org-link
  4355. 'keymap org-mouse-map))
  4356. (org-rear-nonsticky-at (match-end 0)))
  4357. t)))))
  4358. (defun org-activate-code (limit)
  4359. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4360. (progn
  4361. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4362. (remove-text-properties (match-beginning 0) (match-end 0)
  4363. '(display t invisible t intangible t))
  4364. t)))
  4365. (defun org-fontify-meta-lines-and-blocks (limit)
  4366. "Fontify #+ lines and blocks, in the correct ways."
  4367. (let ((case-fold-search t))
  4368. (if (re-search-forward
  4369. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4370. limit t)
  4371. (let ((beg (match-beginning 0))
  4372. (beg1 (line-beginning-position 2))
  4373. (dc1 (downcase (match-string 2)))
  4374. (dc3 (downcase (match-string 3)))
  4375. end end1 quoting block-type)
  4376. (cond
  4377. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4378. ;; a single line of backend-specific content
  4379. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4380. (remove-text-properties (match-beginning 0) (match-end 0)
  4381. '(display t invisible t intangible t))
  4382. (add-text-properties (match-beginning 1) (match-end 3)
  4383. '(font-lock-fontified t face org-meta-line))
  4384. (add-text-properties (match-beginning 6) (match-end 6)
  4385. '(font-lock-fontified t face org-block))
  4386. t)
  4387. ((and (match-end 4) (equal dc3 "begin"))
  4388. ;; Truly a block
  4389. (setq block-type (downcase (match-string 5))
  4390. quoting (member block-type org-protecting-blocks))
  4391. (when (re-search-forward
  4392. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4393. nil t) ;; on purpose, we look further than LIMIT
  4394. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4395. (when quoting
  4396. (remove-text-properties beg end
  4397. '(display t invisible t intangible t)))
  4398. (add-text-properties
  4399. beg end
  4400. '(font-lock-fontified t font-lock-multiline t))
  4401. (add-text-properties beg beg1 '(face org-meta-line))
  4402. (add-text-properties end1 end '(face org-meta-line))
  4403. (cond
  4404. (quoting
  4405. (add-text-properties beg1 end1 '(face org-block)))
  4406. ((not org-fontify-quote-and-verse-blocks))
  4407. ((string= block-type "quote")
  4408. (add-text-properties beg1 end1 '(face org-quote)))
  4409. ((string= block-type "verse")
  4410. (add-text-properties beg1 end1 '(face org-verse))))
  4411. t))
  4412. ((member dc1 '("title:" "author:" "email:" "date:"))
  4413. (add-text-properties
  4414. beg (match-end 3)
  4415. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4416. '(font-lock-fontified t invisible t)
  4417. '(font-lock-fontified t face org-document-info-keyword)))
  4418. (add-text-properties
  4419. (match-beginning 6) (match-end 6)
  4420. (if (string-equal dc1 "title:")
  4421. '(font-lock-fontified t face org-document-title)
  4422. '(font-lock-fontified t face org-document-info))))
  4423. ((not (member (char-after beg) '(?\ ?\t)))
  4424. ;; just any other in-buffer setting, but not indented
  4425. (add-text-properties
  4426. beg (match-end 0)
  4427. '(font-lock-fontified t face org-meta-line))
  4428. t)
  4429. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4430. "orgtbl:" "tblfm:" "tblname:" "result:"
  4431. "results:" "source:" "srcname:" "call:"))
  4432. (and (match-end 4) (equal dc3 "attr")))
  4433. (add-text-properties
  4434. beg (match-end 0)
  4435. '(font-lock-fontified t face org-meta-line))
  4436. t)
  4437. ((member dc3 '(" " ""))
  4438. (add-text-properties
  4439. beg (match-end 0)
  4440. '(font-lock-fontified t face font-lock-comment-face)))
  4441. (t nil))))))
  4442. (defun org-activate-angle-links (limit)
  4443. "Run through the buffer and add overlays to links."
  4444. (if (re-search-forward org-angle-link-re limit t)
  4445. (progn
  4446. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4447. (add-text-properties (match-beginning 0) (match-end 0)
  4448. (list 'mouse-face 'highlight
  4449. 'keymap org-mouse-map))
  4450. (org-rear-nonsticky-at (match-end 0))
  4451. t)))
  4452. (defun org-activate-footnote-links (limit)
  4453. "Run through the buffer and add overlays to links."
  4454. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4455. limit t)
  4456. (progn
  4457. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4458. (add-text-properties (match-beginning 2) (match-end 2)
  4459. (list 'mouse-face 'highlight
  4460. 'keymap org-mouse-map
  4461. 'help-echo
  4462. (if (= (point-at-bol) (match-beginning 2))
  4463. "Footnote definition"
  4464. "Footnote reference")
  4465. ))
  4466. (org-rear-nonsticky-at (match-end 2))
  4467. t)))
  4468. (defun org-activate-bracket-links (limit)
  4469. "Run through the buffer and add overlays to bracketed links."
  4470. (if (re-search-forward org-bracket-link-regexp limit t)
  4471. (let* ((help (concat "LINK: "
  4472. (org-match-string-no-properties 1)))
  4473. ;; FIXME: above we should remove the escapes.
  4474. ;; but that requires another match, protecting match data,
  4475. ;; a lot of overhead for font-lock.
  4476. (ip (org-maybe-intangible
  4477. (list 'invisible 'org-link
  4478. 'keymap org-mouse-map 'mouse-face 'highlight
  4479. 'font-lock-multiline t 'help-echo help)))
  4480. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4481. 'font-lock-multiline t 'help-echo help)))
  4482. ;; We need to remove the invisible property here. Table narrowing
  4483. ;; may have made some of this invisible.
  4484. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4485. (remove-text-properties (match-beginning 0) (match-end 0)
  4486. '(invisible nil))
  4487. (if (match-end 3)
  4488. (progn
  4489. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4490. (org-rear-nonsticky-at (match-beginning 3))
  4491. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4492. (org-rear-nonsticky-at (match-end 3))
  4493. (add-text-properties (match-end 3) (match-end 0) ip)
  4494. (org-rear-nonsticky-at (match-end 0)))
  4495. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4496. (org-rear-nonsticky-at (match-beginning 1))
  4497. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4498. (org-rear-nonsticky-at (match-end 1))
  4499. (add-text-properties (match-end 1) (match-end 0) ip)
  4500. (org-rear-nonsticky-at (match-end 0)))
  4501. t)))
  4502. (defun org-activate-dates (limit)
  4503. "Run through the buffer and add overlays to dates."
  4504. (if (re-search-forward org-tsr-regexp-both limit t)
  4505. (progn
  4506. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4507. (add-text-properties (match-beginning 0) (match-end 0)
  4508. (list 'mouse-face 'highlight
  4509. 'keymap org-mouse-map))
  4510. (org-rear-nonsticky-at (match-end 0))
  4511. (when org-display-custom-times
  4512. (if (match-end 3)
  4513. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4514. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4515. t)))
  4516. (defvar org-target-link-regexp nil
  4517. "Regular expression matching radio targets in plain text.")
  4518. (make-variable-buffer-local 'org-target-link-regexp)
  4519. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4520. "Regular expression matching a link target.")
  4521. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4522. "Regular expression matching a radio target.")
  4523. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4524. "Regular expression matching any target.")
  4525. (defun org-activate-target-links (limit)
  4526. "Run through the buffer and add overlays to target matches."
  4527. (when org-target-link-regexp
  4528. (let ((case-fold-search t))
  4529. (if (re-search-forward org-target-link-regexp limit t)
  4530. (progn
  4531. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4532. (add-text-properties (match-beginning 0) (match-end 0)
  4533. (list 'mouse-face 'highlight
  4534. 'keymap org-mouse-map
  4535. 'help-echo "Radio target link"
  4536. 'org-linked-text t))
  4537. (org-rear-nonsticky-at (match-end 0))
  4538. t)))))
  4539. (defun org-update-radio-target-regexp ()
  4540. "Find all radio targets in this file and update the regular expression."
  4541. (interactive)
  4542. (when (memq 'radio org-activate-links)
  4543. (setq org-target-link-regexp
  4544. (org-make-target-link-regexp (org-all-targets 'radio)))
  4545. (org-restart-font-lock)))
  4546. (defun org-hide-wide-columns (limit)
  4547. (let (s e)
  4548. (setq s (text-property-any (point) (or limit (point-max))
  4549. 'org-cwidth t))
  4550. (when s
  4551. (setq e (next-single-property-change s 'org-cwidth))
  4552. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4553. (goto-char e)
  4554. t)))
  4555. (defvar org-latex-and-specials-regexp nil
  4556. "Regular expression for highlighting export special stuff.")
  4557. (defvar org-match-substring-regexp)
  4558. (defvar org-match-substring-with-braces-regexp)
  4559. ;; This should be with the exporter code, but we also use if for font-locking
  4560. (defconst org-export-html-special-string-regexps
  4561. '(("\\\\-" . "&shy;")
  4562. ("---\\([^-]\\)" . "&mdash;\\1")
  4563. ("--\\([^-]\\)" . "&ndash;\\1")
  4564. ("\\.\\.\\." . "&hellip;"))
  4565. "Regular expressions for special string conversion.")
  4566. (defun org-compute-latex-and-specials-regexp ()
  4567. "Compute regular expression for stuff treated specially by exporters."
  4568. (if (not org-highlight-latex-fragments-and-specials)
  4569. (org-set-local 'org-latex-and-specials-regexp nil)
  4570. (require 'org-exp)
  4571. (let*
  4572. ((matchers (plist-get org-format-latex-options :matchers))
  4573. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4574. org-latex-regexps)))
  4575. (org-export-allow-BIND nil)
  4576. (options (org-combine-plists (org-default-export-plist)
  4577. (org-infile-export-plist)))
  4578. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4579. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4580. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4581. (org-export-html-expand (plist-get options :expand-quoted-html))
  4582. (org-export-with-special-strings (plist-get options :special-strings))
  4583. (re-sub
  4584. (cond
  4585. ((equal org-export-with-sub-superscripts '{})
  4586. (list org-match-substring-with-braces-regexp))
  4587. (org-export-with-sub-superscripts
  4588. (list org-match-substring-regexp))
  4589. (t nil)))
  4590. (re-latex
  4591. (if org-export-with-LaTeX-fragments
  4592. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4593. (re-macros
  4594. (if org-export-with-TeX-macros
  4595. (list (concat "\\\\"
  4596. (regexp-opt
  4597. (append (mapcar 'car (append org-entities-user
  4598. org-entities))
  4599. (if (boundp 'org-latex-entities)
  4600. (mapcar (lambda (x)
  4601. (or (car-safe x) x))
  4602. org-latex-entities)
  4603. nil))
  4604. 'words))) ; FIXME
  4605. ))
  4606. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4607. (re-special (if org-export-with-special-strings
  4608. (mapcar (lambda (x) (car x))
  4609. org-export-html-special-string-regexps)))
  4610. (re-rest
  4611. (delq nil
  4612. (list
  4613. (if org-export-html-expand "@<[^>\n]+>")
  4614. ))))
  4615. (org-set-local
  4616. 'org-latex-and-specials-regexp
  4617. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4618. re-rest) "\\|")))))
  4619. (defun org-do-latex-and-special-faces (limit)
  4620. "Run through the buffer and add overlays to links."
  4621. (when org-latex-and-specials-regexp
  4622. (let (rtn d)
  4623. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4624. limit t))
  4625. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4626. 'face))
  4627. '(org-code org-verbatim underline)))
  4628. (progn
  4629. (setq rtn t
  4630. d (cond ((member (char-after (1+ (match-beginning 0)))
  4631. '(?_ ?^)) 1)
  4632. (t 0)))
  4633. (font-lock-prepend-text-property
  4634. (+ d (match-beginning 0)) (match-end 0)
  4635. 'face 'org-latex-and-export-specials)
  4636. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4637. '(font-lock-multiline t)))))
  4638. rtn)))
  4639. (defun org-restart-font-lock ()
  4640. "Restart font-lock-mode, to force refontification."
  4641. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4642. (font-lock-mode -1)
  4643. (font-lock-mode 1)))
  4644. (defun org-all-targets (&optional radio)
  4645. "Return a list of all targets in this file.
  4646. With optional argument RADIO, only find radio targets."
  4647. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4648. rtn)
  4649. (save-excursion
  4650. (goto-char (point-min))
  4651. (while (re-search-forward re nil t)
  4652. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4653. rtn)))
  4654. (defun org-make-target-link-regexp (targets)
  4655. "Make regular expression matching all strings in TARGETS.
  4656. The regular expression finds the targets also if there is a line break
  4657. between words."
  4658. (and targets
  4659. (concat
  4660. "\\<\\("
  4661. (mapconcat
  4662. (lambda (x)
  4663. (while (string-match " +" x)
  4664. (setq x (replace-match "\\s-+" t t x)))
  4665. x)
  4666. targets
  4667. "\\|")
  4668. "\\)\\>")))
  4669. (defun org-activate-tags (limit)
  4670. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4671. (progn
  4672. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4673. (add-text-properties (match-beginning 1) (match-end 1)
  4674. (list 'mouse-face 'highlight
  4675. 'keymap org-mouse-map))
  4676. (org-rear-nonsticky-at (match-end 1))
  4677. t)))
  4678. (defun org-outline-level ()
  4679. "Compute the outline level of the heading at point.
  4680. This function assumes that the cursor is at the beginning of a line matched
  4681. by outline-regexp. Otherwise it returns garbage.
  4682. If this is called at a normal headline, the level is the number of stars.
  4683. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4684. For plain list items, if they are matched by `outline-regexp', this returns
  4685. 1000 plus the line indentation."
  4686. (save-excursion
  4687. (looking-at outline-regexp)
  4688. (if (match-beginning 1)
  4689. (+ (org-get-string-indentation (match-string 1)) 1000)
  4690. (1- (- (match-end 0) (match-beginning 0))))))
  4691. (defvar org-font-lock-keywords nil)
  4692. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4693. "Regular expression matching a property line.")
  4694. (defvar org-font-lock-hook nil
  4695. "Functions to be called for special font lock stuff.")
  4696. (defun org-font-lock-hook (limit)
  4697. (run-hook-with-args 'org-font-lock-hook limit))
  4698. (defun org-set-font-lock-defaults ()
  4699. (let* ((em org-fontify-emphasized-text)
  4700. (lk org-activate-links)
  4701. (org-font-lock-extra-keywords
  4702. (list
  4703. ;; Call the hook
  4704. '(org-font-lock-hook)
  4705. ;; Headlines
  4706. `(,(if org-fontify-whole-heading-line
  4707. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4708. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4709. (1 (org-get-level-face 1))
  4710. (2 (org-get-level-face 2))
  4711. (3 (org-get-level-face 3)))
  4712. ;; Table lines
  4713. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4714. (1 'org-table t))
  4715. ;; Table internals
  4716. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4717. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4718. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4719. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4720. ;; Drawers
  4721. (list org-drawer-regexp '(0 'org-special-keyword t))
  4722. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4723. ;; Properties
  4724. (list org-property-re
  4725. '(1 'org-special-keyword t)
  4726. '(3 'org-property-value t))
  4727. ;; Links
  4728. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4729. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4730. (if (memq 'plain lk) '(org-activate-plain-links))
  4731. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4732. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4733. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4734. (if (memq 'footnote lk) '(org-activate-footnote-links
  4735. (2 'org-footnote t)))
  4736. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4737. '(org-hide-wide-columns (0 nil append))
  4738. ;; TODO lines
  4739. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4740. '(1 (org-get-todo-face 1) t))
  4741. ;; DONE
  4742. (if org-fontify-done-headline
  4743. (list (concat "^[*]+ +\\<\\("
  4744. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4745. "\\)\\(.*\\)")
  4746. '(2 'org-headline-done t))
  4747. nil)
  4748. ;; Priorities
  4749. '(org-font-lock-add-priority-faces)
  4750. ;; Tags
  4751. '(org-font-lock-add-tag-faces)
  4752. ;; Special keywords
  4753. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4754. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4755. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4756. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4757. ;; Emphasis
  4758. (if em
  4759. (if (featurep 'xemacs)
  4760. '(org-do-emphasis-faces (0 nil append))
  4761. '(org-do-emphasis-faces)))
  4762. ;; Checkboxes
  4763. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4764. 2 'org-checkbox prepend)
  4765. (if org-provide-checkbox-statistics
  4766. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4767. (0 (org-get-checkbox-statistics-face) t)))
  4768. ;; Description list items
  4769. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4770. 2 'bold prepend)
  4771. ;; ARCHIVEd headings
  4772. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4773. '(1 'org-archived prepend))
  4774. ;; Specials
  4775. '(org-do-latex-and-special-faces)
  4776. '(org-fontify-entities)
  4777. '(org-raise-scripts)
  4778. ;; Code
  4779. '(org-activate-code (1 'org-code t))
  4780. ;; COMMENT
  4781. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4782. "\\|" org-quote-string "\\)\\>")
  4783. '(1 'org-special-keyword t))
  4784. '("^#.*" (0 'font-lock-comment-face t))
  4785. ;; Blocks and meta lines
  4786. '(org-fontify-meta-lines-and-blocks)
  4787. )))
  4788. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4789. ;; Now set the full font-lock-keywords
  4790. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4791. (org-set-local 'font-lock-defaults
  4792. '(org-font-lock-keywords t nil nil backward-paragraph))
  4793. (kill-local-variable 'font-lock-keywords) nil))
  4794. (defun org-toggle-pretty-entities ()
  4795. "Toggle the compostion display of entities as UTF8 characters."
  4796. (interactive)
  4797. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4798. (org-restart-font-lock)
  4799. (if org-pretty-entities
  4800. (message "Entities are displayed as UTF8 characers")
  4801. (save-restriction
  4802. (widen)
  4803. (decompose-region (point-min) (point-max))
  4804. (message "Entities are displayed plain"))))
  4805. (defun org-fontify-entities (limit)
  4806. "Find an entity to fontify."
  4807. (let (ee)
  4808. (when org-pretty-entities
  4809. (catch 'match
  4810. (while (re-search-forward
  4811. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  4812. limit t)
  4813. (if (and (not (org-in-indented-comment-line))
  4814. (setq ee (org-entity-get (match-string 1)))
  4815. (= (length (nth 6 ee)) 1))
  4816. (progn
  4817. (add-text-properties
  4818. (match-beginning 0) (match-end 1)
  4819. (list 'font-lock-fontified t))
  4820. (compose-region (match-beginning 0) (match-end 1)
  4821. (nth 6 ee) nil)
  4822. (backward-char 1)
  4823. (throw 'match t))))
  4824. nil))))
  4825. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4826. "Fontify string S like in Org-mode"
  4827. (with-temp-buffer
  4828. (insert s)
  4829. (let ((org-odd-levels-only odd-levels))
  4830. (org-mode)
  4831. (font-lock-fontify-buffer)
  4832. (buffer-string))))
  4833. (defvar org-m nil)
  4834. (defvar org-l nil)
  4835. (defvar org-f nil)
  4836. (defun org-get-level-face (n)
  4837. "Get the right face for match N in font-lock matching of headlines."
  4838. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4839. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4840. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4841. (cond
  4842. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4843. ((eq n 2) org-f)
  4844. (t (if org-level-color-stars-only nil org-f))))
  4845. (defun org-get-todo-face (kwd)
  4846. "Get the right face for a TODO keyword KWD.
  4847. If KWD is a number, get the corresponding match group."
  4848. (if (numberp kwd) (setq kwd (match-string kwd)))
  4849. (or (org-face-from-face-or-color
  4850. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4851. (and (member kwd org-done-keywords) 'org-done)
  4852. 'org-todo))
  4853. (defun org-face-from-face-or-color (context inherit face-or-color)
  4854. "Create a face list that inherits INHERIT, but sets the foreground color.
  4855. When FACE-OR-COLOR is not a string, just return it."
  4856. (if (stringp face-or-color)
  4857. (list :inherit inherit
  4858. (cdr (assoc context org-faces-easy-properties))
  4859. face-or-color)
  4860. face-or-color))
  4861. (defun org-font-lock-add-tag-faces (limit)
  4862. "Add the special tag faces."
  4863. (when (and org-tag-faces org-tags-special-faces-re)
  4864. (while (re-search-forward org-tags-special-faces-re limit t)
  4865. (add-text-properties (match-beginning 1) (match-end 1)
  4866. (list 'face (org-get-tag-face 1)
  4867. 'font-lock-fontified t))
  4868. (backward-char 1))))
  4869. (defun org-font-lock-add-priority-faces (limit)
  4870. "Add the special priority faces."
  4871. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4872. (add-text-properties
  4873. (match-beginning 0) (match-end 0)
  4874. (list 'face (or (org-face-from-face-or-color
  4875. 'priority 'org-special-keyword
  4876. (cdr (assoc (char-after (match-beginning 1))
  4877. org-priority-faces)))
  4878. 'org-special-keyword)
  4879. 'font-lock-fontified t))))
  4880. (defun org-get-tag-face (kwd)
  4881. "Get the right face for a TODO keyword KWD.
  4882. If KWD is a number, get the corresponding match group."
  4883. (if (numberp kwd) (setq kwd (match-string kwd)))
  4884. (or (org-face-from-face-or-color
  4885. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4886. 'org-tag))
  4887. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4888. "Remove fontification and activation overlays from links."
  4889. (font-lock-default-unfontify-region beg end)
  4890. (let* ((buffer-undo-list t)
  4891. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4892. (inhibit-modification-hooks t)
  4893. deactivate-mark buffer-file-name buffer-file-truename)
  4894. (decompose-region beg end)
  4895. (remove-text-properties
  4896. beg end
  4897. (if org-indent-mode
  4898. ;; also remove line-prefix and wrap-prefix properties
  4899. '(mouse-face t keymap t org-linked-text t
  4900. invisible t intangible t
  4901. line-prefix t wrap-prefix t
  4902. org-no-flyspell t org-emphasis t)
  4903. '(mouse-face t keymap t org-linked-text t
  4904. invisible t intangible t
  4905. org-no-flyspell t org-emphasis t)))
  4906. (org-remove-font-lock-display-properties beg end)))
  4907. (defconst org-script-display '(((raise -0.3) (height 0.7))
  4908. ((raise 0.3) (height 0.7))
  4909. ((raise -0.5))
  4910. ((raise 0.5)))
  4911. "Display properties for showing superscripts and subscripts.")
  4912. (defun org-remove-font-lock-display-properties (beg end)
  4913. "Remove specific display properties that have been added by font lock.
  4914. The will remove the raise properties that are used to show superscripts
  4915. and subscriipts."
  4916. (let (next prop)
  4917. (while (< beg end)
  4918. (setq next (next-single-property-change beg 'display nil end)
  4919. prop (get-text-property beg 'display))
  4920. (if (member prop org-script-display)
  4921. (put-text-property beg next 'display nil))
  4922. (setq beg next))))
  4923. (defun org-raise-scripts (limit)
  4924. "Add raise properties to sub/superscripts."
  4925. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  4926. (if (re-search-forward
  4927. (if (eq org-use-sub-superscripts t)
  4928. org-match-substring-regexp
  4929. org-match-substring-with-braces-regexp)
  4930. limit t)
  4931. (let* ((pos (point)) table-p comment-p
  4932. (mpos (match-beginning 3))
  4933. (emph-p (get-text-property mpos 'org-emphasis))
  4934. (link-p (get-text-property mpos 'mouse-face))
  4935. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  4936. (goto-char (point-at-bol))
  4937. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  4938. comment-p (org-looking-at-p "[ \t]*#"))
  4939. (goto-char pos)
  4940. (if (or comment-p emph-p link-p keyw-p)
  4941. t
  4942. (put-text-property (match-beginning 3) (match-end 0)
  4943. 'display
  4944. (if (equal (char-after (match-beginning 2)) ?^)
  4945. (nth (if table-p 3 1) org-script-display)
  4946. (nth (if table-p 2 0) org-script-display)))
  4947. (add-text-properties (match-beginning 2) (match-end 2)
  4948. (list 'invisible t
  4949. 'org-dwidth t 'org-dwidth-n 1))
  4950. (if (and (eq (char-after (match-beginning 3)) ?{)
  4951. (eq (char-before (match-end 3)) ?}))
  4952. (progn
  4953. (add-text-properties
  4954. (match-beginning 3) (1+ (match-beginning 3))
  4955. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  4956. (add-text-properties
  4957. (1- (match-end 3)) (match-end 3)
  4958. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  4959. t)))))
  4960. ;;;; Visibility cycling, including org-goto and indirect buffer
  4961. ;;; Cycling
  4962. (defvar org-cycle-global-status nil)
  4963. (make-variable-buffer-local 'org-cycle-global-status)
  4964. (defvar org-cycle-subtree-status nil)
  4965. (make-variable-buffer-local 'org-cycle-subtree-status)
  4966. ;;;###autoload
  4967. (defvar org-inlinetask-min-level)
  4968. (defun org-cycle (&optional arg)
  4969. "TAB-action and visibility cycling for Org-mode.
  4970. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4971. is outline visibility cycling, but it also invokes other actions
  4972. in special contexts.
  4973. - When this function is called with a prefix argument, rotate the entire
  4974. buffer through 3 states (global cycling)
  4975. 1. OVERVIEW: Show only top-level headlines.
  4976. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4977. 3. SHOW ALL: Show everything.
  4978. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4979. determined by the variable `org-startup-folded', and by any VISIBILITY
  4980. properties in the buffer.
  4981. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4982. including any drawers.
  4983. - When inside a table, re-align the table and move to the next field.
  4984. - When point is at the beginning of a headline, rotate the subtree started
  4985. by this line through 3 different states (local cycling)
  4986. 1. FOLDED: Only the main headline is shown.
  4987. 2. CHILDREN: The main headline and the direct children are shown.
  4988. From this state, you can move to one of the children
  4989. and zoom in further.
  4990. 3. SUBTREE: Show the entire subtree, including body text.
  4991. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4992. - When point is at the beginning of an empty headline and the variable
  4993. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4994. of the headline by demoting and promoting it to likely levels. This
  4995. speeds up creation document structure by presing TAB once or several
  4996. times right after creating a new headline.
  4997. - When there is a numeric prefix, go up to a heading with level ARG, do
  4998. a `show-subtree' and return to the previous cursor position. If ARG
  4999. is negative, go up that many levels.
  5000. - When point is not at the beginning of a headline, execute the global
  5001. binding for TAB, which is re-indenting the line. See the option
  5002. `org-cycle-emulate-tab' for details.
  5003. - Special case: if point is at the beginning of the buffer and there is
  5004. no headline in line 1, this function will act as if called with prefix arg.
  5005. But only if also the variable `org-cycle-global-at-bob' is t."
  5006. (interactive "P")
  5007. (org-load-modules-maybe)
  5008. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5009. (and org-cycle-level-after-item/entry-creation
  5010. (or (org-cycle-level)
  5011. (org-cycle-item-indentation))))
  5012. (let* ((limit-level
  5013. (or org-cycle-max-level
  5014. (and (boundp 'org-inlinetask-min-level)
  5015. org-inlinetask-min-level
  5016. (1- org-inlinetask-min-level))))
  5017. (nstars (and limit-level
  5018. (if org-odd-levels-only
  5019. (and limit-level (1- (* limit-level 2)))
  5020. limit-level)))
  5021. (outline-regexp
  5022. (cond
  5023. ((not (org-mode-p)) outline-regexp)
  5024. ((or (eq org-cycle-include-plain-lists 'integrate)
  5025. (and org-cycle-include-plain-lists (org-at-item-p)))
  5026. (concat "\\(?:\\*"
  5027. (if nstars (format "\\{1,%d\\}" nstars) "+")
  5028. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  5029. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  5030. (bob-special (and org-cycle-global-at-bob (bobp)
  5031. (not (looking-at outline-regexp))))
  5032. (org-cycle-hook
  5033. (if bob-special
  5034. (delq 'org-optimize-window-after-visibility-change
  5035. (copy-sequence org-cycle-hook))
  5036. org-cycle-hook))
  5037. (pos (point)))
  5038. (if (or bob-special (equal arg '(4)))
  5039. ;; special case: use global cycling
  5040. (setq arg t))
  5041. (cond
  5042. ((equal arg '(16))
  5043. (org-set-startup-visibility)
  5044. (message "Startup visibility, plus VISIBILITY properties"))
  5045. ((equal arg '(64))
  5046. (show-all)
  5047. (message "Entire buffer visible, including drawers"))
  5048. ((org-at-table-p 'any)
  5049. ;; Enter the table or move to the next field in the table
  5050. (if (org-at-table.el-p)
  5051. (message "Use C-c ' to edit table.el tables")
  5052. (if arg (org-table-edit-field t)
  5053. (org-table-justify-field-maybe)
  5054. (call-interactively 'org-table-next-field))))
  5055. ((run-hook-with-args-until-success
  5056. 'org-tab-after-check-for-table-hook))
  5057. ((eq arg t) ;; Global cycling
  5058. (org-cycle-internal-global))
  5059. ((and org-drawers org-drawer-regexp
  5060. (save-excursion
  5061. (beginning-of-line 1)
  5062. (looking-at org-drawer-regexp)))
  5063. ;; Toggle block visibility
  5064. (org-flag-drawer
  5065. (not (get-char-property (match-end 0) 'invisible))))
  5066. ((integerp arg)
  5067. ;; Show-subtree, ARG levels up from here.
  5068. (save-excursion
  5069. (org-back-to-heading)
  5070. (outline-up-heading (if (< arg 0) (- arg)
  5071. (- (funcall outline-level) arg)))
  5072. (org-show-subtree)))
  5073. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5074. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5075. (org-cycle-internal-local))
  5076. ;; TAB emulation and template completion
  5077. (buffer-read-only (org-back-to-heading))
  5078. ((run-hook-with-args-until-success
  5079. 'org-tab-after-check-for-cycling-hook))
  5080. ((org-try-structure-completion))
  5081. ((org-try-cdlatex-tab))
  5082. ((run-hook-with-args-until-success
  5083. 'org-tab-before-tab-emulation-hook))
  5084. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5085. (or (not (bolp))
  5086. (not (looking-at outline-regexp))))
  5087. (call-interactively (global-key-binding "\t")))
  5088. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5089. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5090. (or (and (eq org-cycle-emulate-tab 'white)
  5091. (= (match-end 0) (point-at-eol)))
  5092. (and (eq org-cycle-emulate-tab 'whitestart)
  5093. (>= (match-end 0) pos))))
  5094. t
  5095. (eq org-cycle-emulate-tab t))
  5096. (call-interactively (global-key-binding "\t")))
  5097. (t (save-excursion
  5098. (org-back-to-heading)
  5099. (org-cycle)))))))
  5100. (defun org-cycle-internal-global ()
  5101. "Do the global cycling action."
  5102. (cond
  5103. ((and (eq last-command this-command)
  5104. (eq org-cycle-global-status 'overview))
  5105. ;; We just created the overview - now do table of contents
  5106. ;; This can be slow in very large buffers, so indicate action
  5107. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5108. (message "CONTENTS...")
  5109. (org-content)
  5110. (message "CONTENTS...done")
  5111. (setq org-cycle-global-status 'contents)
  5112. (run-hook-with-args 'org-cycle-hook 'contents))
  5113. ((and (eq last-command this-command)
  5114. (eq org-cycle-global-status 'contents))
  5115. ;; We just showed the table of contents - now show everything
  5116. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5117. (show-all)
  5118. (message "SHOW ALL")
  5119. (setq org-cycle-global-status 'all)
  5120. (run-hook-with-args 'org-cycle-hook 'all))
  5121. (t
  5122. ;; Default action: go to overview
  5123. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5124. (org-overview)
  5125. (message "OVERVIEW")
  5126. (setq org-cycle-global-status 'overview)
  5127. (run-hook-with-args 'org-cycle-hook 'overview))))
  5128. (defun org-cycle-internal-local ()
  5129. "Do the local cycling action."
  5130. (org-back-to-heading)
  5131. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  5132. ;; First, some boundaries
  5133. (save-excursion
  5134. (org-back-to-heading)
  5135. (setq level (funcall outline-level))
  5136. (save-excursion
  5137. (beginning-of-line 2)
  5138. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  5139. ; XEmacs does not have `next-single-char-property-change'
  5140. ; I'm not sure about Emacs 21.
  5141. (while (and (not (eobp)) ;; this is like `next-line'
  5142. (get-char-property (1- (point)) 'invisible))
  5143. (beginning-of-line 2))
  5144. (while (and (not (eobp)) ;; this is like `next-line'
  5145. (get-char-property (1- (point)) 'invisible))
  5146. (goto-char (next-single-char-property-change (point) 'invisible))
  5147. (and (eolp) (beginning-of-line 2))))
  5148. (setq eol (point)))
  5149. (outline-end-of-heading) (setq eoh (point))
  5150. (save-excursion
  5151. (outline-next-heading)
  5152. (setq has-children (and (org-at-heading-p t)
  5153. (> (funcall outline-level) level))))
  5154. (org-end-of-subtree t)
  5155. (unless (eobp)
  5156. (skip-chars-forward " \t\n")
  5157. (beginning-of-line 1) ; in case this is an item
  5158. )
  5159. (setq eos (if (eobp) (point) (1- (point)))))
  5160. ;; Find out what to do next and set `this-command'
  5161. (cond
  5162. ((= eos eoh)
  5163. ;; Nothing is hidden behind this heading
  5164. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5165. (message "EMPTY ENTRY")
  5166. (setq org-cycle-subtree-status nil)
  5167. (save-excursion
  5168. (goto-char eos)
  5169. (outline-next-heading)
  5170. (if (org-invisible-p) (org-flag-heading nil))))
  5171. ((and (or (>= eol eos)
  5172. (not (string-match "\\S-" (buffer-substring eol eos))))
  5173. (or has-children
  5174. (not (setq children-skipped
  5175. org-cycle-skip-children-state-if-no-children))))
  5176. ;; Entire subtree is hidden in one line: children view
  5177. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5178. (org-show-entry)
  5179. (show-children)
  5180. (message "CHILDREN")
  5181. (save-excursion
  5182. (goto-char eos)
  5183. (outline-next-heading)
  5184. (if (org-invisible-p) (org-flag-heading nil)))
  5185. (setq org-cycle-subtree-status 'children)
  5186. (run-hook-with-args 'org-cycle-hook 'children))
  5187. ((or children-skipped
  5188. (and (eq last-command this-command)
  5189. (eq org-cycle-subtree-status 'children)))
  5190. ;; We just showed the children, or no children are there,
  5191. ;; now show everything.
  5192. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5193. (org-show-subtree)
  5194. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5195. (setq org-cycle-subtree-status 'subtree)
  5196. (run-hook-with-args 'org-cycle-hook 'subtree))
  5197. (t
  5198. ;; Default action: hide the subtree.
  5199. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5200. (hide-subtree)
  5201. (message "FOLDED")
  5202. (setq org-cycle-subtree-status 'folded)
  5203. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5204. ;;;###autoload
  5205. (defun org-global-cycle (&optional arg)
  5206. "Cycle the global visibility. For details see `org-cycle'.
  5207. With C-u prefix arg, switch to startup visibility.
  5208. With a numeric prefix, show all headlines up to that level."
  5209. (interactive "P")
  5210. (let ((org-cycle-include-plain-lists
  5211. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5212. (cond
  5213. ((integerp arg)
  5214. (show-all)
  5215. (hide-sublevels arg)
  5216. (setq org-cycle-global-status 'contents))
  5217. ((equal arg '(4))
  5218. (org-set-startup-visibility)
  5219. (message "Startup visibility, plus VISIBILITY properties."))
  5220. (t
  5221. (org-cycle '(4))))))
  5222. (defun org-set-startup-visibility ()
  5223. "Set the visibility required by startup options and properties."
  5224. (cond
  5225. ((eq org-startup-folded t)
  5226. (org-cycle '(4)))
  5227. ((eq org-startup-folded 'content)
  5228. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5229. (org-cycle '(4)) (org-cycle '(4)))))
  5230. (unless (eq org-startup-folded 'showeverything)
  5231. (if org-hide-block-startup (org-hide-block-all))
  5232. (org-set-visibility-according-to-property 'no-cleanup)
  5233. (org-cycle-hide-archived-subtrees 'all)
  5234. (org-cycle-hide-drawers 'all)
  5235. (org-cycle-show-empty-lines t)))
  5236. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5237. "Switch subtree visibilities according to :VISIBILITY: property."
  5238. (interactive)
  5239. (let (org-show-entry-below state)
  5240. (save-excursion
  5241. (goto-char (point-min))
  5242. (while (re-search-forward
  5243. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5244. nil t)
  5245. (setq state (match-string 1))
  5246. (save-excursion
  5247. (org-back-to-heading t)
  5248. (hide-subtree)
  5249. (org-reveal)
  5250. (cond
  5251. ((equal state '("fold" "folded"))
  5252. (hide-subtree))
  5253. ((equal state "children")
  5254. (org-show-hidden-entry)
  5255. (show-children))
  5256. ((equal state "content")
  5257. (save-excursion
  5258. (save-restriction
  5259. (org-narrow-to-subtree)
  5260. (org-content))))
  5261. ((member state '("all" "showall"))
  5262. (show-subtree)))))
  5263. (unless no-cleanup
  5264. (org-cycle-hide-archived-subtrees 'all)
  5265. (org-cycle-hide-drawers 'all)
  5266. (org-cycle-show-empty-lines 'all)))))
  5267. (defun org-overview ()
  5268. "Switch to overview mode, showing only top-level headlines.
  5269. Really, this shows all headlines with level equal or greater than the level
  5270. of the first headline in the buffer. This is important, because if the
  5271. first headline is not level one, then (hide-sublevels 1) gives confusing
  5272. results."
  5273. (interactive)
  5274. (let ((level (save-excursion
  5275. (goto-char (point-min))
  5276. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5277. (progn
  5278. (goto-char (match-beginning 0))
  5279. (funcall outline-level))))))
  5280. (and level (hide-sublevels level))))
  5281. (defun org-content (&optional arg)
  5282. "Show all headlines in the buffer, like a table of contents.
  5283. With numerical argument N, show content up to level N."
  5284. (interactive "P")
  5285. (save-excursion
  5286. ;; Visit all headings and show their offspring
  5287. (and (integerp arg) (org-overview))
  5288. (goto-char (point-max))
  5289. (catch 'exit
  5290. (while (and (progn (condition-case nil
  5291. (outline-previous-visible-heading 1)
  5292. (error (goto-char (point-min))))
  5293. t)
  5294. (looking-at outline-regexp))
  5295. (if (integerp arg)
  5296. (show-children (1- arg))
  5297. (show-branches))
  5298. (if (bobp) (throw 'exit nil))))))
  5299. (defun org-optimize-window-after-visibility-change (state)
  5300. "Adjust the window after a change in outline visibility.
  5301. This function is the default value of the hook `org-cycle-hook'."
  5302. (when (get-buffer-window (current-buffer))
  5303. (cond
  5304. ((eq state 'content) nil)
  5305. ((eq state 'all) nil)
  5306. ((eq state 'folded) nil)
  5307. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5308. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5309. (defun org-remove-empty-overlays-at (pos)
  5310. "Remove outline overlays that do not contain non-white stuff."
  5311. (mapc
  5312. (lambda (o)
  5313. (and (eq 'outline (overlay-get o 'invisible))
  5314. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5315. (overlay-end o))))
  5316. (delete-overlay o)))
  5317. (overlays-at pos)))
  5318. (defun org-clean-visibility-after-subtree-move ()
  5319. "Fix visibility issues after moving a subtree."
  5320. ;; First, find a reasonable region to look at:
  5321. ;; Start two siblings above, end three below
  5322. (let* ((beg (save-excursion
  5323. (and (org-get-last-sibling)
  5324. (org-get-last-sibling))
  5325. (point)))
  5326. (end (save-excursion
  5327. (and (org-get-next-sibling)
  5328. (org-get-next-sibling)
  5329. (org-get-next-sibling))
  5330. (if (org-at-heading-p)
  5331. (point-at-eol)
  5332. (point))))
  5333. (level (looking-at "\\*+"))
  5334. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5335. (save-excursion
  5336. (save-restriction
  5337. (narrow-to-region beg end)
  5338. (when re
  5339. ;; Properly fold already folded siblings
  5340. (goto-char (point-min))
  5341. (while (re-search-forward re nil t)
  5342. (if (and (not (org-invisible-p))
  5343. (save-excursion
  5344. (goto-char (point-at-eol)) (org-invisible-p)))
  5345. (hide-entry))))
  5346. (org-cycle-show-empty-lines 'overview)
  5347. (org-cycle-hide-drawers 'overview)))))
  5348. (defun org-cycle-show-empty-lines (state)
  5349. "Show empty lines above all visible headlines.
  5350. The region to be covered depends on STATE when called through
  5351. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5352. entire buffer covered. Note that an empty line is only shown if there
  5353. are at least `org-cycle-separator-lines' empty lines before the headline."
  5354. (when (not (= org-cycle-separator-lines 0))
  5355. (save-excursion
  5356. (let* ((n (abs org-cycle-separator-lines))
  5357. (re (cond
  5358. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5359. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5360. (t (let ((ns (number-to-string (- n 2))))
  5361. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5362. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5363. beg end b e)
  5364. (cond
  5365. ((memq state '(overview contents t))
  5366. (setq beg (point-min) end (point-max)))
  5367. ((memq state '(children folded))
  5368. (setq beg (point) end (progn (org-end-of-subtree t t)
  5369. (beginning-of-line 2)
  5370. (point)))))
  5371. (when beg
  5372. (goto-char beg)
  5373. (while (re-search-forward re end t)
  5374. (unless (get-char-property (match-end 1) 'invisible)
  5375. (setq e (match-end 1))
  5376. (if (< org-cycle-separator-lines 0)
  5377. (setq b (save-excursion
  5378. (goto-char (match-beginning 0))
  5379. (org-back-over-empty-lines)
  5380. (if (save-excursion
  5381. (goto-char (max (point-min) (1- (point))))
  5382. (org-on-heading-p))
  5383. (1- (point))
  5384. (point))))
  5385. (setq b (match-beginning 1)))
  5386. (outline-flag-region b e nil)))))))
  5387. ;; Never hide empty lines at the end of the file.
  5388. (save-excursion
  5389. (goto-char (point-max))
  5390. (outline-previous-heading)
  5391. (outline-end-of-heading)
  5392. (if (and (looking-at "[ \t\n]+")
  5393. (= (match-end 0) (point-max)))
  5394. (outline-flag-region (point) (match-end 0) nil))))
  5395. (defun org-show-empty-lines-in-parent ()
  5396. "Move to the parent and re-show empty lines before visible headlines."
  5397. (save-excursion
  5398. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5399. (org-cycle-show-empty-lines context))))
  5400. (defun org-files-list ()
  5401. "Return `org-agenda-files' list, plus all open org-mode files.
  5402. This is useful for operations that need to scan all of a user's
  5403. open and agenda-wise Org files."
  5404. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5405. (dolist (buf (buffer-list))
  5406. (with-current-buffer buf
  5407. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5408. (let ((file (expand-file-name (buffer-file-name))))
  5409. (unless (member file files)
  5410. (push file files))))))
  5411. files))
  5412. (defsubst org-entry-beginning-position ()
  5413. "Return the beginning position of the current entry."
  5414. (save-excursion (outline-back-to-heading t) (point)))
  5415. (defsubst org-entry-end-position ()
  5416. "Return the end position of the current entry."
  5417. (save-excursion (outline-next-heading) (point)))
  5418. (defun org-cycle-hide-drawers (state)
  5419. "Re-hide all drawers after a visibility state change."
  5420. (when (and (org-mode-p)
  5421. (not (memq state '(overview folded contents))))
  5422. (save-excursion
  5423. (let* ((globalp (memq state '(contents all)))
  5424. (beg (if globalp (point-min) (point)))
  5425. (end (if globalp (point-max)
  5426. (if (eq state 'children)
  5427. (save-excursion (outline-next-heading) (point))
  5428. (org-end-of-subtree t)))))
  5429. (goto-char beg)
  5430. (while (re-search-forward org-drawer-regexp end t)
  5431. (org-flag-drawer t))))))
  5432. (defun org-flag-drawer (flag)
  5433. (save-excursion
  5434. (beginning-of-line 1)
  5435. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5436. (let ((b (match-end 0))
  5437. (outline-regexp org-outline-regexp))
  5438. (if (re-search-forward
  5439. "^[ \t]*:END:"
  5440. (save-excursion (outline-next-heading) (point)) t)
  5441. (outline-flag-region b (point-at-eol) flag)
  5442. (error ":END: line missing at position %s" b))))))
  5443. (defun org-subtree-end-visible-p ()
  5444. "Is the end of the current subtree visible?"
  5445. (pos-visible-in-window-p
  5446. (save-excursion (org-end-of-subtree t) (point))))
  5447. (defun org-first-headline-recenter (&optional N)
  5448. "Move cursor to the first headline and recenter the headline.
  5449. Optional argument N means put the headline into the Nth line of the window."
  5450. (goto-char (point-min))
  5451. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5452. (beginning-of-line)
  5453. (recenter (prefix-numeric-value N))))
  5454. ;;; Saving and restoring visibility
  5455. (defun org-outline-overlay-data (&optional use-markers)
  5456. "Return a list of the locations of all outline overlays.
  5457. The are overlays with the `invisible' property value `outline'.
  5458. The return valus is a list of cons cells, with start and stop
  5459. positions for each overlay.
  5460. If USE-MARKERS is set, return the positions as markers."
  5461. (let (beg end)
  5462. (save-excursion
  5463. (save-restriction
  5464. (widen)
  5465. (delq nil
  5466. (mapcar (lambda (o)
  5467. (when (eq (overlay-get o 'invisible) 'outline)
  5468. (setq beg (overlay-start o)
  5469. end (overlay-end o))
  5470. (and beg end (> end beg)
  5471. (if use-markers
  5472. (cons (move-marker (make-marker) beg)
  5473. (move-marker (make-marker) end))
  5474. (cons beg end)))))
  5475. (overlays-in (point-min) (point-max))))))))
  5476. (defun org-set-outline-overlay-data (data)
  5477. "Create visibility overlays for all positions in DATA.
  5478. DATA should have been made by `org-outline-overlay-data'."
  5479. (let (o)
  5480. (save-excursion
  5481. (save-restriction
  5482. (widen)
  5483. (show-all)
  5484. (mapc (lambda (c)
  5485. (setq o (make-overlay (car c) (cdr c)))
  5486. (overlay-put o 'invisible 'outline))
  5487. data)))))
  5488. (defmacro org-save-outline-visibility (use-markers &rest body)
  5489. "Save and restore outline visibility around BODY.
  5490. If USE-MARKERS is non-nil, use markers for the positions.
  5491. This means that the buffer may change while running BODY,
  5492. but it also means that the buffer should stay alive
  5493. during the operation, because otherwise all these markers will
  5494. point nowhere."
  5495. (declare (indent 1))
  5496. `(let ((data (org-outline-overlay-data ,use-markers)))
  5497. (unwind-protect
  5498. (progn
  5499. ,@body
  5500. (org-set-outline-overlay-data data))
  5501. (when ,use-markers
  5502. (mapc (lambda (c)
  5503. (and (markerp (car c)) (move-marker (car c) nil))
  5504. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5505. data)))))
  5506. ;;; Folding of blocks
  5507. (defconst org-block-regexp
  5508. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5509. "Regular expression for hiding blocks.")
  5510. (defvar org-hide-block-overlays nil
  5511. "Overlays hiding blocks.")
  5512. (make-variable-buffer-local 'org-hide-block-overlays)
  5513. (defun org-block-map (function &optional start end)
  5514. "Call func at the head of all source blocks in the current
  5515. buffer. Optional arguments START and END can be used to limit
  5516. the range."
  5517. (let ((start (or start (point-min)))
  5518. (end (or end (point-max))))
  5519. (save-excursion
  5520. (goto-char start)
  5521. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5522. (save-excursion
  5523. (save-match-data
  5524. (goto-char (match-beginning 0))
  5525. (funcall function)))))))
  5526. (defun org-hide-block-toggle-all ()
  5527. "Toggle the visibility of all blocks in the current buffer."
  5528. (org-block-map #'org-hide-block-toggle))
  5529. (defun org-hide-block-all ()
  5530. "Fold all blocks in the current buffer."
  5531. (interactive)
  5532. (org-show-block-all)
  5533. (org-block-map #'org-hide-block-toggle-maybe))
  5534. (defun org-show-block-all ()
  5535. "Unfold all blocks in the current buffer."
  5536. (interactive)
  5537. (mapc 'delete-overlay org-hide-block-overlays)
  5538. (setq org-hide-block-overlays nil))
  5539. (defun org-hide-block-toggle-maybe ()
  5540. "Toggle visibility of block at point."
  5541. (interactive)
  5542. (let ((case-fold-search t))
  5543. (if (save-excursion
  5544. (beginning-of-line 1)
  5545. (looking-at org-block-regexp))
  5546. (progn (org-hide-block-toggle)
  5547. t) ;; to signal that we took action
  5548. nil))) ;; to signal that we did not
  5549. (defun org-hide-block-toggle (&optional force)
  5550. "Toggle the visibility of the current block."
  5551. (interactive)
  5552. (save-excursion
  5553. (beginning-of-line)
  5554. (if (re-search-forward org-block-regexp nil t)
  5555. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5556. (end (match-end 0)) ;; end of entire body
  5557. ov)
  5558. (if (memq t (mapcar (lambda (overlay)
  5559. (eq (overlay-get overlay 'invisible)
  5560. 'org-hide-block))
  5561. (overlays-at start)))
  5562. (if (or (not force) (eq force 'off))
  5563. (mapc (lambda (ov)
  5564. (when (member ov org-hide-block-overlays)
  5565. (setq org-hide-block-overlays
  5566. (delq ov org-hide-block-overlays)))
  5567. (when (eq (overlay-get ov 'invisible)
  5568. 'org-hide-block)
  5569. (delete-overlay ov)))
  5570. (overlays-at start)))
  5571. (setq ov (make-overlay start end))
  5572. (overlay-put ov 'invisible 'org-hide-block)
  5573. ;; make the block accessible to isearch
  5574. (overlay-put
  5575. ov 'isearch-open-invisible
  5576. (lambda (ov)
  5577. (when (member ov org-hide-block-overlays)
  5578. (setq org-hide-block-overlays
  5579. (delq ov org-hide-block-overlays)))
  5580. (when (eq (overlay-get ov 'invisible)
  5581. 'org-hide-block)
  5582. (delete-overlay ov))))
  5583. (push ov org-hide-block-overlays)))
  5584. (error "Not looking at a source block"))))
  5585. ;; org-tab-after-check-for-cycling-hook
  5586. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5587. ;; Remove overlays when changing major mode
  5588. (add-hook 'org-mode-hook
  5589. (lambda () (org-add-hook 'change-major-mode-hook
  5590. 'org-show-block-all 'append 'local)))
  5591. ;;; Org-goto
  5592. (defvar org-goto-window-configuration nil)
  5593. (defvar org-goto-marker nil)
  5594. (defvar org-goto-map
  5595. (let ((map (make-sparse-keymap)))
  5596. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5597. (while (setq cmd (pop cmds))
  5598. (substitute-key-definition cmd cmd map global-map)))
  5599. (suppress-keymap map)
  5600. (org-defkey map "\C-m" 'org-goto-ret)
  5601. (org-defkey map [(return)] 'org-goto-ret)
  5602. (org-defkey map [(left)] 'org-goto-left)
  5603. (org-defkey map [(right)] 'org-goto-right)
  5604. (org-defkey map [(control ?g)] 'org-goto-quit)
  5605. (org-defkey map "\C-i" 'org-cycle)
  5606. (org-defkey map [(tab)] 'org-cycle)
  5607. (org-defkey map [(down)] 'outline-next-visible-heading)
  5608. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5609. (if org-goto-auto-isearch
  5610. (if (fboundp 'define-key-after)
  5611. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5612. nil)
  5613. (org-defkey map "q" 'org-goto-quit)
  5614. (org-defkey map "n" 'outline-next-visible-heading)
  5615. (org-defkey map "p" 'outline-previous-visible-heading)
  5616. (org-defkey map "f" 'outline-forward-same-level)
  5617. (org-defkey map "b" 'outline-backward-same-level)
  5618. (org-defkey map "u" 'outline-up-heading))
  5619. (org-defkey map "/" 'org-occur)
  5620. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5621. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5622. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5623. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5624. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5625. map))
  5626. (defconst org-goto-help
  5627. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5628. RET=jump to location [Q]uit and return to previous location
  5629. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5630. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5631. ;; FIXME: Docstring does not mention both interfaces
  5632. (defun org-goto (&optional alternative-interface)
  5633. "Look up a different location in the current file, keeping current visibility.
  5634. When you want look-up or go to a different location in a document, the
  5635. fastest way is often to fold the entire buffer and then dive into the tree.
  5636. This method has the disadvantage, that the previous location will be folded,
  5637. which may not be what you want.
  5638. This command works around this by showing a copy of the current buffer
  5639. in an indirect buffer, in overview mode. You can dive into the tree in
  5640. that copy, use org-occur and incremental search to find a location.
  5641. When pressing RET or `Q', the command returns to the original buffer in
  5642. which the visibility is still unchanged. After RET is will also jump to
  5643. the location selected in the indirect buffer and expose the
  5644. the headline hierarchy above."
  5645. (interactive "P")
  5646. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5647. (org-refile-use-outline-path t)
  5648. (org-refile-target-verify-function nil)
  5649. (interface
  5650. (if (not alternative-interface)
  5651. org-goto-interface
  5652. (if (eq org-goto-interface 'outline)
  5653. 'outline-path-completion
  5654. 'outline)))
  5655. (org-goto-start-pos (point))
  5656. (selected-point
  5657. (if (eq interface 'outline)
  5658. (car (org-get-location (current-buffer) org-goto-help))
  5659. (nth 3 (org-refile-get-location "Goto: ")))))
  5660. (if selected-point
  5661. (progn
  5662. (org-mark-ring-push org-goto-start-pos)
  5663. (goto-char selected-point)
  5664. (if (or (org-invisible-p) (org-invisible-p2))
  5665. (org-show-context 'org-goto)))
  5666. (message "Quit"))))
  5667. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5668. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5669. (defvar org-goto-local-auto-isearch-map) ; defined below
  5670. (defun org-get-location (buf help)
  5671. "Let the user select a location in the Org-mode buffer BUF.
  5672. This function uses a recursive edit. It returns the selected position
  5673. or nil."
  5674. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5675. (isearch-hide-immediately nil)
  5676. (isearch-search-fun-function
  5677. (lambda () 'org-goto-local-search-headings))
  5678. (org-goto-selected-point org-goto-exit-command)
  5679. (pop-up-frames nil)
  5680. (special-display-buffer-names nil)
  5681. (special-display-regexps nil)
  5682. (special-display-function nil))
  5683. (save-excursion
  5684. (save-window-excursion
  5685. (delete-other-windows)
  5686. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5687. (switch-to-buffer
  5688. (condition-case nil
  5689. (make-indirect-buffer (current-buffer) "*org-goto*")
  5690. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5691. (with-output-to-temp-buffer "*Help*"
  5692. (princ help))
  5693. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5694. (setq buffer-read-only nil)
  5695. (let ((org-startup-truncated t)
  5696. (org-startup-folded nil)
  5697. (org-startup-align-all-tables nil))
  5698. (org-mode)
  5699. (org-overview))
  5700. (setq buffer-read-only t)
  5701. (if (and (boundp 'org-goto-start-pos)
  5702. (integer-or-marker-p org-goto-start-pos))
  5703. (let ((org-show-hierarchy-above t)
  5704. (org-show-siblings t)
  5705. (org-show-following-heading t))
  5706. (goto-char org-goto-start-pos)
  5707. (and (org-invisible-p) (org-show-context)))
  5708. (goto-char (point-min)))
  5709. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5710. (message "Select location and press RET")
  5711. (use-local-map org-goto-map)
  5712. (recursive-edit)
  5713. ))
  5714. (kill-buffer "*org-goto*")
  5715. (cons org-goto-selected-point org-goto-exit-command)))
  5716. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5717. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5718. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5719. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5720. (defun org-goto-local-search-headings (string bound noerror)
  5721. "Search and make sure that any matches are in headlines."
  5722. (catch 'return
  5723. (while (if isearch-forward
  5724. (search-forward string bound noerror)
  5725. (search-backward string bound noerror))
  5726. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5727. (and (member :headline context)
  5728. (not (member :tags context))))
  5729. (throw 'return (point))))))
  5730. (defun org-goto-local-auto-isearch ()
  5731. "Start isearch."
  5732. (interactive)
  5733. (goto-char (point-min))
  5734. (let ((keys (this-command-keys)))
  5735. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5736. (isearch-mode t)
  5737. (isearch-process-search-char (string-to-char keys)))))
  5738. (defun org-goto-ret (&optional arg)
  5739. "Finish `org-goto' by going to the new location."
  5740. (interactive "P")
  5741. (setq org-goto-selected-point (point)
  5742. org-goto-exit-command 'return)
  5743. (throw 'exit nil))
  5744. (defun org-goto-left ()
  5745. "Finish `org-goto' by going to the new location."
  5746. (interactive)
  5747. (if (org-on-heading-p)
  5748. (progn
  5749. (beginning-of-line 1)
  5750. (setq org-goto-selected-point (point)
  5751. org-goto-exit-command 'left)
  5752. (throw 'exit nil))
  5753. (error "Not on a heading")))
  5754. (defun org-goto-right ()
  5755. "Finish `org-goto' by going to the new location."
  5756. (interactive)
  5757. (if (org-on-heading-p)
  5758. (progn
  5759. (setq org-goto-selected-point (point)
  5760. org-goto-exit-command 'right)
  5761. (throw 'exit nil))
  5762. (error "Not on a heading")))
  5763. (defun org-goto-quit ()
  5764. "Finish `org-goto' without cursor motion."
  5765. (interactive)
  5766. (setq org-goto-selected-point nil)
  5767. (setq org-goto-exit-command 'quit)
  5768. (throw 'exit nil))
  5769. ;;; Indirect buffer display of subtrees
  5770. (defvar org-indirect-dedicated-frame nil
  5771. "This is the frame being used for indirect tree display.")
  5772. (defvar org-last-indirect-buffer nil)
  5773. (defun org-tree-to-indirect-buffer (&optional arg)
  5774. "Create indirect buffer and narrow it to current subtree.
  5775. With numerical prefix ARG, go up to this level and then take that tree.
  5776. If ARG is negative, go up that many levels.
  5777. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5778. indirect buffer previously made with this command, to avoid proliferation of
  5779. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5780. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5781. is kept so that you can work with several indirect buffers at the same time.
  5782. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5783. requests that a new frame be made for the new buffer, so that the dedicated
  5784. frame is not changed."
  5785. (interactive "P")
  5786. (let ((cbuf (current-buffer))
  5787. (cwin (selected-window))
  5788. (pos (point))
  5789. beg end level heading ibuf)
  5790. (save-excursion
  5791. (org-back-to-heading t)
  5792. (when (numberp arg)
  5793. (setq level (org-outline-level))
  5794. (if (< arg 0) (setq arg (+ level arg)))
  5795. (while (> (setq level (org-outline-level)) arg)
  5796. (outline-up-heading 1 t)))
  5797. (setq beg (point)
  5798. heading (org-get-heading))
  5799. (org-end-of-subtree t t)
  5800. (if (org-on-heading-p) (backward-char 1))
  5801. (setq end (point)))
  5802. (if (and (buffer-live-p org-last-indirect-buffer)
  5803. (not (eq org-indirect-buffer-display 'new-frame))
  5804. (not arg))
  5805. (kill-buffer org-last-indirect-buffer))
  5806. (setq ibuf (org-get-indirect-buffer cbuf)
  5807. org-last-indirect-buffer ibuf)
  5808. (cond
  5809. ((or (eq org-indirect-buffer-display 'new-frame)
  5810. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5811. (select-frame (make-frame))
  5812. (delete-other-windows)
  5813. (switch-to-buffer ibuf)
  5814. (org-set-frame-title heading))
  5815. ((eq org-indirect-buffer-display 'dedicated-frame)
  5816. (raise-frame
  5817. (select-frame (or (and org-indirect-dedicated-frame
  5818. (frame-live-p org-indirect-dedicated-frame)
  5819. org-indirect-dedicated-frame)
  5820. (setq org-indirect-dedicated-frame (make-frame)))))
  5821. (delete-other-windows)
  5822. (switch-to-buffer ibuf)
  5823. (org-set-frame-title (concat "Indirect: " heading)))
  5824. ((eq org-indirect-buffer-display 'current-window)
  5825. (switch-to-buffer ibuf))
  5826. ((eq org-indirect-buffer-display 'other-window)
  5827. (pop-to-buffer ibuf))
  5828. (t (error "Invalid value")))
  5829. (if (featurep 'xemacs)
  5830. (save-excursion (org-mode) (turn-on-font-lock)))
  5831. (narrow-to-region beg end)
  5832. (show-all)
  5833. (goto-char pos)
  5834. (and (window-live-p cwin) (select-window cwin))))
  5835. (defun org-get-indirect-buffer (&optional buffer)
  5836. (setq buffer (or buffer (current-buffer)))
  5837. (let ((n 1) (base (buffer-name buffer)) bname)
  5838. (while (buffer-live-p
  5839. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5840. (setq n (1+ n)))
  5841. (condition-case nil
  5842. (make-indirect-buffer buffer bname 'clone)
  5843. (error (make-indirect-buffer buffer bname)))))
  5844. (defun org-set-frame-title (title)
  5845. "Set the title of the current frame to the string TITLE."
  5846. ;; FIXME: how to name a single frame in XEmacs???
  5847. (unless (featurep 'xemacs)
  5848. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5849. ;;;; Structure editing
  5850. ;;; Inserting headlines
  5851. (defun org-previous-line-empty-p ()
  5852. (save-excursion
  5853. (and (not (bobp))
  5854. (or (beginning-of-line 0) t)
  5855. (save-match-data
  5856. (looking-at "[ \t]*$")))))
  5857. (defun org-insert-heading (&optional force-heading invisible-ok)
  5858. "Insert a new heading or item with same depth at point.
  5859. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5860. If point is at the beginning of a headline, insert a sibling before the
  5861. current headline. If point is not at the beginning, do not split the line,
  5862. but create the new headline after the current line.
  5863. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5864. This is important for non-interactive uses of the command."
  5865. (interactive "P")
  5866. (if (or (= (buffer-size) 0)
  5867. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5868. (org-on-heading-p))))
  5869. (not (org-in-item-p))))
  5870. (insert "\n* ")
  5871. (when (or force-heading (not (org-insert-item)))
  5872. (let* ((empty-line-p nil)
  5873. (head (save-excursion
  5874. (condition-case nil
  5875. (progn
  5876. (org-back-to-heading invisible-ok)
  5877. (setq empty-line-p (org-previous-line-empty-p))
  5878. (match-string 0))
  5879. (error "*"))))
  5880. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5881. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5882. pos hide-previous previous-pos)
  5883. (cond
  5884. ((and (org-on-heading-p) (bolp)
  5885. (or (bobp)
  5886. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5887. ;; insert before the current line
  5888. (open-line (if blank 2 1)))
  5889. ((and (bolp)
  5890. (not org-insert-heading-respect-content)
  5891. (or (bobp)
  5892. (save-excursion
  5893. (backward-char 1) (not (org-invisible-p)))))
  5894. ;; insert right here
  5895. nil)
  5896. (t
  5897. ;; somewhere in the line
  5898. (save-excursion
  5899. (setq previous-pos (point-at-bol))
  5900. (end-of-line)
  5901. (setq hide-previous (org-invisible-p)))
  5902. (and org-insert-heading-respect-content (org-show-subtree))
  5903. (let ((split
  5904. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5905. (save-excursion
  5906. (let ((p (point)))
  5907. (goto-char (point-at-bol))
  5908. (and (looking-at org-complex-heading-regexp)
  5909. (> p (match-beginning 4)))))))
  5910. tags pos)
  5911. (cond
  5912. (org-insert-heading-respect-content
  5913. (org-end-of-subtree nil t)
  5914. (or (bolp) (newline))
  5915. (or (org-previous-line-empty-p)
  5916. (and blank (newline)))
  5917. (open-line 1))
  5918. ((org-on-heading-p)
  5919. (when hide-previous
  5920. (show-children)
  5921. (org-show-entry))
  5922. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5923. (setq tags (and (match-end 2) (match-string 2)))
  5924. (and (match-end 1)
  5925. (delete-region (match-beginning 1) (match-end 1)))
  5926. (setq pos (point-at-bol))
  5927. (or split (end-of-line 1))
  5928. (delete-horizontal-space)
  5929. (if (string-match "\\`\\*+\\'"
  5930. (buffer-substring (point-at-bol) (point)))
  5931. (insert " "))
  5932. (newline (if blank 2 1))
  5933. (when tags
  5934. (save-excursion
  5935. (goto-char pos)
  5936. (end-of-line 1)
  5937. (insert " " tags)
  5938. (org-set-tags nil 'align))))
  5939. (t
  5940. (or split (end-of-line 1))
  5941. (newline (if blank 2 1)))))))
  5942. (insert head) (just-one-space)
  5943. (setq pos (point))
  5944. (end-of-line 1)
  5945. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5946. (when (and org-insert-heading-respect-content hide-previous)
  5947. (save-excursion
  5948. (goto-char previous-pos)
  5949. (hide-subtree)))
  5950. (run-hooks 'org-insert-heading-hook)))))
  5951. (defun org-get-heading (&optional no-tags)
  5952. "Return the heading of the current entry, without the stars."
  5953. (save-excursion
  5954. (org-back-to-heading t)
  5955. (if (looking-at
  5956. (if no-tags
  5957. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5958. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5959. (match-string 1) "")))
  5960. (defun org-heading-components ()
  5961. "Return the components of the current heading.
  5962. This is a list with the following elements:
  5963. - the level as an integer
  5964. - the reduced level, different if `org-odd-levels-only' is set.
  5965. - the TODO keyword, or nil
  5966. - the priority character, like ?A, or nil if no priority is given
  5967. - the headline text itself, or the tags string if no headline text
  5968. - the tags string, or nil."
  5969. (save-excursion
  5970. (org-back-to-heading t)
  5971. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5972. (list (length (match-string 1))
  5973. (org-reduced-level (length (match-string 1)))
  5974. (org-match-string-no-properties 2)
  5975. (and (match-end 3) (aref (match-string 3) 2))
  5976. (org-match-string-no-properties 4)
  5977. (org-match-string-no-properties 5)))))
  5978. (defun org-get-entry ()
  5979. "Get the entry text, after heading, entire subtree."
  5980. (save-excursion
  5981. (org-back-to-heading t)
  5982. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5983. (defun org-insert-heading-after-current ()
  5984. "Insert a new heading with same level as current, after current subtree."
  5985. (interactive)
  5986. (org-back-to-heading)
  5987. (org-insert-heading)
  5988. (org-move-subtree-down)
  5989. (end-of-line 1))
  5990. (defun org-insert-heading-respect-content ()
  5991. (interactive)
  5992. (let ((org-insert-heading-respect-content t))
  5993. (org-insert-heading t)))
  5994. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5995. (interactive "P")
  5996. (let ((org-insert-heading-respect-content t))
  5997. (org-insert-todo-heading force-state t)))
  5998. (defun org-insert-todo-heading (arg &optional force-heading)
  5999. "Insert a new heading with the same level and TODO state as current heading.
  6000. If the heading has no TODO state, or if the state is DONE, use the first
  6001. state (TODO by default). Also with prefix arg, force first state."
  6002. (interactive "P")
  6003. (when (or force-heading (not (org-insert-item 'checkbox)))
  6004. (org-insert-heading force-heading)
  6005. (save-excursion
  6006. (org-back-to-heading)
  6007. (outline-previous-heading)
  6008. (looking-at org-todo-line-regexp))
  6009. (let*
  6010. ((new-mark-x
  6011. (if (or arg
  6012. (not (match-beginning 2))
  6013. (member (match-string 2) org-done-keywords))
  6014. (car org-todo-keywords-1)
  6015. (match-string 2)))
  6016. (new-mark
  6017. (or
  6018. (run-hook-with-args-until-success
  6019. 'org-todo-get-default-hook new-mark-x nil)
  6020. new-mark-x)))
  6021. (beginning-of-line 1)
  6022. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6023. (if org-treat-insert-todo-heading-as-state-change
  6024. (org-todo new-mark)
  6025. (insert new-mark " "))))
  6026. (when org-provide-todo-statistics
  6027. (org-update-parent-todo-statistics))))
  6028. (defun org-insert-subheading (arg)
  6029. "Insert a new subheading and demote it.
  6030. Works for outline headings and for plain lists alike."
  6031. (interactive "P")
  6032. (org-insert-heading arg)
  6033. (cond
  6034. ((org-on-heading-p) (org-do-demote))
  6035. ((org-at-item-p) (org-indent-item 1))))
  6036. (defun org-insert-todo-subheading (arg)
  6037. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6038. Works for outline headings and for plain lists alike."
  6039. (interactive "P")
  6040. (org-insert-todo-heading arg)
  6041. (cond
  6042. ((org-on-heading-p) (org-do-demote))
  6043. ((org-at-item-p) (org-indent-item 1))))
  6044. ;;; Promotion and Demotion
  6045. (defvar org-after-demote-entry-hook nil
  6046. "Hook run after an entry has been demoted.
  6047. The cursor will be at the beginning of the entry.
  6048. When a subtree is being demoted, the hook will be called for each node.")
  6049. (defvar org-after-promote-entry-hook nil
  6050. "Hook run after an entry has been promoted.
  6051. The cursor will be at the beginning of the entry.
  6052. When a subtree is being promoted, the hook will be called for each node.")
  6053. (defun org-promote-subtree ()
  6054. "Promote the entire subtree.
  6055. See also `org-promote'."
  6056. (interactive)
  6057. (save-excursion
  6058. (org-map-tree 'org-promote))
  6059. (org-fix-position-after-promote))
  6060. (defun org-demote-subtree ()
  6061. "Demote the entire subtree. See `org-demote'.
  6062. See also `org-promote'."
  6063. (interactive)
  6064. (save-excursion
  6065. (org-map-tree 'org-demote))
  6066. (org-fix-position-after-promote))
  6067. (defun org-do-promote ()
  6068. "Promote the current heading higher up the tree.
  6069. If the region is active in `transient-mark-mode', promote all headings
  6070. in the region."
  6071. (interactive)
  6072. (save-excursion
  6073. (if (org-region-active-p)
  6074. (org-map-region 'org-promote (region-beginning) (region-end))
  6075. (org-promote)))
  6076. (org-fix-position-after-promote))
  6077. (defun org-do-demote ()
  6078. "Demote the current heading lower down the tree.
  6079. If the region is active in `transient-mark-mode', demote all headings
  6080. in the region."
  6081. (interactive)
  6082. (save-excursion
  6083. (if (org-region-active-p)
  6084. (org-map-region 'org-demote (region-beginning) (region-end))
  6085. (org-demote)))
  6086. (org-fix-position-after-promote))
  6087. (defun org-fix-position-after-promote ()
  6088. "Make sure that after pro/demotion cursor position is right."
  6089. (let ((pos (point)))
  6090. (when (save-excursion
  6091. (beginning-of-line 1)
  6092. (looking-at org-todo-line-regexp)
  6093. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6094. (cond ((eobp) (insert " "))
  6095. ((eolp) (insert " "))
  6096. ((equal (char-after) ?\ ) (forward-char 1))))))
  6097. (defun org-current-level ()
  6098. "Return the level of the current entry, or nil if before the first headline.
  6099. The level is the number of stars at the beginning of the headline."
  6100. (save-excursion
  6101. (condition-case nil
  6102. (progn
  6103. (org-back-to-heading t)
  6104. (funcall outline-level))
  6105. (error nil))))
  6106. (defun org-get-previous-line-level ()
  6107. "Return the outline depth of the last headline before the current line.
  6108. Returns 0 for the first headline in the buffer, and nil if before the
  6109. first headline."
  6110. (let ((current-level (org-current-level))
  6111. (prev-level (when (> (line-number-at-pos) 1)
  6112. (save-excursion
  6113. (beginning-of-line 0)
  6114. (org-current-level)))))
  6115. (cond ((null current-level) nil) ; Before first headline
  6116. ((null prev-level) 0) ; At first headline
  6117. (prev-level))))
  6118. (defun org-reduced-level (l)
  6119. "Compute the effective level of a heading.
  6120. This takes into account the setting of `org-odd-levels-only'."
  6121. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6122. (defun org-level-increment ()
  6123. "Return the number of stars that will be added or removed at a
  6124. time to headlines when structure editing, based on the value of
  6125. `org-odd-levels-only'."
  6126. (if org-odd-levels-only 2 1))
  6127. (defun org-get-valid-level (level &optional change)
  6128. "Rectify a level change under the influence of `org-odd-levels-only'
  6129. LEVEL is a current level, CHANGE is by how much the level should be
  6130. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6131. even level numbers will become the next higher odd number."
  6132. (if org-odd-levels-only
  6133. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6134. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6135. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6136. (max 1 (+ level (or change 0)))))
  6137. (if (boundp 'define-obsolete-function-alias)
  6138. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6139. (define-obsolete-function-alias 'org-get-legal-level
  6140. 'org-get-valid-level)
  6141. (define-obsolete-function-alias 'org-get-legal-level
  6142. 'org-get-valid-level "23.1")))
  6143. (defun org-promote ()
  6144. "Promote the current heading higher up the tree.
  6145. If the region is active in `transient-mark-mode', promote all headings
  6146. in the region."
  6147. (org-back-to-heading t)
  6148. (let* ((level (save-match-data (funcall outline-level)))
  6149. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6150. (diff (abs (- level (length up-head) -1))))
  6151. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6152. (replace-match up-head nil t)
  6153. ;; Fixup tag positioning
  6154. (and org-auto-align-tags (org-set-tags nil t))
  6155. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6156. (run-hooks 'org-after-promote-entry-hook)))
  6157. (defun org-demote ()
  6158. "Demote the current heading lower down the tree.
  6159. If the region is active in `transient-mark-mode', demote all headings
  6160. in the region."
  6161. (org-back-to-heading t)
  6162. (let* ((level (save-match-data (funcall outline-level)))
  6163. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6164. (diff (abs (- level (length down-head) -1))))
  6165. (replace-match down-head nil t)
  6166. ;; Fixup tag positioning
  6167. (and org-auto-align-tags (org-set-tags nil t))
  6168. (if org-adapt-indentation (org-fixup-indentation diff))
  6169. (run-hooks 'org-after-demote-entry-hook)))
  6170. (defun org-cycle-level ()
  6171. "Cycle the level of an empty headline through possible states.
  6172. This goes first to child, then to parent, level, then up the hierarchy.
  6173. After top level, it switches back to sibling level."
  6174. (interactive)
  6175. (let ((org-adapt-indentation nil))
  6176. (when (org-point-at-end-of-empty-headline)
  6177. (setq this-command 'org-cycle-level) ; Only needed for caching
  6178. (let ((cur-level (org-current-level))
  6179. (prev-level (org-get-previous-line-level)))
  6180. (cond
  6181. ;; If first headline in file, promote to top-level.
  6182. ((= prev-level 0)
  6183. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6184. do (org-do-promote)))
  6185. ;; If same level as prev, demote one.
  6186. ((= prev-level cur-level)
  6187. (org-do-demote))
  6188. ;; If parent is top-level, promote to top level if not already.
  6189. ((= prev-level 1)
  6190. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6191. do (org-do-promote)))
  6192. ;; If top-level, return to prev-level.
  6193. ((= cur-level 1)
  6194. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6195. do (org-do-demote)))
  6196. ;; If less than prev-level, promote one.
  6197. ((< cur-level prev-level)
  6198. (org-do-promote))
  6199. ;; If deeper than prev-level, promote until higher than
  6200. ;; prev-level.
  6201. ((> cur-level prev-level)
  6202. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6203. do (org-do-promote))))
  6204. t))))
  6205. (defun org-map-tree (fun)
  6206. "Call FUN for every heading underneath the current one."
  6207. (org-back-to-heading)
  6208. (let ((level (funcall outline-level)))
  6209. (save-excursion
  6210. (funcall fun)
  6211. (while (and (progn
  6212. (outline-next-heading)
  6213. (> (funcall outline-level) level))
  6214. (not (eobp)))
  6215. (funcall fun)))))
  6216. (defun org-map-region (fun beg end)
  6217. "Call FUN for every heading between BEG and END."
  6218. (let ((org-ignore-region t))
  6219. (save-excursion
  6220. (setq end (copy-marker end))
  6221. (goto-char beg)
  6222. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6223. (< (point) end))
  6224. (funcall fun))
  6225. (while (and (progn
  6226. (outline-next-heading)
  6227. (< (point) end))
  6228. (not (eobp)))
  6229. (funcall fun)))))
  6230. (defun org-fixup-indentation (diff)
  6231. "Change the indentation in the current entry by DIFF
  6232. However, if any line in the current entry has no indentation, or if it
  6233. would end up with no indentation after the change, nothing at all is done."
  6234. (save-excursion
  6235. (let ((end (save-excursion (outline-next-heading)
  6236. (point-marker)))
  6237. (prohibit (if (> diff 0)
  6238. "^\\S-"
  6239. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6240. col)
  6241. (unless (save-excursion (end-of-line 1)
  6242. (re-search-forward prohibit end t))
  6243. (while (and (< (point) end)
  6244. (re-search-forward "^[ \t]+" end t))
  6245. (goto-char (match-end 0))
  6246. (setq col (current-column))
  6247. (if (< diff 0) (replace-match ""))
  6248. (org-indent-to-column (+ diff col))))
  6249. (move-marker end nil))))
  6250. (defun org-convert-to-odd-levels ()
  6251. "Convert an org-mode file with all levels allowed to one with odd levels.
  6252. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6253. level 5 etc."
  6254. (interactive)
  6255. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6256. (let ((outline-regexp org-outline-regexp)
  6257. (outline-level 'org-outline-level)
  6258. (org-odd-levels-only nil) n)
  6259. (save-excursion
  6260. (goto-char (point-min))
  6261. (while (re-search-forward "^\\*\\*+ " nil t)
  6262. (setq n (- (length (match-string 0)) 2))
  6263. (while (>= (setq n (1- n)) 0)
  6264. (org-demote))
  6265. (end-of-line 1))))))
  6266. (defun org-convert-to-oddeven-levels ()
  6267. "Convert an org-mode file with only odd levels to one with odd and even levels.
  6268. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  6269. section with an even level, conversion would destroy the structure of the file. An error
  6270. is signaled in this case."
  6271. (interactive)
  6272. (goto-char (point-min))
  6273. ;; First check if there are no even levels
  6274. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6275. (org-show-context t)
  6276. (error "Not all levels are odd in this file. Conversion not possible"))
  6277. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6278. (let ((outline-regexp org-outline-regexp)
  6279. (outline-level 'org-outline-level)
  6280. (org-odd-levels-only nil) n)
  6281. (save-excursion
  6282. (goto-char (point-min))
  6283. (while (re-search-forward "^\\*\\*+ " nil t)
  6284. (setq n (/ (1- (length (match-string 0))) 2))
  6285. (while (>= (setq n (1- n)) 0)
  6286. (org-promote))
  6287. (end-of-line 1))))))
  6288. (defun org-tr-level (n)
  6289. "Make N odd if required."
  6290. (if org-odd-levels-only (1+ (/ n 2)) n))
  6291. ;;; Vertical tree motion, cutting and pasting of subtrees
  6292. (defun org-move-subtree-up (&optional arg)
  6293. "Move the current subtree up past ARG headlines of the same level."
  6294. (interactive "p")
  6295. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6296. (defun org-move-subtree-down (&optional arg)
  6297. "Move the current subtree down past ARG headlines of the same level."
  6298. (interactive "p")
  6299. (setq arg (prefix-numeric-value arg))
  6300. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6301. 'org-get-last-sibling))
  6302. (ins-point (make-marker))
  6303. (cnt (abs arg))
  6304. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6305. ;; Select the tree
  6306. (org-back-to-heading)
  6307. (setq beg0 (point))
  6308. (save-excursion
  6309. (setq ne-beg (org-back-over-empty-lines))
  6310. (setq beg (point)))
  6311. (save-match-data
  6312. (save-excursion (outline-end-of-heading)
  6313. (setq folded (org-invisible-p)))
  6314. (outline-end-of-subtree))
  6315. (outline-next-heading)
  6316. (setq ne-end (org-back-over-empty-lines))
  6317. (setq end (point))
  6318. (goto-char beg0)
  6319. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6320. ;; include less whitespace
  6321. (save-excursion
  6322. (goto-char beg)
  6323. (forward-line (- ne-beg ne-end))
  6324. (setq beg (point))))
  6325. ;; Find insertion point, with error handling
  6326. (while (> cnt 0)
  6327. (or (and (funcall movfunc) (looking-at outline-regexp))
  6328. (progn (goto-char beg0)
  6329. (error "Cannot move past superior level or buffer limit")))
  6330. (setq cnt (1- cnt)))
  6331. (if (> arg 0)
  6332. ;; Moving forward - still need to move over subtree
  6333. (progn (org-end-of-subtree t t)
  6334. (save-excursion
  6335. (org-back-over-empty-lines)
  6336. (or (bolp) (newline)))))
  6337. (setq ne-ins (org-back-over-empty-lines))
  6338. (move-marker ins-point (point))
  6339. (setq txt (buffer-substring beg end))
  6340. (org-save-markers-in-region beg end)
  6341. (delete-region beg end)
  6342. (org-remove-empty-overlays-at beg)
  6343. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6344. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6345. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6346. (let ((bbb (point)))
  6347. (insert-before-markers txt)
  6348. (org-reinstall-markers-in-region bbb)
  6349. (move-marker ins-point bbb))
  6350. (or (bolp) (insert "\n"))
  6351. (setq ins-end (point))
  6352. (goto-char ins-point)
  6353. (org-skip-whitespace)
  6354. (when (and (< arg 0)
  6355. (org-first-sibling-p)
  6356. (> ne-ins ne-beg))
  6357. ;; Move whitespace back to beginning
  6358. (save-excursion
  6359. (goto-char ins-end)
  6360. (let ((kill-whole-line t))
  6361. (kill-line (- ne-ins ne-beg)) (point)))
  6362. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6363. (move-marker ins-point nil)
  6364. (if folded
  6365. (hide-subtree)
  6366. (org-show-entry)
  6367. (show-children)
  6368. (org-cycle-hide-drawers 'children))
  6369. (org-clean-visibility-after-subtree-move)))
  6370. (defvar org-subtree-clip ""
  6371. "Clipboard for cut and paste of subtrees.
  6372. This is actually only a copy of the kill, because we use the normal kill
  6373. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6374. (defvar org-subtree-clip-folded nil
  6375. "Was the last copied subtree folded?
  6376. This is used to fold the tree back after pasting.")
  6377. (defun org-cut-subtree (&optional n)
  6378. "Cut the current subtree into the clipboard.
  6379. With prefix arg N, cut this many sequential subtrees.
  6380. This is a short-hand for marking the subtree and then cutting it."
  6381. (interactive "p")
  6382. (org-copy-subtree n 'cut))
  6383. (defun org-copy-subtree (&optional n cut force-store-markers)
  6384. "Cut the current subtree into the clipboard.
  6385. With prefix arg N, cut this many sequential subtrees.
  6386. This is a short-hand for marking the subtree and then copying it.
  6387. If CUT is non-nil, actually cut the subtree.
  6388. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6389. of some markers in the region, even if CUT is non-nil. This is
  6390. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6391. (interactive "p")
  6392. (let (beg end folded (beg0 (point)))
  6393. (if (interactive-p)
  6394. (org-back-to-heading nil) ; take what looks like a subtree
  6395. (org-back-to-heading t)) ; take what is really there
  6396. (org-back-over-empty-lines)
  6397. (setq beg (point))
  6398. (skip-chars-forward " \t\r\n")
  6399. (save-match-data
  6400. (save-excursion (outline-end-of-heading)
  6401. (setq folded (org-invisible-p)))
  6402. (condition-case nil
  6403. (org-forward-same-level (1- n) t)
  6404. (error nil))
  6405. (org-end-of-subtree t t))
  6406. (org-back-over-empty-lines)
  6407. (setq end (point))
  6408. (goto-char beg0)
  6409. (when (> end beg)
  6410. (setq org-subtree-clip-folded folded)
  6411. (when (or cut force-store-markers)
  6412. (org-save-markers-in-region beg end))
  6413. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6414. (setq org-subtree-clip (current-kill 0))
  6415. (message "%s: Subtree(s) with %d characters"
  6416. (if cut "Cut" "Copied")
  6417. (length org-subtree-clip)))))
  6418. (defun org-paste-subtree (&optional level tree for-yank)
  6419. "Paste the clipboard as a subtree, with modification of headline level.
  6420. The entire subtree is promoted or demoted in order to match a new headline
  6421. level.
  6422. If the cursor is at the beginning of a headline, the same level as
  6423. that headline is used to paste the tree
  6424. If not, the new level is derived from the *visible* headings
  6425. before and after the insertion point, and taken to be the inferior headline
  6426. level of the two. So if the previous visible heading is level 3 and the
  6427. next is level 4 (or vice versa), level 4 will be used for insertion.
  6428. This makes sure that the subtree remains an independent subtree and does
  6429. not swallow low level entries.
  6430. You can also force a different level, either by using a numeric prefix
  6431. argument, or by inserting the heading marker by hand. For example, if the
  6432. cursor is after \"*****\", then the tree will be shifted to level 5.
  6433. If optional TREE is given, use this text instead of the kill ring.
  6434. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6435. move back over whitespace before inserting, and move point to the end of
  6436. the inserted text when done."
  6437. (interactive "P")
  6438. (setq tree (or tree (and kill-ring (current-kill 0))))
  6439. (unless (org-kill-is-subtree-p tree)
  6440. (error "%s"
  6441. (substitute-command-keys
  6442. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6443. (let* ((visp (not (org-invisible-p)))
  6444. (txt tree)
  6445. (^re (concat "^\\(" outline-regexp "\\)"))
  6446. (re (concat "\\(" outline-regexp "\\)"))
  6447. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6448. (old-level (if (string-match ^re txt)
  6449. (- (match-end 0) (match-beginning 0) 1)
  6450. -1))
  6451. (force-level (cond (level (prefix-numeric-value level))
  6452. ((and (looking-at "[ \t]*$")
  6453. (string-match
  6454. ^re_ (buffer-substring
  6455. (point-at-bol) (point))))
  6456. (- (match-end 1) (match-beginning 1)))
  6457. ((and (bolp)
  6458. (looking-at org-outline-regexp))
  6459. (- (match-end 0) (point) 1))
  6460. (t nil)))
  6461. (previous-level (save-excursion
  6462. (condition-case nil
  6463. (progn
  6464. (outline-previous-visible-heading 1)
  6465. (if (looking-at re)
  6466. (- (match-end 0) (match-beginning 0) 1)
  6467. 1))
  6468. (error 1))))
  6469. (next-level (save-excursion
  6470. (condition-case nil
  6471. (progn
  6472. (or (looking-at outline-regexp)
  6473. (outline-next-visible-heading 1))
  6474. (if (looking-at re)
  6475. (- (match-end 0) (match-beginning 0) 1)
  6476. 1))
  6477. (error 1))))
  6478. (new-level (or force-level (max previous-level next-level)))
  6479. (shift (if (or (= old-level -1)
  6480. (= new-level -1)
  6481. (= old-level new-level))
  6482. 0
  6483. (- new-level old-level)))
  6484. (delta (if (> shift 0) -1 1))
  6485. (func (if (> shift 0) 'org-demote 'org-promote))
  6486. (org-odd-levels-only nil)
  6487. beg end newend)
  6488. ;; Remove the forced level indicator
  6489. (if force-level
  6490. (delete-region (point-at-bol) (point)))
  6491. ;; Paste
  6492. (beginning-of-line 1)
  6493. (unless for-yank (org-back-over-empty-lines))
  6494. (setq beg (point))
  6495. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6496. (insert-before-markers txt)
  6497. (unless (string-match "\n\\'" txt) (insert "\n"))
  6498. (setq newend (point))
  6499. (org-reinstall-markers-in-region beg)
  6500. (setq end (point))
  6501. (goto-char beg)
  6502. (skip-chars-forward " \t\n\r")
  6503. (setq beg (point))
  6504. (if (and (org-invisible-p) visp)
  6505. (save-excursion (outline-show-heading)))
  6506. ;; Shift if necessary
  6507. (unless (= shift 0)
  6508. (save-restriction
  6509. (narrow-to-region beg end)
  6510. (while (not (= shift 0))
  6511. (org-map-region func (point-min) (point-max))
  6512. (setq shift (+ delta shift)))
  6513. (goto-char (point-min))
  6514. (setq newend (point-max))))
  6515. (when (or (interactive-p) for-yank)
  6516. (message "Clipboard pasted as level %d subtree" new-level))
  6517. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6518. kill-ring
  6519. (eq org-subtree-clip (current-kill 0))
  6520. org-subtree-clip-folded)
  6521. ;; The tree was folded before it was killed/copied
  6522. (hide-subtree))
  6523. (and for-yank (goto-char newend))))
  6524. (defun org-kill-is-subtree-p (&optional txt)
  6525. "Check if the current kill is an outline subtree, or a set of trees.
  6526. Returns nil if kill does not start with a headline, or if the first
  6527. headline level is not the largest headline level in the tree.
  6528. So this will actually accept several entries of equal levels as well,
  6529. which is OK for `org-paste-subtree'.
  6530. If optional TXT is given, check this string instead of the current kill."
  6531. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6532. (start-level (and kill
  6533. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6534. org-outline-regexp "\\)")
  6535. kill)
  6536. (- (match-end 2) (match-beginning 2) 1)))
  6537. (re (concat "^" org-outline-regexp))
  6538. (start (1+ (or (match-beginning 2) -1))))
  6539. (if (not start-level)
  6540. (progn
  6541. nil) ;; does not even start with a heading
  6542. (catch 'exit
  6543. (while (setq start (string-match re kill (1+ start)))
  6544. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6545. (throw 'exit nil)))
  6546. t))))
  6547. (defvar org-markers-to-move nil
  6548. "Markers that should be moved with a cut-and-paste operation.
  6549. Those markers are stored together with their positions relative to
  6550. the start of the region.")
  6551. (defun org-save-markers-in-region (beg end)
  6552. "Check markers in region.
  6553. If these markers are between BEG and END, record their position relative
  6554. to BEG, so that after moving the block of text, we can put the markers back
  6555. into place.
  6556. This function gets called just before an entry or tree gets cut from the
  6557. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6558. called immediately, to move the markers with the entries."
  6559. (setq org-markers-to-move nil)
  6560. (when (featurep 'org-clock)
  6561. (org-clock-save-markers-for-cut-and-paste beg end))
  6562. (when (featurep 'org-agenda)
  6563. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6564. (defun org-check-and-save-marker (marker beg end)
  6565. "Check if MARKER is between BEG and END.
  6566. If yes, remember the marker and the distance to BEG."
  6567. (when (and (marker-buffer marker)
  6568. (equal (marker-buffer marker) (current-buffer)))
  6569. (if (and (>= marker beg) (< marker end))
  6570. (push (cons marker (- marker beg)) org-markers-to-move))))
  6571. (defun org-reinstall-markers-in-region (beg)
  6572. "Move all remembered markers to their position relative to BEG."
  6573. (mapc (lambda (x)
  6574. (move-marker (car x) (+ beg (cdr x))))
  6575. org-markers-to-move)
  6576. (setq org-markers-to-move nil))
  6577. (defun org-narrow-to-subtree ()
  6578. "Narrow buffer to the current subtree."
  6579. (interactive)
  6580. (save-excursion
  6581. (save-match-data
  6582. (narrow-to-region
  6583. (progn (org-back-to-heading t) (point))
  6584. (progn (org-end-of-subtree t t)
  6585. (if (org-on-heading-p) (backward-char 1))
  6586. (point))))))
  6587. (eval-when-compile
  6588. (defvar org-property-drawer-re))
  6589. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6590. "Clone the task (subtree) at point N times.
  6591. The clones will be inserted as siblings.
  6592. In interactive use, the user will be prompted for the number of
  6593. clones to be produced, and for a time SHIFT, which may be a
  6594. repeater as used in time stamps, for example `+3d'.
  6595. When a valid repeater is given and the entry contains any time
  6596. stamps, the clones will become a sequence in time, with time
  6597. stamps in the subtree shifted for each clone produced. If SHIFT
  6598. is nil or the empty string, time stamps will be left alone. The
  6599. ID property of the original subtree is removed.
  6600. If the original subtree did contain time stamps with a repeater,
  6601. the following will happen:
  6602. - the repeater will be removed in each clone
  6603. - an additional clone will be produced, with the current, unshifted
  6604. date(s) in the entry.
  6605. - the original entry will be placed *after* all the clones, with
  6606. repeater intact.
  6607. - the start days in the repeater in the original entry will be shifted
  6608. to past the last clone.
  6609. I this way you can spell out a number of instances of a repeating task,
  6610. and still retain the repeater to cover future instances of the task."
  6611. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6612. (let (beg end template task idprop
  6613. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6614. (if (not (and (integerp n) (> n 0)))
  6615. (error "Invalid number of replications %s" n))
  6616. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6617. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6618. shift)))
  6619. (error "Invalid shift specification %s" shift))
  6620. (when doshift
  6621. (setq shift-n (string-to-number (match-string 1 shift))
  6622. shift-what (cdr (assoc (match-string 2 shift)
  6623. '(("d" . day) ("w" . week)
  6624. ("m" . month) ("y" . year))))))
  6625. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6626. (setq nmin 1 nmax n)
  6627. (org-back-to-heading t)
  6628. (setq beg (point))
  6629. (setq idprop (org-entry-get nil "ID"))
  6630. (org-end-of-subtree t t)
  6631. (or (bolp) (insert "\n"))
  6632. (setq end (point))
  6633. (setq template (buffer-substring beg end))
  6634. (when (and doshift
  6635. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6636. (delete-region beg end)
  6637. (setq end beg)
  6638. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6639. (goto-char end)
  6640. (loop for n from nmin to nmax do
  6641. ;; prepare clone
  6642. (with-temp-buffer
  6643. (insert template)
  6644. (org-mode)
  6645. (goto-char (point-min))
  6646. (and idprop (if org-clone-delete-id
  6647. (org-entry-delete nil "ID")
  6648. (org-id-get-create t)))
  6649. (while (re-search-forward org-property-drawer-re nil t)
  6650. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6651. (goto-char (point-min))
  6652. (when doshift
  6653. (while (re-search-forward org-ts-regexp-both nil t)
  6654. (org-timestamp-change (* n shift-n) shift-what))
  6655. (unless (= n n-no-remove)
  6656. (goto-char (point-min))
  6657. (while (re-search-forward org-ts-regexp nil t)
  6658. (save-excursion
  6659. (goto-char (match-beginning 0))
  6660. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6661. (delete-region (match-beginning 1) (match-end 1)))))))
  6662. (setq task (buffer-string)))
  6663. (insert task))
  6664. (goto-char beg)))
  6665. ;;; Outline Sorting
  6666. (defun org-sort (with-case)
  6667. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6668. Optional argument WITH-CASE means sort case-sensitively.
  6669. With a double prefix argument, also remove duplicate entries."
  6670. (interactive "P")
  6671. (if (org-at-table-p)
  6672. (org-call-with-arg 'org-table-sort-lines with-case)
  6673. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6674. (defun org-sort-remove-invisible (s)
  6675. (remove-text-properties 0 (length s) org-rm-props s)
  6676. (while (string-match org-bracket-link-regexp s)
  6677. (setq s (replace-match (if (match-end 2)
  6678. (match-string 3 s)
  6679. (match-string 1 s)) t t s)))
  6680. s)
  6681. (defvar org-priority-regexp) ; defined later in the file
  6682. (defvar org-after-sorting-entries-or-items-hook nil
  6683. "Hook that is run after a bunch of entries or items have been sorted.
  6684. When children are sorted, the cursor is in the parent line when this
  6685. hook gets called. When a region or a plain list is sorted, the cursor
  6686. will be in the first entry of the sorted region/list.")
  6687. (defun org-sort-entries-or-items
  6688. (&optional with-case sorting-type getkey-func compare-func property)
  6689. "Sort entries on a certain level of an outline tree, or plain list items.
  6690. If there is an active region, the entries in the region are sorted.
  6691. Else, if the cursor is before the first entry, sort the top-level items.
  6692. Else, the children of the entry at point are sorted.
  6693. If the cursor is at the first item in a plain list, the list items will be
  6694. sorted.
  6695. Sorting can be alphabetically, numerically, by date/time as given by
  6696. a time stamp, by a property or by priority.
  6697. The command prompts for the sorting type unless it has been given to the
  6698. function through the SORTING-TYPE argument, which needs to be a character,
  6699. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6700. precise meaning of each character:
  6701. n Numerically, by converting the beginning of the entry/item to a number.
  6702. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6703. t By date/time, either the first active time stamp in the entry, or, if
  6704. none exist, by the first inactive one.
  6705. In items, only the first line will be checked.
  6706. s By the scheduled date/time.
  6707. d By deadline date/time.
  6708. c By creation time, which is assumed to be the first inactive time stamp
  6709. at the beginning of a line.
  6710. p By priority according to the cookie.
  6711. r By the value of a property.
  6712. Capital letters will reverse the sort order.
  6713. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6714. called with point at the beginning of the record. It must return either
  6715. a string or a number that should serve as the sorting key for that record.
  6716. Comparing entries ignores case by default. However, with an optional argument
  6717. WITH-CASE, the sorting considers case as well."
  6718. (interactive "P")
  6719. (let ((case-func (if with-case 'identity 'downcase))
  6720. start beg end stars re re2
  6721. txt what tmp plain-list-p)
  6722. ;; Find beginning and end of region to sort
  6723. (cond
  6724. ((org-region-active-p)
  6725. ;; we will sort the region
  6726. (setq end (region-end)
  6727. what "region")
  6728. (goto-char (region-beginning))
  6729. (if (not (org-on-heading-p)) (outline-next-heading))
  6730. (setq start (point)))
  6731. ((org-at-item-p)
  6732. ;; we will sort this plain list
  6733. (org-beginning-of-item-list) (setq start (point))
  6734. (org-end-of-item-list)
  6735. (or (bolp) (insert "\n"))
  6736. (setq end (point))
  6737. (goto-char start)
  6738. (setq plain-list-p t
  6739. what "plain list"))
  6740. ((or (org-on-heading-p)
  6741. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6742. ;; we will sort the children of the current headline
  6743. (org-back-to-heading)
  6744. (setq start (point)
  6745. end (progn (org-end-of-subtree t t)
  6746. (or (bolp) (insert "\n"))
  6747. (org-back-over-empty-lines)
  6748. (point))
  6749. what "children")
  6750. (goto-char start)
  6751. (show-subtree)
  6752. (outline-next-heading))
  6753. (t
  6754. ;; we will sort the top-level entries in this file
  6755. (goto-char (point-min))
  6756. (or (org-on-heading-p) (outline-next-heading))
  6757. (setq start (point))
  6758. (goto-char (point-max))
  6759. (beginning-of-line 1)
  6760. (when (looking-at ".*?\\S-")
  6761. ;; File ends in a non-white line
  6762. (end-of-line 1)
  6763. (insert "\n"))
  6764. (setq end (point-max))
  6765. (setq what "top-level")
  6766. (goto-char start)
  6767. (show-all)))
  6768. (setq beg (point))
  6769. (if (>= beg end) (error "Nothing to sort"))
  6770. (unless plain-list-p
  6771. (looking-at "\\(\\*+\\)")
  6772. (setq stars (match-string 1)
  6773. re (concat "^" (regexp-quote stars) " +")
  6774. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6775. txt (buffer-substring beg end))
  6776. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6777. (if (and (not (equal stars "*")) (string-match re2 txt))
  6778. (error "Region to sort contains a level above the first entry")))
  6779. (unless sorting-type
  6780. (message
  6781. (if plain-list-p
  6782. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6783. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6784. [t]ime [s]cheduled [d]eadline [c]reated
  6785. A/N/T/S/D/C/P/O/F means reversed:")
  6786. what)
  6787. (setq sorting-type (read-char-exclusive))
  6788. (and (= (downcase sorting-type) ?f)
  6789. (setq getkey-func
  6790. (org-icompleting-read "Sort using function: "
  6791. obarray 'fboundp t nil nil))
  6792. (setq getkey-func (intern getkey-func)))
  6793. (and (= (downcase sorting-type) ?r)
  6794. (setq property
  6795. (org-icompleting-read "Property: "
  6796. (mapcar 'list (org-buffer-property-keys t))
  6797. nil t))))
  6798. (message "Sorting entries...")
  6799. (save-restriction
  6800. (narrow-to-region start end)
  6801. (let ((dcst (downcase sorting-type))
  6802. (case-fold-search nil)
  6803. (now (current-time)))
  6804. (sort-subr
  6805. (/= dcst sorting-type)
  6806. ;; This function moves to the beginning character of the "record" to
  6807. ;; be sorted.
  6808. (if plain-list-p
  6809. (lambda nil
  6810. (if (org-at-item-p) t (goto-char (point-max))))
  6811. (lambda nil
  6812. (if (re-search-forward re nil t)
  6813. (goto-char (match-beginning 0))
  6814. (goto-char (point-max)))))
  6815. ;; This function moves to the last character of the "record" being
  6816. ;; sorted.
  6817. (if plain-list-p
  6818. 'org-end-of-item
  6819. (lambda nil
  6820. (save-match-data
  6821. (condition-case nil
  6822. (outline-forward-same-level 1)
  6823. (error
  6824. (goto-char (point-max)))))))
  6825. ;; This function returns the value that gets sorted against.
  6826. (if plain-list-p
  6827. (lambda nil
  6828. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6829. (cond
  6830. ((= dcst ?n)
  6831. (string-to-number (buffer-substring (match-end 0)
  6832. (point-at-eol))))
  6833. ((= dcst ?a)
  6834. (buffer-substring (match-end 0) (point-at-eol)))
  6835. ((= dcst ?t)
  6836. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6837. (re-search-forward org-ts-regexp-both
  6838. (point-at-eol) t))
  6839. (org-time-string-to-seconds (match-string 0))
  6840. (org-float-time now)))
  6841. ((= dcst ?f)
  6842. (if getkey-func
  6843. (progn
  6844. (setq tmp (funcall getkey-func))
  6845. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6846. tmp)
  6847. (error "Invalid key function `%s'" getkey-func)))
  6848. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6849. (lambda nil
  6850. (cond
  6851. ((= dcst ?n)
  6852. (if (looking-at org-complex-heading-regexp)
  6853. (string-to-number (match-string 4))
  6854. nil))
  6855. ((= dcst ?a)
  6856. (if (looking-at org-complex-heading-regexp)
  6857. (funcall case-func (match-string 4))
  6858. nil))
  6859. ((= dcst ?t)
  6860. (let ((end (save-excursion (outline-next-heading) (point))))
  6861. (if (or (re-search-forward org-ts-regexp end t)
  6862. (re-search-forward org-ts-regexp-both end t))
  6863. (org-time-string-to-seconds (match-string 0))
  6864. (org-float-time now))))
  6865. ((= dcst ?c)
  6866. (let ((end (save-excursion (outline-next-heading) (point))))
  6867. (if (re-search-forward
  6868. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6869. end t)
  6870. (org-time-string-to-seconds (match-string 0))
  6871. (org-float-time now))))
  6872. ((= dcst ?s)
  6873. (let ((end (save-excursion (outline-next-heading) (point))))
  6874. (if (re-search-forward org-scheduled-time-regexp end t)
  6875. (org-time-string-to-seconds (match-string 1))
  6876. (org-float-time now))))
  6877. ((= dcst ?d)
  6878. (let ((end (save-excursion (outline-next-heading) (point))))
  6879. (if (re-search-forward org-deadline-time-regexp end t)
  6880. (org-time-string-to-seconds (match-string 1))
  6881. (org-float-time now))))
  6882. ((= dcst ?p)
  6883. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6884. (string-to-char (match-string 2))
  6885. org-default-priority))
  6886. ((= dcst ?r)
  6887. (or (org-entry-get nil property) ""))
  6888. ((= dcst ?o)
  6889. (if (looking-at org-complex-heading-regexp)
  6890. (- 9999 (length (member (match-string 2)
  6891. org-todo-keywords-1)))))
  6892. ((= dcst ?f)
  6893. (if getkey-func
  6894. (progn
  6895. (setq tmp (funcall getkey-func))
  6896. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6897. tmp)
  6898. (error "Invalid key function `%s'" getkey-func)))
  6899. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6900. nil
  6901. (cond
  6902. ((= dcst ?a) 'string<)
  6903. ((= dcst ?f) compare-func)
  6904. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6905. (t nil)))))
  6906. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6907. (message "Sorting entries...done")))
  6908. (defun org-do-sort (table what &optional with-case sorting-type)
  6909. "Sort TABLE of WHAT according to SORTING-TYPE.
  6910. The user will be prompted for the SORTING-TYPE if the call to this
  6911. function does not specify it. WHAT is only for the prompt, to indicate
  6912. what is being sorted. The sorting key will be extracted from
  6913. the car of the elements of the table.
  6914. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6915. (unless sorting-type
  6916. (message
  6917. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6918. what)
  6919. (setq sorting-type (read-char-exclusive)))
  6920. (let ((dcst (downcase sorting-type))
  6921. extractfun comparefun)
  6922. ;; Define the appropriate functions
  6923. (cond
  6924. ((= dcst ?n)
  6925. (setq extractfun 'string-to-number
  6926. comparefun (if (= dcst sorting-type) '< '>)))
  6927. ((= dcst ?a)
  6928. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6929. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6930. comparefun (if (= dcst sorting-type)
  6931. 'string<
  6932. (lambda (a b) (and (not (string< a b))
  6933. (not (string= a b)))))))
  6934. ((= dcst ?t)
  6935. (setq extractfun
  6936. (lambda (x)
  6937. (if (or (string-match org-ts-regexp x)
  6938. (string-match org-ts-regexp-both x))
  6939. (org-float-time
  6940. (org-time-string-to-time (match-string 0 x)))
  6941. 0))
  6942. comparefun (if (= dcst sorting-type) '< '>)))
  6943. (t (error "Invalid sorting type `%c'" sorting-type)))
  6944. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6945. table)
  6946. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6947. ;;; The orgstruct minor mode
  6948. ;; Define a minor mode which can be used in other modes in order to
  6949. ;; integrate the org-mode structure editing commands.
  6950. ;; This is really a hack, because the org-mode structure commands use
  6951. ;; keys which normally belong to the major mode. Here is how it
  6952. ;; works: The minor mode defines all the keys necessary to operate the
  6953. ;; structure commands, but wraps the commands into a function which
  6954. ;; tests if the cursor is currently at a headline or a plain list
  6955. ;; item. If that is the case, the structure command is used,
  6956. ;; temporarily setting many Org-mode variables like regular
  6957. ;; expressions for filling etc. However, when any of those keys is
  6958. ;; used at a different location, function uses `key-binding' to look
  6959. ;; up if the key has an associated command in another currently active
  6960. ;; keymap (minor modes, major mode, global), and executes that
  6961. ;; command. There might be problems if any of the keys is otherwise
  6962. ;; used as a prefix key.
  6963. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6964. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6965. ;; addresses this by checking explicitly for both bindings.
  6966. (defvar orgstruct-mode-map (make-sparse-keymap)
  6967. "Keymap for the minor `orgstruct-mode'.")
  6968. (defvar org-local-vars nil
  6969. "List of local variables, for use by `orgstruct-mode'")
  6970. ;;;###autoload
  6971. (define-minor-mode orgstruct-mode
  6972. "Toggle the minor mode `orgstruct-mode'.
  6973. This mode is for using Org-mode structure commands in other
  6974. modes. The following keys behave as if Org-mode were active, if
  6975. the cursor is on a headline, or on a plain list item (both as
  6976. defined by Org-mode).
  6977. M-up Move entry/item up
  6978. M-down Move entry/item down
  6979. M-left Promote
  6980. M-right Demote
  6981. M-S-up Move entry/item up
  6982. M-S-down Move entry/item down
  6983. M-S-left Promote subtree
  6984. M-S-right Demote subtree
  6985. M-q Fill paragraph and items like in Org-mode
  6986. C-c ^ Sort entries
  6987. C-c - Cycle list bullet
  6988. TAB Cycle item visibility
  6989. M-RET Insert new heading/item
  6990. S-M-RET Insert new TODO heading / Checkbox item
  6991. C-c C-c Set tags / toggle checkbox"
  6992. nil " OrgStruct" nil
  6993. (org-load-modules-maybe)
  6994. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6995. ;;;###autoload
  6996. (defun turn-on-orgstruct ()
  6997. "Unconditionally turn on `orgstruct-mode'."
  6998. (orgstruct-mode 1))
  6999. (defun orgstruct++-mode (&optional arg)
  7000. "Toggle `orgstruct-mode', the enhanced version of it.
  7001. In addition to setting orgstruct-mode, this also exports all indentation
  7002. and autofilling variables from org-mode into the buffer. It will also
  7003. recognize item context in multiline items.
  7004. Note that turning off orgstruct-mode will *not* remove the
  7005. indentation/paragraph settings. This can only be done by refreshing the
  7006. major mode, for example with \\[normal-mode]."
  7007. (interactive "P")
  7008. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7009. (if (< arg 1)
  7010. (orgstruct-mode -1)
  7011. (orgstruct-mode 1)
  7012. (let (var val)
  7013. (mapc
  7014. (lambda (x)
  7015. (when (string-match
  7016. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7017. (symbol-name (car x)))
  7018. (setq var (car x) val (nth 1 x))
  7019. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7020. org-local-vars)
  7021. (org-set-local 'orgstruct-is-++ t))))
  7022. (defvar orgstruct-is-++ nil
  7023. "Is orgstruct-mode in ++ version in the current-buffer?")
  7024. (make-variable-buffer-local 'orgstruct-is-++)
  7025. ;;;###autoload
  7026. (defun turn-on-orgstruct++ ()
  7027. "Unconditionally turn on `orgstruct++-mode'."
  7028. (orgstruct++-mode 1))
  7029. (defun orgstruct-error ()
  7030. "Error when there is no default binding for a structure key."
  7031. (interactive)
  7032. (error "This key has no function outside structure elements"))
  7033. (defun orgstruct-setup ()
  7034. "Setup orgstruct keymaps."
  7035. (let ((nfunc 0)
  7036. (bindings
  7037. (list
  7038. '([(meta up)] org-metaup)
  7039. '([(meta down)] org-metadown)
  7040. '([(meta left)] org-metaleft)
  7041. '([(meta right)] org-metaright)
  7042. '([(meta shift up)] org-shiftmetaup)
  7043. '([(meta shift down)] org-shiftmetadown)
  7044. '([(meta shift left)] org-shiftmetaleft)
  7045. '([(meta shift right)] org-shiftmetaright)
  7046. '([?\e (up)] org-metaup)
  7047. '([?\e (down)] org-metadown)
  7048. '([?\e (left)] org-metaleft)
  7049. '([?\e (right)] org-metaright)
  7050. '([?\e (shift up)] org-shiftmetaup)
  7051. '([?\e (shift down)] org-shiftmetadown)
  7052. '([?\e (shift left)] org-shiftmetaleft)
  7053. '([?\e (shift right)] org-shiftmetaright)
  7054. '([(shift up)] org-shiftup)
  7055. '([(shift down)] org-shiftdown)
  7056. '([(shift left)] org-shiftleft)
  7057. '([(shift right)] org-shiftright)
  7058. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7059. '("\M-q" fill-paragraph)
  7060. '("\C-c^" org-sort)
  7061. '("\C-c-" org-cycle-list-bullet)))
  7062. elt key fun cmd)
  7063. (while (setq elt (pop bindings))
  7064. (setq nfunc (1+ nfunc))
  7065. (setq key (org-key (car elt))
  7066. fun (nth 1 elt)
  7067. cmd (orgstruct-make-binding fun nfunc key))
  7068. (org-defkey orgstruct-mode-map key cmd))
  7069. ;; Special treatment needed for TAB and RET
  7070. (org-defkey orgstruct-mode-map [(tab)]
  7071. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7072. (org-defkey orgstruct-mode-map "\C-i"
  7073. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7074. (org-defkey orgstruct-mode-map "\M-\C-m"
  7075. (orgstruct-make-binding 'org-insert-heading 105
  7076. "\M-\C-m" [(meta return)]))
  7077. (org-defkey orgstruct-mode-map [(meta return)]
  7078. (orgstruct-make-binding 'org-insert-heading 106
  7079. [(meta return)] "\M-\C-m"))
  7080. (org-defkey orgstruct-mode-map [(shift meta return)]
  7081. (orgstruct-make-binding 'org-insert-todo-heading 107
  7082. [(meta return)] "\M-\C-m"))
  7083. (org-defkey orgstruct-mode-map "\e\C-m"
  7084. (orgstruct-make-binding 'org-insert-heading 108
  7085. "\e\C-m" [?\e (return)]))
  7086. (org-defkey orgstruct-mode-map [?\e (return)]
  7087. (orgstruct-make-binding 'org-insert-heading 109
  7088. [?\e (return)] "\e\C-m"))
  7089. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7090. (orgstruct-make-binding 'org-insert-todo-heading 110
  7091. [?\e (return)] "\e\C-m"))
  7092. (unless org-local-vars
  7093. (setq org-local-vars (org-get-local-variables)))
  7094. t))
  7095. (defun orgstruct-make-binding (fun n &rest keys)
  7096. "Create a function for binding in the structure minor mode.
  7097. FUN is the command to call inside a table. N is used to create a unique
  7098. command name. KEYS are keys that should be checked in for a command
  7099. to execute outside of tables."
  7100. (eval
  7101. (list 'defun
  7102. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7103. '(arg)
  7104. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7105. "Outside of structure, run the binding of `"
  7106. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7107. "'.")
  7108. '(interactive "p")
  7109. (list 'if
  7110. `(org-context-p 'headline 'item
  7111. (and orgstruct-is-++
  7112. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7113. 'item-body))
  7114. (list 'org-run-like-in-org-mode (list 'quote fun))
  7115. (list 'let '(orgstruct-mode)
  7116. (list 'call-interactively
  7117. (append '(or)
  7118. (mapcar (lambda (k)
  7119. (list 'key-binding k))
  7120. keys)
  7121. '('orgstruct-error))))))))
  7122. (defun org-context-p (&rest contexts)
  7123. "Check if local context is any of CONTEXTS.
  7124. Possible values in the list of contexts are `table', `headline', and `item'."
  7125. (let ((pos (point)))
  7126. (goto-char (point-at-bol))
  7127. (prog1 (or (and (memq 'table contexts)
  7128. (looking-at "[ \t]*|"))
  7129. (and (memq 'headline contexts)
  7130. ;;????????? (looking-at "\\*+"))
  7131. (looking-at outline-regexp))
  7132. (and (memq 'item contexts)
  7133. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7134. (and (memq 'item-body contexts)
  7135. (org-in-item-p)))
  7136. (goto-char pos))))
  7137. (defun org-get-local-variables ()
  7138. "Return a list of all local variables in an org-mode buffer."
  7139. (let (varlist)
  7140. (with-current-buffer (get-buffer-create "*Org tmp*")
  7141. (erase-buffer)
  7142. (org-mode)
  7143. (setq varlist (buffer-local-variables)))
  7144. (kill-buffer "*Org tmp*")
  7145. (delq nil
  7146. (mapcar
  7147. (lambda (x)
  7148. (setq x
  7149. (if (symbolp x)
  7150. (list x)
  7151. (list (car x) (list 'quote (cdr x)))))
  7152. (if (string-match
  7153. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7154. (symbol-name (car x)))
  7155. x nil))
  7156. varlist))))
  7157. ;;;###autoload
  7158. (defun org-run-like-in-org-mode (cmd)
  7159. "Run a command, pretending that the current buffer is in Org-mode.
  7160. This will temporarily bind local variables that are typically bound in
  7161. Org-mode to the values they have in Org-mode, and then interactively
  7162. call CMD."
  7163. (org-load-modules-maybe)
  7164. (unless org-local-vars
  7165. (setq org-local-vars (org-get-local-variables)))
  7166. (eval (list 'let org-local-vars
  7167. (list 'call-interactively (list 'quote cmd)))))
  7168. ;;;; Archiving
  7169. (defun org-get-category (&optional pos)
  7170. "Get the category applying to position POS."
  7171. (get-text-property (or pos (point)) 'org-category))
  7172. (defun org-refresh-category-properties ()
  7173. "Refresh category text properties in the buffer."
  7174. (let ((def-cat (cond
  7175. ((null org-category)
  7176. (if buffer-file-name
  7177. (file-name-sans-extension
  7178. (file-name-nondirectory buffer-file-name))
  7179. "???"))
  7180. ((symbolp org-category) (symbol-name org-category))
  7181. (t org-category)))
  7182. beg end cat pos optionp)
  7183. (org-unmodified
  7184. (save-excursion
  7185. (save-restriction
  7186. (widen)
  7187. (goto-char (point-min))
  7188. (put-text-property (point) (point-max) 'org-category def-cat)
  7189. (while (re-search-forward
  7190. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7191. (setq pos (match-end 0)
  7192. optionp (equal (char-after (match-beginning 0)) ?#)
  7193. cat (org-trim (match-string 2)))
  7194. (if optionp
  7195. (setq beg (point-at-bol) end (point-max))
  7196. (org-back-to-heading t)
  7197. (setq beg (point) end (org-end-of-subtree t t)))
  7198. (put-text-property beg end 'org-category cat)
  7199. (goto-char pos)))))))
  7200. ;;;; Link Stuff
  7201. ;;; Link abbreviations
  7202. (defun org-link-expand-abbrev (link)
  7203. "Apply replacements as defined in `org-link-abbrev-alist."
  7204. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7205. (let* ((key (match-string 1 link))
  7206. (as (or (assoc key org-link-abbrev-alist-local)
  7207. (assoc key org-link-abbrev-alist)))
  7208. (tag (and (match-end 2) (match-string 3 link)))
  7209. rpl)
  7210. (if (not as)
  7211. link
  7212. (setq rpl (cdr as))
  7213. (cond
  7214. ((symbolp rpl) (funcall rpl tag))
  7215. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7216. ((string-match "%h" rpl)
  7217. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7218. (t (concat rpl tag)))))
  7219. link))
  7220. ;;; Storing and inserting links
  7221. (defvar org-insert-link-history nil
  7222. "Minibuffer history for links inserted with `org-insert-link'.")
  7223. (defvar org-stored-links nil
  7224. "Contains the links stored with `org-store-link'.")
  7225. (defvar org-store-link-plist nil
  7226. "Plist with info about the most recently link created with `org-store-link'.")
  7227. (defvar org-link-protocols nil
  7228. "Link protocols added to Org-mode using `org-add-link-type'.")
  7229. (defvar org-store-link-functions nil
  7230. "List of functions that are called to create and store a link.
  7231. Each function will be called in turn until one returns a non-nil
  7232. value. Each function should check if it is responsible for creating
  7233. this link (for example by looking at the major mode).
  7234. If not, it must exit and return nil.
  7235. If yes, it should return a non-nil value after a calling
  7236. `org-store-link-props' with a list of properties and values.
  7237. Special properties are:
  7238. :type The link prefix. like \"http\". This must be given.
  7239. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7240. This is obligatory as well.
  7241. :description Optional default description for the second pair
  7242. of brackets in an Org-mode link. The user can still change
  7243. this when inserting this link into an Org-mode buffer.
  7244. In addition to these, any additional properties can be specified
  7245. and then used in remember templates.")
  7246. (defun org-add-link-type (type &optional follow export)
  7247. "Add TYPE to the list of `org-link-types'.
  7248. Re-compute all regular expressions depending on `org-link-types'
  7249. FOLLOW and EXPORT are two functions.
  7250. FOLLOW should take the link path as the single argument and do whatever
  7251. is necessary to follow the link, for example find a file or display
  7252. a mail message.
  7253. EXPORT should format the link path for export to one of the export formats.
  7254. It should be a function accepting three arguments:
  7255. path the path of the link, the text after the prefix (like \"http:\")
  7256. desc the description of the link, if any, nil if there was no description
  7257. format the export format, a symbol like `html' or `latex'.
  7258. The function may use the FORMAT information to return different values
  7259. depending on the format. The return value will be put literally into
  7260. the exported file.
  7261. Org-mode has a built-in default for exporting links. If you are happy with
  7262. this default, there is no need to define an export function for the link
  7263. type. For a simple example of an export function, see `org-bbdb.el'."
  7264. (add-to-list 'org-link-types type t)
  7265. (org-make-link-regexps)
  7266. (if (assoc type org-link-protocols)
  7267. (setcdr (assoc type org-link-protocols) (list follow export))
  7268. (push (list type follow export) org-link-protocols)))
  7269. (defvar org-agenda-buffer-name)
  7270. ;;;###autoload
  7271. (defun org-store-link (arg)
  7272. "\\<org-mode-map>Store an org-link to the current location.
  7273. This link is added to `org-stored-links' and can later be inserted
  7274. into an org-buffer with \\[org-insert-link].
  7275. For some link types, a prefix arg is interpreted:
  7276. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7277. For file links, arg negates `org-context-in-file-links'."
  7278. (interactive "P")
  7279. (org-load-modules-maybe)
  7280. (setq org-store-link-plist nil) ; reset
  7281. (let ((outline-regexp (org-get-limited-outline-regexp))
  7282. link cpltxt desc description search txt custom-id)
  7283. (cond
  7284. ((run-hook-with-args-until-success 'org-store-link-functions)
  7285. (setq link (plist-get org-store-link-plist :link)
  7286. desc (or (plist-get org-store-link-plist :description) link)))
  7287. ((equal (buffer-name) "*Org Edit Src Example*")
  7288. (let (label gc)
  7289. (while (or (not label)
  7290. (save-excursion
  7291. (save-restriction
  7292. (widen)
  7293. (goto-char (point-min))
  7294. (re-search-forward
  7295. (regexp-quote (format org-coderef-label-format label))
  7296. nil t))))
  7297. (when label (message "Label exists already") (sit-for 2))
  7298. (setq label (read-string "Code line label: " label)))
  7299. (end-of-line 1)
  7300. (setq link (format org-coderef-label-format label))
  7301. (setq gc (- 79 (length link)))
  7302. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7303. (insert link)
  7304. (setq link (concat "(" label ")") desc nil)))
  7305. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7306. ;; We are in the agenda, link to referenced location
  7307. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7308. (get-text-property (point) 'org-marker))))
  7309. (when m
  7310. (org-with-point-at m
  7311. (if (interactive-p)
  7312. (call-interactively 'org-store-link)
  7313. (org-store-link nil))))))
  7314. ((eq major-mode 'calendar-mode)
  7315. (let ((cd (calendar-cursor-to-date)))
  7316. (setq link
  7317. (format-time-string
  7318. (car org-time-stamp-formats)
  7319. (apply 'encode-time
  7320. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7321. nil nil nil))))
  7322. (org-store-link-props :type "calendar" :date cd)))
  7323. ((eq major-mode 'w3-mode)
  7324. (setq cpltxt (if (and (buffer-name)
  7325. (not (string-match "Untitled" (buffer-name))))
  7326. (buffer-name)
  7327. (url-view-url t))
  7328. link (org-make-link (url-view-url t)))
  7329. (org-store-link-props :type "w3" :url (url-view-url t)))
  7330. ((eq major-mode 'w3m-mode)
  7331. (setq cpltxt (or w3m-current-title w3m-current-url)
  7332. link (org-make-link w3m-current-url))
  7333. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7334. ((setq search (run-hook-with-args-until-success
  7335. 'org-create-file-search-functions))
  7336. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7337. "::" search))
  7338. (setq cpltxt (or description link)))
  7339. ((eq major-mode 'image-mode)
  7340. (setq cpltxt (concat "file:"
  7341. (abbreviate-file-name buffer-file-name))
  7342. link (org-make-link cpltxt))
  7343. (org-store-link-props :type "image" :file buffer-file-name))
  7344. ((eq major-mode 'dired-mode)
  7345. ;; link to the file in the current line
  7346. (let ((file (dired-get-filename nil t)))
  7347. (setq file (if file
  7348. (abbreviate-file-name
  7349. (expand-file-name (dired-get-filename nil t)))
  7350. ;; otherwise, no file so use current directory.
  7351. default-directory))
  7352. (setq cpltxt (concat "file:" file)
  7353. link (org-make-link cpltxt))))
  7354. ((and buffer-file-name (org-mode-p))
  7355. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7356. (cond
  7357. ((org-in-regexp "<<\\(.*?\\)>>")
  7358. (setq cpltxt
  7359. (concat "file:"
  7360. (abbreviate-file-name buffer-file-name)
  7361. "::" (match-string 1))
  7362. link (org-make-link cpltxt)))
  7363. ((and (featurep 'org-id)
  7364. (or (eq org-link-to-org-use-id t)
  7365. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7366. (interactive-p))
  7367. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7368. (interactive-p)
  7369. (not custom-id))
  7370. (and org-link-to-org-use-id
  7371. (condition-case nil
  7372. (org-entry-get nil "ID")
  7373. (error nil)))))
  7374. ;; We can make a link using the ID.
  7375. (setq link (condition-case nil
  7376. (prog1 (org-id-store-link)
  7377. (setq desc (plist-get org-store-link-plist
  7378. :description)))
  7379. (error
  7380. ;; probably before first headline, link to file only
  7381. (concat "file:"
  7382. (abbreviate-file-name buffer-file-name))))))
  7383. (t
  7384. ;; Just link to current headline
  7385. (setq cpltxt (concat "file:"
  7386. (abbreviate-file-name buffer-file-name)))
  7387. ;; Add a context search string
  7388. (when (org-xor org-context-in-file-links arg)
  7389. (setq txt (cond
  7390. ((org-on-heading-p) nil)
  7391. ((org-region-active-p)
  7392. (buffer-substring (region-beginning) (region-end)))
  7393. (t nil)))
  7394. (when (or (null txt) (string-match "\\S-" txt))
  7395. (setq cpltxt
  7396. (concat cpltxt "::"
  7397. (condition-case nil
  7398. (org-make-org-heading-search-string txt)
  7399. (error "")))
  7400. desc (or (nth 4 (ignore-errors
  7401. (org-heading-components))) "NONE"))))
  7402. (if (string-match "::\\'" cpltxt)
  7403. (setq cpltxt (substring cpltxt 0 -2)))
  7404. (setq link (org-make-link cpltxt)))))
  7405. ((buffer-file-name (buffer-base-buffer))
  7406. ;; Just link to this file here.
  7407. (setq cpltxt (concat "file:"
  7408. (abbreviate-file-name
  7409. (buffer-file-name (buffer-base-buffer)))))
  7410. ;; Add a context string
  7411. (when (org-xor org-context-in-file-links arg)
  7412. (setq txt (if (org-region-active-p)
  7413. (buffer-substring (region-beginning) (region-end))
  7414. (buffer-substring (point-at-bol) (point-at-eol))))
  7415. ;; Only use search option if there is some text.
  7416. (when (string-match "\\S-" txt)
  7417. (setq cpltxt
  7418. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7419. desc "NONE")))
  7420. (setq link (org-make-link cpltxt)))
  7421. ((interactive-p)
  7422. (error "Cannot link to a buffer which is not visiting a file"))
  7423. (t (setq link nil)))
  7424. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7425. (setq link (or link cpltxt)
  7426. desc (or desc cpltxt))
  7427. (if (equal desc "NONE") (setq desc nil))
  7428. (if (and (or (interactive-p) executing-kbd-macro) link)
  7429. (progn
  7430. (setq org-stored-links
  7431. (cons (list link desc) org-stored-links))
  7432. (message "Stored: %s" (or desc link))
  7433. (when custom-id
  7434. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7435. "::#" custom-id))
  7436. (setq org-stored-links
  7437. (cons (list link desc) org-stored-links))))
  7438. (and link (org-make-link-string link desc)))))
  7439. (defun org-store-link-props (&rest plist)
  7440. "Store link properties, extract names and addresses."
  7441. (let (x adr)
  7442. (when (setq x (plist-get plist :from))
  7443. (setq adr (mail-extract-address-components x))
  7444. (setq plist (plist-put plist :fromname (car adr)))
  7445. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7446. (when (setq x (plist-get plist :to))
  7447. (setq adr (mail-extract-address-components x))
  7448. (setq plist (plist-put plist :toname (car adr)))
  7449. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7450. (let ((from (plist-get plist :from))
  7451. (to (plist-get plist :to)))
  7452. (when (and from to org-from-is-user-regexp)
  7453. (setq plist
  7454. (plist-put plist :fromto
  7455. (if (string-match org-from-is-user-regexp from)
  7456. (concat "to %t")
  7457. (concat "from %f"))))))
  7458. (setq org-store-link-plist plist))
  7459. (defun org-add-link-props (&rest plist)
  7460. "Add these properties to the link property list."
  7461. (let (key value)
  7462. (while plist
  7463. (setq key (pop plist) value (pop plist))
  7464. (setq org-store-link-plist
  7465. (plist-put org-store-link-plist key value)))))
  7466. (defun org-email-link-description (&optional fmt)
  7467. "Return the description part of an email link.
  7468. This takes information from `org-store-link-plist' and formats it
  7469. according to FMT (default from `org-email-link-description-format')."
  7470. (setq fmt (or fmt org-email-link-description-format))
  7471. (let* ((p org-store-link-plist)
  7472. (to (plist-get p :toaddress))
  7473. (from (plist-get p :fromaddress))
  7474. (table
  7475. (list
  7476. (cons "%c" (plist-get p :fromto))
  7477. (cons "%F" (plist-get p :from))
  7478. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7479. (cons "%T" (plist-get p :to))
  7480. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7481. (cons "%s" (plist-get p :subject))
  7482. (cons "%m" (plist-get p :message-id)))))
  7483. (when (string-match "%c" fmt)
  7484. ;; Check if the user wrote this message
  7485. (if (and org-from-is-user-regexp from to
  7486. (save-match-data (string-match org-from-is-user-regexp from)))
  7487. (setq fmt (replace-match "to %t" t t fmt))
  7488. (setq fmt (replace-match "from %f" t t fmt))))
  7489. (org-replace-escapes fmt table)))
  7490. (defun org-make-org-heading-search-string (&optional string heading)
  7491. "Make search string for STRING or current headline."
  7492. (interactive)
  7493. (let ((s (or string (org-get-heading))))
  7494. (unless (and string (not heading))
  7495. ;; We are using a headline, clean up garbage in there.
  7496. (if (string-match org-todo-regexp s)
  7497. (setq s (replace-match "" t t s)))
  7498. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7499. (setq s (replace-match "" t t s)))
  7500. (setq s (org-trim s))
  7501. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7502. org-comment-string "\\)") s)
  7503. (setq s (replace-match "" t t s)))
  7504. (while (string-match org-ts-regexp s)
  7505. (setq s (replace-match "" t t s))))
  7506. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7507. (setq s (replace-match " " t t s)))
  7508. (or string (setq s (concat "*" s))) ; Add * for headlines
  7509. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7510. (defun org-make-link (&rest strings)
  7511. "Concatenate STRINGS."
  7512. (apply 'concat strings))
  7513. (defun org-make-link-string (link &optional description)
  7514. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7515. (unless (string-match "\\S-" link)
  7516. (error "Empty link"))
  7517. (when (and description
  7518. (stringp description)
  7519. (not (string-match "\\S-" description)))
  7520. (setq description nil))
  7521. (when (stringp description)
  7522. ;; Remove brackets from the description, they are fatal.
  7523. (while (string-match "\\[" description)
  7524. (setq description (replace-match "{" t t description)))
  7525. (while (string-match "\\]" description)
  7526. (setq description (replace-match "}" t t description))))
  7527. (when (equal (org-link-escape link) description)
  7528. ;; No description needed, it is identical
  7529. (setq description nil))
  7530. (when (and (not description)
  7531. (not (equal link (org-link-escape link))))
  7532. (setq description (org-extract-attributes link)))
  7533. (concat "[[" (org-link-escape link) "]"
  7534. (if description (concat "[" description "]") "")
  7535. "]"))
  7536. (defconst org-link-escape-chars
  7537. '((?\ . "%20")
  7538. (?\[ . "%5B")
  7539. (?\] . "%5D")
  7540. (?\340 . "%E0") ; `a
  7541. (?\342 . "%E2") ; ^a
  7542. (?\347 . "%E7") ; ,c
  7543. (?\350 . "%E8") ; `e
  7544. (?\351 . "%E9") ; 'e
  7545. (?\352 . "%EA") ; ^e
  7546. (?\356 . "%EE") ; ^i
  7547. (?\364 . "%F4") ; ^o
  7548. (?\371 . "%F9") ; `u
  7549. (?\373 . "%FB") ; ^u
  7550. (?\; . "%3B")
  7551. ;; (?? . "%3F")
  7552. (?= . "%3D")
  7553. (?+ . "%2B")
  7554. )
  7555. "Association list of escapes for some characters problematic in links.
  7556. This is the list that is used for internal purposes.")
  7557. (defvar org-url-encoding-use-url-hexify nil)
  7558. (defconst org-link-escape-chars-browser
  7559. '((?\ . "%20")) ; 32 for the SPC char
  7560. "Association list of escapes for some characters problematic in links.
  7561. This is the list that is used before handing over to the browser.")
  7562. (defun org-link-escape (text &optional table)
  7563. "Escape characters in TEXT that are problematic for links."
  7564. (if (and org-url-encoding-use-url-hexify (not table))
  7565. (url-hexify-string text)
  7566. (setq table (or table org-link-escape-chars))
  7567. (when text
  7568. (let ((re (mapconcat (lambda (x) (regexp-quote
  7569. (char-to-string (car x))))
  7570. table "\\|")))
  7571. (while (string-match re text)
  7572. (setq text
  7573. (replace-match
  7574. (cdr (assoc (string-to-char (match-string 0 text))
  7575. table))
  7576. t t text)))
  7577. text))))
  7578. (defun org-link-unescape (text &optional table)
  7579. "Reverse the action of `org-link-escape'."
  7580. (if (and org-url-encoding-use-url-hexify (not table))
  7581. (url-unhex-string text)
  7582. (setq table (or table org-link-escape-chars))
  7583. (when text
  7584. (let ((case-fold-search t)
  7585. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7586. table "\\|")))
  7587. (while (string-match re text)
  7588. (setq text
  7589. (replace-match
  7590. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7591. table)))
  7592. t t text)))
  7593. text))))
  7594. (defun org-xor (a b)
  7595. "Exclusive or."
  7596. (if a (not b) b))
  7597. (defun org-fixup-message-id-for-http (s)
  7598. "Replace special characters in a message id, so it can be used in an http query."
  7599. (when (string-match "%" s)
  7600. (setq s (mapconcat (lambda (c)
  7601. (if (eq c ?%)
  7602. "%25"
  7603. (char-to-string c)))
  7604. s "")))
  7605. (while (string-match "<" s)
  7606. (setq s (replace-match "%3C" t t s)))
  7607. (while (string-match ">" s)
  7608. (setq s (replace-match "%3E" t t s)))
  7609. (while (string-match "@" s)
  7610. (setq s (replace-match "%40" t t s)))
  7611. s)
  7612. ;;;###autoload
  7613. (defun org-insert-link-global ()
  7614. "Insert a link like Org-mode does.
  7615. This command can be called in any mode to insert a link in Org-mode syntax."
  7616. (interactive)
  7617. (org-load-modules-maybe)
  7618. (org-run-like-in-org-mode 'org-insert-link))
  7619. (defun org-insert-link (&optional complete-file link-location)
  7620. "Insert a link. At the prompt, enter the link.
  7621. Completion can be used to insert any of the link protocol prefixes like
  7622. http or ftp in use.
  7623. The history can be used to select a link previously stored with
  7624. `org-store-link'. When the empty string is entered (i.e. if you just
  7625. press RET at the prompt), the link defaults to the most recently
  7626. stored link. As SPC triggers completion in the minibuffer, you need to
  7627. use M-SPC or C-q SPC to force the insertion of a space character.
  7628. You will also be prompted for a description, and if one is given, it will
  7629. be displayed in the buffer instead of the link.
  7630. If there is already a link at point, this command will allow you to edit link
  7631. and description parts.
  7632. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7633. be selected using completion. The path to the file will be relative to the
  7634. current directory if the file is in the current directory or a subdirectory.
  7635. Otherwise, the link will be the absolute path as completed in the minibuffer
  7636. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7637. option `org-link-file-path-type'.
  7638. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7639. the current directory or below.
  7640. With three \\[universal-argument] prefixes, negate the meaning of
  7641. `org-keep-stored-link-after-insertion'.
  7642. If `org-make-link-description-function' is non-nil, this function will be
  7643. called with the link target, and the result will be the default
  7644. link description.
  7645. If the LINK-LOCATION parameter is non-nil, this value will be
  7646. used as the link location instead of reading one interactively."
  7647. (interactive "P")
  7648. (let* ((wcf (current-window-configuration))
  7649. (region (if (org-region-active-p)
  7650. (buffer-substring (region-beginning) (region-end))))
  7651. (remove (and region (list (region-beginning) (region-end))))
  7652. (desc region)
  7653. tmphist ; byte-compile incorrectly complains about this
  7654. (link link-location)
  7655. entry file all-prefixes)
  7656. (cond
  7657. (link-location) ; specified by arg, just use it.
  7658. ((org-in-regexp org-bracket-link-regexp 1)
  7659. ;; We do have a link at point, and we are going to edit it.
  7660. (setq remove (list (match-beginning 0) (match-end 0)))
  7661. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7662. (setq link (read-string "Link: "
  7663. (org-link-unescape
  7664. (org-match-string-no-properties 1)))))
  7665. ((or (org-in-regexp org-angle-link-re)
  7666. (org-in-regexp org-plain-link-re))
  7667. ;; Convert to bracket link
  7668. (setq remove (list (match-beginning 0) (match-end 0))
  7669. link (read-string "Link: "
  7670. (org-remove-angle-brackets (match-string 0)))))
  7671. ((member complete-file '((4) (16)))
  7672. ;; Completing read for file names.
  7673. (setq link (org-file-complete-link complete-file)))
  7674. (t
  7675. ;; Read link, with completion for stored links.
  7676. (with-output-to-temp-buffer "*Org Links*"
  7677. (princ "Insert a link.
  7678. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7679. (when org-stored-links
  7680. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7681. (princ (mapconcat
  7682. (lambda (x)
  7683. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7684. (reverse org-stored-links) "\n"))))
  7685. (let ((cw (selected-window)))
  7686. (select-window (get-buffer-window "*Org Links*" 'visible))
  7687. (setq truncate-lines t)
  7688. (unless (pos-visible-in-window-p (point-max))
  7689. (org-fit-window-to-buffer))
  7690. (and (window-live-p cw) (select-window cw)))
  7691. ;; Fake a link history, containing the stored links.
  7692. (setq tmphist (append (mapcar 'car org-stored-links)
  7693. org-insert-link-history))
  7694. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7695. (mapcar 'car org-link-abbrev-alist)
  7696. org-link-types))
  7697. (unwind-protect
  7698. (progn
  7699. (setq link
  7700. (let ((org-completion-use-ido nil)
  7701. (org-completion-use-iswitchb nil))
  7702. (org-completing-read
  7703. "Link: "
  7704. (append
  7705. (mapcar (lambda (x) (list (concat x ":")))
  7706. all-prefixes)
  7707. (mapcar 'car org-stored-links))
  7708. nil nil nil
  7709. 'tmphist
  7710. (car (car org-stored-links)))))
  7711. (if (not (string-match "\\S-" link))
  7712. (error "No link selected"))
  7713. (if (or (member link all-prefixes)
  7714. (and (equal ":" (substring link -1))
  7715. (member (substring link 0 -1) all-prefixes)
  7716. (setq link (substring link 0 -1))))
  7717. (setq link (org-link-try-special-completion link))))
  7718. (set-window-configuration wcf)
  7719. (kill-buffer "*Org Links*"))
  7720. (setq entry (assoc link org-stored-links))
  7721. (or entry (push link org-insert-link-history))
  7722. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7723. (not org-keep-stored-link-after-insertion))
  7724. (setq org-stored-links (delq (assoc link org-stored-links)
  7725. org-stored-links)))
  7726. (setq desc (or desc (nth 1 entry)))))
  7727. (if (string-match org-plain-link-re link)
  7728. ;; URL-like link, normalize the use of angular brackets.
  7729. (setq link (org-make-link (org-remove-angle-brackets link))))
  7730. ;; Check if we are linking to the current file with a search option
  7731. ;; If yes, simplify the link by using only the search option.
  7732. (when (and buffer-file-name
  7733. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7734. (let* ((path (match-string 1 link))
  7735. (case-fold-search nil)
  7736. (search (match-string 2 link)))
  7737. (save-match-data
  7738. (if (equal (file-truename buffer-file-name) (file-truename path))
  7739. ;; We are linking to this same file, with a search option
  7740. (setq link search)))))
  7741. ;; Check if we can/should use a relative path. If yes, simplify the link
  7742. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7743. (let* ((type (match-string 1 link))
  7744. (path (match-string 2 link))
  7745. (origpath path)
  7746. (case-fold-search nil))
  7747. (cond
  7748. ((or (eq org-link-file-path-type 'absolute)
  7749. (equal complete-file '(16)))
  7750. (setq path (abbreviate-file-name (expand-file-name path))))
  7751. ((eq org-link-file-path-type 'noabbrev)
  7752. (setq path (expand-file-name path)))
  7753. ((eq org-link-file-path-type 'relative)
  7754. (setq path (file-relative-name path)))
  7755. (t
  7756. (save-match-data
  7757. (if (string-match (concat "^" (regexp-quote
  7758. (file-name-as-directory
  7759. (expand-file-name "."))))
  7760. (expand-file-name path))
  7761. ;; We are linking a file with relative path name.
  7762. (setq path (substring (expand-file-name path)
  7763. (match-end 0)))
  7764. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7765. (setq link (concat type path))
  7766. (if (equal desc origpath)
  7767. (setq desc path))))
  7768. (if org-make-link-description-function
  7769. (setq desc (funcall org-make-link-description-function link desc)))
  7770. (setq desc (read-string "Description: " desc))
  7771. (unless (string-match "\\S-" desc) (setq desc nil))
  7772. (if remove (apply 'delete-region remove))
  7773. (insert (org-make-link-string link desc))))
  7774. (defun org-link-try-special-completion (type)
  7775. "If there is completion support for link type TYPE, offer it."
  7776. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7777. (if (functionp fun)
  7778. (funcall fun)
  7779. (read-string "Link (no completion support): " (concat type ":")))))
  7780. (defun org-file-complete-link (&optional arg)
  7781. "Create a file link using completion."
  7782. (let (file link)
  7783. (setq file (read-file-name "File: "))
  7784. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7785. (pwd1 (file-name-as-directory (abbreviate-file-name
  7786. (expand-file-name ".")))))
  7787. (cond
  7788. ((equal arg '(16))
  7789. (setq link (org-make-link
  7790. "file:"
  7791. (abbreviate-file-name (expand-file-name file)))))
  7792. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7793. (setq link (org-make-link "file:" (match-string 1 file))))
  7794. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7795. (expand-file-name file))
  7796. (setq link (org-make-link
  7797. "file:" (match-string 1 (expand-file-name file)))))
  7798. (t (setq link (org-make-link "file:" file)))))
  7799. link))
  7800. (defun org-completing-read (&rest args)
  7801. "Completing-read with SPACE being a normal character."
  7802. (let ((minibuffer-local-completion-map
  7803. (copy-keymap minibuffer-local-completion-map)))
  7804. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7805. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7806. (apply 'org-icompleting-read args)))
  7807. (defun org-completing-read-no-i (&rest args)
  7808. (let (org-completion-use-ido org-completion-use-iswitchb)
  7809. (apply 'org-completing-read args)))
  7810. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7811. "Use iswitch as a completing-read replacement to choose from choices.
  7812. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7813. from."
  7814. (let* ((iswitchb-use-virtual-buffers nil)
  7815. (iswitchb-make-buflist-hook
  7816. (lambda ()
  7817. (setq iswitchb-temp-buflist choices))))
  7818. (iswitchb-read-buffer prompt)))
  7819. (defun org-icompleting-read (&rest args)
  7820. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7821. (org-without-partial-completion
  7822. (if (and org-completion-use-ido
  7823. (fboundp 'ido-completing-read)
  7824. (boundp 'ido-mode) ido-mode
  7825. (listp (second args)))
  7826. (let ((ido-enter-matching-directory nil))
  7827. (apply 'ido-completing-read (concat (car args))
  7828. (if (consp (car (nth 1 args)))
  7829. (mapcar (lambda (x) (car x)) (nth 1 args))
  7830. (nth 1 args))
  7831. (cddr args)))
  7832. (if (and org-completion-use-iswitchb
  7833. (boundp 'iswitchb-mode) iswitchb-mode
  7834. (listp (second args)))
  7835. (apply 'org-iswitchb-completing-read (concat (car args))
  7836. (if (consp (car (nth 1 args)))
  7837. (mapcar (lambda (x) (car x)) (nth 1 args))
  7838. (nth 1 args))
  7839. (cddr args))
  7840. (apply 'completing-read args)))))
  7841. (defun org-extract-attributes (s)
  7842. "Extract the attributes cookie from a string and set as text property."
  7843. (let (a attr (start 0) key value)
  7844. (save-match-data
  7845. (when (string-match "{{\\([^}]+\\)}}$" s)
  7846. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7847. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7848. (setq key (match-string 1 a) value (match-string 2 a)
  7849. start (match-end 0)
  7850. attr (plist-put attr (intern key) value))))
  7851. (org-add-props s nil 'org-attr attr))
  7852. s))
  7853. (defun org-extract-attributes-from-string (tag)
  7854. (let (key value attr)
  7855. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7856. (setq key (match-string 1 tag) value (match-string 2 tag)
  7857. tag (replace-match "" t t tag)
  7858. attr (plist-put attr (intern key) value)))
  7859. (cons tag attr)))
  7860. (defun org-attributes-to-string (plist)
  7861. "Format a property list into an HTML attribute list."
  7862. (let ((s "") key value)
  7863. (while plist
  7864. (setq key (pop plist) value (pop plist))
  7865. (and value
  7866. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7867. s))
  7868. ;;; Opening/following a link
  7869. (defvar org-link-search-failed nil)
  7870. (defvar org-open-link-functions nil
  7871. "Hook for functions finding a plain text link.
  7872. These functions must take a single argument, the link content.
  7873. They will be called for links that look like [[link text][description]]
  7874. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7875. like a filename (e.g. \"./blue.png\").
  7876. These functions will be called *before* Org attempts to resolve the
  7877. link by doing text searches in the current buffer - so if you want a
  7878. link \"[[target]]\" to still find \"<<target>>\", your function should
  7879. handle this as a special case.
  7880. When the function does handle the link, it must return a non-nil value.
  7881. If it decides that it is not responsible for this link, it must return
  7882. nil to indicate that that Org-mode can continue with other options
  7883. like exact and fuzzy text search.")
  7884. (defun org-next-link ()
  7885. "Move forward to the next link.
  7886. If the link is in hidden text, expose it."
  7887. (interactive)
  7888. (when (and org-link-search-failed (eq this-command last-command))
  7889. (goto-char (point-min))
  7890. (message "Link search wrapped back to beginning of buffer"))
  7891. (setq org-link-search-failed nil)
  7892. (let* ((pos (point))
  7893. (ct (org-context))
  7894. (a (assoc :link ct)))
  7895. (if a (goto-char (nth 2 a)))
  7896. (if (re-search-forward org-any-link-re nil t)
  7897. (progn
  7898. (goto-char (match-beginning 0))
  7899. (if (org-invisible-p) (org-show-context)))
  7900. (goto-char pos)
  7901. (setq org-link-search-failed t)
  7902. (error "No further link found"))))
  7903. (defun org-previous-link ()
  7904. "Move backward to the previous link.
  7905. If the link is in hidden text, expose it."
  7906. (interactive)
  7907. (when (and org-link-search-failed (eq this-command last-command))
  7908. (goto-char (point-max))
  7909. (message "Link search wrapped back to end of buffer"))
  7910. (setq org-link-search-failed nil)
  7911. (let* ((pos (point))
  7912. (ct (org-context))
  7913. (a (assoc :link ct)))
  7914. (if a (goto-char (nth 1 a)))
  7915. (if (re-search-backward org-any-link-re nil t)
  7916. (progn
  7917. (goto-char (match-beginning 0))
  7918. (if (org-invisible-p) (org-show-context)))
  7919. (goto-char pos)
  7920. (setq org-link-search-failed t)
  7921. (error "No further link found"))))
  7922. (defun org-translate-link (s)
  7923. "Translate a link string if a translation function has been defined."
  7924. (if (and org-link-translation-function
  7925. (fboundp org-link-translation-function)
  7926. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7927. (progn
  7928. (setq s (funcall org-link-translation-function
  7929. (match-string 1) (match-string 2)))
  7930. (concat (car s) ":" (cdr s)))
  7931. s))
  7932. (defun org-translate-link-from-planner (type path)
  7933. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7934. This is still an experimental function, your mileage may vary."
  7935. (cond
  7936. ((member type '("http" "https" "news" "ftp"))
  7937. ;; standard Internet links are the same.
  7938. nil)
  7939. ((and (equal type "irc") (string-match "^//" path))
  7940. ;; Planner has two / at the beginning of an irc link, we have 1.
  7941. ;; We should have zero, actually....
  7942. (setq path (substring path 1)))
  7943. ((and (equal type "lisp") (string-match "^/" path))
  7944. ;; Planner has a slash, we do not.
  7945. (setq type "elisp" path (substring path 1)))
  7946. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7947. ;; A typical message link. Planner has the id after the final slash,
  7948. ;; we separate it with a hash mark
  7949. (setq path (concat (match-string 1 path) "#"
  7950. (org-remove-angle-brackets (match-string 2 path)))))
  7951. )
  7952. (cons type path))
  7953. (defun org-find-file-at-mouse (ev)
  7954. "Open file link or URL at mouse."
  7955. (interactive "e")
  7956. (mouse-set-point ev)
  7957. (org-open-at-point 'in-emacs))
  7958. (defun org-open-at-mouse (ev)
  7959. "Open file link or URL at mouse."
  7960. (interactive "e")
  7961. (mouse-set-point ev)
  7962. (if (eq major-mode 'org-agenda-mode)
  7963. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7964. (org-open-at-point))
  7965. (defvar org-window-config-before-follow-link nil
  7966. "The window configuration before following a link.
  7967. This is saved in case the need arises to restore it.")
  7968. (defvar org-open-link-marker (make-marker)
  7969. "Marker pointing to the location where `org-open-at-point; was called.")
  7970. ;;;###autoload
  7971. (defun org-open-at-point-global ()
  7972. "Follow a link like Org-mode does.
  7973. This command can be called in any mode to follow a link that has
  7974. Org-mode syntax."
  7975. (interactive)
  7976. (org-run-like-in-org-mode 'org-open-at-point))
  7977. ;;;###autoload
  7978. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7979. "Open a link in the string S, as if it was in Org-mode."
  7980. (interactive "sLink: \nP")
  7981. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7982. (with-temp-buffer
  7983. (let ((org-inhibit-startup t))
  7984. (org-mode)
  7985. (insert s)
  7986. (goto-char (point-min))
  7987. (when reference-buffer
  7988. (setq org-link-abbrev-alist-local
  7989. (with-current-buffer reference-buffer
  7990. org-link-abbrev-alist-local)))
  7991. (org-open-at-point arg reference-buffer)))))
  7992. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7993. "Open link at or after point.
  7994. If there is no link at point, this function will search forward up to
  7995. the end of the current line.
  7996. Normally, files will be opened by an appropriate application. If the
  7997. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7998. With a double prefix argument, try to open outside of Emacs, in the
  7999. application the system uses for this file type."
  8000. (interactive "P")
  8001. ;; if in a code block, then open the block's results
  8002. (unless (call-interactively #'org-babel-open-src-block-result)
  8003. (org-load-modules-maybe)
  8004. (move-marker org-open-link-marker (point))
  8005. (setq org-window-config-before-follow-link (current-window-configuration))
  8006. (org-remove-occur-highlights nil nil t)
  8007. (cond
  8008. ((and (org-on-heading-p)
  8009. (not (org-in-regexp
  8010. (concat org-plain-link-re "\\|"
  8011. org-bracket-link-regexp "\\|"
  8012. org-angle-link-re "\\|"
  8013. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8014. (not (get-text-property (point) 'org-linked-text)))
  8015. (or (org-offer-links-in-entry in-emacs)
  8016. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8017. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8018. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8019. (org-footnote-action))
  8020. (t
  8021. (let (type path link line search (pos (point)))
  8022. (catch 'match
  8023. (save-excursion
  8024. (skip-chars-forward "^]\n\r")
  8025. (when (org-in-regexp org-bracket-link-regexp 1)
  8026. (setq link (org-extract-attributes
  8027. (org-link-unescape (org-match-string-no-properties 1))))
  8028. (while (string-match " *\n *" link)
  8029. (setq link (replace-match " " t t link)))
  8030. (setq link (org-link-expand-abbrev link))
  8031. (cond
  8032. ((or (file-name-absolute-p link)
  8033. (string-match "^\\.\\.?/" link))
  8034. (setq type "file" path link))
  8035. ((string-match org-link-re-with-space3 link)
  8036. (setq type (match-string 1 link) path (match-string 2 link)))
  8037. (t (setq type "thisfile" path link)))
  8038. (throw 'match t)))
  8039. (when (get-text-property (point) 'org-linked-text)
  8040. (setq type "thisfile"
  8041. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8042. (1+ (point)) (point))
  8043. path (buffer-substring
  8044. (previous-single-property-change pos 'org-linked-text)
  8045. (next-single-property-change pos 'org-linked-text)))
  8046. (throw 'match t))
  8047. (save-excursion
  8048. (when (or (org-in-regexp org-angle-link-re)
  8049. (org-in-regexp org-plain-link-re))
  8050. (setq type (match-string 1) path (match-string 2))
  8051. (throw 'match t)))
  8052. (save-excursion
  8053. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  8054. (setq type "tags"
  8055. path (match-string 1))
  8056. (while (string-match ":" path)
  8057. (setq path (replace-match "+" t t path)))
  8058. (throw 'match t)))
  8059. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8060. (setq type "tree-match"
  8061. path (match-string 1))
  8062. (throw 'match t)))
  8063. (unless path
  8064. (error "No link found"))
  8065. ;; switch back to reference buffer
  8066. ;; needed when if called in a temporary buffer through
  8067. ;; org-open-link-from-string
  8068. (with-current-buffer (or reference-buffer (current-buffer))
  8069. ;; Remove any trailing spaces in path
  8070. (if (string-match " +\\'" path)
  8071. (setq path (replace-match "" t t path)))
  8072. (if (and org-link-translation-function
  8073. (fboundp org-link-translation-function))
  8074. ;; Check if we need to translate the link
  8075. (let ((tmp (funcall org-link-translation-function type path)))
  8076. (setq type (car tmp) path (cdr tmp))))
  8077. (cond
  8078. ((assoc type org-link-protocols)
  8079. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8080. ((equal type "mailto")
  8081. (let ((cmd (car org-link-mailto-program))
  8082. (args (cdr org-link-mailto-program)) args1
  8083. (address path) (subject "") a)
  8084. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8085. (setq address (match-string 1 path)
  8086. subject (org-link-escape (match-string 2 path))))
  8087. (while args
  8088. (cond
  8089. ((not (stringp (car args))) (push (pop args) args1))
  8090. (t (setq a (pop args))
  8091. (if (string-match "%a" a)
  8092. (setq a (replace-match address t t a)))
  8093. (if (string-match "%s" a)
  8094. (setq a (replace-match subject t t a)))
  8095. (push a args1))))
  8096. (apply cmd (nreverse args1))))
  8097. ((member type '("http" "https" "ftp" "news"))
  8098. (browse-url (concat type ":" (org-link-escape
  8099. path org-link-escape-chars-browser))))
  8100. ((string= type "doi")
  8101. (browse-url (concat "http://dx.doi.org/"
  8102. (org-link-escape
  8103. path org-link-escape-chars-browser))))
  8104. ((member type '("message"))
  8105. (browse-url (concat type ":" path)))
  8106. ((string= type "tags")
  8107. (org-tags-view in-emacs path))
  8108. ((string= type "tree-match")
  8109. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8110. ((string= type "file")
  8111. (if (string-match "::\\([0-9]+\\)\\'" path)
  8112. (setq line (string-to-number (match-string 1 path))
  8113. path (substring path 0 (match-beginning 0)))
  8114. (if (string-match "::\\(.+\\)\\'" path)
  8115. (setq search (match-string 1 path)
  8116. path (substring path 0 (match-beginning 0)))))
  8117. (if (string-match "[*?{]" (file-name-nondirectory path))
  8118. (dired path)
  8119. (org-open-file path in-emacs line search)))
  8120. ((string= type "news")
  8121. (require 'org-gnus)
  8122. (org-gnus-follow-link path))
  8123. ((string= type "shell")
  8124. (let ((cmd path))
  8125. (if (or (not org-confirm-shell-link-function)
  8126. (funcall org-confirm-shell-link-function
  8127. (format "Execute \"%s\" in shell? "
  8128. (org-add-props cmd nil
  8129. 'face 'org-warning))))
  8130. (progn
  8131. (message "Executing %s" cmd)
  8132. (shell-command cmd))
  8133. (error "Abort"))))
  8134. ((string= type "elisp")
  8135. (let ((cmd path))
  8136. (if (or (not org-confirm-elisp-link-function)
  8137. (funcall org-confirm-elisp-link-function
  8138. (format "Execute \"%s\" as elisp? "
  8139. (org-add-props cmd nil
  8140. 'face 'org-warning))))
  8141. (message "%s => %s" cmd
  8142. (if (equal (string-to-char cmd) ?\()
  8143. (eval (read cmd))
  8144. (call-interactively (read cmd))))
  8145. (error "Abort"))))
  8146. ((and (string= type "thisfile")
  8147. (run-hook-with-args-until-success
  8148. 'org-open-link-functions path)))
  8149. ((string= type "thisfile")
  8150. (if in-emacs
  8151. (switch-to-buffer-other-window
  8152. (org-get-buffer-for-internal-link (current-buffer)))
  8153. (org-mark-ring-push))
  8154. (let ((cmd `(org-link-search
  8155. ,path
  8156. ,(cond ((equal in-emacs '(4)) 'occur)
  8157. ((equal in-emacs '(16)) 'org-occur)
  8158. (t nil))
  8159. ,pos)))
  8160. (condition-case nil (eval cmd)
  8161. (error (progn (widen) (eval cmd))))))
  8162. (t
  8163. (browse-url-at-point)))))))
  8164. (move-marker org-open-link-marker nil)
  8165. (run-hook-with-args 'org-follow-link-hook)))
  8166. (defun org-offer-links-in-entry (&optional nth zero)
  8167. "Offer links in the current entry and follow the selected link.
  8168. If there is only one link, follow it immediately as well.
  8169. If NTH is an integer, immediately pick the NTH link found.
  8170. If ZERO is a string, check also this string for a link, and if
  8171. there is one, offer it as link number zero."
  8172. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8173. "\\(" org-angle-link-re "\\)\\|"
  8174. "\\(" org-plain-link-re "\\)"))
  8175. (cnt ?0)
  8176. (in-emacs (if (integerp nth) nil nth))
  8177. have-zero end links link c)
  8178. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8179. (push (match-string 0 zero) links)
  8180. (setq cnt (1- cnt) have-zero t))
  8181. (save-excursion
  8182. (org-back-to-heading t)
  8183. (setq end (save-excursion (outline-next-heading) (point)))
  8184. (while (re-search-forward re end t)
  8185. (push (match-string 0) links))
  8186. (setq links (org-uniquify (reverse links))))
  8187. (cond
  8188. ((null links)
  8189. (message "No links"))
  8190. ((equal (length links) 1)
  8191. (setq link (list (car links))))
  8192. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8193. (setq link (nth (if have-zero nth (1- nth)) links)))
  8194. (t ; we have to select a link
  8195. (save-excursion
  8196. (save-window-excursion
  8197. (delete-other-windows)
  8198. (with-output-to-temp-buffer "*Select Link*"
  8199. (mapc (lambda (l)
  8200. (if (not (string-match org-bracket-link-regexp l))
  8201. (princ (format "[%c] %s\n" (incf cnt)
  8202. (org-remove-angle-brackets l)))
  8203. (if (match-end 3)
  8204. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8205. (match-string 3 l) (match-string 1 l)))
  8206. (princ (format "[%c] %s\n" (incf cnt)
  8207. (match-string 1 l))))))
  8208. links))
  8209. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8210. (message "Select link to open, RET to open all:")
  8211. (setq c (read-char-exclusive))
  8212. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8213. (when (equal c ?q) (error "Abort"))
  8214. (if (equal c ?\C-m)
  8215. (setq link links)
  8216. (setq nth (- c ?0))
  8217. (if have-zero (setq nth (1+ nth)))
  8218. (unless (and (integerp nth) (>= (length links) nth))
  8219. (error "Invalid link selection"))
  8220. (setq link (list (nth (1- nth) links))))))
  8221. (if link
  8222. (let ((buf (current-buffer)))
  8223. (dolist (l link)
  8224. (org-open-link-from-string l in-emacs buf))
  8225. t)
  8226. nil)))
  8227. ;; Add special file links that specify the way of opening
  8228. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8229. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8230. (defun org-open-file-with-system (path)
  8231. "Open file at PATH using the system way of opeing it."
  8232. (org-open-file path 'system))
  8233. (defun org-open-file-with-emacs (path)
  8234. "Open file at PATH in emacs."
  8235. (org-open-file path 'emacs))
  8236. (defun org-remove-file-link-modifiers ()
  8237. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8238. (goto-char (point-min))
  8239. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8240. (org-if-unprotected
  8241. (replace-match "file:" t t))))
  8242. (eval-after-load "org-exp"
  8243. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8244. 'org-remove-file-link-modifiers))
  8245. ;;;; Time estimates
  8246. (defun org-get-effort (&optional pom)
  8247. "Get the effort estimate for the current entry."
  8248. (org-entry-get pom org-effort-property))
  8249. ;;; File search
  8250. (defvar org-create-file-search-functions nil
  8251. "List of functions to construct the right search string for a file link.
  8252. These functions are called in turn with point at the location to
  8253. which the link should point.
  8254. A function in the hook should first test if it would like to
  8255. handle this file type, for example by checking the major-mode or
  8256. the file extension. If it decides not to handle this file, it
  8257. should just return nil to give other functions a chance. If it
  8258. does handle the file, it must return the search string to be used
  8259. when following the link. The search string will be part of the
  8260. file link, given after a double colon, and `org-open-at-point'
  8261. will automatically search for it. If special measures must be
  8262. taken to make the search successful, another function should be
  8263. added to the companion hook `org-execute-file-search-functions',
  8264. which see.
  8265. A function in this hook may also use `setq' to set the variable
  8266. `description' to provide a suggestion for the descriptive text to
  8267. be used for this link when it gets inserted into an Org-mode
  8268. buffer with \\[org-insert-link].")
  8269. (defvar org-execute-file-search-functions nil
  8270. "List of functions to execute a file search triggered by a link.
  8271. Functions added to this hook must accept a single argument, the
  8272. search string that was part of the file link, the part after the
  8273. double colon. The function must first check if it would like to
  8274. handle this search, for example by checking the major-mode or the
  8275. file extension. If it decides not to handle this search, it
  8276. should just return nil to give other functions a chance. If it
  8277. does handle the search, it must return a non-nil value to keep
  8278. other functions from trying.
  8279. Each function can access the current prefix argument through the
  8280. variable `current-prefix-argument'. Note that a single prefix is
  8281. used to force opening a link in Emacs, so it may be good to only
  8282. use a numeric or double prefix to guide the search function.
  8283. In case this is needed, a function in this hook can also restore
  8284. the window configuration before `org-open-at-point' was called using:
  8285. (set-window-configuration org-window-config-before-follow-link)")
  8286. (defun org-link-search (s &optional type avoid-pos)
  8287. "Search for a link search option.
  8288. If S is surrounded by forward slashes, it is interpreted as a
  8289. regular expression. In org-mode files, this will create an `org-occur'
  8290. sparse tree. In ordinary files, `occur' will be used to list matches.
  8291. If the current buffer is in `dired-mode', grep will be used to search
  8292. in all files. If AVOID-POS is given, ignore matches near that position."
  8293. (let ((case-fold-search t)
  8294. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8295. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8296. (append '(("") (" ") ("\t") ("\n"))
  8297. org-emphasis-alist)
  8298. "\\|") "\\)"))
  8299. (pos (point))
  8300. (pre nil) (post nil)
  8301. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8302. (cond
  8303. ;; First check if there are any special
  8304. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8305. ;; Now try the builtin stuff
  8306. ((and (equal (string-to-char s0) ?#)
  8307. (> (length s0) 1)
  8308. (save-excursion
  8309. (goto-char (point-min))
  8310. (and
  8311. (re-search-forward
  8312. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8313. (setq type 'dedicated
  8314. pos (match-beginning 0))))
  8315. ;; There is an exact target for this
  8316. (goto-char pos)
  8317. (org-back-to-heading t)))
  8318. ((save-excursion
  8319. (goto-char (point-min))
  8320. (and
  8321. (re-search-forward
  8322. (concat "<<" (regexp-quote s0) ">>") nil t)
  8323. (setq type 'dedicated
  8324. pos (match-beginning 0))))
  8325. ;; There is an exact target for this
  8326. (goto-char pos))
  8327. ((and (string-match "^(\\(.*\\))$" s0)
  8328. (save-excursion
  8329. (goto-char (point-min))
  8330. (and
  8331. (re-search-forward
  8332. (concat "[^[]" (regexp-quote
  8333. (format org-coderef-label-format
  8334. (match-string 1 s0))))
  8335. nil t)
  8336. (setq type 'dedicated
  8337. pos (1+ (match-beginning 0))))))
  8338. ;; There is a coderef target for this
  8339. (goto-char pos))
  8340. ((string-match "^/\\(.*\\)/$" s)
  8341. ;; A regular expression
  8342. (cond
  8343. ((org-mode-p)
  8344. (org-occur (match-string 1 s)))
  8345. ;;((eq major-mode 'dired-mode)
  8346. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8347. (t (org-do-occur (match-string 1 s)))))
  8348. (t
  8349. ;; A normal search strings
  8350. (when (equal (string-to-char s) ?*)
  8351. ;; Anchor on headlines, post may include tags.
  8352. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8353. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8354. s (substring s 1)))
  8355. (remove-text-properties
  8356. 0 (length s)
  8357. '(face nil mouse-face nil keymap nil fontified nil) s)
  8358. ;; Make a series of regular expressions to find a match
  8359. (setq words (org-split-string s "[ \n\r\t]+")
  8360. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8361. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8362. "\\)" markers)
  8363. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8364. re2a (concat "[ \t\r\n]" re2a_)
  8365. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8366. re4 (concat "[^a-zA-Z_]" re4_)
  8367. re1 (concat pre re2 post)
  8368. re3 (concat pre (if pre re4_ re4) post)
  8369. re5 (concat pre ".*" re4)
  8370. re2 (concat pre re2)
  8371. re2a (concat pre (if pre re2a_ re2a))
  8372. re4 (concat pre (if pre re4_ re4))
  8373. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8374. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8375. re5 "\\)"
  8376. ))
  8377. (cond
  8378. ((eq type 'org-occur) (org-occur reall))
  8379. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8380. (t (goto-char (point-min))
  8381. (setq type 'fuzzy)
  8382. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8383. (org-search-not-self 1 re1 nil t)
  8384. (org-search-not-self 1 re2 nil t)
  8385. (org-search-not-self 1 re2a nil t)
  8386. (org-search-not-self 1 re3 nil t)
  8387. (org-search-not-self 1 re4 nil t)
  8388. (org-search-not-self 1 re5 nil t)
  8389. )
  8390. (goto-char (match-beginning 1))
  8391. (goto-char pos)
  8392. (error "No match")))))
  8393. (t
  8394. ;; Normal string-search
  8395. (goto-char (point-min))
  8396. (if (search-forward s nil t)
  8397. (goto-char (match-beginning 0))
  8398. (error "No match"))))
  8399. (and (org-mode-p) (org-show-context 'link-search))
  8400. type))
  8401. (defun org-search-not-self (group &rest args)
  8402. "Execute `re-search-forward', but only accept matches that do not
  8403. enclose the position of `org-open-link-marker'."
  8404. (let ((m org-open-link-marker))
  8405. (catch 'exit
  8406. (while (apply 're-search-forward args)
  8407. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8408. (goto-char (match-end group))
  8409. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8410. (> (match-beginning 0) (marker-position m))
  8411. (< (match-end 0) (marker-position m)))
  8412. (save-match-data
  8413. (or (not (org-in-regexp
  8414. org-bracket-link-analytic-regexp 1))
  8415. (not (match-end 4)) ; no description
  8416. (and (<= (match-beginning 4) (point))
  8417. (>= (match-end 4) (point))))))
  8418. (throw 'exit (point))))))))
  8419. (defun org-get-buffer-for-internal-link (buffer)
  8420. "Return a buffer to be used for displaying the link target of internal links."
  8421. (cond
  8422. ((not org-display-internal-link-with-indirect-buffer)
  8423. buffer)
  8424. ((string-match "(Clone)$" (buffer-name buffer))
  8425. (message "Buffer is already a clone, not making another one")
  8426. ;; we also do not modify visibility in this case
  8427. buffer)
  8428. (t ; make a new indirect buffer for displaying the link
  8429. (let* ((bn (buffer-name buffer))
  8430. (ibn (concat bn "(Clone)"))
  8431. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8432. (with-current-buffer ib (org-overview))
  8433. ib))))
  8434. (defun org-do-occur (regexp &optional cleanup)
  8435. "Call the Emacs command `occur'.
  8436. If CLEANUP is non-nil, remove the printout of the regular expression
  8437. in the *Occur* buffer. This is useful if the regex is long and not useful
  8438. to read."
  8439. (occur regexp)
  8440. (when cleanup
  8441. (let ((cwin (selected-window)) win beg end)
  8442. (when (setq win (get-buffer-window "*Occur*"))
  8443. (select-window win))
  8444. (goto-char (point-min))
  8445. (when (re-search-forward "match[a-z]+" nil t)
  8446. (setq beg (match-end 0))
  8447. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8448. (setq end (1- (match-beginning 0)))))
  8449. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8450. (goto-char (point-min))
  8451. (select-window cwin))))
  8452. ;;; The mark ring for links jumps
  8453. (defvar org-mark-ring nil
  8454. "Mark ring for positions before jumps in Org-mode.")
  8455. (defvar org-mark-ring-last-goto nil
  8456. "Last position in the mark ring used to go back.")
  8457. ;; Fill and close the ring
  8458. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8459. (loop for i from 1 to org-mark-ring-length do
  8460. (push (make-marker) org-mark-ring))
  8461. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8462. org-mark-ring)
  8463. (defun org-mark-ring-push (&optional pos buffer)
  8464. "Put the current position or POS into the mark ring and rotate it."
  8465. (interactive)
  8466. (setq pos (or pos (point)))
  8467. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8468. (move-marker (car org-mark-ring)
  8469. (or pos (point))
  8470. (or buffer (current-buffer)))
  8471. (message "%s"
  8472. (substitute-command-keys
  8473. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8474. (defun org-mark-ring-goto (&optional n)
  8475. "Jump to the previous position in the mark ring.
  8476. With prefix arg N, jump back that many stored positions. When
  8477. called several times in succession, walk through the entire ring.
  8478. Org-mode commands jumping to a different position in the current file,
  8479. or to another Org-mode file, automatically push the old position
  8480. onto the ring."
  8481. (interactive "p")
  8482. (let (p m)
  8483. (if (eq last-command this-command)
  8484. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8485. (setq p org-mark-ring))
  8486. (setq org-mark-ring-last-goto p)
  8487. (setq m (car p))
  8488. (switch-to-buffer (marker-buffer m))
  8489. (goto-char m)
  8490. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8491. (defun org-remove-angle-brackets (s)
  8492. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8493. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8494. s)
  8495. (defun org-add-angle-brackets (s)
  8496. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8497. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8498. s)
  8499. (defun org-remove-double-quotes (s)
  8500. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8501. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8502. s)
  8503. ;;; Following specific links
  8504. (defun org-follow-timestamp-link ()
  8505. (cond
  8506. ((org-at-date-range-p t)
  8507. (let ((org-agenda-start-on-weekday)
  8508. (t1 (match-string 1))
  8509. (t2 (match-string 2)))
  8510. (setq t1 (time-to-days (org-time-string-to-time t1))
  8511. t2 (time-to-days (org-time-string-to-time t2)))
  8512. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8513. ((org-at-timestamp-p t)
  8514. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8515. (substring (match-string 1) 0 10)))
  8516. 1))
  8517. (t (error "This should not happen"))))
  8518. ;;; Following file links
  8519. (defvar org-wait nil)
  8520. (defun org-open-file (path &optional in-emacs line search)
  8521. "Open the file at PATH.
  8522. First, this expands any special file name abbreviations. Then the
  8523. configuration variable `org-file-apps' is checked if it contains an
  8524. entry for this file type, and if yes, the corresponding command is launched.
  8525. If no application is found, Emacs simply visits the file.
  8526. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8527. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8528. and to use an external application to visit the file.
  8529. Optional LINE specifies a line to go to, optional SEARCH a string
  8530. to search for. If LINE or SEARCH is given, the file will be
  8531. opened in Emacs, unless an entry from org-file-apps that makes
  8532. use of groups in a regexp matches.
  8533. If the file does not exist, an error is thrown."
  8534. (let* ((file (if (equal path "")
  8535. buffer-file-name
  8536. (substitute-in-file-name (expand-file-name path))))
  8537. (file-apps (append org-file-apps (org-default-apps)))
  8538. (apps (org-remove-if
  8539. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8540. (apps-dlink (org-remove-if-not
  8541. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8542. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8543. (dirp (if remp nil (file-directory-p file)))
  8544. (file (if (and dirp org-open-directory-means-index-dot-org)
  8545. (concat (file-name-as-directory file) "index.org")
  8546. file))
  8547. (a-m-a-p (assq 'auto-mode apps))
  8548. (dfile (downcase file))
  8549. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8550. (link (cond ((and (eq line nil)
  8551. (eq search nil))
  8552. file)
  8553. (line
  8554. (concat file "::" (number-to-string line)))
  8555. (search
  8556. (concat file "::" search))))
  8557. (dlink (downcase link))
  8558. (old-buffer (current-buffer))
  8559. (old-pos (point))
  8560. (old-mode major-mode)
  8561. ext cmd link-match-data)
  8562. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8563. (setq ext (match-string 1 dfile))
  8564. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8565. (setq ext (match-string 1 dfile))))
  8566. (cond
  8567. ((member in-emacs '((16) system))
  8568. (setq cmd (cdr (assoc 'system apps))))
  8569. (in-emacs (setq cmd 'emacs))
  8570. (t
  8571. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8572. (and dirp (cdr (assoc 'directory apps)))
  8573. ; first, try matching against apps-dlink
  8574. ; if we get a match here, store the match data for later
  8575. (let ((match (assoc-default dlink apps-dlink
  8576. 'string-match)))
  8577. (if match
  8578. (progn (setq link-match-data (match-data))
  8579. match)
  8580. (progn (setq in-emacs (or in-emacs line search))
  8581. nil))) ; if we have no match in apps-dlink,
  8582. ; always open the file in emacs if line or search
  8583. ; is given (for backwards compatibility)
  8584. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8585. 'string-match)
  8586. (cdr (assoc ext apps))
  8587. (cdr (assoc t apps))))))
  8588. (when (eq cmd 'system)
  8589. (setq cmd (cdr (assoc 'system apps))))
  8590. (when (eq cmd 'default)
  8591. (setq cmd (cdr (assoc t apps))))
  8592. (when (eq cmd 'mailcap)
  8593. (require 'mailcap)
  8594. (mailcap-parse-mailcaps)
  8595. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8596. (command (mailcap-mime-info mime-type)))
  8597. (if (stringp command)
  8598. (setq cmd command)
  8599. (setq cmd 'emacs))))
  8600. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8601. (not (file-exists-p file))
  8602. (not org-open-non-existing-files))
  8603. (error "No such file: %s" file))
  8604. (cond
  8605. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8606. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8607. (while (string-match "['\"]%s['\"]" cmd)
  8608. (setq cmd (replace-match "%s" t t cmd)))
  8609. (while (string-match "%s" cmd)
  8610. (setq cmd (replace-match
  8611. (save-match-data
  8612. (shell-quote-argument
  8613. (convert-standard-filename file)))
  8614. t t cmd)))
  8615. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8616. (save-match-data
  8617. (let ((match-index 1)
  8618. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8619. (set-match-data link-match-data)
  8620. (while (<= match-index number-of-groups)
  8621. (let ((regex (concat "%" (number-to-string match-index)))
  8622. (replace-with (match-string match-index dlink)))
  8623. (while (string-match regex cmd)
  8624. (setq cmd (replace-match replace-with t t cmd))))
  8625. (setq match-index (+ match-index 1)))))
  8626. (save-window-excursion
  8627. (start-process-shell-command cmd nil cmd)
  8628. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8629. ))
  8630. ((or (stringp cmd)
  8631. (eq cmd 'emacs))
  8632. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8633. (widen)
  8634. (if line (org-goto-line line)
  8635. (if search (org-link-search search))))
  8636. ((consp cmd)
  8637. (let ((file (convert-standard-filename file)))
  8638. (save-match-data
  8639. (set-match-data link-match-data)
  8640. (eval cmd))))
  8641. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8642. (and (org-mode-p) (eq old-mode 'org-mode)
  8643. (or (not (equal old-buffer (current-buffer)))
  8644. (not (equal old-pos (point))))
  8645. (org-mark-ring-push old-pos old-buffer))))
  8646. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8647. "This function returns non-nil if `entry' uses a regular
  8648. expression which should be matched against the whole link by
  8649. org-open-file.
  8650. It assumes that is the case when the entry uses a regular
  8651. expression which has at least one grouping construct and the
  8652. action is either a lisp form or a command string containing
  8653. '%1', i.e. using at least one subexpression match as a
  8654. parameter."
  8655. (let ((selector (car entry))
  8656. (action (cdr entry)))
  8657. (if (stringp selector)
  8658. (and (> (regexp-opt-depth selector) 0)
  8659. (or (and (stringp action)
  8660. (string-match "%[0-9]" action))
  8661. (consp action)))
  8662. nil)))
  8663. (defun org-default-apps ()
  8664. "Return the default applications for this operating system."
  8665. (cond
  8666. ((eq system-type 'darwin)
  8667. org-file-apps-defaults-macosx)
  8668. ((eq system-type 'windows-nt)
  8669. org-file-apps-defaults-windowsnt)
  8670. (t org-file-apps-defaults-gnu)))
  8671. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8672. "Convert extensions to regular expressions in the cars of LIST.
  8673. Also, weed out any non-string entries, because the return value is used
  8674. only for regexp matching.
  8675. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8676. point to the symbol `emacs', indicating that the file should
  8677. be opened in Emacs."
  8678. (append
  8679. (delq nil
  8680. (mapcar (lambda (x)
  8681. (if (not (stringp (car x)))
  8682. nil
  8683. (if (string-match "\\W" (car x))
  8684. x
  8685. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8686. list))
  8687. (if add-auto-mode
  8688. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8689. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8690. (defun org-file-remote-p (file)
  8691. "Test whether FILE specifies a location on a remote system.
  8692. Return non-nil if the location is indeed remote.
  8693. For example, the filename \"/user@host:/foo\" specifies a location
  8694. on the system \"/user@host:\"."
  8695. (cond ((fboundp 'file-remote-p)
  8696. (file-remote-p file))
  8697. ((fboundp 'tramp-handle-file-remote-p)
  8698. (tramp-handle-file-remote-p file))
  8699. ((and (boundp 'ange-ftp-name-format)
  8700. (string-match (car ange-ftp-name-format) file))
  8701. t)
  8702. (t nil)))
  8703. ;;;; Refiling
  8704. (defun org-get-org-file ()
  8705. "Read a filename, with default directory `org-directory'."
  8706. (let ((default (or org-default-notes-file remember-data-file)))
  8707. (read-file-name (format "File name [%s]: " default)
  8708. (file-name-as-directory org-directory)
  8709. default)))
  8710. (defun org-notes-order-reversed-p ()
  8711. "Check if the current file should receive notes in reversed order."
  8712. (cond
  8713. ((not org-reverse-note-order) nil)
  8714. ((eq t org-reverse-note-order) t)
  8715. ((not (listp org-reverse-note-order)) nil)
  8716. (t (catch 'exit
  8717. (let ((all org-reverse-note-order)
  8718. entry)
  8719. (while (setq entry (pop all))
  8720. (if (string-match (car entry) buffer-file-name)
  8721. (throw 'exit (cdr entry))))
  8722. nil)))))
  8723. (defvar org-refile-target-table nil
  8724. "The list of refile targets, created by `org-refile'.")
  8725. (defvar org-agenda-new-buffers nil
  8726. "Buffers created to visit agenda files.")
  8727. (defvar org-refile-cache nil
  8728. "Cache for refile targets.")
  8729. (defvar org-refile-markers nil
  8730. "All the markers used for caching refile locations.")
  8731. (defun org-refile-marker (pos)
  8732. "Get a new refile marker, but only if caching is in use."
  8733. (if (not org-refile-use-cache)
  8734. pos
  8735. (let ((m (make-marker)))
  8736. (move-marker m pos)
  8737. (push m org-refile-markers)
  8738. m)))
  8739. (defun org-refile-cache-clear ()
  8740. "Clear the refile cache and disable all the markers."
  8741. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8742. (setq org-refile-markers nil)
  8743. (setq org-refile-cache nil)
  8744. (message "Refile cache has been cleared"))
  8745. (defun org-refile-cache-check-set (set)
  8746. "Check if all the markers in the cache still have live buffers."
  8747. (let (marker)
  8748. (catch 'exit
  8749. (while (and set (setq marker (nth 3 (pop set))))
  8750. ;; if org-refile-use-outline-path is 'file, marker may be nil
  8751. (when (and marker (null (marker-buffer marker)))
  8752. (message "not found") (sit-for 3)
  8753. (throw 'exit nil)))
  8754. t)))
  8755. (defun org-refile-cache-put (set &rest identifiers)
  8756. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8757. (let* ((key (sha1 (prin1-to-string identifiers)))
  8758. (entry (assoc key org-refile-cache)))
  8759. (if entry
  8760. (setcdr entry set)
  8761. (push (cons key set) org-refile-cache))))
  8762. (defun org-refile-cache-get (&rest identifiers)
  8763. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8764. (cond
  8765. ((not org-refile-cache) nil)
  8766. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8767. (t
  8768. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8769. org-refile-cache))))
  8770. (and set (org-refile-cache-check-set set) set)))))
  8771. (defun org-get-refile-targets (&optional default-buffer)
  8772. "Produce a table with refile targets."
  8773. (let ((case-fold-search nil)
  8774. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8775. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8776. targets tgs txt re files f desc descre fast-path-p level pos0)
  8777. (message "Getting targets...")
  8778. (with-current-buffer (or default-buffer (current-buffer))
  8779. (while (setq entry (pop entries))
  8780. (setq files (car entry) desc (cdr entry))
  8781. (setq fast-path-p nil)
  8782. (cond
  8783. ((null files) (setq files (list (current-buffer))))
  8784. ((eq files 'org-agenda-files)
  8785. (setq files (org-agenda-files 'unrestricted)))
  8786. ((and (symbolp files) (fboundp files))
  8787. (setq files (funcall files)))
  8788. ((and (symbolp files) (boundp files))
  8789. (setq files (symbol-value files))))
  8790. (if (stringp files) (setq files (list files)))
  8791. (cond
  8792. ((eq (car desc) :tag)
  8793. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8794. ((eq (car desc) :todo)
  8795. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8796. ((eq (car desc) :regexp)
  8797. (setq descre (cdr desc)))
  8798. ((eq (car desc) :level)
  8799. (setq descre (concat "^\\*\\{" (number-to-string
  8800. (if org-odd-levels-only
  8801. (1- (* 2 (cdr desc)))
  8802. (cdr desc)))
  8803. "\\}[ \t]")))
  8804. ((eq (car desc) :maxlevel)
  8805. (setq fast-path-p t)
  8806. (setq descre (concat "^\\*\\{1," (number-to-string
  8807. (if org-odd-levels-only
  8808. (1- (* 2 (cdr desc)))
  8809. (cdr desc)))
  8810. "\\}[ \t]")))
  8811. (t (error "Bad refiling target description %s" desc)))
  8812. (while (setq f (pop files))
  8813. (with-current-buffer
  8814. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8815. (or
  8816. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  8817. (progn
  8818. (if (bufferp f) (setq f (buffer-file-name
  8819. (buffer-base-buffer f))))
  8820. (setq f (and f (expand-file-name f)))
  8821. (if (eq org-refile-use-outline-path 'file)
  8822. (push (list (file-name-nondirectory f) f nil nil) tgs))
  8823. (save-excursion
  8824. (save-restriction
  8825. (widen)
  8826. (goto-char (point-min))
  8827. (while (re-search-forward descre nil t)
  8828. (goto-char (setq pos0 (point-at-bol)))
  8829. (catch 'next
  8830. (when org-refile-target-verify-function
  8831. (save-match-data
  8832. (or (funcall org-refile-target-verify-function)
  8833. (throw 'next t))))
  8834. (when (looking-at org-complex-heading-regexp)
  8835. (setq level (org-reduced-level
  8836. (- (match-end 1) (match-beginning 1)))
  8837. txt (org-link-display-format (match-string 4))
  8838. re (concat "^" (regexp-quote
  8839. (buffer-substring
  8840. (match-beginning 1)
  8841. (match-end 4)))))
  8842. (if (match-end 5) (setq re (concat
  8843. re "[ \t]+"
  8844. (regexp-quote
  8845. (match-string 5)))))
  8846. (setq re (concat re "[ \t]*$"))
  8847. (when org-refile-use-outline-path
  8848. (setq txt (mapconcat
  8849. 'org-protect-slash
  8850. (append
  8851. (if (eq org-refile-use-outline-path
  8852. 'file)
  8853. (list (file-name-nondirectory
  8854. (buffer-file-name
  8855. (buffer-base-buffer))))
  8856. (if (eq org-refile-use-outline-path
  8857. 'full-file-path)
  8858. (list (buffer-file-name
  8859. (buffer-base-buffer)))))
  8860. (org-get-outline-path fast-path-p
  8861. level txt)
  8862. (list txt))
  8863. "/")))
  8864. (push (list txt f re (org-refile-marker (point)))
  8865. tgs)))
  8866. (when (= (point) pos0)
  8867. ;; verification function has not moved point
  8868. (goto-char (point-at-eol))))))))
  8869. (when org-refile-use-cache
  8870. (org-refile-cache-put tgs (buffer-file-name) descre))
  8871. (setq targets (append tgs targets))
  8872. ))))
  8873. (message "Getting targets...done")
  8874. (nreverse targets)))
  8875. (defun org-protect-slash (s)
  8876. (while (string-match "/" s)
  8877. (setq s (replace-match "\\" t t s)))
  8878. s)
  8879. (defvar org-olpa (make-vector 20 nil))
  8880. (defun org-get-outline-path (&optional fastp level heading)
  8881. "Return the outline path to the current entry, as a list.
  8882. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  8883. routine which makes outline path derivations for an entire file,
  8884. avoiding backtracing. Refile target collection makes use of that."
  8885. (if fastp
  8886. (progn
  8887. (if (> level 19)
  8888. (error "Outline path failure, more than 19 levels."))
  8889. (loop for i from level upto 19 do
  8890. (aset org-olpa i nil))
  8891. (prog1
  8892. (delq nil (append org-olpa nil))
  8893. (aset org-olpa level heading)))
  8894. (let (rtn case-fold-search)
  8895. (save-excursion
  8896. (save-restriction
  8897. (widen)
  8898. (while (org-up-heading-safe)
  8899. (when (looking-at org-complex-heading-regexp)
  8900. (push (org-match-string-no-properties 4) rtn)))
  8901. rtn)))))
  8902. (defun org-format-outline-path (path &optional width prefix)
  8903. "Format the outlie path PATH for display.
  8904. Width is the maximum number of characters that is available.
  8905. Prefix is a prefix to be included in the returned string,
  8906. such as the file name."
  8907. (setq width (or width 79))
  8908. (if prefix (setq width (- width (length prefix))))
  8909. (if (not path)
  8910. (or prefix "")
  8911. (let* ((nsteps (length path))
  8912. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8913. (maxwidth (if (<= total-width width)
  8914. 10000 ;; everything fits
  8915. ;; we need to shorten the level headings
  8916. (/ (- width nsteps) nsteps)))
  8917. (org-odd-levels-only nil)
  8918. (n 0)
  8919. (total (1+ (length prefix))))
  8920. (setq maxwidth (max maxwidth 10))
  8921. (concat prefix
  8922. (mapconcat
  8923. (lambda (h)
  8924. (setq n (1+ n))
  8925. (if (and (= n nsteps) (< maxwidth 10000))
  8926. (setq maxwidth (- total-width total)))
  8927. (if (< (length h) maxwidth)
  8928. (progn (setq total (+ total (length h) 1)) h)
  8929. (setq h (substring h 0 (- maxwidth 2))
  8930. total (+ total maxwidth 1))
  8931. (if (string-match "[ \t]+\\'" h)
  8932. (setq h (substring h 0 (match-beginning 0))))
  8933. (setq h (concat h "..")))
  8934. (org-add-props h nil 'face
  8935. (nth (% (1- n) org-n-level-faces)
  8936. org-level-faces))
  8937. h)
  8938. path "/")))))
  8939. (defun org-display-outline-path (&optional file current)
  8940. "Display the current outline path in the echo area."
  8941. (interactive "P")
  8942. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8943. (case-fold-search nil)
  8944. (path (and (org-mode-p) (org-get-outline-path))))
  8945. (if current (setq path (append path
  8946. (save-excursion
  8947. (org-back-to-heading t)
  8948. (if (looking-at org-complex-heading-regexp)
  8949. (list (match-string 4)))))))
  8950. (message "%s"
  8951. (org-format-outline-path
  8952. path
  8953. (1- (frame-width))
  8954. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8955. (defvar org-refile-history nil
  8956. "History for refiling operations.")
  8957. (defvar org-after-refile-insert-hook nil
  8958. "Hook run after `org-refile' has inserted its stuff at the new location.
  8959. Note that this is still *before* the stuff will be removed from
  8960. the *old* location.")
  8961. (defun org-refile (&optional goto default-buffer rfloc)
  8962. "Move the entry at point to another heading.
  8963. The list of target headings is compiled using the information in
  8964. `org-refile-targets', which see. This list is created before each use
  8965. and will therefore always be up-to-date.
  8966. At the target location, the entry is filed as a subitem of the target heading.
  8967. Depending on `org-reverse-note-order', the new subitem will either be the
  8968. first or the last subitem.
  8969. If there is an active region, all entries in that region will be moved.
  8970. However, the region must fulfil the requirement that the first heading
  8971. is the first one sets the top-level of the moved text - at most siblings
  8972. below it are allowed.
  8973. With prefix arg GOTO, the command will only visit the target location,
  8974. not actually move anything.
  8975. With a double prefix `C-u C-u', go to the location where the last refiling
  8976. operation has put the subtree.
  8977. With a prefix argument of `2', refile to the running clock.
  8978. RFLOC can be a refile location obtained in a different way.
  8979. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  8980. If you are using target caching (see `org-refile-use-cache'),
  8981. You have to clear the target cache in order to find new targets.
  8982. This can be done with a 0 prefix: `C-0 C-c C-w'"
  8983. (interactive "P")
  8984. (if (member goto '(0 (64)))
  8985. (org-refile-cache-clear)
  8986. (let* ((cbuf (current-buffer))
  8987. (regionp (org-region-active-p))
  8988. (region-start (and regionp (region-beginning)))
  8989. (region-end (and regionp (region-end)))
  8990. (region-length (and regionp (- region-end region-start)))
  8991. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8992. pos it nbuf file re level reversed)
  8993. (setq last-command nil)
  8994. (when regionp
  8995. (goto-char region-start)
  8996. (or (bolp) (goto-char (point-at-bol)))
  8997. (setq region-start (point))
  8998. (unless (org-kill-is-subtree-p
  8999. (buffer-substring region-start region-end))
  9000. (error "The region is not a (sequence of) subtree(s)")))
  9001. (if (equal goto '(16))
  9002. (org-refile-goto-last-stored)
  9003. (when (or
  9004. (and (equal goto 2)
  9005. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9006. (prog1
  9007. (setq it (list (or org-clock-heading "running clock")
  9008. (buffer-file-name
  9009. (marker-buffer org-clock-hd-marker))
  9010. ""
  9011. (marker-position org-clock-hd-marker)))
  9012. (setq goto nil)))
  9013. (setq it (or rfloc
  9014. (save-excursion
  9015. (org-refile-get-location
  9016. (if goto "Goto: " "Refile to: ") default-buffer
  9017. org-refile-allow-creating-parent-nodes)))))
  9018. (setq file (nth 1 it)
  9019. re (nth 2 it)
  9020. pos (nth 3 it))
  9021. (if (and (not goto)
  9022. pos
  9023. (equal (buffer-file-name) file)
  9024. (if regionp
  9025. (and (>= pos region-start)
  9026. (<= pos region-end))
  9027. (and (>= pos (point))
  9028. (< pos (save-excursion
  9029. (org-end-of-subtree t t))))))
  9030. (error "Cannot refile to position inside the tree or region"))
  9031. (setq nbuf (or (find-buffer-visiting file)
  9032. (find-file-noselect file)))
  9033. (if goto
  9034. (progn
  9035. (switch-to-buffer nbuf)
  9036. (goto-char pos)
  9037. (org-show-context 'org-goto))
  9038. (if regionp
  9039. (progn
  9040. (org-kill-new (buffer-substring region-start region-end))
  9041. (org-save-markers-in-region region-start region-end))
  9042. (org-copy-subtree 1 nil t))
  9043. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9044. (find-file-noselect file)))
  9045. (setq reversed (org-notes-order-reversed-p))
  9046. (save-excursion
  9047. (save-restriction
  9048. (widen)
  9049. (if pos
  9050. (progn
  9051. (goto-char pos)
  9052. (looking-at outline-regexp)
  9053. (setq level (org-get-valid-level (funcall outline-level) 1))
  9054. (goto-char
  9055. (if reversed
  9056. (or (outline-next-heading) (point-max))
  9057. (or (save-excursion (org-get-next-sibling))
  9058. (org-end-of-subtree t t)
  9059. (point-max)))))
  9060. (setq level 1)
  9061. (if (not reversed)
  9062. (goto-char (point-max))
  9063. (goto-char (point-min))
  9064. (or (outline-next-heading) (goto-char (point-max)))))
  9065. (if (not (bolp)) (newline))
  9066. (org-paste-subtree level)
  9067. (when org-log-refile
  9068. (org-add-log-setup 'refile nil nil 'findpos
  9069. org-log-refile)
  9070. (unless (eq org-log-refile 'note)
  9071. (save-excursion (org-add-log-note))))
  9072. (and org-auto-align-tags (org-set-tags nil t))
  9073. (bookmark-set "org-refile-last-stored")
  9074. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9075. (run-hooks 'org-after-refile-insert-hook))))
  9076. (if regionp
  9077. (delete-region (point) (+ (point) region-length))
  9078. (org-cut-subtree))
  9079. (when (featurep 'org-inlinetask)
  9080. (org-inlinetask-remove-END-maybe))
  9081. (setq org-markers-to-move nil)
  9082. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9083. (defun org-refile-goto-last-stored ()
  9084. "Go to the location where the last refile was stored."
  9085. (interactive)
  9086. (bookmark-jump "org-refile-last-stored")
  9087. (message "This is the location of the last refile"))
  9088. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9089. "Prompt the user for a refile location, using PROMPT."
  9090. (let ((org-refile-targets org-refile-targets)
  9091. (org-refile-use-outline-path org-refile-use-outline-path))
  9092. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  9093. (unless org-refile-target-table
  9094. (error "No refile targets"))
  9095. (let* ((cbuf (current-buffer))
  9096. (partial-completion-mode nil)
  9097. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9098. (cfunc (if (and org-refile-use-outline-path
  9099. org-outline-path-complete-in-steps)
  9100. 'org-olpath-completing-read
  9101. 'org-icompleting-read))
  9102. (extra (if org-refile-use-outline-path "/" ""))
  9103. (filename (and cfn (expand-file-name cfn)))
  9104. (tbl (mapcar
  9105. (lambda (x)
  9106. (if (and (not (member org-refile-use-outline-path
  9107. '(file full-file-path)))
  9108. (not (equal filename (nth 1 x))))
  9109. (cons (concat (car x) extra " ("
  9110. (file-name-nondirectory (nth 1 x)) ")")
  9111. (cdr x))
  9112. (cons (concat (car x) extra) (cdr x))))
  9113. org-refile-target-table))
  9114. (completion-ignore-case t)
  9115. pa answ parent-target child parent old-hist)
  9116. (setq old-hist org-refile-history)
  9117. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9118. nil 'org-refile-history))
  9119. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9120. (if pa
  9121. (progn
  9122. (when (or (not org-refile-history)
  9123. (not (eq old-hist org-refile-history))
  9124. (not (equal (car pa) (car org-refile-history))))
  9125. (setq org-refile-history
  9126. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9127. org-refile-history
  9128. (cdr org-refile-history))))
  9129. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9130. (pop org-refile-history)))
  9131. pa)
  9132. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9133. (progn
  9134. (setq parent (match-string 1 answ)
  9135. child (match-string 2 answ))
  9136. (setq parent-target (or (assoc parent tbl)
  9137. (assoc (concat parent "/") tbl)))
  9138. (when (and parent-target
  9139. (or (eq new-nodes t)
  9140. (and (eq new-nodes 'confirm)
  9141. (y-or-n-p (format "Create new node \"%s\"? "
  9142. child)))))
  9143. (org-refile-new-child parent-target child)))
  9144. (error "Invalid target location")))))
  9145. (defun org-refile-new-child (parent-target child)
  9146. "Use refile target PARENT-TARGET to add new CHILD below it."
  9147. (unless parent-target
  9148. (error "Cannot find parent for new node"))
  9149. (let ((file (nth 1 parent-target))
  9150. (pos (nth 3 parent-target))
  9151. level)
  9152. (with-current-buffer (or (find-buffer-visiting file)
  9153. (find-file-noselect file))
  9154. (save-excursion
  9155. (save-restriction
  9156. (widen)
  9157. (if pos
  9158. (goto-char pos)
  9159. (goto-char (point-max))
  9160. (if (not (bolp)) (newline)))
  9161. (when (looking-at outline-regexp)
  9162. (setq level (funcall outline-level))
  9163. (org-end-of-subtree t t))
  9164. (org-back-over-empty-lines)
  9165. (insert "\n" (make-string
  9166. (if pos (org-get-valid-level level 1) 1) ?*)
  9167. " " child "\n")
  9168. (beginning-of-line 0)
  9169. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9170. (defun org-olpath-completing-read (prompt collection &rest args)
  9171. "Read an outline path like a file name."
  9172. (let ((thetable collection)
  9173. (org-completion-use-ido nil) ; does not work with ido.
  9174. (org-completion-use-iswitchb nil)) ; or iswitchb
  9175. (apply
  9176. 'org-icompleting-read prompt
  9177. (lambda (string predicate &optional flag)
  9178. (let (rtn r f (l (length string)))
  9179. (cond
  9180. ((eq flag nil)
  9181. ;; try completion
  9182. (try-completion string thetable))
  9183. ((eq flag t)
  9184. ;; all-completions
  9185. (setq rtn (all-completions string thetable predicate))
  9186. (mapcar
  9187. (lambda (x)
  9188. (setq r (substring x l))
  9189. (if (string-match " ([^)]*)$" x)
  9190. (setq f (match-string 0 x))
  9191. (setq f ""))
  9192. (if (string-match "/" r)
  9193. (concat string (substring r 0 (match-end 0)) f)
  9194. x))
  9195. rtn))
  9196. ((eq flag 'lambda)
  9197. ;; exact match?
  9198. (assoc string thetable)))
  9199. ))
  9200. args)))
  9201. ;;;; Dynamic blocks
  9202. (defun org-find-dblock (name)
  9203. "Find the first dynamic block with name NAME in the buffer.
  9204. If not found, stay at current position and return nil."
  9205. (let (pos)
  9206. (save-excursion
  9207. (goto-char (point-min))
  9208. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9209. nil t)
  9210. (match-beginning 0))))
  9211. (if pos (goto-char pos))
  9212. pos))
  9213. (defconst org-dblock-start-re
  9214. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9215. "Matches the start line of a dynamic block, with parameters.")
  9216. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9217. "Matches the end of a dynamic block.")
  9218. (defun org-create-dblock (plist)
  9219. "Create a dynamic block section, with parameters taken from PLIST.
  9220. PLIST must contain a :name entry which is used as name of the block."
  9221. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9222. (end-of-line 1)
  9223. (newline))
  9224. (let ((col (current-column))
  9225. (name (plist-get plist :name)))
  9226. (insert "#+BEGIN: " name)
  9227. (while plist
  9228. (if (eq (car plist) :name)
  9229. (setq plist (cddr plist))
  9230. (insert " " (prin1-to-string (pop plist)))))
  9231. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9232. (beginning-of-line -2)))
  9233. (defun org-prepare-dblock ()
  9234. "Prepare dynamic block for refresh.
  9235. This empties the block, puts the cursor at the insert position and returns
  9236. the property list including an extra property :name with the block name."
  9237. (unless (looking-at org-dblock-start-re)
  9238. (error "Not at a dynamic block"))
  9239. (let* ((begdel (1+ (match-end 0)))
  9240. (name (org-no-properties (match-string 1)))
  9241. (params (append (list :name name)
  9242. (read (concat "(" (match-string 3) ")")))))
  9243. (save-excursion
  9244. (beginning-of-line 1)
  9245. (skip-chars-forward " \t")
  9246. (setq params (plist-put params :indentation-column (current-column))))
  9247. (unless (re-search-forward org-dblock-end-re nil t)
  9248. (error "Dynamic block not terminated"))
  9249. (setq params
  9250. (append params
  9251. (list :content (buffer-substring
  9252. begdel (match-beginning 0)))))
  9253. (delete-region begdel (match-beginning 0))
  9254. (goto-char begdel)
  9255. (open-line 1)
  9256. params))
  9257. (defun org-map-dblocks (&optional command)
  9258. "Apply COMMAND to all dynamic blocks in the current buffer.
  9259. If COMMAND is not given, use `org-update-dblock'."
  9260. (let ((cmd (or command 'org-update-dblock)))
  9261. (save-excursion
  9262. (goto-char (point-min))
  9263. (while (re-search-forward org-dblock-start-re nil t)
  9264. (goto-char (match-beginning 0))
  9265. (save-excursion
  9266. (condition-case nil
  9267. (funcall cmd)
  9268. (error (message "Error during update of dynamic block"))))
  9269. (unless (re-search-forward org-dblock-end-re nil t)
  9270. (error "Dynamic block not terminated"))))))
  9271. (defun org-dblock-update (&optional arg)
  9272. "User command for updating dynamic blocks.
  9273. Update the dynamic block at point. With prefix ARG, update all dynamic
  9274. blocks in the buffer."
  9275. (interactive "P")
  9276. (if arg
  9277. (org-update-all-dblocks)
  9278. (or (looking-at org-dblock-start-re)
  9279. (org-beginning-of-dblock))
  9280. (org-update-dblock)))
  9281. (defun org-update-dblock ()
  9282. "Update the dynamic block at point
  9283. This means to empty the block, parse for parameters and then call
  9284. the correct writing function."
  9285. (save-window-excursion
  9286. (let* ((pos (point))
  9287. (line (org-current-line))
  9288. (params (org-prepare-dblock))
  9289. (name (plist-get params :name))
  9290. (indent (plist-get params :indentation-column))
  9291. (cmd (intern (concat "org-dblock-write:" name))))
  9292. (message "Updating dynamic block `%s' at line %d..." name line)
  9293. (funcall cmd params)
  9294. (message "Updating dynamic block `%s' at line %d...done" name line)
  9295. (goto-char pos)
  9296. (when (and indent (> indent 0))
  9297. (setq indent (make-string indent ?\ ))
  9298. (save-excursion
  9299. (org-beginning-of-dblock)
  9300. (forward-line 1)
  9301. (while (not (looking-at org-dblock-end-re))
  9302. (insert indent)
  9303. (beginning-of-line 2))
  9304. (when (looking-at org-dblock-end-re)
  9305. (and (looking-at "[ \t]+")
  9306. (replace-match ""))
  9307. (insert indent)))))))
  9308. (defun org-beginning-of-dblock ()
  9309. "Find the beginning of the dynamic block at point.
  9310. Error if there is no such block at point."
  9311. (let ((pos (point))
  9312. beg)
  9313. (end-of-line 1)
  9314. (if (and (re-search-backward org-dblock-start-re nil t)
  9315. (setq beg (match-beginning 0))
  9316. (re-search-forward org-dblock-end-re nil t)
  9317. (> (match-end 0) pos))
  9318. (goto-char beg)
  9319. (goto-char pos)
  9320. (error "Not in a dynamic block"))))
  9321. (defun org-update-all-dblocks ()
  9322. "Update all dynamic blocks in the buffer.
  9323. This function can be used in a hook."
  9324. (when (org-mode-p)
  9325. (org-map-dblocks 'org-update-dblock)))
  9326. ;;;; Completion
  9327. (defconst org-additional-option-like-keywords
  9328. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  9329. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  9330. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9331. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  9332. "BEGIN:" "END:"
  9333. "ORGTBL" "TBLFM:" "TBLNAME:"
  9334. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9335. "BEGIN_QUOTE" "END_QUOTE"
  9336. "BEGIN_VERSE" "END_VERSE"
  9337. "BEGIN_CENTER" "END_CENTER"
  9338. "BEGIN_SRC" "END_SRC"
  9339. "CATEGORY" "COLUMNS"
  9340. "CAPTION" "LABEL"
  9341. "SETUPFILE"
  9342. "BIND"
  9343. "MACRO"))
  9344. (defcustom org-structure-template-alist
  9345. '(
  9346. ("s" "#+begin_src ?\n\n#+end_src"
  9347. "<src lang=\"?\">\n\n</src>")
  9348. ("e" "#+begin_example\n?\n#+end_example"
  9349. "<example>\n?\n</example>")
  9350. ("q" "#+begin_quote\n?\n#+end_quote"
  9351. "<quote>\n?\n</quote>")
  9352. ("v" "#+begin_verse\n?\n#+end_verse"
  9353. "<verse>\n?\n/verse>")
  9354. ("c" "#+begin_center\n?\n#+end_center"
  9355. "<center>\n?\n/center>")
  9356. ("l" "#+begin_latex\n?\n#+end_latex"
  9357. "<literal style=\"latex\">\n?\n</literal>")
  9358. ("L" "#+latex: "
  9359. "<literal style=\"latex\">?</literal>")
  9360. ("h" "#+begin_html\n?\n#+end_html"
  9361. "<literal style=\"html\">\n?\n</literal>")
  9362. ("H" "#+html: "
  9363. "<literal style=\"html\">?</literal>")
  9364. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9365. ("A" "#+ascii: ")
  9366. ("i" "#+include %file ?"
  9367. "<include file=%file markup=\"?\">")
  9368. )
  9369. "Structure completion elements.
  9370. This is a list of abbreviation keys and values. The value gets inserted
  9371. if you type `<' followed by the key and then press the completion key,
  9372. usually `M-TAB'. %file will be replaced by a file name after prompting
  9373. for the file using completion.
  9374. There are two templates for each key, the first uses the original Org syntax,
  9375. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9376. the default when the /org-mtags.el/ module has been loaded. See also the
  9377. variable `org-mtags-prefer-muse-templates'.
  9378. This is an experimental feature, it is undecided if it is going to stay in."
  9379. :group 'org-completion
  9380. :type '(repeat
  9381. (string :tag "Key")
  9382. (string :tag "Template")
  9383. (string :tag "Muse Template")))
  9384. (defun org-try-structure-completion ()
  9385. "Try to complete a structure template before point.
  9386. This looks for strings like \"<e\" on an otherwise empty line and
  9387. expands them."
  9388. (let ((l (buffer-substring (point-at-bol) (point)))
  9389. a)
  9390. (when (and (looking-at "[ \t]*$")
  9391. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9392. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9393. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9394. (match-beginning 1)) a)
  9395. t)))
  9396. (defun org-complete-expand-structure-template (start cell)
  9397. "Expand a structure template."
  9398. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9399. (rpl (nth (if musep 2 1) cell))
  9400. (ind ""))
  9401. (delete-region start (point))
  9402. (when (string-match "\\`#\\+" rpl)
  9403. (cond
  9404. ((bolp))
  9405. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9406. (setq ind (buffer-substring (point-at-bol) (point))))
  9407. (t (newline))))
  9408. (setq start (point))
  9409. (if (string-match "%file" rpl)
  9410. (setq rpl (replace-match
  9411. (concat
  9412. "\""
  9413. (save-match-data
  9414. (abbreviate-file-name (read-file-name "Include file: ")))
  9415. "\"")
  9416. t t rpl)))
  9417. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9418. (concat "\n" ind)))
  9419. (insert rpl)
  9420. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9421. (defun org-complete (&optional arg)
  9422. "Perform completion on word at point.
  9423. At the beginning of a headline, this completes TODO keywords as given in
  9424. `org-todo-keywords'.
  9425. If the current word is preceded by a backslash, completes the TeX symbols
  9426. that are supported for HTML support.
  9427. If the current word is preceded by \"#+\", completes special words for
  9428. setting file options.
  9429. In the line after \"#+STARTUP:, complete valid keywords.\"
  9430. At all other locations, this simply calls the value of
  9431. `org-completion-fallback-command'."
  9432. (interactive "P")
  9433. (org-without-partial-completion
  9434. (catch 'exit
  9435. (let* ((a nil)
  9436. (end (point))
  9437. (beg1 (save-excursion
  9438. (skip-chars-backward (org-re "[:alnum:]_@"))
  9439. (point)))
  9440. (beg (save-excursion
  9441. (skip-chars-backward "a-zA-Z0-9_:$")
  9442. (point)))
  9443. (confirm (lambda (x) (stringp (car x))))
  9444. (searchhead (equal (char-before beg) ?*))
  9445. (struct
  9446. (when (and (member (char-before beg1) '(?. ?<))
  9447. (setq a (assoc (buffer-substring beg1 (point))
  9448. org-structure-template-alist)))
  9449. (org-complete-expand-structure-template (1- beg1) a)
  9450. (throw 'exit t)))
  9451. (tag (and (equal (char-before beg1) ?:)
  9452. (equal (char-after (point-at-bol)) ?*)))
  9453. (prop (and (equal (char-before beg1) ?:)
  9454. (not (equal (char-after (point-at-bol)) ?*))))
  9455. (texp (equal (char-before beg) ?\\))
  9456. (link (equal (char-before beg) ?\[))
  9457. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9458. beg)
  9459. "#+"))
  9460. (startup (string-match "^#\\+STARTUP:.*"
  9461. (buffer-substring (point-at-bol) (point))))
  9462. (completion-ignore-case opt)
  9463. (type nil)
  9464. (tbl nil)
  9465. (table (cond
  9466. (opt
  9467. (setq type :opt)
  9468. (require 'org-exp)
  9469. (append
  9470. (delq nil
  9471. (mapcar
  9472. (lambda (x)
  9473. (if (string-match
  9474. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9475. (cons (match-string 2 x)
  9476. (match-string 1 x))))
  9477. (org-split-string (org-get-current-options) "\n")))
  9478. (mapcar 'list org-additional-option-like-keywords)))
  9479. (startup
  9480. (setq type :startup)
  9481. org-startup-options)
  9482. (link (append org-link-abbrev-alist-local
  9483. org-link-abbrev-alist))
  9484. (texp
  9485. (setq type :tex)
  9486. (append org-entities-user org-entities))
  9487. ((string-match "\\`\\*+[ \t]+\\'"
  9488. (buffer-substring (point-at-bol) beg))
  9489. (setq type :todo)
  9490. (mapcar 'list org-todo-keywords-1))
  9491. (searchhead
  9492. (setq type :searchhead)
  9493. (save-excursion
  9494. (goto-char (point-min))
  9495. (while (re-search-forward org-todo-line-regexp nil t)
  9496. (push (list
  9497. (org-make-org-heading-search-string
  9498. (match-string 3) t))
  9499. tbl)))
  9500. tbl)
  9501. (tag (setq type :tag beg beg1)
  9502. (or org-tag-alist (org-get-buffer-tags)))
  9503. (prop (setq type :prop beg beg1)
  9504. (mapcar 'list (org-buffer-property-keys nil t t)))
  9505. (t (progn
  9506. (call-interactively org-completion-fallback-command)
  9507. (throw 'exit nil)))))
  9508. (pattern (buffer-substring-no-properties beg end))
  9509. (completion (try-completion pattern table confirm)))
  9510. (cond ((eq completion t)
  9511. (if (not (assoc (upcase pattern) table))
  9512. (message "Already complete")
  9513. (if (and (equal type :opt)
  9514. (not (member (car (assoc (upcase pattern) table))
  9515. org-additional-option-like-keywords)))
  9516. (insert (substring (cdr (assoc (upcase pattern) table))
  9517. (length pattern)))
  9518. (if (memq type '(:tag :prop)) (insert ":")))))
  9519. ((null completion)
  9520. (message "Can't find completion for \"%s\"" pattern)
  9521. (ding))
  9522. ((not (string= pattern completion))
  9523. (delete-region beg end)
  9524. (if (string-match " +$" completion)
  9525. (setq completion (replace-match "" t t completion)))
  9526. (insert completion)
  9527. (if (get-buffer-window "*Completions*")
  9528. (delete-window (get-buffer-window "*Completions*")))
  9529. (if (assoc completion table)
  9530. (if (eq type :todo) (insert " ")
  9531. (if (memq type '(:tag :prop)) (insert ":"))))
  9532. (if (and (equal type :opt) (assoc completion table))
  9533. (message "%s" (substitute-command-keys
  9534. "Press \\[org-complete] again to insert example settings"))))
  9535. (t
  9536. (message "Making completion list...")
  9537. (let ((list (sort (all-completions pattern table confirm)
  9538. 'string<)))
  9539. (with-output-to-temp-buffer "*Completions*"
  9540. (condition-case nil
  9541. ;; Protection needed for XEmacs and emacs 21
  9542. (display-completion-list list pattern)
  9543. (error (display-completion-list list)))))
  9544. (message "Making completion list...%s" "done")))))))
  9545. ;;;; TODO, DEADLINE, Comments
  9546. (defun org-toggle-comment ()
  9547. "Change the COMMENT state of an entry."
  9548. (interactive)
  9549. (save-excursion
  9550. (org-back-to-heading)
  9551. (let (case-fold-search)
  9552. (if (looking-at (concat outline-regexp
  9553. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9554. (replace-match "" t t nil 1)
  9555. (if (looking-at outline-regexp)
  9556. (progn
  9557. (goto-char (match-end 0))
  9558. (insert org-comment-string " ")))))))
  9559. (defvar org-last-todo-state-is-todo nil
  9560. "This is non-nil when the last TODO state change led to a TODO state.
  9561. If the last change removed the TODO tag or switched to DONE, then
  9562. this is nil.")
  9563. (defvar org-setting-tags nil) ; dynamically skipped
  9564. (defun org-parse-local-options (string var)
  9565. "Parse STRING for startup setting relevant for variable VAR."
  9566. (let ((rtn (symbol-value var))
  9567. e opts)
  9568. (save-match-data
  9569. (if (or (not string) (not (string-match "\\S-" string)))
  9570. rtn
  9571. (setq opts (delq nil (mapcar (lambda (x)
  9572. (setq e (assoc x org-startup-options))
  9573. (if (eq (nth 1 e) var) e nil))
  9574. (org-split-string string "[ \t]+"))))
  9575. (if (not opts)
  9576. rtn
  9577. (setq rtn nil)
  9578. (while (setq e (pop opts))
  9579. (if (not (nth 3 e))
  9580. (setq rtn (nth 2 e))
  9581. (if (not (listp rtn)) (setq rtn nil))
  9582. (push (nth 2 e) rtn)))
  9583. rtn)))))
  9584. (defvar org-todo-setup-filter-hook nil
  9585. "Hook for functions that pre-filter todo specs.
  9586. Each function takes a todo spec and returns either `nil' or the spec
  9587. transformed into canonical form." )
  9588. (defvar org-todo-get-default-hook nil
  9589. "Hook for functions that get a default item for todo.
  9590. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9591. `nil' or a string to be used for the todo mark." )
  9592. (defvar org-agenda-headline-snapshot-before-repeat)
  9593. (defun org-todo (&optional arg)
  9594. "Change the TODO state of an item.
  9595. The state of an item is given by a keyword at the start of the heading,
  9596. like
  9597. *** TODO Write paper
  9598. *** DONE Call mom
  9599. The different keywords are specified in the variable `org-todo-keywords'.
  9600. By default the available states are \"TODO\" and \"DONE\".
  9601. So for this example: when the item starts with TODO, it is changed to DONE.
  9602. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9603. DONE are present, add TODO at the beginning of the heading.
  9604. With C-u prefix arg, use completion to determine the new state.
  9605. With numeric prefix arg, switch to that state.
  9606. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9607. With a triple C-u prefix, circumvent any state blocking.
  9608. For calling through lisp, arg is also interpreted in the following way:
  9609. 'none -> empty state
  9610. \"\"(empty string) -> switch to empty state
  9611. 'done -> switch to DONE
  9612. 'nextset -> switch to the next set of keywords
  9613. 'previousset -> switch to the previous set of keywords
  9614. \"WAITING\" -> switch to the specified keyword, but only if it
  9615. really is a member of `org-todo-keywords'."
  9616. (interactive "P")
  9617. (if (equal arg '(16)) (setq arg 'nextset))
  9618. (let ((org-blocker-hook org-blocker-hook)
  9619. (case-fold-search nil))
  9620. (when (equal arg '(64))
  9621. (setq arg nil org-blocker-hook nil))
  9622. (when (and org-blocker-hook
  9623. (or org-inhibit-blocking
  9624. (org-entry-get nil "NOBLOCKING")))
  9625. (setq org-blocker-hook nil))
  9626. (save-excursion
  9627. (catch 'exit
  9628. (org-back-to-heading t)
  9629. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9630. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9631. (looking-at " *"))
  9632. (let* ((match-data (match-data))
  9633. (startpos (point-at-bol))
  9634. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9635. (org-log-done org-log-done)
  9636. (org-log-repeat org-log-repeat)
  9637. (org-todo-log-states org-todo-log-states)
  9638. (this (match-string 1))
  9639. (hl-pos (match-beginning 0))
  9640. (head (org-get-todo-sequence-head this))
  9641. (ass (assoc head org-todo-kwd-alist))
  9642. (interpret (nth 1 ass))
  9643. (done-word (nth 3 ass))
  9644. (final-done-word (nth 4 ass))
  9645. (last-state (or this ""))
  9646. (completion-ignore-case t)
  9647. (member (member this org-todo-keywords-1))
  9648. (tail (cdr member))
  9649. (state (cond
  9650. ((and org-todo-key-trigger
  9651. (or (and (equal arg '(4))
  9652. (eq org-use-fast-todo-selection 'prefix))
  9653. (and (not arg) org-use-fast-todo-selection
  9654. (not (eq org-use-fast-todo-selection
  9655. 'prefix)))))
  9656. ;; Use fast selection
  9657. (org-fast-todo-selection))
  9658. ((and (equal arg '(4))
  9659. (or (not org-use-fast-todo-selection)
  9660. (not org-todo-key-trigger)))
  9661. ;; Read a state with completion
  9662. (org-icompleting-read
  9663. "State: " (mapcar (lambda(x) (list x))
  9664. org-todo-keywords-1)
  9665. nil t))
  9666. ((eq arg 'right)
  9667. (if this
  9668. (if tail (car tail) nil)
  9669. (car org-todo-keywords-1)))
  9670. ((eq arg 'left)
  9671. (if (equal member org-todo-keywords-1)
  9672. nil
  9673. (if this
  9674. (nth (- (length org-todo-keywords-1)
  9675. (length tail) 2)
  9676. org-todo-keywords-1)
  9677. (org-last org-todo-keywords-1))))
  9678. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9679. (setq arg nil))) ; hack to fall back to cycling
  9680. (arg
  9681. ;; user or caller requests a specific state
  9682. (cond
  9683. ((equal arg "") nil)
  9684. ((eq arg 'none) nil)
  9685. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9686. ((eq arg 'nextset)
  9687. (or (car (cdr (member head org-todo-heads)))
  9688. (car org-todo-heads)))
  9689. ((eq arg 'previousset)
  9690. (let ((org-todo-heads (reverse org-todo-heads)))
  9691. (or (car (cdr (member head org-todo-heads)))
  9692. (car org-todo-heads))))
  9693. ((car (member arg org-todo-keywords-1)))
  9694. ((stringp arg)
  9695. (error "State `%s' not valid in this file" arg))
  9696. ((nth (1- (prefix-numeric-value arg))
  9697. org-todo-keywords-1))))
  9698. ((null member) (or head (car org-todo-keywords-1)))
  9699. ((equal this final-done-word) nil) ;; -> make empty
  9700. ((null tail) nil) ;; -> first entry
  9701. ((memq interpret '(type priority))
  9702. (if (eq this-command last-command)
  9703. (car tail)
  9704. (if (> (length tail) 0)
  9705. (or done-word (car org-done-keywords))
  9706. nil)))
  9707. (t
  9708. (car tail))))
  9709. (state (or
  9710. (run-hook-with-args-until-success
  9711. 'org-todo-get-default-hook state last-state)
  9712. state))
  9713. (next (if state (concat " " state " ") " "))
  9714. (change-plist (list :type 'todo-state-change :from this :to state
  9715. :position startpos))
  9716. dolog now-done-p)
  9717. (when org-blocker-hook
  9718. (setq org-last-todo-state-is-todo
  9719. (not (member this org-done-keywords)))
  9720. (unless (save-excursion
  9721. (save-match-data
  9722. (run-hook-with-args-until-failure
  9723. 'org-blocker-hook change-plist)))
  9724. (if (interactive-p)
  9725. (error "TODO state change from %s to %s blocked" this state)
  9726. ;; fail silently
  9727. (message "TODO state change from %s to %s blocked" this state)
  9728. (throw 'exit nil))))
  9729. (store-match-data match-data)
  9730. (replace-match next t t)
  9731. (unless (pos-visible-in-window-p hl-pos)
  9732. (message "TODO state changed to %s" (org-trim next)))
  9733. (unless head
  9734. (setq head (org-get-todo-sequence-head state)
  9735. ass (assoc head org-todo-kwd-alist)
  9736. interpret (nth 1 ass)
  9737. done-word (nth 3 ass)
  9738. final-done-word (nth 4 ass)))
  9739. (when (memq arg '(nextset previousset))
  9740. (message "Keyword-Set %d/%d: %s"
  9741. (- (length org-todo-sets) -1
  9742. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9743. (length org-todo-sets)
  9744. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9745. (setq org-last-todo-state-is-todo
  9746. (not (member state org-done-keywords)))
  9747. (setq now-done-p (and (member state org-done-keywords)
  9748. (not (member this org-done-keywords))))
  9749. (and logging (org-local-logging logging))
  9750. (when (and (or org-todo-log-states org-log-done)
  9751. (not (eq org-inhibit-logging t))
  9752. (not (memq arg '(nextset previousset))))
  9753. ;; we need to look at recording a time and note
  9754. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9755. (nth 2 (assoc this org-todo-log-states))))
  9756. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9757. (setq dolog 'time))
  9758. (when (and state
  9759. (member state org-not-done-keywords)
  9760. (not (member this org-not-done-keywords)))
  9761. ;; This is now a todo state and was not one before
  9762. ;; If there was a CLOSED time stamp, get rid of it.
  9763. (org-add-planning-info nil nil 'closed))
  9764. (when (and now-done-p org-log-done)
  9765. ;; It is now done, and it was not done before
  9766. (org-add-planning-info 'closed (org-current-time))
  9767. (if (and (not dolog) (eq 'note org-log-done))
  9768. (org-add-log-setup 'done state this 'findpos 'note)))
  9769. (when (and state dolog)
  9770. ;; This is a non-nil state, and we need to log it
  9771. (org-add-log-setup 'state state this 'findpos dolog)))
  9772. ;; Fixup tag positioning
  9773. (org-todo-trigger-tag-changes state)
  9774. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9775. (when org-provide-todo-statistics
  9776. (org-update-parent-todo-statistics))
  9777. (run-hooks 'org-after-todo-state-change-hook)
  9778. (if (and arg (not (member state org-done-keywords)))
  9779. (setq head (org-get-todo-sequence-head state)))
  9780. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9781. ;; Do we need to trigger a repeat?
  9782. (when now-done-p
  9783. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9784. ;; This is for the agenda, take a snapshot of the headline.
  9785. (save-match-data
  9786. (setq org-agenda-headline-snapshot-before-repeat
  9787. (org-get-heading))))
  9788. (org-auto-repeat-maybe state))
  9789. ;; Fixup cursor location if close to the keyword
  9790. (if (and (outline-on-heading-p)
  9791. (not (bolp))
  9792. (save-excursion (beginning-of-line 1)
  9793. (looking-at org-todo-line-regexp))
  9794. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9795. (progn
  9796. (goto-char (or (match-end 2) (match-end 1)))
  9797. (and (looking-at " ") (just-one-space))))
  9798. (when org-trigger-hook
  9799. (save-excursion
  9800. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9801. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9802. "Block turning an entry into a TODO, using the hierarchy.
  9803. This checks whether the current task should be blocked from state
  9804. changes. Such blocking occurs when:
  9805. 1. The task has children which are not all in a completed state.
  9806. 2. A task has a parent with the property :ORDERED:, and there
  9807. are siblings prior to the current task with incomplete
  9808. status.
  9809. 3. The parent of the task is blocked because it has siblings that should
  9810. be done first, or is child of a block grandparent TODO entry."
  9811. (if (not org-enforce-todo-dependencies)
  9812. t ; if locally turned off don't block
  9813. (catch 'dont-block
  9814. ;; If this is not a todo state change, or if this entry is already DONE,
  9815. ;; do not block
  9816. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9817. (member (plist-get change-plist :from)
  9818. (cons 'done org-done-keywords))
  9819. (member (plist-get change-plist :to)
  9820. (cons 'todo org-not-done-keywords))
  9821. (not (plist-get change-plist :to)))
  9822. (throw 'dont-block t))
  9823. ;; If this task has children, and any are undone, it's blocked
  9824. (save-excursion
  9825. (org-back-to-heading t)
  9826. (let ((this-level (funcall outline-level)))
  9827. (outline-next-heading)
  9828. (let ((child-level (funcall outline-level)))
  9829. (while (and (not (eobp))
  9830. (> child-level this-level))
  9831. ;; this todo has children, check whether they are all
  9832. ;; completed
  9833. (if (and (not (org-entry-is-done-p))
  9834. (org-entry-is-todo-p))
  9835. (throw 'dont-block nil))
  9836. (outline-next-heading)
  9837. (setq child-level (funcall outline-level))))))
  9838. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9839. ;; any previous siblings are undone, it's blocked
  9840. (save-excursion
  9841. (org-back-to-heading t)
  9842. (let* ((pos (point))
  9843. (parent-pos (and (org-up-heading-safe) (point))))
  9844. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9845. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9846. (forward-line 1)
  9847. (re-search-forward org-not-done-heading-regexp pos t))
  9848. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9849. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9850. (while t
  9851. (goto-char parent-pos)
  9852. (if (not (looking-at org-not-done-heading-regexp))
  9853. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9854. (setq pos (point))
  9855. (setq parent-pos (and (org-up-heading-safe) (point)))
  9856. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9857. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9858. (forward-line 1)
  9859. (re-search-forward org-not-done-heading-regexp pos t))
  9860. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9861. (defcustom org-track-ordered-property-with-tag nil
  9862. "Should the ORDERED property also be shown as a tag?
  9863. The ORDERED property decides if an entry should require subtasks to be
  9864. completed in sequence. Since a property is not very visible, setting
  9865. this option means that toggling the ORDERED property with the command
  9866. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9867. not relevant for the behavior, but it makes things more visible.
  9868. Note that toggling the tag with tags commands will not change the property
  9869. and therefore not influence behavior!
  9870. This can be t, meaning the tag ORDERED should be used, It can also be a
  9871. string to select a different tag for this task."
  9872. :group 'org-todo
  9873. :type '(choice
  9874. (const :tag "No tracking" nil)
  9875. (const :tag "Track with ORDERED tag" t)
  9876. (string :tag "Use other tag")))
  9877. (defun org-toggle-ordered-property ()
  9878. "Toggle the ORDERED property of the current entry.
  9879. For better visibility, you can track the value of this property with a tag.
  9880. See variable `org-track-ordered-property-with-tag'."
  9881. (interactive)
  9882. (let* ((t1 org-track-ordered-property-with-tag)
  9883. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9884. (save-excursion
  9885. (org-back-to-heading)
  9886. (if (org-entry-get nil "ORDERED")
  9887. (progn
  9888. (org-delete-property "ORDERED")
  9889. (and tag (org-toggle-tag tag 'off))
  9890. (message "Subtasks can be completed in arbitrary order"))
  9891. (org-entry-put nil "ORDERED" "t")
  9892. (and tag (org-toggle-tag tag 'on))
  9893. (message "Subtasks must be completed in sequence")))))
  9894. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9895. (defun org-block-todo-from-checkboxes (change-plist)
  9896. "Block turning an entry into a TODO, using checkboxes.
  9897. This checks whether the current task should be blocked from state
  9898. changes because there are unchecked boxes in this entry."
  9899. (if (not org-enforce-todo-checkbox-dependencies)
  9900. t ; if locally turned off don't block
  9901. (catch 'dont-block
  9902. ;; If this is not a todo state change, or if this entry is already DONE,
  9903. ;; do not block
  9904. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9905. (member (plist-get change-plist :from)
  9906. (cons 'done org-done-keywords))
  9907. (member (plist-get change-plist :to)
  9908. (cons 'todo org-not-done-keywords))
  9909. (not (plist-get change-plist :to)))
  9910. (throw 'dont-block t))
  9911. ;; If this task has checkboxes that are not checked, it's blocked
  9912. (save-excursion
  9913. (org-back-to-heading t)
  9914. (let ((beg (point)) end)
  9915. (outline-next-heading)
  9916. (setq end (point))
  9917. (goto-char beg)
  9918. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9919. end t)
  9920. (progn
  9921. (if (boundp 'org-blocked-by-checkboxes)
  9922. (setq org-blocked-by-checkboxes t))
  9923. (throw 'dont-block nil)))))
  9924. t))) ; do not block
  9925. (defun org-entry-blocked-p ()
  9926. "Is the current entry blocked?"
  9927. (if (org-entry-get nil "NOBLOCKING")
  9928. nil ;; Never block this entry
  9929. (not
  9930. (run-hook-with-args-until-failure
  9931. 'org-blocker-hook
  9932. (list :type 'todo-state-change
  9933. :position (point)
  9934. :from 'todo
  9935. :to 'done)))))
  9936. (defun org-update-statistics-cookies (all)
  9937. "Update the statistics cookie, either from TODO or from checkboxes.
  9938. This should be called with the cursor in a line with a statistics cookie."
  9939. (interactive "P")
  9940. (if all
  9941. (progn
  9942. (org-update-checkbox-count 'all)
  9943. (org-map-entries 'org-update-parent-todo-statistics))
  9944. (if (not (org-on-heading-p))
  9945. (org-update-checkbox-count)
  9946. (let ((pos (move-marker (make-marker) (point)))
  9947. end l1 l2)
  9948. (ignore-errors (org-back-to-heading t))
  9949. (if (not (org-on-heading-p))
  9950. (org-update-checkbox-count)
  9951. (setq l1 (org-outline-level))
  9952. (setq end (save-excursion
  9953. (outline-next-heading)
  9954. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9955. (point)))
  9956. (if (and (save-excursion
  9957. (re-search-forward
  9958. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9959. (not (save-excursion (re-search-forward
  9960. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9961. (org-update-checkbox-count)
  9962. (if (and l2 (> l2 l1))
  9963. (progn
  9964. (goto-char end)
  9965. (org-update-parent-todo-statistics))
  9966. (goto-char pos)
  9967. (beginning-of-line 1)
  9968. (while (re-search-forward
  9969. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9970. (point-at-eol) t)
  9971. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9972. (goto-char pos)
  9973. (move-marker pos nil)))))
  9974. (defvar org-entry-property-inherited-from) ;; defined below
  9975. (defun org-update-parent-todo-statistics ()
  9976. "Update any statistics cookie in the parent of the current headline.
  9977. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9978. the entire subtree and this will travel up the hierarchy and update
  9979. statistics everywhere."
  9980. (interactive)
  9981. (let* ((lim 0) prop
  9982. (recursive (or (not org-hierarchical-todo-statistics)
  9983. (string-match
  9984. "\\<recursive\\>"
  9985. (or (setq prop (org-entry-get
  9986. nil "COOKIE_DATA" 'inherit)) ""))))
  9987. (lim (or (and prop (marker-position
  9988. org-entry-property-inherited-from))
  9989. lim))
  9990. (first t)
  9991. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9992. level ltoggle l1 new ndel
  9993. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9994. (catch 'exit
  9995. (save-excursion
  9996. (beginning-of-line 1)
  9997. (if (org-at-heading-p)
  9998. (setq ltoggle (funcall outline-level))
  9999. (error "This should not happen"))
  10000. (while (and (setq level (org-up-heading-safe))
  10001. (or recursive first)
  10002. (>= (point) lim))
  10003. (setq first nil cookie-present nil)
  10004. (unless (and level
  10005. (not (string-match
  10006. "\\<checkbox\\>"
  10007. (downcase
  10008. (or (org-entry-get
  10009. nil "COOKIE_DATA")
  10010. "")))))
  10011. (throw 'exit nil))
  10012. (while (re-search-forward box-re (point-at-eol) t)
  10013. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10014. (setq is-percent (match-end 2))
  10015. (save-match-data
  10016. (unless (outline-next-heading) (throw 'exit nil))
  10017. (while (and (looking-at org-complex-heading-regexp)
  10018. (> (setq l1 (length (match-string 1))) level))
  10019. (setq kwd (and (or recursive (= l1 ltoggle))
  10020. (match-string 2)))
  10021. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10022. (and (listp org-provide-todo-statistics)
  10023. (or (member kwd org-provide-todo-statistics)
  10024. (member kwd org-done-keywords))))
  10025. (setq cnt-all (1+ cnt-all))
  10026. (if (eq org-provide-todo-statistics t)
  10027. (and kwd (setq cnt-all (1+ cnt-all)))))
  10028. (and (member kwd org-done-keywords)
  10029. (setq cnt-done (1+ cnt-done)))
  10030. (outline-next-heading)))
  10031. (setq new
  10032. (if is-percent
  10033. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10034. (format "[%d/%d]" cnt-done cnt-all))
  10035. ndel (- (match-end 0) (match-beginning 0)))
  10036. (goto-char (match-beginning 0))
  10037. (insert new)
  10038. (delete-region (point) (+ (point) ndel)))
  10039. (when cookie-present
  10040. (run-hook-with-args 'org-after-todo-statistics-hook
  10041. cnt-done (- cnt-all cnt-done))))))
  10042. (run-hooks 'org-todo-statistics-hook)))
  10043. (defvar org-after-todo-statistics-hook nil
  10044. "Hook that is called after a TODO statistics cookie has been updated.
  10045. Each function is called with two arguments: the number of not-done entries
  10046. and the number of done entries.
  10047. For example, the following function, when added to this hook, will switch
  10048. an entry to DONE when all children are done, and back to TODO when new
  10049. entries are set to a TODO status. Note that this hook is only called
  10050. when there is a statistics cookie in the headline!
  10051. (defun org-summary-todo (n-done n-not-done)
  10052. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10053. (let (org-log-done org-log-states) ; turn off logging
  10054. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10055. ")
  10056. (defvar org-todo-statistics-hook nil
  10057. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10058. This hook runs even if there is no statistics cookie present, in which case
  10059. `org-after-todo-statistics-hook' would not run.")
  10060. (defun org-todo-trigger-tag-changes (state)
  10061. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10062. (let ((l org-todo-state-tags-triggers)
  10063. changes)
  10064. (when (or (not state) (equal state ""))
  10065. (setq changes (append changes (cdr (assoc "" l)))))
  10066. (when (and (stringp state) (> (length state) 0))
  10067. (setq changes (append changes (cdr (assoc state l)))))
  10068. (when (member state org-not-done-keywords)
  10069. (setq changes (append changes (cdr (assoc 'todo l)))))
  10070. (when (member state org-done-keywords)
  10071. (setq changes (append changes (cdr (assoc 'done l)))))
  10072. (dolist (c changes)
  10073. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10074. (defun org-local-logging (value)
  10075. "Get logging settings from a property VALUE."
  10076. (let* (words w a)
  10077. ;; directly set the variables, they are already local.
  10078. (setq org-log-done nil
  10079. org-log-repeat nil
  10080. org-todo-log-states nil)
  10081. (setq words (org-split-string value))
  10082. (while (setq w (pop words))
  10083. (cond
  10084. ((setq a (assoc w org-startup-options))
  10085. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10086. (set (nth 1 a) (nth 2 a))))
  10087. ((setq a (org-extract-log-state-settings w))
  10088. (and (member (car a) org-todo-keywords-1)
  10089. (push a org-todo-log-states)))))))
  10090. (defun org-get-todo-sequence-head (kwd)
  10091. "Return the head of the TODO sequence to which KWD belongs.
  10092. If KWD is not set, check if there is a text property remembering the
  10093. right sequence."
  10094. (let (p)
  10095. (cond
  10096. ((not kwd)
  10097. (or (get-text-property (point-at-bol) 'org-todo-head)
  10098. (progn
  10099. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10100. nil (point-at-eol)))
  10101. (get-text-property p 'org-todo-head))))
  10102. ((not (member kwd org-todo-keywords-1))
  10103. (car org-todo-keywords-1))
  10104. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10105. (defun org-fast-todo-selection ()
  10106. "Fast TODO keyword selection with single keys.
  10107. Returns the new TODO keyword, or nil if no state change should occur."
  10108. (let* ((fulltable org-todo-key-alist)
  10109. (done-keywords org-done-keywords) ;; needed for the faces.
  10110. (maxlen (apply 'max (mapcar
  10111. (lambda (x)
  10112. (if (stringp (car x)) (string-width (car x)) 0))
  10113. fulltable)))
  10114. (expert nil)
  10115. (fwidth (+ maxlen 3 1 3))
  10116. (ncol (/ (- (window-width) 4) fwidth))
  10117. tg cnt e c tbl
  10118. groups ingroup)
  10119. (save-excursion
  10120. (save-window-excursion
  10121. (if expert
  10122. (set-buffer (get-buffer-create " *Org todo*"))
  10123. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10124. (erase-buffer)
  10125. (org-set-local 'org-done-keywords done-keywords)
  10126. (setq tbl fulltable cnt 0)
  10127. (while (setq e (pop tbl))
  10128. (cond
  10129. ((equal e '(:startgroup))
  10130. (push '() groups) (setq ingroup t)
  10131. (when (not (= cnt 0))
  10132. (setq cnt 0)
  10133. (insert "\n"))
  10134. (insert "{ "))
  10135. ((equal e '(:endgroup))
  10136. (setq ingroup nil cnt 0)
  10137. (insert "}\n"))
  10138. ((equal e '(:newline))
  10139. (when (not (= cnt 0))
  10140. (setq cnt 0)
  10141. (insert "\n")
  10142. (setq e (car tbl))
  10143. (while (equal (car tbl) '(:newline))
  10144. (insert "\n")
  10145. (setq tbl (cdr tbl)))))
  10146. (t
  10147. (setq tg (car e) c (cdr e))
  10148. (if ingroup (push tg (car groups)))
  10149. (setq tg (org-add-props tg nil 'face
  10150. (org-get-todo-face tg)))
  10151. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10152. (insert "[" c "] " tg (make-string
  10153. (- fwidth 4 (length tg)) ?\ ))
  10154. (when (= (setq cnt (1+ cnt)) ncol)
  10155. (insert "\n")
  10156. (if ingroup (insert " "))
  10157. (setq cnt 0)))))
  10158. (insert "\n")
  10159. (goto-char (point-min))
  10160. (if (not expert) (org-fit-window-to-buffer))
  10161. (message "[a-z..]:Set [SPC]:clear")
  10162. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10163. (cond
  10164. ((or (= c ?\C-g)
  10165. (and (= c ?q) (not (rassoc c fulltable))))
  10166. (setq quit-flag t))
  10167. ((= c ?\ ) nil)
  10168. ((setq e (rassoc c fulltable) tg (car e))
  10169. tg)
  10170. (t (setq quit-flag t)))))))
  10171. (defun org-entry-is-todo-p ()
  10172. (member (org-get-todo-state) org-not-done-keywords))
  10173. (defun org-entry-is-done-p ()
  10174. (member (org-get-todo-state) org-done-keywords))
  10175. (defun org-get-todo-state ()
  10176. (save-excursion
  10177. (org-back-to-heading t)
  10178. (and (looking-at org-todo-line-regexp)
  10179. (match-end 2)
  10180. (match-string 2))))
  10181. (defun org-at-date-range-p (&optional inactive-ok)
  10182. "Is the cursor inside a date range?"
  10183. (interactive)
  10184. (save-excursion
  10185. (catch 'exit
  10186. (let ((pos (point)))
  10187. (skip-chars-backward "^[<\r\n")
  10188. (skip-chars-backward "<[")
  10189. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10190. (>= (match-end 0) pos)
  10191. (throw 'exit t))
  10192. (skip-chars-backward "^<[\r\n")
  10193. (skip-chars-backward "<[")
  10194. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10195. (>= (match-end 0) pos)
  10196. (throw 'exit t)))
  10197. nil)))
  10198. (defun org-get-repeat (&optional tagline)
  10199. "Check if there is a deadline/schedule with repeater in this entry."
  10200. (save-match-data
  10201. (save-excursion
  10202. (org-back-to-heading t)
  10203. (and (re-search-forward (if tagline
  10204. (concat tagline "\\s-*" org-repeat-re)
  10205. org-repeat-re)
  10206. (org-entry-end-position) t)
  10207. (match-string-no-properties 1)))))
  10208. (defvar org-last-changed-timestamp)
  10209. (defvar org-last-inserted-timestamp)
  10210. (defvar org-log-post-message)
  10211. (defvar org-log-note-purpose)
  10212. (defvar org-log-note-how)
  10213. (defvar org-log-note-extra)
  10214. (defun org-auto-repeat-maybe (done-word)
  10215. "Check if the current headline contains a repeated deadline/schedule.
  10216. If yes, set TODO state back to what it was and change the base date
  10217. of repeating deadline/scheduled time stamps to new date.
  10218. This function is run automatically after each state change to a DONE state."
  10219. ;; last-state is dynamically scoped into this function
  10220. (let* ((repeat (org-get-repeat))
  10221. (aa (assoc last-state org-todo-kwd-alist))
  10222. (interpret (nth 1 aa))
  10223. (head (nth 2 aa))
  10224. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10225. (msg "Entry repeats: ")
  10226. (org-log-done nil)
  10227. (org-todo-log-states nil)
  10228. (nshiftmax 10) (nshift 0)
  10229. re type n what ts time to-state)
  10230. (when repeat
  10231. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10232. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10233. org-todo-repeat-to-state))
  10234. (unless (and to-state (member to-state org-todo-keywords-1))
  10235. (setq to-state (if (eq interpret 'type) last-state head)))
  10236. (org-todo to-state)
  10237. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10238. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10239. (org-time-stamp-format t t))))
  10240. (when org-log-repeat
  10241. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10242. (memq 'org-add-log-note post-command-hook))
  10243. ;; OK, we are already setup for some record
  10244. (if (eq org-log-repeat 'note)
  10245. ;; make sure we take a note, not only a time stamp
  10246. (setq org-log-note-how 'note))
  10247. ;; Set up for taking a record
  10248. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10249. last-state
  10250. 'findpos org-log-repeat)))
  10251. (org-back-to-heading t)
  10252. (org-add-planning-info nil nil 'closed)
  10253. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10254. org-deadline-time-regexp "\\)\\|\\("
  10255. org-ts-regexp "\\)"))
  10256. (while (re-search-forward
  10257. re (save-excursion (outline-next-heading) (point)) t)
  10258. (setq type (if (match-end 1) org-scheduled-string
  10259. (if (match-end 3) org-deadline-string "Plain:"))
  10260. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10261. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10262. (setq n (string-to-number (match-string 2 ts))
  10263. what (match-string 3 ts))
  10264. (if (equal what "w") (setq n (* n 7) what "d"))
  10265. ;; Preparation, see if we need to modify the start date for the change
  10266. (when (match-end 1)
  10267. (setq time (save-match-data (org-time-string-to-time ts)))
  10268. (cond
  10269. ((equal (match-string 1 ts) ".")
  10270. ;; Shift starting date to today
  10271. (org-timestamp-change
  10272. (- (time-to-days (current-time)) (time-to-days time))
  10273. 'day))
  10274. ((equal (match-string 1 ts) "+")
  10275. (while (or (= nshift 0)
  10276. (<= (time-to-days time) (time-to-days (current-time))))
  10277. (when (= (incf nshift) nshiftmax)
  10278. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10279. (error "Abort")))
  10280. (org-timestamp-change n (cdr (assoc what whata)))
  10281. (org-at-timestamp-p t)
  10282. (setq ts (match-string 1))
  10283. (setq time (save-match-data (org-time-string-to-time ts))))
  10284. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10285. ;; rematch, so that we have everything in place for the real shift
  10286. (org-at-timestamp-p t)
  10287. (setq ts (match-string 1))
  10288. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10289. (org-timestamp-change n (cdr (assoc what whata)))
  10290. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10291. (setq org-log-post-message msg)
  10292. (message "%s" msg))))
  10293. (defun org-show-todo-tree (arg)
  10294. "Make a compact tree which shows all headlines marked with TODO.
  10295. The tree will show the lines where the regexp matches, and all higher
  10296. headlines above the match.
  10297. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10298. With a numeric prefix N, construct a sparse tree for the Nth element
  10299. of `org-todo-keywords-1'."
  10300. (interactive "P")
  10301. (let ((case-fold-search nil)
  10302. (kwd-re
  10303. (cond ((null arg) org-not-done-regexp)
  10304. ((equal arg '(4))
  10305. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10306. (mapcar 'list org-todo-keywords-1))))
  10307. (concat "\\("
  10308. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10309. "\\)\\>")))
  10310. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10311. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10312. org-todo-keywords-1)))
  10313. (t (error "Invalid prefix argument: %s" arg)))))
  10314. (message "%d TODO entries found"
  10315. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10316. (defun org-deadline (&optional remove time)
  10317. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10318. With argument REMOVE, remove any deadline from the item.
  10319. When TIME is set, it should be an internal time specification, and the
  10320. scheduling will use the corresponding date."
  10321. (interactive "P")
  10322. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10323. (repeater (and old-date
  10324. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10325. (match-string 1 old-date))))
  10326. (if remove
  10327. (progn
  10328. (when (and old-date org-log-redeadline)
  10329. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10330. org-log-redeadline))
  10331. (org-remove-timestamp-with-keyword org-deadline-string)
  10332. (message "Item no longer has a deadline."))
  10333. (org-add-planning-info 'deadline time 'closed)
  10334. (when (and old-date org-log-redeadline
  10335. (not (equal old-date
  10336. (substring org-last-inserted-timestamp 1 -1))))
  10337. (org-add-log-setup 'redeadline nil old-date 'findpos
  10338. org-log-redeadline))
  10339. (when repeater
  10340. (save-excursion
  10341. (org-back-to-heading t)
  10342. (when (re-search-forward (concat org-deadline-string " "
  10343. org-last-inserted-timestamp)
  10344. (save-excursion
  10345. (outline-next-heading) (point)) t)
  10346. (goto-char (1- (match-end 0)))
  10347. (insert " " repeater)
  10348. (setq org-last-inserted-timestamp
  10349. (concat (substring org-last-inserted-timestamp 0 -1)
  10350. " " repeater
  10351. (substring org-last-inserted-timestamp -1))))))
  10352. (message "Deadline on %s" org-last-inserted-timestamp))))
  10353. (defun org-schedule (&optional remove time)
  10354. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10355. With argument REMOVE, remove any scheduling date from the item.
  10356. When TIME is set, it should be an internal time specification, and the
  10357. scheduling will use the corresponding date."
  10358. (interactive "P")
  10359. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10360. (repeater (and old-date
  10361. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10362. (match-string 1 old-date))))
  10363. (if remove
  10364. (progn
  10365. (when (and old-date org-log-reschedule)
  10366. (org-add-log-setup 'delschedule nil old-date 'findpos
  10367. org-log-reschedule))
  10368. (org-remove-timestamp-with-keyword org-scheduled-string)
  10369. (message "Item is no longer scheduled."))
  10370. (org-add-planning-info 'scheduled time 'closed)
  10371. (when (and old-date org-log-reschedule
  10372. (not (equal old-date
  10373. (substring org-last-inserted-timestamp 1 -1))))
  10374. (org-add-log-setup 'reschedule nil old-date 'findpos
  10375. org-log-reschedule))
  10376. (when repeater
  10377. (save-excursion
  10378. (org-back-to-heading t)
  10379. (when (re-search-forward (concat org-scheduled-string " "
  10380. org-last-inserted-timestamp)
  10381. (save-excursion
  10382. (outline-next-heading) (point)) t)
  10383. (goto-char (1- (match-end 0)))
  10384. (insert " " repeater)
  10385. (setq org-last-inserted-timestamp
  10386. (concat (substring org-last-inserted-timestamp 0 -1)
  10387. " " repeater
  10388. (substring org-last-inserted-timestamp -1))))))
  10389. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10390. (defun org-get-scheduled-time (pom &optional inherit)
  10391. "Get the scheduled time as a time tuple, of a format suitable
  10392. for calling org-schedule with, or if there is no scheduling,
  10393. returns nil."
  10394. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10395. (when time
  10396. (apply 'encode-time (org-parse-time-string time)))))
  10397. (defun org-get-deadline-time (pom &optional inherit)
  10398. "Get the deadine as a time tuple, of a format suitable for
  10399. calling org-deadline with, or if there is no scheduling, returns
  10400. nil."
  10401. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10402. (when time
  10403. (apply 'encode-time (org-parse-time-string time)))))
  10404. (defun org-remove-timestamp-with-keyword (keyword)
  10405. "Remove all time stamps with KEYWORD in the current entry."
  10406. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10407. beg)
  10408. (save-excursion
  10409. (org-back-to-heading t)
  10410. (setq beg (point))
  10411. (outline-next-heading)
  10412. (while (re-search-backward re beg t)
  10413. (replace-match "")
  10414. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10415. (equal (char-before) ?\ ))
  10416. (backward-delete-char 1)
  10417. (if (string-match "^[ \t]*$" (buffer-substring
  10418. (point-at-bol) (point-at-eol)))
  10419. (delete-region (point-at-bol)
  10420. (min (point-max) (1+ (point-at-eol))))))))))
  10421. (defun org-add-planning-info (what &optional time &rest remove)
  10422. "Insert new timestamp with keyword in the line directly after the headline.
  10423. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10424. If non is given, the user is prompted for a date.
  10425. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10426. be removed."
  10427. (interactive)
  10428. (let (org-time-was-given org-end-time-was-given ts
  10429. end default-time default-input)
  10430. (catch 'exit
  10431. (when (and (not time) (memq what '(scheduled deadline)))
  10432. ;; Try to get a default date/time from existing timestamp
  10433. (save-excursion
  10434. (org-back-to-heading t)
  10435. (setq end (save-excursion (outline-next-heading) (point)))
  10436. (when (re-search-forward (if (eq what 'scheduled)
  10437. org-scheduled-time-regexp
  10438. org-deadline-time-regexp)
  10439. end t)
  10440. (setq ts (match-string 1)
  10441. default-time
  10442. (apply 'encode-time (org-parse-time-string ts))
  10443. default-input (and ts (org-get-compact-tod ts))))))
  10444. (when what
  10445. ;; If necessary, get the time from the user
  10446. (setq time (or time (org-read-date nil 'to-time nil nil
  10447. default-time default-input))))
  10448. (when (and org-insert-labeled-timestamps-at-point
  10449. (member what '(scheduled deadline)))
  10450. (insert
  10451. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10452. (org-insert-time-stamp time org-time-was-given
  10453. nil nil nil (list org-end-time-was-given))
  10454. (setq what nil))
  10455. (save-excursion
  10456. (save-restriction
  10457. (let (col list elt ts buffer-invisibility-spec)
  10458. (org-back-to-heading t)
  10459. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10460. (goto-char (match-end 1))
  10461. (setq col (current-column))
  10462. (goto-char (match-end 0))
  10463. (if (eobp) (insert "\n") (forward-char 1))
  10464. (when (and (not what)
  10465. (not (looking-at
  10466. (concat "[ \t]*"
  10467. org-keyword-time-not-clock-regexp))))
  10468. ;; Nothing to add, nothing to remove...... :-)
  10469. (throw 'exit nil))
  10470. (if (and (not (looking-at outline-regexp))
  10471. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10472. "[^\r\n]*"))
  10473. (not (equal (match-string 1) org-clock-string)))
  10474. (narrow-to-region (match-beginning 0) (match-end 0))
  10475. (insert-before-markers "\n")
  10476. (backward-char 1)
  10477. (narrow-to-region (point) (point))
  10478. (and org-adapt-indentation (org-indent-to-column col)))
  10479. ;; Check if we have to remove something.
  10480. (setq list (cons what remove))
  10481. (while list
  10482. (setq elt (pop list))
  10483. (goto-char (point-min))
  10484. (when (or (and (eq elt 'scheduled)
  10485. (re-search-forward org-scheduled-time-regexp nil t))
  10486. (and (eq elt 'deadline)
  10487. (re-search-forward org-deadline-time-regexp nil t))
  10488. (and (eq elt 'closed)
  10489. (re-search-forward org-closed-time-regexp nil t)))
  10490. (replace-match "")
  10491. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10492. (skip-chars-backward " ")
  10493. (if (looking-at " +") (replace-match ""))))
  10494. (goto-char (point-max))
  10495. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10496. (when what
  10497. (insert
  10498. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10499. (cond ((eq what 'scheduled) org-scheduled-string)
  10500. ((eq what 'deadline) org-deadline-string)
  10501. ((eq what 'closed) org-closed-string))
  10502. " ")
  10503. (setq ts (org-insert-time-stamp
  10504. time
  10505. (or org-time-was-given
  10506. (and (eq what 'closed) org-log-done-with-time))
  10507. (eq what 'closed)
  10508. nil nil (list org-end-time-was-given)))
  10509. (end-of-line 1))
  10510. (goto-char (point-min))
  10511. (widen)
  10512. (if (and (looking-at "[ \t]*\n")
  10513. (equal (char-before) ?\n))
  10514. (delete-region (1- (point)) (point-at-eol)))
  10515. ts))))))
  10516. (defvar org-log-note-marker (make-marker))
  10517. (defvar org-log-note-purpose nil)
  10518. (defvar org-log-note-state nil)
  10519. (defvar org-log-note-previous-state nil)
  10520. (defvar org-log-note-how nil)
  10521. (defvar org-log-note-extra nil)
  10522. (defvar org-log-note-window-configuration nil)
  10523. (defvar org-log-note-return-to (make-marker))
  10524. (defvar org-log-post-message nil
  10525. "Message to be displayed after a log note has been stored.
  10526. The auto-repeater uses this.")
  10527. (defun org-add-note ()
  10528. "Add a note to the current entry.
  10529. This is done in the same way as adding a state change note."
  10530. (interactive)
  10531. (org-add-log-setup 'note nil nil 'findpos nil))
  10532. (defvar org-property-end-re)
  10533. (defun org-add-log-setup (&optional purpose state prev-state
  10534. findpos how &optional extra)
  10535. "Set up the post command hook to take a note.
  10536. If this is about to TODO state change, the new state is expected in STATE.
  10537. When FINDPOS is non-nil, find the correct position for the note in
  10538. the current entry. If not, assume that it can be inserted at point.
  10539. HOW is an indicator what kind of note should be created.
  10540. EXTRA is additional text that will be inserted into the notes buffer."
  10541. (let* ((org-log-into-drawer (org-log-into-drawer))
  10542. (drawer (cond ((stringp org-log-into-drawer)
  10543. org-log-into-drawer)
  10544. (org-log-into-drawer "LOGBOOK")
  10545. (t nil))))
  10546. (save-restriction
  10547. (save-excursion
  10548. (when findpos
  10549. (org-back-to-heading t)
  10550. (narrow-to-region (point) (save-excursion
  10551. (outline-next-heading) (point)))
  10552. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10553. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10554. "[^\r\n]*\\)?"))
  10555. (goto-char (match-end 0))
  10556. (cond
  10557. (drawer
  10558. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10559. nil t)
  10560. (progn
  10561. (goto-char (match-end 0))
  10562. (or org-log-states-order-reversed
  10563. (and (re-search-forward org-property-end-re nil t)
  10564. (goto-char (1- (match-beginning 0))))))
  10565. (insert "\n:" drawer ":\n:END:")
  10566. (beginning-of-line 0)
  10567. (org-indent-line-function)
  10568. (beginning-of-line 2)
  10569. (org-indent-line-function)
  10570. (end-of-line 0)))
  10571. ((and org-log-state-notes-insert-after-drawers
  10572. (save-excursion
  10573. (forward-line) (looking-at org-drawer-regexp)))
  10574. (forward-line)
  10575. (while (looking-at org-drawer-regexp)
  10576. (goto-char (match-end 0))
  10577. (re-search-forward org-property-end-re (point-max) t)
  10578. (forward-line))
  10579. (forward-line -1)))
  10580. (unless org-log-states-order-reversed
  10581. (and (= (char-after) ?\n) (forward-char 1))
  10582. (org-skip-over-state-notes)
  10583. (skip-chars-backward " \t\n\r")))
  10584. (move-marker org-log-note-marker (point))
  10585. (setq org-log-note-purpose purpose
  10586. org-log-note-state state
  10587. org-log-note-previous-state prev-state
  10588. org-log-note-how how
  10589. org-log-note-extra extra)
  10590. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10591. (defun org-skip-over-state-notes ()
  10592. "Skip past the list of State notes in an entry."
  10593. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10594. (while (looking-at "[ \t]*- State")
  10595. (condition-case nil
  10596. (org-next-item)
  10597. (error (org-end-of-item)))))
  10598. (defun org-add-log-note (&optional purpose)
  10599. "Pop up a window for taking a note, and add this note later at point."
  10600. (remove-hook 'post-command-hook 'org-add-log-note)
  10601. (setq org-log-note-window-configuration (current-window-configuration))
  10602. (delete-other-windows)
  10603. (move-marker org-log-note-return-to (point))
  10604. (switch-to-buffer (marker-buffer org-log-note-marker))
  10605. (goto-char org-log-note-marker)
  10606. (org-switch-to-buffer-other-window "*Org Note*")
  10607. (erase-buffer)
  10608. (if (memq org-log-note-how '(time state))
  10609. (let (current-prefix-arg) (org-store-log-note))
  10610. (let ((org-inhibit-startup t)) (org-mode))
  10611. (insert (format "# Insert note for %s.
  10612. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10613. (cond
  10614. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10615. ((eq org-log-note-purpose 'done) "closed todo item")
  10616. ((eq org-log-note-purpose 'state)
  10617. (format "state change from \"%s\" to \"%s\""
  10618. (or org-log-note-previous-state "")
  10619. (or org-log-note-state "")))
  10620. ((eq org-log-note-purpose 'reschedule)
  10621. "rescheduling")
  10622. ((eq org-log-note-purpose 'delschedule)
  10623. "no longer scheduled")
  10624. ((eq org-log-note-purpose 'redeadline)
  10625. "changing deadline")
  10626. ((eq org-log-note-purpose 'deldeadline)
  10627. "removing deadline")
  10628. ((eq org-log-note-purpose 'refile)
  10629. "refiling")
  10630. ((eq org-log-note-purpose 'note)
  10631. "this entry")
  10632. (t (error "This should not happen")))))
  10633. (if org-log-note-extra (insert org-log-note-extra))
  10634. (org-set-local 'org-finish-function 'org-store-log-note)))
  10635. (defvar org-note-abort nil) ; dynamically scoped
  10636. (defun org-store-log-note ()
  10637. "Finish taking a log note, and insert it to where it belongs."
  10638. (let ((txt (buffer-string))
  10639. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10640. lines ind)
  10641. (kill-buffer (current-buffer))
  10642. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10643. (setq txt (replace-match "" t t txt)))
  10644. (if (string-match "\\s-+\\'" txt)
  10645. (setq txt (replace-match "" t t txt)))
  10646. (setq lines (org-split-string txt "\n"))
  10647. (when (and note (string-match "\\S-" note))
  10648. (setq note
  10649. (org-replace-escapes
  10650. note
  10651. (list (cons "%u" (user-login-name))
  10652. (cons "%U" user-full-name)
  10653. (cons "%t" (format-time-string
  10654. (org-time-stamp-format 'long 'inactive)
  10655. (current-time)))
  10656. (cons "%T" (format-time-string
  10657. (org-time-stamp-format 'long nil)
  10658. (current-time)))
  10659. (cons "%s" (if org-log-note-state
  10660. (concat "\"" org-log-note-state "\"")
  10661. ""))
  10662. (cons "%S" (if org-log-note-previous-state
  10663. (concat "\"" org-log-note-previous-state "\"")
  10664. "\"\"")))))
  10665. (if lines (setq note (concat note " \\\\")))
  10666. (push note lines))
  10667. (when (or current-prefix-arg org-note-abort)
  10668. (when org-log-into-drawer
  10669. (org-remove-empty-drawer-at
  10670. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10671. org-log-note-marker))
  10672. (setq lines nil))
  10673. (when lines
  10674. (with-current-buffer (marker-buffer org-log-note-marker)
  10675. (save-excursion
  10676. (goto-char org-log-note-marker)
  10677. (move-marker org-log-note-marker nil)
  10678. (end-of-line 1)
  10679. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10680. (insert "- " (pop lines))
  10681. (org-indent-line-function)
  10682. (beginning-of-line 1)
  10683. (looking-at "[ \t]*")
  10684. (setq ind (concat (match-string 0) " "))
  10685. (end-of-line 1)
  10686. (while lines (insert "\n" ind (pop lines)))
  10687. (message "Note stored")
  10688. (org-back-to-heading t)
  10689. (org-cycle-hide-drawers 'children)))))
  10690. (set-window-configuration org-log-note-window-configuration)
  10691. (with-current-buffer (marker-buffer org-log-note-return-to)
  10692. (goto-char org-log-note-return-to))
  10693. (move-marker org-log-note-return-to nil)
  10694. (and org-log-post-message (message "%s" org-log-post-message)))
  10695. (defun org-remove-empty-drawer-at (drawer pos)
  10696. "Remove an empty drawer DRAWER at position POS.
  10697. POS may also be a marker."
  10698. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10699. (save-excursion
  10700. (save-restriction
  10701. (widen)
  10702. (goto-char pos)
  10703. (if (org-in-regexp
  10704. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10705. (replace-match ""))))))
  10706. (defun org-sparse-tree (&optional arg)
  10707. "Create a sparse tree, prompt for the details.
  10708. This command can create sparse trees. You first need to select the type
  10709. of match used to create the tree:
  10710. t Show all TODO entries.
  10711. T Show entries with a specific TODO keyword.
  10712. m Show entries selected by a tags/property match.
  10713. p Enter a property name and its value (both with completion on existing
  10714. names/values) and show entries with that property.
  10715. / Show entries matching a regular expression (`r' can be used as well)
  10716. d Show deadlines due within `org-deadline-warning-days'.
  10717. b Show deadlines and scheduled items before a date.
  10718. a Show deadlines and scheduled items after a date."
  10719. (interactive "P")
  10720. (let (ans kwd value)
  10721. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty [d]eadlines\n [b]efore-date [a]fter-date")
  10722. (setq ans (read-char-exclusive))
  10723. (cond
  10724. ((equal ans ?d)
  10725. (call-interactively 'org-check-deadlines))
  10726. ((equal ans ?b)
  10727. (call-interactively 'org-check-before-date))
  10728. ((equal ans ?a)
  10729. (call-interactively 'org-check-after-date))
  10730. ((equal ans ?t)
  10731. (org-show-todo-tree nil))
  10732. ((equal ans ?T)
  10733. (org-show-todo-tree '(4)))
  10734. ((member ans '(?T ?m))
  10735. (call-interactively 'org-match-sparse-tree))
  10736. ((member ans '(?p ?P))
  10737. (setq kwd (org-icompleting-read "Property: "
  10738. (mapcar 'list (org-buffer-property-keys))))
  10739. (setq value (org-icompleting-read "Value: "
  10740. (mapcar 'list (org-property-values kwd))))
  10741. (unless (string-match "\\`{.*}\\'" value)
  10742. (setq value (concat "\"" value "\"")))
  10743. (org-match-sparse-tree arg (concat kwd "=" value)))
  10744. ((member ans '(?r ?R ?/))
  10745. (call-interactively 'org-occur))
  10746. (t (error "No such sparse tree command \"%c\"" ans)))))
  10747. (defvar org-occur-highlights nil
  10748. "List of overlays used for occur matches.")
  10749. (make-variable-buffer-local 'org-occur-highlights)
  10750. (defvar org-occur-parameters nil
  10751. "Parameters of the active org-occur calls.
  10752. This is a list, each call to org-occur pushes as cons cell,
  10753. containing the regular expression and the callback, onto the list.
  10754. The list can contain several entries if `org-occur' has been called
  10755. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10756. will only contain one set of parameters. When the highlights are
  10757. removed (for example with `C-c C-c', or with the next edit (depending
  10758. on `org-remove-highlights-with-change'), this variable is emptied
  10759. as well.")
  10760. (make-variable-buffer-local 'org-occur-parameters)
  10761. (defun org-occur (regexp &optional keep-previous callback)
  10762. "Make a compact tree which shows all matches of REGEXP.
  10763. The tree will show the lines where the regexp matches, and all higher
  10764. headlines above the match. It will also show the heading after the match,
  10765. to make sure editing the matching entry is easy.
  10766. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10767. call to `org-occur' will be kept, to allow stacking of calls to this
  10768. command.
  10769. If CALLBACK is non-nil, it is a function which is called to confirm
  10770. that the match should indeed be shown."
  10771. (interactive "sRegexp: \nP")
  10772. (when (equal regexp "")
  10773. (error "Regexp cannot be empty"))
  10774. (unless keep-previous
  10775. (org-remove-occur-highlights nil nil t))
  10776. (push (cons regexp callback) org-occur-parameters)
  10777. (let ((cnt 0))
  10778. (save-excursion
  10779. (goto-char (point-min))
  10780. (if (or (not keep-previous) ; do not want to keep
  10781. (not org-occur-highlights)) ; no previous matches
  10782. ;; hide everything
  10783. (org-overview))
  10784. (while (re-search-forward regexp nil t)
  10785. (when (or (not callback)
  10786. (save-match-data (funcall callback)))
  10787. (setq cnt (1+ cnt))
  10788. (when org-highlight-sparse-tree-matches
  10789. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10790. (org-show-context 'occur-tree))))
  10791. (when org-remove-highlights-with-change
  10792. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10793. nil 'local))
  10794. (unless org-sparse-tree-open-archived-trees
  10795. (org-hide-archived-subtrees (point-min) (point-max)))
  10796. (run-hooks 'org-occur-hook)
  10797. (if (interactive-p)
  10798. (message "%d match(es) for regexp %s" cnt regexp))
  10799. cnt))
  10800. (defun org-show-context (&optional key)
  10801. "Make sure point and context are visible.
  10802. How much context is shown depends upon the variables
  10803. `org-show-hierarchy-above', `org-show-following-heading'. and
  10804. `org-show-siblings'."
  10805. (let ((heading-p (org-on-heading-p t))
  10806. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10807. (following-p (org-get-alist-option org-show-following-heading key))
  10808. (entry-p (org-get-alist-option org-show-entry-below key))
  10809. (siblings-p (org-get-alist-option org-show-siblings key)))
  10810. (catch 'exit
  10811. ;; Show heading or entry text
  10812. (if (and heading-p (not entry-p))
  10813. (org-flag-heading nil) ; only show the heading
  10814. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10815. (org-show-hidden-entry))) ; show entire entry
  10816. (when following-p
  10817. ;; Show next sibling, or heading below text
  10818. (save-excursion
  10819. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10820. (org-flag-heading nil))))
  10821. (when siblings-p (org-show-siblings))
  10822. (when hierarchy-p
  10823. ;; show all higher headings, possibly with siblings
  10824. (save-excursion
  10825. (while (and (condition-case nil
  10826. (progn (org-up-heading-all 1) t)
  10827. (error nil))
  10828. (not (bobp)))
  10829. (org-flag-heading nil)
  10830. (when siblings-p (org-show-siblings))))))))
  10831. (defvar org-reveal-start-hook nil
  10832. "Hook run before revealing a location.")
  10833. (defun org-reveal (&optional siblings)
  10834. "Show current entry, hierarchy above it, and the following headline.
  10835. This can be used to show a consistent set of context around locations
  10836. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10837. not t for the search context.
  10838. With optional argument SIBLINGS, on each level of the hierarchy all
  10839. siblings are shown. This repairs the tree structure to what it would
  10840. look like when opened with hierarchical calls to `org-cycle'.
  10841. With double optional argument `C-u C-u', go to the parent and show the
  10842. entire tree."
  10843. (interactive "P")
  10844. (run-hooks 'org-reveal-start-hook)
  10845. (let ((org-show-hierarchy-above t)
  10846. (org-show-following-heading t)
  10847. (org-show-siblings (if siblings t org-show-siblings)))
  10848. (org-show-context nil))
  10849. (when (equal siblings '(16))
  10850. (save-excursion
  10851. (when (org-up-heading-safe)
  10852. (org-show-subtree)
  10853. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10854. (defun org-highlight-new-match (beg end)
  10855. "Highlight from BEG to END and mark the highlight is an occur headline."
  10856. (let ((ov (make-overlay beg end)))
  10857. (overlay-put ov 'face 'secondary-selection)
  10858. (push ov org-occur-highlights)))
  10859. (defun org-remove-occur-highlights (&optional beg end noremove)
  10860. "Remove the occur highlights from the buffer.
  10861. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10862. from the `before-change-functions' in the current buffer."
  10863. (interactive)
  10864. (unless org-inhibit-highlight-removal
  10865. (mapc 'delete-overlay org-occur-highlights)
  10866. (setq org-occur-highlights nil)
  10867. (setq org-occur-parameters nil)
  10868. (unless noremove
  10869. (remove-hook 'before-change-functions
  10870. 'org-remove-occur-highlights 'local))))
  10871. ;;;; Priorities
  10872. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10873. "Regular expression matching the priority indicator.")
  10874. (defvar org-remove-priority-next-time nil)
  10875. (defun org-priority-up ()
  10876. "Increase the priority of the current item."
  10877. (interactive)
  10878. (org-priority 'up))
  10879. (defun org-priority-down ()
  10880. "Decrease the priority of the current item."
  10881. (interactive)
  10882. (org-priority 'down))
  10883. (defun org-priority (&optional action)
  10884. "Change the priority of an item by ARG.
  10885. ACTION can be `set', `up', `down', or a character."
  10886. (interactive)
  10887. (unless org-enable-priority-commands
  10888. (error "Priority commands are disabled"))
  10889. (setq action (or action 'set))
  10890. (let (current new news have remove)
  10891. (save-excursion
  10892. (org-back-to-heading t)
  10893. (if (looking-at org-priority-regexp)
  10894. (setq current (string-to-char (match-string 2))
  10895. have t)
  10896. (setq current org-default-priority))
  10897. (cond
  10898. ((eq action 'remove)
  10899. (setq remove t new ?\ ))
  10900. ((or (eq action 'set)
  10901. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10902. (if (not (eq action 'set))
  10903. (setq new action)
  10904. (message "Priority %c-%c, SPC to remove: "
  10905. org-highest-priority org-lowest-priority)
  10906. (setq new (read-char-exclusive)))
  10907. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10908. (= (upcase org-lowest-priority) org-lowest-priority))
  10909. (setq new (upcase new)))
  10910. (cond ((equal new ?\ ) (setq remove t))
  10911. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10912. (error "Priority must be between `%c' and `%c'"
  10913. org-highest-priority org-lowest-priority))))
  10914. ((eq action 'up)
  10915. (if (and (not have) (eq last-command this-command))
  10916. (setq new org-lowest-priority)
  10917. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10918. org-default-priority (1- current)))))
  10919. ((eq action 'down)
  10920. (if (and (not have) (eq last-command this-command))
  10921. (setq new org-highest-priority)
  10922. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10923. org-default-priority (1+ current)))))
  10924. (t (error "Invalid action")))
  10925. (if (or (< (upcase new) org-highest-priority)
  10926. (> (upcase new) org-lowest-priority))
  10927. (setq remove t))
  10928. (setq news (format "%c" new))
  10929. (if have
  10930. (if remove
  10931. (replace-match "" t t nil 1)
  10932. (replace-match news t t nil 2))
  10933. (if remove
  10934. (error "No priority cookie found in line")
  10935. (let ((case-fold-search nil))
  10936. (looking-at org-todo-line-regexp))
  10937. (if (match-end 2)
  10938. (progn
  10939. (goto-char (match-end 2))
  10940. (insert " [#" news "]"))
  10941. (goto-char (match-beginning 3))
  10942. (insert "[#" news "] "))))
  10943. (org-preserve-lc (org-set-tags nil 'align)))
  10944. (if remove
  10945. (message "Priority removed")
  10946. (message "Priority of current item set to %s" news))))
  10947. (defun org-get-priority (s)
  10948. "Find priority cookie and return priority."
  10949. (save-match-data
  10950. (if (not (string-match org-priority-regexp s))
  10951. (* 1000 (- org-lowest-priority org-default-priority))
  10952. (* 1000 (- org-lowest-priority
  10953. (string-to-char (match-string 2 s)))))))
  10954. ;;;; Tags
  10955. (defvar org-agenda-archives-mode)
  10956. (defvar org-map-continue-from nil
  10957. "Position from where mapping should continue.
  10958. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10959. (defvar org-scanner-tags nil
  10960. "The current tag list while the tags scanner is running.")
  10961. (defvar org-trust-scanner-tags nil
  10962. "Should `org-get-tags-at' use the tags fro the scanner.
  10963. This is for internal dynamical scoping only.
  10964. When this is non-nil, the function `org-get-tags-at' will return the value
  10965. of `org-scanner-tags' instead of building the list by itself. This
  10966. can lead to large speed-ups when the tags scanner is used in a file with
  10967. many entries, and when the list of tags is retrieved, for example to
  10968. obtain a list of properties. Building the tags list for each entry in such
  10969. a file becomes an N^2 operation - but with this variable set, it scales
  10970. as N.")
  10971. (defun org-scan-tags (action matcher &optional todo-only)
  10972. "Scan headline tags with inheritance and produce output ACTION.
  10973. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10974. or `agenda' to produce an entry list for an agenda view. It can also be
  10975. a Lisp form or a function that should be called at each matched headline, in
  10976. this case the return value is a list of all return values from these calls.
  10977. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10978. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10979. only lines with a TODO keyword are included in the output."
  10980. (require 'org-agenda)
  10981. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10982. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10983. (org-re
  10984. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10985. (props (list 'face 'default
  10986. 'done-face 'org-agenda-done
  10987. 'undone-face 'default
  10988. 'mouse-face 'highlight
  10989. 'org-not-done-regexp org-not-done-regexp
  10990. 'org-todo-regexp org-todo-regexp
  10991. 'help-echo
  10992. (format "mouse-2 or RET jump to org file %s"
  10993. (abbreviate-file-name
  10994. (or (buffer-file-name (buffer-base-buffer))
  10995. (buffer-name (buffer-base-buffer)))))))
  10996. (case-fold-search nil)
  10997. (org-map-continue-from nil)
  10998. lspos tags tags-list
  10999. (tags-alist (list (cons 0 org-file-tags)))
  11000. (llast 0) rtn rtn1 level category i txt
  11001. todo marker entry priority)
  11002. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11003. (setq action (list 'lambda nil action)))
  11004. (save-excursion
  11005. (goto-char (point-min))
  11006. (when (eq action 'sparse-tree)
  11007. (org-overview)
  11008. (org-remove-occur-highlights))
  11009. (while (re-search-forward re nil t)
  11010. (catch :skip
  11011. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11012. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11013. (goto-char (setq lspos (match-beginning 0)))
  11014. (setq level (org-reduced-level (funcall outline-level))
  11015. category (org-get-category))
  11016. (setq i llast llast level)
  11017. ;; remove tag lists from same and sublevels
  11018. (while (>= i level)
  11019. (when (setq entry (assoc i tags-alist))
  11020. (setq tags-alist (delete entry tags-alist)))
  11021. (setq i (1- i)))
  11022. ;; add the next tags
  11023. (when tags
  11024. (setq tags (org-split-string tags ":")
  11025. tags-alist
  11026. (cons (cons level tags) tags-alist)))
  11027. ;; compile tags for current headline
  11028. (setq tags-list
  11029. (if org-use-tag-inheritance
  11030. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11031. tags)
  11032. org-scanner-tags tags-list)
  11033. (when org-use-tag-inheritance
  11034. (setcdr (car tags-alist)
  11035. (mapcar (lambda (x)
  11036. (setq x (copy-sequence x))
  11037. (org-add-prop-inherited x))
  11038. (cdar tags-alist))))
  11039. (when (and tags org-use-tag-inheritance
  11040. (or (not (eq t org-use-tag-inheritance))
  11041. org-tags-exclude-from-inheritance))
  11042. ;; selective inheritance, remove uninherited ones
  11043. (setcdr (car tags-alist)
  11044. (org-remove-uniherited-tags (cdar tags-alist))))
  11045. (when (and (or (not todo-only)
  11046. (and (member todo org-not-done-keywords)
  11047. (or (not org-agenda-tags-todo-honor-ignore-options)
  11048. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11049. (let ((case-fold-search t)) (eval matcher))
  11050. (or
  11051. (not (member org-archive-tag tags-list))
  11052. ;; we have an archive tag, should we use this anyway?
  11053. (or (not org-agenda-skip-archived-trees)
  11054. (and (eq action 'agenda) org-agenda-archives-mode))))
  11055. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11056. ;; select this headline
  11057. (cond
  11058. ((eq action 'sparse-tree)
  11059. (and org-highlight-sparse-tree-matches
  11060. (org-get-heading) (match-end 0)
  11061. (org-highlight-new-match
  11062. (match-beginning 0) (match-beginning 1)))
  11063. (org-show-context 'tags-tree))
  11064. ((eq action 'agenda)
  11065. (setq txt (org-format-agenda-item
  11066. ""
  11067. (concat
  11068. (if (eq org-tags-match-list-sublevels 'indented)
  11069. (make-string (1- level) ?.) "")
  11070. (org-get-heading))
  11071. category
  11072. tags-list
  11073. )
  11074. priority (org-get-priority txt))
  11075. (goto-char lspos)
  11076. (setq marker (org-agenda-new-marker))
  11077. (org-add-props txt props
  11078. 'org-marker marker 'org-hd-marker marker 'org-category category
  11079. 'todo-state todo
  11080. 'priority priority 'type "tagsmatch")
  11081. (push txt rtn))
  11082. ((functionp action)
  11083. (setq org-map-continue-from nil)
  11084. (save-excursion
  11085. (setq rtn1 (funcall action))
  11086. (push rtn1 rtn)))
  11087. (t (error "Invalid action")))
  11088. ;; if we are to skip sublevels, jump to end of subtree
  11089. (unless org-tags-match-list-sublevels
  11090. (org-end-of-subtree t)
  11091. (backward-char 1))))
  11092. ;; Get the correct position from where to continue
  11093. (if org-map-continue-from
  11094. (goto-char org-map-continue-from)
  11095. (and (= (point) lspos) (end-of-line 1)))))
  11096. (when (and (eq action 'sparse-tree)
  11097. (not org-sparse-tree-open-archived-trees))
  11098. (org-hide-archived-subtrees (point-min) (point-max)))
  11099. (nreverse rtn)))
  11100. (defun org-remove-uniherited-tags (tags)
  11101. "Remove all tags that are not inherited from the list TAGS."
  11102. (cond
  11103. ((eq org-use-tag-inheritance t)
  11104. (if org-tags-exclude-from-inheritance
  11105. (org-delete-all org-tags-exclude-from-inheritance tags)
  11106. tags))
  11107. ((not org-use-tag-inheritance) nil)
  11108. ((stringp org-use-tag-inheritance)
  11109. (delq nil (mapcar
  11110. (lambda (x)
  11111. (if (and (string-match org-use-tag-inheritance x)
  11112. (not (member x org-tags-exclude-from-inheritance)))
  11113. x nil))
  11114. tags)))
  11115. ((listp org-use-tag-inheritance)
  11116. (delq nil (mapcar
  11117. (lambda (x)
  11118. (if (member x org-use-tag-inheritance) x nil))
  11119. tags)))))
  11120. (defvar todo-only) ;; dynamically scoped
  11121. (defun org-match-sparse-tree (&optional todo-only match)
  11122. "Create a sparse tree according to tags string MATCH.
  11123. MATCH can contain positive and negative selection of tags, like
  11124. \"+WORK+URGENT-WITHBOSS\".
  11125. If optional argument TODO-ONLY is non-nil, only select lines that are
  11126. also TODO lines."
  11127. (interactive "P")
  11128. (org-prepare-agenda-buffers (list (current-buffer)))
  11129. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11130. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11131. (defvar org-cached-props nil)
  11132. (defun org-cached-entry-get (pom property)
  11133. (if (or (eq t org-use-property-inheritance)
  11134. (and (stringp org-use-property-inheritance)
  11135. (string-match org-use-property-inheritance property))
  11136. (and (listp org-use-property-inheritance)
  11137. (member property org-use-property-inheritance)))
  11138. ;; Caching is not possible, check it directly
  11139. (org-entry-get pom property 'inherit)
  11140. ;; Get all properties, so that we can do complicated checks easily
  11141. (cdr (assoc property (or org-cached-props
  11142. (setq org-cached-props
  11143. (org-entry-properties pom)))))))
  11144. (defun org-global-tags-completion-table (&optional files)
  11145. "Return the list of all tags in all agenda buffer/files."
  11146. (save-excursion
  11147. (org-uniquify
  11148. (delq nil
  11149. (apply 'append
  11150. (mapcar
  11151. (lambda (file)
  11152. (set-buffer (find-file-noselect file))
  11153. (append (org-get-buffer-tags)
  11154. (mapcar (lambda (x) (if (stringp (car-safe x))
  11155. (list (car-safe x)) nil))
  11156. org-tag-alist)))
  11157. (if (and files (car files))
  11158. files
  11159. (org-agenda-files))))))))
  11160. (defun org-make-tags-matcher (match)
  11161. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11162. ;; todo-only is scoped dynamically into this function, and the function
  11163. ;; may change it if the matcher asks for it.
  11164. (unless match
  11165. ;; Get a new match request, with completion
  11166. (let ((org-last-tags-completion-table
  11167. (org-global-tags-completion-table)))
  11168. (setq match (org-completing-read-no-i
  11169. "Match: " 'org-tags-completion-function nil nil nil
  11170. 'org-tags-history))))
  11171. ;; Parse the string and create a lisp form
  11172. (let ((match0 match)
  11173. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  11174. minus tag mm
  11175. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11176. orterms term orlist re-p str-p level-p level-op time-p
  11177. prop-p pn pv po cat-p gv rest)
  11178. (if (string-match "/+" match)
  11179. ;; match contains also a todo-matching request
  11180. (progn
  11181. (setq tagsmatch (substring match 0 (match-beginning 0))
  11182. todomatch (substring match (match-end 0)))
  11183. (if (string-match "^!" todomatch)
  11184. (setq todo-only t todomatch (substring todomatch 1)))
  11185. (if (string-match "^\\s-*$" todomatch)
  11186. (setq todomatch nil)))
  11187. ;; only matching tags
  11188. (setq tagsmatch match todomatch nil))
  11189. ;; Make the tags matcher
  11190. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11191. (setq tagsmatcher t)
  11192. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11193. (while (setq term (pop orterms))
  11194. (while (and (equal (substring term -1) "\\") orterms)
  11195. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11196. (while (string-match re term)
  11197. (setq rest (substring term (match-end 0))
  11198. minus (and (match-end 1)
  11199. (equal (match-string 1 term) "-"))
  11200. tag (match-string 2 term)
  11201. re-p (equal (string-to-char tag) ?{)
  11202. level-p (match-end 4)
  11203. prop-p (match-end 5)
  11204. mm (cond
  11205. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11206. (level-p
  11207. (setq level-op (org-op-to-function (match-string 3 term)))
  11208. `(,level-op level ,(string-to-number
  11209. (match-string 4 term))))
  11210. (prop-p
  11211. (setq pn (match-string 5 term)
  11212. po (match-string 6 term)
  11213. pv (match-string 7 term)
  11214. cat-p (equal pn "CATEGORY")
  11215. re-p (equal (string-to-char pv) ?{)
  11216. str-p (equal (string-to-char pv) ?\")
  11217. time-p (save-match-data
  11218. (string-match "^\"[[<].*[]>]\"$" pv))
  11219. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11220. (if time-p (setq pv (org-matcher-time pv)))
  11221. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11222. (cond
  11223. ((equal pn "CATEGORY")
  11224. (setq gv '(get-text-property (point) 'org-category)))
  11225. ((equal pn "TODO")
  11226. (setq gv 'todo))
  11227. (t
  11228. (setq gv `(org-cached-entry-get nil ,pn))))
  11229. (if re-p
  11230. (if (eq po 'org<>)
  11231. `(not (string-match ,pv (or ,gv "")))
  11232. `(string-match ,pv (or ,gv "")))
  11233. (if str-p
  11234. `(,po (or ,gv "") ,pv)
  11235. `(,po (string-to-number (or ,gv ""))
  11236. ,(string-to-number pv) ))))
  11237. (t `(member ,tag tags-list)))
  11238. mm (if minus (list 'not mm) mm)
  11239. term rest)
  11240. (push mm tagsmatcher))
  11241. (push (if (> (length tagsmatcher) 1)
  11242. (cons 'and tagsmatcher)
  11243. (car tagsmatcher))
  11244. orlist)
  11245. (setq tagsmatcher nil))
  11246. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11247. (setq tagsmatcher
  11248. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11249. ;; Make the todo matcher
  11250. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11251. (setq todomatcher t)
  11252. (setq orterms (org-split-string todomatch "|") orlist nil)
  11253. (while (setq term (pop orterms))
  11254. (while (string-match re term)
  11255. (setq minus (and (match-end 1)
  11256. (equal (match-string 1 term) "-"))
  11257. kwd (match-string 2 term)
  11258. re-p (equal (string-to-char kwd) ?{)
  11259. term (substring term (match-end 0))
  11260. mm (if re-p
  11261. `(string-match ,(substring kwd 1 -1) todo)
  11262. (list 'equal 'todo kwd))
  11263. mm (if minus (list 'not mm) mm))
  11264. (push mm todomatcher))
  11265. (push (if (> (length todomatcher) 1)
  11266. (cons 'and todomatcher)
  11267. (car todomatcher))
  11268. orlist)
  11269. (setq todomatcher nil))
  11270. (setq todomatcher (if (> (length orlist) 1)
  11271. (cons 'or orlist) (car orlist))))
  11272. ;; Return the string and lisp forms of the matcher
  11273. (setq matcher (if todomatcher
  11274. (list 'and tagsmatcher todomatcher)
  11275. tagsmatcher))
  11276. (cons match0 matcher)))
  11277. (defun org-op-to-function (op &optional stringp)
  11278. "Turn an operator into the appropriate function."
  11279. (setq op
  11280. (cond
  11281. ((equal op "<" ) '(< string< org-time<))
  11282. ((equal op ">" ) '(> org-string> org-time>))
  11283. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11284. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11285. ((member op '("=" "==")) '(= string= org-time=))
  11286. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11287. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11288. (defun org<> (a b) (not (= a b)))
  11289. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11290. (defun org-string>= (a b) (not (string< a b)))
  11291. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11292. (defun org-string<> (a b) (not (string= a b)))
  11293. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11294. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11295. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11296. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11297. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11298. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11299. (defun org-2ft (s)
  11300. "Convert S to a floating point time.
  11301. If S is already a number, just return it. If it is a string, parse
  11302. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11303. (cond
  11304. ((numberp s) s)
  11305. ((stringp s)
  11306. (condition-case nil
  11307. (float-time (apply 'encode-time (org-parse-time-string s)))
  11308. (error 0.)))
  11309. (t 0.)))
  11310. (defun org-time-today ()
  11311. "Time in seconds today at 0:00.
  11312. Returns the float number of seconds since the beginning of the
  11313. epoch to the beginning of today (00:00)."
  11314. (float-time (apply 'encode-time
  11315. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11316. (defun org-matcher-time (s)
  11317. "Interpret a time comparison value."
  11318. (save-match-data
  11319. (cond
  11320. ((string= s "<now>") (float-time))
  11321. ((string= s "<today>") (org-time-today))
  11322. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11323. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11324. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11325. (+ (org-time-today)
  11326. (* (string-to-number (match-string 1 s))
  11327. (cdr (assoc (match-string 2 s)
  11328. '(("d" . 86400.0) ("w" . 604800.0)
  11329. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11330. (t (org-2ft s)))))
  11331. (defun org-match-any-p (re list)
  11332. "Does re match any element of list?"
  11333. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11334. (delq nil list))
  11335. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11336. (defvar org-tags-overlay (make-overlay 1 1))
  11337. (org-detach-overlay org-tags-overlay)
  11338. (defun org-get-local-tags-at (&optional pos)
  11339. "Get a list of tags defined in the current headline."
  11340. (org-get-tags-at pos 'local))
  11341. (defun org-get-local-tags ()
  11342. "Get a list of tags defined in the current headline."
  11343. (org-get-tags-at nil 'local))
  11344. (defun org-get-tags-at (&optional pos local)
  11345. "Get a list of all headline tags applicable at POS.
  11346. POS defaults to point. If tags are inherited, the list contains
  11347. the targets in the same sequence as the headlines appear, i.e.
  11348. the tags of the current headline come last.
  11349. When LOCAL is non-nil, only return tags from the current headline,
  11350. ignore inherited ones."
  11351. (interactive)
  11352. (if (and org-trust-scanner-tags
  11353. (or (not pos) (equal pos (point)))
  11354. (not local))
  11355. org-scanner-tags
  11356. (let (tags ltags lastpos parent)
  11357. (save-excursion
  11358. (save-restriction
  11359. (widen)
  11360. (goto-char (or pos (point)))
  11361. (save-match-data
  11362. (catch 'done
  11363. (condition-case nil
  11364. (progn
  11365. (org-back-to-heading t)
  11366. (while (not (equal lastpos (point)))
  11367. (setq lastpos (point))
  11368. (when (looking-at
  11369. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  11370. (setq ltags (org-split-string
  11371. (org-match-string-no-properties 1) ":"))
  11372. (when parent
  11373. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11374. (setq tags (append
  11375. (if parent
  11376. (org-remove-uniherited-tags ltags)
  11377. ltags)
  11378. tags)))
  11379. (or org-use-tag-inheritance (throw 'done t))
  11380. (if local (throw 'done t))
  11381. (or (org-up-heading-safe) (error nil))
  11382. (setq parent t)))
  11383. (error nil)))))
  11384. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11385. (defun org-add-prop-inherited (s)
  11386. (add-text-properties 0 (length s) '(inherited t) s)
  11387. s)
  11388. (defun org-toggle-tag (tag &optional onoff)
  11389. "Toggle the tag TAG for the current line.
  11390. If ONOFF is `on' or `off', don't toggle but set to this state."
  11391. (let (res current)
  11392. (save-excursion
  11393. (org-back-to-heading t)
  11394. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  11395. (point-at-eol) t)
  11396. (progn
  11397. (setq current (match-string 1))
  11398. (replace-match ""))
  11399. (setq current ""))
  11400. (setq current (nreverse (org-split-string current ":")))
  11401. (cond
  11402. ((eq onoff 'on)
  11403. (setq res t)
  11404. (or (member tag current) (push tag current)))
  11405. ((eq onoff 'off)
  11406. (or (not (member tag current)) (setq current (delete tag current))))
  11407. (t (if (member tag current)
  11408. (setq current (delete tag current))
  11409. (setq res t)
  11410. (push tag current))))
  11411. (end-of-line 1)
  11412. (if current
  11413. (progn
  11414. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11415. (org-set-tags nil t))
  11416. (delete-horizontal-space))
  11417. (run-hooks 'org-after-tags-change-hook))
  11418. res))
  11419. (defun org-align-tags-here (to-col)
  11420. ;; Assumes that this is a headline
  11421. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11422. (beginning-of-line 1)
  11423. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11424. (< pos (match-beginning 2)))
  11425. (progn
  11426. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11427. (goto-char (match-beginning 1))
  11428. (insert " ")
  11429. (delete-region (point) (1+ (match-beginning 2)))
  11430. (setq ncol (max (1+ (current-column))
  11431. (1+ col)
  11432. (if (> to-col 0)
  11433. to-col
  11434. (- (abs to-col) tags-l))))
  11435. (setq p (point))
  11436. (insert (make-string (- ncol (current-column)) ?\ ))
  11437. (setq ncol (current-column))
  11438. (when indent-tabs-mode (tabify p (point-at-eol)))
  11439. (org-move-to-column (min ncol col) t))
  11440. (goto-char pos))))
  11441. (defun org-set-tags-command (&optional arg just-align)
  11442. "Call the set-tags command for the current entry."
  11443. (interactive "P")
  11444. (if (org-on-heading-p)
  11445. (org-set-tags arg just-align)
  11446. (save-excursion
  11447. (org-back-to-heading t)
  11448. (org-set-tags arg just-align))))
  11449. (defun org-set-tags-to (data)
  11450. "Set the tags of the current entry to DATA, replacing the current tags.
  11451. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11452. If DATA is nil or the empty string, any tags will be removed."
  11453. (interactive "sTags: ")
  11454. (setq data
  11455. (cond
  11456. ((eq data nil) "")
  11457. ((equal data "") "")
  11458. ((stringp data)
  11459. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11460. ":"))
  11461. ((listp data)
  11462. (concat ":" (mapconcat 'identity data ":") ":"))
  11463. (t nil)))
  11464. (when data
  11465. (save-excursion
  11466. (org-back-to-heading t)
  11467. (when (looking-at org-complex-heading-regexp)
  11468. (if (match-end 5)
  11469. (progn
  11470. (goto-char (match-beginning 5))
  11471. (insert data)
  11472. (delete-region (point) (point-at-eol))
  11473. (org-set-tags nil 'align))
  11474. (goto-char (point-at-eol))
  11475. (insert " " data)
  11476. (org-set-tags nil 'align)))
  11477. (beginning-of-line 1)
  11478. (if (looking-at ".*?\\([ \t]+\\)$")
  11479. (delete-region (match-beginning 1) (match-end 1))))))
  11480. (defun org-align-all-tags ()
  11481. "Align the tags i all headings."
  11482. (interactive)
  11483. (save-excursion
  11484. (or (ignore-errors (org-back-to-heading t))
  11485. (outline-next-heading))
  11486. (if (org-on-heading-p)
  11487. (org-set-tags t)
  11488. (message "No headings"))))
  11489. (defun org-set-tags (&optional arg just-align)
  11490. "Set the tags for the current headline.
  11491. With prefix ARG, realign all tags in headings in the current buffer."
  11492. (interactive "P")
  11493. (let* ((re (concat "^" outline-regexp))
  11494. (current (org-get-tags-string))
  11495. (col (current-column))
  11496. (org-setting-tags t)
  11497. table current-tags inherited-tags ; computed below when needed
  11498. tags p0 c0 c1 rpl)
  11499. (if arg
  11500. (save-excursion
  11501. (goto-char (point-min))
  11502. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11503. (while (re-search-forward re nil t)
  11504. (org-set-tags nil t)
  11505. (end-of-line 1)))
  11506. (message "All tags realigned to column %d" org-tags-column))
  11507. (if just-align
  11508. (setq tags current)
  11509. ;; Get a new set of tags from the user
  11510. (save-excursion
  11511. (setq table (append org-tag-persistent-alist
  11512. (or org-tag-alist (org-get-buffer-tags))
  11513. (and org-complete-tags-always-offer-all-agenda-tags
  11514. (org-global-tags-completion-table (org-agenda-files))))
  11515. org-last-tags-completion-table table
  11516. current-tags (org-split-string current ":")
  11517. inherited-tags (nreverse
  11518. (nthcdr (length current-tags)
  11519. (nreverse (org-get-tags-at))))
  11520. tags
  11521. (if (or (eq t org-use-fast-tag-selection)
  11522. (and org-use-fast-tag-selection
  11523. (delq nil (mapcar 'cdr table))))
  11524. (org-fast-tag-selection
  11525. current-tags inherited-tags table
  11526. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11527. (let ((org-add-colon-after-tag-completion t))
  11528. (org-trim
  11529. (org-without-partial-completion
  11530. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11531. nil nil current 'org-tags-history)))))))
  11532. (while (string-match "[-+&]+" tags)
  11533. ;; No boolean logic, just a list
  11534. (setq tags (replace-match ":" t t tags))))
  11535. (if org-tags-sort-function
  11536. (setq tags (mapconcat 'identity
  11537. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11538. org-tags-sort-function) ":")))
  11539. (if (string-match "\\`[\t ]*\\'" tags)
  11540. (setq tags "")
  11541. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11542. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11543. ;; Insert new tags at the correct column
  11544. (beginning-of-line 1)
  11545. (cond
  11546. ((and (equal current "") (equal tags "")))
  11547. ((re-search-forward
  11548. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11549. (point-at-eol) t)
  11550. (if (equal tags "")
  11551. (setq rpl "")
  11552. (goto-char (match-beginning 0))
  11553. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11554. (1+ (point)) (point))
  11555. c1 (max (1+ c0) (if (> org-tags-column 0)
  11556. org-tags-column
  11557. (- (- org-tags-column) (length tags))))
  11558. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11559. (replace-match rpl t t)
  11560. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11561. tags)
  11562. (t (error "Tags alignment failed")))
  11563. (org-move-to-column col)
  11564. (unless just-align
  11565. (run-hooks 'org-after-tags-change-hook)))))
  11566. (defun org-change-tag-in-region (beg end tag off)
  11567. "Add or remove TAG for each entry in the region.
  11568. This works in the agenda, and also in an org-mode buffer."
  11569. (interactive
  11570. (list (region-beginning) (region-end)
  11571. (let ((org-last-tags-completion-table
  11572. (if (org-mode-p)
  11573. (org-get-buffer-tags)
  11574. (org-global-tags-completion-table))))
  11575. (org-icompleting-read
  11576. "Tag: " 'org-tags-completion-function nil nil nil
  11577. 'org-tags-history))
  11578. (progn
  11579. (message "[s]et or [r]emove? ")
  11580. (equal (read-char-exclusive) ?r))))
  11581. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11582. (let ((agendap (equal major-mode 'org-agenda-mode))
  11583. l1 l2 m buf pos newhead (cnt 0))
  11584. (goto-char end)
  11585. (setq l2 (1- (org-current-line)))
  11586. (goto-char beg)
  11587. (setq l1 (org-current-line))
  11588. (loop for l from l1 to l2 do
  11589. (org-goto-line l)
  11590. (setq m (get-text-property (point) 'org-hd-marker))
  11591. (when (or (and (org-mode-p) (org-on-heading-p))
  11592. (and agendap m))
  11593. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11594. pos (if agendap m (point)))
  11595. (with-current-buffer buf
  11596. (save-excursion
  11597. (save-restriction
  11598. (goto-char pos)
  11599. (setq cnt (1+ cnt))
  11600. (org-toggle-tag tag (if off 'off 'on))
  11601. (setq newhead (org-get-heading)))))
  11602. (and agendap (org-agenda-change-all-lines newhead m))))
  11603. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11604. (defun org-tags-completion-function (string predicate &optional flag)
  11605. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11606. (confirm (lambda (x) (stringp (car x)))))
  11607. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11608. (setq s1 (match-string 1 string)
  11609. s2 (match-string 2 string))
  11610. (setq s1 "" s2 string))
  11611. (cond
  11612. ((eq flag nil)
  11613. ;; try completion
  11614. (setq rtn (try-completion s2 ctable confirm))
  11615. (if (stringp rtn)
  11616. (setq rtn
  11617. (concat s1 s2 (substring rtn (length s2))
  11618. (if (and org-add-colon-after-tag-completion
  11619. (assoc rtn ctable))
  11620. ":" ""))))
  11621. rtn)
  11622. ((eq flag t)
  11623. ;; all-completions
  11624. (all-completions s2 ctable confirm)
  11625. )
  11626. ((eq flag 'lambda)
  11627. ;; exact match?
  11628. (assoc s2 ctable)))
  11629. ))
  11630. (defun org-fast-tag-insert (kwd tags face &optional end)
  11631. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11632. (insert (format "%-12s" (concat kwd ":"))
  11633. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11634. (or end "")))
  11635. (defun org-fast-tag-show-exit (flag)
  11636. (save-excursion
  11637. (org-goto-line 3)
  11638. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11639. (replace-match ""))
  11640. (when flag
  11641. (end-of-line 1)
  11642. (org-move-to-column (- (window-width) 19) t)
  11643. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11644. (defun org-set-current-tags-overlay (current prefix)
  11645. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11646. (if (featurep 'xemacs)
  11647. (org-overlay-display org-tags-overlay (concat prefix s)
  11648. 'secondary-selection)
  11649. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11650. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11651. (defvar org-last-tag-selection-key nil)
  11652. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11653. "Fast tag selection with single keys.
  11654. CURRENT is the current list of tags in the headline, INHERITED is the
  11655. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11656. possibly with grouping information. TODO-TABLE is a similar table with
  11657. TODO keywords, should these have keys assigned to them.
  11658. If the keys are nil, a-z are automatically assigned.
  11659. Returns the new tags string, or nil to not change the current settings."
  11660. (let* ((fulltable (append table todo-table))
  11661. (maxlen (apply 'max (mapcar
  11662. (lambda (x)
  11663. (if (stringp (car x)) (string-width (car x)) 0))
  11664. fulltable)))
  11665. (buf (current-buffer))
  11666. (expert (eq org-fast-tag-selection-single-key 'expert))
  11667. (buffer-tags nil)
  11668. (fwidth (+ maxlen 3 1 3))
  11669. (ncol (/ (- (window-width) 4) fwidth))
  11670. (i-face 'org-done)
  11671. (c-face 'org-todo)
  11672. tg cnt e c char c1 c2 ntable tbl rtn
  11673. ov-start ov-end ov-prefix
  11674. (exit-after-next org-fast-tag-selection-single-key)
  11675. (done-keywords org-done-keywords)
  11676. groups ingroup)
  11677. (save-excursion
  11678. (beginning-of-line 1)
  11679. (if (looking-at
  11680. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11681. (setq ov-start (match-beginning 1)
  11682. ov-end (match-end 1)
  11683. ov-prefix "")
  11684. (setq ov-start (1- (point-at-eol))
  11685. ov-end (1+ ov-start))
  11686. (skip-chars-forward "^\n\r")
  11687. (setq ov-prefix
  11688. (concat
  11689. (buffer-substring (1- (point)) (point))
  11690. (if (> (current-column) org-tags-column)
  11691. " "
  11692. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11693. (move-overlay org-tags-overlay ov-start ov-end)
  11694. (save-window-excursion
  11695. (if expert
  11696. (set-buffer (get-buffer-create " *Org tags*"))
  11697. (delete-other-windows)
  11698. (split-window-vertically)
  11699. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11700. (erase-buffer)
  11701. (org-set-local 'org-done-keywords done-keywords)
  11702. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11703. (org-fast-tag-insert "Current" current c-face "\n\n")
  11704. (org-fast-tag-show-exit exit-after-next)
  11705. (org-set-current-tags-overlay current ov-prefix)
  11706. (setq tbl fulltable char ?a cnt 0)
  11707. (while (setq e (pop tbl))
  11708. (cond
  11709. ((equal (car e) :startgroup)
  11710. (push '() groups) (setq ingroup t)
  11711. (when (not (= cnt 0))
  11712. (setq cnt 0)
  11713. (insert "\n"))
  11714. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11715. ((equal (car e) :endgroup)
  11716. (setq ingroup nil cnt 0)
  11717. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11718. ((equal e '(:newline))
  11719. (when (not (= cnt 0))
  11720. (setq cnt 0)
  11721. (insert "\n")
  11722. (setq e (car tbl))
  11723. (while (equal (car tbl) '(:newline))
  11724. (insert "\n")
  11725. (setq tbl (cdr tbl)))))
  11726. (t
  11727. (setq tg (copy-sequence (car e)) c2 nil)
  11728. (if (cdr e)
  11729. (setq c (cdr e))
  11730. ;; automatically assign a character.
  11731. (setq c1 (string-to-char
  11732. (downcase (substring
  11733. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11734. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11735. (while (or (rassoc char ntable) (rassoc char table))
  11736. (setq char (1+ char)))
  11737. (setq c2 c1))
  11738. (setq c (or c2 char)))
  11739. (if ingroup (push tg (car groups)))
  11740. (setq tg (org-add-props tg nil 'face
  11741. (cond
  11742. ((not (assoc tg table))
  11743. (org-get-todo-face tg))
  11744. ((member tg current) c-face)
  11745. ((member tg inherited) i-face)
  11746. (t nil))))
  11747. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11748. (insert "[" c "] " tg (make-string
  11749. (- fwidth 4 (length tg)) ?\ ))
  11750. (push (cons tg c) ntable)
  11751. (when (= (setq cnt (1+ cnt)) ncol)
  11752. (insert "\n")
  11753. (if ingroup (insert " "))
  11754. (setq cnt 0)))))
  11755. (setq ntable (nreverse ntable))
  11756. (insert "\n")
  11757. (goto-char (point-min))
  11758. (if (not expert) (org-fit-window-to-buffer))
  11759. (setq rtn
  11760. (catch 'exit
  11761. (while t
  11762. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11763. (if (not groups) "no " "")
  11764. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11765. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11766. (setq org-last-tag-selection-key c)
  11767. (cond
  11768. ((= c ?\r) (throw 'exit t))
  11769. ((= c ?!)
  11770. (setq groups (not groups))
  11771. (goto-char (point-min))
  11772. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11773. ((= c ?\C-c)
  11774. (if (not expert)
  11775. (org-fast-tag-show-exit
  11776. (setq exit-after-next (not exit-after-next)))
  11777. (setq expert nil)
  11778. (delete-other-windows)
  11779. (split-window-vertically)
  11780. (org-switch-to-buffer-other-window " *Org tags*")
  11781. (org-fit-window-to-buffer)))
  11782. ((or (= c ?\C-g)
  11783. (and (= c ?q) (not (rassoc c ntable))))
  11784. (org-detach-overlay org-tags-overlay)
  11785. (setq quit-flag t))
  11786. ((= c ?\ )
  11787. (setq current nil)
  11788. (if exit-after-next (setq exit-after-next 'now)))
  11789. ((= c ?\t)
  11790. (condition-case nil
  11791. (setq tg (org-icompleting-read
  11792. "Tag: "
  11793. (or buffer-tags
  11794. (with-current-buffer buf
  11795. (org-get-buffer-tags)))))
  11796. (quit (setq tg "")))
  11797. (when (string-match "\\S-" tg)
  11798. (add-to-list 'buffer-tags (list tg))
  11799. (if (member tg current)
  11800. (setq current (delete tg current))
  11801. (push tg current)))
  11802. (if exit-after-next (setq exit-after-next 'now)))
  11803. ((setq e (rassoc c todo-table) tg (car e))
  11804. (with-current-buffer buf
  11805. (save-excursion (org-todo tg)))
  11806. (if exit-after-next (setq exit-after-next 'now)))
  11807. ((setq e (rassoc c ntable) tg (car e))
  11808. (if (member tg current)
  11809. (setq current (delete tg current))
  11810. (loop for g in groups do
  11811. (if (member tg g)
  11812. (mapc (lambda (x)
  11813. (setq current (delete x current)))
  11814. g)))
  11815. (push tg current))
  11816. (if exit-after-next (setq exit-after-next 'now))))
  11817. ;; Create a sorted list
  11818. (setq current
  11819. (sort current
  11820. (lambda (a b)
  11821. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11822. (if (eq exit-after-next 'now) (throw 'exit t))
  11823. (goto-char (point-min))
  11824. (beginning-of-line 2)
  11825. (delete-region (point) (point-at-eol))
  11826. (org-fast-tag-insert "Current" current c-face)
  11827. (org-set-current-tags-overlay current ov-prefix)
  11828. (while (re-search-forward
  11829. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11830. (setq tg (match-string 1))
  11831. (add-text-properties
  11832. (match-beginning 1) (match-end 1)
  11833. (list 'face
  11834. (cond
  11835. ((member tg current) c-face)
  11836. ((member tg inherited) i-face)
  11837. (t (get-text-property (match-beginning 1) 'face))))))
  11838. (goto-char (point-min)))))
  11839. (org-detach-overlay org-tags-overlay)
  11840. (if rtn
  11841. (mapconcat 'identity current ":")
  11842. nil))))
  11843. (defun org-get-tags-string ()
  11844. "Get the TAGS string in the current headline."
  11845. (unless (org-on-heading-p t)
  11846. (error "Not on a heading"))
  11847. (save-excursion
  11848. (beginning-of-line 1)
  11849. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11850. (org-match-string-no-properties 1)
  11851. "")))
  11852. (defun org-get-tags ()
  11853. "Get the list of tags specified in the current headline."
  11854. (org-split-string (org-get-tags-string) ":"))
  11855. (defun org-get-buffer-tags ()
  11856. "Get a table of all tags used in the buffer, for completion."
  11857. (let (tags)
  11858. (save-excursion
  11859. (goto-char (point-min))
  11860. (while (re-search-forward
  11861. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11862. (when (equal (char-after (point-at-bol 0)) ?*)
  11863. (mapc (lambda (x) (add-to-list 'tags x))
  11864. (org-split-string (org-match-string-no-properties 1) ":")))))
  11865. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11866. (mapcar 'list tags)))
  11867. ;;;; The mapping API
  11868. ;;;###autoload
  11869. (defun org-map-entries (func &optional match scope &rest skip)
  11870. "Call FUNC at each headline selected by MATCH in SCOPE.
  11871. FUNC is a function or a lisp form. The function will be called without
  11872. arguments, with the cursor positioned at the beginning of the headline.
  11873. The return values of all calls to the function will be collected and
  11874. returned as a list.
  11875. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11876. does not need to preserve point. After evaluation, the cursor will be
  11877. moved to the end of the line (presumably of the headline of the
  11878. processed entry) and search continues from there. Under some
  11879. circumstances, this may not produce the wanted results. For example,
  11880. if you have removed (e.g. archived) the current (sub)tree it could
  11881. mean that the next entry will be skipped entirely. In such cases, you
  11882. can specify the position from where search should continue by making
  11883. FUNC set the variable `org-map-continue-from' to the desired buffer
  11884. position.
  11885. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11886. Only headlines that are matched by this query will be considered during
  11887. the iteration. When MATCH is nil or t, all headlines will be
  11888. visited by the iteration.
  11889. SCOPE determines the scope of this command. It can be any of:
  11890. nil The current buffer, respecting the restriction if any
  11891. tree The subtree started with the entry at point
  11892. file The current buffer, without restriction
  11893. file-with-archives
  11894. The current buffer, and any archives associated with it
  11895. agenda All agenda files
  11896. agenda-with-archives
  11897. All agenda files with any archive files associated with them
  11898. \(file1 file2 ...)
  11899. If this is a list, all files in the list will be scanned
  11900. The remaining args are treated as settings for the skipping facilities of
  11901. the scanner. The following items can be given here:
  11902. archive skip trees with the archive tag.
  11903. comment skip trees with the COMMENT keyword
  11904. function or Emacs Lisp form:
  11905. will be used as value for `org-agenda-skip-function', so whenever
  11906. the function returns t, FUNC will not be called for that
  11907. entry and search will continue from the point where the
  11908. function leaves it.
  11909. If your function needs to retrieve the tags including inherited tags
  11910. at the *current* entry, you can use the value of the variable
  11911. `org-scanner-tags' which will be much faster than getting the value
  11912. with `org-get-tags-at'. If your function gets properties with
  11913. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11914. to t around the call to `org-entry-properties' to get the same speedup.
  11915. Note that if your function moves around to retrieve tags and properties at
  11916. a *different* entry, you cannot use these techniques."
  11917. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11918. (org-agenda-skip-archived-trees (memq 'archive skip))
  11919. (org-agenda-skip-comment-trees (memq 'comment skip))
  11920. (org-agenda-skip-function
  11921. (car (org-delete-all '(comment archive) skip)))
  11922. (org-tags-match-list-sublevels t)
  11923. matcher file res
  11924. org-todo-keywords-for-agenda
  11925. org-done-keywords-for-agenda
  11926. org-todo-keyword-alist-for-agenda
  11927. org-drawers-for-agenda
  11928. org-tag-alist-for-agenda)
  11929. (cond
  11930. ((eq match t) (setq matcher t))
  11931. ((eq match nil) (setq matcher t))
  11932. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11933. (save-excursion
  11934. (save-restriction
  11935. (when (eq scope 'tree)
  11936. (org-back-to-heading t)
  11937. (org-narrow-to-subtree)
  11938. (setq scope nil))
  11939. (if (not scope)
  11940. (progn
  11941. (org-prepare-agenda-buffers
  11942. (list (buffer-file-name (current-buffer))))
  11943. (setq res (org-scan-tags func matcher)))
  11944. ;; Get the right scope
  11945. (cond
  11946. ((and scope (listp scope) (symbolp (car scope)))
  11947. (setq scope (eval scope)))
  11948. ((eq scope 'agenda)
  11949. (setq scope (org-agenda-files t)))
  11950. ((eq scope 'agenda-with-archives)
  11951. (setq scope (org-agenda-files t))
  11952. (setq scope (org-add-archive-files scope)))
  11953. ((eq scope 'file)
  11954. (setq scope (list (buffer-file-name))))
  11955. ((eq scope 'file-with-archives)
  11956. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11957. (org-prepare-agenda-buffers scope)
  11958. (while (setq file (pop scope))
  11959. (with-current-buffer (org-find-base-buffer-visiting file)
  11960. (save-excursion
  11961. (save-restriction
  11962. (widen)
  11963. (goto-char (point-min))
  11964. (setq res (append res (org-scan-tags func matcher))))))))))
  11965. res))
  11966. ;;;; Properties
  11967. ;;; Setting and retrieving properties
  11968. (defconst org-special-properties
  11969. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11970. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11971. "The special properties valid in Org-mode.
  11972. These are properties that are not defined in the property drawer,
  11973. but in some other way.")
  11974. (defconst org-default-properties
  11975. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11976. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11977. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11978. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11979. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  11980. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11981. "Some properties that are used by Org-mode for various purposes.
  11982. Being in this list makes sure that they are offered for completion.")
  11983. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11984. "Regular expression matching the first line of a property drawer.")
  11985. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11986. "Regular expression matching the last line of a property drawer.")
  11987. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11988. "Regular expression matching the first line of a property drawer.")
  11989. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11990. "Regular expression matching the first line of a property drawer.")
  11991. (defconst org-property-drawer-re
  11992. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11993. org-property-end-re "\\)\n?")
  11994. "Matches an entire property drawer.")
  11995. (defconst org-clock-drawer-re
  11996. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11997. org-property-end-re "\\)\n?")
  11998. "Matches an entire clock drawer.")
  11999. (defun org-property-action ()
  12000. "Do an action on properties."
  12001. (interactive)
  12002. (let (c)
  12003. (org-at-property-p)
  12004. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12005. (setq c (read-char-exclusive))
  12006. (cond
  12007. ((equal c ?s)
  12008. (call-interactively 'org-set-property))
  12009. ((equal c ?d)
  12010. (call-interactively 'org-delete-property))
  12011. ((equal c ?D)
  12012. (call-interactively 'org-delete-property-globally))
  12013. ((equal c ?c)
  12014. (call-interactively 'org-compute-property-at-point))
  12015. (t (error "No such property action %c" c)))))
  12016. (defun org-set-effort (&optional value)
  12017. "Set the effort property of the current entry.
  12018. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12019. allowed value."
  12020. (interactive "P")
  12021. (if (equal value 0) (setq value 10))
  12022. (let* ((completion-ignore-case t)
  12023. (prop org-effort-property)
  12024. (cur (org-entry-get nil prop))
  12025. (allowed (org-property-get-allowed-values nil prop 'table))
  12026. (existing (mapcar 'list (org-property-values prop)))
  12027. rpl
  12028. (val (cond
  12029. ((stringp value) value)
  12030. ((and allowed (integerp value))
  12031. (or (car (nth (1- value) allowed))
  12032. (car (org-last allowed))))
  12033. (allowed
  12034. (message "Select 1-9,0, [RET%s]: %s"
  12035. (if cur (concat "=" cur) "")
  12036. (mapconcat 'car allowed " "))
  12037. (setq rpl (read-char-exclusive))
  12038. (if (equal rpl ?\r)
  12039. cur
  12040. (setq rpl (- rpl ?0))
  12041. (if (equal rpl 0) (setq rpl 10))
  12042. (if (and (> rpl 0) (<= rpl (length allowed)))
  12043. (car (nth (1- rpl) allowed))
  12044. (org-completing-read "Effort: " allowed nil))))
  12045. (t
  12046. (let (org-completion-use-ido org-completion-use-iswitchb)
  12047. (org-completing-read
  12048. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12049. (concat "[" cur "]") "")
  12050. ": ")
  12051. existing nil nil "" nil cur))))))
  12052. (unless (equal (org-entry-get nil prop) val)
  12053. (org-entry-put nil prop val))
  12054. (message "%s is now %s" prop val)))
  12055. (defun org-at-property-p ()
  12056. "Is cursor inside a property drawer?"
  12057. (save-excursion
  12058. (beginning-of-line 1)
  12059. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12060. (save-match-data ;; Used by calling procedures
  12061. (let ((p (point))
  12062. (range (unless (org-before-first-heading-p)
  12063. (org-get-property-block))))
  12064. (and range (<= (car range) p) (< p (cdr range))))))))
  12065. (defun org-get-property-block (&optional beg end force)
  12066. "Return the (beg . end) range of the body of the property drawer.
  12067. BEG and END can be beginning and end of subtree, if not given
  12068. they will be found.
  12069. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12070. (catch 'exit
  12071. (save-excursion
  12072. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12073. (end (or end (progn (outline-next-heading) (point)))))
  12074. (goto-char beg)
  12075. (if (re-search-forward org-property-start-re end t)
  12076. (setq beg (1+ (match-end 0)))
  12077. (if force
  12078. (save-excursion
  12079. (org-insert-property-drawer)
  12080. (setq end (progn (outline-next-heading) (point))))
  12081. (throw 'exit nil))
  12082. (goto-char beg)
  12083. (if (re-search-forward org-property-start-re end t)
  12084. (setq beg (1+ (match-end 0)))))
  12085. (if (re-search-forward org-property-end-re end t)
  12086. (setq end (match-beginning 0))
  12087. (or force (throw 'exit nil))
  12088. (goto-char beg)
  12089. (setq end beg)
  12090. (org-indent-line-function)
  12091. (insert ":END:\n"))
  12092. (cons beg end)))))
  12093. (defun org-entry-properties (&optional pom which specific)
  12094. "Get all properties of the entry at point-or-marker POM.
  12095. This includes the TODO keyword, the tags, time strings for deadline,
  12096. scheduled, and clocking, and any additional properties defined in the
  12097. entry. The return value is an alist, keys may occur multiple times
  12098. if the property key was used several times.
  12099. POM may also be nil, in which case the current entry is used.
  12100. If WHICH is nil or `all', get all properties. If WHICH is
  12101. `special' or `standard', only get that subclass. If WHICH
  12102. is a string only get exactly this property. Specific can be a string, the
  12103. specific property we are interested in. Specifying it can speed
  12104. things up because then unnecessary parsing is avoided."
  12105. (setq which (or which 'all))
  12106. (org-with-point-at pom
  12107. (let ((clockstr (substring org-clock-string 0 -1))
  12108. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12109. (case-fold-search nil)
  12110. beg end range props sum-props key value string clocksum)
  12111. (save-excursion
  12112. (when (condition-case nil
  12113. (and (org-mode-p) (org-back-to-heading t))
  12114. (error nil))
  12115. (setq beg (point))
  12116. (setq sum-props (get-text-property (point) 'org-summaries))
  12117. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12118. (outline-next-heading)
  12119. (setq end (point))
  12120. (when (memq which '(all special))
  12121. ;; Get the special properties, like TODO and tags
  12122. (goto-char beg)
  12123. (when (and (or (not specific) (string= specific "TODO"))
  12124. (looking-at org-todo-line-regexp) (match-end 2))
  12125. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12126. (when (and (or (not specific) (string= specific "PRIORITY"))
  12127. (looking-at org-priority-regexp))
  12128. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12129. (when (and (or (not specific) (string= specific "TAGS"))
  12130. (setq value (org-get-tags-string))
  12131. (string-match "\\S-" value))
  12132. (push (cons "TAGS" value) props))
  12133. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12134. (setq value (org-get-tags-at)))
  12135. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12136. ":"))
  12137. props))
  12138. (when (or (not specific) (string= specific "BLOCKED"))
  12139. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12140. (when (or (not specific)
  12141. (member specific org-all-time-keywords)
  12142. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  12143. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12144. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  12145. string (if (equal key clockstr)
  12146. (org-no-properties
  12147. (org-trim
  12148. (buffer-substring
  12149. (match-beginning 3) (goto-char (point-at-eol)))))
  12150. (substring (org-match-string-no-properties 3) 1 -1)))
  12151. (unless key
  12152. (if (= (char-after (match-beginning 3)) ?\[)
  12153. (setq key "TIMESTAMP_IA")
  12154. (setq key "TIMESTAMP")))
  12155. (when (or (equal key clockstr) (not (assoc key props)))
  12156. (push (cons key string) props))))
  12157. )
  12158. (when (memq which '(all standard))
  12159. ;; Get the standard properties, like :PROP: ...
  12160. (setq range (org-get-property-block beg end))
  12161. (when range
  12162. (goto-char (car range))
  12163. (while (re-search-forward
  12164. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12165. (cdr range) t)
  12166. (setq key (org-match-string-no-properties 1)
  12167. value (org-trim (or (org-match-string-no-properties 2) "")))
  12168. (unless (member key excluded)
  12169. (push (cons key (or value "")) props)))))
  12170. (if clocksum
  12171. (push (cons "CLOCKSUM"
  12172. (org-columns-number-to-string (/ (float clocksum) 60.)
  12173. 'add_times))
  12174. props))
  12175. (unless (assoc "CATEGORY" props)
  12176. (setq value (or (org-get-category)
  12177. (progn (org-refresh-category-properties)
  12178. (org-get-category))))
  12179. (push (cons "CATEGORY" value) props))
  12180. (append sum-props (nreverse props)))))))
  12181. (defun org-entry-get (pom property &optional inherit literal-nil)
  12182. "Get value of PROPERTY for entry at point-or-marker POM.
  12183. If INHERIT is non-nil and the entry does not have the property,
  12184. then also check higher levels of the hierarchy.
  12185. If INHERIT is the symbol `selective', use inheritance only if the setting
  12186. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12187. If the property is present but empty, the return value is the empty string.
  12188. If the property is not present at all, nil is returned.
  12189. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12190. do not interpret it as the list atom nil. This is used for inheritance
  12191. when a \"nil\" value can supercede a non-nil value higher up the hierarchy."
  12192. (org-with-point-at pom
  12193. (if (and inherit (if (eq inherit 'selective)
  12194. (org-property-inherit-p property)
  12195. t))
  12196. (org-entry-get-with-inheritance property)
  12197. (if (member property org-special-properties)
  12198. ;; We need a special property. Use `org-entry-properties' to
  12199. ;; retrieve it, but specify the wanted property
  12200. (cdr (assoc property (org-entry-properties nil 'special property)))
  12201. (let ((range (org-get-property-block)))
  12202. (if (and range
  12203. (goto-char (car range))
  12204. (re-search-forward
  12205. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12206. (cdr range) t))
  12207. ;; Found the property, return it.
  12208. (if (match-end 1)
  12209. (if literal-nil
  12210. (org-match-string-no-properties 1)
  12211. (org-not-nil (org-match-string-no-properties 1)))
  12212. "")))))))
  12213. (defun org-property-or-variable-value (var &optional inherit)
  12214. "Check if there is a property fixing the value of VAR.
  12215. If yes, return this value. If not, return the current value of the variable."
  12216. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12217. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12218. (read prop)
  12219. (symbol-value var))))
  12220. (defun org-entry-delete (pom property)
  12221. "Delete the property PROPERTY from entry at point-or-marker POM."
  12222. (org-with-point-at pom
  12223. (if (member property org-special-properties)
  12224. nil ; cannot delete these properties.
  12225. (let ((range (org-get-property-block)))
  12226. (if (and range
  12227. (goto-char (car range))
  12228. (re-search-forward
  12229. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12230. (cdr range) t))
  12231. (progn
  12232. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12233. t)
  12234. nil)))))
  12235. ;; Multi-values properties are properties that contain multiple values
  12236. ;; These values are assumed to be single words, separated by whitespace.
  12237. (defun org-entry-add-to-multivalued-property (pom property value)
  12238. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12239. (let* ((old (org-entry-get pom property))
  12240. (values (and old (org-split-string old "[ \t]"))))
  12241. (setq value (org-entry-protect-space value))
  12242. (unless (member value values)
  12243. (setq values (cons value values))
  12244. (org-entry-put pom property
  12245. (mapconcat 'identity values " ")))))
  12246. (defun org-entry-remove-from-multivalued-property (pom property value)
  12247. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12248. (let* ((old (org-entry-get pom property))
  12249. (values (and old (org-split-string old "[ \t]"))))
  12250. (setq value (org-entry-protect-space value))
  12251. (when (member value values)
  12252. (setq values (delete value values))
  12253. (org-entry-put pom property
  12254. (mapconcat 'identity values " ")))))
  12255. (defun org-entry-member-in-multivalued-property (pom property value)
  12256. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12257. (let* ((old (org-entry-get pom property))
  12258. (values (and old (org-split-string old "[ \t]"))))
  12259. (setq value (org-entry-protect-space value))
  12260. (member value values)))
  12261. (defun org-entry-get-multivalued-property (pom property)
  12262. "Return a list of values in a multivalued property."
  12263. (let* ((value (org-entry-get pom property))
  12264. (values (and value (org-split-string value "[ \t]"))))
  12265. (mapcar 'org-entry-restore-space values)))
  12266. (defun org-entry-put-multivalued-property (pom property &rest values)
  12267. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12268. VALUES should be a list of strings. Spaces will be protected."
  12269. (org-entry-put pom property
  12270. (mapconcat 'org-entry-protect-space values " "))
  12271. (let* ((value (org-entry-get pom property))
  12272. (values (and value (org-split-string value "[ \t]"))))
  12273. (mapcar 'org-entry-restore-space values)))
  12274. (defun org-entry-protect-space (s)
  12275. "Protect spaces and newline in string S."
  12276. (while (string-match " " s)
  12277. (setq s (replace-match "%20" t t s)))
  12278. (while (string-match "\n" s)
  12279. (setq s (replace-match "%0A" t t s)))
  12280. s)
  12281. (defun org-entry-restore-space (s)
  12282. "Restore spaces and newline in string S."
  12283. (while (string-match "%20" s)
  12284. (setq s (replace-match " " t t s)))
  12285. (while (string-match "%0A" s)
  12286. (setq s (replace-match "\n" t t s)))
  12287. s)
  12288. (defvar org-entry-property-inherited-from (make-marker)
  12289. "Marker pointing to the entry from where a property was inherited.
  12290. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12291. location of the entry where the inheritance search matched. If there was
  12292. no match, the marker will point nowhere.
  12293. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12294. is set.")
  12295. (defun org-entry-get-with-inheritance (property)
  12296. "Get entry property, and search higher levels if not present."
  12297. (move-marker org-entry-property-inherited-from nil)
  12298. (let (tmp)
  12299. (save-excursion
  12300. (save-restriction
  12301. (widen)
  12302. (catch 'ex
  12303. (while t
  12304. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12305. (org-back-to-heading t)
  12306. (move-marker org-entry-property-inherited-from (point))
  12307. (throw 'ex tmp))
  12308. (or (org-up-heading-safe) (throw 'ex nil)))))
  12309. (org-not-nil
  12310. (or tmp
  12311. (cdr (assoc property org-file-properties))
  12312. (cdr (assoc property org-global-properties))
  12313. (cdr (assoc property org-global-properties-fixed)))))))
  12314. (defvar org-property-changed-functions nil
  12315. "Hook called when the value of a property has changed.
  12316. Each hook function should accept two arguments, the name of the property
  12317. and the new value.")
  12318. (defun org-entry-put (pom property value)
  12319. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12320. (org-with-point-at pom
  12321. (org-back-to-heading t)
  12322. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12323. range)
  12324. (cond
  12325. ((equal property "TODO")
  12326. (when (and (stringp value) (string-match "\\S-" value)
  12327. (not (member value org-todo-keywords-1)))
  12328. (error "\"%s\" is not a valid TODO state" value))
  12329. (if (or (not value)
  12330. (not (string-match "\\S-" value)))
  12331. (setq value 'none))
  12332. (org-todo value)
  12333. (org-set-tags nil 'align))
  12334. ((equal property "PRIORITY")
  12335. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12336. (string-to-char value) ?\ ))
  12337. (org-set-tags nil 'align))
  12338. ((equal property "SCHEDULED")
  12339. (if (re-search-forward org-scheduled-time-regexp end t)
  12340. (cond
  12341. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12342. ((eq value 'later) (org-timestamp-change 1 'day))
  12343. (t (call-interactively 'org-schedule)))
  12344. (call-interactively 'org-schedule)))
  12345. ((equal property "DEADLINE")
  12346. (if (re-search-forward org-deadline-time-regexp end t)
  12347. (cond
  12348. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12349. ((eq value 'later) (org-timestamp-change 1 'day))
  12350. (t (call-interactively 'org-deadline)))
  12351. (call-interactively 'org-deadline)))
  12352. ((member property org-special-properties)
  12353. (error "The %s property can not yet be set with `org-entry-put'"
  12354. property))
  12355. (t ; a non-special property
  12356. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12357. (setq range (org-get-property-block beg end 'force))
  12358. (goto-char (car range))
  12359. (if (re-search-forward
  12360. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12361. (progn
  12362. (delete-region (match-beginning 1) (match-end 1))
  12363. (goto-char (match-beginning 1)))
  12364. (goto-char (cdr range))
  12365. (insert "\n")
  12366. (backward-char 1)
  12367. (org-indent-line-function)
  12368. (insert ":" property ":"))
  12369. (and value (insert " " value))
  12370. (org-indent-line-function)))))
  12371. (run-hook-with-args 'org-property-changed-functions property value)))
  12372. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12373. "Get all property keys in the current buffer.
  12374. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12375. like tags and TODO state.
  12376. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12377. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12378. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12379. formats in the current buffer."
  12380. (let (rtn range cfmt s p)
  12381. (save-excursion
  12382. (save-restriction
  12383. (widen)
  12384. (goto-char (point-min))
  12385. (while (re-search-forward org-property-start-re nil t)
  12386. (setq range (org-get-property-block))
  12387. (goto-char (car range))
  12388. (while (re-search-forward
  12389. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12390. (cdr range) t)
  12391. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12392. (outline-next-heading))))
  12393. (when include-specials
  12394. (setq rtn (append org-special-properties rtn)))
  12395. (when include-defaults
  12396. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12397. (add-to-list 'rtn org-effort-property))
  12398. (when include-columns
  12399. (save-excursion
  12400. (save-restriction
  12401. (widen)
  12402. (goto-char (point-min))
  12403. (while (re-search-forward
  12404. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12405. nil t)
  12406. (setq cfmt (match-string 2) s 0)
  12407. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12408. cfmt s)
  12409. (setq s (match-end 0)
  12410. p (match-string 1 cfmt))
  12411. (unless (or (equal p "ITEM")
  12412. (member p org-special-properties))
  12413. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12414. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12415. (defun org-property-values (key)
  12416. "Return a list of all values of property KEY."
  12417. (save-excursion
  12418. (save-restriction
  12419. (widen)
  12420. (goto-char (point-min))
  12421. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12422. values)
  12423. (while (re-search-forward re nil t)
  12424. (add-to-list 'values (org-trim (match-string 1))))
  12425. (delete "" values)))))
  12426. (defun org-insert-property-drawer ()
  12427. "Insert a property drawer into the current entry."
  12428. (interactive)
  12429. (org-back-to-heading t)
  12430. (looking-at outline-regexp)
  12431. (let ((indent (if org-adapt-indentation
  12432. (- (match-end 0)(match-beginning 0))
  12433. 0))
  12434. (beg (point))
  12435. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12436. end hiddenp)
  12437. (outline-next-heading)
  12438. (setq end (point))
  12439. (goto-char beg)
  12440. (while (re-search-forward re end t))
  12441. (setq hiddenp (org-invisible-p))
  12442. (end-of-line 1)
  12443. (and (equal (char-after) ?\n) (forward-char 1))
  12444. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12445. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12446. ;; just skip this line
  12447. (beginning-of-line 2)
  12448. ;; Drawer start, find the end
  12449. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12450. (beginning-of-line 1)))
  12451. (org-skip-over-state-notes)
  12452. (skip-chars-backward " \t\n\r")
  12453. (if (eq (char-before) ?*) (forward-char 1))
  12454. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12455. (beginning-of-line 0)
  12456. (org-indent-to-column indent)
  12457. (beginning-of-line 2)
  12458. (org-indent-to-column indent)
  12459. (beginning-of-line 0)
  12460. (if hiddenp
  12461. (save-excursion
  12462. (org-back-to-heading t)
  12463. (hide-entry))
  12464. (org-flag-drawer t))))
  12465. (defun org-set-property (property value)
  12466. "In the current entry, set PROPERTY to VALUE.
  12467. When called interactively, this will prompt for a property name, offering
  12468. completion on existing and default properties. And then it will prompt
  12469. for a value, offering completion either on allowed values (via an inherited
  12470. xxx_ALL property) or on existing values in other instances of this property
  12471. in the current file."
  12472. (interactive
  12473. (let* ((completion-ignore-case t)
  12474. (keys (org-buffer-property-keys nil t t))
  12475. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12476. (prop (if (member prop0 keys)
  12477. prop0
  12478. (or (cdr (assoc (downcase prop0)
  12479. (mapcar (lambda (x) (cons (downcase x) x))
  12480. keys)))
  12481. prop0)))
  12482. (cur (org-entry-get nil prop))
  12483. (prompt (concat prop " value"
  12484. (if (and cur (string-match "\\S-" cur))
  12485. (concat " [" cur "]") "") ": "))
  12486. (allowed (org-property-get-allowed-values nil prop 'table))
  12487. (existing (mapcar 'list (org-property-values prop)))
  12488. (val (if allowed
  12489. (org-completing-read prompt allowed nil
  12490. (not (get-text-property 0 'org-unrestricted
  12491. (caar allowed))))
  12492. (let (org-completion-use-ido org-completion-use-iswitchb)
  12493. (org-completing-read prompt existing nil nil "" nil cur)))))
  12494. (list prop (if (equal val "") cur val))))
  12495. (unless (equal (org-entry-get nil property) value)
  12496. (org-entry-put nil property value)))
  12497. (defun org-delete-property (property)
  12498. "In the current entry, delete PROPERTY."
  12499. (interactive
  12500. (let* ((completion-ignore-case t)
  12501. (prop (org-icompleting-read "Property: "
  12502. (org-entry-properties nil 'standard))))
  12503. (list prop)))
  12504. (message "Property %s %s" property
  12505. (if (org-entry-delete nil property)
  12506. "deleted"
  12507. "was not present in the entry")))
  12508. (defun org-delete-property-globally (property)
  12509. "Remove PROPERTY globally, from all entries."
  12510. (interactive
  12511. (let* ((completion-ignore-case t)
  12512. (prop (org-icompleting-read
  12513. "Globally remove property: "
  12514. (mapcar 'list (org-buffer-property-keys)))))
  12515. (list prop)))
  12516. (save-excursion
  12517. (save-restriction
  12518. (widen)
  12519. (goto-char (point-min))
  12520. (let ((cnt 0))
  12521. (while (re-search-forward
  12522. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12523. nil t)
  12524. (setq cnt (1+ cnt))
  12525. (replace-match ""))
  12526. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12527. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12528. (defun org-compute-property-at-point ()
  12529. "Compute the property at point.
  12530. This looks for an enclosing column format, extracts the operator and
  12531. then applies it to the property in the column format's scope."
  12532. (interactive)
  12533. (unless (org-at-property-p)
  12534. (error "Not at a property"))
  12535. (let ((prop (org-match-string-no-properties 2)))
  12536. (org-columns-get-format-and-top-level)
  12537. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12538. (error "No operator defined for property %s" prop))
  12539. (org-columns-compute prop)))
  12540. (defvar org-property-allowed-value-functions nil
  12541. "Hook for functions supplying allowed values for a specific property.
  12542. The functions must take a single argument, the name of the property, and
  12543. return a flat list of allowed values. If \":ETC\" is one of
  12544. the values, this means that these values are intended as defaults for
  12545. completion, but that other values should be allowed too.
  12546. The functions must return nil if they are not responsible for this
  12547. property.")
  12548. (defun org-property-get-allowed-values (pom property &optional table)
  12549. "Get allowed values for the property PROPERTY.
  12550. When TABLE is non-nil, return an alist that can directly be used for
  12551. completion."
  12552. (let (vals)
  12553. (cond
  12554. ((equal property "TODO")
  12555. (setq vals (org-with-point-at pom
  12556. (append org-todo-keywords-1 '("")))))
  12557. ((equal property "PRIORITY")
  12558. (let ((n org-lowest-priority))
  12559. (while (>= n org-highest-priority)
  12560. (push (char-to-string n) vals)
  12561. (setq n (1- n)))))
  12562. ((member property org-special-properties))
  12563. ((setq vals (run-hook-with-args-until-success
  12564. 'org-property-allowed-value-functions property)))
  12565. (t
  12566. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12567. (when (and vals (string-match "\\S-" vals))
  12568. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12569. (setq vals (mapcar (lambda (x)
  12570. (cond ((stringp x) x)
  12571. ((numberp x) (number-to-string x))
  12572. ((symbolp x) (symbol-name x))
  12573. (t "???")))
  12574. vals)))))
  12575. (when (member ":ETC" vals)
  12576. (setq vals (remove ":ETC" vals))
  12577. (org-add-props (car vals) '(org-unrestricted t)))
  12578. (if table (mapcar 'list vals) vals)))
  12579. (defun org-property-previous-allowed-value (&optional previous)
  12580. "Switch to the next allowed value for this property."
  12581. (interactive)
  12582. (org-property-next-allowed-value t))
  12583. (defun org-property-next-allowed-value (&optional previous)
  12584. "Switch to the next allowed value for this property."
  12585. (interactive)
  12586. (unless (org-at-property-p)
  12587. (error "Not at a property"))
  12588. (let* ((key (match-string 2))
  12589. (value (match-string 3))
  12590. (allowed (or (org-property-get-allowed-values (point) key)
  12591. (and (member value '("[ ]" "[-]" "[X]"))
  12592. '("[ ]" "[X]"))))
  12593. nval)
  12594. (unless allowed
  12595. (error "Allowed values for this property have not been defined"))
  12596. (if previous (setq allowed (reverse allowed)))
  12597. (if (member value allowed)
  12598. (setq nval (car (cdr (member value allowed)))))
  12599. (setq nval (or nval (car allowed)))
  12600. (if (equal nval value)
  12601. (error "Only one allowed value for this property"))
  12602. (org-at-property-p)
  12603. (replace-match (concat " :" key ": " nval) t t)
  12604. (org-indent-line-function)
  12605. (beginning-of-line 1)
  12606. (skip-chars-forward " \t")
  12607. (run-hook-with-args 'org-property-changed-functions key nval)))
  12608. (defun org-find-olp (path &optional this-buffer)
  12609. "Return a marker pointing to the entry at outline path OLP.
  12610. If anything goes wrong, throw an error.
  12611. You can wrap this call to cathc the error like this:
  12612. (condition-case msg
  12613. (org-mobile-locate-entry (match-string 4))
  12614. (error (nth 1 msg)))
  12615. The return value will then be either a string with the error message,
  12616. or a marker if everyhing is OK.
  12617. If THIS-BUFFER is set, the putline path does not contain a file,
  12618. only headings."
  12619. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12620. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12621. (level 1)
  12622. (lmin 1)
  12623. (lmax 1)
  12624. limit re end found pos heading cnt)
  12625. (unless buffer (error "File not found :%s" file))
  12626. (with-current-buffer buffer
  12627. (save-excursion
  12628. (save-restriction
  12629. (widen)
  12630. (setq limit (point-max))
  12631. (goto-char (point-min))
  12632. (while (setq heading (pop path))
  12633. (setq re (format org-complex-heading-regexp-format
  12634. (regexp-quote heading)))
  12635. (setq cnt 0 pos (point))
  12636. (while (re-search-forward re end t)
  12637. (setq level (- (match-end 1) (match-beginning 1)))
  12638. (if (and (>= level lmin) (<= level lmax))
  12639. (setq found (match-beginning 0) cnt (1+ cnt))))
  12640. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12641. lmax heading))
  12642. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  12643. lmax heading))
  12644. (goto-char found)
  12645. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  12646. (setq end (save-excursion (org-end-of-subtree t t))))
  12647. (when (org-on-heading-p)
  12648. (move-marker (make-marker) (point))))))))
  12649. (defun org-find-entry-with-id (ident)
  12650. "Locate the entry that contains the ID property with exact value IDENT.
  12651. IDENT can be a string, a symbol or a number, this function will search for
  12652. the string representation of it.
  12653. Return the position where this entry starts, or nil if there is no such entry."
  12654. (interactive "sID: ")
  12655. (let ((id (cond
  12656. ((stringp ident) ident)
  12657. ((symbol-name ident) (symbol-name ident))
  12658. ((numberp ident) (number-to-string ident))
  12659. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12660. (case-fold-search nil))
  12661. (save-excursion
  12662. (save-restriction
  12663. (widen)
  12664. (goto-char (point-min))
  12665. (when (re-search-forward
  12666. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12667. nil t)
  12668. (org-back-to-heading t)
  12669. (point))))))
  12670. ;;;; Timestamps
  12671. (defvar org-last-changed-timestamp nil)
  12672. (defvar org-last-inserted-timestamp nil
  12673. "The last time stamp inserted with `org-insert-time-stamp'.")
  12674. (defvar org-time-was-given) ; dynamically scoped parameter
  12675. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12676. (defvar org-ts-what) ; dynamically scoped parameter
  12677. (defun org-time-stamp (arg &optional inactive)
  12678. "Prompt for a date/time and insert a time stamp.
  12679. If the user specifies a time like HH:MM, or if this command is called
  12680. with a prefix argument, the time stamp will contain date and time.
  12681. Otherwise, only the date will be included. All parts of a date not
  12682. specified by the user will be filled in from the current date/time.
  12683. So if you press just return without typing anything, the time stamp
  12684. will represent the current date/time. If there is already a timestamp
  12685. at the cursor, it will be modified."
  12686. (interactive "P")
  12687. (let* ((ts nil)
  12688. (default-time
  12689. ;; Default time is either today, or, when entering a range,
  12690. ;; the range start.
  12691. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12692. (save-excursion
  12693. (re-search-backward
  12694. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12695. (- (point) 20) t)))
  12696. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12697. (current-time)))
  12698. (default-input (and ts (org-get-compact-tod ts)))
  12699. org-time-was-given org-end-time-was-given time)
  12700. (cond
  12701. ((and (org-at-timestamp-p t)
  12702. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12703. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12704. (insert "--")
  12705. (setq time (let ((this-command this-command))
  12706. (org-read-date arg 'totime nil nil
  12707. default-time default-input)))
  12708. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12709. ((org-at-timestamp-p t)
  12710. (setq time (let ((this-command this-command))
  12711. (org-read-date arg 'totime nil nil default-time default-input)))
  12712. (when (org-at-timestamp-p t) ; just to get the match data
  12713. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12714. (replace-match "")
  12715. (setq org-last-changed-timestamp
  12716. (org-insert-time-stamp
  12717. time (or org-time-was-given arg)
  12718. inactive nil nil (list org-end-time-was-given))))
  12719. (message "Timestamp updated"))
  12720. (t
  12721. (setq time (let ((this-command this-command))
  12722. (org-read-date arg 'totime nil nil default-time default-input)))
  12723. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12724. nil nil (list org-end-time-was-given))))))
  12725. ;; FIXME: can we use this for something else, like computing time differences?
  12726. (defun org-get-compact-tod (s)
  12727. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12728. (let* ((t1 (match-string 1 s))
  12729. (h1 (string-to-number (match-string 2 s)))
  12730. (m1 (string-to-number (match-string 3 s)))
  12731. (t2 (and (match-end 4) (match-string 5 s)))
  12732. (h2 (and t2 (string-to-number (match-string 6 s))))
  12733. (m2 (and t2 (string-to-number (match-string 7 s))))
  12734. dh dm)
  12735. (if (not t2)
  12736. t1
  12737. (setq dh (- h2 h1) dm (- m2 m1))
  12738. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12739. (concat t1 "+" (number-to-string dh)
  12740. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12741. (defun org-time-stamp-inactive (&optional arg)
  12742. "Insert an inactive time stamp.
  12743. An inactive time stamp is enclosed in square brackets instead of angle
  12744. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12745. does not link to the calendar and cannot be changed with the S-cursor keys.
  12746. So these are more for recording a certain time/date."
  12747. (interactive "P")
  12748. (org-time-stamp arg 'inactive))
  12749. (defvar org-date-ovl (make-overlay 1 1))
  12750. (overlay-put org-date-ovl 'face 'org-warning)
  12751. (org-detach-overlay org-date-ovl)
  12752. (defvar org-ans1) ; dynamically scoped parameter
  12753. (defvar org-ans2) ; dynamically scoped parameter
  12754. (defvar org-plain-time-of-day-regexp) ; defined below
  12755. (defvar org-overriding-default-time nil) ; dynamically scoped
  12756. (defvar org-read-date-overlay nil)
  12757. (defvar org-dcst nil) ; dynamically scoped
  12758. (defvar org-read-date-history nil)
  12759. (defvar org-read-date-final-answer nil)
  12760. (defun org-read-date (&optional with-time to-time from-string prompt
  12761. default-time default-input)
  12762. "Read a date, possibly a time, and make things smooth for the user.
  12763. The prompt will suggest to enter an ISO date, but you can also enter anything
  12764. which will at least partially be understood by `parse-time-string'.
  12765. Unrecognized parts of the date will default to the current day, month, year,
  12766. hour and minute. If this command is called to replace a timestamp at point,
  12767. of to enter the second timestamp of a range, the default time is taken
  12768. from the existing stamp. Furthermore, the command prefers the future,
  12769. so if you are giving a date where the year is not given, and the day-month
  12770. combination is already past in the current year, it will assume you
  12771. mean next year. For details, see the manual. A few examples:
  12772. 3-2-5 --> 2003-02-05
  12773. feb 15 --> currentyear-02-15
  12774. 2/15 --> currentyear-02-15
  12775. sep 12 9 --> 2009-09-12
  12776. 12:45 --> today 12:45
  12777. 22 sept 0:34 --> currentyear-09-22 0:34
  12778. 12 --> currentyear-currentmonth-12
  12779. Fri --> nearest Friday (today or later)
  12780. etc.
  12781. Furthermore you can specify a relative date by giving, as the *first* thing
  12782. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12783. change in days weeks, months, years.
  12784. With a single plus or minus, the date is relative to today. With a double
  12785. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12786. +4d --> four days from today
  12787. +4 --> same as above
  12788. +2w --> two weeks from today
  12789. ++5 --> five days from default date
  12790. The function understands only English month and weekday abbreviations,
  12791. but this can be configured with the variables `parse-time-months' and
  12792. `parse-time-weekdays'.
  12793. While prompting, a calendar is popped up - you can also select the
  12794. date with the mouse (button 1). The calendar shows a period of three
  12795. months. To scroll it to other months, use the keys `>' and `<'.
  12796. If you don't like the calendar, turn it off with
  12797. \(setq org-read-date-popup-calendar nil)
  12798. With optional argument TO-TIME, the date will immediately be converted
  12799. to an internal time.
  12800. With an optional argument WITH-TIME, the prompt will suggest to also
  12801. insert a time. Note that when WITH-TIME is not set, you can still
  12802. enter a time, and this function will inform the calling routine about
  12803. this change. The calling routine may then choose to change the format
  12804. used to insert the time stamp into the buffer to include the time.
  12805. With optional argument FROM-STRING, read from this string instead from
  12806. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12807. the time/date that is used for everything that is not specified by the
  12808. user."
  12809. (require 'parse-time)
  12810. (let* ((org-time-stamp-rounding-minutes
  12811. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12812. (org-dcst org-display-custom-times)
  12813. (ct (org-current-time))
  12814. (def (or org-overriding-default-time default-time ct))
  12815. (defdecode (decode-time def))
  12816. (dummy (progn
  12817. (when (< (nth 2 defdecode) org-extend-today-until)
  12818. (setcar (nthcdr 2 defdecode) -1)
  12819. (setcar (nthcdr 1 defdecode) 59)
  12820. (setq def (apply 'encode-time defdecode)
  12821. defdecode (decode-time def)))))
  12822. (calendar-frame-setup nil)
  12823. (calendar-setup nil)
  12824. (calendar-move-hook nil)
  12825. (calendar-view-diary-initially-flag nil)
  12826. (calendar-view-holidays-initially-flag nil)
  12827. (timestr (format-time-string
  12828. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12829. (prompt (concat (if prompt (concat prompt " ") "")
  12830. (format "Date+time [%s]: " timestr)))
  12831. ans (org-ans0 "") org-ans1 org-ans2 final)
  12832. (cond
  12833. (from-string (setq ans from-string))
  12834. (org-read-date-popup-calendar
  12835. (save-excursion
  12836. (save-window-excursion
  12837. (calendar)
  12838. (calendar-forward-day (- (time-to-days def)
  12839. (calendar-absolute-from-gregorian
  12840. (calendar-current-date))))
  12841. (org-eval-in-calendar nil t)
  12842. (let* ((old-map (current-local-map))
  12843. (map (copy-keymap calendar-mode-map))
  12844. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12845. (org-defkey map (kbd "RET") 'org-calendar-select)
  12846. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  12847. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  12848. (org-defkey minibuffer-local-map [(meta shift left)]
  12849. (lambda () (interactive)
  12850. (org-eval-in-calendar '(calendar-backward-month 1))))
  12851. (org-defkey minibuffer-local-map [(meta shift right)]
  12852. (lambda () (interactive)
  12853. (org-eval-in-calendar '(calendar-forward-month 1))))
  12854. (org-defkey minibuffer-local-map [(meta shift up)]
  12855. (lambda () (interactive)
  12856. (org-eval-in-calendar '(calendar-backward-year 1))))
  12857. (org-defkey minibuffer-local-map [(meta shift down)]
  12858. (lambda () (interactive)
  12859. (org-eval-in-calendar '(calendar-forward-year 1))))
  12860. (org-defkey minibuffer-local-map [?\e (shift left)]
  12861. (lambda () (interactive)
  12862. (org-eval-in-calendar '(calendar-backward-month 1))))
  12863. (org-defkey minibuffer-local-map [?\e (shift right)]
  12864. (lambda () (interactive)
  12865. (org-eval-in-calendar '(calendar-forward-month 1))))
  12866. (org-defkey minibuffer-local-map [?\e (shift up)]
  12867. (lambda () (interactive)
  12868. (org-eval-in-calendar '(calendar-backward-year 1))))
  12869. (org-defkey minibuffer-local-map [?\e (shift down)]
  12870. (lambda () (interactive)
  12871. (org-eval-in-calendar '(calendar-forward-year 1))))
  12872. (org-defkey minibuffer-local-map [(shift up)]
  12873. (lambda () (interactive)
  12874. (org-eval-in-calendar '(calendar-backward-week 1))))
  12875. (org-defkey minibuffer-local-map [(shift down)]
  12876. (lambda () (interactive)
  12877. (org-eval-in-calendar '(calendar-forward-week 1))))
  12878. (org-defkey minibuffer-local-map [(shift left)]
  12879. (lambda () (interactive)
  12880. (org-eval-in-calendar '(calendar-backward-day 1))))
  12881. (org-defkey minibuffer-local-map [(shift right)]
  12882. (lambda () (interactive)
  12883. (org-eval-in-calendar '(calendar-forward-day 1))))
  12884. (org-defkey minibuffer-local-map ">"
  12885. (lambda () (interactive)
  12886. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12887. (org-defkey minibuffer-local-map "<"
  12888. (lambda () (interactive)
  12889. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12890. (org-defkey minibuffer-local-map "\C-v"
  12891. (lambda () (interactive)
  12892. (org-eval-in-calendar
  12893. '(calendar-scroll-left-three-months 1))))
  12894. (org-defkey minibuffer-local-map "\M-v"
  12895. (lambda () (interactive)
  12896. (org-eval-in-calendar
  12897. '(calendar-scroll-right-three-months 1))))
  12898. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12899. (unwind-protect
  12900. (progn
  12901. (use-local-map map)
  12902. (add-hook 'post-command-hook 'org-read-date-display)
  12903. (setq org-ans0 (read-string prompt default-input
  12904. 'org-read-date-history nil))
  12905. ;; org-ans0: from prompt
  12906. ;; org-ans1: from mouse click
  12907. ;; org-ans2: from calendar motion
  12908. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12909. (remove-hook 'post-command-hook 'org-read-date-display)
  12910. (use-local-map old-map)
  12911. (when org-read-date-overlay
  12912. (delete-overlay org-read-date-overlay)
  12913. (setq org-read-date-overlay nil)))))))
  12914. (t ; Naked prompt only
  12915. (unwind-protect
  12916. (setq ans (read-string prompt default-input
  12917. 'org-read-date-history timestr))
  12918. (when org-read-date-overlay
  12919. (delete-overlay org-read-date-overlay)
  12920. (setq org-read-date-overlay nil)))))
  12921. (setq final (org-read-date-analyze ans def defdecode))
  12922. (setq org-read-date-final-answer ans)
  12923. (if to-time
  12924. (apply 'encode-time final)
  12925. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12926. (format "%04d-%02d-%02d %02d:%02d"
  12927. (nth 5 final) (nth 4 final) (nth 3 final)
  12928. (nth 2 final) (nth 1 final))
  12929. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12930. (defvar def)
  12931. (defvar defdecode)
  12932. (defvar with-time)
  12933. (defvar org-read-date-analyze-futurep nil)
  12934. (defun org-read-date-display ()
  12935. "Display the current date prompt interpretation in the minibuffer."
  12936. (when org-read-date-display-live
  12937. (when org-read-date-overlay
  12938. (delete-overlay org-read-date-overlay))
  12939. (let ((p (point)))
  12940. (end-of-line 1)
  12941. (while (not (equal (buffer-substring
  12942. (max (point-min) (- (point) 4)) (point))
  12943. " "))
  12944. (insert " "))
  12945. (goto-char p))
  12946. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12947. " " (or org-ans1 org-ans2)))
  12948. (org-end-time-was-given nil)
  12949. (f (org-read-date-analyze ans def defdecode))
  12950. (fmts (if org-dcst
  12951. org-time-stamp-custom-formats
  12952. org-time-stamp-formats))
  12953. (fmt (if (or with-time
  12954. (and (boundp 'org-time-was-given) org-time-was-given))
  12955. (cdr fmts)
  12956. (car fmts)))
  12957. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12958. (when (and org-end-time-was-given
  12959. (string-match org-plain-time-of-day-regexp txt))
  12960. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12961. org-end-time-was-given
  12962. (substring txt (match-end 0)))))
  12963. (when org-read-date-analyze-futurep
  12964. (setq txt (concat txt " (=>F)")))
  12965. (setq org-read-date-overlay
  12966. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  12967. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12968. (defun org-read-date-analyze (ans def defdecode)
  12969. "Analyse the combined answer of the date prompt."
  12970. ;; FIXME: cleanup and comment
  12971. (let ((nowdecode (decode-time (current-time)))
  12972. delta deltan deltaw deltadef year month day
  12973. hour minute second wday pm h2 m2 tl wday1
  12974. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12975. (setq org-read-date-analyze-futurep nil)
  12976. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12977. (setq ans "+0"))
  12978. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12979. (setq ans (replace-match "" t t ans)
  12980. deltan (car delta)
  12981. deltaw (nth 1 delta)
  12982. deltadef (nth 2 delta)))
  12983. ;; Check if there is an iso week date in there
  12984. ;; If yes, store the info and postpone interpreting it until the rest
  12985. ;; of the parsing is done
  12986. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12987. (setq iso-year (if (match-end 1)
  12988. (org-small-year-to-year
  12989. (string-to-number (match-string 1 ans))))
  12990. iso-weekday (if (match-end 3)
  12991. (string-to-number (match-string 3 ans)))
  12992. iso-week (string-to-number (match-string 2 ans)))
  12993. (setq ans (replace-match "" t t ans)))
  12994. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12995. (when (string-match
  12996. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12997. (setq year (if (match-end 2)
  12998. (string-to-number (match-string 2 ans))
  12999. (progn (setq kill-year t)
  13000. (string-to-number (format-time-string "%Y"))))
  13001. month (string-to-number (match-string 3 ans))
  13002. day (string-to-number (match-string 4 ans)))
  13003. (if (< year 100) (setq year (+ 2000 year)))
  13004. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13005. t nil ans)))
  13006. ;; Help matching american dates, like 5/30 or 5/30/7
  13007. (when (string-match
  13008. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13009. (setq year (if (match-end 4)
  13010. (string-to-number (match-string 4 ans))
  13011. (progn (setq kill-year t)
  13012. (string-to-number (format-time-string "%Y"))))
  13013. month (string-to-number (match-string 1 ans))
  13014. day (string-to-number (match-string 2 ans)))
  13015. (if (< year 100) (setq year (+ 2000 year)))
  13016. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13017. t nil ans)))
  13018. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13019. ;; If there is a time with am/pm, and *no* time without it, we convert
  13020. ;; so that matching will be successful.
  13021. (loop for i from 1 to 2 do ; twice, for end time as well
  13022. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13023. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13024. (setq hour (string-to-number (match-string 1 ans))
  13025. minute (if (match-end 3)
  13026. (string-to-number (match-string 3 ans))
  13027. 0)
  13028. pm (equal ?p
  13029. (string-to-char (downcase (match-string 4 ans)))))
  13030. (if (and (= hour 12) (not pm))
  13031. (setq hour 0)
  13032. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13033. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13034. t t ans))))
  13035. ;; Check if a time range is given as a duration
  13036. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13037. (setq hour (string-to-number (match-string 1 ans))
  13038. h2 (+ hour (string-to-number (match-string 3 ans)))
  13039. minute (string-to-number (match-string 2 ans))
  13040. m2 (+ minute (if (match-end 5) (string-to-number
  13041. (match-string 5 ans))0)))
  13042. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13043. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13044. t t ans)))
  13045. ;; Check if there is a time range
  13046. (when (boundp 'org-end-time-was-given)
  13047. (setq org-time-was-given nil)
  13048. (when (and (string-match org-plain-time-of-day-regexp ans)
  13049. (match-end 8))
  13050. (setq org-end-time-was-given (match-string 8 ans))
  13051. (setq ans (concat (substring ans 0 (match-beginning 7))
  13052. (substring ans (match-end 7))))))
  13053. (setq tl (parse-time-string ans)
  13054. day (or (nth 3 tl) (nth 3 defdecode))
  13055. month (or (nth 4 tl)
  13056. (if (and org-read-date-prefer-future
  13057. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13058. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13059. (nth 4 defdecode)))
  13060. year (or (and (not kill-year) (nth 5 tl))
  13061. (if (and org-read-date-prefer-future
  13062. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13063. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13064. (nth 5 defdecode)))
  13065. hour (or (nth 2 tl) (nth 2 defdecode))
  13066. minute (or (nth 1 tl) (nth 1 defdecode))
  13067. second (or (nth 0 tl) 0)
  13068. wday (nth 6 tl))
  13069. (when (and (eq org-read-date-prefer-future 'time)
  13070. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13071. (equal day (nth 3 nowdecode))
  13072. (equal month (nth 4 nowdecode))
  13073. (equal year (nth 5 nowdecode))
  13074. (nth 2 tl)
  13075. (or (< (nth 2 tl) (nth 2 nowdecode))
  13076. (and (= (nth 2 tl) (nth 2 nowdecode))
  13077. (nth 1 tl)
  13078. (< (nth 1 tl) (nth 1 nowdecode)))))
  13079. (setq day (1+ day)
  13080. futurep t))
  13081. ;; Special date definitions below
  13082. (cond
  13083. (iso-week
  13084. ;; There was an iso week
  13085. (require 'cal-iso)
  13086. (setq futurep nil)
  13087. (setq year (or iso-year year)
  13088. day (or iso-weekday wday 1)
  13089. wday nil ; to make sure that the trigger below does not match
  13090. iso-date (calendar-gregorian-from-absolute
  13091. (calendar-absolute-from-iso
  13092. (list iso-week day year))))
  13093. ; FIXME: Should we also push ISO weeks into the future?
  13094. ; (when (and org-read-date-prefer-future
  13095. ; (not iso-year)
  13096. ; (< (calendar-absolute-from-gregorian iso-date)
  13097. ; (time-to-days (current-time))))
  13098. ; (setq year (1+ year)
  13099. ; iso-date (calendar-gregorian-from-absolute
  13100. ; (calendar-absolute-from-iso
  13101. ; (list iso-week day year)))))
  13102. (setq month (car iso-date)
  13103. year (nth 2 iso-date)
  13104. day (nth 1 iso-date)))
  13105. (deltan
  13106. (setq futurep nil)
  13107. (unless deltadef
  13108. (let ((now (decode-time (current-time))))
  13109. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13110. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13111. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13112. ((equal deltaw "m") (setq month (+ month deltan)))
  13113. ((equal deltaw "y") (setq year (+ year deltan)))))
  13114. ((and wday (not (nth 3 tl)))
  13115. (setq futurep nil)
  13116. ;; Weekday was given, but no day, so pick that day in the week
  13117. ;; on or after the derived date.
  13118. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13119. (unless (equal wday wday1)
  13120. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13121. (if (and (boundp 'org-time-was-given)
  13122. (nth 2 tl))
  13123. (setq org-time-was-given t))
  13124. (if (< year 100) (setq year (+ 2000 year)))
  13125. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  13126. (setq org-read-date-analyze-futurep futurep)
  13127. (list second minute hour day month year)))
  13128. (defvar parse-time-weekdays)
  13129. (defun org-read-date-get-relative (s today default)
  13130. "Check string S for special relative date string.
  13131. TODAY and DEFAULT are internal times, for today and for a default.
  13132. Return shift list (N what def-flag)
  13133. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13134. N is the number of WHATs to shift.
  13135. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13136. the DEFAULT date rather than TODAY."
  13137. (when (and
  13138. (string-match
  13139. (concat
  13140. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13141. "\\([0-9]+\\)?"
  13142. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13143. "\\([ \t]\\|$\\)") s)
  13144. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13145. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13146. (string-to-char (substring (match-string 1 s) -1))
  13147. ?+))
  13148. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13149. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13150. (what (if (match-end 3) (match-string 3 s) "d"))
  13151. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13152. (date (if rel default today))
  13153. (wday (nth 6 (decode-time date)))
  13154. delta)
  13155. (if wday1
  13156. (progn
  13157. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13158. (if (= dir ?-) (setq delta (- delta 7)))
  13159. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13160. (list delta "d" rel))
  13161. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13162. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13163. "Turn a user-specified date into the internal representation.
  13164. The internal representation needed by the calendar is (month day year).
  13165. This is a wrapper to handle the brain-dead convention in calendar that
  13166. user function argument order change dependent on argument order."
  13167. (if (boundp 'calendar-date-style)
  13168. (cond
  13169. ((eq calendar-date-style 'american)
  13170. (list arg1 arg2 arg3))
  13171. ((eq calendar-date-style 'european)
  13172. (list arg2 arg1 arg3))
  13173. ((eq calendar-date-style 'iso)
  13174. (list arg2 arg3 arg1)))
  13175. (if (org-bound-and-true-p european-calendar-style)
  13176. (list arg2 arg1 arg3)
  13177. (list arg1 arg2 arg3))))
  13178. (defun org-eval-in-calendar (form &optional keepdate)
  13179. "Eval FORM in the calendar window and return to current window.
  13180. Also, store the cursor date in variable org-ans2."
  13181. (let ((sf (selected-frame))
  13182. (sw (selected-window)))
  13183. (select-window (get-buffer-window "*Calendar*" t))
  13184. (eval form)
  13185. (when (and (not keepdate) (calendar-cursor-to-date))
  13186. (let* ((date (calendar-cursor-to-date))
  13187. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13188. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13189. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13190. (select-window sw)
  13191. (org-select-frame-set-input-focus sf)))
  13192. (defun org-calendar-select ()
  13193. "Return to `org-read-date' with the date currently selected.
  13194. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13195. (interactive)
  13196. (when (calendar-cursor-to-date)
  13197. (let* ((date (calendar-cursor-to-date))
  13198. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13199. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13200. (if (active-minibuffer-window) (exit-minibuffer))))
  13201. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13202. "Insert a date stamp for the date given by the internal TIME.
  13203. WITH-HM means use the stamp format that includes the time of the day.
  13204. INACTIVE means use square brackets instead of angular ones, so that the
  13205. stamp will not contribute to the agenda.
  13206. PRE and POST are optional strings to be inserted before and after the
  13207. stamp.
  13208. The command returns the inserted time stamp."
  13209. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13210. stamp)
  13211. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13212. (insert-before-markers (or pre ""))
  13213. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13214. (when (listp extra)
  13215. (setq extra (car extra))
  13216. (if (and (stringp extra)
  13217. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13218. (setq extra (format "-%02d:%02d"
  13219. (string-to-number (match-string 1 extra))
  13220. (string-to-number (match-string 2 extra))))
  13221. (setq extra nil)))
  13222. (when extra
  13223. (backward-char 1)
  13224. (insert-before-markers extra)
  13225. (forward-char 1))
  13226. (insert-before-markers (or post ""))
  13227. (setq org-last-inserted-timestamp stamp)))
  13228. (defun org-toggle-time-stamp-overlays ()
  13229. "Toggle the use of custom time stamp formats."
  13230. (interactive)
  13231. (setq org-display-custom-times (not org-display-custom-times))
  13232. (unless org-display-custom-times
  13233. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13234. (while (setq p (next-single-property-change p 'display))
  13235. (if (and (get-text-property p 'display)
  13236. (eq (get-text-property p 'face) 'org-date))
  13237. (remove-text-properties
  13238. p (setq p (next-single-property-change p 'display))
  13239. '(display t))))
  13240. (set-buffer-modified-p bmp)))
  13241. (if (featurep 'xemacs)
  13242. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13243. (org-restart-font-lock)
  13244. (setq org-table-may-need-update t)
  13245. (if org-display-custom-times
  13246. (message "Time stamps are overlayed with custom format")
  13247. (message "Time stamp overlays removed")))
  13248. (defun org-display-custom-time (beg end)
  13249. "Overlay modified time stamp format over timestamp between BEG and END."
  13250. (let* ((ts (buffer-substring beg end))
  13251. t1 w1 with-hm tf time str w2 (off 0))
  13252. (save-match-data
  13253. (setq t1 (org-parse-time-string ts t))
  13254. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13255. (setq off (- (match-end 0) (match-beginning 0)))))
  13256. (setq end (- end off))
  13257. (setq w1 (- end beg)
  13258. with-hm (and (nth 1 t1) (nth 2 t1))
  13259. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13260. time (org-fix-decoded-time t1)
  13261. str (org-add-props
  13262. (format-time-string
  13263. (substring tf 1 -1) (apply 'encode-time time))
  13264. nil 'mouse-face 'highlight)
  13265. w2 (length str))
  13266. (if (not (= w2 w1))
  13267. (add-text-properties (1+ beg) (+ 2 beg)
  13268. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13269. (if (featurep 'xemacs)
  13270. (progn
  13271. (put-text-property beg end 'invisible t)
  13272. (put-text-property beg end 'end-glyph (make-glyph str)))
  13273. (put-text-property beg end 'display str))))
  13274. (defun org-translate-time (string)
  13275. "Translate all timestamps in STRING to custom format.
  13276. But do this only if the variable `org-display-custom-times' is set."
  13277. (when org-display-custom-times
  13278. (save-match-data
  13279. (let* ((start 0)
  13280. (re org-ts-regexp-both)
  13281. t1 with-hm inactive tf time str beg end)
  13282. (while (setq start (string-match re string start))
  13283. (setq beg (match-beginning 0)
  13284. end (match-end 0)
  13285. t1 (save-match-data
  13286. (org-parse-time-string (substring string beg end) t))
  13287. with-hm (and (nth 1 t1) (nth 2 t1))
  13288. inactive (equal (substring string beg (1+ beg)) "[")
  13289. tf (funcall (if with-hm 'cdr 'car)
  13290. org-time-stamp-custom-formats)
  13291. time (org-fix-decoded-time t1)
  13292. str (format-time-string
  13293. (concat
  13294. (if inactive "[" "<") (substring tf 1 -1)
  13295. (if inactive "]" ">"))
  13296. (apply 'encode-time time))
  13297. string (replace-match str t t string)
  13298. start (+ start (length str)))))))
  13299. string)
  13300. (defun org-fix-decoded-time (time)
  13301. "Set 0 instead of nil for the first 6 elements of time.
  13302. Don't touch the rest."
  13303. (let ((n 0))
  13304. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13305. (defun org-days-to-time (timestamp-string)
  13306. "Difference between TIMESTAMP-STRING and now in days."
  13307. (- (time-to-days (org-time-string-to-time timestamp-string))
  13308. (time-to-days (current-time))))
  13309. (defun org-deadline-close (timestamp-string &optional ndays)
  13310. "Is the time in TIMESTAMP-STRING close to the current date?"
  13311. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13312. (and (< (org-days-to-time timestamp-string) ndays)
  13313. (not (org-entry-is-done-p))))
  13314. (defun org-get-wdays (ts)
  13315. "Get the deadline lead time appropriate for timestring TS."
  13316. (cond
  13317. ((<= org-deadline-warning-days 0)
  13318. ;; 0 or negative, enforce this value no matter what
  13319. (- org-deadline-warning-days))
  13320. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13321. ;; lead time is specified.
  13322. (floor (* (string-to-number (match-string 1 ts))
  13323. (cdr (assoc (match-string 2 ts)
  13324. '(("d" . 1) ("w" . 7)
  13325. ("m" . 30.4) ("y" . 365.25)))))))
  13326. ;; go for the default.
  13327. (t org-deadline-warning-days)))
  13328. (defun org-calendar-select-mouse (ev)
  13329. "Return to `org-read-date' with the date currently selected.
  13330. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13331. (interactive "e")
  13332. (mouse-set-point ev)
  13333. (when (calendar-cursor-to-date)
  13334. (let* ((date (calendar-cursor-to-date))
  13335. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13336. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13337. (if (active-minibuffer-window) (exit-minibuffer))))
  13338. (defun org-check-deadlines (ndays)
  13339. "Check if there are any deadlines due or past due.
  13340. A deadline is considered due if it happens within `org-deadline-warning-days'
  13341. days from today's date. If the deadline appears in an entry marked DONE,
  13342. it is not shown. The prefix arg NDAYS can be used to test that many
  13343. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13344. (interactive "P")
  13345. (let* ((org-warn-days
  13346. (cond
  13347. ((equal ndays '(4)) 100000)
  13348. (ndays (prefix-numeric-value ndays))
  13349. (t (abs org-deadline-warning-days))))
  13350. (case-fold-search nil)
  13351. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13352. (callback
  13353. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13354. (message "%d deadlines past-due or due within %d days"
  13355. (org-occur regexp nil callback)
  13356. org-warn-days)))
  13357. (defun org-check-before-date (date)
  13358. "Check if there are deadlines or scheduled entries before DATE."
  13359. (interactive (list (org-read-date)))
  13360. (let ((case-fold-search nil)
  13361. (regexp (concat "\\<\\(" org-deadline-string
  13362. "\\|" org-scheduled-string
  13363. "\\) *<\\([^>]+\\)>"))
  13364. (callback
  13365. (lambda () (time-less-p
  13366. (org-time-string-to-time (match-string 2))
  13367. (org-time-string-to-time date)))))
  13368. (message "%d entries before %s"
  13369. (org-occur regexp nil callback) date)))
  13370. (defun org-check-after-date (date)
  13371. "Check if there are deadlines or scheduled entries after DATE."
  13372. (interactive (list (org-read-date)))
  13373. (let ((case-fold-search nil)
  13374. (regexp (concat "\\<\\(" org-deadline-string
  13375. "\\|" org-scheduled-string
  13376. "\\) *<\\([^>]+\\)>"))
  13377. (callback
  13378. (lambda () (not
  13379. (time-less-p
  13380. (org-time-string-to-time (match-string 2))
  13381. (org-time-string-to-time date))))))
  13382. (message "%d entries after %s"
  13383. (org-occur regexp nil callback) date)))
  13384. (defun org-evaluate-time-range (&optional to-buffer)
  13385. "Evaluate a time range by computing the difference between start and end.
  13386. Normally the result is just printed in the echo area, but with prefix arg
  13387. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13388. If the time range is actually in a table, the result is inserted into the
  13389. next column.
  13390. For time difference computation, a year is assumed to be exactly 365
  13391. days in order to avoid rounding problems."
  13392. (interactive "P")
  13393. (or
  13394. (org-clock-update-time-maybe)
  13395. (save-excursion
  13396. (unless (org-at-date-range-p t)
  13397. (goto-char (point-at-bol))
  13398. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13399. (if (not (org-at-date-range-p t))
  13400. (error "Not at a time-stamp range, and none found in current line")))
  13401. (let* ((ts1 (match-string 1))
  13402. (ts2 (match-string 2))
  13403. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13404. (match-end (match-end 0))
  13405. (time1 (org-time-string-to-time ts1))
  13406. (time2 (org-time-string-to-time ts2))
  13407. (t1 (org-float-time time1))
  13408. (t2 (org-float-time time2))
  13409. (diff (abs (- t2 t1)))
  13410. (negative (< (- t2 t1) 0))
  13411. ;; (ys (floor (* 365 24 60 60)))
  13412. (ds (* 24 60 60))
  13413. (hs (* 60 60))
  13414. (fy "%dy %dd %02d:%02d")
  13415. (fy1 "%dy %dd")
  13416. (fd "%dd %02d:%02d")
  13417. (fd1 "%dd")
  13418. (fh "%02d:%02d")
  13419. y d h m align)
  13420. (if havetime
  13421. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13422. y 0
  13423. d (floor (/ diff ds)) diff (mod diff ds)
  13424. h (floor (/ diff hs)) diff (mod diff hs)
  13425. m (floor (/ diff 60)))
  13426. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13427. y 0
  13428. d (floor (+ (/ diff ds) 0.5))
  13429. h 0 m 0))
  13430. (if (not to-buffer)
  13431. (message "%s" (org-make-tdiff-string y d h m))
  13432. (if (org-at-table-p)
  13433. (progn
  13434. (goto-char match-end)
  13435. (setq align t)
  13436. (and (looking-at " *|") (goto-char (match-end 0))))
  13437. (goto-char match-end))
  13438. (if (looking-at
  13439. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13440. (replace-match ""))
  13441. (if negative (insert " -"))
  13442. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13443. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13444. (insert " " (format fh h m))))
  13445. (if align (org-table-align))
  13446. (message "Time difference inserted")))))
  13447. (defun org-make-tdiff-string (y d h m)
  13448. (let ((fmt "")
  13449. (l nil))
  13450. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13451. l (push y l)))
  13452. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13453. l (push d l)))
  13454. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13455. l (push h l)))
  13456. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13457. l (push m l)))
  13458. (apply 'format fmt (nreverse l))))
  13459. (defun org-time-string-to-time (s)
  13460. (apply 'encode-time (org-parse-time-string s)))
  13461. (defun org-time-string-to-seconds (s)
  13462. (org-float-time (org-time-string-to-time s)))
  13463. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13464. "Convert a time stamp to an absolute day number.
  13465. If there is a specifyer for a cyclic time stamp, get the closest date to
  13466. DAYNR.
  13467. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13468. the variable date is bound by the calendar when this is called."
  13469. (cond
  13470. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13471. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13472. daynr
  13473. (+ daynr 1000)))
  13474. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13475. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13476. (time-to-days (current-time))) (match-string 0 s)
  13477. prefer show-all))
  13478. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13479. (defun org-days-to-iso-week (days)
  13480. "Return the iso week number."
  13481. (require 'cal-iso)
  13482. (car (calendar-iso-from-absolute days)))
  13483. (defun org-small-year-to-year (year)
  13484. "Convert 2-digit years into 4-digit years.
  13485. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13486. The year 2000 cannot be abbreviated. Any year larger than 99
  13487. is returned unchanged."
  13488. (if (< year 38)
  13489. (setq year (+ 2000 year))
  13490. (if (< year 100)
  13491. (setq year (+ 1900 year))))
  13492. year)
  13493. (defun org-time-from-absolute (d)
  13494. "Return the time corresponding to date D.
  13495. D may be an absolute day number, or a calendar-type list (month day year)."
  13496. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13497. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13498. (defun org-calendar-holiday ()
  13499. "List of holidays, for Diary display in Org-mode."
  13500. (require 'holidays)
  13501. (let ((hl (funcall
  13502. (if (fboundp 'calendar-check-holidays)
  13503. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13504. (if hl (mapconcat 'identity hl "; "))))
  13505. (defun org-diary-sexp-entry (sexp entry date)
  13506. "Process a SEXP diary ENTRY for DATE."
  13507. (require 'diary-lib)
  13508. (let ((result (if calendar-debug-sexp
  13509. (let ((stack-trace-on-error t))
  13510. (eval (car (read-from-string sexp))))
  13511. (condition-case nil
  13512. (eval (car (read-from-string sexp)))
  13513. (error
  13514. (beep)
  13515. (message "Bad sexp at line %d in %s: %s"
  13516. (org-current-line)
  13517. (buffer-file-name) sexp)
  13518. (sleep-for 2))))))
  13519. (cond ((stringp result) result)
  13520. ((and (consp result)
  13521. (stringp (cdr result))) (cdr result))
  13522. (result entry)
  13523. (t nil))))
  13524. (defun org-diary-to-ical-string (frombuf)
  13525. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13526. This uses the icalendar.el library."
  13527. (let* ((tmpdir (if (featurep 'xemacs)
  13528. (temp-directory)
  13529. temporary-file-directory))
  13530. (tmpfile (make-temp-name
  13531. (expand-file-name "orgics" tmpdir)))
  13532. buf rtn b e)
  13533. (with-current-buffer frombuf
  13534. (icalendar-export-region (point-min) (point-max) tmpfile)
  13535. (setq buf (find-buffer-visiting tmpfile))
  13536. (set-buffer buf)
  13537. (goto-char (point-min))
  13538. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13539. (setq b (match-beginning 0)))
  13540. (goto-char (point-max))
  13541. (if (re-search-backward "^END:VEVENT" nil t)
  13542. (setq e (match-end 0)))
  13543. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13544. (kill-buffer buf)
  13545. (delete-file tmpfile)
  13546. rtn))
  13547. (defun org-closest-date (start current change prefer show-all)
  13548. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13549. When PREFER is `past' return a date that is either CURRENT or past.
  13550. When PREFER is `future', return a date that is either CURRENT or future.
  13551. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13552. ;; Make the proper lists from the dates
  13553. (catch 'exit
  13554. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13555. dn dw sday cday n1 n2 n0
  13556. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13557. (setq start (org-date-to-gregorian start)
  13558. current (org-date-to-gregorian
  13559. (if show-all
  13560. current
  13561. (time-to-days (current-time))))
  13562. sday (calendar-absolute-from-gregorian start)
  13563. cday (calendar-absolute-from-gregorian current))
  13564. (if (<= cday sday) (throw 'exit sday))
  13565. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13566. (setq dn (string-to-number (match-string 1 change))
  13567. dw (cdr (assoc (match-string 2 change) a1)))
  13568. (error "Invalid change specifyer: %s" change))
  13569. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13570. (cond
  13571. ((eq dw 'day)
  13572. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13573. n2 (+ n1 dn)))
  13574. ((eq dw 'year)
  13575. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13576. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13577. (setq date1 (list m d y1)
  13578. n1 (calendar-absolute-from-gregorian date1)
  13579. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13580. n2 (calendar-absolute-from-gregorian date2)))
  13581. ((eq dw 'month)
  13582. ;; approx number of month between the two dates
  13583. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13584. ;; How often does dn fit in there?
  13585. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13586. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13587. m (+ m nm)
  13588. ny (floor (/ m 12))
  13589. y (+ y ny)
  13590. m (- m (* ny 12)))
  13591. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13592. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13593. (setq m2 (+ m dn) y2 y)
  13594. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13595. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13596. (while (<= n2 cday)
  13597. (setq n1 n2 m m2 y y2)
  13598. (setq m2 (+ m dn) y2 y)
  13599. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13600. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13601. ;; Make sure n1 is the earlier date
  13602. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13603. (if show-all
  13604. (cond
  13605. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13606. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13607. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13608. (cond
  13609. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13610. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13611. (t (if (= cday n1) n1 n2)))))))
  13612. (defun org-date-to-gregorian (date)
  13613. "Turn any specification of DATE into a gregorian date for the calendar."
  13614. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13615. ((and (listp date) (= (length date) 3)) date)
  13616. ((stringp date)
  13617. (setq date (org-parse-time-string date))
  13618. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13619. ((listp date)
  13620. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13621. (defun org-parse-time-string (s &optional nodefault)
  13622. "Parse the standard Org-mode time string.
  13623. This should be a lot faster than the normal `parse-time-string'.
  13624. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13625. hour and minute fields will be nil if not given."
  13626. (if (string-match org-ts-regexp0 s)
  13627. (list 0
  13628. (if (or (match-beginning 8) (not nodefault))
  13629. (string-to-number (or (match-string 8 s) "0")))
  13630. (if (or (match-beginning 7) (not nodefault))
  13631. (string-to-number (or (match-string 7 s) "0")))
  13632. (string-to-number (match-string 4 s))
  13633. (string-to-number (match-string 3 s))
  13634. (string-to-number (match-string 2 s))
  13635. nil nil nil)
  13636. (error "Not a standard Org-mode time string: %s" s)))
  13637. (defun org-timestamp-up (&optional arg)
  13638. "Increase the date item at the cursor by one.
  13639. If the cursor is on the year, change the year. If it is on the month or
  13640. the day, change that.
  13641. With prefix ARG, change by that many units."
  13642. (interactive "p")
  13643. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  13644. (defun org-timestamp-down (&optional arg)
  13645. "Decrease the date item at the cursor by one.
  13646. If the cursor is on the year, change the year. If it is on the month or
  13647. the day, change that.
  13648. With prefix ARG, change by that many units."
  13649. (interactive "p")
  13650. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  13651. (defun org-timestamp-up-day (&optional arg)
  13652. "Increase the date in the time stamp by one day.
  13653. With prefix ARG, change that many days."
  13654. (interactive "p")
  13655. (if (and (not (org-at-timestamp-p t))
  13656. (org-on-heading-p))
  13657. (org-todo 'up)
  13658. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  13659. (defun org-timestamp-down-day (&optional arg)
  13660. "Decrease the date in the time stamp by one day.
  13661. With prefix ARG, change that many days."
  13662. (interactive "p")
  13663. (if (and (not (org-at-timestamp-p t))
  13664. (org-on-heading-p))
  13665. (org-todo 'down)
  13666. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  13667. (defun org-at-timestamp-p (&optional inactive-ok)
  13668. "Determine if the cursor is in or at a timestamp."
  13669. (interactive)
  13670. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13671. (pos (point))
  13672. (ans (or (looking-at tsr)
  13673. (save-excursion
  13674. (skip-chars-backward "^[<\n\r\t")
  13675. (if (> (point) (point-min)) (backward-char 1))
  13676. (and (looking-at tsr)
  13677. (> (- (match-end 0) pos) -1))))))
  13678. (and ans
  13679. (boundp 'org-ts-what)
  13680. (setq org-ts-what
  13681. (cond
  13682. ((= pos (match-beginning 0)) 'bracket)
  13683. ((= pos (1- (match-end 0))) 'bracket)
  13684. ((org-pos-in-match-range pos 2) 'year)
  13685. ((org-pos-in-match-range pos 3) 'month)
  13686. ((org-pos-in-match-range pos 7) 'hour)
  13687. ((org-pos-in-match-range pos 8) 'minute)
  13688. ((or (org-pos-in-match-range pos 4)
  13689. (org-pos-in-match-range pos 5)) 'day)
  13690. ((and (> pos (or (match-end 8) (match-end 5)))
  13691. (< pos (match-end 0)))
  13692. (- pos (or (match-end 8) (match-end 5))))
  13693. (t 'day))))
  13694. ans))
  13695. (defun org-toggle-timestamp-type ()
  13696. "Toggle the type (<active> or [inactive]) of a time stamp."
  13697. (interactive)
  13698. (when (org-at-timestamp-p t)
  13699. (let ((beg (match-beginning 0)) (end (match-end 0))
  13700. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13701. (save-excursion
  13702. (goto-char beg)
  13703. (while (re-search-forward "[][<>]" end t)
  13704. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13705. t t)))
  13706. (message "Timestamp is now %sactive"
  13707. (if (equal (char-after beg) ?<) "" "in")))))
  13708. (defun org-timestamp-change (n &optional what updown)
  13709. "Change the date in the time stamp at point.
  13710. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13711. `year', `minute', `second'. If WHAT is not given, the cursor position
  13712. in the timestamp determines what will be changed."
  13713. (let ((pos (point))
  13714. with-hm inactive
  13715. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13716. org-ts-what
  13717. extra rem
  13718. ts time time0)
  13719. (if (not (org-at-timestamp-p t))
  13720. (error "Not at a timestamp"))
  13721. (if (and (not what) (eq org-ts-what 'bracket))
  13722. (org-toggle-timestamp-type)
  13723. (if (and (not what) (not (eq org-ts-what 'day))
  13724. org-display-custom-times
  13725. (get-text-property (point) 'display)
  13726. (not (get-text-property (1- (point)) 'display)))
  13727. (setq org-ts-what 'day))
  13728. (setq org-ts-what (or what org-ts-what)
  13729. inactive (= (char-after (match-beginning 0)) ?\[)
  13730. ts (match-string 0))
  13731. (replace-match "")
  13732. (if (string-match
  13733. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13734. ts)
  13735. (setq extra (match-string 1 ts)))
  13736. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13737. (setq with-hm t))
  13738. (setq time0 (org-parse-time-string ts))
  13739. (when (and updown
  13740. (eq org-ts-what 'minute)
  13741. (not current-prefix-arg))
  13742. ;; This looks like s-up and s-down. Change by one rounding step.
  13743. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13744. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13745. (setcar (cdr time0) (+ (nth 1 time0)
  13746. (if (> n 0) (- rem) (- dm rem))))))
  13747. (setq time
  13748. (encode-time (or (car time0) 0)
  13749. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13750. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13751. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13752. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13753. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13754. (nthcdr 6 time0)))
  13755. (when (and (member org-ts-what '(hour minute))
  13756. extra
  13757. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13758. (setq extra (org-modify-ts-extra
  13759. extra
  13760. (if (eq org-ts-what 'hour) 2 5)
  13761. n dm)))
  13762. (when (integerp org-ts-what)
  13763. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13764. (if (eq what 'calendar)
  13765. (let ((cal-date (org-get-date-from-calendar)))
  13766. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13767. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13768. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13769. (setcar time0 (or (car time0) 0))
  13770. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13771. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13772. (setq time (apply 'encode-time time0))))
  13773. (setq org-last-changed-timestamp
  13774. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13775. (org-clock-update-time-maybe)
  13776. (goto-char pos)
  13777. ;; Try to recenter the calendar window, if any
  13778. (if (and org-calendar-follow-timestamp-change
  13779. (get-buffer-window "*Calendar*" t)
  13780. (memq org-ts-what '(day month year)))
  13781. (org-recenter-calendar (time-to-days time))))))
  13782. (defun org-modify-ts-extra (s pos n dm)
  13783. "Change the different parts of the lead-time and repeat fields in timestamp."
  13784. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13785. ng h m new rem)
  13786. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13787. (cond
  13788. ((or (org-pos-in-match-range pos 2)
  13789. (org-pos-in-match-range pos 3))
  13790. (setq m (string-to-number (match-string 3 s))
  13791. h (string-to-number (match-string 2 s)))
  13792. (if (org-pos-in-match-range pos 2)
  13793. (setq h (+ h n))
  13794. (setq n (* dm (org-no-warnings (signum n))))
  13795. (when (not (= 0 (setq rem (% m dm))))
  13796. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13797. (setq m (+ m n)))
  13798. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13799. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13800. (setq h (min 24 (max 0 h)))
  13801. (setq ng 1 new (format "-%02d:%02d" h m)))
  13802. ((org-pos-in-match-range pos 6)
  13803. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13804. ((org-pos-in-match-range pos 5)
  13805. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13806. ((org-pos-in-match-range pos 9)
  13807. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13808. ((org-pos-in-match-range pos 8)
  13809. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13810. (when ng
  13811. (setq s (concat
  13812. (substring s 0 (match-beginning ng))
  13813. new
  13814. (substring s (match-end ng))))))
  13815. s))
  13816. (defun org-recenter-calendar (date)
  13817. "If the calendar is visible, recenter it to DATE."
  13818. (let* ((win (selected-window))
  13819. (cwin (get-buffer-window "*Calendar*" t))
  13820. (calendar-move-hook nil))
  13821. (when cwin
  13822. (select-window cwin)
  13823. (calendar-goto-date (if (listp date) date
  13824. (calendar-gregorian-from-absolute date)))
  13825. (select-window win))))
  13826. (defun org-goto-calendar (&optional arg)
  13827. "Go to the Emacs calendar at the current date.
  13828. If there is a time stamp in the current line, go to that date.
  13829. A prefix ARG can be used to force the current date."
  13830. (interactive "P")
  13831. (let ((tsr org-ts-regexp) diff
  13832. (calendar-move-hook nil)
  13833. (calendar-view-holidays-initially-flag nil)
  13834. (calendar-view-diary-initially-flag nil))
  13835. (if (or (org-at-timestamp-p)
  13836. (save-excursion
  13837. (beginning-of-line 1)
  13838. (looking-at (concat ".*" tsr))))
  13839. (let ((d1 (time-to-days (current-time)))
  13840. (d2 (time-to-days
  13841. (org-time-string-to-time (match-string 1)))))
  13842. (setq diff (- d2 d1))))
  13843. (calendar)
  13844. (calendar-goto-today)
  13845. (if (and diff (not arg)) (calendar-forward-day diff))))
  13846. (defun org-get-date-from-calendar ()
  13847. "Return a list (month day year) of date at point in calendar."
  13848. (with-current-buffer "*Calendar*"
  13849. (save-match-data
  13850. (calendar-cursor-to-date))))
  13851. (defun org-date-from-calendar ()
  13852. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13853. If there is already a time stamp at the cursor position, update it."
  13854. (interactive)
  13855. (if (org-at-timestamp-p t)
  13856. (org-timestamp-change 0 'calendar)
  13857. (let ((cal-date (org-get-date-from-calendar)))
  13858. (org-insert-time-stamp
  13859. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13860. (defun org-minutes-to-hh:mm-string (m)
  13861. "Compute H:MM from a number of minutes."
  13862. (let ((h (/ m 60)))
  13863. (setq m (- m (* 60 h)))
  13864. (format org-time-clocksum-format h m)))
  13865. (defun org-hh:mm-string-to-minutes (s)
  13866. "Convert a string H:MM to a number of minutes.
  13867. If the string is just a number, interpret it as minutes.
  13868. In fact, the first hh:mm or number in the string will be taken,
  13869. there can be extra stuff in the string.
  13870. If no number is found, the return value is 0."
  13871. (cond
  13872. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13873. (+ (* (string-to-number (match-string 1 s)) 60)
  13874. (string-to-number (match-string 2 s))))
  13875. ((string-match "\\([0-9]+\\)" s)
  13876. (string-to-number (match-string 1 s)))
  13877. (t 0)))
  13878. ;;;; Files
  13879. (defun org-save-all-org-buffers ()
  13880. "Save all Org-mode buffers without user confirmation."
  13881. (interactive)
  13882. (message "Saving all Org-mode buffers...")
  13883. (save-some-buffers t 'org-mode-p)
  13884. (when (featurep 'org-id) (org-id-locations-save))
  13885. (message "Saving all Org-mode buffers... done"))
  13886. (defun org-revert-all-org-buffers ()
  13887. "Revert all Org-mode buffers.
  13888. Prompt for confirmation when there are unsaved changes.
  13889. Be sure you know what you are doing before letting this function
  13890. overwrite your changes.
  13891. This function is useful in a setup where one tracks org files
  13892. with a version control system, to revert on one machine after pulling
  13893. changes from another. I believe the procedure must be like this:
  13894. 1. M-x org-save-all-org-buffers
  13895. 2. Pull changes from the other machine, resolve conflicts
  13896. 3. M-x org-revert-all-org-buffers"
  13897. (interactive)
  13898. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13899. (error "Abort"))
  13900. (save-excursion
  13901. (save-window-excursion
  13902. (mapc
  13903. (lambda (b)
  13904. (when (and (with-current-buffer b (org-mode-p))
  13905. (with-current-buffer b buffer-file-name))
  13906. (switch-to-buffer b)
  13907. (revert-buffer t 'no-confirm)))
  13908. (buffer-list))
  13909. (when (and (featurep 'org-id) org-id-track-globally)
  13910. (org-id-locations-load)))))
  13911. ;;;; Agenda files
  13912. ;;;###autoload
  13913. (defun org-iswitchb (&optional arg)
  13914. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13915. With a prefix argument, restrict available to files.
  13916. With two prefix arguments, restrict available buffers to agenda files."
  13917. (interactive "P")
  13918. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13919. ((equal arg '(16)) (org-buffer-list 'agenda))
  13920. (t (org-buffer-list)))))
  13921. (switch-to-buffer
  13922. (org-icompleting-read "Org buffer: "
  13923. (mapcar 'list (mapcar 'buffer-name blist))
  13924. nil t))))
  13925. ;;;###autoload
  13926. (defalias 'org-ido-switchb 'org-iswitchb)
  13927. (defun org-buffer-list (&optional predicate exclude-tmp)
  13928. "Return a list of Org buffers.
  13929. PREDICATE can be `export', `files' or `agenda'.
  13930. export restrict the list to Export buffers.
  13931. files restrict the list to buffers visiting Org files.
  13932. agenda restrict the list to buffers visiting agenda files.
  13933. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13934. (let* ((bfn nil)
  13935. (agenda-files (and (eq predicate 'agenda)
  13936. (mapcar 'file-truename (org-agenda-files t))))
  13937. (filter
  13938. (cond
  13939. ((eq predicate 'files)
  13940. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13941. ((eq predicate 'export)
  13942. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13943. ((eq predicate 'agenda)
  13944. (lambda (b)
  13945. (with-current-buffer b
  13946. (and (eq major-mode 'org-mode)
  13947. (setq bfn (buffer-file-name b))
  13948. (member (file-truename bfn) agenda-files)))))
  13949. (t (lambda (b) (with-current-buffer b
  13950. (or (eq major-mode 'org-mode)
  13951. (string-match "\*Org .*Export"
  13952. (buffer-name b)))))))))
  13953. (delq nil
  13954. (mapcar
  13955. (lambda(b)
  13956. (if (and (funcall filter b)
  13957. (or (not exclude-tmp)
  13958. (not (string-match "tmp" (buffer-name b)))))
  13959. b
  13960. nil))
  13961. (buffer-list)))))
  13962. (defun org-agenda-files (&optional unrestricted archives)
  13963. "Get the list of agenda files.
  13964. Optional UNRESTRICTED means return the full list even if a restriction
  13965. is currently in place.
  13966. When ARCHIVES is t, include all archive files that are really being
  13967. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13968. `org-agenda-archives-mode' is t."
  13969. (let ((files
  13970. (cond
  13971. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13972. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13973. ((listp org-agenda-files) org-agenda-files)
  13974. (t (error "Invalid value of `org-agenda-files'")))))
  13975. (setq files (apply 'append
  13976. (mapcar (lambda (f)
  13977. (if (file-directory-p f)
  13978. (directory-files
  13979. f t org-agenda-file-regexp)
  13980. (list f)))
  13981. files)))
  13982. (when org-agenda-skip-unavailable-files
  13983. (setq files (delq nil
  13984. (mapcar (function
  13985. (lambda (file)
  13986. (and (file-readable-p file) file)))
  13987. files))))
  13988. (when (or (eq archives t)
  13989. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13990. (setq files (org-add-archive-files files)))
  13991. files))
  13992. (defun org-agenda-file-p (&optional file)
  13993. "Return non-nil, if FILE is an agenda file.
  13994. If FILE is omitted, use the file associated with the current
  13995. buffer."
  13996. (member (or file (buffer-file-name))
  13997. (org-agenda-files t)))
  13998. (defun org-edit-agenda-file-list ()
  13999. "Edit the list of agenda files.
  14000. Depending on setup, this either uses customize to edit the variable
  14001. `org-agenda-files', or it visits the file that is holding the list. In the
  14002. latter case, the buffer is set up in a way that saving it automatically kills
  14003. the buffer and restores the previous window configuration."
  14004. (interactive)
  14005. (if (stringp org-agenda-files)
  14006. (let ((cw (current-window-configuration)))
  14007. (find-file org-agenda-files)
  14008. (org-set-local 'org-window-configuration cw)
  14009. (org-add-hook 'after-save-hook
  14010. (lambda ()
  14011. (set-window-configuration
  14012. (prog1 org-window-configuration
  14013. (kill-buffer (current-buffer))))
  14014. (org-install-agenda-files-menu)
  14015. (message "New agenda file list installed"))
  14016. nil 'local)
  14017. (message "%s" (substitute-command-keys
  14018. "Edit list and finish with \\[save-buffer]")))
  14019. (customize-variable 'org-agenda-files)))
  14020. (defun org-store-new-agenda-file-list (list)
  14021. "Set new value for the agenda file list and save it correctly."
  14022. (if (stringp org-agenda-files)
  14023. (let ((fe (org-read-agenda-file-list t)) b u)
  14024. (while (setq b (find-buffer-visiting org-agenda-files))
  14025. (kill-buffer b))
  14026. (with-temp-file org-agenda-files
  14027. (insert
  14028. (mapconcat
  14029. (lambda (f) ;; Keep un-expanded entries.
  14030. (if (setq u (assoc f fe))
  14031. (cdr u)
  14032. f))
  14033. list "\n")
  14034. "\n")))
  14035. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14036. (org-insert-mode-line-in-empty-file nil))
  14037. (setq org-agenda-files list)
  14038. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14039. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14040. "Read the list of agenda files from a file.
  14041. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14042. filenames, used by `org-store-new-agenda-file-list' to write back
  14043. un-expanded file names."
  14044. (when (file-directory-p org-agenda-files)
  14045. (error "`org-agenda-files' cannot be a single directory"))
  14046. (when (stringp org-agenda-files)
  14047. (with-temp-buffer
  14048. (insert-file-contents org-agenda-files)
  14049. (mapcar
  14050. (lambda (f)
  14051. (let ((e (expand-file-name (substitute-in-file-name f)
  14052. org-directory)))
  14053. (if pair-with-expansion
  14054. (cons e f)
  14055. e)))
  14056. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14057. ;;;###autoload
  14058. (defun org-cycle-agenda-files ()
  14059. "Cycle through the files in `org-agenda-files'.
  14060. If the current buffer visits an agenda file, find the next one in the list.
  14061. If the current buffer does not, find the first agenda file."
  14062. (interactive)
  14063. (let* ((fs (org-agenda-files t))
  14064. (files (append fs (list (car fs))))
  14065. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14066. file)
  14067. (unless files (error "No agenda files"))
  14068. (catch 'exit
  14069. (while (setq file (pop files))
  14070. (if (equal (file-truename file) tcf)
  14071. (when (car files)
  14072. (find-file (car files))
  14073. (throw 'exit t))))
  14074. (find-file (car fs)))
  14075. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14076. (defun org-agenda-file-to-front (&optional to-end)
  14077. "Move/add the current file to the top of the agenda file list.
  14078. If the file is not present in the list, it is added to the front. If it is
  14079. present, it is moved there. With optional argument TO-END, add/move to the
  14080. end of the list."
  14081. (interactive "P")
  14082. (let ((org-agenda-skip-unavailable-files nil)
  14083. (file-alist (mapcar (lambda (x)
  14084. (cons (file-truename x) x))
  14085. (org-agenda-files t)))
  14086. (ctf (file-truename buffer-file-name))
  14087. x had)
  14088. (setq x (assoc ctf file-alist) had x)
  14089. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14090. (if to-end
  14091. (setq file-alist (append (delq x file-alist) (list x)))
  14092. (setq file-alist (cons x (delq x file-alist))))
  14093. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14094. (org-install-agenda-files-menu)
  14095. (message "File %s to %s of agenda file list"
  14096. (if had "moved" "added") (if to-end "end" "front"))))
  14097. (defun org-remove-file (&optional file)
  14098. "Remove current file from the list of files in variable `org-agenda-files'.
  14099. These are the files which are being checked for agenda entries.
  14100. Optional argument FILE means use this file instead of the current."
  14101. (interactive)
  14102. (let* ((org-agenda-skip-unavailable-files nil)
  14103. (file (or file buffer-file-name))
  14104. (true-file (file-truename file))
  14105. (afile (abbreviate-file-name file))
  14106. (files (delq nil (mapcar
  14107. (lambda (x)
  14108. (if (equal true-file
  14109. (file-truename x))
  14110. nil x))
  14111. (org-agenda-files t)))))
  14112. (if (not (= (length files) (length (org-agenda-files t))))
  14113. (progn
  14114. (org-store-new-agenda-file-list files)
  14115. (org-install-agenda-files-menu)
  14116. (message "Removed file: %s" afile))
  14117. (message "File was not in list: %s (not removed)" afile))))
  14118. (defun org-file-menu-entry (file)
  14119. (vector file (list 'find-file file) t))
  14120. (defun org-check-agenda-file (file)
  14121. "Make sure FILE exists. If not, ask user what to do."
  14122. (when (not (file-exists-p file))
  14123. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14124. (abbreviate-file-name file))
  14125. (let ((r (downcase (read-char-exclusive))))
  14126. (cond
  14127. ((equal r ?r)
  14128. (org-remove-file file)
  14129. (throw 'nextfile t))
  14130. (t (error "Abort"))))))
  14131. (defun org-get-agenda-file-buffer (file)
  14132. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14133. it to the list of buffers which might be released later."
  14134. (let ((buf (org-find-base-buffer-visiting file)))
  14135. (if buf
  14136. buf ; just return it
  14137. ;; Make a new buffer and remember it
  14138. (setq buf (find-file-noselect file))
  14139. (if buf (push buf org-agenda-new-buffers))
  14140. buf)))
  14141. (defun org-release-buffers (blist)
  14142. "Release all buffers in list, asking the user for confirmation when needed.
  14143. When a buffer is unmodified, it is just killed. When modified, it is saved
  14144. \(if the user agrees) and then killed."
  14145. (let (buf file)
  14146. (while (setq buf (pop blist))
  14147. (setq file (buffer-file-name buf))
  14148. (when (and (buffer-modified-p buf)
  14149. file
  14150. (y-or-n-p (format "Save file %s? " file)))
  14151. (with-current-buffer buf (save-buffer)))
  14152. (kill-buffer buf))))
  14153. (defun org-prepare-agenda-buffers (files)
  14154. "Create buffers for all agenda files, protect archived trees and comments."
  14155. (interactive)
  14156. (let ((pa '(:org-archived t))
  14157. (pc '(:org-comment t))
  14158. (pall '(:org-archived t :org-comment t))
  14159. (inhibit-read-only t)
  14160. (rea (concat ":" org-archive-tag ":"))
  14161. bmp file re)
  14162. (save-excursion
  14163. (save-restriction
  14164. (while (setq file (pop files))
  14165. (catch 'nextfile
  14166. (if (bufferp file)
  14167. (set-buffer file)
  14168. (org-check-agenda-file file)
  14169. (set-buffer (org-get-agenda-file-buffer file)))
  14170. (widen)
  14171. (setq bmp (buffer-modified-p))
  14172. (org-refresh-category-properties)
  14173. (setq org-todo-keywords-for-agenda
  14174. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14175. (setq org-done-keywords-for-agenda
  14176. (append org-done-keywords-for-agenda org-done-keywords))
  14177. (setq org-todo-keyword-alist-for-agenda
  14178. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14179. (setq org-drawers-for-agenda
  14180. (append org-drawers-for-agenda org-drawers))
  14181. (setq org-tag-alist-for-agenda
  14182. (append org-tag-alist-for-agenda org-tag-alist))
  14183. (save-excursion
  14184. (remove-text-properties (point-min) (point-max) pall)
  14185. (when org-agenda-skip-archived-trees
  14186. (goto-char (point-min))
  14187. (while (re-search-forward rea nil t)
  14188. (if (org-on-heading-p t)
  14189. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14190. (goto-char (point-min))
  14191. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14192. (while (re-search-forward re nil t)
  14193. (add-text-properties
  14194. (match-beginning 0) (org-end-of-subtree t) pc)))
  14195. (set-buffer-modified-p bmp)))))
  14196. (setq org-todo-keywords-for-agenda
  14197. (org-uniquify org-todo-keywords-for-agenda))
  14198. (setq org-todo-keyword-alist-for-agenda
  14199. (org-uniquify org-todo-keyword-alist-for-agenda)
  14200. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14201. ;;;; Embedded LaTeX
  14202. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14203. "Keymap for the minor `org-cdlatex-mode'.")
  14204. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14205. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14206. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14207. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14208. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14209. (defvar org-cdlatex-texmathp-advice-is-done nil
  14210. "Flag remembering if we have applied the advice to texmathp already.")
  14211. (define-minor-mode org-cdlatex-mode
  14212. "Toggle the minor `org-cdlatex-mode'.
  14213. This mode supports entering LaTeX environment and math in LaTeX fragments
  14214. in Org-mode.
  14215. \\{org-cdlatex-mode-map}"
  14216. nil " OCDL" nil
  14217. (when org-cdlatex-mode (require 'cdlatex))
  14218. (unless org-cdlatex-texmathp-advice-is-done
  14219. (setq org-cdlatex-texmathp-advice-is-done t)
  14220. (defadvice texmathp (around org-math-always-on activate)
  14221. "Always return t in org-mode buffers.
  14222. This is because we want to insert math symbols without dollars even outside
  14223. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14224. an embedded LaTeX fragment, let texmathp do its job.
  14225. \\[org-cdlatex-mode-map]"
  14226. (interactive)
  14227. (let (p)
  14228. (cond
  14229. ((not (org-mode-p)) ad-do-it)
  14230. ((eq this-command 'cdlatex-math-symbol)
  14231. (setq ad-return-value t
  14232. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14233. (t
  14234. (let ((p (org-inside-LaTeX-fragment-p)))
  14235. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14236. (setq ad-return-value t
  14237. texmathp-why '("Org-mode embedded math" . 0))
  14238. (if p ad-do-it)))))))))
  14239. (defun turn-on-org-cdlatex ()
  14240. "Unconditionally turn on `org-cdlatex-mode'."
  14241. (org-cdlatex-mode 1))
  14242. (defun org-inside-LaTeX-fragment-p ()
  14243. "Test if point is inside a LaTeX fragment.
  14244. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14245. sequence appearing also before point.
  14246. Even though the matchers for math are configurable, this function assumes
  14247. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14248. delimiters are skipped when they have been removed by customization.
  14249. The return value is nil, or a cons cell with the delimiter and
  14250. and the position of this delimiter.
  14251. This function does a reasonably good job, but can locally be fooled by
  14252. for example currency specifications. For example it will assume being in
  14253. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14254. fragments that are properly closed, but during editing, we have to live
  14255. with the uncertainty caused by missing closing delimiters. This function
  14256. looks only before point, not after."
  14257. (catch 'exit
  14258. (let ((pos (point))
  14259. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14260. (lim (progn
  14261. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14262. (point)))
  14263. dd-on str (start 0) m re)
  14264. (goto-char pos)
  14265. (when dodollar
  14266. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14267. re (nth 1 (assoc "$" org-latex-regexps)))
  14268. (while (string-match re str start)
  14269. (cond
  14270. ((= (match-end 0) (length str))
  14271. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14272. ((= (match-end 0) (- (length str) 5))
  14273. (throw 'exit nil))
  14274. (t (setq start (match-end 0))))))
  14275. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14276. (goto-char pos)
  14277. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14278. (and (match-beginning 2) (throw 'exit nil))
  14279. ;; count $$
  14280. (while (re-search-backward "\\$\\$" lim t)
  14281. (setq dd-on (not dd-on)))
  14282. (goto-char pos)
  14283. (if dd-on (cons "$$" m))))))
  14284. (defun org-inside-latex-macro-p ()
  14285. "Is point inside a LaTeX macro or its arguments?"
  14286. (save-match-data
  14287. (org-in-regexp
  14288. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14289. (defun org-try-cdlatex-tab ()
  14290. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14291. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14292. - inside a LaTeX fragment, or
  14293. - after the first word in a line, where an abbreviation expansion could
  14294. insert a LaTeX environment."
  14295. (when org-cdlatex-mode
  14296. (cond
  14297. ((save-excursion
  14298. (skip-chars-backward "a-zA-Z0-9*")
  14299. (skip-chars-backward " \t")
  14300. (bolp))
  14301. (cdlatex-tab) t)
  14302. ((org-inside-LaTeX-fragment-p)
  14303. (cdlatex-tab) t)
  14304. (t nil))))
  14305. (defun org-cdlatex-underscore-caret (&optional arg)
  14306. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14307. Revert to the normal definition outside of these fragments."
  14308. (interactive "P")
  14309. (if (org-inside-LaTeX-fragment-p)
  14310. (call-interactively 'cdlatex-sub-superscript)
  14311. (let (org-cdlatex-mode)
  14312. (call-interactively (key-binding (vector last-input-event))))))
  14313. (defun org-cdlatex-math-modify (&optional arg)
  14314. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14315. Revert to the normal definition outside of these fragments."
  14316. (interactive "P")
  14317. (if (org-inside-LaTeX-fragment-p)
  14318. (call-interactively 'cdlatex-math-modify)
  14319. (let (org-cdlatex-mode)
  14320. (call-interactively (key-binding (vector last-input-event))))))
  14321. (defvar org-latex-fragment-image-overlays nil
  14322. "List of overlays carrying the images of latex fragments.")
  14323. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14324. (defun org-remove-latex-fragment-image-overlays ()
  14325. "Remove all overlays with LaTeX fragment images in current buffer."
  14326. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14327. (setq org-latex-fragment-image-overlays nil))
  14328. (defun org-preview-latex-fragment (&optional subtree)
  14329. "Preview the LaTeX fragment at point, or all locally or globally.
  14330. If the cursor is in a LaTeX fragment, create the image and overlay
  14331. it over the source code. If there is no fragment at point, display
  14332. all fragments in the current text, from one headline to the next. With
  14333. prefix SUBTREE, display all fragments in the current subtree. With a
  14334. double prefix `C-u C-u', or when the cursor is before the first headline,
  14335. display all fragments in the buffer.
  14336. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14337. (interactive "P")
  14338. (org-remove-latex-fragment-image-overlays)
  14339. (save-excursion
  14340. (save-restriction
  14341. (let (beg end at msg)
  14342. (cond
  14343. ((or (equal subtree '(16))
  14344. (not (save-excursion
  14345. (re-search-backward (concat "^" outline-regexp) nil t))))
  14346. (setq beg (point-min) end (point-max)
  14347. msg "Creating images for buffer...%s"))
  14348. ((equal subtree '(4))
  14349. (org-back-to-heading)
  14350. (setq beg (point) end (org-end-of-subtree t)
  14351. msg "Creating images for subtree...%s"))
  14352. (t
  14353. (if (setq at (org-inside-LaTeX-fragment-p))
  14354. (goto-char (max (point-min) (- (cdr at) 2)))
  14355. (org-back-to-heading))
  14356. (setq beg (point) end (progn (outline-next-heading) (point))
  14357. msg (if at "Creating image...%s"
  14358. "Creating images for entry...%s"))))
  14359. (message msg "")
  14360. (narrow-to-region beg end)
  14361. (goto-char beg)
  14362. (org-format-latex
  14363. (concat "ltxpng/" (file-name-sans-extension
  14364. (file-name-nondirectory
  14365. buffer-file-name)))
  14366. default-directory 'overlays msg at 'forbuffer)
  14367. (message msg "done. Use `C-c C-c' to remove images.")))))
  14368. (defvar org-latex-regexps
  14369. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14370. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14371. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14372. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14373. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14374. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14375. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14376. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14377. "Regular expressions for matching embedded LaTeX.")
  14378. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  14379. "Replace LaTeX fragments with links to an image, and produce images.
  14380. Some of the options can be changed using the variable
  14381. `org-format-latex-options'."
  14382. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14383. (let* ((prefixnodir (file-name-nondirectory prefix))
  14384. (absprefix (expand-file-name prefix dir))
  14385. (todir (file-name-directory absprefix))
  14386. (opt org-format-latex-options)
  14387. (matchers (plist-get opt :matchers))
  14388. (re-list org-latex-regexps)
  14389. (org-format-latex-header-extra
  14390. (plist-get (org-infile-export-plist) :latex-header-extra))
  14391. (cnt 0) txt hash link beg end re e checkdir
  14392. executables-checked
  14393. m n block linkfile movefile ov)
  14394. ;; Check the different regular expressions
  14395. (while (setq e (pop re-list))
  14396. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14397. block (if (nth 3 e) "\n\n" ""))
  14398. (when (member m matchers)
  14399. (goto-char (point-min))
  14400. (while (re-search-forward re nil t)
  14401. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14402. (not (get-text-property (match-beginning n)
  14403. 'org-protected))
  14404. (or (not overlays)
  14405. (not (eq (get-char-property (match-beginning n)
  14406. 'org-overlay-type)
  14407. 'org-latex-overlay))))
  14408. (setq txt (match-string n)
  14409. beg (match-beginning n) end (match-end n)
  14410. cnt (1+ cnt))
  14411. (let (print-length print-level) ; make sure full list is printed
  14412. (setq hash (sha1 (prin1-to-string
  14413. (list org-format-latex-header
  14414. org-format-latex-header-extra
  14415. org-export-latex-default-packages-alist
  14416. org-export-latex-packages-alist
  14417. org-format-latex-options
  14418. forbuffer txt)))
  14419. linkfile (format "%s_%s.png" prefix hash)
  14420. movefile (format "%s_%s.png" absprefix hash)))
  14421. (setq link (concat block "[[file:" linkfile "]]" block))
  14422. (if msg (message msg cnt))
  14423. (goto-char beg)
  14424. (unless checkdir ; make sure the directory exists
  14425. (setq checkdir t)
  14426. (or (file-directory-p todir) (make-directory todir)))
  14427. (unless executables-checked
  14428. (org-check-external-command
  14429. "latex" "needed to convert LaTeX fragments to images")
  14430. (org-check-external-command
  14431. "dvipng" "needed to convert LaTeX fragments to images")
  14432. (setq executables-checked t))
  14433. (unless (file-exists-p movefile)
  14434. (org-create-formula-image
  14435. txt movefile opt forbuffer))
  14436. (if overlays
  14437. (progn
  14438. (mapc (lambda (o)
  14439. (if (eq (overlay-get o 'org-overlay-type)
  14440. 'org-latex-overlay)
  14441. (delete-overlay o)))
  14442. (overlays-in beg end))
  14443. (setq ov (make-overlay beg end))
  14444. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14445. (if (featurep 'xemacs)
  14446. (progn
  14447. (overlay-put ov 'invisible t)
  14448. (overlay-put
  14449. ov 'end-glyph
  14450. (make-glyph (vector 'png :file movefile))))
  14451. (overlay-put
  14452. ov 'display
  14453. (list 'image :type 'png :file movefile :ascent 'center)))
  14454. (push ov org-latex-fragment-image-overlays)
  14455. (goto-char end))
  14456. (delete-region beg end)
  14457. (insert (org-add-props link
  14458. (list 'org-latex-src
  14459. (replace-regexp-in-string "\"" "" txt)))))))))))
  14460. ;; This function borrows from Ganesh Swami's latex2png.el
  14461. (defun org-create-formula-image (string tofile options buffer)
  14462. "This calls dvipng."
  14463. (require 'org-latex)
  14464. (let* ((tmpdir (if (featurep 'xemacs)
  14465. (temp-directory)
  14466. temporary-file-directory))
  14467. (texfilebase (make-temp-name
  14468. (expand-file-name "orgtex" tmpdir)))
  14469. (texfile (concat texfilebase ".tex"))
  14470. (dvifile (concat texfilebase ".dvi"))
  14471. (pngfile (concat texfilebase ".png"))
  14472. (fnh (if (featurep 'xemacs)
  14473. (font-height (get-face-font 'default))
  14474. (face-attribute 'default :height nil)))
  14475. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14476. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14477. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14478. "Black"))
  14479. (bg (or (plist-get options (if buffer :background :html-background))
  14480. "Transparent")))
  14481. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14482. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14483. (with-temp-file texfile
  14484. (insert (org-splice-latex-header
  14485. org-format-latex-header
  14486. org-export-latex-default-packages-alist
  14487. org-export-latex-packages-alist t
  14488. org-format-latex-header-extra))
  14489. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14490. (require 'org-latex)
  14491. (org-export-latex-fix-inputenc))
  14492. (let ((dir default-directory))
  14493. (condition-case nil
  14494. (progn
  14495. (cd tmpdir)
  14496. (call-process "latex" nil nil nil texfile))
  14497. (error nil))
  14498. (cd dir))
  14499. (if (not (file-exists-p dvifile))
  14500. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14501. (condition-case nil
  14502. (call-process "dvipng" nil nil nil
  14503. "-fg" fg "-bg" bg
  14504. "-D" dpi
  14505. ;;"-x" scale "-y" scale
  14506. "-T" "tight"
  14507. "-o" pngfile
  14508. dvifile)
  14509. (error nil))
  14510. (if (not (file-exists-p pngfile))
  14511. (if org-format-latex-signal-error
  14512. (error "Failed to create png file from %s" texfile)
  14513. (message "Failed to create png file from %s" texfile)
  14514. nil)
  14515. ;; Use the requested file name and clean up
  14516. (copy-file pngfile tofile 'replace)
  14517. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14518. (delete-file (concat texfilebase e)))
  14519. pngfile))))
  14520. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14521. "Fill a LaTeX header template TPL.
  14522. In the template, the following place holders will be recognized:
  14523. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14524. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14525. [PACKAGES] \\usepackage statements for PKG
  14526. [NO-PACKAGES] do not include PKG
  14527. [EXTRA] the string EXTRA
  14528. [NO-EXTRA] do not include EXTRA
  14529. For backward compatibility, if both the positive and the negative place
  14530. holder is missing, the positive one (without the \"NO-\") will be
  14531. assumed to be present at the end of the template.
  14532. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14533. EXTRA is a string.
  14534. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14535. (let (rpl (end ""))
  14536. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14537. (setq rpl (if (or (match-end 1) (not def-pkg))
  14538. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14539. tpl (replace-match rpl t t tpl))
  14540. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14541. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14542. (setq rpl (if (or (match-end 1) (not pkg))
  14543. "" (org-latex-packages-to-string pkg snippets-p t))
  14544. tpl (replace-match rpl t t tpl))
  14545. (if pkg (setq end
  14546. (concat end "\n"
  14547. (org-latex-packages-to-string pkg snippets-p)))))
  14548. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14549. (setq rpl (if (or (match-end 1) (not extra))
  14550. "" (concat extra "\n"))
  14551. tpl (replace-match rpl t t tpl))
  14552. (if (and extra (string-match "\\S-" extra))
  14553. (setq end (concat end "\n" extra))))
  14554. (if (string-match "\\S-" end)
  14555. (concat tpl "\n" end)
  14556. tpl)))
  14557. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14558. "Turn an alist of packages into a string with the \\usepackage macros."
  14559. (setq pkg (mapconcat (lambda(p)
  14560. (cond
  14561. ((stringp p) p)
  14562. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14563. (format "%% Package %s omitted" (cadr p)))
  14564. ((equal "" (car p))
  14565. (format "\\usepackage{%s}" (cadr p)))
  14566. (t
  14567. (format "\\usepackage[%s]{%s}"
  14568. (car p) (cadr p)))))
  14569. pkg
  14570. "\n"))
  14571. (if newline (concat pkg "\n") pkg))
  14572. (defun org-dvipng-color (attr)
  14573. "Return an rgb color specification for dvipng."
  14574. (apply 'format "rgb %s %s %s"
  14575. (mapcar 'org-normalize-color
  14576. (color-values (face-attribute 'default attr nil)))))
  14577. (defun org-normalize-color (value)
  14578. "Return string to be used as color value for an RGB component."
  14579. (format "%g" (/ value 65535.0)))
  14580. ;; Image display
  14581. (defvar org-inline-image-overlays nil)
  14582. (make-variable-buffer-local 'org-inline-image-overlays)
  14583. (defun org-toggle-inline-images (&optional include-linked)
  14584. "Toggle the display of inline images.
  14585. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14586. (interactive "P")
  14587. (if org-inline-image-overlays
  14588. (progn
  14589. (org-remove-inline-images)
  14590. (message "Inline image display turned off"))
  14591. (org-display-inline-images include-linked)
  14592. (if org-inline-image-overlays
  14593. (message "%d images displayed inline"
  14594. (length org-inline-image-overlays))
  14595. (message "No images to display inline"))))
  14596. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14597. "Display inline images.
  14598. Normally only links without a description part are inlined, because this
  14599. is how it will work for export. When INCLUDE-LINKED is set, also links
  14600. with a description part will be inlined. This can be nice for a quick
  14601. look at those images, but it does not reflect whatexported files will look
  14602. like.
  14603. When REFRESH is set, refresh existing images between BEG and END.
  14604. This will create new image displays only if necessary.
  14605. BEG and END default to the buffer boundaries."
  14606. (interactive "P")
  14607. (unless refresh
  14608. (org-remove-inline-images)
  14609. (clear-image-cache))
  14610. (save-excursion
  14611. (save-restriction
  14612. (widen)
  14613. (setq beg (or beg (point-min)) end (or end (point-max)))
  14614. (goto-char (point-min))
  14615. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([-+~.:/\\_0-9a-zA-Z ]+"
  14616. (substring (org-image-file-name-regexp) 0 -2)
  14617. "\\)\\]" (if include-linked "" "\\]")))
  14618. old file ov img)
  14619. (while (re-search-forward re end t)
  14620. (setq old (get-char-property-and-overlay (match-beginning 1)
  14621. 'org-image-overlay))
  14622. (setq file (expand-file-name
  14623. (concat (or (match-string 3) "") (match-string 4))))
  14624. (when (file-exists-p file)
  14625. (if (and (car-safe old) refresh)
  14626. (image-refresh (overlay-get (cdr old) 'display))
  14627. (setq img (create-image file))
  14628. (when img
  14629. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14630. (overlay-put ov 'display img)
  14631. (overlay-put ov 'face 'default)
  14632. (overlay-put ov 'org-image-overlay t)
  14633. (overlay-put ov 'modification-hooks
  14634. (list 'org-display-inline-modification-hook))
  14635. (push ov org-inline-image-overlays)))))))))
  14636. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  14637. "Remove inline-display overlay if a corresponding region is modified."
  14638. (let ((inhibit-modification-hooks t))
  14639. (when (and ov after)
  14640. (delete ov org-inline-image-overlays)
  14641. (delete-overlay ov))))
  14642. (defun org-remove-inline-images ()
  14643. "Remove inline display of images."
  14644. (interactive)
  14645. (mapc 'delete-overlay org-inline-image-overlays)
  14646. (setq org-inline-image-overlays nil))
  14647. ;;;; Key bindings
  14648. ;; Make `C-c C-x' a prefix key
  14649. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14650. ;; TAB key with modifiers
  14651. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14652. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14653. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14654. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14655. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14656. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14657. ;; The following line is necessary under Suse GNU/Linux
  14658. (unless (featurep 'xemacs)
  14659. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14660. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14661. (define-key org-mode-map [backtab] 'org-shifttab)
  14662. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14663. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14664. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14665. ;; Cursor keys with modifiers
  14666. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14667. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14668. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14669. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14670. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14671. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14672. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14673. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14674. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14675. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14676. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14677. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14678. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14679. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14680. ;; Babel keys
  14681. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  14682. (mapc (lambda (pair)
  14683. (define-key org-babel-map (car pair) (cdr pair)))
  14684. org-babel-key-bindings)
  14685. ;;; Extra keys for tty access.
  14686. ;; We only set them when really needed because otherwise the
  14687. ;; menus don't show the simple keys
  14688. (when (or org-use-extra-keys
  14689. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14690. (not window-system))
  14691. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14692. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14693. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14694. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14695. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14696. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14697. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14698. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14699. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14700. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14701. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14702. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14703. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14704. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14705. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14706. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14707. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14708. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14709. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14710. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14711. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14712. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14713. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14714. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14715. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14716. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14717. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14718. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14719. ;; All the other keys
  14720. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14721. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14722. (if (boundp 'narrow-map)
  14723. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14724. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14725. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14726. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14727. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14728. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14729. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14730. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14731. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14732. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14733. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14734. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14735. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14736. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14737. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14738. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14739. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14740. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14741. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14742. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14743. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14744. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14745. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14746. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14747. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14748. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14749. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14750. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14751. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14752. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14753. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14754. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14755. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14756. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14757. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14758. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14759. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14760. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14761. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14762. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14763. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14764. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14765. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14766. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14767. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14768. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14769. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14770. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14771. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14772. (org-defkey org-mode-map "\C-m" 'org-return)
  14773. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14774. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14775. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14776. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14777. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14778. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14779. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14780. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14781. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14782. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14783. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14784. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14785. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14786. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14787. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14788. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14789. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14790. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14791. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14792. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14793. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14794. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14795. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14796. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14797. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14798. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14799. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14800. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14801. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14802. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14803. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14804. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14805. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14806. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14807. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  14808. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  14809. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14810. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14811. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14812. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14813. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14814. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14815. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14816. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14817. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14818. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14819. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14820. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14821. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14822. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14823. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14824. (when (featurep 'xemacs)
  14825. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14826. (defconst org-speed-commands-default
  14827. '(
  14828. ("Outline Navigation")
  14829. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14830. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14831. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14832. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14833. ("u" . (org-speed-move-safe 'outline-up-heading))
  14834. ("j" . org-goto)
  14835. ("g" . (org-refile t))
  14836. ("Outline Visibility")
  14837. ("c" . org-cycle)
  14838. ("C" . org-shifttab)
  14839. (" " . org-display-outline-path)
  14840. ("Outline Structure Editing")
  14841. ("U" . org-shiftmetaup)
  14842. ("D" . org-shiftmetadown)
  14843. ("r" . org-metaright)
  14844. ("l" . org-metaleft)
  14845. ("R" . org-shiftmetaright)
  14846. ("L" . org-shiftmetaleft)
  14847. ("i" . (progn (forward-char 1) (call-interactively
  14848. 'org-insert-heading-respect-content)))
  14849. ("^" . org-sort)
  14850. ("w" . org-refile)
  14851. ("a" . org-archive-subtree-default-with-confirmation)
  14852. ("." . outline-mark-subtree)
  14853. ("Clock Commands")
  14854. ("I" . org-clock-in)
  14855. ("O" . org-clock-out)
  14856. ("Meta Data Editing")
  14857. ("t" . org-todo)
  14858. ("0" . (org-priority ?\ ))
  14859. ("1" . (org-priority ?A))
  14860. ("2" . (org-priority ?B))
  14861. ("3" . (org-priority ?C))
  14862. (";" . org-set-tags-command)
  14863. ("e" . org-set-effort)
  14864. ("Agenda Views etc")
  14865. ("v" . org-agenda)
  14866. ("/" . org-sparse-tree)
  14867. ("Misc")
  14868. ("o" . org-open-at-point)
  14869. ("?" . org-speed-command-help)
  14870. )
  14871. "The default speed commands.")
  14872. (defun org-print-speed-command (e)
  14873. (if (> (length (car e)) 1)
  14874. (progn
  14875. (princ "\n")
  14876. (princ (car e))
  14877. (princ "\n")
  14878. (princ (make-string (length (car e)) ?-))
  14879. (princ "\n"))
  14880. (princ (car e))
  14881. (princ " ")
  14882. (if (symbolp (cdr e))
  14883. (princ (symbol-name (cdr e)))
  14884. (prin1 (cdr e)))
  14885. (princ "\n")))
  14886. (defun org-speed-command-help ()
  14887. "Show the available speed commands."
  14888. (interactive)
  14889. (if (not org-use-speed-commands)
  14890. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14891. (with-output-to-temp-buffer "*Help*"
  14892. (princ "User-defined Speed commands\n===========================\n")
  14893. (mapc 'org-print-speed-command org-speed-commands-user)
  14894. (princ "\n")
  14895. (princ "Built-in Speed commands\n=======================\n")
  14896. (mapc 'org-print-speed-command org-speed-commands-default))
  14897. (with-current-buffer "*Help*"
  14898. (setq truncate-lines t))))
  14899. (defun org-speed-move-safe (cmd)
  14900. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14901. If not, return to the original position and throw an error."
  14902. (interactive)
  14903. (let ((pos (point)))
  14904. (call-interactively cmd)
  14905. (unless (and (bolp) (org-on-heading-p))
  14906. (goto-char pos)
  14907. (error "Boundary reached while executing %s" cmd))))
  14908. (defvar org-self-insert-command-undo-counter 0)
  14909. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14910. (defvar org-speed-command nil)
  14911. (defun org-self-insert-command (N)
  14912. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14913. If the cursor is in a table looking at whitespace, the whitespace is
  14914. overwritten, and the table is not marked as requiring realignment."
  14915. (interactive "p")
  14916. (cond
  14917. ((and org-use-speed-commands
  14918. (or (and (bolp) (looking-at outline-regexp))
  14919. (and (functionp org-use-speed-commands)
  14920. (funcall org-use-speed-commands)))
  14921. (setq
  14922. org-speed-command
  14923. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14924. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14925. (cond
  14926. ((commandp org-speed-command)
  14927. (setq this-command org-speed-command)
  14928. (call-interactively org-speed-command))
  14929. ((functionp org-speed-command)
  14930. (funcall org-speed-command))
  14931. ((and org-speed-command (listp org-speed-command))
  14932. (eval org-speed-command))
  14933. (t (let (org-use-speed-commands)
  14934. (call-interactively 'org-self-insert-command)))))
  14935. ((and
  14936. (org-table-p)
  14937. (progn
  14938. ;; check if we blank the field, and if that triggers align
  14939. (and (featurep 'org-table) org-table-auto-blank-field
  14940. (member last-command
  14941. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14942. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14943. ;; got extra space, this field does not determine column width
  14944. (let (org-table-may-need-update) (org-table-blank-field))
  14945. ;; no extra space, this field may determine column width
  14946. (org-table-blank-field)))
  14947. t)
  14948. (eq N 1)
  14949. (looking-at "[^|\n]* |"))
  14950. (let (org-table-may-need-update)
  14951. (goto-char (1- (match-end 0)))
  14952. (delete-backward-char 1)
  14953. (goto-char (match-beginning 0))
  14954. (self-insert-command N)))
  14955. (t
  14956. (setq org-table-may-need-update t)
  14957. (self-insert-command N)
  14958. (org-fix-tags-on-the-fly)
  14959. (if org-self-insert-cluster-for-undo
  14960. (if (not (eq last-command 'org-self-insert-command))
  14961. (setq org-self-insert-command-undo-counter 1)
  14962. (if (>= org-self-insert-command-undo-counter 20)
  14963. (setq org-self-insert-command-undo-counter 1)
  14964. (and (> org-self-insert-command-undo-counter 0)
  14965. buffer-undo-list
  14966. (not (cadr buffer-undo-list)) ; remove nil entry
  14967. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14968. (setq org-self-insert-command-undo-counter
  14969. (1+ org-self-insert-command-undo-counter))))))))
  14970. (defun org-fix-tags-on-the-fly ()
  14971. (when (and (equal (char-after (point-at-bol)) ?*)
  14972. (org-on-heading-p))
  14973. (org-align-tags-here org-tags-column)))
  14974. (defun org-delete-backward-char (N)
  14975. "Like `delete-backward-char', insert whitespace at field end in tables.
  14976. When deleting backwards, in tables this function will insert whitespace in
  14977. front of the next \"|\" separator, to keep the table aligned. The table will
  14978. still be marked for re-alignment if the field did fill the entire column,
  14979. because, in this case the deletion might narrow the column."
  14980. (interactive "p")
  14981. (if (and (org-table-p)
  14982. (eq N 1)
  14983. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14984. (looking-at ".*?|"))
  14985. (let ((pos (point))
  14986. (noalign (looking-at "[^|\n\r]* |"))
  14987. (c org-table-may-need-update))
  14988. (backward-delete-char N)
  14989. (skip-chars-forward "^|")
  14990. (insert " ")
  14991. (goto-char (1- pos))
  14992. ;; noalign: if there were two spaces at the end, this field
  14993. ;; does not determine the width of the column.
  14994. (if noalign (setq org-table-may-need-update c)))
  14995. (backward-delete-char N)
  14996. (org-fix-tags-on-the-fly)))
  14997. (defun org-delete-char (N)
  14998. "Like `delete-char', but insert whitespace at field end in tables.
  14999. When deleting characters, in tables this function will insert whitespace in
  15000. front of the next \"|\" separator, to keep the table aligned. The table will
  15001. still be marked for re-alignment if the field did fill the entire column,
  15002. because, in this case the deletion might narrow the column."
  15003. (interactive "p")
  15004. (if (and (org-table-p)
  15005. (not (bolp))
  15006. (not (= (char-after) ?|))
  15007. (eq N 1))
  15008. (if (looking-at ".*?|")
  15009. (let ((pos (point))
  15010. (noalign (looking-at "[^|\n\r]* |"))
  15011. (c org-table-may-need-update))
  15012. (replace-match (concat
  15013. (substring (match-string 0) 1 -1)
  15014. " |"))
  15015. (goto-char pos)
  15016. ;; noalign: if there were two spaces at the end, this field
  15017. ;; does not determine the width of the column.
  15018. (if noalign (setq org-table-may-need-update c)))
  15019. (delete-char N))
  15020. (delete-char N)
  15021. (org-fix-tags-on-the-fly)))
  15022. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15023. (put 'org-self-insert-command 'delete-selection t)
  15024. (put 'orgtbl-self-insert-command 'delete-selection t)
  15025. (put 'org-delete-char 'delete-selection 'supersede)
  15026. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15027. (put 'org-yank 'delete-selection 'yank)
  15028. ;; Make `flyspell-mode' delay after some commands
  15029. (put 'org-self-insert-command 'flyspell-delayed t)
  15030. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15031. (put 'org-delete-char 'flyspell-delayed t)
  15032. (put 'org-delete-backward-char 'flyspell-delayed t)
  15033. ;; Make pabbrev-mode expand after org-mode commands
  15034. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15035. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15036. ;; How to do this: Measure non-white length of current string
  15037. ;; If equal to column width, we should realign.
  15038. (defun org-remap (map &rest commands)
  15039. "In MAP, remap the functions given in COMMANDS.
  15040. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15041. (let (new old)
  15042. (while commands
  15043. (setq old (pop commands) new (pop commands))
  15044. (if (fboundp 'command-remapping)
  15045. (org-defkey map (vector 'remap old) new)
  15046. (substitute-key-definition old new map global-map)))))
  15047. (when (eq org-enable-table-editor 'optimized)
  15048. ;; If the user wants maximum table support, we need to hijack
  15049. ;; some standard editing functions
  15050. (org-remap org-mode-map
  15051. 'self-insert-command 'org-self-insert-command
  15052. 'delete-char 'org-delete-char
  15053. 'delete-backward-char 'org-delete-backward-char)
  15054. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15055. (defvar org-ctrl-c-ctrl-c-hook nil
  15056. "Hook for functions attaching themselves to `C-c C-c'.
  15057. This can be used to add additional functionality to the C-c C-c key which
  15058. executes context-dependent commands.
  15059. Each function will be called with no arguments. The function must check
  15060. if the context is appropriate for it to act. If yes, it should do its
  15061. thing and then return a non-nil value. If the context is wrong,
  15062. just do nothing and return nil.")
  15063. (defvar org-tab-first-hook nil
  15064. "Hook for functions to attach themselves to TAB.
  15065. See `org-ctrl-c-ctrl-c-hook' for more information.
  15066. This hook runs as the first action when TAB is pressed, even before
  15067. `org-cycle' messes around with the `outline-regexp' to cater for
  15068. inline tasks and plain list item folding.
  15069. If any function in this hook returns t, any other actions that
  15070. would have been caused by TAB (such as table field motion or visibility
  15071. cycling) will not occur.")
  15072. (defvar org-tab-after-check-for-table-hook nil
  15073. "Hook for functions to attach themselves to TAB.
  15074. See `org-ctrl-c-ctrl-c-hook' for more information.
  15075. This hook runs after it has been established that the cursor is not in a
  15076. table, but before checking if the cursor is in a headline or if global cycling
  15077. should be done.
  15078. If any function in this hook returns t, not other actions like visibility
  15079. cycling will be done.")
  15080. (defvar org-tab-after-check-for-cycling-hook nil
  15081. "Hook for functions to attach themselves to TAB.
  15082. See `org-ctrl-c-ctrl-c-hook' for more information.
  15083. This hook runs after it has been established that not table field motion and
  15084. not visibility should be done because of current context. This is probably
  15085. the place where a package like yasnippets can hook in.")
  15086. (defvar org-tab-before-tab-emulation-hook nil
  15087. "Hook for functions to attach themselves to TAB.
  15088. See `org-ctrl-c-ctrl-c-hook' for more information.
  15089. This hook runs after every other options for TAB have been exhausted, but
  15090. before indentation and \t insertion takes place.")
  15091. (defvar org-metaleft-hook nil
  15092. "Hook for functions attaching themselves to `M-left'.
  15093. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15094. (defvar org-metaright-hook nil
  15095. "Hook for functions attaching themselves to `M-right'.
  15096. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15097. (defvar org-metaup-hook nil
  15098. "Hook for functions attaching themselves to `M-up'.
  15099. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15100. (defvar org-metadown-hook nil
  15101. "Hook for functions attaching themselves to `M-down'.
  15102. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15103. (defvar org-shiftmetaleft-hook nil
  15104. "Hook for functions attaching themselves to `M-S-left'.
  15105. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15106. (defvar org-shiftmetaright-hook nil
  15107. "Hook for functions attaching themselves to `M-S-right'.
  15108. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15109. (defvar org-shiftmetaup-hook nil
  15110. "Hook for functions attaching themselves to `M-S-up'.
  15111. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15112. (defvar org-shiftmetadown-hook nil
  15113. "Hook for functions attaching themselves to `M-S-down'.
  15114. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15115. (defvar org-metareturn-hook nil
  15116. "Hook for functions attaching themselves to `M-RET'.
  15117. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15118. (defvar org-shiftup-hook nil
  15119. "Hook for functions attaching themselves to `S-up'.
  15120. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15121. (defvar org-shiftup-final-hook nil
  15122. "Hook for functions attaching themselves to `S-up'.
  15123. This one runs after all other options except shift-select have been excluded.
  15124. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15125. (defvar org-shiftdown-hook nil
  15126. "Hook for functions attaching themselves to `S-down'.
  15127. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15128. (defvar org-shiftdown-final-hook nil
  15129. "Hook for functions attaching themselves to `S-down'.
  15130. This one runs after all other options except shift-select have been excluded.
  15131. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15132. (defvar org-shiftleft-hook nil
  15133. "Hook for functions attaching themselves to `S-left'.
  15134. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15135. (defvar org-shiftleft-final-hook nil
  15136. "Hook for functions attaching themselves to `S-left'.
  15137. This one runs after all other options except shift-select have been excluded.
  15138. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15139. (defvar org-shiftright-hook nil
  15140. "Hook for functions attaching themselves to `S-right'.
  15141. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15142. (defvar org-shiftright-final-hook nil
  15143. "Hook for functions attaching themselves to `S-right'.
  15144. This one runs after all other options except shift-select have been excluded.
  15145. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15146. (defun org-modifier-cursor-error ()
  15147. "Throw an error, a modified cursor command was applied in wrong context."
  15148. (error "This command is active in special context like tables, headlines or items"))
  15149. (defun org-shiftselect-error ()
  15150. "Throw an error because Shift-Cursor command was applied in wrong context."
  15151. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15152. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15153. (error "This command works only in special context like headlines or timestamps")))
  15154. (defun org-call-for-shift-select (cmd)
  15155. (let ((this-command-keys-shift-translated t))
  15156. (call-interactively cmd)))
  15157. (defun org-shifttab (&optional arg)
  15158. "Global visibility cycling or move to previous table field.
  15159. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15160. on context.
  15161. See the individual commands for more information."
  15162. (interactive "P")
  15163. (cond
  15164. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15165. ((integerp arg)
  15166. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15167. (message "Content view to level: %d" arg)
  15168. (org-content (prefix-numeric-value arg2))
  15169. (setq org-cycle-global-status 'overview)))
  15170. (t (call-interactively 'org-global-cycle))))
  15171. (defun org-shiftmetaleft ()
  15172. "Promote subtree or delete table column.
  15173. Calls `org-promote-subtree', `org-outdent-item',
  15174. or `org-table-delete-column', depending on context.
  15175. See the individual commands for more information."
  15176. (interactive)
  15177. (cond
  15178. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15179. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15180. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15181. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15182. (t (org-modifier-cursor-error))))
  15183. (defun org-shiftmetaright ()
  15184. "Demote subtree or insert table column.
  15185. Calls `org-demote-subtree', `org-indent-item',
  15186. or `org-table-insert-column', depending on context.
  15187. See the individual commands for more information."
  15188. (interactive)
  15189. (cond
  15190. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15191. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15192. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15193. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15194. (t (org-modifier-cursor-error))))
  15195. (defun org-shiftmetaup (&optional arg)
  15196. "Move subtree up or kill table row.
  15197. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15198. `org-move-item-up' depending on context. See the individual commands
  15199. for more information."
  15200. (interactive "P")
  15201. (cond
  15202. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15203. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15204. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15205. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15206. (t (org-modifier-cursor-error))))
  15207. (defun org-shiftmetadown (&optional arg)
  15208. "Move subtree down or insert table row.
  15209. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15210. `org-move-item-down', depending on context. See the individual
  15211. commands for more information."
  15212. (interactive "P")
  15213. (cond
  15214. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15215. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15216. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15217. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15218. (t (org-modifier-cursor-error))))
  15219. (defsubst org-hidden-tree-error ()
  15220. (error
  15221. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15222. (defun org-metaleft (&optional arg)
  15223. "Promote heading or move table column to left.
  15224. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15225. With no specific context, calls the Emacs default `backward-word'.
  15226. See the individual commands for more information."
  15227. (interactive "P")
  15228. (cond
  15229. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15230. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15231. ((or (org-on-heading-p)
  15232. (and (org-region-active-p)
  15233. (save-excursion
  15234. (goto-char (region-beginning))
  15235. (org-on-heading-p))))
  15236. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15237. (call-interactively 'org-do-promote))
  15238. ((or (org-at-item-p)
  15239. (and (org-region-active-p)
  15240. (save-excursion
  15241. (goto-char (region-beginning))
  15242. (org-at-item-p))))
  15243. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15244. (call-interactively 'org-outdent-item))
  15245. (t (call-interactively 'backward-word))))
  15246. (defun org-metaright (&optional arg)
  15247. "Demote subtree or move table column to right.
  15248. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15249. With no specific context, calls the Emacs default `forward-word'.
  15250. See the individual commands for more information."
  15251. (interactive "P")
  15252. (cond
  15253. ((run-hook-with-args-until-success 'org-metaright-hook))
  15254. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15255. ((or (org-on-heading-p)
  15256. (and (org-region-active-p)
  15257. (save-excursion
  15258. (goto-char (region-beginning))
  15259. (org-on-heading-p))))
  15260. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15261. (call-interactively 'org-do-demote))
  15262. ((or (org-at-item-p)
  15263. (and (org-region-active-p)
  15264. (save-excursion
  15265. (goto-char (region-beginning))
  15266. (org-at-item-p))))
  15267. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15268. (call-interactively 'org-indent-item))
  15269. (t (call-interactively 'forward-word))))
  15270. (defun org-check-for-hidden (what)
  15271. "Check if there are hidden headlines/items in the current visual line.
  15272. WHAT can be either `headlines' or `items'. If the current line is
  15273. an outline or item heading and it has a folded subtree below it,
  15274. this fucntion returns t, nil otherwise."
  15275. (let ((re (cond
  15276. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15277. ((eq what 'items) (concat "^" (org-item-re t)))
  15278. (t (error "This should not happen"))))
  15279. beg end)
  15280. (save-excursion
  15281. (catch 'exit
  15282. (unless (org-region-active-p)
  15283. (setq beg (point-at-bol))
  15284. (beginning-of-line 2)
  15285. (while (and (not (eobp)) ;; this is like `next-line'
  15286. (get-char-property (1- (point)) 'invisible))
  15287. (beginning-of-line 2))
  15288. (setq end (point))
  15289. (goto-char beg)
  15290. (goto-char (point-at-eol))
  15291. (setq end (max end (point)))
  15292. (while (re-search-forward re end t)
  15293. (if (get-char-property (match-beginning 0) 'invisible)
  15294. (throw 'exit t))))
  15295. nil))))
  15296. (defun org-metaup (&optional arg)
  15297. "Move subtree up or move table row up.
  15298. Calls `org-move-subtree-up' or `org-table-move-row' or
  15299. `org-move-item-up', depending on context. See the individual commands
  15300. for more information."
  15301. (interactive "P")
  15302. (cond
  15303. ((run-hook-with-args-until-success 'org-metaup-hook))
  15304. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15305. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15306. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15307. (t (transpose-lines 1) (beginning-of-line -1))))
  15308. (defun org-metadown (&optional arg)
  15309. "Move subtree down or move table row down.
  15310. Calls `org-move-subtree-down' or `org-table-move-row' or
  15311. `org-move-item-down', depending on context. See the individual
  15312. commands for more information."
  15313. (interactive "P")
  15314. (cond
  15315. ((run-hook-with-args-until-success 'org-metadown-hook))
  15316. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15317. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15318. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15319. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15320. (defun org-shiftup (&optional arg)
  15321. "Increase item in timestamp or increase priority of current headline.
  15322. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15323. depending on context. See the individual commands for more information."
  15324. (interactive "P")
  15325. (cond
  15326. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15327. ((and org-support-shift-select (org-region-active-p))
  15328. (org-call-for-shift-select 'previous-line))
  15329. ((org-at-timestamp-p t)
  15330. (call-interactively (if org-edit-timestamp-down-means-later
  15331. 'org-timestamp-down 'org-timestamp-up)))
  15332. ((and (not (eq org-support-shift-select 'always))
  15333. org-enable-priority-commands
  15334. (org-on-heading-p))
  15335. (call-interactively 'org-priority-up))
  15336. ((and (not org-support-shift-select) (org-at-item-p))
  15337. (call-interactively 'org-previous-item))
  15338. ((org-clocktable-try-shift 'up arg))
  15339. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15340. (org-support-shift-select
  15341. (org-call-for-shift-select 'previous-line))
  15342. (t (org-shiftselect-error))))
  15343. (defun org-shiftdown (&optional arg)
  15344. "Decrease item in timestamp or decrease priority of current headline.
  15345. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15346. depending on context. See the individual commands for more information."
  15347. (interactive "P")
  15348. (cond
  15349. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15350. ((and org-support-shift-select (org-region-active-p))
  15351. (org-call-for-shift-select 'next-line))
  15352. ((org-at-timestamp-p t)
  15353. (call-interactively (if org-edit-timestamp-down-means-later
  15354. 'org-timestamp-up 'org-timestamp-down)))
  15355. ((and (not (eq org-support-shift-select 'always))
  15356. org-enable-priority-commands
  15357. (org-on-heading-p))
  15358. (call-interactively 'org-priority-down))
  15359. ((and (not org-support-shift-select) (org-at-item-p))
  15360. (call-interactively 'org-next-item))
  15361. ((org-clocktable-try-shift 'down arg))
  15362. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15363. (org-support-shift-select
  15364. (org-call-for-shift-select 'next-line))
  15365. (t (org-shiftselect-error))))
  15366. (defun org-shiftright (&optional arg)
  15367. "Cycle the thing at point or in the current line, depending on context.
  15368. Depending on context, this does one of the following:
  15369. - switch a timestamp at point one day into the future
  15370. - on a headline, switch to the next TODO keyword.
  15371. - on an item, switch entire list to the next bullet type
  15372. - on a property line, switch to the next allowed value
  15373. - on a clocktable definition line, move time block into the future"
  15374. (interactive "P")
  15375. (cond
  15376. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15377. ((and org-support-shift-select (org-region-active-p))
  15378. (org-call-for-shift-select 'forward-char))
  15379. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15380. ((and (not (eq org-support-shift-select 'always))
  15381. (org-on-heading-p))
  15382. (let ((org-inhibit-logging
  15383. (not org-treat-S-cursor-todo-selection-as-state-change))
  15384. (org-inhibit-blocking
  15385. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15386. (org-call-with-arg 'org-todo 'right)))
  15387. ((or (and org-support-shift-select
  15388. (not (eq org-support-shift-select 'always))
  15389. (org-at-item-bullet-p))
  15390. (and (not org-support-shift-select) (org-at-item-p)))
  15391. (org-call-with-arg 'org-cycle-list-bullet nil))
  15392. ((and (not (eq org-support-shift-select 'always))
  15393. (org-at-property-p))
  15394. (call-interactively 'org-property-next-allowed-value))
  15395. ((org-clocktable-try-shift 'right arg))
  15396. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15397. (org-support-shift-select
  15398. (org-call-for-shift-select 'forward-char))
  15399. (t (org-shiftselect-error))))
  15400. (defun org-shiftleft (&optional arg)
  15401. "Cycle the thing at point or in the current line, depending on context.
  15402. Depending on context, this does one of the following:
  15403. - switch a timestamp at point one day into the past
  15404. - on a headline, switch to the previous TODO keyword.
  15405. - on an item, switch entire list to the previous bullet type
  15406. - on a property line, switch to the previous allowed value
  15407. - on a clocktable definition line, move time block into the past"
  15408. (interactive "P")
  15409. (cond
  15410. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15411. ((and org-support-shift-select (org-region-active-p))
  15412. (org-call-for-shift-select 'backward-char))
  15413. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15414. ((and (not (eq org-support-shift-select 'always))
  15415. (org-on-heading-p))
  15416. (let ((org-inhibit-logging
  15417. (not org-treat-S-cursor-todo-selection-as-state-change))
  15418. (org-inhibit-blocking
  15419. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15420. (org-call-with-arg 'org-todo 'left)))
  15421. ((or (and org-support-shift-select
  15422. (not (eq org-support-shift-select 'always))
  15423. (org-at-item-bullet-p))
  15424. (and (not org-support-shift-select) (org-at-item-p)))
  15425. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15426. ((and (not (eq org-support-shift-select 'always))
  15427. (org-at-property-p))
  15428. (call-interactively 'org-property-previous-allowed-value))
  15429. ((org-clocktable-try-shift 'left arg))
  15430. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15431. (org-support-shift-select
  15432. (org-call-for-shift-select 'backward-char))
  15433. (t (org-shiftselect-error))))
  15434. (defun org-shiftcontrolright ()
  15435. "Switch to next TODO set."
  15436. (interactive)
  15437. (cond
  15438. ((and org-support-shift-select (org-region-active-p))
  15439. (org-call-for-shift-select 'forward-word))
  15440. ((and (not (eq org-support-shift-select 'always))
  15441. (org-on-heading-p))
  15442. (org-call-with-arg 'org-todo 'nextset))
  15443. (org-support-shift-select
  15444. (org-call-for-shift-select 'forward-word))
  15445. (t (org-shiftselect-error))))
  15446. (defun org-shiftcontrolleft ()
  15447. "Switch to previous TODO set."
  15448. (interactive)
  15449. (cond
  15450. ((and org-support-shift-select (org-region-active-p))
  15451. (org-call-for-shift-select 'backward-word))
  15452. ((and (not (eq org-support-shift-select 'always))
  15453. (org-on-heading-p))
  15454. (org-call-with-arg 'org-todo 'previousset))
  15455. (org-support-shift-select
  15456. (org-call-for-shift-select 'backward-word))
  15457. (t (org-shiftselect-error))))
  15458. (defun org-ctrl-c-ret ()
  15459. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15460. (interactive)
  15461. (cond
  15462. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15463. (t (call-interactively 'org-insert-heading))))
  15464. (defun org-copy-special ()
  15465. "Copy region in table or copy current subtree.
  15466. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15467. See the individual commands for more information."
  15468. (interactive)
  15469. (call-interactively
  15470. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15471. (defun org-cut-special ()
  15472. "Cut region in table or cut current subtree.
  15473. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15474. See the individual commands for more information."
  15475. (interactive)
  15476. (call-interactively
  15477. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15478. (defun org-paste-special (arg)
  15479. "Paste rectangular region into table, or past subtree relative to level.
  15480. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15481. See the individual commands for more information."
  15482. (interactive "P")
  15483. (if (org-at-table-p)
  15484. (org-table-paste-rectangle)
  15485. (org-paste-subtree arg)))
  15486. (defun org-edit-special (&optional arg)
  15487. "Call a special editor for the stuff at point.
  15488. When at a table, call the formula editor with `org-table-edit-formulas'.
  15489. When at the first line of an src example, call `org-edit-src-code'.
  15490. When in an #+include line, visit the include file. Otherwise call
  15491. `ffap' to visit the file at point."
  15492. (interactive)
  15493. ;; possibly prep session before editing source
  15494. (when arg
  15495. (let* ((info (org-babel-get-src-block-info))
  15496. (lang (nth 0 info))
  15497. (params (nth 2 info))
  15498. (session (cdr (assoc :session params))))
  15499. (when (and info session) ;; we are in a source-code block with a session
  15500. (funcall
  15501. (intern (concat "org-babel-prep-session:" lang)) session params))))
  15502. (cond ;; proceed with `org-edit-special'
  15503. ((save-excursion
  15504. (beginning-of-line 1)
  15505. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15506. (find-file (org-trim (match-string 1))))
  15507. ((org-edit-src-code))
  15508. ((org-edit-fixed-width-region))
  15509. ((org-at-table.el-p)
  15510. (org-edit-src-code))
  15511. ((org-at-table-p)
  15512. (call-interactively 'org-table-edit-formulas))
  15513. (t (call-interactively 'ffap))))
  15514. (defun org-ctrl-c-ctrl-c (&optional arg)
  15515. "Set tags in headline, or update according to changed information at point.
  15516. This command does many different things, depending on context:
  15517. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15518. this is what we do.
  15519. - If the cursor is on a statistics cookie, update it.
  15520. - If the cursor is in a headline, prompt for tags and insert them
  15521. into the current line, aligned to `org-tags-column'. When called
  15522. with prefix arg, realign all tags in the current buffer.
  15523. - If the cursor is in one of the special #+KEYWORD lines, this
  15524. triggers scanning the buffer for these lines and updating the
  15525. information.
  15526. - If the cursor is inside a table, realign the table. This command
  15527. works even if the automatic table editor has been turned off.
  15528. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15529. the entire table.
  15530. - If the cursor is at a footnote reference or definition, jump to
  15531. the corresponding definition or references, respectively.
  15532. - If the cursor is a the beginning of a dynamic block, update it.
  15533. - If the current buffer is a remember buffer, close note and file
  15534. it. A prefix argument of 1 files to the default location
  15535. without further interaction. A prefix argument of 2 files to
  15536. the currently clocking task.
  15537. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  15538. links in this buffer.
  15539. - If the cursor is on a numbered item in a plain list, renumber the
  15540. ordered list.
  15541. - If the cursor is on a checkbox, toggle it."
  15542. (interactive "P")
  15543. (let ((org-enable-table-editor t))
  15544. (cond
  15545. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15546. org-occur-highlights
  15547. org-latex-fragment-image-overlays)
  15548. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15549. (org-remove-occur-highlights)
  15550. (org-remove-latex-fragment-image-overlays)
  15551. (message "Temporary highlights/overlays removed from current buffer"))
  15552. ((and (local-variable-p 'org-finish-function (current-buffer))
  15553. (fboundp org-finish-function))
  15554. (funcall org-finish-function))
  15555. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15556. ((or (looking-at org-property-start-re)
  15557. (org-at-property-p))
  15558. (call-interactively 'org-property-action))
  15559. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15560. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15561. (or (org-on-heading-p) (org-at-item-p)))
  15562. (call-interactively 'org-update-statistics-cookies))
  15563. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15564. ((org-at-table.el-p)
  15565. (message "Use C-c ' to edit table.el tables"))
  15566. ((org-at-table-p)
  15567. (org-table-maybe-eval-formula)
  15568. (if arg
  15569. (call-interactively 'org-table-recalculate)
  15570. (org-table-maybe-recalculate-line))
  15571. (call-interactively 'org-table-align))
  15572. ((or (org-footnote-at-reference-p)
  15573. (org-footnote-at-definition-p))
  15574. (call-interactively 'org-footnote-action))
  15575. ((org-at-item-checkbox-p)
  15576. (call-interactively 'org-toggle-checkbox))
  15577. ((org-at-item-p)
  15578. (if arg
  15579. (call-interactively 'org-toggle-checkbox)
  15580. (call-interactively 'org-maybe-renumber-ordered-list)))
  15581. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15582. ;; Dynamic block
  15583. (beginning-of-line 1)
  15584. (save-excursion (org-update-dblock)))
  15585. ((save-excursion
  15586. (beginning-of-line 1)
  15587. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15588. (cond
  15589. ((equal (match-string 1) "TBLFM")
  15590. ;; Recalculate the table before this line
  15591. (save-excursion
  15592. (beginning-of-line 1)
  15593. (skip-chars-backward " \r\n\t")
  15594. (if (org-at-table-p)
  15595. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15596. (t
  15597. (let ((org-inhibit-startup-visibility-stuff t)
  15598. (org-startup-align-all-tables nil))
  15599. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15600. (message "Local setup has been refreshed"))))
  15601. ((org-clock-update-time-maybe))
  15602. (t (error "C-c C-c can do nothing useful at this location")))))
  15603. (defun org-mode-restart ()
  15604. "Restart Org-mode, to scan again for special lines.
  15605. Also updates the keyword regular expressions."
  15606. (interactive)
  15607. (org-mode)
  15608. (message "Org-mode restarted"))
  15609. (defun org-kill-note-or-show-branches ()
  15610. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15611. (interactive)
  15612. (if (not org-finish-function)
  15613. (progn
  15614. (hide-subtree)
  15615. (call-interactively 'show-branches))
  15616. (let ((org-note-abort t))
  15617. (funcall org-finish-function))))
  15618. (defun org-return (&optional indent)
  15619. "Goto next table row or insert a newline.
  15620. Calls `org-table-next-row' or `newline', depending on context.
  15621. See the individual commands for more information."
  15622. (interactive)
  15623. (cond
  15624. ((bobp) (if indent (newline-and-indent) (newline)))
  15625. ((org-at-table-p)
  15626. (org-table-justify-field-maybe)
  15627. (call-interactively 'org-table-next-row))
  15628. ((and org-return-follows-link
  15629. (eq (get-text-property (point) 'face) 'org-link))
  15630. (call-interactively 'org-open-at-point))
  15631. ((and (org-at-heading-p)
  15632. (looking-at
  15633. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15634. (org-show-entry)
  15635. (end-of-line 1)
  15636. (newline))
  15637. (t (if indent (newline-and-indent) (newline)))))
  15638. (defun org-return-indent ()
  15639. "Goto next table row or insert a newline and indent.
  15640. Calls `org-table-next-row' or `newline-and-indent', depending on
  15641. context. See the individual commands for more information."
  15642. (interactive)
  15643. (org-return t))
  15644. (defun org-ctrl-c-star ()
  15645. "Compute table, or change heading status of lines.
  15646. Calls `org-table-recalculate' or `org-toggle-heading',
  15647. depending on context."
  15648. (interactive)
  15649. (cond
  15650. ((org-at-table-p)
  15651. (call-interactively 'org-table-recalculate))
  15652. (t
  15653. ;; Convert all lines in region to list items
  15654. (call-interactively 'org-toggle-heading))))
  15655. (defun org-ctrl-c-minus ()
  15656. "Insert separator line in table or modify bullet status of line.
  15657. Also turns a plain line or a region of lines into list items.
  15658. Calls `org-table-insert-hline', `org-toggle-item', or
  15659. `org-cycle-list-bullet', depending on context."
  15660. (interactive)
  15661. (cond
  15662. ((org-at-table-p)
  15663. (call-interactively 'org-table-insert-hline))
  15664. ((org-region-active-p)
  15665. (call-interactively 'org-toggle-item))
  15666. ((org-in-item-p)
  15667. (call-interactively 'org-cycle-list-bullet))
  15668. (t
  15669. (call-interactively 'org-toggle-item))))
  15670. (defun org-toggle-item ()
  15671. "Convert headings or normal lines to items, items to normal lines.
  15672. If there is no active region, only the current line is considered.
  15673. If the first line in the region is a headline, convert all headlines to items.
  15674. If the first line in the region is an item, convert all items to normal lines.
  15675. If the first line is normal text, add an item bullet to each line."
  15676. (interactive)
  15677. (let (l2 l beg end)
  15678. (if (org-region-active-p)
  15679. (setq beg (region-beginning) end (region-end))
  15680. (setq beg (point-at-bol)
  15681. end (min (1+ (point-at-eol)) (point-max))))
  15682. (save-excursion
  15683. (goto-char end)
  15684. (setq l2 (org-current-line))
  15685. (goto-char beg)
  15686. (beginning-of-line 1)
  15687. (setq l (1- (org-current-line)))
  15688. (if (org-at-item-p)
  15689. ;; We already have items, de-itemize
  15690. (while (< (setq l (1+ l)) l2)
  15691. (when (org-at-item-p)
  15692. (goto-char (match-beginning 2))
  15693. (delete-region (match-beginning 2) (match-end 2))
  15694. (and (looking-at "[ \t]+") (replace-match "")))
  15695. (beginning-of-line 2))
  15696. (if (org-on-heading-p)
  15697. ;; Headings, convert to items
  15698. (while (< (setq l (1+ l)) l2)
  15699. (if (looking-at org-outline-regexp)
  15700. (replace-match "- " t t))
  15701. (beginning-of-line 2))
  15702. ;; normal lines, turn them into items
  15703. (while (< (setq l (1+ l)) l2)
  15704. (unless (org-at-item-p)
  15705. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15706. (replace-match "\\1- \\2")))
  15707. (beginning-of-line 2)))))))
  15708. (defun org-toggle-heading (&optional nstars)
  15709. "Convert headings to normal text, or items or text to headings.
  15710. If there is no active region, only the current line is considered.
  15711. If the first line is a heading, remove the stars from all headlines
  15712. in the region.
  15713. If the first line is a plain list item, turn all plain list items
  15714. into headings.
  15715. If the first line is a normal line, turn each and every line in the
  15716. region into a heading.
  15717. When converting a line into a heading, the number of stars is chosen
  15718. such that the lines become children of the current entry. However,
  15719. when a prefix argument is given, its value determines the number of
  15720. stars to add."
  15721. (interactive "P")
  15722. (let (l2 l itemp beg end)
  15723. (if (org-region-active-p)
  15724. (setq beg (region-beginning) end (region-end))
  15725. (setq beg (point-at-bol)
  15726. end (min (1+ (point-at-eol)) (point-max))))
  15727. (save-excursion
  15728. (goto-char end)
  15729. (setq l2 (org-current-line))
  15730. (goto-char beg)
  15731. (beginning-of-line 1)
  15732. (setq l (1- (org-current-line)))
  15733. (if (org-on-heading-p)
  15734. ;; We already have headlines, de-star them
  15735. (while (< (setq l (1+ l)) l2)
  15736. (when (org-on-heading-p t)
  15737. (and (looking-at outline-regexp) (replace-match "")))
  15738. (beginning-of-line 2))
  15739. (setq itemp (org-at-item-p))
  15740. (let* ((stars
  15741. (if nstars
  15742. (make-string (prefix-numeric-value current-prefix-arg)
  15743. ?*)
  15744. (save-excursion
  15745. (if (re-search-backward org-complex-heading-regexp nil t)
  15746. (match-string 1) ""))))
  15747. (add-stars (cond (nstars "")
  15748. ((equal stars "") "*")
  15749. (org-odd-levels-only "**")
  15750. (t "*")))
  15751. (rpl (concat stars add-stars " ")))
  15752. (while (< (setq l (1+ l)) l2)
  15753. (if itemp
  15754. (and (org-at-item-p) (replace-match rpl t t))
  15755. (unless (org-on-heading-p)
  15756. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15757. (replace-match (concat rpl (match-string 2))))))
  15758. (beginning-of-line 2)))))))
  15759. (defun org-meta-return (&optional arg)
  15760. "Insert a new heading or wrap a region in a table.
  15761. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15762. See the individual commands for more information."
  15763. (interactive "P")
  15764. (cond
  15765. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15766. ((org-at-table-p)
  15767. (call-interactively 'org-table-wrap-region))
  15768. (t (call-interactively 'org-insert-heading))))
  15769. ;;; Menu entries
  15770. ;; Define the Org-mode menus
  15771. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15772. '("Tbl"
  15773. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15774. ["Next Field" org-cycle (org-at-table-p)]
  15775. ["Previous Field" org-shifttab (org-at-table-p)]
  15776. ["Next Row" org-return (org-at-table-p)]
  15777. "--"
  15778. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15779. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15780. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15781. "--"
  15782. ("Column"
  15783. ["Move Column Left" org-metaleft (org-at-table-p)]
  15784. ["Move Column Right" org-metaright (org-at-table-p)]
  15785. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15786. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15787. ("Row"
  15788. ["Move Row Up" org-metaup (org-at-table-p)]
  15789. ["Move Row Down" org-metadown (org-at-table-p)]
  15790. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15791. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15792. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15793. "--"
  15794. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15795. ("Rectangle"
  15796. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15797. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15798. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15799. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15800. "--"
  15801. ("Calculate"
  15802. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15803. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15804. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15805. "--"
  15806. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15807. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15808. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15809. "--"
  15810. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15811. "--"
  15812. ["Sum Column/Rectangle" org-table-sum
  15813. (or (org-at-table-p) (org-region-active-p))]
  15814. ["Which Column?" org-table-current-column (org-at-table-p)])
  15815. ["Debug Formulas"
  15816. org-table-toggle-formula-debugger
  15817. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15818. ["Show Col/Row Numbers"
  15819. org-table-toggle-coordinate-overlays
  15820. :style toggle
  15821. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15822. "--"
  15823. ["Create" org-table-create (and (not (org-at-table-p))
  15824. org-enable-table-editor)]
  15825. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15826. ["Import from File" org-table-import (not (org-at-table-p))]
  15827. ["Export to File" org-table-export (org-at-table-p)]
  15828. "--"
  15829. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15830. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15831. '("Org"
  15832. ("Show/Hide"
  15833. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15834. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15835. ["Sparse Tree..." org-sparse-tree t]
  15836. ["Reveal Context" org-reveal t]
  15837. ["Show All" show-all t]
  15838. "--"
  15839. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15840. "--"
  15841. ["New Heading" org-insert-heading t]
  15842. ("Navigate Headings"
  15843. ["Up" outline-up-heading t]
  15844. ["Next" outline-next-visible-heading t]
  15845. ["Previous" outline-previous-visible-heading t]
  15846. ["Next Same Level" outline-forward-same-level t]
  15847. ["Previous Same Level" outline-backward-same-level t]
  15848. "--"
  15849. ["Jump" org-goto t])
  15850. ("Edit Structure"
  15851. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15852. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15853. "--"
  15854. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15855. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15856. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15857. "--"
  15858. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15859. "--"
  15860. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15861. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15862. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15863. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15864. "--"
  15865. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15866. "--"
  15867. ["Convert to odd levels" org-convert-to-odd-levels t]
  15868. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15869. ("Editing"
  15870. ["Emphasis..." org-emphasize t]
  15871. ["Edit Source Example" org-edit-special t]
  15872. "--"
  15873. ["Footnote new/jump" org-footnote-action t]
  15874. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15875. ("Archive"
  15876. ["Archive (default method)" org-archive-subtree-default t]
  15877. "--"
  15878. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15879. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15880. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15881. )
  15882. "--"
  15883. ("Hyperlinks"
  15884. ["Store Link (Global)" org-store-link t]
  15885. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15886. ["Insert Link" org-insert-link t]
  15887. ["Follow Link" org-open-at-point t]
  15888. "--"
  15889. ["Next link" org-next-link t]
  15890. ["Previous link" org-previous-link t]
  15891. "--"
  15892. ["Descriptive Links"
  15893. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15894. :style radio
  15895. :selected (member '(org-link) buffer-invisibility-spec)]
  15896. ["Literal Links"
  15897. (progn
  15898. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15899. :style radio
  15900. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15901. "--"
  15902. ("TODO Lists"
  15903. ["TODO/DONE/-" org-todo t]
  15904. ("Select keyword"
  15905. ["Next keyword" org-shiftright (org-on-heading-p)]
  15906. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15907. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15908. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15909. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15910. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  15911. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  15912. "--"
  15913. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15914. :selected org-enforce-todo-dependencies :style toggle :active t]
  15915. "Settings for tree at point"
  15916. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15917. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15918. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15919. ["Do Children parallel" org-toggle-ordered-property :style radio
  15920. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15921. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15922. "--"
  15923. ["Set Priority" org-priority t]
  15924. ["Priority Up" org-shiftup t]
  15925. ["Priority Down" org-shiftdown t]
  15926. "--"
  15927. ["Get news from all feeds" org-feed-update-all t]
  15928. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15929. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15930. ("TAGS and Properties"
  15931. ["Set Tags" org-set-tags-command t]
  15932. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15933. "--"
  15934. ["Set property" org-set-property t]
  15935. ["Column view of properties" org-columns t]
  15936. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15937. ("Dates and Scheduling"
  15938. ["Timestamp" org-time-stamp t]
  15939. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15940. ("Change Date"
  15941. ["1 Day Later" org-shiftright t]
  15942. ["1 Day Earlier" org-shiftleft t]
  15943. ["1 ... Later" org-shiftup t]
  15944. ["1 ... Earlier" org-shiftdown t])
  15945. ["Compute Time Range" org-evaluate-time-range t]
  15946. ["Schedule Item" org-schedule t]
  15947. ["Deadline" org-deadline t]
  15948. "--"
  15949. ["Custom time format" org-toggle-time-stamp-overlays
  15950. :style radio :selected org-display-custom-times]
  15951. "--"
  15952. ["Goto Calendar" org-goto-calendar t]
  15953. ["Date from Calendar" org-date-from-calendar t]
  15954. "--"
  15955. ["Start/Restart Timer" org-timer-start t]
  15956. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15957. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15958. ["Insert Timer String" org-timer t]
  15959. ["Insert Timer Item" org-timer-item t])
  15960. ("Logging work"
  15961. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15962. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15963. ["Clock out" org-clock-out t]
  15964. ["Clock cancel" org-clock-cancel t]
  15965. "--"
  15966. ["Mark as default task" org-clock-mark-default-task t]
  15967. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15968. ["Goto running clock" org-clock-goto t]
  15969. "--"
  15970. ["Display times" org-clock-display t]
  15971. ["Create clock table" org-clock-report t]
  15972. "--"
  15973. ["Record DONE time"
  15974. (progn (setq org-log-done (not org-log-done))
  15975. (message "Switching to %s will %s record a timestamp"
  15976. (car org-done-keywords)
  15977. (if org-log-done "automatically" "not")))
  15978. :style toggle :selected org-log-done])
  15979. "--"
  15980. ["Agenda Command..." org-agenda t]
  15981. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15982. ("File List for Agenda")
  15983. ("Special views current file"
  15984. ["TODO Tree" org-show-todo-tree t]
  15985. ["Check Deadlines" org-check-deadlines t]
  15986. ["Timeline" org-timeline t]
  15987. ["Tags/Property tree" org-match-sparse-tree t])
  15988. "--"
  15989. ["Export/Publish..." org-export t]
  15990. ("LaTeX"
  15991. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15992. :selected org-cdlatex-mode]
  15993. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15994. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15995. ["Modify math symbol" org-cdlatex-math-modify
  15996. (org-inside-LaTeX-fragment-p)]
  15997. ["Insert citation" org-reftex-citation t]
  15998. "--"
  15999. ["Export LaTeX fragments as images"
  16000. (if (featurep 'org-exp)
  16001. (setq org-export-with-LaTeX-fragments
  16002. (not org-export-with-LaTeX-fragments))
  16003. (require 'org-exp))
  16004. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  16005. org-export-with-LaTeX-fragments)]
  16006. "--"
  16007. ["Template for BEAMER" org-insert-beamer-options-template t])
  16008. "--"
  16009. ("MobileOrg"
  16010. ["Push Files and Views" org-mobile-push t]
  16011. ["Get Captured and Flagged" org-mobile-pull t]
  16012. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16013. "--"
  16014. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16015. "--"
  16016. ("Documentation"
  16017. ["Show Version" org-version t]
  16018. ["Info Documentation" org-info t])
  16019. ("Customize"
  16020. ["Browse Org Group" org-customize t]
  16021. "--"
  16022. ["Expand This Menu" org-create-customize-menu
  16023. (fboundp 'customize-menu-create)])
  16024. ["Send bug report" org-submit-bug-report t]
  16025. "--"
  16026. ("Refresh/Reload"
  16027. ["Refresh setup current buffer" org-mode-restart t]
  16028. ["Reload Org (after update)" org-reload t]
  16029. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16030. ))
  16031. (defun org-info (&optional node)
  16032. "Read documentation for Org-mode in the info system.
  16033. With optional NODE, go directly to that node."
  16034. (interactive)
  16035. (info (format "(org)%s" (or node ""))))
  16036. ;;;###autoload
  16037. (defun org-submit-bug-report ()
  16038. "Submit a bug report on Org-mode via mail.
  16039. Don't hesitate to report any problems or inaccurate documentation.
  16040. If you don't have setup sending mail from (X)Emacs, please copy the
  16041. output buffer into your mail program, as it gives us important
  16042. information about your Org-mode version and configuration."
  16043. (interactive)
  16044. (require 'reporter)
  16045. (org-load-modules-maybe)
  16046. (org-require-autoloaded-modules)
  16047. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16048. (reporter-submit-bug-report
  16049. "emacs-orgmode@gnu.org"
  16050. (org-version)
  16051. (let (list)
  16052. (save-window-excursion
  16053. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16054. (delete-other-windows)
  16055. (erase-buffer)
  16056. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16057. We would like to add your full Org-mode and Outline configuration to the
  16058. bug report. This greatly simplifies the work of the maintainer and
  16059. other experts on the mailing list.
  16060. HOWEVER, some variables you have customized may contain private
  16061. information. The names of customers, colleagues, or friends, might
  16062. appear in the form of file names, tags, todo states, or search strings.
  16063. If you answer yes to the prompt, you might want to check and remove
  16064. such private information before sending the email.")
  16065. (add-text-properties (point-min) (point-max) '(face org-warning))
  16066. (when (yes-or-no-p "Include your Org-mode configuration ")
  16067. (mapatoms
  16068. (lambda (v)
  16069. (and (boundp v)
  16070. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16071. (or (and (symbol-value v)
  16072. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16073. (and
  16074. (get v 'custom-type) (get v 'standard-value)
  16075. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16076. (push v list)))))
  16077. (kill-buffer (get-buffer "*Warn about privacy*"))
  16078. list))
  16079. nil nil
  16080. "Remember to cover the basics, that is, what you expected to happen and
  16081. what in fact did happen. You don't know how to make a good report? See
  16082. http://orgmode.org/manual/Feedback.html#Feedback
  16083. Your bug report will be posted to the Org-mode mailing list.
  16084. ------------------------------------------------------------------------")
  16085. (save-excursion
  16086. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16087. (replace-match "\\1Bug: \\3 [\\2]")))))
  16088. (defun org-install-agenda-files-menu ()
  16089. (let ((bl (buffer-list)))
  16090. (save-excursion
  16091. (while bl
  16092. (set-buffer (pop bl))
  16093. (if (org-mode-p) (setq bl nil)))
  16094. (when (org-mode-p)
  16095. (easy-menu-change
  16096. '("Org") "File List for Agenda"
  16097. (append
  16098. (list
  16099. ["Edit File List" (org-edit-agenda-file-list) t]
  16100. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16101. ["Remove Current File from List" org-remove-file t]
  16102. ["Cycle through agenda files" org-cycle-agenda-files t]
  16103. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16104. "--")
  16105. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16106. ;;;; Documentation
  16107. ;;;###autoload
  16108. (defun org-require-autoloaded-modules ()
  16109. (interactive)
  16110. (mapc 'require
  16111. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16112. org-docbook org-exp org-html org-icalendar
  16113. org-id org-latex
  16114. org-publish org-remember org-table
  16115. org-timer org-xoxo)))
  16116. ;;;###autoload
  16117. (defun org-reload (&optional uncompiled)
  16118. "Reload all org lisp files.
  16119. With prefix arg UNCOMPILED, load the uncompiled versions."
  16120. (interactive "P")
  16121. (require 'find-func)
  16122. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16123. (dir-org (file-name-directory (org-find-library-name "org")))
  16124. (dir-org-contrib (ignore-errors
  16125. (file-name-directory
  16126. (org-find-library-name "org-contribdir"))))
  16127. (files
  16128. (append (directory-files dir-org t file-re)
  16129. (and dir-org-contrib
  16130. (directory-files dir-org-contrib t file-re))))
  16131. (remove-re (concat (if (featurep 'xemacs)
  16132. "org-colview" "org-colview-xemacs")
  16133. "\\'")))
  16134. (setq files (mapcar 'file-name-sans-extension files))
  16135. (setq files (mapcar
  16136. (lambda (x) (if (string-match remove-re x) nil x))
  16137. files))
  16138. (setq files (delq nil files))
  16139. (mapc
  16140. (lambda (f)
  16141. (when (featurep (intern (file-name-nondirectory f)))
  16142. (if (and (not uncompiled)
  16143. (file-exists-p (concat f ".elc")))
  16144. (load (concat f ".elc") nil nil t)
  16145. (load (concat f ".el") nil nil t))))
  16146. files))
  16147. (org-version))
  16148. ;;;###autoload
  16149. (defun org-customize ()
  16150. "Call the customize function with org as argument."
  16151. (interactive)
  16152. (org-load-modules-maybe)
  16153. (org-require-autoloaded-modules)
  16154. (customize-browse 'org))
  16155. (defun org-create-customize-menu ()
  16156. "Create a full customization menu for Org-mode, insert it into the menu."
  16157. (interactive)
  16158. (org-load-modules-maybe)
  16159. (org-require-autoloaded-modules)
  16160. (if (fboundp 'customize-menu-create)
  16161. (progn
  16162. (easy-menu-change
  16163. '("Org") "Customize"
  16164. `(["Browse Org group" org-customize t]
  16165. "--"
  16166. ,(customize-menu-create 'org)
  16167. ["Set" Custom-set t]
  16168. ["Save" Custom-save t]
  16169. ["Reset to Current" Custom-reset-current t]
  16170. ["Reset to Saved" Custom-reset-saved t]
  16171. ["Reset to Standard Settings" Custom-reset-standard t]))
  16172. (message "\"Org\"-menu now contains full customization menu"))
  16173. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16174. ;;;; Miscellaneous stuff
  16175. ;;; Generally useful functions
  16176. (defun org-get-at-bol (property)
  16177. "Get text property PROPERTY at beginning of line."
  16178. (get-text-property (point-at-bol) property))
  16179. (defun org-find-text-property-in-string (prop s)
  16180. "Return the first non-nil value of property PROP in string S."
  16181. (or (get-text-property 0 prop s)
  16182. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16183. prop s)))
  16184. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16185. "Display the given MESSAGE as a warning."
  16186. (if (fboundp 'display-warning)
  16187. (display-warning 'org message
  16188. (if (featurep 'xemacs) 'warning :warning))
  16189. (let ((buf (get-buffer-create "*Org warnings*")))
  16190. (with-current-buffer buf
  16191. (goto-char (point-max))
  16192. (insert "Warning (Org): " message)
  16193. (unless (bolp)
  16194. (newline)))
  16195. (display-buffer buf)
  16196. (sit-for 0))))
  16197. (defun org-in-commented-line ()
  16198. "Is point in a line starting with `#'?"
  16199. (equal (char-after (point-at-bol)) ?#))
  16200. (defun org-in-indented-comment-line ()
  16201. "Is point in a line starting with `#' after some white space?"
  16202. (save-excursion
  16203. (save-match-data
  16204. (goto-char (point-at-bol))
  16205. (looking-at "[ \t]*#"))))
  16206. (defun org-in-verbatim-emphasis ()
  16207. (save-match-data
  16208. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16209. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16210. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16211. (if (and marker (marker-buffer marker)
  16212. (buffer-live-p (marker-buffer marker)))
  16213. (progn
  16214. (switch-to-buffer (marker-buffer marker))
  16215. (if (or (> marker (point-max)) (< marker (point-min)))
  16216. (widen))
  16217. (goto-char marker)
  16218. (org-show-context 'org-goto))
  16219. (if bookmark
  16220. (bookmark-jump bookmark)
  16221. (error "Cannot find location"))))
  16222. (defun org-quote-csv-field (s)
  16223. "Quote field for inclusion in CSV material."
  16224. (if (string-match "[\",]" s)
  16225. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16226. s))
  16227. (defun org-plist-delete (plist property)
  16228. "Delete PROPERTY from PLIST.
  16229. This is in contrast to merely setting it to 0."
  16230. (let (p)
  16231. (while plist
  16232. (if (not (eq property (car plist)))
  16233. (setq p (plist-put p (car plist) (nth 1 plist))))
  16234. (setq plist (cddr plist)))
  16235. p))
  16236. (defun org-force-self-insert (N)
  16237. "Needed to enforce self-insert under remapping."
  16238. (interactive "p")
  16239. (self-insert-command N))
  16240. (defun org-string-width (s)
  16241. "Compute width of string, ignoring invisible characters.
  16242. This ignores character with invisibility property `org-link', and also
  16243. characters with property `org-cwidth', because these will become invisible
  16244. upon the next fontification round."
  16245. (let (b l)
  16246. (when (or (eq t buffer-invisibility-spec)
  16247. (assq 'org-link buffer-invisibility-spec))
  16248. (while (setq b (text-property-any 0 (length s)
  16249. 'invisible 'org-link s))
  16250. (setq s (concat (substring s 0 b)
  16251. (substring s (or (next-single-property-change
  16252. b 'invisible s) (length s)))))))
  16253. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16254. (setq s (concat (substring s 0 b)
  16255. (substring s (or (next-single-property-change
  16256. b 'org-cwidth s) (length s))))))
  16257. (setq l (string-width s) b -1)
  16258. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16259. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16260. l))
  16261. (defun org-get-indentation (&optional line)
  16262. "Get the indentation of the current line, interpreting tabs.
  16263. When LINE is given, assume it represents a line and compute its indentation."
  16264. (if line
  16265. (if (string-match "^ *" (org-remove-tabs line))
  16266. (match-end 0))
  16267. (save-excursion
  16268. (beginning-of-line 1)
  16269. (skip-chars-forward " \t")
  16270. (current-column))))
  16271. (defun org-remove-tabs (s &optional width)
  16272. "Replace tabulators in S with spaces.
  16273. Assumes that s is a single line, starting in column 0."
  16274. (setq width (or width tab-width))
  16275. (while (string-match "\t" s)
  16276. (setq s (replace-match
  16277. (make-string
  16278. (- (* width (/ (+ (match-beginning 0) width) width))
  16279. (match-beginning 0)) ?\ )
  16280. t t s)))
  16281. s)
  16282. (defun org-fix-indentation (line ind)
  16283. "Fix indentation in LINE.
  16284. IND is a cons cell with target and minimum indentation.
  16285. If the current indentation in LINE is smaller than the minimum,
  16286. leave it alone. If it is larger than ind, set it to the target."
  16287. (let* ((l (org-remove-tabs line))
  16288. (i (org-get-indentation l))
  16289. (i1 (car ind)) (i2 (cdr ind)))
  16290. (if (>= i i2) (setq l (substring line i2)))
  16291. (if (> i1 0)
  16292. (concat (make-string i1 ?\ ) l)
  16293. l)))
  16294. (defun org-remove-indentation (code &optional n)
  16295. "Remove the maximum common indentation from the lines in CODE.
  16296. N may optionally be the number of spaces to remove."
  16297. (with-temp-buffer
  16298. (insert code)
  16299. (org-do-remove-indentation n)
  16300. (buffer-string)))
  16301. (defun org-do-remove-indentation (&optional n)
  16302. "Remove the maximum common indentation from the buffer."
  16303. (untabify (point-min) (point-max))
  16304. (let ((min 10000) re)
  16305. (if n
  16306. (setq min n)
  16307. (goto-char (point-min))
  16308. (while (re-search-forward "^ *[^ \n]" nil t)
  16309. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16310. (unless (or (= min 0) (= min 10000))
  16311. (setq re (format "^ \\{%d\\}" min))
  16312. (goto-char (point-min))
  16313. (while (re-search-forward re nil t)
  16314. (replace-match "")
  16315. (end-of-line 1))
  16316. min)))
  16317. (defun org-fill-template (template alist)
  16318. "Find each %key of ALIST in TEMPLATE and replace it."
  16319. (let ((case-fold-search nil)
  16320. entry key value)
  16321. (setq alist (sort (copy-sequence alist)
  16322. (lambda (a b) (< (length (car a)) (length (car b))))))
  16323. (while (setq entry (pop alist))
  16324. (setq template
  16325. (replace-regexp-in-string
  16326. (concat "%" (regexp-quote (car entry)))
  16327. (cdr entry) template t t)))
  16328. template))
  16329. (defun org-base-buffer (buffer)
  16330. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16331. (if (not buffer)
  16332. buffer
  16333. (or (buffer-base-buffer buffer)
  16334. buffer)))
  16335. (defun org-trim (s)
  16336. "Remove whitespace at beginning and end of string."
  16337. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16338. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16339. s)
  16340. (defun org-wrap (string &optional width lines)
  16341. "Wrap string to either a number of lines, or a width in characters.
  16342. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16343. that costs. If there is a word longer than WIDTH, the text is actually
  16344. wrapped to the length of that word.
  16345. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16346. many lines, whatever width that takes.
  16347. The return value is a list of lines, without newlines at the end."
  16348. (let* ((words (org-split-string string "[ \t\n]+"))
  16349. (maxword (apply 'max (mapcar 'org-string-width words)))
  16350. w ll)
  16351. (cond (width
  16352. (org-do-wrap words (max maxword width)))
  16353. (lines
  16354. (setq w maxword)
  16355. (setq ll (org-do-wrap words maxword))
  16356. (if (<= (length ll) lines)
  16357. ll
  16358. (setq ll words)
  16359. (while (> (length ll) lines)
  16360. (setq w (1+ w))
  16361. (setq ll (org-do-wrap words w)))
  16362. ll))
  16363. (t (error "Cannot wrap this")))))
  16364. (defun org-do-wrap (words width)
  16365. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16366. (let (lines line)
  16367. (while words
  16368. (setq line (pop words))
  16369. (while (and words (< (+ (length line) (length (car words))) width))
  16370. (setq line (concat line " " (pop words))))
  16371. (setq lines (push line lines)))
  16372. (nreverse lines)))
  16373. (defun org-split-string (string &optional separators)
  16374. "Splits STRING into substrings at SEPARATORS.
  16375. No empty strings are returned if there are matches at the beginning
  16376. and end of string."
  16377. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16378. (start 0)
  16379. notfirst
  16380. (list nil))
  16381. (while (and (string-match rexp string
  16382. (if (and notfirst
  16383. (= start (match-beginning 0))
  16384. (< start (length string)))
  16385. (1+ start) start))
  16386. (< (match-beginning 0) (length string)))
  16387. (setq notfirst t)
  16388. (or (eq (match-beginning 0) 0)
  16389. (and (eq (match-beginning 0) (match-end 0))
  16390. (eq (match-beginning 0) start))
  16391. (setq list
  16392. (cons (substring string start (match-beginning 0))
  16393. list)))
  16394. (setq start (match-end 0)))
  16395. (or (eq start (length string))
  16396. (setq list
  16397. (cons (substring string start)
  16398. list)))
  16399. (nreverse list)))
  16400. (defun org-quote-vert (s)
  16401. "Replace \"|\" with \"\\vert\"."
  16402. (while (string-match "|" s)
  16403. (setq s (replace-match "\\vert" t t s)))
  16404. s)
  16405. (defun org-uuidgen-p (s)
  16406. "Is S an ID created by UUIDGEN?"
  16407. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16408. (defun org-context ()
  16409. "Return a list of contexts of the current cursor position.
  16410. If several contexts apply, all are returned.
  16411. Each context entry is a list with a symbol naming the context, and
  16412. two positions indicating start and end of the context. Possible
  16413. contexts are:
  16414. :headline anywhere in a headline
  16415. :headline-stars on the leading stars in a headline
  16416. :todo-keyword on a TODO keyword (including DONE) in a headline
  16417. :tags on the TAGS in a headline
  16418. :priority on the priority cookie in a headline
  16419. :item on the first line of a plain list item
  16420. :item-bullet on the bullet/number of a plain list item
  16421. :checkbox on the checkbox in a plain list item
  16422. :table in an org-mode table
  16423. :table-special on a special filed in a table
  16424. :table-table in a table.el table
  16425. :link on a hyperlink
  16426. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16427. :target on a <<target>>
  16428. :radio-target on a <<<radio-target>>>
  16429. :latex-fragment on a LaTeX fragment
  16430. :latex-preview on a LaTeX fragment with overlayed preview image
  16431. This function expects the position to be visible because it uses font-lock
  16432. faces as a help to recognize the following contexts: :table-special, :link,
  16433. and :keyword."
  16434. (let* ((f (get-text-property (point) 'face))
  16435. (faces (if (listp f) f (list f)))
  16436. (p (point)) clist o)
  16437. ;; First the large context
  16438. (cond
  16439. ((org-on-heading-p t)
  16440. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16441. (when (progn
  16442. (beginning-of-line 1)
  16443. (looking-at org-todo-line-tags-regexp))
  16444. (push (org-point-in-group p 1 :headline-stars) clist)
  16445. (push (org-point-in-group p 2 :todo-keyword) clist)
  16446. (push (org-point-in-group p 4 :tags) clist))
  16447. (goto-char p)
  16448. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16449. (if (looking-at "\\[#[A-Z0-9]\\]")
  16450. (push (org-point-in-group p 0 :priority) clist)))
  16451. ((org-at-item-p)
  16452. (push (org-point-in-group p 2 :item-bullet) clist)
  16453. (push (list :item (point-at-bol)
  16454. (save-excursion (org-end-of-item) (point)))
  16455. clist)
  16456. (and (org-at-item-checkbox-p)
  16457. (push (org-point-in-group p 0 :checkbox) clist)))
  16458. ((org-at-table-p)
  16459. (push (list :table (org-table-begin) (org-table-end)) clist)
  16460. (if (memq 'org-formula faces)
  16461. (push (list :table-special
  16462. (previous-single-property-change p 'face)
  16463. (next-single-property-change p 'face)) clist)))
  16464. ((org-at-table-p 'any)
  16465. (push (list :table-table) clist)))
  16466. (goto-char p)
  16467. ;; Now the small context
  16468. (cond
  16469. ((org-at-timestamp-p)
  16470. (push (org-point-in-group p 0 :timestamp) clist))
  16471. ((memq 'org-link faces)
  16472. (push (list :link
  16473. (previous-single-property-change p 'face)
  16474. (next-single-property-change p 'face)) clist))
  16475. ((memq 'org-special-keyword faces)
  16476. (push (list :keyword
  16477. (previous-single-property-change p 'face)
  16478. (next-single-property-change p 'face)) clist))
  16479. ((org-on-target-p)
  16480. (push (org-point-in-group p 0 :target) clist)
  16481. (goto-char (1- (match-beginning 0)))
  16482. (if (looking-at org-radio-target-regexp)
  16483. (push (org-point-in-group p 0 :radio-target) clist))
  16484. (goto-char p))
  16485. ((setq o (car (delq nil
  16486. (mapcar
  16487. (lambda (x)
  16488. (if (memq x org-latex-fragment-image-overlays) x))
  16489. (overlays-at (point))))))
  16490. (push (list :latex-fragment
  16491. (overlay-start o) (overlay-end o)) clist)
  16492. (push (list :latex-preview
  16493. (overlay-start o) (overlay-end o)) clist))
  16494. ((org-inside-LaTeX-fragment-p)
  16495. ;; FIXME: positions wrong.
  16496. (push (list :latex-fragment (point) (point)) clist)))
  16497. (setq clist (nreverse (delq nil clist)))
  16498. clist))
  16499. ;; FIXME: Compare with at-regexp-p Do we need both?
  16500. (defun org-in-regexp (re &optional nlines visually)
  16501. "Check if point is inside a match of regexp.
  16502. Normally only the current line is checked, but you can include NLINES extra
  16503. lines both before and after point into the search.
  16504. If VISUALLY is set, require that the cursor is not after the match but
  16505. really on, so that the block visually is on the match."
  16506. (catch 'exit
  16507. (let ((pos (point))
  16508. (eol (point-at-eol (+ 1 (or nlines 0))))
  16509. (inc (if visually 1 0)))
  16510. (save-excursion
  16511. (beginning-of-line (- 1 (or nlines 0)))
  16512. (while (re-search-forward re eol t)
  16513. (if (and (<= (match-beginning 0) pos)
  16514. (>= (+ inc (match-end 0)) pos))
  16515. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16516. (defun org-at-regexp-p (regexp)
  16517. "Is point inside a match of REGEXP in the current line?"
  16518. (catch 'exit
  16519. (save-excursion
  16520. (let ((pos (point)) (end (point-at-eol)))
  16521. (beginning-of-line 1)
  16522. (while (re-search-forward regexp end t)
  16523. (if (and (<= (match-beginning 0) pos)
  16524. (>= (match-end 0) pos))
  16525. (throw 'exit t)))
  16526. nil))))
  16527. (defun org-in-regexps-block-p (start-re end-re)
  16528. "Returns t if the current point is between matches of START-RE and END-RE.
  16529. This will also return to if point is on one of the two matches."
  16530. (interactive)
  16531. (let ((p (point)))
  16532. (save-excursion
  16533. (and (or (org-at-regexp-p start-re)
  16534. (re-search-backward start-re nil t))
  16535. (re-search-forward end-re nil t)
  16536. (>= (point) p)))))
  16537. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16538. "Call `multi-occur' with buffers for all agenda files."
  16539. (interactive "sOrg-files matching: \np")
  16540. (let* ((files (org-agenda-files))
  16541. (tnames (mapcar 'file-truename files))
  16542. (extra org-agenda-text-search-extra-files)
  16543. f)
  16544. (when (eq (car extra) 'agenda-archives)
  16545. (setq extra (cdr extra))
  16546. (setq files (org-add-archive-files files)))
  16547. (while (setq f (pop extra))
  16548. (unless (member (file-truename f) tnames)
  16549. (add-to-list 'files f 'append)
  16550. (add-to-list 'tnames (file-truename f) 'append)))
  16551. (multi-occur
  16552. (mapcar (lambda (x)
  16553. (with-current-buffer
  16554. (or (get-file-buffer x) (find-file-noselect x))
  16555. (widen)
  16556. (current-buffer)))
  16557. files)
  16558. regexp)))
  16559. (if (boundp 'occur-mode-find-occurrence-hook)
  16560. ;; Emacs 23
  16561. (add-hook 'occur-mode-find-occurrence-hook
  16562. (lambda ()
  16563. (when (org-mode-p)
  16564. (org-reveal))))
  16565. ;; Emacs 22
  16566. (defadvice occur-mode-goto-occurrence
  16567. (after org-occur-reveal activate)
  16568. (and (org-mode-p) (org-reveal)))
  16569. (defadvice occur-mode-goto-occurrence-other-window
  16570. (after org-occur-reveal activate)
  16571. (and (org-mode-p) (org-reveal)))
  16572. (defadvice occur-mode-display-occurrence
  16573. (after org-occur-reveal activate)
  16574. (when (org-mode-p)
  16575. (let ((pos (occur-mode-find-occurrence)))
  16576. (with-current-buffer (marker-buffer pos)
  16577. (save-excursion
  16578. (goto-char pos)
  16579. (org-reveal)))))))
  16580. (defun org-occur-link-in-agenda-files ()
  16581. "Create a link and search for it in the agendas.
  16582. The link is not stored in `org-stored-links', it is just created
  16583. for the search purpose."
  16584. (interactive)
  16585. (let ((link (condition-case nil
  16586. (org-store-link nil)
  16587. (error "Unable to create a link to here"))))
  16588. (org-occur-in-agenda-files (regexp-quote link))))
  16589. (defun org-uniquify (list)
  16590. "Remove duplicate elements from LIST."
  16591. (let (res)
  16592. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16593. res))
  16594. (defun org-delete-all (elts list)
  16595. "Remove all elements in ELTS from LIST."
  16596. (while elts
  16597. (setq list (delete (pop elts) list)))
  16598. list)
  16599. (defun org-remove-if (predicate seq)
  16600. "Remove everything from SEQ that fulfills PREDICATE."
  16601. (let (res e)
  16602. (while seq
  16603. (setq e (pop seq))
  16604. (if (not (funcall predicate e)) (push e res)))
  16605. (nreverse res)))
  16606. (defun org-remove-if-not (predicate seq)
  16607. "Remove everything from SEQ that does not fulfill PREDICATE."
  16608. (let (res e)
  16609. (while seq
  16610. (setq e (pop seq))
  16611. (if (funcall predicate e) (push e res)))
  16612. (nreverse res)))
  16613. (defun org-back-over-empty-lines ()
  16614. "Move backwards over whitespace, to the beginning of the first empty line.
  16615. Returns the number of empty lines passed."
  16616. (let ((pos (point)))
  16617. (skip-chars-backward " \t\n\r")
  16618. (beginning-of-line 2)
  16619. (goto-char (min (point) pos))
  16620. (count-lines (point) pos)))
  16621. (defun org-skip-whitespace ()
  16622. (skip-chars-forward " \t\n\r"))
  16623. (defun org-point-in-group (point group &optional context)
  16624. "Check if POINT is in match-group GROUP.
  16625. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16626. match. If the match group does ot exist or point is not inside it,
  16627. return nil."
  16628. (and (match-beginning group)
  16629. (>= point (match-beginning group))
  16630. (<= point (match-end group))
  16631. (if context
  16632. (list context (match-beginning group) (match-end group))
  16633. t)))
  16634. (defun org-switch-to-buffer-other-window (&rest args)
  16635. "Switch to buffer in a second window on the current frame.
  16636. In particular, do not allow pop-up frames.
  16637. Returns the newly created buffer."
  16638. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16639. special-display-function)
  16640. (apply 'switch-to-buffer-other-window args)))
  16641. (defun org-combine-plists (&rest plists)
  16642. "Create a single property list from all plists in PLISTS.
  16643. The process starts by copying the first list, and then setting properties
  16644. from the other lists. Settings in the last list are the most significant
  16645. ones and overrule settings in the other lists."
  16646. (let ((rtn (copy-sequence (pop plists)))
  16647. p v ls)
  16648. (while plists
  16649. (setq ls (pop plists))
  16650. (while ls
  16651. (setq p (pop ls) v (pop ls))
  16652. (setq rtn (plist-put rtn p v))))
  16653. rtn))
  16654. (defun org-move-line-down (arg)
  16655. "Move the current line down. With prefix argument, move it past ARG lines."
  16656. (interactive "p")
  16657. (let ((col (current-column))
  16658. beg end pos)
  16659. (beginning-of-line 1) (setq beg (point))
  16660. (beginning-of-line 2) (setq end (point))
  16661. (beginning-of-line (+ 1 arg))
  16662. (setq pos (move-marker (make-marker) (point)))
  16663. (insert (delete-and-extract-region beg end))
  16664. (goto-char pos)
  16665. (org-move-to-column col)))
  16666. (defun org-move-line-up (arg)
  16667. "Move the current line up. With prefix argument, move it past ARG lines."
  16668. (interactive "p")
  16669. (let ((col (current-column))
  16670. beg end pos)
  16671. (beginning-of-line 1) (setq beg (point))
  16672. (beginning-of-line 2) (setq end (point))
  16673. (beginning-of-line (- arg))
  16674. (setq pos (move-marker (make-marker) (point)))
  16675. (insert (delete-and-extract-region beg end))
  16676. (goto-char pos)
  16677. (org-move-to-column col)))
  16678. (defun org-replace-escapes (string table)
  16679. "Replace %-escapes in STRING with values in TABLE.
  16680. TABLE is an association list with keys like \"%a\" and string values.
  16681. The sequences in STRING may contain normal field width and padding information,
  16682. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16683. so values can contain further %-escapes if they are define later in TABLE."
  16684. (let ((tbl (copy-alist table))
  16685. (case-fold-search nil)
  16686. (pchg 0)
  16687. e re rpl)
  16688. (while (setq e (pop tbl))
  16689. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16690. (when (and (cdr e) (string-match re (cdr e)))
  16691. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16692. (safe "SREF"))
  16693. (add-text-properties 0 3 (list 'sref sref) safe)
  16694. (setcdr e (replace-match safe t t (cdr e)))))
  16695. (while (string-match re string)
  16696. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16697. (cdr e)))
  16698. (setq string (replace-match rpl t t string))))
  16699. (while (setq pchg (next-property-change pchg string))
  16700. (let ((sref (get-text-property pchg 'sref string)))
  16701. (when (and sref (string-match "SREF" string pchg))
  16702. (setq string (replace-match sref t t string)))))
  16703. string))
  16704. (defun org-sublist (list start end)
  16705. "Return a section of LIST, from START to END.
  16706. Counting starts at 1."
  16707. (let (rtn (c start))
  16708. (setq list (nthcdr (1- start) list))
  16709. (while (and list (<= c end))
  16710. (push (pop list) rtn)
  16711. (setq c (1+ c)))
  16712. (nreverse rtn)))
  16713. (defun org-find-base-buffer-visiting (file)
  16714. "Like `find-buffer-visiting' but always return the base buffer and
  16715. not an indirect buffer."
  16716. (let ((buf (or (get-file-buffer file)
  16717. (find-buffer-visiting file))))
  16718. (if buf
  16719. (or (buffer-base-buffer buf) buf)
  16720. nil)))
  16721. (defun org-image-file-name-regexp (&optional extensions)
  16722. "Return regexp matching the file names of images.
  16723. If EXTENSIONS is given, only match these."
  16724. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16725. (image-file-name-regexp)
  16726. (let ((image-file-name-extensions
  16727. (or extensions
  16728. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16729. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16730. (concat "\\."
  16731. (regexp-opt (nconc (mapcar 'upcase
  16732. image-file-name-extensions)
  16733. image-file-name-extensions)
  16734. t)
  16735. "\\'"))))
  16736. (defun org-file-image-p (file &optional extensions)
  16737. "Return non-nil if FILE is an image."
  16738. (save-match-data
  16739. (string-match (org-image-file-name-regexp extensions) file)))
  16740. (defun org-get-cursor-date ()
  16741. "Return the date at cursor in as a time.
  16742. This works in the calendar and in the agenda, anywhere else it just
  16743. returns the current time."
  16744. (let (date day defd)
  16745. (cond
  16746. ((eq major-mode 'calendar-mode)
  16747. (setq date (calendar-cursor-to-date)
  16748. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16749. ((eq major-mode 'org-agenda-mode)
  16750. (setq day (get-text-property (point) 'day))
  16751. (if day
  16752. (setq date (calendar-gregorian-from-absolute day)
  16753. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16754. (nth 2 date))))))
  16755. (or defd (current-time))))
  16756. (defvar org-agenda-action-marker (make-marker)
  16757. "Marker pointing to the entry for the next agenda action.")
  16758. (defun org-mark-entry-for-agenda-action ()
  16759. "Mark the current entry as target of an agenda action.
  16760. Agenda actions are actions executed from the agenda with the key `k',
  16761. which make use of the date at the cursor."
  16762. (interactive)
  16763. (move-marker org-agenda-action-marker
  16764. (save-excursion (org-back-to-heading t) (point))
  16765. (current-buffer))
  16766. (message
  16767. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16768. ;;; Paragraph filling stuff.
  16769. ;; We want this to be just right, so use the full arsenal.
  16770. (defun org-indent-line-function ()
  16771. "Indent line like previous, but further if previous was headline or item."
  16772. (interactive)
  16773. (let* ((pos (point))
  16774. (itemp (org-at-item-p))
  16775. (case-fold-search t)
  16776. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16777. column bpos bcol tpos tcol bullet btype bullet-type)
  16778. ;; Find the previous relevant line
  16779. (beginning-of-line 1)
  16780. (cond
  16781. ((looking-at "#") (setq column 0))
  16782. ((looking-at "\\*+ ") (setq column 0))
  16783. ((and (looking-at "[ \t]*:END:")
  16784. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16785. (save-excursion
  16786. (goto-char (1- (match-beginning 1)))
  16787. (setq column (current-column))))
  16788. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16789. (save-excursion
  16790. (re-search-backward
  16791. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16792. (setq column (org-get-indentation (match-string 0))))
  16793. (t
  16794. (beginning-of-line 0)
  16795. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16796. (not (looking-at "[ \t]*:END:"))
  16797. (not (looking-at org-drawer-regexp)))
  16798. (beginning-of-line 0))
  16799. (cond
  16800. ((looking-at "\\*+[ \t]+")
  16801. (if (not org-adapt-indentation)
  16802. (setq column 0)
  16803. (goto-char (match-end 0))
  16804. (setq column (current-column))))
  16805. ((looking-at org-drawer-regexp)
  16806. (goto-char (1- (match-beginning 1)))
  16807. (setq column (current-column)))
  16808. ((looking-at "\\([ \t]*\\):END:")
  16809. (goto-char (match-end 1))
  16810. (setq column (current-column)))
  16811. ((org-in-item-p)
  16812. (org-beginning-of-item)
  16813. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16814. (setq bpos (match-beginning 1) tpos (match-end 0)
  16815. bcol (progn (goto-char bpos) (current-column))
  16816. tcol (progn (goto-char tpos) (current-column))
  16817. bullet (match-string 1)
  16818. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16819. (if (> tcol (+ bcol org-description-max-indent))
  16820. (setq tcol (+ bcol 5)))
  16821. (if (not itemp)
  16822. (setq column tcol)
  16823. (goto-char pos)
  16824. (beginning-of-line 1)
  16825. (if (looking-at "\\S-")
  16826. (progn
  16827. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16828. (setq bullet (match-string 1)
  16829. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16830. (setq column (if (equal btype bullet-type) bcol tcol)))
  16831. (setq column (org-get-indentation)))))
  16832. (t (setq column (org-get-indentation))))))
  16833. (goto-char pos)
  16834. (if (<= (current-column) (current-indentation))
  16835. (org-indent-line-to column)
  16836. (save-excursion (org-indent-line-to column)))
  16837. (setq column (current-column))
  16838. (beginning-of-line 1)
  16839. (if (looking-at
  16840. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16841. (replace-match (concat (match-string 1)
  16842. (format org-property-format
  16843. (match-string 2) (match-string 3)))
  16844. t t))
  16845. (org-move-to-column column)))
  16846. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16847. "Variable to store copy of `adaptive-fill-regexp'.
  16848. Since `adaptive-fill-regexp' is set to never match, we need to
  16849. store a backup of its value before entering `org-mode' so that
  16850. the functionality can be provided as a fall-back.")
  16851. (defun org-set-autofill-regexps ()
  16852. (interactive)
  16853. ;; In the paragraph separator we include headlines, because filling
  16854. ;; text in a line directly attached to a headline would otherwise
  16855. ;; fill the headline as well.
  16856. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16857. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16858. ;; The paragraph starter includes hand-formatted lists.
  16859. (org-set-local
  16860. 'paragraph-start
  16861. (concat
  16862. "\f" "\\|"
  16863. "[ ]*$" "\\|"
  16864. "\\*+ " "\\|"
  16865. "[ \t]*#" "\\|"
  16866. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16867. "[ \t]*[:|]" "\\|"
  16868. "\\$\\$" "\\|"
  16869. "\\\\\\(begin\\|end\\|[][]\\)"))
  16870. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16871. ;; But only if the user has not turned off tables or fixed-width regions
  16872. (org-set-local
  16873. 'auto-fill-inhibit-regexp
  16874. (concat "\\*+ \\|#\\+"
  16875. "\\|[ \t]*" org-keyword-time-regexp
  16876. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16877. (concat
  16878. "\\|[ \t]*["
  16879. (if org-enable-table-editor "|" "")
  16880. (if org-enable-fixed-width-editor ":" "")
  16881. "]"))))
  16882. ;; We use our own fill-paragraph function, to make sure that tables
  16883. ;; and fixed-width regions are not wrapped. That function will pass
  16884. ;; through to `fill-paragraph' when appropriate.
  16885. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16886. ;; Adaptive filling: To get full control, first make sure that
  16887. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16888. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  16889. (org-set-local 'org-adaptive-fill-regexp-backup
  16890. adaptive-fill-regexp))
  16891. (org-set-local 'adaptive-fill-regexp "\000")
  16892. (org-set-local 'adaptive-fill-function
  16893. 'org-adaptive-fill-function)
  16894. (org-set-local
  16895. 'align-mode-rules-list
  16896. '((org-in-buffer-settings
  16897. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16898. (modes . '(org-mode))))))
  16899. (defun org-fill-paragraph (&optional justify)
  16900. "Re-align a table, pass through to fill-paragraph if no table."
  16901. (let ((table-p (org-at-table-p))
  16902. (table.el-p (org-at-table.el-p)))
  16903. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16904. (save-excursion (goto-char (point-at-bol))
  16905. (looking-at outline-regexp)))
  16906. t) ; skip headlines
  16907. (table.el-p t) ; skip table.el tables
  16908. (table-p (org-table-align) t) ; align org-mode tables
  16909. (t nil)))) ; call paragraph-fill
  16910. ;; For reference, this is the default value of adaptive-fill-regexp
  16911. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16912. (defun org-adaptive-fill-function ()
  16913. "Return a fill prefix for org-mode files.
  16914. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16915. work correctly."
  16916. (cond
  16917. ;; Comment line
  16918. ((looking-at "#[ \t]+")
  16919. (match-string-no-properties 0))
  16920. ;; Description list
  16921. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16922. (save-excursion
  16923. (if (> (match-end 1) (+ (match-beginning 1)
  16924. org-description-max-indent))
  16925. (goto-char (+ (match-beginning 1) 5))
  16926. (goto-char (match-end 0)))
  16927. (make-string (current-column) ?\ )))
  16928. ;; Ordered or unordered list
  16929. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16930. (save-excursion
  16931. (goto-char (match-end 0))
  16932. (make-string (current-column) ?\ )))
  16933. ;; Other text
  16934. ((looking-at org-adaptive-fill-regexp-backup)
  16935. (match-string-no-properties 0))))
  16936. ;;; Other stuff.
  16937. (defun org-toggle-fixed-width-section (arg)
  16938. "Toggle the fixed-width export.
  16939. If there is no active region, the QUOTE keyword at the current headline is
  16940. inserted or removed. When present, it causes the text between this headline
  16941. and the next to be exported as fixed-width text, and unmodified.
  16942. If there is an active region, this command adds or removes a colon as the
  16943. first character of this line. If the first character of a line is a colon,
  16944. this line is also exported in fixed-width font."
  16945. (interactive "P")
  16946. (let* ((cc 0)
  16947. (regionp (org-region-active-p))
  16948. (beg (if regionp (region-beginning) (point)))
  16949. (end (if regionp (region-end)))
  16950. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16951. (case-fold-search nil)
  16952. (re "[ \t]*\\(: \\)")
  16953. off)
  16954. (if regionp
  16955. (save-excursion
  16956. (goto-char beg)
  16957. (setq cc (current-column))
  16958. (beginning-of-line 1)
  16959. (setq off (looking-at re))
  16960. (while (> nlines 0)
  16961. (setq nlines (1- nlines))
  16962. (beginning-of-line 1)
  16963. (cond
  16964. (arg
  16965. (org-move-to-column cc t)
  16966. (insert ": \n")
  16967. (forward-line -1))
  16968. ((and off (looking-at re))
  16969. (replace-match "" t t nil 1))
  16970. ((not off) (org-move-to-column cc t) (insert ": ")))
  16971. (forward-line 1)))
  16972. (save-excursion
  16973. (org-back-to-heading)
  16974. (if (looking-at (concat outline-regexp
  16975. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16976. (replace-match "" t t nil 1)
  16977. (if (looking-at outline-regexp)
  16978. (progn
  16979. (goto-char (match-end 0))
  16980. (insert org-quote-string " "))))))))
  16981. (defun org-reftex-citation ()
  16982. "Use reftex-citation to insert a citation into the buffer.
  16983. This looks for a line like
  16984. #+BIBLIOGRAPHY: foo plain option:-d
  16985. and derives from it that foo.bib is the bibliography file relevant
  16986. for this document. It then installs the necessary environment for RefTeX
  16987. to work in this buffer and calls `reftex-citation' to insert a citation
  16988. into the buffer.
  16989. Export of such citations to both LaTeX and HTML is handled by the contributed
  16990. package org-exp-bibtex by Taru Karttunen."
  16991. (interactive)
  16992. (let ((reftex-docstruct-symbol 'rds)
  16993. (reftex-cite-format "\\cite{%l}")
  16994. rds bib)
  16995. (save-excursion
  16996. (save-restriction
  16997. (widen)
  16998. (let ((case-fold-search t)
  16999. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17000. (if (not (save-excursion
  17001. (or (re-search-forward re nil t)
  17002. (re-search-backward re nil t))))
  17003. (error "No bibliography defined in file")
  17004. (setq bib (concat (match-string 1) ".bib")
  17005. rds (list (list 'bib bib)))))))
  17006. (call-interactively 'reftex-citation)))
  17007. ;;;; Functions extending outline functionality
  17008. (defun org-beginning-of-line (&optional arg)
  17009. "Go to the beginning of the current line. If that is invisible, continue
  17010. to a visible line beginning. This makes the function of C-a more intuitive.
  17011. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17012. first attempt, and only move to after the tags when the cursor is already
  17013. beyond the end of the headline."
  17014. (interactive "P")
  17015. (let ((pos (point))
  17016. (special (if (consp org-special-ctrl-a/e)
  17017. (car org-special-ctrl-a/e)
  17018. org-special-ctrl-a/e))
  17019. refpos)
  17020. (if (org-bound-and-true-p line-move-visual)
  17021. (beginning-of-visual-line 1)
  17022. (beginning-of-line 1))
  17023. (if (and arg (fboundp 'move-beginning-of-line))
  17024. (call-interactively 'move-beginning-of-line)
  17025. (if (bobp)
  17026. nil
  17027. (backward-char 1)
  17028. (if (org-truely-invisible-p)
  17029. (while (and (not (bobp)) (org-truely-invisible-p))
  17030. (backward-char 1)
  17031. (beginning-of-line 1))
  17032. (forward-char 1))))
  17033. (when special
  17034. (cond
  17035. ((and (looking-at org-complex-heading-regexp)
  17036. (= (char-after (match-end 1)) ?\ ))
  17037. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17038. (point-at-eol)))
  17039. (goto-char
  17040. (if (eq special t)
  17041. (cond ((> pos refpos) refpos)
  17042. ((= pos (point)) refpos)
  17043. (t (point)))
  17044. (cond ((> pos (point)) (point))
  17045. ((not (eq last-command this-command)) (point))
  17046. (t refpos)))))
  17047. ((org-at-item-p)
  17048. (goto-char
  17049. (if (eq special t)
  17050. (cond ((> pos (match-end 4)) (match-end 4))
  17051. ((= pos (point)) (match-end 4))
  17052. (t (point)))
  17053. (cond ((> pos (point)) (point))
  17054. ((not (eq last-command this-command)) (point))
  17055. (t (match-end 4))))))))
  17056. (org-no-warnings
  17057. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17058. (defun org-end-of-line (&optional arg)
  17059. "Go to the end of the line.
  17060. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17061. first attempt, and only move to after the tags when the cursor is already
  17062. beyond the end of the headline."
  17063. (interactive "P")
  17064. (let ((special (if (consp org-special-ctrl-a/e)
  17065. (cdr org-special-ctrl-a/e)
  17066. org-special-ctrl-a/e)))
  17067. (if (or (not special)
  17068. (not (org-on-heading-p))
  17069. arg)
  17070. (call-interactively
  17071. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17072. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17073. (t 'end-of-line)))
  17074. (let ((pos (point)))
  17075. (beginning-of-line 1)
  17076. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  17077. (if (eq special t)
  17078. (if (or (< pos (match-beginning 1))
  17079. (= pos (match-end 0)))
  17080. (goto-char (match-beginning 1))
  17081. (goto-char (match-end 0)))
  17082. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17083. (goto-char (match-end 0))
  17084. (goto-char (match-beginning 1))))
  17085. (call-interactively (if (fboundp 'move-end-of-line)
  17086. 'move-end-of-line
  17087. 'end-of-line)))))
  17088. (org-no-warnings
  17089. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17090. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17091. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17092. (define-key org-mode-map [home] 'org-beginning-of-line)
  17093. (define-key org-mode-map [end] 'org-end-of-line)
  17094. (defun org-backward-sentence (&optional arg)
  17095. "Go to beginning of sentence, or beginning of table field.
  17096. This will call `backward-sentence' or `org-table-beginning-of-field',
  17097. depending on context."
  17098. (interactive "P")
  17099. (cond
  17100. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17101. (t (call-interactively 'backward-sentence))))
  17102. (defun org-forward-sentence (&optional arg)
  17103. "Go to end of sentence, or end of table field.
  17104. This will call `forward-sentence' or `org-table-end-of-field',
  17105. depending on context."
  17106. (interactive "P")
  17107. (cond
  17108. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17109. (t (call-interactively 'forward-sentence))))
  17110. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17111. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17112. (defun org-kill-line (&optional arg)
  17113. "Kill line, to tags or end of line."
  17114. (interactive "P")
  17115. (cond
  17116. ((or (not org-special-ctrl-k)
  17117. (bolp)
  17118. (not (org-on-heading-p)))
  17119. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17120. org-ctrl-k-protect-subtree)
  17121. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17122. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17123. (error "C-k aborted - would kill hidden subtree")))
  17124. (call-interactively 'kill-line))
  17125. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  17126. (kill-region (point) (match-beginning 1))
  17127. (org-set-tags nil t))
  17128. (t (kill-region (point) (point-at-eol)))))
  17129. (define-key org-mode-map "\C-k" 'org-kill-line)
  17130. (defun org-yank (&optional arg)
  17131. "Yank. If the kill is a subtree, treat it specially.
  17132. This command will look at the current kill and check if is a single
  17133. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17134. cursor is at the beginning of a line or after the stars of a currently
  17135. empty headline, then the yank is handled specially. How exactly depends
  17136. on the value of the following variables, both set by default.
  17137. org-yank-folded-subtrees
  17138. When set, the subtree(s) will be folded after insertion, but only
  17139. if doing so would now swallow text after the yanked text.
  17140. org-yank-adjusted-subtrees
  17141. When set, the subtree will be promoted or demoted in order to
  17142. fit into the local outline tree structure, which means that the level
  17143. will be adjusted so that it becomes the smaller one of the two
  17144. *visible* surrounding headings.
  17145. Any prefix to this command will cause `yank' to be called directly with
  17146. no special treatment. In particular, a simple `C-u' prefix will just
  17147. plainly yank the text as it is.
  17148. \[1] The test checks if the first non-white line is a heading
  17149. and if there are no other headings with fewer stars."
  17150. (interactive "P")
  17151. (org-yank-generic 'yank arg))
  17152. (defun org-yank-generic (command arg)
  17153. "Perform some yank-like command.
  17154. This function implements the behavior described in the `org-yank'
  17155. documentation. However, it has been generalized to work for any
  17156. interactive command with similar behavior."
  17157. ;; pretend to be command COMMAND
  17158. (setq this-command command)
  17159. (if arg
  17160. (call-interactively command)
  17161. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17162. (and (org-kill-is-subtree-p)
  17163. (or (bolp)
  17164. (and (looking-at "[ \t]*$")
  17165. (string-match
  17166. "\\`\\*+\\'"
  17167. (buffer-substring (point-at-bol) (point)))))))
  17168. swallowp)
  17169. (cond
  17170. ((and subtreep org-yank-folded-subtrees)
  17171. (let ((beg (point))
  17172. end)
  17173. (if (and subtreep org-yank-adjusted-subtrees)
  17174. (org-paste-subtree nil nil 'for-yank)
  17175. (call-interactively command))
  17176. (setq end (point))
  17177. (goto-char beg)
  17178. (when (and (bolp) subtreep
  17179. (not (setq swallowp
  17180. (org-yank-folding-would-swallow-text beg end))))
  17181. (or (looking-at outline-regexp)
  17182. (re-search-forward (concat "^" outline-regexp) end t))
  17183. (while (and (< (point) end) (looking-at outline-regexp))
  17184. (hide-subtree)
  17185. (org-cycle-show-empty-lines 'folded)
  17186. (condition-case nil
  17187. (outline-forward-same-level 1)
  17188. (error (goto-char end)))))
  17189. (when swallowp
  17190. (message
  17191. "Inserted text not folded because that would swallow text"))
  17192. (goto-char end)
  17193. (skip-chars-forward " \t\n\r")
  17194. (beginning-of-line 1)
  17195. (push-mark beg 'nomsg)))
  17196. ((and subtreep org-yank-adjusted-subtrees)
  17197. (let ((beg (point-at-bol)))
  17198. (org-paste-subtree nil nil 'for-yank)
  17199. (push-mark beg 'nomsg)))
  17200. (t
  17201. (call-interactively command))))))
  17202. (defun org-yank-folding-would-swallow-text (beg end)
  17203. "Would hide-subtree at BEG swallow any text after END?"
  17204. (let (level)
  17205. (save-excursion
  17206. (goto-char beg)
  17207. (when (or (looking-at outline-regexp)
  17208. (re-search-forward (concat "^" outline-regexp) end t))
  17209. (setq level (org-outline-level)))
  17210. (goto-char end)
  17211. (skip-chars-forward " \t\r\n\v\f")
  17212. (if (or (eobp)
  17213. (and (bolp) (looking-at org-outline-regexp)
  17214. (<= (org-outline-level) level)))
  17215. nil ; Nothing would be swallowed
  17216. t)))) ; something would swallow
  17217. (define-key org-mode-map "\C-y" 'org-yank)
  17218. (defun org-invisible-p ()
  17219. "Check if point is at a character currently not visible."
  17220. ;; Early versions of noutline don't have `outline-invisible-p'.
  17221. (if (fboundp 'outline-invisible-p)
  17222. (outline-invisible-p)
  17223. (get-char-property (point) 'invisible)))
  17224. (defun org-truely-invisible-p ()
  17225. "Check if point is at a character currently not visible.
  17226. This version does not only check the character property, but also
  17227. `visible-mode'."
  17228. ;; Early versions of noutline don't have `outline-invisible-p'.
  17229. (if (org-bound-and-true-p visible-mode)
  17230. nil
  17231. (if (fboundp 'outline-invisible-p)
  17232. (outline-invisible-p)
  17233. (get-char-property (point) 'invisible))))
  17234. (defun org-invisible-p2 ()
  17235. "Check if point is at a character currently not visible."
  17236. (save-excursion
  17237. (if (and (eolp) (not (bobp))) (backward-char 1))
  17238. ;; Early versions of noutline don't have `outline-invisible-p'.
  17239. (if (fboundp 'outline-invisible-p)
  17240. (outline-invisible-p)
  17241. (get-char-property (point) 'invisible))))
  17242. (defun org-back-to-heading (&optional invisible-ok)
  17243. "Call `outline-back-to-heading', but provide a better error message."
  17244. (condition-case nil
  17245. (outline-back-to-heading invisible-ok)
  17246. (error (error "Before first headline at position %d in buffer %s"
  17247. (point) (current-buffer)))))
  17248. (defun org-beginning-of-defun ()
  17249. "Go to the beginning of the subtree, i.e. back to the heading."
  17250. (org-back-to-heading))
  17251. (defun org-end-of-defun ()
  17252. "Go to the end of the subtree."
  17253. (org-end-of-subtree nil t))
  17254. (defun org-before-first-heading-p ()
  17255. "Before first heading?"
  17256. (save-excursion
  17257. (null (re-search-backward "^\\*+ " nil t))))
  17258. (defun org-on-heading-p (&optional ignored)
  17259. (outline-on-heading-p t))
  17260. (defun org-at-heading-p (&optional ignored)
  17261. (outline-on-heading-p t))
  17262. (defun org-point-at-end-of-empty-headline ()
  17263. "If point is at the end of an empty headline, return t, else nil.
  17264. If the heading only contains a TODO keyword, it is still still considered
  17265. empty."
  17266. (and (looking-at "[ \t]*$")
  17267. (save-excursion
  17268. (beginning-of-line 1)
  17269. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  17270. "\\)?[ \t]*$")))))
  17271. (defun org-at-heading-or-item-p ()
  17272. (or (org-on-heading-p) (org-at-item-p)))
  17273. (defun org-on-target-p ()
  17274. (or (org-in-regexp org-radio-target-regexp)
  17275. (org-in-regexp org-target-regexp)))
  17276. (defun org-up-heading-all (arg)
  17277. "Move to the heading line of which the present line is a subheading.
  17278. This function considers both visible and invisible heading lines.
  17279. With argument, move up ARG levels."
  17280. (if (fboundp 'outline-up-heading-all)
  17281. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17282. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17283. (defun org-up-heading-safe ()
  17284. "Move to the heading line of which the present line is a subheading.
  17285. This version will not throw an error. It will return the level of the
  17286. headline found, or nil if no higher level is found.
  17287. Also, this function will be a lot faster than `outline-up-heading',
  17288. because it relies on stars being the outline starters. This can really
  17289. make a significant difference in outlines with very many siblings."
  17290. (let (start-level re)
  17291. (org-back-to-heading t)
  17292. (setq start-level (funcall outline-level))
  17293. (if (equal start-level 1)
  17294. nil
  17295. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17296. (if (re-search-backward re nil t)
  17297. (funcall outline-level)))))
  17298. (defun org-first-sibling-p ()
  17299. "Is this heading the first child of its parents?"
  17300. (interactive)
  17301. (let ((re (concat "^" outline-regexp))
  17302. level l)
  17303. (unless (org-at-heading-p t)
  17304. (error "Not at a heading"))
  17305. (setq level (funcall outline-level))
  17306. (save-excursion
  17307. (if (not (re-search-backward re nil t))
  17308. t
  17309. (setq l (funcall outline-level))
  17310. (< l level)))))
  17311. (defun org-goto-sibling (&optional previous)
  17312. "Goto the next sibling, even if it is invisible.
  17313. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17314. when a sibling was found. When none is found, return nil and don't
  17315. move point."
  17316. (let ((fun (if previous 're-search-backward 're-search-forward))
  17317. (pos (point))
  17318. (re (concat "^" outline-regexp))
  17319. level l)
  17320. (when (condition-case nil (org-back-to-heading t) (error nil))
  17321. (setq level (funcall outline-level))
  17322. (catch 'exit
  17323. (or previous (forward-char 1))
  17324. (while (funcall fun re nil t)
  17325. (setq l (funcall outline-level))
  17326. (when (< l level) (goto-char pos) (throw 'exit nil))
  17327. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17328. (goto-char pos)
  17329. nil))))
  17330. (defun org-show-siblings ()
  17331. "Show all siblings of the current headline."
  17332. (save-excursion
  17333. (while (org-goto-sibling) (org-flag-heading nil)))
  17334. (save-excursion
  17335. (while (org-goto-sibling 'previous)
  17336. (org-flag-heading nil))))
  17337. (defun org-show-hidden-entry ()
  17338. "Show an entry where even the heading is hidden."
  17339. (save-excursion
  17340. (org-show-entry)))
  17341. (defun org-flag-heading (flag &optional entry)
  17342. "Flag the current heading. FLAG non-nil means make invisible.
  17343. When ENTRY is non-nil, show the entire entry."
  17344. (save-excursion
  17345. (org-back-to-heading t)
  17346. ;; Check if we should show the entire entry
  17347. (if entry
  17348. (progn
  17349. (org-show-entry)
  17350. (save-excursion
  17351. (and (outline-next-heading)
  17352. (org-flag-heading nil))))
  17353. (outline-flag-region (max (point-min) (1- (point)))
  17354. (save-excursion (outline-end-of-heading) (point))
  17355. flag))))
  17356. (defun org-get-next-sibling ()
  17357. "Move to next heading of the same level, and return point.
  17358. If there is no such heading, return nil.
  17359. This is like outline-next-sibling, but invisible headings are ok."
  17360. (let ((level (funcall outline-level)))
  17361. (outline-next-heading)
  17362. (while (and (not (eobp)) (> (funcall outline-level) level))
  17363. (outline-next-heading))
  17364. (if (or (eobp) (< (funcall outline-level) level))
  17365. nil
  17366. (point))))
  17367. (defun org-get-last-sibling ()
  17368. "Move to previous heading of the same level, and return point.
  17369. If there is no such heading, return nil."
  17370. (let ((opoint (point))
  17371. (level (funcall outline-level)))
  17372. (outline-previous-heading)
  17373. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17374. (while (and (> (funcall outline-level) level)
  17375. (not (bobp)))
  17376. (outline-previous-heading))
  17377. (if (< (funcall outline-level) level)
  17378. nil
  17379. (point)))))
  17380. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17381. ;; This contains an exact copy of the original function, but it uses
  17382. ;; `org-back-to-heading', to make it work also in invisible
  17383. ;; trees. And is uses an invisible-OK argument.
  17384. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17385. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17386. ;; with many children and grandchildren etc, this can be much faster
  17387. ;; than the outline version.
  17388. (org-back-to-heading invisible-OK)
  17389. (let ((first t)
  17390. (level (funcall outline-level)))
  17391. (if (and (org-mode-p) (< level 1000))
  17392. ;; A true heading (not a plain list item), in Org-mode
  17393. ;; This means we can easily find the end by looking
  17394. ;; only for the right number of stars. Using a regexp to do
  17395. ;; this is so much faster than using a Lisp loop.
  17396. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17397. (forward-char 1)
  17398. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17399. ;; something else, do it the slow way
  17400. (while (and (not (eobp))
  17401. (or first (> (funcall outline-level) level)))
  17402. (setq first nil)
  17403. (outline-next-heading)))
  17404. (unless to-heading
  17405. (if (memq (preceding-char) '(?\n ?\^M))
  17406. (progn
  17407. ;; Go to end of line before heading
  17408. (forward-char -1)
  17409. (if (memq (preceding-char) '(?\n ?\^M))
  17410. ;; leave blank line before heading
  17411. (forward-char -1))))))
  17412. (point))
  17413. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17414. "Use Org version in org-mode, for dramatic speed-up."
  17415. (if (eq major-mode 'org-mode)
  17416. (progn
  17417. (org-end-of-subtree nil t)
  17418. (unless (eobp) (backward-char 1)))
  17419. ad-do-it))
  17420. (defun org-forward-same-level (arg &optional invisible-ok)
  17421. "Move forward to the arg'th subheading at same level as this one.
  17422. Stop at the first and last subheadings of a superior heading."
  17423. (interactive "p")
  17424. (org-back-to-heading invisible-ok)
  17425. (org-on-heading-p)
  17426. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17427. (re (format "^\\*\\{1,%d\\} " level))
  17428. l)
  17429. (forward-char 1)
  17430. (while (> arg 0)
  17431. (while (and (re-search-forward re nil 'move)
  17432. (setq l (- (match-end 0) (match-beginning 0) 1))
  17433. (= l level)
  17434. (not invisible-ok)
  17435. (progn (backward-char 1) (org-invisible-p)))
  17436. (if (< l level) (setq arg 1)))
  17437. (setq arg (1- arg)))
  17438. (beginning-of-line 1)))
  17439. (defun org-backward-same-level (arg &optional invisible-ok)
  17440. "Move backward to the arg'th subheading at same level as this one.
  17441. Stop at the first and last subheadings of a superior heading."
  17442. (interactive "p")
  17443. (org-back-to-heading)
  17444. (org-on-heading-p)
  17445. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17446. (re (format "^\\*\\{1,%d\\} " level))
  17447. l)
  17448. (while (> arg 0)
  17449. (while (and (re-search-backward re nil 'move)
  17450. (setq l (- (match-end 0) (match-beginning 0) 1))
  17451. (= l level)
  17452. (not invisible-ok)
  17453. (org-invisible-p))
  17454. (if (< l level) (setq arg 1)))
  17455. (setq arg (1- arg)))))
  17456. (defun org-show-subtree ()
  17457. "Show everything after this heading at deeper levels."
  17458. (outline-flag-region
  17459. (point)
  17460. (save-excursion
  17461. (org-end-of-subtree t t))
  17462. nil))
  17463. (defun org-show-entry ()
  17464. "Show the body directly following this heading.
  17465. Show the heading too, if it is currently invisible."
  17466. (interactive)
  17467. (save-excursion
  17468. (condition-case nil
  17469. (progn
  17470. (org-back-to-heading t)
  17471. (outline-flag-region
  17472. (max (point-min) (1- (point)))
  17473. (save-excursion
  17474. (if (re-search-forward
  17475. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17476. (match-beginning 1)
  17477. (point-max)))
  17478. nil)
  17479. (org-cycle-hide-drawers 'children))
  17480. (error nil))))
  17481. (defun org-make-options-regexp (kwds &optional extra)
  17482. "Make a regular expression for keyword lines."
  17483. (concat
  17484. "^"
  17485. "#?[ \t]*\\+\\("
  17486. (mapconcat 'regexp-quote kwds "\\|")
  17487. (if extra (concat "\\|" extra))
  17488. "\\):[ \t]*"
  17489. "\\(.*\\)"))
  17490. ;; Make isearch reveal the necessary context
  17491. (defun org-isearch-end ()
  17492. "Reveal context after isearch exits."
  17493. (when isearch-success ; only if search was successful
  17494. (if (featurep 'xemacs)
  17495. ;; Under XEmacs, the hook is run in the correct place,
  17496. ;; we directly show the context.
  17497. (org-show-context 'isearch)
  17498. ;; In Emacs the hook runs *before* restoring the overlays.
  17499. ;; So we have to use a one-time post-command-hook to do this.
  17500. ;; (Emacs 22 has a special variable, see function `org-mode')
  17501. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17502. isearch-mode-end-hook-quit)
  17503. ;; Only when the isearch was not quitted.
  17504. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17505. 'append 'local)))))
  17506. (defun org-isearch-post-command ()
  17507. "Remove self from hook, and show context."
  17508. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17509. (org-show-context 'isearch))
  17510. ;;;; Integration with and fixes for other packages
  17511. ;;; Imenu support
  17512. (defvar org-imenu-markers nil
  17513. "All markers currently used by Imenu.")
  17514. (make-variable-buffer-local 'org-imenu-markers)
  17515. (defun org-imenu-new-marker (&optional pos)
  17516. "Return a new marker for use by Imenu, and remember the marker."
  17517. (let ((m (make-marker)))
  17518. (move-marker m (or pos (point)))
  17519. (push m org-imenu-markers)
  17520. m))
  17521. (defun org-imenu-get-tree ()
  17522. "Produce the index for Imenu."
  17523. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17524. (setq org-imenu-markers nil)
  17525. (let* ((n org-imenu-depth)
  17526. (re (concat "^" outline-regexp))
  17527. (subs (make-vector (1+ n) nil))
  17528. (last-level 0)
  17529. m level head)
  17530. (save-excursion
  17531. (save-restriction
  17532. (widen)
  17533. (goto-char (point-max))
  17534. (while (re-search-backward re nil t)
  17535. (setq level (org-reduced-level (funcall outline-level)))
  17536. (when (<= level n)
  17537. (looking-at org-complex-heading-regexp)
  17538. (setq head (org-link-display-format
  17539. (org-match-string-no-properties 4))
  17540. m (org-imenu-new-marker))
  17541. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17542. (if (>= level last-level)
  17543. (push (cons head m) (aref subs level))
  17544. (push (cons head (aref subs (1+ level))) (aref subs level))
  17545. (loop for i from (1+ level) to n do (aset subs i nil)))
  17546. (setq last-level level)))))
  17547. (aref subs 1)))
  17548. (eval-after-load "imenu"
  17549. '(progn
  17550. (add-hook 'imenu-after-jump-hook
  17551. (lambda ()
  17552. (if (eq major-mode 'org-mode)
  17553. (org-show-context 'org-goto))))))
  17554. (defun org-link-display-format (link)
  17555. "Replace a link with either the description, or the link target
  17556. if no description is present"
  17557. (save-match-data
  17558. (if (string-match org-bracket-link-analytic-regexp link)
  17559. (replace-match (if (match-end 5)
  17560. (match-string 5 link)
  17561. (concat (match-string 1 link)
  17562. (match-string 3 link)))
  17563. nil t link)
  17564. link)))
  17565. ;; Speedbar support
  17566. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17567. "Overlay marking the agenda restriction line in speedbar.")
  17568. (overlay-put org-speedbar-restriction-lock-overlay
  17569. 'face 'org-agenda-restriction-lock)
  17570. (overlay-put org-speedbar-restriction-lock-overlay
  17571. 'help-echo "Agendas are currently limited to this item.")
  17572. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17573. (defun org-speedbar-set-agenda-restriction ()
  17574. "Restrict future agenda commands to the location at point in speedbar.
  17575. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17576. (interactive)
  17577. (require 'org-agenda)
  17578. (let (p m tp np dir txt)
  17579. (cond
  17580. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17581. 'org-imenu t))
  17582. (setq m (get-text-property p 'org-imenu-marker))
  17583. (with-current-buffer (marker-buffer m)
  17584. (goto-char m)
  17585. (org-agenda-set-restriction-lock 'subtree)))
  17586. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17587. 'speedbar-function 'speedbar-find-file))
  17588. (setq tp (previous-single-property-change
  17589. (1+ p) 'speedbar-function)
  17590. np (next-single-property-change
  17591. tp 'speedbar-function)
  17592. dir (speedbar-line-directory)
  17593. txt (buffer-substring-no-properties (or tp (point-min))
  17594. (or np (point-max))))
  17595. (with-current-buffer (find-file-noselect
  17596. (let ((default-directory dir))
  17597. (expand-file-name txt)))
  17598. (unless (org-mode-p)
  17599. (error "Cannot restrict to non-Org-mode file"))
  17600. (org-agenda-set-restriction-lock 'file)))
  17601. (t (error "Don't know how to restrict Org-mode's agenda")))
  17602. (move-overlay org-speedbar-restriction-lock-overlay
  17603. (point-at-bol) (point-at-eol))
  17604. (setq current-prefix-arg nil)
  17605. (org-agenda-maybe-redo)))
  17606. (eval-after-load "speedbar"
  17607. '(progn
  17608. (speedbar-add-supported-extension ".org")
  17609. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17610. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17611. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17612. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17613. (add-hook 'speedbar-visiting-tag-hook
  17614. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17615. ;;; Fixes and Hacks for problems with other packages
  17616. ;; Make flyspell not check words in links, to not mess up our keymap
  17617. (defun org-mode-flyspell-verify ()
  17618. "Don't let flyspell put overlays at active buttons."
  17619. (and (not (get-text-property (point) 'keymap))
  17620. (not (get-text-property (point) 'org-no-flyspell))))
  17621. (defun org-remove-flyspell-overlays-in (beg end)
  17622. "Remove flyspell overlays in region."
  17623. (and (org-bound-and-true-p flyspell-mode)
  17624. (fboundp 'flyspell-delete-region-overlays)
  17625. (flyspell-delete-region-overlays beg end))
  17626. (add-text-properties beg end '(org-no-flyspell t)))
  17627. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17628. (eval-after-load "bookmark"
  17629. '(if (boundp 'bookmark-after-jump-hook)
  17630. ;; We can use the hook
  17631. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17632. ;; Hook not available, use advice
  17633. (defadvice bookmark-jump (after org-make-visible activate)
  17634. "Make the position visible."
  17635. (org-bookmark-jump-unhide))))
  17636. ;; Make sure saveplace shows the location if it was hidden
  17637. (eval-after-load "saveplace"
  17638. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17639. "Make the position visible."
  17640. (org-bookmark-jump-unhide)))
  17641. ;; Make sure ecb shows the location if it was hidden
  17642. (eval-after-load "ecb"
  17643. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17644. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17645. (if (eq major-mode 'org-mode)
  17646. (org-show-context))))
  17647. (defun org-bookmark-jump-unhide ()
  17648. "Unhide the current position, to show the bookmark location."
  17649. (and (org-mode-p)
  17650. (or (org-invisible-p)
  17651. (save-excursion (goto-char (max (point-min) (1- (point))))
  17652. (org-invisible-p)))
  17653. (org-show-context 'bookmark-jump)))
  17654. ;; Make session.el ignore our circular variable
  17655. (eval-after-load "session"
  17656. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17657. ;;;; Experimental code
  17658. (defun org-closed-in-range ()
  17659. "Sparse tree of items closed in a certain time range.
  17660. Still experimental, may disappear in the future."
  17661. (interactive)
  17662. ;; Get the time interval from the user.
  17663. (let* ((time1 (org-float-time
  17664. (org-read-date nil 'to-time nil "Starting date: ")))
  17665. (time2 (org-float-time
  17666. (org-read-date nil 'to-time nil "End date:")))
  17667. ;; callback function
  17668. (callback (lambda ()
  17669. (let ((time
  17670. (org-float-time
  17671. (apply 'encode-time
  17672. (org-parse-time-string
  17673. (match-string 1))))))
  17674. ;; check if time in interval
  17675. (and (>= time time1) (<= time time2))))))
  17676. ;; make tree, check each match with the callback
  17677. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17678. ;;;; Finish up
  17679. (provide 'org)
  17680. (run-hooks 'org-load-hook)
  17681. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17682. ;;; org.el ends here